i40e_txrx.c 45 KB

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