en_rx.c 35 KB

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