rx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2008 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 <linux/socket.h>
  11. #include <linux/in.h>
  12. #include <linux/ip.h>
  13. #include <linux/tcp.h>
  14. #include <linux/udp.h>
  15. #include <net/ip.h>
  16. #include <net/checksum.h>
  17. #include "net_driver.h"
  18. #include "rx.h"
  19. #include "efx.h"
  20. #include "falcon.h"
  21. #include "selftest.h"
  22. #include "workarounds.h"
  23. /* Number of RX descriptors pushed at once. */
  24. #define EFX_RX_BATCH 8
  25. /* Size of buffer allocated for skb header area. */
  26. #define EFX_SKB_HEADERS 64u
  27. /*
  28. * rx_alloc_method - RX buffer allocation method
  29. *
  30. * This driver supports two methods for allocating and using RX buffers:
  31. * each RX buffer may be backed by an skb or by an order-n page.
  32. *
  33. * When LRO is in use then the second method has a lower overhead,
  34. * since we don't have to allocate then free skbs on reassembled frames.
  35. *
  36. * Values:
  37. * - RX_ALLOC_METHOD_AUTO = 0
  38. * - RX_ALLOC_METHOD_SKB = 1
  39. * - RX_ALLOC_METHOD_PAGE = 2
  40. *
  41. * The heuristic for %RX_ALLOC_METHOD_AUTO is a simple hysteresis count
  42. * controlled by the parameters below.
  43. *
  44. * - Since pushing and popping descriptors are separated by the rx_queue
  45. * size, so the watermarks should be ~rxd_size.
  46. * - The performance win by using page-based allocation for LRO is less
  47. * than the performance hit of using page-based allocation of non-LRO,
  48. * so the watermarks should reflect this.
  49. *
  50. * Per channel we maintain a single variable, updated by each channel:
  51. *
  52. * rx_alloc_level += (lro_performed ? RX_ALLOC_FACTOR_LRO :
  53. * RX_ALLOC_FACTOR_SKB)
  54. * Per NAPI poll interval, we constrain rx_alloc_level to 0..MAX (which
  55. * limits the hysteresis), and update the allocation strategy:
  56. *
  57. * rx_alloc_method = (rx_alloc_level > RX_ALLOC_LEVEL_LRO ?
  58. * RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB)
  59. */
  60. static int rx_alloc_method = RX_ALLOC_METHOD_PAGE;
  61. #define RX_ALLOC_LEVEL_LRO 0x2000
  62. #define RX_ALLOC_LEVEL_MAX 0x3000
  63. #define RX_ALLOC_FACTOR_LRO 1
  64. #define RX_ALLOC_FACTOR_SKB (-2)
  65. /* This is the percentage fill level below which new RX descriptors
  66. * will be added to the RX descriptor ring.
  67. */
  68. static unsigned int rx_refill_threshold = 90;
  69. /* This is the percentage fill level to which an RX queue will be refilled
  70. * when the "RX refill threshold" is reached.
  71. */
  72. static unsigned int rx_refill_limit = 95;
  73. /*
  74. * RX maximum head room required.
  75. *
  76. * This must be at least 1 to prevent overflow and at least 2 to allow
  77. * pipelined receives.
  78. */
  79. #define EFX_RXD_HEAD_ROOM 2
  80. static inline unsigned int efx_rx_buf_offset(struct efx_rx_buffer *buf)
  81. {
  82. /* Offset is always within one page, so we don't need to consider
  83. * the page order.
  84. */
  85. return (__force unsigned long) buf->data & (PAGE_SIZE - 1);
  86. }
  87. static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
  88. {
  89. return PAGE_SIZE << efx->rx_buffer_order;
  90. }
  91. /**
  92. * efx_init_rx_buffer_skb - create new RX buffer using skb-based allocation
  93. *
  94. * @rx_queue: Efx RX queue
  95. * @rx_buf: RX buffer structure to populate
  96. *
  97. * This allocates memory for a new receive buffer, maps it for DMA,
  98. * and populates a struct efx_rx_buffer with the relevant
  99. * information. Return a negative error code or 0 on success.
  100. */
  101. static int efx_init_rx_buffer_skb(struct efx_rx_queue *rx_queue,
  102. struct efx_rx_buffer *rx_buf)
  103. {
  104. struct efx_nic *efx = rx_queue->efx;
  105. struct net_device *net_dev = efx->net_dev;
  106. int skb_len = efx->rx_buffer_len;
  107. rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
  108. if (unlikely(!rx_buf->skb))
  109. return -ENOMEM;
  110. /* Adjust the SKB for padding and checksum */
  111. skb_reserve(rx_buf->skb, NET_IP_ALIGN);
  112. rx_buf->len = skb_len - NET_IP_ALIGN;
  113. rx_buf->data = (char *)rx_buf->skb->data;
  114. rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
  115. rx_buf->dma_addr = pci_map_single(efx->pci_dev,
  116. rx_buf->data, rx_buf->len,
  117. PCI_DMA_FROMDEVICE);
  118. if (unlikely(pci_dma_mapping_error(efx->pci_dev, rx_buf->dma_addr))) {
  119. dev_kfree_skb_any(rx_buf->skb);
  120. rx_buf->skb = NULL;
  121. return -EIO;
  122. }
  123. return 0;
  124. }
  125. /**
  126. * efx_init_rx_buffer_page - create new RX buffer using page-based allocation
  127. *
  128. * @rx_queue: Efx RX queue
  129. * @rx_buf: RX buffer structure to populate
  130. *
  131. * This allocates memory for a new receive buffer, maps it for DMA,
  132. * and populates a struct efx_rx_buffer with the relevant
  133. * information. Return a negative error code or 0 on success.
  134. */
  135. static int efx_init_rx_buffer_page(struct efx_rx_queue *rx_queue,
  136. struct efx_rx_buffer *rx_buf)
  137. {
  138. struct efx_nic *efx = rx_queue->efx;
  139. int bytes, space, offset;
  140. bytes = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
  141. /* If there is space left in the previously allocated page,
  142. * then use it. Otherwise allocate a new one */
  143. rx_buf->page = rx_queue->buf_page;
  144. if (rx_buf->page == NULL) {
  145. dma_addr_t dma_addr;
  146. rx_buf->page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
  147. efx->rx_buffer_order);
  148. if (unlikely(rx_buf->page == NULL))
  149. return -ENOMEM;
  150. dma_addr = pci_map_page(efx->pci_dev, rx_buf->page,
  151. 0, efx_rx_buf_size(efx),
  152. PCI_DMA_FROMDEVICE);
  153. if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
  154. __free_pages(rx_buf->page, efx->rx_buffer_order);
  155. rx_buf->page = NULL;
  156. return -EIO;
  157. }
  158. rx_queue->buf_page = rx_buf->page;
  159. rx_queue->buf_dma_addr = dma_addr;
  160. rx_queue->buf_data = (page_address(rx_buf->page) +
  161. EFX_PAGE_IP_ALIGN);
  162. }
  163. rx_buf->len = bytes;
  164. rx_buf->data = rx_queue->buf_data;
  165. offset = efx_rx_buf_offset(rx_buf);
  166. rx_buf->dma_addr = rx_queue->buf_dma_addr + offset;
  167. /* Try to pack multiple buffers per page */
  168. if (efx->rx_buffer_order == 0) {
  169. /* The next buffer starts on the next 512 byte boundary */
  170. rx_queue->buf_data += ((bytes + 0x1ff) & ~0x1ff);
  171. offset += ((bytes + 0x1ff) & ~0x1ff);
  172. space = efx_rx_buf_size(efx) - offset;
  173. if (space >= bytes) {
  174. /* Refs dropped on kernel releasing each skb */
  175. get_page(rx_queue->buf_page);
  176. goto out;
  177. }
  178. }
  179. /* This is the final RX buffer for this page, so mark it for
  180. * unmapping */
  181. rx_queue->buf_page = NULL;
  182. rx_buf->unmap_addr = rx_queue->buf_dma_addr;
  183. out:
  184. return 0;
  185. }
  186. /* This allocates memory for a new receive buffer, maps it for DMA,
  187. * and populates a struct efx_rx_buffer with the relevant
  188. * information.
  189. */
  190. static int efx_init_rx_buffer(struct efx_rx_queue *rx_queue,
  191. struct efx_rx_buffer *new_rx_buf)
  192. {
  193. int rc = 0;
  194. if (rx_queue->channel->rx_alloc_push_pages) {
  195. new_rx_buf->skb = NULL;
  196. rc = efx_init_rx_buffer_page(rx_queue, new_rx_buf);
  197. rx_queue->alloc_page_count++;
  198. } else {
  199. new_rx_buf->page = NULL;
  200. rc = efx_init_rx_buffer_skb(rx_queue, new_rx_buf);
  201. rx_queue->alloc_skb_count++;
  202. }
  203. if (unlikely(rc < 0))
  204. EFX_LOG_RL(rx_queue->efx, "%s RXQ[%d] =%d\n", __func__,
  205. rx_queue->queue, rc);
  206. return rc;
  207. }
  208. static void efx_unmap_rx_buffer(struct efx_nic *efx,
  209. struct efx_rx_buffer *rx_buf)
  210. {
  211. if (rx_buf->page) {
  212. EFX_BUG_ON_PARANOID(rx_buf->skb);
  213. if (rx_buf->unmap_addr) {
  214. pci_unmap_page(efx->pci_dev, rx_buf->unmap_addr,
  215. efx_rx_buf_size(efx),
  216. PCI_DMA_FROMDEVICE);
  217. rx_buf->unmap_addr = 0;
  218. }
  219. } else if (likely(rx_buf->skb)) {
  220. pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
  221. rx_buf->len, PCI_DMA_FROMDEVICE);
  222. }
  223. }
  224. static void efx_free_rx_buffer(struct efx_nic *efx,
  225. struct efx_rx_buffer *rx_buf)
  226. {
  227. if (rx_buf->page) {
  228. __free_pages(rx_buf->page, efx->rx_buffer_order);
  229. rx_buf->page = NULL;
  230. } else if (likely(rx_buf->skb)) {
  231. dev_kfree_skb_any(rx_buf->skb);
  232. rx_buf->skb = NULL;
  233. }
  234. }
  235. static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  236. struct efx_rx_buffer *rx_buf)
  237. {
  238. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  239. efx_free_rx_buffer(rx_queue->efx, rx_buf);
  240. }
  241. /**
  242. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  243. * @rx_queue: RX descriptor queue
  244. * @retry: Recheck the fill level
  245. * This will aim to fill the RX descriptor queue up to
  246. * @rx_queue->@fast_fill_limit. If there is insufficient atomic
  247. * memory to do so, the caller should retry.
  248. */
  249. static int __efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
  250. int retry)
  251. {
  252. struct efx_rx_buffer *rx_buf;
  253. unsigned fill_level, index;
  254. int i, space, rc = 0;
  255. /* Calculate current fill level. Do this outside the lock,
  256. * because most of the time we'll end up not wanting to do the
  257. * fill anyway.
  258. */
  259. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  260. EFX_BUG_ON_PARANOID(fill_level >
  261. rx_queue->efx->type->rxd_ring_mask + 1);
  262. /* Don't fill if we don't need to */
  263. if (fill_level >= rx_queue->fast_fill_trigger)
  264. return 0;
  265. /* Record minimum fill level */
  266. if (unlikely(fill_level < rx_queue->min_fill)) {
  267. if (fill_level)
  268. rx_queue->min_fill = fill_level;
  269. }
  270. /* Acquire RX add lock. If this lock is contended, then a fast
  271. * fill must already be in progress (e.g. in the refill
  272. * tasklet), so we don't need to do anything
  273. */
  274. if (!spin_trylock_bh(&rx_queue->add_lock))
  275. return -1;
  276. retry:
  277. /* Recalculate current fill level now that we have the lock */
  278. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  279. EFX_BUG_ON_PARANOID(fill_level >
  280. rx_queue->efx->type->rxd_ring_mask + 1);
  281. space = rx_queue->fast_fill_limit - fill_level;
  282. if (space < EFX_RX_BATCH)
  283. goto out_unlock;
  284. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
  285. " level %d to level %d using %s allocation\n",
  286. rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
  287. rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
  288. do {
  289. for (i = 0; i < EFX_RX_BATCH; ++i) {
  290. index = (rx_queue->added_count &
  291. rx_queue->efx->type->rxd_ring_mask);
  292. rx_buf = efx_rx_buffer(rx_queue, index);
  293. rc = efx_init_rx_buffer(rx_queue, rx_buf);
  294. if (unlikely(rc))
  295. goto out;
  296. ++rx_queue->added_count;
  297. }
  298. } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
  299. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
  300. "to level %d\n", rx_queue->queue,
  301. rx_queue->added_count - rx_queue->removed_count);
  302. out:
  303. /* Send write pointer to card. */
  304. falcon_notify_rx_desc(rx_queue);
  305. /* If the fast fill is running inside from the refill tasklet, then
  306. * for SMP systems it may be running on a different CPU to
  307. * RX event processing, which means that the fill level may now be
  308. * out of date. */
  309. if (unlikely(retry && (rc == 0)))
  310. goto retry;
  311. out_unlock:
  312. spin_unlock_bh(&rx_queue->add_lock);
  313. return rc;
  314. }
  315. /**
  316. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  317. * @rx_queue: RX descriptor queue
  318. *
  319. * This will aim to fill the RX descriptor queue up to
  320. * @rx_queue->@fast_fill_limit. If there is insufficient memory to do so,
  321. * it will schedule a work item to immediately continue the fast fill
  322. */
  323. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
  324. {
  325. int rc;
  326. rc = __efx_fast_push_rx_descriptors(rx_queue, 0);
  327. if (unlikely(rc)) {
  328. /* Schedule the work item to run immediately. The hope is
  329. * that work is immediately pending to free some memory
  330. * (e.g. an RX event or TX completion)
  331. */
  332. efx_schedule_slow_fill(rx_queue, 0);
  333. }
  334. }
  335. void efx_rx_work(struct work_struct *data)
  336. {
  337. struct efx_rx_queue *rx_queue;
  338. int rc;
  339. rx_queue = container_of(data, struct efx_rx_queue, work.work);
  340. if (unlikely(!rx_queue->channel->enabled))
  341. return;
  342. EFX_TRACE(rx_queue->efx, "RX queue %d worker thread executing on CPU "
  343. "%d\n", rx_queue->queue, raw_smp_processor_id());
  344. ++rx_queue->slow_fill_count;
  345. /* Push new RX descriptors, allowing at least 1 jiffy for
  346. * the kernel to free some more memory. */
  347. rc = __efx_fast_push_rx_descriptors(rx_queue, 1);
  348. if (rc)
  349. efx_schedule_slow_fill(rx_queue, 1);
  350. }
  351. static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  352. struct efx_rx_buffer *rx_buf,
  353. int len, bool *discard,
  354. bool *leak_packet)
  355. {
  356. struct efx_nic *efx = rx_queue->efx;
  357. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  358. if (likely(len <= max_len))
  359. return;
  360. /* The packet must be discarded, but this is only a fatal error
  361. * if the caller indicated it was
  362. */
  363. *discard = true;
  364. if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
  365. EFX_ERR_RL(efx, " RX queue %d seriously overlength "
  366. "RX event (0x%x > 0x%x+0x%x). Leaking\n",
  367. rx_queue->queue, len, max_len,
  368. efx->type->rx_buffer_padding);
  369. /* If this buffer was skb-allocated, then the meta
  370. * data at the end of the skb will be trashed. So
  371. * we have no choice but to leak the fragment.
  372. */
  373. *leak_packet = (rx_buf->skb != NULL);
  374. efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
  375. } else {
  376. EFX_ERR_RL(efx, " RX queue %d overlength RX event "
  377. "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
  378. }
  379. rx_queue->channel->n_rx_overlength++;
  380. }
  381. /* Pass a received packet up through the generic LRO stack
  382. *
  383. * Handles driverlink veto, and passes the fragment up via
  384. * the appropriate LRO method
  385. */
  386. static void efx_rx_packet_lro(struct efx_channel *channel,
  387. struct efx_rx_buffer *rx_buf)
  388. {
  389. struct napi_struct *napi = &channel->napi_str;
  390. /* Pass the skb/page into the LRO engine */
  391. if (rx_buf->page) {
  392. struct sk_buff *skb = napi_get_frags(napi);
  393. if (!skb) {
  394. put_page(rx_buf->page);
  395. goto out;
  396. }
  397. skb_shinfo(skb)->frags[0].page = rx_buf->page;
  398. skb_shinfo(skb)->frags[0].page_offset =
  399. efx_rx_buf_offset(rx_buf);
  400. skb_shinfo(skb)->frags[0].size = rx_buf->len;
  401. skb_shinfo(skb)->nr_frags = 1;
  402. skb->len = rx_buf->len;
  403. skb->data_len = rx_buf->len;
  404. skb->truesize += rx_buf->len;
  405. skb->ip_summed = CHECKSUM_UNNECESSARY;
  406. napi_gro_frags(napi);
  407. out:
  408. EFX_BUG_ON_PARANOID(rx_buf->skb);
  409. rx_buf->page = NULL;
  410. } else {
  411. EFX_BUG_ON_PARANOID(!rx_buf->skb);
  412. napi_gro_receive(napi, rx_buf->skb);
  413. rx_buf->skb = NULL;
  414. }
  415. }
  416. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  417. unsigned int len, bool checksummed, bool discard)
  418. {
  419. struct efx_nic *efx = rx_queue->efx;
  420. struct efx_rx_buffer *rx_buf;
  421. bool leak_packet = false;
  422. rx_buf = efx_rx_buffer(rx_queue, index);
  423. EFX_BUG_ON_PARANOID(!rx_buf->data);
  424. EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
  425. EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
  426. /* This allows the refill path to post another buffer.
  427. * EFX_RXD_HEAD_ROOM ensures that the slot we are using
  428. * isn't overwritten yet.
  429. */
  430. rx_queue->removed_count++;
  431. /* Validate the length encoded in the event vs the descriptor pushed */
  432. efx_rx_packet__check_len(rx_queue, rx_buf, len,
  433. &discard, &leak_packet);
  434. EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
  435. rx_queue->queue, index,
  436. (unsigned long long)rx_buf->dma_addr, len,
  437. (checksummed ? " [SUMMED]" : ""),
  438. (discard ? " [DISCARD]" : ""));
  439. /* Discard packet, if instructed to do so */
  440. if (unlikely(discard)) {
  441. if (unlikely(leak_packet))
  442. rx_queue->channel->n_skbuff_leaks++;
  443. else
  444. /* We haven't called efx_unmap_rx_buffer yet,
  445. * so fini the entire rx_buffer here */
  446. efx_fini_rx_buffer(rx_queue, rx_buf);
  447. return;
  448. }
  449. /* Release card resources - assumes all RX buffers consumed in-order
  450. * per RX queue
  451. */
  452. efx_unmap_rx_buffer(efx, rx_buf);
  453. /* Prefetch nice and early so data will (hopefully) be in cache by
  454. * the time we look at it.
  455. */
  456. prefetch(rx_buf->data);
  457. /* Pipeline receives so that we give time for packet headers to be
  458. * prefetched into cache.
  459. */
  460. rx_buf->len = len;
  461. if (rx_queue->channel->rx_pkt)
  462. __efx_rx_packet(rx_queue->channel,
  463. rx_queue->channel->rx_pkt,
  464. rx_queue->channel->rx_pkt_csummed);
  465. rx_queue->channel->rx_pkt = rx_buf;
  466. rx_queue->channel->rx_pkt_csummed = checksummed;
  467. }
  468. /* Handle a received packet. Second half: Touches packet payload. */
  469. void __efx_rx_packet(struct efx_channel *channel,
  470. struct efx_rx_buffer *rx_buf, bool checksummed)
  471. {
  472. struct efx_nic *efx = channel->efx;
  473. struct sk_buff *skb;
  474. /* If we're in loopback test, then pass the packet directly to the
  475. * loopback layer, and free the rx_buf here
  476. */
  477. if (unlikely(efx->loopback_selftest)) {
  478. efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
  479. efx_free_rx_buffer(efx, rx_buf);
  480. goto done;
  481. }
  482. if (rx_buf->skb) {
  483. prefetch(skb_shinfo(rx_buf->skb));
  484. skb_put(rx_buf->skb, rx_buf->len);
  485. /* Move past the ethernet header. rx_buf->data still points
  486. * at the ethernet header */
  487. rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
  488. efx->net_dev);
  489. }
  490. if (likely(checksummed || rx_buf->page)) {
  491. efx_rx_packet_lro(channel, rx_buf);
  492. goto done;
  493. }
  494. /* We now own the SKB */
  495. skb = rx_buf->skb;
  496. rx_buf->skb = NULL;
  497. EFX_BUG_ON_PARANOID(rx_buf->page);
  498. EFX_BUG_ON_PARANOID(rx_buf->skb);
  499. EFX_BUG_ON_PARANOID(!skb);
  500. /* Set the SKB flags */
  501. skb->ip_summed = CHECKSUM_NONE;
  502. skb_record_rx_queue(skb, channel->channel);
  503. /* Pass the packet up */
  504. netif_receive_skb(skb);
  505. /* Update allocation strategy method */
  506. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  507. done:
  508. ;
  509. }
  510. void efx_rx_strategy(struct efx_channel *channel)
  511. {
  512. enum efx_rx_alloc_method method = rx_alloc_method;
  513. /* Only makes sense to use page based allocation if LRO is enabled */
  514. if (!(channel->efx->net_dev->features & NETIF_F_GRO)) {
  515. method = RX_ALLOC_METHOD_SKB;
  516. } else if (method == RX_ALLOC_METHOD_AUTO) {
  517. /* Constrain the rx_alloc_level */
  518. if (channel->rx_alloc_level < 0)
  519. channel->rx_alloc_level = 0;
  520. else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
  521. channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
  522. /* Decide on the allocation method */
  523. method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
  524. RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
  525. }
  526. /* Push the option */
  527. channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
  528. }
  529. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  530. {
  531. struct efx_nic *efx = rx_queue->efx;
  532. unsigned int rxq_size;
  533. int rc;
  534. EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
  535. /* Allocate RX buffers */
  536. rxq_size = (efx->type->rxd_ring_mask + 1) * sizeof(*rx_queue->buffer);
  537. rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
  538. if (!rx_queue->buffer)
  539. return -ENOMEM;
  540. rc = falcon_probe_rx(rx_queue);
  541. if (rc) {
  542. kfree(rx_queue->buffer);
  543. rx_queue->buffer = NULL;
  544. }
  545. return rc;
  546. }
  547. void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  548. {
  549. struct efx_nic *efx = rx_queue->efx;
  550. unsigned int max_fill, trigger, limit;
  551. EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
  552. /* Initialise ptr fields */
  553. rx_queue->added_count = 0;
  554. rx_queue->notified_count = 0;
  555. rx_queue->removed_count = 0;
  556. rx_queue->min_fill = -1U;
  557. rx_queue->min_overfill = -1U;
  558. /* Initialise limit fields */
  559. max_fill = efx->type->rxd_ring_mask + 1 - EFX_RXD_HEAD_ROOM;
  560. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  561. limit = max_fill * min(rx_refill_limit, 100U) / 100U;
  562. rx_queue->max_fill = max_fill;
  563. rx_queue->fast_fill_trigger = trigger;
  564. rx_queue->fast_fill_limit = limit;
  565. /* Set up RX descriptor ring */
  566. falcon_init_rx(rx_queue);
  567. }
  568. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  569. {
  570. int i;
  571. struct efx_rx_buffer *rx_buf;
  572. EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
  573. falcon_fini_rx(rx_queue);
  574. /* Release RX buffers NB start at index 0 not current HW ptr */
  575. if (rx_queue->buffer) {
  576. for (i = 0; i <= rx_queue->efx->type->rxd_ring_mask; i++) {
  577. rx_buf = efx_rx_buffer(rx_queue, i);
  578. efx_fini_rx_buffer(rx_queue, rx_buf);
  579. }
  580. }
  581. /* For a page that is part-way through splitting into RX buffers */
  582. if (rx_queue->buf_page != NULL) {
  583. pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
  584. efx_rx_buf_size(rx_queue->efx),
  585. PCI_DMA_FROMDEVICE);
  586. __free_pages(rx_queue->buf_page,
  587. rx_queue->efx->rx_buffer_order);
  588. rx_queue->buf_page = NULL;
  589. }
  590. }
  591. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  592. {
  593. EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
  594. falcon_remove_rx(rx_queue);
  595. kfree(rx_queue->buffer);
  596. rx_queue->buffer = NULL;
  597. }
  598. module_param(rx_alloc_method, int, 0644);
  599. MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
  600. module_param(rx_refill_threshold, uint, 0444);
  601. MODULE_PARM_DESC(rx_refill_threshold,
  602. "RX descriptor ring fast/slow fill threshold (%)");