rx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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_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_buffers(struct efx_rx_queue *rx_queue,
  202. struct efx_rx_buffer *rx_buf,
  203. unsigned int num_bufs)
  204. {
  205. do {
  206. if (rx_buf->page) {
  207. put_page(rx_buf->page);
  208. rx_buf->page = NULL;
  209. }
  210. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  211. } while (--num_bufs);
  212. }
  213. /* Attempt to recycle the page if there is an RX recycle ring; the page can
  214. * only be added if this is the final RX buffer, to prevent pages being used in
  215. * the descriptor ring and appearing in the recycle ring simultaneously.
  216. */
  217. static void efx_recycle_rx_page(struct efx_channel *channel,
  218. struct efx_rx_buffer *rx_buf)
  219. {
  220. struct page *page = rx_buf->page;
  221. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  222. struct efx_nic *efx = rx_queue->efx;
  223. unsigned index;
  224. /* Only recycle the page after processing the final buffer. */
  225. if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
  226. return;
  227. index = rx_queue->page_add & rx_queue->page_ptr_mask;
  228. if (rx_queue->page_ring[index] == NULL) {
  229. unsigned read_index = rx_queue->page_remove &
  230. rx_queue->page_ptr_mask;
  231. /* The next slot in the recycle ring is available, but
  232. * increment page_remove if the read pointer currently
  233. * points here.
  234. */
  235. if (read_index == index)
  236. ++rx_queue->page_remove;
  237. rx_queue->page_ring[index] = page;
  238. ++rx_queue->page_add;
  239. return;
  240. }
  241. ++rx_queue->page_recycle_full;
  242. efx_unmap_rx_buffer(efx, rx_buf);
  243. put_page(rx_buf->page);
  244. }
  245. static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
  246. struct efx_rx_buffer *rx_buf)
  247. {
  248. /* Release the page reference we hold for the buffer. */
  249. if (rx_buf->page)
  250. put_page(rx_buf->page);
  251. /* If this is the last buffer in a page, unmap and free it. */
  252. if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
  253. efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
  254. efx_free_rx_buffers(rx_queue, rx_buf, 1);
  255. }
  256. rx_buf->page = NULL;
  257. }
  258. /* Recycle the pages that are used by buffers that have just been received. */
  259. static void efx_recycle_rx_pages(struct efx_channel *channel,
  260. struct efx_rx_buffer *rx_buf,
  261. unsigned int n_frags)
  262. {
  263. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  264. do {
  265. efx_recycle_rx_page(channel, rx_buf);
  266. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  267. } while (--n_frags);
  268. }
  269. static void efx_discard_rx_packet(struct efx_channel *channel,
  270. struct efx_rx_buffer *rx_buf,
  271. unsigned int n_frags)
  272. {
  273. struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
  274. efx_recycle_rx_pages(channel, rx_buf, n_frags);
  275. efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
  276. }
  277. /**
  278. * efx_fast_push_rx_descriptors - push new RX descriptors quickly
  279. * @rx_queue: RX descriptor queue
  280. *
  281. * This will aim to fill the RX descriptor queue up to
  282. * @rx_queue->@max_fill. If there is insufficient atomic
  283. * memory to do so, a slow fill will be scheduled.
  284. *
  285. * The caller must provide serialisation (none is used here). In practise,
  286. * this means this function must run from the NAPI handler, or be called
  287. * when NAPI is disabled.
  288. */
  289. void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
  290. {
  291. struct efx_nic *efx = rx_queue->efx;
  292. unsigned int fill_level, batch_size;
  293. int space, rc = 0;
  294. if (!rx_queue->refill_enabled)
  295. return;
  296. /* Calculate current fill level, and exit if we don't need to fill */
  297. fill_level = (rx_queue->added_count - rx_queue->removed_count);
  298. EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
  299. if (fill_level >= rx_queue->fast_fill_trigger)
  300. goto out;
  301. /* Record minimum fill level */
  302. if (unlikely(fill_level < rx_queue->min_fill)) {
  303. if (fill_level)
  304. rx_queue->min_fill = fill_level;
  305. }
  306. batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  307. space = rx_queue->max_fill - fill_level;
  308. EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
  309. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  310. "RX queue %d fast-filling descriptor ring from"
  311. " level %d to level %d\n",
  312. efx_rx_queue_index(rx_queue), fill_level,
  313. rx_queue->max_fill);
  314. do {
  315. rc = efx_init_rx_buffers(rx_queue, atomic);
  316. if (unlikely(rc)) {
  317. /* Ensure that we don't leave the rx queue empty */
  318. if (rx_queue->added_count == rx_queue->removed_count)
  319. efx_schedule_slow_fill(rx_queue);
  320. goto out;
  321. }
  322. } while ((space -= batch_size) >= batch_size);
  323. netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
  324. "RX queue %d fast-filled descriptor ring "
  325. "to level %d\n", efx_rx_queue_index(rx_queue),
  326. rx_queue->added_count - rx_queue->removed_count);
  327. out:
  328. if (rx_queue->notified_count != rx_queue->added_count)
  329. efx_nic_notify_rx_desc(rx_queue);
  330. }
  331. void efx_rx_slow_fill(struct timer_list *t)
  332. {
  333. struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
  334. /* Post an event to cause NAPI to run and refill the queue */
  335. efx_nic_generate_fill_event(rx_queue);
  336. ++rx_queue->slow_fill_count;
  337. }
  338. static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
  339. struct efx_rx_buffer *rx_buf,
  340. int len)
  341. {
  342. struct efx_nic *efx = rx_queue->efx;
  343. unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
  344. if (likely(len <= max_len))
  345. return;
  346. /* The packet must be discarded, but this is only a fatal error
  347. * if the caller indicated it was
  348. */
  349. rx_buf->flags |= EFX_RX_PKT_DISCARD;
  350. if (net_ratelimit())
  351. netif_err(efx, rx_err, efx->net_dev,
  352. "RX queue %d overlength RX event (%#x > %#x)\n",
  353. efx_rx_queue_index(rx_queue), len, max_len);
  354. efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
  355. }
  356. /* Pass a received packet up through GRO. GRO can handle pages
  357. * regardless of checksum state and skbs with a good checksum.
  358. */
  359. static void
  360. efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
  361. unsigned int n_frags, u8 *eh)
  362. {
  363. struct napi_struct *napi = &channel->napi_str;
  364. gro_result_t gro_result;
  365. struct efx_nic *efx = channel->efx;
  366. struct sk_buff *skb;
  367. skb = napi_get_frags(napi);
  368. if (unlikely(!skb)) {
  369. struct efx_rx_queue *rx_queue;
  370. rx_queue = efx_channel_get_rx_queue(channel);
  371. efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
  372. return;
  373. }
  374. if (efx->net_dev->features & NETIF_F_RXHASH)
  375. skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
  376. PKT_HASH_TYPE_L3);
  377. skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
  378. CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  379. skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
  380. for (;;) {
  381. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  382. rx_buf->page, rx_buf->page_offset,
  383. rx_buf->len);
  384. rx_buf->page = NULL;
  385. skb->len += rx_buf->len;
  386. if (skb_shinfo(skb)->nr_frags == n_frags)
  387. break;
  388. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  389. }
  390. skb->data_len = skb->len;
  391. skb->truesize += n_frags * efx->rx_buffer_truesize;
  392. skb_record_rx_queue(skb, channel->rx_queue.core_index);
  393. gro_result = napi_gro_frags(napi);
  394. if (gro_result != GRO_DROP)
  395. channel->irq_mod_score += 2;
  396. }
  397. /* Allocate and construct an SKB around page fragments */
  398. static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
  399. struct efx_rx_buffer *rx_buf,
  400. unsigned int n_frags,
  401. u8 *eh, int hdr_len)
  402. {
  403. struct efx_nic *efx = channel->efx;
  404. struct sk_buff *skb;
  405. /* Allocate an SKB to store the headers */
  406. skb = netdev_alloc_skb(efx->net_dev,
  407. efx->rx_ip_align + efx->rx_prefix_size +
  408. hdr_len);
  409. if (unlikely(skb == NULL)) {
  410. atomic_inc(&efx->n_rx_noskb_drops);
  411. return NULL;
  412. }
  413. EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
  414. memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
  415. efx->rx_prefix_size + hdr_len);
  416. skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
  417. __skb_put(skb, hdr_len);
  418. /* Append the remaining page(s) onto the frag list */
  419. if (rx_buf->len > hdr_len) {
  420. rx_buf->page_offset += hdr_len;
  421. rx_buf->len -= hdr_len;
  422. for (;;) {
  423. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  424. rx_buf->page, rx_buf->page_offset,
  425. rx_buf->len);
  426. rx_buf->page = NULL;
  427. skb->len += rx_buf->len;
  428. skb->data_len += rx_buf->len;
  429. if (skb_shinfo(skb)->nr_frags == n_frags)
  430. break;
  431. rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
  432. }
  433. } else {
  434. __free_pages(rx_buf->page, efx->rx_buffer_order);
  435. rx_buf->page = NULL;
  436. n_frags = 0;
  437. }
  438. skb->truesize += n_frags * efx->rx_buffer_truesize;
  439. /* Move past the ethernet header */
  440. skb->protocol = eth_type_trans(skb, efx->net_dev);
  441. skb_mark_napi_id(skb, &channel->napi_str);
  442. return skb;
  443. }
  444. void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
  445. unsigned int n_frags, unsigned int len, u16 flags)
  446. {
  447. struct efx_nic *efx = rx_queue->efx;
  448. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  449. struct efx_rx_buffer *rx_buf;
  450. rx_queue->rx_packets++;
  451. rx_buf = efx_rx_buffer(rx_queue, index);
  452. rx_buf->flags |= flags;
  453. /* Validate the number of fragments and completed length */
  454. if (n_frags == 1) {
  455. if (!(flags & EFX_RX_PKT_PREFIX_LEN))
  456. efx_rx_packet__check_len(rx_queue, rx_buf, len);
  457. } else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
  458. unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
  459. unlikely(len > n_frags * efx->rx_dma_len) ||
  460. unlikely(!efx->rx_scatter)) {
  461. /* If this isn't an explicit discard request, either
  462. * the hardware or the driver is broken.
  463. */
  464. WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
  465. rx_buf->flags |= EFX_RX_PKT_DISCARD;
  466. }
  467. netif_vdbg(efx, rx_status, efx->net_dev,
  468. "RX queue %d received ids %x-%x len %d %s%s\n",
  469. efx_rx_queue_index(rx_queue), index,
  470. (index + n_frags - 1) & rx_queue->ptr_mask, len,
  471. (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
  472. (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
  473. /* Discard packet, if instructed to do so. Process the
  474. * previous receive first.
  475. */
  476. if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
  477. efx_rx_flush_packet(channel);
  478. efx_discard_rx_packet(channel, rx_buf, n_frags);
  479. return;
  480. }
  481. if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
  482. rx_buf->len = len;
  483. /* Release and/or sync the DMA mapping - assumes all RX buffers
  484. * consumed in-order per RX queue.
  485. */
  486. efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
  487. /* Prefetch nice and early so data will (hopefully) be in cache by
  488. * the time we look at it.
  489. */
  490. prefetch(efx_rx_buf_va(rx_buf));
  491. rx_buf->page_offset += efx->rx_prefix_size;
  492. rx_buf->len -= efx->rx_prefix_size;
  493. if (n_frags > 1) {
  494. /* Release/sync DMA mapping for additional fragments.
  495. * Fix length for last fragment.
  496. */
  497. unsigned int tail_frags = n_frags - 1;
  498. for (;;) {
  499. rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
  500. if (--tail_frags == 0)
  501. break;
  502. efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
  503. }
  504. rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
  505. efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
  506. }
  507. /* All fragments have been DMA-synced, so recycle pages. */
  508. rx_buf = efx_rx_buffer(rx_queue, index);
  509. efx_recycle_rx_pages(channel, rx_buf, n_frags);
  510. /* Pipeline receives so that we give time for packet headers to be
  511. * prefetched into cache.
  512. */
  513. efx_rx_flush_packet(channel);
  514. channel->rx_pkt_n_frags = n_frags;
  515. channel->rx_pkt_index = index;
  516. }
  517. static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
  518. struct efx_rx_buffer *rx_buf,
  519. unsigned int n_frags)
  520. {
  521. struct sk_buff *skb;
  522. u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
  523. skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
  524. if (unlikely(skb == NULL)) {
  525. struct efx_rx_queue *rx_queue;
  526. rx_queue = efx_channel_get_rx_queue(channel);
  527. efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
  528. return;
  529. }
  530. skb_record_rx_queue(skb, channel->rx_queue.core_index);
  531. /* Set the SKB flags */
  532. skb_checksum_none_assert(skb);
  533. if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
  534. skb->ip_summed = CHECKSUM_UNNECESSARY;
  535. skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
  536. }
  537. efx_rx_skb_attach_timestamp(channel, skb);
  538. if (channel->type->receive_skb)
  539. if (channel->type->receive_skb(channel, skb))
  540. return;
  541. /* Pass the packet up */
  542. netif_receive_skb(skb);
  543. }
  544. /* Handle a received packet. Second half: Touches packet payload. */
  545. void __efx_rx_packet(struct efx_channel *channel)
  546. {
  547. struct efx_nic *efx = channel->efx;
  548. struct efx_rx_buffer *rx_buf =
  549. efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
  550. u8 *eh = efx_rx_buf_va(rx_buf);
  551. /* Read length from the prefix if necessary. This already
  552. * excludes the length of the prefix itself.
  553. */
  554. if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
  555. rx_buf->len = le16_to_cpup((__le16 *)
  556. (eh + efx->rx_packet_len_offset));
  557. /* If we're in loopback test, then pass the packet directly to the
  558. * loopback layer, and free the rx_buf here
  559. */
  560. if (unlikely(efx->loopback_selftest)) {
  561. struct efx_rx_queue *rx_queue;
  562. efx_loopback_rx_packet(efx, eh, rx_buf->len);
  563. rx_queue = efx_channel_get_rx_queue(channel);
  564. efx_free_rx_buffers(rx_queue, rx_buf,
  565. channel->rx_pkt_n_frags);
  566. goto out;
  567. }
  568. if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
  569. rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
  570. if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
  571. efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh);
  572. else
  573. efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
  574. out:
  575. channel->rx_pkt_n_frags = 0;
  576. }
  577. int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
  578. {
  579. struct efx_nic *efx = rx_queue->efx;
  580. unsigned int entries;
  581. int rc;
  582. /* Create the smallest power-of-two aligned ring */
  583. entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
  584. EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
  585. rx_queue->ptr_mask = entries - 1;
  586. netif_dbg(efx, probe, efx->net_dev,
  587. "creating RX queue %d size %#x mask %#x\n",
  588. efx_rx_queue_index(rx_queue), efx->rxq_entries,
  589. rx_queue->ptr_mask);
  590. /* Allocate RX buffers */
  591. rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
  592. GFP_KERNEL);
  593. if (!rx_queue->buffer)
  594. return -ENOMEM;
  595. rc = efx_nic_probe_rx(rx_queue);
  596. if (rc) {
  597. kfree(rx_queue->buffer);
  598. rx_queue->buffer = NULL;
  599. }
  600. return rc;
  601. }
  602. static void efx_init_rx_recycle_ring(struct efx_nic *efx,
  603. struct efx_rx_queue *rx_queue)
  604. {
  605. unsigned int bufs_in_recycle_ring, page_ring_size;
  606. /* Set the RX recycle ring size */
  607. #ifdef CONFIG_PPC64
  608. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
  609. #else
  610. if (iommu_present(&pci_bus_type))
  611. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
  612. else
  613. bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_NOIOMMU;
  614. #endif /* CONFIG_PPC64 */
  615. page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
  616. efx->rx_bufs_per_page);
  617. rx_queue->page_ring = kcalloc(page_ring_size,
  618. sizeof(*rx_queue->page_ring), GFP_KERNEL);
  619. rx_queue->page_ptr_mask = page_ring_size - 1;
  620. }
  621. void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
  622. {
  623. struct efx_nic *efx = rx_queue->efx;
  624. unsigned int max_fill, trigger, max_trigger;
  625. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  626. "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
  627. /* Initialise ptr fields */
  628. rx_queue->added_count = 0;
  629. rx_queue->notified_count = 0;
  630. rx_queue->removed_count = 0;
  631. rx_queue->min_fill = -1U;
  632. efx_init_rx_recycle_ring(efx, rx_queue);
  633. rx_queue->page_remove = 0;
  634. rx_queue->page_add = rx_queue->page_ptr_mask + 1;
  635. rx_queue->page_recycle_count = 0;
  636. rx_queue->page_recycle_failed = 0;
  637. rx_queue->page_recycle_full = 0;
  638. /* Initialise limit fields */
  639. max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
  640. max_trigger =
  641. max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
  642. if (rx_refill_threshold != 0) {
  643. trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
  644. if (trigger > max_trigger)
  645. trigger = max_trigger;
  646. } else {
  647. trigger = max_trigger;
  648. }
  649. rx_queue->max_fill = max_fill;
  650. rx_queue->fast_fill_trigger = trigger;
  651. rx_queue->refill_enabled = true;
  652. /* Set up RX descriptor ring */
  653. efx_nic_init_rx(rx_queue);
  654. }
  655. void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
  656. {
  657. int i;
  658. struct efx_nic *efx = rx_queue->efx;
  659. struct efx_rx_buffer *rx_buf;
  660. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  661. "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
  662. del_timer_sync(&rx_queue->slow_fill);
  663. /* Release RX buffers from the current read ptr to the write ptr */
  664. if (rx_queue->buffer) {
  665. for (i = rx_queue->removed_count; i < rx_queue->added_count;
  666. i++) {
  667. unsigned index = i & rx_queue->ptr_mask;
  668. rx_buf = efx_rx_buffer(rx_queue, index);
  669. efx_fini_rx_buffer(rx_queue, rx_buf);
  670. }
  671. }
  672. /* Unmap and release the pages in the recycle ring. Remove the ring. */
  673. for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
  674. struct page *page = rx_queue->page_ring[i];
  675. struct efx_rx_page_state *state;
  676. if (page == NULL)
  677. continue;
  678. state = page_address(page);
  679. dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
  680. PAGE_SIZE << efx->rx_buffer_order,
  681. DMA_FROM_DEVICE);
  682. put_page(page);
  683. }
  684. kfree(rx_queue->page_ring);
  685. rx_queue->page_ring = NULL;
  686. }
  687. void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
  688. {
  689. netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
  690. "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
  691. efx_nic_remove_rx(rx_queue);
  692. kfree(rx_queue->buffer);
  693. rx_queue->buffer = NULL;
  694. }
  695. module_param(rx_refill_threshold, uint, 0444);
  696. MODULE_PARM_DESC(rx_refill_threshold,
  697. "RX descriptor ring refill threshold (%)");
  698. #ifdef CONFIG_RFS_ACCEL
  699. int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  700. u16 rxq_index, u32 flow_id)
  701. {
  702. struct efx_nic *efx = netdev_priv(net_dev);
  703. struct efx_channel *channel;
  704. struct efx_filter_spec spec;
  705. struct flow_keys fk;
  706. int rc;
  707. if (flow_id == RPS_FLOW_ID_INVALID)
  708. return -EINVAL;
  709. if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
  710. return -EPROTONOSUPPORT;
  711. if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6))
  712. return -EPROTONOSUPPORT;
  713. if (fk.control.flags & FLOW_DIS_IS_FRAGMENT)
  714. return -EPROTONOSUPPORT;
  715. efx_filter_init_rx(&spec, EFX_FILTER_PRI_HINT,
  716. efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
  717. rxq_index);
  718. spec.match_flags =
  719. EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  720. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  721. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
  722. spec.ether_type = fk.basic.n_proto;
  723. spec.ip_proto = fk.basic.ip_proto;
  724. if (fk.basic.n_proto == htons(ETH_P_IP)) {
  725. spec.rem_host[0] = fk.addrs.v4addrs.src;
  726. spec.loc_host[0] = fk.addrs.v4addrs.dst;
  727. } else {
  728. memcpy(spec.rem_host, &fk.addrs.v6addrs.src, sizeof(struct in6_addr));
  729. memcpy(spec.loc_host, &fk.addrs.v6addrs.dst, sizeof(struct in6_addr));
  730. }
  731. spec.rem_port = fk.ports.src;
  732. spec.loc_port = fk.ports.dst;
  733. rc = efx->type->filter_rfs_insert(efx, &spec);
  734. if (rc < 0)
  735. return rc;
  736. /* Remember this so we can check whether to expire the filter later */
  737. channel = efx_get_channel(efx, rxq_index);
  738. channel->rps_flow_id[rc] = flow_id;
  739. ++channel->rfs_filters_added;
  740. if (spec.ether_type == htons(ETH_P_IP))
  741. netif_info(efx, rx_status, efx->net_dev,
  742. "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d]\n",
  743. (spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  744. spec.rem_host, ntohs(spec.rem_port), spec.loc_host,
  745. ntohs(spec.loc_port), rxq_index, flow_id, rc);
  746. else
  747. netif_info(efx, rx_status, efx->net_dev,
  748. "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d]\n",
  749. (spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  750. spec.rem_host, ntohs(spec.rem_port), spec.loc_host,
  751. ntohs(spec.loc_port), rxq_index, flow_id, rc);
  752. return rc;
  753. }
  754. bool __efx_filter_rfs_expire(struct efx_nic *efx, unsigned int quota)
  755. {
  756. bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
  757. unsigned int channel_idx, index, size;
  758. u32 flow_id;
  759. if (!spin_trylock_bh(&efx->filter_lock))
  760. return false;
  761. expire_one = efx->type->filter_rfs_expire_one;
  762. channel_idx = efx->rps_expire_channel;
  763. index = efx->rps_expire_index;
  764. size = efx->type->max_rx_ip_filters;
  765. while (quota--) {
  766. struct efx_channel *channel = efx_get_channel(efx, channel_idx);
  767. flow_id = channel->rps_flow_id[index];
  768. if (flow_id != RPS_FLOW_ID_INVALID &&
  769. expire_one(efx, flow_id, index)) {
  770. netif_info(efx, rx_status, efx->net_dev,
  771. "expired filter %d [queue %u flow %u]\n",
  772. index, channel_idx, flow_id);
  773. channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
  774. }
  775. if (++index == size) {
  776. if (++channel_idx == efx->n_channels)
  777. channel_idx = 0;
  778. index = 0;
  779. }
  780. }
  781. efx->rps_expire_channel = channel_idx;
  782. efx->rps_expire_index = index;
  783. spin_unlock_bh(&efx->filter_lock);
  784. return true;
  785. }
  786. #endif /* CONFIG_RFS_ACCEL */
  787. /**
  788. * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
  789. * @spec: Specification to test
  790. *
  791. * Return: %true if the specification is a non-drop RX filter that
  792. * matches a local MAC address I/G bit value of 1 or matches a local
  793. * IPv4 or IPv6 address value in the respective multicast address
  794. * range. Otherwise %false.
  795. */
  796. bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
  797. {
  798. if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
  799. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
  800. return false;
  801. if (spec->match_flags &
  802. (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
  803. is_multicast_ether_addr(spec->loc_mac))
  804. return true;
  805. if ((spec->match_flags &
  806. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  807. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  808. if (spec->ether_type == htons(ETH_P_IP) &&
  809. ipv4_is_multicast(spec->loc_host[0]))
  810. return true;
  811. if (spec->ether_type == htons(ETH_P_IPV6) &&
  812. ((const u8 *)spec->loc_host)[0] == 0xff)
  813. return true;
  814. }
  815. return false;
  816. }