en_rx.c 39 KB

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