cryp_core.c 45 KB

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