rx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2005-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/socket.h>
  11. #include <linux/in.h>
  12. #include <linux/slab.h>
  13. #include <linux/ip.h>
  14. #include <linux/ipv6.h>
  15. #include <linux/tcp.h>
  16. #include <linux/udp.h>
  17. #include <linux/prefetch.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/iommu.h>
  20. #include <net/ip.h>
  21. #include <net/checksum.h>
  22. #include "net_driver.h"
  23. #include "efx.h"
  24. #include "filter.h"
  25. #include "nic.h"
  26. #include "selftest.h"
  27. #include "workarounds.h"
  28. /* Preferred number of descriptors to fill at once */
  29. #define EFX_RX_PREFERRED_BATCH 8U
  30. /* Number of RX buffers to recycle pages for. When creating the RX page recycle
  31. * ring, this number is divided by the number of buffers per page to calculate
  32. * the number of pages to store in the RX page recycle ring.
  33. */
  34. #define EFX_RECYCLE_RING_SIZE_IOMMU 4096
  35. #define EFX_RECYCLE_RING_SIZE_NOIOMMU (2 * EFX_RX_PREFERRED_BATCH)
  36. /* Size of buffer allocated for skb header area. */
  37. #define EFX_SKB_HEADERS 128u
  38. /* This is the percentage fill level below which new RX descriptors
  39. * will be added to the RX descriptor ring.
  40. */
  41. static unsigned int rx_refill_threshold;
  42. /* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
  43. #define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
  44. EFX_RX_USR_BUF_SIZE)
  45. /*
  46. * RX maximum head room required.
  47. *
  48. * This must be at least 1 to prevent overflow, plus one packet-worth
  49. * to allow pipelined receives.
  50. */
  51. #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
  52. static inline u8 *efx_rx_buf_va(struct efx_rx_buffer *buf)
  53. {
  54. return page_address(buf->page) + buf->page_offset;
  55. }
  56. static inline u32 efx_rx_buf_hash(struct efx_nic *efx, const u8 *eh)
  57. {
  58. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  59. return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_hash_offset));
  60. #else
  61. const u8 *data = eh + efx->rx_packet_hash_offset;
  62. return (u32)data[0] |
  63. (u32)data[1] << 8 |
  64. (u32)data[2] << 16 |
  65. (u32)data[3] << 24;
  66. #endif
  67. }
  68. static inline struct efx_rx_buffer *
  69. efx_rx_buf_next(struct efx_rx_queue *rx_queue, struct efx_rx_buffer *rx_buf)
  70. {
  71. if (unlikely(rx_buf == efx_rx_buffer(rx_queue, rx_queue->ptr_mask)))
  72. return efx_rx_buffer(rx_queue, 0);
  73. else
  74. return rx_buf + 1;
  75. }
  76. static inline void efx_sync_rx_buffer(struct efx_nic *efx,
  77. struct efx_rx_buffer *rx_buf,
  78. unsigned int len)
  79. {
  80. dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr, len,
  81. DMA_FROM_DEVICE);
  82. }
  83. void efx_rx_config_page_split(struct efx_nic *efx)
  84. {
  85. efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align,
  86. EFX_RX_BUF_ALIGNMENT);
  87. efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
  88. ((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
  89. efx->rx_page_buf_step);
  90. efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
  91. efx->rx_bufs_per_page;
  92. efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
  93. efx->rx_bufs_per_page);
  94. }
  95. /* Check the RX page recycle ring for a page that can be reused. */
  96. static struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
  97. {
  98. struct efx_nic *efx = rx_queue->efx;
  99. struct page *page;
  100. struct efx_rx_page_state *state;
  101. unsigned index;
  102. index = rx_queue->page_remove & rx_queue->page_ptr_mask;
  103. page = rx_queue->page_ring[index];
  104. if (page == NULL)
  105. return NULL;
  106. rx_queue->page_ring[index] = NULL;
  107. /* page_remove cannot exceed page_add. */
  108. if (rx_queue->page_remove != rx_queue->page_add)
  109. ++rx_queue->page_remove;
  110. /* If page_count is 1 then we hold the only reference to this page. */
  111. if (page_count(page) == 1) {
  112. ++rx_queue->page_recycle_count;
  113. return page;
  114. } else {
  115. state = page_address(page);
  116. dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
  117. PAGE_SIZE << efx->rx_buffer_order,
  118. DMA_FROM_DEVICE);
  119. put_page(page);
  120. ++rx_queue->page_recycle_failed;
  121. }
  122. return NULL;
  123. }
  124. /**
  125. * efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
  126. *
  127. * @rx_queue: Efx RX queue
  128. *
  129. * This allocates a batch of pages, maps them for DMA, and populates
  130. * struct efx_rx_buffers for each one. Return a negative error code or
  131. * 0 on success. If a single page can be used for multiple buffers,
  132. * then the page will either be inserted fully, or not at all.
  133. */
  134. static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
  135. {
  136. struct efx_nic *efx = rx_queue->efx;
  137. struct efx_rx_buffer *rx_buf;
  138. struct page *page;
  139. unsigned int page_offset;
  140. struct efx_rx_page_state *state;
  141. dma_addr_t dma_addr;
  142. unsigned index, count;
  143. count = 0;
  144. do {
  145. page = efx_reuse_page(rx_queue);
  146. if (page == NULL) {
  147. page = alloc_pages(__GFP_COLD | __GFP_COMP |
  148. (atomic ? GFP_ATOMIC : GFP_KERNEL),
  149. efx->rx_buffer_order);
  150. if (unlikely(page == NULL))
  151. return -ENOMEM;
  152. dma_addr =
  153. dma_map_page(&efx->pci_dev->dev, page, 0,
  154. PAGE_SIZE << efx->rx_buffer_order,
  155. DMA_FROM_DEVICE);
  156. if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
  157. dma_addr))) {
  158. __free_pages(page, efx->rx_buffer_order);
  159. return -EIO;
  160. }
  161. state = page_address(page);
  162. state->dma_addr = dma_addr;
  163. } else {
  164. state = page_address(page);
  165. dma_addr = state->dma_addr;
  166. }
  167. dma_addr += sizeof(struct efx_rx_page_state);
  168. page_offset = sizeof(struct efx_rx_page_state);
  169. do {
  170. index = rx_queue->added_count & rx_queue->ptr_mask;
  171. rx_buf = efx_rx_buffer(rx_queue, index);
  172. rx_buf->dma_addr = dma_addr + efx->rx_ip_align;
  173. rx_buf->page = page;
  174. rx_buf->page_offset = page_offset + efx->rx_ip_align;
  175. rx_buf->len = efx->rx_dma_len;
  176. rx_buf->flags = 0;
  177. ++rx_queue->added_count;
  178. get_page(page);
  179. dma_addr += efx->rx_page_buf_step;
  180. page_offset += efx->rx_page_buf_step;
  181. } while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
  182. rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
  183. } while (++count < efx->rx_pages_per_batch);
  184. return 0;
  185. }
  186. /* Unmap a DMA-mapped page. This function is only called for the final RX
  187. * buffer in a page.
  188. */
  189. static void efx_unmap_rx_buffer(struct efx_nic *efx,
  190. struct efx_rx_buffer *rx_buf)
  191. {
  192. struct page *page = rx_buf->page;
  193. if (page) {
  194. struct efx_rx_page_state *state = page_address(page);
  195. dma_unmap_page(&efx->pci_dev->dev,
  196. state->dma_addr,
  197. PAGE_SIZE << efx->rx_buffer_order,
  198. DMA_FROM_DEVICE);
  199. }
  200. }
  201. static void efx_free_rx_buffer(struct efx_rx_buffer *rx_buf)
  202. {
  203. if (rx_buf->page) {
  204. put_page(rx_buf->page);
  205. rx_buf->page = NULL;
  206. }
  207. }
  208. /* Attempt to recycle the page if there is an RX recycle ring; the page can
  209. * only be added if this is the final RX buffer, to prevent pages being used in
  210. * the descriptor ring and appearing in the recycle ring simultaneously.
  211. */
  212. static void efx_recycle_rx_page(struct efx_channel *channel,
  213. struct efx_rx_buffer *rx_buf)
  214. {
  215. struct page *page = rx_buf->page;
  216. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  217. struct efx_nic *efx = rx_queue->efx;
  218. unsigned index;
  219. /* Only recycle the page after processing the final buffer. */
  220. if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
  221. return;
  222. index = rx_queue->page_add & rx_queue->page_ptr_mask;
  223. if (rx_queue->page_ring[index] == NULL) {
  224. unsigned read_index = rx_queue->page_remove &
  225. rx_queue->page_ptr_mask;
  226. /* The next slot in the recycle ring is available, but
  227. * increment page_remove if the read pointer currently
  228. * points here.
  229. */
  230. if (read_index == index)
  231. ++rx_queue->page_remove;
  232. rx_queue->page_ring[index] = page;
  233. ++rx_queue->page_add;
  234. return;
  235. }
  236. ++rx_queue->page_recycle_full;
  237. efx_unmap_rx_buffer(efx, rx_buf);
  238. put_page(rx_buf->page);
  239. }
  240. static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  241. struct efx_rx_buffer *rx_buf)
  242. {
  243. /* Release the page reference we hold for the buffer. */
  244. if (rx_buf->page)
  245. put_page(rx_buf->page);
  246. /* If this is the last buffer in a page, unmap and free it. */
  247. if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
  248. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  249. efx_free_rx_buffer(rx_buf);
  250. }
  251. rx_buf->page = NULL;
  252. }
  253. /* Recycle the pages that are used by buffers that have just been received. */
  254. static void efx_recycle_rx_pages(struct efx_channel *channel,
  255. struct efx_rx_buffer *rx_buf,
  256. unsigned int n_frags)
  257. {
  258. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  259. do {
  260. efx_recycle_rx_page(channel, rx_buf);
  261. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  262. } while (--n_frags);
  263. }
  264. static void efx_discard_rx_packet(struct efx_channel *channel,
  265. struct efx_rx_buffer *rx_buf,
  266. unsigned int n_frags)
  267. {
  268. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  269. efx_recycle_rx_pages(channel, rx_buf, n_frags);
  270. do {
  271. efx_free_rx_buffer(rx_buf);
  272. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  273. } while (--n_frags);
  274. }
  275. /**
  276. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  277. * @rx_queue: RX descriptor queue
  278. *
  279. * This will aim to fill the RX descriptor queue up to
  280. * @rx_queue->@max_fill. If there is insufficient atomic
  281. * memory to do so, a slow fill will be scheduled.
  282. *
  283. * The caller must provide serialisation (none is used here). In practise,
  284. * this means this function must run from the NAPI handler, or be called
  285. * when NAPI is disabled.
  286. */
  287. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
  288. {
  289. struct efx_nic *efx = rx_queue->efx;
  290. unsigned int fill_level, batch_size;
  291. int space, rc = 0;
  292. if (!rx_queue->refill_enabled)
  293. return;
  294. /* Calculate current fill level, and exit if we don't need to fill */
  295. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  296. EFX_BUG_ON_PARANOID(fill_level > rx_queue->efx->rxq_entries);
  297. if (fill_level >= rx_queue->fast_fill_trigger)
  298. goto out;
  299. /* Record minimum fill level */
  300. if (unlikely(fill_level < rx_queue->min_fill)) {
  301. if (fill_level)
  302. rx_queue->min_fill = fill_level;
  303. }
  304. batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  305. space = rx_queue->max_fill - fill_level;
  306. EFX_BUG_ON_PARANOID(space < batch_size);
  307. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  308. "RX queue %d fast-filling descriptor ring from"
  309. " level %d to level %d\n",
  310. efx_rx_queue_index(rx_queue), fill_level,
  311. rx_queue->max_fill);
  312. do {
  313. rc = efx_init_rx_buffers(rx_queue, atomic);
  314. if (unlikely(rc)) {
  315. /* Ensure that we don't leave the rx queue empty */
  316. if (rx_queue->added_count == rx_queue->removed_count)
  317. efx_schedule_slow_fill(rx_queue);
  318. goto out;
  319. }
  320. } while ((space -= batch_size) >= batch_size);
  321. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  322. "RX queue %d fast-filled descriptor ring "
  323. "to level %d\n", efx_rx_queue_index(rx_queue),
  324. rx_queue->added_count - rx_queue->removed_count);
  325. out:
  326. if (rx_queue->notified_count != rx_queue->added_count)
  327. efx_nic_notify_rx_desc(rx_queue);
  328. }
  329. void efx_rx_slow_fill(unsigned long context)
  330. {
  331. struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context;
  332. /* Post an event to cause NAPI to run and refill the queue */
  333. efx_nic_generate_fill_event(rx_queue);
  334. ++rx_queue->slow_fill_count;
  335. }
  336. static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  337. struct efx_rx_buffer *rx_buf,
  338. int len)
  339. {
  340. struct efx_nic *efx = rx_queue->efx;
  341. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  342. if (likely(len <= max_len))
  343. return;
  344. /* The packet must be discarded, but this is only a fatal error
  345. * if the caller indicated it was
  346. */
  347. rx_buf->flags |= EFX_RX_PKT_DISCARD;
  348. if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
  349. if (net_ratelimit())
  350. netif_err(efx, rx_err, efx->net_dev,
  351. " RX queue %d seriously overlength "
  352. "RX event (0x%x > 0x%x+0x%x). Leaking\n",
  353. efx_rx_queue_index(rx_queue), len, max_len,
  354. efx->type->rx_buffer_padding);
  355. efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
  356. } else {
  357. if (net_ratelimit())
  358. netif_err(efx, rx_err, efx->net_dev,
  359. " RX queue %d overlength RX event "
  360. "(0x%x > 0x%x)\n",
  361. efx_rx_queue_index(rx_queue), len, max_len);
  362. }
  363. efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
  364. }
  365. /* Pass a received packet up through GRO. GRO can handle pages
  366. * regardless of checksum state and skbs with a good checksum.
  367. */
  368. static void
  369. efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
  370. unsigned int n_frags, u8 *eh)
  371. {
  372. struct napi_struct *napi = &channel->napi_str;
  373. gro_result_t gro_result;
  374. struct efx_nic *efx = channel->efx;
  375. struct sk_buff *skb;
  376. skb = napi_get_frags(napi);
  377. if (unlikely(!skb)) {
  378. while (n_frags--) {
  379. put_page(rx_buf->page);
  380. rx_buf->page = NULL;
  381. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  382. }
  383. return;
  384. }
  385. if (efx->net_dev->features & NETIF_F_RXHASH)
  386. skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
  387. PKT_HASH_TYPE_L3);
  388. skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
  389. CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  390. for (;;) {
  391. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  392. rx_buf->page, rx_buf->page_offset,
  393. rx_buf->len);
  394. rx_buf->page = NULL;
  395. skb->len += rx_buf->len;
  396. if (skb_shinfo(skb)->nr_frags == n_frags)
  397. break;
  398. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  399. }
  400. skb->data_len = skb->len;
  401. skb->truesize += n_frags * efx->rx_buffer_truesize;
  402. skb_record_rx_queue(skb, channel->rx_queue.core_index);
  403. skb_mark_napi_id(skb, &channel->napi_str);
  404. gro_result = napi_gro_frags(napi);
  405. if (gro_result != GRO_DROP)
  406. channel->irq_mod_score += 2;
  407. }
  408. /* Allocate and construct an SKB around page fragments */
  409. static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
  410. struct efx_rx_buffer *rx_buf,
  411. unsigned int n_frags,
  412. u8 *eh, int hdr_len)
  413. {
  414. struct efx_nic *efx = channel->efx;
  415. struct sk_buff *skb;
  416. /* Allocate an SKB to store the headers */
  417. skb = netdev_alloc_skb(efx->net_dev,
  418. efx->rx_ip_align + efx->rx_prefix_size +
  419. hdr_len);
  420. if (unlikely(skb == NULL)) {
  421. atomic_inc(&efx->n_rx_noskb_drops);
  422. return NULL;
  423. }
  424. EFX_BUG_ON_PARANOID(rx_buf->len < hdr_len);
  425. memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
  426. efx->rx_prefix_size + hdr_len);
  427. skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
  428. __skb_put(skb, hdr_len);
  429. /* Append the remaining page(s) onto the frag list */
  430. if (rx_buf->len > hdr_len) {
  431. rx_buf->page_offset += hdr_len;
  432. rx_buf->len -= hdr_len;
  433. for (;;) {
  434. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  435. rx_buf->page, rx_buf->page_offset,
  436. rx_buf->len);
  437. rx_buf->page = NULL;
  438. skb->len += rx_buf->len;
  439. skb->data_len += rx_buf->len;
  440. if (skb_shinfo(skb)->nr_frags == n_frags)
  441. break;
  442. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  443. }
  444. } else {
  445. __free_pages(rx_buf->page, efx->rx_buffer_order);
  446. rx_buf->page = NULL;
  447. n_frags = 0;
  448. }
  449. skb->truesize += n_frags * efx->rx_buffer_truesize;
  450. /* Move past the ethernet header */
  451. skb->protocol = eth_type_trans(skb, efx->net_dev);
  452. skb_mark_napi_id(skb, &channel->napi_str);
  453. return skb;
  454. }
  455. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  456. unsigned int n_frags, unsigned int len, u16 flags)
  457. {
  458. struct efx_nic *efx = rx_queue->efx;
  459. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  460. struct efx_rx_buffer *rx_buf;
  461. rx_queue->rx_packets++;
  462. rx_buf = efx_rx_buffer(rx_queue, index);
  463. rx_buf->flags |= flags;
  464. /* Validate the number of fragments and completed length */
  465. if (n_frags == 1) {
  466. if (!(flags & EFX_RX_PKT_PREFIX_LEN))
  467. efx_rx_packet__check_len(rx_queue, rx_buf, len);
  468. } else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
  469. unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
  470. unlikely(len > n_frags * efx->rx_dma_len) ||
  471. unlikely(!efx->rx_scatter)) {
  472. /* If this isn't an explicit discard request, either
  473. * the hardware or the driver is broken.
  474. */
  475. WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
  476. rx_buf->flags |= EFX_RX_PKT_DISCARD;
  477. }
  478. netif_vdbg(efx, rx_status, efx->net_dev,
  479. "RX queue %d received ids %x-%x len %d %s%s\n",
  480. efx_rx_queue_index(rx_queue), index,
  481. (index + n_frags - 1) & rx_queue->ptr_mask, len,
  482. (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
  483. (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
  484. /* Discard packet, if instructed to do so. Process the
  485. * previous receive first.
  486. */
  487. if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
  488. efx_rx_flush_packet(channel);
  489. efx_discard_rx_packet(channel, rx_buf, n_frags);
  490. return;
  491. }
  492. if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
  493. rx_buf->len = len;
  494. /* Release and/or sync the DMA mapping - assumes all RX buffers
  495. * consumed in-order per RX queue.
  496. */
  497. efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
  498. /* Prefetch nice and early so data will (hopefully) be in cache by
  499. * the time we look at it.
  500. */
  501. prefetch(efx_rx_buf_va(rx_buf));
  502. rx_buf->page_offset += efx->rx_prefix_size;
  503. rx_buf->len -= efx->rx_prefix_size;
  504. if (n_frags > 1) {
  505. /* Release/sync DMA mapping for additional fragments.
  506. * Fix length for last fragment.
  507. */
  508. unsigned int tail_frags = n_frags - 1;
  509. for (;;) {
  510. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  511. if (--tail_frags == 0)
  512. break;
  513. efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
  514. }
  515. rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
  516. efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
  517. }
  518. /* All fragments have been DMA-synced, so recycle pages. */
  519. rx_buf = efx_rx_buffer(rx_queue, index);
  520. efx_recycle_rx_pages(channel, rx_buf, n_frags);
  521. /* Pipeline receives so that we give time for packet headers to be
  522. * prefetched into cache.
  523. */
  524. efx_rx_flush_packet(channel);
  525. channel->rx_pkt_n_frags = n_frags;
  526. channel->rx_pkt_index = index;
  527. }
  528. static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
  529. struct efx_rx_buffer *rx_buf,
  530. unsigned int n_frags)
  531. {
  532. struct sk_buff *skb;
  533. u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
  534. skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
  535. if (unlikely(skb == NULL)) {
  536. efx_free_rx_buffer(rx_buf);
  537. return;
  538. }
  539. skb_record_rx_queue(skb, channel->rx_queue.core_index);
  540. /* Set the SKB flags */
  541. skb_checksum_none_assert(skb);
  542. if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED))
  543. skb->ip_summed = CHECKSUM_UNNECESSARY;
  544. efx_rx_skb_attach_timestamp(channel, skb);
  545. if (channel->type->receive_skb)
  546. if (channel->type->receive_skb(channel, skb))
  547. return;
  548. /* Pass the packet up */
  549. netif_receive_skb(skb);
  550. }
  551. /* Handle a received packet. Second half: Touches packet payload. */
  552. void __efx_rx_packet(struct efx_channel *channel)
  553. {
  554. struct efx_nic *efx = channel->efx;
  555. struct efx_rx_buffer *rx_buf =
  556. efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
  557. u8 *eh = efx_rx_buf_va(rx_buf);
  558. /* Read length from the prefix if necessary. This already
  559. * excludes the length of the prefix itself.
  560. */
  561. if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
  562. rx_buf->len = le16_to_cpup((__le16 *)
  563. (eh + efx->rx_packet_len_offset));
  564. /* If we're in loopback test, then pass the packet directly to the
  565. * loopback layer, and free the rx_buf here
  566. */
  567. if (unlikely(efx->loopback_selftest)) {
  568. efx_loopback_rx_packet(efx, eh, rx_buf->len);
  569. efx_free_rx_buffer(rx_buf);
  570. goto out;
  571. }
  572. if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
  573. rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
  574. if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb &&
  575. !efx_channel_busy_polling(channel))
  576. efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh);
  577. else
  578. efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
  579. out:
  580. channel->rx_pkt_n_frags = 0;
  581. }
  582. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  583. {
  584. struct efx_nic *efx = rx_queue->efx;
  585. unsigned int entries;
  586. int rc;
  587. /* Create the smallest power-of-two aligned ring */
  588. entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
  589. EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  590. rx_queue->ptr_mask = entries - 1;
  591. netif_dbg(efx, probe, efx->net_dev,
  592. "creating RX queue %d size %#x mask %#x\n",
  593. efx_rx_queue_index(rx_queue), efx->rxq_entries,
  594. rx_queue->ptr_mask);
  595. /* Allocate RX buffers */
  596. rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
  597. GFP_KERNEL);
  598. if (!rx_queue->buffer)
  599. return -ENOMEM;
  600. rc = efx_nic_probe_rx(rx_queue);
  601. if (rc) {
  602. kfree(rx_queue->buffer);
  603. rx_queue->buffer = NULL;
  604. }
  605. return rc;
  606. }
  607. static void efx_init_rx_recycle_ring(struct efx_nic *efx,
  608. struct efx_rx_queue *rx_queue)
  609. {
  610. unsigned int bufs_in_recycle_ring, page_ring_size;
  611. /* Set the RX recycle ring size */
  612. #ifdef CONFIG_PPC64
  613. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
  614. #else
  615. if (iommu_present(&pci_bus_type))
  616. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
  617. else
  618. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_NOIOMMU;
  619. #endif /* CONFIG_PPC64 */
  620. page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
  621. efx->rx_bufs_per_page);
  622. rx_queue->page_ring = kcalloc(page_ring_size,
  623. sizeof(*rx_queue->page_ring), GFP_KERNEL);
  624. rx_queue->page_ptr_mask = page_ring_size - 1;
  625. }
  626. void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  627. {
  628. struct efx_nic *efx = rx_queue->efx;
  629. unsigned int max_fill, trigger, max_trigger;
  630. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  631. "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
  632. /* Initialise ptr fields */
  633. rx_queue->added_count = 0;
  634. rx_queue->notified_count = 0;
  635. rx_queue->removed_count = 0;
  636. rx_queue->min_fill = -1U;
  637. efx_init_rx_recycle_ring(efx, rx_queue);
  638. rx_queue->page_remove = 0;
  639. rx_queue->page_add = rx_queue->page_ptr_mask + 1;
  640. rx_queue->page_recycle_count = 0;
  641. rx_queue->page_recycle_failed = 0;
  642. rx_queue->page_recycle_full = 0;
  643. /* Initialise limit fields */
  644. max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
  645. max_trigger =
  646. max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  647. if (rx_refill_threshold != 0) {
  648. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  649. if (trigger > max_trigger)
  650. trigger = max_trigger;
  651. } else {
  652. trigger = max_trigger;
  653. }
  654. rx_queue->max_fill = max_fill;
  655. rx_queue->fast_fill_trigger = trigger;
  656. rx_queue->refill_enabled = true;
  657. /* Set up RX descriptor ring */
  658. efx_nic_init_rx(rx_queue);
  659. }
  660. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  661. {
  662. int i;
  663. struct efx_nic *efx = rx_queue->efx;
  664. struct efx_rx_buffer *rx_buf;
  665. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  666. "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
  667. del_timer_sync(&rx_queue->slow_fill);
  668. /* Release RX buffers from the current read ptr to the write ptr */
  669. if (rx_queue->buffer) {
  670. for (i = rx_queue->removed_count; i < rx_queue->added_count;
  671. i++) {
  672. unsigned index = i & rx_queue->ptr_mask;
  673. rx_buf = efx_rx_buffer(rx_queue, index);
  674. efx_fini_rx_buffer(rx_queue, rx_buf);
  675. }
  676. }
  677. /* Unmap and release the pages in the recycle ring. Remove the ring. */
  678. for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
  679. struct page *page = rx_queue->page_ring[i];
  680. struct efx_rx_page_state *state;
  681. if (page == NULL)
  682. continue;
  683. state = page_address(page);
  684. dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
  685. PAGE_SIZE << efx->rx_buffer_order,
  686. DMA_FROM_DEVICE);
  687. put_page(page);
  688. }
  689. kfree(rx_queue->page_ring);
  690. rx_queue->page_ring = NULL;
  691. }
  692. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  693. {
  694. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  695. "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
  696. efx_nic_remove_rx(rx_queue);
  697. kfree(rx_queue->buffer);
  698. rx_queue->buffer = NULL;
  699. }
  700. module_param(rx_refill_threshold, uint, 0444);
  701. MODULE_PARM_DESC(rx_refill_threshold,
  702. "RX descriptor ring refill threshold (%)");
  703. #ifdef CONFIG_RFS_ACCEL
  704. int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  705. u16 rxq_index, u32 flow_id)
  706. {
  707. struct efx_nic *efx = netdev_priv(net_dev);
  708. struct efx_channel *channel;
  709. struct efx_filter_spec spec;
  710. const __be16 *ports;
  711. __be16 ether_type;
  712. int nhoff;
  713. int rc;
  714. /* The core RPS/RFS code has already parsed and validated
  715. * VLAN, IP and transport headers. We assume they are in the
  716. * header area.
  717. */
  718. if (skb->protocol == htons(ETH_P_8021Q)) {
  719. const struct vlan_hdr *vh =
  720. (const struct vlan_hdr *)skb->data;
  721. /* We can't filter on the IP 5-tuple and the vlan
  722. * together, so just strip the vlan header and filter
  723. * on the IP part.
  724. */
  725. EFX_BUG_ON_PARANOID(skb_headlen(skb) < sizeof(*vh));
  726. ether_type = vh->h_vlan_encapsulated_proto;
  727. nhoff = sizeof(struct vlan_hdr);
  728. } else {
  729. ether_type = skb->protocol;
  730. nhoff = 0;
  731. }
  732. if (ether_type != htons(ETH_P_IP) && ether_type != htons(ETH_P_IPV6))
  733. return -EPROTONOSUPPORT;
  734. efx_filter_init_rx(&spec, EFX_FILTER_PRI_HINT,
  735. efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
  736. rxq_index);
  737. spec.match_flags =
  738. EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  739. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  740. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
  741. spec.ether_type = ether_type;
  742. if (ether_type == htons(ETH_P_IP)) {
  743. const struct iphdr *ip =
  744. (const struct iphdr *)(skb->data + nhoff);
  745. EFX_BUG_ON_PARANOID(skb_headlen(skb) < nhoff + sizeof(*ip));
  746. if (ip_is_fragment(ip))
  747. return -EPROTONOSUPPORT;
  748. spec.ip_proto = ip->protocol;
  749. spec.rem_host[0] = ip->saddr;
  750. spec.loc_host[0] = ip->daddr;
  751. EFX_BUG_ON_PARANOID(skb_headlen(skb) < nhoff + 4 * ip->ihl + 4);
  752. ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
  753. } else {
  754. const struct ipv6hdr *ip6 =
  755. (const struct ipv6hdr *)(skb->data + nhoff);
  756. EFX_BUG_ON_PARANOID(skb_headlen(skb) <
  757. nhoff + sizeof(*ip6) + 4);
  758. spec.ip_proto = ip6->nexthdr;
  759. memcpy(spec.rem_host, &ip6->saddr, sizeof(ip6->saddr));
  760. memcpy(spec.loc_host, &ip6->daddr, sizeof(ip6->daddr));
  761. ports = (const __be16 *)(ip6 + 1);
  762. }
  763. spec.rem_port = ports[0];
  764. spec.loc_port = ports[1];
  765. rc = efx->type->filter_rfs_insert(efx, &spec);
  766. if (rc < 0)
  767. return rc;
  768. /* Remember this so we can check whether to expire the filter later */
  769. efx->rps_flow_id[rc] = flow_id;
  770. channel = efx_get_channel(efx, skb_get_rx_queue(skb));
  771. ++channel->rfs_filters_added;
  772. if (ether_type == htons(ETH_P_IP))
  773. netif_info(efx, rx_status, efx->net_dev,
  774. "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d]\n",
  775. (spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  776. spec.rem_host, ntohs(ports[0]), spec.loc_host,
  777. ntohs(ports[1]), rxq_index, flow_id, rc);
  778. else
  779. netif_info(efx, rx_status, efx->net_dev,
  780. "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d]\n",
  781. (spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  782. spec.rem_host, ntohs(ports[0]), spec.loc_host,
  783. ntohs(ports[1]), rxq_index, flow_id, rc);
  784. return rc;
  785. }
  786. bool __efx_filter_rfs_expire(struct efx_nic *efx, unsigned int quota)
  787. {
  788. bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
  789. unsigned int index, size;
  790. u32 flow_id;
  791. if (!spin_trylock_bh(&efx->filter_lock))
  792. return false;
  793. expire_one = efx->type->filter_rfs_expire_one;
  794. index = efx->rps_expire_index;
  795. size = efx->type->max_rx_ip_filters;
  796. while (quota--) {
  797. flow_id = efx->rps_flow_id[index];
  798. if (expire_one(efx, flow_id, index))
  799. netif_info(efx, rx_status, efx->net_dev,
  800. "expired filter %d [flow %u]\n",
  801. index, flow_id);
  802. if (++index == size)
  803. index = 0;
  804. }
  805. efx->rps_expire_index = index;
  806. spin_unlock_bh(&efx->filter_lock);
  807. return true;
  808. }
  809. #endif /* CONFIG_RFS_ACCEL */
  810. /**
  811. * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
  812. * @spec: Specification to test
  813. *
  814. * Return: %true if the specification is a non-drop RX filter that
  815. * matches a local MAC address I/G bit value of 1 or matches a local
  816. * IPv4 or IPv6 address value in the respective multicast address
  817. * range. Otherwise %false.
  818. */
  819. bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
  820. {
  821. if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
  822. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
  823. return false;
  824. if (spec->match_flags &
  825. (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
  826. is_multicast_ether_addr(spec->loc_mac))
  827. return true;
  828. if ((spec->match_flags &
  829. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  830. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  831. if (spec->ether_type == htons(ETH_P_IP) &&
  832. ipv4_is_multicast(spec->loc_host[0]))
  833. return true;
  834. if (spec->ether_type == htons(ETH_P_IPV6) &&
  835. ((const u8 *)spec->loc_host)[0] == 0xff)
  836. return true;
  837. }
  838. return false;
  839. }