n2_core.c 51 KB

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