rx.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. if (channel->rx_list != NULL)
  543. /* Add to list, will pass up later */
  544. list_add_tail(&skb->list, channel->rx_list);
  545. else
  546. /* No list, so pass it up now */
  547. netif_receive_skb(skb);
  548. }
  549. /* Handle a received packet. Second half: Touches packet payload. */
  550. void __efx_rx_packet(struct efx_channel *channel)
  551. {
  552. struct efx_nic *efx = channel->efx;
  553. struct efx_rx_buffer *rx_buf =
  554. efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
  555. u8 *eh = efx_rx_buf_va(rx_buf);
  556. /* Read length from the prefix if necessary. This already
  557. * excludes the length of the prefix itself.
  558. */
  559. if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
  560. rx_buf->len = le16_to_cpup((__le16 *)
  561. (eh + efx->rx_packet_len_offset));
  562. /* If we're in loopback test, then pass the packet directly to the
  563. * loopback layer, and free the rx_buf here
  564. */
  565. if (unlikely(efx->loopback_selftest)) {
  566. struct efx_rx_queue *rx_queue;
  567. efx_loopback_rx_packet(efx, eh, rx_buf->len);
  568. rx_queue = efx_channel_get_rx_queue(channel);
  569. efx_free_rx_buffers(rx_queue, rx_buf,
  570. channel->rx_pkt_n_frags);
  571. goto out;
  572. }
  573. if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
  574. rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
  575. if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
  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_WARN_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. static void efx_filter_rfs_work(struct work_struct *data)
  705. {
  706. struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
  707. work);
  708. struct efx_nic *efx = netdev_priv(req->net_dev);
  709. struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
  710. int slot_idx = req - efx->rps_slot;
  711. struct efx_arfs_rule *rule;
  712. u16 arfs_id = 0;
  713. int rc;
  714. rc = efx->type->filter_insert(efx, &req->spec, true);
  715. if (rc >= 0)
  716. rc %= efx->type->max_rx_ip_filters;
  717. if (efx->rps_hash_table) {
  718. spin_lock_bh(&efx->rps_hash_lock);
  719. rule = efx_rps_hash_find(efx, &req->spec);
  720. /* The rule might have already gone, if someone else's request
  721. * for the same spec was already worked and then expired before
  722. * we got around to our work. In that case we have nothing
  723. * tying us to an arfs_id, meaning that as soon as the filter
  724. * is considered for expiry it will be removed.
  725. */
  726. if (rule) {
  727. if (rc < 0)
  728. rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
  729. else
  730. rule->filter_id = rc;
  731. arfs_id = rule->arfs_id;
  732. }
  733. spin_unlock_bh(&efx->rps_hash_lock);
  734. }
  735. if (rc >= 0) {
  736. /* Remember this so we can check whether to expire the filter
  737. * later.
  738. */
  739. mutex_lock(&efx->rps_mutex);
  740. channel->rps_flow_id[rc] = req->flow_id;
  741. ++channel->rfs_filters_added;
  742. mutex_unlock(&efx->rps_mutex);
  743. if (req->spec.ether_type == htons(ETH_P_IP))
  744. netif_info(efx, rx_status, efx->net_dev,
  745. "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
  746. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  747. req->spec.rem_host, ntohs(req->spec.rem_port),
  748. req->spec.loc_host, ntohs(req->spec.loc_port),
  749. req->rxq_index, req->flow_id, rc, arfs_id);
  750. else
  751. netif_info(efx, rx_status, efx->net_dev,
  752. "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
  753. (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
  754. req->spec.rem_host, ntohs(req->spec.rem_port),
  755. req->spec.loc_host, ntohs(req->spec.loc_port),
  756. req->rxq_index, req->flow_id, rc, arfs_id);
  757. }
  758. /* Release references */
  759. clear_bit(slot_idx, &efx->rps_slot_map);
  760. dev_put(req->net_dev);
  761. }
  762. int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  763. u16 rxq_index, u32 flow_id)
  764. {
  765. struct efx_nic *efx = netdev_priv(net_dev);
  766. struct efx_async_filter_insertion *req;
  767. struct efx_arfs_rule *rule;
  768. struct flow_keys fk;
  769. int slot_idx;
  770. bool new;
  771. int rc;
  772. /* find a free slot */
  773. for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
  774. if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
  775. break;
  776. if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
  777. return -EBUSY;
  778. if (flow_id == RPS_FLOW_ID_INVALID) {
  779. rc = -EINVAL;
  780. goto out_clear;
  781. }
  782. if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
  783. rc = -EPROTONOSUPPORT;
  784. goto out_clear;
  785. }
  786. if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
  787. rc = -EPROTONOSUPPORT;
  788. goto out_clear;
  789. }
  790. if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
  791. rc = -EPROTONOSUPPORT;
  792. goto out_clear;
  793. }
  794. req = efx->rps_slot + slot_idx;
  795. efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
  796. efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
  797. rxq_index);
  798. req->spec.match_flags =
  799. EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  800. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  801. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
  802. req->spec.ether_type = fk.basic.n_proto;
  803. req->spec.ip_proto = fk.basic.ip_proto;
  804. if (fk.basic.n_proto == htons(ETH_P_IP)) {
  805. req->spec.rem_host[0] = fk.addrs.v4addrs.src;
  806. req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
  807. } else {
  808. memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
  809. sizeof(struct in6_addr));
  810. memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
  811. sizeof(struct in6_addr));
  812. }
  813. req->spec.rem_port = fk.ports.src;
  814. req->spec.loc_port = fk.ports.dst;
  815. if (efx->rps_hash_table) {
  816. /* Add it to ARFS hash table */
  817. spin_lock(&efx->rps_hash_lock);
  818. rule = efx_rps_hash_add(efx, &req->spec, &new);
  819. if (!rule) {
  820. rc = -ENOMEM;
  821. goto out_unlock;
  822. }
  823. if (new)
  824. rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
  825. rc = rule->arfs_id;
  826. /* Skip if existing or pending filter already does the right thing */
  827. if (!new && rule->rxq_index == rxq_index &&
  828. rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
  829. goto out_unlock;
  830. rule->rxq_index = rxq_index;
  831. rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
  832. spin_unlock(&efx->rps_hash_lock);
  833. } else {
  834. /* Without an ARFS hash table, we just use arfs_id 0 for all
  835. * filters. This means if multiple flows hash to the same
  836. * flow_id, all but the most recently touched will be eligible
  837. * for expiry.
  838. */
  839. rc = 0;
  840. }
  841. /* Queue the request */
  842. dev_hold(req->net_dev = net_dev);
  843. INIT_WORK(&req->work, efx_filter_rfs_work);
  844. req->rxq_index = rxq_index;
  845. req->flow_id = flow_id;
  846. schedule_work(&req->work);
  847. return rc;
  848. out_unlock:
  849. spin_unlock(&efx->rps_hash_lock);
  850. out_clear:
  851. clear_bit(slot_idx, &efx->rps_slot_map);
  852. return rc;
  853. }
  854. bool __efx_filter_rfs_expire(struct efx_nic *efx, unsigned int quota)
  855. {
  856. bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
  857. unsigned int channel_idx, index, size;
  858. u32 flow_id;
  859. if (!mutex_trylock(&efx->rps_mutex))
  860. return false;
  861. expire_one = efx->type->filter_rfs_expire_one;
  862. channel_idx = efx->rps_expire_channel;
  863. index = efx->rps_expire_index;
  864. size = efx->type->max_rx_ip_filters;
  865. while (quota--) {
  866. struct efx_channel *channel = efx_get_channel(efx, channel_idx);
  867. flow_id = channel->rps_flow_id[index];
  868. if (flow_id != RPS_FLOW_ID_INVALID &&
  869. expire_one(efx, flow_id, index)) {
  870. netif_info(efx, rx_status, efx->net_dev,
  871. "expired filter %d [queue %u flow %u]\n",
  872. index, channel_idx, flow_id);
  873. channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
  874. }
  875. if (++index == size) {
  876. if (++channel_idx == efx->n_channels)
  877. channel_idx = 0;
  878. index = 0;
  879. }
  880. }
  881. efx->rps_expire_channel = channel_idx;
  882. efx->rps_expire_index = index;
  883. mutex_unlock(&efx->rps_mutex);
  884. return true;
  885. }
  886. #endif /* CONFIG_RFS_ACCEL */
  887. /**
  888. * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
  889. * @spec: Specification to test
  890. *
  891. * Return: %true if the specification is a non-drop RX filter that
  892. * matches a local MAC address I/G bit value of 1 or matches a local
  893. * IPv4 or IPv6 address value in the respective multicast address
  894. * range. Otherwise %false.
  895. */
  896. bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
  897. {
  898. if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
  899. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
  900. return false;
  901. if (spec->match_flags &
  902. (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
  903. is_multicast_ether_addr(spec->loc_mac))
  904. return true;
  905. if ((spec->match_flags &
  906. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  907. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  908. if (spec->ether_type == htons(ETH_P_IP) &&
  909. ipv4_is_multicast(spec->loc_host[0]))
  910. return true;
  911. if (spec->ether_type == htons(ETH_P_IPV6) &&
  912. ((const u8 *)spec->loc_host)[0] == 0xff)
  913. return true;
  914. }
  915. return false;
  916. }