ice_txrx.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* The driver transmit and receive code */
  4. #include <linux/mm.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/prefetch.h>
  7. #include <linux/bpf_trace.h>
  8. #include <net/dsfield.h>
  9. #include <net/mpls.h>
  10. #include <net/xdp.h>
  11. #include "ice_txrx_lib.h"
  12. #include "ice_lib.h"
  13. #include "ice.h"
  14. #include "ice_trace.h"
  15. #include "ice_dcb_lib.h"
  16. #include "ice_xsk.h"
  17. #include "ice_eswitch.h"
  18. #define ICE_RX_HDR_SIZE 256
  19. #define FDIR_DESC_RXDID 0x40
  20. #define ICE_FDIR_CLEAN_DELAY 10
  21. /**
  22. * ice_prgm_fdir_fltr - Program a Flow Director filter
  23. * @vsi: VSI to send dummy packet
  24. * @fdir_desc: flow director descriptor
  25. * @raw_packet: allocated buffer for flow director
  26. */
  27. int
  28. ice_prgm_fdir_fltr(struct ice_vsi *vsi, struct ice_fltr_desc *fdir_desc,
  29. u8 *raw_packet)
  30. {
  31. struct ice_tx_buf *tx_buf, *first;
  32. struct ice_fltr_desc *f_desc;
  33. struct ice_tx_desc *tx_desc;
  34. struct ice_tx_ring *tx_ring;
  35. struct device *dev;
  36. dma_addr_t dma;
  37. u32 td_cmd;
  38. u16 i;
  39. /* VSI and Tx ring */
  40. if (!vsi)
  41. return -ENOENT;
  42. tx_ring = vsi->tx_rings[0];
  43. if (!tx_ring || !tx_ring->desc)
  44. return -ENOENT;
  45. dev = tx_ring->dev;
  46. /* we are using two descriptors to add/del a filter and we can wait */
  47. for (i = ICE_FDIR_CLEAN_DELAY; ICE_DESC_UNUSED(tx_ring) < 2; i--) {
  48. if (!i)
  49. return -EAGAIN;
  50. msleep_interruptible(1);
  51. }
  52. dma = dma_map_single(dev, raw_packet, ICE_FDIR_MAX_RAW_PKT_SIZE,
  53. DMA_TO_DEVICE);
  54. if (dma_mapping_error(dev, dma))
  55. return -EINVAL;
  56. /* grab the next descriptor */
  57. i = tx_ring->next_to_use;
  58. first = &tx_ring->tx_buf[i];
  59. f_desc = ICE_TX_FDIRDESC(tx_ring, i);
  60. memcpy(f_desc, fdir_desc, sizeof(*f_desc));
  61. i++;
  62. i = (i < tx_ring->count) ? i : 0;
  63. tx_desc = ICE_TX_DESC(tx_ring, i);
  64. tx_buf = &tx_ring->tx_buf[i];
  65. i++;
  66. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  67. memset(tx_buf, 0, sizeof(*tx_buf));
  68. dma_unmap_len_set(tx_buf, len, ICE_FDIR_MAX_RAW_PKT_SIZE);
  69. dma_unmap_addr_set(tx_buf, dma, dma);
  70. tx_desc->buf_addr = cpu_to_le64(dma);
  71. td_cmd = ICE_TXD_LAST_DESC_CMD | ICE_TX_DESC_CMD_DUMMY |
  72. ICE_TX_DESC_CMD_RE;
  73. tx_buf->type = ICE_TX_BUF_DUMMY;
  74. tx_buf->raw_buf = raw_packet;
  75. tx_desc->cmd_type_offset_bsz =
  76. ice_build_ctob(td_cmd, 0, ICE_FDIR_MAX_RAW_PKT_SIZE, 0);
  77. /* Force memory write to complete before letting h/w know
  78. * there are new descriptors to fetch.
  79. */
  80. wmb();
  81. /* mark the data descriptor to be watched */
  82. first->next_to_watch = tx_desc;
  83. writel(tx_ring->next_to_use, tx_ring->tail);
  84. return 0;
  85. }
  86. /**
  87. * ice_unmap_and_free_tx_buf - Release a Tx buffer
  88. * @ring: the ring that owns the buffer
  89. * @tx_buf: the buffer to free
  90. */
  91. static void
  92. ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf)
  93. {
  94. if (dma_unmap_len(tx_buf, len))
  95. dma_unmap_page(ring->dev,
  96. dma_unmap_addr(tx_buf, dma),
  97. dma_unmap_len(tx_buf, len),
  98. DMA_TO_DEVICE);
  99. switch (tx_buf->type) {
  100. case ICE_TX_BUF_DUMMY:
  101. devm_kfree(ring->dev, tx_buf->raw_buf);
  102. break;
  103. case ICE_TX_BUF_SKB:
  104. dev_kfree_skb_any(tx_buf->skb);
  105. break;
  106. case ICE_TX_BUF_XDP_TX:
  107. page_frag_free(tx_buf->raw_buf);
  108. break;
  109. case ICE_TX_BUF_XDP_XMIT:
  110. xdp_return_frame(tx_buf->xdpf);
  111. break;
  112. }
  113. tx_buf->next_to_watch = NULL;
  114. tx_buf->type = ICE_TX_BUF_EMPTY;
  115. dma_unmap_len_set(tx_buf, len, 0);
  116. /* tx_buf must be completely set up in the transmit path */
  117. }
  118. static struct netdev_queue *txring_txq(const struct ice_tx_ring *ring)
  119. {
  120. return netdev_get_tx_queue(ring->netdev, ring->q_index);
  121. }
  122. /**
  123. * ice_clean_tx_ring - Free any empty Tx buffers
  124. * @tx_ring: ring to be cleaned
  125. */
  126. void ice_clean_tx_ring(struct ice_tx_ring *tx_ring)
  127. {
  128. u32 size;
  129. u16 i;
  130. if (ice_ring_is_xdp(tx_ring) && tx_ring->xsk_pool) {
  131. ice_xsk_clean_xdp_ring(tx_ring);
  132. goto tx_skip_free;
  133. }
  134. /* ring already cleared, nothing to do */
  135. if (!tx_ring->tx_buf)
  136. return;
  137. /* Free all the Tx ring sk_buffs */
  138. for (i = 0; i < tx_ring->count; i++)
  139. ice_unmap_and_free_tx_buf(tx_ring, &tx_ring->tx_buf[i]);
  140. tx_skip_free:
  141. memset(tx_ring->tx_buf, 0, sizeof(*tx_ring->tx_buf) * tx_ring->count);
  142. size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
  143. PAGE_SIZE);
  144. /* Zero out the descriptor ring */
  145. memset(tx_ring->desc, 0, size);
  146. tx_ring->next_to_use = 0;
  147. tx_ring->next_to_clean = 0;
  148. if (!tx_ring->netdev)
  149. return;
  150. /* cleanup Tx queue statistics */
  151. netdev_tx_reset_queue(txring_txq(tx_ring));
  152. }
  153. /**
  154. * ice_free_tx_ring - Free Tx resources per queue
  155. * @tx_ring: Tx descriptor ring for a specific queue
  156. *
  157. * Free all transmit software resources
  158. */
  159. void ice_free_tx_ring(struct ice_tx_ring *tx_ring)
  160. {
  161. u32 size;
  162. ice_clean_tx_ring(tx_ring);
  163. devm_kfree(tx_ring->dev, tx_ring->tx_buf);
  164. tx_ring->tx_buf = NULL;
  165. if (tx_ring->desc) {
  166. size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
  167. PAGE_SIZE);
  168. dmam_free_coherent(tx_ring->dev, size,
  169. tx_ring->desc, tx_ring->dma);
  170. tx_ring->desc = NULL;
  171. }
  172. }
  173. /**
  174. * ice_clean_tx_irq - Reclaim resources after transmit completes
  175. * @tx_ring: Tx ring to clean
  176. * @napi_budget: Used to determine if we are in netpoll
  177. *
  178. * Returns true if there's any budget left (e.g. the clean is finished)
  179. */
  180. static bool ice_clean_tx_irq(struct ice_tx_ring *tx_ring, int napi_budget)
  181. {
  182. unsigned int total_bytes = 0, total_pkts = 0;
  183. unsigned int budget = ICE_DFLT_IRQ_WORK;
  184. struct ice_vsi *vsi = tx_ring->vsi;
  185. s16 i = tx_ring->next_to_clean;
  186. struct ice_tx_desc *tx_desc;
  187. struct ice_tx_buf *tx_buf;
  188. /* get the bql data ready */
  189. netdev_txq_bql_complete_prefetchw(txring_txq(tx_ring));
  190. tx_buf = &tx_ring->tx_buf[i];
  191. tx_desc = ICE_TX_DESC(tx_ring, i);
  192. i -= tx_ring->count;
  193. prefetch(&vsi->state);
  194. do {
  195. struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
  196. /* if next_to_watch is not set then there is no work pending */
  197. if (!eop_desc)
  198. break;
  199. /* follow the guidelines of other drivers */
  200. prefetchw(&tx_buf->skb->users);
  201. smp_rmb(); /* prevent any other reads prior to eop_desc */
  202. ice_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  203. /* if the descriptor isn't done, no work yet to do */
  204. if (!(eop_desc->cmd_type_offset_bsz &
  205. cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
  206. break;
  207. /* clear next_to_watch to prevent false hangs */
  208. tx_buf->next_to_watch = NULL;
  209. /* update the statistics for this packet */
  210. total_bytes += tx_buf->bytecount;
  211. total_pkts += tx_buf->gso_segs;
  212. /* free the skb */
  213. napi_consume_skb(tx_buf->skb, napi_budget);
  214. /* unmap skb header data */
  215. dma_unmap_single(tx_ring->dev,
  216. dma_unmap_addr(tx_buf, dma),
  217. dma_unmap_len(tx_buf, len),
  218. DMA_TO_DEVICE);
  219. /* clear tx_buf data */
  220. tx_buf->type = ICE_TX_BUF_EMPTY;
  221. dma_unmap_len_set(tx_buf, len, 0);
  222. /* unmap remaining buffers */
  223. while (tx_desc != eop_desc) {
  224. ice_trace(clean_tx_irq_unmap, tx_ring, tx_desc, tx_buf);
  225. tx_buf++;
  226. tx_desc++;
  227. i++;
  228. if (unlikely(!i)) {
  229. i -= tx_ring->count;
  230. tx_buf = tx_ring->tx_buf;
  231. tx_desc = ICE_TX_DESC(tx_ring, 0);
  232. }
  233. /* unmap any remaining paged data */
  234. if (dma_unmap_len(tx_buf, len)) {
  235. dma_unmap_page(tx_ring->dev,
  236. dma_unmap_addr(tx_buf, dma),
  237. dma_unmap_len(tx_buf, len),
  238. DMA_TO_DEVICE);
  239. dma_unmap_len_set(tx_buf, len, 0);
  240. }
  241. }
  242. ice_trace(clean_tx_irq_unmap_eop, tx_ring, tx_desc, tx_buf);
  243. /* move us one more past the eop_desc for start of next pkt */
  244. tx_buf++;
  245. tx_desc++;
  246. i++;
  247. if (unlikely(!i)) {
  248. i -= tx_ring->count;
  249. tx_buf = tx_ring->tx_buf;
  250. tx_desc = ICE_TX_DESC(tx_ring, 0);
  251. }
  252. prefetch(tx_desc);
  253. /* update budget accounting */
  254. budget--;
  255. } while (likely(budget));
  256. i += tx_ring->count;
  257. tx_ring->next_to_clean = i;
  258. ice_update_tx_ring_stats(tx_ring, total_pkts, total_bytes);
  259. netdev_tx_completed_queue(txring_txq(tx_ring), total_pkts, total_bytes);
  260. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  261. if (unlikely(total_pkts && netif_carrier_ok(tx_ring->netdev) &&
  262. (ICE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  263. /* Make sure that anybody stopping the queue after this
  264. * sees the new next_to_clean.
  265. */
  266. smp_mb();
  267. if (netif_tx_queue_stopped(txring_txq(tx_ring)) &&
  268. !test_bit(ICE_VSI_DOWN, vsi->state)) {
  269. netif_tx_wake_queue(txring_txq(tx_ring));
  270. ++tx_ring->ring_stats->tx_stats.restart_q;
  271. }
  272. }
  273. return !!budget;
  274. }
  275. /**
  276. * ice_setup_tx_ring - Allocate the Tx descriptors
  277. * @tx_ring: the Tx ring to set up
  278. *
  279. * Return 0 on success, negative on error
  280. */
  281. int ice_setup_tx_ring(struct ice_tx_ring *tx_ring)
  282. {
  283. struct device *dev = tx_ring->dev;
  284. u32 size;
  285. if (!dev)
  286. return -ENOMEM;
  287. /* warn if we are about to overwrite the pointer */
  288. WARN_ON(tx_ring->tx_buf);
  289. tx_ring->tx_buf =
  290. devm_kcalloc(dev, sizeof(*tx_ring->tx_buf), tx_ring->count,
  291. GFP_KERNEL);
  292. if (!tx_ring->tx_buf)
  293. return -ENOMEM;
  294. /* round up to nearest page */
  295. size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc),
  296. PAGE_SIZE);
  297. tx_ring->desc = dmam_alloc_coherent(dev, size, &tx_ring->dma,
  298. GFP_KERNEL);
  299. if (!tx_ring->desc) {
  300. dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  301. size);
  302. goto err;
  303. }
  304. tx_ring->next_to_use = 0;
  305. tx_ring->next_to_clean = 0;
  306. tx_ring->ring_stats->tx_stats.prev_pkt = -1;
  307. return 0;
  308. err:
  309. devm_kfree(dev, tx_ring->tx_buf);
  310. tx_ring->tx_buf = NULL;
  311. return -ENOMEM;
  312. }
  313. /**
  314. * ice_clean_rx_ring - Free Rx buffers
  315. * @rx_ring: ring to be cleaned
  316. */
  317. void ice_clean_rx_ring(struct ice_rx_ring *rx_ring)
  318. {
  319. struct xdp_buff *xdp = &rx_ring->xdp;
  320. struct device *dev = rx_ring->dev;
  321. u32 size;
  322. u16 i;
  323. /* ring already cleared, nothing to do */
  324. if (!rx_ring->rx_buf)
  325. return;
  326. if (rx_ring->xsk_pool) {
  327. ice_xsk_clean_rx_ring(rx_ring);
  328. goto rx_skip_free;
  329. }
  330. if (xdp->data) {
  331. xdp_return_buff(xdp);
  332. xdp->data = NULL;
  333. }
  334. /* Free all the Rx ring sk_buffs */
  335. for (i = 0; i < rx_ring->count; i++) {
  336. struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
  337. if (!rx_buf->page)
  338. continue;
  339. /* Invalidate cache lines that may have been written to by
  340. * device so that we avoid corrupting memory.
  341. */
  342. dma_sync_single_range_for_cpu(dev, rx_buf->dma,
  343. rx_buf->page_offset,
  344. rx_ring->rx_buf_len,
  345. DMA_FROM_DEVICE);
  346. /* free resources associated with mapping */
  347. dma_unmap_page_attrs(dev, rx_buf->dma, ice_rx_pg_size(rx_ring),
  348. DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
  349. __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
  350. rx_buf->page = NULL;
  351. rx_buf->page_offset = 0;
  352. }
  353. rx_skip_free:
  354. if (rx_ring->xsk_pool)
  355. memset(rx_ring->xdp_buf, 0, array_size(rx_ring->count, sizeof(*rx_ring->xdp_buf)));
  356. else
  357. memset(rx_ring->rx_buf, 0, array_size(rx_ring->count, sizeof(*rx_ring->rx_buf)));
  358. /* Zero out the descriptor ring */
  359. size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
  360. PAGE_SIZE);
  361. memset(rx_ring->desc, 0, size);
  362. rx_ring->next_to_alloc = 0;
  363. rx_ring->next_to_clean = 0;
  364. rx_ring->first_desc = 0;
  365. rx_ring->next_to_use = 0;
  366. }
  367. /**
  368. * ice_free_rx_ring - Free Rx resources
  369. * @rx_ring: ring to clean the resources from
  370. *
  371. * Free all receive software resources
  372. */
  373. void ice_free_rx_ring(struct ice_rx_ring *rx_ring)
  374. {
  375. u32 size;
  376. ice_clean_rx_ring(rx_ring);
  377. if (rx_ring->vsi->type == ICE_VSI_PF)
  378. if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
  379. xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
  380. WRITE_ONCE(rx_ring->xdp_prog, NULL);
  381. if (rx_ring->xsk_pool) {
  382. kfree(rx_ring->xdp_buf);
  383. rx_ring->xdp_buf = NULL;
  384. } else {
  385. kfree(rx_ring->rx_buf);
  386. rx_ring->rx_buf = NULL;
  387. }
  388. if (rx_ring->desc) {
  389. size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
  390. PAGE_SIZE);
  391. dmam_free_coherent(rx_ring->dev, size,
  392. rx_ring->desc, rx_ring->dma);
  393. rx_ring->desc = NULL;
  394. }
  395. }
  396. /**
  397. * ice_setup_rx_ring - Allocate the Rx descriptors
  398. * @rx_ring: the Rx ring to set up
  399. *
  400. * Return 0 on success, negative on error
  401. */
  402. int ice_setup_rx_ring(struct ice_rx_ring *rx_ring)
  403. {
  404. struct device *dev = rx_ring->dev;
  405. u32 size;
  406. if (!dev)
  407. return -ENOMEM;
  408. /* warn if we are about to overwrite the pointer */
  409. WARN_ON(rx_ring->rx_buf);
  410. rx_ring->rx_buf =
  411. kcalloc(rx_ring->count, sizeof(*rx_ring->rx_buf), GFP_KERNEL);
  412. if (!rx_ring->rx_buf)
  413. return -ENOMEM;
  414. /* round up to nearest page */
  415. size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc),
  416. PAGE_SIZE);
  417. rx_ring->desc = dmam_alloc_coherent(dev, size, &rx_ring->dma,
  418. GFP_KERNEL);
  419. if (!rx_ring->desc) {
  420. dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  421. size);
  422. goto err;
  423. }
  424. rx_ring->next_to_use = 0;
  425. rx_ring->next_to_clean = 0;
  426. rx_ring->first_desc = 0;
  427. if (ice_is_xdp_ena_vsi(rx_ring->vsi))
  428. WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog);
  429. return 0;
  430. err:
  431. kfree(rx_ring->rx_buf);
  432. rx_ring->rx_buf = NULL;
  433. return -ENOMEM;
  434. }
  435. /**
  436. * ice_run_xdp - Executes an XDP program on initialized xdp_buff
  437. * @rx_ring: Rx ring
  438. * @xdp: xdp_buff used as input to the XDP program
  439. * @xdp_prog: XDP program to run
  440. * @xdp_ring: ring to be used for XDP_TX action
  441. * @eop_desc: Last descriptor in packet to read metadata from
  442. *
  443. * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
  444. */
  445. static u32
  446. ice_run_xdp(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
  447. struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring,
  448. union ice_32b_rx_flex_desc *eop_desc)
  449. {
  450. unsigned int ret = ICE_XDP_PASS;
  451. u32 act;
  452. if (!xdp_prog)
  453. goto exit;
  454. ice_xdp_meta_set_desc(xdp, eop_desc);
  455. act = bpf_prog_run_xdp(xdp_prog, xdp);
  456. switch (act) {
  457. case XDP_PASS:
  458. break;
  459. case XDP_TX:
  460. if (static_branch_unlikely(&ice_xdp_locking_key))
  461. spin_lock(&xdp_ring->tx_lock);
  462. ret = __ice_xmit_xdp_ring(xdp, xdp_ring, false);
  463. if (static_branch_unlikely(&ice_xdp_locking_key))
  464. spin_unlock(&xdp_ring->tx_lock);
  465. if (ret == ICE_XDP_CONSUMED)
  466. goto out_failure;
  467. break;
  468. case XDP_REDIRECT:
  469. if (xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog))
  470. goto out_failure;
  471. ret = ICE_XDP_REDIR;
  472. break;
  473. default:
  474. bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
  475. fallthrough;
  476. case XDP_ABORTED:
  477. out_failure:
  478. trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
  479. fallthrough;
  480. case XDP_DROP:
  481. ret = ICE_XDP_CONSUMED;
  482. }
  483. exit:
  484. return ret;
  485. }
  486. /**
  487. * ice_xmit_xdp_ring - submit frame to XDP ring for transmission
  488. * @xdpf: XDP frame that will be converted to XDP buff
  489. * @xdp_ring: XDP ring for transmission
  490. */
  491. static int ice_xmit_xdp_ring(const struct xdp_frame *xdpf,
  492. struct ice_tx_ring *xdp_ring)
  493. {
  494. struct xdp_buff xdp;
  495. xdp.data_hard_start = (void *)xdpf;
  496. xdp.data = xdpf->data;
  497. xdp.data_end = xdp.data + xdpf->len;
  498. xdp.frame_sz = xdpf->frame_sz;
  499. xdp.flags = xdpf->flags;
  500. return __ice_xmit_xdp_ring(&xdp, xdp_ring, true);
  501. }
  502. /**
  503. * ice_xdp_xmit - submit packets to XDP ring for transmission
  504. * @dev: netdev
  505. * @n: number of XDP frames to be transmitted
  506. * @frames: XDP frames to be transmitted
  507. * @flags: transmit flags
  508. *
  509. * Returns number of frames successfully sent. Failed frames
  510. * will be free'ed by XDP core.
  511. * For error cases, a negative errno code is returned and no-frames
  512. * are transmitted (caller must handle freeing frames).
  513. */
  514. int
  515. ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
  516. u32 flags)
  517. {
  518. struct ice_netdev_priv *np = netdev_priv(dev);
  519. unsigned int queue_index = smp_processor_id();
  520. struct ice_vsi *vsi = np->vsi;
  521. struct ice_tx_ring *xdp_ring;
  522. struct ice_tx_buf *tx_buf;
  523. int nxmit = 0, i;
  524. if (test_bit(ICE_VSI_DOWN, vsi->state))
  525. return -ENETDOWN;
  526. if (!ice_is_xdp_ena_vsi(vsi))
  527. return -ENXIO;
  528. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  529. return -EINVAL;
  530. if (static_branch_unlikely(&ice_xdp_locking_key)) {
  531. queue_index %= vsi->num_xdp_txq;
  532. xdp_ring = vsi->xdp_rings[queue_index];
  533. spin_lock(&xdp_ring->tx_lock);
  534. } else {
  535. /* Generally, should not happen */
  536. if (unlikely(queue_index >= vsi->num_xdp_txq))
  537. return -ENXIO;
  538. xdp_ring = vsi->xdp_rings[queue_index];
  539. }
  540. tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use];
  541. for (i = 0; i < n; i++) {
  542. const struct xdp_frame *xdpf = frames[i];
  543. int err;
  544. err = ice_xmit_xdp_ring(xdpf, xdp_ring);
  545. if (err != ICE_XDP_TX)
  546. break;
  547. nxmit++;
  548. }
  549. tx_buf->rs_idx = ice_set_rs_bit(xdp_ring);
  550. if (unlikely(flags & XDP_XMIT_FLUSH))
  551. ice_xdp_ring_update_tail(xdp_ring);
  552. if (static_branch_unlikely(&ice_xdp_locking_key))
  553. spin_unlock(&xdp_ring->tx_lock);
  554. return nxmit;
  555. }
  556. /**
  557. * ice_alloc_mapped_page - recycle or make a new page
  558. * @rx_ring: ring to use
  559. * @bi: rx_buf struct to modify
  560. *
  561. * Returns true if the page was successfully allocated or
  562. * reused.
  563. */
  564. static bool
  565. ice_alloc_mapped_page(struct ice_rx_ring *rx_ring, struct ice_rx_buf *bi)
  566. {
  567. struct page *page = bi->page;
  568. dma_addr_t dma;
  569. /* since we are recycling buffers we should seldom need to alloc */
  570. if (likely(page))
  571. return true;
  572. /* alloc new page for storage */
  573. page = dev_alloc_pages(ice_rx_pg_order(rx_ring));
  574. if (unlikely(!page)) {
  575. rx_ring->ring_stats->rx_stats.alloc_page_failed++;
  576. return false;
  577. }
  578. /* map page for use */
  579. dma = dma_map_page_attrs(rx_ring->dev, page, 0, ice_rx_pg_size(rx_ring),
  580. DMA_FROM_DEVICE, ICE_RX_DMA_ATTR);
  581. /* if mapping failed free memory back to system since
  582. * there isn't much point in holding memory we can't use
  583. */
  584. if (dma_mapping_error(rx_ring->dev, dma)) {
  585. __free_pages(page, ice_rx_pg_order(rx_ring));
  586. rx_ring->ring_stats->rx_stats.alloc_page_failed++;
  587. return false;
  588. }
  589. bi->dma = dma;
  590. bi->page = page;
  591. bi->page_offset = rx_ring->rx_offset;
  592. page_ref_add(page, USHRT_MAX - 1);
  593. bi->pagecnt_bias = USHRT_MAX;
  594. return true;
  595. }
  596. /**
  597. * ice_alloc_rx_bufs - Replace used receive buffers
  598. * @rx_ring: ring to place buffers on
  599. * @cleaned_count: number of buffers to replace
  600. *
  601. * Returns false if all allocations were successful, true if any fail. Returning
  602. * true signals to the caller that we didn't replace cleaned_count buffers and
  603. * there is more work to do.
  604. *
  605. * First, try to clean "cleaned_count" Rx buffers. Then refill the cleaned Rx
  606. * buffers. Then bump tail at most one time. Grouping like this lets us avoid
  607. * multiple tail writes per call.
  608. */
  609. bool ice_alloc_rx_bufs(struct ice_rx_ring *rx_ring, unsigned int cleaned_count)
  610. {
  611. union ice_32b_rx_flex_desc *rx_desc;
  612. u16 ntu = rx_ring->next_to_use;
  613. struct ice_rx_buf *bi;
  614. /* do nothing if no valid netdev defined */
  615. if ((!rx_ring->netdev && rx_ring->vsi->type != ICE_VSI_CTRL) ||
  616. !cleaned_count)
  617. return false;
  618. /* get the Rx descriptor and buffer based on next_to_use */
  619. rx_desc = ICE_RX_DESC(rx_ring, ntu);
  620. bi = &rx_ring->rx_buf[ntu];
  621. do {
  622. /* if we fail here, we have work remaining */
  623. if (!ice_alloc_mapped_page(rx_ring, bi))
  624. break;
  625. /* sync the buffer for use by the device */
  626. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  627. bi->page_offset,
  628. rx_ring->rx_buf_len,
  629. DMA_FROM_DEVICE);
  630. /* Refresh the desc even if buffer_addrs didn't change
  631. * because each write-back erases this info.
  632. */
  633. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  634. rx_desc++;
  635. bi++;
  636. ntu++;
  637. if (unlikely(ntu == rx_ring->count)) {
  638. rx_desc = ICE_RX_DESC(rx_ring, 0);
  639. bi = rx_ring->rx_buf;
  640. ntu = 0;
  641. }
  642. /* clear the status bits for the next_to_use descriptor */
  643. rx_desc->wb.status_error0 = 0;
  644. cleaned_count--;
  645. } while (cleaned_count);
  646. if (rx_ring->next_to_use != ntu)
  647. ice_release_rx_desc(rx_ring, ntu);
  648. return !!cleaned_count;
  649. }
  650. /**
  651. * ice_rx_buf_adjust_pg_offset - Prepare Rx buffer for reuse
  652. * @rx_buf: Rx buffer to adjust
  653. * @size: Size of adjustment
  654. *
  655. * Update the offset within page so that Rx buf will be ready to be reused.
  656. * For systems with PAGE_SIZE < 8192 this function will flip the page offset
  657. * so the second half of page assigned to Rx buffer will be used, otherwise
  658. * the offset is moved by "size" bytes
  659. */
  660. static void
  661. ice_rx_buf_adjust_pg_offset(struct ice_rx_buf *rx_buf, unsigned int size)
  662. {
  663. #if (PAGE_SIZE < 8192)
  664. /* flip page offset to other buffer */
  665. rx_buf->page_offset ^= size;
  666. #else
  667. /* move offset up to the next cache line */
  668. rx_buf->page_offset += size;
  669. #endif
  670. }
  671. /**
  672. * ice_can_reuse_rx_page - Determine if page can be reused for another Rx
  673. * @rx_buf: buffer containing the page
  674. *
  675. * If page is reusable, we have a green light for calling ice_reuse_rx_page,
  676. * which will assign the current buffer to the buffer that next_to_alloc is
  677. * pointing to; otherwise, the DMA mapping needs to be destroyed and
  678. * page freed
  679. */
  680. static bool
  681. ice_can_reuse_rx_page(struct ice_rx_buf *rx_buf)
  682. {
  683. unsigned int pagecnt_bias = rx_buf->pagecnt_bias;
  684. struct page *page = rx_buf->page;
  685. /* avoid re-using remote and pfmemalloc pages */
  686. if (!dev_page_is_reusable(page))
  687. return false;
  688. /* if we are only owner of page we can reuse it */
  689. if (unlikely(rx_buf->pgcnt - pagecnt_bias > 1))
  690. return false;
  691. #if (PAGE_SIZE >= 8192)
  692. #define ICE_LAST_OFFSET \
  693. (SKB_WITH_OVERHEAD(PAGE_SIZE) - ICE_RXBUF_3072)
  694. if (rx_buf->page_offset > ICE_LAST_OFFSET)
  695. return false;
  696. #endif /* PAGE_SIZE >= 8192) */
  697. /* If we have drained the page fragment pool we need to update
  698. * the pagecnt_bias and page count so that we fully restock the
  699. * number of references the driver holds.
  700. */
  701. if (unlikely(pagecnt_bias == 1)) {
  702. page_ref_add(page, USHRT_MAX - 1);
  703. rx_buf->pagecnt_bias = USHRT_MAX;
  704. }
  705. return true;
  706. }
  707. /**
  708. * ice_add_xdp_frag - Add contents of Rx buffer to xdp buf as a frag
  709. * @rx_ring: Rx descriptor ring to transact packets on
  710. * @xdp: xdp buff to place the data into
  711. * @rx_buf: buffer containing page to add
  712. * @size: packet length from rx_desc
  713. *
  714. * This function will add the data contained in rx_buf->page to the xdp buf.
  715. * It will just attach the page as a frag.
  716. */
  717. static int
  718. ice_add_xdp_frag(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
  719. struct ice_rx_buf *rx_buf, const unsigned int size)
  720. {
  721. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  722. if (!size)
  723. return 0;
  724. if (!xdp_buff_has_frags(xdp)) {
  725. sinfo->nr_frags = 0;
  726. sinfo->xdp_frags_size = 0;
  727. xdp_buff_set_frags_flag(xdp);
  728. }
  729. if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS))
  730. return -ENOMEM;
  731. __skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++, rx_buf->page,
  732. rx_buf->page_offset, size);
  733. sinfo->xdp_frags_size += size;
  734. if (page_is_pfmemalloc(rx_buf->page))
  735. xdp_buff_set_frag_pfmemalloc(xdp);
  736. return 0;
  737. }
  738. /**
  739. * ice_reuse_rx_page - page flip buffer and store it back on the ring
  740. * @rx_ring: Rx descriptor ring to store buffers on
  741. * @old_buf: donor buffer to have page reused
  742. *
  743. * Synchronizes page for reuse by the adapter
  744. */
  745. static void
  746. ice_reuse_rx_page(struct ice_rx_ring *rx_ring, struct ice_rx_buf *old_buf)
  747. {
  748. u16 nta = rx_ring->next_to_alloc;
  749. struct ice_rx_buf *new_buf;
  750. new_buf = &rx_ring->rx_buf[nta];
  751. /* update, and store next to alloc */
  752. nta++;
  753. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  754. /* Transfer page from old buffer to new buffer.
  755. * Move each member individually to avoid possible store
  756. * forwarding stalls and unnecessary copy of skb.
  757. */
  758. new_buf->dma = old_buf->dma;
  759. new_buf->page = old_buf->page;
  760. new_buf->page_offset = old_buf->page_offset;
  761. new_buf->pagecnt_bias = old_buf->pagecnt_bias;
  762. }
  763. /**
  764. * ice_get_rx_buf - Fetch Rx buffer and synchronize data for use
  765. * @rx_ring: Rx descriptor ring to transact packets on
  766. * @size: size of buffer to add to skb
  767. * @ntc: index of next to clean element
  768. *
  769. * This function will pull an Rx buffer from the ring and synchronize it
  770. * for use by the CPU.
  771. */
  772. static struct ice_rx_buf *
  773. ice_get_rx_buf(struct ice_rx_ring *rx_ring, const unsigned int size,
  774. const unsigned int ntc)
  775. {
  776. struct ice_rx_buf *rx_buf;
  777. rx_buf = &rx_ring->rx_buf[ntc];
  778. prefetchw(rx_buf->page);
  779. if (!size)
  780. return rx_buf;
  781. /* we are reusing so sync this buffer for CPU use */
  782. dma_sync_single_range_for_cpu(rx_ring->dev, rx_buf->dma,
  783. rx_buf->page_offset, size,
  784. DMA_FROM_DEVICE);
  785. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  786. rx_buf->pagecnt_bias--;
  787. return rx_buf;
  788. }
  789. /**
  790. * ice_get_pgcnts - grab page_count() for gathered fragments
  791. * @rx_ring: Rx descriptor ring to store the page counts on
  792. * @ntc: the next to clean element (not included in this frame!)
  793. *
  794. * This function is intended to be called right before running XDP
  795. * program so that the page recycling mechanism will be able to take
  796. * a correct decision regarding underlying pages; this is done in such
  797. * way as XDP program can change the refcount of page
  798. */
  799. static void ice_get_pgcnts(struct ice_rx_ring *rx_ring, unsigned int ntc)
  800. {
  801. u32 idx = rx_ring->first_desc;
  802. struct ice_rx_buf *rx_buf;
  803. u32 cnt = rx_ring->count;
  804. while (idx != ntc) {
  805. rx_buf = &rx_ring->rx_buf[idx];
  806. rx_buf->pgcnt = page_count(rx_buf->page);
  807. if (++idx == cnt)
  808. idx = 0;
  809. }
  810. }
  811. /**
  812. * ice_build_skb - Build skb around an existing buffer
  813. * @rx_ring: Rx descriptor ring to transact packets on
  814. * @xdp: xdp_buff pointing to the data
  815. *
  816. * This function builds an skb around an existing XDP buffer, taking care
  817. * to set up the skb correctly and avoid any memcpy overhead. Driver has
  818. * already combined frags (if any) to skb_shared_info.
  819. */
  820. static struct sk_buff *
  821. ice_build_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
  822. {
  823. u8 metasize = xdp->data - xdp->data_meta;
  824. struct skb_shared_info *sinfo = NULL;
  825. unsigned int nr_frags;
  826. struct sk_buff *skb;
  827. if (unlikely(xdp_buff_has_frags(xdp))) {
  828. sinfo = xdp_get_shared_info_from_buff(xdp);
  829. nr_frags = sinfo->nr_frags;
  830. }
  831. /* Prefetch first cache line of first page. If xdp->data_meta
  832. * is unused, this points exactly as xdp->data, otherwise we
  833. * likely have a consumer accessing first few bytes of meta
  834. * data, and then actual data.
  835. */
  836. net_prefetch(xdp->data_meta);
  837. /* build an skb around the page buffer */
  838. skb = napi_build_skb(xdp->data_hard_start, xdp->frame_sz);
  839. if (unlikely(!skb))
  840. return NULL;
  841. /* must to record Rx queue, otherwise OS features such as
  842. * symmetric queue won't work
  843. */
  844. skb_record_rx_queue(skb, rx_ring->q_index);
  845. /* update pointers within the skb to store the data */
  846. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  847. __skb_put(skb, xdp->data_end - xdp->data);
  848. if (metasize)
  849. skb_metadata_set(skb, metasize);
  850. if (unlikely(xdp_buff_has_frags(xdp)))
  851. xdp_update_skb_shared_info(skb, nr_frags,
  852. sinfo->xdp_frags_size,
  853. nr_frags * xdp->frame_sz,
  854. xdp_buff_is_frag_pfmemalloc(xdp));
  855. return skb;
  856. }
  857. /**
  858. * ice_construct_skb - Allocate skb and populate it
  859. * @rx_ring: Rx descriptor ring to transact packets on
  860. * @xdp: xdp_buff pointing to the data
  861. *
  862. * This function allocates an skb. It then populates it with the page
  863. * data from the current receive descriptor, taking care to set up the
  864. * skb correctly.
  865. */
  866. static struct sk_buff *
  867. ice_construct_skb(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
  868. {
  869. unsigned int size = xdp->data_end - xdp->data;
  870. struct skb_shared_info *sinfo = NULL;
  871. struct ice_rx_buf *rx_buf;
  872. unsigned int nr_frags = 0;
  873. unsigned int headlen;
  874. struct sk_buff *skb;
  875. /* prefetch first cache line of first page */
  876. net_prefetch(xdp->data);
  877. if (unlikely(xdp_buff_has_frags(xdp))) {
  878. sinfo = xdp_get_shared_info_from_buff(xdp);
  879. nr_frags = sinfo->nr_frags;
  880. }
  881. /* allocate a skb to store the frags */
  882. skb = napi_alloc_skb(&rx_ring->q_vector->napi, ICE_RX_HDR_SIZE);
  883. if (unlikely(!skb))
  884. return NULL;
  885. rx_buf = &rx_ring->rx_buf[rx_ring->first_desc];
  886. skb_record_rx_queue(skb, rx_ring->q_index);
  887. /* Determine available headroom for copy */
  888. headlen = size;
  889. if (headlen > ICE_RX_HDR_SIZE)
  890. headlen = eth_get_headlen(skb->dev, xdp->data, ICE_RX_HDR_SIZE);
  891. /* align pull length to size of long to optimize memcpy performance */
  892. memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen,
  893. sizeof(long)));
  894. /* if we exhaust the linear part then add what is left as a frag */
  895. size -= headlen;
  896. if (size) {
  897. /* besides adding here a partial frag, we are going to add
  898. * frags from xdp_buff, make sure there is enough space for
  899. * them
  900. */
  901. if (unlikely(nr_frags >= MAX_SKB_FRAGS - 1)) {
  902. dev_kfree_skb(skb);
  903. return NULL;
  904. }
  905. skb_add_rx_frag(skb, 0, rx_buf->page,
  906. rx_buf->page_offset + headlen, size,
  907. xdp->frame_sz);
  908. } else {
  909. /* buffer is unused, restore biased page count in Rx buffer;
  910. * data was copied onto skb's linear part so there's no
  911. * need for adjusting page offset and we can reuse this buffer
  912. * as-is
  913. */
  914. rx_buf->pagecnt_bias++;
  915. }
  916. if (unlikely(xdp_buff_has_frags(xdp))) {
  917. struct skb_shared_info *skinfo = skb_shinfo(skb);
  918. memcpy(&skinfo->frags[skinfo->nr_frags], &sinfo->frags[0],
  919. sizeof(skb_frag_t) * nr_frags);
  920. xdp_update_skb_shared_info(skb, skinfo->nr_frags + nr_frags,
  921. sinfo->xdp_frags_size,
  922. nr_frags * xdp->frame_sz,
  923. xdp_buff_is_frag_pfmemalloc(xdp));
  924. }
  925. return skb;
  926. }
  927. /**
  928. * ice_put_rx_buf - Clean up used buffer and either recycle or free
  929. * @rx_ring: Rx descriptor ring to transact packets on
  930. * @rx_buf: Rx buffer to pull data from
  931. *
  932. * This function will clean up the contents of the rx_buf. It will either
  933. * recycle the buffer or unmap it and free the associated resources.
  934. */
  935. static void
  936. ice_put_rx_buf(struct ice_rx_ring *rx_ring, struct ice_rx_buf *rx_buf)
  937. {
  938. if (!rx_buf)
  939. return;
  940. if (ice_can_reuse_rx_page(rx_buf)) {
  941. /* hand second half of page back to the ring */
  942. ice_reuse_rx_page(rx_ring, rx_buf);
  943. } else {
  944. /* we are not reusing the buffer so unmap it */
  945. dma_unmap_page_attrs(rx_ring->dev, rx_buf->dma,
  946. ice_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
  947. ICE_RX_DMA_ATTR);
  948. __page_frag_cache_drain(rx_buf->page, rx_buf->pagecnt_bias);
  949. }
  950. /* clear contents of buffer_info */
  951. rx_buf->page = NULL;
  952. }
  953. /**
  954. * ice_put_rx_mbuf - ice_put_rx_buf() caller, for all buffers in frame
  955. * @rx_ring: Rx ring with all the auxiliary data
  956. * @xdp: XDP buffer carrying linear + frags part
  957. * @ntc: the next to clean element (not included in this frame!)
  958. * @verdict: return code from XDP program execution
  959. *
  960. * Called after XDP program is completed, or on error with verdict set to
  961. * ICE_XDP_CONSUMED.
  962. *
  963. * Walk through buffers from first_desc to the end of the frame, releasing
  964. * buffers and satisfying internal page recycle mechanism. The action depends
  965. * on verdict from XDP program.
  966. */
  967. static void ice_put_rx_mbuf(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
  968. u32 ntc, u32 verdict)
  969. {
  970. u32 idx = rx_ring->first_desc;
  971. u32 cnt = rx_ring->count;
  972. struct ice_rx_buf *buf;
  973. u32 xdp_frags = 0;
  974. int i = 0;
  975. if (unlikely(xdp_buff_has_frags(xdp)))
  976. xdp_frags = xdp_get_shared_info_from_buff(xdp)->nr_frags;
  977. while (idx != ntc) {
  978. buf = &rx_ring->rx_buf[idx];
  979. if (++idx == cnt)
  980. idx = 0;
  981. /* An XDP program could release fragments from the end of the
  982. * buffer. For these, we need to keep the pagecnt_bias as-is.
  983. * To do this, only adjust pagecnt_bias for fragments up to
  984. * the total remaining after the XDP program has run.
  985. */
  986. if (verdict != ICE_XDP_CONSUMED)
  987. ice_rx_buf_adjust_pg_offset(buf, xdp->frame_sz);
  988. else if (i++ <= xdp_frags)
  989. buf->pagecnt_bias++;
  990. ice_put_rx_buf(rx_ring, buf);
  991. }
  992. xdp->data = NULL;
  993. rx_ring->first_desc = ntc;
  994. }
  995. /**
  996. * ice_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  997. * @rx_ring: Rx descriptor ring to transact packets on
  998. * @budget: Total limit on number of packets to process
  999. *
  1000. * This function provides a "bounce buffer" approach to Rx interrupt
  1001. * processing. The advantage to this is that on systems that have
  1002. * expensive overhead for IOMMU access this provides a means of avoiding
  1003. * it by maintaining the mapping of the page to the system.
  1004. *
  1005. * Returns amount of work completed
  1006. */
  1007. int ice_clean_rx_irq(struct ice_rx_ring *rx_ring, int budget)
  1008. {
  1009. unsigned int total_rx_bytes = 0, total_rx_pkts = 0;
  1010. unsigned int offset = rx_ring->rx_offset;
  1011. struct xdp_buff *xdp = &rx_ring->xdp;
  1012. struct ice_tx_ring *xdp_ring = NULL;
  1013. struct bpf_prog *xdp_prog = NULL;
  1014. u32 ntc = rx_ring->next_to_clean;
  1015. u32 cached_ntu, xdp_verdict;
  1016. u32 cnt = rx_ring->count;
  1017. u32 xdp_xmit = 0;
  1018. bool failure;
  1019. xdp_prog = READ_ONCE(rx_ring->xdp_prog);
  1020. if (xdp_prog) {
  1021. xdp_ring = rx_ring->xdp_ring;
  1022. cached_ntu = xdp_ring->next_to_use;
  1023. }
  1024. /* start the loop to process Rx packets bounded by 'budget' */
  1025. while (likely(total_rx_pkts < (unsigned int)budget)) {
  1026. union ice_32b_rx_flex_desc *rx_desc;
  1027. struct ice_rx_buf *rx_buf;
  1028. struct sk_buff *skb;
  1029. unsigned int size;
  1030. u16 stat_err_bits;
  1031. u16 vlan_tci;
  1032. /* get the Rx desc from Rx ring based on 'next_to_clean' */
  1033. rx_desc = ICE_RX_DESC(rx_ring, ntc);
  1034. /* status_error_len will always be zero for unused descriptors
  1035. * because it's cleared in cleanup, and overlaps with hdr_addr
  1036. * which is always zero because packet split isn't used, if the
  1037. * hardware wrote DD then it will be non-zero
  1038. */
  1039. stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
  1040. if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits))
  1041. break;
  1042. /* This memory barrier is needed to keep us from reading
  1043. * any other fields out of the rx_desc until we know the
  1044. * DD bit is set.
  1045. */
  1046. dma_rmb();
  1047. ice_trace(clean_rx_irq, rx_ring, rx_desc);
  1048. if (rx_desc->wb.rxdid == FDIR_DESC_RXDID || !rx_ring->netdev) {
  1049. struct ice_vsi *ctrl_vsi = rx_ring->vsi;
  1050. if (rx_desc->wb.rxdid == FDIR_DESC_RXDID &&
  1051. ctrl_vsi->vf)
  1052. ice_vc_fdir_irq_handler(ctrl_vsi, rx_desc);
  1053. if (++ntc == cnt)
  1054. ntc = 0;
  1055. rx_ring->first_desc = ntc;
  1056. continue;
  1057. }
  1058. size = le16_to_cpu(rx_desc->wb.pkt_len) &
  1059. ICE_RX_FLX_DESC_PKT_LEN_M;
  1060. /* retrieve a buffer from the ring */
  1061. rx_buf = ice_get_rx_buf(rx_ring, size, ntc);
  1062. /* Increment ntc before calls to ice_put_rx_mbuf() */
  1063. if (++ntc == cnt)
  1064. ntc = 0;
  1065. if (!xdp->data) {
  1066. void *hard_start;
  1067. hard_start = page_address(rx_buf->page) + rx_buf->page_offset -
  1068. offset;
  1069. xdp_prepare_buff(xdp, hard_start, offset, size, !!offset);
  1070. xdp_buff_clear_frags_flag(xdp);
  1071. } else if (ice_add_xdp_frag(rx_ring, xdp, rx_buf, size)) {
  1072. ice_put_rx_mbuf(rx_ring, xdp, ntc, ICE_XDP_CONSUMED);
  1073. break;
  1074. }
  1075. /* skip if it is NOP desc */
  1076. if (ice_is_non_eop(rx_ring, rx_desc))
  1077. continue;
  1078. ice_get_pgcnts(rx_ring, ntc);
  1079. xdp_verdict = ice_run_xdp(rx_ring, xdp, xdp_prog, xdp_ring, rx_desc);
  1080. if (xdp_verdict == ICE_XDP_PASS)
  1081. goto construct_skb;
  1082. total_rx_bytes += xdp_get_buff_len(xdp);
  1083. total_rx_pkts++;
  1084. ice_put_rx_mbuf(rx_ring, xdp, ntc, xdp_verdict);
  1085. xdp_xmit |= xdp_verdict & (ICE_XDP_TX | ICE_XDP_REDIR);
  1086. continue;
  1087. construct_skb:
  1088. if (likely(ice_ring_uses_build_skb(rx_ring)))
  1089. skb = ice_build_skb(rx_ring, xdp);
  1090. else
  1091. skb = ice_construct_skb(rx_ring, xdp);
  1092. /* exit if we failed to retrieve a buffer */
  1093. if (!skb) {
  1094. rx_ring->ring_stats->rx_stats.alloc_buf_failed++;
  1095. xdp_verdict = ICE_XDP_CONSUMED;
  1096. }
  1097. ice_put_rx_mbuf(rx_ring, xdp, ntc, xdp_verdict);
  1098. if (!skb)
  1099. break;
  1100. stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S);
  1101. if (unlikely(ice_test_staterr(rx_desc->wb.status_error0,
  1102. stat_err_bits))) {
  1103. dev_kfree_skb_any(skb);
  1104. continue;
  1105. }
  1106. vlan_tci = ice_get_vlan_tci(rx_desc);
  1107. /* pad the skb if needed, to make a valid ethernet frame */
  1108. if (eth_skb_pad(skb))
  1109. continue;
  1110. /* probably a little skewed due to removing CRC */
  1111. total_rx_bytes += skb->len;
  1112. /* populate checksum, VLAN, and protocol */
  1113. ice_process_skb_fields(rx_ring, rx_desc, skb);
  1114. ice_trace(clean_rx_irq_indicate, rx_ring, rx_desc, skb);
  1115. /* send completed skb up the stack */
  1116. ice_receive_skb(rx_ring, skb, vlan_tci);
  1117. /* update budget accounting */
  1118. total_rx_pkts++;
  1119. }
  1120. rx_ring->next_to_clean = ntc;
  1121. /* return up to cleaned_count buffers to hardware */
  1122. failure = ice_alloc_rx_bufs(rx_ring, ICE_RX_DESC_UNUSED(rx_ring));
  1123. if (xdp_xmit)
  1124. ice_finalize_xdp_rx(xdp_ring, xdp_xmit, cached_ntu);
  1125. if (rx_ring->ring_stats)
  1126. ice_update_rx_ring_stats(rx_ring, total_rx_pkts,
  1127. total_rx_bytes);
  1128. /* guarantee a trip back through this routine if there was a failure */
  1129. return failure ? budget : (int)total_rx_pkts;
  1130. }
  1131. static void __ice_update_sample(struct ice_q_vector *q_vector,
  1132. struct ice_ring_container *rc,
  1133. struct dim_sample *sample,
  1134. bool is_tx)
  1135. {
  1136. u64 packets = 0, bytes = 0;
  1137. if (is_tx) {
  1138. struct ice_tx_ring *tx_ring;
  1139. ice_for_each_tx_ring(tx_ring, *rc) {
  1140. struct ice_ring_stats *ring_stats;
  1141. ring_stats = tx_ring->ring_stats;
  1142. if (!ring_stats)
  1143. continue;
  1144. packets += ring_stats->stats.pkts;
  1145. bytes += ring_stats->stats.bytes;
  1146. }
  1147. } else {
  1148. struct ice_rx_ring *rx_ring;
  1149. ice_for_each_rx_ring(rx_ring, *rc) {
  1150. struct ice_ring_stats *ring_stats;
  1151. ring_stats = rx_ring->ring_stats;
  1152. if (!ring_stats)
  1153. continue;
  1154. packets += ring_stats->stats.pkts;
  1155. bytes += ring_stats->stats.bytes;
  1156. }
  1157. }
  1158. dim_update_sample(q_vector->total_events, packets, bytes, sample);
  1159. sample->comp_ctr = 0;
  1160. /* if dim settings get stale, like when not updated for 1
  1161. * second or longer, force it to start again. This addresses the
  1162. * frequent case of an idle queue being switched to by the
  1163. * scheduler. The 1,000 here means 1,000 milliseconds.
  1164. */
  1165. if (ktime_ms_delta(sample->time, rc->dim.start_sample.time) >= 1000)
  1166. rc->dim.state = DIM_START_MEASURE;
  1167. }
  1168. /**
  1169. * ice_net_dim - Update net DIM algorithm
  1170. * @q_vector: the vector associated with the interrupt
  1171. *
  1172. * Create a DIM sample and notify net_dim() so that it can possibly decide
  1173. * a new ITR value based on incoming packets, bytes, and interrupts.
  1174. *
  1175. * This function is a no-op if the ring is not configured to dynamic ITR.
  1176. */
  1177. static void ice_net_dim(struct ice_q_vector *q_vector)
  1178. {
  1179. struct ice_ring_container *tx = &q_vector->tx;
  1180. struct ice_ring_container *rx = &q_vector->rx;
  1181. if (ITR_IS_DYNAMIC(tx)) {
  1182. struct dim_sample dim_sample;
  1183. __ice_update_sample(q_vector, tx, &dim_sample, true);
  1184. net_dim(&tx->dim, dim_sample);
  1185. }
  1186. if (ITR_IS_DYNAMIC(rx)) {
  1187. struct dim_sample dim_sample;
  1188. __ice_update_sample(q_vector, rx, &dim_sample, false);
  1189. net_dim(&rx->dim, dim_sample);
  1190. }
  1191. }
  1192. /**
  1193. * ice_buildreg_itr - build value for writing to the GLINT_DYN_CTL register
  1194. * @itr_idx: interrupt throttling index
  1195. * @itr: interrupt throttling value in usecs
  1196. */
  1197. static u32 ice_buildreg_itr(u16 itr_idx, u16 itr)
  1198. {
  1199. /* The ITR value is reported in microseconds, and the register value is
  1200. * recorded in 2 microsecond units. For this reason we only need to
  1201. * shift by the GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S to apply this
  1202. * granularity as a shift instead of division. The mask makes sure the
  1203. * ITR value is never odd so we don't accidentally write into the field
  1204. * prior to the ITR field.
  1205. */
  1206. itr &= ICE_ITR_MASK;
  1207. return GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
  1208. (itr_idx << GLINT_DYN_CTL_ITR_INDX_S) |
  1209. (itr << (GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S));
  1210. }
  1211. /**
  1212. * ice_enable_interrupt - re-enable MSI-X interrupt
  1213. * @q_vector: the vector associated with the interrupt to enable
  1214. *
  1215. * If the VSI is down, the interrupt will not be re-enabled. Also,
  1216. * when enabling the interrupt always reset the wb_on_itr to false
  1217. * and trigger a software interrupt to clean out internal state.
  1218. */
  1219. static void ice_enable_interrupt(struct ice_q_vector *q_vector)
  1220. {
  1221. struct ice_vsi *vsi = q_vector->vsi;
  1222. bool wb_en = q_vector->wb_on_itr;
  1223. u32 itr_val;
  1224. if (test_bit(ICE_DOWN, vsi->state))
  1225. return;
  1226. /* trigger an ITR delayed software interrupt when exiting busy poll, to
  1227. * make sure to catch any pending cleanups that might have been missed
  1228. * due to interrupt state transition. If busy poll or poll isn't
  1229. * enabled, then don't update ITR, and just enable the interrupt.
  1230. */
  1231. if (!wb_en) {
  1232. itr_val = ice_buildreg_itr(ICE_ITR_NONE, 0);
  1233. } else {
  1234. q_vector->wb_on_itr = false;
  1235. /* do two things here with a single write. Set up the third ITR
  1236. * index to be used for software interrupt moderation, and then
  1237. * trigger a software interrupt with a rate limit of 20K on
  1238. * software interrupts, this will help avoid high interrupt
  1239. * loads due to frequently polling and exiting polling.
  1240. */
  1241. itr_val = ice_buildreg_itr(ICE_IDX_ITR2, ICE_ITR_20K);
  1242. itr_val |= GLINT_DYN_CTL_SWINT_TRIG_M |
  1243. ICE_IDX_ITR2 << GLINT_DYN_CTL_SW_ITR_INDX_S |
  1244. GLINT_DYN_CTL_SW_ITR_INDX_ENA_M;
  1245. }
  1246. wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), itr_val);
  1247. }
  1248. /**
  1249. * ice_set_wb_on_itr - set WB_ON_ITR for this q_vector
  1250. * @q_vector: q_vector to set WB_ON_ITR on
  1251. *
  1252. * We need to tell hardware to write-back completed descriptors even when
  1253. * interrupts are disabled. Descriptors will be written back on cache line
  1254. * boundaries without WB_ON_ITR enabled, but if we don't enable WB_ON_ITR
  1255. * descriptors may not be written back if they don't fill a cache line until
  1256. * the next interrupt.
  1257. *
  1258. * This sets the write-back frequency to whatever was set previously for the
  1259. * ITR indices. Also, set the INTENA_MSK bit to make sure hardware knows we
  1260. * aren't meddling with the INTENA_M bit.
  1261. */
  1262. static void ice_set_wb_on_itr(struct ice_q_vector *q_vector)
  1263. {
  1264. struct ice_vsi *vsi = q_vector->vsi;
  1265. /* already in wb_on_itr mode no need to change it */
  1266. if (q_vector->wb_on_itr)
  1267. return;
  1268. /* use previously set ITR values for all of the ITR indices by
  1269. * specifying ICE_ITR_NONE, which will vary in adaptive (AIM) mode and
  1270. * be static in non-adaptive mode (user configured)
  1271. */
  1272. wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx),
  1273. FIELD_PREP(GLINT_DYN_CTL_ITR_INDX_M, ICE_ITR_NONE) |
  1274. FIELD_PREP(GLINT_DYN_CTL_INTENA_MSK_M, 1) |
  1275. FIELD_PREP(GLINT_DYN_CTL_WB_ON_ITR_M, 1));
  1276. q_vector->wb_on_itr = true;
  1277. }
  1278. /**
  1279. * ice_napi_poll - NAPI polling Rx/Tx cleanup routine
  1280. * @napi: napi struct with our devices info in it
  1281. * @budget: amount of work driver is allowed to do this pass, in packets
  1282. *
  1283. * This function will clean all queues associated with a q_vector.
  1284. *
  1285. * Returns the amount of work done
  1286. */
  1287. int ice_napi_poll(struct napi_struct *napi, int budget)
  1288. {
  1289. struct ice_q_vector *q_vector =
  1290. container_of(napi, struct ice_q_vector, napi);
  1291. struct ice_tx_ring *tx_ring;
  1292. struct ice_rx_ring *rx_ring;
  1293. bool clean_complete = true;
  1294. int budget_per_ring;
  1295. int work_done = 0;
  1296. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1297. * budget and be more aggressive about cleaning up the Tx descriptors.
  1298. */
  1299. ice_for_each_tx_ring(tx_ring, q_vector->tx) {
  1300. struct xsk_buff_pool *xsk_pool = READ_ONCE(tx_ring->xsk_pool);
  1301. bool wd;
  1302. if (xsk_pool)
  1303. wd = ice_xmit_zc(tx_ring, xsk_pool);
  1304. else if (ice_ring_is_xdp(tx_ring))
  1305. wd = true;
  1306. else
  1307. wd = ice_clean_tx_irq(tx_ring, budget);
  1308. if (!wd)
  1309. clean_complete = false;
  1310. }
  1311. /* Handle case where we are called by netpoll with a budget of 0 */
  1312. if (unlikely(budget <= 0))
  1313. return budget;
  1314. /* normally we have 1 Rx ring per q_vector */
  1315. if (unlikely(q_vector->num_ring_rx > 1))
  1316. /* We attempt to distribute budget to each Rx queue fairly, but
  1317. * don't allow the budget to go below 1 because that would exit
  1318. * polling early.
  1319. */
  1320. budget_per_ring = max_t(int, budget / q_vector->num_ring_rx, 1);
  1321. else
  1322. /* Max of 1 Rx ring in this q_vector so give it the budget */
  1323. budget_per_ring = budget;
  1324. ice_for_each_rx_ring(rx_ring, q_vector->rx) {
  1325. struct xsk_buff_pool *xsk_pool = READ_ONCE(rx_ring->xsk_pool);
  1326. int cleaned;
  1327. /* A dedicated path for zero-copy allows making a single
  1328. * comparison in the irq context instead of many inside the
  1329. * ice_clean_rx_irq function and makes the codebase cleaner.
  1330. */
  1331. cleaned = rx_ring->xsk_pool ?
  1332. ice_clean_rx_irq_zc(rx_ring, xsk_pool, budget_per_ring) :
  1333. ice_clean_rx_irq(rx_ring, budget_per_ring);
  1334. work_done += cleaned;
  1335. /* if we clean as many as budgeted, we must not be done */
  1336. if (cleaned >= budget_per_ring)
  1337. clean_complete = false;
  1338. }
  1339. /* If work not completed, return budget and polling will return */
  1340. if (!clean_complete) {
  1341. /* Set the writeback on ITR so partial completions of
  1342. * cache-lines will still continue even if we're polling.
  1343. */
  1344. ice_set_wb_on_itr(q_vector);
  1345. return budget;
  1346. }
  1347. /* Exit the polling mode, but don't re-enable interrupts if stack might
  1348. * poll us due to busy-polling
  1349. */
  1350. if (napi_complete_done(napi, work_done)) {
  1351. ice_net_dim(q_vector);
  1352. ice_enable_interrupt(q_vector);
  1353. } else {
  1354. ice_set_wb_on_itr(q_vector);
  1355. }
  1356. return min_t(int, work_done, budget - 1);
  1357. }
  1358. /**
  1359. * __ice_maybe_stop_tx - 2nd level check for Tx stop conditions
  1360. * @tx_ring: the ring to be checked
  1361. * @size: the size buffer we want to assure is available
  1362. *
  1363. * Returns -EBUSY if a stop is needed, else 0
  1364. */
  1365. static int __ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size)
  1366. {
  1367. netif_tx_stop_queue(txring_txq(tx_ring));
  1368. /* Memory barrier before checking head and tail */
  1369. smp_mb();
  1370. /* Check again in a case another CPU has just made room available. */
  1371. if (likely(ICE_DESC_UNUSED(tx_ring) < size))
  1372. return -EBUSY;
  1373. /* A reprieve! - use start_queue because it doesn't call schedule */
  1374. netif_tx_start_queue(txring_txq(tx_ring));
  1375. ++tx_ring->ring_stats->tx_stats.restart_q;
  1376. return 0;
  1377. }
  1378. /**
  1379. * ice_maybe_stop_tx - 1st level check for Tx stop conditions
  1380. * @tx_ring: the ring to be checked
  1381. * @size: the size buffer we want to assure is available
  1382. *
  1383. * Returns 0 if stop is not needed
  1384. */
  1385. static int ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size)
  1386. {
  1387. if (likely(ICE_DESC_UNUSED(tx_ring) >= size))
  1388. return 0;
  1389. return __ice_maybe_stop_tx(tx_ring, size);
  1390. }
  1391. /**
  1392. * ice_tx_map - Build the Tx descriptor
  1393. * @tx_ring: ring to send buffer on
  1394. * @first: first buffer info buffer to use
  1395. * @off: pointer to struct that holds offload parameters
  1396. *
  1397. * This function loops over the skb data pointed to by *first
  1398. * and gets a physical address for each memory location and programs
  1399. * it and the length into the transmit descriptor.
  1400. */
  1401. static void
  1402. ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first,
  1403. struct ice_tx_offload_params *off)
  1404. {
  1405. u64 td_offset, td_tag, td_cmd;
  1406. u16 i = tx_ring->next_to_use;
  1407. unsigned int data_len, size;
  1408. struct ice_tx_desc *tx_desc;
  1409. struct ice_tx_buf *tx_buf;
  1410. struct sk_buff *skb;
  1411. skb_frag_t *frag;
  1412. dma_addr_t dma;
  1413. bool kick;
  1414. td_tag = off->td_l2tag1;
  1415. td_cmd = off->td_cmd;
  1416. td_offset = off->td_offset;
  1417. skb = first->skb;
  1418. data_len = skb->data_len;
  1419. size = skb_headlen(skb);
  1420. tx_desc = ICE_TX_DESC(tx_ring, i);
  1421. if (first->tx_flags & ICE_TX_FLAGS_HW_VLAN) {
  1422. td_cmd |= (u64)ICE_TX_DESC_CMD_IL2TAG1;
  1423. td_tag = first->vid;
  1424. }
  1425. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1426. tx_buf = first;
  1427. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1428. unsigned int max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
  1429. if (dma_mapping_error(tx_ring->dev, dma))
  1430. goto dma_error;
  1431. /* record length, and DMA address */
  1432. dma_unmap_len_set(tx_buf, len, size);
  1433. dma_unmap_addr_set(tx_buf, dma, dma);
  1434. /* align size to end of page */
  1435. max_data += -dma & (ICE_MAX_READ_REQ_SIZE - 1);
  1436. tx_desc->buf_addr = cpu_to_le64(dma);
  1437. /* account for data chunks larger than the hardware
  1438. * can handle
  1439. */
  1440. while (unlikely(size > ICE_MAX_DATA_PER_TXD)) {
  1441. tx_desc->cmd_type_offset_bsz =
  1442. ice_build_ctob(td_cmd, td_offset, max_data,
  1443. td_tag);
  1444. tx_desc++;
  1445. i++;
  1446. if (i == tx_ring->count) {
  1447. tx_desc = ICE_TX_DESC(tx_ring, 0);
  1448. i = 0;
  1449. }
  1450. dma += max_data;
  1451. size -= max_data;
  1452. max_data = ICE_MAX_DATA_PER_TXD_ALIGNED;
  1453. tx_desc->buf_addr = cpu_to_le64(dma);
  1454. }
  1455. if (likely(!data_len))
  1456. break;
  1457. tx_desc->cmd_type_offset_bsz = ice_build_ctob(td_cmd, td_offset,
  1458. size, td_tag);
  1459. tx_desc++;
  1460. i++;
  1461. if (i == tx_ring->count) {
  1462. tx_desc = ICE_TX_DESC(tx_ring, 0);
  1463. i = 0;
  1464. }
  1465. size = skb_frag_size(frag);
  1466. data_len -= size;
  1467. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1468. DMA_TO_DEVICE);
  1469. tx_buf = &tx_ring->tx_buf[i];
  1470. tx_buf->type = ICE_TX_BUF_FRAG;
  1471. }
  1472. /* record SW timestamp if HW timestamp is not available */
  1473. skb_tx_timestamp(first->skb);
  1474. i++;
  1475. if (i == tx_ring->count)
  1476. i = 0;
  1477. /* write last descriptor with RS and EOP bits */
  1478. td_cmd |= (u64)ICE_TXD_LAST_DESC_CMD;
  1479. tx_desc->cmd_type_offset_bsz =
  1480. ice_build_ctob(td_cmd, td_offset, size, td_tag);
  1481. /* Force memory writes to complete before letting h/w know there
  1482. * are new descriptors to fetch.
  1483. *
  1484. * We also use this memory barrier to make certain all of the
  1485. * status bits have been updated before next_to_watch is written.
  1486. */
  1487. wmb();
  1488. /* set next_to_watch value indicating a packet is present */
  1489. first->next_to_watch = tx_desc;
  1490. tx_ring->next_to_use = i;
  1491. ice_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1492. /* notify HW of packet */
  1493. kick = __netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount,
  1494. netdev_xmit_more());
  1495. if (kick)
  1496. /* notify HW of packet */
  1497. writel(i, tx_ring->tail);
  1498. return;
  1499. dma_error:
  1500. /* clear DMA mappings for failed tx_buf map */
  1501. for (;;) {
  1502. tx_buf = &tx_ring->tx_buf[i];
  1503. ice_unmap_and_free_tx_buf(tx_ring, tx_buf);
  1504. if (tx_buf == first)
  1505. break;
  1506. if (i == 0)
  1507. i = tx_ring->count;
  1508. i--;
  1509. }
  1510. tx_ring->next_to_use = i;
  1511. }
  1512. /**
  1513. * ice_tx_csum - Enable Tx checksum offloads
  1514. * @first: pointer to the first descriptor
  1515. * @off: pointer to struct that holds offload parameters
  1516. *
  1517. * Returns 0 or error (negative) if checksum offload can't happen, 1 otherwise.
  1518. */
  1519. static
  1520. int ice_tx_csum(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
  1521. {
  1522. u32 l4_len = 0, l3_len = 0, l2_len = 0;
  1523. struct sk_buff *skb = first->skb;
  1524. union {
  1525. struct iphdr *v4;
  1526. struct ipv6hdr *v6;
  1527. unsigned char *hdr;
  1528. } ip;
  1529. union {
  1530. struct tcphdr *tcp;
  1531. unsigned char *hdr;
  1532. } l4;
  1533. __be16 frag_off, protocol;
  1534. unsigned char *exthdr;
  1535. u32 offset, cmd = 0;
  1536. u8 l4_proto = 0;
  1537. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1538. return 0;
  1539. protocol = vlan_get_protocol(skb);
  1540. if (eth_p_mpls(protocol)) {
  1541. ip.hdr = skb_inner_network_header(skb);
  1542. l4.hdr = skb_checksum_start(skb);
  1543. } else {
  1544. ip.hdr = skb_network_header(skb);
  1545. l4.hdr = skb_transport_header(skb);
  1546. }
  1547. /* compute outer L2 header size */
  1548. l2_len = ip.hdr - skb->data;
  1549. offset = (l2_len / 2) << ICE_TX_DESC_LEN_MACLEN_S;
  1550. /* set the tx_flags to indicate the IP protocol type. this is
  1551. * required so that checksum header computation below is accurate.
  1552. */
  1553. if (ip.v4->version == 4)
  1554. first->tx_flags |= ICE_TX_FLAGS_IPV4;
  1555. else if (ip.v6->version == 6)
  1556. first->tx_flags |= ICE_TX_FLAGS_IPV6;
  1557. if (skb->encapsulation) {
  1558. bool gso_ena = false;
  1559. u32 tunnel = 0;
  1560. /* define outer network header type */
  1561. if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
  1562. tunnel |= (first->tx_flags & ICE_TX_FLAGS_TSO) ?
  1563. ICE_TX_CTX_EIPT_IPV4 :
  1564. ICE_TX_CTX_EIPT_IPV4_NO_CSUM;
  1565. l4_proto = ip.v4->protocol;
  1566. } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
  1567. int ret;
  1568. tunnel |= ICE_TX_CTX_EIPT_IPV6;
  1569. exthdr = ip.hdr + sizeof(*ip.v6);
  1570. l4_proto = ip.v6->nexthdr;
  1571. ret = ipv6_skip_exthdr(skb, exthdr - skb->data,
  1572. &l4_proto, &frag_off);
  1573. if (ret < 0)
  1574. return -1;
  1575. }
  1576. /* define outer transport */
  1577. switch (l4_proto) {
  1578. case IPPROTO_UDP:
  1579. tunnel |= ICE_TXD_CTX_UDP_TUNNELING;
  1580. first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
  1581. break;
  1582. case IPPROTO_GRE:
  1583. tunnel |= ICE_TXD_CTX_GRE_TUNNELING;
  1584. first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
  1585. break;
  1586. case IPPROTO_IPIP:
  1587. case IPPROTO_IPV6:
  1588. first->tx_flags |= ICE_TX_FLAGS_TUNNEL;
  1589. l4.hdr = skb_inner_network_header(skb);
  1590. break;
  1591. default:
  1592. if (first->tx_flags & ICE_TX_FLAGS_TSO)
  1593. return -1;
  1594. skb_checksum_help(skb);
  1595. return 0;
  1596. }
  1597. /* compute outer L3 header size */
  1598. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  1599. ICE_TXD_CTX_QW0_EIPLEN_S;
  1600. /* switch IP header pointer from outer to inner header */
  1601. ip.hdr = skb_inner_network_header(skb);
  1602. /* compute tunnel header size */
  1603. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  1604. ICE_TXD_CTX_QW0_NATLEN_S;
  1605. gso_ena = skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL;
  1606. /* indicate if we need to offload outer UDP header */
  1607. if ((first->tx_flags & ICE_TX_FLAGS_TSO) && !gso_ena &&
  1608. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  1609. tunnel |= ICE_TXD_CTX_QW0_L4T_CS_M;
  1610. /* record tunnel offload values */
  1611. off->cd_tunnel_params |= tunnel;
  1612. /* set DTYP=1 to indicate that it's an Tx context descriptor
  1613. * in IPsec tunnel mode with Tx offloads in Quad word 1
  1614. */
  1615. off->cd_qw1 |= (u64)ICE_TX_DESC_DTYPE_CTX;
  1616. /* switch L4 header pointer from outer to inner */
  1617. l4.hdr = skb_inner_transport_header(skb);
  1618. l4_proto = 0;
  1619. /* reset type as we transition from outer to inner headers */
  1620. first->tx_flags &= ~(ICE_TX_FLAGS_IPV4 | ICE_TX_FLAGS_IPV6);
  1621. if (ip.v4->version == 4)
  1622. first->tx_flags |= ICE_TX_FLAGS_IPV4;
  1623. if (ip.v6->version == 6)
  1624. first->tx_flags |= ICE_TX_FLAGS_IPV6;
  1625. }
  1626. /* Enable IP checksum offloads */
  1627. if (first->tx_flags & ICE_TX_FLAGS_IPV4) {
  1628. l4_proto = ip.v4->protocol;
  1629. /* the stack computes the IP header already, the only time we
  1630. * need the hardware to recompute it is in the case of TSO.
  1631. */
  1632. if (first->tx_flags & ICE_TX_FLAGS_TSO)
  1633. cmd |= ICE_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1634. else
  1635. cmd |= ICE_TX_DESC_CMD_IIPT_IPV4;
  1636. } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) {
  1637. cmd |= ICE_TX_DESC_CMD_IIPT_IPV6;
  1638. exthdr = ip.hdr + sizeof(*ip.v6);
  1639. l4_proto = ip.v6->nexthdr;
  1640. if (l4.hdr != exthdr)
  1641. ipv6_skip_exthdr(skb, exthdr - skb->data, &l4_proto,
  1642. &frag_off);
  1643. } else {
  1644. return -1;
  1645. }
  1646. /* compute inner L3 header size */
  1647. l3_len = l4.hdr - ip.hdr;
  1648. offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;
  1649. /* Enable L4 checksum offloads */
  1650. switch (l4_proto) {
  1651. case IPPROTO_TCP:
  1652. /* enable checksum offloads */
  1653. cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
  1654. l4_len = l4.tcp->doff;
  1655. offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
  1656. break;
  1657. case IPPROTO_UDP:
  1658. /* enable UDP checksum offload */
  1659. cmd |= ICE_TX_DESC_CMD_L4T_EOFT_UDP;
  1660. l4_len = (sizeof(struct udphdr) >> 2);
  1661. offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
  1662. break;
  1663. case IPPROTO_SCTP:
  1664. /* enable SCTP checksum offload */
  1665. cmd |= ICE_TX_DESC_CMD_L4T_EOFT_SCTP;
  1666. l4_len = sizeof(struct sctphdr) >> 2;
  1667. offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S;
  1668. break;
  1669. default:
  1670. if (first->tx_flags & ICE_TX_FLAGS_TSO)
  1671. return -1;
  1672. skb_checksum_help(skb);
  1673. return 0;
  1674. }
  1675. off->td_cmd |= cmd;
  1676. off->td_offset |= offset;
  1677. return 1;
  1678. }
  1679. /**
  1680. * ice_tx_prepare_vlan_flags - prepare generic Tx VLAN tagging flags for HW
  1681. * @tx_ring: ring to send buffer on
  1682. * @first: pointer to struct ice_tx_buf
  1683. *
  1684. * Checks the skb and set up correspondingly several generic transmit flags
  1685. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1686. */
  1687. static void
  1688. ice_tx_prepare_vlan_flags(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first)
  1689. {
  1690. struct sk_buff *skb = first->skb;
  1691. /* nothing left to do, software offloaded VLAN */
  1692. if (!skb_vlan_tag_present(skb) && eth_type_vlan(skb->protocol))
  1693. return;
  1694. /* the VLAN ethertype/tpid is determined by VSI configuration and netdev
  1695. * feature flags, which the driver only allows either 802.1Q or 802.1ad
  1696. * VLAN offloads exclusively so we only care about the VLAN ID here
  1697. */
  1698. if (skb_vlan_tag_present(skb)) {
  1699. first->vid = skb_vlan_tag_get(skb);
  1700. if (tx_ring->flags & ICE_TX_FLAGS_RING_VLAN_L2TAG2)
  1701. first->tx_flags |= ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN;
  1702. else
  1703. first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
  1704. }
  1705. ice_tx_prepare_vlan_flags_dcb(tx_ring, first);
  1706. }
  1707. /**
  1708. * ice_tso - computes mss and TSO length to prepare for TSO
  1709. * @first: pointer to struct ice_tx_buf
  1710. * @off: pointer to struct that holds offload parameters
  1711. *
  1712. * Returns 0 or error (negative) if TSO can't happen, 1 otherwise.
  1713. */
  1714. static
  1715. int ice_tso(struct ice_tx_buf *first, struct ice_tx_offload_params *off)
  1716. {
  1717. struct sk_buff *skb = first->skb;
  1718. union {
  1719. struct iphdr *v4;
  1720. struct ipv6hdr *v6;
  1721. unsigned char *hdr;
  1722. } ip;
  1723. union {
  1724. struct tcphdr *tcp;
  1725. struct udphdr *udp;
  1726. unsigned char *hdr;
  1727. } l4;
  1728. u64 cd_mss, cd_tso_len;
  1729. __be16 protocol;
  1730. u32 paylen;
  1731. u8 l4_start;
  1732. int err;
  1733. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1734. return 0;
  1735. if (!skb_is_gso(skb))
  1736. return 0;
  1737. err = skb_cow_head(skb, 0);
  1738. if (err < 0)
  1739. return err;
  1740. protocol = vlan_get_protocol(skb);
  1741. if (eth_p_mpls(protocol))
  1742. ip.hdr = skb_inner_network_header(skb);
  1743. else
  1744. ip.hdr = skb_network_header(skb);
  1745. l4.hdr = skb_checksum_start(skb);
  1746. /* initialize outer IP header fields */
  1747. if (ip.v4->version == 4) {
  1748. ip.v4->tot_len = 0;
  1749. ip.v4->check = 0;
  1750. } else {
  1751. ip.v6->payload_len = 0;
  1752. }
  1753. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1754. SKB_GSO_GRE_CSUM |
  1755. SKB_GSO_IPXIP4 |
  1756. SKB_GSO_IPXIP6 |
  1757. SKB_GSO_UDP_TUNNEL |
  1758. SKB_GSO_UDP_TUNNEL_CSUM)) {
  1759. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1760. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  1761. l4.udp->len = 0;
  1762. /* determine offset of outer transport header */
  1763. l4_start = (u8)(l4.hdr - skb->data);
  1764. /* remove payload length from outer checksum */
  1765. paylen = skb->len - l4_start;
  1766. csum_replace_by_diff(&l4.udp->check,
  1767. (__force __wsum)htonl(paylen));
  1768. }
  1769. /* reset pointers to inner headers */
  1770. ip.hdr = skb_inner_network_header(skb);
  1771. l4.hdr = skb_inner_transport_header(skb);
  1772. /* initialize inner IP header fields */
  1773. if (ip.v4->version == 4) {
  1774. ip.v4->tot_len = 0;
  1775. ip.v4->check = 0;
  1776. } else {
  1777. ip.v6->payload_len = 0;
  1778. }
  1779. }
  1780. /* determine offset of transport header */
  1781. l4_start = (u8)(l4.hdr - skb->data);
  1782. /* remove payload length from checksum */
  1783. paylen = skb->len - l4_start;
  1784. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
  1785. csum_replace_by_diff(&l4.udp->check,
  1786. (__force __wsum)htonl(paylen));
  1787. /* compute length of UDP segmentation header */
  1788. off->header_len = (u8)sizeof(l4.udp) + l4_start;
  1789. } else {
  1790. csum_replace_by_diff(&l4.tcp->check,
  1791. (__force __wsum)htonl(paylen));
  1792. /* compute length of TCP segmentation header */
  1793. off->header_len = (u8)((l4.tcp->doff * 4) + l4_start);
  1794. }
  1795. /* update gso_segs and bytecount */
  1796. first->gso_segs = skb_shinfo(skb)->gso_segs;
  1797. first->bytecount += (first->gso_segs - 1) * off->header_len;
  1798. cd_tso_len = skb->len - off->header_len;
  1799. cd_mss = skb_shinfo(skb)->gso_size;
  1800. /* record cdesc_qw1 with TSO parameters */
  1801. off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
  1802. (ICE_TX_CTX_DESC_TSO << ICE_TXD_CTX_QW1_CMD_S) |
  1803. (cd_tso_len << ICE_TXD_CTX_QW1_TSO_LEN_S) |
  1804. (cd_mss << ICE_TXD_CTX_QW1_MSS_S));
  1805. first->tx_flags |= ICE_TX_FLAGS_TSO;
  1806. return 1;
  1807. }
  1808. /**
  1809. * ice_txd_use_count - estimate the number of descriptors needed for Tx
  1810. * @size: transmit request size in bytes
  1811. *
  1812. * Due to hardware alignment restrictions (4K alignment), we need to
  1813. * assume that we can have no more than 12K of data per descriptor, even
  1814. * though each descriptor can take up to 16K - 1 bytes of aligned memory.
  1815. * Thus, we need to divide by 12K. But division is slow! Instead,
  1816. * we decompose the operation into shifts and one relatively cheap
  1817. * multiply operation.
  1818. *
  1819. * To divide by 12K, we first divide by 4K, then divide by 3:
  1820. * To divide by 4K, shift right by 12 bits
  1821. * To divide by 3, multiply by 85, then divide by 256
  1822. * (Divide by 256 is done by shifting right by 8 bits)
  1823. * Finally, we add one to round up. Because 256 isn't an exact multiple of
  1824. * 3, we'll underestimate near each multiple of 12K. This is actually more
  1825. * accurate as we have 4K - 1 of wiggle room that we can fit into the last
  1826. * segment. For our purposes this is accurate out to 1M which is orders of
  1827. * magnitude greater than our largest possible GSO size.
  1828. *
  1829. * This would then be implemented as:
  1830. * return (((size >> 12) * 85) >> 8) + ICE_DESCS_FOR_SKB_DATA_PTR;
  1831. *
  1832. * Since multiplication and division are commutative, we can reorder
  1833. * operations into:
  1834. * return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
  1835. */
  1836. static unsigned int ice_txd_use_count(unsigned int size)
  1837. {
  1838. return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR;
  1839. }
  1840. /**
  1841. * ice_xmit_desc_count - calculate number of Tx descriptors needed
  1842. * @skb: send buffer
  1843. *
  1844. * Returns number of data descriptors needed for this skb.
  1845. */
  1846. static unsigned int ice_xmit_desc_count(struct sk_buff *skb)
  1847. {
  1848. const skb_frag_t *frag = &skb_shinfo(skb)->frags[0];
  1849. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  1850. unsigned int count = 0, size = skb_headlen(skb);
  1851. for (;;) {
  1852. count += ice_txd_use_count(size);
  1853. if (!nr_frags--)
  1854. break;
  1855. size = skb_frag_size(frag++);
  1856. }
  1857. return count;
  1858. }
  1859. /**
  1860. * __ice_chk_linearize - Check if there are more than 8 buffers per packet
  1861. * @skb: send buffer
  1862. *
  1863. * Note: This HW can't DMA more than 8 buffers to build a packet on the wire
  1864. * and so we need to figure out the cases where we need to linearize the skb.
  1865. *
  1866. * For TSO we need to count the TSO header and segment payload separately.
  1867. * As such we need to check cases where we have 7 fragments or more as we
  1868. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  1869. * the segment payload in the first descriptor, and another 7 for the
  1870. * fragments.
  1871. */
  1872. static bool __ice_chk_linearize(struct sk_buff *skb)
  1873. {
  1874. const skb_frag_t *frag, *stale;
  1875. int nr_frags, sum;
  1876. /* no need to check if number of frags is less than 7 */
  1877. nr_frags = skb_shinfo(skb)->nr_frags;
  1878. if (nr_frags < (ICE_MAX_BUF_TXD - 1))
  1879. return false;
  1880. /* We need to walk through the list and validate that each group
  1881. * of 6 fragments totals at least gso_size.
  1882. */
  1883. nr_frags -= ICE_MAX_BUF_TXD - 2;
  1884. frag = &skb_shinfo(skb)->frags[0];
  1885. /* Initialize size to the negative value of gso_size minus 1. We
  1886. * use this as the worst case scenario in which the frag ahead
  1887. * of us only provides one byte which is why we are limited to 6
  1888. * descriptors for a single transmit as the header and previous
  1889. * fragment are already consuming 2 descriptors.
  1890. */
  1891. sum = 1 - skb_shinfo(skb)->gso_size;
  1892. /* Add size of frags 0 through 4 to create our initial sum */
  1893. sum += skb_frag_size(frag++);
  1894. sum += skb_frag_size(frag++);
  1895. sum += skb_frag_size(frag++);
  1896. sum += skb_frag_size(frag++);
  1897. sum += skb_frag_size(frag++);
  1898. /* Walk through fragments adding latest fragment, testing it, and
  1899. * then removing stale fragments from the sum.
  1900. */
  1901. for (stale = &skb_shinfo(skb)->frags[0];; stale++) {
  1902. int stale_size = skb_frag_size(stale);
  1903. sum += skb_frag_size(frag++);
  1904. /* The stale fragment may present us with a smaller
  1905. * descriptor than the actual fragment size. To account
  1906. * for that we need to remove all the data on the front and
  1907. * figure out what the remainder would be in the last
  1908. * descriptor associated with the fragment.
  1909. */
  1910. if (stale_size > ICE_MAX_DATA_PER_TXD) {
  1911. int align_pad = -(skb_frag_off(stale)) &
  1912. (ICE_MAX_READ_REQ_SIZE - 1);
  1913. sum -= align_pad;
  1914. stale_size -= align_pad;
  1915. do {
  1916. sum -= ICE_MAX_DATA_PER_TXD_ALIGNED;
  1917. stale_size -= ICE_MAX_DATA_PER_TXD_ALIGNED;
  1918. } while (stale_size > ICE_MAX_DATA_PER_TXD);
  1919. }
  1920. /* if sum is negative we failed to make sufficient progress */
  1921. if (sum < 0)
  1922. return true;
  1923. if (!nr_frags--)
  1924. break;
  1925. sum -= stale_size;
  1926. }
  1927. return false;
  1928. }
  1929. /**
  1930. * ice_chk_linearize - Check if there are more than 8 fragments per packet
  1931. * @skb: send buffer
  1932. * @count: number of buffers used
  1933. *
  1934. * Note: Our HW can't scatter-gather more than 8 fragments to build
  1935. * a packet on the wire and so we need to figure out the cases where we
  1936. * need to linearize the skb.
  1937. */
  1938. static bool ice_chk_linearize(struct sk_buff *skb, unsigned int count)
  1939. {
  1940. /* Both TSO and single send will work if count is less than 8 */
  1941. if (likely(count < ICE_MAX_BUF_TXD))
  1942. return false;
  1943. if (skb_is_gso(skb))
  1944. return __ice_chk_linearize(skb);
  1945. /* we can support up to 8 data buffers for a single send */
  1946. return count != ICE_MAX_BUF_TXD;
  1947. }
  1948. /**
  1949. * ice_tstamp - set up context descriptor for hardware timestamp
  1950. * @tx_ring: pointer to the Tx ring to send buffer on
  1951. * @skb: pointer to the SKB we're sending
  1952. * @first: Tx buffer
  1953. * @off: Tx offload parameters
  1954. */
  1955. static void
  1956. ice_tstamp(struct ice_tx_ring *tx_ring, struct sk_buff *skb,
  1957. struct ice_tx_buf *first, struct ice_tx_offload_params *off)
  1958. {
  1959. s8 idx;
  1960. /* only timestamp the outbound packet if the user has requested it */
  1961. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  1962. return;
  1963. /* Tx timestamps cannot be sampled when doing TSO */
  1964. if (first->tx_flags & ICE_TX_FLAGS_TSO)
  1965. return;
  1966. /* Grab an open timestamp slot */
  1967. idx = ice_ptp_request_ts(tx_ring->tx_tstamps, skb);
  1968. if (idx < 0) {
  1969. tx_ring->vsi->back->ptp.tx_hwtstamp_skipped++;
  1970. return;
  1971. }
  1972. off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
  1973. (ICE_TX_CTX_DESC_TSYN << ICE_TXD_CTX_QW1_CMD_S) |
  1974. ((u64)idx << ICE_TXD_CTX_QW1_TSO_LEN_S));
  1975. first->tx_flags |= ICE_TX_FLAGS_TSYN;
  1976. }
  1977. /**
  1978. * ice_xmit_frame_ring - Sends buffer on Tx ring
  1979. * @skb: send buffer
  1980. * @tx_ring: ring to send buffer on
  1981. *
  1982. * Returns NETDEV_TX_OK if sent, else an error code
  1983. */
  1984. static netdev_tx_t
  1985. ice_xmit_frame_ring(struct sk_buff *skb, struct ice_tx_ring *tx_ring)
  1986. {
  1987. struct ice_tx_offload_params offload = { 0 };
  1988. struct ice_vsi *vsi = tx_ring->vsi;
  1989. struct ice_tx_buf *first;
  1990. struct ethhdr *eth;
  1991. unsigned int count;
  1992. int tso, csum;
  1993. ice_trace(xmit_frame_ring, tx_ring, skb);
  1994. if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
  1995. goto out_drop;
  1996. count = ice_xmit_desc_count(skb);
  1997. if (ice_chk_linearize(skb, count)) {
  1998. if (__skb_linearize(skb))
  1999. goto out_drop;
  2000. count = ice_txd_use_count(skb->len);
  2001. tx_ring->ring_stats->tx_stats.tx_linearize++;
  2002. }
  2003. /* need: 1 descriptor per page * PAGE_SIZE/ICE_MAX_DATA_PER_TXD,
  2004. * + 1 desc for skb_head_len/ICE_MAX_DATA_PER_TXD,
  2005. * + 4 desc gap to avoid the cache line where head is,
  2006. * + 1 desc for context descriptor,
  2007. * otherwise try next time
  2008. */
  2009. if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE +
  2010. ICE_DESCS_FOR_CTX_DESC)) {
  2011. tx_ring->ring_stats->tx_stats.tx_busy++;
  2012. return NETDEV_TX_BUSY;
  2013. }
  2014. /* prefetch for bql data which is infrequently used */
  2015. netdev_txq_bql_enqueue_prefetchw(txring_txq(tx_ring));
  2016. offload.tx_ring = tx_ring;
  2017. /* record the location of the first descriptor for this packet */
  2018. first = &tx_ring->tx_buf[tx_ring->next_to_use];
  2019. first->skb = skb;
  2020. first->type = ICE_TX_BUF_SKB;
  2021. first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
  2022. first->gso_segs = 1;
  2023. first->tx_flags = 0;
  2024. /* prepare the VLAN tagging flags for Tx */
  2025. ice_tx_prepare_vlan_flags(tx_ring, first);
  2026. if (first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) {
  2027. offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
  2028. (ICE_TX_CTX_DESC_IL2TAG2 <<
  2029. ICE_TXD_CTX_QW1_CMD_S));
  2030. offload.cd_l2tag2 = first->vid;
  2031. }
  2032. /* set up TSO offload */
  2033. tso = ice_tso(first, &offload);
  2034. if (tso < 0)
  2035. goto out_drop;
  2036. /* always set up Tx checksum offload */
  2037. csum = ice_tx_csum(first, &offload);
  2038. if (csum < 0)
  2039. goto out_drop;
  2040. /* allow CONTROL frames egress from main VSI if FW LLDP disabled */
  2041. eth = (struct ethhdr *)skb_mac_header(skb);
  2042. if (unlikely((skb->priority == TC_PRIO_CONTROL ||
  2043. eth->h_proto == htons(ETH_P_LLDP)) &&
  2044. vsi->type == ICE_VSI_PF &&
  2045. vsi->port_info->qos_cfg.is_sw_lldp))
  2046. offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX |
  2047. ICE_TX_CTX_DESC_SWTCH_UPLINK <<
  2048. ICE_TXD_CTX_QW1_CMD_S);
  2049. ice_tstamp(tx_ring, skb, first, &offload);
  2050. if ((ice_is_switchdev_running(vsi->back) ||
  2051. ice_lag_is_switchdev_running(vsi->back)) &&
  2052. vsi->type != ICE_VSI_SF)
  2053. ice_eswitch_set_target_vsi(skb, &offload);
  2054. if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) {
  2055. struct ice_tx_ctx_desc *cdesc;
  2056. u16 i = tx_ring->next_to_use;
  2057. /* grab the next descriptor */
  2058. cdesc = ICE_TX_CTX_DESC(tx_ring, i);
  2059. i++;
  2060. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2061. /* setup context descriptor */
  2062. cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params);
  2063. cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2);
  2064. cdesc->rsvd = cpu_to_le16(0);
  2065. cdesc->qw1 = cpu_to_le64(offload.cd_qw1);
  2066. }
  2067. ice_tx_map(tx_ring, first, &offload);
  2068. return NETDEV_TX_OK;
  2069. out_drop:
  2070. ice_trace(xmit_frame_ring_drop, tx_ring, skb);
  2071. dev_kfree_skb_any(skb);
  2072. return NETDEV_TX_OK;
  2073. }
  2074. /**
  2075. * ice_start_xmit - Selects the correct VSI and Tx queue to send buffer
  2076. * @skb: send buffer
  2077. * @netdev: network interface device structure
  2078. *
  2079. * Returns NETDEV_TX_OK if sent, else an error code
  2080. */
  2081. netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  2082. {
  2083. struct ice_netdev_priv *np = netdev_priv(netdev);
  2084. struct ice_vsi *vsi = np->vsi;
  2085. struct ice_tx_ring *tx_ring;
  2086. tx_ring = vsi->tx_rings[skb->queue_mapping];
  2087. /* hardware can't handle really short frames, hardware padding works
  2088. * beyond this point
  2089. */
  2090. if (skb_put_padto(skb, ICE_MIN_TX_LEN))
  2091. return NETDEV_TX_OK;
  2092. return ice_xmit_frame_ring(skb, tx_ring);
  2093. }
  2094. /**
  2095. * ice_get_dscp_up - return the UP/TC value for a SKB
  2096. * @dcbcfg: DCB config that contains DSCP to UP/TC mapping
  2097. * @skb: SKB to query for info to determine UP/TC
  2098. *
  2099. * This function is to only be called when the PF is in L3 DSCP PFC mode
  2100. */
  2101. static u8 ice_get_dscp_up(struct ice_dcbx_cfg *dcbcfg, struct sk_buff *skb)
  2102. {
  2103. u8 dscp = 0;
  2104. if (skb->protocol == htons(ETH_P_IP))
  2105. dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
  2106. else if (skb->protocol == htons(ETH_P_IPV6))
  2107. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
  2108. return dcbcfg->dscp_map[dscp];
  2109. }
  2110. u16
  2111. ice_select_queue(struct net_device *netdev, struct sk_buff *skb,
  2112. struct net_device *sb_dev)
  2113. {
  2114. struct ice_pf *pf = ice_netdev_to_pf(netdev);
  2115. struct ice_dcbx_cfg *dcbcfg;
  2116. dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
  2117. if (dcbcfg->pfc_mode == ICE_QOS_MODE_DSCP)
  2118. skb->priority = ice_get_dscp_up(dcbcfg, skb);
  2119. return netdev_pick_tx(netdev, skb, sb_dev);
  2120. }
  2121. /**
  2122. * ice_clean_ctrl_tx_irq - interrupt handler for flow director Tx queue
  2123. * @tx_ring: tx_ring to clean
  2124. */
  2125. void ice_clean_ctrl_tx_irq(struct ice_tx_ring *tx_ring)
  2126. {
  2127. struct ice_vsi *vsi = tx_ring->vsi;
  2128. s16 i = tx_ring->next_to_clean;
  2129. int budget = ICE_DFLT_IRQ_WORK;
  2130. struct ice_tx_desc *tx_desc;
  2131. struct ice_tx_buf *tx_buf;
  2132. tx_buf = &tx_ring->tx_buf[i];
  2133. tx_desc = ICE_TX_DESC(tx_ring, i);
  2134. i -= tx_ring->count;
  2135. do {
  2136. struct ice_tx_desc *eop_desc = tx_buf->next_to_watch;
  2137. /* if next_to_watch is not set then there is no pending work */
  2138. if (!eop_desc)
  2139. break;
  2140. /* prevent any other reads prior to eop_desc */
  2141. smp_rmb();
  2142. /* if the descriptor isn't done, no work to do */
  2143. if (!(eop_desc->cmd_type_offset_bsz &
  2144. cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
  2145. break;
  2146. /* clear next_to_watch to prevent false hangs */
  2147. tx_buf->next_to_watch = NULL;
  2148. tx_desc->buf_addr = 0;
  2149. tx_desc->cmd_type_offset_bsz = 0;
  2150. /* move past filter desc */
  2151. tx_buf++;
  2152. tx_desc++;
  2153. i++;
  2154. if (unlikely(!i)) {
  2155. i -= tx_ring->count;
  2156. tx_buf = tx_ring->tx_buf;
  2157. tx_desc = ICE_TX_DESC(tx_ring, 0);
  2158. }
  2159. /* unmap the data header */
  2160. if (dma_unmap_len(tx_buf, len))
  2161. dma_unmap_single(tx_ring->dev,
  2162. dma_unmap_addr(tx_buf, dma),
  2163. dma_unmap_len(tx_buf, len),
  2164. DMA_TO_DEVICE);
  2165. if (tx_buf->type == ICE_TX_BUF_DUMMY)
  2166. devm_kfree(tx_ring->dev, tx_buf->raw_buf);
  2167. /* clear next_to_watch to prevent false hangs */
  2168. tx_buf->type = ICE_TX_BUF_EMPTY;
  2169. tx_buf->tx_flags = 0;
  2170. tx_buf->next_to_watch = NULL;
  2171. dma_unmap_len_set(tx_buf, len, 0);
  2172. tx_desc->buf_addr = 0;
  2173. tx_desc->cmd_type_offset_bsz = 0;
  2174. /* move past eop_desc for start of next FD desc */
  2175. tx_buf++;
  2176. tx_desc++;
  2177. i++;
  2178. if (unlikely(!i)) {
  2179. i -= tx_ring->count;
  2180. tx_buf = tx_ring->tx_buf;
  2181. tx_desc = ICE_TX_DESC(tx_ring, 0);
  2182. }
  2183. budget--;
  2184. } while (likely(budget));
  2185. i += tx_ring->count;
  2186. tx_ring->next_to_clean = i;
  2187. /* re-enable interrupt if needed */
  2188. ice_irq_dynamic_ena(&vsi->back->hw, vsi, vsi->q_vectors[0]);
  2189. }