qcedev.c 73 KB

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