i40e_txrx.c 83 KB

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