rx.c 25 KB

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