cryp_core.c 44 KB

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