i40e_txrx.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Intel Ethernet Controller XL710 Family Linux 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 <linux/bpf_trace.h>
  30. #include <net/xdp.h>
  31. #include "i40e.h"
  32. #include "i40e_trace.h"
  33. #include "i40e_prototype.h"
  34. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  35. u32 td_tag)
  36. {
  37. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  38. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  39. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  40. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  41. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  42. }
  43. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  44. /**
  45. * i40e_fdir - Generate a Flow Director descriptor based on fdata
  46. * @tx_ring: Tx ring to send buffer on
  47. * @fdata: Flow director filter data
  48. * @add: Indicate if we are adding a rule or deleting one
  49. *
  50. **/
  51. static void i40e_fdir(struct i40e_ring *tx_ring,
  52. struct i40e_fdir_filter *fdata, bool add)
  53. {
  54. struct i40e_filter_program_desc *fdir_desc;
  55. struct i40e_pf *pf = tx_ring->vsi->back;
  56. u32 flex_ptype, dtype_cmd;
  57. u16 i;
  58. /* grab the next descriptor */
  59. i = tx_ring->next_to_use;
  60. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  61. i++;
  62. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  63. flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
  64. (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
  65. flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
  66. (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  67. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  68. (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  69. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  70. (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  71. /* Use LAN VSI Id if not programmed by user */
  72. flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
  73. ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
  74. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
  75. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  76. dtype_cmd |= add ?
  77. I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  78. I40E_TXD_FLTR_QW1_PCMD_SHIFT :
  79. I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  80. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  81. dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
  82. (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
  83. dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
  84. (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
  85. if (fdata->cnt_index) {
  86. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  87. dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
  88. ((u32)fdata->cnt_index <<
  89. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
  90. }
  91. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  92. fdir_desc->rsvd = cpu_to_le32(0);
  93. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  94. fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
  95. }
  96. #define I40E_FD_CLEAN_DELAY 10
  97. /**
  98. * i40e_program_fdir_filter - Program a Flow Director filter
  99. * @fdir_data: Packet data that will be filter parameters
  100. * @raw_packet: the pre-allocated packet buffer for FDir
  101. * @pf: The PF pointer
  102. * @add: True for add/update, False for remove
  103. **/
  104. static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
  105. u8 *raw_packet, struct i40e_pf *pf,
  106. bool add)
  107. {
  108. struct i40e_tx_buffer *tx_buf, *first;
  109. struct i40e_tx_desc *tx_desc;
  110. struct i40e_ring *tx_ring;
  111. struct i40e_vsi *vsi;
  112. struct device *dev;
  113. dma_addr_t dma;
  114. u32 td_cmd = 0;
  115. u16 i;
  116. /* find existing FDIR VSI */
  117. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  118. if (!vsi)
  119. return -ENOENT;
  120. tx_ring = vsi->tx_rings[0];
  121. dev = tx_ring->dev;
  122. /* we need two descriptors to add/del a filter and we can wait */
  123. for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
  124. if (!i)
  125. return -EAGAIN;
  126. msleep_interruptible(1);
  127. }
  128. dma = dma_map_single(dev, raw_packet,
  129. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  130. if (dma_mapping_error(dev, dma))
  131. goto dma_fail;
  132. /* grab the next descriptor */
  133. i = tx_ring->next_to_use;
  134. first = &tx_ring->tx_bi[i];
  135. i40e_fdir(tx_ring, fdir_data, add);
  136. /* Now program a dummy descriptor */
  137. i = tx_ring->next_to_use;
  138. tx_desc = I40E_TX_DESC(tx_ring, i);
  139. tx_buf = &tx_ring->tx_bi[i];
  140. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  141. memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
  142. /* record length, and DMA address */
  143. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  144. dma_unmap_addr_set(tx_buf, dma, dma);
  145. tx_desc->buffer_addr = cpu_to_le64(dma);
  146. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  147. tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
  148. tx_buf->raw_buf = (void *)raw_packet;
  149. tx_desc->cmd_type_offset_bsz =
  150. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  151. /* Force memory writes to complete before letting h/w
  152. * know there are new descriptors to fetch.
  153. */
  154. wmb();
  155. /* Mark the data descriptor to be watched */
  156. first->next_to_watch = tx_desc;
  157. writel(tx_ring->next_to_use, tx_ring->tail);
  158. return 0;
  159. dma_fail:
  160. return -1;
  161. }
  162. #define IP_HEADER_OFFSET 14
  163. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  164. /**
  165. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  166. * @vsi: pointer to the targeted VSI
  167. * @fd_data: the flow director data required for the FDir descriptor
  168. * @add: true adds a filter, false removes it
  169. *
  170. * Returns 0 if the filters were successfully added or removed
  171. **/
  172. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  173. struct i40e_fdir_filter *fd_data,
  174. bool add)
  175. {
  176. struct i40e_pf *pf = vsi->back;
  177. struct udphdr *udp;
  178. struct iphdr *ip;
  179. u8 *raw_packet;
  180. int ret;
  181. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  182. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  183. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  184. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  185. if (!raw_packet)
  186. return -ENOMEM;
  187. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  188. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  189. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  190. + sizeof(struct iphdr));
  191. ip->daddr = fd_data->dst_ip;
  192. udp->dest = fd_data->dst_port;
  193. ip->saddr = fd_data->src_ip;
  194. udp->source = fd_data->src_port;
  195. if (fd_data->flex_filter) {
  196. u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
  197. __be16 pattern = fd_data->flex_word;
  198. u16 off = fd_data->flex_offset;
  199. *((__force __be16 *)(payload + off)) = pattern;
  200. }
  201. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  202. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  203. if (ret) {
  204. dev_info(&pf->pdev->dev,
  205. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  206. fd_data->pctype, fd_data->fd_id, ret);
  207. /* Free the packet buffer since it wasn't added to the ring */
  208. kfree(raw_packet);
  209. return -EOPNOTSUPP;
  210. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  211. if (add)
  212. dev_info(&pf->pdev->dev,
  213. "Filter OK for PCTYPE %d loc = %d\n",
  214. fd_data->pctype, fd_data->fd_id);
  215. else
  216. dev_info(&pf->pdev->dev,
  217. "Filter deleted for PCTYPE %d loc = %d\n",
  218. fd_data->pctype, fd_data->fd_id);
  219. }
  220. if (add)
  221. pf->fd_udp4_filter_cnt++;
  222. else
  223. pf->fd_udp4_filter_cnt--;
  224. return 0;
  225. }
  226. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  227. /**
  228. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  229. * @vsi: pointer to the targeted VSI
  230. * @fd_data: the flow director data required for the FDir descriptor
  231. * @add: true adds a filter, false removes it
  232. *
  233. * Returns 0 if the filters were successfully added or removed
  234. **/
  235. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  236. struct i40e_fdir_filter *fd_data,
  237. bool add)
  238. {
  239. struct i40e_pf *pf = vsi->back;
  240. struct tcphdr *tcp;
  241. struct iphdr *ip;
  242. u8 *raw_packet;
  243. int ret;
  244. /* Dummy packet */
  245. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  246. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  247. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  248. 0x0, 0x72, 0, 0, 0, 0};
  249. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  250. if (!raw_packet)
  251. return -ENOMEM;
  252. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  253. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  254. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  255. + sizeof(struct iphdr));
  256. ip->daddr = fd_data->dst_ip;
  257. tcp->dest = fd_data->dst_port;
  258. ip->saddr = fd_data->src_ip;
  259. tcp->source = fd_data->src_port;
  260. if (fd_data->flex_filter) {
  261. u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
  262. __be16 pattern = fd_data->flex_word;
  263. u16 off = fd_data->flex_offset;
  264. *((__force __be16 *)(payload + off)) = pattern;
  265. }
  266. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  267. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  268. if (ret) {
  269. dev_info(&pf->pdev->dev,
  270. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  271. fd_data->pctype, fd_data->fd_id, ret);
  272. /* Free the packet buffer since it wasn't added to the ring */
  273. kfree(raw_packet);
  274. return -EOPNOTSUPP;
  275. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  276. if (add)
  277. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
  278. fd_data->pctype, fd_data->fd_id);
  279. else
  280. dev_info(&pf->pdev->dev,
  281. "Filter deleted for PCTYPE %d loc = %d\n",
  282. fd_data->pctype, fd_data->fd_id);
  283. }
  284. if (add) {
  285. pf->fd_tcp4_filter_cnt++;
  286. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  287. I40E_DEBUG_FD & pf->hw.debug_mask)
  288. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  289. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  290. } else {
  291. pf->fd_tcp4_filter_cnt--;
  292. }
  293. return 0;
  294. }
  295. #define I40E_SCTPIP_DUMMY_PACKET_LEN 46
  296. /**
  297. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  298. * a specific flow spec
  299. * @vsi: pointer to the targeted VSI
  300. * @fd_data: the flow director data required for the FDir descriptor
  301. * @add: true adds a filter, false removes it
  302. *
  303. * Returns 0 if the filters were successfully added or removed
  304. **/
  305. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  306. struct i40e_fdir_filter *fd_data,
  307. bool add)
  308. {
  309. struct i40e_pf *pf = vsi->back;
  310. struct sctphdr *sctp;
  311. struct iphdr *ip;
  312. u8 *raw_packet;
  313. int ret;
  314. /* Dummy packet */
  315. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  316. 0x45, 0, 0, 0x20, 0, 0, 0x40, 0, 0x40, 0x84, 0, 0, 0, 0, 0, 0,
  317. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  318. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  319. if (!raw_packet)
  320. return -ENOMEM;
  321. memcpy(raw_packet, packet, I40E_SCTPIP_DUMMY_PACKET_LEN);
  322. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  323. sctp = (struct sctphdr *)(raw_packet + IP_HEADER_OFFSET
  324. + sizeof(struct iphdr));
  325. ip->daddr = fd_data->dst_ip;
  326. sctp->dest = fd_data->dst_port;
  327. ip->saddr = fd_data->src_ip;
  328. sctp->source = fd_data->src_port;
  329. if (fd_data->flex_filter) {
  330. u8 *payload = raw_packet + I40E_SCTPIP_DUMMY_PACKET_LEN;
  331. __be16 pattern = fd_data->flex_word;
  332. u16 off = fd_data->flex_offset;
  333. *((__force __be16 *)(payload + off)) = pattern;
  334. }
  335. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  336. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  337. if (ret) {
  338. dev_info(&pf->pdev->dev,
  339. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  340. fd_data->pctype, fd_data->fd_id, ret);
  341. /* Free the packet buffer since it wasn't added to the ring */
  342. kfree(raw_packet);
  343. return -EOPNOTSUPP;
  344. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  345. if (add)
  346. dev_info(&pf->pdev->dev,
  347. "Filter OK for PCTYPE %d loc = %d\n",
  348. fd_data->pctype, fd_data->fd_id);
  349. else
  350. dev_info(&pf->pdev->dev,
  351. "Filter deleted for PCTYPE %d loc = %d\n",
  352. fd_data->pctype, fd_data->fd_id);
  353. }
  354. if (add)
  355. pf->fd_sctp4_filter_cnt++;
  356. else
  357. pf->fd_sctp4_filter_cnt--;
  358. return 0;
  359. }
  360. #define I40E_IP_DUMMY_PACKET_LEN 34
  361. /**
  362. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  363. * a specific flow spec
  364. * @vsi: pointer to the targeted VSI
  365. * @fd_data: the flow director data required for the FDir descriptor
  366. * @add: true adds a filter, false removes it
  367. *
  368. * Returns 0 if the filters were successfully added or removed
  369. **/
  370. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  371. struct i40e_fdir_filter *fd_data,
  372. bool add)
  373. {
  374. struct i40e_pf *pf = vsi->back;
  375. struct iphdr *ip;
  376. u8 *raw_packet;
  377. int ret;
  378. int i;
  379. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  380. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  381. 0, 0, 0, 0};
  382. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  383. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  384. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  385. if (!raw_packet)
  386. return -ENOMEM;
  387. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  388. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  389. ip->saddr = fd_data->src_ip;
  390. ip->daddr = fd_data->dst_ip;
  391. ip->protocol = 0;
  392. if (fd_data->flex_filter) {
  393. u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
  394. __be16 pattern = fd_data->flex_word;
  395. u16 off = fd_data->flex_offset;
  396. *((__force __be16 *)(payload + off)) = pattern;
  397. }
  398. fd_data->pctype = i;
  399. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  400. if (ret) {
  401. dev_info(&pf->pdev->dev,
  402. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  403. fd_data->pctype, fd_data->fd_id, ret);
  404. /* The packet buffer wasn't added to the ring so we
  405. * need to free it now.
  406. */
  407. kfree(raw_packet);
  408. return -EOPNOTSUPP;
  409. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  410. if (add)
  411. dev_info(&pf->pdev->dev,
  412. "Filter OK for PCTYPE %d loc = %d\n",
  413. fd_data->pctype, fd_data->fd_id);
  414. else
  415. dev_info(&pf->pdev->dev,
  416. "Filter deleted for PCTYPE %d loc = %d\n",
  417. fd_data->pctype, fd_data->fd_id);
  418. }
  419. }
  420. if (add)
  421. pf->fd_ip4_filter_cnt++;
  422. else
  423. pf->fd_ip4_filter_cnt--;
  424. return 0;
  425. }
  426. /**
  427. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  428. * @vsi: pointer to the targeted VSI
  429. * @cmd: command to get or set RX flow classification rules
  430. * @add: true adds a filter, false removes it
  431. *
  432. **/
  433. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  434. struct i40e_fdir_filter *input, bool add)
  435. {
  436. struct i40e_pf *pf = vsi->back;
  437. int ret;
  438. switch (input->flow_type & ~FLOW_EXT) {
  439. case TCP_V4_FLOW:
  440. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  441. break;
  442. case UDP_V4_FLOW:
  443. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  444. break;
  445. case SCTP_V4_FLOW:
  446. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  447. break;
  448. case IP_USER_FLOW:
  449. switch (input->ip4_proto) {
  450. case IPPROTO_TCP:
  451. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  452. break;
  453. case IPPROTO_UDP:
  454. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  455. break;
  456. case IPPROTO_SCTP:
  457. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  458. break;
  459. case IPPROTO_IP:
  460. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  461. break;
  462. default:
  463. /* We cannot support masking based on protocol */
  464. dev_info(&pf->pdev->dev, "Unsupported IPv4 protocol 0x%02x\n",
  465. input->ip4_proto);
  466. return -EINVAL;
  467. }
  468. break;
  469. default:
  470. dev_info(&pf->pdev->dev, "Unsupported flow type 0x%02x\n",
  471. input->flow_type);
  472. return -EINVAL;
  473. }
  474. /* The buffer allocated here will be normally be freed by
  475. * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
  476. * completion. In the event of an error adding the buffer to the FDIR
  477. * ring, it will immediately be freed. It may also be freed by
  478. * i40e_clean_tx_ring() when closing the VSI.
  479. */
  480. return ret;
  481. }
  482. /**
  483. * i40e_fd_handle_status - check the Programming Status for FD
  484. * @rx_ring: the Rx ring for this descriptor
  485. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  486. * @prog_id: the id originally used for programming
  487. *
  488. * This is used to verify if the FD programming or invalidation
  489. * requested by SW to the HW is successful or not and take actions accordingly.
  490. **/
  491. static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  492. union i40e_rx_desc *rx_desc, u8 prog_id)
  493. {
  494. struct i40e_pf *pf = rx_ring->vsi->back;
  495. struct pci_dev *pdev = pf->pdev;
  496. u32 fcnt_prog, fcnt_avail;
  497. u32 error;
  498. u64 qw;
  499. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  500. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  501. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  502. if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  503. pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
  504. if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
  505. (I40E_DEBUG_FD & pf->hw.debug_mask))
  506. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  507. pf->fd_inv);
  508. /* Check if the programming error is for ATR.
  509. * If so, auto disable ATR and set a state for
  510. * flush in progress. Next time we come here if flush is in
  511. * progress do nothing, once flush is complete the state will
  512. * be cleared.
  513. */
  514. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  515. return;
  516. pf->fd_add_err++;
  517. /* store the current atr filter count */
  518. pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
  519. if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
  520. test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state)) {
  521. /* These set_bit() calls aren't atomic with the
  522. * test_bit() here, but worse case we potentially
  523. * disable ATR and queue a flush right after SB
  524. * support is re-enabled. That shouldn't cause an
  525. * issue in practice
  526. */
  527. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  528. set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  529. }
  530. /* filter programming failed most likely due to table full */
  531. fcnt_prog = i40e_get_global_fd_count(pf);
  532. fcnt_avail = pf->fdir_pf_filter_count;
  533. /* If ATR is running fcnt_prog can quickly change,
  534. * if we are very close to full, it makes sense to disable
  535. * FD ATR/SB and then re-enable it when there is room.
  536. */
  537. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  538. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  539. !test_and_set_bit(__I40E_FD_SB_AUTO_DISABLED,
  540. pf->state))
  541. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  542. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  543. }
  544. } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  545. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  546. dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
  547. rx_desc->wb.qword0.hi_dword.fd_id);
  548. }
  549. }
  550. /**
  551. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  552. * @ring: the ring that owns the buffer
  553. * @tx_buffer: the buffer to free
  554. **/
  555. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  556. struct i40e_tx_buffer *tx_buffer)
  557. {
  558. if (tx_buffer->skb) {
  559. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  560. kfree(tx_buffer->raw_buf);
  561. else if (ring_is_xdp(ring))
  562. page_frag_free(tx_buffer->raw_buf);
  563. else
  564. dev_kfree_skb_any(tx_buffer->skb);
  565. if (dma_unmap_len(tx_buffer, len))
  566. dma_unmap_single(ring->dev,
  567. dma_unmap_addr(tx_buffer, dma),
  568. dma_unmap_len(tx_buffer, len),
  569. DMA_TO_DEVICE);
  570. } else if (dma_unmap_len(tx_buffer, len)) {
  571. dma_unmap_page(ring->dev,
  572. dma_unmap_addr(tx_buffer, dma),
  573. dma_unmap_len(tx_buffer, len),
  574. DMA_TO_DEVICE);
  575. }
  576. tx_buffer->next_to_watch = NULL;
  577. tx_buffer->skb = NULL;
  578. dma_unmap_len_set(tx_buffer, len, 0);
  579. /* tx_buffer must be completely set up in the transmit path */
  580. }
  581. /**
  582. * i40e_clean_tx_ring - Free any empty Tx buffers
  583. * @tx_ring: ring to be cleaned
  584. **/
  585. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  586. {
  587. unsigned long bi_size;
  588. u16 i;
  589. /* ring already cleared, nothing to do */
  590. if (!tx_ring->tx_bi)
  591. return;
  592. /* Free all the Tx ring sk_buffs */
  593. for (i = 0; i < tx_ring->count; i++)
  594. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  595. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  596. memset(tx_ring->tx_bi, 0, bi_size);
  597. /* Zero out the descriptor ring */
  598. memset(tx_ring->desc, 0, tx_ring->size);
  599. tx_ring->next_to_use = 0;
  600. tx_ring->next_to_clean = 0;
  601. if (!tx_ring->netdev)
  602. return;
  603. /* cleanup Tx queue statistics */
  604. netdev_tx_reset_queue(txring_txq(tx_ring));
  605. }
  606. /**
  607. * i40e_free_tx_resources - Free Tx resources per queue
  608. * @tx_ring: Tx descriptor ring for a specific queue
  609. *
  610. * Free all transmit software resources
  611. **/
  612. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  613. {
  614. i40e_clean_tx_ring(tx_ring);
  615. kfree(tx_ring->tx_bi);
  616. tx_ring->tx_bi = NULL;
  617. if (tx_ring->desc) {
  618. dma_free_coherent(tx_ring->dev, tx_ring->size,
  619. tx_ring->desc, tx_ring->dma);
  620. tx_ring->desc = NULL;
  621. }
  622. }
  623. /**
  624. * i40e_get_tx_pending - how many tx descriptors not processed
  625. * @tx_ring: the ring of descriptors
  626. * @in_sw: use SW variables
  627. *
  628. * Since there is no access to the ring head register
  629. * in XL710, we need to use our local copies
  630. **/
  631. u32 i40e_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  632. {
  633. u32 head, tail;
  634. if (!in_sw) {
  635. head = i40e_get_head(ring);
  636. tail = readl(ring->tail);
  637. } else {
  638. head = ring->next_to_clean;
  639. tail = ring->next_to_use;
  640. }
  641. if (head != tail)
  642. return (head < tail) ?
  643. tail - head : (tail + ring->count - head);
  644. return 0;
  645. }
  646. /**
  647. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  648. * @vsi: pointer to vsi struct with tx queues
  649. *
  650. * VSI has netdev and netdev has TX queues. This function is to check each of
  651. * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
  652. **/
  653. void i40e_detect_recover_hung(struct i40e_vsi *vsi)
  654. {
  655. struct i40e_ring *tx_ring = NULL;
  656. struct net_device *netdev;
  657. unsigned int i;
  658. int packets;
  659. if (!vsi)
  660. return;
  661. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  662. return;
  663. netdev = vsi->netdev;
  664. if (!netdev)
  665. return;
  666. if (!netif_carrier_ok(netdev))
  667. return;
  668. for (i = 0; i < vsi->num_queue_pairs; i++) {
  669. tx_ring = vsi->tx_rings[i];
  670. if (tx_ring && tx_ring->desc) {
  671. /* If packet counter has not changed the queue is
  672. * likely stalled, so force an interrupt for this
  673. * queue.
  674. *
  675. * prev_pkt_ctr would be negative if there was no
  676. * pending work.
  677. */
  678. packets = tx_ring->stats.packets & INT_MAX;
  679. if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
  680. i40e_force_wb(vsi, tx_ring->q_vector);
  681. continue;
  682. }
  683. /* Memory barrier between read of packet count and call
  684. * to i40e_get_tx_pending()
  685. */
  686. smp_rmb();
  687. tx_ring->tx_stats.prev_pkt_ctr =
  688. i40e_get_tx_pending(tx_ring, true) ? packets : -1;
  689. }
  690. }
  691. }
  692. #define WB_STRIDE 4
  693. /**
  694. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  695. * @vsi: the VSI we care about
  696. * @tx_ring: Tx ring to clean
  697. * @napi_budget: Used to determine if we are in netpoll
  698. *
  699. * Returns true if there's any budget left (e.g. the clean is finished)
  700. **/
  701. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  702. struct i40e_ring *tx_ring, int napi_budget)
  703. {
  704. u16 i = tx_ring->next_to_clean;
  705. struct i40e_tx_buffer *tx_buf;
  706. struct i40e_tx_desc *tx_head;
  707. struct i40e_tx_desc *tx_desc;
  708. unsigned int total_bytes = 0, total_packets = 0;
  709. unsigned int budget = vsi->work_limit;
  710. tx_buf = &tx_ring->tx_bi[i];
  711. tx_desc = I40E_TX_DESC(tx_ring, i);
  712. i -= tx_ring->count;
  713. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  714. do {
  715. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  716. /* if next_to_watch is not set then there is no work pending */
  717. if (!eop_desc)
  718. break;
  719. /* prevent any other reads prior to eop_desc */
  720. smp_rmb();
  721. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  722. /* we have caught up to head, no work left to do */
  723. if (tx_head == tx_desc)
  724. break;
  725. /* clear next_to_watch to prevent false hangs */
  726. tx_buf->next_to_watch = NULL;
  727. /* update the statistics for this packet */
  728. total_bytes += tx_buf->bytecount;
  729. total_packets += tx_buf->gso_segs;
  730. /* free the skb/XDP data */
  731. if (ring_is_xdp(tx_ring))
  732. page_frag_free(tx_buf->raw_buf);
  733. else
  734. napi_consume_skb(tx_buf->skb, napi_budget);
  735. /* unmap skb header data */
  736. dma_unmap_single(tx_ring->dev,
  737. dma_unmap_addr(tx_buf, dma),
  738. dma_unmap_len(tx_buf, len),
  739. DMA_TO_DEVICE);
  740. /* clear tx_buffer data */
  741. tx_buf->skb = NULL;
  742. dma_unmap_len_set(tx_buf, len, 0);
  743. /* unmap remaining buffers */
  744. while (tx_desc != eop_desc) {
  745. i40e_trace(clean_tx_irq_unmap,
  746. tx_ring, tx_desc, tx_buf);
  747. tx_buf++;
  748. tx_desc++;
  749. i++;
  750. if (unlikely(!i)) {
  751. i -= tx_ring->count;
  752. tx_buf = tx_ring->tx_bi;
  753. tx_desc = I40E_TX_DESC(tx_ring, 0);
  754. }
  755. /* unmap any remaining paged data */
  756. if (dma_unmap_len(tx_buf, len)) {
  757. dma_unmap_page(tx_ring->dev,
  758. dma_unmap_addr(tx_buf, dma),
  759. dma_unmap_len(tx_buf, len),
  760. DMA_TO_DEVICE);
  761. dma_unmap_len_set(tx_buf, len, 0);
  762. }
  763. }
  764. /* move us one more past the eop_desc for start of next pkt */
  765. tx_buf++;
  766. tx_desc++;
  767. i++;
  768. if (unlikely(!i)) {
  769. i -= tx_ring->count;
  770. tx_buf = tx_ring->tx_bi;
  771. tx_desc = I40E_TX_DESC(tx_ring, 0);
  772. }
  773. prefetch(tx_desc);
  774. /* update budget accounting */
  775. budget--;
  776. } while (likely(budget));
  777. i += tx_ring->count;
  778. tx_ring->next_to_clean = i;
  779. u64_stats_update_begin(&tx_ring->syncp);
  780. tx_ring->stats.bytes += total_bytes;
  781. tx_ring->stats.packets += total_packets;
  782. u64_stats_update_end(&tx_ring->syncp);
  783. tx_ring->q_vector->tx.total_bytes += total_bytes;
  784. tx_ring->q_vector->tx.total_packets += total_packets;
  785. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  786. /* check to see if there are < 4 descriptors
  787. * waiting to be written back, then kick the hardware to force
  788. * them to be written back in case we stay in NAPI.
  789. * In this mode on X722 we do not enable Interrupt.
  790. */
  791. unsigned int j = i40e_get_tx_pending(tx_ring, false);
  792. if (budget &&
  793. ((j / WB_STRIDE) == 0) && (j > 0) &&
  794. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  795. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  796. tx_ring->arm_wb = true;
  797. }
  798. if (ring_is_xdp(tx_ring))
  799. return !!budget;
  800. /* notify netdev of completed buffers */
  801. netdev_tx_completed_queue(txring_txq(tx_ring),
  802. total_packets, total_bytes);
  803. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  804. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  805. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  806. /* Make sure that anybody stopping the queue after this
  807. * sees the new next_to_clean.
  808. */
  809. smp_mb();
  810. if (__netif_subqueue_stopped(tx_ring->netdev,
  811. tx_ring->queue_index) &&
  812. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  813. netif_wake_subqueue(tx_ring->netdev,
  814. tx_ring->queue_index);
  815. ++tx_ring->tx_stats.restart_queue;
  816. }
  817. }
  818. return !!budget;
  819. }
  820. /**
  821. * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  822. * @vsi: the VSI we care about
  823. * @q_vector: the vector on which to enable writeback
  824. *
  825. **/
  826. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  827. struct i40e_q_vector *q_vector)
  828. {
  829. u16 flags = q_vector->tx.ring[0].flags;
  830. u32 val;
  831. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  832. return;
  833. if (q_vector->arm_wb_state)
  834. return;
  835. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  836. val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
  837. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
  838. wr32(&vsi->back->hw,
  839. I40E_PFINT_DYN_CTLN(q_vector->reg_idx),
  840. val);
  841. } else {
  842. val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
  843. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
  844. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  845. }
  846. q_vector->arm_wb_state = true;
  847. }
  848. /**
  849. * i40e_force_wb - Issue SW Interrupt so HW does a wb
  850. * @vsi: the VSI we care about
  851. * @q_vector: the vector on which to force writeback
  852. *
  853. **/
  854. void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  855. {
  856. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  857. u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  858. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
  859. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
  860. I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
  861. /* allow 00 to be written to the index */
  862. wr32(&vsi->back->hw,
  863. I40E_PFINT_DYN_CTLN(q_vector->reg_idx), val);
  864. } else {
  865. u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  866. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
  867. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
  868. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
  869. /* allow 00 to be written to the index */
  870. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  871. }
  872. }
  873. static inline bool i40e_container_is_rx(struct i40e_q_vector *q_vector,
  874. struct i40e_ring_container *rc)
  875. {
  876. return &q_vector->rx == rc;
  877. }
  878. static inline unsigned int i40e_itr_divisor(struct i40e_q_vector *q_vector)
  879. {
  880. unsigned int divisor;
  881. switch (q_vector->vsi->back->hw.phy.link_info.link_speed) {
  882. case I40E_LINK_SPEED_40GB:
  883. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 1024;
  884. break;
  885. case I40E_LINK_SPEED_25GB:
  886. case I40E_LINK_SPEED_20GB:
  887. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 512;
  888. break;
  889. default:
  890. case I40E_LINK_SPEED_10GB:
  891. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 256;
  892. break;
  893. case I40E_LINK_SPEED_1GB:
  894. case I40E_LINK_SPEED_100MB:
  895. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 32;
  896. break;
  897. }
  898. return divisor;
  899. }
  900. /**
  901. * i40e_update_itr - update the dynamic ITR value based on statistics
  902. * @q_vector: structure containing interrupt and ring information
  903. * @rc: structure containing ring performance data
  904. *
  905. * Stores a new ITR value based on packets and byte
  906. * counts during the last interrupt. The advantage of per interrupt
  907. * computation is faster updates and more accurate ITR for the current
  908. * traffic pattern. Constants in this function were computed
  909. * based on theoretical maximum wire speed and thresholds were set based
  910. * on testing data as well as attempting to minimize response time
  911. * while increasing bulk throughput.
  912. **/
  913. static void i40e_update_itr(struct i40e_q_vector *q_vector,
  914. struct i40e_ring_container *rc)
  915. {
  916. unsigned int avg_wire_size, packets, bytes, itr;
  917. unsigned long next_update = jiffies;
  918. /* If we don't have any rings just leave ourselves set for maximum
  919. * possible latency so we take ourselves out of the equation.
  920. */
  921. if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
  922. return;
  923. /* For Rx we want to push the delay up and default to low latency.
  924. * for Tx we want to pull the delay down and default to high latency.
  925. */
  926. itr = i40e_container_is_rx(q_vector, rc) ?
  927. I40E_ITR_ADAPTIVE_MIN_USECS | I40E_ITR_ADAPTIVE_LATENCY :
  928. I40E_ITR_ADAPTIVE_MAX_USECS | I40E_ITR_ADAPTIVE_LATENCY;
  929. /* If we didn't update within up to 1 - 2 jiffies we can assume
  930. * that either packets are coming in so slow there hasn't been
  931. * any work, or that there is so much work that NAPI is dealing
  932. * with interrupt moderation and we don't need to do anything.
  933. */
  934. if (time_after(next_update, rc->next_update))
  935. goto clear_counts;
  936. /* If itr_countdown is set it means we programmed an ITR within
  937. * the last 4 interrupt cycles. This has a side effect of us
  938. * potentially firing an early interrupt. In order to work around
  939. * this we need to throw out any data received for a few
  940. * interrupts following the update.
  941. */
  942. if (q_vector->itr_countdown) {
  943. itr = rc->target_itr;
  944. goto clear_counts;
  945. }
  946. packets = rc->total_packets;
  947. bytes = rc->total_bytes;
  948. if (i40e_container_is_rx(q_vector, rc)) {
  949. /* If Rx there are 1 to 4 packets and bytes are less than
  950. * 9000 assume insufficient data to use bulk rate limiting
  951. * approach unless Tx is already in bulk rate limiting. We
  952. * are likely latency driven.
  953. */
  954. if (packets && packets < 4 && bytes < 9000 &&
  955. (q_vector->tx.target_itr & I40E_ITR_ADAPTIVE_LATENCY)) {
  956. itr = I40E_ITR_ADAPTIVE_LATENCY;
  957. goto adjust_by_size;
  958. }
  959. } else if (packets < 4) {
  960. /* If we have Tx and Rx ITR maxed and Tx ITR is running in
  961. * bulk mode and we are receiving 4 or fewer packets just
  962. * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
  963. * that the Rx can relax.
  964. */
  965. if (rc->target_itr == I40E_ITR_ADAPTIVE_MAX_USECS &&
  966. (q_vector->rx.target_itr & I40E_ITR_MASK) ==
  967. I40E_ITR_ADAPTIVE_MAX_USECS)
  968. goto clear_counts;
  969. } else if (packets > 32) {
  970. /* If we have processed over 32 packets in a single interrupt
  971. * for Tx assume we need to switch over to "bulk" mode.
  972. */
  973. rc->target_itr &= ~I40E_ITR_ADAPTIVE_LATENCY;
  974. }
  975. /* We have no packets to actually measure against. This means
  976. * either one of the other queues on this vector is active or
  977. * we are a Tx queue doing TSO with too high of an interrupt rate.
  978. *
  979. * Between 4 and 56 we can assume that our current interrupt delay
  980. * is only slightly too low. As such we should increase it by a small
  981. * fixed amount.
  982. */
  983. if (packets < 56) {
  984. itr = rc->target_itr + I40E_ITR_ADAPTIVE_MIN_INC;
  985. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  986. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  987. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  988. }
  989. goto clear_counts;
  990. }
  991. if (packets <= 256) {
  992. itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
  993. itr &= I40E_ITR_MASK;
  994. /* Between 56 and 112 is our "goldilocks" zone where we are
  995. * working out "just right". Just report that our current
  996. * ITR is good for us.
  997. */
  998. if (packets <= 112)
  999. goto clear_counts;
  1000. /* If packet count is 128 or greater we are likely looking
  1001. * at a slight overrun of the delay we want. Try halving
  1002. * our delay to see if that will cut the number of packets
  1003. * in half per interrupt.
  1004. */
  1005. itr /= 2;
  1006. itr &= I40E_ITR_MASK;
  1007. if (itr < I40E_ITR_ADAPTIVE_MIN_USECS)
  1008. itr = I40E_ITR_ADAPTIVE_MIN_USECS;
  1009. goto clear_counts;
  1010. }
  1011. /* The paths below assume we are dealing with a bulk ITR since
  1012. * number of packets is greater than 256. We are just going to have
  1013. * to compute a value and try to bring the count under control,
  1014. * though for smaller packet sizes there isn't much we can do as
  1015. * NAPI polling will likely be kicking in sooner rather than later.
  1016. */
  1017. itr = I40E_ITR_ADAPTIVE_BULK;
  1018. adjust_by_size:
  1019. /* If packet counts are 256 or greater we can assume we have a gross
  1020. * overestimation of what the rate should be. Instead of trying to fine
  1021. * tune it just use the formula below to try and dial in an exact value
  1022. * give the current packet size of the frame.
  1023. */
  1024. avg_wire_size = bytes / packets;
  1025. /* The following is a crude approximation of:
  1026. * wmem_default / (size + overhead) = desired_pkts_per_int
  1027. * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
  1028. * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
  1029. *
  1030. * Assuming wmem_default is 212992 and overhead is 640 bytes per
  1031. * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
  1032. * formula down to
  1033. *
  1034. * (170 * (size + 24)) / (size + 640) = ITR
  1035. *
  1036. * We first do some math on the packet size and then finally bitshift
  1037. * by 8 after rounding up. We also have to account for PCIe link speed
  1038. * difference as ITR scales based on this.
  1039. */
  1040. if (avg_wire_size <= 60) {
  1041. /* Start at 250k ints/sec */
  1042. avg_wire_size = 4096;
  1043. } else if (avg_wire_size <= 380) {
  1044. /* 250K ints/sec to 60K ints/sec */
  1045. avg_wire_size *= 40;
  1046. avg_wire_size += 1696;
  1047. } else if (avg_wire_size <= 1084) {
  1048. /* 60K ints/sec to 36K ints/sec */
  1049. avg_wire_size *= 15;
  1050. avg_wire_size += 11452;
  1051. } else if (avg_wire_size <= 1980) {
  1052. /* 36K ints/sec to 30K ints/sec */
  1053. avg_wire_size *= 5;
  1054. avg_wire_size += 22420;
  1055. } else {
  1056. /* plateau at a limit of 30K ints/sec */
  1057. avg_wire_size = 32256;
  1058. }
  1059. /* If we are in low latency mode halve our delay which doubles the
  1060. * rate to somewhere between 100K to 16K ints/sec
  1061. */
  1062. if (itr & I40E_ITR_ADAPTIVE_LATENCY)
  1063. avg_wire_size /= 2;
  1064. /* Resultant value is 256 times larger than it needs to be. This
  1065. * gives us room to adjust the value as needed to either increase
  1066. * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
  1067. *
  1068. * Use addition as we have already recorded the new latency flag
  1069. * for the ITR value.
  1070. */
  1071. itr += DIV_ROUND_UP(avg_wire_size, i40e_itr_divisor(q_vector)) *
  1072. I40E_ITR_ADAPTIVE_MIN_INC;
  1073. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  1074. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  1075. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  1076. }
  1077. clear_counts:
  1078. /* write back value */
  1079. rc->target_itr = itr;
  1080. /* next update should occur within next jiffy */
  1081. rc->next_update = next_update + 1;
  1082. rc->total_bytes = 0;
  1083. rc->total_packets = 0;
  1084. }
  1085. /**
  1086. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  1087. * @rx_ring: rx descriptor ring to store buffers on
  1088. * @old_buff: donor buffer to have page reused
  1089. *
  1090. * Synchronizes page for reuse by the adapter
  1091. **/
  1092. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  1093. struct i40e_rx_buffer *old_buff)
  1094. {
  1095. struct i40e_rx_buffer *new_buff;
  1096. u16 nta = rx_ring->next_to_alloc;
  1097. new_buff = &rx_ring->rx_bi[nta];
  1098. /* update, and store next to alloc */
  1099. nta++;
  1100. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  1101. /* transfer page from old buffer to new buffer */
  1102. new_buff->dma = old_buff->dma;
  1103. new_buff->page = old_buff->page;
  1104. new_buff->page_offset = old_buff->page_offset;
  1105. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  1106. }
  1107. /**
  1108. * i40e_rx_is_programming_status - check for programming status descriptor
  1109. * @qw: qword representing status_error_len in CPU ordering
  1110. *
  1111. * The value of in the descriptor length field indicate if this
  1112. * is a programming status descriptor for flow director or FCoE
  1113. * by the value of I40E_RX_PROG_STATUS_DESC_LENGTH, otherwise
  1114. * it is a packet descriptor.
  1115. **/
  1116. static inline bool i40e_rx_is_programming_status(u64 qw)
  1117. {
  1118. /* The Rx filter programming status and SPH bit occupy the same
  1119. * spot in the descriptor. Since we don't support packet split we
  1120. * can just reuse the bit as an indication that this is a
  1121. * programming status descriptor.
  1122. */
  1123. return qw & I40E_RXD_QW1_LENGTH_SPH_MASK;
  1124. }
  1125. /**
  1126. * i40e_clean_programming_status - clean the programming status descriptor
  1127. * @rx_ring: the rx ring that has this descriptor
  1128. * @rx_desc: the rx descriptor written back by HW
  1129. * @qw: qword representing status_error_len in CPU ordering
  1130. *
  1131. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  1132. * status being successful or not and take actions accordingly. FCoE should
  1133. * handle its context/filter programming/invalidation status and take actions.
  1134. *
  1135. **/
  1136. static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
  1137. union i40e_rx_desc *rx_desc,
  1138. u64 qw)
  1139. {
  1140. struct i40e_rx_buffer *rx_buffer;
  1141. u32 ntc = rx_ring->next_to_clean;
  1142. u8 id;
  1143. /* fetch, update, and store next to clean */
  1144. rx_buffer = &rx_ring->rx_bi[ntc++];
  1145. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1146. rx_ring->next_to_clean = ntc;
  1147. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1148. /* place unused page back on the ring */
  1149. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1150. rx_ring->rx_stats.page_reuse_count++;
  1151. /* clear contents of buffer_info */
  1152. rx_buffer->page = NULL;
  1153. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  1154. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  1155. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  1156. i40e_fd_handle_status(rx_ring, rx_desc, id);
  1157. }
  1158. /**
  1159. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  1160. * @tx_ring: the tx ring to set up
  1161. *
  1162. * Return 0 on success, negative on error
  1163. **/
  1164. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  1165. {
  1166. struct device *dev = tx_ring->dev;
  1167. int bi_size;
  1168. if (!dev)
  1169. return -ENOMEM;
  1170. /* warn if we are about to overwrite the pointer */
  1171. WARN_ON(tx_ring->tx_bi);
  1172. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  1173. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  1174. if (!tx_ring->tx_bi)
  1175. goto err;
  1176. u64_stats_init(&tx_ring->syncp);
  1177. /* round up to nearest 4K */
  1178. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  1179. /* add u32 for head writeback, align after this takes care of
  1180. * guaranteeing this is at least one cache line in size
  1181. */
  1182. tx_ring->size += sizeof(u32);
  1183. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1184. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  1185. &tx_ring->dma, GFP_KERNEL);
  1186. if (!tx_ring->desc) {
  1187. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  1188. tx_ring->size);
  1189. goto err;
  1190. }
  1191. tx_ring->next_to_use = 0;
  1192. tx_ring->next_to_clean = 0;
  1193. tx_ring->tx_stats.prev_pkt_ctr = -1;
  1194. return 0;
  1195. err:
  1196. kfree(tx_ring->tx_bi);
  1197. tx_ring->tx_bi = NULL;
  1198. return -ENOMEM;
  1199. }
  1200. /**
  1201. * i40e_clean_rx_ring - Free Rx buffers
  1202. * @rx_ring: ring to be cleaned
  1203. **/
  1204. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  1205. {
  1206. unsigned long bi_size;
  1207. u16 i;
  1208. /* ring already cleared, nothing to do */
  1209. if (!rx_ring->rx_bi)
  1210. return;
  1211. if (rx_ring->skb) {
  1212. dev_kfree_skb(rx_ring->skb);
  1213. rx_ring->skb = NULL;
  1214. }
  1215. /* Free all the Rx ring sk_buffs */
  1216. for (i = 0; i < rx_ring->count; i++) {
  1217. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  1218. if (!rx_bi->page)
  1219. continue;
  1220. /* Invalidate cache lines that may have been written to by
  1221. * device so that we avoid corrupting memory.
  1222. */
  1223. dma_sync_single_range_for_cpu(rx_ring->dev,
  1224. rx_bi->dma,
  1225. rx_bi->page_offset,
  1226. rx_ring->rx_buf_len,
  1227. DMA_FROM_DEVICE);
  1228. /* free resources associated with mapping */
  1229. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  1230. i40e_rx_pg_size(rx_ring),
  1231. DMA_FROM_DEVICE,
  1232. I40E_RX_DMA_ATTR);
  1233. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  1234. rx_bi->page = NULL;
  1235. rx_bi->page_offset = 0;
  1236. }
  1237. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1238. memset(rx_ring->rx_bi, 0, bi_size);
  1239. /* Zero out the descriptor ring */
  1240. memset(rx_ring->desc, 0, rx_ring->size);
  1241. rx_ring->next_to_alloc = 0;
  1242. rx_ring->next_to_clean = 0;
  1243. rx_ring->next_to_use = 0;
  1244. }
  1245. /**
  1246. * i40e_free_rx_resources - Free Rx resources
  1247. * @rx_ring: ring to clean the resources from
  1248. *
  1249. * Free all receive software resources
  1250. **/
  1251. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  1252. {
  1253. i40e_clean_rx_ring(rx_ring);
  1254. if (rx_ring->vsi->type == I40E_VSI_MAIN)
  1255. xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
  1256. rx_ring->xdp_prog = NULL;
  1257. kfree(rx_ring->rx_bi);
  1258. rx_ring->rx_bi = NULL;
  1259. if (rx_ring->desc) {
  1260. dma_free_coherent(rx_ring->dev, rx_ring->size,
  1261. rx_ring->desc, rx_ring->dma);
  1262. rx_ring->desc = NULL;
  1263. }
  1264. }
  1265. /**
  1266. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  1267. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  1268. *
  1269. * Returns 0 on success, negative on failure
  1270. **/
  1271. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  1272. {
  1273. struct device *dev = rx_ring->dev;
  1274. int err = -ENOMEM;
  1275. int bi_size;
  1276. /* warn if we are about to overwrite the pointer */
  1277. WARN_ON(rx_ring->rx_bi);
  1278. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1279. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  1280. if (!rx_ring->rx_bi)
  1281. goto err;
  1282. u64_stats_init(&rx_ring->syncp);
  1283. /* Round up to nearest 4K */
  1284. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  1285. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1286. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  1287. &rx_ring->dma, GFP_KERNEL);
  1288. if (!rx_ring->desc) {
  1289. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  1290. rx_ring->size);
  1291. goto err;
  1292. }
  1293. rx_ring->next_to_alloc = 0;
  1294. rx_ring->next_to_clean = 0;
  1295. rx_ring->next_to_use = 0;
  1296. /* XDP RX-queue info only needed for RX rings exposed to XDP */
  1297. if (rx_ring->vsi->type == I40E_VSI_MAIN) {
  1298. err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
  1299. rx_ring->queue_index);
  1300. if (err < 0)
  1301. goto err;
  1302. }
  1303. rx_ring->xdp_prog = rx_ring->vsi->xdp_prog;
  1304. return 0;
  1305. err:
  1306. kfree(rx_ring->rx_bi);
  1307. rx_ring->rx_bi = NULL;
  1308. return err;
  1309. }
  1310. /**
  1311. * i40e_release_rx_desc - Store the new tail and head values
  1312. * @rx_ring: ring to bump
  1313. * @val: new head index
  1314. **/
  1315. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  1316. {
  1317. rx_ring->next_to_use = val;
  1318. /* update next to alloc since we have filled the ring */
  1319. rx_ring->next_to_alloc = val;
  1320. /* Force memory writes to complete before letting h/w
  1321. * know there are new descriptors to fetch. (Only
  1322. * applicable for weak-ordered memory model archs,
  1323. * such as IA-64).
  1324. */
  1325. wmb();
  1326. writel(val, rx_ring->tail);
  1327. }
  1328. /**
  1329. * i40e_rx_offset - Return expected offset into page to access data
  1330. * @rx_ring: Ring we are requesting offset of
  1331. *
  1332. * Returns the offset value for ring into the data buffer.
  1333. */
  1334. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  1335. {
  1336. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  1337. }
  1338. /**
  1339. * i40e_alloc_mapped_page - recycle or make a new page
  1340. * @rx_ring: ring to use
  1341. * @bi: rx_buffer struct to modify
  1342. *
  1343. * Returns true if the page was successfully allocated or
  1344. * reused.
  1345. **/
  1346. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  1347. struct i40e_rx_buffer *bi)
  1348. {
  1349. struct page *page = bi->page;
  1350. dma_addr_t dma;
  1351. /* since we are recycling buffers we should seldom need to alloc */
  1352. if (likely(page)) {
  1353. rx_ring->rx_stats.page_reuse_count++;
  1354. return true;
  1355. }
  1356. /* alloc new page for storage */
  1357. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  1358. if (unlikely(!page)) {
  1359. rx_ring->rx_stats.alloc_page_failed++;
  1360. return false;
  1361. }
  1362. /* map page for use */
  1363. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  1364. i40e_rx_pg_size(rx_ring),
  1365. DMA_FROM_DEVICE,
  1366. I40E_RX_DMA_ATTR);
  1367. /* if mapping failed free memory back to system since
  1368. * there isn't much point in holding memory we can't use
  1369. */
  1370. if (dma_mapping_error(rx_ring->dev, dma)) {
  1371. __free_pages(page, i40e_rx_pg_order(rx_ring));
  1372. rx_ring->rx_stats.alloc_page_failed++;
  1373. return false;
  1374. }
  1375. bi->dma = dma;
  1376. bi->page = page;
  1377. bi->page_offset = i40e_rx_offset(rx_ring);
  1378. page_ref_add(page, USHRT_MAX - 1);
  1379. bi->pagecnt_bias = USHRT_MAX;
  1380. return true;
  1381. }
  1382. /**
  1383. * i40e_receive_skb - Send a completed packet up the stack
  1384. * @rx_ring: rx ring in play
  1385. * @skb: packet to send up
  1386. * @vlan_tag: vlan tag for packet
  1387. **/
  1388. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  1389. struct sk_buff *skb, u16 vlan_tag)
  1390. {
  1391. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  1392. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1393. (vlan_tag & VLAN_VID_MASK))
  1394. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1395. napi_gro_receive(&q_vector->napi, skb);
  1396. }
  1397. /**
  1398. * i40e_alloc_rx_buffers - Replace used receive buffers
  1399. * @rx_ring: ring to place buffers on
  1400. * @cleaned_count: number of buffers to replace
  1401. *
  1402. * Returns false if all allocations were successful, true if any fail
  1403. **/
  1404. bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  1405. {
  1406. u16 ntu = rx_ring->next_to_use;
  1407. union i40e_rx_desc *rx_desc;
  1408. struct i40e_rx_buffer *bi;
  1409. /* do nothing if no valid netdev defined */
  1410. if (!rx_ring->netdev || !cleaned_count)
  1411. return false;
  1412. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  1413. bi = &rx_ring->rx_bi[ntu];
  1414. do {
  1415. if (!i40e_alloc_mapped_page(rx_ring, bi))
  1416. goto no_buffers;
  1417. /* sync the buffer for use by the device */
  1418. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1419. bi->page_offset,
  1420. rx_ring->rx_buf_len,
  1421. DMA_FROM_DEVICE);
  1422. /* Refresh the desc even if buffer_addrs didn't change
  1423. * because each write-back erases this info.
  1424. */
  1425. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1426. rx_desc++;
  1427. bi++;
  1428. ntu++;
  1429. if (unlikely(ntu == rx_ring->count)) {
  1430. rx_desc = I40E_RX_DESC(rx_ring, 0);
  1431. bi = rx_ring->rx_bi;
  1432. ntu = 0;
  1433. }
  1434. /* clear the status bits for the next_to_use descriptor */
  1435. rx_desc->wb.qword1.status_error_len = 0;
  1436. cleaned_count--;
  1437. } while (cleaned_count);
  1438. if (rx_ring->next_to_use != ntu)
  1439. i40e_release_rx_desc(rx_ring, ntu);
  1440. return false;
  1441. no_buffers:
  1442. if (rx_ring->next_to_use != ntu)
  1443. i40e_release_rx_desc(rx_ring, ntu);
  1444. /* make sure to come back via polling to try again after
  1445. * allocation failure
  1446. */
  1447. return true;
  1448. }
  1449. /**
  1450. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1451. * @vsi: the VSI we care about
  1452. * @skb: skb currently being received and modified
  1453. * @rx_desc: the receive descriptor
  1454. **/
  1455. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1456. struct sk_buff *skb,
  1457. union i40e_rx_desc *rx_desc)
  1458. {
  1459. struct i40e_rx_ptype_decoded decoded;
  1460. u32 rx_error, rx_status;
  1461. bool ipv4, ipv6;
  1462. u8 ptype;
  1463. u64 qword;
  1464. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1465. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  1466. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1467. I40E_RXD_QW1_ERROR_SHIFT;
  1468. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1469. I40E_RXD_QW1_STATUS_SHIFT;
  1470. decoded = decode_rx_desc_ptype(ptype);
  1471. skb->ip_summed = CHECKSUM_NONE;
  1472. skb_checksum_none_assert(skb);
  1473. /* Rx csum enabled and ip headers found? */
  1474. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1475. return;
  1476. /* did the hardware decode the packet and checksum? */
  1477. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1478. return;
  1479. /* both known and outer_ip must be set for the below code to work */
  1480. if (!(decoded.known && decoded.outer_ip))
  1481. return;
  1482. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1483. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  1484. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1485. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  1486. if (ipv4 &&
  1487. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1488. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1489. goto checksum_fail;
  1490. /* likely incorrect csum if alternate IP extension headers found */
  1491. if (ipv6 &&
  1492. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1493. /* don't increment checksum err here, non-fatal err */
  1494. return;
  1495. /* there was some L4 error, count error and punt packet to the stack */
  1496. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  1497. goto checksum_fail;
  1498. /* handle packets that were not able to be checksummed due
  1499. * to arrival speed, in this case the stack can compute
  1500. * the csum.
  1501. */
  1502. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1503. return;
  1504. /* If there is an outer header present that might contain a checksum
  1505. * we need to bump the checksum level by 1 to reflect the fact that
  1506. * we are indicating we validated the inner checksum.
  1507. */
  1508. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  1509. skb->csum_level = 1;
  1510. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  1511. switch (decoded.inner_prot) {
  1512. case I40E_RX_PTYPE_INNER_PROT_TCP:
  1513. case I40E_RX_PTYPE_INNER_PROT_UDP:
  1514. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  1515. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1516. /* fall though */
  1517. default:
  1518. break;
  1519. }
  1520. return;
  1521. checksum_fail:
  1522. vsi->back->hw_csum_rx_error++;
  1523. }
  1524. /**
  1525. * i40e_ptype_to_htype - get a hash type
  1526. * @ptype: the ptype value from the descriptor
  1527. *
  1528. * Returns a hash type to be used by skb_set_hash
  1529. **/
  1530. static inline int i40e_ptype_to_htype(u8 ptype)
  1531. {
  1532. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1533. if (!decoded.known)
  1534. return PKT_HASH_TYPE_NONE;
  1535. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1536. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1537. return PKT_HASH_TYPE_L4;
  1538. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1539. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1540. return PKT_HASH_TYPE_L3;
  1541. else
  1542. return PKT_HASH_TYPE_L2;
  1543. }
  1544. /**
  1545. * i40e_rx_hash - set the hash value in the skb
  1546. * @ring: descriptor ring
  1547. * @rx_desc: specific descriptor
  1548. **/
  1549. static inline void i40e_rx_hash(struct i40e_ring *ring,
  1550. union i40e_rx_desc *rx_desc,
  1551. struct sk_buff *skb,
  1552. u8 rx_ptype)
  1553. {
  1554. u32 hash;
  1555. const __le64 rss_mask =
  1556. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1557. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1558. if (!(ring->netdev->features & NETIF_F_RXHASH))
  1559. return;
  1560. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  1561. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1562. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  1563. }
  1564. }
  1565. /**
  1566. * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
  1567. * @rx_ring: rx descriptor ring packet is being transacted on
  1568. * @rx_desc: pointer to the EOP Rx descriptor
  1569. * @skb: pointer to current skb being populated
  1570. * @rx_ptype: the packet type decoded by hardware
  1571. *
  1572. * This function checks the ring, descriptor, and packet information in
  1573. * order to populate the hash, checksum, VLAN, protocol, and
  1574. * other fields within the skb.
  1575. **/
  1576. static inline
  1577. void i40e_process_skb_fields(struct i40e_ring *rx_ring,
  1578. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  1579. u8 rx_ptype)
  1580. {
  1581. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1582. u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1583. I40E_RXD_QW1_STATUS_SHIFT;
  1584. u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
  1585. u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1586. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
  1587. if (unlikely(tsynvalid))
  1588. i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
  1589. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1590. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  1591. skb_record_rx_queue(skb, rx_ring->queue_index);
  1592. /* modifies the skb - consumes the enet header */
  1593. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1594. }
  1595. /**
  1596. * i40e_cleanup_headers - Correct empty headers
  1597. * @rx_ring: rx descriptor ring packet is being transacted on
  1598. * @skb: pointer to current skb being fixed
  1599. * @rx_desc: pointer to the EOP Rx descriptor
  1600. *
  1601. * Also address the case where we are pulling data in on pages only
  1602. * and as such no data is present in the skb header.
  1603. *
  1604. * In addition if skb is not at least 60 bytes we need to pad it so that
  1605. * it is large enough to qualify as a valid Ethernet frame.
  1606. *
  1607. * Returns true if an error was encountered and skb was freed.
  1608. **/
  1609. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb,
  1610. union i40e_rx_desc *rx_desc)
  1611. {
  1612. /* XDP packets use error pointer so abort at this point */
  1613. if (IS_ERR(skb))
  1614. return true;
  1615. /* ERR_MASK will only have valid bits if EOP set, and
  1616. * what we are doing here is actually checking
  1617. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1618. * the error field
  1619. */
  1620. if (unlikely(i40e_test_staterr(rx_desc,
  1621. BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1622. dev_kfree_skb_any(skb);
  1623. return true;
  1624. }
  1625. /* if eth_skb_pad returns an error the skb was freed */
  1626. if (eth_skb_pad(skb))
  1627. return true;
  1628. return false;
  1629. }
  1630. /**
  1631. * i40e_page_is_reusable - check if any reuse is possible
  1632. * @page: page struct to check
  1633. *
  1634. * A page is not reusable if it was allocated under low memory
  1635. * conditions, or it's not in the same NUMA node as this CPU.
  1636. */
  1637. static inline bool i40e_page_is_reusable(struct page *page)
  1638. {
  1639. return (page_to_nid(page) == numa_mem_id()) &&
  1640. !page_is_pfmemalloc(page);
  1641. }
  1642. /**
  1643. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1644. * the adapter for another receive
  1645. *
  1646. * @rx_buffer: buffer containing the page
  1647. *
  1648. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1649. * an unused region in the page.
  1650. *
  1651. * For small pages, @truesize will be a constant value, half the size
  1652. * of the memory at page. We'll attempt to alternate between high and
  1653. * low halves of the page, with one half ready for use by the hardware
  1654. * and the other half being consumed by the stack. We use the page
  1655. * ref count to determine whether the stack has finished consuming the
  1656. * portion of this page that was passed up with a previous packet. If
  1657. * the page ref count is >1, we'll assume the "other" half page is
  1658. * still busy, and this page cannot be reused.
  1659. *
  1660. * For larger pages, @truesize will be the actual space used by the
  1661. * received packet (adjusted upward to an even multiple of the cache
  1662. * line size). This will advance through the page by the amount
  1663. * actually consumed by the received packets while there is still
  1664. * space for a buffer. Each region of larger pages will be used at
  1665. * most once, after which the page will not be reused.
  1666. *
  1667. * In either case, if the page is reusable its refcount is increased.
  1668. **/
  1669. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1670. {
  1671. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1672. struct page *page = rx_buffer->page;
  1673. /* Is any reuse possible? */
  1674. if (unlikely(!i40e_page_is_reusable(page)))
  1675. return false;
  1676. #if (PAGE_SIZE < 8192)
  1677. /* if we are only owner of page we can reuse it */
  1678. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1679. return false;
  1680. #else
  1681. #define I40E_LAST_OFFSET \
  1682. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1683. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1684. return false;
  1685. #endif
  1686. /* If we have drained the page fragment pool we need to update
  1687. * the pagecnt_bias and page count so that we fully restock the
  1688. * number of references the driver holds.
  1689. */
  1690. if (unlikely(pagecnt_bias == 1)) {
  1691. page_ref_add(page, USHRT_MAX - 1);
  1692. rx_buffer->pagecnt_bias = USHRT_MAX;
  1693. }
  1694. return true;
  1695. }
  1696. /**
  1697. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1698. * @rx_ring: rx descriptor ring to transact packets on
  1699. * @rx_buffer: buffer containing page to add
  1700. * @skb: sk_buff to place the data into
  1701. * @size: packet length from rx_desc
  1702. *
  1703. * This function will add the data contained in rx_buffer->page to the skb.
  1704. * It will just attach the page as a frag to the skb.
  1705. *
  1706. * The function will then update the page offset.
  1707. **/
  1708. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1709. struct i40e_rx_buffer *rx_buffer,
  1710. struct sk_buff *skb,
  1711. unsigned int size)
  1712. {
  1713. #if (PAGE_SIZE < 8192)
  1714. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1715. #else
  1716. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1717. #endif
  1718. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1719. rx_buffer->page_offset, size, truesize);
  1720. /* page is being used so we must update the page offset */
  1721. #if (PAGE_SIZE < 8192)
  1722. rx_buffer->page_offset ^= truesize;
  1723. #else
  1724. rx_buffer->page_offset += truesize;
  1725. #endif
  1726. }
  1727. /**
  1728. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1729. * @rx_ring: rx descriptor ring to transact packets on
  1730. * @size: size of buffer to add to skb
  1731. *
  1732. * This function will pull an Rx buffer from the ring and synchronize it
  1733. * for use by the CPU.
  1734. */
  1735. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1736. const unsigned int size)
  1737. {
  1738. struct i40e_rx_buffer *rx_buffer;
  1739. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1740. prefetchw(rx_buffer->page);
  1741. /* we are reusing so sync this buffer for CPU use */
  1742. dma_sync_single_range_for_cpu(rx_ring->dev,
  1743. rx_buffer->dma,
  1744. rx_buffer->page_offset,
  1745. size,
  1746. DMA_FROM_DEVICE);
  1747. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1748. rx_buffer->pagecnt_bias--;
  1749. return rx_buffer;
  1750. }
  1751. /**
  1752. * i40e_construct_skb - Allocate skb and populate it
  1753. * @rx_ring: rx descriptor ring to transact packets on
  1754. * @rx_buffer: rx buffer to pull data from
  1755. * @xdp: xdp_buff pointing to the data
  1756. *
  1757. * This function allocates an skb. It then populates it with the page
  1758. * data from the current receive descriptor, taking care to set up the
  1759. * skb correctly.
  1760. */
  1761. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1762. struct i40e_rx_buffer *rx_buffer,
  1763. struct xdp_buff *xdp)
  1764. {
  1765. unsigned int size = xdp->data_end - xdp->data;
  1766. #if (PAGE_SIZE < 8192)
  1767. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1768. #else
  1769. unsigned int truesize = SKB_DATA_ALIGN(size);
  1770. #endif
  1771. unsigned int headlen;
  1772. struct sk_buff *skb;
  1773. /* prefetch first cache line of first page */
  1774. prefetch(xdp->data);
  1775. #if L1_CACHE_BYTES < 128
  1776. prefetch(xdp->data + L1_CACHE_BYTES);
  1777. #endif
  1778. /* allocate a skb to store the frags */
  1779. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1780. I40E_RX_HDR_SIZE,
  1781. GFP_ATOMIC | __GFP_NOWARN);
  1782. if (unlikely(!skb))
  1783. return NULL;
  1784. /* Determine available headroom for copy */
  1785. headlen = size;
  1786. if (headlen > I40E_RX_HDR_SIZE)
  1787. headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
  1788. /* align pull length to size of long to optimize memcpy performance */
  1789. memcpy(__skb_put(skb, headlen), xdp->data,
  1790. ALIGN(headlen, sizeof(long)));
  1791. /* update all of the pointers */
  1792. size -= headlen;
  1793. if (size) {
  1794. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1795. rx_buffer->page_offset + headlen,
  1796. size, truesize);
  1797. /* buffer is used by skb, update page_offset */
  1798. #if (PAGE_SIZE < 8192)
  1799. rx_buffer->page_offset ^= truesize;
  1800. #else
  1801. rx_buffer->page_offset += truesize;
  1802. #endif
  1803. } else {
  1804. /* buffer is unused, reset bias back to rx_buffer */
  1805. rx_buffer->pagecnt_bias++;
  1806. }
  1807. return skb;
  1808. }
  1809. /**
  1810. * i40e_build_skb - Build skb around an existing buffer
  1811. * @rx_ring: Rx descriptor ring to transact packets on
  1812. * @rx_buffer: Rx buffer to pull data from
  1813. * @xdp: xdp_buff pointing to the data
  1814. *
  1815. * This function builds an skb around an existing Rx buffer, taking care
  1816. * to set up the skb correctly and avoid any memcpy overhead.
  1817. */
  1818. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1819. struct i40e_rx_buffer *rx_buffer,
  1820. struct xdp_buff *xdp)
  1821. {
  1822. unsigned int size = xdp->data_end - xdp->data;
  1823. #if (PAGE_SIZE < 8192)
  1824. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1825. #else
  1826. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1827. SKB_DATA_ALIGN(I40E_SKB_PAD + size);
  1828. #endif
  1829. struct sk_buff *skb;
  1830. /* prefetch first cache line of first page */
  1831. prefetch(xdp->data);
  1832. #if L1_CACHE_BYTES < 128
  1833. prefetch(xdp->data + L1_CACHE_BYTES);
  1834. #endif
  1835. /* build an skb around the page buffer */
  1836. skb = build_skb(xdp->data_hard_start, truesize);
  1837. if (unlikely(!skb))
  1838. return NULL;
  1839. /* update pointers within the skb to store the data */
  1840. skb_reserve(skb, I40E_SKB_PAD);
  1841. __skb_put(skb, size);
  1842. /* buffer is used by skb, update page_offset */
  1843. #if (PAGE_SIZE < 8192)
  1844. rx_buffer->page_offset ^= truesize;
  1845. #else
  1846. rx_buffer->page_offset += truesize;
  1847. #endif
  1848. return skb;
  1849. }
  1850. /**
  1851. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1852. * @rx_ring: rx descriptor ring to transact packets on
  1853. * @rx_buffer: rx buffer to pull data from
  1854. *
  1855. * This function will clean up the contents of the rx_buffer. It will
  1856. * either recycle the buffer or unmap it and free the associated resources.
  1857. */
  1858. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1859. struct i40e_rx_buffer *rx_buffer)
  1860. {
  1861. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1862. /* hand second half of page back to the ring */
  1863. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1864. rx_ring->rx_stats.page_reuse_count++;
  1865. } else {
  1866. /* we are not reusing the buffer so unmap it */
  1867. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1868. i40e_rx_pg_size(rx_ring),
  1869. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1870. __page_frag_cache_drain(rx_buffer->page,
  1871. rx_buffer->pagecnt_bias);
  1872. }
  1873. /* clear contents of buffer_info */
  1874. rx_buffer->page = NULL;
  1875. }
  1876. /**
  1877. * i40e_is_non_eop - process handling of non-EOP buffers
  1878. * @rx_ring: Rx ring being processed
  1879. * @rx_desc: Rx descriptor for current buffer
  1880. * @skb: Current socket buffer containing buffer in progress
  1881. *
  1882. * This function updates next to clean. If the buffer is an EOP buffer
  1883. * this function exits returning false, otherwise it will place the
  1884. * sk_buff in the next buffer to be chained and return true indicating
  1885. * that this is in fact a non-EOP buffer.
  1886. **/
  1887. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1888. union i40e_rx_desc *rx_desc,
  1889. struct sk_buff *skb)
  1890. {
  1891. u32 ntc = rx_ring->next_to_clean + 1;
  1892. /* fetch, update, and store next to clean */
  1893. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1894. rx_ring->next_to_clean = ntc;
  1895. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1896. /* if we are the last buffer then there is nothing else to do */
  1897. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1898. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1899. return false;
  1900. rx_ring->rx_stats.non_eop_descs++;
  1901. return true;
  1902. }
  1903. #define I40E_XDP_PASS 0
  1904. #define I40E_XDP_CONSUMED 1
  1905. #define I40E_XDP_TX 2
  1906. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  1907. struct i40e_ring *xdp_ring);
  1908. /**
  1909. * i40e_run_xdp - run an XDP program
  1910. * @rx_ring: Rx ring being processed
  1911. * @xdp: XDP buffer containing the frame
  1912. **/
  1913. static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring,
  1914. struct xdp_buff *xdp)
  1915. {
  1916. int err, result = I40E_XDP_PASS;
  1917. struct i40e_ring *xdp_ring;
  1918. struct bpf_prog *xdp_prog;
  1919. u32 act;
  1920. rcu_read_lock();
  1921. xdp_prog = READ_ONCE(rx_ring->xdp_prog);
  1922. if (!xdp_prog)
  1923. goto xdp_out;
  1924. act = bpf_prog_run_xdp(xdp_prog, xdp);
  1925. switch (act) {
  1926. case XDP_PASS:
  1927. break;
  1928. case XDP_TX:
  1929. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1930. result = i40e_xmit_xdp_ring(xdp, xdp_ring);
  1931. break;
  1932. case XDP_REDIRECT:
  1933. err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
  1934. result = !err ? I40E_XDP_TX : I40E_XDP_CONSUMED;
  1935. break;
  1936. default:
  1937. bpf_warn_invalid_xdp_action(act);
  1938. case XDP_ABORTED:
  1939. trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
  1940. /* fallthrough -- handle aborts by dropping packet */
  1941. case XDP_DROP:
  1942. result = I40E_XDP_CONSUMED;
  1943. break;
  1944. }
  1945. xdp_out:
  1946. rcu_read_unlock();
  1947. return ERR_PTR(-result);
  1948. }
  1949. /**
  1950. * i40e_rx_buffer_flip - adjusted rx_buffer to point to an unused region
  1951. * @rx_ring: Rx ring
  1952. * @rx_buffer: Rx buffer to adjust
  1953. * @size: Size of adjustment
  1954. **/
  1955. static void i40e_rx_buffer_flip(struct i40e_ring *rx_ring,
  1956. struct i40e_rx_buffer *rx_buffer,
  1957. unsigned int size)
  1958. {
  1959. #if (PAGE_SIZE < 8192)
  1960. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1961. rx_buffer->page_offset ^= truesize;
  1962. #else
  1963. unsigned int truesize = SKB_DATA_ALIGN(i40e_rx_offset(rx_ring) + size);
  1964. rx_buffer->page_offset += truesize;
  1965. #endif
  1966. }
  1967. static inline void i40e_xdp_ring_update_tail(struct i40e_ring *xdp_ring)
  1968. {
  1969. /* Force memory writes to complete before letting h/w
  1970. * know there are new descriptors to fetch.
  1971. */
  1972. wmb();
  1973. writel_relaxed(xdp_ring->next_to_use, xdp_ring->tail);
  1974. }
  1975. /**
  1976. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1977. * @rx_ring: rx descriptor ring to transact packets on
  1978. * @budget: Total limit on number of packets to process
  1979. *
  1980. * This function provides a "bounce buffer" approach to Rx interrupt
  1981. * processing. The advantage to this is that on systems that have
  1982. * expensive overhead for IOMMU access this provides a means of avoiding
  1983. * it by maintaining the mapping of the page to the system.
  1984. *
  1985. * Returns amount of work completed
  1986. **/
  1987. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1988. {
  1989. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1990. struct sk_buff *skb = rx_ring->skb;
  1991. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1992. bool failure = false, xdp_xmit = false;
  1993. struct xdp_buff xdp;
  1994. xdp.rxq = &rx_ring->xdp_rxq;
  1995. while (likely(total_rx_packets < (unsigned int)budget)) {
  1996. struct i40e_rx_buffer *rx_buffer;
  1997. union i40e_rx_desc *rx_desc;
  1998. unsigned int size;
  1999. u16 vlan_tag;
  2000. u8 rx_ptype;
  2001. u64 qword;
  2002. /* return some buffers to hardware, one at a time is too slow */
  2003. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  2004. failure = failure ||
  2005. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  2006. cleaned_count = 0;
  2007. }
  2008. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  2009. /* status_error_len will always be zero for unused descriptors
  2010. * because it's cleared in cleanup, and overlaps with hdr_addr
  2011. * which is always zero because packet split isn't used, if the
  2012. * hardware wrote DD then the length will be non-zero
  2013. */
  2014. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  2015. /* This memory barrier is needed to keep us from reading
  2016. * any other fields out of the rx_desc until we have
  2017. * verified the descriptor has been written back.
  2018. */
  2019. dma_rmb();
  2020. if (unlikely(i40e_rx_is_programming_status(qword))) {
  2021. i40e_clean_programming_status(rx_ring, rx_desc, qword);
  2022. cleaned_count++;
  2023. continue;
  2024. }
  2025. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  2026. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  2027. if (!size)
  2028. break;
  2029. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  2030. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  2031. /* retrieve a buffer from the ring */
  2032. if (!skb) {
  2033. xdp.data = page_address(rx_buffer->page) +
  2034. rx_buffer->page_offset;
  2035. xdp_set_data_meta_invalid(&xdp);
  2036. xdp.data_hard_start = xdp.data -
  2037. i40e_rx_offset(rx_ring);
  2038. xdp.data_end = xdp.data + size;
  2039. skb = i40e_run_xdp(rx_ring, &xdp);
  2040. }
  2041. if (IS_ERR(skb)) {
  2042. if (PTR_ERR(skb) == -I40E_XDP_TX) {
  2043. xdp_xmit = true;
  2044. i40e_rx_buffer_flip(rx_ring, rx_buffer, size);
  2045. } else {
  2046. rx_buffer->pagecnt_bias++;
  2047. }
  2048. total_rx_bytes += size;
  2049. total_rx_packets++;
  2050. } else if (skb) {
  2051. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  2052. } else if (ring_uses_build_skb(rx_ring)) {
  2053. skb = i40e_build_skb(rx_ring, rx_buffer, &xdp);
  2054. } else {
  2055. skb = i40e_construct_skb(rx_ring, rx_buffer, &xdp);
  2056. }
  2057. /* exit if we failed to retrieve a buffer */
  2058. if (!skb) {
  2059. rx_ring->rx_stats.alloc_buff_failed++;
  2060. rx_buffer->pagecnt_bias++;
  2061. break;
  2062. }
  2063. i40e_put_rx_buffer(rx_ring, rx_buffer);
  2064. cleaned_count++;
  2065. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  2066. continue;
  2067. if (i40e_cleanup_headers(rx_ring, skb, rx_desc)) {
  2068. skb = NULL;
  2069. continue;
  2070. }
  2071. /* probably a little skewed due to removing CRC */
  2072. total_rx_bytes += skb->len;
  2073. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  2074. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  2075. I40E_RXD_QW1_PTYPE_SHIFT;
  2076. /* populate checksum, VLAN, and protocol */
  2077. i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  2078. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  2079. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  2080. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  2081. i40e_receive_skb(rx_ring, skb, vlan_tag);
  2082. skb = NULL;
  2083. /* update budget accounting */
  2084. total_rx_packets++;
  2085. }
  2086. if (xdp_xmit) {
  2087. struct i40e_ring *xdp_ring =
  2088. rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  2089. i40e_xdp_ring_update_tail(xdp_ring);
  2090. xdp_do_flush_map();
  2091. }
  2092. rx_ring->skb = skb;
  2093. u64_stats_update_begin(&rx_ring->syncp);
  2094. rx_ring->stats.packets += total_rx_packets;
  2095. rx_ring->stats.bytes += total_rx_bytes;
  2096. u64_stats_update_end(&rx_ring->syncp);
  2097. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  2098. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  2099. /* guarantee a trip back through this routine if there was a failure */
  2100. return failure ? budget : (int)total_rx_packets;
  2101. }
  2102. static inline u32 i40e_buildreg_itr(const int type, u16 itr)
  2103. {
  2104. u32 val;
  2105. /* We don't bother with setting the CLEARPBA bit as the data sheet
  2106. * points out doing so is "meaningless since it was already
  2107. * auto-cleared". The auto-clearing happens when the interrupt is
  2108. * asserted.
  2109. *
  2110. * Hardware errata 28 for also indicates that writing to a
  2111. * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
  2112. * an event in the PBA anyway so we need to rely on the automask
  2113. * to hold pending events for us until the interrupt is re-enabled
  2114. *
  2115. * The itr value is reported in microseconds, and the register
  2116. * value is recorded in 2 microsecond units. For this reason we
  2117. * only need to shift by the interval shift - 1 instead of the
  2118. * full value.
  2119. */
  2120. itr &= I40E_ITR_MASK;
  2121. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  2122. (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
  2123. (itr << (I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT - 1));
  2124. return val;
  2125. }
  2126. /* a small macro to shorten up some long lines */
  2127. #define INTREG I40E_PFINT_DYN_CTLN
  2128. /* The act of updating the ITR will cause it to immediately trigger. In order
  2129. * to prevent this from throwing off adaptive update statistics we defer the
  2130. * update so that it can only happen so often. So after either Tx or Rx are
  2131. * updated we make the adaptive scheme wait until either the ITR completely
  2132. * expires via the next_update expiration or we have been through at least
  2133. * 3 interrupts.
  2134. */
  2135. #define ITR_COUNTDOWN_START 3
  2136. /**
  2137. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  2138. * @vsi: the VSI we care about
  2139. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  2140. *
  2141. **/
  2142. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  2143. struct i40e_q_vector *q_vector)
  2144. {
  2145. struct i40e_hw *hw = &vsi->back->hw;
  2146. u32 intval;
  2147. /* If we don't have MSIX, then we only need to re-enable icr0 */
  2148. if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED)) {
  2149. i40e_irq_dynamic_enable_icr0(vsi->back);
  2150. return;
  2151. }
  2152. /* These will do nothing if dynamic updates are not enabled */
  2153. i40e_update_itr(q_vector, &q_vector->tx);
  2154. i40e_update_itr(q_vector, &q_vector->rx);
  2155. /* This block of logic allows us to get away with only updating
  2156. * one ITR value with each interrupt. The idea is to perform a
  2157. * pseudo-lazy update with the following criteria.
  2158. *
  2159. * 1. Rx is given higher priority than Tx if both are in same state
  2160. * 2. If we must reduce an ITR that is given highest priority.
  2161. * 3. We then give priority to increasing ITR based on amount.
  2162. */
  2163. if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
  2164. /* Rx ITR needs to be reduced, this is highest priority */
  2165. intval = i40e_buildreg_itr(I40E_RX_ITR,
  2166. q_vector->rx.target_itr);
  2167. q_vector->rx.current_itr = q_vector->rx.target_itr;
  2168. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2169. } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
  2170. ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
  2171. (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
  2172. /* Tx ITR needs to be reduced, this is second priority
  2173. * Tx ITR needs to be increased more than Rx, fourth priority
  2174. */
  2175. intval = i40e_buildreg_itr(I40E_TX_ITR,
  2176. q_vector->tx.target_itr);
  2177. q_vector->tx.current_itr = q_vector->tx.target_itr;
  2178. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2179. } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
  2180. /* Rx ITR needs to be increased, third priority */
  2181. intval = i40e_buildreg_itr(I40E_RX_ITR,
  2182. q_vector->rx.target_itr);
  2183. q_vector->rx.current_itr = q_vector->rx.target_itr;
  2184. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2185. } else {
  2186. /* No ITR update, lowest priority */
  2187. intval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  2188. if (q_vector->itr_countdown)
  2189. q_vector->itr_countdown--;
  2190. }
  2191. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  2192. wr32(hw, INTREG(q_vector->reg_idx), intval);
  2193. }
  2194. /**
  2195. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  2196. * @napi: napi struct with our devices info in it
  2197. * @budget: amount of work driver is allowed to do this pass, in packets
  2198. *
  2199. * This function will clean all queues associated with a q_vector.
  2200. *
  2201. * Returns the amount of work done
  2202. **/
  2203. int i40e_napi_poll(struct napi_struct *napi, int budget)
  2204. {
  2205. struct i40e_q_vector *q_vector =
  2206. container_of(napi, struct i40e_q_vector, napi);
  2207. struct i40e_vsi *vsi = q_vector->vsi;
  2208. struct i40e_ring *ring;
  2209. bool clean_complete = true;
  2210. bool arm_wb = false;
  2211. int budget_per_ring;
  2212. int work_done = 0;
  2213. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  2214. napi_complete(napi);
  2215. return 0;
  2216. }
  2217. /* Since the actual Tx work is minimal, we can give the Tx a larger
  2218. * budget and be more aggressive about cleaning up the Tx descriptors.
  2219. */
  2220. i40e_for_each_ring(ring, q_vector->tx) {
  2221. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  2222. clean_complete = false;
  2223. continue;
  2224. }
  2225. arm_wb |= ring->arm_wb;
  2226. ring->arm_wb = false;
  2227. }
  2228. /* Handle case where we are called by netpoll with a budget of 0 */
  2229. if (budget <= 0)
  2230. goto tx_only;
  2231. /* We attempt to distribute budget to each Rx queue fairly, but don't
  2232. * allow the budget to go below 1 because that would exit polling early.
  2233. */
  2234. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  2235. i40e_for_each_ring(ring, q_vector->rx) {
  2236. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  2237. work_done += cleaned;
  2238. /* if we clean as many as budgeted, we must not be done */
  2239. if (cleaned >= budget_per_ring)
  2240. clean_complete = false;
  2241. }
  2242. /* If work not completed, return budget and polling will return */
  2243. if (!clean_complete) {
  2244. int cpu_id = smp_processor_id();
  2245. /* It is possible that the interrupt affinity has changed but,
  2246. * if the cpu is pegged at 100%, polling will never exit while
  2247. * traffic continues and the interrupt will be stuck on this
  2248. * cpu. We check to make sure affinity is correct before we
  2249. * continue to poll, otherwise we must stop polling so the
  2250. * interrupt can move to the correct cpu.
  2251. */
  2252. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  2253. /* Tell napi that we are done polling */
  2254. napi_complete_done(napi, work_done);
  2255. /* Force an interrupt */
  2256. i40e_force_wb(vsi, q_vector);
  2257. /* Return budget-1 so that polling stops */
  2258. return budget - 1;
  2259. }
  2260. tx_only:
  2261. if (arm_wb) {
  2262. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  2263. i40e_enable_wb_on_itr(vsi, q_vector);
  2264. }
  2265. return budget;
  2266. }
  2267. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  2268. q_vector->arm_wb_state = false;
  2269. /* Work is done so exit the polling mode and re-enable the interrupt */
  2270. napi_complete_done(napi, work_done);
  2271. i40e_update_enable_itr(vsi, q_vector);
  2272. return min(work_done, budget - 1);
  2273. }
  2274. /**
  2275. * i40e_atr - Add a Flow Director ATR filter
  2276. * @tx_ring: ring to add programming descriptor to
  2277. * @skb: send buffer
  2278. * @tx_flags: send tx flags
  2279. **/
  2280. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2281. u32 tx_flags)
  2282. {
  2283. struct i40e_filter_program_desc *fdir_desc;
  2284. struct i40e_pf *pf = tx_ring->vsi->back;
  2285. union {
  2286. unsigned char *network;
  2287. struct iphdr *ipv4;
  2288. struct ipv6hdr *ipv6;
  2289. } hdr;
  2290. struct tcphdr *th;
  2291. unsigned int hlen;
  2292. u32 flex_ptype, dtype_cmd;
  2293. int l4_proto;
  2294. u16 i;
  2295. /* make sure ATR is enabled */
  2296. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  2297. return;
  2298. if (test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  2299. return;
  2300. /* if sampling is disabled do nothing */
  2301. if (!tx_ring->atr_sample_rate)
  2302. return;
  2303. /* Currently only IPv4/IPv6 with TCP is supported */
  2304. if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
  2305. return;
  2306. /* snag network header to get L4 type and address */
  2307. hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
  2308. skb_inner_network_header(skb) : skb_network_header(skb);
  2309. /* Note: tx_flags gets modified to reflect inner protocols in
  2310. * tx_enable_csum function if encap is enabled.
  2311. */
  2312. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  2313. /* access ihl as u8 to avoid unaligned access on ia64 */
  2314. hlen = (hdr.network[0] & 0x0F) << 2;
  2315. l4_proto = hdr.ipv4->protocol;
  2316. } else {
  2317. /* find the start of the innermost ipv6 header */
  2318. unsigned int inner_hlen = hdr.network - skb->data;
  2319. unsigned int h_offset = inner_hlen;
  2320. /* this function updates h_offset to the end of the header */
  2321. l4_proto =
  2322. ipv6_find_hdr(skb, &h_offset, IPPROTO_TCP, NULL, NULL);
  2323. /* hlen will contain our best estimate of the tcp header */
  2324. hlen = h_offset - inner_hlen;
  2325. }
  2326. if (l4_proto != IPPROTO_TCP)
  2327. return;
  2328. th = (struct tcphdr *)(hdr.network + hlen);
  2329. /* Due to lack of space, no more new filters can be programmed */
  2330. if (th->syn && test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  2331. return;
  2332. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) {
  2333. /* HW ATR eviction will take care of removing filters on FIN
  2334. * and RST packets.
  2335. */
  2336. if (th->fin || th->rst)
  2337. return;
  2338. }
  2339. tx_ring->atr_count++;
  2340. /* sample on all syn/fin/rst packets or once every atr sample rate */
  2341. if (!th->fin &&
  2342. !th->syn &&
  2343. !th->rst &&
  2344. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  2345. return;
  2346. tx_ring->atr_count = 0;
  2347. /* grab the next descriptor */
  2348. i = tx_ring->next_to_use;
  2349. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  2350. i++;
  2351. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2352. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  2353. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  2354. flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
  2355. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  2356. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  2357. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  2358. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  2359. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  2360. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  2361. dtype_cmd |= (th->fin || th->rst) ?
  2362. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  2363. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  2364. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  2365. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  2366. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  2367. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  2368. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  2369. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  2370. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  2371. if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
  2372. dtype_cmd |=
  2373. ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
  2374. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2375. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2376. else
  2377. dtype_cmd |=
  2378. ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
  2379. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2380. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2381. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED)
  2382. dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
  2383. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  2384. fdir_desc->rsvd = cpu_to_le32(0);
  2385. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  2386. fdir_desc->fd_id = cpu_to_le32(0);
  2387. }
  2388. /**
  2389. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  2390. * @skb: send buffer
  2391. * @tx_ring: ring to send buffer on
  2392. * @flags: the tx flags to be set
  2393. *
  2394. * Checks the skb and set up correspondingly several generic transmit flags
  2395. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  2396. *
  2397. * Returns error code indicate the frame should be dropped upon error and the
  2398. * otherwise returns 0 to indicate the flags has been set properly.
  2399. **/
  2400. static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  2401. struct i40e_ring *tx_ring,
  2402. u32 *flags)
  2403. {
  2404. __be16 protocol = skb->protocol;
  2405. u32 tx_flags = 0;
  2406. if (protocol == htons(ETH_P_8021Q) &&
  2407. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  2408. /* When HW VLAN acceleration is turned off by the user the
  2409. * stack sets the protocol to 8021q so that the driver
  2410. * can take any steps required to support the SW only
  2411. * VLAN handling. In our case the driver doesn't need
  2412. * to take any further steps so just set the protocol
  2413. * to the encapsulated ethertype.
  2414. */
  2415. skb->protocol = vlan_get_protocol(skb);
  2416. goto out;
  2417. }
  2418. /* if we have a HW VLAN tag being added, default to the HW one */
  2419. if (skb_vlan_tag_present(skb)) {
  2420. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  2421. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2422. /* else if it is a SW VLAN, check the next protocol and store the tag */
  2423. } else if (protocol == htons(ETH_P_8021Q)) {
  2424. struct vlan_hdr *vhdr, _vhdr;
  2425. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  2426. if (!vhdr)
  2427. return -EINVAL;
  2428. protocol = vhdr->h_vlan_encapsulated_proto;
  2429. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  2430. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  2431. }
  2432. if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2433. goto out;
  2434. /* Insert 802.1p priority into VLAN header */
  2435. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  2436. (skb->priority != TC_PRIO_CONTROL)) {
  2437. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  2438. tx_flags |= (skb->priority & 0x7) <<
  2439. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  2440. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  2441. struct vlan_ethhdr *vhdr;
  2442. int rc;
  2443. rc = skb_cow_head(skb, 0);
  2444. if (rc < 0)
  2445. return rc;
  2446. vhdr = (struct vlan_ethhdr *)skb->data;
  2447. vhdr->h_vlan_TCI = htons(tx_flags >>
  2448. I40E_TX_FLAGS_VLAN_SHIFT);
  2449. } else {
  2450. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2451. }
  2452. }
  2453. out:
  2454. *flags = tx_flags;
  2455. return 0;
  2456. }
  2457. /**
  2458. * i40e_tso - set up the tso context descriptor
  2459. * @first: pointer to first Tx buffer for xmit
  2460. * @hdr_len: ptr to the size of the packet header
  2461. * @cd_type_cmd_tso_mss: Quad Word 1
  2462. *
  2463. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  2464. **/
  2465. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  2466. u64 *cd_type_cmd_tso_mss)
  2467. {
  2468. struct sk_buff *skb = first->skb;
  2469. u64 cd_cmd, cd_tso_len, cd_mss;
  2470. union {
  2471. struct iphdr *v4;
  2472. struct ipv6hdr *v6;
  2473. unsigned char *hdr;
  2474. } ip;
  2475. union {
  2476. struct tcphdr *tcp;
  2477. struct udphdr *udp;
  2478. unsigned char *hdr;
  2479. } l4;
  2480. u32 paylen, l4_offset;
  2481. u16 gso_segs, gso_size;
  2482. int err;
  2483. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2484. return 0;
  2485. if (!skb_is_gso(skb))
  2486. return 0;
  2487. err = skb_cow_head(skb, 0);
  2488. if (err < 0)
  2489. return err;
  2490. ip.hdr = skb_network_header(skb);
  2491. l4.hdr = skb_transport_header(skb);
  2492. /* initialize outer IP header fields */
  2493. if (ip.v4->version == 4) {
  2494. ip.v4->tot_len = 0;
  2495. ip.v4->check = 0;
  2496. } else {
  2497. ip.v6->payload_len = 0;
  2498. }
  2499. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  2500. SKB_GSO_GRE_CSUM |
  2501. SKB_GSO_IPXIP4 |
  2502. SKB_GSO_IPXIP6 |
  2503. SKB_GSO_UDP_TUNNEL |
  2504. SKB_GSO_UDP_TUNNEL_CSUM)) {
  2505. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2506. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  2507. l4.udp->len = 0;
  2508. /* determine offset of outer transport header */
  2509. l4_offset = l4.hdr - skb->data;
  2510. /* remove payload length from outer checksum */
  2511. paylen = skb->len - l4_offset;
  2512. csum_replace_by_diff(&l4.udp->check,
  2513. (__force __wsum)htonl(paylen));
  2514. }
  2515. /* reset pointers to inner headers */
  2516. ip.hdr = skb_inner_network_header(skb);
  2517. l4.hdr = skb_inner_transport_header(skb);
  2518. /* initialize inner IP header fields */
  2519. if (ip.v4->version == 4) {
  2520. ip.v4->tot_len = 0;
  2521. ip.v4->check = 0;
  2522. } else {
  2523. ip.v6->payload_len = 0;
  2524. }
  2525. }
  2526. /* determine offset of inner transport header */
  2527. l4_offset = l4.hdr - skb->data;
  2528. /* remove payload length from inner checksum */
  2529. paylen = skb->len - l4_offset;
  2530. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  2531. /* compute length of segmentation header */
  2532. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2533. /* pull values out of skb_shinfo */
  2534. gso_size = skb_shinfo(skb)->gso_size;
  2535. gso_segs = skb_shinfo(skb)->gso_segs;
  2536. /* update GSO size and bytecount with header size */
  2537. first->gso_segs = gso_segs;
  2538. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2539. /* find the field values */
  2540. cd_cmd = I40E_TX_CTX_DESC_TSO;
  2541. cd_tso_len = skb->len - *hdr_len;
  2542. cd_mss = gso_size;
  2543. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  2544. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  2545. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  2546. return 1;
  2547. }
  2548. /**
  2549. * i40e_tsyn - set up the tsyn context descriptor
  2550. * @tx_ring: ptr to the ring to send
  2551. * @skb: ptr to the skb we're sending
  2552. * @tx_flags: the collected send information
  2553. * @cd_type_cmd_tso_mss: Quad Word 1
  2554. *
  2555. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  2556. **/
  2557. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2558. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  2559. {
  2560. struct i40e_pf *pf;
  2561. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  2562. return 0;
  2563. /* Tx timestamps cannot be sampled when doing TSO */
  2564. if (tx_flags & I40E_TX_FLAGS_TSO)
  2565. return 0;
  2566. /* only timestamp the outbound packet if the user has requested it and
  2567. * we are not already transmitting a packet to be timestamped
  2568. */
  2569. pf = i40e_netdev_to_pf(tx_ring->netdev);
  2570. if (!(pf->flags & I40E_FLAG_PTP))
  2571. return 0;
  2572. if (pf->ptp_tx &&
  2573. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
  2574. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2575. pf->ptp_tx_start = jiffies;
  2576. pf->ptp_tx_skb = skb_get(skb);
  2577. } else {
  2578. pf->tx_hwtstamp_skipped++;
  2579. return 0;
  2580. }
  2581. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  2582. I40E_TXD_CTX_QW1_CMD_SHIFT;
  2583. return 1;
  2584. }
  2585. /**
  2586. * i40e_tx_enable_csum - Enable Tx checksum offloads
  2587. * @skb: send buffer
  2588. * @tx_flags: pointer to Tx flags currently set
  2589. * @td_cmd: Tx descriptor command bits to set
  2590. * @td_offset: Tx descriptor header offsets to set
  2591. * @tx_ring: Tx descriptor ring
  2592. * @cd_tunneling: ptr to context desc bits
  2593. **/
  2594. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  2595. u32 *td_cmd, u32 *td_offset,
  2596. struct i40e_ring *tx_ring,
  2597. u32 *cd_tunneling)
  2598. {
  2599. union {
  2600. struct iphdr *v4;
  2601. struct ipv6hdr *v6;
  2602. unsigned char *hdr;
  2603. } ip;
  2604. union {
  2605. struct tcphdr *tcp;
  2606. struct udphdr *udp;
  2607. unsigned char *hdr;
  2608. } l4;
  2609. unsigned char *exthdr;
  2610. u32 offset, cmd = 0;
  2611. __be16 frag_off;
  2612. u8 l4_proto = 0;
  2613. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2614. return 0;
  2615. ip.hdr = skb_network_header(skb);
  2616. l4.hdr = skb_transport_header(skb);
  2617. /* compute outer L2 header size */
  2618. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  2619. if (skb->encapsulation) {
  2620. u32 tunnel = 0;
  2621. /* define outer network header type */
  2622. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2623. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2624. I40E_TX_CTX_EXT_IP_IPV4 :
  2625. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  2626. l4_proto = ip.v4->protocol;
  2627. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2628. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  2629. exthdr = ip.hdr + sizeof(*ip.v6);
  2630. l4_proto = ip.v6->nexthdr;
  2631. if (l4.hdr != exthdr)
  2632. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2633. &l4_proto, &frag_off);
  2634. }
  2635. /* define outer transport */
  2636. switch (l4_proto) {
  2637. case IPPROTO_UDP:
  2638. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  2639. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2640. break;
  2641. case IPPROTO_GRE:
  2642. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  2643. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2644. break;
  2645. case IPPROTO_IPIP:
  2646. case IPPROTO_IPV6:
  2647. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2648. l4.hdr = skb_inner_network_header(skb);
  2649. break;
  2650. default:
  2651. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2652. return -1;
  2653. skb_checksum_help(skb);
  2654. return 0;
  2655. }
  2656. /* compute outer L3 header size */
  2657. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  2658. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  2659. /* switch IP header pointer from outer to inner header */
  2660. ip.hdr = skb_inner_network_header(skb);
  2661. /* compute tunnel header size */
  2662. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  2663. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  2664. /* indicate if we need to offload outer UDP header */
  2665. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  2666. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2667. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  2668. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  2669. /* record tunnel offload values */
  2670. *cd_tunneling |= tunnel;
  2671. /* switch L4 header pointer from outer to inner */
  2672. l4.hdr = skb_inner_transport_header(skb);
  2673. l4_proto = 0;
  2674. /* reset type as we transition from outer to inner headers */
  2675. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  2676. if (ip.v4->version == 4)
  2677. *tx_flags |= I40E_TX_FLAGS_IPV4;
  2678. if (ip.v6->version == 6)
  2679. *tx_flags |= I40E_TX_FLAGS_IPV6;
  2680. }
  2681. /* Enable IP checksum offloads */
  2682. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2683. l4_proto = ip.v4->protocol;
  2684. /* the stack computes the IP header already, the only time we
  2685. * need the hardware to recompute it is in the case of TSO.
  2686. */
  2687. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2688. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  2689. I40E_TX_DESC_CMD_IIPT_IPV4;
  2690. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2691. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  2692. exthdr = ip.hdr + sizeof(*ip.v6);
  2693. l4_proto = ip.v6->nexthdr;
  2694. if (l4.hdr != exthdr)
  2695. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2696. &l4_proto, &frag_off);
  2697. }
  2698. /* compute inner L3 header size */
  2699. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  2700. /* Enable L4 checksum offloads */
  2701. switch (l4_proto) {
  2702. case IPPROTO_TCP:
  2703. /* enable checksum offloads */
  2704. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  2705. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2706. break;
  2707. case IPPROTO_SCTP:
  2708. /* enable SCTP checksum offload */
  2709. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  2710. offset |= (sizeof(struct sctphdr) >> 2) <<
  2711. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2712. break;
  2713. case IPPROTO_UDP:
  2714. /* enable UDP checksum offload */
  2715. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  2716. offset |= (sizeof(struct udphdr) >> 2) <<
  2717. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2718. break;
  2719. default:
  2720. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2721. return -1;
  2722. skb_checksum_help(skb);
  2723. return 0;
  2724. }
  2725. *td_cmd |= cmd;
  2726. *td_offset |= offset;
  2727. return 1;
  2728. }
  2729. /**
  2730. * i40e_create_tx_ctx Build the Tx context descriptor
  2731. * @tx_ring: ring to create the descriptor on
  2732. * @cd_type_cmd_tso_mss: Quad Word 1
  2733. * @cd_tunneling: Quad Word 0 - bits 0-31
  2734. * @cd_l2tag2: Quad Word 0 - bits 32-63
  2735. **/
  2736. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  2737. const u64 cd_type_cmd_tso_mss,
  2738. const u32 cd_tunneling, const u32 cd_l2tag2)
  2739. {
  2740. struct i40e_tx_context_desc *context_desc;
  2741. int i = tx_ring->next_to_use;
  2742. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  2743. !cd_tunneling && !cd_l2tag2)
  2744. return;
  2745. /* grab the next descriptor */
  2746. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  2747. i++;
  2748. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2749. /* cpu_to_le32 and assign to struct fields */
  2750. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  2751. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  2752. context_desc->rsvd = cpu_to_le16(0);
  2753. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  2754. }
  2755. /**
  2756. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  2757. * @tx_ring: the ring to be checked
  2758. * @size: the size buffer we want to assure is available
  2759. *
  2760. * Returns -EBUSY if a stop is needed, else 0
  2761. **/
  2762. int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  2763. {
  2764. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2765. /* Memory barrier before checking head and tail */
  2766. smp_mb();
  2767. /* Check again in a case another CPU has just made room available. */
  2768. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  2769. return -EBUSY;
  2770. /* A reprieve! - use start_queue because it doesn't call schedule */
  2771. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2772. ++tx_ring->tx_stats.restart_queue;
  2773. return 0;
  2774. }
  2775. /**
  2776. * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
  2777. * @skb: send buffer
  2778. *
  2779. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  2780. * and so we need to figure out the cases where we need to linearize the skb.
  2781. *
  2782. * For TSO we need to count the TSO header and segment payload separately.
  2783. * As such we need to check cases where we have 7 fragments or more as we
  2784. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  2785. * the segment payload in the first descriptor, and another 7 for the
  2786. * fragments.
  2787. **/
  2788. bool __i40e_chk_linearize(struct sk_buff *skb)
  2789. {
  2790. const struct skb_frag_struct *frag, *stale;
  2791. int nr_frags, sum;
  2792. /* no need to check if number of frags is less than 7 */
  2793. nr_frags = skb_shinfo(skb)->nr_frags;
  2794. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  2795. return false;
  2796. /* We need to walk through the list and validate that each group
  2797. * of 6 fragments totals at least gso_size.
  2798. */
  2799. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  2800. frag = &skb_shinfo(skb)->frags[0];
  2801. /* Initialize size to the negative value of gso_size minus 1. We
  2802. * use this as the worst case scenerio in which the frag ahead
  2803. * of us only provides one byte which is why we are limited to 6
  2804. * descriptors for a single transmit as the header and previous
  2805. * fragment are already consuming 2 descriptors.
  2806. */
  2807. sum = 1 - skb_shinfo(skb)->gso_size;
  2808. /* Add size of frags 0 through 4 to create our initial sum */
  2809. sum += skb_frag_size(frag++);
  2810. sum += skb_frag_size(frag++);
  2811. sum += skb_frag_size(frag++);
  2812. sum += skb_frag_size(frag++);
  2813. sum += skb_frag_size(frag++);
  2814. /* Walk through fragments adding latest fragment, testing it, and
  2815. * then removing stale fragments from the sum.
  2816. */
  2817. for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
  2818. int stale_size = skb_frag_size(stale);
  2819. sum += skb_frag_size(frag++);
  2820. /* The stale fragment may present us with a smaller
  2821. * descriptor than the actual fragment size. To account
  2822. * for that we need to remove all the data on the front and
  2823. * figure out what the remainder would be in the last
  2824. * descriptor associated with the fragment.
  2825. */
  2826. if (stale_size > I40E_MAX_DATA_PER_TXD) {
  2827. int align_pad = -(stale->page_offset) &
  2828. (I40E_MAX_READ_REQ_SIZE - 1);
  2829. sum -= align_pad;
  2830. stale_size -= align_pad;
  2831. do {
  2832. sum -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  2833. stale_size -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  2834. } while (stale_size > I40E_MAX_DATA_PER_TXD);
  2835. }
  2836. /* if sum is negative we failed to make sufficient progress */
  2837. if (sum < 0)
  2838. return true;
  2839. if (!nr_frags--)
  2840. break;
  2841. sum -= stale_size;
  2842. }
  2843. return false;
  2844. }
  2845. /**
  2846. * i40e_tx_map - Build the Tx descriptor
  2847. * @tx_ring: ring to send buffer on
  2848. * @skb: send buffer
  2849. * @first: first buffer info buffer to use
  2850. * @tx_flags: collected send information
  2851. * @hdr_len: size of the packet header
  2852. * @td_cmd: the command field in the descriptor
  2853. * @td_offset: offset for checksum or crc
  2854. *
  2855. * Returns 0 on success, -1 on failure to DMA
  2856. **/
  2857. static inline int i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2858. struct i40e_tx_buffer *first, u32 tx_flags,
  2859. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  2860. {
  2861. unsigned int data_len = skb->data_len;
  2862. unsigned int size = skb_headlen(skb);
  2863. struct skb_frag_struct *frag;
  2864. struct i40e_tx_buffer *tx_bi;
  2865. struct i40e_tx_desc *tx_desc;
  2866. u16 i = tx_ring->next_to_use;
  2867. u32 td_tag = 0;
  2868. dma_addr_t dma;
  2869. u16 desc_count = 1;
  2870. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  2871. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  2872. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  2873. I40E_TX_FLAGS_VLAN_SHIFT;
  2874. }
  2875. first->tx_flags = tx_flags;
  2876. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2877. tx_desc = I40E_TX_DESC(tx_ring, i);
  2878. tx_bi = first;
  2879. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  2880. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2881. if (dma_mapping_error(tx_ring->dev, dma))
  2882. goto dma_error;
  2883. /* record length, and DMA address */
  2884. dma_unmap_len_set(tx_bi, len, size);
  2885. dma_unmap_addr_set(tx_bi, dma, dma);
  2886. /* align size to end of page */
  2887. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  2888. tx_desc->buffer_addr = cpu_to_le64(dma);
  2889. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  2890. tx_desc->cmd_type_offset_bsz =
  2891. build_ctob(td_cmd, td_offset,
  2892. max_data, td_tag);
  2893. tx_desc++;
  2894. i++;
  2895. desc_count++;
  2896. if (i == tx_ring->count) {
  2897. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2898. i = 0;
  2899. }
  2900. dma += max_data;
  2901. size -= max_data;
  2902. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2903. tx_desc->buffer_addr = cpu_to_le64(dma);
  2904. }
  2905. if (likely(!data_len))
  2906. break;
  2907. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  2908. size, td_tag);
  2909. tx_desc++;
  2910. i++;
  2911. desc_count++;
  2912. if (i == tx_ring->count) {
  2913. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2914. i = 0;
  2915. }
  2916. size = skb_frag_size(frag);
  2917. data_len -= size;
  2918. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2919. DMA_TO_DEVICE);
  2920. tx_bi = &tx_ring->tx_bi[i];
  2921. }
  2922. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2923. i++;
  2924. if (i == tx_ring->count)
  2925. i = 0;
  2926. tx_ring->next_to_use = i;
  2927. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2928. /* write last descriptor with EOP bit */
  2929. td_cmd |= I40E_TX_DESC_CMD_EOP;
  2930. /* We OR these values together to check both against 4 (WB_STRIDE)
  2931. * below. This is safe since we don't re-use desc_count afterwards.
  2932. */
  2933. desc_count |= ++tx_ring->packet_stride;
  2934. if (desc_count >= WB_STRIDE) {
  2935. /* write last descriptor with RS bit set */
  2936. td_cmd |= I40E_TX_DESC_CMD_RS;
  2937. tx_ring->packet_stride = 0;
  2938. }
  2939. tx_desc->cmd_type_offset_bsz =
  2940. build_ctob(td_cmd, td_offset, size, td_tag);
  2941. /* Force memory writes to complete before letting h/w know there
  2942. * are new descriptors to fetch.
  2943. *
  2944. * We also use this memory barrier to make certain all of the
  2945. * status bits have been updated before next_to_watch is written.
  2946. */
  2947. wmb();
  2948. /* set next_to_watch value indicating a packet is present */
  2949. first->next_to_watch = tx_desc;
  2950. /* notify HW of packet */
  2951. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  2952. writel(i, tx_ring->tail);
  2953. /* we need this if more than one processor can write to our tail
  2954. * at a time, it synchronizes IO on IA64/Altix systems
  2955. */
  2956. mmiowb();
  2957. }
  2958. return 0;
  2959. dma_error:
  2960. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2961. /* clear dma mappings for failed tx_bi map */
  2962. for (;;) {
  2963. tx_bi = &tx_ring->tx_bi[i];
  2964. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2965. if (tx_bi == first)
  2966. break;
  2967. if (i == 0)
  2968. i = tx_ring->count;
  2969. i--;
  2970. }
  2971. tx_ring->next_to_use = i;
  2972. return -1;
  2973. }
  2974. /**
  2975. * i40e_xmit_xdp_ring - transmits an XDP buffer to an XDP Tx ring
  2976. * @xdp: data to transmit
  2977. * @xdp_ring: XDP Tx ring
  2978. **/
  2979. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  2980. struct i40e_ring *xdp_ring)
  2981. {
  2982. u32 size = xdp->data_end - xdp->data;
  2983. u16 i = xdp_ring->next_to_use;
  2984. struct i40e_tx_buffer *tx_bi;
  2985. struct i40e_tx_desc *tx_desc;
  2986. dma_addr_t dma;
  2987. if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
  2988. xdp_ring->tx_stats.tx_busy++;
  2989. return I40E_XDP_CONSUMED;
  2990. }
  2991. dma = dma_map_single(xdp_ring->dev, xdp->data, size, DMA_TO_DEVICE);
  2992. if (dma_mapping_error(xdp_ring->dev, dma))
  2993. return I40E_XDP_CONSUMED;
  2994. tx_bi = &xdp_ring->tx_bi[i];
  2995. tx_bi->bytecount = size;
  2996. tx_bi->gso_segs = 1;
  2997. tx_bi->raw_buf = xdp->data;
  2998. /* record length, and DMA address */
  2999. dma_unmap_len_set(tx_bi, len, size);
  3000. dma_unmap_addr_set(tx_bi, dma, dma);
  3001. tx_desc = I40E_TX_DESC(xdp_ring, i);
  3002. tx_desc->buffer_addr = cpu_to_le64(dma);
  3003. tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC
  3004. | I40E_TXD_CMD,
  3005. 0, size, 0);
  3006. /* Make certain all of the status bits have been updated
  3007. * before next_to_watch is written.
  3008. */
  3009. smp_wmb();
  3010. i++;
  3011. if (i == xdp_ring->count)
  3012. i = 0;
  3013. tx_bi->next_to_watch = tx_desc;
  3014. xdp_ring->next_to_use = i;
  3015. return I40E_XDP_TX;
  3016. }
  3017. /**
  3018. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  3019. * @skb: send buffer
  3020. * @tx_ring: ring to send buffer on
  3021. *
  3022. * Returns NETDEV_TX_OK if sent, else an error code
  3023. **/
  3024. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  3025. struct i40e_ring *tx_ring)
  3026. {
  3027. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  3028. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  3029. struct i40e_tx_buffer *first;
  3030. u32 td_offset = 0;
  3031. u32 tx_flags = 0;
  3032. __be16 protocol;
  3033. u32 td_cmd = 0;
  3034. u8 hdr_len = 0;
  3035. int tso, count;
  3036. int tsyn;
  3037. /* prefetch the data, we'll need it later */
  3038. prefetch(skb->data);
  3039. i40e_trace(xmit_frame_ring, skb, tx_ring);
  3040. count = i40e_xmit_descriptor_count(skb);
  3041. if (i40e_chk_linearize(skb, count)) {
  3042. if (__skb_linearize(skb)) {
  3043. dev_kfree_skb_any(skb);
  3044. return NETDEV_TX_OK;
  3045. }
  3046. count = i40e_txd_use_count(skb->len);
  3047. tx_ring->tx_stats.tx_linearize++;
  3048. }
  3049. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  3050. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  3051. * + 4 desc gap to avoid the cache line where head is,
  3052. * + 1 desc for context descriptor,
  3053. * otherwise try next time
  3054. */
  3055. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  3056. tx_ring->tx_stats.tx_busy++;
  3057. return NETDEV_TX_BUSY;
  3058. }
  3059. /* record the location of the first descriptor for this packet */
  3060. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  3061. first->skb = skb;
  3062. first->bytecount = skb->len;
  3063. first->gso_segs = 1;
  3064. /* prepare the xmit flags */
  3065. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  3066. goto out_drop;
  3067. /* obtain protocol of skb */
  3068. protocol = vlan_get_protocol(skb);
  3069. /* setup IPv4/IPv6 offloads */
  3070. if (protocol == htons(ETH_P_IP))
  3071. tx_flags |= I40E_TX_FLAGS_IPV4;
  3072. else if (protocol == htons(ETH_P_IPV6))
  3073. tx_flags |= I40E_TX_FLAGS_IPV6;
  3074. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  3075. if (tso < 0)
  3076. goto out_drop;
  3077. else if (tso)
  3078. tx_flags |= I40E_TX_FLAGS_TSO;
  3079. /* Always offload the checksum, since it's in the data descriptor */
  3080. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  3081. tx_ring, &cd_tunneling);
  3082. if (tso < 0)
  3083. goto out_drop;
  3084. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  3085. if (tsyn)
  3086. tx_flags |= I40E_TX_FLAGS_TSYN;
  3087. skb_tx_timestamp(skb);
  3088. /* always enable CRC insertion offload */
  3089. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  3090. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  3091. cd_tunneling, cd_l2tag2);
  3092. /* Add Flow Director ATR if it's enabled.
  3093. *
  3094. * NOTE: this must always be directly before the data descriptor.
  3095. */
  3096. i40e_atr(tx_ring, skb, tx_flags);
  3097. if (i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  3098. td_cmd, td_offset))
  3099. goto cleanup_tx_tstamp;
  3100. return NETDEV_TX_OK;
  3101. out_drop:
  3102. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  3103. dev_kfree_skb_any(first->skb);
  3104. first->skb = NULL;
  3105. cleanup_tx_tstamp:
  3106. if (unlikely(tx_flags & I40E_TX_FLAGS_TSYN)) {
  3107. struct i40e_pf *pf = i40e_netdev_to_pf(tx_ring->netdev);
  3108. dev_kfree_skb_any(pf->ptp_tx_skb);
  3109. pf->ptp_tx_skb = NULL;
  3110. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  3111. }
  3112. return NETDEV_TX_OK;
  3113. }
  3114. /**
  3115. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  3116. * @skb: send buffer
  3117. * @netdev: network interface device structure
  3118. *
  3119. * Returns NETDEV_TX_OK if sent, else an error code
  3120. **/
  3121. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  3122. {
  3123. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3124. struct i40e_vsi *vsi = np->vsi;
  3125. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  3126. /* hardware can't handle really short frames, hardware padding works
  3127. * beyond this point
  3128. */
  3129. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  3130. return NETDEV_TX_OK;
  3131. return i40e_xmit_frame_ring(skb, tx_ring);
  3132. }
  3133. /**
  3134. * i40e_xdp_xmit - Implements ndo_xdp_xmit
  3135. * @dev: netdev
  3136. * @xdp: XDP buffer
  3137. *
  3138. * Returns Zero if sent, else an error code
  3139. **/
  3140. int i40e_xdp_xmit(struct net_device *dev, struct xdp_buff *xdp)
  3141. {
  3142. struct i40e_netdev_priv *np = netdev_priv(dev);
  3143. unsigned int queue_index = smp_processor_id();
  3144. struct i40e_vsi *vsi = np->vsi;
  3145. int err;
  3146. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3147. return -ENETDOWN;
  3148. if (!i40e_enabled_xdp_vsi(vsi) || queue_index >= vsi->num_queue_pairs)
  3149. return -ENXIO;
  3150. err = i40e_xmit_xdp_ring(xdp, vsi->xdp_rings[queue_index]);
  3151. if (err != I40E_XDP_TX)
  3152. return -ENOSPC;
  3153. return 0;
  3154. }
  3155. /**
  3156. * i40e_xdp_flush - Implements ndo_xdp_flush
  3157. * @dev: netdev
  3158. **/
  3159. void i40e_xdp_flush(struct net_device *dev)
  3160. {
  3161. struct i40e_netdev_priv *np = netdev_priv(dev);
  3162. unsigned int queue_index = smp_processor_id();
  3163. struct i40e_vsi *vsi = np->vsi;
  3164. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3165. return;
  3166. if (!i40e_enabled_xdp_vsi(vsi) || queue_index >= vsi->num_queue_pairs)
  3167. return;
  3168. i40e_xdp_ring_update_tail(vsi->xdp_rings[queue_index]);
  3169. }