i40e_txrx.c 88 KB

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