picoxcell_crypto.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*
  2. * Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <crypto/internal/aead.h>
  19. #include <crypto/aes.h>
  20. #include <crypto/algapi.h>
  21. #include <crypto/authenc.h>
  22. #include <crypto/des.h>
  23. #include <crypto/md5.h>
  24. #include <crypto/sha.h>
  25. #include <crypto/internal/skcipher.h>
  26. #include <linux/clk.h>
  27. #include <linux/crypto.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/dmapool.h>
  31. #include <linux/err.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/list.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/pm.h>
  40. #include <linux/rtnetlink.h>
  41. #include <linux/scatterlist.h>
  42. #include <linux/sched.h>
  43. #include <linux/sizes.h>
  44. #include <linux/slab.h>
  45. #include <linux/timer.h>
  46. #include "picoxcell_crypto_regs.h"
  47. /*
  48. * The threshold for the number of entries in the CMD FIFO available before
  49. * the CMD0_CNT interrupt is raised. Increasing this value will reduce the
  50. * number of interrupts raised to the CPU.
  51. */
  52. #define CMD0_IRQ_THRESHOLD 1
  53. /*
  54. * The timeout period (in jiffies) for a PDU. When the the number of PDUs in
  55. * flight is greater than the STAT_IRQ_THRESHOLD or 0 the timer is disabled.
  56. * When there are packets in flight but lower than the threshold, we enable
  57. * the timer and at expiry, attempt to remove any processed packets from the
  58. * queue and if there are still packets left, schedule the timer again.
  59. */
  60. #define PACKET_TIMEOUT 1
  61. /* The priority to register each algorithm with. */
  62. #define SPACC_CRYPTO_ALG_PRIORITY 10000
  63. #define SPACC_CRYPTO_KASUMI_F8_KEY_LEN 16
  64. #define SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ 64
  65. #define SPACC_CRYPTO_IPSEC_HASH_PG_SZ 64
  66. #define SPACC_CRYPTO_IPSEC_MAX_CTXS 32
  67. #define SPACC_CRYPTO_IPSEC_FIFO_SZ 32
  68. #define SPACC_CRYPTO_L2_CIPHER_PG_SZ 64
  69. #define SPACC_CRYPTO_L2_HASH_PG_SZ 64
  70. #define SPACC_CRYPTO_L2_MAX_CTXS 128
  71. #define SPACC_CRYPTO_L2_FIFO_SZ 128
  72. #define MAX_DDT_LEN 16
  73. /* DDT format. This must match the hardware DDT format exactly. */
  74. struct spacc_ddt {
  75. dma_addr_t p;
  76. u32 len;
  77. };
  78. /*
  79. * Asynchronous crypto request structure.
  80. *
  81. * This structure defines a request that is either queued for processing or
  82. * being processed.
  83. */
  84. struct spacc_req {
  85. struct list_head list;
  86. struct spacc_engine *engine;
  87. struct crypto_async_request *req;
  88. int result;
  89. bool is_encrypt;
  90. unsigned ctx_id;
  91. dma_addr_t src_addr, dst_addr;
  92. struct spacc_ddt *src_ddt, *dst_ddt;
  93. void (*complete)(struct spacc_req *req);
  94. };
  95. struct spacc_aead {
  96. unsigned long ctrl_default;
  97. unsigned long type;
  98. struct aead_alg alg;
  99. struct spacc_engine *engine;
  100. struct list_head entry;
  101. int key_offs;
  102. int iv_offs;
  103. };
  104. struct spacc_engine {
  105. void __iomem *regs;
  106. struct list_head pending;
  107. int next_ctx;
  108. spinlock_t hw_lock;
  109. int in_flight;
  110. struct list_head completed;
  111. struct list_head in_progress;
  112. struct tasklet_struct complete;
  113. unsigned long fifo_sz;
  114. void __iomem *cipher_ctx_base;
  115. void __iomem *hash_key_base;
  116. struct spacc_alg *algs;
  117. unsigned num_algs;
  118. struct list_head registered_algs;
  119. struct spacc_aead *aeads;
  120. unsigned num_aeads;
  121. struct list_head registered_aeads;
  122. size_t cipher_pg_sz;
  123. size_t hash_pg_sz;
  124. const char *name;
  125. struct clk *clk;
  126. struct device *dev;
  127. unsigned max_ctxs;
  128. struct timer_list packet_timeout;
  129. unsigned stat_irq_thresh;
  130. struct dma_pool *req_pool;
  131. };
  132. /* Algorithm type mask. */
  133. #define SPACC_CRYPTO_ALG_MASK 0x7
  134. /* SPACC definition of a crypto algorithm. */
  135. struct spacc_alg {
  136. unsigned long ctrl_default;
  137. unsigned long type;
  138. struct crypto_alg alg;
  139. struct spacc_engine *engine;
  140. struct list_head entry;
  141. int key_offs;
  142. int iv_offs;
  143. };
  144. /* Generic context structure for any algorithm type. */
  145. struct spacc_generic_ctx {
  146. struct spacc_engine *engine;
  147. int flags;
  148. int key_offs;
  149. int iv_offs;
  150. };
  151. /* Block cipher context. */
  152. struct spacc_ablk_ctx {
  153. struct spacc_generic_ctx generic;
  154. u8 key[AES_MAX_KEY_SIZE];
  155. u8 key_len;
  156. /*
  157. * The fallback cipher. If the operation can't be done in hardware,
  158. * fallback to a software version.
  159. */
  160. struct crypto_skcipher *sw_cipher;
  161. };
  162. /* AEAD cipher context. */
  163. struct spacc_aead_ctx {
  164. struct spacc_generic_ctx generic;
  165. u8 cipher_key[AES_MAX_KEY_SIZE];
  166. u8 hash_ctx[SPACC_CRYPTO_IPSEC_HASH_PG_SZ];
  167. u8 cipher_key_len;
  168. u8 hash_key_len;
  169. struct crypto_aead *sw_cipher;
  170. };
  171. static int spacc_ablk_submit(struct spacc_req *req);
  172. static inline struct spacc_alg *to_spacc_alg(struct crypto_alg *alg)
  173. {
  174. return alg ? container_of(alg, struct spacc_alg, alg) : NULL;
  175. }
  176. static inline struct spacc_aead *to_spacc_aead(struct aead_alg *alg)
  177. {
  178. return container_of(alg, struct spacc_aead, alg);
  179. }
  180. static inline int spacc_fifo_cmd_full(struct spacc_engine *engine)
  181. {
  182. u32 fifo_stat = readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET);
  183. return fifo_stat & SPA_FIFO_CMD_FULL;
  184. }
  185. /*
  186. * Given a cipher context, and a context number, get the base address of the
  187. * context page.
  188. *
  189. * Returns the address of the context page where the key/context may
  190. * be written.
  191. */
  192. static inline void __iomem *spacc_ctx_page_addr(struct spacc_generic_ctx *ctx,
  193. unsigned indx,
  194. bool is_cipher_ctx)
  195. {
  196. return is_cipher_ctx ? ctx->engine->cipher_ctx_base +
  197. (indx * ctx->engine->cipher_pg_sz) :
  198. ctx->engine->hash_key_base + (indx * ctx->engine->hash_pg_sz);
  199. }
  200. /* The context pages can only be written with 32-bit accesses. */
  201. static inline void memcpy_toio32(u32 __iomem *dst, const void *src,
  202. unsigned count)
  203. {
  204. const u32 *src32 = (const u32 *) src;
  205. while (count--)
  206. writel(*src32++, dst++);
  207. }
  208. static void spacc_cipher_write_ctx(struct spacc_generic_ctx *ctx,
  209. void __iomem *page_addr, const u8 *key,
  210. size_t key_len, const u8 *iv, size_t iv_len)
  211. {
  212. void __iomem *key_ptr = page_addr + ctx->key_offs;
  213. void __iomem *iv_ptr = page_addr + ctx->iv_offs;
  214. memcpy_toio32(key_ptr, key, key_len / 4);
  215. memcpy_toio32(iv_ptr, iv, iv_len / 4);
  216. }
  217. /*
  218. * Load a context into the engines context memory.
  219. *
  220. * Returns the index of the context page where the context was loaded.
  221. */
  222. static unsigned spacc_load_ctx(struct spacc_generic_ctx *ctx,
  223. const u8 *ciph_key, size_t ciph_len,
  224. const u8 *iv, size_t ivlen, const u8 *hash_key,
  225. size_t hash_len)
  226. {
  227. unsigned indx = ctx->engine->next_ctx++;
  228. void __iomem *ciph_page_addr, *hash_page_addr;
  229. ciph_page_addr = spacc_ctx_page_addr(ctx, indx, 1);
  230. hash_page_addr = spacc_ctx_page_addr(ctx, indx, 0);
  231. ctx->engine->next_ctx &= ctx->engine->fifo_sz - 1;
  232. spacc_cipher_write_ctx(ctx, ciph_page_addr, ciph_key, ciph_len, iv,
  233. ivlen);
  234. writel(ciph_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET) |
  235. (1 << SPA_KEY_SZ_CIPHER_OFFSET),
  236. ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET);
  237. if (hash_key) {
  238. memcpy_toio32(hash_page_addr, hash_key, hash_len / 4);
  239. writel(hash_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET),
  240. ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET);
  241. }
  242. return indx;
  243. }
  244. static inline void ddt_set(struct spacc_ddt *ddt, dma_addr_t phys, size_t len)
  245. {
  246. ddt->p = phys;
  247. ddt->len = len;
  248. }
  249. /*
  250. * Take a crypto request and scatterlists for the data and turn them into DDTs
  251. * for passing to the crypto engines. This also DMA maps the data so that the
  252. * crypto engines can DMA to/from them.
  253. */
  254. static struct spacc_ddt *spacc_sg_to_ddt(struct spacc_engine *engine,
  255. struct scatterlist *payload,
  256. unsigned nbytes,
  257. enum dma_data_direction dir,
  258. dma_addr_t *ddt_phys)
  259. {
  260. unsigned mapped_ents;
  261. struct scatterlist *cur;
  262. struct spacc_ddt *ddt;
  263. int i;
  264. int nents;
  265. nents = sg_nents_for_len(payload, nbytes);
  266. if (nents < 0) {
  267. dev_err(engine->dev, "Invalid numbers of SG.\n");
  268. return NULL;
  269. }
  270. mapped_ents = dma_map_sg(engine->dev, payload, nents, dir);
  271. if (mapped_ents + 1 > MAX_DDT_LEN)
  272. goto out;
  273. ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, ddt_phys);
  274. if (!ddt)
  275. goto out;
  276. for_each_sg(payload, cur, mapped_ents, i)
  277. ddt_set(&ddt[i], sg_dma_address(cur), sg_dma_len(cur));
  278. ddt_set(&ddt[mapped_ents], 0, 0);
  279. return ddt;
  280. out:
  281. dma_unmap_sg(engine->dev, payload, nents, dir);
  282. return NULL;
  283. }
  284. static int spacc_aead_make_ddts(struct aead_request *areq)
  285. {
  286. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  287. struct spacc_req *req = aead_request_ctx(areq);
  288. struct spacc_engine *engine = req->engine;
  289. struct spacc_ddt *src_ddt, *dst_ddt;
  290. unsigned total;
  291. int src_nents, dst_nents;
  292. struct scatterlist *cur;
  293. int i, dst_ents, src_ents;
  294. total = areq->assoclen + areq->cryptlen;
  295. if (req->is_encrypt)
  296. total += crypto_aead_authsize(aead);
  297. src_nents = sg_nents_for_len(areq->src, total);
  298. if (src_nents < 0) {
  299. dev_err(engine->dev, "Invalid numbers of src SG.\n");
  300. return src_nents;
  301. }
  302. if (src_nents + 1 > MAX_DDT_LEN)
  303. return -E2BIG;
  304. dst_nents = 0;
  305. if (areq->src != areq->dst) {
  306. dst_nents = sg_nents_for_len(areq->dst, total);
  307. if (dst_nents < 0) {
  308. dev_err(engine->dev, "Invalid numbers of dst SG.\n");
  309. return dst_nents;
  310. }
  311. if (src_nents + 1 > MAX_DDT_LEN)
  312. return -E2BIG;
  313. }
  314. src_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->src_addr);
  315. if (!src_ddt)
  316. goto err;
  317. dst_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->dst_addr);
  318. if (!dst_ddt)
  319. goto err_free_src;
  320. req->src_ddt = src_ddt;
  321. req->dst_ddt = dst_ddt;
  322. if (dst_nents) {
  323. src_ents = dma_map_sg(engine->dev, areq->src, src_nents,
  324. DMA_TO_DEVICE);
  325. if (!src_ents)
  326. goto err_free_dst;
  327. dst_ents = dma_map_sg(engine->dev, areq->dst, dst_nents,
  328. DMA_FROM_DEVICE);
  329. if (!dst_ents) {
  330. dma_unmap_sg(engine->dev, areq->src, src_nents,
  331. DMA_TO_DEVICE);
  332. goto err_free_dst;
  333. }
  334. } else {
  335. src_ents = dma_map_sg(engine->dev, areq->src, src_nents,
  336. DMA_BIDIRECTIONAL);
  337. if (!src_ents)
  338. goto err_free_dst;
  339. dst_ents = src_ents;
  340. }
  341. /*
  342. * Now map in the payload for the source and destination and terminate
  343. * with the NULL pointers.
  344. */
  345. for_each_sg(areq->src, cur, src_ents, i)
  346. ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur));
  347. /* For decryption we need to skip the associated data. */
  348. total = req->is_encrypt ? 0 : areq->assoclen;
  349. for_each_sg(areq->dst, cur, dst_ents, i) {
  350. unsigned len = sg_dma_len(cur);
  351. if (len <= total) {
  352. total -= len;
  353. continue;
  354. }
  355. ddt_set(dst_ddt++, sg_dma_address(cur) + total, len - total);
  356. }
  357. ddt_set(src_ddt, 0, 0);
  358. ddt_set(dst_ddt, 0, 0);
  359. return 0;
  360. err_free_dst:
  361. dma_pool_free(engine->req_pool, dst_ddt, req->dst_addr);
  362. err_free_src:
  363. dma_pool_free(engine->req_pool, src_ddt, req->src_addr);
  364. err:
  365. return -ENOMEM;
  366. }
  367. static void spacc_aead_free_ddts(struct spacc_req *req)
  368. {
  369. struct aead_request *areq = container_of(req->req, struct aead_request,
  370. base);
  371. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  372. unsigned total = areq->assoclen + areq->cryptlen +
  373. (req->is_encrypt ? crypto_aead_authsize(aead) : 0);
  374. struct spacc_aead_ctx *aead_ctx = crypto_aead_ctx(aead);
  375. struct spacc_engine *engine = aead_ctx->generic.engine;
  376. int nents = sg_nents_for_len(areq->src, total);
  377. /* sg_nents_for_len should not fail since it works when mapping sg */
  378. if (unlikely(nents < 0)) {
  379. dev_err(engine->dev, "Invalid numbers of src SG.\n");
  380. return;
  381. }
  382. if (areq->src != areq->dst) {
  383. dma_unmap_sg(engine->dev, areq->src, nents, DMA_TO_DEVICE);
  384. nents = sg_nents_for_len(areq->dst, total);
  385. if (unlikely(nents < 0)) {
  386. dev_err(engine->dev, "Invalid numbers of dst SG.\n");
  387. return;
  388. }
  389. dma_unmap_sg(engine->dev, areq->dst, nents, DMA_FROM_DEVICE);
  390. } else
  391. dma_unmap_sg(engine->dev, areq->src, nents, DMA_BIDIRECTIONAL);
  392. dma_pool_free(engine->req_pool, req->src_ddt, req->src_addr);
  393. dma_pool_free(engine->req_pool, req->dst_ddt, req->dst_addr);
  394. }
  395. static void spacc_free_ddt(struct spacc_req *req, struct spacc_ddt *ddt,
  396. dma_addr_t ddt_addr, struct scatterlist *payload,
  397. unsigned nbytes, enum dma_data_direction dir)
  398. {
  399. int nents = sg_nents_for_len(payload, nbytes);
  400. if (nents < 0) {
  401. dev_err(req->engine->dev, "Invalid numbers of SG.\n");
  402. return;
  403. }
  404. dma_unmap_sg(req->engine->dev, payload, nents, dir);
  405. dma_pool_free(req->engine->req_pool, ddt, ddt_addr);
  406. }
  407. static int spacc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  408. unsigned int keylen)
  409. {
  410. struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  411. struct crypto_authenc_keys keys;
  412. int err;
  413. crypto_aead_clear_flags(ctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
  414. crypto_aead_set_flags(ctx->sw_cipher, crypto_aead_get_flags(tfm) &
  415. CRYPTO_TFM_REQ_MASK);
  416. err = crypto_aead_setkey(ctx->sw_cipher, key, keylen);
  417. crypto_aead_clear_flags(tfm, CRYPTO_TFM_RES_MASK);
  418. crypto_aead_set_flags(tfm, crypto_aead_get_flags(ctx->sw_cipher) &
  419. CRYPTO_TFM_RES_MASK);
  420. if (err)
  421. return err;
  422. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  423. goto badkey;
  424. if (keys.enckeylen > AES_MAX_KEY_SIZE)
  425. goto badkey;
  426. if (keys.authkeylen > sizeof(ctx->hash_ctx))
  427. goto badkey;
  428. memcpy(ctx->cipher_key, keys.enckey, keys.enckeylen);
  429. ctx->cipher_key_len = keys.enckeylen;
  430. memcpy(ctx->hash_ctx, keys.authkey, keys.authkeylen);
  431. ctx->hash_key_len = keys.authkeylen;
  432. memzero_explicit(&keys, sizeof(keys));
  433. return 0;
  434. badkey:
  435. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  436. memzero_explicit(&keys, sizeof(keys));
  437. return -EINVAL;
  438. }
  439. static int spacc_aead_setauthsize(struct crypto_aead *tfm,
  440. unsigned int authsize)
  441. {
  442. struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm));
  443. return crypto_aead_setauthsize(ctx->sw_cipher, authsize);
  444. }
  445. /*
  446. * Check if an AEAD request requires a fallback operation. Some requests can't
  447. * be completed in hardware because the hardware may not support certain key
  448. * sizes. In these cases we need to complete the request in software.
  449. */
  450. static int spacc_aead_need_fallback(struct aead_request *aead_req)
  451. {
  452. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  453. struct aead_alg *alg = crypto_aead_alg(aead);
  454. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  455. struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
  456. /*
  457. * If we have a non-supported key-length, then we need to do a
  458. * software fallback.
  459. */
  460. if ((spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
  461. SPA_CTRL_CIPH_ALG_AES &&
  462. ctx->cipher_key_len != AES_KEYSIZE_128 &&
  463. ctx->cipher_key_len != AES_KEYSIZE_256)
  464. return 1;
  465. return 0;
  466. }
  467. static int spacc_aead_do_fallback(struct aead_request *req, unsigned alg_type,
  468. bool is_encrypt)
  469. {
  470. struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req));
  471. struct spacc_aead_ctx *ctx = crypto_tfm_ctx(old_tfm);
  472. struct aead_request *subreq = aead_request_ctx(req);
  473. aead_request_set_tfm(subreq, ctx->sw_cipher);
  474. aead_request_set_callback(subreq, req->base.flags,
  475. req->base.complete, req->base.data);
  476. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  477. req->iv);
  478. aead_request_set_ad(subreq, req->assoclen);
  479. return is_encrypt ? crypto_aead_encrypt(subreq) :
  480. crypto_aead_decrypt(subreq);
  481. }
  482. static void spacc_aead_complete(struct spacc_req *req)
  483. {
  484. spacc_aead_free_ddts(req);
  485. req->req->complete(req->req, req->result);
  486. }
  487. static int spacc_aead_submit(struct spacc_req *req)
  488. {
  489. struct aead_request *aead_req =
  490. container_of(req->req, struct aead_request, base);
  491. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  492. unsigned int authsize = crypto_aead_authsize(aead);
  493. struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
  494. struct aead_alg *alg = crypto_aead_alg(aead);
  495. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  496. struct spacc_engine *engine = ctx->generic.engine;
  497. u32 ctrl, proc_len, assoc_len;
  498. req->result = -EINPROGRESS;
  499. req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->cipher_key,
  500. ctx->cipher_key_len, aead_req->iv, crypto_aead_ivsize(aead),
  501. ctx->hash_ctx, ctx->hash_key_len);
  502. /* Set the source and destination DDT pointers. */
  503. writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
  504. writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
  505. writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
  506. assoc_len = aead_req->assoclen;
  507. proc_len = aead_req->cryptlen + assoc_len;
  508. /*
  509. * If we are decrypting, we need to take the length of the ICV out of
  510. * the processing length.
  511. */
  512. if (!req->is_encrypt)
  513. proc_len -= authsize;
  514. writel(proc_len, engine->regs + SPA_PROC_LEN_REG_OFFSET);
  515. writel(assoc_len, engine->regs + SPA_AAD_LEN_REG_OFFSET);
  516. writel(authsize, engine->regs + SPA_ICV_LEN_REG_OFFSET);
  517. writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
  518. writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
  519. ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
  520. (1 << SPA_CTRL_ICV_APPEND);
  521. if (req->is_encrypt)
  522. ctrl |= (1 << SPA_CTRL_ENCRYPT_IDX) | (1 << SPA_CTRL_AAD_COPY);
  523. else
  524. ctrl |= (1 << SPA_CTRL_KEY_EXP);
  525. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  526. writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
  527. return -EINPROGRESS;
  528. }
  529. static int spacc_req_submit(struct spacc_req *req);
  530. static void spacc_push(struct spacc_engine *engine)
  531. {
  532. struct spacc_req *req;
  533. while (!list_empty(&engine->pending) &&
  534. engine->in_flight + 1 <= engine->fifo_sz) {
  535. ++engine->in_flight;
  536. req = list_first_entry(&engine->pending, struct spacc_req,
  537. list);
  538. list_move_tail(&req->list, &engine->in_progress);
  539. req->result = spacc_req_submit(req);
  540. }
  541. }
  542. /*
  543. * Setup an AEAD request for processing. This will configure the engine, load
  544. * the context and then start the packet processing.
  545. */
  546. static int spacc_aead_setup(struct aead_request *req,
  547. unsigned alg_type, bool is_encrypt)
  548. {
  549. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  550. struct aead_alg *alg = crypto_aead_alg(aead);
  551. struct spacc_engine *engine = to_spacc_aead(alg)->engine;
  552. struct spacc_req *dev_req = aead_request_ctx(req);
  553. int err;
  554. unsigned long flags;
  555. dev_req->req = &req->base;
  556. dev_req->is_encrypt = is_encrypt;
  557. dev_req->result = -EBUSY;
  558. dev_req->engine = engine;
  559. dev_req->complete = spacc_aead_complete;
  560. if (unlikely(spacc_aead_need_fallback(req) ||
  561. ((err = spacc_aead_make_ddts(req)) == -E2BIG)))
  562. return spacc_aead_do_fallback(req, alg_type, is_encrypt);
  563. if (err)
  564. goto out;
  565. err = -EINPROGRESS;
  566. spin_lock_irqsave(&engine->hw_lock, flags);
  567. if (unlikely(spacc_fifo_cmd_full(engine)) ||
  568. engine->in_flight + 1 > engine->fifo_sz) {
  569. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  570. err = -EBUSY;
  571. spin_unlock_irqrestore(&engine->hw_lock, flags);
  572. goto out_free_ddts;
  573. }
  574. list_add_tail(&dev_req->list, &engine->pending);
  575. } else {
  576. list_add_tail(&dev_req->list, &engine->pending);
  577. spacc_push(engine);
  578. }
  579. spin_unlock_irqrestore(&engine->hw_lock, flags);
  580. goto out;
  581. out_free_ddts:
  582. spacc_aead_free_ddts(dev_req);
  583. out:
  584. return err;
  585. }
  586. static int spacc_aead_encrypt(struct aead_request *req)
  587. {
  588. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  589. struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
  590. return spacc_aead_setup(req, alg->type, 1);
  591. }
  592. static int spacc_aead_decrypt(struct aead_request *req)
  593. {
  594. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  595. struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
  596. return spacc_aead_setup(req, alg->type, 0);
  597. }
  598. /*
  599. * Initialise a new AEAD context. This is responsible for allocating the
  600. * fallback cipher and initialising the context.
  601. */
  602. static int spacc_aead_cra_init(struct crypto_aead *tfm)
  603. {
  604. struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  605. struct aead_alg *alg = crypto_aead_alg(tfm);
  606. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  607. struct spacc_engine *engine = spacc_alg->engine;
  608. ctx->generic.flags = spacc_alg->type;
  609. ctx->generic.engine = engine;
  610. ctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
  611. CRYPTO_ALG_NEED_FALLBACK);
  612. if (IS_ERR(ctx->sw_cipher))
  613. return PTR_ERR(ctx->sw_cipher);
  614. ctx->generic.key_offs = spacc_alg->key_offs;
  615. ctx->generic.iv_offs = spacc_alg->iv_offs;
  616. crypto_aead_set_reqsize(
  617. tfm,
  618. max(sizeof(struct spacc_req),
  619. sizeof(struct aead_request) +
  620. crypto_aead_reqsize(ctx->sw_cipher)));
  621. return 0;
  622. }
  623. /*
  624. * Destructor for an AEAD context. This is called when the transform is freed
  625. * and must free the fallback cipher.
  626. */
  627. static void spacc_aead_cra_exit(struct crypto_aead *tfm)
  628. {
  629. struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  630. crypto_free_aead(ctx->sw_cipher);
  631. }
  632. /*
  633. * Set the DES key for a block cipher transform. This also performs weak key
  634. * checking if the transform has requested it.
  635. */
  636. static int spacc_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  637. unsigned int len)
  638. {
  639. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  640. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  641. u32 tmp[DES_EXPKEY_WORDS];
  642. if (len > DES3_EDE_KEY_SIZE) {
  643. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  644. return -EINVAL;
  645. }
  646. if (unlikely(!des_ekey(tmp, key)) &&
  647. (crypto_ablkcipher_get_flags(cipher) & CRYPTO_TFM_REQ_WEAK_KEY)) {
  648. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  649. return -EINVAL;
  650. }
  651. memcpy(ctx->key, key, len);
  652. ctx->key_len = len;
  653. return 0;
  654. }
  655. /*
  656. * Set the key for an AES block cipher. Some key lengths are not supported in
  657. * hardware so this must also check whether a fallback is needed.
  658. */
  659. static int spacc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  660. unsigned int len)
  661. {
  662. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  663. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  664. int err = 0;
  665. if (len > AES_MAX_KEY_SIZE) {
  666. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  667. return -EINVAL;
  668. }
  669. /*
  670. * IPSec engine only supports 128 and 256 bit AES keys. If we get a
  671. * request for any other size (192 bits) then we need to do a software
  672. * fallback.
  673. */
  674. if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256) {
  675. if (!ctx->sw_cipher)
  676. return -EINVAL;
  677. /*
  678. * Set the fallback transform to use the same request flags as
  679. * the hardware transform.
  680. */
  681. crypto_skcipher_clear_flags(ctx->sw_cipher,
  682. CRYPTO_TFM_REQ_MASK);
  683. crypto_skcipher_set_flags(ctx->sw_cipher,
  684. cipher->base.crt_flags &
  685. CRYPTO_TFM_REQ_MASK);
  686. err = crypto_skcipher_setkey(ctx->sw_cipher, key, len);
  687. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  688. tfm->crt_flags |=
  689. crypto_skcipher_get_flags(ctx->sw_cipher) &
  690. CRYPTO_TFM_RES_MASK;
  691. if (err)
  692. goto sw_setkey_failed;
  693. }
  694. memcpy(ctx->key, key, len);
  695. ctx->key_len = len;
  696. sw_setkey_failed:
  697. return err;
  698. }
  699. static int spacc_kasumi_f8_setkey(struct crypto_ablkcipher *cipher,
  700. const u8 *key, unsigned int len)
  701. {
  702. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  703. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  704. int err = 0;
  705. if (len > AES_MAX_KEY_SIZE) {
  706. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  707. err = -EINVAL;
  708. goto out;
  709. }
  710. memcpy(ctx->key, key, len);
  711. ctx->key_len = len;
  712. out:
  713. return err;
  714. }
  715. static int spacc_ablk_need_fallback(struct spacc_req *req)
  716. {
  717. struct spacc_ablk_ctx *ctx;
  718. struct crypto_tfm *tfm = req->req->tfm;
  719. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  720. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  721. ctx = crypto_tfm_ctx(tfm);
  722. return (spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
  723. SPA_CTRL_CIPH_ALG_AES &&
  724. ctx->key_len != AES_KEYSIZE_128 &&
  725. ctx->key_len != AES_KEYSIZE_256;
  726. }
  727. static void spacc_ablk_complete(struct spacc_req *req)
  728. {
  729. struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req);
  730. if (ablk_req->src != ablk_req->dst) {
  731. spacc_free_ddt(req, req->src_ddt, req->src_addr, ablk_req->src,
  732. ablk_req->nbytes, DMA_TO_DEVICE);
  733. spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
  734. ablk_req->nbytes, DMA_FROM_DEVICE);
  735. } else
  736. spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
  737. ablk_req->nbytes, DMA_BIDIRECTIONAL);
  738. req->req->complete(req->req, req->result);
  739. }
  740. static int spacc_ablk_submit(struct spacc_req *req)
  741. {
  742. struct crypto_tfm *tfm = req->req->tfm;
  743. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  744. struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req);
  745. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  746. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  747. struct spacc_engine *engine = ctx->generic.engine;
  748. u32 ctrl;
  749. req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->key,
  750. ctx->key_len, ablk_req->info, alg->cra_ablkcipher.ivsize,
  751. NULL, 0);
  752. writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
  753. writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
  754. writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
  755. writel(ablk_req->nbytes, engine->regs + SPA_PROC_LEN_REG_OFFSET);
  756. writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
  757. writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
  758. writel(0, engine->regs + SPA_AAD_LEN_REG_OFFSET);
  759. ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
  760. (req->is_encrypt ? (1 << SPA_CTRL_ENCRYPT_IDX) :
  761. (1 << SPA_CTRL_KEY_EXP));
  762. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  763. writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
  764. return -EINPROGRESS;
  765. }
  766. static int spacc_ablk_do_fallback(struct ablkcipher_request *req,
  767. unsigned alg_type, bool is_encrypt)
  768. {
  769. struct crypto_tfm *old_tfm =
  770. crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
  771. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(old_tfm);
  772. SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher);
  773. int err;
  774. /*
  775. * Change the request to use the software fallback transform, and once
  776. * the ciphering has completed, put the old transform back into the
  777. * request.
  778. */
  779. skcipher_request_set_tfm(subreq, ctx->sw_cipher);
  780. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  781. skcipher_request_set_crypt(subreq, req->src, req->dst,
  782. req->nbytes, req->info);
  783. err = is_encrypt ? crypto_skcipher_encrypt(subreq) :
  784. crypto_skcipher_decrypt(subreq);
  785. skcipher_request_zero(subreq);
  786. return err;
  787. }
  788. static int spacc_ablk_setup(struct ablkcipher_request *req, unsigned alg_type,
  789. bool is_encrypt)
  790. {
  791. struct crypto_alg *alg = req->base.tfm->__crt_alg;
  792. struct spacc_engine *engine = to_spacc_alg(alg)->engine;
  793. struct spacc_req *dev_req = ablkcipher_request_ctx(req);
  794. unsigned long flags;
  795. int err = -ENOMEM;
  796. dev_req->req = &req->base;
  797. dev_req->is_encrypt = is_encrypt;
  798. dev_req->engine = engine;
  799. dev_req->complete = spacc_ablk_complete;
  800. dev_req->result = -EINPROGRESS;
  801. if (unlikely(spacc_ablk_need_fallback(dev_req)))
  802. return spacc_ablk_do_fallback(req, alg_type, is_encrypt);
  803. /*
  804. * Create the DDT's for the engine. If we share the same source and
  805. * destination then we can optimize by reusing the DDT's.
  806. */
  807. if (req->src != req->dst) {
  808. dev_req->src_ddt = spacc_sg_to_ddt(engine, req->src,
  809. req->nbytes, DMA_TO_DEVICE, &dev_req->src_addr);
  810. if (!dev_req->src_ddt)
  811. goto out;
  812. dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
  813. req->nbytes, DMA_FROM_DEVICE, &dev_req->dst_addr);
  814. if (!dev_req->dst_ddt)
  815. goto out_free_src;
  816. } else {
  817. dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
  818. req->nbytes, DMA_BIDIRECTIONAL, &dev_req->dst_addr);
  819. if (!dev_req->dst_ddt)
  820. goto out;
  821. dev_req->src_ddt = NULL;
  822. dev_req->src_addr = dev_req->dst_addr;
  823. }
  824. err = -EINPROGRESS;
  825. spin_lock_irqsave(&engine->hw_lock, flags);
  826. /*
  827. * Check if the engine will accept the operation now. If it won't then
  828. * we either stick it on the end of a pending list if we can backlog,
  829. * or bailout with an error if not.
  830. */
  831. if (unlikely(spacc_fifo_cmd_full(engine)) ||
  832. engine->in_flight + 1 > engine->fifo_sz) {
  833. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  834. err = -EBUSY;
  835. spin_unlock_irqrestore(&engine->hw_lock, flags);
  836. goto out_free_ddts;
  837. }
  838. list_add_tail(&dev_req->list, &engine->pending);
  839. } else {
  840. list_add_tail(&dev_req->list, &engine->pending);
  841. spacc_push(engine);
  842. }
  843. spin_unlock_irqrestore(&engine->hw_lock, flags);
  844. goto out;
  845. out_free_ddts:
  846. spacc_free_ddt(dev_req, dev_req->dst_ddt, dev_req->dst_addr, req->dst,
  847. req->nbytes, req->src == req->dst ?
  848. DMA_BIDIRECTIONAL : DMA_FROM_DEVICE);
  849. out_free_src:
  850. if (req->src != req->dst)
  851. spacc_free_ddt(dev_req, dev_req->src_ddt, dev_req->src_addr,
  852. req->src, req->nbytes, DMA_TO_DEVICE);
  853. out:
  854. return err;
  855. }
  856. static int spacc_ablk_cra_init(struct crypto_tfm *tfm)
  857. {
  858. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  859. struct crypto_alg *alg = tfm->__crt_alg;
  860. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  861. struct spacc_engine *engine = spacc_alg->engine;
  862. ctx->generic.flags = spacc_alg->type;
  863. ctx->generic.engine = engine;
  864. if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
  865. ctx->sw_cipher = crypto_alloc_skcipher(
  866. alg->cra_name, 0, CRYPTO_ALG_ASYNC |
  867. CRYPTO_ALG_NEED_FALLBACK);
  868. if (IS_ERR(ctx->sw_cipher)) {
  869. dev_warn(engine->dev, "failed to allocate fallback for %s\n",
  870. alg->cra_name);
  871. return PTR_ERR(ctx->sw_cipher);
  872. }
  873. }
  874. ctx->generic.key_offs = spacc_alg->key_offs;
  875. ctx->generic.iv_offs = spacc_alg->iv_offs;
  876. tfm->crt_ablkcipher.reqsize = sizeof(struct spacc_req);
  877. return 0;
  878. }
  879. static void spacc_ablk_cra_exit(struct crypto_tfm *tfm)
  880. {
  881. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  882. crypto_free_skcipher(ctx->sw_cipher);
  883. }
  884. static int spacc_ablk_encrypt(struct ablkcipher_request *req)
  885. {
  886. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  887. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  888. struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
  889. return spacc_ablk_setup(req, alg->type, 1);
  890. }
  891. static int spacc_ablk_decrypt(struct ablkcipher_request *req)
  892. {
  893. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  894. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  895. struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
  896. return spacc_ablk_setup(req, alg->type, 0);
  897. }
  898. static inline int spacc_fifo_stat_empty(struct spacc_engine *engine)
  899. {
  900. return readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET) &
  901. SPA_FIFO_STAT_EMPTY;
  902. }
  903. static void spacc_process_done(struct spacc_engine *engine)
  904. {
  905. struct spacc_req *req;
  906. unsigned long flags;
  907. spin_lock_irqsave(&engine->hw_lock, flags);
  908. while (!spacc_fifo_stat_empty(engine)) {
  909. req = list_first_entry(&engine->in_progress, struct spacc_req,
  910. list);
  911. list_move_tail(&req->list, &engine->completed);
  912. --engine->in_flight;
  913. /* POP the status register. */
  914. writel(~0, engine->regs + SPA_STAT_POP_REG_OFFSET);
  915. req->result = (readl(engine->regs + SPA_STATUS_REG_OFFSET) &
  916. SPA_STATUS_RES_CODE_MASK) >> SPA_STATUS_RES_CODE_OFFSET;
  917. /*
  918. * Convert the SPAcc error status into the standard POSIX error
  919. * codes.
  920. */
  921. if (unlikely(req->result)) {
  922. switch (req->result) {
  923. case SPA_STATUS_ICV_FAIL:
  924. req->result = -EBADMSG;
  925. break;
  926. case SPA_STATUS_MEMORY_ERROR:
  927. dev_warn(engine->dev,
  928. "memory error triggered\n");
  929. req->result = -EFAULT;
  930. break;
  931. case SPA_STATUS_BLOCK_ERROR:
  932. dev_warn(engine->dev,
  933. "block error triggered\n");
  934. req->result = -EIO;
  935. break;
  936. }
  937. }
  938. }
  939. tasklet_schedule(&engine->complete);
  940. spin_unlock_irqrestore(&engine->hw_lock, flags);
  941. }
  942. static irqreturn_t spacc_spacc_irq(int irq, void *dev)
  943. {
  944. struct spacc_engine *engine = (struct spacc_engine *)dev;
  945. u32 spacc_irq_stat = readl(engine->regs + SPA_IRQ_STAT_REG_OFFSET);
  946. writel(spacc_irq_stat, engine->regs + SPA_IRQ_STAT_REG_OFFSET);
  947. spacc_process_done(engine);
  948. return IRQ_HANDLED;
  949. }
  950. static void spacc_packet_timeout(struct timer_list *t)
  951. {
  952. struct spacc_engine *engine = from_timer(engine, t, packet_timeout);
  953. spacc_process_done(engine);
  954. }
  955. static int spacc_req_submit(struct spacc_req *req)
  956. {
  957. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  958. if (CRYPTO_ALG_TYPE_AEAD == (CRYPTO_ALG_TYPE_MASK & alg->cra_flags))
  959. return spacc_aead_submit(req);
  960. else
  961. return spacc_ablk_submit(req);
  962. }
  963. static void spacc_spacc_complete(unsigned long data)
  964. {
  965. struct spacc_engine *engine = (struct spacc_engine *)data;
  966. struct spacc_req *req, *tmp;
  967. unsigned long flags;
  968. LIST_HEAD(completed);
  969. spin_lock_irqsave(&engine->hw_lock, flags);
  970. list_splice_init(&engine->completed, &completed);
  971. spacc_push(engine);
  972. if (engine->in_flight)
  973. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  974. spin_unlock_irqrestore(&engine->hw_lock, flags);
  975. list_for_each_entry_safe(req, tmp, &completed, list) {
  976. list_del(&req->list);
  977. req->complete(req);
  978. }
  979. }
  980. #ifdef CONFIG_PM
  981. static int spacc_suspend(struct device *dev)
  982. {
  983. struct platform_device *pdev = to_platform_device(dev);
  984. struct spacc_engine *engine = platform_get_drvdata(pdev);
  985. /*
  986. * We only support standby mode. All we have to do is gate the clock to
  987. * the spacc. The hardware will preserve state until we turn it back
  988. * on again.
  989. */
  990. clk_disable(engine->clk);
  991. return 0;
  992. }
  993. static int spacc_resume(struct device *dev)
  994. {
  995. struct platform_device *pdev = to_platform_device(dev);
  996. struct spacc_engine *engine = platform_get_drvdata(pdev);
  997. return clk_enable(engine->clk);
  998. }
  999. static const struct dev_pm_ops spacc_pm_ops = {
  1000. .suspend = spacc_suspend,
  1001. .resume = spacc_resume,
  1002. };
  1003. #endif /* CONFIG_PM */
  1004. static inline struct spacc_engine *spacc_dev_to_engine(struct device *dev)
  1005. {
  1006. return dev ? platform_get_drvdata(to_platform_device(dev)) : NULL;
  1007. }
  1008. static ssize_t spacc_stat_irq_thresh_show(struct device *dev,
  1009. struct device_attribute *attr,
  1010. char *buf)
  1011. {
  1012. struct spacc_engine *engine = spacc_dev_to_engine(dev);
  1013. return snprintf(buf, PAGE_SIZE, "%u\n", engine->stat_irq_thresh);
  1014. }
  1015. static ssize_t spacc_stat_irq_thresh_store(struct device *dev,
  1016. struct device_attribute *attr,
  1017. const char *buf, size_t len)
  1018. {
  1019. struct spacc_engine *engine = spacc_dev_to_engine(dev);
  1020. unsigned long thresh;
  1021. if (kstrtoul(buf, 0, &thresh))
  1022. return -EINVAL;
  1023. thresh = clamp(thresh, 1UL, engine->fifo_sz - 1);
  1024. engine->stat_irq_thresh = thresh;
  1025. writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
  1026. engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
  1027. return len;
  1028. }
  1029. static DEVICE_ATTR(stat_irq_thresh, 0644, spacc_stat_irq_thresh_show,
  1030. spacc_stat_irq_thresh_store);
  1031. static struct spacc_alg ipsec_engine_algs[] = {
  1032. {
  1033. .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC,
  1034. .key_offs = 0,
  1035. .iv_offs = AES_MAX_KEY_SIZE,
  1036. .alg = {
  1037. .cra_name = "cbc(aes)",
  1038. .cra_driver_name = "cbc-aes-picoxcell",
  1039. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1040. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1041. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1042. CRYPTO_ALG_ASYNC |
  1043. CRYPTO_ALG_NEED_FALLBACK,
  1044. .cra_blocksize = AES_BLOCK_SIZE,
  1045. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1046. .cra_type = &crypto_ablkcipher_type,
  1047. .cra_module = THIS_MODULE,
  1048. .cra_ablkcipher = {
  1049. .setkey = spacc_aes_setkey,
  1050. .encrypt = spacc_ablk_encrypt,
  1051. .decrypt = spacc_ablk_decrypt,
  1052. .min_keysize = AES_MIN_KEY_SIZE,
  1053. .max_keysize = AES_MAX_KEY_SIZE,
  1054. .ivsize = AES_BLOCK_SIZE,
  1055. },
  1056. .cra_init = spacc_ablk_cra_init,
  1057. .cra_exit = spacc_ablk_cra_exit,
  1058. },
  1059. },
  1060. {
  1061. .key_offs = 0,
  1062. .iv_offs = AES_MAX_KEY_SIZE,
  1063. .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_ECB,
  1064. .alg = {
  1065. .cra_name = "ecb(aes)",
  1066. .cra_driver_name = "ecb-aes-picoxcell",
  1067. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1068. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1069. CRYPTO_ALG_KERN_DRIVER_ONLY |
  1070. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  1071. .cra_blocksize = AES_BLOCK_SIZE,
  1072. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1073. .cra_type = &crypto_ablkcipher_type,
  1074. .cra_module = THIS_MODULE,
  1075. .cra_ablkcipher = {
  1076. .setkey = spacc_aes_setkey,
  1077. .encrypt = spacc_ablk_encrypt,
  1078. .decrypt = spacc_ablk_decrypt,
  1079. .min_keysize = AES_MIN_KEY_SIZE,
  1080. .max_keysize = AES_MAX_KEY_SIZE,
  1081. },
  1082. .cra_init = spacc_ablk_cra_init,
  1083. .cra_exit = spacc_ablk_cra_exit,
  1084. },
  1085. },
  1086. {
  1087. .key_offs = DES_BLOCK_SIZE,
  1088. .iv_offs = 0,
  1089. .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
  1090. .alg = {
  1091. .cra_name = "cbc(des)",
  1092. .cra_driver_name = "cbc-des-picoxcell",
  1093. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1094. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1095. CRYPTO_ALG_ASYNC |
  1096. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1097. .cra_blocksize = DES_BLOCK_SIZE,
  1098. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1099. .cra_type = &crypto_ablkcipher_type,
  1100. .cra_module = THIS_MODULE,
  1101. .cra_ablkcipher = {
  1102. .setkey = spacc_des_setkey,
  1103. .encrypt = spacc_ablk_encrypt,
  1104. .decrypt = spacc_ablk_decrypt,
  1105. .min_keysize = DES_KEY_SIZE,
  1106. .max_keysize = DES_KEY_SIZE,
  1107. .ivsize = DES_BLOCK_SIZE,
  1108. },
  1109. .cra_init = spacc_ablk_cra_init,
  1110. .cra_exit = spacc_ablk_cra_exit,
  1111. },
  1112. },
  1113. {
  1114. .key_offs = DES_BLOCK_SIZE,
  1115. .iv_offs = 0,
  1116. .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
  1117. .alg = {
  1118. .cra_name = "ecb(des)",
  1119. .cra_driver_name = "ecb-des-picoxcell",
  1120. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1121. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1122. CRYPTO_ALG_ASYNC |
  1123. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1124. .cra_blocksize = DES_BLOCK_SIZE,
  1125. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1126. .cra_type = &crypto_ablkcipher_type,
  1127. .cra_module = THIS_MODULE,
  1128. .cra_ablkcipher = {
  1129. .setkey = spacc_des_setkey,
  1130. .encrypt = spacc_ablk_encrypt,
  1131. .decrypt = spacc_ablk_decrypt,
  1132. .min_keysize = DES_KEY_SIZE,
  1133. .max_keysize = DES_KEY_SIZE,
  1134. },
  1135. .cra_init = spacc_ablk_cra_init,
  1136. .cra_exit = spacc_ablk_cra_exit,
  1137. },
  1138. },
  1139. {
  1140. .key_offs = DES_BLOCK_SIZE,
  1141. .iv_offs = 0,
  1142. .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
  1143. .alg = {
  1144. .cra_name = "cbc(des3_ede)",
  1145. .cra_driver_name = "cbc-des3-ede-picoxcell",
  1146. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1147. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1148. CRYPTO_ALG_ASYNC |
  1149. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1150. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1151. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1152. .cra_type = &crypto_ablkcipher_type,
  1153. .cra_module = THIS_MODULE,
  1154. .cra_ablkcipher = {
  1155. .setkey = spacc_des_setkey,
  1156. .encrypt = spacc_ablk_encrypt,
  1157. .decrypt = spacc_ablk_decrypt,
  1158. .min_keysize = DES3_EDE_KEY_SIZE,
  1159. .max_keysize = DES3_EDE_KEY_SIZE,
  1160. .ivsize = DES3_EDE_BLOCK_SIZE,
  1161. },
  1162. .cra_init = spacc_ablk_cra_init,
  1163. .cra_exit = spacc_ablk_cra_exit,
  1164. },
  1165. },
  1166. {
  1167. .key_offs = DES_BLOCK_SIZE,
  1168. .iv_offs = 0,
  1169. .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
  1170. .alg = {
  1171. .cra_name = "ecb(des3_ede)",
  1172. .cra_driver_name = "ecb-des3-ede-picoxcell",
  1173. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1174. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1175. CRYPTO_ALG_ASYNC |
  1176. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1177. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1178. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1179. .cra_type = &crypto_ablkcipher_type,
  1180. .cra_module = THIS_MODULE,
  1181. .cra_ablkcipher = {
  1182. .setkey = spacc_des_setkey,
  1183. .encrypt = spacc_ablk_encrypt,
  1184. .decrypt = spacc_ablk_decrypt,
  1185. .min_keysize = DES3_EDE_KEY_SIZE,
  1186. .max_keysize = DES3_EDE_KEY_SIZE,
  1187. },
  1188. .cra_init = spacc_ablk_cra_init,
  1189. .cra_exit = spacc_ablk_cra_exit,
  1190. },
  1191. },
  1192. };
  1193. static struct spacc_aead ipsec_engine_aeads[] = {
  1194. {
  1195. .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
  1196. SPA_CTRL_CIPH_MODE_CBC |
  1197. SPA_CTRL_HASH_ALG_SHA |
  1198. SPA_CTRL_HASH_MODE_HMAC,
  1199. .key_offs = 0,
  1200. .iv_offs = AES_MAX_KEY_SIZE,
  1201. .alg = {
  1202. .base = {
  1203. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1204. .cra_driver_name = "authenc-hmac-sha1-"
  1205. "cbc-aes-picoxcell",
  1206. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1207. .cra_flags = CRYPTO_ALG_ASYNC |
  1208. CRYPTO_ALG_NEED_FALLBACK |
  1209. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1210. .cra_blocksize = AES_BLOCK_SIZE,
  1211. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1212. .cra_module = THIS_MODULE,
  1213. },
  1214. .setkey = spacc_aead_setkey,
  1215. .setauthsize = spacc_aead_setauthsize,
  1216. .encrypt = spacc_aead_encrypt,
  1217. .decrypt = spacc_aead_decrypt,
  1218. .ivsize = AES_BLOCK_SIZE,
  1219. .maxauthsize = SHA1_DIGEST_SIZE,
  1220. .init = spacc_aead_cra_init,
  1221. .exit = spacc_aead_cra_exit,
  1222. },
  1223. },
  1224. {
  1225. .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
  1226. SPA_CTRL_CIPH_MODE_CBC |
  1227. SPA_CTRL_HASH_ALG_SHA256 |
  1228. SPA_CTRL_HASH_MODE_HMAC,
  1229. .key_offs = 0,
  1230. .iv_offs = AES_MAX_KEY_SIZE,
  1231. .alg = {
  1232. .base = {
  1233. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1234. .cra_driver_name = "authenc-hmac-sha256-"
  1235. "cbc-aes-picoxcell",
  1236. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1237. .cra_flags = CRYPTO_ALG_ASYNC |
  1238. CRYPTO_ALG_NEED_FALLBACK |
  1239. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1240. .cra_blocksize = AES_BLOCK_SIZE,
  1241. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1242. .cra_module = THIS_MODULE,
  1243. },
  1244. .setkey = spacc_aead_setkey,
  1245. .setauthsize = spacc_aead_setauthsize,
  1246. .encrypt = spacc_aead_encrypt,
  1247. .decrypt = spacc_aead_decrypt,
  1248. .ivsize = AES_BLOCK_SIZE,
  1249. .maxauthsize = SHA256_DIGEST_SIZE,
  1250. .init = spacc_aead_cra_init,
  1251. .exit = spacc_aead_cra_exit,
  1252. },
  1253. },
  1254. {
  1255. .key_offs = 0,
  1256. .iv_offs = AES_MAX_KEY_SIZE,
  1257. .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
  1258. SPA_CTRL_CIPH_MODE_CBC |
  1259. SPA_CTRL_HASH_ALG_MD5 |
  1260. SPA_CTRL_HASH_MODE_HMAC,
  1261. .alg = {
  1262. .base = {
  1263. .cra_name = "authenc(hmac(md5),cbc(aes))",
  1264. .cra_driver_name = "authenc-hmac-md5-"
  1265. "cbc-aes-picoxcell",
  1266. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1267. .cra_flags = CRYPTO_ALG_ASYNC |
  1268. CRYPTO_ALG_NEED_FALLBACK |
  1269. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1270. .cra_blocksize = AES_BLOCK_SIZE,
  1271. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1272. .cra_module = THIS_MODULE,
  1273. },
  1274. .setkey = spacc_aead_setkey,
  1275. .setauthsize = spacc_aead_setauthsize,
  1276. .encrypt = spacc_aead_encrypt,
  1277. .decrypt = spacc_aead_decrypt,
  1278. .ivsize = AES_BLOCK_SIZE,
  1279. .maxauthsize = MD5_DIGEST_SIZE,
  1280. .init = spacc_aead_cra_init,
  1281. .exit = spacc_aead_cra_exit,
  1282. },
  1283. },
  1284. {
  1285. .key_offs = DES_BLOCK_SIZE,
  1286. .iv_offs = 0,
  1287. .ctrl_default = SPA_CTRL_CIPH_ALG_DES |
  1288. SPA_CTRL_CIPH_MODE_CBC |
  1289. SPA_CTRL_HASH_ALG_SHA |
  1290. SPA_CTRL_HASH_MODE_HMAC,
  1291. .alg = {
  1292. .base = {
  1293. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  1294. .cra_driver_name = "authenc-hmac-sha1-"
  1295. "cbc-3des-picoxcell",
  1296. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1297. .cra_flags = CRYPTO_ALG_ASYNC |
  1298. CRYPTO_ALG_NEED_FALLBACK |
  1299. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1300. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1301. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1302. .cra_module = THIS_MODULE,
  1303. },
  1304. .setkey = spacc_aead_setkey,
  1305. .setauthsize = spacc_aead_setauthsize,
  1306. .encrypt = spacc_aead_encrypt,
  1307. .decrypt = spacc_aead_decrypt,
  1308. .ivsize = DES3_EDE_BLOCK_SIZE,
  1309. .maxauthsize = SHA1_DIGEST_SIZE,
  1310. .init = spacc_aead_cra_init,
  1311. .exit = spacc_aead_cra_exit,
  1312. },
  1313. },
  1314. {
  1315. .key_offs = DES_BLOCK_SIZE,
  1316. .iv_offs = 0,
  1317. .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
  1318. SPA_CTRL_CIPH_MODE_CBC |
  1319. SPA_CTRL_HASH_ALG_SHA256 |
  1320. SPA_CTRL_HASH_MODE_HMAC,
  1321. .alg = {
  1322. .base = {
  1323. .cra_name = "authenc(hmac(sha256),"
  1324. "cbc(des3_ede))",
  1325. .cra_driver_name = "authenc-hmac-sha256-"
  1326. "cbc-3des-picoxcell",
  1327. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1328. .cra_flags = CRYPTO_ALG_ASYNC |
  1329. CRYPTO_ALG_NEED_FALLBACK |
  1330. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1331. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1332. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1333. .cra_module = THIS_MODULE,
  1334. },
  1335. .setkey = spacc_aead_setkey,
  1336. .setauthsize = spacc_aead_setauthsize,
  1337. .encrypt = spacc_aead_encrypt,
  1338. .decrypt = spacc_aead_decrypt,
  1339. .ivsize = DES3_EDE_BLOCK_SIZE,
  1340. .maxauthsize = SHA256_DIGEST_SIZE,
  1341. .init = spacc_aead_cra_init,
  1342. .exit = spacc_aead_cra_exit,
  1343. },
  1344. },
  1345. {
  1346. .key_offs = DES_BLOCK_SIZE,
  1347. .iv_offs = 0,
  1348. .ctrl_default = SPA_CTRL_CIPH_ALG_DES |
  1349. SPA_CTRL_CIPH_MODE_CBC |
  1350. SPA_CTRL_HASH_ALG_MD5 |
  1351. SPA_CTRL_HASH_MODE_HMAC,
  1352. .alg = {
  1353. .base = {
  1354. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  1355. .cra_driver_name = "authenc-hmac-md5-"
  1356. "cbc-3des-picoxcell",
  1357. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1358. .cra_flags = CRYPTO_ALG_ASYNC |
  1359. CRYPTO_ALG_NEED_FALLBACK |
  1360. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1361. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1362. .cra_ctxsize = sizeof(struct spacc_aead_ctx),
  1363. .cra_module = THIS_MODULE,
  1364. },
  1365. .setkey = spacc_aead_setkey,
  1366. .setauthsize = spacc_aead_setauthsize,
  1367. .encrypt = spacc_aead_encrypt,
  1368. .decrypt = spacc_aead_decrypt,
  1369. .ivsize = DES3_EDE_BLOCK_SIZE,
  1370. .maxauthsize = MD5_DIGEST_SIZE,
  1371. .init = spacc_aead_cra_init,
  1372. .exit = spacc_aead_cra_exit,
  1373. },
  1374. },
  1375. };
  1376. static struct spacc_alg l2_engine_algs[] = {
  1377. {
  1378. .key_offs = 0,
  1379. .iv_offs = SPACC_CRYPTO_KASUMI_F8_KEY_LEN,
  1380. .ctrl_default = SPA_CTRL_CIPH_ALG_KASUMI |
  1381. SPA_CTRL_CIPH_MODE_F8,
  1382. .alg = {
  1383. .cra_name = "f8(kasumi)",
  1384. .cra_driver_name = "f8-kasumi-picoxcell",
  1385. .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
  1386. .cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER |
  1387. CRYPTO_ALG_ASYNC |
  1388. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1389. .cra_blocksize = 8,
  1390. .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
  1391. .cra_type = &crypto_ablkcipher_type,
  1392. .cra_module = THIS_MODULE,
  1393. .cra_ablkcipher = {
  1394. .setkey = spacc_kasumi_f8_setkey,
  1395. .encrypt = spacc_ablk_encrypt,
  1396. .decrypt = spacc_ablk_decrypt,
  1397. .min_keysize = 16,
  1398. .max_keysize = 16,
  1399. .ivsize = 8,
  1400. },
  1401. .cra_init = spacc_ablk_cra_init,
  1402. .cra_exit = spacc_ablk_cra_exit,
  1403. },
  1404. },
  1405. };
  1406. #ifdef CONFIG_OF
  1407. static const struct of_device_id spacc_of_id_table[] = {
  1408. { .compatible = "picochip,spacc-ipsec" },
  1409. { .compatible = "picochip,spacc-l2" },
  1410. {}
  1411. };
  1412. MODULE_DEVICE_TABLE(of, spacc_of_id_table);
  1413. #endif /* CONFIG_OF */
  1414. static int spacc_probe(struct platform_device *pdev)
  1415. {
  1416. int i, err, ret;
  1417. struct resource *mem, *irq;
  1418. struct device_node *np = pdev->dev.of_node;
  1419. struct spacc_engine *engine = devm_kzalloc(&pdev->dev, sizeof(*engine),
  1420. GFP_KERNEL);
  1421. if (!engine)
  1422. return -ENOMEM;
  1423. if (of_device_is_compatible(np, "picochip,spacc-ipsec")) {
  1424. engine->max_ctxs = SPACC_CRYPTO_IPSEC_MAX_CTXS;
  1425. engine->cipher_pg_sz = SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ;
  1426. engine->hash_pg_sz = SPACC_CRYPTO_IPSEC_HASH_PG_SZ;
  1427. engine->fifo_sz = SPACC_CRYPTO_IPSEC_FIFO_SZ;
  1428. engine->algs = ipsec_engine_algs;
  1429. engine->num_algs = ARRAY_SIZE(ipsec_engine_algs);
  1430. engine->aeads = ipsec_engine_aeads;
  1431. engine->num_aeads = ARRAY_SIZE(ipsec_engine_aeads);
  1432. } else if (of_device_is_compatible(np, "picochip,spacc-l2")) {
  1433. engine->max_ctxs = SPACC_CRYPTO_L2_MAX_CTXS;
  1434. engine->cipher_pg_sz = SPACC_CRYPTO_L2_CIPHER_PG_SZ;
  1435. engine->hash_pg_sz = SPACC_CRYPTO_L2_HASH_PG_SZ;
  1436. engine->fifo_sz = SPACC_CRYPTO_L2_FIFO_SZ;
  1437. engine->algs = l2_engine_algs;
  1438. engine->num_algs = ARRAY_SIZE(l2_engine_algs);
  1439. } else {
  1440. return -EINVAL;
  1441. }
  1442. engine->name = dev_name(&pdev->dev);
  1443. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1444. engine->regs = devm_ioremap_resource(&pdev->dev, mem);
  1445. if (IS_ERR(engine->regs))
  1446. return PTR_ERR(engine->regs);
  1447. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1448. if (!irq) {
  1449. dev_err(&pdev->dev, "no memory/irq resource for engine\n");
  1450. return -ENXIO;
  1451. }
  1452. if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0,
  1453. engine->name, engine)) {
  1454. dev_err(engine->dev, "failed to request IRQ\n");
  1455. return -EBUSY;
  1456. }
  1457. engine->dev = &pdev->dev;
  1458. engine->cipher_ctx_base = engine->regs + SPA_CIPH_KEY_BASE_REG_OFFSET;
  1459. engine->hash_key_base = engine->regs + SPA_HASH_KEY_BASE_REG_OFFSET;
  1460. engine->req_pool = dmam_pool_create(engine->name, engine->dev,
  1461. MAX_DDT_LEN * sizeof(struct spacc_ddt), 8, SZ_64K);
  1462. if (!engine->req_pool)
  1463. return -ENOMEM;
  1464. spin_lock_init(&engine->hw_lock);
  1465. engine->clk = clk_get(&pdev->dev, "ref");
  1466. if (IS_ERR(engine->clk)) {
  1467. dev_info(&pdev->dev, "clk unavailable\n");
  1468. return PTR_ERR(engine->clk);
  1469. }
  1470. if (clk_prepare_enable(engine->clk)) {
  1471. dev_info(&pdev->dev, "unable to prepare/enable clk\n");
  1472. ret = -EIO;
  1473. goto err_clk_put;
  1474. }
  1475. ret = device_create_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1476. if (ret)
  1477. goto err_clk_disable;
  1478. /*
  1479. * Use an IRQ threshold of 50% as a default. This seems to be a
  1480. * reasonable trade off of latency against throughput but can be
  1481. * changed at runtime.
  1482. */
  1483. engine->stat_irq_thresh = (engine->fifo_sz / 2);
  1484. /*
  1485. * Configure the interrupts. We only use the STAT_CNT interrupt as we
  1486. * only submit a new packet for processing when we complete another in
  1487. * the queue. This minimizes time spent in the interrupt handler.
  1488. */
  1489. writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
  1490. engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
  1491. writel(SPA_IRQ_EN_STAT_EN | SPA_IRQ_EN_GLBL_EN,
  1492. engine->regs + SPA_IRQ_EN_REG_OFFSET);
  1493. timer_setup(&engine->packet_timeout, spacc_packet_timeout, 0);
  1494. INIT_LIST_HEAD(&engine->pending);
  1495. INIT_LIST_HEAD(&engine->completed);
  1496. INIT_LIST_HEAD(&engine->in_progress);
  1497. engine->in_flight = 0;
  1498. tasklet_init(&engine->complete, spacc_spacc_complete,
  1499. (unsigned long)engine);
  1500. platform_set_drvdata(pdev, engine);
  1501. ret = -EINVAL;
  1502. INIT_LIST_HEAD(&engine->registered_algs);
  1503. for (i = 0; i < engine->num_algs; ++i) {
  1504. engine->algs[i].engine = engine;
  1505. err = crypto_register_alg(&engine->algs[i].alg);
  1506. if (!err) {
  1507. list_add_tail(&engine->algs[i].entry,
  1508. &engine->registered_algs);
  1509. ret = 0;
  1510. }
  1511. if (err)
  1512. dev_err(engine->dev, "failed to register alg \"%s\"\n",
  1513. engine->algs[i].alg.cra_name);
  1514. else
  1515. dev_dbg(engine->dev, "registered alg \"%s\"\n",
  1516. engine->algs[i].alg.cra_name);
  1517. }
  1518. INIT_LIST_HEAD(&engine->registered_aeads);
  1519. for (i = 0; i < engine->num_aeads; ++i) {
  1520. engine->aeads[i].engine = engine;
  1521. err = crypto_register_aead(&engine->aeads[i].alg);
  1522. if (!err) {
  1523. list_add_tail(&engine->aeads[i].entry,
  1524. &engine->registered_aeads);
  1525. ret = 0;
  1526. }
  1527. if (err)
  1528. dev_err(engine->dev, "failed to register alg \"%s\"\n",
  1529. engine->aeads[i].alg.base.cra_name);
  1530. else
  1531. dev_dbg(engine->dev, "registered alg \"%s\"\n",
  1532. engine->aeads[i].alg.base.cra_name);
  1533. }
  1534. if (!ret)
  1535. return 0;
  1536. del_timer_sync(&engine->packet_timeout);
  1537. device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1538. err_clk_disable:
  1539. clk_disable_unprepare(engine->clk);
  1540. err_clk_put:
  1541. clk_put(engine->clk);
  1542. return ret;
  1543. }
  1544. static int spacc_remove(struct platform_device *pdev)
  1545. {
  1546. struct spacc_aead *aead, *an;
  1547. struct spacc_alg *alg, *next;
  1548. struct spacc_engine *engine = platform_get_drvdata(pdev);
  1549. del_timer_sync(&engine->packet_timeout);
  1550. device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1551. list_for_each_entry_safe(aead, an, &engine->registered_aeads, entry) {
  1552. list_del(&aead->entry);
  1553. crypto_unregister_aead(&aead->alg);
  1554. }
  1555. list_for_each_entry_safe(alg, next, &engine->registered_algs, entry) {
  1556. list_del(&alg->entry);
  1557. crypto_unregister_alg(&alg->alg);
  1558. }
  1559. clk_disable_unprepare(engine->clk);
  1560. clk_put(engine->clk);
  1561. return 0;
  1562. }
  1563. static struct platform_driver spacc_driver = {
  1564. .probe = spacc_probe,
  1565. .remove = spacc_remove,
  1566. .driver = {
  1567. .name = "picochip,spacc",
  1568. #ifdef CONFIG_PM
  1569. .pm = &spacc_pm_ops,
  1570. #endif /* CONFIG_PM */
  1571. .of_match_table = of_match_ptr(spacc_of_id_table),
  1572. },
  1573. };
  1574. module_platform_driver(spacc_driver);
  1575. MODULE_LICENSE("GPL");
  1576. MODULE_AUTHOR("Jamie Iles");