picoxcell_crypto.c 51 KB

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