i40e_txrx.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include <net/busy_poll.h>
  28. #include "i40evf.h"
  29. #include "i40e_prototype.h"
  30. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  31. u32 td_tag)
  32. {
  33. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  34. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  35. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  36. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  37. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  38. }
  39. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  40. /**
  41. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  42. * @ring: the ring that owns the buffer
  43. * @tx_buffer: the buffer to free
  44. **/
  45. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  46. struct i40e_tx_buffer *tx_buffer)
  47. {
  48. if (tx_buffer->skb) {
  49. dev_kfree_skb_any(tx_buffer->skb);
  50. if (dma_unmap_len(tx_buffer, len))
  51. dma_unmap_single(ring->dev,
  52. dma_unmap_addr(tx_buffer, dma),
  53. dma_unmap_len(tx_buffer, len),
  54. DMA_TO_DEVICE);
  55. } else if (dma_unmap_len(tx_buffer, len)) {
  56. dma_unmap_page(ring->dev,
  57. dma_unmap_addr(tx_buffer, dma),
  58. dma_unmap_len(tx_buffer, len),
  59. DMA_TO_DEVICE);
  60. }
  61. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  62. kfree(tx_buffer->raw_buf);
  63. tx_buffer->next_to_watch = NULL;
  64. tx_buffer->skb = NULL;
  65. dma_unmap_len_set(tx_buffer, len, 0);
  66. /* tx_buffer must be completely set up in the transmit path */
  67. }
  68. /**
  69. * i40evf_clean_tx_ring - Free any empty Tx buffers
  70. * @tx_ring: ring to be cleaned
  71. **/
  72. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  73. {
  74. unsigned long bi_size;
  75. u16 i;
  76. /* ring already cleared, nothing to do */
  77. if (!tx_ring->tx_bi)
  78. return;
  79. /* Free all the Tx ring sk_buffs */
  80. for (i = 0; i < tx_ring->count; i++)
  81. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  82. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  83. memset(tx_ring->tx_bi, 0, bi_size);
  84. /* Zero out the descriptor ring */
  85. memset(tx_ring->desc, 0, tx_ring->size);
  86. tx_ring->next_to_use = 0;
  87. tx_ring->next_to_clean = 0;
  88. if (!tx_ring->netdev)
  89. return;
  90. /* cleanup Tx queue statistics */
  91. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  92. tx_ring->queue_index));
  93. }
  94. /**
  95. * i40evf_free_tx_resources - Free Tx resources per queue
  96. * @tx_ring: Tx descriptor ring for a specific queue
  97. *
  98. * Free all transmit software resources
  99. **/
  100. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  101. {
  102. i40evf_clean_tx_ring(tx_ring);
  103. kfree(tx_ring->tx_bi);
  104. tx_ring->tx_bi = NULL;
  105. if (tx_ring->desc) {
  106. dma_free_coherent(tx_ring->dev, tx_ring->size,
  107. tx_ring->desc, tx_ring->dma);
  108. tx_ring->desc = NULL;
  109. }
  110. }
  111. /**
  112. * i40evf_get_tx_pending - how many Tx descriptors not processed
  113. * @tx_ring: the ring of descriptors
  114. *
  115. * Since there is no access to the ring head register
  116. * in XL710, we need to use our local copies
  117. **/
  118. u32 i40evf_get_tx_pending(struct i40e_ring *ring)
  119. {
  120. u32 head, tail;
  121. head = i40e_get_head(ring);
  122. tail = readl(ring->tail);
  123. if (head != tail)
  124. return (head < tail) ?
  125. tail - head : (tail + ring->count - head);
  126. return 0;
  127. }
  128. #define WB_STRIDE 0x3
  129. /**
  130. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  131. * @tx_ring: tx ring to clean
  132. * @budget: how many cleans we're allowed
  133. *
  134. * Returns true if there's any budget left (e.g. the clean is finished)
  135. **/
  136. static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
  137. {
  138. u16 i = tx_ring->next_to_clean;
  139. struct i40e_tx_buffer *tx_buf;
  140. struct i40e_tx_desc *tx_head;
  141. struct i40e_tx_desc *tx_desc;
  142. unsigned int total_packets = 0;
  143. unsigned int total_bytes = 0;
  144. tx_buf = &tx_ring->tx_bi[i];
  145. tx_desc = I40E_TX_DESC(tx_ring, i);
  146. i -= tx_ring->count;
  147. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  148. do {
  149. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  150. /* if next_to_watch is not set then there is no work pending */
  151. if (!eop_desc)
  152. break;
  153. /* prevent any other reads prior to eop_desc */
  154. read_barrier_depends();
  155. /* we have caught up to head, no work left to do */
  156. if (tx_head == tx_desc)
  157. break;
  158. /* clear next_to_watch to prevent false hangs */
  159. tx_buf->next_to_watch = NULL;
  160. /* update the statistics for this packet */
  161. total_bytes += tx_buf->bytecount;
  162. total_packets += tx_buf->gso_segs;
  163. /* free the skb */
  164. dev_kfree_skb_any(tx_buf->skb);
  165. /* unmap skb header data */
  166. dma_unmap_single(tx_ring->dev,
  167. dma_unmap_addr(tx_buf, dma),
  168. dma_unmap_len(tx_buf, len),
  169. DMA_TO_DEVICE);
  170. /* clear tx_buffer data */
  171. tx_buf->skb = NULL;
  172. dma_unmap_len_set(tx_buf, len, 0);
  173. /* unmap remaining buffers */
  174. while (tx_desc != eop_desc) {
  175. tx_buf++;
  176. tx_desc++;
  177. i++;
  178. if (unlikely(!i)) {
  179. i -= tx_ring->count;
  180. tx_buf = tx_ring->tx_bi;
  181. tx_desc = I40E_TX_DESC(tx_ring, 0);
  182. }
  183. /* unmap any remaining paged data */
  184. if (dma_unmap_len(tx_buf, len)) {
  185. dma_unmap_page(tx_ring->dev,
  186. dma_unmap_addr(tx_buf, dma),
  187. dma_unmap_len(tx_buf, len),
  188. DMA_TO_DEVICE);
  189. dma_unmap_len_set(tx_buf, len, 0);
  190. }
  191. }
  192. /* move us one more past the eop_desc for start of next pkt */
  193. tx_buf++;
  194. tx_desc++;
  195. i++;
  196. if (unlikely(!i)) {
  197. i -= tx_ring->count;
  198. tx_buf = tx_ring->tx_bi;
  199. tx_desc = I40E_TX_DESC(tx_ring, 0);
  200. }
  201. prefetch(tx_desc);
  202. /* update budget accounting */
  203. budget--;
  204. } while (likely(budget));
  205. i += tx_ring->count;
  206. tx_ring->next_to_clean = i;
  207. u64_stats_update_begin(&tx_ring->syncp);
  208. tx_ring->stats.bytes += total_bytes;
  209. tx_ring->stats.packets += total_packets;
  210. u64_stats_update_end(&tx_ring->syncp);
  211. tx_ring->q_vector->tx.total_bytes += total_bytes;
  212. tx_ring->q_vector->tx.total_packets += total_packets;
  213. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  214. unsigned int j = 0;
  215. /* check to see if there are < 4 descriptors
  216. * waiting to be written back, then kick the hardware to force
  217. * them to be written back in case we stay in NAPI.
  218. * In this mode on X722 we do not enable Interrupt.
  219. */
  220. j = i40evf_get_tx_pending(tx_ring);
  221. if (budget &&
  222. ((j / (WB_STRIDE + 1)) == 0) && (j > 0) &&
  223. !test_bit(__I40E_DOWN, &tx_ring->vsi->state) &&
  224. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  225. tx_ring->arm_wb = true;
  226. }
  227. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  228. tx_ring->queue_index),
  229. total_packets, total_bytes);
  230. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  231. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  232. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  233. /* Make sure that anybody stopping the queue after this
  234. * sees the new next_to_clean.
  235. */
  236. smp_mb();
  237. if (__netif_subqueue_stopped(tx_ring->netdev,
  238. tx_ring->queue_index) &&
  239. !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
  240. netif_wake_subqueue(tx_ring->netdev,
  241. tx_ring->queue_index);
  242. ++tx_ring->tx_stats.restart_queue;
  243. }
  244. }
  245. return !!budget;
  246. }
  247. /**
  248. * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  249. * @vsi: the VSI we care about
  250. * @q_vector: the vector on which to enable writeback
  251. *
  252. **/
  253. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  254. struct i40e_q_vector *q_vector)
  255. {
  256. u16 flags = q_vector->tx.ring[0].flags;
  257. u32 val;
  258. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  259. return;
  260. if (q_vector->arm_wb_state)
  261. return;
  262. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
  263. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
  264. wr32(&vsi->back->hw,
  265. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  266. vsi->base_vector - 1), val);
  267. q_vector->arm_wb_state = true;
  268. }
  269. /**
  270. * i40evf_force_wb - Issue SW Interrupt so HW does a wb
  271. * @vsi: the VSI we care about
  272. * @q_vector: the vector on which to force writeback
  273. *
  274. **/
  275. void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  276. {
  277. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  278. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  279. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  280. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
  281. /* allow 00 to be written to the index */;
  282. wr32(&vsi->back->hw,
  283. I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
  284. val);
  285. }
  286. /**
  287. * i40e_set_new_dynamic_itr - Find new ITR level
  288. * @rc: structure containing ring performance data
  289. *
  290. * Returns true if ITR changed, false if not
  291. *
  292. * Stores a new ITR value based on packets and byte counts during
  293. * the last interrupt. The advantage of per interrupt computation
  294. * is faster updates and more accurate ITR for the current traffic
  295. * pattern. Constants in this function were computed based on
  296. * theoretical maximum wire speed and thresholds were set based on
  297. * testing data as well as attempting to minimize response time
  298. * while increasing bulk throughput.
  299. **/
  300. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  301. {
  302. enum i40e_latency_range new_latency_range = rc->latency_range;
  303. struct i40e_q_vector *qv = rc->ring->q_vector;
  304. u32 new_itr = rc->itr;
  305. int bytes_per_int;
  306. int usecs;
  307. if (rc->total_packets == 0 || !rc->itr)
  308. return false;
  309. /* simple throttlerate management
  310. * 0-10MB/s lowest (50000 ints/s)
  311. * 10-20MB/s low (20000 ints/s)
  312. * 20-1249MB/s bulk (18000 ints/s)
  313. * > 40000 Rx packets per second (8000 ints/s)
  314. *
  315. * The math works out because the divisor is in 10^(-6) which
  316. * turns the bytes/us input value into MB/s values, but
  317. * make sure to use usecs, as the register values written
  318. * are in 2 usec increments in the ITR registers, and make sure
  319. * to use the smoothed values that the countdown timer gives us.
  320. */
  321. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  322. bytes_per_int = rc->total_bytes / usecs;
  323. switch (new_latency_range) {
  324. case I40E_LOWEST_LATENCY:
  325. if (bytes_per_int > 10)
  326. new_latency_range = I40E_LOW_LATENCY;
  327. break;
  328. case I40E_LOW_LATENCY:
  329. if (bytes_per_int > 20)
  330. new_latency_range = I40E_BULK_LATENCY;
  331. else if (bytes_per_int <= 10)
  332. new_latency_range = I40E_LOWEST_LATENCY;
  333. break;
  334. case I40E_BULK_LATENCY:
  335. case I40E_ULTRA_LATENCY:
  336. default:
  337. if (bytes_per_int <= 20)
  338. new_latency_range = I40E_LOW_LATENCY;
  339. break;
  340. }
  341. /* this is to adjust RX more aggressively when streaming small
  342. * packets. The value of 40000 was picked as it is just beyond
  343. * what the hardware can receive per second if in low latency
  344. * mode.
  345. */
  346. #define RX_ULTRA_PACKET_RATE 40000
  347. if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
  348. (&qv->rx == rc))
  349. new_latency_range = I40E_ULTRA_LATENCY;
  350. rc->latency_range = new_latency_range;
  351. switch (new_latency_range) {
  352. case I40E_LOWEST_LATENCY:
  353. new_itr = I40E_ITR_50K;
  354. break;
  355. case I40E_LOW_LATENCY:
  356. new_itr = I40E_ITR_20K;
  357. break;
  358. case I40E_BULK_LATENCY:
  359. new_itr = I40E_ITR_18K;
  360. break;
  361. case I40E_ULTRA_LATENCY:
  362. new_itr = I40E_ITR_8K;
  363. break;
  364. default:
  365. break;
  366. }
  367. rc->total_bytes = 0;
  368. rc->total_packets = 0;
  369. if (new_itr != rc->itr) {
  370. rc->itr = new_itr;
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  377. * @tx_ring: the tx ring to set up
  378. *
  379. * Return 0 on success, negative on error
  380. **/
  381. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  382. {
  383. struct device *dev = tx_ring->dev;
  384. int bi_size;
  385. if (!dev)
  386. return -ENOMEM;
  387. /* warn if we are about to overwrite the pointer */
  388. WARN_ON(tx_ring->tx_bi);
  389. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  390. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  391. if (!tx_ring->tx_bi)
  392. goto err;
  393. /* round up to nearest 4K */
  394. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  395. /* add u32 for head writeback, align after this takes care of
  396. * guaranteeing this is at least one cache line in size
  397. */
  398. tx_ring->size += sizeof(u32);
  399. tx_ring->size = ALIGN(tx_ring->size, 4096);
  400. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  401. &tx_ring->dma, GFP_KERNEL);
  402. if (!tx_ring->desc) {
  403. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  404. tx_ring->size);
  405. goto err;
  406. }
  407. tx_ring->next_to_use = 0;
  408. tx_ring->next_to_clean = 0;
  409. return 0;
  410. err:
  411. kfree(tx_ring->tx_bi);
  412. tx_ring->tx_bi = NULL;
  413. return -ENOMEM;
  414. }
  415. /**
  416. * i40evf_clean_rx_ring - Free Rx buffers
  417. * @rx_ring: ring to be cleaned
  418. **/
  419. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  420. {
  421. struct device *dev = rx_ring->dev;
  422. struct i40e_rx_buffer *rx_bi;
  423. unsigned long bi_size;
  424. u16 i;
  425. /* ring already cleared, nothing to do */
  426. if (!rx_ring->rx_bi)
  427. return;
  428. if (ring_is_ps_enabled(rx_ring)) {
  429. int bufsz = ALIGN(rx_ring->rx_hdr_len, 256) * rx_ring->count;
  430. rx_bi = &rx_ring->rx_bi[0];
  431. if (rx_bi->hdr_buf) {
  432. dma_free_coherent(dev,
  433. bufsz,
  434. rx_bi->hdr_buf,
  435. rx_bi->dma);
  436. for (i = 0; i < rx_ring->count; i++) {
  437. rx_bi = &rx_ring->rx_bi[i];
  438. rx_bi->dma = 0;
  439. rx_bi->hdr_buf = NULL;
  440. }
  441. }
  442. }
  443. /* Free all the Rx ring sk_buffs */
  444. for (i = 0; i < rx_ring->count; i++) {
  445. rx_bi = &rx_ring->rx_bi[i];
  446. if (rx_bi->dma) {
  447. dma_unmap_single(dev,
  448. rx_bi->dma,
  449. rx_ring->rx_buf_len,
  450. DMA_FROM_DEVICE);
  451. rx_bi->dma = 0;
  452. }
  453. if (rx_bi->skb) {
  454. dev_kfree_skb(rx_bi->skb);
  455. rx_bi->skb = NULL;
  456. }
  457. if (rx_bi->page) {
  458. if (rx_bi->page_dma) {
  459. dma_unmap_page(dev,
  460. rx_bi->page_dma,
  461. PAGE_SIZE,
  462. DMA_FROM_DEVICE);
  463. rx_bi->page_dma = 0;
  464. }
  465. __free_page(rx_bi->page);
  466. rx_bi->page = NULL;
  467. rx_bi->page_offset = 0;
  468. }
  469. }
  470. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  471. memset(rx_ring->rx_bi, 0, bi_size);
  472. /* Zero out the descriptor ring */
  473. memset(rx_ring->desc, 0, rx_ring->size);
  474. rx_ring->next_to_clean = 0;
  475. rx_ring->next_to_use = 0;
  476. }
  477. /**
  478. * i40evf_free_rx_resources - Free Rx resources
  479. * @rx_ring: ring to clean the resources from
  480. *
  481. * Free all receive software resources
  482. **/
  483. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  484. {
  485. i40evf_clean_rx_ring(rx_ring);
  486. kfree(rx_ring->rx_bi);
  487. rx_ring->rx_bi = NULL;
  488. if (rx_ring->desc) {
  489. dma_free_coherent(rx_ring->dev, rx_ring->size,
  490. rx_ring->desc, rx_ring->dma);
  491. rx_ring->desc = NULL;
  492. }
  493. }
  494. /**
  495. * i40evf_alloc_rx_headers - allocate rx header buffers
  496. * @rx_ring: ring to alloc buffers
  497. *
  498. * Allocate rx header buffers for the entire ring. As these are static,
  499. * this is only called when setting up a new ring.
  500. **/
  501. void i40evf_alloc_rx_headers(struct i40e_ring *rx_ring)
  502. {
  503. struct device *dev = rx_ring->dev;
  504. struct i40e_rx_buffer *rx_bi;
  505. dma_addr_t dma;
  506. void *buffer;
  507. int buf_size;
  508. int i;
  509. if (rx_ring->rx_bi[0].hdr_buf)
  510. return;
  511. /* Make sure the buffers don't cross cache line boundaries. */
  512. buf_size = ALIGN(rx_ring->rx_hdr_len, 256);
  513. buffer = dma_alloc_coherent(dev, buf_size * rx_ring->count,
  514. &dma, GFP_KERNEL);
  515. if (!buffer)
  516. return;
  517. for (i = 0; i < rx_ring->count; i++) {
  518. rx_bi = &rx_ring->rx_bi[i];
  519. rx_bi->dma = dma + (i * buf_size);
  520. rx_bi->hdr_buf = buffer + (i * buf_size);
  521. }
  522. }
  523. /**
  524. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  525. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  526. *
  527. * Returns 0 on success, negative on failure
  528. **/
  529. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  530. {
  531. struct device *dev = rx_ring->dev;
  532. int bi_size;
  533. /* warn if we are about to overwrite the pointer */
  534. WARN_ON(rx_ring->rx_bi);
  535. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  536. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  537. if (!rx_ring->rx_bi)
  538. goto err;
  539. u64_stats_init(&rx_ring->syncp);
  540. /* Round up to nearest 4K */
  541. rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
  542. ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
  543. : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  544. rx_ring->size = ALIGN(rx_ring->size, 4096);
  545. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  546. &rx_ring->dma, GFP_KERNEL);
  547. if (!rx_ring->desc) {
  548. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  549. rx_ring->size);
  550. goto err;
  551. }
  552. rx_ring->next_to_clean = 0;
  553. rx_ring->next_to_use = 0;
  554. return 0;
  555. err:
  556. kfree(rx_ring->rx_bi);
  557. rx_ring->rx_bi = NULL;
  558. return -ENOMEM;
  559. }
  560. /**
  561. * i40e_release_rx_desc - Store the new tail and head values
  562. * @rx_ring: ring to bump
  563. * @val: new head index
  564. **/
  565. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  566. {
  567. rx_ring->next_to_use = val;
  568. /* Force memory writes to complete before letting h/w
  569. * know there are new descriptors to fetch. (Only
  570. * applicable for weak-ordered memory model archs,
  571. * such as IA-64).
  572. */
  573. wmb();
  574. writel(val, rx_ring->tail);
  575. }
  576. /**
  577. * i40evf_alloc_rx_buffers_ps - Replace used receive buffers; packet split
  578. * @rx_ring: ring to place buffers on
  579. * @cleaned_count: number of buffers to replace
  580. *
  581. * Returns true if any errors on allocation
  582. **/
  583. bool i40evf_alloc_rx_buffers_ps(struct i40e_ring *rx_ring, u16 cleaned_count)
  584. {
  585. u16 i = rx_ring->next_to_use;
  586. union i40e_rx_desc *rx_desc;
  587. struct i40e_rx_buffer *bi;
  588. const int current_node = numa_node_id();
  589. /* do nothing if no valid netdev defined */
  590. if (!rx_ring->netdev || !cleaned_count)
  591. return false;
  592. while (cleaned_count--) {
  593. rx_desc = I40E_RX_DESC(rx_ring, i);
  594. bi = &rx_ring->rx_bi[i];
  595. if (bi->skb) /* desc is in use */
  596. goto no_buffers;
  597. /* If we've been moved to a different NUMA node, release the
  598. * page so we can get a new one on the current node.
  599. */
  600. if (bi->page && page_to_nid(bi->page) != current_node) {
  601. dma_unmap_page(rx_ring->dev,
  602. bi->page_dma,
  603. PAGE_SIZE,
  604. DMA_FROM_DEVICE);
  605. __free_page(bi->page);
  606. bi->page = NULL;
  607. bi->page_dma = 0;
  608. rx_ring->rx_stats.realloc_count++;
  609. } else if (bi->page) {
  610. rx_ring->rx_stats.page_reuse_count++;
  611. }
  612. if (!bi->page) {
  613. bi->page = alloc_page(GFP_ATOMIC);
  614. if (!bi->page) {
  615. rx_ring->rx_stats.alloc_page_failed++;
  616. goto no_buffers;
  617. }
  618. bi->page_dma = dma_map_page(rx_ring->dev,
  619. bi->page,
  620. 0,
  621. PAGE_SIZE,
  622. DMA_FROM_DEVICE);
  623. if (dma_mapping_error(rx_ring->dev, bi->page_dma)) {
  624. rx_ring->rx_stats.alloc_page_failed++;
  625. __free_page(bi->page);
  626. bi->page = NULL;
  627. bi->page_dma = 0;
  628. bi->page_offset = 0;
  629. goto no_buffers;
  630. }
  631. bi->page_offset = 0;
  632. }
  633. /* Refresh the desc even if buffer_addrs didn't change
  634. * because each write-back erases this info.
  635. */
  636. rx_desc->read.pkt_addr =
  637. cpu_to_le64(bi->page_dma + bi->page_offset);
  638. rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
  639. i++;
  640. if (i == rx_ring->count)
  641. i = 0;
  642. }
  643. if (rx_ring->next_to_use != i)
  644. i40e_release_rx_desc(rx_ring, i);
  645. return false;
  646. no_buffers:
  647. if (rx_ring->next_to_use != i)
  648. i40e_release_rx_desc(rx_ring, i);
  649. /* make sure to come back via polling to try again after
  650. * allocation failure
  651. */
  652. return true;
  653. }
  654. /**
  655. * i40evf_alloc_rx_buffers_1buf - Replace used receive buffers; single buffer
  656. * @rx_ring: ring to place buffers on
  657. * @cleaned_count: number of buffers to replace
  658. *
  659. * Returns true if any errors on allocation
  660. **/
  661. bool i40evf_alloc_rx_buffers_1buf(struct i40e_ring *rx_ring, u16 cleaned_count)
  662. {
  663. u16 i = rx_ring->next_to_use;
  664. union i40e_rx_desc *rx_desc;
  665. struct i40e_rx_buffer *bi;
  666. struct sk_buff *skb;
  667. /* do nothing if no valid netdev defined */
  668. if (!rx_ring->netdev || !cleaned_count)
  669. return false;
  670. while (cleaned_count--) {
  671. rx_desc = I40E_RX_DESC(rx_ring, i);
  672. bi = &rx_ring->rx_bi[i];
  673. skb = bi->skb;
  674. if (!skb) {
  675. skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
  676. rx_ring->rx_buf_len,
  677. GFP_ATOMIC |
  678. __GFP_NOWARN);
  679. if (!skb) {
  680. rx_ring->rx_stats.alloc_buff_failed++;
  681. goto no_buffers;
  682. }
  683. /* initialize queue mapping */
  684. skb_record_rx_queue(skb, rx_ring->queue_index);
  685. bi->skb = skb;
  686. }
  687. if (!bi->dma) {
  688. bi->dma = dma_map_single(rx_ring->dev,
  689. skb->data,
  690. rx_ring->rx_buf_len,
  691. DMA_FROM_DEVICE);
  692. if (dma_mapping_error(rx_ring->dev, bi->dma)) {
  693. rx_ring->rx_stats.alloc_buff_failed++;
  694. bi->dma = 0;
  695. dev_kfree_skb(bi->skb);
  696. bi->skb = NULL;
  697. goto no_buffers;
  698. }
  699. }
  700. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  701. rx_desc->read.hdr_addr = 0;
  702. i++;
  703. if (i == rx_ring->count)
  704. i = 0;
  705. }
  706. if (rx_ring->next_to_use != i)
  707. i40e_release_rx_desc(rx_ring, i);
  708. return false;
  709. no_buffers:
  710. if (rx_ring->next_to_use != i)
  711. i40e_release_rx_desc(rx_ring, i);
  712. /* make sure to come back via polling to try again after
  713. * allocation failure
  714. */
  715. return true;
  716. }
  717. /**
  718. * i40e_receive_skb - Send a completed packet up the stack
  719. * @rx_ring: rx ring in play
  720. * @skb: packet to send up
  721. * @vlan_tag: vlan tag for packet
  722. **/
  723. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  724. struct sk_buff *skb, u16 vlan_tag)
  725. {
  726. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  727. if (vlan_tag & VLAN_VID_MASK)
  728. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  729. napi_gro_receive(&q_vector->napi, skb);
  730. }
  731. /**
  732. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  733. * @vsi: the VSI we care about
  734. * @skb: skb currently being received and modified
  735. * @rx_status: status value of last descriptor in packet
  736. * @rx_error: error value of last descriptor in packet
  737. * @rx_ptype: ptype value of last descriptor in packet
  738. **/
  739. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  740. struct sk_buff *skb,
  741. u32 rx_status,
  742. u32 rx_error,
  743. u16 rx_ptype)
  744. {
  745. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
  746. bool ipv4 = false, ipv6 = false;
  747. bool ipv4_tunnel, ipv6_tunnel;
  748. __wsum rx_udp_csum;
  749. struct iphdr *iph;
  750. __sum16 csum;
  751. ipv4_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
  752. (rx_ptype <= I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
  753. ipv6_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
  754. (rx_ptype <= I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
  755. skb->ip_summed = CHECKSUM_NONE;
  756. /* Rx csum enabled and ip headers found? */
  757. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  758. return;
  759. /* did the hardware decode the packet and checksum? */
  760. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  761. return;
  762. /* both known and outer_ip must be set for the below code to work */
  763. if (!(decoded.known && decoded.outer_ip))
  764. return;
  765. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  766. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
  767. ipv4 = true;
  768. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  769. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
  770. ipv6 = true;
  771. if (ipv4 &&
  772. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  773. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  774. goto checksum_fail;
  775. /* likely incorrect csum if alternate IP extension headers found */
  776. if (ipv6 &&
  777. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  778. /* don't increment checksum err here, non-fatal err */
  779. return;
  780. /* there was some L4 error, count error and punt packet to the stack */
  781. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  782. goto checksum_fail;
  783. /* handle packets that were not able to be checksummed due
  784. * to arrival speed, in this case the stack can compute
  785. * the csum.
  786. */
  787. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  788. return;
  789. /* If VXLAN traffic has an outer UDPv4 checksum we need to check
  790. * it in the driver, hardware does not do it for us.
  791. * Since L3L4P bit was set we assume a valid IHL value (>=5)
  792. * so the total length of IPv4 header is IHL*4 bytes
  793. * The UDP_0 bit *may* bet set if the *inner* header is UDP
  794. */
  795. if (ipv4_tunnel) {
  796. skb->transport_header = skb->mac_header +
  797. sizeof(struct ethhdr) +
  798. (ip_hdr(skb)->ihl * 4);
  799. /* Add 4 bytes for VLAN tagged packets */
  800. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  801. skb->protocol == htons(ETH_P_8021AD))
  802. ? VLAN_HLEN : 0;
  803. if ((ip_hdr(skb)->protocol == IPPROTO_UDP) &&
  804. (udp_hdr(skb)->check != 0)) {
  805. rx_udp_csum = udp_csum(skb);
  806. iph = ip_hdr(skb);
  807. csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
  808. (skb->len -
  809. skb_transport_offset(skb)),
  810. IPPROTO_UDP, rx_udp_csum);
  811. if (udp_hdr(skb)->check != csum)
  812. goto checksum_fail;
  813. } /* else its GRE and so no outer UDP header */
  814. }
  815. skb->ip_summed = CHECKSUM_UNNECESSARY;
  816. skb->csum_level = ipv4_tunnel || ipv6_tunnel;
  817. return;
  818. checksum_fail:
  819. vsi->back->hw_csum_rx_error++;
  820. }
  821. /**
  822. * i40e_ptype_to_htype - get a hash type
  823. * @ptype: the ptype value from the descriptor
  824. *
  825. * Returns a hash type to be used by skb_set_hash
  826. **/
  827. static inline enum pkt_hash_types i40e_ptype_to_htype(u8 ptype)
  828. {
  829. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  830. if (!decoded.known)
  831. return PKT_HASH_TYPE_NONE;
  832. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  833. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  834. return PKT_HASH_TYPE_L4;
  835. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  836. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  837. return PKT_HASH_TYPE_L3;
  838. else
  839. return PKT_HASH_TYPE_L2;
  840. }
  841. /**
  842. * i40e_rx_hash - set the hash value in the skb
  843. * @ring: descriptor ring
  844. * @rx_desc: specific descriptor
  845. **/
  846. static inline void i40e_rx_hash(struct i40e_ring *ring,
  847. union i40e_rx_desc *rx_desc,
  848. struct sk_buff *skb,
  849. u8 rx_ptype)
  850. {
  851. u32 hash;
  852. const __le64 rss_mask =
  853. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  854. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  855. if (ring->netdev->features & NETIF_F_RXHASH)
  856. return;
  857. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  858. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  859. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  860. }
  861. }
  862. /**
  863. * i40e_clean_rx_irq_ps - Reclaim resources after receive; packet split
  864. * @rx_ring: rx ring to clean
  865. * @budget: how many cleans we're allowed
  866. *
  867. * Returns true if there's any budget left (e.g. the clean is finished)
  868. **/
  869. static int i40e_clean_rx_irq_ps(struct i40e_ring *rx_ring, const int budget)
  870. {
  871. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  872. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  873. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  874. struct i40e_vsi *vsi = rx_ring->vsi;
  875. u16 i = rx_ring->next_to_clean;
  876. union i40e_rx_desc *rx_desc;
  877. u32 rx_error, rx_status;
  878. bool failure = false;
  879. u8 rx_ptype;
  880. u64 qword;
  881. u32 copysize;
  882. do {
  883. struct i40e_rx_buffer *rx_bi;
  884. struct sk_buff *skb;
  885. u16 vlan_tag;
  886. /* return some buffers to hardware, one at a time is too slow */
  887. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  888. failure = failure ||
  889. i40evf_alloc_rx_buffers_ps(rx_ring,
  890. cleaned_count);
  891. cleaned_count = 0;
  892. }
  893. i = rx_ring->next_to_clean;
  894. rx_desc = I40E_RX_DESC(rx_ring, i);
  895. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  896. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  897. I40E_RXD_QW1_STATUS_SHIFT;
  898. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
  899. break;
  900. /* This memory barrier is needed to keep us from reading
  901. * any other fields out of the rx_desc until we know the
  902. * DD bit is set.
  903. */
  904. dma_rmb();
  905. /* sync header buffer for reading */
  906. dma_sync_single_range_for_cpu(rx_ring->dev,
  907. rx_ring->rx_bi[0].dma,
  908. i * rx_ring->rx_hdr_len,
  909. rx_ring->rx_hdr_len,
  910. DMA_FROM_DEVICE);
  911. rx_bi = &rx_ring->rx_bi[i];
  912. skb = rx_bi->skb;
  913. if (likely(!skb)) {
  914. skb = __netdev_alloc_skb_ip_align(rx_ring->netdev,
  915. rx_ring->rx_hdr_len,
  916. GFP_ATOMIC |
  917. __GFP_NOWARN);
  918. if (!skb) {
  919. rx_ring->rx_stats.alloc_buff_failed++;
  920. failure = true;
  921. break;
  922. }
  923. /* initialize queue mapping */
  924. skb_record_rx_queue(skb, rx_ring->queue_index);
  925. /* we are reusing so sync this buffer for CPU use */
  926. dma_sync_single_range_for_cpu(rx_ring->dev,
  927. rx_ring->rx_bi[0].dma,
  928. i * rx_ring->rx_hdr_len,
  929. rx_ring->rx_hdr_len,
  930. DMA_FROM_DEVICE);
  931. }
  932. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  933. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  934. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  935. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  936. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  937. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  938. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  939. I40E_RXD_QW1_ERROR_SHIFT;
  940. rx_hbo = rx_error & BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  941. rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  942. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  943. I40E_RXD_QW1_PTYPE_SHIFT;
  944. /* sync half-page for reading */
  945. dma_sync_single_range_for_cpu(rx_ring->dev,
  946. rx_bi->page_dma,
  947. rx_bi->page_offset,
  948. PAGE_SIZE / 2,
  949. DMA_FROM_DEVICE);
  950. prefetch(page_address(rx_bi->page) + rx_bi->page_offset);
  951. rx_bi->skb = NULL;
  952. cleaned_count++;
  953. copysize = 0;
  954. if (rx_hbo || rx_sph) {
  955. int len;
  956. if (rx_hbo)
  957. len = I40E_RX_HDR_SIZE;
  958. else
  959. len = rx_header_len;
  960. memcpy(__skb_put(skb, len), rx_bi->hdr_buf, len);
  961. } else if (skb->len == 0) {
  962. int len;
  963. unsigned char *va = page_address(rx_bi->page) +
  964. rx_bi->page_offset;
  965. len = min(rx_packet_len, rx_ring->rx_hdr_len);
  966. memcpy(__skb_put(skb, len), va, len);
  967. copysize = len;
  968. rx_packet_len -= len;
  969. }
  970. /* Get the rest of the data if this was a header split */
  971. if (rx_packet_len) {
  972. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  973. rx_bi->page,
  974. rx_bi->page_offset + copysize,
  975. rx_packet_len, I40E_RXBUFFER_2048);
  976. get_page(rx_bi->page);
  977. /* switch to the other half-page here; the allocation
  978. * code programs the right addr into HW. If we haven't
  979. * used this half-page, the address won't be changed,
  980. * and HW can just use it next time through.
  981. */
  982. rx_bi->page_offset ^= PAGE_SIZE / 2;
  983. /* If the page count is more than 2, then both halves
  984. * of the page are used and we need to free it. Do it
  985. * here instead of in the alloc code. Otherwise one
  986. * of the half-pages might be released between now and
  987. * then, and we wouldn't know which one to use.
  988. */
  989. if (page_count(rx_bi->page) > 2) {
  990. dma_unmap_page(rx_ring->dev,
  991. rx_bi->page_dma,
  992. PAGE_SIZE,
  993. DMA_FROM_DEVICE);
  994. __free_page(rx_bi->page);
  995. rx_bi->page = NULL;
  996. rx_bi->page_dma = 0;
  997. rx_ring->rx_stats.realloc_count++;
  998. }
  999. }
  1000. I40E_RX_INCREMENT(rx_ring, i);
  1001. if (unlikely(
  1002. !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1003. struct i40e_rx_buffer *next_buffer;
  1004. next_buffer = &rx_ring->rx_bi[i];
  1005. next_buffer->skb = skb;
  1006. rx_ring->rx_stats.non_eop_descs++;
  1007. continue;
  1008. }
  1009. /* ERR_MASK will only have valid bits if EOP set */
  1010. if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1011. dev_kfree_skb_any(skb);
  1012. continue;
  1013. }
  1014. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1015. /* probably a little skewed due to removing CRC */
  1016. total_rx_bytes += skb->len;
  1017. total_rx_packets++;
  1018. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1019. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1020. vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1021. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1022. : 0;
  1023. #ifdef I40E_FCOE
  1024. if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
  1025. dev_kfree_skb_any(skb);
  1026. continue;
  1027. }
  1028. #endif
  1029. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1030. rx_desc->wb.qword1.status_error_len = 0;
  1031. } while (likely(total_rx_packets < budget));
  1032. u64_stats_update_begin(&rx_ring->syncp);
  1033. rx_ring->stats.packets += total_rx_packets;
  1034. rx_ring->stats.bytes += total_rx_bytes;
  1035. u64_stats_update_end(&rx_ring->syncp);
  1036. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1037. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1038. return failure ? budget : total_rx_packets;
  1039. }
  1040. /**
  1041. * i40e_clean_rx_irq_1buf - Reclaim resources after receive; single buffer
  1042. * @rx_ring: rx ring to clean
  1043. * @budget: how many cleans we're allowed
  1044. *
  1045. * Returns number of packets cleaned
  1046. **/
  1047. static int i40e_clean_rx_irq_1buf(struct i40e_ring *rx_ring, int budget)
  1048. {
  1049. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1050. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1051. struct i40e_vsi *vsi = rx_ring->vsi;
  1052. union i40e_rx_desc *rx_desc;
  1053. u32 rx_error, rx_status;
  1054. u16 rx_packet_len;
  1055. bool failure = false;
  1056. u8 rx_ptype;
  1057. u64 qword;
  1058. u16 i;
  1059. do {
  1060. struct i40e_rx_buffer *rx_bi;
  1061. struct sk_buff *skb;
  1062. u16 vlan_tag;
  1063. /* return some buffers to hardware, one at a time is too slow */
  1064. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1065. failure = failure ||
  1066. i40evf_alloc_rx_buffers_1buf(rx_ring,
  1067. cleaned_count);
  1068. cleaned_count = 0;
  1069. }
  1070. i = rx_ring->next_to_clean;
  1071. rx_desc = I40E_RX_DESC(rx_ring, i);
  1072. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1073. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1074. I40E_RXD_QW1_STATUS_SHIFT;
  1075. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
  1076. break;
  1077. /* This memory barrier is needed to keep us from reading
  1078. * any other fields out of the rx_desc until we know the
  1079. * DD bit is set.
  1080. */
  1081. dma_rmb();
  1082. rx_bi = &rx_ring->rx_bi[i];
  1083. skb = rx_bi->skb;
  1084. prefetch(skb->data);
  1085. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1086. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1087. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1088. I40E_RXD_QW1_ERROR_SHIFT;
  1089. rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  1090. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1091. I40E_RXD_QW1_PTYPE_SHIFT;
  1092. rx_bi->skb = NULL;
  1093. cleaned_count++;
  1094. /* Get the header and possibly the whole packet
  1095. * If this is an skb from previous receive dma will be 0
  1096. */
  1097. skb_put(skb, rx_packet_len);
  1098. dma_unmap_single(rx_ring->dev, rx_bi->dma, rx_ring->rx_buf_len,
  1099. DMA_FROM_DEVICE);
  1100. rx_bi->dma = 0;
  1101. I40E_RX_INCREMENT(rx_ring, i);
  1102. if (unlikely(
  1103. !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1104. rx_ring->rx_stats.non_eop_descs++;
  1105. continue;
  1106. }
  1107. /* ERR_MASK will only have valid bits if EOP set */
  1108. if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1109. dev_kfree_skb_any(skb);
  1110. continue;
  1111. }
  1112. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1113. /* probably a little skewed due to removing CRC */
  1114. total_rx_bytes += skb->len;
  1115. total_rx_packets++;
  1116. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1117. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1118. vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1119. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1120. : 0;
  1121. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1122. rx_desc->wb.qword1.status_error_len = 0;
  1123. } while (likely(total_rx_packets < budget));
  1124. u64_stats_update_begin(&rx_ring->syncp);
  1125. rx_ring->stats.packets += total_rx_packets;
  1126. rx_ring->stats.bytes += total_rx_bytes;
  1127. u64_stats_update_end(&rx_ring->syncp);
  1128. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1129. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1130. return failure ? budget : total_rx_packets;
  1131. }
  1132. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1133. {
  1134. u32 val;
  1135. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1136. /* Don't clear PBA because that can cause lost interrupts that
  1137. * came in while we were cleaning/polling
  1138. */
  1139. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1140. (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
  1141. return val;
  1142. }
  1143. /* a small macro to shorten up some long lines */
  1144. #define INTREG I40E_VFINT_DYN_CTLN1
  1145. /**
  1146. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1147. * @vsi: the VSI we care about
  1148. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1149. *
  1150. **/
  1151. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1152. struct i40e_q_vector *q_vector)
  1153. {
  1154. struct i40e_hw *hw = &vsi->back->hw;
  1155. bool rx = false, tx = false;
  1156. u32 rxval, txval;
  1157. int vector;
  1158. vector = (q_vector->v_idx + vsi->base_vector);
  1159. /* avoid dynamic calculation if in countdown mode OR if
  1160. * all dynamic is disabled
  1161. */
  1162. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1163. if (q_vector->itr_countdown > 0 ||
  1164. (!ITR_IS_DYNAMIC(vsi->rx_itr_setting) &&
  1165. !ITR_IS_DYNAMIC(vsi->tx_itr_setting))) {
  1166. goto enable_int;
  1167. }
  1168. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting)) {
  1169. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1170. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1171. }
  1172. if (ITR_IS_DYNAMIC(vsi->tx_itr_setting)) {
  1173. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1174. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1175. }
  1176. if (rx || tx) {
  1177. /* get the higher of the two ITR adjustments and
  1178. * use the same value for both ITR registers
  1179. * when in adaptive mode (Rx and/or Tx)
  1180. */
  1181. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1182. q_vector->tx.itr = q_vector->rx.itr = itr;
  1183. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1184. tx = true;
  1185. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1186. rx = true;
  1187. }
  1188. /* only need to enable the interrupt once, but need
  1189. * to possibly update both ITR values
  1190. */
  1191. if (rx) {
  1192. /* set the INTENA_MSK_MASK so that this first write
  1193. * won't actually enable the interrupt, instead just
  1194. * updating the ITR (it's bit 31 PF and VF)
  1195. */
  1196. rxval |= BIT(31);
  1197. /* don't check _DOWN because interrupt isn't being enabled */
  1198. wr32(hw, INTREG(vector - 1), rxval);
  1199. }
  1200. enable_int:
  1201. if (!test_bit(__I40E_DOWN, &vsi->state))
  1202. wr32(hw, INTREG(vector - 1), txval);
  1203. if (q_vector->itr_countdown)
  1204. q_vector->itr_countdown--;
  1205. else
  1206. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1207. }
  1208. /**
  1209. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1210. * @napi: napi struct with our devices info in it
  1211. * @budget: amount of work driver is allowed to do this pass, in packets
  1212. *
  1213. * This function will clean all queues associated with a q_vector.
  1214. *
  1215. * Returns the amount of work done
  1216. **/
  1217. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1218. {
  1219. struct i40e_q_vector *q_vector =
  1220. container_of(napi, struct i40e_q_vector, napi);
  1221. struct i40e_vsi *vsi = q_vector->vsi;
  1222. struct i40e_ring *ring;
  1223. bool clean_complete = true;
  1224. bool arm_wb = false;
  1225. int budget_per_ring;
  1226. int work_done = 0;
  1227. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1228. napi_complete(napi);
  1229. return 0;
  1230. }
  1231. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1232. * budget and be more aggressive about cleaning up the Tx descriptors.
  1233. */
  1234. i40e_for_each_ring(ring, q_vector->tx) {
  1235. clean_complete = clean_complete &&
  1236. i40e_clean_tx_irq(ring, vsi->work_limit);
  1237. arm_wb = arm_wb || ring->arm_wb;
  1238. ring->arm_wb = false;
  1239. }
  1240. /* Handle case where we are called by netpoll with a budget of 0 */
  1241. if (budget <= 0)
  1242. goto tx_only;
  1243. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1244. * allow the budget to go below 1 because that would exit polling early.
  1245. */
  1246. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1247. i40e_for_each_ring(ring, q_vector->rx) {
  1248. int cleaned;
  1249. if (ring_is_ps_enabled(ring))
  1250. cleaned = i40e_clean_rx_irq_ps(ring, budget_per_ring);
  1251. else
  1252. cleaned = i40e_clean_rx_irq_1buf(ring, budget_per_ring);
  1253. work_done += cleaned;
  1254. /* if we didn't clean as many as budgeted, we must be done */
  1255. clean_complete = clean_complete && (budget_per_ring > cleaned);
  1256. }
  1257. /* If work not completed, return budget and polling will return */
  1258. if (!clean_complete) {
  1259. tx_only:
  1260. if (arm_wb) {
  1261. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1262. i40e_enable_wb_on_itr(vsi, q_vector);
  1263. }
  1264. return budget;
  1265. }
  1266. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1267. q_vector->arm_wb_state = false;
  1268. /* Work is done so exit the polling mode and re-enable the interrupt */
  1269. napi_complete_done(napi, work_done);
  1270. i40e_update_enable_itr(vsi, q_vector);
  1271. return 0;
  1272. }
  1273. /**
  1274. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1275. * @skb: send buffer
  1276. * @tx_ring: ring to send buffer on
  1277. * @flags: the tx flags to be set
  1278. *
  1279. * Checks the skb and set up correspondingly several generic transmit flags
  1280. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1281. *
  1282. * Returns error code indicate the frame should be dropped upon error and the
  1283. * otherwise returns 0 to indicate the flags has been set properly.
  1284. **/
  1285. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1286. struct i40e_ring *tx_ring,
  1287. u32 *flags)
  1288. {
  1289. __be16 protocol = skb->protocol;
  1290. u32 tx_flags = 0;
  1291. if (protocol == htons(ETH_P_8021Q) &&
  1292. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1293. /* When HW VLAN acceleration is turned off by the user the
  1294. * stack sets the protocol to 8021q so that the driver
  1295. * can take any steps required to support the SW only
  1296. * VLAN handling. In our case the driver doesn't need
  1297. * to take any further steps so just set the protocol
  1298. * to the encapsulated ethertype.
  1299. */
  1300. skb->protocol = vlan_get_protocol(skb);
  1301. goto out;
  1302. }
  1303. /* if we have a HW VLAN tag being added, default to the HW one */
  1304. if (skb_vlan_tag_present(skb)) {
  1305. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1306. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1307. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1308. } else if (protocol == htons(ETH_P_8021Q)) {
  1309. struct vlan_hdr *vhdr, _vhdr;
  1310. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1311. if (!vhdr)
  1312. return -EINVAL;
  1313. protocol = vhdr->h_vlan_encapsulated_proto;
  1314. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1315. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1316. }
  1317. out:
  1318. *flags = tx_flags;
  1319. return 0;
  1320. }
  1321. /**
  1322. * i40e_tso - set up the tso context descriptor
  1323. * @tx_ring: ptr to the ring to send
  1324. * @skb: ptr to the skb we're sending
  1325. * @hdr_len: ptr to the size of the packet header
  1326. * @cd_type_cmd_tso_mss: Quad Word 1
  1327. *
  1328. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1329. **/
  1330. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1331. u8 *hdr_len, u64 *cd_type_cmd_tso_mss)
  1332. {
  1333. u32 cd_cmd, cd_tso_len, cd_mss;
  1334. struct ipv6hdr *ipv6h;
  1335. struct tcphdr *tcph;
  1336. struct iphdr *iph;
  1337. u32 l4len;
  1338. int err;
  1339. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1340. return 0;
  1341. if (!skb_is_gso(skb))
  1342. return 0;
  1343. err = skb_cow_head(skb, 0);
  1344. if (err < 0)
  1345. return err;
  1346. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  1347. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  1348. if (iph->version == 4) {
  1349. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1350. iph->tot_len = 0;
  1351. iph->check = 0;
  1352. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1353. 0, IPPROTO_TCP, 0);
  1354. } else if (ipv6h->version == 6) {
  1355. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1356. ipv6h->payload_len = 0;
  1357. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  1358. 0, IPPROTO_TCP, 0);
  1359. }
  1360. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1361. *hdr_len = (skb->encapsulation
  1362. ? (skb_inner_transport_header(skb) - skb->data)
  1363. : skb_transport_offset(skb)) + l4len;
  1364. /* find the field values */
  1365. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1366. cd_tso_len = skb->len - *hdr_len;
  1367. cd_mss = skb_shinfo(skb)->gso_size;
  1368. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1369. ((u64)cd_tso_len <<
  1370. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1371. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1372. return 1;
  1373. }
  1374. /**
  1375. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1376. * @skb: send buffer
  1377. * @tx_flags: pointer to Tx flags currently set
  1378. * @td_cmd: Tx descriptor command bits to set
  1379. * @td_offset: Tx descriptor header offsets to set
  1380. * @cd_tunneling: ptr to context desc bits
  1381. **/
  1382. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1383. u32 *td_cmd, u32 *td_offset,
  1384. struct i40e_ring *tx_ring,
  1385. u32 *cd_tunneling)
  1386. {
  1387. struct ipv6hdr *this_ipv6_hdr;
  1388. unsigned int this_tcp_hdrlen;
  1389. struct iphdr *this_ip_hdr;
  1390. u32 network_hdr_len;
  1391. u8 l4_hdr = 0;
  1392. struct udphdr *oudph;
  1393. struct iphdr *oiph;
  1394. u32 l4_tunnel = 0;
  1395. if (skb->encapsulation) {
  1396. switch (ip_hdr(skb)->protocol) {
  1397. case IPPROTO_UDP:
  1398. oudph = udp_hdr(skb);
  1399. oiph = ip_hdr(skb);
  1400. l4_tunnel = I40E_TXD_CTX_UDP_TUNNELING;
  1401. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1402. break;
  1403. default:
  1404. return;
  1405. }
  1406. network_hdr_len = skb_inner_network_header_len(skb);
  1407. this_ip_hdr = inner_ip_hdr(skb);
  1408. this_ipv6_hdr = inner_ipv6_hdr(skb);
  1409. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  1410. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1411. if (*tx_flags & I40E_TX_FLAGS_TSO) {
  1412. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  1413. ip_hdr(skb)->check = 0;
  1414. } else {
  1415. *cd_tunneling |=
  1416. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1417. }
  1418. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1419. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1420. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1421. ip_hdr(skb)->check = 0;
  1422. }
  1423. /* Now set the ctx descriptor fields */
  1424. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1425. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1426. l4_tunnel |
  1427. ((skb_inner_network_offset(skb) -
  1428. skb_transport_offset(skb)) >> 1) <<
  1429. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1430. if (this_ip_hdr->version == 6) {
  1431. *tx_flags &= ~I40E_TX_FLAGS_IPV4;
  1432. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1433. }
  1434. if ((tx_ring->flags & I40E_TXR_FLAGS_OUTER_UDP_CSUM) &&
  1435. (l4_tunnel == I40E_TXD_CTX_UDP_TUNNELING) &&
  1436. (*cd_tunneling & I40E_TXD_CTX_QW0_EXT_IP_MASK)) {
  1437. oudph->check = ~csum_tcpudp_magic(oiph->saddr,
  1438. oiph->daddr,
  1439. (skb->len - skb_transport_offset(skb)),
  1440. IPPROTO_UDP, 0);
  1441. *cd_tunneling |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1442. }
  1443. } else {
  1444. network_hdr_len = skb_network_header_len(skb);
  1445. this_ip_hdr = ip_hdr(skb);
  1446. this_ipv6_hdr = ipv6_hdr(skb);
  1447. this_tcp_hdrlen = tcp_hdrlen(skb);
  1448. }
  1449. /* Enable IP checksum offloads */
  1450. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1451. l4_hdr = this_ip_hdr->protocol;
  1452. /* the stack computes the IP header already, the only time we
  1453. * need the hardware to recompute it is in the case of TSO.
  1454. */
  1455. if (*tx_flags & I40E_TX_FLAGS_TSO) {
  1456. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1457. this_ip_hdr->check = 0;
  1458. } else {
  1459. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1460. }
  1461. /* Now set the td_offset for IP header length */
  1462. *td_offset = (network_hdr_len >> 2) <<
  1463. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1464. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1465. l4_hdr = this_ipv6_hdr->nexthdr;
  1466. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1467. /* Now set the td_offset for IP header length */
  1468. *td_offset = (network_hdr_len >> 2) <<
  1469. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1470. }
  1471. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1472. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1473. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1474. /* Enable L4 checksum offloads */
  1475. switch (l4_hdr) {
  1476. case IPPROTO_TCP:
  1477. /* enable checksum offloads */
  1478. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1479. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1480. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1481. break;
  1482. case IPPROTO_SCTP:
  1483. /* enable SCTP checksum offload */
  1484. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1485. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1486. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1487. break;
  1488. case IPPROTO_UDP:
  1489. /* enable UDP checksum offload */
  1490. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1491. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1492. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1493. break;
  1494. default:
  1495. break;
  1496. }
  1497. }
  1498. /**
  1499. * i40e_create_tx_ctx Build the Tx context descriptor
  1500. * @tx_ring: ring to create the descriptor on
  1501. * @cd_type_cmd_tso_mss: Quad Word 1
  1502. * @cd_tunneling: Quad Word 0 - bits 0-31
  1503. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1504. **/
  1505. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1506. const u64 cd_type_cmd_tso_mss,
  1507. const u32 cd_tunneling, const u32 cd_l2tag2)
  1508. {
  1509. struct i40e_tx_context_desc *context_desc;
  1510. int i = tx_ring->next_to_use;
  1511. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1512. !cd_tunneling && !cd_l2tag2)
  1513. return;
  1514. /* grab the next descriptor */
  1515. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1516. i++;
  1517. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1518. /* cpu_to_le32 and assign to struct fields */
  1519. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1520. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1521. context_desc->rsvd = cpu_to_le16(0);
  1522. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1523. }
  1524. /**
  1525. * i40e_chk_linearize - Check if there are more than 8 fragments per packet
  1526. * @skb: send buffer
  1527. * @tx_flags: collected send information
  1528. *
  1529. * Note: Our HW can't scatter-gather more than 8 fragments to build
  1530. * a packet on the wire and so we need to figure out the cases where we
  1531. * need to linearize the skb.
  1532. **/
  1533. static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags)
  1534. {
  1535. struct skb_frag_struct *frag;
  1536. bool linearize = false;
  1537. unsigned int size = 0;
  1538. u16 num_frags;
  1539. u16 gso_segs;
  1540. num_frags = skb_shinfo(skb)->nr_frags;
  1541. gso_segs = skb_shinfo(skb)->gso_segs;
  1542. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
  1543. u16 j = 0;
  1544. if (num_frags < (I40E_MAX_BUFFER_TXD))
  1545. goto linearize_chk_done;
  1546. /* try the simple math, if we have too many frags per segment */
  1547. if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
  1548. I40E_MAX_BUFFER_TXD) {
  1549. linearize = true;
  1550. goto linearize_chk_done;
  1551. }
  1552. frag = &skb_shinfo(skb)->frags[0];
  1553. /* we might still have more fragments per segment */
  1554. do {
  1555. size += skb_frag_size(frag);
  1556. frag++; j++;
  1557. if ((size >= skb_shinfo(skb)->gso_size) &&
  1558. (j < I40E_MAX_BUFFER_TXD)) {
  1559. size = (size % skb_shinfo(skb)->gso_size);
  1560. j = (size) ? 1 : 0;
  1561. }
  1562. if (j == I40E_MAX_BUFFER_TXD) {
  1563. linearize = true;
  1564. break;
  1565. }
  1566. num_frags--;
  1567. } while (num_frags);
  1568. } else {
  1569. if (num_frags >= I40E_MAX_BUFFER_TXD)
  1570. linearize = true;
  1571. }
  1572. linearize_chk_done:
  1573. return linearize;
  1574. }
  1575. /**
  1576. * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
  1577. * @tx_ring: the ring to be checked
  1578. * @size: the size buffer we want to assure is available
  1579. *
  1580. * Returns -EBUSY if a stop is needed, else 0
  1581. **/
  1582. static inline int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1583. {
  1584. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1585. /* Memory barrier before checking head and tail */
  1586. smp_mb();
  1587. /* Check again in a case another CPU has just made room available. */
  1588. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1589. return -EBUSY;
  1590. /* A reprieve! - use start_queue because it doesn't call schedule */
  1591. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1592. ++tx_ring->tx_stats.restart_queue;
  1593. return 0;
  1594. }
  1595. /**
  1596. * i40evf_maybe_stop_tx - 1st level check for tx stop conditions
  1597. * @tx_ring: the ring to be checked
  1598. * @size: the size buffer we want to assure is available
  1599. *
  1600. * Returns 0 if stop is not needed
  1601. **/
  1602. static inline int i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1603. {
  1604. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1605. return 0;
  1606. return __i40evf_maybe_stop_tx(tx_ring, size);
  1607. }
  1608. /**
  1609. * i40evf_tx_map - Build the Tx descriptor
  1610. * @tx_ring: ring to send buffer on
  1611. * @skb: send buffer
  1612. * @first: first buffer info buffer to use
  1613. * @tx_flags: collected send information
  1614. * @hdr_len: size of the packet header
  1615. * @td_cmd: the command field in the descriptor
  1616. * @td_offset: offset for checksum or crc
  1617. **/
  1618. static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1619. struct i40e_tx_buffer *first, u32 tx_flags,
  1620. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1621. {
  1622. unsigned int data_len = skb->data_len;
  1623. unsigned int size = skb_headlen(skb);
  1624. struct skb_frag_struct *frag;
  1625. struct i40e_tx_buffer *tx_bi;
  1626. struct i40e_tx_desc *tx_desc;
  1627. u16 i = tx_ring->next_to_use;
  1628. u32 td_tag = 0;
  1629. dma_addr_t dma;
  1630. u16 gso_segs;
  1631. u16 desc_count = 0;
  1632. bool tail_bump = true;
  1633. bool do_rs = false;
  1634. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1635. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1636. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1637. I40E_TX_FLAGS_VLAN_SHIFT;
  1638. }
  1639. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1640. gso_segs = skb_shinfo(skb)->gso_segs;
  1641. else
  1642. gso_segs = 1;
  1643. /* multiply data chunks by size of headers */
  1644. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1645. first->gso_segs = gso_segs;
  1646. first->skb = skb;
  1647. first->tx_flags = tx_flags;
  1648. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1649. tx_desc = I40E_TX_DESC(tx_ring, i);
  1650. tx_bi = first;
  1651. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1652. if (dma_mapping_error(tx_ring->dev, dma))
  1653. goto dma_error;
  1654. /* record length, and DMA address */
  1655. dma_unmap_len_set(tx_bi, len, size);
  1656. dma_unmap_addr_set(tx_bi, dma, dma);
  1657. tx_desc->buffer_addr = cpu_to_le64(dma);
  1658. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1659. tx_desc->cmd_type_offset_bsz =
  1660. build_ctob(td_cmd, td_offset,
  1661. I40E_MAX_DATA_PER_TXD, td_tag);
  1662. tx_desc++;
  1663. i++;
  1664. desc_count++;
  1665. if (i == tx_ring->count) {
  1666. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1667. i = 0;
  1668. }
  1669. dma += I40E_MAX_DATA_PER_TXD;
  1670. size -= I40E_MAX_DATA_PER_TXD;
  1671. tx_desc->buffer_addr = cpu_to_le64(dma);
  1672. }
  1673. if (likely(!data_len))
  1674. break;
  1675. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1676. size, td_tag);
  1677. tx_desc++;
  1678. i++;
  1679. desc_count++;
  1680. if (i == tx_ring->count) {
  1681. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1682. i = 0;
  1683. }
  1684. size = skb_frag_size(frag);
  1685. data_len -= size;
  1686. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1687. DMA_TO_DEVICE);
  1688. tx_bi = &tx_ring->tx_bi[i];
  1689. }
  1690. /* set next_to_watch value indicating a packet is present */
  1691. first->next_to_watch = tx_desc;
  1692. i++;
  1693. if (i == tx_ring->count)
  1694. i = 0;
  1695. tx_ring->next_to_use = i;
  1696. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  1697. tx_ring->queue_index),
  1698. first->bytecount);
  1699. i40evf_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1700. /* Algorithm to optimize tail and RS bit setting:
  1701. * if xmit_more is supported
  1702. * if xmit_more is true
  1703. * do not update tail and do not mark RS bit.
  1704. * if xmit_more is false and last xmit_more was false
  1705. * if every packet spanned less than 4 desc
  1706. * then set RS bit on 4th packet and update tail
  1707. * on every packet
  1708. * else
  1709. * update tail and set RS bit on every packet.
  1710. * if xmit_more is false and last_xmit_more was true
  1711. * update tail and set RS bit.
  1712. *
  1713. * Optimization: wmb to be issued only in case of tail update.
  1714. * Also optimize the Descriptor WB path for RS bit with the same
  1715. * algorithm.
  1716. *
  1717. * Note: If there are less than 4 packets
  1718. * pending and interrupts were disabled the service task will
  1719. * trigger a force WB.
  1720. */
  1721. if (skb->xmit_more &&
  1722. !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
  1723. tx_ring->queue_index))) {
  1724. tx_ring->flags |= I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
  1725. tail_bump = false;
  1726. } else if (!skb->xmit_more &&
  1727. !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
  1728. tx_ring->queue_index)) &&
  1729. (!(tx_ring->flags & I40E_TXR_FLAGS_LAST_XMIT_MORE_SET)) &&
  1730. (tx_ring->packet_stride < WB_STRIDE) &&
  1731. (desc_count < WB_STRIDE)) {
  1732. tx_ring->packet_stride++;
  1733. } else {
  1734. tx_ring->packet_stride = 0;
  1735. tx_ring->flags &= ~I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
  1736. do_rs = true;
  1737. }
  1738. if (do_rs)
  1739. tx_ring->packet_stride = 0;
  1740. tx_desc->cmd_type_offset_bsz =
  1741. build_ctob(td_cmd, td_offset, size, td_tag) |
  1742. cpu_to_le64((u64)(do_rs ? I40E_TXD_CMD :
  1743. I40E_TX_DESC_CMD_EOP) <<
  1744. I40E_TXD_QW1_CMD_SHIFT);
  1745. /* notify HW of packet */
  1746. if (!tail_bump)
  1747. prefetchw(tx_desc + 1);
  1748. if (tail_bump) {
  1749. /* Force memory writes to complete before letting h/w
  1750. * know there are new descriptors to fetch. (Only
  1751. * applicable for weak-ordered memory model archs,
  1752. * such as IA-64).
  1753. */
  1754. wmb();
  1755. writel(i, tx_ring->tail);
  1756. }
  1757. return;
  1758. dma_error:
  1759. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1760. /* clear dma mappings for failed tx_bi map */
  1761. for (;;) {
  1762. tx_bi = &tx_ring->tx_bi[i];
  1763. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1764. if (tx_bi == first)
  1765. break;
  1766. if (i == 0)
  1767. i = tx_ring->count;
  1768. i--;
  1769. }
  1770. tx_ring->next_to_use = i;
  1771. }
  1772. /**
  1773. * i40evf_xmit_descriptor_count - calculate number of tx descriptors needed
  1774. * @skb: send buffer
  1775. * @tx_ring: ring to send buffer on
  1776. *
  1777. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  1778. * there is not enough descriptors available in this ring since we need at least
  1779. * one descriptor.
  1780. **/
  1781. static inline int i40evf_xmit_descriptor_count(struct sk_buff *skb,
  1782. struct i40e_ring *tx_ring)
  1783. {
  1784. unsigned int f;
  1785. int count = 0;
  1786. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1787. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1788. * + 4 desc gap to avoid the cache line where head is,
  1789. * + 1 desc for context descriptor,
  1790. * otherwise try next time
  1791. */
  1792. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1793. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  1794. count += TXD_USE_COUNT(skb_headlen(skb));
  1795. if (i40evf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1796. tx_ring->tx_stats.tx_busy++;
  1797. return 0;
  1798. }
  1799. return count;
  1800. }
  1801. /**
  1802. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1803. * @skb: send buffer
  1804. * @tx_ring: ring to send buffer on
  1805. *
  1806. * Returns NETDEV_TX_OK if sent, else an error code
  1807. **/
  1808. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1809. struct i40e_ring *tx_ring)
  1810. {
  1811. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1812. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1813. struct i40e_tx_buffer *first;
  1814. u32 td_offset = 0;
  1815. u32 tx_flags = 0;
  1816. __be16 protocol;
  1817. u32 td_cmd = 0;
  1818. u8 hdr_len = 0;
  1819. int tso;
  1820. /* prefetch the data, we'll need it later */
  1821. prefetch(skb->data);
  1822. if (0 == i40evf_xmit_descriptor_count(skb, tx_ring))
  1823. return NETDEV_TX_BUSY;
  1824. /* prepare the xmit flags */
  1825. if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1826. goto out_drop;
  1827. /* obtain protocol of skb */
  1828. protocol = vlan_get_protocol(skb);
  1829. /* record the location of the first descriptor for this packet */
  1830. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1831. /* setup IPv4/IPv6 offloads */
  1832. if (protocol == htons(ETH_P_IP))
  1833. tx_flags |= I40E_TX_FLAGS_IPV4;
  1834. else if (protocol == htons(ETH_P_IPV6))
  1835. tx_flags |= I40E_TX_FLAGS_IPV6;
  1836. tso = i40e_tso(tx_ring, skb, &hdr_len, &cd_type_cmd_tso_mss);
  1837. if (tso < 0)
  1838. goto out_drop;
  1839. else if (tso)
  1840. tx_flags |= I40E_TX_FLAGS_TSO;
  1841. if (i40e_chk_linearize(skb, tx_flags)) {
  1842. if (skb_linearize(skb))
  1843. goto out_drop;
  1844. tx_ring->tx_stats.tx_linearize++;
  1845. }
  1846. skb_tx_timestamp(skb);
  1847. /* always enable CRC insertion offload */
  1848. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1849. /* Always offload the checksum, since it's in the data descriptor */
  1850. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1851. tx_flags |= I40E_TX_FLAGS_CSUM;
  1852. i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  1853. tx_ring, &cd_tunneling);
  1854. }
  1855. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1856. cd_tunneling, cd_l2tag2);
  1857. i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1858. td_cmd, td_offset);
  1859. return NETDEV_TX_OK;
  1860. out_drop:
  1861. dev_kfree_skb_any(skb);
  1862. return NETDEV_TX_OK;
  1863. }
  1864. /**
  1865. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1866. * @skb: send buffer
  1867. * @netdev: network interface device structure
  1868. *
  1869. * Returns NETDEV_TX_OK if sent, else an error code
  1870. **/
  1871. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1872. {
  1873. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1874. struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
  1875. /* hardware can't handle really short frames, hardware padding works
  1876. * beyond this point
  1877. */
  1878. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1879. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1880. return NETDEV_TX_OK;
  1881. skb->len = I40E_MIN_TX_LEN;
  1882. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1883. }
  1884. return i40e_xmit_frame_ring(skb, tx_ring);
  1885. }