i40e_txrx.c 71 KB

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