picoxcell_crypto.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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. return 0;
  433. badkey:
  434. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  435. return -EINVAL;
  436. }
  437. static int spacc_aead_setauthsize(struct crypto_aead *tfm,
  438. unsigned int authsize)
  439. {
  440. struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm));
  441. return crypto_aead_setauthsize(ctx->sw_cipher, authsize);
  442. }
  443. /*
  444. * Check if an AEAD request requires a fallback operation. Some requests can't
  445. * be completed in hardware because the hardware may not support certain key
  446. * sizes. In these cases we need to complete the request in software.
  447. */
  448. static int spacc_aead_need_fallback(struct aead_request *aead_req)
  449. {
  450. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  451. struct aead_alg *alg = crypto_aead_alg(aead);
  452. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  453. struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
  454. /*
  455. * If we have a non-supported key-length, then we need to do a
  456. * software fallback.
  457. */
  458. if ((spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
  459. SPA_CTRL_CIPH_ALG_AES &&
  460. ctx->cipher_key_len != AES_KEYSIZE_128 &&
  461. ctx->cipher_key_len != AES_KEYSIZE_256)
  462. return 1;
  463. return 0;
  464. }
  465. static int spacc_aead_do_fallback(struct aead_request *req, unsigned alg_type,
  466. bool is_encrypt)
  467. {
  468. struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req));
  469. struct spacc_aead_ctx *ctx = crypto_tfm_ctx(old_tfm);
  470. struct aead_request *subreq = aead_request_ctx(req);
  471. aead_request_set_tfm(subreq, ctx->sw_cipher);
  472. aead_request_set_callback(subreq, req->base.flags,
  473. req->base.complete, req->base.data);
  474. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  475. req->iv);
  476. aead_request_set_ad(subreq, req->assoclen);
  477. return is_encrypt ? crypto_aead_encrypt(subreq) :
  478. crypto_aead_decrypt(subreq);
  479. }
  480. static void spacc_aead_complete(struct spacc_req *req)
  481. {
  482. spacc_aead_free_ddts(req);
  483. req->req->complete(req->req, req->result);
  484. }
  485. static int spacc_aead_submit(struct spacc_req *req)
  486. {
  487. struct aead_request *aead_req =
  488. container_of(req->req, struct aead_request, base);
  489. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  490. unsigned int authsize = crypto_aead_authsize(aead);
  491. struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
  492. struct aead_alg *alg = crypto_aead_alg(aead);
  493. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  494. struct spacc_engine *engine = ctx->generic.engine;
  495. u32 ctrl, proc_len, assoc_len;
  496. req->result = -EINPROGRESS;
  497. req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->cipher_key,
  498. ctx->cipher_key_len, aead_req->iv, crypto_aead_ivsize(aead),
  499. ctx->hash_ctx, ctx->hash_key_len);
  500. /* Set the source and destination DDT pointers. */
  501. writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
  502. writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
  503. writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
  504. assoc_len = aead_req->assoclen;
  505. proc_len = aead_req->cryptlen + assoc_len;
  506. /*
  507. * If we are decrypting, we need to take the length of the ICV out of
  508. * the processing length.
  509. */
  510. if (!req->is_encrypt)
  511. proc_len -= authsize;
  512. writel(proc_len, engine->regs + SPA_PROC_LEN_REG_OFFSET);
  513. writel(assoc_len, engine->regs + SPA_AAD_LEN_REG_OFFSET);
  514. writel(authsize, engine->regs + SPA_ICV_LEN_REG_OFFSET);
  515. writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
  516. writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
  517. ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
  518. (1 << SPA_CTRL_ICV_APPEND);
  519. if (req->is_encrypt)
  520. ctrl |= (1 << SPA_CTRL_ENCRYPT_IDX) | (1 << SPA_CTRL_AAD_COPY);
  521. else
  522. ctrl |= (1 << SPA_CTRL_KEY_EXP);
  523. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  524. writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
  525. return -EINPROGRESS;
  526. }
  527. static int spacc_req_submit(struct spacc_req *req);
  528. static void spacc_push(struct spacc_engine *engine)
  529. {
  530. struct spacc_req *req;
  531. while (!list_empty(&engine->pending) &&
  532. engine->in_flight + 1 <= engine->fifo_sz) {
  533. ++engine->in_flight;
  534. req = list_first_entry(&engine->pending, struct spacc_req,
  535. list);
  536. list_move_tail(&req->list, &engine->in_progress);
  537. req->result = spacc_req_submit(req);
  538. }
  539. }
  540. /*
  541. * Setup an AEAD request for processing. This will configure the engine, load
  542. * the context and then start the packet processing.
  543. */
  544. static int spacc_aead_setup(struct aead_request *req,
  545. unsigned alg_type, bool is_encrypt)
  546. {
  547. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  548. struct aead_alg *alg = crypto_aead_alg(aead);
  549. struct spacc_engine *engine = to_spacc_aead(alg)->engine;
  550. struct spacc_req *dev_req = aead_request_ctx(req);
  551. int err;
  552. unsigned long flags;
  553. dev_req->req = &req->base;
  554. dev_req->is_encrypt = is_encrypt;
  555. dev_req->result = -EBUSY;
  556. dev_req->engine = engine;
  557. dev_req->complete = spacc_aead_complete;
  558. if (unlikely(spacc_aead_need_fallback(req) ||
  559. ((err = spacc_aead_make_ddts(req)) == -E2BIG)))
  560. return spacc_aead_do_fallback(req, alg_type, is_encrypt);
  561. if (err)
  562. goto out;
  563. err = -EINPROGRESS;
  564. spin_lock_irqsave(&engine->hw_lock, flags);
  565. if (unlikely(spacc_fifo_cmd_full(engine)) ||
  566. engine->in_flight + 1 > engine->fifo_sz) {
  567. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  568. err = -EBUSY;
  569. spin_unlock_irqrestore(&engine->hw_lock, flags);
  570. goto out_free_ddts;
  571. }
  572. list_add_tail(&dev_req->list, &engine->pending);
  573. } else {
  574. list_add_tail(&dev_req->list, &engine->pending);
  575. spacc_push(engine);
  576. }
  577. spin_unlock_irqrestore(&engine->hw_lock, flags);
  578. goto out;
  579. out_free_ddts:
  580. spacc_aead_free_ddts(dev_req);
  581. out:
  582. return err;
  583. }
  584. static int spacc_aead_encrypt(struct aead_request *req)
  585. {
  586. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  587. struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
  588. return spacc_aead_setup(req, alg->type, 1);
  589. }
  590. static int spacc_aead_decrypt(struct aead_request *req)
  591. {
  592. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  593. struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
  594. return spacc_aead_setup(req, alg->type, 0);
  595. }
  596. /*
  597. * Initialise a new AEAD context. This is responsible for allocating the
  598. * fallback cipher and initialising the context.
  599. */
  600. static int spacc_aead_cra_init(struct crypto_aead *tfm)
  601. {
  602. struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  603. struct aead_alg *alg = crypto_aead_alg(tfm);
  604. struct spacc_aead *spacc_alg = to_spacc_aead(alg);
  605. struct spacc_engine *engine = spacc_alg->engine;
  606. ctx->generic.flags = spacc_alg->type;
  607. ctx->generic.engine = engine;
  608. ctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
  609. CRYPTO_ALG_NEED_FALLBACK);
  610. if (IS_ERR(ctx->sw_cipher))
  611. return PTR_ERR(ctx->sw_cipher);
  612. ctx->generic.key_offs = spacc_alg->key_offs;
  613. ctx->generic.iv_offs = spacc_alg->iv_offs;
  614. crypto_aead_set_reqsize(
  615. tfm,
  616. max(sizeof(struct spacc_req),
  617. sizeof(struct aead_request) +
  618. crypto_aead_reqsize(ctx->sw_cipher)));
  619. return 0;
  620. }
  621. /*
  622. * Destructor for an AEAD context. This is called when the transform is freed
  623. * and must free the fallback cipher.
  624. */
  625. static void spacc_aead_cra_exit(struct crypto_aead *tfm)
  626. {
  627. struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  628. crypto_free_aead(ctx->sw_cipher);
  629. }
  630. /*
  631. * Set the DES key for a block cipher transform. This also performs weak key
  632. * checking if the transform has requested it.
  633. */
  634. static int spacc_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  635. unsigned int len)
  636. {
  637. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  638. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  639. u32 tmp[DES_EXPKEY_WORDS];
  640. if (len > DES3_EDE_KEY_SIZE) {
  641. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  642. return -EINVAL;
  643. }
  644. if (unlikely(!des_ekey(tmp, key)) &&
  645. (crypto_ablkcipher_get_flags(cipher) & CRYPTO_TFM_REQ_WEAK_KEY)) {
  646. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  647. return -EINVAL;
  648. }
  649. memcpy(ctx->key, key, len);
  650. ctx->key_len = len;
  651. return 0;
  652. }
  653. /*
  654. * Set the key for an AES block cipher. Some key lengths are not supported in
  655. * hardware so this must also check whether a fallback is needed.
  656. */
  657. static int spacc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  658. unsigned int len)
  659. {
  660. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  661. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  662. int err = 0;
  663. if (len > AES_MAX_KEY_SIZE) {
  664. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  665. return -EINVAL;
  666. }
  667. /*
  668. * IPSec engine only supports 128 and 256 bit AES keys. If we get a
  669. * request for any other size (192 bits) then we need to do a software
  670. * fallback.
  671. */
  672. if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256) {
  673. if (!ctx->sw_cipher)
  674. return -EINVAL;
  675. /*
  676. * Set the fallback transform to use the same request flags as
  677. * the hardware transform.
  678. */
  679. crypto_skcipher_clear_flags(ctx->sw_cipher,
  680. CRYPTO_TFM_REQ_MASK);
  681. crypto_skcipher_set_flags(ctx->sw_cipher,
  682. cipher->base.crt_flags &
  683. CRYPTO_TFM_REQ_MASK);
  684. err = crypto_skcipher_setkey(ctx->sw_cipher, key, len);
  685. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  686. tfm->crt_flags |=
  687. crypto_skcipher_get_flags(ctx->sw_cipher) &
  688. CRYPTO_TFM_RES_MASK;
  689. if (err)
  690. goto sw_setkey_failed;
  691. }
  692. memcpy(ctx->key, key, len);
  693. ctx->key_len = len;
  694. sw_setkey_failed:
  695. return err;
  696. }
  697. static int spacc_kasumi_f8_setkey(struct crypto_ablkcipher *cipher,
  698. const u8 *key, unsigned int len)
  699. {
  700. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  701. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  702. int err = 0;
  703. if (len > AES_MAX_KEY_SIZE) {
  704. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  705. err = -EINVAL;
  706. goto out;
  707. }
  708. memcpy(ctx->key, key, len);
  709. ctx->key_len = len;
  710. out:
  711. return err;
  712. }
  713. static int spacc_ablk_need_fallback(struct spacc_req *req)
  714. {
  715. struct spacc_ablk_ctx *ctx;
  716. struct crypto_tfm *tfm = req->req->tfm;
  717. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  718. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  719. ctx = crypto_tfm_ctx(tfm);
  720. return (spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
  721. SPA_CTRL_CIPH_ALG_AES &&
  722. ctx->key_len != AES_KEYSIZE_128 &&
  723. ctx->key_len != AES_KEYSIZE_256;
  724. }
  725. static void spacc_ablk_complete(struct spacc_req *req)
  726. {
  727. struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req);
  728. if (ablk_req->src != ablk_req->dst) {
  729. spacc_free_ddt(req, req->src_ddt, req->src_addr, ablk_req->src,
  730. ablk_req->nbytes, DMA_TO_DEVICE);
  731. spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
  732. ablk_req->nbytes, DMA_FROM_DEVICE);
  733. } else
  734. spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
  735. ablk_req->nbytes, DMA_BIDIRECTIONAL);
  736. req->req->complete(req->req, req->result);
  737. }
  738. static int spacc_ablk_submit(struct spacc_req *req)
  739. {
  740. struct crypto_tfm *tfm = req->req->tfm;
  741. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  742. struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req);
  743. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  744. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  745. struct spacc_engine *engine = ctx->generic.engine;
  746. u32 ctrl;
  747. req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->key,
  748. ctx->key_len, ablk_req->info, alg->cra_ablkcipher.ivsize,
  749. NULL, 0);
  750. writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
  751. writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
  752. writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
  753. writel(ablk_req->nbytes, engine->regs + SPA_PROC_LEN_REG_OFFSET);
  754. writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
  755. writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
  756. writel(0, engine->regs + SPA_AAD_LEN_REG_OFFSET);
  757. ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
  758. (req->is_encrypt ? (1 << SPA_CTRL_ENCRYPT_IDX) :
  759. (1 << SPA_CTRL_KEY_EXP));
  760. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  761. writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
  762. return -EINPROGRESS;
  763. }
  764. static int spacc_ablk_do_fallback(struct ablkcipher_request *req,
  765. unsigned alg_type, bool is_encrypt)
  766. {
  767. struct crypto_tfm *old_tfm =
  768. crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
  769. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(old_tfm);
  770. SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher);
  771. int err;
  772. /*
  773. * Change the request to use the software fallback transform, and once
  774. * the ciphering has completed, put the old transform back into the
  775. * request.
  776. */
  777. skcipher_request_set_tfm(subreq, ctx->sw_cipher);
  778. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  779. skcipher_request_set_crypt(subreq, req->src, req->dst,
  780. req->nbytes, req->info);
  781. err = is_encrypt ? crypto_skcipher_encrypt(subreq) :
  782. crypto_skcipher_decrypt(subreq);
  783. skcipher_request_zero(subreq);
  784. return err;
  785. }
  786. static int spacc_ablk_setup(struct ablkcipher_request *req, unsigned alg_type,
  787. bool is_encrypt)
  788. {
  789. struct crypto_alg *alg = req->base.tfm->__crt_alg;
  790. struct spacc_engine *engine = to_spacc_alg(alg)->engine;
  791. struct spacc_req *dev_req = ablkcipher_request_ctx(req);
  792. unsigned long flags;
  793. int err = -ENOMEM;
  794. dev_req->req = &req->base;
  795. dev_req->is_encrypt = is_encrypt;
  796. dev_req->engine = engine;
  797. dev_req->complete = spacc_ablk_complete;
  798. dev_req->result = -EINPROGRESS;
  799. if (unlikely(spacc_ablk_need_fallback(dev_req)))
  800. return spacc_ablk_do_fallback(req, alg_type, is_encrypt);
  801. /*
  802. * Create the DDT's for the engine. If we share the same source and
  803. * destination then we can optimize by reusing the DDT's.
  804. */
  805. if (req->src != req->dst) {
  806. dev_req->src_ddt = spacc_sg_to_ddt(engine, req->src,
  807. req->nbytes, DMA_TO_DEVICE, &dev_req->src_addr);
  808. if (!dev_req->src_ddt)
  809. goto out;
  810. dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
  811. req->nbytes, DMA_FROM_DEVICE, &dev_req->dst_addr);
  812. if (!dev_req->dst_ddt)
  813. goto out_free_src;
  814. } else {
  815. dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
  816. req->nbytes, DMA_BIDIRECTIONAL, &dev_req->dst_addr);
  817. if (!dev_req->dst_ddt)
  818. goto out;
  819. dev_req->src_ddt = NULL;
  820. dev_req->src_addr = dev_req->dst_addr;
  821. }
  822. err = -EINPROGRESS;
  823. spin_lock_irqsave(&engine->hw_lock, flags);
  824. /*
  825. * Check if the engine will accept the operation now. If it won't then
  826. * we either stick it on the end of a pending list if we can backlog,
  827. * or bailout with an error if not.
  828. */
  829. if (unlikely(spacc_fifo_cmd_full(engine)) ||
  830. engine->in_flight + 1 > engine->fifo_sz) {
  831. if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  832. err = -EBUSY;
  833. spin_unlock_irqrestore(&engine->hw_lock, flags);
  834. goto out_free_ddts;
  835. }
  836. list_add_tail(&dev_req->list, &engine->pending);
  837. } else {
  838. list_add_tail(&dev_req->list, &engine->pending);
  839. spacc_push(engine);
  840. }
  841. spin_unlock_irqrestore(&engine->hw_lock, flags);
  842. goto out;
  843. out_free_ddts:
  844. spacc_free_ddt(dev_req, dev_req->dst_ddt, dev_req->dst_addr, req->dst,
  845. req->nbytes, req->src == req->dst ?
  846. DMA_BIDIRECTIONAL : DMA_FROM_DEVICE);
  847. out_free_src:
  848. if (req->src != req->dst)
  849. spacc_free_ddt(dev_req, dev_req->src_ddt, dev_req->src_addr,
  850. req->src, req->nbytes, DMA_TO_DEVICE);
  851. out:
  852. return err;
  853. }
  854. static int spacc_ablk_cra_init(struct crypto_tfm *tfm)
  855. {
  856. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  857. struct crypto_alg *alg = tfm->__crt_alg;
  858. struct spacc_alg *spacc_alg = to_spacc_alg(alg);
  859. struct spacc_engine *engine = spacc_alg->engine;
  860. ctx->generic.flags = spacc_alg->type;
  861. ctx->generic.engine = engine;
  862. if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
  863. ctx->sw_cipher = crypto_alloc_skcipher(
  864. alg->cra_name, 0, CRYPTO_ALG_ASYNC |
  865. CRYPTO_ALG_NEED_FALLBACK);
  866. if (IS_ERR(ctx->sw_cipher)) {
  867. dev_warn(engine->dev, "failed to allocate fallback for %s\n",
  868. alg->cra_name);
  869. return PTR_ERR(ctx->sw_cipher);
  870. }
  871. }
  872. ctx->generic.key_offs = spacc_alg->key_offs;
  873. ctx->generic.iv_offs = spacc_alg->iv_offs;
  874. tfm->crt_ablkcipher.reqsize = sizeof(struct spacc_req);
  875. return 0;
  876. }
  877. static void spacc_ablk_cra_exit(struct crypto_tfm *tfm)
  878. {
  879. struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
  880. crypto_free_skcipher(ctx->sw_cipher);
  881. }
  882. static int spacc_ablk_encrypt(struct ablkcipher_request *req)
  883. {
  884. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  885. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  886. struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
  887. return spacc_ablk_setup(req, alg->type, 1);
  888. }
  889. static int spacc_ablk_decrypt(struct ablkcipher_request *req)
  890. {
  891. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
  892. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  893. struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
  894. return spacc_ablk_setup(req, alg->type, 0);
  895. }
  896. static inline int spacc_fifo_stat_empty(struct spacc_engine *engine)
  897. {
  898. return readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET) &
  899. SPA_FIFO_STAT_EMPTY;
  900. }
  901. static void spacc_process_done(struct spacc_engine *engine)
  902. {
  903. struct spacc_req *req;
  904. unsigned long flags;
  905. spin_lock_irqsave(&engine->hw_lock, flags);
  906. while (!spacc_fifo_stat_empty(engine)) {
  907. req = list_first_entry(&engine->in_progress, struct spacc_req,
  908. list);
  909. list_move_tail(&req->list, &engine->completed);
  910. --engine->in_flight;
  911. /* POP the status register. */
  912. writel(~0, engine->regs + SPA_STAT_POP_REG_OFFSET);
  913. req->result = (readl(engine->regs + SPA_STATUS_REG_OFFSET) &
  914. SPA_STATUS_RES_CODE_MASK) >> SPA_STATUS_RES_CODE_OFFSET;
  915. /*
  916. * Convert the SPAcc error status into the standard POSIX error
  917. * codes.
  918. */
  919. if (unlikely(req->result)) {
  920. switch (req->result) {
  921. case SPA_STATUS_ICV_FAIL:
  922. req->result = -EBADMSG;
  923. break;
  924. case SPA_STATUS_MEMORY_ERROR:
  925. dev_warn(engine->dev,
  926. "memory error triggered\n");
  927. req->result = -EFAULT;
  928. break;
  929. case SPA_STATUS_BLOCK_ERROR:
  930. dev_warn(engine->dev,
  931. "block error triggered\n");
  932. req->result = -EIO;
  933. break;
  934. }
  935. }
  936. }
  937. tasklet_schedule(&engine->complete);
  938. spin_unlock_irqrestore(&engine->hw_lock, flags);
  939. }
  940. static irqreturn_t spacc_spacc_irq(int irq, void *dev)
  941. {
  942. struct spacc_engine *engine = (struct spacc_engine *)dev;
  943. u32 spacc_irq_stat = readl(engine->regs + SPA_IRQ_STAT_REG_OFFSET);
  944. writel(spacc_irq_stat, engine->regs + SPA_IRQ_STAT_REG_OFFSET);
  945. spacc_process_done(engine);
  946. return IRQ_HANDLED;
  947. }
  948. static void spacc_packet_timeout(unsigned long data)
  949. {
  950. struct spacc_engine *engine = (struct spacc_engine *)data;
  951. spacc_process_done(engine);
  952. }
  953. static int spacc_req_submit(struct spacc_req *req)
  954. {
  955. struct crypto_alg *alg = req->req->tfm->__crt_alg;
  956. if (CRYPTO_ALG_TYPE_AEAD == (CRYPTO_ALG_TYPE_MASK & alg->cra_flags))
  957. return spacc_aead_submit(req);
  958. else
  959. return spacc_ablk_submit(req);
  960. }
  961. static void spacc_spacc_complete(unsigned long data)
  962. {
  963. struct spacc_engine *engine = (struct spacc_engine *)data;
  964. struct spacc_req *req, *tmp;
  965. unsigned long flags;
  966. LIST_HEAD(completed);
  967. spin_lock_irqsave(&engine->hw_lock, flags);
  968. list_splice_init(&engine->completed, &completed);
  969. spacc_push(engine);
  970. if (engine->in_flight)
  971. mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
  972. spin_unlock_irqrestore(&engine->hw_lock, flags);
  973. list_for_each_entry_safe(req, tmp, &completed, list) {
  974. list_del(&req->list);
  975. req->complete(req);
  976. }
  977. }
  978. #ifdef CONFIG_PM
  979. static int spacc_suspend(struct device *dev)
  980. {
  981. struct platform_device *pdev = to_platform_device(dev);
  982. struct spacc_engine *engine = platform_get_drvdata(pdev);
  983. /*
  984. * We only support standby mode. All we have to do is gate the clock to
  985. * the spacc. The hardware will preserve state until we turn it back
  986. * on again.
  987. */
  988. clk_disable(engine->clk);
  989. return 0;
  990. }
  991. static int spacc_resume(struct device *dev)
  992. {
  993. struct platform_device *pdev = to_platform_device(dev);
  994. struct spacc_engine *engine = platform_get_drvdata(pdev);
  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 = -EINVAL;
  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. device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1477. return PTR_ERR(engine->clk);
  1478. }
  1479. if (clk_prepare_enable(engine->clk)) {
  1480. dev_info(&pdev->dev, "unable to prepare/enable clk\n");
  1481. clk_put(engine->clk);
  1482. return -EIO;
  1483. }
  1484. err = device_create_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1485. if (err) {
  1486. clk_disable_unprepare(engine->clk);
  1487. clk_put(engine->clk);
  1488. return err;
  1489. }
  1490. /*
  1491. * Use an IRQ threshold of 50% as a default. This seems to be a
  1492. * reasonable trade off of latency against throughput but can be
  1493. * changed at runtime.
  1494. */
  1495. engine->stat_irq_thresh = (engine->fifo_sz / 2);
  1496. /*
  1497. * Configure the interrupts. We only use the STAT_CNT interrupt as we
  1498. * only submit a new packet for processing when we complete another in
  1499. * the queue. This minimizes time spent in the interrupt handler.
  1500. */
  1501. writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
  1502. engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
  1503. writel(SPA_IRQ_EN_STAT_EN | SPA_IRQ_EN_GLBL_EN,
  1504. engine->regs + SPA_IRQ_EN_REG_OFFSET);
  1505. setup_timer(&engine->packet_timeout, spacc_packet_timeout,
  1506. (unsigned long)engine);
  1507. INIT_LIST_HEAD(&engine->pending);
  1508. INIT_LIST_HEAD(&engine->completed);
  1509. INIT_LIST_HEAD(&engine->in_progress);
  1510. engine->in_flight = 0;
  1511. platform_set_drvdata(pdev, engine);
  1512. INIT_LIST_HEAD(&engine->registered_algs);
  1513. for (i = 0; i < engine->num_algs; ++i) {
  1514. engine->algs[i].engine = engine;
  1515. err = crypto_register_alg(&engine->algs[i].alg);
  1516. if (!err) {
  1517. list_add_tail(&engine->algs[i].entry,
  1518. &engine->registered_algs);
  1519. ret = 0;
  1520. }
  1521. if (err)
  1522. dev_err(engine->dev, "failed to register alg \"%s\"\n",
  1523. engine->algs[i].alg.cra_name);
  1524. else
  1525. dev_dbg(engine->dev, "registered alg \"%s\"\n",
  1526. engine->algs[i].alg.cra_name);
  1527. }
  1528. INIT_LIST_HEAD(&engine->registered_aeads);
  1529. for (i = 0; i < engine->num_aeads; ++i) {
  1530. engine->aeads[i].engine = engine;
  1531. err = crypto_register_aead(&engine->aeads[i].alg);
  1532. if (!err) {
  1533. list_add_tail(&engine->aeads[i].entry,
  1534. &engine->registered_aeads);
  1535. ret = 0;
  1536. }
  1537. if (err)
  1538. dev_err(engine->dev, "failed to register alg \"%s\"\n",
  1539. engine->aeads[i].alg.base.cra_name);
  1540. else
  1541. dev_dbg(engine->dev, "registered alg \"%s\"\n",
  1542. engine->aeads[i].alg.base.cra_name);
  1543. }
  1544. return ret;
  1545. }
  1546. static int spacc_remove(struct platform_device *pdev)
  1547. {
  1548. struct spacc_aead *aead, *an;
  1549. struct spacc_alg *alg, *next;
  1550. struct spacc_engine *engine = platform_get_drvdata(pdev);
  1551. del_timer_sync(&engine->packet_timeout);
  1552. device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
  1553. list_for_each_entry_safe(aead, an, &engine->registered_aeads, entry) {
  1554. list_del(&aead->entry);
  1555. crypto_unregister_aead(&aead->alg);
  1556. }
  1557. list_for_each_entry_safe(alg, next, &engine->registered_algs, entry) {
  1558. list_del(&alg->entry);
  1559. crypto_unregister_alg(&alg->alg);
  1560. }
  1561. clk_disable_unprepare(engine->clk);
  1562. clk_put(engine->clk);
  1563. return 0;
  1564. }
  1565. static struct platform_driver spacc_driver = {
  1566. .probe = spacc_probe,
  1567. .remove = spacc_remove,
  1568. .driver = {
  1569. .name = "picochip,spacc",
  1570. #ifdef CONFIG_PM
  1571. .pm = &spacc_pm_ops,
  1572. #endif /* CONFIG_PM */
  1573. .of_match_table = of_match_ptr(spacc_of_id_table),
  1574. },
  1575. };
  1576. module_platform_driver(spacc_driver);
  1577. MODULE_LICENSE("GPL");
  1578. MODULE_AUTHOR("Jamie Iles");