i40e_txrx.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include <net/busy_poll.h>
  28. #include "i40evf.h"
  29. #include "i40e_trace.h"
  30. #include "i40e_prototype.h"
  31. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  32. u32 td_tag)
  33. {
  34. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  35. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  36. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  37. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  38. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  39. }
  40. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  41. /**
  42. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  43. * @ring: the ring that owns the buffer
  44. * @tx_buffer: the buffer to free
  45. **/
  46. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  47. struct i40e_tx_buffer *tx_buffer)
  48. {
  49. if (tx_buffer->skb) {
  50. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  51. kfree(tx_buffer->raw_buf);
  52. else
  53. dev_kfree_skb_any(tx_buffer->skb);
  54. if (dma_unmap_len(tx_buffer, len))
  55. dma_unmap_single(ring->dev,
  56. dma_unmap_addr(tx_buffer, dma),
  57. dma_unmap_len(tx_buffer, len),
  58. DMA_TO_DEVICE);
  59. } else if (dma_unmap_len(tx_buffer, len)) {
  60. dma_unmap_page(ring->dev,
  61. dma_unmap_addr(tx_buffer, dma),
  62. dma_unmap_len(tx_buffer, len),
  63. DMA_TO_DEVICE);
  64. }
  65. tx_buffer->next_to_watch = NULL;
  66. tx_buffer->skb = NULL;
  67. dma_unmap_len_set(tx_buffer, len, 0);
  68. /* tx_buffer must be completely set up in the transmit path */
  69. }
  70. /**
  71. * i40evf_clean_tx_ring - Free any empty Tx buffers
  72. * @tx_ring: ring to be cleaned
  73. **/
  74. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  75. {
  76. unsigned long bi_size;
  77. u16 i;
  78. /* ring already cleared, nothing to do */
  79. if (!tx_ring->tx_bi)
  80. return;
  81. /* Free all the Tx ring sk_buffs */
  82. for (i = 0; i < tx_ring->count; i++)
  83. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  84. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  85. memset(tx_ring->tx_bi, 0, bi_size);
  86. /* Zero out the descriptor ring */
  87. memset(tx_ring->desc, 0, tx_ring->size);
  88. tx_ring->next_to_use = 0;
  89. tx_ring->next_to_clean = 0;
  90. if (!tx_ring->netdev)
  91. return;
  92. /* cleanup Tx queue statistics */
  93. netdev_tx_reset_queue(txring_txq(tx_ring));
  94. }
  95. /**
  96. * i40evf_free_tx_resources - Free Tx resources per queue
  97. * @tx_ring: Tx descriptor ring for a specific queue
  98. *
  99. * Free all transmit software resources
  100. **/
  101. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  102. {
  103. i40evf_clean_tx_ring(tx_ring);
  104. kfree(tx_ring->tx_bi);
  105. tx_ring->tx_bi = NULL;
  106. if (tx_ring->desc) {
  107. dma_free_coherent(tx_ring->dev, tx_ring->size,
  108. tx_ring->desc, tx_ring->dma);
  109. tx_ring->desc = NULL;
  110. }
  111. }
  112. /**
  113. * i40evf_get_tx_pending - how many Tx descriptors not processed
  114. * @tx_ring: the ring of descriptors
  115. * @in_sw: is tx_pending being checked in SW or HW
  116. *
  117. * Since there is no access to the ring head register
  118. * in XL710, we need to use our local copies
  119. **/
  120. u32 i40evf_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  121. {
  122. u32 head, tail;
  123. head = ring->next_to_clean;
  124. tail = readl(ring->tail);
  125. if (head != tail)
  126. return (head < tail) ?
  127. tail - head : (tail + ring->count - head);
  128. return 0;
  129. }
  130. /**
  131. * i40evf_detect_recover_hung - Function to detect and recover hung_queues
  132. * @vsi: pointer to vsi struct with tx queues
  133. *
  134. * VSI has netdev and netdev has TX queues. This function is to check each of
  135. * those TX queues if they are hung, trigger recovery by issuing SW interrupt.
  136. **/
  137. void i40evf_detect_recover_hung(struct i40e_vsi *vsi)
  138. {
  139. struct i40e_ring *tx_ring = NULL;
  140. struct net_device *netdev;
  141. unsigned int i;
  142. int packets;
  143. if (!vsi)
  144. return;
  145. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  146. return;
  147. netdev = vsi->netdev;
  148. if (!netdev)
  149. return;
  150. if (!netif_carrier_ok(netdev))
  151. return;
  152. for (i = 0; i < vsi->back->num_active_queues; i++) {
  153. tx_ring = &vsi->back->tx_rings[i];
  154. if (tx_ring && tx_ring->desc) {
  155. /* If packet counter has not changed the queue is
  156. * likely stalled, so force an interrupt for this
  157. * queue.
  158. *
  159. * prev_pkt_ctr would be negative if there was no
  160. * pending work.
  161. */
  162. packets = tx_ring->stats.packets & INT_MAX;
  163. if (tx_ring->tx_stats.prev_pkt_ctr == packets) {
  164. i40evf_force_wb(vsi, tx_ring->q_vector);
  165. continue;
  166. }
  167. /* Memory barrier between read of packet count and call
  168. * to i40evf_get_tx_pending()
  169. */
  170. smp_rmb();
  171. tx_ring->tx_stats.prev_pkt_ctr =
  172. i40evf_get_tx_pending(tx_ring, false) ? packets : -1;
  173. }
  174. }
  175. }
  176. #define WB_STRIDE 4
  177. /**
  178. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  179. * @vsi: the VSI we care about
  180. * @tx_ring: Tx ring to clean
  181. * @napi_budget: Used to determine if we are in netpoll
  182. *
  183. * Returns true if there's any budget left (e.g. the clean is finished)
  184. **/
  185. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  186. struct i40e_ring *tx_ring, int napi_budget)
  187. {
  188. u16 i = tx_ring->next_to_clean;
  189. struct i40e_tx_buffer *tx_buf;
  190. struct i40e_tx_desc *tx_desc;
  191. unsigned int total_bytes = 0, total_packets = 0;
  192. unsigned int budget = vsi->work_limit;
  193. tx_buf = &tx_ring->tx_bi[i];
  194. tx_desc = I40E_TX_DESC(tx_ring, i);
  195. i -= tx_ring->count;
  196. do {
  197. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  198. /* if next_to_watch is not set then there is no work pending */
  199. if (!eop_desc)
  200. break;
  201. /* prevent any other reads prior to eop_desc */
  202. smp_rmb();
  203. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  204. /* if the descriptor isn't done, no work yet to do */
  205. if (!(eop_desc->cmd_type_offset_bsz &
  206. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  207. break;
  208. /* clear next_to_watch to prevent false hangs */
  209. tx_buf->next_to_watch = NULL;
  210. /* update the statistics for this packet */
  211. total_bytes += tx_buf->bytecount;
  212. total_packets += tx_buf->gso_segs;
  213. /* free the skb */
  214. napi_consume_skb(tx_buf->skb, napi_budget);
  215. /* unmap skb header data */
  216. dma_unmap_single(tx_ring->dev,
  217. dma_unmap_addr(tx_buf, dma),
  218. dma_unmap_len(tx_buf, len),
  219. DMA_TO_DEVICE);
  220. /* clear tx_buffer data */
  221. tx_buf->skb = NULL;
  222. dma_unmap_len_set(tx_buf, len, 0);
  223. /* unmap remaining buffers */
  224. while (tx_desc != eop_desc) {
  225. i40e_trace(clean_tx_irq_unmap,
  226. tx_ring, tx_desc, tx_buf);
  227. tx_buf++;
  228. tx_desc++;
  229. i++;
  230. if (unlikely(!i)) {
  231. i -= tx_ring->count;
  232. tx_buf = tx_ring->tx_bi;
  233. tx_desc = I40E_TX_DESC(tx_ring, 0);
  234. }
  235. /* unmap any remaining paged data */
  236. if (dma_unmap_len(tx_buf, len)) {
  237. dma_unmap_page(tx_ring->dev,
  238. dma_unmap_addr(tx_buf, dma),
  239. dma_unmap_len(tx_buf, len),
  240. DMA_TO_DEVICE);
  241. dma_unmap_len_set(tx_buf, len, 0);
  242. }
  243. }
  244. /* move us one more past the eop_desc for start of next pkt */
  245. tx_buf++;
  246. tx_desc++;
  247. i++;
  248. if (unlikely(!i)) {
  249. i -= tx_ring->count;
  250. tx_buf = tx_ring->tx_bi;
  251. tx_desc = I40E_TX_DESC(tx_ring, 0);
  252. }
  253. prefetch(tx_desc);
  254. /* update budget accounting */
  255. budget--;
  256. } while (likely(budget));
  257. i += tx_ring->count;
  258. tx_ring->next_to_clean = i;
  259. u64_stats_update_begin(&tx_ring->syncp);
  260. tx_ring->stats.bytes += total_bytes;
  261. tx_ring->stats.packets += total_packets;
  262. u64_stats_update_end(&tx_ring->syncp);
  263. tx_ring->q_vector->tx.total_bytes += total_bytes;
  264. tx_ring->q_vector->tx.total_packets += total_packets;
  265. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  266. /* check to see if there are < 4 descriptors
  267. * waiting to be written back, then kick the hardware to force
  268. * them to be written back in case we stay in NAPI.
  269. * In this mode on X722 we do not enable Interrupt.
  270. */
  271. unsigned int j = i40evf_get_tx_pending(tx_ring, false);
  272. if (budget &&
  273. ((j / WB_STRIDE) == 0) && (j > 0) &&
  274. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  275. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  276. tx_ring->arm_wb = true;
  277. }
  278. /* notify netdev of completed buffers */
  279. netdev_tx_completed_queue(txring_txq(tx_ring),
  280. total_packets, total_bytes);
  281. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  282. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  283. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  284. /* Make sure that anybody stopping the queue after this
  285. * sees the new next_to_clean.
  286. */
  287. smp_mb();
  288. if (__netif_subqueue_stopped(tx_ring->netdev,
  289. tx_ring->queue_index) &&
  290. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  291. netif_wake_subqueue(tx_ring->netdev,
  292. tx_ring->queue_index);
  293. ++tx_ring->tx_stats.restart_queue;
  294. }
  295. }
  296. return !!budget;
  297. }
  298. /**
  299. * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  300. * @vsi: the VSI we care about
  301. * @q_vector: the vector on which to enable writeback
  302. *
  303. **/
  304. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  305. struct i40e_q_vector *q_vector)
  306. {
  307. u16 flags = q_vector->tx.ring[0].flags;
  308. u32 val;
  309. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  310. return;
  311. if (q_vector->arm_wb_state)
  312. return;
  313. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
  314. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
  315. wr32(&vsi->back->hw,
  316. I40E_VFINT_DYN_CTLN1(q_vector->reg_idx), val);
  317. q_vector->arm_wb_state = true;
  318. }
  319. /**
  320. * i40evf_force_wb - Issue SW Interrupt so HW does a wb
  321. * @vsi: the VSI we care about
  322. * @q_vector: the vector on which to force writeback
  323. *
  324. **/
  325. void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  326. {
  327. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  328. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  329. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  330. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
  331. /* allow 00 to be written to the index */;
  332. wr32(&vsi->back->hw,
  333. I40E_VFINT_DYN_CTLN1(q_vector->reg_idx),
  334. val);
  335. }
  336. static inline bool i40e_container_is_rx(struct i40e_q_vector *q_vector,
  337. struct i40e_ring_container *rc)
  338. {
  339. return &q_vector->rx == rc;
  340. }
  341. static inline unsigned int i40e_itr_divisor(struct i40e_q_vector *q_vector)
  342. {
  343. unsigned int divisor;
  344. switch (q_vector->adapter->link_speed) {
  345. case I40E_LINK_SPEED_40GB:
  346. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 1024;
  347. break;
  348. case I40E_LINK_SPEED_25GB:
  349. case I40E_LINK_SPEED_20GB:
  350. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 512;
  351. break;
  352. default:
  353. case I40E_LINK_SPEED_10GB:
  354. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 256;
  355. break;
  356. case I40E_LINK_SPEED_1GB:
  357. case I40E_LINK_SPEED_100MB:
  358. divisor = I40E_ITR_ADAPTIVE_MIN_INC * 32;
  359. break;
  360. }
  361. return divisor;
  362. }
  363. /**
  364. * i40e_update_itr - update the dynamic ITR value based on statistics
  365. * @q_vector: structure containing interrupt and ring information
  366. * @rc: structure containing ring performance data
  367. *
  368. * Stores a new ITR value based on packets and byte
  369. * counts during the last interrupt. The advantage of per interrupt
  370. * computation is faster updates and more accurate ITR for the current
  371. * traffic pattern. Constants in this function were computed
  372. * based on theoretical maximum wire speed and thresholds were set based
  373. * on testing data as well as attempting to minimize response time
  374. * while increasing bulk throughput.
  375. **/
  376. static void i40e_update_itr(struct i40e_q_vector *q_vector,
  377. struct i40e_ring_container *rc)
  378. {
  379. unsigned int avg_wire_size, packets, bytes, itr;
  380. unsigned long next_update = jiffies;
  381. /* If we don't have any rings just leave ourselves set for maximum
  382. * possible latency so we take ourselves out of the equation.
  383. */
  384. if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting))
  385. return;
  386. /* For Rx we want to push the delay up and default to low latency.
  387. * for Tx we want to pull the delay down and default to high latency.
  388. */
  389. itr = i40e_container_is_rx(q_vector, rc) ?
  390. I40E_ITR_ADAPTIVE_MIN_USECS | I40E_ITR_ADAPTIVE_LATENCY :
  391. I40E_ITR_ADAPTIVE_MAX_USECS | I40E_ITR_ADAPTIVE_LATENCY;
  392. /* If we didn't update within up to 1 - 2 jiffies we can assume
  393. * that either packets are coming in so slow there hasn't been
  394. * any work, or that there is so much work that NAPI is dealing
  395. * with interrupt moderation and we don't need to do anything.
  396. */
  397. if (time_after(next_update, rc->next_update))
  398. goto clear_counts;
  399. /* If itr_countdown is set it means we programmed an ITR within
  400. * the last 4 interrupt cycles. This has a side effect of us
  401. * potentially firing an early interrupt. In order to work around
  402. * this we need to throw out any data received for a few
  403. * interrupts following the update.
  404. */
  405. if (q_vector->itr_countdown) {
  406. itr = rc->target_itr;
  407. goto clear_counts;
  408. }
  409. packets = rc->total_packets;
  410. bytes = rc->total_bytes;
  411. if (i40e_container_is_rx(q_vector, rc)) {
  412. /* If Rx there are 1 to 4 packets and bytes are less than
  413. * 9000 assume insufficient data to use bulk rate limiting
  414. * approach unless Tx is already in bulk rate limiting. We
  415. * are likely latency driven.
  416. */
  417. if (packets && packets < 4 && bytes < 9000 &&
  418. (q_vector->tx.target_itr & I40E_ITR_ADAPTIVE_LATENCY)) {
  419. itr = I40E_ITR_ADAPTIVE_LATENCY;
  420. goto adjust_by_size;
  421. }
  422. } else if (packets < 4) {
  423. /* If we have Tx and Rx ITR maxed and Tx ITR is running in
  424. * bulk mode and we are receiving 4 or fewer packets just
  425. * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so
  426. * that the Rx can relax.
  427. */
  428. if (rc->target_itr == I40E_ITR_ADAPTIVE_MAX_USECS &&
  429. (q_vector->rx.target_itr & I40E_ITR_MASK) ==
  430. I40E_ITR_ADAPTIVE_MAX_USECS)
  431. goto clear_counts;
  432. } else if (packets > 32) {
  433. /* If we have processed over 32 packets in a single interrupt
  434. * for Tx assume we need to switch over to "bulk" mode.
  435. */
  436. rc->target_itr &= ~I40E_ITR_ADAPTIVE_LATENCY;
  437. }
  438. /* We have no packets to actually measure against. This means
  439. * either one of the other queues on this vector is active or
  440. * we are a Tx queue doing TSO with too high of an interrupt rate.
  441. *
  442. * Between 4 and 56 we can assume that our current interrupt delay
  443. * is only slightly too low. As such we should increase it by a small
  444. * fixed amount.
  445. */
  446. if (packets < 56) {
  447. itr = rc->target_itr + I40E_ITR_ADAPTIVE_MIN_INC;
  448. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  449. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  450. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  451. }
  452. goto clear_counts;
  453. }
  454. if (packets <= 256) {
  455. itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr);
  456. itr &= I40E_ITR_MASK;
  457. /* Between 56 and 112 is our "goldilocks" zone where we are
  458. * working out "just right". Just report that our current
  459. * ITR is good for us.
  460. */
  461. if (packets <= 112)
  462. goto clear_counts;
  463. /* If packet count is 128 or greater we are likely looking
  464. * at a slight overrun of the delay we want. Try halving
  465. * our delay to see if that will cut the number of packets
  466. * in half per interrupt.
  467. */
  468. itr /= 2;
  469. itr &= I40E_ITR_MASK;
  470. if (itr < I40E_ITR_ADAPTIVE_MIN_USECS)
  471. itr = I40E_ITR_ADAPTIVE_MIN_USECS;
  472. goto clear_counts;
  473. }
  474. /* The paths below assume we are dealing with a bulk ITR since
  475. * number of packets is greater than 256. We are just going to have
  476. * to compute a value and try to bring the count under control,
  477. * though for smaller packet sizes there isn't much we can do as
  478. * NAPI polling will likely be kicking in sooner rather than later.
  479. */
  480. itr = I40E_ITR_ADAPTIVE_BULK;
  481. adjust_by_size:
  482. /* If packet counts are 256 or greater we can assume we have a gross
  483. * overestimation of what the rate should be. Instead of trying to fine
  484. * tune it just use the formula below to try and dial in an exact value
  485. * give the current packet size of the frame.
  486. */
  487. avg_wire_size = bytes / packets;
  488. /* The following is a crude approximation of:
  489. * wmem_default / (size + overhead) = desired_pkts_per_int
  490. * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
  491. * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
  492. *
  493. * Assuming wmem_default is 212992 and overhead is 640 bytes per
  494. * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
  495. * formula down to
  496. *
  497. * (170 * (size + 24)) / (size + 640) = ITR
  498. *
  499. * We first do some math on the packet size and then finally bitshift
  500. * by 8 after rounding up. We also have to account for PCIe link speed
  501. * difference as ITR scales based on this.
  502. */
  503. if (avg_wire_size <= 60) {
  504. /* Start at 250k ints/sec */
  505. avg_wire_size = 4096;
  506. } else if (avg_wire_size <= 380) {
  507. /* 250K ints/sec to 60K ints/sec */
  508. avg_wire_size *= 40;
  509. avg_wire_size += 1696;
  510. } else if (avg_wire_size <= 1084) {
  511. /* 60K ints/sec to 36K ints/sec */
  512. avg_wire_size *= 15;
  513. avg_wire_size += 11452;
  514. } else if (avg_wire_size <= 1980) {
  515. /* 36K ints/sec to 30K ints/sec */
  516. avg_wire_size *= 5;
  517. avg_wire_size += 22420;
  518. } else {
  519. /* plateau at a limit of 30K ints/sec */
  520. avg_wire_size = 32256;
  521. }
  522. /* If we are in low latency mode halve our delay which doubles the
  523. * rate to somewhere between 100K to 16K ints/sec
  524. */
  525. if (itr & I40E_ITR_ADAPTIVE_LATENCY)
  526. avg_wire_size /= 2;
  527. /* Resultant value is 256 times larger than it needs to be. This
  528. * gives us room to adjust the value as needed to either increase
  529. * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc.
  530. *
  531. * Use addition as we have already recorded the new latency flag
  532. * for the ITR value.
  533. */
  534. itr += DIV_ROUND_UP(avg_wire_size, i40e_itr_divisor(q_vector)) *
  535. I40E_ITR_ADAPTIVE_MIN_INC;
  536. if ((itr & I40E_ITR_MASK) > I40E_ITR_ADAPTIVE_MAX_USECS) {
  537. itr &= I40E_ITR_ADAPTIVE_LATENCY;
  538. itr += I40E_ITR_ADAPTIVE_MAX_USECS;
  539. }
  540. clear_counts:
  541. /* write back value */
  542. rc->target_itr = itr;
  543. /* next update should occur within next jiffy */
  544. rc->next_update = next_update + 1;
  545. rc->total_bytes = 0;
  546. rc->total_packets = 0;
  547. }
  548. /**
  549. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  550. * @tx_ring: the tx ring to set up
  551. *
  552. * Return 0 on success, negative on error
  553. **/
  554. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  555. {
  556. struct device *dev = tx_ring->dev;
  557. int bi_size;
  558. if (!dev)
  559. return -ENOMEM;
  560. /* warn if we are about to overwrite the pointer */
  561. WARN_ON(tx_ring->tx_bi);
  562. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  563. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  564. if (!tx_ring->tx_bi)
  565. goto err;
  566. /* round up to nearest 4K */
  567. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  568. tx_ring->size = ALIGN(tx_ring->size, 4096);
  569. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  570. &tx_ring->dma, GFP_KERNEL);
  571. if (!tx_ring->desc) {
  572. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  573. tx_ring->size);
  574. goto err;
  575. }
  576. tx_ring->next_to_use = 0;
  577. tx_ring->next_to_clean = 0;
  578. tx_ring->tx_stats.prev_pkt_ctr = -1;
  579. return 0;
  580. err:
  581. kfree(tx_ring->tx_bi);
  582. tx_ring->tx_bi = NULL;
  583. return -ENOMEM;
  584. }
  585. /**
  586. * i40evf_clean_rx_ring - Free Rx buffers
  587. * @rx_ring: ring to be cleaned
  588. **/
  589. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  590. {
  591. unsigned long bi_size;
  592. u16 i;
  593. /* ring already cleared, nothing to do */
  594. if (!rx_ring->rx_bi)
  595. return;
  596. if (rx_ring->skb) {
  597. dev_kfree_skb(rx_ring->skb);
  598. rx_ring->skb = NULL;
  599. }
  600. /* Free all the Rx ring sk_buffs */
  601. for (i = 0; i < rx_ring->count; i++) {
  602. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  603. if (!rx_bi->page)
  604. continue;
  605. /* Invalidate cache lines that may have been written to by
  606. * device so that we avoid corrupting memory.
  607. */
  608. dma_sync_single_range_for_cpu(rx_ring->dev,
  609. rx_bi->dma,
  610. rx_bi->page_offset,
  611. rx_ring->rx_buf_len,
  612. DMA_FROM_DEVICE);
  613. /* free resources associated with mapping */
  614. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  615. i40e_rx_pg_size(rx_ring),
  616. DMA_FROM_DEVICE,
  617. I40E_RX_DMA_ATTR);
  618. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  619. rx_bi->page = NULL;
  620. rx_bi->page_offset = 0;
  621. }
  622. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  623. memset(rx_ring->rx_bi, 0, bi_size);
  624. /* Zero out the descriptor ring */
  625. memset(rx_ring->desc, 0, rx_ring->size);
  626. rx_ring->next_to_alloc = 0;
  627. rx_ring->next_to_clean = 0;
  628. rx_ring->next_to_use = 0;
  629. }
  630. /**
  631. * i40evf_free_rx_resources - Free Rx resources
  632. * @rx_ring: ring to clean the resources from
  633. *
  634. * Free all receive software resources
  635. **/
  636. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  637. {
  638. i40evf_clean_rx_ring(rx_ring);
  639. kfree(rx_ring->rx_bi);
  640. rx_ring->rx_bi = NULL;
  641. if (rx_ring->desc) {
  642. dma_free_coherent(rx_ring->dev, rx_ring->size,
  643. rx_ring->desc, rx_ring->dma);
  644. rx_ring->desc = NULL;
  645. }
  646. }
  647. /**
  648. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  649. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  650. *
  651. * Returns 0 on success, negative on failure
  652. **/
  653. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  654. {
  655. struct device *dev = rx_ring->dev;
  656. int bi_size;
  657. /* warn if we are about to overwrite the pointer */
  658. WARN_ON(rx_ring->rx_bi);
  659. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  660. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  661. if (!rx_ring->rx_bi)
  662. goto err;
  663. u64_stats_init(&rx_ring->syncp);
  664. /* Round up to nearest 4K */
  665. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  666. rx_ring->size = ALIGN(rx_ring->size, 4096);
  667. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  668. &rx_ring->dma, GFP_KERNEL);
  669. if (!rx_ring->desc) {
  670. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  671. rx_ring->size);
  672. goto err;
  673. }
  674. rx_ring->next_to_alloc = 0;
  675. rx_ring->next_to_clean = 0;
  676. rx_ring->next_to_use = 0;
  677. return 0;
  678. err:
  679. kfree(rx_ring->rx_bi);
  680. rx_ring->rx_bi = NULL;
  681. return -ENOMEM;
  682. }
  683. /**
  684. * i40e_release_rx_desc - Store the new tail and head values
  685. * @rx_ring: ring to bump
  686. * @val: new head index
  687. **/
  688. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  689. {
  690. rx_ring->next_to_use = val;
  691. /* update next to alloc since we have filled the ring */
  692. rx_ring->next_to_alloc = val;
  693. /* Force memory writes to complete before letting h/w
  694. * know there are new descriptors to fetch. (Only
  695. * applicable for weak-ordered memory model archs,
  696. * such as IA-64).
  697. */
  698. wmb();
  699. writel(val, rx_ring->tail);
  700. }
  701. /**
  702. * i40e_rx_offset - Return expected offset into page to access data
  703. * @rx_ring: Ring we are requesting offset of
  704. *
  705. * Returns the offset value for ring into the data buffer.
  706. */
  707. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  708. {
  709. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  710. }
  711. /**
  712. * i40e_alloc_mapped_page - recycle or make a new page
  713. * @rx_ring: ring to use
  714. * @bi: rx_buffer struct to modify
  715. *
  716. * Returns true if the page was successfully allocated or
  717. * reused.
  718. **/
  719. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  720. struct i40e_rx_buffer *bi)
  721. {
  722. struct page *page = bi->page;
  723. dma_addr_t dma;
  724. /* since we are recycling buffers we should seldom need to alloc */
  725. if (likely(page)) {
  726. rx_ring->rx_stats.page_reuse_count++;
  727. return true;
  728. }
  729. /* alloc new page for storage */
  730. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  731. if (unlikely(!page)) {
  732. rx_ring->rx_stats.alloc_page_failed++;
  733. return false;
  734. }
  735. /* map page for use */
  736. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  737. i40e_rx_pg_size(rx_ring),
  738. DMA_FROM_DEVICE,
  739. I40E_RX_DMA_ATTR);
  740. /* if mapping failed free memory back to system since
  741. * there isn't much point in holding memory we can't use
  742. */
  743. if (dma_mapping_error(rx_ring->dev, dma)) {
  744. __free_pages(page, i40e_rx_pg_order(rx_ring));
  745. rx_ring->rx_stats.alloc_page_failed++;
  746. return false;
  747. }
  748. bi->dma = dma;
  749. bi->page = page;
  750. bi->page_offset = i40e_rx_offset(rx_ring);
  751. /* initialize pagecnt_bias to 1 representing we fully own page */
  752. bi->pagecnt_bias = 1;
  753. return true;
  754. }
  755. /**
  756. * i40e_receive_skb - Send a completed packet up the stack
  757. * @rx_ring: rx ring in play
  758. * @skb: packet to send up
  759. * @vlan_tag: vlan tag for packet
  760. **/
  761. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  762. struct sk_buff *skb, u16 vlan_tag)
  763. {
  764. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  765. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  766. (vlan_tag & VLAN_VID_MASK))
  767. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  768. napi_gro_receive(&q_vector->napi, skb);
  769. }
  770. /**
  771. * i40evf_alloc_rx_buffers - Replace used receive buffers
  772. * @rx_ring: ring to place buffers on
  773. * @cleaned_count: number of buffers to replace
  774. *
  775. * Returns false if all allocations were successful, true if any fail
  776. **/
  777. bool i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  778. {
  779. u16 ntu = rx_ring->next_to_use;
  780. union i40e_rx_desc *rx_desc;
  781. struct i40e_rx_buffer *bi;
  782. /* do nothing if no valid netdev defined */
  783. if (!rx_ring->netdev || !cleaned_count)
  784. return false;
  785. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  786. bi = &rx_ring->rx_bi[ntu];
  787. do {
  788. if (!i40e_alloc_mapped_page(rx_ring, bi))
  789. goto no_buffers;
  790. /* sync the buffer for use by the device */
  791. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  792. bi->page_offset,
  793. rx_ring->rx_buf_len,
  794. DMA_FROM_DEVICE);
  795. /* Refresh the desc even if buffer_addrs didn't change
  796. * because each write-back erases this info.
  797. */
  798. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  799. rx_desc++;
  800. bi++;
  801. ntu++;
  802. if (unlikely(ntu == rx_ring->count)) {
  803. rx_desc = I40E_RX_DESC(rx_ring, 0);
  804. bi = rx_ring->rx_bi;
  805. ntu = 0;
  806. }
  807. /* clear the status bits for the next_to_use descriptor */
  808. rx_desc->wb.qword1.status_error_len = 0;
  809. cleaned_count--;
  810. } while (cleaned_count);
  811. if (rx_ring->next_to_use != ntu)
  812. i40e_release_rx_desc(rx_ring, ntu);
  813. return false;
  814. no_buffers:
  815. if (rx_ring->next_to_use != ntu)
  816. i40e_release_rx_desc(rx_ring, ntu);
  817. /* make sure to come back via polling to try again after
  818. * allocation failure
  819. */
  820. return true;
  821. }
  822. /**
  823. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  824. * @vsi: the VSI we care about
  825. * @skb: skb currently being received and modified
  826. * @rx_desc: the receive descriptor
  827. **/
  828. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  829. struct sk_buff *skb,
  830. union i40e_rx_desc *rx_desc)
  831. {
  832. struct i40e_rx_ptype_decoded decoded;
  833. u32 rx_error, rx_status;
  834. bool ipv4, ipv6;
  835. u8 ptype;
  836. u64 qword;
  837. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  838. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  839. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  840. I40E_RXD_QW1_ERROR_SHIFT;
  841. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  842. I40E_RXD_QW1_STATUS_SHIFT;
  843. decoded = decode_rx_desc_ptype(ptype);
  844. skb->ip_summed = CHECKSUM_NONE;
  845. skb_checksum_none_assert(skb);
  846. /* Rx csum enabled and ip headers found? */
  847. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  848. return;
  849. /* did the hardware decode the packet and checksum? */
  850. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  851. return;
  852. /* both known and outer_ip must be set for the below code to work */
  853. if (!(decoded.known && decoded.outer_ip))
  854. return;
  855. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  856. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  857. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  858. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  859. if (ipv4 &&
  860. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  861. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  862. goto checksum_fail;
  863. /* likely incorrect csum if alternate IP extension headers found */
  864. if (ipv6 &&
  865. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  866. /* don't increment checksum err here, non-fatal err */
  867. return;
  868. /* there was some L4 error, count error and punt packet to the stack */
  869. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  870. goto checksum_fail;
  871. /* handle packets that were not able to be checksummed due
  872. * to arrival speed, in this case the stack can compute
  873. * the csum.
  874. */
  875. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  876. return;
  877. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  878. switch (decoded.inner_prot) {
  879. case I40E_RX_PTYPE_INNER_PROT_TCP:
  880. case I40E_RX_PTYPE_INNER_PROT_UDP:
  881. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  882. skb->ip_summed = CHECKSUM_UNNECESSARY;
  883. /* fall though */
  884. default:
  885. break;
  886. }
  887. return;
  888. checksum_fail:
  889. vsi->back->hw_csum_rx_error++;
  890. }
  891. /**
  892. * i40e_ptype_to_htype - get a hash type
  893. * @ptype: the ptype value from the descriptor
  894. *
  895. * Returns a hash type to be used by skb_set_hash
  896. **/
  897. static inline int i40e_ptype_to_htype(u8 ptype)
  898. {
  899. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  900. if (!decoded.known)
  901. return PKT_HASH_TYPE_NONE;
  902. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  903. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  904. return PKT_HASH_TYPE_L4;
  905. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  906. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  907. return PKT_HASH_TYPE_L3;
  908. else
  909. return PKT_HASH_TYPE_L2;
  910. }
  911. /**
  912. * i40e_rx_hash - set the hash value in the skb
  913. * @ring: descriptor ring
  914. * @rx_desc: specific descriptor
  915. **/
  916. static inline void i40e_rx_hash(struct i40e_ring *ring,
  917. union i40e_rx_desc *rx_desc,
  918. struct sk_buff *skb,
  919. u8 rx_ptype)
  920. {
  921. u32 hash;
  922. const __le64 rss_mask =
  923. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  924. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  925. if (ring->netdev->features & NETIF_F_RXHASH)
  926. return;
  927. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  928. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  929. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  930. }
  931. }
  932. /**
  933. * i40evf_process_skb_fields - Populate skb header fields from Rx descriptor
  934. * @rx_ring: rx descriptor ring packet is being transacted on
  935. * @rx_desc: pointer to the EOP Rx descriptor
  936. * @skb: pointer to current skb being populated
  937. * @rx_ptype: the packet type decoded by hardware
  938. *
  939. * This function checks the ring, descriptor, and packet information in
  940. * order to populate the hash, checksum, VLAN, protocol, and
  941. * other fields within the skb.
  942. **/
  943. static inline
  944. void i40evf_process_skb_fields(struct i40e_ring *rx_ring,
  945. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  946. u8 rx_ptype)
  947. {
  948. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  949. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  950. skb_record_rx_queue(skb, rx_ring->queue_index);
  951. /* modifies the skb - consumes the enet header */
  952. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  953. }
  954. /**
  955. * i40e_cleanup_headers - Correct empty headers
  956. * @rx_ring: rx descriptor ring packet is being transacted on
  957. * @skb: pointer to current skb being fixed
  958. *
  959. * Also address the case where we are pulling data in on pages only
  960. * and as such no data is present in the skb header.
  961. *
  962. * In addition if skb is not at least 60 bytes we need to pad it so that
  963. * it is large enough to qualify as a valid Ethernet frame.
  964. *
  965. * Returns true if an error was encountered and skb was freed.
  966. **/
  967. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
  968. {
  969. /* if eth_skb_pad returns an error the skb was freed */
  970. if (eth_skb_pad(skb))
  971. return true;
  972. return false;
  973. }
  974. /**
  975. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  976. * @rx_ring: rx descriptor ring to store buffers on
  977. * @old_buff: donor buffer to have page reused
  978. *
  979. * Synchronizes page for reuse by the adapter
  980. **/
  981. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  982. struct i40e_rx_buffer *old_buff)
  983. {
  984. struct i40e_rx_buffer *new_buff;
  985. u16 nta = rx_ring->next_to_alloc;
  986. new_buff = &rx_ring->rx_bi[nta];
  987. /* update, and store next to alloc */
  988. nta++;
  989. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  990. /* transfer page from old buffer to new buffer */
  991. new_buff->dma = old_buff->dma;
  992. new_buff->page = old_buff->page;
  993. new_buff->page_offset = old_buff->page_offset;
  994. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  995. }
  996. /**
  997. * i40e_page_is_reusable - check if any reuse is possible
  998. * @page: page struct to check
  999. *
  1000. * A page is not reusable if it was allocated under low memory
  1001. * conditions, or it's not in the same NUMA node as this CPU.
  1002. */
  1003. static inline bool i40e_page_is_reusable(struct page *page)
  1004. {
  1005. return (page_to_nid(page) == numa_mem_id()) &&
  1006. !page_is_pfmemalloc(page);
  1007. }
  1008. /**
  1009. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1010. * the adapter for another receive
  1011. *
  1012. * @rx_buffer: buffer containing the page
  1013. *
  1014. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1015. * an unused region in the page.
  1016. *
  1017. * For small pages, @truesize will be a constant value, half the size
  1018. * of the memory at page. We'll attempt to alternate between high and
  1019. * low halves of the page, with one half ready for use by the hardware
  1020. * and the other half being consumed by the stack. We use the page
  1021. * ref count to determine whether the stack has finished consuming the
  1022. * portion of this page that was passed up with a previous packet. If
  1023. * the page ref count is >1, we'll assume the "other" half page is
  1024. * still busy, and this page cannot be reused.
  1025. *
  1026. * For larger pages, @truesize will be the actual space used by the
  1027. * received packet (adjusted upward to an even multiple of the cache
  1028. * line size). This will advance through the page by the amount
  1029. * actually consumed by the received packets while there is still
  1030. * space for a buffer. Each region of larger pages will be used at
  1031. * most once, after which the page will not be reused.
  1032. *
  1033. * In either case, if the page is reusable its refcount is increased.
  1034. **/
  1035. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1036. {
  1037. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1038. struct page *page = rx_buffer->page;
  1039. /* Is any reuse possible? */
  1040. if (unlikely(!i40e_page_is_reusable(page)))
  1041. return false;
  1042. #if (PAGE_SIZE < 8192)
  1043. /* if we are only owner of page we can reuse it */
  1044. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1045. return false;
  1046. #else
  1047. #define I40E_LAST_OFFSET \
  1048. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1049. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1050. return false;
  1051. #endif
  1052. /* If we have drained the page fragment pool we need to update
  1053. * the pagecnt_bias and page count so that we fully restock the
  1054. * number of references the driver holds.
  1055. */
  1056. if (unlikely(!pagecnt_bias)) {
  1057. page_ref_add(page, USHRT_MAX);
  1058. rx_buffer->pagecnt_bias = USHRT_MAX;
  1059. }
  1060. return true;
  1061. }
  1062. /**
  1063. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1064. * @rx_ring: rx descriptor ring to transact packets on
  1065. * @rx_buffer: buffer containing page to add
  1066. * @skb: sk_buff to place the data into
  1067. * @size: packet length from rx_desc
  1068. *
  1069. * This function will add the data contained in rx_buffer->page to the skb.
  1070. * It will just attach the page as a frag to the skb.
  1071. *
  1072. * The function will then update the page offset.
  1073. **/
  1074. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1075. struct i40e_rx_buffer *rx_buffer,
  1076. struct sk_buff *skb,
  1077. unsigned int size)
  1078. {
  1079. #if (PAGE_SIZE < 8192)
  1080. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1081. #else
  1082. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1083. #endif
  1084. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1085. rx_buffer->page_offset, size, truesize);
  1086. /* page is being used so we must update the page offset */
  1087. #if (PAGE_SIZE < 8192)
  1088. rx_buffer->page_offset ^= truesize;
  1089. #else
  1090. rx_buffer->page_offset += truesize;
  1091. #endif
  1092. }
  1093. /**
  1094. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1095. * @rx_ring: rx descriptor ring to transact packets on
  1096. * @size: size of buffer to add to skb
  1097. *
  1098. * This function will pull an Rx buffer from the ring and synchronize it
  1099. * for use by the CPU.
  1100. */
  1101. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1102. const unsigned int size)
  1103. {
  1104. struct i40e_rx_buffer *rx_buffer;
  1105. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1106. prefetchw(rx_buffer->page);
  1107. /* we are reusing so sync this buffer for CPU use */
  1108. dma_sync_single_range_for_cpu(rx_ring->dev,
  1109. rx_buffer->dma,
  1110. rx_buffer->page_offset,
  1111. size,
  1112. DMA_FROM_DEVICE);
  1113. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1114. rx_buffer->pagecnt_bias--;
  1115. return rx_buffer;
  1116. }
  1117. /**
  1118. * i40e_construct_skb - Allocate skb and populate it
  1119. * @rx_ring: rx descriptor ring to transact packets on
  1120. * @rx_buffer: rx buffer to pull data from
  1121. * @size: size of buffer to add to skb
  1122. *
  1123. * This function allocates an skb. It then populates it with the page
  1124. * data from the current receive descriptor, taking care to set up the
  1125. * skb correctly.
  1126. */
  1127. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1128. struct i40e_rx_buffer *rx_buffer,
  1129. unsigned int size)
  1130. {
  1131. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1132. #if (PAGE_SIZE < 8192)
  1133. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1134. #else
  1135. unsigned int truesize = SKB_DATA_ALIGN(size);
  1136. #endif
  1137. unsigned int headlen;
  1138. struct sk_buff *skb;
  1139. /* prefetch first cache line of first page */
  1140. prefetch(va);
  1141. #if L1_CACHE_BYTES < 128
  1142. prefetch(va + L1_CACHE_BYTES);
  1143. #endif
  1144. /* allocate a skb to store the frags */
  1145. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1146. I40E_RX_HDR_SIZE,
  1147. GFP_ATOMIC | __GFP_NOWARN);
  1148. if (unlikely(!skb))
  1149. return NULL;
  1150. /* Determine available headroom for copy */
  1151. headlen = size;
  1152. if (headlen > I40E_RX_HDR_SIZE)
  1153. headlen = eth_get_headlen(va, I40E_RX_HDR_SIZE);
  1154. /* align pull length to size of long to optimize memcpy performance */
  1155. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  1156. /* update all of the pointers */
  1157. size -= headlen;
  1158. if (size) {
  1159. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1160. rx_buffer->page_offset + headlen,
  1161. size, truesize);
  1162. /* buffer is used by skb, update page_offset */
  1163. #if (PAGE_SIZE < 8192)
  1164. rx_buffer->page_offset ^= truesize;
  1165. #else
  1166. rx_buffer->page_offset += truesize;
  1167. #endif
  1168. } else {
  1169. /* buffer is unused, reset bias back to rx_buffer */
  1170. rx_buffer->pagecnt_bias++;
  1171. }
  1172. return skb;
  1173. }
  1174. /**
  1175. * i40e_build_skb - Build skb around an existing buffer
  1176. * @rx_ring: Rx descriptor ring to transact packets on
  1177. * @rx_buffer: Rx buffer to pull data from
  1178. * @size: size of buffer to add to skb
  1179. *
  1180. * This function builds an skb around an existing Rx buffer, taking care
  1181. * to set up the skb correctly and avoid any memcpy overhead.
  1182. */
  1183. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1184. struct i40e_rx_buffer *rx_buffer,
  1185. unsigned int size)
  1186. {
  1187. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1188. #if (PAGE_SIZE < 8192)
  1189. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1190. #else
  1191. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1192. SKB_DATA_ALIGN(I40E_SKB_PAD + size);
  1193. #endif
  1194. struct sk_buff *skb;
  1195. /* prefetch first cache line of first page */
  1196. prefetch(va);
  1197. #if L1_CACHE_BYTES < 128
  1198. prefetch(va + L1_CACHE_BYTES);
  1199. #endif
  1200. /* build an skb around the page buffer */
  1201. skb = build_skb(va - I40E_SKB_PAD, truesize);
  1202. if (unlikely(!skb))
  1203. return NULL;
  1204. /* update pointers within the skb to store the data */
  1205. skb_reserve(skb, I40E_SKB_PAD);
  1206. __skb_put(skb, size);
  1207. /* buffer is used by skb, update page_offset */
  1208. #if (PAGE_SIZE < 8192)
  1209. rx_buffer->page_offset ^= truesize;
  1210. #else
  1211. rx_buffer->page_offset += truesize;
  1212. #endif
  1213. return skb;
  1214. }
  1215. /**
  1216. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1217. * @rx_ring: rx descriptor ring to transact packets on
  1218. * @rx_buffer: rx buffer to pull data from
  1219. *
  1220. * This function will clean up the contents of the rx_buffer. It will
  1221. * either recycle the buffer or unmap it and free the associated resources.
  1222. */
  1223. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1224. struct i40e_rx_buffer *rx_buffer)
  1225. {
  1226. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1227. /* hand second half of page back to the ring */
  1228. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1229. rx_ring->rx_stats.page_reuse_count++;
  1230. } else {
  1231. /* we are not reusing the buffer so unmap it */
  1232. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1233. i40e_rx_pg_size(rx_ring),
  1234. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1235. __page_frag_cache_drain(rx_buffer->page,
  1236. rx_buffer->pagecnt_bias);
  1237. }
  1238. /* clear contents of buffer_info */
  1239. rx_buffer->page = NULL;
  1240. }
  1241. /**
  1242. * i40e_is_non_eop - process handling of non-EOP buffers
  1243. * @rx_ring: Rx ring being processed
  1244. * @rx_desc: Rx descriptor for current buffer
  1245. * @skb: Current socket buffer containing buffer in progress
  1246. *
  1247. * This function updates next to clean. If the buffer is an EOP buffer
  1248. * this function exits returning false, otherwise it will place the
  1249. * sk_buff in the next buffer to be chained and return true indicating
  1250. * that this is in fact a non-EOP buffer.
  1251. **/
  1252. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1253. union i40e_rx_desc *rx_desc,
  1254. struct sk_buff *skb)
  1255. {
  1256. u32 ntc = rx_ring->next_to_clean + 1;
  1257. /* fetch, update, and store next to clean */
  1258. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1259. rx_ring->next_to_clean = ntc;
  1260. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1261. /* if we are the last buffer then there is nothing else to do */
  1262. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1263. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1264. return false;
  1265. rx_ring->rx_stats.non_eop_descs++;
  1266. return true;
  1267. }
  1268. /**
  1269. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1270. * @rx_ring: rx descriptor ring to transact packets on
  1271. * @budget: Total limit on number of packets to process
  1272. *
  1273. * This function provides a "bounce buffer" approach to Rx interrupt
  1274. * processing. The advantage to this is that on systems that have
  1275. * expensive overhead for IOMMU access this provides a means of avoiding
  1276. * it by maintaining the mapping of the page to the system.
  1277. *
  1278. * Returns amount of work completed
  1279. **/
  1280. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1281. {
  1282. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1283. struct sk_buff *skb = rx_ring->skb;
  1284. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1285. bool failure = false;
  1286. while (likely(total_rx_packets < (unsigned int)budget)) {
  1287. struct i40e_rx_buffer *rx_buffer;
  1288. union i40e_rx_desc *rx_desc;
  1289. unsigned int size;
  1290. u16 vlan_tag;
  1291. u8 rx_ptype;
  1292. u64 qword;
  1293. /* return some buffers to hardware, one at a time is too slow */
  1294. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1295. failure = failure ||
  1296. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  1297. cleaned_count = 0;
  1298. }
  1299. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1300. /* status_error_len will always be zero for unused descriptors
  1301. * because it's cleared in cleanup, and overlaps with hdr_addr
  1302. * which is always zero because packet split isn't used, if the
  1303. * hardware wrote DD then the length will be non-zero
  1304. */
  1305. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1306. /* This memory barrier is needed to keep us from reading
  1307. * any other fields out of the rx_desc until we have
  1308. * verified the descriptor has been written back.
  1309. */
  1310. dma_rmb();
  1311. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1312. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1313. if (!size)
  1314. break;
  1315. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1316. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1317. /* retrieve a buffer from the ring */
  1318. if (skb)
  1319. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1320. else if (ring_uses_build_skb(rx_ring))
  1321. skb = i40e_build_skb(rx_ring, rx_buffer, size);
  1322. else
  1323. skb = i40e_construct_skb(rx_ring, rx_buffer, size);
  1324. /* exit if we failed to retrieve a buffer */
  1325. if (!skb) {
  1326. rx_ring->rx_stats.alloc_buff_failed++;
  1327. rx_buffer->pagecnt_bias++;
  1328. break;
  1329. }
  1330. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1331. cleaned_count++;
  1332. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1333. continue;
  1334. /* ERR_MASK will only have valid bits if EOP set, and
  1335. * what we are doing here is actually checking
  1336. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1337. * the error field
  1338. */
  1339. if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1340. dev_kfree_skb_any(skb);
  1341. skb = NULL;
  1342. continue;
  1343. }
  1344. if (i40e_cleanup_headers(rx_ring, skb)) {
  1345. skb = NULL;
  1346. continue;
  1347. }
  1348. /* probably a little skewed due to removing CRC */
  1349. total_rx_bytes += skb->len;
  1350. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1351. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1352. I40E_RXD_QW1_PTYPE_SHIFT;
  1353. /* populate checksum, VLAN, and protocol */
  1354. i40evf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1355. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1356. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1357. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1358. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1359. skb = NULL;
  1360. /* update budget accounting */
  1361. total_rx_packets++;
  1362. }
  1363. rx_ring->skb = skb;
  1364. u64_stats_update_begin(&rx_ring->syncp);
  1365. rx_ring->stats.packets += total_rx_packets;
  1366. rx_ring->stats.bytes += total_rx_bytes;
  1367. u64_stats_update_end(&rx_ring->syncp);
  1368. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1369. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1370. /* guarantee a trip back through this routine if there was a failure */
  1371. return failure ? budget : (int)total_rx_packets;
  1372. }
  1373. static inline u32 i40e_buildreg_itr(const int type, u16 itr)
  1374. {
  1375. u32 val;
  1376. /* We don't bother with setting the CLEARPBA bit as the data sheet
  1377. * points out doing so is "meaningless since it was already
  1378. * auto-cleared". The auto-clearing happens when the interrupt is
  1379. * asserted.
  1380. *
  1381. * Hardware errata 28 for also indicates that writing to a
  1382. * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear
  1383. * an event in the PBA anyway so we need to rely on the automask
  1384. * to hold pending events for us until the interrupt is re-enabled
  1385. *
  1386. * The itr value is reported in microseconds, and the register
  1387. * value is recorded in 2 microsecond units. For this reason we
  1388. * only need to shift by the interval shift - 1 instead of the
  1389. * full value.
  1390. */
  1391. itr &= I40E_ITR_MASK;
  1392. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1393. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1394. (itr << (I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT - 1));
  1395. return val;
  1396. }
  1397. /* a small macro to shorten up some long lines */
  1398. #define INTREG I40E_VFINT_DYN_CTLN1
  1399. /* The act of updating the ITR will cause it to immediately trigger. In order
  1400. * to prevent this from throwing off adaptive update statistics we defer the
  1401. * update so that it can only happen so often. So after either Tx or Rx are
  1402. * updated we make the adaptive scheme wait until either the ITR completely
  1403. * expires via the next_update expiration or we have been through at least
  1404. * 3 interrupts.
  1405. */
  1406. #define ITR_COUNTDOWN_START 3
  1407. /**
  1408. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1409. * @vsi: the VSI we care about
  1410. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1411. *
  1412. **/
  1413. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1414. struct i40e_q_vector *q_vector)
  1415. {
  1416. struct i40e_hw *hw = &vsi->back->hw;
  1417. u32 intval;
  1418. /* These will do nothing if dynamic updates are not enabled */
  1419. i40e_update_itr(q_vector, &q_vector->tx);
  1420. i40e_update_itr(q_vector, &q_vector->rx);
  1421. /* This block of logic allows us to get away with only updating
  1422. * one ITR value with each interrupt. The idea is to perform a
  1423. * pseudo-lazy update with the following criteria.
  1424. *
  1425. * 1. Rx is given higher priority than Tx if both are in same state
  1426. * 2. If we must reduce an ITR that is given highest priority.
  1427. * 3. We then give priority to increasing ITR based on amount.
  1428. */
  1429. if (q_vector->rx.target_itr < q_vector->rx.current_itr) {
  1430. /* Rx ITR needs to be reduced, this is highest priority */
  1431. intval = i40e_buildreg_itr(I40E_RX_ITR,
  1432. q_vector->rx.target_itr);
  1433. q_vector->rx.current_itr = q_vector->rx.target_itr;
  1434. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1435. } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) ||
  1436. ((q_vector->rx.target_itr - q_vector->rx.current_itr) <
  1437. (q_vector->tx.target_itr - q_vector->tx.current_itr))) {
  1438. /* Tx ITR needs to be reduced, this is second priority
  1439. * Tx ITR needs to be increased more than Rx, fourth priority
  1440. */
  1441. intval = i40e_buildreg_itr(I40E_TX_ITR,
  1442. q_vector->tx.target_itr);
  1443. q_vector->tx.current_itr = q_vector->tx.target_itr;
  1444. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1445. } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) {
  1446. /* Rx ITR needs to be increased, third priority */
  1447. intval = i40e_buildreg_itr(I40E_RX_ITR,
  1448. q_vector->rx.target_itr);
  1449. q_vector->rx.current_itr = q_vector->rx.target_itr;
  1450. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1451. } else {
  1452. /* No ITR update, lowest priority */
  1453. intval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1454. if (q_vector->itr_countdown)
  1455. q_vector->itr_countdown--;
  1456. }
  1457. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1458. wr32(hw, INTREG(q_vector->reg_idx), intval);
  1459. }
  1460. /**
  1461. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1462. * @napi: napi struct with our devices info in it
  1463. * @budget: amount of work driver is allowed to do this pass, in packets
  1464. *
  1465. * This function will clean all queues associated with a q_vector.
  1466. *
  1467. * Returns the amount of work done
  1468. **/
  1469. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1470. {
  1471. struct i40e_q_vector *q_vector =
  1472. container_of(napi, struct i40e_q_vector, napi);
  1473. struct i40e_vsi *vsi = q_vector->vsi;
  1474. struct i40e_ring *ring;
  1475. bool clean_complete = true;
  1476. bool arm_wb = false;
  1477. int budget_per_ring;
  1478. int work_done = 0;
  1479. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  1480. napi_complete(napi);
  1481. return 0;
  1482. }
  1483. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1484. * budget and be more aggressive about cleaning up the Tx descriptors.
  1485. */
  1486. i40e_for_each_ring(ring, q_vector->tx) {
  1487. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  1488. clean_complete = false;
  1489. continue;
  1490. }
  1491. arm_wb |= ring->arm_wb;
  1492. ring->arm_wb = false;
  1493. }
  1494. /* Handle case where we are called by netpoll with a budget of 0 */
  1495. if (budget <= 0)
  1496. goto tx_only;
  1497. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1498. * allow the budget to go below 1 because that would exit polling early.
  1499. */
  1500. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1501. i40e_for_each_ring(ring, q_vector->rx) {
  1502. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  1503. work_done += cleaned;
  1504. /* if we clean as many as budgeted, we must not be done */
  1505. if (cleaned >= budget_per_ring)
  1506. clean_complete = false;
  1507. }
  1508. /* If work not completed, return budget and polling will return */
  1509. if (!clean_complete) {
  1510. int cpu_id = smp_processor_id();
  1511. /* It is possible that the interrupt affinity has changed but,
  1512. * if the cpu is pegged at 100%, polling will never exit while
  1513. * traffic continues and the interrupt will be stuck on this
  1514. * cpu. We check to make sure affinity is correct before we
  1515. * continue to poll, otherwise we must stop polling so the
  1516. * interrupt can move to the correct cpu.
  1517. */
  1518. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  1519. /* Tell napi that we are done polling */
  1520. napi_complete_done(napi, work_done);
  1521. /* Force an interrupt */
  1522. i40evf_force_wb(vsi, q_vector);
  1523. /* Return budget-1 so that polling stops */
  1524. return budget - 1;
  1525. }
  1526. tx_only:
  1527. if (arm_wb) {
  1528. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1529. i40e_enable_wb_on_itr(vsi, q_vector);
  1530. }
  1531. return budget;
  1532. }
  1533. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1534. q_vector->arm_wb_state = false;
  1535. /* Work is done so exit the polling mode and re-enable the interrupt */
  1536. napi_complete_done(napi, work_done);
  1537. i40e_update_enable_itr(vsi, q_vector);
  1538. return min(work_done, budget - 1);
  1539. }
  1540. /**
  1541. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1542. * @skb: send buffer
  1543. * @tx_ring: ring to send buffer on
  1544. * @flags: the tx flags to be set
  1545. *
  1546. * Checks the skb and set up correspondingly several generic transmit flags
  1547. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1548. *
  1549. * Returns error code indicate the frame should be dropped upon error and the
  1550. * otherwise returns 0 to indicate the flags has been set properly.
  1551. **/
  1552. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1553. struct i40e_ring *tx_ring,
  1554. u32 *flags)
  1555. {
  1556. __be16 protocol = skb->protocol;
  1557. u32 tx_flags = 0;
  1558. if (protocol == htons(ETH_P_8021Q) &&
  1559. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1560. /* When HW VLAN acceleration is turned off by the user the
  1561. * stack sets the protocol to 8021q so that the driver
  1562. * can take any steps required to support the SW only
  1563. * VLAN handling. In our case the driver doesn't need
  1564. * to take any further steps so just set the protocol
  1565. * to the encapsulated ethertype.
  1566. */
  1567. skb->protocol = vlan_get_protocol(skb);
  1568. goto out;
  1569. }
  1570. /* if we have a HW VLAN tag being added, default to the HW one */
  1571. if (skb_vlan_tag_present(skb)) {
  1572. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1573. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1574. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1575. } else if (protocol == htons(ETH_P_8021Q)) {
  1576. struct vlan_hdr *vhdr, _vhdr;
  1577. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1578. if (!vhdr)
  1579. return -EINVAL;
  1580. protocol = vhdr->h_vlan_encapsulated_proto;
  1581. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1582. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1583. }
  1584. out:
  1585. *flags = tx_flags;
  1586. return 0;
  1587. }
  1588. /**
  1589. * i40e_tso - set up the tso context descriptor
  1590. * @first: pointer to first Tx buffer for xmit
  1591. * @hdr_len: ptr to the size of the packet header
  1592. * @cd_type_cmd_tso_mss: Quad Word 1
  1593. *
  1594. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1595. **/
  1596. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  1597. u64 *cd_type_cmd_tso_mss)
  1598. {
  1599. struct sk_buff *skb = first->skb;
  1600. u64 cd_cmd, cd_tso_len, cd_mss;
  1601. union {
  1602. struct iphdr *v4;
  1603. struct ipv6hdr *v6;
  1604. unsigned char *hdr;
  1605. } ip;
  1606. union {
  1607. struct tcphdr *tcp;
  1608. struct udphdr *udp;
  1609. unsigned char *hdr;
  1610. } l4;
  1611. u32 paylen, l4_offset;
  1612. u16 gso_segs, gso_size;
  1613. int err;
  1614. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1615. return 0;
  1616. if (!skb_is_gso(skb))
  1617. return 0;
  1618. err = skb_cow_head(skb, 0);
  1619. if (err < 0)
  1620. return err;
  1621. ip.hdr = skb_network_header(skb);
  1622. l4.hdr = skb_transport_header(skb);
  1623. /* initialize outer IP header fields */
  1624. if (ip.v4->version == 4) {
  1625. ip.v4->tot_len = 0;
  1626. ip.v4->check = 0;
  1627. } else {
  1628. ip.v6->payload_len = 0;
  1629. }
  1630. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1631. SKB_GSO_GRE_CSUM |
  1632. SKB_GSO_IPXIP4 |
  1633. SKB_GSO_IPXIP6 |
  1634. SKB_GSO_UDP_TUNNEL |
  1635. SKB_GSO_UDP_TUNNEL_CSUM)) {
  1636. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1637. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  1638. l4.udp->len = 0;
  1639. /* determine offset of outer transport header */
  1640. l4_offset = l4.hdr - skb->data;
  1641. /* remove payload length from outer checksum */
  1642. paylen = skb->len - l4_offset;
  1643. csum_replace_by_diff(&l4.udp->check,
  1644. (__force __wsum)htonl(paylen));
  1645. }
  1646. /* reset pointers to inner headers */
  1647. ip.hdr = skb_inner_network_header(skb);
  1648. l4.hdr = skb_inner_transport_header(skb);
  1649. /* initialize inner IP header fields */
  1650. if (ip.v4->version == 4) {
  1651. ip.v4->tot_len = 0;
  1652. ip.v4->check = 0;
  1653. } else {
  1654. ip.v6->payload_len = 0;
  1655. }
  1656. }
  1657. /* determine offset of inner transport header */
  1658. l4_offset = l4.hdr - skb->data;
  1659. /* remove payload length from inner checksum */
  1660. paylen = skb->len - l4_offset;
  1661. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  1662. /* compute length of segmentation header */
  1663. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  1664. /* pull values out of skb_shinfo */
  1665. gso_size = skb_shinfo(skb)->gso_size;
  1666. gso_segs = skb_shinfo(skb)->gso_segs;
  1667. /* update GSO size and bytecount with header size */
  1668. first->gso_segs = gso_segs;
  1669. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  1670. /* find the field values */
  1671. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1672. cd_tso_len = skb->len - *hdr_len;
  1673. cd_mss = gso_size;
  1674. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1675. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1676. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1677. return 1;
  1678. }
  1679. /**
  1680. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1681. * @skb: send buffer
  1682. * @tx_flags: pointer to Tx flags currently set
  1683. * @td_cmd: Tx descriptor command bits to set
  1684. * @td_offset: Tx descriptor header offsets to set
  1685. * @tx_ring: Tx descriptor ring
  1686. * @cd_tunneling: ptr to context desc bits
  1687. **/
  1688. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1689. u32 *td_cmd, u32 *td_offset,
  1690. struct i40e_ring *tx_ring,
  1691. u32 *cd_tunneling)
  1692. {
  1693. union {
  1694. struct iphdr *v4;
  1695. struct ipv6hdr *v6;
  1696. unsigned char *hdr;
  1697. } ip;
  1698. union {
  1699. struct tcphdr *tcp;
  1700. struct udphdr *udp;
  1701. unsigned char *hdr;
  1702. } l4;
  1703. unsigned char *exthdr;
  1704. u32 offset, cmd = 0;
  1705. __be16 frag_off;
  1706. u8 l4_proto = 0;
  1707. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1708. return 0;
  1709. ip.hdr = skb_network_header(skb);
  1710. l4.hdr = skb_transport_header(skb);
  1711. /* compute outer L2 header size */
  1712. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1713. if (skb->encapsulation) {
  1714. u32 tunnel = 0;
  1715. /* define outer network header type */
  1716. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1717. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1718. I40E_TX_CTX_EXT_IP_IPV4 :
  1719. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1720. l4_proto = ip.v4->protocol;
  1721. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1722. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  1723. exthdr = ip.hdr + sizeof(*ip.v6);
  1724. l4_proto = ip.v6->nexthdr;
  1725. if (l4.hdr != exthdr)
  1726. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1727. &l4_proto, &frag_off);
  1728. }
  1729. /* define outer transport */
  1730. switch (l4_proto) {
  1731. case IPPROTO_UDP:
  1732. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  1733. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1734. break;
  1735. case IPPROTO_GRE:
  1736. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  1737. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1738. break;
  1739. case IPPROTO_IPIP:
  1740. case IPPROTO_IPV6:
  1741. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1742. l4.hdr = skb_inner_network_header(skb);
  1743. break;
  1744. default:
  1745. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1746. return -1;
  1747. skb_checksum_help(skb);
  1748. return 0;
  1749. }
  1750. /* compute outer L3 header size */
  1751. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  1752. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  1753. /* switch IP header pointer from outer to inner header */
  1754. ip.hdr = skb_inner_network_header(skb);
  1755. /* compute tunnel header size */
  1756. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  1757. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1758. /* indicate if we need to offload outer UDP header */
  1759. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  1760. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1761. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  1762. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1763. /* record tunnel offload values */
  1764. *cd_tunneling |= tunnel;
  1765. /* switch L4 header pointer from outer to inner */
  1766. l4.hdr = skb_inner_transport_header(skb);
  1767. l4_proto = 0;
  1768. /* reset type as we transition from outer to inner headers */
  1769. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  1770. if (ip.v4->version == 4)
  1771. *tx_flags |= I40E_TX_FLAGS_IPV4;
  1772. if (ip.v6->version == 6)
  1773. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1774. }
  1775. /* Enable IP checksum offloads */
  1776. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1777. l4_proto = ip.v4->protocol;
  1778. /* the stack computes the IP header already, the only time we
  1779. * need the hardware to recompute it is in the case of TSO.
  1780. */
  1781. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1782. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  1783. I40E_TX_DESC_CMD_IIPT_IPV4;
  1784. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1785. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1786. exthdr = ip.hdr + sizeof(*ip.v6);
  1787. l4_proto = ip.v6->nexthdr;
  1788. if (l4.hdr != exthdr)
  1789. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1790. &l4_proto, &frag_off);
  1791. }
  1792. /* compute inner L3 header size */
  1793. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1794. /* Enable L4 checksum offloads */
  1795. switch (l4_proto) {
  1796. case IPPROTO_TCP:
  1797. /* enable checksum offloads */
  1798. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1799. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1800. break;
  1801. case IPPROTO_SCTP:
  1802. /* enable SCTP checksum offload */
  1803. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1804. offset |= (sizeof(struct sctphdr) >> 2) <<
  1805. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1806. break;
  1807. case IPPROTO_UDP:
  1808. /* enable UDP checksum offload */
  1809. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1810. offset |= (sizeof(struct udphdr) >> 2) <<
  1811. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1812. break;
  1813. default:
  1814. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1815. return -1;
  1816. skb_checksum_help(skb);
  1817. return 0;
  1818. }
  1819. *td_cmd |= cmd;
  1820. *td_offset |= offset;
  1821. return 1;
  1822. }
  1823. /**
  1824. * i40e_create_tx_ctx Build the Tx context descriptor
  1825. * @tx_ring: ring to create the descriptor on
  1826. * @cd_type_cmd_tso_mss: Quad Word 1
  1827. * @cd_tunneling: Quad Word 0 - bits 0-31
  1828. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1829. **/
  1830. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1831. const u64 cd_type_cmd_tso_mss,
  1832. const u32 cd_tunneling, const u32 cd_l2tag2)
  1833. {
  1834. struct i40e_tx_context_desc *context_desc;
  1835. int i = tx_ring->next_to_use;
  1836. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1837. !cd_tunneling && !cd_l2tag2)
  1838. return;
  1839. /* grab the next descriptor */
  1840. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1841. i++;
  1842. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1843. /* cpu_to_le32 and assign to struct fields */
  1844. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1845. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1846. context_desc->rsvd = cpu_to_le16(0);
  1847. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1848. }
  1849. /**
  1850. * __i40evf_chk_linearize - Check if there are more than 8 buffers per packet
  1851. * @skb: send buffer
  1852. *
  1853. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  1854. * and so we need to figure out the cases where we need to linearize the skb.
  1855. *
  1856. * For TSO we need to count the TSO header and segment payload separately.
  1857. * As such we need to check cases where we have 7 fragments or more as we
  1858. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  1859. * the segment payload in the first descriptor, and another 7 for the
  1860. * fragments.
  1861. **/
  1862. bool __i40evf_chk_linearize(struct sk_buff *skb)
  1863. {
  1864. const struct skb_frag_struct *frag, *stale;
  1865. int nr_frags, sum;
  1866. /* no need to check if number of frags is less than 7 */
  1867. nr_frags = skb_shinfo(skb)->nr_frags;
  1868. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  1869. return false;
  1870. /* We need to walk through the list and validate that each group
  1871. * of 6 fragments totals at least gso_size.
  1872. */
  1873. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  1874. frag = &skb_shinfo(skb)->frags[0];
  1875. /* Initialize size to the negative value of gso_size minus 1. We
  1876. * use this as the worst case scenerio in which the frag ahead
  1877. * of us only provides one byte which is why we are limited to 6
  1878. * descriptors for a single transmit as the header and previous
  1879. * fragment are already consuming 2 descriptors.
  1880. */
  1881. sum = 1 - skb_shinfo(skb)->gso_size;
  1882. /* Add size of frags 0 through 4 to create our initial sum */
  1883. sum += skb_frag_size(frag++);
  1884. sum += skb_frag_size(frag++);
  1885. sum += skb_frag_size(frag++);
  1886. sum += skb_frag_size(frag++);
  1887. sum += skb_frag_size(frag++);
  1888. /* Walk through fragments adding latest fragment, testing it, and
  1889. * then removing stale fragments from the sum.
  1890. */
  1891. for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
  1892. int stale_size = skb_frag_size(stale);
  1893. sum += skb_frag_size(frag++);
  1894. /* The stale fragment may present us with a smaller
  1895. * descriptor than the actual fragment size. To account
  1896. * for that we need to remove all the data on the front and
  1897. * figure out what the remainder would be in the last
  1898. * descriptor associated with the fragment.
  1899. */
  1900. if (stale_size > I40E_MAX_DATA_PER_TXD) {
  1901. int align_pad = -(stale->page_offset) &
  1902. (I40E_MAX_READ_REQ_SIZE - 1);
  1903. sum -= align_pad;
  1904. stale_size -= align_pad;
  1905. do {
  1906. sum -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  1907. stale_size -= I40E_MAX_DATA_PER_TXD_ALIGNED;
  1908. } while (stale_size > I40E_MAX_DATA_PER_TXD);
  1909. }
  1910. /* if sum is negative we failed to make sufficient progress */
  1911. if (sum < 0)
  1912. return true;
  1913. if (!nr_frags--)
  1914. break;
  1915. sum -= stale_size;
  1916. }
  1917. return false;
  1918. }
  1919. /**
  1920. * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
  1921. * @tx_ring: the ring to be checked
  1922. * @size: the size buffer we want to assure is available
  1923. *
  1924. * Returns -EBUSY if a stop is needed, else 0
  1925. **/
  1926. int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1927. {
  1928. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1929. /* Memory barrier before checking head and tail */
  1930. smp_mb();
  1931. /* Check again in a case another CPU has just made room available. */
  1932. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1933. return -EBUSY;
  1934. /* A reprieve! - use start_queue because it doesn't call schedule */
  1935. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1936. ++tx_ring->tx_stats.restart_queue;
  1937. return 0;
  1938. }
  1939. /**
  1940. * i40evf_tx_map - Build the Tx descriptor
  1941. * @tx_ring: ring to send buffer on
  1942. * @skb: send buffer
  1943. * @first: first buffer info buffer to use
  1944. * @tx_flags: collected send information
  1945. * @hdr_len: size of the packet header
  1946. * @td_cmd: the command field in the descriptor
  1947. * @td_offset: offset for checksum or crc
  1948. **/
  1949. static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1950. struct i40e_tx_buffer *first, u32 tx_flags,
  1951. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1952. {
  1953. unsigned int data_len = skb->data_len;
  1954. unsigned int size = skb_headlen(skb);
  1955. struct skb_frag_struct *frag;
  1956. struct i40e_tx_buffer *tx_bi;
  1957. struct i40e_tx_desc *tx_desc;
  1958. u16 i = tx_ring->next_to_use;
  1959. u32 td_tag = 0;
  1960. dma_addr_t dma;
  1961. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1962. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1963. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1964. I40E_TX_FLAGS_VLAN_SHIFT;
  1965. }
  1966. first->tx_flags = tx_flags;
  1967. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1968. tx_desc = I40E_TX_DESC(tx_ring, i);
  1969. tx_bi = first;
  1970. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1971. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1972. if (dma_mapping_error(tx_ring->dev, dma))
  1973. goto dma_error;
  1974. /* record length, and DMA address */
  1975. dma_unmap_len_set(tx_bi, len, size);
  1976. dma_unmap_addr_set(tx_bi, dma, dma);
  1977. /* align size to end of page */
  1978. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  1979. tx_desc->buffer_addr = cpu_to_le64(dma);
  1980. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1981. tx_desc->cmd_type_offset_bsz =
  1982. build_ctob(td_cmd, td_offset,
  1983. max_data, td_tag);
  1984. tx_desc++;
  1985. i++;
  1986. if (i == tx_ring->count) {
  1987. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1988. i = 0;
  1989. }
  1990. dma += max_data;
  1991. size -= max_data;
  1992. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1993. tx_desc->buffer_addr = cpu_to_le64(dma);
  1994. }
  1995. if (likely(!data_len))
  1996. break;
  1997. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1998. size, td_tag);
  1999. tx_desc++;
  2000. i++;
  2001. if (i == tx_ring->count) {
  2002. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2003. i = 0;
  2004. }
  2005. size = skb_frag_size(frag);
  2006. data_len -= size;
  2007. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2008. DMA_TO_DEVICE);
  2009. tx_bi = &tx_ring->tx_bi[i];
  2010. }
  2011. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2012. i++;
  2013. if (i == tx_ring->count)
  2014. i = 0;
  2015. tx_ring->next_to_use = i;
  2016. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2017. /* write last descriptor with RS and EOP bits */
  2018. td_cmd |= I40E_TXD_CMD;
  2019. tx_desc->cmd_type_offset_bsz =
  2020. build_ctob(td_cmd, td_offset, size, td_tag);
  2021. /* Force memory writes to complete before letting h/w know there
  2022. * are new descriptors to fetch.
  2023. *
  2024. * We also use this memory barrier to make certain all of the
  2025. * status bits have been updated before next_to_watch is written.
  2026. */
  2027. wmb();
  2028. /* set next_to_watch value indicating a packet is present */
  2029. first->next_to_watch = tx_desc;
  2030. /* notify HW of packet */
  2031. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  2032. writel(i, tx_ring->tail);
  2033. /* we need this if more than one processor can write to our tail
  2034. * at a time, it synchronizes IO on IA64/Altix systems
  2035. */
  2036. mmiowb();
  2037. }
  2038. return;
  2039. dma_error:
  2040. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2041. /* clear dma mappings for failed tx_bi map */
  2042. for (;;) {
  2043. tx_bi = &tx_ring->tx_bi[i];
  2044. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2045. if (tx_bi == first)
  2046. break;
  2047. if (i == 0)
  2048. i = tx_ring->count;
  2049. i--;
  2050. }
  2051. tx_ring->next_to_use = i;
  2052. }
  2053. /**
  2054. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  2055. * @skb: send buffer
  2056. * @tx_ring: ring to send buffer on
  2057. *
  2058. * Returns NETDEV_TX_OK if sent, else an error code
  2059. **/
  2060. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  2061. struct i40e_ring *tx_ring)
  2062. {
  2063. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  2064. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  2065. struct i40e_tx_buffer *first;
  2066. u32 td_offset = 0;
  2067. u32 tx_flags = 0;
  2068. __be16 protocol;
  2069. u32 td_cmd = 0;
  2070. u8 hdr_len = 0;
  2071. int tso, count;
  2072. /* prefetch the data, we'll need it later */
  2073. prefetch(skb->data);
  2074. i40e_trace(xmit_frame_ring, skb, tx_ring);
  2075. count = i40e_xmit_descriptor_count(skb);
  2076. if (i40e_chk_linearize(skb, count)) {
  2077. if (__skb_linearize(skb)) {
  2078. dev_kfree_skb_any(skb);
  2079. return NETDEV_TX_OK;
  2080. }
  2081. count = i40e_txd_use_count(skb->len);
  2082. tx_ring->tx_stats.tx_linearize++;
  2083. }
  2084. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  2085. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  2086. * + 4 desc gap to avoid the cache line where head is,
  2087. * + 1 desc for context descriptor,
  2088. * otherwise try next time
  2089. */
  2090. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  2091. tx_ring->tx_stats.tx_busy++;
  2092. return NETDEV_TX_BUSY;
  2093. }
  2094. /* record the location of the first descriptor for this packet */
  2095. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  2096. first->skb = skb;
  2097. first->bytecount = skb->len;
  2098. first->gso_segs = 1;
  2099. /* prepare the xmit flags */
  2100. if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  2101. goto out_drop;
  2102. /* obtain protocol of skb */
  2103. protocol = vlan_get_protocol(skb);
  2104. /* setup IPv4/IPv6 offloads */
  2105. if (protocol == htons(ETH_P_IP))
  2106. tx_flags |= I40E_TX_FLAGS_IPV4;
  2107. else if (protocol == htons(ETH_P_IPV6))
  2108. tx_flags |= I40E_TX_FLAGS_IPV6;
  2109. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  2110. if (tso < 0)
  2111. goto out_drop;
  2112. else if (tso)
  2113. tx_flags |= I40E_TX_FLAGS_TSO;
  2114. /* Always offload the checksum, since it's in the data descriptor */
  2115. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  2116. tx_ring, &cd_tunneling);
  2117. if (tso < 0)
  2118. goto out_drop;
  2119. skb_tx_timestamp(skb);
  2120. /* always enable CRC insertion offload */
  2121. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  2122. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  2123. cd_tunneling, cd_l2tag2);
  2124. i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  2125. td_cmd, td_offset);
  2126. return NETDEV_TX_OK;
  2127. out_drop:
  2128. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  2129. dev_kfree_skb_any(first->skb);
  2130. first->skb = NULL;
  2131. return NETDEV_TX_OK;
  2132. }
  2133. /**
  2134. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  2135. * @skb: send buffer
  2136. * @netdev: network interface device structure
  2137. *
  2138. * Returns NETDEV_TX_OK if sent, else an error code
  2139. **/
  2140. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2141. {
  2142. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2143. struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
  2144. /* hardware can't handle really short frames, hardware padding works
  2145. * beyond this point
  2146. */
  2147. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  2148. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  2149. return NETDEV_TX_OK;
  2150. skb->len = I40E_MIN_TX_LEN;
  2151. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  2152. }
  2153. return i40e_xmit_frame_ring(skb, tx_ring);
  2154. }