i40e_txrx.c 70 KB

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