n2_core.c 47 KB

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