en_rx.c 40 KB

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