i40e_txrx.c 91 KB

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