PageRenderTime 65ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/ethernet/intel/i40evf/i40e_txrx.c

http://github.com/mirrors/linux-2.6
C | 2215 lines | 1250 code | 342 blank | 623 comment | 222 complexity | 392e82dcfeefa4fe0b69296554833285 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include <net/busy_poll.h>
  28. #include "i40evf.h"
  29. #include "i40e_prototype.h"
  30. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  31. u32 td_tag)
  32. {
  33. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  34. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  35. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  36. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  37. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  38. }
  39. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  40. /**
  41. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  42. * @ring: the ring that owns the buffer
  43. * @tx_buffer: the buffer to free
  44. **/
  45. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  46. struct i40e_tx_buffer *tx_buffer)
  47. {
  48. if (tx_buffer->skb) {
  49. dev_kfree_skb_any(tx_buffer->skb);
  50. if (dma_unmap_len(tx_buffer, len))
  51. dma_unmap_single(ring->dev,
  52. dma_unmap_addr(tx_buffer, dma),
  53. dma_unmap_len(tx_buffer, len),
  54. DMA_TO_DEVICE);
  55. } else if (dma_unmap_len(tx_buffer, len)) {
  56. dma_unmap_page(ring->dev,
  57. dma_unmap_addr(tx_buffer, dma),
  58. dma_unmap_len(tx_buffer, len),
  59. DMA_TO_DEVICE);
  60. }
  61. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  62. kfree(tx_buffer->raw_buf);
  63. tx_buffer->next_to_watch = NULL;
  64. tx_buffer->skb = NULL;
  65. dma_unmap_len_set(tx_buffer, len, 0);
  66. /* tx_buffer must be completely set up in the transmit path */
  67. }
  68. /**
  69. * i40evf_clean_tx_ring - Free any empty Tx buffers
  70. * @tx_ring: ring to be cleaned
  71. **/
  72. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  73. {
  74. unsigned long bi_size;
  75. u16 i;
  76. /* ring already cleared, nothing to do */
  77. if (!tx_ring->tx_bi)
  78. return;
  79. /* Free all the Tx ring sk_buffs */
  80. for (i = 0; i < tx_ring->count; i++)
  81. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  82. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  83. memset(tx_ring->tx_bi, 0, bi_size);
  84. /* Zero out the descriptor ring */
  85. memset(tx_ring->desc, 0, tx_ring->size);
  86. tx_ring->next_to_use = 0;
  87. tx_ring->next_to_clean = 0;
  88. if (!tx_ring->netdev)
  89. return;
  90. /* cleanup Tx queue statistics */
  91. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  92. tx_ring->queue_index));
  93. }
  94. /**
  95. * i40evf_free_tx_resources - Free Tx resources per queue
  96. * @tx_ring: Tx descriptor ring for a specific queue
  97. *
  98. * Free all transmit software resources
  99. **/
  100. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  101. {
  102. i40evf_clean_tx_ring(tx_ring);
  103. kfree(tx_ring->tx_bi);
  104. tx_ring->tx_bi = NULL;
  105. if (tx_ring->desc) {
  106. dma_free_coherent(tx_ring->dev, tx_ring->size,
  107. tx_ring->desc, tx_ring->dma);
  108. tx_ring->desc = NULL;
  109. }
  110. }
  111. /**
  112. * i40evf_get_tx_pending - how many Tx descriptors not processed
  113. * @tx_ring: the ring of descriptors
  114. * @in_sw: is tx_pending being checked in SW or HW
  115. *
  116. * Since there is no access to the ring head register
  117. * in XL710, we need to use our local copies
  118. **/
  119. u32 i40evf_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  120. {
  121. u32 head, tail;
  122. if (!in_sw)
  123. head = i40e_get_head(ring);
  124. else
  125. head = ring->next_to_clean;
  126. tail = readl(ring->tail);
  127. if (head != tail)
  128. return (head < tail) ?
  129. tail - head : (tail + ring->count - head);
  130. return 0;
  131. }
  132. #define WB_STRIDE 0x3
  133. /**
  134. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  135. * @vsi: the VSI we care about
  136. * @tx_ring: Tx ring to clean
  137. * @napi_budget: Used to determine if we are in netpoll
  138. *
  139. * Returns true if there's any budget left (e.g. the clean is finished)
  140. **/
  141. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  142. struct i40e_ring *tx_ring, int napi_budget)
  143. {
  144. u16 i = tx_ring->next_to_clean;
  145. struct i40e_tx_buffer *tx_buf;
  146. struct i40e_tx_desc *tx_head;
  147. struct i40e_tx_desc *tx_desc;
  148. unsigned int total_bytes = 0, total_packets = 0;
  149. unsigned int budget = vsi->work_limit;
  150. tx_buf = &tx_ring->tx_bi[i];
  151. tx_desc = I40E_TX_DESC(tx_ring, i);
  152. i -= tx_ring->count;
  153. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  154. do {
  155. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  156. /* if next_to_watch is not set then there is no work pending */
  157. if (!eop_desc)
  158. break;
  159. /* prevent any other reads prior to eop_desc */
  160. read_barrier_depends();
  161. /* we have caught up to head, no work left to do */
  162. if (tx_head == tx_desc)
  163. break;
  164. /* clear next_to_watch to prevent false hangs */
  165. tx_buf->next_to_watch = NULL;
  166. /* update the statistics for this packet */
  167. total_bytes += tx_buf->bytecount;
  168. total_packets += tx_buf->gso_segs;
  169. /* free the skb */
  170. napi_consume_skb(tx_buf->skb, napi_budget);
  171. /* unmap skb header data */
  172. dma_unmap_single(tx_ring->dev,
  173. dma_unmap_addr(tx_buf, dma),
  174. dma_unmap_len(tx_buf, len),
  175. DMA_TO_DEVICE);
  176. /* clear tx_buffer data */
  177. tx_buf->skb = NULL;
  178. dma_unmap_len_set(tx_buf, len, 0);
  179. /* unmap remaining buffers */
  180. while (tx_desc != eop_desc) {
  181. tx_buf++;
  182. tx_desc++;
  183. i++;
  184. if (unlikely(!i)) {
  185. i -= tx_ring->count;
  186. tx_buf = tx_ring->tx_bi;
  187. tx_desc = I40E_TX_DESC(tx_ring, 0);
  188. }
  189. /* unmap any remaining paged data */
  190. if (dma_unmap_len(tx_buf, len)) {
  191. dma_unmap_page(tx_ring->dev,
  192. dma_unmap_addr(tx_buf, dma),
  193. dma_unmap_len(tx_buf, len),
  194. DMA_TO_DEVICE);
  195. dma_unmap_len_set(tx_buf, len, 0);
  196. }
  197. }
  198. /* move us one more past the eop_desc for start of next pkt */
  199. tx_buf++;
  200. tx_desc++;
  201. i++;
  202. if (unlikely(!i)) {
  203. i -= tx_ring->count;
  204. tx_buf = tx_ring->tx_bi;
  205. tx_desc = I40E_TX_DESC(tx_ring, 0);
  206. }
  207. prefetch(tx_desc);
  208. /* update budget accounting */
  209. budget--;
  210. } while (likely(budget));
  211. i += tx_ring->count;
  212. tx_ring->next_to_clean = i;
  213. u64_stats_update_begin(&tx_ring->syncp);
  214. tx_ring->stats.bytes += total_bytes;
  215. tx_ring->stats.packets += total_packets;
  216. u64_stats_update_end(&tx_ring->syncp);
  217. tx_ring->q_vector->tx.total_bytes += total_bytes;
  218. tx_ring->q_vector->tx.total_packets += total_packets;
  219. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  220. /* check to see if there are < 4 descriptors
  221. * waiting to be written back, then kick the hardware to force
  222. * them to be written back in case we stay in NAPI.
  223. * In this mode on X722 we do not enable Interrupt.
  224. */
  225. unsigned int j = i40evf_get_tx_pending(tx_ring, false);
  226. if (budget &&
  227. ((j / (WB_STRIDE + 1)) == 0) && (j > 0) &&
  228. !test_bit(__I40E_DOWN, &vsi->state) &&
  229. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  230. tx_ring->arm_wb = true;
  231. }
  232. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  233. tx_ring->queue_index),
  234. total_packets, total_bytes);
  235. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  236. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  237. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  238. /* Make sure that anybody stopping the queue after this
  239. * sees the new next_to_clean.
  240. */
  241. smp_mb();
  242. if (__netif_subqueue_stopped(tx_ring->netdev,
  243. tx_ring->queue_index) &&
  244. !test_bit(__I40E_DOWN, &vsi->state)) {
  245. netif_wake_subqueue(tx_ring->netdev,
  246. tx_ring->queue_index);
  247. ++tx_ring->tx_stats.restart_queue;
  248. }
  249. }
  250. return !!budget;
  251. }
  252. /**
  253. * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  254. * @vsi: the VSI we care about
  255. * @q_vector: the vector on which to enable writeback
  256. *
  257. **/
  258. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  259. struct i40e_q_vector *q_vector)
  260. {
  261. u16 flags = q_vector->tx.ring[0].flags;
  262. u32 val;
  263. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  264. return;
  265. if (q_vector->arm_wb_state)
  266. return;
  267. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
  268. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
  269. wr32(&vsi->back->hw,
  270. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  271. vsi->base_vector - 1), val);
  272. q_vector->arm_wb_state = true;
  273. }
  274. /**
  275. * i40evf_force_wb - Issue SW Interrupt so HW does a wb
  276. * @vsi: the VSI we care about
  277. * @q_vector: the vector on which to force writeback
  278. *
  279. **/
  280. void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  281. {
  282. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  283. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  284. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  285. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
  286. /* allow 00 to be written to the index */;
  287. wr32(&vsi->back->hw,
  288. I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
  289. val);
  290. }
  291. /**
  292. * i40e_set_new_dynamic_itr - Find new ITR level
  293. * @rc: structure containing ring performance data
  294. *
  295. * Returns true if ITR changed, false if not
  296. *
  297. * Stores a new ITR value based on packets and byte counts during
  298. * the last interrupt. The advantage of per interrupt computation
  299. * is faster updates and more accurate ITR for the current traffic
  300. * pattern. Constants in this function were computed based on
  301. * theoretical maximum wire speed and thresholds were set based on
  302. * testing data as well as attempting to minimize response time
  303. * while increasing bulk throughput.
  304. **/
  305. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  306. {
  307. enum i40e_latency_range new_latency_range = rc->latency_range;
  308. struct i40e_q_vector *qv = rc->ring->q_vector;
  309. u32 new_itr = rc->itr;
  310. int bytes_per_int;
  311. int usecs;
  312. if (rc->total_packets == 0 || !rc->itr)
  313. return false;
  314. /* simple throttlerate management
  315. * 0-10MB/s lowest (50000 ints/s)
  316. * 10-20MB/s low (20000 ints/s)
  317. * 20-1249MB/s bulk (18000 ints/s)
  318. * > 40000 Rx packets per second (8000 ints/s)
  319. *
  320. * The math works out because the divisor is in 10^(-6) which
  321. * turns the bytes/us input value into MB/s values, but
  322. * make sure to use usecs, as the register values written
  323. * are in 2 usec increments in the ITR registers, and make sure
  324. * to use the smoothed values that the countdown timer gives us.
  325. */
  326. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  327. bytes_per_int = rc->total_bytes / usecs;
  328. switch (new_latency_range) {
  329. case I40E_LOWEST_LATENCY:
  330. if (bytes_per_int > 10)
  331. new_latency_range = I40E_LOW_LATENCY;
  332. break;
  333. case I40E_LOW_LATENCY:
  334. if (bytes_per_int > 20)
  335. new_latency_range = I40E_BULK_LATENCY;
  336. else if (bytes_per_int <= 10)
  337. new_latency_range = I40E_LOWEST_LATENCY;
  338. break;
  339. case I40E_BULK_LATENCY:
  340. case I40E_ULTRA_LATENCY:
  341. default:
  342. if (bytes_per_int <= 20)
  343. new_latency_range = I40E_LOW_LATENCY;
  344. break;
  345. }
  346. /* this is to adjust RX more aggressively when streaming small
  347. * packets. The value of 40000 was picked as it is just beyond
  348. * what the hardware can receive per second if in low latency
  349. * mode.
  350. */
  351. #define RX_ULTRA_PACKET_RATE 40000
  352. if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
  353. (&qv->rx == rc))
  354. new_latency_range = I40E_ULTRA_LATENCY;
  355. rc->latency_range = new_latency_range;
  356. switch (new_latency_range) {
  357. case I40E_LOWEST_LATENCY:
  358. new_itr = I40E_ITR_50K;
  359. break;
  360. case I40E_LOW_LATENCY:
  361. new_itr = I40E_ITR_20K;
  362. break;
  363. case I40E_BULK_LATENCY:
  364. new_itr = I40E_ITR_18K;
  365. break;
  366. case I40E_ULTRA_LATENCY:
  367. new_itr = I40E_ITR_8K;
  368. break;
  369. default:
  370. break;
  371. }
  372. rc->total_bytes = 0;
  373. rc->total_packets = 0;
  374. if (new_itr != rc->itr) {
  375. rc->itr = new_itr;
  376. return true;
  377. }
  378. return false;
  379. }
  380. /**
  381. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  382. * @tx_ring: the tx ring to set up
  383. *
  384. * Return 0 on success, negative on error
  385. **/
  386. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  387. {
  388. struct device *dev = tx_ring->dev;
  389. int bi_size;
  390. if (!dev)
  391. return -ENOMEM;
  392. /* warn if we are about to overwrite the pointer */
  393. WARN_ON(tx_ring->tx_bi);
  394. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  395. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  396. if (!tx_ring->tx_bi)
  397. goto err;
  398. /* round up to nearest 4K */
  399. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  400. /* add u32 for head writeback, align after this takes care of
  401. * guaranteeing this is at least one cache line in size
  402. */
  403. tx_ring->size += sizeof(u32);
  404. tx_ring->size = ALIGN(tx_ring->size, 4096);
  405. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  406. &tx_ring->dma, GFP_KERNEL);
  407. if (!tx_ring->desc) {
  408. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  409. tx_ring->size);
  410. goto err;
  411. }
  412. tx_ring->next_to_use = 0;
  413. tx_ring->next_to_clean = 0;
  414. return 0;
  415. err:
  416. kfree(tx_ring->tx_bi);
  417. tx_ring->tx_bi = NULL;
  418. return -ENOMEM;
  419. }
  420. /**
  421. * i40evf_clean_rx_ring - Free Rx buffers
  422. * @rx_ring: ring to be cleaned
  423. **/
  424. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  425. {
  426. struct device *dev = rx_ring->dev;
  427. unsigned long bi_size;
  428. u16 i;
  429. /* ring already cleared, nothing to do */
  430. if (!rx_ring->rx_bi)
  431. return;
  432. /* Free all the Rx ring sk_buffs */
  433. for (i = 0; i < rx_ring->count; i++) {
  434. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  435. if (rx_bi->skb) {
  436. dev_kfree_skb(rx_bi->skb);
  437. rx_bi->skb = NULL;
  438. }
  439. if (!rx_bi->page)
  440. continue;
  441. dma_unmap_page(dev, rx_bi->dma, PAGE_SIZE, DMA_FROM_DEVICE);
  442. __free_pages(rx_bi->page, 0);
  443. rx_bi->page = NULL;
  444. rx_bi->page_offset = 0;
  445. }
  446. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  447. memset(rx_ring->rx_bi, 0, bi_size);
  448. /* Zero out the descriptor ring */
  449. memset(rx_ring->desc, 0, rx_ring->size);
  450. rx_ring->next_to_alloc = 0;
  451. rx_ring->next_to_clean = 0;
  452. rx_ring->next_to_use = 0;
  453. }
  454. /**
  455. * i40evf_free_rx_resources - Free Rx resources
  456. * @rx_ring: ring to clean the resources from
  457. *
  458. * Free all receive software resources
  459. **/
  460. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  461. {
  462. i40evf_clean_rx_ring(rx_ring);
  463. kfree(rx_ring->rx_bi);
  464. rx_ring->rx_bi = NULL;
  465. if (rx_ring->desc) {
  466. dma_free_coherent(rx_ring->dev, rx_ring->size,
  467. rx_ring->desc, rx_ring->dma);
  468. rx_ring->desc = NULL;
  469. }
  470. }
  471. /**
  472. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  473. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  474. *
  475. * Returns 0 on success, negative on failure
  476. **/
  477. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  478. {
  479. struct device *dev = rx_ring->dev;
  480. int bi_size;
  481. /* warn if we are about to overwrite the pointer */
  482. WARN_ON(rx_ring->rx_bi);
  483. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  484. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  485. if (!rx_ring->rx_bi)
  486. goto err;
  487. u64_stats_init(&rx_ring->syncp);
  488. /* Round up to nearest 4K */
  489. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  490. rx_ring->size = ALIGN(rx_ring->size, 4096);
  491. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  492. &rx_ring->dma, GFP_KERNEL);
  493. if (!rx_ring->desc) {
  494. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  495. rx_ring->size);
  496. goto err;
  497. }
  498. rx_ring->next_to_alloc = 0;
  499. rx_ring->next_to_clean = 0;
  500. rx_ring->next_to_use = 0;
  501. return 0;
  502. err:
  503. kfree(rx_ring->rx_bi);
  504. rx_ring->rx_bi = NULL;
  505. return -ENOMEM;
  506. }
  507. /**
  508. * i40e_release_rx_desc - Store the new tail and head values
  509. * @rx_ring: ring to bump
  510. * @val: new head index
  511. **/
  512. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  513. {
  514. rx_ring->next_to_use = val;
  515. /* update next to alloc since we have filled the ring */
  516. rx_ring->next_to_alloc = val;
  517. /* Force memory writes to complete before letting h/w
  518. * know there are new descriptors to fetch. (Only
  519. * applicable for weak-ordered memory model archs,
  520. * such as IA-64).
  521. */
  522. wmb();
  523. writel(val, rx_ring->tail);
  524. }
  525. /**
  526. * i40e_alloc_mapped_page - recycle or make a new page
  527. * @rx_ring: ring to use
  528. * @bi: rx_buffer struct to modify
  529. *
  530. * Returns true if the page was successfully allocated or
  531. * reused.
  532. **/
  533. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  534. struct i40e_rx_buffer *bi)
  535. {
  536. struct page *page = bi->page;
  537. dma_addr_t dma;
  538. /* since we are recycling buffers we should seldom need to alloc */
  539. if (likely(page)) {
  540. rx_ring->rx_stats.page_reuse_count++;
  541. return true;
  542. }
  543. /* alloc new page for storage */
  544. page = dev_alloc_page();
  545. if (unlikely(!page)) {
  546. rx_ring->rx_stats.alloc_page_failed++;
  547. return false;
  548. }
  549. /* map page for use */
  550. dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  551. /* if mapping failed free memory back to system since
  552. * there isn't much point in holding memory we can't use
  553. */
  554. if (dma_mapping_error(rx_ring->dev, dma)) {
  555. __free_pages(page, 0);
  556. rx_ring->rx_stats.alloc_page_failed++;
  557. return false;
  558. }
  559. bi->dma = dma;
  560. bi->page = page;
  561. bi->page_offset = 0;
  562. return true;
  563. }
  564. /**
  565. * i40e_receive_skb - Send a completed packet up the stack
  566. * @rx_ring: rx ring in play
  567. * @skb: packet to send up
  568. * @vlan_tag: vlan tag for packet
  569. **/
  570. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  571. struct sk_buff *skb, u16 vlan_tag)
  572. {
  573. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  574. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  575. (vlan_tag & VLAN_VID_MASK))
  576. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  577. napi_gro_receive(&q_vector->napi, skb);
  578. }
  579. /**
  580. * i40evf_alloc_rx_buffers - Replace used receive buffers
  581. * @rx_ring: ring to place buffers on
  582. * @cleaned_count: number of buffers to replace
  583. *
  584. * Returns false if all allocations were successful, true if any fail
  585. **/
  586. bool i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  587. {
  588. u16 ntu = rx_ring->next_to_use;
  589. union i40e_rx_desc *rx_desc;
  590. struct i40e_rx_buffer *bi;
  591. /* do nothing if no valid netdev defined */
  592. if (!rx_ring->netdev || !cleaned_count)
  593. return false;
  594. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  595. bi = &rx_ring->rx_bi[ntu];
  596. do {
  597. if (!i40e_alloc_mapped_page(rx_ring, bi))
  598. goto no_buffers;
  599. /* Refresh the desc even if buffer_addrs didn't change
  600. * because each write-back erases this info.
  601. */
  602. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  603. rx_desc->read.hdr_addr = 0;
  604. rx_desc++;
  605. bi++;
  606. ntu++;
  607. if (unlikely(ntu == rx_ring->count)) {
  608. rx_desc = I40E_RX_DESC(rx_ring, 0);
  609. bi = rx_ring->rx_bi;
  610. ntu = 0;
  611. }
  612. /* clear the status bits for the next_to_use descriptor */
  613. rx_desc->wb.qword1.status_error_len = 0;
  614. cleaned_count--;
  615. } while (cleaned_count);
  616. if (rx_ring->next_to_use != ntu)
  617. i40e_release_rx_desc(rx_ring, ntu);
  618. return false;
  619. no_buffers:
  620. if (rx_ring->next_to_use != ntu)
  621. i40e_release_rx_desc(rx_ring, ntu);
  622. /* make sure to come back via polling to try again after
  623. * allocation failure
  624. */
  625. return true;
  626. }
  627. /**
  628. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  629. * @vsi: the VSI we care about
  630. * @skb: skb currently being received and modified
  631. * @rx_desc: the receive descriptor
  632. *
  633. * skb->protocol must be set before this function is called
  634. **/
  635. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  636. struct sk_buff *skb,
  637. union i40e_rx_desc *rx_desc)
  638. {
  639. struct i40e_rx_ptype_decoded decoded;
  640. u32 rx_error, rx_status;
  641. bool ipv4, ipv6;
  642. u8 ptype;
  643. u64 qword;
  644. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  645. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  646. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  647. I40E_RXD_QW1_ERROR_SHIFT;
  648. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  649. I40E_RXD_QW1_STATUS_SHIFT;
  650. decoded = decode_rx_desc_ptype(ptype);
  651. skb->ip_summed = CHECKSUM_NONE;
  652. skb_checksum_none_assert(skb);
  653. /* Rx csum enabled and ip headers found? */
  654. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  655. return;
  656. /* did the hardware decode the packet and checksum? */
  657. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  658. return;
  659. /* both known and outer_ip must be set for the below code to work */
  660. if (!(decoded.known && decoded.outer_ip))
  661. return;
  662. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  663. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  664. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  665. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  666. if (ipv4 &&
  667. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  668. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  669. goto checksum_fail;
  670. /* likely incorrect csum if alternate IP extension headers found */
  671. if (ipv6 &&
  672. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  673. /* don't increment checksum err here, non-fatal err */
  674. return;
  675. /* there was some L4 error, count error and punt packet to the stack */
  676. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  677. goto checksum_fail;
  678. /* handle packets that were not able to be checksummed due
  679. * to arrival speed, in this case the stack can compute
  680. * the csum.
  681. */
  682. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  683. return;
  684. /* If there is an outer header present that might contain a checksum
  685. * we need to bump the checksum level by 1 to reflect the fact that
  686. * we are indicating we validated the inner checksum.
  687. */
  688. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  689. skb->csum_level = 1;
  690. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  691. switch (decoded.inner_prot) {
  692. case I40E_RX_PTYPE_INNER_PROT_TCP:
  693. case I40E_RX_PTYPE_INNER_PROT_UDP:
  694. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  695. skb->ip_summed = CHECKSUM_UNNECESSARY;
  696. /* fall though */
  697. default:
  698. break;
  699. }
  700. return;
  701. checksum_fail:
  702. vsi->back->hw_csum_rx_error++;
  703. }
  704. /**
  705. * i40e_ptype_to_htype - get a hash type
  706. * @ptype: the ptype value from the descriptor
  707. *
  708. * Returns a hash type to be used by skb_set_hash
  709. **/
  710. static inline int i40e_ptype_to_htype(u8 ptype)
  711. {
  712. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  713. if (!decoded.known)
  714. return PKT_HASH_TYPE_NONE;
  715. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  716. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  717. return PKT_HASH_TYPE_L4;
  718. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  719. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  720. return PKT_HASH_TYPE_L3;
  721. else
  722. return PKT_HASH_TYPE_L2;
  723. }
  724. /**
  725. * i40e_rx_hash - set the hash value in the skb
  726. * @ring: descriptor ring
  727. * @rx_desc: specific descriptor
  728. **/
  729. static inline void i40e_rx_hash(struct i40e_ring *ring,
  730. union i40e_rx_desc *rx_desc,
  731. struct sk_buff *skb,
  732. u8 rx_ptype)
  733. {
  734. u32 hash;
  735. const __le64 rss_mask =
  736. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  737. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  738. if (ring->netdev->features & NETIF_F_RXHASH)
  739. return;
  740. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  741. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  742. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  743. }
  744. }
  745. /**
  746. * i40evf_process_skb_fields - Populate skb header fields from Rx descriptor
  747. * @rx_ring: rx descriptor ring packet is being transacted on
  748. * @rx_desc: pointer to the EOP Rx descriptor
  749. * @skb: pointer to current skb being populated
  750. * @rx_ptype: the packet type decoded by hardware
  751. *
  752. * This function checks the ring, descriptor, and packet information in
  753. * order to populate the hash, checksum, VLAN, protocol, and
  754. * other fields within the skb.
  755. **/
  756. static inline
  757. void i40evf_process_skb_fields(struct i40e_ring *rx_ring,
  758. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  759. u8 rx_ptype)
  760. {
  761. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  762. /* modifies the skb - consumes the enet header */
  763. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  764. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  765. skb_record_rx_queue(skb, rx_ring->queue_index);
  766. }
  767. /**
  768. * i40e_pull_tail - i40e specific version of skb_pull_tail
  769. * @rx_ring: rx descriptor ring packet is being transacted on
  770. * @skb: pointer to current skb being adjusted
  771. *
  772. * This function is an i40e specific version of __pskb_pull_tail. The
  773. * main difference between this version and the original function is that
  774. * this function can make several assumptions about the state of things
  775. * that allow for significant optimizations versus the standard function.
  776. * As a result we can do things like drop a frag and maintain an accurate
  777. * truesize for the skb.
  778. */
  779. static void i40e_pull_tail(struct i40e_ring *rx_ring, struct sk_buff *skb)
  780. {
  781. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
  782. unsigned char *va;
  783. unsigned int pull_len;
  784. /* it is valid to use page_address instead of kmap since we are
  785. * working with pages allocated out of the lomem pool per
  786. * alloc_page(GFP_ATOMIC)
  787. */
  788. va = skb_frag_address(frag);
  789. /* we need the header to contain the greater of either ETH_HLEN or
  790. * 60 bytes if the skb->len is less than 60 for skb_pad.
  791. */
  792. pull_len = eth_get_headlen(va, I40E_RX_HDR_SIZE);
  793. /* align pull length to size of long to optimize memcpy performance */
  794. skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
  795. /* update all of the pointers */
  796. skb_frag_size_sub(frag, pull_len);
  797. frag->page_offset += pull_len;
  798. skb->data_len -= pull_len;
  799. skb->tail += pull_len;
  800. }
  801. /**
  802. * i40e_cleanup_headers - Correct empty headers
  803. * @rx_ring: rx descriptor ring packet is being transacted on
  804. * @skb: pointer to current skb being fixed
  805. *
  806. * Also address the case where we are pulling data in on pages only
  807. * and as such no data is present in the skb header.
  808. *
  809. * In addition if skb is not at least 60 bytes we need to pad it so that
  810. * it is large enough to qualify as a valid Ethernet frame.
  811. *
  812. * Returns true if an error was encountered and skb was freed.
  813. **/
  814. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
  815. {
  816. /* place header in linear portion of buffer */
  817. if (skb_is_nonlinear(skb))
  818. i40e_pull_tail(rx_ring, skb);
  819. /* if eth_skb_pad returns an error the skb was freed */
  820. if (eth_skb_pad(skb))
  821. return true;
  822. return false;
  823. }
  824. /**
  825. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  826. * @rx_ring: rx descriptor ring to store buffers on
  827. * @old_buff: donor buffer to have page reused
  828. *
  829. * Synchronizes page for reuse by the adapter
  830. **/
  831. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  832. struct i40e_rx_buffer *old_buff)
  833. {
  834. struct i40e_rx_buffer *new_buff;
  835. u16 nta = rx_ring->next_to_alloc;
  836. new_buff = &rx_ring->rx_bi[nta];
  837. /* update, and store next to alloc */
  838. nta++;
  839. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  840. /* transfer page from old buffer to new buffer */
  841. *new_buff = *old_buff;
  842. }
  843. /**
  844. * i40e_page_is_reserved - check if reuse is possible
  845. * @page: page struct to check
  846. */
  847. static inline bool i40e_page_is_reserved(struct page *page)
  848. {
  849. return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
  850. }
  851. /**
  852. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  853. * @rx_ring: rx descriptor ring to transact packets on
  854. * @rx_buffer: buffer containing page to add
  855. * @rx_desc: descriptor containing length of buffer written by hardware
  856. * @skb: sk_buff to place the data into
  857. *
  858. * This function will add the data contained in rx_buffer->page to the skb.
  859. * This is done either through a direct copy if the data in the buffer is
  860. * less than the skb header size, otherwise it will just attach the page as
  861. * a frag to the skb.
  862. *
  863. * The function will then update the page offset if necessary and return
  864. * true if the buffer can be reused by the adapter.
  865. **/
  866. static bool i40e_add_rx_frag(struct i40e_ring *rx_ring,
  867. struct i40e_rx_buffer *rx_buffer,
  868. union i40e_rx_desc *rx_desc,
  869. struct sk_buff *skb)
  870. {
  871. struct page *page = rx_buffer->page;
  872. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  873. unsigned int size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  874. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  875. #if (PAGE_SIZE < 8192)
  876. unsigned int truesize = I40E_RXBUFFER_2048;
  877. #else
  878. unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
  879. unsigned int last_offset = PAGE_SIZE - I40E_RXBUFFER_2048;
  880. #endif
  881. /* will the data fit in the skb we allocated? if so, just
  882. * copy it as it is pretty small anyway
  883. */
  884. if ((size <= I40E_RX_HDR_SIZE) && !skb_is_nonlinear(skb)) {
  885. unsigned char *va = page_address(page) + rx_buffer->page_offset;
  886. memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
  887. /* page is not reserved, we can reuse buffer as-is */
  888. if (likely(!i40e_page_is_reserved(page)))
  889. return true;
  890. /* this page cannot be reused so discard it */
  891. __free_pages(page, 0);
  892. return false;
  893. }
  894. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
  895. rx_buffer->page_offset, size, truesize);
  896. /* avoid re-using remote pages */
  897. if (unlikely(i40e_page_is_reserved(page)))
  898. return false;
  899. #if (PAGE_SIZE < 8192)
  900. /* if we are only owner of page we can reuse it */
  901. if (unlikely(page_count(page) != 1))
  902. return false;
  903. /* flip page offset to other buffer */
  904. rx_buffer->page_offset ^= truesize;
  905. #else
  906. /* move offset up to the next cache line */
  907. rx_buffer->page_offset += truesize;
  908. if (rx_buffer->page_offset > last_offset)
  909. return false;
  910. #endif
  911. /* Even if we own the page, we are not allowed to use atomic_set()
  912. * This would break get_page_unless_zero() users.
  913. */
  914. get_page(rx_buffer->page);
  915. return true;
  916. }
  917. /**
  918. * i40evf_fetch_rx_buffer - Allocate skb and populate it
  919. * @rx_ring: rx descriptor ring to transact packets on
  920. * @rx_desc: descriptor containing info written by hardware
  921. *
  922. * This function allocates an skb on the fly, and populates it with the page
  923. * data from the current receive descriptor, taking care to set up the skb
  924. * correctly, as well as handling calling the page recycle function if
  925. * necessary.
  926. */
  927. static inline
  928. struct sk_buff *i40evf_fetch_rx_buffer(struct i40e_ring *rx_ring,
  929. union i40e_rx_desc *rx_desc)
  930. {
  931. struct i40e_rx_buffer *rx_buffer;
  932. struct sk_buff *skb;
  933. struct page *page;
  934. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  935. page = rx_buffer->page;
  936. prefetchw(page);
  937. skb = rx_buffer->skb;
  938. if (likely(!skb)) {
  939. void *page_addr = page_address(page) + rx_buffer->page_offset;
  940. /* prefetch first cache line of first page */
  941. prefetch(page_addr);
  942. #if L1_CACHE_BYTES < 128
  943. prefetch(page_addr + L1_CACHE_BYTES);
  944. #endif
  945. /* allocate a skb to store the frags */
  946. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  947. I40E_RX_HDR_SIZE,
  948. GFP_ATOMIC | __GFP_NOWARN);
  949. if (unlikely(!skb)) {
  950. rx_ring->rx_stats.alloc_buff_failed++;
  951. return NULL;
  952. }
  953. /* we will be copying header into skb->data in
  954. * pskb_may_pull so it is in our interest to prefetch
  955. * it now to avoid a possible cache miss
  956. */
  957. prefetchw(skb->data);
  958. } else {
  959. rx_buffer->skb = NULL;
  960. }
  961. /* we are reusing so sync this buffer for CPU use */
  962. dma_sync_single_range_for_cpu(rx_ring->dev,
  963. rx_buffer->dma,
  964. rx_buffer->page_offset,
  965. I40E_RXBUFFER_2048,
  966. DMA_FROM_DEVICE);
  967. /* pull page into skb */
  968. if (i40e_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
  969. /* hand second half of page back to the ring */
  970. i40e_reuse_rx_page(rx_ring, rx_buffer);
  971. rx_ring->rx_stats.page_reuse_count++;
  972. } else {
  973. /* we are not reusing the buffer so unmap it */
  974. dma_unmap_page(rx_ring->dev, rx_buffer->dma, PAGE_SIZE,
  975. DMA_FROM_DEVICE);
  976. }
  977. /* clear contents of buffer_info */
  978. rx_buffer->page = NULL;
  979. return skb;
  980. }
  981. /**
  982. * i40e_is_non_eop - process handling of non-EOP buffers
  983. * @rx_ring: Rx ring being processed
  984. * @rx_desc: Rx descriptor for current buffer
  985. * @skb: Current socket buffer containing buffer in progress
  986. *
  987. * This function updates next to clean. If the buffer is an EOP buffer
  988. * this function exits returning false, otherwise it will place the
  989. * sk_buff in the next buffer to be chained and return true indicating
  990. * that this is in fact a non-EOP buffer.
  991. **/
  992. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  993. union i40e_rx_desc *rx_desc,
  994. struct sk_buff *skb)
  995. {
  996. u32 ntc = rx_ring->next_to_clean + 1;
  997. /* fetch, update, and store next to clean */
  998. ntc = (ntc < rx_ring->count) ? ntc : 0;
  999. rx_ring->next_to_clean = ntc;
  1000. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1001. /* if we are the last buffer then there is nothing else to do */
  1002. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1003. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1004. return false;
  1005. /* place skb in next buffer to be received */
  1006. rx_ring->rx_bi[ntc].skb = skb;
  1007. rx_ring->rx_stats.non_eop_descs++;
  1008. return true;
  1009. }
  1010. /**
  1011. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1012. * @rx_ring: rx descriptor ring to transact packets on
  1013. * @budget: Total limit on number of packets to process
  1014. *
  1015. * This function provides a "bounce buffer" approach to Rx interrupt
  1016. * processing. The advantage to this is that on systems that have
  1017. * expensive overhead for IOMMU access this provides a means of avoiding
  1018. * it by maintaining the mapping of the page to the system.
  1019. *
  1020. * Returns amount of work completed
  1021. **/
  1022. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1023. {
  1024. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1025. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1026. bool failure = false;
  1027. while (likely(total_rx_packets < budget)) {
  1028. union i40e_rx_desc *rx_desc;
  1029. struct sk_buff *skb;
  1030. u32 rx_status;
  1031. u16 vlan_tag;
  1032. u8 rx_ptype;
  1033. u64 qword;
  1034. /* return some buffers to hardware, one at a time is too slow */
  1035. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1036. failure = failure ||
  1037. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  1038. cleaned_count = 0;
  1039. }
  1040. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1041. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1042. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1043. I40E_RXD_QW1_PTYPE_SHIFT;
  1044. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1045. I40E_RXD_QW1_STATUS_SHIFT;
  1046. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
  1047. break;
  1048. /* status_error_len will always be zero for unused descriptors
  1049. * because it's cleared in cleanup, and overlaps with hdr_addr
  1050. * which is always zero because packet split isn't used, if the
  1051. * hardware wrote DD then it will be non-zero
  1052. */
  1053. if (!rx_desc->wb.qword1.status_error_len)
  1054. break;
  1055. /* This memory barrier is needed to keep us from reading
  1056. * any other fields out of the rx_desc until we know the
  1057. * DD bit is set.
  1058. */
  1059. dma_rmb();
  1060. skb = i40evf_fetch_rx_buffer(rx_ring, rx_desc);
  1061. if (!skb)
  1062. break;
  1063. cleaned_count++;
  1064. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1065. continue;
  1066. /* ERR_MASK will only have valid bits if EOP set, and
  1067. * what we are doing here is actually checking
  1068. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1069. * the error field
  1070. */
  1071. if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1072. dev_kfree_skb_any(skb);
  1073. continue;
  1074. }
  1075. if (i40e_cleanup_headers(rx_ring, skb))
  1076. continue;
  1077. /* probably a little skewed due to removing CRC */
  1078. total_rx_bytes += skb->len;
  1079. /* populate checksum, VLAN, and protocol */
  1080. i40evf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1081. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1082. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1083. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1084. /* update budget accounting */
  1085. total_rx_packets++;
  1086. }
  1087. u64_stats_update_begin(&rx_ring->syncp);
  1088. rx_ring->stats.packets += total_rx_packets;
  1089. rx_ring->stats.bytes += total_rx_bytes;
  1090. u64_stats_update_end(&rx_ring->syncp);
  1091. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1092. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1093. /* guarantee a trip back through this routine if there was a failure */
  1094. return failure ? budget : total_rx_packets;
  1095. }
  1096. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1097. {
  1098. u32 val;
  1099. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1100. /* Don't clear PBA because that can cause lost interrupts that
  1101. * came in while we were cleaning/polling
  1102. */
  1103. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1104. (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
  1105. return val;
  1106. }
  1107. /* a small macro to shorten up some long lines */
  1108. #define INTREG I40E_VFINT_DYN_CTLN1
  1109. /**
  1110. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1111. * @vsi: the VSI we care about
  1112. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1113. *
  1114. **/
  1115. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1116. struct i40e_q_vector *q_vector)
  1117. {
  1118. struct i40e_hw *hw = &vsi->back->hw;
  1119. bool rx = false, tx = false;
  1120. u32 rxval, txval;
  1121. int vector;
  1122. vector = (q_vector->v_idx + vsi->base_vector);
  1123. /* avoid dynamic calculation if in countdown mode OR if
  1124. * all dynamic is disabled
  1125. */
  1126. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1127. if (q_vector->itr_countdown > 0 ||
  1128. (!ITR_IS_DYNAMIC(vsi->rx_itr_setting) &&
  1129. !ITR_IS_DYNAMIC(vsi->tx_itr_setting))) {
  1130. goto enable_int;
  1131. }
  1132. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting)) {
  1133. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1134. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1135. }
  1136. if (ITR_IS_DYNAMIC(vsi->tx_itr_setting)) {
  1137. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1138. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1139. }
  1140. if (rx || tx) {
  1141. /* get the higher of the two ITR adjustments and
  1142. * use the same value for both ITR registers
  1143. * when in adaptive mode (Rx and/or Tx)
  1144. */
  1145. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1146. q_vector->tx.itr = q_vector->rx.itr = itr;
  1147. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1148. tx = true;
  1149. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1150. rx = true;
  1151. }
  1152. /* only need to enable the interrupt once, but need
  1153. * to possibly update both ITR values
  1154. */
  1155. if (rx) {
  1156. /* set the INTENA_MSK_MASK so that this first write
  1157. * won't actually enable the interrupt, instead just
  1158. * updating the ITR (it's bit 31 PF and VF)
  1159. */
  1160. rxval |= BIT(31);
  1161. /* don't check _DOWN because interrupt isn't being enabled */
  1162. wr32(hw, INTREG(vector - 1), rxval);
  1163. }
  1164. enable_int:
  1165. if (!test_bit(__I40E_DOWN, &vsi->state))
  1166. wr32(hw, INTREG(vector - 1), txval);
  1167. if (q_vector->itr_countdown)
  1168. q_vector->itr_countdown--;
  1169. else
  1170. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1171. }
  1172. /**
  1173. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1174. * @napi: napi struct with our devices info in it
  1175. * @budget: amount of work driver is allowed to do this pass, in packets
  1176. *
  1177. * This function will clean all queues associated with a q_vector.
  1178. *
  1179. * Returns the amount of work done
  1180. **/
  1181. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1182. {
  1183. struct i40e_q_vector *q_vector =
  1184. container_of(napi, struct i40e_q_vector, napi);
  1185. struct i40e_vsi *vsi = q_vector->vsi;
  1186. struct i40e_ring *ring;
  1187. bool clean_complete = true;
  1188. bool arm_wb = false;
  1189. int budget_per_ring;
  1190. int work_done = 0;
  1191. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1192. napi_complete(napi);
  1193. return 0;
  1194. }
  1195. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1196. * budget and be more aggressive about cleaning up the Tx descriptors.
  1197. */
  1198. i40e_for_each_ring(ring, q_vector->tx) {
  1199. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  1200. clean_complete = false;
  1201. continue;
  1202. }
  1203. arm_wb |= ring->arm_wb;
  1204. ring->arm_wb = false;
  1205. }
  1206. /* Handle case where we are called by netpoll with a budget of 0 */
  1207. if (budget <= 0)
  1208. goto tx_only;
  1209. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1210. * allow the budget to go below 1 because that would exit polling early.
  1211. */
  1212. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1213. i40e_for_each_ring(ring, q_vector->rx) {
  1214. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  1215. work_done += cleaned;
  1216. /* if we clean as many as budgeted, we must not be done */
  1217. if (cleaned >= budget_per_ring)
  1218. clean_complete = false;
  1219. }
  1220. /* If work not completed, return budget and polling will return */
  1221. if (!clean_complete) {
  1222. tx_only:
  1223. if (arm_wb) {
  1224. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1225. i40e_enable_wb_on_itr(vsi, q_vector);
  1226. }
  1227. return budget;
  1228. }
  1229. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1230. q_vector->arm_wb_state = false;
  1231. /* Work is done so exit the polling mode and re-enable the interrupt */
  1232. napi_complete_done(napi, work_done);
  1233. i40e_update_enable_itr(vsi, q_vector);
  1234. return 0;
  1235. }
  1236. /**
  1237. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1238. * @skb: send buffer
  1239. * @tx_ring: ring to send buffer on
  1240. * @flags: the tx flags to be set
  1241. *
  1242. * Checks the skb and set up correspondingly several generic transmit flags
  1243. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1244. *
  1245. * Returns error code indicate the frame should be dropped upon error and the
  1246. * otherwise returns 0 to indicate the flags has been set properly.
  1247. **/
  1248. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1249. struct i40e_ring *tx_ring,
  1250. u32 *flags)
  1251. {
  1252. __be16 protocol = skb->protocol;
  1253. u32 tx_flags = 0;
  1254. if (protocol == htons(ETH_P_8021Q) &&
  1255. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1256. /* When HW VLAN acceleration is turned off by the user the
  1257. * stack sets the protocol to 8021q so that the driver
  1258. * can take any steps required to support the SW only
  1259. * VLAN handling. In our case the driver doesn't need
  1260. * to take any further steps so just set the protocol
  1261. * to the encapsulated ethertype.
  1262. */
  1263. skb->protocol = vlan_get_protocol(skb);
  1264. goto out;
  1265. }
  1266. /* if we have a HW VLAN tag being added, default to the HW one */
  1267. if (skb_vlan_tag_present(skb)) {
  1268. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1269. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1270. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1271. } else if (protocol == htons(ETH_P_8021Q)) {
  1272. struct vlan_hdr *vhdr, _vhdr;
  1273. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1274. if (!vhdr)
  1275. return -EINVAL;
  1276. protocol = vhdr->h_vlan_encapsulated_proto;
  1277. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1278. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1279. }
  1280. out:
  1281. *flags = tx_flags;
  1282. return 0;
  1283. }
  1284. /**
  1285. * i40e_tso - set up the tso context descriptor
  1286. * @skb: ptr to the skb we're sending
  1287. * @hdr_len: ptr to the size of the packet header
  1288. * @cd_type_cmd_tso_mss: Quad Word 1
  1289. *
  1290. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1291. **/
  1292. static int i40e_tso(struct sk_buff *skb, u8 *hdr_len, u64 *cd_type_cmd_tso_mss)
  1293. {
  1294. u64 cd_cmd, cd_tso_len, cd_mss;
  1295. union {
  1296. struct iphdr *v4;
  1297. struct ipv6hdr *v6;
  1298. unsigned char *hdr;
  1299. } ip;
  1300. union {
  1301. struct tcphdr *tcp;
  1302. struct udphdr *udp;
  1303. unsigned char *hdr;
  1304. } l4;
  1305. u32 paylen, l4_offset;
  1306. int err;
  1307. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1308. return 0;
  1309. if (!skb_is_gso(skb))
  1310. return 0;
  1311. err = skb_cow_head(skb, 0);
  1312. if (err < 0)
  1313. return err;
  1314. ip.hdr = skb_network_header(skb);
  1315. l4.hdr = skb_transport_header(skb);
  1316. /* initialize outer IP header fields */
  1317. if (ip.v4->version == 4) {
  1318. ip.v4->tot_len = 0;
  1319. ip.v4->check = 0;
  1320. } else {
  1321. ip.v6->payload_len = 0;
  1322. }
  1323. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1324. SKB_GSO_GRE_CSUM |
  1325. SKB_GSO_IPXIP4 |
  1326. SKB_GSO_IPXIP6 |
  1327. SKB_GSO_UDP_TUNNEL |
  1328. SKB_GSO_UDP_TUNNEL_CSUM)) {
  1329. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1330. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  1331. l4.udp->len = 0;
  1332. /* determine offset of outer transport header */
  1333. l4_offset = l4.hdr - skb->data;
  1334. /* remove payload length from outer checksum */
  1335. paylen = skb->len - l4_offset;
  1336. csum_replace_by_diff(&l4.udp->check, htonl(paylen));
  1337. }
  1338. /* reset pointers to inner headers */
  1339. ip.hdr = skb_inner_network_header(skb);
  1340. l4.hdr = skb_inner_transport_header(skb);
  1341. /* initialize inner IP header fields */
  1342. if (ip.v4->version == 4) {
  1343. ip.v4->tot_len = 0;
  1344. ip.v4->check = 0;
  1345. } else {
  1346. ip.v6->payload_len = 0;
  1347. }
  1348. }
  1349. /* determine offset of inner transport header */
  1350. l4_offset = l4.hdr - skb->data;
  1351. /* remove payload length from inner checksum */
  1352. paylen = skb->len - l4_offset;
  1353. csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
  1354. /* compute length of segmentation header */
  1355. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  1356. /* find the field values */
  1357. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1358. cd_tso_len = skb->len - *hdr_len;
  1359. cd_mss = skb_shinfo(skb)->gso_size;
  1360. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1361. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1362. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1363. return 1;
  1364. }
  1365. /**
  1366. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1367. * @skb: send buffer
  1368. * @tx_flags: pointer to Tx flags currently set
  1369. * @td_cmd: Tx descriptor command bits to set
  1370. * @td_offset: Tx descriptor header offsets to set
  1371. * @tx_ring: Tx descriptor ring
  1372. * @cd_tunneling: ptr to context desc bits
  1373. **/
  1374. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1375. u32 *td_cmd, u32 *td_offset,
  1376. struct i40e_ring *tx_ring,
  1377. u32 *cd_tunneling)
  1378. {
  1379. union {
  1380. struct iphdr *v4;
  1381. struct ipv6hdr *v6;
  1382. unsigned char *hdr;
  1383. } ip;
  1384. union {
  1385. struct tcphdr *tcp;
  1386. struct udphdr *udp;
  1387. unsigned char *hdr;
  1388. } l4;
  1389. unsigned char *exthdr;
  1390. u32 offset, cmd = 0;
  1391. __be16 frag_off;
  1392. u8 l4_proto = 0;
  1393. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1394. return 0;
  1395. ip.hdr = skb_network_header(skb);
  1396. l4.hdr = skb_transport_header(skb);
  1397. /* compute outer L2 header size */
  1398. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1399. if (skb->encapsulation) {
  1400. u32 tunnel = 0;
  1401. /* define outer network header type */
  1402. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1403. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1404. I40E_TX_CTX_EXT_IP_IPV4 :
  1405. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1406. l4_proto = ip.v4->protocol;
  1407. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1408. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  1409. exthdr = ip.hdr + sizeof(*ip.v6);
  1410. l4_proto = ip.v6->nexthdr;
  1411. if (l4.hdr != exthdr)
  1412. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1413. &l4_proto, &frag_off);
  1414. }
  1415. /* define outer transport */
  1416. switch (l4_proto) {
  1417. case IPPROTO_UDP:
  1418. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  1419. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1420. break;
  1421. case IPPROTO_GRE:
  1422. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  1423. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1424. break;
  1425. case IPPROTO_IPIP:
  1426. case IPPROTO_IPV6:
  1427. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1428. l4.hdr = skb_inner_network_header(skb);
  1429. break;
  1430. default:
  1431. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1432. return -1;
  1433. skb_checksum_help(skb);
  1434. return 0;
  1435. }
  1436. /* compute outer L3 header size */
  1437. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  1438. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  1439. /* switch IP header pointer from outer to inner header */
  1440. ip.hdr = skb_inner_network_header(skb);
  1441. /* compute tunnel header size */
  1442. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  1443. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1444. /* indicate if we need to offload outer UDP header */
  1445. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  1446. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1447. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  1448. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1449. /* record tunnel offload values */
  1450. *cd_tunneling |= tunnel;
  1451. /* switch L4 header pointer from outer to inner */
  1452. l4.hdr = skb_inner_transport_header(skb);
  1453. l4_proto = 0;
  1454. /* reset type as we transition from outer to inner headers */
  1455. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  1456. if (ip.v4->version == 4)
  1457. *tx_flags |= I40E_TX_FLAGS_IPV4;
  1458. if (ip.v6->version == 6)
  1459. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1460. }
  1461. /* Enable IP checksum offloads */
  1462. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1463. l4_proto = ip.v4->protocol;
  1464. /* the stack computes the IP header already, the only time we
  1465. * need the hardware to recompute it is in the case of TSO.
  1466. */
  1467. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1468. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  1469. I40E_TX_DESC_CMD_IIPT_IPV4;
  1470. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1471. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1472. exthdr = ip.hdr + sizeof(*ip.v6);
  1473. l4_proto = ip.v6->nexthdr;
  1474. if (l4.hdr != exthdr)
  1475. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1476. &l4_proto, &frag_off);
  1477. }
  1478. /* compute inner L3 header size */
  1479. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1480. /* Enable L4 checksum offloads */
  1481. switch (l4_proto) {
  1482. case IPPROTO_TCP:
  1483. /* enable checksum offloads */
  1484. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1485. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1486. break;
  1487. case IPPROTO_SCTP:
  1488. /* enable SCTP checksum offload */
  1489. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1490. offset |= (sizeof(struct sctphdr) >> 2) <<
  1491. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1492. break;
  1493. case IPPROTO_UDP:
  1494. /* enable UDP checksum offload */
  1495. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1496. offset |= (sizeof(struct udphdr) >> 2) <<
  1497. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1498. break;
  1499. default:
  1500. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1501. return -1;
  1502. skb_checksum_help(skb);
  1503. return 0;
  1504. }
  1505. *td_cmd |= cmd;
  1506. *td_offset |= offset;
  1507. return 1;
  1508. }
  1509. /**
  1510. * i40e_create_tx_ctx Build the Tx context descriptor
  1511. * @tx_ring: ring to create the descriptor on
  1512. * @cd_type_cmd_tso_mss: Quad Word 1
  1513. * @cd_tunneling: Quad Word 0 - bits 0-31
  1514. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1515. **…

Large files files are truncated, but you can click here to view the full file