i40e_txrx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 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. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Contact Information:
  19. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  20. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  21. *
  22. ******************************************************************************/
  23. #include <linux/prefetch.h>
  24. #include "i40evf.h"
  25. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  26. u32 td_tag)
  27. {
  28. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  29. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  30. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  31. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  32. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  33. }
  34. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  35. /**
  36. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  37. * @ring: the ring that owns the buffer
  38. * @tx_buffer: the buffer to free
  39. **/
  40. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  41. struct i40e_tx_buffer *tx_buffer)
  42. {
  43. if (tx_buffer->skb) {
  44. dev_kfree_skb_any(tx_buffer->skb);
  45. if (dma_unmap_len(tx_buffer, len))
  46. dma_unmap_single(ring->dev,
  47. dma_unmap_addr(tx_buffer, dma),
  48. dma_unmap_len(tx_buffer, len),
  49. DMA_TO_DEVICE);
  50. } else if (dma_unmap_len(tx_buffer, len)) {
  51. dma_unmap_page(ring->dev,
  52. dma_unmap_addr(tx_buffer, dma),
  53. dma_unmap_len(tx_buffer, len),
  54. DMA_TO_DEVICE);
  55. }
  56. tx_buffer->next_to_watch = NULL;
  57. tx_buffer->skb = NULL;
  58. dma_unmap_len_set(tx_buffer, len, 0);
  59. /* tx_buffer must be completely set up in the transmit path */
  60. }
  61. /**
  62. * i40evf_clean_tx_ring - Free any empty Tx buffers
  63. * @tx_ring: ring to be cleaned
  64. **/
  65. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  66. {
  67. unsigned long bi_size;
  68. u16 i;
  69. /* ring already cleared, nothing to do */
  70. if (!tx_ring->tx_bi)
  71. return;
  72. /* Free all the Tx ring sk_buffs */
  73. for (i = 0; i < tx_ring->count; i++)
  74. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  75. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  76. memset(tx_ring->tx_bi, 0, bi_size);
  77. /* Zero out the descriptor ring */
  78. memset(tx_ring->desc, 0, tx_ring->size);
  79. tx_ring->next_to_use = 0;
  80. tx_ring->next_to_clean = 0;
  81. if (!tx_ring->netdev)
  82. return;
  83. /* cleanup Tx queue statistics */
  84. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  85. tx_ring->queue_index));
  86. }
  87. /**
  88. * i40evf_free_tx_resources - Free Tx resources per queue
  89. * @tx_ring: Tx descriptor ring for a specific queue
  90. *
  91. * Free all transmit software resources
  92. **/
  93. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  94. {
  95. i40evf_clean_tx_ring(tx_ring);
  96. kfree(tx_ring->tx_bi);
  97. tx_ring->tx_bi = NULL;
  98. if (tx_ring->desc) {
  99. dma_free_coherent(tx_ring->dev, tx_ring->size,
  100. tx_ring->desc, tx_ring->dma);
  101. tx_ring->desc = NULL;
  102. }
  103. }
  104. /**
  105. * i40e_get_tx_pending - how many tx descriptors not processed
  106. * @tx_ring: the ring of descriptors
  107. *
  108. * Since there is no access to the ring head register
  109. * in XL710, we need to use our local copies
  110. **/
  111. static u32 i40e_get_tx_pending(struct i40e_ring *ring)
  112. {
  113. u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
  114. ? ring->next_to_use
  115. : ring->next_to_use + ring->count);
  116. return ntu - ring->next_to_clean;
  117. }
  118. /**
  119. * i40e_check_tx_hang - Is there a hang in the Tx queue
  120. * @tx_ring: the ring of descriptors
  121. **/
  122. static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
  123. {
  124. u32 tx_pending = i40e_get_tx_pending(tx_ring);
  125. bool ret = false;
  126. clear_check_for_tx_hang(tx_ring);
  127. /* Check for a hung queue, but be thorough. This verifies
  128. * that a transmit has been completed since the previous
  129. * check AND there is at least one packet pending. The
  130. * ARMED bit is set to indicate a potential hang. The
  131. * bit is cleared if a pause frame is received to remove
  132. * false hang detection due to PFC or 802.3x frames. By
  133. * requiring this to fail twice we avoid races with
  134. * PFC clearing the ARMED bit and conditions where we
  135. * run the check_tx_hang logic with a transmit completion
  136. * pending but without time to complete it yet.
  137. */
  138. if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
  139. tx_pending) {
  140. /* make sure it is true for two checks in a row */
  141. ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
  142. &tx_ring->state);
  143. } else {
  144. /* update completed stats and disarm the hang check */
  145. tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
  146. clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
  147. }
  148. return ret;
  149. }
  150. /**
  151. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  152. * @tx_ring: tx ring to clean
  153. * @budget: how many cleans we're allowed
  154. *
  155. * Returns true if there's any budget left (e.g. the clean is finished)
  156. **/
  157. static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
  158. {
  159. u16 i = tx_ring->next_to_clean;
  160. struct i40e_tx_buffer *tx_buf;
  161. struct i40e_tx_desc *tx_desc;
  162. unsigned int total_packets = 0;
  163. unsigned int total_bytes = 0;
  164. tx_buf = &tx_ring->tx_bi[i];
  165. tx_desc = I40E_TX_DESC(tx_ring, i);
  166. i -= tx_ring->count;
  167. do {
  168. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  169. /* if next_to_watch is not set then there is no work pending */
  170. if (!eop_desc)
  171. break;
  172. /* prevent any other reads prior to eop_desc */
  173. read_barrier_depends();
  174. /* if the descriptor isn't done, no work yet to do */
  175. if (!(eop_desc->cmd_type_offset_bsz &
  176. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  177. break;
  178. /* clear next_to_watch to prevent false hangs */
  179. tx_buf->next_to_watch = NULL;
  180. /* update the statistics for this packet */
  181. total_bytes += tx_buf->bytecount;
  182. total_packets += tx_buf->gso_segs;
  183. /* free the skb */
  184. dev_kfree_skb_any(tx_buf->skb);
  185. /* unmap skb header data */
  186. dma_unmap_single(tx_ring->dev,
  187. dma_unmap_addr(tx_buf, dma),
  188. dma_unmap_len(tx_buf, len),
  189. DMA_TO_DEVICE);
  190. /* clear tx_buffer data */
  191. tx_buf->skb = NULL;
  192. dma_unmap_len_set(tx_buf, len, 0);
  193. /* unmap remaining buffers */
  194. while (tx_desc != eop_desc) {
  195. tx_buf++;
  196. tx_desc++;
  197. i++;
  198. if (unlikely(!i)) {
  199. i -= tx_ring->count;
  200. tx_buf = tx_ring->tx_bi;
  201. tx_desc = I40E_TX_DESC(tx_ring, 0);
  202. }
  203. /* unmap any remaining paged data */
  204. if (dma_unmap_len(tx_buf, len)) {
  205. dma_unmap_page(tx_ring->dev,
  206. dma_unmap_addr(tx_buf, dma),
  207. dma_unmap_len(tx_buf, len),
  208. DMA_TO_DEVICE);
  209. dma_unmap_len_set(tx_buf, len, 0);
  210. }
  211. }
  212. /* move us one more past the eop_desc for start of next pkt */
  213. tx_buf++;
  214. tx_desc++;
  215. i++;
  216. if (unlikely(!i)) {
  217. i -= tx_ring->count;
  218. tx_buf = tx_ring->tx_bi;
  219. tx_desc = I40E_TX_DESC(tx_ring, 0);
  220. }
  221. /* update budget accounting */
  222. budget--;
  223. } while (likely(budget));
  224. i += tx_ring->count;
  225. tx_ring->next_to_clean = i;
  226. u64_stats_update_begin(&tx_ring->syncp);
  227. tx_ring->stats.bytes += total_bytes;
  228. tx_ring->stats.packets += total_packets;
  229. u64_stats_update_end(&tx_ring->syncp);
  230. tx_ring->q_vector->tx.total_bytes += total_bytes;
  231. tx_ring->q_vector->tx.total_packets += total_packets;
  232. if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
  233. /* schedule immediate reset if we believe we hung */
  234. dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
  235. " VSI <%d>\n"
  236. " Tx Queue <%d>\n"
  237. " next_to_use <%x>\n"
  238. " next_to_clean <%x>\n",
  239. tx_ring->vsi->seid,
  240. tx_ring->queue_index,
  241. tx_ring->next_to_use, i);
  242. dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
  243. " time_stamp <%lx>\n"
  244. " jiffies <%lx>\n",
  245. tx_ring->tx_bi[i].time_stamp, jiffies);
  246. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  247. dev_info(tx_ring->dev,
  248. "tx hang detected on queue %d, resetting adapter\n",
  249. tx_ring->queue_index);
  250. tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
  251. /* the adapter is about to reset, no point in enabling stuff */
  252. return true;
  253. }
  254. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  255. tx_ring->queue_index),
  256. total_packets, total_bytes);
  257. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  258. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  259. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  260. /* Make sure that anybody stopping the queue after this
  261. * sees the new next_to_clean.
  262. */
  263. smp_mb();
  264. if (__netif_subqueue_stopped(tx_ring->netdev,
  265. tx_ring->queue_index) &&
  266. !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
  267. netif_wake_subqueue(tx_ring->netdev,
  268. tx_ring->queue_index);
  269. ++tx_ring->tx_stats.restart_queue;
  270. }
  271. }
  272. return budget > 0;
  273. }
  274. /**
  275. * i40e_set_new_dynamic_itr - Find new ITR level
  276. * @rc: structure containing ring performance data
  277. *
  278. * Stores a new ITR value based on packets and byte counts during
  279. * the last interrupt. The advantage of per interrupt computation
  280. * is faster updates and more accurate ITR for the current traffic
  281. * pattern. Constants in this function were computed based on
  282. * theoretical maximum wire speed and thresholds were set based on
  283. * testing data as well as attempting to minimize response time
  284. * while increasing bulk throughput.
  285. **/
  286. static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  287. {
  288. enum i40e_latency_range new_latency_range = rc->latency_range;
  289. u32 new_itr = rc->itr;
  290. int bytes_per_int;
  291. if (rc->total_packets == 0 || !rc->itr)
  292. return;
  293. /* simple throttlerate management
  294. * 0-10MB/s lowest (100000 ints/s)
  295. * 10-20MB/s low (20000 ints/s)
  296. * 20-1249MB/s bulk (8000 ints/s)
  297. */
  298. bytes_per_int = rc->total_bytes / rc->itr;
  299. switch (rc->itr) {
  300. case I40E_LOWEST_LATENCY:
  301. if (bytes_per_int > 10)
  302. new_latency_range = I40E_LOW_LATENCY;
  303. break;
  304. case I40E_LOW_LATENCY:
  305. if (bytes_per_int > 20)
  306. new_latency_range = I40E_BULK_LATENCY;
  307. else if (bytes_per_int <= 10)
  308. new_latency_range = I40E_LOWEST_LATENCY;
  309. break;
  310. case I40E_BULK_LATENCY:
  311. if (bytes_per_int <= 20)
  312. rc->latency_range = I40E_LOW_LATENCY;
  313. break;
  314. }
  315. switch (new_latency_range) {
  316. case I40E_LOWEST_LATENCY:
  317. new_itr = I40E_ITR_100K;
  318. break;
  319. case I40E_LOW_LATENCY:
  320. new_itr = I40E_ITR_20K;
  321. break;
  322. case I40E_BULK_LATENCY:
  323. new_itr = I40E_ITR_8K;
  324. break;
  325. default:
  326. break;
  327. }
  328. if (new_itr != rc->itr) {
  329. /* do an exponential smoothing */
  330. new_itr = (10 * new_itr * rc->itr) /
  331. ((9 * new_itr) + rc->itr);
  332. rc->itr = new_itr & I40E_MAX_ITR;
  333. }
  334. rc->total_bytes = 0;
  335. rc->total_packets = 0;
  336. }
  337. /**
  338. * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
  339. * @q_vector: the vector to adjust
  340. **/
  341. static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
  342. {
  343. u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
  344. struct i40e_hw *hw = &q_vector->vsi->back->hw;
  345. u32 reg_addr;
  346. u16 old_itr;
  347. reg_addr = I40E_VFINT_ITRN1(I40E_RX_ITR, vector - 1);
  348. old_itr = q_vector->rx.itr;
  349. i40e_set_new_dynamic_itr(&q_vector->rx);
  350. if (old_itr != q_vector->rx.itr)
  351. wr32(hw, reg_addr, q_vector->rx.itr);
  352. reg_addr = I40E_VFINT_ITRN1(I40E_TX_ITR, vector - 1);
  353. old_itr = q_vector->tx.itr;
  354. i40e_set_new_dynamic_itr(&q_vector->tx);
  355. if (old_itr != q_vector->tx.itr)
  356. wr32(hw, reg_addr, q_vector->tx.itr);
  357. }
  358. /**
  359. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  360. * @tx_ring: the tx ring to set up
  361. *
  362. * Return 0 on success, negative on error
  363. **/
  364. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  365. {
  366. struct device *dev = tx_ring->dev;
  367. int bi_size;
  368. if (!dev)
  369. return -ENOMEM;
  370. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  371. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  372. if (!tx_ring->tx_bi)
  373. goto err;
  374. /* round up to nearest 4K */
  375. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  376. tx_ring->size = ALIGN(tx_ring->size, 4096);
  377. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  378. &tx_ring->dma, GFP_KERNEL);
  379. if (!tx_ring->desc) {
  380. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  381. tx_ring->size);
  382. goto err;
  383. }
  384. tx_ring->next_to_use = 0;
  385. tx_ring->next_to_clean = 0;
  386. return 0;
  387. err:
  388. kfree(tx_ring->tx_bi);
  389. tx_ring->tx_bi = NULL;
  390. return -ENOMEM;
  391. }
  392. /**
  393. * i40evf_clean_rx_ring - Free Rx buffers
  394. * @rx_ring: ring to be cleaned
  395. **/
  396. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  397. {
  398. struct device *dev = rx_ring->dev;
  399. struct i40e_rx_buffer *rx_bi;
  400. unsigned long bi_size;
  401. u16 i;
  402. /* ring already cleared, nothing to do */
  403. if (!rx_ring->rx_bi)
  404. return;
  405. /* Free all the Rx ring sk_buffs */
  406. for (i = 0; i < rx_ring->count; i++) {
  407. rx_bi = &rx_ring->rx_bi[i];
  408. if (rx_bi->dma) {
  409. dma_unmap_single(dev,
  410. rx_bi->dma,
  411. rx_ring->rx_buf_len,
  412. DMA_FROM_DEVICE);
  413. rx_bi->dma = 0;
  414. }
  415. if (rx_bi->skb) {
  416. dev_kfree_skb(rx_bi->skb);
  417. rx_bi->skb = NULL;
  418. }
  419. if (rx_bi->page) {
  420. if (rx_bi->page_dma) {
  421. dma_unmap_page(dev,
  422. rx_bi->page_dma,
  423. PAGE_SIZE / 2,
  424. DMA_FROM_DEVICE);
  425. rx_bi->page_dma = 0;
  426. }
  427. __free_page(rx_bi->page);
  428. rx_bi->page = NULL;
  429. rx_bi->page_offset = 0;
  430. }
  431. }
  432. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  433. memset(rx_ring->rx_bi, 0, bi_size);
  434. /* Zero out the descriptor ring */
  435. memset(rx_ring->desc, 0, rx_ring->size);
  436. rx_ring->next_to_clean = 0;
  437. rx_ring->next_to_use = 0;
  438. }
  439. /**
  440. * i40evf_free_rx_resources - Free Rx resources
  441. * @rx_ring: ring to clean the resources from
  442. *
  443. * Free all receive software resources
  444. **/
  445. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  446. {
  447. i40evf_clean_rx_ring(rx_ring);
  448. kfree(rx_ring->rx_bi);
  449. rx_ring->rx_bi = NULL;
  450. if (rx_ring->desc) {
  451. dma_free_coherent(rx_ring->dev, rx_ring->size,
  452. rx_ring->desc, rx_ring->dma);
  453. rx_ring->desc = NULL;
  454. }
  455. }
  456. /**
  457. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  458. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  459. *
  460. * Returns 0 on success, negative on failure
  461. **/
  462. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  463. {
  464. struct device *dev = rx_ring->dev;
  465. int bi_size;
  466. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  467. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  468. if (!rx_ring->rx_bi)
  469. goto err;
  470. /* Round up to nearest 4K */
  471. rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
  472. ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
  473. : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  474. rx_ring->size = ALIGN(rx_ring->size, 4096);
  475. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  476. &rx_ring->dma, GFP_KERNEL);
  477. if (!rx_ring->desc) {
  478. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  479. rx_ring->size);
  480. goto err;
  481. }
  482. rx_ring->next_to_clean = 0;
  483. rx_ring->next_to_use = 0;
  484. return 0;
  485. err:
  486. kfree(rx_ring->rx_bi);
  487. rx_ring->rx_bi = NULL;
  488. return -ENOMEM;
  489. }
  490. /**
  491. * i40e_release_rx_desc - Store the new tail and head values
  492. * @rx_ring: ring to bump
  493. * @val: new head index
  494. **/
  495. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  496. {
  497. rx_ring->next_to_use = val;
  498. /* Force memory writes to complete before letting h/w
  499. * know there are new descriptors to fetch. (Only
  500. * applicable for weak-ordered memory model archs,
  501. * such as IA-64).
  502. */
  503. wmb();
  504. writel(val, rx_ring->tail);
  505. }
  506. /**
  507. * i40evf_alloc_rx_buffers - Replace used receive buffers; packet split
  508. * @rx_ring: ring to place buffers on
  509. * @cleaned_count: number of buffers to replace
  510. **/
  511. void i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  512. {
  513. u16 i = rx_ring->next_to_use;
  514. union i40e_rx_desc *rx_desc;
  515. struct i40e_rx_buffer *bi;
  516. struct sk_buff *skb;
  517. /* do nothing if no valid netdev defined */
  518. if (!rx_ring->netdev || !cleaned_count)
  519. return;
  520. while (cleaned_count--) {
  521. rx_desc = I40E_RX_DESC(rx_ring, i);
  522. bi = &rx_ring->rx_bi[i];
  523. skb = bi->skb;
  524. if (!skb) {
  525. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  526. rx_ring->rx_buf_len);
  527. if (!skb) {
  528. rx_ring->rx_stats.alloc_buff_failed++;
  529. goto no_buffers;
  530. }
  531. /* initialize queue mapping */
  532. skb_record_rx_queue(skb, rx_ring->queue_index);
  533. bi->skb = skb;
  534. }
  535. if (!bi->dma) {
  536. bi->dma = dma_map_single(rx_ring->dev,
  537. skb->data,
  538. rx_ring->rx_buf_len,
  539. DMA_FROM_DEVICE);
  540. if (dma_mapping_error(rx_ring->dev, bi->dma)) {
  541. rx_ring->rx_stats.alloc_buff_failed++;
  542. bi->dma = 0;
  543. goto no_buffers;
  544. }
  545. }
  546. if (ring_is_ps_enabled(rx_ring)) {
  547. if (!bi->page) {
  548. bi->page = alloc_page(GFP_ATOMIC);
  549. if (!bi->page) {
  550. rx_ring->rx_stats.alloc_page_failed++;
  551. goto no_buffers;
  552. }
  553. }
  554. if (!bi->page_dma) {
  555. /* use a half page if we're re-using */
  556. bi->page_offset ^= PAGE_SIZE / 2;
  557. bi->page_dma = dma_map_page(rx_ring->dev,
  558. bi->page,
  559. bi->page_offset,
  560. PAGE_SIZE / 2,
  561. DMA_FROM_DEVICE);
  562. if (dma_mapping_error(rx_ring->dev,
  563. bi->page_dma)) {
  564. rx_ring->rx_stats.alloc_page_failed++;
  565. bi->page_dma = 0;
  566. goto no_buffers;
  567. }
  568. }
  569. /* Refresh the desc even if buffer_addrs didn't change
  570. * because each write-back erases this info.
  571. */
  572. rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
  573. rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
  574. } else {
  575. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  576. rx_desc->read.hdr_addr = 0;
  577. }
  578. i++;
  579. if (i == rx_ring->count)
  580. i = 0;
  581. }
  582. no_buffers:
  583. if (rx_ring->next_to_use != i)
  584. i40e_release_rx_desc(rx_ring, i);
  585. }
  586. /**
  587. * i40e_receive_skb - Send a completed packet up the stack
  588. * @rx_ring: rx ring in play
  589. * @skb: packet to send up
  590. * @vlan_tag: vlan tag for packet
  591. **/
  592. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  593. struct sk_buff *skb, u16 vlan_tag)
  594. {
  595. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  596. struct i40e_vsi *vsi = rx_ring->vsi;
  597. u64 flags = vsi->back->flags;
  598. if (vlan_tag & VLAN_VID_MASK)
  599. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  600. if (flags & I40E_FLAG_IN_NETPOLL)
  601. netif_rx(skb);
  602. else
  603. napi_gro_receive(&q_vector->napi, skb);
  604. }
  605. /**
  606. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  607. * @vsi: the VSI we care about
  608. * @skb: skb currently being received and modified
  609. * @rx_status: status value of last descriptor in packet
  610. * @rx_error: error value of last descriptor in packet
  611. * @rx_ptype: ptype value of last descriptor in packet
  612. **/
  613. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  614. struct sk_buff *skb,
  615. u32 rx_status,
  616. u32 rx_error,
  617. u16 rx_ptype)
  618. {
  619. bool ipv4_tunnel, ipv6_tunnel;
  620. __wsum rx_udp_csum;
  621. __sum16 csum;
  622. struct iphdr *iph;
  623. ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
  624. (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
  625. ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
  626. (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
  627. skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
  628. skb->ip_summed = CHECKSUM_NONE;
  629. /* Rx csum enabled and ip headers found? */
  630. if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
  631. rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  632. return;
  633. /* likely incorrect csum if alternate IP extention headers found */
  634. if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  635. return;
  636. /* IP or L4 or outmost IP checksum error */
  637. if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
  638. (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
  639. (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
  640. vsi->back->hw_csum_rx_error++;
  641. return;
  642. }
  643. if (ipv4_tunnel &&
  644. !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
  645. /* If VXLAN traffic has an outer UDPv4 checksum we need to check
  646. * it in the driver, hardware does not do it for us.
  647. * Since L3L4P bit was set we assume a valid IHL value (>=5)
  648. * so the total length of IPv4 header is IHL*4 bytes
  649. */
  650. skb->transport_header = skb->mac_header +
  651. sizeof(struct ethhdr) +
  652. (ip_hdr(skb)->ihl * 4);
  653. /* Add 4 bytes for VLAN tagged packets */
  654. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  655. skb->protocol == htons(ETH_P_8021AD))
  656. ? VLAN_HLEN : 0;
  657. rx_udp_csum = udp_csum(skb);
  658. iph = ip_hdr(skb);
  659. csum = csum_tcpudp_magic(
  660. iph->saddr, iph->daddr,
  661. (skb->len - skb_transport_offset(skb)),
  662. IPPROTO_UDP, rx_udp_csum);
  663. if (udp_hdr(skb)->check != csum) {
  664. vsi->back->hw_csum_rx_error++;
  665. return;
  666. }
  667. }
  668. skb->ip_summed = CHECKSUM_UNNECESSARY;
  669. }
  670. /**
  671. * i40e_rx_hash - returns the hash value from the Rx descriptor
  672. * @ring: descriptor ring
  673. * @rx_desc: specific descriptor
  674. **/
  675. static inline u32 i40e_rx_hash(struct i40e_ring *ring,
  676. union i40e_rx_desc *rx_desc)
  677. {
  678. const __le64 rss_mask =
  679. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  680. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  681. if ((ring->netdev->features & NETIF_F_RXHASH) &&
  682. (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
  683. return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  684. else
  685. return 0;
  686. }
  687. /**
  688. * i40e_clean_rx_irq - Reclaim resources after receive completes
  689. * @rx_ring: rx ring to clean
  690. * @budget: how many cleans we're allowed
  691. *
  692. * Returns true if there's any budget left (e.g. the clean is finished)
  693. **/
  694. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  695. {
  696. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  697. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  698. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  699. const int current_node = numa_node_id();
  700. struct i40e_vsi *vsi = rx_ring->vsi;
  701. u16 i = rx_ring->next_to_clean;
  702. union i40e_rx_desc *rx_desc;
  703. u32 rx_error, rx_status;
  704. u64 qword;
  705. u16 rx_ptype;
  706. rx_desc = I40E_RX_DESC(rx_ring, i);
  707. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  708. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK)
  709. >> I40E_RXD_QW1_STATUS_SHIFT;
  710. while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
  711. union i40e_rx_desc *next_rxd;
  712. struct i40e_rx_buffer *rx_bi;
  713. struct sk_buff *skb;
  714. u16 vlan_tag;
  715. rx_bi = &rx_ring->rx_bi[i];
  716. skb = rx_bi->skb;
  717. prefetch(skb->data);
  718. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  719. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  720. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  721. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  722. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  723. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  724. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  725. I40E_RXD_QW1_ERROR_SHIFT;
  726. rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  727. rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  728. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  729. I40E_RXD_QW1_PTYPE_SHIFT;
  730. rx_bi->skb = NULL;
  731. /* This memory barrier is needed to keep us from reading
  732. * any other fields out of the rx_desc until we know the
  733. * STATUS_DD bit is set
  734. */
  735. rmb();
  736. /* Get the header and possibly the whole packet
  737. * If this is an skb from previous receive dma will be 0
  738. */
  739. if (rx_bi->dma) {
  740. u16 len;
  741. if (rx_hbo)
  742. len = I40E_RX_HDR_SIZE;
  743. else if (rx_sph)
  744. len = rx_header_len;
  745. else if (rx_packet_len)
  746. len = rx_packet_len; /* 1buf/no split found */
  747. else
  748. len = rx_header_len; /* split always mode */
  749. skb_put(skb, len);
  750. dma_unmap_single(rx_ring->dev,
  751. rx_bi->dma,
  752. rx_ring->rx_buf_len,
  753. DMA_FROM_DEVICE);
  754. rx_bi->dma = 0;
  755. }
  756. /* Get the rest of the data if this was a header split */
  757. if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
  758. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  759. rx_bi->page,
  760. rx_bi->page_offset,
  761. rx_packet_len);
  762. skb->len += rx_packet_len;
  763. skb->data_len += rx_packet_len;
  764. skb->truesize += rx_packet_len;
  765. if ((page_count(rx_bi->page) == 1) &&
  766. (page_to_nid(rx_bi->page) == current_node))
  767. get_page(rx_bi->page);
  768. else
  769. rx_bi->page = NULL;
  770. dma_unmap_page(rx_ring->dev,
  771. rx_bi->page_dma,
  772. PAGE_SIZE / 2,
  773. DMA_FROM_DEVICE);
  774. rx_bi->page_dma = 0;
  775. }
  776. I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
  777. if (unlikely(
  778. !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  779. struct i40e_rx_buffer *next_buffer;
  780. next_buffer = &rx_ring->rx_bi[i];
  781. if (ring_is_ps_enabled(rx_ring)) {
  782. rx_bi->skb = next_buffer->skb;
  783. rx_bi->dma = next_buffer->dma;
  784. next_buffer->skb = skb;
  785. next_buffer->dma = 0;
  786. }
  787. rx_ring->rx_stats.non_eop_descs++;
  788. goto next_desc;
  789. }
  790. /* ERR_MASK will only have valid bits if EOP set */
  791. if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  792. dev_kfree_skb_any(skb);
  793. goto next_desc;
  794. }
  795. skb->rxhash = i40e_rx_hash(rx_ring, rx_desc);
  796. /* probably a little skewed due to removing CRC */
  797. total_rx_bytes += skb->len;
  798. total_rx_packets++;
  799. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  800. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  801. vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  802. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  803. : 0;
  804. i40e_receive_skb(rx_ring, skb, vlan_tag);
  805. rx_ring->netdev->last_rx = jiffies;
  806. budget--;
  807. next_desc:
  808. rx_desc->wb.qword1.status_error_len = 0;
  809. if (!budget)
  810. break;
  811. cleaned_count++;
  812. /* return some buffers to hardware, one at a time is too slow */
  813. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  814. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  815. cleaned_count = 0;
  816. }
  817. /* use prefetched values */
  818. rx_desc = next_rxd;
  819. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  820. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  821. I40E_RXD_QW1_STATUS_SHIFT;
  822. }
  823. rx_ring->next_to_clean = i;
  824. u64_stats_update_begin(&rx_ring->syncp);
  825. rx_ring->stats.packets += total_rx_packets;
  826. rx_ring->stats.bytes += total_rx_bytes;
  827. u64_stats_update_end(&rx_ring->syncp);
  828. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  829. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  830. if (cleaned_count)
  831. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  832. return budget > 0;
  833. }
  834. /**
  835. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  836. * @napi: napi struct with our devices info in it
  837. * @budget: amount of work driver is allowed to do this pass, in packets
  838. *
  839. * This function will clean all queues associated with a q_vector.
  840. *
  841. * Returns the amount of work done
  842. **/
  843. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  844. {
  845. struct i40e_q_vector *q_vector =
  846. container_of(napi, struct i40e_q_vector, napi);
  847. struct i40e_vsi *vsi = q_vector->vsi;
  848. struct i40e_ring *ring;
  849. bool clean_complete = true;
  850. int budget_per_ring;
  851. if (test_bit(__I40E_DOWN, &vsi->state)) {
  852. napi_complete(napi);
  853. return 0;
  854. }
  855. /* Since the actual Tx work is minimal, we can give the Tx a larger
  856. * budget and be more aggressive about cleaning up the Tx descriptors.
  857. */
  858. i40e_for_each_ring(ring, q_vector->tx)
  859. clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
  860. /* We attempt to distribute budget to each Rx queue fairly, but don't
  861. * allow the budget to go below 1 because that would exit polling early.
  862. */
  863. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  864. i40e_for_each_ring(ring, q_vector->rx)
  865. clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
  866. /* If work not completed, return budget and polling will return */
  867. if (!clean_complete)
  868. return budget;
  869. /* Work is done so exit the polling mode and re-enable the interrupt */
  870. napi_complete(napi);
  871. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
  872. ITR_IS_DYNAMIC(vsi->tx_itr_setting))
  873. i40e_update_dynamic_itr(q_vector);
  874. if (!test_bit(__I40E_DOWN, &vsi->state))
  875. i40evf_irq_enable_queues(vsi->back, 1 << q_vector->v_idx);
  876. return 0;
  877. }
  878. /**
  879. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  880. * @skb: send buffer
  881. * @tx_ring: ring to send buffer on
  882. * @flags: the tx flags to be set
  883. *
  884. * Checks the skb and set up correspondingly several generic transmit flags
  885. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  886. *
  887. * Returns error code indicate the frame should be dropped upon error and the
  888. * otherwise returns 0 to indicate the flags has been set properly.
  889. **/
  890. static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  891. struct i40e_ring *tx_ring,
  892. u32 *flags)
  893. {
  894. __be16 protocol = skb->protocol;
  895. u32 tx_flags = 0;
  896. /* if we have a HW VLAN tag being added, default to the HW one */
  897. if (vlan_tx_tag_present(skb)) {
  898. tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  899. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  900. /* else if it is a SW VLAN, check the next protocol and store the tag */
  901. } else if (protocol == htons(ETH_P_8021Q)) {
  902. struct vlan_hdr *vhdr, _vhdr;
  903. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  904. if (!vhdr)
  905. return -EINVAL;
  906. protocol = vhdr->h_vlan_encapsulated_proto;
  907. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  908. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  909. }
  910. *flags = tx_flags;
  911. return 0;
  912. }
  913. /**
  914. * i40e_tso - set up the tso context descriptor
  915. * @tx_ring: ptr to the ring to send
  916. * @skb: ptr to the skb we're sending
  917. * @tx_flags: the collected send information
  918. * @protocol: the send protocol
  919. * @hdr_len: ptr to the size of the packet header
  920. * @cd_tunneling: ptr to context descriptor bits
  921. *
  922. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  923. **/
  924. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  925. u32 tx_flags, __be16 protocol, u8 *hdr_len,
  926. u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
  927. {
  928. u32 cd_cmd, cd_tso_len, cd_mss;
  929. struct tcphdr *tcph;
  930. struct iphdr *iph;
  931. u32 l4len;
  932. int err;
  933. struct ipv6hdr *ipv6h;
  934. if (!skb_is_gso(skb))
  935. return 0;
  936. if (skb_header_cloned(skb)) {
  937. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  938. if (err)
  939. return err;
  940. }
  941. if (protocol == htons(ETH_P_IP)) {
  942. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  943. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  944. iph->tot_len = 0;
  945. iph->check = 0;
  946. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  947. 0, IPPROTO_TCP, 0);
  948. } else if (skb_is_gso_v6(skb)) {
  949. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
  950. : ipv6_hdr(skb);
  951. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  952. ipv6h->payload_len = 0;
  953. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  954. 0, IPPROTO_TCP, 0);
  955. }
  956. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  957. *hdr_len = (skb->encapsulation
  958. ? (skb_inner_transport_header(skb) - skb->data)
  959. : skb_transport_offset(skb)) + l4len;
  960. /* find the field values */
  961. cd_cmd = I40E_TX_CTX_DESC_TSO;
  962. cd_tso_len = skb->len - *hdr_len;
  963. cd_mss = skb_shinfo(skb)->gso_size;
  964. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  965. ((u64)cd_tso_len <<
  966. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  967. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  968. return 1;
  969. }
  970. /**
  971. * i40e_tx_enable_csum - Enable Tx checksum offloads
  972. * @skb: send buffer
  973. * @tx_flags: Tx flags currently set
  974. * @td_cmd: Tx descriptor command bits to set
  975. * @td_offset: Tx descriptor header offsets to set
  976. * @cd_tunneling: ptr to context desc bits
  977. **/
  978. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
  979. u32 *td_cmd, u32 *td_offset,
  980. struct i40e_ring *tx_ring,
  981. u32 *cd_tunneling)
  982. {
  983. struct ipv6hdr *this_ipv6_hdr;
  984. unsigned int this_tcp_hdrlen;
  985. struct iphdr *this_ip_hdr;
  986. u32 network_hdr_len;
  987. u8 l4_hdr = 0;
  988. if (skb->encapsulation) {
  989. network_hdr_len = skb_inner_network_header_len(skb);
  990. this_ip_hdr = inner_ip_hdr(skb);
  991. this_ipv6_hdr = inner_ipv6_hdr(skb);
  992. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  993. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  994. if (tx_flags & I40E_TX_FLAGS_TSO) {
  995. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  996. ip_hdr(skb)->check = 0;
  997. } else {
  998. *cd_tunneling |=
  999. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1000. }
  1001. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1002. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1003. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1004. ip_hdr(skb)->check = 0;
  1005. } else {
  1006. *cd_tunneling |=
  1007. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1008. }
  1009. }
  1010. /* Now set the ctx descriptor fields */
  1011. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1012. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1013. I40E_TXD_CTX_UDP_TUNNELING |
  1014. ((skb_inner_network_offset(skb) -
  1015. skb_transport_offset(skb)) >> 1) <<
  1016. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1017. } else {
  1018. network_hdr_len = skb_network_header_len(skb);
  1019. this_ip_hdr = ip_hdr(skb);
  1020. this_ipv6_hdr = ipv6_hdr(skb);
  1021. this_tcp_hdrlen = tcp_hdrlen(skb);
  1022. }
  1023. /* Enable IP checksum offloads */
  1024. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1025. l4_hdr = this_ip_hdr->protocol;
  1026. /* the stack computes the IP header already, the only time we
  1027. * need the hardware to recompute it is in the case of TSO.
  1028. */
  1029. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1030. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1031. this_ip_hdr->check = 0;
  1032. } else {
  1033. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1034. }
  1035. /* Now set the td_offset for IP header length */
  1036. *td_offset = (network_hdr_len >> 2) <<
  1037. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1038. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1039. l4_hdr = this_ipv6_hdr->nexthdr;
  1040. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1041. /* Now set the td_offset for IP header length */
  1042. *td_offset = (network_hdr_len >> 2) <<
  1043. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1044. }
  1045. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1046. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1047. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1048. /* Enable L4 checksum offloads */
  1049. switch (l4_hdr) {
  1050. case IPPROTO_TCP:
  1051. /* enable checksum offloads */
  1052. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1053. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1054. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1055. break;
  1056. case IPPROTO_SCTP:
  1057. /* enable SCTP checksum offload */
  1058. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1059. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1060. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1061. break;
  1062. case IPPROTO_UDP:
  1063. /* enable UDP checksum offload */
  1064. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1065. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1066. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1067. break;
  1068. default:
  1069. break;
  1070. }
  1071. }
  1072. /**
  1073. * i40e_create_tx_ctx Build the Tx context descriptor
  1074. * @tx_ring: ring to create the descriptor on
  1075. * @cd_type_cmd_tso_mss: Quad Word 1
  1076. * @cd_tunneling: Quad Word 0 - bits 0-31
  1077. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1078. **/
  1079. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1080. const u64 cd_type_cmd_tso_mss,
  1081. const u32 cd_tunneling, const u32 cd_l2tag2)
  1082. {
  1083. struct i40e_tx_context_desc *context_desc;
  1084. int i = tx_ring->next_to_use;
  1085. if (!cd_type_cmd_tso_mss && !cd_tunneling && !cd_l2tag2)
  1086. return;
  1087. /* grab the next descriptor */
  1088. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1089. i++;
  1090. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1091. /* cpu_to_le32 and assign to struct fields */
  1092. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1093. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1094. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1095. }
  1096. /**
  1097. * i40e_tx_map - Build the Tx descriptor
  1098. * @tx_ring: ring to send buffer on
  1099. * @skb: send buffer
  1100. * @first: first buffer info buffer to use
  1101. * @tx_flags: collected send information
  1102. * @hdr_len: size of the packet header
  1103. * @td_cmd: the command field in the descriptor
  1104. * @td_offset: offset for checksum or crc
  1105. **/
  1106. static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1107. struct i40e_tx_buffer *first, u32 tx_flags,
  1108. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1109. {
  1110. unsigned int data_len = skb->data_len;
  1111. unsigned int size = skb_headlen(skb);
  1112. struct skb_frag_struct *frag;
  1113. struct i40e_tx_buffer *tx_bi;
  1114. struct i40e_tx_desc *tx_desc;
  1115. u16 i = tx_ring->next_to_use;
  1116. u32 td_tag = 0;
  1117. dma_addr_t dma;
  1118. u16 gso_segs;
  1119. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1120. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1121. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1122. I40E_TX_FLAGS_VLAN_SHIFT;
  1123. }
  1124. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1125. gso_segs = skb_shinfo(skb)->gso_segs;
  1126. else
  1127. gso_segs = 1;
  1128. /* multiply data chunks by size of headers */
  1129. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1130. first->gso_segs = gso_segs;
  1131. first->skb = skb;
  1132. first->tx_flags = tx_flags;
  1133. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1134. tx_desc = I40E_TX_DESC(tx_ring, i);
  1135. tx_bi = first;
  1136. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1137. if (dma_mapping_error(tx_ring->dev, dma))
  1138. goto dma_error;
  1139. /* record length, and DMA address */
  1140. dma_unmap_len_set(tx_bi, len, size);
  1141. dma_unmap_addr_set(tx_bi, dma, dma);
  1142. tx_desc->buffer_addr = cpu_to_le64(dma);
  1143. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1144. tx_desc->cmd_type_offset_bsz =
  1145. build_ctob(td_cmd, td_offset,
  1146. I40E_MAX_DATA_PER_TXD, td_tag);
  1147. tx_desc++;
  1148. i++;
  1149. if (i == tx_ring->count) {
  1150. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1151. i = 0;
  1152. }
  1153. dma += I40E_MAX_DATA_PER_TXD;
  1154. size -= I40E_MAX_DATA_PER_TXD;
  1155. tx_desc->buffer_addr = cpu_to_le64(dma);
  1156. }
  1157. if (likely(!data_len))
  1158. break;
  1159. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1160. size, td_tag);
  1161. tx_desc++;
  1162. i++;
  1163. if (i == tx_ring->count) {
  1164. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1165. i = 0;
  1166. }
  1167. size = skb_frag_size(frag);
  1168. data_len -= size;
  1169. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1170. DMA_TO_DEVICE);
  1171. tx_bi = &tx_ring->tx_bi[i];
  1172. }
  1173. tx_desc->cmd_type_offset_bsz =
  1174. build_ctob(td_cmd, td_offset, size, td_tag) |
  1175. cpu_to_le64((u64)I40E_TXD_CMD << I40E_TXD_QW1_CMD_SHIFT);
  1176. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  1177. tx_ring->queue_index),
  1178. first->bytecount);
  1179. /* set the timestamp */
  1180. first->time_stamp = jiffies;
  1181. /* Force memory writes to complete before letting h/w
  1182. * know there are new descriptors to fetch. (Only
  1183. * applicable for weak-ordered memory model archs,
  1184. * such as IA-64).
  1185. */
  1186. wmb();
  1187. /* set next_to_watch value indicating a packet is present */
  1188. first->next_to_watch = tx_desc;
  1189. i++;
  1190. if (i == tx_ring->count)
  1191. i = 0;
  1192. tx_ring->next_to_use = i;
  1193. /* notify HW of packet */
  1194. writel(i, tx_ring->tail);
  1195. return;
  1196. dma_error:
  1197. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1198. /* clear dma mappings for failed tx_bi map */
  1199. for (;;) {
  1200. tx_bi = &tx_ring->tx_bi[i];
  1201. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1202. if (tx_bi == first)
  1203. break;
  1204. if (i == 0)
  1205. i = tx_ring->count;
  1206. i--;
  1207. }
  1208. tx_ring->next_to_use = i;
  1209. }
  1210. /**
  1211. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  1212. * @tx_ring: the ring to be checked
  1213. * @size: the size buffer we want to assure is available
  1214. *
  1215. * Returns -EBUSY if a stop is needed, else 0
  1216. **/
  1217. static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1218. {
  1219. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1220. /* Memory barrier before checking head and tail */
  1221. smp_mb();
  1222. /* Check again in a case another CPU has just made room available. */
  1223. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1224. return -EBUSY;
  1225. /* A reprieve! - use start_queue because it doesn't call schedule */
  1226. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1227. ++tx_ring->tx_stats.restart_queue;
  1228. return 0;
  1229. }
  1230. /**
  1231. * i40e_maybe_stop_tx - 1st level check for tx stop conditions
  1232. * @tx_ring: the ring to be checked
  1233. * @size: the size buffer we want to assure is available
  1234. *
  1235. * Returns 0 if stop is not needed
  1236. **/
  1237. static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1238. {
  1239. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1240. return 0;
  1241. return __i40e_maybe_stop_tx(tx_ring, size);
  1242. }
  1243. /**
  1244. * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
  1245. * @skb: send buffer
  1246. * @tx_ring: ring to send buffer on
  1247. *
  1248. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  1249. * there is not enough descriptors available in this ring since we need at least
  1250. * one descriptor.
  1251. **/
  1252. static int i40e_xmit_descriptor_count(struct sk_buff *skb,
  1253. struct i40e_ring *tx_ring)
  1254. {
  1255. #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
  1256. unsigned int f;
  1257. #endif
  1258. int count = 0;
  1259. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1260. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1261. * + 2 desc gap to keep tail from touching head,
  1262. * + 1 desc for context descriptor,
  1263. * otherwise try next time
  1264. */
  1265. #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
  1266. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1267. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  1268. #else
  1269. count += skb_shinfo(skb)->nr_frags;
  1270. #endif
  1271. count += TXD_USE_COUNT(skb_headlen(skb));
  1272. if (i40e_maybe_stop_tx(tx_ring, count + 3)) {
  1273. tx_ring->tx_stats.tx_busy++;
  1274. return 0;
  1275. }
  1276. return count;
  1277. }
  1278. /**
  1279. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1280. * @skb: send buffer
  1281. * @tx_ring: ring to send buffer on
  1282. *
  1283. * Returns NETDEV_TX_OK if sent, else an error code
  1284. **/
  1285. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1286. struct i40e_ring *tx_ring)
  1287. {
  1288. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1289. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1290. struct i40e_tx_buffer *first;
  1291. u32 td_offset = 0;
  1292. u32 tx_flags = 0;
  1293. __be16 protocol;
  1294. u32 td_cmd = 0;
  1295. u8 hdr_len = 0;
  1296. int tso;
  1297. if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
  1298. return NETDEV_TX_BUSY;
  1299. /* prepare the xmit flags */
  1300. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1301. goto out_drop;
  1302. /* obtain protocol of skb */
  1303. protocol = skb->protocol;
  1304. /* record the location of the first descriptor for this packet */
  1305. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1306. /* setup IPv4/IPv6 offloads */
  1307. if (protocol == htons(ETH_P_IP))
  1308. tx_flags |= I40E_TX_FLAGS_IPV4;
  1309. else if (protocol == htons(ETH_P_IPV6))
  1310. tx_flags |= I40E_TX_FLAGS_IPV6;
  1311. tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
  1312. &cd_type_cmd_tso_mss, &cd_tunneling);
  1313. if (tso < 0)
  1314. goto out_drop;
  1315. else if (tso)
  1316. tx_flags |= I40E_TX_FLAGS_TSO;
  1317. skb_tx_timestamp(skb);
  1318. /* always enable CRC insertion offload */
  1319. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1320. /* Always offload the checksum, since it's in the data descriptor */
  1321. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1322. tx_flags |= I40E_TX_FLAGS_CSUM;
  1323. i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
  1324. tx_ring, &cd_tunneling);
  1325. }
  1326. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1327. cd_tunneling, cd_l2tag2);
  1328. i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1329. td_cmd, td_offset);
  1330. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1331. return NETDEV_TX_OK;
  1332. out_drop:
  1333. dev_kfree_skb_any(skb);
  1334. return NETDEV_TX_OK;
  1335. }
  1336. /**
  1337. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1338. * @skb: send buffer
  1339. * @netdev: network interface device structure
  1340. *
  1341. * Returns NETDEV_TX_OK if sent, else an error code
  1342. **/
  1343. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1344. {
  1345. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1346. struct i40e_ring *tx_ring = adapter->tx_rings[skb->queue_mapping];
  1347. /* hardware can't handle really short frames, hardware padding works
  1348. * beyond this point
  1349. */
  1350. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1351. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1352. return NETDEV_TX_OK;
  1353. skb->len = I40E_MIN_TX_LEN;
  1354. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1355. }
  1356. return i40e_xmit_frame_ring(skb, tx_ring);
  1357. }