i40e_txrx.c 82 KB

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