n2_core.c 52 KB

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