cryp_core.c 44 KB

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