ionic_txrx.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/ip.h>
  4. #include <linux/ipv6.h>
  5. #include <linux/if_vlan.h>
  6. #include <net/ip6_checksum.h>
  7. #include <net/netdev_queues.h>
  8. #include <net/page_pool/helpers.h>
  9. #include "ionic.h"
  10. #include "ionic_lif.h"
  11. #include "ionic_txrx.h"
  12. static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
  13. void *data, size_t len);
  14. static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
  15. const skb_frag_t *frag,
  16. size_t offset, size_t len);
  17. static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
  18. struct ionic_tx_desc_info *desc_info);
  19. static void ionic_tx_clean(struct ionic_queue *q,
  20. struct ionic_tx_desc_info *desc_info,
  21. struct ionic_txq_comp *comp,
  22. bool in_napi);
  23. static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell)
  24. {
  25. ionic_q_post(q, ring_dbell);
  26. }
  27. static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell)
  28. {
  29. ionic_q_post(q, ring_dbell);
  30. }
  31. bool ionic_txq_poke_doorbell(struct ionic_queue *q)
  32. {
  33. struct netdev_queue *netdev_txq;
  34. unsigned long now, then, dif;
  35. struct net_device *netdev;
  36. netdev = q->lif->netdev;
  37. netdev_txq = netdev_get_tx_queue(netdev, q->index);
  38. HARD_TX_LOCK(netdev, netdev_txq, smp_processor_id());
  39. if (q->tail_idx == q->head_idx) {
  40. HARD_TX_UNLOCK(netdev, netdev_txq);
  41. return false;
  42. }
  43. now = READ_ONCE(jiffies);
  44. then = q->dbell_jiffies;
  45. dif = now - then;
  46. if (dif > q->dbell_deadline) {
  47. ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
  48. q->dbval | q->head_idx);
  49. q->dbell_jiffies = now;
  50. }
  51. HARD_TX_UNLOCK(netdev, netdev_txq);
  52. return true;
  53. }
  54. bool ionic_rxq_poke_doorbell(struct ionic_queue *q)
  55. {
  56. unsigned long now, then, dif;
  57. /* no lock, called from rx napi or txrx napi, nothing else can fill */
  58. if (q->tail_idx == q->head_idx)
  59. return false;
  60. now = READ_ONCE(jiffies);
  61. then = q->dbell_jiffies;
  62. dif = now - then;
  63. if (dif > q->dbell_deadline) {
  64. ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
  65. q->dbval | q->head_idx);
  66. q->dbell_jiffies = now;
  67. dif = 2 * q->dbell_deadline;
  68. if (dif > IONIC_RX_MAX_DOORBELL_DEADLINE)
  69. dif = IONIC_RX_MAX_DOORBELL_DEADLINE;
  70. q->dbell_deadline = dif;
  71. }
  72. return true;
  73. }
  74. static inline struct ionic_txq_sg_elem *ionic_tx_sg_elems(struct ionic_queue *q)
  75. {
  76. if (likely(q->sg_desc_size == sizeof(struct ionic_txq_sg_desc_v1)))
  77. return q->txq_sgl_v1[q->head_idx].elems;
  78. else
  79. return q->txq_sgl[q->head_idx].elems;
  80. }
  81. static inline struct netdev_queue *q_to_ndq(struct net_device *netdev,
  82. struct ionic_queue *q)
  83. {
  84. return netdev_get_tx_queue(netdev, q->index);
  85. }
  86. static void *ionic_rx_buf_va(struct ionic_buf_info *buf_info)
  87. {
  88. return page_address(buf_info->page) + buf_info->page_offset;
  89. }
  90. static dma_addr_t ionic_rx_buf_pa(struct ionic_buf_info *buf_info)
  91. {
  92. return page_pool_get_dma_addr(buf_info->page) + buf_info->page_offset;
  93. }
  94. static void __ionic_rx_put_buf(struct ionic_queue *q,
  95. struct ionic_buf_info *buf_info,
  96. bool recycle_direct)
  97. {
  98. if (!buf_info->page)
  99. return;
  100. page_pool_put_full_page(q->page_pool, buf_info->page, recycle_direct);
  101. buf_info->page = NULL;
  102. buf_info->len = 0;
  103. buf_info->page_offset = 0;
  104. }
  105. static void ionic_rx_put_buf(struct ionic_queue *q,
  106. struct ionic_buf_info *buf_info)
  107. {
  108. __ionic_rx_put_buf(q, buf_info, false);
  109. }
  110. static void ionic_rx_put_buf_direct(struct ionic_queue *q,
  111. struct ionic_buf_info *buf_info)
  112. {
  113. __ionic_rx_put_buf(q, buf_info, true);
  114. }
  115. static void ionic_rx_add_skb_frag(struct ionic_queue *q,
  116. struct sk_buff *skb,
  117. struct ionic_buf_info *buf_info,
  118. u32 headroom, u32 len,
  119. bool synced)
  120. {
  121. if (!synced)
  122. page_pool_dma_sync_for_cpu(q->page_pool,
  123. buf_info->page,
  124. buf_info->page_offset + headroom,
  125. len);
  126. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  127. buf_info->page, buf_info->page_offset + headroom,
  128. len, buf_info->len);
  129. /* napi_gro_frags() will release/recycle the
  130. * page_pool buffers from the frags list
  131. */
  132. buf_info->page = NULL;
  133. buf_info->len = 0;
  134. buf_info->page_offset = 0;
  135. }
  136. static struct sk_buff *ionic_rx_build_skb(struct ionic_queue *q,
  137. struct ionic_rx_desc_info *desc_info,
  138. unsigned int headroom,
  139. unsigned int len,
  140. unsigned int num_sg_elems,
  141. bool synced)
  142. {
  143. struct ionic_buf_info *buf_info;
  144. struct sk_buff *skb;
  145. unsigned int i;
  146. u16 frag_len;
  147. buf_info = &desc_info->bufs[0];
  148. prefetchw(buf_info->page);
  149. skb = napi_get_frags(&q_to_qcq(q)->napi);
  150. if (unlikely(!skb)) {
  151. net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
  152. dev_name(q->dev), q->name);
  153. q_to_rx_stats(q)->alloc_err++;
  154. return NULL;
  155. }
  156. skb_mark_for_recycle(skb);
  157. if (headroom)
  158. frag_len = min_t(u16, len,
  159. IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
  160. else
  161. frag_len = min_t(u16, len, IONIC_PAGE_SIZE);
  162. if (unlikely(!buf_info->page))
  163. goto err_bad_buf_page;
  164. ionic_rx_add_skb_frag(q, skb, buf_info, headroom, frag_len, synced);
  165. len -= frag_len;
  166. buf_info++;
  167. for (i = 0; i < num_sg_elems; i++, buf_info++) {
  168. if (unlikely(!buf_info->page))
  169. goto err_bad_buf_page;
  170. frag_len = min_t(u16, len, buf_info->len);
  171. ionic_rx_add_skb_frag(q, skb, buf_info, 0, frag_len, synced);
  172. len -= frag_len;
  173. }
  174. return skb;
  175. err_bad_buf_page:
  176. dev_kfree_skb(skb);
  177. return NULL;
  178. }
  179. static struct sk_buff *ionic_rx_copybreak(struct net_device *netdev,
  180. struct ionic_queue *q,
  181. struct ionic_rx_desc_info *desc_info,
  182. unsigned int headroom,
  183. unsigned int len,
  184. unsigned int num_sg_elems,
  185. bool synced)
  186. {
  187. struct ionic_buf_info *buf_info;
  188. struct device *dev = q->dev;
  189. struct sk_buff *skb;
  190. int i;
  191. buf_info = &desc_info->bufs[0];
  192. skb = napi_alloc_skb(&q_to_qcq(q)->napi, len);
  193. if (unlikely(!skb)) {
  194. net_warn_ratelimited("%s: SKB alloc failed on %s!\n",
  195. dev_name(dev), q->name);
  196. q_to_rx_stats(q)->alloc_err++;
  197. return NULL;
  198. }
  199. skb_mark_for_recycle(skb);
  200. if (!synced)
  201. page_pool_dma_sync_for_cpu(q->page_pool,
  202. buf_info->page,
  203. buf_info->page_offset + headroom,
  204. len);
  205. skb_copy_to_linear_data(skb, ionic_rx_buf_va(buf_info) + headroom, len);
  206. skb_put(skb, len);
  207. skb->protocol = eth_type_trans(skb, netdev);
  208. /* recycle the Rx buffer now that we're done with it */
  209. ionic_rx_put_buf_direct(q, buf_info);
  210. buf_info++;
  211. for (i = 0; i < num_sg_elems; i++, buf_info++)
  212. ionic_rx_put_buf_direct(q, buf_info);
  213. return skb;
  214. }
  215. static void ionic_xdp_tx_desc_clean(struct ionic_queue *q,
  216. struct ionic_tx_desc_info *desc_info,
  217. bool in_napi)
  218. {
  219. struct xdp_frame_bulk bq;
  220. if (!desc_info->nbufs)
  221. return;
  222. xdp_frame_bulk_init(&bq);
  223. rcu_read_lock(); /* need for xdp_return_frame_bulk */
  224. if (desc_info->act == XDP_TX) {
  225. if (likely(in_napi))
  226. xdp_return_frame_rx_napi(desc_info->xdpf);
  227. else
  228. xdp_return_frame(desc_info->xdpf);
  229. } else if (desc_info->act == XDP_REDIRECT) {
  230. ionic_tx_desc_unmap_bufs(q, desc_info);
  231. xdp_return_frame_bulk(desc_info->xdpf, &bq);
  232. }
  233. xdp_flush_frame_bulk(&bq);
  234. rcu_read_unlock();
  235. desc_info->nbufs = 0;
  236. desc_info->xdpf = NULL;
  237. desc_info->act = 0;
  238. }
  239. static int ionic_xdp_post_frame(struct ionic_queue *q, struct xdp_frame *frame,
  240. enum xdp_action act, struct page *page, int off,
  241. bool ring_doorbell)
  242. {
  243. struct ionic_tx_desc_info *desc_info;
  244. struct ionic_buf_info *buf_info;
  245. struct ionic_tx_stats *stats;
  246. struct ionic_txq_desc *desc;
  247. size_t len = frame->len;
  248. dma_addr_t dma_addr;
  249. u64 cmd;
  250. desc_info = &q->tx_info[q->head_idx];
  251. desc = &q->txq[q->head_idx];
  252. buf_info = desc_info->bufs;
  253. stats = q_to_tx_stats(q);
  254. if (act == XDP_TX) {
  255. dma_addr = page_pool_get_dma_addr(page) +
  256. off + XDP_PACKET_HEADROOM;
  257. dma_sync_single_for_device(q->dev, dma_addr,
  258. len, DMA_TO_DEVICE);
  259. } else /* XDP_REDIRECT */ {
  260. dma_addr = ionic_tx_map_single(q, frame->data, len);
  261. if (dma_addr == DMA_MAPPING_ERROR)
  262. return -EIO;
  263. }
  264. buf_info->dma_addr = dma_addr;
  265. buf_info->len = len;
  266. buf_info->page = page;
  267. buf_info->page_offset = off;
  268. desc_info->nbufs = 1;
  269. desc_info->xdpf = frame;
  270. desc_info->act = act;
  271. if (xdp_frame_has_frags(frame)) {
  272. struct ionic_txq_sg_elem *elem;
  273. struct skb_shared_info *sinfo;
  274. struct ionic_buf_info *bi;
  275. skb_frag_t *frag;
  276. int i;
  277. bi = &buf_info[1];
  278. sinfo = xdp_get_shared_info_from_frame(frame);
  279. frag = sinfo->frags;
  280. elem = ionic_tx_sg_elems(q);
  281. for (i = 0; i < sinfo->nr_frags; i++, frag++, bi++) {
  282. if (act == XDP_TX) {
  283. struct page *pg = skb_frag_page(frag);
  284. dma_addr = page_pool_get_dma_addr(pg) +
  285. skb_frag_off(frag);
  286. dma_sync_single_for_device(q->dev, dma_addr,
  287. skb_frag_size(frag),
  288. DMA_TO_DEVICE);
  289. } else {
  290. dma_addr = ionic_tx_map_frag(q, frag, 0,
  291. skb_frag_size(frag));
  292. if (dma_addr == DMA_MAPPING_ERROR) {
  293. ionic_tx_desc_unmap_bufs(q, desc_info);
  294. return -EIO;
  295. }
  296. }
  297. bi->dma_addr = dma_addr;
  298. bi->len = skb_frag_size(frag);
  299. bi->page = skb_frag_page(frag);
  300. elem->addr = cpu_to_le64(bi->dma_addr);
  301. elem->len = cpu_to_le16(bi->len);
  302. elem++;
  303. desc_info->nbufs++;
  304. }
  305. }
  306. cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
  307. 0, (desc_info->nbufs - 1), buf_info->dma_addr);
  308. desc->cmd = cpu_to_le64(cmd);
  309. desc->len = cpu_to_le16(len);
  310. desc->csum_start = 0;
  311. desc->csum_offset = 0;
  312. stats->xdp_frames++;
  313. stats->pkts++;
  314. stats->bytes += len;
  315. ionic_txq_post(q, ring_doorbell);
  316. return 0;
  317. }
  318. int ionic_xdp_xmit(struct net_device *netdev, int n,
  319. struct xdp_frame **xdp_frames, u32 flags)
  320. {
  321. struct ionic_lif *lif = netdev_priv(netdev);
  322. struct ionic_queue *txq;
  323. struct netdev_queue *nq;
  324. int nxmit;
  325. int space;
  326. int cpu;
  327. int qi;
  328. if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state)))
  329. return -ENETDOWN;
  330. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  331. return -EINVAL;
  332. /* AdminQ is assumed on cpu 0, while we attempt to affinitize the
  333. * TxRx queue pairs 0..n-1 on cpus 1..n. We try to keep with that
  334. * affinitization here, but of course irqbalance and friends might
  335. * have juggled things anyway, so we have to check for the 0 case.
  336. */
  337. cpu = smp_processor_id();
  338. qi = cpu ? (cpu - 1) % lif->nxqs : cpu;
  339. txq = &lif->txqcqs[qi]->q;
  340. nq = netdev_get_tx_queue(netdev, txq->index);
  341. __netif_tx_lock(nq, cpu);
  342. txq_trans_cond_update(nq);
  343. if (netif_tx_queue_stopped(nq) ||
  344. !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
  345. ionic_q_space_avail(txq),
  346. 1, 1)) {
  347. __netif_tx_unlock(nq);
  348. return -EIO;
  349. }
  350. space = min_t(int, n, ionic_q_space_avail(txq));
  351. for (nxmit = 0; nxmit < space ; nxmit++) {
  352. if (ionic_xdp_post_frame(txq, xdp_frames[nxmit],
  353. XDP_REDIRECT,
  354. virt_to_page(xdp_frames[nxmit]->data),
  355. 0, false)) {
  356. nxmit--;
  357. break;
  358. }
  359. }
  360. if (flags & XDP_XMIT_FLUSH)
  361. ionic_dbell_ring(lif->kern_dbpage, txq->hw_type,
  362. txq->dbval | txq->head_idx);
  363. netif_txq_maybe_stop(q_to_ndq(netdev, txq),
  364. ionic_q_space_avail(txq),
  365. 4, 4);
  366. __netif_tx_unlock(nq);
  367. return nxmit;
  368. }
  369. static void ionic_xdp_rx_unlink_bufs(struct ionic_queue *q,
  370. struct ionic_buf_info *buf_info,
  371. int nbufs)
  372. {
  373. int i;
  374. for (i = 0; i < nbufs; i++) {
  375. buf_info->page = NULL;
  376. buf_info++;
  377. }
  378. }
  379. static bool ionic_run_xdp(struct ionic_rx_stats *stats,
  380. struct net_device *netdev,
  381. struct bpf_prog *xdp_prog,
  382. struct ionic_queue *rxq,
  383. struct ionic_buf_info *buf_info,
  384. int len)
  385. {
  386. u32 xdp_action = XDP_ABORTED;
  387. struct xdp_buff xdp_buf;
  388. struct ionic_queue *txq;
  389. struct netdev_queue *nq;
  390. struct xdp_frame *xdpf;
  391. int remain_len;
  392. int nbufs = 1;
  393. int frag_len;
  394. int err = 0;
  395. xdp_init_buff(&xdp_buf, IONIC_PAGE_SIZE, rxq->xdp_rxq_info);
  396. frag_len = min_t(u16, len, IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN);
  397. xdp_prepare_buff(&xdp_buf, ionic_rx_buf_va(buf_info),
  398. XDP_PACKET_HEADROOM, frag_len, false);
  399. page_pool_dma_sync_for_cpu(rxq->page_pool, buf_info->page,
  400. buf_info->page_offset + XDP_PACKET_HEADROOM,
  401. frag_len);
  402. prefetchw(&xdp_buf.data_hard_start);
  403. /* We limit MTU size to one buffer if !xdp_has_frags, so
  404. * if the recv len is bigger than one buffer
  405. * then we know we have frag info to gather
  406. */
  407. remain_len = len - frag_len;
  408. if (remain_len) {
  409. struct skb_shared_info *sinfo;
  410. struct ionic_buf_info *bi;
  411. skb_frag_t *frag;
  412. bi = buf_info;
  413. sinfo = xdp_get_shared_info_from_buff(&xdp_buf);
  414. sinfo->nr_frags = 0;
  415. sinfo->xdp_frags_size = 0;
  416. xdp_buff_set_frags_flag(&xdp_buf);
  417. do {
  418. if (unlikely(sinfo->nr_frags >= MAX_SKB_FRAGS)) {
  419. err = -ENOSPC;
  420. break;
  421. }
  422. frag = &sinfo->frags[sinfo->nr_frags];
  423. sinfo->nr_frags++;
  424. bi++;
  425. frag_len = min_t(u16, remain_len, bi->len);
  426. page_pool_dma_sync_for_cpu(rxq->page_pool, bi->page,
  427. buf_info->page_offset,
  428. frag_len);
  429. skb_frag_fill_page_desc(frag, bi->page, 0, frag_len);
  430. sinfo->xdp_frags_size += frag_len;
  431. remain_len -= frag_len;
  432. if (page_is_pfmemalloc(bi->page))
  433. xdp_buff_set_frag_pfmemalloc(&xdp_buf);
  434. } while (remain_len > 0);
  435. nbufs += sinfo->nr_frags;
  436. }
  437. xdp_action = bpf_prog_run_xdp(xdp_prog, &xdp_buf);
  438. switch (xdp_action) {
  439. case XDP_PASS:
  440. stats->xdp_pass++;
  441. return false; /* false = we didn't consume the packet */
  442. case XDP_DROP:
  443. ionic_rx_put_buf_direct(rxq, buf_info);
  444. stats->xdp_drop++;
  445. break;
  446. case XDP_TX:
  447. xdpf = xdp_convert_buff_to_frame(&xdp_buf);
  448. if (!xdpf) {
  449. err = -ENOSPC;
  450. break;
  451. }
  452. txq = rxq->partner;
  453. nq = netdev_get_tx_queue(netdev, txq->index);
  454. __netif_tx_lock(nq, smp_processor_id());
  455. txq_trans_cond_update(nq);
  456. if (netif_tx_queue_stopped(nq) ||
  457. !netif_txq_maybe_stop(q_to_ndq(netdev, txq),
  458. ionic_q_space_avail(txq),
  459. 1, 1)) {
  460. __netif_tx_unlock(nq);
  461. err = -EIO;
  462. break;
  463. }
  464. err = ionic_xdp_post_frame(txq, xdpf, XDP_TX,
  465. buf_info->page,
  466. buf_info->page_offset,
  467. true);
  468. __netif_tx_unlock(nq);
  469. if (unlikely(err)) {
  470. netdev_dbg(netdev, "tx ionic_xdp_post_frame err %d\n", err);
  471. break;
  472. }
  473. ionic_xdp_rx_unlink_bufs(rxq, buf_info, nbufs);
  474. stats->xdp_tx++;
  475. break;
  476. case XDP_REDIRECT:
  477. err = xdp_do_redirect(netdev, &xdp_buf, xdp_prog);
  478. if (unlikely(err)) {
  479. netdev_dbg(netdev, "xdp_do_redirect err %d\n", err);
  480. break;
  481. }
  482. ionic_xdp_rx_unlink_bufs(rxq, buf_info, nbufs);
  483. rxq->xdp_flush = true;
  484. stats->xdp_redirect++;
  485. break;
  486. case XDP_ABORTED:
  487. default:
  488. err = -EIO;
  489. break;
  490. }
  491. if (err) {
  492. ionic_rx_put_buf_direct(rxq, buf_info);
  493. trace_xdp_exception(netdev, xdp_prog, xdp_action);
  494. stats->xdp_aborted++;
  495. }
  496. return true;
  497. }
  498. static void ionic_rx_clean(struct ionic_queue *q,
  499. struct ionic_rx_desc_info *desc_info,
  500. struct ionic_rxq_comp *comp,
  501. struct bpf_prog *xdp_prog)
  502. {
  503. struct net_device *netdev = q->lif->netdev;
  504. struct ionic_qcq *qcq = q_to_qcq(q);
  505. struct ionic_rx_stats *stats;
  506. unsigned int headroom = 0;
  507. struct sk_buff *skb;
  508. bool synced = false;
  509. bool use_copybreak;
  510. u16 len;
  511. stats = q_to_rx_stats(q);
  512. if (unlikely(comp->status)) {
  513. /* Most likely status==2 and the pkt received was bigger
  514. * than the buffer available: comp->len will show the
  515. * pkt size received that didn't fit the advertised desc.len
  516. */
  517. dev_dbg(q->dev, "q%d drop comp->status %d comp->len %d desc->len %d\n",
  518. q->index, comp->status, comp->len, q->rxq[q->head_idx].len);
  519. stats->dropped++;
  520. return;
  521. }
  522. len = le16_to_cpu(comp->len);
  523. stats->pkts++;
  524. stats->bytes += len;
  525. if (xdp_prog) {
  526. if (ionic_run_xdp(stats, netdev, xdp_prog, q, desc_info->bufs, len))
  527. return;
  528. synced = true;
  529. headroom = XDP_PACKET_HEADROOM;
  530. }
  531. use_copybreak = len <= q->lif->rx_copybreak;
  532. if (use_copybreak)
  533. skb = ionic_rx_copybreak(netdev, q, desc_info,
  534. headroom, len,
  535. comp->num_sg_elems, synced);
  536. else
  537. skb = ionic_rx_build_skb(q, desc_info, headroom, len,
  538. comp->num_sg_elems, synced);
  539. if (unlikely(!skb)) {
  540. stats->dropped++;
  541. return;
  542. }
  543. skb_record_rx_queue(skb, q->index);
  544. if (likely(netdev->features & NETIF_F_RXHASH)) {
  545. switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) {
  546. case IONIC_PKT_TYPE_IPV4:
  547. case IONIC_PKT_TYPE_IPV6:
  548. skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
  549. PKT_HASH_TYPE_L3);
  550. break;
  551. case IONIC_PKT_TYPE_IPV4_TCP:
  552. case IONIC_PKT_TYPE_IPV6_TCP:
  553. case IONIC_PKT_TYPE_IPV4_UDP:
  554. case IONIC_PKT_TYPE_IPV6_UDP:
  555. skb_set_hash(skb, le32_to_cpu(comp->rss_hash),
  556. PKT_HASH_TYPE_L4);
  557. break;
  558. }
  559. }
  560. if (likely(netdev->features & NETIF_F_RXCSUM) &&
  561. (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC)) {
  562. skb->ip_summed = CHECKSUM_COMPLETE;
  563. skb->csum = (__force __wsum)le16_to_cpu(comp->csum);
  564. stats->csum_complete++;
  565. } else {
  566. stats->csum_none++;
  567. }
  568. if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) ||
  569. (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) ||
  570. (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD)))
  571. stats->csum_error++;
  572. if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  573. (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) {
  574. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  575. le16_to_cpu(comp->vlan_tci));
  576. stats->vlan_stripped++;
  577. }
  578. if (unlikely(q->features & IONIC_RXQ_F_HWSTAMP)) {
  579. __le64 *cq_desc_hwstamp;
  580. u64 hwstamp;
  581. cq_desc_hwstamp =
  582. (void *)comp +
  583. qcq->cq.desc_size -
  584. sizeof(struct ionic_rxq_comp) -
  585. IONIC_HWSTAMP_CQ_NEGOFFSET;
  586. hwstamp = le64_to_cpu(*cq_desc_hwstamp);
  587. if (hwstamp != IONIC_HWSTAMP_INVALID) {
  588. skb_hwtstamps(skb)->hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
  589. stats->hwstamp_valid++;
  590. } else {
  591. stats->hwstamp_invalid++;
  592. }
  593. }
  594. if (use_copybreak)
  595. napi_gro_receive(&qcq->napi, skb);
  596. else
  597. napi_gro_frags(&qcq->napi);
  598. }
  599. static bool __ionic_rx_service(struct ionic_cq *cq, struct bpf_prog *xdp_prog)
  600. {
  601. struct ionic_rx_desc_info *desc_info;
  602. struct ionic_queue *q = cq->bound_q;
  603. struct ionic_rxq_comp *comp;
  604. comp = &((struct ionic_rxq_comp *)cq->base)[cq->tail_idx];
  605. if (!color_match(comp->pkt_type_color, cq->done_color))
  606. return false;
  607. /* check for empty queue */
  608. if (q->tail_idx == q->head_idx)
  609. return false;
  610. if (q->tail_idx != le16_to_cpu(comp->comp_index))
  611. return false;
  612. desc_info = &q->rx_info[q->tail_idx];
  613. q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
  614. /* clean the related q entry, only one per qc completion */
  615. ionic_rx_clean(q, desc_info, comp, xdp_prog);
  616. return true;
  617. }
  618. bool ionic_rx_service(struct ionic_cq *cq)
  619. {
  620. return __ionic_rx_service(cq, NULL);
  621. }
  622. static inline void ionic_write_cmb_desc(struct ionic_queue *q,
  623. void *desc)
  624. {
  625. /* Since Rx and Tx descriptors are the same size, we can
  626. * save an instruction or two and skip the qtype check.
  627. */
  628. if (unlikely(q_to_qcq(q)->flags & IONIC_QCQ_F_CMB_RINGS))
  629. memcpy_toio(&q->cmb_txq[q->head_idx], desc, sizeof(q->cmb_txq[0]));
  630. }
  631. void ionic_rx_fill(struct ionic_queue *q, struct bpf_prog *xdp_prog)
  632. {
  633. struct net_device *netdev = q->lif->netdev;
  634. struct ionic_rx_desc_info *desc_info;
  635. struct ionic_rxq_sg_elem *sg_elem;
  636. struct ionic_buf_info *buf_info;
  637. unsigned int fill_threshold;
  638. struct ionic_rxq_desc *desc;
  639. unsigned int first_frag_len;
  640. unsigned int first_buf_len;
  641. unsigned int headroom = 0;
  642. unsigned int remain_len;
  643. unsigned int frag_len;
  644. unsigned int nfrags;
  645. unsigned int n_fill;
  646. unsigned int len;
  647. unsigned int i;
  648. unsigned int j;
  649. n_fill = ionic_q_space_avail(q);
  650. fill_threshold = min_t(unsigned int, IONIC_RX_FILL_THRESHOLD,
  651. q->num_descs / IONIC_RX_FILL_DIV);
  652. if (n_fill < fill_threshold)
  653. return;
  654. len = netdev->mtu + VLAN_ETH_HLEN;
  655. if (xdp_prog) {
  656. /* Always alloc the full size buffer, but only need
  657. * the actual frag_len in the descriptor
  658. * XDP uses space in the first buffer, so account for
  659. * head room, tail room, and ip header in the first frag size.
  660. */
  661. headroom = XDP_PACKET_HEADROOM;
  662. first_buf_len = IONIC_XDP_MAX_LINEAR_MTU + VLAN_ETH_HLEN + headroom;
  663. first_frag_len = min_t(u16, len + headroom, first_buf_len);
  664. } else {
  665. /* Use MTU size if smaller than max buffer size */
  666. first_frag_len = min_t(u16, len, IONIC_PAGE_SIZE);
  667. first_buf_len = first_frag_len;
  668. }
  669. for (i = n_fill; i; i--) {
  670. /* fill main descriptor - buf[0] */
  671. nfrags = 0;
  672. remain_len = len;
  673. desc = &q->rxq[q->head_idx];
  674. desc_info = &q->rx_info[q->head_idx];
  675. buf_info = &desc_info->bufs[0];
  676. buf_info->len = first_buf_len;
  677. frag_len = first_frag_len - headroom;
  678. /* get a new buffer if we can't reuse one */
  679. if (!buf_info->page)
  680. buf_info->page = page_pool_alloc(q->page_pool,
  681. &buf_info->page_offset,
  682. &buf_info->len,
  683. GFP_ATOMIC);
  684. if (unlikely(!buf_info->page)) {
  685. buf_info->len = 0;
  686. return;
  687. }
  688. desc->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info) + headroom);
  689. desc->len = cpu_to_le16(frag_len);
  690. remain_len -= frag_len;
  691. buf_info++;
  692. nfrags++;
  693. /* fill sg descriptors - buf[1..n] */
  694. sg_elem = q->rxq_sgl[q->head_idx].elems;
  695. for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++, sg_elem++) {
  696. frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE);
  697. /* Recycle any leftover buffers that are too small to reuse */
  698. if (unlikely(buf_info->page && buf_info->len < frag_len))
  699. ionic_rx_put_buf_direct(q, buf_info);
  700. /* Get new buffer if needed */
  701. if (!buf_info->page) {
  702. buf_info->len = frag_len;
  703. buf_info->page = page_pool_alloc(q->page_pool,
  704. &buf_info->page_offset,
  705. &buf_info->len,
  706. GFP_ATOMIC);
  707. if (unlikely(!buf_info->page)) {
  708. buf_info->len = 0;
  709. return;
  710. }
  711. }
  712. sg_elem->addr = cpu_to_le64(ionic_rx_buf_pa(buf_info));
  713. sg_elem->len = cpu_to_le16(frag_len);
  714. remain_len -= frag_len;
  715. buf_info++;
  716. nfrags++;
  717. }
  718. /* clear end sg element as a sentinel */
  719. if (j < q->max_sg_elems)
  720. memset(sg_elem, 0, sizeof(*sg_elem));
  721. desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG :
  722. IONIC_RXQ_DESC_OPCODE_SIMPLE;
  723. desc_info->nbufs = nfrags;
  724. ionic_write_cmb_desc(q, desc);
  725. ionic_rxq_post(q, false);
  726. }
  727. ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
  728. q->dbval | q->head_idx);
  729. q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
  730. q->dbell_jiffies = jiffies;
  731. }
  732. void ionic_rx_empty(struct ionic_queue *q)
  733. {
  734. struct ionic_rx_desc_info *desc_info;
  735. unsigned int i, j;
  736. for (i = 0; i < q->num_descs; i++) {
  737. desc_info = &q->rx_info[i];
  738. for (j = 0; j < ARRAY_SIZE(desc_info->bufs); j++)
  739. ionic_rx_put_buf(q, &desc_info->bufs[j]);
  740. desc_info->nbufs = 0;
  741. }
  742. q->head_idx = 0;
  743. q->tail_idx = 0;
  744. }
  745. static void ionic_dim_update(struct ionic_qcq *qcq, int napi_mode)
  746. {
  747. struct dim_sample dim_sample;
  748. struct ionic_lif *lif;
  749. unsigned int qi;
  750. u64 pkts, bytes;
  751. if (!qcq->intr.dim_coal_hw)
  752. return;
  753. lif = qcq->q.lif;
  754. qi = qcq->cq.bound_q->index;
  755. switch (napi_mode) {
  756. case IONIC_LIF_F_TX_DIM_INTR:
  757. pkts = lif->txqstats[qi].pkts;
  758. bytes = lif->txqstats[qi].bytes;
  759. break;
  760. case IONIC_LIF_F_RX_DIM_INTR:
  761. pkts = lif->rxqstats[qi].pkts;
  762. bytes = lif->rxqstats[qi].bytes;
  763. break;
  764. default:
  765. pkts = lif->txqstats[qi].pkts + lif->rxqstats[qi].pkts;
  766. bytes = lif->txqstats[qi].bytes + lif->rxqstats[qi].bytes;
  767. break;
  768. }
  769. dim_update_sample(qcq->cq.bound_intr->rearm_count,
  770. pkts, bytes, &dim_sample);
  771. net_dim(&qcq->dim, dim_sample);
  772. }
  773. int ionic_tx_napi(struct napi_struct *napi, int budget)
  774. {
  775. struct ionic_qcq *qcq = napi_to_qcq(napi);
  776. struct ionic_cq *cq = napi_to_cq(napi);
  777. u32 work_done = 0;
  778. u32 flags = 0;
  779. work_done = ionic_tx_cq_service(cq, budget, !!budget);
  780. if (unlikely(!budget))
  781. return budget;
  782. if (work_done < budget && napi_complete_done(napi, work_done)) {
  783. ionic_dim_update(qcq, IONIC_LIF_F_TX_DIM_INTR);
  784. flags |= IONIC_INTR_CRED_UNMASK;
  785. cq->bound_intr->rearm_count++;
  786. }
  787. if (work_done || flags) {
  788. flags |= IONIC_INTR_CRED_RESET_COALESCE;
  789. ionic_intr_credits(cq->idev->intr_ctrl,
  790. cq->bound_intr->index,
  791. work_done, flags);
  792. }
  793. if (!work_done && cq->bound_q->lif->doorbell_wa)
  794. ionic_txq_poke_doorbell(&qcq->q);
  795. return work_done;
  796. }
  797. static void ionic_xdp_do_flush(struct ionic_cq *cq)
  798. {
  799. if (cq->bound_q->xdp_flush) {
  800. xdp_do_flush();
  801. cq->bound_q->xdp_flush = false;
  802. }
  803. }
  804. static unsigned int ionic_rx_cq_service(struct ionic_cq *cq,
  805. unsigned int work_to_do)
  806. {
  807. struct ionic_queue *q = cq->bound_q;
  808. unsigned int work_done = 0;
  809. struct bpf_prog *xdp_prog;
  810. if (work_to_do == 0)
  811. return 0;
  812. xdp_prog = READ_ONCE(q->xdp_prog);
  813. while (__ionic_rx_service(cq, xdp_prog)) {
  814. if (cq->tail_idx == cq->num_descs - 1)
  815. cq->done_color = !cq->done_color;
  816. cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
  817. if (++work_done >= work_to_do)
  818. break;
  819. }
  820. ionic_rx_fill(q, xdp_prog);
  821. ionic_xdp_do_flush(cq);
  822. return work_done;
  823. }
  824. int ionic_rx_napi(struct napi_struct *napi, int budget)
  825. {
  826. struct ionic_qcq *qcq = napi_to_qcq(napi);
  827. struct ionic_cq *cq = napi_to_cq(napi);
  828. u32 work_done = 0;
  829. u32 flags = 0;
  830. if (unlikely(!budget))
  831. return budget;
  832. work_done = ionic_rx_cq_service(cq, budget);
  833. if (work_done < budget && napi_complete_done(napi, work_done)) {
  834. ionic_dim_update(qcq, IONIC_LIF_F_RX_DIM_INTR);
  835. flags |= IONIC_INTR_CRED_UNMASK;
  836. cq->bound_intr->rearm_count++;
  837. }
  838. if (work_done || flags) {
  839. flags |= IONIC_INTR_CRED_RESET_COALESCE;
  840. ionic_intr_credits(cq->idev->intr_ctrl,
  841. cq->bound_intr->index,
  842. work_done, flags);
  843. }
  844. if (!work_done && cq->bound_q->lif->doorbell_wa)
  845. ionic_rxq_poke_doorbell(&qcq->q);
  846. return work_done;
  847. }
  848. int ionic_txrx_napi(struct napi_struct *napi, int budget)
  849. {
  850. struct ionic_qcq *rxqcq = napi_to_qcq(napi);
  851. struct ionic_cq *rxcq = napi_to_cq(napi);
  852. unsigned int qi = rxcq->bound_q->index;
  853. struct ionic_qcq *txqcq;
  854. struct ionic_lif *lif;
  855. struct ionic_cq *txcq;
  856. u32 rx_work_done = 0;
  857. u32 tx_work_done = 0;
  858. u32 flags = 0;
  859. lif = rxcq->bound_q->lif;
  860. txqcq = lif->txqcqs[qi];
  861. txcq = &lif->txqcqs[qi]->cq;
  862. tx_work_done = ionic_tx_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT, !!budget);
  863. if (unlikely(!budget))
  864. return budget;
  865. rx_work_done = ionic_rx_cq_service(rxcq, budget);
  866. if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) {
  867. ionic_dim_update(rxqcq, 0);
  868. flags |= IONIC_INTR_CRED_UNMASK;
  869. rxcq->bound_intr->rearm_count++;
  870. }
  871. if (rx_work_done || flags) {
  872. flags |= IONIC_INTR_CRED_RESET_COALESCE;
  873. ionic_intr_credits(rxcq->idev->intr_ctrl, rxcq->bound_intr->index,
  874. tx_work_done + rx_work_done, flags);
  875. }
  876. if (lif->doorbell_wa) {
  877. if (!rx_work_done)
  878. ionic_rxq_poke_doorbell(&rxqcq->q);
  879. if (!tx_work_done)
  880. ionic_txq_poke_doorbell(&txqcq->q);
  881. }
  882. return rx_work_done;
  883. }
  884. static dma_addr_t ionic_tx_map_single(struct ionic_queue *q,
  885. void *data, size_t len)
  886. {
  887. struct device *dev = q->dev;
  888. dma_addr_t dma_addr;
  889. dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE);
  890. if (unlikely(dma_mapping_error(dev, dma_addr))) {
  891. net_warn_ratelimited("%s: DMA single map failed on %s!\n",
  892. dev_name(dev), q->name);
  893. q_to_tx_stats(q)->dma_map_err++;
  894. return DMA_MAPPING_ERROR;
  895. }
  896. return dma_addr;
  897. }
  898. static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q,
  899. const skb_frag_t *frag,
  900. size_t offset, size_t len)
  901. {
  902. struct device *dev = q->dev;
  903. dma_addr_t dma_addr;
  904. dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE);
  905. if (unlikely(dma_mapping_error(dev, dma_addr))) {
  906. net_warn_ratelimited("%s: DMA frag map failed on %s!\n",
  907. dev_name(dev), q->name);
  908. q_to_tx_stats(q)->dma_map_err++;
  909. return DMA_MAPPING_ERROR;
  910. }
  911. return dma_addr;
  912. }
  913. static int ionic_tx_map_skb(struct ionic_queue *q, struct sk_buff *skb,
  914. struct ionic_tx_desc_info *desc_info)
  915. {
  916. struct ionic_buf_info *buf_info = desc_info->bufs;
  917. struct device *dev = q->dev;
  918. dma_addr_t dma_addr;
  919. unsigned int nfrags;
  920. skb_frag_t *frag;
  921. int frag_idx;
  922. dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb));
  923. if (dma_addr == DMA_MAPPING_ERROR)
  924. return -EIO;
  925. buf_info->dma_addr = dma_addr;
  926. buf_info->len = skb_headlen(skb);
  927. buf_info++;
  928. frag = skb_shinfo(skb)->frags;
  929. nfrags = skb_shinfo(skb)->nr_frags;
  930. for (frag_idx = 0; frag_idx < nfrags; frag_idx++, frag++) {
  931. dma_addr = ionic_tx_map_frag(q, frag, 0, skb_frag_size(frag));
  932. if (dma_addr == DMA_MAPPING_ERROR)
  933. goto dma_fail;
  934. buf_info->dma_addr = dma_addr;
  935. buf_info->len = skb_frag_size(frag);
  936. buf_info++;
  937. }
  938. desc_info->nbufs = 1 + nfrags;
  939. return 0;
  940. dma_fail:
  941. /* unwind the frag mappings and the head mapping */
  942. while (frag_idx > 0) {
  943. frag_idx--;
  944. buf_info--;
  945. dma_unmap_page(dev, buf_info->dma_addr,
  946. buf_info->len, DMA_TO_DEVICE);
  947. }
  948. dma_unmap_single(dev, desc_info->bufs[0].dma_addr,
  949. desc_info->bufs[0].len, DMA_TO_DEVICE);
  950. return -EIO;
  951. }
  952. static void ionic_tx_desc_unmap_bufs(struct ionic_queue *q,
  953. struct ionic_tx_desc_info *desc_info)
  954. {
  955. struct ionic_buf_info *buf_info = desc_info->bufs;
  956. struct device *dev = q->dev;
  957. unsigned int i;
  958. if (!desc_info->nbufs)
  959. return;
  960. dma_unmap_single(dev, buf_info->dma_addr,
  961. buf_info->len, DMA_TO_DEVICE);
  962. buf_info++;
  963. for (i = 1; i < desc_info->nbufs; i++, buf_info++)
  964. dma_unmap_page(dev, buf_info->dma_addr,
  965. buf_info->len, DMA_TO_DEVICE);
  966. desc_info->nbufs = 0;
  967. }
  968. static void ionic_tx_clean(struct ionic_queue *q,
  969. struct ionic_tx_desc_info *desc_info,
  970. struct ionic_txq_comp *comp,
  971. bool in_napi)
  972. {
  973. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  974. struct ionic_qcq *qcq = q_to_qcq(q);
  975. struct sk_buff *skb;
  976. if (desc_info->xdpf) {
  977. ionic_xdp_tx_desc_clean(q->partner, desc_info, in_napi);
  978. stats->clean++;
  979. if (unlikely(__netif_subqueue_stopped(q->lif->netdev, q->index)))
  980. netif_wake_subqueue(q->lif->netdev, q->index);
  981. return;
  982. }
  983. ionic_tx_desc_unmap_bufs(q, desc_info);
  984. skb = desc_info->skb;
  985. if (!skb)
  986. return;
  987. if (unlikely(ionic_txq_hwstamp_enabled(q))) {
  988. if (comp) {
  989. struct skb_shared_hwtstamps hwts = {};
  990. __le64 *cq_desc_hwstamp;
  991. u64 hwstamp;
  992. cq_desc_hwstamp =
  993. (void *)comp +
  994. qcq->cq.desc_size -
  995. sizeof(struct ionic_txq_comp) -
  996. IONIC_HWSTAMP_CQ_NEGOFFSET;
  997. hwstamp = le64_to_cpu(*cq_desc_hwstamp);
  998. if (hwstamp != IONIC_HWSTAMP_INVALID) {
  999. hwts.hwtstamp = ionic_lif_phc_ktime(q->lif, hwstamp);
  1000. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1001. skb_tstamp_tx(skb, &hwts);
  1002. stats->hwstamp_valid++;
  1003. } else {
  1004. stats->hwstamp_invalid++;
  1005. }
  1006. }
  1007. }
  1008. desc_info->bytes = skb->len;
  1009. stats->clean++;
  1010. napi_consume_skb(skb, likely(in_napi) ? 1 : 0);
  1011. }
  1012. static bool ionic_tx_service(struct ionic_cq *cq,
  1013. unsigned int *total_pkts,
  1014. unsigned int *total_bytes,
  1015. bool in_napi)
  1016. {
  1017. struct ionic_tx_desc_info *desc_info;
  1018. struct ionic_queue *q = cq->bound_q;
  1019. struct ionic_txq_comp *comp;
  1020. unsigned int bytes = 0;
  1021. unsigned int pkts = 0;
  1022. u16 index;
  1023. comp = &((struct ionic_txq_comp *)cq->base)[cq->tail_idx];
  1024. if (!color_match(comp->color, cq->done_color))
  1025. return false;
  1026. /* clean the related q entries, there could be
  1027. * several q entries completed for each cq completion
  1028. */
  1029. do {
  1030. desc_info = &q->tx_info[q->tail_idx];
  1031. desc_info->bytes = 0;
  1032. index = q->tail_idx;
  1033. q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
  1034. ionic_tx_clean(q, desc_info, comp, in_napi);
  1035. if (desc_info->skb) {
  1036. pkts++;
  1037. bytes += desc_info->bytes;
  1038. desc_info->skb = NULL;
  1039. }
  1040. } while (index != le16_to_cpu(comp->comp_index));
  1041. (*total_pkts) += pkts;
  1042. (*total_bytes) += bytes;
  1043. return true;
  1044. }
  1045. unsigned int ionic_tx_cq_service(struct ionic_cq *cq,
  1046. unsigned int work_to_do,
  1047. bool in_napi)
  1048. {
  1049. unsigned int work_done = 0;
  1050. unsigned int bytes = 0;
  1051. unsigned int pkts = 0;
  1052. if (work_to_do == 0)
  1053. return 0;
  1054. while (ionic_tx_service(cq, &pkts, &bytes, in_napi)) {
  1055. if (cq->tail_idx == cq->num_descs - 1)
  1056. cq->done_color = !cq->done_color;
  1057. cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
  1058. if (++work_done >= work_to_do)
  1059. break;
  1060. }
  1061. if (work_done) {
  1062. struct ionic_queue *q = cq->bound_q;
  1063. if (likely(!ionic_txq_hwstamp_enabled(q)))
  1064. netif_txq_completed_wake(q_to_ndq(q->lif->netdev, q),
  1065. pkts, bytes,
  1066. ionic_q_space_avail(q),
  1067. IONIC_TSO_DESCS_NEEDED);
  1068. }
  1069. return work_done;
  1070. }
  1071. void ionic_tx_flush(struct ionic_cq *cq)
  1072. {
  1073. u32 work_done;
  1074. work_done = ionic_tx_cq_service(cq, cq->num_descs, false);
  1075. if (work_done)
  1076. ionic_intr_credits(cq->idev->intr_ctrl, cq->bound_intr->index,
  1077. work_done, IONIC_INTR_CRED_RESET_COALESCE);
  1078. }
  1079. void ionic_tx_empty(struct ionic_queue *q)
  1080. {
  1081. struct ionic_tx_desc_info *desc_info;
  1082. int bytes = 0;
  1083. int pkts = 0;
  1084. /* walk the not completed tx entries, if any */
  1085. while (q->head_idx != q->tail_idx) {
  1086. desc_info = &q->tx_info[q->tail_idx];
  1087. desc_info->bytes = 0;
  1088. q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
  1089. ionic_tx_clean(q, desc_info, NULL, false);
  1090. if (desc_info->skb) {
  1091. pkts++;
  1092. bytes += desc_info->bytes;
  1093. desc_info->skb = NULL;
  1094. }
  1095. }
  1096. if (likely(!ionic_txq_hwstamp_enabled(q))) {
  1097. struct netdev_queue *ndq = q_to_ndq(q->lif->netdev, q);
  1098. netdev_tx_completed_queue(ndq, pkts, bytes);
  1099. netdev_tx_reset_queue(ndq);
  1100. }
  1101. }
  1102. static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb)
  1103. {
  1104. int err;
  1105. err = skb_cow_head(skb, 0);
  1106. if (unlikely(err))
  1107. return err;
  1108. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  1109. inner_ip_hdr(skb)->check = 0;
  1110. inner_tcp_hdr(skb)->check =
  1111. ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
  1112. inner_ip_hdr(skb)->daddr,
  1113. 0, IPPROTO_TCP, 0);
  1114. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  1115. inner_tcp_hdr(skb)->check =
  1116. ~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr,
  1117. &inner_ipv6_hdr(skb)->daddr,
  1118. 0, IPPROTO_TCP, 0);
  1119. }
  1120. return 0;
  1121. }
  1122. static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb)
  1123. {
  1124. int err;
  1125. err = skb_cow_head(skb, 0);
  1126. if (unlikely(err))
  1127. return err;
  1128. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  1129. ip_hdr(skb)->check = 0;
  1130. tcp_hdr(skb)->check =
  1131. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  1132. ip_hdr(skb)->daddr,
  1133. 0, IPPROTO_TCP, 0);
  1134. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  1135. tcp_v6_gso_csum_prep(skb);
  1136. }
  1137. return 0;
  1138. }
  1139. static void ionic_tx_tso_post(struct net_device *netdev, struct ionic_queue *q,
  1140. struct ionic_txq_desc *desc,
  1141. struct sk_buff *skb,
  1142. dma_addr_t addr, u8 nsge, u16 len,
  1143. unsigned int hdrlen, unsigned int mss,
  1144. bool outer_csum,
  1145. u16 vlan_tci, bool has_vlan,
  1146. bool start, bool done)
  1147. {
  1148. u8 flags = 0;
  1149. u64 cmd;
  1150. flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
  1151. flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
  1152. flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0;
  1153. flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0;
  1154. cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr);
  1155. desc->cmd = cpu_to_le64(cmd);
  1156. desc->len = cpu_to_le16(len);
  1157. desc->vlan_tci = cpu_to_le16(vlan_tci);
  1158. desc->hdr_len = cpu_to_le16(hdrlen);
  1159. desc->mss = cpu_to_le16(mss);
  1160. ionic_write_cmb_desc(q, desc);
  1161. if (start) {
  1162. skb_tx_timestamp(skb);
  1163. if (likely(!ionic_txq_hwstamp_enabled(q)))
  1164. netdev_tx_sent_queue(q_to_ndq(netdev, q), skb->len);
  1165. ionic_txq_post(q, false);
  1166. } else {
  1167. ionic_txq_post(q, done);
  1168. }
  1169. }
  1170. static int ionic_tx_tso(struct net_device *netdev, struct ionic_queue *q,
  1171. struct sk_buff *skb)
  1172. {
  1173. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  1174. struct ionic_tx_desc_info *desc_info;
  1175. struct ionic_buf_info *buf_info;
  1176. struct ionic_txq_sg_elem *elem;
  1177. struct ionic_txq_desc *desc;
  1178. unsigned int chunk_len;
  1179. unsigned int frag_rem;
  1180. unsigned int tso_rem;
  1181. unsigned int seg_rem;
  1182. dma_addr_t desc_addr;
  1183. dma_addr_t frag_addr;
  1184. unsigned int hdrlen;
  1185. unsigned int len;
  1186. unsigned int mss;
  1187. bool start, done;
  1188. bool outer_csum;
  1189. bool has_vlan;
  1190. u16 desc_len;
  1191. u8 desc_nsge;
  1192. u16 vlan_tci;
  1193. bool encap;
  1194. int err;
  1195. desc_info = &q->tx_info[q->head_idx];
  1196. if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
  1197. return -EIO;
  1198. len = skb->len;
  1199. mss = skb_shinfo(skb)->gso_size;
  1200. outer_csum = (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1201. SKB_GSO_GRE_CSUM |
  1202. SKB_GSO_IPXIP4 |
  1203. SKB_GSO_IPXIP6 |
  1204. SKB_GSO_UDP_TUNNEL |
  1205. SKB_GSO_UDP_TUNNEL_CSUM));
  1206. has_vlan = !!skb_vlan_tag_present(skb);
  1207. vlan_tci = skb_vlan_tag_get(skb);
  1208. encap = skb->encapsulation;
  1209. /* Preload inner-most TCP csum field with IP pseudo hdr
  1210. * calculated with IP length set to zero. HW will later
  1211. * add in length to each TCP segment resulting from the TSO.
  1212. */
  1213. if (encap)
  1214. err = ionic_tx_tcp_inner_pseudo_csum(skb);
  1215. else
  1216. err = ionic_tx_tcp_pseudo_csum(skb);
  1217. if (unlikely(err)) {
  1218. /* clean up mapping from ionic_tx_map_skb */
  1219. ionic_tx_desc_unmap_bufs(q, desc_info);
  1220. return err;
  1221. }
  1222. if (encap)
  1223. hdrlen = skb_inner_tcp_all_headers(skb);
  1224. else
  1225. hdrlen = skb_tcp_all_headers(skb);
  1226. desc_info->skb = skb;
  1227. buf_info = desc_info->bufs;
  1228. tso_rem = len;
  1229. seg_rem = min(tso_rem, hdrlen + mss);
  1230. frag_addr = 0;
  1231. frag_rem = 0;
  1232. start = true;
  1233. while (tso_rem > 0) {
  1234. desc = NULL;
  1235. elem = NULL;
  1236. desc_addr = 0;
  1237. desc_len = 0;
  1238. desc_nsge = 0;
  1239. /* use fragments until we have enough to post a single descriptor */
  1240. while (seg_rem > 0) {
  1241. /* if the fragment is exhausted then move to the next one */
  1242. if (frag_rem == 0) {
  1243. /* grab the next fragment */
  1244. frag_addr = buf_info->dma_addr;
  1245. frag_rem = buf_info->len;
  1246. buf_info++;
  1247. }
  1248. chunk_len = min(frag_rem, seg_rem);
  1249. if (!desc) {
  1250. /* fill main descriptor */
  1251. desc = &q->txq[q->head_idx];
  1252. elem = ionic_tx_sg_elems(q);
  1253. desc_addr = frag_addr;
  1254. desc_len = chunk_len;
  1255. } else {
  1256. /* fill sg descriptor */
  1257. elem->addr = cpu_to_le64(frag_addr);
  1258. elem->len = cpu_to_le16(chunk_len);
  1259. elem++;
  1260. desc_nsge++;
  1261. }
  1262. frag_addr += chunk_len;
  1263. frag_rem -= chunk_len;
  1264. tso_rem -= chunk_len;
  1265. seg_rem -= chunk_len;
  1266. }
  1267. seg_rem = min(tso_rem, mss);
  1268. done = (tso_rem == 0);
  1269. /* post descriptor */
  1270. ionic_tx_tso_post(netdev, q, desc, skb, desc_addr, desc_nsge,
  1271. desc_len, hdrlen, mss, outer_csum, vlan_tci,
  1272. has_vlan, start, done);
  1273. start = false;
  1274. /* Buffer information is stored with the first tso descriptor */
  1275. desc_info = &q->tx_info[q->head_idx];
  1276. desc_info->nbufs = 0;
  1277. }
  1278. stats->pkts += DIV_ROUND_UP(len - hdrlen, mss);
  1279. stats->bytes += len;
  1280. stats->tso++;
  1281. stats->tso_bytes = len;
  1282. return 0;
  1283. }
  1284. static void ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb,
  1285. struct ionic_tx_desc_info *desc_info)
  1286. {
  1287. struct ionic_txq_desc *desc = &q->txq[q->head_idx];
  1288. struct ionic_buf_info *buf_info = desc_info->bufs;
  1289. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  1290. bool has_vlan;
  1291. u8 flags = 0;
  1292. bool encap;
  1293. u64 cmd;
  1294. has_vlan = !!skb_vlan_tag_present(skb);
  1295. encap = skb->encapsulation;
  1296. flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
  1297. flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
  1298. cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL,
  1299. flags, skb_shinfo(skb)->nr_frags,
  1300. buf_info->dma_addr);
  1301. desc->cmd = cpu_to_le64(cmd);
  1302. desc->len = cpu_to_le16(buf_info->len);
  1303. if (has_vlan) {
  1304. desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
  1305. stats->vlan_inserted++;
  1306. } else {
  1307. desc->vlan_tci = 0;
  1308. }
  1309. desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb));
  1310. desc->csum_offset = cpu_to_le16(skb->csum_offset);
  1311. ionic_write_cmb_desc(q, desc);
  1312. if (skb_csum_is_sctp(skb))
  1313. stats->crc32_csum++;
  1314. else
  1315. stats->csum++;
  1316. }
  1317. static void ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb,
  1318. struct ionic_tx_desc_info *desc_info)
  1319. {
  1320. struct ionic_txq_desc *desc = &q->txq[q->head_idx];
  1321. struct ionic_buf_info *buf_info = desc_info->bufs;
  1322. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  1323. bool has_vlan;
  1324. u8 flags = 0;
  1325. bool encap;
  1326. u64 cmd;
  1327. has_vlan = !!skb_vlan_tag_present(skb);
  1328. encap = skb->encapsulation;
  1329. flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0;
  1330. flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0;
  1331. cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE,
  1332. flags, skb_shinfo(skb)->nr_frags,
  1333. buf_info->dma_addr);
  1334. desc->cmd = cpu_to_le64(cmd);
  1335. desc->len = cpu_to_le16(buf_info->len);
  1336. if (has_vlan) {
  1337. desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb));
  1338. stats->vlan_inserted++;
  1339. } else {
  1340. desc->vlan_tci = 0;
  1341. }
  1342. desc->csum_start = 0;
  1343. desc->csum_offset = 0;
  1344. ionic_write_cmb_desc(q, desc);
  1345. stats->csum_none++;
  1346. }
  1347. static void ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb,
  1348. struct ionic_tx_desc_info *desc_info)
  1349. {
  1350. struct ionic_buf_info *buf_info = &desc_info->bufs[1];
  1351. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  1352. struct ionic_txq_sg_elem *elem;
  1353. unsigned int i;
  1354. elem = ionic_tx_sg_elems(q);
  1355. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, buf_info++, elem++) {
  1356. elem->addr = cpu_to_le64(buf_info->dma_addr);
  1357. elem->len = cpu_to_le16(buf_info->len);
  1358. }
  1359. stats->frags += skb_shinfo(skb)->nr_frags;
  1360. }
  1361. static int ionic_tx(struct net_device *netdev, struct ionic_queue *q,
  1362. struct sk_buff *skb)
  1363. {
  1364. struct ionic_tx_desc_info *desc_info = &q->tx_info[q->head_idx];
  1365. struct ionic_tx_stats *stats = q_to_tx_stats(q);
  1366. bool ring_dbell = true;
  1367. if (unlikely(ionic_tx_map_skb(q, skb, desc_info)))
  1368. return -EIO;
  1369. desc_info->skb = skb;
  1370. /* set up the initial descriptor */
  1371. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1372. ionic_tx_calc_csum(q, skb, desc_info);
  1373. else
  1374. ionic_tx_calc_no_csum(q, skb, desc_info);
  1375. /* add frags */
  1376. ionic_tx_skb_frags(q, skb, desc_info);
  1377. skb_tx_timestamp(skb);
  1378. stats->pkts++;
  1379. stats->bytes += skb->len;
  1380. if (likely(!ionic_txq_hwstamp_enabled(q))) {
  1381. struct netdev_queue *ndq = q_to_ndq(netdev, q);
  1382. if (unlikely(!ionic_q_has_space(q, MAX_SKB_FRAGS + 1)))
  1383. netif_tx_stop_queue(ndq);
  1384. ring_dbell = __netdev_tx_sent_queue(ndq, skb->len,
  1385. netdev_xmit_more());
  1386. }
  1387. ionic_txq_post(q, ring_dbell);
  1388. return 0;
  1389. }
  1390. static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb)
  1391. {
  1392. int nr_frags = skb_shinfo(skb)->nr_frags;
  1393. bool too_many_frags = false;
  1394. skb_frag_t *frag;
  1395. int desc_bufs;
  1396. int chunk_len;
  1397. int frag_rem;
  1398. int tso_rem;
  1399. int seg_rem;
  1400. bool encap;
  1401. int hdrlen;
  1402. int ndescs;
  1403. int err;
  1404. /* Each desc is mss long max, so a descriptor for each gso_seg */
  1405. if (skb_is_gso(skb)) {
  1406. ndescs = skb_shinfo(skb)->gso_segs;
  1407. if (!nr_frags)
  1408. return ndescs;
  1409. } else {
  1410. ndescs = 1;
  1411. if (!nr_frags)
  1412. return ndescs;
  1413. if (unlikely(nr_frags > q->max_sg_elems)) {
  1414. too_many_frags = true;
  1415. goto linearize;
  1416. }
  1417. return ndescs;
  1418. }
  1419. /* We need to scan the skb to be sure that none of the MTU sized
  1420. * packets in the TSO will require more sgs per descriptor than we
  1421. * can support. We loop through the frags, add up the lengths for
  1422. * a packet, and count the number of sgs used per packet.
  1423. */
  1424. tso_rem = skb->len;
  1425. frag = skb_shinfo(skb)->frags;
  1426. encap = skb->encapsulation;
  1427. /* start with just hdr in first part of first descriptor */
  1428. if (encap)
  1429. hdrlen = skb_inner_tcp_all_headers(skb);
  1430. else
  1431. hdrlen = skb_tcp_all_headers(skb);
  1432. seg_rem = min_t(int, tso_rem, hdrlen + skb_shinfo(skb)->gso_size);
  1433. frag_rem = hdrlen;
  1434. while (tso_rem > 0) {
  1435. desc_bufs = 0;
  1436. while (seg_rem > 0) {
  1437. desc_bufs++;
  1438. /* We add the +1 because we can take buffers for one
  1439. * more than we have SGs: one for the initial desc data
  1440. * in addition to the SG segments that might follow.
  1441. */
  1442. if (desc_bufs > q->max_sg_elems + 1) {
  1443. too_many_frags = true;
  1444. goto linearize;
  1445. }
  1446. if (frag_rem == 0) {
  1447. frag_rem = skb_frag_size(frag);
  1448. frag++;
  1449. }
  1450. chunk_len = min(frag_rem, seg_rem);
  1451. frag_rem -= chunk_len;
  1452. tso_rem -= chunk_len;
  1453. seg_rem -= chunk_len;
  1454. }
  1455. seg_rem = min_t(int, tso_rem, skb_shinfo(skb)->gso_size);
  1456. }
  1457. linearize:
  1458. if (too_many_frags) {
  1459. err = skb_linearize(skb);
  1460. if (unlikely(err))
  1461. return err;
  1462. q_to_tx_stats(q)->linearize++;
  1463. }
  1464. return ndescs;
  1465. }
  1466. static netdev_tx_t ionic_start_hwstamp_xmit(struct sk_buff *skb,
  1467. struct net_device *netdev)
  1468. {
  1469. struct ionic_lif *lif = netdev_priv(netdev);
  1470. struct ionic_queue *q;
  1471. int err, ndescs;
  1472. /* Does not stop/start txq, because we post to a separate tx queue
  1473. * for timestamping, and if a packet can't be posted immediately to
  1474. * the timestamping queue, it is dropped.
  1475. */
  1476. q = &lif->hwstamp_txq->q;
  1477. ndescs = ionic_tx_descs_needed(q, skb);
  1478. if (unlikely(ndescs < 0))
  1479. goto err_out_drop;
  1480. if (unlikely(!ionic_q_has_space(q, ndescs)))
  1481. goto err_out_drop;
  1482. skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP;
  1483. if (skb_is_gso(skb))
  1484. err = ionic_tx_tso(netdev, q, skb);
  1485. else
  1486. err = ionic_tx(netdev, q, skb);
  1487. if (unlikely(err))
  1488. goto err_out_drop;
  1489. return NETDEV_TX_OK;
  1490. err_out_drop:
  1491. q->drop++;
  1492. dev_kfree_skb(skb);
  1493. return NETDEV_TX_OK;
  1494. }
  1495. netdev_tx_t ionic_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  1496. {
  1497. u16 queue_index = skb_get_queue_mapping(skb);
  1498. struct ionic_lif *lif = netdev_priv(netdev);
  1499. struct ionic_queue *q;
  1500. int ndescs;
  1501. int err;
  1502. if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state))) {
  1503. dev_kfree_skb(skb);
  1504. return NETDEV_TX_OK;
  1505. }
  1506. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
  1507. if (lif->hwstamp_txq && lif->phc->ts_config_tx_mode)
  1508. return ionic_start_hwstamp_xmit(skb, netdev);
  1509. if (unlikely(queue_index >= lif->nxqs))
  1510. queue_index = 0;
  1511. q = &lif->txqcqs[queue_index]->q;
  1512. ndescs = ionic_tx_descs_needed(q, skb);
  1513. if (ndescs < 0)
  1514. goto err_out_drop;
  1515. if (!netif_txq_maybe_stop(q_to_ndq(netdev, q),
  1516. ionic_q_space_avail(q),
  1517. ndescs, ndescs))
  1518. return NETDEV_TX_BUSY;
  1519. if (skb_is_gso(skb))
  1520. err = ionic_tx_tso(netdev, q, skb);
  1521. else
  1522. err = ionic_tx(netdev, q, skb);
  1523. if (unlikely(err))
  1524. goto err_out_drop;
  1525. return NETDEV_TX_OK;
  1526. err_out_drop:
  1527. q->drop++;
  1528. dev_kfree_skb(skb);
  1529. return NETDEV_TX_OK;
  1530. }