en_rx.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <net/busy_poll.h>
  34. #include <linux/bpf.h>
  35. #include <linux/bpf_trace.h>
  36. #include <linux/mlx4/cq.h>
  37. #include <linux/slab.h>
  38. #include <linux/mlx4/qp.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/rculist.h>
  41. #include <linux/if_ether.h>
  42. #include <linux/if_vlan.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/irq.h>
  45. #if IS_ENABLED(CONFIG_IPV6)
  46. #include <net/ip6_checksum.h>
  47. #endif
  48. #include "mlx4_en.h"
  49. static int mlx4_alloc_page(struct mlx4_en_priv *priv,
  50. struct mlx4_en_rx_alloc *frag,
  51. gfp_t gfp)
  52. {
  53. struct page *page;
  54. dma_addr_t dma;
  55. page = alloc_page(gfp);
  56. if (unlikely(!page))
  57. return -ENOMEM;
  58. dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE, priv->dma_dir);
  59. if (unlikely(dma_mapping_error(priv->ddev, dma))) {
  60. __free_page(page);
  61. return -ENOMEM;
  62. }
  63. frag->page = page;
  64. frag->dma = dma;
  65. frag->page_offset = priv->rx_headroom;
  66. return 0;
  67. }
  68. static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
  69. struct mlx4_en_rx_ring *ring,
  70. struct mlx4_en_rx_desc *rx_desc,
  71. struct mlx4_en_rx_alloc *frags,
  72. gfp_t gfp)
  73. {
  74. int i;
  75. for (i = 0; i < priv->num_frags; i++, frags++) {
  76. if (!frags->page) {
  77. if (mlx4_alloc_page(priv, frags, gfp))
  78. return -ENOMEM;
  79. ring->rx_alloc_pages++;
  80. }
  81. rx_desc->data[i].addr = cpu_to_be64(frags->dma +
  82. frags->page_offset);
  83. }
  84. return 0;
  85. }
  86. static void mlx4_en_free_frag(const struct mlx4_en_priv *priv,
  87. struct mlx4_en_rx_alloc *frag)
  88. {
  89. if (frag->page) {
  90. dma_unmap_page(priv->ddev, frag->dma,
  91. PAGE_SIZE, priv->dma_dir);
  92. __free_page(frag->page);
  93. }
  94. /* We need to clear all fields, otherwise a change of priv->log_rx_info
  95. * could lead to see garbage later in frag->page.
  96. */
  97. memset(frag, 0, sizeof(*frag));
  98. }
  99. static void mlx4_en_init_rx_desc(const struct mlx4_en_priv *priv,
  100. struct mlx4_en_rx_ring *ring, int index)
  101. {
  102. struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
  103. int possible_frags;
  104. int i;
  105. /* Set size and memtype fields */
  106. for (i = 0; i < priv->num_frags; i++) {
  107. rx_desc->data[i].byte_count =
  108. cpu_to_be32(priv->frag_info[i].frag_size);
  109. rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
  110. }
  111. /* If the number of used fragments does not fill up the ring stride,
  112. * remaining (unused) fragments must be padded with null address/size
  113. * and a special memory key */
  114. possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
  115. for (i = priv->num_frags; i < possible_frags; i++) {
  116. rx_desc->data[i].byte_count = 0;
  117. rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
  118. rx_desc->data[i].addr = 0;
  119. }
  120. }
  121. static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
  122. struct mlx4_en_rx_ring *ring, int index,
  123. gfp_t gfp)
  124. {
  125. struct mlx4_en_rx_desc *rx_desc = ring->buf +
  126. (index << ring->log_stride);
  127. struct mlx4_en_rx_alloc *frags = ring->rx_info +
  128. (index << priv->log_rx_info);
  129. if (likely(ring->page_cache.index > 0)) {
  130. /* XDP uses a single page per frame */
  131. if (!frags->page) {
  132. ring->page_cache.index--;
  133. frags->page = ring->page_cache.buf[ring->page_cache.index].page;
  134. frags->dma = ring->page_cache.buf[ring->page_cache.index].dma;
  135. }
  136. frags->page_offset = XDP_PACKET_HEADROOM;
  137. rx_desc->data[0].addr = cpu_to_be64(frags->dma +
  138. XDP_PACKET_HEADROOM);
  139. return 0;
  140. }
  141. return mlx4_en_alloc_frags(priv, ring, rx_desc, frags, gfp);
  142. }
  143. static bool mlx4_en_is_ring_empty(const struct mlx4_en_rx_ring *ring)
  144. {
  145. return ring->prod == ring->cons;
  146. }
  147. static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
  148. {
  149. *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
  150. }
  151. /* slow path */
  152. static void mlx4_en_free_rx_desc(const struct mlx4_en_priv *priv,
  153. struct mlx4_en_rx_ring *ring,
  154. int index)
  155. {
  156. struct mlx4_en_rx_alloc *frags;
  157. int nr;
  158. frags = ring->rx_info + (index << priv->log_rx_info);
  159. for (nr = 0; nr < priv->num_frags; nr++) {
  160. en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
  161. mlx4_en_free_frag(priv, frags + nr);
  162. }
  163. }
  164. /* Function not in fast-path */
  165. static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
  166. {
  167. struct mlx4_en_rx_ring *ring;
  168. int ring_ind;
  169. int buf_ind;
  170. int new_size;
  171. for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
  172. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  173. ring = priv->rx_ring[ring_ind];
  174. if (mlx4_en_prepare_rx_desc(priv, ring,
  175. ring->actual_size,
  176. GFP_KERNEL)) {
  177. if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
  178. en_err(priv, "Failed to allocate enough rx buffers\n");
  179. return -ENOMEM;
  180. } else {
  181. new_size = rounddown_pow_of_two(ring->actual_size);
  182. en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
  183. ring->actual_size, new_size);
  184. goto reduce_rings;
  185. }
  186. }
  187. ring->actual_size++;
  188. ring->prod++;
  189. }
  190. }
  191. return 0;
  192. reduce_rings:
  193. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  194. ring = priv->rx_ring[ring_ind];
  195. while (ring->actual_size > new_size) {
  196. ring->actual_size--;
  197. ring->prod--;
  198. mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
  199. }
  200. }
  201. return 0;
  202. }
  203. static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
  204. struct mlx4_en_rx_ring *ring)
  205. {
  206. int index;
  207. en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
  208. ring->cons, ring->prod);
  209. /* Unmap and free Rx buffers */
  210. for (index = 0; index < ring->size; index++) {
  211. en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
  212. mlx4_en_free_rx_desc(priv, ring, index);
  213. }
  214. ring->cons = 0;
  215. ring->prod = 0;
  216. }
  217. void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
  218. {
  219. int i;
  220. int num_of_eqs;
  221. int num_rx_rings;
  222. struct mlx4_dev *dev = mdev->dev;
  223. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
  224. num_of_eqs = max_t(int, MIN_RX_RINGS,
  225. min_t(int,
  226. mlx4_get_eqs_per_port(mdev->dev, i),
  227. DEF_RX_RINGS));
  228. num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
  229. min_t(int, num_of_eqs, num_online_cpus());
  230. mdev->profile.prof[i].rx_ring_num =
  231. rounddown_pow_of_two(num_rx_rings);
  232. }
  233. }
  234. int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
  235. struct mlx4_en_rx_ring **pring,
  236. u32 size, u16 stride, int node, int queue_index)
  237. {
  238. struct mlx4_en_dev *mdev = priv->mdev;
  239. struct mlx4_en_rx_ring *ring;
  240. int err = -ENOMEM;
  241. int tmp;
  242. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  243. if (!ring) {
  244. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  245. if (!ring) {
  246. en_err(priv, "Failed to allocate RX ring structure\n");
  247. return -ENOMEM;
  248. }
  249. }
  250. ring->prod = 0;
  251. ring->cons = 0;
  252. ring->size = size;
  253. ring->size_mask = size - 1;
  254. ring->stride = stride;
  255. ring->log_stride = ffs(ring->stride) - 1;
  256. ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
  257. if (xdp_rxq_info_reg(&ring->xdp_rxq, priv->dev, queue_index) < 0)
  258. goto err_ring;
  259. tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
  260. sizeof(struct mlx4_en_rx_alloc));
  261. ring->rx_info = kvzalloc_node(tmp, GFP_KERNEL, node);
  262. if (!ring->rx_info) {
  263. err = -ENOMEM;
  264. goto err_xdp_info;
  265. }
  266. en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
  267. ring->rx_info, tmp);
  268. /* Allocate HW buffers on provided NUMA node */
  269. set_dev_node(&mdev->dev->persist->pdev->dev, node);
  270. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  271. set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
  272. if (err)
  273. goto err_info;
  274. ring->buf = ring->wqres.buf.direct.buf;
  275. ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
  276. *pring = ring;
  277. return 0;
  278. err_info:
  279. kvfree(ring->rx_info);
  280. ring->rx_info = NULL;
  281. err_xdp_info:
  282. xdp_rxq_info_unreg(&ring->xdp_rxq);
  283. err_ring:
  284. kfree(ring);
  285. *pring = NULL;
  286. return err;
  287. }
  288. int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
  289. {
  290. struct mlx4_en_rx_ring *ring;
  291. int i;
  292. int ring_ind;
  293. int err;
  294. int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  295. DS_SIZE * priv->num_frags);
  296. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  297. ring = priv->rx_ring[ring_ind];
  298. ring->prod = 0;
  299. ring->cons = 0;
  300. ring->actual_size = 0;
  301. ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
  302. ring->stride = stride;
  303. if (ring->stride <= TXBB_SIZE) {
  304. /* Stamp first unused send wqe */
  305. __be32 *ptr = (__be32 *)ring->buf;
  306. __be32 stamp = cpu_to_be32(1 << STAMP_SHIFT);
  307. *ptr = stamp;
  308. /* Move pointer to start of rx section */
  309. ring->buf += TXBB_SIZE;
  310. }
  311. ring->log_stride = ffs(ring->stride) - 1;
  312. ring->buf_size = ring->size * ring->stride;
  313. memset(ring->buf, 0, ring->buf_size);
  314. mlx4_en_update_rx_prod_db(ring);
  315. /* Initialize all descriptors */
  316. for (i = 0; i < ring->size; i++)
  317. mlx4_en_init_rx_desc(priv, ring, i);
  318. }
  319. err = mlx4_en_fill_rx_buffers(priv);
  320. if (err)
  321. goto err_buffers;
  322. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  323. ring = priv->rx_ring[ring_ind];
  324. ring->size_mask = ring->actual_size - 1;
  325. mlx4_en_update_rx_prod_db(ring);
  326. }
  327. return 0;
  328. err_buffers:
  329. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
  330. mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
  331. ring_ind = priv->rx_ring_num - 1;
  332. while (ring_ind >= 0) {
  333. if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
  334. priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
  335. ring_ind--;
  336. }
  337. return err;
  338. }
  339. /* We recover from out of memory by scheduling our napi poll
  340. * function (mlx4_en_process_cq), which tries to allocate
  341. * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
  342. */
  343. void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
  344. {
  345. int ring;
  346. if (!priv->port_up)
  347. return;
  348. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  349. if (mlx4_en_is_ring_empty(priv->rx_ring[ring])) {
  350. local_bh_disable();
  351. napi_reschedule(&priv->rx_cq[ring]->napi);
  352. local_bh_enable();
  353. }
  354. }
  355. }
  356. /* When the rx ring is running in page-per-packet mode, a released frame can go
  357. * directly into a small cache, to avoid unmapping or touching the page
  358. * allocator. In bpf prog performance scenarios, buffers are either forwarded
  359. * or dropped, never converted to skbs, so every page can come directly from
  360. * this cache when it is sized to be a multiple of the napi budget.
  361. */
  362. bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
  363. struct mlx4_en_rx_alloc *frame)
  364. {
  365. struct mlx4_en_page_cache *cache = &ring->page_cache;
  366. if (cache->index >= MLX4_EN_CACHE_SIZE)
  367. return false;
  368. cache->buf[cache->index].page = frame->page;
  369. cache->buf[cache->index].dma = frame->dma;
  370. cache->index++;
  371. return true;
  372. }
  373. void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
  374. struct mlx4_en_rx_ring **pring,
  375. u32 size, u16 stride)
  376. {
  377. struct mlx4_en_dev *mdev = priv->mdev;
  378. struct mlx4_en_rx_ring *ring = *pring;
  379. struct bpf_prog *old_prog;
  380. old_prog = rcu_dereference_protected(
  381. ring->xdp_prog,
  382. lockdep_is_held(&mdev->state_lock));
  383. if (old_prog)
  384. bpf_prog_put(old_prog);
  385. xdp_rxq_info_unreg(&ring->xdp_rxq);
  386. mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
  387. kvfree(ring->rx_info);
  388. ring->rx_info = NULL;
  389. kfree(ring);
  390. *pring = NULL;
  391. }
  392. void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
  393. struct mlx4_en_rx_ring *ring)
  394. {
  395. int i;
  396. for (i = 0; i < ring->page_cache.index; i++) {
  397. dma_unmap_page(priv->ddev, ring->page_cache.buf[i].dma,
  398. PAGE_SIZE, priv->dma_dir);
  399. put_page(ring->page_cache.buf[i].page);
  400. }
  401. ring->page_cache.index = 0;
  402. mlx4_en_free_rx_buf(priv, ring);
  403. if (ring->stride <= TXBB_SIZE)
  404. ring->buf -= TXBB_SIZE;
  405. }
  406. static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
  407. struct mlx4_en_rx_alloc *frags,
  408. struct sk_buff *skb,
  409. int length)
  410. {
  411. const struct mlx4_en_frag_info *frag_info = priv->frag_info;
  412. unsigned int truesize = 0;
  413. bool release = true;
  414. int nr, frag_size;
  415. struct page *page;
  416. dma_addr_t dma;
  417. /* Collect used fragments while replacing them in the HW descriptors */
  418. for (nr = 0;; frags++) {
  419. frag_size = min_t(int, length, frag_info->frag_size);
  420. page = frags->page;
  421. if (unlikely(!page))
  422. goto fail;
  423. dma = frags->dma;
  424. dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
  425. frag_size, priv->dma_dir);
  426. __skb_fill_page_desc(skb, nr, page, frags->page_offset,
  427. frag_size);
  428. truesize += frag_info->frag_stride;
  429. if (frag_info->frag_stride == PAGE_SIZE / 2) {
  430. frags->page_offset ^= PAGE_SIZE / 2;
  431. release = page_count(page) != 1 ||
  432. page_is_pfmemalloc(page) ||
  433. page_to_nid(page) != numa_mem_id();
  434. } else if (!priv->rx_headroom) {
  435. /* rx_headroom for non XDP setup is always 0.
  436. * When XDP is set, the above condition will
  437. * guarantee page is always released.
  438. */
  439. u32 sz_align = ALIGN(frag_size, SMP_CACHE_BYTES);
  440. frags->page_offset += sz_align;
  441. release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
  442. }
  443. if (release) {
  444. dma_unmap_page(priv->ddev, dma, PAGE_SIZE, priv->dma_dir);
  445. frags->page = NULL;
  446. } else {
  447. page_ref_inc(page);
  448. }
  449. nr++;
  450. length -= frag_size;
  451. if (!length)
  452. break;
  453. frag_info++;
  454. }
  455. skb->truesize += truesize;
  456. return nr;
  457. fail:
  458. while (nr > 0) {
  459. nr--;
  460. __skb_frag_unref(skb_shinfo(skb)->frags + nr);
  461. }
  462. return 0;
  463. }
  464. static void validate_loopback(struct mlx4_en_priv *priv, void *va)
  465. {
  466. const unsigned char *data = va + ETH_HLEN;
  467. int i;
  468. for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++) {
  469. if (data[i] != (unsigned char)i)
  470. return;
  471. }
  472. /* Loopback found */
  473. priv->loopback_ok = 1;
  474. }
  475. static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
  476. struct mlx4_en_rx_ring *ring)
  477. {
  478. u32 missing = ring->actual_size - (ring->prod - ring->cons);
  479. /* Try to batch allocations, but not too much. */
  480. if (missing < 8)
  481. return;
  482. do {
  483. if (mlx4_en_prepare_rx_desc(priv, ring,
  484. ring->prod & ring->size_mask,
  485. GFP_ATOMIC | __GFP_MEMALLOC))
  486. break;
  487. ring->prod++;
  488. } while (likely(--missing));
  489. mlx4_en_update_rx_prod_db(ring);
  490. }
  491. /* When hardware doesn't strip the vlan, we need to calculate the checksum
  492. * over it and add it to the hardware's checksum calculation
  493. */
  494. static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
  495. struct vlan_hdr *vlanh)
  496. {
  497. return csum_add(hw_checksum, *(__wsum *)vlanh);
  498. }
  499. /* Although the stack expects checksum which doesn't include the pseudo
  500. * header, the HW adds it. To address that, we are subtracting the pseudo
  501. * header checksum from the checksum value provided by the HW.
  502. */
  503. static int get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
  504. struct iphdr *iph)
  505. {
  506. __u16 length_for_csum = 0;
  507. __wsum csum_pseudo_header = 0;
  508. __u8 ipproto = iph->protocol;
  509. if (unlikely(ipproto == IPPROTO_SCTP))
  510. return -1;
  511. length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
  512. csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  513. length_for_csum, ipproto, 0);
  514. skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
  515. return 0;
  516. }
  517. #if IS_ENABLED(CONFIG_IPV6)
  518. /* In IPv6 packets, hw_checksum lacks 6 bytes from IPv6 header:
  519. * 4 first bytes : priority, version, flow_lbl
  520. * and 2 additional bytes : nexthdr, hop_limit.
  521. */
  522. static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
  523. struct ipv6hdr *ipv6h)
  524. {
  525. __u8 nexthdr = ipv6h->nexthdr;
  526. __wsum temp;
  527. if (unlikely(nexthdr == IPPROTO_FRAGMENT ||
  528. nexthdr == IPPROTO_HOPOPTS ||
  529. nexthdr == IPPROTO_SCTP))
  530. return -1;
  531. /* priority, version, flow_lbl */
  532. temp = csum_add(hw_checksum, *(__wsum *)ipv6h);
  533. /* nexthdr and hop_limit */
  534. skb->csum = csum_add(temp, (__force __wsum)*(__be16 *)&ipv6h->nexthdr);
  535. return 0;
  536. }
  537. #endif
  538. #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
  539. /* We reach this function only after checking that any of
  540. * the (IPv4 | IPv6) bits are set in cqe->status.
  541. */
  542. static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  543. netdev_features_t dev_features)
  544. {
  545. __wsum hw_checksum = 0;
  546. void *hdr;
  547. /* CQE csum doesn't cover padding octets in short ethernet
  548. * frames. And the pad field is appended prior to calculating
  549. * and appending the FCS field.
  550. *
  551. * Detecting these padded frames requires to verify and parse
  552. * IP headers, so we simply force all those small frames to skip
  553. * checksum complete.
  554. */
  555. if (short_frame(skb->len))
  556. return -EINVAL;
  557. hdr = (u8 *)va + sizeof(struct ethhdr);
  558. hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
  559. if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
  560. !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
  561. hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
  562. hdr += sizeof(struct vlan_hdr);
  563. }
  564. #if IS_ENABLED(CONFIG_IPV6)
  565. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
  566. return get_fixed_ipv6_csum(hw_checksum, skb, hdr);
  567. #endif
  568. return get_fixed_ipv4_csum(hw_checksum, skb, hdr);
  569. }
  570. #if IS_ENABLED(CONFIG_IPV6)
  571. #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4 | MLX4_CQE_STATUS_IPV6)
  572. #else
  573. #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4)
  574. #endif
  575. int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  576. {
  577. struct mlx4_en_priv *priv = netdev_priv(dev);
  578. int factor = priv->cqe_factor;
  579. struct mlx4_en_rx_ring *ring;
  580. struct bpf_prog *xdp_prog;
  581. int cq_ring = cq->ring;
  582. bool doorbell_pending;
  583. struct mlx4_cqe *cqe;
  584. struct xdp_buff xdp;
  585. int polled = 0;
  586. int index;
  587. if (unlikely(!priv->port_up || budget <= 0))
  588. return 0;
  589. ring = priv->rx_ring[cq_ring];
  590. /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
  591. rcu_read_lock();
  592. xdp_prog = rcu_dereference(ring->xdp_prog);
  593. xdp.rxq = &ring->xdp_rxq;
  594. doorbell_pending = 0;
  595. /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
  596. * descriptor offset can be deduced from the CQE index instead of
  597. * reading 'cqe->index' */
  598. index = cq->mcq.cons_index & ring->size_mask;
  599. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  600. /* Process all completed CQEs */
  601. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  602. cq->mcq.cons_index & cq->size)) {
  603. struct mlx4_en_rx_alloc *frags;
  604. enum pkt_hash_types hash_type;
  605. struct sk_buff *skb;
  606. unsigned int length;
  607. int ip_summed;
  608. void *va;
  609. int nr;
  610. frags = ring->rx_info + (index << priv->log_rx_info);
  611. va = page_address(frags[0].page) + frags[0].page_offset;
  612. prefetchw(va);
  613. /*
  614. * make sure we read the CQE after we read the ownership bit
  615. */
  616. dma_rmb();
  617. /* Drop packet on bad receive or bad checksum */
  618. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  619. MLX4_CQE_OPCODE_ERROR)) {
  620. en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
  621. ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
  622. ((struct mlx4_err_cqe *)cqe)->syndrome);
  623. goto next;
  624. }
  625. if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
  626. en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
  627. goto next;
  628. }
  629. /* Check if we need to drop the packet if SRIOV is not enabled
  630. * and not performing the selftest or flb disabled
  631. */
  632. if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
  633. const struct ethhdr *ethh = va;
  634. dma_addr_t dma;
  635. /* Get pointer to first fragment since we haven't
  636. * skb yet and cast it to ethhdr struct
  637. */
  638. dma = frags[0].dma + frags[0].page_offset;
  639. dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
  640. DMA_FROM_DEVICE);
  641. if (is_multicast_ether_addr(ethh->h_dest)) {
  642. struct mlx4_mac_entry *entry;
  643. struct hlist_head *bucket;
  644. unsigned int mac_hash;
  645. /* Drop the packet, since HW loopback-ed it */
  646. mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
  647. bucket = &priv->mac_hash[mac_hash];
  648. hlist_for_each_entry_rcu(entry, bucket, hlist) {
  649. if (ether_addr_equal_64bits(entry->mac,
  650. ethh->h_source))
  651. goto next;
  652. }
  653. }
  654. }
  655. if (unlikely(priv->validate_loopback)) {
  656. validate_loopback(priv, va);
  657. goto next;
  658. }
  659. /*
  660. * Packet is OK - process it.
  661. */
  662. length = be32_to_cpu(cqe->byte_cnt);
  663. length -= ring->fcs_del;
  664. /* A bpf program gets first chance to drop the packet. It may
  665. * read bytes but not past the end of the frag.
  666. */
  667. if (xdp_prog) {
  668. dma_addr_t dma;
  669. void *orig_data;
  670. u32 act;
  671. dma = frags[0].dma + frags[0].page_offset;
  672. dma_sync_single_for_cpu(priv->ddev, dma,
  673. priv->frag_info[0].frag_size,
  674. DMA_FROM_DEVICE);
  675. xdp.data_hard_start = va - frags[0].page_offset;
  676. xdp.data = va;
  677. xdp_set_data_meta_invalid(&xdp);
  678. xdp.data_end = xdp.data + length;
  679. orig_data = xdp.data;
  680. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  681. length = xdp.data_end - xdp.data;
  682. if (xdp.data != orig_data) {
  683. frags[0].page_offset = xdp.data -
  684. xdp.data_hard_start;
  685. va = xdp.data;
  686. }
  687. switch (act) {
  688. case XDP_PASS:
  689. break;
  690. case XDP_TX:
  691. if (likely(!mlx4_en_xmit_frame(ring, frags, priv,
  692. length, cq_ring,
  693. &doorbell_pending))) {
  694. frags[0].page = NULL;
  695. goto next;
  696. }
  697. trace_xdp_exception(dev, xdp_prog, act);
  698. goto xdp_drop_no_cnt; /* Drop on xmit failure */
  699. default:
  700. bpf_warn_invalid_xdp_action(act);
  701. /* fall through */
  702. case XDP_ABORTED:
  703. trace_xdp_exception(dev, xdp_prog, act);
  704. /* fall through */
  705. case XDP_DROP:
  706. ring->xdp_drop++;
  707. xdp_drop_no_cnt:
  708. goto next;
  709. }
  710. }
  711. ring->bytes += length;
  712. ring->packets++;
  713. skb = napi_get_frags(&cq->napi);
  714. if (unlikely(!skb))
  715. goto next;
  716. if (unlikely(ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL)) {
  717. u64 timestamp = mlx4_en_get_cqe_ts(cqe);
  718. mlx4_en_fill_hwtstamps(priv->mdev, skb_hwtstamps(skb),
  719. timestamp);
  720. }
  721. skb_record_rx_queue(skb, cq_ring);
  722. if (likely(dev->features & NETIF_F_RXCSUM)) {
  723. /* TODO: For IP non TCP/UDP packets when csum complete is
  724. * not an option (not supported or any other reason) we can
  725. * actually check cqe IPOK status bit and report
  726. * CHECKSUM_UNNECESSARY rather than CHECKSUM_NONE
  727. */
  728. if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
  729. MLX4_CQE_STATUS_UDP)) &&
  730. (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
  731. cqe->checksum == cpu_to_be16(0xffff)) {
  732. bool l2_tunnel;
  733. l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
  734. (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
  735. ip_summed = CHECKSUM_UNNECESSARY;
  736. hash_type = PKT_HASH_TYPE_L4;
  737. if (l2_tunnel)
  738. skb->csum_level = 1;
  739. ring->csum_ok++;
  740. } else {
  741. if (!(priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
  742. (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IP_ANY))))
  743. goto csum_none;
  744. if (check_csum(cqe, skb, va, dev->features))
  745. goto csum_none;
  746. ip_summed = CHECKSUM_COMPLETE;
  747. hash_type = PKT_HASH_TYPE_L3;
  748. ring->csum_complete++;
  749. }
  750. } else {
  751. csum_none:
  752. ip_summed = CHECKSUM_NONE;
  753. hash_type = PKT_HASH_TYPE_L3;
  754. ring->csum_none++;
  755. }
  756. skb->ip_summed = ip_summed;
  757. if (dev->features & NETIF_F_RXHASH)
  758. skb_set_hash(skb,
  759. be32_to_cpu(cqe->immed_rss_invalid),
  760. hash_type);
  761. if ((cqe->vlan_my_qpn &
  762. cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
  763. (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
  764. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  765. be16_to_cpu(cqe->sl_vid));
  766. else if ((cqe->vlan_my_qpn &
  767. cpu_to_be32(MLX4_CQE_SVLAN_PRESENT_MASK)) &&
  768. (dev->features & NETIF_F_HW_VLAN_STAG_RX))
  769. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
  770. be16_to_cpu(cqe->sl_vid));
  771. nr = mlx4_en_complete_rx_desc(priv, frags, skb, length);
  772. if (likely(nr)) {
  773. skb_shinfo(skb)->nr_frags = nr;
  774. skb->len = length;
  775. skb->data_len = length;
  776. napi_gro_frags(&cq->napi);
  777. } else {
  778. skb->vlan_tci = 0;
  779. skb_clear_hash(skb);
  780. }
  781. next:
  782. ++cq->mcq.cons_index;
  783. index = (cq->mcq.cons_index) & ring->size_mask;
  784. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  785. if (unlikely(++polled == budget))
  786. break;
  787. }
  788. rcu_read_unlock();
  789. if (likely(polled)) {
  790. if (doorbell_pending) {
  791. priv->tx_cq[TX_XDP][cq_ring]->xdp_busy = true;
  792. mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq_ring]);
  793. }
  794. mlx4_cq_set_ci(&cq->mcq);
  795. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  796. ring->cons = cq->mcq.cons_index;
  797. }
  798. AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
  799. mlx4_en_refill_rx_buffers(priv, ring);
  800. return polled;
  801. }
  802. void mlx4_en_rx_irq(struct mlx4_cq *mcq)
  803. {
  804. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  805. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  806. if (likely(priv->port_up))
  807. napi_schedule_irqoff(&cq->napi);
  808. else
  809. mlx4_en_arm_cq(priv, cq);
  810. }
  811. /* Rx CQ polling - called by NAPI */
  812. int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
  813. {
  814. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  815. struct net_device *dev = cq->dev;
  816. struct mlx4_en_priv *priv = netdev_priv(dev);
  817. struct mlx4_en_cq *xdp_tx_cq = NULL;
  818. bool clean_complete = true;
  819. int done;
  820. if (priv->tx_ring_num[TX_XDP]) {
  821. xdp_tx_cq = priv->tx_cq[TX_XDP][cq->ring];
  822. if (xdp_tx_cq->xdp_busy) {
  823. clean_complete = mlx4_en_process_tx_cq(dev, xdp_tx_cq,
  824. budget);
  825. xdp_tx_cq->xdp_busy = !clean_complete;
  826. }
  827. }
  828. done = mlx4_en_process_rx_cq(dev, cq, budget);
  829. /* If we used up all the quota - we're probably not done yet... */
  830. if (done == budget || !clean_complete) {
  831. const struct cpumask *aff;
  832. struct irq_data *idata;
  833. int cpu_curr;
  834. /* in case we got here because of !clean_complete */
  835. done = budget;
  836. INC_PERF_COUNTER(priv->pstats.napi_quota);
  837. cpu_curr = smp_processor_id();
  838. idata = irq_desc_get_irq_data(cq->irq_desc);
  839. aff = irq_data_get_affinity_mask(idata);
  840. if (likely(cpumask_test_cpu(cpu_curr, aff)))
  841. return budget;
  842. /* Current cpu is not according to smp_irq_affinity -
  843. * probably affinity changed. Need to stop this NAPI
  844. * poll, and restart it on the right CPU.
  845. * Try to avoid returning a too small value (like 0),
  846. * to not fool net_rx_action() and its netdev_budget
  847. */
  848. if (done)
  849. done--;
  850. }
  851. /* Done for now */
  852. if (likely(napi_complete_done(napi, done)))
  853. mlx4_en_arm_cq(priv, cq);
  854. return done;
  855. }
  856. void mlx4_en_calc_rx_buf(struct net_device *dev)
  857. {
  858. struct mlx4_en_priv *priv = netdev_priv(dev);
  859. int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
  860. int i = 0;
  861. /* bpf requires buffers to be set up as 1 packet per page.
  862. * This only works when num_frags == 1.
  863. */
  864. if (priv->tx_ring_num[TX_XDP]) {
  865. priv->frag_info[0].frag_size = eff_mtu;
  866. /* This will gain efficient xdp frame recycling at the
  867. * expense of more costly truesize accounting
  868. */
  869. priv->frag_info[0].frag_stride = PAGE_SIZE;
  870. priv->dma_dir = PCI_DMA_BIDIRECTIONAL;
  871. priv->rx_headroom = XDP_PACKET_HEADROOM;
  872. i = 1;
  873. } else {
  874. int frag_size_max = 2048, buf_size = 0;
  875. /* should not happen, right ? */
  876. if (eff_mtu > PAGE_SIZE + (MLX4_EN_MAX_RX_FRAGS - 1) * 2048)
  877. frag_size_max = PAGE_SIZE;
  878. while (buf_size < eff_mtu) {
  879. int frag_stride, frag_size = eff_mtu - buf_size;
  880. int pad, nb;
  881. if (i < MLX4_EN_MAX_RX_FRAGS - 1)
  882. frag_size = min(frag_size, frag_size_max);
  883. priv->frag_info[i].frag_size = frag_size;
  884. frag_stride = ALIGN(frag_size, SMP_CACHE_BYTES);
  885. /* We can only pack 2 1536-bytes frames in on 4K page
  886. * Therefore, each frame would consume more bytes (truesize)
  887. */
  888. nb = PAGE_SIZE / frag_stride;
  889. pad = (PAGE_SIZE - nb * frag_stride) / nb;
  890. pad &= ~(SMP_CACHE_BYTES - 1);
  891. priv->frag_info[i].frag_stride = frag_stride + pad;
  892. buf_size += frag_size;
  893. i++;
  894. }
  895. priv->dma_dir = PCI_DMA_FROMDEVICE;
  896. priv->rx_headroom = 0;
  897. }
  898. priv->num_frags = i;
  899. priv->rx_skb_size = eff_mtu;
  900. priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
  901. en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
  902. eff_mtu, priv->num_frags);
  903. for (i = 0; i < priv->num_frags; i++) {
  904. en_dbg(DRV,
  905. priv,
  906. " frag:%d - size:%d stride:%d\n",
  907. i,
  908. priv->frag_info[i].frag_size,
  909. priv->frag_info[i].frag_stride);
  910. }
  911. }
  912. /* RSS related functions */
  913. static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
  914. struct mlx4_en_rx_ring *ring,
  915. enum mlx4_qp_state *state,
  916. struct mlx4_qp *qp)
  917. {
  918. struct mlx4_en_dev *mdev = priv->mdev;
  919. struct mlx4_qp_context *context;
  920. int err = 0;
  921. context = kmalloc(sizeof(*context), GFP_KERNEL);
  922. if (!context)
  923. return -ENOMEM;
  924. err = mlx4_qp_alloc(mdev->dev, qpn, qp);
  925. if (err) {
  926. en_err(priv, "Failed to allocate qp #%x\n", qpn);
  927. goto out;
  928. }
  929. qp->event = mlx4_en_sqp_event;
  930. memset(context, 0, sizeof(*context));
  931. mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
  932. qpn, ring->cqn, -1, context);
  933. context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
  934. /* Cancel FCS removal if FW allows */
  935. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
  936. context->param3 |= cpu_to_be32(1 << 29);
  937. if (priv->dev->features & NETIF_F_RXFCS)
  938. ring->fcs_del = 0;
  939. else
  940. ring->fcs_del = ETH_FCS_LEN;
  941. } else
  942. ring->fcs_del = 0;
  943. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
  944. if (err) {
  945. mlx4_qp_remove(mdev->dev, qp);
  946. mlx4_qp_free(mdev->dev, qp);
  947. }
  948. mlx4_en_update_rx_prod_db(ring);
  949. out:
  950. kfree(context);
  951. return err;
  952. }
  953. int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
  954. {
  955. int err;
  956. u32 qpn;
  957. err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
  958. MLX4_RESERVE_A0_QP,
  959. MLX4_RES_USAGE_DRIVER);
  960. if (err) {
  961. en_err(priv, "Failed reserving drop qpn\n");
  962. return err;
  963. }
  964. err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp);
  965. if (err) {
  966. en_err(priv, "Failed allocating drop qp\n");
  967. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  968. return err;
  969. }
  970. return 0;
  971. }
  972. void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
  973. {
  974. u32 qpn;
  975. qpn = priv->drop_qp.qpn;
  976. mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
  977. mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
  978. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  979. }
  980. /* Allocate rx qp's and configure them according to rss map */
  981. int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
  982. {
  983. struct mlx4_en_dev *mdev = priv->mdev;
  984. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  985. struct mlx4_qp_context context;
  986. struct mlx4_rss_context *rss_context;
  987. int rss_rings;
  988. void *ptr;
  989. u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
  990. MLX4_RSS_TCP_IPV6);
  991. int i, qpn;
  992. int err = 0;
  993. int good_qps = 0;
  994. u8 flags;
  995. en_dbg(DRV, priv, "Configuring rss steering\n");
  996. flags = priv->rx_ring_num == 1 ? MLX4_RESERVE_A0_QP : 0;
  997. err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
  998. priv->rx_ring_num,
  999. &rss_map->base_qpn, flags,
  1000. MLX4_RES_USAGE_DRIVER);
  1001. if (err) {
  1002. en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
  1003. return err;
  1004. }
  1005. for (i = 0; i < priv->rx_ring_num; i++) {
  1006. qpn = rss_map->base_qpn + i;
  1007. err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
  1008. &rss_map->state[i],
  1009. &rss_map->qps[i]);
  1010. if (err)
  1011. goto rss_err;
  1012. ++good_qps;
  1013. }
  1014. if (priv->rx_ring_num == 1) {
  1015. rss_map->indir_qp = &rss_map->qps[0];
  1016. priv->base_qpn = rss_map->indir_qp->qpn;
  1017. en_info(priv, "Optimized Non-RSS steering\n");
  1018. return 0;
  1019. }
  1020. rss_map->indir_qp = kzalloc(sizeof(*rss_map->indir_qp), GFP_KERNEL);
  1021. if (!rss_map->indir_qp) {
  1022. err = -ENOMEM;
  1023. goto rss_err;
  1024. }
  1025. /* Configure RSS indirection qp */
  1026. err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, rss_map->indir_qp);
  1027. if (err) {
  1028. en_err(priv, "Failed to allocate RSS indirection QP\n");
  1029. goto qp_alloc_err;
  1030. }
  1031. rss_map->indir_qp->event = mlx4_en_sqp_event;
  1032. mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
  1033. priv->rx_ring[0]->cqn, -1, &context);
  1034. if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
  1035. rss_rings = priv->rx_ring_num;
  1036. else
  1037. rss_rings = priv->prof->rss_rings;
  1038. ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
  1039. + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  1040. rss_context = ptr;
  1041. rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
  1042. (rss_map->base_qpn));
  1043. rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
  1044. if (priv->mdev->profile.udp_rss) {
  1045. rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
  1046. rss_context->base_qpn_udp = rss_context->default_qpn;
  1047. }
  1048. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1049. en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
  1050. rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
  1051. }
  1052. rss_context->flags = rss_mask;
  1053. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1054. if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
  1055. rss_context->hash_fn = MLX4_RSS_HASH_XOR;
  1056. } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
  1057. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1058. memcpy(rss_context->rss_key, priv->rss_key,
  1059. MLX4_EN_RSS_KEY_SIZE);
  1060. } else {
  1061. en_err(priv, "Unknown RSS hash function requested\n");
  1062. err = -EINVAL;
  1063. goto indir_err;
  1064. }
  1065. err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
  1066. rss_map->indir_qp, &rss_map->indir_state);
  1067. if (err)
  1068. goto indir_err;
  1069. return 0;
  1070. indir_err:
  1071. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1072. MLX4_QP_STATE_RST, NULL, 0, 0, rss_map->indir_qp);
  1073. mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
  1074. mlx4_qp_free(mdev->dev, rss_map->indir_qp);
  1075. qp_alloc_err:
  1076. kfree(rss_map->indir_qp);
  1077. rss_map->indir_qp = NULL;
  1078. rss_err:
  1079. for (i = 0; i < good_qps; i++) {
  1080. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1081. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1082. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1083. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1084. }
  1085. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1086. return err;
  1087. }
  1088. void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
  1089. {
  1090. struct mlx4_en_dev *mdev = priv->mdev;
  1091. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  1092. int i;
  1093. if (priv->rx_ring_num > 1) {
  1094. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1095. MLX4_QP_STATE_RST, NULL, 0, 0,
  1096. rss_map->indir_qp);
  1097. mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
  1098. mlx4_qp_free(mdev->dev, rss_map->indir_qp);
  1099. kfree(rss_map->indir_qp);
  1100. rss_map->indir_qp = NULL;
  1101. }
  1102. for (i = 0; i < priv->rx_ring_num; i++) {
  1103. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1104. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1105. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1106. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1107. }
  1108. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1109. }