en_rx.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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/mlx4/cq.h>
  36. #include <linux/slab.h>
  37. #include <linux/mlx4/qp.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/rculist.h>
  40. #include <linux/if_ether.h>
  41. #include <linux/if_vlan.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/irq.h>
  44. #if IS_ENABLED(CONFIG_IPV6)
  45. #include <net/ip6_checksum.h>
  46. #endif
  47. #include "mlx4_en.h"
  48. static int mlx4_alloc_pages(struct mlx4_en_priv *priv,
  49. struct mlx4_en_rx_alloc *page_alloc,
  50. const struct mlx4_en_frag_info *frag_info,
  51. gfp_t _gfp)
  52. {
  53. int order;
  54. struct page *page;
  55. dma_addr_t dma;
  56. for (order = frag_info->order; ;) {
  57. gfp_t gfp = _gfp;
  58. if (order)
  59. gfp |= __GFP_COMP | __GFP_NOWARN | __GFP_NOMEMALLOC;
  60. page = alloc_pages(gfp, order);
  61. if (likely(page))
  62. break;
  63. if (--order < 0 ||
  64. ((PAGE_SIZE << order) < frag_info->frag_size))
  65. return -ENOMEM;
  66. }
  67. dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE << order,
  68. frag_info->dma_dir);
  69. if (unlikely(dma_mapping_error(priv->ddev, dma))) {
  70. put_page(page);
  71. return -ENOMEM;
  72. }
  73. page_alloc->page_size = PAGE_SIZE << order;
  74. page_alloc->page = page;
  75. page_alloc->dma = dma;
  76. page_alloc->page_offset = 0;
  77. /* Not doing get_page() for each frag is a big win
  78. * on asymetric workloads. Note we can not use atomic_set().
  79. */
  80. page_ref_add(page, page_alloc->page_size / frag_info->frag_stride - 1);
  81. return 0;
  82. }
  83. static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
  84. struct mlx4_en_rx_desc *rx_desc,
  85. struct mlx4_en_rx_alloc *frags,
  86. struct mlx4_en_rx_alloc *ring_alloc,
  87. gfp_t gfp)
  88. {
  89. struct mlx4_en_rx_alloc page_alloc[MLX4_EN_MAX_RX_FRAGS];
  90. const struct mlx4_en_frag_info *frag_info;
  91. struct page *page;
  92. int i;
  93. for (i = 0; i < priv->num_frags; i++) {
  94. frag_info = &priv->frag_info[i];
  95. page_alloc[i] = ring_alloc[i];
  96. page_alloc[i].page_offset += frag_info->frag_stride;
  97. if (page_alloc[i].page_offset + frag_info->frag_stride <=
  98. ring_alloc[i].page_size)
  99. continue;
  100. if (unlikely(mlx4_alloc_pages(priv, &page_alloc[i],
  101. frag_info, gfp)))
  102. goto out;
  103. }
  104. for (i = 0; i < priv->num_frags; i++) {
  105. frags[i] = ring_alloc[i];
  106. frags[i].page_offset += priv->frag_info[i].rx_headroom;
  107. rx_desc->data[i].addr = cpu_to_be64(frags[i].dma +
  108. frags[i].page_offset);
  109. ring_alloc[i] = page_alloc[i];
  110. }
  111. return 0;
  112. out:
  113. while (i--) {
  114. if (page_alloc[i].page != ring_alloc[i].page) {
  115. dma_unmap_page(priv->ddev, page_alloc[i].dma,
  116. page_alloc[i].page_size,
  117. priv->frag_info[i].dma_dir);
  118. page = page_alloc[i].page;
  119. /* Revert changes done by mlx4_alloc_pages */
  120. page_ref_sub(page, page_alloc[i].page_size /
  121. priv->frag_info[i].frag_stride - 1);
  122. put_page(page);
  123. }
  124. }
  125. return -ENOMEM;
  126. }
  127. static void mlx4_en_free_frag(struct mlx4_en_priv *priv,
  128. struct mlx4_en_rx_alloc *frags,
  129. int i)
  130. {
  131. const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
  132. u32 next_frag_end = frags[i].page_offset + 2 * frag_info->frag_stride;
  133. if (next_frag_end > frags[i].page_size)
  134. dma_unmap_page(priv->ddev, frags[i].dma, frags[i].page_size,
  135. frag_info->dma_dir);
  136. if (frags[i].page)
  137. put_page(frags[i].page);
  138. }
  139. static int mlx4_en_init_allocator(struct mlx4_en_priv *priv,
  140. struct mlx4_en_rx_ring *ring)
  141. {
  142. int i;
  143. struct mlx4_en_rx_alloc *page_alloc;
  144. for (i = 0; i < priv->num_frags; i++) {
  145. const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
  146. if (mlx4_alloc_pages(priv, &ring->page_alloc[i],
  147. frag_info, GFP_KERNEL | __GFP_COLD))
  148. goto out;
  149. en_dbg(DRV, priv, " frag %d allocator: - size:%d frags:%d\n",
  150. i, ring->page_alloc[i].page_size,
  151. page_ref_count(ring->page_alloc[i].page));
  152. }
  153. return 0;
  154. out:
  155. while (i--) {
  156. struct page *page;
  157. page_alloc = &ring->page_alloc[i];
  158. dma_unmap_page(priv->ddev, page_alloc->dma,
  159. page_alloc->page_size,
  160. priv->frag_info[i].dma_dir);
  161. page = page_alloc->page;
  162. /* Revert changes done by mlx4_alloc_pages */
  163. page_ref_sub(page, page_alloc->page_size /
  164. priv->frag_info[i].frag_stride - 1);
  165. put_page(page);
  166. page_alloc->page = NULL;
  167. }
  168. return -ENOMEM;
  169. }
  170. static void mlx4_en_destroy_allocator(struct mlx4_en_priv *priv,
  171. struct mlx4_en_rx_ring *ring)
  172. {
  173. struct mlx4_en_rx_alloc *page_alloc;
  174. int i;
  175. for (i = 0; i < priv->num_frags; i++) {
  176. const struct mlx4_en_frag_info *frag_info = &priv->frag_info[i];
  177. page_alloc = &ring->page_alloc[i];
  178. en_dbg(DRV, priv, "Freeing allocator:%d count:%d\n",
  179. i, page_count(page_alloc->page));
  180. dma_unmap_page(priv->ddev, page_alloc->dma,
  181. page_alloc->page_size, frag_info->dma_dir);
  182. while (page_alloc->page_offset + frag_info->frag_stride <
  183. page_alloc->page_size) {
  184. put_page(page_alloc->page);
  185. page_alloc->page_offset += frag_info->frag_stride;
  186. }
  187. page_alloc->page = NULL;
  188. }
  189. }
  190. static void mlx4_en_init_rx_desc(struct mlx4_en_priv *priv,
  191. struct mlx4_en_rx_ring *ring, int index)
  192. {
  193. struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
  194. int possible_frags;
  195. int i;
  196. /* Set size and memtype fields */
  197. for (i = 0; i < priv->num_frags; i++) {
  198. rx_desc->data[i].byte_count =
  199. cpu_to_be32(priv->frag_info[i].frag_size);
  200. rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
  201. }
  202. /* If the number of used fragments does not fill up the ring stride,
  203. * remaining (unused) fragments must be padded with null address/size
  204. * and a special memory key */
  205. possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
  206. for (i = priv->num_frags; i < possible_frags; i++) {
  207. rx_desc->data[i].byte_count = 0;
  208. rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
  209. rx_desc->data[i].addr = 0;
  210. }
  211. }
  212. static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
  213. struct mlx4_en_rx_ring *ring, int index,
  214. gfp_t gfp)
  215. {
  216. struct mlx4_en_rx_desc *rx_desc = ring->buf + (index * ring->stride);
  217. struct mlx4_en_rx_alloc *frags = ring->rx_info +
  218. (index << priv->log_rx_info);
  219. if (ring->page_cache.index > 0) {
  220. frags[0] = ring->page_cache.buf[--ring->page_cache.index];
  221. rx_desc->data[0].addr = cpu_to_be64(frags[0].dma +
  222. frags[0].page_offset);
  223. return 0;
  224. }
  225. return mlx4_en_alloc_frags(priv, rx_desc, frags, ring->page_alloc, gfp);
  226. }
  227. static inline bool mlx4_en_is_ring_empty(struct mlx4_en_rx_ring *ring)
  228. {
  229. return ring->prod == ring->cons;
  230. }
  231. static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
  232. {
  233. *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
  234. }
  235. static void mlx4_en_free_rx_desc(struct mlx4_en_priv *priv,
  236. struct mlx4_en_rx_ring *ring,
  237. int index)
  238. {
  239. struct mlx4_en_rx_alloc *frags;
  240. int nr;
  241. frags = ring->rx_info + (index << priv->log_rx_info);
  242. for (nr = 0; nr < priv->num_frags; nr++) {
  243. en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
  244. mlx4_en_free_frag(priv, frags, nr);
  245. }
  246. }
  247. static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
  248. {
  249. struct mlx4_en_rx_ring *ring;
  250. int ring_ind;
  251. int buf_ind;
  252. int new_size;
  253. for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
  254. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  255. ring = priv->rx_ring[ring_ind];
  256. if (mlx4_en_prepare_rx_desc(priv, ring,
  257. ring->actual_size,
  258. GFP_KERNEL | __GFP_COLD)) {
  259. if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
  260. en_err(priv, "Failed to allocate enough rx buffers\n");
  261. return -ENOMEM;
  262. } else {
  263. new_size = rounddown_pow_of_two(ring->actual_size);
  264. en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
  265. ring->actual_size, new_size);
  266. goto reduce_rings;
  267. }
  268. }
  269. ring->actual_size++;
  270. ring->prod++;
  271. }
  272. }
  273. return 0;
  274. reduce_rings:
  275. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  276. ring = priv->rx_ring[ring_ind];
  277. while (ring->actual_size > new_size) {
  278. ring->actual_size--;
  279. ring->prod--;
  280. mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
  281. }
  282. }
  283. return 0;
  284. }
  285. static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
  286. struct mlx4_en_rx_ring *ring)
  287. {
  288. int index;
  289. en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
  290. ring->cons, ring->prod);
  291. /* Unmap and free Rx buffers */
  292. while (!mlx4_en_is_ring_empty(ring)) {
  293. index = ring->cons & ring->size_mask;
  294. en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
  295. mlx4_en_free_rx_desc(priv, ring, index);
  296. ++ring->cons;
  297. }
  298. }
  299. void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
  300. {
  301. int i;
  302. int num_of_eqs;
  303. int num_rx_rings;
  304. struct mlx4_dev *dev = mdev->dev;
  305. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
  306. num_of_eqs = max_t(int, MIN_RX_RINGS,
  307. min_t(int,
  308. mlx4_get_eqs_per_port(mdev->dev, i),
  309. DEF_RX_RINGS));
  310. num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
  311. min_t(int, num_of_eqs,
  312. netif_get_num_default_rss_queues());
  313. mdev->profile.prof[i].rx_ring_num =
  314. rounddown_pow_of_two(num_rx_rings);
  315. }
  316. }
  317. int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
  318. struct mlx4_en_rx_ring **pring,
  319. u32 size, u16 stride, int node)
  320. {
  321. struct mlx4_en_dev *mdev = priv->mdev;
  322. struct mlx4_en_rx_ring *ring;
  323. int err = -ENOMEM;
  324. int tmp;
  325. ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
  326. if (!ring) {
  327. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  328. if (!ring) {
  329. en_err(priv, "Failed to allocate RX ring structure\n");
  330. return -ENOMEM;
  331. }
  332. }
  333. ring->prod = 0;
  334. ring->cons = 0;
  335. ring->size = size;
  336. ring->size_mask = size - 1;
  337. ring->stride = stride;
  338. ring->log_stride = ffs(ring->stride) - 1;
  339. ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
  340. tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
  341. sizeof(struct mlx4_en_rx_alloc));
  342. ring->rx_info = vmalloc_node(tmp, node);
  343. if (!ring->rx_info) {
  344. ring->rx_info = vmalloc(tmp);
  345. if (!ring->rx_info) {
  346. err = -ENOMEM;
  347. goto err_ring;
  348. }
  349. }
  350. en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
  351. ring->rx_info, tmp);
  352. /* Allocate HW buffers on provided NUMA node */
  353. set_dev_node(&mdev->dev->persist->pdev->dev, node);
  354. err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
  355. set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
  356. if (err)
  357. goto err_info;
  358. ring->buf = ring->wqres.buf.direct.buf;
  359. ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
  360. *pring = ring;
  361. return 0;
  362. err_info:
  363. vfree(ring->rx_info);
  364. ring->rx_info = NULL;
  365. err_ring:
  366. kfree(ring);
  367. *pring = NULL;
  368. return err;
  369. }
  370. int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
  371. {
  372. struct mlx4_en_rx_ring *ring;
  373. int i;
  374. int ring_ind;
  375. int err;
  376. int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  377. DS_SIZE * priv->num_frags);
  378. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  379. ring = priv->rx_ring[ring_ind];
  380. ring->prod = 0;
  381. ring->cons = 0;
  382. ring->actual_size = 0;
  383. ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
  384. ring->stride = stride;
  385. if (ring->stride <= TXBB_SIZE) {
  386. /* Stamp first unused send wqe */
  387. __be32 *ptr = (__be32 *)ring->buf;
  388. __be32 stamp = cpu_to_be32(1 << STAMP_SHIFT);
  389. *ptr = stamp;
  390. /* Move pointer to start of rx section */
  391. ring->buf += TXBB_SIZE;
  392. }
  393. ring->log_stride = ffs(ring->stride) - 1;
  394. ring->buf_size = ring->size * ring->stride;
  395. memset(ring->buf, 0, ring->buf_size);
  396. mlx4_en_update_rx_prod_db(ring);
  397. /* Initialize all descriptors */
  398. for (i = 0; i < ring->size; i++)
  399. mlx4_en_init_rx_desc(priv, ring, i);
  400. /* Initialize page allocators */
  401. err = mlx4_en_init_allocator(priv, ring);
  402. if (err) {
  403. en_err(priv, "Failed initializing ring allocator\n");
  404. if (ring->stride <= TXBB_SIZE)
  405. ring->buf -= TXBB_SIZE;
  406. ring_ind--;
  407. goto err_allocator;
  408. }
  409. }
  410. err = mlx4_en_fill_rx_buffers(priv);
  411. if (err)
  412. goto err_buffers;
  413. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
  414. ring = priv->rx_ring[ring_ind];
  415. ring->size_mask = ring->actual_size - 1;
  416. mlx4_en_update_rx_prod_db(ring);
  417. }
  418. return 0;
  419. err_buffers:
  420. for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
  421. mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
  422. ring_ind = priv->rx_ring_num - 1;
  423. err_allocator:
  424. while (ring_ind >= 0) {
  425. if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
  426. priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
  427. mlx4_en_destroy_allocator(priv, priv->rx_ring[ring_ind]);
  428. ring_ind--;
  429. }
  430. return err;
  431. }
  432. /* We recover from out of memory by scheduling our napi poll
  433. * function (mlx4_en_process_cq), which tries to allocate
  434. * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
  435. */
  436. void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
  437. {
  438. int ring;
  439. if (!priv->port_up)
  440. return;
  441. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  442. if (mlx4_en_is_ring_empty(priv->rx_ring[ring])) {
  443. local_bh_disable();
  444. napi_reschedule(&priv->rx_cq[ring]->napi);
  445. local_bh_enable();
  446. }
  447. }
  448. }
  449. /* When the rx ring is running in page-per-packet mode, a released frame can go
  450. * directly into a small cache, to avoid unmapping or touching the page
  451. * allocator. In bpf prog performance scenarios, buffers are either forwarded
  452. * or dropped, never converted to skbs, so every page can come directly from
  453. * this cache when it is sized to be a multiple of the napi budget.
  454. */
  455. bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
  456. struct mlx4_en_rx_alloc *frame)
  457. {
  458. struct mlx4_en_page_cache *cache = &ring->page_cache;
  459. if (cache->index >= MLX4_EN_CACHE_SIZE)
  460. return false;
  461. cache->buf[cache->index++] = *frame;
  462. return true;
  463. }
  464. void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
  465. struct mlx4_en_rx_ring **pring,
  466. u32 size, u16 stride)
  467. {
  468. struct mlx4_en_dev *mdev = priv->mdev;
  469. struct mlx4_en_rx_ring *ring = *pring;
  470. struct bpf_prog *old_prog;
  471. old_prog = rcu_dereference_protected(
  472. ring->xdp_prog,
  473. lockdep_is_held(&mdev->state_lock));
  474. if (old_prog)
  475. bpf_prog_put(old_prog);
  476. mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
  477. vfree(ring->rx_info);
  478. ring->rx_info = NULL;
  479. kfree(ring);
  480. *pring = NULL;
  481. }
  482. void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
  483. struct mlx4_en_rx_ring *ring)
  484. {
  485. int i;
  486. for (i = 0; i < ring->page_cache.index; i++) {
  487. struct mlx4_en_rx_alloc *frame = &ring->page_cache.buf[i];
  488. dma_unmap_page(priv->ddev, frame->dma, frame->page_size,
  489. priv->frag_info[0].dma_dir);
  490. put_page(frame->page);
  491. }
  492. ring->page_cache.index = 0;
  493. mlx4_en_free_rx_buf(priv, ring);
  494. if (ring->stride <= TXBB_SIZE)
  495. ring->buf -= TXBB_SIZE;
  496. mlx4_en_destroy_allocator(priv, ring);
  497. }
  498. static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
  499. struct mlx4_en_rx_desc *rx_desc,
  500. struct mlx4_en_rx_alloc *frags,
  501. struct sk_buff *skb,
  502. int length)
  503. {
  504. struct skb_frag_struct *skb_frags_rx = skb_shinfo(skb)->frags;
  505. struct mlx4_en_frag_info *frag_info;
  506. int nr;
  507. dma_addr_t dma;
  508. /* Collect used fragments while replacing them in the HW descriptors */
  509. for (nr = 0; nr < priv->num_frags; nr++) {
  510. frag_info = &priv->frag_info[nr];
  511. if (length <= frag_info->frag_prefix_size)
  512. break;
  513. if (unlikely(!frags[nr].page))
  514. goto fail;
  515. dma = be64_to_cpu(rx_desc->data[nr].addr);
  516. dma_sync_single_for_cpu(priv->ddev, dma, frag_info->frag_size,
  517. DMA_FROM_DEVICE);
  518. /* Save page reference in skb */
  519. __skb_frag_set_page(&skb_frags_rx[nr], frags[nr].page);
  520. skb_frag_size_set(&skb_frags_rx[nr], frag_info->frag_size);
  521. skb_frags_rx[nr].page_offset = frags[nr].page_offset;
  522. skb->truesize += frag_info->frag_stride;
  523. frags[nr].page = NULL;
  524. }
  525. /* Adjust size of last fragment to match actual length */
  526. if (nr > 0)
  527. skb_frag_size_set(&skb_frags_rx[nr - 1],
  528. length - priv->frag_info[nr - 1].frag_prefix_size);
  529. return nr;
  530. fail:
  531. while (nr > 0) {
  532. nr--;
  533. __skb_frag_unref(&skb_frags_rx[nr]);
  534. }
  535. return 0;
  536. }
  537. static struct sk_buff *mlx4_en_rx_skb(struct mlx4_en_priv *priv,
  538. struct mlx4_en_rx_desc *rx_desc,
  539. struct mlx4_en_rx_alloc *frags,
  540. unsigned int length)
  541. {
  542. struct sk_buff *skb;
  543. void *va;
  544. int used_frags;
  545. dma_addr_t dma;
  546. skb = netdev_alloc_skb(priv->dev, SMALL_PACKET_SIZE + NET_IP_ALIGN);
  547. if (unlikely(!skb)) {
  548. en_dbg(RX_ERR, priv, "Failed allocating skb\n");
  549. return NULL;
  550. }
  551. skb_reserve(skb, NET_IP_ALIGN);
  552. skb->len = length;
  553. /* Get pointer to first fragment so we could copy the headers into the
  554. * (linear part of the) skb */
  555. va = page_address(frags[0].page) + frags[0].page_offset;
  556. if (length <= SMALL_PACKET_SIZE) {
  557. /* We are copying all relevant data to the skb - temporarily
  558. * sync buffers for the copy */
  559. dma = be64_to_cpu(rx_desc->data[0].addr);
  560. dma_sync_single_for_cpu(priv->ddev, dma, length,
  561. DMA_FROM_DEVICE);
  562. skb_copy_to_linear_data(skb, va, length);
  563. skb->tail += length;
  564. } else {
  565. unsigned int pull_len;
  566. /* Move relevant fragments to skb */
  567. used_frags = mlx4_en_complete_rx_desc(priv, rx_desc, frags,
  568. skb, length);
  569. if (unlikely(!used_frags)) {
  570. kfree_skb(skb);
  571. return NULL;
  572. }
  573. skb_shinfo(skb)->nr_frags = used_frags;
  574. pull_len = eth_get_headlen(va, SMALL_PACKET_SIZE);
  575. /* Copy headers into the skb linear buffer */
  576. memcpy(skb->data, va, pull_len);
  577. skb->tail += pull_len;
  578. /* Skip headers in first fragment */
  579. skb_shinfo(skb)->frags[0].page_offset += pull_len;
  580. /* Adjust size of first fragment */
  581. skb_frag_size_sub(&skb_shinfo(skb)->frags[0], pull_len);
  582. skb->data_len = length - pull_len;
  583. }
  584. return skb;
  585. }
  586. static void validate_loopback(struct mlx4_en_priv *priv, struct sk_buff *skb)
  587. {
  588. int i;
  589. int offset = ETH_HLEN;
  590. for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++, offset++) {
  591. if (*(skb->data + offset) != (unsigned char) (i & 0xff))
  592. goto out_loopback;
  593. }
  594. /* Loopback found */
  595. priv->loopback_ok = 1;
  596. out_loopback:
  597. dev_kfree_skb_any(skb);
  598. }
  599. static bool mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
  600. struct mlx4_en_rx_ring *ring)
  601. {
  602. u32 missing = ring->actual_size - (ring->prod - ring->cons);
  603. /* Try to batch allocations, but not too much. */
  604. if (missing < 8)
  605. return false;
  606. do {
  607. if (mlx4_en_prepare_rx_desc(priv, ring,
  608. ring->prod & ring->size_mask,
  609. GFP_ATOMIC | __GFP_COLD))
  610. break;
  611. ring->prod++;
  612. } while (--missing);
  613. return true;
  614. }
  615. /* When hardware doesn't strip the vlan, we need to calculate the checksum
  616. * over it and add it to the hardware's checksum calculation
  617. */
  618. static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
  619. struct vlan_hdr *vlanh)
  620. {
  621. return csum_add(hw_checksum, *(__wsum *)vlanh);
  622. }
  623. /* Although the stack expects checksum which doesn't include the pseudo
  624. * header, the HW adds it. To address that, we are subtracting the pseudo
  625. * header checksum from the checksum value provided by the HW.
  626. */
  627. static void get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
  628. struct iphdr *iph)
  629. {
  630. __u16 length_for_csum = 0;
  631. __wsum csum_pseudo_header = 0;
  632. length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
  633. csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  634. length_for_csum, iph->protocol, 0);
  635. skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
  636. }
  637. #if IS_ENABLED(CONFIG_IPV6)
  638. /* In IPv6 packets, besides subtracting the pseudo header checksum,
  639. * we also compute/add the IP header checksum which
  640. * is not added by the HW.
  641. */
  642. static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
  643. struct ipv6hdr *ipv6h)
  644. {
  645. __wsum csum_pseudo_hdr = 0;
  646. if (unlikely(ipv6h->nexthdr == IPPROTO_FRAGMENT ||
  647. ipv6h->nexthdr == IPPROTO_HOPOPTS))
  648. return -1;
  649. hw_checksum = csum_add(hw_checksum, (__force __wsum)htons(ipv6h->nexthdr));
  650. csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
  651. sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
  652. csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
  653. csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ntohs(ipv6h->nexthdr));
  654. skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
  655. skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
  656. return 0;
  657. }
  658. #endif
  659. static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  660. netdev_features_t dev_features)
  661. {
  662. __wsum hw_checksum = 0;
  663. void *hdr = (u8 *)va + sizeof(struct ethhdr);
  664. hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
  665. if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
  666. !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
  667. hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
  668. hdr += sizeof(struct vlan_hdr);
  669. }
  670. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
  671. get_fixed_ipv4_csum(hw_checksum, skb, hdr);
  672. #if IS_ENABLED(CONFIG_IPV6)
  673. else if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
  674. if (unlikely(get_fixed_ipv6_csum(hw_checksum, skb, hdr)))
  675. return -1;
  676. #endif
  677. return 0;
  678. }
  679. int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  680. {
  681. struct mlx4_en_priv *priv = netdev_priv(dev);
  682. struct mlx4_en_dev *mdev = priv->mdev;
  683. struct mlx4_cqe *cqe;
  684. struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
  685. struct mlx4_en_rx_alloc *frags;
  686. struct mlx4_en_rx_desc *rx_desc;
  687. struct bpf_prog *xdp_prog;
  688. int doorbell_pending;
  689. struct sk_buff *skb;
  690. int index;
  691. int nr;
  692. unsigned int length;
  693. int polled = 0;
  694. int ip_summed;
  695. int factor = priv->cqe_factor;
  696. u64 timestamp;
  697. bool l2_tunnel;
  698. if (unlikely(!priv->port_up))
  699. return 0;
  700. if (unlikely(budget <= 0))
  701. return polled;
  702. /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
  703. rcu_read_lock();
  704. xdp_prog = rcu_dereference(ring->xdp_prog);
  705. doorbell_pending = 0;
  706. /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
  707. * descriptor offset can be deduced from the CQE index instead of
  708. * reading 'cqe->index' */
  709. index = cq->mcq.cons_index & ring->size_mask;
  710. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  711. /* Process all completed CQEs */
  712. while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
  713. cq->mcq.cons_index & cq->size)) {
  714. frags = ring->rx_info + (index << priv->log_rx_info);
  715. rx_desc = ring->buf + (index << ring->log_stride);
  716. /*
  717. * make sure we read the CQE after we read the ownership bit
  718. */
  719. dma_rmb();
  720. /* Drop packet on bad receive or bad checksum */
  721. if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
  722. MLX4_CQE_OPCODE_ERROR)) {
  723. en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
  724. ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
  725. ((struct mlx4_err_cqe *)cqe)->syndrome);
  726. goto next;
  727. }
  728. if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
  729. en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
  730. goto next;
  731. }
  732. /* Check if we need to drop the packet if SRIOV is not enabled
  733. * and not performing the selftest or flb disabled
  734. */
  735. if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
  736. struct ethhdr *ethh;
  737. dma_addr_t dma;
  738. /* Get pointer to first fragment since we haven't
  739. * skb yet and cast it to ethhdr struct
  740. */
  741. dma = be64_to_cpu(rx_desc->data[0].addr);
  742. dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
  743. DMA_FROM_DEVICE);
  744. ethh = (struct ethhdr *)(page_address(frags[0].page) +
  745. frags[0].page_offset);
  746. if (is_multicast_ether_addr(ethh->h_dest)) {
  747. struct mlx4_mac_entry *entry;
  748. struct hlist_head *bucket;
  749. unsigned int mac_hash;
  750. /* Drop the packet, since HW loopback-ed it */
  751. mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
  752. bucket = &priv->mac_hash[mac_hash];
  753. hlist_for_each_entry_rcu(entry, bucket, hlist) {
  754. if (ether_addr_equal_64bits(entry->mac,
  755. ethh->h_source))
  756. goto next;
  757. }
  758. }
  759. }
  760. /*
  761. * Packet is OK - process it.
  762. */
  763. length = be32_to_cpu(cqe->byte_cnt);
  764. length -= ring->fcs_del;
  765. l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
  766. (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
  767. /* A bpf program gets first chance to drop the packet. It may
  768. * read bytes but not past the end of the frag.
  769. */
  770. if (xdp_prog) {
  771. struct xdp_buff xdp;
  772. dma_addr_t dma;
  773. void *orig_data;
  774. u32 act;
  775. dma = be64_to_cpu(rx_desc->data[0].addr);
  776. dma_sync_single_for_cpu(priv->ddev, dma,
  777. priv->frag_info[0].frag_size,
  778. DMA_FROM_DEVICE);
  779. xdp.data_hard_start = page_address(frags[0].page);
  780. xdp.data = xdp.data_hard_start + frags[0].page_offset;
  781. xdp.data_end = xdp.data + length;
  782. orig_data = xdp.data;
  783. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  784. if (xdp.data != orig_data) {
  785. length = xdp.data_end - xdp.data;
  786. frags[0].page_offset = xdp.data -
  787. xdp.data_hard_start;
  788. }
  789. switch (act) {
  790. case XDP_PASS:
  791. break;
  792. case XDP_TX:
  793. if (likely(!mlx4_en_xmit_frame(ring, frags, dev,
  794. length, cq->ring,
  795. &doorbell_pending)))
  796. goto consumed;
  797. goto xdp_drop_no_cnt; /* Drop on xmit failure */
  798. default:
  799. bpf_warn_invalid_xdp_action(act);
  800. case XDP_ABORTED:
  801. case XDP_DROP:
  802. ring->xdp_drop++;
  803. xdp_drop_no_cnt:
  804. if (likely(mlx4_en_rx_recycle(ring, frags)))
  805. goto consumed;
  806. goto next;
  807. }
  808. }
  809. ring->bytes += length;
  810. ring->packets++;
  811. if (likely(dev->features & NETIF_F_RXCSUM)) {
  812. if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
  813. MLX4_CQE_STATUS_UDP)) {
  814. if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
  815. cqe->checksum == cpu_to_be16(0xffff)) {
  816. ip_summed = CHECKSUM_UNNECESSARY;
  817. ring->csum_ok++;
  818. } else {
  819. ip_summed = CHECKSUM_NONE;
  820. ring->csum_none++;
  821. }
  822. } else {
  823. if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
  824. (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
  825. MLX4_CQE_STATUS_IPV6))) {
  826. ip_summed = CHECKSUM_COMPLETE;
  827. ring->csum_complete++;
  828. } else {
  829. ip_summed = CHECKSUM_NONE;
  830. ring->csum_none++;
  831. }
  832. }
  833. } else {
  834. ip_summed = CHECKSUM_NONE;
  835. ring->csum_none++;
  836. }
  837. /* This packet is eligible for GRO if it is:
  838. * - DIX Ethernet (type interpretation)
  839. * - TCP/IP (v4)
  840. * - without IP options
  841. * - not an IP fragment
  842. */
  843. if (dev->features & NETIF_F_GRO) {
  844. struct sk_buff *gro_skb = napi_get_frags(&cq->napi);
  845. if (!gro_skb)
  846. goto next;
  847. nr = mlx4_en_complete_rx_desc(priv,
  848. rx_desc, frags, gro_skb,
  849. length);
  850. if (!nr)
  851. goto next;
  852. if (ip_summed == CHECKSUM_COMPLETE) {
  853. void *va = skb_frag_address(skb_shinfo(gro_skb)->frags);
  854. if (check_csum(cqe, gro_skb, va,
  855. dev->features)) {
  856. ip_summed = CHECKSUM_NONE;
  857. ring->csum_none++;
  858. ring->csum_complete--;
  859. }
  860. }
  861. skb_shinfo(gro_skb)->nr_frags = nr;
  862. gro_skb->len = length;
  863. gro_skb->data_len = length;
  864. gro_skb->ip_summed = ip_summed;
  865. if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
  866. gro_skb->csum_level = 1;
  867. if ((cqe->vlan_my_qpn &
  868. cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
  869. (dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  870. u16 vid = be16_to_cpu(cqe->sl_vid);
  871. __vlan_hwaccel_put_tag(gro_skb, htons(ETH_P_8021Q), vid);
  872. } else if ((be32_to_cpu(cqe->vlan_my_qpn) &
  873. MLX4_CQE_SVLAN_PRESENT_MASK) &&
  874. (dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
  875. __vlan_hwaccel_put_tag(gro_skb,
  876. htons(ETH_P_8021AD),
  877. be16_to_cpu(cqe->sl_vid));
  878. }
  879. if (dev->features & NETIF_F_RXHASH)
  880. skb_set_hash(gro_skb,
  881. be32_to_cpu(cqe->immed_rss_invalid),
  882. (ip_summed == CHECKSUM_UNNECESSARY) ?
  883. PKT_HASH_TYPE_L4 :
  884. PKT_HASH_TYPE_L3);
  885. skb_record_rx_queue(gro_skb, cq->ring);
  886. if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
  887. timestamp = mlx4_en_get_cqe_ts(cqe);
  888. mlx4_en_fill_hwtstamps(mdev,
  889. skb_hwtstamps(gro_skb),
  890. timestamp);
  891. }
  892. napi_gro_frags(&cq->napi);
  893. goto next;
  894. }
  895. /* GRO not possible, complete processing here */
  896. skb = mlx4_en_rx_skb(priv, rx_desc, frags, length);
  897. if (unlikely(!skb)) {
  898. ring->dropped++;
  899. goto next;
  900. }
  901. if (unlikely(priv->validate_loopback)) {
  902. validate_loopback(priv, skb);
  903. goto next;
  904. }
  905. if (ip_summed == CHECKSUM_COMPLETE) {
  906. if (check_csum(cqe, skb, skb->data, dev->features)) {
  907. ip_summed = CHECKSUM_NONE;
  908. ring->csum_complete--;
  909. ring->csum_none++;
  910. }
  911. }
  912. skb->ip_summed = ip_summed;
  913. skb->protocol = eth_type_trans(skb, dev);
  914. skb_record_rx_queue(skb, cq->ring);
  915. if (l2_tunnel && ip_summed == CHECKSUM_UNNECESSARY)
  916. skb->csum_level = 1;
  917. if (dev->features & NETIF_F_RXHASH)
  918. skb_set_hash(skb,
  919. be32_to_cpu(cqe->immed_rss_invalid),
  920. (ip_summed == CHECKSUM_UNNECESSARY) ?
  921. PKT_HASH_TYPE_L4 :
  922. PKT_HASH_TYPE_L3);
  923. if ((be32_to_cpu(cqe->vlan_my_qpn) &
  924. MLX4_CQE_CVLAN_PRESENT_MASK) &&
  925. (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
  926. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(cqe->sl_vid));
  927. else if ((be32_to_cpu(cqe->vlan_my_qpn) &
  928. MLX4_CQE_SVLAN_PRESENT_MASK) &&
  929. (dev->features & NETIF_F_HW_VLAN_STAG_RX))
  930. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
  931. be16_to_cpu(cqe->sl_vid));
  932. if (ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL) {
  933. timestamp = mlx4_en_get_cqe_ts(cqe);
  934. mlx4_en_fill_hwtstamps(mdev, skb_hwtstamps(skb),
  935. timestamp);
  936. }
  937. napi_gro_receive(&cq->napi, skb);
  938. next:
  939. for (nr = 0; nr < priv->num_frags; nr++)
  940. mlx4_en_free_frag(priv, frags, nr);
  941. consumed:
  942. ++cq->mcq.cons_index;
  943. index = (cq->mcq.cons_index) & ring->size_mask;
  944. cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
  945. if (++polled == budget)
  946. goto out;
  947. }
  948. out:
  949. rcu_read_unlock();
  950. if (polled) {
  951. if (doorbell_pending)
  952. mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq->ring]);
  953. mlx4_cq_set_ci(&cq->mcq);
  954. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  955. ring->cons = cq->mcq.cons_index;
  956. }
  957. AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
  958. if (mlx4_en_refill_rx_buffers(priv, ring))
  959. mlx4_en_update_rx_prod_db(ring);
  960. return polled;
  961. }
  962. void mlx4_en_rx_irq(struct mlx4_cq *mcq)
  963. {
  964. struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
  965. struct mlx4_en_priv *priv = netdev_priv(cq->dev);
  966. if (likely(priv->port_up))
  967. napi_schedule_irqoff(&cq->napi);
  968. else
  969. mlx4_en_arm_cq(priv, cq);
  970. }
  971. /* Rx CQ polling - called by NAPI */
  972. int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
  973. {
  974. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  975. struct net_device *dev = cq->dev;
  976. struct mlx4_en_priv *priv = netdev_priv(dev);
  977. int done;
  978. done = mlx4_en_process_rx_cq(dev, cq, budget);
  979. /* If we used up all the quota - we're probably not done yet... */
  980. if (done == budget) {
  981. const struct cpumask *aff;
  982. struct irq_data *idata;
  983. int cpu_curr;
  984. INC_PERF_COUNTER(priv->pstats.napi_quota);
  985. cpu_curr = smp_processor_id();
  986. idata = irq_desc_get_irq_data(cq->irq_desc);
  987. aff = irq_data_get_affinity_mask(idata);
  988. if (likely(cpumask_test_cpu(cpu_curr, aff)))
  989. return budget;
  990. /* Current cpu is not according to smp_irq_affinity -
  991. * probably affinity changed. Need to stop this NAPI
  992. * poll, and restart it on the right CPU.
  993. * Try to avoid returning a too small value (like 0),
  994. * to not fool net_rx_action() and its netdev_budget
  995. */
  996. if (done)
  997. done--;
  998. }
  999. /* Done for now */
  1000. if (napi_complete_done(napi, done))
  1001. mlx4_en_arm_cq(priv, cq);
  1002. return done;
  1003. }
  1004. static const int frag_sizes[] = {
  1005. FRAG_SZ0,
  1006. FRAG_SZ1,
  1007. FRAG_SZ2,
  1008. FRAG_SZ3
  1009. };
  1010. void mlx4_en_calc_rx_buf(struct net_device *dev)
  1011. {
  1012. struct mlx4_en_priv *priv = netdev_priv(dev);
  1013. int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
  1014. int i = 0;
  1015. /* bpf requires buffers to be set up as 1 packet per page.
  1016. * This only works when num_frags == 1.
  1017. */
  1018. if (priv->tx_ring_num[TX_XDP]) {
  1019. priv->frag_info[0].order = 0;
  1020. priv->frag_info[0].frag_size = eff_mtu;
  1021. priv->frag_info[0].frag_prefix_size = 0;
  1022. /* This will gain efficient xdp frame recycling at the
  1023. * expense of more costly truesize accounting
  1024. */
  1025. priv->frag_info[0].frag_stride = PAGE_SIZE;
  1026. priv->frag_info[0].dma_dir = PCI_DMA_BIDIRECTIONAL;
  1027. priv->frag_info[0].rx_headroom = XDP_PACKET_HEADROOM;
  1028. i = 1;
  1029. } else {
  1030. int buf_size = 0;
  1031. while (buf_size < eff_mtu) {
  1032. priv->frag_info[i].order = MLX4_EN_ALLOC_PREFER_ORDER;
  1033. priv->frag_info[i].frag_size =
  1034. (eff_mtu > buf_size + frag_sizes[i]) ?
  1035. frag_sizes[i] : eff_mtu - buf_size;
  1036. priv->frag_info[i].frag_prefix_size = buf_size;
  1037. priv->frag_info[i].frag_stride =
  1038. ALIGN(priv->frag_info[i].frag_size,
  1039. SMP_CACHE_BYTES);
  1040. priv->frag_info[i].dma_dir = PCI_DMA_FROMDEVICE;
  1041. priv->frag_info[i].rx_headroom = 0;
  1042. buf_size += priv->frag_info[i].frag_size;
  1043. i++;
  1044. }
  1045. }
  1046. priv->num_frags = i;
  1047. priv->rx_skb_size = eff_mtu;
  1048. priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
  1049. en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
  1050. eff_mtu, priv->num_frags);
  1051. for (i = 0; i < priv->num_frags; i++) {
  1052. en_err(priv,
  1053. " frag:%d - size:%d prefix:%d stride:%d\n",
  1054. i,
  1055. priv->frag_info[i].frag_size,
  1056. priv->frag_info[i].frag_prefix_size,
  1057. priv->frag_info[i].frag_stride);
  1058. }
  1059. }
  1060. /* RSS related functions */
  1061. static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
  1062. struct mlx4_en_rx_ring *ring,
  1063. enum mlx4_qp_state *state,
  1064. struct mlx4_qp *qp)
  1065. {
  1066. struct mlx4_en_dev *mdev = priv->mdev;
  1067. struct mlx4_qp_context *context;
  1068. int err = 0;
  1069. context = kmalloc(sizeof(*context), GFP_KERNEL);
  1070. if (!context)
  1071. return -ENOMEM;
  1072. err = mlx4_qp_alloc(mdev->dev, qpn, qp, GFP_KERNEL);
  1073. if (err) {
  1074. en_err(priv, "Failed to allocate qp #%x\n", qpn);
  1075. goto out;
  1076. }
  1077. qp->event = mlx4_en_sqp_event;
  1078. memset(context, 0, sizeof *context);
  1079. mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
  1080. qpn, ring->cqn, -1, context);
  1081. context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
  1082. /* Cancel FCS removal if FW allows */
  1083. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
  1084. context->param3 |= cpu_to_be32(1 << 29);
  1085. if (priv->dev->features & NETIF_F_RXFCS)
  1086. ring->fcs_del = 0;
  1087. else
  1088. ring->fcs_del = ETH_FCS_LEN;
  1089. } else
  1090. ring->fcs_del = 0;
  1091. err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
  1092. if (err) {
  1093. mlx4_qp_remove(mdev->dev, qp);
  1094. mlx4_qp_free(mdev->dev, qp);
  1095. }
  1096. mlx4_en_update_rx_prod_db(ring);
  1097. out:
  1098. kfree(context);
  1099. return err;
  1100. }
  1101. int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
  1102. {
  1103. int err;
  1104. u32 qpn;
  1105. err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
  1106. MLX4_RESERVE_A0_QP);
  1107. if (err) {
  1108. en_err(priv, "Failed reserving drop qpn\n");
  1109. return err;
  1110. }
  1111. err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp, GFP_KERNEL);
  1112. if (err) {
  1113. en_err(priv, "Failed allocating drop qp\n");
  1114. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  1115. return err;
  1116. }
  1117. return 0;
  1118. }
  1119. void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
  1120. {
  1121. u32 qpn;
  1122. qpn = priv->drop_qp.qpn;
  1123. mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
  1124. mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
  1125. mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
  1126. }
  1127. /* Allocate rx qp's and configure them according to rss map */
  1128. int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
  1129. {
  1130. struct mlx4_en_dev *mdev = priv->mdev;
  1131. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  1132. struct mlx4_qp_context context;
  1133. struct mlx4_rss_context *rss_context;
  1134. int rss_rings;
  1135. void *ptr;
  1136. u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
  1137. MLX4_RSS_TCP_IPV6);
  1138. int i, qpn;
  1139. int err = 0;
  1140. int good_qps = 0;
  1141. en_dbg(DRV, priv, "Configuring rss steering\n");
  1142. err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
  1143. priv->rx_ring_num,
  1144. &rss_map->base_qpn, 0);
  1145. if (err) {
  1146. en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
  1147. return err;
  1148. }
  1149. for (i = 0; i < priv->rx_ring_num; i++) {
  1150. qpn = rss_map->base_qpn + i;
  1151. err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
  1152. &rss_map->state[i],
  1153. &rss_map->qps[i]);
  1154. if (err)
  1155. goto rss_err;
  1156. ++good_qps;
  1157. }
  1158. /* Configure RSS indirection qp */
  1159. err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, &rss_map->indir_qp, GFP_KERNEL);
  1160. if (err) {
  1161. en_err(priv, "Failed to allocate RSS indirection QP\n");
  1162. goto rss_err;
  1163. }
  1164. rss_map->indir_qp.event = mlx4_en_sqp_event;
  1165. mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
  1166. priv->rx_ring[0]->cqn, -1, &context);
  1167. if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
  1168. rss_rings = priv->rx_ring_num;
  1169. else
  1170. rss_rings = priv->prof->rss_rings;
  1171. ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
  1172. + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  1173. rss_context = ptr;
  1174. rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
  1175. (rss_map->base_qpn));
  1176. rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
  1177. if (priv->mdev->profile.udp_rss) {
  1178. rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
  1179. rss_context->base_qpn_udp = rss_context->default_qpn;
  1180. }
  1181. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1182. en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
  1183. rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
  1184. }
  1185. rss_context->flags = rss_mask;
  1186. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1187. if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
  1188. rss_context->hash_fn = MLX4_RSS_HASH_XOR;
  1189. } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
  1190. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1191. memcpy(rss_context->rss_key, priv->rss_key,
  1192. MLX4_EN_RSS_KEY_SIZE);
  1193. } else {
  1194. en_err(priv, "Unknown RSS hash function requested\n");
  1195. err = -EINVAL;
  1196. goto indir_err;
  1197. }
  1198. err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
  1199. &rss_map->indir_qp, &rss_map->indir_state);
  1200. if (err)
  1201. goto indir_err;
  1202. return 0;
  1203. indir_err:
  1204. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1205. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
  1206. mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
  1207. mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
  1208. rss_err:
  1209. for (i = 0; i < good_qps; i++) {
  1210. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1211. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1212. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1213. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1214. }
  1215. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1216. return err;
  1217. }
  1218. void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
  1219. {
  1220. struct mlx4_en_dev *mdev = priv->mdev;
  1221. struct mlx4_en_rss_map *rss_map = &priv->rss_map;
  1222. int i;
  1223. mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
  1224. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->indir_qp);
  1225. mlx4_qp_remove(mdev->dev, &rss_map->indir_qp);
  1226. mlx4_qp_free(mdev->dev, &rss_map->indir_qp);
  1227. for (i = 0; i < priv->rx_ring_num; i++) {
  1228. mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
  1229. MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
  1230. mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
  1231. mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
  1232. }
  1233. mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
  1234. }