main.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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: Main component of the bnxt_re driver
  37. */
  38. #include <linux/module.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/ethtool.h>
  41. #include <linux/mutex.h>
  42. #include <linux/list.h>
  43. #include <linux/rculist.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/pci.h>
  46. #include <net/dcbnl.h>
  47. #include <net/ipv6.h>
  48. #include <net/addrconf.h>
  49. #include <linux/if_ether.h>
  50. #include <rdma/ib_verbs.h>
  51. #include <rdma/ib_user_verbs.h>
  52. #include <rdma/ib_umem.h>
  53. #include <rdma/ib_addr.h>
  54. #include "bnxt_ulp.h"
  55. #include "roce_hsi.h"
  56. #include "qplib_res.h"
  57. #include "qplib_sp.h"
  58. #include "qplib_fp.h"
  59. #include "qplib_rcfw.h"
  60. #include "bnxt_re.h"
  61. #include "ib_verbs.h"
  62. #include <rdma/bnxt_re-abi.h>
  63. #include "bnxt.h"
  64. #include "hw_counters.h"
  65. static char version[] =
  66. BNXT_RE_DESC "\n";
  67. MODULE_AUTHOR("Eddie Wai <[email protected]>");
  68. MODULE_DESCRIPTION(BNXT_RE_DESC);
  69. MODULE_LICENSE("Dual BSD/GPL");
  70. /* globals */
  71. static struct list_head bnxt_re_dev_list = LIST_HEAD_INIT(bnxt_re_dev_list);
  72. /* Mutex to protect the list of bnxt_re devices added */
  73. static DEFINE_MUTEX(bnxt_re_dev_lock);
  74. static struct workqueue_struct *bnxt_re_wq;
  75. static void bnxt_re_remove_device(struct bnxt_re_dev *rdev);
  76. static void bnxt_re_dealloc_driver(struct ib_device *ib_dev);
  77. static void bnxt_re_stop_irq(void *handle);
  78. static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev);
  79. static void bnxt_re_set_drv_mode(struct bnxt_re_dev *rdev, u8 mode)
  80. {
  81. struct bnxt_qplib_chip_ctx *cctx;
  82. cctx = rdev->chip_ctx;
  83. cctx->modes.wqe_mode = bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
  84. mode : BNXT_QPLIB_WQE_MODE_STATIC;
  85. }
  86. static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
  87. {
  88. struct bnxt_qplib_chip_ctx *chip_ctx;
  89. if (!rdev->chip_ctx)
  90. return;
  91. chip_ctx = rdev->chip_ctx;
  92. rdev->chip_ctx = NULL;
  93. rdev->rcfw.res = NULL;
  94. rdev->qplib_res.cctx = NULL;
  95. rdev->qplib_res.pdev = NULL;
  96. rdev->qplib_res.netdev = NULL;
  97. kfree(chip_ctx);
  98. }
  99. static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev, u8 wqe_mode)
  100. {
  101. struct bnxt_qplib_chip_ctx *chip_ctx;
  102. struct bnxt_en_dev *en_dev;
  103. struct bnxt *bp;
  104. en_dev = rdev->en_dev;
  105. bp = netdev_priv(en_dev->net);
  106. chip_ctx = kzalloc(sizeof(*chip_ctx), GFP_KERNEL);
  107. if (!chip_ctx)
  108. return -ENOMEM;
  109. chip_ctx->chip_num = bp->chip_num;
  110. chip_ctx->hw_stats_size = bp->hw_ring_stats_size;
  111. rdev->chip_ctx = chip_ctx;
  112. /* rest members to follow eventually */
  113. rdev->qplib_res.cctx = rdev->chip_ctx;
  114. rdev->rcfw.res = &rdev->qplib_res;
  115. rdev->qplib_res.dattr = &rdev->dev_attr;
  116. rdev->qplib_res.is_vf = BNXT_VF(bp);
  117. bnxt_re_set_drv_mode(rdev, wqe_mode);
  118. if (bnxt_qplib_determine_atomics(en_dev->pdev))
  119. ibdev_info(&rdev->ibdev,
  120. "platform doesn't support global atomics.");
  121. return 0;
  122. }
  123. /* SR-IOV helper functions */
  124. static void bnxt_re_get_sriov_func_type(struct bnxt_re_dev *rdev)
  125. {
  126. struct bnxt *bp;
  127. bp = netdev_priv(rdev->en_dev->net);
  128. if (BNXT_VF(bp))
  129. rdev->is_virtfn = 1;
  130. }
  131. /* Set the maximum number of each resource that the driver actually wants
  132. * to allocate. This may be up to the maximum number the firmware has
  133. * reserved for the function. The driver may choose to allocate fewer
  134. * resources than the firmware maximum.
  135. */
  136. static void bnxt_re_limit_pf_res(struct bnxt_re_dev *rdev)
  137. {
  138. struct bnxt_qplib_dev_attr *attr;
  139. struct bnxt_qplib_ctx *ctx;
  140. int i;
  141. attr = &rdev->dev_attr;
  142. ctx = &rdev->qplib_ctx;
  143. ctx->qpc_count = min_t(u32, BNXT_RE_MAX_QPC_COUNT,
  144. attr->max_qp);
  145. ctx->mrw_count = BNXT_RE_MAX_MRW_COUNT_256K;
  146. /* Use max_mr from fw since max_mrw does not get set */
  147. ctx->mrw_count = min_t(u32, ctx->mrw_count, attr->max_mr);
  148. ctx->srqc_count = min_t(u32, BNXT_RE_MAX_SRQC_COUNT,
  149. attr->max_srq);
  150. ctx->cq_count = min_t(u32, BNXT_RE_MAX_CQ_COUNT, attr->max_cq);
  151. if (!bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx))
  152. for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
  153. rdev->qplib_ctx.tqm_ctx.qcount[i] =
  154. rdev->dev_attr.tqm_alloc_reqs[i];
  155. }
  156. static void bnxt_re_limit_vf_res(struct bnxt_qplib_ctx *qplib_ctx, u32 num_vf)
  157. {
  158. struct bnxt_qplib_vf_res *vf_res;
  159. u32 mrws = 0;
  160. u32 vf_pct;
  161. u32 nvfs;
  162. vf_res = &qplib_ctx->vf_res;
  163. /*
  164. * Reserve a set of resources for the PF. Divide the remaining
  165. * resources among the VFs
  166. */
  167. vf_pct = 100 - BNXT_RE_PCT_RSVD_FOR_PF;
  168. nvfs = num_vf;
  169. num_vf = 100 * num_vf;
  170. vf_res->max_qp_per_vf = (qplib_ctx->qpc_count * vf_pct) / num_vf;
  171. vf_res->max_srq_per_vf = (qplib_ctx->srqc_count * vf_pct) / num_vf;
  172. vf_res->max_cq_per_vf = (qplib_ctx->cq_count * vf_pct) / num_vf;
  173. /*
  174. * The driver allows many more MRs than other resources. If the
  175. * firmware does also, then reserve a fixed amount for the PF and
  176. * divide the rest among VFs. VFs may use many MRs for NFS
  177. * mounts, ISER, NVME applications, etc. If the firmware severely
  178. * restricts the number of MRs, then let PF have half and divide
  179. * the rest among VFs, as for the other resource types.
  180. */
  181. if (qplib_ctx->mrw_count < BNXT_RE_MAX_MRW_COUNT_64K) {
  182. mrws = qplib_ctx->mrw_count * vf_pct;
  183. nvfs = num_vf;
  184. } else {
  185. mrws = qplib_ctx->mrw_count - BNXT_RE_RESVD_MR_FOR_PF;
  186. }
  187. vf_res->max_mrw_per_vf = (mrws / nvfs);
  188. vf_res->max_gid_per_vf = BNXT_RE_MAX_GID_PER_VF;
  189. }
  190. static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
  191. {
  192. u32 num_vfs;
  193. memset(&rdev->qplib_ctx.vf_res, 0, sizeof(struct bnxt_qplib_vf_res));
  194. bnxt_re_limit_pf_res(rdev);
  195. num_vfs = bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
  196. BNXT_RE_GEN_P5_MAX_VF : rdev->num_vfs;
  197. if (num_vfs)
  198. bnxt_re_limit_vf_res(&rdev->qplib_ctx, num_vfs);
  199. }
  200. /* for handling bnxt_en callbacks later */
  201. static void bnxt_re_stop(void *p)
  202. {
  203. struct bnxt_re_dev *rdev = p;
  204. struct bnxt *bp;
  205. if (!rdev)
  206. return;
  207. ASSERT_RTNL();
  208. /* L2 driver invokes this callback during device error/crash or device
  209. * reset. Current RoCE driver doesn't recover the device in case of
  210. * error. Handle the error by dispatching fatal events to all qps
  211. * ie. by calling bnxt_re_dev_stop and release the MSIx vectors as
  212. * L2 driver want to modify the MSIx table.
  213. */
  214. bp = netdev_priv(rdev->netdev);
  215. ibdev_info(&rdev->ibdev, "Handle device stop call from L2 driver");
  216. /* Check the current device state from L2 structure and move the
  217. * device to detached state if FW_FATAL_COND is set.
  218. * This prevents more commands to HW during clean-up,
  219. * in case the device is already in error.
  220. */
  221. if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
  222. set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
  223. bnxt_re_dev_stop(rdev);
  224. bnxt_re_stop_irq(rdev);
  225. /* Move the device states to detached and avoid sending any more
  226. * commands to HW
  227. */
  228. set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
  229. set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
  230. }
  231. static void bnxt_re_start(void *p)
  232. {
  233. }
  234. static void bnxt_re_sriov_config(void *p, int num_vfs)
  235. {
  236. struct bnxt_re_dev *rdev = p;
  237. if (!rdev)
  238. return;
  239. if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
  240. return;
  241. rdev->num_vfs = num_vfs;
  242. if (!bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx)) {
  243. bnxt_re_set_resource_limits(rdev);
  244. bnxt_qplib_set_func_resources(&rdev->qplib_res, &rdev->rcfw,
  245. &rdev->qplib_ctx);
  246. }
  247. }
  248. static void bnxt_re_shutdown(void *p)
  249. {
  250. struct bnxt_re_dev *rdev = p;
  251. if (!rdev)
  252. return;
  253. ASSERT_RTNL();
  254. /* Release the MSIx vectors before queuing unregister */
  255. bnxt_re_stop_irq(rdev);
  256. ib_unregister_device_queued(&rdev->ibdev);
  257. }
  258. static void bnxt_re_stop_irq(void *handle)
  259. {
  260. struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
  261. struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
  262. struct bnxt_qplib_nq *nq;
  263. int indx;
  264. for (indx = BNXT_RE_NQ_IDX; indx < rdev->num_msix; indx++) {
  265. nq = &rdev->nq[indx - 1];
  266. bnxt_qplib_nq_stop_irq(nq, false);
  267. }
  268. bnxt_qplib_rcfw_stop_irq(rcfw, false);
  269. }
  270. static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
  271. {
  272. struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
  273. struct bnxt_msix_entry *msix_ent = rdev->msix_entries;
  274. struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
  275. struct bnxt_qplib_nq *nq;
  276. int indx, rc;
  277. if (!ent) {
  278. /* Not setting the f/w timeout bit in rcfw.
  279. * During the driver unload the first command
  280. * to f/w will timeout and that will set the
  281. * timeout bit.
  282. */
  283. ibdev_err(&rdev->ibdev, "Failed to re-start IRQs\n");
  284. return;
  285. }
  286. /* Vectors may change after restart, so update with new vectors
  287. * in device sctructure.
  288. */
  289. for (indx = 0; indx < rdev->num_msix; indx++)
  290. rdev->msix_entries[indx].vector = ent[indx].vector;
  291. rc = bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
  292. false);
  293. if (rc) {
  294. ibdev_warn(&rdev->ibdev, "Failed to reinit CREQ\n");
  295. return;
  296. }
  297. for (indx = BNXT_RE_NQ_IDX ; indx < rdev->num_msix; indx++) {
  298. nq = &rdev->nq[indx - 1];
  299. rc = bnxt_qplib_nq_start_irq(nq, indx - 1,
  300. msix_ent[indx].vector, false);
  301. if (rc) {
  302. ibdev_warn(&rdev->ibdev, "Failed to reinit NQ index %d\n",
  303. indx - 1);
  304. return;
  305. }
  306. }
  307. }
  308. static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
  309. .ulp_async_notifier = NULL,
  310. .ulp_stop = bnxt_re_stop,
  311. .ulp_start = bnxt_re_start,
  312. .ulp_sriov_config = bnxt_re_sriov_config,
  313. .ulp_shutdown = bnxt_re_shutdown,
  314. .ulp_irq_stop = bnxt_re_stop_irq,
  315. .ulp_irq_restart = bnxt_re_start_irq
  316. };
  317. /* RoCE -> Net driver */
  318. /* Driver registration routines used to let the networking driver (bnxt_en)
  319. * to know that the RoCE driver is now installed
  320. */
  321. static int bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev)
  322. {
  323. struct bnxt_en_dev *en_dev;
  324. int rc;
  325. if (!rdev)
  326. return -EINVAL;
  327. en_dev = rdev->en_dev;
  328. rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
  329. BNXT_ROCE_ULP);
  330. return rc;
  331. }
  332. static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
  333. {
  334. struct bnxt_en_dev *en_dev;
  335. int rc = 0;
  336. if (!rdev)
  337. return -EINVAL;
  338. en_dev = rdev->en_dev;
  339. rc = en_dev->en_ops->bnxt_register_device(en_dev, BNXT_ROCE_ULP,
  340. &bnxt_re_ulp_ops, rdev);
  341. rdev->qplib_res.pdev = rdev->en_dev->pdev;
  342. return rc;
  343. }
  344. static int bnxt_re_free_msix(struct bnxt_re_dev *rdev)
  345. {
  346. struct bnxt_en_dev *en_dev;
  347. int rc;
  348. if (!rdev)
  349. return -EINVAL;
  350. en_dev = rdev->en_dev;
  351. rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
  352. return rc;
  353. }
  354. static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
  355. {
  356. int rc = 0, num_msix_want = BNXT_RE_MAX_MSIX, num_msix_got;
  357. struct bnxt_en_dev *en_dev;
  358. if (!rdev)
  359. return -EINVAL;
  360. en_dev = rdev->en_dev;
  361. num_msix_want = min_t(u32, BNXT_RE_MAX_MSIX, num_online_cpus());
  362. num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
  363. rdev->msix_entries,
  364. num_msix_want);
  365. if (num_msix_got < BNXT_RE_MIN_MSIX) {
  366. rc = -EINVAL;
  367. goto done;
  368. }
  369. if (num_msix_got != num_msix_want) {
  370. ibdev_warn(&rdev->ibdev,
  371. "Requested %d MSI-X vectors, got %d\n",
  372. num_msix_want, num_msix_got);
  373. }
  374. rdev->num_msix = num_msix_got;
  375. done:
  376. return rc;
  377. }
  378. static void bnxt_re_init_hwrm_hdr(struct bnxt_re_dev *rdev, struct input *hdr,
  379. u16 opcd, u16 crid, u16 trid)
  380. {
  381. hdr->req_type = cpu_to_le16(opcd);
  382. hdr->cmpl_ring = cpu_to_le16(crid);
  383. hdr->target_id = cpu_to_le16(trid);
  384. }
  385. static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
  386. int msg_len, void *resp, int resp_max_len,
  387. int timeout)
  388. {
  389. fw_msg->msg = msg;
  390. fw_msg->msg_len = msg_len;
  391. fw_msg->resp = resp;
  392. fw_msg->resp_max_len = resp_max_len;
  393. fw_msg->timeout = timeout;
  394. }
  395. static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev,
  396. u16 fw_ring_id, int type)
  397. {
  398. struct bnxt_en_dev *en_dev = rdev->en_dev;
  399. struct hwrm_ring_free_input req = {0};
  400. struct hwrm_ring_free_output resp;
  401. struct bnxt_fw_msg fw_msg;
  402. int rc = -EINVAL;
  403. if (!en_dev)
  404. return rc;
  405. if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
  406. return 0;
  407. memset(&fw_msg, 0, sizeof(fw_msg));
  408. bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
  409. req.ring_type = type;
  410. req.ring_id = cpu_to_le16(fw_ring_id);
  411. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  412. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  413. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  414. if (rc)
  415. ibdev_err(&rdev->ibdev, "Failed to free HW ring:%d :%#x",
  416. req.ring_id, rc);
  417. return rc;
  418. }
  419. static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev,
  420. struct bnxt_re_ring_attr *ring_attr,
  421. u16 *fw_ring_id)
  422. {
  423. struct bnxt_en_dev *en_dev = rdev->en_dev;
  424. struct hwrm_ring_alloc_input req = {0};
  425. struct hwrm_ring_alloc_output resp;
  426. struct bnxt_fw_msg fw_msg;
  427. int rc = -EINVAL;
  428. if (!en_dev)
  429. return rc;
  430. memset(&fw_msg, 0, sizeof(fw_msg));
  431. bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
  432. req.enables = 0;
  433. req.page_tbl_addr = cpu_to_le64(ring_attr->dma_arr[0]);
  434. if (ring_attr->pages > 1) {
  435. /* Page size is in log2 units */
  436. req.page_size = BNXT_PAGE_SHIFT;
  437. req.page_tbl_depth = 1;
  438. }
  439. req.fbo = 0;
  440. /* Association of ring index with doorbell index and MSIX number */
  441. req.logical_id = cpu_to_le16(ring_attr->lrid);
  442. req.length = cpu_to_le32(ring_attr->depth + 1);
  443. req.ring_type = ring_attr->type;
  444. req.int_mode = ring_attr->mode;
  445. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  446. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  447. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  448. if (!rc)
  449. *fw_ring_id = le16_to_cpu(resp.ring_id);
  450. return rc;
  451. }
  452. static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
  453. u32 fw_stats_ctx_id)
  454. {
  455. struct bnxt_en_dev *en_dev = rdev->en_dev;
  456. struct hwrm_stat_ctx_free_input req = {};
  457. struct hwrm_stat_ctx_free_output resp = {};
  458. struct bnxt_fw_msg fw_msg;
  459. int rc = -EINVAL;
  460. if (!en_dev)
  461. return rc;
  462. if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
  463. return 0;
  464. memset(&fw_msg, 0, sizeof(fw_msg));
  465. bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, -1);
  466. req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
  467. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  468. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  469. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  470. if (rc)
  471. ibdev_err(&rdev->ibdev, "Failed to free HW stats context %#x",
  472. rc);
  473. return rc;
  474. }
  475. static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
  476. dma_addr_t dma_map,
  477. u32 *fw_stats_ctx_id)
  478. {
  479. struct bnxt_qplib_chip_ctx *chip_ctx = rdev->chip_ctx;
  480. struct hwrm_stat_ctx_alloc_output resp = {0};
  481. struct hwrm_stat_ctx_alloc_input req = {0};
  482. struct bnxt_en_dev *en_dev = rdev->en_dev;
  483. struct bnxt_fw_msg fw_msg;
  484. int rc = -EINVAL;
  485. *fw_stats_ctx_id = INVALID_STATS_CTX_ID;
  486. if (!en_dev)
  487. return rc;
  488. memset(&fw_msg, 0, sizeof(fw_msg));
  489. bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_ALLOC, -1, -1);
  490. req.update_period_ms = cpu_to_le32(1000);
  491. req.stats_dma_addr = cpu_to_le64(dma_map);
  492. req.stats_dma_length = cpu_to_le16(chip_ctx->hw_stats_size);
  493. req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
  494. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  495. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  496. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  497. if (!rc)
  498. *fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
  499. return rc;
  500. }
  501. /* Device */
  502. static bool is_bnxt_re_dev(struct net_device *netdev)
  503. {
  504. struct ethtool_drvinfo drvinfo;
  505. if (netdev->ethtool_ops && netdev->ethtool_ops->get_drvinfo) {
  506. memset(&drvinfo, 0, sizeof(drvinfo));
  507. netdev->ethtool_ops->get_drvinfo(netdev, &drvinfo);
  508. if (strcmp(drvinfo.driver, "bnxt_en"))
  509. return false;
  510. return true;
  511. }
  512. return false;
  513. }
  514. static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
  515. {
  516. struct ib_device *ibdev =
  517. ib_device_get_by_netdev(netdev, RDMA_DRIVER_BNXT_RE);
  518. if (!ibdev)
  519. return NULL;
  520. return container_of(ibdev, struct bnxt_re_dev, ibdev);
  521. }
  522. static struct bnxt_en_dev *bnxt_re_dev_probe(struct net_device *netdev)
  523. {
  524. struct bnxt_en_dev *en_dev;
  525. struct pci_dev *pdev;
  526. en_dev = bnxt_ulp_probe(netdev);
  527. if (IS_ERR(en_dev))
  528. return en_dev;
  529. pdev = en_dev->pdev;
  530. if (!pdev)
  531. return ERR_PTR(-EINVAL);
  532. if (!(en_dev->flags & BNXT_EN_FLAG_ROCE_CAP)) {
  533. dev_info(&pdev->dev,
  534. "%s: probe error: RoCE is not supported on this device",
  535. ROCE_DRV_MODULE_NAME);
  536. return ERR_PTR(-ENODEV);
  537. }
  538. dev_hold(netdev);
  539. return en_dev;
  540. }
  541. static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
  542. char *buf)
  543. {
  544. struct bnxt_re_dev *rdev =
  545. rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
  546. return sysfs_emit(buf, "0x%x\n", rdev->en_dev->pdev->vendor);
  547. }
  548. static DEVICE_ATTR_RO(hw_rev);
  549. static ssize_t hca_type_show(struct device *device,
  550. struct device_attribute *attr, char *buf)
  551. {
  552. struct bnxt_re_dev *rdev =
  553. rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
  554. return sysfs_emit(buf, "%s\n", rdev->ibdev.node_desc);
  555. }
  556. static DEVICE_ATTR_RO(hca_type);
  557. static struct attribute *bnxt_re_attributes[] = {
  558. &dev_attr_hw_rev.attr,
  559. &dev_attr_hca_type.attr,
  560. NULL
  561. };
  562. static const struct attribute_group bnxt_re_dev_attr_group = {
  563. .attrs = bnxt_re_attributes,
  564. };
  565. static const struct ib_device_ops bnxt_re_dev_ops = {
  566. .owner = THIS_MODULE,
  567. .driver_id = RDMA_DRIVER_BNXT_RE,
  568. .uverbs_abi_ver = BNXT_RE_ABI_VERSION,
  569. .add_gid = bnxt_re_add_gid,
  570. .alloc_hw_port_stats = bnxt_re_ib_alloc_hw_port_stats,
  571. .alloc_mr = bnxt_re_alloc_mr,
  572. .alloc_pd = bnxt_re_alloc_pd,
  573. .alloc_ucontext = bnxt_re_alloc_ucontext,
  574. .create_ah = bnxt_re_create_ah,
  575. .create_cq = bnxt_re_create_cq,
  576. .create_qp = bnxt_re_create_qp,
  577. .create_srq = bnxt_re_create_srq,
  578. .create_user_ah = bnxt_re_create_ah,
  579. .dealloc_driver = bnxt_re_dealloc_driver,
  580. .dealloc_pd = bnxt_re_dealloc_pd,
  581. .dealloc_ucontext = bnxt_re_dealloc_ucontext,
  582. .del_gid = bnxt_re_del_gid,
  583. .dereg_mr = bnxt_re_dereg_mr,
  584. .destroy_ah = bnxt_re_destroy_ah,
  585. .destroy_cq = bnxt_re_destroy_cq,
  586. .destroy_qp = bnxt_re_destroy_qp,
  587. .destroy_srq = bnxt_re_destroy_srq,
  588. .device_group = &bnxt_re_dev_attr_group,
  589. .get_dev_fw_str = bnxt_re_query_fw_str,
  590. .get_dma_mr = bnxt_re_get_dma_mr,
  591. .get_hw_stats = bnxt_re_ib_get_hw_stats,
  592. .get_link_layer = bnxt_re_get_link_layer,
  593. .get_port_immutable = bnxt_re_get_port_immutable,
  594. .map_mr_sg = bnxt_re_map_mr_sg,
  595. .mmap = bnxt_re_mmap,
  596. .modify_qp = bnxt_re_modify_qp,
  597. .modify_srq = bnxt_re_modify_srq,
  598. .poll_cq = bnxt_re_poll_cq,
  599. .post_recv = bnxt_re_post_recv,
  600. .post_send = bnxt_re_post_send,
  601. .post_srq_recv = bnxt_re_post_srq_recv,
  602. .query_ah = bnxt_re_query_ah,
  603. .query_device = bnxt_re_query_device,
  604. .query_pkey = bnxt_re_query_pkey,
  605. .query_port = bnxt_re_query_port,
  606. .query_qp = bnxt_re_query_qp,
  607. .query_srq = bnxt_re_query_srq,
  608. .reg_user_mr = bnxt_re_reg_user_mr,
  609. .req_notify_cq = bnxt_re_req_notify_cq,
  610. INIT_RDMA_OBJ_SIZE(ib_ah, bnxt_re_ah, ib_ah),
  611. INIT_RDMA_OBJ_SIZE(ib_cq, bnxt_re_cq, ib_cq),
  612. INIT_RDMA_OBJ_SIZE(ib_pd, bnxt_re_pd, ib_pd),
  613. INIT_RDMA_OBJ_SIZE(ib_qp, bnxt_re_qp, ib_qp),
  614. INIT_RDMA_OBJ_SIZE(ib_srq, bnxt_re_srq, ib_srq),
  615. INIT_RDMA_OBJ_SIZE(ib_ucontext, bnxt_re_ucontext, ib_uctx),
  616. };
  617. static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
  618. {
  619. struct ib_device *ibdev = &rdev->ibdev;
  620. int ret;
  621. /* ib device init */
  622. ibdev->node_type = RDMA_NODE_IB_CA;
  623. strscpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
  624. strlen(BNXT_RE_DESC) + 5);
  625. ibdev->phys_port_cnt = 1;
  626. addrconf_addr_eui48((u8 *)&ibdev->node_guid, rdev->netdev->dev_addr);
  627. ibdev->num_comp_vectors = rdev->num_msix - 1;
  628. ibdev->dev.parent = &rdev->en_dev->pdev->dev;
  629. ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
  630. ib_set_device_ops(ibdev, &bnxt_re_dev_ops);
  631. ret = ib_device_set_netdev(&rdev->ibdev, rdev->netdev, 1);
  632. if (ret)
  633. return ret;
  634. dma_set_max_seg_size(&rdev->en_dev->pdev->dev, UINT_MAX);
  635. return ib_register_device(ibdev, "bnxt_re%d", &rdev->en_dev->pdev->dev);
  636. }
  637. static void bnxt_re_dev_remove(struct bnxt_re_dev *rdev)
  638. {
  639. dev_put(rdev->netdev);
  640. rdev->netdev = NULL;
  641. mutex_lock(&bnxt_re_dev_lock);
  642. list_del_rcu(&rdev->list);
  643. mutex_unlock(&bnxt_re_dev_lock);
  644. synchronize_rcu();
  645. }
  646. static struct bnxt_re_dev *bnxt_re_dev_add(struct net_device *netdev,
  647. struct bnxt_en_dev *en_dev)
  648. {
  649. struct bnxt_re_dev *rdev;
  650. /* Allocate bnxt_re_dev instance here */
  651. rdev = ib_alloc_device(bnxt_re_dev, ibdev);
  652. if (!rdev) {
  653. ibdev_err(NULL, "%s: bnxt_re_dev allocation failure!",
  654. ROCE_DRV_MODULE_NAME);
  655. return NULL;
  656. }
  657. /* Default values */
  658. rdev->netdev = netdev;
  659. dev_hold(rdev->netdev);
  660. rdev->en_dev = en_dev;
  661. rdev->id = rdev->en_dev->pdev->devfn;
  662. INIT_LIST_HEAD(&rdev->qp_list);
  663. mutex_init(&rdev->qp_lock);
  664. atomic_set(&rdev->qp_count, 0);
  665. atomic_set(&rdev->cq_count, 0);
  666. atomic_set(&rdev->srq_count, 0);
  667. atomic_set(&rdev->mr_count, 0);
  668. atomic_set(&rdev->mw_count, 0);
  669. atomic_set(&rdev->ah_count, 0);
  670. atomic_set(&rdev->pd_count, 0);
  671. rdev->cosq[0] = 0xFFFF;
  672. rdev->cosq[1] = 0xFFFF;
  673. mutex_lock(&bnxt_re_dev_lock);
  674. list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
  675. mutex_unlock(&bnxt_re_dev_lock);
  676. return rdev;
  677. }
  678. static int bnxt_re_handle_unaffi_async_event(struct creq_func_event
  679. *unaffi_async)
  680. {
  681. switch (unaffi_async->event) {
  682. case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
  683. break;
  684. case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
  685. break;
  686. case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
  687. break;
  688. case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
  689. break;
  690. case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
  691. break;
  692. case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
  693. break;
  694. case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
  695. break;
  696. case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
  697. break;
  698. case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
  699. break;
  700. case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
  701. break;
  702. case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
  703. break;
  704. default:
  705. return -EINVAL;
  706. }
  707. return 0;
  708. }
  709. static int bnxt_re_handle_qp_async_event(struct creq_qp_event *qp_event,
  710. struct bnxt_re_qp *qp)
  711. {
  712. struct ib_event event;
  713. unsigned int flags;
  714. if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR &&
  715. rdma_is_kernel_res(&qp->ib_qp.res)) {
  716. flags = bnxt_re_lock_cqs(qp);
  717. bnxt_qplib_add_flush_qp(&qp->qplib_qp);
  718. bnxt_re_unlock_cqs(qp, flags);
  719. }
  720. memset(&event, 0, sizeof(event));
  721. if (qp->qplib_qp.srq) {
  722. event.device = &qp->rdev->ibdev;
  723. event.element.qp = &qp->ib_qp;
  724. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  725. }
  726. if (event.device && qp->ib_qp.event_handler)
  727. qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
  728. return 0;
  729. }
  730. static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
  731. void *obj)
  732. {
  733. int rc = 0;
  734. u8 event;
  735. if (!obj)
  736. return rc; /* QP was already dead, still return success */
  737. event = affi_async->event;
  738. if (event == CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION) {
  739. struct bnxt_qplib_qp *lib_qp = obj;
  740. struct bnxt_re_qp *qp = container_of(lib_qp, struct bnxt_re_qp,
  741. qplib_qp);
  742. rc = bnxt_re_handle_qp_async_event(affi_async, qp);
  743. }
  744. return rc;
  745. }
  746. static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
  747. void *aeqe, void *obj)
  748. {
  749. struct creq_qp_event *affi_async;
  750. struct creq_func_event *unaffi_async;
  751. u8 type;
  752. int rc;
  753. type = ((struct creq_base *)aeqe)->type;
  754. if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
  755. unaffi_async = aeqe;
  756. rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
  757. } else {
  758. affi_async = aeqe;
  759. rc = bnxt_re_handle_affi_async_event(affi_async, obj);
  760. }
  761. return rc;
  762. }
  763. static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
  764. struct bnxt_qplib_srq *handle, u8 event)
  765. {
  766. struct bnxt_re_srq *srq = container_of(handle, struct bnxt_re_srq,
  767. qplib_srq);
  768. struct ib_event ib_event;
  769. ib_event.device = &srq->rdev->ibdev;
  770. ib_event.element.srq = &srq->ib_srq;
  771. if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
  772. ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
  773. else
  774. ib_event.event = IB_EVENT_SRQ_ERR;
  775. if (srq->ib_srq.event_handler) {
  776. /* Lock event_handler? */
  777. (*srq->ib_srq.event_handler)(&ib_event,
  778. srq->ib_srq.srq_context);
  779. }
  780. return 0;
  781. }
  782. static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
  783. struct bnxt_qplib_cq *handle)
  784. {
  785. struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
  786. qplib_cq);
  787. if (cq->ib_cq.comp_handler) {
  788. /* Lock comp_handler? */
  789. (*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
  790. }
  791. return 0;
  792. }
  793. #define BNXT_RE_GEN_P5_PF_NQ_DB 0x10000
  794. #define BNXT_RE_GEN_P5_VF_NQ_DB 0x4000
  795. static u32 bnxt_re_get_nqdb_offset(struct bnxt_re_dev *rdev, u16 indx)
  796. {
  797. return bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx) ?
  798. (rdev->is_virtfn ? BNXT_RE_GEN_P5_VF_NQ_DB :
  799. BNXT_RE_GEN_P5_PF_NQ_DB) :
  800. rdev->msix_entries[indx].db_offset;
  801. }
  802. static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
  803. {
  804. int i;
  805. for (i = 1; i < rdev->num_msix; i++)
  806. bnxt_qplib_disable_nq(&rdev->nq[i - 1]);
  807. if (rdev->qplib_res.rcfw)
  808. bnxt_qplib_cleanup_res(&rdev->qplib_res);
  809. }
  810. static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
  811. {
  812. int num_vec_enabled = 0;
  813. int rc = 0, i;
  814. u32 db_offt;
  815. bnxt_qplib_init_res(&rdev->qplib_res);
  816. for (i = 1; i < rdev->num_msix ; i++) {
  817. db_offt = bnxt_re_get_nqdb_offset(rdev, i);
  818. rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq[i - 1],
  819. i - 1, rdev->msix_entries[i].vector,
  820. db_offt, &bnxt_re_cqn_handler,
  821. &bnxt_re_srqn_handler);
  822. if (rc) {
  823. ibdev_err(&rdev->ibdev,
  824. "Failed to enable NQ with rc = 0x%x", rc);
  825. goto fail;
  826. }
  827. num_vec_enabled++;
  828. }
  829. return 0;
  830. fail:
  831. for (i = num_vec_enabled; i >= 0; i--)
  832. bnxt_qplib_disable_nq(&rdev->nq[i]);
  833. return rc;
  834. }
  835. static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev)
  836. {
  837. u8 type;
  838. int i;
  839. for (i = 0; i < rdev->num_msix - 1; i++) {
  840. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  841. bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
  842. bnxt_qplib_free_nq(&rdev->nq[i]);
  843. rdev->nq[i].res = NULL;
  844. }
  845. }
  846. static void bnxt_re_free_res(struct bnxt_re_dev *rdev)
  847. {
  848. bnxt_re_free_nq_res(rdev);
  849. if (rdev->qplib_res.dpi_tbl.max) {
  850. bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
  851. &rdev->qplib_res.dpi_tbl,
  852. &rdev->dpi_privileged);
  853. }
  854. if (rdev->qplib_res.rcfw) {
  855. bnxt_qplib_free_res(&rdev->qplib_res);
  856. rdev->qplib_res.rcfw = NULL;
  857. }
  858. }
  859. static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
  860. {
  861. struct bnxt_re_ring_attr rattr = {};
  862. int num_vec_created = 0;
  863. int rc = 0, i;
  864. u8 type;
  865. /* Configure and allocate resources for qplib */
  866. rdev->qplib_res.rcfw = &rdev->rcfw;
  867. rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
  868. rdev->is_virtfn);
  869. if (rc)
  870. goto fail;
  871. rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
  872. rdev->netdev, &rdev->dev_attr);
  873. if (rc)
  874. goto fail;
  875. rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res.dpi_tbl,
  876. &rdev->dpi_privileged,
  877. rdev);
  878. if (rc)
  879. goto dealloc_res;
  880. for (i = 0; i < rdev->num_msix - 1; i++) {
  881. struct bnxt_qplib_nq *nq;
  882. nq = &rdev->nq[i];
  883. nq->hwq.max_elements = BNXT_QPLIB_NQE_MAX_CNT;
  884. rc = bnxt_qplib_alloc_nq(&rdev->qplib_res, &rdev->nq[i]);
  885. if (rc) {
  886. ibdev_err(&rdev->ibdev, "Alloc Failed NQ%d rc:%#x",
  887. i, rc);
  888. goto free_nq;
  889. }
  890. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  891. rattr.dma_arr = nq->hwq.pbl[PBL_LVL_0].pg_map_arr;
  892. rattr.pages = nq->hwq.pbl[rdev->nq[i].hwq.level].pg_count;
  893. rattr.type = type;
  894. rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  895. rattr.depth = BNXT_QPLIB_NQE_MAX_CNT - 1;
  896. rattr.lrid = rdev->msix_entries[i + 1].ring_idx;
  897. rc = bnxt_re_net_ring_alloc(rdev, &rattr, &nq->ring_id);
  898. if (rc) {
  899. ibdev_err(&rdev->ibdev,
  900. "Failed to allocate NQ fw id with rc = 0x%x",
  901. rc);
  902. bnxt_qplib_free_nq(&rdev->nq[i]);
  903. goto free_nq;
  904. }
  905. num_vec_created++;
  906. }
  907. return 0;
  908. free_nq:
  909. for (i = num_vec_created - 1; i >= 0; i--) {
  910. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  911. bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
  912. bnxt_qplib_free_nq(&rdev->nq[i]);
  913. }
  914. bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
  915. &rdev->qplib_res.dpi_tbl,
  916. &rdev->dpi_privileged);
  917. dealloc_res:
  918. bnxt_qplib_free_res(&rdev->qplib_res);
  919. fail:
  920. rdev->qplib_res.rcfw = NULL;
  921. return rc;
  922. }
  923. static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
  924. u8 port_num, enum ib_event_type event)
  925. {
  926. struct ib_event ib_event;
  927. ib_event.device = ibdev;
  928. if (qp) {
  929. ib_event.element.qp = qp;
  930. ib_event.event = event;
  931. if (qp->event_handler)
  932. qp->event_handler(&ib_event, qp->qp_context);
  933. } else {
  934. ib_event.element.port_num = port_num;
  935. ib_event.event = event;
  936. ib_dispatch_event(&ib_event);
  937. }
  938. }
  939. #define HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN 0x02
  940. static int bnxt_re_query_hwrm_pri2cos(struct bnxt_re_dev *rdev, u8 dir,
  941. u64 *cid_map)
  942. {
  943. struct hwrm_queue_pri2cos_qcfg_input req = {0};
  944. struct bnxt *bp = netdev_priv(rdev->netdev);
  945. struct hwrm_queue_pri2cos_qcfg_output resp;
  946. struct bnxt_en_dev *en_dev = rdev->en_dev;
  947. struct bnxt_fw_msg fw_msg;
  948. u32 flags = 0;
  949. u8 *qcfgmap, *tmp_map;
  950. int rc = 0, i;
  951. if (!cid_map)
  952. return -EINVAL;
  953. memset(&fw_msg, 0, sizeof(fw_msg));
  954. bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
  955. HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
  956. flags |= (dir & 0x01);
  957. flags |= HWRM_QUEUE_PRI2COS_QCFG_INPUT_FLAGS_IVLAN;
  958. req.flags = cpu_to_le32(flags);
  959. req.port_id = bp->pf.port_id;
  960. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  961. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  962. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  963. if (rc)
  964. return rc;
  965. if (resp.queue_cfg_info) {
  966. ibdev_warn(&rdev->ibdev,
  967. "Asymmetric cos queue configuration detected");
  968. ibdev_warn(&rdev->ibdev,
  969. " on device, QoS may not be fully functional\n");
  970. }
  971. qcfgmap = &resp.pri0_cos_queue_id;
  972. tmp_map = (u8 *)cid_map;
  973. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  974. tmp_map[i] = qcfgmap[i];
  975. return rc;
  976. }
  977. static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
  978. struct bnxt_re_qp *qp)
  979. {
  980. return (qp->ib_qp.qp_type == IB_QPT_GSI) ||
  981. (qp == rdev->gsi_ctx.gsi_sqp);
  982. }
  983. static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
  984. {
  985. int mask = IB_QP_STATE;
  986. struct ib_qp_attr qp_attr;
  987. struct bnxt_re_qp *qp;
  988. qp_attr.qp_state = IB_QPS_ERR;
  989. mutex_lock(&rdev->qp_lock);
  990. list_for_each_entry(qp, &rdev->qp_list, list) {
  991. /* Modify the state of all QPs except QP1/Shadow QP */
  992. if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
  993. if (qp->qplib_qp.state !=
  994. CMDQ_MODIFY_QP_NEW_STATE_RESET &&
  995. qp->qplib_qp.state !=
  996. CMDQ_MODIFY_QP_NEW_STATE_ERR) {
  997. bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
  998. 1, IB_EVENT_QP_FATAL);
  999. bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
  1000. NULL);
  1001. }
  1002. }
  1003. }
  1004. mutex_unlock(&rdev->qp_lock);
  1005. }
  1006. static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
  1007. {
  1008. struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
  1009. struct bnxt_qplib_gid gid;
  1010. u16 gid_idx, index;
  1011. int rc = 0;
  1012. if (!ib_device_try_get(&rdev->ibdev))
  1013. return 0;
  1014. for (index = 0; index < sgid_tbl->active; index++) {
  1015. gid_idx = sgid_tbl->hw_id[index];
  1016. if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
  1017. sizeof(bnxt_qplib_gid_zero)))
  1018. continue;
  1019. /* need to modify the VLAN enable setting of non VLAN GID only
  1020. * as setting is done for VLAN GID while adding GID
  1021. */
  1022. if (sgid_tbl->vlan[index])
  1023. continue;
  1024. memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
  1025. rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
  1026. rdev->qplib_res.netdev->dev_addr);
  1027. }
  1028. ib_device_put(&rdev->ibdev);
  1029. return rc;
  1030. }
  1031. static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
  1032. {
  1033. u32 prio_map = 0, tmp_map = 0;
  1034. struct net_device *netdev;
  1035. struct dcb_app app;
  1036. netdev = rdev->netdev;
  1037. memset(&app, 0, sizeof(app));
  1038. app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
  1039. app.protocol = ETH_P_IBOE;
  1040. tmp_map = dcb_ieee_getapp_mask(netdev, &app);
  1041. prio_map = tmp_map;
  1042. app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
  1043. app.protocol = ROCE_V2_UDP_DPORT;
  1044. tmp_map = dcb_ieee_getapp_mask(netdev, &app);
  1045. prio_map |= tmp_map;
  1046. return prio_map;
  1047. }
  1048. static void bnxt_re_parse_cid_map(u8 prio_map, u8 *cid_map, u16 *cosq)
  1049. {
  1050. u16 prio;
  1051. u8 id;
  1052. for (prio = 0, id = 0; prio < 8; prio++) {
  1053. if (prio_map & (1 << prio)) {
  1054. cosq[id] = cid_map[prio];
  1055. id++;
  1056. if (id == 2) /* Max 2 tcs supported */
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
  1062. {
  1063. u8 prio_map = 0;
  1064. u64 cid_map;
  1065. int rc;
  1066. /* Get priority for roce */
  1067. prio_map = bnxt_re_get_priority_mask(rdev);
  1068. if (prio_map == rdev->cur_prio_map)
  1069. return 0;
  1070. rdev->cur_prio_map = prio_map;
  1071. /* Get cosq id for this priority */
  1072. rc = bnxt_re_query_hwrm_pri2cos(rdev, 0, &cid_map);
  1073. if (rc) {
  1074. ibdev_warn(&rdev->ibdev, "no cos for p_mask %x\n", prio_map);
  1075. return rc;
  1076. }
  1077. /* Parse CoS IDs for app priority */
  1078. bnxt_re_parse_cid_map(prio_map, (u8 *)&cid_map, rdev->cosq);
  1079. /* Config BONO. */
  1080. rc = bnxt_qplib_map_tc2cos(&rdev->qplib_res, rdev->cosq);
  1081. if (rc) {
  1082. ibdev_warn(&rdev->ibdev, "no tc for cos{%x, %x}\n",
  1083. rdev->cosq[0], rdev->cosq[1]);
  1084. return rc;
  1085. }
  1086. /* Actual priorities are not programmed as they are already
  1087. * done by L2 driver; just enable or disable priority vlan tagging
  1088. */
  1089. if ((prio_map == 0 && rdev->qplib_res.prio) ||
  1090. (prio_map != 0 && !rdev->qplib_res.prio)) {
  1091. rdev->qplib_res.prio = prio_map ? true : false;
  1092. bnxt_re_update_gid(rdev);
  1093. }
  1094. return 0;
  1095. }
  1096. static void bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
  1097. {
  1098. struct bnxt_en_dev *en_dev = rdev->en_dev;
  1099. struct hwrm_ver_get_output resp = {0};
  1100. struct hwrm_ver_get_input req = {0};
  1101. struct bnxt_fw_msg fw_msg;
  1102. int rc = 0;
  1103. memset(&fw_msg, 0, sizeof(fw_msg));
  1104. bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
  1105. HWRM_VER_GET, -1, -1);
  1106. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  1107. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  1108. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  1109. bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
  1110. sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
  1111. rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
  1112. if (rc) {
  1113. ibdev_err(&rdev->ibdev, "Failed to query HW version, rc = 0x%x",
  1114. rc);
  1115. return;
  1116. }
  1117. rdev->qplib_ctx.hwrm_intf_ver =
  1118. (u64)le16_to_cpu(resp.hwrm_intf_major) << 48 |
  1119. (u64)le16_to_cpu(resp.hwrm_intf_minor) << 32 |
  1120. (u64)le16_to_cpu(resp.hwrm_intf_build) << 16 |
  1121. le16_to_cpu(resp.hwrm_intf_patch);
  1122. }
  1123. static int bnxt_re_ib_init(struct bnxt_re_dev *rdev)
  1124. {
  1125. int rc = 0;
  1126. u32 event;
  1127. /* Register ib dev */
  1128. rc = bnxt_re_register_ib(rdev);
  1129. if (rc) {
  1130. pr_err("Failed to register with IB: %#x\n", rc);
  1131. return rc;
  1132. }
  1133. dev_info(rdev_to_dev(rdev), "Device registered successfully");
  1134. ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
  1135. &rdev->active_width);
  1136. set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
  1137. event = netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev) ?
  1138. IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  1139. bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, event);
  1140. return rc;
  1141. }
  1142. static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev)
  1143. {
  1144. u8 type;
  1145. int rc;
  1146. if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
  1147. cancel_delayed_work_sync(&rdev->worker);
  1148. if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED,
  1149. &rdev->flags))
  1150. bnxt_re_cleanup_res(rdev);
  1151. if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags))
  1152. bnxt_re_free_res(rdev);
  1153. if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
  1154. rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
  1155. if (rc)
  1156. ibdev_warn(&rdev->ibdev,
  1157. "Failed to deinitialize RCFW: %#x", rc);
  1158. bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
  1159. bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
  1160. bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
  1161. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  1162. bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
  1163. bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
  1164. }
  1165. if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags)) {
  1166. rc = bnxt_re_free_msix(rdev);
  1167. if (rc)
  1168. ibdev_warn(&rdev->ibdev,
  1169. "Failed to free MSI-X vectors: %#x", rc);
  1170. }
  1171. bnxt_re_destroy_chip_ctx(rdev);
  1172. if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
  1173. rc = bnxt_re_unregister_netdev(rdev);
  1174. if (rc)
  1175. ibdev_warn(&rdev->ibdev,
  1176. "Failed to unregister with netdev: %#x", rc);
  1177. }
  1178. }
  1179. /* worker thread for polling periodic events. Now used for QoS programming*/
  1180. static void bnxt_re_worker(struct work_struct *work)
  1181. {
  1182. struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
  1183. worker.work);
  1184. bnxt_re_setup_qos(rdev);
  1185. schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
  1186. }
  1187. static int bnxt_re_dev_init(struct bnxt_re_dev *rdev, u8 wqe_mode)
  1188. {
  1189. struct bnxt_qplib_creq_ctx *creq;
  1190. struct bnxt_re_ring_attr rattr;
  1191. u32 db_offt;
  1192. int vid;
  1193. u8 type;
  1194. int rc;
  1195. /* Registered a new RoCE device instance to netdev */
  1196. memset(&rattr, 0, sizeof(rattr));
  1197. rc = bnxt_re_register_netdev(rdev);
  1198. if (rc) {
  1199. ibdev_err(&rdev->ibdev,
  1200. "Failed to register with netedev: %#x\n", rc);
  1201. return -EINVAL;
  1202. }
  1203. set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
  1204. rc = bnxt_re_setup_chip_ctx(rdev, wqe_mode);
  1205. if (rc) {
  1206. ibdev_err(&rdev->ibdev, "Failed to get chip context\n");
  1207. return -EINVAL;
  1208. }
  1209. /* Check whether VF or PF */
  1210. bnxt_re_get_sriov_func_type(rdev);
  1211. rc = bnxt_re_request_msix(rdev);
  1212. if (rc) {
  1213. ibdev_err(&rdev->ibdev,
  1214. "Failed to get MSI-X vectors: %#x\n", rc);
  1215. rc = -EINVAL;
  1216. goto fail;
  1217. }
  1218. set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
  1219. bnxt_re_query_hwrm_intf_version(rdev);
  1220. /* Establish RCFW Communication Channel to initialize the context
  1221. * memory for the function and all child VFs
  1222. */
  1223. rc = bnxt_qplib_alloc_rcfw_channel(&rdev->qplib_res, &rdev->rcfw,
  1224. &rdev->qplib_ctx,
  1225. BNXT_RE_MAX_QPC_COUNT);
  1226. if (rc) {
  1227. ibdev_err(&rdev->ibdev,
  1228. "Failed to allocate RCFW Channel: %#x\n", rc);
  1229. goto fail;
  1230. }
  1231. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  1232. creq = &rdev->rcfw.creq;
  1233. rattr.dma_arr = creq->hwq.pbl[PBL_LVL_0].pg_map_arr;
  1234. rattr.pages = creq->hwq.pbl[creq->hwq.level].pg_count;
  1235. rattr.type = type;
  1236. rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  1237. rattr.depth = BNXT_QPLIB_CREQE_MAX_CNT - 1;
  1238. rattr.lrid = rdev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
  1239. rc = bnxt_re_net_ring_alloc(rdev, &rattr, &creq->ring_id);
  1240. if (rc) {
  1241. ibdev_err(&rdev->ibdev, "Failed to allocate CREQ: %#x\n", rc);
  1242. goto free_rcfw;
  1243. }
  1244. db_offt = bnxt_re_get_nqdb_offset(rdev, BNXT_RE_AEQ_IDX);
  1245. vid = rdev->msix_entries[BNXT_RE_AEQ_IDX].vector;
  1246. rc = bnxt_qplib_enable_rcfw_channel(&rdev->rcfw,
  1247. vid, db_offt, rdev->is_virtfn,
  1248. &bnxt_re_aeq_handler);
  1249. if (rc) {
  1250. ibdev_err(&rdev->ibdev, "Failed to enable RCFW channel: %#x\n",
  1251. rc);
  1252. goto free_ring;
  1253. }
  1254. rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr,
  1255. rdev->is_virtfn);
  1256. if (rc)
  1257. goto disable_rcfw;
  1258. bnxt_re_set_resource_limits(rdev);
  1259. rc = bnxt_qplib_alloc_ctx(&rdev->qplib_res, &rdev->qplib_ctx, 0,
  1260. bnxt_qplib_is_chip_gen_p5(rdev->chip_ctx));
  1261. if (rc) {
  1262. ibdev_err(&rdev->ibdev,
  1263. "Failed to allocate QPLIB context: %#x\n", rc);
  1264. goto disable_rcfw;
  1265. }
  1266. rc = bnxt_re_net_stats_ctx_alloc(rdev,
  1267. rdev->qplib_ctx.stats.dma_map,
  1268. &rdev->qplib_ctx.stats.fw_id);
  1269. if (rc) {
  1270. ibdev_err(&rdev->ibdev,
  1271. "Failed to allocate stats context: %#x\n", rc);
  1272. goto free_ctx;
  1273. }
  1274. rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx,
  1275. rdev->is_virtfn);
  1276. if (rc) {
  1277. ibdev_err(&rdev->ibdev,
  1278. "Failed to initialize RCFW: %#x\n", rc);
  1279. goto free_sctx;
  1280. }
  1281. set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
  1282. /* Resources based on the 'new' device caps */
  1283. rc = bnxt_re_alloc_res(rdev);
  1284. if (rc) {
  1285. ibdev_err(&rdev->ibdev,
  1286. "Failed to allocate resources: %#x\n", rc);
  1287. goto fail;
  1288. }
  1289. set_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags);
  1290. rc = bnxt_re_init_res(rdev);
  1291. if (rc) {
  1292. ibdev_err(&rdev->ibdev,
  1293. "Failed to initialize resources: %#x\n", rc);
  1294. goto fail;
  1295. }
  1296. set_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED, &rdev->flags);
  1297. if (!rdev->is_virtfn) {
  1298. rc = bnxt_re_setup_qos(rdev);
  1299. if (rc)
  1300. ibdev_info(&rdev->ibdev,
  1301. "RoCE priority not yet configured\n");
  1302. INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
  1303. set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
  1304. schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
  1305. }
  1306. return 0;
  1307. free_sctx:
  1308. bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
  1309. free_ctx:
  1310. bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
  1311. disable_rcfw:
  1312. bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
  1313. free_ring:
  1314. type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
  1315. bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
  1316. free_rcfw:
  1317. bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
  1318. fail:
  1319. bnxt_re_dev_uninit(rdev);
  1320. return rc;
  1321. }
  1322. static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
  1323. {
  1324. struct net_device *netdev = rdev->netdev;
  1325. bnxt_re_dev_remove(rdev);
  1326. if (netdev)
  1327. dev_put(netdev);
  1328. }
  1329. static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct net_device *netdev)
  1330. {
  1331. struct bnxt_en_dev *en_dev;
  1332. int rc = 0;
  1333. if (!is_bnxt_re_dev(netdev))
  1334. return -ENODEV;
  1335. en_dev = bnxt_re_dev_probe(netdev);
  1336. if (IS_ERR(en_dev)) {
  1337. if (en_dev != ERR_PTR(-ENODEV))
  1338. ibdev_err(&(*rdev)->ibdev, "%s: Failed to probe\n",
  1339. ROCE_DRV_MODULE_NAME);
  1340. rc = PTR_ERR(en_dev);
  1341. goto exit;
  1342. }
  1343. *rdev = bnxt_re_dev_add(netdev, en_dev);
  1344. if (!*rdev) {
  1345. rc = -ENOMEM;
  1346. dev_put(netdev);
  1347. goto exit;
  1348. }
  1349. exit:
  1350. return rc;
  1351. }
  1352. static void bnxt_re_remove_device(struct bnxt_re_dev *rdev)
  1353. {
  1354. bnxt_re_dev_uninit(rdev);
  1355. pci_dev_put(rdev->en_dev->pdev);
  1356. bnxt_re_dev_unreg(rdev);
  1357. }
  1358. static int bnxt_re_add_device(struct bnxt_re_dev **rdev,
  1359. struct net_device *netdev, u8 wqe_mode)
  1360. {
  1361. int rc;
  1362. rc = bnxt_re_dev_reg(rdev, netdev);
  1363. if (rc == -ENODEV)
  1364. return rc;
  1365. if (rc) {
  1366. pr_err("Failed to register with the device %s: %#x\n",
  1367. netdev->name, rc);
  1368. return rc;
  1369. }
  1370. pci_dev_get((*rdev)->en_dev->pdev);
  1371. rc = bnxt_re_dev_init(*rdev, wqe_mode);
  1372. if (rc) {
  1373. pci_dev_put((*rdev)->en_dev->pdev);
  1374. bnxt_re_dev_unreg(*rdev);
  1375. }
  1376. return rc;
  1377. }
  1378. static void bnxt_re_dealloc_driver(struct ib_device *ib_dev)
  1379. {
  1380. struct bnxt_re_dev *rdev =
  1381. container_of(ib_dev, struct bnxt_re_dev, ibdev);
  1382. dev_info(rdev_to_dev(rdev), "Unregistering Device");
  1383. rtnl_lock();
  1384. bnxt_re_remove_device(rdev);
  1385. rtnl_unlock();
  1386. }
  1387. /* Handle all deferred netevents tasks */
  1388. static void bnxt_re_task(struct work_struct *work)
  1389. {
  1390. struct bnxt_re_work *re_work;
  1391. struct bnxt_re_dev *rdev;
  1392. int rc = 0;
  1393. re_work = container_of(work, struct bnxt_re_work, work);
  1394. rdev = re_work->rdev;
  1395. if (re_work->event == NETDEV_REGISTER) {
  1396. rc = bnxt_re_ib_init(rdev);
  1397. if (rc) {
  1398. ibdev_err(&rdev->ibdev,
  1399. "Failed to register with IB: %#x", rc);
  1400. rtnl_lock();
  1401. bnxt_re_remove_device(rdev);
  1402. rtnl_unlock();
  1403. goto exit;
  1404. }
  1405. goto exit;
  1406. }
  1407. if (!ib_device_try_get(&rdev->ibdev))
  1408. goto exit;
  1409. switch (re_work->event) {
  1410. case NETDEV_UP:
  1411. bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
  1412. IB_EVENT_PORT_ACTIVE);
  1413. break;
  1414. case NETDEV_DOWN:
  1415. bnxt_re_dev_stop(rdev);
  1416. break;
  1417. case NETDEV_CHANGE:
  1418. if (!netif_carrier_ok(rdev->netdev))
  1419. bnxt_re_dev_stop(rdev);
  1420. else if (netif_carrier_ok(rdev->netdev))
  1421. bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
  1422. IB_EVENT_PORT_ACTIVE);
  1423. ib_get_eth_speed(&rdev->ibdev, 1, &rdev->active_speed,
  1424. &rdev->active_width);
  1425. break;
  1426. default:
  1427. break;
  1428. }
  1429. ib_device_put(&rdev->ibdev);
  1430. exit:
  1431. put_device(&rdev->ibdev.dev);
  1432. kfree(re_work);
  1433. }
  1434. /*
  1435. * "Notifier chain callback can be invoked for the same chain from
  1436. * different CPUs at the same time".
  1437. *
  1438. * For cases when the netdev is already present, our call to the
  1439. * register_netdevice_notifier() will actually get the rtnl_lock()
  1440. * before sending NETDEV_REGISTER and (if up) NETDEV_UP
  1441. * events.
  1442. *
  1443. * But for cases when the netdev is not already present, the notifier
  1444. * chain is subjected to be invoked from different CPUs simultaneously.
  1445. *
  1446. * This is protected by the netdev_mutex.
  1447. */
  1448. static int bnxt_re_netdev_event(struct notifier_block *notifier,
  1449. unsigned long event, void *ptr)
  1450. {
  1451. struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
  1452. struct bnxt_re_work *re_work;
  1453. struct bnxt_re_dev *rdev;
  1454. int rc = 0;
  1455. bool sch_work = false;
  1456. bool release = true;
  1457. real_dev = rdma_vlan_dev_real_dev(netdev);
  1458. if (!real_dev)
  1459. real_dev = netdev;
  1460. rdev = bnxt_re_from_netdev(real_dev);
  1461. if (!rdev && event != NETDEV_REGISTER)
  1462. return NOTIFY_OK;
  1463. if (real_dev != netdev)
  1464. goto exit;
  1465. switch (event) {
  1466. case NETDEV_REGISTER:
  1467. if (rdev)
  1468. break;
  1469. rc = bnxt_re_add_device(&rdev, real_dev,
  1470. BNXT_QPLIB_WQE_MODE_STATIC);
  1471. if (!rc)
  1472. sch_work = true;
  1473. release = false;
  1474. break;
  1475. case NETDEV_UNREGISTER:
  1476. ib_unregister_device_queued(&rdev->ibdev);
  1477. break;
  1478. default:
  1479. sch_work = true;
  1480. break;
  1481. }
  1482. if (sch_work) {
  1483. /* Allocate for the deferred task */
  1484. re_work = kzalloc(sizeof(*re_work), GFP_KERNEL);
  1485. if (re_work) {
  1486. get_device(&rdev->ibdev.dev);
  1487. re_work->rdev = rdev;
  1488. re_work->event = event;
  1489. re_work->vlan_dev = (real_dev == netdev ?
  1490. NULL : netdev);
  1491. INIT_WORK(&re_work->work, bnxt_re_task);
  1492. queue_work(bnxt_re_wq, &re_work->work);
  1493. }
  1494. }
  1495. exit:
  1496. if (rdev && release)
  1497. ib_device_put(&rdev->ibdev);
  1498. return NOTIFY_DONE;
  1499. }
  1500. static struct notifier_block bnxt_re_netdev_notifier = {
  1501. .notifier_call = bnxt_re_netdev_event
  1502. };
  1503. static int __init bnxt_re_mod_init(void)
  1504. {
  1505. int rc = 0;
  1506. pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
  1507. bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
  1508. if (!bnxt_re_wq)
  1509. return -ENOMEM;
  1510. INIT_LIST_HEAD(&bnxt_re_dev_list);
  1511. rc = register_netdevice_notifier(&bnxt_re_netdev_notifier);
  1512. if (rc) {
  1513. pr_err("%s: Cannot register to netdevice_notifier",
  1514. ROCE_DRV_MODULE_NAME);
  1515. goto err_netdev;
  1516. }
  1517. return 0;
  1518. err_netdev:
  1519. destroy_workqueue(bnxt_re_wq);
  1520. return rc;
  1521. }
  1522. static void __exit bnxt_re_mod_exit(void)
  1523. {
  1524. struct bnxt_re_dev *rdev;
  1525. unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
  1526. if (bnxt_re_wq)
  1527. destroy_workqueue(bnxt_re_wq);
  1528. list_for_each_entry(rdev, &bnxt_re_dev_list, list) {
  1529. /* VF device removal should be called before the removal
  1530. * of PF device. Queue VFs unregister first, so that VFs
  1531. * shall be removed before the PF during the call of
  1532. * ib_unregister_driver.
  1533. */
  1534. if (rdev->is_virtfn)
  1535. ib_unregister_device(&rdev->ibdev);
  1536. }
  1537. ib_unregister_driver(RDMA_DRIVER_BNXT_RE);
  1538. }
  1539. module_init(bnxt_re_mod_init);
  1540. module_exit(bnxt_re_mod_exit);