main.c 43 KB

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