i40iw_verbs.c 79 KB

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