gve_tx.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /* Google virtual Ethernet (gve) driver
  3. *
  4. * Copyright (C) 2015-2021 Google, Inc.
  5. */
  6. #include "gve.h"
  7. #include "gve_adminq.h"
  8. #include "gve_utils.h"
  9. #include <linux/ip.h>
  10. #include <linux/tcp.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/skbuff.h>
  13. #include <net/xdp_sock_drv.h>
  14. static inline void gve_tx_put_doorbell(struct gve_priv *priv,
  15. struct gve_queue_resources *q_resources,
  16. u32 val)
  17. {
  18. iowrite32be(val, &priv->db_bar2[be32_to_cpu(q_resources->db_index)]);
  19. }
  20. void gve_xdp_tx_flush(struct gve_priv *priv, u32 xdp_qid)
  21. {
  22. u32 tx_qid = gve_xdp_tx_queue_id(priv, xdp_qid);
  23. struct gve_tx_ring *tx = &priv->tx[tx_qid];
  24. gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
  25. }
  26. /* gvnic can only transmit from a Registered Segment.
  27. * We copy skb payloads into the registered segment before writing Tx
  28. * descriptors and ringing the Tx doorbell.
  29. *
  30. * gve_tx_fifo_* manages the Registered Segment as a FIFO - clients must
  31. * free allocations in the order they were allocated.
  32. */
  33. static int gve_tx_fifo_init(struct gve_priv *priv, struct gve_tx_fifo *fifo)
  34. {
  35. fifo->base = vmap(fifo->qpl->pages, fifo->qpl->num_entries, VM_MAP,
  36. PAGE_KERNEL);
  37. if (unlikely(!fifo->base)) {
  38. netif_err(priv, drv, priv->dev, "Failed to vmap fifo, qpl_id = %d\n",
  39. fifo->qpl->id);
  40. return -ENOMEM;
  41. }
  42. fifo->size = fifo->qpl->num_entries * PAGE_SIZE;
  43. atomic_set(&fifo->available, fifo->size);
  44. fifo->head = 0;
  45. return 0;
  46. }
  47. static void gve_tx_fifo_release(struct gve_priv *priv, struct gve_tx_fifo *fifo)
  48. {
  49. WARN(atomic_read(&fifo->available) != fifo->size,
  50. "Releasing non-empty fifo");
  51. vunmap(fifo->base);
  52. }
  53. static int gve_tx_fifo_pad_alloc_one_frag(struct gve_tx_fifo *fifo,
  54. size_t bytes)
  55. {
  56. return (fifo->head + bytes < fifo->size) ? 0 : fifo->size - fifo->head;
  57. }
  58. static bool gve_tx_fifo_can_alloc(struct gve_tx_fifo *fifo, size_t bytes)
  59. {
  60. return (atomic_read(&fifo->available) <= bytes) ? false : true;
  61. }
  62. /* gve_tx_alloc_fifo - Allocate fragment(s) from Tx FIFO
  63. * @fifo: FIFO to allocate from
  64. * @bytes: Allocation size
  65. * @iov: Scatter-gather elements to fill with allocation fragment base/len
  66. *
  67. * Returns number of valid elements in iov[] or negative on error.
  68. *
  69. * Allocations from a given FIFO must be externally synchronized but concurrent
  70. * allocation and frees are allowed.
  71. */
  72. static int gve_tx_alloc_fifo(struct gve_tx_fifo *fifo, size_t bytes,
  73. struct gve_tx_iovec iov[2])
  74. {
  75. size_t overflow, padding;
  76. u32 aligned_head;
  77. int nfrags = 0;
  78. if (!bytes)
  79. return 0;
  80. /* This check happens before we know how much padding is needed to
  81. * align to a cacheline boundary for the payload, but that is fine,
  82. * because the FIFO head always start aligned, and the FIFO's boundaries
  83. * are aligned, so if there is space for the data, there is space for
  84. * the padding to the next alignment.
  85. */
  86. WARN(!gve_tx_fifo_can_alloc(fifo, bytes),
  87. "Reached %s when there's not enough space in the fifo", __func__);
  88. nfrags++;
  89. iov[0].iov_offset = fifo->head;
  90. iov[0].iov_len = bytes;
  91. fifo->head += bytes;
  92. if (fifo->head > fifo->size) {
  93. /* If the allocation did not fit in the tail fragment of the
  94. * FIFO, also use the head fragment.
  95. */
  96. nfrags++;
  97. overflow = fifo->head - fifo->size;
  98. iov[0].iov_len -= overflow;
  99. iov[1].iov_offset = 0; /* Start of fifo*/
  100. iov[1].iov_len = overflow;
  101. fifo->head = overflow;
  102. }
  103. /* Re-align to a cacheline boundary */
  104. aligned_head = L1_CACHE_ALIGN(fifo->head);
  105. padding = aligned_head - fifo->head;
  106. iov[nfrags - 1].iov_padding = padding;
  107. atomic_sub(bytes + padding, &fifo->available);
  108. fifo->head = aligned_head;
  109. if (fifo->head == fifo->size)
  110. fifo->head = 0;
  111. return nfrags;
  112. }
  113. /* gve_tx_free_fifo - Return space to Tx FIFO
  114. * @fifo: FIFO to return fragments to
  115. * @bytes: Bytes to free
  116. */
  117. static void gve_tx_free_fifo(struct gve_tx_fifo *fifo, size_t bytes)
  118. {
  119. atomic_add(bytes, &fifo->available);
  120. }
  121. static size_t gve_tx_clear_buffer_state(struct gve_tx_buffer_state *info)
  122. {
  123. size_t space_freed = 0;
  124. int i;
  125. for (i = 0; i < ARRAY_SIZE(info->iov); i++) {
  126. space_freed += info->iov[i].iov_len + info->iov[i].iov_padding;
  127. info->iov[i].iov_len = 0;
  128. info->iov[i].iov_padding = 0;
  129. }
  130. return space_freed;
  131. }
  132. static int gve_clean_xdp_done(struct gve_priv *priv, struct gve_tx_ring *tx,
  133. u32 to_do)
  134. {
  135. struct gve_tx_buffer_state *info;
  136. u64 pkts = 0, bytes = 0;
  137. size_t space_freed = 0;
  138. u32 xsk_complete = 0;
  139. u32 idx;
  140. int i;
  141. for (i = 0; i < to_do; i++) {
  142. idx = tx->done & tx->mask;
  143. info = &tx->info[idx];
  144. tx->done++;
  145. if (unlikely(!info->xdp.size))
  146. continue;
  147. bytes += info->xdp.size;
  148. pkts++;
  149. xsk_complete += info->xdp.is_xsk;
  150. info->xdp.size = 0;
  151. if (info->xdp_frame) {
  152. xdp_return_frame(info->xdp_frame);
  153. info->xdp_frame = NULL;
  154. }
  155. space_freed += gve_tx_clear_buffer_state(info);
  156. }
  157. gve_tx_free_fifo(&tx->tx_fifo, space_freed);
  158. if (xsk_complete > 0 && tx->xsk_pool)
  159. xsk_tx_completed(tx->xsk_pool, xsk_complete);
  160. u64_stats_update_begin(&tx->statss);
  161. tx->bytes_done += bytes;
  162. tx->pkt_done += pkts;
  163. u64_stats_update_end(&tx->statss);
  164. return pkts;
  165. }
  166. static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
  167. u32 to_do, bool try_to_wake);
  168. void gve_tx_stop_ring_gqi(struct gve_priv *priv, int idx)
  169. {
  170. int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
  171. struct gve_tx_ring *tx = &priv->tx[idx];
  172. if (!gve_tx_was_added_to_block(priv, idx))
  173. return;
  174. gve_remove_napi(priv, ntfy_idx);
  175. if (tx->q_num < priv->tx_cfg.num_queues)
  176. gve_clean_tx_done(priv, tx, priv->tx_desc_cnt, false);
  177. else
  178. gve_clean_xdp_done(priv, tx, priv->tx_desc_cnt);
  179. netdev_tx_reset_queue(tx->netdev_txq);
  180. gve_tx_remove_from_block(priv, idx);
  181. }
  182. static void gve_tx_free_ring_gqi(struct gve_priv *priv, struct gve_tx_ring *tx,
  183. struct gve_tx_alloc_rings_cfg *cfg)
  184. {
  185. struct device *hdev = &priv->pdev->dev;
  186. int idx = tx->q_num;
  187. size_t bytes;
  188. u32 qpl_id;
  189. u32 slots;
  190. slots = tx->mask + 1;
  191. dma_free_coherent(hdev, sizeof(*tx->q_resources),
  192. tx->q_resources, tx->q_resources_bus);
  193. tx->q_resources = NULL;
  194. if (tx->tx_fifo.qpl) {
  195. if (tx->tx_fifo.base)
  196. gve_tx_fifo_release(priv, &tx->tx_fifo);
  197. qpl_id = gve_tx_qpl_id(priv, tx->q_num);
  198. gve_free_queue_page_list(priv, tx->tx_fifo.qpl, qpl_id);
  199. tx->tx_fifo.qpl = NULL;
  200. }
  201. bytes = sizeof(*tx->desc) * slots;
  202. dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
  203. tx->desc = NULL;
  204. vfree(tx->info);
  205. tx->info = NULL;
  206. netif_dbg(priv, drv, priv->dev, "freed tx queue %d\n", idx);
  207. }
  208. void gve_tx_start_ring_gqi(struct gve_priv *priv, int idx)
  209. {
  210. int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
  211. struct gve_tx_ring *tx = &priv->tx[idx];
  212. gve_tx_add_to_block(priv, idx);
  213. tx->netdev_txq = netdev_get_tx_queue(priv->dev, idx);
  214. gve_add_napi(priv, ntfy_idx, gve_napi_poll);
  215. }
  216. static int gve_tx_alloc_ring_gqi(struct gve_priv *priv,
  217. struct gve_tx_alloc_rings_cfg *cfg,
  218. struct gve_tx_ring *tx,
  219. int idx)
  220. {
  221. struct device *hdev = &priv->pdev->dev;
  222. int qpl_page_cnt;
  223. u32 qpl_id = 0;
  224. size_t bytes;
  225. /* Make sure everything is zeroed to start */
  226. memset(tx, 0, sizeof(*tx));
  227. spin_lock_init(&tx->clean_lock);
  228. spin_lock_init(&tx->xdp_lock);
  229. tx->q_num = idx;
  230. tx->mask = cfg->ring_size - 1;
  231. /* alloc metadata */
  232. tx->info = vcalloc(cfg->ring_size, sizeof(*tx->info));
  233. if (!tx->info)
  234. return -ENOMEM;
  235. /* alloc tx queue */
  236. bytes = sizeof(*tx->desc) * cfg->ring_size;
  237. tx->desc = dma_alloc_coherent(hdev, bytes, &tx->bus, GFP_KERNEL);
  238. if (!tx->desc)
  239. goto abort_with_info;
  240. tx->raw_addressing = cfg->raw_addressing;
  241. tx->dev = hdev;
  242. if (!tx->raw_addressing) {
  243. qpl_id = gve_tx_qpl_id(priv, tx->q_num);
  244. qpl_page_cnt = priv->tx_pages_per_qpl;
  245. tx->tx_fifo.qpl = gve_alloc_queue_page_list(priv, qpl_id,
  246. qpl_page_cnt);
  247. if (!tx->tx_fifo.qpl)
  248. goto abort_with_desc;
  249. /* map Tx FIFO */
  250. if (gve_tx_fifo_init(priv, &tx->tx_fifo))
  251. goto abort_with_qpl;
  252. }
  253. tx->q_resources =
  254. dma_alloc_coherent(hdev,
  255. sizeof(*tx->q_resources),
  256. &tx->q_resources_bus,
  257. GFP_KERNEL);
  258. if (!tx->q_resources)
  259. goto abort_with_fifo;
  260. return 0;
  261. abort_with_fifo:
  262. if (!tx->raw_addressing)
  263. gve_tx_fifo_release(priv, &tx->tx_fifo);
  264. abort_with_qpl:
  265. if (!tx->raw_addressing) {
  266. gve_free_queue_page_list(priv, tx->tx_fifo.qpl, qpl_id);
  267. tx->tx_fifo.qpl = NULL;
  268. }
  269. abort_with_desc:
  270. dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
  271. tx->desc = NULL;
  272. abort_with_info:
  273. vfree(tx->info);
  274. tx->info = NULL;
  275. return -ENOMEM;
  276. }
  277. int gve_tx_alloc_rings_gqi(struct gve_priv *priv,
  278. struct gve_tx_alloc_rings_cfg *cfg)
  279. {
  280. struct gve_tx_ring *tx = cfg->tx;
  281. int err = 0;
  282. int i, j;
  283. if (cfg->start_idx + cfg->num_rings > cfg->qcfg->max_queues) {
  284. netif_err(priv, drv, priv->dev,
  285. "Cannot alloc more than the max num of Tx rings\n");
  286. return -EINVAL;
  287. }
  288. if (cfg->start_idx == 0) {
  289. tx = kvcalloc(cfg->qcfg->max_queues, sizeof(struct gve_tx_ring),
  290. GFP_KERNEL);
  291. if (!tx)
  292. return -ENOMEM;
  293. } else if (!tx) {
  294. netif_err(priv, drv, priv->dev,
  295. "Cannot alloc tx rings from a nonzero start idx without tx array\n");
  296. return -EINVAL;
  297. }
  298. for (i = cfg->start_idx; i < cfg->start_idx + cfg->num_rings; i++) {
  299. err = gve_tx_alloc_ring_gqi(priv, cfg, &tx[i], i);
  300. if (err) {
  301. netif_err(priv, drv, priv->dev,
  302. "Failed to alloc tx ring=%d: err=%d\n",
  303. i, err);
  304. goto cleanup;
  305. }
  306. }
  307. cfg->tx = tx;
  308. return 0;
  309. cleanup:
  310. for (j = 0; j < i; j++)
  311. gve_tx_free_ring_gqi(priv, &tx[j], cfg);
  312. if (cfg->start_idx == 0)
  313. kvfree(tx);
  314. return err;
  315. }
  316. void gve_tx_free_rings_gqi(struct gve_priv *priv,
  317. struct gve_tx_alloc_rings_cfg *cfg)
  318. {
  319. struct gve_tx_ring *tx = cfg->tx;
  320. int i;
  321. if (!tx)
  322. return;
  323. for (i = cfg->start_idx; i < cfg->start_idx + cfg->num_rings; i++)
  324. gve_tx_free_ring_gqi(priv, &tx[i], cfg);
  325. if (cfg->start_idx == 0) {
  326. kvfree(tx);
  327. cfg->tx = NULL;
  328. }
  329. }
  330. /* gve_tx_avail - Calculates the number of slots available in the ring
  331. * @tx: tx ring to check
  332. *
  333. * Returns the number of slots available
  334. *
  335. * The capacity of the queue is mask + 1. We don't need to reserve an entry.
  336. **/
  337. static inline u32 gve_tx_avail(struct gve_tx_ring *tx)
  338. {
  339. return tx->mask + 1 - (tx->req - tx->done);
  340. }
  341. static inline int gve_skb_fifo_bytes_required(struct gve_tx_ring *tx,
  342. struct sk_buff *skb)
  343. {
  344. int pad_bytes, align_hdr_pad;
  345. int bytes;
  346. int hlen;
  347. hlen = skb_is_gso(skb) ? skb_checksum_start_offset(skb) + tcp_hdrlen(skb) :
  348. min_t(int, GVE_GQ_TX_MIN_PKT_DESC_BYTES, skb->len);
  349. pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo,
  350. hlen);
  351. /* We need to take into account the header alignment padding. */
  352. align_hdr_pad = L1_CACHE_ALIGN(hlen) - hlen;
  353. bytes = align_hdr_pad + pad_bytes + skb->len;
  354. return bytes;
  355. }
  356. /* The most descriptors we could need is MAX_SKB_FRAGS + 4 :
  357. * 1 for each skb frag
  358. * 1 for the skb linear portion
  359. * 1 for when tcp hdr needs to be in separate descriptor
  360. * 1 if the payload wraps to the beginning of the FIFO
  361. * 1 for metadata descriptor
  362. */
  363. #define MAX_TX_DESC_NEEDED (MAX_SKB_FRAGS + 4)
  364. static void gve_tx_unmap_buf(struct device *dev, struct gve_tx_buffer_state *info)
  365. {
  366. if (info->skb) {
  367. dma_unmap_single(dev, dma_unmap_addr(info, dma),
  368. dma_unmap_len(info, len),
  369. DMA_TO_DEVICE);
  370. dma_unmap_len_set(info, len, 0);
  371. } else {
  372. dma_unmap_page(dev, dma_unmap_addr(info, dma),
  373. dma_unmap_len(info, len),
  374. DMA_TO_DEVICE);
  375. dma_unmap_len_set(info, len, 0);
  376. }
  377. }
  378. /* Check if sufficient resources (descriptor ring space, FIFO space) are
  379. * available to transmit the given number of bytes.
  380. */
  381. static inline bool gve_can_tx(struct gve_tx_ring *tx, int bytes_required)
  382. {
  383. bool can_alloc = true;
  384. if (!tx->raw_addressing)
  385. can_alloc = gve_tx_fifo_can_alloc(&tx->tx_fifo, bytes_required);
  386. return (gve_tx_avail(tx) >= MAX_TX_DESC_NEEDED && can_alloc);
  387. }
  388. static_assert(NAPI_POLL_WEIGHT >= MAX_TX_DESC_NEEDED);
  389. /* Stops the queue if the skb cannot be transmitted. */
  390. static int gve_maybe_stop_tx(struct gve_priv *priv, struct gve_tx_ring *tx,
  391. struct sk_buff *skb)
  392. {
  393. int bytes_required = 0;
  394. u32 nic_done;
  395. u32 to_do;
  396. int ret;
  397. if (!tx->raw_addressing)
  398. bytes_required = gve_skb_fifo_bytes_required(tx, skb);
  399. if (likely(gve_can_tx(tx, bytes_required)))
  400. return 0;
  401. ret = -EBUSY;
  402. spin_lock(&tx->clean_lock);
  403. nic_done = gve_tx_load_event_counter(priv, tx);
  404. to_do = nic_done - tx->done;
  405. /* Only try to clean if there is hope for TX */
  406. if (to_do + gve_tx_avail(tx) >= MAX_TX_DESC_NEEDED) {
  407. if (to_do > 0) {
  408. to_do = min_t(u32, to_do, NAPI_POLL_WEIGHT);
  409. gve_clean_tx_done(priv, tx, to_do, false);
  410. }
  411. if (likely(gve_can_tx(tx, bytes_required)))
  412. ret = 0;
  413. }
  414. if (ret) {
  415. /* No space, so stop the queue */
  416. tx->stop_queue++;
  417. netif_tx_stop_queue(tx->netdev_txq);
  418. }
  419. spin_unlock(&tx->clean_lock);
  420. return ret;
  421. }
  422. static void gve_tx_fill_pkt_desc(union gve_tx_desc *pkt_desc,
  423. u16 csum_offset, u8 ip_summed, bool is_gso,
  424. int l4_hdr_offset, u32 desc_cnt,
  425. u16 hlen, u64 addr, u16 pkt_len)
  426. {
  427. /* l4_hdr_offset and csum_offset are in units of 16-bit words */
  428. if (is_gso) {
  429. pkt_desc->pkt.type_flags = GVE_TXD_TSO | GVE_TXF_L4CSUM;
  430. pkt_desc->pkt.l4_csum_offset = csum_offset >> 1;
  431. pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
  432. } else if (likely(ip_summed == CHECKSUM_PARTIAL)) {
  433. pkt_desc->pkt.type_flags = GVE_TXD_STD | GVE_TXF_L4CSUM;
  434. pkt_desc->pkt.l4_csum_offset = csum_offset >> 1;
  435. pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
  436. } else {
  437. pkt_desc->pkt.type_flags = GVE_TXD_STD;
  438. pkt_desc->pkt.l4_csum_offset = 0;
  439. pkt_desc->pkt.l4_hdr_offset = 0;
  440. }
  441. pkt_desc->pkt.desc_cnt = desc_cnt;
  442. pkt_desc->pkt.len = cpu_to_be16(pkt_len);
  443. pkt_desc->pkt.seg_len = cpu_to_be16(hlen);
  444. pkt_desc->pkt.seg_addr = cpu_to_be64(addr);
  445. }
  446. static void gve_tx_fill_mtd_desc(union gve_tx_desc *mtd_desc,
  447. struct sk_buff *skb)
  448. {
  449. BUILD_BUG_ON(sizeof(mtd_desc->mtd) != sizeof(mtd_desc->pkt));
  450. mtd_desc->mtd.type_flags = GVE_TXD_MTD | GVE_MTD_SUBTYPE_PATH;
  451. mtd_desc->mtd.path_state = GVE_MTD_PATH_STATE_DEFAULT |
  452. GVE_MTD_PATH_HASH_L4;
  453. mtd_desc->mtd.path_hash = cpu_to_be32(skb->hash);
  454. mtd_desc->mtd.reserved0 = 0;
  455. mtd_desc->mtd.reserved1 = 0;
  456. }
  457. static void gve_tx_fill_seg_desc(union gve_tx_desc *seg_desc,
  458. u16 l3_offset, u16 gso_size,
  459. bool is_gso_v6, bool is_gso,
  460. u16 len, u64 addr)
  461. {
  462. seg_desc->seg.type_flags = GVE_TXD_SEG;
  463. if (is_gso) {
  464. if (is_gso_v6)
  465. seg_desc->seg.type_flags |= GVE_TXSF_IPV6;
  466. seg_desc->seg.l3_offset = l3_offset >> 1;
  467. seg_desc->seg.mss = cpu_to_be16(gso_size);
  468. }
  469. seg_desc->seg.seg_len = cpu_to_be16(len);
  470. seg_desc->seg.seg_addr = cpu_to_be64(addr);
  471. }
  472. static void gve_dma_sync_for_device(struct device *dev, dma_addr_t *page_buses,
  473. u64 iov_offset, u64 iov_len)
  474. {
  475. u64 last_page = (iov_offset + iov_len - 1) / PAGE_SIZE;
  476. u64 first_page = iov_offset / PAGE_SIZE;
  477. u64 page;
  478. for (page = first_page; page <= last_page; page++)
  479. dma_sync_single_for_device(dev, page_buses[page], PAGE_SIZE, DMA_TO_DEVICE);
  480. }
  481. static int gve_tx_add_skb_copy(struct gve_priv *priv, struct gve_tx_ring *tx, struct sk_buff *skb)
  482. {
  483. int pad_bytes, hlen, hdr_nfrags, payload_nfrags, l4_hdr_offset;
  484. union gve_tx_desc *pkt_desc, *seg_desc;
  485. struct gve_tx_buffer_state *info;
  486. int mtd_desc_nr = !!skb->l4_hash;
  487. bool is_gso = skb_is_gso(skb);
  488. u32 idx = tx->req & tx->mask;
  489. int payload_iov = 2;
  490. int copy_offset;
  491. u32 next_idx;
  492. int i;
  493. info = &tx->info[idx];
  494. pkt_desc = &tx->desc[idx];
  495. l4_hdr_offset = skb_checksum_start_offset(skb);
  496. /* If the skb is gso, then we want the tcp header alone in the first segment
  497. * otherwise we want the minimum required by the gVNIC spec.
  498. */
  499. hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) :
  500. min_t(int, GVE_GQ_TX_MIN_PKT_DESC_BYTES, skb->len);
  501. info->skb = skb;
  502. /* We don't want to split the header, so if necessary, pad to the end
  503. * of the fifo and then put the header at the beginning of the fifo.
  504. */
  505. pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo, hlen);
  506. hdr_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, hlen + pad_bytes,
  507. &info->iov[0]);
  508. WARN(!hdr_nfrags, "hdr_nfrags should never be 0!");
  509. payload_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, skb->len - hlen,
  510. &info->iov[payload_iov]);
  511. gve_tx_fill_pkt_desc(pkt_desc, skb->csum_offset, skb->ip_summed,
  512. is_gso, l4_hdr_offset,
  513. 1 + mtd_desc_nr + payload_nfrags, hlen,
  514. info->iov[hdr_nfrags - 1].iov_offset, skb->len);
  515. skb_copy_bits(skb, 0,
  516. tx->tx_fifo.base + info->iov[hdr_nfrags - 1].iov_offset,
  517. hlen);
  518. gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
  519. info->iov[hdr_nfrags - 1].iov_offset,
  520. info->iov[hdr_nfrags - 1].iov_len);
  521. copy_offset = hlen;
  522. if (mtd_desc_nr) {
  523. next_idx = (tx->req + 1) & tx->mask;
  524. gve_tx_fill_mtd_desc(&tx->desc[next_idx], skb);
  525. }
  526. for (i = payload_iov; i < payload_nfrags + payload_iov; i++) {
  527. next_idx = (tx->req + 1 + mtd_desc_nr + i - payload_iov) & tx->mask;
  528. seg_desc = &tx->desc[next_idx];
  529. gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
  530. skb_shinfo(skb)->gso_size,
  531. skb_is_gso_v6(skb), is_gso,
  532. info->iov[i].iov_len,
  533. info->iov[i].iov_offset);
  534. skb_copy_bits(skb, copy_offset,
  535. tx->tx_fifo.base + info->iov[i].iov_offset,
  536. info->iov[i].iov_len);
  537. gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
  538. info->iov[i].iov_offset,
  539. info->iov[i].iov_len);
  540. copy_offset += info->iov[i].iov_len;
  541. }
  542. return 1 + mtd_desc_nr + payload_nfrags;
  543. }
  544. static int gve_tx_add_skb_no_copy(struct gve_priv *priv, struct gve_tx_ring *tx,
  545. struct sk_buff *skb)
  546. {
  547. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  548. int hlen, num_descriptors, l4_hdr_offset;
  549. union gve_tx_desc *pkt_desc, *mtd_desc, *seg_desc;
  550. struct gve_tx_buffer_state *info;
  551. int mtd_desc_nr = !!skb->l4_hash;
  552. bool is_gso = skb_is_gso(skb);
  553. u32 idx = tx->req & tx->mask;
  554. u64 addr;
  555. u32 len;
  556. int i;
  557. info = &tx->info[idx];
  558. pkt_desc = &tx->desc[idx];
  559. l4_hdr_offset = skb_checksum_start_offset(skb);
  560. /* If the skb is gso, then we want only up to the tcp header in the first segment
  561. * to efficiently replicate on each segment otherwise we want the linear portion
  562. * of the skb (which will contain the checksum because skb->csum_start and
  563. * skb->csum_offset are given relative to skb->head) in the first segment.
  564. */
  565. hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) : skb_headlen(skb);
  566. len = skb_headlen(skb);
  567. info->skb = skb;
  568. addr = dma_map_single(tx->dev, skb->data, len, DMA_TO_DEVICE);
  569. if (unlikely(dma_mapping_error(tx->dev, addr))) {
  570. tx->dma_mapping_error++;
  571. goto drop;
  572. }
  573. dma_unmap_len_set(info, len, len);
  574. dma_unmap_addr_set(info, dma, addr);
  575. num_descriptors = 1 + shinfo->nr_frags;
  576. if (hlen < len)
  577. num_descriptors++;
  578. if (mtd_desc_nr)
  579. num_descriptors++;
  580. gve_tx_fill_pkt_desc(pkt_desc, skb->csum_offset, skb->ip_summed,
  581. is_gso, l4_hdr_offset,
  582. num_descriptors, hlen, addr, skb->len);
  583. if (mtd_desc_nr) {
  584. idx = (idx + 1) & tx->mask;
  585. mtd_desc = &tx->desc[idx];
  586. gve_tx_fill_mtd_desc(mtd_desc, skb);
  587. }
  588. if (hlen < len) {
  589. /* For gso the rest of the linear portion of the skb needs to
  590. * be in its own descriptor.
  591. */
  592. len -= hlen;
  593. addr += hlen;
  594. idx = (idx + 1) & tx->mask;
  595. seg_desc = &tx->desc[idx];
  596. gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
  597. skb_shinfo(skb)->gso_size,
  598. skb_is_gso_v6(skb), is_gso, len, addr);
  599. }
  600. for (i = 0; i < shinfo->nr_frags; i++) {
  601. const skb_frag_t *frag = &shinfo->frags[i];
  602. idx = (idx + 1) & tx->mask;
  603. seg_desc = &tx->desc[idx];
  604. len = skb_frag_size(frag);
  605. addr = skb_frag_dma_map(tx->dev, frag, 0, len, DMA_TO_DEVICE);
  606. if (unlikely(dma_mapping_error(tx->dev, addr))) {
  607. tx->dma_mapping_error++;
  608. goto unmap_drop;
  609. }
  610. tx->info[idx].skb = NULL;
  611. dma_unmap_len_set(&tx->info[idx], len, len);
  612. dma_unmap_addr_set(&tx->info[idx], dma, addr);
  613. gve_tx_fill_seg_desc(seg_desc, skb_network_offset(skb),
  614. skb_shinfo(skb)->gso_size,
  615. skb_is_gso_v6(skb), is_gso, len, addr);
  616. }
  617. return num_descriptors;
  618. unmap_drop:
  619. i += num_descriptors - shinfo->nr_frags;
  620. while (i--) {
  621. /* Skip metadata descriptor, if set */
  622. if (i == 1 && mtd_desc_nr == 1)
  623. continue;
  624. idx--;
  625. gve_tx_unmap_buf(tx->dev, &tx->info[idx & tx->mask]);
  626. }
  627. drop:
  628. tx->dropped_pkt++;
  629. return 0;
  630. }
  631. netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev)
  632. {
  633. struct gve_priv *priv = netdev_priv(dev);
  634. struct gve_tx_ring *tx;
  635. int nsegs;
  636. WARN(skb_get_queue_mapping(skb) >= priv->tx_cfg.num_queues,
  637. "skb queue index out of range");
  638. tx = &priv->tx[skb_get_queue_mapping(skb)];
  639. if (unlikely(gve_maybe_stop_tx(priv, tx, skb))) {
  640. /* We need to ring the txq doorbell -- we have stopped the Tx
  641. * queue for want of resources, but prior calls to gve_tx()
  642. * may have added descriptors without ringing the doorbell.
  643. */
  644. gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
  645. return NETDEV_TX_BUSY;
  646. }
  647. if (tx->raw_addressing)
  648. nsegs = gve_tx_add_skb_no_copy(priv, tx, skb);
  649. else
  650. nsegs = gve_tx_add_skb_copy(priv, tx, skb);
  651. /* If the packet is getting sent, we need to update the skb */
  652. if (nsegs) {
  653. netdev_tx_sent_queue(tx->netdev_txq, skb->len);
  654. skb_tx_timestamp(skb);
  655. tx->req += nsegs;
  656. } else {
  657. dev_kfree_skb_any(skb);
  658. }
  659. if (!netif_xmit_stopped(tx->netdev_txq) && netdev_xmit_more())
  660. return NETDEV_TX_OK;
  661. /* Give packets to NIC. Even if this packet failed to send the doorbell
  662. * might need to be rung because of xmit_more.
  663. */
  664. gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
  665. return NETDEV_TX_OK;
  666. }
  667. static int gve_tx_fill_xdp(struct gve_priv *priv, struct gve_tx_ring *tx,
  668. void *data, int len, void *frame_p, bool is_xsk)
  669. {
  670. int pad, nfrags, ndescs, iovi, offset;
  671. struct gve_tx_buffer_state *info;
  672. u32 reqi = tx->req;
  673. pad = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo, len);
  674. if (pad >= GVE_GQ_TX_MIN_PKT_DESC_BYTES)
  675. pad = 0;
  676. info = &tx->info[reqi & tx->mask];
  677. info->xdp_frame = frame_p;
  678. info->xdp.size = len;
  679. info->xdp.is_xsk = is_xsk;
  680. nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, pad + len,
  681. &info->iov[0]);
  682. iovi = pad > 0;
  683. ndescs = nfrags - iovi;
  684. offset = 0;
  685. while (iovi < nfrags) {
  686. if (!offset)
  687. gve_tx_fill_pkt_desc(&tx->desc[reqi & tx->mask], 0,
  688. CHECKSUM_NONE, false, 0, ndescs,
  689. info->iov[iovi].iov_len,
  690. info->iov[iovi].iov_offset, len);
  691. else
  692. gve_tx_fill_seg_desc(&tx->desc[reqi & tx->mask],
  693. 0, 0, false, false,
  694. info->iov[iovi].iov_len,
  695. info->iov[iovi].iov_offset);
  696. memcpy(tx->tx_fifo.base + info->iov[iovi].iov_offset,
  697. data + offset, info->iov[iovi].iov_len);
  698. gve_dma_sync_for_device(&priv->pdev->dev,
  699. tx->tx_fifo.qpl->page_buses,
  700. info->iov[iovi].iov_offset,
  701. info->iov[iovi].iov_len);
  702. offset += info->iov[iovi].iov_len;
  703. iovi++;
  704. reqi++;
  705. }
  706. return ndescs;
  707. }
  708. int gve_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
  709. u32 flags)
  710. {
  711. struct gve_priv *priv = netdev_priv(dev);
  712. struct gve_tx_ring *tx;
  713. int i, err = 0, qid;
  714. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK) || !priv->xdp_prog)
  715. return -EINVAL;
  716. if (!gve_get_napi_enabled(priv))
  717. return -ENETDOWN;
  718. qid = gve_xdp_tx_queue_id(priv,
  719. smp_processor_id() % priv->num_xdp_queues);
  720. tx = &priv->tx[qid];
  721. spin_lock(&tx->xdp_lock);
  722. for (i = 0; i < n; i++) {
  723. err = gve_xdp_xmit_one(priv, tx, frames[i]->data,
  724. frames[i]->len, frames[i]);
  725. if (err)
  726. break;
  727. }
  728. if (flags & XDP_XMIT_FLUSH)
  729. gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
  730. spin_unlock(&tx->xdp_lock);
  731. u64_stats_update_begin(&tx->statss);
  732. tx->xdp_xmit += n;
  733. tx->xdp_xmit_errors += n - i;
  734. u64_stats_update_end(&tx->statss);
  735. return i ? i : err;
  736. }
  737. int gve_xdp_xmit_one(struct gve_priv *priv, struct gve_tx_ring *tx,
  738. void *data, int len, void *frame_p)
  739. {
  740. int nsegs;
  741. if (!gve_can_tx(tx, len + GVE_GQ_TX_MIN_PKT_DESC_BYTES - 1))
  742. return -EBUSY;
  743. nsegs = gve_tx_fill_xdp(priv, tx, data, len, frame_p, false);
  744. tx->req += nsegs;
  745. return 0;
  746. }
  747. #define GVE_TX_START_THRESH 4096
  748. static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
  749. u32 to_do, bool try_to_wake)
  750. {
  751. struct gve_tx_buffer_state *info;
  752. u64 pkts = 0, bytes = 0;
  753. size_t space_freed = 0;
  754. struct sk_buff *skb;
  755. u32 idx;
  756. int j;
  757. for (j = 0; j < to_do; j++) {
  758. idx = tx->done & tx->mask;
  759. netif_info(priv, tx_done, priv->dev,
  760. "[%d] %s: idx=%d (req=%u done=%u)\n",
  761. tx->q_num, __func__, idx, tx->req, tx->done);
  762. info = &tx->info[idx];
  763. skb = info->skb;
  764. /* Unmap the buffer */
  765. if (tx->raw_addressing)
  766. gve_tx_unmap_buf(tx->dev, info);
  767. tx->done++;
  768. /* Mark as free */
  769. if (skb) {
  770. info->skb = NULL;
  771. bytes += skb->len;
  772. pkts++;
  773. dev_consume_skb_any(skb);
  774. if (tx->raw_addressing)
  775. continue;
  776. space_freed += gve_tx_clear_buffer_state(info);
  777. }
  778. }
  779. if (!tx->raw_addressing)
  780. gve_tx_free_fifo(&tx->tx_fifo, space_freed);
  781. u64_stats_update_begin(&tx->statss);
  782. tx->bytes_done += bytes;
  783. tx->pkt_done += pkts;
  784. u64_stats_update_end(&tx->statss);
  785. netdev_tx_completed_queue(tx->netdev_txq, pkts, bytes);
  786. /* start the queue if we've stopped it */
  787. #ifndef CONFIG_BQL
  788. /* Make sure that the doorbells are synced */
  789. smp_mb();
  790. #endif
  791. if (try_to_wake && netif_tx_queue_stopped(tx->netdev_txq) &&
  792. likely(gve_can_tx(tx, GVE_TX_START_THRESH))) {
  793. tx->wake_queue++;
  794. netif_tx_wake_queue(tx->netdev_txq);
  795. }
  796. return pkts;
  797. }
  798. u32 gve_tx_load_event_counter(struct gve_priv *priv,
  799. struct gve_tx_ring *tx)
  800. {
  801. u32 counter_index = be32_to_cpu(tx->q_resources->counter_index);
  802. __be32 counter = READ_ONCE(priv->counter_array[counter_index]);
  803. return be32_to_cpu(counter);
  804. }
  805. static int gve_xsk_tx(struct gve_priv *priv, struct gve_tx_ring *tx,
  806. int budget)
  807. {
  808. struct xdp_desc desc;
  809. int sent = 0, nsegs;
  810. void *data;
  811. spin_lock(&tx->xdp_lock);
  812. while (sent < budget) {
  813. if (!gve_can_tx(tx, GVE_TX_START_THRESH))
  814. goto out;
  815. if (!xsk_tx_peek_desc(tx->xsk_pool, &desc)) {
  816. tx->xdp_xsk_done = tx->xdp_xsk_wakeup;
  817. goto out;
  818. }
  819. data = xsk_buff_raw_get_data(tx->xsk_pool, desc.addr);
  820. nsegs = gve_tx_fill_xdp(priv, tx, data, desc.len, NULL, true);
  821. tx->req += nsegs;
  822. sent++;
  823. }
  824. out:
  825. if (sent > 0) {
  826. gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
  827. xsk_tx_release(tx->xsk_pool);
  828. }
  829. spin_unlock(&tx->xdp_lock);
  830. return sent;
  831. }
  832. int gve_xsk_tx_poll(struct gve_notify_block *rx_block, int budget)
  833. {
  834. struct gve_rx_ring *rx = rx_block->rx;
  835. struct gve_priv *priv = rx->gve;
  836. struct gve_tx_ring *tx;
  837. int sent = 0;
  838. tx = &priv->tx[gve_xdp_tx_queue_id(priv, rx->q_num)];
  839. if (tx->xsk_pool) {
  840. sent = gve_xsk_tx(priv, tx, budget);
  841. u64_stats_update_begin(&tx->statss);
  842. tx->xdp_xsk_sent += sent;
  843. u64_stats_update_end(&tx->statss);
  844. if (xsk_uses_need_wakeup(tx->xsk_pool))
  845. xsk_set_tx_need_wakeup(tx->xsk_pool);
  846. }
  847. return sent;
  848. }
  849. bool gve_xdp_poll(struct gve_notify_block *block, int budget)
  850. {
  851. struct gve_priv *priv = block->priv;
  852. struct gve_tx_ring *tx = block->tx;
  853. u32 nic_done;
  854. u32 to_do;
  855. /* Find out how much work there is to be done */
  856. nic_done = gve_tx_load_event_counter(priv, tx);
  857. to_do = min_t(u32, (nic_done - tx->done), budget);
  858. gve_clean_xdp_done(priv, tx, to_do);
  859. /* If we still have work we want to repoll */
  860. return nic_done != tx->done;
  861. }
  862. bool gve_tx_poll(struct gve_notify_block *block, int budget)
  863. {
  864. struct gve_priv *priv = block->priv;
  865. struct gve_tx_ring *tx = block->tx;
  866. u32 nic_done;
  867. u32 to_do;
  868. /* If budget is 0, do all the work */
  869. if (budget == 0)
  870. budget = INT_MAX;
  871. /* In TX path, it may try to clean completed pkts in order to xmit,
  872. * to avoid cleaning conflict, use spin_lock(), it yields better
  873. * concurrency between xmit/clean than netif's lock.
  874. */
  875. spin_lock(&tx->clean_lock);
  876. /* Find out how much work there is to be done */
  877. nic_done = gve_tx_load_event_counter(priv, tx);
  878. to_do = min_t(u32, (nic_done - tx->done), budget);
  879. gve_clean_tx_done(priv, tx, to_do, true);
  880. spin_unlock(&tx->clean_lock);
  881. /* If we still have work we want to repoll */
  882. return nic_done != tx->done;
  883. }
  884. bool gve_tx_clean_pending(struct gve_priv *priv, struct gve_tx_ring *tx)
  885. {
  886. u32 nic_done = gve_tx_load_event_counter(priv, tx);
  887. return nic_done != tx->done;
  888. }