rx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2009 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/slab.h>
  13. #include <linux/ip.h>
  14. #include <linux/tcp.h>
  15. #include <linux/udp.h>
  16. #include <net/ip.h>
  17. #include <net/checksum.h>
  18. #include "net_driver.h"
  19. #include "efx.h"
  20. #include "nic.h"
  21. #include "selftest.h"
  22. #include "workarounds.h"
  23. /* Number of RX descriptors pushed at once. */
  24. #define EFX_RX_BATCH 8
  25. /* Maximum size of a buffer sharing a page */
  26. #define EFX_RX_HALF_PAGE ((PAGE_SIZE >> 1) - sizeof(struct efx_rx_page_state))
  27. /* Size of buffer allocated for skb header area. */
  28. #define EFX_SKB_HEADERS 64u
  29. /*
  30. * rx_alloc_method - RX buffer allocation method
  31. *
  32. * This driver supports two methods for allocating and using RX buffers:
  33. * each RX buffer may be backed by an skb or by an order-n page.
  34. *
  35. * When LRO is in use then the second method has a lower overhead,
  36. * since we don't have to allocate then free skbs on reassembled frames.
  37. *
  38. * Values:
  39. * - RX_ALLOC_METHOD_AUTO = 0
  40. * - RX_ALLOC_METHOD_SKB = 1
  41. * - RX_ALLOC_METHOD_PAGE = 2
  42. *
  43. * The heuristic for %RX_ALLOC_METHOD_AUTO is a simple hysteresis count
  44. * controlled by the parameters below.
  45. *
  46. * - Since pushing and popping descriptors are separated by the rx_queue
  47. * size, so the watermarks should be ~rxd_size.
  48. * - The performance win by using page-based allocation for LRO is less
  49. * than the performance hit of using page-based allocation of non-LRO,
  50. * so the watermarks should reflect this.
  51. *
  52. * Per channel we maintain a single variable, updated by each channel:
  53. *
  54. * rx_alloc_level += (lro_performed ? RX_ALLOC_FACTOR_LRO :
  55. * RX_ALLOC_FACTOR_SKB)
  56. * Per NAPI poll interval, we constrain rx_alloc_level to 0..MAX (which
  57. * limits the hysteresis), and update the allocation strategy:
  58. *
  59. * rx_alloc_method = (rx_alloc_level > RX_ALLOC_LEVEL_LRO ?
  60. * RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB)
  61. */
  62. static int rx_alloc_method = RX_ALLOC_METHOD_AUTO;
  63. #define RX_ALLOC_LEVEL_LRO 0x2000
  64. #define RX_ALLOC_LEVEL_MAX 0x3000
  65. #define RX_ALLOC_FACTOR_LRO 1
  66. #define RX_ALLOC_FACTOR_SKB (-2)
  67. /* This is the percentage fill level below which new RX descriptors
  68. * will be added to the RX descriptor ring.
  69. */
  70. static unsigned int rx_refill_threshold = 90;
  71. /* This is the percentage fill level to which an RX queue will be refilled
  72. * when the "RX refill threshold" is reached.
  73. */
  74. static unsigned int rx_refill_limit = 95;
  75. /*
  76. * RX maximum head room required.
  77. *
  78. * This must be at least 1 to prevent overflow and at least 2 to allow
  79. * pipelined receives.
  80. */
  81. #define EFX_RXD_HEAD_ROOM 2
  82. static inline unsigned int efx_rx_buf_offset(struct efx_rx_buffer *buf)
  83. {
  84. /* Offset is always within one page, so we don't need to consider
  85. * the page order.
  86. */
  87. return (__force unsigned long) buf->data & (PAGE_SIZE - 1);
  88. }
  89. static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
  90. {
  91. return PAGE_SIZE << efx->rx_buffer_order;
  92. }
  93. /**
  94. * efx_init_rx_buffers_skb - create EFX_RX_BATCH skb-based RX buffers
  95. *
  96. * @rx_queue: Efx RX queue
  97. *
  98. * This allocates EFX_RX_BATCH skbs, maps them for DMA, and populates a
  99. * struct efx_rx_buffer for each one. Return a negative error code or 0
  100. * on success. May fail having only inserted fewer than EFX_RX_BATCH
  101. * buffers.
  102. */
  103. static int efx_init_rx_buffers_skb(struct efx_rx_queue *rx_queue)
  104. {
  105. struct efx_nic *efx = rx_queue->efx;
  106. struct net_device *net_dev = efx->net_dev;
  107. struct efx_rx_buffer *rx_buf;
  108. int skb_len = efx->rx_buffer_len;
  109. unsigned index, count;
  110. for (count = 0; count < EFX_RX_BATCH; ++count) {
  111. index = rx_queue->added_count & EFX_RXQ_MASK;
  112. rx_buf = efx_rx_buffer(rx_queue, index);
  113. rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
  114. if (unlikely(!rx_buf->skb))
  115. return -ENOMEM;
  116. rx_buf->page = NULL;
  117. /* Adjust the SKB for padding and checksum */
  118. skb_reserve(rx_buf->skb, NET_IP_ALIGN);
  119. rx_buf->len = skb_len - NET_IP_ALIGN;
  120. rx_buf->data = (char *)rx_buf->skb->data;
  121. rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
  122. rx_buf->dma_addr = pci_map_single(efx->pci_dev,
  123. rx_buf->data, rx_buf->len,
  124. PCI_DMA_FROMDEVICE);
  125. if (unlikely(pci_dma_mapping_error(efx->pci_dev,
  126. rx_buf->dma_addr))) {
  127. dev_kfree_skb_any(rx_buf->skb);
  128. rx_buf->skb = NULL;
  129. return -EIO;
  130. }
  131. ++rx_queue->added_count;
  132. ++rx_queue->alloc_skb_count;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * efx_init_rx_buffers_page - create EFX_RX_BATCH page-based RX buffers
  138. *
  139. * @rx_queue: Efx RX queue
  140. *
  141. * This allocates memory for EFX_RX_BATCH receive buffers, maps them for DMA,
  142. * and populates struct efx_rx_buffers for each one. Return a negative error
  143. * code or 0 on success. If a single page can be split between two buffers,
  144. * then the page will either be inserted fully, or not at at all.
  145. */
  146. static int efx_init_rx_buffers_page(struct efx_rx_queue *rx_queue)
  147. {
  148. struct efx_nic *efx = rx_queue->efx;
  149. struct efx_rx_buffer *rx_buf;
  150. struct page *page;
  151. void *page_addr;
  152. struct efx_rx_page_state *state;
  153. dma_addr_t dma_addr;
  154. unsigned index, count;
  155. /* We can split a page between two buffers */
  156. BUILD_BUG_ON(EFX_RX_BATCH & 1);
  157. for (count = 0; count < EFX_RX_BATCH; ++count) {
  158. page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
  159. efx->rx_buffer_order);
  160. if (unlikely(page == NULL))
  161. return -ENOMEM;
  162. dma_addr = pci_map_page(efx->pci_dev, page, 0,
  163. efx_rx_buf_size(efx),
  164. PCI_DMA_FROMDEVICE);
  165. if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
  166. __free_pages(page, efx->rx_buffer_order);
  167. return -EIO;
  168. }
  169. page_addr = page_address(page);
  170. state = page_addr;
  171. state->refcnt = 0;
  172. state->dma_addr = dma_addr;
  173. page_addr += sizeof(struct efx_rx_page_state);
  174. dma_addr += sizeof(struct efx_rx_page_state);
  175. split:
  176. index = rx_queue->added_count & EFX_RXQ_MASK;
  177. rx_buf = efx_rx_buffer(rx_queue, index);
  178. rx_buf->dma_addr = dma_addr + EFX_PAGE_IP_ALIGN;
  179. rx_buf->skb = NULL;
  180. rx_buf->page = page;
  181. rx_buf->data = page_addr + EFX_PAGE_IP_ALIGN;
  182. rx_buf->len = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
  183. ++rx_queue->added_count;
  184. ++rx_queue->alloc_page_count;
  185. ++state->refcnt;
  186. if ((~count & 1) && (efx->rx_buffer_len <= EFX_RX_HALF_PAGE)) {
  187. /* Use the second half of the page */
  188. get_page(page);
  189. dma_addr += (PAGE_SIZE >> 1);
  190. page_addr += (PAGE_SIZE >> 1);
  191. ++count;
  192. goto split;
  193. }
  194. }
  195. return 0;
  196. }
  197. static void efx_unmap_rx_buffer(struct efx_nic *efx,
  198. struct efx_rx_buffer *rx_buf)
  199. {
  200. if (rx_buf->page) {
  201. struct efx_rx_page_state *state;
  202. EFX_BUG_ON_PARANOID(rx_buf->skb);
  203. state = page_address(rx_buf->page);
  204. if (--state->refcnt == 0) {
  205. pci_unmap_page(efx->pci_dev,
  206. state->dma_addr,
  207. efx_rx_buf_size(efx),
  208. PCI_DMA_FROMDEVICE);
  209. }
  210. } else if (likely(rx_buf->skb)) {
  211. pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
  212. rx_buf->len, PCI_DMA_FROMDEVICE);
  213. }
  214. }
  215. static void efx_free_rx_buffer(struct efx_nic *efx,
  216. struct efx_rx_buffer *rx_buf)
  217. {
  218. if (rx_buf->page) {
  219. __free_pages(rx_buf->page, efx->rx_buffer_order);
  220. rx_buf->page = NULL;
  221. } else if (likely(rx_buf->skb)) {
  222. dev_kfree_skb_any(rx_buf->skb);
  223. rx_buf->skb = NULL;
  224. }
  225. }
  226. static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  227. struct efx_rx_buffer *rx_buf)
  228. {
  229. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  230. efx_free_rx_buffer(rx_queue->efx, rx_buf);
  231. }
  232. /* Attempt to resurrect the other receive buffer that used to share this page,
  233. * which had previously been passed up to the kernel and freed. */
  234. static void efx_resurrect_rx_buffer(struct efx_rx_queue *rx_queue,
  235. struct efx_rx_buffer *rx_buf)
  236. {
  237. struct efx_rx_page_state *state = page_address(rx_buf->page);
  238. struct efx_rx_buffer *new_buf;
  239. unsigned fill_level, index;
  240. /* +1 because efx_rx_packet() incremented removed_count. +1 because
  241. * we'd like to insert an additional descriptor whilst leaving
  242. * EFX_RXD_HEAD_ROOM for the non-recycle path */
  243. fill_level = (rx_queue->added_count - rx_queue->removed_count + 2);
  244. if (unlikely(fill_level >= EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM)) {
  245. /* We could place "state" on a list, and drain the list in
  246. * efx_fast_push_rx_descriptors(). For now, this will do. */
  247. return;
  248. }
  249. ++state->refcnt;
  250. get_page(rx_buf->page);
  251. index = rx_queue->added_count & EFX_RXQ_MASK;
  252. new_buf = efx_rx_buffer(rx_queue, index);
  253. new_buf->dma_addr = rx_buf->dma_addr ^ (PAGE_SIZE >> 1);
  254. new_buf->skb = NULL;
  255. new_buf->page = rx_buf->page;
  256. new_buf->data = (void *)
  257. ((__force unsigned long)rx_buf->data ^ (PAGE_SIZE >> 1));
  258. new_buf->len = rx_buf->len;
  259. ++rx_queue->added_count;
  260. }
  261. /* Recycle the given rx buffer directly back into the rx_queue. There is
  262. * always room to add this buffer, because we've just popped a buffer. */
  263. static void efx_recycle_rx_buffer(struct efx_channel *channel,
  264. struct efx_rx_buffer *rx_buf)
  265. {
  266. struct efx_nic *efx = channel->efx;
  267. struct efx_rx_queue *rx_queue = &efx->rx_queue[channel->channel];
  268. struct efx_rx_buffer *new_buf;
  269. unsigned index;
  270. if (rx_buf->page != NULL && efx->rx_buffer_len <= EFX_RX_HALF_PAGE &&
  271. page_count(rx_buf->page) == 1)
  272. efx_resurrect_rx_buffer(rx_queue, rx_buf);
  273. index = rx_queue->added_count & EFX_RXQ_MASK;
  274. new_buf = efx_rx_buffer(rx_queue, index);
  275. memcpy(new_buf, rx_buf, sizeof(*new_buf));
  276. rx_buf->page = NULL;
  277. rx_buf->skb = NULL;
  278. ++rx_queue->added_count;
  279. }
  280. /**
  281. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  282. * @rx_queue: RX descriptor queue
  283. * This will aim to fill the RX descriptor queue up to
  284. * @rx_queue->@fast_fill_limit. If there is insufficient atomic
  285. * memory to do so, a slow fill will be scheduled.
  286. *
  287. * The caller must provide serialisation (none is used here). In practise,
  288. * this means this function must run from the NAPI handler, or be called
  289. * when NAPI is disabled.
  290. */
  291. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
  292. {
  293. struct efx_channel *channel = rx_queue->channel;
  294. unsigned fill_level;
  295. int space, rc = 0;
  296. /* Calculate current fill level, and exit if we don't need to fill */
  297. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  298. EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
  299. if (fill_level >= rx_queue->fast_fill_trigger)
  300. goto out;
  301. /* Record minimum fill level */
  302. if (unlikely(fill_level < rx_queue->min_fill)) {
  303. if (fill_level)
  304. rx_queue->min_fill = fill_level;
  305. }
  306. space = rx_queue->fast_fill_limit - fill_level;
  307. if (space < EFX_RX_BATCH)
  308. goto out;
  309. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
  310. " level %d to level %d using %s allocation\n",
  311. rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
  312. channel->rx_alloc_push_pages ? "page" : "skb");
  313. do {
  314. if (channel->rx_alloc_push_pages)
  315. rc = efx_init_rx_buffers_page(rx_queue);
  316. else
  317. rc = efx_init_rx_buffers_skb(rx_queue);
  318. if (unlikely(rc)) {
  319. /* Ensure that we don't leave the rx queue empty */
  320. if (rx_queue->added_count == rx_queue->removed_count)
  321. efx_schedule_slow_fill(rx_queue);
  322. goto out;
  323. }
  324. } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
  325. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
  326. "to level %d\n", rx_queue->queue,
  327. rx_queue->added_count - rx_queue->removed_count);
  328. out:
  329. if (rx_queue->notified_count != rx_queue->added_count)
  330. efx_nic_notify_rx_desc(rx_queue);
  331. }
  332. void efx_rx_slow_fill(unsigned long context)
  333. {
  334. struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context;
  335. struct efx_channel *channel = rx_queue->channel;
  336. /* Post an event to cause NAPI to run and refill the queue */
  337. efx_nic_generate_fill_event(channel);
  338. ++rx_queue->slow_fill_count;
  339. }
  340. static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  341. struct efx_rx_buffer *rx_buf,
  342. int len, bool *discard,
  343. bool *leak_packet)
  344. {
  345. struct efx_nic *efx = rx_queue->efx;
  346. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  347. if (likely(len <= max_len))
  348. return;
  349. /* The packet must be discarded, but this is only a fatal error
  350. * if the caller indicated it was
  351. */
  352. *discard = true;
  353. if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
  354. EFX_ERR_RL(efx, " RX queue %d seriously overlength "
  355. "RX event (0x%x > 0x%x+0x%x). Leaking\n",
  356. rx_queue->queue, len, max_len,
  357. efx->type->rx_buffer_padding);
  358. /* If this buffer was skb-allocated, then the meta
  359. * data at the end of the skb will be trashed. So
  360. * we have no choice but to leak the fragment.
  361. */
  362. *leak_packet = (rx_buf->skb != NULL);
  363. efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
  364. } else {
  365. EFX_ERR_RL(efx, " RX queue %d overlength RX event "
  366. "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
  367. }
  368. rx_queue->channel->n_rx_overlength++;
  369. }
  370. /* Pass a received packet up through the generic LRO stack
  371. *
  372. * Handles driverlink veto, and passes the fragment up via
  373. * the appropriate LRO method
  374. */
  375. static void efx_rx_packet_lro(struct efx_channel *channel,
  376. struct efx_rx_buffer *rx_buf,
  377. bool checksummed)
  378. {
  379. struct napi_struct *napi = &channel->napi_str;
  380. gro_result_t gro_result;
  381. /* Pass the skb/page into the LRO engine */
  382. if (rx_buf->page) {
  383. struct page *page = rx_buf->page;
  384. struct sk_buff *skb;
  385. EFX_BUG_ON_PARANOID(rx_buf->skb);
  386. rx_buf->page = NULL;
  387. skb = napi_get_frags(napi);
  388. if (!skb) {
  389. put_page(page);
  390. return;
  391. }
  392. skb_shinfo(skb)->frags[0].page = page;
  393. skb_shinfo(skb)->frags[0].page_offset =
  394. efx_rx_buf_offset(rx_buf);
  395. skb_shinfo(skb)->frags[0].size = rx_buf->len;
  396. skb_shinfo(skb)->nr_frags = 1;
  397. skb->len = rx_buf->len;
  398. skb->data_len = rx_buf->len;
  399. skb->truesize += rx_buf->len;
  400. skb->ip_summed =
  401. checksummed ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
  402. skb_record_rx_queue(skb, channel->channel);
  403. gro_result = napi_gro_frags(napi);
  404. } else {
  405. struct sk_buff *skb = rx_buf->skb;
  406. EFX_BUG_ON_PARANOID(!skb);
  407. EFX_BUG_ON_PARANOID(!checksummed);
  408. rx_buf->skb = NULL;
  409. gro_result = napi_gro_receive(napi, skb);
  410. }
  411. if (gro_result == GRO_NORMAL) {
  412. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  413. } else if (gro_result != GRO_DROP) {
  414. channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
  415. channel->irq_mod_score += 2;
  416. }
  417. }
  418. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  419. unsigned int len, bool checksummed, bool discard)
  420. {
  421. struct efx_nic *efx = rx_queue->efx;
  422. struct efx_channel *channel = rx_queue->channel;
  423. struct efx_rx_buffer *rx_buf;
  424. bool leak_packet = false;
  425. rx_buf = efx_rx_buffer(rx_queue, index);
  426. EFX_BUG_ON_PARANOID(!rx_buf->data);
  427. EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
  428. EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
  429. /* This allows the refill path to post another buffer.
  430. * EFX_RXD_HEAD_ROOM ensures that the slot we are using
  431. * isn't overwritten yet.
  432. */
  433. rx_queue->removed_count++;
  434. /* Validate the length encoded in the event vs the descriptor pushed */
  435. efx_rx_packet__check_len(rx_queue, rx_buf, len,
  436. &discard, &leak_packet);
  437. EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
  438. rx_queue->queue, index,
  439. (unsigned long long)rx_buf->dma_addr, len,
  440. (checksummed ? " [SUMMED]" : ""),
  441. (discard ? " [DISCARD]" : ""));
  442. /* Discard packet, if instructed to do so */
  443. if (unlikely(discard)) {
  444. if (unlikely(leak_packet))
  445. channel->n_skbuff_leaks++;
  446. else
  447. efx_recycle_rx_buffer(channel, rx_buf);
  448. /* Don't hold off the previous receive */
  449. rx_buf = NULL;
  450. goto out;
  451. }
  452. /* Release card resources - assumes all RX buffers consumed in-order
  453. * per RX queue
  454. */
  455. efx_unmap_rx_buffer(efx, rx_buf);
  456. /* Prefetch nice and early so data will (hopefully) be in cache by
  457. * the time we look at it.
  458. */
  459. prefetch(rx_buf->data);
  460. /* Pipeline receives so that we give time for packet headers to be
  461. * prefetched into cache.
  462. */
  463. rx_buf->len = len;
  464. out:
  465. if (rx_queue->channel->rx_pkt)
  466. __efx_rx_packet(rx_queue->channel,
  467. rx_queue->channel->rx_pkt,
  468. rx_queue->channel->rx_pkt_csummed);
  469. rx_queue->channel->rx_pkt = rx_buf;
  470. rx_queue->channel->rx_pkt_csummed = checksummed;
  471. }
  472. /* Handle a received packet. Second half: Touches packet payload. */
  473. void __efx_rx_packet(struct efx_channel *channel,
  474. struct efx_rx_buffer *rx_buf, bool checksummed)
  475. {
  476. struct efx_nic *efx = channel->efx;
  477. struct sk_buff *skb;
  478. /* If we're in loopback test, then pass the packet directly to the
  479. * loopback layer, and free the rx_buf here
  480. */
  481. if (unlikely(efx->loopback_selftest)) {
  482. efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
  483. efx_free_rx_buffer(efx, rx_buf);
  484. return;
  485. }
  486. if (rx_buf->skb) {
  487. prefetch(skb_shinfo(rx_buf->skb));
  488. skb_put(rx_buf->skb, rx_buf->len);
  489. /* Move past the ethernet header. rx_buf->data still points
  490. * at the ethernet header */
  491. rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
  492. efx->net_dev);
  493. skb_record_rx_queue(rx_buf->skb, channel->channel);
  494. }
  495. if (likely(checksummed || rx_buf->page)) {
  496. efx_rx_packet_lro(channel, rx_buf, checksummed);
  497. return;
  498. }
  499. /* We now own the SKB */
  500. skb = rx_buf->skb;
  501. rx_buf->skb = NULL;
  502. EFX_BUG_ON_PARANOID(!skb);
  503. /* Set the SKB flags */
  504. skb->ip_summed = CHECKSUM_NONE;
  505. /* Pass the packet up */
  506. netif_receive_skb(skb);
  507. /* Update allocation strategy method */
  508. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  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_RXQ_SIZE * sizeof(*rx_queue->buffer);
  537. rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
  538. if (!rx_queue->buffer)
  539. return -ENOMEM;
  540. rc = efx_nic_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. unsigned int max_fill, trigger, limit;
  550. EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
  551. /* Initialise ptr fields */
  552. rx_queue->added_count = 0;
  553. rx_queue->notified_count = 0;
  554. rx_queue->removed_count = 0;
  555. rx_queue->min_fill = -1U;
  556. rx_queue->min_overfill = -1U;
  557. /* Initialise limit fields */
  558. max_fill = EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM;
  559. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  560. limit = max_fill * min(rx_refill_limit, 100U) / 100U;
  561. rx_queue->max_fill = max_fill;
  562. rx_queue->fast_fill_trigger = trigger;
  563. rx_queue->fast_fill_limit = limit;
  564. /* Set up RX descriptor ring */
  565. efx_nic_init_rx(rx_queue);
  566. }
  567. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  568. {
  569. int i;
  570. struct efx_rx_buffer *rx_buf;
  571. EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
  572. del_timer_sync(&rx_queue->slow_fill);
  573. efx_nic_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 <= EFX_RXQ_MASK; i++) {
  577. rx_buf = efx_rx_buffer(rx_queue, i);
  578. efx_fini_rx_buffer(rx_queue, rx_buf);
  579. }
  580. }
  581. }
  582. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  583. {
  584. EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
  585. efx_nic_remove_rx(rx_queue);
  586. kfree(rx_queue->buffer);
  587. rx_queue->buffer = NULL;
  588. }
  589. module_param(rx_alloc_method, int, 0644);
  590. MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
  591. module_param(rx_refill_threshold, uint, 0444);
  592. MODULE_PARM_DESC(rx_refill_threshold,
  593. "RX descriptor ring fast/slow fill threshold (%)");