netdev_rx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * Copyright(c) 2020 Intel Corporation.
  4. *
  5. */
  6. /*
  7. * This file contains HFI1 support for netdev RX functionality
  8. */
  9. #include "sdma.h"
  10. #include "verbs.h"
  11. #include "netdev.h"
  12. #include "hfi.h"
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <rdma/ib_verbs.h>
  16. static int hfi1_netdev_setup_ctxt(struct hfi1_netdev_rx *rx,
  17. struct hfi1_ctxtdata *uctxt)
  18. {
  19. unsigned int rcvctrl_ops;
  20. struct hfi1_devdata *dd = rx->dd;
  21. int ret;
  22. uctxt->rhf_rcv_function_map = netdev_rhf_rcv_functions;
  23. uctxt->do_interrupt = &handle_receive_interrupt_napi_sp;
  24. /* Now allocate the RcvHdr queue and eager buffers. */
  25. ret = hfi1_create_rcvhdrq(dd, uctxt);
  26. if (ret)
  27. goto done;
  28. ret = hfi1_setup_eagerbufs(uctxt);
  29. if (ret)
  30. goto done;
  31. clear_rcvhdrtail(uctxt);
  32. rcvctrl_ops = HFI1_RCVCTRL_CTXT_DIS;
  33. rcvctrl_ops |= HFI1_RCVCTRL_INTRAVAIL_DIS;
  34. if (!HFI1_CAP_KGET_MASK(uctxt->flags, MULTI_PKT_EGR))
  35. rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
  36. if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_EGR_FULL))
  37. rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
  38. if (HFI1_CAP_KGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
  39. rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
  40. if (HFI1_CAP_KGET_MASK(uctxt->flags, DMA_RTAIL))
  41. rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
  42. hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
  43. done:
  44. return ret;
  45. }
  46. static int hfi1_netdev_allocate_ctxt(struct hfi1_devdata *dd,
  47. struct hfi1_ctxtdata **ctxt)
  48. {
  49. struct hfi1_ctxtdata *uctxt;
  50. int ret;
  51. if (dd->flags & HFI1_FROZEN)
  52. return -EIO;
  53. ret = hfi1_create_ctxtdata(dd->pport, dd->node, &uctxt);
  54. if (ret < 0) {
  55. dd_dev_err(dd, "Unable to create ctxtdata, failing open\n");
  56. return -ENOMEM;
  57. }
  58. uctxt->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
  59. HFI1_CAP_KGET(NODROP_RHQ_FULL) |
  60. HFI1_CAP_KGET(NODROP_EGR_FULL) |
  61. HFI1_CAP_KGET(DMA_RTAIL);
  62. /* Netdev contexts are always NO_RDMA_RTAIL */
  63. uctxt->fast_handler = handle_receive_interrupt_napi_fp;
  64. uctxt->slow_handler = handle_receive_interrupt_napi_sp;
  65. hfi1_set_seq_cnt(uctxt, 1);
  66. uctxt->is_vnic = true;
  67. hfi1_stats.sps_ctxts++;
  68. dd_dev_info(dd, "created netdev context %d\n", uctxt->ctxt);
  69. *ctxt = uctxt;
  70. return 0;
  71. }
  72. static void hfi1_netdev_deallocate_ctxt(struct hfi1_devdata *dd,
  73. struct hfi1_ctxtdata *uctxt)
  74. {
  75. flush_wc();
  76. /*
  77. * Disable receive context and interrupt available, reset all
  78. * RcvCtxtCtrl bits to default values.
  79. */
  80. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
  81. HFI1_RCVCTRL_TIDFLOW_DIS |
  82. HFI1_RCVCTRL_INTRAVAIL_DIS |
  83. HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
  84. HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
  85. HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
  86. if (uctxt->msix_intr != CCE_NUM_MSIX_VECTORS)
  87. msix_free_irq(dd, uctxt->msix_intr);
  88. uctxt->msix_intr = CCE_NUM_MSIX_VECTORS;
  89. uctxt->event_flags = 0;
  90. hfi1_clear_tids(uctxt);
  91. hfi1_clear_ctxt_pkey(dd, uctxt);
  92. hfi1_stats.sps_ctxts--;
  93. hfi1_free_ctxt(uctxt);
  94. }
  95. static int hfi1_netdev_allot_ctxt(struct hfi1_netdev_rx *rx,
  96. struct hfi1_ctxtdata **ctxt)
  97. {
  98. int rc;
  99. struct hfi1_devdata *dd = rx->dd;
  100. rc = hfi1_netdev_allocate_ctxt(dd, ctxt);
  101. if (rc) {
  102. dd_dev_err(dd, "netdev ctxt alloc failed %d\n", rc);
  103. return rc;
  104. }
  105. rc = hfi1_netdev_setup_ctxt(rx, *ctxt);
  106. if (rc) {
  107. dd_dev_err(dd, "netdev ctxt setup failed %d\n", rc);
  108. hfi1_netdev_deallocate_ctxt(dd, *ctxt);
  109. *ctxt = NULL;
  110. }
  111. return rc;
  112. }
  113. /**
  114. * hfi1_num_netdev_contexts - Count of netdev recv contexts to use.
  115. * @dd: device on which to allocate netdev contexts
  116. * @available_contexts: count of available receive contexts
  117. * @cpu_mask: mask of possible cpus to include for contexts
  118. *
  119. * Return: count of physical cores on a node or the remaining available recv
  120. * contexts for netdev recv context usage up to the maximum of
  121. * HFI1_MAX_NETDEV_CTXTS.
  122. * A value of 0 can be returned when acceleration is explicitly turned off,
  123. * a memory allocation error occurs or when there are no available contexts.
  124. *
  125. */
  126. u32 hfi1_num_netdev_contexts(struct hfi1_devdata *dd, u32 available_contexts,
  127. struct cpumask *cpu_mask)
  128. {
  129. cpumask_var_t node_cpu_mask;
  130. unsigned int available_cpus;
  131. if (!HFI1_CAP_IS_KSET(AIP))
  132. return 0;
  133. /* Always give user contexts priority over netdev contexts */
  134. if (available_contexts == 0) {
  135. dd_dev_info(dd, "No receive contexts available for netdevs.\n");
  136. return 0;
  137. }
  138. if (!zalloc_cpumask_var(&node_cpu_mask, GFP_KERNEL)) {
  139. dd_dev_err(dd, "Unable to allocate cpu_mask for netdevs.\n");
  140. return 0;
  141. }
  142. cpumask_and(node_cpu_mask, cpu_mask, cpumask_of_node(dd->node));
  143. available_cpus = cpumask_weight(node_cpu_mask);
  144. free_cpumask_var(node_cpu_mask);
  145. return min3(available_cpus, available_contexts,
  146. (u32)HFI1_MAX_NETDEV_CTXTS);
  147. }
  148. static int hfi1_netdev_rxq_init(struct hfi1_netdev_rx *rx)
  149. {
  150. int i;
  151. int rc;
  152. struct hfi1_devdata *dd = rx->dd;
  153. struct net_device *dev = &rx->rx_napi;
  154. rx->num_rx_q = dd->num_netdev_contexts;
  155. rx->rxq = kcalloc_node(rx->num_rx_q, sizeof(*rx->rxq),
  156. GFP_KERNEL, dd->node);
  157. if (!rx->rxq) {
  158. dd_dev_err(dd, "Unable to allocate netdev queue data\n");
  159. return (-ENOMEM);
  160. }
  161. for (i = 0; i < rx->num_rx_q; i++) {
  162. struct hfi1_netdev_rxq *rxq = &rx->rxq[i];
  163. rc = hfi1_netdev_allot_ctxt(rx, &rxq->rcd);
  164. if (rc)
  165. goto bail_context_irq_failure;
  166. hfi1_rcd_get(rxq->rcd);
  167. rxq->rx = rx;
  168. rxq->rcd->napi = &rxq->napi;
  169. dd_dev_info(dd, "Setting rcv queue %d napi to context %d\n",
  170. i, rxq->rcd->ctxt);
  171. /*
  172. * Disable BUSY_POLL on this NAPI as this is not supported
  173. * right now.
  174. */
  175. set_bit(NAPI_STATE_NO_BUSY_POLL, &rxq->napi.state);
  176. netif_napi_add_weight(dev, &rxq->napi, hfi1_netdev_rx_napi, 64);
  177. rc = msix_netdev_request_rcd_irq(rxq->rcd);
  178. if (rc)
  179. goto bail_context_irq_failure;
  180. }
  181. return 0;
  182. bail_context_irq_failure:
  183. dd_dev_err(dd, "Unable to allot receive context\n");
  184. for (; i >= 0; i--) {
  185. struct hfi1_netdev_rxq *rxq = &rx->rxq[i];
  186. if (rxq->rcd) {
  187. hfi1_netdev_deallocate_ctxt(dd, rxq->rcd);
  188. hfi1_rcd_put(rxq->rcd);
  189. rxq->rcd = NULL;
  190. }
  191. }
  192. kfree(rx->rxq);
  193. rx->rxq = NULL;
  194. return rc;
  195. }
  196. static void hfi1_netdev_rxq_deinit(struct hfi1_netdev_rx *rx)
  197. {
  198. int i;
  199. struct hfi1_devdata *dd = rx->dd;
  200. for (i = 0; i < rx->num_rx_q; i++) {
  201. struct hfi1_netdev_rxq *rxq = &rx->rxq[i];
  202. netif_napi_del(&rxq->napi);
  203. hfi1_netdev_deallocate_ctxt(dd, rxq->rcd);
  204. hfi1_rcd_put(rxq->rcd);
  205. rxq->rcd = NULL;
  206. }
  207. kfree(rx->rxq);
  208. rx->rxq = NULL;
  209. rx->num_rx_q = 0;
  210. }
  211. static void enable_queues(struct hfi1_netdev_rx *rx)
  212. {
  213. int i;
  214. for (i = 0; i < rx->num_rx_q; i++) {
  215. struct hfi1_netdev_rxq *rxq = &rx->rxq[i];
  216. dd_dev_info(rx->dd, "enabling queue %d on context %d\n", i,
  217. rxq->rcd->ctxt);
  218. napi_enable(&rxq->napi);
  219. hfi1_rcvctrl(rx->dd,
  220. HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB,
  221. rxq->rcd);
  222. }
  223. }
  224. static void disable_queues(struct hfi1_netdev_rx *rx)
  225. {
  226. int i;
  227. msix_netdev_synchronize_irq(rx->dd);
  228. for (i = 0; i < rx->num_rx_q; i++) {
  229. struct hfi1_netdev_rxq *rxq = &rx->rxq[i];
  230. dd_dev_info(rx->dd, "disabling queue %d on context %d\n", i,
  231. rxq->rcd->ctxt);
  232. /* wait for napi if it was scheduled */
  233. hfi1_rcvctrl(rx->dd,
  234. HFI1_RCVCTRL_CTXT_DIS | HFI1_RCVCTRL_INTRAVAIL_DIS,
  235. rxq->rcd);
  236. napi_synchronize(&rxq->napi);
  237. napi_disable(&rxq->napi);
  238. }
  239. }
  240. /**
  241. * hfi1_netdev_rx_init - Incrememnts netdevs counter. When called first time,
  242. * it allocates receive queue data and calls netif_napi_add
  243. * for each queue.
  244. *
  245. * @dd: hfi1 dev data
  246. */
  247. int hfi1_netdev_rx_init(struct hfi1_devdata *dd)
  248. {
  249. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  250. int res;
  251. if (atomic_fetch_inc(&rx->netdevs))
  252. return 0;
  253. mutex_lock(&hfi1_mutex);
  254. res = hfi1_netdev_rxq_init(rx);
  255. mutex_unlock(&hfi1_mutex);
  256. return res;
  257. }
  258. /**
  259. * hfi1_netdev_rx_destroy - Decrements netdevs counter, when it reaches 0
  260. * napi is deleted and receive queses memory is freed.
  261. *
  262. * @dd: hfi1 dev data
  263. */
  264. int hfi1_netdev_rx_destroy(struct hfi1_devdata *dd)
  265. {
  266. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  267. /* destroy the RX queues only if it is the last netdev going away */
  268. if (atomic_fetch_add_unless(&rx->netdevs, -1, 0) == 1) {
  269. mutex_lock(&hfi1_mutex);
  270. hfi1_netdev_rxq_deinit(rx);
  271. mutex_unlock(&hfi1_mutex);
  272. }
  273. return 0;
  274. }
  275. /**
  276. * hfi1_alloc_rx - Allocates the rx support structure
  277. * @dd: hfi1 dev data
  278. *
  279. * Allocate the rx structure to support gathering the receive
  280. * resources and the dummy netdev.
  281. *
  282. * Updates dd struct pointer upon success.
  283. *
  284. * Return: 0 (success) -error on failure
  285. *
  286. */
  287. int hfi1_alloc_rx(struct hfi1_devdata *dd)
  288. {
  289. struct hfi1_netdev_rx *rx;
  290. dd_dev_info(dd, "allocating rx size %ld\n", sizeof(*rx));
  291. rx = kzalloc_node(sizeof(*rx), GFP_KERNEL, dd->node);
  292. if (!rx)
  293. return -ENOMEM;
  294. rx->dd = dd;
  295. init_dummy_netdev(&rx->rx_napi);
  296. xa_init(&rx->dev_tbl);
  297. atomic_set(&rx->enabled, 0);
  298. atomic_set(&rx->netdevs, 0);
  299. dd->netdev_rx = rx;
  300. return 0;
  301. }
  302. void hfi1_free_rx(struct hfi1_devdata *dd)
  303. {
  304. if (dd->netdev_rx) {
  305. dd_dev_info(dd, "hfi1 rx freed\n");
  306. kfree(dd->netdev_rx);
  307. dd->netdev_rx = NULL;
  308. }
  309. }
  310. /**
  311. * hfi1_netdev_enable_queues - This is napi enable function.
  312. * It enables napi objects associated with queues.
  313. * When at least one device has called it it increments atomic counter.
  314. * Disable function decrements counter and when it is 0,
  315. * calls napi_disable for every queue.
  316. *
  317. * @dd: hfi1 dev data
  318. */
  319. void hfi1_netdev_enable_queues(struct hfi1_devdata *dd)
  320. {
  321. struct hfi1_netdev_rx *rx;
  322. if (!dd->netdev_rx)
  323. return;
  324. rx = dd->netdev_rx;
  325. if (atomic_fetch_inc(&rx->enabled))
  326. return;
  327. mutex_lock(&hfi1_mutex);
  328. enable_queues(rx);
  329. mutex_unlock(&hfi1_mutex);
  330. }
  331. void hfi1_netdev_disable_queues(struct hfi1_devdata *dd)
  332. {
  333. struct hfi1_netdev_rx *rx;
  334. if (!dd->netdev_rx)
  335. return;
  336. rx = dd->netdev_rx;
  337. if (atomic_dec_if_positive(&rx->enabled))
  338. return;
  339. mutex_lock(&hfi1_mutex);
  340. disable_queues(rx);
  341. mutex_unlock(&hfi1_mutex);
  342. }
  343. /**
  344. * hfi1_netdev_add_data - Registers data with unique identifier
  345. * to be requested later this is needed for VNIC and IPoIB VLANs
  346. * implementations.
  347. * This call is protected by mutex idr_lock.
  348. *
  349. * @dd: hfi1 dev data
  350. * @id: requested integer id up to INT_MAX
  351. * @data: data to be associated with index
  352. */
  353. int hfi1_netdev_add_data(struct hfi1_devdata *dd, int id, void *data)
  354. {
  355. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  356. return xa_insert(&rx->dev_tbl, id, data, GFP_NOWAIT);
  357. }
  358. /**
  359. * hfi1_netdev_remove_data - Removes data with previously given id.
  360. * Returns the reference to removed entry.
  361. *
  362. * @dd: hfi1 dev data
  363. * @id: requested integer id up to INT_MAX
  364. */
  365. void *hfi1_netdev_remove_data(struct hfi1_devdata *dd, int id)
  366. {
  367. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  368. return xa_erase(&rx->dev_tbl, id);
  369. }
  370. /**
  371. * hfi1_netdev_get_data - Gets data with given id
  372. *
  373. * @dd: hfi1 dev data
  374. * @id: requested integer id up to INT_MAX
  375. */
  376. void *hfi1_netdev_get_data(struct hfi1_devdata *dd, int id)
  377. {
  378. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  379. return xa_load(&rx->dev_tbl, id);
  380. }
  381. /**
  382. * hfi1_netdev_get_first_data - Gets first entry with greater or equal id.
  383. *
  384. * @dd: hfi1 dev data
  385. * @start_id: requested integer id up to INT_MAX
  386. */
  387. void *hfi1_netdev_get_first_data(struct hfi1_devdata *dd, int *start_id)
  388. {
  389. struct hfi1_netdev_rx *rx = dd->netdev_rx;
  390. unsigned long index = *start_id;
  391. void *ret;
  392. ret = xa_find(&rx->dev_tbl, &index, UINT_MAX, XA_PRESENT);
  393. *start_id = (int)index;
  394. return ret;
  395. }