i40e_txrx.c 68 KB

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