rx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. /* 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_AUTO;
  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. * 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, a slow fill will be scheduled.
  247. *
  248. * The caller must provide serialisation (none is used here). In practise,
  249. * this means this function must run from the NAPI handler, or be called
  250. * when NAPI is disabled.
  251. */
  252. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
  253. {
  254. struct efx_rx_buffer *rx_buf;
  255. unsigned fill_level, index;
  256. int i, space, rc = 0;
  257. /* Calculate current fill level, and exit if we don't need to fill */
  258. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  259. EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
  260. if (fill_level >= rx_queue->fast_fill_trigger)
  261. return;
  262. /* Record minimum fill level */
  263. if (unlikely(fill_level < rx_queue->min_fill)) {
  264. if (fill_level)
  265. rx_queue->min_fill = fill_level;
  266. }
  267. space = rx_queue->fast_fill_limit - fill_level;
  268. if (space < EFX_RX_BATCH)
  269. return;
  270. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
  271. " level %d to level %d using %s allocation\n",
  272. rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
  273. rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
  274. do {
  275. for (i = 0; i < EFX_RX_BATCH; ++i) {
  276. index = rx_queue->added_count & EFX_RXQ_MASK;
  277. rx_buf = efx_rx_buffer(rx_queue, index);
  278. rc = efx_init_rx_buffer(rx_queue, rx_buf);
  279. if (unlikely(rc)) {
  280. /* Ensure that we don't leave the rx queue
  281. * empty */
  282. if (rx_queue->added_count == rx_queue->removed_count)
  283. efx_schedule_slow_fill(rx_queue);
  284. goto out;
  285. }
  286. ++rx_queue->added_count;
  287. }
  288. } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
  289. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
  290. "to level %d\n", rx_queue->queue,
  291. rx_queue->added_count - rx_queue->removed_count);
  292. out:
  293. /* Send write pointer to card. */
  294. efx_nic_notify_rx_desc(rx_queue);
  295. }
  296. void efx_rx_slow_fill(unsigned long context)
  297. {
  298. struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context;
  299. struct efx_channel *channel = rx_queue->channel;
  300. /* Post an event to cause NAPI to run and refill the queue */
  301. efx_nic_generate_fill_event(channel);
  302. ++rx_queue->slow_fill_count;
  303. }
  304. static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  305. struct efx_rx_buffer *rx_buf,
  306. int len, bool *discard,
  307. bool *leak_packet)
  308. {
  309. struct efx_nic *efx = rx_queue->efx;
  310. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  311. if (likely(len <= max_len))
  312. return;
  313. /* The packet must be discarded, but this is only a fatal error
  314. * if the caller indicated it was
  315. */
  316. *discard = true;
  317. if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
  318. EFX_ERR_RL(efx, " RX queue %d seriously overlength "
  319. "RX event (0x%x > 0x%x+0x%x). Leaking\n",
  320. rx_queue->queue, len, max_len,
  321. efx->type->rx_buffer_padding);
  322. /* If this buffer was skb-allocated, then the meta
  323. * data at the end of the skb will be trashed. So
  324. * we have no choice but to leak the fragment.
  325. */
  326. *leak_packet = (rx_buf->skb != NULL);
  327. efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
  328. } else {
  329. EFX_ERR_RL(efx, " RX queue %d overlength RX event "
  330. "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
  331. }
  332. rx_queue->channel->n_rx_overlength++;
  333. }
  334. /* Pass a received packet up through the generic LRO stack
  335. *
  336. * Handles driverlink veto, and passes the fragment up via
  337. * the appropriate LRO method
  338. */
  339. static void efx_rx_packet_lro(struct efx_channel *channel,
  340. struct efx_rx_buffer *rx_buf,
  341. bool checksummed)
  342. {
  343. struct napi_struct *napi = &channel->napi_str;
  344. gro_result_t gro_result;
  345. /* Pass the skb/page into the LRO engine */
  346. if (rx_buf->page) {
  347. struct page *page = rx_buf->page;
  348. struct sk_buff *skb;
  349. EFX_BUG_ON_PARANOID(rx_buf->skb);
  350. rx_buf->page = NULL;
  351. skb = napi_get_frags(napi);
  352. if (!skb) {
  353. put_page(page);
  354. return;
  355. }
  356. skb_shinfo(skb)->frags[0].page = page;
  357. skb_shinfo(skb)->frags[0].page_offset =
  358. efx_rx_buf_offset(rx_buf);
  359. skb_shinfo(skb)->frags[0].size = rx_buf->len;
  360. skb_shinfo(skb)->nr_frags = 1;
  361. skb->len = rx_buf->len;
  362. skb->data_len = rx_buf->len;
  363. skb->truesize += rx_buf->len;
  364. skb->ip_summed =
  365. checksummed ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
  366. skb_record_rx_queue(skb, channel->channel);
  367. gro_result = napi_gro_frags(napi);
  368. } else {
  369. struct sk_buff *skb = rx_buf->skb;
  370. EFX_BUG_ON_PARANOID(!skb);
  371. EFX_BUG_ON_PARANOID(!checksummed);
  372. rx_buf->skb = NULL;
  373. gro_result = napi_gro_receive(napi, skb);
  374. }
  375. if (gro_result == GRO_NORMAL) {
  376. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  377. } else if (gro_result != GRO_DROP) {
  378. channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
  379. channel->irq_mod_score += 2;
  380. }
  381. }
  382. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  383. unsigned int len, bool checksummed, bool discard)
  384. {
  385. struct efx_nic *efx = rx_queue->efx;
  386. struct efx_rx_buffer *rx_buf;
  387. bool leak_packet = false;
  388. rx_buf = efx_rx_buffer(rx_queue, index);
  389. EFX_BUG_ON_PARANOID(!rx_buf->data);
  390. EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
  391. EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
  392. /* This allows the refill path to post another buffer.
  393. * EFX_RXD_HEAD_ROOM ensures that the slot we are using
  394. * isn't overwritten yet.
  395. */
  396. rx_queue->removed_count++;
  397. /* Validate the length encoded in the event vs the descriptor pushed */
  398. efx_rx_packet__check_len(rx_queue, rx_buf, len,
  399. &discard, &leak_packet);
  400. EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
  401. rx_queue->queue, index,
  402. (unsigned long long)rx_buf->dma_addr, len,
  403. (checksummed ? " [SUMMED]" : ""),
  404. (discard ? " [DISCARD]" : ""));
  405. /* Discard packet, if instructed to do so */
  406. if (unlikely(discard)) {
  407. if (unlikely(leak_packet))
  408. rx_queue->channel->n_skbuff_leaks++;
  409. else
  410. /* We haven't called efx_unmap_rx_buffer yet,
  411. * so fini the entire rx_buffer here */
  412. efx_fini_rx_buffer(rx_queue, rx_buf);
  413. return;
  414. }
  415. /* Release card resources - assumes all RX buffers consumed in-order
  416. * per RX queue
  417. */
  418. efx_unmap_rx_buffer(efx, rx_buf);
  419. /* Prefetch nice and early so data will (hopefully) be in cache by
  420. * the time we look at it.
  421. */
  422. prefetch(rx_buf->data);
  423. /* Pipeline receives so that we give time for packet headers to be
  424. * prefetched into cache.
  425. */
  426. rx_buf->len = len;
  427. if (rx_queue->channel->rx_pkt)
  428. __efx_rx_packet(rx_queue->channel,
  429. rx_queue->channel->rx_pkt,
  430. rx_queue->channel->rx_pkt_csummed);
  431. rx_queue->channel->rx_pkt = rx_buf;
  432. rx_queue->channel->rx_pkt_csummed = checksummed;
  433. }
  434. /* Handle a received packet. Second half: Touches packet payload. */
  435. void __efx_rx_packet(struct efx_channel *channel,
  436. struct efx_rx_buffer *rx_buf, bool checksummed)
  437. {
  438. struct efx_nic *efx = channel->efx;
  439. struct sk_buff *skb;
  440. /* If we're in loopback test, then pass the packet directly to the
  441. * loopback layer, and free the rx_buf here
  442. */
  443. if (unlikely(efx->loopback_selftest)) {
  444. efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
  445. efx_free_rx_buffer(efx, rx_buf);
  446. return;
  447. }
  448. if (rx_buf->skb) {
  449. prefetch(skb_shinfo(rx_buf->skb));
  450. skb_put(rx_buf->skb, rx_buf->len);
  451. /* Move past the ethernet header. rx_buf->data still points
  452. * at the ethernet header */
  453. rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
  454. efx->net_dev);
  455. skb_record_rx_queue(rx_buf->skb, channel->channel);
  456. }
  457. if (likely(checksummed || rx_buf->page)) {
  458. efx_rx_packet_lro(channel, rx_buf, checksummed);
  459. return;
  460. }
  461. /* We now own the SKB */
  462. skb = rx_buf->skb;
  463. rx_buf->skb = NULL;
  464. EFX_BUG_ON_PARANOID(!skb);
  465. /* Set the SKB flags */
  466. skb->ip_summed = CHECKSUM_NONE;
  467. /* Pass the packet up */
  468. netif_receive_skb(skb);
  469. /* Update allocation strategy method */
  470. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  471. }
  472. void efx_rx_strategy(struct efx_channel *channel)
  473. {
  474. enum efx_rx_alloc_method method = rx_alloc_method;
  475. /* Only makes sense to use page based allocation if LRO is enabled */
  476. if (!(channel->efx->net_dev->features & NETIF_F_GRO)) {
  477. method = RX_ALLOC_METHOD_SKB;
  478. } else if (method == RX_ALLOC_METHOD_AUTO) {
  479. /* Constrain the rx_alloc_level */
  480. if (channel->rx_alloc_level < 0)
  481. channel->rx_alloc_level = 0;
  482. else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
  483. channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
  484. /* Decide on the allocation method */
  485. method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
  486. RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
  487. }
  488. /* Push the option */
  489. channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
  490. }
  491. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  492. {
  493. struct efx_nic *efx = rx_queue->efx;
  494. unsigned int rxq_size;
  495. int rc;
  496. EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
  497. /* Allocate RX buffers */
  498. rxq_size = EFX_RXQ_SIZE * sizeof(*rx_queue->buffer);
  499. rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
  500. if (!rx_queue->buffer)
  501. return -ENOMEM;
  502. rc = efx_nic_probe_rx(rx_queue);
  503. if (rc) {
  504. kfree(rx_queue->buffer);
  505. rx_queue->buffer = NULL;
  506. }
  507. return rc;
  508. }
  509. void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  510. {
  511. unsigned int max_fill, trigger, limit;
  512. EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
  513. /* Initialise ptr fields */
  514. rx_queue->added_count = 0;
  515. rx_queue->notified_count = 0;
  516. rx_queue->removed_count = 0;
  517. rx_queue->min_fill = -1U;
  518. rx_queue->min_overfill = -1U;
  519. /* Initialise limit fields */
  520. max_fill = EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM;
  521. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  522. limit = max_fill * min(rx_refill_limit, 100U) / 100U;
  523. rx_queue->max_fill = max_fill;
  524. rx_queue->fast_fill_trigger = trigger;
  525. rx_queue->fast_fill_limit = limit;
  526. /* Set up RX descriptor ring */
  527. efx_nic_init_rx(rx_queue);
  528. }
  529. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  530. {
  531. int i;
  532. struct efx_rx_buffer *rx_buf;
  533. EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
  534. del_timer_sync(&rx_queue->slow_fill);
  535. efx_nic_fini_rx(rx_queue);
  536. /* Release RX buffers NB start at index 0 not current HW ptr */
  537. if (rx_queue->buffer) {
  538. for (i = 0; i <= EFX_RXQ_MASK; i++) {
  539. rx_buf = efx_rx_buffer(rx_queue, i);
  540. efx_fini_rx_buffer(rx_queue, rx_buf);
  541. }
  542. }
  543. /* For a page that is part-way through splitting into RX buffers */
  544. if (rx_queue->buf_page != NULL) {
  545. pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
  546. efx_rx_buf_size(rx_queue->efx),
  547. PCI_DMA_FROMDEVICE);
  548. __free_pages(rx_queue->buf_page,
  549. rx_queue->efx->rx_buffer_order);
  550. rx_queue->buf_page = NULL;
  551. }
  552. }
  553. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  554. {
  555. EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
  556. efx_nic_remove_rx(rx_queue);
  557. kfree(rx_queue->buffer);
  558. rx_queue->buffer = NULL;
  559. }
  560. module_param(rx_alloc_method, int, 0644);
  561. MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
  562. module_param(rx_refill_threshold, uint, 0444);
  563. MODULE_PARM_DESC(rx_refill_threshold,
  564. "RX descriptor ring fast/slow fill threshold (%)");