picoxcell_crypto.c 53 KB

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