artpec6_crypto.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for ARTPEC-6 crypto block using the kernel asynchronous crypto api.
  4. *
  5. * Copyright (C) 2014-2017 Axis Communications AB
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/bitfield.h>
  9. #include <linux/crypto.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/delay.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/fault-inject.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/slab.h>
  23. #include <crypto/aes.h>
  24. #include <crypto/gcm.h>
  25. #include <crypto/internal/aead.h>
  26. #include <crypto/internal/hash.h>
  27. #include <crypto/internal/skcipher.h>
  28. #include <crypto/scatterwalk.h>
  29. #include <crypto/sha1.h>
  30. #include <crypto/sha2.h>
  31. #include <crypto/xts.h>
  32. /* Max length of a line in all cache levels for Artpec SoCs. */
  33. #define ARTPEC_CACHE_LINE_MAX 32
  34. #define PDMA_OUT_CFG 0x0000
  35. #define PDMA_OUT_BUF_CFG 0x0004
  36. #define PDMA_OUT_CMD 0x0008
  37. #define PDMA_OUT_DESCRQ_PUSH 0x0010
  38. #define PDMA_OUT_DESCRQ_STAT 0x0014
  39. #define A6_PDMA_IN_CFG 0x0028
  40. #define A6_PDMA_IN_BUF_CFG 0x002c
  41. #define A6_PDMA_IN_CMD 0x0030
  42. #define A6_PDMA_IN_STATQ_PUSH 0x0038
  43. #define A6_PDMA_IN_DESCRQ_PUSH 0x0044
  44. #define A6_PDMA_IN_DESCRQ_STAT 0x0048
  45. #define A6_PDMA_INTR_MASK 0x0068
  46. #define A6_PDMA_ACK_INTR 0x006c
  47. #define A6_PDMA_MASKED_INTR 0x0074
  48. #define A7_PDMA_IN_CFG 0x002c
  49. #define A7_PDMA_IN_BUF_CFG 0x0030
  50. #define A7_PDMA_IN_CMD 0x0034
  51. #define A7_PDMA_IN_STATQ_PUSH 0x003c
  52. #define A7_PDMA_IN_DESCRQ_PUSH 0x0048
  53. #define A7_PDMA_IN_DESCRQ_STAT 0x004C
  54. #define A7_PDMA_INTR_MASK 0x006c
  55. #define A7_PDMA_ACK_INTR 0x0070
  56. #define A7_PDMA_MASKED_INTR 0x0078
  57. #define PDMA_OUT_CFG_EN BIT(0)
  58. #define PDMA_OUT_BUF_CFG_DATA_BUF_SIZE GENMASK(4, 0)
  59. #define PDMA_OUT_BUF_CFG_DESCR_BUF_SIZE GENMASK(9, 5)
  60. #define PDMA_OUT_CMD_START BIT(0)
  61. #define A6_PDMA_OUT_CMD_STOP BIT(3)
  62. #define A7_PDMA_OUT_CMD_STOP BIT(2)
  63. #define PDMA_OUT_DESCRQ_PUSH_LEN GENMASK(5, 0)
  64. #define PDMA_OUT_DESCRQ_PUSH_ADDR GENMASK(31, 6)
  65. #define PDMA_OUT_DESCRQ_STAT_LEVEL GENMASK(3, 0)
  66. #define PDMA_OUT_DESCRQ_STAT_SIZE GENMASK(7, 4)
  67. #define PDMA_IN_CFG_EN BIT(0)
  68. #define PDMA_IN_BUF_CFG_DATA_BUF_SIZE GENMASK(4, 0)
  69. #define PDMA_IN_BUF_CFG_DESCR_BUF_SIZE GENMASK(9, 5)
  70. #define PDMA_IN_BUF_CFG_STAT_BUF_SIZE GENMASK(14, 10)
  71. #define PDMA_IN_CMD_START BIT(0)
  72. #define A6_PDMA_IN_CMD_FLUSH_STAT BIT(2)
  73. #define A6_PDMA_IN_CMD_STOP BIT(3)
  74. #define A7_PDMA_IN_CMD_FLUSH_STAT BIT(1)
  75. #define A7_PDMA_IN_CMD_STOP BIT(2)
  76. #define PDMA_IN_STATQ_PUSH_LEN GENMASK(5, 0)
  77. #define PDMA_IN_STATQ_PUSH_ADDR GENMASK(31, 6)
  78. #define PDMA_IN_DESCRQ_PUSH_LEN GENMASK(5, 0)
  79. #define PDMA_IN_DESCRQ_PUSH_ADDR GENMASK(31, 6)
  80. #define PDMA_IN_DESCRQ_STAT_LEVEL GENMASK(3, 0)
  81. #define PDMA_IN_DESCRQ_STAT_SIZE GENMASK(7, 4)
  82. #define A6_PDMA_INTR_MASK_IN_DATA BIT(2)
  83. #define A6_PDMA_INTR_MASK_IN_EOP BIT(3)
  84. #define A6_PDMA_INTR_MASK_IN_EOP_FLUSH BIT(4)
  85. #define A7_PDMA_INTR_MASK_IN_DATA BIT(3)
  86. #define A7_PDMA_INTR_MASK_IN_EOP BIT(4)
  87. #define A7_PDMA_INTR_MASK_IN_EOP_FLUSH BIT(5)
  88. #define A6_CRY_MD_OPER GENMASK(19, 16)
  89. #define A6_CRY_MD_HASH_SEL_CTX GENMASK(21, 20)
  90. #define A6_CRY_MD_HASH_HMAC_FIN BIT(23)
  91. #define A6_CRY_MD_CIPHER_LEN GENMASK(21, 20)
  92. #define A6_CRY_MD_CIPHER_DECR BIT(22)
  93. #define A6_CRY_MD_CIPHER_TWEAK BIT(23)
  94. #define A6_CRY_MD_CIPHER_DSEQ BIT(24)
  95. #define A7_CRY_MD_OPER GENMASK(11, 8)
  96. #define A7_CRY_MD_HASH_SEL_CTX GENMASK(13, 12)
  97. #define A7_CRY_MD_HASH_HMAC_FIN BIT(15)
  98. #define A7_CRY_MD_CIPHER_LEN GENMASK(13, 12)
  99. #define A7_CRY_MD_CIPHER_DECR BIT(14)
  100. #define A7_CRY_MD_CIPHER_TWEAK BIT(15)
  101. #define A7_CRY_MD_CIPHER_DSEQ BIT(16)
  102. /* DMA metadata constants */
  103. #define regk_crypto_aes_cbc 0x00000002
  104. #define regk_crypto_aes_ctr 0x00000003
  105. #define regk_crypto_aes_ecb 0x00000001
  106. #define regk_crypto_aes_gcm 0x00000004
  107. #define regk_crypto_aes_xts 0x00000005
  108. #define regk_crypto_cache 0x00000002
  109. #define a6_regk_crypto_dlkey 0x0000000a
  110. #define a7_regk_crypto_dlkey 0x0000000e
  111. #define regk_crypto_ext 0x00000001
  112. #define regk_crypto_hmac_sha1 0x00000007
  113. #define regk_crypto_hmac_sha256 0x00000009
  114. #define regk_crypto_init 0x00000000
  115. #define regk_crypto_key_128 0x00000000
  116. #define regk_crypto_key_192 0x00000001
  117. #define regk_crypto_key_256 0x00000002
  118. #define regk_crypto_null 0x00000000
  119. #define regk_crypto_sha1 0x00000006
  120. #define regk_crypto_sha256 0x00000008
  121. /* DMA descriptor structures */
  122. struct pdma_descr_ctrl {
  123. unsigned char short_descr : 1;
  124. unsigned char pad1 : 1;
  125. unsigned char eop : 1;
  126. unsigned char intr : 1;
  127. unsigned char short_len : 3;
  128. unsigned char pad2 : 1;
  129. } __packed;
  130. struct pdma_data_descr {
  131. unsigned int len : 24;
  132. unsigned int buf : 32;
  133. } __packed;
  134. struct pdma_short_descr {
  135. unsigned char data[7];
  136. } __packed;
  137. struct pdma_descr {
  138. struct pdma_descr_ctrl ctrl;
  139. union {
  140. struct pdma_data_descr data;
  141. struct pdma_short_descr shrt;
  142. };
  143. };
  144. struct pdma_stat_descr {
  145. unsigned char pad1 : 1;
  146. unsigned char pad2 : 1;
  147. unsigned char eop : 1;
  148. unsigned char pad3 : 5;
  149. unsigned int len : 24;
  150. };
  151. /* Each descriptor array can hold max 64 entries */
  152. #define PDMA_DESCR_COUNT 64
  153. #define MODULE_NAME "Artpec-6 CA"
  154. /* Hash modes (including HMAC variants) */
  155. #define ARTPEC6_CRYPTO_HASH_SHA1 1
  156. #define ARTPEC6_CRYPTO_HASH_SHA256 2
  157. /* Crypto modes */
  158. #define ARTPEC6_CRYPTO_CIPHER_AES_ECB 1
  159. #define ARTPEC6_CRYPTO_CIPHER_AES_CBC 2
  160. #define ARTPEC6_CRYPTO_CIPHER_AES_CTR 3
  161. #define ARTPEC6_CRYPTO_CIPHER_AES_XTS 5
  162. /* The PDMA is a DMA-engine tightly coupled with a ciphering engine.
  163. * It operates on a descriptor array with up to 64 descriptor entries.
  164. * The arrays must be 64 byte aligned in memory.
  165. *
  166. * The ciphering unit has no registers and is completely controlled by
  167. * a 4-byte metadata that is inserted at the beginning of each dma packet.
  168. *
  169. * A dma packet is a sequence of descriptors terminated by setting the .eop
  170. * field in the final descriptor of the packet.
  171. *
  172. * Multiple packets are used for providing context data, key data and
  173. * the plain/ciphertext.
  174. *
  175. * PDMA Descriptors (Array)
  176. * +------+------+------+~~+-------+------+----
  177. * | 0 | 1 | 2 |~~| 11 EOP| 12 | ....
  178. * +--+---+--+---+----+-+~~+-------+----+-+----
  179. * | | | | |
  180. * | | | | |
  181. * __|__ +-------++-------++-------+ +----+
  182. * | MD | |Payload||Payload||Payload| | MD |
  183. * +-----+ +-------++-------++-------+ +----+
  184. */
  185. struct artpec6_crypto_bounce_buffer {
  186. struct list_head list;
  187. size_t length;
  188. struct scatterlist *sg;
  189. size_t offset;
  190. /* buf is aligned to ARTPEC_CACHE_LINE_MAX and
  191. * holds up to ARTPEC_CACHE_LINE_MAX bytes data.
  192. */
  193. void *buf;
  194. };
  195. struct artpec6_crypto_dma_map {
  196. dma_addr_t dma_addr;
  197. size_t size;
  198. enum dma_data_direction dir;
  199. };
  200. struct artpec6_crypto_dma_descriptors {
  201. struct pdma_descr out[PDMA_DESCR_COUNT] __aligned(64);
  202. struct pdma_descr in[PDMA_DESCR_COUNT] __aligned(64);
  203. u32 stat[PDMA_DESCR_COUNT] __aligned(64);
  204. struct list_head bounce_buffers;
  205. /* Enough maps for all out/in buffers, and all three descr. arrays */
  206. struct artpec6_crypto_dma_map maps[PDMA_DESCR_COUNT * 2 + 2];
  207. dma_addr_t out_dma_addr;
  208. dma_addr_t in_dma_addr;
  209. dma_addr_t stat_dma_addr;
  210. size_t out_cnt;
  211. size_t in_cnt;
  212. size_t map_count;
  213. };
  214. enum artpec6_crypto_variant {
  215. ARTPEC6_CRYPTO = 1,
  216. ARTPEC7_CRYPTO,
  217. };
  218. struct artpec6_crypto {
  219. void __iomem *base;
  220. spinlock_t queue_lock;
  221. struct list_head queue; /* waiting for pdma fifo space */
  222. struct list_head pending; /* submitted to pdma fifo */
  223. struct tasklet_struct task;
  224. struct kmem_cache *dma_cache;
  225. int pending_count;
  226. struct timer_list timer;
  227. enum artpec6_crypto_variant variant;
  228. void *pad_buffer; /* cache-aligned block padding buffer */
  229. void *zero_buffer;
  230. };
  231. enum artpec6_crypto_hash_flags {
  232. HASH_FLAG_INIT_CTX = 2,
  233. HASH_FLAG_UPDATE = 4,
  234. HASH_FLAG_FINALIZE = 8,
  235. HASH_FLAG_HMAC = 16,
  236. HASH_FLAG_UPDATE_KEY = 32,
  237. };
  238. struct artpec6_crypto_req_common {
  239. struct list_head list;
  240. struct list_head complete_in_progress;
  241. struct artpec6_crypto_dma_descriptors *dma;
  242. struct crypto_async_request *req;
  243. void (*complete)(struct crypto_async_request *req);
  244. gfp_t gfp_flags;
  245. };
  246. struct artpec6_hash_request_context {
  247. char partial_buffer[SHA256_BLOCK_SIZE];
  248. char partial_buffer_out[SHA256_BLOCK_SIZE];
  249. char key_buffer[SHA256_BLOCK_SIZE];
  250. char pad_buffer[SHA256_BLOCK_SIZE + 32];
  251. unsigned char digeststate[SHA256_DIGEST_SIZE];
  252. size_t partial_bytes;
  253. u64 digcnt;
  254. u32 key_md;
  255. u32 hash_md;
  256. enum artpec6_crypto_hash_flags hash_flags;
  257. struct artpec6_crypto_req_common common;
  258. };
  259. struct artpec6_hash_export_state {
  260. char partial_buffer[SHA256_BLOCK_SIZE];
  261. unsigned char digeststate[SHA256_DIGEST_SIZE];
  262. size_t partial_bytes;
  263. u64 digcnt;
  264. int oper;
  265. unsigned int hash_flags;
  266. };
  267. struct artpec6_hashalg_context {
  268. char hmac_key[SHA256_BLOCK_SIZE];
  269. size_t hmac_key_length;
  270. struct crypto_shash *child_hash;
  271. };
  272. struct artpec6_crypto_request_context {
  273. u32 cipher_md;
  274. bool decrypt;
  275. struct artpec6_crypto_req_common common;
  276. };
  277. struct artpec6_cryptotfm_context {
  278. unsigned char aes_key[2*AES_MAX_KEY_SIZE];
  279. size_t key_length;
  280. u32 key_md;
  281. int crypto_type;
  282. struct crypto_sync_skcipher *fallback;
  283. };
  284. struct artpec6_crypto_aead_hw_ctx {
  285. __be64 aad_length_bits;
  286. __be64 text_length_bits;
  287. __u8 J0[AES_BLOCK_SIZE];
  288. };
  289. struct artpec6_crypto_aead_req_ctx {
  290. struct artpec6_crypto_aead_hw_ctx hw_ctx;
  291. u32 cipher_md;
  292. bool decrypt;
  293. struct artpec6_crypto_req_common common;
  294. __u8 decryption_tag[AES_BLOCK_SIZE] ____cacheline_aligned;
  295. };
  296. /* The crypto framework makes it hard to avoid this global. */
  297. static struct device *artpec6_crypto_dev;
  298. #ifdef CONFIG_FAULT_INJECTION
  299. static DECLARE_FAULT_ATTR(artpec6_crypto_fail_status_read);
  300. static DECLARE_FAULT_ATTR(artpec6_crypto_fail_dma_array_full);
  301. #endif
  302. enum {
  303. ARTPEC6_CRYPTO_PREPARE_HASH_NO_START,
  304. ARTPEC6_CRYPTO_PREPARE_HASH_START,
  305. };
  306. static int artpec6_crypto_prepare_aead(struct aead_request *areq);
  307. static int artpec6_crypto_prepare_crypto(struct skcipher_request *areq);
  308. static int artpec6_crypto_prepare_hash(struct ahash_request *areq);
  309. static void
  310. artpec6_crypto_complete_crypto(struct crypto_async_request *req);
  311. static void
  312. artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req);
  313. static void
  314. artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req);
  315. static void
  316. artpec6_crypto_complete_aead(struct crypto_async_request *req);
  317. static void
  318. artpec6_crypto_complete_hash(struct crypto_async_request *req);
  319. static int
  320. artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common);
  321. static void
  322. artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common);
  323. struct artpec6_crypto_walk {
  324. struct scatterlist *sg;
  325. size_t offset;
  326. };
  327. static void artpec6_crypto_walk_init(struct artpec6_crypto_walk *awalk,
  328. struct scatterlist *sg)
  329. {
  330. awalk->sg = sg;
  331. awalk->offset = 0;
  332. }
  333. static size_t artpec6_crypto_walk_advance(struct artpec6_crypto_walk *awalk,
  334. size_t nbytes)
  335. {
  336. while (nbytes && awalk->sg) {
  337. size_t piece;
  338. WARN_ON(awalk->offset > awalk->sg->length);
  339. piece = min(nbytes, (size_t)awalk->sg->length - awalk->offset);
  340. nbytes -= piece;
  341. awalk->offset += piece;
  342. if (awalk->offset == awalk->sg->length) {
  343. awalk->sg = sg_next(awalk->sg);
  344. awalk->offset = 0;
  345. }
  346. }
  347. return nbytes;
  348. }
  349. static size_t
  350. artpec6_crypto_walk_chunklen(const struct artpec6_crypto_walk *awalk)
  351. {
  352. WARN_ON(awalk->sg->length == awalk->offset);
  353. return awalk->sg->length - awalk->offset;
  354. }
  355. static dma_addr_t
  356. artpec6_crypto_walk_chunk_phys(const struct artpec6_crypto_walk *awalk)
  357. {
  358. return sg_phys(awalk->sg) + awalk->offset;
  359. }
  360. static void
  361. artpec6_crypto_copy_bounce_buffers(struct artpec6_crypto_req_common *common)
  362. {
  363. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  364. struct artpec6_crypto_bounce_buffer *b;
  365. struct artpec6_crypto_bounce_buffer *next;
  366. list_for_each_entry_safe(b, next, &dma->bounce_buffers, list) {
  367. pr_debug("bounce entry %p: %zu bytes @ %zu from %p\n",
  368. b, b->length, b->offset, b->buf);
  369. sg_pcopy_from_buffer(b->sg,
  370. 1,
  371. b->buf,
  372. b->length,
  373. b->offset);
  374. list_del(&b->list);
  375. kfree(b);
  376. }
  377. }
  378. static inline bool artpec6_crypto_busy(void)
  379. {
  380. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  381. int fifo_count = ac->pending_count;
  382. return fifo_count > 6;
  383. }
  384. static int artpec6_crypto_submit(struct artpec6_crypto_req_common *req)
  385. {
  386. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  387. int ret = -EBUSY;
  388. spin_lock_bh(&ac->queue_lock);
  389. if (!artpec6_crypto_busy()) {
  390. list_add_tail(&req->list, &ac->pending);
  391. artpec6_crypto_start_dma(req);
  392. ret = -EINPROGRESS;
  393. } else if (req->req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG) {
  394. list_add_tail(&req->list, &ac->queue);
  395. } else {
  396. artpec6_crypto_common_destroy(req);
  397. }
  398. spin_unlock_bh(&ac->queue_lock);
  399. return ret;
  400. }
  401. static void artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common)
  402. {
  403. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  404. enum artpec6_crypto_variant variant = ac->variant;
  405. void __iomem *base = ac->base;
  406. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  407. u32 ind, statd, outd;
  408. /* Make descriptor content visible to the DMA before starting it. */
  409. wmb();
  410. ind = FIELD_PREP(PDMA_IN_DESCRQ_PUSH_LEN, dma->in_cnt - 1) |
  411. FIELD_PREP(PDMA_IN_DESCRQ_PUSH_ADDR, dma->in_dma_addr >> 6);
  412. statd = FIELD_PREP(PDMA_IN_STATQ_PUSH_LEN, dma->in_cnt - 1) |
  413. FIELD_PREP(PDMA_IN_STATQ_PUSH_ADDR, dma->stat_dma_addr >> 6);
  414. outd = FIELD_PREP(PDMA_OUT_DESCRQ_PUSH_LEN, dma->out_cnt - 1) |
  415. FIELD_PREP(PDMA_OUT_DESCRQ_PUSH_ADDR, dma->out_dma_addr >> 6);
  416. if (variant == ARTPEC6_CRYPTO) {
  417. writel_relaxed(ind, base + A6_PDMA_IN_DESCRQ_PUSH);
  418. writel_relaxed(statd, base + A6_PDMA_IN_STATQ_PUSH);
  419. writel_relaxed(PDMA_IN_CMD_START, base + A6_PDMA_IN_CMD);
  420. } else {
  421. writel_relaxed(ind, base + A7_PDMA_IN_DESCRQ_PUSH);
  422. writel_relaxed(statd, base + A7_PDMA_IN_STATQ_PUSH);
  423. writel_relaxed(PDMA_IN_CMD_START, base + A7_PDMA_IN_CMD);
  424. }
  425. writel_relaxed(outd, base + PDMA_OUT_DESCRQ_PUSH);
  426. writel_relaxed(PDMA_OUT_CMD_START, base + PDMA_OUT_CMD);
  427. ac->pending_count++;
  428. }
  429. static void
  430. artpec6_crypto_init_dma_operation(struct artpec6_crypto_req_common *common)
  431. {
  432. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  433. dma->out_cnt = 0;
  434. dma->in_cnt = 0;
  435. dma->map_count = 0;
  436. INIT_LIST_HEAD(&dma->bounce_buffers);
  437. }
  438. static bool fault_inject_dma_descr(void)
  439. {
  440. #ifdef CONFIG_FAULT_INJECTION
  441. return should_fail(&artpec6_crypto_fail_dma_array_full, 1);
  442. #else
  443. return false;
  444. #endif
  445. }
  446. /** artpec6_crypto_setup_out_descr_phys - Setup an out channel with a
  447. * physical address
  448. *
  449. * @addr: The physical address of the data buffer
  450. * @len: The length of the data buffer
  451. * @eop: True if this is the last buffer in the packet
  452. *
  453. * @return 0 on success or -ENOSPC if there are no more descriptors available
  454. */
  455. static int
  456. artpec6_crypto_setup_out_descr_phys(struct artpec6_crypto_req_common *common,
  457. dma_addr_t addr, size_t len, bool eop)
  458. {
  459. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  460. struct pdma_descr *d;
  461. if (dma->out_cnt >= PDMA_DESCR_COUNT ||
  462. fault_inject_dma_descr()) {
  463. pr_err("No free OUT DMA descriptors available!\n");
  464. return -ENOSPC;
  465. }
  466. d = &dma->out[dma->out_cnt++];
  467. memset(d, 0, sizeof(*d));
  468. d->ctrl.short_descr = 0;
  469. d->ctrl.eop = eop;
  470. d->data.len = len;
  471. d->data.buf = addr;
  472. return 0;
  473. }
  474. /** artpec6_crypto_setup_out_descr_short - Setup a short out descriptor
  475. *
  476. * @dst: The virtual address of the data
  477. * @len: The length of the data, must be between 1 to 7 bytes
  478. * @eop: True if this is the last buffer in the packet
  479. *
  480. * @return 0 on success
  481. * -ENOSPC if no more descriptors are available
  482. * -EINVAL if the data length exceeds 7 bytes
  483. */
  484. static int
  485. artpec6_crypto_setup_out_descr_short(struct artpec6_crypto_req_common *common,
  486. void *dst, unsigned int len, bool eop)
  487. {
  488. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  489. struct pdma_descr *d;
  490. if (dma->out_cnt >= PDMA_DESCR_COUNT ||
  491. fault_inject_dma_descr()) {
  492. pr_err("No free OUT DMA descriptors available!\n");
  493. return -ENOSPC;
  494. } else if (len > 7 || len < 1) {
  495. return -EINVAL;
  496. }
  497. d = &dma->out[dma->out_cnt++];
  498. memset(d, 0, sizeof(*d));
  499. d->ctrl.short_descr = 1;
  500. d->ctrl.short_len = len;
  501. d->ctrl.eop = eop;
  502. memcpy(d->shrt.data, dst, len);
  503. return 0;
  504. }
  505. static int artpec6_crypto_dma_map_page(struct artpec6_crypto_req_common *common,
  506. struct page *page, size_t offset,
  507. size_t size,
  508. enum dma_data_direction dir,
  509. dma_addr_t *dma_addr_out)
  510. {
  511. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  512. struct device *dev = artpec6_crypto_dev;
  513. struct artpec6_crypto_dma_map *map;
  514. dma_addr_t dma_addr;
  515. *dma_addr_out = 0;
  516. if (dma->map_count >= ARRAY_SIZE(dma->maps))
  517. return -ENOMEM;
  518. dma_addr = dma_map_page(dev, page, offset, size, dir);
  519. if (dma_mapping_error(dev, dma_addr))
  520. return -ENOMEM;
  521. map = &dma->maps[dma->map_count++];
  522. map->size = size;
  523. map->dma_addr = dma_addr;
  524. map->dir = dir;
  525. *dma_addr_out = dma_addr;
  526. return 0;
  527. }
  528. static int
  529. artpec6_crypto_dma_map_single(struct artpec6_crypto_req_common *common,
  530. void *ptr, size_t size,
  531. enum dma_data_direction dir,
  532. dma_addr_t *dma_addr_out)
  533. {
  534. struct page *page = virt_to_page(ptr);
  535. size_t offset = (uintptr_t)ptr & ~PAGE_MASK;
  536. return artpec6_crypto_dma_map_page(common, page, offset, size, dir,
  537. dma_addr_out);
  538. }
  539. static int
  540. artpec6_crypto_dma_map_descs(struct artpec6_crypto_req_common *common)
  541. {
  542. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  543. int ret;
  544. ret = artpec6_crypto_dma_map_single(common, dma->in,
  545. sizeof(dma->in[0]) * dma->in_cnt,
  546. DMA_TO_DEVICE, &dma->in_dma_addr);
  547. if (ret)
  548. return ret;
  549. ret = artpec6_crypto_dma_map_single(common, dma->out,
  550. sizeof(dma->out[0]) * dma->out_cnt,
  551. DMA_TO_DEVICE, &dma->out_dma_addr);
  552. if (ret)
  553. return ret;
  554. /* We only read one stat descriptor */
  555. dma->stat[dma->in_cnt - 1] = 0;
  556. /*
  557. * DMA_BIDIRECTIONAL since we need our zeroing of the stat descriptor
  558. * to be written.
  559. */
  560. return artpec6_crypto_dma_map_single(common,
  561. dma->stat,
  562. sizeof(dma->stat[0]) * dma->in_cnt,
  563. DMA_BIDIRECTIONAL,
  564. &dma->stat_dma_addr);
  565. }
  566. static void
  567. artpec6_crypto_dma_unmap_all(struct artpec6_crypto_req_common *common)
  568. {
  569. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  570. struct device *dev = artpec6_crypto_dev;
  571. int i;
  572. for (i = 0; i < dma->map_count; i++) {
  573. struct artpec6_crypto_dma_map *map = &dma->maps[i];
  574. dma_unmap_page(dev, map->dma_addr, map->size, map->dir);
  575. }
  576. dma->map_count = 0;
  577. }
  578. /** artpec6_crypto_setup_out_descr - Setup an out descriptor
  579. *
  580. * @dst: The virtual address of the data
  581. * @len: The length of the data
  582. * @eop: True if this is the last buffer in the packet
  583. * @use_short: If this is true and the data length is 7 bytes or less then
  584. * a short descriptor will be used
  585. *
  586. * @return 0 on success
  587. * Any errors from artpec6_crypto_setup_out_descr_short() or
  588. * setup_out_descr_phys()
  589. */
  590. static int
  591. artpec6_crypto_setup_out_descr(struct artpec6_crypto_req_common *common,
  592. void *dst, unsigned int len, bool eop,
  593. bool use_short)
  594. {
  595. if (use_short && len < 7) {
  596. return artpec6_crypto_setup_out_descr_short(common, dst, len,
  597. eop);
  598. } else {
  599. int ret;
  600. dma_addr_t dma_addr;
  601. ret = artpec6_crypto_dma_map_single(common, dst, len,
  602. DMA_TO_DEVICE,
  603. &dma_addr);
  604. if (ret)
  605. return ret;
  606. return artpec6_crypto_setup_out_descr_phys(common, dma_addr,
  607. len, eop);
  608. }
  609. }
  610. /** artpec6_crypto_setup_in_descr_phys - Setup an in channel with a
  611. * physical address
  612. *
  613. * @addr: The physical address of the data buffer
  614. * @len: The length of the data buffer
  615. * @intr: True if an interrupt should be fired after HW processing of this
  616. * descriptor
  617. *
  618. */
  619. static int
  620. artpec6_crypto_setup_in_descr_phys(struct artpec6_crypto_req_common *common,
  621. dma_addr_t addr, unsigned int len, bool intr)
  622. {
  623. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  624. struct pdma_descr *d;
  625. if (dma->in_cnt >= PDMA_DESCR_COUNT ||
  626. fault_inject_dma_descr()) {
  627. pr_err("No free IN DMA descriptors available!\n");
  628. return -ENOSPC;
  629. }
  630. d = &dma->in[dma->in_cnt++];
  631. memset(d, 0, sizeof(*d));
  632. d->ctrl.intr = intr;
  633. d->data.len = len;
  634. d->data.buf = addr;
  635. return 0;
  636. }
  637. /** artpec6_crypto_setup_in_descr - Setup an in channel descriptor
  638. *
  639. * @buffer: The virtual address to of the data buffer
  640. * @len: The length of the data buffer
  641. * @last: If this is the last data buffer in the request (i.e. an interrupt
  642. * is needed
  643. *
  644. * Short descriptors are not used for the in channel
  645. */
  646. static int
  647. artpec6_crypto_setup_in_descr(struct artpec6_crypto_req_common *common,
  648. void *buffer, unsigned int len, bool last)
  649. {
  650. dma_addr_t dma_addr;
  651. int ret;
  652. ret = artpec6_crypto_dma_map_single(common, buffer, len,
  653. DMA_FROM_DEVICE, &dma_addr);
  654. if (ret)
  655. return ret;
  656. return artpec6_crypto_setup_in_descr_phys(common, dma_addr, len, last);
  657. }
  658. static struct artpec6_crypto_bounce_buffer *
  659. artpec6_crypto_alloc_bounce(gfp_t flags)
  660. {
  661. void *base;
  662. size_t alloc_size = sizeof(struct artpec6_crypto_bounce_buffer) +
  663. 2 * ARTPEC_CACHE_LINE_MAX;
  664. struct artpec6_crypto_bounce_buffer *bbuf = kzalloc(alloc_size, flags);
  665. if (!bbuf)
  666. return NULL;
  667. base = bbuf + 1;
  668. bbuf->buf = PTR_ALIGN(base, ARTPEC_CACHE_LINE_MAX);
  669. return bbuf;
  670. }
  671. static int setup_bounce_buffer_in(struct artpec6_crypto_req_common *common,
  672. struct artpec6_crypto_walk *walk, size_t size)
  673. {
  674. struct artpec6_crypto_bounce_buffer *bbuf;
  675. int ret;
  676. bbuf = artpec6_crypto_alloc_bounce(common->gfp_flags);
  677. if (!bbuf)
  678. return -ENOMEM;
  679. bbuf->length = size;
  680. bbuf->sg = walk->sg;
  681. bbuf->offset = walk->offset;
  682. ret = artpec6_crypto_setup_in_descr(common, bbuf->buf, size, false);
  683. if (ret) {
  684. kfree(bbuf);
  685. return ret;
  686. }
  687. pr_debug("BOUNCE %zu offset %zu\n", size, walk->offset);
  688. list_add_tail(&bbuf->list, &common->dma->bounce_buffers);
  689. return 0;
  690. }
  691. static int
  692. artpec6_crypto_setup_sg_descrs_in(struct artpec6_crypto_req_common *common,
  693. struct artpec6_crypto_walk *walk,
  694. size_t count)
  695. {
  696. size_t chunk;
  697. int ret;
  698. dma_addr_t addr;
  699. while (walk->sg && count) {
  700. chunk = min(count, artpec6_crypto_walk_chunklen(walk));
  701. addr = artpec6_crypto_walk_chunk_phys(walk);
  702. /* When destination buffers are not aligned to the cache line
  703. * size we need bounce buffers. The DMA-API requires that the
  704. * entire line is owned by the DMA buffer and this holds also
  705. * for the case when coherent DMA is used.
  706. */
  707. if (!IS_ALIGNED(addr, ARTPEC_CACHE_LINE_MAX)) {
  708. chunk = min_t(dma_addr_t, chunk,
  709. ALIGN(addr, ARTPEC_CACHE_LINE_MAX) -
  710. addr);
  711. pr_debug("CHUNK-b %pad:%zu\n", &addr, chunk);
  712. ret = setup_bounce_buffer_in(common, walk, chunk);
  713. } else if (chunk < ARTPEC_CACHE_LINE_MAX) {
  714. pr_debug("CHUNK-b %pad:%zu\n", &addr, chunk);
  715. ret = setup_bounce_buffer_in(common, walk, chunk);
  716. } else {
  717. dma_addr_t dma_addr;
  718. chunk = chunk & ~(ARTPEC_CACHE_LINE_MAX-1);
  719. pr_debug("CHUNK %pad:%zu\n", &addr, chunk);
  720. ret = artpec6_crypto_dma_map_page(common,
  721. sg_page(walk->sg),
  722. walk->sg->offset +
  723. walk->offset,
  724. chunk,
  725. DMA_FROM_DEVICE,
  726. &dma_addr);
  727. if (ret)
  728. return ret;
  729. ret = artpec6_crypto_setup_in_descr_phys(common,
  730. dma_addr,
  731. chunk, false);
  732. }
  733. if (ret)
  734. return ret;
  735. count = count - chunk;
  736. artpec6_crypto_walk_advance(walk, chunk);
  737. }
  738. if (count)
  739. pr_err("EOL unexpected %zu bytes left\n", count);
  740. return count ? -EINVAL : 0;
  741. }
  742. static int
  743. artpec6_crypto_setup_sg_descrs_out(struct artpec6_crypto_req_common *common,
  744. struct artpec6_crypto_walk *walk,
  745. size_t count)
  746. {
  747. size_t chunk;
  748. int ret;
  749. dma_addr_t addr;
  750. while (walk->sg && count) {
  751. chunk = min(count, artpec6_crypto_walk_chunklen(walk));
  752. addr = artpec6_crypto_walk_chunk_phys(walk);
  753. pr_debug("OUT-CHUNK %pad:%zu\n", &addr, chunk);
  754. if (addr & 3) {
  755. char buf[3];
  756. chunk = min_t(size_t, chunk, (4-(addr&3)));
  757. sg_pcopy_to_buffer(walk->sg, 1, buf, chunk,
  758. walk->offset);
  759. ret = artpec6_crypto_setup_out_descr_short(common, buf,
  760. chunk,
  761. false);
  762. } else {
  763. dma_addr_t dma_addr;
  764. ret = artpec6_crypto_dma_map_page(common,
  765. sg_page(walk->sg),
  766. walk->sg->offset +
  767. walk->offset,
  768. chunk,
  769. DMA_TO_DEVICE,
  770. &dma_addr);
  771. if (ret)
  772. return ret;
  773. ret = artpec6_crypto_setup_out_descr_phys(common,
  774. dma_addr,
  775. chunk, false);
  776. }
  777. if (ret)
  778. return ret;
  779. count = count - chunk;
  780. artpec6_crypto_walk_advance(walk, chunk);
  781. }
  782. if (count)
  783. pr_err("EOL unexpected %zu bytes left\n", count);
  784. return count ? -EINVAL : 0;
  785. }
  786. /** artpec6_crypto_terminate_out_descrs - Set the EOP on the last out descriptor
  787. *
  788. * If the out descriptor list is non-empty, then the eop flag on the
  789. * last used out descriptor will be set.
  790. *
  791. * @return 0 on success
  792. * -EINVAL if the out descriptor is empty or has overflown
  793. */
  794. static int
  795. artpec6_crypto_terminate_out_descrs(struct artpec6_crypto_req_common *common)
  796. {
  797. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  798. struct pdma_descr *d;
  799. if (!dma->out_cnt || dma->out_cnt > PDMA_DESCR_COUNT) {
  800. pr_err("%s: OUT descriptor list is %s\n",
  801. MODULE_NAME, dma->out_cnt ? "empty" : "full");
  802. return -EINVAL;
  803. }
  804. d = &dma->out[dma->out_cnt-1];
  805. d->ctrl.eop = 1;
  806. return 0;
  807. }
  808. /** artpec6_crypto_terminate_in_descrs - Set the interrupt flag on the last
  809. * in descriptor
  810. *
  811. * See artpec6_crypto_terminate_out_descrs() for return values
  812. */
  813. static int
  814. artpec6_crypto_terminate_in_descrs(struct artpec6_crypto_req_common *common)
  815. {
  816. struct artpec6_crypto_dma_descriptors *dma = common->dma;
  817. struct pdma_descr *d;
  818. if (!dma->in_cnt || dma->in_cnt > PDMA_DESCR_COUNT) {
  819. pr_err("%s: IN descriptor list is %s\n",
  820. MODULE_NAME, dma->in_cnt ? "empty" : "full");
  821. return -EINVAL;
  822. }
  823. d = &dma->in[dma->in_cnt-1];
  824. d->ctrl.intr = 1;
  825. return 0;
  826. }
  827. /** create_hash_pad - Create a Secure Hash conformant pad
  828. *
  829. * @dst: The destination buffer to write the pad. Must be at least 64 bytes
  830. * @dgstlen: The total length of the hash digest in bytes
  831. * @bitcount: The total length of the digest in bits
  832. *
  833. * @return The total number of padding bytes written to @dst
  834. */
  835. static size_t
  836. create_hash_pad(int oper, unsigned char *dst, u64 dgstlen, u64 bitcount)
  837. {
  838. unsigned int mod, target, diff, pad_bytes, size_bytes;
  839. __be64 bits = __cpu_to_be64(bitcount);
  840. switch (oper) {
  841. case regk_crypto_sha1:
  842. case regk_crypto_sha256:
  843. case regk_crypto_hmac_sha1:
  844. case regk_crypto_hmac_sha256:
  845. target = 448 / 8;
  846. mod = 512 / 8;
  847. size_bytes = 8;
  848. break;
  849. default:
  850. target = 896 / 8;
  851. mod = 1024 / 8;
  852. size_bytes = 16;
  853. break;
  854. }
  855. target -= 1;
  856. diff = dgstlen & (mod - 1);
  857. pad_bytes = diff > target ? target + mod - diff : target - diff;
  858. memset(dst + 1, 0, pad_bytes);
  859. dst[0] = 0x80;
  860. if (size_bytes == 16) {
  861. memset(dst + 1 + pad_bytes, 0, 8);
  862. memcpy(dst + 1 + pad_bytes + 8, &bits, 8);
  863. } else {
  864. memcpy(dst + 1 + pad_bytes, &bits, 8);
  865. }
  866. return pad_bytes + size_bytes + 1;
  867. }
  868. static int artpec6_crypto_common_init(struct artpec6_crypto_req_common *common,
  869. struct crypto_async_request *parent,
  870. void (*complete)(struct crypto_async_request *req),
  871. struct scatterlist *dstsg, unsigned int nbytes)
  872. {
  873. gfp_t flags;
  874. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  875. flags = (parent->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  876. GFP_KERNEL : GFP_ATOMIC;
  877. common->gfp_flags = flags;
  878. common->dma = kmem_cache_alloc(ac->dma_cache, flags);
  879. if (!common->dma)
  880. return -ENOMEM;
  881. common->req = parent;
  882. common->complete = complete;
  883. return 0;
  884. }
  885. static void
  886. artpec6_crypto_bounce_destroy(struct artpec6_crypto_dma_descriptors *dma)
  887. {
  888. struct artpec6_crypto_bounce_buffer *b;
  889. struct artpec6_crypto_bounce_buffer *next;
  890. list_for_each_entry_safe(b, next, &dma->bounce_buffers, list) {
  891. kfree(b);
  892. }
  893. }
  894. static int
  895. artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common)
  896. {
  897. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  898. artpec6_crypto_dma_unmap_all(common);
  899. artpec6_crypto_bounce_destroy(common->dma);
  900. kmem_cache_free(ac->dma_cache, common->dma);
  901. common->dma = NULL;
  902. return 0;
  903. }
  904. /*
  905. * Ciphering functions.
  906. */
  907. static int artpec6_crypto_encrypt(struct skcipher_request *req)
  908. {
  909. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  910. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
  911. struct artpec6_crypto_request_context *req_ctx = NULL;
  912. void (*complete)(struct crypto_async_request *req);
  913. int ret;
  914. req_ctx = skcipher_request_ctx(req);
  915. switch (ctx->crypto_type) {
  916. case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
  917. case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
  918. case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
  919. req_ctx->decrypt = 0;
  920. break;
  921. default:
  922. break;
  923. }
  924. switch (ctx->crypto_type) {
  925. case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
  926. complete = artpec6_crypto_complete_cbc_encrypt;
  927. break;
  928. default:
  929. complete = artpec6_crypto_complete_crypto;
  930. break;
  931. }
  932. ret = artpec6_crypto_common_init(&req_ctx->common,
  933. &req->base,
  934. complete,
  935. req->dst, req->cryptlen);
  936. if (ret)
  937. return ret;
  938. ret = artpec6_crypto_prepare_crypto(req);
  939. if (ret) {
  940. artpec6_crypto_common_destroy(&req_ctx->common);
  941. return ret;
  942. }
  943. return artpec6_crypto_submit(&req_ctx->common);
  944. }
  945. static int artpec6_crypto_decrypt(struct skcipher_request *req)
  946. {
  947. int ret;
  948. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  949. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
  950. struct artpec6_crypto_request_context *req_ctx = NULL;
  951. void (*complete)(struct crypto_async_request *req);
  952. req_ctx = skcipher_request_ctx(req);
  953. switch (ctx->crypto_type) {
  954. case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
  955. case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
  956. case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
  957. req_ctx->decrypt = 1;
  958. break;
  959. default:
  960. break;
  961. }
  962. switch (ctx->crypto_type) {
  963. case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
  964. complete = artpec6_crypto_complete_cbc_decrypt;
  965. break;
  966. default:
  967. complete = artpec6_crypto_complete_crypto;
  968. break;
  969. }
  970. ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
  971. complete,
  972. req->dst, req->cryptlen);
  973. if (ret)
  974. return ret;
  975. ret = artpec6_crypto_prepare_crypto(req);
  976. if (ret) {
  977. artpec6_crypto_common_destroy(&req_ctx->common);
  978. return ret;
  979. }
  980. return artpec6_crypto_submit(&req_ctx->common);
  981. }
  982. static int
  983. artpec6_crypto_ctr_crypt(struct skcipher_request *req, bool encrypt)
  984. {
  985. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  986. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
  987. size_t iv_len = crypto_skcipher_ivsize(cipher);
  988. unsigned int counter = be32_to_cpup((__be32 *)
  989. (req->iv + iv_len - 4));
  990. unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
  991. AES_BLOCK_SIZE;
  992. /*
  993. * The hardware uses only the last 32-bits as the counter while the
  994. * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
  995. * the whole IV is a counter. So fallback if the counter is going to
  996. * overlow.
  997. */
  998. if (counter + nblks < counter) {
  999. int ret;
  1000. pr_debug("counter %x will overflow (nblks %u), falling back\n",
  1001. counter, counter + nblks);
  1002. ret = crypto_sync_skcipher_setkey(ctx->fallback, ctx->aes_key,
  1003. ctx->key_length);
  1004. if (ret)
  1005. return ret;
  1006. {
  1007. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
  1008. skcipher_request_set_sync_tfm(subreq, ctx->fallback);
  1009. skcipher_request_set_callback(subreq, req->base.flags,
  1010. NULL, NULL);
  1011. skcipher_request_set_crypt(subreq, req->src, req->dst,
  1012. req->cryptlen, req->iv);
  1013. ret = encrypt ? crypto_skcipher_encrypt(subreq)
  1014. : crypto_skcipher_decrypt(subreq);
  1015. skcipher_request_zero(subreq);
  1016. }
  1017. return ret;
  1018. }
  1019. return encrypt ? artpec6_crypto_encrypt(req)
  1020. : artpec6_crypto_decrypt(req);
  1021. }
  1022. static int artpec6_crypto_ctr_encrypt(struct skcipher_request *req)
  1023. {
  1024. return artpec6_crypto_ctr_crypt(req, true);
  1025. }
  1026. static int artpec6_crypto_ctr_decrypt(struct skcipher_request *req)
  1027. {
  1028. return artpec6_crypto_ctr_crypt(req, false);
  1029. }
  1030. /*
  1031. * AEAD functions
  1032. */
  1033. static int artpec6_crypto_aead_init(struct crypto_aead *tfm)
  1034. {
  1035. struct artpec6_cryptotfm_context *tfm_ctx = crypto_aead_ctx(tfm);
  1036. memset(tfm_ctx, 0, sizeof(*tfm_ctx));
  1037. crypto_aead_set_reqsize(tfm,
  1038. sizeof(struct artpec6_crypto_aead_req_ctx));
  1039. return 0;
  1040. }
  1041. static int artpec6_crypto_aead_set_key(struct crypto_aead *tfm, const u8 *key,
  1042. unsigned int len)
  1043. {
  1044. struct artpec6_cryptotfm_context *ctx = crypto_tfm_ctx(&tfm->base);
  1045. if (len != 16 && len != 24 && len != 32)
  1046. return -EINVAL;
  1047. ctx->key_length = len;
  1048. memcpy(ctx->aes_key, key, len);
  1049. return 0;
  1050. }
  1051. static int artpec6_crypto_aead_encrypt(struct aead_request *req)
  1052. {
  1053. int ret;
  1054. struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
  1055. req_ctx->decrypt = false;
  1056. ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
  1057. artpec6_crypto_complete_aead,
  1058. NULL, 0);
  1059. if (ret)
  1060. return ret;
  1061. ret = artpec6_crypto_prepare_aead(req);
  1062. if (ret) {
  1063. artpec6_crypto_common_destroy(&req_ctx->common);
  1064. return ret;
  1065. }
  1066. return artpec6_crypto_submit(&req_ctx->common);
  1067. }
  1068. static int artpec6_crypto_aead_decrypt(struct aead_request *req)
  1069. {
  1070. int ret;
  1071. struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
  1072. req_ctx->decrypt = true;
  1073. if (req->cryptlen < AES_BLOCK_SIZE)
  1074. return -EINVAL;
  1075. ret = artpec6_crypto_common_init(&req_ctx->common,
  1076. &req->base,
  1077. artpec6_crypto_complete_aead,
  1078. NULL, 0);
  1079. if (ret)
  1080. return ret;
  1081. ret = artpec6_crypto_prepare_aead(req);
  1082. if (ret) {
  1083. artpec6_crypto_common_destroy(&req_ctx->common);
  1084. return ret;
  1085. }
  1086. return artpec6_crypto_submit(&req_ctx->common);
  1087. }
  1088. static int artpec6_crypto_prepare_hash(struct ahash_request *areq)
  1089. {
  1090. struct artpec6_hashalg_context *ctx = crypto_tfm_ctx(areq->base.tfm);
  1091. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(areq);
  1092. size_t digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq));
  1093. size_t contextsize = digestsize;
  1094. size_t blocksize = crypto_tfm_alg_blocksize(
  1095. crypto_ahash_tfm(crypto_ahash_reqtfm(areq)));
  1096. struct artpec6_crypto_req_common *common = &req_ctx->common;
  1097. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  1098. enum artpec6_crypto_variant variant = ac->variant;
  1099. u32 sel_ctx;
  1100. bool ext_ctx = false;
  1101. bool run_hw = false;
  1102. int error = 0;
  1103. artpec6_crypto_init_dma_operation(common);
  1104. /* Upload HMAC key, must be first the first packet */
  1105. if (req_ctx->hash_flags & HASH_FLAG_HMAC) {
  1106. if (variant == ARTPEC6_CRYPTO) {
  1107. req_ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER,
  1108. a6_regk_crypto_dlkey);
  1109. } else {
  1110. req_ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
  1111. a7_regk_crypto_dlkey);
  1112. }
  1113. /* Copy and pad up the key */
  1114. memcpy(req_ctx->key_buffer, ctx->hmac_key,
  1115. ctx->hmac_key_length);
  1116. memset(req_ctx->key_buffer + ctx->hmac_key_length, 0,
  1117. blocksize - ctx->hmac_key_length);
  1118. error = artpec6_crypto_setup_out_descr(common,
  1119. (void *)&req_ctx->key_md,
  1120. sizeof(req_ctx->key_md), false, false);
  1121. if (error)
  1122. return error;
  1123. error = artpec6_crypto_setup_out_descr(common,
  1124. req_ctx->key_buffer, blocksize,
  1125. true, false);
  1126. if (error)
  1127. return error;
  1128. }
  1129. if (!(req_ctx->hash_flags & HASH_FLAG_INIT_CTX)) {
  1130. /* Restore context */
  1131. sel_ctx = regk_crypto_ext;
  1132. ext_ctx = true;
  1133. } else {
  1134. sel_ctx = regk_crypto_init;
  1135. }
  1136. if (variant == ARTPEC6_CRYPTO) {
  1137. req_ctx->hash_md &= ~A6_CRY_MD_HASH_SEL_CTX;
  1138. req_ctx->hash_md |= FIELD_PREP(A6_CRY_MD_HASH_SEL_CTX, sel_ctx);
  1139. /* If this is the final round, set the final flag */
  1140. if (req_ctx->hash_flags & HASH_FLAG_FINALIZE)
  1141. req_ctx->hash_md |= A6_CRY_MD_HASH_HMAC_FIN;
  1142. } else {
  1143. req_ctx->hash_md &= ~A7_CRY_MD_HASH_SEL_CTX;
  1144. req_ctx->hash_md |= FIELD_PREP(A7_CRY_MD_HASH_SEL_CTX, sel_ctx);
  1145. /* If this is the final round, set the final flag */
  1146. if (req_ctx->hash_flags & HASH_FLAG_FINALIZE)
  1147. req_ctx->hash_md |= A7_CRY_MD_HASH_HMAC_FIN;
  1148. }
  1149. /* Setup up metadata descriptors */
  1150. error = artpec6_crypto_setup_out_descr(common,
  1151. (void *)&req_ctx->hash_md,
  1152. sizeof(req_ctx->hash_md), false, false);
  1153. if (error)
  1154. return error;
  1155. error = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
  1156. if (error)
  1157. return error;
  1158. if (ext_ctx) {
  1159. error = artpec6_crypto_setup_out_descr(common,
  1160. req_ctx->digeststate,
  1161. contextsize, false, false);
  1162. if (error)
  1163. return error;
  1164. }
  1165. if (req_ctx->hash_flags & HASH_FLAG_UPDATE) {
  1166. size_t done_bytes = 0;
  1167. size_t total_bytes = areq->nbytes + req_ctx->partial_bytes;
  1168. size_t ready_bytes = round_down(total_bytes, blocksize);
  1169. struct artpec6_crypto_walk walk;
  1170. run_hw = ready_bytes > 0;
  1171. if (req_ctx->partial_bytes && ready_bytes) {
  1172. /* We have a partial buffer and will at least some bytes
  1173. * to the HW. Empty this partial buffer before tackling
  1174. * the SG lists
  1175. */
  1176. memcpy(req_ctx->partial_buffer_out,
  1177. req_ctx->partial_buffer,
  1178. req_ctx->partial_bytes);
  1179. error = artpec6_crypto_setup_out_descr(common,
  1180. req_ctx->partial_buffer_out,
  1181. req_ctx->partial_bytes,
  1182. false, true);
  1183. if (error)
  1184. return error;
  1185. /* Reset partial buffer */
  1186. done_bytes += req_ctx->partial_bytes;
  1187. req_ctx->partial_bytes = 0;
  1188. }
  1189. artpec6_crypto_walk_init(&walk, areq->src);
  1190. error = artpec6_crypto_setup_sg_descrs_out(common, &walk,
  1191. ready_bytes -
  1192. done_bytes);
  1193. if (error)
  1194. return error;
  1195. if (walk.sg) {
  1196. size_t sg_skip = ready_bytes - done_bytes;
  1197. size_t sg_rem = areq->nbytes - sg_skip;
  1198. sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
  1199. req_ctx->partial_buffer +
  1200. req_ctx->partial_bytes,
  1201. sg_rem, sg_skip);
  1202. req_ctx->partial_bytes += sg_rem;
  1203. }
  1204. req_ctx->digcnt += ready_bytes;
  1205. req_ctx->hash_flags &= ~(HASH_FLAG_UPDATE);
  1206. }
  1207. /* Finalize */
  1208. if (req_ctx->hash_flags & HASH_FLAG_FINALIZE) {
  1209. size_t hash_pad_len;
  1210. u64 digest_bits;
  1211. u32 oper;
  1212. if (variant == ARTPEC6_CRYPTO)
  1213. oper = FIELD_GET(A6_CRY_MD_OPER, req_ctx->hash_md);
  1214. else
  1215. oper = FIELD_GET(A7_CRY_MD_OPER, req_ctx->hash_md);
  1216. /* Write out the partial buffer if present */
  1217. if (req_ctx->partial_bytes) {
  1218. memcpy(req_ctx->partial_buffer_out,
  1219. req_ctx->partial_buffer,
  1220. req_ctx->partial_bytes);
  1221. error = artpec6_crypto_setup_out_descr(common,
  1222. req_ctx->partial_buffer_out,
  1223. req_ctx->partial_bytes,
  1224. false, true);
  1225. if (error)
  1226. return error;
  1227. req_ctx->digcnt += req_ctx->partial_bytes;
  1228. req_ctx->partial_bytes = 0;
  1229. }
  1230. if (req_ctx->hash_flags & HASH_FLAG_HMAC)
  1231. digest_bits = 8 * (req_ctx->digcnt + blocksize);
  1232. else
  1233. digest_bits = 8 * req_ctx->digcnt;
  1234. /* Add the hash pad */
  1235. hash_pad_len = create_hash_pad(oper, req_ctx->pad_buffer,
  1236. req_ctx->digcnt, digest_bits);
  1237. error = artpec6_crypto_setup_out_descr(common,
  1238. req_ctx->pad_buffer,
  1239. hash_pad_len, false,
  1240. true);
  1241. req_ctx->digcnt = 0;
  1242. if (error)
  1243. return error;
  1244. /* Descriptor for the final result */
  1245. error = artpec6_crypto_setup_in_descr(common, areq->result,
  1246. digestsize,
  1247. true);
  1248. if (error)
  1249. return error;
  1250. } else { /* This is not the final operation for this request */
  1251. if (!run_hw)
  1252. return ARTPEC6_CRYPTO_PREPARE_HASH_NO_START;
  1253. /* Save the result to the context */
  1254. error = artpec6_crypto_setup_in_descr(common,
  1255. req_ctx->digeststate,
  1256. contextsize, false);
  1257. if (error)
  1258. return error;
  1259. /* fall through */
  1260. }
  1261. req_ctx->hash_flags &= ~(HASH_FLAG_INIT_CTX | HASH_FLAG_UPDATE |
  1262. HASH_FLAG_FINALIZE);
  1263. error = artpec6_crypto_terminate_in_descrs(common);
  1264. if (error)
  1265. return error;
  1266. error = artpec6_crypto_terminate_out_descrs(common);
  1267. if (error)
  1268. return error;
  1269. error = artpec6_crypto_dma_map_descs(common);
  1270. if (error)
  1271. return error;
  1272. return ARTPEC6_CRYPTO_PREPARE_HASH_START;
  1273. }
  1274. static int artpec6_crypto_aes_ecb_init(struct crypto_skcipher *tfm)
  1275. {
  1276. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1277. crypto_skcipher_set_reqsize(tfm,
  1278. sizeof(struct artpec6_crypto_request_context));
  1279. ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_ECB;
  1280. return 0;
  1281. }
  1282. static int artpec6_crypto_aes_ctr_init(struct crypto_skcipher *tfm)
  1283. {
  1284. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1285. ctx->fallback =
  1286. crypto_alloc_sync_skcipher(crypto_tfm_alg_name(&tfm->base),
  1287. 0, CRYPTO_ALG_NEED_FALLBACK);
  1288. if (IS_ERR(ctx->fallback))
  1289. return PTR_ERR(ctx->fallback);
  1290. crypto_skcipher_set_reqsize(tfm,
  1291. sizeof(struct artpec6_crypto_request_context));
  1292. ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CTR;
  1293. return 0;
  1294. }
  1295. static int artpec6_crypto_aes_cbc_init(struct crypto_skcipher *tfm)
  1296. {
  1297. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1298. crypto_skcipher_set_reqsize(tfm,
  1299. sizeof(struct artpec6_crypto_request_context));
  1300. ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CBC;
  1301. return 0;
  1302. }
  1303. static int artpec6_crypto_aes_xts_init(struct crypto_skcipher *tfm)
  1304. {
  1305. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1306. crypto_skcipher_set_reqsize(tfm,
  1307. sizeof(struct artpec6_crypto_request_context));
  1308. ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_XTS;
  1309. return 0;
  1310. }
  1311. static void artpec6_crypto_aes_exit(struct crypto_skcipher *tfm)
  1312. {
  1313. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1314. memset(ctx, 0, sizeof(*ctx));
  1315. }
  1316. static void artpec6_crypto_aes_ctr_exit(struct crypto_skcipher *tfm)
  1317. {
  1318. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
  1319. crypto_free_sync_skcipher(ctx->fallback);
  1320. artpec6_crypto_aes_exit(tfm);
  1321. }
  1322. static int
  1323. artpec6_crypto_cipher_set_key(struct crypto_skcipher *cipher, const u8 *key,
  1324. unsigned int keylen)
  1325. {
  1326. struct artpec6_cryptotfm_context *ctx =
  1327. crypto_skcipher_ctx(cipher);
  1328. switch (keylen) {
  1329. case 16:
  1330. case 24:
  1331. case 32:
  1332. break;
  1333. default:
  1334. return -EINVAL;
  1335. }
  1336. memcpy(ctx->aes_key, key, keylen);
  1337. ctx->key_length = keylen;
  1338. return 0;
  1339. }
  1340. static int
  1341. artpec6_crypto_xts_set_key(struct crypto_skcipher *cipher, const u8 *key,
  1342. unsigned int keylen)
  1343. {
  1344. struct artpec6_cryptotfm_context *ctx =
  1345. crypto_skcipher_ctx(cipher);
  1346. int ret;
  1347. ret = xts_verify_key(cipher, key, keylen);
  1348. if (ret)
  1349. return ret;
  1350. switch (keylen) {
  1351. case 32:
  1352. case 48:
  1353. case 64:
  1354. break;
  1355. default:
  1356. return -EINVAL;
  1357. }
  1358. memcpy(ctx->aes_key, key, keylen);
  1359. ctx->key_length = keylen;
  1360. return 0;
  1361. }
  1362. /** artpec6_crypto_process_crypto - Prepare an async block cipher crypto request
  1363. *
  1364. * @req: The asynch request to process
  1365. *
  1366. * @return 0 if the dma job was successfully prepared
  1367. * <0 on error
  1368. *
  1369. * This function sets up the PDMA descriptors for a block cipher request.
  1370. *
  1371. * The required padding is added for AES-CTR using a statically defined
  1372. * buffer.
  1373. *
  1374. * The PDMA descriptor list will be as follows:
  1375. *
  1376. * OUT: [KEY_MD][KEY][EOP]<CIPHER_MD>[IV]<data_0>...[data_n][AES-CTR_pad]<eop>
  1377. * IN: <CIPHER_MD><data_0>...[data_n]<intr>
  1378. *
  1379. */
  1380. static int artpec6_crypto_prepare_crypto(struct skcipher_request *areq)
  1381. {
  1382. int ret;
  1383. struct artpec6_crypto_walk walk;
  1384. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
  1385. struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
  1386. struct artpec6_crypto_request_context *req_ctx = NULL;
  1387. size_t iv_len = crypto_skcipher_ivsize(cipher);
  1388. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  1389. enum artpec6_crypto_variant variant = ac->variant;
  1390. struct artpec6_crypto_req_common *common;
  1391. bool cipher_decr = false;
  1392. size_t cipher_klen;
  1393. u32 cipher_len = 0; /* Same as regk_crypto_key_128 for NULL crypto */
  1394. u32 oper;
  1395. req_ctx = skcipher_request_ctx(areq);
  1396. common = &req_ctx->common;
  1397. artpec6_crypto_init_dma_operation(common);
  1398. if (variant == ARTPEC6_CRYPTO)
  1399. ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER, a6_regk_crypto_dlkey);
  1400. else
  1401. ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER, a7_regk_crypto_dlkey);
  1402. ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
  1403. sizeof(ctx->key_md), false, false);
  1404. if (ret)
  1405. return ret;
  1406. ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
  1407. ctx->key_length, true, false);
  1408. if (ret)
  1409. return ret;
  1410. req_ctx->cipher_md = 0;
  1411. if (ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_XTS)
  1412. cipher_klen = ctx->key_length/2;
  1413. else
  1414. cipher_klen = ctx->key_length;
  1415. /* Metadata */
  1416. switch (cipher_klen) {
  1417. case 16:
  1418. cipher_len = regk_crypto_key_128;
  1419. break;
  1420. case 24:
  1421. cipher_len = regk_crypto_key_192;
  1422. break;
  1423. case 32:
  1424. cipher_len = regk_crypto_key_256;
  1425. break;
  1426. default:
  1427. pr_err("%s: Invalid key length %zu!\n",
  1428. MODULE_NAME, ctx->key_length);
  1429. return -EINVAL;
  1430. }
  1431. switch (ctx->crypto_type) {
  1432. case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
  1433. oper = regk_crypto_aes_ecb;
  1434. cipher_decr = req_ctx->decrypt;
  1435. break;
  1436. case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
  1437. oper = regk_crypto_aes_cbc;
  1438. cipher_decr = req_ctx->decrypt;
  1439. break;
  1440. case ARTPEC6_CRYPTO_CIPHER_AES_CTR:
  1441. oper = regk_crypto_aes_ctr;
  1442. cipher_decr = false;
  1443. break;
  1444. case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
  1445. oper = regk_crypto_aes_xts;
  1446. cipher_decr = req_ctx->decrypt;
  1447. if (variant == ARTPEC6_CRYPTO)
  1448. req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DSEQ;
  1449. else
  1450. req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DSEQ;
  1451. break;
  1452. default:
  1453. pr_err("%s: Invalid cipher mode %d!\n",
  1454. MODULE_NAME, ctx->crypto_type);
  1455. return -EINVAL;
  1456. }
  1457. if (variant == ARTPEC6_CRYPTO) {
  1458. req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_OPER, oper);
  1459. req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_CIPHER_LEN,
  1460. cipher_len);
  1461. if (cipher_decr)
  1462. req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
  1463. } else {
  1464. req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_OPER, oper);
  1465. req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_CIPHER_LEN,
  1466. cipher_len);
  1467. if (cipher_decr)
  1468. req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
  1469. }
  1470. ret = artpec6_crypto_setup_out_descr(common,
  1471. &req_ctx->cipher_md,
  1472. sizeof(req_ctx->cipher_md),
  1473. false, false);
  1474. if (ret)
  1475. return ret;
  1476. ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
  1477. if (ret)
  1478. return ret;
  1479. if (iv_len) {
  1480. ret = artpec6_crypto_setup_out_descr(common, areq->iv, iv_len,
  1481. false, false);
  1482. if (ret)
  1483. return ret;
  1484. }
  1485. /* Data out */
  1486. artpec6_crypto_walk_init(&walk, areq->src);
  1487. ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, areq->cryptlen);
  1488. if (ret)
  1489. return ret;
  1490. /* Data in */
  1491. artpec6_crypto_walk_init(&walk, areq->dst);
  1492. ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, areq->cryptlen);
  1493. if (ret)
  1494. return ret;
  1495. /* CTR-mode padding required by the HW. */
  1496. if (ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_CTR ||
  1497. ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_XTS) {
  1498. size_t pad = ALIGN(areq->cryptlen, AES_BLOCK_SIZE) -
  1499. areq->cryptlen;
  1500. if (pad) {
  1501. ret = artpec6_crypto_setup_out_descr(common,
  1502. ac->pad_buffer,
  1503. pad, false, false);
  1504. if (ret)
  1505. return ret;
  1506. ret = artpec6_crypto_setup_in_descr(common,
  1507. ac->pad_buffer, pad,
  1508. false);
  1509. if (ret)
  1510. return ret;
  1511. }
  1512. }
  1513. ret = artpec6_crypto_terminate_out_descrs(common);
  1514. if (ret)
  1515. return ret;
  1516. ret = artpec6_crypto_terminate_in_descrs(common);
  1517. if (ret)
  1518. return ret;
  1519. return artpec6_crypto_dma_map_descs(common);
  1520. }
  1521. static int artpec6_crypto_prepare_aead(struct aead_request *areq)
  1522. {
  1523. size_t count;
  1524. int ret;
  1525. size_t input_length;
  1526. struct artpec6_cryptotfm_context *ctx = crypto_tfm_ctx(areq->base.tfm);
  1527. struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(areq);
  1528. struct crypto_aead *cipher = crypto_aead_reqtfm(areq);
  1529. struct artpec6_crypto_req_common *common = &req_ctx->common;
  1530. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  1531. enum artpec6_crypto_variant variant = ac->variant;
  1532. u32 md_cipher_len;
  1533. artpec6_crypto_init_dma_operation(common);
  1534. /* Key */
  1535. if (variant == ARTPEC6_CRYPTO) {
  1536. ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER,
  1537. a6_regk_crypto_dlkey);
  1538. } else {
  1539. ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
  1540. a7_regk_crypto_dlkey);
  1541. }
  1542. ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
  1543. sizeof(ctx->key_md), false, false);
  1544. if (ret)
  1545. return ret;
  1546. ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
  1547. ctx->key_length, true, false);
  1548. if (ret)
  1549. return ret;
  1550. req_ctx->cipher_md = 0;
  1551. switch (ctx->key_length) {
  1552. case 16:
  1553. md_cipher_len = regk_crypto_key_128;
  1554. break;
  1555. case 24:
  1556. md_cipher_len = regk_crypto_key_192;
  1557. break;
  1558. case 32:
  1559. md_cipher_len = regk_crypto_key_256;
  1560. break;
  1561. default:
  1562. return -EINVAL;
  1563. }
  1564. if (variant == ARTPEC6_CRYPTO) {
  1565. req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_OPER,
  1566. regk_crypto_aes_gcm);
  1567. req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_CIPHER_LEN,
  1568. md_cipher_len);
  1569. if (req_ctx->decrypt)
  1570. req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
  1571. } else {
  1572. req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_OPER,
  1573. regk_crypto_aes_gcm);
  1574. req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_CIPHER_LEN,
  1575. md_cipher_len);
  1576. if (req_ctx->decrypt)
  1577. req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
  1578. }
  1579. ret = artpec6_crypto_setup_out_descr(common,
  1580. (void *) &req_ctx->cipher_md,
  1581. sizeof(req_ctx->cipher_md), false,
  1582. false);
  1583. if (ret)
  1584. return ret;
  1585. ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
  1586. if (ret)
  1587. return ret;
  1588. /* For the decryption, cryptlen includes the tag. */
  1589. input_length = areq->cryptlen;
  1590. if (req_ctx->decrypt)
  1591. input_length -= crypto_aead_authsize(cipher);
  1592. /* Prepare the context buffer */
  1593. req_ctx->hw_ctx.aad_length_bits =
  1594. __cpu_to_be64(8*areq->assoclen);
  1595. req_ctx->hw_ctx.text_length_bits =
  1596. __cpu_to_be64(8*input_length);
  1597. memcpy(req_ctx->hw_ctx.J0, areq->iv, crypto_aead_ivsize(cipher));
  1598. // The HW omits the initial increment of the counter field.
  1599. memcpy(req_ctx->hw_ctx.J0 + GCM_AES_IV_SIZE, "\x00\x00\x00\x01", 4);
  1600. ret = artpec6_crypto_setup_out_descr(common, &req_ctx->hw_ctx,
  1601. sizeof(struct artpec6_crypto_aead_hw_ctx), false, false);
  1602. if (ret)
  1603. return ret;
  1604. {
  1605. struct artpec6_crypto_walk walk;
  1606. artpec6_crypto_walk_init(&walk, areq->src);
  1607. /* Associated data */
  1608. count = areq->assoclen;
  1609. ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
  1610. if (ret)
  1611. return ret;
  1612. if (!IS_ALIGNED(areq->assoclen, 16)) {
  1613. size_t assoc_pad = 16 - (areq->assoclen % 16);
  1614. /* The HW mandates zero padding here */
  1615. ret = artpec6_crypto_setup_out_descr(common,
  1616. ac->zero_buffer,
  1617. assoc_pad, false,
  1618. false);
  1619. if (ret)
  1620. return ret;
  1621. }
  1622. /* Data to crypto */
  1623. count = input_length;
  1624. ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
  1625. if (ret)
  1626. return ret;
  1627. if (!IS_ALIGNED(input_length, 16)) {
  1628. size_t crypto_pad = 16 - (input_length % 16);
  1629. /* The HW mandates zero padding here */
  1630. ret = artpec6_crypto_setup_out_descr(common,
  1631. ac->zero_buffer,
  1632. crypto_pad,
  1633. false,
  1634. false);
  1635. if (ret)
  1636. return ret;
  1637. }
  1638. }
  1639. /* Data from crypto */
  1640. {
  1641. struct artpec6_crypto_walk walk;
  1642. size_t output_len = areq->cryptlen;
  1643. if (req_ctx->decrypt)
  1644. output_len -= crypto_aead_authsize(cipher);
  1645. artpec6_crypto_walk_init(&walk, areq->dst);
  1646. /* skip associated data in the output */
  1647. count = artpec6_crypto_walk_advance(&walk, areq->assoclen);
  1648. if (count)
  1649. return -EINVAL;
  1650. count = output_len;
  1651. ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, count);
  1652. if (ret)
  1653. return ret;
  1654. /* Put padding between the cryptotext and the auth tag */
  1655. if (!IS_ALIGNED(output_len, 16)) {
  1656. size_t crypto_pad = 16 - (output_len % 16);
  1657. ret = artpec6_crypto_setup_in_descr(common,
  1658. ac->pad_buffer,
  1659. crypto_pad, false);
  1660. if (ret)
  1661. return ret;
  1662. }
  1663. /* The authentication tag shall follow immediately after
  1664. * the output ciphertext. For decryption it is put in a context
  1665. * buffer for later compare against the input tag.
  1666. */
  1667. if (req_ctx->decrypt) {
  1668. ret = artpec6_crypto_setup_in_descr(common,
  1669. req_ctx->decryption_tag, AES_BLOCK_SIZE, false);
  1670. if (ret)
  1671. return ret;
  1672. } else {
  1673. /* For encryption the requested tag size may be smaller
  1674. * than the hardware's generated tag.
  1675. */
  1676. size_t authsize = crypto_aead_authsize(cipher);
  1677. ret = artpec6_crypto_setup_sg_descrs_in(common, &walk,
  1678. authsize);
  1679. if (ret)
  1680. return ret;
  1681. if (authsize < AES_BLOCK_SIZE) {
  1682. count = AES_BLOCK_SIZE - authsize;
  1683. ret = artpec6_crypto_setup_in_descr(common,
  1684. ac->pad_buffer,
  1685. count, false);
  1686. if (ret)
  1687. return ret;
  1688. }
  1689. }
  1690. }
  1691. ret = artpec6_crypto_terminate_in_descrs(common);
  1692. if (ret)
  1693. return ret;
  1694. ret = artpec6_crypto_terminate_out_descrs(common);
  1695. if (ret)
  1696. return ret;
  1697. return artpec6_crypto_dma_map_descs(common);
  1698. }
  1699. static void artpec6_crypto_process_queue(struct artpec6_crypto *ac,
  1700. struct list_head *completions)
  1701. {
  1702. struct artpec6_crypto_req_common *req;
  1703. while (!list_empty(&ac->queue) && !artpec6_crypto_busy()) {
  1704. req = list_first_entry(&ac->queue,
  1705. struct artpec6_crypto_req_common,
  1706. list);
  1707. list_move_tail(&req->list, &ac->pending);
  1708. artpec6_crypto_start_dma(req);
  1709. list_add_tail(&req->complete_in_progress, completions);
  1710. }
  1711. /*
  1712. * In some cases, the hardware can raise an in_eop_flush interrupt
  1713. * before actually updating the status, so we have an timer which will
  1714. * recheck the status on timeout. Since the cases are expected to be
  1715. * very rare, we use a relatively large timeout value. There should be
  1716. * no noticeable negative effect if we timeout spuriously.
  1717. */
  1718. if (ac->pending_count)
  1719. mod_timer(&ac->timer, jiffies + msecs_to_jiffies(100));
  1720. else
  1721. timer_delete(&ac->timer);
  1722. }
  1723. static void artpec6_crypto_timeout(struct timer_list *t)
  1724. {
  1725. struct artpec6_crypto *ac = timer_container_of(ac, t, timer);
  1726. dev_info_ratelimited(artpec6_crypto_dev, "timeout\n");
  1727. tasklet_schedule(&ac->task);
  1728. }
  1729. static void artpec6_crypto_task(unsigned long data)
  1730. {
  1731. struct artpec6_crypto *ac = (struct artpec6_crypto *)data;
  1732. struct artpec6_crypto_req_common *req;
  1733. struct artpec6_crypto_req_common *n;
  1734. struct list_head complete_done;
  1735. struct list_head complete_in_progress;
  1736. INIT_LIST_HEAD(&complete_done);
  1737. INIT_LIST_HEAD(&complete_in_progress);
  1738. if (list_empty(&ac->pending)) {
  1739. pr_debug("Spurious IRQ\n");
  1740. return;
  1741. }
  1742. spin_lock(&ac->queue_lock);
  1743. list_for_each_entry_safe(req, n, &ac->pending, list) {
  1744. struct artpec6_crypto_dma_descriptors *dma = req->dma;
  1745. u32 stat;
  1746. dma_addr_t stataddr;
  1747. stataddr = dma->stat_dma_addr + 4 * (req->dma->in_cnt - 1);
  1748. dma_sync_single_for_cpu(artpec6_crypto_dev,
  1749. stataddr,
  1750. 4,
  1751. DMA_BIDIRECTIONAL);
  1752. stat = req->dma->stat[req->dma->in_cnt-1];
  1753. /* A non-zero final status descriptor indicates
  1754. * this job has finished.
  1755. */
  1756. pr_debug("Request %p status is %X\n", req, stat);
  1757. if (!stat)
  1758. break;
  1759. /* Allow testing of timeout handling with fault injection */
  1760. #ifdef CONFIG_FAULT_INJECTION
  1761. if (should_fail(&artpec6_crypto_fail_status_read, 1))
  1762. continue;
  1763. #endif
  1764. pr_debug("Completing request %p\n", req);
  1765. list_move_tail(&req->list, &complete_done);
  1766. ac->pending_count--;
  1767. }
  1768. artpec6_crypto_process_queue(ac, &complete_in_progress);
  1769. spin_unlock(&ac->queue_lock);
  1770. /* Perform the completion callbacks without holding the queue lock
  1771. * to allow new request submissions from the callbacks.
  1772. */
  1773. list_for_each_entry_safe(req, n, &complete_done, list) {
  1774. artpec6_crypto_dma_unmap_all(req);
  1775. artpec6_crypto_copy_bounce_buffers(req);
  1776. artpec6_crypto_common_destroy(req);
  1777. req->complete(req->req);
  1778. }
  1779. list_for_each_entry_safe(req, n, &complete_in_progress,
  1780. complete_in_progress) {
  1781. crypto_request_complete(req->req, -EINPROGRESS);
  1782. }
  1783. }
  1784. static void artpec6_crypto_complete_crypto(struct crypto_async_request *req)
  1785. {
  1786. crypto_request_complete(req, 0);
  1787. }
  1788. static void
  1789. artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req)
  1790. {
  1791. struct skcipher_request *cipher_req = container_of(req,
  1792. struct skcipher_request, base);
  1793. scatterwalk_map_and_copy(cipher_req->iv, cipher_req->src,
  1794. cipher_req->cryptlen - AES_BLOCK_SIZE,
  1795. AES_BLOCK_SIZE, 0);
  1796. skcipher_request_complete(cipher_req, 0);
  1797. }
  1798. static void
  1799. artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req)
  1800. {
  1801. struct skcipher_request *cipher_req = container_of(req,
  1802. struct skcipher_request, base);
  1803. scatterwalk_map_and_copy(cipher_req->iv, cipher_req->dst,
  1804. cipher_req->cryptlen - AES_BLOCK_SIZE,
  1805. AES_BLOCK_SIZE, 0);
  1806. skcipher_request_complete(cipher_req, 0);
  1807. }
  1808. static void artpec6_crypto_complete_aead(struct crypto_async_request *req)
  1809. {
  1810. int result = 0;
  1811. /* Verify GCM hashtag. */
  1812. struct aead_request *areq = container_of(req,
  1813. struct aead_request, base);
  1814. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  1815. struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(areq);
  1816. if (req_ctx->decrypt) {
  1817. u8 input_tag[AES_BLOCK_SIZE];
  1818. unsigned int authsize = crypto_aead_authsize(aead);
  1819. sg_pcopy_to_buffer(areq->src,
  1820. sg_nents(areq->src),
  1821. input_tag,
  1822. authsize,
  1823. areq->assoclen + areq->cryptlen -
  1824. authsize);
  1825. if (crypto_memneq(req_ctx->decryption_tag,
  1826. input_tag,
  1827. authsize)) {
  1828. pr_debug("***EBADMSG:\n");
  1829. print_hex_dump_debug("ref:", DUMP_PREFIX_ADDRESS, 32, 1,
  1830. input_tag, authsize, true);
  1831. print_hex_dump_debug("out:", DUMP_PREFIX_ADDRESS, 32, 1,
  1832. req_ctx->decryption_tag,
  1833. authsize, true);
  1834. result = -EBADMSG;
  1835. }
  1836. }
  1837. aead_request_complete(areq, result);
  1838. }
  1839. static void artpec6_crypto_complete_hash(struct crypto_async_request *req)
  1840. {
  1841. crypto_request_complete(req, 0);
  1842. }
  1843. /*------------------- Hash functions -----------------------------------------*/
  1844. static int
  1845. artpec6_crypto_hash_set_key(struct crypto_ahash *tfm,
  1846. const u8 *key, unsigned int keylen)
  1847. {
  1848. struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(&tfm->base);
  1849. size_t blocksize;
  1850. int ret;
  1851. if (!keylen) {
  1852. pr_err("Invalid length (%d) of HMAC key\n",
  1853. keylen);
  1854. return -EINVAL;
  1855. }
  1856. memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
  1857. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1858. if (keylen > blocksize) {
  1859. tfm_ctx->hmac_key_length = blocksize;
  1860. ret = crypto_shash_tfm_digest(tfm_ctx->child_hash, key, keylen,
  1861. tfm_ctx->hmac_key);
  1862. if (ret)
  1863. return ret;
  1864. } else {
  1865. memcpy(tfm_ctx->hmac_key, key, keylen);
  1866. tfm_ctx->hmac_key_length = keylen;
  1867. }
  1868. return 0;
  1869. }
  1870. static int
  1871. artpec6_crypto_init_hash(struct ahash_request *req, u8 type, int hmac)
  1872. {
  1873. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  1874. enum artpec6_crypto_variant variant = ac->variant;
  1875. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1876. u32 oper;
  1877. memset(req_ctx, 0, sizeof(*req_ctx));
  1878. req_ctx->hash_flags = HASH_FLAG_INIT_CTX;
  1879. if (hmac)
  1880. req_ctx->hash_flags |= (HASH_FLAG_HMAC | HASH_FLAG_UPDATE_KEY);
  1881. switch (type) {
  1882. case ARTPEC6_CRYPTO_HASH_SHA1:
  1883. oper = hmac ? regk_crypto_hmac_sha1 : regk_crypto_sha1;
  1884. break;
  1885. case ARTPEC6_CRYPTO_HASH_SHA256:
  1886. oper = hmac ? regk_crypto_hmac_sha256 : regk_crypto_sha256;
  1887. break;
  1888. default:
  1889. pr_err("%s: Unsupported hash type 0x%x\n", MODULE_NAME, type);
  1890. return -EINVAL;
  1891. }
  1892. if (variant == ARTPEC6_CRYPTO)
  1893. req_ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, oper);
  1894. else
  1895. req_ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, oper);
  1896. return 0;
  1897. }
  1898. static int artpec6_crypto_prepare_submit_hash(struct ahash_request *req)
  1899. {
  1900. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1901. int ret;
  1902. if (!req_ctx->common.dma) {
  1903. ret = artpec6_crypto_common_init(&req_ctx->common,
  1904. &req->base,
  1905. artpec6_crypto_complete_hash,
  1906. NULL, 0);
  1907. if (ret)
  1908. return ret;
  1909. }
  1910. ret = artpec6_crypto_prepare_hash(req);
  1911. switch (ret) {
  1912. case ARTPEC6_CRYPTO_PREPARE_HASH_START:
  1913. ret = artpec6_crypto_submit(&req_ctx->common);
  1914. break;
  1915. case ARTPEC6_CRYPTO_PREPARE_HASH_NO_START:
  1916. ret = 0;
  1917. fallthrough;
  1918. default:
  1919. artpec6_crypto_common_destroy(&req_ctx->common);
  1920. break;
  1921. }
  1922. return ret;
  1923. }
  1924. static int artpec6_crypto_hash_final(struct ahash_request *req)
  1925. {
  1926. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1927. req_ctx->hash_flags |= HASH_FLAG_FINALIZE;
  1928. return artpec6_crypto_prepare_submit_hash(req);
  1929. }
  1930. static int artpec6_crypto_hash_update(struct ahash_request *req)
  1931. {
  1932. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1933. req_ctx->hash_flags |= HASH_FLAG_UPDATE;
  1934. return artpec6_crypto_prepare_submit_hash(req);
  1935. }
  1936. static int artpec6_crypto_sha1_init(struct ahash_request *req)
  1937. {
  1938. return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
  1939. }
  1940. static int artpec6_crypto_sha1_digest(struct ahash_request *req)
  1941. {
  1942. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1943. artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
  1944. req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
  1945. return artpec6_crypto_prepare_submit_hash(req);
  1946. }
  1947. static int artpec6_crypto_sha256_init(struct ahash_request *req)
  1948. {
  1949. return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
  1950. }
  1951. static int artpec6_crypto_sha256_digest(struct ahash_request *req)
  1952. {
  1953. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1954. artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
  1955. req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
  1956. return artpec6_crypto_prepare_submit_hash(req);
  1957. }
  1958. static int artpec6_crypto_hmac_sha256_init(struct ahash_request *req)
  1959. {
  1960. return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
  1961. }
  1962. static int artpec6_crypto_hmac_sha256_digest(struct ahash_request *req)
  1963. {
  1964. struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
  1965. artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
  1966. req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
  1967. return artpec6_crypto_prepare_submit_hash(req);
  1968. }
  1969. static int artpec6_crypto_ahash_init_common(struct crypto_tfm *tfm,
  1970. const char *base_hash_name)
  1971. {
  1972. struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
  1973. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1974. sizeof(struct artpec6_hash_request_context));
  1975. memset(tfm_ctx, 0, sizeof(*tfm_ctx));
  1976. if (base_hash_name) {
  1977. struct crypto_shash *child;
  1978. child = crypto_alloc_shash(base_hash_name, 0,
  1979. CRYPTO_ALG_NEED_FALLBACK);
  1980. if (IS_ERR(child))
  1981. return PTR_ERR(child);
  1982. tfm_ctx->child_hash = child;
  1983. }
  1984. return 0;
  1985. }
  1986. static int artpec6_crypto_ahash_init(struct crypto_tfm *tfm)
  1987. {
  1988. return artpec6_crypto_ahash_init_common(tfm, NULL);
  1989. }
  1990. static int artpec6_crypto_ahash_init_hmac_sha256(struct crypto_tfm *tfm)
  1991. {
  1992. return artpec6_crypto_ahash_init_common(tfm, "sha256");
  1993. }
  1994. static void artpec6_crypto_ahash_exit(struct crypto_tfm *tfm)
  1995. {
  1996. struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
  1997. if (tfm_ctx->child_hash)
  1998. crypto_free_shash(tfm_ctx->child_hash);
  1999. memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
  2000. tfm_ctx->hmac_key_length = 0;
  2001. }
  2002. static int artpec6_crypto_hash_export(struct ahash_request *req, void *out)
  2003. {
  2004. const struct artpec6_hash_request_context *ctx = ahash_request_ctx(req);
  2005. struct artpec6_hash_export_state *state = out;
  2006. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  2007. enum artpec6_crypto_variant variant = ac->variant;
  2008. BUILD_BUG_ON(sizeof(state->partial_buffer) !=
  2009. sizeof(ctx->partial_buffer));
  2010. BUILD_BUG_ON(sizeof(state->digeststate) != sizeof(ctx->digeststate));
  2011. state->digcnt = ctx->digcnt;
  2012. state->partial_bytes = ctx->partial_bytes;
  2013. state->hash_flags = ctx->hash_flags;
  2014. if (variant == ARTPEC6_CRYPTO)
  2015. state->oper = FIELD_GET(A6_CRY_MD_OPER, ctx->hash_md);
  2016. else
  2017. state->oper = FIELD_GET(A7_CRY_MD_OPER, ctx->hash_md);
  2018. memcpy(state->partial_buffer, ctx->partial_buffer,
  2019. sizeof(state->partial_buffer));
  2020. memcpy(state->digeststate, ctx->digeststate,
  2021. sizeof(state->digeststate));
  2022. return 0;
  2023. }
  2024. static int artpec6_crypto_hash_import(struct ahash_request *req, const void *in)
  2025. {
  2026. struct artpec6_hash_request_context *ctx = ahash_request_ctx(req);
  2027. const struct artpec6_hash_export_state *state = in;
  2028. struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
  2029. enum artpec6_crypto_variant variant = ac->variant;
  2030. memset(ctx, 0, sizeof(*ctx));
  2031. ctx->digcnt = state->digcnt;
  2032. ctx->partial_bytes = state->partial_bytes;
  2033. ctx->hash_flags = state->hash_flags;
  2034. if (variant == ARTPEC6_CRYPTO)
  2035. ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, state->oper);
  2036. else
  2037. ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, state->oper);
  2038. memcpy(ctx->partial_buffer, state->partial_buffer,
  2039. sizeof(state->partial_buffer));
  2040. memcpy(ctx->digeststate, state->digeststate,
  2041. sizeof(state->digeststate));
  2042. return 0;
  2043. }
  2044. static int init_crypto_hw(struct artpec6_crypto *ac)
  2045. {
  2046. enum artpec6_crypto_variant variant = ac->variant;
  2047. void __iomem *base = ac->base;
  2048. u32 out_descr_buf_size;
  2049. u32 out_data_buf_size;
  2050. u32 in_data_buf_size;
  2051. u32 in_descr_buf_size;
  2052. u32 in_stat_buf_size;
  2053. u32 in, out;
  2054. /*
  2055. * The PDMA unit contains 1984 bytes of internal memory for the OUT
  2056. * channels and 1024 bytes for the IN channel. This is an elastic
  2057. * memory used to internally store the descriptors and data. The values
  2058. * ares specified in 64 byte incremements. Trustzone buffers are not
  2059. * used at this stage.
  2060. */
  2061. out_data_buf_size = 16; /* 1024 bytes for data */
  2062. out_descr_buf_size = 15; /* 960 bytes for descriptors */
  2063. in_data_buf_size = 8; /* 512 bytes for data */
  2064. in_descr_buf_size = 4; /* 256 bytes for descriptors */
  2065. in_stat_buf_size = 4; /* 256 bytes for stat descrs */
  2066. BUILD_BUG_ON_MSG((out_data_buf_size
  2067. + out_descr_buf_size) * 64 > 1984,
  2068. "Invalid OUT configuration");
  2069. BUILD_BUG_ON_MSG((in_data_buf_size
  2070. + in_descr_buf_size
  2071. + in_stat_buf_size) * 64 > 1024,
  2072. "Invalid IN configuration");
  2073. in = FIELD_PREP(PDMA_IN_BUF_CFG_DATA_BUF_SIZE, in_data_buf_size) |
  2074. FIELD_PREP(PDMA_IN_BUF_CFG_DESCR_BUF_SIZE, in_descr_buf_size) |
  2075. FIELD_PREP(PDMA_IN_BUF_CFG_STAT_BUF_SIZE, in_stat_buf_size);
  2076. out = FIELD_PREP(PDMA_OUT_BUF_CFG_DATA_BUF_SIZE, out_data_buf_size) |
  2077. FIELD_PREP(PDMA_OUT_BUF_CFG_DESCR_BUF_SIZE, out_descr_buf_size);
  2078. writel_relaxed(out, base + PDMA_OUT_BUF_CFG);
  2079. writel_relaxed(PDMA_OUT_CFG_EN, base + PDMA_OUT_CFG);
  2080. if (variant == ARTPEC6_CRYPTO) {
  2081. writel_relaxed(in, base + A6_PDMA_IN_BUF_CFG);
  2082. writel_relaxed(PDMA_IN_CFG_EN, base + A6_PDMA_IN_CFG);
  2083. writel_relaxed(A6_PDMA_INTR_MASK_IN_DATA |
  2084. A6_PDMA_INTR_MASK_IN_EOP_FLUSH,
  2085. base + A6_PDMA_INTR_MASK);
  2086. } else {
  2087. writel_relaxed(in, base + A7_PDMA_IN_BUF_CFG);
  2088. writel_relaxed(PDMA_IN_CFG_EN, base + A7_PDMA_IN_CFG);
  2089. writel_relaxed(A7_PDMA_INTR_MASK_IN_DATA |
  2090. A7_PDMA_INTR_MASK_IN_EOP_FLUSH,
  2091. base + A7_PDMA_INTR_MASK);
  2092. }
  2093. return 0;
  2094. }
  2095. static void artpec6_crypto_disable_hw(struct artpec6_crypto *ac)
  2096. {
  2097. enum artpec6_crypto_variant variant = ac->variant;
  2098. void __iomem *base = ac->base;
  2099. if (variant == ARTPEC6_CRYPTO) {
  2100. writel_relaxed(A6_PDMA_IN_CMD_STOP, base + A6_PDMA_IN_CMD);
  2101. writel_relaxed(0, base + A6_PDMA_IN_CFG);
  2102. writel_relaxed(A6_PDMA_OUT_CMD_STOP, base + PDMA_OUT_CMD);
  2103. } else {
  2104. writel_relaxed(A7_PDMA_IN_CMD_STOP, base + A7_PDMA_IN_CMD);
  2105. writel_relaxed(0, base + A7_PDMA_IN_CFG);
  2106. writel_relaxed(A7_PDMA_OUT_CMD_STOP, base + PDMA_OUT_CMD);
  2107. }
  2108. writel_relaxed(0, base + PDMA_OUT_CFG);
  2109. }
  2110. static irqreturn_t artpec6_crypto_irq(int irq, void *dev_id)
  2111. {
  2112. struct artpec6_crypto *ac = dev_id;
  2113. enum artpec6_crypto_variant variant = ac->variant;
  2114. void __iomem *base = ac->base;
  2115. u32 mask_in_data, mask_in_eop_flush;
  2116. u32 in_cmd_flush_stat, in_cmd_reg;
  2117. u32 ack_intr_reg;
  2118. u32 ack = 0;
  2119. u32 intr;
  2120. if (variant == ARTPEC6_CRYPTO) {
  2121. intr = readl_relaxed(base + A6_PDMA_MASKED_INTR);
  2122. mask_in_data = A6_PDMA_INTR_MASK_IN_DATA;
  2123. mask_in_eop_flush = A6_PDMA_INTR_MASK_IN_EOP_FLUSH;
  2124. in_cmd_flush_stat = A6_PDMA_IN_CMD_FLUSH_STAT;
  2125. in_cmd_reg = A6_PDMA_IN_CMD;
  2126. ack_intr_reg = A6_PDMA_ACK_INTR;
  2127. } else {
  2128. intr = readl_relaxed(base + A7_PDMA_MASKED_INTR);
  2129. mask_in_data = A7_PDMA_INTR_MASK_IN_DATA;
  2130. mask_in_eop_flush = A7_PDMA_INTR_MASK_IN_EOP_FLUSH;
  2131. in_cmd_flush_stat = A7_PDMA_IN_CMD_FLUSH_STAT;
  2132. in_cmd_reg = A7_PDMA_IN_CMD;
  2133. ack_intr_reg = A7_PDMA_ACK_INTR;
  2134. }
  2135. /* We get two interrupt notifications from each job.
  2136. * The in_data means all data was sent to memory and then
  2137. * we request a status flush command to write the per-job
  2138. * status to its status vector. This ensures that the
  2139. * tasklet can detect exactly how many submitted jobs
  2140. * that have finished.
  2141. */
  2142. if (intr & mask_in_data)
  2143. ack |= mask_in_data;
  2144. if (intr & mask_in_eop_flush)
  2145. ack |= mask_in_eop_flush;
  2146. else
  2147. writel_relaxed(in_cmd_flush_stat, base + in_cmd_reg);
  2148. writel_relaxed(ack, base + ack_intr_reg);
  2149. if (intr & mask_in_eop_flush)
  2150. tasklet_schedule(&ac->task);
  2151. return IRQ_HANDLED;
  2152. }
  2153. /*------------------- Algorithm definitions ----------------------------------*/
  2154. /* Hashes */
  2155. static struct ahash_alg hash_algos[] = {
  2156. /* SHA-1 */
  2157. {
  2158. .init = artpec6_crypto_sha1_init,
  2159. .update = artpec6_crypto_hash_update,
  2160. .final = artpec6_crypto_hash_final,
  2161. .digest = artpec6_crypto_sha1_digest,
  2162. .import = artpec6_crypto_hash_import,
  2163. .export = artpec6_crypto_hash_export,
  2164. .halg.digestsize = SHA1_DIGEST_SIZE,
  2165. .halg.statesize = sizeof(struct artpec6_hash_export_state),
  2166. .halg.base = {
  2167. .cra_name = "sha1",
  2168. .cra_driver_name = "artpec-sha1",
  2169. .cra_priority = 300,
  2170. .cra_flags = CRYPTO_ALG_ASYNC |
  2171. CRYPTO_ALG_ALLOCATES_MEMORY,
  2172. .cra_blocksize = SHA1_BLOCK_SIZE,
  2173. .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
  2174. .cra_module = THIS_MODULE,
  2175. .cra_init = artpec6_crypto_ahash_init,
  2176. .cra_exit = artpec6_crypto_ahash_exit,
  2177. }
  2178. },
  2179. /* SHA-256 */
  2180. {
  2181. .init = artpec6_crypto_sha256_init,
  2182. .update = artpec6_crypto_hash_update,
  2183. .final = artpec6_crypto_hash_final,
  2184. .digest = artpec6_crypto_sha256_digest,
  2185. .import = artpec6_crypto_hash_import,
  2186. .export = artpec6_crypto_hash_export,
  2187. .halg.digestsize = SHA256_DIGEST_SIZE,
  2188. .halg.statesize = sizeof(struct artpec6_hash_export_state),
  2189. .halg.base = {
  2190. .cra_name = "sha256",
  2191. .cra_driver_name = "artpec-sha256",
  2192. .cra_priority = 300,
  2193. .cra_flags = CRYPTO_ALG_ASYNC |
  2194. CRYPTO_ALG_ALLOCATES_MEMORY,
  2195. .cra_blocksize = SHA256_BLOCK_SIZE,
  2196. .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
  2197. .cra_module = THIS_MODULE,
  2198. .cra_init = artpec6_crypto_ahash_init,
  2199. .cra_exit = artpec6_crypto_ahash_exit,
  2200. }
  2201. },
  2202. /* HMAC SHA-256 */
  2203. {
  2204. .init = artpec6_crypto_hmac_sha256_init,
  2205. .update = artpec6_crypto_hash_update,
  2206. .final = artpec6_crypto_hash_final,
  2207. .digest = artpec6_crypto_hmac_sha256_digest,
  2208. .import = artpec6_crypto_hash_import,
  2209. .export = artpec6_crypto_hash_export,
  2210. .setkey = artpec6_crypto_hash_set_key,
  2211. .halg.digestsize = SHA256_DIGEST_SIZE,
  2212. .halg.statesize = sizeof(struct artpec6_hash_export_state),
  2213. .halg.base = {
  2214. .cra_name = "hmac(sha256)",
  2215. .cra_driver_name = "artpec-hmac-sha256",
  2216. .cra_priority = 300,
  2217. .cra_flags = CRYPTO_ALG_ASYNC |
  2218. CRYPTO_ALG_ALLOCATES_MEMORY,
  2219. .cra_blocksize = SHA256_BLOCK_SIZE,
  2220. .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
  2221. .cra_module = THIS_MODULE,
  2222. .cra_init = artpec6_crypto_ahash_init_hmac_sha256,
  2223. .cra_exit = artpec6_crypto_ahash_exit,
  2224. }
  2225. },
  2226. };
  2227. /* Crypto */
  2228. static struct skcipher_alg crypto_algos[] = {
  2229. /* AES - ECB */
  2230. {
  2231. .base = {
  2232. .cra_name = "ecb(aes)",
  2233. .cra_driver_name = "artpec6-ecb-aes",
  2234. .cra_priority = 300,
  2235. .cra_flags = CRYPTO_ALG_ASYNC |
  2236. CRYPTO_ALG_ALLOCATES_MEMORY,
  2237. .cra_blocksize = AES_BLOCK_SIZE,
  2238. .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
  2239. .cra_alignmask = 3,
  2240. .cra_module = THIS_MODULE,
  2241. },
  2242. .min_keysize = AES_MIN_KEY_SIZE,
  2243. .max_keysize = AES_MAX_KEY_SIZE,
  2244. .setkey = artpec6_crypto_cipher_set_key,
  2245. .encrypt = artpec6_crypto_encrypt,
  2246. .decrypt = artpec6_crypto_decrypt,
  2247. .init = artpec6_crypto_aes_ecb_init,
  2248. .exit = artpec6_crypto_aes_exit,
  2249. },
  2250. /* AES - CTR */
  2251. {
  2252. .base = {
  2253. .cra_name = "ctr(aes)",
  2254. .cra_driver_name = "artpec6-ctr-aes",
  2255. .cra_priority = 300,
  2256. .cra_flags = CRYPTO_ALG_ASYNC |
  2257. CRYPTO_ALG_ALLOCATES_MEMORY |
  2258. CRYPTO_ALG_NEED_FALLBACK,
  2259. .cra_blocksize = 1,
  2260. .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
  2261. .cra_alignmask = 3,
  2262. .cra_module = THIS_MODULE,
  2263. },
  2264. .min_keysize = AES_MIN_KEY_SIZE,
  2265. .max_keysize = AES_MAX_KEY_SIZE,
  2266. .ivsize = AES_BLOCK_SIZE,
  2267. .setkey = artpec6_crypto_cipher_set_key,
  2268. .encrypt = artpec6_crypto_ctr_encrypt,
  2269. .decrypt = artpec6_crypto_ctr_decrypt,
  2270. .init = artpec6_crypto_aes_ctr_init,
  2271. .exit = artpec6_crypto_aes_ctr_exit,
  2272. },
  2273. /* AES - CBC */
  2274. {
  2275. .base = {
  2276. .cra_name = "cbc(aes)",
  2277. .cra_driver_name = "artpec6-cbc-aes",
  2278. .cra_priority = 300,
  2279. .cra_flags = CRYPTO_ALG_ASYNC |
  2280. CRYPTO_ALG_ALLOCATES_MEMORY,
  2281. .cra_blocksize = AES_BLOCK_SIZE,
  2282. .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
  2283. .cra_alignmask = 3,
  2284. .cra_module = THIS_MODULE,
  2285. },
  2286. .min_keysize = AES_MIN_KEY_SIZE,
  2287. .max_keysize = AES_MAX_KEY_SIZE,
  2288. .ivsize = AES_BLOCK_SIZE,
  2289. .setkey = artpec6_crypto_cipher_set_key,
  2290. .encrypt = artpec6_crypto_encrypt,
  2291. .decrypt = artpec6_crypto_decrypt,
  2292. .init = artpec6_crypto_aes_cbc_init,
  2293. .exit = artpec6_crypto_aes_exit
  2294. },
  2295. /* AES - XTS */
  2296. {
  2297. .base = {
  2298. .cra_name = "xts(aes)",
  2299. .cra_driver_name = "artpec6-xts-aes",
  2300. .cra_priority = 300,
  2301. .cra_flags = CRYPTO_ALG_ASYNC |
  2302. CRYPTO_ALG_ALLOCATES_MEMORY,
  2303. .cra_blocksize = 1,
  2304. .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
  2305. .cra_alignmask = 3,
  2306. .cra_module = THIS_MODULE,
  2307. },
  2308. .min_keysize = 2*AES_MIN_KEY_SIZE,
  2309. .max_keysize = 2*AES_MAX_KEY_SIZE,
  2310. .ivsize = 16,
  2311. .setkey = artpec6_crypto_xts_set_key,
  2312. .encrypt = artpec6_crypto_encrypt,
  2313. .decrypt = artpec6_crypto_decrypt,
  2314. .init = artpec6_crypto_aes_xts_init,
  2315. .exit = artpec6_crypto_aes_exit,
  2316. },
  2317. };
  2318. static struct aead_alg aead_algos[] = {
  2319. {
  2320. .init = artpec6_crypto_aead_init,
  2321. .setkey = artpec6_crypto_aead_set_key,
  2322. .encrypt = artpec6_crypto_aead_encrypt,
  2323. .decrypt = artpec6_crypto_aead_decrypt,
  2324. .ivsize = GCM_AES_IV_SIZE,
  2325. .maxauthsize = AES_BLOCK_SIZE,
  2326. .base = {
  2327. .cra_name = "gcm(aes)",
  2328. .cra_driver_name = "artpec-gcm-aes",
  2329. .cra_priority = 300,
  2330. .cra_flags = CRYPTO_ALG_ASYNC |
  2331. CRYPTO_ALG_ALLOCATES_MEMORY |
  2332. CRYPTO_ALG_KERN_DRIVER_ONLY,
  2333. .cra_blocksize = 1,
  2334. .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
  2335. .cra_alignmask = 3,
  2336. .cra_module = THIS_MODULE,
  2337. },
  2338. }
  2339. };
  2340. #ifdef CONFIG_DEBUG_FS
  2341. static struct dentry *dbgfs_root;
  2342. static void artpec6_crypto_init_debugfs(void)
  2343. {
  2344. dbgfs_root = debugfs_create_dir("artpec6_crypto", NULL);
  2345. #ifdef CONFIG_FAULT_INJECTION
  2346. fault_create_debugfs_attr("fail_status_read", dbgfs_root,
  2347. &artpec6_crypto_fail_status_read);
  2348. fault_create_debugfs_attr("fail_dma_array_full", dbgfs_root,
  2349. &artpec6_crypto_fail_dma_array_full);
  2350. #endif
  2351. }
  2352. static void artpec6_crypto_free_debugfs(void)
  2353. {
  2354. debugfs_remove_recursive(dbgfs_root);
  2355. dbgfs_root = NULL;
  2356. }
  2357. #endif
  2358. static const struct of_device_id artpec6_crypto_of_match[] = {
  2359. { .compatible = "axis,artpec6-crypto", .data = (void *)ARTPEC6_CRYPTO },
  2360. { .compatible = "axis,artpec7-crypto", .data = (void *)ARTPEC7_CRYPTO },
  2361. {}
  2362. };
  2363. MODULE_DEVICE_TABLE(of, artpec6_crypto_of_match);
  2364. static int artpec6_crypto_probe(struct platform_device *pdev)
  2365. {
  2366. enum artpec6_crypto_variant variant;
  2367. struct artpec6_crypto *ac;
  2368. struct device *dev = &pdev->dev;
  2369. void __iomem *base;
  2370. int irq;
  2371. int err;
  2372. if (artpec6_crypto_dev)
  2373. return -ENODEV;
  2374. variant = (enum artpec6_crypto_variant)of_device_get_match_data(dev);
  2375. if (!variant)
  2376. return -EINVAL;
  2377. base = devm_platform_ioremap_resource(pdev, 0);
  2378. if (IS_ERR(base))
  2379. return PTR_ERR(base);
  2380. irq = platform_get_irq(pdev, 0);
  2381. if (irq < 0)
  2382. return -ENODEV;
  2383. ac = devm_kzalloc(&pdev->dev, sizeof(struct artpec6_crypto),
  2384. GFP_KERNEL);
  2385. if (!ac)
  2386. return -ENOMEM;
  2387. platform_set_drvdata(pdev, ac);
  2388. ac->variant = variant;
  2389. spin_lock_init(&ac->queue_lock);
  2390. INIT_LIST_HEAD(&ac->queue);
  2391. INIT_LIST_HEAD(&ac->pending);
  2392. timer_setup(&ac->timer, artpec6_crypto_timeout, 0);
  2393. ac->base = base;
  2394. ac->dma_cache = kmem_cache_create("artpec6_crypto_dma",
  2395. sizeof(struct artpec6_crypto_dma_descriptors),
  2396. 64,
  2397. 0,
  2398. NULL);
  2399. if (!ac->dma_cache)
  2400. return -ENOMEM;
  2401. #ifdef CONFIG_DEBUG_FS
  2402. artpec6_crypto_init_debugfs();
  2403. #endif
  2404. tasklet_init(&ac->task, artpec6_crypto_task,
  2405. (unsigned long)ac);
  2406. ac->pad_buffer = devm_kcalloc(&pdev->dev, 2, ARTPEC_CACHE_LINE_MAX,
  2407. GFP_KERNEL);
  2408. if (!ac->pad_buffer)
  2409. return -ENOMEM;
  2410. ac->pad_buffer = PTR_ALIGN(ac->pad_buffer, ARTPEC_CACHE_LINE_MAX);
  2411. ac->zero_buffer = devm_kcalloc(&pdev->dev, 2, ARTPEC_CACHE_LINE_MAX,
  2412. GFP_KERNEL);
  2413. if (!ac->zero_buffer)
  2414. return -ENOMEM;
  2415. ac->zero_buffer = PTR_ALIGN(ac->zero_buffer, ARTPEC_CACHE_LINE_MAX);
  2416. err = init_crypto_hw(ac);
  2417. if (err)
  2418. goto free_cache;
  2419. err = devm_request_irq(&pdev->dev, irq, artpec6_crypto_irq, 0,
  2420. "artpec6-crypto", ac);
  2421. if (err)
  2422. goto disable_hw;
  2423. artpec6_crypto_dev = &pdev->dev;
  2424. err = crypto_register_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
  2425. if (err) {
  2426. dev_err(dev, "Failed to register ahashes\n");
  2427. goto disable_hw;
  2428. }
  2429. err = crypto_register_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
  2430. if (err) {
  2431. dev_err(dev, "Failed to register ciphers\n");
  2432. goto unregister_ahashes;
  2433. }
  2434. err = crypto_register_aeads(aead_algos, ARRAY_SIZE(aead_algos));
  2435. if (err) {
  2436. dev_err(dev, "Failed to register aeads\n");
  2437. goto unregister_algs;
  2438. }
  2439. return 0;
  2440. unregister_algs:
  2441. crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
  2442. unregister_ahashes:
  2443. crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
  2444. disable_hw:
  2445. artpec6_crypto_disable_hw(ac);
  2446. free_cache:
  2447. kmem_cache_destroy(ac->dma_cache);
  2448. return err;
  2449. }
  2450. static void artpec6_crypto_remove(struct platform_device *pdev)
  2451. {
  2452. struct artpec6_crypto *ac = platform_get_drvdata(pdev);
  2453. int irq = platform_get_irq(pdev, 0);
  2454. crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
  2455. crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
  2456. crypto_unregister_aeads(aead_algos, ARRAY_SIZE(aead_algos));
  2457. tasklet_disable(&ac->task);
  2458. devm_free_irq(&pdev->dev, irq, ac);
  2459. tasklet_kill(&ac->task);
  2460. timer_delete_sync(&ac->timer);
  2461. artpec6_crypto_disable_hw(ac);
  2462. kmem_cache_destroy(ac->dma_cache);
  2463. #ifdef CONFIG_DEBUG_FS
  2464. artpec6_crypto_free_debugfs();
  2465. #endif
  2466. }
  2467. static struct platform_driver artpec6_crypto_driver = {
  2468. .probe = artpec6_crypto_probe,
  2469. .remove = artpec6_crypto_remove,
  2470. .driver = {
  2471. .name = "artpec6-crypto",
  2472. .of_match_table = artpec6_crypto_of_match,
  2473. },
  2474. };
  2475. module_platform_driver(artpec6_crypto_driver);
  2476. MODULE_AUTHOR("Axis Communications AB");
  2477. MODULE_DESCRIPTION("ARTPEC-6 Crypto driver");
  2478. MODULE_LICENSE("GPL");