i40e_txrx.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include <net/busy_poll.h>
  28. #include "i40evf.h"
  29. #include "i40e_trace.h"
  30. #include "i40e_prototype.h"
  31. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  32. u32 td_tag)
  33. {
  34. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  35. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  36. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  37. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  38. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  39. }
  40. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  41. /**
  42. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  43. * @ring: the ring that owns the buffer
  44. * @tx_buffer: the buffer to free
  45. **/
  46. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  47. struct i40e_tx_buffer *tx_buffer)
  48. {
  49. if (tx_buffer->skb) {
  50. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  51. kfree(tx_buffer->raw_buf);
  52. else
  53. dev_kfree_skb_any(tx_buffer->skb);
  54. if (dma_unmap_len(tx_buffer, len))
  55. dma_unmap_single(ring->dev,
  56. dma_unmap_addr(tx_buffer, dma),
  57. dma_unmap_len(tx_buffer, len),
  58. DMA_TO_DEVICE);
  59. } else if (dma_unmap_len(tx_buffer, len)) {
  60. dma_unmap_page(ring->dev,
  61. dma_unmap_addr(tx_buffer, dma),
  62. dma_unmap_len(tx_buffer, len),
  63. DMA_TO_DEVICE);
  64. }
  65. tx_buffer->next_to_watch = NULL;
  66. tx_buffer->skb = NULL;
  67. dma_unmap_len_set(tx_buffer, len, 0);
  68. /* tx_buffer must be completely set up in the transmit path */
  69. }
  70. /**
  71. * i40evf_clean_tx_ring - Free any empty Tx buffers
  72. * @tx_ring: ring to be cleaned
  73. **/
  74. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  75. {
  76. unsigned long bi_size;
  77. u16 i;
  78. /* ring already cleared, nothing to do */
  79. if (!tx_ring->tx_bi)
  80. return;
  81. /* Free all the Tx ring sk_buffs */
  82. for (i = 0; i < tx_ring->count; i++)
  83. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  84. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  85. memset(tx_ring->tx_bi, 0, bi_size);
  86. /* Zero out the descriptor ring */
  87. memset(tx_ring->desc, 0, tx_ring->size);
  88. tx_ring->next_to_use = 0;
  89. tx_ring->next_to_clean = 0;
  90. if (!tx_ring->netdev)
  91. return;
  92. /* cleanup Tx queue statistics */
  93. netdev_tx_reset_queue(txring_txq(tx_ring));
  94. }
  95. /**
  96. * i40evf_free_tx_resources - Free Tx resources per queue
  97. * @tx_ring: Tx descriptor ring for a specific queue
  98. *
  99. * Free all transmit software resources
  100. **/
  101. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  102. {
  103. i40evf_clean_tx_ring(tx_ring);
  104. kfree(tx_ring->tx_bi);
  105. tx_ring->tx_bi = NULL;
  106. if (tx_ring->desc) {
  107. dma_free_coherent(tx_ring->dev, tx_ring->size,
  108. tx_ring->desc, tx_ring->dma);
  109. tx_ring->desc = NULL;
  110. }
  111. }
  112. /**
  113. * i40evf_get_tx_pending - how many Tx descriptors not processed
  114. * @tx_ring: the ring of descriptors
  115. * @in_sw: is tx_pending being checked in SW or HW
  116. *
  117. * Since there is no access to the ring head register
  118. * in XL710, we need to use our local copies
  119. **/
  120. u32 i40evf_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  121. {
  122. u32 head, tail;
  123. head = ring->next_to_clean;
  124. tail = readl(ring->tail);
  125. if (head != tail)
  126. return (head < tail) ?
  127. tail - head : (tail + ring->count - head);
  128. return 0;
  129. }
  130. #define WB_STRIDE 4
  131. /**
  132. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  133. * @vsi: the VSI we care about
  134. * @tx_ring: Tx ring to clean
  135. * @napi_budget: Used to determine if we are in netpoll
  136. *
  137. * Returns true if there's any budget left (e.g. the clean is finished)
  138. **/
  139. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  140. struct i40e_ring *tx_ring, int napi_budget)
  141. {
  142. u16 i = tx_ring->next_to_clean;
  143. struct i40e_tx_buffer *tx_buf;
  144. struct i40e_tx_desc *tx_desc;
  145. unsigned int total_bytes = 0, total_packets = 0;
  146. unsigned int budget = vsi->work_limit;
  147. tx_buf = &tx_ring->tx_bi[i];
  148. tx_desc = I40E_TX_DESC(tx_ring, i);
  149. i -= tx_ring->count;
  150. do {
  151. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  152. /* if next_to_watch is not set then there is no work pending */
  153. if (!eop_desc)
  154. break;
  155. /* prevent any other reads prior to eop_desc */
  156. read_barrier_depends();
  157. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  158. /* if the descriptor isn't done, no work yet to do */
  159. if (!(eop_desc->cmd_type_offset_bsz &
  160. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  161. break;
  162. /* clear next_to_watch to prevent false hangs */
  163. tx_buf->next_to_watch = NULL;
  164. /* update the statistics for this packet */
  165. total_bytes += tx_buf->bytecount;
  166. total_packets += tx_buf->gso_segs;
  167. /* free the skb */
  168. napi_consume_skb(tx_buf->skb, napi_budget);
  169. /* unmap skb header data */
  170. dma_unmap_single(tx_ring->dev,
  171. dma_unmap_addr(tx_buf, dma),
  172. dma_unmap_len(tx_buf, len),
  173. DMA_TO_DEVICE);
  174. /* clear tx_buffer data */
  175. tx_buf->skb = NULL;
  176. dma_unmap_len_set(tx_buf, len, 0);
  177. /* unmap remaining buffers */
  178. while (tx_desc != eop_desc) {
  179. i40e_trace(clean_tx_irq_unmap,
  180. tx_ring, tx_desc, tx_buf);
  181. tx_buf++;
  182. tx_desc++;
  183. i++;
  184. if (unlikely(!i)) {
  185. i -= tx_ring->count;
  186. tx_buf = tx_ring->tx_bi;
  187. tx_desc = I40E_TX_DESC(tx_ring, 0);
  188. }
  189. /* unmap any remaining paged data */
  190. if (dma_unmap_len(tx_buf, len)) {
  191. dma_unmap_page(tx_ring->dev,
  192. dma_unmap_addr(tx_buf, dma),
  193. dma_unmap_len(tx_buf, len),
  194. DMA_TO_DEVICE);
  195. dma_unmap_len_set(tx_buf, len, 0);
  196. }
  197. }
  198. /* move us one more past the eop_desc for start of next pkt */
  199. tx_buf++;
  200. tx_desc++;
  201. i++;
  202. if (unlikely(!i)) {
  203. i -= tx_ring->count;
  204. tx_buf = tx_ring->tx_bi;
  205. tx_desc = I40E_TX_DESC(tx_ring, 0);
  206. }
  207. prefetch(tx_desc);
  208. /* update budget accounting */
  209. budget--;
  210. } while (likely(budget));
  211. i += tx_ring->count;
  212. tx_ring->next_to_clean = i;
  213. u64_stats_update_begin(&tx_ring->syncp);
  214. tx_ring->stats.bytes += total_bytes;
  215. tx_ring->stats.packets += total_packets;
  216. u64_stats_update_end(&tx_ring->syncp);
  217. tx_ring->q_vector->tx.total_bytes += total_bytes;
  218. tx_ring->q_vector->tx.total_packets += total_packets;
  219. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  220. /* check to see if there are < 4 descriptors
  221. * waiting to be written back, then kick the hardware to force
  222. * them to be written back in case we stay in NAPI.
  223. * In this mode on X722 we do not enable Interrupt.
  224. */
  225. unsigned int j = i40evf_get_tx_pending(tx_ring, false);
  226. if (budget &&
  227. ((j / WB_STRIDE) == 0) && (j > 0) &&
  228. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  229. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  230. tx_ring->arm_wb = true;
  231. }
  232. /* notify netdev of completed buffers */
  233. netdev_tx_completed_queue(txring_txq(tx_ring),
  234. total_packets, total_bytes);
  235. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  236. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  237. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  238. /* Make sure that anybody stopping the queue after this
  239. * sees the new next_to_clean.
  240. */
  241. smp_mb();
  242. if (__netif_subqueue_stopped(tx_ring->netdev,
  243. tx_ring->queue_index) &&
  244. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  245. netif_wake_subqueue(tx_ring->netdev,
  246. tx_ring->queue_index);
  247. ++tx_ring->tx_stats.restart_queue;
  248. }
  249. }
  250. return !!budget;
  251. }
  252. /**
  253. * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  254. * @vsi: the VSI we care about
  255. * @q_vector: the vector on which to enable writeback
  256. *
  257. **/
  258. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  259. struct i40e_q_vector *q_vector)
  260. {
  261. u16 flags = q_vector->tx.ring[0].flags;
  262. u32 val;
  263. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  264. return;
  265. if (q_vector->arm_wb_state)
  266. return;
  267. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
  268. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
  269. wr32(&vsi->back->hw,
  270. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  271. vsi->base_vector - 1), val);
  272. q_vector->arm_wb_state = true;
  273. }
  274. /**
  275. * i40evf_force_wb - Issue SW Interrupt so HW does a wb
  276. * @vsi: the VSI we care about
  277. * @q_vector: the vector on which to force writeback
  278. *
  279. **/
  280. void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  281. {
  282. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  283. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  284. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  285. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
  286. /* allow 00 to be written to the index */;
  287. wr32(&vsi->back->hw,
  288. I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
  289. val);
  290. }
  291. /**
  292. * i40e_set_new_dynamic_itr - Find new ITR level
  293. * @rc: structure containing ring performance data
  294. *
  295. * Returns true if ITR changed, false if not
  296. *
  297. * Stores a new ITR value based on packets and byte counts during
  298. * the last interrupt. The advantage of per interrupt computation
  299. * is faster updates and more accurate ITR for the current traffic
  300. * pattern. Constants in this function were computed based on
  301. * theoretical maximum wire speed and thresholds were set based on
  302. * testing data as well as attempting to minimize response time
  303. * while increasing bulk throughput.
  304. **/
  305. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  306. {
  307. enum i40e_latency_range new_latency_range = rc->latency_range;
  308. u32 new_itr = rc->itr;
  309. int bytes_per_int;
  310. unsigned int usecs, estimated_usecs;
  311. if (rc->total_packets == 0 || !rc->itr)
  312. return false;
  313. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  314. bytes_per_int = rc->total_bytes / usecs;
  315. /* The calculations in this algorithm depend on interrupts actually
  316. * firing at the ITR rate. This may not happen if the packet rate is
  317. * really low, or if we've been napi polling. Check to make sure
  318. * that's not the case before we continue.
  319. */
  320. estimated_usecs = jiffies_to_usecs(jiffies - rc->last_itr_update);
  321. if (estimated_usecs > usecs) {
  322. new_latency_range = I40E_LOW_LATENCY;
  323. goto reset_latency;
  324. }
  325. /* simple throttlerate management
  326. * 0-10MB/s lowest (50000 ints/s)
  327. * 10-20MB/s low (20000 ints/s)
  328. * 20-1249MB/s bulk (18000 ints/s)
  329. *
  330. * The math works out because the divisor is in 10^(-6) which
  331. * turns the bytes/us input value into MB/s values, but
  332. * make sure to use usecs, as the register values written
  333. * are in 2 usec increments in the ITR registers, and make sure
  334. * to use the smoothed values that the countdown timer gives us.
  335. */
  336. switch (new_latency_range) {
  337. case I40E_LOWEST_LATENCY:
  338. if (bytes_per_int > 10)
  339. new_latency_range = I40E_LOW_LATENCY;
  340. break;
  341. case I40E_LOW_LATENCY:
  342. if (bytes_per_int > 20)
  343. new_latency_range = I40E_BULK_LATENCY;
  344. else if (bytes_per_int <= 10)
  345. new_latency_range = I40E_LOWEST_LATENCY;
  346. break;
  347. case I40E_BULK_LATENCY:
  348. default:
  349. if (bytes_per_int <= 20)
  350. new_latency_range = I40E_LOW_LATENCY;
  351. break;
  352. }
  353. reset_latency:
  354. rc->latency_range = new_latency_range;
  355. switch (new_latency_range) {
  356. case I40E_LOWEST_LATENCY:
  357. new_itr = I40E_ITR_50K;
  358. break;
  359. case I40E_LOW_LATENCY:
  360. new_itr = I40E_ITR_20K;
  361. break;
  362. case I40E_BULK_LATENCY:
  363. new_itr = I40E_ITR_18K;
  364. break;
  365. default:
  366. break;
  367. }
  368. rc->total_bytes = 0;
  369. rc->total_packets = 0;
  370. rc->last_itr_update = jiffies;
  371. if (new_itr != rc->itr) {
  372. rc->itr = new_itr;
  373. return true;
  374. }
  375. return false;
  376. }
  377. /**
  378. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  379. * @tx_ring: the tx ring to set up
  380. *
  381. * Return 0 on success, negative on error
  382. **/
  383. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  384. {
  385. struct device *dev = tx_ring->dev;
  386. int bi_size;
  387. if (!dev)
  388. return -ENOMEM;
  389. /* warn if we are about to overwrite the pointer */
  390. WARN_ON(tx_ring->tx_bi);
  391. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  392. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  393. if (!tx_ring->tx_bi)
  394. goto err;
  395. /* round up to nearest 4K */
  396. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  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. unsigned long bi_size;
  420. u16 i;
  421. /* ring already cleared, nothing to do */
  422. if (!rx_ring->rx_bi)
  423. return;
  424. if (rx_ring->skb) {
  425. dev_kfree_skb(rx_ring->skb);
  426. rx_ring->skb = NULL;
  427. }
  428. /* Free all the Rx ring sk_buffs */
  429. for (i = 0; i < rx_ring->count; i++) {
  430. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  431. if (!rx_bi->page)
  432. continue;
  433. /* Invalidate cache lines that may have been written to by
  434. * device so that we avoid corrupting memory.
  435. */
  436. dma_sync_single_range_for_cpu(rx_ring->dev,
  437. rx_bi->dma,
  438. rx_bi->page_offset,
  439. rx_ring->rx_buf_len,
  440. DMA_FROM_DEVICE);
  441. /* free resources associated with mapping */
  442. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  443. i40e_rx_pg_size(rx_ring),
  444. DMA_FROM_DEVICE,
  445. I40E_RX_DMA_ATTR);
  446. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  447. rx_bi->page = NULL;
  448. rx_bi->page_offset = 0;
  449. }
  450. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  451. memset(rx_ring->rx_bi, 0, bi_size);
  452. /* Zero out the descriptor ring */
  453. memset(rx_ring->desc, 0, rx_ring->size);
  454. rx_ring->next_to_alloc = 0;
  455. rx_ring->next_to_clean = 0;
  456. rx_ring->next_to_use = 0;
  457. }
  458. /**
  459. * i40evf_free_rx_resources - Free Rx resources
  460. * @rx_ring: ring to clean the resources from
  461. *
  462. * Free all receive software resources
  463. **/
  464. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  465. {
  466. i40evf_clean_rx_ring(rx_ring);
  467. kfree(rx_ring->rx_bi);
  468. rx_ring->rx_bi = NULL;
  469. if (rx_ring->desc) {
  470. dma_free_coherent(rx_ring->dev, rx_ring->size,
  471. rx_ring->desc, rx_ring->dma);
  472. rx_ring->desc = NULL;
  473. }
  474. }
  475. /**
  476. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  477. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  478. *
  479. * Returns 0 on success, negative on failure
  480. **/
  481. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  482. {
  483. struct device *dev = rx_ring->dev;
  484. int bi_size;
  485. /* warn if we are about to overwrite the pointer */
  486. WARN_ON(rx_ring->rx_bi);
  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. u64_stats_init(&rx_ring->syncp);
  492. /* Round up to nearest 4K */
  493. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  494. rx_ring->size = ALIGN(rx_ring->size, 4096);
  495. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  496. &rx_ring->dma, GFP_KERNEL);
  497. if (!rx_ring->desc) {
  498. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  499. rx_ring->size);
  500. goto err;
  501. }
  502. rx_ring->next_to_alloc = 0;
  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. /* update next to alloc since we have filled the ring */
  520. rx_ring->next_to_alloc = val;
  521. /* Force memory writes to complete before letting h/w
  522. * know there are new descriptors to fetch. (Only
  523. * applicable for weak-ordered memory model archs,
  524. * such as IA-64).
  525. */
  526. wmb();
  527. writel(val, rx_ring->tail);
  528. }
  529. /**
  530. * i40e_rx_offset - Return expected offset into page to access data
  531. * @rx_ring: Ring we are requesting offset of
  532. *
  533. * Returns the offset value for ring into the data buffer.
  534. */
  535. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  536. {
  537. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  538. }
  539. /**
  540. * i40e_alloc_mapped_page - recycle or make a new page
  541. * @rx_ring: ring to use
  542. * @bi: rx_buffer struct to modify
  543. *
  544. * Returns true if the page was successfully allocated or
  545. * reused.
  546. **/
  547. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  548. struct i40e_rx_buffer *bi)
  549. {
  550. struct page *page = bi->page;
  551. dma_addr_t dma;
  552. /* since we are recycling buffers we should seldom need to alloc */
  553. if (likely(page)) {
  554. rx_ring->rx_stats.page_reuse_count++;
  555. return true;
  556. }
  557. /* alloc new page for storage */
  558. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  559. if (unlikely(!page)) {
  560. rx_ring->rx_stats.alloc_page_failed++;
  561. return false;
  562. }
  563. /* map page for use */
  564. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  565. i40e_rx_pg_size(rx_ring),
  566. DMA_FROM_DEVICE,
  567. I40E_RX_DMA_ATTR);
  568. /* if mapping failed free memory back to system since
  569. * there isn't much point in holding memory we can't use
  570. */
  571. if (dma_mapping_error(rx_ring->dev, dma)) {
  572. __free_pages(page, i40e_rx_pg_order(rx_ring));
  573. rx_ring->rx_stats.alloc_page_failed++;
  574. return false;
  575. }
  576. bi->dma = dma;
  577. bi->page = page;
  578. bi->page_offset = i40e_rx_offset(rx_ring);
  579. /* initialize pagecnt_bias to 1 representing we fully own page */
  580. bi->pagecnt_bias = 1;
  581. return true;
  582. }
  583. /**
  584. * i40e_receive_skb - Send a completed packet up the stack
  585. * @rx_ring: rx ring in play
  586. * @skb: packet to send up
  587. * @vlan_tag: vlan tag for packet
  588. **/
  589. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  590. struct sk_buff *skb, u16 vlan_tag)
  591. {
  592. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  593. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  594. (vlan_tag & VLAN_VID_MASK))
  595. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  596. napi_gro_receive(&q_vector->napi, skb);
  597. }
  598. /**
  599. * i40evf_alloc_rx_buffers - Replace used receive buffers
  600. * @rx_ring: ring to place buffers on
  601. * @cleaned_count: number of buffers to replace
  602. *
  603. * Returns false if all allocations were successful, true if any fail
  604. **/
  605. bool i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  606. {
  607. u16 ntu = rx_ring->next_to_use;
  608. union i40e_rx_desc *rx_desc;
  609. struct i40e_rx_buffer *bi;
  610. /* do nothing if no valid netdev defined */
  611. if (!rx_ring->netdev || !cleaned_count)
  612. return false;
  613. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  614. bi = &rx_ring->rx_bi[ntu];
  615. do {
  616. if (!i40e_alloc_mapped_page(rx_ring, bi))
  617. goto no_buffers;
  618. /* sync the buffer for use by the device */
  619. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  620. bi->page_offset,
  621. rx_ring->rx_buf_len,
  622. DMA_FROM_DEVICE);
  623. /* Refresh the desc even if buffer_addrs didn't change
  624. * because each write-back erases this info.
  625. */
  626. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  627. rx_desc++;
  628. bi++;
  629. ntu++;
  630. if (unlikely(ntu == rx_ring->count)) {
  631. rx_desc = I40E_RX_DESC(rx_ring, 0);
  632. bi = rx_ring->rx_bi;
  633. ntu = 0;
  634. }
  635. /* clear the status bits for the next_to_use descriptor */
  636. rx_desc->wb.qword1.status_error_len = 0;
  637. cleaned_count--;
  638. } while (cleaned_count);
  639. if (rx_ring->next_to_use != ntu)
  640. i40e_release_rx_desc(rx_ring, ntu);
  641. return false;
  642. no_buffers:
  643. if (rx_ring->next_to_use != ntu)
  644. i40e_release_rx_desc(rx_ring, ntu);
  645. /* make sure to come back via polling to try again after
  646. * allocation failure
  647. */
  648. return true;
  649. }
  650. /**
  651. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  652. * @vsi: the VSI we care about
  653. * @skb: skb currently being received and modified
  654. * @rx_desc: the receive descriptor
  655. **/
  656. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  657. struct sk_buff *skb,
  658. union i40e_rx_desc *rx_desc)
  659. {
  660. struct i40e_rx_ptype_decoded decoded;
  661. u32 rx_error, rx_status;
  662. bool ipv4, ipv6;
  663. u8 ptype;
  664. u64 qword;
  665. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  666. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  667. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  668. I40E_RXD_QW1_ERROR_SHIFT;
  669. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  670. I40E_RXD_QW1_STATUS_SHIFT;
  671. decoded = decode_rx_desc_ptype(ptype);
  672. skb->ip_summed = CHECKSUM_NONE;
  673. skb_checksum_none_assert(skb);
  674. /* Rx csum enabled and ip headers found? */
  675. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  676. return;
  677. /* did the hardware decode the packet and checksum? */
  678. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  679. return;
  680. /* both known and outer_ip must be set for the below code to work */
  681. if (!(decoded.known && decoded.outer_ip))
  682. return;
  683. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  684. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  685. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  686. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  687. if (ipv4 &&
  688. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  689. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  690. goto checksum_fail;
  691. /* likely incorrect csum if alternate IP extension headers found */
  692. if (ipv6 &&
  693. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  694. /* don't increment checksum err here, non-fatal err */
  695. return;
  696. /* there was some L4 error, count error and punt packet to the stack */
  697. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  698. goto checksum_fail;
  699. /* handle packets that were not able to be checksummed due
  700. * to arrival speed, in this case the stack can compute
  701. * the csum.
  702. */
  703. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  704. return;
  705. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  706. switch (decoded.inner_prot) {
  707. case I40E_RX_PTYPE_INNER_PROT_TCP:
  708. case I40E_RX_PTYPE_INNER_PROT_UDP:
  709. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  710. skb->ip_summed = CHECKSUM_UNNECESSARY;
  711. /* fall though */
  712. default:
  713. break;
  714. }
  715. return;
  716. checksum_fail:
  717. vsi->back->hw_csum_rx_error++;
  718. }
  719. /**
  720. * i40e_ptype_to_htype - get a hash type
  721. * @ptype: the ptype value from the descriptor
  722. *
  723. * Returns a hash type to be used by skb_set_hash
  724. **/
  725. static inline int i40e_ptype_to_htype(u8 ptype)
  726. {
  727. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  728. if (!decoded.known)
  729. return PKT_HASH_TYPE_NONE;
  730. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  731. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  732. return PKT_HASH_TYPE_L4;
  733. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  734. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  735. return PKT_HASH_TYPE_L3;
  736. else
  737. return PKT_HASH_TYPE_L2;
  738. }
  739. /**
  740. * i40e_rx_hash - set the hash value in the skb
  741. * @ring: descriptor ring
  742. * @rx_desc: specific descriptor
  743. **/
  744. static inline void i40e_rx_hash(struct i40e_ring *ring,
  745. union i40e_rx_desc *rx_desc,
  746. struct sk_buff *skb,
  747. u8 rx_ptype)
  748. {
  749. u32 hash;
  750. const __le64 rss_mask =
  751. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  752. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  753. if (ring->netdev->features & NETIF_F_RXHASH)
  754. return;
  755. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  756. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  757. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  758. }
  759. }
  760. /**
  761. * i40evf_process_skb_fields - Populate skb header fields from Rx descriptor
  762. * @rx_ring: rx descriptor ring packet is being transacted on
  763. * @rx_desc: pointer to the EOP Rx descriptor
  764. * @skb: pointer to current skb being populated
  765. * @rx_ptype: the packet type decoded by hardware
  766. *
  767. * This function checks the ring, descriptor, and packet information in
  768. * order to populate the hash, checksum, VLAN, protocol, and
  769. * other fields within the skb.
  770. **/
  771. static inline
  772. void i40evf_process_skb_fields(struct i40e_ring *rx_ring,
  773. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  774. u8 rx_ptype)
  775. {
  776. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  777. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  778. skb_record_rx_queue(skb, rx_ring->queue_index);
  779. /* modifies the skb - consumes the enet header */
  780. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  781. }
  782. /**
  783. * i40e_cleanup_headers - Correct empty headers
  784. * @rx_ring: rx descriptor ring packet is being transacted on
  785. * @skb: pointer to current skb being fixed
  786. *
  787. * Also address the case where we are pulling data in on pages only
  788. * and as such no data is present in the skb header.
  789. *
  790. * In addition if skb is not at least 60 bytes we need to pad it so that
  791. * it is large enough to qualify as a valid Ethernet frame.
  792. *
  793. * Returns true if an error was encountered and skb was freed.
  794. **/
  795. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
  796. {
  797. /* if eth_skb_pad returns an error the skb was freed */
  798. if (eth_skb_pad(skb))
  799. return true;
  800. return false;
  801. }
  802. /**
  803. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  804. * @rx_ring: rx descriptor ring to store buffers on
  805. * @old_buff: donor buffer to have page reused
  806. *
  807. * Synchronizes page for reuse by the adapter
  808. **/
  809. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  810. struct i40e_rx_buffer *old_buff)
  811. {
  812. struct i40e_rx_buffer *new_buff;
  813. u16 nta = rx_ring->next_to_alloc;
  814. new_buff = &rx_ring->rx_bi[nta];
  815. /* update, and store next to alloc */
  816. nta++;
  817. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  818. /* transfer page from old buffer to new buffer */
  819. new_buff->dma = old_buff->dma;
  820. new_buff->page = old_buff->page;
  821. new_buff->page_offset = old_buff->page_offset;
  822. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  823. }
  824. /**
  825. * i40e_page_is_reusable - check if any reuse is possible
  826. * @page: page struct to check
  827. *
  828. * A page is not reusable if it was allocated under low memory
  829. * conditions, or it's not in the same NUMA node as this CPU.
  830. */
  831. static inline bool i40e_page_is_reusable(struct page *page)
  832. {
  833. return (page_to_nid(page) == numa_mem_id()) &&
  834. !page_is_pfmemalloc(page);
  835. }
  836. /**
  837. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  838. * the adapter for another receive
  839. *
  840. * @rx_buffer: buffer containing the page
  841. *
  842. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  843. * an unused region in the page.
  844. *
  845. * For small pages, @truesize will be a constant value, half the size
  846. * of the memory at page. We'll attempt to alternate between high and
  847. * low halves of the page, with one half ready for use by the hardware
  848. * and the other half being consumed by the stack. We use the page
  849. * ref count to determine whether the stack has finished consuming the
  850. * portion of this page that was passed up with a previous packet. If
  851. * the page ref count is >1, we'll assume the "other" half page is
  852. * still busy, and this page cannot be reused.
  853. *
  854. * For larger pages, @truesize will be the actual space used by the
  855. * received packet (adjusted upward to an even multiple of the cache
  856. * line size). This will advance through the page by the amount
  857. * actually consumed by the received packets while there is still
  858. * space for a buffer. Each region of larger pages will be used at
  859. * most once, after which the page will not be reused.
  860. *
  861. * In either case, if the page is reusable its refcount is increased.
  862. **/
  863. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  864. {
  865. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  866. struct page *page = rx_buffer->page;
  867. /* Is any reuse possible? */
  868. if (unlikely(!i40e_page_is_reusable(page)))
  869. return false;
  870. #if (PAGE_SIZE < 8192)
  871. /* if we are only owner of page we can reuse it */
  872. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  873. return false;
  874. #else
  875. #define I40E_LAST_OFFSET \
  876. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  877. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  878. return false;
  879. #endif
  880. /* If we have drained the page fragment pool we need to update
  881. * the pagecnt_bias and page count so that we fully restock the
  882. * number of references the driver holds.
  883. */
  884. if (unlikely(!pagecnt_bias)) {
  885. page_ref_add(page, USHRT_MAX);
  886. rx_buffer->pagecnt_bias = USHRT_MAX;
  887. }
  888. return true;
  889. }
  890. /**
  891. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  892. * @rx_ring: rx descriptor ring to transact packets on
  893. * @rx_buffer: buffer containing page to add
  894. * @skb: sk_buff to place the data into
  895. * @size: packet length from rx_desc
  896. *
  897. * This function will add the data contained in rx_buffer->page to the skb.
  898. * It will just attach the page as a frag to the skb.
  899. *
  900. * The function will then update the page offset.
  901. **/
  902. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  903. struct i40e_rx_buffer *rx_buffer,
  904. struct sk_buff *skb,
  905. unsigned int size)
  906. {
  907. #if (PAGE_SIZE < 8192)
  908. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  909. #else
  910. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  911. #endif
  912. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  913. rx_buffer->page_offset, size, truesize);
  914. /* page is being used so we must update the page offset */
  915. #if (PAGE_SIZE < 8192)
  916. rx_buffer->page_offset ^= truesize;
  917. #else
  918. rx_buffer->page_offset += truesize;
  919. #endif
  920. }
  921. /**
  922. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  923. * @rx_ring: rx descriptor ring to transact packets on
  924. * @size: size of buffer to add to skb
  925. *
  926. * This function will pull an Rx buffer from the ring and synchronize it
  927. * for use by the CPU.
  928. */
  929. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  930. const unsigned int size)
  931. {
  932. struct i40e_rx_buffer *rx_buffer;
  933. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  934. prefetchw(rx_buffer->page);
  935. /* we are reusing so sync this buffer for CPU use */
  936. dma_sync_single_range_for_cpu(rx_ring->dev,
  937. rx_buffer->dma,
  938. rx_buffer->page_offset,
  939. size,
  940. DMA_FROM_DEVICE);
  941. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  942. rx_buffer->pagecnt_bias--;
  943. return rx_buffer;
  944. }
  945. /**
  946. * i40e_construct_skb - Allocate skb and populate it
  947. * @rx_ring: rx descriptor ring to transact packets on
  948. * @rx_buffer: rx buffer to pull data from
  949. * @size: size of buffer to add to skb
  950. *
  951. * This function allocates an skb. It then populates it with the page
  952. * data from the current receive descriptor, taking care to set up the
  953. * skb correctly.
  954. */
  955. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  956. struct i40e_rx_buffer *rx_buffer,
  957. unsigned int size)
  958. {
  959. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  960. #if (PAGE_SIZE < 8192)
  961. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  962. #else
  963. unsigned int truesize = SKB_DATA_ALIGN(size);
  964. #endif
  965. unsigned int headlen;
  966. struct sk_buff *skb;
  967. /* prefetch first cache line of first page */
  968. prefetch(va);
  969. #if L1_CACHE_BYTES < 128
  970. prefetch(va + L1_CACHE_BYTES);
  971. #endif
  972. /* allocate a skb to store the frags */
  973. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  974. I40E_RX_HDR_SIZE,
  975. GFP_ATOMIC | __GFP_NOWARN);
  976. if (unlikely(!skb))
  977. return NULL;
  978. /* Determine available headroom for copy */
  979. headlen = size;
  980. if (headlen > I40E_RX_HDR_SIZE)
  981. headlen = eth_get_headlen(va, I40E_RX_HDR_SIZE);
  982. /* align pull length to size of long to optimize memcpy performance */
  983. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  984. /* update all of the pointers */
  985. size -= headlen;
  986. if (size) {
  987. skb_add_rx_frag(skb, 0, rx_buffer->page,
  988. rx_buffer->page_offset + headlen,
  989. size, truesize);
  990. /* buffer is used by skb, update page_offset */
  991. #if (PAGE_SIZE < 8192)
  992. rx_buffer->page_offset ^= truesize;
  993. #else
  994. rx_buffer->page_offset += truesize;
  995. #endif
  996. } else {
  997. /* buffer is unused, reset bias back to rx_buffer */
  998. rx_buffer->pagecnt_bias++;
  999. }
  1000. return skb;
  1001. }
  1002. /**
  1003. * i40e_build_skb - Build skb around an existing buffer
  1004. * @rx_ring: Rx descriptor ring to transact packets on
  1005. * @rx_buffer: Rx buffer to pull data from
  1006. * @size: size of buffer to add to skb
  1007. *
  1008. * This function builds an skb around an existing Rx buffer, taking care
  1009. * to set up the skb correctly and avoid any memcpy overhead.
  1010. */
  1011. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1012. struct i40e_rx_buffer *rx_buffer,
  1013. unsigned int size)
  1014. {
  1015. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1016. #if (PAGE_SIZE < 8192)
  1017. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1018. #else
  1019. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1020. SKB_DATA_ALIGN(I40E_SKB_PAD + size);
  1021. #endif
  1022. struct sk_buff *skb;
  1023. /* prefetch first cache line of first page */
  1024. prefetch(va);
  1025. #if L1_CACHE_BYTES < 128
  1026. prefetch(va + L1_CACHE_BYTES);
  1027. #endif
  1028. /* build an skb around the page buffer */
  1029. skb = build_skb(va - I40E_SKB_PAD, truesize);
  1030. if (unlikely(!skb))
  1031. return NULL;
  1032. /* update pointers within the skb to store the data */
  1033. skb_reserve(skb, I40E_SKB_PAD);
  1034. __skb_put(skb, size);
  1035. /* buffer is used by skb, update page_offset */
  1036. #if (PAGE_SIZE < 8192)
  1037. rx_buffer->page_offset ^= truesize;
  1038. #else
  1039. rx_buffer->page_offset += truesize;
  1040. #endif
  1041. return skb;
  1042. }
  1043. /**
  1044. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1045. * @rx_ring: rx descriptor ring to transact packets on
  1046. * @rx_buffer: rx buffer to pull data from
  1047. *
  1048. * This function will clean up the contents of the rx_buffer. It will
  1049. * either recycle the bufer or unmap it and free the associated resources.
  1050. */
  1051. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1052. struct i40e_rx_buffer *rx_buffer)
  1053. {
  1054. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1055. /* hand second half of page back to the ring */
  1056. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1057. rx_ring->rx_stats.page_reuse_count++;
  1058. } else {
  1059. /* we are not reusing the buffer so unmap it */
  1060. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1061. i40e_rx_pg_size(rx_ring),
  1062. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1063. __page_frag_cache_drain(rx_buffer->page,
  1064. rx_buffer->pagecnt_bias);
  1065. }
  1066. /* clear contents of buffer_info */
  1067. rx_buffer->page = NULL;
  1068. }
  1069. /**
  1070. * i40e_is_non_eop - process handling of non-EOP buffers
  1071. * @rx_ring: Rx ring being processed
  1072. * @rx_desc: Rx descriptor for current buffer
  1073. * @skb: Current socket buffer containing buffer in progress
  1074. *
  1075. * This function updates next to clean. If the buffer is an EOP buffer
  1076. * this function exits returning false, otherwise it will place the
  1077. * sk_buff in the next buffer to be chained and return true indicating
  1078. * that this is in fact a non-EOP buffer.
  1079. **/
  1080. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1081. union i40e_rx_desc *rx_desc,
  1082. struct sk_buff *skb)
  1083. {
  1084. u32 ntc = rx_ring->next_to_clean + 1;
  1085. /* fetch, update, and store next to clean */
  1086. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1087. rx_ring->next_to_clean = ntc;
  1088. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1089. /* if we are the last buffer then there is nothing else to do */
  1090. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1091. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1092. return false;
  1093. rx_ring->rx_stats.non_eop_descs++;
  1094. return true;
  1095. }
  1096. /**
  1097. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1098. * @rx_ring: rx descriptor ring to transact packets on
  1099. * @budget: Total limit on number of packets to process
  1100. *
  1101. * This function provides a "bounce buffer" approach to Rx interrupt
  1102. * processing. The advantage to this is that on systems that have
  1103. * expensive overhead for IOMMU access this provides a means of avoiding
  1104. * it by maintaining the mapping of the page to the system.
  1105. *
  1106. * Returns amount of work completed
  1107. **/
  1108. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1109. {
  1110. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1111. struct sk_buff *skb = rx_ring->skb;
  1112. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1113. bool failure = false;
  1114. while (likely(total_rx_packets < (unsigned int)budget)) {
  1115. struct i40e_rx_buffer *rx_buffer;
  1116. union i40e_rx_desc *rx_desc;
  1117. unsigned int size;
  1118. u16 vlan_tag;
  1119. u8 rx_ptype;
  1120. u64 qword;
  1121. /* return some buffers to hardware, one at a time is too slow */
  1122. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1123. failure = failure ||
  1124. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  1125. cleaned_count = 0;
  1126. }
  1127. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1128. /* status_error_len will always be zero for unused descriptors
  1129. * because it's cleared in cleanup, and overlaps with hdr_addr
  1130. * which is always zero because packet split isn't used, if the
  1131. * hardware wrote DD then the length will be non-zero
  1132. */
  1133. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1134. /* This memory barrier is needed to keep us from reading
  1135. * any other fields out of the rx_desc until we have
  1136. * verified the descriptor has been written back.
  1137. */
  1138. dma_rmb();
  1139. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1140. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1141. if (!size)
  1142. break;
  1143. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1144. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1145. /* retrieve a buffer from the ring */
  1146. if (skb)
  1147. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1148. else if (ring_uses_build_skb(rx_ring))
  1149. skb = i40e_build_skb(rx_ring, rx_buffer, size);
  1150. else
  1151. skb = i40e_construct_skb(rx_ring, rx_buffer, size);
  1152. /* exit if we failed to retrieve a buffer */
  1153. if (!skb) {
  1154. rx_ring->rx_stats.alloc_buff_failed++;
  1155. rx_buffer->pagecnt_bias++;
  1156. break;
  1157. }
  1158. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1159. cleaned_count++;
  1160. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1161. continue;
  1162. /* ERR_MASK will only have valid bits if EOP set, and
  1163. * what we are doing here is actually checking
  1164. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1165. * the error field
  1166. */
  1167. if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1168. dev_kfree_skb_any(skb);
  1169. skb = NULL;
  1170. continue;
  1171. }
  1172. if (i40e_cleanup_headers(rx_ring, skb)) {
  1173. skb = NULL;
  1174. continue;
  1175. }
  1176. /* probably a little skewed due to removing CRC */
  1177. total_rx_bytes += skb->len;
  1178. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1179. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1180. I40E_RXD_QW1_PTYPE_SHIFT;
  1181. /* populate checksum, VLAN, and protocol */
  1182. i40evf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1183. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1184. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1185. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1186. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1187. skb = NULL;
  1188. /* update budget accounting */
  1189. total_rx_packets++;
  1190. }
  1191. rx_ring->skb = skb;
  1192. u64_stats_update_begin(&rx_ring->syncp);
  1193. rx_ring->stats.packets += total_rx_packets;
  1194. rx_ring->stats.bytes += total_rx_bytes;
  1195. u64_stats_update_end(&rx_ring->syncp);
  1196. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1197. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1198. /* guarantee a trip back through this routine if there was a failure */
  1199. return failure ? budget : (int)total_rx_packets;
  1200. }
  1201. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1202. {
  1203. u32 val;
  1204. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1205. /* Don't clear PBA because that can cause lost interrupts that
  1206. * came in while we were cleaning/polling
  1207. */
  1208. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1209. (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
  1210. return val;
  1211. }
  1212. /* a small macro to shorten up some long lines */
  1213. #define INTREG I40E_VFINT_DYN_CTLN1
  1214. static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
  1215. {
  1216. struct i40evf_adapter *adapter = vsi->back;
  1217. return adapter->rx_rings[idx].rx_itr_setting;
  1218. }
  1219. static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
  1220. {
  1221. struct i40evf_adapter *adapter = vsi->back;
  1222. return adapter->tx_rings[idx].tx_itr_setting;
  1223. }
  1224. /**
  1225. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1226. * @vsi: the VSI we care about
  1227. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1228. *
  1229. **/
  1230. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1231. struct i40e_q_vector *q_vector)
  1232. {
  1233. struct i40e_hw *hw = &vsi->back->hw;
  1234. bool rx = false, tx = false;
  1235. u32 rxval, txval;
  1236. int vector;
  1237. int idx = q_vector->v_idx;
  1238. int rx_itr_setting, tx_itr_setting;
  1239. vector = (q_vector->v_idx + vsi->base_vector);
  1240. /* avoid dynamic calculation if in countdown mode OR if
  1241. * all dynamic is disabled
  1242. */
  1243. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1244. rx_itr_setting = get_rx_itr(vsi, idx);
  1245. tx_itr_setting = get_tx_itr(vsi, idx);
  1246. if (q_vector->itr_countdown > 0 ||
  1247. (!ITR_IS_DYNAMIC(rx_itr_setting) &&
  1248. !ITR_IS_DYNAMIC(tx_itr_setting))) {
  1249. goto enable_int;
  1250. }
  1251. if (ITR_IS_DYNAMIC(rx_itr_setting)) {
  1252. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1253. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1254. }
  1255. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1256. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1257. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1258. }
  1259. if (rx || tx) {
  1260. /* get the higher of the two ITR adjustments and
  1261. * use the same value for both ITR registers
  1262. * when in adaptive mode (Rx and/or Tx)
  1263. */
  1264. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1265. q_vector->tx.itr = q_vector->rx.itr = itr;
  1266. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1267. tx = true;
  1268. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1269. rx = true;
  1270. }
  1271. /* only need to enable the interrupt once, but need
  1272. * to possibly update both ITR values
  1273. */
  1274. if (rx) {
  1275. /* set the INTENA_MSK_MASK so that this first write
  1276. * won't actually enable the interrupt, instead just
  1277. * updating the ITR (it's bit 31 PF and VF)
  1278. */
  1279. rxval |= BIT(31);
  1280. /* don't check _DOWN because interrupt isn't being enabled */
  1281. wr32(hw, INTREG(vector - 1), rxval);
  1282. }
  1283. enable_int:
  1284. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1285. wr32(hw, INTREG(vector - 1), txval);
  1286. if (q_vector->itr_countdown)
  1287. q_vector->itr_countdown--;
  1288. else
  1289. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1290. }
  1291. /**
  1292. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1293. * @napi: napi struct with our devices info in it
  1294. * @budget: amount of work driver is allowed to do this pass, in packets
  1295. *
  1296. * This function will clean all queues associated with a q_vector.
  1297. *
  1298. * Returns the amount of work done
  1299. **/
  1300. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1301. {
  1302. struct i40e_q_vector *q_vector =
  1303. container_of(napi, struct i40e_q_vector, napi);
  1304. struct i40e_vsi *vsi = q_vector->vsi;
  1305. struct i40e_ring *ring;
  1306. bool clean_complete = true;
  1307. bool arm_wb = false;
  1308. int budget_per_ring;
  1309. int work_done = 0;
  1310. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  1311. napi_complete(napi);
  1312. return 0;
  1313. }
  1314. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1315. * budget and be more aggressive about cleaning up the Tx descriptors.
  1316. */
  1317. i40e_for_each_ring(ring, q_vector->tx) {
  1318. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  1319. clean_complete = false;
  1320. continue;
  1321. }
  1322. arm_wb |= ring->arm_wb;
  1323. ring->arm_wb = false;
  1324. }
  1325. /* Handle case where we are called by netpoll with a budget of 0 */
  1326. if (budget <= 0)
  1327. goto tx_only;
  1328. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1329. * allow the budget to go below 1 because that would exit polling early.
  1330. */
  1331. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1332. i40e_for_each_ring(ring, q_vector->rx) {
  1333. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  1334. work_done += cleaned;
  1335. /* if we clean as many as budgeted, we must not be done */
  1336. if (cleaned >= budget_per_ring)
  1337. clean_complete = false;
  1338. }
  1339. /* If work not completed, return budget and polling will return */
  1340. if (!clean_complete) {
  1341. int cpu_id = smp_processor_id();
  1342. /* It is possible that the interrupt affinity has changed but,
  1343. * if the cpu is pegged at 100%, polling will never exit while
  1344. * traffic continues and the interrupt will be stuck on this
  1345. * cpu. We check to make sure affinity is correct before we
  1346. * continue to poll, otherwise we must stop polling so the
  1347. * interrupt can move to the correct cpu.
  1348. */
  1349. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  1350. /* Tell napi that we are done polling */
  1351. napi_complete_done(napi, work_done);
  1352. /* Force an interrupt */
  1353. i40evf_force_wb(vsi, q_vector);
  1354. /* Return budget-1 so that polling stops */
  1355. return budget - 1;
  1356. }
  1357. tx_only:
  1358. if (arm_wb) {
  1359. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1360. i40e_enable_wb_on_itr(vsi, q_vector);
  1361. }
  1362. return budget;
  1363. }
  1364. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1365. q_vector->arm_wb_state = false;
  1366. /* Work is done so exit the polling mode and re-enable the interrupt */
  1367. napi_complete_done(napi, work_done);
  1368. i40e_update_enable_itr(vsi, q_vector);
  1369. return min(work_done, budget - 1);
  1370. }
  1371. /**
  1372. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1373. * @skb: send buffer
  1374. * @tx_ring: ring to send buffer on
  1375. * @flags: the tx flags to be set
  1376. *
  1377. * Checks the skb and set up correspondingly several generic transmit flags
  1378. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1379. *
  1380. * Returns error code indicate the frame should be dropped upon error and the
  1381. * otherwise returns 0 to indicate the flags has been set properly.
  1382. **/
  1383. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1384. struct i40e_ring *tx_ring,
  1385. u32 *flags)
  1386. {
  1387. __be16 protocol = skb->protocol;
  1388. u32 tx_flags = 0;
  1389. if (protocol == htons(ETH_P_8021Q) &&
  1390. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1391. /* When HW VLAN acceleration is turned off by the user the
  1392. * stack sets the protocol to 8021q so that the driver
  1393. * can take any steps required to support the SW only
  1394. * VLAN handling. In our case the driver doesn't need
  1395. * to take any further steps so just set the protocol
  1396. * to the encapsulated ethertype.
  1397. */
  1398. skb->protocol = vlan_get_protocol(skb);
  1399. goto out;
  1400. }
  1401. /* if we have a HW VLAN tag being added, default to the HW one */
  1402. if (skb_vlan_tag_present(skb)) {
  1403. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1404. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1405. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1406. } else if (protocol == htons(ETH_P_8021Q)) {
  1407. struct vlan_hdr *vhdr, _vhdr;
  1408. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1409. if (!vhdr)
  1410. return -EINVAL;
  1411. protocol = vhdr->h_vlan_encapsulated_proto;
  1412. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1413. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1414. }
  1415. out:
  1416. *flags = tx_flags;
  1417. return 0;
  1418. }
  1419. /**
  1420. * i40e_tso - set up the tso context descriptor
  1421. * @first: pointer to first Tx buffer for xmit
  1422. * @hdr_len: ptr to the size of the packet header
  1423. * @cd_type_cmd_tso_mss: Quad Word 1
  1424. *
  1425. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1426. **/
  1427. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  1428. u64 *cd_type_cmd_tso_mss)
  1429. {
  1430. struct sk_buff *skb = first->skb;
  1431. u64 cd_cmd, cd_tso_len, cd_mss;
  1432. union {
  1433. struct iphdr *v4;
  1434. struct ipv6hdr *v6;
  1435. unsigned char *hdr;
  1436. } ip;
  1437. union {
  1438. struct tcphdr *tcp;
  1439. struct udphdr *udp;
  1440. unsigned char *hdr;
  1441. } l4;
  1442. u32 paylen, l4_offset;
  1443. u16 gso_segs, gso_size;
  1444. int err;
  1445. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1446. return 0;
  1447. if (!skb_is_gso(skb))
  1448. return 0;
  1449. err = skb_cow_head(skb, 0);
  1450. if (err < 0)
  1451. return err;
  1452. ip.hdr = skb_network_header(skb);
  1453. l4.hdr = skb_transport_header(skb);
  1454. /* initialize outer IP header fields */
  1455. if (ip.v4->version == 4) {
  1456. ip.v4->tot_len = 0;
  1457. ip.v4->check = 0;
  1458. } else {
  1459. ip.v6->payload_len = 0;
  1460. }
  1461. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1462. SKB_GSO_GRE_CSUM |
  1463. SKB_GSO_IPXIP4 |
  1464. SKB_GSO_IPXIP6 |
  1465. SKB_GSO_UDP_TUNNEL |
  1466. SKB_GSO_UDP_TUNNEL_CSUM)) {
  1467. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1468. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  1469. l4.udp->len = 0;
  1470. /* determine offset of outer transport header */
  1471. l4_offset = l4.hdr - skb->data;
  1472. /* remove payload length from outer checksum */
  1473. paylen = skb->len - l4_offset;
  1474. csum_replace_by_diff(&l4.udp->check,
  1475. (__force __wsum)htonl(paylen));
  1476. }
  1477. /* reset pointers to inner headers */
  1478. ip.hdr = skb_inner_network_header(skb);
  1479. l4.hdr = skb_inner_transport_header(skb);
  1480. /* initialize inner IP header fields */
  1481. if (ip.v4->version == 4) {
  1482. ip.v4->tot_len = 0;
  1483. ip.v4->check = 0;
  1484. } else {
  1485. ip.v6->payload_len = 0;
  1486. }
  1487. }
  1488. /* determine offset of inner transport header */
  1489. l4_offset = l4.hdr - skb->data;
  1490. /* remove payload length from inner checksum */
  1491. paylen = skb->len - l4_offset;
  1492. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  1493. /* compute length of segmentation header */
  1494. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  1495. /* pull values out of skb_shinfo */
  1496. gso_size = skb_shinfo(skb)->gso_size;
  1497. gso_segs = skb_shinfo(skb)->gso_segs;
  1498. /* update GSO size and bytecount with header size */
  1499. first->gso_segs = gso_segs;
  1500. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  1501. /* find the field values */
  1502. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1503. cd_tso_len = skb->len - *hdr_len;
  1504. cd_mss = gso_size;
  1505. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1506. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1507. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1508. return 1;
  1509. }
  1510. /**
  1511. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1512. * @skb: send buffer
  1513. * @tx_flags: pointer to Tx flags currently set
  1514. * @td_cmd: Tx descriptor command bits to set
  1515. * @td_offset: Tx descriptor header offsets to set
  1516. * @tx_ring: Tx descriptor ring
  1517. * @cd_tunneling: ptr to context desc bits
  1518. **/
  1519. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1520. u32 *td_cmd, u32 *td_offset,
  1521. struct i40e_ring *tx_ring,
  1522. u32 *cd_tunneling)
  1523. {
  1524. union {
  1525. struct iphdr *v4;
  1526. struct ipv6hdr *v6;
  1527. unsigned char *hdr;
  1528. } ip;
  1529. union {
  1530. struct tcphdr *tcp;
  1531. struct udphdr *udp;
  1532. unsigned char *hdr;
  1533. } l4;
  1534. unsigned char *exthdr;
  1535. u32 offset, cmd = 0;
  1536. __be16 frag_off;
  1537. u8 l4_proto = 0;
  1538. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1539. return 0;
  1540. ip.hdr = skb_network_header(skb);
  1541. l4.hdr = skb_transport_header(skb);
  1542. /* compute outer L2 header size */
  1543. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1544. if (skb->encapsulation) {
  1545. u32 tunnel = 0;
  1546. /* define outer network header type */
  1547. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1548. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1549. I40E_TX_CTX_EXT_IP_IPV4 :
  1550. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1551. l4_proto = ip.v4->protocol;
  1552. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1553. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  1554. exthdr = ip.hdr + sizeof(*ip.v6);
  1555. l4_proto = ip.v6->nexthdr;
  1556. if (l4.hdr != exthdr)
  1557. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1558. &l4_proto, &frag_off);
  1559. }
  1560. /* define outer transport */
  1561. switch (l4_proto) {
  1562. case IPPROTO_UDP:
  1563. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  1564. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1565. break;
  1566. case IPPROTO_GRE:
  1567. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  1568. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1569. break;
  1570. case IPPROTO_IPIP:
  1571. case IPPROTO_IPV6:
  1572. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1573. l4.hdr = skb_inner_network_header(skb);
  1574. break;
  1575. default:
  1576. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1577. return -1;
  1578. skb_checksum_help(skb);
  1579. return 0;
  1580. }
  1581. /* compute outer L3 header size */
  1582. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  1583. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  1584. /* switch IP header pointer from outer to inner header */
  1585. ip.hdr = skb_inner_network_header(skb);
  1586. /* compute tunnel header size */
  1587. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  1588. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1589. /* indicate if we need to offload outer UDP header */
  1590. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  1591. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1592. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  1593. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1594. /* record tunnel offload values */
  1595. *cd_tunneling |= tunnel;
  1596. /* switch L4 header pointer from outer to inner */
  1597. l4.hdr = skb_inner_transport_header(skb);
  1598. l4_proto = 0;
  1599. /* reset type as we transition from outer to inner headers */
  1600. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  1601. if (ip.v4->version == 4)
  1602. *tx_flags |= I40E_TX_FLAGS_IPV4;
  1603. if (ip.v6->version == 6)
  1604. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1605. }
  1606. /* Enable IP checksum offloads */
  1607. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1608. l4_proto = ip.v4->protocol;
  1609. /* the stack computes the IP header already, the only time we
  1610. * need the hardware to recompute it is in the case of TSO.
  1611. */
  1612. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1613. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  1614. I40E_TX_DESC_CMD_IIPT_IPV4;
  1615. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1616. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1617. exthdr = ip.hdr + sizeof(*ip.v6);
  1618. l4_proto = ip.v6->nexthdr;
  1619. if (l4.hdr != exthdr)
  1620. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1621. &l4_proto, &frag_off);
  1622. }
  1623. /* compute inner L3 header size */
  1624. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1625. /* Enable L4 checksum offloads */
  1626. switch (l4_proto) {
  1627. case IPPROTO_TCP:
  1628. /* enable checksum offloads */
  1629. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1630. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1631. break;
  1632. case IPPROTO_SCTP:
  1633. /* enable SCTP checksum offload */
  1634. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1635. offset |= (sizeof(struct sctphdr) >> 2) <<
  1636. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1637. break;
  1638. case IPPROTO_UDP:
  1639. /* enable UDP checksum offload */
  1640. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1641. offset |= (sizeof(struct udphdr) >> 2) <<
  1642. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1643. break;
  1644. default:
  1645. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1646. return -1;
  1647. skb_checksum_help(skb);
  1648. return 0;
  1649. }
  1650. *td_cmd |= cmd;
  1651. *td_offset |= offset;
  1652. return 1;
  1653. }
  1654. /**
  1655. * i40e_create_tx_ctx Build the Tx context descriptor
  1656. * @tx_ring: ring to create the descriptor on
  1657. * @cd_type_cmd_tso_mss: Quad Word 1
  1658. * @cd_tunneling: Quad Word 0 - bits 0-31
  1659. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1660. **/
  1661. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1662. const u64 cd_type_cmd_tso_mss,
  1663. const u32 cd_tunneling, const u32 cd_l2tag2)
  1664. {
  1665. struct i40e_tx_context_desc *context_desc;
  1666. int i = tx_ring->next_to_use;
  1667. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1668. !cd_tunneling && !cd_l2tag2)
  1669. return;
  1670. /* grab the next descriptor */
  1671. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1672. i++;
  1673. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1674. /* cpu_to_le32 and assign to struct fields */
  1675. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1676. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1677. context_desc->rsvd = cpu_to_le16(0);
  1678. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1679. }
  1680. /**
  1681. * __i40evf_chk_linearize - Check if there are more than 8 buffers per packet
  1682. * @skb: send buffer
  1683. *
  1684. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  1685. * and so we need to figure out the cases where we need to linearize the skb.
  1686. *
  1687. * For TSO we need to count the TSO header and segment payload separately.
  1688. * As such we need to check cases where we have 7 fragments or more as we
  1689. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  1690. * the segment payload in the first descriptor, and another 7 for the
  1691. * fragments.
  1692. **/
  1693. bool __i40evf_chk_linearize(struct sk_buff *skb)
  1694. {
  1695. const struct skb_frag_struct *frag, *stale;
  1696. int nr_frags, sum;
  1697. /* no need to check if number of frags is less than 7 */
  1698. nr_frags = skb_shinfo(skb)->nr_frags;
  1699. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  1700. return false;
  1701. /* We need to walk through the list and validate that each group
  1702. * of 6 fragments totals at least gso_size.
  1703. */
  1704. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  1705. frag = &skb_shinfo(skb)->frags[0];
  1706. /* Initialize size to the negative value of gso_size minus 1. We
  1707. * use this as the worst case scenerio in which the frag ahead
  1708. * of us only provides one byte which is why we are limited to 6
  1709. * descriptors for a single transmit as the header and previous
  1710. * fragment are already consuming 2 descriptors.
  1711. */
  1712. sum = 1 - skb_shinfo(skb)->gso_size;
  1713. /* Add size of frags 0 through 4 to create our initial sum */
  1714. sum += skb_frag_size(frag++);
  1715. sum += skb_frag_size(frag++);
  1716. sum += skb_frag_size(frag++);
  1717. sum += skb_frag_size(frag++);
  1718. sum += skb_frag_size(frag++);
  1719. /* Walk through fragments adding latest fragment, testing it, and
  1720. * then removing stale fragments from the sum.
  1721. */
  1722. stale = &skb_shinfo(skb)->frags[0];
  1723. for (;;) {
  1724. sum += skb_frag_size(frag++);
  1725. /* if sum is negative we failed to make sufficient progress */
  1726. if (sum < 0)
  1727. return true;
  1728. if (!nr_frags--)
  1729. break;
  1730. sum -= skb_frag_size(stale++);
  1731. }
  1732. return false;
  1733. }
  1734. /**
  1735. * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
  1736. * @tx_ring: the ring to be checked
  1737. * @size: the size buffer we want to assure is available
  1738. *
  1739. * Returns -EBUSY if a stop is needed, else 0
  1740. **/
  1741. int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1742. {
  1743. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1744. /* Memory barrier before checking head and tail */
  1745. smp_mb();
  1746. /* Check again in a case another CPU has just made room available. */
  1747. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1748. return -EBUSY;
  1749. /* A reprieve! - use start_queue because it doesn't call schedule */
  1750. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1751. ++tx_ring->tx_stats.restart_queue;
  1752. return 0;
  1753. }
  1754. /**
  1755. * i40evf_tx_map - Build the Tx descriptor
  1756. * @tx_ring: ring to send buffer on
  1757. * @skb: send buffer
  1758. * @first: first buffer info buffer to use
  1759. * @tx_flags: collected send information
  1760. * @hdr_len: size of the packet header
  1761. * @td_cmd: the command field in the descriptor
  1762. * @td_offset: offset for checksum or crc
  1763. **/
  1764. static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1765. struct i40e_tx_buffer *first, u32 tx_flags,
  1766. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1767. {
  1768. unsigned int data_len = skb->data_len;
  1769. unsigned int size = skb_headlen(skb);
  1770. struct skb_frag_struct *frag;
  1771. struct i40e_tx_buffer *tx_bi;
  1772. struct i40e_tx_desc *tx_desc;
  1773. u16 i = tx_ring->next_to_use;
  1774. u32 td_tag = 0;
  1775. dma_addr_t dma;
  1776. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1777. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1778. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1779. I40E_TX_FLAGS_VLAN_SHIFT;
  1780. }
  1781. first->tx_flags = tx_flags;
  1782. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1783. tx_desc = I40E_TX_DESC(tx_ring, i);
  1784. tx_bi = first;
  1785. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1786. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1787. if (dma_mapping_error(tx_ring->dev, dma))
  1788. goto dma_error;
  1789. /* record length, and DMA address */
  1790. dma_unmap_len_set(tx_bi, len, size);
  1791. dma_unmap_addr_set(tx_bi, dma, dma);
  1792. /* align size to end of page */
  1793. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  1794. tx_desc->buffer_addr = cpu_to_le64(dma);
  1795. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1796. tx_desc->cmd_type_offset_bsz =
  1797. build_ctob(td_cmd, td_offset,
  1798. max_data, td_tag);
  1799. tx_desc++;
  1800. i++;
  1801. if (i == tx_ring->count) {
  1802. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1803. i = 0;
  1804. }
  1805. dma += max_data;
  1806. size -= max_data;
  1807. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1808. tx_desc->buffer_addr = cpu_to_le64(dma);
  1809. }
  1810. if (likely(!data_len))
  1811. break;
  1812. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1813. size, td_tag);
  1814. tx_desc++;
  1815. i++;
  1816. if (i == tx_ring->count) {
  1817. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1818. i = 0;
  1819. }
  1820. size = skb_frag_size(frag);
  1821. data_len -= size;
  1822. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1823. DMA_TO_DEVICE);
  1824. tx_bi = &tx_ring->tx_bi[i];
  1825. }
  1826. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  1827. i++;
  1828. if (i == tx_ring->count)
  1829. i = 0;
  1830. tx_ring->next_to_use = i;
  1831. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1832. /* write last descriptor with RS and EOP bits */
  1833. td_cmd |= I40E_TXD_CMD;
  1834. tx_desc->cmd_type_offset_bsz =
  1835. build_ctob(td_cmd, td_offset, size, td_tag);
  1836. /* Force memory writes to complete before letting h/w know there
  1837. * are new descriptors to fetch.
  1838. *
  1839. * We also use this memory barrier to make certain all of the
  1840. * status bits have been updated before next_to_watch is written.
  1841. */
  1842. wmb();
  1843. /* set next_to_watch value indicating a packet is present */
  1844. first->next_to_watch = tx_desc;
  1845. /* notify HW of packet */
  1846. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  1847. writel(i, tx_ring->tail);
  1848. /* we need this if more than one processor can write to our tail
  1849. * at a time, it synchronizes IO on IA64/Altix systems
  1850. */
  1851. mmiowb();
  1852. }
  1853. return;
  1854. dma_error:
  1855. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1856. /* clear dma mappings for failed tx_bi map */
  1857. for (;;) {
  1858. tx_bi = &tx_ring->tx_bi[i];
  1859. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1860. if (tx_bi == first)
  1861. break;
  1862. if (i == 0)
  1863. i = tx_ring->count;
  1864. i--;
  1865. }
  1866. tx_ring->next_to_use = i;
  1867. }
  1868. /**
  1869. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1870. * @skb: send buffer
  1871. * @tx_ring: ring to send buffer on
  1872. *
  1873. * Returns NETDEV_TX_OK if sent, else an error code
  1874. **/
  1875. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1876. struct i40e_ring *tx_ring)
  1877. {
  1878. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1879. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1880. struct i40e_tx_buffer *first;
  1881. u32 td_offset = 0;
  1882. u32 tx_flags = 0;
  1883. __be16 protocol;
  1884. u32 td_cmd = 0;
  1885. u8 hdr_len = 0;
  1886. int tso, count;
  1887. /* prefetch the data, we'll need it later */
  1888. prefetch(skb->data);
  1889. i40e_trace(xmit_frame_ring, skb, tx_ring);
  1890. count = i40e_xmit_descriptor_count(skb);
  1891. if (i40e_chk_linearize(skb, count)) {
  1892. if (__skb_linearize(skb)) {
  1893. dev_kfree_skb_any(skb);
  1894. return NETDEV_TX_OK;
  1895. }
  1896. count = i40e_txd_use_count(skb->len);
  1897. tx_ring->tx_stats.tx_linearize++;
  1898. }
  1899. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1900. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1901. * + 4 desc gap to avoid the cache line where head is,
  1902. * + 1 desc for context descriptor,
  1903. * otherwise try next time
  1904. */
  1905. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1906. tx_ring->tx_stats.tx_busy++;
  1907. return NETDEV_TX_BUSY;
  1908. }
  1909. /* record the location of the first descriptor for this packet */
  1910. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1911. first->skb = skb;
  1912. first->bytecount = skb->len;
  1913. first->gso_segs = 1;
  1914. /* prepare the xmit flags */
  1915. if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1916. goto out_drop;
  1917. /* obtain protocol of skb */
  1918. protocol = vlan_get_protocol(skb);
  1919. /* setup IPv4/IPv6 offloads */
  1920. if (protocol == htons(ETH_P_IP))
  1921. tx_flags |= I40E_TX_FLAGS_IPV4;
  1922. else if (protocol == htons(ETH_P_IPV6))
  1923. tx_flags |= I40E_TX_FLAGS_IPV6;
  1924. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  1925. if (tso < 0)
  1926. goto out_drop;
  1927. else if (tso)
  1928. tx_flags |= I40E_TX_FLAGS_TSO;
  1929. /* Always offload the checksum, since it's in the data descriptor */
  1930. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  1931. tx_ring, &cd_tunneling);
  1932. if (tso < 0)
  1933. goto out_drop;
  1934. skb_tx_timestamp(skb);
  1935. /* always enable CRC insertion offload */
  1936. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1937. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1938. cd_tunneling, cd_l2tag2);
  1939. i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1940. td_cmd, td_offset);
  1941. return NETDEV_TX_OK;
  1942. out_drop:
  1943. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  1944. dev_kfree_skb_any(first->skb);
  1945. first->skb = NULL;
  1946. return NETDEV_TX_OK;
  1947. }
  1948. /**
  1949. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1950. * @skb: send buffer
  1951. * @netdev: network interface device structure
  1952. *
  1953. * Returns NETDEV_TX_OK if sent, else an error code
  1954. **/
  1955. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1956. {
  1957. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1958. struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
  1959. /* hardware can't handle really short frames, hardware padding works
  1960. * beyond this point
  1961. */
  1962. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1963. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1964. return NETDEV_TX_OK;
  1965. skb->len = I40E_MIN_TX_LEN;
  1966. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1967. }
  1968. return i40e_xmit_frame_ring(skb, tx_ring);
  1969. }