rx.c 25 KB

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