i40e_txrx.c 61 KB

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