i40e_txrx.c 77 KB

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