i40e_txrx.c 95 KB

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