1d4fd0404SClaudiu Manoil // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2d4fd0404SClaudiu Manoil /* Copyright 2017-2019 NXP */ 3d4fd0404SClaudiu Manoil 4d4fd0404SClaudiu Manoil #include "enetc.h" 5d1b15102SVladimir Oltean #include <linux/bpf_trace.h> 6d4fd0404SClaudiu Manoil #include <linux/tcp.h> 7d4fd0404SClaudiu Manoil #include <linux/udp.h> 8bbcbf2eeSStephen Rothwell #include <linux/vmalloc.h> 97294380cSYangbo Lu #include <linux/ptp_classify.h> 10edce2a93SIoana Ciornei #include <net/ip6_checksum.h> 11847cbfc0SVladimir Oltean #include <net/pkt_sched.h> 12fb8629e2SIoana Ciornei #include <net/tso.h> 13d4fd0404SClaudiu Manoil 147eab503bSVladimir Oltean static int enetc_num_stack_tx_queues(struct enetc_ndev_priv *priv) 157eab503bSVladimir Oltean { 167eab503bSVladimir Oltean int num_tx_rings = priv->num_tx_rings; 177eab503bSVladimir Oltean int i; 187eab503bSVladimir Oltean 197eab503bSVladimir Oltean for (i = 0; i < priv->num_rx_rings; i++) 207eab503bSVladimir Oltean if (priv->rx_ring[i]->xdp.prog) 217eab503bSVladimir Oltean return num_tx_rings - num_possible_cpus(); 227eab503bSVladimir Oltean 237eab503bSVladimir Oltean return num_tx_rings; 247eab503bSVladimir Oltean } 257eab503bSVladimir Oltean 267eab503bSVladimir Oltean static struct enetc_bdr *enetc_rx_ring_from_xdp_tx_ring(struct enetc_ndev_priv *priv, 277eab503bSVladimir Oltean struct enetc_bdr *tx_ring) 287eab503bSVladimir Oltean { 297eab503bSVladimir Oltean int index = &priv->tx_ring[tx_ring->index] - priv->xdp_tx_ring; 307eab503bSVladimir Oltean 317eab503bSVladimir Oltean return priv->rx_ring[index]; 327eab503bSVladimir Oltean } 337eab503bSVladimir Oltean 349d2b68ccSVladimir Oltean static struct sk_buff *enetc_tx_swbd_get_skb(struct enetc_tx_swbd *tx_swbd) 359d2b68ccSVladimir Oltean { 369d2b68ccSVladimir Oltean if (tx_swbd->is_xdp_tx || tx_swbd->is_xdp_redirect) 379d2b68ccSVladimir Oltean return NULL; 389d2b68ccSVladimir Oltean 399d2b68ccSVladimir Oltean return tx_swbd->skb; 409d2b68ccSVladimir Oltean } 419d2b68ccSVladimir Oltean 429d2b68ccSVladimir Oltean static struct xdp_frame * 439d2b68ccSVladimir Oltean enetc_tx_swbd_get_xdp_frame(struct enetc_tx_swbd *tx_swbd) 449d2b68ccSVladimir Oltean { 459d2b68ccSVladimir Oltean if (tx_swbd->is_xdp_redirect) 469d2b68ccSVladimir Oltean return tx_swbd->xdp_frame; 479d2b68ccSVladimir Oltean 489d2b68ccSVladimir Oltean return NULL; 499d2b68ccSVladimir Oltean } 509d2b68ccSVladimir Oltean 51d4fd0404SClaudiu Manoil static void enetc_unmap_tx_buff(struct enetc_bdr *tx_ring, 52d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd) 53d4fd0404SClaudiu Manoil { 547ed2bc80SVladimir Oltean /* For XDP_TX, pages come from RX, whereas for the other contexts where 557ed2bc80SVladimir Oltean * we have is_dma_page_set, those come from skb_frag_dma_map. We need 567ed2bc80SVladimir Oltean * to match the DMA mapping length, so we need to differentiate those. 577ed2bc80SVladimir Oltean */ 58d4fd0404SClaudiu Manoil if (tx_swbd->is_dma_page) 59d4fd0404SClaudiu Manoil dma_unmap_page(tx_ring->dev, tx_swbd->dma, 607ed2bc80SVladimir Oltean tx_swbd->is_xdp_tx ? PAGE_SIZE : tx_swbd->len, 617ed2bc80SVladimir Oltean tx_swbd->dir); 62d4fd0404SClaudiu Manoil else 63d4fd0404SClaudiu Manoil dma_unmap_single(tx_ring->dev, tx_swbd->dma, 647ed2bc80SVladimir Oltean tx_swbd->len, tx_swbd->dir); 65d4fd0404SClaudiu Manoil tx_swbd->dma = 0; 66d4fd0404SClaudiu Manoil } 67d4fd0404SClaudiu Manoil 689d2b68ccSVladimir Oltean static void enetc_free_tx_frame(struct enetc_bdr *tx_ring, 69d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd) 70d4fd0404SClaudiu Manoil { 719d2b68ccSVladimir Oltean struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd); 729d2b68ccSVladimir Oltean struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd); 739d2b68ccSVladimir Oltean 74d4fd0404SClaudiu Manoil if (tx_swbd->dma) 75d4fd0404SClaudiu Manoil enetc_unmap_tx_buff(tx_ring, tx_swbd); 76d4fd0404SClaudiu Manoil 779d2b68ccSVladimir Oltean if (xdp_frame) { 789d2b68ccSVladimir Oltean xdp_return_frame(tx_swbd->xdp_frame); 799d2b68ccSVladimir Oltean tx_swbd->xdp_frame = NULL; 809d2b68ccSVladimir Oltean } else if (skb) { 819d2b68ccSVladimir Oltean dev_kfree_skb_any(skb); 82d4fd0404SClaudiu Manoil tx_swbd->skb = NULL; 83d4fd0404SClaudiu Manoil } 84d4fd0404SClaudiu Manoil } 85d4fd0404SClaudiu Manoil 867ed2bc80SVladimir Oltean /* Let H/W know BD ring has been updated */ 877ed2bc80SVladimir Oltean static void enetc_update_tx_ring_tail(struct enetc_bdr *tx_ring) 887ed2bc80SVladimir Oltean { 897ed2bc80SVladimir Oltean /* includes wmb() */ 907ed2bc80SVladimir Oltean enetc_wr_reg_hot(tx_ring->tpir, tx_ring->next_to_use); 917ed2bc80SVladimir Oltean } 927ed2bc80SVladimir Oltean 937294380cSYangbo Lu static int enetc_ptp_parse(struct sk_buff *skb, u8 *udp, 947294380cSYangbo Lu u8 *msgtype, u8 *twostep, 957294380cSYangbo Lu u16 *correction_offset, u16 *body_offset) 967294380cSYangbo Lu { 977294380cSYangbo Lu unsigned int ptp_class; 987294380cSYangbo Lu struct ptp_header *hdr; 997294380cSYangbo Lu unsigned int type; 1007294380cSYangbo Lu u8 *base; 1017294380cSYangbo Lu 1027294380cSYangbo Lu ptp_class = ptp_classify_raw(skb); 1037294380cSYangbo Lu if (ptp_class == PTP_CLASS_NONE) 1047294380cSYangbo Lu return -EINVAL; 1057294380cSYangbo Lu 1067294380cSYangbo Lu hdr = ptp_parse_header(skb, ptp_class); 1077294380cSYangbo Lu if (!hdr) 1087294380cSYangbo Lu return -EINVAL; 1097294380cSYangbo Lu 1107294380cSYangbo Lu type = ptp_class & PTP_CLASS_PMASK; 1117294380cSYangbo Lu if (type == PTP_CLASS_IPV4 || type == PTP_CLASS_IPV6) 1127294380cSYangbo Lu *udp = 1; 1137294380cSYangbo Lu else 1147294380cSYangbo Lu *udp = 0; 1157294380cSYangbo Lu 1167294380cSYangbo Lu *msgtype = ptp_get_msgtype(hdr, ptp_class); 1177294380cSYangbo Lu *twostep = hdr->flag_field[0] & 0x2; 1187294380cSYangbo Lu 1197294380cSYangbo Lu base = skb_mac_header(skb); 1207294380cSYangbo Lu *correction_offset = (u8 *)&hdr->correction - base; 1217294380cSYangbo Lu *body_offset = (u8 *)hdr + sizeof(struct ptp_header) - base; 1227294380cSYangbo Lu 1237294380cSYangbo Lu return 0; 1247294380cSYangbo Lu } 1257294380cSYangbo Lu 126f768e751SYangbo Lu static int enetc_map_tx_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb) 127d4fd0404SClaudiu Manoil { 1287294380cSYangbo Lu bool do_vlan, do_onestep_tstamp = false, do_twostep_tstamp = false; 1297294380cSYangbo Lu struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev); 1307294380cSYangbo Lu struct enetc_hw *hw = &priv->si->hw; 131d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd; 132d4fd0404SClaudiu Manoil int len = skb_headlen(skb); 133d4fd0404SClaudiu Manoil union enetc_tx_bd temp_bd; 1347294380cSYangbo Lu u8 msgtype, twostep, udp; 135d4fd0404SClaudiu Manoil union enetc_tx_bd *txbd; 1367294380cSYangbo Lu u16 offset1, offset2; 137d4fd0404SClaudiu Manoil int i, count = 0; 1387294380cSYangbo Lu skb_frag_t *frag; 139d4fd0404SClaudiu Manoil unsigned int f; 140d4fd0404SClaudiu Manoil dma_addr_t dma; 141d4fd0404SClaudiu Manoil u8 flags = 0; 142d4fd0404SClaudiu Manoil 143d4fd0404SClaudiu Manoil i = tx_ring->next_to_use; 144d4fd0404SClaudiu Manoil txbd = ENETC_TXBD(*tx_ring, i); 145d4fd0404SClaudiu Manoil prefetchw(txbd); 146d4fd0404SClaudiu Manoil 147d4fd0404SClaudiu Manoil dma = dma_map_single(tx_ring->dev, skb->data, len, DMA_TO_DEVICE); 148d4fd0404SClaudiu Manoil if (unlikely(dma_mapping_error(tx_ring->dev, dma))) 149d4fd0404SClaudiu Manoil goto dma_err; 150d4fd0404SClaudiu Manoil 151d4fd0404SClaudiu Manoil temp_bd.addr = cpu_to_le64(dma); 152d4fd0404SClaudiu Manoil temp_bd.buf_len = cpu_to_le16(len); 153d4fd0404SClaudiu Manoil temp_bd.lstatus = 0; 154d4fd0404SClaudiu Manoil 155d4fd0404SClaudiu Manoil tx_swbd = &tx_ring->tx_swbd[i]; 156d4fd0404SClaudiu Manoil tx_swbd->dma = dma; 157d4fd0404SClaudiu Manoil tx_swbd->len = len; 158d4fd0404SClaudiu Manoil tx_swbd->is_dma_page = 0; 1597ed2bc80SVladimir Oltean tx_swbd->dir = DMA_TO_DEVICE; 160d4fd0404SClaudiu Manoil count++; 161d4fd0404SClaudiu Manoil 162d4fd0404SClaudiu Manoil do_vlan = skb_vlan_tag_present(skb); 1637294380cSYangbo Lu if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) { 1647294380cSYangbo Lu if (enetc_ptp_parse(skb, &udp, &msgtype, &twostep, &offset1, 1657294380cSYangbo Lu &offset2) || 1667294380cSYangbo Lu msgtype != PTP_MSGTYPE_SYNC || twostep) 1677294380cSYangbo Lu WARN_ONCE(1, "Bad packet for one-step timestamping\n"); 1687294380cSYangbo Lu else 1697294380cSYangbo Lu do_onestep_tstamp = true; 1707294380cSYangbo Lu } else if (skb->cb[0] & ENETC_F_TX_TSTAMP) { 1717294380cSYangbo Lu do_twostep_tstamp = true; 1727294380cSYangbo Lu } 173d4fd0404SClaudiu Manoil 1747294380cSYangbo Lu tx_swbd->do_twostep_tstamp = do_twostep_tstamp; 1757294380cSYangbo Lu tx_swbd->check_wb = tx_swbd->do_twostep_tstamp; 1767294380cSYangbo Lu 1777294380cSYangbo Lu if (do_vlan || do_onestep_tstamp || do_twostep_tstamp) 178d4fd0404SClaudiu Manoil flags |= ENETC_TXBD_FLAGS_EX; 179d4fd0404SClaudiu Manoil 18082728b91SClaudiu Manoil if (tx_ring->tsd_enable) 1810d08c9ecSPo Liu flags |= ENETC_TXBD_FLAGS_TSE | ENETC_TXBD_FLAGS_TXSTART; 182d4fd0404SClaudiu Manoil 183d4fd0404SClaudiu Manoil /* first BD needs frm_len and offload flags set */ 184d4fd0404SClaudiu Manoil temp_bd.frm_len = cpu_to_le16(skb->len); 185d4fd0404SClaudiu Manoil temp_bd.flags = flags; 186d4fd0404SClaudiu Manoil 18782728b91SClaudiu Manoil if (flags & ENETC_TXBD_FLAGS_TSE) 18882728b91SClaudiu Manoil temp_bd.txstart = enetc_txbd_set_tx_start(skb->skb_mstamp_ns, 18982728b91SClaudiu Manoil flags); 1900d08c9ecSPo Liu 191d4fd0404SClaudiu Manoil if (flags & ENETC_TXBD_FLAGS_EX) { 192d4fd0404SClaudiu Manoil u8 e_flags = 0; 193d4fd0404SClaudiu Manoil *txbd = temp_bd; 194d4fd0404SClaudiu Manoil enetc_clear_tx_bd(&temp_bd); 195d4fd0404SClaudiu Manoil 196d4fd0404SClaudiu Manoil /* add extension BD for VLAN and/or timestamping */ 197d4fd0404SClaudiu Manoil flags = 0; 198d4fd0404SClaudiu Manoil tx_swbd++; 199d4fd0404SClaudiu Manoil txbd++; 200d4fd0404SClaudiu Manoil i++; 201d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 202d4fd0404SClaudiu Manoil i = 0; 203d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 204d4fd0404SClaudiu Manoil txbd = ENETC_TXBD(*tx_ring, 0); 205d4fd0404SClaudiu Manoil } 206d4fd0404SClaudiu Manoil prefetchw(txbd); 207d4fd0404SClaudiu Manoil 208d4fd0404SClaudiu Manoil if (do_vlan) { 209d4fd0404SClaudiu Manoil temp_bd.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb)); 210d4fd0404SClaudiu Manoil temp_bd.ext.tpid = 0; /* < C-TAG */ 211d4fd0404SClaudiu Manoil e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS; 212d4fd0404SClaudiu Manoil } 213d4fd0404SClaudiu Manoil 2147294380cSYangbo Lu if (do_onestep_tstamp) { 2157294380cSYangbo Lu u32 lo, hi, val; 2167294380cSYangbo Lu u64 sec, nsec; 2177294380cSYangbo Lu u8 *data; 2187294380cSYangbo Lu 2197294380cSYangbo Lu lo = enetc_rd_hot(hw, ENETC_SICTR0); 2207294380cSYangbo Lu hi = enetc_rd_hot(hw, ENETC_SICTR1); 2217294380cSYangbo Lu sec = (u64)hi << 32 | lo; 2227294380cSYangbo Lu nsec = do_div(sec, 1000000000); 2237294380cSYangbo Lu 2247294380cSYangbo Lu /* Configure extension BD */ 2257294380cSYangbo Lu temp_bd.ext.tstamp = cpu_to_le32(lo & 0x3fffffff); 2267294380cSYangbo Lu e_flags |= ENETC_TXBD_E_FLAGS_ONE_STEP_PTP; 2277294380cSYangbo Lu 2287294380cSYangbo Lu /* Update originTimestamp field of Sync packet 2297294380cSYangbo Lu * - 48 bits seconds field 2307294380cSYangbo Lu * - 32 bits nanseconds field 2317294380cSYangbo Lu */ 2327294380cSYangbo Lu data = skb_mac_header(skb); 2337294380cSYangbo Lu *(__be16 *)(data + offset2) = 2347294380cSYangbo Lu htons((sec >> 32) & 0xffff); 2357294380cSYangbo Lu *(__be32 *)(data + offset2 + 2) = 2367294380cSYangbo Lu htonl(sec & 0xffffffff); 2377294380cSYangbo Lu *(__be32 *)(data + offset2 + 6) = htonl(nsec); 2387294380cSYangbo Lu 2397294380cSYangbo Lu /* Configure single-step register */ 2407294380cSYangbo Lu val = ENETC_PM0_SINGLE_STEP_EN; 2417294380cSYangbo Lu val |= ENETC_SET_SINGLE_STEP_OFFSET(offset1); 2427294380cSYangbo Lu if (udp) 2437294380cSYangbo Lu val |= ENETC_PM0_SINGLE_STEP_CH; 2447294380cSYangbo Lu 2457294380cSYangbo Lu enetc_port_wr(hw, ENETC_PM0_SINGLE_STEP, val); 2467294380cSYangbo Lu enetc_port_wr(hw, ENETC_PM1_SINGLE_STEP, val); 2477294380cSYangbo Lu } else if (do_twostep_tstamp) { 248d4fd0404SClaudiu Manoil skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 249d4fd0404SClaudiu Manoil e_flags |= ENETC_TXBD_E_FLAGS_TWO_STEP_PTP; 250d4fd0404SClaudiu Manoil } 251d4fd0404SClaudiu Manoil 252d4fd0404SClaudiu Manoil temp_bd.ext.e_flags = e_flags; 253d4fd0404SClaudiu Manoil count++; 254d4fd0404SClaudiu Manoil } 255d4fd0404SClaudiu Manoil 256d4fd0404SClaudiu Manoil frag = &skb_shinfo(skb)->frags[0]; 257d4fd0404SClaudiu Manoil for (f = 0; f < skb_shinfo(skb)->nr_frags; f++, frag++) { 258d4fd0404SClaudiu Manoil len = skb_frag_size(frag); 259d4fd0404SClaudiu Manoil dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len, 260d4fd0404SClaudiu Manoil DMA_TO_DEVICE); 261d4fd0404SClaudiu Manoil if (dma_mapping_error(tx_ring->dev, dma)) 262d4fd0404SClaudiu Manoil goto dma_err; 263d4fd0404SClaudiu Manoil 264d4fd0404SClaudiu Manoil *txbd = temp_bd; 265d4fd0404SClaudiu Manoil enetc_clear_tx_bd(&temp_bd); 266d4fd0404SClaudiu Manoil 267d4fd0404SClaudiu Manoil flags = 0; 268d4fd0404SClaudiu Manoil tx_swbd++; 269d4fd0404SClaudiu Manoil txbd++; 270d4fd0404SClaudiu Manoil i++; 271d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 272d4fd0404SClaudiu Manoil i = 0; 273d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 274d4fd0404SClaudiu Manoil txbd = ENETC_TXBD(*tx_ring, 0); 275d4fd0404SClaudiu Manoil } 276d4fd0404SClaudiu Manoil prefetchw(txbd); 277d4fd0404SClaudiu Manoil 278d4fd0404SClaudiu Manoil temp_bd.addr = cpu_to_le64(dma); 279d4fd0404SClaudiu Manoil temp_bd.buf_len = cpu_to_le16(len); 280d4fd0404SClaudiu Manoil 281d4fd0404SClaudiu Manoil tx_swbd->dma = dma; 282d4fd0404SClaudiu Manoil tx_swbd->len = len; 283d4fd0404SClaudiu Manoil tx_swbd->is_dma_page = 1; 2847ed2bc80SVladimir Oltean tx_swbd->dir = DMA_TO_DEVICE; 285d4fd0404SClaudiu Manoil count++; 286d4fd0404SClaudiu Manoil } 287d4fd0404SClaudiu Manoil 288d4fd0404SClaudiu Manoil /* last BD needs 'F' bit set */ 289d4fd0404SClaudiu Manoil flags |= ENETC_TXBD_FLAGS_F; 290d4fd0404SClaudiu Manoil temp_bd.flags = flags; 291d4fd0404SClaudiu Manoil *txbd = temp_bd; 292d4fd0404SClaudiu Manoil 293d504498dSVladimir Oltean tx_ring->tx_swbd[i].is_eof = true; 294d4fd0404SClaudiu Manoil tx_ring->tx_swbd[i].skb = skb; 295d4fd0404SClaudiu Manoil 296d4fd0404SClaudiu Manoil enetc_bdr_idx_inc(tx_ring, &i); 297d4fd0404SClaudiu Manoil tx_ring->next_to_use = i; 298d4fd0404SClaudiu Manoil 2994caefbceSMichael Walle skb_tx_timestamp(skb); 3004caefbceSMichael Walle 3017ed2bc80SVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 302d4fd0404SClaudiu Manoil 303d4fd0404SClaudiu Manoil return count; 304d4fd0404SClaudiu Manoil 305d4fd0404SClaudiu Manoil dma_err: 306d4fd0404SClaudiu Manoil dev_err(tx_ring->dev, "DMA map error"); 307d4fd0404SClaudiu Manoil 308d4fd0404SClaudiu Manoil do { 309d4fd0404SClaudiu Manoil tx_swbd = &tx_ring->tx_swbd[i]; 3109d2b68ccSVladimir Oltean enetc_free_tx_frame(tx_ring, tx_swbd); 311d4fd0404SClaudiu Manoil if (i == 0) 312d4fd0404SClaudiu Manoil i = tx_ring->bd_count; 313d4fd0404SClaudiu Manoil i--; 314d4fd0404SClaudiu Manoil } while (count--); 315d4fd0404SClaudiu Manoil 316d4fd0404SClaudiu Manoil return 0; 317d4fd0404SClaudiu Manoil } 318d4fd0404SClaudiu Manoil 319fb8629e2SIoana Ciornei static void enetc_map_tx_tso_hdr(struct enetc_bdr *tx_ring, struct sk_buff *skb, 320fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd, 321fb8629e2SIoana Ciornei union enetc_tx_bd *txbd, int *i, int hdr_len, 322fb8629e2SIoana Ciornei int data_len) 323fb8629e2SIoana Ciornei { 324fb8629e2SIoana Ciornei union enetc_tx_bd txbd_tmp; 325fb8629e2SIoana Ciornei u8 flags = 0, e_flags = 0; 326fb8629e2SIoana Ciornei dma_addr_t addr; 327fb8629e2SIoana Ciornei 328fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 329fb8629e2SIoana Ciornei addr = tx_ring->tso_headers_dma + *i * TSO_HEADER_SIZE; 330fb8629e2SIoana Ciornei 331fb8629e2SIoana Ciornei if (skb_vlan_tag_present(skb)) 332fb8629e2SIoana Ciornei flags |= ENETC_TXBD_FLAGS_EX; 333fb8629e2SIoana Ciornei 334fb8629e2SIoana Ciornei txbd_tmp.addr = cpu_to_le64(addr); 335fb8629e2SIoana Ciornei txbd_tmp.buf_len = cpu_to_le16(hdr_len); 336fb8629e2SIoana Ciornei 337fb8629e2SIoana Ciornei /* first BD needs frm_len and offload flags set */ 338fb8629e2SIoana Ciornei txbd_tmp.frm_len = cpu_to_le16(hdr_len + data_len); 339fb8629e2SIoana Ciornei txbd_tmp.flags = flags; 340fb8629e2SIoana Ciornei 341fb8629e2SIoana Ciornei /* For the TSO header we do not set the dma address since we do not 342fb8629e2SIoana Ciornei * want it unmapped when we do cleanup. We still set len so that we 343fb8629e2SIoana Ciornei * count the bytes sent. 344fb8629e2SIoana Ciornei */ 345fb8629e2SIoana Ciornei tx_swbd->len = hdr_len; 346fb8629e2SIoana Ciornei tx_swbd->do_twostep_tstamp = false; 347fb8629e2SIoana Ciornei tx_swbd->check_wb = false; 348fb8629e2SIoana Ciornei 349fb8629e2SIoana Ciornei /* Actually write the header in the BD */ 350fb8629e2SIoana Ciornei *txbd = txbd_tmp; 351fb8629e2SIoana Ciornei 352fb8629e2SIoana Ciornei /* Add extension BD for VLAN */ 353fb8629e2SIoana Ciornei if (flags & ENETC_TXBD_FLAGS_EX) { 354fb8629e2SIoana Ciornei /* Get the next BD */ 355fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, i); 356fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, *i); 357fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[*i]; 358fb8629e2SIoana Ciornei prefetchw(txbd); 359fb8629e2SIoana Ciornei 360fb8629e2SIoana Ciornei /* Setup the VLAN fields */ 361fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 362fb8629e2SIoana Ciornei txbd_tmp.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb)); 363fb8629e2SIoana Ciornei txbd_tmp.ext.tpid = 0; /* < C-TAG */ 364fb8629e2SIoana Ciornei e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS; 365fb8629e2SIoana Ciornei 366fb8629e2SIoana Ciornei /* Write the BD */ 367fb8629e2SIoana Ciornei txbd_tmp.ext.e_flags = e_flags; 368fb8629e2SIoana Ciornei *txbd = txbd_tmp; 369fb8629e2SIoana Ciornei } 370fb8629e2SIoana Ciornei } 371fb8629e2SIoana Ciornei 372fb8629e2SIoana Ciornei static int enetc_map_tx_tso_data(struct enetc_bdr *tx_ring, struct sk_buff *skb, 373fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd, 374fb8629e2SIoana Ciornei union enetc_tx_bd *txbd, char *data, 375fb8629e2SIoana Ciornei int size, bool last_bd) 376fb8629e2SIoana Ciornei { 377fb8629e2SIoana Ciornei union enetc_tx_bd txbd_tmp; 378fb8629e2SIoana Ciornei dma_addr_t addr; 379fb8629e2SIoana Ciornei u8 flags = 0; 380fb8629e2SIoana Ciornei 381fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 382fb8629e2SIoana Ciornei 383fb8629e2SIoana Ciornei addr = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE); 384fb8629e2SIoana Ciornei if (unlikely(dma_mapping_error(tx_ring->dev, addr))) { 385fb8629e2SIoana Ciornei netdev_err(tx_ring->ndev, "DMA map error\n"); 386fb8629e2SIoana Ciornei return -ENOMEM; 387fb8629e2SIoana Ciornei } 388fb8629e2SIoana Ciornei 389fb8629e2SIoana Ciornei if (last_bd) { 390fb8629e2SIoana Ciornei flags |= ENETC_TXBD_FLAGS_F; 391fb8629e2SIoana Ciornei tx_swbd->is_eof = 1; 392fb8629e2SIoana Ciornei } 393fb8629e2SIoana Ciornei 394fb8629e2SIoana Ciornei txbd_tmp.addr = cpu_to_le64(addr); 395fb8629e2SIoana Ciornei txbd_tmp.buf_len = cpu_to_le16(size); 396fb8629e2SIoana Ciornei txbd_tmp.flags = flags; 397fb8629e2SIoana Ciornei 398fb8629e2SIoana Ciornei tx_swbd->dma = addr; 399fb8629e2SIoana Ciornei tx_swbd->len = size; 400fb8629e2SIoana Ciornei tx_swbd->dir = DMA_TO_DEVICE; 401fb8629e2SIoana Ciornei 402fb8629e2SIoana Ciornei *txbd = txbd_tmp; 403fb8629e2SIoana Ciornei 404fb8629e2SIoana Ciornei return 0; 405fb8629e2SIoana Ciornei } 406fb8629e2SIoana Ciornei 407fb8629e2SIoana Ciornei static __wsum enetc_tso_hdr_csum(struct tso_t *tso, struct sk_buff *skb, 408fb8629e2SIoana Ciornei char *hdr, int hdr_len, int *l4_hdr_len) 409fb8629e2SIoana Ciornei { 410fb8629e2SIoana Ciornei char *l4_hdr = hdr + skb_transport_offset(skb); 411fb8629e2SIoana Ciornei int mac_hdr_len = skb_network_offset(skb); 412fb8629e2SIoana Ciornei 413fb8629e2SIoana Ciornei if (tso->tlen != sizeof(struct udphdr)) { 414fb8629e2SIoana Ciornei struct tcphdr *tcph = (struct tcphdr *)(l4_hdr); 415fb8629e2SIoana Ciornei 416fb8629e2SIoana Ciornei tcph->check = 0; 417fb8629e2SIoana Ciornei } else { 418fb8629e2SIoana Ciornei struct udphdr *udph = (struct udphdr *)(l4_hdr); 419fb8629e2SIoana Ciornei 420fb8629e2SIoana Ciornei udph->check = 0; 421fb8629e2SIoana Ciornei } 422fb8629e2SIoana Ciornei 423fb8629e2SIoana Ciornei /* Compute the IP checksum. This is necessary since tso_build_hdr() 424fb8629e2SIoana Ciornei * already incremented the IP ID field. 425fb8629e2SIoana Ciornei */ 426fb8629e2SIoana Ciornei if (!tso->ipv6) { 427fb8629e2SIoana Ciornei struct iphdr *iph = (void *)(hdr + mac_hdr_len); 428fb8629e2SIoana Ciornei 429fb8629e2SIoana Ciornei iph->check = 0; 430fb8629e2SIoana Ciornei iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 431fb8629e2SIoana Ciornei } 432fb8629e2SIoana Ciornei 433fb8629e2SIoana Ciornei /* Compute the checksum over the L4 header. */ 434fb8629e2SIoana Ciornei *l4_hdr_len = hdr_len - skb_transport_offset(skb); 435fb8629e2SIoana Ciornei return csum_partial(l4_hdr, *l4_hdr_len, 0); 436fb8629e2SIoana Ciornei } 437fb8629e2SIoana Ciornei 438fb8629e2SIoana Ciornei static void enetc_tso_complete_csum(struct enetc_bdr *tx_ring, struct tso_t *tso, 439fb8629e2SIoana Ciornei struct sk_buff *skb, char *hdr, int len, 440fb8629e2SIoana Ciornei __wsum sum) 441fb8629e2SIoana Ciornei { 442fb8629e2SIoana Ciornei char *l4_hdr = hdr + skb_transport_offset(skb); 443fb8629e2SIoana Ciornei __sum16 csum_final; 444fb8629e2SIoana Ciornei 445fb8629e2SIoana Ciornei /* Complete the L4 checksum by appending the pseudo-header to the 446fb8629e2SIoana Ciornei * already computed checksum. 447fb8629e2SIoana Ciornei */ 448fb8629e2SIoana Ciornei if (!tso->ipv6) 449fb8629e2SIoana Ciornei csum_final = csum_tcpudp_magic(ip_hdr(skb)->saddr, 450fb8629e2SIoana Ciornei ip_hdr(skb)->daddr, 451fb8629e2SIoana Ciornei len, ip_hdr(skb)->protocol, sum); 452fb8629e2SIoana Ciornei else 453fb8629e2SIoana Ciornei csum_final = csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 454fb8629e2SIoana Ciornei &ipv6_hdr(skb)->daddr, 455fb8629e2SIoana Ciornei len, ipv6_hdr(skb)->nexthdr, sum); 456fb8629e2SIoana Ciornei 457fb8629e2SIoana Ciornei if (tso->tlen != sizeof(struct udphdr)) { 458fb8629e2SIoana Ciornei struct tcphdr *tcph = (struct tcphdr *)(l4_hdr); 459fb8629e2SIoana Ciornei 460fb8629e2SIoana Ciornei tcph->check = csum_final; 461fb8629e2SIoana Ciornei } else { 462fb8629e2SIoana Ciornei struct udphdr *udph = (struct udphdr *)(l4_hdr); 463fb8629e2SIoana Ciornei 464fb8629e2SIoana Ciornei udph->check = csum_final; 465fb8629e2SIoana Ciornei } 466fb8629e2SIoana Ciornei } 467fb8629e2SIoana Ciornei 468fb8629e2SIoana Ciornei static int enetc_map_tx_tso_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb) 469fb8629e2SIoana Ciornei { 470fb8629e2SIoana Ciornei int hdr_len, total_len, data_len; 471fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd; 472fb8629e2SIoana Ciornei union enetc_tx_bd *txbd; 473fb8629e2SIoana Ciornei struct tso_t tso; 474fb8629e2SIoana Ciornei __wsum csum, csum2; 475fb8629e2SIoana Ciornei int count = 0, pos; 476fb8629e2SIoana Ciornei int err, i, bd_data_num; 477fb8629e2SIoana Ciornei 478fb8629e2SIoana Ciornei /* Initialize the TSO handler, and prepare the first payload */ 479fb8629e2SIoana Ciornei hdr_len = tso_start(skb, &tso); 480fb8629e2SIoana Ciornei total_len = skb->len - hdr_len; 481fb8629e2SIoana Ciornei i = tx_ring->next_to_use; 482fb8629e2SIoana Ciornei 483fb8629e2SIoana Ciornei while (total_len > 0) { 484fb8629e2SIoana Ciornei char *hdr; 485fb8629e2SIoana Ciornei 486fb8629e2SIoana Ciornei /* Get the BD */ 487fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, i); 488fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 489fb8629e2SIoana Ciornei prefetchw(txbd); 490fb8629e2SIoana Ciornei 491fb8629e2SIoana Ciornei /* Determine the length of this packet */ 492fb8629e2SIoana Ciornei data_len = min_t(int, skb_shinfo(skb)->gso_size, total_len); 493fb8629e2SIoana Ciornei total_len -= data_len; 494fb8629e2SIoana Ciornei 495fb8629e2SIoana Ciornei /* prepare packet headers: MAC + IP + TCP */ 496fb8629e2SIoana Ciornei hdr = tx_ring->tso_headers + i * TSO_HEADER_SIZE; 497fb8629e2SIoana Ciornei tso_build_hdr(skb, hdr, &tso, data_len, total_len == 0); 498fb8629e2SIoana Ciornei 499fb8629e2SIoana Ciornei /* compute the csum over the L4 header */ 500fb8629e2SIoana Ciornei csum = enetc_tso_hdr_csum(&tso, skb, hdr, hdr_len, &pos); 501fb8629e2SIoana Ciornei enetc_map_tx_tso_hdr(tx_ring, skb, tx_swbd, txbd, &i, hdr_len, data_len); 502fb8629e2SIoana Ciornei bd_data_num = 0; 503fb8629e2SIoana Ciornei count++; 504fb8629e2SIoana Ciornei 505fb8629e2SIoana Ciornei while (data_len > 0) { 506fb8629e2SIoana Ciornei int size; 507fb8629e2SIoana Ciornei 508fb8629e2SIoana Ciornei size = min_t(int, tso.size, data_len); 509fb8629e2SIoana Ciornei 510fb8629e2SIoana Ciornei /* Advance the index in the BDR */ 511fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, &i); 512fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, i); 513fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 514fb8629e2SIoana Ciornei prefetchw(txbd); 515fb8629e2SIoana Ciornei 516fb8629e2SIoana Ciornei /* Compute the checksum over this segment of data and 517fb8629e2SIoana Ciornei * add it to the csum already computed (over the L4 518fb8629e2SIoana Ciornei * header and possible other data segments). 519fb8629e2SIoana Ciornei */ 520fb8629e2SIoana Ciornei csum2 = csum_partial(tso.data, size, 0); 521fb8629e2SIoana Ciornei csum = csum_block_add(csum, csum2, pos); 522fb8629e2SIoana Ciornei pos += size; 523fb8629e2SIoana Ciornei 524fb8629e2SIoana Ciornei err = enetc_map_tx_tso_data(tx_ring, skb, tx_swbd, txbd, 525fb8629e2SIoana Ciornei tso.data, size, 526fb8629e2SIoana Ciornei size == data_len); 527fb8629e2SIoana Ciornei if (err) 528fb8629e2SIoana Ciornei goto err_map_data; 529fb8629e2SIoana Ciornei 530fb8629e2SIoana Ciornei data_len -= size; 531fb8629e2SIoana Ciornei count++; 532fb8629e2SIoana Ciornei bd_data_num++; 533fb8629e2SIoana Ciornei tso_build_data(skb, &tso, size); 534fb8629e2SIoana Ciornei 535fb8629e2SIoana Ciornei if (unlikely(bd_data_num >= ENETC_MAX_SKB_FRAGS && data_len)) 536fb8629e2SIoana Ciornei goto err_chained_bd; 537fb8629e2SIoana Ciornei } 538fb8629e2SIoana Ciornei 539fb8629e2SIoana Ciornei enetc_tso_complete_csum(tx_ring, &tso, skb, hdr, pos, csum); 540fb8629e2SIoana Ciornei 541fb8629e2SIoana Ciornei if (total_len == 0) 542fb8629e2SIoana Ciornei tx_swbd->skb = skb; 543fb8629e2SIoana Ciornei 544fb8629e2SIoana Ciornei /* Go to the next BD */ 545fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, &i); 546fb8629e2SIoana Ciornei } 547fb8629e2SIoana Ciornei 548fb8629e2SIoana Ciornei tx_ring->next_to_use = i; 549fb8629e2SIoana Ciornei enetc_update_tx_ring_tail(tx_ring); 550fb8629e2SIoana Ciornei 551fb8629e2SIoana Ciornei return count; 552fb8629e2SIoana Ciornei 553fb8629e2SIoana Ciornei err_map_data: 554fb8629e2SIoana Ciornei dev_err(tx_ring->dev, "DMA map error"); 555fb8629e2SIoana Ciornei 556fb8629e2SIoana Ciornei err_chained_bd: 557fb8629e2SIoana Ciornei do { 558fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 559fb8629e2SIoana Ciornei enetc_free_tx_frame(tx_ring, tx_swbd); 560fb8629e2SIoana Ciornei if (i == 0) 561fb8629e2SIoana Ciornei i = tx_ring->bd_count; 562fb8629e2SIoana Ciornei i--; 563fb8629e2SIoana Ciornei } while (count--); 564fb8629e2SIoana Ciornei 565fb8629e2SIoana Ciornei return 0; 566fb8629e2SIoana Ciornei } 567fb8629e2SIoana Ciornei 5687294380cSYangbo Lu static netdev_tx_t enetc_start_xmit(struct sk_buff *skb, 5697294380cSYangbo Lu struct net_device *ndev) 5700486185eSVladimir Oltean { 5710486185eSVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(ndev); 5720486185eSVladimir Oltean struct enetc_bdr *tx_ring; 573acede3c5SIoana Ciornei int count, err; 5740486185eSVladimir Oltean 5757ce9c3d3SYangbo Lu /* Queue one-step Sync packet if already locked */ 5767ce9c3d3SYangbo Lu if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) { 5777ce9c3d3SYangbo Lu if (test_and_set_bit_lock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS, 5787ce9c3d3SYangbo Lu &priv->flags)) { 5797ce9c3d3SYangbo Lu skb_queue_tail(&priv->tx_skbs, skb); 5807ce9c3d3SYangbo Lu return NETDEV_TX_OK; 5817ce9c3d3SYangbo Lu } 5827ce9c3d3SYangbo Lu } 5837ce9c3d3SYangbo Lu 5840486185eSVladimir Oltean tx_ring = priv->tx_ring[skb->queue_mapping]; 5850486185eSVladimir Oltean 586fb8629e2SIoana Ciornei if (skb_is_gso(skb)) { 587fb8629e2SIoana Ciornei if (enetc_bd_unused(tx_ring) < tso_count_descs(skb)) { 588fb8629e2SIoana Ciornei netif_stop_subqueue(ndev, tx_ring->index); 589fb8629e2SIoana Ciornei return NETDEV_TX_BUSY; 590fb8629e2SIoana Ciornei } 591fb8629e2SIoana Ciornei 592fb8629e2SIoana Ciornei enetc_lock_mdio(); 593fb8629e2SIoana Ciornei count = enetc_map_tx_tso_buffs(tx_ring, skb); 594fb8629e2SIoana Ciornei enetc_unlock_mdio(); 595fb8629e2SIoana Ciornei } else { 5960486185eSVladimir Oltean if (unlikely(skb_shinfo(skb)->nr_frags > ENETC_MAX_SKB_FRAGS)) 5970486185eSVladimir Oltean if (unlikely(skb_linearize(skb))) 5980486185eSVladimir Oltean goto drop_packet_err; 5990486185eSVladimir Oltean 6000486185eSVladimir Oltean count = skb_shinfo(skb)->nr_frags + 1; /* fragments + head */ 6010486185eSVladimir Oltean if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(count)) { 6020486185eSVladimir Oltean netif_stop_subqueue(ndev, tx_ring->index); 6030486185eSVladimir Oltean return NETDEV_TX_BUSY; 6040486185eSVladimir Oltean } 6050486185eSVladimir Oltean 606acede3c5SIoana Ciornei if (skb->ip_summed == CHECKSUM_PARTIAL) { 607acede3c5SIoana Ciornei err = skb_checksum_help(skb); 608acede3c5SIoana Ciornei if (err) 609acede3c5SIoana Ciornei goto drop_packet_err; 610acede3c5SIoana Ciornei } 6110486185eSVladimir Oltean enetc_lock_mdio(); 612f768e751SYangbo Lu count = enetc_map_tx_buffs(tx_ring, skb); 6130486185eSVladimir Oltean enetc_unlock_mdio(); 614fb8629e2SIoana Ciornei } 6150486185eSVladimir Oltean 6160486185eSVladimir Oltean if (unlikely(!count)) 6170486185eSVladimir Oltean goto drop_packet_err; 6180486185eSVladimir Oltean 6190486185eSVladimir Oltean if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_MAX_NEEDED) 6200486185eSVladimir Oltean netif_stop_subqueue(ndev, tx_ring->index); 6210486185eSVladimir Oltean 6220486185eSVladimir Oltean return NETDEV_TX_OK; 6230486185eSVladimir Oltean 6240486185eSVladimir Oltean drop_packet_err: 6250486185eSVladimir Oltean dev_kfree_skb_any(skb); 6260486185eSVladimir Oltean return NETDEV_TX_OK; 6270486185eSVladimir Oltean } 6280486185eSVladimir Oltean 6297294380cSYangbo Lu netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev) 6307294380cSYangbo Lu { 6317294380cSYangbo Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 6327294380cSYangbo Lu u8 udp, msgtype, twostep; 6337294380cSYangbo Lu u16 offset1, offset2; 6347294380cSYangbo Lu 6357294380cSYangbo Lu /* Mark tx timestamp type on skb->cb[0] if requires */ 6367294380cSYangbo Lu if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 6377294380cSYangbo Lu (priv->active_offloads & ENETC_F_TX_TSTAMP_MASK)) { 6387294380cSYangbo Lu skb->cb[0] = priv->active_offloads & ENETC_F_TX_TSTAMP_MASK; 6397294380cSYangbo Lu } else { 6407294380cSYangbo Lu skb->cb[0] = 0; 6417294380cSYangbo Lu } 6427294380cSYangbo Lu 6437294380cSYangbo Lu /* Fall back to two-step timestamp if not one-step Sync packet */ 6447294380cSYangbo Lu if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) { 6457294380cSYangbo Lu if (enetc_ptp_parse(skb, &udp, &msgtype, &twostep, 6467294380cSYangbo Lu &offset1, &offset2) || 6477294380cSYangbo Lu msgtype != PTP_MSGTYPE_SYNC || twostep != 0) 6487294380cSYangbo Lu skb->cb[0] = ENETC_F_TX_TSTAMP; 6497294380cSYangbo Lu } 6507294380cSYangbo Lu 6517294380cSYangbo Lu return enetc_start_xmit(skb, ndev); 6527294380cSYangbo Lu } 6537294380cSYangbo Lu 654d4fd0404SClaudiu Manoil static irqreturn_t enetc_msix(int irq, void *data) 655d4fd0404SClaudiu Manoil { 656d4fd0404SClaudiu Manoil struct enetc_int_vector *v = data; 657d4fd0404SClaudiu Manoil int i; 658d4fd0404SClaudiu Manoil 659fd5736bfSAlex Marginean enetc_lock_mdio(); 660fd5736bfSAlex Marginean 661d4fd0404SClaudiu Manoil /* disable interrupts */ 662fd5736bfSAlex Marginean enetc_wr_reg_hot(v->rbier, 0); 663fd5736bfSAlex Marginean enetc_wr_reg_hot(v->ricr1, v->rx_ictt); 664d4fd0404SClaudiu Manoil 6650574e200SClaudiu Manoil for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS) 666fd5736bfSAlex Marginean enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 0); 667fd5736bfSAlex Marginean 668fd5736bfSAlex Marginean enetc_unlock_mdio(); 669d4fd0404SClaudiu Manoil 670215602a8SJiafei Pan napi_schedule(&v->napi); 671d4fd0404SClaudiu Manoil 672d4fd0404SClaudiu Manoil return IRQ_HANDLED; 673d4fd0404SClaudiu Manoil } 674d4fd0404SClaudiu Manoil 675ae0e6a5dSClaudiu Manoil static void enetc_rx_dim_work(struct work_struct *w) 676ae0e6a5dSClaudiu Manoil { 677ae0e6a5dSClaudiu Manoil struct dim *dim = container_of(w, struct dim, work); 678ae0e6a5dSClaudiu Manoil struct dim_cq_moder moder = 679ae0e6a5dSClaudiu Manoil net_dim_get_rx_moderation(dim->mode, dim->profile_ix); 680ae0e6a5dSClaudiu Manoil struct enetc_int_vector *v = 681ae0e6a5dSClaudiu Manoil container_of(dim, struct enetc_int_vector, rx_dim); 682ae0e6a5dSClaudiu Manoil 683ae0e6a5dSClaudiu Manoil v->rx_ictt = enetc_usecs_to_cycles(moder.usec); 684ae0e6a5dSClaudiu Manoil dim->state = DIM_START_MEASURE; 685ae0e6a5dSClaudiu Manoil } 686ae0e6a5dSClaudiu Manoil 687ae0e6a5dSClaudiu Manoil static void enetc_rx_net_dim(struct enetc_int_vector *v) 688ae0e6a5dSClaudiu Manoil { 6899f7afa05SClaudiu Manoil struct dim_sample dim_sample = {}; 690ae0e6a5dSClaudiu Manoil 691ae0e6a5dSClaudiu Manoil v->comp_cnt++; 692ae0e6a5dSClaudiu Manoil 693ae0e6a5dSClaudiu Manoil if (!v->rx_napi_work) 694ae0e6a5dSClaudiu Manoil return; 695ae0e6a5dSClaudiu Manoil 696ae0e6a5dSClaudiu Manoil dim_update_sample(v->comp_cnt, 697ae0e6a5dSClaudiu Manoil v->rx_ring.stats.packets, 698ae0e6a5dSClaudiu Manoil v->rx_ring.stats.bytes, 699ae0e6a5dSClaudiu Manoil &dim_sample); 700ae0e6a5dSClaudiu Manoil net_dim(&v->rx_dim, dim_sample); 701ae0e6a5dSClaudiu Manoil } 702ae0e6a5dSClaudiu Manoil 703d4fd0404SClaudiu Manoil static int enetc_bd_ready_count(struct enetc_bdr *tx_ring, int ci) 704d4fd0404SClaudiu Manoil { 705fd5736bfSAlex Marginean int pi = enetc_rd_reg_hot(tx_ring->tcir) & ENETC_TBCIR_IDX_MASK; 706d4fd0404SClaudiu Manoil 707d4fd0404SClaudiu Manoil return pi >= ci ? pi - ci : tx_ring->bd_count - ci + pi; 708d4fd0404SClaudiu Manoil } 709d4fd0404SClaudiu Manoil 71065d0cbb4SVladimir Oltean static bool enetc_page_reusable(struct page *page) 71165d0cbb4SVladimir Oltean { 71265d0cbb4SVladimir Oltean return (!page_is_pfmemalloc(page) && page_ref_count(page) == 1); 71365d0cbb4SVladimir Oltean } 71465d0cbb4SVladimir Oltean 71565d0cbb4SVladimir Oltean static void enetc_reuse_page(struct enetc_bdr *rx_ring, 71665d0cbb4SVladimir Oltean struct enetc_rx_swbd *old) 71765d0cbb4SVladimir Oltean { 71865d0cbb4SVladimir Oltean struct enetc_rx_swbd *new; 71965d0cbb4SVladimir Oltean 72065d0cbb4SVladimir Oltean new = &rx_ring->rx_swbd[rx_ring->next_to_alloc]; 72165d0cbb4SVladimir Oltean 72265d0cbb4SVladimir Oltean /* next buf that may reuse a page */ 72365d0cbb4SVladimir Oltean enetc_bdr_idx_inc(rx_ring, &rx_ring->next_to_alloc); 72465d0cbb4SVladimir Oltean 72565d0cbb4SVladimir Oltean /* copy page reference */ 72665d0cbb4SVladimir Oltean *new = *old; 72765d0cbb4SVladimir Oltean } 72865d0cbb4SVladimir Oltean 729d3982312SY.b. Lu static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd, 730d3982312SY.b. Lu u64 *tstamp) 731d3982312SY.b. Lu { 732cec4f328SY.b. Lu u32 lo, hi, tstamp_lo; 733d3982312SY.b. Lu 7346d36ecdbSVladimir Oltean lo = enetc_rd_hot(hw, ENETC_SICTR0); 7356d36ecdbSVladimir Oltean hi = enetc_rd_hot(hw, ENETC_SICTR1); 736cec4f328SY.b. Lu tstamp_lo = le32_to_cpu(txbd->wb.tstamp); 737cec4f328SY.b. Lu if (lo <= tstamp_lo) 738d3982312SY.b. Lu hi -= 1; 739cec4f328SY.b. Lu *tstamp = (u64)hi << 32 | tstamp_lo; 740d3982312SY.b. Lu } 741d3982312SY.b. Lu 742d3982312SY.b. Lu static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp) 743d3982312SY.b. Lu { 744d3982312SY.b. Lu struct skb_shared_hwtstamps shhwtstamps; 745d3982312SY.b. Lu 746d3982312SY.b. Lu if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) { 747d3982312SY.b. Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 748d3982312SY.b. Lu shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 749847cbfc0SVladimir Oltean skb_txtime_consumed(skb); 750d3982312SY.b. Lu skb_tstamp_tx(skb, &shhwtstamps); 751d3982312SY.b. Lu } 752d3982312SY.b. Lu } 753d3982312SY.b. Lu 7547ed2bc80SVladimir Oltean static void enetc_recycle_xdp_tx_buff(struct enetc_bdr *tx_ring, 7557ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd) 7567ed2bc80SVladimir Oltean { 7577ed2bc80SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev); 7587ed2bc80SVladimir Oltean struct enetc_rx_swbd rx_swbd = { 7597ed2bc80SVladimir Oltean .dma = tx_swbd->dma, 7607ed2bc80SVladimir Oltean .page = tx_swbd->page, 7617ed2bc80SVladimir Oltean .page_offset = tx_swbd->page_offset, 7627ed2bc80SVladimir Oltean .dir = tx_swbd->dir, 7637ed2bc80SVladimir Oltean .len = tx_swbd->len, 7647ed2bc80SVladimir Oltean }; 7657eab503bSVladimir Oltean struct enetc_bdr *rx_ring; 7667eab503bSVladimir Oltean 7677eab503bSVladimir Oltean rx_ring = enetc_rx_ring_from_xdp_tx_ring(priv, tx_ring); 7687ed2bc80SVladimir Oltean 7697ed2bc80SVladimir Oltean if (likely(enetc_swbd_unused(rx_ring))) { 7707ed2bc80SVladimir Oltean enetc_reuse_page(rx_ring, &rx_swbd); 7717ed2bc80SVladimir Oltean 7727ed2bc80SVladimir Oltean /* sync for use by the device */ 7737ed2bc80SVladimir Oltean dma_sync_single_range_for_device(rx_ring->dev, rx_swbd.dma, 7747ed2bc80SVladimir Oltean rx_swbd.page_offset, 7757ed2bc80SVladimir Oltean ENETC_RXB_DMA_SIZE_XDP, 7767ed2bc80SVladimir Oltean rx_swbd.dir); 7777ed2bc80SVladimir Oltean 7787ed2bc80SVladimir Oltean rx_ring->stats.recycles++; 7797ed2bc80SVladimir Oltean } else { 7807ed2bc80SVladimir Oltean /* RX ring is already full, we need to unmap and free the 7817ed2bc80SVladimir Oltean * page, since there's nothing useful we can do with it. 7827ed2bc80SVladimir Oltean */ 7837ed2bc80SVladimir Oltean rx_ring->stats.recycle_failures++; 7847ed2bc80SVladimir Oltean 7857ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd.dma, PAGE_SIZE, 7867ed2bc80SVladimir Oltean rx_swbd.dir); 7877ed2bc80SVladimir Oltean __free_page(rx_swbd.page); 7887ed2bc80SVladimir Oltean } 7897ed2bc80SVladimir Oltean 7907ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight--; 7917ed2bc80SVladimir Oltean } 7927ed2bc80SVladimir Oltean 793d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget) 794d4fd0404SClaudiu Manoil { 795d4fd0404SClaudiu Manoil struct net_device *ndev = tx_ring->ndev; 7967294380cSYangbo Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 797d4fd0404SClaudiu Manoil int tx_frm_cnt = 0, tx_byte_cnt = 0; 798d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd; 799d4fd0404SClaudiu Manoil int i, bds_to_clean; 8007294380cSYangbo Lu bool do_twostep_tstamp; 801d3982312SY.b. Lu u64 tstamp = 0; 802d4fd0404SClaudiu Manoil 803d4fd0404SClaudiu Manoil i = tx_ring->next_to_clean; 804d4fd0404SClaudiu Manoil tx_swbd = &tx_ring->tx_swbd[i]; 805fd5736bfSAlex Marginean 806d4fd0404SClaudiu Manoil bds_to_clean = enetc_bd_ready_count(tx_ring, i); 807d4fd0404SClaudiu Manoil 8087294380cSYangbo Lu do_twostep_tstamp = false; 809d3982312SY.b. Lu 810d4fd0404SClaudiu Manoil while (bds_to_clean && tx_frm_cnt < ENETC_DEFAULT_TX_WORK) { 8119d2b68ccSVladimir Oltean struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd); 8129d2b68ccSVladimir Oltean struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd); 813a93580a0SVladimir Oltean bool is_eof = tx_swbd->is_eof; 8149d2b68ccSVladimir Oltean 815d3982312SY.b. Lu if (unlikely(tx_swbd->check_wb)) { 816ae77bdbcSVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 817d3982312SY.b. Lu 818d3982312SY.b. Lu if (txbd->flags & ENETC_TXBD_FLAGS_W && 8197294380cSYangbo Lu tx_swbd->do_twostep_tstamp) { 820d3982312SY.b. Lu enetc_get_tx_tstamp(&priv->si->hw, txbd, 821d3982312SY.b. Lu &tstamp); 8227294380cSYangbo Lu do_twostep_tstamp = true; 823d3982312SY.b. Lu } 824d3982312SY.b. Lu } 825d3982312SY.b. Lu 8267ed2bc80SVladimir Oltean if (tx_swbd->is_xdp_tx) 8277ed2bc80SVladimir Oltean enetc_recycle_xdp_tx_buff(tx_ring, tx_swbd); 8287ed2bc80SVladimir Oltean else if (likely(tx_swbd->dma)) 829d4fd0404SClaudiu Manoil enetc_unmap_tx_buff(tx_ring, tx_swbd); 830f4a0be84SClaudiu Manoil 8319d2b68ccSVladimir Oltean if (xdp_frame) { 8329d2b68ccSVladimir Oltean xdp_return_frame(xdp_frame); 8339d2b68ccSVladimir Oltean } else if (skb) { 834*52066149SVladimir Oltean if (unlikely(skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP)) { 8357294380cSYangbo Lu /* Start work to release lock for next one-step 8367294380cSYangbo Lu * timestamping packet. And send one skb in 8377294380cSYangbo Lu * tx_skbs queue if has. 8387294380cSYangbo Lu */ 839b6faf160SYangbo Lu schedule_work(&priv->tx_onestep_tstamp); 8407294380cSYangbo Lu } else if (unlikely(do_twostep_tstamp)) { 8419d2b68ccSVladimir Oltean enetc_tstamp_tx(skb, tstamp); 8427294380cSYangbo Lu do_twostep_tstamp = false; 843d3982312SY.b. Lu } 8449d2b68ccSVladimir Oltean napi_consume_skb(skb, napi_budget); 845d4fd0404SClaudiu Manoil } 846d4fd0404SClaudiu Manoil 847d4fd0404SClaudiu Manoil tx_byte_cnt += tx_swbd->len; 8481ee8d6f3SVladimir Oltean /* Scrub the swbd here so we don't have to do that 8491ee8d6f3SVladimir Oltean * when we reuse it during xmit 8501ee8d6f3SVladimir Oltean */ 8511ee8d6f3SVladimir Oltean memset(tx_swbd, 0, sizeof(*tx_swbd)); 852d4fd0404SClaudiu Manoil 853d4fd0404SClaudiu Manoil bds_to_clean--; 854d4fd0404SClaudiu Manoil tx_swbd++; 855d4fd0404SClaudiu Manoil i++; 856d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 857d4fd0404SClaudiu Manoil i = 0; 858d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 859d4fd0404SClaudiu Manoil } 860d4fd0404SClaudiu Manoil 861d4fd0404SClaudiu Manoil /* BD iteration loop end */ 862a93580a0SVladimir Oltean if (is_eof) { 863d4fd0404SClaudiu Manoil tx_frm_cnt++; 864d4fd0404SClaudiu Manoil /* re-arm interrupt source */ 865fd5736bfSAlex Marginean enetc_wr_reg_hot(tx_ring->idr, BIT(tx_ring->index) | 866d4fd0404SClaudiu Manoil BIT(16 + tx_ring->index)); 867d4fd0404SClaudiu Manoil } 868d4fd0404SClaudiu Manoil 869d4fd0404SClaudiu Manoil if (unlikely(!bds_to_clean)) 870d4fd0404SClaudiu Manoil bds_to_clean = enetc_bd_ready_count(tx_ring, i); 871d4fd0404SClaudiu Manoil } 872d4fd0404SClaudiu Manoil 873d4fd0404SClaudiu Manoil tx_ring->next_to_clean = i; 874d4fd0404SClaudiu Manoil tx_ring->stats.packets += tx_frm_cnt; 875d4fd0404SClaudiu Manoil tx_ring->stats.bytes += tx_byte_cnt; 876d4fd0404SClaudiu Manoil 877d4fd0404SClaudiu Manoil if (unlikely(tx_frm_cnt && netif_carrier_ok(ndev) && 878d4fd0404SClaudiu Manoil __netif_subqueue_stopped(ndev, tx_ring->index) && 879d4fd0404SClaudiu Manoil (enetc_bd_unused(tx_ring) >= ENETC_TXBDS_MAX_NEEDED))) { 880d4fd0404SClaudiu Manoil netif_wake_subqueue(ndev, tx_ring->index); 881d4fd0404SClaudiu Manoil } 882d4fd0404SClaudiu Manoil 883d4fd0404SClaudiu Manoil return tx_frm_cnt != ENETC_DEFAULT_TX_WORK; 884d4fd0404SClaudiu Manoil } 885d4fd0404SClaudiu Manoil 886d4fd0404SClaudiu Manoil static bool enetc_new_page(struct enetc_bdr *rx_ring, 887d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd) 888d4fd0404SClaudiu Manoil { 8897ed2bc80SVladimir Oltean bool xdp = !!(rx_ring->xdp.prog); 890d4fd0404SClaudiu Manoil struct page *page; 891d4fd0404SClaudiu Manoil dma_addr_t addr; 892d4fd0404SClaudiu Manoil 893d4fd0404SClaudiu Manoil page = dev_alloc_page(); 894d4fd0404SClaudiu Manoil if (unlikely(!page)) 895d4fd0404SClaudiu Manoil return false; 896d4fd0404SClaudiu Manoil 8977ed2bc80SVladimir Oltean /* For XDP_TX, we forgo dma_unmap -> dma_map */ 8987ed2bc80SVladimir Oltean rx_swbd->dir = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE; 8997ed2bc80SVladimir Oltean 9007ed2bc80SVladimir Oltean addr = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, rx_swbd->dir); 901d4fd0404SClaudiu Manoil if (unlikely(dma_mapping_error(rx_ring->dev, addr))) { 902d4fd0404SClaudiu Manoil __free_page(page); 903d4fd0404SClaudiu Manoil 904d4fd0404SClaudiu Manoil return false; 905d4fd0404SClaudiu Manoil } 906d4fd0404SClaudiu Manoil 907d4fd0404SClaudiu Manoil rx_swbd->dma = addr; 908d4fd0404SClaudiu Manoil rx_swbd->page = page; 909d1b15102SVladimir Oltean rx_swbd->page_offset = rx_ring->buffer_offset; 910d4fd0404SClaudiu Manoil 911d4fd0404SClaudiu Manoil return true; 912d4fd0404SClaudiu Manoil } 913d4fd0404SClaudiu Manoil 914d4fd0404SClaudiu Manoil static int enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt) 915d4fd0404SClaudiu Manoil { 916d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd; 917d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd; 918d4fd0404SClaudiu Manoil int i, j; 919d4fd0404SClaudiu Manoil 920d4fd0404SClaudiu Manoil i = rx_ring->next_to_use; 921d4fd0404SClaudiu Manoil rx_swbd = &rx_ring->rx_swbd[i]; 922714239acSClaudiu Manoil rxbd = enetc_rxbd(rx_ring, i); 923d4fd0404SClaudiu Manoil 924d4fd0404SClaudiu Manoil for (j = 0; j < buff_cnt; j++) { 925d4fd0404SClaudiu Manoil /* try reuse page */ 926d4fd0404SClaudiu Manoil if (unlikely(!rx_swbd->page)) { 927d4fd0404SClaudiu Manoil if (unlikely(!enetc_new_page(rx_ring, rx_swbd))) { 928d4fd0404SClaudiu Manoil rx_ring->stats.rx_alloc_errs++; 929d4fd0404SClaudiu Manoil break; 930d4fd0404SClaudiu Manoil } 931d4fd0404SClaudiu Manoil } 932d4fd0404SClaudiu Manoil 933d4fd0404SClaudiu Manoil /* update RxBD */ 934d4fd0404SClaudiu Manoil rxbd->w.addr = cpu_to_le64(rx_swbd->dma + 935d4fd0404SClaudiu Manoil rx_swbd->page_offset); 936d4fd0404SClaudiu Manoil /* clear 'R" as well */ 937d4fd0404SClaudiu Manoil rxbd->r.lstatus = 0; 938d4fd0404SClaudiu Manoil 939c027aa92SVladimir Oltean enetc_rxbd_next(rx_ring, &rxbd, &i); 940c027aa92SVladimir Oltean rx_swbd = &rx_ring->rx_swbd[i]; 941d4fd0404SClaudiu Manoil } 942d4fd0404SClaudiu Manoil 943d4fd0404SClaudiu Manoil if (likely(j)) { 944d4fd0404SClaudiu Manoil rx_ring->next_to_alloc = i; /* keep track from page reuse */ 945d4fd0404SClaudiu Manoil rx_ring->next_to_use = i; 9467a5222cbSVladimir Oltean 9477a5222cbSVladimir Oltean /* update ENETC's consumer index */ 9487a5222cbSVladimir Oltean enetc_wr_reg_hot(rx_ring->rcir, rx_ring->next_to_use); 949d4fd0404SClaudiu Manoil } 950d4fd0404SClaudiu Manoil 951d4fd0404SClaudiu Manoil return j; 952d4fd0404SClaudiu Manoil } 953d4fd0404SClaudiu Manoil 954434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 955d3982312SY.b. Lu static void enetc_get_rx_tstamp(struct net_device *ndev, 956d3982312SY.b. Lu union enetc_rx_bd *rxbd, 957d3982312SY.b. Lu struct sk_buff *skb) 958d3982312SY.b. Lu { 959d3982312SY.b. Lu struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 960d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 961d3982312SY.b. Lu struct enetc_hw *hw = &priv->si->hw; 962cec4f328SY.b. Lu u32 lo, hi, tstamp_lo; 963d3982312SY.b. Lu u64 tstamp; 964d3982312SY.b. Lu 965cec4f328SY.b. Lu if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) { 966fd5736bfSAlex Marginean lo = enetc_rd_reg_hot(hw->reg + ENETC_SICTR0); 967fd5736bfSAlex Marginean hi = enetc_rd_reg_hot(hw->reg + ENETC_SICTR1); 968434cebabSClaudiu Manoil rxbd = enetc_rxbd_ext(rxbd); 969434cebabSClaudiu Manoil tstamp_lo = le32_to_cpu(rxbd->ext.tstamp); 970cec4f328SY.b. Lu if (lo <= tstamp_lo) 971d3982312SY.b. Lu hi -= 1; 972d3982312SY.b. Lu 973cec4f328SY.b. Lu tstamp = (u64)hi << 32 | tstamp_lo; 974d3982312SY.b. Lu memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 975d3982312SY.b. Lu shhwtstamps->hwtstamp = ns_to_ktime(tstamp); 976d3982312SY.b. Lu } 977d3982312SY.b. Lu } 978d3982312SY.b. Lu #endif 979d3982312SY.b. Lu 980d4fd0404SClaudiu Manoil static void enetc_get_offloads(struct enetc_bdr *rx_ring, 981d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd, struct sk_buff *skb) 982d4fd0404SClaudiu Manoil { 983d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev); 984827b6fd0SVladimir Oltean 985d3982312SY.b. Lu /* TODO: hashing */ 986d4fd0404SClaudiu Manoil if (rx_ring->ndev->features & NETIF_F_RXCSUM) { 987d4fd0404SClaudiu Manoil u16 inet_csum = le16_to_cpu(rxbd->r.inet_csum); 988d4fd0404SClaudiu Manoil 989d4fd0404SClaudiu Manoil skb->csum = csum_unfold((__force __sum16)~htons(inet_csum)); 990d4fd0404SClaudiu Manoil skb->ip_summed = CHECKSUM_COMPLETE; 991d4fd0404SClaudiu Manoil } 992d4fd0404SClaudiu Manoil 993827b6fd0SVladimir Oltean if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_VLAN) { 994827b6fd0SVladimir Oltean __be16 tpid = 0; 995827b6fd0SVladimir Oltean 996827b6fd0SVladimir Oltean switch (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TPID) { 997827b6fd0SVladimir Oltean case 0: 998827b6fd0SVladimir Oltean tpid = htons(ETH_P_8021Q); 999827b6fd0SVladimir Oltean break; 1000827b6fd0SVladimir Oltean case 1: 1001827b6fd0SVladimir Oltean tpid = htons(ETH_P_8021AD); 1002827b6fd0SVladimir Oltean break; 1003827b6fd0SVladimir Oltean case 2: 1004827b6fd0SVladimir Oltean tpid = htons(enetc_port_rd(&priv->si->hw, 1005827b6fd0SVladimir Oltean ENETC_PCVLANR1)); 1006827b6fd0SVladimir Oltean break; 1007827b6fd0SVladimir Oltean case 3: 1008827b6fd0SVladimir Oltean tpid = htons(enetc_port_rd(&priv->si->hw, 1009827b6fd0SVladimir Oltean ENETC_PCVLANR2)); 1010827b6fd0SVladimir Oltean break; 1011827b6fd0SVladimir Oltean default: 1012827b6fd0SVladimir Oltean break; 1013827b6fd0SVladimir Oltean } 1014827b6fd0SVladimir Oltean 1015827b6fd0SVladimir Oltean __vlan_hwaccel_put_tag(skb, tpid, le16_to_cpu(rxbd->r.vlan_opt)); 1016827b6fd0SVladimir Oltean } 1017827b6fd0SVladimir Oltean 1018434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 1019d3982312SY.b. Lu if (priv->active_offloads & ENETC_F_RX_TSTAMP) 1020d3982312SY.b. Lu enetc_get_rx_tstamp(rx_ring->ndev, rxbd, skb); 1021d3982312SY.b. Lu #endif 1022d4fd0404SClaudiu Manoil } 1023d4fd0404SClaudiu Manoil 10247ed2bc80SVladimir Oltean /* This gets called during the non-XDP NAPI poll cycle as well as on XDP_PASS, 10257ed2bc80SVladimir Oltean * so it needs to work with both DMA_FROM_DEVICE as well as DMA_BIDIRECTIONAL 10267ed2bc80SVladimir Oltean * mapped buffers. 10277ed2bc80SVladimir Oltean */ 1028d4fd0404SClaudiu Manoil static struct enetc_rx_swbd *enetc_get_rx_buff(struct enetc_bdr *rx_ring, 1029d4fd0404SClaudiu Manoil int i, u16 size) 1030d4fd0404SClaudiu Manoil { 1031d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i]; 1032d4fd0404SClaudiu Manoil 1033d4fd0404SClaudiu Manoil dma_sync_single_range_for_cpu(rx_ring->dev, rx_swbd->dma, 1034d4fd0404SClaudiu Manoil rx_swbd->page_offset, 10357ed2bc80SVladimir Oltean size, rx_swbd->dir); 1036d4fd0404SClaudiu Manoil return rx_swbd; 1037d4fd0404SClaudiu Manoil } 1038d4fd0404SClaudiu Manoil 10396b04830dSVladimir Oltean /* Reuse the current page without performing half-page buffer flipping */ 1040d4fd0404SClaudiu Manoil static void enetc_put_rx_buff(struct enetc_bdr *rx_ring, 1041d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd) 1042d4fd0404SClaudiu Manoil { 1043d1b15102SVladimir Oltean size_t buffer_size = ENETC_RXB_TRUESIZE - rx_ring->buffer_offset; 1044d1b15102SVladimir Oltean 1045d4fd0404SClaudiu Manoil enetc_reuse_page(rx_ring, rx_swbd); 1046d4fd0404SClaudiu Manoil 1047d4fd0404SClaudiu Manoil dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma, 1048d4fd0404SClaudiu Manoil rx_swbd->page_offset, 10497ed2bc80SVladimir Oltean buffer_size, rx_swbd->dir); 10506b04830dSVladimir Oltean 10516b04830dSVladimir Oltean rx_swbd->page = NULL; 10526b04830dSVladimir Oltean } 10536b04830dSVladimir Oltean 10546b04830dSVladimir Oltean /* Reuse the current page by performing half-page buffer flipping */ 10556b04830dSVladimir Oltean static void enetc_flip_rx_buff(struct enetc_bdr *rx_ring, 10566b04830dSVladimir Oltean struct enetc_rx_swbd *rx_swbd) 10576b04830dSVladimir Oltean { 10586b04830dSVladimir Oltean if (likely(enetc_page_reusable(rx_swbd->page))) { 10596b04830dSVladimir Oltean rx_swbd->page_offset ^= ENETC_RXB_TRUESIZE; 10606b04830dSVladimir Oltean page_ref_inc(rx_swbd->page); 10616b04830dSVladimir Oltean 10626b04830dSVladimir Oltean enetc_put_rx_buff(rx_ring, rx_swbd); 1063d4fd0404SClaudiu Manoil } else { 10647ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE, 10657ed2bc80SVladimir Oltean rx_swbd->dir); 1066d4fd0404SClaudiu Manoil rx_swbd->page = NULL; 1067d4fd0404SClaudiu Manoil } 10686b04830dSVladimir Oltean } 1069d4fd0404SClaudiu Manoil 1070d4fd0404SClaudiu Manoil static struct sk_buff *enetc_map_rx_buff_to_skb(struct enetc_bdr *rx_ring, 1071d4fd0404SClaudiu Manoil int i, u16 size) 1072d4fd0404SClaudiu Manoil { 1073d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1074d4fd0404SClaudiu Manoil struct sk_buff *skb; 1075d4fd0404SClaudiu Manoil void *ba; 1076d4fd0404SClaudiu Manoil 1077d4fd0404SClaudiu Manoil ba = page_address(rx_swbd->page) + rx_swbd->page_offset; 1078d1b15102SVladimir Oltean skb = build_skb(ba - rx_ring->buffer_offset, ENETC_RXB_TRUESIZE); 1079d4fd0404SClaudiu Manoil if (unlikely(!skb)) { 1080d4fd0404SClaudiu Manoil rx_ring->stats.rx_alloc_errs++; 1081d4fd0404SClaudiu Manoil return NULL; 1082d4fd0404SClaudiu Manoil } 1083d4fd0404SClaudiu Manoil 1084d1b15102SVladimir Oltean skb_reserve(skb, rx_ring->buffer_offset); 1085d4fd0404SClaudiu Manoil __skb_put(skb, size); 1086d4fd0404SClaudiu Manoil 10876b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, rx_swbd); 1088d4fd0404SClaudiu Manoil 1089d4fd0404SClaudiu Manoil return skb; 1090d4fd0404SClaudiu Manoil } 1091d4fd0404SClaudiu Manoil 1092d4fd0404SClaudiu Manoil static void enetc_add_rx_buff_to_skb(struct enetc_bdr *rx_ring, int i, 1093d4fd0404SClaudiu Manoil u16 size, struct sk_buff *skb) 1094d4fd0404SClaudiu Manoil { 1095d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1096d4fd0404SClaudiu Manoil 1097d4fd0404SClaudiu Manoil skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_swbd->page, 1098d4fd0404SClaudiu Manoil rx_swbd->page_offset, size, ENETC_RXB_TRUESIZE); 1099d4fd0404SClaudiu Manoil 11006b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, rx_swbd); 1101d4fd0404SClaudiu Manoil } 1102d4fd0404SClaudiu Manoil 11032fa423f5SVladimir Oltean static bool enetc_check_bd_errors_and_consume(struct enetc_bdr *rx_ring, 11042fa423f5SVladimir Oltean u32 bd_status, 11052fa423f5SVladimir Oltean union enetc_rx_bd **rxbd, int *i) 11062fa423f5SVladimir Oltean { 11072fa423f5SVladimir Oltean if (likely(!(bd_status & ENETC_RXBD_LSTATUS(ENETC_RXBD_ERR_MASK)))) 11082fa423f5SVladimir Oltean return false; 11092fa423f5SVladimir Oltean 1110672f9a21SVladimir Oltean enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]); 11112fa423f5SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 11122fa423f5SVladimir Oltean 11132fa423f5SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 11142fa423f5SVladimir Oltean dma_rmb(); 11152fa423f5SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 11162fa423f5SVladimir Oltean 1117672f9a21SVladimir Oltean enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]); 11182fa423f5SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 11192fa423f5SVladimir Oltean } 11202fa423f5SVladimir Oltean 11212fa423f5SVladimir Oltean rx_ring->ndev->stats.rx_dropped++; 11222fa423f5SVladimir Oltean rx_ring->ndev->stats.rx_errors++; 11232fa423f5SVladimir Oltean 11242fa423f5SVladimir Oltean return true; 11252fa423f5SVladimir Oltean } 11262fa423f5SVladimir Oltean 1127a800abd3SVladimir Oltean static struct sk_buff *enetc_build_skb(struct enetc_bdr *rx_ring, 1128a800abd3SVladimir Oltean u32 bd_status, union enetc_rx_bd **rxbd, 1129d1b15102SVladimir Oltean int *i, int *cleaned_cnt, int buffer_size) 1130a800abd3SVladimir Oltean { 1131a800abd3SVladimir Oltean struct sk_buff *skb; 1132a800abd3SVladimir Oltean u16 size; 1133a800abd3SVladimir Oltean 1134a800abd3SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1135a800abd3SVladimir Oltean skb = enetc_map_rx_buff_to_skb(rx_ring, *i, size); 1136a800abd3SVladimir Oltean if (!skb) 1137a800abd3SVladimir Oltean return NULL; 1138a800abd3SVladimir Oltean 1139a800abd3SVladimir Oltean enetc_get_offloads(rx_ring, *rxbd, skb); 1140a800abd3SVladimir Oltean 1141a800abd3SVladimir Oltean (*cleaned_cnt)++; 1142a800abd3SVladimir Oltean 1143a800abd3SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1144a800abd3SVladimir Oltean 1145a800abd3SVladimir Oltean /* not last BD in frame? */ 1146a800abd3SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 1147a800abd3SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 1148d1b15102SVladimir Oltean size = buffer_size; 1149a800abd3SVladimir Oltean 1150a800abd3SVladimir Oltean if (bd_status & ENETC_RXBD_LSTATUS_F) { 1151a800abd3SVladimir Oltean dma_rmb(); 1152a800abd3SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1153a800abd3SVladimir Oltean } 1154a800abd3SVladimir Oltean 1155a800abd3SVladimir Oltean enetc_add_rx_buff_to_skb(rx_ring, *i, size, skb); 1156a800abd3SVladimir Oltean 1157a800abd3SVladimir Oltean (*cleaned_cnt)++; 1158a800abd3SVladimir Oltean 1159a800abd3SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1160a800abd3SVladimir Oltean } 1161a800abd3SVladimir Oltean 1162a800abd3SVladimir Oltean skb_record_rx_queue(skb, rx_ring->index); 1163a800abd3SVladimir Oltean skb->protocol = eth_type_trans(skb, rx_ring->ndev); 1164a800abd3SVladimir Oltean 1165a800abd3SVladimir Oltean return skb; 1166a800abd3SVladimir Oltean } 1167a800abd3SVladimir Oltean 1168d4fd0404SClaudiu Manoil #define ENETC_RXBD_BUNDLE 16 /* # of BDs to update at once */ 1169d4fd0404SClaudiu Manoil 1170d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring, 1171d4fd0404SClaudiu Manoil struct napi_struct *napi, int work_limit) 1172d4fd0404SClaudiu Manoil { 1173d4fd0404SClaudiu Manoil int rx_frm_cnt = 0, rx_byte_cnt = 0; 1174d4fd0404SClaudiu Manoil int cleaned_cnt, i; 1175d4fd0404SClaudiu Manoil 1176d4fd0404SClaudiu Manoil cleaned_cnt = enetc_bd_unused(rx_ring); 1177d4fd0404SClaudiu Manoil /* next descriptor to process */ 1178d4fd0404SClaudiu Manoil i = rx_ring->next_to_clean; 1179d4fd0404SClaudiu Manoil 1180d4fd0404SClaudiu Manoil while (likely(rx_frm_cnt < work_limit)) { 1181d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd; 1182d4fd0404SClaudiu Manoil struct sk_buff *skb; 1183d4fd0404SClaudiu Manoil u32 bd_status; 1184d4fd0404SClaudiu Manoil 11857a5222cbSVladimir Oltean if (cleaned_cnt >= ENETC_RXBD_BUNDLE) 11867a5222cbSVladimir Oltean cleaned_cnt -= enetc_refill_rx_ring(rx_ring, 11877a5222cbSVladimir Oltean cleaned_cnt); 1188d4fd0404SClaudiu Manoil 1189714239acSClaudiu Manoil rxbd = enetc_rxbd(rx_ring, i); 1190d4fd0404SClaudiu Manoil bd_status = le32_to_cpu(rxbd->r.lstatus); 11916d36ecdbSVladimir Oltean if (!bd_status) 1192d4fd0404SClaudiu Manoil break; 1193d4fd0404SClaudiu Manoil 1194fd5736bfSAlex Marginean enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index)); 1195d4fd0404SClaudiu Manoil dma_rmb(); /* for reading other rxbd fields */ 11962fa423f5SVladimir Oltean 11972fa423f5SVladimir Oltean if (enetc_check_bd_errors_and_consume(rx_ring, bd_status, 11982fa423f5SVladimir Oltean &rxbd, &i)) 11992fa423f5SVladimir Oltean break; 12002fa423f5SVladimir Oltean 1201a800abd3SVladimir Oltean skb = enetc_build_skb(rx_ring, bd_status, &rxbd, &i, 1202d1b15102SVladimir Oltean &cleaned_cnt, ENETC_RXB_DMA_SIZE); 12036d36ecdbSVladimir Oltean if (!skb) 1204d4fd0404SClaudiu Manoil break; 1205d4fd0404SClaudiu Manoil 1206d4fd0404SClaudiu Manoil rx_byte_cnt += skb->len; 1207a800abd3SVladimir Oltean rx_frm_cnt++; 1208d4fd0404SClaudiu Manoil 1209d4fd0404SClaudiu Manoil napi_gro_receive(napi, skb); 1210d4fd0404SClaudiu Manoil } 1211d4fd0404SClaudiu Manoil 1212d4fd0404SClaudiu Manoil rx_ring->next_to_clean = i; 1213d4fd0404SClaudiu Manoil 1214d4fd0404SClaudiu Manoil rx_ring->stats.packets += rx_frm_cnt; 1215d4fd0404SClaudiu Manoil rx_ring->stats.bytes += rx_byte_cnt; 1216d4fd0404SClaudiu Manoil 1217d4fd0404SClaudiu Manoil return rx_frm_cnt; 1218d4fd0404SClaudiu Manoil } 1219d4fd0404SClaudiu Manoil 12207ed2bc80SVladimir Oltean static void enetc_xdp_map_tx_buff(struct enetc_bdr *tx_ring, int i, 12217ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd, 12227ed2bc80SVladimir Oltean int frm_len) 12237ed2bc80SVladimir Oltean { 12247ed2bc80SVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 12257ed2bc80SVladimir Oltean 12267ed2bc80SVladimir Oltean prefetchw(txbd); 12277ed2bc80SVladimir Oltean 12287ed2bc80SVladimir Oltean enetc_clear_tx_bd(txbd); 12297ed2bc80SVladimir Oltean txbd->addr = cpu_to_le64(tx_swbd->dma + tx_swbd->page_offset); 12307ed2bc80SVladimir Oltean txbd->buf_len = cpu_to_le16(tx_swbd->len); 12317ed2bc80SVladimir Oltean txbd->frm_len = cpu_to_le16(frm_len); 12327ed2bc80SVladimir Oltean 12337ed2bc80SVladimir Oltean memcpy(&tx_ring->tx_swbd[i], tx_swbd, sizeof(*tx_swbd)); 12347ed2bc80SVladimir Oltean } 12357ed2bc80SVladimir Oltean 12367ed2bc80SVladimir Oltean /* Puts in the TX ring one XDP frame, mapped as an array of TX software buffer 12377ed2bc80SVladimir Oltean * descriptors. 12387ed2bc80SVladimir Oltean */ 12397ed2bc80SVladimir Oltean static bool enetc_xdp_tx(struct enetc_bdr *tx_ring, 12407ed2bc80SVladimir Oltean struct enetc_tx_swbd *xdp_tx_arr, int num_tx_swbd) 12417ed2bc80SVladimir Oltean { 12427ed2bc80SVladimir Oltean struct enetc_tx_swbd *tmp_tx_swbd = xdp_tx_arr; 12437ed2bc80SVladimir Oltean int i, k, frm_len = tmp_tx_swbd->len; 12447ed2bc80SVladimir Oltean 12457ed2bc80SVladimir Oltean if (unlikely(enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(num_tx_swbd))) 12467ed2bc80SVladimir Oltean return false; 12477ed2bc80SVladimir Oltean 12487ed2bc80SVladimir Oltean while (unlikely(!tmp_tx_swbd->is_eof)) { 12497ed2bc80SVladimir Oltean tmp_tx_swbd++; 12507ed2bc80SVladimir Oltean frm_len += tmp_tx_swbd->len; 12517ed2bc80SVladimir Oltean } 12527ed2bc80SVladimir Oltean 12537ed2bc80SVladimir Oltean i = tx_ring->next_to_use; 12547ed2bc80SVladimir Oltean 12557ed2bc80SVladimir Oltean for (k = 0; k < num_tx_swbd; k++) { 12567ed2bc80SVladimir Oltean struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[k]; 12577ed2bc80SVladimir Oltean 12587ed2bc80SVladimir Oltean enetc_xdp_map_tx_buff(tx_ring, i, xdp_tx_swbd, frm_len); 12597ed2bc80SVladimir Oltean 12607ed2bc80SVladimir Oltean /* last BD needs 'F' bit set */ 12617ed2bc80SVladimir Oltean if (xdp_tx_swbd->is_eof) { 12627ed2bc80SVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 12637ed2bc80SVladimir Oltean 12647ed2bc80SVladimir Oltean txbd->flags = ENETC_TXBD_FLAGS_F; 12657ed2bc80SVladimir Oltean } 12667ed2bc80SVladimir Oltean 12677ed2bc80SVladimir Oltean enetc_bdr_idx_inc(tx_ring, &i); 12687ed2bc80SVladimir Oltean } 12697ed2bc80SVladimir Oltean 12707ed2bc80SVladimir Oltean tx_ring->next_to_use = i; 12717ed2bc80SVladimir Oltean 12727ed2bc80SVladimir Oltean return true; 12737ed2bc80SVladimir Oltean } 12747ed2bc80SVladimir Oltean 12759d2b68ccSVladimir Oltean static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring, 12769d2b68ccSVladimir Oltean struct enetc_tx_swbd *xdp_tx_arr, 12779d2b68ccSVladimir Oltean struct xdp_frame *xdp_frame) 12789d2b68ccSVladimir Oltean { 12799d2b68ccSVladimir Oltean struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[0]; 12809d2b68ccSVladimir Oltean struct skb_shared_info *shinfo; 12819d2b68ccSVladimir Oltean void *data = xdp_frame->data; 12829d2b68ccSVladimir Oltean int len = xdp_frame->len; 12839d2b68ccSVladimir Oltean skb_frag_t *frag; 12849d2b68ccSVladimir Oltean dma_addr_t dma; 12859d2b68ccSVladimir Oltean unsigned int f; 12869d2b68ccSVladimir Oltean int n = 0; 12879d2b68ccSVladimir Oltean 12889d2b68ccSVladimir Oltean dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE); 12899d2b68ccSVladimir Oltean if (unlikely(dma_mapping_error(tx_ring->dev, dma))) { 12909d2b68ccSVladimir Oltean netdev_err(tx_ring->ndev, "DMA map error\n"); 12919d2b68ccSVladimir Oltean return -1; 12929d2b68ccSVladimir Oltean } 12939d2b68ccSVladimir Oltean 12949d2b68ccSVladimir Oltean xdp_tx_swbd->dma = dma; 12959d2b68ccSVladimir Oltean xdp_tx_swbd->dir = DMA_TO_DEVICE; 12969d2b68ccSVladimir Oltean xdp_tx_swbd->len = len; 12979d2b68ccSVladimir Oltean xdp_tx_swbd->is_xdp_redirect = true; 12989d2b68ccSVladimir Oltean xdp_tx_swbd->is_eof = false; 12999d2b68ccSVladimir Oltean xdp_tx_swbd->xdp_frame = NULL; 13009d2b68ccSVladimir Oltean 13019d2b68ccSVladimir Oltean n++; 13029d2b68ccSVladimir Oltean xdp_tx_swbd = &xdp_tx_arr[n]; 13039d2b68ccSVladimir Oltean 13049d2b68ccSVladimir Oltean shinfo = xdp_get_shared_info_from_frame(xdp_frame); 13059d2b68ccSVladimir Oltean 13069d2b68ccSVladimir Oltean for (f = 0, frag = &shinfo->frags[0]; f < shinfo->nr_frags; 13079d2b68ccSVladimir Oltean f++, frag++) { 13089d2b68ccSVladimir Oltean data = skb_frag_address(frag); 13099d2b68ccSVladimir Oltean len = skb_frag_size(frag); 13109d2b68ccSVladimir Oltean 13119d2b68ccSVladimir Oltean dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE); 13129d2b68ccSVladimir Oltean if (unlikely(dma_mapping_error(tx_ring->dev, dma))) { 13139d2b68ccSVladimir Oltean /* Undo the DMA mapping for all fragments */ 1314626b598aSDan Carpenter while (--n >= 0) 13159d2b68ccSVladimir Oltean enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]); 13169d2b68ccSVladimir Oltean 13179d2b68ccSVladimir Oltean netdev_err(tx_ring->ndev, "DMA map error\n"); 13189d2b68ccSVladimir Oltean return -1; 13199d2b68ccSVladimir Oltean } 13209d2b68ccSVladimir Oltean 13219d2b68ccSVladimir Oltean xdp_tx_swbd->dma = dma; 13229d2b68ccSVladimir Oltean xdp_tx_swbd->dir = DMA_TO_DEVICE; 13239d2b68ccSVladimir Oltean xdp_tx_swbd->len = len; 13249d2b68ccSVladimir Oltean xdp_tx_swbd->is_xdp_redirect = true; 13259d2b68ccSVladimir Oltean xdp_tx_swbd->is_eof = false; 13269d2b68ccSVladimir Oltean xdp_tx_swbd->xdp_frame = NULL; 13279d2b68ccSVladimir Oltean 13289d2b68ccSVladimir Oltean n++; 13299d2b68ccSVladimir Oltean xdp_tx_swbd = &xdp_tx_arr[n]; 13309d2b68ccSVladimir Oltean } 13319d2b68ccSVladimir Oltean 13329d2b68ccSVladimir Oltean xdp_tx_arr[n - 1].is_eof = true; 13339d2b68ccSVladimir Oltean xdp_tx_arr[n - 1].xdp_frame = xdp_frame; 13349d2b68ccSVladimir Oltean 13359d2b68ccSVladimir Oltean return n; 13369d2b68ccSVladimir Oltean } 13379d2b68ccSVladimir Oltean 13389d2b68ccSVladimir Oltean int enetc_xdp_xmit(struct net_device *ndev, int num_frames, 13399d2b68ccSVladimir Oltean struct xdp_frame **frames, u32 flags) 13409d2b68ccSVladimir Oltean { 13419d2b68ccSVladimir Oltean struct enetc_tx_swbd xdp_redirect_arr[ENETC_MAX_SKB_FRAGS] = {0}; 13429d2b68ccSVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(ndev); 13439d2b68ccSVladimir Oltean struct enetc_bdr *tx_ring; 13449d2b68ccSVladimir Oltean int xdp_tx_bd_cnt, i, k; 13459d2b68ccSVladimir Oltean int xdp_tx_frm_cnt = 0; 13469d2b68ccSVladimir Oltean 134724e39309SVladimir Oltean enetc_lock_mdio(); 134824e39309SVladimir Oltean 13497eab503bSVladimir Oltean tx_ring = priv->xdp_tx_ring[smp_processor_id()]; 13509d2b68ccSVladimir Oltean 13519d2b68ccSVladimir Oltean prefetchw(ENETC_TXBD(*tx_ring, tx_ring->next_to_use)); 13529d2b68ccSVladimir Oltean 13539d2b68ccSVladimir Oltean for (k = 0; k < num_frames; k++) { 13549d2b68ccSVladimir Oltean xdp_tx_bd_cnt = enetc_xdp_frame_to_xdp_tx_swbd(tx_ring, 13559d2b68ccSVladimir Oltean xdp_redirect_arr, 13569d2b68ccSVladimir Oltean frames[k]); 13579d2b68ccSVladimir Oltean if (unlikely(xdp_tx_bd_cnt < 0)) 13589d2b68ccSVladimir Oltean break; 13599d2b68ccSVladimir Oltean 13609d2b68ccSVladimir Oltean if (unlikely(!enetc_xdp_tx(tx_ring, xdp_redirect_arr, 13619d2b68ccSVladimir Oltean xdp_tx_bd_cnt))) { 13629d2b68ccSVladimir Oltean for (i = 0; i < xdp_tx_bd_cnt; i++) 13639d2b68ccSVladimir Oltean enetc_unmap_tx_buff(tx_ring, 13649d2b68ccSVladimir Oltean &xdp_redirect_arr[i]); 13659d2b68ccSVladimir Oltean tx_ring->stats.xdp_tx_drops++; 13669d2b68ccSVladimir Oltean break; 13679d2b68ccSVladimir Oltean } 13689d2b68ccSVladimir Oltean 13699d2b68ccSVladimir Oltean xdp_tx_frm_cnt++; 13709d2b68ccSVladimir Oltean } 13719d2b68ccSVladimir Oltean 13729d2b68ccSVladimir Oltean if (unlikely((flags & XDP_XMIT_FLUSH) || k != xdp_tx_frm_cnt)) 13739d2b68ccSVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 13749d2b68ccSVladimir Oltean 13759d2b68ccSVladimir Oltean tx_ring->stats.xdp_tx += xdp_tx_frm_cnt; 13769d2b68ccSVladimir Oltean 137724e39309SVladimir Oltean enetc_unlock_mdio(); 137824e39309SVladimir Oltean 13799d2b68ccSVladimir Oltean return xdp_tx_frm_cnt; 13809d2b68ccSVladimir Oltean } 13819d2b68ccSVladimir Oltean 1382d1b15102SVladimir Oltean static void enetc_map_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i, 1383d1b15102SVladimir Oltean struct xdp_buff *xdp_buff, u16 size) 1384d1b15102SVladimir Oltean { 1385d1b15102SVladimir Oltean struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1386d1b15102SVladimir Oltean void *hard_start = page_address(rx_swbd->page) + rx_swbd->page_offset; 1387d1b15102SVladimir Oltean struct skb_shared_info *shinfo; 1388d1b15102SVladimir Oltean 13897ed2bc80SVladimir Oltean /* To be used for XDP_TX */ 13907ed2bc80SVladimir Oltean rx_swbd->len = size; 13917ed2bc80SVladimir Oltean 1392d1b15102SVladimir Oltean xdp_prepare_buff(xdp_buff, hard_start - rx_ring->buffer_offset, 1393d1b15102SVladimir Oltean rx_ring->buffer_offset, size, false); 1394d1b15102SVladimir Oltean 1395d1b15102SVladimir Oltean shinfo = xdp_get_shared_info_from_buff(xdp_buff); 1396d1b15102SVladimir Oltean shinfo->nr_frags = 0; 1397d1b15102SVladimir Oltean } 1398d1b15102SVladimir Oltean 1399d1b15102SVladimir Oltean static void enetc_add_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i, 1400d1b15102SVladimir Oltean u16 size, struct xdp_buff *xdp_buff) 1401d1b15102SVladimir Oltean { 1402d1b15102SVladimir Oltean struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp_buff); 1403d1b15102SVladimir Oltean struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1404d1b15102SVladimir Oltean skb_frag_t *frag = &shinfo->frags[shinfo->nr_frags]; 1405d1b15102SVladimir Oltean 14067ed2bc80SVladimir Oltean /* To be used for XDP_TX */ 14077ed2bc80SVladimir Oltean rx_swbd->len = size; 14087ed2bc80SVladimir Oltean 1409d1b15102SVladimir Oltean skb_frag_off_set(frag, rx_swbd->page_offset); 1410d1b15102SVladimir Oltean skb_frag_size_set(frag, size); 1411d1b15102SVladimir Oltean __skb_frag_set_page(frag, rx_swbd->page); 1412d1b15102SVladimir Oltean 1413d1b15102SVladimir Oltean shinfo->nr_frags++; 1414d1b15102SVladimir Oltean } 1415d1b15102SVladimir Oltean 1416d1b15102SVladimir Oltean static void enetc_build_xdp_buff(struct enetc_bdr *rx_ring, u32 bd_status, 1417d1b15102SVladimir Oltean union enetc_rx_bd **rxbd, int *i, 1418d1b15102SVladimir Oltean int *cleaned_cnt, struct xdp_buff *xdp_buff) 1419d1b15102SVladimir Oltean { 1420d1b15102SVladimir Oltean u16 size = le16_to_cpu((*rxbd)->r.buf_len); 1421d1b15102SVladimir Oltean 1422d1b15102SVladimir Oltean xdp_init_buff(xdp_buff, ENETC_RXB_TRUESIZE, &rx_ring->xdp.rxq); 1423d1b15102SVladimir Oltean 1424d1b15102SVladimir Oltean enetc_map_rx_buff_to_xdp(rx_ring, *i, xdp_buff, size); 1425d1b15102SVladimir Oltean (*cleaned_cnt)++; 1426d1b15102SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1427d1b15102SVladimir Oltean 1428d1b15102SVladimir Oltean /* not last BD in frame? */ 1429d1b15102SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 1430d1b15102SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 1431d1b15102SVladimir Oltean size = ENETC_RXB_DMA_SIZE_XDP; 1432d1b15102SVladimir Oltean 1433d1b15102SVladimir Oltean if (bd_status & ENETC_RXBD_LSTATUS_F) { 1434d1b15102SVladimir Oltean dma_rmb(); 1435d1b15102SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1436d1b15102SVladimir Oltean } 1437d1b15102SVladimir Oltean 1438d1b15102SVladimir Oltean enetc_add_rx_buff_to_xdp(rx_ring, *i, size, xdp_buff); 1439d1b15102SVladimir Oltean (*cleaned_cnt)++; 1440d1b15102SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1441d1b15102SVladimir Oltean } 1442d1b15102SVladimir Oltean } 1443d1b15102SVladimir Oltean 14447ed2bc80SVladimir Oltean /* Convert RX buffer descriptors to TX buffer descriptors. These will be 144592ff9a6eSVladimir Oltean * recycled back into the RX ring in enetc_clean_tx_ring. 14467ed2bc80SVladimir Oltean */ 14477ed2bc80SVladimir Oltean static int enetc_rx_swbd_to_xdp_tx_swbd(struct enetc_tx_swbd *xdp_tx_arr, 14487ed2bc80SVladimir Oltean struct enetc_bdr *rx_ring, 14497ed2bc80SVladimir Oltean int rx_ring_first, int rx_ring_last) 14507ed2bc80SVladimir Oltean { 14517ed2bc80SVladimir Oltean int n = 0; 14527ed2bc80SVladimir Oltean 14537ed2bc80SVladimir Oltean for (; rx_ring_first != rx_ring_last; 14547ed2bc80SVladimir Oltean n++, enetc_bdr_idx_inc(rx_ring, &rx_ring_first)) { 14557ed2bc80SVladimir Oltean struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first]; 14567ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd = &xdp_tx_arr[n]; 14577ed2bc80SVladimir Oltean 14587ed2bc80SVladimir Oltean /* No need to dma_map, we already have DMA_BIDIRECTIONAL */ 14597ed2bc80SVladimir Oltean tx_swbd->dma = rx_swbd->dma; 14607ed2bc80SVladimir Oltean tx_swbd->dir = rx_swbd->dir; 14617ed2bc80SVladimir Oltean tx_swbd->page = rx_swbd->page; 14627ed2bc80SVladimir Oltean tx_swbd->page_offset = rx_swbd->page_offset; 14637ed2bc80SVladimir Oltean tx_swbd->len = rx_swbd->len; 14647ed2bc80SVladimir Oltean tx_swbd->is_dma_page = true; 14657ed2bc80SVladimir Oltean tx_swbd->is_xdp_tx = true; 14667ed2bc80SVladimir Oltean tx_swbd->is_eof = false; 14677ed2bc80SVladimir Oltean } 14687ed2bc80SVladimir Oltean 14697ed2bc80SVladimir Oltean /* We rely on caller providing an rx_ring_last > rx_ring_first */ 14707ed2bc80SVladimir Oltean xdp_tx_arr[n - 1].is_eof = true; 14717ed2bc80SVladimir Oltean 14727ed2bc80SVladimir Oltean return n; 14737ed2bc80SVladimir Oltean } 14747ed2bc80SVladimir Oltean 1475d1b15102SVladimir Oltean static void enetc_xdp_drop(struct enetc_bdr *rx_ring, int rx_ring_first, 1476d1b15102SVladimir Oltean int rx_ring_last) 1477d1b15102SVladimir Oltean { 1478d1b15102SVladimir Oltean while (rx_ring_first != rx_ring_last) { 14796b04830dSVladimir Oltean enetc_put_rx_buff(rx_ring, 1480d1b15102SVladimir Oltean &rx_ring->rx_swbd[rx_ring_first]); 1481d1b15102SVladimir Oltean enetc_bdr_idx_inc(rx_ring, &rx_ring_first); 1482d1b15102SVladimir Oltean } 1483d1b15102SVladimir Oltean rx_ring->stats.xdp_drops++; 1484d1b15102SVladimir Oltean } 1485d1b15102SVladimir Oltean 14869d2b68ccSVladimir Oltean static void enetc_xdp_free(struct enetc_bdr *rx_ring, int rx_ring_first, 14879d2b68ccSVladimir Oltean int rx_ring_last) 14889d2b68ccSVladimir Oltean { 14899d2b68ccSVladimir Oltean while (rx_ring_first != rx_ring_last) { 14909d2b68ccSVladimir Oltean struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first]; 14919d2b68ccSVladimir Oltean 14929d2b68ccSVladimir Oltean if (rx_swbd->page) { 14939d2b68ccSVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE, 14949d2b68ccSVladimir Oltean rx_swbd->dir); 14959d2b68ccSVladimir Oltean __free_page(rx_swbd->page); 14969d2b68ccSVladimir Oltean rx_swbd->page = NULL; 14979d2b68ccSVladimir Oltean } 14989d2b68ccSVladimir Oltean enetc_bdr_idx_inc(rx_ring, &rx_ring_first); 14999d2b68ccSVladimir Oltean } 15009d2b68ccSVladimir Oltean rx_ring->stats.xdp_redirect_failures++; 15019d2b68ccSVladimir Oltean } 15029d2b68ccSVladimir Oltean 1503d1b15102SVladimir Oltean static int enetc_clean_rx_ring_xdp(struct enetc_bdr *rx_ring, 1504d1b15102SVladimir Oltean struct napi_struct *napi, int work_limit, 1505d1b15102SVladimir Oltean struct bpf_prog *prog) 1506d1b15102SVladimir Oltean { 15079d2b68ccSVladimir Oltean int xdp_tx_bd_cnt, xdp_tx_frm_cnt = 0, xdp_redirect_frm_cnt = 0; 15087ed2bc80SVladimir Oltean struct enetc_tx_swbd xdp_tx_arr[ENETC_MAX_SKB_FRAGS] = {0}; 15097ed2bc80SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev); 1510d1b15102SVladimir Oltean int rx_frm_cnt = 0, rx_byte_cnt = 0; 15117eab503bSVladimir Oltean struct enetc_bdr *tx_ring; 1512d1b15102SVladimir Oltean int cleaned_cnt, i; 1513d1b15102SVladimir Oltean u32 xdp_act; 1514d1b15102SVladimir Oltean 1515d1b15102SVladimir Oltean cleaned_cnt = enetc_bd_unused(rx_ring); 1516d1b15102SVladimir Oltean /* next descriptor to process */ 1517d1b15102SVladimir Oltean i = rx_ring->next_to_clean; 1518d1b15102SVladimir Oltean 1519d1b15102SVladimir Oltean while (likely(rx_frm_cnt < work_limit)) { 1520d1b15102SVladimir Oltean union enetc_rx_bd *rxbd, *orig_rxbd; 1521d1b15102SVladimir Oltean int orig_i, orig_cleaned_cnt; 1522d1b15102SVladimir Oltean struct xdp_buff xdp_buff; 1523d1b15102SVladimir Oltean struct sk_buff *skb; 15249d2b68ccSVladimir Oltean int tmp_orig_i, err; 1525d1b15102SVladimir Oltean u32 bd_status; 1526d1b15102SVladimir Oltean 1527d1b15102SVladimir Oltean rxbd = enetc_rxbd(rx_ring, i); 1528d1b15102SVladimir Oltean bd_status = le32_to_cpu(rxbd->r.lstatus); 1529d1b15102SVladimir Oltean if (!bd_status) 1530d1b15102SVladimir Oltean break; 1531d1b15102SVladimir Oltean 1532d1b15102SVladimir Oltean enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index)); 1533d1b15102SVladimir Oltean dma_rmb(); /* for reading other rxbd fields */ 1534d1b15102SVladimir Oltean 1535d1b15102SVladimir Oltean if (enetc_check_bd_errors_and_consume(rx_ring, bd_status, 1536d1b15102SVladimir Oltean &rxbd, &i)) 1537d1b15102SVladimir Oltean break; 1538d1b15102SVladimir Oltean 1539d1b15102SVladimir Oltean orig_rxbd = rxbd; 1540d1b15102SVladimir Oltean orig_cleaned_cnt = cleaned_cnt; 1541d1b15102SVladimir Oltean orig_i = i; 1542d1b15102SVladimir Oltean 1543d1b15102SVladimir Oltean enetc_build_xdp_buff(rx_ring, bd_status, &rxbd, &i, 1544d1b15102SVladimir Oltean &cleaned_cnt, &xdp_buff); 1545d1b15102SVladimir Oltean 1546d1b15102SVladimir Oltean xdp_act = bpf_prog_run_xdp(prog, &xdp_buff); 1547d1b15102SVladimir Oltean 1548d1b15102SVladimir Oltean switch (xdp_act) { 1549975acc83SVladimir Oltean default: 1550975acc83SVladimir Oltean bpf_warn_invalid_xdp_action(xdp_act); 1551975acc83SVladimir Oltean fallthrough; 1552d1b15102SVladimir Oltean case XDP_ABORTED: 1553d1b15102SVladimir Oltean trace_xdp_exception(rx_ring->ndev, prog, xdp_act); 1554d1b15102SVladimir Oltean fallthrough; 1555d1b15102SVladimir Oltean case XDP_DROP: 1556d1b15102SVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 1557d1b15102SVladimir Oltean break; 1558d1b15102SVladimir Oltean case XDP_PASS: 1559d1b15102SVladimir Oltean rxbd = orig_rxbd; 1560d1b15102SVladimir Oltean cleaned_cnt = orig_cleaned_cnt; 1561d1b15102SVladimir Oltean i = orig_i; 1562d1b15102SVladimir Oltean 1563d1b15102SVladimir Oltean skb = enetc_build_skb(rx_ring, bd_status, &rxbd, 1564d1b15102SVladimir Oltean &i, &cleaned_cnt, 1565d1b15102SVladimir Oltean ENETC_RXB_DMA_SIZE_XDP); 1566d1b15102SVladimir Oltean if (unlikely(!skb)) 15678f50d8bbSVladimir Oltean goto out; 1568d1b15102SVladimir Oltean 1569d1b15102SVladimir Oltean napi_gro_receive(napi, skb); 1570d1b15102SVladimir Oltean break; 15717ed2bc80SVladimir Oltean case XDP_TX: 15727eab503bSVladimir Oltean tx_ring = priv->xdp_tx_ring[rx_ring->index]; 15737ed2bc80SVladimir Oltean xdp_tx_bd_cnt = enetc_rx_swbd_to_xdp_tx_swbd(xdp_tx_arr, 15747ed2bc80SVladimir Oltean rx_ring, 15757ed2bc80SVladimir Oltean orig_i, i); 15767ed2bc80SVladimir Oltean 15777ed2bc80SVladimir Oltean if (!enetc_xdp_tx(tx_ring, xdp_tx_arr, xdp_tx_bd_cnt)) { 15787ed2bc80SVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 15797ed2bc80SVladimir Oltean tx_ring->stats.xdp_tx_drops++; 15807ed2bc80SVladimir Oltean } else { 15817ed2bc80SVladimir Oltean tx_ring->stats.xdp_tx += xdp_tx_bd_cnt; 15827ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight += xdp_tx_bd_cnt; 15837ed2bc80SVladimir Oltean xdp_tx_frm_cnt++; 158492ff9a6eSVladimir Oltean /* The XDP_TX enqueue was successful, so we 158592ff9a6eSVladimir Oltean * need to scrub the RX software BDs because 158692ff9a6eSVladimir Oltean * the ownership of the buffers no longer 158792ff9a6eSVladimir Oltean * belongs to the RX ring, and we must prevent 158892ff9a6eSVladimir Oltean * enetc_refill_rx_ring() from reusing 158992ff9a6eSVladimir Oltean * rx_swbd->page. 159092ff9a6eSVladimir Oltean */ 159192ff9a6eSVladimir Oltean while (orig_i != i) { 159292ff9a6eSVladimir Oltean rx_ring->rx_swbd[orig_i].page = NULL; 159392ff9a6eSVladimir Oltean enetc_bdr_idx_inc(rx_ring, &orig_i); 159492ff9a6eSVladimir Oltean } 15957ed2bc80SVladimir Oltean } 15967ed2bc80SVladimir Oltean break; 15979d2b68ccSVladimir Oltean case XDP_REDIRECT: 15989d2b68ccSVladimir Oltean /* xdp_return_frame does not support S/G in the sense 15999d2b68ccSVladimir Oltean * that it leaks the fragments (__xdp_return should not 16009d2b68ccSVladimir Oltean * call page_frag_free only for the initial buffer). 16019d2b68ccSVladimir Oltean * Until XDP_REDIRECT gains support for S/G let's keep 16029d2b68ccSVladimir Oltean * the code structure in place, but dead. We drop the 16039d2b68ccSVladimir Oltean * S/G frames ourselves to avoid memory leaks which 16049d2b68ccSVladimir Oltean * would otherwise leave the kernel OOM. 16059d2b68ccSVladimir Oltean */ 16069d2b68ccSVladimir Oltean if (unlikely(cleaned_cnt - orig_cleaned_cnt != 1)) { 16079d2b68ccSVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 16089d2b68ccSVladimir Oltean rx_ring->stats.xdp_redirect_sg++; 16099d2b68ccSVladimir Oltean break; 16109d2b68ccSVladimir Oltean } 16119d2b68ccSVladimir Oltean 16129d2b68ccSVladimir Oltean tmp_orig_i = orig_i; 16139d2b68ccSVladimir Oltean 16149d2b68ccSVladimir Oltean while (orig_i != i) { 16156b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, 16169d2b68ccSVladimir Oltean &rx_ring->rx_swbd[orig_i]); 16179d2b68ccSVladimir Oltean enetc_bdr_idx_inc(rx_ring, &orig_i); 16189d2b68ccSVladimir Oltean } 16199d2b68ccSVladimir Oltean 16209d2b68ccSVladimir Oltean err = xdp_do_redirect(rx_ring->ndev, &xdp_buff, prog); 16219d2b68ccSVladimir Oltean if (unlikely(err)) { 16229d2b68ccSVladimir Oltean enetc_xdp_free(rx_ring, tmp_orig_i, i); 16239d2b68ccSVladimir Oltean } else { 16249d2b68ccSVladimir Oltean xdp_redirect_frm_cnt++; 16259d2b68ccSVladimir Oltean rx_ring->stats.xdp_redirect++; 16269d2b68ccSVladimir Oltean } 1627d1b15102SVladimir Oltean } 1628d1b15102SVladimir Oltean 1629d1b15102SVladimir Oltean rx_frm_cnt++; 1630d1b15102SVladimir Oltean } 1631d1b15102SVladimir Oltean 16328f50d8bbSVladimir Oltean out: 1633d1b15102SVladimir Oltean rx_ring->next_to_clean = i; 1634d1b15102SVladimir Oltean 1635d1b15102SVladimir Oltean rx_ring->stats.packets += rx_frm_cnt; 1636d1b15102SVladimir Oltean rx_ring->stats.bytes += rx_byte_cnt; 1637d1b15102SVladimir Oltean 16389d2b68ccSVladimir Oltean if (xdp_redirect_frm_cnt) 16399d2b68ccSVladimir Oltean xdp_do_flush_map(); 16409d2b68ccSVladimir Oltean 16417ed2bc80SVladimir Oltean if (xdp_tx_frm_cnt) 16427ed2bc80SVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 16437ed2bc80SVladimir Oltean 16447ed2bc80SVladimir Oltean if (cleaned_cnt > rx_ring->xdp.xdp_tx_in_flight) 16457ed2bc80SVladimir Oltean enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring) - 16467ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight); 16477ed2bc80SVladimir Oltean 1648d1b15102SVladimir Oltean return rx_frm_cnt; 1649d1b15102SVladimir Oltean } 1650d1b15102SVladimir Oltean 16518580b3c3SVladimir Oltean static int enetc_poll(struct napi_struct *napi, int budget) 16528580b3c3SVladimir Oltean { 16538580b3c3SVladimir Oltean struct enetc_int_vector 16548580b3c3SVladimir Oltean *v = container_of(napi, struct enetc_int_vector, napi); 1655d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 1656d1b15102SVladimir Oltean struct bpf_prog *prog; 16578580b3c3SVladimir Oltean bool complete = true; 16588580b3c3SVladimir Oltean int work_done; 16598580b3c3SVladimir Oltean int i; 16608580b3c3SVladimir Oltean 16618580b3c3SVladimir Oltean enetc_lock_mdio(); 16628580b3c3SVladimir Oltean 16638580b3c3SVladimir Oltean for (i = 0; i < v->count_tx_rings; i++) 16648580b3c3SVladimir Oltean if (!enetc_clean_tx_ring(&v->tx_ring[i], budget)) 16658580b3c3SVladimir Oltean complete = false; 16668580b3c3SVladimir Oltean 1667d1b15102SVladimir Oltean prog = rx_ring->xdp.prog; 1668d1b15102SVladimir Oltean if (prog) 1669d1b15102SVladimir Oltean work_done = enetc_clean_rx_ring_xdp(rx_ring, napi, budget, prog); 1670d1b15102SVladimir Oltean else 1671d1b15102SVladimir Oltean work_done = enetc_clean_rx_ring(rx_ring, napi, budget); 16728580b3c3SVladimir Oltean if (work_done == budget) 16738580b3c3SVladimir Oltean complete = false; 16748580b3c3SVladimir Oltean if (work_done) 16758580b3c3SVladimir Oltean v->rx_napi_work = true; 16768580b3c3SVladimir Oltean 16778580b3c3SVladimir Oltean if (!complete) { 16788580b3c3SVladimir Oltean enetc_unlock_mdio(); 16798580b3c3SVladimir Oltean return budget; 16808580b3c3SVladimir Oltean } 16818580b3c3SVladimir Oltean 16828580b3c3SVladimir Oltean napi_complete_done(napi, work_done); 16838580b3c3SVladimir Oltean 16848580b3c3SVladimir Oltean if (likely(v->rx_dim_en)) 16858580b3c3SVladimir Oltean enetc_rx_net_dim(v); 16868580b3c3SVladimir Oltean 16878580b3c3SVladimir Oltean v->rx_napi_work = false; 16888580b3c3SVladimir Oltean 16898580b3c3SVladimir Oltean /* enable interrupts */ 16908580b3c3SVladimir Oltean enetc_wr_reg_hot(v->rbier, ENETC_RBIER_RXTIE); 16918580b3c3SVladimir Oltean 16928580b3c3SVladimir Oltean for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS) 16938580b3c3SVladimir Oltean enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 16948580b3c3SVladimir Oltean ENETC_TBIER_TXTIE); 16958580b3c3SVladimir Oltean 16968580b3c3SVladimir Oltean enetc_unlock_mdio(); 16978580b3c3SVladimir Oltean 16988580b3c3SVladimir Oltean return work_done; 16998580b3c3SVladimir Oltean } 17008580b3c3SVladimir Oltean 1701d4fd0404SClaudiu Manoil /* Probing and Init */ 1702d382563fSClaudiu Manoil #define ENETC_MAX_RFS_SIZE 64 1703d4fd0404SClaudiu Manoil void enetc_get_si_caps(struct enetc_si *si) 1704d4fd0404SClaudiu Manoil { 1705d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 1706d4fd0404SClaudiu Manoil u32 val; 1707d4fd0404SClaudiu Manoil 1708d4fd0404SClaudiu Manoil /* find out how many of various resources we have to work with */ 1709d4fd0404SClaudiu Manoil val = enetc_rd(hw, ENETC_SICAPR0); 1710d4fd0404SClaudiu Manoil si->num_rx_rings = (val >> 16) & 0xff; 1711d4fd0404SClaudiu Manoil si->num_tx_rings = val & 0xff; 1712d382563fSClaudiu Manoil 1713d382563fSClaudiu Manoil val = enetc_rd(hw, ENETC_SIRFSCAPR); 1714d382563fSClaudiu Manoil si->num_fs_entries = ENETC_SIRFSCAPR_GET_NUM_RFS(val); 1715d382563fSClaudiu Manoil si->num_fs_entries = min(si->num_fs_entries, ENETC_MAX_RFS_SIZE); 1716d382563fSClaudiu Manoil 1717d382563fSClaudiu Manoil si->num_rss = 0; 1718d382563fSClaudiu Manoil val = enetc_rd(hw, ENETC_SIPCAPR0); 1719d382563fSClaudiu Manoil if (val & ENETC_SIPCAPR0_RSS) { 17202e47cb41SPo Liu u32 rss; 17212e47cb41SPo Liu 17222e47cb41SPo Liu rss = enetc_rd(hw, ENETC_SIRSSCAPR); 17232e47cb41SPo Liu si->num_rss = ENETC_SIRSSCAPR_GET_NUM_RSS(rss); 1724d382563fSClaudiu Manoil } 17252e47cb41SPo Liu 17262e47cb41SPo Liu if (val & ENETC_SIPCAPR0_QBV) 17272e47cb41SPo Liu si->hw_features |= ENETC_SI_F_QBV; 172879e49982SPo Liu 172979e49982SPo Liu if (val & ENETC_SIPCAPR0_PSFP) 173079e49982SPo Liu si->hw_features |= ENETC_SI_F_PSFP; 1731d4fd0404SClaudiu Manoil } 1732d4fd0404SClaudiu Manoil 1733d4fd0404SClaudiu Manoil static int enetc_dma_alloc_bdr(struct enetc_bdr *r, size_t bd_size) 1734d4fd0404SClaudiu Manoil { 1735d4fd0404SClaudiu Manoil r->bd_base = dma_alloc_coherent(r->dev, r->bd_count * bd_size, 1736d4fd0404SClaudiu Manoil &r->bd_dma_base, GFP_KERNEL); 1737d4fd0404SClaudiu Manoil if (!r->bd_base) 1738d4fd0404SClaudiu Manoil return -ENOMEM; 1739d4fd0404SClaudiu Manoil 1740d4fd0404SClaudiu Manoil /* h/w requires 128B alignment */ 1741d4fd0404SClaudiu Manoil if (!IS_ALIGNED(r->bd_dma_base, 128)) { 1742d4fd0404SClaudiu Manoil dma_free_coherent(r->dev, r->bd_count * bd_size, r->bd_base, 1743d4fd0404SClaudiu Manoil r->bd_dma_base); 1744d4fd0404SClaudiu Manoil return -EINVAL; 1745d4fd0404SClaudiu Manoil } 1746d4fd0404SClaudiu Manoil 1747d4fd0404SClaudiu Manoil return 0; 1748d4fd0404SClaudiu Manoil } 1749d4fd0404SClaudiu Manoil 1750d4fd0404SClaudiu Manoil static int enetc_alloc_txbdr(struct enetc_bdr *txr) 1751d4fd0404SClaudiu Manoil { 1752d4fd0404SClaudiu Manoil int err; 1753d4fd0404SClaudiu Manoil 1754d4fd0404SClaudiu Manoil txr->tx_swbd = vzalloc(txr->bd_count * sizeof(struct enetc_tx_swbd)); 1755d4fd0404SClaudiu Manoil if (!txr->tx_swbd) 1756d4fd0404SClaudiu Manoil return -ENOMEM; 1757d4fd0404SClaudiu Manoil 1758d4fd0404SClaudiu Manoil err = enetc_dma_alloc_bdr(txr, sizeof(union enetc_tx_bd)); 1759fb8629e2SIoana Ciornei if (err) 1760fb8629e2SIoana Ciornei goto err_alloc_bdr; 1761fb8629e2SIoana Ciornei 1762fb8629e2SIoana Ciornei txr->tso_headers = dma_alloc_coherent(txr->dev, 1763fb8629e2SIoana Ciornei txr->bd_count * TSO_HEADER_SIZE, 1764fb8629e2SIoana Ciornei &txr->tso_headers_dma, 1765fb8629e2SIoana Ciornei GFP_KERNEL); 1766e79d8264SDan Carpenter if (!txr->tso_headers) { 1767e79d8264SDan Carpenter err = -ENOMEM; 1768fb8629e2SIoana Ciornei goto err_alloc_tso; 1769e79d8264SDan Carpenter } 1770d4fd0404SClaudiu Manoil 1771d4fd0404SClaudiu Manoil txr->next_to_clean = 0; 1772d4fd0404SClaudiu Manoil txr->next_to_use = 0; 1773d4fd0404SClaudiu Manoil 1774d4fd0404SClaudiu Manoil return 0; 1775fb8629e2SIoana Ciornei 1776fb8629e2SIoana Ciornei err_alloc_tso: 1777fb8629e2SIoana Ciornei dma_free_coherent(txr->dev, txr->bd_count * sizeof(union enetc_tx_bd), 1778fb8629e2SIoana Ciornei txr->bd_base, txr->bd_dma_base); 1779fb8629e2SIoana Ciornei txr->bd_base = NULL; 1780fb8629e2SIoana Ciornei err_alloc_bdr: 1781fb8629e2SIoana Ciornei vfree(txr->tx_swbd); 1782fb8629e2SIoana Ciornei txr->tx_swbd = NULL; 1783fb8629e2SIoana Ciornei 1784fb8629e2SIoana Ciornei return err; 1785d4fd0404SClaudiu Manoil } 1786d4fd0404SClaudiu Manoil 1787d4fd0404SClaudiu Manoil static void enetc_free_txbdr(struct enetc_bdr *txr) 1788d4fd0404SClaudiu Manoil { 1789d4fd0404SClaudiu Manoil int size, i; 1790d4fd0404SClaudiu Manoil 1791d4fd0404SClaudiu Manoil for (i = 0; i < txr->bd_count; i++) 17929d2b68ccSVladimir Oltean enetc_free_tx_frame(txr, &txr->tx_swbd[i]); 1793d4fd0404SClaudiu Manoil 1794d4fd0404SClaudiu Manoil size = txr->bd_count * sizeof(union enetc_tx_bd); 1795d4fd0404SClaudiu Manoil 1796fb8629e2SIoana Ciornei dma_free_coherent(txr->dev, txr->bd_count * TSO_HEADER_SIZE, 1797fb8629e2SIoana Ciornei txr->tso_headers, txr->tso_headers_dma); 1798fb8629e2SIoana Ciornei txr->tso_headers = NULL; 1799fb8629e2SIoana Ciornei 1800d4fd0404SClaudiu Manoil dma_free_coherent(txr->dev, size, txr->bd_base, txr->bd_dma_base); 1801d4fd0404SClaudiu Manoil txr->bd_base = NULL; 1802d4fd0404SClaudiu Manoil 1803d4fd0404SClaudiu Manoil vfree(txr->tx_swbd); 1804d4fd0404SClaudiu Manoil txr->tx_swbd = NULL; 1805d4fd0404SClaudiu Manoil } 1806d4fd0404SClaudiu Manoil 1807d4fd0404SClaudiu Manoil static int enetc_alloc_tx_resources(struct enetc_ndev_priv *priv) 1808d4fd0404SClaudiu Manoil { 1809d4fd0404SClaudiu Manoil int i, err; 1810d4fd0404SClaudiu Manoil 1811d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 1812d4fd0404SClaudiu Manoil err = enetc_alloc_txbdr(priv->tx_ring[i]); 1813d4fd0404SClaudiu Manoil 1814d4fd0404SClaudiu Manoil if (err) 1815d4fd0404SClaudiu Manoil goto fail; 1816d4fd0404SClaudiu Manoil } 1817d4fd0404SClaudiu Manoil 1818d4fd0404SClaudiu Manoil return 0; 1819d4fd0404SClaudiu Manoil 1820d4fd0404SClaudiu Manoil fail: 1821d4fd0404SClaudiu Manoil while (i-- > 0) 1822d4fd0404SClaudiu Manoil enetc_free_txbdr(priv->tx_ring[i]); 1823d4fd0404SClaudiu Manoil 1824d4fd0404SClaudiu Manoil return err; 1825d4fd0404SClaudiu Manoil } 1826d4fd0404SClaudiu Manoil 1827d4fd0404SClaudiu Manoil static void enetc_free_tx_resources(struct enetc_ndev_priv *priv) 1828d4fd0404SClaudiu Manoil { 1829d4fd0404SClaudiu Manoil int i; 1830d4fd0404SClaudiu Manoil 1831d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 1832d4fd0404SClaudiu Manoil enetc_free_txbdr(priv->tx_ring[i]); 1833d4fd0404SClaudiu Manoil } 1834d4fd0404SClaudiu Manoil 1835434cebabSClaudiu Manoil static int enetc_alloc_rxbdr(struct enetc_bdr *rxr, bool extended) 1836d4fd0404SClaudiu Manoil { 1837434cebabSClaudiu Manoil size_t size = sizeof(union enetc_rx_bd); 1838d4fd0404SClaudiu Manoil int err; 1839d4fd0404SClaudiu Manoil 1840d4fd0404SClaudiu Manoil rxr->rx_swbd = vzalloc(rxr->bd_count * sizeof(struct enetc_rx_swbd)); 1841d4fd0404SClaudiu Manoil if (!rxr->rx_swbd) 1842d4fd0404SClaudiu Manoil return -ENOMEM; 1843d4fd0404SClaudiu Manoil 1844434cebabSClaudiu Manoil if (extended) 1845434cebabSClaudiu Manoil size *= 2; 1846434cebabSClaudiu Manoil 1847434cebabSClaudiu Manoil err = enetc_dma_alloc_bdr(rxr, size); 1848d4fd0404SClaudiu Manoil if (err) { 1849d4fd0404SClaudiu Manoil vfree(rxr->rx_swbd); 1850d4fd0404SClaudiu Manoil return err; 1851d4fd0404SClaudiu Manoil } 1852d4fd0404SClaudiu Manoil 1853d4fd0404SClaudiu Manoil rxr->next_to_clean = 0; 1854d4fd0404SClaudiu Manoil rxr->next_to_use = 0; 1855d4fd0404SClaudiu Manoil rxr->next_to_alloc = 0; 1856434cebabSClaudiu Manoil rxr->ext_en = extended; 1857d4fd0404SClaudiu Manoil 1858d4fd0404SClaudiu Manoil return 0; 1859d4fd0404SClaudiu Manoil } 1860d4fd0404SClaudiu Manoil 1861d4fd0404SClaudiu Manoil static void enetc_free_rxbdr(struct enetc_bdr *rxr) 1862d4fd0404SClaudiu Manoil { 1863d4fd0404SClaudiu Manoil int size; 1864d4fd0404SClaudiu Manoil 1865d4fd0404SClaudiu Manoil size = rxr->bd_count * sizeof(union enetc_rx_bd); 1866d4fd0404SClaudiu Manoil 1867d4fd0404SClaudiu Manoil dma_free_coherent(rxr->dev, size, rxr->bd_base, rxr->bd_dma_base); 1868d4fd0404SClaudiu Manoil rxr->bd_base = NULL; 1869d4fd0404SClaudiu Manoil 1870d4fd0404SClaudiu Manoil vfree(rxr->rx_swbd); 1871d4fd0404SClaudiu Manoil rxr->rx_swbd = NULL; 1872d4fd0404SClaudiu Manoil } 1873d4fd0404SClaudiu Manoil 1874d4fd0404SClaudiu Manoil static int enetc_alloc_rx_resources(struct enetc_ndev_priv *priv) 1875d4fd0404SClaudiu Manoil { 1876434cebabSClaudiu Manoil bool extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP); 1877d4fd0404SClaudiu Manoil int i, err; 1878d4fd0404SClaudiu Manoil 1879d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) { 1880434cebabSClaudiu Manoil err = enetc_alloc_rxbdr(priv->rx_ring[i], extended); 1881d4fd0404SClaudiu Manoil 1882d4fd0404SClaudiu Manoil if (err) 1883d4fd0404SClaudiu Manoil goto fail; 1884d4fd0404SClaudiu Manoil } 1885d4fd0404SClaudiu Manoil 1886d4fd0404SClaudiu Manoil return 0; 1887d4fd0404SClaudiu Manoil 1888d4fd0404SClaudiu Manoil fail: 1889d4fd0404SClaudiu Manoil while (i-- > 0) 1890d4fd0404SClaudiu Manoil enetc_free_rxbdr(priv->rx_ring[i]); 1891d4fd0404SClaudiu Manoil 1892d4fd0404SClaudiu Manoil return err; 1893d4fd0404SClaudiu Manoil } 1894d4fd0404SClaudiu Manoil 1895d4fd0404SClaudiu Manoil static void enetc_free_rx_resources(struct enetc_ndev_priv *priv) 1896d4fd0404SClaudiu Manoil { 1897d4fd0404SClaudiu Manoil int i; 1898d4fd0404SClaudiu Manoil 1899d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 1900d4fd0404SClaudiu Manoil enetc_free_rxbdr(priv->rx_ring[i]); 1901d4fd0404SClaudiu Manoil } 1902d4fd0404SClaudiu Manoil 1903d4fd0404SClaudiu Manoil static void enetc_free_tx_ring(struct enetc_bdr *tx_ring) 1904d4fd0404SClaudiu Manoil { 1905d4fd0404SClaudiu Manoil int i; 1906d4fd0404SClaudiu Manoil 1907d4fd0404SClaudiu Manoil if (!tx_ring->tx_swbd) 1908d4fd0404SClaudiu Manoil return; 1909d4fd0404SClaudiu Manoil 1910d4fd0404SClaudiu Manoil for (i = 0; i < tx_ring->bd_count; i++) { 1911d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd = &tx_ring->tx_swbd[i]; 1912d4fd0404SClaudiu Manoil 19139d2b68ccSVladimir Oltean enetc_free_tx_frame(tx_ring, tx_swbd); 1914d4fd0404SClaudiu Manoil } 1915d4fd0404SClaudiu Manoil 1916d4fd0404SClaudiu Manoil tx_ring->next_to_clean = 0; 1917d4fd0404SClaudiu Manoil tx_ring->next_to_use = 0; 1918d4fd0404SClaudiu Manoil } 1919d4fd0404SClaudiu Manoil 1920d4fd0404SClaudiu Manoil static void enetc_free_rx_ring(struct enetc_bdr *rx_ring) 1921d4fd0404SClaudiu Manoil { 1922d4fd0404SClaudiu Manoil int i; 1923d4fd0404SClaudiu Manoil 1924d4fd0404SClaudiu Manoil if (!rx_ring->rx_swbd) 1925d4fd0404SClaudiu Manoil return; 1926d4fd0404SClaudiu Manoil 1927d4fd0404SClaudiu Manoil for (i = 0; i < rx_ring->bd_count; i++) { 1928d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i]; 1929d4fd0404SClaudiu Manoil 1930d4fd0404SClaudiu Manoil if (!rx_swbd->page) 1931d4fd0404SClaudiu Manoil continue; 1932d4fd0404SClaudiu Manoil 19337ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE, 19347ed2bc80SVladimir Oltean rx_swbd->dir); 1935d4fd0404SClaudiu Manoil __free_page(rx_swbd->page); 1936d4fd0404SClaudiu Manoil rx_swbd->page = NULL; 1937d4fd0404SClaudiu Manoil } 1938d4fd0404SClaudiu Manoil 1939d4fd0404SClaudiu Manoil rx_ring->next_to_clean = 0; 1940d4fd0404SClaudiu Manoil rx_ring->next_to_use = 0; 1941d4fd0404SClaudiu Manoil rx_ring->next_to_alloc = 0; 1942d4fd0404SClaudiu Manoil } 1943d4fd0404SClaudiu Manoil 1944d4fd0404SClaudiu Manoil static void enetc_free_rxtx_rings(struct enetc_ndev_priv *priv) 1945d4fd0404SClaudiu Manoil { 1946d4fd0404SClaudiu Manoil int i; 1947d4fd0404SClaudiu Manoil 1948d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 1949d4fd0404SClaudiu Manoil enetc_free_rx_ring(priv->rx_ring[i]); 1950d4fd0404SClaudiu Manoil 1951d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 1952d4fd0404SClaudiu Manoil enetc_free_tx_ring(priv->tx_ring[i]); 1953d4fd0404SClaudiu Manoil } 1954d4fd0404SClaudiu Manoil 1955d382563fSClaudiu Manoil static int enetc_setup_default_rss_table(struct enetc_si *si, int num_groups) 1956d382563fSClaudiu Manoil { 1957d382563fSClaudiu Manoil int *rss_table; 1958d382563fSClaudiu Manoil int i; 1959d382563fSClaudiu Manoil 1960d382563fSClaudiu Manoil rss_table = kmalloc_array(si->num_rss, sizeof(*rss_table), GFP_KERNEL); 1961d382563fSClaudiu Manoil if (!rss_table) 1962d382563fSClaudiu Manoil return -ENOMEM; 1963d382563fSClaudiu Manoil 1964d382563fSClaudiu Manoil /* Set up RSS table defaults */ 1965d382563fSClaudiu Manoil for (i = 0; i < si->num_rss; i++) 1966d382563fSClaudiu Manoil rss_table[i] = i % num_groups; 1967d382563fSClaudiu Manoil 1968d382563fSClaudiu Manoil enetc_set_rss_table(si, rss_table, si->num_rss); 1969d382563fSClaudiu Manoil 1970d382563fSClaudiu Manoil kfree(rss_table); 1971d382563fSClaudiu Manoil 1972d382563fSClaudiu Manoil return 0; 1973d382563fSClaudiu Manoil } 1974d382563fSClaudiu Manoil 1975c646d10dSVladimir Oltean int enetc_configure_si(struct enetc_ndev_priv *priv) 1976d4fd0404SClaudiu Manoil { 1977d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 1978d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 1979d382563fSClaudiu Manoil int err; 1980d4fd0404SClaudiu Manoil 1981d4fd0404SClaudiu Manoil /* set SI cache attributes */ 1982d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SICAR0, 1983d4fd0404SClaudiu Manoil ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT); 1984d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SICAR1, ENETC_SICAR_MSI); 1985d4fd0404SClaudiu Manoil /* enable SI */ 1986d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMR, ENETC_SIMR_EN); 1987d4fd0404SClaudiu Manoil 1988d382563fSClaudiu Manoil if (si->num_rss) { 1989d382563fSClaudiu Manoil err = enetc_setup_default_rss_table(si, priv->num_rx_rings); 1990d382563fSClaudiu Manoil if (err) 1991d382563fSClaudiu Manoil return err; 1992d382563fSClaudiu Manoil } 1993d382563fSClaudiu Manoil 1994d4fd0404SClaudiu Manoil return 0; 1995d4fd0404SClaudiu Manoil } 1996d4fd0404SClaudiu Manoil 1997d4fd0404SClaudiu Manoil void enetc_init_si_rings_params(struct enetc_ndev_priv *priv) 1998d4fd0404SClaudiu Manoil { 1999d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 2000d4fd0404SClaudiu Manoil int cpus = num_online_cpus(); 2001d4fd0404SClaudiu Manoil 200202293dd4SClaudiu Manoil priv->tx_bd_count = ENETC_TX_RING_DEFAULT_SIZE; 200302293dd4SClaudiu Manoil priv->rx_bd_count = ENETC_RX_RING_DEFAULT_SIZE; 2004d4fd0404SClaudiu Manoil 2005d4fd0404SClaudiu Manoil /* Enable all available TX rings in order to configure as many 2006d4fd0404SClaudiu Manoil * priorities as possible, when needed. 2007d4fd0404SClaudiu Manoil * TODO: Make # of TX rings run-time configurable 2008d4fd0404SClaudiu Manoil */ 2009d4fd0404SClaudiu Manoil priv->num_rx_rings = min_t(int, cpus, si->num_rx_rings); 2010d4fd0404SClaudiu Manoil priv->num_tx_rings = si->num_tx_rings; 2011d4fd0404SClaudiu Manoil priv->bdr_int_num = cpus; 2012ae0e6a5dSClaudiu Manoil priv->ic_mode = ENETC_IC_RX_ADAPTIVE | ENETC_IC_TX_MANUAL; 2013ae0e6a5dSClaudiu Manoil priv->tx_ictt = ENETC_TXIC_TIMETHR; 2014d4fd0404SClaudiu Manoil } 2015d4fd0404SClaudiu Manoil 2016d4fd0404SClaudiu Manoil int enetc_alloc_si_resources(struct enetc_ndev_priv *priv) 2017d4fd0404SClaudiu Manoil { 2018d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 2019d4fd0404SClaudiu Manoil 2020d382563fSClaudiu Manoil priv->cls_rules = kcalloc(si->num_fs_entries, sizeof(*priv->cls_rules), 2021d382563fSClaudiu Manoil GFP_KERNEL); 20224b47c0b8SVladimir Oltean if (!priv->cls_rules) 20234b47c0b8SVladimir Oltean return -ENOMEM; 2024d382563fSClaudiu Manoil 2025d4fd0404SClaudiu Manoil return 0; 2026d4fd0404SClaudiu Manoil } 2027d4fd0404SClaudiu Manoil 2028d4fd0404SClaudiu Manoil void enetc_free_si_resources(struct enetc_ndev_priv *priv) 2029d4fd0404SClaudiu Manoil { 2030d382563fSClaudiu Manoil kfree(priv->cls_rules); 2031d4fd0404SClaudiu Manoil } 2032d4fd0404SClaudiu Manoil 2033d4fd0404SClaudiu Manoil static void enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring) 2034d4fd0404SClaudiu Manoil { 2035d4fd0404SClaudiu Manoil int idx = tx_ring->index; 2036d4fd0404SClaudiu Manoil u32 tbmr; 2037d4fd0404SClaudiu Manoil 2038d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBBAR0, 2039d4fd0404SClaudiu Manoil lower_32_bits(tx_ring->bd_dma_base)); 2040d4fd0404SClaudiu Manoil 2041d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBBAR1, 2042d4fd0404SClaudiu Manoil upper_32_bits(tx_ring->bd_dma_base)); 2043d4fd0404SClaudiu Manoil 2044d4fd0404SClaudiu Manoil WARN_ON(!IS_ALIGNED(tx_ring->bd_count, 64)); /* multiple of 64 */ 2045d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBLENR, 2046d4fd0404SClaudiu Manoil ENETC_RTBLENR_LEN(tx_ring->bd_count)); 2047d4fd0404SClaudiu Manoil 2048d4fd0404SClaudiu Manoil /* clearing PI/CI registers for Tx not supported, adjust sw indexes */ 2049d4fd0404SClaudiu Manoil tx_ring->next_to_use = enetc_txbdr_rd(hw, idx, ENETC_TBPIR); 2050d4fd0404SClaudiu Manoil tx_ring->next_to_clean = enetc_txbdr_rd(hw, idx, ENETC_TBCIR); 2051d4fd0404SClaudiu Manoil 2052d4fd0404SClaudiu Manoil /* enable Tx ints by setting pkt thr to 1 */ 205312460a0aSClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBICR0, ENETC_TBICR0_ICEN | 0x1); 2054d4fd0404SClaudiu Manoil 2055d4fd0404SClaudiu Manoil tbmr = ENETC_TBMR_EN; 2056d4fd0404SClaudiu Manoil if (tx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_TX) 2057d4fd0404SClaudiu Manoil tbmr |= ENETC_TBMR_VIH; 2058d4fd0404SClaudiu Manoil 2059d4fd0404SClaudiu Manoil /* enable ring */ 2060d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr); 2061d4fd0404SClaudiu Manoil 2062d4fd0404SClaudiu Manoil tx_ring->tpir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBPIR); 2063d4fd0404SClaudiu Manoil tx_ring->tcir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBCIR); 2064d4fd0404SClaudiu Manoil tx_ring->idr = hw->reg + ENETC_SITXIDR; 2065d4fd0404SClaudiu Manoil } 2066d4fd0404SClaudiu Manoil 2067d4fd0404SClaudiu Manoil static void enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring) 2068d4fd0404SClaudiu Manoil { 2069d4fd0404SClaudiu Manoil int idx = rx_ring->index; 2070d4fd0404SClaudiu Manoil u32 rbmr; 2071d4fd0404SClaudiu Manoil 2072d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0, 2073d4fd0404SClaudiu Manoil lower_32_bits(rx_ring->bd_dma_base)); 2074d4fd0404SClaudiu Manoil 2075d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1, 2076d4fd0404SClaudiu Manoil upper_32_bits(rx_ring->bd_dma_base)); 2077d4fd0404SClaudiu Manoil 2078d4fd0404SClaudiu Manoil WARN_ON(!IS_ALIGNED(rx_ring->bd_count, 64)); /* multiple of 64 */ 2079d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBLENR, 2080d4fd0404SClaudiu Manoil ENETC_RTBLENR_LEN(rx_ring->bd_count)); 2081d4fd0404SClaudiu Manoil 2082d1b15102SVladimir Oltean if (rx_ring->xdp.prog) 2083d1b15102SVladimir Oltean enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE_XDP); 2084d1b15102SVladimir Oltean else 2085d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE); 2086d4fd0404SClaudiu Manoil 2087d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0); 2088d4fd0404SClaudiu Manoil 2089d4fd0404SClaudiu Manoil /* enable Rx ints by setting pkt thr to 1 */ 209012460a0aSClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBICR0, ENETC_RBICR0_ICEN | 0x1); 2091d4fd0404SClaudiu Manoil 2092d4fd0404SClaudiu Manoil rbmr = ENETC_RBMR_EN; 2093434cebabSClaudiu Manoil 2094434cebabSClaudiu Manoil if (rx_ring->ext_en) 2095d3982312SY.b. Lu rbmr |= ENETC_RBMR_BDS; 2096434cebabSClaudiu Manoil 2097d4fd0404SClaudiu Manoil if (rx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_RX) 2098d4fd0404SClaudiu Manoil rbmr |= ENETC_RBMR_VTE; 2099d4fd0404SClaudiu Manoil 2100d4fd0404SClaudiu Manoil rx_ring->rcir = hw->reg + ENETC_BDR(RX, idx, ENETC_RBCIR); 2101d4fd0404SClaudiu Manoil rx_ring->idr = hw->reg + ENETC_SIRXIDR; 2102d4fd0404SClaudiu Manoil 21037a5222cbSVladimir Oltean enetc_lock_mdio(); 2104d4fd0404SClaudiu Manoil enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring)); 21057a5222cbSVladimir Oltean enetc_unlock_mdio(); 2106d4fd0404SClaudiu Manoil 2107d4fd0404SClaudiu Manoil /* enable ring */ 2108d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr); 2109d4fd0404SClaudiu Manoil } 2110d4fd0404SClaudiu Manoil 2111d4fd0404SClaudiu Manoil static void enetc_setup_bdrs(struct enetc_ndev_priv *priv) 2112d4fd0404SClaudiu Manoil { 2113d4fd0404SClaudiu Manoil int i; 2114d4fd0404SClaudiu Manoil 2115d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2116d4fd0404SClaudiu Manoil enetc_setup_txbdr(&priv->si->hw, priv->tx_ring[i]); 2117d4fd0404SClaudiu Manoil 2118d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2119d4fd0404SClaudiu Manoil enetc_setup_rxbdr(&priv->si->hw, priv->rx_ring[i]); 2120d4fd0404SClaudiu Manoil } 2121d4fd0404SClaudiu Manoil 2122d4fd0404SClaudiu Manoil static void enetc_clear_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring) 2123d4fd0404SClaudiu Manoil { 2124d4fd0404SClaudiu Manoil int idx = rx_ring->index; 2125d4fd0404SClaudiu Manoil 2126d4fd0404SClaudiu Manoil /* disable EN bit on ring */ 2127d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBMR, 0); 2128d4fd0404SClaudiu Manoil } 2129d4fd0404SClaudiu Manoil 2130d4fd0404SClaudiu Manoil static void enetc_clear_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring) 2131d4fd0404SClaudiu Manoil { 2132d4fd0404SClaudiu Manoil int delay = 8, timeout = 100; 2133d4fd0404SClaudiu Manoil int idx = tx_ring->index; 2134d4fd0404SClaudiu Manoil 2135d4fd0404SClaudiu Manoil /* disable EN bit on ring */ 2136d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBMR, 0); 2137d4fd0404SClaudiu Manoil 2138d4fd0404SClaudiu Manoil /* wait for busy to clear */ 2139d4fd0404SClaudiu Manoil while (delay < timeout && 2140d4fd0404SClaudiu Manoil enetc_txbdr_rd(hw, idx, ENETC_TBSR) & ENETC_TBSR_BUSY) { 2141d4fd0404SClaudiu Manoil msleep(delay); 2142d4fd0404SClaudiu Manoil delay *= 2; 2143d4fd0404SClaudiu Manoil } 2144d4fd0404SClaudiu Manoil 2145d4fd0404SClaudiu Manoil if (delay >= timeout) 2146d4fd0404SClaudiu Manoil netdev_warn(tx_ring->ndev, "timeout for tx ring #%d clear\n", 2147d4fd0404SClaudiu Manoil idx); 2148d4fd0404SClaudiu Manoil } 2149d4fd0404SClaudiu Manoil 2150d4fd0404SClaudiu Manoil static void enetc_clear_bdrs(struct enetc_ndev_priv *priv) 2151d4fd0404SClaudiu Manoil { 2152d4fd0404SClaudiu Manoil int i; 2153d4fd0404SClaudiu Manoil 2154d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2155d4fd0404SClaudiu Manoil enetc_clear_txbdr(&priv->si->hw, priv->tx_ring[i]); 2156d4fd0404SClaudiu Manoil 2157d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2158d4fd0404SClaudiu Manoil enetc_clear_rxbdr(&priv->si->hw, priv->rx_ring[i]); 2159d4fd0404SClaudiu Manoil 2160d4fd0404SClaudiu Manoil udelay(1); 2161d4fd0404SClaudiu Manoil } 2162d4fd0404SClaudiu Manoil 2163d4fd0404SClaudiu Manoil static int enetc_setup_irqs(struct enetc_ndev_priv *priv) 2164d4fd0404SClaudiu Manoil { 2165d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 2166d4fd0404SClaudiu Manoil int i, j, err; 2167d4fd0404SClaudiu Manoil 2168d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2169d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2170d4fd0404SClaudiu Manoil struct enetc_int_vector *v = priv->int_vector[i]; 2171d4fd0404SClaudiu Manoil int entry = ENETC_BDR_INT_BASE_IDX + i; 2172d4fd0404SClaudiu Manoil struct enetc_hw *hw = &priv->si->hw; 2173d4fd0404SClaudiu Manoil 2174d4fd0404SClaudiu Manoil snprintf(v->name, sizeof(v->name), "%s-rxtx%d", 2175d4fd0404SClaudiu Manoil priv->ndev->name, i); 2176d4fd0404SClaudiu Manoil err = request_irq(irq, enetc_msix, 0, v->name, v); 2177d4fd0404SClaudiu Manoil if (err) { 2178d4fd0404SClaudiu Manoil dev_err(priv->dev, "request_irq() failed!\n"); 2179d4fd0404SClaudiu Manoil goto irq_err; 2180d4fd0404SClaudiu Manoil } 2181bbb96dc7SClaudiu Manoil disable_irq(irq); 2182d4fd0404SClaudiu Manoil 2183d4fd0404SClaudiu Manoil v->tbier_base = hw->reg + ENETC_BDR(TX, 0, ENETC_TBIER); 2184d4fd0404SClaudiu Manoil v->rbier = hw->reg + ENETC_BDR(RX, i, ENETC_RBIER); 218591571081SClaudiu Manoil v->ricr1 = hw->reg + ENETC_BDR(RX, i, ENETC_RBICR1); 2186d4fd0404SClaudiu Manoil 2187d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMSIRRV(i), entry); 2188d4fd0404SClaudiu Manoil 2189d4fd0404SClaudiu Manoil for (j = 0; j < v->count_tx_rings; j++) { 2190d4fd0404SClaudiu Manoil int idx = v->tx_ring[j].index; 2191d4fd0404SClaudiu Manoil 2192d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMSITRV(idx), entry); 2193d4fd0404SClaudiu Manoil } 21947237a494SClaudiu Manoil irq_set_affinity_hint(irq, get_cpu_mask(i % num_online_cpus())); 2195d4fd0404SClaudiu Manoil } 2196d4fd0404SClaudiu Manoil 2197d4fd0404SClaudiu Manoil return 0; 2198d4fd0404SClaudiu Manoil 2199d4fd0404SClaudiu Manoil irq_err: 2200d4fd0404SClaudiu Manoil while (i--) { 2201d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2202d4fd0404SClaudiu Manoil 2203d4fd0404SClaudiu Manoil irq_set_affinity_hint(irq, NULL); 2204d4fd0404SClaudiu Manoil free_irq(irq, priv->int_vector[i]); 2205d4fd0404SClaudiu Manoil } 2206d4fd0404SClaudiu Manoil 2207d4fd0404SClaudiu Manoil return err; 2208d4fd0404SClaudiu Manoil } 2209d4fd0404SClaudiu Manoil 2210d4fd0404SClaudiu Manoil static void enetc_free_irqs(struct enetc_ndev_priv *priv) 2211d4fd0404SClaudiu Manoil { 2212d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 2213d4fd0404SClaudiu Manoil int i; 2214d4fd0404SClaudiu Manoil 2215d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2216d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2217d4fd0404SClaudiu Manoil 2218d4fd0404SClaudiu Manoil irq_set_affinity_hint(irq, NULL); 2219d4fd0404SClaudiu Manoil free_irq(irq, priv->int_vector[i]); 2220d4fd0404SClaudiu Manoil } 2221d4fd0404SClaudiu Manoil } 2222d4fd0404SClaudiu Manoil 2223bbb96dc7SClaudiu Manoil static void enetc_setup_interrupts(struct enetc_ndev_priv *priv) 2224d4fd0404SClaudiu Manoil { 222591571081SClaudiu Manoil struct enetc_hw *hw = &priv->si->hw; 222691571081SClaudiu Manoil u32 icpt, ictt; 2227d4fd0404SClaudiu Manoil int i; 2228d4fd0404SClaudiu Manoil 2229d4fd0404SClaudiu Manoil /* enable Tx & Rx event indication */ 2230ae0e6a5dSClaudiu Manoil if (priv->ic_mode & 2231ae0e6a5dSClaudiu Manoil (ENETC_IC_RX_MANUAL | ENETC_IC_RX_ADAPTIVE)) { 223291571081SClaudiu Manoil icpt = ENETC_RBICR0_SET_ICPT(ENETC_RXIC_PKTTHR); 223391571081SClaudiu Manoil /* init to non-0 minimum, will be adjusted later */ 223491571081SClaudiu Manoil ictt = 0x1; 223591571081SClaudiu Manoil } else { 223691571081SClaudiu Manoil icpt = 0x1; /* enable Rx ints by setting pkt thr to 1 */ 223791571081SClaudiu Manoil ictt = 0; 2238d4fd0404SClaudiu Manoil } 2239d4fd0404SClaudiu Manoil 224091571081SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) { 224191571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBICR1, ictt); 224291571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBICR0, ENETC_RBICR0_ICEN | icpt); 224391571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBIER, ENETC_RBIER_RXTIE); 224491571081SClaudiu Manoil } 224591571081SClaudiu Manoil 224691571081SClaudiu Manoil if (priv->ic_mode & ENETC_IC_TX_MANUAL) 224791571081SClaudiu Manoil icpt = ENETC_TBICR0_SET_ICPT(ENETC_TXIC_PKTTHR); 224891571081SClaudiu Manoil else 224991571081SClaudiu Manoil icpt = 0x1; /* enable Tx ints by setting pkt thr to 1 */ 225091571081SClaudiu Manoil 2251d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 225291571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBICR1, priv->tx_ictt); 225391571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBICR0, ENETC_TBICR0_ICEN | icpt); 225491571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBIER, ENETC_TBIER_TXTIE); 2255d4fd0404SClaudiu Manoil } 2256d4fd0404SClaudiu Manoil } 2257d4fd0404SClaudiu Manoil 2258bbb96dc7SClaudiu Manoil static void enetc_clear_interrupts(struct enetc_ndev_priv *priv) 2259d4fd0404SClaudiu Manoil { 2260d4fd0404SClaudiu Manoil int i; 2261d4fd0404SClaudiu Manoil 2262d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2263d4fd0404SClaudiu Manoil enetc_txbdr_wr(&priv->si->hw, i, ENETC_TBIER, 0); 2264d4fd0404SClaudiu Manoil 2265d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2266d4fd0404SClaudiu Manoil enetc_rxbdr_wr(&priv->si->hw, i, ENETC_RBIER, 0); 2267d4fd0404SClaudiu Manoil } 2268d4fd0404SClaudiu Manoil 226971b77a7aSClaudiu Manoil static int enetc_phylink_connect(struct net_device *ndev) 2270d4fd0404SClaudiu Manoil { 22712e47cb41SPo Liu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2272a6a10d45SYangbo Lu struct ethtool_eee edata; 227371b77a7aSClaudiu Manoil int err; 2274d4fd0404SClaudiu Manoil 227571b77a7aSClaudiu Manoil if (!priv->phylink) 2276d4fd0404SClaudiu Manoil return 0; /* phy-less mode */ 2277d4fd0404SClaudiu Manoil 227871b77a7aSClaudiu Manoil err = phylink_of_phy_connect(priv->phylink, priv->dev->of_node, 0); 227971b77a7aSClaudiu Manoil if (err) { 2280d4fd0404SClaudiu Manoil dev_err(&ndev->dev, "could not attach to PHY\n"); 228171b77a7aSClaudiu Manoil return err; 2282d4fd0404SClaudiu Manoil } 2283d4fd0404SClaudiu Manoil 2284a6a10d45SYangbo Lu /* disable EEE autoneg, until ENETC driver supports it */ 2285a6a10d45SYangbo Lu memset(&edata, 0, sizeof(struct ethtool_eee)); 228671b77a7aSClaudiu Manoil phylink_ethtool_set_eee(priv->phylink, &edata); 2287a6a10d45SYangbo Lu 2288d4fd0404SClaudiu Manoil return 0; 2289d4fd0404SClaudiu Manoil } 2290d4fd0404SClaudiu Manoil 22917294380cSYangbo Lu static void enetc_tx_onestep_tstamp(struct work_struct *work) 22927294380cSYangbo Lu { 22937294380cSYangbo Lu struct enetc_ndev_priv *priv; 22947294380cSYangbo Lu struct sk_buff *skb; 22957294380cSYangbo Lu 22967294380cSYangbo Lu priv = container_of(work, struct enetc_ndev_priv, tx_onestep_tstamp); 22977294380cSYangbo Lu 22987294380cSYangbo Lu netif_tx_lock(priv->ndev); 22997294380cSYangbo Lu 23007294380cSYangbo Lu clear_bit_unlock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS, &priv->flags); 23017294380cSYangbo Lu skb = skb_dequeue(&priv->tx_skbs); 23027294380cSYangbo Lu if (skb) 23037294380cSYangbo Lu enetc_start_xmit(skb, priv->ndev); 23047294380cSYangbo Lu 23057294380cSYangbo Lu netif_tx_unlock(priv->ndev); 23067294380cSYangbo Lu } 23077294380cSYangbo Lu 23087294380cSYangbo Lu static void enetc_tx_onestep_tstamp_init(struct enetc_ndev_priv *priv) 23097294380cSYangbo Lu { 23107294380cSYangbo Lu INIT_WORK(&priv->tx_onestep_tstamp, enetc_tx_onestep_tstamp); 23117294380cSYangbo Lu skb_queue_head_init(&priv->tx_skbs); 23127294380cSYangbo Lu } 23137294380cSYangbo Lu 231491571081SClaudiu Manoil void enetc_start(struct net_device *ndev) 2315bbb96dc7SClaudiu Manoil { 2316bbb96dc7SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2317bbb96dc7SClaudiu Manoil int i; 2318bbb96dc7SClaudiu Manoil 2319bbb96dc7SClaudiu Manoil enetc_setup_interrupts(priv); 2320bbb96dc7SClaudiu Manoil 2321bbb96dc7SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2322bbb96dc7SClaudiu Manoil int irq = pci_irq_vector(priv->si->pdev, 2323bbb96dc7SClaudiu Manoil ENETC_BDR_INT_BASE_IDX + i); 2324bbb96dc7SClaudiu Manoil 2325bbb96dc7SClaudiu Manoil napi_enable(&priv->int_vector[i]->napi); 2326bbb96dc7SClaudiu Manoil enable_irq(irq); 2327bbb96dc7SClaudiu Manoil } 2328bbb96dc7SClaudiu Manoil 232971b77a7aSClaudiu Manoil if (priv->phylink) 233071b77a7aSClaudiu Manoil phylink_start(priv->phylink); 2331bbb96dc7SClaudiu Manoil else 2332bbb96dc7SClaudiu Manoil netif_carrier_on(ndev); 2333bbb96dc7SClaudiu Manoil 2334bbb96dc7SClaudiu Manoil netif_tx_start_all_queues(ndev); 2335bbb96dc7SClaudiu Manoil } 2336bbb96dc7SClaudiu Manoil 2337d4fd0404SClaudiu Manoil int enetc_open(struct net_device *ndev) 2338d4fd0404SClaudiu Manoil { 2339d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 23407eab503bSVladimir Oltean int num_stack_tx_queues; 2341bbb96dc7SClaudiu Manoil int err; 2342d4fd0404SClaudiu Manoil 2343d4fd0404SClaudiu Manoil err = enetc_setup_irqs(priv); 2344d4fd0404SClaudiu Manoil if (err) 2345d4fd0404SClaudiu Manoil return err; 2346d4fd0404SClaudiu Manoil 234771b77a7aSClaudiu Manoil err = enetc_phylink_connect(ndev); 2348d4fd0404SClaudiu Manoil if (err) 2349d4fd0404SClaudiu Manoil goto err_phy_connect; 2350d4fd0404SClaudiu Manoil 2351d4fd0404SClaudiu Manoil err = enetc_alloc_tx_resources(priv); 2352d4fd0404SClaudiu Manoil if (err) 2353d4fd0404SClaudiu Manoil goto err_alloc_tx; 2354d4fd0404SClaudiu Manoil 2355d4fd0404SClaudiu Manoil err = enetc_alloc_rx_resources(priv); 2356d4fd0404SClaudiu Manoil if (err) 2357d4fd0404SClaudiu Manoil goto err_alloc_rx; 2358d4fd0404SClaudiu Manoil 23597eab503bSVladimir Oltean num_stack_tx_queues = enetc_num_stack_tx_queues(priv); 23607eab503bSVladimir Oltean 23617eab503bSVladimir Oltean err = netif_set_real_num_tx_queues(ndev, num_stack_tx_queues); 2362d4fd0404SClaudiu Manoil if (err) 2363d4fd0404SClaudiu Manoil goto err_set_queues; 2364d4fd0404SClaudiu Manoil 2365d4fd0404SClaudiu Manoil err = netif_set_real_num_rx_queues(ndev, priv->num_rx_rings); 2366d4fd0404SClaudiu Manoil if (err) 2367d4fd0404SClaudiu Manoil goto err_set_queues; 2368d4fd0404SClaudiu Manoil 23697294380cSYangbo Lu enetc_tx_onestep_tstamp_init(priv); 2370bbb96dc7SClaudiu Manoil enetc_setup_bdrs(priv); 2371bbb96dc7SClaudiu Manoil enetc_start(ndev); 2372d4fd0404SClaudiu Manoil 2373d4fd0404SClaudiu Manoil return 0; 2374d4fd0404SClaudiu Manoil 2375d4fd0404SClaudiu Manoil err_set_queues: 2376d4fd0404SClaudiu Manoil enetc_free_rx_resources(priv); 2377d4fd0404SClaudiu Manoil err_alloc_rx: 2378d4fd0404SClaudiu Manoil enetc_free_tx_resources(priv); 2379d4fd0404SClaudiu Manoil err_alloc_tx: 238071b77a7aSClaudiu Manoil if (priv->phylink) 238171b77a7aSClaudiu Manoil phylink_disconnect_phy(priv->phylink); 2382d4fd0404SClaudiu Manoil err_phy_connect: 2383d4fd0404SClaudiu Manoil enetc_free_irqs(priv); 2384d4fd0404SClaudiu Manoil 2385d4fd0404SClaudiu Manoil return err; 2386d4fd0404SClaudiu Manoil } 2387d4fd0404SClaudiu Manoil 238891571081SClaudiu Manoil void enetc_stop(struct net_device *ndev) 2389d4fd0404SClaudiu Manoil { 2390d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2391d4fd0404SClaudiu Manoil int i; 2392d4fd0404SClaudiu Manoil 2393d4fd0404SClaudiu Manoil netif_tx_stop_all_queues(ndev); 2394d4fd0404SClaudiu Manoil 2395d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2396bbb96dc7SClaudiu Manoil int irq = pci_irq_vector(priv->si->pdev, 2397bbb96dc7SClaudiu Manoil ENETC_BDR_INT_BASE_IDX + i); 2398bbb96dc7SClaudiu Manoil 2399bbb96dc7SClaudiu Manoil disable_irq(irq); 2400d4fd0404SClaudiu Manoil napi_synchronize(&priv->int_vector[i]->napi); 2401d4fd0404SClaudiu Manoil napi_disable(&priv->int_vector[i]->napi); 2402d4fd0404SClaudiu Manoil } 2403d4fd0404SClaudiu Manoil 240471b77a7aSClaudiu Manoil if (priv->phylink) 240571b77a7aSClaudiu Manoil phylink_stop(priv->phylink); 2406bbb96dc7SClaudiu Manoil else 2407bbb96dc7SClaudiu Manoil netif_carrier_off(ndev); 2408bbb96dc7SClaudiu Manoil 2409bbb96dc7SClaudiu Manoil enetc_clear_interrupts(priv); 2410bbb96dc7SClaudiu Manoil } 2411bbb96dc7SClaudiu Manoil 2412bbb96dc7SClaudiu Manoil int enetc_close(struct net_device *ndev) 2413bbb96dc7SClaudiu Manoil { 2414bbb96dc7SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2415bbb96dc7SClaudiu Manoil 2416bbb96dc7SClaudiu Manoil enetc_stop(ndev); 2417d4fd0404SClaudiu Manoil enetc_clear_bdrs(priv); 2418d4fd0404SClaudiu Manoil 241971b77a7aSClaudiu Manoil if (priv->phylink) 242071b77a7aSClaudiu Manoil phylink_disconnect_phy(priv->phylink); 2421d4fd0404SClaudiu Manoil enetc_free_rxtx_rings(priv); 2422d4fd0404SClaudiu Manoil enetc_free_rx_resources(priv); 2423d4fd0404SClaudiu Manoil enetc_free_tx_resources(priv); 2424d4fd0404SClaudiu Manoil enetc_free_irqs(priv); 2425d4fd0404SClaudiu Manoil 2426d4fd0404SClaudiu Manoil return 0; 2427d4fd0404SClaudiu Manoil } 2428d4fd0404SClaudiu Manoil 242913baf667SMao Wenan static int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data) 2430cbe9e835SCamelia Groza { 2431cbe9e835SCamelia Groza struct enetc_ndev_priv *priv = netdev_priv(ndev); 2432cbe9e835SCamelia Groza struct tc_mqprio_qopt *mqprio = type_data; 2433cbe9e835SCamelia Groza struct enetc_bdr *tx_ring; 24347eab503bSVladimir Oltean int num_stack_tx_queues; 2435cbe9e835SCamelia Groza u8 num_tc; 2436cbe9e835SCamelia Groza int i; 2437cbe9e835SCamelia Groza 24387eab503bSVladimir Oltean num_stack_tx_queues = enetc_num_stack_tx_queues(priv); 2439cbe9e835SCamelia Groza mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 2440cbe9e835SCamelia Groza num_tc = mqprio->num_tc; 2441cbe9e835SCamelia Groza 2442cbe9e835SCamelia Groza if (!num_tc) { 2443cbe9e835SCamelia Groza netdev_reset_tc(ndev); 24447eab503bSVladimir Oltean netif_set_real_num_tx_queues(ndev, num_stack_tx_queues); 2445cbe9e835SCamelia Groza 2446cbe9e835SCamelia Groza /* Reset all ring priorities to 0 */ 2447cbe9e835SCamelia Groza for (i = 0; i < priv->num_tx_rings; i++) { 2448cbe9e835SCamelia Groza tx_ring = priv->tx_ring[i]; 2449cbe9e835SCamelia Groza enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, 0); 2450cbe9e835SCamelia Groza } 2451cbe9e835SCamelia Groza 2452cbe9e835SCamelia Groza return 0; 2453cbe9e835SCamelia Groza } 2454cbe9e835SCamelia Groza 2455cbe9e835SCamelia Groza /* Check if we have enough BD rings available to accommodate all TCs */ 24567eab503bSVladimir Oltean if (num_tc > num_stack_tx_queues) { 2457cbe9e835SCamelia Groza netdev_err(ndev, "Max %d traffic classes supported\n", 2458cbe9e835SCamelia Groza priv->num_tx_rings); 2459cbe9e835SCamelia Groza return -EINVAL; 2460cbe9e835SCamelia Groza } 2461cbe9e835SCamelia Groza 2462cbe9e835SCamelia Groza /* For the moment, we use only one BD ring per TC. 2463cbe9e835SCamelia Groza * 2464cbe9e835SCamelia Groza * Configure num_tc BD rings with increasing priorities. 2465cbe9e835SCamelia Groza */ 2466cbe9e835SCamelia Groza for (i = 0; i < num_tc; i++) { 2467cbe9e835SCamelia Groza tx_ring = priv->tx_ring[i]; 2468cbe9e835SCamelia Groza enetc_set_bdr_prio(&priv->si->hw, tx_ring->index, i); 2469cbe9e835SCamelia Groza } 2470cbe9e835SCamelia Groza 2471cbe9e835SCamelia Groza /* Reset the number of netdev queues based on the TC count */ 2472cbe9e835SCamelia Groza netif_set_real_num_tx_queues(ndev, num_tc); 2473cbe9e835SCamelia Groza 2474cbe9e835SCamelia Groza netdev_set_num_tc(ndev, num_tc); 2475cbe9e835SCamelia Groza 2476cbe9e835SCamelia Groza /* Each TC is associated with one netdev queue */ 2477cbe9e835SCamelia Groza for (i = 0; i < num_tc; i++) 2478cbe9e835SCamelia Groza netdev_set_tc_queue(ndev, i, 1, i); 2479cbe9e835SCamelia Groza 2480cbe9e835SCamelia Groza return 0; 2481cbe9e835SCamelia Groza } 2482cbe9e835SCamelia Groza 248334c6adf1SPo Liu int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type, 248434c6adf1SPo Liu void *type_data) 248534c6adf1SPo Liu { 248634c6adf1SPo Liu switch (type) { 248734c6adf1SPo Liu case TC_SETUP_QDISC_MQPRIO: 248834c6adf1SPo Liu return enetc_setup_tc_mqprio(ndev, type_data); 248934c6adf1SPo Liu case TC_SETUP_QDISC_TAPRIO: 249034c6adf1SPo Liu return enetc_setup_tc_taprio(ndev, type_data); 2491c431047cSPo Liu case TC_SETUP_QDISC_CBS: 2492c431047cSPo Liu return enetc_setup_tc_cbs(ndev, type_data); 24930d08c9ecSPo Liu case TC_SETUP_QDISC_ETF: 24940d08c9ecSPo Liu return enetc_setup_tc_txtime(ndev, type_data); 2495888ae5a3SPo Liu case TC_SETUP_BLOCK: 2496888ae5a3SPo Liu return enetc_setup_tc_psfp(ndev, type_data); 249734c6adf1SPo Liu default: 249834c6adf1SPo Liu return -EOPNOTSUPP; 249934c6adf1SPo Liu } 250034c6adf1SPo Liu } 250134c6adf1SPo Liu 2502d1b15102SVladimir Oltean static int enetc_setup_xdp_prog(struct net_device *dev, struct bpf_prog *prog, 2503d1b15102SVladimir Oltean struct netlink_ext_ack *extack) 2504d1b15102SVladimir Oltean { 2505d1b15102SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(dev); 2506d1b15102SVladimir Oltean struct bpf_prog *old_prog; 2507d1b15102SVladimir Oltean bool is_up; 2508d1b15102SVladimir Oltean int i; 2509d1b15102SVladimir Oltean 2510d1b15102SVladimir Oltean /* The buffer layout is changing, so we need to drain the old 2511d1b15102SVladimir Oltean * RX buffers and seed new ones. 2512d1b15102SVladimir Oltean */ 2513d1b15102SVladimir Oltean is_up = netif_running(dev); 2514d1b15102SVladimir Oltean if (is_up) 2515d1b15102SVladimir Oltean dev_close(dev); 2516d1b15102SVladimir Oltean 2517d1b15102SVladimir Oltean old_prog = xchg(&priv->xdp_prog, prog); 2518d1b15102SVladimir Oltean if (old_prog) 2519d1b15102SVladimir Oltean bpf_prog_put(old_prog); 2520d1b15102SVladimir Oltean 2521d1b15102SVladimir Oltean for (i = 0; i < priv->num_rx_rings; i++) { 2522d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = priv->rx_ring[i]; 2523d1b15102SVladimir Oltean 2524d1b15102SVladimir Oltean rx_ring->xdp.prog = prog; 2525d1b15102SVladimir Oltean 2526d1b15102SVladimir Oltean if (prog) 2527d1b15102SVladimir Oltean rx_ring->buffer_offset = XDP_PACKET_HEADROOM; 2528d1b15102SVladimir Oltean else 2529d1b15102SVladimir Oltean rx_ring->buffer_offset = ENETC_RXB_PAD; 2530d1b15102SVladimir Oltean } 2531d1b15102SVladimir Oltean 2532d1b15102SVladimir Oltean if (is_up) 2533d1b15102SVladimir Oltean return dev_open(dev, extack); 2534d1b15102SVladimir Oltean 2535d1b15102SVladimir Oltean return 0; 2536d1b15102SVladimir Oltean } 2537d1b15102SVladimir Oltean 2538d1b15102SVladimir Oltean int enetc_setup_bpf(struct net_device *dev, struct netdev_bpf *xdp) 2539d1b15102SVladimir Oltean { 2540d1b15102SVladimir Oltean switch (xdp->command) { 2541d1b15102SVladimir Oltean case XDP_SETUP_PROG: 2542d1b15102SVladimir Oltean return enetc_setup_xdp_prog(dev, xdp->prog, xdp->extack); 2543d1b15102SVladimir Oltean default: 2544d1b15102SVladimir Oltean return -EINVAL; 2545d1b15102SVladimir Oltean } 2546d1b15102SVladimir Oltean 2547d1b15102SVladimir Oltean return 0; 2548d1b15102SVladimir Oltean } 2549d1b15102SVladimir Oltean 2550d4fd0404SClaudiu Manoil struct net_device_stats *enetc_get_stats(struct net_device *ndev) 2551d4fd0404SClaudiu Manoil { 2552d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2553d4fd0404SClaudiu Manoil struct net_device_stats *stats = &ndev->stats; 2554d4fd0404SClaudiu Manoil unsigned long packets = 0, bytes = 0; 2555d4fd0404SClaudiu Manoil int i; 2556d4fd0404SClaudiu Manoil 2557d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) { 2558d4fd0404SClaudiu Manoil packets += priv->rx_ring[i]->stats.packets; 2559d4fd0404SClaudiu Manoil bytes += priv->rx_ring[i]->stats.bytes; 2560d4fd0404SClaudiu Manoil } 2561d4fd0404SClaudiu Manoil 2562d4fd0404SClaudiu Manoil stats->rx_packets = packets; 2563d4fd0404SClaudiu Manoil stats->rx_bytes = bytes; 2564d4fd0404SClaudiu Manoil bytes = 0; 2565d4fd0404SClaudiu Manoil packets = 0; 2566d4fd0404SClaudiu Manoil 2567d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 2568d4fd0404SClaudiu Manoil packets += priv->tx_ring[i]->stats.packets; 2569d4fd0404SClaudiu Manoil bytes += priv->tx_ring[i]->stats.bytes; 2570d4fd0404SClaudiu Manoil } 2571d4fd0404SClaudiu Manoil 2572d4fd0404SClaudiu Manoil stats->tx_packets = packets; 2573d4fd0404SClaudiu Manoil stats->tx_bytes = bytes; 2574d4fd0404SClaudiu Manoil 2575d4fd0404SClaudiu Manoil return stats; 2576d4fd0404SClaudiu Manoil } 2577d4fd0404SClaudiu Manoil 2578d382563fSClaudiu Manoil static int enetc_set_rss(struct net_device *ndev, int en) 2579d382563fSClaudiu Manoil { 2580d382563fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2581d382563fSClaudiu Manoil struct enetc_hw *hw = &priv->si->hw; 2582d382563fSClaudiu Manoil u32 reg; 2583d382563fSClaudiu Manoil 2584d382563fSClaudiu Manoil enetc_wr(hw, ENETC_SIRBGCR, priv->num_rx_rings); 2585d382563fSClaudiu Manoil 2586d382563fSClaudiu Manoil reg = enetc_rd(hw, ENETC_SIMR); 2587d382563fSClaudiu Manoil reg &= ~ENETC_SIMR_RSSE; 2588d382563fSClaudiu Manoil reg |= (en) ? ENETC_SIMR_RSSE : 0; 2589d382563fSClaudiu Manoil enetc_wr(hw, ENETC_SIMR, reg); 2590d382563fSClaudiu Manoil 2591d382563fSClaudiu Manoil return 0; 2592d382563fSClaudiu Manoil } 2593d382563fSClaudiu Manoil 259479e49982SPo Liu static int enetc_set_psfp(struct net_device *ndev, int en) 259579e49982SPo Liu { 259679e49982SPo Liu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2597888ae5a3SPo Liu int err; 259879e49982SPo Liu 259979e49982SPo Liu if (en) { 2600888ae5a3SPo Liu err = enetc_psfp_enable(priv); 2601888ae5a3SPo Liu if (err) 2602888ae5a3SPo Liu return err; 2603888ae5a3SPo Liu 260479e49982SPo Liu priv->active_offloads |= ENETC_F_QCI; 2605888ae5a3SPo Liu return 0; 260679e49982SPo Liu } 260779e49982SPo Liu 2608888ae5a3SPo Liu err = enetc_psfp_disable(priv); 2609888ae5a3SPo Liu if (err) 2610888ae5a3SPo Liu return err; 2611888ae5a3SPo Liu 2612888ae5a3SPo Liu priv->active_offloads &= ~ENETC_F_QCI; 2613888ae5a3SPo Liu 261479e49982SPo Liu return 0; 261579e49982SPo Liu } 261679e49982SPo Liu 26179deba33fSClaudiu Manoil static void enetc_enable_rxvlan(struct net_device *ndev, bool en) 26189deba33fSClaudiu Manoil { 26199deba33fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 26209deba33fSClaudiu Manoil int i; 26219deba33fSClaudiu Manoil 26229deba33fSClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 26239deba33fSClaudiu Manoil enetc_bdr_enable_rxvlan(&priv->si->hw, i, en); 26249deba33fSClaudiu Manoil } 26259deba33fSClaudiu Manoil 26269deba33fSClaudiu Manoil static void enetc_enable_txvlan(struct net_device *ndev, bool en) 26279deba33fSClaudiu Manoil { 26289deba33fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 26299deba33fSClaudiu Manoil int i; 26309deba33fSClaudiu Manoil 26319deba33fSClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 26329deba33fSClaudiu Manoil enetc_bdr_enable_txvlan(&priv->si->hw, i, en); 26339deba33fSClaudiu Manoil } 26349deba33fSClaudiu Manoil 2635d382563fSClaudiu Manoil int enetc_set_features(struct net_device *ndev, 2636d382563fSClaudiu Manoil netdev_features_t features) 2637d382563fSClaudiu Manoil { 2638d382563fSClaudiu Manoil netdev_features_t changed = ndev->features ^ features; 2639888ae5a3SPo Liu int err = 0; 2640d382563fSClaudiu Manoil 2641d382563fSClaudiu Manoil if (changed & NETIF_F_RXHASH) 2642d382563fSClaudiu Manoil enetc_set_rss(ndev, !!(features & NETIF_F_RXHASH)); 2643d382563fSClaudiu Manoil 26449deba33fSClaudiu Manoil if (changed & NETIF_F_HW_VLAN_CTAG_RX) 26459deba33fSClaudiu Manoil enetc_enable_rxvlan(ndev, 26469deba33fSClaudiu Manoil !!(features & NETIF_F_HW_VLAN_CTAG_RX)); 26479deba33fSClaudiu Manoil 26489deba33fSClaudiu Manoil if (changed & NETIF_F_HW_VLAN_CTAG_TX) 26499deba33fSClaudiu Manoil enetc_enable_txvlan(ndev, 26509deba33fSClaudiu Manoil !!(features & NETIF_F_HW_VLAN_CTAG_TX)); 26519deba33fSClaudiu Manoil 265279e49982SPo Liu if (changed & NETIF_F_HW_TC) 2653888ae5a3SPo Liu err = enetc_set_psfp(ndev, !!(features & NETIF_F_HW_TC)); 265479e49982SPo Liu 2655888ae5a3SPo Liu return err; 2656d382563fSClaudiu Manoil } 2657d382563fSClaudiu Manoil 2658434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 2659d3982312SY.b. Lu static int enetc_hwtstamp_set(struct net_device *ndev, struct ifreq *ifr) 2660d3982312SY.b. Lu { 2661d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2662d3982312SY.b. Lu struct hwtstamp_config config; 2663434cebabSClaudiu Manoil int ao; 2664d3982312SY.b. Lu 2665d3982312SY.b. Lu if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2666d3982312SY.b. Lu return -EFAULT; 2667d3982312SY.b. Lu 2668d3982312SY.b. Lu switch (config.tx_type) { 2669d3982312SY.b. Lu case HWTSTAMP_TX_OFF: 26707294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 2671d3982312SY.b. Lu break; 2672d3982312SY.b. Lu case HWTSTAMP_TX_ON: 26737294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 2674d3982312SY.b. Lu priv->active_offloads |= ENETC_F_TX_TSTAMP; 2675d3982312SY.b. Lu break; 26767294380cSYangbo Lu case HWTSTAMP_TX_ONESTEP_SYNC: 26777294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 26787294380cSYangbo Lu priv->active_offloads |= ENETC_F_TX_ONESTEP_SYNC_TSTAMP; 26797294380cSYangbo Lu break; 2680d3982312SY.b. Lu default: 2681d3982312SY.b. Lu return -ERANGE; 2682d3982312SY.b. Lu } 2683d3982312SY.b. Lu 2684434cebabSClaudiu Manoil ao = priv->active_offloads; 2685d3982312SY.b. Lu switch (config.rx_filter) { 2686d3982312SY.b. Lu case HWTSTAMP_FILTER_NONE: 2687d3982312SY.b. Lu priv->active_offloads &= ~ENETC_F_RX_TSTAMP; 2688d3982312SY.b. Lu break; 2689d3982312SY.b. Lu default: 2690d3982312SY.b. Lu priv->active_offloads |= ENETC_F_RX_TSTAMP; 2691d3982312SY.b. Lu config.rx_filter = HWTSTAMP_FILTER_ALL; 2692d3982312SY.b. Lu } 2693d3982312SY.b. Lu 2694434cebabSClaudiu Manoil if (netif_running(ndev) && ao != priv->active_offloads) { 2695434cebabSClaudiu Manoil enetc_close(ndev); 2696434cebabSClaudiu Manoil enetc_open(ndev); 2697434cebabSClaudiu Manoil } 2698434cebabSClaudiu Manoil 2699d3982312SY.b. Lu return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2700d3982312SY.b. Lu -EFAULT : 0; 2701d3982312SY.b. Lu } 2702d3982312SY.b. Lu 2703d3982312SY.b. Lu static int enetc_hwtstamp_get(struct net_device *ndev, struct ifreq *ifr) 2704d3982312SY.b. Lu { 2705d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2706d3982312SY.b. Lu struct hwtstamp_config config; 2707d3982312SY.b. Lu 2708d3982312SY.b. Lu config.flags = 0; 2709d3982312SY.b. Lu 27107294380cSYangbo Lu if (priv->active_offloads & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) 27117294380cSYangbo Lu config.tx_type = HWTSTAMP_TX_ONESTEP_SYNC; 27127294380cSYangbo Lu else if (priv->active_offloads & ENETC_F_TX_TSTAMP) 2713d3982312SY.b. Lu config.tx_type = HWTSTAMP_TX_ON; 2714d3982312SY.b. Lu else 2715d3982312SY.b. Lu config.tx_type = HWTSTAMP_TX_OFF; 2716d3982312SY.b. Lu 2717d3982312SY.b. Lu config.rx_filter = (priv->active_offloads & ENETC_F_RX_TSTAMP) ? 2718d3982312SY.b. Lu HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 2719d3982312SY.b. Lu 2720d3982312SY.b. Lu return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2721d3982312SY.b. Lu -EFAULT : 0; 2722d3982312SY.b. Lu } 2723d3982312SY.b. Lu #endif 2724d3982312SY.b. Lu 2725d3982312SY.b. Lu int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 2726d3982312SY.b. Lu { 272771b77a7aSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2728434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 2729d3982312SY.b. Lu if (cmd == SIOCSHWTSTAMP) 2730d3982312SY.b. Lu return enetc_hwtstamp_set(ndev, rq); 2731d3982312SY.b. Lu if (cmd == SIOCGHWTSTAMP) 2732d3982312SY.b. Lu return enetc_hwtstamp_get(ndev, rq); 2733d3982312SY.b. Lu #endif 2734a613bafeSMichael Walle 273571b77a7aSClaudiu Manoil if (!priv->phylink) 2736c55b810aSMichael Walle return -EOPNOTSUPP; 273771b77a7aSClaudiu Manoil 273871b77a7aSClaudiu Manoil return phylink_mii_ioctl(priv->phylink, rq, cmd); 2739d3982312SY.b. Lu } 2740d3982312SY.b. Lu 2741d4fd0404SClaudiu Manoil int enetc_alloc_msix(struct enetc_ndev_priv *priv) 2742d4fd0404SClaudiu Manoil { 2743d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 27447eab503bSVladimir Oltean int first_xdp_tx_ring; 2745d4fd0404SClaudiu Manoil int i, n, err, nvec; 27467eab503bSVladimir Oltean int v_tx_rings; 2747d4fd0404SClaudiu Manoil 2748d4fd0404SClaudiu Manoil nvec = ENETC_BDR_INT_BASE_IDX + priv->bdr_int_num; 2749d4fd0404SClaudiu Manoil /* allocate MSIX for both messaging and Rx/Tx interrupts */ 2750d4fd0404SClaudiu Manoil n = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX); 2751d4fd0404SClaudiu Manoil 2752d4fd0404SClaudiu Manoil if (n < 0) 2753d4fd0404SClaudiu Manoil return n; 2754d4fd0404SClaudiu Manoil 2755d4fd0404SClaudiu Manoil if (n != nvec) 2756d4fd0404SClaudiu Manoil return -EPERM; 2757d4fd0404SClaudiu Manoil 2758d4fd0404SClaudiu Manoil /* # of tx rings per int vector */ 2759d4fd0404SClaudiu Manoil v_tx_rings = priv->num_tx_rings / priv->bdr_int_num; 2760d4fd0404SClaudiu Manoil 2761d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2762d4fd0404SClaudiu Manoil struct enetc_int_vector *v; 2763d4fd0404SClaudiu Manoil struct enetc_bdr *bdr; 2764d4fd0404SClaudiu Manoil int j; 2765d4fd0404SClaudiu Manoil 27661260e772SGustavo A. R. Silva v = kzalloc(struct_size(v, tx_ring, v_tx_rings), GFP_KERNEL); 2767d4fd0404SClaudiu Manoil if (!v) { 2768d4fd0404SClaudiu Manoil err = -ENOMEM; 2769d4fd0404SClaudiu Manoil goto fail; 2770d4fd0404SClaudiu Manoil } 2771d4fd0404SClaudiu Manoil 2772d4fd0404SClaudiu Manoil priv->int_vector[i] = v; 2773d4fd0404SClaudiu Manoil 2774d1b15102SVladimir Oltean bdr = &v->rx_ring; 2775d1b15102SVladimir Oltean bdr->index = i; 2776d1b15102SVladimir Oltean bdr->ndev = priv->ndev; 2777d1b15102SVladimir Oltean bdr->dev = priv->dev; 2778d1b15102SVladimir Oltean bdr->bd_count = priv->rx_bd_count; 2779d1b15102SVladimir Oltean bdr->buffer_offset = ENETC_RXB_PAD; 2780d1b15102SVladimir Oltean priv->rx_ring[i] = bdr; 2781d1b15102SVladimir Oltean 2782d1b15102SVladimir Oltean err = xdp_rxq_info_reg(&bdr->xdp.rxq, priv->ndev, i, 0); 2783d1b15102SVladimir Oltean if (err) { 2784d1b15102SVladimir Oltean kfree(v); 2785d1b15102SVladimir Oltean goto fail; 2786d1b15102SVladimir Oltean } 2787d1b15102SVladimir Oltean 2788d1b15102SVladimir Oltean err = xdp_rxq_info_reg_mem_model(&bdr->xdp.rxq, 2789d1b15102SVladimir Oltean MEM_TYPE_PAGE_SHARED, NULL); 2790d1b15102SVladimir Oltean if (err) { 2791d1b15102SVladimir Oltean xdp_rxq_info_unreg(&bdr->xdp.rxq); 2792d1b15102SVladimir Oltean kfree(v); 2793d1b15102SVladimir Oltean goto fail; 2794d1b15102SVladimir Oltean } 2795d1b15102SVladimir Oltean 2796ae0e6a5dSClaudiu Manoil /* init defaults for adaptive IC */ 2797ae0e6a5dSClaudiu Manoil if (priv->ic_mode & ENETC_IC_RX_ADAPTIVE) { 2798ae0e6a5dSClaudiu Manoil v->rx_ictt = 0x1; 2799ae0e6a5dSClaudiu Manoil v->rx_dim_en = true; 2800ae0e6a5dSClaudiu Manoil } 2801ae0e6a5dSClaudiu Manoil INIT_WORK(&v->rx_dim.work, enetc_rx_dim_work); 2802d4fd0404SClaudiu Manoil netif_napi_add(priv->ndev, &v->napi, enetc_poll, 2803d4fd0404SClaudiu Manoil NAPI_POLL_WEIGHT); 2804d4fd0404SClaudiu Manoil v->count_tx_rings = v_tx_rings; 2805d4fd0404SClaudiu Manoil 2806d4fd0404SClaudiu Manoil for (j = 0; j < v_tx_rings; j++) { 2807d4fd0404SClaudiu Manoil int idx; 2808d4fd0404SClaudiu Manoil 2809d4fd0404SClaudiu Manoil /* default tx ring mapping policy */ 28106c5e6b4cSClaudiu Manoil idx = priv->bdr_int_num * j + i; 2811d4fd0404SClaudiu Manoil __set_bit(idx, &v->tx_rings_map); 2812d4fd0404SClaudiu Manoil bdr = &v->tx_ring[j]; 2813d4fd0404SClaudiu Manoil bdr->index = idx; 2814d4fd0404SClaudiu Manoil bdr->ndev = priv->ndev; 2815d4fd0404SClaudiu Manoil bdr->dev = priv->dev; 2816d4fd0404SClaudiu Manoil bdr->bd_count = priv->tx_bd_count; 2817d4fd0404SClaudiu Manoil priv->tx_ring[idx] = bdr; 2818d4fd0404SClaudiu Manoil } 2819d4fd0404SClaudiu Manoil } 2820d4fd0404SClaudiu Manoil 28217eab503bSVladimir Oltean first_xdp_tx_ring = priv->num_tx_rings - num_possible_cpus(); 28227eab503bSVladimir Oltean priv->xdp_tx_ring = &priv->tx_ring[first_xdp_tx_ring]; 28237eab503bSVladimir Oltean 2824d4fd0404SClaudiu Manoil return 0; 2825d4fd0404SClaudiu Manoil 2826d4fd0404SClaudiu Manoil fail: 2827d4fd0404SClaudiu Manoil while (i--) { 2828d1b15102SVladimir Oltean struct enetc_int_vector *v = priv->int_vector[i]; 2829d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 2830d1b15102SVladimir Oltean 2831d1b15102SVladimir Oltean xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq); 2832d1b15102SVladimir Oltean xdp_rxq_info_unreg(&rx_ring->xdp.rxq); 2833d1b15102SVladimir Oltean netif_napi_del(&v->napi); 2834d1b15102SVladimir Oltean cancel_work_sync(&v->rx_dim.work); 2835d1b15102SVladimir Oltean kfree(v); 2836d4fd0404SClaudiu Manoil } 2837d4fd0404SClaudiu Manoil 2838d4fd0404SClaudiu Manoil pci_free_irq_vectors(pdev); 2839d4fd0404SClaudiu Manoil 2840d4fd0404SClaudiu Manoil return err; 2841d4fd0404SClaudiu Manoil } 2842d4fd0404SClaudiu Manoil 2843d4fd0404SClaudiu Manoil void enetc_free_msix(struct enetc_ndev_priv *priv) 2844d4fd0404SClaudiu Manoil { 2845d4fd0404SClaudiu Manoil int i; 2846d4fd0404SClaudiu Manoil 2847d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2848d4fd0404SClaudiu Manoil struct enetc_int_vector *v = priv->int_vector[i]; 2849d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 2850d4fd0404SClaudiu Manoil 2851d1b15102SVladimir Oltean xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq); 2852d1b15102SVladimir Oltean xdp_rxq_info_unreg(&rx_ring->xdp.rxq); 2853d4fd0404SClaudiu Manoil netif_napi_del(&v->napi); 2854ae0e6a5dSClaudiu Manoil cancel_work_sync(&v->rx_dim.work); 2855d4fd0404SClaudiu Manoil } 2856d4fd0404SClaudiu Manoil 2857d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2858d4fd0404SClaudiu Manoil priv->rx_ring[i] = NULL; 2859d4fd0404SClaudiu Manoil 2860d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2861d4fd0404SClaudiu Manoil priv->tx_ring[i] = NULL; 2862d4fd0404SClaudiu Manoil 2863d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2864d4fd0404SClaudiu Manoil kfree(priv->int_vector[i]); 2865d4fd0404SClaudiu Manoil priv->int_vector[i] = NULL; 2866d4fd0404SClaudiu Manoil } 2867d4fd0404SClaudiu Manoil 2868d4fd0404SClaudiu Manoil /* disable all MSIX for this device */ 2869d4fd0404SClaudiu Manoil pci_free_irq_vectors(priv->si->pdev); 2870d4fd0404SClaudiu Manoil } 2871d4fd0404SClaudiu Manoil 2872d4fd0404SClaudiu Manoil static void enetc_kfree_si(struct enetc_si *si) 2873d4fd0404SClaudiu Manoil { 2874d4fd0404SClaudiu Manoil char *p = (char *)si - si->pad; 2875d4fd0404SClaudiu Manoil 2876d4fd0404SClaudiu Manoil kfree(p); 2877d4fd0404SClaudiu Manoil } 2878d4fd0404SClaudiu Manoil 2879d4fd0404SClaudiu Manoil static void enetc_detect_errata(struct enetc_si *si) 2880d4fd0404SClaudiu Manoil { 2881d4fd0404SClaudiu Manoil if (si->pdev->revision == ENETC_REV1) 288282728b91SClaudiu Manoil si->errata = ENETC_ERR_VLAN_ISOL | ENETC_ERR_UCMCSWP; 2883d4fd0404SClaudiu Manoil } 2884d4fd0404SClaudiu Manoil 2885d4fd0404SClaudiu Manoil int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv) 2886d4fd0404SClaudiu Manoil { 2887d4fd0404SClaudiu Manoil struct enetc_si *si, *p; 2888d4fd0404SClaudiu Manoil struct enetc_hw *hw; 2889d4fd0404SClaudiu Manoil size_t alloc_size; 2890d4fd0404SClaudiu Manoil int err, len; 2891d4fd0404SClaudiu Manoil 2892d4fd0404SClaudiu Manoil pcie_flr(pdev); 2893d4fd0404SClaudiu Manoil err = pci_enable_device_mem(pdev); 2894a72691eeSCai Huoqing if (err) 2895a72691eeSCai Huoqing return dev_err_probe(&pdev->dev, err, "device enable failed\n"); 2896d4fd0404SClaudiu Manoil 2897d4fd0404SClaudiu Manoil /* set up for high or low dma */ 2898d4fd0404SClaudiu Manoil err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 2899d4fd0404SClaudiu Manoil if (err) { 2900d4fd0404SClaudiu Manoil err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 2901d4fd0404SClaudiu Manoil if (err) { 2902d4fd0404SClaudiu Manoil dev_err(&pdev->dev, 2903d4fd0404SClaudiu Manoil "DMA configuration failed: 0x%x\n", err); 2904d4fd0404SClaudiu Manoil goto err_dma; 2905d4fd0404SClaudiu Manoil } 2906d4fd0404SClaudiu Manoil } 2907d4fd0404SClaudiu Manoil 2908d4fd0404SClaudiu Manoil err = pci_request_mem_regions(pdev, name); 2909d4fd0404SClaudiu Manoil if (err) { 2910d4fd0404SClaudiu Manoil dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err); 2911d4fd0404SClaudiu Manoil goto err_pci_mem_reg; 2912d4fd0404SClaudiu Manoil } 2913d4fd0404SClaudiu Manoil 2914d4fd0404SClaudiu Manoil pci_set_master(pdev); 2915d4fd0404SClaudiu Manoil 2916d4fd0404SClaudiu Manoil alloc_size = sizeof(struct enetc_si); 2917d4fd0404SClaudiu Manoil if (sizeof_priv) { 2918d4fd0404SClaudiu Manoil /* align priv to 32B */ 2919d4fd0404SClaudiu Manoil alloc_size = ALIGN(alloc_size, ENETC_SI_ALIGN); 2920d4fd0404SClaudiu Manoil alloc_size += sizeof_priv; 2921d4fd0404SClaudiu Manoil } 2922d4fd0404SClaudiu Manoil /* force 32B alignment for enetc_si */ 2923d4fd0404SClaudiu Manoil alloc_size += ENETC_SI_ALIGN - 1; 2924d4fd0404SClaudiu Manoil 2925d4fd0404SClaudiu Manoil p = kzalloc(alloc_size, GFP_KERNEL); 2926d4fd0404SClaudiu Manoil if (!p) { 2927d4fd0404SClaudiu Manoil err = -ENOMEM; 2928d4fd0404SClaudiu Manoil goto err_alloc_si; 2929d4fd0404SClaudiu Manoil } 2930d4fd0404SClaudiu Manoil 2931d4fd0404SClaudiu Manoil si = PTR_ALIGN(p, ENETC_SI_ALIGN); 2932d4fd0404SClaudiu Manoil si->pad = (char *)si - (char *)p; 2933d4fd0404SClaudiu Manoil 2934d4fd0404SClaudiu Manoil pci_set_drvdata(pdev, si); 2935d4fd0404SClaudiu Manoil si->pdev = pdev; 2936d4fd0404SClaudiu Manoil hw = &si->hw; 2937d4fd0404SClaudiu Manoil 2938d4fd0404SClaudiu Manoil len = pci_resource_len(pdev, ENETC_BAR_REGS); 2939d4fd0404SClaudiu Manoil hw->reg = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len); 2940d4fd0404SClaudiu Manoil if (!hw->reg) { 2941d4fd0404SClaudiu Manoil err = -ENXIO; 2942d4fd0404SClaudiu Manoil dev_err(&pdev->dev, "ioremap() failed\n"); 2943d4fd0404SClaudiu Manoil goto err_ioremap; 2944d4fd0404SClaudiu Manoil } 2945d4fd0404SClaudiu Manoil if (len > ENETC_PORT_BASE) 2946d4fd0404SClaudiu Manoil hw->port = hw->reg + ENETC_PORT_BASE; 2947d4fd0404SClaudiu Manoil if (len > ENETC_GLOBAL_BASE) 2948d4fd0404SClaudiu Manoil hw->global = hw->reg + ENETC_GLOBAL_BASE; 2949d4fd0404SClaudiu Manoil 2950d4fd0404SClaudiu Manoil enetc_detect_errata(si); 2951d4fd0404SClaudiu Manoil 2952d4fd0404SClaudiu Manoil return 0; 2953d4fd0404SClaudiu Manoil 2954d4fd0404SClaudiu Manoil err_ioremap: 2955d4fd0404SClaudiu Manoil enetc_kfree_si(si); 2956d4fd0404SClaudiu Manoil err_alloc_si: 2957d4fd0404SClaudiu Manoil pci_release_mem_regions(pdev); 2958d4fd0404SClaudiu Manoil err_pci_mem_reg: 2959d4fd0404SClaudiu Manoil err_dma: 2960d4fd0404SClaudiu Manoil pci_disable_device(pdev); 2961d4fd0404SClaudiu Manoil 2962d4fd0404SClaudiu Manoil return err; 2963d4fd0404SClaudiu Manoil } 2964d4fd0404SClaudiu Manoil 2965d4fd0404SClaudiu Manoil void enetc_pci_remove(struct pci_dev *pdev) 2966d4fd0404SClaudiu Manoil { 2967d4fd0404SClaudiu Manoil struct enetc_si *si = pci_get_drvdata(pdev); 2968d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 2969d4fd0404SClaudiu Manoil 2970d4fd0404SClaudiu Manoil iounmap(hw->reg); 2971d4fd0404SClaudiu Manoil enetc_kfree_si(si); 2972d4fd0404SClaudiu Manoil pci_release_mem_regions(pdev); 2973d4fd0404SClaudiu Manoil pci_disable_device(pdev); 2974d4fd0404SClaudiu Manoil } 2975