rx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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 "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_PAGE;
  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. /* Macros for zero-order pages (potentially) containing multiple RX buffers */
  80. #define RX_DATA_OFFSET(_data) \
  81. (((unsigned long) (_data)) & (PAGE_SIZE-1))
  82. #define RX_BUF_OFFSET(_rx_buf) \
  83. RX_DATA_OFFSET((_rx_buf)->data)
  84. #define RX_PAGE_SIZE(_efx) \
  85. (PAGE_SIZE * (1u << (_efx)->rx_buffer_order))
  86. /**************************************************************************
  87. *
  88. * Linux generic LRO handling
  89. *
  90. **************************************************************************
  91. */
  92. static int efx_lro_get_skb_hdr(struct sk_buff *skb, void **ip_hdr,
  93. void **tcpudp_hdr, u64 *hdr_flags, void *priv)
  94. {
  95. struct efx_channel *channel = (struct efx_channel *)priv;
  96. struct iphdr *iph;
  97. struct tcphdr *th;
  98. iph = (struct iphdr *)skb->data;
  99. if (skb->protocol != htons(ETH_P_IP) || iph->protocol != IPPROTO_TCP)
  100. goto fail;
  101. th = (struct tcphdr *)(skb->data + iph->ihl * 4);
  102. *tcpudp_hdr = th;
  103. *ip_hdr = iph;
  104. *hdr_flags = LRO_IPV4 | LRO_TCP;
  105. channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
  106. return 0;
  107. fail:
  108. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  109. return -1;
  110. }
  111. static int efx_get_frag_hdr(struct skb_frag_struct *frag, void **mac_hdr,
  112. void **ip_hdr, void **tcpudp_hdr, u64 *hdr_flags,
  113. void *priv)
  114. {
  115. struct efx_channel *channel = (struct efx_channel *)priv;
  116. struct ethhdr *eh;
  117. struct iphdr *iph;
  118. /* We support EtherII and VLAN encapsulated IPv4 */
  119. eh = (struct ethhdr *)(page_address(frag->page) + frag->page_offset);
  120. *mac_hdr = eh;
  121. if (eh->h_proto == htons(ETH_P_IP)) {
  122. iph = (struct iphdr *)(eh + 1);
  123. } else {
  124. struct vlan_ethhdr *veh = (struct vlan_ethhdr *)eh;
  125. if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
  126. goto fail;
  127. iph = (struct iphdr *)(veh + 1);
  128. }
  129. *ip_hdr = iph;
  130. /* We can only do LRO over TCP */
  131. if (iph->protocol != IPPROTO_TCP)
  132. goto fail;
  133. *hdr_flags = LRO_IPV4 | LRO_TCP;
  134. *tcpudp_hdr = (struct tcphdr *)((u8 *) iph + iph->ihl * 4);
  135. channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
  136. return 0;
  137. fail:
  138. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  139. return -1;
  140. }
  141. int efx_lro_init(struct net_lro_mgr *lro_mgr, struct efx_nic *efx)
  142. {
  143. size_t s = sizeof(struct net_lro_desc) * EFX_MAX_LRO_DESCRIPTORS;
  144. struct net_lro_desc *lro_arr;
  145. /* Allocate the LRO descriptors structure */
  146. lro_arr = kzalloc(s, GFP_KERNEL);
  147. if (lro_arr == NULL)
  148. return -ENOMEM;
  149. lro_mgr->lro_arr = lro_arr;
  150. lro_mgr->max_desc = EFX_MAX_LRO_DESCRIPTORS;
  151. lro_mgr->max_aggr = EFX_MAX_LRO_AGGR;
  152. lro_mgr->frag_align_pad = EFX_PAGE_SKB_ALIGN;
  153. lro_mgr->get_skb_header = efx_lro_get_skb_hdr;
  154. lro_mgr->get_frag_header = efx_get_frag_hdr;
  155. lro_mgr->dev = efx->net_dev;
  156. lro_mgr->features = LRO_F_NAPI;
  157. /* We can pass packets up with the checksum intact */
  158. lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
  159. lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
  160. return 0;
  161. }
  162. void efx_lro_fini(struct net_lro_mgr *lro_mgr)
  163. {
  164. kfree(lro_mgr->lro_arr);
  165. lro_mgr->lro_arr = NULL;
  166. }
  167. /**
  168. * efx_init_rx_buffer_skb - create new RX buffer using skb-based allocation
  169. *
  170. * @rx_queue: Efx RX queue
  171. * @rx_buf: RX buffer structure to populate
  172. *
  173. * This allocates memory for a new receive buffer, maps it for DMA,
  174. * and populates a struct efx_rx_buffer with the relevant
  175. * information. Return a negative error code or 0 on success.
  176. */
  177. static inline int efx_init_rx_buffer_skb(struct efx_rx_queue *rx_queue,
  178. struct efx_rx_buffer *rx_buf)
  179. {
  180. struct efx_nic *efx = rx_queue->efx;
  181. struct net_device *net_dev = efx->net_dev;
  182. int skb_len = efx->rx_buffer_len;
  183. rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
  184. if (unlikely(!rx_buf->skb))
  185. return -ENOMEM;
  186. /* Adjust the SKB for padding and checksum */
  187. skb_reserve(rx_buf->skb, NET_IP_ALIGN);
  188. rx_buf->len = skb_len - NET_IP_ALIGN;
  189. rx_buf->data = (char *)rx_buf->skb->data;
  190. rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
  191. rx_buf->dma_addr = pci_map_single(efx->pci_dev,
  192. rx_buf->data, rx_buf->len,
  193. PCI_DMA_FROMDEVICE);
  194. if (unlikely(pci_dma_mapping_error(rx_buf->dma_addr))) {
  195. dev_kfree_skb_any(rx_buf->skb);
  196. rx_buf->skb = NULL;
  197. return -EIO;
  198. }
  199. return 0;
  200. }
  201. /**
  202. * efx_init_rx_buffer_page - create new RX buffer using page-based allocation
  203. *
  204. * @rx_queue: Efx RX queue
  205. * @rx_buf: RX buffer structure to populate
  206. *
  207. * This allocates memory for a new receive buffer, maps it for DMA,
  208. * and populates a struct efx_rx_buffer with the relevant
  209. * information. Return a negative error code or 0 on success.
  210. */
  211. static inline int efx_init_rx_buffer_page(struct efx_rx_queue *rx_queue,
  212. struct efx_rx_buffer *rx_buf)
  213. {
  214. struct efx_nic *efx = rx_queue->efx;
  215. int bytes, space, offset;
  216. bytes = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
  217. /* If there is space left in the previously allocated page,
  218. * then use it. Otherwise allocate a new one */
  219. rx_buf->page = rx_queue->buf_page;
  220. if (rx_buf->page == NULL) {
  221. dma_addr_t dma_addr;
  222. rx_buf->page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
  223. efx->rx_buffer_order);
  224. if (unlikely(rx_buf->page == NULL))
  225. return -ENOMEM;
  226. dma_addr = pci_map_page(efx->pci_dev, rx_buf->page,
  227. 0, RX_PAGE_SIZE(efx),
  228. PCI_DMA_FROMDEVICE);
  229. if (unlikely(pci_dma_mapping_error(dma_addr))) {
  230. __free_pages(rx_buf->page, efx->rx_buffer_order);
  231. rx_buf->page = NULL;
  232. return -EIO;
  233. }
  234. rx_queue->buf_page = rx_buf->page;
  235. rx_queue->buf_dma_addr = dma_addr;
  236. rx_queue->buf_data = ((char *) page_address(rx_buf->page) +
  237. EFX_PAGE_IP_ALIGN);
  238. }
  239. offset = RX_DATA_OFFSET(rx_queue->buf_data);
  240. rx_buf->len = bytes;
  241. rx_buf->dma_addr = rx_queue->buf_dma_addr + offset;
  242. rx_buf->data = rx_queue->buf_data;
  243. /* Try to pack multiple buffers per page */
  244. if (efx->rx_buffer_order == 0) {
  245. /* The next buffer starts on the next 512 byte boundary */
  246. rx_queue->buf_data += ((bytes + 0x1ff) & ~0x1ff);
  247. offset += ((bytes + 0x1ff) & ~0x1ff);
  248. space = RX_PAGE_SIZE(efx) - offset;
  249. if (space >= bytes) {
  250. /* Refs dropped on kernel releasing each skb */
  251. get_page(rx_queue->buf_page);
  252. goto out;
  253. }
  254. }
  255. /* This is the final RX buffer for this page, so mark it for
  256. * unmapping */
  257. rx_queue->buf_page = NULL;
  258. rx_buf->unmap_addr = rx_queue->buf_dma_addr;
  259. out:
  260. return 0;
  261. }
  262. /* This allocates memory for a new receive buffer, maps it for DMA,
  263. * and populates a struct efx_rx_buffer with the relevant
  264. * information.
  265. */
  266. static inline int efx_init_rx_buffer(struct efx_rx_queue *rx_queue,
  267. struct efx_rx_buffer *new_rx_buf)
  268. {
  269. int rc = 0;
  270. if (rx_queue->channel->rx_alloc_push_pages) {
  271. new_rx_buf->skb = NULL;
  272. rc = efx_init_rx_buffer_page(rx_queue, new_rx_buf);
  273. rx_queue->alloc_page_count++;
  274. } else {
  275. new_rx_buf->page = NULL;
  276. rc = efx_init_rx_buffer_skb(rx_queue, new_rx_buf);
  277. rx_queue->alloc_skb_count++;
  278. }
  279. if (unlikely(rc < 0))
  280. EFX_LOG_RL(rx_queue->efx, "%s RXQ[%d] =%d\n", __func__,
  281. rx_queue->queue, rc);
  282. return rc;
  283. }
  284. static inline void efx_unmap_rx_buffer(struct efx_nic *efx,
  285. struct efx_rx_buffer *rx_buf)
  286. {
  287. if (rx_buf->page) {
  288. EFX_BUG_ON_PARANOID(rx_buf->skb);
  289. if (rx_buf->unmap_addr) {
  290. pci_unmap_page(efx->pci_dev, rx_buf->unmap_addr,
  291. RX_PAGE_SIZE(efx), PCI_DMA_FROMDEVICE);
  292. rx_buf->unmap_addr = 0;
  293. }
  294. } else if (likely(rx_buf->skb)) {
  295. pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
  296. rx_buf->len, PCI_DMA_FROMDEVICE);
  297. }
  298. }
  299. static inline void efx_free_rx_buffer(struct efx_nic *efx,
  300. struct efx_rx_buffer *rx_buf)
  301. {
  302. if (rx_buf->page) {
  303. __free_pages(rx_buf->page, efx->rx_buffer_order);
  304. rx_buf->page = NULL;
  305. } else if (likely(rx_buf->skb)) {
  306. dev_kfree_skb_any(rx_buf->skb);
  307. rx_buf->skb = NULL;
  308. }
  309. }
  310. static inline void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  311. struct efx_rx_buffer *rx_buf)
  312. {
  313. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  314. efx_free_rx_buffer(rx_queue->efx, rx_buf);
  315. }
  316. /**
  317. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  318. * @rx_queue: RX descriptor queue
  319. * @retry: Recheck the fill level
  320. * This will aim to fill the RX descriptor queue up to
  321. * @rx_queue->@fast_fill_limit. If there is insufficient atomic
  322. * memory to do so, the caller should retry.
  323. */
  324. static int __efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
  325. int retry)
  326. {
  327. struct efx_rx_buffer *rx_buf;
  328. unsigned fill_level, index;
  329. int i, space, rc = 0;
  330. /* Calculate current fill level. Do this outside the lock,
  331. * because most of the time we'll end up not wanting to do the
  332. * fill anyway.
  333. */
  334. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  335. EFX_BUG_ON_PARANOID(fill_level >
  336. rx_queue->efx->type->rxd_ring_mask + 1);
  337. /* Don't fill if we don't need to */
  338. if (fill_level >= rx_queue->fast_fill_trigger)
  339. return 0;
  340. /* Record minimum fill level */
  341. if (unlikely(fill_level < rx_queue->min_fill))
  342. if (fill_level)
  343. rx_queue->min_fill = fill_level;
  344. /* Acquire RX add lock. If this lock is contended, then a fast
  345. * fill must already be in progress (e.g. in the refill
  346. * tasklet), so we don't need to do anything
  347. */
  348. if (!spin_trylock_bh(&rx_queue->add_lock))
  349. return -1;
  350. retry:
  351. /* Recalculate current fill level now that we have the lock */
  352. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  353. EFX_BUG_ON_PARANOID(fill_level >
  354. rx_queue->efx->type->rxd_ring_mask + 1);
  355. space = rx_queue->fast_fill_limit - fill_level;
  356. if (space < EFX_RX_BATCH)
  357. goto out_unlock;
  358. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
  359. " level %d to level %d using %s allocation\n",
  360. rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
  361. rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
  362. do {
  363. for (i = 0; i < EFX_RX_BATCH; ++i) {
  364. index = (rx_queue->added_count &
  365. rx_queue->efx->type->rxd_ring_mask);
  366. rx_buf = efx_rx_buffer(rx_queue, index);
  367. rc = efx_init_rx_buffer(rx_queue, rx_buf);
  368. if (unlikely(rc))
  369. goto out;
  370. ++rx_queue->added_count;
  371. }
  372. } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
  373. EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
  374. "to level %d\n", rx_queue->queue,
  375. rx_queue->added_count - rx_queue->removed_count);
  376. out:
  377. /* Send write pointer to card. */
  378. falcon_notify_rx_desc(rx_queue);
  379. /* If the fast fill is running inside from the refill tasklet, then
  380. * for SMP systems it may be running on a different CPU to
  381. * RX event processing, which means that the fill level may now be
  382. * out of date. */
  383. if (unlikely(retry && (rc == 0)))
  384. goto retry;
  385. out_unlock:
  386. spin_unlock_bh(&rx_queue->add_lock);
  387. return rc;
  388. }
  389. /**
  390. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  391. * @rx_queue: RX descriptor queue
  392. *
  393. * This will aim to fill the RX descriptor queue up to
  394. * @rx_queue->@fast_fill_limit. If there is insufficient memory to do so,
  395. * it will schedule a work item to immediately continue the fast fill
  396. */
  397. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
  398. {
  399. int rc;
  400. rc = __efx_fast_push_rx_descriptors(rx_queue, 0);
  401. if (unlikely(rc)) {
  402. /* Schedule the work item to run immediately. The hope is
  403. * that work is immediately pending to free some memory
  404. * (e.g. an RX event or TX completion)
  405. */
  406. efx_schedule_slow_fill(rx_queue, 0);
  407. }
  408. }
  409. void efx_rx_work(struct work_struct *data)
  410. {
  411. struct efx_rx_queue *rx_queue;
  412. int rc;
  413. rx_queue = container_of(data, struct efx_rx_queue, work.work);
  414. if (unlikely(!rx_queue->channel->enabled))
  415. return;
  416. EFX_TRACE(rx_queue->efx, "RX queue %d worker thread executing on CPU "
  417. "%d\n", rx_queue->queue, raw_smp_processor_id());
  418. ++rx_queue->slow_fill_count;
  419. /* Push new RX descriptors, allowing at least 1 jiffy for
  420. * the kernel to free some more memory. */
  421. rc = __efx_fast_push_rx_descriptors(rx_queue, 1);
  422. if (rc)
  423. efx_schedule_slow_fill(rx_queue, 1);
  424. }
  425. static inline void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  426. struct efx_rx_buffer *rx_buf,
  427. int len, int *discard,
  428. int *leak_packet)
  429. {
  430. struct efx_nic *efx = rx_queue->efx;
  431. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  432. if (likely(len <= max_len))
  433. return;
  434. /* The packet must be discarded, but this is only a fatal error
  435. * if the caller indicated it was
  436. */
  437. *discard = 1;
  438. if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
  439. EFX_ERR_RL(efx, " RX queue %d seriously overlength "
  440. "RX event (0x%x > 0x%x+0x%x). Leaking\n",
  441. rx_queue->queue, len, max_len,
  442. efx->type->rx_buffer_padding);
  443. /* If this buffer was skb-allocated, then the meta
  444. * data at the end of the skb will be trashed. So
  445. * we have no choice but to leak the fragment.
  446. */
  447. *leak_packet = (rx_buf->skb != NULL);
  448. efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
  449. } else {
  450. EFX_ERR_RL(efx, " RX queue %d overlength RX event "
  451. "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
  452. }
  453. rx_queue->channel->n_rx_overlength++;
  454. }
  455. /* Pass a received packet up through the generic LRO stack
  456. *
  457. * Handles driverlink veto, and passes the fragment up via
  458. * the appropriate LRO method
  459. */
  460. static inline void efx_rx_packet_lro(struct efx_channel *channel,
  461. struct efx_rx_buffer *rx_buf)
  462. {
  463. struct net_lro_mgr *lro_mgr = &channel->lro_mgr;
  464. void *priv = channel;
  465. /* Pass the skb/page into the LRO engine */
  466. if (rx_buf->page) {
  467. struct skb_frag_struct frags;
  468. frags.page = rx_buf->page;
  469. frags.page_offset = RX_BUF_OFFSET(rx_buf);
  470. frags.size = rx_buf->len;
  471. lro_receive_frags(lro_mgr, &frags, rx_buf->len,
  472. rx_buf->len, priv, 0);
  473. EFX_BUG_ON_PARANOID(rx_buf->skb);
  474. rx_buf->page = NULL;
  475. } else {
  476. EFX_BUG_ON_PARANOID(!rx_buf->skb);
  477. lro_receive_skb(lro_mgr, rx_buf->skb, priv);
  478. rx_buf->skb = NULL;
  479. }
  480. }
  481. /* Allocate and construct an SKB around a struct page.*/
  482. static inline struct sk_buff *efx_rx_mk_skb(struct efx_rx_buffer *rx_buf,
  483. struct efx_nic *efx,
  484. int hdr_len)
  485. {
  486. struct sk_buff *skb;
  487. /* Allocate an SKB to store the headers */
  488. skb = netdev_alloc_skb(efx->net_dev, hdr_len + EFX_PAGE_SKB_ALIGN);
  489. if (unlikely(skb == NULL)) {
  490. EFX_ERR_RL(efx, "RX out of memory for skb\n");
  491. return NULL;
  492. }
  493. EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags);
  494. EFX_BUG_ON_PARANOID(rx_buf->len < hdr_len);
  495. skb->ip_summed = CHECKSUM_UNNECESSARY;
  496. skb_reserve(skb, EFX_PAGE_SKB_ALIGN);
  497. skb->len = rx_buf->len;
  498. skb->truesize = rx_buf->len + sizeof(struct sk_buff);
  499. memcpy(skb->data, rx_buf->data, hdr_len);
  500. skb->tail += hdr_len;
  501. /* Append the remaining page onto the frag list */
  502. if (unlikely(rx_buf->len > hdr_len)) {
  503. struct skb_frag_struct *frag = skb_shinfo(skb)->frags;
  504. frag->page = rx_buf->page;
  505. frag->page_offset = RX_BUF_OFFSET(rx_buf) + hdr_len;
  506. frag->size = skb->len - hdr_len;
  507. skb_shinfo(skb)->nr_frags = 1;
  508. skb->data_len = frag->size;
  509. } else {
  510. __free_pages(rx_buf->page, efx->rx_buffer_order);
  511. skb->data_len = 0;
  512. }
  513. /* Ownership has transferred from the rx_buf to skb */
  514. rx_buf->page = NULL;
  515. /* Move past the ethernet header */
  516. skb->protocol = eth_type_trans(skb, efx->net_dev);
  517. return skb;
  518. }
  519. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  520. unsigned int len, int checksummed, int discard)
  521. {
  522. struct efx_nic *efx = rx_queue->efx;
  523. struct efx_rx_buffer *rx_buf;
  524. int leak_packet = 0;
  525. rx_buf = efx_rx_buffer(rx_queue, index);
  526. EFX_BUG_ON_PARANOID(!rx_buf->data);
  527. EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
  528. EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
  529. /* This allows the refill path to post another buffer.
  530. * EFX_RXD_HEAD_ROOM ensures that the slot we are using
  531. * isn't overwritten yet.
  532. */
  533. rx_queue->removed_count++;
  534. /* Validate the length encoded in the event vs the descriptor pushed */
  535. efx_rx_packet__check_len(rx_queue, rx_buf, len,
  536. &discard, &leak_packet);
  537. EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
  538. rx_queue->queue, index,
  539. (unsigned long long)rx_buf->dma_addr, len,
  540. (checksummed ? " [SUMMED]" : ""),
  541. (discard ? " [DISCARD]" : ""));
  542. /* Discard packet, if instructed to do so */
  543. if (unlikely(discard)) {
  544. if (unlikely(leak_packet))
  545. rx_queue->channel->n_skbuff_leaks++;
  546. else
  547. /* We haven't called efx_unmap_rx_buffer yet,
  548. * so fini the entire rx_buffer here */
  549. efx_fini_rx_buffer(rx_queue, rx_buf);
  550. return;
  551. }
  552. /* Release card resources - assumes all RX buffers consumed in-order
  553. * per RX queue
  554. */
  555. efx_unmap_rx_buffer(efx, rx_buf);
  556. /* Prefetch nice and early so data will (hopefully) be in cache by
  557. * the time we look at it.
  558. */
  559. prefetch(rx_buf->data);
  560. /* Pipeline receives so that we give time for packet headers to be
  561. * prefetched into cache.
  562. */
  563. rx_buf->len = len;
  564. if (rx_queue->channel->rx_pkt)
  565. __efx_rx_packet(rx_queue->channel,
  566. rx_queue->channel->rx_pkt,
  567. rx_queue->channel->rx_pkt_csummed);
  568. rx_queue->channel->rx_pkt = rx_buf;
  569. rx_queue->channel->rx_pkt_csummed = checksummed;
  570. }
  571. /* Handle a received packet. Second half: Touches packet payload. */
  572. void __efx_rx_packet(struct efx_channel *channel,
  573. struct efx_rx_buffer *rx_buf, int checksummed)
  574. {
  575. struct efx_nic *efx = channel->efx;
  576. struct sk_buff *skb;
  577. int lro = efx->net_dev->features & NETIF_F_LRO;
  578. if (rx_buf->skb) {
  579. prefetch(skb_shinfo(rx_buf->skb));
  580. skb_put(rx_buf->skb, rx_buf->len);
  581. /* Move past the ethernet header. rx_buf->data still points
  582. * at the ethernet header */
  583. rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
  584. efx->net_dev);
  585. }
  586. /* Both our generic-LRO and SFC-SSR support skb and page based
  587. * allocation, but neither support switching from one to the
  588. * other on the fly. If we spot that the allocation mode has
  589. * changed, then flush the LRO state.
  590. */
  591. if (unlikely(channel->rx_alloc_pop_pages != (rx_buf->page != NULL))) {
  592. efx_flush_lro(channel);
  593. channel->rx_alloc_pop_pages = (rx_buf->page != NULL);
  594. }
  595. if (likely(checksummed && lro)) {
  596. efx_rx_packet_lro(channel, rx_buf);
  597. goto done;
  598. }
  599. /* Form an skb if required */
  600. if (rx_buf->page) {
  601. int hdr_len = min(rx_buf->len, EFX_SKB_HEADERS);
  602. skb = efx_rx_mk_skb(rx_buf, efx, hdr_len);
  603. if (unlikely(skb == NULL)) {
  604. efx_free_rx_buffer(efx, rx_buf);
  605. goto done;
  606. }
  607. } else {
  608. /* We now own the SKB */
  609. skb = rx_buf->skb;
  610. rx_buf->skb = NULL;
  611. }
  612. EFX_BUG_ON_PARANOID(rx_buf->page);
  613. EFX_BUG_ON_PARANOID(rx_buf->skb);
  614. EFX_BUG_ON_PARANOID(!skb);
  615. /* Set the SKB flags */
  616. if (unlikely(!checksummed || !efx->rx_checksum_enabled))
  617. skb->ip_summed = CHECKSUM_NONE;
  618. /* Pass the packet up */
  619. netif_receive_skb(skb);
  620. /* Update allocation strategy method */
  621. channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
  622. /* fall-thru */
  623. done:
  624. efx->net_dev->last_rx = jiffies;
  625. }
  626. void efx_rx_strategy(struct efx_channel *channel)
  627. {
  628. enum efx_rx_alloc_method method = rx_alloc_method;
  629. /* Only makes sense to use page based allocation if LRO is enabled */
  630. if (!(channel->efx->net_dev->features & NETIF_F_LRO)) {
  631. method = RX_ALLOC_METHOD_SKB;
  632. } else if (method == RX_ALLOC_METHOD_AUTO) {
  633. /* Constrain the rx_alloc_level */
  634. if (channel->rx_alloc_level < 0)
  635. channel->rx_alloc_level = 0;
  636. else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
  637. channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
  638. /* Decide on the allocation method */
  639. method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
  640. RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
  641. }
  642. /* Push the option */
  643. channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
  644. }
  645. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  646. {
  647. struct efx_nic *efx = rx_queue->efx;
  648. unsigned int rxq_size;
  649. int rc;
  650. EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
  651. /* Allocate RX buffers */
  652. rxq_size = (efx->type->rxd_ring_mask + 1) * sizeof(*rx_queue->buffer);
  653. rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
  654. if (!rx_queue->buffer) {
  655. rc = -ENOMEM;
  656. goto fail1;
  657. }
  658. rc = falcon_probe_rx(rx_queue);
  659. if (rc)
  660. goto fail2;
  661. return 0;
  662. fail2:
  663. kfree(rx_queue->buffer);
  664. rx_queue->buffer = NULL;
  665. fail1:
  666. rx_queue->used = 0;
  667. return rc;
  668. }
  669. int efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  670. {
  671. struct efx_nic *efx = rx_queue->efx;
  672. unsigned int max_fill, trigger, limit;
  673. EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
  674. /* Initialise ptr fields */
  675. rx_queue->added_count = 0;
  676. rx_queue->notified_count = 0;
  677. rx_queue->removed_count = 0;
  678. rx_queue->min_fill = -1U;
  679. rx_queue->min_overfill = -1U;
  680. /* Initialise limit fields */
  681. max_fill = efx->type->rxd_ring_mask + 1 - EFX_RXD_HEAD_ROOM;
  682. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  683. limit = max_fill * min(rx_refill_limit, 100U) / 100U;
  684. rx_queue->max_fill = max_fill;
  685. rx_queue->fast_fill_trigger = trigger;
  686. rx_queue->fast_fill_limit = limit;
  687. /* Set up RX descriptor ring */
  688. return falcon_init_rx(rx_queue);
  689. }
  690. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  691. {
  692. int i;
  693. struct efx_rx_buffer *rx_buf;
  694. EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
  695. falcon_fini_rx(rx_queue);
  696. /* Release RX buffers NB start at index 0 not current HW ptr */
  697. if (rx_queue->buffer) {
  698. for (i = 0; i <= rx_queue->efx->type->rxd_ring_mask; i++) {
  699. rx_buf = efx_rx_buffer(rx_queue, i);
  700. efx_fini_rx_buffer(rx_queue, rx_buf);
  701. }
  702. }
  703. /* For a page that is part-way through splitting into RX buffers */
  704. if (rx_queue->buf_page != NULL) {
  705. pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
  706. RX_PAGE_SIZE(rx_queue->efx), PCI_DMA_FROMDEVICE);
  707. __free_pages(rx_queue->buf_page,
  708. rx_queue->efx->rx_buffer_order);
  709. rx_queue->buf_page = NULL;
  710. }
  711. }
  712. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  713. {
  714. EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
  715. falcon_remove_rx(rx_queue);
  716. kfree(rx_queue->buffer);
  717. rx_queue->buffer = NULL;
  718. rx_queue->used = 0;
  719. }
  720. void efx_flush_lro(struct efx_channel *channel)
  721. {
  722. lro_flush_all(&channel->lro_mgr);
  723. }
  724. module_param(rx_alloc_method, int, 0644);
  725. MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
  726. module_param(rx_refill_threshold, uint, 0444);
  727. MODULE_PARM_DESC(rx_refill_threshold,
  728. "RX descriptor ring fast/slow fill threshold (%)");