i40iw_verbs.c 79 KB

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