i40e_txrx.c 95 KB

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