cryp_core.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /**
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson.
  4. * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson.
  5. * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
  6. * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
  7. * Author: Jonas Linde <jonas.linde@stericsson.com> for ST-Ericsson.
  8. * Author: Andreas Westin <andreas.westin@stericsson.com> for ST-Ericsson.
  9. * License terms: GNU General Public License (GPL) version 2
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/completion.h>
  13. #include <linux/crypto.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/klist.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/semaphore.h>
  25. #include <linux/platform_data/dma-ste-dma40.h>
  26. #include <crypto/aes.h>
  27. #include <crypto/algapi.h>
  28. #include <crypto/ctr.h>
  29. #include <crypto/des.h>
  30. #include <crypto/scatterwalk.h>
  31. #include <linux/platform_data/crypto-ux500.h>
  32. #include "cryp_p.h"
  33. #include "cryp.h"
  34. #define CRYP_MAX_KEY_SIZE 32
  35. #define BYTES_PER_WORD 4
  36. static int cryp_mode;
  37. static atomic_t session_id;
  38. static struct stedma40_chan_cfg *mem_to_engine;
  39. static struct stedma40_chan_cfg *engine_to_mem;
  40. /**
  41. * struct cryp_driver_data - data specific to the driver.
  42. *
  43. * @device_list: A list of registered devices to choose from.
  44. * @device_allocation: A semaphore initialized with number of devices.
  45. */
  46. struct cryp_driver_data {
  47. struct klist device_list;
  48. struct semaphore device_allocation;
  49. };
  50. /**
  51. * struct cryp_ctx - Crypto context
  52. * @config: Crypto mode.
  53. * @key[CRYP_MAX_KEY_SIZE]: Key.
  54. * @keylen: Length of key.
  55. * @iv: Pointer to initialization vector.
  56. * @indata: Pointer to indata.
  57. * @outdata: Pointer to outdata.
  58. * @datalen: Length of indata.
  59. * @outlen: Length of outdata.
  60. * @blocksize: Size of blocks.
  61. * @updated: Updated flag.
  62. * @dev_ctx: Device dependent context.
  63. * @device: Pointer to the device.
  64. */
  65. struct cryp_ctx {
  66. struct cryp_config config;
  67. u8 key[CRYP_MAX_KEY_SIZE];
  68. u32 keylen;
  69. u8 *iv;
  70. const u8 *indata;
  71. u8 *outdata;
  72. u32 datalen;
  73. u32 outlen;
  74. u32 blocksize;
  75. u8 updated;
  76. struct cryp_device_context dev_ctx;
  77. struct cryp_device_data *device;
  78. u32 session_id;
  79. };
  80. static struct cryp_driver_data driver_data;
  81. /**
  82. * uint8p_to_uint32_be - 4*uint8 to uint32 big endian
  83. * @in: Data to convert.
  84. */
  85. static inline u32 uint8p_to_uint32_be(u8 *in)
  86. {
  87. u32 *data = (u32 *)in;
  88. return cpu_to_be32p(data);
  89. }
  90. /**
  91. * swap_bits_in_byte - mirror the bits in a byte
  92. * @b: the byte to be mirrored
  93. *
  94. * The bits are swapped the following way:
  95. * Byte b include bits 0-7, nibble 1 (n1) include bits 0-3 and
  96. * nibble 2 (n2) bits 4-7.
  97. *
  98. * Nibble 1 (n1):
  99. * (The "old" (moved) bit is replaced with a zero)
  100. * 1. Move bit 6 and 7, 4 positions to the left.
  101. * 2. Move bit 3 and 5, 2 positions to the left.
  102. * 3. Move bit 1-4, 1 position to the left.
  103. *
  104. * Nibble 2 (n2):
  105. * 1. Move bit 0 and 1, 4 positions to the right.
  106. * 2. Move bit 2 and 4, 2 positions to the right.
  107. * 3. Move bit 3-6, 1 position to the right.
  108. *
  109. * Combine the two nibbles to a complete and swapped byte.
  110. */
  111. static inline u8 swap_bits_in_byte(u8 b)
  112. {
  113. #define R_SHIFT_4_MASK 0xc0 /* Bits 6 and 7, right shift 4 */
  114. #define R_SHIFT_2_MASK 0x28 /* (After right shift 4) Bits 3 and 5,
  115. right shift 2 */
  116. #define R_SHIFT_1_MASK 0x1e /* (After right shift 2) Bits 1-4,
  117. right shift 1 */
  118. #define L_SHIFT_4_MASK 0x03 /* Bits 0 and 1, left shift 4 */
  119. #define L_SHIFT_2_MASK 0x14 /* (After left shift 4) Bits 2 and 4,
  120. left shift 2 */
  121. #define L_SHIFT_1_MASK 0x78 /* (After left shift 1) Bits 3-6,
  122. left shift 1 */
  123. u8 n1;
  124. u8 n2;
  125. /* Swap most significant nibble */
  126. /* Right shift 4, bits 6 and 7 */
  127. n1 = ((b & R_SHIFT_4_MASK) >> 4) | (b & ~(R_SHIFT_4_MASK >> 4));
  128. /* Right shift 2, bits 3 and 5 */
  129. n1 = ((n1 & R_SHIFT_2_MASK) >> 2) | (n1 & ~(R_SHIFT_2_MASK >> 2));
  130. /* Right shift 1, bits 1-4 */
  131. n1 = (n1 & R_SHIFT_1_MASK) >> 1;
  132. /* Swap least significant nibble */
  133. /* Left shift 4, bits 0 and 1 */
  134. n2 = ((b & L_SHIFT_4_MASK) << 4) | (b & ~(L_SHIFT_4_MASK << 4));
  135. /* Left shift 2, bits 2 and 4 */
  136. n2 = ((n2 & L_SHIFT_2_MASK) << 2) | (n2 & ~(L_SHIFT_2_MASK << 2));
  137. /* Left shift 1, bits 3-6 */
  138. n2 = (n2 & L_SHIFT_1_MASK) << 1;
  139. return n1 | n2;
  140. }
  141. static inline void swap_words_in_key_and_bits_in_byte(const u8 *in,
  142. u8 *out, u32 len)
  143. {
  144. unsigned int i = 0;
  145. int j;
  146. int index = 0;
  147. j = len - BYTES_PER_WORD;
  148. while (j >= 0) {
  149. for (i = 0; i < BYTES_PER_WORD; i++) {
  150. index = len - j - BYTES_PER_WORD + i;
  151. out[j + i] =
  152. swap_bits_in_byte(in[index]);
  153. }
  154. j -= BYTES_PER_WORD;
  155. }
  156. }
  157. static void add_session_id(struct cryp_ctx *ctx)
  158. {
  159. /*
  160. * We never want 0 to be a valid value, since this is the default value
  161. * for the software context.
  162. */
  163. if (unlikely(atomic_inc_and_test(&session_id)))
  164. atomic_inc(&session_id);
  165. ctx->session_id = atomic_read(&session_id);
  166. }
  167. static irqreturn_t cryp_interrupt_handler(int irq, void *param)
  168. {
  169. struct cryp_ctx *ctx;
  170. int count;
  171. struct cryp_device_data *device_data;
  172. if (param == NULL) {
  173. BUG_ON(!param);
  174. return IRQ_HANDLED;
  175. }
  176. /* The device is coming from the one found in hw_crypt_noxts. */
  177. device_data = (struct cryp_device_data *)param;
  178. ctx = device_data->current_ctx;
  179. if (ctx == NULL) {
  180. BUG_ON(!ctx);
  181. return IRQ_HANDLED;
  182. }
  183. dev_dbg(ctx->device->dev, "[%s] (len: %d) %s, ", __func__, ctx->outlen,
  184. cryp_pending_irq_src(device_data, CRYP_IRQ_SRC_OUTPUT_FIFO) ?
  185. "out" : "in");
  186. if (cryp_pending_irq_src(device_data,
  187. CRYP_IRQ_SRC_OUTPUT_FIFO)) {
  188. if (ctx->outlen / ctx->blocksize > 0) {
  189. count = ctx->blocksize / 4;
  190. readsl(&device_data->base->dout, ctx->outdata, count);
  191. ctx->outdata += count;
  192. ctx->outlen -= count;
  193. if (ctx->outlen == 0) {
  194. cryp_disable_irq_src(device_data,
  195. CRYP_IRQ_SRC_OUTPUT_FIFO);
  196. }
  197. }
  198. } else if (cryp_pending_irq_src(device_data,
  199. CRYP_IRQ_SRC_INPUT_FIFO)) {
  200. if (ctx->datalen / ctx->blocksize > 0) {
  201. count = ctx->blocksize / 4;
  202. writesl(&device_data->base->din, ctx->indata, count);
  203. ctx->indata += count;
  204. ctx->datalen -= count;
  205. if (ctx->datalen == 0)
  206. cryp_disable_irq_src(device_data,
  207. CRYP_IRQ_SRC_INPUT_FIFO);
  208. if (ctx->config.algomode == CRYP_ALGO_AES_XTS) {
  209. CRYP_PUT_BITS(&device_data->base->cr,
  210. CRYP_START_ENABLE,
  211. CRYP_CR_START_POS,
  212. CRYP_CR_START_MASK);
  213. cryp_wait_until_done(device_data);
  214. }
  215. }
  216. }
  217. return IRQ_HANDLED;
  218. }
  219. static int mode_is_aes(enum cryp_algo_mode mode)
  220. {
  221. return CRYP_ALGO_AES_ECB == mode ||
  222. CRYP_ALGO_AES_CBC == mode ||
  223. CRYP_ALGO_AES_CTR == mode ||
  224. CRYP_ALGO_AES_XTS == mode;
  225. }
  226. static int cfg_iv(struct cryp_device_data *device_data, u32 left, u32 right,
  227. enum cryp_init_vector_index index)
  228. {
  229. struct cryp_init_vector_value vector_value;
  230. dev_dbg(device_data->dev, "[%s]", __func__);
  231. vector_value.init_value_left = left;
  232. vector_value.init_value_right = right;
  233. return cryp_configure_init_vector(device_data,
  234. index,
  235. vector_value);
  236. }
  237. static int cfg_ivs(struct cryp_device_data *device_data, struct cryp_ctx *ctx)
  238. {
  239. int i;
  240. int status = 0;
  241. int num_of_regs = ctx->blocksize / 8;
  242. u32 iv[AES_BLOCK_SIZE / 4];
  243. dev_dbg(device_data->dev, "[%s]", __func__);
  244. /*
  245. * Since we loop on num_of_regs we need to have a check in case
  246. * someone provides an incorrect blocksize which would force calling
  247. * cfg_iv with i greater than 2 which is an error.
  248. */
  249. if (num_of_regs > 2) {
  250. dev_err(device_data->dev, "[%s] Incorrect blocksize %d",
  251. __func__, ctx->blocksize);
  252. return -EINVAL;
  253. }
  254. for (i = 0; i < ctx->blocksize / 4; i++)
  255. iv[i] = uint8p_to_uint32_be(ctx->iv + i*4);
  256. for (i = 0; i < num_of_regs; i++) {
  257. status = cfg_iv(device_data, iv[i*2], iv[i*2+1],
  258. (enum cryp_init_vector_index) i);
  259. if (status != 0)
  260. return status;
  261. }
  262. return status;
  263. }
  264. static int set_key(struct cryp_device_data *device_data,
  265. u32 left_key,
  266. u32 right_key,
  267. enum cryp_key_reg_index index)
  268. {
  269. struct cryp_key_value key_value;
  270. int cryp_error;
  271. dev_dbg(device_data->dev, "[%s]", __func__);
  272. key_value.key_value_left = left_key;
  273. key_value.key_value_right = right_key;
  274. cryp_error = cryp_configure_key_values(device_data,
  275. index,
  276. key_value);
  277. if (cryp_error != 0)
  278. dev_err(device_data->dev, "[%s]: "
  279. "cryp_configure_key_values() failed!", __func__);
  280. return cryp_error;
  281. }
  282. static int cfg_keys(struct cryp_ctx *ctx)
  283. {
  284. int i;
  285. int num_of_regs = ctx->keylen / 8;
  286. u32 swapped_key[CRYP_MAX_KEY_SIZE / 4];
  287. int cryp_error = 0;
  288. dev_dbg(ctx->device->dev, "[%s]", __func__);
  289. if (mode_is_aes(ctx->config.algomode)) {
  290. swap_words_in_key_and_bits_in_byte((u8 *)ctx->key,
  291. (u8 *)swapped_key,
  292. ctx->keylen);
  293. } else {
  294. for (i = 0; i < ctx->keylen / 4; i++)
  295. swapped_key[i] = uint8p_to_uint32_be(ctx->key + i*4);
  296. }
  297. for (i = 0; i < num_of_regs; i++) {
  298. cryp_error = set_key(ctx->device,
  299. *(((u32 *)swapped_key)+i*2),
  300. *(((u32 *)swapped_key)+i*2+1),
  301. (enum cryp_key_reg_index) i);
  302. if (cryp_error != 0) {
  303. dev_err(ctx->device->dev, "[%s]: set_key() failed!",
  304. __func__);
  305. return cryp_error;
  306. }
  307. }
  308. return cryp_error;
  309. }
  310. static int cryp_setup_context(struct cryp_ctx *ctx,
  311. struct cryp_device_data *device_data)
  312. {
  313. u32 control_register = CRYP_CR_DEFAULT;
  314. switch (cryp_mode) {
  315. case CRYP_MODE_INTERRUPT:
  316. writel_relaxed(CRYP_IMSC_DEFAULT, &device_data->base->imsc);
  317. break;
  318. case CRYP_MODE_DMA:
  319. writel_relaxed(CRYP_DMACR_DEFAULT, &device_data->base->dmacr);
  320. break;
  321. default:
  322. break;
  323. }
  324. if (ctx->updated == 0) {
  325. cryp_flush_inoutfifo(device_data);
  326. if (cfg_keys(ctx) != 0) {
  327. dev_err(ctx->device->dev, "[%s]: cfg_keys failed!",
  328. __func__);
  329. return -EINVAL;
  330. }
  331. if (ctx->iv &&
  332. CRYP_ALGO_AES_ECB != ctx->config.algomode &&
  333. CRYP_ALGO_DES_ECB != ctx->config.algomode &&
  334. CRYP_ALGO_TDES_ECB != ctx->config.algomode) {
  335. if (cfg_ivs(device_data, ctx) != 0)
  336. return -EPERM;
  337. }
  338. cryp_set_configuration(device_data, &ctx->config,
  339. &control_register);
  340. add_session_id(ctx);
  341. } else if (ctx->updated == 1 &&
  342. ctx->session_id != atomic_read(&session_id)) {
  343. cryp_flush_inoutfifo(device_data);
  344. cryp_restore_device_context(device_data, &ctx->dev_ctx);
  345. add_session_id(ctx);
  346. control_register = ctx->dev_ctx.cr;
  347. } else
  348. control_register = ctx->dev_ctx.cr;
  349. writel(control_register |
  350. (CRYP_CRYPEN_ENABLE << CRYP_CR_CRYPEN_POS),
  351. &device_data->base->cr);
  352. return 0;
  353. }
  354. static int cryp_get_device_data(struct cryp_ctx *ctx,
  355. struct cryp_device_data **device_data)
  356. {
  357. int ret;
  358. struct klist_iter device_iterator;
  359. struct klist_node *device_node;
  360. struct cryp_device_data *local_device_data = NULL;
  361. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  362. /* Wait until a device is available */
  363. ret = down_interruptible(&driver_data.device_allocation);
  364. if (ret)
  365. return ret; /* Interrupted */
  366. /* Select a device */
  367. klist_iter_init(&driver_data.device_list, &device_iterator);
  368. device_node = klist_next(&device_iterator);
  369. while (device_node) {
  370. local_device_data = container_of(device_node,
  371. struct cryp_device_data, list_node);
  372. spin_lock(&local_device_data->ctx_lock);
  373. /* current_ctx allocates a device, NULL = unallocated */
  374. if (local_device_data->current_ctx) {
  375. device_node = klist_next(&device_iterator);
  376. } else {
  377. local_device_data->current_ctx = ctx;
  378. ctx->device = local_device_data;
  379. spin_unlock(&local_device_data->ctx_lock);
  380. break;
  381. }
  382. spin_unlock(&local_device_data->ctx_lock);
  383. }
  384. klist_iter_exit(&device_iterator);
  385. if (!device_node) {
  386. /**
  387. * No free device found.
  388. * Since we allocated a device with down_interruptible, this
  389. * should not be able to happen.
  390. * Number of available devices, which are contained in
  391. * device_allocation, is therefore decremented by not doing
  392. * an up(device_allocation).
  393. */
  394. return -EBUSY;
  395. }
  396. *device_data = local_device_data;
  397. return 0;
  398. }
  399. static void cryp_dma_setup_channel(struct cryp_device_data *device_data,
  400. struct device *dev)
  401. {
  402. struct dma_slave_config mem2cryp = {
  403. .direction = DMA_MEM_TO_DEV,
  404. .dst_addr = device_data->phybase + CRYP_DMA_TX_FIFO,
  405. .dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  406. .dst_maxburst = 4,
  407. };
  408. struct dma_slave_config cryp2mem = {
  409. .direction = DMA_DEV_TO_MEM,
  410. .src_addr = device_data->phybase + CRYP_DMA_RX_FIFO,
  411. .src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
  412. .src_maxburst = 4,
  413. };
  414. dma_cap_zero(device_data->dma.mask);
  415. dma_cap_set(DMA_SLAVE, device_data->dma.mask);
  416. device_data->dma.cfg_mem2cryp = mem_to_engine;
  417. device_data->dma.chan_mem2cryp =
  418. dma_request_channel(device_data->dma.mask,
  419. stedma40_filter,
  420. device_data->dma.cfg_mem2cryp);
  421. device_data->dma.cfg_cryp2mem = engine_to_mem;
  422. device_data->dma.chan_cryp2mem =
  423. dma_request_channel(device_data->dma.mask,
  424. stedma40_filter,
  425. device_data->dma.cfg_cryp2mem);
  426. dmaengine_slave_config(device_data->dma.chan_mem2cryp, &mem2cryp);
  427. dmaengine_slave_config(device_data->dma.chan_cryp2mem, &cryp2mem);
  428. init_completion(&device_data->dma.cryp_dma_complete);
  429. }
  430. static void cryp_dma_out_callback(void *data)
  431. {
  432. struct cryp_ctx *ctx = (struct cryp_ctx *) data;
  433. dev_dbg(ctx->device->dev, "[%s]: ", __func__);
  434. complete(&ctx->device->dma.cryp_dma_complete);
  435. }
  436. static int cryp_set_dma_transfer(struct cryp_ctx *ctx,
  437. struct scatterlist *sg,
  438. int len,
  439. enum dma_data_direction direction)
  440. {
  441. struct dma_async_tx_descriptor *desc;
  442. struct dma_chan *channel = NULL;
  443. dma_cookie_t cookie;
  444. dev_dbg(ctx->device->dev, "[%s]: ", __func__);
  445. if (unlikely(!IS_ALIGNED((u32)sg, 4))) {
  446. dev_err(ctx->device->dev, "[%s]: Data in sg list isn't "
  447. "aligned! Addr: 0x%08x", __func__, (u32)sg);
  448. return -EFAULT;
  449. }
  450. switch (direction) {
  451. case DMA_TO_DEVICE:
  452. channel = ctx->device->dma.chan_mem2cryp;
  453. ctx->device->dma.sg_src = sg;
  454. ctx->device->dma.sg_src_len = dma_map_sg(channel->device->dev,
  455. ctx->device->dma.sg_src,
  456. ctx->device->dma.nents_src,
  457. direction);
  458. if (!ctx->device->dma.sg_src_len) {
  459. dev_dbg(ctx->device->dev,
  460. "[%s]: Could not map the sg list (TO_DEVICE)",
  461. __func__);
  462. return -EFAULT;
  463. }
  464. dev_dbg(ctx->device->dev, "[%s]: Setting up DMA for buffer "
  465. "(TO_DEVICE)", __func__);
  466. desc = dmaengine_prep_slave_sg(channel,
  467. ctx->device->dma.sg_src,
  468. ctx->device->dma.sg_src_len,
  469. direction, DMA_CTRL_ACK);
  470. break;
  471. case DMA_FROM_DEVICE:
  472. channel = ctx->device->dma.chan_cryp2mem;
  473. ctx->device->dma.sg_dst = sg;
  474. ctx->device->dma.sg_dst_len = dma_map_sg(channel->device->dev,
  475. ctx->device->dma.sg_dst,
  476. ctx->device->dma.nents_dst,
  477. direction);
  478. if (!ctx->device->dma.sg_dst_len) {
  479. dev_dbg(ctx->device->dev,
  480. "[%s]: Could not map the sg list (FROM_DEVICE)",
  481. __func__);
  482. return -EFAULT;
  483. }
  484. dev_dbg(ctx->device->dev, "[%s]: Setting up DMA for buffer "
  485. "(FROM_DEVICE)", __func__);
  486. desc = dmaengine_prep_slave_sg(channel,
  487. ctx->device->dma.sg_dst,
  488. ctx->device->dma.sg_dst_len,
  489. direction,
  490. DMA_CTRL_ACK |
  491. DMA_PREP_INTERRUPT);
  492. desc->callback = cryp_dma_out_callback;
  493. desc->callback_param = ctx;
  494. break;
  495. default:
  496. dev_dbg(ctx->device->dev, "[%s]: Invalid DMA direction",
  497. __func__);
  498. return -EFAULT;
  499. }
  500. cookie = dmaengine_submit(desc);
  501. dma_async_issue_pending(channel);
  502. return 0;
  503. }
  504. static void cryp_dma_done(struct cryp_ctx *ctx)
  505. {
  506. struct dma_chan *chan;
  507. dev_dbg(ctx->device->dev, "[%s]: ", __func__);
  508. chan = ctx->device->dma.chan_mem2cryp;
  509. dmaengine_terminate_all(chan);
  510. dma_unmap_sg(chan->device->dev, ctx->device->dma.sg_src,
  511. ctx->device->dma.sg_src_len, DMA_TO_DEVICE);
  512. chan = ctx->device->dma.chan_cryp2mem;
  513. dmaengine_terminate_all(chan);
  514. dma_unmap_sg(chan->device->dev, ctx->device->dma.sg_dst,
  515. ctx->device->dma.sg_dst_len, DMA_FROM_DEVICE);
  516. }
  517. static int cryp_dma_write(struct cryp_ctx *ctx, struct scatterlist *sg,
  518. int len)
  519. {
  520. int error = cryp_set_dma_transfer(ctx, sg, len, DMA_TO_DEVICE);
  521. dev_dbg(ctx->device->dev, "[%s]: ", __func__);
  522. if (error) {
  523. dev_dbg(ctx->device->dev, "[%s]: cryp_set_dma_transfer() "
  524. "failed", __func__);
  525. return error;
  526. }
  527. return len;
  528. }
  529. static int cryp_dma_read(struct cryp_ctx *ctx, struct scatterlist *sg, int len)
  530. {
  531. int error = cryp_set_dma_transfer(ctx, sg, len, DMA_FROM_DEVICE);
  532. if (error) {
  533. dev_dbg(ctx->device->dev, "[%s]: cryp_set_dma_transfer() "
  534. "failed", __func__);
  535. return error;
  536. }
  537. return len;
  538. }
  539. static void cryp_polling_mode(struct cryp_ctx *ctx,
  540. struct cryp_device_data *device_data)
  541. {
  542. int len = ctx->blocksize / BYTES_PER_WORD;
  543. int remaining_length = ctx->datalen;
  544. u32 *indata = (u32 *)ctx->indata;
  545. u32 *outdata = (u32 *)ctx->outdata;
  546. while (remaining_length > 0) {
  547. writesl(&device_data->base->din, indata, len);
  548. indata += len;
  549. remaining_length -= (len * BYTES_PER_WORD);
  550. cryp_wait_until_done(device_data);
  551. readsl(&device_data->base->dout, outdata, len);
  552. outdata += len;
  553. cryp_wait_until_done(device_data);
  554. }
  555. }
  556. static int cryp_disable_power(struct device *dev,
  557. struct cryp_device_data *device_data,
  558. bool save_device_context)
  559. {
  560. int ret = 0;
  561. dev_dbg(dev, "[%s]", __func__);
  562. spin_lock(&device_data->power_state_spinlock);
  563. if (!device_data->power_state)
  564. goto out;
  565. spin_lock(&device_data->ctx_lock);
  566. if (save_device_context && device_data->current_ctx) {
  567. cryp_save_device_context(device_data,
  568. &device_data->current_ctx->dev_ctx,
  569. cryp_mode);
  570. device_data->restore_dev_ctx = true;
  571. }
  572. spin_unlock(&device_data->ctx_lock);
  573. clk_disable(device_data->clk);
  574. ret = regulator_disable(device_data->pwr_regulator);
  575. if (ret)
  576. dev_err(dev, "[%s]: "
  577. "regulator_disable() failed!",
  578. __func__);
  579. device_data->power_state = false;
  580. out:
  581. spin_unlock(&device_data->power_state_spinlock);
  582. return ret;
  583. }
  584. static int cryp_enable_power(
  585. struct device *dev,
  586. struct cryp_device_data *device_data,
  587. bool restore_device_context)
  588. {
  589. int ret = 0;
  590. dev_dbg(dev, "[%s]", __func__);
  591. spin_lock(&device_data->power_state_spinlock);
  592. if (!device_data->power_state) {
  593. ret = regulator_enable(device_data->pwr_regulator);
  594. if (ret) {
  595. dev_err(dev, "[%s]: regulator_enable() failed!",
  596. __func__);
  597. goto out;
  598. }
  599. ret = clk_enable(device_data->clk);
  600. if (ret) {
  601. dev_err(dev, "[%s]: clk_enable() failed!",
  602. __func__);
  603. regulator_disable(device_data->pwr_regulator);
  604. goto out;
  605. }
  606. device_data->power_state = true;
  607. }
  608. if (device_data->restore_dev_ctx) {
  609. spin_lock(&device_data->ctx_lock);
  610. if (restore_device_context && device_data->current_ctx) {
  611. device_data->restore_dev_ctx = false;
  612. cryp_restore_device_context(device_data,
  613. &device_data->current_ctx->dev_ctx);
  614. }
  615. spin_unlock(&device_data->ctx_lock);
  616. }
  617. out:
  618. spin_unlock(&device_data->power_state_spinlock);
  619. return ret;
  620. }
  621. static int hw_crypt_noxts(struct cryp_ctx *ctx,
  622. struct cryp_device_data *device_data)
  623. {
  624. int ret = 0;
  625. const u8 *indata = ctx->indata;
  626. u8 *outdata = ctx->outdata;
  627. u32 datalen = ctx->datalen;
  628. u32 outlen = datalen;
  629. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  630. ctx->outlen = ctx->datalen;
  631. if (unlikely(!IS_ALIGNED((u32)indata, 4))) {
  632. pr_debug(DEV_DBG_NAME " [%s]: Data isn't aligned! Addr: "
  633. "0x%08x", __func__, (u32)indata);
  634. return -EINVAL;
  635. }
  636. ret = cryp_setup_context(ctx, device_data);
  637. if (ret)
  638. goto out;
  639. if (cryp_mode == CRYP_MODE_INTERRUPT) {
  640. cryp_enable_irq_src(device_data, CRYP_IRQ_SRC_INPUT_FIFO |
  641. CRYP_IRQ_SRC_OUTPUT_FIFO);
  642. /*
  643. * ctx->outlen is decremented in the cryp_interrupt_handler
  644. * function. We had to add cpu_relax() (barrier) to make sure
  645. * that gcc didn't optimze away this variable.
  646. */
  647. while (ctx->outlen > 0)
  648. cpu_relax();
  649. } else if (cryp_mode == CRYP_MODE_POLLING ||
  650. cryp_mode == CRYP_MODE_DMA) {
  651. /*
  652. * The reason for having DMA in this if case is that if we are
  653. * running cryp_mode = 2, then we separate DMA routines for
  654. * handling cipher/plaintext > blocksize, except when
  655. * running the normal CRYPTO_ALG_TYPE_CIPHER, then we still use
  656. * the polling mode. Overhead of doing DMA setup eats up the
  657. * benefits using it.
  658. */
  659. cryp_polling_mode(ctx, device_data);
  660. } else {
  661. dev_err(ctx->device->dev, "[%s]: Invalid operation mode!",
  662. __func__);
  663. ret = -EPERM;
  664. goto out;
  665. }
  666. cryp_save_device_context(device_data, &ctx->dev_ctx, cryp_mode);
  667. ctx->updated = 1;
  668. out:
  669. ctx->indata = indata;
  670. ctx->outdata = outdata;
  671. ctx->datalen = datalen;
  672. ctx->outlen = outlen;
  673. return ret;
  674. }
  675. static int get_nents(struct scatterlist *sg, int nbytes)
  676. {
  677. int nents = 0;
  678. while (nbytes > 0) {
  679. nbytes -= sg->length;
  680. sg = sg_next(sg);
  681. nents++;
  682. }
  683. return nents;
  684. }
  685. static int ablk_dma_crypt(struct ablkcipher_request *areq)
  686. {
  687. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  688. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  689. struct cryp_device_data *device_data;
  690. int bytes_written = 0;
  691. int bytes_read = 0;
  692. int ret;
  693. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  694. ctx->datalen = areq->nbytes;
  695. ctx->outlen = areq->nbytes;
  696. ret = cryp_get_device_data(ctx, &device_data);
  697. if (ret)
  698. return ret;
  699. ret = cryp_setup_context(ctx, device_data);
  700. if (ret)
  701. goto out;
  702. /* We have the device now, so store the nents in the dma struct. */
  703. ctx->device->dma.nents_src = get_nents(areq->src, ctx->datalen);
  704. ctx->device->dma.nents_dst = get_nents(areq->dst, ctx->outlen);
  705. /* Enable DMA in- and output. */
  706. cryp_configure_for_dma(device_data, CRYP_DMA_ENABLE_BOTH_DIRECTIONS);
  707. bytes_written = cryp_dma_write(ctx, areq->src, ctx->datalen);
  708. bytes_read = cryp_dma_read(ctx, areq->dst, bytes_written);
  709. wait_for_completion(&ctx->device->dma.cryp_dma_complete);
  710. cryp_dma_done(ctx);
  711. cryp_save_device_context(device_data, &ctx->dev_ctx, cryp_mode);
  712. ctx->updated = 1;
  713. out:
  714. spin_lock(&device_data->ctx_lock);
  715. device_data->current_ctx = NULL;
  716. ctx->device = NULL;
  717. spin_unlock(&device_data->ctx_lock);
  718. /*
  719. * The down_interruptible part for this semaphore is called in
  720. * cryp_get_device_data.
  721. */
  722. up(&driver_data.device_allocation);
  723. if (unlikely(bytes_written != bytes_read))
  724. return -EPERM;
  725. return 0;
  726. }
  727. static int ablk_crypt(struct ablkcipher_request *areq)
  728. {
  729. struct ablkcipher_walk walk;
  730. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  731. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  732. struct cryp_device_data *device_data;
  733. unsigned long src_paddr;
  734. unsigned long dst_paddr;
  735. int ret;
  736. int nbytes;
  737. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  738. ret = cryp_get_device_data(ctx, &device_data);
  739. if (ret)
  740. goto out;
  741. ablkcipher_walk_init(&walk, areq->dst, areq->src, areq->nbytes);
  742. ret = ablkcipher_walk_phys(areq, &walk);
  743. if (ret) {
  744. pr_err(DEV_DBG_NAME "[%s]: ablkcipher_walk_phys() failed!",
  745. __func__);
  746. goto out;
  747. }
  748. while ((nbytes = walk.nbytes) > 0) {
  749. ctx->iv = walk.iv;
  750. src_paddr = (page_to_phys(walk.src.page) + walk.src.offset);
  751. ctx->indata = phys_to_virt(src_paddr);
  752. dst_paddr = (page_to_phys(walk.dst.page) + walk.dst.offset);
  753. ctx->outdata = phys_to_virt(dst_paddr);
  754. ctx->datalen = nbytes - (nbytes % ctx->blocksize);
  755. ret = hw_crypt_noxts(ctx, device_data);
  756. if (ret)
  757. goto out;
  758. nbytes -= ctx->datalen;
  759. ret = ablkcipher_walk_done(areq, &walk, nbytes);
  760. if (ret)
  761. goto out;
  762. }
  763. ablkcipher_walk_complete(&walk);
  764. out:
  765. /* Release the device */
  766. spin_lock(&device_data->ctx_lock);
  767. device_data->current_ctx = NULL;
  768. ctx->device = NULL;
  769. spin_unlock(&device_data->ctx_lock);
  770. /*
  771. * The down_interruptible part for this semaphore is called in
  772. * cryp_get_device_data.
  773. */
  774. up(&driver_data.device_allocation);
  775. return ret;
  776. }
  777. static int aes_ablkcipher_setkey(struct crypto_ablkcipher *cipher,
  778. const u8 *key, unsigned int keylen)
  779. {
  780. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  781. u32 *flags = &cipher->base.crt_flags;
  782. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  783. switch (keylen) {
  784. case AES_KEYSIZE_128:
  785. ctx->config.keysize = CRYP_KEY_SIZE_128;
  786. break;
  787. case AES_KEYSIZE_192:
  788. ctx->config.keysize = CRYP_KEY_SIZE_192;
  789. break;
  790. case AES_KEYSIZE_256:
  791. ctx->config.keysize = CRYP_KEY_SIZE_256;
  792. break;
  793. default:
  794. pr_err(DEV_DBG_NAME "[%s]: Unknown keylen!", __func__);
  795. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  796. return -EINVAL;
  797. }
  798. memcpy(ctx->key, key, keylen);
  799. ctx->keylen = keylen;
  800. ctx->updated = 0;
  801. return 0;
  802. }
  803. static int des_ablkcipher_setkey(struct crypto_ablkcipher *cipher,
  804. const u8 *key, unsigned int keylen)
  805. {
  806. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  807. u32 *flags = &cipher->base.crt_flags;
  808. u32 tmp[DES_EXPKEY_WORDS];
  809. int ret;
  810. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  811. if (keylen != DES_KEY_SIZE) {
  812. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  813. pr_debug(DEV_DBG_NAME " [%s]: CRYPTO_TFM_RES_BAD_KEY_LEN",
  814. __func__);
  815. return -EINVAL;
  816. }
  817. ret = des_ekey(tmp, key);
  818. if (unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  819. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  820. pr_debug(DEV_DBG_NAME " [%s]: CRYPTO_TFM_REQ_WEAK_KEY",
  821. __func__);
  822. return -EINVAL;
  823. }
  824. memcpy(ctx->key, key, keylen);
  825. ctx->keylen = keylen;
  826. ctx->updated = 0;
  827. return 0;
  828. }
  829. static int des3_ablkcipher_setkey(struct crypto_ablkcipher *cipher,
  830. const u8 *key, unsigned int keylen)
  831. {
  832. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  833. u32 *flags = &cipher->base.crt_flags;
  834. const u32 *K = (const u32 *)key;
  835. u32 tmp[DES3_EDE_EXPKEY_WORDS];
  836. int i, ret;
  837. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  838. if (keylen != DES3_EDE_KEY_SIZE) {
  839. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  840. pr_debug(DEV_DBG_NAME " [%s]: CRYPTO_TFM_RES_BAD_KEY_LEN",
  841. __func__);
  842. return -EINVAL;
  843. }
  844. /* Checking key interdependency for weak key detection. */
  845. if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
  846. !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
  847. (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  848. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  849. pr_debug(DEV_DBG_NAME " [%s]: CRYPTO_TFM_REQ_WEAK_KEY",
  850. __func__);
  851. return -EINVAL;
  852. }
  853. for (i = 0; i < 3; i++) {
  854. ret = des_ekey(tmp, key + i*DES_KEY_SIZE);
  855. if (unlikely(ret == 0) && (*flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  856. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  857. pr_debug(DEV_DBG_NAME " [%s]: "
  858. "CRYPTO_TFM_REQ_WEAK_KEY", __func__);
  859. return -EINVAL;
  860. }
  861. }
  862. memcpy(ctx->key, key, keylen);
  863. ctx->keylen = keylen;
  864. ctx->updated = 0;
  865. return 0;
  866. }
  867. static int cryp_blk_encrypt(struct ablkcipher_request *areq)
  868. {
  869. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  870. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  871. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  872. ctx->config.algodir = CRYP_ALGORITHM_ENCRYPT;
  873. /*
  874. * DMA does not work for DES due to a hw bug */
  875. if (cryp_mode == CRYP_MODE_DMA && mode_is_aes(ctx->config.algomode))
  876. return ablk_dma_crypt(areq);
  877. /* For everything except DMA, we run the non DMA version. */
  878. return ablk_crypt(areq);
  879. }
  880. static int cryp_blk_decrypt(struct ablkcipher_request *areq)
  881. {
  882. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  883. struct cryp_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  884. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  885. ctx->config.algodir = CRYP_ALGORITHM_DECRYPT;
  886. /* DMA does not work for DES due to a hw bug */
  887. if (cryp_mode == CRYP_MODE_DMA && mode_is_aes(ctx->config.algomode))
  888. return ablk_dma_crypt(areq);
  889. /* For everything except DMA, we run the non DMA version. */
  890. return ablk_crypt(areq);
  891. }
  892. struct cryp_algo_template {
  893. enum cryp_algo_mode algomode;
  894. struct crypto_alg crypto;
  895. };
  896. static int cryp_cra_init(struct crypto_tfm *tfm)
  897. {
  898. struct cryp_ctx *ctx = crypto_tfm_ctx(tfm);
  899. struct crypto_alg *alg = tfm->__crt_alg;
  900. struct cryp_algo_template *cryp_alg = container_of(alg,
  901. struct cryp_algo_template,
  902. crypto);
  903. ctx->config.algomode = cryp_alg->algomode;
  904. ctx->blocksize = crypto_tfm_alg_blocksize(tfm);
  905. return 0;
  906. }
  907. static struct cryp_algo_template cryp_algs[] = {
  908. {
  909. .algomode = CRYP_ALGO_AES_ECB,
  910. .crypto = {
  911. .cra_name = "aes",
  912. .cra_driver_name = "aes-ux500",
  913. .cra_priority = 300,
  914. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  915. CRYPTO_ALG_ASYNC,
  916. .cra_blocksize = AES_BLOCK_SIZE,
  917. .cra_ctxsize = sizeof(struct cryp_ctx),
  918. .cra_alignmask = 3,
  919. .cra_type = &crypto_ablkcipher_type,
  920. .cra_init = cryp_cra_init,
  921. .cra_module = THIS_MODULE,
  922. .cra_u = {
  923. .ablkcipher = {
  924. .min_keysize = AES_MIN_KEY_SIZE,
  925. .max_keysize = AES_MAX_KEY_SIZE,
  926. .setkey = aes_ablkcipher_setkey,
  927. .encrypt = cryp_blk_encrypt,
  928. .decrypt = cryp_blk_decrypt
  929. }
  930. }
  931. }
  932. },
  933. {
  934. .algomode = CRYP_ALGO_AES_ECB,
  935. .crypto = {
  936. .cra_name = "ecb(aes)",
  937. .cra_driver_name = "ecb-aes-ux500",
  938. .cra_priority = 300,
  939. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  940. CRYPTO_ALG_ASYNC,
  941. .cra_blocksize = AES_BLOCK_SIZE,
  942. .cra_ctxsize = sizeof(struct cryp_ctx),
  943. .cra_alignmask = 3,
  944. .cra_type = &crypto_ablkcipher_type,
  945. .cra_init = cryp_cra_init,
  946. .cra_module = THIS_MODULE,
  947. .cra_u = {
  948. .ablkcipher = {
  949. .min_keysize = AES_MIN_KEY_SIZE,
  950. .max_keysize = AES_MAX_KEY_SIZE,
  951. .setkey = aes_ablkcipher_setkey,
  952. .encrypt = cryp_blk_encrypt,
  953. .decrypt = cryp_blk_decrypt,
  954. }
  955. }
  956. }
  957. },
  958. {
  959. .algomode = CRYP_ALGO_AES_CBC,
  960. .crypto = {
  961. .cra_name = "cbc(aes)",
  962. .cra_driver_name = "cbc-aes-ux500",
  963. .cra_priority = 300,
  964. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  965. CRYPTO_ALG_ASYNC,
  966. .cra_blocksize = AES_BLOCK_SIZE,
  967. .cra_ctxsize = sizeof(struct cryp_ctx),
  968. .cra_alignmask = 3,
  969. .cra_type = &crypto_ablkcipher_type,
  970. .cra_init = cryp_cra_init,
  971. .cra_module = THIS_MODULE,
  972. .cra_u = {
  973. .ablkcipher = {
  974. .min_keysize = AES_MIN_KEY_SIZE,
  975. .max_keysize = AES_MAX_KEY_SIZE,
  976. .setkey = aes_ablkcipher_setkey,
  977. .encrypt = cryp_blk_encrypt,
  978. .decrypt = cryp_blk_decrypt,
  979. .ivsize = AES_BLOCK_SIZE,
  980. }
  981. }
  982. }
  983. },
  984. {
  985. .algomode = CRYP_ALGO_AES_CTR,
  986. .crypto = {
  987. .cra_name = "ctr(aes)",
  988. .cra_driver_name = "ctr-aes-ux500",
  989. .cra_priority = 300,
  990. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  991. CRYPTO_ALG_ASYNC,
  992. .cra_blocksize = AES_BLOCK_SIZE,
  993. .cra_ctxsize = sizeof(struct cryp_ctx),
  994. .cra_alignmask = 3,
  995. .cra_type = &crypto_ablkcipher_type,
  996. .cra_init = cryp_cra_init,
  997. .cra_module = THIS_MODULE,
  998. .cra_u = {
  999. .ablkcipher = {
  1000. .min_keysize = AES_MIN_KEY_SIZE,
  1001. .max_keysize = AES_MAX_KEY_SIZE,
  1002. .setkey = aes_ablkcipher_setkey,
  1003. .encrypt = cryp_blk_encrypt,
  1004. .decrypt = cryp_blk_decrypt,
  1005. .ivsize = AES_BLOCK_SIZE,
  1006. }
  1007. }
  1008. }
  1009. },
  1010. {
  1011. .algomode = CRYP_ALGO_DES_ECB,
  1012. .crypto = {
  1013. .cra_name = "des",
  1014. .cra_driver_name = "des-ux500",
  1015. .cra_priority = 300,
  1016. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1017. CRYPTO_ALG_ASYNC,
  1018. .cra_blocksize = DES_BLOCK_SIZE,
  1019. .cra_ctxsize = sizeof(struct cryp_ctx),
  1020. .cra_alignmask = 3,
  1021. .cra_type = &crypto_ablkcipher_type,
  1022. .cra_init = cryp_cra_init,
  1023. .cra_module = THIS_MODULE,
  1024. .cra_u = {
  1025. .ablkcipher = {
  1026. .min_keysize = DES_KEY_SIZE,
  1027. .max_keysize = DES_KEY_SIZE,
  1028. .setkey = des_ablkcipher_setkey,
  1029. .encrypt = cryp_blk_encrypt,
  1030. .decrypt = cryp_blk_decrypt
  1031. }
  1032. }
  1033. }
  1034. },
  1035. {
  1036. .algomode = CRYP_ALGO_TDES_ECB,
  1037. .crypto = {
  1038. .cra_name = "des3_ede",
  1039. .cra_driver_name = "des3_ede-ux500",
  1040. .cra_priority = 300,
  1041. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1042. CRYPTO_ALG_ASYNC,
  1043. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1044. .cra_ctxsize = sizeof(struct cryp_ctx),
  1045. .cra_alignmask = 3,
  1046. .cra_type = &crypto_ablkcipher_type,
  1047. .cra_init = cryp_cra_init,
  1048. .cra_module = THIS_MODULE,
  1049. .cra_u = {
  1050. .ablkcipher = {
  1051. .min_keysize = DES3_EDE_KEY_SIZE,
  1052. .max_keysize = DES3_EDE_KEY_SIZE,
  1053. .setkey = des_ablkcipher_setkey,
  1054. .encrypt = cryp_blk_encrypt,
  1055. .decrypt = cryp_blk_decrypt
  1056. }
  1057. }
  1058. }
  1059. },
  1060. {
  1061. .algomode = CRYP_ALGO_DES_ECB,
  1062. .crypto = {
  1063. .cra_name = "ecb(des)",
  1064. .cra_driver_name = "ecb-des-ux500",
  1065. .cra_priority = 300,
  1066. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1067. CRYPTO_ALG_ASYNC,
  1068. .cra_blocksize = DES_BLOCK_SIZE,
  1069. .cra_ctxsize = sizeof(struct cryp_ctx),
  1070. .cra_alignmask = 3,
  1071. .cra_type = &crypto_ablkcipher_type,
  1072. .cra_init = cryp_cra_init,
  1073. .cra_module = THIS_MODULE,
  1074. .cra_u = {
  1075. .ablkcipher = {
  1076. .min_keysize = DES_KEY_SIZE,
  1077. .max_keysize = DES_KEY_SIZE,
  1078. .setkey = des_ablkcipher_setkey,
  1079. .encrypt = cryp_blk_encrypt,
  1080. .decrypt = cryp_blk_decrypt,
  1081. }
  1082. }
  1083. }
  1084. },
  1085. {
  1086. .algomode = CRYP_ALGO_TDES_ECB,
  1087. .crypto = {
  1088. .cra_name = "ecb(des3_ede)",
  1089. .cra_driver_name = "ecb-des3_ede-ux500",
  1090. .cra_priority = 300,
  1091. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1092. CRYPTO_ALG_ASYNC,
  1093. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1094. .cra_ctxsize = sizeof(struct cryp_ctx),
  1095. .cra_alignmask = 3,
  1096. .cra_type = &crypto_ablkcipher_type,
  1097. .cra_init = cryp_cra_init,
  1098. .cra_module = THIS_MODULE,
  1099. .cra_u = {
  1100. .ablkcipher = {
  1101. .min_keysize = DES3_EDE_KEY_SIZE,
  1102. .max_keysize = DES3_EDE_KEY_SIZE,
  1103. .setkey = des3_ablkcipher_setkey,
  1104. .encrypt = cryp_blk_encrypt,
  1105. .decrypt = cryp_blk_decrypt,
  1106. }
  1107. }
  1108. }
  1109. },
  1110. {
  1111. .algomode = CRYP_ALGO_DES_CBC,
  1112. .crypto = {
  1113. .cra_name = "cbc(des)",
  1114. .cra_driver_name = "cbc-des-ux500",
  1115. .cra_priority = 300,
  1116. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1117. CRYPTO_ALG_ASYNC,
  1118. .cra_blocksize = DES_BLOCK_SIZE,
  1119. .cra_ctxsize = sizeof(struct cryp_ctx),
  1120. .cra_alignmask = 3,
  1121. .cra_type = &crypto_ablkcipher_type,
  1122. .cra_init = cryp_cra_init,
  1123. .cra_module = THIS_MODULE,
  1124. .cra_u = {
  1125. .ablkcipher = {
  1126. .min_keysize = DES_KEY_SIZE,
  1127. .max_keysize = DES_KEY_SIZE,
  1128. .setkey = des_ablkcipher_setkey,
  1129. .encrypt = cryp_blk_encrypt,
  1130. .decrypt = cryp_blk_decrypt,
  1131. }
  1132. }
  1133. }
  1134. },
  1135. {
  1136. .algomode = CRYP_ALGO_TDES_CBC,
  1137. .crypto = {
  1138. .cra_name = "cbc(des3_ede)",
  1139. .cra_driver_name = "cbc-des3_ede-ux500",
  1140. .cra_priority = 300,
  1141. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1142. CRYPTO_ALG_ASYNC,
  1143. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1144. .cra_ctxsize = sizeof(struct cryp_ctx),
  1145. .cra_alignmask = 3,
  1146. .cra_type = &crypto_ablkcipher_type,
  1147. .cra_init = cryp_cra_init,
  1148. .cra_module = THIS_MODULE,
  1149. .cra_u = {
  1150. .ablkcipher = {
  1151. .min_keysize = DES3_EDE_KEY_SIZE,
  1152. .max_keysize = DES3_EDE_KEY_SIZE,
  1153. .setkey = des3_ablkcipher_setkey,
  1154. .encrypt = cryp_blk_encrypt,
  1155. .decrypt = cryp_blk_decrypt,
  1156. .ivsize = DES3_EDE_BLOCK_SIZE,
  1157. }
  1158. }
  1159. }
  1160. }
  1161. };
  1162. /**
  1163. * cryp_algs_register_all -
  1164. */
  1165. static int cryp_algs_register_all(void)
  1166. {
  1167. int ret;
  1168. int i;
  1169. int count;
  1170. pr_debug("[%s]", __func__);
  1171. for (i = 0; i < ARRAY_SIZE(cryp_algs); i++) {
  1172. ret = crypto_register_alg(&cryp_algs[i].crypto);
  1173. if (ret) {
  1174. count = i;
  1175. pr_err("[%s] alg registration failed",
  1176. cryp_algs[i].crypto.cra_driver_name);
  1177. goto unreg;
  1178. }
  1179. }
  1180. return 0;
  1181. unreg:
  1182. for (i = 0; i < count; i++)
  1183. crypto_unregister_alg(&cryp_algs[i].crypto);
  1184. return ret;
  1185. }
  1186. /**
  1187. * cryp_algs_unregister_all -
  1188. */
  1189. static void cryp_algs_unregister_all(void)
  1190. {
  1191. int i;
  1192. pr_debug(DEV_DBG_NAME " [%s]", __func__);
  1193. for (i = 0; i < ARRAY_SIZE(cryp_algs); i++)
  1194. crypto_unregister_alg(&cryp_algs[i].crypto);
  1195. }
  1196. static int ux500_cryp_probe(struct platform_device *pdev)
  1197. {
  1198. int ret;
  1199. int cryp_error = 0;
  1200. struct resource *res = NULL;
  1201. struct resource *res_irq = NULL;
  1202. struct cryp_device_data *device_data;
  1203. struct cryp_protection_config prot = {
  1204. .privilege_access = CRYP_STATE_ENABLE
  1205. };
  1206. struct device *dev = &pdev->dev;
  1207. dev_dbg(dev, "[%s]", __func__);
  1208. device_data = devm_kzalloc(dev, sizeof(*device_data), GFP_ATOMIC);
  1209. if (!device_data) {
  1210. dev_err(dev, "[%s]: kzalloc() failed!", __func__);
  1211. ret = -ENOMEM;
  1212. goto out;
  1213. }
  1214. device_data->dev = dev;
  1215. device_data->current_ctx = NULL;
  1216. /* Grab the DMA configuration from platform data. */
  1217. mem_to_engine = &((struct cryp_platform_data *)
  1218. dev->platform_data)->mem_to_engine;
  1219. engine_to_mem = &((struct cryp_platform_data *)
  1220. dev->platform_data)->engine_to_mem;
  1221. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1222. if (!res) {
  1223. dev_err(dev, "[%s]: platform_get_resource() failed",
  1224. __func__);
  1225. ret = -ENODEV;
  1226. goto out;
  1227. }
  1228. device_data->phybase = res->start;
  1229. device_data->base = devm_ioremap_resource(dev, res);
  1230. if (IS_ERR(device_data->base)) {
  1231. dev_err(dev, "[%s]: ioremap failed!", __func__);
  1232. ret = PTR_ERR(device_data->base);
  1233. goto out;
  1234. }
  1235. spin_lock_init(&device_data->ctx_lock);
  1236. spin_lock_init(&device_data->power_state_spinlock);
  1237. /* Enable power for CRYP hardware block */
  1238. device_data->pwr_regulator = regulator_get(&pdev->dev, "v-ape");
  1239. if (IS_ERR(device_data->pwr_regulator)) {
  1240. dev_err(dev, "[%s]: could not get cryp regulator", __func__);
  1241. ret = PTR_ERR(device_data->pwr_regulator);
  1242. device_data->pwr_regulator = NULL;
  1243. goto out;
  1244. }
  1245. /* Enable the clk for CRYP hardware block */
  1246. device_data->clk = devm_clk_get(&pdev->dev, NULL);
  1247. if (IS_ERR(device_data->clk)) {
  1248. dev_err(dev, "[%s]: clk_get() failed!", __func__);
  1249. ret = PTR_ERR(device_data->clk);
  1250. goto out_regulator;
  1251. }
  1252. ret = clk_prepare(device_data->clk);
  1253. if (ret) {
  1254. dev_err(dev, "[%s]: clk_prepare() failed!", __func__);
  1255. goto out_regulator;
  1256. }
  1257. /* Enable device power (and clock) */
  1258. ret = cryp_enable_power(device_data->dev, device_data, false);
  1259. if (ret) {
  1260. dev_err(dev, "[%s]: cryp_enable_power() failed!", __func__);
  1261. goto out_clk_unprepare;
  1262. }
  1263. cryp_error = cryp_check(device_data);
  1264. if (cryp_error != 0) {
  1265. dev_err(dev, "[%s]: cryp_init() failed!", __func__);
  1266. ret = -EINVAL;
  1267. goto out_power;
  1268. }
  1269. cryp_error = cryp_configure_protection(device_data, &prot);
  1270. if (cryp_error != 0) {
  1271. dev_err(dev, "[%s]: cryp_configure_protection() failed!",
  1272. __func__);
  1273. ret = -EINVAL;
  1274. goto out_power;
  1275. }
  1276. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1277. if (!res_irq) {
  1278. dev_err(dev, "[%s]: IORESOURCE_IRQ unavailable",
  1279. __func__);
  1280. ret = -ENODEV;
  1281. goto out_power;
  1282. }
  1283. ret = devm_request_irq(&pdev->dev, res_irq->start,
  1284. cryp_interrupt_handler, 0, "cryp1", device_data);
  1285. if (ret) {
  1286. dev_err(dev, "[%s]: Unable to request IRQ", __func__);
  1287. goto out_power;
  1288. }
  1289. if (cryp_mode == CRYP_MODE_DMA)
  1290. cryp_dma_setup_channel(device_data, dev);
  1291. platform_set_drvdata(pdev, device_data);
  1292. /* Put the new device into the device list... */
  1293. klist_add_tail(&device_data->list_node, &driver_data.device_list);
  1294. /* ... and signal that a new device is available. */
  1295. up(&driver_data.device_allocation);
  1296. atomic_set(&session_id, 1);
  1297. ret = cryp_algs_register_all();
  1298. if (ret) {
  1299. dev_err(dev, "[%s]: cryp_algs_register_all() failed!",
  1300. __func__);
  1301. goto out_power;
  1302. }
  1303. dev_info(dev, "successfully registered\n");
  1304. return 0;
  1305. out_power:
  1306. cryp_disable_power(device_data->dev, device_data, false);
  1307. out_clk_unprepare:
  1308. clk_unprepare(device_data->clk);
  1309. out_regulator:
  1310. regulator_put(device_data->pwr_regulator);
  1311. out:
  1312. return ret;
  1313. }
  1314. static int ux500_cryp_remove(struct platform_device *pdev)
  1315. {
  1316. struct cryp_device_data *device_data;
  1317. dev_dbg(&pdev->dev, "[%s]", __func__);
  1318. device_data = platform_get_drvdata(pdev);
  1319. if (!device_data) {
  1320. dev_err(&pdev->dev, "[%s]: platform_get_drvdata() failed!",
  1321. __func__);
  1322. return -ENOMEM;
  1323. }
  1324. /* Try to decrease the number of available devices. */
  1325. if (down_trylock(&driver_data.device_allocation))
  1326. return -EBUSY;
  1327. /* Check that the device is free */
  1328. spin_lock(&device_data->ctx_lock);
  1329. /* current_ctx allocates a device, NULL = unallocated */
  1330. if (device_data->current_ctx) {
  1331. /* The device is busy */
  1332. spin_unlock(&device_data->ctx_lock);
  1333. /* Return the device to the pool. */
  1334. up(&driver_data.device_allocation);
  1335. return -EBUSY;
  1336. }
  1337. spin_unlock(&device_data->ctx_lock);
  1338. /* Remove the device from the list */
  1339. if (klist_node_attached(&device_data->list_node))
  1340. klist_remove(&device_data->list_node);
  1341. /* If this was the last device, remove the services */
  1342. if (list_empty(&driver_data.device_list.k_list))
  1343. cryp_algs_unregister_all();
  1344. if (cryp_disable_power(&pdev->dev, device_data, false))
  1345. dev_err(&pdev->dev, "[%s]: cryp_disable_power() failed",
  1346. __func__);
  1347. clk_unprepare(device_data->clk);
  1348. regulator_put(device_data->pwr_regulator);
  1349. return 0;
  1350. }
  1351. static void ux500_cryp_shutdown(struct platform_device *pdev)
  1352. {
  1353. struct cryp_device_data *device_data;
  1354. dev_dbg(&pdev->dev, "[%s]", __func__);
  1355. device_data = platform_get_drvdata(pdev);
  1356. if (!device_data) {
  1357. dev_err(&pdev->dev, "[%s]: platform_get_drvdata() failed!",
  1358. __func__);
  1359. return;
  1360. }
  1361. /* Check that the device is free */
  1362. spin_lock(&device_data->ctx_lock);
  1363. /* current_ctx allocates a device, NULL = unallocated */
  1364. if (!device_data->current_ctx) {
  1365. if (down_trylock(&driver_data.device_allocation))
  1366. dev_dbg(&pdev->dev, "[%s]: Cryp still in use!"
  1367. "Shutting down anyway...", __func__);
  1368. /**
  1369. * (Allocate the device)
  1370. * Need to set this to non-null (dummy) value,
  1371. * to avoid usage if context switching.
  1372. */
  1373. device_data->current_ctx++;
  1374. }
  1375. spin_unlock(&device_data->ctx_lock);
  1376. /* Remove the device from the list */
  1377. if (klist_node_attached(&device_data->list_node))
  1378. klist_remove(&device_data->list_node);
  1379. /* If this was the last device, remove the services */
  1380. if (list_empty(&driver_data.device_list.k_list))
  1381. cryp_algs_unregister_all();
  1382. if (cryp_disable_power(&pdev->dev, device_data, false))
  1383. dev_err(&pdev->dev, "[%s]: cryp_disable_power() failed",
  1384. __func__);
  1385. }
  1386. #ifdef CONFIG_PM_SLEEP
  1387. static int ux500_cryp_suspend(struct device *dev)
  1388. {
  1389. int ret;
  1390. struct platform_device *pdev = to_platform_device(dev);
  1391. struct cryp_device_data *device_data;
  1392. struct resource *res_irq;
  1393. struct cryp_ctx *temp_ctx = NULL;
  1394. dev_dbg(dev, "[%s]", __func__);
  1395. /* Handle state? */
  1396. device_data = platform_get_drvdata(pdev);
  1397. if (!device_data) {
  1398. dev_err(dev, "[%s]: platform_get_drvdata() failed!", __func__);
  1399. return -ENOMEM;
  1400. }
  1401. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1402. if (!res_irq)
  1403. dev_err(dev, "[%s]: IORESOURCE_IRQ, unavailable", __func__);
  1404. else
  1405. disable_irq(res_irq->start);
  1406. spin_lock(&device_data->ctx_lock);
  1407. if (!device_data->current_ctx)
  1408. device_data->current_ctx++;
  1409. spin_unlock(&device_data->ctx_lock);
  1410. if (device_data->current_ctx == ++temp_ctx) {
  1411. if (down_interruptible(&driver_data.device_allocation))
  1412. dev_dbg(dev, "[%s]: down_interruptible() failed",
  1413. __func__);
  1414. ret = cryp_disable_power(dev, device_data, false);
  1415. } else
  1416. ret = cryp_disable_power(dev, device_data, true);
  1417. if (ret)
  1418. dev_err(dev, "[%s]: cryp_disable_power()", __func__);
  1419. return ret;
  1420. }
  1421. static int ux500_cryp_resume(struct device *dev)
  1422. {
  1423. int ret = 0;
  1424. struct platform_device *pdev = to_platform_device(dev);
  1425. struct cryp_device_data *device_data;
  1426. struct resource *res_irq;
  1427. struct cryp_ctx *temp_ctx = NULL;
  1428. dev_dbg(dev, "[%s]", __func__);
  1429. device_data = platform_get_drvdata(pdev);
  1430. if (!device_data) {
  1431. dev_err(dev, "[%s]: platform_get_drvdata() failed!", __func__);
  1432. return -ENOMEM;
  1433. }
  1434. spin_lock(&device_data->ctx_lock);
  1435. if (device_data->current_ctx == ++temp_ctx)
  1436. device_data->current_ctx = NULL;
  1437. spin_unlock(&device_data->ctx_lock);
  1438. if (!device_data->current_ctx)
  1439. up(&driver_data.device_allocation);
  1440. else
  1441. ret = cryp_enable_power(dev, device_data, true);
  1442. if (ret)
  1443. dev_err(dev, "[%s]: cryp_enable_power() failed!", __func__);
  1444. else {
  1445. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1446. if (res_irq)
  1447. enable_irq(res_irq->start);
  1448. }
  1449. return ret;
  1450. }
  1451. #endif
  1452. static SIMPLE_DEV_PM_OPS(ux500_cryp_pm, ux500_cryp_suspend, ux500_cryp_resume);
  1453. static const struct of_device_id ux500_cryp_match[] = {
  1454. { .compatible = "stericsson,ux500-cryp" },
  1455. { },
  1456. };
  1457. MODULE_DEVICE_TABLE(of, ux500_cryp_match);
  1458. static struct platform_driver cryp_driver = {
  1459. .probe = ux500_cryp_probe,
  1460. .remove = ux500_cryp_remove,
  1461. .shutdown = ux500_cryp_shutdown,
  1462. .driver = {
  1463. .name = "cryp1",
  1464. .of_match_table = ux500_cryp_match,
  1465. .pm = &ux500_cryp_pm,
  1466. }
  1467. };
  1468. static int __init ux500_cryp_mod_init(void)
  1469. {
  1470. pr_debug("[%s] is called!", __func__);
  1471. klist_init(&driver_data.device_list, NULL, NULL);
  1472. /* Initialize the semaphore to 0 devices (locked state) */
  1473. sema_init(&driver_data.device_allocation, 0);
  1474. return platform_driver_register(&cryp_driver);
  1475. }
  1476. static void __exit ux500_cryp_mod_fini(void)
  1477. {
  1478. pr_debug("[%s] is called!", __func__);
  1479. platform_driver_unregister(&cryp_driver);
  1480. return;
  1481. }
  1482. module_init(ux500_cryp_mod_init);
  1483. module_exit(ux500_cryp_mod_fini);
  1484. module_param(cryp_mode, int, 0);
  1485. MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 CRYP crypto engine.");
  1486. MODULE_ALIAS_CRYPTO("aes-all");
  1487. MODULE_ALIAS_CRYPTO("des-all");
  1488. MODULE_LICENSE("GPL");