i40e_txrx.c 47 KB

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