rx.c 20 KB

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