i40e_txrx.c 105 KB

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