i40e_txrx.c 62 KB

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