funeth_tx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. #include <linux/dma-mapping.h>
  3. #include <linux/ip.h>
  4. #include <linux/pci.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/tcp.h>
  7. #include <uapi/linux/udp.h>
  8. #include "funeth.h"
  9. #include "funeth_ktls.h"
  10. #include "funeth_txrx.h"
  11. #include "funeth_trace.h"
  12. #include "fun_queue.h"
  13. #define FUN_XDP_CLEAN_THRES 32
  14. #define FUN_XDP_CLEAN_BATCH 16
  15. /* DMA-map a packet and return the (length, DMA_address) pairs for its
  16. * segments. If a mapping error occurs -ENOMEM is returned. The packet
  17. * consists of an skb_shared_info and one additional address/length pair.
  18. */
  19. static int fun_map_pkt(struct device *dev, const struct skb_shared_info *si,
  20. void *data, unsigned int data_len,
  21. dma_addr_t *addr, unsigned int *len)
  22. {
  23. const skb_frag_t *fp, *end;
  24. *len = data_len;
  25. *addr = dma_map_single(dev, data, *len, DMA_TO_DEVICE);
  26. if (dma_mapping_error(dev, *addr))
  27. return -ENOMEM;
  28. if (!si)
  29. return 0;
  30. for (fp = si->frags, end = fp + si->nr_frags; fp < end; fp++) {
  31. *++len = skb_frag_size(fp);
  32. *++addr = skb_frag_dma_map(dev, fp, 0, *len, DMA_TO_DEVICE);
  33. if (dma_mapping_error(dev, *addr))
  34. goto unwind;
  35. }
  36. return 0;
  37. unwind:
  38. while (fp-- > si->frags)
  39. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  40. dma_unmap_single(dev, addr[-1], data_len, DMA_TO_DEVICE);
  41. return -ENOMEM;
  42. }
  43. /* Return the address just past the end of a Tx queue's descriptor ring.
  44. * It exploits the fact that the HW writeback area is just after the end
  45. * of the descriptor ring.
  46. */
  47. static void *txq_end(const struct funeth_txq *q)
  48. {
  49. return (void *)q->hw_wb;
  50. }
  51. /* Return the amount of space within a Tx ring from the given address to the
  52. * end.
  53. */
  54. static unsigned int txq_to_end(const struct funeth_txq *q, void *p)
  55. {
  56. return txq_end(q) - p;
  57. }
  58. /* Return the number of Tx descriptors occupied by a Tx request. */
  59. static unsigned int tx_req_ndesc(const struct fun_eth_tx_req *req)
  60. {
  61. return DIV_ROUND_UP(req->len8, FUNETH_SQE_SIZE / 8);
  62. }
  63. /* Write a gather list to the Tx descriptor at @req from @ngle address/length
  64. * pairs.
  65. */
  66. static struct fun_dataop_gl *fun_write_gl(const struct funeth_txq *q,
  67. struct fun_eth_tx_req *req,
  68. const dma_addr_t *addrs,
  69. const unsigned int *lens,
  70. unsigned int ngle)
  71. {
  72. struct fun_dataop_gl *gle;
  73. unsigned int i;
  74. req->len8 = (sizeof(*req) + ngle * sizeof(*gle)) / 8;
  75. for (i = 0, gle = (struct fun_dataop_gl *)req->dataop.imm;
  76. i < ngle && txq_to_end(q, gle); i++, gle++)
  77. fun_dataop_gl_init(gle, 0, 0, lens[i], addrs[i]);
  78. if (txq_to_end(q, gle) == 0) {
  79. gle = (struct fun_dataop_gl *)q->desc;
  80. for ( ; i < ngle; i++, gle++)
  81. fun_dataop_gl_init(gle, 0, 0, lens[i], addrs[i]);
  82. }
  83. return gle;
  84. }
  85. static __be16 tcp_hdr_doff_flags(const struct tcphdr *th)
  86. {
  87. return *(__be16 *)&tcp_flag_word(th);
  88. }
  89. static struct sk_buff *fun_tls_tx(struct sk_buff *skb, struct funeth_txq *q,
  90. unsigned int *tls_len)
  91. {
  92. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  93. const struct fun_ktls_tx_ctx *tls_ctx;
  94. u32 datalen, seq;
  95. datalen = skb->len - skb_tcp_all_headers(skb);
  96. if (!datalen)
  97. return skb;
  98. if (likely(!tls_offload_tx_resync_pending(skb->sk))) {
  99. seq = ntohl(tcp_hdr(skb)->seq);
  100. tls_ctx = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
  101. if (likely(tls_ctx->next_seq == seq)) {
  102. *tls_len = datalen;
  103. return skb;
  104. }
  105. if (seq - tls_ctx->next_seq < U32_MAX / 4) {
  106. tls_offload_tx_resync_request(skb->sk, seq,
  107. tls_ctx->next_seq);
  108. }
  109. }
  110. FUN_QSTAT_INC(q, tx_tls_fallback);
  111. skb = tls_encrypt_skb(skb);
  112. if (!skb)
  113. FUN_QSTAT_INC(q, tx_tls_drops);
  114. return skb;
  115. #else
  116. return NULL;
  117. #endif
  118. }
  119. /* Write as many descriptors as needed for the supplied skb starting at the
  120. * current producer location. The caller has made certain enough descriptors
  121. * are available.
  122. *
  123. * Returns the number of descriptors written, 0 on error.
  124. */
  125. static unsigned int write_pkt_desc(struct sk_buff *skb, struct funeth_txq *q,
  126. unsigned int tls_len)
  127. {
  128. unsigned int extra_bytes = 0, extra_pkts = 0;
  129. unsigned int idx = q->prod_cnt & q->mask;
  130. const struct skb_shared_info *shinfo;
  131. unsigned int lens[MAX_SKB_FRAGS + 1];
  132. dma_addr_t addrs[MAX_SKB_FRAGS + 1];
  133. struct fun_eth_tx_req *req;
  134. struct fun_dataop_gl *gle;
  135. const struct tcphdr *th;
  136. unsigned int l4_hlen;
  137. unsigned int ngle;
  138. u16 flags;
  139. shinfo = skb_shinfo(skb);
  140. if (unlikely(fun_map_pkt(q->dma_dev, shinfo, skb->data,
  141. skb_headlen(skb), addrs, lens))) {
  142. FUN_QSTAT_INC(q, tx_map_err);
  143. return 0;
  144. }
  145. req = fun_tx_desc_addr(q, idx);
  146. req->op = FUN_ETH_OP_TX;
  147. req->len8 = 0;
  148. req->flags = 0;
  149. req->suboff8 = offsetof(struct fun_eth_tx_req, dataop);
  150. req->repr_idn = 0;
  151. req->encap_proto = 0;
  152. if (likely(shinfo->gso_size)) {
  153. if (skb->encapsulation) {
  154. u16 ol4_ofst;
  155. flags = FUN_ETH_OUTER_EN | FUN_ETH_INNER_LSO |
  156. FUN_ETH_UPDATE_INNER_L4_CKSUM |
  157. FUN_ETH_UPDATE_OUTER_L3_LEN;
  158. if (shinfo->gso_type & (SKB_GSO_UDP_TUNNEL |
  159. SKB_GSO_UDP_TUNNEL_CSUM)) {
  160. flags |= FUN_ETH_UPDATE_OUTER_L4_LEN |
  161. FUN_ETH_OUTER_UDP;
  162. if (shinfo->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)
  163. flags |= FUN_ETH_UPDATE_OUTER_L4_CKSUM;
  164. ol4_ofst = skb_transport_offset(skb);
  165. } else {
  166. ol4_ofst = skb_inner_network_offset(skb);
  167. }
  168. if (ip_hdr(skb)->version == 4)
  169. flags |= FUN_ETH_UPDATE_OUTER_L3_CKSUM;
  170. else
  171. flags |= FUN_ETH_OUTER_IPV6;
  172. if (skb->inner_network_header) {
  173. if (inner_ip_hdr(skb)->version == 4)
  174. flags |= FUN_ETH_UPDATE_INNER_L3_CKSUM |
  175. FUN_ETH_UPDATE_INNER_L3_LEN;
  176. else
  177. flags |= FUN_ETH_INNER_IPV6 |
  178. FUN_ETH_UPDATE_INNER_L3_LEN;
  179. }
  180. th = inner_tcp_hdr(skb);
  181. l4_hlen = __tcp_hdrlen(th);
  182. fun_eth_offload_init(&req->offload, flags,
  183. shinfo->gso_size,
  184. tcp_hdr_doff_flags(th), 0,
  185. skb_inner_network_offset(skb),
  186. skb_inner_transport_offset(skb),
  187. skb_network_offset(skb), ol4_ofst);
  188. FUN_QSTAT_INC(q, tx_encap_tso);
  189. } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
  190. flags = FUN_ETH_INNER_LSO | FUN_ETH_INNER_UDP |
  191. FUN_ETH_UPDATE_INNER_L4_CKSUM |
  192. FUN_ETH_UPDATE_INNER_L4_LEN |
  193. FUN_ETH_UPDATE_INNER_L3_LEN;
  194. if (ip_hdr(skb)->version == 4)
  195. flags |= FUN_ETH_UPDATE_INNER_L3_CKSUM;
  196. else
  197. flags |= FUN_ETH_INNER_IPV6;
  198. l4_hlen = sizeof(struct udphdr);
  199. fun_eth_offload_init(&req->offload, flags,
  200. shinfo->gso_size,
  201. cpu_to_be16(l4_hlen << 10), 0,
  202. skb_network_offset(skb),
  203. skb_transport_offset(skb), 0, 0);
  204. FUN_QSTAT_INC(q, tx_uso);
  205. } else {
  206. /* HW considers one set of headers as inner */
  207. flags = FUN_ETH_INNER_LSO |
  208. FUN_ETH_UPDATE_INNER_L4_CKSUM |
  209. FUN_ETH_UPDATE_INNER_L3_LEN;
  210. if (shinfo->gso_type & SKB_GSO_TCPV6)
  211. flags |= FUN_ETH_INNER_IPV6;
  212. else
  213. flags |= FUN_ETH_UPDATE_INNER_L3_CKSUM;
  214. th = tcp_hdr(skb);
  215. l4_hlen = __tcp_hdrlen(th);
  216. fun_eth_offload_init(&req->offload, flags,
  217. shinfo->gso_size,
  218. tcp_hdr_doff_flags(th), 0,
  219. skb_network_offset(skb),
  220. skb_transport_offset(skb), 0, 0);
  221. FUN_QSTAT_INC(q, tx_tso);
  222. }
  223. u64_stats_update_begin(&q->syncp);
  224. q->stats.tx_cso += shinfo->gso_segs;
  225. u64_stats_update_end(&q->syncp);
  226. extra_pkts = shinfo->gso_segs - 1;
  227. extra_bytes = (be16_to_cpu(req->offload.inner_l4_off) +
  228. l4_hlen) * extra_pkts;
  229. } else if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  230. flags = FUN_ETH_UPDATE_INNER_L4_CKSUM;
  231. if (skb->csum_offset == offsetof(struct udphdr, check))
  232. flags |= FUN_ETH_INNER_UDP;
  233. fun_eth_offload_init(&req->offload, flags, 0, 0, 0, 0,
  234. skb_checksum_start_offset(skb), 0, 0);
  235. FUN_QSTAT_INC(q, tx_cso);
  236. } else {
  237. fun_eth_offload_init(&req->offload, 0, 0, 0, 0, 0, 0, 0, 0);
  238. }
  239. ngle = shinfo->nr_frags + 1;
  240. req->dataop = FUN_DATAOP_HDR_INIT(ngle, 0, ngle, 0, skb->len);
  241. gle = fun_write_gl(q, req, addrs, lens, ngle);
  242. if (IS_ENABLED(CONFIG_TLS_DEVICE) && unlikely(tls_len)) {
  243. struct fun_eth_tls *tls = (struct fun_eth_tls *)gle;
  244. struct fun_ktls_tx_ctx *tls_ctx;
  245. req->len8 += FUNETH_TLS_SZ / 8;
  246. req->flags = cpu_to_be16(FUN_ETH_TX_TLS);
  247. tls_ctx = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
  248. tls->tlsid = tls_ctx->tlsid;
  249. tls_ctx->next_seq += tls_len;
  250. u64_stats_update_begin(&q->syncp);
  251. q->stats.tx_tls_bytes += tls_len;
  252. q->stats.tx_tls_pkts += 1 + extra_pkts;
  253. u64_stats_update_end(&q->syncp);
  254. }
  255. u64_stats_update_begin(&q->syncp);
  256. q->stats.tx_bytes += skb->len + extra_bytes;
  257. q->stats.tx_pkts += 1 + extra_pkts;
  258. u64_stats_update_end(&q->syncp);
  259. q->info[idx].skb = skb;
  260. trace_funeth_tx(q, skb->len, idx, req->dataop.ngather);
  261. return tx_req_ndesc(req);
  262. }
  263. /* Return the number of available descriptors of a Tx queue.
  264. * HW assumes head==tail means the ring is empty so we need to keep one
  265. * descriptor unused.
  266. */
  267. static unsigned int fun_txq_avail(const struct funeth_txq *q)
  268. {
  269. return q->mask - q->prod_cnt + q->cons_cnt;
  270. }
  271. /* Stop a queue if it can't handle another worst-case packet. */
  272. static void fun_tx_check_stop(struct funeth_txq *q)
  273. {
  274. if (likely(fun_txq_avail(q) >= FUNETH_MAX_PKT_DESC))
  275. return;
  276. netif_tx_stop_queue(q->ndq);
  277. /* NAPI reclaim is freeing packets in parallel with us and we may race.
  278. * We have stopped the queue but check again after synchronizing with
  279. * reclaim.
  280. */
  281. smp_mb();
  282. if (likely(fun_txq_avail(q) < FUNETH_MAX_PKT_DESC))
  283. FUN_QSTAT_INC(q, tx_nstops);
  284. else
  285. netif_tx_start_queue(q->ndq);
  286. }
  287. /* Return true if a queue has enough space to restart. Current condition is
  288. * that the queue must be >= 1/4 empty.
  289. */
  290. static bool fun_txq_may_restart(struct funeth_txq *q)
  291. {
  292. return fun_txq_avail(q) >= q->mask / 4;
  293. }
  294. netdev_tx_t fun_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  295. {
  296. struct funeth_priv *fp = netdev_priv(netdev);
  297. unsigned int qid = skb_get_queue_mapping(skb);
  298. struct funeth_txq *q = fp->txqs[qid];
  299. unsigned int tls_len = 0;
  300. unsigned int ndesc;
  301. if (tls_is_skb_tx_device_offloaded(skb)) {
  302. skb = fun_tls_tx(skb, q, &tls_len);
  303. if (unlikely(!skb))
  304. goto dropped;
  305. }
  306. ndesc = write_pkt_desc(skb, q, tls_len);
  307. if (unlikely(!ndesc)) {
  308. dev_kfree_skb_any(skb);
  309. goto dropped;
  310. }
  311. q->prod_cnt += ndesc;
  312. fun_tx_check_stop(q);
  313. skb_tx_timestamp(skb);
  314. if (__netdev_tx_sent_queue(q->ndq, skb->len, netdev_xmit_more()))
  315. fun_txq_wr_db(q);
  316. else
  317. FUN_QSTAT_INC(q, tx_more);
  318. return NETDEV_TX_OK;
  319. dropped:
  320. /* A dropped packet may be the last one in a xmit_more train,
  321. * ring the doorbell just in case.
  322. */
  323. if (!netdev_xmit_more())
  324. fun_txq_wr_db(q);
  325. return NETDEV_TX_OK;
  326. }
  327. /* Return a Tx queue's HW head index written back to host memory. */
  328. static u16 txq_hw_head(const struct funeth_txq *q)
  329. {
  330. return (u16)be64_to_cpu(*q->hw_wb);
  331. }
  332. /* Unmap the Tx packet starting at the given descriptor index and
  333. * return the number of Tx descriptors it occupied.
  334. */
  335. static unsigned int fun_unmap_pkt(const struct funeth_txq *q, unsigned int idx)
  336. {
  337. const struct fun_eth_tx_req *req = fun_tx_desc_addr(q, idx);
  338. unsigned int ngle = req->dataop.ngather;
  339. struct fun_dataop_gl *gle;
  340. if (ngle) {
  341. gle = (struct fun_dataop_gl *)req->dataop.imm;
  342. dma_unmap_single(q->dma_dev, be64_to_cpu(gle->sgl_data),
  343. be32_to_cpu(gle->sgl_len), DMA_TO_DEVICE);
  344. for (gle++; --ngle && txq_to_end(q, gle); gle++)
  345. dma_unmap_page(q->dma_dev, be64_to_cpu(gle->sgl_data),
  346. be32_to_cpu(gle->sgl_len),
  347. DMA_TO_DEVICE);
  348. for (gle = (struct fun_dataop_gl *)q->desc; ngle; ngle--, gle++)
  349. dma_unmap_page(q->dma_dev, be64_to_cpu(gle->sgl_data),
  350. be32_to_cpu(gle->sgl_len),
  351. DMA_TO_DEVICE);
  352. }
  353. return tx_req_ndesc(req);
  354. }
  355. /* Reclaim completed Tx descriptors and free their packets. Restart a stopped
  356. * queue if we freed enough descriptors.
  357. *
  358. * Return true if we exhausted the budget while there is more work to be done.
  359. */
  360. static bool fun_txq_reclaim(struct funeth_txq *q, int budget)
  361. {
  362. unsigned int npkts = 0, nbytes = 0, ndesc = 0;
  363. unsigned int head, limit, reclaim_idx;
  364. /* budget may be 0, e.g., netpoll */
  365. limit = budget ? budget : UINT_MAX;
  366. for (head = txq_hw_head(q), reclaim_idx = q->cons_cnt & q->mask;
  367. head != reclaim_idx && npkts < limit; head = txq_hw_head(q)) {
  368. /* The HW head is continually updated, ensure we don't read
  369. * descriptor state before the head tells us to reclaim it.
  370. * On the enqueue side the doorbell is an implicit write
  371. * barrier.
  372. */
  373. rmb();
  374. do {
  375. unsigned int pkt_desc = fun_unmap_pkt(q, reclaim_idx);
  376. struct sk_buff *skb = q->info[reclaim_idx].skb;
  377. trace_funeth_tx_free(q, reclaim_idx, pkt_desc, head);
  378. nbytes += skb->len;
  379. napi_consume_skb(skb, budget);
  380. ndesc += pkt_desc;
  381. reclaim_idx = (reclaim_idx + pkt_desc) & q->mask;
  382. npkts++;
  383. } while (reclaim_idx != head && npkts < limit);
  384. }
  385. q->cons_cnt += ndesc;
  386. netdev_tx_completed_queue(q->ndq, npkts, nbytes);
  387. smp_mb(); /* pairs with the one in fun_tx_check_stop() */
  388. if (unlikely(netif_tx_queue_stopped(q->ndq) &&
  389. fun_txq_may_restart(q))) {
  390. netif_tx_wake_queue(q->ndq);
  391. FUN_QSTAT_INC(q, tx_nrestarts);
  392. }
  393. return reclaim_idx != head;
  394. }
  395. /* The NAPI handler for Tx queues. */
  396. int fun_txq_napi_poll(struct napi_struct *napi, int budget)
  397. {
  398. struct fun_irq *irq = container_of(napi, struct fun_irq, napi);
  399. struct funeth_txq *q = irq->txq;
  400. unsigned int db_val;
  401. if (fun_txq_reclaim(q, budget))
  402. return budget; /* exhausted budget */
  403. napi_complete(napi); /* exhausted pending work */
  404. db_val = READ_ONCE(q->irq_db_val) | (q->cons_cnt & q->mask);
  405. writel(db_val, q->db);
  406. return 0;
  407. }
  408. /* Reclaim up to @budget completed Tx packets from a TX XDP queue. */
  409. static unsigned int fun_xdpq_clean(struct funeth_txq *q, unsigned int budget)
  410. {
  411. unsigned int npkts = 0, ndesc = 0, head, reclaim_idx;
  412. for (head = txq_hw_head(q), reclaim_idx = q->cons_cnt & q->mask;
  413. head != reclaim_idx && npkts < budget; head = txq_hw_head(q)) {
  414. /* The HW head is continually updated, ensure we don't read
  415. * descriptor state before the head tells us to reclaim it.
  416. * On the enqueue side the doorbell is an implicit write
  417. * barrier.
  418. */
  419. rmb();
  420. do {
  421. unsigned int pkt_desc = fun_unmap_pkt(q, reclaim_idx);
  422. xdp_return_frame(q->info[reclaim_idx].xdpf);
  423. trace_funeth_tx_free(q, reclaim_idx, pkt_desc, head);
  424. reclaim_idx = (reclaim_idx + pkt_desc) & q->mask;
  425. ndesc += pkt_desc;
  426. npkts++;
  427. } while (reclaim_idx != head && npkts < budget);
  428. }
  429. q->cons_cnt += ndesc;
  430. return npkts;
  431. }
  432. bool fun_xdp_tx(struct funeth_txq *q, struct xdp_frame *xdpf)
  433. {
  434. unsigned int idx, nfrags = 1, ndesc = 1, tot_len = xdpf->len;
  435. const struct skb_shared_info *si = NULL;
  436. unsigned int lens[MAX_SKB_FRAGS + 1];
  437. dma_addr_t dma[MAX_SKB_FRAGS + 1];
  438. struct fun_eth_tx_req *req;
  439. if (fun_txq_avail(q) < FUN_XDP_CLEAN_THRES)
  440. fun_xdpq_clean(q, FUN_XDP_CLEAN_BATCH);
  441. if (unlikely(xdp_frame_has_frags(xdpf))) {
  442. si = xdp_get_shared_info_from_frame(xdpf);
  443. tot_len = xdp_get_frame_len(xdpf);
  444. nfrags += si->nr_frags;
  445. ndesc = DIV_ROUND_UP((sizeof(*req) + nfrags *
  446. sizeof(struct fun_dataop_gl)),
  447. FUNETH_SQE_SIZE);
  448. }
  449. if (unlikely(fun_txq_avail(q) < ndesc)) {
  450. FUN_QSTAT_INC(q, tx_xdp_full);
  451. return false;
  452. }
  453. if (unlikely(fun_map_pkt(q->dma_dev, si, xdpf->data, xdpf->len, dma,
  454. lens))) {
  455. FUN_QSTAT_INC(q, tx_map_err);
  456. return false;
  457. }
  458. idx = q->prod_cnt & q->mask;
  459. req = fun_tx_desc_addr(q, idx);
  460. req->op = FUN_ETH_OP_TX;
  461. req->len8 = 0;
  462. req->flags = 0;
  463. req->suboff8 = offsetof(struct fun_eth_tx_req, dataop);
  464. req->repr_idn = 0;
  465. req->encap_proto = 0;
  466. fun_eth_offload_init(&req->offload, 0, 0, 0, 0, 0, 0, 0, 0);
  467. req->dataop = FUN_DATAOP_HDR_INIT(nfrags, 0, nfrags, 0, tot_len);
  468. fun_write_gl(q, req, dma, lens, nfrags);
  469. q->info[idx].xdpf = xdpf;
  470. u64_stats_update_begin(&q->syncp);
  471. q->stats.tx_bytes += tot_len;
  472. q->stats.tx_pkts++;
  473. u64_stats_update_end(&q->syncp);
  474. trace_funeth_tx(q, tot_len, idx, nfrags);
  475. q->prod_cnt += ndesc;
  476. return true;
  477. }
  478. int fun_xdp_xmit_frames(struct net_device *dev, int n,
  479. struct xdp_frame **frames, u32 flags)
  480. {
  481. struct funeth_priv *fp = netdev_priv(dev);
  482. struct funeth_txq *q, **xdpqs;
  483. int i, q_idx;
  484. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  485. return -EINVAL;
  486. xdpqs = rcu_dereference_bh(fp->xdpqs);
  487. if (unlikely(!xdpqs))
  488. return -ENETDOWN;
  489. q_idx = smp_processor_id();
  490. if (unlikely(q_idx >= fp->num_xdpqs))
  491. return -ENXIO;
  492. for (q = xdpqs[q_idx], i = 0; i < n; i++)
  493. if (!fun_xdp_tx(q, frames[i]))
  494. break;
  495. if (unlikely(flags & XDP_XMIT_FLUSH))
  496. fun_txq_wr_db(q);
  497. return i;
  498. }
  499. /* Purge a Tx queue of any queued packets. Should be called once HW access
  500. * to the packets has been revoked, e.g., after the queue has been disabled.
  501. */
  502. static void fun_txq_purge(struct funeth_txq *q)
  503. {
  504. while (q->cons_cnt != q->prod_cnt) {
  505. unsigned int idx = q->cons_cnt & q->mask;
  506. q->cons_cnt += fun_unmap_pkt(q, idx);
  507. dev_kfree_skb_any(q->info[idx].skb);
  508. }
  509. netdev_tx_reset_queue(q->ndq);
  510. }
  511. static void fun_xdpq_purge(struct funeth_txq *q)
  512. {
  513. while (q->cons_cnt != q->prod_cnt) {
  514. unsigned int idx = q->cons_cnt & q->mask;
  515. q->cons_cnt += fun_unmap_pkt(q, idx);
  516. xdp_return_frame(q->info[idx].xdpf);
  517. }
  518. }
  519. /* Create a Tx queue, allocating all the host resources needed. */
  520. static struct funeth_txq *fun_txq_create_sw(struct net_device *dev,
  521. unsigned int qidx,
  522. unsigned int ndesc,
  523. struct fun_irq *irq)
  524. {
  525. struct funeth_priv *fp = netdev_priv(dev);
  526. struct funeth_txq *q;
  527. int numa_node;
  528. if (irq)
  529. numa_node = fun_irq_node(irq); /* skb Tx queue */
  530. else
  531. numa_node = cpu_to_node(qidx); /* XDP Tx queue */
  532. q = kzalloc_node(sizeof(*q), GFP_KERNEL, numa_node);
  533. if (!q)
  534. goto err;
  535. q->dma_dev = &fp->pdev->dev;
  536. q->desc = fun_alloc_ring_mem(q->dma_dev, ndesc, FUNETH_SQE_SIZE,
  537. sizeof(*q->info), true, numa_node,
  538. &q->dma_addr, (void **)&q->info,
  539. &q->hw_wb);
  540. if (!q->desc)
  541. goto free_q;
  542. q->netdev = dev;
  543. q->mask = ndesc - 1;
  544. q->qidx = qidx;
  545. q->numa_node = numa_node;
  546. u64_stats_init(&q->syncp);
  547. q->init_state = FUN_QSTATE_INIT_SW;
  548. return q;
  549. free_q:
  550. kfree(q);
  551. err:
  552. netdev_err(dev, "Can't allocate memory for %s queue %u\n",
  553. irq ? "Tx" : "XDP", qidx);
  554. return NULL;
  555. }
  556. static void fun_txq_free_sw(struct funeth_txq *q)
  557. {
  558. struct funeth_priv *fp = netdev_priv(q->netdev);
  559. fun_free_ring_mem(q->dma_dev, q->mask + 1, FUNETH_SQE_SIZE, true,
  560. q->desc, q->dma_addr, q->info);
  561. fp->tx_packets += q->stats.tx_pkts;
  562. fp->tx_bytes += q->stats.tx_bytes;
  563. fp->tx_dropped += q->stats.tx_map_err;
  564. kfree(q);
  565. }
  566. /* Allocate the device portion of a Tx queue. */
  567. int fun_txq_create_dev(struct funeth_txq *q, struct fun_irq *irq)
  568. {
  569. struct funeth_priv *fp = netdev_priv(q->netdev);
  570. unsigned int irq_idx, ndesc = q->mask + 1;
  571. int err;
  572. q->irq = irq;
  573. *q->hw_wb = 0;
  574. q->prod_cnt = 0;
  575. q->cons_cnt = 0;
  576. irq_idx = irq ? irq->irq_idx : 0;
  577. err = fun_sq_create(fp->fdev,
  578. FUN_ADMIN_EPSQ_CREATE_FLAG_HEAD_WB_ADDRESS |
  579. FUN_ADMIN_RES_CREATE_FLAG_ALLOCATOR, 0,
  580. FUN_HCI_ID_INVALID, ilog2(FUNETH_SQE_SIZE), ndesc,
  581. q->dma_addr, fp->tx_coal_count, fp->tx_coal_usec,
  582. irq_idx, 0, fp->fdev->kern_end_qid, 0,
  583. &q->hw_qid, &q->db);
  584. if (err)
  585. goto out;
  586. err = fun_create_and_bind_tx(fp, q->hw_qid);
  587. if (err < 0)
  588. goto free_devq;
  589. q->ethid = err;
  590. if (irq) {
  591. irq->txq = q;
  592. q->ndq = netdev_get_tx_queue(q->netdev, q->qidx);
  593. q->irq_db_val = FUN_IRQ_SQ_DB(fp->tx_coal_usec,
  594. fp->tx_coal_count);
  595. writel(q->irq_db_val, q->db);
  596. }
  597. q->init_state = FUN_QSTATE_INIT_FULL;
  598. netif_info(fp, ifup, q->netdev,
  599. "%s queue %u, depth %u, HW qid %u, IRQ idx %u, eth id %u, node %d\n",
  600. irq ? "Tx" : "XDP", q->qidx, ndesc, q->hw_qid, irq_idx,
  601. q->ethid, q->numa_node);
  602. return 0;
  603. free_devq:
  604. fun_destroy_sq(fp->fdev, q->hw_qid);
  605. out:
  606. netdev_err(q->netdev,
  607. "Failed to create %s queue %u on device, error %d\n",
  608. irq ? "Tx" : "XDP", q->qidx, err);
  609. return err;
  610. }
  611. static void fun_txq_free_dev(struct funeth_txq *q)
  612. {
  613. struct funeth_priv *fp = netdev_priv(q->netdev);
  614. if (q->init_state < FUN_QSTATE_INIT_FULL)
  615. return;
  616. netif_info(fp, ifdown, q->netdev,
  617. "Freeing %s queue %u (id %u), IRQ %u, ethid %u\n",
  618. q->irq ? "Tx" : "XDP", q->qidx, q->hw_qid,
  619. q->irq ? q->irq->irq_idx : 0, q->ethid);
  620. fun_destroy_sq(fp->fdev, q->hw_qid);
  621. fun_res_destroy(fp->fdev, FUN_ADMIN_OP_ETH, 0, q->ethid);
  622. if (q->irq) {
  623. q->irq->txq = NULL;
  624. fun_txq_purge(q);
  625. } else {
  626. fun_xdpq_purge(q);
  627. }
  628. q->init_state = FUN_QSTATE_INIT_SW;
  629. }
  630. /* Create or advance a Tx queue, allocating all the host and device resources
  631. * needed to reach the target state.
  632. */
  633. int funeth_txq_create(struct net_device *dev, unsigned int qidx,
  634. unsigned int ndesc, struct fun_irq *irq, int state,
  635. struct funeth_txq **qp)
  636. {
  637. struct funeth_txq *q = *qp;
  638. int err;
  639. if (!q)
  640. q = fun_txq_create_sw(dev, qidx, ndesc, irq);
  641. if (!q)
  642. return -ENOMEM;
  643. if (q->init_state >= state)
  644. goto out;
  645. err = fun_txq_create_dev(q, irq);
  646. if (err) {
  647. if (!*qp)
  648. fun_txq_free_sw(q);
  649. return err;
  650. }
  651. out:
  652. *qp = q;
  653. return 0;
  654. }
  655. /* Free Tx queue resources until it reaches the target state.
  656. * The queue must be already disconnected from the stack.
  657. */
  658. struct funeth_txq *funeth_txq_free(struct funeth_txq *q, int state)
  659. {
  660. if (state < FUN_QSTATE_INIT_FULL)
  661. fun_txq_free_dev(q);
  662. if (state == FUN_QSTATE_DESTROYED) {
  663. fun_txq_free_sw(q);
  664. q = NULL;
  665. }
  666. return q;
  667. }