picoxcell_crypto.c 51 KB

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