n2_core.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* n2_core.c: Niagara2 Stream Processing Unit (SPU) crypto support.
  3. *
  4. * Copyright (C) 2010, 2011 David S. Miller <davem@davemloft.net>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/crypto.h>
  16. #include <crypto/md5.h>
  17. #include <crypto/sha1.h>
  18. #include <crypto/sha2.h>
  19. #include <crypto/aes.h>
  20. #include <crypto/internal/des.h>
  21. #include <linux/mutex.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched.h>
  24. #include <crypto/internal/hash.h>
  25. #include <crypto/internal/skcipher.h>
  26. #include <crypto/scatterwalk.h>
  27. #include <crypto/algapi.h>
  28. #include <asm/hypervisor.h>
  29. #include <asm/mdesc.h>
  30. #include "n2_core.h"
  31. #define DRV_MODULE_NAME "n2_crypto"
  32. #define DRV_MODULE_VERSION "0.2"
  33. #define DRV_MODULE_RELDATE "July 28, 2011"
  34. static const char version[] =
  35. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  36. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  37. MODULE_DESCRIPTION("Niagara2 Crypto driver");
  38. MODULE_LICENSE("GPL");
  39. MODULE_VERSION(DRV_MODULE_VERSION);
  40. #define N2_CRA_PRIORITY 200
  41. static DEFINE_MUTEX(spu_lock);
  42. struct spu_queue {
  43. cpumask_t sharing;
  44. unsigned long qhandle;
  45. spinlock_t lock;
  46. u8 q_type;
  47. void *q;
  48. unsigned long head;
  49. unsigned long tail;
  50. struct list_head jobs;
  51. unsigned long devino;
  52. char irq_name[32];
  53. unsigned int irq;
  54. struct list_head list;
  55. };
  56. struct spu_qreg {
  57. struct spu_queue *queue;
  58. unsigned long type;
  59. };
  60. static struct spu_queue **cpu_to_cwq;
  61. static struct spu_queue **cpu_to_mau;
  62. static unsigned long spu_next_offset(struct spu_queue *q, unsigned long off)
  63. {
  64. if (q->q_type == HV_NCS_QTYPE_MAU) {
  65. off += MAU_ENTRY_SIZE;
  66. if (off == (MAU_ENTRY_SIZE * MAU_NUM_ENTRIES))
  67. off = 0;
  68. } else {
  69. off += CWQ_ENTRY_SIZE;
  70. if (off == (CWQ_ENTRY_SIZE * CWQ_NUM_ENTRIES))
  71. off = 0;
  72. }
  73. return off;
  74. }
  75. struct n2_request_common {
  76. struct list_head entry;
  77. unsigned int offset;
  78. };
  79. #define OFFSET_NOT_RUNNING (~(unsigned int)0)
  80. /* An async job request records the final tail value it used in
  81. * n2_request_common->offset, test to see if that offset is in
  82. * the range old_head, new_head, inclusive.
  83. */
  84. static inline bool job_finished(struct spu_queue *q, unsigned int offset,
  85. unsigned long old_head, unsigned long new_head)
  86. {
  87. if (old_head <= new_head) {
  88. if (offset > old_head && offset <= new_head)
  89. return true;
  90. } else {
  91. if (offset > old_head || offset <= new_head)
  92. return true;
  93. }
  94. return false;
  95. }
  96. /* When the HEAD marker is unequal to the actual HEAD, we get
  97. * a virtual device INO interrupt. We should process the
  98. * completed CWQ entries and adjust the HEAD marker to clear
  99. * the IRQ.
  100. */
  101. static irqreturn_t cwq_intr(int irq, void *dev_id)
  102. {
  103. unsigned long off, new_head, hv_ret;
  104. struct spu_queue *q = dev_id;
  105. pr_err("CPU[%d]: Got CWQ interrupt for qhdl[%lx]\n",
  106. smp_processor_id(), q->qhandle);
  107. spin_lock(&q->lock);
  108. hv_ret = sun4v_ncs_gethead(q->qhandle, &new_head);
  109. pr_err("CPU[%d]: CWQ gethead[%lx] hv_ret[%lu]\n",
  110. smp_processor_id(), new_head, hv_ret);
  111. for (off = q->head; off != new_head; off = spu_next_offset(q, off)) {
  112. /* XXX ... XXX */
  113. }
  114. hv_ret = sun4v_ncs_sethead_marker(q->qhandle, new_head);
  115. if (hv_ret == HV_EOK)
  116. q->head = new_head;
  117. spin_unlock(&q->lock);
  118. return IRQ_HANDLED;
  119. }
  120. static irqreturn_t mau_intr(int irq, void *dev_id)
  121. {
  122. struct spu_queue *q = dev_id;
  123. unsigned long head, hv_ret;
  124. spin_lock(&q->lock);
  125. pr_err("CPU[%d]: Got MAU interrupt for qhdl[%lx]\n",
  126. smp_processor_id(), q->qhandle);
  127. hv_ret = sun4v_ncs_gethead(q->qhandle, &head);
  128. pr_err("CPU[%d]: MAU gethead[%lx] hv_ret[%lu]\n",
  129. smp_processor_id(), head, hv_ret);
  130. sun4v_ncs_sethead_marker(q->qhandle, head);
  131. spin_unlock(&q->lock);
  132. return IRQ_HANDLED;
  133. }
  134. static void *spu_queue_next(struct spu_queue *q, void *cur)
  135. {
  136. return q->q + spu_next_offset(q, cur - q->q);
  137. }
  138. static int spu_queue_num_free(struct spu_queue *q)
  139. {
  140. unsigned long head = q->head;
  141. unsigned long tail = q->tail;
  142. unsigned long end = (CWQ_ENTRY_SIZE * CWQ_NUM_ENTRIES);
  143. unsigned long diff;
  144. if (head > tail)
  145. diff = head - tail;
  146. else
  147. diff = (end - tail) + head;
  148. return (diff / CWQ_ENTRY_SIZE) - 1;
  149. }
  150. static void *spu_queue_alloc(struct spu_queue *q, int num_entries)
  151. {
  152. int avail = spu_queue_num_free(q);
  153. if (avail >= num_entries)
  154. return q->q + q->tail;
  155. return NULL;
  156. }
  157. static unsigned long spu_queue_submit(struct spu_queue *q, void *last)
  158. {
  159. unsigned long hv_ret, new_tail;
  160. new_tail = spu_next_offset(q, last - q->q);
  161. hv_ret = sun4v_ncs_settail(q->qhandle, new_tail);
  162. if (hv_ret == HV_EOK)
  163. q->tail = new_tail;
  164. return hv_ret;
  165. }
  166. static u64 control_word_base(unsigned int len, unsigned int hmac_key_len,
  167. int enc_type, int auth_type,
  168. unsigned int hash_len,
  169. bool sfas, bool sob, bool eob, bool encrypt,
  170. int opcode)
  171. {
  172. u64 word = (len - 1) & CONTROL_LEN;
  173. word |= ((u64) opcode << CONTROL_OPCODE_SHIFT);
  174. word |= ((u64) enc_type << CONTROL_ENC_TYPE_SHIFT);
  175. word |= ((u64) auth_type << CONTROL_AUTH_TYPE_SHIFT);
  176. if (sfas)
  177. word |= CONTROL_STORE_FINAL_AUTH_STATE;
  178. if (sob)
  179. word |= CONTROL_START_OF_BLOCK;
  180. if (eob)
  181. word |= CONTROL_END_OF_BLOCK;
  182. if (encrypt)
  183. word |= CONTROL_ENCRYPT;
  184. if (hmac_key_len)
  185. word |= ((u64) (hmac_key_len - 1)) << CONTROL_HMAC_KEY_LEN_SHIFT;
  186. if (hash_len)
  187. word |= ((u64) (hash_len - 1)) << CONTROL_HASH_LEN_SHIFT;
  188. return word;
  189. }
  190. #if 0
  191. static inline bool n2_should_run_async(struct spu_queue *qp, int this_len)
  192. {
  193. if (this_len >= 64 ||
  194. qp->head != qp->tail)
  195. return true;
  196. return false;
  197. }
  198. #endif
  199. struct n2_ahash_alg {
  200. struct list_head entry;
  201. const u8 *hash_zero;
  202. const u8 *hash_init;
  203. u8 hw_op_hashsz;
  204. u8 digest_size;
  205. u8 auth_type;
  206. u8 hmac_type;
  207. struct ahash_alg alg;
  208. };
  209. static inline struct n2_ahash_alg *n2_ahash_alg(struct crypto_tfm *tfm)
  210. {
  211. struct crypto_alg *alg = tfm->__crt_alg;
  212. struct ahash_alg *ahash_alg;
  213. ahash_alg = container_of(alg, struct ahash_alg, halg.base);
  214. return container_of(ahash_alg, struct n2_ahash_alg, alg);
  215. }
  216. struct n2_hmac_alg {
  217. const char *child_alg;
  218. struct n2_ahash_alg derived;
  219. };
  220. static inline struct n2_hmac_alg *n2_hmac_alg(struct crypto_tfm *tfm)
  221. {
  222. struct crypto_alg *alg = tfm->__crt_alg;
  223. struct ahash_alg *ahash_alg;
  224. ahash_alg = container_of(alg, struct ahash_alg, halg.base);
  225. return container_of(ahash_alg, struct n2_hmac_alg, derived.alg);
  226. }
  227. struct n2_hash_ctx {
  228. struct crypto_ahash *fallback_tfm;
  229. };
  230. #define N2_HASH_KEY_MAX 32 /* HW limit for all HMAC requests */
  231. struct n2_hmac_ctx {
  232. struct n2_hash_ctx base;
  233. struct crypto_shash *child_shash;
  234. int hash_key_len;
  235. unsigned char hash_key[N2_HASH_KEY_MAX];
  236. };
  237. struct n2_hash_req_ctx {
  238. union {
  239. struct md5_state md5;
  240. struct sha1_state sha1;
  241. struct sha256_state sha256;
  242. } u;
  243. struct ahash_request fallback_req;
  244. };
  245. static int n2_hash_async_init(struct ahash_request *req)
  246. {
  247. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  248. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  249. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  250. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  251. rctx->fallback_req.base.flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  252. return crypto_ahash_init(&rctx->fallback_req);
  253. }
  254. static int n2_hash_async_update(struct ahash_request *req)
  255. {
  256. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  257. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  258. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  259. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  260. rctx->fallback_req.base.flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  261. rctx->fallback_req.nbytes = req->nbytes;
  262. rctx->fallback_req.src = req->src;
  263. return crypto_ahash_update(&rctx->fallback_req);
  264. }
  265. static int n2_hash_async_final(struct ahash_request *req)
  266. {
  267. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  268. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  269. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  270. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  271. rctx->fallback_req.base.flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  272. rctx->fallback_req.result = req->result;
  273. return crypto_ahash_final(&rctx->fallback_req);
  274. }
  275. static int n2_hash_async_finup(struct ahash_request *req)
  276. {
  277. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  278. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  279. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  280. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  281. rctx->fallback_req.base.flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  282. rctx->fallback_req.nbytes = req->nbytes;
  283. rctx->fallback_req.src = req->src;
  284. rctx->fallback_req.result = req->result;
  285. return crypto_ahash_finup(&rctx->fallback_req);
  286. }
  287. static int n2_hash_async_noimport(struct ahash_request *req, const void *in)
  288. {
  289. return -ENOSYS;
  290. }
  291. static int n2_hash_async_noexport(struct ahash_request *req, void *out)
  292. {
  293. return -ENOSYS;
  294. }
  295. static int n2_hash_cra_init(struct crypto_tfm *tfm)
  296. {
  297. const char *fallback_driver_name = crypto_tfm_alg_name(tfm);
  298. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  299. struct n2_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  300. struct crypto_ahash *fallback_tfm;
  301. int err;
  302. fallback_tfm = crypto_alloc_ahash(fallback_driver_name, 0,
  303. CRYPTO_ALG_NEED_FALLBACK);
  304. if (IS_ERR(fallback_tfm)) {
  305. pr_warn("Fallback driver '%s' could not be loaded!\n",
  306. fallback_driver_name);
  307. err = PTR_ERR(fallback_tfm);
  308. goto out;
  309. }
  310. crypto_ahash_set_reqsize(ahash, (sizeof(struct n2_hash_req_ctx) +
  311. crypto_ahash_reqsize(fallback_tfm)));
  312. ctx->fallback_tfm = fallback_tfm;
  313. return 0;
  314. out:
  315. return err;
  316. }
  317. static void n2_hash_cra_exit(struct crypto_tfm *tfm)
  318. {
  319. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  320. struct n2_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  321. crypto_free_ahash(ctx->fallback_tfm);
  322. }
  323. static int n2_hmac_cra_init(struct crypto_tfm *tfm)
  324. {
  325. const char *fallback_driver_name = crypto_tfm_alg_name(tfm);
  326. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  327. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(ahash);
  328. struct n2_hmac_alg *n2alg = n2_hmac_alg(tfm);
  329. struct crypto_ahash *fallback_tfm;
  330. struct crypto_shash *child_shash;
  331. int err;
  332. fallback_tfm = crypto_alloc_ahash(fallback_driver_name, 0,
  333. CRYPTO_ALG_NEED_FALLBACK);
  334. if (IS_ERR(fallback_tfm)) {
  335. pr_warn("Fallback driver '%s' could not be loaded!\n",
  336. fallback_driver_name);
  337. err = PTR_ERR(fallback_tfm);
  338. goto out;
  339. }
  340. child_shash = crypto_alloc_shash(n2alg->child_alg, 0, 0);
  341. if (IS_ERR(child_shash)) {
  342. pr_warn("Child shash '%s' could not be loaded!\n",
  343. n2alg->child_alg);
  344. err = PTR_ERR(child_shash);
  345. goto out_free_fallback;
  346. }
  347. crypto_ahash_set_reqsize(ahash, (sizeof(struct n2_hash_req_ctx) +
  348. crypto_ahash_reqsize(fallback_tfm)));
  349. ctx->child_shash = child_shash;
  350. ctx->base.fallback_tfm = fallback_tfm;
  351. return 0;
  352. out_free_fallback:
  353. crypto_free_ahash(fallback_tfm);
  354. out:
  355. return err;
  356. }
  357. static void n2_hmac_cra_exit(struct crypto_tfm *tfm)
  358. {
  359. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  360. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(ahash);
  361. crypto_free_ahash(ctx->base.fallback_tfm);
  362. crypto_free_shash(ctx->child_shash);
  363. }
  364. static int n2_hmac_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  365. unsigned int keylen)
  366. {
  367. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(tfm);
  368. struct crypto_shash *child_shash = ctx->child_shash;
  369. struct crypto_ahash *fallback_tfm;
  370. int err, bs, ds;
  371. fallback_tfm = ctx->base.fallback_tfm;
  372. err = crypto_ahash_setkey(fallback_tfm, key, keylen);
  373. if (err)
  374. return err;
  375. bs = crypto_shash_blocksize(child_shash);
  376. ds = crypto_shash_digestsize(child_shash);
  377. BUG_ON(ds > N2_HASH_KEY_MAX);
  378. if (keylen > bs) {
  379. err = crypto_shash_tfm_digest(child_shash, key, keylen,
  380. ctx->hash_key);
  381. if (err)
  382. return err;
  383. keylen = ds;
  384. } else if (keylen <= N2_HASH_KEY_MAX)
  385. memcpy(ctx->hash_key, key, keylen);
  386. ctx->hash_key_len = keylen;
  387. return err;
  388. }
  389. static unsigned long wait_for_tail(struct spu_queue *qp)
  390. {
  391. unsigned long head, hv_ret;
  392. do {
  393. hv_ret = sun4v_ncs_gethead(qp->qhandle, &head);
  394. if (hv_ret != HV_EOK) {
  395. pr_err("Hypervisor error on gethead\n");
  396. break;
  397. }
  398. if (head == qp->tail) {
  399. qp->head = head;
  400. break;
  401. }
  402. } while (1);
  403. return hv_ret;
  404. }
  405. static unsigned long submit_and_wait_for_tail(struct spu_queue *qp,
  406. struct cwq_initial_entry *ent)
  407. {
  408. unsigned long hv_ret = spu_queue_submit(qp, ent);
  409. if (hv_ret == HV_EOK)
  410. hv_ret = wait_for_tail(qp);
  411. return hv_ret;
  412. }
  413. static int n2_do_async_digest(struct ahash_request *req,
  414. unsigned int auth_type, unsigned int digest_size,
  415. unsigned int result_size, void *hash_loc,
  416. unsigned long auth_key, unsigned int auth_key_len)
  417. {
  418. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  419. struct cwq_initial_entry *ent;
  420. struct crypto_hash_walk walk;
  421. struct spu_queue *qp;
  422. unsigned long flags;
  423. int err = -ENODEV;
  424. int nbytes, cpu;
  425. /* The total effective length of the operation may not
  426. * exceed 2^16.
  427. */
  428. if (unlikely(req->nbytes > (1 << 16))) {
  429. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  430. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  431. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  432. rctx->fallback_req.base.flags =
  433. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  434. rctx->fallback_req.nbytes = req->nbytes;
  435. rctx->fallback_req.src = req->src;
  436. rctx->fallback_req.result = req->result;
  437. return crypto_ahash_digest(&rctx->fallback_req);
  438. }
  439. nbytes = crypto_hash_walk_first(req, &walk);
  440. cpu = get_cpu();
  441. qp = cpu_to_cwq[cpu];
  442. if (!qp)
  443. goto out;
  444. spin_lock_irqsave(&qp->lock, flags);
  445. /* XXX can do better, improve this later by doing a by-hand scatterlist
  446. * XXX walk, etc.
  447. */
  448. ent = qp->q + qp->tail;
  449. ent->control = control_word_base(nbytes, auth_key_len, 0,
  450. auth_type, digest_size,
  451. false, true, false, false,
  452. OPCODE_INPLACE_BIT |
  453. OPCODE_AUTH_MAC);
  454. ent->src_addr = __pa(walk.data);
  455. ent->auth_key_addr = auth_key;
  456. ent->auth_iv_addr = __pa(hash_loc);
  457. ent->final_auth_state_addr = 0UL;
  458. ent->enc_key_addr = 0UL;
  459. ent->enc_iv_addr = 0UL;
  460. ent->dest_addr = __pa(hash_loc);
  461. nbytes = crypto_hash_walk_done(&walk, 0);
  462. while (nbytes > 0) {
  463. ent = spu_queue_next(qp, ent);
  464. ent->control = (nbytes - 1);
  465. ent->src_addr = __pa(walk.data);
  466. ent->auth_key_addr = 0UL;
  467. ent->auth_iv_addr = 0UL;
  468. ent->final_auth_state_addr = 0UL;
  469. ent->enc_key_addr = 0UL;
  470. ent->enc_iv_addr = 0UL;
  471. ent->dest_addr = 0UL;
  472. nbytes = crypto_hash_walk_done(&walk, 0);
  473. }
  474. ent->control |= CONTROL_END_OF_BLOCK;
  475. if (submit_and_wait_for_tail(qp, ent) != HV_EOK)
  476. err = -EINVAL;
  477. else
  478. err = 0;
  479. spin_unlock_irqrestore(&qp->lock, flags);
  480. if (!err)
  481. memcpy(req->result, hash_loc, result_size);
  482. out:
  483. put_cpu();
  484. return err;
  485. }
  486. static int n2_hash_async_digest(struct ahash_request *req)
  487. {
  488. struct n2_ahash_alg *n2alg = n2_ahash_alg(req->base.tfm);
  489. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  490. int ds;
  491. ds = n2alg->digest_size;
  492. if (unlikely(req->nbytes == 0)) {
  493. memcpy(req->result, n2alg->hash_zero, ds);
  494. return 0;
  495. }
  496. memcpy(&rctx->u, n2alg->hash_init, n2alg->hw_op_hashsz);
  497. return n2_do_async_digest(req, n2alg->auth_type,
  498. n2alg->hw_op_hashsz, ds,
  499. &rctx->u, 0UL, 0);
  500. }
  501. static int n2_hmac_async_digest(struct ahash_request *req)
  502. {
  503. struct n2_hmac_alg *n2alg = n2_hmac_alg(req->base.tfm);
  504. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  505. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  506. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(tfm);
  507. int ds;
  508. ds = n2alg->derived.digest_size;
  509. if (unlikely(req->nbytes == 0) ||
  510. unlikely(ctx->hash_key_len > N2_HASH_KEY_MAX)) {
  511. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  512. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  513. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  514. rctx->fallback_req.base.flags =
  515. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  516. rctx->fallback_req.nbytes = req->nbytes;
  517. rctx->fallback_req.src = req->src;
  518. rctx->fallback_req.result = req->result;
  519. return crypto_ahash_digest(&rctx->fallback_req);
  520. }
  521. memcpy(&rctx->u, n2alg->derived.hash_init,
  522. n2alg->derived.hw_op_hashsz);
  523. return n2_do_async_digest(req, n2alg->derived.hmac_type,
  524. n2alg->derived.hw_op_hashsz, ds,
  525. &rctx->u,
  526. __pa(&ctx->hash_key),
  527. ctx->hash_key_len);
  528. }
  529. struct n2_skcipher_context {
  530. int key_len;
  531. int enc_type;
  532. union {
  533. u8 aes[AES_MAX_KEY_SIZE];
  534. u8 des[DES_KEY_SIZE];
  535. u8 des3[3 * DES_KEY_SIZE];
  536. } key;
  537. };
  538. #define N2_CHUNK_ARR_LEN 16
  539. struct n2_crypto_chunk {
  540. struct list_head entry;
  541. unsigned long iv_paddr : 44;
  542. unsigned long arr_len : 20;
  543. unsigned long dest_paddr;
  544. unsigned long dest_final;
  545. struct {
  546. unsigned long src_paddr : 44;
  547. unsigned long src_len : 20;
  548. } arr[N2_CHUNK_ARR_LEN];
  549. };
  550. struct n2_request_context {
  551. struct skcipher_walk walk;
  552. struct list_head chunk_list;
  553. struct n2_crypto_chunk chunk;
  554. u8 temp_iv[16];
  555. };
  556. /* The SPU allows some level of flexibility for partial cipher blocks
  557. * being specified in a descriptor.
  558. *
  559. * It merely requires that every descriptor's length field is at least
  560. * as large as the cipher block size. This means that a cipher block
  561. * can span at most 2 descriptors. However, this does not allow a
  562. * partial block to span into the final descriptor as that would
  563. * violate the rule (since every descriptor's length must be at lest
  564. * the block size). So, for example, assuming an 8 byte block size:
  565. *
  566. * 0xe --> 0xa --> 0x8
  567. *
  568. * is a valid length sequence, whereas:
  569. *
  570. * 0xe --> 0xb --> 0x7
  571. *
  572. * is not a valid sequence.
  573. */
  574. struct n2_skcipher_alg {
  575. struct list_head entry;
  576. u8 enc_type;
  577. struct skcipher_alg skcipher;
  578. };
  579. static inline struct n2_skcipher_alg *n2_skcipher_alg(struct crypto_skcipher *tfm)
  580. {
  581. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  582. return container_of(alg, struct n2_skcipher_alg, skcipher);
  583. }
  584. static int n2_aes_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  585. unsigned int keylen)
  586. {
  587. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  588. struct n2_skcipher_context *ctx = crypto_tfm_ctx(tfm);
  589. struct n2_skcipher_alg *n2alg = n2_skcipher_alg(skcipher);
  590. ctx->enc_type = (n2alg->enc_type & ENC_TYPE_CHAINING_MASK);
  591. switch (keylen) {
  592. case AES_KEYSIZE_128:
  593. ctx->enc_type |= ENC_TYPE_ALG_AES128;
  594. break;
  595. case AES_KEYSIZE_192:
  596. ctx->enc_type |= ENC_TYPE_ALG_AES192;
  597. break;
  598. case AES_KEYSIZE_256:
  599. ctx->enc_type |= ENC_TYPE_ALG_AES256;
  600. break;
  601. default:
  602. return -EINVAL;
  603. }
  604. ctx->key_len = keylen;
  605. memcpy(ctx->key.aes, key, keylen);
  606. return 0;
  607. }
  608. static int n2_des_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  609. unsigned int keylen)
  610. {
  611. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  612. struct n2_skcipher_context *ctx = crypto_tfm_ctx(tfm);
  613. struct n2_skcipher_alg *n2alg = n2_skcipher_alg(skcipher);
  614. int err;
  615. err = verify_skcipher_des_key(skcipher, key);
  616. if (err)
  617. return err;
  618. ctx->enc_type = n2alg->enc_type;
  619. ctx->key_len = keylen;
  620. memcpy(ctx->key.des, key, keylen);
  621. return 0;
  622. }
  623. static int n2_3des_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  624. unsigned int keylen)
  625. {
  626. struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  627. struct n2_skcipher_context *ctx = crypto_tfm_ctx(tfm);
  628. struct n2_skcipher_alg *n2alg = n2_skcipher_alg(skcipher);
  629. int err;
  630. err = verify_skcipher_des3_key(skcipher, key);
  631. if (err)
  632. return err;
  633. ctx->enc_type = n2alg->enc_type;
  634. ctx->key_len = keylen;
  635. memcpy(ctx->key.des3, key, keylen);
  636. return 0;
  637. }
  638. static inline int skcipher_descriptor_len(int nbytes, unsigned int block_size)
  639. {
  640. int this_len = nbytes;
  641. this_len -= (nbytes & (block_size - 1));
  642. return this_len > (1 << 16) ? (1 << 16) : this_len;
  643. }
  644. static int __n2_crypt_chunk(struct crypto_skcipher *skcipher,
  645. struct n2_crypto_chunk *cp,
  646. struct spu_queue *qp, bool encrypt)
  647. {
  648. struct n2_skcipher_context *ctx = crypto_skcipher_ctx(skcipher);
  649. struct cwq_initial_entry *ent;
  650. bool in_place;
  651. int i;
  652. ent = spu_queue_alloc(qp, cp->arr_len);
  653. if (!ent) {
  654. pr_info("queue_alloc() of %d fails\n",
  655. cp->arr_len);
  656. return -EBUSY;
  657. }
  658. in_place = (cp->dest_paddr == cp->arr[0].src_paddr);
  659. ent->control = control_word_base(cp->arr[0].src_len,
  660. 0, ctx->enc_type, 0, 0,
  661. false, true, false, encrypt,
  662. OPCODE_ENCRYPT |
  663. (in_place ? OPCODE_INPLACE_BIT : 0));
  664. ent->src_addr = cp->arr[0].src_paddr;
  665. ent->auth_key_addr = 0UL;
  666. ent->auth_iv_addr = 0UL;
  667. ent->final_auth_state_addr = 0UL;
  668. ent->enc_key_addr = __pa(&ctx->key);
  669. ent->enc_iv_addr = cp->iv_paddr;
  670. ent->dest_addr = (in_place ? 0UL : cp->dest_paddr);
  671. for (i = 1; i < cp->arr_len; i++) {
  672. ent = spu_queue_next(qp, ent);
  673. ent->control = cp->arr[i].src_len - 1;
  674. ent->src_addr = cp->arr[i].src_paddr;
  675. ent->auth_key_addr = 0UL;
  676. ent->auth_iv_addr = 0UL;
  677. ent->final_auth_state_addr = 0UL;
  678. ent->enc_key_addr = 0UL;
  679. ent->enc_iv_addr = 0UL;
  680. ent->dest_addr = 0UL;
  681. }
  682. ent->control |= CONTROL_END_OF_BLOCK;
  683. return (spu_queue_submit(qp, ent) != HV_EOK) ? -EINVAL : 0;
  684. }
  685. static int n2_compute_chunks(struct skcipher_request *req)
  686. {
  687. struct n2_request_context *rctx = skcipher_request_ctx(req);
  688. struct skcipher_walk *walk = &rctx->walk;
  689. struct n2_crypto_chunk *chunk;
  690. unsigned long dest_prev;
  691. unsigned int tot_len;
  692. bool prev_in_place;
  693. int err, nbytes;
  694. err = skcipher_walk_async(walk, req);
  695. if (err)
  696. return err;
  697. INIT_LIST_HEAD(&rctx->chunk_list);
  698. chunk = &rctx->chunk;
  699. INIT_LIST_HEAD(&chunk->entry);
  700. chunk->iv_paddr = 0UL;
  701. chunk->arr_len = 0;
  702. chunk->dest_paddr = 0UL;
  703. prev_in_place = false;
  704. dest_prev = ~0UL;
  705. tot_len = 0;
  706. while ((nbytes = walk->nbytes) != 0) {
  707. unsigned long dest_paddr, src_paddr;
  708. bool in_place;
  709. int this_len;
  710. src_paddr = (page_to_phys(walk->src.phys.page) +
  711. walk->src.phys.offset);
  712. dest_paddr = (page_to_phys(walk->dst.phys.page) +
  713. walk->dst.phys.offset);
  714. in_place = (src_paddr == dest_paddr);
  715. this_len = skcipher_descriptor_len(nbytes, walk->blocksize);
  716. if (chunk->arr_len != 0) {
  717. if (in_place != prev_in_place ||
  718. (!prev_in_place &&
  719. dest_paddr != dest_prev) ||
  720. chunk->arr_len == N2_CHUNK_ARR_LEN ||
  721. tot_len + this_len > (1 << 16)) {
  722. chunk->dest_final = dest_prev;
  723. list_add_tail(&chunk->entry,
  724. &rctx->chunk_list);
  725. chunk = kzalloc(sizeof(*chunk), GFP_ATOMIC);
  726. if (!chunk) {
  727. err = -ENOMEM;
  728. break;
  729. }
  730. INIT_LIST_HEAD(&chunk->entry);
  731. }
  732. }
  733. if (chunk->arr_len == 0) {
  734. chunk->dest_paddr = dest_paddr;
  735. tot_len = 0;
  736. }
  737. chunk->arr[chunk->arr_len].src_paddr = src_paddr;
  738. chunk->arr[chunk->arr_len].src_len = this_len;
  739. chunk->arr_len++;
  740. dest_prev = dest_paddr + this_len;
  741. prev_in_place = in_place;
  742. tot_len += this_len;
  743. err = skcipher_walk_done(walk, nbytes - this_len);
  744. if (err)
  745. break;
  746. }
  747. if (!err && chunk->arr_len != 0) {
  748. chunk->dest_final = dest_prev;
  749. list_add_tail(&chunk->entry, &rctx->chunk_list);
  750. }
  751. return err;
  752. }
  753. static void n2_chunk_complete(struct skcipher_request *req, void *final_iv)
  754. {
  755. struct n2_request_context *rctx = skcipher_request_ctx(req);
  756. struct n2_crypto_chunk *c, *tmp;
  757. if (final_iv)
  758. memcpy(rctx->walk.iv, final_iv, rctx->walk.blocksize);
  759. list_for_each_entry_safe(c, tmp, &rctx->chunk_list, entry) {
  760. list_del(&c->entry);
  761. if (unlikely(c != &rctx->chunk))
  762. kfree(c);
  763. }
  764. }
  765. static int n2_do_ecb(struct skcipher_request *req, bool encrypt)
  766. {
  767. struct n2_request_context *rctx = skcipher_request_ctx(req);
  768. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  769. int err = n2_compute_chunks(req);
  770. struct n2_crypto_chunk *c, *tmp;
  771. unsigned long flags, hv_ret;
  772. struct spu_queue *qp;
  773. if (err)
  774. return err;
  775. qp = cpu_to_cwq[get_cpu()];
  776. err = -ENODEV;
  777. if (!qp)
  778. goto out;
  779. spin_lock_irqsave(&qp->lock, flags);
  780. list_for_each_entry_safe(c, tmp, &rctx->chunk_list, entry) {
  781. err = __n2_crypt_chunk(tfm, c, qp, encrypt);
  782. if (err)
  783. break;
  784. list_del(&c->entry);
  785. if (unlikely(c != &rctx->chunk))
  786. kfree(c);
  787. }
  788. if (!err) {
  789. hv_ret = wait_for_tail(qp);
  790. if (hv_ret != HV_EOK)
  791. err = -EINVAL;
  792. }
  793. spin_unlock_irqrestore(&qp->lock, flags);
  794. out:
  795. put_cpu();
  796. n2_chunk_complete(req, NULL);
  797. return err;
  798. }
  799. static int n2_encrypt_ecb(struct skcipher_request *req)
  800. {
  801. return n2_do_ecb(req, true);
  802. }
  803. static int n2_decrypt_ecb(struct skcipher_request *req)
  804. {
  805. return n2_do_ecb(req, false);
  806. }
  807. static int n2_do_chaining(struct skcipher_request *req, bool encrypt)
  808. {
  809. struct n2_request_context *rctx = skcipher_request_ctx(req);
  810. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  811. unsigned long flags, hv_ret, iv_paddr;
  812. int err = n2_compute_chunks(req);
  813. struct n2_crypto_chunk *c, *tmp;
  814. struct spu_queue *qp;
  815. void *final_iv_addr;
  816. final_iv_addr = NULL;
  817. if (err)
  818. return err;
  819. qp = cpu_to_cwq[get_cpu()];
  820. err = -ENODEV;
  821. if (!qp)
  822. goto out;
  823. spin_lock_irqsave(&qp->lock, flags);
  824. if (encrypt) {
  825. iv_paddr = __pa(rctx->walk.iv);
  826. list_for_each_entry_safe(c, tmp, &rctx->chunk_list,
  827. entry) {
  828. c->iv_paddr = iv_paddr;
  829. err = __n2_crypt_chunk(tfm, c, qp, true);
  830. if (err)
  831. break;
  832. iv_paddr = c->dest_final - rctx->walk.blocksize;
  833. list_del(&c->entry);
  834. if (unlikely(c != &rctx->chunk))
  835. kfree(c);
  836. }
  837. final_iv_addr = __va(iv_paddr);
  838. } else {
  839. list_for_each_entry_safe_reverse(c, tmp, &rctx->chunk_list,
  840. entry) {
  841. if (c == &rctx->chunk) {
  842. iv_paddr = __pa(rctx->walk.iv);
  843. } else {
  844. iv_paddr = (tmp->arr[tmp->arr_len-1].src_paddr +
  845. tmp->arr[tmp->arr_len-1].src_len -
  846. rctx->walk.blocksize);
  847. }
  848. if (!final_iv_addr) {
  849. unsigned long pa;
  850. pa = (c->arr[c->arr_len-1].src_paddr +
  851. c->arr[c->arr_len-1].src_len -
  852. rctx->walk.blocksize);
  853. final_iv_addr = rctx->temp_iv;
  854. memcpy(rctx->temp_iv, __va(pa),
  855. rctx->walk.blocksize);
  856. }
  857. c->iv_paddr = iv_paddr;
  858. err = __n2_crypt_chunk(tfm, c, qp, false);
  859. if (err)
  860. break;
  861. list_del(&c->entry);
  862. if (unlikely(c != &rctx->chunk))
  863. kfree(c);
  864. }
  865. }
  866. if (!err) {
  867. hv_ret = wait_for_tail(qp);
  868. if (hv_ret != HV_EOK)
  869. err = -EINVAL;
  870. }
  871. spin_unlock_irqrestore(&qp->lock, flags);
  872. out:
  873. put_cpu();
  874. n2_chunk_complete(req, err ? NULL : final_iv_addr);
  875. return err;
  876. }
  877. static int n2_encrypt_chaining(struct skcipher_request *req)
  878. {
  879. return n2_do_chaining(req, true);
  880. }
  881. static int n2_decrypt_chaining(struct skcipher_request *req)
  882. {
  883. return n2_do_chaining(req, false);
  884. }
  885. struct n2_skcipher_tmpl {
  886. const char *name;
  887. const char *drv_name;
  888. u8 block_size;
  889. u8 enc_type;
  890. struct skcipher_alg skcipher;
  891. };
  892. static const struct n2_skcipher_tmpl skcipher_tmpls[] = {
  893. /* DES: ECB CBC and CFB are supported */
  894. { .name = "ecb(des)",
  895. .drv_name = "ecb-des",
  896. .block_size = DES_BLOCK_SIZE,
  897. .enc_type = (ENC_TYPE_ALG_DES |
  898. ENC_TYPE_CHAINING_ECB),
  899. .skcipher = {
  900. .min_keysize = DES_KEY_SIZE,
  901. .max_keysize = DES_KEY_SIZE,
  902. .setkey = n2_des_setkey,
  903. .encrypt = n2_encrypt_ecb,
  904. .decrypt = n2_decrypt_ecb,
  905. },
  906. },
  907. { .name = "cbc(des)",
  908. .drv_name = "cbc-des",
  909. .block_size = DES_BLOCK_SIZE,
  910. .enc_type = (ENC_TYPE_ALG_DES |
  911. ENC_TYPE_CHAINING_CBC),
  912. .skcipher = {
  913. .ivsize = DES_BLOCK_SIZE,
  914. .min_keysize = DES_KEY_SIZE,
  915. .max_keysize = DES_KEY_SIZE,
  916. .setkey = n2_des_setkey,
  917. .encrypt = n2_encrypt_chaining,
  918. .decrypt = n2_decrypt_chaining,
  919. },
  920. },
  921. /* 3DES: ECB CBC and CFB are supported */
  922. { .name = "ecb(des3_ede)",
  923. .drv_name = "ecb-3des",
  924. .block_size = DES_BLOCK_SIZE,
  925. .enc_type = (ENC_TYPE_ALG_3DES |
  926. ENC_TYPE_CHAINING_ECB),
  927. .skcipher = {
  928. .min_keysize = 3 * DES_KEY_SIZE,
  929. .max_keysize = 3 * DES_KEY_SIZE,
  930. .setkey = n2_3des_setkey,
  931. .encrypt = n2_encrypt_ecb,
  932. .decrypt = n2_decrypt_ecb,
  933. },
  934. },
  935. { .name = "cbc(des3_ede)",
  936. .drv_name = "cbc-3des",
  937. .block_size = DES_BLOCK_SIZE,
  938. .enc_type = (ENC_TYPE_ALG_3DES |
  939. ENC_TYPE_CHAINING_CBC),
  940. .skcipher = {
  941. .ivsize = DES_BLOCK_SIZE,
  942. .min_keysize = 3 * DES_KEY_SIZE,
  943. .max_keysize = 3 * DES_KEY_SIZE,
  944. .setkey = n2_3des_setkey,
  945. .encrypt = n2_encrypt_chaining,
  946. .decrypt = n2_decrypt_chaining,
  947. },
  948. },
  949. /* AES: ECB CBC and CTR are supported */
  950. { .name = "ecb(aes)",
  951. .drv_name = "ecb-aes",
  952. .block_size = AES_BLOCK_SIZE,
  953. .enc_type = (ENC_TYPE_ALG_AES128 |
  954. ENC_TYPE_CHAINING_ECB),
  955. .skcipher = {
  956. .min_keysize = AES_MIN_KEY_SIZE,
  957. .max_keysize = AES_MAX_KEY_SIZE,
  958. .setkey = n2_aes_setkey,
  959. .encrypt = n2_encrypt_ecb,
  960. .decrypt = n2_decrypt_ecb,
  961. },
  962. },
  963. { .name = "cbc(aes)",
  964. .drv_name = "cbc-aes",
  965. .block_size = AES_BLOCK_SIZE,
  966. .enc_type = (ENC_TYPE_ALG_AES128 |
  967. ENC_TYPE_CHAINING_CBC),
  968. .skcipher = {
  969. .ivsize = AES_BLOCK_SIZE,
  970. .min_keysize = AES_MIN_KEY_SIZE,
  971. .max_keysize = AES_MAX_KEY_SIZE,
  972. .setkey = n2_aes_setkey,
  973. .encrypt = n2_encrypt_chaining,
  974. .decrypt = n2_decrypt_chaining,
  975. },
  976. },
  977. { .name = "ctr(aes)",
  978. .drv_name = "ctr-aes",
  979. .block_size = AES_BLOCK_SIZE,
  980. .enc_type = (ENC_TYPE_ALG_AES128 |
  981. ENC_TYPE_CHAINING_COUNTER),
  982. .skcipher = {
  983. .ivsize = AES_BLOCK_SIZE,
  984. .min_keysize = AES_MIN_KEY_SIZE,
  985. .max_keysize = AES_MAX_KEY_SIZE,
  986. .setkey = n2_aes_setkey,
  987. .encrypt = n2_encrypt_chaining,
  988. .decrypt = n2_encrypt_chaining,
  989. },
  990. },
  991. };
  992. #define NUM_CIPHER_TMPLS ARRAY_SIZE(skcipher_tmpls)
  993. static LIST_HEAD(skcipher_algs);
  994. struct n2_hash_tmpl {
  995. const char *name;
  996. const u8 *hash_zero;
  997. const u8 *hash_init;
  998. u8 hw_op_hashsz;
  999. u8 digest_size;
  1000. u8 statesize;
  1001. u8 block_size;
  1002. u8 auth_type;
  1003. u8 hmac_type;
  1004. };
  1005. static const __le32 n2_md5_init[MD5_HASH_WORDS] = {
  1006. cpu_to_le32(MD5_H0),
  1007. cpu_to_le32(MD5_H1),
  1008. cpu_to_le32(MD5_H2),
  1009. cpu_to_le32(MD5_H3),
  1010. };
  1011. static const u32 n2_sha1_init[SHA1_DIGEST_SIZE / 4] = {
  1012. SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4,
  1013. };
  1014. static const u32 n2_sha256_init[SHA256_DIGEST_SIZE / 4] = {
  1015. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  1016. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7,
  1017. };
  1018. static const u32 n2_sha224_init[SHA256_DIGEST_SIZE / 4] = {
  1019. SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
  1020. SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
  1021. };
  1022. static const struct n2_hash_tmpl hash_tmpls[] = {
  1023. { .name = "md5",
  1024. .hash_zero = md5_zero_message_hash,
  1025. .hash_init = (u8 *)n2_md5_init,
  1026. .auth_type = AUTH_TYPE_MD5,
  1027. .hmac_type = AUTH_TYPE_HMAC_MD5,
  1028. .hw_op_hashsz = MD5_DIGEST_SIZE,
  1029. .digest_size = MD5_DIGEST_SIZE,
  1030. .statesize = sizeof(struct md5_state),
  1031. .block_size = MD5_HMAC_BLOCK_SIZE },
  1032. { .name = "sha1",
  1033. .hash_zero = sha1_zero_message_hash,
  1034. .hash_init = (u8 *)n2_sha1_init,
  1035. .auth_type = AUTH_TYPE_SHA1,
  1036. .hmac_type = AUTH_TYPE_HMAC_SHA1,
  1037. .hw_op_hashsz = SHA1_DIGEST_SIZE,
  1038. .digest_size = SHA1_DIGEST_SIZE,
  1039. .statesize = sizeof(struct sha1_state),
  1040. .block_size = SHA1_BLOCK_SIZE },
  1041. { .name = "sha256",
  1042. .hash_zero = sha256_zero_message_hash,
  1043. .hash_init = (u8 *)n2_sha256_init,
  1044. .auth_type = AUTH_TYPE_SHA256,
  1045. .hmac_type = AUTH_TYPE_HMAC_SHA256,
  1046. .hw_op_hashsz = SHA256_DIGEST_SIZE,
  1047. .digest_size = SHA256_DIGEST_SIZE,
  1048. .statesize = sizeof(struct sha256_state),
  1049. .block_size = SHA256_BLOCK_SIZE },
  1050. { .name = "sha224",
  1051. .hash_zero = sha224_zero_message_hash,
  1052. .hash_init = (u8 *)n2_sha224_init,
  1053. .auth_type = AUTH_TYPE_SHA256,
  1054. .hmac_type = AUTH_TYPE_RESERVED,
  1055. .hw_op_hashsz = SHA256_DIGEST_SIZE,
  1056. .digest_size = SHA224_DIGEST_SIZE,
  1057. .statesize = sizeof(struct sha256_state),
  1058. .block_size = SHA224_BLOCK_SIZE },
  1059. };
  1060. #define NUM_HASH_TMPLS ARRAY_SIZE(hash_tmpls)
  1061. static LIST_HEAD(ahash_algs);
  1062. static LIST_HEAD(hmac_algs);
  1063. static int algs_registered;
  1064. static void __n2_unregister_algs(void)
  1065. {
  1066. struct n2_skcipher_alg *skcipher, *skcipher_tmp;
  1067. struct n2_ahash_alg *alg, *alg_tmp;
  1068. struct n2_hmac_alg *hmac, *hmac_tmp;
  1069. list_for_each_entry_safe(skcipher, skcipher_tmp, &skcipher_algs, entry) {
  1070. crypto_unregister_skcipher(&skcipher->skcipher);
  1071. list_del(&skcipher->entry);
  1072. kfree(skcipher);
  1073. }
  1074. list_for_each_entry_safe(hmac, hmac_tmp, &hmac_algs, derived.entry) {
  1075. crypto_unregister_ahash(&hmac->derived.alg);
  1076. list_del(&hmac->derived.entry);
  1077. kfree(hmac);
  1078. }
  1079. list_for_each_entry_safe(alg, alg_tmp, &ahash_algs, entry) {
  1080. crypto_unregister_ahash(&alg->alg);
  1081. list_del(&alg->entry);
  1082. kfree(alg);
  1083. }
  1084. }
  1085. static int n2_skcipher_init_tfm(struct crypto_skcipher *tfm)
  1086. {
  1087. crypto_skcipher_set_reqsize(tfm, sizeof(struct n2_request_context));
  1088. return 0;
  1089. }
  1090. static int __n2_register_one_skcipher(const struct n2_skcipher_tmpl *tmpl)
  1091. {
  1092. struct n2_skcipher_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1093. struct skcipher_alg *alg;
  1094. int err;
  1095. if (!p)
  1096. return -ENOMEM;
  1097. alg = &p->skcipher;
  1098. *alg = tmpl->skcipher;
  1099. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1100. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-n2", tmpl->drv_name);
  1101. alg->base.cra_priority = N2_CRA_PRIORITY;
  1102. alg->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
  1103. CRYPTO_ALG_ALLOCATES_MEMORY;
  1104. alg->base.cra_blocksize = tmpl->block_size;
  1105. p->enc_type = tmpl->enc_type;
  1106. alg->base.cra_ctxsize = sizeof(struct n2_skcipher_context);
  1107. alg->base.cra_module = THIS_MODULE;
  1108. alg->init = n2_skcipher_init_tfm;
  1109. list_add(&p->entry, &skcipher_algs);
  1110. err = crypto_register_skcipher(alg);
  1111. if (err) {
  1112. pr_err("%s alg registration failed\n", alg->base.cra_name);
  1113. list_del(&p->entry);
  1114. kfree(p);
  1115. } else {
  1116. pr_info("%s alg registered\n", alg->base.cra_name);
  1117. }
  1118. return err;
  1119. }
  1120. static int __n2_register_one_hmac(struct n2_ahash_alg *n2ahash)
  1121. {
  1122. struct n2_hmac_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1123. struct ahash_alg *ahash;
  1124. struct crypto_alg *base;
  1125. int err;
  1126. if (!p)
  1127. return -ENOMEM;
  1128. p->child_alg = n2ahash->alg.halg.base.cra_name;
  1129. memcpy(&p->derived, n2ahash, sizeof(struct n2_ahash_alg));
  1130. INIT_LIST_HEAD(&p->derived.entry);
  1131. ahash = &p->derived.alg;
  1132. ahash->digest = n2_hmac_async_digest;
  1133. ahash->setkey = n2_hmac_async_setkey;
  1134. base = &ahash->halg.base;
  1135. err = -EINVAL;
  1136. if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
  1137. p->child_alg) >= CRYPTO_MAX_ALG_NAME)
  1138. goto out_free_p;
  1139. if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s-n2",
  1140. p->child_alg) >= CRYPTO_MAX_ALG_NAME)
  1141. goto out_free_p;
  1142. base->cra_ctxsize = sizeof(struct n2_hmac_ctx);
  1143. base->cra_init = n2_hmac_cra_init;
  1144. base->cra_exit = n2_hmac_cra_exit;
  1145. list_add(&p->derived.entry, &hmac_algs);
  1146. err = crypto_register_ahash(ahash);
  1147. if (err) {
  1148. pr_err("%s alg registration failed\n", base->cra_name);
  1149. list_del(&p->derived.entry);
  1150. out_free_p:
  1151. kfree(p);
  1152. } else {
  1153. pr_info("%s alg registered\n", base->cra_name);
  1154. }
  1155. return err;
  1156. }
  1157. static int __n2_register_one_ahash(const struct n2_hash_tmpl *tmpl)
  1158. {
  1159. struct n2_ahash_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1160. struct hash_alg_common *halg;
  1161. struct crypto_alg *base;
  1162. struct ahash_alg *ahash;
  1163. int err;
  1164. if (!p)
  1165. return -ENOMEM;
  1166. p->hash_zero = tmpl->hash_zero;
  1167. p->hash_init = tmpl->hash_init;
  1168. p->auth_type = tmpl->auth_type;
  1169. p->hmac_type = tmpl->hmac_type;
  1170. p->hw_op_hashsz = tmpl->hw_op_hashsz;
  1171. p->digest_size = tmpl->digest_size;
  1172. ahash = &p->alg;
  1173. ahash->init = n2_hash_async_init;
  1174. ahash->update = n2_hash_async_update;
  1175. ahash->final = n2_hash_async_final;
  1176. ahash->finup = n2_hash_async_finup;
  1177. ahash->digest = n2_hash_async_digest;
  1178. ahash->export = n2_hash_async_noexport;
  1179. ahash->import = n2_hash_async_noimport;
  1180. halg = &ahash->halg;
  1181. halg->digestsize = tmpl->digest_size;
  1182. halg->statesize = tmpl->statesize;
  1183. base = &halg->base;
  1184. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1185. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-n2", tmpl->name);
  1186. base->cra_priority = N2_CRA_PRIORITY;
  1187. base->cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
  1188. CRYPTO_ALG_NEED_FALLBACK;
  1189. base->cra_blocksize = tmpl->block_size;
  1190. base->cra_ctxsize = sizeof(struct n2_hash_ctx);
  1191. base->cra_module = THIS_MODULE;
  1192. base->cra_init = n2_hash_cra_init;
  1193. base->cra_exit = n2_hash_cra_exit;
  1194. list_add(&p->entry, &ahash_algs);
  1195. err = crypto_register_ahash(ahash);
  1196. if (err) {
  1197. pr_err("%s alg registration failed\n", base->cra_name);
  1198. list_del(&p->entry);
  1199. kfree(p);
  1200. } else {
  1201. pr_info("%s alg registered\n", base->cra_name);
  1202. }
  1203. if (!err && p->hmac_type != AUTH_TYPE_RESERVED)
  1204. err = __n2_register_one_hmac(p);
  1205. return err;
  1206. }
  1207. static int n2_register_algs(void)
  1208. {
  1209. int i, err = 0;
  1210. mutex_lock(&spu_lock);
  1211. if (algs_registered++)
  1212. goto out;
  1213. for (i = 0; i < NUM_HASH_TMPLS; i++) {
  1214. err = __n2_register_one_ahash(&hash_tmpls[i]);
  1215. if (err) {
  1216. __n2_unregister_algs();
  1217. goto out;
  1218. }
  1219. }
  1220. for (i = 0; i < NUM_CIPHER_TMPLS; i++) {
  1221. err = __n2_register_one_skcipher(&skcipher_tmpls[i]);
  1222. if (err) {
  1223. __n2_unregister_algs();
  1224. goto out;
  1225. }
  1226. }
  1227. out:
  1228. mutex_unlock(&spu_lock);
  1229. return err;
  1230. }
  1231. static void n2_unregister_algs(void)
  1232. {
  1233. mutex_lock(&spu_lock);
  1234. if (!--algs_registered)
  1235. __n2_unregister_algs();
  1236. mutex_unlock(&spu_lock);
  1237. }
  1238. /* To map CWQ queues to interrupt sources, the hypervisor API provides
  1239. * a devino. This isn't very useful to us because all of the
  1240. * interrupts listed in the device_node have been translated to
  1241. * Linux virtual IRQ cookie numbers.
  1242. *
  1243. * So we have to back-translate, going through the 'intr' and 'ino'
  1244. * property tables of the n2cp MDESC node, matching it with the OF
  1245. * 'interrupts' property entries, in order to figure out which
  1246. * devino goes to which already-translated IRQ.
  1247. */
  1248. static int find_devino_index(struct platform_device *dev, struct spu_mdesc_info *ip,
  1249. unsigned long dev_ino)
  1250. {
  1251. const unsigned int *dev_intrs;
  1252. unsigned int intr;
  1253. int i;
  1254. for (i = 0; i < ip->num_intrs; i++) {
  1255. if (ip->ino_table[i].ino == dev_ino)
  1256. break;
  1257. }
  1258. if (i == ip->num_intrs)
  1259. return -ENODEV;
  1260. intr = ip->ino_table[i].intr;
  1261. dev_intrs = of_get_property(dev->dev.of_node, "interrupts", NULL);
  1262. if (!dev_intrs)
  1263. return -ENODEV;
  1264. for (i = 0; i < dev->archdata.num_irqs; i++) {
  1265. if (dev_intrs[i] == intr)
  1266. return i;
  1267. }
  1268. return -ENODEV;
  1269. }
  1270. static int spu_map_ino(struct platform_device *dev, struct spu_mdesc_info *ip,
  1271. const char *irq_name, struct spu_queue *p,
  1272. irq_handler_t handler)
  1273. {
  1274. unsigned long herr;
  1275. int index;
  1276. herr = sun4v_ncs_qhandle_to_devino(p->qhandle, &p->devino);
  1277. if (herr)
  1278. return -EINVAL;
  1279. index = find_devino_index(dev, ip, p->devino);
  1280. if (index < 0)
  1281. return index;
  1282. p->irq = dev->archdata.irqs[index];
  1283. sprintf(p->irq_name, "%s-%d", irq_name, index);
  1284. return request_irq(p->irq, handler, 0, p->irq_name, p);
  1285. }
  1286. static struct kmem_cache *queue_cache[2];
  1287. static void *new_queue(unsigned long q_type)
  1288. {
  1289. return kmem_cache_zalloc(queue_cache[q_type - 1], GFP_KERNEL);
  1290. }
  1291. static void free_queue(void *p, unsigned long q_type)
  1292. {
  1293. kmem_cache_free(queue_cache[q_type - 1], p);
  1294. }
  1295. static int queue_cache_init(void)
  1296. {
  1297. if (!queue_cache[HV_NCS_QTYPE_MAU - 1])
  1298. queue_cache[HV_NCS_QTYPE_MAU - 1] =
  1299. kmem_cache_create("mau_queue",
  1300. (MAU_NUM_ENTRIES *
  1301. MAU_ENTRY_SIZE),
  1302. MAU_ENTRY_SIZE, 0, NULL);
  1303. if (!queue_cache[HV_NCS_QTYPE_MAU - 1])
  1304. return -ENOMEM;
  1305. if (!queue_cache[HV_NCS_QTYPE_CWQ - 1])
  1306. queue_cache[HV_NCS_QTYPE_CWQ - 1] =
  1307. kmem_cache_create("cwq_queue",
  1308. (CWQ_NUM_ENTRIES *
  1309. CWQ_ENTRY_SIZE),
  1310. CWQ_ENTRY_SIZE, 0, NULL);
  1311. if (!queue_cache[HV_NCS_QTYPE_CWQ - 1]) {
  1312. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_MAU - 1]);
  1313. queue_cache[HV_NCS_QTYPE_MAU - 1] = NULL;
  1314. return -ENOMEM;
  1315. }
  1316. return 0;
  1317. }
  1318. static void queue_cache_destroy(void)
  1319. {
  1320. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_MAU - 1]);
  1321. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_CWQ - 1]);
  1322. queue_cache[HV_NCS_QTYPE_MAU - 1] = NULL;
  1323. queue_cache[HV_NCS_QTYPE_CWQ - 1] = NULL;
  1324. }
  1325. static long spu_queue_register_workfn(void *arg)
  1326. {
  1327. struct spu_qreg *qr = arg;
  1328. struct spu_queue *p = qr->queue;
  1329. unsigned long q_type = qr->type;
  1330. unsigned long hv_ret;
  1331. hv_ret = sun4v_ncs_qconf(q_type, __pa(p->q),
  1332. CWQ_NUM_ENTRIES, &p->qhandle);
  1333. if (!hv_ret)
  1334. sun4v_ncs_sethead_marker(p->qhandle, 0);
  1335. return hv_ret ? -EINVAL : 0;
  1336. }
  1337. static int spu_queue_register(struct spu_queue *p, unsigned long q_type)
  1338. {
  1339. int cpu = cpumask_any_and(&p->sharing, cpu_online_mask);
  1340. struct spu_qreg qr = { .queue = p, .type = q_type };
  1341. return work_on_cpu_safe(cpu, spu_queue_register_workfn, &qr);
  1342. }
  1343. static int spu_queue_setup(struct spu_queue *p)
  1344. {
  1345. int err;
  1346. p->q = new_queue(p->q_type);
  1347. if (!p->q)
  1348. return -ENOMEM;
  1349. err = spu_queue_register(p, p->q_type);
  1350. if (err) {
  1351. free_queue(p->q, p->q_type);
  1352. p->q = NULL;
  1353. }
  1354. return err;
  1355. }
  1356. static void spu_queue_destroy(struct spu_queue *p)
  1357. {
  1358. unsigned long hv_ret;
  1359. if (!p->q)
  1360. return;
  1361. hv_ret = sun4v_ncs_qconf(p->q_type, p->qhandle, 0, &p->qhandle);
  1362. if (!hv_ret)
  1363. free_queue(p->q, p->q_type);
  1364. }
  1365. static void spu_list_destroy(struct list_head *list)
  1366. {
  1367. struct spu_queue *p, *n;
  1368. list_for_each_entry_safe(p, n, list, list) {
  1369. int i;
  1370. for (i = 0; i < NR_CPUS; i++) {
  1371. if (cpu_to_cwq[i] == p)
  1372. cpu_to_cwq[i] = NULL;
  1373. }
  1374. if (p->irq) {
  1375. free_irq(p->irq, p);
  1376. p->irq = 0;
  1377. }
  1378. spu_queue_destroy(p);
  1379. list_del(&p->list);
  1380. kfree(p);
  1381. }
  1382. }
  1383. /* Walk the backward arcs of a CWQ 'exec-unit' node,
  1384. * gathering cpu membership information.
  1385. */
  1386. static int spu_mdesc_walk_arcs(struct mdesc_handle *mdesc,
  1387. struct platform_device *dev,
  1388. u64 node, struct spu_queue *p,
  1389. struct spu_queue **table)
  1390. {
  1391. u64 arc;
  1392. mdesc_for_each_arc(arc, mdesc, node, MDESC_ARC_TYPE_BACK) {
  1393. u64 tgt = mdesc_arc_target(mdesc, arc);
  1394. const char *name = mdesc_node_name(mdesc, tgt);
  1395. const u64 *id;
  1396. if (strcmp(name, "cpu"))
  1397. continue;
  1398. id = mdesc_get_property(mdesc, tgt, "id", NULL);
  1399. if (table[*id] != NULL) {
  1400. dev_err(&dev->dev, "%pOF: SPU cpu slot already set.\n",
  1401. dev->dev.of_node);
  1402. return -EINVAL;
  1403. }
  1404. cpumask_set_cpu(*id, &p->sharing);
  1405. table[*id] = p;
  1406. }
  1407. return 0;
  1408. }
  1409. /* Process an 'exec-unit' MDESC node of type 'cwq'. */
  1410. static int handle_exec_unit(struct spu_mdesc_info *ip, struct list_head *list,
  1411. struct platform_device *dev, struct mdesc_handle *mdesc,
  1412. u64 node, const char *iname, unsigned long q_type,
  1413. irq_handler_t handler, struct spu_queue **table)
  1414. {
  1415. struct spu_queue *p;
  1416. int err;
  1417. p = kzalloc(sizeof(struct spu_queue), GFP_KERNEL);
  1418. if (!p) {
  1419. dev_err(&dev->dev, "%pOF: Could not allocate SPU queue.\n",
  1420. dev->dev.of_node);
  1421. return -ENOMEM;
  1422. }
  1423. cpumask_clear(&p->sharing);
  1424. spin_lock_init(&p->lock);
  1425. p->q_type = q_type;
  1426. INIT_LIST_HEAD(&p->jobs);
  1427. list_add(&p->list, list);
  1428. err = spu_mdesc_walk_arcs(mdesc, dev, node, p, table);
  1429. if (err)
  1430. return err;
  1431. err = spu_queue_setup(p);
  1432. if (err)
  1433. return err;
  1434. return spu_map_ino(dev, ip, iname, p, handler);
  1435. }
  1436. static int spu_mdesc_scan(struct mdesc_handle *mdesc, struct platform_device *dev,
  1437. struct spu_mdesc_info *ip, struct list_head *list,
  1438. const char *exec_name, unsigned long q_type,
  1439. irq_handler_t handler, struct spu_queue **table)
  1440. {
  1441. int err = 0;
  1442. u64 node;
  1443. mdesc_for_each_node_by_name(mdesc, node, "exec-unit") {
  1444. const char *type;
  1445. type = mdesc_get_property(mdesc, node, "type", NULL);
  1446. if (!type || strcmp(type, exec_name))
  1447. continue;
  1448. err = handle_exec_unit(ip, list, dev, mdesc, node,
  1449. exec_name, q_type, handler, table);
  1450. if (err) {
  1451. spu_list_destroy(list);
  1452. break;
  1453. }
  1454. }
  1455. return err;
  1456. }
  1457. static int get_irq_props(struct mdesc_handle *mdesc, u64 node,
  1458. struct spu_mdesc_info *ip)
  1459. {
  1460. const u64 *ino;
  1461. int ino_len;
  1462. int i;
  1463. ino = mdesc_get_property(mdesc, node, "ino", &ino_len);
  1464. if (!ino) {
  1465. printk("NO 'ino'\n");
  1466. return -ENODEV;
  1467. }
  1468. ip->num_intrs = ino_len / sizeof(u64);
  1469. ip->ino_table = kzalloc((sizeof(struct ino_blob) *
  1470. ip->num_intrs),
  1471. GFP_KERNEL);
  1472. if (!ip->ino_table)
  1473. return -ENOMEM;
  1474. for (i = 0; i < ip->num_intrs; i++) {
  1475. struct ino_blob *b = &ip->ino_table[i];
  1476. b->intr = i + 1;
  1477. b->ino = ino[i];
  1478. }
  1479. return 0;
  1480. }
  1481. static int grab_mdesc_irq_props(struct mdesc_handle *mdesc,
  1482. struct platform_device *dev,
  1483. struct spu_mdesc_info *ip,
  1484. const char *node_name)
  1485. {
  1486. u64 node, reg;
  1487. if (of_property_read_reg(dev->dev.of_node, 0, &reg, NULL) < 0)
  1488. return -ENODEV;
  1489. mdesc_for_each_node_by_name(mdesc, node, "virtual-device") {
  1490. const char *name;
  1491. const u64 *chdl;
  1492. name = mdesc_get_property(mdesc, node, "name", NULL);
  1493. if (!name || strcmp(name, node_name))
  1494. continue;
  1495. chdl = mdesc_get_property(mdesc, node, "cfg-handle", NULL);
  1496. if (!chdl || (*chdl != reg))
  1497. continue;
  1498. ip->cfg_handle = *chdl;
  1499. return get_irq_props(mdesc, node, ip);
  1500. }
  1501. return -ENODEV;
  1502. }
  1503. static unsigned long n2_spu_hvapi_major;
  1504. static unsigned long n2_spu_hvapi_minor;
  1505. static int n2_spu_hvapi_register(void)
  1506. {
  1507. int err;
  1508. n2_spu_hvapi_major = 2;
  1509. n2_spu_hvapi_minor = 0;
  1510. err = sun4v_hvapi_register(HV_GRP_NCS,
  1511. n2_spu_hvapi_major,
  1512. &n2_spu_hvapi_minor);
  1513. if (!err)
  1514. pr_info("Registered NCS HVAPI version %lu.%lu\n",
  1515. n2_spu_hvapi_major,
  1516. n2_spu_hvapi_minor);
  1517. return err;
  1518. }
  1519. static void n2_spu_hvapi_unregister(void)
  1520. {
  1521. sun4v_hvapi_unregister(HV_GRP_NCS);
  1522. }
  1523. static int global_ref;
  1524. static int grab_global_resources(void)
  1525. {
  1526. int err = 0;
  1527. mutex_lock(&spu_lock);
  1528. if (global_ref++)
  1529. goto out;
  1530. err = n2_spu_hvapi_register();
  1531. if (err)
  1532. goto out;
  1533. err = queue_cache_init();
  1534. if (err)
  1535. goto out_hvapi_release;
  1536. err = -ENOMEM;
  1537. cpu_to_cwq = kcalloc(NR_CPUS, sizeof(struct spu_queue *),
  1538. GFP_KERNEL);
  1539. if (!cpu_to_cwq)
  1540. goto out_queue_cache_destroy;
  1541. cpu_to_mau = kcalloc(NR_CPUS, sizeof(struct spu_queue *),
  1542. GFP_KERNEL);
  1543. if (!cpu_to_mau)
  1544. goto out_free_cwq_table;
  1545. err = 0;
  1546. out:
  1547. if (err)
  1548. global_ref--;
  1549. mutex_unlock(&spu_lock);
  1550. return err;
  1551. out_free_cwq_table:
  1552. kfree(cpu_to_cwq);
  1553. cpu_to_cwq = NULL;
  1554. out_queue_cache_destroy:
  1555. queue_cache_destroy();
  1556. out_hvapi_release:
  1557. n2_spu_hvapi_unregister();
  1558. goto out;
  1559. }
  1560. static void release_global_resources(void)
  1561. {
  1562. mutex_lock(&spu_lock);
  1563. if (!--global_ref) {
  1564. kfree(cpu_to_cwq);
  1565. cpu_to_cwq = NULL;
  1566. kfree(cpu_to_mau);
  1567. cpu_to_mau = NULL;
  1568. queue_cache_destroy();
  1569. n2_spu_hvapi_unregister();
  1570. }
  1571. mutex_unlock(&spu_lock);
  1572. }
  1573. static struct n2_crypto *alloc_n2cp(void)
  1574. {
  1575. struct n2_crypto *np = kzalloc(sizeof(struct n2_crypto), GFP_KERNEL);
  1576. if (np)
  1577. INIT_LIST_HEAD(&np->cwq_list);
  1578. return np;
  1579. }
  1580. static void free_n2cp(struct n2_crypto *np)
  1581. {
  1582. kfree(np->cwq_info.ino_table);
  1583. np->cwq_info.ino_table = NULL;
  1584. kfree(np);
  1585. }
  1586. static void n2_spu_driver_version(void)
  1587. {
  1588. static int n2_spu_version_printed;
  1589. if (n2_spu_version_printed++ == 0)
  1590. pr_info("%s", version);
  1591. }
  1592. static int n2_crypto_probe(struct platform_device *dev)
  1593. {
  1594. struct mdesc_handle *mdesc;
  1595. struct n2_crypto *np;
  1596. int err;
  1597. n2_spu_driver_version();
  1598. pr_info("Found N2CP at %pOF\n", dev->dev.of_node);
  1599. np = alloc_n2cp();
  1600. if (!np) {
  1601. dev_err(&dev->dev, "%pOF: Unable to allocate n2cp.\n",
  1602. dev->dev.of_node);
  1603. return -ENOMEM;
  1604. }
  1605. err = grab_global_resources();
  1606. if (err) {
  1607. dev_err(&dev->dev, "%pOF: Unable to grab global resources.\n",
  1608. dev->dev.of_node);
  1609. goto out_free_n2cp;
  1610. }
  1611. mdesc = mdesc_grab();
  1612. if (!mdesc) {
  1613. dev_err(&dev->dev, "%pOF: Unable to grab MDESC.\n",
  1614. dev->dev.of_node);
  1615. err = -ENODEV;
  1616. goto out_free_global;
  1617. }
  1618. err = grab_mdesc_irq_props(mdesc, dev, &np->cwq_info, "n2cp");
  1619. if (err) {
  1620. dev_err(&dev->dev, "%pOF: Unable to grab IRQ props.\n",
  1621. dev->dev.of_node);
  1622. mdesc_release(mdesc);
  1623. goto out_free_global;
  1624. }
  1625. err = spu_mdesc_scan(mdesc, dev, &np->cwq_info, &np->cwq_list,
  1626. "cwq", HV_NCS_QTYPE_CWQ, cwq_intr,
  1627. cpu_to_cwq);
  1628. mdesc_release(mdesc);
  1629. if (err) {
  1630. dev_err(&dev->dev, "%pOF: CWQ MDESC scan failed.\n",
  1631. dev->dev.of_node);
  1632. goto out_free_global;
  1633. }
  1634. err = n2_register_algs();
  1635. if (err) {
  1636. dev_err(&dev->dev, "%pOF: Unable to register algorithms.\n",
  1637. dev->dev.of_node);
  1638. goto out_free_spu_list;
  1639. }
  1640. dev_set_drvdata(&dev->dev, np);
  1641. return 0;
  1642. out_free_spu_list:
  1643. spu_list_destroy(&np->cwq_list);
  1644. out_free_global:
  1645. release_global_resources();
  1646. out_free_n2cp:
  1647. free_n2cp(np);
  1648. return err;
  1649. }
  1650. static void n2_crypto_remove(struct platform_device *dev)
  1651. {
  1652. struct n2_crypto *np = dev_get_drvdata(&dev->dev);
  1653. n2_unregister_algs();
  1654. spu_list_destroy(&np->cwq_list);
  1655. release_global_resources();
  1656. free_n2cp(np);
  1657. }
  1658. static struct n2_mau *alloc_ncp(void)
  1659. {
  1660. struct n2_mau *mp = kzalloc(sizeof(struct n2_mau), GFP_KERNEL);
  1661. if (mp)
  1662. INIT_LIST_HEAD(&mp->mau_list);
  1663. return mp;
  1664. }
  1665. static void free_ncp(struct n2_mau *mp)
  1666. {
  1667. kfree(mp->mau_info.ino_table);
  1668. mp->mau_info.ino_table = NULL;
  1669. kfree(mp);
  1670. }
  1671. static int n2_mau_probe(struct platform_device *dev)
  1672. {
  1673. struct mdesc_handle *mdesc;
  1674. struct n2_mau *mp;
  1675. int err;
  1676. n2_spu_driver_version();
  1677. pr_info("Found NCP at %pOF\n", dev->dev.of_node);
  1678. mp = alloc_ncp();
  1679. if (!mp) {
  1680. dev_err(&dev->dev, "%pOF: Unable to allocate ncp.\n",
  1681. dev->dev.of_node);
  1682. return -ENOMEM;
  1683. }
  1684. err = grab_global_resources();
  1685. if (err) {
  1686. dev_err(&dev->dev, "%pOF: Unable to grab global resources.\n",
  1687. dev->dev.of_node);
  1688. goto out_free_ncp;
  1689. }
  1690. mdesc = mdesc_grab();
  1691. if (!mdesc) {
  1692. dev_err(&dev->dev, "%pOF: Unable to grab MDESC.\n",
  1693. dev->dev.of_node);
  1694. err = -ENODEV;
  1695. goto out_free_global;
  1696. }
  1697. err = grab_mdesc_irq_props(mdesc, dev, &mp->mau_info, "ncp");
  1698. if (err) {
  1699. dev_err(&dev->dev, "%pOF: Unable to grab IRQ props.\n",
  1700. dev->dev.of_node);
  1701. mdesc_release(mdesc);
  1702. goto out_free_global;
  1703. }
  1704. err = spu_mdesc_scan(mdesc, dev, &mp->mau_info, &mp->mau_list,
  1705. "mau", HV_NCS_QTYPE_MAU, mau_intr,
  1706. cpu_to_mau);
  1707. mdesc_release(mdesc);
  1708. if (err) {
  1709. dev_err(&dev->dev, "%pOF: MAU MDESC scan failed.\n",
  1710. dev->dev.of_node);
  1711. goto out_free_global;
  1712. }
  1713. dev_set_drvdata(&dev->dev, mp);
  1714. return 0;
  1715. out_free_global:
  1716. release_global_resources();
  1717. out_free_ncp:
  1718. free_ncp(mp);
  1719. return err;
  1720. }
  1721. static void n2_mau_remove(struct platform_device *dev)
  1722. {
  1723. struct n2_mau *mp = dev_get_drvdata(&dev->dev);
  1724. spu_list_destroy(&mp->mau_list);
  1725. release_global_resources();
  1726. free_ncp(mp);
  1727. }
  1728. static const struct of_device_id n2_crypto_match[] = {
  1729. {
  1730. .name = "n2cp",
  1731. .compatible = "SUNW,n2-cwq",
  1732. },
  1733. {
  1734. .name = "n2cp",
  1735. .compatible = "SUNW,vf-cwq",
  1736. },
  1737. {
  1738. .name = "n2cp",
  1739. .compatible = "SUNW,kt-cwq",
  1740. },
  1741. {},
  1742. };
  1743. MODULE_DEVICE_TABLE(of, n2_crypto_match);
  1744. static struct platform_driver n2_crypto_driver = {
  1745. .driver = {
  1746. .name = "n2cp",
  1747. .of_match_table = n2_crypto_match,
  1748. },
  1749. .probe = n2_crypto_probe,
  1750. .remove_new = n2_crypto_remove,
  1751. };
  1752. static const struct of_device_id n2_mau_match[] = {
  1753. {
  1754. .name = "ncp",
  1755. .compatible = "SUNW,n2-mau",
  1756. },
  1757. {
  1758. .name = "ncp",
  1759. .compatible = "SUNW,vf-mau",
  1760. },
  1761. {
  1762. .name = "ncp",
  1763. .compatible = "SUNW,kt-mau",
  1764. },
  1765. {},
  1766. };
  1767. MODULE_DEVICE_TABLE(of, n2_mau_match);
  1768. static struct platform_driver n2_mau_driver = {
  1769. .driver = {
  1770. .name = "ncp",
  1771. .of_match_table = n2_mau_match,
  1772. },
  1773. .probe = n2_mau_probe,
  1774. .remove_new = n2_mau_remove,
  1775. };
  1776. static struct platform_driver * const drivers[] = {
  1777. &n2_crypto_driver,
  1778. &n2_mau_driver,
  1779. };
  1780. static int __init n2_init(void)
  1781. {
  1782. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  1783. }
  1784. static void __exit n2_exit(void)
  1785. {
  1786. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  1787. }
  1788. module_init(n2_init);
  1789. module_exit(n2_exit);