n2_core.c 47 KB

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