cryp_core.c 44 KB

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