ocrdma_verbs.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080
  1. /*******************************************************************
  2. * This file is part of the Emulex RoCE Device Driver for *
  3. * RoCE (RDMA over Converged Ethernet) adapters. *
  4. * Copyright (C) 2008-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *
  20. * Contact Information:
  21. * linux-drivers@emulex.com
  22. *
  23. * Emulex
  24. * 3333 Susan Street
  25. * Costa Mesa, CA 92626
  26. *******************************************************************/
  27. #include <linux/dma-mapping.h>
  28. #include <rdma/ib_verbs.h>
  29. #include <rdma/ib_user_verbs.h>
  30. #include <rdma/iw_cm.h>
  31. #include <rdma/ib_umem.h>
  32. #include <rdma/ib_addr.h>
  33. #include "ocrdma.h"
  34. #include "ocrdma_hw.h"
  35. #include "ocrdma_verbs.h"
  36. #include "ocrdma_abi.h"
  37. int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
  38. {
  39. if (index > 1)
  40. return -EINVAL;
  41. *pkey = 0xffff;
  42. return 0;
  43. }
  44. int ocrdma_query_gid(struct ib_device *ibdev, u8 port,
  45. int index, union ib_gid *sgid)
  46. {
  47. struct ocrdma_dev *dev;
  48. dev = get_ocrdma_dev(ibdev);
  49. memset(sgid, 0, sizeof(*sgid));
  50. if (index > OCRDMA_MAX_SGID)
  51. return -EINVAL;
  52. memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid));
  53. return 0;
  54. }
  55. int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr)
  56. {
  57. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  58. memset(attr, 0, sizeof *attr);
  59. memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
  60. min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
  61. ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
  62. attr->max_mr_size = dev->attr.max_mr_size;
  63. attr->page_size_cap = 0xffff000;
  64. attr->vendor_id = dev->nic_info.pdev->vendor;
  65. attr->vendor_part_id = dev->nic_info.pdev->device;
  66. attr->hw_ver = dev->asic_id;
  67. attr->max_qp = dev->attr.max_qp;
  68. attr->max_ah = OCRDMA_MAX_AH;
  69. attr->max_qp_wr = dev->attr.max_wqe;
  70. attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
  71. IB_DEVICE_RC_RNR_NAK_GEN |
  72. IB_DEVICE_SHUTDOWN_PORT |
  73. IB_DEVICE_SYS_IMAGE_GUID |
  74. IB_DEVICE_LOCAL_DMA_LKEY |
  75. IB_DEVICE_MEM_MGT_EXTENSIONS;
  76. attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_srq_sge);
  77. attr->max_sge_rd = 0;
  78. attr->max_cq = dev->attr.max_cq;
  79. attr->max_cqe = dev->attr.max_cqe;
  80. attr->max_mr = dev->attr.max_mr;
  81. attr->max_mw = dev->attr.max_mw;
  82. attr->max_pd = dev->attr.max_pd;
  83. attr->atomic_cap = 0;
  84. attr->max_fmr = 0;
  85. attr->max_map_per_fmr = 0;
  86. attr->max_qp_rd_atom =
  87. min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
  88. attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
  89. attr->max_srq = dev->attr.max_srq;
  90. attr->max_srq_sge = dev->attr.max_srq_sge;
  91. attr->max_srq_wr = dev->attr.max_rqe;
  92. attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
  93. attr->max_fast_reg_page_list_len = dev->attr.max_pages_per_frmr;
  94. attr->max_pkeys = 1;
  95. return 0;
  96. }
  97. static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
  98. u8 *ib_speed, u8 *ib_width)
  99. {
  100. int status;
  101. u8 speed;
  102. status = ocrdma_mbx_get_link_speed(dev, &speed);
  103. if (status)
  104. speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
  105. switch (speed) {
  106. case OCRDMA_PHYS_LINK_SPEED_1GBPS:
  107. *ib_speed = IB_SPEED_SDR;
  108. *ib_width = IB_WIDTH_1X;
  109. break;
  110. case OCRDMA_PHYS_LINK_SPEED_10GBPS:
  111. *ib_speed = IB_SPEED_QDR;
  112. *ib_width = IB_WIDTH_1X;
  113. break;
  114. case OCRDMA_PHYS_LINK_SPEED_20GBPS:
  115. *ib_speed = IB_SPEED_DDR;
  116. *ib_width = IB_WIDTH_4X;
  117. break;
  118. case OCRDMA_PHYS_LINK_SPEED_40GBPS:
  119. *ib_speed = IB_SPEED_QDR;
  120. *ib_width = IB_WIDTH_4X;
  121. break;
  122. default:
  123. /* Unsupported */
  124. *ib_speed = IB_SPEED_SDR;
  125. *ib_width = IB_WIDTH_1X;
  126. }
  127. }
  128. int ocrdma_query_port(struct ib_device *ibdev,
  129. u8 port, struct ib_port_attr *props)
  130. {
  131. enum ib_port_state port_state;
  132. struct ocrdma_dev *dev;
  133. struct net_device *netdev;
  134. dev = get_ocrdma_dev(ibdev);
  135. if (port > 1) {
  136. pr_err("%s(%d) invalid_port=0x%x\n", __func__,
  137. dev->id, port);
  138. return -EINVAL;
  139. }
  140. netdev = dev->nic_info.netdev;
  141. if (netif_running(netdev) && netif_oper_up(netdev)) {
  142. port_state = IB_PORT_ACTIVE;
  143. props->phys_state = 5;
  144. } else {
  145. port_state = IB_PORT_DOWN;
  146. props->phys_state = 3;
  147. }
  148. props->max_mtu = IB_MTU_4096;
  149. props->active_mtu = iboe_get_mtu(netdev->mtu);
  150. props->lid = 0;
  151. props->lmc = 0;
  152. props->sm_lid = 0;
  153. props->sm_sl = 0;
  154. props->state = port_state;
  155. props->port_cap_flags =
  156. IB_PORT_CM_SUP |
  157. IB_PORT_REINIT_SUP |
  158. IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP | IB_PORT_IP_BASED_GIDS;
  159. props->gid_tbl_len = OCRDMA_MAX_SGID;
  160. props->pkey_tbl_len = 1;
  161. props->bad_pkey_cntr = 0;
  162. props->qkey_viol_cntr = 0;
  163. get_link_speed_and_width(dev, &props->active_speed,
  164. &props->active_width);
  165. props->max_msg_sz = 0x80000000;
  166. props->max_vl_num = 4;
  167. return 0;
  168. }
  169. int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
  170. struct ib_port_modify *props)
  171. {
  172. struct ocrdma_dev *dev;
  173. dev = get_ocrdma_dev(ibdev);
  174. if (port > 1) {
  175. pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  181. unsigned long len)
  182. {
  183. struct ocrdma_mm *mm;
  184. mm = kzalloc(sizeof(*mm), GFP_KERNEL);
  185. if (mm == NULL)
  186. return -ENOMEM;
  187. mm->key.phy_addr = phy_addr;
  188. mm->key.len = len;
  189. INIT_LIST_HEAD(&mm->entry);
  190. mutex_lock(&uctx->mm_list_lock);
  191. list_add_tail(&mm->entry, &uctx->mm_head);
  192. mutex_unlock(&uctx->mm_list_lock);
  193. return 0;
  194. }
  195. static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  196. unsigned long len)
  197. {
  198. struct ocrdma_mm *mm, *tmp;
  199. mutex_lock(&uctx->mm_list_lock);
  200. list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
  201. if (len != mm->key.len && phy_addr != mm->key.phy_addr)
  202. continue;
  203. list_del(&mm->entry);
  204. kfree(mm);
  205. break;
  206. }
  207. mutex_unlock(&uctx->mm_list_lock);
  208. }
  209. static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  210. unsigned long len)
  211. {
  212. bool found = false;
  213. struct ocrdma_mm *mm;
  214. mutex_lock(&uctx->mm_list_lock);
  215. list_for_each_entry(mm, &uctx->mm_head, entry) {
  216. if (len != mm->key.len && phy_addr != mm->key.phy_addr)
  217. continue;
  218. found = true;
  219. break;
  220. }
  221. mutex_unlock(&uctx->mm_list_lock);
  222. return found;
  223. }
  224. static struct ocrdma_pd *_ocrdma_alloc_pd(struct ocrdma_dev *dev,
  225. struct ocrdma_ucontext *uctx,
  226. struct ib_udata *udata)
  227. {
  228. struct ocrdma_pd *pd = NULL;
  229. int status = 0;
  230. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  231. if (!pd)
  232. return ERR_PTR(-ENOMEM);
  233. if (udata && uctx) {
  234. pd->dpp_enabled =
  235. ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R;
  236. pd->num_dpp_qp =
  237. pd->dpp_enabled ? (dev->nic_info.db_page_size /
  238. dev->attr.wqe_size) : 0;
  239. }
  240. retry:
  241. status = ocrdma_mbx_alloc_pd(dev, pd);
  242. if (status) {
  243. if (pd->dpp_enabled) {
  244. pd->dpp_enabled = false;
  245. pd->num_dpp_qp = 0;
  246. goto retry;
  247. } else {
  248. kfree(pd);
  249. return ERR_PTR(status);
  250. }
  251. }
  252. return pd;
  253. }
  254. static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
  255. struct ocrdma_pd *pd)
  256. {
  257. return (uctx->cntxt_pd == pd ? true : false);
  258. }
  259. static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
  260. struct ocrdma_pd *pd)
  261. {
  262. int status = 0;
  263. status = ocrdma_mbx_dealloc_pd(dev, pd);
  264. kfree(pd);
  265. return status;
  266. }
  267. static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
  268. struct ocrdma_ucontext *uctx,
  269. struct ib_udata *udata)
  270. {
  271. int status = 0;
  272. uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
  273. if (IS_ERR(uctx->cntxt_pd)) {
  274. status = PTR_ERR(uctx->cntxt_pd);
  275. uctx->cntxt_pd = NULL;
  276. goto err;
  277. }
  278. uctx->cntxt_pd->uctx = uctx;
  279. uctx->cntxt_pd->ibpd.device = &dev->ibdev;
  280. err:
  281. return status;
  282. }
  283. static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
  284. {
  285. int status = 0;
  286. struct ocrdma_pd *pd = uctx->cntxt_pd;
  287. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  288. if (uctx->pd_in_use) {
  289. pr_err("%s(%d) Freeing in use pdid=0x%x.\n",
  290. __func__, dev->id, pd->id);
  291. }
  292. uctx->cntxt_pd = NULL;
  293. status = _ocrdma_dealloc_pd(dev, pd);
  294. return status;
  295. }
  296. static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
  297. {
  298. struct ocrdma_pd *pd = NULL;
  299. mutex_lock(&uctx->mm_list_lock);
  300. if (!uctx->pd_in_use) {
  301. uctx->pd_in_use = true;
  302. pd = uctx->cntxt_pd;
  303. }
  304. mutex_unlock(&uctx->mm_list_lock);
  305. return pd;
  306. }
  307. static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
  308. {
  309. mutex_lock(&uctx->mm_list_lock);
  310. uctx->pd_in_use = false;
  311. mutex_unlock(&uctx->mm_list_lock);
  312. }
  313. struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
  314. struct ib_udata *udata)
  315. {
  316. int status;
  317. struct ocrdma_ucontext *ctx;
  318. struct ocrdma_alloc_ucontext_resp resp;
  319. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  320. struct pci_dev *pdev = dev->nic_info.pdev;
  321. u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
  322. if (!udata)
  323. return ERR_PTR(-EFAULT);
  324. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  325. if (!ctx)
  326. return ERR_PTR(-ENOMEM);
  327. INIT_LIST_HEAD(&ctx->mm_head);
  328. mutex_init(&ctx->mm_list_lock);
  329. ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
  330. &ctx->ah_tbl.pa, GFP_KERNEL);
  331. if (!ctx->ah_tbl.va) {
  332. kfree(ctx);
  333. return ERR_PTR(-ENOMEM);
  334. }
  335. memset(ctx->ah_tbl.va, 0, map_len);
  336. ctx->ah_tbl.len = map_len;
  337. memset(&resp, 0, sizeof(resp));
  338. resp.ah_tbl_len = ctx->ah_tbl.len;
  339. resp.ah_tbl_page = virt_to_phys(ctx->ah_tbl.va);
  340. status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
  341. if (status)
  342. goto map_err;
  343. status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
  344. if (status)
  345. goto pd_err;
  346. resp.dev_id = dev->id;
  347. resp.max_inline_data = dev->attr.max_inline_data;
  348. resp.wqe_size = dev->attr.wqe_size;
  349. resp.rqe_size = dev->attr.rqe_size;
  350. resp.dpp_wqe_size = dev->attr.wqe_size;
  351. memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
  352. status = ib_copy_to_udata(udata, &resp, sizeof(resp));
  353. if (status)
  354. goto cpy_err;
  355. return &ctx->ibucontext;
  356. cpy_err:
  357. pd_err:
  358. ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
  359. map_err:
  360. dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
  361. ctx->ah_tbl.pa);
  362. kfree(ctx);
  363. return ERR_PTR(status);
  364. }
  365. int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
  366. {
  367. int status = 0;
  368. struct ocrdma_mm *mm, *tmp;
  369. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
  370. struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
  371. struct pci_dev *pdev = dev->nic_info.pdev;
  372. status = ocrdma_dealloc_ucontext_pd(uctx);
  373. ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
  374. dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
  375. uctx->ah_tbl.pa);
  376. list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
  377. list_del(&mm->entry);
  378. kfree(mm);
  379. }
  380. kfree(uctx);
  381. return status;
  382. }
  383. int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  384. {
  385. struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
  386. struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
  387. unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
  388. u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
  389. unsigned long len = (vma->vm_end - vma->vm_start);
  390. int status = 0;
  391. bool found;
  392. if (vma->vm_start & (PAGE_SIZE - 1))
  393. return -EINVAL;
  394. found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
  395. if (!found)
  396. return -EINVAL;
  397. if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
  398. dev->nic_info.db_total_size)) &&
  399. (len <= dev->nic_info.db_page_size)) {
  400. if (vma->vm_flags & VM_READ)
  401. return -EPERM;
  402. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  403. status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  404. len, vma->vm_page_prot);
  405. } else if (dev->nic_info.dpp_unmapped_len &&
  406. (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
  407. (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
  408. dev->nic_info.dpp_unmapped_len)) &&
  409. (len <= dev->nic_info.dpp_unmapped_len)) {
  410. if (vma->vm_flags & VM_READ)
  411. return -EPERM;
  412. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  413. status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  414. len, vma->vm_page_prot);
  415. } else {
  416. status = remap_pfn_range(vma, vma->vm_start,
  417. vma->vm_pgoff, len, vma->vm_page_prot);
  418. }
  419. return status;
  420. }
  421. static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
  422. struct ib_ucontext *ib_ctx,
  423. struct ib_udata *udata)
  424. {
  425. int status;
  426. u64 db_page_addr;
  427. u64 dpp_page_addr = 0;
  428. u32 db_page_size;
  429. struct ocrdma_alloc_pd_uresp rsp;
  430. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
  431. memset(&rsp, 0, sizeof(rsp));
  432. rsp.id = pd->id;
  433. rsp.dpp_enabled = pd->dpp_enabled;
  434. db_page_addr = ocrdma_get_db_addr(dev, pd->id);
  435. db_page_size = dev->nic_info.db_page_size;
  436. status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
  437. if (status)
  438. return status;
  439. if (pd->dpp_enabled) {
  440. dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
  441. (pd->id * PAGE_SIZE);
  442. status = ocrdma_add_mmap(uctx, dpp_page_addr,
  443. PAGE_SIZE);
  444. if (status)
  445. goto dpp_map_err;
  446. rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
  447. rsp.dpp_page_addr_lo = dpp_page_addr;
  448. }
  449. status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
  450. if (status)
  451. goto ucopy_err;
  452. pd->uctx = uctx;
  453. return 0;
  454. ucopy_err:
  455. if (pd->dpp_enabled)
  456. ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
  457. dpp_map_err:
  458. ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
  459. return status;
  460. }
  461. struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
  462. struct ib_ucontext *context,
  463. struct ib_udata *udata)
  464. {
  465. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  466. struct ocrdma_pd *pd;
  467. struct ocrdma_ucontext *uctx = NULL;
  468. int status;
  469. u8 is_uctx_pd = false;
  470. if (udata && context) {
  471. uctx = get_ocrdma_ucontext(context);
  472. pd = ocrdma_get_ucontext_pd(uctx);
  473. if (pd) {
  474. is_uctx_pd = true;
  475. goto pd_mapping;
  476. }
  477. }
  478. pd = _ocrdma_alloc_pd(dev, uctx, udata);
  479. if (IS_ERR(pd)) {
  480. status = PTR_ERR(pd);
  481. goto exit;
  482. }
  483. pd_mapping:
  484. if (udata && context) {
  485. status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
  486. if (status)
  487. goto err;
  488. }
  489. return &pd->ibpd;
  490. err:
  491. if (is_uctx_pd) {
  492. ocrdma_release_ucontext_pd(uctx);
  493. } else {
  494. status = ocrdma_mbx_dealloc_pd(dev, pd);
  495. kfree(pd);
  496. }
  497. exit:
  498. return ERR_PTR(status);
  499. }
  500. int ocrdma_dealloc_pd(struct ib_pd *ibpd)
  501. {
  502. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  503. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  504. struct ocrdma_ucontext *uctx = NULL;
  505. int status = 0;
  506. u64 usr_db;
  507. uctx = pd->uctx;
  508. if (uctx) {
  509. u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
  510. (pd->id * PAGE_SIZE);
  511. if (pd->dpp_enabled)
  512. ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
  513. usr_db = ocrdma_get_db_addr(dev, pd->id);
  514. ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
  515. if (is_ucontext_pd(uctx, pd)) {
  516. ocrdma_release_ucontext_pd(uctx);
  517. return status;
  518. }
  519. }
  520. status = _ocrdma_dealloc_pd(dev, pd);
  521. return status;
  522. }
  523. static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  524. u32 pdid, int acc, u32 num_pbls, u32 addr_check)
  525. {
  526. int status;
  527. mr->hwmr.fr_mr = 0;
  528. mr->hwmr.local_rd = 1;
  529. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  530. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  531. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  532. mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
  533. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  534. mr->hwmr.num_pbls = num_pbls;
  535. status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
  536. if (status)
  537. return status;
  538. mr->ibmr.lkey = mr->hwmr.lkey;
  539. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  540. mr->ibmr.rkey = mr->hwmr.lkey;
  541. return 0;
  542. }
  543. struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
  544. {
  545. int status;
  546. struct ocrdma_mr *mr;
  547. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  548. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  549. if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
  550. pr_err("%s err, invalid access rights\n", __func__);
  551. return ERR_PTR(-EINVAL);
  552. }
  553. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  554. if (!mr)
  555. return ERR_PTR(-ENOMEM);
  556. status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
  557. OCRDMA_ADDR_CHECK_DISABLE);
  558. if (status) {
  559. kfree(mr);
  560. return ERR_PTR(status);
  561. }
  562. return &mr->ibmr;
  563. }
  564. static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
  565. struct ocrdma_hw_mr *mr)
  566. {
  567. struct pci_dev *pdev = dev->nic_info.pdev;
  568. int i = 0;
  569. if (mr->pbl_table) {
  570. for (i = 0; i < mr->num_pbls; i++) {
  571. if (!mr->pbl_table[i].va)
  572. continue;
  573. dma_free_coherent(&pdev->dev, mr->pbl_size,
  574. mr->pbl_table[i].va,
  575. mr->pbl_table[i].pa);
  576. }
  577. kfree(mr->pbl_table);
  578. mr->pbl_table = NULL;
  579. }
  580. }
  581. static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  582. u32 num_pbes)
  583. {
  584. u32 num_pbls = 0;
  585. u32 idx = 0;
  586. int status = 0;
  587. u32 pbl_size;
  588. do {
  589. pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
  590. if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
  591. status = -EFAULT;
  592. break;
  593. }
  594. num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
  595. num_pbls = num_pbls / (pbl_size / sizeof(u64));
  596. idx++;
  597. } while (num_pbls >= dev->attr.max_num_mr_pbl);
  598. mr->hwmr.num_pbes = num_pbes;
  599. mr->hwmr.num_pbls = num_pbls;
  600. mr->hwmr.pbl_size = pbl_size;
  601. return status;
  602. }
  603. static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
  604. {
  605. int status = 0;
  606. int i;
  607. u32 dma_len = mr->pbl_size;
  608. struct pci_dev *pdev = dev->nic_info.pdev;
  609. void *va;
  610. dma_addr_t pa;
  611. mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
  612. mr->num_pbls, GFP_KERNEL);
  613. if (!mr->pbl_table)
  614. return -ENOMEM;
  615. for (i = 0; i < mr->num_pbls; i++) {
  616. va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
  617. if (!va) {
  618. ocrdma_free_mr_pbl_tbl(dev, mr);
  619. status = -ENOMEM;
  620. break;
  621. }
  622. memset(va, 0, dma_len);
  623. mr->pbl_table[i].va = va;
  624. mr->pbl_table[i].pa = pa;
  625. }
  626. return status;
  627. }
  628. static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  629. u32 num_pbes)
  630. {
  631. struct ocrdma_pbe *pbe;
  632. struct scatterlist *sg;
  633. struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
  634. struct ib_umem *umem = mr->umem;
  635. int shift, pg_cnt, pages, pbe_cnt, entry, total_num_pbes = 0;
  636. if (!mr->hwmr.num_pbes)
  637. return;
  638. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  639. pbe_cnt = 0;
  640. shift = ilog2(umem->page_size);
  641. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  642. pages = sg_dma_len(sg) >> shift;
  643. for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
  644. /* store the page address in pbe */
  645. pbe->pa_lo =
  646. cpu_to_le32(sg_dma_address
  647. (sg) +
  648. (umem->page_size * pg_cnt));
  649. pbe->pa_hi =
  650. cpu_to_le32(upper_32_bits
  651. ((sg_dma_address
  652. (sg) +
  653. umem->page_size * pg_cnt)));
  654. pbe_cnt += 1;
  655. total_num_pbes += 1;
  656. pbe++;
  657. /* if done building pbes, issue the mbx cmd. */
  658. if (total_num_pbes == num_pbes)
  659. return;
  660. /* if the given pbl is full storing the pbes,
  661. * move to next pbl.
  662. */
  663. if (pbe_cnt ==
  664. (mr->hwmr.pbl_size / sizeof(u64))) {
  665. pbl_tbl++;
  666. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  667. pbe_cnt = 0;
  668. }
  669. }
  670. }
  671. }
  672. struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
  673. u64 usr_addr, int acc, struct ib_udata *udata)
  674. {
  675. int status = -ENOMEM;
  676. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  677. struct ocrdma_mr *mr;
  678. struct ocrdma_pd *pd;
  679. u32 num_pbes;
  680. pd = get_ocrdma_pd(ibpd);
  681. if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
  682. return ERR_PTR(-EINVAL);
  683. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  684. if (!mr)
  685. return ERR_PTR(status);
  686. mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
  687. if (IS_ERR(mr->umem)) {
  688. status = -EFAULT;
  689. goto umem_err;
  690. }
  691. num_pbes = ib_umem_page_count(mr->umem);
  692. status = ocrdma_get_pbl_info(dev, mr, num_pbes);
  693. if (status)
  694. goto umem_err;
  695. mr->hwmr.pbe_size = mr->umem->page_size;
  696. mr->hwmr.fbo = mr->umem->offset;
  697. mr->hwmr.va = usr_addr;
  698. mr->hwmr.len = len;
  699. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  700. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  701. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  702. mr->hwmr.local_rd = 1;
  703. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  704. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  705. if (status)
  706. goto umem_err;
  707. build_user_pbes(dev, mr, num_pbes);
  708. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
  709. if (status)
  710. goto mbx_err;
  711. mr->ibmr.lkey = mr->hwmr.lkey;
  712. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  713. mr->ibmr.rkey = mr->hwmr.lkey;
  714. return &mr->ibmr;
  715. mbx_err:
  716. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  717. umem_err:
  718. kfree(mr);
  719. return ERR_PTR(status);
  720. }
  721. int ocrdma_dereg_mr(struct ib_mr *ib_mr)
  722. {
  723. struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
  724. struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
  725. int status;
  726. status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
  727. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  728. /* it could be user registered memory. */
  729. if (mr->umem)
  730. ib_umem_release(mr->umem);
  731. kfree(mr);
  732. /* Don't stop cleanup, in case FW is unresponsive */
  733. if (dev->mqe_ctx.fw_error_state) {
  734. status = 0;
  735. pr_err("%s(%d) fw not responding.\n",
  736. __func__, dev->id);
  737. }
  738. return status;
  739. }
  740. static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
  741. struct ib_udata *udata,
  742. struct ib_ucontext *ib_ctx)
  743. {
  744. int status;
  745. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
  746. struct ocrdma_create_cq_uresp uresp;
  747. memset(&uresp, 0, sizeof(uresp));
  748. uresp.cq_id = cq->id;
  749. uresp.page_size = PAGE_ALIGN(cq->len);
  750. uresp.num_pages = 1;
  751. uresp.max_hw_cqe = cq->max_hw_cqe;
  752. uresp.page_addr[0] = virt_to_phys(cq->va);
  753. uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
  754. uresp.db_page_size = dev->nic_info.db_page_size;
  755. uresp.phase_change = cq->phase_change ? 1 : 0;
  756. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  757. if (status) {
  758. pr_err("%s(%d) copy error cqid=0x%x.\n",
  759. __func__, dev->id, cq->id);
  760. goto err;
  761. }
  762. status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
  763. if (status)
  764. goto err;
  765. status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
  766. if (status) {
  767. ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
  768. goto err;
  769. }
  770. cq->ucontext = uctx;
  771. err:
  772. return status;
  773. }
  774. struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
  775. struct ib_ucontext *ib_ctx,
  776. struct ib_udata *udata)
  777. {
  778. struct ocrdma_cq *cq;
  779. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  780. struct ocrdma_ucontext *uctx = NULL;
  781. u16 pd_id = 0;
  782. int status;
  783. struct ocrdma_create_cq_ureq ureq;
  784. if (udata) {
  785. if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
  786. return ERR_PTR(-EFAULT);
  787. } else
  788. ureq.dpp_cq = 0;
  789. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  790. if (!cq)
  791. return ERR_PTR(-ENOMEM);
  792. spin_lock_init(&cq->cq_lock);
  793. spin_lock_init(&cq->comp_handler_lock);
  794. INIT_LIST_HEAD(&cq->sq_head);
  795. INIT_LIST_HEAD(&cq->rq_head);
  796. cq->first_arm = true;
  797. if (ib_ctx) {
  798. uctx = get_ocrdma_ucontext(ib_ctx);
  799. pd_id = uctx->cntxt_pd->id;
  800. }
  801. status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
  802. if (status) {
  803. kfree(cq);
  804. return ERR_PTR(status);
  805. }
  806. if (ib_ctx) {
  807. status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
  808. if (status)
  809. goto ctx_err;
  810. }
  811. cq->phase = OCRDMA_CQE_VALID;
  812. dev->cq_tbl[cq->id] = cq;
  813. return &cq->ibcq;
  814. ctx_err:
  815. ocrdma_mbx_destroy_cq(dev, cq);
  816. kfree(cq);
  817. return ERR_PTR(status);
  818. }
  819. int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
  820. struct ib_udata *udata)
  821. {
  822. int status = 0;
  823. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  824. if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
  825. status = -EINVAL;
  826. return status;
  827. }
  828. ibcq->cqe = new_cnt;
  829. return status;
  830. }
  831. static void ocrdma_flush_cq(struct ocrdma_cq *cq)
  832. {
  833. int cqe_cnt;
  834. int valid_count = 0;
  835. unsigned long flags;
  836. struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
  837. struct ocrdma_cqe *cqe = NULL;
  838. cqe = cq->va;
  839. cqe_cnt = cq->cqe_cnt;
  840. /* Last irq might have scheduled a polling thread
  841. * sync-up with it before hard flushing.
  842. */
  843. spin_lock_irqsave(&cq->cq_lock, flags);
  844. while (cqe_cnt) {
  845. if (is_cqe_valid(cq, cqe))
  846. valid_count++;
  847. cqe++;
  848. cqe_cnt--;
  849. }
  850. ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
  851. spin_unlock_irqrestore(&cq->cq_lock, flags);
  852. }
  853. int ocrdma_destroy_cq(struct ib_cq *ibcq)
  854. {
  855. int status;
  856. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  857. struct ocrdma_eq *eq = NULL;
  858. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  859. int pdid = 0;
  860. u32 irq, indx;
  861. dev->cq_tbl[cq->id] = NULL;
  862. indx = ocrdma_get_eq_table_index(dev, cq->eqn);
  863. if (indx == -EINVAL)
  864. BUG();
  865. eq = &dev->eq_tbl[indx];
  866. irq = ocrdma_get_irq(dev, eq);
  867. synchronize_irq(irq);
  868. ocrdma_flush_cq(cq);
  869. status = ocrdma_mbx_destroy_cq(dev, cq);
  870. if (cq->ucontext) {
  871. pdid = cq->ucontext->cntxt_pd->id;
  872. ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
  873. PAGE_ALIGN(cq->len));
  874. ocrdma_del_mmap(cq->ucontext,
  875. ocrdma_get_db_addr(dev, pdid),
  876. dev->nic_info.db_page_size);
  877. }
  878. kfree(cq);
  879. return status;
  880. }
  881. static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
  882. {
  883. int status = -EINVAL;
  884. if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
  885. dev->qp_tbl[qp->id] = qp;
  886. status = 0;
  887. }
  888. return status;
  889. }
  890. static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
  891. {
  892. dev->qp_tbl[qp->id] = NULL;
  893. }
  894. static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
  895. struct ib_qp_init_attr *attrs)
  896. {
  897. if ((attrs->qp_type != IB_QPT_GSI) &&
  898. (attrs->qp_type != IB_QPT_RC) &&
  899. (attrs->qp_type != IB_QPT_UC) &&
  900. (attrs->qp_type != IB_QPT_UD)) {
  901. pr_err("%s(%d) unsupported qp type=0x%x requested\n",
  902. __func__, dev->id, attrs->qp_type);
  903. return -EINVAL;
  904. }
  905. /* Skip the check for QP1 to support CM size of 128 */
  906. if ((attrs->qp_type != IB_QPT_GSI) &&
  907. (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
  908. pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
  909. __func__, dev->id, attrs->cap.max_send_wr);
  910. pr_err("%s(%d) supported send_wr=0x%x\n",
  911. __func__, dev->id, dev->attr.max_wqe);
  912. return -EINVAL;
  913. }
  914. if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
  915. pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
  916. __func__, dev->id, attrs->cap.max_recv_wr);
  917. pr_err("%s(%d) supported recv_wr=0x%x\n",
  918. __func__, dev->id, dev->attr.max_rqe);
  919. return -EINVAL;
  920. }
  921. if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
  922. pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
  923. __func__, dev->id, attrs->cap.max_inline_data);
  924. pr_err("%s(%d) supported inline data size=0x%x\n",
  925. __func__, dev->id, dev->attr.max_inline_data);
  926. return -EINVAL;
  927. }
  928. if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
  929. pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
  930. __func__, dev->id, attrs->cap.max_send_sge);
  931. pr_err("%s(%d) supported send_sge=0x%x\n",
  932. __func__, dev->id, dev->attr.max_send_sge);
  933. return -EINVAL;
  934. }
  935. if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
  936. pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
  937. __func__, dev->id, attrs->cap.max_recv_sge);
  938. pr_err("%s(%d) supported recv_sge=0x%x\n",
  939. __func__, dev->id, dev->attr.max_recv_sge);
  940. return -EINVAL;
  941. }
  942. /* unprivileged user space cannot create special QP */
  943. if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
  944. pr_err
  945. ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
  946. __func__, dev->id, attrs->qp_type);
  947. return -EINVAL;
  948. }
  949. /* allow creating only one GSI type of QP */
  950. if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
  951. pr_err("%s(%d) GSI special QPs already created.\n",
  952. __func__, dev->id);
  953. return -EINVAL;
  954. }
  955. /* verify consumer QPs are not trying to use GSI QP's CQ */
  956. if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
  957. if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
  958. (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
  959. pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
  960. __func__, dev->id);
  961. return -EINVAL;
  962. }
  963. }
  964. return 0;
  965. }
  966. static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
  967. struct ib_udata *udata, int dpp_offset,
  968. int dpp_credit_lmt, int srq)
  969. {
  970. int status = 0;
  971. u64 usr_db;
  972. struct ocrdma_create_qp_uresp uresp;
  973. struct ocrdma_dev *dev = qp->dev;
  974. struct ocrdma_pd *pd = qp->pd;
  975. memset(&uresp, 0, sizeof(uresp));
  976. usr_db = dev->nic_info.unmapped_db +
  977. (pd->id * dev->nic_info.db_page_size);
  978. uresp.qp_id = qp->id;
  979. uresp.sq_dbid = qp->sq.dbid;
  980. uresp.num_sq_pages = 1;
  981. uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
  982. uresp.sq_page_addr[0] = virt_to_phys(qp->sq.va);
  983. uresp.num_wqe_allocated = qp->sq.max_cnt;
  984. if (!srq) {
  985. uresp.rq_dbid = qp->rq.dbid;
  986. uresp.num_rq_pages = 1;
  987. uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
  988. uresp.rq_page_addr[0] = virt_to_phys(qp->rq.va);
  989. uresp.num_rqe_allocated = qp->rq.max_cnt;
  990. }
  991. uresp.db_page_addr = usr_db;
  992. uresp.db_page_size = dev->nic_info.db_page_size;
  993. uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
  994. uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
  995. uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
  996. if (qp->dpp_enabled) {
  997. uresp.dpp_credit = dpp_credit_lmt;
  998. uresp.dpp_offset = dpp_offset;
  999. }
  1000. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  1001. if (status) {
  1002. pr_err("%s(%d) user copy error.\n", __func__, dev->id);
  1003. goto err;
  1004. }
  1005. status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
  1006. uresp.sq_page_size);
  1007. if (status)
  1008. goto err;
  1009. if (!srq) {
  1010. status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
  1011. uresp.rq_page_size);
  1012. if (status)
  1013. goto rq_map_err;
  1014. }
  1015. return status;
  1016. rq_map_err:
  1017. ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
  1018. err:
  1019. return status;
  1020. }
  1021. static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
  1022. struct ocrdma_pd *pd)
  1023. {
  1024. if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
  1025. qp->sq_db = dev->nic_info.db +
  1026. (pd->id * dev->nic_info.db_page_size) +
  1027. OCRDMA_DB_GEN2_SQ_OFFSET;
  1028. qp->rq_db = dev->nic_info.db +
  1029. (pd->id * dev->nic_info.db_page_size) +
  1030. OCRDMA_DB_GEN2_RQ_OFFSET;
  1031. } else {
  1032. qp->sq_db = dev->nic_info.db +
  1033. (pd->id * dev->nic_info.db_page_size) +
  1034. OCRDMA_DB_SQ_OFFSET;
  1035. qp->rq_db = dev->nic_info.db +
  1036. (pd->id * dev->nic_info.db_page_size) +
  1037. OCRDMA_DB_RQ_OFFSET;
  1038. }
  1039. }
  1040. static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
  1041. {
  1042. qp->wqe_wr_id_tbl =
  1043. kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
  1044. GFP_KERNEL);
  1045. if (qp->wqe_wr_id_tbl == NULL)
  1046. return -ENOMEM;
  1047. qp->rqe_wr_id_tbl =
  1048. kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
  1049. if (qp->rqe_wr_id_tbl == NULL)
  1050. return -ENOMEM;
  1051. return 0;
  1052. }
  1053. static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
  1054. struct ocrdma_pd *pd,
  1055. struct ib_qp_init_attr *attrs)
  1056. {
  1057. qp->pd = pd;
  1058. spin_lock_init(&qp->q_lock);
  1059. INIT_LIST_HEAD(&qp->sq_entry);
  1060. INIT_LIST_HEAD(&qp->rq_entry);
  1061. qp->qp_type = attrs->qp_type;
  1062. qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
  1063. qp->max_inline_data = attrs->cap.max_inline_data;
  1064. qp->sq.max_sges = attrs->cap.max_send_sge;
  1065. qp->rq.max_sges = attrs->cap.max_recv_sge;
  1066. qp->state = OCRDMA_QPS_RST;
  1067. qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
  1068. }
  1069. static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
  1070. struct ib_qp_init_attr *attrs)
  1071. {
  1072. if (attrs->qp_type == IB_QPT_GSI) {
  1073. dev->gsi_qp_created = 1;
  1074. dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
  1075. dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
  1076. }
  1077. }
  1078. struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
  1079. struct ib_qp_init_attr *attrs,
  1080. struct ib_udata *udata)
  1081. {
  1082. int status;
  1083. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  1084. struct ocrdma_qp *qp;
  1085. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  1086. struct ocrdma_create_qp_ureq ureq;
  1087. u16 dpp_credit_lmt, dpp_offset;
  1088. status = ocrdma_check_qp_params(ibpd, dev, attrs);
  1089. if (status)
  1090. goto gen_err;
  1091. memset(&ureq, 0, sizeof(ureq));
  1092. if (udata) {
  1093. if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
  1094. return ERR_PTR(-EFAULT);
  1095. }
  1096. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  1097. if (!qp) {
  1098. status = -ENOMEM;
  1099. goto gen_err;
  1100. }
  1101. qp->dev = dev;
  1102. ocrdma_set_qp_init_params(qp, pd, attrs);
  1103. if (udata == NULL)
  1104. qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
  1105. OCRDMA_QP_FAST_REG);
  1106. mutex_lock(&dev->dev_lock);
  1107. status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
  1108. ureq.dpp_cq_id,
  1109. &dpp_offset, &dpp_credit_lmt);
  1110. if (status)
  1111. goto mbx_err;
  1112. /* user space QP's wr_id table are managed in library */
  1113. if (udata == NULL) {
  1114. status = ocrdma_alloc_wr_id_tbl(qp);
  1115. if (status)
  1116. goto map_err;
  1117. }
  1118. status = ocrdma_add_qpn_map(dev, qp);
  1119. if (status)
  1120. goto map_err;
  1121. ocrdma_set_qp_db(dev, qp, pd);
  1122. if (udata) {
  1123. status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
  1124. dpp_credit_lmt,
  1125. (attrs->srq != NULL));
  1126. if (status)
  1127. goto cpy_err;
  1128. }
  1129. ocrdma_store_gsi_qp_cq(dev, attrs);
  1130. qp->ibqp.qp_num = qp->id;
  1131. mutex_unlock(&dev->dev_lock);
  1132. return &qp->ibqp;
  1133. cpy_err:
  1134. ocrdma_del_qpn_map(dev, qp);
  1135. map_err:
  1136. ocrdma_mbx_destroy_qp(dev, qp);
  1137. mbx_err:
  1138. mutex_unlock(&dev->dev_lock);
  1139. kfree(qp->wqe_wr_id_tbl);
  1140. kfree(qp->rqe_wr_id_tbl);
  1141. kfree(qp);
  1142. pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
  1143. gen_err:
  1144. return ERR_PTR(status);
  1145. }
  1146. int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1147. int attr_mask)
  1148. {
  1149. int status = 0;
  1150. struct ocrdma_qp *qp;
  1151. struct ocrdma_dev *dev;
  1152. enum ib_qp_state old_qps;
  1153. qp = get_ocrdma_qp(ibqp);
  1154. dev = qp->dev;
  1155. if (attr_mask & IB_QP_STATE)
  1156. status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
  1157. /* if new and previous states are same hw doesn't need to
  1158. * know about it.
  1159. */
  1160. if (status < 0)
  1161. return status;
  1162. status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
  1163. return status;
  1164. }
  1165. int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1166. int attr_mask, struct ib_udata *udata)
  1167. {
  1168. unsigned long flags;
  1169. int status = -EINVAL;
  1170. struct ocrdma_qp *qp;
  1171. struct ocrdma_dev *dev;
  1172. enum ib_qp_state old_qps, new_qps;
  1173. qp = get_ocrdma_qp(ibqp);
  1174. dev = qp->dev;
  1175. /* syncronize with multiple context trying to change, retrive qps */
  1176. mutex_lock(&dev->dev_lock);
  1177. /* syncronize with wqe, rqe posting and cqe processing contexts */
  1178. spin_lock_irqsave(&qp->q_lock, flags);
  1179. old_qps = get_ibqp_state(qp->state);
  1180. if (attr_mask & IB_QP_STATE)
  1181. new_qps = attr->qp_state;
  1182. else
  1183. new_qps = old_qps;
  1184. spin_unlock_irqrestore(&qp->q_lock, flags);
  1185. if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask,
  1186. IB_LINK_LAYER_ETHERNET)) {
  1187. pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
  1188. "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
  1189. __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
  1190. old_qps, new_qps);
  1191. goto param_err;
  1192. }
  1193. status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
  1194. if (status > 0)
  1195. status = 0;
  1196. param_err:
  1197. mutex_unlock(&dev->dev_lock);
  1198. return status;
  1199. }
  1200. static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
  1201. {
  1202. switch (mtu) {
  1203. case 256:
  1204. return IB_MTU_256;
  1205. case 512:
  1206. return IB_MTU_512;
  1207. case 1024:
  1208. return IB_MTU_1024;
  1209. case 2048:
  1210. return IB_MTU_2048;
  1211. case 4096:
  1212. return IB_MTU_4096;
  1213. default:
  1214. return IB_MTU_1024;
  1215. }
  1216. }
  1217. static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
  1218. {
  1219. int ib_qp_acc_flags = 0;
  1220. if (qp_cap_flags & OCRDMA_QP_INB_WR)
  1221. ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
  1222. if (qp_cap_flags & OCRDMA_QP_INB_RD)
  1223. ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
  1224. return ib_qp_acc_flags;
  1225. }
  1226. int ocrdma_query_qp(struct ib_qp *ibqp,
  1227. struct ib_qp_attr *qp_attr,
  1228. int attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1229. {
  1230. int status;
  1231. u32 qp_state;
  1232. struct ocrdma_qp_params params;
  1233. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1234. struct ocrdma_dev *dev = qp->dev;
  1235. memset(&params, 0, sizeof(params));
  1236. mutex_lock(&dev->dev_lock);
  1237. status = ocrdma_mbx_query_qp(dev, qp, &params);
  1238. mutex_unlock(&dev->dev_lock);
  1239. if (status)
  1240. goto mbx_err;
  1241. qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
  1242. qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
  1243. qp_attr->path_mtu =
  1244. ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
  1245. OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
  1246. OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
  1247. qp_attr->path_mig_state = IB_MIG_MIGRATED;
  1248. qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
  1249. qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
  1250. qp_attr->dest_qp_num =
  1251. params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
  1252. qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
  1253. qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
  1254. qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
  1255. qp_attr->cap.max_send_sge = qp->sq.max_sges;
  1256. qp_attr->cap.max_recv_sge = qp->rq.max_sges;
  1257. qp_attr->cap.max_inline_data = qp->max_inline_data;
  1258. qp_init_attr->cap = qp_attr->cap;
  1259. memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
  1260. sizeof(params.dgid));
  1261. qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
  1262. OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
  1263. qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
  1264. qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
  1265. OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
  1266. OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
  1267. qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
  1268. OCRDMA_QP_PARAMS_TCLASS_MASK) >>
  1269. OCRDMA_QP_PARAMS_TCLASS_SHIFT;
  1270. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1271. qp_attr->ah_attr.port_num = 1;
  1272. qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
  1273. OCRDMA_QP_PARAMS_SL_MASK) >>
  1274. OCRDMA_QP_PARAMS_SL_SHIFT;
  1275. qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
  1276. OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
  1277. OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
  1278. qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
  1279. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
  1280. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
  1281. qp_attr->retry_cnt =
  1282. (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
  1283. OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
  1284. qp_attr->min_rnr_timer = 0;
  1285. qp_attr->pkey_index = 0;
  1286. qp_attr->port_num = 1;
  1287. qp_attr->ah_attr.src_path_bits = 0;
  1288. qp_attr->ah_attr.static_rate = 0;
  1289. qp_attr->alt_pkey_index = 0;
  1290. qp_attr->alt_port_num = 0;
  1291. qp_attr->alt_timeout = 0;
  1292. memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
  1293. qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
  1294. OCRDMA_QP_PARAMS_STATE_SHIFT;
  1295. qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
  1296. qp_attr->max_dest_rd_atomic =
  1297. params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
  1298. qp_attr->max_rd_atomic =
  1299. params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
  1300. qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
  1301. OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
  1302. mbx_err:
  1303. return status;
  1304. }
  1305. static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
  1306. {
  1307. int i = idx / 32;
  1308. unsigned int mask = (1 << (idx % 32));
  1309. if (srq->idx_bit_fields[i] & mask)
  1310. srq->idx_bit_fields[i] &= ~mask;
  1311. else
  1312. srq->idx_bit_fields[i] |= mask;
  1313. }
  1314. static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
  1315. {
  1316. return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
  1317. }
  1318. static int is_hw_sq_empty(struct ocrdma_qp *qp)
  1319. {
  1320. return (qp->sq.tail == qp->sq.head);
  1321. }
  1322. static int is_hw_rq_empty(struct ocrdma_qp *qp)
  1323. {
  1324. return (qp->rq.tail == qp->rq.head);
  1325. }
  1326. static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
  1327. {
  1328. return q->va + (q->head * q->entry_size);
  1329. }
  1330. static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
  1331. u32 idx)
  1332. {
  1333. return q->va + (idx * q->entry_size);
  1334. }
  1335. static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
  1336. {
  1337. q->head = (q->head + 1) & q->max_wqe_idx;
  1338. }
  1339. static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
  1340. {
  1341. q->tail = (q->tail + 1) & q->max_wqe_idx;
  1342. }
  1343. /* discard the cqe for a given QP */
  1344. static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
  1345. {
  1346. unsigned long cq_flags;
  1347. unsigned long flags;
  1348. int discard_cnt = 0;
  1349. u32 cur_getp, stop_getp;
  1350. struct ocrdma_cqe *cqe;
  1351. u32 qpn = 0, wqe_idx = 0;
  1352. spin_lock_irqsave(&cq->cq_lock, cq_flags);
  1353. /* traverse through the CQEs in the hw CQ,
  1354. * find the matching CQE for a given qp,
  1355. * mark the matching one discarded by clearing qpn.
  1356. * ring the doorbell in the poll_cq() as
  1357. * we don't complete out of order cqe.
  1358. */
  1359. cur_getp = cq->getp;
  1360. /* find upto when do we reap the cq. */
  1361. stop_getp = cur_getp;
  1362. do {
  1363. if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
  1364. break;
  1365. cqe = cq->va + cur_getp;
  1366. /* if (a) done reaping whole hw cq, or
  1367. * (b) qp_xq becomes empty.
  1368. * then exit
  1369. */
  1370. qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
  1371. /* if previously discarded cqe found, skip that too. */
  1372. /* check for matching qp */
  1373. if (qpn == 0 || qpn != qp->id)
  1374. goto skip_cqe;
  1375. if (is_cqe_for_sq(cqe)) {
  1376. ocrdma_hwq_inc_tail(&qp->sq);
  1377. } else {
  1378. if (qp->srq) {
  1379. wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
  1380. OCRDMA_CQE_BUFTAG_SHIFT) &
  1381. qp->srq->rq.max_wqe_idx;
  1382. if (wqe_idx < 1)
  1383. BUG();
  1384. spin_lock_irqsave(&qp->srq->q_lock, flags);
  1385. ocrdma_hwq_inc_tail(&qp->srq->rq);
  1386. ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
  1387. spin_unlock_irqrestore(&qp->srq->q_lock, flags);
  1388. } else {
  1389. ocrdma_hwq_inc_tail(&qp->rq);
  1390. }
  1391. }
  1392. /* mark cqe discarded so that it is not picked up later
  1393. * in the poll_cq().
  1394. */
  1395. discard_cnt += 1;
  1396. cqe->cmn.qpn = 0;
  1397. skip_cqe:
  1398. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  1399. } while (cur_getp != stop_getp);
  1400. spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
  1401. }
  1402. void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
  1403. {
  1404. int found = false;
  1405. unsigned long flags;
  1406. struct ocrdma_dev *dev = qp->dev;
  1407. /* sync with any active CQ poll */
  1408. spin_lock_irqsave(&dev->flush_q_lock, flags);
  1409. found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
  1410. if (found)
  1411. list_del(&qp->sq_entry);
  1412. if (!qp->srq) {
  1413. found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
  1414. if (found)
  1415. list_del(&qp->rq_entry);
  1416. }
  1417. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  1418. }
  1419. int ocrdma_destroy_qp(struct ib_qp *ibqp)
  1420. {
  1421. int status;
  1422. struct ocrdma_pd *pd;
  1423. struct ocrdma_qp *qp;
  1424. struct ocrdma_dev *dev;
  1425. struct ib_qp_attr attrs;
  1426. int attr_mask = IB_QP_STATE;
  1427. unsigned long flags;
  1428. qp = get_ocrdma_qp(ibqp);
  1429. dev = qp->dev;
  1430. attrs.qp_state = IB_QPS_ERR;
  1431. pd = qp->pd;
  1432. /* change the QP state to ERROR */
  1433. _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
  1434. /* ensure that CQEs for newly created QP (whose id may be same with
  1435. * one which just getting destroyed are same), dont get
  1436. * discarded until the old CQEs are discarded.
  1437. */
  1438. mutex_lock(&dev->dev_lock);
  1439. status = ocrdma_mbx_destroy_qp(dev, qp);
  1440. /*
  1441. * acquire CQ lock while destroy is in progress, in order to
  1442. * protect against proessing in-flight CQEs for this QP.
  1443. */
  1444. spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
  1445. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1446. spin_lock(&qp->rq_cq->cq_lock);
  1447. ocrdma_del_qpn_map(dev, qp);
  1448. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1449. spin_unlock(&qp->rq_cq->cq_lock);
  1450. spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
  1451. if (!pd->uctx) {
  1452. ocrdma_discard_cqes(qp, qp->sq_cq);
  1453. ocrdma_discard_cqes(qp, qp->rq_cq);
  1454. }
  1455. mutex_unlock(&dev->dev_lock);
  1456. if (pd->uctx) {
  1457. ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
  1458. PAGE_ALIGN(qp->sq.len));
  1459. if (!qp->srq)
  1460. ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
  1461. PAGE_ALIGN(qp->rq.len));
  1462. }
  1463. ocrdma_del_flush_qp(qp);
  1464. kfree(qp->wqe_wr_id_tbl);
  1465. kfree(qp->rqe_wr_id_tbl);
  1466. kfree(qp);
  1467. return status;
  1468. }
  1469. static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
  1470. struct ib_udata *udata)
  1471. {
  1472. int status;
  1473. struct ocrdma_create_srq_uresp uresp;
  1474. memset(&uresp, 0, sizeof(uresp));
  1475. uresp.rq_dbid = srq->rq.dbid;
  1476. uresp.num_rq_pages = 1;
  1477. uresp.rq_page_addr[0] = virt_to_phys(srq->rq.va);
  1478. uresp.rq_page_size = srq->rq.len;
  1479. uresp.db_page_addr = dev->nic_info.unmapped_db +
  1480. (srq->pd->id * dev->nic_info.db_page_size);
  1481. uresp.db_page_size = dev->nic_info.db_page_size;
  1482. uresp.num_rqe_allocated = srq->rq.max_cnt;
  1483. if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
  1484. uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
  1485. uresp.db_shift = 24;
  1486. } else {
  1487. uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
  1488. uresp.db_shift = 16;
  1489. }
  1490. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  1491. if (status)
  1492. return status;
  1493. status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
  1494. uresp.rq_page_size);
  1495. if (status)
  1496. return status;
  1497. return status;
  1498. }
  1499. struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
  1500. struct ib_srq_init_attr *init_attr,
  1501. struct ib_udata *udata)
  1502. {
  1503. int status = -ENOMEM;
  1504. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  1505. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  1506. struct ocrdma_srq *srq;
  1507. if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
  1508. return ERR_PTR(-EINVAL);
  1509. if (init_attr->attr.max_wr > dev->attr.max_rqe)
  1510. return ERR_PTR(-EINVAL);
  1511. srq = kzalloc(sizeof(*srq), GFP_KERNEL);
  1512. if (!srq)
  1513. return ERR_PTR(status);
  1514. spin_lock_init(&srq->q_lock);
  1515. srq->pd = pd;
  1516. srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
  1517. status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
  1518. if (status)
  1519. goto err;
  1520. if (udata == NULL) {
  1521. srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
  1522. GFP_KERNEL);
  1523. if (srq->rqe_wr_id_tbl == NULL)
  1524. goto arm_err;
  1525. srq->bit_fields_len = (srq->rq.max_cnt / 32) +
  1526. (srq->rq.max_cnt % 32 ? 1 : 0);
  1527. srq->idx_bit_fields =
  1528. kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
  1529. if (srq->idx_bit_fields == NULL)
  1530. goto arm_err;
  1531. memset(srq->idx_bit_fields, 0xff,
  1532. srq->bit_fields_len * sizeof(u32));
  1533. }
  1534. if (init_attr->attr.srq_limit) {
  1535. status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
  1536. if (status)
  1537. goto arm_err;
  1538. }
  1539. if (udata) {
  1540. status = ocrdma_copy_srq_uresp(dev, srq, udata);
  1541. if (status)
  1542. goto arm_err;
  1543. }
  1544. return &srq->ibsrq;
  1545. arm_err:
  1546. ocrdma_mbx_destroy_srq(dev, srq);
  1547. err:
  1548. kfree(srq->rqe_wr_id_tbl);
  1549. kfree(srq->idx_bit_fields);
  1550. kfree(srq);
  1551. return ERR_PTR(status);
  1552. }
  1553. int ocrdma_modify_srq(struct ib_srq *ibsrq,
  1554. struct ib_srq_attr *srq_attr,
  1555. enum ib_srq_attr_mask srq_attr_mask,
  1556. struct ib_udata *udata)
  1557. {
  1558. int status = 0;
  1559. struct ocrdma_srq *srq;
  1560. srq = get_ocrdma_srq(ibsrq);
  1561. if (srq_attr_mask & IB_SRQ_MAX_WR)
  1562. status = -EINVAL;
  1563. else
  1564. status = ocrdma_mbx_modify_srq(srq, srq_attr);
  1565. return status;
  1566. }
  1567. int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  1568. {
  1569. int status;
  1570. struct ocrdma_srq *srq;
  1571. srq = get_ocrdma_srq(ibsrq);
  1572. status = ocrdma_mbx_query_srq(srq, srq_attr);
  1573. return status;
  1574. }
  1575. int ocrdma_destroy_srq(struct ib_srq *ibsrq)
  1576. {
  1577. int status;
  1578. struct ocrdma_srq *srq;
  1579. struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
  1580. srq = get_ocrdma_srq(ibsrq);
  1581. status = ocrdma_mbx_destroy_srq(dev, srq);
  1582. if (srq->pd->uctx)
  1583. ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
  1584. PAGE_ALIGN(srq->rq.len));
  1585. kfree(srq->idx_bit_fields);
  1586. kfree(srq->rqe_wr_id_tbl);
  1587. kfree(srq);
  1588. return status;
  1589. }
  1590. /* unprivileged verbs and their support functions. */
  1591. static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
  1592. struct ocrdma_hdr_wqe *hdr,
  1593. struct ib_send_wr *wr)
  1594. {
  1595. struct ocrdma_ewqe_ud_hdr *ud_hdr =
  1596. (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
  1597. struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
  1598. ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
  1599. if (qp->qp_type == IB_QPT_GSI)
  1600. ud_hdr->qkey = qp->qkey;
  1601. else
  1602. ud_hdr->qkey = wr->wr.ud.remote_qkey;
  1603. ud_hdr->rsvd_ahid = ah->id;
  1604. }
  1605. static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
  1606. struct ocrdma_sge *sge, int num_sge,
  1607. struct ib_sge *sg_list)
  1608. {
  1609. int i;
  1610. for (i = 0; i < num_sge; i++) {
  1611. sge[i].lrkey = sg_list[i].lkey;
  1612. sge[i].addr_lo = sg_list[i].addr;
  1613. sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
  1614. sge[i].len = sg_list[i].length;
  1615. hdr->total_len += sg_list[i].length;
  1616. }
  1617. if (num_sge == 0)
  1618. memset(sge, 0, sizeof(*sge));
  1619. }
  1620. static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
  1621. {
  1622. uint32_t total_len = 0, i;
  1623. for (i = 0; i < num_sge; i++)
  1624. total_len += sg_list[i].length;
  1625. return total_len;
  1626. }
  1627. static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
  1628. struct ocrdma_hdr_wqe *hdr,
  1629. struct ocrdma_sge *sge,
  1630. struct ib_send_wr *wr, u32 wqe_size)
  1631. {
  1632. int i;
  1633. char *dpp_addr;
  1634. if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
  1635. hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
  1636. if (unlikely(hdr->total_len > qp->max_inline_data)) {
  1637. pr_err("%s() supported_len=0x%x,\n"
  1638. " unsupported len req=0x%x\n", __func__,
  1639. qp->max_inline_data, hdr->total_len);
  1640. return -EINVAL;
  1641. }
  1642. dpp_addr = (char *)sge;
  1643. for (i = 0; i < wr->num_sge; i++) {
  1644. memcpy(dpp_addr,
  1645. (void *)(unsigned long)wr->sg_list[i].addr,
  1646. wr->sg_list[i].length);
  1647. dpp_addr += wr->sg_list[i].length;
  1648. }
  1649. wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
  1650. if (0 == hdr->total_len)
  1651. wqe_size += sizeof(struct ocrdma_sge);
  1652. hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
  1653. } else {
  1654. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1655. if (wr->num_sge)
  1656. wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
  1657. else
  1658. wqe_size += sizeof(struct ocrdma_sge);
  1659. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1660. }
  1661. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1662. return 0;
  1663. }
  1664. static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1665. struct ib_send_wr *wr)
  1666. {
  1667. int status;
  1668. struct ocrdma_sge *sge;
  1669. u32 wqe_size = sizeof(*hdr);
  1670. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  1671. ocrdma_build_ud_hdr(qp, hdr, wr);
  1672. sge = (struct ocrdma_sge *)(hdr + 2);
  1673. wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
  1674. } else {
  1675. sge = (struct ocrdma_sge *)(hdr + 1);
  1676. }
  1677. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1678. return status;
  1679. }
  1680. static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1681. struct ib_send_wr *wr)
  1682. {
  1683. int status;
  1684. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1685. struct ocrdma_sge *sge = ext_rw + 1;
  1686. u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
  1687. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1688. if (status)
  1689. return status;
  1690. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1691. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1692. ext_rw->lrkey = wr->wr.rdma.rkey;
  1693. ext_rw->len = hdr->total_len;
  1694. return 0;
  1695. }
  1696. static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1697. struct ib_send_wr *wr)
  1698. {
  1699. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1700. struct ocrdma_sge *sge = ext_rw + 1;
  1701. u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
  1702. sizeof(struct ocrdma_hdr_wqe);
  1703. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1704. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1705. hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
  1706. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1707. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1708. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1709. ext_rw->lrkey = wr->wr.rdma.rkey;
  1710. ext_rw->len = hdr->total_len;
  1711. }
  1712. static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
  1713. struct ocrdma_hw_mr *hwmr)
  1714. {
  1715. int i;
  1716. u64 buf_addr = 0;
  1717. int num_pbes;
  1718. struct ocrdma_pbe *pbe;
  1719. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1720. num_pbes = 0;
  1721. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  1722. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  1723. /* number of pbes can be more for one OS buf, when
  1724. * buffers are of different sizes.
  1725. * split the ib_buf to one or more pbes.
  1726. */
  1727. buf_addr = wr->wr.fast_reg.page_list->page_list[i];
  1728. pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  1729. pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
  1730. num_pbes += 1;
  1731. pbe++;
  1732. /* if the pbl is full storing the pbes,
  1733. * move to next pbl.
  1734. */
  1735. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  1736. pbl_tbl++;
  1737. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1738. }
  1739. }
  1740. return;
  1741. }
  1742. static int get_encoded_page_size(int pg_sz)
  1743. {
  1744. /* Max size is 256M 4096 << 16 */
  1745. int i = 0;
  1746. for (; i < 17; i++)
  1747. if (pg_sz == (4096 << i))
  1748. break;
  1749. return i;
  1750. }
  1751. static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1752. struct ib_send_wr *wr)
  1753. {
  1754. u64 fbo;
  1755. struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
  1756. struct ocrdma_mr *mr;
  1757. u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
  1758. wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
  1759. if (wr->wr.fast_reg.page_list_len > qp->dev->attr.max_pages_per_frmr)
  1760. return -EINVAL;
  1761. hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
  1762. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1763. if (wr->wr.fast_reg.page_list_len == 0)
  1764. BUG();
  1765. if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
  1766. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
  1767. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
  1768. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
  1769. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
  1770. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
  1771. hdr->lkey = wr->wr.fast_reg.rkey;
  1772. hdr->total_len = wr->wr.fast_reg.length;
  1773. fbo = wr->wr.fast_reg.iova_start -
  1774. (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
  1775. fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
  1776. fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
  1777. fast_reg->fbo_hi = upper_32_bits(fbo);
  1778. fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
  1779. fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
  1780. fast_reg->size_sge =
  1781. get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
  1782. mr = (struct ocrdma_mr *) (unsigned long)
  1783. qp->dev->stag_arr[(hdr->lkey >> 8) & (OCRDMA_MAX_STAG - 1)];
  1784. build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
  1785. return 0;
  1786. }
  1787. static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
  1788. {
  1789. u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
  1790. iowrite32(val, qp->sq_db);
  1791. }
  1792. int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1793. struct ib_send_wr **bad_wr)
  1794. {
  1795. int status = 0;
  1796. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1797. struct ocrdma_hdr_wqe *hdr;
  1798. unsigned long flags;
  1799. spin_lock_irqsave(&qp->q_lock, flags);
  1800. if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
  1801. spin_unlock_irqrestore(&qp->q_lock, flags);
  1802. *bad_wr = wr;
  1803. return -EINVAL;
  1804. }
  1805. while (wr) {
  1806. if (qp->qp_type == IB_QPT_UD &&
  1807. (wr->opcode != IB_WR_SEND &&
  1808. wr->opcode != IB_WR_SEND_WITH_IMM)) {
  1809. *bad_wr = wr;
  1810. status = -EINVAL;
  1811. break;
  1812. }
  1813. if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
  1814. wr->num_sge > qp->sq.max_sges) {
  1815. *bad_wr = wr;
  1816. status = -ENOMEM;
  1817. break;
  1818. }
  1819. hdr = ocrdma_hwq_head(&qp->sq);
  1820. hdr->cw = 0;
  1821. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1822. hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1823. if (wr->send_flags & IB_SEND_FENCE)
  1824. hdr->cw |=
  1825. (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
  1826. if (wr->send_flags & IB_SEND_SOLICITED)
  1827. hdr->cw |=
  1828. (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
  1829. hdr->total_len = 0;
  1830. switch (wr->opcode) {
  1831. case IB_WR_SEND_WITH_IMM:
  1832. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1833. hdr->immdt = ntohl(wr->ex.imm_data);
  1834. case IB_WR_SEND:
  1835. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1836. ocrdma_build_send(qp, hdr, wr);
  1837. break;
  1838. case IB_WR_SEND_WITH_INV:
  1839. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1840. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1841. hdr->lkey = wr->ex.invalidate_rkey;
  1842. status = ocrdma_build_send(qp, hdr, wr);
  1843. break;
  1844. case IB_WR_RDMA_WRITE_WITH_IMM:
  1845. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1846. hdr->immdt = ntohl(wr->ex.imm_data);
  1847. case IB_WR_RDMA_WRITE:
  1848. hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
  1849. status = ocrdma_build_write(qp, hdr, wr);
  1850. break;
  1851. case IB_WR_RDMA_READ_WITH_INV:
  1852. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1853. case IB_WR_RDMA_READ:
  1854. ocrdma_build_read(qp, hdr, wr);
  1855. break;
  1856. case IB_WR_LOCAL_INV:
  1857. hdr->cw |=
  1858. (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
  1859. hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
  1860. sizeof(struct ocrdma_sge)) /
  1861. OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
  1862. hdr->lkey = wr->ex.invalidate_rkey;
  1863. break;
  1864. case IB_WR_FAST_REG_MR:
  1865. status = ocrdma_build_fr(qp, hdr, wr);
  1866. break;
  1867. default:
  1868. status = -EINVAL;
  1869. break;
  1870. }
  1871. if (status) {
  1872. *bad_wr = wr;
  1873. break;
  1874. }
  1875. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1876. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
  1877. else
  1878. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
  1879. qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
  1880. ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
  1881. OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
  1882. /* make sure wqe is written before adapter can access it */
  1883. wmb();
  1884. /* inform hw to start processing it */
  1885. ocrdma_ring_sq_db(qp);
  1886. /* update pointer, counter for next wr */
  1887. ocrdma_hwq_inc_head(&qp->sq);
  1888. wr = wr->next;
  1889. }
  1890. spin_unlock_irqrestore(&qp->q_lock, flags);
  1891. return status;
  1892. }
  1893. static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
  1894. {
  1895. u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
  1896. iowrite32(val, qp->rq_db);
  1897. }
  1898. static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
  1899. u16 tag)
  1900. {
  1901. u32 wqe_size = 0;
  1902. struct ocrdma_sge *sge;
  1903. if (wr->num_sge)
  1904. wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
  1905. else
  1906. wqe_size = sizeof(*sge) + sizeof(*rqe);
  1907. rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
  1908. OCRDMA_WQE_SIZE_SHIFT);
  1909. rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1910. rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1911. rqe->total_len = 0;
  1912. rqe->rsvd_tag = tag;
  1913. sge = (struct ocrdma_sge *)(rqe + 1);
  1914. ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
  1915. ocrdma_cpu_to_le32(rqe, wqe_size);
  1916. }
  1917. int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1918. struct ib_recv_wr **bad_wr)
  1919. {
  1920. int status = 0;
  1921. unsigned long flags;
  1922. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1923. struct ocrdma_hdr_wqe *rqe;
  1924. spin_lock_irqsave(&qp->q_lock, flags);
  1925. if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
  1926. spin_unlock_irqrestore(&qp->q_lock, flags);
  1927. *bad_wr = wr;
  1928. return -EINVAL;
  1929. }
  1930. while (wr) {
  1931. if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
  1932. wr->num_sge > qp->rq.max_sges) {
  1933. *bad_wr = wr;
  1934. status = -ENOMEM;
  1935. break;
  1936. }
  1937. rqe = ocrdma_hwq_head(&qp->rq);
  1938. ocrdma_build_rqe(rqe, wr, 0);
  1939. qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
  1940. /* make sure rqe is written before adapter can access it */
  1941. wmb();
  1942. /* inform hw to start processing it */
  1943. ocrdma_ring_rq_db(qp);
  1944. /* update pointer, counter for next wr */
  1945. ocrdma_hwq_inc_head(&qp->rq);
  1946. wr = wr->next;
  1947. }
  1948. spin_unlock_irqrestore(&qp->q_lock, flags);
  1949. return status;
  1950. }
  1951. /* cqe for srq's rqe can potentially arrive out of order.
  1952. * index gives the entry in the shadow table where to store
  1953. * the wr_id. tag/index is returned in cqe to reference back
  1954. * for a given rqe.
  1955. */
  1956. static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
  1957. {
  1958. int row = 0;
  1959. int indx = 0;
  1960. for (row = 0; row < srq->bit_fields_len; row++) {
  1961. if (srq->idx_bit_fields[row]) {
  1962. indx = ffs(srq->idx_bit_fields[row]);
  1963. indx = (row * 32) + (indx - 1);
  1964. if (indx >= srq->rq.max_cnt)
  1965. BUG();
  1966. ocrdma_srq_toggle_bit(srq, indx);
  1967. break;
  1968. }
  1969. }
  1970. if (row == srq->bit_fields_len)
  1971. BUG();
  1972. return indx + 1; /* Use from index 1 */
  1973. }
  1974. static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
  1975. {
  1976. u32 val = srq->rq.dbid | (1 << 16);
  1977. iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
  1978. }
  1979. int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1980. struct ib_recv_wr **bad_wr)
  1981. {
  1982. int status = 0;
  1983. unsigned long flags;
  1984. struct ocrdma_srq *srq;
  1985. struct ocrdma_hdr_wqe *rqe;
  1986. u16 tag;
  1987. srq = get_ocrdma_srq(ibsrq);
  1988. spin_lock_irqsave(&srq->q_lock, flags);
  1989. while (wr) {
  1990. if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
  1991. wr->num_sge > srq->rq.max_sges) {
  1992. status = -ENOMEM;
  1993. *bad_wr = wr;
  1994. break;
  1995. }
  1996. tag = ocrdma_srq_get_idx(srq);
  1997. rqe = ocrdma_hwq_head(&srq->rq);
  1998. ocrdma_build_rqe(rqe, wr, tag);
  1999. srq->rqe_wr_id_tbl[tag] = wr->wr_id;
  2000. /* make sure rqe is written before adapter can perform DMA */
  2001. wmb();
  2002. /* inform hw to start processing it */
  2003. ocrdma_ring_srq_db(srq);
  2004. /* update pointer, counter for next wr */
  2005. ocrdma_hwq_inc_head(&srq->rq);
  2006. wr = wr->next;
  2007. }
  2008. spin_unlock_irqrestore(&srq->q_lock, flags);
  2009. return status;
  2010. }
  2011. static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
  2012. {
  2013. enum ib_wc_status ibwc_status;
  2014. switch (status) {
  2015. case OCRDMA_CQE_GENERAL_ERR:
  2016. ibwc_status = IB_WC_GENERAL_ERR;
  2017. break;
  2018. case OCRDMA_CQE_LOC_LEN_ERR:
  2019. ibwc_status = IB_WC_LOC_LEN_ERR;
  2020. break;
  2021. case OCRDMA_CQE_LOC_QP_OP_ERR:
  2022. ibwc_status = IB_WC_LOC_QP_OP_ERR;
  2023. break;
  2024. case OCRDMA_CQE_LOC_EEC_OP_ERR:
  2025. ibwc_status = IB_WC_LOC_EEC_OP_ERR;
  2026. break;
  2027. case OCRDMA_CQE_LOC_PROT_ERR:
  2028. ibwc_status = IB_WC_LOC_PROT_ERR;
  2029. break;
  2030. case OCRDMA_CQE_WR_FLUSH_ERR:
  2031. ibwc_status = IB_WC_WR_FLUSH_ERR;
  2032. break;
  2033. case OCRDMA_CQE_MW_BIND_ERR:
  2034. ibwc_status = IB_WC_MW_BIND_ERR;
  2035. break;
  2036. case OCRDMA_CQE_BAD_RESP_ERR:
  2037. ibwc_status = IB_WC_BAD_RESP_ERR;
  2038. break;
  2039. case OCRDMA_CQE_LOC_ACCESS_ERR:
  2040. ibwc_status = IB_WC_LOC_ACCESS_ERR;
  2041. break;
  2042. case OCRDMA_CQE_REM_INV_REQ_ERR:
  2043. ibwc_status = IB_WC_REM_INV_REQ_ERR;
  2044. break;
  2045. case OCRDMA_CQE_REM_ACCESS_ERR:
  2046. ibwc_status = IB_WC_REM_ACCESS_ERR;
  2047. break;
  2048. case OCRDMA_CQE_REM_OP_ERR:
  2049. ibwc_status = IB_WC_REM_OP_ERR;
  2050. break;
  2051. case OCRDMA_CQE_RETRY_EXC_ERR:
  2052. ibwc_status = IB_WC_RETRY_EXC_ERR;
  2053. break;
  2054. case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
  2055. ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
  2056. break;
  2057. case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
  2058. ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
  2059. break;
  2060. case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
  2061. ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
  2062. break;
  2063. case OCRDMA_CQE_REM_ABORT_ERR:
  2064. ibwc_status = IB_WC_REM_ABORT_ERR;
  2065. break;
  2066. case OCRDMA_CQE_INV_EECN_ERR:
  2067. ibwc_status = IB_WC_INV_EECN_ERR;
  2068. break;
  2069. case OCRDMA_CQE_INV_EEC_STATE_ERR:
  2070. ibwc_status = IB_WC_INV_EEC_STATE_ERR;
  2071. break;
  2072. case OCRDMA_CQE_FATAL_ERR:
  2073. ibwc_status = IB_WC_FATAL_ERR;
  2074. break;
  2075. case OCRDMA_CQE_RESP_TIMEOUT_ERR:
  2076. ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
  2077. break;
  2078. default:
  2079. ibwc_status = IB_WC_GENERAL_ERR;
  2080. break;
  2081. }
  2082. return ibwc_status;
  2083. }
  2084. static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
  2085. u32 wqe_idx)
  2086. {
  2087. struct ocrdma_hdr_wqe *hdr;
  2088. struct ocrdma_sge *rw;
  2089. int opcode;
  2090. hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
  2091. ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
  2092. /* Undo the hdr->cw swap */
  2093. opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
  2094. switch (opcode) {
  2095. case OCRDMA_WRITE:
  2096. ibwc->opcode = IB_WC_RDMA_WRITE;
  2097. break;
  2098. case OCRDMA_READ:
  2099. rw = (struct ocrdma_sge *)(hdr + 1);
  2100. ibwc->opcode = IB_WC_RDMA_READ;
  2101. ibwc->byte_len = rw->len;
  2102. break;
  2103. case OCRDMA_SEND:
  2104. ibwc->opcode = IB_WC_SEND;
  2105. break;
  2106. case OCRDMA_FR_MR:
  2107. ibwc->opcode = IB_WC_FAST_REG_MR;
  2108. break;
  2109. case OCRDMA_LKEY_INV:
  2110. ibwc->opcode = IB_WC_LOCAL_INV;
  2111. break;
  2112. default:
  2113. ibwc->status = IB_WC_GENERAL_ERR;
  2114. pr_err("%s() invalid opcode received = 0x%x\n",
  2115. __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
  2116. break;
  2117. }
  2118. }
  2119. static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
  2120. struct ocrdma_cqe *cqe)
  2121. {
  2122. if (is_cqe_for_sq(cqe)) {
  2123. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2124. cqe->flags_status_srcqpn) &
  2125. ~OCRDMA_CQE_STATUS_MASK);
  2126. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2127. cqe->flags_status_srcqpn) |
  2128. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2129. OCRDMA_CQE_STATUS_SHIFT));
  2130. } else {
  2131. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2132. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2133. cqe->flags_status_srcqpn) &
  2134. ~OCRDMA_CQE_UD_STATUS_MASK);
  2135. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2136. cqe->flags_status_srcqpn) |
  2137. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2138. OCRDMA_CQE_UD_STATUS_SHIFT));
  2139. } else {
  2140. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2141. cqe->flags_status_srcqpn) &
  2142. ~OCRDMA_CQE_STATUS_MASK);
  2143. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2144. cqe->flags_status_srcqpn) |
  2145. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2146. OCRDMA_CQE_STATUS_SHIFT));
  2147. }
  2148. }
  2149. }
  2150. static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2151. struct ocrdma_qp *qp, int status)
  2152. {
  2153. bool expand = false;
  2154. ibwc->byte_len = 0;
  2155. ibwc->qp = &qp->ibqp;
  2156. ibwc->status = ocrdma_to_ibwc_err(status);
  2157. ocrdma_flush_qp(qp);
  2158. ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
  2159. /* if wqe/rqe pending for which cqe needs to be returned,
  2160. * trigger inflating it.
  2161. */
  2162. if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
  2163. expand = true;
  2164. ocrdma_set_cqe_status_flushed(qp, cqe);
  2165. }
  2166. return expand;
  2167. }
  2168. static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2169. struct ocrdma_qp *qp, int status)
  2170. {
  2171. ibwc->opcode = IB_WC_RECV;
  2172. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2173. ocrdma_hwq_inc_tail(&qp->rq);
  2174. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2175. }
  2176. static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2177. struct ocrdma_qp *qp, int status)
  2178. {
  2179. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2180. ocrdma_hwq_inc_tail(&qp->sq);
  2181. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2182. }
  2183. static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
  2184. struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
  2185. bool *polled, bool *stop)
  2186. {
  2187. bool expand;
  2188. int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2189. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2190. /* when hw sq is empty, but rq is not empty, so we continue
  2191. * to keep the cqe in order to get the cq event again.
  2192. */
  2193. if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
  2194. /* when cq for rq and sq is same, it is safe to return
  2195. * flush cqe for RQEs.
  2196. */
  2197. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2198. *polled = true;
  2199. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2200. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2201. } else {
  2202. /* stop processing further cqe as this cqe is used for
  2203. * triggering cq event on buddy cq of RQ.
  2204. * When QP is destroyed, this cqe will be removed
  2205. * from the cq's hardware q.
  2206. */
  2207. *polled = false;
  2208. *stop = true;
  2209. expand = false;
  2210. }
  2211. } else if (is_hw_sq_empty(qp)) {
  2212. /* Do nothing */
  2213. expand = false;
  2214. *polled = false;
  2215. *stop = false;
  2216. } else {
  2217. *polled = true;
  2218. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2219. }
  2220. return expand;
  2221. }
  2222. static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
  2223. struct ocrdma_cqe *cqe,
  2224. struct ib_wc *ibwc, bool *polled)
  2225. {
  2226. bool expand = false;
  2227. int tail = qp->sq.tail;
  2228. u32 wqe_idx;
  2229. if (!qp->wqe_wr_id_tbl[tail].signaled) {
  2230. *polled = false; /* WC cannot be consumed yet */
  2231. } else {
  2232. ibwc->status = IB_WC_SUCCESS;
  2233. ibwc->wc_flags = 0;
  2234. ibwc->qp = &qp->ibqp;
  2235. ocrdma_update_wc(qp, ibwc, tail);
  2236. *polled = true;
  2237. }
  2238. wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
  2239. OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
  2240. if (tail != wqe_idx)
  2241. expand = true; /* Coalesced CQE can't be consumed yet */
  2242. ocrdma_hwq_inc_tail(&qp->sq);
  2243. return expand;
  2244. }
  2245. static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2246. struct ib_wc *ibwc, bool *polled, bool *stop)
  2247. {
  2248. int status;
  2249. bool expand;
  2250. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2251. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2252. if (status == OCRDMA_CQE_SUCCESS)
  2253. expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
  2254. else
  2255. expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
  2256. return expand;
  2257. }
  2258. static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
  2259. {
  2260. int status;
  2261. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2262. OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
  2263. ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
  2264. OCRDMA_CQE_SRCQP_MASK;
  2265. ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
  2266. OCRDMA_CQE_PKEY_MASK;
  2267. ibwc->wc_flags = IB_WC_GRH;
  2268. ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
  2269. OCRDMA_CQE_UD_XFER_LEN_SHIFT);
  2270. return status;
  2271. }
  2272. static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
  2273. struct ocrdma_cqe *cqe,
  2274. struct ocrdma_qp *qp)
  2275. {
  2276. unsigned long flags;
  2277. struct ocrdma_srq *srq;
  2278. u32 wqe_idx;
  2279. srq = get_ocrdma_srq(qp->ibqp.srq);
  2280. wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
  2281. OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
  2282. if (wqe_idx < 1)
  2283. BUG();
  2284. ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
  2285. spin_lock_irqsave(&srq->q_lock, flags);
  2286. ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
  2287. spin_unlock_irqrestore(&srq->q_lock, flags);
  2288. ocrdma_hwq_inc_tail(&srq->rq);
  2289. }
  2290. static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2291. struct ib_wc *ibwc, bool *polled, bool *stop,
  2292. int status)
  2293. {
  2294. bool expand;
  2295. /* when hw_rq is empty, but wq is not empty, so continue
  2296. * to keep the cqe to get the cq event again.
  2297. */
  2298. if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
  2299. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2300. *polled = true;
  2301. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2302. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2303. } else {
  2304. *polled = false;
  2305. *stop = true;
  2306. expand = false;
  2307. }
  2308. } else if (is_hw_rq_empty(qp)) {
  2309. /* Do nothing */
  2310. expand = false;
  2311. *polled = false;
  2312. *stop = false;
  2313. } else {
  2314. *polled = true;
  2315. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2316. }
  2317. return expand;
  2318. }
  2319. static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
  2320. struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
  2321. {
  2322. ibwc->opcode = IB_WC_RECV;
  2323. ibwc->qp = &qp->ibqp;
  2324. ibwc->status = IB_WC_SUCCESS;
  2325. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
  2326. ocrdma_update_ud_rcqe(ibwc, cqe);
  2327. else
  2328. ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
  2329. if (is_cqe_imm(cqe)) {
  2330. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2331. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2332. } else if (is_cqe_wr_imm(cqe)) {
  2333. ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2334. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2335. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2336. } else if (is_cqe_invalidated(cqe)) {
  2337. ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
  2338. ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2339. }
  2340. if (qp->ibqp.srq) {
  2341. ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
  2342. } else {
  2343. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2344. ocrdma_hwq_inc_tail(&qp->rq);
  2345. }
  2346. }
  2347. static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2348. struct ib_wc *ibwc, bool *polled, bool *stop)
  2349. {
  2350. int status;
  2351. bool expand = false;
  2352. ibwc->wc_flags = 0;
  2353. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2354. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2355. OCRDMA_CQE_UD_STATUS_MASK) >>
  2356. OCRDMA_CQE_UD_STATUS_SHIFT;
  2357. } else {
  2358. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2359. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2360. }
  2361. if (status == OCRDMA_CQE_SUCCESS) {
  2362. *polled = true;
  2363. ocrdma_poll_success_rcqe(qp, cqe, ibwc);
  2364. } else {
  2365. expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
  2366. status);
  2367. }
  2368. return expand;
  2369. }
  2370. static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
  2371. u16 cur_getp)
  2372. {
  2373. if (cq->phase_change) {
  2374. if (cur_getp == 0)
  2375. cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
  2376. } else {
  2377. /* clear valid bit */
  2378. cqe->flags_status_srcqpn = 0;
  2379. }
  2380. }
  2381. static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
  2382. struct ib_wc *ibwc)
  2383. {
  2384. u16 qpn = 0;
  2385. int i = 0;
  2386. bool expand = false;
  2387. int polled_hw_cqes = 0;
  2388. struct ocrdma_qp *qp = NULL;
  2389. struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
  2390. struct ocrdma_cqe *cqe;
  2391. u16 cur_getp; bool polled = false; bool stop = false;
  2392. cur_getp = cq->getp;
  2393. while (num_entries) {
  2394. cqe = cq->va + cur_getp;
  2395. /* check whether valid cqe or not */
  2396. if (!is_cqe_valid(cq, cqe))
  2397. break;
  2398. qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
  2399. /* ignore discarded cqe */
  2400. if (qpn == 0)
  2401. goto skip_cqe;
  2402. qp = dev->qp_tbl[qpn];
  2403. BUG_ON(qp == NULL);
  2404. if (is_cqe_for_sq(cqe)) {
  2405. expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
  2406. &stop);
  2407. } else {
  2408. expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
  2409. &stop);
  2410. }
  2411. if (expand)
  2412. goto expand_cqe;
  2413. if (stop)
  2414. goto stop_cqe;
  2415. /* clear qpn to avoid duplicate processing by discard_cqe() */
  2416. cqe->cmn.qpn = 0;
  2417. skip_cqe:
  2418. polled_hw_cqes += 1;
  2419. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  2420. ocrdma_change_cq_phase(cq, cqe, cur_getp);
  2421. expand_cqe:
  2422. if (polled) {
  2423. num_entries -= 1;
  2424. i += 1;
  2425. ibwc = ibwc + 1;
  2426. polled = false;
  2427. }
  2428. }
  2429. stop_cqe:
  2430. cq->getp = cur_getp;
  2431. if (cq->deferred_arm) {
  2432. ocrdma_ring_cq_db(dev, cq->id, true, cq->deferred_sol,
  2433. polled_hw_cqes);
  2434. cq->deferred_arm = false;
  2435. cq->deferred_sol = false;
  2436. } else {
  2437. /* We need to pop the CQE. No need to arm */
  2438. ocrdma_ring_cq_db(dev, cq->id, false, cq->deferred_sol,
  2439. polled_hw_cqes);
  2440. cq->deferred_sol = false;
  2441. }
  2442. return i;
  2443. }
  2444. /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
  2445. static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
  2446. struct ocrdma_qp *qp, struct ib_wc *ibwc)
  2447. {
  2448. int err_cqes = 0;
  2449. while (num_entries) {
  2450. if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
  2451. break;
  2452. if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
  2453. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2454. ocrdma_hwq_inc_tail(&qp->sq);
  2455. } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
  2456. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2457. ocrdma_hwq_inc_tail(&qp->rq);
  2458. } else {
  2459. return err_cqes;
  2460. }
  2461. ibwc->byte_len = 0;
  2462. ibwc->status = IB_WC_WR_FLUSH_ERR;
  2463. ibwc = ibwc + 1;
  2464. err_cqes += 1;
  2465. num_entries -= 1;
  2466. }
  2467. return err_cqes;
  2468. }
  2469. int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  2470. {
  2471. int cqes_to_poll = num_entries;
  2472. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2473. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2474. int num_os_cqe = 0, err_cqes = 0;
  2475. struct ocrdma_qp *qp;
  2476. unsigned long flags;
  2477. /* poll cqes from adapter CQ */
  2478. spin_lock_irqsave(&cq->cq_lock, flags);
  2479. num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
  2480. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2481. cqes_to_poll -= num_os_cqe;
  2482. if (cqes_to_poll) {
  2483. wc = wc + num_os_cqe;
  2484. /* adapter returns single error cqe when qp moves to
  2485. * error state. So insert error cqes with wc_status as
  2486. * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
  2487. * respectively which uses this CQ.
  2488. */
  2489. spin_lock_irqsave(&dev->flush_q_lock, flags);
  2490. list_for_each_entry(qp, &cq->sq_head, sq_entry) {
  2491. if (cqes_to_poll == 0)
  2492. break;
  2493. err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
  2494. cqes_to_poll -= err_cqes;
  2495. num_os_cqe += err_cqes;
  2496. wc = wc + err_cqes;
  2497. }
  2498. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  2499. }
  2500. return num_os_cqe;
  2501. }
  2502. int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
  2503. {
  2504. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2505. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2506. u16 cq_id;
  2507. unsigned long flags;
  2508. bool arm_needed = false, sol_needed = false;
  2509. cq_id = cq->id;
  2510. spin_lock_irqsave(&cq->cq_lock, flags);
  2511. if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
  2512. arm_needed = true;
  2513. if (cq_flags & IB_CQ_SOLICITED)
  2514. sol_needed = true;
  2515. if (cq->first_arm) {
  2516. ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
  2517. cq->first_arm = false;
  2518. }
  2519. cq->deferred_arm = true;
  2520. cq->deferred_sol = sol_needed;
  2521. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2522. return 0;
  2523. }
  2524. struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
  2525. {
  2526. int status;
  2527. struct ocrdma_mr *mr;
  2528. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2529. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2530. if (max_page_list_len > dev->attr.max_pages_per_frmr)
  2531. return ERR_PTR(-EINVAL);
  2532. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2533. if (!mr)
  2534. return ERR_PTR(-ENOMEM);
  2535. status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
  2536. if (status)
  2537. goto pbl_err;
  2538. mr->hwmr.fr_mr = 1;
  2539. mr->hwmr.remote_rd = 0;
  2540. mr->hwmr.remote_wr = 0;
  2541. mr->hwmr.local_rd = 0;
  2542. mr->hwmr.local_wr = 0;
  2543. mr->hwmr.mw_bind = 0;
  2544. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2545. if (status)
  2546. goto pbl_err;
  2547. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
  2548. if (status)
  2549. goto mbx_err;
  2550. mr->ibmr.rkey = mr->hwmr.lkey;
  2551. mr->ibmr.lkey = mr->hwmr.lkey;
  2552. dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
  2553. (unsigned long) mr;
  2554. return &mr->ibmr;
  2555. mbx_err:
  2556. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2557. pbl_err:
  2558. kfree(mr);
  2559. return ERR_PTR(-ENOMEM);
  2560. }
  2561. struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
  2562. *ibdev,
  2563. int page_list_len)
  2564. {
  2565. struct ib_fast_reg_page_list *frmr_list;
  2566. int size;
  2567. size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
  2568. frmr_list = kzalloc(size, GFP_KERNEL);
  2569. if (!frmr_list)
  2570. return ERR_PTR(-ENOMEM);
  2571. frmr_list->page_list = (u64 *)(frmr_list + 1);
  2572. return frmr_list;
  2573. }
  2574. void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
  2575. {
  2576. kfree(page_list);
  2577. }
  2578. #define MAX_KERNEL_PBE_SIZE 65536
  2579. static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
  2580. int buf_cnt, u32 *pbe_size)
  2581. {
  2582. u64 total_size = 0;
  2583. u64 buf_size = 0;
  2584. int i;
  2585. *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
  2586. *pbe_size = roundup_pow_of_two(*pbe_size);
  2587. /* find the smallest PBE size that we can have */
  2588. for (i = 0; i < buf_cnt; i++) {
  2589. /* first addr may not be page aligned, so ignore checking */
  2590. if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
  2591. (buf_list[i].size & ~PAGE_MASK))) {
  2592. return 0;
  2593. }
  2594. /* if configured PBE size is greater then the chosen one,
  2595. * reduce the PBE size.
  2596. */
  2597. buf_size = roundup(buf_list[i].size, PAGE_SIZE);
  2598. /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
  2599. buf_size = roundup_pow_of_two(buf_size);
  2600. if (*pbe_size > buf_size)
  2601. *pbe_size = buf_size;
  2602. total_size += buf_size;
  2603. }
  2604. *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
  2605. (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
  2606. /* num_pbes = total_size / (*pbe_size); this is implemented below. */
  2607. return total_size >> ilog2(*pbe_size);
  2608. }
  2609. static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
  2610. u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
  2611. struct ocrdma_hw_mr *hwmr)
  2612. {
  2613. int i;
  2614. int idx;
  2615. int pbes_per_buf = 0;
  2616. u64 buf_addr = 0;
  2617. int num_pbes;
  2618. struct ocrdma_pbe *pbe;
  2619. int total_num_pbes = 0;
  2620. if (!hwmr->num_pbes)
  2621. return;
  2622. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2623. num_pbes = 0;
  2624. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  2625. for (i = 0; i < ib_buf_cnt; i++) {
  2626. buf_addr = buf_list[i].addr;
  2627. pbes_per_buf =
  2628. roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
  2629. pbe_size;
  2630. hwmr->len += buf_list[i].size;
  2631. /* number of pbes can be more for one OS buf, when
  2632. * buffers are of different sizes.
  2633. * split the ib_buf to one or more pbes.
  2634. */
  2635. for (idx = 0; idx < pbes_per_buf; idx++) {
  2636. /* we program always page aligned addresses,
  2637. * first unaligned address is taken care by fbo.
  2638. */
  2639. if (i == 0) {
  2640. /* for non zero fbo, assign the
  2641. * start of the page.
  2642. */
  2643. pbe->pa_lo =
  2644. cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  2645. pbe->pa_hi =
  2646. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2647. } else {
  2648. pbe->pa_lo =
  2649. cpu_to_le32((u32) (buf_addr & 0xffffffff));
  2650. pbe->pa_hi =
  2651. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2652. }
  2653. buf_addr += pbe_size;
  2654. num_pbes += 1;
  2655. total_num_pbes += 1;
  2656. pbe++;
  2657. if (total_num_pbes == hwmr->num_pbes)
  2658. goto mr_tbl_done;
  2659. /* if the pbl is full storing the pbes,
  2660. * move to next pbl.
  2661. */
  2662. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  2663. pbl_tbl++;
  2664. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2665. num_pbes = 0;
  2666. }
  2667. }
  2668. }
  2669. mr_tbl_done:
  2670. return;
  2671. }
  2672. struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
  2673. struct ib_phys_buf *buf_list,
  2674. int buf_cnt, int acc, u64 *iova_start)
  2675. {
  2676. int status = -ENOMEM;
  2677. struct ocrdma_mr *mr;
  2678. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2679. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2680. u32 num_pbes;
  2681. u32 pbe_size = 0;
  2682. if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
  2683. return ERR_PTR(-EINVAL);
  2684. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2685. if (!mr)
  2686. return ERR_PTR(status);
  2687. num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
  2688. if (num_pbes == 0) {
  2689. status = -EINVAL;
  2690. goto pbl_err;
  2691. }
  2692. status = ocrdma_get_pbl_info(dev, mr, num_pbes);
  2693. if (status)
  2694. goto pbl_err;
  2695. mr->hwmr.pbe_size = pbe_size;
  2696. mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
  2697. mr->hwmr.va = *iova_start;
  2698. mr->hwmr.local_rd = 1;
  2699. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  2700. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  2701. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  2702. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  2703. mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
  2704. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2705. if (status)
  2706. goto pbl_err;
  2707. build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
  2708. &mr->hwmr);
  2709. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
  2710. if (status)
  2711. goto mbx_err;
  2712. mr->ibmr.lkey = mr->hwmr.lkey;
  2713. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  2714. mr->ibmr.rkey = mr->hwmr.lkey;
  2715. return &mr->ibmr;
  2716. mbx_err:
  2717. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2718. pbl_err:
  2719. kfree(mr);
  2720. return ERR_PTR(status);
  2721. }