hash_core.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*
  2. * Cryptographic API.
  3. * Support for Nomadik hardware crypto engine.
  4. * Copyright (C) ST-Ericsson SA 2010
  5. * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson
  6. * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson
  7. * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
  8. * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
  9. * Author: Andreas Westin <andreas.westin@stericsson.com> for ST-Ericsson.
  10. * License terms: GNU General Public License (GPL) version 2
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/klist.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/crypto.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/bitops.h>
  25. #include <crypto/internal/hash.h>
  26. #include <crypto/sha.h>
  27. #include <crypto/scatterwalk.h>
  28. #include <crypto/algapi.h>
  29. #include <linux/platform_data/crypto-ux500.h>
  30. #include "hash_alg.h"
  31. #define DEV_DBG_NAME "hashX hashX:"
  32. static int hash_mode;
  33. module_param(hash_mode, int, 0);
  34. MODULE_PARM_DESC(hash_mode, "CPU or DMA mode. CPU = 0 (default), DMA = 1");
  35. /**
  36. * Pre-calculated empty message digests.
  37. */
  38. static u8 zero_message_hash_sha1[SHA1_DIGEST_SIZE] = {
  39. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
  40. 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
  41. 0xaf, 0xd8, 0x07, 0x09
  42. };
  43. static u8 zero_message_hash_sha256[SHA256_DIGEST_SIZE] = {
  44. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
  45. 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
  46. 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
  47. 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  48. };
  49. /* HMAC-SHA1, no key */
  50. static u8 zero_message_hmac_sha1[SHA1_DIGEST_SIZE] = {
  51. 0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
  52. 0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
  53. 0x70, 0x69, 0x0e, 0x1d
  54. };
  55. /* HMAC-SHA256, no key */
  56. static u8 zero_message_hmac_sha256[SHA256_DIGEST_SIZE] = {
  57. 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
  58. 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5,
  59. 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53,
  60. 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad
  61. };
  62. /**
  63. * struct hash_driver_data - data specific to the driver.
  64. *
  65. * @device_list: A list of registered devices to choose from.
  66. * @device_allocation: A semaphore initialized with number of devices.
  67. */
  68. struct hash_driver_data {
  69. struct klist device_list;
  70. struct semaphore device_allocation;
  71. };
  72. static struct hash_driver_data driver_data;
  73. /* Declaration of functions */
  74. /**
  75. * hash_messagepad - Pads a message and write the nblw bits.
  76. * @device_data: Structure for the hash device.
  77. * @message: Last word of a message
  78. * @index_bytes: The number of bytes in the last message
  79. *
  80. * This function manages the final part of the digest calculation, when less
  81. * than 512 bits (64 bytes) remain in message. This means index_bytes < 64.
  82. *
  83. */
  84. static void hash_messagepad(struct hash_device_data *device_data,
  85. const u32 *message, u8 index_bytes);
  86. /**
  87. * release_hash_device - Releases a previously allocated hash device.
  88. * @device_data: Structure for the hash device.
  89. *
  90. */
  91. static void release_hash_device(struct hash_device_data *device_data)
  92. {
  93. spin_lock(&device_data->ctx_lock);
  94. device_data->current_ctx->device = NULL;
  95. device_data->current_ctx = NULL;
  96. spin_unlock(&device_data->ctx_lock);
  97. /*
  98. * The down_interruptible part for this semaphore is called in
  99. * cryp_get_device_data.
  100. */
  101. up(&driver_data.device_allocation);
  102. }
  103. static void hash_dma_setup_channel(struct hash_device_data *device_data,
  104. struct device *dev)
  105. {
  106. struct hash_platform_data *platform_data = dev->platform_data;
  107. dma_cap_zero(device_data->dma.mask);
  108. dma_cap_set(DMA_SLAVE, device_data->dma.mask);
  109. device_data->dma.cfg_mem2hash = platform_data->mem_to_engine;
  110. device_data->dma.chan_mem2hash =
  111. dma_request_channel(device_data->dma.mask,
  112. platform_data->dma_filter,
  113. device_data->dma.cfg_mem2hash);
  114. init_completion(&device_data->dma.complete);
  115. }
  116. static void hash_dma_callback(void *data)
  117. {
  118. struct hash_ctx *ctx = (struct hash_ctx *) data;
  119. complete(&ctx->device->dma.complete);
  120. }
  121. static int hash_set_dma_transfer(struct hash_ctx *ctx, struct scatterlist *sg,
  122. int len, enum dma_data_direction direction)
  123. {
  124. struct dma_async_tx_descriptor *desc = NULL;
  125. struct dma_chan *channel = NULL;
  126. dma_cookie_t cookie;
  127. if (direction != DMA_TO_DEVICE) {
  128. dev_err(ctx->device->dev, "[%s] Invalid DMA direction",
  129. __func__);
  130. return -EFAULT;
  131. }
  132. sg->length = ALIGN(sg->length, HASH_DMA_ALIGN_SIZE);
  133. channel = ctx->device->dma.chan_mem2hash;
  134. ctx->device->dma.sg = sg;
  135. ctx->device->dma.sg_len = dma_map_sg(channel->device->dev,
  136. ctx->device->dma.sg, ctx->device->dma.nents,
  137. direction);
  138. if (!ctx->device->dma.sg_len) {
  139. dev_err(ctx->device->dev,
  140. "[%s]: Could not map the sg list (TO_DEVICE)",
  141. __func__);
  142. return -EFAULT;
  143. }
  144. dev_dbg(ctx->device->dev, "[%s]: Setting up DMA for buffer "
  145. "(TO_DEVICE)", __func__);
  146. desc = channel->device->device_prep_slave_sg(channel,
  147. ctx->device->dma.sg, ctx->device->dma.sg_len,
  148. direction, DMA_CTRL_ACK | DMA_PREP_INTERRUPT, NULL);
  149. if (!desc) {
  150. dev_err(ctx->device->dev,
  151. "[%s]: device_prep_slave_sg() failed!", __func__);
  152. return -EFAULT;
  153. }
  154. desc->callback = hash_dma_callback;
  155. desc->callback_param = ctx;
  156. cookie = desc->tx_submit(desc);
  157. dma_async_issue_pending(channel);
  158. return 0;
  159. }
  160. static void hash_dma_done(struct hash_ctx *ctx)
  161. {
  162. struct dma_chan *chan;
  163. chan = ctx->device->dma.chan_mem2hash;
  164. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  165. dma_unmap_sg(chan->device->dev, ctx->device->dma.sg,
  166. ctx->device->dma.sg_len, DMA_TO_DEVICE);
  167. }
  168. static int hash_dma_write(struct hash_ctx *ctx,
  169. struct scatterlist *sg, int len)
  170. {
  171. int error = hash_set_dma_transfer(ctx, sg, len, DMA_TO_DEVICE);
  172. if (error) {
  173. dev_dbg(ctx->device->dev, "[%s]: hash_set_dma_transfer() "
  174. "failed", __func__);
  175. return error;
  176. }
  177. return len;
  178. }
  179. /**
  180. * get_empty_message_digest - Returns a pre-calculated digest for
  181. * the empty message.
  182. * @device_data: Structure for the hash device.
  183. * @zero_hash: Buffer to return the empty message digest.
  184. * @zero_hash_size: Hash size of the empty message digest.
  185. * @zero_digest: True if zero_digest returned.
  186. */
  187. static int get_empty_message_digest(
  188. struct hash_device_data *device_data,
  189. u8 *zero_hash, u32 *zero_hash_size, bool *zero_digest)
  190. {
  191. int ret = 0;
  192. struct hash_ctx *ctx = device_data->current_ctx;
  193. *zero_digest = false;
  194. /**
  195. * Caller responsible for ctx != NULL.
  196. */
  197. if (HASH_OPER_MODE_HASH == ctx->config.oper_mode) {
  198. if (HASH_ALGO_SHA1 == ctx->config.algorithm) {
  199. memcpy(zero_hash, &zero_message_hash_sha1[0],
  200. SHA1_DIGEST_SIZE);
  201. *zero_hash_size = SHA1_DIGEST_SIZE;
  202. *zero_digest = true;
  203. } else if (HASH_ALGO_SHA256 ==
  204. ctx->config.algorithm) {
  205. memcpy(zero_hash, &zero_message_hash_sha256[0],
  206. SHA256_DIGEST_SIZE);
  207. *zero_hash_size = SHA256_DIGEST_SIZE;
  208. *zero_digest = true;
  209. } else {
  210. dev_err(device_data->dev, "[%s] "
  211. "Incorrect algorithm!"
  212. , __func__);
  213. ret = -EINVAL;
  214. goto out;
  215. }
  216. } else if (HASH_OPER_MODE_HMAC == ctx->config.oper_mode) {
  217. if (!ctx->keylen) {
  218. if (HASH_ALGO_SHA1 == ctx->config.algorithm) {
  219. memcpy(zero_hash, &zero_message_hmac_sha1[0],
  220. SHA1_DIGEST_SIZE);
  221. *zero_hash_size = SHA1_DIGEST_SIZE;
  222. *zero_digest = true;
  223. } else if (HASH_ALGO_SHA256 == ctx->config.algorithm) {
  224. memcpy(zero_hash, &zero_message_hmac_sha256[0],
  225. SHA256_DIGEST_SIZE);
  226. *zero_hash_size = SHA256_DIGEST_SIZE;
  227. *zero_digest = true;
  228. } else {
  229. dev_err(device_data->dev, "[%s] "
  230. "Incorrect algorithm!"
  231. , __func__);
  232. ret = -EINVAL;
  233. goto out;
  234. }
  235. } else {
  236. dev_dbg(device_data->dev, "[%s] Continue hash "
  237. "calculation, since hmac key avalable",
  238. __func__);
  239. }
  240. }
  241. out:
  242. return ret;
  243. }
  244. /**
  245. * hash_disable_power - Request to disable power and clock.
  246. * @device_data: Structure for the hash device.
  247. * @save_device_state: If true, saves the current hw state.
  248. *
  249. * This function request for disabling power (regulator) and clock,
  250. * and could also save current hw state.
  251. */
  252. static int hash_disable_power(
  253. struct hash_device_data *device_data,
  254. bool save_device_state)
  255. {
  256. int ret = 0;
  257. struct device *dev = device_data->dev;
  258. spin_lock(&device_data->power_state_lock);
  259. if (!device_data->power_state)
  260. goto out;
  261. if (save_device_state) {
  262. hash_save_state(device_data,
  263. &device_data->state);
  264. device_data->restore_dev_state = true;
  265. }
  266. clk_disable(device_data->clk);
  267. ret = regulator_disable(device_data->regulator);
  268. if (ret)
  269. dev_err(dev, "[%s] regulator_disable() failed!", __func__);
  270. device_data->power_state = false;
  271. out:
  272. spin_unlock(&device_data->power_state_lock);
  273. return ret;
  274. }
  275. /**
  276. * hash_enable_power - Request to enable power and clock.
  277. * @device_data: Structure for the hash device.
  278. * @restore_device_state: If true, restores a previous saved hw state.
  279. *
  280. * This function request for enabling power (regulator) and clock,
  281. * and could also restore a previously saved hw state.
  282. */
  283. static int hash_enable_power(
  284. struct hash_device_data *device_data,
  285. bool restore_device_state)
  286. {
  287. int ret = 0;
  288. struct device *dev = device_data->dev;
  289. spin_lock(&device_data->power_state_lock);
  290. if (!device_data->power_state) {
  291. ret = regulator_enable(device_data->regulator);
  292. if (ret) {
  293. dev_err(dev, "[%s]: regulator_enable() failed!",
  294. __func__);
  295. goto out;
  296. }
  297. ret = clk_enable(device_data->clk);
  298. if (ret) {
  299. dev_err(dev, "[%s]: clk_enable() failed!",
  300. __func__);
  301. ret = regulator_disable(
  302. device_data->regulator);
  303. goto out;
  304. }
  305. device_data->power_state = true;
  306. }
  307. if (device_data->restore_dev_state) {
  308. if (restore_device_state) {
  309. device_data->restore_dev_state = false;
  310. hash_resume_state(device_data,
  311. &device_data->state);
  312. }
  313. }
  314. out:
  315. spin_unlock(&device_data->power_state_lock);
  316. return ret;
  317. }
  318. /**
  319. * hash_get_device_data - Checks for an available hash device and return it.
  320. * @hash_ctx: Structure for the hash context.
  321. * @device_data: Structure for the hash device.
  322. *
  323. * This function check for an available hash device and return it to
  324. * the caller.
  325. * Note! Caller need to release the device, calling up().
  326. */
  327. static int hash_get_device_data(struct hash_ctx *ctx,
  328. struct hash_device_data **device_data)
  329. {
  330. int ret;
  331. struct klist_iter device_iterator;
  332. struct klist_node *device_node;
  333. struct hash_device_data *local_device_data = NULL;
  334. /* Wait until a device is available */
  335. ret = down_interruptible(&driver_data.device_allocation);
  336. if (ret)
  337. return ret; /* Interrupted */
  338. /* Select a device */
  339. klist_iter_init(&driver_data.device_list, &device_iterator);
  340. device_node = klist_next(&device_iterator);
  341. while (device_node) {
  342. local_device_data = container_of(device_node,
  343. struct hash_device_data, list_node);
  344. spin_lock(&local_device_data->ctx_lock);
  345. /* current_ctx allocates a device, NULL = unallocated */
  346. if (local_device_data->current_ctx) {
  347. device_node = klist_next(&device_iterator);
  348. } else {
  349. local_device_data->current_ctx = ctx;
  350. ctx->device = local_device_data;
  351. spin_unlock(&local_device_data->ctx_lock);
  352. break;
  353. }
  354. spin_unlock(&local_device_data->ctx_lock);
  355. }
  356. klist_iter_exit(&device_iterator);
  357. if (!device_node) {
  358. /**
  359. * No free device found.
  360. * Since we allocated a device with down_interruptible, this
  361. * should not be able to happen.
  362. * Number of available devices, which are contained in
  363. * device_allocation, is therefore decremented by not doing
  364. * an up(device_allocation).
  365. */
  366. return -EBUSY;
  367. }
  368. *device_data = local_device_data;
  369. return 0;
  370. }
  371. /**
  372. * hash_hw_write_key - Writes the key to the hardware registries.
  373. *
  374. * @device_data: Structure for the hash device.
  375. * @key: Key to be written.
  376. * @keylen: The lengt of the key.
  377. *
  378. * Note! This function DOES NOT write to the NBLW registry, even though
  379. * specified in the the hw design spec. Either due to incorrect info in the
  380. * spec or due to a bug in the hw.
  381. */
  382. static void hash_hw_write_key(struct hash_device_data *device_data,
  383. const u8 *key, unsigned int keylen)
  384. {
  385. u32 word = 0;
  386. int nwords = 1;
  387. HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
  388. while (keylen >= 4) {
  389. u32 *key_word = (u32 *)key;
  390. HASH_SET_DIN(key_word, nwords);
  391. keylen -= 4;
  392. key += 4;
  393. }
  394. /* Take care of the remaining bytes in the last word */
  395. if (keylen) {
  396. word = 0;
  397. while (keylen) {
  398. word |= (key[keylen - 1] << (8 * (keylen - 1)));
  399. keylen--;
  400. }
  401. HASH_SET_DIN(&word, nwords);
  402. }
  403. while (device_data->base->str & HASH_STR_DCAL_MASK)
  404. cpu_relax();
  405. HASH_SET_DCAL;
  406. while (device_data->base->str & HASH_STR_DCAL_MASK)
  407. cpu_relax();
  408. }
  409. /**
  410. * init_hash_hw - Initialise the hash hardware for a new calculation.
  411. * @device_data: Structure for the hash device.
  412. * @ctx: The hash context.
  413. *
  414. * This function will enable the bits needed to clear and start a new
  415. * calculation.
  416. */
  417. static int init_hash_hw(struct hash_device_data *device_data,
  418. struct hash_ctx *ctx)
  419. {
  420. int ret = 0;
  421. ret = hash_setconfiguration(device_data, &ctx->config);
  422. if (ret) {
  423. dev_err(device_data->dev, "[%s] hash_setconfiguration() "
  424. "failed!", __func__);
  425. return ret;
  426. }
  427. hash_begin(device_data, ctx);
  428. if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC)
  429. hash_hw_write_key(device_data, ctx->key, ctx->keylen);
  430. return ret;
  431. }
  432. /**
  433. * hash_get_nents - Return number of entries (nents) in scatterlist (sg).
  434. *
  435. * @sg: Scatterlist.
  436. * @size: Size in bytes.
  437. * @aligned: True if sg data aligned to work in DMA mode.
  438. *
  439. */
  440. static int hash_get_nents(struct scatterlist *sg, int size, bool *aligned)
  441. {
  442. int nents = 0;
  443. bool aligned_data = true;
  444. while (size > 0 && sg) {
  445. nents++;
  446. size -= sg->length;
  447. /* hash_set_dma_transfer will align last nent */
  448. if ((aligned && !IS_ALIGNED(sg->offset, HASH_DMA_ALIGN_SIZE))
  449. || (!IS_ALIGNED(sg->length, HASH_DMA_ALIGN_SIZE) &&
  450. size > 0))
  451. aligned_data = false;
  452. sg = sg_next(sg);
  453. }
  454. if (aligned)
  455. *aligned = aligned_data;
  456. if (size != 0)
  457. return -EFAULT;
  458. return nents;
  459. }
  460. /**
  461. * hash_dma_valid_data - checks for dma valid sg data.
  462. * @sg: Scatterlist.
  463. * @datasize: Datasize in bytes.
  464. *
  465. * NOTE! This function checks for dma valid sg data, since dma
  466. * only accept datasizes of even wordsize.
  467. */
  468. static bool hash_dma_valid_data(struct scatterlist *sg, int datasize)
  469. {
  470. bool aligned;
  471. /* Need to include at least one nent, else error */
  472. if (hash_get_nents(sg, datasize, &aligned) < 1)
  473. return false;
  474. return aligned;
  475. }
  476. /**
  477. * hash_init - Common hash init function for SHA1/SHA2 (SHA256).
  478. * @req: The hash request for the job.
  479. *
  480. * Initialize structures.
  481. */
  482. static int hash_init(struct ahash_request *req)
  483. {
  484. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  485. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  486. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  487. if (!ctx->key)
  488. ctx->keylen = 0;
  489. memset(&req_ctx->state, 0, sizeof(struct hash_state));
  490. req_ctx->updated = 0;
  491. if (hash_mode == HASH_MODE_DMA) {
  492. if (req->nbytes < HASH_DMA_ALIGN_SIZE) {
  493. req_ctx->dma_mode = false; /* Don't use DMA */
  494. pr_debug(DEV_DBG_NAME " [%s] DMA mode, but direct "
  495. "to CPU mode for data size < %d",
  496. __func__, HASH_DMA_ALIGN_SIZE);
  497. } else {
  498. if (req->nbytes >= HASH_DMA_PERFORMANCE_MIN_SIZE &&
  499. hash_dma_valid_data(req->src,
  500. req->nbytes)) {
  501. req_ctx->dma_mode = true;
  502. } else {
  503. req_ctx->dma_mode = false;
  504. pr_debug(DEV_DBG_NAME " [%s] DMA mode, but use"
  505. " CPU mode for datalength < %d"
  506. " or non-aligned data, except "
  507. "in last nent", __func__,
  508. HASH_DMA_PERFORMANCE_MIN_SIZE);
  509. }
  510. }
  511. }
  512. return 0;
  513. }
  514. /**
  515. * hash_processblock - This function processes a single block of 512 bits (64
  516. * bytes), word aligned, starting at message.
  517. * @device_data: Structure for the hash device.
  518. * @message: Block (512 bits) of message to be written to
  519. * the HASH hardware.
  520. *
  521. */
  522. static void hash_processblock(
  523. struct hash_device_data *device_data,
  524. const u32 *message, int length)
  525. {
  526. int len = length / HASH_BYTES_PER_WORD;
  527. /*
  528. * NBLW bits. Reset the number of bits in last word (NBLW).
  529. */
  530. HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
  531. /*
  532. * Write message data to the HASH_DIN register.
  533. */
  534. HASH_SET_DIN(message, len);
  535. }
  536. /**
  537. * hash_messagepad - Pads a message and write the nblw bits.
  538. * @device_data: Structure for the hash device.
  539. * @message: Last word of a message.
  540. * @index_bytes: The number of bytes in the last message.
  541. *
  542. * This function manages the final part of the digest calculation, when less
  543. * than 512 bits (64 bytes) remain in message. This means index_bytes < 64.
  544. *
  545. */
  546. static void hash_messagepad(struct hash_device_data *device_data,
  547. const u32 *message, u8 index_bytes)
  548. {
  549. int nwords = 1;
  550. /*
  551. * Clear hash str register, only clear NBLW
  552. * since DCAL will be reset by hardware.
  553. */
  554. HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
  555. /* Main loop */
  556. while (index_bytes >= 4) {
  557. HASH_SET_DIN(message, nwords);
  558. index_bytes -= 4;
  559. message++;
  560. }
  561. if (index_bytes)
  562. HASH_SET_DIN(message, nwords);
  563. while (device_data->base->str & HASH_STR_DCAL_MASK)
  564. cpu_relax();
  565. /* num_of_bytes == 0 => NBLW <- 0 (32 bits valid in DATAIN) */
  566. HASH_SET_NBLW(index_bytes * 8);
  567. dev_dbg(device_data->dev, "[%s] DIN=0x%08x NBLW=%d", __func__,
  568. readl_relaxed(&device_data->base->din),
  569. (int)(readl_relaxed(&device_data->base->str) &
  570. HASH_STR_NBLW_MASK));
  571. HASH_SET_DCAL;
  572. dev_dbg(device_data->dev, "[%s] after dcal -> DIN=0x%08x NBLW=%d",
  573. __func__, readl_relaxed(&device_data->base->din),
  574. (int)(readl_relaxed(&device_data->base->str) &
  575. HASH_STR_NBLW_MASK));
  576. while (device_data->base->str & HASH_STR_DCAL_MASK)
  577. cpu_relax();
  578. }
  579. /**
  580. * hash_incrementlength - Increments the length of the current message.
  581. * @ctx: Hash context
  582. * @incr: Length of message processed already
  583. *
  584. * Overflow cannot occur, because conditions for overflow are checked in
  585. * hash_hw_update.
  586. */
  587. static void hash_incrementlength(struct hash_req_ctx *ctx, u32 incr)
  588. {
  589. ctx->state.length.low_word += incr;
  590. /* Check for wrap-around */
  591. if (ctx->state.length.low_word < incr)
  592. ctx->state.length.high_word++;
  593. }
  594. /**
  595. * hash_setconfiguration - Sets the required configuration for the hash
  596. * hardware.
  597. * @device_data: Structure for the hash device.
  598. * @config: Pointer to a configuration structure.
  599. */
  600. int hash_setconfiguration(struct hash_device_data *device_data,
  601. struct hash_config *config)
  602. {
  603. int ret = 0;
  604. if (config->algorithm != HASH_ALGO_SHA1 &&
  605. config->algorithm != HASH_ALGO_SHA256)
  606. return -EPERM;
  607. /*
  608. * DATAFORM bits. Set the DATAFORM bits to 0b11, which means the data
  609. * to be written to HASH_DIN is considered as 32 bits.
  610. */
  611. HASH_SET_DATA_FORMAT(config->data_format);
  612. /*
  613. * ALGO bit. Set to 0b1 for SHA-1 and 0b0 for SHA-256
  614. */
  615. switch (config->algorithm) {
  616. case HASH_ALGO_SHA1:
  617. HASH_SET_BITS(&device_data->base->cr, HASH_CR_ALGO_MASK);
  618. break;
  619. case HASH_ALGO_SHA256:
  620. HASH_CLEAR_BITS(&device_data->base->cr, HASH_CR_ALGO_MASK);
  621. break;
  622. default:
  623. dev_err(device_data->dev, "[%s] Incorrect algorithm.",
  624. __func__);
  625. return -EPERM;
  626. }
  627. /*
  628. * MODE bit. This bit selects between HASH or HMAC mode for the
  629. * selected algorithm. 0b0 = HASH and 0b1 = HMAC.
  630. */
  631. if (HASH_OPER_MODE_HASH == config->oper_mode)
  632. HASH_CLEAR_BITS(&device_data->base->cr,
  633. HASH_CR_MODE_MASK);
  634. else if (HASH_OPER_MODE_HMAC == config->oper_mode) {
  635. HASH_SET_BITS(&device_data->base->cr,
  636. HASH_CR_MODE_MASK);
  637. if (device_data->current_ctx->keylen > HASH_BLOCK_SIZE) {
  638. /* Truncate key to blocksize */
  639. dev_dbg(device_data->dev, "[%s] LKEY set", __func__);
  640. HASH_SET_BITS(&device_data->base->cr,
  641. HASH_CR_LKEY_MASK);
  642. } else {
  643. dev_dbg(device_data->dev, "[%s] LKEY cleared",
  644. __func__);
  645. HASH_CLEAR_BITS(&device_data->base->cr,
  646. HASH_CR_LKEY_MASK);
  647. }
  648. } else { /* Wrong hash mode */
  649. ret = -EPERM;
  650. dev_err(device_data->dev, "[%s] HASH_INVALID_PARAMETER!",
  651. __func__);
  652. }
  653. return ret;
  654. }
  655. /**
  656. * hash_begin - This routine resets some globals and initializes the hash
  657. * hardware.
  658. * @device_data: Structure for the hash device.
  659. * @ctx: Hash context.
  660. */
  661. void hash_begin(struct hash_device_data *device_data, struct hash_ctx *ctx)
  662. {
  663. /* HW and SW initializations */
  664. /* Note: there is no need to initialize buffer and digest members */
  665. while (device_data->base->str & HASH_STR_DCAL_MASK)
  666. cpu_relax();
  667. /*
  668. * INIT bit. Set this bit to 0b1 to reset the HASH processor core and
  669. * prepare the initialize the HASH accelerator to compute the message
  670. * digest of a new message.
  671. */
  672. HASH_INITIALIZE;
  673. /*
  674. * NBLW bits. Reset the number of bits in last word (NBLW).
  675. */
  676. HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
  677. }
  678. int hash_process_data(
  679. struct hash_device_data *device_data,
  680. struct hash_ctx *ctx, struct hash_req_ctx *req_ctx,
  681. int msg_length, u8 *data_buffer, u8 *buffer, u8 *index)
  682. {
  683. int ret = 0;
  684. u32 count;
  685. do {
  686. if ((*index + msg_length) < HASH_BLOCK_SIZE) {
  687. for (count = 0; count < msg_length; count++) {
  688. buffer[*index + count] =
  689. *(data_buffer + count);
  690. }
  691. *index += msg_length;
  692. msg_length = 0;
  693. } else {
  694. if (req_ctx->updated) {
  695. ret = hash_resume_state(device_data,
  696. &device_data->state);
  697. memmove(req_ctx->state.buffer,
  698. device_data->state.buffer,
  699. HASH_BLOCK_SIZE / sizeof(u32));
  700. if (ret) {
  701. dev_err(device_data->dev, "[%s] "
  702. "hash_resume_state()"
  703. " failed!", __func__);
  704. goto out;
  705. }
  706. } else {
  707. ret = init_hash_hw(device_data, ctx);
  708. if (ret) {
  709. dev_err(device_data->dev, "[%s] "
  710. "init_hash_hw()"
  711. " failed!", __func__);
  712. goto out;
  713. }
  714. req_ctx->updated = 1;
  715. }
  716. /*
  717. * If 'data_buffer' is four byte aligned and
  718. * local buffer does not have any data, we can
  719. * write data directly from 'data_buffer' to
  720. * HW peripheral, otherwise we first copy data
  721. * to a local buffer
  722. */
  723. if ((0 == (((u32)data_buffer) % 4))
  724. && (0 == *index))
  725. hash_processblock(device_data,
  726. (const u32 *)
  727. data_buffer, HASH_BLOCK_SIZE);
  728. else {
  729. for (count = 0; count <
  730. (u32)(HASH_BLOCK_SIZE -
  731. *index);
  732. count++) {
  733. buffer[*index + count] =
  734. *(data_buffer + count);
  735. }
  736. hash_processblock(device_data,
  737. (const u32 *)buffer,
  738. HASH_BLOCK_SIZE);
  739. }
  740. hash_incrementlength(req_ctx, HASH_BLOCK_SIZE);
  741. data_buffer += (HASH_BLOCK_SIZE - *index);
  742. msg_length -= (HASH_BLOCK_SIZE - *index);
  743. *index = 0;
  744. ret = hash_save_state(device_data,
  745. &device_data->state);
  746. memmove(device_data->state.buffer,
  747. req_ctx->state.buffer,
  748. HASH_BLOCK_SIZE / sizeof(u32));
  749. if (ret) {
  750. dev_err(device_data->dev, "[%s] "
  751. "hash_save_state()"
  752. " failed!", __func__);
  753. goto out;
  754. }
  755. }
  756. } while (msg_length != 0);
  757. out:
  758. return ret;
  759. }
  760. /**
  761. * hash_dma_final - The hash dma final function for SHA1/SHA256.
  762. * @req: The hash request for the job.
  763. */
  764. static int hash_dma_final(struct ahash_request *req)
  765. {
  766. int ret = 0;
  767. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  768. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  769. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  770. struct hash_device_data *device_data;
  771. u8 digest[SHA256_DIGEST_SIZE];
  772. int bytes_written = 0;
  773. ret = hash_get_device_data(ctx, &device_data);
  774. if (ret)
  775. return ret;
  776. dev_dbg(device_data->dev, "[%s] (ctx=0x%x)!", __func__, (u32) ctx);
  777. if (req_ctx->updated) {
  778. ret = hash_resume_state(device_data, &device_data->state);
  779. if (ret) {
  780. dev_err(device_data->dev, "[%s] hash_resume_state() "
  781. "failed!", __func__);
  782. goto out;
  783. }
  784. }
  785. if (!req_ctx->updated) {
  786. ret = hash_setconfiguration(device_data, &ctx->config);
  787. if (ret) {
  788. dev_err(device_data->dev, "[%s] "
  789. "hash_setconfiguration() failed!",
  790. __func__);
  791. goto out;
  792. }
  793. /* Enable DMA input */
  794. if (hash_mode != HASH_MODE_DMA || !req_ctx->dma_mode) {
  795. HASH_CLEAR_BITS(&device_data->base->cr,
  796. HASH_CR_DMAE_MASK);
  797. } else {
  798. HASH_SET_BITS(&device_data->base->cr,
  799. HASH_CR_DMAE_MASK);
  800. HASH_SET_BITS(&device_data->base->cr,
  801. HASH_CR_PRIVN_MASK);
  802. }
  803. HASH_INITIALIZE;
  804. if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC)
  805. hash_hw_write_key(device_data, ctx->key, ctx->keylen);
  806. /* Number of bits in last word = (nbytes * 8) % 32 */
  807. HASH_SET_NBLW((req->nbytes * 8) % 32);
  808. req_ctx->updated = 1;
  809. }
  810. /* Store the nents in the dma struct. */
  811. ctx->device->dma.nents = hash_get_nents(req->src, req->nbytes, NULL);
  812. if (!ctx->device->dma.nents) {
  813. dev_err(device_data->dev, "[%s] "
  814. "ctx->device->dma.nents = 0", __func__);
  815. ret = ctx->device->dma.nents;
  816. goto out;
  817. }
  818. bytes_written = hash_dma_write(ctx, req->src, req->nbytes);
  819. if (bytes_written != req->nbytes) {
  820. dev_err(device_data->dev, "[%s] "
  821. "hash_dma_write() failed!", __func__);
  822. ret = bytes_written;
  823. goto out;
  824. }
  825. wait_for_completion(&ctx->device->dma.complete);
  826. hash_dma_done(ctx);
  827. while (device_data->base->str & HASH_STR_DCAL_MASK)
  828. cpu_relax();
  829. if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC && ctx->key) {
  830. unsigned int keylen = ctx->keylen;
  831. u8 *key = ctx->key;
  832. dev_dbg(device_data->dev, "[%s] keylen: %d", __func__,
  833. ctx->keylen);
  834. hash_hw_write_key(device_data, key, keylen);
  835. }
  836. hash_get_digest(device_data, digest, ctx->config.algorithm);
  837. memcpy(req->result, digest, ctx->digestsize);
  838. out:
  839. release_hash_device(device_data);
  840. /**
  841. * Allocated in setkey, and only used in HMAC.
  842. */
  843. kfree(ctx->key);
  844. return ret;
  845. }
  846. /**
  847. * hash_hw_final - The final hash calculation function
  848. * @req: The hash request for the job.
  849. */
  850. int hash_hw_final(struct ahash_request *req)
  851. {
  852. int ret = 0;
  853. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  854. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  855. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  856. struct hash_device_data *device_data;
  857. u8 digest[SHA256_DIGEST_SIZE];
  858. ret = hash_get_device_data(ctx, &device_data);
  859. if (ret)
  860. return ret;
  861. dev_dbg(device_data->dev, "[%s] (ctx=0x%x)!", __func__, (u32) ctx);
  862. if (req_ctx->updated) {
  863. ret = hash_resume_state(device_data, &device_data->state);
  864. if (ret) {
  865. dev_err(device_data->dev, "[%s] hash_resume_state() "
  866. "failed!", __func__);
  867. goto out;
  868. }
  869. } else if (req->nbytes == 0 && ctx->keylen == 0) {
  870. u8 zero_hash[SHA256_DIGEST_SIZE];
  871. u32 zero_hash_size = 0;
  872. bool zero_digest = false;
  873. /**
  874. * Use a pre-calculated empty message digest
  875. * (workaround since hw return zeroes, hw bug!?)
  876. */
  877. ret = get_empty_message_digest(device_data, &zero_hash[0],
  878. &zero_hash_size, &zero_digest);
  879. if (!ret && likely(zero_hash_size == ctx->digestsize) &&
  880. zero_digest) {
  881. memcpy(req->result, &zero_hash[0], ctx->digestsize);
  882. goto out;
  883. } else if (!ret && !zero_digest) {
  884. dev_dbg(device_data->dev, "[%s] HMAC zero msg with "
  885. "key, continue...", __func__);
  886. } else {
  887. dev_err(device_data->dev, "[%s] ret=%d, or wrong "
  888. "digest size? %s", __func__, ret,
  889. (zero_hash_size == ctx->digestsize) ?
  890. "true" : "false");
  891. /* Return error */
  892. goto out;
  893. }
  894. } else if (req->nbytes == 0 && ctx->keylen > 0) {
  895. dev_err(device_data->dev, "[%s] Empty message with "
  896. "keylength > 0, NOT supported.", __func__);
  897. goto out;
  898. }
  899. if (!req_ctx->updated) {
  900. ret = init_hash_hw(device_data, ctx);
  901. if (ret) {
  902. dev_err(device_data->dev, "[%s] init_hash_hw() "
  903. "failed!", __func__);
  904. goto out;
  905. }
  906. }
  907. if (req_ctx->state.index) {
  908. hash_messagepad(device_data, req_ctx->state.buffer,
  909. req_ctx->state.index);
  910. } else {
  911. HASH_SET_DCAL;
  912. while (device_data->base->str & HASH_STR_DCAL_MASK)
  913. cpu_relax();
  914. }
  915. if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC && ctx->key) {
  916. unsigned int keylen = ctx->keylen;
  917. u8 *key = ctx->key;
  918. dev_dbg(device_data->dev, "[%s] keylen: %d", __func__,
  919. ctx->keylen);
  920. hash_hw_write_key(device_data, key, keylen);
  921. }
  922. hash_get_digest(device_data, digest, ctx->config.algorithm);
  923. memcpy(req->result, digest, ctx->digestsize);
  924. out:
  925. release_hash_device(device_data);
  926. /**
  927. * Allocated in setkey, and only used in HMAC.
  928. */
  929. kfree(ctx->key);
  930. return ret;
  931. }
  932. /**
  933. * hash_hw_update - Updates current HASH computation hashing another part of
  934. * the message.
  935. * @req: Byte array containing the message to be hashed (caller
  936. * allocated).
  937. */
  938. int hash_hw_update(struct ahash_request *req)
  939. {
  940. int ret = 0;
  941. u8 index = 0;
  942. u8 *buffer;
  943. struct hash_device_data *device_data;
  944. u8 *data_buffer;
  945. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  946. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  947. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  948. struct crypto_hash_walk walk;
  949. int msg_length = crypto_hash_walk_first(req, &walk);
  950. /* Empty message ("") is correct indata */
  951. if (msg_length == 0)
  952. return ret;
  953. index = req_ctx->state.index;
  954. buffer = (u8 *)req_ctx->state.buffer;
  955. /* Check if ctx->state.length + msg_length
  956. overflows */
  957. if (msg_length > (req_ctx->state.length.low_word + msg_length) &&
  958. HASH_HIGH_WORD_MAX_VAL ==
  959. req_ctx->state.length.high_word) {
  960. pr_err(DEV_DBG_NAME " [%s] HASH_MSG_LENGTH_OVERFLOW!",
  961. __func__);
  962. return -EPERM;
  963. }
  964. ret = hash_get_device_data(ctx, &device_data);
  965. if (ret)
  966. return ret;
  967. /* Main loop */
  968. while (0 != msg_length) {
  969. data_buffer = walk.data;
  970. ret = hash_process_data(device_data, ctx, req_ctx, msg_length,
  971. data_buffer, buffer, &index);
  972. if (ret) {
  973. dev_err(device_data->dev, "[%s] hash_internal_hw_"
  974. "update() failed!", __func__);
  975. goto out;
  976. }
  977. msg_length = crypto_hash_walk_done(&walk, 0);
  978. }
  979. req_ctx->state.index = index;
  980. dev_dbg(device_data->dev, "[%s] indata length=%d, bin=%d))",
  981. __func__, req_ctx->state.index,
  982. req_ctx->state.bit_index);
  983. out:
  984. release_hash_device(device_data);
  985. return ret;
  986. }
  987. /**
  988. * hash_resume_state - Function that resumes the state of an calculation.
  989. * @device_data: Pointer to the device structure.
  990. * @device_state: The state to be restored in the hash hardware
  991. */
  992. int hash_resume_state(struct hash_device_data *device_data,
  993. const struct hash_state *device_state)
  994. {
  995. u32 temp_cr;
  996. s32 count;
  997. int hash_mode = HASH_OPER_MODE_HASH;
  998. if (NULL == device_state) {
  999. dev_err(device_data->dev, "[%s] HASH_INVALID_PARAMETER!",
  1000. __func__);
  1001. return -EPERM;
  1002. }
  1003. /* Check correctness of index and length members */
  1004. if (device_state->index > HASH_BLOCK_SIZE
  1005. || (device_state->length.low_word % HASH_BLOCK_SIZE) != 0) {
  1006. dev_err(device_data->dev, "[%s] HASH_INVALID_PARAMETER!",
  1007. __func__);
  1008. return -EPERM;
  1009. }
  1010. /*
  1011. * INIT bit. Set this bit to 0b1 to reset the HASH processor core and
  1012. * prepare the initialize the HASH accelerator to compute the message
  1013. * digest of a new message.
  1014. */
  1015. HASH_INITIALIZE;
  1016. temp_cr = device_state->temp_cr;
  1017. writel_relaxed(temp_cr & HASH_CR_RESUME_MASK, &device_data->base->cr);
  1018. if (device_data->base->cr & HASH_CR_MODE_MASK)
  1019. hash_mode = HASH_OPER_MODE_HMAC;
  1020. else
  1021. hash_mode = HASH_OPER_MODE_HASH;
  1022. for (count = 0; count < HASH_CSR_COUNT; count++) {
  1023. if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH))
  1024. break;
  1025. writel_relaxed(device_state->csr[count],
  1026. &device_data->base->csrx[count]);
  1027. }
  1028. writel_relaxed(device_state->csfull, &device_data->base->csfull);
  1029. writel_relaxed(device_state->csdatain, &device_data->base->csdatain);
  1030. writel_relaxed(device_state->str_reg, &device_data->base->str);
  1031. writel_relaxed(temp_cr, &device_data->base->cr);
  1032. return 0;
  1033. }
  1034. /**
  1035. * hash_save_state - Function that saves the state of hardware.
  1036. * @device_data: Pointer to the device structure.
  1037. * @device_state: The strucure where the hardware state should be saved.
  1038. */
  1039. int hash_save_state(struct hash_device_data *device_data,
  1040. struct hash_state *device_state)
  1041. {
  1042. u32 temp_cr;
  1043. u32 count;
  1044. int hash_mode = HASH_OPER_MODE_HASH;
  1045. if (NULL == device_state) {
  1046. dev_err(device_data->dev, "[%s] HASH_INVALID_PARAMETER!",
  1047. __func__);
  1048. return -ENOTSUPP;
  1049. }
  1050. /* Write dummy value to force digest intermediate calculation. This
  1051. * actually makes sure that there isn't any ongoing calculation in the
  1052. * hardware.
  1053. */
  1054. while (device_data->base->str & HASH_STR_DCAL_MASK)
  1055. cpu_relax();
  1056. temp_cr = readl_relaxed(&device_data->base->cr);
  1057. device_state->str_reg = readl_relaxed(&device_data->base->str);
  1058. device_state->din_reg = readl_relaxed(&device_data->base->din);
  1059. if (device_data->base->cr & HASH_CR_MODE_MASK)
  1060. hash_mode = HASH_OPER_MODE_HMAC;
  1061. else
  1062. hash_mode = HASH_OPER_MODE_HASH;
  1063. for (count = 0; count < HASH_CSR_COUNT; count++) {
  1064. if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH))
  1065. break;
  1066. device_state->csr[count] =
  1067. readl_relaxed(&device_data->base->csrx[count]);
  1068. }
  1069. device_state->csfull = readl_relaxed(&device_data->base->csfull);
  1070. device_state->csdatain = readl_relaxed(&device_data->base->csdatain);
  1071. device_state->temp_cr = temp_cr;
  1072. return 0;
  1073. }
  1074. /**
  1075. * hash_check_hw - This routine checks for peripheral Ids and PCell Ids.
  1076. * @device_data:
  1077. *
  1078. */
  1079. int hash_check_hw(struct hash_device_data *device_data)
  1080. {
  1081. /* Checking Peripheral Ids */
  1082. if (HASH_P_ID0 == readl_relaxed(&device_data->base->periphid0)
  1083. && HASH_P_ID1 == readl_relaxed(&device_data->base->periphid1)
  1084. && HASH_P_ID2 == readl_relaxed(&device_data->base->periphid2)
  1085. && HASH_P_ID3 == readl_relaxed(&device_data->base->periphid3)
  1086. && HASH_CELL_ID0 == readl_relaxed(&device_data->base->cellid0)
  1087. && HASH_CELL_ID1 == readl_relaxed(&device_data->base->cellid1)
  1088. && HASH_CELL_ID2 == readl_relaxed(&device_data->base->cellid2)
  1089. && HASH_CELL_ID3 == readl_relaxed(&device_data->base->cellid3)
  1090. ) {
  1091. return 0;
  1092. }
  1093. dev_err(device_data->dev, "[%s] HASH_UNSUPPORTED_HW!",
  1094. __func__);
  1095. return -ENOTSUPP;
  1096. }
  1097. /**
  1098. * hash_get_digest - Gets the digest.
  1099. * @device_data: Pointer to the device structure.
  1100. * @digest: User allocated byte array for the calculated digest.
  1101. * @algorithm: The algorithm in use.
  1102. */
  1103. void hash_get_digest(struct hash_device_data *device_data,
  1104. u8 *digest, int algorithm)
  1105. {
  1106. u32 temp_hx_val, count;
  1107. int loop_ctr;
  1108. if (algorithm != HASH_ALGO_SHA1 && algorithm != HASH_ALGO_SHA256) {
  1109. dev_err(device_data->dev, "[%s] Incorrect algorithm %d",
  1110. __func__, algorithm);
  1111. return;
  1112. }
  1113. if (algorithm == HASH_ALGO_SHA1)
  1114. loop_ctr = SHA1_DIGEST_SIZE / sizeof(u32);
  1115. else
  1116. loop_ctr = SHA256_DIGEST_SIZE / sizeof(u32);
  1117. dev_dbg(device_data->dev, "[%s] digest array:(0x%x)",
  1118. __func__, (u32) digest);
  1119. /* Copy result into digest array */
  1120. for (count = 0; count < loop_ctr; count++) {
  1121. temp_hx_val = readl_relaxed(&device_data->base->hx[count]);
  1122. digest[count * 4] = (u8) ((temp_hx_val >> 24) & 0xFF);
  1123. digest[count * 4 + 1] = (u8) ((temp_hx_val >> 16) & 0xFF);
  1124. digest[count * 4 + 2] = (u8) ((temp_hx_val >> 8) & 0xFF);
  1125. digest[count * 4 + 3] = (u8) ((temp_hx_val >> 0) & 0xFF);
  1126. }
  1127. }
  1128. /**
  1129. * hash_update - The hash update function for SHA1/SHA2 (SHA256).
  1130. * @req: The hash request for the job.
  1131. */
  1132. static int ahash_update(struct ahash_request *req)
  1133. {
  1134. int ret = 0;
  1135. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  1136. if (hash_mode != HASH_MODE_DMA || !req_ctx->dma_mode)
  1137. ret = hash_hw_update(req);
  1138. /* Skip update for DMA, all data will be passed to DMA in final */
  1139. if (ret) {
  1140. pr_err(DEV_DBG_NAME " [%s] hash_hw_update() failed!",
  1141. __func__);
  1142. }
  1143. return ret;
  1144. }
  1145. /**
  1146. * hash_final - The hash final function for SHA1/SHA2 (SHA256).
  1147. * @req: The hash request for the job.
  1148. */
  1149. static int ahash_final(struct ahash_request *req)
  1150. {
  1151. int ret = 0;
  1152. struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
  1153. pr_debug(DEV_DBG_NAME " [%s] data size: %d", __func__, req->nbytes);
  1154. if ((hash_mode == HASH_MODE_DMA) && req_ctx->dma_mode)
  1155. ret = hash_dma_final(req);
  1156. else
  1157. ret = hash_hw_final(req);
  1158. if (ret) {
  1159. pr_err(DEV_DBG_NAME " [%s] hash_hw/dma_final() failed",
  1160. __func__);
  1161. }
  1162. return ret;
  1163. }
  1164. static int hash_setkey(struct crypto_ahash *tfm,
  1165. const u8 *key, unsigned int keylen, int alg)
  1166. {
  1167. int ret = 0;
  1168. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1169. /**
  1170. * Freed in final.
  1171. */
  1172. ctx->key = kmemdup(key, keylen, GFP_KERNEL);
  1173. if (!ctx->key) {
  1174. pr_err(DEV_DBG_NAME " [%s] Failed to allocate ctx->key "
  1175. "for %d\n", __func__, alg);
  1176. return -ENOMEM;
  1177. }
  1178. ctx->keylen = keylen;
  1179. return ret;
  1180. }
  1181. static int ahash_sha1_init(struct ahash_request *req)
  1182. {
  1183. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1184. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1185. ctx->config.data_format = HASH_DATA_8_BITS;
  1186. ctx->config.algorithm = HASH_ALGO_SHA1;
  1187. ctx->config.oper_mode = HASH_OPER_MODE_HASH;
  1188. ctx->digestsize = SHA1_DIGEST_SIZE;
  1189. return hash_init(req);
  1190. }
  1191. static int ahash_sha256_init(struct ahash_request *req)
  1192. {
  1193. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1194. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1195. ctx->config.data_format = HASH_DATA_8_BITS;
  1196. ctx->config.algorithm = HASH_ALGO_SHA256;
  1197. ctx->config.oper_mode = HASH_OPER_MODE_HASH;
  1198. ctx->digestsize = SHA256_DIGEST_SIZE;
  1199. return hash_init(req);
  1200. }
  1201. static int ahash_sha1_digest(struct ahash_request *req)
  1202. {
  1203. int ret2, ret1;
  1204. ret1 = ahash_sha1_init(req);
  1205. if (ret1)
  1206. goto out;
  1207. ret1 = ahash_update(req);
  1208. ret2 = ahash_final(req);
  1209. out:
  1210. return ret1 ? ret1 : ret2;
  1211. }
  1212. static int ahash_sha256_digest(struct ahash_request *req)
  1213. {
  1214. int ret2, ret1;
  1215. ret1 = ahash_sha256_init(req);
  1216. if (ret1)
  1217. goto out;
  1218. ret1 = ahash_update(req);
  1219. ret2 = ahash_final(req);
  1220. out:
  1221. return ret1 ? ret1 : ret2;
  1222. }
  1223. static int hmac_sha1_init(struct ahash_request *req)
  1224. {
  1225. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1226. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1227. ctx->config.data_format = HASH_DATA_8_BITS;
  1228. ctx->config.algorithm = HASH_ALGO_SHA1;
  1229. ctx->config.oper_mode = HASH_OPER_MODE_HMAC;
  1230. ctx->digestsize = SHA1_DIGEST_SIZE;
  1231. return hash_init(req);
  1232. }
  1233. static int hmac_sha256_init(struct ahash_request *req)
  1234. {
  1235. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1236. struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1237. ctx->config.data_format = HASH_DATA_8_BITS;
  1238. ctx->config.algorithm = HASH_ALGO_SHA256;
  1239. ctx->config.oper_mode = HASH_OPER_MODE_HMAC;
  1240. ctx->digestsize = SHA256_DIGEST_SIZE;
  1241. return hash_init(req);
  1242. }
  1243. static int hmac_sha1_digest(struct ahash_request *req)
  1244. {
  1245. int ret2, ret1;
  1246. ret1 = hmac_sha1_init(req);
  1247. if (ret1)
  1248. goto out;
  1249. ret1 = ahash_update(req);
  1250. ret2 = ahash_final(req);
  1251. out:
  1252. return ret1 ? ret1 : ret2;
  1253. }
  1254. static int hmac_sha256_digest(struct ahash_request *req)
  1255. {
  1256. int ret2, ret1;
  1257. ret1 = hmac_sha256_init(req);
  1258. if (ret1)
  1259. goto out;
  1260. ret1 = ahash_update(req);
  1261. ret2 = ahash_final(req);
  1262. out:
  1263. return ret1 ? ret1 : ret2;
  1264. }
  1265. static int hmac_sha1_setkey(struct crypto_ahash *tfm,
  1266. const u8 *key, unsigned int keylen)
  1267. {
  1268. return hash_setkey(tfm, key, keylen, HASH_ALGO_SHA1);
  1269. }
  1270. static int hmac_sha256_setkey(struct crypto_ahash *tfm,
  1271. const u8 *key, unsigned int keylen)
  1272. {
  1273. return hash_setkey(tfm, key, keylen, HASH_ALGO_SHA256);
  1274. }
  1275. struct hash_algo_template {
  1276. struct hash_config conf;
  1277. struct ahash_alg hash;
  1278. };
  1279. static int hash_cra_init(struct crypto_tfm *tfm)
  1280. {
  1281. struct hash_ctx *ctx = crypto_tfm_ctx(tfm);
  1282. struct crypto_alg *alg = tfm->__crt_alg;
  1283. struct hash_algo_template *hash_alg;
  1284. hash_alg = container_of(__crypto_ahash_alg(alg),
  1285. struct hash_algo_template,
  1286. hash);
  1287. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1288. sizeof(struct hash_req_ctx));
  1289. ctx->config.data_format = HASH_DATA_8_BITS;
  1290. ctx->config.algorithm = hash_alg->conf.algorithm;
  1291. ctx->config.oper_mode = hash_alg->conf.oper_mode;
  1292. ctx->digestsize = hash_alg->hash.halg.digestsize;
  1293. return 0;
  1294. }
  1295. static struct hash_algo_template hash_algs[] = {
  1296. {
  1297. .conf.algorithm = HASH_ALGO_SHA1,
  1298. .conf.oper_mode = HASH_OPER_MODE_HASH,
  1299. .hash = {
  1300. .init = hash_init,
  1301. .update = ahash_update,
  1302. .final = ahash_final,
  1303. .digest = ahash_sha1_digest,
  1304. .halg.digestsize = SHA1_DIGEST_SIZE,
  1305. .halg.statesize = sizeof(struct hash_ctx),
  1306. .halg.base = {
  1307. .cra_name = "sha1",
  1308. .cra_driver_name = "sha1-ux500",
  1309. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1310. CRYPTO_ALG_ASYNC,
  1311. .cra_blocksize = SHA1_BLOCK_SIZE,
  1312. .cra_ctxsize = sizeof(struct hash_ctx),
  1313. .cra_init = hash_cra_init,
  1314. .cra_module = THIS_MODULE,
  1315. }
  1316. }
  1317. },
  1318. {
  1319. .conf.algorithm = HASH_ALGO_SHA256,
  1320. .conf.oper_mode = HASH_OPER_MODE_HASH,
  1321. .hash = {
  1322. .init = hash_init,
  1323. .update = ahash_update,
  1324. .final = ahash_final,
  1325. .digest = ahash_sha256_digest,
  1326. .halg.digestsize = SHA256_DIGEST_SIZE,
  1327. .halg.statesize = sizeof(struct hash_ctx),
  1328. .halg.base = {
  1329. .cra_name = "sha256",
  1330. .cra_driver_name = "sha256-ux500",
  1331. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1332. CRYPTO_ALG_ASYNC,
  1333. .cra_blocksize = SHA256_BLOCK_SIZE,
  1334. .cra_ctxsize = sizeof(struct hash_ctx),
  1335. .cra_type = &crypto_ahash_type,
  1336. .cra_init = hash_cra_init,
  1337. .cra_module = THIS_MODULE,
  1338. }
  1339. }
  1340. },
  1341. {
  1342. .conf.algorithm = HASH_ALGO_SHA1,
  1343. .conf.oper_mode = HASH_OPER_MODE_HMAC,
  1344. .hash = {
  1345. .init = hash_init,
  1346. .update = ahash_update,
  1347. .final = ahash_final,
  1348. .digest = hmac_sha1_digest,
  1349. .setkey = hmac_sha1_setkey,
  1350. .halg.digestsize = SHA1_DIGEST_SIZE,
  1351. .halg.statesize = sizeof(struct hash_ctx),
  1352. .halg.base = {
  1353. .cra_name = "hmac(sha1)",
  1354. .cra_driver_name = "hmac-sha1-ux500",
  1355. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1356. CRYPTO_ALG_ASYNC,
  1357. .cra_blocksize = SHA1_BLOCK_SIZE,
  1358. .cra_ctxsize = sizeof(struct hash_ctx),
  1359. .cra_type = &crypto_ahash_type,
  1360. .cra_init = hash_cra_init,
  1361. .cra_module = THIS_MODULE,
  1362. }
  1363. }
  1364. },
  1365. {
  1366. .conf.algorithm = HASH_ALGO_SHA256,
  1367. .conf.oper_mode = HASH_OPER_MODE_HMAC,
  1368. .hash = {
  1369. .init = hash_init,
  1370. .update = ahash_update,
  1371. .final = ahash_final,
  1372. .digest = hmac_sha256_digest,
  1373. .setkey = hmac_sha256_setkey,
  1374. .halg.digestsize = SHA256_DIGEST_SIZE,
  1375. .halg.statesize = sizeof(struct hash_ctx),
  1376. .halg.base = {
  1377. .cra_name = "hmac(sha256)",
  1378. .cra_driver_name = "hmac-sha256-ux500",
  1379. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1380. CRYPTO_ALG_ASYNC,
  1381. .cra_blocksize = SHA256_BLOCK_SIZE,
  1382. .cra_ctxsize = sizeof(struct hash_ctx),
  1383. .cra_type = &crypto_ahash_type,
  1384. .cra_init = hash_cra_init,
  1385. .cra_module = THIS_MODULE,
  1386. }
  1387. }
  1388. }
  1389. };
  1390. /**
  1391. * hash_algs_register_all -
  1392. */
  1393. static int ahash_algs_register_all(struct hash_device_data *device_data)
  1394. {
  1395. int ret;
  1396. int i;
  1397. int count;
  1398. for (i = 0; i < ARRAY_SIZE(hash_algs); i++) {
  1399. ret = crypto_register_ahash(&hash_algs[i].hash);
  1400. if (ret) {
  1401. count = i;
  1402. dev_err(device_data->dev, "[%s] alg registration failed",
  1403. hash_algs[i].hash.halg.base.cra_driver_name);
  1404. goto unreg;
  1405. }
  1406. }
  1407. return 0;
  1408. unreg:
  1409. for (i = 0; i < count; i++)
  1410. crypto_unregister_ahash(&hash_algs[i].hash);
  1411. return ret;
  1412. }
  1413. /**
  1414. * hash_algs_unregister_all -
  1415. */
  1416. static void ahash_algs_unregister_all(struct hash_device_data *device_data)
  1417. {
  1418. int i;
  1419. for (i = 0; i < ARRAY_SIZE(hash_algs); i++)
  1420. crypto_unregister_ahash(&hash_algs[i].hash);
  1421. }
  1422. /**
  1423. * ux500_hash_probe - Function that probes the hash hardware.
  1424. * @pdev: The platform device.
  1425. */
  1426. static int ux500_hash_probe(struct platform_device *pdev)
  1427. {
  1428. int ret = 0;
  1429. struct resource *res = NULL;
  1430. struct hash_device_data *device_data;
  1431. struct device *dev = &pdev->dev;
  1432. device_data = kzalloc(sizeof(struct hash_device_data), GFP_ATOMIC);
  1433. if (!device_data) {
  1434. dev_dbg(dev, "[%s] kzalloc() failed!", __func__);
  1435. ret = -ENOMEM;
  1436. goto out;
  1437. }
  1438. device_data->dev = dev;
  1439. device_data->current_ctx = NULL;
  1440. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1441. if (!res) {
  1442. dev_dbg(dev, "[%s] platform_get_resource() failed!", __func__);
  1443. ret = -ENODEV;
  1444. goto out_kfree;
  1445. }
  1446. res = request_mem_region(res->start, resource_size(res), pdev->name);
  1447. if (res == NULL) {
  1448. dev_dbg(dev, "[%s] request_mem_region() failed!", __func__);
  1449. ret = -EBUSY;
  1450. goto out_kfree;
  1451. }
  1452. device_data->base = ioremap(res->start, resource_size(res));
  1453. if (!device_data->base) {
  1454. dev_err(dev, "[%s] ioremap() failed!",
  1455. __func__);
  1456. ret = -ENOMEM;
  1457. goto out_free_mem;
  1458. }
  1459. spin_lock_init(&device_data->ctx_lock);
  1460. spin_lock_init(&device_data->power_state_lock);
  1461. /* Enable power for HASH1 hardware block */
  1462. device_data->regulator = regulator_get(dev, "v-ape");
  1463. if (IS_ERR(device_data->regulator)) {
  1464. dev_err(dev, "[%s] regulator_get() failed!", __func__);
  1465. ret = PTR_ERR(device_data->regulator);
  1466. device_data->regulator = NULL;
  1467. goto out_unmap;
  1468. }
  1469. /* Enable the clock for HASH1 hardware block */
  1470. device_data->clk = clk_get(dev, NULL);
  1471. if (IS_ERR(device_data->clk)) {
  1472. dev_err(dev, "[%s] clk_get() failed!", __func__);
  1473. ret = PTR_ERR(device_data->clk);
  1474. goto out_regulator;
  1475. }
  1476. /* Enable device power (and clock) */
  1477. ret = hash_enable_power(device_data, false);
  1478. if (ret) {
  1479. dev_err(dev, "[%s]: hash_enable_power() failed!", __func__);
  1480. goto out_clk;
  1481. }
  1482. ret = hash_check_hw(device_data);
  1483. if (ret) {
  1484. dev_err(dev, "[%s] hash_check_hw() failed!", __func__);
  1485. goto out_power;
  1486. }
  1487. if (hash_mode == HASH_MODE_DMA)
  1488. hash_dma_setup_channel(device_data, dev);
  1489. platform_set_drvdata(pdev, device_data);
  1490. /* Put the new device into the device list... */
  1491. klist_add_tail(&device_data->list_node, &driver_data.device_list);
  1492. /* ... and signal that a new device is available. */
  1493. up(&driver_data.device_allocation);
  1494. ret = ahash_algs_register_all(device_data);
  1495. if (ret) {
  1496. dev_err(dev, "[%s] ahash_algs_register_all() "
  1497. "failed!", __func__);
  1498. goto out_power;
  1499. }
  1500. dev_info(dev, "[%s] successfully probed\n", __func__);
  1501. return 0;
  1502. out_power:
  1503. hash_disable_power(device_data, false);
  1504. out_clk:
  1505. clk_put(device_data->clk);
  1506. out_regulator:
  1507. regulator_put(device_data->regulator);
  1508. out_unmap:
  1509. iounmap(device_data->base);
  1510. out_free_mem:
  1511. release_mem_region(res->start, resource_size(res));
  1512. out_kfree:
  1513. kfree(device_data);
  1514. out:
  1515. return ret;
  1516. }
  1517. /**
  1518. * ux500_hash_remove - Function that removes the hash device from the platform.
  1519. * @pdev: The platform device.
  1520. */
  1521. static int ux500_hash_remove(struct platform_device *pdev)
  1522. {
  1523. struct resource *res;
  1524. struct hash_device_data *device_data;
  1525. struct device *dev = &pdev->dev;
  1526. device_data = platform_get_drvdata(pdev);
  1527. if (!device_data) {
  1528. dev_err(dev, "[%s]: platform_get_drvdata() failed!",
  1529. __func__);
  1530. return -ENOMEM;
  1531. }
  1532. /* Try to decrease the number of available devices. */
  1533. if (down_trylock(&driver_data.device_allocation))
  1534. return -EBUSY;
  1535. /* Check that the device is free */
  1536. spin_lock(&device_data->ctx_lock);
  1537. /* current_ctx allocates a device, NULL = unallocated */
  1538. if (device_data->current_ctx) {
  1539. /* The device is busy */
  1540. spin_unlock(&device_data->ctx_lock);
  1541. /* Return the device to the pool. */
  1542. up(&driver_data.device_allocation);
  1543. return -EBUSY;
  1544. }
  1545. spin_unlock(&device_data->ctx_lock);
  1546. /* Remove the device from the list */
  1547. if (klist_node_attached(&device_data->list_node))
  1548. klist_remove(&device_data->list_node);
  1549. /* If this was the last device, remove the services */
  1550. if (list_empty(&driver_data.device_list.k_list))
  1551. ahash_algs_unregister_all(device_data);
  1552. if (hash_disable_power(device_data, false))
  1553. dev_err(dev, "[%s]: hash_disable_power() failed",
  1554. __func__);
  1555. clk_put(device_data->clk);
  1556. regulator_put(device_data->regulator);
  1557. iounmap(device_data->base);
  1558. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1559. if (res)
  1560. release_mem_region(res->start, resource_size(res));
  1561. kfree(device_data);
  1562. return 0;
  1563. }
  1564. /**
  1565. * ux500_hash_shutdown - Function that shutdown the hash device.
  1566. * @pdev: The platform device
  1567. */
  1568. static void ux500_hash_shutdown(struct platform_device *pdev)
  1569. {
  1570. struct resource *res = NULL;
  1571. struct hash_device_data *device_data;
  1572. device_data = platform_get_drvdata(pdev);
  1573. if (!device_data) {
  1574. dev_err(&pdev->dev, "[%s] platform_get_drvdata() failed!",
  1575. __func__);
  1576. return;
  1577. }
  1578. /* Check that the device is free */
  1579. spin_lock(&device_data->ctx_lock);
  1580. /* current_ctx allocates a device, NULL = unallocated */
  1581. if (!device_data->current_ctx) {
  1582. if (down_trylock(&driver_data.device_allocation))
  1583. dev_dbg(&pdev->dev, "[%s]: Cryp still in use!"
  1584. "Shutting down anyway...", __func__);
  1585. /**
  1586. * (Allocate the device)
  1587. * Need to set this to non-null (dummy) value,
  1588. * to avoid usage if context switching.
  1589. */
  1590. device_data->current_ctx++;
  1591. }
  1592. spin_unlock(&device_data->ctx_lock);
  1593. /* Remove the device from the list */
  1594. if (klist_node_attached(&device_data->list_node))
  1595. klist_remove(&device_data->list_node);
  1596. /* If this was the last device, remove the services */
  1597. if (list_empty(&driver_data.device_list.k_list))
  1598. ahash_algs_unregister_all(device_data);
  1599. iounmap(device_data->base);
  1600. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1601. if (res)
  1602. release_mem_region(res->start, resource_size(res));
  1603. if (hash_disable_power(device_data, false))
  1604. dev_err(&pdev->dev, "[%s] hash_disable_power() failed",
  1605. __func__);
  1606. }
  1607. /**
  1608. * ux500_hash_suspend - Function that suspends the hash device.
  1609. * @dev: Device to suspend.
  1610. */
  1611. static int ux500_hash_suspend(struct device *dev)
  1612. {
  1613. int ret;
  1614. struct hash_device_data *device_data;
  1615. struct hash_ctx *temp_ctx = NULL;
  1616. device_data = dev_get_drvdata(dev);
  1617. if (!device_data) {
  1618. dev_err(dev, "[%s] platform_get_drvdata() failed!", __func__);
  1619. return -ENOMEM;
  1620. }
  1621. spin_lock(&device_data->ctx_lock);
  1622. if (!device_data->current_ctx)
  1623. device_data->current_ctx++;
  1624. spin_unlock(&device_data->ctx_lock);
  1625. if (device_data->current_ctx == ++temp_ctx) {
  1626. if (down_interruptible(&driver_data.device_allocation))
  1627. dev_dbg(dev, "[%s]: down_interruptible() failed",
  1628. __func__);
  1629. ret = hash_disable_power(device_data, false);
  1630. } else
  1631. ret = hash_disable_power(device_data, true);
  1632. if (ret)
  1633. dev_err(dev, "[%s]: hash_disable_power()", __func__);
  1634. return ret;
  1635. }
  1636. /**
  1637. * ux500_hash_resume - Function that resume the hash device.
  1638. * @dev: Device to resume.
  1639. */
  1640. static int ux500_hash_resume(struct device *dev)
  1641. {
  1642. int ret = 0;
  1643. struct hash_device_data *device_data;
  1644. struct hash_ctx *temp_ctx = NULL;
  1645. device_data = dev_get_drvdata(dev);
  1646. if (!device_data) {
  1647. dev_err(dev, "[%s] platform_get_drvdata() failed!", __func__);
  1648. return -ENOMEM;
  1649. }
  1650. spin_lock(&device_data->ctx_lock);
  1651. if (device_data->current_ctx == ++temp_ctx)
  1652. device_data->current_ctx = NULL;
  1653. spin_unlock(&device_data->ctx_lock);
  1654. if (!device_data->current_ctx)
  1655. up(&driver_data.device_allocation);
  1656. else
  1657. ret = hash_enable_power(device_data, true);
  1658. if (ret)
  1659. dev_err(dev, "[%s]: hash_enable_power() failed!", __func__);
  1660. return ret;
  1661. }
  1662. static SIMPLE_DEV_PM_OPS(ux500_hash_pm, ux500_hash_suspend, ux500_hash_resume);
  1663. static struct platform_driver hash_driver = {
  1664. .probe = ux500_hash_probe,
  1665. .remove = ux500_hash_remove,
  1666. .shutdown = ux500_hash_shutdown,
  1667. .driver = {
  1668. .owner = THIS_MODULE,
  1669. .name = "hash1",
  1670. .pm = &ux500_hash_pm,
  1671. }
  1672. };
  1673. /**
  1674. * ux500_hash_mod_init - The kernel module init function.
  1675. */
  1676. static int __init ux500_hash_mod_init(void)
  1677. {
  1678. klist_init(&driver_data.device_list, NULL, NULL);
  1679. /* Initialize the semaphore to 0 devices (locked state) */
  1680. sema_init(&driver_data.device_allocation, 0);
  1681. return platform_driver_register(&hash_driver);
  1682. }
  1683. /**
  1684. * ux500_hash_mod_fini - The kernel module exit function.
  1685. */
  1686. static void __exit ux500_hash_mod_fini(void)
  1687. {
  1688. platform_driver_unregister(&hash_driver);
  1689. }
  1690. module_init(ux500_hash_mod_init);
  1691. module_exit(ux500_hash_mod_fini);
  1692. MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 HASH engine.");
  1693. MODULE_LICENSE("GPL");
  1694. MODULE_ALIAS("sha1-all");
  1695. MODULE_ALIAS("sha256-all");
  1696. MODULE_ALIAS("hmac-sha1-all");
  1697. MODULE_ALIAS("hmac-sha256-all");