i40iw_verbs.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenFabrics.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. *******************************************************************************/
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/random.h>
  37. #include <linux/highmem.h>
  38. #include <linux/time.h>
  39. #include <linux/hugetlb.h>
  40. #include <linux/irq.h>
  41. #include <asm/byteorder.h>
  42. #include <net/ip.h>
  43. #include <rdma/ib_verbs.h>
  44. #include <rdma/iw_cm.h>
  45. #include <rdma/ib_user_verbs.h>
  46. #include <rdma/ib_umem.h>
  47. #include "i40iw.h"
  48. /**
  49. * i40iw_query_device - get device attributes
  50. * @ibdev: device pointer from stack
  51. * @props: returning device attributes
  52. * @udata: user data
  53. */
  54. static int i40iw_query_device(struct ib_device *ibdev,
  55. struct ib_device_attr *props,
  56. struct ib_udata *udata)
  57. {
  58. struct i40iw_device *iwdev = to_iwdev(ibdev);
  59. if (udata->inlen || udata->outlen)
  60. return -EINVAL;
  61. memset(props, 0, sizeof(*props));
  62. ether_addr_copy((u8 *)&props->sys_image_guid, iwdev->netdev->dev_addr);
  63. props->fw_ver = I40IW_FW_VERSION;
  64. props->device_cap_flags = iwdev->device_cap_flags;
  65. props->vendor_id = iwdev->ldev->pcidev->vendor;
  66. props->vendor_part_id = iwdev->ldev->pcidev->device;
  67. props->hw_ver = (u32)iwdev->sc_dev.hw_rev;
  68. props->max_mr_size = I40IW_MAX_OUTBOUND_MESSAGE_SIZE;
  69. props->max_qp = iwdev->max_qp - iwdev->used_qps;
  70. props->max_qp_wr = I40IW_MAX_QP_WRS;
  71. props->max_send_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  72. props->max_recv_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  73. props->max_cq = iwdev->max_cq - iwdev->used_cqs;
  74. props->max_cqe = iwdev->max_cqe;
  75. props->max_mr = iwdev->max_mr - iwdev->used_mrs;
  76. props->max_pd = iwdev->max_pd - iwdev->used_pds;
  77. props->max_sge_rd = I40IW_MAX_SGE_RD;
  78. props->max_qp_rd_atom = I40IW_MAX_IRD_SIZE;
  79. props->max_qp_init_rd_atom = props->max_qp_rd_atom;
  80. props->atomic_cap = IB_ATOMIC_NONE;
  81. props->max_map_per_fmr = 1;
  82. props->max_fast_reg_page_list_len = I40IW_MAX_PAGES_PER_FMR;
  83. return 0;
  84. }
  85. /**
  86. * i40iw_query_port - get port attrubutes
  87. * @ibdev: device pointer from stack
  88. * @port: port number for query
  89. * @props: returning device attributes
  90. */
  91. static int i40iw_query_port(struct ib_device *ibdev,
  92. u8 port,
  93. struct ib_port_attr *props)
  94. {
  95. struct i40iw_device *iwdev = to_iwdev(ibdev);
  96. struct net_device *netdev = iwdev->netdev;
  97. /* props being zeroed by the caller, avoid zeroing it here */
  98. props->max_mtu = IB_MTU_4096;
  99. props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
  100. props->lid = 1;
  101. if (netif_carrier_ok(iwdev->netdev))
  102. props->state = IB_PORT_ACTIVE;
  103. else
  104. props->state = IB_PORT_DOWN;
  105. props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
  106. IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
  107. props->gid_tbl_len = 1;
  108. props->pkey_tbl_len = 1;
  109. props->active_width = IB_WIDTH_4X;
  110. props->active_speed = 1;
  111. props->max_msg_sz = I40IW_MAX_OUTBOUND_MESSAGE_SIZE;
  112. return 0;
  113. }
  114. /**
  115. * i40iw_alloc_ucontext - Allocate the user context data structure
  116. * @ibdev: device pointer from stack
  117. * @udata: user data
  118. *
  119. * This keeps track of all objects associated with a particular
  120. * user-mode client.
  121. */
  122. static struct ib_ucontext *i40iw_alloc_ucontext(struct ib_device *ibdev,
  123. struct ib_udata *udata)
  124. {
  125. struct i40iw_device *iwdev = to_iwdev(ibdev);
  126. struct i40iw_alloc_ucontext_req req;
  127. struct i40iw_alloc_ucontext_resp uresp;
  128. struct i40iw_ucontext *ucontext;
  129. if (ib_copy_from_udata(&req, udata, sizeof(req)))
  130. return ERR_PTR(-EINVAL);
  131. if (req.userspace_ver < 4 || req.userspace_ver > I40IW_ABI_VER) {
  132. i40iw_pr_err("Unsupported provider library version %u.\n", req.userspace_ver);
  133. return ERR_PTR(-EINVAL);
  134. }
  135. memset(&uresp, 0, sizeof(uresp));
  136. uresp.max_qps = iwdev->max_qp;
  137. uresp.max_pds = iwdev->max_pd;
  138. uresp.wq_size = iwdev->max_qp_wr * 2;
  139. uresp.kernel_ver = req.userspace_ver;
  140. ucontext = kzalloc(sizeof(*ucontext), GFP_KERNEL);
  141. if (!ucontext)
  142. return ERR_PTR(-ENOMEM);
  143. ucontext->iwdev = iwdev;
  144. ucontext->abi_ver = req.userspace_ver;
  145. if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
  146. kfree(ucontext);
  147. return ERR_PTR(-EFAULT);
  148. }
  149. INIT_LIST_HEAD(&ucontext->cq_reg_mem_list);
  150. spin_lock_init(&ucontext->cq_reg_mem_list_lock);
  151. INIT_LIST_HEAD(&ucontext->qp_reg_mem_list);
  152. spin_lock_init(&ucontext->qp_reg_mem_list_lock);
  153. return &ucontext->ibucontext;
  154. }
  155. /**
  156. * i40iw_dealloc_ucontext - deallocate the user context data structure
  157. * @context: user context created during alloc
  158. */
  159. static int i40iw_dealloc_ucontext(struct ib_ucontext *context)
  160. {
  161. struct i40iw_ucontext *ucontext = to_ucontext(context);
  162. unsigned long flags;
  163. spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
  164. if (!list_empty(&ucontext->cq_reg_mem_list)) {
  165. spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
  166. return -EBUSY;
  167. }
  168. spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
  169. spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
  170. if (!list_empty(&ucontext->qp_reg_mem_list)) {
  171. spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
  172. return -EBUSY;
  173. }
  174. spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
  175. kfree(ucontext);
  176. return 0;
  177. }
  178. /**
  179. * i40iw_mmap - user memory map
  180. * @context: context created during alloc
  181. * @vma: kernel info for user memory map
  182. */
  183. static int i40iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  184. {
  185. struct i40iw_ucontext *ucontext;
  186. u64 db_addr_offset;
  187. u64 push_offset;
  188. ucontext = to_ucontext(context);
  189. if (ucontext->iwdev->sc_dev.is_pf) {
  190. db_addr_offset = I40IW_DB_ADDR_OFFSET;
  191. push_offset = I40IW_PUSH_OFFSET;
  192. if (vma->vm_pgoff)
  193. vma->vm_pgoff += I40IW_PF_FIRST_PUSH_PAGE_INDEX - 1;
  194. } else {
  195. db_addr_offset = I40IW_VF_DB_ADDR_OFFSET;
  196. push_offset = I40IW_VF_PUSH_OFFSET;
  197. if (vma->vm_pgoff)
  198. vma->vm_pgoff += I40IW_VF_FIRST_PUSH_PAGE_INDEX - 1;
  199. }
  200. vma->vm_pgoff += db_addr_offset >> PAGE_SHIFT;
  201. if (vma->vm_pgoff == (db_addr_offset >> PAGE_SHIFT)) {
  202. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  203. vma->vm_private_data = ucontext;
  204. } else {
  205. if ((vma->vm_pgoff - (push_offset >> PAGE_SHIFT)) % 2)
  206. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  207. else
  208. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  209. }
  210. if (io_remap_pfn_range(vma, vma->vm_start,
  211. vma->vm_pgoff + (pci_resource_start(ucontext->iwdev->ldev->pcidev, 0) >> PAGE_SHIFT),
  212. PAGE_SIZE, vma->vm_page_prot))
  213. return -EAGAIN;
  214. return 0;
  215. }
  216. /**
  217. * i40iw_alloc_push_page - allocate a push page for qp
  218. * @iwdev: iwarp device
  219. * @qp: hardware control qp
  220. */
  221. static void i40iw_alloc_push_page(struct i40iw_device *iwdev, struct i40iw_sc_qp *qp)
  222. {
  223. struct i40iw_cqp_request *cqp_request;
  224. struct cqp_commands_info *cqp_info;
  225. enum i40iw_status_code status;
  226. if (qp->push_idx != I40IW_INVALID_PUSH_PAGE_INDEX)
  227. return;
  228. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  229. if (!cqp_request)
  230. return;
  231. atomic_inc(&cqp_request->refcount);
  232. cqp_info = &cqp_request->info;
  233. cqp_info->cqp_cmd = OP_MANAGE_PUSH_PAGE;
  234. cqp_info->post_sq = 1;
  235. cqp_info->in.u.manage_push_page.info.qs_handle = qp->qs_handle;
  236. cqp_info->in.u.manage_push_page.info.free_page = 0;
  237. cqp_info->in.u.manage_push_page.cqp = &iwdev->cqp.sc_cqp;
  238. cqp_info->in.u.manage_push_page.scratch = (uintptr_t)cqp_request;
  239. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  240. if (!status)
  241. qp->push_idx = cqp_request->compl_info.op_ret_val;
  242. else
  243. i40iw_pr_err("CQP-OP Push page fail");
  244. i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
  245. }
  246. /**
  247. * i40iw_dealloc_push_page - free a push page for qp
  248. * @iwdev: iwarp device
  249. * @qp: hardware control qp
  250. */
  251. static void i40iw_dealloc_push_page(struct i40iw_device *iwdev, struct i40iw_sc_qp *qp)
  252. {
  253. struct i40iw_cqp_request *cqp_request;
  254. struct cqp_commands_info *cqp_info;
  255. enum i40iw_status_code status;
  256. if (qp->push_idx == I40IW_INVALID_PUSH_PAGE_INDEX)
  257. return;
  258. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
  259. if (!cqp_request)
  260. return;
  261. cqp_info = &cqp_request->info;
  262. cqp_info->cqp_cmd = OP_MANAGE_PUSH_PAGE;
  263. cqp_info->post_sq = 1;
  264. cqp_info->in.u.manage_push_page.info.push_idx = qp->push_idx;
  265. cqp_info->in.u.manage_push_page.info.qs_handle = qp->qs_handle;
  266. cqp_info->in.u.manage_push_page.info.free_page = 1;
  267. cqp_info->in.u.manage_push_page.cqp = &iwdev->cqp.sc_cqp;
  268. cqp_info->in.u.manage_push_page.scratch = (uintptr_t)cqp_request;
  269. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  270. if (!status)
  271. qp->push_idx = I40IW_INVALID_PUSH_PAGE_INDEX;
  272. else
  273. i40iw_pr_err("CQP-OP Push page fail");
  274. }
  275. /**
  276. * i40iw_alloc_pd - allocate protection domain
  277. * @ibdev: device pointer from stack
  278. * @context: user context created during alloc
  279. * @udata: user data
  280. */
  281. static struct ib_pd *i40iw_alloc_pd(struct ib_device *ibdev,
  282. struct ib_ucontext *context,
  283. struct ib_udata *udata)
  284. {
  285. struct i40iw_pd *iwpd;
  286. struct i40iw_device *iwdev = to_iwdev(ibdev);
  287. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  288. struct i40iw_alloc_pd_resp uresp;
  289. struct i40iw_sc_pd *sc_pd;
  290. struct i40iw_ucontext *ucontext;
  291. u32 pd_id = 0;
  292. int err;
  293. if (iwdev->closing)
  294. return ERR_PTR(-ENODEV);
  295. err = i40iw_alloc_resource(iwdev, iwdev->allocated_pds,
  296. iwdev->max_pd, &pd_id, &iwdev->next_pd);
  297. if (err) {
  298. i40iw_pr_err("alloc resource failed\n");
  299. return ERR_PTR(err);
  300. }
  301. iwpd = kzalloc(sizeof(*iwpd), GFP_KERNEL);
  302. if (!iwpd) {
  303. err = -ENOMEM;
  304. goto free_res;
  305. }
  306. sc_pd = &iwpd->sc_pd;
  307. if (context) {
  308. ucontext = to_ucontext(context);
  309. dev->iw_pd_ops->pd_init(dev, sc_pd, pd_id, ucontext->abi_ver);
  310. memset(&uresp, 0, sizeof(uresp));
  311. uresp.pd_id = pd_id;
  312. if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
  313. err = -EFAULT;
  314. goto error;
  315. }
  316. } else {
  317. dev->iw_pd_ops->pd_init(dev, sc_pd, pd_id, -1);
  318. }
  319. i40iw_add_pdusecount(iwpd);
  320. return &iwpd->ibpd;
  321. error:
  322. kfree(iwpd);
  323. free_res:
  324. i40iw_free_resource(iwdev, iwdev->allocated_pds, pd_id);
  325. return ERR_PTR(err);
  326. }
  327. /**
  328. * i40iw_dealloc_pd - deallocate pd
  329. * @ibpd: ptr of pd to be deallocated
  330. */
  331. static int i40iw_dealloc_pd(struct ib_pd *ibpd)
  332. {
  333. struct i40iw_pd *iwpd = to_iwpd(ibpd);
  334. struct i40iw_device *iwdev = to_iwdev(ibpd->device);
  335. i40iw_rem_pdusecount(iwpd, iwdev);
  336. return 0;
  337. }
  338. /**
  339. * i40iw_get_pbl - Retrieve pbl from a list given a virtual
  340. * address
  341. * @va: user virtual address
  342. * @pbl_list: pbl list to search in (QP's or CQ's)
  343. */
  344. static struct i40iw_pbl *i40iw_get_pbl(unsigned long va,
  345. struct list_head *pbl_list)
  346. {
  347. struct i40iw_pbl *iwpbl;
  348. list_for_each_entry(iwpbl, pbl_list, list) {
  349. if (iwpbl->user_base == va) {
  350. iwpbl->on_list = false;
  351. list_del(&iwpbl->list);
  352. return iwpbl;
  353. }
  354. }
  355. return NULL;
  356. }
  357. /**
  358. * i40iw_free_qp_resources - free up memory resources for qp
  359. * @iwdev: iwarp device
  360. * @iwqp: qp ptr (user or kernel)
  361. * @qp_num: qp number assigned
  362. */
  363. void i40iw_free_qp_resources(struct i40iw_device *iwdev,
  364. struct i40iw_qp *iwqp,
  365. u32 qp_num)
  366. {
  367. struct i40iw_pbl *iwpbl = &iwqp->iwpbl;
  368. i40iw_ieq_cleanup_qp(iwdev->vsi.ieq, &iwqp->sc_qp);
  369. i40iw_dealloc_push_page(iwdev, &iwqp->sc_qp);
  370. if (qp_num)
  371. i40iw_free_resource(iwdev, iwdev->allocated_qps, qp_num);
  372. if (iwpbl->pbl_allocated)
  373. i40iw_free_pble(iwdev->pble_rsrc, &iwpbl->pble_alloc);
  374. i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwqp->q2_ctx_mem);
  375. i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwqp->kqp.dma_mem);
  376. kfree(iwqp->kqp.wrid_mem);
  377. iwqp->kqp.wrid_mem = NULL;
  378. kfree(iwqp->allocated_buffer);
  379. }
  380. /**
  381. * i40iw_clean_cqes - clean cq entries for qp
  382. * @iwqp: qp ptr (user or kernel)
  383. * @iwcq: cq ptr
  384. */
  385. static void i40iw_clean_cqes(struct i40iw_qp *iwqp, struct i40iw_cq *iwcq)
  386. {
  387. struct i40iw_cq_uk *ukcq = &iwcq->sc_cq.cq_uk;
  388. ukcq->ops.iw_cq_clean(&iwqp->sc_qp.qp_uk, ukcq);
  389. }
  390. /**
  391. * i40iw_destroy_qp - destroy qp
  392. * @ibqp: qp's ib pointer also to get to device's qp address
  393. */
  394. static int i40iw_destroy_qp(struct ib_qp *ibqp)
  395. {
  396. struct i40iw_qp *iwqp = to_iwqp(ibqp);
  397. iwqp->destroyed = 1;
  398. if (iwqp->ibqp_state >= IB_QPS_INIT && iwqp->ibqp_state < IB_QPS_RTS)
  399. i40iw_next_iw_state(iwqp, I40IW_QP_STATE_ERROR, 0, 0, 0);
  400. if (!iwqp->user_mode) {
  401. if (iwqp->iwscq) {
  402. i40iw_clean_cqes(iwqp, iwqp->iwscq);
  403. if (iwqp->iwrcq != iwqp->iwscq)
  404. i40iw_clean_cqes(iwqp, iwqp->iwrcq);
  405. }
  406. }
  407. i40iw_rem_ref(&iwqp->ibqp);
  408. return 0;
  409. }
  410. /**
  411. * i40iw_setup_virt_qp - setup for allocation of virtual qp
  412. * @dev: iwarp device
  413. * @qp: qp ptr
  414. * @init_info: initialize info to return
  415. */
  416. static int i40iw_setup_virt_qp(struct i40iw_device *iwdev,
  417. struct i40iw_qp *iwqp,
  418. struct i40iw_qp_init_info *init_info)
  419. {
  420. struct i40iw_pbl *iwpbl = &iwqp->iwpbl;
  421. struct i40iw_qp_mr *qpmr = &iwpbl->qp_mr;
  422. iwqp->page = qpmr->sq_page;
  423. init_info->shadow_area_pa = cpu_to_le64(qpmr->shadow);
  424. if (iwpbl->pbl_allocated) {
  425. init_info->virtual_map = true;
  426. init_info->sq_pa = qpmr->sq_pbl.idx;
  427. init_info->rq_pa = qpmr->rq_pbl.idx;
  428. } else {
  429. init_info->sq_pa = qpmr->sq_pbl.addr;
  430. init_info->rq_pa = qpmr->rq_pbl.addr;
  431. }
  432. return 0;
  433. }
  434. /**
  435. * i40iw_setup_kmode_qp - setup initialization for kernel mode qp
  436. * @iwdev: iwarp device
  437. * @iwqp: qp ptr (user or kernel)
  438. * @info: initialize info to return
  439. */
  440. static int i40iw_setup_kmode_qp(struct i40iw_device *iwdev,
  441. struct i40iw_qp *iwqp,
  442. struct i40iw_qp_init_info *info)
  443. {
  444. struct i40iw_dma_mem *mem = &iwqp->kqp.dma_mem;
  445. u32 sqdepth, rqdepth;
  446. u8 sqshift;
  447. u32 size;
  448. enum i40iw_status_code status;
  449. struct i40iw_qp_uk_init_info *ukinfo = &info->qp_uk_init_info;
  450. i40iw_get_wqe_shift(ukinfo->max_sq_frag_cnt, ukinfo->max_inline_data, &sqshift);
  451. status = i40iw_get_sqdepth(ukinfo->sq_size, sqshift, &sqdepth);
  452. if (status)
  453. return -ENOMEM;
  454. status = i40iw_get_rqdepth(ukinfo->rq_size, I40IW_MAX_RQ_WQE_SHIFT, &rqdepth);
  455. if (status)
  456. return -ENOMEM;
  457. size = sqdepth * sizeof(struct i40iw_sq_uk_wr_trk_info) + (rqdepth << 3);
  458. iwqp->kqp.wrid_mem = kzalloc(size, GFP_KERNEL);
  459. ukinfo->sq_wrtrk_array = (struct i40iw_sq_uk_wr_trk_info *)iwqp->kqp.wrid_mem;
  460. if (!ukinfo->sq_wrtrk_array)
  461. return -ENOMEM;
  462. ukinfo->rq_wrid_array = (u64 *)&ukinfo->sq_wrtrk_array[sqdepth];
  463. size = (sqdepth + rqdepth) * I40IW_QP_WQE_MIN_SIZE;
  464. size += (I40IW_SHADOW_AREA_SIZE << 3);
  465. status = i40iw_allocate_dma_mem(iwdev->sc_dev.hw, mem, size, 256);
  466. if (status) {
  467. kfree(ukinfo->sq_wrtrk_array);
  468. ukinfo->sq_wrtrk_array = NULL;
  469. return -ENOMEM;
  470. }
  471. ukinfo->sq = mem->va;
  472. info->sq_pa = mem->pa;
  473. ukinfo->rq = &ukinfo->sq[sqdepth];
  474. info->rq_pa = info->sq_pa + (sqdepth * I40IW_QP_WQE_MIN_SIZE);
  475. ukinfo->shadow_area = ukinfo->rq[rqdepth].elem;
  476. info->shadow_area_pa = info->rq_pa + (rqdepth * I40IW_QP_WQE_MIN_SIZE);
  477. ukinfo->sq_size = sqdepth >> sqshift;
  478. ukinfo->rq_size = rqdepth >> I40IW_MAX_RQ_WQE_SHIFT;
  479. ukinfo->qp_id = iwqp->ibqp.qp_num;
  480. return 0;
  481. }
  482. /**
  483. * i40iw_create_qp - create qp
  484. * @ibpd: ptr of pd
  485. * @init_attr: attributes for qp
  486. * @udata: user data for create qp
  487. */
  488. static struct ib_qp *i40iw_create_qp(struct ib_pd *ibpd,
  489. struct ib_qp_init_attr *init_attr,
  490. struct ib_udata *udata)
  491. {
  492. struct i40iw_pd *iwpd = to_iwpd(ibpd);
  493. struct i40iw_device *iwdev = to_iwdev(ibpd->device);
  494. struct i40iw_cqp *iwcqp = &iwdev->cqp;
  495. struct i40iw_qp *iwqp;
  496. struct i40iw_ucontext *ucontext;
  497. struct i40iw_create_qp_req req;
  498. struct i40iw_create_qp_resp uresp;
  499. u32 qp_num = 0;
  500. void *mem;
  501. enum i40iw_status_code ret;
  502. int err_code;
  503. int sq_size;
  504. int rq_size;
  505. struct i40iw_sc_qp *qp;
  506. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  507. struct i40iw_qp_init_info init_info;
  508. struct i40iw_create_qp_info *qp_info;
  509. struct i40iw_cqp_request *cqp_request;
  510. struct cqp_commands_info *cqp_info;
  511. struct i40iw_qp_host_ctx_info *ctx_info;
  512. struct i40iwarp_offload_info *iwarp_info;
  513. unsigned long flags;
  514. if (iwdev->closing)
  515. return ERR_PTR(-ENODEV);
  516. if (init_attr->create_flags)
  517. return ERR_PTR(-EINVAL);
  518. if (init_attr->cap.max_inline_data > I40IW_MAX_INLINE_DATA_SIZE)
  519. init_attr->cap.max_inline_data = I40IW_MAX_INLINE_DATA_SIZE;
  520. if (init_attr->cap.max_send_sge > I40IW_MAX_WQ_FRAGMENT_COUNT)
  521. init_attr->cap.max_send_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  522. if (init_attr->cap.max_recv_sge > I40IW_MAX_WQ_FRAGMENT_COUNT)
  523. init_attr->cap.max_recv_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  524. memset(&init_info, 0, sizeof(init_info));
  525. sq_size = init_attr->cap.max_send_wr;
  526. rq_size = init_attr->cap.max_recv_wr;
  527. init_info.vsi = &iwdev->vsi;
  528. init_info.qp_uk_init_info.sq_size = sq_size;
  529. init_info.qp_uk_init_info.rq_size = rq_size;
  530. init_info.qp_uk_init_info.max_sq_frag_cnt = init_attr->cap.max_send_sge;
  531. init_info.qp_uk_init_info.max_rq_frag_cnt = init_attr->cap.max_recv_sge;
  532. init_info.qp_uk_init_info.max_inline_data = init_attr->cap.max_inline_data;
  533. mem = kzalloc(sizeof(*iwqp), GFP_KERNEL);
  534. if (!mem)
  535. return ERR_PTR(-ENOMEM);
  536. iwqp = (struct i40iw_qp *)mem;
  537. iwqp->allocated_buffer = mem;
  538. qp = &iwqp->sc_qp;
  539. qp->back_qp = (void *)iwqp;
  540. qp->push_idx = I40IW_INVALID_PUSH_PAGE_INDEX;
  541. iwqp->ctx_info.iwarp_info = &iwqp->iwarp_info;
  542. if (i40iw_allocate_dma_mem(dev->hw,
  543. &iwqp->q2_ctx_mem,
  544. I40IW_Q2_BUFFER_SIZE + I40IW_QP_CTX_SIZE,
  545. 256)) {
  546. i40iw_pr_err("dma_mem failed\n");
  547. err_code = -ENOMEM;
  548. goto error;
  549. }
  550. init_info.q2 = iwqp->q2_ctx_mem.va;
  551. init_info.q2_pa = iwqp->q2_ctx_mem.pa;
  552. init_info.host_ctx = (void *)init_info.q2 + I40IW_Q2_BUFFER_SIZE;
  553. init_info.host_ctx_pa = init_info.q2_pa + I40IW_Q2_BUFFER_SIZE;
  554. err_code = i40iw_alloc_resource(iwdev, iwdev->allocated_qps, iwdev->max_qp,
  555. &qp_num, &iwdev->next_qp);
  556. if (err_code) {
  557. i40iw_pr_err("qp resource\n");
  558. goto error;
  559. }
  560. iwqp->iwdev = iwdev;
  561. iwqp->iwpd = iwpd;
  562. iwqp->ibqp.qp_num = qp_num;
  563. qp = &iwqp->sc_qp;
  564. iwqp->iwscq = to_iwcq(init_attr->send_cq);
  565. iwqp->iwrcq = to_iwcq(init_attr->recv_cq);
  566. iwqp->host_ctx.va = init_info.host_ctx;
  567. iwqp->host_ctx.pa = init_info.host_ctx_pa;
  568. iwqp->host_ctx.size = I40IW_QP_CTX_SIZE;
  569. init_info.pd = &iwpd->sc_pd;
  570. init_info.qp_uk_init_info.qp_id = iwqp->ibqp.qp_num;
  571. iwqp->ctx_info.qp_compl_ctx = (uintptr_t)qp;
  572. if (init_attr->qp_type != IB_QPT_RC) {
  573. err_code = -EINVAL;
  574. goto error;
  575. }
  576. if (iwdev->push_mode)
  577. i40iw_alloc_push_page(iwdev, qp);
  578. if (udata) {
  579. err_code = ib_copy_from_udata(&req, udata, sizeof(req));
  580. if (err_code) {
  581. i40iw_pr_err("ib_copy_from_data\n");
  582. goto error;
  583. }
  584. iwqp->ctx_info.qp_compl_ctx = req.user_compl_ctx;
  585. if (ibpd->uobject && ibpd->uobject->context) {
  586. iwqp->user_mode = 1;
  587. ucontext = to_ucontext(ibpd->uobject->context);
  588. if (req.user_wqe_buffers) {
  589. struct i40iw_pbl *iwpbl;
  590. spin_lock_irqsave(
  591. &ucontext->qp_reg_mem_list_lock, flags);
  592. iwpbl = i40iw_get_pbl(
  593. (unsigned long)req.user_wqe_buffers,
  594. &ucontext->qp_reg_mem_list);
  595. spin_unlock_irqrestore(
  596. &ucontext->qp_reg_mem_list_lock, flags);
  597. if (!iwpbl) {
  598. err_code = -ENODATA;
  599. i40iw_pr_err("no pbl info\n");
  600. goto error;
  601. }
  602. memcpy(&iwqp->iwpbl, iwpbl, sizeof(iwqp->iwpbl));
  603. }
  604. }
  605. err_code = i40iw_setup_virt_qp(iwdev, iwqp, &init_info);
  606. } else {
  607. err_code = i40iw_setup_kmode_qp(iwdev, iwqp, &init_info);
  608. }
  609. if (err_code) {
  610. i40iw_pr_err("setup qp failed\n");
  611. goto error;
  612. }
  613. init_info.type = I40IW_QP_TYPE_IWARP;
  614. ret = dev->iw_priv_qp_ops->qp_init(qp, &init_info);
  615. if (ret) {
  616. err_code = -EPROTO;
  617. i40iw_pr_err("qp_init fail\n");
  618. goto error;
  619. }
  620. ctx_info = &iwqp->ctx_info;
  621. iwarp_info = &iwqp->iwarp_info;
  622. iwarp_info->rd_enable = true;
  623. iwarp_info->wr_rdresp_en = true;
  624. if (!iwqp->user_mode) {
  625. iwarp_info->fast_reg_en = true;
  626. iwarp_info->priv_mode_en = true;
  627. }
  628. iwarp_info->ddp_ver = 1;
  629. iwarp_info->rdmap_ver = 1;
  630. ctx_info->iwarp_info_valid = true;
  631. ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
  632. ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
  633. if (qp->push_idx == I40IW_INVALID_PUSH_PAGE_INDEX) {
  634. ctx_info->push_mode_en = false;
  635. } else {
  636. ctx_info->push_mode_en = true;
  637. ctx_info->push_idx = qp->push_idx;
  638. }
  639. ret = dev->iw_priv_qp_ops->qp_setctx(&iwqp->sc_qp,
  640. (u64 *)iwqp->host_ctx.va,
  641. ctx_info);
  642. ctx_info->iwarp_info_valid = false;
  643. cqp_request = i40iw_get_cqp_request(iwcqp, true);
  644. if (!cqp_request) {
  645. err_code = -ENOMEM;
  646. goto error;
  647. }
  648. cqp_info = &cqp_request->info;
  649. qp_info = &cqp_request->info.in.u.qp_create.info;
  650. memset(qp_info, 0, sizeof(*qp_info));
  651. qp_info->cq_num_valid = true;
  652. qp_info->next_iwarp_state = I40IW_QP_STATE_IDLE;
  653. cqp_info->cqp_cmd = OP_QP_CREATE;
  654. cqp_info->post_sq = 1;
  655. cqp_info->in.u.qp_create.qp = qp;
  656. cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
  657. ret = i40iw_handle_cqp_op(iwdev, cqp_request);
  658. if (ret) {
  659. i40iw_pr_err("CQP-OP QP create fail");
  660. err_code = -EACCES;
  661. goto error;
  662. }
  663. i40iw_add_ref(&iwqp->ibqp);
  664. spin_lock_init(&iwqp->lock);
  665. iwqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) ? 1 : 0;
  666. iwdev->qp_table[qp_num] = iwqp;
  667. i40iw_add_pdusecount(iwqp->iwpd);
  668. i40iw_add_devusecount(iwdev);
  669. if (ibpd->uobject && udata) {
  670. memset(&uresp, 0, sizeof(uresp));
  671. uresp.actual_sq_size = sq_size;
  672. uresp.actual_rq_size = rq_size;
  673. uresp.qp_id = qp_num;
  674. uresp.push_idx = qp->push_idx;
  675. err_code = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  676. if (err_code) {
  677. i40iw_pr_err("copy_to_udata failed\n");
  678. i40iw_destroy_qp(&iwqp->ibqp);
  679. /* let the completion of the qp destroy free the qp */
  680. return ERR_PTR(err_code);
  681. }
  682. }
  683. init_completion(&iwqp->sq_drained);
  684. init_completion(&iwqp->rq_drained);
  685. return &iwqp->ibqp;
  686. error:
  687. i40iw_free_qp_resources(iwdev, iwqp, qp_num);
  688. return ERR_PTR(err_code);
  689. }
  690. /**
  691. * i40iw_query - query qp attributes
  692. * @ibqp: qp pointer
  693. * @attr: attributes pointer
  694. * @attr_mask: Not used
  695. * @init_attr: qp attributes to return
  696. */
  697. static int i40iw_query_qp(struct ib_qp *ibqp,
  698. struct ib_qp_attr *attr,
  699. int attr_mask,
  700. struct ib_qp_init_attr *init_attr)
  701. {
  702. struct i40iw_qp *iwqp = to_iwqp(ibqp);
  703. struct i40iw_sc_qp *qp = &iwqp->sc_qp;
  704. attr->qp_access_flags = 0;
  705. attr->cap.max_send_wr = qp->qp_uk.sq_size;
  706. attr->cap.max_recv_wr = qp->qp_uk.rq_size;
  707. attr->cap.max_inline_data = I40IW_MAX_INLINE_DATA_SIZE;
  708. attr->cap.max_send_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  709. attr->cap.max_recv_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
  710. attr->port_num = 1;
  711. init_attr->event_handler = iwqp->ibqp.event_handler;
  712. init_attr->qp_context = iwqp->ibqp.qp_context;
  713. init_attr->send_cq = iwqp->ibqp.send_cq;
  714. init_attr->recv_cq = iwqp->ibqp.recv_cq;
  715. init_attr->srq = iwqp->ibqp.srq;
  716. init_attr->cap = attr->cap;
  717. init_attr->port_num = 1;
  718. return 0;
  719. }
  720. /**
  721. * i40iw_hw_modify_qp - setup cqp for modify qp
  722. * @iwdev: iwarp device
  723. * @iwqp: qp ptr (user or kernel)
  724. * @info: info for modify qp
  725. * @wait: flag to wait or not for modify qp completion
  726. */
  727. void i40iw_hw_modify_qp(struct i40iw_device *iwdev, struct i40iw_qp *iwqp,
  728. struct i40iw_modify_qp_info *info, bool wait)
  729. {
  730. struct i40iw_cqp_request *cqp_request;
  731. struct cqp_commands_info *cqp_info;
  732. struct i40iw_modify_qp_info *m_info;
  733. struct i40iw_gen_ae_info ae_info;
  734. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, wait);
  735. if (!cqp_request)
  736. return;
  737. cqp_info = &cqp_request->info;
  738. m_info = &cqp_info->in.u.qp_modify.info;
  739. memcpy(m_info, info, sizeof(*m_info));
  740. cqp_info->cqp_cmd = OP_QP_MODIFY;
  741. cqp_info->post_sq = 1;
  742. cqp_info->in.u.qp_modify.qp = &iwqp->sc_qp;
  743. cqp_info->in.u.qp_modify.scratch = (uintptr_t)cqp_request;
  744. if (!i40iw_handle_cqp_op(iwdev, cqp_request))
  745. return;
  746. switch (m_info->next_iwarp_state) {
  747. case I40IW_QP_STATE_RTS:
  748. if (iwqp->iwarp_state == I40IW_QP_STATE_IDLE)
  749. i40iw_send_reset(iwqp->cm_node);
  750. /* fall through */
  751. case I40IW_QP_STATE_IDLE:
  752. case I40IW_QP_STATE_TERMINATE:
  753. case I40IW_QP_STATE_CLOSING:
  754. ae_info.ae_code = I40IW_AE_BAD_CLOSE;
  755. ae_info.ae_source = 0;
  756. i40iw_gen_ae(iwdev, &iwqp->sc_qp, &ae_info, false);
  757. break;
  758. case I40IW_QP_STATE_ERROR:
  759. default:
  760. break;
  761. }
  762. }
  763. /**
  764. * i40iw_modify_qp - modify qp request
  765. * @ibqp: qp's pointer for modify
  766. * @attr: access attributes
  767. * @attr_mask: state mask
  768. * @udata: user data
  769. */
  770. int i40iw_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  771. int attr_mask, struct ib_udata *udata)
  772. {
  773. struct i40iw_qp *iwqp = to_iwqp(ibqp);
  774. struct i40iw_device *iwdev = iwqp->iwdev;
  775. struct i40iw_qp_host_ctx_info *ctx_info;
  776. struct i40iwarp_offload_info *iwarp_info;
  777. struct i40iw_modify_qp_info info;
  778. u8 issue_modify_qp = 0;
  779. u8 dont_wait = 0;
  780. u32 err;
  781. unsigned long flags;
  782. memset(&info, 0, sizeof(info));
  783. ctx_info = &iwqp->ctx_info;
  784. iwarp_info = &iwqp->iwarp_info;
  785. spin_lock_irqsave(&iwqp->lock, flags);
  786. if (attr_mask & IB_QP_STATE) {
  787. if (iwdev->closing && attr->qp_state != IB_QPS_ERR) {
  788. err = -EINVAL;
  789. goto exit;
  790. }
  791. switch (attr->qp_state) {
  792. case IB_QPS_INIT:
  793. case IB_QPS_RTR:
  794. if (iwqp->iwarp_state > (u32)I40IW_QP_STATE_IDLE) {
  795. err = -EINVAL;
  796. goto exit;
  797. }
  798. if (iwqp->iwarp_state == I40IW_QP_STATE_INVALID) {
  799. info.next_iwarp_state = I40IW_QP_STATE_IDLE;
  800. issue_modify_qp = 1;
  801. }
  802. break;
  803. case IB_QPS_RTS:
  804. if ((iwqp->iwarp_state > (u32)I40IW_QP_STATE_RTS) ||
  805. (!iwqp->cm_id)) {
  806. err = -EINVAL;
  807. goto exit;
  808. }
  809. issue_modify_qp = 1;
  810. iwqp->hw_tcp_state = I40IW_TCP_STATE_ESTABLISHED;
  811. iwqp->hte_added = 1;
  812. info.next_iwarp_state = I40IW_QP_STATE_RTS;
  813. info.tcp_ctx_valid = true;
  814. info.ord_valid = true;
  815. info.arp_cache_idx_valid = true;
  816. info.cq_num_valid = true;
  817. break;
  818. case IB_QPS_SQD:
  819. if (iwqp->hw_iwarp_state > (u32)I40IW_QP_STATE_RTS) {
  820. err = 0;
  821. goto exit;
  822. }
  823. if ((iwqp->iwarp_state == (u32)I40IW_QP_STATE_CLOSING) ||
  824. (iwqp->iwarp_state < (u32)I40IW_QP_STATE_RTS)) {
  825. err = 0;
  826. goto exit;
  827. }
  828. if (iwqp->iwarp_state > (u32)I40IW_QP_STATE_CLOSING) {
  829. err = -EINVAL;
  830. goto exit;
  831. }
  832. info.next_iwarp_state = I40IW_QP_STATE_CLOSING;
  833. issue_modify_qp = 1;
  834. break;
  835. case IB_QPS_SQE:
  836. if (iwqp->iwarp_state >= (u32)I40IW_QP_STATE_TERMINATE) {
  837. err = -EINVAL;
  838. goto exit;
  839. }
  840. info.next_iwarp_state = I40IW_QP_STATE_TERMINATE;
  841. issue_modify_qp = 1;
  842. break;
  843. case IB_QPS_ERR:
  844. case IB_QPS_RESET:
  845. if (iwqp->iwarp_state == (u32)I40IW_QP_STATE_ERROR) {
  846. err = -EINVAL;
  847. goto exit;
  848. }
  849. if (iwqp->sc_qp.term_flags)
  850. i40iw_terminate_del_timer(&iwqp->sc_qp);
  851. info.next_iwarp_state = I40IW_QP_STATE_ERROR;
  852. if ((iwqp->hw_tcp_state > I40IW_TCP_STATE_CLOSED) &&
  853. iwdev->iw_status &&
  854. (iwqp->hw_tcp_state != I40IW_TCP_STATE_TIME_WAIT))
  855. info.reset_tcp_conn = true;
  856. else
  857. dont_wait = 1;
  858. issue_modify_qp = 1;
  859. info.next_iwarp_state = I40IW_QP_STATE_ERROR;
  860. break;
  861. default:
  862. err = -EINVAL;
  863. goto exit;
  864. }
  865. iwqp->ibqp_state = attr->qp_state;
  866. }
  867. if (attr_mask & IB_QP_ACCESS_FLAGS) {
  868. ctx_info->iwarp_info_valid = true;
  869. if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE)
  870. iwarp_info->wr_rdresp_en = true;
  871. if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
  872. iwarp_info->wr_rdresp_en = true;
  873. if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
  874. iwarp_info->rd_enable = true;
  875. if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
  876. iwarp_info->bind_en = true;
  877. if (iwqp->user_mode) {
  878. iwarp_info->rd_enable = true;
  879. iwarp_info->wr_rdresp_en = true;
  880. iwarp_info->priv_mode_en = false;
  881. }
  882. }
  883. if (ctx_info->iwarp_info_valid) {
  884. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  885. int ret;
  886. ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
  887. ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
  888. ret = dev->iw_priv_qp_ops->qp_setctx(&iwqp->sc_qp,
  889. (u64 *)iwqp->host_ctx.va,
  890. ctx_info);
  891. if (ret) {
  892. i40iw_pr_err("setting QP context\n");
  893. err = -EINVAL;
  894. goto exit;
  895. }
  896. }
  897. spin_unlock_irqrestore(&iwqp->lock, flags);
  898. if (issue_modify_qp) {
  899. i40iw_hw_modify_qp(iwdev, iwqp, &info, true);
  900. spin_lock_irqsave(&iwqp->lock, flags);
  901. iwqp->iwarp_state = info.next_iwarp_state;
  902. spin_unlock_irqrestore(&iwqp->lock, flags);
  903. }
  904. if (issue_modify_qp && (iwqp->ibqp_state > IB_QPS_RTS)) {
  905. if (dont_wait) {
  906. if (iwqp->cm_id && iwqp->hw_tcp_state) {
  907. spin_lock_irqsave(&iwqp->lock, flags);
  908. iwqp->hw_tcp_state = I40IW_TCP_STATE_CLOSED;
  909. iwqp->last_aeq = I40IW_AE_RESET_SENT;
  910. spin_unlock_irqrestore(&iwqp->lock, flags);
  911. i40iw_cm_disconn(iwqp);
  912. }
  913. } else {
  914. spin_lock_irqsave(&iwqp->lock, flags);
  915. if (iwqp->cm_id) {
  916. if (atomic_inc_return(&iwqp->close_timer_started) == 1) {
  917. iwqp->cm_id->add_ref(iwqp->cm_id);
  918. i40iw_schedule_cm_timer(iwqp->cm_node,
  919. (struct i40iw_puda_buf *)iwqp,
  920. I40IW_TIMER_TYPE_CLOSE, 1, 0);
  921. }
  922. }
  923. spin_unlock_irqrestore(&iwqp->lock, flags);
  924. }
  925. }
  926. return 0;
  927. exit:
  928. spin_unlock_irqrestore(&iwqp->lock, flags);
  929. return err;
  930. }
  931. /**
  932. * cq_free_resources - free up recources for cq
  933. * @iwdev: iwarp device
  934. * @iwcq: cq ptr
  935. */
  936. static void cq_free_resources(struct i40iw_device *iwdev, struct i40iw_cq *iwcq)
  937. {
  938. struct i40iw_sc_cq *cq = &iwcq->sc_cq;
  939. if (!iwcq->user_mode)
  940. i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwcq->kmem);
  941. i40iw_free_resource(iwdev, iwdev->allocated_cqs, cq->cq_uk.cq_id);
  942. }
  943. /**
  944. * i40iw_cq_wq_destroy - send cq destroy cqp
  945. * @iwdev: iwarp device
  946. * @cq: hardware control cq
  947. */
  948. void i40iw_cq_wq_destroy(struct i40iw_device *iwdev, struct i40iw_sc_cq *cq)
  949. {
  950. enum i40iw_status_code status;
  951. struct i40iw_cqp_request *cqp_request;
  952. struct cqp_commands_info *cqp_info;
  953. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  954. if (!cqp_request)
  955. return;
  956. cqp_info = &cqp_request->info;
  957. cqp_info->cqp_cmd = OP_CQ_DESTROY;
  958. cqp_info->post_sq = 1;
  959. cqp_info->in.u.cq_destroy.cq = cq;
  960. cqp_info->in.u.cq_destroy.scratch = (uintptr_t)cqp_request;
  961. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  962. if (status)
  963. i40iw_pr_err("CQP-OP Destroy QP fail");
  964. }
  965. /**
  966. * i40iw_destroy_cq - destroy cq
  967. * @ib_cq: cq pointer
  968. */
  969. static int i40iw_destroy_cq(struct ib_cq *ib_cq)
  970. {
  971. struct i40iw_cq *iwcq;
  972. struct i40iw_device *iwdev;
  973. struct i40iw_sc_cq *cq;
  974. if (!ib_cq) {
  975. i40iw_pr_err("ib_cq == NULL\n");
  976. return 0;
  977. }
  978. iwcq = to_iwcq(ib_cq);
  979. iwdev = to_iwdev(ib_cq->device);
  980. cq = &iwcq->sc_cq;
  981. i40iw_cq_wq_destroy(iwdev, cq);
  982. cq_free_resources(iwdev, iwcq);
  983. kfree(iwcq);
  984. i40iw_rem_devusecount(iwdev);
  985. return 0;
  986. }
  987. /**
  988. * i40iw_create_cq - create cq
  989. * @ibdev: device pointer from stack
  990. * @attr: attributes for cq
  991. * @context: user context created during alloc
  992. * @udata: user data
  993. */
  994. static struct ib_cq *i40iw_create_cq(struct ib_device *ibdev,
  995. const struct ib_cq_init_attr *attr,
  996. struct ib_ucontext *context,
  997. struct ib_udata *udata)
  998. {
  999. struct i40iw_device *iwdev = to_iwdev(ibdev);
  1000. struct i40iw_cq *iwcq;
  1001. struct i40iw_pbl *iwpbl;
  1002. u32 cq_num = 0;
  1003. struct i40iw_sc_cq *cq;
  1004. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  1005. struct i40iw_cq_init_info info;
  1006. enum i40iw_status_code status;
  1007. struct i40iw_cqp_request *cqp_request;
  1008. struct cqp_commands_info *cqp_info;
  1009. struct i40iw_cq_uk_init_info *ukinfo = &info.cq_uk_init_info;
  1010. unsigned long flags;
  1011. int err_code;
  1012. int entries = attr->cqe;
  1013. if (iwdev->closing)
  1014. return ERR_PTR(-ENODEV);
  1015. if (entries > iwdev->max_cqe)
  1016. return ERR_PTR(-EINVAL);
  1017. iwcq = kzalloc(sizeof(*iwcq), GFP_KERNEL);
  1018. if (!iwcq)
  1019. return ERR_PTR(-ENOMEM);
  1020. memset(&info, 0, sizeof(info));
  1021. err_code = i40iw_alloc_resource(iwdev, iwdev->allocated_cqs,
  1022. iwdev->max_cq, &cq_num,
  1023. &iwdev->next_cq);
  1024. if (err_code)
  1025. goto error;
  1026. cq = &iwcq->sc_cq;
  1027. cq->back_cq = (void *)iwcq;
  1028. spin_lock_init(&iwcq->lock);
  1029. info.dev = dev;
  1030. ukinfo->cq_size = max(entries, 4);
  1031. ukinfo->cq_id = cq_num;
  1032. iwcq->ibcq.cqe = info.cq_uk_init_info.cq_size;
  1033. info.ceqe_mask = 0;
  1034. if (attr->comp_vector < iwdev->ceqs_count)
  1035. info.ceq_id = attr->comp_vector;
  1036. info.ceq_id_valid = true;
  1037. info.ceqe_mask = 1;
  1038. info.type = I40IW_CQ_TYPE_IWARP;
  1039. if (context) {
  1040. struct i40iw_ucontext *ucontext;
  1041. struct i40iw_create_cq_req req;
  1042. struct i40iw_cq_mr *cqmr;
  1043. memset(&req, 0, sizeof(req));
  1044. iwcq->user_mode = true;
  1045. ucontext = to_ucontext(context);
  1046. if (ib_copy_from_udata(&req, udata, sizeof(struct i40iw_create_cq_req))) {
  1047. err_code = -EFAULT;
  1048. goto cq_free_resources;
  1049. }
  1050. spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
  1051. iwpbl = i40iw_get_pbl((unsigned long)req.user_cq_buffer,
  1052. &ucontext->cq_reg_mem_list);
  1053. spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
  1054. if (!iwpbl) {
  1055. err_code = -EPROTO;
  1056. goto cq_free_resources;
  1057. }
  1058. iwcq->iwpbl = iwpbl;
  1059. iwcq->cq_mem_size = 0;
  1060. cqmr = &iwpbl->cq_mr;
  1061. info.shadow_area_pa = cpu_to_le64(cqmr->shadow);
  1062. if (iwpbl->pbl_allocated) {
  1063. info.virtual_map = true;
  1064. info.pbl_chunk_size = 1;
  1065. info.first_pm_pbl_idx = cqmr->cq_pbl.idx;
  1066. } else {
  1067. info.cq_base_pa = cqmr->cq_pbl.addr;
  1068. }
  1069. } else {
  1070. /* Kmode allocations */
  1071. int rsize;
  1072. int shadow;
  1073. rsize = info.cq_uk_init_info.cq_size * sizeof(struct i40iw_cqe);
  1074. rsize = round_up(rsize, 256);
  1075. shadow = I40IW_SHADOW_AREA_SIZE << 3;
  1076. status = i40iw_allocate_dma_mem(dev->hw, &iwcq->kmem,
  1077. rsize + shadow, 256);
  1078. if (status) {
  1079. err_code = -ENOMEM;
  1080. goto cq_free_resources;
  1081. }
  1082. ukinfo->cq_base = iwcq->kmem.va;
  1083. info.cq_base_pa = iwcq->kmem.pa;
  1084. info.shadow_area_pa = info.cq_base_pa + rsize;
  1085. ukinfo->shadow_area = iwcq->kmem.va + rsize;
  1086. }
  1087. if (dev->iw_priv_cq_ops->cq_init(cq, &info)) {
  1088. i40iw_pr_err("init cq fail\n");
  1089. err_code = -EPROTO;
  1090. goto cq_free_resources;
  1091. }
  1092. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  1093. if (!cqp_request) {
  1094. err_code = -ENOMEM;
  1095. goto cq_free_resources;
  1096. }
  1097. cqp_info = &cqp_request->info;
  1098. cqp_info->cqp_cmd = OP_CQ_CREATE;
  1099. cqp_info->post_sq = 1;
  1100. cqp_info->in.u.cq_create.cq = cq;
  1101. cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
  1102. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  1103. if (status) {
  1104. i40iw_pr_err("CQP-OP Create QP fail");
  1105. err_code = -EPROTO;
  1106. goto cq_free_resources;
  1107. }
  1108. if (context) {
  1109. struct i40iw_create_cq_resp resp;
  1110. memset(&resp, 0, sizeof(resp));
  1111. resp.cq_id = info.cq_uk_init_info.cq_id;
  1112. resp.cq_size = info.cq_uk_init_info.cq_size;
  1113. if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
  1114. i40iw_pr_err("copy to user data\n");
  1115. err_code = -EPROTO;
  1116. goto cq_destroy;
  1117. }
  1118. }
  1119. i40iw_add_devusecount(iwdev);
  1120. return (struct ib_cq *)iwcq;
  1121. cq_destroy:
  1122. i40iw_cq_wq_destroy(iwdev, cq);
  1123. cq_free_resources:
  1124. cq_free_resources(iwdev, iwcq);
  1125. error:
  1126. kfree(iwcq);
  1127. return ERR_PTR(err_code);
  1128. }
  1129. /**
  1130. * i40iw_get_user_access - get hw access from IB access
  1131. * @acc: IB access to return hw access
  1132. */
  1133. static inline u16 i40iw_get_user_access(int acc)
  1134. {
  1135. u16 access = 0;
  1136. access |= (acc & IB_ACCESS_LOCAL_WRITE) ? I40IW_ACCESS_FLAGS_LOCALWRITE : 0;
  1137. access |= (acc & IB_ACCESS_REMOTE_WRITE) ? I40IW_ACCESS_FLAGS_REMOTEWRITE : 0;
  1138. access |= (acc & IB_ACCESS_REMOTE_READ) ? I40IW_ACCESS_FLAGS_REMOTEREAD : 0;
  1139. access |= (acc & IB_ACCESS_MW_BIND) ? I40IW_ACCESS_FLAGS_BIND_WINDOW : 0;
  1140. return access;
  1141. }
  1142. /**
  1143. * i40iw_free_stag - free stag resource
  1144. * @iwdev: iwarp device
  1145. * @stag: stag to free
  1146. */
  1147. static void i40iw_free_stag(struct i40iw_device *iwdev, u32 stag)
  1148. {
  1149. u32 stag_idx;
  1150. stag_idx = (stag & iwdev->mr_stagmask) >> I40IW_CQPSQ_STAG_IDX_SHIFT;
  1151. i40iw_free_resource(iwdev, iwdev->allocated_mrs, stag_idx);
  1152. i40iw_rem_devusecount(iwdev);
  1153. }
  1154. /**
  1155. * i40iw_create_stag - create random stag
  1156. * @iwdev: iwarp device
  1157. */
  1158. static u32 i40iw_create_stag(struct i40iw_device *iwdev)
  1159. {
  1160. u32 stag = 0;
  1161. u32 stag_index = 0;
  1162. u32 next_stag_index;
  1163. u32 driver_key;
  1164. u32 random;
  1165. u8 consumer_key;
  1166. int ret;
  1167. get_random_bytes(&random, sizeof(random));
  1168. consumer_key = (u8)random;
  1169. driver_key = random & ~iwdev->mr_stagmask;
  1170. next_stag_index = (random & iwdev->mr_stagmask) >> 8;
  1171. next_stag_index %= iwdev->max_mr;
  1172. ret = i40iw_alloc_resource(iwdev,
  1173. iwdev->allocated_mrs, iwdev->max_mr,
  1174. &stag_index, &next_stag_index);
  1175. if (!ret) {
  1176. stag = stag_index << I40IW_CQPSQ_STAG_IDX_SHIFT;
  1177. stag |= driver_key;
  1178. stag += (u32)consumer_key;
  1179. i40iw_add_devusecount(iwdev);
  1180. }
  1181. return stag;
  1182. }
  1183. /**
  1184. * i40iw_next_pbl_addr - Get next pbl address
  1185. * @pbl: pointer to a pble
  1186. * @pinfo: info pointer
  1187. * @idx: index
  1188. */
  1189. static inline u64 *i40iw_next_pbl_addr(u64 *pbl,
  1190. struct i40iw_pble_info **pinfo,
  1191. u32 *idx)
  1192. {
  1193. *idx += 1;
  1194. if ((!(*pinfo)) || (*idx != (*pinfo)->cnt))
  1195. return ++pbl;
  1196. *idx = 0;
  1197. (*pinfo)++;
  1198. return (u64 *)(*pinfo)->addr;
  1199. }
  1200. /**
  1201. * i40iw_copy_user_pgaddrs - copy user page address to pble's os locally
  1202. * @iwmr: iwmr for IB's user page addresses
  1203. * @pbl: ple pointer to save 1 level or 0 level pble
  1204. * @level: indicated level 0, 1 or 2
  1205. */
  1206. static void i40iw_copy_user_pgaddrs(struct i40iw_mr *iwmr,
  1207. u64 *pbl,
  1208. enum i40iw_pble_level level)
  1209. {
  1210. struct ib_umem *region = iwmr->region;
  1211. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1212. int chunk_pages, entry, i;
  1213. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1214. struct i40iw_pble_info *pinfo;
  1215. struct scatterlist *sg;
  1216. u64 pg_addr = 0;
  1217. u32 idx = 0;
  1218. pinfo = (level == I40IW_LEVEL_1) ? NULL : palloc->level2.leaf;
  1219. for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
  1220. chunk_pages = sg_dma_len(sg) >> region->page_shift;
  1221. if ((iwmr->type == IW_MEMREG_TYPE_QP) &&
  1222. !iwpbl->qp_mr.sq_page)
  1223. iwpbl->qp_mr.sq_page = sg_page(sg);
  1224. for (i = 0; i < chunk_pages; i++) {
  1225. pg_addr = sg_dma_address(sg) +
  1226. (i << region->page_shift);
  1227. if ((entry + i) == 0)
  1228. *pbl = cpu_to_le64(pg_addr & iwmr->page_msk);
  1229. else if (!(pg_addr & ~iwmr->page_msk))
  1230. *pbl = cpu_to_le64(pg_addr);
  1231. else
  1232. continue;
  1233. pbl = i40iw_next_pbl_addr(pbl, &pinfo, &idx);
  1234. }
  1235. }
  1236. }
  1237. /**
  1238. * i40iw_set_hugetlb_params - set MR pg size and mask to huge pg values.
  1239. * @addr: virtual address
  1240. * @iwmr: mr pointer for this memory registration
  1241. */
  1242. static void i40iw_set_hugetlb_values(u64 addr, struct i40iw_mr *iwmr)
  1243. {
  1244. struct vm_area_struct *vma;
  1245. struct hstate *h;
  1246. down_read(&current->mm->mmap_sem);
  1247. vma = find_vma(current->mm, addr);
  1248. if (vma && is_vm_hugetlb_page(vma)) {
  1249. h = hstate_vma(vma);
  1250. if (huge_page_size(h) == 0x200000) {
  1251. iwmr->page_size = huge_page_size(h);
  1252. iwmr->page_msk = huge_page_mask(h);
  1253. }
  1254. }
  1255. up_read(&current->mm->mmap_sem);
  1256. }
  1257. /**
  1258. * i40iw_check_mem_contiguous - check if pbls stored in arr are contiguous
  1259. * @arr: lvl1 pbl array
  1260. * @npages: page count
  1261. * pg_size: page size
  1262. *
  1263. */
  1264. static bool i40iw_check_mem_contiguous(u64 *arr, u32 npages, u32 pg_size)
  1265. {
  1266. u32 pg_idx;
  1267. for (pg_idx = 0; pg_idx < npages; pg_idx++) {
  1268. if ((*arr + (pg_size * pg_idx)) != arr[pg_idx])
  1269. return false;
  1270. }
  1271. return true;
  1272. }
  1273. /**
  1274. * i40iw_check_mr_contiguous - check if MR is physically contiguous
  1275. * @palloc: pbl allocation struct
  1276. * pg_size: page size
  1277. */
  1278. static bool i40iw_check_mr_contiguous(struct i40iw_pble_alloc *palloc, u32 pg_size)
  1279. {
  1280. struct i40iw_pble_level2 *lvl2 = &palloc->level2;
  1281. struct i40iw_pble_info *leaf = lvl2->leaf;
  1282. u64 *arr = NULL;
  1283. u64 *start_addr = NULL;
  1284. int i;
  1285. bool ret;
  1286. if (palloc->level == I40IW_LEVEL_1) {
  1287. arr = (u64 *)palloc->level1.addr;
  1288. ret = i40iw_check_mem_contiguous(arr, palloc->total_cnt, pg_size);
  1289. return ret;
  1290. }
  1291. start_addr = (u64 *)leaf->addr;
  1292. for (i = 0; i < lvl2->leaf_cnt; i++, leaf++) {
  1293. arr = (u64 *)leaf->addr;
  1294. if ((*start_addr + (i * pg_size * PBLE_PER_PAGE)) != *arr)
  1295. return false;
  1296. ret = i40iw_check_mem_contiguous(arr, leaf->cnt, pg_size);
  1297. if (!ret)
  1298. return false;
  1299. }
  1300. return true;
  1301. }
  1302. /**
  1303. * i40iw_setup_pbles - copy user pg address to pble's
  1304. * @iwdev: iwarp device
  1305. * @iwmr: mr pointer for this memory registration
  1306. * @use_pbles: flag if to use pble's
  1307. */
  1308. static int i40iw_setup_pbles(struct i40iw_device *iwdev,
  1309. struct i40iw_mr *iwmr,
  1310. bool use_pbles)
  1311. {
  1312. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1313. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1314. struct i40iw_pble_info *pinfo;
  1315. u64 *pbl;
  1316. enum i40iw_status_code status;
  1317. enum i40iw_pble_level level = I40IW_LEVEL_1;
  1318. if (use_pbles) {
  1319. mutex_lock(&iwdev->pbl_mutex);
  1320. status = i40iw_get_pble(&iwdev->sc_dev, iwdev->pble_rsrc, palloc, iwmr->page_cnt);
  1321. mutex_unlock(&iwdev->pbl_mutex);
  1322. if (status)
  1323. return -ENOMEM;
  1324. iwpbl->pbl_allocated = true;
  1325. level = palloc->level;
  1326. pinfo = (level == I40IW_LEVEL_1) ? &palloc->level1 : palloc->level2.leaf;
  1327. pbl = (u64 *)pinfo->addr;
  1328. } else {
  1329. pbl = iwmr->pgaddrmem;
  1330. }
  1331. i40iw_copy_user_pgaddrs(iwmr, pbl, level);
  1332. if (use_pbles)
  1333. iwmr->pgaddrmem[0] = *pbl;
  1334. return 0;
  1335. }
  1336. /**
  1337. * i40iw_handle_q_mem - handle memory for qp and cq
  1338. * @iwdev: iwarp device
  1339. * @req: information for q memory management
  1340. * @iwpbl: pble struct
  1341. * @use_pbles: flag to use pble
  1342. */
  1343. static int i40iw_handle_q_mem(struct i40iw_device *iwdev,
  1344. struct i40iw_mem_reg_req *req,
  1345. struct i40iw_pbl *iwpbl,
  1346. bool use_pbles)
  1347. {
  1348. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1349. struct i40iw_mr *iwmr = iwpbl->iwmr;
  1350. struct i40iw_qp_mr *qpmr = &iwpbl->qp_mr;
  1351. struct i40iw_cq_mr *cqmr = &iwpbl->cq_mr;
  1352. struct i40iw_hmc_pble *hmc_p;
  1353. u64 *arr = iwmr->pgaddrmem;
  1354. u32 pg_size;
  1355. int err;
  1356. int total;
  1357. bool ret = true;
  1358. total = req->sq_pages + req->rq_pages + req->cq_pages;
  1359. pg_size = iwmr->page_size;
  1360. err = i40iw_setup_pbles(iwdev, iwmr, use_pbles);
  1361. if (err)
  1362. return err;
  1363. if (use_pbles && (palloc->level != I40IW_LEVEL_1)) {
  1364. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1365. iwpbl->pbl_allocated = false;
  1366. return -ENOMEM;
  1367. }
  1368. if (use_pbles)
  1369. arr = (u64 *)palloc->level1.addr;
  1370. if (iwmr->type == IW_MEMREG_TYPE_QP) {
  1371. hmc_p = &qpmr->sq_pbl;
  1372. qpmr->shadow = (dma_addr_t)arr[total];
  1373. if (use_pbles) {
  1374. ret = i40iw_check_mem_contiguous(arr, req->sq_pages, pg_size);
  1375. if (ret)
  1376. ret = i40iw_check_mem_contiguous(&arr[req->sq_pages], req->rq_pages, pg_size);
  1377. }
  1378. if (!ret) {
  1379. hmc_p->idx = palloc->level1.idx;
  1380. hmc_p = &qpmr->rq_pbl;
  1381. hmc_p->idx = palloc->level1.idx + req->sq_pages;
  1382. } else {
  1383. hmc_p->addr = arr[0];
  1384. hmc_p = &qpmr->rq_pbl;
  1385. hmc_p->addr = arr[req->sq_pages];
  1386. }
  1387. } else { /* CQ */
  1388. hmc_p = &cqmr->cq_pbl;
  1389. cqmr->shadow = (dma_addr_t)arr[total];
  1390. if (use_pbles)
  1391. ret = i40iw_check_mem_contiguous(arr, req->cq_pages, pg_size);
  1392. if (!ret)
  1393. hmc_p->idx = palloc->level1.idx;
  1394. else
  1395. hmc_p->addr = arr[0];
  1396. }
  1397. if (use_pbles && ret) {
  1398. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1399. iwpbl->pbl_allocated = false;
  1400. }
  1401. return err;
  1402. }
  1403. /**
  1404. * i40iw_hw_alloc_stag - cqp command to allocate stag
  1405. * @iwdev: iwarp device
  1406. * @iwmr: iwarp mr pointer
  1407. */
  1408. static int i40iw_hw_alloc_stag(struct i40iw_device *iwdev, struct i40iw_mr *iwmr)
  1409. {
  1410. struct i40iw_allocate_stag_info *info;
  1411. struct i40iw_pd *iwpd = to_iwpd(iwmr->ibmr.pd);
  1412. enum i40iw_status_code status;
  1413. int err = 0;
  1414. struct i40iw_cqp_request *cqp_request;
  1415. struct cqp_commands_info *cqp_info;
  1416. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  1417. if (!cqp_request)
  1418. return -ENOMEM;
  1419. cqp_info = &cqp_request->info;
  1420. info = &cqp_info->in.u.alloc_stag.info;
  1421. memset(info, 0, sizeof(*info));
  1422. info->page_size = PAGE_SIZE;
  1423. info->stag_idx = iwmr->stag >> I40IW_CQPSQ_STAG_IDX_SHIFT;
  1424. info->pd_id = iwpd->sc_pd.pd_id;
  1425. info->total_len = iwmr->length;
  1426. info->remote_access = true;
  1427. cqp_info->cqp_cmd = OP_ALLOC_STAG;
  1428. cqp_info->post_sq = 1;
  1429. cqp_info->in.u.alloc_stag.dev = &iwdev->sc_dev;
  1430. cqp_info->in.u.alloc_stag.scratch = (uintptr_t)cqp_request;
  1431. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  1432. if (status) {
  1433. err = -ENOMEM;
  1434. i40iw_pr_err("CQP-OP MR Reg fail");
  1435. }
  1436. return err;
  1437. }
  1438. /**
  1439. * i40iw_alloc_mr - register stag for fast memory registration
  1440. * @pd: ibpd pointer
  1441. * @mr_type: memory for stag registrion
  1442. * @max_num_sg: man number of pages
  1443. */
  1444. static struct ib_mr *i40iw_alloc_mr(struct ib_pd *pd,
  1445. enum ib_mr_type mr_type,
  1446. u32 max_num_sg)
  1447. {
  1448. struct i40iw_pd *iwpd = to_iwpd(pd);
  1449. struct i40iw_device *iwdev = to_iwdev(pd->device);
  1450. struct i40iw_pble_alloc *palloc;
  1451. struct i40iw_pbl *iwpbl;
  1452. struct i40iw_mr *iwmr;
  1453. enum i40iw_status_code status;
  1454. u32 stag;
  1455. int err_code = -ENOMEM;
  1456. iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
  1457. if (!iwmr)
  1458. return ERR_PTR(-ENOMEM);
  1459. stag = i40iw_create_stag(iwdev);
  1460. if (!stag) {
  1461. err_code = -EOVERFLOW;
  1462. goto err;
  1463. }
  1464. stag &= ~I40IW_CQPSQ_STAG_KEY_MASK;
  1465. iwmr->stag = stag;
  1466. iwmr->ibmr.rkey = stag;
  1467. iwmr->ibmr.lkey = stag;
  1468. iwmr->ibmr.pd = pd;
  1469. iwmr->ibmr.device = pd->device;
  1470. iwpbl = &iwmr->iwpbl;
  1471. iwpbl->iwmr = iwmr;
  1472. iwmr->type = IW_MEMREG_TYPE_MEM;
  1473. palloc = &iwpbl->pble_alloc;
  1474. iwmr->page_cnt = max_num_sg;
  1475. mutex_lock(&iwdev->pbl_mutex);
  1476. status = i40iw_get_pble(&iwdev->sc_dev, iwdev->pble_rsrc, palloc, iwmr->page_cnt);
  1477. mutex_unlock(&iwdev->pbl_mutex);
  1478. if (status)
  1479. goto err1;
  1480. if (palloc->level != I40IW_LEVEL_1)
  1481. goto err2;
  1482. err_code = i40iw_hw_alloc_stag(iwdev, iwmr);
  1483. if (err_code)
  1484. goto err2;
  1485. iwpbl->pbl_allocated = true;
  1486. i40iw_add_pdusecount(iwpd);
  1487. return &iwmr->ibmr;
  1488. err2:
  1489. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1490. err1:
  1491. i40iw_free_stag(iwdev, stag);
  1492. err:
  1493. kfree(iwmr);
  1494. return ERR_PTR(err_code);
  1495. }
  1496. /**
  1497. * i40iw_set_page - populate pbl list for fmr
  1498. * @ibmr: ib mem to access iwarp mr pointer
  1499. * @addr: page dma address fro pbl list
  1500. */
  1501. static int i40iw_set_page(struct ib_mr *ibmr, u64 addr)
  1502. {
  1503. struct i40iw_mr *iwmr = to_iwmr(ibmr);
  1504. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1505. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1506. u64 *pbl;
  1507. if (unlikely(iwmr->npages == iwmr->page_cnt))
  1508. return -ENOMEM;
  1509. pbl = (u64 *)palloc->level1.addr;
  1510. pbl[iwmr->npages++] = cpu_to_le64(addr);
  1511. return 0;
  1512. }
  1513. /**
  1514. * i40iw_map_mr_sg - map of sg list for fmr
  1515. * @ibmr: ib mem to access iwarp mr pointer
  1516. * @sg: scatter gather list for fmr
  1517. * @sg_nents: number of sg pages
  1518. */
  1519. static int i40iw_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
  1520. int sg_nents, unsigned int *sg_offset)
  1521. {
  1522. struct i40iw_mr *iwmr = to_iwmr(ibmr);
  1523. iwmr->npages = 0;
  1524. return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, i40iw_set_page);
  1525. }
  1526. /**
  1527. * i40iw_drain_sq - drain the send queue
  1528. * @ibqp: ib qp pointer
  1529. */
  1530. static void i40iw_drain_sq(struct ib_qp *ibqp)
  1531. {
  1532. struct i40iw_qp *iwqp = to_iwqp(ibqp);
  1533. struct i40iw_sc_qp *qp = &iwqp->sc_qp;
  1534. if (I40IW_RING_MORE_WORK(qp->qp_uk.sq_ring))
  1535. wait_for_completion(&iwqp->sq_drained);
  1536. }
  1537. /**
  1538. * i40iw_drain_rq - drain the receive queue
  1539. * @ibqp: ib qp pointer
  1540. */
  1541. static void i40iw_drain_rq(struct ib_qp *ibqp)
  1542. {
  1543. struct i40iw_qp *iwqp = to_iwqp(ibqp);
  1544. struct i40iw_sc_qp *qp = &iwqp->sc_qp;
  1545. if (I40IW_RING_MORE_WORK(qp->qp_uk.rq_ring))
  1546. wait_for_completion(&iwqp->rq_drained);
  1547. }
  1548. /**
  1549. * i40iw_hwreg_mr - send cqp command for memory registration
  1550. * @iwdev: iwarp device
  1551. * @iwmr: iwarp mr pointer
  1552. * @access: access for MR
  1553. */
  1554. static int i40iw_hwreg_mr(struct i40iw_device *iwdev,
  1555. struct i40iw_mr *iwmr,
  1556. u16 access)
  1557. {
  1558. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1559. struct i40iw_reg_ns_stag_info *stag_info;
  1560. struct i40iw_pd *iwpd = to_iwpd(iwmr->ibmr.pd);
  1561. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1562. enum i40iw_status_code status;
  1563. int err = 0;
  1564. struct i40iw_cqp_request *cqp_request;
  1565. struct cqp_commands_info *cqp_info;
  1566. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  1567. if (!cqp_request)
  1568. return -ENOMEM;
  1569. cqp_info = &cqp_request->info;
  1570. stag_info = &cqp_info->in.u.mr_reg_non_shared.info;
  1571. memset(stag_info, 0, sizeof(*stag_info));
  1572. stag_info->va = (void *)(unsigned long)iwpbl->user_base;
  1573. stag_info->stag_idx = iwmr->stag >> I40IW_CQPSQ_STAG_IDX_SHIFT;
  1574. stag_info->stag_key = (u8)iwmr->stag;
  1575. stag_info->total_len = iwmr->length;
  1576. stag_info->access_rights = access;
  1577. stag_info->pd_id = iwpd->sc_pd.pd_id;
  1578. stag_info->addr_type = I40IW_ADDR_TYPE_VA_BASED;
  1579. stag_info->page_size = iwmr->page_size;
  1580. if (iwpbl->pbl_allocated) {
  1581. if (palloc->level == I40IW_LEVEL_1) {
  1582. stag_info->first_pm_pbl_index = palloc->level1.idx;
  1583. stag_info->chunk_size = 1;
  1584. } else {
  1585. stag_info->first_pm_pbl_index = palloc->level2.root.idx;
  1586. stag_info->chunk_size = 3;
  1587. }
  1588. } else {
  1589. stag_info->reg_addr_pa = iwmr->pgaddrmem[0];
  1590. }
  1591. cqp_info->cqp_cmd = OP_MR_REG_NON_SHARED;
  1592. cqp_info->post_sq = 1;
  1593. cqp_info->in.u.mr_reg_non_shared.dev = &iwdev->sc_dev;
  1594. cqp_info->in.u.mr_reg_non_shared.scratch = (uintptr_t)cqp_request;
  1595. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  1596. if (status) {
  1597. err = -ENOMEM;
  1598. i40iw_pr_err("CQP-OP MR Reg fail");
  1599. }
  1600. return err;
  1601. }
  1602. /**
  1603. * i40iw_reg_user_mr - Register a user memory region
  1604. * @pd: ptr of pd
  1605. * @start: virtual start address
  1606. * @length: length of mr
  1607. * @virt: virtual address
  1608. * @acc: access of mr
  1609. * @udata: user data
  1610. */
  1611. static struct ib_mr *i40iw_reg_user_mr(struct ib_pd *pd,
  1612. u64 start,
  1613. u64 length,
  1614. u64 virt,
  1615. int acc,
  1616. struct ib_udata *udata)
  1617. {
  1618. struct i40iw_pd *iwpd = to_iwpd(pd);
  1619. struct i40iw_device *iwdev = to_iwdev(pd->device);
  1620. struct i40iw_ucontext *ucontext;
  1621. struct i40iw_pble_alloc *palloc;
  1622. struct i40iw_pbl *iwpbl;
  1623. struct i40iw_mr *iwmr;
  1624. struct ib_umem *region;
  1625. struct i40iw_mem_reg_req req;
  1626. u64 pbl_depth = 0;
  1627. u32 stag = 0;
  1628. u16 access;
  1629. u64 region_length;
  1630. bool use_pbles = false;
  1631. unsigned long flags;
  1632. int err = -ENOSYS;
  1633. int ret;
  1634. int pg_shift;
  1635. if (iwdev->closing)
  1636. return ERR_PTR(-ENODEV);
  1637. if (length > I40IW_MAX_MR_SIZE)
  1638. return ERR_PTR(-EINVAL);
  1639. region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
  1640. if (IS_ERR(region))
  1641. return (struct ib_mr *)region;
  1642. if (ib_copy_from_udata(&req, udata, sizeof(req))) {
  1643. ib_umem_release(region);
  1644. return ERR_PTR(-EFAULT);
  1645. }
  1646. iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
  1647. if (!iwmr) {
  1648. ib_umem_release(region);
  1649. return ERR_PTR(-ENOMEM);
  1650. }
  1651. iwpbl = &iwmr->iwpbl;
  1652. iwpbl->iwmr = iwmr;
  1653. iwmr->region = region;
  1654. iwmr->ibmr.pd = pd;
  1655. iwmr->ibmr.device = pd->device;
  1656. ucontext = to_ucontext(pd->uobject->context);
  1657. iwmr->page_size = PAGE_SIZE;
  1658. iwmr->page_msk = PAGE_MASK;
  1659. if (region->hugetlb && (req.reg_type == IW_MEMREG_TYPE_MEM))
  1660. i40iw_set_hugetlb_values(start, iwmr);
  1661. region_length = region->length + (start & (iwmr->page_size - 1));
  1662. pg_shift = ffs(iwmr->page_size) - 1;
  1663. pbl_depth = region_length >> pg_shift;
  1664. pbl_depth += (region_length & (iwmr->page_size - 1)) ? 1 : 0;
  1665. iwmr->length = region->length;
  1666. iwpbl->user_base = virt;
  1667. palloc = &iwpbl->pble_alloc;
  1668. iwmr->type = req.reg_type;
  1669. iwmr->page_cnt = (u32)pbl_depth;
  1670. switch (req.reg_type) {
  1671. case IW_MEMREG_TYPE_QP:
  1672. use_pbles = ((req.sq_pages + req.rq_pages) > 2);
  1673. err = i40iw_handle_q_mem(iwdev, &req, iwpbl, use_pbles);
  1674. if (err)
  1675. goto error;
  1676. spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
  1677. list_add_tail(&iwpbl->list, &ucontext->qp_reg_mem_list);
  1678. iwpbl->on_list = true;
  1679. spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
  1680. break;
  1681. case IW_MEMREG_TYPE_CQ:
  1682. use_pbles = (req.cq_pages > 1);
  1683. err = i40iw_handle_q_mem(iwdev, &req, iwpbl, use_pbles);
  1684. if (err)
  1685. goto error;
  1686. spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
  1687. list_add_tail(&iwpbl->list, &ucontext->cq_reg_mem_list);
  1688. iwpbl->on_list = true;
  1689. spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
  1690. break;
  1691. case IW_MEMREG_TYPE_MEM:
  1692. use_pbles = (iwmr->page_cnt != 1);
  1693. access = I40IW_ACCESS_FLAGS_LOCALREAD;
  1694. err = i40iw_setup_pbles(iwdev, iwmr, use_pbles);
  1695. if (err)
  1696. goto error;
  1697. if (use_pbles) {
  1698. ret = i40iw_check_mr_contiguous(palloc, iwmr->page_size);
  1699. if (ret) {
  1700. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1701. iwpbl->pbl_allocated = false;
  1702. }
  1703. }
  1704. access |= i40iw_get_user_access(acc);
  1705. stag = i40iw_create_stag(iwdev);
  1706. if (!stag) {
  1707. err = -ENOMEM;
  1708. goto error;
  1709. }
  1710. iwmr->stag = stag;
  1711. iwmr->ibmr.rkey = stag;
  1712. iwmr->ibmr.lkey = stag;
  1713. err = i40iw_hwreg_mr(iwdev, iwmr, access);
  1714. if (err) {
  1715. i40iw_free_stag(iwdev, stag);
  1716. goto error;
  1717. }
  1718. break;
  1719. default:
  1720. goto error;
  1721. }
  1722. iwmr->type = req.reg_type;
  1723. if (req.reg_type == IW_MEMREG_TYPE_MEM)
  1724. i40iw_add_pdusecount(iwpd);
  1725. return &iwmr->ibmr;
  1726. error:
  1727. if (palloc->level != I40IW_LEVEL_0 && iwpbl->pbl_allocated)
  1728. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1729. ib_umem_release(region);
  1730. kfree(iwmr);
  1731. return ERR_PTR(err);
  1732. }
  1733. /**
  1734. * i40iw_reg_phys_mr - register kernel physical memory
  1735. * @pd: ibpd pointer
  1736. * @addr: physical address of memory to register
  1737. * @size: size of memory to register
  1738. * @acc: Access rights
  1739. * @iova_start: start of virtual address for physical buffers
  1740. */
  1741. struct ib_mr *i40iw_reg_phys_mr(struct ib_pd *pd,
  1742. u64 addr,
  1743. u64 size,
  1744. int acc,
  1745. u64 *iova_start)
  1746. {
  1747. struct i40iw_pd *iwpd = to_iwpd(pd);
  1748. struct i40iw_device *iwdev = to_iwdev(pd->device);
  1749. struct i40iw_pbl *iwpbl;
  1750. struct i40iw_mr *iwmr;
  1751. enum i40iw_status_code status;
  1752. u32 stag;
  1753. u16 access = I40IW_ACCESS_FLAGS_LOCALREAD;
  1754. int ret;
  1755. iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
  1756. if (!iwmr)
  1757. return ERR_PTR(-ENOMEM);
  1758. iwmr->ibmr.pd = pd;
  1759. iwmr->ibmr.device = pd->device;
  1760. iwpbl = &iwmr->iwpbl;
  1761. iwpbl->iwmr = iwmr;
  1762. iwmr->type = IW_MEMREG_TYPE_MEM;
  1763. iwpbl->user_base = *iova_start;
  1764. stag = i40iw_create_stag(iwdev);
  1765. if (!stag) {
  1766. ret = -EOVERFLOW;
  1767. goto err;
  1768. }
  1769. access |= i40iw_get_user_access(acc);
  1770. iwmr->stag = stag;
  1771. iwmr->ibmr.rkey = stag;
  1772. iwmr->ibmr.lkey = stag;
  1773. iwmr->page_cnt = 1;
  1774. iwmr->pgaddrmem[0] = addr;
  1775. iwmr->length = size;
  1776. status = i40iw_hwreg_mr(iwdev, iwmr, access);
  1777. if (status) {
  1778. i40iw_free_stag(iwdev, stag);
  1779. ret = -ENOMEM;
  1780. goto err;
  1781. }
  1782. i40iw_add_pdusecount(iwpd);
  1783. return &iwmr->ibmr;
  1784. err:
  1785. kfree(iwmr);
  1786. return ERR_PTR(ret);
  1787. }
  1788. /**
  1789. * i40iw_get_dma_mr - register physical mem
  1790. * @pd: ptr of pd
  1791. * @acc: access for memory
  1792. */
  1793. static struct ib_mr *i40iw_get_dma_mr(struct ib_pd *pd, int acc)
  1794. {
  1795. u64 kva = 0;
  1796. return i40iw_reg_phys_mr(pd, 0, 0, acc, &kva);
  1797. }
  1798. /**
  1799. * i40iw_del_mem_list - Deleting pbl list entries for CQ/QP
  1800. * @iwmr: iwmr for IB's user page addresses
  1801. * @ucontext: ptr to user context
  1802. */
  1803. static void i40iw_del_memlist(struct i40iw_mr *iwmr,
  1804. struct i40iw_ucontext *ucontext)
  1805. {
  1806. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1807. unsigned long flags;
  1808. switch (iwmr->type) {
  1809. case IW_MEMREG_TYPE_CQ:
  1810. spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
  1811. if (iwpbl->on_list) {
  1812. iwpbl->on_list = false;
  1813. list_del(&iwpbl->list);
  1814. }
  1815. spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
  1816. break;
  1817. case IW_MEMREG_TYPE_QP:
  1818. spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
  1819. if (iwpbl->on_list) {
  1820. iwpbl->on_list = false;
  1821. list_del(&iwpbl->list);
  1822. }
  1823. spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
  1824. break;
  1825. default:
  1826. break;
  1827. }
  1828. }
  1829. /**
  1830. * i40iw_dereg_mr - deregister mr
  1831. * @ib_mr: mr ptr for dereg
  1832. */
  1833. static int i40iw_dereg_mr(struct ib_mr *ib_mr)
  1834. {
  1835. struct ib_pd *ibpd = ib_mr->pd;
  1836. struct i40iw_pd *iwpd = to_iwpd(ibpd);
  1837. struct i40iw_mr *iwmr = to_iwmr(ib_mr);
  1838. struct i40iw_device *iwdev = to_iwdev(ib_mr->device);
  1839. enum i40iw_status_code status;
  1840. struct i40iw_dealloc_stag_info *info;
  1841. struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
  1842. struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
  1843. struct i40iw_cqp_request *cqp_request;
  1844. struct cqp_commands_info *cqp_info;
  1845. u32 stag_idx;
  1846. if (iwmr->region)
  1847. ib_umem_release(iwmr->region);
  1848. if (iwmr->type != IW_MEMREG_TYPE_MEM) {
  1849. if (ibpd->uobject) {
  1850. struct i40iw_ucontext *ucontext;
  1851. ucontext = to_ucontext(ibpd->uobject->context);
  1852. i40iw_del_memlist(iwmr, ucontext);
  1853. }
  1854. if (iwpbl->pbl_allocated && iwmr->type != IW_MEMREG_TYPE_QP)
  1855. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1856. kfree(iwmr);
  1857. return 0;
  1858. }
  1859. cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
  1860. if (!cqp_request)
  1861. return -ENOMEM;
  1862. cqp_info = &cqp_request->info;
  1863. info = &cqp_info->in.u.dealloc_stag.info;
  1864. memset(info, 0, sizeof(*info));
  1865. info->pd_id = cpu_to_le32(iwpd->sc_pd.pd_id & 0x00007fff);
  1866. info->stag_idx = RS_64_1(ib_mr->rkey, I40IW_CQPSQ_STAG_IDX_SHIFT);
  1867. stag_idx = info->stag_idx;
  1868. info->mr = true;
  1869. if (iwpbl->pbl_allocated)
  1870. info->dealloc_pbl = true;
  1871. cqp_info->cqp_cmd = OP_DEALLOC_STAG;
  1872. cqp_info->post_sq = 1;
  1873. cqp_info->in.u.dealloc_stag.dev = &iwdev->sc_dev;
  1874. cqp_info->in.u.dealloc_stag.scratch = (uintptr_t)cqp_request;
  1875. status = i40iw_handle_cqp_op(iwdev, cqp_request);
  1876. if (status)
  1877. i40iw_pr_err("CQP-OP dealloc failed for stag_idx = 0x%x\n", stag_idx);
  1878. i40iw_rem_pdusecount(iwpd, iwdev);
  1879. i40iw_free_stag(iwdev, iwmr->stag);
  1880. if (iwpbl->pbl_allocated)
  1881. i40iw_free_pble(iwdev->pble_rsrc, palloc);
  1882. kfree(iwmr);
  1883. return 0;
  1884. }
  1885. /**
  1886. * hw_rev_show
  1887. */
  1888. static ssize_t hw_rev_show(struct device *dev,
  1889. struct device_attribute *attr, char *buf)
  1890. {
  1891. struct i40iw_ib_device *iwibdev = container_of(dev,
  1892. struct i40iw_ib_device,
  1893. ibdev.dev);
  1894. u32 hw_rev = iwibdev->iwdev->sc_dev.hw_rev;
  1895. return sprintf(buf, "%x\n", hw_rev);
  1896. }
  1897. static DEVICE_ATTR_RO(hw_rev);
  1898. /**
  1899. * hca_type_show
  1900. */
  1901. static ssize_t hca_type_show(struct device *dev,
  1902. struct device_attribute *attr, char *buf)
  1903. {
  1904. return sprintf(buf, "I40IW\n");
  1905. }
  1906. static DEVICE_ATTR_RO(hca_type);
  1907. /**
  1908. * board_id_show
  1909. */
  1910. static ssize_t board_id_show(struct device *dev,
  1911. struct device_attribute *attr, char *buf)
  1912. {
  1913. return sprintf(buf, "%.*s\n", 32, "I40IW Board ID");
  1914. }
  1915. static DEVICE_ATTR_RO(board_id);
  1916. static struct attribute *i40iw_dev_attributes[] = {
  1917. &dev_attr_hw_rev.attr,
  1918. &dev_attr_hca_type.attr,
  1919. &dev_attr_board_id.attr,
  1920. NULL
  1921. };
  1922. static const struct attribute_group i40iw_attr_group = {
  1923. .attrs = i40iw_dev_attributes,
  1924. };
  1925. /**
  1926. * i40iw_copy_sg_list - copy sg list for qp
  1927. * @sg_list: copied into sg_list
  1928. * @sgl: copy from sgl
  1929. * @num_sges: count of sg entries
  1930. */
  1931. static void i40iw_copy_sg_list(struct i40iw_sge *sg_list, struct ib_sge *sgl, int num_sges)
  1932. {
  1933. unsigned int i;
  1934. for (i = 0; (i < num_sges) && (i < I40IW_MAX_WQ_FRAGMENT_COUNT); i++) {
  1935. sg_list[i].tag_off = sgl[i].addr;
  1936. sg_list[i].len = sgl[i].length;
  1937. sg_list[i].stag = sgl[i].lkey;
  1938. }
  1939. }
  1940. /**
  1941. * i40iw_post_send - kernel application wr
  1942. * @ibqp: qp ptr for wr
  1943. * @ib_wr: work request ptr
  1944. * @bad_wr: return of bad wr if err
  1945. */
  1946. static int i40iw_post_send(struct ib_qp *ibqp,
  1947. const struct ib_send_wr *ib_wr,
  1948. const struct ib_send_wr **bad_wr)
  1949. {
  1950. struct i40iw_qp *iwqp;
  1951. struct i40iw_qp_uk *ukqp;
  1952. struct i40iw_post_sq_info info;
  1953. enum i40iw_status_code ret;
  1954. int err = 0;
  1955. unsigned long flags;
  1956. bool inv_stag;
  1957. iwqp = (struct i40iw_qp *)ibqp;
  1958. ukqp = &iwqp->sc_qp.qp_uk;
  1959. spin_lock_irqsave(&iwqp->lock, flags);
  1960. if (iwqp->flush_issued) {
  1961. err = -EINVAL;
  1962. goto out;
  1963. }
  1964. while (ib_wr) {
  1965. inv_stag = false;
  1966. memset(&info, 0, sizeof(info));
  1967. info.wr_id = (u64)(ib_wr->wr_id);
  1968. if ((ib_wr->send_flags & IB_SEND_SIGNALED) || iwqp->sig_all)
  1969. info.signaled = true;
  1970. if (ib_wr->send_flags & IB_SEND_FENCE)
  1971. info.read_fence = true;
  1972. switch (ib_wr->opcode) {
  1973. case IB_WR_SEND:
  1974. /* fall-through */
  1975. case IB_WR_SEND_WITH_INV:
  1976. if (ib_wr->opcode == IB_WR_SEND) {
  1977. if (ib_wr->send_flags & IB_SEND_SOLICITED)
  1978. info.op_type = I40IW_OP_TYPE_SEND_SOL;
  1979. else
  1980. info.op_type = I40IW_OP_TYPE_SEND;
  1981. } else {
  1982. if (ib_wr->send_flags & IB_SEND_SOLICITED)
  1983. info.op_type = I40IW_OP_TYPE_SEND_SOL_INV;
  1984. else
  1985. info.op_type = I40IW_OP_TYPE_SEND_INV;
  1986. }
  1987. if (ib_wr->send_flags & IB_SEND_INLINE) {
  1988. info.op.inline_send.data = (void *)(unsigned long)ib_wr->sg_list[0].addr;
  1989. info.op.inline_send.len = ib_wr->sg_list[0].length;
  1990. ret = ukqp->ops.iw_inline_send(ukqp, &info, ib_wr->ex.invalidate_rkey, false);
  1991. } else {
  1992. info.op.send.num_sges = ib_wr->num_sge;
  1993. info.op.send.sg_list = (struct i40iw_sge *)ib_wr->sg_list;
  1994. ret = ukqp->ops.iw_send(ukqp, &info, ib_wr->ex.invalidate_rkey, false);
  1995. }
  1996. if (ret) {
  1997. if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
  1998. err = -ENOMEM;
  1999. else
  2000. err = -EINVAL;
  2001. }
  2002. break;
  2003. case IB_WR_RDMA_WRITE:
  2004. info.op_type = I40IW_OP_TYPE_RDMA_WRITE;
  2005. if (ib_wr->send_flags & IB_SEND_INLINE) {
  2006. info.op.inline_rdma_write.data = (void *)(unsigned long)ib_wr->sg_list[0].addr;
  2007. info.op.inline_rdma_write.len = ib_wr->sg_list[0].length;
  2008. info.op.inline_rdma_write.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
  2009. info.op.inline_rdma_write.rem_addr.stag = rdma_wr(ib_wr)->rkey;
  2010. ret = ukqp->ops.iw_inline_rdma_write(ukqp, &info, false);
  2011. } else {
  2012. info.op.rdma_write.lo_sg_list = (void *)ib_wr->sg_list;
  2013. info.op.rdma_write.num_lo_sges = ib_wr->num_sge;
  2014. info.op.rdma_write.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
  2015. info.op.rdma_write.rem_addr.stag = rdma_wr(ib_wr)->rkey;
  2016. ret = ukqp->ops.iw_rdma_write(ukqp, &info, false);
  2017. }
  2018. if (ret) {
  2019. if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
  2020. err = -ENOMEM;
  2021. else
  2022. err = -EINVAL;
  2023. }
  2024. break;
  2025. case IB_WR_RDMA_READ_WITH_INV:
  2026. inv_stag = true;
  2027. /* fall-through*/
  2028. case IB_WR_RDMA_READ:
  2029. if (ib_wr->num_sge > I40IW_MAX_SGE_RD) {
  2030. err = -EINVAL;
  2031. break;
  2032. }
  2033. info.op_type = I40IW_OP_TYPE_RDMA_READ;
  2034. info.op.rdma_read.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
  2035. info.op.rdma_read.rem_addr.stag = rdma_wr(ib_wr)->rkey;
  2036. info.op.rdma_read.lo_addr.tag_off = ib_wr->sg_list->addr;
  2037. info.op.rdma_read.lo_addr.stag = ib_wr->sg_list->lkey;
  2038. info.op.rdma_read.lo_addr.len = ib_wr->sg_list->length;
  2039. ret = ukqp->ops.iw_rdma_read(ukqp, &info, inv_stag, false);
  2040. if (ret) {
  2041. if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
  2042. err = -ENOMEM;
  2043. else
  2044. err = -EINVAL;
  2045. }
  2046. break;
  2047. case IB_WR_LOCAL_INV:
  2048. info.op_type = I40IW_OP_TYPE_INV_STAG;
  2049. info.op.inv_local_stag.target_stag = ib_wr->ex.invalidate_rkey;
  2050. ret = ukqp->ops.iw_stag_local_invalidate(ukqp, &info, true);
  2051. if (ret)
  2052. err = -ENOMEM;
  2053. break;
  2054. case IB_WR_REG_MR:
  2055. {
  2056. struct i40iw_mr *iwmr = to_iwmr(reg_wr(ib_wr)->mr);
  2057. int flags = reg_wr(ib_wr)->access;
  2058. struct i40iw_pble_alloc *palloc = &iwmr->iwpbl.pble_alloc;
  2059. struct i40iw_sc_dev *dev = &iwqp->iwdev->sc_dev;
  2060. struct i40iw_fast_reg_stag_info info;
  2061. memset(&info, 0, sizeof(info));
  2062. info.access_rights = I40IW_ACCESS_FLAGS_LOCALREAD;
  2063. info.access_rights |= i40iw_get_user_access(flags);
  2064. info.stag_key = reg_wr(ib_wr)->key & 0xff;
  2065. info.stag_idx = reg_wr(ib_wr)->key >> 8;
  2066. info.page_size = reg_wr(ib_wr)->mr->page_size;
  2067. info.wr_id = ib_wr->wr_id;
  2068. info.addr_type = I40IW_ADDR_TYPE_VA_BASED;
  2069. info.va = (void *)(uintptr_t)iwmr->ibmr.iova;
  2070. info.total_len = iwmr->ibmr.length;
  2071. info.reg_addr_pa = *(u64 *)palloc->level1.addr;
  2072. info.first_pm_pbl_index = palloc->level1.idx;
  2073. info.local_fence = ib_wr->send_flags & IB_SEND_FENCE;
  2074. info.signaled = ib_wr->send_flags & IB_SEND_SIGNALED;
  2075. if (iwmr->npages > I40IW_MIN_PAGES_PER_FMR)
  2076. info.chunk_size = 1;
  2077. ret = dev->iw_priv_qp_ops->iw_mr_fast_register(&iwqp->sc_qp, &info, true);
  2078. if (ret)
  2079. err = -ENOMEM;
  2080. break;
  2081. }
  2082. default:
  2083. err = -EINVAL;
  2084. i40iw_pr_err(" upost_send bad opcode = 0x%x\n",
  2085. ib_wr->opcode);
  2086. break;
  2087. }
  2088. if (err)
  2089. break;
  2090. ib_wr = ib_wr->next;
  2091. }
  2092. out:
  2093. if (err)
  2094. *bad_wr = ib_wr;
  2095. else
  2096. ukqp->ops.iw_qp_post_wr(ukqp);
  2097. spin_unlock_irqrestore(&iwqp->lock, flags);
  2098. return err;
  2099. }
  2100. /**
  2101. * i40iw_post_recv - post receive wr for kernel application
  2102. * @ibqp: ib qp pointer
  2103. * @ib_wr: work request for receive
  2104. * @bad_wr: bad wr caused an error
  2105. */
  2106. static int i40iw_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *ib_wr,
  2107. const struct ib_recv_wr **bad_wr)
  2108. {
  2109. struct i40iw_qp *iwqp;
  2110. struct i40iw_qp_uk *ukqp;
  2111. struct i40iw_post_rq_info post_recv;
  2112. struct i40iw_sge sg_list[I40IW_MAX_WQ_FRAGMENT_COUNT];
  2113. enum i40iw_status_code ret = 0;
  2114. unsigned long flags;
  2115. int err = 0;
  2116. iwqp = (struct i40iw_qp *)ibqp;
  2117. ukqp = &iwqp->sc_qp.qp_uk;
  2118. memset(&post_recv, 0, sizeof(post_recv));
  2119. spin_lock_irqsave(&iwqp->lock, flags);
  2120. if (iwqp->flush_issued) {
  2121. err = -EINVAL;
  2122. goto out;
  2123. }
  2124. while (ib_wr) {
  2125. post_recv.num_sges = ib_wr->num_sge;
  2126. post_recv.wr_id = ib_wr->wr_id;
  2127. i40iw_copy_sg_list(sg_list, ib_wr->sg_list, ib_wr->num_sge);
  2128. post_recv.sg_list = sg_list;
  2129. ret = ukqp->ops.iw_post_receive(ukqp, &post_recv);
  2130. if (ret) {
  2131. i40iw_pr_err(" post_recv err %d\n", ret);
  2132. if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
  2133. err = -ENOMEM;
  2134. else
  2135. err = -EINVAL;
  2136. *bad_wr = ib_wr;
  2137. goto out;
  2138. }
  2139. ib_wr = ib_wr->next;
  2140. }
  2141. out:
  2142. spin_unlock_irqrestore(&iwqp->lock, flags);
  2143. return err;
  2144. }
  2145. /**
  2146. * i40iw_poll_cq - poll cq for completion (kernel apps)
  2147. * @ibcq: cq to poll
  2148. * @num_entries: number of entries to poll
  2149. * @entry: wr of entry completed
  2150. */
  2151. static int i40iw_poll_cq(struct ib_cq *ibcq,
  2152. int num_entries,
  2153. struct ib_wc *entry)
  2154. {
  2155. struct i40iw_cq *iwcq;
  2156. int cqe_count = 0;
  2157. struct i40iw_cq_poll_info cq_poll_info;
  2158. enum i40iw_status_code ret;
  2159. struct i40iw_cq_uk *ukcq;
  2160. struct i40iw_sc_qp *qp;
  2161. struct i40iw_qp *iwqp;
  2162. unsigned long flags;
  2163. iwcq = (struct i40iw_cq *)ibcq;
  2164. ukcq = &iwcq->sc_cq.cq_uk;
  2165. spin_lock_irqsave(&iwcq->lock, flags);
  2166. while (cqe_count < num_entries) {
  2167. ret = ukcq->ops.iw_cq_poll_completion(ukcq, &cq_poll_info);
  2168. if (ret == I40IW_ERR_QUEUE_EMPTY) {
  2169. break;
  2170. } else if (ret == I40IW_ERR_QUEUE_DESTROYED) {
  2171. continue;
  2172. } else if (ret) {
  2173. if (!cqe_count)
  2174. cqe_count = -1;
  2175. break;
  2176. }
  2177. entry->wc_flags = 0;
  2178. entry->wr_id = cq_poll_info.wr_id;
  2179. if (cq_poll_info.error) {
  2180. entry->status = IB_WC_WR_FLUSH_ERR;
  2181. entry->vendor_err = cq_poll_info.major_err << 16 | cq_poll_info.minor_err;
  2182. } else {
  2183. entry->status = IB_WC_SUCCESS;
  2184. }
  2185. switch (cq_poll_info.op_type) {
  2186. case I40IW_OP_TYPE_RDMA_WRITE:
  2187. entry->opcode = IB_WC_RDMA_WRITE;
  2188. break;
  2189. case I40IW_OP_TYPE_RDMA_READ_INV_STAG:
  2190. case I40IW_OP_TYPE_RDMA_READ:
  2191. entry->opcode = IB_WC_RDMA_READ;
  2192. break;
  2193. case I40IW_OP_TYPE_SEND_SOL:
  2194. case I40IW_OP_TYPE_SEND_SOL_INV:
  2195. case I40IW_OP_TYPE_SEND_INV:
  2196. case I40IW_OP_TYPE_SEND:
  2197. entry->opcode = IB_WC_SEND;
  2198. break;
  2199. case I40IW_OP_TYPE_REC:
  2200. entry->opcode = IB_WC_RECV;
  2201. break;
  2202. default:
  2203. entry->opcode = IB_WC_RECV;
  2204. break;
  2205. }
  2206. entry->ex.imm_data = 0;
  2207. qp = (struct i40iw_sc_qp *)cq_poll_info.qp_handle;
  2208. entry->qp = (struct ib_qp *)qp->back_qp;
  2209. entry->src_qp = cq_poll_info.qp_id;
  2210. iwqp = (struct i40iw_qp *)qp->back_qp;
  2211. if (iwqp->iwarp_state > I40IW_QP_STATE_RTS) {
  2212. if (!I40IW_RING_MORE_WORK(qp->qp_uk.sq_ring))
  2213. complete(&iwqp->sq_drained);
  2214. if (!I40IW_RING_MORE_WORK(qp->qp_uk.rq_ring))
  2215. complete(&iwqp->rq_drained);
  2216. }
  2217. entry->byte_len = cq_poll_info.bytes_xfered;
  2218. entry++;
  2219. cqe_count++;
  2220. }
  2221. spin_unlock_irqrestore(&iwcq->lock, flags);
  2222. return cqe_count;
  2223. }
  2224. /**
  2225. * i40iw_req_notify_cq - arm cq kernel application
  2226. * @ibcq: cq to arm
  2227. * @notify_flags: notofication flags
  2228. */
  2229. static int i40iw_req_notify_cq(struct ib_cq *ibcq,
  2230. enum ib_cq_notify_flags notify_flags)
  2231. {
  2232. struct i40iw_cq *iwcq;
  2233. struct i40iw_cq_uk *ukcq;
  2234. unsigned long flags;
  2235. enum i40iw_completion_notify cq_notify = IW_CQ_COMPL_EVENT;
  2236. iwcq = (struct i40iw_cq *)ibcq;
  2237. ukcq = &iwcq->sc_cq.cq_uk;
  2238. if (notify_flags == IB_CQ_SOLICITED)
  2239. cq_notify = IW_CQ_COMPL_SOLICITED;
  2240. spin_lock_irqsave(&iwcq->lock, flags);
  2241. ukcq->ops.iw_cq_request_notification(ukcq, cq_notify);
  2242. spin_unlock_irqrestore(&iwcq->lock, flags);
  2243. return 0;
  2244. }
  2245. /**
  2246. * i40iw_port_immutable - return port's immutable data
  2247. * @ibdev: ib dev struct
  2248. * @port_num: port number
  2249. * @immutable: immutable data for the port return
  2250. */
  2251. static int i40iw_port_immutable(struct ib_device *ibdev, u8 port_num,
  2252. struct ib_port_immutable *immutable)
  2253. {
  2254. struct ib_port_attr attr;
  2255. int err;
  2256. immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
  2257. err = ib_query_port(ibdev, port_num, &attr);
  2258. if (err)
  2259. return err;
  2260. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  2261. immutable->gid_tbl_len = attr.gid_tbl_len;
  2262. return 0;
  2263. }
  2264. static const char * const i40iw_hw_stat_names[] = {
  2265. // 32bit names
  2266. [I40IW_HW_STAT_INDEX_IP4RXDISCARD] = "ip4InDiscards",
  2267. [I40IW_HW_STAT_INDEX_IP4RXTRUNC] = "ip4InTruncatedPkts",
  2268. [I40IW_HW_STAT_INDEX_IP4TXNOROUTE] = "ip4OutNoRoutes",
  2269. [I40IW_HW_STAT_INDEX_IP6RXDISCARD] = "ip6InDiscards",
  2270. [I40IW_HW_STAT_INDEX_IP6RXTRUNC] = "ip6InTruncatedPkts",
  2271. [I40IW_HW_STAT_INDEX_IP6TXNOROUTE] = "ip6OutNoRoutes",
  2272. [I40IW_HW_STAT_INDEX_TCPRTXSEG] = "tcpRetransSegs",
  2273. [I40IW_HW_STAT_INDEX_TCPRXOPTERR] = "tcpInOptErrors",
  2274. [I40IW_HW_STAT_INDEX_TCPRXPROTOERR] = "tcpInProtoErrors",
  2275. // 64bit names
  2276. [I40IW_HW_STAT_INDEX_IP4RXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2277. "ip4InOctets",
  2278. [I40IW_HW_STAT_INDEX_IP4RXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2279. "ip4InPkts",
  2280. [I40IW_HW_STAT_INDEX_IP4RXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
  2281. "ip4InReasmRqd",
  2282. [I40IW_HW_STAT_INDEX_IP4RXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2283. "ip4InMcastPkts",
  2284. [I40IW_HW_STAT_INDEX_IP4TXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2285. "ip4OutOctets",
  2286. [I40IW_HW_STAT_INDEX_IP4TXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2287. "ip4OutPkts",
  2288. [I40IW_HW_STAT_INDEX_IP4TXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
  2289. "ip4OutSegRqd",
  2290. [I40IW_HW_STAT_INDEX_IP4TXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2291. "ip4OutMcastPkts",
  2292. [I40IW_HW_STAT_INDEX_IP6RXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2293. "ip6InOctets",
  2294. [I40IW_HW_STAT_INDEX_IP6RXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2295. "ip6InPkts",
  2296. [I40IW_HW_STAT_INDEX_IP6RXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
  2297. "ip6InReasmRqd",
  2298. [I40IW_HW_STAT_INDEX_IP6RXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2299. "ip6InMcastPkts",
  2300. [I40IW_HW_STAT_INDEX_IP6TXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2301. "ip6OutOctets",
  2302. [I40IW_HW_STAT_INDEX_IP6TXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2303. "ip6OutPkts",
  2304. [I40IW_HW_STAT_INDEX_IP6TXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
  2305. "ip6OutSegRqd",
  2306. [I40IW_HW_STAT_INDEX_IP6TXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
  2307. "ip6OutMcastPkts",
  2308. [I40IW_HW_STAT_INDEX_TCPRXSEGS + I40IW_HW_STAT_INDEX_MAX_32] =
  2309. "tcpInSegs",
  2310. [I40IW_HW_STAT_INDEX_TCPTXSEG + I40IW_HW_STAT_INDEX_MAX_32] =
  2311. "tcpOutSegs",
  2312. [I40IW_HW_STAT_INDEX_RDMARXRDS + I40IW_HW_STAT_INDEX_MAX_32] =
  2313. "iwInRdmaReads",
  2314. [I40IW_HW_STAT_INDEX_RDMARXSNDS + I40IW_HW_STAT_INDEX_MAX_32] =
  2315. "iwInRdmaSends",
  2316. [I40IW_HW_STAT_INDEX_RDMARXWRS + I40IW_HW_STAT_INDEX_MAX_32] =
  2317. "iwInRdmaWrites",
  2318. [I40IW_HW_STAT_INDEX_RDMATXRDS + I40IW_HW_STAT_INDEX_MAX_32] =
  2319. "iwOutRdmaReads",
  2320. [I40IW_HW_STAT_INDEX_RDMATXSNDS + I40IW_HW_STAT_INDEX_MAX_32] =
  2321. "iwOutRdmaSends",
  2322. [I40IW_HW_STAT_INDEX_RDMATXWRS + I40IW_HW_STAT_INDEX_MAX_32] =
  2323. "iwOutRdmaWrites",
  2324. [I40IW_HW_STAT_INDEX_RDMAVBND + I40IW_HW_STAT_INDEX_MAX_32] =
  2325. "iwRdmaBnd",
  2326. [I40IW_HW_STAT_INDEX_RDMAVINV + I40IW_HW_STAT_INDEX_MAX_32] =
  2327. "iwRdmaInv"
  2328. };
  2329. static void i40iw_get_dev_fw_str(struct ib_device *dev, char *str)
  2330. {
  2331. u32 firmware_version = I40IW_FW_VERSION;
  2332. snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u", firmware_version,
  2333. (firmware_version & 0x000000ff));
  2334. }
  2335. /**
  2336. * i40iw_alloc_hw_stats - Allocate a hw stats structure
  2337. * @ibdev: device pointer from stack
  2338. * @port_num: port number
  2339. */
  2340. static struct rdma_hw_stats *i40iw_alloc_hw_stats(struct ib_device *ibdev,
  2341. u8 port_num)
  2342. {
  2343. struct i40iw_device *iwdev = to_iwdev(ibdev);
  2344. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  2345. int num_counters = I40IW_HW_STAT_INDEX_MAX_32 +
  2346. I40IW_HW_STAT_INDEX_MAX_64;
  2347. unsigned long lifespan = RDMA_HW_STATS_DEFAULT_LIFESPAN;
  2348. BUILD_BUG_ON(ARRAY_SIZE(i40iw_hw_stat_names) !=
  2349. (I40IW_HW_STAT_INDEX_MAX_32 +
  2350. I40IW_HW_STAT_INDEX_MAX_64));
  2351. /*
  2352. * PFs get the default update lifespan, but VFs only update once
  2353. * per second
  2354. */
  2355. if (!dev->is_pf)
  2356. lifespan = 1000;
  2357. return rdma_alloc_hw_stats_struct(i40iw_hw_stat_names, num_counters,
  2358. lifespan);
  2359. }
  2360. /**
  2361. * i40iw_get_hw_stats - Populates the rdma_hw_stats structure
  2362. * @ibdev: device pointer from stack
  2363. * @stats: stats pointer from stack
  2364. * @port_num: port number
  2365. * @index: which hw counter the stack is requesting we update
  2366. */
  2367. static int i40iw_get_hw_stats(struct ib_device *ibdev,
  2368. struct rdma_hw_stats *stats,
  2369. u8 port_num, int index)
  2370. {
  2371. struct i40iw_device *iwdev = to_iwdev(ibdev);
  2372. struct i40iw_sc_dev *dev = &iwdev->sc_dev;
  2373. struct i40iw_vsi_pestat *devstat = iwdev->vsi.pestat;
  2374. struct i40iw_dev_hw_stats *hw_stats = &devstat->hw_stats;
  2375. if (dev->is_pf) {
  2376. i40iw_hw_stats_read_all(devstat, &devstat->hw_stats);
  2377. } else {
  2378. if (i40iw_vchnl_vf_get_pe_stats(dev, &devstat->hw_stats))
  2379. return -ENOSYS;
  2380. }
  2381. memcpy(&stats->value[0], hw_stats, sizeof(*hw_stats));
  2382. return stats->num_counters;
  2383. }
  2384. /**
  2385. * i40iw_query_gid - Query port GID
  2386. * @ibdev: device pointer from stack
  2387. * @port: port number
  2388. * @index: Entry index
  2389. * @gid: Global ID
  2390. */
  2391. static int i40iw_query_gid(struct ib_device *ibdev,
  2392. u8 port,
  2393. int index,
  2394. union ib_gid *gid)
  2395. {
  2396. struct i40iw_device *iwdev = to_iwdev(ibdev);
  2397. memset(gid->raw, 0, sizeof(gid->raw));
  2398. ether_addr_copy(gid->raw, iwdev->netdev->dev_addr);
  2399. return 0;
  2400. }
  2401. /**
  2402. * i40iw_query_pkey - Query partition key
  2403. * @ibdev: device pointer from stack
  2404. * @port: port number
  2405. * @index: index of pkey
  2406. * @pkey: pointer to store the pkey
  2407. */
  2408. static int i40iw_query_pkey(struct ib_device *ibdev,
  2409. u8 port,
  2410. u16 index,
  2411. u16 *pkey)
  2412. {
  2413. *pkey = 0;
  2414. return 0;
  2415. }
  2416. /**
  2417. * i40iw_get_vector_affinity - report IRQ affinity mask
  2418. * @ibdev: IB device
  2419. * @comp_vector: completion vector index
  2420. */
  2421. static const struct cpumask *i40iw_get_vector_affinity(struct ib_device *ibdev,
  2422. int comp_vector)
  2423. {
  2424. struct i40iw_device *iwdev = to_iwdev(ibdev);
  2425. struct i40iw_msix_vector *msix_vec;
  2426. if (iwdev->msix_shared)
  2427. msix_vec = &iwdev->iw_msixtbl[comp_vector];
  2428. else
  2429. msix_vec = &iwdev->iw_msixtbl[comp_vector + 1];
  2430. return irq_get_affinity_mask(msix_vec->irq);
  2431. }
  2432. /**
  2433. * i40iw_init_rdma_device - initialization of iwarp device
  2434. * @iwdev: iwarp device
  2435. */
  2436. static struct i40iw_ib_device *i40iw_init_rdma_device(struct i40iw_device *iwdev)
  2437. {
  2438. struct i40iw_ib_device *iwibdev;
  2439. struct net_device *netdev = iwdev->netdev;
  2440. struct pci_dev *pcidev = (struct pci_dev *)iwdev->hw.dev_context;
  2441. iwibdev = (struct i40iw_ib_device *)ib_alloc_device(sizeof(*iwibdev));
  2442. if (!iwibdev) {
  2443. i40iw_pr_err("iwdev == NULL\n");
  2444. return NULL;
  2445. }
  2446. iwibdev->ibdev.owner = THIS_MODULE;
  2447. iwdev->iwibdev = iwibdev;
  2448. iwibdev->iwdev = iwdev;
  2449. iwibdev->ibdev.node_type = RDMA_NODE_RNIC;
  2450. ether_addr_copy((u8 *)&iwibdev->ibdev.node_guid, netdev->dev_addr);
  2451. iwibdev->ibdev.uverbs_cmd_mask =
  2452. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  2453. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  2454. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  2455. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  2456. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  2457. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  2458. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  2459. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  2460. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  2461. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  2462. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  2463. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  2464. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  2465. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  2466. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  2467. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  2468. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  2469. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  2470. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  2471. (1ull << IB_USER_VERBS_CMD_POST_SEND);
  2472. iwibdev->ibdev.phys_port_cnt = 1;
  2473. iwibdev->ibdev.num_comp_vectors = iwdev->ceqs_count;
  2474. iwibdev->ibdev.dev.parent = &pcidev->dev;
  2475. iwibdev->ibdev.query_port = i40iw_query_port;
  2476. iwibdev->ibdev.query_pkey = i40iw_query_pkey;
  2477. iwibdev->ibdev.query_gid = i40iw_query_gid;
  2478. iwibdev->ibdev.alloc_ucontext = i40iw_alloc_ucontext;
  2479. iwibdev->ibdev.dealloc_ucontext = i40iw_dealloc_ucontext;
  2480. iwibdev->ibdev.mmap = i40iw_mmap;
  2481. iwibdev->ibdev.alloc_pd = i40iw_alloc_pd;
  2482. iwibdev->ibdev.dealloc_pd = i40iw_dealloc_pd;
  2483. iwibdev->ibdev.create_qp = i40iw_create_qp;
  2484. iwibdev->ibdev.modify_qp = i40iw_modify_qp;
  2485. iwibdev->ibdev.query_qp = i40iw_query_qp;
  2486. iwibdev->ibdev.destroy_qp = i40iw_destroy_qp;
  2487. iwibdev->ibdev.create_cq = i40iw_create_cq;
  2488. iwibdev->ibdev.destroy_cq = i40iw_destroy_cq;
  2489. iwibdev->ibdev.get_dma_mr = i40iw_get_dma_mr;
  2490. iwibdev->ibdev.reg_user_mr = i40iw_reg_user_mr;
  2491. iwibdev->ibdev.dereg_mr = i40iw_dereg_mr;
  2492. iwibdev->ibdev.alloc_hw_stats = i40iw_alloc_hw_stats;
  2493. iwibdev->ibdev.get_hw_stats = i40iw_get_hw_stats;
  2494. iwibdev->ibdev.query_device = i40iw_query_device;
  2495. iwibdev->ibdev.drain_sq = i40iw_drain_sq;
  2496. iwibdev->ibdev.drain_rq = i40iw_drain_rq;
  2497. iwibdev->ibdev.alloc_mr = i40iw_alloc_mr;
  2498. iwibdev->ibdev.map_mr_sg = i40iw_map_mr_sg;
  2499. iwibdev->ibdev.iwcm = kzalloc(sizeof(*iwibdev->ibdev.iwcm), GFP_KERNEL);
  2500. if (!iwibdev->ibdev.iwcm) {
  2501. ib_dealloc_device(&iwibdev->ibdev);
  2502. return NULL;
  2503. }
  2504. iwibdev->ibdev.iwcm->add_ref = i40iw_add_ref;
  2505. iwibdev->ibdev.iwcm->rem_ref = i40iw_rem_ref;
  2506. iwibdev->ibdev.iwcm->get_qp = i40iw_get_qp;
  2507. iwibdev->ibdev.iwcm->connect = i40iw_connect;
  2508. iwibdev->ibdev.iwcm->accept = i40iw_accept;
  2509. iwibdev->ibdev.iwcm->reject = i40iw_reject;
  2510. iwibdev->ibdev.iwcm->create_listen = i40iw_create_listen;
  2511. iwibdev->ibdev.iwcm->destroy_listen = i40iw_destroy_listen;
  2512. memcpy(iwibdev->ibdev.iwcm->ifname, netdev->name,
  2513. sizeof(iwibdev->ibdev.iwcm->ifname));
  2514. iwibdev->ibdev.get_port_immutable = i40iw_port_immutable;
  2515. iwibdev->ibdev.get_dev_fw_str = i40iw_get_dev_fw_str;
  2516. iwibdev->ibdev.poll_cq = i40iw_poll_cq;
  2517. iwibdev->ibdev.req_notify_cq = i40iw_req_notify_cq;
  2518. iwibdev->ibdev.post_send = i40iw_post_send;
  2519. iwibdev->ibdev.post_recv = i40iw_post_recv;
  2520. iwibdev->ibdev.get_vector_affinity = i40iw_get_vector_affinity;
  2521. return iwibdev;
  2522. }
  2523. /**
  2524. * i40iw_port_ibevent - indicate port event
  2525. * @iwdev: iwarp device
  2526. */
  2527. void i40iw_port_ibevent(struct i40iw_device *iwdev)
  2528. {
  2529. struct i40iw_ib_device *iwibdev = iwdev->iwibdev;
  2530. struct ib_event event;
  2531. event.device = &iwibdev->ibdev;
  2532. event.element.port_num = 1;
  2533. event.event = iwdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  2534. ib_dispatch_event(&event);
  2535. }
  2536. /**
  2537. * i40iw_destroy_rdma_device - destroy rdma device and free resources
  2538. * @iwibdev: IB device ptr
  2539. */
  2540. void i40iw_destroy_rdma_device(struct i40iw_ib_device *iwibdev)
  2541. {
  2542. if (!iwibdev)
  2543. return;
  2544. ib_unregister_device(&iwibdev->ibdev);
  2545. kfree(iwibdev->ibdev.iwcm);
  2546. iwibdev->ibdev.iwcm = NULL;
  2547. wait_event_timeout(iwibdev->iwdev->close_wq,
  2548. !atomic64_read(&iwibdev->iwdev->use_count),
  2549. I40IW_EVENT_TIMEOUT);
  2550. ib_dealloc_device(&iwibdev->ibdev);
  2551. }
  2552. /**
  2553. * i40iw_register_rdma_device - register iwarp device to IB
  2554. * @iwdev: iwarp device
  2555. */
  2556. int i40iw_register_rdma_device(struct i40iw_device *iwdev)
  2557. {
  2558. int ret;
  2559. struct i40iw_ib_device *iwibdev;
  2560. iwdev->iwibdev = i40iw_init_rdma_device(iwdev);
  2561. if (!iwdev->iwibdev)
  2562. return -ENOMEM;
  2563. iwibdev = iwdev->iwibdev;
  2564. rdma_set_device_sysfs_group(&iwibdev->ibdev, &i40iw_attr_group);
  2565. iwibdev->ibdev.driver_id = RDMA_DRIVER_I40IW;
  2566. ret = ib_register_device(&iwibdev->ibdev, "i40iw%d", NULL);
  2567. if (ret)
  2568. goto error;
  2569. return 0;
  2570. error:
  2571. kfree(iwdev->iwibdev->ibdev.iwcm);
  2572. iwdev->iwibdev->ibdev.iwcm = NULL;
  2573. ib_dealloc_device(&iwdev->iwibdev->ibdev);
  2574. return ret;
  2575. }