ice_txrx_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019, Intel Corporation. */
  3. #include <linux/filter.h>
  4. #include <linux/net/intel/libie/rx.h>
  5. #include "ice_txrx_lib.h"
  6. #include "ice_eswitch.h"
  7. #include "ice_lib.h"
  8. /**
  9. * ice_release_rx_desc - Store the new tail and head values
  10. * @rx_ring: ring to bump
  11. * @val: new head index
  12. */
  13. void ice_release_rx_desc(struct ice_rx_ring *rx_ring, u16 val)
  14. {
  15. u16 prev_ntu = rx_ring->next_to_use & ~0x7;
  16. rx_ring->next_to_use = val;
  17. /* update next to alloc since we have filled the ring */
  18. rx_ring->next_to_alloc = val;
  19. /* QRX_TAIL will be updated with any tail value, but hardware ignores
  20. * the lower 3 bits. This makes it so we only bump tail on meaningful
  21. * boundaries. Also, this allows us to bump tail on intervals of 8 up to
  22. * the budget depending on the current traffic load.
  23. */
  24. val &= ~0x7;
  25. if (prev_ntu != val) {
  26. /* Force memory writes to complete before letting h/w
  27. * know there are new descriptors to fetch. (Only
  28. * applicable for weak-ordered memory model archs,
  29. * such as IA-64).
  30. */
  31. wmb();
  32. writel(val, rx_ring->tail);
  33. }
  34. }
  35. /**
  36. * ice_get_rx_hash - get RX hash value from descriptor
  37. * @rx_desc: specific descriptor
  38. *
  39. * Returns hash, if present, 0 otherwise.
  40. */
  41. static u32 ice_get_rx_hash(const union ice_32b_rx_flex_desc *rx_desc)
  42. {
  43. const struct ice_32b_rx_flex_desc_nic *nic_mdid;
  44. if (unlikely(rx_desc->wb.rxdid != ICE_RXDID_FLEX_NIC))
  45. return 0;
  46. nic_mdid = (struct ice_32b_rx_flex_desc_nic *)rx_desc;
  47. return le32_to_cpu(nic_mdid->rss_hash);
  48. }
  49. /**
  50. * ice_rx_hash_to_skb - set the hash value in the skb
  51. * @rx_ring: descriptor ring
  52. * @rx_desc: specific descriptor
  53. * @skb: pointer to current skb
  54. * @rx_ptype: the ptype value from the descriptor
  55. */
  56. static void
  57. ice_rx_hash_to_skb(const struct ice_rx_ring *rx_ring,
  58. const union ice_32b_rx_flex_desc *rx_desc,
  59. struct sk_buff *skb, u16 rx_ptype)
  60. {
  61. struct libeth_rx_pt decoded;
  62. u32 hash;
  63. decoded = libie_rx_pt_parse(rx_ptype);
  64. if (!libeth_rx_pt_has_hash(rx_ring->netdev, decoded))
  65. return;
  66. hash = ice_get_rx_hash(rx_desc);
  67. if (likely(hash))
  68. libeth_rx_pt_set_hash(skb, hash, decoded);
  69. }
  70. /**
  71. * ice_rx_csum - Indicate in skb if checksum is good
  72. * @ring: the ring we care about
  73. * @skb: skb currently being received and modified
  74. * @rx_desc: the receive descriptor
  75. * @ptype: the packet type decoded by hardware
  76. *
  77. * skb->protocol must be set before this function is called
  78. */
  79. static void
  80. ice_rx_csum(struct ice_rx_ring *ring, struct sk_buff *skb,
  81. union ice_32b_rx_flex_desc *rx_desc, u16 ptype)
  82. {
  83. struct libeth_rx_pt decoded;
  84. u16 rx_status0, rx_status1;
  85. bool ipv4, ipv6;
  86. /* Start with CHECKSUM_NONE and by default csum_level = 0 */
  87. skb->ip_summed = CHECKSUM_NONE;
  88. decoded = libie_rx_pt_parse(ptype);
  89. if (!libeth_rx_pt_has_checksum(ring->netdev, decoded))
  90. return;
  91. rx_status0 = le16_to_cpu(rx_desc->wb.status_error0);
  92. rx_status1 = le16_to_cpu(rx_desc->wb.status_error1);
  93. /* check if HW has decoded the packet and checksum */
  94. if (!(rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_L3L4P_S)))
  95. return;
  96. ipv4 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV4;
  97. ipv6 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV6;
  98. if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EIPE_S)))) {
  99. ring->vsi->back->hw_rx_eipe_error++;
  100. return;
  101. }
  102. if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_IPE_S))))
  103. goto checksum_fail;
  104. if (ipv6 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_IPV6EXADD_S))))
  105. goto checksum_fail;
  106. /* check for L4 errors and handle packets that were not able to be
  107. * checksummed due to arrival speed
  108. */
  109. if (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_L4E_S))
  110. goto checksum_fail;
  111. /* check for outer UDP checksum error in tunneled packets */
  112. if ((rx_status1 & BIT(ICE_RX_FLEX_DESC_STATUS1_NAT_S)) &&
  113. (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EUDPE_S)))
  114. goto checksum_fail;
  115. /* If there is an outer header present that might contain a checksum
  116. * we need to bump the checksum level by 1 to reflect the fact that
  117. * we are indicating we validated the inner checksum.
  118. */
  119. if (decoded.tunnel_type >= LIBETH_RX_PT_TUNNEL_IP_GRENAT)
  120. skb->csum_level = 1;
  121. skb->ip_summed = CHECKSUM_UNNECESSARY;
  122. return;
  123. checksum_fail:
  124. ring->vsi->back->hw_csum_rx_error++;
  125. }
  126. /**
  127. * ice_ptp_rx_hwts_to_skb - Put RX timestamp into skb
  128. * @rx_ring: Ring to get the VSI info
  129. * @rx_desc: Receive descriptor
  130. * @skb: Particular skb to send timestamp with
  131. *
  132. * The timestamp is in ns, so we must convert the result first.
  133. */
  134. static void
  135. ice_ptp_rx_hwts_to_skb(struct ice_rx_ring *rx_ring,
  136. const union ice_32b_rx_flex_desc *rx_desc,
  137. struct sk_buff *skb)
  138. {
  139. u64 ts_ns = ice_ptp_get_rx_hwts(rx_desc, &rx_ring->pkt_ctx);
  140. skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ts_ns);
  141. }
  142. /**
  143. * ice_get_ptype - Read HW packet type from the descriptor
  144. * @rx_desc: RX descriptor
  145. */
  146. static u16 ice_get_ptype(const union ice_32b_rx_flex_desc *rx_desc)
  147. {
  148. return le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
  149. ICE_RX_FLEX_DESC_PTYPE_M;
  150. }
  151. /**
  152. * ice_process_skb_fields - Populate skb header fields from Rx descriptor
  153. * @rx_ring: Rx descriptor ring packet is being transacted on
  154. * @rx_desc: pointer to the EOP Rx descriptor
  155. * @skb: pointer to current skb being populated
  156. *
  157. * This function checks the ring, descriptor, and packet information in
  158. * order to populate the hash, checksum, VLAN, protocol, and
  159. * other fields within the skb.
  160. */
  161. void
  162. ice_process_skb_fields(struct ice_rx_ring *rx_ring,
  163. union ice_32b_rx_flex_desc *rx_desc,
  164. struct sk_buff *skb)
  165. {
  166. u16 ptype = ice_get_ptype(rx_desc);
  167. ice_rx_hash_to_skb(rx_ring, rx_desc, skb, ptype);
  168. /* modifies the skb - consumes the enet header */
  169. if (unlikely(rx_ring->flags & ICE_RX_FLAGS_MULTIDEV)) {
  170. struct net_device *netdev = ice_eswitch_get_target(rx_ring,
  171. rx_desc);
  172. if (ice_is_port_repr_netdev(netdev))
  173. ice_repr_inc_rx_stats(netdev, skb->len);
  174. skb->protocol = eth_type_trans(skb, netdev);
  175. } else {
  176. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  177. }
  178. ice_rx_csum(rx_ring, skb, rx_desc, ptype);
  179. if (rx_ring->ptp_rx)
  180. ice_ptp_rx_hwts_to_skb(rx_ring, rx_desc, skb);
  181. }
  182. /**
  183. * ice_receive_skb - Send a completed packet up the stack
  184. * @rx_ring: Rx ring in play
  185. * @skb: packet to send up
  186. * @vlan_tci: VLAN TCI for packet
  187. *
  188. * This function sends the completed packet (via. skb) up the stack using
  189. * gro receive functions (with/without VLAN tag)
  190. */
  191. void
  192. ice_receive_skb(struct ice_rx_ring *rx_ring, struct sk_buff *skb, u16 vlan_tci)
  193. {
  194. if ((vlan_tci & VLAN_VID_MASK) && rx_ring->vlan_proto)
  195. __vlan_hwaccel_put_tag(skb, rx_ring->vlan_proto,
  196. vlan_tci);
  197. napi_gro_receive(&rx_ring->q_vector->napi, skb);
  198. }
  199. /**
  200. * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
  201. * @dev: device for DMA mapping
  202. * @tx_buf: Tx buffer to clean
  203. * @bq: XDP bulk flush struct
  204. */
  205. static void
  206. ice_clean_xdp_tx_buf(struct device *dev, struct ice_tx_buf *tx_buf,
  207. struct xdp_frame_bulk *bq)
  208. {
  209. dma_unmap_single(dev, dma_unmap_addr(tx_buf, dma),
  210. dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
  211. dma_unmap_len_set(tx_buf, len, 0);
  212. switch (tx_buf->type) {
  213. case ICE_TX_BUF_XDP_TX:
  214. page_frag_free(tx_buf->raw_buf);
  215. break;
  216. case ICE_TX_BUF_XDP_XMIT:
  217. xdp_return_frame_bulk(tx_buf->xdpf, bq);
  218. break;
  219. }
  220. tx_buf->type = ICE_TX_BUF_EMPTY;
  221. }
  222. /**
  223. * ice_clean_xdp_irq - Reclaim resources after transmit completes on XDP ring
  224. * @xdp_ring: XDP ring to clean
  225. */
  226. static u32 ice_clean_xdp_irq(struct ice_tx_ring *xdp_ring)
  227. {
  228. int total_bytes = 0, total_pkts = 0;
  229. struct device *dev = xdp_ring->dev;
  230. u32 ntc = xdp_ring->next_to_clean;
  231. struct ice_tx_desc *tx_desc;
  232. u32 cnt = xdp_ring->count;
  233. struct xdp_frame_bulk bq;
  234. u32 frags, xdp_tx = 0;
  235. u32 ready_frames = 0;
  236. u32 idx;
  237. u32 ret;
  238. idx = xdp_ring->tx_buf[ntc].rs_idx;
  239. tx_desc = ICE_TX_DESC(xdp_ring, idx);
  240. if (tx_desc->cmd_type_offset_bsz &
  241. cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)) {
  242. if (idx >= ntc)
  243. ready_frames = idx - ntc + 1;
  244. else
  245. ready_frames = idx + cnt - ntc + 1;
  246. }
  247. if (unlikely(!ready_frames))
  248. return 0;
  249. ret = ready_frames;
  250. xdp_frame_bulk_init(&bq);
  251. rcu_read_lock(); /* xdp_return_frame_bulk() */
  252. while (ready_frames) {
  253. struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
  254. struct ice_tx_buf *head = tx_buf;
  255. /* bytecount holds size of head + frags */
  256. total_bytes += tx_buf->bytecount;
  257. frags = tx_buf->nr_frags;
  258. total_pkts++;
  259. /* count head + frags */
  260. ready_frames -= frags + 1;
  261. xdp_tx++;
  262. ntc++;
  263. if (ntc == cnt)
  264. ntc = 0;
  265. for (int i = 0; i < frags; i++) {
  266. tx_buf = &xdp_ring->tx_buf[ntc];
  267. ice_clean_xdp_tx_buf(dev, tx_buf, &bq);
  268. ntc++;
  269. if (ntc == cnt)
  270. ntc = 0;
  271. }
  272. ice_clean_xdp_tx_buf(dev, head, &bq);
  273. }
  274. xdp_flush_frame_bulk(&bq);
  275. rcu_read_unlock();
  276. tx_desc->cmd_type_offset_bsz = 0;
  277. xdp_ring->next_to_clean = ntc;
  278. xdp_ring->xdp_tx_active -= xdp_tx;
  279. ice_update_tx_ring_stats(xdp_ring, total_pkts, total_bytes);
  280. return ret;
  281. }
  282. /**
  283. * __ice_xmit_xdp_ring - submit frame to XDP ring for transmission
  284. * @xdp: XDP buffer to be placed onto Tx descriptors
  285. * @xdp_ring: XDP ring for transmission
  286. * @frame: whether this comes from .ndo_xdp_xmit()
  287. */
  288. int __ice_xmit_xdp_ring(struct xdp_buff *xdp, struct ice_tx_ring *xdp_ring,
  289. bool frame)
  290. {
  291. struct skb_shared_info *sinfo = NULL;
  292. u32 size = xdp->data_end - xdp->data;
  293. struct device *dev = xdp_ring->dev;
  294. u32 ntu = xdp_ring->next_to_use;
  295. struct ice_tx_desc *tx_desc;
  296. struct ice_tx_buf *tx_head;
  297. struct ice_tx_buf *tx_buf;
  298. u32 cnt = xdp_ring->count;
  299. void *data = xdp->data;
  300. u32 nr_frags = 0;
  301. u32 free_space;
  302. u32 frag = 0;
  303. free_space = ICE_DESC_UNUSED(xdp_ring);
  304. if (free_space < ICE_RING_QUARTER(xdp_ring))
  305. free_space += ice_clean_xdp_irq(xdp_ring);
  306. if (unlikely(!free_space))
  307. goto busy;
  308. if (unlikely(xdp_buff_has_frags(xdp))) {
  309. sinfo = xdp_get_shared_info_from_buff(xdp);
  310. nr_frags = sinfo->nr_frags;
  311. if (free_space < nr_frags + 1)
  312. goto busy;
  313. }
  314. tx_desc = ICE_TX_DESC(xdp_ring, ntu);
  315. tx_head = &xdp_ring->tx_buf[ntu];
  316. tx_buf = tx_head;
  317. for (;;) {
  318. dma_addr_t dma;
  319. dma = dma_map_single(dev, data, size, DMA_TO_DEVICE);
  320. if (dma_mapping_error(dev, dma))
  321. goto dma_unmap;
  322. /* record length, and DMA address */
  323. dma_unmap_len_set(tx_buf, len, size);
  324. dma_unmap_addr_set(tx_buf, dma, dma);
  325. if (frame) {
  326. tx_buf->type = ICE_TX_BUF_FRAG;
  327. } else {
  328. tx_buf->type = ICE_TX_BUF_XDP_TX;
  329. tx_buf->raw_buf = data;
  330. }
  331. tx_desc->buf_addr = cpu_to_le64(dma);
  332. tx_desc->cmd_type_offset_bsz = ice_build_ctob(0, 0, size, 0);
  333. ntu++;
  334. if (ntu == cnt)
  335. ntu = 0;
  336. if (frag == nr_frags)
  337. break;
  338. tx_desc = ICE_TX_DESC(xdp_ring, ntu);
  339. tx_buf = &xdp_ring->tx_buf[ntu];
  340. data = skb_frag_address(&sinfo->frags[frag]);
  341. size = skb_frag_size(&sinfo->frags[frag]);
  342. frag++;
  343. }
  344. /* store info about bytecount and frag count in first desc */
  345. tx_head->bytecount = xdp_get_buff_len(xdp);
  346. tx_head->nr_frags = nr_frags;
  347. if (frame) {
  348. tx_head->type = ICE_TX_BUF_XDP_XMIT;
  349. tx_head->xdpf = xdp->data_hard_start;
  350. }
  351. /* update last descriptor from a frame with EOP */
  352. tx_desc->cmd_type_offset_bsz |=
  353. cpu_to_le64(ICE_TX_DESC_CMD_EOP << ICE_TXD_QW1_CMD_S);
  354. xdp_ring->xdp_tx_active++;
  355. xdp_ring->next_to_use = ntu;
  356. return ICE_XDP_TX;
  357. dma_unmap:
  358. for (;;) {
  359. tx_buf = &xdp_ring->tx_buf[ntu];
  360. dma_unmap_page(dev, dma_unmap_addr(tx_buf, dma),
  361. dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
  362. dma_unmap_len_set(tx_buf, len, 0);
  363. if (tx_buf == tx_head)
  364. break;
  365. if (!ntu)
  366. ntu += cnt;
  367. ntu--;
  368. }
  369. return ICE_XDP_CONSUMED;
  370. busy:
  371. xdp_ring->ring_stats->tx_stats.tx_busy++;
  372. return ICE_XDP_CONSUMED;
  373. }
  374. /**
  375. * ice_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
  376. * @xdp_ring: XDP ring
  377. * @xdp_res: Result of the receive batch
  378. * @first_idx: index to write from caller
  379. *
  380. * This function bumps XDP Tx tail and/or flush redirect map, and
  381. * should be called when a batch of packets has been processed in the
  382. * napi loop.
  383. */
  384. void ice_finalize_xdp_rx(struct ice_tx_ring *xdp_ring, unsigned int xdp_res,
  385. u32 first_idx)
  386. {
  387. struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[first_idx];
  388. if (xdp_res & ICE_XDP_REDIR)
  389. xdp_do_flush();
  390. if (xdp_res & ICE_XDP_TX) {
  391. if (static_branch_unlikely(&ice_xdp_locking_key))
  392. spin_lock(&xdp_ring->tx_lock);
  393. /* store index of descriptor with RS bit set in the first
  394. * ice_tx_buf of given NAPI batch
  395. */
  396. tx_buf->rs_idx = ice_set_rs_bit(xdp_ring);
  397. ice_xdp_ring_update_tail(xdp_ring);
  398. if (static_branch_unlikely(&ice_xdp_locking_key))
  399. spin_unlock(&xdp_ring->tx_lock);
  400. }
  401. }
  402. /**
  403. * ice_xdp_rx_hw_ts - HW timestamp XDP hint handler
  404. * @ctx: XDP buff pointer
  405. * @ts_ns: destination address
  406. *
  407. * Copy HW timestamp (if available) to the destination address.
  408. */
  409. static int ice_xdp_rx_hw_ts(const struct xdp_md *ctx, u64 *ts_ns)
  410. {
  411. const struct ice_xdp_buff *xdp_ext = (void *)ctx;
  412. *ts_ns = ice_ptp_get_rx_hwts(xdp_ext->eop_desc,
  413. xdp_ext->pkt_ctx);
  414. if (!*ts_ns)
  415. return -ENODATA;
  416. return 0;
  417. }
  418. /**
  419. * ice_xdp_rx_hash_type - Get XDP-specific hash type from the RX descriptor
  420. * @eop_desc: End of Packet descriptor
  421. */
  422. static enum xdp_rss_hash_type
  423. ice_xdp_rx_hash_type(const union ice_32b_rx_flex_desc *eop_desc)
  424. {
  425. return libie_rx_pt_parse(ice_get_ptype(eop_desc)).hash_type;
  426. }
  427. /**
  428. * ice_xdp_rx_hash - RX hash XDP hint handler
  429. * @ctx: XDP buff pointer
  430. * @hash: hash destination address
  431. * @rss_type: XDP hash type destination address
  432. *
  433. * Copy RX hash (if available) and its type to the destination address.
  434. */
  435. static int ice_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
  436. enum xdp_rss_hash_type *rss_type)
  437. {
  438. const struct ice_xdp_buff *xdp_ext = (void *)ctx;
  439. *hash = ice_get_rx_hash(xdp_ext->eop_desc);
  440. *rss_type = ice_xdp_rx_hash_type(xdp_ext->eop_desc);
  441. if (!likely(*hash))
  442. return -ENODATA;
  443. return 0;
  444. }
  445. /**
  446. * ice_xdp_rx_vlan_tag - VLAN tag XDP hint handler
  447. * @ctx: XDP buff pointer
  448. * @vlan_proto: destination address for VLAN protocol
  449. * @vlan_tci: destination address for VLAN TCI
  450. *
  451. * Copy VLAN tag (if was stripped) and corresponding protocol
  452. * to the destination address.
  453. */
  454. static int ice_xdp_rx_vlan_tag(const struct xdp_md *ctx, __be16 *vlan_proto,
  455. u16 *vlan_tci)
  456. {
  457. const struct ice_xdp_buff *xdp_ext = (void *)ctx;
  458. *vlan_proto = xdp_ext->pkt_ctx->vlan_proto;
  459. if (!*vlan_proto)
  460. return -ENODATA;
  461. *vlan_tci = ice_get_vlan_tci(xdp_ext->eop_desc);
  462. if (!*vlan_tci)
  463. return -ENODATA;
  464. return 0;
  465. }
  466. const struct xdp_metadata_ops ice_xdp_md_ops = {
  467. .xmo_rx_timestamp = ice_xdp_rx_hw_ts,
  468. .xmo_rx_hash = ice_xdp_rx_hash,
  469. .xmo_rx_vlan_tag = ice_xdp_rx_vlan_tag,
  470. };