i40e_txrx.c 105 KB

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