en_rx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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 | __GFP_COLD)) {
  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,
  230. netif_get_num_default_rss_queues());
  231. mdev->profile.prof[i].rx_ring_num =
  232. rounddown_pow_of_two(num_rx_rings);
  233. }
  234. }
  235. int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
  236. struct mlx4_en_rx_ring **pring,
  237. u32 size, u16 stride, int node)
  238. {
  239. struct mlx4_en_dev *mdev = priv->mdev;
  240. struct mlx4_en_rx_ring *ring;
  241. int err = -ENOMEM;
  242. int tmp;
  243. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  244. if (!ring) {
  245. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  246. if (!ring) {
  247. en_err(priv, "Failed to allocate RX ring structure\n");
  248. return -ENOMEM;
  249. }
  250. }
  251. ring->prod = 0;
  252. ring->cons = 0;
  253. ring->size = size;
  254. ring->size_mask = size - 1;
  255. ring->stride = stride;
  256. ring->log_stride = ffs(ring->stride) - 1;
  257. ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
  258. tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
  259. sizeof(struct mlx4_en_rx_alloc));
  260. ring->rx_info = vzalloc_node(tmp, node);
  261. if (!ring->rx_info) {
  262. ring->rx_info = vzalloc(tmp);
  263. if (!ring->rx_info) {
  264. err = -ENOMEM;
  265. goto err_ring;
  266. }
  267. }
  268. en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
  269. ring->rx_info, tmp);
  270. /* Allocate HW buffers on provided NUMA node */
  271. set_dev_node(&mdev->dev->persist->pdev->dev, node);
  272. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  273. set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
  274. if (err)
  275. goto err_info;
  276. ring->buf = ring->wqres.buf.direct.buf;
  277. ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
  278. *pring = ring;
  279. return 0;
  280. err_info:
  281. vfree(ring->rx_info);
  282. ring->rx_info = NULL;
  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. mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
  386. vfree(ring->rx_info);
  387. ring->rx_info = NULL;
  388. kfree(ring);
  389. *pring = NULL;
  390. }
  391. void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
  392. struct mlx4_en_rx_ring *ring)
  393. {
  394. int i;
  395. for (i = 0; i < ring->page_cache.index; i++) {
  396. dma_unmap_page(priv->ddev, ring->page_cache.buf[i].dma,
  397. PAGE_SIZE, priv->dma_dir);
  398. put_page(ring->page_cache.buf[i].page);
  399. }
  400. ring->page_cache.index = 0;
  401. mlx4_en_free_rx_buf(priv, ring);
  402. if (ring->stride <= TXBB_SIZE)
  403. ring->buf -= TXBB_SIZE;
  404. }
  405. static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
  406. struct mlx4_en_rx_alloc *frags,
  407. struct sk_buff *skb,
  408. int length)
  409. {
  410. const struct mlx4_en_frag_info *frag_info = priv->frag_info;
  411. unsigned int truesize = 0;
  412. int nr, frag_size;
  413. struct page *page;
  414. dma_addr_t dma;
  415. bool release;
  416. /* Collect used fragments while replacing them in the HW descriptors */
  417. for (nr = 0;; frags++) {
  418. frag_size = min_t(int, length, frag_info->frag_size);
  419. page = frags->page;
  420. if (unlikely(!page))
  421. goto fail;
  422. dma = frags->dma;
  423. dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
  424. frag_size, priv->dma_dir);
  425. __skb_fill_page_desc(skb, nr, page, frags->page_offset,
  426. frag_size);
  427. truesize += frag_info->frag_stride;
  428. if (frag_info->frag_stride == PAGE_SIZE / 2) {
  429. frags->page_offset ^= PAGE_SIZE / 2;
  430. release = page_count(page) != 1 ||
  431. page_is_pfmemalloc(page) ||
  432. page_to_nid(page) != numa_mem_id();
  433. } else {
  434. u32 sz_align = ALIGN(frag_size, SMP_CACHE_BYTES);
  435. frags->page_offset += sz_align;
  436. release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
  437. }
  438. if (release) {
  439. dma_unmap_page(priv->ddev, dma, PAGE_SIZE, priv->dma_dir);
  440. frags->page = NULL;
  441. } else {
  442. page_ref_inc(page);
  443. }
  444. nr++;
  445. length -= frag_size;
  446. if (!length)
  447. break;
  448. frag_info++;
  449. }
  450. skb->truesize += truesize;
  451. return nr;
  452. fail:
  453. while (nr > 0) {
  454. nr--;
  455. __skb_frag_unref(skb_shinfo(skb)->frags + nr);
  456. }
  457. return 0;
  458. }
  459. static void validate_loopback(struct mlx4_en_priv *priv, void *va)
  460. {
  461. const unsigned char *data = va + ETH_HLEN;
  462. int i;
  463. for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++) {
  464. if (data[i] != (unsigned char)i)
  465. return;
  466. }
  467. /* Loopback found */
  468. priv->loopback_ok = 1;
  469. }
  470. static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
  471. struct mlx4_en_rx_ring *ring)
  472. {
  473. u32 missing = ring->actual_size - (ring->prod - ring->cons);
  474. /* Try to batch allocations, but not too much. */
  475. if (missing < 8)
  476. return;
  477. do {
  478. if (mlx4_en_prepare_rx_desc(priv, ring,
  479. ring->prod & ring->size_mask,
  480. GFP_ATOMIC | __GFP_COLD |
  481. __GFP_MEMALLOC))
  482. break;
  483. ring->prod++;
  484. } while (likely(--missing));
  485. mlx4_en_update_rx_prod_db(ring);
  486. }
  487. /* When hardware doesn't strip the vlan, we need to calculate the checksum
  488. * over it and add it to the hardware's checksum calculation
  489. */
  490. static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
  491. struct vlan_hdr *vlanh)
  492. {
  493. return csum_add(hw_checksum, *(__wsum *)vlanh);
  494. }
  495. /* Although the stack expects checksum which doesn't include the pseudo
  496. * header, the HW adds it. To address that, we are subtracting the pseudo
  497. * header checksum from the checksum value provided by the HW.
  498. */
  499. static int get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
  500. struct iphdr *iph)
  501. {
  502. __u16 length_for_csum = 0;
  503. __wsum csum_pseudo_header = 0;
  504. __u8 ipproto = iph->protocol;
  505. if (unlikely(ipproto == IPPROTO_SCTP))
  506. return -1;
  507. length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
  508. csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  509. length_for_csum, ipproto, 0);
  510. skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
  511. return 0;
  512. }
  513. #if IS_ENABLED(CONFIG_IPV6)
  514. /* In IPv6 packets, besides subtracting the pseudo header checksum,
  515. * we also compute/add the IP header checksum which
  516. * is not added by the HW.
  517. */
  518. static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
  519. struct ipv6hdr *ipv6h)
  520. {
  521. __u8 nexthdr = ipv6h->nexthdr;
  522. __wsum csum_pseudo_hdr = 0;
  523. if (unlikely(nexthdr == IPPROTO_FRAGMENT ||
  524. nexthdr == IPPROTO_HOPOPTS ||
  525. nexthdr == IPPROTO_SCTP))
  526. return -1;
  527. hw_checksum = csum_add(hw_checksum, (__force __wsum)htons(nexthdr));
  528. csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
  529. sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
  530. csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
  531. csum_pseudo_hdr = csum_add(csum_pseudo_hdr,
  532. (__force __wsum)htons(nexthdr));
  533. skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
  534. skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
  535. return 0;
  536. }
  537. #endif
  538. static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  539. netdev_features_t dev_features)
  540. {
  541. __wsum hw_checksum = 0;
  542. void *hdr = (u8 *)va + sizeof(struct ethhdr);
  543. hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
  544. if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
  545. !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
  546. hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
  547. hdr += sizeof(struct vlan_hdr);
  548. }
  549. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
  550. return get_fixed_ipv4_csum(hw_checksum, skb, hdr);
  551. #if IS_ENABLED(CONFIG_IPV6)
  552. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
  553. return get_fixed_ipv6_csum(hw_checksum, skb, hdr);
  554. #endif
  555. return 0;
  556. }
  557. int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  558. {
  559. struct mlx4_en_priv *priv = netdev_priv(dev);
  560. int factor = priv->cqe_factor;
  561. struct mlx4_en_rx_ring *ring;
  562. struct bpf_prog *xdp_prog;
  563. int cq_ring = cq->ring;
  564. bool doorbell_pending;
  565. struct mlx4_cqe *cqe;
  566. int polled = 0;
  567. int index;
  568. if (unlikely(!priv->port_up))
  569. return 0;
  570. if (unlikely(budget <= 0))
  571. return polled;
  572. ring = priv->rx_ring[cq_ring];
  573. /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
  574. rcu_read_lock();
  575. xdp_prog = rcu_dereference(ring->xdp_prog);
  576. doorbell_pending = 0;
  577. /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
  578. * descriptor offset can be deduced from the CQE index instead of
  579. * reading 'cqe->index' */
  580. index = cq->mcq.cons_index & ring->size_mask;
  581. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  582. /* Process all completed CQEs */
  583. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  584. cq->mcq.cons_index & cq->size)) {
  585. struct mlx4_en_rx_alloc *frags;
  586. enum pkt_hash_types hash_type;
  587. struct sk_buff *skb;
  588. unsigned int length;
  589. int ip_summed;
  590. void *va;
  591. int nr;
  592. frags = ring->rx_info + (index << priv->log_rx_info);
  593. va = page_address(frags[0].page) + frags[0].page_offset;
  594. prefetchw(va);
  595. /*
  596. * make sure we read the CQE after we read the ownership bit
  597. */
  598. dma_rmb();
  599. /* Drop packet on bad receive or bad checksum */
  600. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  601. MLX4_CQE_OPCODE_ERROR)) {
  602. en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
  603. ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
  604. ((struct mlx4_err_cqe *)cqe)->syndrome);
  605. goto next;
  606. }
  607. if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
  608. en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
  609. goto next;
  610. }
  611. /* Check if we need to drop the packet if SRIOV is not enabled
  612. * and not performing the selftest or flb disabled
  613. */
  614. if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
  615. const struct ethhdr *ethh = va;
  616. dma_addr_t dma;
  617. /* Get pointer to first fragment since we haven't
  618. * skb yet and cast it to ethhdr struct
  619. */
  620. dma = frags[0].dma + frags[0].page_offset;
  621. dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
  622. DMA_FROM_DEVICE);
  623. if (is_multicast_ether_addr(ethh->h_dest)) {
  624. struct mlx4_mac_entry *entry;
  625. struct hlist_head *bucket;
  626. unsigned int mac_hash;
  627. /* Drop the packet, since HW loopback-ed it */
  628. mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
  629. bucket = &priv->mac_hash[mac_hash];
  630. hlist_for_each_entry_rcu(entry, bucket, hlist) {
  631. if (ether_addr_equal_64bits(entry->mac,
  632. ethh->h_source))
  633. goto next;
  634. }
  635. }
  636. }
  637. if (unlikely(priv->validate_loopback)) {
  638. validate_loopback(priv, va);
  639. goto next;
  640. }
  641. /*
  642. * Packet is OK - process it.
  643. */
  644. length = be32_to_cpu(cqe->byte_cnt);
  645. length -= ring->fcs_del;
  646. /* A bpf program gets first chance to drop the packet. It may
  647. * read bytes but not past the end of the frag.
  648. */
  649. if (xdp_prog) {
  650. struct xdp_buff xdp;
  651. dma_addr_t dma;
  652. void *orig_data;
  653. u32 act;
  654. dma = frags[0].dma + frags[0].page_offset;
  655. dma_sync_single_for_cpu(priv->ddev, dma,
  656. priv->frag_info[0].frag_size,
  657. DMA_FROM_DEVICE);
  658. xdp.data_hard_start = va - frags[0].page_offset;
  659. xdp.data = va;
  660. xdp.data_end = xdp.data + length;
  661. orig_data = xdp.data;
  662. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  663. if (xdp.data != orig_data) {
  664. length = xdp.data_end - xdp.data;
  665. frags[0].page_offset = xdp.data -
  666. xdp.data_hard_start;
  667. va = xdp.data;
  668. }
  669. switch (act) {
  670. case XDP_PASS:
  671. break;
  672. case XDP_TX:
  673. if (likely(!mlx4_en_xmit_frame(ring, frags, dev,
  674. length, cq_ring,
  675. &doorbell_pending))) {
  676. frags[0].page = NULL;
  677. goto next;
  678. }
  679. trace_xdp_exception(dev, xdp_prog, act);
  680. goto xdp_drop_no_cnt; /* Drop on xmit failure */
  681. default:
  682. bpf_warn_invalid_xdp_action(act);
  683. case XDP_ABORTED:
  684. trace_xdp_exception(dev, xdp_prog, act);
  685. case XDP_DROP:
  686. ring->xdp_drop++;
  687. xdp_drop_no_cnt:
  688. goto next;
  689. }
  690. }
  691. ring->bytes += length;
  692. ring->packets++;
  693. skb = napi_get_frags(&cq->napi);
  694. if (unlikely(!skb))
  695. goto next;
  696. if (unlikely(ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL)) {
  697. u64 timestamp = mlx4_en_get_cqe_ts(cqe);
  698. mlx4_en_fill_hwtstamps(priv->mdev, skb_hwtstamps(skb),
  699. timestamp);
  700. }
  701. skb_record_rx_queue(skb, cq_ring);
  702. if (likely(dev->features & NETIF_F_RXCSUM)) {
  703. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
  704. MLX4_CQE_STATUS_UDP)) {
  705. if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
  706. cqe->checksum == cpu_to_be16(0xffff)) {
  707. bool l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
  708. (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
  709. ip_summed = CHECKSUM_UNNECESSARY;
  710. hash_type = PKT_HASH_TYPE_L4;
  711. if (l2_tunnel)
  712. skb->csum_level = 1;
  713. ring->csum_ok++;
  714. } else {
  715. goto csum_none;
  716. }
  717. } else {
  718. if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
  719. (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  720. MLX4_CQE_STATUS_IPV6))) {
  721. if (check_csum(cqe, skb, va, dev->features)) {
  722. goto csum_none;
  723. } else {
  724. ip_summed = CHECKSUM_COMPLETE;
  725. hash_type = PKT_HASH_TYPE_L3;
  726. ring->csum_complete++;
  727. }
  728. } else {
  729. goto csum_none;
  730. }
  731. }
  732. } else {
  733. csum_none:
  734. ip_summed = CHECKSUM_NONE;
  735. hash_type = PKT_HASH_TYPE_L3;
  736. ring->csum_none++;
  737. }
  738. skb->ip_summed = ip_summed;
  739. if (dev->features & NETIF_F_RXHASH)
  740. skb_set_hash(skb,
  741. be32_to_cpu(cqe->immed_rss_invalid),
  742. hash_type);
  743. if ((cqe->vlan_my_qpn &
  744. cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
  745. (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
  746. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  747. be16_to_cpu(cqe->sl_vid));
  748. else if ((cqe->vlan_my_qpn &
  749. cpu_to_be32(MLX4_CQE_SVLAN_PRESENT_MASK)) &&
  750. (dev->features & NETIF_F_HW_VLAN_STAG_RX))
  751. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
  752. be16_to_cpu(cqe->sl_vid));
  753. nr = mlx4_en_complete_rx_desc(priv, frags, skb, length);
  754. if (likely(nr)) {
  755. skb_shinfo(skb)->nr_frags = nr;
  756. skb->len = length;
  757. skb->data_len = length;
  758. napi_gro_frags(&cq->napi);
  759. } else {
  760. skb->vlan_tci = 0;
  761. skb_clear_hash(skb);
  762. }
  763. next:
  764. ++cq->mcq.cons_index;
  765. index = (cq->mcq.cons_index) & ring->size_mask;
  766. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  767. if (unlikely(++polled == budget))
  768. break;
  769. }
  770. rcu_read_unlock();
  771. if (likely(polled)) {
  772. if (doorbell_pending) {
  773. priv->tx_cq[TX_XDP][cq_ring]->xdp_busy = true;
  774. mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq_ring]);
  775. }
  776. mlx4_cq_set_ci(&cq->mcq);
  777. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  778. ring->cons = cq->mcq.cons_index;
  779. }
  780. AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
  781. mlx4_en_refill_rx_buffers(priv, ring);
  782. return polled;
  783. }
  784. void mlx4_en_rx_irq(struct mlx4_cq *mcq)
  785. {
  786. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  787. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  788. if (likely(priv->port_up))
  789. napi_schedule_irqoff(&cq->napi);
  790. else
  791. mlx4_en_arm_cq(priv, cq);
  792. }
  793. /* Rx CQ polling - called by NAPI */
  794. int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
  795. {
  796. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  797. struct net_device *dev = cq->dev;
  798. struct mlx4_en_priv *priv = netdev_priv(dev);
  799. struct mlx4_en_cq *xdp_tx_cq = NULL;
  800. bool clean_complete = true;
  801. int done;
  802. if (priv->tx_ring_num[TX_XDP]) {
  803. xdp_tx_cq = priv->tx_cq[TX_XDP][cq->ring];
  804. if (xdp_tx_cq->xdp_busy) {
  805. clean_complete = mlx4_en_process_tx_cq(dev, xdp_tx_cq,
  806. budget);
  807. xdp_tx_cq->xdp_busy = !clean_complete;
  808. }
  809. }
  810. done = mlx4_en_process_rx_cq(dev, cq, budget);
  811. /* If we used up all the quota - we're probably not done yet... */
  812. if (done == budget || !clean_complete) {
  813. const struct cpumask *aff;
  814. struct irq_data *idata;
  815. int cpu_curr;
  816. /* in case we got here because of !clean_complete */
  817. done = budget;
  818. INC_PERF_COUNTER(priv->pstats.napi_quota);
  819. cpu_curr = smp_processor_id();
  820. idata = irq_desc_get_irq_data(cq->irq_desc);
  821. aff = irq_data_get_affinity_mask(idata);
  822. if (likely(cpumask_test_cpu(cpu_curr, aff)))
  823. return budget;
  824. /* Current cpu is not according to smp_irq_affinity -
  825. * probably affinity changed. Need to stop this NAPI
  826. * poll, and restart it on the right CPU.
  827. * Try to avoid returning a too small value (like 0),
  828. * to not fool net_rx_action() and its netdev_budget
  829. */
  830. if (done)
  831. done--;
  832. }
  833. /* Done for now */
  834. if (likely(napi_complete_done(napi, done)))
  835. mlx4_en_arm_cq(priv, cq);
  836. return done;
  837. }
  838. void mlx4_en_calc_rx_buf(struct net_device *dev)
  839. {
  840. struct mlx4_en_priv *priv = netdev_priv(dev);
  841. int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
  842. int i = 0;
  843. /* bpf requires buffers to be set up as 1 packet per page.
  844. * This only works when num_frags == 1.
  845. */
  846. if (priv->tx_ring_num[TX_XDP]) {
  847. priv->frag_info[0].frag_size = eff_mtu;
  848. /* This will gain efficient xdp frame recycling at the
  849. * expense of more costly truesize accounting
  850. */
  851. priv->frag_info[0].frag_stride = PAGE_SIZE;
  852. priv->dma_dir = PCI_DMA_BIDIRECTIONAL;
  853. priv->rx_headroom = XDP_PACKET_HEADROOM;
  854. i = 1;
  855. } else {
  856. int frag_size_max = 2048, buf_size = 0;
  857. /* should not happen, right ? */
  858. if (eff_mtu > PAGE_SIZE + (MLX4_EN_MAX_RX_FRAGS - 1) * 2048)
  859. frag_size_max = PAGE_SIZE;
  860. while (buf_size < eff_mtu) {
  861. int frag_stride, frag_size = eff_mtu - buf_size;
  862. int pad, nb;
  863. if (i < MLX4_EN_MAX_RX_FRAGS - 1)
  864. frag_size = min(frag_size, frag_size_max);
  865. priv->frag_info[i].frag_size = frag_size;
  866. frag_stride = ALIGN(frag_size, SMP_CACHE_BYTES);
  867. /* We can only pack 2 1536-bytes frames in on 4K page
  868. * Therefore, each frame would consume more bytes (truesize)
  869. */
  870. nb = PAGE_SIZE / frag_stride;
  871. pad = (PAGE_SIZE - nb * frag_stride) / nb;
  872. pad &= ~(SMP_CACHE_BYTES - 1);
  873. priv->frag_info[i].frag_stride = frag_stride + pad;
  874. buf_size += frag_size;
  875. i++;
  876. }
  877. priv->dma_dir = PCI_DMA_FROMDEVICE;
  878. priv->rx_headroom = 0;
  879. }
  880. priv->num_frags = i;
  881. priv->rx_skb_size = eff_mtu;
  882. priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
  883. en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
  884. eff_mtu, priv->num_frags);
  885. for (i = 0; i < priv->num_frags; i++) {
  886. en_dbg(DRV,
  887. priv,
  888. " frag:%d - size:%d stride:%d\n",
  889. i,
  890. priv->frag_info[i].frag_size,
  891. priv->frag_info[i].frag_stride);
  892. }
  893. }
  894. /* RSS related functions */
  895. static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
  896. struct mlx4_en_rx_ring *ring,
  897. enum mlx4_qp_state *state,
  898. struct mlx4_qp *qp)
  899. {
  900. struct mlx4_en_dev *mdev = priv->mdev;
  901. struct mlx4_qp_context *context;
  902. int err = 0;
  903. context = kmalloc(sizeof(*context), GFP_KERNEL);
  904. if (!context)
  905. return -ENOMEM;
  906. err = mlx4_qp_alloc(mdev->dev, qpn, qp);
  907. if (err) {
  908. en_err(priv, "Failed to allocate qp #%x\n", qpn);
  909. goto out;
  910. }
  911. qp->event = mlx4_en_sqp_event;
  912. memset(context, 0, sizeof(*context));
  913. mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
  914. qpn, ring->cqn, -1, context);
  915. context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
  916. /* Cancel FCS removal if FW allows */
  917. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
  918. context->param3 |= cpu_to_be32(1 << 29);
  919. if (priv->dev->features & NETIF_F_RXFCS)
  920. ring->fcs_del = 0;
  921. else
  922. ring->fcs_del = ETH_FCS_LEN;
  923. } else
  924. ring->fcs_del = 0;
  925. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
  926. if (err) {
  927. mlx4_qp_remove(mdev->dev, qp);
  928. mlx4_qp_free(mdev->dev, qp);
  929. }
  930. mlx4_en_update_rx_prod_db(ring);
  931. out:
  932. kfree(context);
  933. return err;
  934. }
  935. int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
  936. {
  937. int err;
  938. u32 qpn;
  939. err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
  940. MLX4_RESERVE_A0_QP,
  941. MLX4_RES_USAGE_DRIVER);
  942. if (err) {
  943. en_err(priv, "Failed reserving drop qpn\n");
  944. return err;
  945. }
  946. err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp);
  947. if (err) {
  948. en_err(priv, "Failed allocating drop qp\n");
  949. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  950. return err;
  951. }
  952. return 0;
  953. }
  954. void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
  955. {
  956. u32 qpn;
  957. qpn = priv->drop_qp.qpn;
  958. mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
  959. mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
  960. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  961. }
  962. /* Allocate rx qp's and configure them according to rss map */
  963. int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
  964. {
  965. struct mlx4_en_dev *mdev = priv->mdev;
  966. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  967. struct mlx4_qp_context context;
  968. struct mlx4_rss_context *rss_context;
  969. int rss_rings;
  970. void *ptr;
  971. u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
  972. MLX4_RSS_TCP_IPV6);
  973. int i, qpn;
  974. int err = 0;
  975. int good_qps = 0;
  976. u8 flags;
  977. en_dbg(DRV, priv, "Configuring rss steering\n");
  978. flags = priv->rx_ring_num == 1 ? MLX4_RESERVE_A0_QP : 0;
  979. err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
  980. priv->rx_ring_num,
  981. &rss_map->base_qpn, flags,
  982. MLX4_RES_USAGE_DRIVER);
  983. if (err) {
  984. en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
  985. return err;
  986. }
  987. for (i = 0; i < priv->rx_ring_num; i++) {
  988. qpn = rss_map->base_qpn + i;
  989. err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
  990. &rss_map->state[i],
  991. &rss_map->qps[i]);
  992. if (err)
  993. goto rss_err;
  994. ++good_qps;
  995. }
  996. if (priv->rx_ring_num == 1) {
  997. rss_map->indir_qp = &rss_map->qps[0];
  998. priv->base_qpn = rss_map->indir_qp->qpn;
  999. en_info(priv, "Optimized Non-RSS steering\n");
  1000. return 0;
  1001. }
  1002. rss_map->indir_qp = kzalloc(sizeof(*rss_map->indir_qp), GFP_KERNEL);
  1003. if (!rss_map->indir_qp) {
  1004. err = -ENOMEM;
  1005. goto rss_err;
  1006. }
  1007. /* Configure RSS indirection qp */
  1008. err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, rss_map->indir_qp);
  1009. if (err) {
  1010. en_err(priv, "Failed to allocate RSS indirection QP\n");
  1011. goto rss_err;
  1012. }
  1013. rss_map->indir_qp->event = mlx4_en_sqp_event;
  1014. mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
  1015. priv->rx_ring[0]->cqn, -1, &context);
  1016. if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
  1017. rss_rings = priv->rx_ring_num;
  1018. else
  1019. rss_rings = priv->prof->rss_rings;
  1020. ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
  1021. + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  1022. rss_context = ptr;
  1023. rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
  1024. (rss_map->base_qpn));
  1025. rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
  1026. if (priv->mdev->profile.udp_rss) {
  1027. rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
  1028. rss_context->base_qpn_udp = rss_context->default_qpn;
  1029. }
  1030. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1031. en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
  1032. rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
  1033. }
  1034. rss_context->flags = rss_mask;
  1035. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1036. if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
  1037. rss_context->hash_fn = MLX4_RSS_HASH_XOR;
  1038. } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
  1039. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1040. memcpy(rss_context->rss_key, priv->rss_key,
  1041. MLX4_EN_RSS_KEY_SIZE);
  1042. } else {
  1043. en_err(priv, "Unknown RSS hash function requested\n");
  1044. err = -EINVAL;
  1045. goto indir_err;
  1046. }
  1047. err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
  1048. rss_map->indir_qp, &rss_map->indir_state);
  1049. if (err)
  1050. goto indir_err;
  1051. return 0;
  1052. indir_err:
  1053. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1054. MLX4_QP_STATE_RST, NULL, 0, 0, rss_map->indir_qp);
  1055. mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
  1056. mlx4_qp_free(mdev->dev, rss_map->indir_qp);
  1057. kfree(rss_map->indir_qp);
  1058. rss_map->indir_qp = NULL;
  1059. rss_err:
  1060. for (i = 0; i < good_qps; i++) {
  1061. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1062. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1063. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1064. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1065. }
  1066. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1067. return err;
  1068. }
  1069. void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
  1070. {
  1071. struct mlx4_en_dev *mdev = priv->mdev;
  1072. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  1073. int i;
  1074. if (priv->rx_ring_num > 1) {
  1075. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1076. MLX4_QP_STATE_RST, NULL, 0, 0,
  1077. rss_map->indir_qp);
  1078. mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
  1079. mlx4_qp_free(mdev->dev, rss_map->indir_qp);
  1080. kfree(rss_map->indir_qp);
  1081. rss_map->indir_qp = NULL;
  1082. }
  1083. for (i = 0; i < priv->rx_ring_num; i++) {
  1084. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1085. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1086. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1087. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1088. }
  1089. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1090. }