rx_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2018 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include "net_driver.h"
  11. #include <linux/module.h>
  12. #include <linux/iommu.h>
  13. #include <net/rps.h>
  14. #include "efx.h"
  15. #include "nic.h"
  16. #include "rx_common.h"
  17. /* This is the percentage fill level below which new RX descriptors
  18. * will be added to the RX descriptor ring.
  19. */
  20. static unsigned int rx_refill_threshold;
  21. module_param(rx_refill_threshold, uint, 0444);
  22. MODULE_PARM_DESC(rx_refill_threshold,
  23. "RX descriptor ring refill threshold (%)");
  24. /* RX maximum head room required.
  25. *
  26. * This must be at least 1 to prevent overflow, plus one packet-worth
  27. * to allow pipelined receives.
  28. */
  29. #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
  30. /* Check the RX page recycle ring for a page that can be reused. */
  31. static struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
  32. {
  33. struct efx_nic *efx = rx_queue->efx;
  34. struct efx_rx_page_state *state;
  35. unsigned int index;
  36. struct page *page;
  37. if (unlikely(!rx_queue->page_ring))
  38. return NULL;
  39. index = rx_queue->page_remove & rx_queue->page_ptr_mask;
  40. page = rx_queue->page_ring[index];
  41. if (page == NULL)
  42. return NULL;
  43. rx_queue->page_ring[index] = NULL;
  44. /* page_remove cannot exceed page_add. */
  45. if (rx_queue->page_remove != rx_queue->page_add)
  46. ++rx_queue->page_remove;
  47. /* If page_count is 1 then we hold the only reference to this page. */
  48. if (page_count(page) == 1) {
  49. ++rx_queue->page_recycle_count;
  50. return page;
  51. } else {
  52. state = page_address(page);
  53. dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
  54. PAGE_SIZE << efx->rx_buffer_order,
  55. DMA_FROM_DEVICE);
  56. put_page(page);
  57. ++rx_queue->page_recycle_failed;
  58. }
  59. return NULL;
  60. }
  61. /* Attempt to recycle the page if there is an RX recycle ring; the page can
  62. * only be added if this is the final RX buffer, to prevent pages being used in
  63. * the descriptor ring and appearing in the recycle ring simultaneously.
  64. */
  65. static void efx_recycle_rx_page(struct efx_channel *channel,
  66. struct efx_rx_buffer *rx_buf)
  67. {
  68. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  69. struct efx_nic *efx = rx_queue->efx;
  70. struct page *page = rx_buf->page;
  71. unsigned int index;
  72. /* Only recycle the page after processing the final buffer. */
  73. if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
  74. return;
  75. index = rx_queue->page_add & rx_queue->page_ptr_mask;
  76. if (rx_queue->page_ring[index] == NULL) {
  77. unsigned int read_index = rx_queue->page_remove &
  78. rx_queue->page_ptr_mask;
  79. /* The next slot in the recycle ring is available, but
  80. * increment page_remove if the read pointer currently
  81. * points here.
  82. */
  83. if (read_index == index)
  84. ++rx_queue->page_remove;
  85. rx_queue->page_ring[index] = page;
  86. ++rx_queue->page_add;
  87. return;
  88. }
  89. ++rx_queue->page_recycle_full;
  90. efx_unmap_rx_buffer(efx, rx_buf);
  91. put_page(rx_buf->page);
  92. }
  93. /* Recycle the pages that are used by buffers that have just been received. */
  94. void efx_recycle_rx_pages(struct efx_channel *channel,
  95. struct efx_rx_buffer *rx_buf,
  96. unsigned int n_frags)
  97. {
  98. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  99. if (unlikely(!rx_queue->page_ring))
  100. return;
  101. do {
  102. efx_recycle_rx_page(channel, rx_buf);
  103. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  104. } while (--n_frags);
  105. }
  106. void efx_discard_rx_packet(struct efx_channel *channel,
  107. struct efx_rx_buffer *rx_buf,
  108. unsigned int n_frags)
  109. {
  110. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  111. efx_recycle_rx_pages(channel, rx_buf, n_frags);
  112. efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
  113. }
  114. static void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
  115. {
  116. unsigned int bufs_in_recycle_ring, page_ring_size;
  117. struct efx_nic *efx = rx_queue->efx;
  118. bufs_in_recycle_ring = efx_rx_recycle_ring_size(efx);
  119. page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
  120. efx->rx_bufs_per_page);
  121. rx_queue->page_ring = kcalloc(page_ring_size,
  122. sizeof(*rx_queue->page_ring), GFP_KERNEL);
  123. if (!rx_queue->page_ring)
  124. rx_queue->page_ptr_mask = 0;
  125. else
  126. rx_queue->page_ptr_mask = page_ring_size - 1;
  127. }
  128. static void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
  129. {
  130. struct efx_nic *efx = rx_queue->efx;
  131. int i;
  132. if (unlikely(!rx_queue->page_ring))
  133. return;
  134. /* Unmap and release the pages in the recycle ring. Remove the ring. */
  135. for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
  136. struct page *page = rx_queue->page_ring[i];
  137. struct efx_rx_page_state *state;
  138. if (page == NULL)
  139. continue;
  140. state = page_address(page);
  141. dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
  142. PAGE_SIZE << efx->rx_buffer_order,
  143. DMA_FROM_DEVICE);
  144. put_page(page);
  145. }
  146. kfree(rx_queue->page_ring);
  147. rx_queue->page_ring = NULL;
  148. }
  149. static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  150. struct efx_rx_buffer *rx_buf)
  151. {
  152. /* Release the page reference we hold for the buffer. */
  153. if (rx_buf->page)
  154. put_page(rx_buf->page);
  155. /* If this is the last buffer in a page, unmap and free it. */
  156. if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
  157. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  158. efx_free_rx_buffers(rx_queue, rx_buf, 1);
  159. }
  160. rx_buf->page = NULL;
  161. }
  162. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  163. {
  164. struct efx_nic *efx = rx_queue->efx;
  165. unsigned int entries;
  166. int rc;
  167. /* Create the smallest power-of-two aligned ring */
  168. entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
  169. EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  170. rx_queue->ptr_mask = entries - 1;
  171. netif_dbg(efx, probe, efx->net_dev,
  172. "creating RX queue %d size %#x mask %#x\n",
  173. efx_rx_queue_index(rx_queue), efx->rxq_entries,
  174. rx_queue->ptr_mask);
  175. /* Allocate RX buffers */
  176. rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
  177. GFP_KERNEL);
  178. if (!rx_queue->buffer)
  179. return -ENOMEM;
  180. rc = efx_nic_probe_rx(rx_queue);
  181. if (rc) {
  182. kfree(rx_queue->buffer);
  183. rx_queue->buffer = NULL;
  184. }
  185. return rc;
  186. }
  187. void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  188. {
  189. unsigned int max_fill, trigger, max_trigger;
  190. struct efx_nic *efx = rx_queue->efx;
  191. int rc = 0;
  192. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  193. "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
  194. /* Initialise ptr fields */
  195. rx_queue->added_count = 0;
  196. rx_queue->notified_count = 0;
  197. rx_queue->granted_count = 0;
  198. rx_queue->removed_count = 0;
  199. rx_queue->min_fill = -1U;
  200. efx_init_rx_recycle_ring(rx_queue);
  201. rx_queue->page_remove = 0;
  202. rx_queue->page_add = rx_queue->page_ptr_mask + 1;
  203. rx_queue->page_recycle_count = 0;
  204. rx_queue->page_recycle_failed = 0;
  205. rx_queue->page_recycle_full = 0;
  206. /* Initialise limit fields */
  207. max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
  208. max_trigger =
  209. max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  210. if (rx_refill_threshold != 0) {
  211. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  212. if (trigger > max_trigger)
  213. trigger = max_trigger;
  214. } else {
  215. trigger = max_trigger;
  216. }
  217. rx_queue->max_fill = max_fill;
  218. rx_queue->fast_fill_trigger = trigger;
  219. rx_queue->refill_enabled = true;
  220. /* Initialise XDP queue information */
  221. rc = xdp_rxq_info_reg(&rx_queue->xdp_rxq_info, efx->net_dev,
  222. rx_queue->core_index, 0);
  223. if (rc) {
  224. netif_err(efx, rx_err, efx->net_dev,
  225. "Failure to initialise XDP queue information rc=%d\n",
  226. rc);
  227. efx->xdp_rxq_info_failed = true;
  228. } else {
  229. rx_queue->xdp_rxq_info_valid = true;
  230. }
  231. /* Set up RX descriptor ring */
  232. efx_nic_init_rx(rx_queue);
  233. }
  234. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  235. {
  236. struct efx_rx_buffer *rx_buf;
  237. int i;
  238. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  239. "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
  240. del_timer_sync(&rx_queue->slow_fill);
  241. if (rx_queue->grant_credits)
  242. flush_work(&rx_queue->grant_work);
  243. /* Release RX buffers from the current read ptr to the write ptr */
  244. if (rx_queue->buffer) {
  245. for (i = rx_queue->removed_count; i < rx_queue->added_count;
  246. i++) {
  247. unsigned int index = i & rx_queue->ptr_mask;
  248. rx_buf = efx_rx_buffer(rx_queue, index);
  249. efx_fini_rx_buffer(rx_queue, rx_buf);
  250. }
  251. }
  252. efx_fini_rx_recycle_ring(rx_queue);
  253. if (rx_queue->xdp_rxq_info_valid)
  254. xdp_rxq_info_unreg(&rx_queue->xdp_rxq_info);
  255. rx_queue->xdp_rxq_info_valid = false;
  256. }
  257. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  258. {
  259. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  260. "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
  261. efx_nic_remove_rx(rx_queue);
  262. kfree(rx_queue->buffer);
  263. rx_queue->buffer = NULL;
  264. }
  265. /* Unmap a DMA-mapped page. This function is only called for the final RX
  266. * buffer in a page.
  267. */
  268. void efx_unmap_rx_buffer(struct efx_nic *efx,
  269. struct efx_rx_buffer *rx_buf)
  270. {
  271. struct page *page = rx_buf->page;
  272. if (page) {
  273. struct efx_rx_page_state *state = page_address(page);
  274. dma_unmap_page(&efx->pci_dev->dev,
  275. state->dma_addr,
  276. PAGE_SIZE << efx->rx_buffer_order,
  277. DMA_FROM_DEVICE);
  278. }
  279. }
  280. void efx_free_rx_buffers(struct efx_rx_queue *rx_queue,
  281. struct efx_rx_buffer *rx_buf,
  282. unsigned int num_bufs)
  283. {
  284. do {
  285. if (rx_buf->page) {
  286. put_page(rx_buf->page);
  287. rx_buf->page = NULL;
  288. }
  289. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  290. } while (--num_bufs);
  291. }
  292. void efx_rx_slow_fill(struct timer_list *t)
  293. {
  294. struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
  295. /* Post an event to cause NAPI to run and refill the queue */
  296. efx_nic_generate_fill_event(rx_queue);
  297. ++rx_queue->slow_fill_count;
  298. }
  299. void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
  300. {
  301. mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
  302. }
  303. /* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
  304. *
  305. * @rx_queue: Efx RX queue
  306. *
  307. * This allocates a batch of pages, maps them for DMA, and populates
  308. * struct efx_rx_buffers for each one. Return a negative error code or
  309. * 0 on success. If a single page can be used for multiple buffers,
  310. * then the page will either be inserted fully, or not at all.
  311. */
  312. static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
  313. {
  314. unsigned int page_offset, index, count;
  315. struct efx_nic *efx = rx_queue->efx;
  316. struct efx_rx_page_state *state;
  317. struct efx_rx_buffer *rx_buf;
  318. dma_addr_t dma_addr;
  319. struct page *page;
  320. count = 0;
  321. do {
  322. page = efx_reuse_page(rx_queue);
  323. if (page == NULL) {
  324. page = alloc_pages(__GFP_COMP |
  325. (atomic ? GFP_ATOMIC : GFP_KERNEL),
  326. efx->rx_buffer_order);
  327. if (unlikely(page == NULL))
  328. return -ENOMEM;
  329. dma_addr =
  330. dma_map_page(&efx->pci_dev->dev, page, 0,
  331. PAGE_SIZE << efx->rx_buffer_order,
  332. DMA_FROM_DEVICE);
  333. if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
  334. dma_addr))) {
  335. __free_pages(page, efx->rx_buffer_order);
  336. return -EIO;
  337. }
  338. state = page_address(page);
  339. state->dma_addr = dma_addr;
  340. } else {
  341. state = page_address(page);
  342. dma_addr = state->dma_addr;
  343. }
  344. dma_addr += sizeof(struct efx_rx_page_state);
  345. page_offset = sizeof(struct efx_rx_page_state);
  346. do {
  347. index = rx_queue->added_count & rx_queue->ptr_mask;
  348. rx_buf = efx_rx_buffer(rx_queue, index);
  349. rx_buf->dma_addr = dma_addr + efx->rx_ip_align +
  350. EFX_XDP_HEADROOM;
  351. rx_buf->page = page;
  352. rx_buf->page_offset = page_offset + efx->rx_ip_align +
  353. EFX_XDP_HEADROOM;
  354. rx_buf->len = efx->rx_dma_len;
  355. rx_buf->flags = 0;
  356. ++rx_queue->added_count;
  357. get_page(page);
  358. dma_addr += efx->rx_page_buf_step;
  359. page_offset += efx->rx_page_buf_step;
  360. } while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
  361. rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
  362. } while (++count < efx->rx_pages_per_batch);
  363. return 0;
  364. }
  365. void efx_rx_config_page_split(struct efx_nic *efx)
  366. {
  367. efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align +
  368. EFX_XDP_HEADROOM + EFX_XDP_TAILROOM,
  369. EFX_RX_BUF_ALIGNMENT);
  370. efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
  371. ((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
  372. efx->rx_page_buf_step);
  373. efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
  374. efx->rx_bufs_per_page;
  375. efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
  376. efx->rx_bufs_per_page);
  377. }
  378. /* efx_fast_push_rx_descriptors - push new RX descriptors quickly
  379. * @rx_queue: RX descriptor queue
  380. *
  381. * This will aim to fill the RX descriptor queue up to
  382. * @rx_queue->@max_fill. If there is insufficient atomic
  383. * memory to do so, a slow fill will be scheduled.
  384. *
  385. * The caller must provide serialisation (none is used here). In practise,
  386. * this means this function must run from the NAPI handler, or be called
  387. * when NAPI is disabled.
  388. */
  389. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
  390. {
  391. struct efx_nic *efx = rx_queue->efx;
  392. unsigned int fill_level, batch_size;
  393. int space, rc = 0;
  394. if (!rx_queue->refill_enabled)
  395. return;
  396. /* Calculate current fill level, and exit if we don't need to fill */
  397. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  398. EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
  399. if (fill_level >= rx_queue->fast_fill_trigger)
  400. goto out;
  401. /* Record minimum fill level */
  402. if (unlikely(fill_level < rx_queue->min_fill)) {
  403. if (fill_level)
  404. rx_queue->min_fill = fill_level;
  405. }
  406. batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  407. space = rx_queue->max_fill - fill_level;
  408. EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
  409. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  410. "RX queue %d fast-filling descriptor ring from"
  411. " level %d to level %d\n",
  412. efx_rx_queue_index(rx_queue), fill_level,
  413. rx_queue->max_fill);
  414. do {
  415. rc = efx_init_rx_buffers(rx_queue, atomic);
  416. if (unlikely(rc)) {
  417. /* Ensure that we don't leave the rx queue empty */
  418. efx_schedule_slow_fill(rx_queue);
  419. goto out;
  420. }
  421. } while ((space -= batch_size) >= batch_size);
  422. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  423. "RX queue %d fast-filled descriptor ring "
  424. "to level %d\n", efx_rx_queue_index(rx_queue),
  425. rx_queue->added_count - rx_queue->removed_count);
  426. out:
  427. if (rx_queue->notified_count != rx_queue->added_count)
  428. efx_nic_notify_rx_desc(rx_queue);
  429. }
  430. /* Pass a received packet up through GRO. GRO can handle pages
  431. * regardless of checksum state and skbs with a good checksum.
  432. */
  433. void
  434. efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
  435. unsigned int n_frags, u8 *eh, __wsum csum)
  436. {
  437. struct napi_struct *napi = &channel->napi_str;
  438. struct efx_nic *efx = channel->efx;
  439. struct sk_buff *skb;
  440. skb = napi_get_frags(napi);
  441. if (unlikely(!skb)) {
  442. struct efx_rx_queue *rx_queue;
  443. rx_queue = efx_channel_get_rx_queue(channel);
  444. efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
  445. return;
  446. }
  447. if (efx->net_dev->features & NETIF_F_RXHASH &&
  448. efx_rx_buf_hash_valid(efx, eh))
  449. skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
  450. PKT_HASH_TYPE_L3);
  451. if (csum) {
  452. skb->csum = csum;
  453. skb->ip_summed = CHECKSUM_COMPLETE;
  454. } else {
  455. skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
  456. CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  457. }
  458. skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
  459. for (;;) {
  460. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  461. rx_buf->page, rx_buf->page_offset,
  462. rx_buf->len);
  463. rx_buf->page = NULL;
  464. skb->len += rx_buf->len;
  465. if (skb_shinfo(skb)->nr_frags == n_frags)
  466. break;
  467. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  468. }
  469. skb->data_len = skb->len;
  470. skb->truesize += n_frags * efx->rx_buffer_truesize;
  471. skb_record_rx_queue(skb, channel->rx_queue.core_index);
  472. napi_gro_frags(napi);
  473. }
  474. struct efx_rss_context_priv *efx_find_rss_context_entry(struct efx_nic *efx,
  475. u32 id)
  476. {
  477. struct ethtool_rxfh_context *ctx;
  478. WARN_ON(!mutex_is_locked(&efx->net_dev->ethtool->rss_lock));
  479. ctx = xa_load(&efx->net_dev->ethtool->rss_ctx, id);
  480. if (!ctx)
  481. return NULL;
  482. return ethtool_rxfh_context_priv(ctx);
  483. }
  484. void efx_set_default_rx_indir_table(struct efx_nic *efx, u32 *indir)
  485. {
  486. size_t i;
  487. for (i = 0; i < ARRAY_SIZE(efx->rss_context.rx_indir_table); i++)
  488. indir[i] = ethtool_rxfh_indir_default(i, efx->rss_spread);
  489. }
  490. /**
  491. * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
  492. * @spec: Specification to test
  493. *
  494. * Return: %true if the specification is a non-drop RX filter that
  495. * matches a local MAC address I/G bit value of 1 or matches a local
  496. * IPv4 or IPv6 address value in the respective multicast address
  497. * range. Otherwise %false.
  498. */
  499. bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
  500. {
  501. if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
  502. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
  503. return false;
  504. if (spec->match_flags &
  505. (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
  506. is_multicast_ether_addr(spec->loc_mac))
  507. return true;
  508. if ((spec->match_flags &
  509. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  510. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  511. if (spec->ether_type == htons(ETH_P_IP) &&
  512. ipv4_is_multicast(spec->loc_host[0]))
  513. return true;
  514. if (spec->ether_type == htons(ETH_P_IPV6) &&
  515. ((const u8 *)spec->loc_host)[0] == 0xff)
  516. return true;
  517. }
  518. return false;
  519. }
  520. bool efx_filter_spec_equal(const struct efx_filter_spec *left,
  521. const struct efx_filter_spec *right)
  522. {
  523. if ((left->match_flags ^ right->match_flags) |
  524. ((left->flags ^ right->flags) &
  525. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
  526. return false;
  527. return memcmp(&left->vport_id, &right->vport_id,
  528. sizeof(struct efx_filter_spec) -
  529. offsetof(struct efx_filter_spec, vport_id)) == 0;
  530. }
  531. u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
  532. {
  533. BUILD_BUG_ON(offsetof(struct efx_filter_spec, vport_id) & 3);
  534. return jhash2((const u32 *)&spec->vport_id,
  535. (sizeof(struct efx_filter_spec) -
  536. offsetof(struct efx_filter_spec, vport_id)) / 4,
  537. 0);
  538. }
  539. #ifdef CONFIG_RFS_ACCEL
  540. bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
  541. bool *force)
  542. {
  543. if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
  544. /* ARFS is currently updating this entry, leave it */
  545. return false;
  546. }
  547. if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
  548. /* ARFS tried and failed to update this, so it's probably out
  549. * of date. Remove the filter and the ARFS rule entry.
  550. */
  551. rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
  552. *force = true;
  553. return true;
  554. } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
  555. /* ARFS has moved on, so old filter is not needed. Since we did
  556. * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
  557. * not be removed by efx_rps_hash_del() subsequently.
  558. */
  559. *force = true;
  560. return true;
  561. }
  562. /* Remove it iff ARFS wants to. */
  563. return true;
  564. }
  565. static
  566. struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
  567. const struct efx_filter_spec *spec)
  568. {
  569. u32 hash = efx_filter_spec_hash(spec);
  570. lockdep_assert_held(&efx->rps_hash_lock);
  571. if (!efx->rps_hash_table)
  572. return NULL;
  573. return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
  574. }
  575. struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
  576. const struct efx_filter_spec *spec)
  577. {
  578. struct efx_arfs_rule *rule;
  579. struct hlist_head *head;
  580. struct hlist_node *node;
  581. head = efx_rps_hash_bucket(efx, spec);
  582. if (!head)
  583. return NULL;
  584. hlist_for_each(node, head) {
  585. rule = container_of(node, struct efx_arfs_rule, node);
  586. if (efx_filter_spec_equal(spec, &rule->spec))
  587. return rule;
  588. }
  589. return NULL;
  590. }
  591. struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
  592. const struct efx_filter_spec *spec,
  593. bool *new)
  594. {
  595. struct efx_arfs_rule *rule;
  596. struct hlist_head *head;
  597. struct hlist_node *node;
  598. head = efx_rps_hash_bucket(efx, spec);
  599. if (!head)
  600. return NULL;
  601. hlist_for_each(node, head) {
  602. rule = container_of(node, struct efx_arfs_rule, node);
  603. if (efx_filter_spec_equal(spec, &rule->spec)) {
  604. *new = false;
  605. return rule;
  606. }
  607. }
  608. rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
  609. *new = true;
  610. if (rule) {
  611. memcpy(&rule->spec, spec, sizeof(rule->spec));
  612. hlist_add_head(&rule->node, head);
  613. }
  614. return rule;
  615. }
  616. void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
  617. {
  618. struct efx_arfs_rule *rule;
  619. struct hlist_head *head;
  620. struct hlist_node *node;
  621. head = efx_rps_hash_bucket(efx, spec);
  622. if (WARN_ON(!head))
  623. return;
  624. hlist_for_each(node, head) {
  625. rule = container_of(node, struct efx_arfs_rule, node);
  626. if (efx_filter_spec_equal(spec, &rule->spec)) {
  627. /* Someone already reused the entry. We know that if
  628. * this check doesn't fire (i.e. filter_id == REMOVING)
  629. * then the REMOVING mark was put there by our caller,
  630. * because caller is holding a lock on filter table and
  631. * only holders of that lock set REMOVING.
  632. */
  633. if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
  634. return;
  635. hlist_del(node);
  636. kfree(rule);
  637. return;
  638. }
  639. }
  640. /* We didn't find it. */
  641. WARN_ON(1);
  642. }
  643. #endif
  644. int efx_probe_filters(struct efx_nic *efx)
  645. {
  646. int rc;
  647. mutex_lock(&efx->mac_lock);
  648. rc = efx->type->filter_table_probe(efx);
  649. if (rc)
  650. goto out_unlock;
  651. #ifdef CONFIG_RFS_ACCEL
  652. if (efx->type->offload_features & NETIF_F_NTUPLE) {
  653. struct efx_channel *channel;
  654. int i, success = 1;
  655. efx_for_each_channel(channel, efx) {
  656. channel->rps_flow_id =
  657. kcalloc(efx->type->max_rx_ip_filters,
  658. sizeof(*channel->rps_flow_id),
  659. GFP_KERNEL);
  660. if (!channel->rps_flow_id)
  661. success = 0;
  662. else
  663. for (i = 0;
  664. i < efx->type->max_rx_ip_filters;
  665. ++i)
  666. channel->rps_flow_id[i] =
  667. RPS_FLOW_ID_INVALID;
  668. channel->rfs_expire_index = 0;
  669. channel->rfs_filter_count = 0;
  670. }
  671. if (!success) {
  672. efx_for_each_channel(channel, efx) {
  673. kfree(channel->rps_flow_id);
  674. channel->rps_flow_id = NULL;
  675. }
  676. efx->type->filter_table_remove(efx);
  677. rc = -ENOMEM;
  678. goto out_unlock;
  679. }
  680. }
  681. #endif
  682. out_unlock:
  683. mutex_unlock(&efx->mac_lock);
  684. return rc;
  685. }
  686. void efx_remove_filters(struct efx_nic *efx)
  687. {
  688. #ifdef CONFIG_RFS_ACCEL
  689. struct efx_channel *channel;
  690. efx_for_each_channel(channel, efx) {
  691. cancel_delayed_work_sync(&channel->filter_work);
  692. kfree(channel->rps_flow_id);
  693. channel->rps_flow_id = NULL;
  694. }
  695. #endif
  696. efx->type->filter_table_remove(efx);
  697. }
  698. #ifdef CONFIG_RFS_ACCEL
  699. static void efx_filter_rfs_work(struct work_struct *data)
  700. {
  701. struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
  702. work);
  703. struct efx_nic *efx = efx_netdev_priv(req->net_dev);
  704. struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
  705. int slot_idx = req - efx->rps_slot;
  706. struct efx_arfs_rule *rule;
  707. u16 arfs_id = 0;
  708. int rc;
  709. rc = efx->type->filter_insert(efx, &req->spec, true);
  710. if (rc >= 0)
  711. /* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
  712. rc %= efx->type->max_rx_ip_filters;
  713. if (efx->rps_hash_table) {
  714. spin_lock_bh(&efx->rps_hash_lock);
  715. rule = efx_rps_hash_find(efx, &req->spec);
  716. /* The rule might have already gone, if someone else's request
  717. * for the same spec was already worked and then expired before
  718. * we got around to our work. In that case we have nothing
  719. * tying us to an arfs_id, meaning that as soon as the filter
  720. * is considered for expiry it will be removed.
  721. */
  722. if (rule) {
  723. if (rc < 0)
  724. rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
  725. else
  726. rule->filter_id = rc;
  727. arfs_id = rule->arfs_id;
  728. }
  729. spin_unlock_bh(&efx->rps_hash_lock);
  730. }
  731. if (rc >= 0) {
  732. /* Remember this so we can check whether to expire the filter
  733. * later.
  734. */
  735. mutex_lock(&efx->rps_mutex);
  736. if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
  737. channel->rfs_filter_count++;
  738. channel->rps_flow_id[rc] = req->flow_id;
  739. mutex_unlock(&efx->rps_mutex);
  740. if (req->spec.ether_type == htons(ETH_P_IP))
  741. netif_info(efx, rx_status, efx->net_dev,
  742. "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
  743. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  744. req->spec.rem_host, ntohs(req->spec.rem_port),
  745. req->spec.loc_host, ntohs(req->spec.loc_port),
  746. req->rxq_index, req->flow_id, rc, arfs_id);
  747. else
  748. netif_info(efx, rx_status, efx->net_dev,
  749. "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
  750. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  751. req->spec.rem_host, ntohs(req->spec.rem_port),
  752. req->spec.loc_host, ntohs(req->spec.loc_port),
  753. req->rxq_index, req->flow_id, rc, arfs_id);
  754. channel->n_rfs_succeeded++;
  755. } else {
  756. if (req->spec.ether_type == htons(ETH_P_IP))
  757. netif_dbg(efx, rx_status, efx->net_dev,
  758. "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
  759. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  760. req->spec.rem_host, ntohs(req->spec.rem_port),
  761. req->spec.loc_host, ntohs(req->spec.loc_port),
  762. req->rxq_index, req->flow_id, rc, arfs_id);
  763. else
  764. netif_dbg(efx, rx_status, efx->net_dev,
  765. "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
  766. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  767. req->spec.rem_host, ntohs(req->spec.rem_port),
  768. req->spec.loc_host, ntohs(req->spec.loc_port),
  769. req->rxq_index, req->flow_id, rc, arfs_id);
  770. channel->n_rfs_failed++;
  771. /* We're overloading the NIC's filter tables, so let's do a
  772. * chunk of extra expiry work.
  773. */
  774. __efx_filter_rfs_expire(channel, min(channel->rfs_filter_count,
  775. 100u));
  776. }
  777. /* Release references */
  778. clear_bit(slot_idx, &efx->rps_slot_map);
  779. dev_put(req->net_dev);
  780. }
  781. int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  782. u16 rxq_index, u32 flow_id)
  783. {
  784. struct efx_nic *efx = efx_netdev_priv(net_dev);
  785. struct efx_async_filter_insertion *req;
  786. struct efx_arfs_rule *rule;
  787. struct flow_keys fk;
  788. int slot_idx;
  789. bool new;
  790. int rc;
  791. /* find a free slot */
  792. for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
  793. if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
  794. break;
  795. if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
  796. return -EBUSY;
  797. if (flow_id == RPS_FLOW_ID_INVALID) {
  798. rc = -EINVAL;
  799. goto out_clear;
  800. }
  801. if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
  802. rc = -EPROTONOSUPPORT;
  803. goto out_clear;
  804. }
  805. if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
  806. rc = -EPROTONOSUPPORT;
  807. goto out_clear;
  808. }
  809. if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
  810. rc = -EPROTONOSUPPORT;
  811. goto out_clear;
  812. }
  813. req = efx->rps_slot + slot_idx;
  814. efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
  815. efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
  816. rxq_index);
  817. req->spec.match_flags =
  818. EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  819. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  820. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
  821. req->spec.ether_type = fk.basic.n_proto;
  822. req->spec.ip_proto = fk.basic.ip_proto;
  823. if (fk.basic.n_proto == htons(ETH_P_IP)) {
  824. req->spec.rem_host[0] = fk.addrs.v4addrs.src;
  825. req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
  826. } else {
  827. memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
  828. sizeof(struct in6_addr));
  829. memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
  830. sizeof(struct in6_addr));
  831. }
  832. req->spec.rem_port = fk.ports.src;
  833. req->spec.loc_port = fk.ports.dst;
  834. if (efx->rps_hash_table) {
  835. /* Add it to ARFS hash table */
  836. spin_lock(&efx->rps_hash_lock);
  837. rule = efx_rps_hash_add(efx, &req->spec, &new);
  838. if (!rule) {
  839. rc = -ENOMEM;
  840. goto out_unlock;
  841. }
  842. if (new)
  843. rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
  844. rc = rule->arfs_id;
  845. /* Skip if existing or pending filter already does the right thing */
  846. if (!new && rule->rxq_index == rxq_index &&
  847. rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
  848. goto out_unlock;
  849. rule->rxq_index = rxq_index;
  850. rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
  851. spin_unlock(&efx->rps_hash_lock);
  852. } else {
  853. /* Without an ARFS hash table, we just use arfs_id 0 for all
  854. * filters. This means if multiple flows hash to the same
  855. * flow_id, all but the most recently touched will be eligible
  856. * for expiry.
  857. */
  858. rc = 0;
  859. }
  860. /* Queue the request */
  861. dev_hold(req->net_dev = net_dev);
  862. INIT_WORK(&req->work, efx_filter_rfs_work);
  863. req->rxq_index = rxq_index;
  864. req->flow_id = flow_id;
  865. schedule_work(&req->work);
  866. return rc;
  867. out_unlock:
  868. spin_unlock(&efx->rps_hash_lock);
  869. out_clear:
  870. clear_bit(slot_idx, &efx->rps_slot_map);
  871. return rc;
  872. }
  873. bool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota)
  874. {
  875. bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
  876. struct efx_nic *efx = channel->efx;
  877. unsigned int index, size, start;
  878. u32 flow_id;
  879. if (!mutex_trylock(&efx->rps_mutex))
  880. return false;
  881. expire_one = efx->type->filter_rfs_expire_one;
  882. index = channel->rfs_expire_index;
  883. start = index;
  884. size = efx->type->max_rx_ip_filters;
  885. while (quota) {
  886. flow_id = channel->rps_flow_id[index];
  887. if (flow_id != RPS_FLOW_ID_INVALID) {
  888. quota--;
  889. if (expire_one(efx, flow_id, index)) {
  890. netif_info(efx, rx_status, efx->net_dev,
  891. "expired filter %d [channel %u flow %u]\n",
  892. index, channel->channel, flow_id);
  893. channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
  894. channel->rfs_filter_count--;
  895. }
  896. }
  897. if (++index == size)
  898. index = 0;
  899. /* If we were called with a quota that exceeds the total number
  900. * of filters in the table (which shouldn't happen, but could
  901. * if two callers race), ensure that we don't loop forever -
  902. * stop when we've examined every row of the table.
  903. */
  904. if (index == start)
  905. break;
  906. }
  907. channel->rfs_expire_index = index;
  908. mutex_unlock(&efx->rps_mutex);
  909. return true;
  910. }
  911. #endif /* CONFIG_RFS_ACCEL */