picoxcell_crypto.c 53 KB

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