ib_verbs.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202
  1. /*
  2. * Broadcom NetXtreme-E RoCE driver.
  3. *
  4. * Copyright (c) 2016 - 2017, Broadcom. All rights reserved. The term
  5. * Broadcom refers to Broadcom Limited and/or its subsidiaries.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
  28. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  34. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Description: IB Verbs interpreter
  37. */
  38. #include <linux/interrupt.h>
  39. #include <linux/types.h>
  40. #include <linux/pci.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/if_ether.h>
  43. #include <rdma/ib_verbs.h>
  44. #include <rdma/ib_user_verbs.h>
  45. #include <rdma/ib_umem.h>
  46. #include <rdma/ib_addr.h>
  47. #include <rdma/ib_mad.h>
  48. #include <rdma/ib_cache.h>
  49. #include "bnxt_ulp.h"
  50. #include "roce_hsi.h"
  51. #include "qplib_res.h"
  52. #include "qplib_sp.h"
  53. #include "qplib_fp.h"
  54. #include "qplib_rcfw.h"
  55. #include "bnxt_re.h"
  56. #include "ib_verbs.h"
  57. #include <rdma/bnxt_re-abi.h>
  58. static int bnxt_re_build_sgl(struct ib_sge *ib_sg_list,
  59. struct bnxt_qplib_sge *sg_list, int num)
  60. {
  61. int i, total = 0;
  62. for (i = 0; i < num; i++) {
  63. sg_list[i].addr = ib_sg_list[i].addr;
  64. sg_list[i].lkey = ib_sg_list[i].lkey;
  65. sg_list[i].size = ib_sg_list[i].length;
  66. total += sg_list[i].size;
  67. }
  68. return total;
  69. }
  70. /* Device */
  71. struct net_device *bnxt_re_get_netdev(struct ib_device *ibdev, u8 port_num)
  72. {
  73. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  74. struct net_device *netdev = NULL;
  75. rcu_read_lock();
  76. if (rdev)
  77. netdev = rdev->netdev;
  78. if (netdev)
  79. dev_hold(netdev);
  80. rcu_read_unlock();
  81. return netdev;
  82. }
  83. int bnxt_re_query_device(struct ib_device *ibdev,
  84. struct ib_device_attr *ib_attr,
  85. struct ib_udata *udata)
  86. {
  87. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  88. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  89. memset(ib_attr, 0, sizeof(*ib_attr));
  90. ib_attr->fw_ver = (u64)(unsigned long)(dev_attr->fw_ver);
  91. bnxt_qplib_get_guid(rdev->netdev->dev_addr,
  92. (u8 *)&ib_attr->sys_image_guid);
  93. ib_attr->max_mr_size = ~0ull;
  94. ib_attr->page_size_cap = BNXT_RE_PAGE_SIZE_4K | BNXT_RE_PAGE_SIZE_8K |
  95. BNXT_RE_PAGE_SIZE_64K | BNXT_RE_PAGE_SIZE_2M |
  96. BNXT_RE_PAGE_SIZE_8M | BNXT_RE_PAGE_SIZE_1G;
  97. ib_attr->vendor_id = rdev->en_dev->pdev->vendor;
  98. ib_attr->vendor_part_id = rdev->en_dev->pdev->device;
  99. ib_attr->hw_ver = rdev->en_dev->pdev->subsystem_device;
  100. ib_attr->max_qp = dev_attr->max_qp;
  101. ib_attr->max_qp_wr = dev_attr->max_qp_wqes;
  102. ib_attr->device_cap_flags =
  103. IB_DEVICE_CURR_QP_STATE_MOD
  104. | IB_DEVICE_RC_RNR_NAK_GEN
  105. | IB_DEVICE_SHUTDOWN_PORT
  106. | IB_DEVICE_SYS_IMAGE_GUID
  107. | IB_DEVICE_LOCAL_DMA_LKEY
  108. | IB_DEVICE_RESIZE_MAX_WR
  109. | IB_DEVICE_PORT_ACTIVE_EVENT
  110. | IB_DEVICE_N_NOTIFY_CQ
  111. | IB_DEVICE_MEM_WINDOW
  112. | IB_DEVICE_MEM_WINDOW_TYPE_2B
  113. | IB_DEVICE_MEM_MGT_EXTENSIONS;
  114. ib_attr->max_sge = dev_attr->max_qp_sges;
  115. ib_attr->max_sge_rd = dev_attr->max_qp_sges;
  116. ib_attr->max_cq = dev_attr->max_cq;
  117. ib_attr->max_cqe = dev_attr->max_cq_wqes;
  118. ib_attr->max_mr = dev_attr->max_mr;
  119. ib_attr->max_pd = dev_attr->max_pd;
  120. ib_attr->max_qp_rd_atom = dev_attr->max_qp_rd_atom;
  121. ib_attr->max_qp_init_rd_atom = dev_attr->max_qp_rd_atom;
  122. ib_attr->atomic_cap = IB_ATOMIC_HCA;
  123. ib_attr->masked_atomic_cap = IB_ATOMIC_HCA;
  124. ib_attr->max_ee_rd_atom = 0;
  125. ib_attr->max_res_rd_atom = 0;
  126. ib_attr->max_ee_init_rd_atom = 0;
  127. ib_attr->max_ee = 0;
  128. ib_attr->max_rdd = 0;
  129. ib_attr->max_mw = dev_attr->max_mw;
  130. ib_attr->max_raw_ipv6_qp = 0;
  131. ib_attr->max_raw_ethy_qp = dev_attr->max_raw_ethy_qp;
  132. ib_attr->max_mcast_grp = 0;
  133. ib_attr->max_mcast_qp_attach = 0;
  134. ib_attr->max_total_mcast_qp_attach = 0;
  135. ib_attr->max_ah = dev_attr->max_ah;
  136. ib_attr->max_fmr = dev_attr->max_fmr;
  137. ib_attr->max_map_per_fmr = 1; /* ? */
  138. ib_attr->max_srq = dev_attr->max_srq;
  139. ib_attr->max_srq_wr = dev_attr->max_srq_wqes;
  140. ib_attr->max_srq_sge = dev_attr->max_srq_sges;
  141. ib_attr->max_fast_reg_page_list_len = MAX_PBL_LVL_1_PGS;
  142. ib_attr->max_pkeys = 1;
  143. ib_attr->local_ca_ack_delay = 0;
  144. return 0;
  145. }
  146. int bnxt_re_modify_device(struct ib_device *ibdev,
  147. int device_modify_mask,
  148. struct ib_device_modify *device_modify)
  149. {
  150. switch (device_modify_mask) {
  151. case IB_DEVICE_MODIFY_SYS_IMAGE_GUID:
  152. /* Modify the GUID requires the modification of the GID table */
  153. /* GUID should be made as READ-ONLY */
  154. break;
  155. case IB_DEVICE_MODIFY_NODE_DESC:
  156. /* Node Desc should be made as READ-ONLY */
  157. break;
  158. default:
  159. break;
  160. }
  161. return 0;
  162. }
  163. static void __to_ib_speed_width(struct net_device *netdev, u8 *speed, u8 *width)
  164. {
  165. struct ethtool_link_ksettings lksettings;
  166. u32 espeed;
  167. if (netdev->ethtool_ops && netdev->ethtool_ops->get_link_ksettings) {
  168. memset(&lksettings, 0, sizeof(lksettings));
  169. rtnl_lock();
  170. netdev->ethtool_ops->get_link_ksettings(netdev, &lksettings);
  171. rtnl_unlock();
  172. espeed = lksettings.base.speed;
  173. } else {
  174. espeed = SPEED_UNKNOWN;
  175. }
  176. switch (espeed) {
  177. case SPEED_1000:
  178. *speed = IB_SPEED_SDR;
  179. *width = IB_WIDTH_1X;
  180. break;
  181. case SPEED_10000:
  182. *speed = IB_SPEED_QDR;
  183. *width = IB_WIDTH_1X;
  184. break;
  185. case SPEED_20000:
  186. *speed = IB_SPEED_DDR;
  187. *width = IB_WIDTH_4X;
  188. break;
  189. case SPEED_25000:
  190. *speed = IB_SPEED_EDR;
  191. *width = IB_WIDTH_1X;
  192. break;
  193. case SPEED_40000:
  194. *speed = IB_SPEED_QDR;
  195. *width = IB_WIDTH_4X;
  196. break;
  197. case SPEED_50000:
  198. break;
  199. default:
  200. *speed = IB_SPEED_SDR;
  201. *width = IB_WIDTH_1X;
  202. break;
  203. }
  204. }
  205. /* Port */
  206. int bnxt_re_query_port(struct ib_device *ibdev, u8 port_num,
  207. struct ib_port_attr *port_attr)
  208. {
  209. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  210. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  211. memset(port_attr, 0, sizeof(*port_attr));
  212. if (netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev)) {
  213. port_attr->state = IB_PORT_ACTIVE;
  214. port_attr->phys_state = 5;
  215. } else {
  216. port_attr->state = IB_PORT_DOWN;
  217. port_attr->phys_state = 3;
  218. }
  219. port_attr->max_mtu = IB_MTU_4096;
  220. port_attr->active_mtu = iboe_get_mtu(rdev->netdev->mtu);
  221. port_attr->gid_tbl_len = dev_attr->max_sgid;
  222. port_attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
  223. IB_PORT_DEVICE_MGMT_SUP |
  224. IB_PORT_VENDOR_CLASS_SUP |
  225. IB_PORT_IP_BASED_GIDS;
  226. /* Max MSG size set to 2G for now */
  227. port_attr->max_msg_sz = 0x80000000;
  228. port_attr->bad_pkey_cntr = 0;
  229. port_attr->qkey_viol_cntr = 0;
  230. port_attr->pkey_tbl_len = dev_attr->max_pkey;
  231. port_attr->lid = 0;
  232. port_attr->sm_lid = 0;
  233. port_attr->lmc = 0;
  234. port_attr->max_vl_num = 4;
  235. port_attr->sm_sl = 0;
  236. port_attr->subnet_timeout = 0;
  237. port_attr->init_type_reply = 0;
  238. /* call the underlying netdev's ethtool hooks to query speed settings
  239. * for which we acquire rtnl_lock _only_ if it's registered with
  240. * IB stack to avoid race in the NETDEV_UNREG path
  241. */
  242. if (test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
  243. __to_ib_speed_width(rdev->netdev, &port_attr->active_speed,
  244. &port_attr->active_width);
  245. return 0;
  246. }
  247. int bnxt_re_modify_port(struct ib_device *ibdev, u8 port_num,
  248. int port_modify_mask,
  249. struct ib_port_modify *port_modify)
  250. {
  251. switch (port_modify_mask) {
  252. case IB_PORT_SHUTDOWN:
  253. break;
  254. case IB_PORT_INIT_TYPE:
  255. break;
  256. case IB_PORT_RESET_QKEY_CNTR:
  257. break;
  258. default:
  259. break;
  260. }
  261. return 0;
  262. }
  263. int bnxt_re_get_port_immutable(struct ib_device *ibdev, u8 port_num,
  264. struct ib_port_immutable *immutable)
  265. {
  266. struct ib_port_attr port_attr;
  267. if (bnxt_re_query_port(ibdev, port_num, &port_attr))
  268. return -EINVAL;
  269. immutable->pkey_tbl_len = port_attr.pkey_tbl_len;
  270. immutable->gid_tbl_len = port_attr.gid_tbl_len;
  271. immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
  272. immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
  273. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  274. return 0;
  275. }
  276. int bnxt_re_query_pkey(struct ib_device *ibdev, u8 port_num,
  277. u16 index, u16 *pkey)
  278. {
  279. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  280. /* Ignore port_num */
  281. memset(pkey, 0, sizeof(*pkey));
  282. return bnxt_qplib_get_pkey(&rdev->qplib_res,
  283. &rdev->qplib_res.pkey_tbl, index, pkey);
  284. }
  285. int bnxt_re_query_gid(struct ib_device *ibdev, u8 port_num,
  286. int index, union ib_gid *gid)
  287. {
  288. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  289. int rc = 0;
  290. /* Ignore port_num */
  291. memset(gid, 0, sizeof(*gid));
  292. rc = bnxt_qplib_get_sgid(&rdev->qplib_res,
  293. &rdev->qplib_res.sgid_tbl, index,
  294. (struct bnxt_qplib_gid *)gid);
  295. return rc;
  296. }
  297. int bnxt_re_del_gid(struct ib_device *ibdev, u8 port_num,
  298. unsigned int index, void **context)
  299. {
  300. int rc = 0;
  301. struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
  302. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  303. struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
  304. /* Delete the entry from the hardware */
  305. ctx = *context;
  306. if (!ctx)
  307. return -EINVAL;
  308. if (sgid_tbl && sgid_tbl->active) {
  309. if (ctx->idx >= sgid_tbl->max)
  310. return -EINVAL;
  311. ctx->refcnt--;
  312. if (!ctx->refcnt) {
  313. rc = bnxt_qplib_del_sgid
  314. (sgid_tbl,
  315. &sgid_tbl->tbl[ctx->idx], true);
  316. if (rc)
  317. dev_err(rdev_to_dev(rdev),
  318. "Failed to remove GID: %#x", rc);
  319. ctx_tbl = sgid_tbl->ctx;
  320. ctx_tbl[ctx->idx] = NULL;
  321. kfree(ctx);
  322. }
  323. } else {
  324. return -EINVAL;
  325. }
  326. return rc;
  327. }
  328. int bnxt_re_add_gid(struct ib_device *ibdev, u8 port_num,
  329. unsigned int index, const union ib_gid *gid,
  330. const struct ib_gid_attr *attr, void **context)
  331. {
  332. int rc;
  333. u32 tbl_idx = 0;
  334. u16 vlan_id = 0xFFFF;
  335. struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
  336. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  337. struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
  338. if ((attr->ndev) && is_vlan_dev(attr->ndev))
  339. vlan_id = vlan_dev_vlan_id(attr->ndev);
  340. rc = bnxt_qplib_add_sgid(sgid_tbl, (struct bnxt_qplib_gid *)gid,
  341. rdev->qplib_res.netdev->dev_addr,
  342. vlan_id, true, &tbl_idx);
  343. if (rc == -EALREADY) {
  344. ctx_tbl = sgid_tbl->ctx;
  345. ctx_tbl[tbl_idx]->refcnt++;
  346. *context = ctx_tbl[tbl_idx];
  347. return 0;
  348. }
  349. if (rc < 0) {
  350. dev_err(rdev_to_dev(rdev), "Failed to add GID: %#x", rc);
  351. return rc;
  352. }
  353. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  354. if (!ctx)
  355. return -ENOMEM;
  356. ctx_tbl = sgid_tbl->ctx;
  357. ctx->idx = tbl_idx;
  358. ctx->refcnt = 1;
  359. ctx_tbl[tbl_idx] = ctx;
  360. return rc;
  361. }
  362. enum rdma_link_layer bnxt_re_get_link_layer(struct ib_device *ibdev,
  363. u8 port_num)
  364. {
  365. return IB_LINK_LAYER_ETHERNET;
  366. }
  367. /* Protection Domains */
  368. int bnxt_re_dealloc_pd(struct ib_pd *ib_pd)
  369. {
  370. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  371. struct bnxt_re_dev *rdev = pd->rdev;
  372. int rc;
  373. if (ib_pd->uobject && pd->dpi.dbr) {
  374. struct ib_ucontext *ib_uctx = ib_pd->uobject->context;
  375. struct bnxt_re_ucontext *ucntx;
  376. /* Free DPI only if this is the first PD allocated by the
  377. * application and mark the context dpi as NULL
  378. */
  379. ucntx = container_of(ib_uctx, struct bnxt_re_ucontext, ib_uctx);
  380. rc = bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
  381. &rdev->qplib_res.dpi_tbl,
  382. &pd->dpi);
  383. if (rc)
  384. dev_err(rdev_to_dev(rdev), "Failed to deallocate HW DPI");
  385. /* Don't fail, continue*/
  386. ucntx->dpi = NULL;
  387. }
  388. rc = bnxt_qplib_dealloc_pd(&rdev->qplib_res,
  389. &rdev->qplib_res.pd_tbl,
  390. &pd->qplib_pd);
  391. if (rc) {
  392. dev_err(rdev_to_dev(rdev), "Failed to deallocate HW PD");
  393. return rc;
  394. }
  395. kfree(pd);
  396. return 0;
  397. }
  398. struct ib_pd *bnxt_re_alloc_pd(struct ib_device *ibdev,
  399. struct ib_ucontext *ucontext,
  400. struct ib_udata *udata)
  401. {
  402. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  403. struct bnxt_re_ucontext *ucntx = container_of(ucontext,
  404. struct bnxt_re_ucontext,
  405. ib_uctx);
  406. struct bnxt_re_pd *pd;
  407. int rc;
  408. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  409. if (!pd)
  410. return ERR_PTR(-ENOMEM);
  411. pd->rdev = rdev;
  412. if (bnxt_qplib_alloc_pd(&rdev->qplib_res.pd_tbl, &pd->qplib_pd)) {
  413. dev_err(rdev_to_dev(rdev), "Failed to allocate HW PD");
  414. rc = -ENOMEM;
  415. goto fail;
  416. }
  417. if (udata) {
  418. struct bnxt_re_pd_resp resp;
  419. if (!ucntx->dpi) {
  420. /* Allocate DPI in alloc_pd to avoid failing of
  421. * ibv_devinfo and family of application when DPIs
  422. * are depleted.
  423. */
  424. if (bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
  425. &pd->dpi, ucntx)) {
  426. rc = -ENOMEM;
  427. goto dbfail;
  428. }
  429. ucntx->dpi = &pd->dpi;
  430. }
  431. resp.pdid = pd->qplib_pd.id;
  432. /* Still allow mapping this DBR to the new user PD. */
  433. resp.dpi = ucntx->dpi->dpi;
  434. resp.dbr = (u64)ucntx->dpi->umdbr;
  435. rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
  436. if (rc) {
  437. dev_err(rdev_to_dev(rdev),
  438. "Failed to copy user response\n");
  439. goto dbfail;
  440. }
  441. }
  442. return &pd->ib_pd;
  443. dbfail:
  444. (void)bnxt_qplib_dealloc_pd(&rdev->qplib_res, &rdev->qplib_res.pd_tbl,
  445. &pd->qplib_pd);
  446. fail:
  447. kfree(pd);
  448. return ERR_PTR(rc);
  449. }
  450. /* Address Handles */
  451. int bnxt_re_destroy_ah(struct ib_ah *ib_ah)
  452. {
  453. struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
  454. struct bnxt_re_dev *rdev = ah->rdev;
  455. int rc;
  456. rc = bnxt_qplib_destroy_ah(&rdev->qplib_res, &ah->qplib_ah);
  457. if (rc) {
  458. dev_err(rdev_to_dev(rdev), "Failed to destroy HW AH");
  459. return rc;
  460. }
  461. kfree(ah);
  462. return 0;
  463. }
  464. struct ib_ah *bnxt_re_create_ah(struct ib_pd *ib_pd,
  465. struct ib_ah_attr *ah_attr,
  466. struct ib_udata *udata)
  467. {
  468. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  469. struct bnxt_re_dev *rdev = pd->rdev;
  470. struct bnxt_re_ah *ah;
  471. int rc;
  472. u16 vlan_tag;
  473. u8 nw_type;
  474. struct ib_gid_attr sgid_attr;
  475. if (!(ah_attr->ah_flags & IB_AH_GRH)) {
  476. dev_err(rdev_to_dev(rdev), "Failed to alloc AH: GRH not set");
  477. return ERR_PTR(-EINVAL);
  478. }
  479. ah = kzalloc(sizeof(*ah), GFP_ATOMIC);
  480. if (!ah)
  481. return ERR_PTR(-ENOMEM);
  482. ah->rdev = rdev;
  483. ah->qplib_ah.pd = &pd->qplib_pd;
  484. /* Supply the configuration for the HW */
  485. memcpy(ah->qplib_ah.dgid.data, ah_attr->grh.dgid.raw,
  486. sizeof(union ib_gid));
  487. /*
  488. * If RoCE V2 is enabled, stack will have two entries for
  489. * each GID entry. Avoiding this duplicte entry in HW. Dividing
  490. * the GID index by 2 for RoCE V2
  491. */
  492. ah->qplib_ah.sgid_index = ah_attr->grh.sgid_index / 2;
  493. ah->qplib_ah.host_sgid_index = ah_attr->grh.sgid_index;
  494. ah->qplib_ah.traffic_class = ah_attr->grh.traffic_class;
  495. ah->qplib_ah.flow_label = ah_attr->grh.flow_label;
  496. ah->qplib_ah.hop_limit = ah_attr->grh.hop_limit;
  497. ah->qplib_ah.sl = ah_attr->sl;
  498. if (ib_pd->uobject &&
  499. !rdma_is_multicast_addr((struct in6_addr *)
  500. ah_attr->grh.dgid.raw) &&
  501. !rdma_link_local_addr((struct in6_addr *)
  502. ah_attr->grh.dgid.raw)) {
  503. union ib_gid sgid;
  504. rc = ib_get_cached_gid(&rdev->ibdev, 1,
  505. ah_attr->grh.sgid_index, &sgid,
  506. &sgid_attr);
  507. if (rc) {
  508. dev_err(rdev_to_dev(rdev),
  509. "Failed to query gid at index %d",
  510. ah_attr->grh.sgid_index);
  511. goto fail;
  512. }
  513. if (sgid_attr.ndev) {
  514. if (is_vlan_dev(sgid_attr.ndev))
  515. vlan_tag = vlan_dev_vlan_id(sgid_attr.ndev);
  516. dev_put(sgid_attr.ndev);
  517. }
  518. /* Get network header type for this GID */
  519. nw_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
  520. switch (nw_type) {
  521. case RDMA_NETWORK_IPV4:
  522. ah->qplib_ah.nw_type = CMDQ_CREATE_AH_TYPE_V2IPV4;
  523. break;
  524. case RDMA_NETWORK_IPV6:
  525. ah->qplib_ah.nw_type = CMDQ_CREATE_AH_TYPE_V2IPV6;
  526. break;
  527. default:
  528. ah->qplib_ah.nw_type = CMDQ_CREATE_AH_TYPE_V1;
  529. break;
  530. }
  531. rc = rdma_addr_find_l2_eth_by_grh(&sgid, &ah_attr->grh.dgid,
  532. ah_attr->dmac, &vlan_tag,
  533. &sgid_attr.ndev->ifindex,
  534. NULL);
  535. if (rc) {
  536. dev_err(rdev_to_dev(rdev), "Failed to get dmac\n");
  537. goto fail;
  538. }
  539. }
  540. memcpy(ah->qplib_ah.dmac, ah_attr->dmac, ETH_ALEN);
  541. rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah);
  542. if (rc) {
  543. dev_err(rdev_to_dev(rdev), "Failed to allocate HW AH");
  544. goto fail;
  545. }
  546. /* Write AVID to shared page. */
  547. if (ib_pd->uobject) {
  548. struct ib_ucontext *ib_uctx = ib_pd->uobject->context;
  549. struct bnxt_re_ucontext *uctx;
  550. unsigned long flag;
  551. u32 *wrptr;
  552. uctx = container_of(ib_uctx, struct bnxt_re_ucontext, ib_uctx);
  553. spin_lock_irqsave(&uctx->sh_lock, flag);
  554. wrptr = (u32 *)(uctx->shpg + BNXT_RE_AVID_OFFT);
  555. *wrptr = ah->qplib_ah.id;
  556. wmb(); /* make sure cache is updated. */
  557. spin_unlock_irqrestore(&uctx->sh_lock, flag);
  558. }
  559. return &ah->ib_ah;
  560. fail:
  561. kfree(ah);
  562. return ERR_PTR(rc);
  563. }
  564. int bnxt_re_modify_ah(struct ib_ah *ib_ah, struct ib_ah_attr *ah_attr)
  565. {
  566. return 0;
  567. }
  568. int bnxt_re_query_ah(struct ib_ah *ib_ah, struct ib_ah_attr *ah_attr)
  569. {
  570. struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
  571. memcpy(ah_attr->grh.dgid.raw, ah->qplib_ah.dgid.data,
  572. sizeof(union ib_gid));
  573. ah_attr->grh.sgid_index = ah->qplib_ah.host_sgid_index;
  574. ah_attr->grh.traffic_class = ah->qplib_ah.traffic_class;
  575. ah_attr->sl = ah->qplib_ah.sl;
  576. memcpy(ah_attr->dmac, ah->qplib_ah.dmac, ETH_ALEN);
  577. ah_attr->ah_flags = IB_AH_GRH;
  578. ah_attr->port_num = 1;
  579. ah_attr->static_rate = 0;
  580. return 0;
  581. }
  582. /* Queue Pairs */
  583. int bnxt_re_destroy_qp(struct ib_qp *ib_qp)
  584. {
  585. struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
  586. struct bnxt_re_dev *rdev = qp->rdev;
  587. int rc;
  588. rc = bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
  589. if (rc) {
  590. dev_err(rdev_to_dev(rdev), "Failed to destroy HW QP");
  591. return rc;
  592. }
  593. if (ib_qp->qp_type == IB_QPT_GSI && rdev->qp1_sqp) {
  594. rc = bnxt_qplib_destroy_ah(&rdev->qplib_res,
  595. &rdev->sqp_ah->qplib_ah);
  596. if (rc) {
  597. dev_err(rdev_to_dev(rdev),
  598. "Failed to destroy HW AH for shadow QP");
  599. return rc;
  600. }
  601. rc = bnxt_qplib_destroy_qp(&rdev->qplib_res,
  602. &rdev->qp1_sqp->qplib_qp);
  603. if (rc) {
  604. dev_err(rdev_to_dev(rdev),
  605. "Failed to destroy Shadow QP");
  606. return rc;
  607. }
  608. mutex_lock(&rdev->qp_lock);
  609. list_del(&rdev->qp1_sqp->list);
  610. atomic_dec(&rdev->qp_count);
  611. mutex_unlock(&rdev->qp_lock);
  612. kfree(rdev->sqp_ah);
  613. kfree(rdev->qp1_sqp);
  614. }
  615. if (qp->rumem && !IS_ERR(qp->rumem))
  616. ib_umem_release(qp->rumem);
  617. if (qp->sumem && !IS_ERR(qp->sumem))
  618. ib_umem_release(qp->sumem);
  619. mutex_lock(&rdev->qp_lock);
  620. list_del(&qp->list);
  621. atomic_dec(&rdev->qp_count);
  622. mutex_unlock(&rdev->qp_lock);
  623. kfree(qp);
  624. return 0;
  625. }
  626. static u8 __from_ib_qp_type(enum ib_qp_type type)
  627. {
  628. switch (type) {
  629. case IB_QPT_GSI:
  630. return CMDQ_CREATE_QP1_TYPE_GSI;
  631. case IB_QPT_RC:
  632. return CMDQ_CREATE_QP_TYPE_RC;
  633. case IB_QPT_UD:
  634. return CMDQ_CREATE_QP_TYPE_UD;
  635. default:
  636. return IB_QPT_MAX;
  637. }
  638. }
  639. static int bnxt_re_init_user_qp(struct bnxt_re_dev *rdev, struct bnxt_re_pd *pd,
  640. struct bnxt_re_qp *qp, struct ib_udata *udata)
  641. {
  642. struct bnxt_re_qp_req ureq;
  643. struct bnxt_qplib_qp *qplib_qp = &qp->qplib_qp;
  644. struct ib_umem *umem;
  645. int bytes = 0;
  646. struct ib_ucontext *context = pd->ib_pd.uobject->context;
  647. struct bnxt_re_ucontext *cntx = container_of(context,
  648. struct bnxt_re_ucontext,
  649. ib_uctx);
  650. if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
  651. return -EFAULT;
  652. bytes = (qplib_qp->sq.max_wqe * BNXT_QPLIB_MAX_SQE_ENTRY_SIZE);
  653. /* Consider mapping PSN search memory only for RC QPs. */
  654. if (qplib_qp->type == CMDQ_CREATE_QP_TYPE_RC)
  655. bytes += (qplib_qp->sq.max_wqe * sizeof(struct sq_psn_search));
  656. bytes = PAGE_ALIGN(bytes);
  657. umem = ib_umem_get(context, ureq.qpsva, bytes,
  658. IB_ACCESS_LOCAL_WRITE, 1);
  659. if (IS_ERR(umem))
  660. return PTR_ERR(umem);
  661. qp->sumem = umem;
  662. qplib_qp->sq.sglist = umem->sg_head.sgl;
  663. qplib_qp->sq.nmap = umem->nmap;
  664. qplib_qp->qp_handle = ureq.qp_handle;
  665. if (!qp->qplib_qp.srq) {
  666. bytes = (qplib_qp->rq.max_wqe * BNXT_QPLIB_MAX_RQE_ENTRY_SIZE);
  667. bytes = PAGE_ALIGN(bytes);
  668. umem = ib_umem_get(context, ureq.qprva, bytes,
  669. IB_ACCESS_LOCAL_WRITE, 1);
  670. if (IS_ERR(umem))
  671. goto rqfail;
  672. qp->rumem = umem;
  673. qplib_qp->rq.sglist = umem->sg_head.sgl;
  674. qplib_qp->rq.nmap = umem->nmap;
  675. }
  676. qplib_qp->dpi = cntx->dpi;
  677. return 0;
  678. rqfail:
  679. ib_umem_release(qp->sumem);
  680. qp->sumem = NULL;
  681. qplib_qp->sq.sglist = NULL;
  682. qplib_qp->sq.nmap = 0;
  683. return PTR_ERR(umem);
  684. }
  685. static struct bnxt_re_ah *bnxt_re_create_shadow_qp_ah
  686. (struct bnxt_re_pd *pd,
  687. struct bnxt_qplib_res *qp1_res,
  688. struct bnxt_qplib_qp *qp1_qp)
  689. {
  690. struct bnxt_re_dev *rdev = pd->rdev;
  691. struct bnxt_re_ah *ah;
  692. union ib_gid sgid;
  693. int rc;
  694. ah = kzalloc(sizeof(*ah), GFP_KERNEL);
  695. if (!ah)
  696. return NULL;
  697. memset(ah, 0, sizeof(*ah));
  698. ah->rdev = rdev;
  699. ah->qplib_ah.pd = &pd->qplib_pd;
  700. rc = bnxt_re_query_gid(&rdev->ibdev, 1, 0, &sgid);
  701. if (rc)
  702. goto fail;
  703. /* supply the dgid data same as sgid */
  704. memcpy(ah->qplib_ah.dgid.data, &sgid.raw,
  705. sizeof(union ib_gid));
  706. ah->qplib_ah.sgid_index = 0;
  707. ah->qplib_ah.traffic_class = 0;
  708. ah->qplib_ah.flow_label = 0;
  709. ah->qplib_ah.hop_limit = 1;
  710. ah->qplib_ah.sl = 0;
  711. /* Have DMAC same as SMAC */
  712. ether_addr_copy(ah->qplib_ah.dmac, rdev->netdev->dev_addr);
  713. rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah);
  714. if (rc) {
  715. dev_err(rdev_to_dev(rdev),
  716. "Failed to allocate HW AH for Shadow QP");
  717. goto fail;
  718. }
  719. return ah;
  720. fail:
  721. kfree(ah);
  722. return NULL;
  723. }
  724. static struct bnxt_re_qp *bnxt_re_create_shadow_qp
  725. (struct bnxt_re_pd *pd,
  726. struct bnxt_qplib_res *qp1_res,
  727. struct bnxt_qplib_qp *qp1_qp)
  728. {
  729. struct bnxt_re_dev *rdev = pd->rdev;
  730. struct bnxt_re_qp *qp;
  731. int rc;
  732. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  733. if (!qp)
  734. return NULL;
  735. memset(qp, 0, sizeof(*qp));
  736. qp->rdev = rdev;
  737. /* Initialize the shadow QP structure from the QP1 values */
  738. ether_addr_copy(qp->qplib_qp.smac, rdev->netdev->dev_addr);
  739. qp->qplib_qp.pd = &pd->qplib_pd;
  740. qp->qplib_qp.qp_handle = (u64)(unsigned long)(&qp->qplib_qp);
  741. qp->qplib_qp.type = IB_QPT_UD;
  742. qp->qplib_qp.max_inline_data = 0;
  743. qp->qplib_qp.sig_type = true;
  744. /* Shadow QP SQ depth should be same as QP1 RQ depth */
  745. qp->qplib_qp.sq.max_wqe = qp1_qp->rq.max_wqe;
  746. qp->qplib_qp.sq.max_sge = 2;
  747. qp->qplib_qp.scq = qp1_qp->scq;
  748. qp->qplib_qp.rcq = qp1_qp->rcq;
  749. qp->qplib_qp.rq.max_wqe = qp1_qp->rq.max_wqe;
  750. qp->qplib_qp.rq.max_sge = qp1_qp->rq.max_sge;
  751. qp->qplib_qp.mtu = qp1_qp->mtu;
  752. qp->qplib_qp.sq_hdr_buf_size = 0;
  753. qp->qplib_qp.rq_hdr_buf_size = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
  754. qp->qplib_qp.dpi = &rdev->dpi_privileged;
  755. rc = bnxt_qplib_create_qp(qp1_res, &qp->qplib_qp);
  756. if (rc)
  757. goto fail;
  758. rdev->sqp_id = qp->qplib_qp.id;
  759. spin_lock_init(&qp->sq_lock);
  760. INIT_LIST_HEAD(&qp->list);
  761. mutex_lock(&rdev->qp_lock);
  762. list_add_tail(&qp->list, &rdev->qp_list);
  763. atomic_inc(&rdev->qp_count);
  764. mutex_unlock(&rdev->qp_lock);
  765. return qp;
  766. fail:
  767. kfree(qp);
  768. return NULL;
  769. }
  770. struct ib_qp *bnxt_re_create_qp(struct ib_pd *ib_pd,
  771. struct ib_qp_init_attr *qp_init_attr,
  772. struct ib_udata *udata)
  773. {
  774. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  775. struct bnxt_re_dev *rdev = pd->rdev;
  776. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  777. struct bnxt_re_qp *qp;
  778. struct bnxt_re_cq *cq;
  779. int rc, entries;
  780. if ((qp_init_attr->cap.max_send_wr > dev_attr->max_qp_wqes) ||
  781. (qp_init_attr->cap.max_recv_wr > dev_attr->max_qp_wqes) ||
  782. (qp_init_attr->cap.max_send_sge > dev_attr->max_qp_sges) ||
  783. (qp_init_attr->cap.max_recv_sge > dev_attr->max_qp_sges) ||
  784. (qp_init_attr->cap.max_inline_data > dev_attr->max_inline_data))
  785. return ERR_PTR(-EINVAL);
  786. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  787. if (!qp)
  788. return ERR_PTR(-ENOMEM);
  789. qp->rdev = rdev;
  790. ether_addr_copy(qp->qplib_qp.smac, rdev->netdev->dev_addr);
  791. qp->qplib_qp.pd = &pd->qplib_pd;
  792. qp->qplib_qp.qp_handle = (u64)(unsigned long)(&qp->qplib_qp);
  793. qp->qplib_qp.type = __from_ib_qp_type(qp_init_attr->qp_type);
  794. if (qp->qplib_qp.type == IB_QPT_MAX) {
  795. dev_err(rdev_to_dev(rdev), "QP type 0x%x not supported",
  796. qp->qplib_qp.type);
  797. rc = -EINVAL;
  798. goto fail;
  799. }
  800. qp->qplib_qp.max_inline_data = qp_init_attr->cap.max_inline_data;
  801. qp->qplib_qp.sig_type = ((qp_init_attr->sq_sig_type ==
  802. IB_SIGNAL_ALL_WR) ? true : false);
  803. entries = roundup_pow_of_two(qp_init_attr->cap.max_send_wr + 1);
  804. qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
  805. dev_attr->max_qp_wqes + 1);
  806. qp->qplib_qp.sq.max_sge = qp_init_attr->cap.max_send_sge;
  807. if (qp->qplib_qp.sq.max_sge > dev_attr->max_qp_sges)
  808. qp->qplib_qp.sq.max_sge = dev_attr->max_qp_sges;
  809. if (qp_init_attr->send_cq) {
  810. cq = container_of(qp_init_attr->send_cq, struct bnxt_re_cq,
  811. ib_cq);
  812. if (!cq) {
  813. dev_err(rdev_to_dev(rdev), "Send CQ not found");
  814. rc = -EINVAL;
  815. goto fail;
  816. }
  817. qp->qplib_qp.scq = &cq->qplib_cq;
  818. }
  819. if (qp_init_attr->recv_cq) {
  820. cq = container_of(qp_init_attr->recv_cq, struct bnxt_re_cq,
  821. ib_cq);
  822. if (!cq) {
  823. dev_err(rdev_to_dev(rdev), "Receive CQ not found");
  824. rc = -EINVAL;
  825. goto fail;
  826. }
  827. qp->qplib_qp.rcq = &cq->qplib_cq;
  828. }
  829. if (qp_init_attr->srq) {
  830. dev_err(rdev_to_dev(rdev), "SRQ not supported");
  831. rc = -ENOTSUPP;
  832. goto fail;
  833. } else {
  834. /* Allocate 1 more than what's provided so posting max doesn't
  835. * mean empty
  836. */
  837. entries = roundup_pow_of_two(qp_init_attr->cap.max_recv_wr + 1);
  838. qp->qplib_qp.rq.max_wqe = min_t(u32, entries,
  839. dev_attr->max_qp_wqes + 1);
  840. qp->qplib_qp.rq.max_sge = qp_init_attr->cap.max_recv_sge;
  841. if (qp->qplib_qp.rq.max_sge > dev_attr->max_qp_sges)
  842. qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
  843. }
  844. qp->qplib_qp.mtu = ib_mtu_enum_to_int(iboe_get_mtu(rdev->netdev->mtu));
  845. if (qp_init_attr->qp_type == IB_QPT_GSI) {
  846. qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
  847. if (qp->qplib_qp.rq.max_sge > dev_attr->max_qp_sges)
  848. qp->qplib_qp.rq.max_sge = dev_attr->max_qp_sges;
  849. qp->qplib_qp.sq.max_sge++;
  850. if (qp->qplib_qp.sq.max_sge > dev_attr->max_qp_sges)
  851. qp->qplib_qp.sq.max_sge = dev_attr->max_qp_sges;
  852. qp->qplib_qp.rq_hdr_buf_size =
  853. BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
  854. qp->qplib_qp.sq_hdr_buf_size =
  855. BNXT_QPLIB_MAX_QP1_SQ_HDR_SIZE_V2;
  856. qp->qplib_qp.dpi = &rdev->dpi_privileged;
  857. rc = bnxt_qplib_create_qp1(&rdev->qplib_res, &qp->qplib_qp);
  858. if (rc) {
  859. dev_err(rdev_to_dev(rdev), "Failed to create HW QP1");
  860. goto fail;
  861. }
  862. /* Create a shadow QP to handle the QP1 traffic */
  863. rdev->qp1_sqp = bnxt_re_create_shadow_qp(pd, &rdev->qplib_res,
  864. &qp->qplib_qp);
  865. if (!rdev->qp1_sqp) {
  866. rc = -EINVAL;
  867. dev_err(rdev_to_dev(rdev),
  868. "Failed to create Shadow QP for QP1");
  869. goto qp_destroy;
  870. }
  871. rdev->sqp_ah = bnxt_re_create_shadow_qp_ah(pd, &rdev->qplib_res,
  872. &qp->qplib_qp);
  873. if (!rdev->sqp_ah) {
  874. bnxt_qplib_destroy_qp(&rdev->qplib_res,
  875. &rdev->qp1_sqp->qplib_qp);
  876. rc = -EINVAL;
  877. dev_err(rdev_to_dev(rdev),
  878. "Failed to create AH entry for ShadowQP");
  879. goto qp_destroy;
  880. }
  881. } else {
  882. qp->qplib_qp.max_rd_atomic = dev_attr->max_qp_rd_atom;
  883. qp->qplib_qp.max_dest_rd_atomic = dev_attr->max_qp_init_rd_atom;
  884. if (udata) {
  885. rc = bnxt_re_init_user_qp(rdev, pd, qp, udata);
  886. if (rc)
  887. goto fail;
  888. } else {
  889. qp->qplib_qp.dpi = &rdev->dpi_privileged;
  890. }
  891. rc = bnxt_qplib_create_qp(&rdev->qplib_res, &qp->qplib_qp);
  892. if (rc) {
  893. dev_err(rdev_to_dev(rdev), "Failed to create HW QP");
  894. goto fail;
  895. }
  896. }
  897. qp->ib_qp.qp_num = qp->qplib_qp.id;
  898. spin_lock_init(&qp->sq_lock);
  899. if (udata) {
  900. struct bnxt_re_qp_resp resp;
  901. resp.qpid = qp->ib_qp.qp_num;
  902. resp.rsvd = 0;
  903. rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
  904. if (rc) {
  905. dev_err(rdev_to_dev(rdev), "Failed to copy QP udata");
  906. goto qp_destroy;
  907. }
  908. }
  909. INIT_LIST_HEAD(&qp->list);
  910. mutex_lock(&rdev->qp_lock);
  911. list_add_tail(&qp->list, &rdev->qp_list);
  912. atomic_inc(&rdev->qp_count);
  913. mutex_unlock(&rdev->qp_lock);
  914. return &qp->ib_qp;
  915. qp_destroy:
  916. bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
  917. fail:
  918. kfree(qp);
  919. return ERR_PTR(rc);
  920. }
  921. static u8 __from_ib_qp_state(enum ib_qp_state state)
  922. {
  923. switch (state) {
  924. case IB_QPS_RESET:
  925. return CMDQ_MODIFY_QP_NEW_STATE_RESET;
  926. case IB_QPS_INIT:
  927. return CMDQ_MODIFY_QP_NEW_STATE_INIT;
  928. case IB_QPS_RTR:
  929. return CMDQ_MODIFY_QP_NEW_STATE_RTR;
  930. case IB_QPS_RTS:
  931. return CMDQ_MODIFY_QP_NEW_STATE_RTS;
  932. case IB_QPS_SQD:
  933. return CMDQ_MODIFY_QP_NEW_STATE_SQD;
  934. case IB_QPS_SQE:
  935. return CMDQ_MODIFY_QP_NEW_STATE_SQE;
  936. case IB_QPS_ERR:
  937. default:
  938. return CMDQ_MODIFY_QP_NEW_STATE_ERR;
  939. }
  940. }
  941. static enum ib_qp_state __to_ib_qp_state(u8 state)
  942. {
  943. switch (state) {
  944. case CMDQ_MODIFY_QP_NEW_STATE_RESET:
  945. return IB_QPS_RESET;
  946. case CMDQ_MODIFY_QP_NEW_STATE_INIT:
  947. return IB_QPS_INIT;
  948. case CMDQ_MODIFY_QP_NEW_STATE_RTR:
  949. return IB_QPS_RTR;
  950. case CMDQ_MODIFY_QP_NEW_STATE_RTS:
  951. return IB_QPS_RTS;
  952. case CMDQ_MODIFY_QP_NEW_STATE_SQD:
  953. return IB_QPS_SQD;
  954. case CMDQ_MODIFY_QP_NEW_STATE_SQE:
  955. return IB_QPS_SQE;
  956. case CMDQ_MODIFY_QP_NEW_STATE_ERR:
  957. default:
  958. return IB_QPS_ERR;
  959. }
  960. }
  961. static u32 __from_ib_mtu(enum ib_mtu mtu)
  962. {
  963. switch (mtu) {
  964. case IB_MTU_256:
  965. return CMDQ_MODIFY_QP_PATH_MTU_MTU_256;
  966. case IB_MTU_512:
  967. return CMDQ_MODIFY_QP_PATH_MTU_MTU_512;
  968. case IB_MTU_1024:
  969. return CMDQ_MODIFY_QP_PATH_MTU_MTU_1024;
  970. case IB_MTU_2048:
  971. return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
  972. case IB_MTU_4096:
  973. return CMDQ_MODIFY_QP_PATH_MTU_MTU_4096;
  974. default:
  975. return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
  976. }
  977. }
  978. static enum ib_mtu __to_ib_mtu(u32 mtu)
  979. {
  980. switch (mtu & CREQ_QUERY_QP_RESP_SB_PATH_MTU_MASK) {
  981. case CMDQ_MODIFY_QP_PATH_MTU_MTU_256:
  982. return IB_MTU_256;
  983. case CMDQ_MODIFY_QP_PATH_MTU_MTU_512:
  984. return IB_MTU_512;
  985. case CMDQ_MODIFY_QP_PATH_MTU_MTU_1024:
  986. return IB_MTU_1024;
  987. case CMDQ_MODIFY_QP_PATH_MTU_MTU_2048:
  988. return IB_MTU_2048;
  989. case CMDQ_MODIFY_QP_PATH_MTU_MTU_4096:
  990. return IB_MTU_4096;
  991. default:
  992. return IB_MTU_2048;
  993. }
  994. }
  995. static int __from_ib_access_flags(int iflags)
  996. {
  997. int qflags = 0;
  998. if (iflags & IB_ACCESS_LOCAL_WRITE)
  999. qflags |= BNXT_QPLIB_ACCESS_LOCAL_WRITE;
  1000. if (iflags & IB_ACCESS_REMOTE_READ)
  1001. qflags |= BNXT_QPLIB_ACCESS_REMOTE_READ;
  1002. if (iflags & IB_ACCESS_REMOTE_WRITE)
  1003. qflags |= BNXT_QPLIB_ACCESS_REMOTE_WRITE;
  1004. if (iflags & IB_ACCESS_REMOTE_ATOMIC)
  1005. qflags |= BNXT_QPLIB_ACCESS_REMOTE_ATOMIC;
  1006. if (iflags & IB_ACCESS_MW_BIND)
  1007. qflags |= BNXT_QPLIB_ACCESS_MW_BIND;
  1008. if (iflags & IB_ZERO_BASED)
  1009. qflags |= BNXT_QPLIB_ACCESS_ZERO_BASED;
  1010. if (iflags & IB_ACCESS_ON_DEMAND)
  1011. qflags |= BNXT_QPLIB_ACCESS_ON_DEMAND;
  1012. return qflags;
  1013. };
  1014. static enum ib_access_flags __to_ib_access_flags(int qflags)
  1015. {
  1016. enum ib_access_flags iflags = 0;
  1017. if (qflags & BNXT_QPLIB_ACCESS_LOCAL_WRITE)
  1018. iflags |= IB_ACCESS_LOCAL_WRITE;
  1019. if (qflags & BNXT_QPLIB_ACCESS_REMOTE_WRITE)
  1020. iflags |= IB_ACCESS_REMOTE_WRITE;
  1021. if (qflags & BNXT_QPLIB_ACCESS_REMOTE_READ)
  1022. iflags |= IB_ACCESS_REMOTE_READ;
  1023. if (qflags & BNXT_QPLIB_ACCESS_REMOTE_ATOMIC)
  1024. iflags |= IB_ACCESS_REMOTE_ATOMIC;
  1025. if (qflags & BNXT_QPLIB_ACCESS_MW_BIND)
  1026. iflags |= IB_ACCESS_MW_BIND;
  1027. if (qflags & BNXT_QPLIB_ACCESS_ZERO_BASED)
  1028. iflags |= IB_ZERO_BASED;
  1029. if (qflags & BNXT_QPLIB_ACCESS_ON_DEMAND)
  1030. iflags |= IB_ACCESS_ON_DEMAND;
  1031. return iflags;
  1032. };
  1033. static int bnxt_re_modify_shadow_qp(struct bnxt_re_dev *rdev,
  1034. struct bnxt_re_qp *qp1_qp,
  1035. int qp_attr_mask)
  1036. {
  1037. struct bnxt_re_qp *qp = rdev->qp1_sqp;
  1038. int rc = 0;
  1039. if (qp_attr_mask & IB_QP_STATE) {
  1040. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
  1041. qp->qplib_qp.state = qp1_qp->qplib_qp.state;
  1042. }
  1043. if (qp_attr_mask & IB_QP_PKEY_INDEX) {
  1044. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
  1045. qp->qplib_qp.pkey_index = qp1_qp->qplib_qp.pkey_index;
  1046. }
  1047. if (qp_attr_mask & IB_QP_QKEY) {
  1048. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
  1049. /* Using a Random QKEY */
  1050. qp->qplib_qp.qkey = 0x81818181;
  1051. }
  1052. if (qp_attr_mask & IB_QP_SQ_PSN) {
  1053. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
  1054. qp->qplib_qp.sq.psn = qp1_qp->qplib_qp.sq.psn;
  1055. }
  1056. rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
  1057. if (rc)
  1058. dev_err(rdev_to_dev(rdev),
  1059. "Failed to modify Shadow QP for QP1");
  1060. return rc;
  1061. }
  1062. int bnxt_re_modify_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
  1063. int qp_attr_mask, struct ib_udata *udata)
  1064. {
  1065. struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
  1066. struct bnxt_re_dev *rdev = qp->rdev;
  1067. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  1068. enum ib_qp_state curr_qp_state, new_qp_state;
  1069. int rc, entries;
  1070. int status;
  1071. union ib_gid sgid;
  1072. struct ib_gid_attr sgid_attr;
  1073. u8 nw_type;
  1074. qp->qplib_qp.modify_flags = 0;
  1075. if (qp_attr_mask & IB_QP_STATE) {
  1076. curr_qp_state = __to_ib_qp_state(qp->qplib_qp.cur_qp_state);
  1077. new_qp_state = qp_attr->qp_state;
  1078. if (!ib_modify_qp_is_ok(curr_qp_state, new_qp_state,
  1079. ib_qp->qp_type, qp_attr_mask,
  1080. IB_LINK_LAYER_ETHERNET)) {
  1081. dev_err(rdev_to_dev(rdev),
  1082. "Invalid attribute mask: %#x specified ",
  1083. qp_attr_mask);
  1084. dev_err(rdev_to_dev(rdev),
  1085. "for qpn: %#x type: %#x",
  1086. ib_qp->qp_num, ib_qp->qp_type);
  1087. dev_err(rdev_to_dev(rdev),
  1088. "curr_qp_state=0x%x, new_qp_state=0x%x\n",
  1089. curr_qp_state, new_qp_state);
  1090. return -EINVAL;
  1091. }
  1092. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
  1093. qp->qplib_qp.state = __from_ib_qp_state(qp_attr->qp_state);
  1094. }
  1095. if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) {
  1096. qp->qplib_qp.modify_flags |=
  1097. CMDQ_MODIFY_QP_MODIFY_MASK_EN_SQD_ASYNC_NOTIFY;
  1098. qp->qplib_qp.en_sqd_async_notify = true;
  1099. }
  1100. if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
  1101. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_ACCESS;
  1102. qp->qplib_qp.access =
  1103. __from_ib_access_flags(qp_attr->qp_access_flags);
  1104. /* LOCAL_WRITE access must be set to allow RC receive */
  1105. qp->qplib_qp.access |= BNXT_QPLIB_ACCESS_LOCAL_WRITE;
  1106. }
  1107. if (qp_attr_mask & IB_QP_PKEY_INDEX) {
  1108. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
  1109. qp->qplib_qp.pkey_index = qp_attr->pkey_index;
  1110. }
  1111. if (qp_attr_mask & IB_QP_QKEY) {
  1112. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
  1113. qp->qplib_qp.qkey = qp_attr->qkey;
  1114. }
  1115. if (qp_attr_mask & IB_QP_AV) {
  1116. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_DGID |
  1117. CMDQ_MODIFY_QP_MODIFY_MASK_FLOW_LABEL |
  1118. CMDQ_MODIFY_QP_MODIFY_MASK_SGID_INDEX |
  1119. CMDQ_MODIFY_QP_MODIFY_MASK_HOP_LIMIT |
  1120. CMDQ_MODIFY_QP_MODIFY_MASK_TRAFFIC_CLASS |
  1121. CMDQ_MODIFY_QP_MODIFY_MASK_DEST_MAC |
  1122. CMDQ_MODIFY_QP_MODIFY_MASK_VLAN_ID;
  1123. memcpy(qp->qplib_qp.ah.dgid.data, qp_attr->ah_attr.grh.dgid.raw,
  1124. sizeof(qp->qplib_qp.ah.dgid.data));
  1125. qp->qplib_qp.ah.flow_label = qp_attr->ah_attr.grh.flow_label;
  1126. /* If RoCE V2 is enabled, stack will have two entries for
  1127. * each GID entry. Avoiding this duplicte entry in HW. Dividing
  1128. * the GID index by 2 for RoCE V2
  1129. */
  1130. qp->qplib_qp.ah.sgid_index =
  1131. qp_attr->ah_attr.grh.sgid_index / 2;
  1132. qp->qplib_qp.ah.host_sgid_index =
  1133. qp_attr->ah_attr.grh.sgid_index;
  1134. qp->qplib_qp.ah.hop_limit = qp_attr->ah_attr.grh.hop_limit;
  1135. qp->qplib_qp.ah.traffic_class =
  1136. qp_attr->ah_attr.grh.traffic_class;
  1137. qp->qplib_qp.ah.sl = qp_attr->ah_attr.sl;
  1138. ether_addr_copy(qp->qplib_qp.ah.dmac, qp_attr->ah_attr.dmac);
  1139. status = ib_get_cached_gid(&rdev->ibdev, 1,
  1140. qp_attr->ah_attr.grh.sgid_index,
  1141. &sgid, &sgid_attr);
  1142. if (!status && sgid_attr.ndev) {
  1143. memcpy(qp->qplib_qp.smac, sgid_attr.ndev->dev_addr,
  1144. ETH_ALEN);
  1145. dev_put(sgid_attr.ndev);
  1146. nw_type = ib_gid_to_network_type(sgid_attr.gid_type,
  1147. &sgid);
  1148. switch (nw_type) {
  1149. case RDMA_NETWORK_IPV4:
  1150. qp->qplib_qp.nw_type =
  1151. CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV4;
  1152. break;
  1153. case RDMA_NETWORK_IPV6:
  1154. qp->qplib_qp.nw_type =
  1155. CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV6;
  1156. break;
  1157. default:
  1158. qp->qplib_qp.nw_type =
  1159. CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV1;
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. if (qp_attr_mask & IB_QP_PATH_MTU) {
  1165. qp->qplib_qp.modify_flags |=
  1166. CMDQ_MODIFY_QP_MODIFY_MASK_PATH_MTU;
  1167. qp->qplib_qp.path_mtu = __from_ib_mtu(qp_attr->path_mtu);
  1168. } else if (qp_attr->qp_state == IB_QPS_RTR) {
  1169. qp->qplib_qp.modify_flags |=
  1170. CMDQ_MODIFY_QP_MODIFY_MASK_PATH_MTU;
  1171. qp->qplib_qp.path_mtu =
  1172. __from_ib_mtu(iboe_get_mtu(rdev->netdev->mtu));
  1173. }
  1174. if (qp_attr_mask & IB_QP_TIMEOUT) {
  1175. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_TIMEOUT;
  1176. qp->qplib_qp.timeout = qp_attr->timeout;
  1177. }
  1178. if (qp_attr_mask & IB_QP_RETRY_CNT) {
  1179. qp->qplib_qp.modify_flags |=
  1180. CMDQ_MODIFY_QP_MODIFY_MASK_RETRY_CNT;
  1181. qp->qplib_qp.retry_cnt = qp_attr->retry_cnt;
  1182. }
  1183. if (qp_attr_mask & IB_QP_RNR_RETRY) {
  1184. qp->qplib_qp.modify_flags |=
  1185. CMDQ_MODIFY_QP_MODIFY_MASK_RNR_RETRY;
  1186. qp->qplib_qp.rnr_retry = qp_attr->rnr_retry;
  1187. }
  1188. if (qp_attr_mask & IB_QP_MIN_RNR_TIMER) {
  1189. qp->qplib_qp.modify_flags |=
  1190. CMDQ_MODIFY_QP_MODIFY_MASK_MIN_RNR_TIMER;
  1191. qp->qplib_qp.min_rnr_timer = qp_attr->min_rnr_timer;
  1192. }
  1193. if (qp_attr_mask & IB_QP_RQ_PSN) {
  1194. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_RQ_PSN;
  1195. qp->qplib_qp.rq.psn = qp_attr->rq_psn;
  1196. }
  1197. if (qp_attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1198. qp->qplib_qp.modify_flags |=
  1199. CMDQ_MODIFY_QP_MODIFY_MASK_MAX_RD_ATOMIC;
  1200. qp->qplib_qp.max_rd_atomic = qp_attr->max_rd_atomic;
  1201. }
  1202. if (qp_attr_mask & IB_QP_SQ_PSN) {
  1203. qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
  1204. qp->qplib_qp.sq.psn = qp_attr->sq_psn;
  1205. }
  1206. if (qp_attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1207. qp->qplib_qp.modify_flags |=
  1208. CMDQ_MODIFY_QP_MODIFY_MASK_MAX_DEST_RD_ATOMIC;
  1209. qp->qplib_qp.max_dest_rd_atomic = qp_attr->max_dest_rd_atomic;
  1210. }
  1211. if (qp_attr_mask & IB_QP_CAP) {
  1212. qp->qplib_qp.modify_flags |=
  1213. CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SIZE |
  1214. CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SIZE |
  1215. CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SGE |
  1216. CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SGE |
  1217. CMDQ_MODIFY_QP_MODIFY_MASK_MAX_INLINE_DATA;
  1218. if ((qp_attr->cap.max_send_wr >= dev_attr->max_qp_wqes) ||
  1219. (qp_attr->cap.max_recv_wr >= dev_attr->max_qp_wqes) ||
  1220. (qp_attr->cap.max_send_sge >= dev_attr->max_qp_sges) ||
  1221. (qp_attr->cap.max_recv_sge >= dev_attr->max_qp_sges) ||
  1222. (qp_attr->cap.max_inline_data >=
  1223. dev_attr->max_inline_data)) {
  1224. dev_err(rdev_to_dev(rdev),
  1225. "Create QP failed - max exceeded");
  1226. return -EINVAL;
  1227. }
  1228. entries = roundup_pow_of_two(qp_attr->cap.max_send_wr);
  1229. qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
  1230. dev_attr->max_qp_wqes + 1);
  1231. qp->qplib_qp.sq.max_sge = qp_attr->cap.max_send_sge;
  1232. if (qp->qplib_qp.rq.max_wqe) {
  1233. entries = roundup_pow_of_two(qp_attr->cap.max_recv_wr);
  1234. qp->qplib_qp.rq.max_wqe =
  1235. min_t(u32, entries, dev_attr->max_qp_wqes + 1);
  1236. qp->qplib_qp.rq.max_sge = qp_attr->cap.max_recv_sge;
  1237. } else {
  1238. /* SRQ was used prior, just ignore the RQ caps */
  1239. }
  1240. }
  1241. if (qp_attr_mask & IB_QP_DEST_QPN) {
  1242. qp->qplib_qp.modify_flags |=
  1243. CMDQ_MODIFY_QP_MODIFY_MASK_DEST_QP_ID;
  1244. qp->qplib_qp.dest_qpn = qp_attr->dest_qp_num;
  1245. }
  1246. rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
  1247. if (rc) {
  1248. dev_err(rdev_to_dev(rdev), "Failed to modify HW QP");
  1249. return rc;
  1250. }
  1251. if (ib_qp->qp_type == IB_QPT_GSI && rdev->qp1_sqp)
  1252. rc = bnxt_re_modify_shadow_qp(rdev, qp, qp_attr_mask);
  1253. return rc;
  1254. }
  1255. int bnxt_re_query_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
  1256. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1257. {
  1258. struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
  1259. struct bnxt_re_dev *rdev = qp->rdev;
  1260. struct bnxt_qplib_qp qplib_qp;
  1261. int rc;
  1262. memset(&qplib_qp, 0, sizeof(struct bnxt_qplib_qp));
  1263. qplib_qp.id = qp->qplib_qp.id;
  1264. qplib_qp.ah.host_sgid_index = qp->qplib_qp.ah.host_sgid_index;
  1265. rc = bnxt_qplib_query_qp(&rdev->qplib_res, &qplib_qp);
  1266. if (rc) {
  1267. dev_err(rdev_to_dev(rdev), "Failed to query HW QP");
  1268. return rc;
  1269. }
  1270. qp_attr->qp_state = __to_ib_qp_state(qplib_qp.state);
  1271. qp_attr->en_sqd_async_notify = qplib_qp.en_sqd_async_notify ? 1 : 0;
  1272. qp_attr->qp_access_flags = __to_ib_access_flags(qplib_qp.access);
  1273. qp_attr->pkey_index = qplib_qp.pkey_index;
  1274. qp_attr->qkey = qplib_qp.qkey;
  1275. memcpy(qp_attr->ah_attr.grh.dgid.raw, qplib_qp.ah.dgid.data,
  1276. sizeof(qplib_qp.ah.dgid.data));
  1277. qp_attr->ah_attr.grh.flow_label = qplib_qp.ah.flow_label;
  1278. qp_attr->ah_attr.grh.sgid_index = qplib_qp.ah.host_sgid_index;
  1279. qp_attr->ah_attr.grh.hop_limit = qplib_qp.ah.hop_limit;
  1280. qp_attr->ah_attr.grh.traffic_class = qplib_qp.ah.traffic_class;
  1281. qp_attr->ah_attr.sl = qplib_qp.ah.sl;
  1282. ether_addr_copy(qp_attr->ah_attr.dmac, qplib_qp.ah.dmac);
  1283. qp_attr->path_mtu = __to_ib_mtu(qplib_qp.path_mtu);
  1284. qp_attr->timeout = qplib_qp.timeout;
  1285. qp_attr->retry_cnt = qplib_qp.retry_cnt;
  1286. qp_attr->rnr_retry = qplib_qp.rnr_retry;
  1287. qp_attr->min_rnr_timer = qplib_qp.min_rnr_timer;
  1288. qp_attr->rq_psn = qplib_qp.rq.psn;
  1289. qp_attr->max_rd_atomic = qplib_qp.max_rd_atomic;
  1290. qp_attr->sq_psn = qplib_qp.sq.psn;
  1291. qp_attr->max_dest_rd_atomic = qplib_qp.max_dest_rd_atomic;
  1292. qp_init_attr->sq_sig_type = qplib_qp.sig_type ? IB_SIGNAL_ALL_WR :
  1293. IB_SIGNAL_REQ_WR;
  1294. qp_attr->dest_qp_num = qplib_qp.dest_qpn;
  1295. qp_attr->cap.max_send_wr = qp->qplib_qp.sq.max_wqe;
  1296. qp_attr->cap.max_send_sge = qp->qplib_qp.sq.max_sge;
  1297. qp_attr->cap.max_recv_wr = qp->qplib_qp.rq.max_wqe;
  1298. qp_attr->cap.max_recv_sge = qp->qplib_qp.rq.max_sge;
  1299. qp_attr->cap.max_inline_data = qp->qplib_qp.max_inline_data;
  1300. qp_init_attr->cap = qp_attr->cap;
  1301. return 0;
  1302. }
  1303. /* Routine for sending QP1 packets for RoCE V1 an V2
  1304. */
  1305. static int bnxt_re_build_qp1_send_v2(struct bnxt_re_qp *qp,
  1306. struct ib_send_wr *wr,
  1307. struct bnxt_qplib_swqe *wqe,
  1308. int payload_size)
  1309. {
  1310. struct ib_device *ibdev = &qp->rdev->ibdev;
  1311. struct bnxt_re_ah *ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah,
  1312. ib_ah);
  1313. struct bnxt_qplib_ah *qplib_ah = &ah->qplib_ah;
  1314. struct bnxt_qplib_sge sge;
  1315. union ib_gid sgid;
  1316. u8 nw_type;
  1317. u16 ether_type;
  1318. struct ib_gid_attr sgid_attr;
  1319. union ib_gid dgid;
  1320. bool is_eth = false;
  1321. bool is_vlan = false;
  1322. bool is_grh = false;
  1323. bool is_udp = false;
  1324. u8 ip_version = 0;
  1325. u16 vlan_id = 0xFFFF;
  1326. void *buf;
  1327. int i, rc = 0, size;
  1328. memset(&qp->qp1_hdr, 0, sizeof(qp->qp1_hdr));
  1329. rc = ib_get_cached_gid(ibdev, 1,
  1330. qplib_ah->host_sgid_index, &sgid,
  1331. &sgid_attr);
  1332. if (rc) {
  1333. dev_err(rdev_to_dev(qp->rdev),
  1334. "Failed to query gid at index %d",
  1335. qplib_ah->host_sgid_index);
  1336. return rc;
  1337. }
  1338. if (sgid_attr.ndev) {
  1339. if (is_vlan_dev(sgid_attr.ndev))
  1340. vlan_id = vlan_dev_vlan_id(sgid_attr.ndev);
  1341. dev_put(sgid_attr.ndev);
  1342. }
  1343. /* Get network header type for this GID */
  1344. nw_type = ib_gid_to_network_type(sgid_attr.gid_type, &sgid);
  1345. switch (nw_type) {
  1346. case RDMA_NETWORK_IPV4:
  1347. nw_type = BNXT_RE_ROCEV2_IPV4_PACKET;
  1348. break;
  1349. case RDMA_NETWORK_IPV6:
  1350. nw_type = BNXT_RE_ROCEV2_IPV6_PACKET;
  1351. break;
  1352. default:
  1353. nw_type = BNXT_RE_ROCE_V1_PACKET;
  1354. break;
  1355. }
  1356. memcpy(&dgid.raw, &qplib_ah->dgid, 16);
  1357. is_udp = sgid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  1358. if (is_udp) {
  1359. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid)) {
  1360. ip_version = 4;
  1361. ether_type = ETH_P_IP;
  1362. } else {
  1363. ip_version = 6;
  1364. ether_type = ETH_P_IPV6;
  1365. }
  1366. is_grh = false;
  1367. } else {
  1368. ether_type = ETH_P_IBOE;
  1369. is_grh = true;
  1370. }
  1371. is_eth = true;
  1372. is_vlan = (vlan_id && (vlan_id < 0x1000)) ? true : false;
  1373. ib_ud_header_init(payload_size, !is_eth, is_eth, is_vlan, is_grh,
  1374. ip_version, is_udp, 0, &qp->qp1_hdr);
  1375. /* ETH */
  1376. ether_addr_copy(qp->qp1_hdr.eth.dmac_h, ah->qplib_ah.dmac);
  1377. ether_addr_copy(qp->qp1_hdr.eth.smac_h, qp->qplib_qp.smac);
  1378. /* For vlan, check the sgid for vlan existence */
  1379. if (!is_vlan) {
  1380. qp->qp1_hdr.eth.type = cpu_to_be16(ether_type);
  1381. } else {
  1382. qp->qp1_hdr.vlan.type = cpu_to_be16(ether_type);
  1383. qp->qp1_hdr.vlan.tag = cpu_to_be16(vlan_id);
  1384. }
  1385. if (is_grh || (ip_version == 6)) {
  1386. memcpy(qp->qp1_hdr.grh.source_gid.raw, sgid.raw, sizeof(sgid));
  1387. memcpy(qp->qp1_hdr.grh.destination_gid.raw, qplib_ah->dgid.data,
  1388. sizeof(sgid));
  1389. qp->qp1_hdr.grh.hop_limit = qplib_ah->hop_limit;
  1390. }
  1391. if (ip_version == 4) {
  1392. qp->qp1_hdr.ip4.tos = 0;
  1393. qp->qp1_hdr.ip4.id = 0;
  1394. qp->qp1_hdr.ip4.frag_off = htons(IP_DF);
  1395. qp->qp1_hdr.ip4.ttl = qplib_ah->hop_limit;
  1396. memcpy(&qp->qp1_hdr.ip4.saddr, sgid.raw + 12, 4);
  1397. memcpy(&qp->qp1_hdr.ip4.daddr, qplib_ah->dgid.data + 12, 4);
  1398. qp->qp1_hdr.ip4.check = ib_ud_ip4_csum(&qp->qp1_hdr);
  1399. }
  1400. if (is_udp) {
  1401. qp->qp1_hdr.udp.dport = htons(ROCE_V2_UDP_DPORT);
  1402. qp->qp1_hdr.udp.sport = htons(0x8CD1);
  1403. qp->qp1_hdr.udp.csum = 0;
  1404. }
  1405. /* BTH */
  1406. if (wr->opcode == IB_WR_SEND_WITH_IMM) {
  1407. qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1408. qp->qp1_hdr.immediate_present = 1;
  1409. } else {
  1410. qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1411. }
  1412. if (wr->send_flags & IB_SEND_SOLICITED)
  1413. qp->qp1_hdr.bth.solicited_event = 1;
  1414. /* pad_count */
  1415. qp->qp1_hdr.bth.pad_count = (4 - payload_size) & 3;
  1416. /* P_key for QP1 is for all members */
  1417. qp->qp1_hdr.bth.pkey = cpu_to_be16(0xFFFF);
  1418. qp->qp1_hdr.bth.destination_qpn = IB_QP1;
  1419. qp->qp1_hdr.bth.ack_req = 0;
  1420. qp->send_psn++;
  1421. qp->send_psn &= BTH_PSN_MASK;
  1422. qp->qp1_hdr.bth.psn = cpu_to_be32(qp->send_psn);
  1423. /* DETH */
  1424. /* Use the priviledged Q_Key for QP1 */
  1425. qp->qp1_hdr.deth.qkey = cpu_to_be32(IB_QP1_QKEY);
  1426. qp->qp1_hdr.deth.source_qpn = IB_QP1;
  1427. /* Pack the QP1 to the transmit buffer */
  1428. buf = bnxt_qplib_get_qp1_sq_buf(&qp->qplib_qp, &sge);
  1429. if (buf) {
  1430. size = ib_ud_header_pack(&qp->qp1_hdr, buf);
  1431. for (i = wqe->num_sge; i; i--) {
  1432. wqe->sg_list[i].addr = wqe->sg_list[i - 1].addr;
  1433. wqe->sg_list[i].lkey = wqe->sg_list[i - 1].lkey;
  1434. wqe->sg_list[i].size = wqe->sg_list[i - 1].size;
  1435. }
  1436. /*
  1437. * Max Header buf size for IPV6 RoCE V2 is 86,
  1438. * which is same as the QP1 SQ header buffer.
  1439. * Header buf size for IPV4 RoCE V2 can be 66.
  1440. * ETH(14) + VLAN(4)+ IP(20) + UDP (8) + BTH(20).
  1441. * Subtract 20 bytes from QP1 SQ header buf size
  1442. */
  1443. if (is_udp && ip_version == 4)
  1444. sge.size -= 20;
  1445. /*
  1446. * Max Header buf size for RoCE V1 is 78.
  1447. * ETH(14) + VLAN(4) + GRH(40) + BTH(20).
  1448. * Subtract 8 bytes from QP1 SQ header buf size
  1449. */
  1450. if (!is_udp)
  1451. sge.size -= 8;
  1452. /* Subtract 4 bytes for non vlan packets */
  1453. if (!is_vlan)
  1454. sge.size -= 4;
  1455. wqe->sg_list[0].addr = sge.addr;
  1456. wqe->sg_list[0].lkey = sge.lkey;
  1457. wqe->sg_list[0].size = sge.size;
  1458. wqe->num_sge++;
  1459. } else {
  1460. dev_err(rdev_to_dev(qp->rdev), "QP1 buffer is empty!");
  1461. rc = -ENOMEM;
  1462. }
  1463. return rc;
  1464. }
  1465. /* For the MAD layer, it only provides the recv SGE the size of
  1466. * ib_grh + MAD datagram. No Ethernet headers, Ethertype, BTH, DETH,
  1467. * nor RoCE iCRC. The Cu+ solution must provide buffer for the entire
  1468. * receive packet (334 bytes) with no VLAN and then copy the GRH
  1469. * and the MAD datagram out to the provided SGE.
  1470. */
  1471. static int bnxt_re_build_qp1_shadow_qp_recv(struct bnxt_re_qp *qp,
  1472. struct ib_recv_wr *wr,
  1473. struct bnxt_qplib_swqe *wqe,
  1474. int payload_size)
  1475. {
  1476. struct bnxt_qplib_sge ref, sge;
  1477. u32 rq_prod_index;
  1478. struct bnxt_re_sqp_entries *sqp_entry;
  1479. rq_prod_index = bnxt_qplib_get_rq_prod_index(&qp->qplib_qp);
  1480. if (!bnxt_qplib_get_qp1_rq_buf(&qp->qplib_qp, &sge))
  1481. return -ENOMEM;
  1482. /* Create 1 SGE to receive the entire
  1483. * ethernet packet
  1484. */
  1485. /* Save the reference from ULP */
  1486. ref.addr = wqe->sg_list[0].addr;
  1487. ref.lkey = wqe->sg_list[0].lkey;
  1488. ref.size = wqe->sg_list[0].size;
  1489. sqp_entry = &qp->rdev->sqp_tbl[rq_prod_index];
  1490. /* SGE 1 */
  1491. wqe->sg_list[0].addr = sge.addr;
  1492. wqe->sg_list[0].lkey = sge.lkey;
  1493. wqe->sg_list[0].size = BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
  1494. sge.size -= wqe->sg_list[0].size;
  1495. sqp_entry->sge.addr = ref.addr;
  1496. sqp_entry->sge.lkey = ref.lkey;
  1497. sqp_entry->sge.size = ref.size;
  1498. /* Store the wrid for reporting completion */
  1499. sqp_entry->wrid = wqe->wr_id;
  1500. /* change the wqe->wrid to table index */
  1501. wqe->wr_id = rq_prod_index;
  1502. return 0;
  1503. }
  1504. static int is_ud_qp(struct bnxt_re_qp *qp)
  1505. {
  1506. return qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_UD;
  1507. }
  1508. static int bnxt_re_build_send_wqe(struct bnxt_re_qp *qp,
  1509. struct ib_send_wr *wr,
  1510. struct bnxt_qplib_swqe *wqe)
  1511. {
  1512. struct bnxt_re_ah *ah = NULL;
  1513. if (is_ud_qp(qp)) {
  1514. ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah, ib_ah);
  1515. wqe->send.q_key = ud_wr(wr)->remote_qkey;
  1516. wqe->send.dst_qp = ud_wr(wr)->remote_qpn;
  1517. wqe->send.avid = ah->qplib_ah.id;
  1518. }
  1519. switch (wr->opcode) {
  1520. case IB_WR_SEND:
  1521. wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND;
  1522. break;
  1523. case IB_WR_SEND_WITH_IMM:
  1524. wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM;
  1525. wqe->send.imm_data = wr->ex.imm_data;
  1526. break;
  1527. case IB_WR_SEND_WITH_INV:
  1528. wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV;
  1529. wqe->send.inv_key = wr->ex.invalidate_rkey;
  1530. break;
  1531. default:
  1532. return -EINVAL;
  1533. }
  1534. if (wr->send_flags & IB_SEND_SIGNALED)
  1535. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
  1536. if (wr->send_flags & IB_SEND_FENCE)
  1537. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
  1538. if (wr->send_flags & IB_SEND_SOLICITED)
  1539. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
  1540. if (wr->send_flags & IB_SEND_INLINE)
  1541. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
  1542. return 0;
  1543. }
  1544. static int bnxt_re_build_rdma_wqe(struct ib_send_wr *wr,
  1545. struct bnxt_qplib_swqe *wqe)
  1546. {
  1547. switch (wr->opcode) {
  1548. case IB_WR_RDMA_WRITE:
  1549. wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE;
  1550. break;
  1551. case IB_WR_RDMA_WRITE_WITH_IMM:
  1552. wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM;
  1553. wqe->rdma.imm_data = wr->ex.imm_data;
  1554. break;
  1555. case IB_WR_RDMA_READ:
  1556. wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_READ;
  1557. wqe->rdma.inv_key = wr->ex.invalidate_rkey;
  1558. break;
  1559. default:
  1560. return -EINVAL;
  1561. }
  1562. wqe->rdma.remote_va = rdma_wr(wr)->remote_addr;
  1563. wqe->rdma.r_key = rdma_wr(wr)->rkey;
  1564. if (wr->send_flags & IB_SEND_SIGNALED)
  1565. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
  1566. if (wr->send_flags & IB_SEND_FENCE)
  1567. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
  1568. if (wr->send_flags & IB_SEND_SOLICITED)
  1569. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
  1570. if (wr->send_flags & IB_SEND_INLINE)
  1571. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
  1572. return 0;
  1573. }
  1574. static int bnxt_re_build_atomic_wqe(struct ib_send_wr *wr,
  1575. struct bnxt_qplib_swqe *wqe)
  1576. {
  1577. switch (wr->opcode) {
  1578. case IB_WR_ATOMIC_CMP_AND_SWP:
  1579. wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP;
  1580. wqe->atomic.swap_data = atomic_wr(wr)->swap;
  1581. break;
  1582. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1583. wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD;
  1584. wqe->atomic.cmp_data = atomic_wr(wr)->compare_add;
  1585. break;
  1586. default:
  1587. return -EINVAL;
  1588. }
  1589. wqe->atomic.remote_va = atomic_wr(wr)->remote_addr;
  1590. wqe->atomic.r_key = atomic_wr(wr)->rkey;
  1591. if (wr->send_flags & IB_SEND_SIGNALED)
  1592. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
  1593. if (wr->send_flags & IB_SEND_FENCE)
  1594. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
  1595. if (wr->send_flags & IB_SEND_SOLICITED)
  1596. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
  1597. return 0;
  1598. }
  1599. static int bnxt_re_build_inv_wqe(struct ib_send_wr *wr,
  1600. struct bnxt_qplib_swqe *wqe)
  1601. {
  1602. wqe->type = BNXT_QPLIB_SWQE_TYPE_LOCAL_INV;
  1603. wqe->local_inv.inv_l_key = wr->ex.invalidate_rkey;
  1604. if (wr->send_flags & IB_SEND_SIGNALED)
  1605. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
  1606. if (wr->send_flags & IB_SEND_FENCE)
  1607. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
  1608. if (wr->send_flags & IB_SEND_SOLICITED)
  1609. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
  1610. return 0;
  1611. }
  1612. static int bnxt_re_build_reg_wqe(struct ib_reg_wr *wr,
  1613. struct bnxt_qplib_swqe *wqe)
  1614. {
  1615. struct bnxt_re_mr *mr = container_of(wr->mr, struct bnxt_re_mr, ib_mr);
  1616. struct bnxt_qplib_frpl *qplib_frpl = &mr->qplib_frpl;
  1617. int access = wr->access;
  1618. wqe->frmr.pbl_ptr = (__le64 *)qplib_frpl->hwq.pbl_ptr[0];
  1619. wqe->frmr.pbl_dma_ptr = qplib_frpl->hwq.pbl_dma_ptr[0];
  1620. wqe->frmr.page_list = mr->pages;
  1621. wqe->frmr.page_list_len = mr->npages;
  1622. wqe->frmr.levels = qplib_frpl->hwq.level + 1;
  1623. wqe->type = BNXT_QPLIB_SWQE_TYPE_REG_MR;
  1624. if (wr->wr.send_flags & IB_SEND_FENCE)
  1625. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
  1626. if (wr->wr.send_flags & IB_SEND_SIGNALED)
  1627. wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
  1628. if (access & IB_ACCESS_LOCAL_WRITE)
  1629. wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_LOCAL_WRITE;
  1630. if (access & IB_ACCESS_REMOTE_READ)
  1631. wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_READ;
  1632. if (access & IB_ACCESS_REMOTE_WRITE)
  1633. wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_WRITE;
  1634. if (access & IB_ACCESS_REMOTE_ATOMIC)
  1635. wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_ATOMIC;
  1636. if (access & IB_ACCESS_MW_BIND)
  1637. wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_WINDOW_BIND;
  1638. wqe->frmr.l_key = wr->key;
  1639. wqe->frmr.length = wr->mr->length;
  1640. wqe->frmr.pbl_pg_sz_log = (wr->mr->page_size >> PAGE_SHIFT_4K) - 1;
  1641. wqe->frmr.va = wr->mr->iova;
  1642. return 0;
  1643. }
  1644. static int bnxt_re_copy_inline_data(struct bnxt_re_dev *rdev,
  1645. struct ib_send_wr *wr,
  1646. struct bnxt_qplib_swqe *wqe)
  1647. {
  1648. /* Copy the inline data to the data field */
  1649. u8 *in_data;
  1650. u32 i, sge_len;
  1651. void *sge_addr;
  1652. in_data = wqe->inline_data;
  1653. for (i = 0; i < wr->num_sge; i++) {
  1654. sge_addr = (void *)(unsigned long)
  1655. wr->sg_list[i].addr;
  1656. sge_len = wr->sg_list[i].length;
  1657. if ((sge_len + wqe->inline_len) >
  1658. BNXT_QPLIB_SWQE_MAX_INLINE_LENGTH) {
  1659. dev_err(rdev_to_dev(rdev),
  1660. "Inline data size requested > supported value");
  1661. return -EINVAL;
  1662. }
  1663. sge_len = wr->sg_list[i].length;
  1664. memcpy(in_data, sge_addr, sge_len);
  1665. in_data += wr->sg_list[i].length;
  1666. wqe->inline_len += wr->sg_list[i].length;
  1667. }
  1668. return wqe->inline_len;
  1669. }
  1670. static int bnxt_re_copy_wr_payload(struct bnxt_re_dev *rdev,
  1671. struct ib_send_wr *wr,
  1672. struct bnxt_qplib_swqe *wqe)
  1673. {
  1674. int payload_sz = 0;
  1675. if (wr->send_flags & IB_SEND_INLINE)
  1676. payload_sz = bnxt_re_copy_inline_data(rdev, wr, wqe);
  1677. else
  1678. payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe->sg_list,
  1679. wqe->num_sge);
  1680. return payload_sz;
  1681. }
  1682. static int bnxt_re_post_send_shadow_qp(struct bnxt_re_dev *rdev,
  1683. struct bnxt_re_qp *qp,
  1684. struct ib_send_wr *wr)
  1685. {
  1686. struct bnxt_qplib_swqe wqe;
  1687. int rc = 0, payload_sz = 0;
  1688. unsigned long flags;
  1689. spin_lock_irqsave(&qp->sq_lock, flags);
  1690. memset(&wqe, 0, sizeof(wqe));
  1691. while (wr) {
  1692. /* House keeping */
  1693. memset(&wqe, 0, sizeof(wqe));
  1694. /* Common */
  1695. wqe.num_sge = wr->num_sge;
  1696. if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
  1697. dev_err(rdev_to_dev(rdev),
  1698. "Limit exceeded for Send SGEs");
  1699. rc = -EINVAL;
  1700. goto bad;
  1701. }
  1702. payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
  1703. if (payload_sz < 0) {
  1704. rc = -EINVAL;
  1705. goto bad;
  1706. }
  1707. wqe.wr_id = wr->wr_id;
  1708. wqe.type = BNXT_QPLIB_SWQE_TYPE_SEND;
  1709. rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
  1710. if (!rc)
  1711. rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
  1712. bad:
  1713. if (rc) {
  1714. dev_err(rdev_to_dev(rdev),
  1715. "Post send failed opcode = %#x rc = %d",
  1716. wr->opcode, rc);
  1717. break;
  1718. }
  1719. wr = wr->next;
  1720. }
  1721. bnxt_qplib_post_send_db(&qp->qplib_qp);
  1722. spin_unlock_irqrestore(&qp->sq_lock, flags);
  1723. return rc;
  1724. }
  1725. int bnxt_re_post_send(struct ib_qp *ib_qp, struct ib_send_wr *wr,
  1726. struct ib_send_wr **bad_wr)
  1727. {
  1728. struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
  1729. struct bnxt_qplib_swqe wqe;
  1730. int rc = 0, payload_sz = 0;
  1731. unsigned long flags;
  1732. spin_lock_irqsave(&qp->sq_lock, flags);
  1733. while (wr) {
  1734. /* House keeping */
  1735. memset(&wqe, 0, sizeof(wqe));
  1736. /* Common */
  1737. wqe.num_sge = wr->num_sge;
  1738. if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
  1739. dev_err(rdev_to_dev(qp->rdev),
  1740. "Limit exceeded for Send SGEs");
  1741. rc = -EINVAL;
  1742. goto bad;
  1743. }
  1744. payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
  1745. if (payload_sz < 0) {
  1746. rc = -EINVAL;
  1747. goto bad;
  1748. }
  1749. wqe.wr_id = wr->wr_id;
  1750. switch (wr->opcode) {
  1751. case IB_WR_SEND:
  1752. case IB_WR_SEND_WITH_IMM:
  1753. if (ib_qp->qp_type == IB_QPT_GSI) {
  1754. rc = bnxt_re_build_qp1_send_v2(qp, wr, &wqe,
  1755. payload_sz);
  1756. if (rc)
  1757. goto bad;
  1758. wqe.rawqp1.lflags |=
  1759. SQ_SEND_RAWETH_QP1_LFLAGS_ROCE_CRC;
  1760. }
  1761. switch (wr->send_flags) {
  1762. case IB_SEND_IP_CSUM:
  1763. wqe.rawqp1.lflags |=
  1764. SQ_SEND_RAWETH_QP1_LFLAGS_IP_CHKSUM;
  1765. break;
  1766. default:
  1767. break;
  1768. }
  1769. /* Fall thru to build the wqe */
  1770. case IB_WR_SEND_WITH_INV:
  1771. rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
  1772. break;
  1773. case IB_WR_RDMA_WRITE:
  1774. case IB_WR_RDMA_WRITE_WITH_IMM:
  1775. case IB_WR_RDMA_READ:
  1776. rc = bnxt_re_build_rdma_wqe(wr, &wqe);
  1777. break;
  1778. case IB_WR_ATOMIC_CMP_AND_SWP:
  1779. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1780. rc = bnxt_re_build_atomic_wqe(wr, &wqe);
  1781. break;
  1782. case IB_WR_RDMA_READ_WITH_INV:
  1783. dev_err(rdev_to_dev(qp->rdev),
  1784. "RDMA Read with Invalidate is not supported");
  1785. rc = -EINVAL;
  1786. goto bad;
  1787. case IB_WR_LOCAL_INV:
  1788. rc = bnxt_re_build_inv_wqe(wr, &wqe);
  1789. break;
  1790. case IB_WR_REG_MR:
  1791. rc = bnxt_re_build_reg_wqe(reg_wr(wr), &wqe);
  1792. break;
  1793. default:
  1794. /* Unsupported WRs */
  1795. dev_err(rdev_to_dev(qp->rdev),
  1796. "WR (%#x) is not supported", wr->opcode);
  1797. rc = -EINVAL;
  1798. goto bad;
  1799. }
  1800. if (!rc)
  1801. rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
  1802. bad:
  1803. if (rc) {
  1804. dev_err(rdev_to_dev(qp->rdev),
  1805. "post_send failed op:%#x qps = %#x rc = %d\n",
  1806. wr->opcode, qp->qplib_qp.state, rc);
  1807. *bad_wr = wr;
  1808. break;
  1809. }
  1810. wr = wr->next;
  1811. }
  1812. bnxt_qplib_post_send_db(&qp->qplib_qp);
  1813. spin_unlock_irqrestore(&qp->sq_lock, flags);
  1814. return rc;
  1815. }
  1816. static int bnxt_re_post_recv_shadow_qp(struct bnxt_re_dev *rdev,
  1817. struct bnxt_re_qp *qp,
  1818. struct ib_recv_wr *wr)
  1819. {
  1820. struct bnxt_qplib_swqe wqe;
  1821. int rc = 0, payload_sz = 0;
  1822. memset(&wqe, 0, sizeof(wqe));
  1823. while (wr) {
  1824. /* House keeping */
  1825. memset(&wqe, 0, sizeof(wqe));
  1826. /* Common */
  1827. wqe.num_sge = wr->num_sge;
  1828. if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
  1829. dev_err(rdev_to_dev(rdev),
  1830. "Limit exceeded for Receive SGEs");
  1831. rc = -EINVAL;
  1832. break;
  1833. }
  1834. payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe.sg_list,
  1835. wr->num_sge);
  1836. wqe.wr_id = wr->wr_id;
  1837. wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
  1838. rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
  1839. if (rc)
  1840. break;
  1841. wr = wr->next;
  1842. }
  1843. if (!rc)
  1844. bnxt_qplib_post_recv_db(&qp->qplib_qp);
  1845. return rc;
  1846. }
  1847. int bnxt_re_post_recv(struct ib_qp *ib_qp, struct ib_recv_wr *wr,
  1848. struct ib_recv_wr **bad_wr)
  1849. {
  1850. struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
  1851. struct bnxt_qplib_swqe wqe;
  1852. int rc = 0, payload_sz = 0;
  1853. while (wr) {
  1854. /* House keeping */
  1855. memset(&wqe, 0, sizeof(wqe));
  1856. /* Common */
  1857. wqe.num_sge = wr->num_sge;
  1858. if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
  1859. dev_err(rdev_to_dev(qp->rdev),
  1860. "Limit exceeded for Receive SGEs");
  1861. rc = -EINVAL;
  1862. *bad_wr = wr;
  1863. break;
  1864. }
  1865. payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe.sg_list,
  1866. wr->num_sge);
  1867. wqe.wr_id = wr->wr_id;
  1868. wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
  1869. if (ib_qp->qp_type == IB_QPT_GSI)
  1870. rc = bnxt_re_build_qp1_shadow_qp_recv(qp, wr, &wqe,
  1871. payload_sz);
  1872. if (!rc)
  1873. rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
  1874. if (rc) {
  1875. *bad_wr = wr;
  1876. break;
  1877. }
  1878. wr = wr->next;
  1879. }
  1880. bnxt_qplib_post_recv_db(&qp->qplib_qp);
  1881. return rc;
  1882. }
  1883. /* Completion Queues */
  1884. int bnxt_re_destroy_cq(struct ib_cq *ib_cq)
  1885. {
  1886. struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
  1887. struct bnxt_re_dev *rdev = cq->rdev;
  1888. int rc;
  1889. rc = bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
  1890. if (rc) {
  1891. dev_err(rdev_to_dev(rdev), "Failed to destroy HW CQ");
  1892. return rc;
  1893. }
  1894. if (cq->umem && !IS_ERR(cq->umem))
  1895. ib_umem_release(cq->umem);
  1896. if (cq) {
  1897. kfree(cq->cql);
  1898. kfree(cq);
  1899. }
  1900. atomic_dec(&rdev->cq_count);
  1901. rdev->nq.budget--;
  1902. return 0;
  1903. }
  1904. struct ib_cq *bnxt_re_create_cq(struct ib_device *ibdev,
  1905. const struct ib_cq_init_attr *attr,
  1906. struct ib_ucontext *context,
  1907. struct ib_udata *udata)
  1908. {
  1909. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  1910. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  1911. struct bnxt_re_cq *cq = NULL;
  1912. int rc, entries;
  1913. int cqe = attr->cqe;
  1914. /* Validate CQ fields */
  1915. if (cqe < 1 || cqe > dev_attr->max_cq_wqes) {
  1916. dev_err(rdev_to_dev(rdev), "Failed to create CQ -max exceeded");
  1917. return ERR_PTR(-EINVAL);
  1918. }
  1919. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  1920. if (!cq)
  1921. return ERR_PTR(-ENOMEM);
  1922. cq->rdev = rdev;
  1923. cq->qplib_cq.cq_handle = (u64)(unsigned long)(&cq->qplib_cq);
  1924. entries = roundup_pow_of_two(cqe + 1);
  1925. if (entries > dev_attr->max_cq_wqes + 1)
  1926. entries = dev_attr->max_cq_wqes + 1;
  1927. if (context) {
  1928. struct bnxt_re_cq_req req;
  1929. struct bnxt_re_ucontext *uctx = container_of
  1930. (context,
  1931. struct bnxt_re_ucontext,
  1932. ib_uctx);
  1933. if (ib_copy_from_udata(&req, udata, sizeof(req))) {
  1934. rc = -EFAULT;
  1935. goto fail;
  1936. }
  1937. cq->umem = ib_umem_get(context, req.cq_va,
  1938. entries * sizeof(struct cq_base),
  1939. IB_ACCESS_LOCAL_WRITE, 1);
  1940. if (IS_ERR(cq->umem)) {
  1941. rc = PTR_ERR(cq->umem);
  1942. goto fail;
  1943. }
  1944. cq->qplib_cq.sghead = cq->umem->sg_head.sgl;
  1945. cq->qplib_cq.nmap = cq->umem->nmap;
  1946. cq->qplib_cq.dpi = uctx->dpi;
  1947. } else {
  1948. cq->max_cql = min_t(u32, entries, MAX_CQL_PER_POLL);
  1949. cq->cql = kcalloc(cq->max_cql, sizeof(struct bnxt_qplib_cqe),
  1950. GFP_KERNEL);
  1951. if (!cq->cql) {
  1952. rc = -ENOMEM;
  1953. goto fail;
  1954. }
  1955. cq->qplib_cq.dpi = &rdev->dpi_privileged;
  1956. cq->qplib_cq.sghead = NULL;
  1957. cq->qplib_cq.nmap = 0;
  1958. }
  1959. cq->qplib_cq.max_wqe = entries;
  1960. cq->qplib_cq.cnq_hw_ring_id = rdev->nq.ring_id;
  1961. rc = bnxt_qplib_create_cq(&rdev->qplib_res, &cq->qplib_cq);
  1962. if (rc) {
  1963. dev_err(rdev_to_dev(rdev), "Failed to create HW CQ");
  1964. goto fail;
  1965. }
  1966. cq->ib_cq.cqe = entries;
  1967. cq->cq_period = cq->qplib_cq.period;
  1968. rdev->nq.budget++;
  1969. atomic_inc(&rdev->cq_count);
  1970. if (context) {
  1971. struct bnxt_re_cq_resp resp;
  1972. resp.cqid = cq->qplib_cq.id;
  1973. resp.tail = cq->qplib_cq.hwq.cons;
  1974. resp.phase = cq->qplib_cq.period;
  1975. resp.rsvd = 0;
  1976. rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
  1977. if (rc) {
  1978. dev_err(rdev_to_dev(rdev), "Failed to copy CQ udata");
  1979. bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
  1980. goto c2fail;
  1981. }
  1982. }
  1983. return &cq->ib_cq;
  1984. c2fail:
  1985. if (context)
  1986. ib_umem_release(cq->umem);
  1987. fail:
  1988. kfree(cq->cql);
  1989. kfree(cq);
  1990. return ERR_PTR(rc);
  1991. }
  1992. static u8 __req_to_ib_wc_status(u8 qstatus)
  1993. {
  1994. switch (qstatus) {
  1995. case CQ_REQ_STATUS_OK:
  1996. return IB_WC_SUCCESS;
  1997. case CQ_REQ_STATUS_BAD_RESPONSE_ERR:
  1998. return IB_WC_BAD_RESP_ERR;
  1999. case CQ_REQ_STATUS_LOCAL_LENGTH_ERR:
  2000. return IB_WC_LOC_LEN_ERR;
  2001. case CQ_REQ_STATUS_LOCAL_QP_OPERATION_ERR:
  2002. return IB_WC_LOC_QP_OP_ERR;
  2003. case CQ_REQ_STATUS_LOCAL_PROTECTION_ERR:
  2004. return IB_WC_LOC_PROT_ERR;
  2005. case CQ_REQ_STATUS_MEMORY_MGT_OPERATION_ERR:
  2006. return IB_WC_GENERAL_ERR;
  2007. case CQ_REQ_STATUS_REMOTE_INVALID_REQUEST_ERR:
  2008. return IB_WC_REM_INV_REQ_ERR;
  2009. case CQ_REQ_STATUS_REMOTE_ACCESS_ERR:
  2010. return IB_WC_REM_ACCESS_ERR;
  2011. case CQ_REQ_STATUS_REMOTE_OPERATION_ERR:
  2012. return IB_WC_REM_OP_ERR;
  2013. case CQ_REQ_STATUS_RNR_NAK_RETRY_CNT_ERR:
  2014. return IB_WC_RNR_RETRY_EXC_ERR;
  2015. case CQ_REQ_STATUS_TRANSPORT_RETRY_CNT_ERR:
  2016. return IB_WC_RETRY_EXC_ERR;
  2017. case CQ_REQ_STATUS_WORK_REQUEST_FLUSHED_ERR:
  2018. return IB_WC_WR_FLUSH_ERR;
  2019. default:
  2020. return IB_WC_GENERAL_ERR;
  2021. }
  2022. return 0;
  2023. }
  2024. static u8 __rawqp1_to_ib_wc_status(u8 qstatus)
  2025. {
  2026. switch (qstatus) {
  2027. case CQ_RES_RAWETH_QP1_STATUS_OK:
  2028. return IB_WC_SUCCESS;
  2029. case CQ_RES_RAWETH_QP1_STATUS_LOCAL_ACCESS_ERROR:
  2030. return IB_WC_LOC_ACCESS_ERR;
  2031. case CQ_RES_RAWETH_QP1_STATUS_HW_LOCAL_LENGTH_ERR:
  2032. return IB_WC_LOC_LEN_ERR;
  2033. case CQ_RES_RAWETH_QP1_STATUS_LOCAL_PROTECTION_ERR:
  2034. return IB_WC_LOC_PROT_ERR;
  2035. case CQ_RES_RAWETH_QP1_STATUS_LOCAL_QP_OPERATION_ERR:
  2036. return IB_WC_LOC_QP_OP_ERR;
  2037. case CQ_RES_RAWETH_QP1_STATUS_MEMORY_MGT_OPERATION_ERR:
  2038. return IB_WC_GENERAL_ERR;
  2039. case CQ_RES_RAWETH_QP1_STATUS_WORK_REQUEST_FLUSHED_ERR:
  2040. return IB_WC_WR_FLUSH_ERR;
  2041. case CQ_RES_RAWETH_QP1_STATUS_HW_FLUSH_ERR:
  2042. return IB_WC_WR_FLUSH_ERR;
  2043. default:
  2044. return IB_WC_GENERAL_ERR;
  2045. }
  2046. }
  2047. static u8 __rc_to_ib_wc_status(u8 qstatus)
  2048. {
  2049. switch (qstatus) {
  2050. case CQ_RES_RC_STATUS_OK:
  2051. return IB_WC_SUCCESS;
  2052. case CQ_RES_RC_STATUS_LOCAL_ACCESS_ERROR:
  2053. return IB_WC_LOC_ACCESS_ERR;
  2054. case CQ_RES_RC_STATUS_LOCAL_LENGTH_ERR:
  2055. return IB_WC_LOC_LEN_ERR;
  2056. case CQ_RES_RC_STATUS_LOCAL_PROTECTION_ERR:
  2057. return IB_WC_LOC_PROT_ERR;
  2058. case CQ_RES_RC_STATUS_LOCAL_QP_OPERATION_ERR:
  2059. return IB_WC_LOC_QP_OP_ERR;
  2060. case CQ_RES_RC_STATUS_MEMORY_MGT_OPERATION_ERR:
  2061. return IB_WC_GENERAL_ERR;
  2062. case CQ_RES_RC_STATUS_REMOTE_INVALID_REQUEST_ERR:
  2063. return IB_WC_REM_INV_REQ_ERR;
  2064. case CQ_RES_RC_STATUS_WORK_REQUEST_FLUSHED_ERR:
  2065. return IB_WC_WR_FLUSH_ERR;
  2066. case CQ_RES_RC_STATUS_HW_FLUSH_ERR:
  2067. return IB_WC_WR_FLUSH_ERR;
  2068. default:
  2069. return IB_WC_GENERAL_ERR;
  2070. }
  2071. }
  2072. static void bnxt_re_process_req_wc(struct ib_wc *wc, struct bnxt_qplib_cqe *cqe)
  2073. {
  2074. switch (cqe->type) {
  2075. case BNXT_QPLIB_SWQE_TYPE_SEND:
  2076. wc->opcode = IB_WC_SEND;
  2077. break;
  2078. case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM:
  2079. wc->opcode = IB_WC_SEND;
  2080. wc->wc_flags |= IB_WC_WITH_IMM;
  2081. break;
  2082. case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV:
  2083. wc->opcode = IB_WC_SEND;
  2084. wc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2085. break;
  2086. case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE:
  2087. wc->opcode = IB_WC_RDMA_WRITE;
  2088. break;
  2089. case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM:
  2090. wc->opcode = IB_WC_RDMA_WRITE;
  2091. wc->wc_flags |= IB_WC_WITH_IMM;
  2092. break;
  2093. case BNXT_QPLIB_SWQE_TYPE_RDMA_READ:
  2094. wc->opcode = IB_WC_RDMA_READ;
  2095. break;
  2096. case BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP:
  2097. wc->opcode = IB_WC_COMP_SWAP;
  2098. break;
  2099. case BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD:
  2100. wc->opcode = IB_WC_FETCH_ADD;
  2101. break;
  2102. case BNXT_QPLIB_SWQE_TYPE_LOCAL_INV:
  2103. wc->opcode = IB_WC_LOCAL_INV;
  2104. break;
  2105. case BNXT_QPLIB_SWQE_TYPE_REG_MR:
  2106. wc->opcode = IB_WC_REG_MR;
  2107. break;
  2108. default:
  2109. wc->opcode = IB_WC_SEND;
  2110. break;
  2111. }
  2112. wc->status = __req_to_ib_wc_status(cqe->status);
  2113. }
  2114. static int bnxt_re_check_packet_type(u16 raweth_qp1_flags,
  2115. u16 raweth_qp1_flags2)
  2116. {
  2117. bool is_udp = false, is_ipv6 = false, is_ipv4 = false;
  2118. /* raweth_qp1_flags Bit 9-6 indicates itype */
  2119. if ((raweth_qp1_flags & CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
  2120. != CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
  2121. return -1;
  2122. if (raweth_qp1_flags2 &
  2123. CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_CS_CALC &&
  2124. raweth_qp1_flags2 &
  2125. CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_L4_CS_CALC) {
  2126. is_udp = true;
  2127. /* raweth_qp1_flags2 Bit 8 indicates ip_type. 0-v4 1 - v6 */
  2128. (raweth_qp1_flags2 &
  2129. CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_TYPE) ?
  2130. (is_ipv6 = true) : (is_ipv4 = true);
  2131. return ((is_ipv6) ?
  2132. BNXT_RE_ROCEV2_IPV6_PACKET :
  2133. BNXT_RE_ROCEV2_IPV4_PACKET);
  2134. } else {
  2135. return BNXT_RE_ROCE_V1_PACKET;
  2136. }
  2137. }
  2138. static int bnxt_re_to_ib_nw_type(int nw_type)
  2139. {
  2140. u8 nw_hdr_type = 0xFF;
  2141. switch (nw_type) {
  2142. case BNXT_RE_ROCE_V1_PACKET:
  2143. nw_hdr_type = RDMA_NETWORK_ROCE_V1;
  2144. break;
  2145. case BNXT_RE_ROCEV2_IPV4_PACKET:
  2146. nw_hdr_type = RDMA_NETWORK_IPV4;
  2147. break;
  2148. case BNXT_RE_ROCEV2_IPV6_PACKET:
  2149. nw_hdr_type = RDMA_NETWORK_IPV6;
  2150. break;
  2151. }
  2152. return nw_hdr_type;
  2153. }
  2154. static bool bnxt_re_is_loopback_packet(struct bnxt_re_dev *rdev,
  2155. void *rq_hdr_buf)
  2156. {
  2157. u8 *tmp_buf = NULL;
  2158. struct ethhdr *eth_hdr;
  2159. u16 eth_type;
  2160. bool rc = false;
  2161. tmp_buf = (u8 *)rq_hdr_buf;
  2162. /*
  2163. * If dest mac is not same as I/F mac, this could be a
  2164. * loopback address or multicast address, check whether
  2165. * it is a loopback packet
  2166. */
  2167. if (!ether_addr_equal(tmp_buf, rdev->netdev->dev_addr)) {
  2168. tmp_buf += 4;
  2169. /* Check the ether type */
  2170. eth_hdr = (struct ethhdr *)tmp_buf;
  2171. eth_type = ntohs(eth_hdr->h_proto);
  2172. switch (eth_type) {
  2173. case ETH_P_IBOE:
  2174. rc = true;
  2175. break;
  2176. case ETH_P_IP:
  2177. case ETH_P_IPV6: {
  2178. u32 len;
  2179. struct udphdr *udp_hdr;
  2180. len = (eth_type == ETH_P_IP ? sizeof(struct iphdr) :
  2181. sizeof(struct ipv6hdr));
  2182. tmp_buf += sizeof(struct ethhdr) + len;
  2183. udp_hdr = (struct udphdr *)tmp_buf;
  2184. if (ntohs(udp_hdr->dest) ==
  2185. ROCE_V2_UDP_DPORT)
  2186. rc = true;
  2187. break;
  2188. }
  2189. default:
  2190. break;
  2191. }
  2192. }
  2193. return rc;
  2194. }
  2195. static int bnxt_re_process_raw_qp_pkt_rx(struct bnxt_re_qp *qp1_qp,
  2196. struct bnxt_qplib_cqe *cqe)
  2197. {
  2198. struct bnxt_re_dev *rdev = qp1_qp->rdev;
  2199. struct bnxt_re_sqp_entries *sqp_entry = NULL;
  2200. struct bnxt_re_qp *qp = rdev->qp1_sqp;
  2201. struct ib_send_wr *swr;
  2202. struct ib_ud_wr udwr;
  2203. struct ib_recv_wr rwr;
  2204. int pkt_type = 0;
  2205. u32 tbl_idx;
  2206. void *rq_hdr_buf;
  2207. dma_addr_t rq_hdr_buf_map;
  2208. dma_addr_t shrq_hdr_buf_map;
  2209. u32 offset = 0;
  2210. u32 skip_bytes = 0;
  2211. struct ib_sge s_sge[2];
  2212. struct ib_sge r_sge[2];
  2213. int rc;
  2214. memset(&udwr, 0, sizeof(udwr));
  2215. memset(&rwr, 0, sizeof(rwr));
  2216. memset(&s_sge, 0, sizeof(s_sge));
  2217. memset(&r_sge, 0, sizeof(r_sge));
  2218. swr = &udwr.wr;
  2219. tbl_idx = cqe->wr_id;
  2220. rq_hdr_buf = qp1_qp->qplib_qp.rq_hdr_buf +
  2221. (tbl_idx * qp1_qp->qplib_qp.rq_hdr_buf_size);
  2222. rq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&qp1_qp->qplib_qp,
  2223. tbl_idx);
  2224. /* Shadow QP header buffer */
  2225. shrq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&qp->qplib_qp,
  2226. tbl_idx);
  2227. sqp_entry = &rdev->sqp_tbl[tbl_idx];
  2228. /* Store this cqe */
  2229. memcpy(&sqp_entry->cqe, cqe, sizeof(struct bnxt_qplib_cqe));
  2230. sqp_entry->qp1_qp = qp1_qp;
  2231. /* Find packet type from the cqe */
  2232. pkt_type = bnxt_re_check_packet_type(cqe->raweth_qp1_flags,
  2233. cqe->raweth_qp1_flags2);
  2234. if (pkt_type < 0) {
  2235. dev_err(rdev_to_dev(rdev), "Invalid packet\n");
  2236. return -EINVAL;
  2237. }
  2238. /* Adjust the offset for the user buffer and post in the rq */
  2239. if (pkt_type == BNXT_RE_ROCEV2_IPV4_PACKET)
  2240. offset = 20;
  2241. /*
  2242. * QP1 loopback packet has 4 bytes of internal header before
  2243. * ether header. Skip these four bytes.
  2244. */
  2245. if (bnxt_re_is_loopback_packet(rdev, rq_hdr_buf))
  2246. skip_bytes = 4;
  2247. /* First send SGE . Skip the ether header*/
  2248. s_sge[0].addr = rq_hdr_buf_map + BNXT_QPLIB_MAX_QP1_RQ_ETH_HDR_SIZE
  2249. + skip_bytes;
  2250. s_sge[0].lkey = 0xFFFFFFFF;
  2251. s_sge[0].length = offset ? BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV4 :
  2252. BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
  2253. /* Second Send SGE */
  2254. s_sge[1].addr = s_sge[0].addr + s_sge[0].length +
  2255. BNXT_QPLIB_MAX_QP1_RQ_BDETH_HDR_SIZE;
  2256. if (pkt_type != BNXT_RE_ROCE_V1_PACKET)
  2257. s_sge[1].addr += 8;
  2258. s_sge[1].lkey = 0xFFFFFFFF;
  2259. s_sge[1].length = 256;
  2260. /* First recv SGE */
  2261. r_sge[0].addr = shrq_hdr_buf_map;
  2262. r_sge[0].lkey = 0xFFFFFFFF;
  2263. r_sge[0].length = 40;
  2264. r_sge[1].addr = sqp_entry->sge.addr + offset;
  2265. r_sge[1].lkey = sqp_entry->sge.lkey;
  2266. r_sge[1].length = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6 + 256 - offset;
  2267. /* Create receive work request */
  2268. rwr.num_sge = 2;
  2269. rwr.sg_list = r_sge;
  2270. rwr.wr_id = tbl_idx;
  2271. rwr.next = NULL;
  2272. rc = bnxt_re_post_recv_shadow_qp(rdev, qp, &rwr);
  2273. if (rc) {
  2274. dev_err(rdev_to_dev(rdev),
  2275. "Failed to post Rx buffers to shadow QP");
  2276. return -ENOMEM;
  2277. }
  2278. swr->num_sge = 2;
  2279. swr->sg_list = s_sge;
  2280. swr->wr_id = tbl_idx;
  2281. swr->opcode = IB_WR_SEND;
  2282. swr->next = NULL;
  2283. udwr.ah = &rdev->sqp_ah->ib_ah;
  2284. udwr.remote_qpn = rdev->qp1_sqp->qplib_qp.id;
  2285. udwr.remote_qkey = rdev->qp1_sqp->qplib_qp.qkey;
  2286. /* post data received in the send queue */
  2287. rc = bnxt_re_post_send_shadow_qp(rdev, qp, swr);
  2288. return 0;
  2289. }
  2290. static void bnxt_re_process_res_rawqp1_wc(struct ib_wc *wc,
  2291. struct bnxt_qplib_cqe *cqe)
  2292. {
  2293. wc->opcode = IB_WC_RECV;
  2294. wc->status = __rawqp1_to_ib_wc_status(cqe->status);
  2295. wc->wc_flags |= IB_WC_GRH;
  2296. }
  2297. static void bnxt_re_process_res_rc_wc(struct ib_wc *wc,
  2298. struct bnxt_qplib_cqe *cqe)
  2299. {
  2300. wc->opcode = IB_WC_RECV;
  2301. wc->status = __rc_to_ib_wc_status(cqe->status);
  2302. if (cqe->flags & CQ_RES_RC_FLAGS_IMM)
  2303. wc->wc_flags |= IB_WC_WITH_IMM;
  2304. if (cqe->flags & CQ_RES_RC_FLAGS_INV)
  2305. wc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2306. if ((cqe->flags & (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM)) ==
  2307. (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM))
  2308. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2309. }
  2310. static void bnxt_re_process_res_shadow_qp_wc(struct bnxt_re_qp *qp,
  2311. struct ib_wc *wc,
  2312. struct bnxt_qplib_cqe *cqe)
  2313. {
  2314. u32 tbl_idx;
  2315. struct bnxt_re_dev *rdev = qp->rdev;
  2316. struct bnxt_re_qp *qp1_qp = NULL;
  2317. struct bnxt_qplib_cqe *orig_cqe = NULL;
  2318. struct bnxt_re_sqp_entries *sqp_entry = NULL;
  2319. int nw_type;
  2320. tbl_idx = cqe->wr_id;
  2321. sqp_entry = &rdev->sqp_tbl[tbl_idx];
  2322. qp1_qp = sqp_entry->qp1_qp;
  2323. orig_cqe = &sqp_entry->cqe;
  2324. wc->wr_id = sqp_entry->wrid;
  2325. wc->byte_len = orig_cqe->length;
  2326. wc->qp = &qp1_qp->ib_qp;
  2327. wc->ex.imm_data = orig_cqe->immdata;
  2328. wc->src_qp = orig_cqe->src_qp;
  2329. memcpy(wc->smac, orig_cqe->smac, ETH_ALEN);
  2330. wc->port_num = 1;
  2331. wc->vendor_err = orig_cqe->status;
  2332. wc->opcode = IB_WC_RECV;
  2333. wc->status = __rawqp1_to_ib_wc_status(orig_cqe->status);
  2334. wc->wc_flags |= IB_WC_GRH;
  2335. nw_type = bnxt_re_check_packet_type(orig_cqe->raweth_qp1_flags,
  2336. orig_cqe->raweth_qp1_flags2);
  2337. if (nw_type >= 0) {
  2338. wc->network_hdr_type = bnxt_re_to_ib_nw_type(nw_type);
  2339. wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
  2340. }
  2341. }
  2342. static void bnxt_re_process_res_ud_wc(struct ib_wc *wc,
  2343. struct bnxt_qplib_cqe *cqe)
  2344. {
  2345. wc->opcode = IB_WC_RECV;
  2346. wc->status = __rc_to_ib_wc_status(cqe->status);
  2347. if (cqe->flags & CQ_RES_RC_FLAGS_IMM)
  2348. wc->wc_flags |= IB_WC_WITH_IMM;
  2349. if (cqe->flags & CQ_RES_RC_FLAGS_INV)
  2350. wc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2351. if ((cqe->flags & (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM)) ==
  2352. (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM))
  2353. wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2354. }
  2355. int bnxt_re_poll_cq(struct ib_cq *ib_cq, int num_entries, struct ib_wc *wc)
  2356. {
  2357. struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
  2358. struct bnxt_re_qp *qp;
  2359. struct bnxt_qplib_cqe *cqe;
  2360. int i, ncqe, budget;
  2361. u32 tbl_idx;
  2362. struct bnxt_re_sqp_entries *sqp_entry = NULL;
  2363. unsigned long flags;
  2364. spin_lock_irqsave(&cq->cq_lock, flags);
  2365. budget = min_t(u32, num_entries, cq->max_cql);
  2366. if (!cq->cql) {
  2367. dev_err(rdev_to_dev(cq->rdev), "POLL CQ : no CQL to use");
  2368. goto exit;
  2369. }
  2370. cqe = &cq->cql[0];
  2371. while (budget) {
  2372. ncqe = bnxt_qplib_poll_cq(&cq->qplib_cq, cqe, budget);
  2373. if (!ncqe)
  2374. break;
  2375. for (i = 0; i < ncqe; i++, cqe++) {
  2376. /* Transcribe each qplib_wqe back to ib_wc */
  2377. memset(wc, 0, sizeof(*wc));
  2378. wc->wr_id = cqe->wr_id;
  2379. wc->byte_len = cqe->length;
  2380. qp = container_of
  2381. ((struct bnxt_qplib_qp *)
  2382. (unsigned long)(cqe->qp_handle),
  2383. struct bnxt_re_qp, qplib_qp);
  2384. if (!qp) {
  2385. dev_err(rdev_to_dev(cq->rdev),
  2386. "POLL CQ : bad QP handle");
  2387. continue;
  2388. }
  2389. wc->qp = &qp->ib_qp;
  2390. wc->ex.imm_data = cqe->immdata;
  2391. wc->src_qp = cqe->src_qp;
  2392. memcpy(wc->smac, cqe->smac, ETH_ALEN);
  2393. wc->port_num = 1;
  2394. wc->vendor_err = cqe->status;
  2395. switch (cqe->opcode) {
  2396. case CQ_BASE_CQE_TYPE_REQ:
  2397. if (qp->qplib_qp.id ==
  2398. qp->rdev->qp1_sqp->qplib_qp.id) {
  2399. /* Handle this completion with
  2400. * the stored completion
  2401. */
  2402. memset(wc, 0, sizeof(*wc));
  2403. continue;
  2404. }
  2405. bnxt_re_process_req_wc(wc, cqe);
  2406. break;
  2407. case CQ_BASE_CQE_TYPE_RES_RAWETH_QP1:
  2408. if (!cqe->status) {
  2409. int rc = 0;
  2410. rc = bnxt_re_process_raw_qp_pkt_rx
  2411. (qp, cqe);
  2412. if (!rc) {
  2413. memset(wc, 0, sizeof(*wc));
  2414. continue;
  2415. }
  2416. cqe->status = -1;
  2417. }
  2418. /* Errors need not be looped back.
  2419. * But change the wr_id to the one
  2420. * stored in the table
  2421. */
  2422. tbl_idx = cqe->wr_id;
  2423. sqp_entry = &cq->rdev->sqp_tbl[tbl_idx];
  2424. wc->wr_id = sqp_entry->wrid;
  2425. bnxt_re_process_res_rawqp1_wc(wc, cqe);
  2426. break;
  2427. case CQ_BASE_CQE_TYPE_RES_RC:
  2428. bnxt_re_process_res_rc_wc(wc, cqe);
  2429. break;
  2430. case CQ_BASE_CQE_TYPE_RES_UD:
  2431. if (qp->qplib_qp.id ==
  2432. qp->rdev->qp1_sqp->qplib_qp.id) {
  2433. /* Handle this completion with
  2434. * the stored completion
  2435. */
  2436. if (cqe->status) {
  2437. continue;
  2438. } else {
  2439. bnxt_re_process_res_shadow_qp_wc
  2440. (qp, wc, cqe);
  2441. break;
  2442. }
  2443. }
  2444. bnxt_re_process_res_ud_wc(wc, cqe);
  2445. break;
  2446. default:
  2447. dev_err(rdev_to_dev(cq->rdev),
  2448. "POLL CQ : type 0x%x not handled",
  2449. cqe->opcode);
  2450. continue;
  2451. }
  2452. wc++;
  2453. budget--;
  2454. }
  2455. }
  2456. exit:
  2457. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2458. return num_entries - budget;
  2459. }
  2460. int bnxt_re_req_notify_cq(struct ib_cq *ib_cq,
  2461. enum ib_cq_notify_flags ib_cqn_flags)
  2462. {
  2463. struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
  2464. int type = 0;
  2465. /* Trigger on the very next completion */
  2466. if (ib_cqn_flags & IB_CQ_NEXT_COMP)
  2467. type = DBR_DBR_TYPE_CQ_ARMALL;
  2468. /* Trigger on the next solicited completion */
  2469. else if (ib_cqn_flags & IB_CQ_SOLICITED)
  2470. type = DBR_DBR_TYPE_CQ_ARMSE;
  2471. bnxt_qplib_req_notify_cq(&cq->qplib_cq, type);
  2472. return 0;
  2473. }
  2474. /* Memory Regions */
  2475. struct ib_mr *bnxt_re_get_dma_mr(struct ib_pd *ib_pd, int mr_access_flags)
  2476. {
  2477. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  2478. struct bnxt_re_dev *rdev = pd->rdev;
  2479. struct bnxt_re_mr *mr;
  2480. u64 pbl = 0;
  2481. int rc;
  2482. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2483. if (!mr)
  2484. return ERR_PTR(-ENOMEM);
  2485. mr->rdev = rdev;
  2486. mr->qplib_mr.pd = &pd->qplib_pd;
  2487. mr->qplib_mr.flags = __from_ib_access_flags(mr_access_flags);
  2488. mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
  2489. /* Allocate and register 0 as the address */
  2490. rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2491. if (rc)
  2492. goto fail;
  2493. mr->qplib_mr.hwq.level = PBL_LVL_MAX;
  2494. mr->qplib_mr.total_size = -1; /* Infinte length */
  2495. rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, &pbl, 0, false);
  2496. if (rc)
  2497. goto fail_mr;
  2498. mr->ib_mr.lkey = mr->qplib_mr.lkey;
  2499. if (mr_access_flags & (IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ |
  2500. IB_ACCESS_REMOTE_ATOMIC))
  2501. mr->ib_mr.rkey = mr->ib_mr.lkey;
  2502. atomic_inc(&rdev->mr_count);
  2503. return &mr->ib_mr;
  2504. fail_mr:
  2505. bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2506. fail:
  2507. kfree(mr);
  2508. return ERR_PTR(rc);
  2509. }
  2510. int bnxt_re_dereg_mr(struct ib_mr *ib_mr)
  2511. {
  2512. struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
  2513. struct bnxt_re_dev *rdev = mr->rdev;
  2514. int rc = 0;
  2515. if (mr->npages && mr->pages) {
  2516. rc = bnxt_qplib_free_fast_reg_page_list(&rdev->qplib_res,
  2517. &mr->qplib_frpl);
  2518. kfree(mr->pages);
  2519. mr->npages = 0;
  2520. mr->pages = NULL;
  2521. }
  2522. rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2523. if (!IS_ERR(mr->ib_umem) && mr->ib_umem)
  2524. ib_umem_release(mr->ib_umem);
  2525. kfree(mr);
  2526. atomic_dec(&rdev->mr_count);
  2527. return rc;
  2528. }
  2529. static int bnxt_re_set_page(struct ib_mr *ib_mr, u64 addr)
  2530. {
  2531. struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
  2532. if (unlikely(mr->npages == mr->qplib_frpl.max_pg_ptrs))
  2533. return -ENOMEM;
  2534. mr->pages[mr->npages++] = addr;
  2535. return 0;
  2536. }
  2537. int bnxt_re_map_mr_sg(struct ib_mr *ib_mr, struct scatterlist *sg, int sg_nents,
  2538. unsigned int *sg_offset)
  2539. {
  2540. struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
  2541. mr->npages = 0;
  2542. return ib_sg_to_pages(ib_mr, sg, sg_nents, sg_offset, bnxt_re_set_page);
  2543. }
  2544. struct ib_mr *bnxt_re_alloc_mr(struct ib_pd *ib_pd, enum ib_mr_type type,
  2545. u32 max_num_sg)
  2546. {
  2547. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  2548. struct bnxt_re_dev *rdev = pd->rdev;
  2549. struct bnxt_re_mr *mr = NULL;
  2550. int rc;
  2551. if (type != IB_MR_TYPE_MEM_REG) {
  2552. dev_dbg(rdev_to_dev(rdev), "MR type 0x%x not supported", type);
  2553. return ERR_PTR(-EINVAL);
  2554. }
  2555. if (max_num_sg > MAX_PBL_LVL_1_PGS)
  2556. return ERR_PTR(-EINVAL);
  2557. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2558. if (!mr)
  2559. return ERR_PTR(-ENOMEM);
  2560. mr->rdev = rdev;
  2561. mr->qplib_mr.pd = &pd->qplib_pd;
  2562. mr->qplib_mr.flags = BNXT_QPLIB_FR_PMR;
  2563. mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
  2564. rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2565. if (rc)
  2566. goto fail;
  2567. mr->ib_mr.lkey = mr->qplib_mr.lkey;
  2568. mr->ib_mr.rkey = mr->ib_mr.lkey;
  2569. mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
  2570. if (!mr->pages) {
  2571. rc = -ENOMEM;
  2572. goto fail;
  2573. }
  2574. rc = bnxt_qplib_alloc_fast_reg_page_list(&rdev->qplib_res,
  2575. &mr->qplib_frpl, max_num_sg);
  2576. if (rc) {
  2577. dev_err(rdev_to_dev(rdev),
  2578. "Failed to allocate HW FR page list");
  2579. goto fail_mr;
  2580. }
  2581. atomic_inc(&rdev->mr_count);
  2582. return &mr->ib_mr;
  2583. fail_mr:
  2584. bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2585. fail:
  2586. kfree(mr->pages);
  2587. kfree(mr);
  2588. return ERR_PTR(rc);
  2589. }
  2590. /* Fast Memory Regions */
  2591. struct ib_fmr *bnxt_re_alloc_fmr(struct ib_pd *ib_pd, int mr_access_flags,
  2592. struct ib_fmr_attr *fmr_attr)
  2593. {
  2594. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  2595. struct bnxt_re_dev *rdev = pd->rdev;
  2596. struct bnxt_re_fmr *fmr;
  2597. int rc;
  2598. if (fmr_attr->max_pages > MAX_PBL_LVL_2_PGS ||
  2599. fmr_attr->max_maps > rdev->dev_attr.max_map_per_fmr) {
  2600. dev_err(rdev_to_dev(rdev), "Allocate FMR exceeded Max limit");
  2601. return ERR_PTR(-ENOMEM);
  2602. }
  2603. fmr = kzalloc(sizeof(*fmr), GFP_KERNEL);
  2604. if (!fmr)
  2605. return ERR_PTR(-ENOMEM);
  2606. fmr->rdev = rdev;
  2607. fmr->qplib_fmr.pd = &pd->qplib_pd;
  2608. fmr->qplib_fmr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
  2609. rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &fmr->qplib_fmr);
  2610. if (rc)
  2611. goto fail;
  2612. fmr->qplib_fmr.flags = __from_ib_access_flags(mr_access_flags);
  2613. fmr->ib_fmr.lkey = fmr->qplib_fmr.lkey;
  2614. fmr->ib_fmr.rkey = fmr->ib_fmr.lkey;
  2615. atomic_inc(&rdev->mr_count);
  2616. return &fmr->ib_fmr;
  2617. fail:
  2618. kfree(fmr);
  2619. return ERR_PTR(rc);
  2620. }
  2621. int bnxt_re_map_phys_fmr(struct ib_fmr *ib_fmr, u64 *page_list, int list_len,
  2622. u64 iova)
  2623. {
  2624. struct bnxt_re_fmr *fmr = container_of(ib_fmr, struct bnxt_re_fmr,
  2625. ib_fmr);
  2626. struct bnxt_re_dev *rdev = fmr->rdev;
  2627. int rc;
  2628. fmr->qplib_fmr.va = iova;
  2629. fmr->qplib_fmr.total_size = list_len * PAGE_SIZE;
  2630. rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &fmr->qplib_fmr, page_list,
  2631. list_len, true);
  2632. if (rc)
  2633. dev_err(rdev_to_dev(rdev), "Failed to map FMR for lkey = 0x%x!",
  2634. fmr->ib_fmr.lkey);
  2635. return rc;
  2636. }
  2637. int bnxt_re_unmap_fmr(struct list_head *fmr_list)
  2638. {
  2639. struct bnxt_re_dev *rdev;
  2640. struct bnxt_re_fmr *fmr;
  2641. struct ib_fmr *ib_fmr;
  2642. int rc = 0;
  2643. /* Validate each FMRs inside the fmr_list */
  2644. list_for_each_entry(ib_fmr, fmr_list, list) {
  2645. fmr = container_of(ib_fmr, struct bnxt_re_fmr, ib_fmr);
  2646. rdev = fmr->rdev;
  2647. if (rdev) {
  2648. rc = bnxt_qplib_dereg_mrw(&rdev->qplib_res,
  2649. &fmr->qplib_fmr, true);
  2650. if (rc)
  2651. break;
  2652. }
  2653. }
  2654. return rc;
  2655. }
  2656. int bnxt_re_dealloc_fmr(struct ib_fmr *ib_fmr)
  2657. {
  2658. struct bnxt_re_fmr *fmr = container_of(ib_fmr, struct bnxt_re_fmr,
  2659. ib_fmr);
  2660. struct bnxt_re_dev *rdev = fmr->rdev;
  2661. int rc;
  2662. rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &fmr->qplib_fmr);
  2663. if (rc)
  2664. dev_err(rdev_to_dev(rdev), "Failed to free FMR");
  2665. kfree(fmr);
  2666. atomic_dec(&rdev->mr_count);
  2667. return rc;
  2668. }
  2669. /* uverbs */
  2670. struct ib_mr *bnxt_re_reg_user_mr(struct ib_pd *ib_pd, u64 start, u64 length,
  2671. u64 virt_addr, int mr_access_flags,
  2672. struct ib_udata *udata)
  2673. {
  2674. struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
  2675. struct bnxt_re_dev *rdev = pd->rdev;
  2676. struct bnxt_re_mr *mr;
  2677. struct ib_umem *umem;
  2678. u64 *pbl_tbl, *pbl_tbl_orig;
  2679. int i, umem_pgs, pages, page_shift, rc;
  2680. struct scatterlist *sg;
  2681. int entry;
  2682. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2683. if (!mr)
  2684. return ERR_PTR(-ENOMEM);
  2685. mr->rdev = rdev;
  2686. mr->qplib_mr.pd = &pd->qplib_pd;
  2687. mr->qplib_mr.flags = __from_ib_access_flags(mr_access_flags);
  2688. mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_MR;
  2689. umem = ib_umem_get(ib_pd->uobject->context, start, length,
  2690. mr_access_flags, 0);
  2691. if (IS_ERR(umem)) {
  2692. dev_err(rdev_to_dev(rdev), "Failed to get umem");
  2693. rc = -EFAULT;
  2694. goto free_mr;
  2695. }
  2696. mr->ib_umem = umem;
  2697. rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2698. if (rc) {
  2699. dev_err(rdev_to_dev(rdev), "Failed to allocate MR");
  2700. goto release_umem;
  2701. }
  2702. /* The fixed portion of the rkey is the same as the lkey */
  2703. mr->ib_mr.rkey = mr->qplib_mr.rkey;
  2704. mr->qplib_mr.va = virt_addr;
  2705. umem_pgs = ib_umem_page_count(umem);
  2706. if (!umem_pgs) {
  2707. dev_err(rdev_to_dev(rdev), "umem is invalid!");
  2708. rc = -EINVAL;
  2709. goto free_mrw;
  2710. }
  2711. mr->qplib_mr.total_size = length;
  2712. pbl_tbl = kcalloc(umem_pgs, sizeof(u64 *), GFP_KERNEL);
  2713. if (!pbl_tbl) {
  2714. rc = -EINVAL;
  2715. goto free_mrw;
  2716. }
  2717. pbl_tbl_orig = pbl_tbl;
  2718. page_shift = ilog2(umem->page_size);
  2719. if (umem->hugetlb) {
  2720. dev_err(rdev_to_dev(rdev), "umem hugetlb not supported!");
  2721. rc = -EFAULT;
  2722. goto fail;
  2723. }
  2724. if (umem->page_size != PAGE_SIZE) {
  2725. dev_err(rdev_to_dev(rdev), "umem page size unsupported!");
  2726. rc = -EFAULT;
  2727. goto fail;
  2728. }
  2729. /* Map umem buf ptrs to the PBL */
  2730. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  2731. pages = sg_dma_len(sg) >> page_shift;
  2732. for (i = 0; i < pages; i++, pbl_tbl++)
  2733. *pbl_tbl = sg_dma_address(sg) + (i << page_shift);
  2734. }
  2735. rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, pbl_tbl_orig,
  2736. umem_pgs, false);
  2737. if (rc) {
  2738. dev_err(rdev_to_dev(rdev), "Failed to register user MR");
  2739. goto fail;
  2740. }
  2741. kfree(pbl_tbl_orig);
  2742. mr->ib_mr.lkey = mr->qplib_mr.lkey;
  2743. mr->ib_mr.rkey = mr->qplib_mr.lkey;
  2744. atomic_inc(&rdev->mr_count);
  2745. return &mr->ib_mr;
  2746. fail:
  2747. kfree(pbl_tbl_orig);
  2748. free_mrw:
  2749. bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
  2750. release_umem:
  2751. ib_umem_release(umem);
  2752. free_mr:
  2753. kfree(mr);
  2754. return ERR_PTR(rc);
  2755. }
  2756. struct ib_ucontext *bnxt_re_alloc_ucontext(struct ib_device *ibdev,
  2757. struct ib_udata *udata)
  2758. {
  2759. struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
  2760. struct bnxt_re_uctx_resp resp;
  2761. struct bnxt_re_ucontext *uctx;
  2762. struct bnxt_qplib_dev_attr *dev_attr = &rdev->dev_attr;
  2763. int rc;
  2764. dev_dbg(rdev_to_dev(rdev), "ABI version requested %d",
  2765. ibdev->uverbs_abi_ver);
  2766. if (ibdev->uverbs_abi_ver != BNXT_RE_ABI_VERSION) {
  2767. dev_dbg(rdev_to_dev(rdev), " is different from the device %d ",
  2768. BNXT_RE_ABI_VERSION);
  2769. return ERR_PTR(-EPERM);
  2770. }
  2771. uctx = kzalloc(sizeof(*uctx), GFP_KERNEL);
  2772. if (!uctx)
  2773. return ERR_PTR(-ENOMEM);
  2774. uctx->rdev = rdev;
  2775. uctx->shpg = (void *)__get_free_page(GFP_KERNEL);
  2776. if (!uctx->shpg) {
  2777. rc = -ENOMEM;
  2778. goto fail;
  2779. }
  2780. spin_lock_init(&uctx->sh_lock);
  2781. resp.dev_id = rdev->en_dev->pdev->devfn; /*Temp, Use idr_alloc instead*/
  2782. resp.max_qp = rdev->qplib_ctx.qpc_count;
  2783. resp.pg_size = PAGE_SIZE;
  2784. resp.cqe_sz = sizeof(struct cq_base);
  2785. resp.max_cqd = dev_attr->max_cq_wqes;
  2786. resp.rsvd = 0;
  2787. rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
  2788. if (rc) {
  2789. dev_err(rdev_to_dev(rdev), "Failed to copy user context");
  2790. rc = -EFAULT;
  2791. goto cfail;
  2792. }
  2793. return &uctx->ib_uctx;
  2794. cfail:
  2795. free_page((unsigned long)uctx->shpg);
  2796. uctx->shpg = NULL;
  2797. fail:
  2798. kfree(uctx);
  2799. return ERR_PTR(rc);
  2800. }
  2801. int bnxt_re_dealloc_ucontext(struct ib_ucontext *ib_uctx)
  2802. {
  2803. struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
  2804. struct bnxt_re_ucontext,
  2805. ib_uctx);
  2806. if (uctx->shpg)
  2807. free_page((unsigned long)uctx->shpg);
  2808. kfree(uctx);
  2809. return 0;
  2810. }
  2811. /* Helper function to mmap the virtual memory from user app */
  2812. int bnxt_re_mmap(struct ib_ucontext *ib_uctx, struct vm_area_struct *vma)
  2813. {
  2814. struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
  2815. struct bnxt_re_ucontext,
  2816. ib_uctx);
  2817. struct bnxt_re_dev *rdev = uctx->rdev;
  2818. u64 pfn;
  2819. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  2820. return -EINVAL;
  2821. if (vma->vm_pgoff) {
  2822. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  2823. if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  2824. PAGE_SIZE, vma->vm_page_prot)) {
  2825. dev_err(rdev_to_dev(rdev), "Failed to map DPI");
  2826. return -EAGAIN;
  2827. }
  2828. } else {
  2829. pfn = virt_to_phys(uctx->shpg) >> PAGE_SHIFT;
  2830. if (remap_pfn_range(vma, vma->vm_start,
  2831. pfn, PAGE_SIZE, vma->vm_page_prot)) {
  2832. dev_err(rdev_to_dev(rdev),
  2833. "Failed to map shared page");
  2834. return -EAGAIN;
  2835. }
  2836. }
  2837. return 0;
  2838. }