n2_core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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_cra_init(struct crypto_tfm *tfm)
  284. {
  285. const char *fallback_driver_name = crypto_tfm_alg_name(tfm);
  286. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  287. struct n2_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  288. struct crypto_ahash *fallback_tfm;
  289. int err;
  290. fallback_tfm = crypto_alloc_ahash(fallback_driver_name, 0,
  291. CRYPTO_ALG_NEED_FALLBACK);
  292. if (IS_ERR(fallback_tfm)) {
  293. pr_warning("Fallback driver '%s' could not be loaded!\n",
  294. fallback_driver_name);
  295. err = PTR_ERR(fallback_tfm);
  296. goto out;
  297. }
  298. crypto_ahash_set_reqsize(ahash, (sizeof(struct n2_hash_req_ctx) +
  299. crypto_ahash_reqsize(fallback_tfm)));
  300. ctx->fallback_tfm = fallback_tfm;
  301. return 0;
  302. out:
  303. return err;
  304. }
  305. static void n2_hash_cra_exit(struct crypto_tfm *tfm)
  306. {
  307. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  308. struct n2_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  309. crypto_free_ahash(ctx->fallback_tfm);
  310. }
  311. static int n2_hmac_cra_init(struct crypto_tfm *tfm)
  312. {
  313. const char *fallback_driver_name = crypto_tfm_alg_name(tfm);
  314. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  315. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(ahash);
  316. struct n2_hmac_alg *n2alg = n2_hmac_alg(tfm);
  317. struct crypto_ahash *fallback_tfm;
  318. struct crypto_shash *child_shash;
  319. int err;
  320. fallback_tfm = crypto_alloc_ahash(fallback_driver_name, 0,
  321. CRYPTO_ALG_NEED_FALLBACK);
  322. if (IS_ERR(fallback_tfm)) {
  323. pr_warning("Fallback driver '%s' could not be loaded!\n",
  324. fallback_driver_name);
  325. err = PTR_ERR(fallback_tfm);
  326. goto out;
  327. }
  328. child_shash = crypto_alloc_shash(n2alg->child_alg, 0, 0);
  329. if (IS_ERR(child_shash)) {
  330. pr_warning("Child shash '%s' could not be loaded!\n",
  331. n2alg->child_alg);
  332. err = PTR_ERR(child_shash);
  333. goto out_free_fallback;
  334. }
  335. crypto_ahash_set_reqsize(ahash, (sizeof(struct n2_hash_req_ctx) +
  336. crypto_ahash_reqsize(fallback_tfm)));
  337. ctx->child_shash = child_shash;
  338. ctx->base.fallback_tfm = fallback_tfm;
  339. return 0;
  340. out_free_fallback:
  341. crypto_free_ahash(fallback_tfm);
  342. out:
  343. return err;
  344. }
  345. static void n2_hmac_cra_exit(struct crypto_tfm *tfm)
  346. {
  347. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  348. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(ahash);
  349. crypto_free_ahash(ctx->base.fallback_tfm);
  350. crypto_free_shash(ctx->child_shash);
  351. }
  352. static int n2_hmac_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  353. unsigned int keylen)
  354. {
  355. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(tfm);
  356. struct crypto_shash *child_shash = ctx->child_shash;
  357. struct crypto_ahash *fallback_tfm;
  358. SHASH_DESC_ON_STACK(shash, child_shash);
  359. int err, bs, ds;
  360. fallback_tfm = ctx->base.fallback_tfm;
  361. err = crypto_ahash_setkey(fallback_tfm, key, keylen);
  362. if (err)
  363. return err;
  364. shash->tfm = child_shash;
  365. shash->flags = crypto_ahash_get_flags(tfm) &
  366. CRYPTO_TFM_REQ_MAY_SLEEP;
  367. bs = crypto_shash_blocksize(child_shash);
  368. ds = crypto_shash_digestsize(child_shash);
  369. BUG_ON(ds > N2_HASH_KEY_MAX);
  370. if (keylen > bs) {
  371. err = crypto_shash_digest(shash, key, keylen,
  372. ctx->hash_key);
  373. if (err)
  374. return err;
  375. keylen = ds;
  376. } else if (keylen <= N2_HASH_KEY_MAX)
  377. memcpy(ctx->hash_key, key, keylen);
  378. ctx->hash_key_len = keylen;
  379. return err;
  380. }
  381. static unsigned long wait_for_tail(struct spu_queue *qp)
  382. {
  383. unsigned long head, hv_ret;
  384. do {
  385. hv_ret = sun4v_ncs_gethead(qp->qhandle, &head);
  386. if (hv_ret != HV_EOK) {
  387. pr_err("Hypervisor error on gethead\n");
  388. break;
  389. }
  390. if (head == qp->tail) {
  391. qp->head = head;
  392. break;
  393. }
  394. } while (1);
  395. return hv_ret;
  396. }
  397. static unsigned long submit_and_wait_for_tail(struct spu_queue *qp,
  398. struct cwq_initial_entry *ent)
  399. {
  400. unsigned long hv_ret = spu_queue_submit(qp, ent);
  401. if (hv_ret == HV_EOK)
  402. hv_ret = wait_for_tail(qp);
  403. return hv_ret;
  404. }
  405. static int n2_do_async_digest(struct ahash_request *req,
  406. unsigned int auth_type, unsigned int digest_size,
  407. unsigned int result_size, void *hash_loc,
  408. unsigned long auth_key, unsigned int auth_key_len)
  409. {
  410. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  411. struct cwq_initial_entry *ent;
  412. struct crypto_hash_walk walk;
  413. struct spu_queue *qp;
  414. unsigned long flags;
  415. int err = -ENODEV;
  416. int nbytes, cpu;
  417. /* The total effective length of the operation may not
  418. * exceed 2^16.
  419. */
  420. if (unlikely(req->nbytes > (1 << 16))) {
  421. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  422. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  423. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  424. rctx->fallback_req.base.flags =
  425. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  426. rctx->fallback_req.nbytes = req->nbytes;
  427. rctx->fallback_req.src = req->src;
  428. rctx->fallback_req.result = req->result;
  429. return crypto_ahash_digest(&rctx->fallback_req);
  430. }
  431. nbytes = crypto_hash_walk_first(req, &walk);
  432. cpu = get_cpu();
  433. qp = cpu_to_cwq[cpu];
  434. if (!qp)
  435. goto out;
  436. spin_lock_irqsave(&qp->lock, flags);
  437. /* XXX can do better, improve this later by doing a by-hand scatterlist
  438. * XXX walk, etc.
  439. */
  440. ent = qp->q + qp->tail;
  441. ent->control = control_word_base(nbytes, auth_key_len, 0,
  442. auth_type, digest_size,
  443. false, true, false, false,
  444. OPCODE_INPLACE_BIT |
  445. OPCODE_AUTH_MAC);
  446. ent->src_addr = __pa(walk.data);
  447. ent->auth_key_addr = auth_key;
  448. ent->auth_iv_addr = __pa(hash_loc);
  449. ent->final_auth_state_addr = 0UL;
  450. ent->enc_key_addr = 0UL;
  451. ent->enc_iv_addr = 0UL;
  452. ent->dest_addr = __pa(hash_loc);
  453. nbytes = crypto_hash_walk_done(&walk, 0);
  454. while (nbytes > 0) {
  455. ent = spu_queue_next(qp, ent);
  456. ent->control = (nbytes - 1);
  457. ent->src_addr = __pa(walk.data);
  458. ent->auth_key_addr = 0UL;
  459. ent->auth_iv_addr = 0UL;
  460. ent->final_auth_state_addr = 0UL;
  461. ent->enc_key_addr = 0UL;
  462. ent->enc_iv_addr = 0UL;
  463. ent->dest_addr = 0UL;
  464. nbytes = crypto_hash_walk_done(&walk, 0);
  465. }
  466. ent->control |= CONTROL_END_OF_BLOCK;
  467. if (submit_and_wait_for_tail(qp, ent) != HV_EOK)
  468. err = -EINVAL;
  469. else
  470. err = 0;
  471. spin_unlock_irqrestore(&qp->lock, flags);
  472. if (!err)
  473. memcpy(req->result, hash_loc, result_size);
  474. out:
  475. put_cpu();
  476. return err;
  477. }
  478. static int n2_hash_async_digest(struct ahash_request *req)
  479. {
  480. struct n2_ahash_alg *n2alg = n2_ahash_alg(req->base.tfm);
  481. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  482. int ds;
  483. ds = n2alg->digest_size;
  484. if (unlikely(req->nbytes == 0)) {
  485. memcpy(req->result, n2alg->hash_zero, ds);
  486. return 0;
  487. }
  488. memcpy(&rctx->u, n2alg->hash_init, n2alg->hw_op_hashsz);
  489. return n2_do_async_digest(req, n2alg->auth_type,
  490. n2alg->hw_op_hashsz, ds,
  491. &rctx->u, 0UL, 0);
  492. }
  493. static int n2_hmac_async_digest(struct ahash_request *req)
  494. {
  495. struct n2_hmac_alg *n2alg = n2_hmac_alg(req->base.tfm);
  496. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  497. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  498. struct n2_hmac_ctx *ctx = crypto_ahash_ctx(tfm);
  499. int ds;
  500. ds = n2alg->derived.digest_size;
  501. if (unlikely(req->nbytes == 0) ||
  502. unlikely(ctx->hash_key_len > N2_HASH_KEY_MAX)) {
  503. struct n2_hash_req_ctx *rctx = ahash_request_ctx(req);
  504. struct n2_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  505. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  506. rctx->fallback_req.base.flags =
  507. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  508. rctx->fallback_req.nbytes = req->nbytes;
  509. rctx->fallback_req.src = req->src;
  510. rctx->fallback_req.result = req->result;
  511. return crypto_ahash_digest(&rctx->fallback_req);
  512. }
  513. memcpy(&rctx->u, n2alg->derived.hash_init,
  514. n2alg->derived.hw_op_hashsz);
  515. return n2_do_async_digest(req, n2alg->derived.hmac_type,
  516. n2alg->derived.hw_op_hashsz, ds,
  517. &rctx->u,
  518. __pa(&ctx->hash_key),
  519. ctx->hash_key_len);
  520. }
  521. struct n2_cipher_context {
  522. int key_len;
  523. int enc_type;
  524. union {
  525. u8 aes[AES_MAX_KEY_SIZE];
  526. u8 des[DES_KEY_SIZE];
  527. u8 des3[3 * DES_KEY_SIZE];
  528. u8 arc4[258]; /* S-box, X, Y */
  529. } key;
  530. };
  531. #define N2_CHUNK_ARR_LEN 16
  532. struct n2_crypto_chunk {
  533. struct list_head entry;
  534. unsigned long iv_paddr : 44;
  535. unsigned long arr_len : 20;
  536. unsigned long dest_paddr;
  537. unsigned long dest_final;
  538. struct {
  539. unsigned long src_paddr : 44;
  540. unsigned long src_len : 20;
  541. } arr[N2_CHUNK_ARR_LEN];
  542. };
  543. struct n2_request_context {
  544. struct ablkcipher_walk walk;
  545. struct list_head chunk_list;
  546. struct n2_crypto_chunk chunk;
  547. u8 temp_iv[16];
  548. };
  549. /* The SPU allows some level of flexibility for partial cipher blocks
  550. * being specified in a descriptor.
  551. *
  552. * It merely requires that every descriptor's length field is at least
  553. * as large as the cipher block size. This means that a cipher block
  554. * can span at most 2 descriptors. However, this does not allow a
  555. * partial block to span into the final descriptor as that would
  556. * violate the rule (since every descriptor's length must be at lest
  557. * the block size). So, for example, assuming an 8 byte block size:
  558. *
  559. * 0xe --> 0xa --> 0x8
  560. *
  561. * is a valid length sequence, whereas:
  562. *
  563. * 0xe --> 0xb --> 0x7
  564. *
  565. * is not a valid sequence.
  566. */
  567. struct n2_cipher_alg {
  568. struct list_head entry;
  569. u8 enc_type;
  570. struct crypto_alg alg;
  571. };
  572. static inline struct n2_cipher_alg *n2_cipher_alg(struct crypto_tfm *tfm)
  573. {
  574. struct crypto_alg *alg = tfm->__crt_alg;
  575. return container_of(alg, struct n2_cipher_alg, alg);
  576. }
  577. struct n2_cipher_request_context {
  578. struct ablkcipher_walk walk;
  579. };
  580. static int n2_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  581. unsigned int keylen)
  582. {
  583. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  584. struct n2_cipher_context *ctx = crypto_tfm_ctx(tfm);
  585. struct n2_cipher_alg *n2alg = n2_cipher_alg(tfm);
  586. ctx->enc_type = (n2alg->enc_type & ENC_TYPE_CHAINING_MASK);
  587. switch (keylen) {
  588. case AES_KEYSIZE_128:
  589. ctx->enc_type |= ENC_TYPE_ALG_AES128;
  590. break;
  591. case AES_KEYSIZE_192:
  592. ctx->enc_type |= ENC_TYPE_ALG_AES192;
  593. break;
  594. case AES_KEYSIZE_256:
  595. ctx->enc_type |= ENC_TYPE_ALG_AES256;
  596. break;
  597. default:
  598. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  599. return -EINVAL;
  600. }
  601. ctx->key_len = keylen;
  602. memcpy(ctx->key.aes, key, keylen);
  603. return 0;
  604. }
  605. static int n2_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  606. unsigned int keylen)
  607. {
  608. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  609. struct n2_cipher_context *ctx = crypto_tfm_ctx(tfm);
  610. struct n2_cipher_alg *n2alg = n2_cipher_alg(tfm);
  611. u32 tmp[DES_EXPKEY_WORDS];
  612. int err;
  613. ctx->enc_type = n2alg->enc_type;
  614. if (keylen != DES_KEY_SIZE) {
  615. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  616. return -EINVAL;
  617. }
  618. err = des_ekey(tmp, key);
  619. if (err == 0 && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  620. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  621. return -EINVAL;
  622. }
  623. ctx->key_len = keylen;
  624. memcpy(ctx->key.des, key, keylen);
  625. return 0;
  626. }
  627. static int n2_3des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  628. unsigned int keylen)
  629. {
  630. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  631. struct n2_cipher_context *ctx = crypto_tfm_ctx(tfm);
  632. struct n2_cipher_alg *n2alg = n2_cipher_alg(tfm);
  633. ctx->enc_type = n2alg->enc_type;
  634. if (keylen != (3 * DES_KEY_SIZE)) {
  635. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  636. return -EINVAL;
  637. }
  638. ctx->key_len = keylen;
  639. memcpy(ctx->key.des3, key, keylen);
  640. return 0;
  641. }
  642. static int n2_arc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  643. unsigned int keylen)
  644. {
  645. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  646. struct n2_cipher_context *ctx = crypto_tfm_ctx(tfm);
  647. struct n2_cipher_alg *n2alg = n2_cipher_alg(tfm);
  648. u8 *s = ctx->key.arc4;
  649. u8 *x = s + 256;
  650. u8 *y = x + 1;
  651. int i, j, k;
  652. ctx->enc_type = n2alg->enc_type;
  653. j = k = 0;
  654. *x = 0;
  655. *y = 0;
  656. for (i = 0; i < 256; i++)
  657. s[i] = i;
  658. for (i = 0; i < 256; i++) {
  659. u8 a = s[i];
  660. j = (j + key[k] + a) & 0xff;
  661. s[i] = s[j];
  662. s[j] = a;
  663. if (++k >= keylen)
  664. k = 0;
  665. }
  666. return 0;
  667. }
  668. static inline int cipher_descriptor_len(int nbytes, unsigned int block_size)
  669. {
  670. int this_len = nbytes;
  671. this_len -= (nbytes & (block_size - 1));
  672. return this_len > (1 << 16) ? (1 << 16) : this_len;
  673. }
  674. static int __n2_crypt_chunk(struct crypto_tfm *tfm, struct n2_crypto_chunk *cp,
  675. struct spu_queue *qp, bool encrypt)
  676. {
  677. struct n2_cipher_context *ctx = crypto_tfm_ctx(tfm);
  678. struct cwq_initial_entry *ent;
  679. bool in_place;
  680. int i;
  681. ent = spu_queue_alloc(qp, cp->arr_len);
  682. if (!ent) {
  683. pr_info("queue_alloc() of %d fails\n",
  684. cp->arr_len);
  685. return -EBUSY;
  686. }
  687. in_place = (cp->dest_paddr == cp->arr[0].src_paddr);
  688. ent->control = control_word_base(cp->arr[0].src_len,
  689. 0, ctx->enc_type, 0, 0,
  690. false, true, false, encrypt,
  691. OPCODE_ENCRYPT |
  692. (in_place ? OPCODE_INPLACE_BIT : 0));
  693. ent->src_addr = cp->arr[0].src_paddr;
  694. ent->auth_key_addr = 0UL;
  695. ent->auth_iv_addr = 0UL;
  696. ent->final_auth_state_addr = 0UL;
  697. ent->enc_key_addr = __pa(&ctx->key);
  698. ent->enc_iv_addr = cp->iv_paddr;
  699. ent->dest_addr = (in_place ? 0UL : cp->dest_paddr);
  700. for (i = 1; i < cp->arr_len; i++) {
  701. ent = spu_queue_next(qp, ent);
  702. ent->control = cp->arr[i].src_len - 1;
  703. ent->src_addr = cp->arr[i].src_paddr;
  704. ent->auth_key_addr = 0UL;
  705. ent->auth_iv_addr = 0UL;
  706. ent->final_auth_state_addr = 0UL;
  707. ent->enc_key_addr = 0UL;
  708. ent->enc_iv_addr = 0UL;
  709. ent->dest_addr = 0UL;
  710. }
  711. ent->control |= CONTROL_END_OF_BLOCK;
  712. return (spu_queue_submit(qp, ent) != HV_EOK) ? -EINVAL : 0;
  713. }
  714. static int n2_compute_chunks(struct ablkcipher_request *req)
  715. {
  716. struct n2_request_context *rctx = ablkcipher_request_ctx(req);
  717. struct ablkcipher_walk *walk = &rctx->walk;
  718. struct n2_crypto_chunk *chunk;
  719. unsigned long dest_prev;
  720. unsigned int tot_len;
  721. bool prev_in_place;
  722. int err, nbytes;
  723. ablkcipher_walk_init(walk, req->dst, req->src, req->nbytes);
  724. err = ablkcipher_walk_phys(req, walk);
  725. if (err)
  726. return err;
  727. INIT_LIST_HEAD(&rctx->chunk_list);
  728. chunk = &rctx->chunk;
  729. INIT_LIST_HEAD(&chunk->entry);
  730. chunk->iv_paddr = 0UL;
  731. chunk->arr_len = 0;
  732. chunk->dest_paddr = 0UL;
  733. prev_in_place = false;
  734. dest_prev = ~0UL;
  735. tot_len = 0;
  736. while ((nbytes = walk->nbytes) != 0) {
  737. unsigned long dest_paddr, src_paddr;
  738. bool in_place;
  739. int this_len;
  740. src_paddr = (page_to_phys(walk->src.page) +
  741. walk->src.offset);
  742. dest_paddr = (page_to_phys(walk->dst.page) +
  743. walk->dst.offset);
  744. in_place = (src_paddr == dest_paddr);
  745. this_len = cipher_descriptor_len(nbytes, walk->blocksize);
  746. if (chunk->arr_len != 0) {
  747. if (in_place != prev_in_place ||
  748. (!prev_in_place &&
  749. dest_paddr != dest_prev) ||
  750. chunk->arr_len == N2_CHUNK_ARR_LEN ||
  751. tot_len + this_len > (1 << 16)) {
  752. chunk->dest_final = dest_prev;
  753. list_add_tail(&chunk->entry,
  754. &rctx->chunk_list);
  755. chunk = kzalloc(sizeof(*chunk), GFP_ATOMIC);
  756. if (!chunk) {
  757. err = -ENOMEM;
  758. break;
  759. }
  760. INIT_LIST_HEAD(&chunk->entry);
  761. }
  762. }
  763. if (chunk->arr_len == 0) {
  764. chunk->dest_paddr = dest_paddr;
  765. tot_len = 0;
  766. }
  767. chunk->arr[chunk->arr_len].src_paddr = src_paddr;
  768. chunk->arr[chunk->arr_len].src_len = this_len;
  769. chunk->arr_len++;
  770. dest_prev = dest_paddr + this_len;
  771. prev_in_place = in_place;
  772. tot_len += this_len;
  773. err = ablkcipher_walk_done(req, walk, nbytes - this_len);
  774. if (err)
  775. break;
  776. }
  777. if (!err && chunk->arr_len != 0) {
  778. chunk->dest_final = dest_prev;
  779. list_add_tail(&chunk->entry, &rctx->chunk_list);
  780. }
  781. return err;
  782. }
  783. static void n2_chunk_complete(struct ablkcipher_request *req, void *final_iv)
  784. {
  785. struct n2_request_context *rctx = ablkcipher_request_ctx(req);
  786. struct n2_crypto_chunk *c, *tmp;
  787. if (final_iv)
  788. memcpy(rctx->walk.iv, final_iv, rctx->walk.blocksize);
  789. ablkcipher_walk_complete(&rctx->walk);
  790. list_for_each_entry_safe(c, tmp, &rctx->chunk_list, entry) {
  791. list_del(&c->entry);
  792. if (unlikely(c != &rctx->chunk))
  793. kfree(c);
  794. }
  795. }
  796. static int n2_do_ecb(struct ablkcipher_request *req, bool encrypt)
  797. {
  798. struct n2_request_context *rctx = ablkcipher_request_ctx(req);
  799. struct crypto_tfm *tfm = req->base.tfm;
  800. int err = n2_compute_chunks(req);
  801. struct n2_crypto_chunk *c, *tmp;
  802. unsigned long flags, hv_ret;
  803. struct spu_queue *qp;
  804. if (err)
  805. return err;
  806. qp = cpu_to_cwq[get_cpu()];
  807. err = -ENODEV;
  808. if (!qp)
  809. goto out;
  810. spin_lock_irqsave(&qp->lock, flags);
  811. list_for_each_entry_safe(c, tmp, &rctx->chunk_list, entry) {
  812. err = __n2_crypt_chunk(tfm, c, qp, encrypt);
  813. if (err)
  814. break;
  815. list_del(&c->entry);
  816. if (unlikely(c != &rctx->chunk))
  817. kfree(c);
  818. }
  819. if (!err) {
  820. hv_ret = wait_for_tail(qp);
  821. if (hv_ret != HV_EOK)
  822. err = -EINVAL;
  823. }
  824. spin_unlock_irqrestore(&qp->lock, flags);
  825. out:
  826. put_cpu();
  827. n2_chunk_complete(req, NULL);
  828. return err;
  829. }
  830. static int n2_encrypt_ecb(struct ablkcipher_request *req)
  831. {
  832. return n2_do_ecb(req, true);
  833. }
  834. static int n2_decrypt_ecb(struct ablkcipher_request *req)
  835. {
  836. return n2_do_ecb(req, false);
  837. }
  838. static int n2_do_chaining(struct ablkcipher_request *req, bool encrypt)
  839. {
  840. struct n2_request_context *rctx = ablkcipher_request_ctx(req);
  841. struct crypto_tfm *tfm = req->base.tfm;
  842. unsigned long flags, hv_ret, iv_paddr;
  843. int err = n2_compute_chunks(req);
  844. struct n2_crypto_chunk *c, *tmp;
  845. struct spu_queue *qp;
  846. void *final_iv_addr;
  847. final_iv_addr = NULL;
  848. if (err)
  849. return err;
  850. qp = cpu_to_cwq[get_cpu()];
  851. err = -ENODEV;
  852. if (!qp)
  853. goto out;
  854. spin_lock_irqsave(&qp->lock, flags);
  855. if (encrypt) {
  856. iv_paddr = __pa(rctx->walk.iv);
  857. list_for_each_entry_safe(c, tmp, &rctx->chunk_list,
  858. entry) {
  859. c->iv_paddr = iv_paddr;
  860. err = __n2_crypt_chunk(tfm, c, qp, true);
  861. if (err)
  862. break;
  863. iv_paddr = c->dest_final - rctx->walk.blocksize;
  864. list_del(&c->entry);
  865. if (unlikely(c != &rctx->chunk))
  866. kfree(c);
  867. }
  868. final_iv_addr = __va(iv_paddr);
  869. } else {
  870. list_for_each_entry_safe_reverse(c, tmp, &rctx->chunk_list,
  871. entry) {
  872. if (c == &rctx->chunk) {
  873. iv_paddr = __pa(rctx->walk.iv);
  874. } else {
  875. iv_paddr = (tmp->arr[tmp->arr_len-1].src_paddr +
  876. tmp->arr[tmp->arr_len-1].src_len -
  877. rctx->walk.blocksize);
  878. }
  879. if (!final_iv_addr) {
  880. unsigned long pa;
  881. pa = (c->arr[c->arr_len-1].src_paddr +
  882. c->arr[c->arr_len-1].src_len -
  883. rctx->walk.blocksize);
  884. final_iv_addr = rctx->temp_iv;
  885. memcpy(rctx->temp_iv, __va(pa),
  886. rctx->walk.blocksize);
  887. }
  888. c->iv_paddr = iv_paddr;
  889. err = __n2_crypt_chunk(tfm, c, qp, false);
  890. if (err)
  891. break;
  892. list_del(&c->entry);
  893. if (unlikely(c != &rctx->chunk))
  894. kfree(c);
  895. }
  896. }
  897. if (!err) {
  898. hv_ret = wait_for_tail(qp);
  899. if (hv_ret != HV_EOK)
  900. err = -EINVAL;
  901. }
  902. spin_unlock_irqrestore(&qp->lock, flags);
  903. out:
  904. put_cpu();
  905. n2_chunk_complete(req, err ? NULL : final_iv_addr);
  906. return err;
  907. }
  908. static int n2_encrypt_chaining(struct ablkcipher_request *req)
  909. {
  910. return n2_do_chaining(req, true);
  911. }
  912. static int n2_decrypt_chaining(struct ablkcipher_request *req)
  913. {
  914. return n2_do_chaining(req, false);
  915. }
  916. struct n2_cipher_tmpl {
  917. const char *name;
  918. const char *drv_name;
  919. u8 block_size;
  920. u8 enc_type;
  921. struct ablkcipher_alg ablkcipher;
  922. };
  923. static const struct n2_cipher_tmpl cipher_tmpls[] = {
  924. /* ARC4: only ECB is supported (chaining bits ignored) */
  925. { .name = "ecb(arc4)",
  926. .drv_name = "ecb-arc4",
  927. .block_size = 1,
  928. .enc_type = (ENC_TYPE_ALG_RC4_STREAM |
  929. ENC_TYPE_CHAINING_ECB),
  930. .ablkcipher = {
  931. .min_keysize = 1,
  932. .max_keysize = 256,
  933. .setkey = n2_arc4_setkey,
  934. .encrypt = n2_encrypt_ecb,
  935. .decrypt = n2_decrypt_ecb,
  936. },
  937. },
  938. /* DES: ECB CBC and CFB are supported */
  939. { .name = "ecb(des)",
  940. .drv_name = "ecb-des",
  941. .block_size = DES_BLOCK_SIZE,
  942. .enc_type = (ENC_TYPE_ALG_DES |
  943. ENC_TYPE_CHAINING_ECB),
  944. .ablkcipher = {
  945. .min_keysize = DES_KEY_SIZE,
  946. .max_keysize = DES_KEY_SIZE,
  947. .setkey = n2_des_setkey,
  948. .encrypt = n2_encrypt_ecb,
  949. .decrypt = n2_decrypt_ecb,
  950. },
  951. },
  952. { .name = "cbc(des)",
  953. .drv_name = "cbc-des",
  954. .block_size = DES_BLOCK_SIZE,
  955. .enc_type = (ENC_TYPE_ALG_DES |
  956. ENC_TYPE_CHAINING_CBC),
  957. .ablkcipher = {
  958. .ivsize = DES_BLOCK_SIZE,
  959. .min_keysize = DES_KEY_SIZE,
  960. .max_keysize = DES_KEY_SIZE,
  961. .setkey = n2_des_setkey,
  962. .encrypt = n2_encrypt_chaining,
  963. .decrypt = n2_decrypt_chaining,
  964. },
  965. },
  966. { .name = "cfb(des)",
  967. .drv_name = "cfb-des",
  968. .block_size = DES_BLOCK_SIZE,
  969. .enc_type = (ENC_TYPE_ALG_DES |
  970. ENC_TYPE_CHAINING_CFB),
  971. .ablkcipher = {
  972. .min_keysize = DES_KEY_SIZE,
  973. .max_keysize = DES_KEY_SIZE,
  974. .setkey = n2_des_setkey,
  975. .encrypt = n2_encrypt_chaining,
  976. .decrypt = n2_decrypt_chaining,
  977. },
  978. },
  979. /* 3DES: ECB CBC and CFB are supported */
  980. { .name = "ecb(des3_ede)",
  981. .drv_name = "ecb-3des",
  982. .block_size = DES_BLOCK_SIZE,
  983. .enc_type = (ENC_TYPE_ALG_3DES |
  984. ENC_TYPE_CHAINING_ECB),
  985. .ablkcipher = {
  986. .min_keysize = 3 * DES_KEY_SIZE,
  987. .max_keysize = 3 * DES_KEY_SIZE,
  988. .setkey = n2_3des_setkey,
  989. .encrypt = n2_encrypt_ecb,
  990. .decrypt = n2_decrypt_ecb,
  991. },
  992. },
  993. { .name = "cbc(des3_ede)",
  994. .drv_name = "cbc-3des",
  995. .block_size = DES_BLOCK_SIZE,
  996. .enc_type = (ENC_TYPE_ALG_3DES |
  997. ENC_TYPE_CHAINING_CBC),
  998. .ablkcipher = {
  999. .ivsize = DES_BLOCK_SIZE,
  1000. .min_keysize = 3 * DES_KEY_SIZE,
  1001. .max_keysize = 3 * DES_KEY_SIZE,
  1002. .setkey = n2_3des_setkey,
  1003. .encrypt = n2_encrypt_chaining,
  1004. .decrypt = n2_decrypt_chaining,
  1005. },
  1006. },
  1007. { .name = "cfb(des3_ede)",
  1008. .drv_name = "cfb-3des",
  1009. .block_size = DES_BLOCK_SIZE,
  1010. .enc_type = (ENC_TYPE_ALG_3DES |
  1011. ENC_TYPE_CHAINING_CFB),
  1012. .ablkcipher = {
  1013. .min_keysize = 3 * DES_KEY_SIZE,
  1014. .max_keysize = 3 * DES_KEY_SIZE,
  1015. .setkey = n2_3des_setkey,
  1016. .encrypt = n2_encrypt_chaining,
  1017. .decrypt = n2_decrypt_chaining,
  1018. },
  1019. },
  1020. /* AES: ECB CBC and CTR are supported */
  1021. { .name = "ecb(aes)",
  1022. .drv_name = "ecb-aes",
  1023. .block_size = AES_BLOCK_SIZE,
  1024. .enc_type = (ENC_TYPE_ALG_AES128 |
  1025. ENC_TYPE_CHAINING_ECB),
  1026. .ablkcipher = {
  1027. .min_keysize = AES_MIN_KEY_SIZE,
  1028. .max_keysize = AES_MAX_KEY_SIZE,
  1029. .setkey = n2_aes_setkey,
  1030. .encrypt = n2_encrypt_ecb,
  1031. .decrypt = n2_decrypt_ecb,
  1032. },
  1033. },
  1034. { .name = "cbc(aes)",
  1035. .drv_name = "cbc-aes",
  1036. .block_size = AES_BLOCK_SIZE,
  1037. .enc_type = (ENC_TYPE_ALG_AES128 |
  1038. ENC_TYPE_CHAINING_CBC),
  1039. .ablkcipher = {
  1040. .ivsize = AES_BLOCK_SIZE,
  1041. .min_keysize = AES_MIN_KEY_SIZE,
  1042. .max_keysize = AES_MAX_KEY_SIZE,
  1043. .setkey = n2_aes_setkey,
  1044. .encrypt = n2_encrypt_chaining,
  1045. .decrypt = n2_decrypt_chaining,
  1046. },
  1047. },
  1048. { .name = "ctr(aes)",
  1049. .drv_name = "ctr-aes",
  1050. .block_size = AES_BLOCK_SIZE,
  1051. .enc_type = (ENC_TYPE_ALG_AES128 |
  1052. ENC_TYPE_CHAINING_COUNTER),
  1053. .ablkcipher = {
  1054. .ivsize = AES_BLOCK_SIZE,
  1055. .min_keysize = AES_MIN_KEY_SIZE,
  1056. .max_keysize = AES_MAX_KEY_SIZE,
  1057. .setkey = n2_aes_setkey,
  1058. .encrypt = n2_encrypt_chaining,
  1059. .decrypt = n2_encrypt_chaining,
  1060. },
  1061. },
  1062. };
  1063. #define NUM_CIPHER_TMPLS ARRAY_SIZE(cipher_tmpls)
  1064. static LIST_HEAD(cipher_algs);
  1065. struct n2_hash_tmpl {
  1066. const char *name;
  1067. const u8 *hash_zero;
  1068. const u32 *hash_init;
  1069. u8 hw_op_hashsz;
  1070. u8 digest_size;
  1071. u8 block_size;
  1072. u8 auth_type;
  1073. u8 hmac_type;
  1074. };
  1075. static const u32 md5_init[MD5_HASH_WORDS] = {
  1076. cpu_to_le32(MD5_H0),
  1077. cpu_to_le32(MD5_H1),
  1078. cpu_to_le32(MD5_H2),
  1079. cpu_to_le32(MD5_H3),
  1080. };
  1081. static const u32 sha1_init[SHA1_DIGEST_SIZE / 4] = {
  1082. SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4,
  1083. };
  1084. static const u32 sha256_init[SHA256_DIGEST_SIZE / 4] = {
  1085. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  1086. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7,
  1087. };
  1088. static const u32 sha224_init[SHA256_DIGEST_SIZE / 4] = {
  1089. SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
  1090. SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
  1091. };
  1092. static const struct n2_hash_tmpl hash_tmpls[] = {
  1093. { .name = "md5",
  1094. .hash_zero = md5_zero_message_hash,
  1095. .hash_init = md5_init,
  1096. .auth_type = AUTH_TYPE_MD5,
  1097. .hmac_type = AUTH_TYPE_HMAC_MD5,
  1098. .hw_op_hashsz = MD5_DIGEST_SIZE,
  1099. .digest_size = MD5_DIGEST_SIZE,
  1100. .block_size = MD5_HMAC_BLOCK_SIZE },
  1101. { .name = "sha1",
  1102. .hash_zero = sha1_zero_message_hash,
  1103. .hash_init = sha1_init,
  1104. .auth_type = AUTH_TYPE_SHA1,
  1105. .hmac_type = AUTH_TYPE_HMAC_SHA1,
  1106. .hw_op_hashsz = SHA1_DIGEST_SIZE,
  1107. .digest_size = SHA1_DIGEST_SIZE,
  1108. .block_size = SHA1_BLOCK_SIZE },
  1109. { .name = "sha256",
  1110. .hash_zero = sha256_zero_message_hash,
  1111. .hash_init = sha256_init,
  1112. .auth_type = AUTH_TYPE_SHA256,
  1113. .hmac_type = AUTH_TYPE_HMAC_SHA256,
  1114. .hw_op_hashsz = SHA256_DIGEST_SIZE,
  1115. .digest_size = SHA256_DIGEST_SIZE,
  1116. .block_size = SHA256_BLOCK_SIZE },
  1117. { .name = "sha224",
  1118. .hash_zero = sha224_zero_message_hash,
  1119. .hash_init = sha224_init,
  1120. .auth_type = AUTH_TYPE_SHA256,
  1121. .hmac_type = AUTH_TYPE_RESERVED,
  1122. .hw_op_hashsz = SHA256_DIGEST_SIZE,
  1123. .digest_size = SHA224_DIGEST_SIZE,
  1124. .block_size = SHA224_BLOCK_SIZE },
  1125. };
  1126. #define NUM_HASH_TMPLS ARRAY_SIZE(hash_tmpls)
  1127. static LIST_HEAD(ahash_algs);
  1128. static LIST_HEAD(hmac_algs);
  1129. static int algs_registered;
  1130. static void __n2_unregister_algs(void)
  1131. {
  1132. struct n2_cipher_alg *cipher, *cipher_tmp;
  1133. struct n2_ahash_alg *alg, *alg_tmp;
  1134. struct n2_hmac_alg *hmac, *hmac_tmp;
  1135. list_for_each_entry_safe(cipher, cipher_tmp, &cipher_algs, entry) {
  1136. crypto_unregister_alg(&cipher->alg);
  1137. list_del(&cipher->entry);
  1138. kfree(cipher);
  1139. }
  1140. list_for_each_entry_safe(hmac, hmac_tmp, &hmac_algs, derived.entry) {
  1141. crypto_unregister_ahash(&hmac->derived.alg);
  1142. list_del(&hmac->derived.entry);
  1143. kfree(hmac);
  1144. }
  1145. list_for_each_entry_safe(alg, alg_tmp, &ahash_algs, entry) {
  1146. crypto_unregister_ahash(&alg->alg);
  1147. list_del(&alg->entry);
  1148. kfree(alg);
  1149. }
  1150. }
  1151. static int n2_cipher_cra_init(struct crypto_tfm *tfm)
  1152. {
  1153. tfm->crt_ablkcipher.reqsize = sizeof(struct n2_request_context);
  1154. return 0;
  1155. }
  1156. static int __n2_register_one_cipher(const struct n2_cipher_tmpl *tmpl)
  1157. {
  1158. struct n2_cipher_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1159. struct crypto_alg *alg;
  1160. int err;
  1161. if (!p)
  1162. return -ENOMEM;
  1163. alg = &p->alg;
  1164. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1165. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-n2", tmpl->drv_name);
  1166. alg->cra_priority = N2_CRA_PRIORITY;
  1167. alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1168. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
  1169. alg->cra_blocksize = tmpl->block_size;
  1170. p->enc_type = tmpl->enc_type;
  1171. alg->cra_ctxsize = sizeof(struct n2_cipher_context);
  1172. alg->cra_type = &crypto_ablkcipher_type;
  1173. alg->cra_u.ablkcipher = tmpl->ablkcipher;
  1174. alg->cra_init = n2_cipher_cra_init;
  1175. alg->cra_module = THIS_MODULE;
  1176. list_add(&p->entry, &cipher_algs);
  1177. err = crypto_register_alg(alg);
  1178. if (err) {
  1179. pr_err("%s alg registration failed\n", alg->cra_name);
  1180. list_del(&p->entry);
  1181. kfree(p);
  1182. } else {
  1183. pr_info("%s alg registered\n", alg->cra_name);
  1184. }
  1185. return err;
  1186. }
  1187. static int __n2_register_one_hmac(struct n2_ahash_alg *n2ahash)
  1188. {
  1189. struct n2_hmac_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1190. struct ahash_alg *ahash;
  1191. struct crypto_alg *base;
  1192. int err;
  1193. if (!p)
  1194. return -ENOMEM;
  1195. p->child_alg = n2ahash->alg.halg.base.cra_name;
  1196. memcpy(&p->derived, n2ahash, sizeof(struct n2_ahash_alg));
  1197. INIT_LIST_HEAD(&p->derived.entry);
  1198. ahash = &p->derived.alg;
  1199. ahash->digest = n2_hmac_async_digest;
  1200. ahash->setkey = n2_hmac_async_setkey;
  1201. base = &ahash->halg.base;
  1202. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", p->child_alg);
  1203. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s-n2", p->child_alg);
  1204. base->cra_ctxsize = sizeof(struct n2_hmac_ctx);
  1205. base->cra_init = n2_hmac_cra_init;
  1206. base->cra_exit = n2_hmac_cra_exit;
  1207. list_add(&p->derived.entry, &hmac_algs);
  1208. err = crypto_register_ahash(ahash);
  1209. if (err) {
  1210. pr_err("%s alg registration failed\n", base->cra_name);
  1211. list_del(&p->derived.entry);
  1212. kfree(p);
  1213. } else {
  1214. pr_info("%s alg registered\n", base->cra_name);
  1215. }
  1216. return err;
  1217. }
  1218. static int __n2_register_one_ahash(const struct n2_hash_tmpl *tmpl)
  1219. {
  1220. struct n2_ahash_alg *p = kzalloc(sizeof(*p), GFP_KERNEL);
  1221. struct hash_alg_common *halg;
  1222. struct crypto_alg *base;
  1223. struct ahash_alg *ahash;
  1224. int err;
  1225. if (!p)
  1226. return -ENOMEM;
  1227. p->hash_zero = tmpl->hash_zero;
  1228. p->hash_init = tmpl->hash_init;
  1229. p->auth_type = tmpl->auth_type;
  1230. p->hmac_type = tmpl->hmac_type;
  1231. p->hw_op_hashsz = tmpl->hw_op_hashsz;
  1232. p->digest_size = tmpl->digest_size;
  1233. ahash = &p->alg;
  1234. ahash->init = n2_hash_async_init;
  1235. ahash->update = n2_hash_async_update;
  1236. ahash->final = n2_hash_async_final;
  1237. ahash->finup = n2_hash_async_finup;
  1238. ahash->digest = n2_hash_async_digest;
  1239. halg = &ahash->halg;
  1240. halg->digestsize = tmpl->digest_size;
  1241. base = &halg->base;
  1242. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1243. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-n2", tmpl->name);
  1244. base->cra_priority = N2_CRA_PRIORITY;
  1245. base->cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1246. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1247. CRYPTO_ALG_NEED_FALLBACK;
  1248. base->cra_blocksize = tmpl->block_size;
  1249. base->cra_ctxsize = sizeof(struct n2_hash_ctx);
  1250. base->cra_module = THIS_MODULE;
  1251. base->cra_init = n2_hash_cra_init;
  1252. base->cra_exit = n2_hash_cra_exit;
  1253. list_add(&p->entry, &ahash_algs);
  1254. err = crypto_register_ahash(ahash);
  1255. if (err) {
  1256. pr_err("%s alg registration failed\n", base->cra_name);
  1257. list_del(&p->entry);
  1258. kfree(p);
  1259. } else {
  1260. pr_info("%s alg registered\n", base->cra_name);
  1261. }
  1262. if (!err && p->hmac_type != AUTH_TYPE_RESERVED)
  1263. err = __n2_register_one_hmac(p);
  1264. return err;
  1265. }
  1266. static int n2_register_algs(void)
  1267. {
  1268. int i, err = 0;
  1269. mutex_lock(&spu_lock);
  1270. if (algs_registered++)
  1271. goto out;
  1272. for (i = 0; i < NUM_HASH_TMPLS; i++) {
  1273. err = __n2_register_one_ahash(&hash_tmpls[i]);
  1274. if (err) {
  1275. __n2_unregister_algs();
  1276. goto out;
  1277. }
  1278. }
  1279. for (i = 0; i < NUM_CIPHER_TMPLS; i++) {
  1280. err = __n2_register_one_cipher(&cipher_tmpls[i]);
  1281. if (err) {
  1282. __n2_unregister_algs();
  1283. goto out;
  1284. }
  1285. }
  1286. out:
  1287. mutex_unlock(&spu_lock);
  1288. return err;
  1289. }
  1290. static void n2_unregister_algs(void)
  1291. {
  1292. mutex_lock(&spu_lock);
  1293. if (!--algs_registered)
  1294. __n2_unregister_algs();
  1295. mutex_unlock(&spu_lock);
  1296. }
  1297. /* To map CWQ queues to interrupt sources, the hypervisor API provides
  1298. * a devino. This isn't very useful to us because all of the
  1299. * interrupts listed in the device_node have been translated to
  1300. * Linux virtual IRQ cookie numbers.
  1301. *
  1302. * So we have to back-translate, going through the 'intr' and 'ino'
  1303. * property tables of the n2cp MDESC node, matching it with the OF
  1304. * 'interrupts' property entries, in order to to figure out which
  1305. * devino goes to which already-translated IRQ.
  1306. */
  1307. static int find_devino_index(struct platform_device *dev, struct spu_mdesc_info *ip,
  1308. unsigned long dev_ino)
  1309. {
  1310. const unsigned int *dev_intrs;
  1311. unsigned int intr;
  1312. int i;
  1313. for (i = 0; i < ip->num_intrs; i++) {
  1314. if (ip->ino_table[i].ino == dev_ino)
  1315. break;
  1316. }
  1317. if (i == ip->num_intrs)
  1318. return -ENODEV;
  1319. intr = ip->ino_table[i].intr;
  1320. dev_intrs = of_get_property(dev->dev.of_node, "interrupts", NULL);
  1321. if (!dev_intrs)
  1322. return -ENODEV;
  1323. for (i = 0; i < dev->archdata.num_irqs; i++) {
  1324. if (dev_intrs[i] == intr)
  1325. return i;
  1326. }
  1327. return -ENODEV;
  1328. }
  1329. static int spu_map_ino(struct platform_device *dev, struct spu_mdesc_info *ip,
  1330. const char *irq_name, struct spu_queue *p,
  1331. irq_handler_t handler)
  1332. {
  1333. unsigned long herr;
  1334. int index;
  1335. herr = sun4v_ncs_qhandle_to_devino(p->qhandle, &p->devino);
  1336. if (herr)
  1337. return -EINVAL;
  1338. index = find_devino_index(dev, ip, p->devino);
  1339. if (index < 0)
  1340. return index;
  1341. p->irq = dev->archdata.irqs[index];
  1342. sprintf(p->irq_name, "%s-%d", irq_name, index);
  1343. return request_irq(p->irq, handler, 0, p->irq_name, p);
  1344. }
  1345. static struct kmem_cache *queue_cache[2];
  1346. static void *new_queue(unsigned long q_type)
  1347. {
  1348. return kmem_cache_zalloc(queue_cache[q_type - 1], GFP_KERNEL);
  1349. }
  1350. static void free_queue(void *p, unsigned long q_type)
  1351. {
  1352. kmem_cache_free(queue_cache[q_type - 1], p);
  1353. }
  1354. static int queue_cache_init(void)
  1355. {
  1356. if (!queue_cache[HV_NCS_QTYPE_MAU - 1])
  1357. queue_cache[HV_NCS_QTYPE_MAU - 1] =
  1358. kmem_cache_create("mau_queue",
  1359. (MAU_NUM_ENTRIES *
  1360. MAU_ENTRY_SIZE),
  1361. MAU_ENTRY_SIZE, 0, NULL);
  1362. if (!queue_cache[HV_NCS_QTYPE_MAU - 1])
  1363. return -ENOMEM;
  1364. if (!queue_cache[HV_NCS_QTYPE_CWQ - 1])
  1365. queue_cache[HV_NCS_QTYPE_CWQ - 1] =
  1366. kmem_cache_create("cwq_queue",
  1367. (CWQ_NUM_ENTRIES *
  1368. CWQ_ENTRY_SIZE),
  1369. CWQ_ENTRY_SIZE, 0, NULL);
  1370. if (!queue_cache[HV_NCS_QTYPE_CWQ - 1]) {
  1371. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_MAU - 1]);
  1372. queue_cache[HV_NCS_QTYPE_MAU - 1] = NULL;
  1373. return -ENOMEM;
  1374. }
  1375. return 0;
  1376. }
  1377. static void queue_cache_destroy(void)
  1378. {
  1379. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_MAU - 1]);
  1380. kmem_cache_destroy(queue_cache[HV_NCS_QTYPE_CWQ - 1]);
  1381. queue_cache[HV_NCS_QTYPE_MAU - 1] = NULL;
  1382. queue_cache[HV_NCS_QTYPE_CWQ - 1] = NULL;
  1383. }
  1384. static long spu_queue_register_workfn(void *arg)
  1385. {
  1386. struct spu_qreg *qr = arg;
  1387. struct spu_queue *p = qr->queue;
  1388. unsigned long q_type = qr->type;
  1389. unsigned long hv_ret;
  1390. hv_ret = sun4v_ncs_qconf(q_type, __pa(p->q),
  1391. CWQ_NUM_ENTRIES, &p->qhandle);
  1392. if (!hv_ret)
  1393. sun4v_ncs_sethead_marker(p->qhandle, 0);
  1394. return hv_ret ? -EINVAL : 0;
  1395. }
  1396. static int spu_queue_register(struct spu_queue *p, unsigned long q_type)
  1397. {
  1398. int cpu = cpumask_any_and(&p->sharing, cpu_online_mask);
  1399. struct spu_qreg qr = { .queue = p, .type = q_type };
  1400. return work_on_cpu_safe(cpu, spu_queue_register_workfn, &qr);
  1401. }
  1402. static int spu_queue_setup(struct spu_queue *p)
  1403. {
  1404. int err;
  1405. p->q = new_queue(p->q_type);
  1406. if (!p->q)
  1407. return -ENOMEM;
  1408. err = spu_queue_register(p, p->q_type);
  1409. if (err) {
  1410. free_queue(p->q, p->q_type);
  1411. p->q = NULL;
  1412. }
  1413. return err;
  1414. }
  1415. static void spu_queue_destroy(struct spu_queue *p)
  1416. {
  1417. unsigned long hv_ret;
  1418. if (!p->q)
  1419. return;
  1420. hv_ret = sun4v_ncs_qconf(p->q_type, p->qhandle, 0, &p->qhandle);
  1421. if (!hv_ret)
  1422. free_queue(p->q, p->q_type);
  1423. }
  1424. static void spu_list_destroy(struct list_head *list)
  1425. {
  1426. struct spu_queue *p, *n;
  1427. list_for_each_entry_safe(p, n, list, list) {
  1428. int i;
  1429. for (i = 0; i < NR_CPUS; i++) {
  1430. if (cpu_to_cwq[i] == p)
  1431. cpu_to_cwq[i] = NULL;
  1432. }
  1433. if (p->irq) {
  1434. free_irq(p->irq, p);
  1435. p->irq = 0;
  1436. }
  1437. spu_queue_destroy(p);
  1438. list_del(&p->list);
  1439. kfree(p);
  1440. }
  1441. }
  1442. /* Walk the backward arcs of a CWQ 'exec-unit' node,
  1443. * gathering cpu membership information.
  1444. */
  1445. static int spu_mdesc_walk_arcs(struct mdesc_handle *mdesc,
  1446. struct platform_device *dev,
  1447. u64 node, struct spu_queue *p,
  1448. struct spu_queue **table)
  1449. {
  1450. u64 arc;
  1451. mdesc_for_each_arc(arc, mdesc, node, MDESC_ARC_TYPE_BACK) {
  1452. u64 tgt = mdesc_arc_target(mdesc, arc);
  1453. const char *name = mdesc_node_name(mdesc, tgt);
  1454. const u64 *id;
  1455. if (strcmp(name, "cpu"))
  1456. continue;
  1457. id = mdesc_get_property(mdesc, tgt, "id", NULL);
  1458. if (table[*id] != NULL) {
  1459. dev_err(&dev->dev, "%pOF: SPU cpu slot already set.\n",
  1460. dev->dev.of_node);
  1461. return -EINVAL;
  1462. }
  1463. cpumask_set_cpu(*id, &p->sharing);
  1464. table[*id] = p;
  1465. }
  1466. return 0;
  1467. }
  1468. /* Process an 'exec-unit' MDESC node of type 'cwq'. */
  1469. static int handle_exec_unit(struct spu_mdesc_info *ip, struct list_head *list,
  1470. struct platform_device *dev, struct mdesc_handle *mdesc,
  1471. u64 node, const char *iname, unsigned long q_type,
  1472. irq_handler_t handler, struct spu_queue **table)
  1473. {
  1474. struct spu_queue *p;
  1475. int err;
  1476. p = kzalloc(sizeof(struct spu_queue), GFP_KERNEL);
  1477. if (!p) {
  1478. dev_err(&dev->dev, "%pOF: Could not allocate SPU queue.\n",
  1479. dev->dev.of_node);
  1480. return -ENOMEM;
  1481. }
  1482. cpumask_clear(&p->sharing);
  1483. spin_lock_init(&p->lock);
  1484. p->q_type = q_type;
  1485. INIT_LIST_HEAD(&p->jobs);
  1486. list_add(&p->list, list);
  1487. err = spu_mdesc_walk_arcs(mdesc, dev, node, p, table);
  1488. if (err)
  1489. return err;
  1490. err = spu_queue_setup(p);
  1491. if (err)
  1492. return err;
  1493. return spu_map_ino(dev, ip, iname, p, handler);
  1494. }
  1495. static int spu_mdesc_scan(struct mdesc_handle *mdesc, struct platform_device *dev,
  1496. struct spu_mdesc_info *ip, struct list_head *list,
  1497. const char *exec_name, unsigned long q_type,
  1498. irq_handler_t handler, struct spu_queue **table)
  1499. {
  1500. int err = 0;
  1501. u64 node;
  1502. mdesc_for_each_node_by_name(mdesc, node, "exec-unit") {
  1503. const char *type;
  1504. type = mdesc_get_property(mdesc, node, "type", NULL);
  1505. if (!type || strcmp(type, exec_name))
  1506. continue;
  1507. err = handle_exec_unit(ip, list, dev, mdesc, node,
  1508. exec_name, q_type, handler, table);
  1509. if (err) {
  1510. spu_list_destroy(list);
  1511. break;
  1512. }
  1513. }
  1514. return err;
  1515. }
  1516. static int get_irq_props(struct mdesc_handle *mdesc, u64 node,
  1517. struct spu_mdesc_info *ip)
  1518. {
  1519. const u64 *ino;
  1520. int ino_len;
  1521. int i;
  1522. ino = mdesc_get_property(mdesc, node, "ino", &ino_len);
  1523. if (!ino) {
  1524. printk("NO 'ino'\n");
  1525. return -ENODEV;
  1526. }
  1527. ip->num_intrs = ino_len / sizeof(u64);
  1528. ip->ino_table = kzalloc((sizeof(struct ino_blob) *
  1529. ip->num_intrs),
  1530. GFP_KERNEL);
  1531. if (!ip->ino_table)
  1532. return -ENOMEM;
  1533. for (i = 0; i < ip->num_intrs; i++) {
  1534. struct ino_blob *b = &ip->ino_table[i];
  1535. b->intr = i + 1;
  1536. b->ino = ino[i];
  1537. }
  1538. return 0;
  1539. }
  1540. static int grab_mdesc_irq_props(struct mdesc_handle *mdesc,
  1541. struct platform_device *dev,
  1542. struct spu_mdesc_info *ip,
  1543. const char *node_name)
  1544. {
  1545. const unsigned int *reg;
  1546. u64 node;
  1547. reg = of_get_property(dev->dev.of_node, "reg", NULL);
  1548. if (!reg)
  1549. return -ENODEV;
  1550. mdesc_for_each_node_by_name(mdesc, node, "virtual-device") {
  1551. const char *name;
  1552. const u64 *chdl;
  1553. name = mdesc_get_property(mdesc, node, "name", NULL);
  1554. if (!name || strcmp(name, node_name))
  1555. continue;
  1556. chdl = mdesc_get_property(mdesc, node, "cfg-handle", NULL);
  1557. if (!chdl || (*chdl != *reg))
  1558. continue;
  1559. ip->cfg_handle = *chdl;
  1560. return get_irq_props(mdesc, node, ip);
  1561. }
  1562. return -ENODEV;
  1563. }
  1564. static unsigned long n2_spu_hvapi_major;
  1565. static unsigned long n2_spu_hvapi_minor;
  1566. static int n2_spu_hvapi_register(void)
  1567. {
  1568. int err;
  1569. n2_spu_hvapi_major = 2;
  1570. n2_spu_hvapi_minor = 0;
  1571. err = sun4v_hvapi_register(HV_GRP_NCS,
  1572. n2_spu_hvapi_major,
  1573. &n2_spu_hvapi_minor);
  1574. if (!err)
  1575. pr_info("Registered NCS HVAPI version %lu.%lu\n",
  1576. n2_spu_hvapi_major,
  1577. n2_spu_hvapi_minor);
  1578. return err;
  1579. }
  1580. static void n2_spu_hvapi_unregister(void)
  1581. {
  1582. sun4v_hvapi_unregister(HV_GRP_NCS);
  1583. }
  1584. static int global_ref;
  1585. static int grab_global_resources(void)
  1586. {
  1587. int err = 0;
  1588. mutex_lock(&spu_lock);
  1589. if (global_ref++)
  1590. goto out;
  1591. err = n2_spu_hvapi_register();
  1592. if (err)
  1593. goto out;
  1594. err = queue_cache_init();
  1595. if (err)
  1596. goto out_hvapi_release;
  1597. err = -ENOMEM;
  1598. cpu_to_cwq = kzalloc(sizeof(struct spu_queue *) * NR_CPUS,
  1599. GFP_KERNEL);
  1600. if (!cpu_to_cwq)
  1601. goto out_queue_cache_destroy;
  1602. cpu_to_mau = kzalloc(sizeof(struct spu_queue *) * NR_CPUS,
  1603. GFP_KERNEL);
  1604. if (!cpu_to_mau)
  1605. goto out_free_cwq_table;
  1606. err = 0;
  1607. out:
  1608. if (err)
  1609. global_ref--;
  1610. mutex_unlock(&spu_lock);
  1611. return err;
  1612. out_free_cwq_table:
  1613. kfree(cpu_to_cwq);
  1614. cpu_to_cwq = NULL;
  1615. out_queue_cache_destroy:
  1616. queue_cache_destroy();
  1617. out_hvapi_release:
  1618. n2_spu_hvapi_unregister();
  1619. goto out;
  1620. }
  1621. static void release_global_resources(void)
  1622. {
  1623. mutex_lock(&spu_lock);
  1624. if (!--global_ref) {
  1625. kfree(cpu_to_cwq);
  1626. cpu_to_cwq = NULL;
  1627. kfree(cpu_to_mau);
  1628. cpu_to_mau = NULL;
  1629. queue_cache_destroy();
  1630. n2_spu_hvapi_unregister();
  1631. }
  1632. mutex_unlock(&spu_lock);
  1633. }
  1634. static struct n2_crypto *alloc_n2cp(void)
  1635. {
  1636. struct n2_crypto *np = kzalloc(sizeof(struct n2_crypto), GFP_KERNEL);
  1637. if (np)
  1638. INIT_LIST_HEAD(&np->cwq_list);
  1639. return np;
  1640. }
  1641. static void free_n2cp(struct n2_crypto *np)
  1642. {
  1643. if (np->cwq_info.ino_table) {
  1644. kfree(np->cwq_info.ino_table);
  1645. np->cwq_info.ino_table = NULL;
  1646. }
  1647. kfree(np);
  1648. }
  1649. static void n2_spu_driver_version(void)
  1650. {
  1651. static int n2_spu_version_printed;
  1652. if (n2_spu_version_printed++ == 0)
  1653. pr_info("%s", version);
  1654. }
  1655. static int n2_crypto_probe(struct platform_device *dev)
  1656. {
  1657. struct mdesc_handle *mdesc;
  1658. struct n2_crypto *np;
  1659. int err;
  1660. n2_spu_driver_version();
  1661. pr_info("Found N2CP at %pOF\n", dev->dev.of_node);
  1662. np = alloc_n2cp();
  1663. if (!np) {
  1664. dev_err(&dev->dev, "%pOF: Unable to allocate n2cp.\n",
  1665. dev->dev.of_node);
  1666. return -ENOMEM;
  1667. }
  1668. err = grab_global_resources();
  1669. if (err) {
  1670. dev_err(&dev->dev, "%pOF: Unable to grab global resources.\n",
  1671. dev->dev.of_node);
  1672. goto out_free_n2cp;
  1673. }
  1674. mdesc = mdesc_grab();
  1675. if (!mdesc) {
  1676. dev_err(&dev->dev, "%pOF: Unable to grab MDESC.\n",
  1677. dev->dev.of_node);
  1678. err = -ENODEV;
  1679. goto out_free_global;
  1680. }
  1681. err = grab_mdesc_irq_props(mdesc, dev, &np->cwq_info, "n2cp");
  1682. if (err) {
  1683. dev_err(&dev->dev, "%pOF: Unable to grab IRQ props.\n",
  1684. dev->dev.of_node);
  1685. mdesc_release(mdesc);
  1686. goto out_free_global;
  1687. }
  1688. err = spu_mdesc_scan(mdesc, dev, &np->cwq_info, &np->cwq_list,
  1689. "cwq", HV_NCS_QTYPE_CWQ, cwq_intr,
  1690. cpu_to_cwq);
  1691. mdesc_release(mdesc);
  1692. if (err) {
  1693. dev_err(&dev->dev, "%pOF: CWQ MDESC scan failed.\n",
  1694. dev->dev.of_node);
  1695. goto out_free_global;
  1696. }
  1697. err = n2_register_algs();
  1698. if (err) {
  1699. dev_err(&dev->dev, "%pOF: Unable to register algorithms.\n",
  1700. dev->dev.of_node);
  1701. goto out_free_spu_list;
  1702. }
  1703. dev_set_drvdata(&dev->dev, np);
  1704. return 0;
  1705. out_free_spu_list:
  1706. spu_list_destroy(&np->cwq_list);
  1707. out_free_global:
  1708. release_global_resources();
  1709. out_free_n2cp:
  1710. free_n2cp(np);
  1711. return err;
  1712. }
  1713. static int n2_crypto_remove(struct platform_device *dev)
  1714. {
  1715. struct n2_crypto *np = dev_get_drvdata(&dev->dev);
  1716. n2_unregister_algs();
  1717. spu_list_destroy(&np->cwq_list);
  1718. release_global_resources();
  1719. free_n2cp(np);
  1720. return 0;
  1721. }
  1722. static struct n2_mau *alloc_ncp(void)
  1723. {
  1724. struct n2_mau *mp = kzalloc(sizeof(struct n2_mau), GFP_KERNEL);
  1725. if (mp)
  1726. INIT_LIST_HEAD(&mp->mau_list);
  1727. return mp;
  1728. }
  1729. static void free_ncp(struct n2_mau *mp)
  1730. {
  1731. if (mp->mau_info.ino_table) {
  1732. kfree(mp->mau_info.ino_table);
  1733. mp->mau_info.ino_table = NULL;
  1734. }
  1735. kfree(mp);
  1736. }
  1737. static int n2_mau_probe(struct platform_device *dev)
  1738. {
  1739. struct mdesc_handle *mdesc;
  1740. struct n2_mau *mp;
  1741. int err;
  1742. n2_spu_driver_version();
  1743. pr_info("Found NCP at %pOF\n", dev->dev.of_node);
  1744. mp = alloc_ncp();
  1745. if (!mp) {
  1746. dev_err(&dev->dev, "%pOF: Unable to allocate ncp.\n",
  1747. dev->dev.of_node);
  1748. return -ENOMEM;
  1749. }
  1750. err = grab_global_resources();
  1751. if (err) {
  1752. dev_err(&dev->dev, "%pOF: Unable to grab global resources.\n",
  1753. dev->dev.of_node);
  1754. goto out_free_ncp;
  1755. }
  1756. mdesc = mdesc_grab();
  1757. if (!mdesc) {
  1758. dev_err(&dev->dev, "%pOF: Unable to grab MDESC.\n",
  1759. dev->dev.of_node);
  1760. err = -ENODEV;
  1761. goto out_free_global;
  1762. }
  1763. err = grab_mdesc_irq_props(mdesc, dev, &mp->mau_info, "ncp");
  1764. if (err) {
  1765. dev_err(&dev->dev, "%pOF: Unable to grab IRQ props.\n",
  1766. dev->dev.of_node);
  1767. mdesc_release(mdesc);
  1768. goto out_free_global;
  1769. }
  1770. err = spu_mdesc_scan(mdesc, dev, &mp->mau_info, &mp->mau_list,
  1771. "mau", HV_NCS_QTYPE_MAU, mau_intr,
  1772. cpu_to_mau);
  1773. mdesc_release(mdesc);
  1774. if (err) {
  1775. dev_err(&dev->dev, "%pOF: MAU MDESC scan failed.\n",
  1776. dev->dev.of_node);
  1777. goto out_free_global;
  1778. }
  1779. dev_set_drvdata(&dev->dev, mp);
  1780. return 0;
  1781. out_free_global:
  1782. release_global_resources();
  1783. out_free_ncp:
  1784. free_ncp(mp);
  1785. return err;
  1786. }
  1787. static int n2_mau_remove(struct platform_device *dev)
  1788. {
  1789. struct n2_mau *mp = dev_get_drvdata(&dev->dev);
  1790. spu_list_destroy(&mp->mau_list);
  1791. release_global_resources();
  1792. free_ncp(mp);
  1793. return 0;
  1794. }
  1795. static const struct of_device_id n2_crypto_match[] = {
  1796. {
  1797. .name = "n2cp",
  1798. .compatible = "SUNW,n2-cwq",
  1799. },
  1800. {
  1801. .name = "n2cp",
  1802. .compatible = "SUNW,vf-cwq",
  1803. },
  1804. {
  1805. .name = "n2cp",
  1806. .compatible = "SUNW,kt-cwq",
  1807. },
  1808. {},
  1809. };
  1810. MODULE_DEVICE_TABLE(of, n2_crypto_match);
  1811. static struct platform_driver n2_crypto_driver = {
  1812. .driver = {
  1813. .name = "n2cp",
  1814. .of_match_table = n2_crypto_match,
  1815. },
  1816. .probe = n2_crypto_probe,
  1817. .remove = n2_crypto_remove,
  1818. };
  1819. static const struct of_device_id n2_mau_match[] = {
  1820. {
  1821. .name = "ncp",
  1822. .compatible = "SUNW,n2-mau",
  1823. },
  1824. {
  1825. .name = "ncp",
  1826. .compatible = "SUNW,vf-mau",
  1827. },
  1828. {
  1829. .name = "ncp",
  1830. .compatible = "SUNW,kt-mau",
  1831. },
  1832. {},
  1833. };
  1834. MODULE_DEVICE_TABLE(of, n2_mau_match);
  1835. static struct platform_driver n2_mau_driver = {
  1836. .driver = {
  1837. .name = "ncp",
  1838. .of_match_table = n2_mau_match,
  1839. },
  1840. .probe = n2_mau_probe,
  1841. .remove = n2_mau_remove,
  1842. };
  1843. static struct platform_driver * const drivers[] = {
  1844. &n2_crypto_driver,
  1845. &n2_mau_driver,
  1846. };
  1847. static int __init n2_init(void)
  1848. {
  1849. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  1850. }
  1851. static void __exit n2_exit(void)
  1852. {
  1853. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  1854. }
  1855. module_init(n2_init);
  1856. module_exit(n2_exit);