qcedev.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI CE device driver.
  4. *
  5. * Copyright (c) 2010-2021, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #include <linux/mman.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/types.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/kernel.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/crypto.h>
  25. #include "linux/qcedev.h"
  26. #include <linux/interconnect.h>
  27. #include <linux/delay.h>
  28. #include "linux/compat_qcedev.h"
  29. #include <crypto/hash.h>
  30. #include "qcedevi.h"
  31. #include "qce.h"
  32. #include "qcedev_smmu.h"
  33. #include "qcom_crypto_device.h"
  34. #include <linux/compat.h>
  35. #define CACHE_LINE_SIZE 64
  36. #define CE_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
  37. #define MAX_CEHW_REQ_TRANSFER_SIZE (128*32*1024)
  38. /*
  39. * Max wait time once a crypto request is submitted.
  40. */
  41. #define MAX_CRYPTO_WAIT_TIME 1500
  42. /*
  43. * Max wait time once a offload crypto request is submitted.
  44. * This is low due to expected timeout and key pause errors.
  45. * This is temporary, and we can use the 1500 value once the
  46. * core irqs are enabled.
  47. */
  48. #define MAX_OFFLOAD_CRYPTO_WAIT_TIME 20
  49. #define MAX_REQUEST_TIME 5000
  50. enum qcedev_req_status {
  51. QCEDEV_REQ_CURRENT = 0,
  52. QCEDEV_REQ_WAITING = 1,
  53. QCEDEV_REQ_SUBMITTED = 2,
  54. QCEDEV_REQ_DONE = 3,
  55. };
  56. static uint8_t _std_init_vector_sha1_uint8[] = {
  57. 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89,
  58. 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x32, 0x54, 0x76,
  59. 0xC3, 0xD2, 0xE1, 0xF0
  60. };
  61. /* standard initialization vector for SHA-256, source: FIPS 180-2 */
  62. static uint8_t _std_init_vector_sha256_uint8[] = {
  63. 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85,
  64. 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A,
  65. 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C,
  66. 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19
  67. };
  68. #define QCEDEV_CTX_KEY_MASK 0x000000ff
  69. #define QCEDEV_CTX_USE_HW_KEY 0x00000001
  70. #define QCEDEV_CTX_USE_PIPE_KEY 0x00000002
  71. static DEFINE_MUTEX(send_cmd_lock);
  72. static DEFINE_MUTEX(qcedev_sent_bw_req);
  73. static DEFINE_MUTEX(hash_access_lock);
  74. static dev_t qcedev_device_no;
  75. static struct class *driver_class;
  76. static struct device *class_dev;
  77. static const struct of_device_id qcedev_match[] = {
  78. { .compatible = "qcom,qcedev"},
  79. { .compatible = "qcom,qcedev,context-bank"},
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(of, qcedev_match);
  83. static int qcedev_control_clocks(struct qcedev_control *podev, bool enable)
  84. {
  85. unsigned int control_flag;
  86. int ret = 0;
  87. if (podev->ce_support.req_bw_before_clk) {
  88. if (enable)
  89. control_flag = QCE_BW_REQUEST_FIRST;
  90. else
  91. control_flag = QCE_CLK_DISABLE_FIRST;
  92. } else {
  93. if (enable)
  94. control_flag = QCE_CLK_ENABLE_FIRST;
  95. else
  96. control_flag = QCE_BW_REQUEST_RESET_FIRST;
  97. }
  98. switch (control_flag) {
  99. case QCE_CLK_ENABLE_FIRST:
  100. ret = qce_enable_clk(podev->qce);
  101. if (ret) {
  102. pr_err("%s Unable enable clk\n", __func__);
  103. return ret;
  104. }
  105. ret = icc_set_bw(podev->icc_path,
  106. podev->icc_avg_bw, podev->icc_peak_bw);
  107. if (ret) {
  108. pr_err("%s Unable to set high bw\n", __func__);
  109. ret = qce_disable_clk(podev->qce);
  110. if (ret)
  111. pr_err("%s Unable disable clk\n", __func__);
  112. return ret;
  113. }
  114. break;
  115. case QCE_BW_REQUEST_FIRST:
  116. ret = icc_set_bw(podev->icc_path,
  117. podev->icc_avg_bw, podev->icc_peak_bw);
  118. if (ret) {
  119. pr_err("%s Unable to set high bw\n", __func__);
  120. return ret;
  121. }
  122. ret = qce_enable_clk(podev->qce);
  123. if (ret) {
  124. pr_err("%s Unable enable clk\n", __func__);
  125. ret = icc_set_bw(podev->icc_path, 0, 0);
  126. if (ret)
  127. pr_err("%s Unable to set low bw\n", __func__);
  128. return ret;
  129. }
  130. break;
  131. case QCE_CLK_DISABLE_FIRST:
  132. ret = qce_disable_clk(podev->qce);
  133. if (ret) {
  134. pr_err("%s Unable to disable clk\n", __func__);
  135. return ret;
  136. }
  137. ret = icc_set_bw(podev->icc_path, 0, 0);
  138. if (ret) {
  139. pr_err("%s Unable to set low bw\n", __func__);
  140. ret = qce_enable_clk(podev->qce);
  141. if (ret)
  142. pr_err("%s Unable enable clk\n", __func__);
  143. return ret;
  144. }
  145. break;
  146. case QCE_BW_REQUEST_RESET_FIRST:
  147. ret = icc_set_bw(podev->icc_path, 0, 0);
  148. if (ret) {
  149. pr_err("%s Unable to set low bw\n", __func__);
  150. return ret;
  151. }
  152. ret = qce_disable_clk(podev->qce);
  153. if (ret) {
  154. pr_err("%s Unable to disable clk\n", __func__);
  155. ret = icc_set_bw(podev->icc_path,
  156. podev->icc_avg_bw, podev->icc_peak_bw);
  157. if (ret)
  158. pr_err("%s Unable to set high bw\n", __func__);
  159. return ret;
  160. }
  161. break;
  162. default:
  163. return -ENOENT;
  164. }
  165. return 0;
  166. }
  167. static void qcedev_ce_high_bw_req(struct qcedev_control *podev,
  168. bool high_bw_req)
  169. {
  170. int ret = 0;
  171. if(podev == NULL) return;
  172. mutex_lock(&qcedev_sent_bw_req);
  173. if (high_bw_req) {
  174. if (podev->high_bw_req_count == 0) {
  175. ret = qcedev_control_clocks(podev, true);
  176. if (ret)
  177. goto exit_unlock_mutex;
  178. ret = qce_set_irqs(podev->qce, true);
  179. if (ret) {
  180. pr_err("%s: could not enable bam irqs, ret = %d",
  181. __func__, ret);
  182. qcedev_control_clocks(podev, false);
  183. goto exit_unlock_mutex;
  184. }
  185. }
  186. podev->high_bw_req_count++;
  187. } else {
  188. if (podev->high_bw_req_count == 1) {
  189. ret = qce_set_irqs(podev->qce, false);
  190. if (ret) {
  191. pr_err("%s: could not disable bam irqs, ret = %d",
  192. __func__, ret);
  193. goto exit_unlock_mutex;
  194. }
  195. ret = qcedev_control_clocks(podev, false);
  196. if (ret)
  197. goto exit_unlock_mutex;
  198. }
  199. podev->high_bw_req_count--;
  200. }
  201. exit_unlock_mutex:
  202. mutex_unlock(&qcedev_sent_bw_req);
  203. }
  204. #define QCEDEV_MAGIC 0x56434544 /* "qced" */
  205. static int qcedev_open(struct inode *inode, struct file *file);
  206. static int qcedev_release(struct inode *inode, struct file *file);
  207. static int start_cipher_req(struct qcedev_control *podev,
  208. int *current_req_info);
  209. static int start_offload_cipher_req(struct qcedev_control *podev,
  210. int *current_req_info);
  211. static int start_sha_req(struct qcedev_control *podev,
  212. int *current_req_info);
  213. static const struct file_operations qcedev_fops = {
  214. .owner = THIS_MODULE,
  215. .unlocked_ioctl = qcedev_ioctl,
  216. #ifdef CONFIG_COMPAT
  217. .compat_ioctl = compat_qcedev_ioctl,
  218. #endif
  219. .open = qcedev_open,
  220. .release = qcedev_release,
  221. };
  222. static struct qcedev_control qce_dev[] = {
  223. {
  224. .magic = QCEDEV_MAGIC,
  225. },
  226. };
  227. #define MAX_QCE_DEVICE ARRAY_SIZE(qce_dev)
  228. #define DEBUG_MAX_FNAME 16
  229. #define DEBUG_MAX_RW_BUF 1024
  230. struct qcedev_stat {
  231. u32 qcedev_dec_success;
  232. u32 qcedev_dec_fail;
  233. u32 qcedev_enc_success;
  234. u32 qcedev_enc_fail;
  235. u32 qcedev_sha_success;
  236. u32 qcedev_sha_fail;
  237. };
  238. static struct qcedev_stat _qcedev_stat;
  239. static struct dentry *_debug_dent;
  240. static char _debug_read_buf[DEBUG_MAX_RW_BUF];
  241. static int _debug_qcedev;
  242. static struct qcedev_control *qcedev_minor_to_control(unsigned int n)
  243. {
  244. int i;
  245. for (i = 0; i < MAX_QCE_DEVICE; i++) {
  246. if (qce_dev[i].minor == n)
  247. return &qce_dev[n];
  248. }
  249. return NULL;
  250. }
  251. static int qcedev_open(struct inode *inode, struct file *file)
  252. {
  253. struct qcedev_handle *handle;
  254. struct qcedev_control *podev;
  255. podev = qcedev_minor_to_control(MINOR(inode->i_rdev));
  256. if (podev == NULL) {
  257. pr_err("%s: no such device %d\n", __func__,
  258. MINOR(inode->i_rdev));
  259. return -ENOENT;
  260. }
  261. handle = kzalloc(sizeof(struct qcedev_handle), GFP_KERNEL);
  262. if (handle == NULL)
  263. return -ENOMEM;
  264. handle->cntl = podev;
  265. file->private_data = handle;
  266. qcedev_ce_high_bw_req(podev, true);
  267. mutex_init(&handle->registeredbufs.lock);
  268. INIT_LIST_HEAD(&handle->registeredbufs.list);
  269. return 0;
  270. }
  271. static int qcedev_release(struct inode *inode, struct file *file)
  272. {
  273. struct qcedev_control *podev;
  274. struct qcedev_handle *handle;
  275. handle = file->private_data;
  276. podev = handle->cntl;
  277. if (podev != NULL && podev->magic != QCEDEV_MAGIC) {
  278. pr_err("%s: invalid handle %pK\n",
  279. __func__, podev);
  280. }
  281. if (podev)
  282. qcedev_ce_high_bw_req(podev, false);
  283. if (qcedev_unmap_all_buffers(handle))
  284. pr_err("%s: failed to unmap all ion buffers\n", __func__);
  285. kfree_sensitive(handle);
  286. file->private_data = NULL;
  287. return 0;
  288. }
  289. static void req_done(unsigned long data)
  290. {
  291. struct qcedev_control *podev = (struct qcedev_control *)data;
  292. struct qcedev_async_req *areq;
  293. unsigned long flags = 0;
  294. struct qcedev_async_req *new_req = NULL;
  295. spin_lock_irqsave(&podev->lock, flags);
  296. areq = podev->active_command;
  297. podev->active_command = NULL;
  298. if (areq) {
  299. if (!areq->timed_out)
  300. complete(&areq->complete);
  301. areq->state = QCEDEV_REQ_DONE;
  302. }
  303. /* Look through queued requests and wake up the corresponding thread */
  304. if (!list_empty(&podev->ready_commands)) {
  305. new_req = container_of(podev->ready_commands.next,
  306. struct qcedev_async_req, list);
  307. list_del(&new_req->list);
  308. new_req->state = QCEDEV_REQ_CURRENT;
  309. wake_up_interruptible(&new_req->wait_q);
  310. }
  311. spin_unlock_irqrestore(&podev->lock, flags);
  312. }
  313. void qcedev_sha_req_cb(void *cookie, unsigned char *digest,
  314. unsigned char *authdata, int ret)
  315. {
  316. struct qcedev_sha_req *areq;
  317. struct qcedev_control *pdev;
  318. struct qcedev_handle *handle;
  319. uint32_t *auth32 = (uint32_t *)authdata;
  320. areq = (struct qcedev_sha_req *) cookie;
  321. if (!areq || !areq->cookie)
  322. return;
  323. handle = (struct qcedev_handle *) areq->cookie;
  324. pdev = handle->cntl;
  325. if (!pdev)
  326. return;
  327. if (digest)
  328. memcpy(&handle->sha_ctxt.digest[0], digest, 32);
  329. if (authdata) {
  330. handle->sha_ctxt.auth_data[0] = auth32[0];
  331. handle->sha_ctxt.auth_data[1] = auth32[1];
  332. }
  333. tasklet_schedule(&pdev->done_tasklet);
  334. };
  335. void qcedev_cipher_req_cb(void *cookie, unsigned char *icv,
  336. unsigned char *iv, int ret)
  337. {
  338. struct qcedev_cipher_req *areq;
  339. struct qcedev_handle *handle;
  340. struct qcedev_control *podev;
  341. struct qcedev_async_req *qcedev_areq;
  342. areq = (struct qcedev_cipher_req *) cookie;
  343. if (!areq || !areq->cookie)
  344. return;
  345. handle = (struct qcedev_handle *) areq->cookie;
  346. podev = handle->cntl;
  347. if (!podev)
  348. return;
  349. qcedev_areq = podev->active_command;
  350. if (iv)
  351. memcpy(&qcedev_areq->cipher_op_req.iv[0], iv,
  352. qcedev_areq->cipher_op_req.ivlen);
  353. tasklet_schedule(&podev->done_tasklet);
  354. };
  355. static int start_cipher_req(struct qcedev_control *podev,
  356. int *current_req_info)
  357. {
  358. struct qcedev_async_req *qcedev_areq;
  359. struct qce_req creq;
  360. int ret = 0;
  361. memset(&creq, 0, sizeof(creq));
  362. /* start the command on the podev->active_command */
  363. qcedev_areq = podev->active_command;
  364. qcedev_areq->cipher_req.cookie = qcedev_areq->handle;
  365. if (qcedev_areq->cipher_op_req.use_pmem == QCEDEV_USE_PMEM) {
  366. pr_err("%s: Use of PMEM is not supported\n", __func__);
  367. goto unsupported;
  368. }
  369. creq.pmem = NULL;
  370. switch (qcedev_areq->cipher_op_req.alg) {
  371. case QCEDEV_ALG_DES:
  372. creq.alg = CIPHER_ALG_DES;
  373. break;
  374. case QCEDEV_ALG_3DES:
  375. creq.alg = CIPHER_ALG_3DES;
  376. break;
  377. case QCEDEV_ALG_AES:
  378. creq.alg = CIPHER_ALG_AES;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. switch (qcedev_areq->cipher_op_req.mode) {
  384. case QCEDEV_AES_MODE_CBC:
  385. case QCEDEV_DES_MODE_CBC:
  386. creq.mode = QCE_MODE_CBC;
  387. break;
  388. case QCEDEV_AES_MODE_ECB:
  389. case QCEDEV_DES_MODE_ECB:
  390. creq.mode = QCE_MODE_ECB;
  391. break;
  392. case QCEDEV_AES_MODE_CTR:
  393. creq.mode = QCE_MODE_CTR;
  394. break;
  395. case QCEDEV_AES_MODE_XTS:
  396. creq.mode = QCE_MODE_XTS;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. if ((creq.alg == CIPHER_ALG_AES) &&
  402. (creq.mode == QCE_MODE_CTR)) {
  403. creq.dir = QCE_ENCRYPT;
  404. } else {
  405. if (qcedev_areq->cipher_op_req.op == QCEDEV_OPER_ENC)
  406. creq.dir = QCE_ENCRYPT;
  407. else
  408. creq.dir = QCE_DECRYPT;
  409. }
  410. creq.iv = &qcedev_areq->cipher_op_req.iv[0];
  411. creq.ivsize = qcedev_areq->cipher_op_req.ivlen;
  412. creq.iv_ctr_size = 0;
  413. creq.enckey = &qcedev_areq->cipher_op_req.enckey[0];
  414. creq.encklen = qcedev_areq->cipher_op_req.encklen;
  415. creq.cryptlen = qcedev_areq->cipher_op_req.data_len;
  416. if (qcedev_areq->cipher_op_req.encklen == 0) {
  417. if ((qcedev_areq->cipher_op_req.op == QCEDEV_OPER_ENC_NO_KEY)
  418. || (qcedev_areq->cipher_op_req.op ==
  419. QCEDEV_OPER_DEC_NO_KEY))
  420. creq.op = QCE_REQ_ABLK_CIPHER_NO_KEY;
  421. else {
  422. int i;
  423. for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) {
  424. if (qcedev_areq->cipher_op_req.enckey[i] != 0)
  425. break;
  426. }
  427. if ((podev->platform_support.hw_key_support == 1) &&
  428. (i == QCEDEV_MAX_KEY_SIZE))
  429. creq.op = QCE_REQ_ABLK_CIPHER;
  430. else {
  431. ret = -EINVAL;
  432. goto unsupported;
  433. }
  434. }
  435. } else {
  436. creq.op = QCE_REQ_ABLK_CIPHER;
  437. }
  438. creq.qce_cb = qcedev_cipher_req_cb;
  439. creq.areq = (void *)&qcedev_areq->cipher_req;
  440. creq.flags = 0;
  441. creq.offload_op = QCE_OFFLOAD_NONE;
  442. ret = qce_ablk_cipher_req(podev->qce, &creq);
  443. *current_req_info = creq.current_req_info;
  444. unsupported:
  445. qcedev_areq->err = ret ? -ENXIO : 0;
  446. return ret;
  447. };
  448. void qcedev_offload_cipher_req_cb(void *cookie, unsigned char *icv,
  449. unsigned char *iv, int ret)
  450. {
  451. struct qcedev_cipher_req *areq;
  452. struct qcedev_handle *handle;
  453. struct qcedev_control *podev;
  454. struct qcedev_async_req *qcedev_areq;
  455. areq = (struct qcedev_cipher_req *) cookie;
  456. if (!areq || !areq->cookie)
  457. return;
  458. handle = (struct qcedev_handle *) areq->cookie;
  459. podev = handle->cntl;
  460. if (!podev)
  461. return;
  462. qcedev_areq = podev->active_command;
  463. if (iv)
  464. memcpy(&qcedev_areq->offload_cipher_op_req.iv[0], iv,
  465. qcedev_areq->offload_cipher_op_req.ivlen);
  466. tasklet_schedule(&podev->done_tasklet);
  467. }
  468. static int start_offload_cipher_req(struct qcedev_control *podev,
  469. int *current_req_info)
  470. {
  471. struct qcedev_async_req *qcedev_areq;
  472. struct qce_req creq;
  473. u8 patt_sz = 0, proc_data_sz = 0;
  474. int ret = 0;
  475. memset(&creq, 0, sizeof(creq));
  476. /* Start the command on the podev->active_command */
  477. qcedev_areq = podev->active_command;
  478. qcedev_areq->cipher_req.cookie = qcedev_areq->handle;
  479. switch (qcedev_areq->offload_cipher_op_req.alg) {
  480. case QCEDEV_ALG_AES:
  481. creq.alg = CIPHER_ALG_AES;
  482. break;
  483. default:
  484. return -EINVAL;
  485. }
  486. switch (qcedev_areq->offload_cipher_op_req.mode) {
  487. case QCEDEV_AES_MODE_CBC:
  488. creq.mode = QCE_MODE_CBC;
  489. break;
  490. case QCEDEV_AES_MODE_CTR:
  491. creq.mode = QCE_MODE_CTR;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. if (qcedev_areq->offload_cipher_op_req.is_copy_op) {
  497. creq.dir = QCE_ENCRYPT;
  498. } else {
  499. switch(qcedev_areq->offload_cipher_op_req.op) {
  500. case QCEDEV_OFFLOAD_HLOS_HLOS:
  501. case QCEDEV_OFFLOAD_HLOS_CPB:
  502. creq.dir = QCE_DECRYPT;
  503. break;
  504. case QCEDEV_OFFLOAD_CPB_HLOS:
  505. creq.dir = QCE_ENCRYPT;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. }
  511. creq.iv = &qcedev_areq->offload_cipher_op_req.iv[0];
  512. creq.ivsize = qcedev_areq->offload_cipher_op_req.ivlen;
  513. creq.iv_ctr_size = qcedev_areq->offload_cipher_op_req.iv_ctr_size;
  514. creq.encklen = qcedev_areq->offload_cipher_op_req.encklen;
  515. /* OFFLOAD use cases use PIPE keys so no need to set keys */
  516. creq.flags = QCEDEV_CTX_USE_PIPE_KEY;
  517. creq.op = QCE_REQ_ABLK_CIPHER_NO_KEY;
  518. creq.offload_op = (int)qcedev_areq->offload_cipher_op_req.op;
  519. if (qcedev_areq->offload_cipher_op_req.is_copy_op)
  520. creq.is_copy_op = true;
  521. creq.cryptlen = qcedev_areq->offload_cipher_op_req.data_len;
  522. creq.qce_cb = qcedev_offload_cipher_req_cb;
  523. creq.areq = (void *)&qcedev_areq->cipher_req;
  524. patt_sz = qcedev_areq->offload_cipher_op_req.pattern_info.patt_sz;
  525. proc_data_sz =
  526. qcedev_areq->offload_cipher_op_req.pattern_info.proc_data_sz;
  527. creq.is_pattern_valid =
  528. qcedev_areq->offload_cipher_op_req.is_pattern_valid;
  529. if (creq.is_pattern_valid) {
  530. creq.pattern_info = 0x1;
  531. if (patt_sz)
  532. creq.pattern_info |= (patt_sz - 1) << 4;
  533. if (proc_data_sz)
  534. creq.pattern_info |= (proc_data_sz - 1) << 8;
  535. creq.pattern_info |=
  536. qcedev_areq->offload_cipher_op_req.pattern_info.patt_offset << 12;
  537. }
  538. creq.block_offset = qcedev_areq->offload_cipher_op_req.block_offset;
  539. ret = qce_ablk_cipher_req(podev->qce, &creq);
  540. *current_req_info = creq.current_req_info;
  541. qcedev_areq->err = ret ? -ENXIO : 0;
  542. return ret;
  543. }
  544. static int start_sha_req(struct qcedev_control *podev,
  545. int *current_req_info)
  546. {
  547. struct qcedev_async_req *qcedev_areq;
  548. struct qce_sha_req sreq;
  549. int ret = 0;
  550. struct qcedev_handle *handle;
  551. /* start the command on the podev->active_command */
  552. qcedev_areq = podev->active_command;
  553. handle = qcedev_areq->handle;
  554. switch (qcedev_areq->sha_op_req.alg) {
  555. case QCEDEV_ALG_SHA1:
  556. sreq.alg = QCE_HASH_SHA1;
  557. break;
  558. case QCEDEV_ALG_SHA256:
  559. sreq.alg = QCE_HASH_SHA256;
  560. break;
  561. case QCEDEV_ALG_SHA1_HMAC:
  562. if (podev->ce_support.sha_hmac) {
  563. sreq.alg = QCE_HASH_SHA1_HMAC;
  564. sreq.authkey = &handle->sha_ctxt.authkey[0];
  565. sreq.authklen = QCEDEV_MAX_SHA_BLOCK_SIZE;
  566. } else {
  567. sreq.alg = QCE_HASH_SHA1;
  568. sreq.authkey = NULL;
  569. }
  570. break;
  571. case QCEDEV_ALG_SHA256_HMAC:
  572. if (podev->ce_support.sha_hmac) {
  573. sreq.alg = QCE_HASH_SHA256_HMAC;
  574. sreq.authkey = &handle->sha_ctxt.authkey[0];
  575. sreq.authklen = QCEDEV_MAX_SHA_BLOCK_SIZE;
  576. } else {
  577. sreq.alg = QCE_HASH_SHA256;
  578. sreq.authkey = NULL;
  579. }
  580. break;
  581. case QCEDEV_ALG_AES_CMAC:
  582. sreq.alg = QCE_HASH_AES_CMAC;
  583. sreq.authkey = &handle->sha_ctxt.authkey[0];
  584. sreq.authklen = qcedev_areq->sha_op_req.authklen;
  585. break;
  586. default:
  587. pr_err("Algorithm %d not supported, exiting\n",
  588. qcedev_areq->sha_op_req.alg);
  589. return -EINVAL;
  590. }
  591. qcedev_areq->sha_req.cookie = handle;
  592. sreq.qce_cb = qcedev_sha_req_cb;
  593. if (qcedev_areq->sha_op_req.alg != QCEDEV_ALG_AES_CMAC) {
  594. sreq.auth_data[0] = handle->sha_ctxt.auth_data[0];
  595. sreq.auth_data[1] = handle->sha_ctxt.auth_data[1];
  596. sreq.auth_data[2] = handle->sha_ctxt.auth_data[2];
  597. sreq.auth_data[3] = handle->sha_ctxt.auth_data[3];
  598. sreq.digest = &handle->sha_ctxt.digest[0];
  599. sreq.first_blk = handle->sha_ctxt.first_blk;
  600. sreq.last_blk = handle->sha_ctxt.last_blk;
  601. }
  602. sreq.size = qcedev_areq->sha_req.sreq.nbytes;
  603. sreq.src = qcedev_areq->sha_req.sreq.src;
  604. sreq.areq = (void *)&qcedev_areq->sha_req;
  605. sreq.flags = 0;
  606. ret = qce_process_sha_req(podev->qce, &sreq);
  607. *current_req_info = sreq.current_req_info;
  608. qcedev_areq->err = ret ? -ENXIO : 0;
  609. return ret;
  610. };
  611. static void qcedev_check_crypto_status(
  612. struct qcedev_async_req *qcedev_areq, void *handle)
  613. {
  614. struct qce_error error = {0};
  615. qcedev_areq->offload_cipher_op_req.err = QCEDEV_OFFLOAD_NO_ERROR;
  616. qce_get_crypto_status(handle, &error);
  617. if (error.timer_error) {
  618. qcedev_areq->offload_cipher_op_req.err =
  619. QCEDEV_OFFLOAD_KEY_TIMER_EXPIRED_ERROR;
  620. } else if (error.key_paused) {
  621. qcedev_areq->offload_cipher_op_req.err =
  622. QCEDEV_OFFLOAD_KEY_PAUSE_ERROR;
  623. } else if (error.generic_error) {
  624. qcedev_areq->offload_cipher_op_req.err =
  625. QCEDEV_OFFLOAD_GENERIC_ERROR;
  626. }
  627. return;
  628. }
  629. #define MAX_RETRIES 333
  630. static int submit_req(struct qcedev_async_req *qcedev_areq,
  631. struct qcedev_handle *handle)
  632. {
  633. struct qcedev_control *podev;
  634. unsigned long flags = 0;
  635. int ret = 0;
  636. struct qcedev_stat *pstat;
  637. int current_req_info = 0;
  638. int wait = MAX_CRYPTO_WAIT_TIME;
  639. struct qcedev_async_req *new_req = NULL;
  640. int retries = 0;
  641. int req_wait = MAX_REQUEST_TIME;
  642. unsigned int crypto_wait = 0;
  643. qcedev_areq->err = 0;
  644. podev = handle->cntl;
  645. init_waitqueue_head(&qcedev_areq->wait_q);
  646. spin_lock_irqsave(&podev->lock, flags);
  647. /*
  648. * Service only one crypto request at a time.
  649. * Any other new requests are queued in ready_commands and woken up
  650. * only when the active command has finished successfully or when the
  651. * request times out or when the command failed when setting up.
  652. */
  653. do {
  654. if (podev->active_command == NULL) {
  655. podev->active_command = qcedev_areq;
  656. qcedev_areq->state = QCEDEV_REQ_SUBMITTED;
  657. switch (qcedev_areq->op_type) {
  658. case QCEDEV_CRYPTO_OPER_CIPHER:
  659. ret = start_cipher_req(podev,
  660. &current_req_info);
  661. crypto_wait = MAX_CRYPTO_WAIT_TIME;
  662. break;
  663. case QCEDEV_CRYPTO_OPER_OFFLOAD_CIPHER:
  664. ret = start_offload_cipher_req(podev,
  665. &current_req_info);
  666. crypto_wait = MAX_OFFLOAD_CRYPTO_WAIT_TIME;
  667. break;
  668. default:
  669. crypto_wait = MAX_CRYPTO_WAIT_TIME;
  670. ret = start_sha_req(podev,
  671. &current_req_info);
  672. break;
  673. }
  674. } else {
  675. list_add_tail(&qcedev_areq->list,
  676. &podev->ready_commands);
  677. qcedev_areq->state = QCEDEV_REQ_WAITING;
  678. req_wait = wait_event_interruptible_lock_irq_timeout(
  679. qcedev_areq->wait_q,
  680. (qcedev_areq->state == QCEDEV_REQ_CURRENT),
  681. podev->lock,
  682. msecs_to_jiffies(MAX_REQUEST_TIME));
  683. if ((req_wait == 0) || (req_wait == -ERESTARTSYS)) {
  684. pr_err("%s: request timed out, req_wait = %d\n",
  685. __func__, req_wait);
  686. list_del(&qcedev_areq->list);
  687. podev->active_command = NULL;
  688. spin_unlock_irqrestore(&podev->lock, flags);
  689. return qcedev_areq->err;
  690. }
  691. }
  692. } while (qcedev_areq->state != QCEDEV_REQ_SUBMITTED);
  693. if (ret != 0) {
  694. podev->active_command = NULL;
  695. /*
  696. * Look through queued requests and wake up the corresponding
  697. * thread.
  698. */
  699. if (!list_empty(&podev->ready_commands)) {
  700. new_req = container_of(podev->ready_commands.next,
  701. struct qcedev_async_req, list);
  702. list_del(&new_req->list);
  703. new_req->state = QCEDEV_REQ_CURRENT;
  704. wake_up_interruptible(&new_req->wait_q);
  705. }
  706. }
  707. spin_unlock_irqrestore(&podev->lock, flags);
  708. qcedev_areq->timed_out = false;
  709. if (ret == 0)
  710. wait = wait_for_completion_timeout(&qcedev_areq->complete,
  711. msecs_to_jiffies(crypto_wait));
  712. if (!wait) {
  713. /*
  714. * This means wait timed out, and the callback routine was not
  715. * exercised. The callback sequence does some housekeeping which
  716. * would be missed here, hence having a call to qce here to do
  717. * that.
  718. */
  719. pr_err("%s: wait timed out, req info = %d\n", __func__,
  720. current_req_info);
  721. spin_lock_irqsave(&podev->lock, flags);
  722. qcedev_areq->timed_out = true;
  723. spin_unlock_irqrestore(&podev->lock, flags);
  724. qcedev_check_crypto_status(qcedev_areq, podev->qce);
  725. if (qcedev_areq->offload_cipher_op_req.err ==
  726. QCEDEV_OFFLOAD_NO_ERROR) {
  727. pr_err("%s: no error, wait for request to be done", __func__);
  728. while (qcedev_areq->state != QCEDEV_REQ_DONE &&
  729. retries < MAX_RETRIES) {
  730. usleep_range(3000, 5000);
  731. retries++;
  732. pr_err("%s: waiting for req state to be done, retries = %d",
  733. __func__, retries);
  734. }
  735. return 0;
  736. }
  737. ret = qce_manage_timeout(podev->qce, current_req_info);
  738. if (ret)
  739. pr_err("%s: error during manage timeout", __func__);
  740. req_done((unsigned long) podev);
  741. if (qcedev_areq->offload_cipher_op_req.err !=
  742. QCEDEV_OFFLOAD_NO_ERROR)
  743. return 0;
  744. }
  745. if (ret)
  746. qcedev_areq->err = -EIO;
  747. pstat = &_qcedev_stat;
  748. if (qcedev_areq->op_type == QCEDEV_CRYPTO_OPER_CIPHER) {
  749. switch (qcedev_areq->cipher_op_req.op) {
  750. case QCEDEV_OPER_DEC:
  751. if (qcedev_areq->err)
  752. pstat->qcedev_dec_fail++;
  753. else
  754. pstat->qcedev_dec_success++;
  755. break;
  756. case QCEDEV_OPER_ENC:
  757. if (qcedev_areq->err)
  758. pstat->qcedev_enc_fail++;
  759. else
  760. pstat->qcedev_enc_success++;
  761. break;
  762. default:
  763. break;
  764. }
  765. } else if (qcedev_areq->op_type == QCEDEV_CRYPTO_OPER_OFFLOAD_CIPHER) {
  766. //Do nothing
  767. } else {
  768. if (qcedev_areq->err)
  769. pstat->qcedev_sha_fail++;
  770. else
  771. pstat->qcedev_sha_success++;
  772. }
  773. return qcedev_areq->err;
  774. }
  775. static int qcedev_sha_init(struct qcedev_async_req *areq,
  776. struct qcedev_handle *handle)
  777. {
  778. struct qcedev_sha_ctxt *sha_ctxt = &handle->sha_ctxt;
  779. memset(sha_ctxt, 0, sizeof(struct qcedev_sha_ctxt));
  780. sha_ctxt->first_blk = 1;
  781. if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) ||
  782. (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC)) {
  783. memcpy(&sha_ctxt->digest[0],
  784. &_std_init_vector_sha1_uint8[0], SHA1_DIGEST_SIZE);
  785. sha_ctxt->diglen = SHA1_DIGEST_SIZE;
  786. } else {
  787. if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA256) ||
  788. (areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC)) {
  789. memcpy(&sha_ctxt->digest[0],
  790. &_std_init_vector_sha256_uint8[0],
  791. SHA256_DIGEST_SIZE);
  792. sha_ctxt->diglen = SHA256_DIGEST_SIZE;
  793. }
  794. }
  795. sha_ctxt->init_done = true;
  796. return 0;
  797. }
  798. static int qcedev_sha_update_max_xfer(struct qcedev_async_req *qcedev_areq,
  799. struct qcedev_handle *handle,
  800. struct scatterlist *sg_src)
  801. {
  802. int err = 0;
  803. int i = 0;
  804. uint32_t total;
  805. uint8_t *user_src = NULL;
  806. uint8_t *k_src = NULL;
  807. uint8_t *k_buf_src = NULL;
  808. uint32_t buf_size = 0;
  809. uint8_t *k_align_src = NULL;
  810. uint32_t sha_pad_len = 0;
  811. uint32_t trailing_buf_len = 0;
  812. uint32_t t_buf = handle->sha_ctxt.trailing_buf_len;
  813. uint32_t sha_block_size;
  814. total = qcedev_areq->sha_op_req.data_len + t_buf;
  815. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1)
  816. sha_block_size = SHA1_BLOCK_SIZE;
  817. else
  818. sha_block_size = SHA256_BLOCK_SIZE;
  819. if (total <= sha_block_size) {
  820. uint32_t len = qcedev_areq->sha_op_req.data_len;
  821. i = 0;
  822. k_src = &handle->sha_ctxt.trailing_buf[t_buf];
  823. /* Copy data from user src(s) */
  824. while (len > 0) {
  825. user_src = qcedev_areq->sha_op_req.data[i].vaddr;
  826. if (user_src && copy_from_user(k_src,
  827. (void __user *)user_src,
  828. qcedev_areq->sha_op_req.data[i].len))
  829. return -EFAULT;
  830. len -= qcedev_areq->sha_op_req.data[i].len;
  831. k_src += qcedev_areq->sha_op_req.data[i].len;
  832. i++;
  833. }
  834. handle->sha_ctxt.trailing_buf_len = total;
  835. return 0;
  836. }
  837. buf_size = total + CACHE_LINE_SIZE * 2;
  838. k_buf_src = kmalloc(buf_size, GFP_KERNEL);
  839. if (k_buf_src == NULL)
  840. return -ENOMEM;
  841. k_align_src = (uint8_t *)ALIGN(((uintptr_t)k_buf_src),
  842. CACHE_LINE_SIZE);
  843. k_src = k_align_src;
  844. /* check for trailing buffer from previous updates and append it */
  845. if (t_buf > 0) {
  846. memcpy(k_src, &handle->sha_ctxt.trailing_buf[0],
  847. t_buf);
  848. k_src += t_buf;
  849. }
  850. /* Copy data from user src(s) */
  851. user_src = qcedev_areq->sha_op_req.data[0].vaddr;
  852. if (user_src && copy_from_user(k_src,
  853. (void __user *)user_src,
  854. qcedev_areq->sha_op_req.data[0].len)) {
  855. memset(k_buf_src, 0, buf_size);
  856. kfree(k_buf_src);
  857. return -EFAULT;
  858. }
  859. k_src += qcedev_areq->sha_op_req.data[0].len;
  860. for (i = 1; i < qcedev_areq->sha_op_req.entries; i++) {
  861. user_src = qcedev_areq->sha_op_req.data[i].vaddr;
  862. if (user_src && copy_from_user(k_src,
  863. (void __user *)user_src,
  864. qcedev_areq->sha_op_req.data[i].len)) {
  865. memset(k_buf_src, 0, buf_size);
  866. kfree(k_buf_src);
  867. return -EFAULT;
  868. }
  869. k_src += qcedev_areq->sha_op_req.data[i].len;
  870. }
  871. /* get new trailing buffer */
  872. sha_pad_len = ALIGN(total, CE_SHA_BLOCK_SIZE) - total;
  873. trailing_buf_len = CE_SHA_BLOCK_SIZE - sha_pad_len;
  874. qcedev_areq->sha_req.sreq.src = sg_src;
  875. sg_init_one(qcedev_areq->sha_req.sreq.src, k_align_src,
  876. total-trailing_buf_len);
  877. qcedev_areq->sha_req.sreq.nbytes = total - trailing_buf_len;
  878. /* update sha_ctxt trailing buf content to new trailing buf */
  879. if (trailing_buf_len > 0) {
  880. memset(&handle->sha_ctxt.trailing_buf[0], 0, 64);
  881. memcpy(&handle->sha_ctxt.trailing_buf[0],
  882. (k_src - trailing_buf_len),
  883. trailing_buf_len);
  884. }
  885. handle->sha_ctxt.trailing_buf_len = trailing_buf_len;
  886. err = submit_req(qcedev_areq, handle);
  887. handle->sha_ctxt.last_blk = 0;
  888. handle->sha_ctxt.first_blk = 0;
  889. memset(k_buf_src, 0, buf_size);
  890. kfree(k_buf_src);
  891. return err;
  892. }
  893. static int qcedev_sha_update(struct qcedev_async_req *qcedev_areq,
  894. struct qcedev_handle *handle,
  895. struct scatterlist *sg_src)
  896. {
  897. int err = 0;
  898. int i = 0;
  899. int j = 0;
  900. int k = 0;
  901. int num_entries = 0;
  902. uint32_t total = 0;
  903. if (!handle->sha_ctxt.init_done) {
  904. pr_err("%s Init was not called\n", __func__);
  905. return -EINVAL;
  906. }
  907. if (qcedev_areq->sha_op_req.data_len > QCE_MAX_OPER_DATA) {
  908. struct qcedev_sha_op_req *saved_req;
  909. struct qcedev_sha_op_req req;
  910. struct qcedev_sha_op_req *sreq = &qcedev_areq->sha_op_req;
  911. uint32_t req_size = 0;
  912. req_size = sizeof(struct qcedev_sha_op_req);
  913. /* save the original req structure */
  914. saved_req =
  915. kmalloc(req_size, GFP_KERNEL);
  916. if (saved_req == NULL) {
  917. pr_err("%s:Can't Allocate mem:saved_req 0x%lx\n",
  918. __func__, (uintptr_t)saved_req);
  919. return -ENOMEM;
  920. }
  921. memcpy(&req, sreq, sizeof(*sreq));
  922. memcpy(saved_req, sreq, sizeof(*sreq));
  923. i = 0;
  924. /* Address 32 KB at a time */
  925. while ((i < req.entries) && (err == 0)) {
  926. if (sreq->data[i].len > QCE_MAX_OPER_DATA) {
  927. sreq->data[0].len = QCE_MAX_OPER_DATA;
  928. if (i > 0) {
  929. sreq->data[0].vaddr =
  930. sreq->data[i].vaddr;
  931. }
  932. sreq->data_len = QCE_MAX_OPER_DATA;
  933. sreq->entries = 1;
  934. err = qcedev_sha_update_max_xfer(qcedev_areq,
  935. handle, sg_src);
  936. sreq->data[i].len = req.data[i].len -
  937. QCE_MAX_OPER_DATA;
  938. sreq->data[i].vaddr = req.data[i].vaddr +
  939. QCE_MAX_OPER_DATA;
  940. req.data[i].vaddr = sreq->data[i].vaddr;
  941. req.data[i].len = sreq->data[i].len;
  942. } else {
  943. total = 0;
  944. for (j = i; j < req.entries; j++) {
  945. num_entries++;
  946. if ((total + sreq->data[j].len) >=
  947. QCE_MAX_OPER_DATA) {
  948. sreq->data[j].len =
  949. (QCE_MAX_OPER_DATA - total);
  950. total = QCE_MAX_OPER_DATA;
  951. break;
  952. }
  953. total += sreq->data[j].len;
  954. }
  955. sreq->data_len = total;
  956. if (i > 0)
  957. for (k = 0; k < num_entries; k++) {
  958. sreq->data[k].len =
  959. sreq->data[i+k].len;
  960. sreq->data[k].vaddr =
  961. sreq->data[i+k].vaddr;
  962. }
  963. sreq->entries = num_entries;
  964. i = j;
  965. err = qcedev_sha_update_max_xfer(qcedev_areq,
  966. handle, sg_src);
  967. num_entries = 0;
  968. sreq->data[i].vaddr = req.data[i].vaddr +
  969. sreq->data[i].len;
  970. sreq->data[i].len = req.data[i].len -
  971. sreq->data[i].len;
  972. req.data[i].vaddr = sreq->data[i].vaddr;
  973. req.data[i].len = sreq->data[i].len;
  974. if (sreq->data[i].len == 0)
  975. i++;
  976. }
  977. } /* end of while ((i < req.entries) && (err == 0)) */
  978. /* Restore the original req structure */
  979. for (i = 0; i < saved_req->entries; i++) {
  980. sreq->data[i].len = saved_req->data[i].len;
  981. sreq->data[i].vaddr = saved_req->data[i].vaddr;
  982. }
  983. sreq->entries = saved_req->entries;
  984. sreq->data_len = saved_req->data_len;
  985. memset(saved_req, 0, req_size);
  986. kfree(saved_req);
  987. } else
  988. err = qcedev_sha_update_max_xfer(qcedev_areq, handle, sg_src);
  989. return err;
  990. }
  991. static int qcedev_sha_final(struct qcedev_async_req *qcedev_areq,
  992. struct qcedev_handle *handle)
  993. {
  994. int err = 0;
  995. struct scatterlist sg_src;
  996. uint32_t total;
  997. uint8_t *k_buf_src = NULL;
  998. uint32_t buf_size = 0;
  999. uint8_t *k_align_src = NULL;
  1000. if (!handle->sha_ctxt.init_done) {
  1001. pr_err("%s Init was not called\n", __func__);
  1002. return -EINVAL;
  1003. }
  1004. handle->sha_ctxt.last_blk = 1;
  1005. total = handle->sha_ctxt.trailing_buf_len;
  1006. buf_size = total + CACHE_LINE_SIZE * 2;
  1007. k_buf_src = kmalloc(buf_size, GFP_KERNEL);
  1008. if (k_buf_src == NULL)
  1009. return -ENOMEM;
  1010. k_align_src = (uint8_t *)ALIGN(((uintptr_t)k_buf_src),
  1011. CACHE_LINE_SIZE);
  1012. memcpy(k_align_src, &handle->sha_ctxt.trailing_buf[0], total);
  1013. qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src;
  1014. sg_init_one(qcedev_areq->sha_req.sreq.src, k_align_src, total);
  1015. qcedev_areq->sha_req.sreq.nbytes = total;
  1016. err = submit_req(qcedev_areq, handle);
  1017. handle->sha_ctxt.first_blk = 0;
  1018. handle->sha_ctxt.last_blk = 0;
  1019. handle->sha_ctxt.auth_data[0] = 0;
  1020. handle->sha_ctxt.auth_data[1] = 0;
  1021. handle->sha_ctxt.trailing_buf_len = 0;
  1022. handle->sha_ctxt.init_done = false;
  1023. memset(&handle->sha_ctxt.trailing_buf[0], 0, 64);
  1024. memset(k_buf_src, 0, buf_size);
  1025. kfree(k_buf_src);
  1026. qcedev_areq->sha_req.sreq.src = NULL;
  1027. return err;
  1028. }
  1029. static int qcedev_hash_cmac(struct qcedev_async_req *qcedev_areq,
  1030. struct qcedev_handle *handle,
  1031. struct scatterlist *sg_src)
  1032. {
  1033. int err = 0;
  1034. int i = 0;
  1035. uint32_t total;
  1036. uint8_t *user_src = NULL;
  1037. uint8_t *k_src = NULL;
  1038. uint8_t *k_buf_src = NULL;
  1039. uint32_t buf_size = 0;
  1040. total = qcedev_areq->sha_op_req.data_len;
  1041. if ((qcedev_areq->sha_op_req.authklen != QCEDEV_AES_KEY_128) &&
  1042. (qcedev_areq->sha_op_req.authklen != QCEDEV_AES_KEY_256)) {
  1043. pr_err("%s: unsupported key length\n", __func__);
  1044. return -EINVAL;
  1045. }
  1046. if (copy_from_user(&handle->sha_ctxt.authkey[0],
  1047. (void __user *)qcedev_areq->sha_op_req.authkey,
  1048. qcedev_areq->sha_op_req.authklen))
  1049. return -EFAULT;
  1050. if (total > U32_MAX - CACHE_LINE_SIZE * 2)
  1051. return -EINVAL;
  1052. buf_size = total + CACHE_LINE_SIZE * 2;
  1053. k_buf_src = kmalloc(buf_size, GFP_KERNEL);
  1054. if (k_buf_src == NULL)
  1055. return -ENOMEM;
  1056. k_src = k_buf_src;
  1057. /* Copy data from user src(s) */
  1058. user_src = qcedev_areq->sha_op_req.data[0].vaddr;
  1059. for (i = 0; i < qcedev_areq->sha_op_req.entries; i++) {
  1060. user_src = qcedev_areq->sha_op_req.data[i].vaddr;
  1061. if (user_src && copy_from_user(k_src, (void __user *)user_src,
  1062. qcedev_areq->sha_op_req.data[i].len)) {
  1063. memset(k_buf_src, 0, buf_size);
  1064. kfree(k_buf_src);
  1065. return -EFAULT;
  1066. }
  1067. k_src += qcedev_areq->sha_op_req.data[i].len;
  1068. }
  1069. qcedev_areq->sha_req.sreq.src = sg_src;
  1070. sg_init_one(qcedev_areq->sha_req.sreq.src, k_buf_src, total);
  1071. qcedev_areq->sha_req.sreq.nbytes = total;
  1072. handle->sha_ctxt.diglen = qcedev_areq->sha_op_req.diglen;
  1073. err = submit_req(qcedev_areq, handle);
  1074. memset(k_buf_src, 0, buf_size);
  1075. kfree(k_buf_src);
  1076. return err;
  1077. }
  1078. static int qcedev_set_hmac_auth_key(struct qcedev_async_req *areq,
  1079. struct qcedev_handle *handle,
  1080. struct scatterlist *sg_src)
  1081. {
  1082. int err = 0;
  1083. if (areq->sha_op_req.authklen <= QCEDEV_MAX_KEY_SIZE) {
  1084. qcedev_sha_init(areq, handle);
  1085. if (copy_from_user(&handle->sha_ctxt.authkey[0],
  1086. (void __user *)areq->sha_op_req.authkey,
  1087. areq->sha_op_req.authklen))
  1088. return -EFAULT;
  1089. } else {
  1090. struct qcedev_async_req authkey_areq;
  1091. uint8_t authkey[QCEDEV_MAX_SHA_BLOCK_SIZE];
  1092. init_completion(&authkey_areq.complete);
  1093. authkey_areq.sha_op_req.entries = 1;
  1094. authkey_areq.sha_op_req.data[0].vaddr =
  1095. areq->sha_op_req.authkey;
  1096. authkey_areq.sha_op_req.data[0].len = areq->sha_op_req.authklen;
  1097. authkey_areq.sha_op_req.data_len = areq->sha_op_req.authklen;
  1098. authkey_areq.sha_op_req.diglen = 0;
  1099. authkey_areq.handle = handle;
  1100. memset(&authkey_areq.sha_op_req.digest[0], 0,
  1101. QCEDEV_MAX_SHA_DIGEST);
  1102. if (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC)
  1103. authkey_areq.sha_op_req.alg = QCEDEV_ALG_SHA1;
  1104. if (areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC)
  1105. authkey_areq.sha_op_req.alg = QCEDEV_ALG_SHA256;
  1106. authkey_areq.op_type = QCEDEV_CRYPTO_OPER_SHA;
  1107. qcedev_sha_init(&authkey_areq, handle);
  1108. err = qcedev_sha_update(&authkey_areq, handle, sg_src);
  1109. if (!err)
  1110. err = qcedev_sha_final(&authkey_areq, handle);
  1111. else
  1112. return err;
  1113. memcpy(&authkey[0], &handle->sha_ctxt.digest[0],
  1114. handle->sha_ctxt.diglen);
  1115. qcedev_sha_init(areq, handle);
  1116. memcpy(&handle->sha_ctxt.authkey[0], &authkey[0],
  1117. handle->sha_ctxt.diglen);
  1118. }
  1119. return err;
  1120. }
  1121. static int qcedev_hmac_get_ohash(struct qcedev_async_req *qcedev_areq,
  1122. struct qcedev_handle *handle)
  1123. {
  1124. int err = 0;
  1125. struct scatterlist sg_src;
  1126. uint8_t *k_src = NULL;
  1127. uint32_t sha_block_size = 0;
  1128. uint32_t sha_digest_size = 0;
  1129. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) {
  1130. sha_digest_size = SHA1_DIGEST_SIZE;
  1131. sha_block_size = SHA1_BLOCK_SIZE;
  1132. } else {
  1133. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC) {
  1134. sha_digest_size = SHA256_DIGEST_SIZE;
  1135. sha_block_size = SHA256_BLOCK_SIZE;
  1136. }
  1137. }
  1138. k_src = kmalloc(sha_block_size, GFP_KERNEL);
  1139. if (k_src == NULL)
  1140. return -ENOMEM;
  1141. /* check for trailing buffer from previous updates and append it */
  1142. memcpy(k_src, &handle->sha_ctxt.trailing_buf[0],
  1143. handle->sha_ctxt.trailing_buf_len);
  1144. qcedev_areq->sha_req.sreq.src = (struct scatterlist *) &sg_src;
  1145. sg_init_one(qcedev_areq->sha_req.sreq.src, k_src, sha_block_size);
  1146. qcedev_areq->sha_req.sreq.nbytes = sha_block_size;
  1147. memset(&handle->sha_ctxt.trailing_buf[0], 0, sha_block_size);
  1148. memcpy(&handle->sha_ctxt.trailing_buf[0], &handle->sha_ctxt.digest[0],
  1149. sha_digest_size);
  1150. handle->sha_ctxt.trailing_buf_len = sha_digest_size;
  1151. handle->sha_ctxt.first_blk = 1;
  1152. handle->sha_ctxt.last_blk = 0;
  1153. handle->sha_ctxt.auth_data[0] = 0;
  1154. handle->sha_ctxt.auth_data[1] = 0;
  1155. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC) {
  1156. memcpy(&handle->sha_ctxt.digest[0],
  1157. &_std_init_vector_sha1_uint8[0], SHA1_DIGEST_SIZE);
  1158. handle->sha_ctxt.diglen = SHA1_DIGEST_SIZE;
  1159. }
  1160. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_SHA256_HMAC) {
  1161. memcpy(&handle->sha_ctxt.digest[0],
  1162. &_std_init_vector_sha256_uint8[0], SHA256_DIGEST_SIZE);
  1163. handle->sha_ctxt.diglen = SHA256_DIGEST_SIZE;
  1164. }
  1165. err = submit_req(qcedev_areq, handle);
  1166. handle->sha_ctxt.last_blk = 0;
  1167. handle->sha_ctxt.first_blk = 0;
  1168. memset(k_src, 0, sha_block_size);
  1169. kfree(k_src);
  1170. qcedev_areq->sha_req.sreq.src = NULL;
  1171. return err;
  1172. }
  1173. static int qcedev_hmac_update_iokey(struct qcedev_async_req *areq,
  1174. struct qcedev_handle *handle, bool ikey)
  1175. {
  1176. int i;
  1177. uint32_t constant;
  1178. uint32_t sha_block_size;
  1179. if (ikey)
  1180. constant = 0x36;
  1181. else
  1182. constant = 0x5c;
  1183. if (areq->sha_op_req.alg == QCEDEV_ALG_SHA1_HMAC)
  1184. sha_block_size = SHA1_BLOCK_SIZE;
  1185. else
  1186. sha_block_size = SHA256_BLOCK_SIZE;
  1187. memset(&handle->sha_ctxt.trailing_buf[0], 0, sha_block_size);
  1188. for (i = 0; i < sha_block_size; i++)
  1189. handle->sha_ctxt.trailing_buf[i] =
  1190. (handle->sha_ctxt.authkey[i] ^ constant);
  1191. handle->sha_ctxt.trailing_buf_len = sha_block_size;
  1192. return 0;
  1193. }
  1194. static int qcedev_hmac_init(struct qcedev_async_req *areq,
  1195. struct qcedev_handle *handle,
  1196. struct scatterlist *sg_src)
  1197. {
  1198. int err;
  1199. struct qcedev_control *podev = handle->cntl;
  1200. err = qcedev_set_hmac_auth_key(areq, handle, sg_src);
  1201. if (err)
  1202. return err;
  1203. if (!podev->ce_support.sha_hmac)
  1204. qcedev_hmac_update_iokey(areq, handle, true);
  1205. return 0;
  1206. }
  1207. static int qcedev_hmac_final(struct qcedev_async_req *areq,
  1208. struct qcedev_handle *handle)
  1209. {
  1210. int err;
  1211. struct qcedev_control *podev = handle->cntl;
  1212. err = qcedev_sha_final(areq, handle);
  1213. if (podev->ce_support.sha_hmac)
  1214. return err;
  1215. qcedev_hmac_update_iokey(areq, handle, false);
  1216. err = qcedev_hmac_get_ohash(areq, handle);
  1217. if (err)
  1218. return err;
  1219. err = qcedev_sha_final(areq, handle);
  1220. return err;
  1221. }
  1222. static int qcedev_hash_init(struct qcedev_async_req *areq,
  1223. struct qcedev_handle *handle,
  1224. struct scatterlist *sg_src)
  1225. {
  1226. if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) ||
  1227. (areq->sha_op_req.alg == QCEDEV_ALG_SHA256))
  1228. return qcedev_sha_init(areq, handle);
  1229. else
  1230. return qcedev_hmac_init(areq, handle, sg_src);
  1231. }
  1232. static int qcedev_hash_update(struct qcedev_async_req *qcedev_areq,
  1233. struct qcedev_handle *handle,
  1234. struct scatterlist *sg_src)
  1235. {
  1236. return qcedev_sha_update(qcedev_areq, handle, sg_src);
  1237. }
  1238. static int qcedev_hash_final(struct qcedev_async_req *areq,
  1239. struct qcedev_handle *handle)
  1240. {
  1241. if ((areq->sha_op_req.alg == QCEDEV_ALG_SHA1) ||
  1242. (areq->sha_op_req.alg == QCEDEV_ALG_SHA256))
  1243. return qcedev_sha_final(areq, handle);
  1244. else
  1245. return qcedev_hmac_final(areq, handle);
  1246. }
  1247. static int qcedev_vbuf_ablk_cipher_max_xfer(struct qcedev_async_req *areq,
  1248. int *di, struct qcedev_handle *handle,
  1249. uint8_t *k_align_src)
  1250. {
  1251. int err = 0;
  1252. int i = 0;
  1253. int dst_i = *di;
  1254. struct scatterlist sg_src;
  1255. uint32_t byteoffset = 0;
  1256. uint8_t *user_src = NULL;
  1257. uint8_t *k_align_dst = k_align_src;
  1258. struct qcedev_cipher_op_req *creq = &areq->cipher_op_req;
  1259. if (areq->cipher_op_req.mode == QCEDEV_AES_MODE_CTR)
  1260. byteoffset = areq->cipher_op_req.byteoffset;
  1261. user_src = areq->cipher_op_req.vbuf.src[0].vaddr;
  1262. if (user_src && copy_from_user((k_align_src + byteoffset),
  1263. (void __user *)user_src,
  1264. areq->cipher_op_req.vbuf.src[0].len))
  1265. return -EFAULT;
  1266. k_align_src += byteoffset + areq->cipher_op_req.vbuf.src[0].len;
  1267. for (i = 1; i < areq->cipher_op_req.entries; i++) {
  1268. user_src = areq->cipher_op_req.vbuf.src[i].vaddr;
  1269. if (user_src && copy_from_user(k_align_src,
  1270. (void __user *)user_src,
  1271. areq->cipher_op_req.vbuf.src[i].len)) {
  1272. return -EFAULT;
  1273. }
  1274. k_align_src += areq->cipher_op_req.vbuf.src[i].len;
  1275. }
  1276. /* restore src beginning */
  1277. k_align_src = k_align_dst;
  1278. areq->cipher_op_req.data_len += byteoffset;
  1279. areq->cipher_req.creq.src = (struct scatterlist *) &sg_src;
  1280. areq->cipher_req.creq.dst = (struct scatterlist *) &sg_src;
  1281. /* In place encryption/decryption */
  1282. sg_init_one(areq->cipher_req.creq.src,
  1283. k_align_dst,
  1284. areq->cipher_op_req.data_len);
  1285. areq->cipher_req.creq.cryptlen = areq->cipher_op_req.data_len;
  1286. areq->cipher_req.creq.iv = areq->cipher_op_req.iv;
  1287. areq->cipher_op_req.entries = 1;
  1288. err = submit_req(areq, handle);
  1289. /* copy data to destination buffer*/
  1290. creq->data_len -= byteoffset;
  1291. while (creq->data_len > 0) {
  1292. if (creq->vbuf.dst[dst_i].len <= creq->data_len) {
  1293. if (err == 0 && copy_to_user(
  1294. (void __user *)creq->vbuf.dst[dst_i].vaddr,
  1295. (k_align_dst + byteoffset),
  1296. creq->vbuf.dst[dst_i].len)) {
  1297. err = -EFAULT;
  1298. goto exit;
  1299. }
  1300. k_align_dst += creq->vbuf.dst[dst_i].len;
  1301. creq->data_len -= creq->vbuf.dst[dst_i].len;
  1302. dst_i++;
  1303. } else {
  1304. if (err == 0 && copy_to_user(
  1305. (void __user *)creq->vbuf.dst[dst_i].vaddr,
  1306. (k_align_dst + byteoffset),
  1307. creq->data_len)) {
  1308. err = -EFAULT;
  1309. goto exit;
  1310. }
  1311. k_align_dst += creq->data_len;
  1312. creq->vbuf.dst[dst_i].len -= creq->data_len;
  1313. creq->vbuf.dst[dst_i].vaddr += creq->data_len;
  1314. creq->data_len = 0;
  1315. }
  1316. }
  1317. *di = dst_i;
  1318. exit:
  1319. areq->cipher_req.creq.src = NULL;
  1320. areq->cipher_req.creq.dst = NULL;
  1321. return err;
  1322. };
  1323. static int qcedev_vbuf_ablk_cipher(struct qcedev_async_req *areq,
  1324. struct qcedev_handle *handle)
  1325. {
  1326. int err = 0;
  1327. int di = 0;
  1328. int i = 0;
  1329. int j = 0;
  1330. int k = 0;
  1331. uint32_t byteoffset = 0;
  1332. int num_entries = 0;
  1333. uint32_t total = 0;
  1334. uint32_t len;
  1335. uint8_t *k_buf_src = NULL;
  1336. uint32_t buf_size = 0;
  1337. uint8_t *k_align_src = NULL;
  1338. uint32_t max_data_xfer;
  1339. struct qcedev_cipher_op_req *saved_req;
  1340. uint32_t req_size = 0;
  1341. struct qcedev_cipher_op_req *creq = &areq->cipher_op_req;
  1342. total = 0;
  1343. if (areq->cipher_op_req.mode == QCEDEV_AES_MODE_CTR)
  1344. byteoffset = areq->cipher_op_req.byteoffset;
  1345. buf_size = QCE_MAX_OPER_DATA + CACHE_LINE_SIZE * 2;
  1346. k_buf_src = kmalloc(buf_size, GFP_KERNEL);
  1347. if (k_buf_src == NULL)
  1348. return -ENOMEM;
  1349. k_align_src = (uint8_t *)ALIGN(((uintptr_t)k_buf_src),
  1350. CACHE_LINE_SIZE);
  1351. max_data_xfer = QCE_MAX_OPER_DATA - byteoffset;
  1352. req_size = sizeof(struct qcedev_cipher_op_req);
  1353. saved_req = kmemdup(creq, req_size, GFP_KERNEL);
  1354. if (saved_req == NULL) {
  1355. memset(k_buf_src, 0, buf_size);
  1356. kfree(k_buf_src);
  1357. return -ENOMEM;
  1358. }
  1359. if (areq->cipher_op_req.data_len > max_data_xfer) {
  1360. struct qcedev_cipher_op_req req;
  1361. /* save the original req structure */
  1362. memcpy(&req, creq, sizeof(struct qcedev_cipher_op_req));
  1363. i = 0;
  1364. /* Address 32 KB at a time */
  1365. while ((i < req.entries) && (err == 0)) {
  1366. if (creq->vbuf.src[i].len > max_data_xfer) {
  1367. creq->vbuf.src[0].len = max_data_xfer;
  1368. if (i > 0) {
  1369. creq->vbuf.src[0].vaddr =
  1370. creq->vbuf.src[i].vaddr;
  1371. }
  1372. creq->data_len = max_data_xfer;
  1373. creq->entries = 1;
  1374. err = qcedev_vbuf_ablk_cipher_max_xfer(areq,
  1375. &di, handle, k_align_src);
  1376. if (err < 0) {
  1377. memset(saved_req, 0, req_size);
  1378. memset(k_buf_src, 0, buf_size);
  1379. kfree(k_buf_src);
  1380. kfree(saved_req);
  1381. return err;
  1382. }
  1383. creq->vbuf.src[i].len = req.vbuf.src[i].len -
  1384. max_data_xfer;
  1385. creq->vbuf.src[i].vaddr =
  1386. req.vbuf.src[i].vaddr +
  1387. max_data_xfer;
  1388. req.vbuf.src[i].vaddr =
  1389. creq->vbuf.src[i].vaddr;
  1390. req.vbuf.src[i].len = creq->vbuf.src[i].len;
  1391. } else {
  1392. total = areq->cipher_op_req.byteoffset;
  1393. for (j = i; j < req.entries; j++) {
  1394. num_entries++;
  1395. if ((total + creq->vbuf.src[j].len)
  1396. >= max_data_xfer) {
  1397. creq->vbuf.src[j].len =
  1398. max_data_xfer - total;
  1399. total = max_data_xfer;
  1400. break;
  1401. }
  1402. total += creq->vbuf.src[j].len;
  1403. }
  1404. creq->data_len = total;
  1405. if (i > 0)
  1406. for (k = 0; k < num_entries; k++) {
  1407. creq->vbuf.src[k].len =
  1408. creq->vbuf.src[i+k].len;
  1409. creq->vbuf.src[k].vaddr =
  1410. creq->vbuf.src[i+k].vaddr;
  1411. }
  1412. creq->entries = num_entries;
  1413. i = j;
  1414. err = qcedev_vbuf_ablk_cipher_max_xfer(areq,
  1415. &di, handle, k_align_src);
  1416. if (err < 0) {
  1417. memset(saved_req, 0, req_size);
  1418. memset(k_buf_src, 0, buf_size);
  1419. kfree(k_buf_src);
  1420. kfree(saved_req);
  1421. return err;
  1422. }
  1423. num_entries = 0;
  1424. areq->cipher_op_req.byteoffset = 0;
  1425. creq->vbuf.src[i].vaddr = req.vbuf.src[i].vaddr
  1426. + creq->vbuf.src[i].len;
  1427. creq->vbuf.src[i].len = req.vbuf.src[i].len -
  1428. creq->vbuf.src[i].len;
  1429. req.vbuf.src[i].vaddr =
  1430. creq->vbuf.src[i].vaddr;
  1431. req.vbuf.src[i].len = creq->vbuf.src[i].len;
  1432. if (creq->vbuf.src[i].len == 0)
  1433. i++;
  1434. }
  1435. areq->cipher_op_req.byteoffset = 0;
  1436. max_data_xfer = QCE_MAX_OPER_DATA;
  1437. byteoffset = 0;
  1438. } /* end of while ((i < req.entries) && (err == 0)) */
  1439. } else
  1440. err = qcedev_vbuf_ablk_cipher_max_xfer(areq, &di, handle,
  1441. k_align_src);
  1442. /* Restore the original req structure */
  1443. for (i = 0; i < saved_req->entries; i++) {
  1444. creq->vbuf.src[i].len = saved_req->vbuf.src[i].len;
  1445. creq->vbuf.src[i].vaddr = saved_req->vbuf.src[i].vaddr;
  1446. }
  1447. for (len = 0, i = 0; len < saved_req->data_len; i++) {
  1448. creq->vbuf.dst[i].len = saved_req->vbuf.dst[i].len;
  1449. creq->vbuf.dst[i].vaddr = saved_req->vbuf.dst[i].vaddr;
  1450. len += saved_req->vbuf.dst[i].len;
  1451. }
  1452. creq->entries = saved_req->entries;
  1453. creq->data_len = saved_req->data_len;
  1454. creq->byteoffset = saved_req->byteoffset;
  1455. memset(saved_req, 0, req_size);
  1456. memset(k_buf_src, 0, buf_size);
  1457. kfree(saved_req);
  1458. kfree(k_buf_src);
  1459. return err;
  1460. }
  1461. static int qcedev_smmu_ablk_offload_cipher(struct qcedev_async_req *areq,
  1462. struct qcedev_handle *handle)
  1463. {
  1464. int i = 0;
  1465. int err = 0;
  1466. size_t byteoffset = 0;
  1467. size_t transfer_data_len = 0;
  1468. size_t pending_data_len = 0;
  1469. size_t max_data_xfer = MAX_CEHW_REQ_TRANSFER_SIZE - byteoffset;
  1470. uint8_t *user_src = NULL;
  1471. uint8_t *user_dst = NULL;
  1472. struct scatterlist sg_src;
  1473. struct scatterlist sg_dst;
  1474. if (areq->offload_cipher_op_req.mode == QCEDEV_AES_MODE_CTR)
  1475. byteoffset = areq->offload_cipher_op_req.byteoffset;
  1476. /*
  1477. * areq has two components:
  1478. * a) Request that comes from userspace i.e. offload_cipher_op_req
  1479. * b) Request that QCE understands - skcipher i.e. cipher_req.creq
  1480. * skcipher has sglist pointers src and dest that would carry
  1481. * data to/from CE.
  1482. */
  1483. areq->cipher_req.creq.src = &sg_src;
  1484. areq->cipher_req.creq.dst = &sg_dst;
  1485. sg_init_table(&sg_src, 1);
  1486. sg_init_table(&sg_dst, 1);
  1487. for (i = 0; i < areq->offload_cipher_op_req.entries; i++) {
  1488. transfer_data_len = 0;
  1489. pending_data_len = areq->offload_cipher_op_req.vbuf.src[i].len;
  1490. user_src = areq->offload_cipher_op_req.vbuf.src[i].vaddr;
  1491. user_src += byteoffset;
  1492. user_dst = areq->offload_cipher_op_req.vbuf.dst[i].vaddr;
  1493. user_dst += byteoffset;
  1494. areq->cipher_req.creq.iv = areq->offload_cipher_op_req.iv;
  1495. while (pending_data_len) {
  1496. transfer_data_len = min(max_data_xfer,
  1497. pending_data_len);
  1498. sg_src.dma_address = (dma_addr_t)user_src;
  1499. sg_dst.dma_address = (dma_addr_t)user_dst;
  1500. areq->cipher_req.creq.cryptlen = transfer_data_len;
  1501. sg_src.length = transfer_data_len;
  1502. sg_dst.length = transfer_data_len;
  1503. err = submit_req(areq, handle);
  1504. if (err) {
  1505. pr_err("%s: Error processing req, err = %d\n",
  1506. __func__, err);
  1507. goto exit;
  1508. }
  1509. /* update data len to be processed */
  1510. pending_data_len -= transfer_data_len;
  1511. user_src += transfer_data_len;
  1512. user_dst += transfer_data_len;
  1513. }
  1514. }
  1515. exit:
  1516. areq->cipher_req.creq.src = NULL;
  1517. areq->cipher_req.creq.dst = NULL;
  1518. return err;
  1519. }
  1520. static int qcedev_check_cipher_key(struct qcedev_cipher_op_req *req,
  1521. struct qcedev_control *podev)
  1522. {
  1523. /* if intending to use HW key make sure key fields are set
  1524. * correctly and HW key is indeed supported in target
  1525. */
  1526. if (req->encklen == 0) {
  1527. int i;
  1528. for (i = 0; i < QCEDEV_MAX_KEY_SIZE; i++) {
  1529. if (req->enckey[i]) {
  1530. pr_err("%s: Invalid key: non-zero key input\n",
  1531. __func__);
  1532. goto error;
  1533. }
  1534. }
  1535. if ((req->op != QCEDEV_OPER_ENC_NO_KEY) &&
  1536. (req->op != QCEDEV_OPER_DEC_NO_KEY))
  1537. if (!podev->platform_support.hw_key_support) {
  1538. pr_err("%s: Invalid op %d\n", __func__,
  1539. (uint32_t)req->op);
  1540. goto error;
  1541. }
  1542. } else {
  1543. if (req->encklen == QCEDEV_AES_KEY_192) {
  1544. if (!podev->ce_support.aes_key_192) {
  1545. pr_err("%s: AES-192 not supported\n", __func__);
  1546. goto error;
  1547. }
  1548. } else {
  1549. /* if not using HW key make sure key
  1550. * length is valid
  1551. */
  1552. if (req->mode == QCEDEV_AES_MODE_XTS) {
  1553. if ((req->encklen != QCEDEV_AES_KEY_128*2) &&
  1554. (req->encklen != QCEDEV_AES_KEY_256*2)) {
  1555. pr_err("%s: unsupported key size: %d\n",
  1556. __func__, req->encklen);
  1557. goto error;
  1558. }
  1559. } else {
  1560. if ((req->encklen != QCEDEV_AES_KEY_128) &&
  1561. (req->encklen != QCEDEV_AES_KEY_256)) {
  1562. pr_err("%s: unsupported key size %d\n",
  1563. __func__, req->encklen);
  1564. goto error;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. return 0;
  1570. error:
  1571. return -EINVAL;
  1572. }
  1573. static int qcedev_check_cipher_params(struct qcedev_cipher_op_req *req,
  1574. struct qcedev_control *podev)
  1575. {
  1576. uint32_t total = 0;
  1577. uint32_t i;
  1578. if (req->use_pmem) {
  1579. pr_err("%s: Use of PMEM is not supported\n", __func__);
  1580. goto error;
  1581. }
  1582. if ((req->entries == 0) || (req->data_len == 0) ||
  1583. (req->entries > QCEDEV_MAX_BUFFERS)) {
  1584. pr_err("%s: Invalid cipher length/entries\n", __func__);
  1585. goto error;
  1586. }
  1587. if ((req->alg >= QCEDEV_ALG_LAST) ||
  1588. (req->mode >= QCEDEV_AES_DES_MODE_LAST)) {
  1589. pr_err("%s: Invalid algorithm %d\n", __func__,
  1590. (uint32_t)req->alg);
  1591. goto error;
  1592. }
  1593. if ((req->mode == QCEDEV_AES_MODE_XTS) &&
  1594. (!podev->ce_support.aes_xts)) {
  1595. pr_err("%s: XTS algorithm is not supported\n", __func__);
  1596. goto error;
  1597. }
  1598. if (req->alg == QCEDEV_ALG_AES) {
  1599. if (qcedev_check_cipher_key(req, podev))
  1600. goto error;
  1601. }
  1602. /* if using a byteoffset, make sure it is CTR mode using vbuf */
  1603. if (req->byteoffset) {
  1604. if (req->mode != QCEDEV_AES_MODE_CTR) {
  1605. pr_err("%s: Operation on byte offset not supported\n",
  1606. __func__);
  1607. goto error;
  1608. }
  1609. if (req->byteoffset >= AES_CE_BLOCK_SIZE) {
  1610. pr_err("%s: Invalid byte offset\n", __func__);
  1611. goto error;
  1612. }
  1613. total = req->byteoffset;
  1614. for (i = 0; i < req->entries; i++) {
  1615. if (total > U32_MAX - req->vbuf.src[i].len) {
  1616. pr_err("%s:Integer overflow on total src len\n",
  1617. __func__);
  1618. goto error;
  1619. }
  1620. total += req->vbuf.src[i].len;
  1621. }
  1622. }
  1623. if (req->data_len < req->byteoffset) {
  1624. pr_err("%s: req data length %u is less than byteoffset %u\n",
  1625. __func__, req->data_len, req->byteoffset);
  1626. goto error;
  1627. }
  1628. /* Ensure IV size */
  1629. if (req->ivlen > QCEDEV_MAX_IV_SIZE) {
  1630. pr_err("%s: ivlen is not correct: %u\n", __func__, req->ivlen);
  1631. goto error;
  1632. }
  1633. /* Ensure Key size */
  1634. if (req->encklen > QCEDEV_MAX_KEY_SIZE) {
  1635. pr_err("%s: Klen is not correct: %u\n", __func__, req->encklen);
  1636. goto error;
  1637. }
  1638. /* Ensure zer ivlen for ECB mode */
  1639. if (req->ivlen > 0) {
  1640. if ((req->mode == QCEDEV_AES_MODE_ECB) ||
  1641. (req->mode == QCEDEV_DES_MODE_ECB)) {
  1642. pr_err("%s: Expecting a zero length IV\n", __func__);
  1643. goto error;
  1644. }
  1645. } else {
  1646. if ((req->mode != QCEDEV_AES_MODE_ECB) &&
  1647. (req->mode != QCEDEV_DES_MODE_ECB)) {
  1648. pr_err("%s: Expecting a non-zero ength IV\n", __func__);
  1649. goto error;
  1650. }
  1651. }
  1652. /* Check for sum of all dst length is equal to data_len */
  1653. for (i = 0, total = 0; i < req->entries; i++) {
  1654. if (!req->vbuf.dst[i].vaddr && req->vbuf.dst[i].len) {
  1655. pr_err("%s: NULL req dst vbuf[%d] with length %d\n",
  1656. __func__, i, req->vbuf.dst[i].len);
  1657. goto error;
  1658. }
  1659. if (req->vbuf.dst[i].len >= U32_MAX - total) {
  1660. pr_err("%s: Integer overflow on total req dst vbuf length\n",
  1661. __func__);
  1662. goto error;
  1663. }
  1664. total += req->vbuf.dst[i].len;
  1665. }
  1666. if (total != req->data_len) {
  1667. pr_err("%s: Total (i=%d) dst(%d) buf size != data_len (%d)\n",
  1668. __func__, i, total, req->data_len);
  1669. goto error;
  1670. }
  1671. /* Check for sum of all src length is equal to data_len */
  1672. for (i = 0, total = 0; i < req->entries; i++) {
  1673. if (!req->vbuf.src[i].vaddr && req->vbuf.src[i].len) {
  1674. pr_err("%s: NULL req src vbuf[%d] with length %d\n",
  1675. __func__, i, req->vbuf.src[i].len);
  1676. goto error;
  1677. }
  1678. if (req->vbuf.src[i].len > U32_MAX - total) {
  1679. pr_err("%s: Integer overflow on total req src vbuf length\n",
  1680. __func__);
  1681. goto error;
  1682. }
  1683. total += req->vbuf.src[i].len;
  1684. }
  1685. if (total != req->data_len) {
  1686. pr_err("%s: Total src(%d) buf size != data_len (%d)\n",
  1687. __func__, total, req->data_len);
  1688. goto error;
  1689. }
  1690. return 0;
  1691. error:
  1692. return -EINVAL;
  1693. }
  1694. static int qcedev_check_sha_params(struct qcedev_sha_op_req *req,
  1695. struct qcedev_control *podev)
  1696. {
  1697. uint32_t total = 0;
  1698. uint32_t i;
  1699. if ((req->alg == QCEDEV_ALG_AES_CMAC) &&
  1700. (!podev->ce_support.cmac)) {
  1701. pr_err("%s: CMAC not supported\n", __func__);
  1702. goto sha_error;
  1703. }
  1704. if ((!req->entries) || (req->entries > QCEDEV_MAX_BUFFERS)) {
  1705. pr_err("%s: Invalid num entries (%d)\n",
  1706. __func__, req->entries);
  1707. goto sha_error;
  1708. }
  1709. if (req->alg >= QCEDEV_ALG_SHA_ALG_LAST) {
  1710. pr_err("%s: Invalid algorithm (%d)\n", __func__, req->alg);
  1711. goto sha_error;
  1712. }
  1713. if ((req->alg == QCEDEV_ALG_SHA1_HMAC) ||
  1714. (req->alg == QCEDEV_ALG_SHA256_HMAC)) {
  1715. if (req->authkey == NULL) {
  1716. pr_err("%s: Invalid authkey pointer\n", __func__);
  1717. goto sha_error;
  1718. }
  1719. if (req->authklen <= 0) {
  1720. pr_err("%s: Invalid authkey length (%d)\n",
  1721. __func__, req->authklen);
  1722. goto sha_error;
  1723. }
  1724. }
  1725. if (req->alg == QCEDEV_ALG_AES_CMAC) {
  1726. if ((req->authklen != QCEDEV_AES_KEY_128) &&
  1727. (req->authklen != QCEDEV_AES_KEY_256)) {
  1728. pr_err("%s: unsupported key length\n", __func__);
  1729. goto sha_error;
  1730. }
  1731. }
  1732. /* Check for sum of all src length is equal to data_len */
  1733. for (i = 0, total = 0; i < req->entries; i++) {
  1734. if (req->data[i].len > U32_MAX - total) {
  1735. pr_err("%s: Integer overflow on total req buf length\n",
  1736. __func__);
  1737. goto sha_error;
  1738. }
  1739. total += req->data[i].len;
  1740. }
  1741. if (total != req->data_len) {
  1742. pr_err("%s: Total src(%d) buf size != data_len (%d)\n",
  1743. __func__, total, req->data_len);
  1744. goto sha_error;
  1745. }
  1746. return 0;
  1747. sha_error:
  1748. return -EINVAL;
  1749. }
  1750. static int qcedev_check_offload_cipher_key(struct qcedev_offload_cipher_op_req *req,
  1751. struct qcedev_control *podev)
  1752. {
  1753. if (req->encklen == 0)
  1754. return -EINVAL;
  1755. /* AES-192 is not a valid option for OFFLOAD use case */
  1756. if ((req->encklen != QCEDEV_AES_KEY_128) &&
  1757. (req->encklen != QCEDEV_AES_KEY_256)) {
  1758. pr_err("%s: unsupported key size %d\n",
  1759. __func__, req->encklen);
  1760. goto error;
  1761. }
  1762. return 0;
  1763. error:
  1764. return -EINVAL;
  1765. }
  1766. static int qcedev_check_offload_cipher_params(struct qcedev_offload_cipher_op_req *req,
  1767. struct qcedev_control *podev)
  1768. {
  1769. uint32_t total = 0;
  1770. int i = 0;
  1771. if ((req->entries == 0) || (req->data_len == 0) ||
  1772. (req->entries > QCEDEV_MAX_BUFFERS)) {
  1773. pr_err("%s: Invalid cipher length/entries\n", __func__);
  1774. goto error;
  1775. }
  1776. if ((req->alg != QCEDEV_ALG_AES) ||
  1777. (req->mode > QCEDEV_AES_MODE_CTR)) {
  1778. pr_err("%s: Invalid algorithm %d\n", __func__,
  1779. (uint32_t)req->alg);
  1780. goto error;
  1781. }
  1782. if (qcedev_check_offload_cipher_key(req, podev))
  1783. goto error;
  1784. if (req->block_offset >= AES_CE_BLOCK_SIZE)
  1785. goto error;
  1786. /* if using a byteoffset, make sure it is CTR mode using vbuf */
  1787. if (req->byteoffset) {
  1788. if (req->mode != QCEDEV_AES_MODE_CTR) {
  1789. pr_err("%s: Operation on byte offset not supported\n",
  1790. __func__);
  1791. goto error;
  1792. }
  1793. if (req->byteoffset >= AES_CE_BLOCK_SIZE) {
  1794. pr_err("%s: Invalid byte offset\n", __func__);
  1795. goto error;
  1796. }
  1797. total = req->byteoffset;
  1798. for (i = 0; i < req->entries; i++) {
  1799. if (total > U32_MAX - req->vbuf.src[i].len) {
  1800. pr_err("%s:Int overflow on total src len\n",
  1801. __func__);
  1802. goto error;
  1803. }
  1804. total += req->vbuf.src[i].len;
  1805. }
  1806. }
  1807. if (req->data_len < req->byteoffset) {
  1808. pr_err("%s: req data length %u is less than byteoffset %u\n",
  1809. __func__, req->data_len, req->byteoffset);
  1810. goto error;
  1811. }
  1812. /* Ensure IV size */
  1813. if (req->ivlen > QCEDEV_MAX_IV_SIZE) {
  1814. pr_err("%s: ivlen is not correct: %u\n", __func__, req->ivlen);
  1815. goto error;
  1816. }
  1817. /* Ensure Key size */
  1818. if (req->encklen > QCEDEV_MAX_KEY_SIZE) {
  1819. pr_err("%s: Klen is not correct: %u\n", __func__,
  1820. req->encklen);
  1821. goto error;
  1822. }
  1823. /* Check for sum of all dst length is equal to data_len */
  1824. for (i = 0, total = 0; i < req->entries; i++) {
  1825. if (!req->vbuf.dst[i].vaddr && req->vbuf.dst[i].len) {
  1826. pr_err("%s: NULL req dst vbuf[%d] with length %d\n",
  1827. __func__, i, req->vbuf.dst[i].len);
  1828. goto error;
  1829. }
  1830. if (req->vbuf.dst[i].len >= U32_MAX - total) {
  1831. pr_err("%s: Int overflow on total req dst vbuf len\n",
  1832. __func__);
  1833. goto error;
  1834. }
  1835. total += req->vbuf.dst[i].len;
  1836. }
  1837. if (total != req->data_len) {
  1838. pr_err("%s: Total (i=%d) dst(%d) buf size != data_len (%d)\n",
  1839. __func__, i, total, req->data_len);
  1840. goto error;
  1841. }
  1842. /* Check for sum of all src length is equal to data_len */
  1843. for (i = 0, total = 0; i < req->entries; i++) {
  1844. if (!req->vbuf.src[i].vaddr && req->vbuf.src[i].len) {
  1845. pr_err("%s: NULL req src vbuf[%d] with length %d\n",
  1846. __func__, i, req->vbuf.src[i].len);
  1847. goto error;
  1848. }
  1849. if (req->vbuf.src[i].len > U32_MAX - total) {
  1850. pr_err("%s: Int overflow on total req src vbuf len\n",
  1851. __func__);
  1852. goto error;
  1853. }
  1854. total += req->vbuf.src[i].len;
  1855. }
  1856. if (total != req->data_len) {
  1857. pr_err("%s: Total src(%d) buf size != data_len (%d)\n",
  1858. __func__, total, req->data_len);
  1859. goto error;
  1860. }
  1861. return 0;
  1862. error:
  1863. return -EINVAL;
  1864. }
  1865. long qcedev_ioctl(struct file *file,
  1866. unsigned int cmd, unsigned long arg)
  1867. {
  1868. int err = 0;
  1869. struct qcedev_handle *handle;
  1870. struct qcedev_control *podev;
  1871. struct qcedev_async_req *qcedev_areq;
  1872. struct qcedev_stat *pstat;
  1873. qcedev_areq = kzalloc(sizeof(struct qcedev_async_req), GFP_KERNEL);
  1874. if (!qcedev_areq)
  1875. return -ENOMEM;
  1876. handle = file->private_data;
  1877. podev = handle->cntl;
  1878. qcedev_areq->handle = handle;
  1879. if (podev == NULL || podev->magic != QCEDEV_MAGIC) {
  1880. pr_err("%s: invalid handle %pK\n",
  1881. __func__, podev);
  1882. err = -ENOENT;
  1883. goto exit_free_qcedev_areq;
  1884. }
  1885. /* Verify user arguments. */
  1886. if (_IOC_TYPE(cmd) != QCEDEV_IOC_MAGIC) {
  1887. err = -ENOTTY;
  1888. goto exit_free_qcedev_areq;
  1889. }
  1890. init_completion(&qcedev_areq->complete);
  1891. pstat = &_qcedev_stat;
  1892. switch (cmd) {
  1893. case QCEDEV_IOCTL_ENC_REQ:
  1894. case QCEDEV_IOCTL_DEC_REQ:
  1895. if (copy_from_user(&qcedev_areq->cipher_op_req,
  1896. (void __user *)arg,
  1897. sizeof(struct qcedev_cipher_op_req))) {
  1898. err = -EFAULT;
  1899. goto exit_free_qcedev_areq;
  1900. }
  1901. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_CIPHER;
  1902. if (qcedev_check_cipher_params(&qcedev_areq->cipher_op_req,
  1903. podev)) {
  1904. err = -EINVAL;
  1905. goto exit_free_qcedev_areq;
  1906. }
  1907. err = qcedev_vbuf_ablk_cipher(qcedev_areq, handle);
  1908. if (err)
  1909. goto exit_free_qcedev_areq;
  1910. if (copy_to_user((void __user *)arg,
  1911. &qcedev_areq->cipher_op_req,
  1912. sizeof(struct qcedev_cipher_op_req))) {
  1913. err = -EFAULT;
  1914. goto exit_free_qcedev_areq;
  1915. }
  1916. break;
  1917. case QCEDEV_IOCTL_OFFLOAD_OP_REQ:
  1918. if (copy_from_user(&qcedev_areq->offload_cipher_op_req,
  1919. (void __user *)arg,
  1920. sizeof(struct qcedev_offload_cipher_op_req))) {
  1921. err = -EFAULT;
  1922. goto exit_free_qcedev_areq;
  1923. }
  1924. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_OFFLOAD_CIPHER;
  1925. if (qcedev_check_offload_cipher_params(
  1926. &qcedev_areq->offload_cipher_op_req, podev)) {
  1927. err = -EINVAL;
  1928. goto exit_free_qcedev_areq;
  1929. }
  1930. qcedev_areq->offload_cipher_op_req.err = QCEDEV_OFFLOAD_NO_ERROR;
  1931. err = qcedev_smmu_ablk_offload_cipher(qcedev_areq, handle);
  1932. if (err)
  1933. goto exit_free_qcedev_areq;
  1934. if (copy_to_user((void __user *)arg,
  1935. &qcedev_areq->offload_cipher_op_req,
  1936. sizeof(struct qcedev_offload_cipher_op_req))) {
  1937. err = -EFAULT;
  1938. goto exit_free_qcedev_areq;
  1939. }
  1940. break;
  1941. case QCEDEV_IOCTL_SHA_INIT_REQ:
  1942. {
  1943. struct scatterlist sg_src;
  1944. if (copy_from_user(&qcedev_areq->sha_op_req,
  1945. (void __user *)arg,
  1946. sizeof(struct qcedev_sha_op_req))) {
  1947. err = -EFAULT;
  1948. goto exit_free_qcedev_areq;
  1949. }
  1950. mutex_lock(&hash_access_lock);
  1951. if (qcedev_check_sha_params(&qcedev_areq->sha_op_req, podev)) {
  1952. mutex_unlock(&hash_access_lock);
  1953. err = -EINVAL;
  1954. goto exit_free_qcedev_areq;
  1955. }
  1956. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_SHA;
  1957. err = qcedev_hash_init(qcedev_areq, handle, &sg_src);
  1958. if (err) {
  1959. mutex_unlock(&hash_access_lock);
  1960. goto exit_free_qcedev_areq;
  1961. }
  1962. mutex_unlock(&hash_access_lock);
  1963. if (copy_to_user((void __user *)arg, &qcedev_areq->sha_op_req,
  1964. sizeof(struct qcedev_sha_op_req))) {
  1965. err = -EFAULT;
  1966. goto exit_free_qcedev_areq;
  1967. }
  1968. handle->sha_ctxt.init_done = true;
  1969. }
  1970. break;
  1971. case QCEDEV_IOCTL_GET_CMAC_REQ:
  1972. if (!podev->ce_support.cmac) {
  1973. err = -ENOTTY;
  1974. goto exit_free_qcedev_areq;
  1975. }
  1976. fallthrough;
  1977. case QCEDEV_IOCTL_SHA_UPDATE_REQ:
  1978. {
  1979. struct scatterlist sg_src;
  1980. if (copy_from_user(&qcedev_areq->sha_op_req,
  1981. (void __user *)arg,
  1982. sizeof(struct qcedev_sha_op_req))) {
  1983. err = -EFAULT;
  1984. goto exit_free_qcedev_areq;
  1985. }
  1986. mutex_lock(&hash_access_lock);
  1987. if (qcedev_check_sha_params(&qcedev_areq->sha_op_req, podev)) {
  1988. mutex_unlock(&hash_access_lock);
  1989. err = -EINVAL;
  1990. goto exit_free_qcedev_areq;
  1991. }
  1992. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_SHA;
  1993. if (qcedev_areq->sha_op_req.alg == QCEDEV_ALG_AES_CMAC) {
  1994. err = qcedev_hash_cmac(qcedev_areq, handle, &sg_src);
  1995. if (err) {
  1996. mutex_unlock(&hash_access_lock);
  1997. goto exit_free_qcedev_areq;
  1998. }
  1999. } else {
  2000. if (!handle->sha_ctxt.init_done) {
  2001. pr_err("%s Init was not called\n", __func__);
  2002. mutex_unlock(&hash_access_lock);
  2003. err = -EINVAL;
  2004. goto exit_free_qcedev_areq;
  2005. }
  2006. err = qcedev_hash_update(qcedev_areq, handle, &sg_src);
  2007. if (err) {
  2008. mutex_unlock(&hash_access_lock);
  2009. goto exit_free_qcedev_areq;
  2010. }
  2011. }
  2012. if (handle->sha_ctxt.diglen > QCEDEV_MAX_SHA_DIGEST) {
  2013. pr_err("Invalid sha_ctxt.diglen %d\n",
  2014. handle->sha_ctxt.diglen);
  2015. mutex_unlock(&hash_access_lock);
  2016. err = -EINVAL;
  2017. goto exit_free_qcedev_areq;
  2018. }
  2019. memcpy(&qcedev_areq->sha_op_req.digest[0],
  2020. &handle->sha_ctxt.digest[0],
  2021. handle->sha_ctxt.diglen);
  2022. mutex_unlock(&hash_access_lock);
  2023. if (copy_to_user((void __user *)arg, &qcedev_areq->sha_op_req,
  2024. sizeof(struct qcedev_sha_op_req))) {
  2025. err = -EFAULT;
  2026. goto exit_free_qcedev_areq;
  2027. }
  2028. }
  2029. break;
  2030. case QCEDEV_IOCTL_SHA_FINAL_REQ:
  2031. if (!handle->sha_ctxt.init_done) {
  2032. pr_err("%s Init was not called\n", __func__);
  2033. err = -EINVAL;
  2034. goto exit_free_qcedev_areq;
  2035. }
  2036. if (copy_from_user(&qcedev_areq->sha_op_req,
  2037. (void __user *)arg,
  2038. sizeof(struct qcedev_sha_op_req))) {
  2039. err = -EFAULT;
  2040. goto exit_free_qcedev_areq;
  2041. }
  2042. mutex_lock(&hash_access_lock);
  2043. if (qcedev_check_sha_params(&qcedev_areq->sha_op_req, podev)) {
  2044. mutex_unlock(&hash_access_lock);
  2045. err = -EINVAL;
  2046. goto exit_free_qcedev_areq;
  2047. }
  2048. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_SHA;
  2049. err = qcedev_hash_final(qcedev_areq, handle);
  2050. if (err) {
  2051. mutex_unlock(&hash_access_lock);
  2052. goto exit_free_qcedev_areq;
  2053. }
  2054. if (handle->sha_ctxt.diglen > QCEDEV_MAX_SHA_DIGEST) {
  2055. pr_err("Invalid sha_ctxt.diglen %d\n",
  2056. handle->sha_ctxt.diglen);
  2057. mutex_unlock(&hash_access_lock);
  2058. err = -EINVAL;
  2059. goto exit_free_qcedev_areq;
  2060. }
  2061. qcedev_areq->sha_op_req.diglen = handle->sha_ctxt.diglen;
  2062. memcpy(&qcedev_areq->sha_op_req.digest[0],
  2063. &handle->sha_ctxt.digest[0],
  2064. handle->sha_ctxt.diglen);
  2065. mutex_unlock(&hash_access_lock);
  2066. if (copy_to_user((void __user *)arg, &qcedev_areq->sha_op_req,
  2067. sizeof(struct qcedev_sha_op_req))) {
  2068. err = -EFAULT;
  2069. goto exit_free_qcedev_areq;
  2070. }
  2071. handle->sha_ctxt.init_done = false;
  2072. break;
  2073. case QCEDEV_IOCTL_GET_SHA_REQ:
  2074. {
  2075. struct scatterlist sg_src;
  2076. if (copy_from_user(&qcedev_areq->sha_op_req,
  2077. (void __user *)arg,
  2078. sizeof(struct qcedev_sha_op_req))) {
  2079. err = -EFAULT;
  2080. goto exit_free_qcedev_areq;
  2081. }
  2082. mutex_lock(&hash_access_lock);
  2083. if (qcedev_check_sha_params(&qcedev_areq->sha_op_req, podev)) {
  2084. mutex_unlock(&hash_access_lock);
  2085. err = -EINVAL;
  2086. goto exit_free_qcedev_areq;
  2087. }
  2088. qcedev_areq->op_type = QCEDEV_CRYPTO_OPER_SHA;
  2089. qcedev_hash_init(qcedev_areq, handle, &sg_src);
  2090. err = qcedev_hash_update(qcedev_areq, handle, &sg_src);
  2091. if (err) {
  2092. mutex_unlock(&hash_access_lock);
  2093. goto exit_free_qcedev_areq;
  2094. }
  2095. err = qcedev_hash_final(qcedev_areq, handle);
  2096. if (err) {
  2097. mutex_unlock(&hash_access_lock);
  2098. goto exit_free_qcedev_areq;
  2099. }
  2100. if (handle->sha_ctxt.diglen > QCEDEV_MAX_SHA_DIGEST) {
  2101. pr_err("Invalid sha_ctxt.diglen %d\n",
  2102. handle->sha_ctxt.diglen);
  2103. mutex_unlock(&hash_access_lock);
  2104. err = -EINVAL;
  2105. goto exit_free_qcedev_areq;
  2106. }
  2107. qcedev_areq->sha_op_req.diglen = handle->sha_ctxt.diglen;
  2108. memcpy(&qcedev_areq->sha_op_req.digest[0],
  2109. &handle->sha_ctxt.digest[0],
  2110. handle->sha_ctxt.diglen);
  2111. mutex_unlock(&hash_access_lock);
  2112. if (copy_to_user((void __user *)arg, &qcedev_areq->sha_op_req,
  2113. sizeof(struct qcedev_sha_op_req))) {
  2114. err = -EFAULT;
  2115. goto exit_free_qcedev_areq;
  2116. }
  2117. }
  2118. break;
  2119. case QCEDEV_IOCTL_MAP_BUF_REQ:
  2120. {
  2121. unsigned long long vaddr = 0;
  2122. struct qcedev_map_buf_req map_buf = { {0} };
  2123. int i = 0;
  2124. if (copy_from_user(&map_buf,
  2125. (void __user *)arg, sizeof(map_buf))) {
  2126. err = -EFAULT;
  2127. goto exit_free_qcedev_areq;
  2128. }
  2129. if (map_buf.num_fds > ARRAY_SIZE(map_buf.fd)) {
  2130. pr_err("%s: err: num_fds = %d exceeds max value\n",
  2131. __func__, map_buf.num_fds);
  2132. err = -EINVAL;
  2133. goto exit_free_qcedev_areq;
  2134. }
  2135. for (i = 0; i < map_buf.num_fds; i++) {
  2136. err = qcedev_check_and_map_buffer(handle,
  2137. map_buf.fd[i],
  2138. map_buf.fd_offset[i],
  2139. map_buf.fd_size[i],
  2140. &vaddr);
  2141. if (err) {
  2142. pr_err(
  2143. "%s: err: failed to map fd(%d) - %d\n",
  2144. __func__, map_buf.fd[i], err);
  2145. goto exit_free_qcedev_areq;
  2146. }
  2147. map_buf.buf_vaddr[i] = vaddr;
  2148. pr_info("%s: info: vaddr = %llx\n, fd = %d",
  2149. __func__, vaddr, map_buf.fd[i]);
  2150. }
  2151. if (copy_to_user((void __user *)arg, &map_buf,
  2152. sizeof(map_buf))) {
  2153. err = -EFAULT;
  2154. goto exit_free_qcedev_areq;
  2155. }
  2156. break;
  2157. }
  2158. case QCEDEV_IOCTL_UNMAP_BUF_REQ:
  2159. {
  2160. struct qcedev_unmap_buf_req unmap_buf = { { 0 } };
  2161. int i = 0;
  2162. if (copy_from_user(&unmap_buf,
  2163. (void __user *)arg, sizeof(unmap_buf))) {
  2164. err = -EFAULT;
  2165. goto exit_free_qcedev_areq;
  2166. }
  2167. if (unmap_buf.num_fds > ARRAY_SIZE(unmap_buf.fd)) {
  2168. pr_err("%s: err: num_fds = %d exceeds max value\n",
  2169. __func__, unmap_buf.num_fds);
  2170. err = -EINVAL;
  2171. goto exit_free_qcedev_areq;
  2172. }
  2173. for (i = 0; i < unmap_buf.num_fds; i++) {
  2174. err = qcedev_check_and_unmap_buffer(handle,
  2175. unmap_buf.fd[i]);
  2176. if (err) {
  2177. pr_err(
  2178. "%s: err: failed to unmap fd(%d) - %d\n",
  2179. __func__,
  2180. unmap_buf.fd[i], err);
  2181. goto exit_free_qcedev_areq;
  2182. }
  2183. }
  2184. break;
  2185. }
  2186. default:
  2187. err = -ENOTTY;
  2188. goto exit_free_qcedev_areq;
  2189. }
  2190. exit_free_qcedev_areq:
  2191. kfree(qcedev_areq);
  2192. return err;
  2193. }
  2194. static int qcedev_probe_device(struct platform_device *pdev)
  2195. {
  2196. void *handle = NULL;
  2197. int rc = 0;
  2198. struct qcedev_control *podev;
  2199. struct msm_ce_hw_support *platform_support;
  2200. podev = &qce_dev[0];
  2201. rc = alloc_chrdev_region(&qcedev_device_no, 0, 1, QCEDEV_DEV);
  2202. if (rc < 0) {
  2203. pr_err("alloc_chrdev_region failed %d\n", rc);
  2204. return rc;
  2205. }
  2206. driver_class = class_create(THIS_MODULE, QCEDEV_DEV);
  2207. if (IS_ERR(driver_class)) {
  2208. rc = -ENOMEM;
  2209. pr_err("class_create failed %d\n", rc);
  2210. goto exit_unreg_chrdev_region;
  2211. }
  2212. class_dev = device_create(driver_class, NULL, qcedev_device_no, NULL,
  2213. QCEDEV_DEV);
  2214. if (IS_ERR(class_dev)) {
  2215. pr_err("class_device_create failed %d\n", rc);
  2216. rc = -ENOMEM;
  2217. goto exit_destroy_class;
  2218. }
  2219. cdev_init(&podev->cdev, &qcedev_fops);
  2220. podev->cdev.owner = THIS_MODULE;
  2221. rc = cdev_add(&podev->cdev, MKDEV(MAJOR(qcedev_device_no), 0), 1);
  2222. if (rc < 0) {
  2223. pr_err("cdev_add failed %d\n", rc);
  2224. goto exit_destroy_device;
  2225. }
  2226. podev->minor = 0;
  2227. podev->high_bw_req_count = 0;
  2228. INIT_LIST_HEAD(&podev->ready_commands);
  2229. podev->active_command = NULL;
  2230. INIT_LIST_HEAD(&podev->context_banks);
  2231. spin_lock_init(&podev->lock);
  2232. tasklet_init(&podev->done_tasklet, req_done, (unsigned long)podev);
  2233. podev->icc_path = of_icc_get(&pdev->dev, "data_path");
  2234. if (IS_ERR(podev->icc_path)) {
  2235. rc = PTR_ERR(podev->icc_path);
  2236. pr_err("%s Failed to get icc path with error %d\n",
  2237. __func__, rc);
  2238. goto exit_del_cdev;
  2239. }
  2240. /*
  2241. * HLOS crypto vote values from DTSI. If no values specified, use
  2242. * nominal values.
  2243. */
  2244. if (of_property_read_u32((&pdev->dev)->of_node,
  2245. "qcom,icc_avg_bw",
  2246. &podev->icc_avg_bw)) {
  2247. pr_warn("%s: No icc avg BW set, using default\n", __func__);
  2248. podev->icc_avg_bw = CRYPTO_AVG_BW;
  2249. }
  2250. if (of_property_read_u32((&pdev->dev)->of_node,
  2251. "qcom,icc_peak_bw",
  2252. &podev->icc_peak_bw)) {
  2253. pr_warn("%s: No icc peak BW set, using default\n", __func__);
  2254. podev->icc_peak_bw = CRYPTO_PEAK_BW;
  2255. }
  2256. rc = icc_set_bw(podev->icc_path, podev->icc_avg_bw,
  2257. podev->icc_peak_bw);
  2258. if (rc) {
  2259. pr_err("%s Unable to set high bandwidth\n", __func__);
  2260. goto exit_unregister_bus_scale;
  2261. }
  2262. handle = qce_open(pdev, &rc);
  2263. if (handle == NULL) {
  2264. rc = -ENODEV;
  2265. goto exit_scale_busbandwidth;
  2266. }
  2267. podev->qce = handle;
  2268. rc = qce_set_irqs(podev->qce, false);
  2269. if (rc) {
  2270. pr_err("%s: could not disable bam irqs, ret = %d",
  2271. __func__, rc);
  2272. goto exit_scale_busbandwidth;
  2273. }
  2274. rc = icc_set_bw(podev->icc_path, 0, 0);
  2275. if (rc) {
  2276. pr_err("%s Unable to set to low bandwidth\n", __func__);
  2277. goto exit_qce_close;
  2278. }
  2279. podev->pdev = pdev;
  2280. platform_set_drvdata(pdev, podev);
  2281. qce_hw_support(podev->qce, &podev->ce_support);
  2282. if (podev->ce_support.bam) {
  2283. podev->platform_support.ce_shared = 0;
  2284. podev->platform_support.shared_ce_resource = 0;
  2285. podev->platform_support.hw_key_support =
  2286. podev->ce_support.hw_key;
  2287. podev->platform_support.sha_hmac = 1;
  2288. } else {
  2289. platform_support =
  2290. (struct msm_ce_hw_support *)pdev->dev.platform_data;
  2291. podev->platform_support.ce_shared = platform_support->ce_shared;
  2292. podev->platform_support.shared_ce_resource =
  2293. platform_support->shared_ce_resource;
  2294. podev->platform_support.hw_key_support =
  2295. platform_support->hw_key_support;
  2296. podev->platform_support.sha_hmac = platform_support->sha_hmac;
  2297. }
  2298. podev->mem_client = qcedev_mem_new_client(MEM_ION);
  2299. if (!podev->mem_client) {
  2300. pr_err("%s: err: qcedev_mem_new_client failed\n", __func__);
  2301. goto exit_qce_close;
  2302. }
  2303. rc = of_platform_populate(pdev->dev.of_node, qcedev_match,
  2304. NULL, &pdev->dev);
  2305. if (rc) {
  2306. pr_err("%s: err: of_platform_populate failed: %d\n",
  2307. __func__, rc);
  2308. goto exit_mem_new_client;
  2309. }
  2310. return 0;
  2311. exit_mem_new_client:
  2312. if (podev->mem_client)
  2313. qcedev_mem_delete_client(podev->mem_client);
  2314. podev->mem_client = NULL;
  2315. exit_qce_close:
  2316. if (handle)
  2317. qce_close(handle);
  2318. exit_scale_busbandwidth:
  2319. icc_set_bw(podev->icc_path, 0, 0);
  2320. exit_unregister_bus_scale:
  2321. if (podev->icc_path)
  2322. icc_put(podev->icc_path);
  2323. exit_del_cdev:
  2324. cdev_del(&podev->cdev);
  2325. exit_destroy_device:
  2326. device_destroy(driver_class, qcedev_device_no);
  2327. exit_destroy_class:
  2328. class_destroy(driver_class);
  2329. exit_unreg_chrdev_region:
  2330. unregister_chrdev_region(qcedev_device_no, 1);
  2331. podev->icc_path = NULL;
  2332. platform_set_drvdata(pdev, NULL);
  2333. podev->pdev = NULL;
  2334. podev->qce = NULL;
  2335. return rc;
  2336. }
  2337. static int qcedev_probe(struct platform_device *pdev)
  2338. {
  2339. if (of_device_is_compatible(pdev->dev.of_node, "qcom,qcedev"))
  2340. return qcedev_probe_device(pdev);
  2341. else if (of_device_is_compatible(pdev->dev.of_node,
  2342. "qcom,qcedev,context-bank"))
  2343. return qcedev_parse_context_bank(pdev);
  2344. return -EINVAL;
  2345. };
  2346. static int qcedev_remove(struct platform_device *pdev)
  2347. {
  2348. struct qcedev_control *podev;
  2349. podev = platform_get_drvdata(pdev);
  2350. if (!podev)
  2351. return 0;
  2352. qcedev_ce_high_bw_req(podev, true);
  2353. if (podev->qce)
  2354. qce_close(podev->qce);
  2355. qcedev_ce_high_bw_req(podev, false);
  2356. if (podev->icc_path)
  2357. icc_put(podev->icc_path);
  2358. tasklet_kill(&podev->done_tasklet);
  2359. cdev_del(&podev->cdev);
  2360. device_destroy(driver_class, qcedev_device_no);
  2361. class_destroy(driver_class);
  2362. unregister_chrdev_region(qcedev_device_no, 1);
  2363. return 0;
  2364. };
  2365. static int qcedev_suspend(struct platform_device *pdev, pm_message_t state)
  2366. {
  2367. struct qcedev_control *podev;
  2368. int ret;
  2369. podev = platform_get_drvdata(pdev);
  2370. if (!podev)
  2371. return 0;
  2372. mutex_lock(&qcedev_sent_bw_req);
  2373. if (podev->high_bw_req_count) {
  2374. ret = qce_set_irqs(podev->qce, false);
  2375. if (ret) {
  2376. pr_err("%s: could not disable bam irqs, ret = %d",
  2377. __func__, ret);
  2378. goto suspend_exit;
  2379. }
  2380. ret = qcedev_control_clocks(podev, false);
  2381. if (ret)
  2382. goto suspend_exit;
  2383. }
  2384. suspend_exit:
  2385. mutex_unlock(&qcedev_sent_bw_req);
  2386. return 0;
  2387. }
  2388. static int qcedev_resume(struct platform_device *pdev)
  2389. {
  2390. struct qcedev_control *podev;
  2391. int ret;
  2392. podev = platform_get_drvdata(pdev);
  2393. if (!podev)
  2394. return 0;
  2395. mutex_lock(&qcedev_sent_bw_req);
  2396. if (podev->high_bw_req_count) {
  2397. ret = qcedev_control_clocks(podev, true);
  2398. if (ret)
  2399. goto resume_exit;
  2400. ret = qce_set_irqs(podev->qce, true);
  2401. if (ret) {
  2402. pr_err("%s: could not enable bam irqs, ret = %d",
  2403. __func__, ret);
  2404. qcedev_control_clocks(podev, false);
  2405. }
  2406. }
  2407. resume_exit:
  2408. mutex_unlock(&qcedev_sent_bw_req);
  2409. return 0;
  2410. }
  2411. static struct platform_driver qcedev_plat_driver = {
  2412. .probe = qcedev_probe,
  2413. .remove = qcedev_remove,
  2414. .suspend = qcedev_suspend,
  2415. .resume = qcedev_resume,
  2416. .driver = {
  2417. .name = "qce",
  2418. .of_match_table = qcedev_match,
  2419. },
  2420. };
  2421. static int _disp_stats(int id)
  2422. {
  2423. struct qcedev_stat *pstat;
  2424. int len = 0;
  2425. pstat = &_qcedev_stat;
  2426. len = scnprintf(_debug_read_buf, DEBUG_MAX_RW_BUF - 1,
  2427. "\nQTI QCE dev driver %d Statistics:\n",
  2428. id + 1);
  2429. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  2430. " Encryption operation success : %d\n",
  2431. pstat->qcedev_enc_success);
  2432. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  2433. " Encryption operation fail : %d\n",
  2434. pstat->qcedev_enc_fail);
  2435. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  2436. " Decryption operation success : %d\n",
  2437. pstat->qcedev_dec_success);
  2438. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  2439. " Encryption operation fail : %d\n",
  2440. pstat->qcedev_dec_fail);
  2441. return len;
  2442. }
  2443. static ssize_t _debug_stats_read(struct file *file, char __user *buf,
  2444. size_t count, loff_t *ppos)
  2445. {
  2446. ssize_t rc = -EINVAL;
  2447. int qcedev = *((int *) file->private_data);
  2448. int len;
  2449. len = _disp_stats(qcedev);
  2450. if (len <= count)
  2451. rc = simple_read_from_buffer((void __user *) buf, len,
  2452. ppos, (void *) _debug_read_buf, len);
  2453. return rc;
  2454. }
  2455. static ssize_t _debug_stats_write(struct file *file, const char __user *buf,
  2456. size_t count, loff_t *ppos)
  2457. {
  2458. memset((char *)&_qcedev_stat, 0, sizeof(struct qcedev_stat));
  2459. return count;
  2460. };
  2461. static const struct file_operations _debug_stats_ops = {
  2462. .open = simple_open,
  2463. .read = _debug_stats_read,
  2464. .write = _debug_stats_write,
  2465. };
  2466. static int _qcedev_debug_init(void)
  2467. {
  2468. int rc;
  2469. char name[DEBUG_MAX_FNAME];
  2470. struct dentry *dent;
  2471. _debug_dent = debugfs_create_dir("qcedev", NULL);
  2472. if (IS_ERR(_debug_dent)) {
  2473. pr_debug("qcedev debugfs_create_dir fail, error %ld\n",
  2474. PTR_ERR(_debug_dent));
  2475. return PTR_ERR(_debug_dent);
  2476. }
  2477. snprintf(name, DEBUG_MAX_FNAME-1, "stats-%d", 1);
  2478. _debug_qcedev = 0;
  2479. dent = debugfs_create_file(name, 0644, _debug_dent,
  2480. &_debug_qcedev, &_debug_stats_ops);
  2481. if (dent == NULL) {
  2482. pr_debug("qcedev debugfs_create_file fail, error %ld\n",
  2483. PTR_ERR(dent));
  2484. rc = PTR_ERR(dent);
  2485. goto err;
  2486. }
  2487. return 0;
  2488. err:
  2489. debugfs_remove_recursive(_debug_dent);
  2490. return rc;
  2491. }
  2492. static int qcedev_init(void)
  2493. {
  2494. _qcedev_debug_init();
  2495. return platform_driver_register(&qcedev_plat_driver);
  2496. }
  2497. static void qcedev_exit(void)
  2498. {
  2499. debugfs_remove_recursive(_debug_dent);
  2500. platform_driver_unregister(&qcedev_plat_driver);
  2501. }
  2502. MODULE_LICENSE("GPL v2");
  2503. MODULE_DESCRIPTION("QTI DEV Crypto driver");
  2504. MODULE_IMPORT_NS(DMA_BUF);
  2505. module_init(qcedev_init);
  2506. module_exit(qcedev_exit);