i40e_txrx.c 106 KB

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