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; 175285e8dedSPo Liu tx_swbd->qbv_en = !!(priv->active_offloads & ENETC_F_QBV); 176285e8dedSPo Liu tx_swbd->check_wb = tx_swbd->do_twostep_tstamp || tx_swbd->qbv_en; 1777294380cSYangbo Lu 1787294380cSYangbo Lu if (do_vlan || do_onestep_tstamp || do_twostep_tstamp) 179d4fd0404SClaudiu Manoil flags |= ENETC_TXBD_FLAGS_EX; 180d4fd0404SClaudiu Manoil 18182728b91SClaudiu Manoil if (tx_ring->tsd_enable) 1820d08c9ecSPo Liu flags |= ENETC_TXBD_FLAGS_TSE | ENETC_TXBD_FLAGS_TXSTART; 183d4fd0404SClaudiu Manoil 184d4fd0404SClaudiu Manoil /* first BD needs frm_len and offload flags set */ 185d4fd0404SClaudiu Manoil temp_bd.frm_len = cpu_to_le16(skb->len); 186d4fd0404SClaudiu Manoil temp_bd.flags = flags; 187d4fd0404SClaudiu Manoil 18882728b91SClaudiu Manoil if (flags & ENETC_TXBD_FLAGS_TSE) 18982728b91SClaudiu Manoil temp_bd.txstart = enetc_txbd_set_tx_start(skb->skb_mstamp_ns, 19082728b91SClaudiu Manoil flags); 1910d08c9ecSPo Liu 192d4fd0404SClaudiu Manoil if (flags & ENETC_TXBD_FLAGS_EX) { 193d4fd0404SClaudiu Manoil u8 e_flags = 0; 194d4fd0404SClaudiu Manoil *txbd = temp_bd; 195d4fd0404SClaudiu Manoil enetc_clear_tx_bd(&temp_bd); 196d4fd0404SClaudiu Manoil 197d4fd0404SClaudiu Manoil /* add extension BD for VLAN and/or timestamping */ 198d4fd0404SClaudiu Manoil flags = 0; 199d4fd0404SClaudiu Manoil tx_swbd++; 200d4fd0404SClaudiu Manoil txbd++; 201d4fd0404SClaudiu Manoil i++; 202d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 203d4fd0404SClaudiu Manoil i = 0; 204d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 205d4fd0404SClaudiu Manoil txbd = ENETC_TXBD(*tx_ring, 0); 206d4fd0404SClaudiu Manoil } 207d4fd0404SClaudiu Manoil prefetchw(txbd); 208d4fd0404SClaudiu Manoil 209d4fd0404SClaudiu Manoil if (do_vlan) { 210d4fd0404SClaudiu Manoil temp_bd.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb)); 211d4fd0404SClaudiu Manoil temp_bd.ext.tpid = 0; /* < C-TAG */ 212d4fd0404SClaudiu Manoil e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS; 213d4fd0404SClaudiu Manoil } 214d4fd0404SClaudiu Manoil 2157294380cSYangbo Lu if (do_onestep_tstamp) { 2167294380cSYangbo Lu u32 lo, hi, val; 2177294380cSYangbo Lu u64 sec, nsec; 2187294380cSYangbo Lu u8 *data; 2197294380cSYangbo Lu 2207294380cSYangbo Lu lo = enetc_rd_hot(hw, ENETC_SICTR0); 2217294380cSYangbo Lu hi = enetc_rd_hot(hw, ENETC_SICTR1); 2227294380cSYangbo Lu sec = (u64)hi << 32 | lo; 2237294380cSYangbo Lu nsec = do_div(sec, 1000000000); 2247294380cSYangbo Lu 2257294380cSYangbo Lu /* Configure extension BD */ 2267294380cSYangbo Lu temp_bd.ext.tstamp = cpu_to_le32(lo & 0x3fffffff); 2277294380cSYangbo Lu e_flags |= ENETC_TXBD_E_FLAGS_ONE_STEP_PTP; 2287294380cSYangbo Lu 2297294380cSYangbo Lu /* Update originTimestamp field of Sync packet 2307294380cSYangbo Lu * - 48 bits seconds field 2317294380cSYangbo Lu * - 32 bits nanseconds field 2327294380cSYangbo Lu */ 2337294380cSYangbo Lu data = skb_mac_header(skb); 2347294380cSYangbo Lu *(__be16 *)(data + offset2) = 2357294380cSYangbo Lu htons((sec >> 32) & 0xffff); 2367294380cSYangbo Lu *(__be32 *)(data + offset2 + 2) = 2377294380cSYangbo Lu htonl(sec & 0xffffffff); 2387294380cSYangbo Lu *(__be32 *)(data + offset2 + 6) = htonl(nsec); 2397294380cSYangbo Lu 2407294380cSYangbo Lu /* Configure single-step register */ 2417294380cSYangbo Lu val = ENETC_PM0_SINGLE_STEP_EN; 2427294380cSYangbo Lu val |= ENETC_SET_SINGLE_STEP_OFFSET(offset1); 2437294380cSYangbo Lu if (udp) 2447294380cSYangbo Lu val |= ENETC_PM0_SINGLE_STEP_CH; 2457294380cSYangbo Lu 2467294380cSYangbo Lu enetc_port_wr(hw, ENETC_PM0_SINGLE_STEP, val); 2477294380cSYangbo Lu enetc_port_wr(hw, ENETC_PM1_SINGLE_STEP, val); 2487294380cSYangbo Lu } else if (do_twostep_tstamp) { 249d4fd0404SClaudiu Manoil skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 250d4fd0404SClaudiu Manoil e_flags |= ENETC_TXBD_E_FLAGS_TWO_STEP_PTP; 251d4fd0404SClaudiu Manoil } 252d4fd0404SClaudiu Manoil 253d4fd0404SClaudiu Manoil temp_bd.ext.e_flags = e_flags; 254d4fd0404SClaudiu Manoil count++; 255d4fd0404SClaudiu Manoil } 256d4fd0404SClaudiu Manoil 257d4fd0404SClaudiu Manoil frag = &skb_shinfo(skb)->frags[0]; 258d4fd0404SClaudiu Manoil for (f = 0; f < skb_shinfo(skb)->nr_frags; f++, frag++) { 259d4fd0404SClaudiu Manoil len = skb_frag_size(frag); 260d4fd0404SClaudiu Manoil dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len, 261d4fd0404SClaudiu Manoil DMA_TO_DEVICE); 262d4fd0404SClaudiu Manoil if (dma_mapping_error(tx_ring->dev, dma)) 263d4fd0404SClaudiu Manoil goto dma_err; 264d4fd0404SClaudiu Manoil 265d4fd0404SClaudiu Manoil *txbd = temp_bd; 266d4fd0404SClaudiu Manoil enetc_clear_tx_bd(&temp_bd); 267d4fd0404SClaudiu Manoil 268d4fd0404SClaudiu Manoil flags = 0; 269d4fd0404SClaudiu Manoil tx_swbd++; 270d4fd0404SClaudiu Manoil txbd++; 271d4fd0404SClaudiu Manoil i++; 272d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 273d4fd0404SClaudiu Manoil i = 0; 274d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 275d4fd0404SClaudiu Manoil txbd = ENETC_TXBD(*tx_ring, 0); 276d4fd0404SClaudiu Manoil } 277d4fd0404SClaudiu Manoil prefetchw(txbd); 278d4fd0404SClaudiu Manoil 279d4fd0404SClaudiu Manoil temp_bd.addr = cpu_to_le64(dma); 280d4fd0404SClaudiu Manoil temp_bd.buf_len = cpu_to_le16(len); 281d4fd0404SClaudiu Manoil 282d4fd0404SClaudiu Manoil tx_swbd->dma = dma; 283d4fd0404SClaudiu Manoil tx_swbd->len = len; 284d4fd0404SClaudiu Manoil tx_swbd->is_dma_page = 1; 2857ed2bc80SVladimir Oltean tx_swbd->dir = DMA_TO_DEVICE; 286d4fd0404SClaudiu Manoil count++; 287d4fd0404SClaudiu Manoil } 288d4fd0404SClaudiu Manoil 289d4fd0404SClaudiu Manoil /* last BD needs 'F' bit set */ 290d4fd0404SClaudiu Manoil flags |= ENETC_TXBD_FLAGS_F; 291d4fd0404SClaudiu Manoil temp_bd.flags = flags; 292d4fd0404SClaudiu Manoil *txbd = temp_bd; 293d4fd0404SClaudiu Manoil 294d504498dSVladimir Oltean tx_ring->tx_swbd[i].is_eof = true; 295d4fd0404SClaudiu Manoil tx_ring->tx_swbd[i].skb = skb; 296d4fd0404SClaudiu Manoil 297d4fd0404SClaudiu Manoil enetc_bdr_idx_inc(tx_ring, &i); 298d4fd0404SClaudiu Manoil tx_ring->next_to_use = i; 299d4fd0404SClaudiu Manoil 3004caefbceSMichael Walle skb_tx_timestamp(skb); 3014caefbceSMichael Walle 3027ed2bc80SVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 303d4fd0404SClaudiu Manoil 304d4fd0404SClaudiu Manoil return count; 305d4fd0404SClaudiu Manoil 306d4fd0404SClaudiu Manoil dma_err: 307d4fd0404SClaudiu Manoil dev_err(tx_ring->dev, "DMA map error"); 308d4fd0404SClaudiu Manoil 309d4fd0404SClaudiu Manoil do { 310d4fd0404SClaudiu Manoil tx_swbd = &tx_ring->tx_swbd[i]; 3119d2b68ccSVladimir Oltean enetc_free_tx_frame(tx_ring, tx_swbd); 312d4fd0404SClaudiu Manoil if (i == 0) 313d4fd0404SClaudiu Manoil i = tx_ring->bd_count; 314d4fd0404SClaudiu Manoil i--; 315d4fd0404SClaudiu Manoil } while (count--); 316d4fd0404SClaudiu Manoil 317d4fd0404SClaudiu Manoil return 0; 318d4fd0404SClaudiu Manoil } 319d4fd0404SClaudiu Manoil 320fb8629e2SIoana Ciornei static void enetc_map_tx_tso_hdr(struct enetc_bdr *tx_ring, struct sk_buff *skb, 321fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd, 322fb8629e2SIoana Ciornei union enetc_tx_bd *txbd, int *i, int hdr_len, 323fb8629e2SIoana Ciornei int data_len) 324fb8629e2SIoana Ciornei { 325fb8629e2SIoana Ciornei union enetc_tx_bd txbd_tmp; 326fb8629e2SIoana Ciornei u8 flags = 0, e_flags = 0; 327fb8629e2SIoana Ciornei dma_addr_t addr; 328fb8629e2SIoana Ciornei 329fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 330fb8629e2SIoana Ciornei addr = tx_ring->tso_headers_dma + *i * TSO_HEADER_SIZE; 331fb8629e2SIoana Ciornei 332fb8629e2SIoana Ciornei if (skb_vlan_tag_present(skb)) 333fb8629e2SIoana Ciornei flags |= ENETC_TXBD_FLAGS_EX; 334fb8629e2SIoana Ciornei 335fb8629e2SIoana Ciornei txbd_tmp.addr = cpu_to_le64(addr); 336fb8629e2SIoana Ciornei txbd_tmp.buf_len = cpu_to_le16(hdr_len); 337fb8629e2SIoana Ciornei 338fb8629e2SIoana Ciornei /* first BD needs frm_len and offload flags set */ 339fb8629e2SIoana Ciornei txbd_tmp.frm_len = cpu_to_le16(hdr_len + data_len); 340fb8629e2SIoana Ciornei txbd_tmp.flags = flags; 341fb8629e2SIoana Ciornei 342fb8629e2SIoana Ciornei /* For the TSO header we do not set the dma address since we do not 343fb8629e2SIoana Ciornei * want it unmapped when we do cleanup. We still set len so that we 344fb8629e2SIoana Ciornei * count the bytes sent. 345fb8629e2SIoana Ciornei */ 346fb8629e2SIoana Ciornei tx_swbd->len = hdr_len; 347fb8629e2SIoana Ciornei tx_swbd->do_twostep_tstamp = false; 348fb8629e2SIoana Ciornei tx_swbd->check_wb = false; 349fb8629e2SIoana Ciornei 350fb8629e2SIoana Ciornei /* Actually write the header in the BD */ 351fb8629e2SIoana Ciornei *txbd = txbd_tmp; 352fb8629e2SIoana Ciornei 353fb8629e2SIoana Ciornei /* Add extension BD for VLAN */ 354fb8629e2SIoana Ciornei if (flags & ENETC_TXBD_FLAGS_EX) { 355fb8629e2SIoana Ciornei /* Get the next BD */ 356fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, i); 357fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, *i); 358fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[*i]; 359fb8629e2SIoana Ciornei prefetchw(txbd); 360fb8629e2SIoana Ciornei 361fb8629e2SIoana Ciornei /* Setup the VLAN fields */ 362fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 363fb8629e2SIoana Ciornei txbd_tmp.ext.vid = cpu_to_le16(skb_vlan_tag_get(skb)); 364fb8629e2SIoana Ciornei txbd_tmp.ext.tpid = 0; /* < C-TAG */ 365fb8629e2SIoana Ciornei e_flags |= ENETC_TXBD_E_FLAGS_VLAN_INS; 366fb8629e2SIoana Ciornei 367fb8629e2SIoana Ciornei /* Write the BD */ 368fb8629e2SIoana Ciornei txbd_tmp.ext.e_flags = e_flags; 369fb8629e2SIoana Ciornei *txbd = txbd_tmp; 370fb8629e2SIoana Ciornei } 371fb8629e2SIoana Ciornei } 372fb8629e2SIoana Ciornei 373fb8629e2SIoana Ciornei static int enetc_map_tx_tso_data(struct enetc_bdr *tx_ring, struct sk_buff *skb, 374fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd, 375fb8629e2SIoana Ciornei union enetc_tx_bd *txbd, char *data, 376fb8629e2SIoana Ciornei int size, bool last_bd) 377fb8629e2SIoana Ciornei { 378fb8629e2SIoana Ciornei union enetc_tx_bd txbd_tmp; 379fb8629e2SIoana Ciornei dma_addr_t addr; 380fb8629e2SIoana Ciornei u8 flags = 0; 381fb8629e2SIoana Ciornei 382fb8629e2SIoana Ciornei enetc_clear_tx_bd(&txbd_tmp); 383fb8629e2SIoana Ciornei 384fb8629e2SIoana Ciornei addr = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE); 385fb8629e2SIoana Ciornei if (unlikely(dma_mapping_error(tx_ring->dev, addr))) { 386fb8629e2SIoana Ciornei netdev_err(tx_ring->ndev, "DMA map error\n"); 387fb8629e2SIoana Ciornei return -ENOMEM; 388fb8629e2SIoana Ciornei } 389fb8629e2SIoana Ciornei 390fb8629e2SIoana Ciornei if (last_bd) { 391fb8629e2SIoana Ciornei flags |= ENETC_TXBD_FLAGS_F; 392fb8629e2SIoana Ciornei tx_swbd->is_eof = 1; 393fb8629e2SIoana Ciornei } 394fb8629e2SIoana Ciornei 395fb8629e2SIoana Ciornei txbd_tmp.addr = cpu_to_le64(addr); 396fb8629e2SIoana Ciornei txbd_tmp.buf_len = cpu_to_le16(size); 397fb8629e2SIoana Ciornei txbd_tmp.flags = flags; 398fb8629e2SIoana Ciornei 399fb8629e2SIoana Ciornei tx_swbd->dma = addr; 400fb8629e2SIoana Ciornei tx_swbd->len = size; 401fb8629e2SIoana Ciornei tx_swbd->dir = DMA_TO_DEVICE; 402fb8629e2SIoana Ciornei 403fb8629e2SIoana Ciornei *txbd = txbd_tmp; 404fb8629e2SIoana Ciornei 405fb8629e2SIoana Ciornei return 0; 406fb8629e2SIoana Ciornei } 407fb8629e2SIoana Ciornei 408fb8629e2SIoana Ciornei static __wsum enetc_tso_hdr_csum(struct tso_t *tso, struct sk_buff *skb, 409fb8629e2SIoana Ciornei char *hdr, int hdr_len, int *l4_hdr_len) 410fb8629e2SIoana Ciornei { 411fb8629e2SIoana Ciornei char *l4_hdr = hdr + skb_transport_offset(skb); 412fb8629e2SIoana Ciornei int mac_hdr_len = skb_network_offset(skb); 413fb8629e2SIoana Ciornei 414fb8629e2SIoana Ciornei if (tso->tlen != sizeof(struct udphdr)) { 415fb8629e2SIoana Ciornei struct tcphdr *tcph = (struct tcphdr *)(l4_hdr); 416fb8629e2SIoana Ciornei 417fb8629e2SIoana Ciornei tcph->check = 0; 418fb8629e2SIoana Ciornei } else { 419fb8629e2SIoana Ciornei struct udphdr *udph = (struct udphdr *)(l4_hdr); 420fb8629e2SIoana Ciornei 421fb8629e2SIoana Ciornei udph->check = 0; 422fb8629e2SIoana Ciornei } 423fb8629e2SIoana Ciornei 424fb8629e2SIoana Ciornei /* Compute the IP checksum. This is necessary since tso_build_hdr() 425fb8629e2SIoana Ciornei * already incremented the IP ID field. 426fb8629e2SIoana Ciornei */ 427fb8629e2SIoana Ciornei if (!tso->ipv6) { 428fb8629e2SIoana Ciornei struct iphdr *iph = (void *)(hdr + mac_hdr_len); 429fb8629e2SIoana Ciornei 430fb8629e2SIoana Ciornei iph->check = 0; 431fb8629e2SIoana Ciornei iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 432fb8629e2SIoana Ciornei } 433fb8629e2SIoana Ciornei 434fb8629e2SIoana Ciornei /* Compute the checksum over the L4 header. */ 435fb8629e2SIoana Ciornei *l4_hdr_len = hdr_len - skb_transport_offset(skb); 436fb8629e2SIoana Ciornei return csum_partial(l4_hdr, *l4_hdr_len, 0); 437fb8629e2SIoana Ciornei } 438fb8629e2SIoana Ciornei 439fb8629e2SIoana Ciornei static void enetc_tso_complete_csum(struct enetc_bdr *tx_ring, struct tso_t *tso, 440fb8629e2SIoana Ciornei struct sk_buff *skb, char *hdr, int len, 441fb8629e2SIoana Ciornei __wsum sum) 442fb8629e2SIoana Ciornei { 443fb8629e2SIoana Ciornei char *l4_hdr = hdr + skb_transport_offset(skb); 444fb8629e2SIoana Ciornei __sum16 csum_final; 445fb8629e2SIoana Ciornei 446fb8629e2SIoana Ciornei /* Complete the L4 checksum by appending the pseudo-header to the 447fb8629e2SIoana Ciornei * already computed checksum. 448fb8629e2SIoana Ciornei */ 449fb8629e2SIoana Ciornei if (!tso->ipv6) 450fb8629e2SIoana Ciornei csum_final = csum_tcpudp_magic(ip_hdr(skb)->saddr, 451fb8629e2SIoana Ciornei ip_hdr(skb)->daddr, 452fb8629e2SIoana Ciornei len, ip_hdr(skb)->protocol, sum); 453fb8629e2SIoana Ciornei else 454fb8629e2SIoana Ciornei csum_final = csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 455fb8629e2SIoana Ciornei &ipv6_hdr(skb)->daddr, 456fb8629e2SIoana Ciornei len, ipv6_hdr(skb)->nexthdr, sum); 457fb8629e2SIoana Ciornei 458fb8629e2SIoana Ciornei if (tso->tlen != sizeof(struct udphdr)) { 459fb8629e2SIoana Ciornei struct tcphdr *tcph = (struct tcphdr *)(l4_hdr); 460fb8629e2SIoana Ciornei 461fb8629e2SIoana Ciornei tcph->check = csum_final; 462fb8629e2SIoana Ciornei } else { 463fb8629e2SIoana Ciornei struct udphdr *udph = (struct udphdr *)(l4_hdr); 464fb8629e2SIoana Ciornei 465fb8629e2SIoana Ciornei udph->check = csum_final; 466fb8629e2SIoana Ciornei } 467fb8629e2SIoana Ciornei } 468fb8629e2SIoana Ciornei 469fb8629e2SIoana Ciornei static int enetc_map_tx_tso_buffs(struct enetc_bdr *tx_ring, struct sk_buff *skb) 470fb8629e2SIoana Ciornei { 471fb8629e2SIoana Ciornei int hdr_len, total_len, data_len; 472fb8629e2SIoana Ciornei struct enetc_tx_swbd *tx_swbd; 473fb8629e2SIoana Ciornei union enetc_tx_bd *txbd; 474fb8629e2SIoana Ciornei struct tso_t tso; 475fb8629e2SIoana Ciornei __wsum csum, csum2; 476fb8629e2SIoana Ciornei int count = 0, pos; 477fb8629e2SIoana Ciornei int err, i, bd_data_num; 478fb8629e2SIoana Ciornei 479fb8629e2SIoana Ciornei /* Initialize the TSO handler, and prepare the first payload */ 480fb8629e2SIoana Ciornei hdr_len = tso_start(skb, &tso); 481fb8629e2SIoana Ciornei total_len = skb->len - hdr_len; 482fb8629e2SIoana Ciornei i = tx_ring->next_to_use; 483fb8629e2SIoana Ciornei 484fb8629e2SIoana Ciornei while (total_len > 0) { 485fb8629e2SIoana Ciornei char *hdr; 486fb8629e2SIoana Ciornei 487fb8629e2SIoana Ciornei /* Get the BD */ 488fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, i); 489fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 490fb8629e2SIoana Ciornei prefetchw(txbd); 491fb8629e2SIoana Ciornei 492fb8629e2SIoana Ciornei /* Determine the length of this packet */ 493fb8629e2SIoana Ciornei data_len = min_t(int, skb_shinfo(skb)->gso_size, total_len); 494fb8629e2SIoana Ciornei total_len -= data_len; 495fb8629e2SIoana Ciornei 496fb8629e2SIoana Ciornei /* prepare packet headers: MAC + IP + TCP */ 497fb8629e2SIoana Ciornei hdr = tx_ring->tso_headers + i * TSO_HEADER_SIZE; 498fb8629e2SIoana Ciornei tso_build_hdr(skb, hdr, &tso, data_len, total_len == 0); 499fb8629e2SIoana Ciornei 500fb8629e2SIoana Ciornei /* compute the csum over the L4 header */ 501fb8629e2SIoana Ciornei csum = enetc_tso_hdr_csum(&tso, skb, hdr, hdr_len, &pos); 502fb8629e2SIoana Ciornei enetc_map_tx_tso_hdr(tx_ring, skb, tx_swbd, txbd, &i, hdr_len, data_len); 503fb8629e2SIoana Ciornei bd_data_num = 0; 504fb8629e2SIoana Ciornei count++; 505fb8629e2SIoana Ciornei 506fb8629e2SIoana Ciornei while (data_len > 0) { 507fb8629e2SIoana Ciornei int size; 508fb8629e2SIoana Ciornei 509fb8629e2SIoana Ciornei size = min_t(int, tso.size, data_len); 510fb8629e2SIoana Ciornei 511fb8629e2SIoana Ciornei /* Advance the index in the BDR */ 512fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, &i); 513fb8629e2SIoana Ciornei txbd = ENETC_TXBD(*tx_ring, i); 514fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 515fb8629e2SIoana Ciornei prefetchw(txbd); 516fb8629e2SIoana Ciornei 517fb8629e2SIoana Ciornei /* Compute the checksum over this segment of data and 518fb8629e2SIoana Ciornei * add it to the csum already computed (over the L4 519fb8629e2SIoana Ciornei * header and possible other data segments). 520fb8629e2SIoana Ciornei */ 521fb8629e2SIoana Ciornei csum2 = csum_partial(tso.data, size, 0); 522fb8629e2SIoana Ciornei csum = csum_block_add(csum, csum2, pos); 523fb8629e2SIoana Ciornei pos += size; 524fb8629e2SIoana Ciornei 525fb8629e2SIoana Ciornei err = enetc_map_tx_tso_data(tx_ring, skb, tx_swbd, txbd, 526fb8629e2SIoana Ciornei tso.data, size, 527fb8629e2SIoana Ciornei size == data_len); 528fb8629e2SIoana Ciornei if (err) 529fb8629e2SIoana Ciornei goto err_map_data; 530fb8629e2SIoana Ciornei 531fb8629e2SIoana Ciornei data_len -= size; 532fb8629e2SIoana Ciornei count++; 533fb8629e2SIoana Ciornei bd_data_num++; 534fb8629e2SIoana Ciornei tso_build_data(skb, &tso, size); 535fb8629e2SIoana Ciornei 536fb8629e2SIoana Ciornei if (unlikely(bd_data_num >= ENETC_MAX_SKB_FRAGS && data_len)) 537fb8629e2SIoana Ciornei goto err_chained_bd; 538fb8629e2SIoana Ciornei } 539fb8629e2SIoana Ciornei 540fb8629e2SIoana Ciornei enetc_tso_complete_csum(tx_ring, &tso, skb, hdr, pos, csum); 541fb8629e2SIoana Ciornei 542fb8629e2SIoana Ciornei if (total_len == 0) 543fb8629e2SIoana Ciornei tx_swbd->skb = skb; 544fb8629e2SIoana Ciornei 545fb8629e2SIoana Ciornei /* Go to the next BD */ 546fb8629e2SIoana Ciornei enetc_bdr_idx_inc(tx_ring, &i); 547fb8629e2SIoana Ciornei } 548fb8629e2SIoana Ciornei 549fb8629e2SIoana Ciornei tx_ring->next_to_use = i; 550fb8629e2SIoana Ciornei enetc_update_tx_ring_tail(tx_ring); 551fb8629e2SIoana Ciornei 552fb8629e2SIoana Ciornei return count; 553fb8629e2SIoana Ciornei 554fb8629e2SIoana Ciornei err_map_data: 555fb8629e2SIoana Ciornei dev_err(tx_ring->dev, "DMA map error"); 556fb8629e2SIoana Ciornei 557fb8629e2SIoana Ciornei err_chained_bd: 558fb8629e2SIoana Ciornei do { 559fb8629e2SIoana Ciornei tx_swbd = &tx_ring->tx_swbd[i]; 560fb8629e2SIoana Ciornei enetc_free_tx_frame(tx_ring, tx_swbd); 561fb8629e2SIoana Ciornei if (i == 0) 562fb8629e2SIoana Ciornei i = tx_ring->bd_count; 563fb8629e2SIoana Ciornei i--; 564fb8629e2SIoana Ciornei } while (count--); 565fb8629e2SIoana Ciornei 566fb8629e2SIoana Ciornei return 0; 567fb8629e2SIoana Ciornei } 568fb8629e2SIoana Ciornei 5697294380cSYangbo Lu static netdev_tx_t enetc_start_xmit(struct sk_buff *skb, 5707294380cSYangbo Lu struct net_device *ndev) 5710486185eSVladimir Oltean { 5720486185eSVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(ndev); 5730486185eSVladimir Oltean struct enetc_bdr *tx_ring; 574acede3c5SIoana Ciornei int count, err; 5750486185eSVladimir Oltean 5767ce9c3d3SYangbo Lu /* Queue one-step Sync packet if already locked */ 5777ce9c3d3SYangbo Lu if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) { 5787ce9c3d3SYangbo Lu if (test_and_set_bit_lock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS, 5797ce9c3d3SYangbo Lu &priv->flags)) { 5807ce9c3d3SYangbo Lu skb_queue_tail(&priv->tx_skbs, skb); 5817ce9c3d3SYangbo Lu return NETDEV_TX_OK; 5827ce9c3d3SYangbo Lu } 5837ce9c3d3SYangbo Lu } 5847ce9c3d3SYangbo Lu 5850486185eSVladimir Oltean tx_ring = priv->tx_ring[skb->queue_mapping]; 5860486185eSVladimir Oltean 587fb8629e2SIoana Ciornei if (skb_is_gso(skb)) { 588fb8629e2SIoana Ciornei if (enetc_bd_unused(tx_ring) < tso_count_descs(skb)) { 589fb8629e2SIoana Ciornei netif_stop_subqueue(ndev, tx_ring->index); 590fb8629e2SIoana Ciornei return NETDEV_TX_BUSY; 591fb8629e2SIoana Ciornei } 592fb8629e2SIoana Ciornei 593fb8629e2SIoana Ciornei enetc_lock_mdio(); 594fb8629e2SIoana Ciornei count = enetc_map_tx_tso_buffs(tx_ring, skb); 595fb8629e2SIoana Ciornei enetc_unlock_mdio(); 596fb8629e2SIoana Ciornei } else { 5970486185eSVladimir Oltean if (unlikely(skb_shinfo(skb)->nr_frags > ENETC_MAX_SKB_FRAGS)) 5980486185eSVladimir Oltean if (unlikely(skb_linearize(skb))) 5990486185eSVladimir Oltean goto drop_packet_err; 6000486185eSVladimir Oltean 6010486185eSVladimir Oltean count = skb_shinfo(skb)->nr_frags + 1; /* fragments + head */ 6020486185eSVladimir Oltean if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(count)) { 6030486185eSVladimir Oltean netif_stop_subqueue(ndev, tx_ring->index); 6040486185eSVladimir Oltean return NETDEV_TX_BUSY; 6050486185eSVladimir Oltean } 6060486185eSVladimir Oltean 607acede3c5SIoana Ciornei if (skb->ip_summed == CHECKSUM_PARTIAL) { 608acede3c5SIoana Ciornei err = skb_checksum_help(skb); 609acede3c5SIoana Ciornei if (err) 610acede3c5SIoana Ciornei goto drop_packet_err; 611acede3c5SIoana Ciornei } 6120486185eSVladimir Oltean enetc_lock_mdio(); 613f768e751SYangbo Lu count = enetc_map_tx_buffs(tx_ring, skb); 6140486185eSVladimir Oltean enetc_unlock_mdio(); 615fb8629e2SIoana Ciornei } 6160486185eSVladimir Oltean 6170486185eSVladimir Oltean if (unlikely(!count)) 6180486185eSVladimir Oltean goto drop_packet_err; 6190486185eSVladimir Oltean 6200486185eSVladimir Oltean if (enetc_bd_unused(tx_ring) < ENETC_TXBDS_MAX_NEEDED) 6210486185eSVladimir Oltean netif_stop_subqueue(ndev, tx_ring->index); 6220486185eSVladimir Oltean 6230486185eSVladimir Oltean return NETDEV_TX_OK; 6240486185eSVladimir Oltean 6250486185eSVladimir Oltean drop_packet_err: 6260486185eSVladimir Oltean dev_kfree_skb_any(skb); 6270486185eSVladimir Oltean return NETDEV_TX_OK; 6280486185eSVladimir Oltean } 6290486185eSVladimir Oltean 6307294380cSYangbo Lu netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev) 6317294380cSYangbo Lu { 6327294380cSYangbo Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 6337294380cSYangbo Lu u8 udp, msgtype, twostep; 6347294380cSYangbo Lu u16 offset1, offset2; 6357294380cSYangbo Lu 6367294380cSYangbo Lu /* Mark tx timestamp type on skb->cb[0] if requires */ 6377294380cSYangbo Lu if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 6387294380cSYangbo Lu (priv->active_offloads & ENETC_F_TX_TSTAMP_MASK)) { 6397294380cSYangbo Lu skb->cb[0] = priv->active_offloads & ENETC_F_TX_TSTAMP_MASK; 6407294380cSYangbo Lu } else { 6417294380cSYangbo Lu skb->cb[0] = 0; 6427294380cSYangbo Lu } 6437294380cSYangbo Lu 6447294380cSYangbo Lu /* Fall back to two-step timestamp if not one-step Sync packet */ 6457294380cSYangbo Lu if (skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) { 6467294380cSYangbo Lu if (enetc_ptp_parse(skb, &udp, &msgtype, &twostep, 6477294380cSYangbo Lu &offset1, &offset2) || 6487294380cSYangbo Lu msgtype != PTP_MSGTYPE_SYNC || twostep != 0) 6497294380cSYangbo Lu skb->cb[0] = ENETC_F_TX_TSTAMP; 6507294380cSYangbo Lu } 6517294380cSYangbo Lu 6527294380cSYangbo Lu return enetc_start_xmit(skb, ndev); 6537294380cSYangbo Lu } 6547294380cSYangbo Lu 655d4fd0404SClaudiu Manoil static irqreturn_t enetc_msix(int irq, void *data) 656d4fd0404SClaudiu Manoil { 657d4fd0404SClaudiu Manoil struct enetc_int_vector *v = data; 658d4fd0404SClaudiu Manoil int i; 659d4fd0404SClaudiu Manoil 660fd5736bfSAlex Marginean enetc_lock_mdio(); 661fd5736bfSAlex Marginean 662d4fd0404SClaudiu Manoil /* disable interrupts */ 663fd5736bfSAlex Marginean enetc_wr_reg_hot(v->rbier, 0); 664fd5736bfSAlex Marginean enetc_wr_reg_hot(v->ricr1, v->rx_ictt); 665d4fd0404SClaudiu Manoil 6660574e200SClaudiu Manoil for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS) 667fd5736bfSAlex Marginean enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 0); 668fd5736bfSAlex Marginean 669fd5736bfSAlex Marginean enetc_unlock_mdio(); 670d4fd0404SClaudiu Manoil 671215602a8SJiafei Pan napi_schedule(&v->napi); 672d4fd0404SClaudiu Manoil 673d4fd0404SClaudiu Manoil return IRQ_HANDLED; 674d4fd0404SClaudiu Manoil } 675d4fd0404SClaudiu Manoil 676ae0e6a5dSClaudiu Manoil static void enetc_rx_dim_work(struct work_struct *w) 677ae0e6a5dSClaudiu Manoil { 678ae0e6a5dSClaudiu Manoil struct dim *dim = container_of(w, struct dim, work); 679ae0e6a5dSClaudiu Manoil struct dim_cq_moder moder = 680ae0e6a5dSClaudiu Manoil net_dim_get_rx_moderation(dim->mode, dim->profile_ix); 681ae0e6a5dSClaudiu Manoil struct enetc_int_vector *v = 682ae0e6a5dSClaudiu Manoil container_of(dim, struct enetc_int_vector, rx_dim); 683ae0e6a5dSClaudiu Manoil 684ae0e6a5dSClaudiu Manoil v->rx_ictt = enetc_usecs_to_cycles(moder.usec); 685ae0e6a5dSClaudiu Manoil dim->state = DIM_START_MEASURE; 686ae0e6a5dSClaudiu Manoil } 687ae0e6a5dSClaudiu Manoil 688ae0e6a5dSClaudiu Manoil static void enetc_rx_net_dim(struct enetc_int_vector *v) 689ae0e6a5dSClaudiu Manoil { 6909f7afa05SClaudiu Manoil struct dim_sample dim_sample = {}; 691ae0e6a5dSClaudiu Manoil 692ae0e6a5dSClaudiu Manoil v->comp_cnt++; 693ae0e6a5dSClaudiu Manoil 694ae0e6a5dSClaudiu Manoil if (!v->rx_napi_work) 695ae0e6a5dSClaudiu Manoil return; 696ae0e6a5dSClaudiu Manoil 697ae0e6a5dSClaudiu Manoil dim_update_sample(v->comp_cnt, 698ae0e6a5dSClaudiu Manoil v->rx_ring.stats.packets, 699ae0e6a5dSClaudiu Manoil v->rx_ring.stats.bytes, 700ae0e6a5dSClaudiu Manoil &dim_sample); 701ae0e6a5dSClaudiu Manoil net_dim(&v->rx_dim, dim_sample); 702ae0e6a5dSClaudiu Manoil } 703ae0e6a5dSClaudiu Manoil 704d4fd0404SClaudiu Manoil static int enetc_bd_ready_count(struct enetc_bdr *tx_ring, int ci) 705d4fd0404SClaudiu Manoil { 706fd5736bfSAlex Marginean int pi = enetc_rd_reg_hot(tx_ring->tcir) & ENETC_TBCIR_IDX_MASK; 707d4fd0404SClaudiu Manoil 708d4fd0404SClaudiu Manoil return pi >= ci ? pi - ci : tx_ring->bd_count - ci + pi; 709d4fd0404SClaudiu Manoil } 710d4fd0404SClaudiu Manoil 71165d0cbb4SVladimir Oltean static bool enetc_page_reusable(struct page *page) 71265d0cbb4SVladimir Oltean { 71365d0cbb4SVladimir Oltean return (!page_is_pfmemalloc(page) && page_ref_count(page) == 1); 71465d0cbb4SVladimir Oltean } 71565d0cbb4SVladimir Oltean 71665d0cbb4SVladimir Oltean static void enetc_reuse_page(struct enetc_bdr *rx_ring, 71765d0cbb4SVladimir Oltean struct enetc_rx_swbd *old) 71865d0cbb4SVladimir Oltean { 71965d0cbb4SVladimir Oltean struct enetc_rx_swbd *new; 72065d0cbb4SVladimir Oltean 72165d0cbb4SVladimir Oltean new = &rx_ring->rx_swbd[rx_ring->next_to_alloc]; 72265d0cbb4SVladimir Oltean 72365d0cbb4SVladimir Oltean /* next buf that may reuse a page */ 72465d0cbb4SVladimir Oltean enetc_bdr_idx_inc(rx_ring, &rx_ring->next_to_alloc); 72565d0cbb4SVladimir Oltean 72665d0cbb4SVladimir Oltean /* copy page reference */ 72765d0cbb4SVladimir Oltean *new = *old; 72865d0cbb4SVladimir Oltean } 72965d0cbb4SVladimir Oltean 730d3982312SY.b. Lu static void enetc_get_tx_tstamp(struct enetc_hw *hw, union enetc_tx_bd *txbd, 731d3982312SY.b. Lu u64 *tstamp) 732d3982312SY.b. Lu { 733cec4f328SY.b. Lu u32 lo, hi, tstamp_lo; 734d3982312SY.b. Lu 7356d36ecdbSVladimir Oltean lo = enetc_rd_hot(hw, ENETC_SICTR0); 7366d36ecdbSVladimir Oltean hi = enetc_rd_hot(hw, ENETC_SICTR1); 737cec4f328SY.b. Lu tstamp_lo = le32_to_cpu(txbd->wb.tstamp); 738cec4f328SY.b. Lu if (lo <= tstamp_lo) 739d3982312SY.b. Lu hi -= 1; 740cec4f328SY.b. Lu *tstamp = (u64)hi << 32 | tstamp_lo; 741d3982312SY.b. Lu } 742d3982312SY.b. Lu 743d3982312SY.b. Lu static void enetc_tstamp_tx(struct sk_buff *skb, u64 tstamp) 744d3982312SY.b. Lu { 745d3982312SY.b. Lu struct skb_shared_hwtstamps shhwtstamps; 746d3982312SY.b. Lu 747d3982312SY.b. Lu if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) { 748d3982312SY.b. Lu memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 749d3982312SY.b. Lu shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 750847cbfc0SVladimir Oltean skb_txtime_consumed(skb); 751d3982312SY.b. Lu skb_tstamp_tx(skb, &shhwtstamps); 752d3982312SY.b. Lu } 753d3982312SY.b. Lu } 754d3982312SY.b. Lu 7557ed2bc80SVladimir Oltean static void enetc_recycle_xdp_tx_buff(struct enetc_bdr *tx_ring, 7567ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd) 7577ed2bc80SVladimir Oltean { 7587ed2bc80SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(tx_ring->ndev); 7597ed2bc80SVladimir Oltean struct enetc_rx_swbd rx_swbd = { 7607ed2bc80SVladimir Oltean .dma = tx_swbd->dma, 7617ed2bc80SVladimir Oltean .page = tx_swbd->page, 7627ed2bc80SVladimir Oltean .page_offset = tx_swbd->page_offset, 7637ed2bc80SVladimir Oltean .dir = tx_swbd->dir, 7647ed2bc80SVladimir Oltean .len = tx_swbd->len, 7657ed2bc80SVladimir Oltean }; 7667eab503bSVladimir Oltean struct enetc_bdr *rx_ring; 7677eab503bSVladimir Oltean 7687eab503bSVladimir Oltean rx_ring = enetc_rx_ring_from_xdp_tx_ring(priv, tx_ring); 7697ed2bc80SVladimir Oltean 7707ed2bc80SVladimir Oltean if (likely(enetc_swbd_unused(rx_ring))) { 7717ed2bc80SVladimir Oltean enetc_reuse_page(rx_ring, &rx_swbd); 7727ed2bc80SVladimir Oltean 7737ed2bc80SVladimir Oltean /* sync for use by the device */ 7747ed2bc80SVladimir Oltean dma_sync_single_range_for_device(rx_ring->dev, rx_swbd.dma, 7757ed2bc80SVladimir Oltean rx_swbd.page_offset, 7767ed2bc80SVladimir Oltean ENETC_RXB_DMA_SIZE_XDP, 7777ed2bc80SVladimir Oltean rx_swbd.dir); 7787ed2bc80SVladimir Oltean 7797ed2bc80SVladimir Oltean rx_ring->stats.recycles++; 7807ed2bc80SVladimir Oltean } else { 7817ed2bc80SVladimir Oltean /* RX ring is already full, we need to unmap and free the 7827ed2bc80SVladimir Oltean * page, since there's nothing useful we can do with it. 7837ed2bc80SVladimir Oltean */ 7847ed2bc80SVladimir Oltean rx_ring->stats.recycle_failures++; 7857ed2bc80SVladimir Oltean 7867ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd.dma, PAGE_SIZE, 7877ed2bc80SVladimir Oltean rx_swbd.dir); 7887ed2bc80SVladimir Oltean __free_page(rx_swbd.page); 7897ed2bc80SVladimir Oltean } 7907ed2bc80SVladimir Oltean 7917ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight--; 7927ed2bc80SVladimir Oltean } 7937ed2bc80SVladimir Oltean 794d4fd0404SClaudiu Manoil static bool enetc_clean_tx_ring(struct enetc_bdr *tx_ring, int napi_budget) 795d4fd0404SClaudiu Manoil { 796285e8dedSPo Liu int tx_frm_cnt = 0, tx_byte_cnt = 0, tx_win_drop = 0; 797d4fd0404SClaudiu Manoil struct net_device *ndev = tx_ring->ndev; 7987294380cSYangbo Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 799d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd; 800d4fd0404SClaudiu Manoil int i, bds_to_clean; 8017294380cSYangbo Lu bool do_twostep_tstamp; 802d3982312SY.b. Lu u64 tstamp = 0; 803d4fd0404SClaudiu Manoil 804d4fd0404SClaudiu Manoil i = tx_ring->next_to_clean; 805d4fd0404SClaudiu Manoil tx_swbd = &tx_ring->tx_swbd[i]; 806fd5736bfSAlex Marginean 807d4fd0404SClaudiu Manoil bds_to_clean = enetc_bd_ready_count(tx_ring, i); 808d4fd0404SClaudiu Manoil 8097294380cSYangbo Lu do_twostep_tstamp = false; 810d3982312SY.b. Lu 811d4fd0404SClaudiu Manoil while (bds_to_clean && tx_frm_cnt < ENETC_DEFAULT_TX_WORK) { 8129d2b68ccSVladimir Oltean struct xdp_frame *xdp_frame = enetc_tx_swbd_get_xdp_frame(tx_swbd); 8139d2b68ccSVladimir Oltean struct sk_buff *skb = enetc_tx_swbd_get_skb(tx_swbd); 814a93580a0SVladimir Oltean bool is_eof = tx_swbd->is_eof; 8159d2b68ccSVladimir Oltean 816d3982312SY.b. Lu if (unlikely(tx_swbd->check_wb)) { 817ae77bdbcSVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 818d3982312SY.b. Lu 819d3982312SY.b. Lu if (txbd->flags & ENETC_TXBD_FLAGS_W && 8207294380cSYangbo Lu tx_swbd->do_twostep_tstamp) { 821d3982312SY.b. Lu enetc_get_tx_tstamp(&priv->si->hw, txbd, 822d3982312SY.b. Lu &tstamp); 8237294380cSYangbo Lu do_twostep_tstamp = true; 824d3982312SY.b. Lu } 825285e8dedSPo Liu 826285e8dedSPo Liu if (tx_swbd->qbv_en && 827285e8dedSPo Liu txbd->wb.status & ENETC_TXBD_STATS_WIN) 828285e8dedSPo Liu tx_win_drop++; 829d3982312SY.b. Lu } 830d3982312SY.b. Lu 8317ed2bc80SVladimir Oltean if (tx_swbd->is_xdp_tx) 8327ed2bc80SVladimir Oltean enetc_recycle_xdp_tx_buff(tx_ring, tx_swbd); 8337ed2bc80SVladimir Oltean else if (likely(tx_swbd->dma)) 834d4fd0404SClaudiu Manoil enetc_unmap_tx_buff(tx_ring, tx_swbd); 835f4a0be84SClaudiu Manoil 8369d2b68ccSVladimir Oltean if (xdp_frame) { 8379d2b68ccSVladimir Oltean xdp_return_frame(xdp_frame); 8389d2b68ccSVladimir Oltean } else if (skb) { 83952066149SVladimir Oltean if (unlikely(skb->cb[0] & ENETC_F_TX_ONESTEP_SYNC_TSTAMP)) { 8407294380cSYangbo Lu /* Start work to release lock for next one-step 8417294380cSYangbo Lu * timestamping packet. And send one skb in 8427294380cSYangbo Lu * tx_skbs queue if has. 8437294380cSYangbo Lu */ 844b6faf160SYangbo Lu schedule_work(&priv->tx_onestep_tstamp); 8457294380cSYangbo Lu } else if (unlikely(do_twostep_tstamp)) { 8469d2b68ccSVladimir Oltean enetc_tstamp_tx(skb, tstamp); 8477294380cSYangbo Lu do_twostep_tstamp = false; 848d3982312SY.b. Lu } 8499d2b68ccSVladimir Oltean napi_consume_skb(skb, napi_budget); 850d4fd0404SClaudiu Manoil } 851d4fd0404SClaudiu Manoil 852d4fd0404SClaudiu Manoil tx_byte_cnt += tx_swbd->len; 8531ee8d6f3SVladimir Oltean /* Scrub the swbd here so we don't have to do that 8541ee8d6f3SVladimir Oltean * when we reuse it during xmit 8551ee8d6f3SVladimir Oltean */ 8561ee8d6f3SVladimir Oltean memset(tx_swbd, 0, sizeof(*tx_swbd)); 857d4fd0404SClaudiu Manoil 858d4fd0404SClaudiu Manoil bds_to_clean--; 859d4fd0404SClaudiu Manoil tx_swbd++; 860d4fd0404SClaudiu Manoil i++; 861d4fd0404SClaudiu Manoil if (unlikely(i == tx_ring->bd_count)) { 862d4fd0404SClaudiu Manoil i = 0; 863d4fd0404SClaudiu Manoil tx_swbd = tx_ring->tx_swbd; 864d4fd0404SClaudiu Manoil } 865d4fd0404SClaudiu Manoil 866d4fd0404SClaudiu Manoil /* BD iteration loop end */ 867a93580a0SVladimir Oltean if (is_eof) { 868d4fd0404SClaudiu Manoil tx_frm_cnt++; 869d4fd0404SClaudiu Manoil /* re-arm interrupt source */ 870fd5736bfSAlex Marginean enetc_wr_reg_hot(tx_ring->idr, BIT(tx_ring->index) | 871d4fd0404SClaudiu Manoil BIT(16 + tx_ring->index)); 872d4fd0404SClaudiu Manoil } 873d4fd0404SClaudiu Manoil 874d4fd0404SClaudiu Manoil if (unlikely(!bds_to_clean)) 875d4fd0404SClaudiu Manoil bds_to_clean = enetc_bd_ready_count(tx_ring, i); 876d4fd0404SClaudiu Manoil } 877d4fd0404SClaudiu Manoil 878d4fd0404SClaudiu Manoil tx_ring->next_to_clean = i; 879d4fd0404SClaudiu Manoil tx_ring->stats.packets += tx_frm_cnt; 880d4fd0404SClaudiu Manoil tx_ring->stats.bytes += tx_byte_cnt; 881285e8dedSPo Liu tx_ring->stats.win_drop += tx_win_drop; 882d4fd0404SClaudiu Manoil 883d4fd0404SClaudiu Manoil if (unlikely(tx_frm_cnt && netif_carrier_ok(ndev) && 884d4fd0404SClaudiu Manoil __netif_subqueue_stopped(ndev, tx_ring->index) && 885d4fd0404SClaudiu Manoil (enetc_bd_unused(tx_ring) >= ENETC_TXBDS_MAX_NEEDED))) { 886d4fd0404SClaudiu Manoil netif_wake_subqueue(ndev, tx_ring->index); 887d4fd0404SClaudiu Manoil } 888d4fd0404SClaudiu Manoil 889d4fd0404SClaudiu Manoil return tx_frm_cnt != ENETC_DEFAULT_TX_WORK; 890d4fd0404SClaudiu Manoil } 891d4fd0404SClaudiu Manoil 892d4fd0404SClaudiu Manoil static bool enetc_new_page(struct enetc_bdr *rx_ring, 893d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd) 894d4fd0404SClaudiu Manoil { 8957ed2bc80SVladimir Oltean bool xdp = !!(rx_ring->xdp.prog); 896d4fd0404SClaudiu Manoil struct page *page; 897d4fd0404SClaudiu Manoil dma_addr_t addr; 898d4fd0404SClaudiu Manoil 899d4fd0404SClaudiu Manoil page = dev_alloc_page(); 900d4fd0404SClaudiu Manoil if (unlikely(!page)) 901d4fd0404SClaudiu Manoil return false; 902d4fd0404SClaudiu Manoil 9037ed2bc80SVladimir Oltean /* For XDP_TX, we forgo dma_unmap -> dma_map */ 9047ed2bc80SVladimir Oltean rx_swbd->dir = xdp ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE; 9057ed2bc80SVladimir Oltean 9067ed2bc80SVladimir Oltean addr = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, rx_swbd->dir); 907d4fd0404SClaudiu Manoil if (unlikely(dma_mapping_error(rx_ring->dev, addr))) { 908d4fd0404SClaudiu Manoil __free_page(page); 909d4fd0404SClaudiu Manoil 910d4fd0404SClaudiu Manoil return false; 911d4fd0404SClaudiu Manoil } 912d4fd0404SClaudiu Manoil 913d4fd0404SClaudiu Manoil rx_swbd->dma = addr; 914d4fd0404SClaudiu Manoil rx_swbd->page = page; 915d1b15102SVladimir Oltean rx_swbd->page_offset = rx_ring->buffer_offset; 916d4fd0404SClaudiu Manoil 917d4fd0404SClaudiu Manoil return true; 918d4fd0404SClaudiu Manoil } 919d4fd0404SClaudiu Manoil 920d4fd0404SClaudiu Manoil static int enetc_refill_rx_ring(struct enetc_bdr *rx_ring, const int buff_cnt) 921d4fd0404SClaudiu Manoil { 922d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd; 923d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd; 924d4fd0404SClaudiu Manoil int i, j; 925d4fd0404SClaudiu Manoil 926d4fd0404SClaudiu Manoil i = rx_ring->next_to_use; 927d4fd0404SClaudiu Manoil rx_swbd = &rx_ring->rx_swbd[i]; 928714239acSClaudiu Manoil rxbd = enetc_rxbd(rx_ring, i); 929d4fd0404SClaudiu Manoil 930d4fd0404SClaudiu Manoil for (j = 0; j < buff_cnt; j++) { 931d4fd0404SClaudiu Manoil /* try reuse page */ 932d4fd0404SClaudiu Manoil if (unlikely(!rx_swbd->page)) { 933d4fd0404SClaudiu Manoil if (unlikely(!enetc_new_page(rx_ring, rx_swbd))) { 934d4fd0404SClaudiu Manoil rx_ring->stats.rx_alloc_errs++; 935d4fd0404SClaudiu Manoil break; 936d4fd0404SClaudiu Manoil } 937d4fd0404SClaudiu Manoil } 938d4fd0404SClaudiu Manoil 939d4fd0404SClaudiu Manoil /* update RxBD */ 940d4fd0404SClaudiu Manoil rxbd->w.addr = cpu_to_le64(rx_swbd->dma + 941d4fd0404SClaudiu Manoil rx_swbd->page_offset); 942d4fd0404SClaudiu Manoil /* clear 'R" as well */ 943d4fd0404SClaudiu Manoil rxbd->r.lstatus = 0; 944d4fd0404SClaudiu Manoil 945c027aa92SVladimir Oltean enetc_rxbd_next(rx_ring, &rxbd, &i); 946c027aa92SVladimir Oltean rx_swbd = &rx_ring->rx_swbd[i]; 947d4fd0404SClaudiu Manoil } 948d4fd0404SClaudiu Manoil 949d4fd0404SClaudiu Manoil if (likely(j)) { 950d4fd0404SClaudiu Manoil rx_ring->next_to_alloc = i; /* keep track from page reuse */ 951d4fd0404SClaudiu Manoil rx_ring->next_to_use = i; 9527a5222cbSVladimir Oltean 9537a5222cbSVladimir Oltean /* update ENETC's consumer index */ 9547a5222cbSVladimir Oltean enetc_wr_reg_hot(rx_ring->rcir, rx_ring->next_to_use); 955d4fd0404SClaudiu Manoil } 956d4fd0404SClaudiu Manoil 957d4fd0404SClaudiu Manoil return j; 958d4fd0404SClaudiu Manoil } 959d4fd0404SClaudiu Manoil 960434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 961d3982312SY.b. Lu static void enetc_get_rx_tstamp(struct net_device *ndev, 962d3982312SY.b. Lu union enetc_rx_bd *rxbd, 963d3982312SY.b. Lu struct sk_buff *skb) 964d3982312SY.b. Lu { 965d3982312SY.b. Lu struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 966d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 967d3982312SY.b. Lu struct enetc_hw *hw = &priv->si->hw; 968cec4f328SY.b. Lu u32 lo, hi, tstamp_lo; 969d3982312SY.b. Lu u64 tstamp; 970d3982312SY.b. Lu 971cec4f328SY.b. Lu if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TSTMP) { 972fd5736bfSAlex Marginean lo = enetc_rd_reg_hot(hw->reg + ENETC_SICTR0); 973fd5736bfSAlex Marginean hi = enetc_rd_reg_hot(hw->reg + ENETC_SICTR1); 974434cebabSClaudiu Manoil rxbd = enetc_rxbd_ext(rxbd); 975434cebabSClaudiu Manoil tstamp_lo = le32_to_cpu(rxbd->ext.tstamp); 976cec4f328SY.b. Lu if (lo <= tstamp_lo) 977d3982312SY.b. Lu hi -= 1; 978d3982312SY.b. Lu 979cec4f328SY.b. Lu tstamp = (u64)hi << 32 | tstamp_lo; 980d3982312SY.b. Lu memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 981d3982312SY.b. Lu shhwtstamps->hwtstamp = ns_to_ktime(tstamp); 982d3982312SY.b. Lu } 983d3982312SY.b. Lu } 984d3982312SY.b. Lu #endif 985d3982312SY.b. Lu 986d4fd0404SClaudiu Manoil static void enetc_get_offloads(struct enetc_bdr *rx_ring, 987d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd, struct sk_buff *skb) 988d4fd0404SClaudiu Manoil { 989d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev); 990827b6fd0SVladimir Oltean 991d3982312SY.b. Lu /* TODO: hashing */ 992d4fd0404SClaudiu Manoil if (rx_ring->ndev->features & NETIF_F_RXCSUM) { 993d4fd0404SClaudiu Manoil u16 inet_csum = le16_to_cpu(rxbd->r.inet_csum); 994d4fd0404SClaudiu Manoil 995d4fd0404SClaudiu Manoil skb->csum = csum_unfold((__force __sum16)~htons(inet_csum)); 996d4fd0404SClaudiu Manoil skb->ip_summed = CHECKSUM_COMPLETE; 997d4fd0404SClaudiu Manoil } 998d4fd0404SClaudiu Manoil 999827b6fd0SVladimir Oltean if (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_VLAN) { 1000827b6fd0SVladimir Oltean __be16 tpid = 0; 1001827b6fd0SVladimir Oltean 1002827b6fd0SVladimir Oltean switch (le16_to_cpu(rxbd->r.flags) & ENETC_RXBD_FLAG_TPID) { 1003827b6fd0SVladimir Oltean case 0: 1004827b6fd0SVladimir Oltean tpid = htons(ETH_P_8021Q); 1005827b6fd0SVladimir Oltean break; 1006827b6fd0SVladimir Oltean case 1: 1007827b6fd0SVladimir Oltean tpid = htons(ETH_P_8021AD); 1008827b6fd0SVladimir Oltean break; 1009827b6fd0SVladimir Oltean case 2: 1010827b6fd0SVladimir Oltean tpid = htons(enetc_port_rd(&priv->si->hw, 1011827b6fd0SVladimir Oltean ENETC_PCVLANR1)); 1012827b6fd0SVladimir Oltean break; 1013827b6fd0SVladimir Oltean case 3: 1014827b6fd0SVladimir Oltean tpid = htons(enetc_port_rd(&priv->si->hw, 1015827b6fd0SVladimir Oltean ENETC_PCVLANR2)); 1016827b6fd0SVladimir Oltean break; 1017827b6fd0SVladimir Oltean default: 1018827b6fd0SVladimir Oltean break; 1019827b6fd0SVladimir Oltean } 1020827b6fd0SVladimir Oltean 1021827b6fd0SVladimir Oltean __vlan_hwaccel_put_tag(skb, tpid, le16_to_cpu(rxbd->r.vlan_opt)); 1022827b6fd0SVladimir Oltean } 1023827b6fd0SVladimir Oltean 1024434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 1025d3982312SY.b. Lu if (priv->active_offloads & ENETC_F_RX_TSTAMP) 1026d3982312SY.b. Lu enetc_get_rx_tstamp(rx_ring->ndev, rxbd, skb); 1027d3982312SY.b. Lu #endif 1028d4fd0404SClaudiu Manoil } 1029d4fd0404SClaudiu Manoil 10307ed2bc80SVladimir Oltean /* This gets called during the non-XDP NAPI poll cycle as well as on XDP_PASS, 10317ed2bc80SVladimir Oltean * so it needs to work with both DMA_FROM_DEVICE as well as DMA_BIDIRECTIONAL 10327ed2bc80SVladimir Oltean * mapped buffers. 10337ed2bc80SVladimir Oltean */ 1034d4fd0404SClaudiu Manoil static struct enetc_rx_swbd *enetc_get_rx_buff(struct enetc_bdr *rx_ring, 1035d4fd0404SClaudiu Manoil int i, u16 size) 1036d4fd0404SClaudiu Manoil { 1037d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i]; 1038d4fd0404SClaudiu Manoil 1039d4fd0404SClaudiu Manoil dma_sync_single_range_for_cpu(rx_ring->dev, rx_swbd->dma, 1040d4fd0404SClaudiu Manoil rx_swbd->page_offset, 10417ed2bc80SVladimir Oltean size, rx_swbd->dir); 1042d4fd0404SClaudiu Manoil return rx_swbd; 1043d4fd0404SClaudiu Manoil } 1044d4fd0404SClaudiu Manoil 10456b04830dSVladimir Oltean /* Reuse the current page without performing half-page buffer flipping */ 1046d4fd0404SClaudiu Manoil static void enetc_put_rx_buff(struct enetc_bdr *rx_ring, 1047d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd) 1048d4fd0404SClaudiu Manoil { 1049d1b15102SVladimir Oltean size_t buffer_size = ENETC_RXB_TRUESIZE - rx_ring->buffer_offset; 1050d1b15102SVladimir Oltean 1051d4fd0404SClaudiu Manoil enetc_reuse_page(rx_ring, rx_swbd); 1052d4fd0404SClaudiu Manoil 1053d4fd0404SClaudiu Manoil dma_sync_single_range_for_device(rx_ring->dev, rx_swbd->dma, 1054d4fd0404SClaudiu Manoil rx_swbd->page_offset, 10557ed2bc80SVladimir Oltean buffer_size, rx_swbd->dir); 10566b04830dSVladimir Oltean 10576b04830dSVladimir Oltean rx_swbd->page = NULL; 10586b04830dSVladimir Oltean } 10596b04830dSVladimir Oltean 10606b04830dSVladimir Oltean /* Reuse the current page by performing half-page buffer flipping */ 10616b04830dSVladimir Oltean static void enetc_flip_rx_buff(struct enetc_bdr *rx_ring, 10626b04830dSVladimir Oltean struct enetc_rx_swbd *rx_swbd) 10636b04830dSVladimir Oltean { 10646b04830dSVladimir Oltean if (likely(enetc_page_reusable(rx_swbd->page))) { 10656b04830dSVladimir Oltean rx_swbd->page_offset ^= ENETC_RXB_TRUESIZE; 10666b04830dSVladimir Oltean page_ref_inc(rx_swbd->page); 10676b04830dSVladimir Oltean 10686b04830dSVladimir Oltean enetc_put_rx_buff(rx_ring, rx_swbd); 1069d4fd0404SClaudiu Manoil } else { 10707ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE, 10717ed2bc80SVladimir Oltean rx_swbd->dir); 1072d4fd0404SClaudiu Manoil rx_swbd->page = NULL; 1073d4fd0404SClaudiu Manoil } 10746b04830dSVladimir Oltean } 1075d4fd0404SClaudiu Manoil 1076d4fd0404SClaudiu Manoil static struct sk_buff *enetc_map_rx_buff_to_skb(struct enetc_bdr *rx_ring, 1077d4fd0404SClaudiu Manoil int i, u16 size) 1078d4fd0404SClaudiu Manoil { 1079d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1080d4fd0404SClaudiu Manoil struct sk_buff *skb; 1081d4fd0404SClaudiu Manoil void *ba; 1082d4fd0404SClaudiu Manoil 1083d4fd0404SClaudiu Manoil ba = page_address(rx_swbd->page) + rx_swbd->page_offset; 1084d1b15102SVladimir Oltean skb = build_skb(ba - rx_ring->buffer_offset, ENETC_RXB_TRUESIZE); 1085d4fd0404SClaudiu Manoil if (unlikely(!skb)) { 1086d4fd0404SClaudiu Manoil rx_ring->stats.rx_alloc_errs++; 1087d4fd0404SClaudiu Manoil return NULL; 1088d4fd0404SClaudiu Manoil } 1089d4fd0404SClaudiu Manoil 1090d1b15102SVladimir Oltean skb_reserve(skb, rx_ring->buffer_offset); 1091d4fd0404SClaudiu Manoil __skb_put(skb, size); 1092d4fd0404SClaudiu Manoil 10936b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, rx_swbd); 1094d4fd0404SClaudiu Manoil 1095d4fd0404SClaudiu Manoil return skb; 1096d4fd0404SClaudiu Manoil } 1097d4fd0404SClaudiu Manoil 1098d4fd0404SClaudiu Manoil static void enetc_add_rx_buff_to_skb(struct enetc_bdr *rx_ring, int i, 1099d4fd0404SClaudiu Manoil u16 size, struct sk_buff *skb) 1100d4fd0404SClaudiu Manoil { 1101d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1102d4fd0404SClaudiu Manoil 1103d4fd0404SClaudiu Manoil skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_swbd->page, 1104d4fd0404SClaudiu Manoil rx_swbd->page_offset, size, ENETC_RXB_TRUESIZE); 1105d4fd0404SClaudiu Manoil 11066b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, rx_swbd); 1107d4fd0404SClaudiu Manoil } 1108d4fd0404SClaudiu Manoil 11092fa423f5SVladimir Oltean static bool enetc_check_bd_errors_and_consume(struct enetc_bdr *rx_ring, 11102fa423f5SVladimir Oltean u32 bd_status, 11112fa423f5SVladimir Oltean union enetc_rx_bd **rxbd, int *i) 11122fa423f5SVladimir Oltean { 11132fa423f5SVladimir Oltean if (likely(!(bd_status & ENETC_RXBD_LSTATUS(ENETC_RXBD_ERR_MASK)))) 11142fa423f5SVladimir Oltean return false; 11152fa423f5SVladimir Oltean 1116672f9a21SVladimir Oltean enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]); 11172fa423f5SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 11182fa423f5SVladimir Oltean 11192fa423f5SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 11202fa423f5SVladimir Oltean dma_rmb(); 11212fa423f5SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 11222fa423f5SVladimir Oltean 1123672f9a21SVladimir Oltean enetc_put_rx_buff(rx_ring, &rx_ring->rx_swbd[*i]); 11242fa423f5SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 11252fa423f5SVladimir Oltean } 11262fa423f5SVladimir Oltean 11272fa423f5SVladimir Oltean rx_ring->ndev->stats.rx_dropped++; 11282fa423f5SVladimir Oltean rx_ring->ndev->stats.rx_errors++; 11292fa423f5SVladimir Oltean 11302fa423f5SVladimir Oltean return true; 11312fa423f5SVladimir Oltean } 11322fa423f5SVladimir Oltean 1133a800abd3SVladimir Oltean static struct sk_buff *enetc_build_skb(struct enetc_bdr *rx_ring, 1134a800abd3SVladimir Oltean u32 bd_status, union enetc_rx_bd **rxbd, 1135d1b15102SVladimir Oltean int *i, int *cleaned_cnt, int buffer_size) 1136a800abd3SVladimir Oltean { 1137a800abd3SVladimir Oltean struct sk_buff *skb; 1138a800abd3SVladimir Oltean u16 size; 1139a800abd3SVladimir Oltean 1140a800abd3SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1141a800abd3SVladimir Oltean skb = enetc_map_rx_buff_to_skb(rx_ring, *i, size); 1142a800abd3SVladimir Oltean if (!skb) 1143a800abd3SVladimir Oltean return NULL; 1144a800abd3SVladimir Oltean 1145a800abd3SVladimir Oltean enetc_get_offloads(rx_ring, *rxbd, skb); 1146a800abd3SVladimir Oltean 1147a800abd3SVladimir Oltean (*cleaned_cnt)++; 1148a800abd3SVladimir Oltean 1149a800abd3SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1150a800abd3SVladimir Oltean 1151a800abd3SVladimir Oltean /* not last BD in frame? */ 1152a800abd3SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 1153a800abd3SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 1154d1b15102SVladimir Oltean size = buffer_size; 1155a800abd3SVladimir Oltean 1156a800abd3SVladimir Oltean if (bd_status & ENETC_RXBD_LSTATUS_F) { 1157a800abd3SVladimir Oltean dma_rmb(); 1158a800abd3SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1159a800abd3SVladimir Oltean } 1160a800abd3SVladimir Oltean 1161a800abd3SVladimir Oltean enetc_add_rx_buff_to_skb(rx_ring, *i, size, skb); 1162a800abd3SVladimir Oltean 1163a800abd3SVladimir Oltean (*cleaned_cnt)++; 1164a800abd3SVladimir Oltean 1165a800abd3SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1166a800abd3SVladimir Oltean } 1167a800abd3SVladimir Oltean 1168a800abd3SVladimir Oltean skb_record_rx_queue(skb, rx_ring->index); 1169a800abd3SVladimir Oltean skb->protocol = eth_type_trans(skb, rx_ring->ndev); 1170a800abd3SVladimir Oltean 1171a800abd3SVladimir Oltean return skb; 1172a800abd3SVladimir Oltean } 1173a800abd3SVladimir Oltean 1174d4fd0404SClaudiu Manoil #define ENETC_RXBD_BUNDLE 16 /* # of BDs to update at once */ 1175d4fd0404SClaudiu Manoil 1176d4fd0404SClaudiu Manoil static int enetc_clean_rx_ring(struct enetc_bdr *rx_ring, 1177d4fd0404SClaudiu Manoil struct napi_struct *napi, int work_limit) 1178d4fd0404SClaudiu Manoil { 1179d4fd0404SClaudiu Manoil int rx_frm_cnt = 0, rx_byte_cnt = 0; 1180d4fd0404SClaudiu Manoil int cleaned_cnt, i; 1181d4fd0404SClaudiu Manoil 1182d4fd0404SClaudiu Manoil cleaned_cnt = enetc_bd_unused(rx_ring); 1183d4fd0404SClaudiu Manoil /* next descriptor to process */ 1184d4fd0404SClaudiu Manoil i = rx_ring->next_to_clean; 1185d4fd0404SClaudiu Manoil 1186d4fd0404SClaudiu Manoil while (likely(rx_frm_cnt < work_limit)) { 1187d4fd0404SClaudiu Manoil union enetc_rx_bd *rxbd; 1188d4fd0404SClaudiu Manoil struct sk_buff *skb; 1189d4fd0404SClaudiu Manoil u32 bd_status; 1190d4fd0404SClaudiu Manoil 11917a5222cbSVladimir Oltean if (cleaned_cnt >= ENETC_RXBD_BUNDLE) 11927a5222cbSVladimir Oltean cleaned_cnt -= enetc_refill_rx_ring(rx_ring, 11937a5222cbSVladimir Oltean cleaned_cnt); 1194d4fd0404SClaudiu Manoil 1195714239acSClaudiu Manoil rxbd = enetc_rxbd(rx_ring, i); 1196d4fd0404SClaudiu Manoil bd_status = le32_to_cpu(rxbd->r.lstatus); 11976d36ecdbSVladimir Oltean if (!bd_status) 1198d4fd0404SClaudiu Manoil break; 1199d4fd0404SClaudiu Manoil 1200fd5736bfSAlex Marginean enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index)); 1201d4fd0404SClaudiu Manoil dma_rmb(); /* for reading other rxbd fields */ 12022fa423f5SVladimir Oltean 12032fa423f5SVladimir Oltean if (enetc_check_bd_errors_and_consume(rx_ring, bd_status, 12042fa423f5SVladimir Oltean &rxbd, &i)) 12052fa423f5SVladimir Oltean break; 12062fa423f5SVladimir Oltean 1207a800abd3SVladimir Oltean skb = enetc_build_skb(rx_ring, bd_status, &rxbd, &i, 1208d1b15102SVladimir Oltean &cleaned_cnt, ENETC_RXB_DMA_SIZE); 12096d36ecdbSVladimir Oltean if (!skb) 1210d4fd0404SClaudiu Manoil break; 1211d4fd0404SClaudiu Manoil 1212d4fd0404SClaudiu Manoil rx_byte_cnt += skb->len; 1213a800abd3SVladimir Oltean rx_frm_cnt++; 1214d4fd0404SClaudiu Manoil 1215d4fd0404SClaudiu Manoil napi_gro_receive(napi, skb); 1216d4fd0404SClaudiu Manoil } 1217d4fd0404SClaudiu Manoil 1218d4fd0404SClaudiu Manoil rx_ring->next_to_clean = i; 1219d4fd0404SClaudiu Manoil 1220d4fd0404SClaudiu Manoil rx_ring->stats.packets += rx_frm_cnt; 1221d4fd0404SClaudiu Manoil rx_ring->stats.bytes += rx_byte_cnt; 1222d4fd0404SClaudiu Manoil 1223d4fd0404SClaudiu Manoil return rx_frm_cnt; 1224d4fd0404SClaudiu Manoil } 1225d4fd0404SClaudiu Manoil 12267ed2bc80SVladimir Oltean static void enetc_xdp_map_tx_buff(struct enetc_bdr *tx_ring, int i, 12277ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd, 12287ed2bc80SVladimir Oltean int frm_len) 12297ed2bc80SVladimir Oltean { 12307ed2bc80SVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 12317ed2bc80SVladimir Oltean 12327ed2bc80SVladimir Oltean prefetchw(txbd); 12337ed2bc80SVladimir Oltean 12347ed2bc80SVladimir Oltean enetc_clear_tx_bd(txbd); 12357ed2bc80SVladimir Oltean txbd->addr = cpu_to_le64(tx_swbd->dma + tx_swbd->page_offset); 12367ed2bc80SVladimir Oltean txbd->buf_len = cpu_to_le16(tx_swbd->len); 12377ed2bc80SVladimir Oltean txbd->frm_len = cpu_to_le16(frm_len); 12387ed2bc80SVladimir Oltean 12397ed2bc80SVladimir Oltean memcpy(&tx_ring->tx_swbd[i], tx_swbd, sizeof(*tx_swbd)); 12407ed2bc80SVladimir Oltean } 12417ed2bc80SVladimir Oltean 12427ed2bc80SVladimir Oltean /* Puts in the TX ring one XDP frame, mapped as an array of TX software buffer 12437ed2bc80SVladimir Oltean * descriptors. 12447ed2bc80SVladimir Oltean */ 12457ed2bc80SVladimir Oltean static bool enetc_xdp_tx(struct enetc_bdr *tx_ring, 12467ed2bc80SVladimir Oltean struct enetc_tx_swbd *xdp_tx_arr, int num_tx_swbd) 12477ed2bc80SVladimir Oltean { 12487ed2bc80SVladimir Oltean struct enetc_tx_swbd *tmp_tx_swbd = xdp_tx_arr; 12497ed2bc80SVladimir Oltean int i, k, frm_len = tmp_tx_swbd->len; 12507ed2bc80SVladimir Oltean 12517ed2bc80SVladimir Oltean if (unlikely(enetc_bd_unused(tx_ring) < ENETC_TXBDS_NEEDED(num_tx_swbd))) 12527ed2bc80SVladimir Oltean return false; 12537ed2bc80SVladimir Oltean 12547ed2bc80SVladimir Oltean while (unlikely(!tmp_tx_swbd->is_eof)) { 12557ed2bc80SVladimir Oltean tmp_tx_swbd++; 12567ed2bc80SVladimir Oltean frm_len += tmp_tx_swbd->len; 12577ed2bc80SVladimir Oltean } 12587ed2bc80SVladimir Oltean 12597ed2bc80SVladimir Oltean i = tx_ring->next_to_use; 12607ed2bc80SVladimir Oltean 12617ed2bc80SVladimir Oltean for (k = 0; k < num_tx_swbd; k++) { 12627ed2bc80SVladimir Oltean struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[k]; 12637ed2bc80SVladimir Oltean 12647ed2bc80SVladimir Oltean enetc_xdp_map_tx_buff(tx_ring, i, xdp_tx_swbd, frm_len); 12657ed2bc80SVladimir Oltean 12667ed2bc80SVladimir Oltean /* last BD needs 'F' bit set */ 12677ed2bc80SVladimir Oltean if (xdp_tx_swbd->is_eof) { 12687ed2bc80SVladimir Oltean union enetc_tx_bd *txbd = ENETC_TXBD(*tx_ring, i); 12697ed2bc80SVladimir Oltean 12707ed2bc80SVladimir Oltean txbd->flags = ENETC_TXBD_FLAGS_F; 12717ed2bc80SVladimir Oltean } 12727ed2bc80SVladimir Oltean 12737ed2bc80SVladimir Oltean enetc_bdr_idx_inc(tx_ring, &i); 12747ed2bc80SVladimir Oltean } 12757ed2bc80SVladimir Oltean 12767ed2bc80SVladimir Oltean tx_ring->next_to_use = i; 12777ed2bc80SVladimir Oltean 12787ed2bc80SVladimir Oltean return true; 12797ed2bc80SVladimir Oltean } 12807ed2bc80SVladimir Oltean 12819d2b68ccSVladimir Oltean static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring, 12829d2b68ccSVladimir Oltean struct enetc_tx_swbd *xdp_tx_arr, 12839d2b68ccSVladimir Oltean struct xdp_frame *xdp_frame) 12849d2b68ccSVladimir Oltean { 12859d2b68ccSVladimir Oltean struct enetc_tx_swbd *xdp_tx_swbd = &xdp_tx_arr[0]; 12869d2b68ccSVladimir Oltean struct skb_shared_info *shinfo; 12879d2b68ccSVladimir Oltean void *data = xdp_frame->data; 12889d2b68ccSVladimir Oltean int len = xdp_frame->len; 12899d2b68ccSVladimir Oltean skb_frag_t *frag; 12909d2b68ccSVladimir Oltean dma_addr_t dma; 12919d2b68ccSVladimir Oltean unsigned int f; 12929d2b68ccSVladimir Oltean int n = 0; 12939d2b68ccSVladimir Oltean 12949d2b68ccSVladimir Oltean dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE); 12959d2b68ccSVladimir Oltean if (unlikely(dma_mapping_error(tx_ring->dev, dma))) { 12969d2b68ccSVladimir Oltean netdev_err(tx_ring->ndev, "DMA map error\n"); 12979d2b68ccSVladimir Oltean return -1; 12989d2b68ccSVladimir Oltean } 12999d2b68ccSVladimir Oltean 13009d2b68ccSVladimir Oltean xdp_tx_swbd->dma = dma; 13019d2b68ccSVladimir Oltean xdp_tx_swbd->dir = DMA_TO_DEVICE; 13029d2b68ccSVladimir Oltean xdp_tx_swbd->len = len; 13039d2b68ccSVladimir Oltean xdp_tx_swbd->is_xdp_redirect = true; 13049d2b68ccSVladimir Oltean xdp_tx_swbd->is_eof = false; 13059d2b68ccSVladimir Oltean xdp_tx_swbd->xdp_frame = NULL; 13069d2b68ccSVladimir Oltean 13079d2b68ccSVladimir Oltean n++; 1308c7030d14SLorenzo Bianconi 1309c7030d14SLorenzo Bianconi if (!xdp_frame_has_frags(xdp_frame)) 1310c7030d14SLorenzo Bianconi goto out; 1311c7030d14SLorenzo Bianconi 13129d2b68ccSVladimir Oltean xdp_tx_swbd = &xdp_tx_arr[n]; 13139d2b68ccSVladimir Oltean 13149d2b68ccSVladimir Oltean shinfo = xdp_get_shared_info_from_frame(xdp_frame); 13159d2b68ccSVladimir Oltean 13169d2b68ccSVladimir Oltean for (f = 0, frag = &shinfo->frags[0]; f < shinfo->nr_frags; 13179d2b68ccSVladimir Oltean f++, frag++) { 13189d2b68ccSVladimir Oltean data = skb_frag_address(frag); 13199d2b68ccSVladimir Oltean len = skb_frag_size(frag); 13209d2b68ccSVladimir Oltean 13219d2b68ccSVladimir Oltean dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE); 13229d2b68ccSVladimir Oltean if (unlikely(dma_mapping_error(tx_ring->dev, dma))) { 13239d2b68ccSVladimir Oltean /* Undo the DMA mapping for all fragments */ 1324626b598aSDan Carpenter while (--n >= 0) 13259d2b68ccSVladimir Oltean enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]); 13269d2b68ccSVladimir Oltean 13279d2b68ccSVladimir Oltean netdev_err(tx_ring->ndev, "DMA map error\n"); 13289d2b68ccSVladimir Oltean return -1; 13299d2b68ccSVladimir Oltean } 13309d2b68ccSVladimir Oltean 13319d2b68ccSVladimir Oltean xdp_tx_swbd->dma = dma; 13329d2b68ccSVladimir Oltean xdp_tx_swbd->dir = DMA_TO_DEVICE; 13339d2b68ccSVladimir Oltean xdp_tx_swbd->len = len; 13349d2b68ccSVladimir Oltean xdp_tx_swbd->is_xdp_redirect = true; 13359d2b68ccSVladimir Oltean xdp_tx_swbd->is_eof = false; 13369d2b68ccSVladimir Oltean xdp_tx_swbd->xdp_frame = NULL; 13379d2b68ccSVladimir Oltean 13389d2b68ccSVladimir Oltean n++; 13399d2b68ccSVladimir Oltean xdp_tx_swbd = &xdp_tx_arr[n]; 13409d2b68ccSVladimir Oltean } 1341c7030d14SLorenzo Bianconi out: 13429d2b68ccSVladimir Oltean xdp_tx_arr[n - 1].is_eof = true; 13439d2b68ccSVladimir Oltean xdp_tx_arr[n - 1].xdp_frame = xdp_frame; 13449d2b68ccSVladimir Oltean 13459d2b68ccSVladimir Oltean return n; 13469d2b68ccSVladimir Oltean } 13479d2b68ccSVladimir Oltean 13489d2b68ccSVladimir Oltean int enetc_xdp_xmit(struct net_device *ndev, int num_frames, 13499d2b68ccSVladimir Oltean struct xdp_frame **frames, u32 flags) 13509d2b68ccSVladimir Oltean { 13519d2b68ccSVladimir Oltean struct enetc_tx_swbd xdp_redirect_arr[ENETC_MAX_SKB_FRAGS] = {0}; 13529d2b68ccSVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(ndev); 13539d2b68ccSVladimir Oltean struct enetc_bdr *tx_ring; 13549d2b68ccSVladimir Oltean int xdp_tx_bd_cnt, i, k; 13559d2b68ccSVladimir Oltean int xdp_tx_frm_cnt = 0; 13569d2b68ccSVladimir Oltean 135724e39309SVladimir Oltean enetc_lock_mdio(); 135824e39309SVladimir Oltean 13597eab503bSVladimir Oltean tx_ring = priv->xdp_tx_ring[smp_processor_id()]; 13609d2b68ccSVladimir Oltean 13619d2b68ccSVladimir Oltean prefetchw(ENETC_TXBD(*tx_ring, tx_ring->next_to_use)); 13629d2b68ccSVladimir Oltean 13639d2b68ccSVladimir Oltean for (k = 0; k < num_frames; k++) { 13649d2b68ccSVladimir Oltean xdp_tx_bd_cnt = enetc_xdp_frame_to_xdp_tx_swbd(tx_ring, 13659d2b68ccSVladimir Oltean xdp_redirect_arr, 13669d2b68ccSVladimir Oltean frames[k]); 13679d2b68ccSVladimir Oltean if (unlikely(xdp_tx_bd_cnt < 0)) 13689d2b68ccSVladimir Oltean break; 13699d2b68ccSVladimir Oltean 13709d2b68ccSVladimir Oltean if (unlikely(!enetc_xdp_tx(tx_ring, xdp_redirect_arr, 13719d2b68ccSVladimir Oltean xdp_tx_bd_cnt))) { 13729d2b68ccSVladimir Oltean for (i = 0; i < xdp_tx_bd_cnt; i++) 13739d2b68ccSVladimir Oltean enetc_unmap_tx_buff(tx_ring, 13749d2b68ccSVladimir Oltean &xdp_redirect_arr[i]); 13759d2b68ccSVladimir Oltean tx_ring->stats.xdp_tx_drops++; 13769d2b68ccSVladimir Oltean break; 13779d2b68ccSVladimir Oltean } 13789d2b68ccSVladimir Oltean 13799d2b68ccSVladimir Oltean xdp_tx_frm_cnt++; 13809d2b68ccSVladimir Oltean } 13819d2b68ccSVladimir Oltean 13829d2b68ccSVladimir Oltean if (unlikely((flags & XDP_XMIT_FLUSH) || k != xdp_tx_frm_cnt)) 13839d2b68ccSVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 13849d2b68ccSVladimir Oltean 13859d2b68ccSVladimir Oltean tx_ring->stats.xdp_tx += xdp_tx_frm_cnt; 13869d2b68ccSVladimir Oltean 138724e39309SVladimir Oltean enetc_unlock_mdio(); 138824e39309SVladimir Oltean 13899d2b68ccSVladimir Oltean return xdp_tx_frm_cnt; 13909d2b68ccSVladimir Oltean } 13919d2b68ccSVladimir Oltean 1392d1b15102SVladimir Oltean static void enetc_map_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i, 1393d1b15102SVladimir Oltean struct xdp_buff *xdp_buff, u16 size) 1394d1b15102SVladimir Oltean { 1395d1b15102SVladimir Oltean struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1396d1b15102SVladimir Oltean void *hard_start = page_address(rx_swbd->page) + rx_swbd->page_offset; 1397d1b15102SVladimir Oltean 13987ed2bc80SVladimir Oltean /* To be used for XDP_TX */ 13997ed2bc80SVladimir Oltean rx_swbd->len = size; 14007ed2bc80SVladimir Oltean 1401d1b15102SVladimir Oltean xdp_prepare_buff(xdp_buff, hard_start - rx_ring->buffer_offset, 1402d1b15102SVladimir Oltean rx_ring->buffer_offset, size, false); 1403d1b15102SVladimir Oltean } 1404d1b15102SVladimir Oltean 1405d1b15102SVladimir Oltean static void enetc_add_rx_buff_to_xdp(struct enetc_bdr *rx_ring, int i, 1406d1b15102SVladimir Oltean u16 size, struct xdp_buff *xdp_buff) 1407d1b15102SVladimir Oltean { 1408d1b15102SVladimir Oltean struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp_buff); 1409d1b15102SVladimir Oltean struct enetc_rx_swbd *rx_swbd = enetc_get_rx_buff(rx_ring, i, size); 1410c7030d14SLorenzo Bianconi skb_frag_t *frag; 1411d1b15102SVladimir Oltean 14127ed2bc80SVladimir Oltean /* To be used for XDP_TX */ 14137ed2bc80SVladimir Oltean rx_swbd->len = size; 14147ed2bc80SVladimir Oltean 14158feb020fSLorenzo Bianconi if (!xdp_buff_has_frags(xdp_buff)) { 14168feb020fSLorenzo Bianconi xdp_buff_set_frags_flag(xdp_buff); 14178feb020fSLorenzo Bianconi shinfo->xdp_frags_size = size; 1418c7030d14SLorenzo Bianconi shinfo->nr_frags = 0; 14198feb020fSLorenzo Bianconi } else { 14208feb020fSLorenzo Bianconi shinfo->xdp_frags_size += size; 14218feb020fSLorenzo Bianconi } 14228feb020fSLorenzo Bianconi 14238feb020fSLorenzo Bianconi if (page_is_pfmemalloc(rx_swbd->page)) 14248feb020fSLorenzo Bianconi xdp_buff_set_frag_pfmemalloc(xdp_buff); 14258feb020fSLorenzo Bianconi 1426c7030d14SLorenzo Bianconi frag = &shinfo->frags[shinfo->nr_frags]; 1427d1b15102SVladimir Oltean skb_frag_off_set(frag, rx_swbd->page_offset); 1428d1b15102SVladimir Oltean skb_frag_size_set(frag, size); 1429d1b15102SVladimir Oltean __skb_frag_set_page(frag, rx_swbd->page); 1430d1b15102SVladimir Oltean 1431d1b15102SVladimir Oltean shinfo->nr_frags++; 1432d1b15102SVladimir Oltean } 1433d1b15102SVladimir Oltean 1434d1b15102SVladimir Oltean static void enetc_build_xdp_buff(struct enetc_bdr *rx_ring, u32 bd_status, 1435d1b15102SVladimir Oltean union enetc_rx_bd **rxbd, int *i, 1436d1b15102SVladimir Oltean int *cleaned_cnt, struct xdp_buff *xdp_buff) 1437d1b15102SVladimir Oltean { 1438d1b15102SVladimir Oltean u16 size = le16_to_cpu((*rxbd)->r.buf_len); 1439d1b15102SVladimir Oltean 1440d1b15102SVladimir Oltean xdp_init_buff(xdp_buff, ENETC_RXB_TRUESIZE, &rx_ring->xdp.rxq); 1441d1b15102SVladimir Oltean 1442d1b15102SVladimir Oltean enetc_map_rx_buff_to_xdp(rx_ring, *i, xdp_buff, size); 1443d1b15102SVladimir Oltean (*cleaned_cnt)++; 1444d1b15102SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1445d1b15102SVladimir Oltean 1446d1b15102SVladimir Oltean /* not last BD in frame? */ 1447d1b15102SVladimir Oltean while (!(bd_status & ENETC_RXBD_LSTATUS_F)) { 1448d1b15102SVladimir Oltean bd_status = le32_to_cpu((*rxbd)->r.lstatus); 1449d1b15102SVladimir Oltean size = ENETC_RXB_DMA_SIZE_XDP; 1450d1b15102SVladimir Oltean 1451d1b15102SVladimir Oltean if (bd_status & ENETC_RXBD_LSTATUS_F) { 1452d1b15102SVladimir Oltean dma_rmb(); 1453d1b15102SVladimir Oltean size = le16_to_cpu((*rxbd)->r.buf_len); 1454d1b15102SVladimir Oltean } 1455d1b15102SVladimir Oltean 1456d1b15102SVladimir Oltean enetc_add_rx_buff_to_xdp(rx_ring, *i, size, xdp_buff); 1457d1b15102SVladimir Oltean (*cleaned_cnt)++; 1458d1b15102SVladimir Oltean enetc_rxbd_next(rx_ring, rxbd, i); 1459d1b15102SVladimir Oltean } 1460d1b15102SVladimir Oltean } 1461d1b15102SVladimir Oltean 14627ed2bc80SVladimir Oltean /* Convert RX buffer descriptors to TX buffer descriptors. These will be 146392ff9a6eSVladimir Oltean * recycled back into the RX ring in enetc_clean_tx_ring. 14647ed2bc80SVladimir Oltean */ 14657ed2bc80SVladimir Oltean static int enetc_rx_swbd_to_xdp_tx_swbd(struct enetc_tx_swbd *xdp_tx_arr, 14667ed2bc80SVladimir Oltean struct enetc_bdr *rx_ring, 14677ed2bc80SVladimir Oltean int rx_ring_first, int rx_ring_last) 14687ed2bc80SVladimir Oltean { 14697ed2bc80SVladimir Oltean int n = 0; 14707ed2bc80SVladimir Oltean 14717ed2bc80SVladimir Oltean for (; rx_ring_first != rx_ring_last; 14727ed2bc80SVladimir Oltean n++, enetc_bdr_idx_inc(rx_ring, &rx_ring_first)) { 14737ed2bc80SVladimir Oltean struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[rx_ring_first]; 14747ed2bc80SVladimir Oltean struct enetc_tx_swbd *tx_swbd = &xdp_tx_arr[n]; 14757ed2bc80SVladimir Oltean 14767ed2bc80SVladimir Oltean /* No need to dma_map, we already have DMA_BIDIRECTIONAL */ 14777ed2bc80SVladimir Oltean tx_swbd->dma = rx_swbd->dma; 14787ed2bc80SVladimir Oltean tx_swbd->dir = rx_swbd->dir; 14797ed2bc80SVladimir Oltean tx_swbd->page = rx_swbd->page; 14807ed2bc80SVladimir Oltean tx_swbd->page_offset = rx_swbd->page_offset; 14817ed2bc80SVladimir Oltean tx_swbd->len = rx_swbd->len; 14827ed2bc80SVladimir Oltean tx_swbd->is_dma_page = true; 14837ed2bc80SVladimir Oltean tx_swbd->is_xdp_tx = true; 14847ed2bc80SVladimir Oltean tx_swbd->is_eof = false; 14857ed2bc80SVladimir Oltean } 14867ed2bc80SVladimir Oltean 14877ed2bc80SVladimir Oltean /* We rely on caller providing an rx_ring_last > rx_ring_first */ 14887ed2bc80SVladimir Oltean xdp_tx_arr[n - 1].is_eof = true; 14897ed2bc80SVladimir Oltean 14907ed2bc80SVladimir Oltean return n; 14917ed2bc80SVladimir Oltean } 14927ed2bc80SVladimir Oltean 1493d1b15102SVladimir Oltean static void enetc_xdp_drop(struct enetc_bdr *rx_ring, int rx_ring_first, 1494d1b15102SVladimir Oltean int rx_ring_last) 1495d1b15102SVladimir Oltean { 1496d1b15102SVladimir Oltean while (rx_ring_first != rx_ring_last) { 14976b04830dSVladimir Oltean enetc_put_rx_buff(rx_ring, 1498d1b15102SVladimir Oltean &rx_ring->rx_swbd[rx_ring_first]); 1499d1b15102SVladimir Oltean enetc_bdr_idx_inc(rx_ring, &rx_ring_first); 1500d1b15102SVladimir Oltean } 1501d1b15102SVladimir Oltean rx_ring->stats.xdp_drops++; 1502d1b15102SVladimir Oltean } 1503d1b15102SVladimir Oltean 1504d1b15102SVladimir Oltean static int enetc_clean_rx_ring_xdp(struct enetc_bdr *rx_ring, 1505d1b15102SVladimir Oltean struct napi_struct *napi, int work_limit, 1506d1b15102SVladimir Oltean struct bpf_prog *prog) 1507d1b15102SVladimir Oltean { 15089d2b68ccSVladimir Oltean int xdp_tx_bd_cnt, xdp_tx_frm_cnt = 0, xdp_redirect_frm_cnt = 0; 15097ed2bc80SVladimir Oltean struct enetc_tx_swbd xdp_tx_arr[ENETC_MAX_SKB_FRAGS] = {0}; 15107ed2bc80SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(rx_ring->ndev); 1511d1b15102SVladimir Oltean int rx_frm_cnt = 0, rx_byte_cnt = 0; 15127eab503bSVladimir Oltean struct enetc_bdr *tx_ring; 1513d1b15102SVladimir Oltean int cleaned_cnt, i; 1514d1b15102SVladimir Oltean u32 xdp_act; 1515d1b15102SVladimir Oltean 1516d1b15102SVladimir Oltean cleaned_cnt = enetc_bd_unused(rx_ring); 1517d1b15102SVladimir Oltean /* next descriptor to process */ 1518d1b15102SVladimir Oltean i = rx_ring->next_to_clean; 1519d1b15102SVladimir Oltean 1520d1b15102SVladimir Oltean while (likely(rx_frm_cnt < work_limit)) { 1521d1b15102SVladimir Oltean union enetc_rx_bd *rxbd, *orig_rxbd; 1522d1b15102SVladimir Oltean int orig_i, orig_cleaned_cnt; 1523d1b15102SVladimir Oltean struct xdp_buff xdp_buff; 1524d1b15102SVladimir Oltean struct sk_buff *skb; 1525d1b15102SVladimir Oltean u32 bd_status; 1526628050ecSVladimir Oltean int err; 1527d1b15102SVladimir Oltean 1528d1b15102SVladimir Oltean rxbd = enetc_rxbd(rx_ring, i); 1529d1b15102SVladimir Oltean bd_status = le32_to_cpu(rxbd->r.lstatus); 1530d1b15102SVladimir Oltean if (!bd_status) 1531d1b15102SVladimir Oltean break; 1532d1b15102SVladimir Oltean 1533d1b15102SVladimir Oltean enetc_wr_reg_hot(rx_ring->idr, BIT(rx_ring->index)); 1534d1b15102SVladimir Oltean dma_rmb(); /* for reading other rxbd fields */ 1535d1b15102SVladimir Oltean 1536d1b15102SVladimir Oltean if (enetc_check_bd_errors_and_consume(rx_ring, bd_status, 1537d1b15102SVladimir Oltean &rxbd, &i)) 1538d1b15102SVladimir Oltean break; 1539d1b15102SVladimir Oltean 1540d1b15102SVladimir Oltean orig_rxbd = rxbd; 1541d1b15102SVladimir Oltean orig_cleaned_cnt = cleaned_cnt; 1542d1b15102SVladimir Oltean orig_i = i; 1543d1b15102SVladimir Oltean 1544d1b15102SVladimir Oltean enetc_build_xdp_buff(rx_ring, bd_status, &rxbd, &i, 1545d1b15102SVladimir Oltean &cleaned_cnt, &xdp_buff); 1546d1b15102SVladimir Oltean 1547d1b15102SVladimir Oltean xdp_act = bpf_prog_run_xdp(prog, &xdp_buff); 1548d1b15102SVladimir Oltean 1549d1b15102SVladimir Oltean switch (xdp_act) { 1550975acc83SVladimir Oltean default: 1551c8064e5bSPaolo Abeni bpf_warn_invalid_xdp_action(rx_ring->ndev, prog, xdp_act); 1552975acc83SVladimir Oltean fallthrough; 1553d1b15102SVladimir Oltean case XDP_ABORTED: 1554d1b15102SVladimir Oltean trace_xdp_exception(rx_ring->ndev, prog, xdp_act); 1555d1b15102SVladimir Oltean fallthrough; 1556d1b15102SVladimir Oltean case XDP_DROP: 1557d1b15102SVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 1558d1b15102SVladimir Oltean break; 1559d1b15102SVladimir Oltean case XDP_PASS: 1560d1b15102SVladimir Oltean rxbd = orig_rxbd; 1561d1b15102SVladimir Oltean cleaned_cnt = orig_cleaned_cnt; 1562d1b15102SVladimir Oltean i = orig_i; 1563d1b15102SVladimir Oltean 1564d1b15102SVladimir Oltean skb = enetc_build_skb(rx_ring, bd_status, &rxbd, 1565d1b15102SVladimir Oltean &i, &cleaned_cnt, 1566d1b15102SVladimir Oltean ENETC_RXB_DMA_SIZE_XDP); 1567d1b15102SVladimir Oltean if (unlikely(!skb)) 15688f50d8bbSVladimir Oltean goto out; 1569d1b15102SVladimir Oltean 1570d1b15102SVladimir Oltean napi_gro_receive(napi, skb); 1571d1b15102SVladimir Oltean break; 15727ed2bc80SVladimir Oltean case XDP_TX: 15737eab503bSVladimir Oltean tx_ring = priv->xdp_tx_ring[rx_ring->index]; 15747ed2bc80SVladimir Oltean xdp_tx_bd_cnt = enetc_rx_swbd_to_xdp_tx_swbd(xdp_tx_arr, 15757ed2bc80SVladimir Oltean rx_ring, 15767ed2bc80SVladimir Oltean orig_i, i); 15777ed2bc80SVladimir Oltean 15787ed2bc80SVladimir Oltean if (!enetc_xdp_tx(tx_ring, xdp_tx_arr, xdp_tx_bd_cnt)) { 15797ed2bc80SVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 15807ed2bc80SVladimir Oltean tx_ring->stats.xdp_tx_drops++; 15817ed2bc80SVladimir Oltean } else { 15827ed2bc80SVladimir Oltean tx_ring->stats.xdp_tx += xdp_tx_bd_cnt; 15837ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight += xdp_tx_bd_cnt; 15847ed2bc80SVladimir Oltean xdp_tx_frm_cnt++; 158592ff9a6eSVladimir Oltean /* The XDP_TX enqueue was successful, so we 158692ff9a6eSVladimir Oltean * need to scrub the RX software BDs because 158792ff9a6eSVladimir Oltean * the ownership of the buffers no longer 158892ff9a6eSVladimir Oltean * belongs to the RX ring, and we must prevent 158992ff9a6eSVladimir Oltean * enetc_refill_rx_ring() from reusing 159092ff9a6eSVladimir Oltean * rx_swbd->page. 159192ff9a6eSVladimir Oltean */ 159292ff9a6eSVladimir Oltean while (orig_i != i) { 159392ff9a6eSVladimir Oltean rx_ring->rx_swbd[orig_i].page = NULL; 159492ff9a6eSVladimir Oltean enetc_bdr_idx_inc(rx_ring, &orig_i); 159592ff9a6eSVladimir Oltean } 15967ed2bc80SVladimir Oltean } 15977ed2bc80SVladimir Oltean break; 15989d2b68ccSVladimir Oltean case XDP_REDIRECT: 1599628050ecSVladimir Oltean err = xdp_do_redirect(rx_ring->ndev, &xdp_buff, prog); 1600628050ecSVladimir Oltean if (unlikely(err)) { 1601628050ecSVladimir Oltean enetc_xdp_drop(rx_ring, orig_i, i); 1602628050ecSVladimir Oltean rx_ring->stats.xdp_redirect_failures++; 1603628050ecSVladimir Oltean } else { 16049d2b68ccSVladimir Oltean while (orig_i != i) { 16056b04830dSVladimir Oltean enetc_flip_rx_buff(rx_ring, 16069d2b68ccSVladimir Oltean &rx_ring->rx_swbd[orig_i]); 16079d2b68ccSVladimir Oltean enetc_bdr_idx_inc(rx_ring, &orig_i); 16089d2b68ccSVladimir Oltean } 16099d2b68ccSVladimir Oltean xdp_redirect_frm_cnt++; 16109d2b68ccSVladimir Oltean rx_ring->stats.xdp_redirect++; 16119d2b68ccSVladimir Oltean } 1612d1b15102SVladimir Oltean } 1613d1b15102SVladimir Oltean 1614d1b15102SVladimir Oltean rx_frm_cnt++; 1615d1b15102SVladimir Oltean } 1616d1b15102SVladimir Oltean 16178f50d8bbSVladimir Oltean out: 1618d1b15102SVladimir Oltean rx_ring->next_to_clean = i; 1619d1b15102SVladimir Oltean 1620d1b15102SVladimir Oltean rx_ring->stats.packets += rx_frm_cnt; 1621d1b15102SVladimir Oltean rx_ring->stats.bytes += rx_byte_cnt; 1622d1b15102SVladimir Oltean 16239d2b68ccSVladimir Oltean if (xdp_redirect_frm_cnt) 16249d2b68ccSVladimir Oltean xdp_do_flush_map(); 16259d2b68ccSVladimir Oltean 16267ed2bc80SVladimir Oltean if (xdp_tx_frm_cnt) 16277ed2bc80SVladimir Oltean enetc_update_tx_ring_tail(tx_ring); 16287ed2bc80SVladimir Oltean 16297ed2bc80SVladimir Oltean if (cleaned_cnt > rx_ring->xdp.xdp_tx_in_flight) 16307ed2bc80SVladimir Oltean enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring) - 16317ed2bc80SVladimir Oltean rx_ring->xdp.xdp_tx_in_flight); 16327ed2bc80SVladimir Oltean 1633d1b15102SVladimir Oltean return rx_frm_cnt; 1634d1b15102SVladimir Oltean } 1635d1b15102SVladimir Oltean 16368580b3c3SVladimir Oltean static int enetc_poll(struct napi_struct *napi, int budget) 16378580b3c3SVladimir Oltean { 16388580b3c3SVladimir Oltean struct enetc_int_vector 16398580b3c3SVladimir Oltean *v = container_of(napi, struct enetc_int_vector, napi); 1640d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 1641d1b15102SVladimir Oltean struct bpf_prog *prog; 16428580b3c3SVladimir Oltean bool complete = true; 16438580b3c3SVladimir Oltean int work_done; 16448580b3c3SVladimir Oltean int i; 16458580b3c3SVladimir Oltean 16468580b3c3SVladimir Oltean enetc_lock_mdio(); 16478580b3c3SVladimir Oltean 16488580b3c3SVladimir Oltean for (i = 0; i < v->count_tx_rings; i++) 16498580b3c3SVladimir Oltean if (!enetc_clean_tx_ring(&v->tx_ring[i], budget)) 16508580b3c3SVladimir Oltean complete = false; 16518580b3c3SVladimir Oltean 1652d1b15102SVladimir Oltean prog = rx_ring->xdp.prog; 1653d1b15102SVladimir Oltean if (prog) 1654d1b15102SVladimir Oltean work_done = enetc_clean_rx_ring_xdp(rx_ring, napi, budget, prog); 1655d1b15102SVladimir Oltean else 1656d1b15102SVladimir Oltean work_done = enetc_clean_rx_ring(rx_ring, napi, budget); 16578580b3c3SVladimir Oltean if (work_done == budget) 16588580b3c3SVladimir Oltean complete = false; 16598580b3c3SVladimir Oltean if (work_done) 16608580b3c3SVladimir Oltean v->rx_napi_work = true; 16618580b3c3SVladimir Oltean 16628580b3c3SVladimir Oltean if (!complete) { 16638580b3c3SVladimir Oltean enetc_unlock_mdio(); 16648580b3c3SVladimir Oltean return budget; 16658580b3c3SVladimir Oltean } 16668580b3c3SVladimir Oltean 16678580b3c3SVladimir Oltean napi_complete_done(napi, work_done); 16688580b3c3SVladimir Oltean 16698580b3c3SVladimir Oltean if (likely(v->rx_dim_en)) 16708580b3c3SVladimir Oltean enetc_rx_net_dim(v); 16718580b3c3SVladimir Oltean 16728580b3c3SVladimir Oltean v->rx_napi_work = false; 16738580b3c3SVladimir Oltean 16748580b3c3SVladimir Oltean /* enable interrupts */ 16758580b3c3SVladimir Oltean enetc_wr_reg_hot(v->rbier, ENETC_RBIER_RXTIE); 16768580b3c3SVladimir Oltean 16778580b3c3SVladimir Oltean for_each_set_bit(i, &v->tx_rings_map, ENETC_MAX_NUM_TXQS) 16788580b3c3SVladimir Oltean enetc_wr_reg_hot(v->tbier_base + ENETC_BDR_OFF(i), 16798580b3c3SVladimir Oltean ENETC_TBIER_TXTIE); 16808580b3c3SVladimir Oltean 16818580b3c3SVladimir Oltean enetc_unlock_mdio(); 16828580b3c3SVladimir Oltean 16838580b3c3SVladimir Oltean return work_done; 16848580b3c3SVladimir Oltean } 16858580b3c3SVladimir Oltean 1686d4fd0404SClaudiu Manoil /* Probing and Init */ 1687d382563fSClaudiu Manoil #define ENETC_MAX_RFS_SIZE 64 1688d4fd0404SClaudiu Manoil void enetc_get_si_caps(struct enetc_si *si) 1689d4fd0404SClaudiu Manoil { 1690d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 1691d4fd0404SClaudiu Manoil u32 val; 1692d4fd0404SClaudiu Manoil 1693d4fd0404SClaudiu Manoil /* find out how many of various resources we have to work with */ 1694d4fd0404SClaudiu Manoil val = enetc_rd(hw, ENETC_SICAPR0); 1695d4fd0404SClaudiu Manoil si->num_rx_rings = (val >> 16) & 0xff; 1696d4fd0404SClaudiu Manoil si->num_tx_rings = val & 0xff; 1697d382563fSClaudiu Manoil 1698d382563fSClaudiu Manoil val = enetc_rd(hw, ENETC_SIRFSCAPR); 1699d382563fSClaudiu Manoil si->num_fs_entries = ENETC_SIRFSCAPR_GET_NUM_RFS(val); 1700d382563fSClaudiu Manoil si->num_fs_entries = min(si->num_fs_entries, ENETC_MAX_RFS_SIZE); 1701d382563fSClaudiu Manoil 1702d382563fSClaudiu Manoil si->num_rss = 0; 1703d382563fSClaudiu Manoil val = enetc_rd(hw, ENETC_SIPCAPR0); 1704d382563fSClaudiu Manoil if (val & ENETC_SIPCAPR0_RSS) { 17052e47cb41SPo Liu u32 rss; 17062e47cb41SPo Liu 17072e47cb41SPo Liu rss = enetc_rd(hw, ENETC_SIRSSCAPR); 17082e47cb41SPo Liu si->num_rss = ENETC_SIRSSCAPR_GET_NUM_RSS(rss); 1709d382563fSClaudiu Manoil } 17102e47cb41SPo Liu 17112e47cb41SPo Liu if (val & ENETC_SIPCAPR0_QBV) 17122e47cb41SPo Liu si->hw_features |= ENETC_SI_F_QBV; 171379e49982SPo Liu 171479e49982SPo Liu if (val & ENETC_SIPCAPR0_PSFP) 171579e49982SPo Liu si->hw_features |= ENETC_SI_F_PSFP; 1716d4fd0404SClaudiu Manoil } 1717d4fd0404SClaudiu Manoil 1718f3ce29e1SVladimir Oltean static int enetc_dma_alloc_bdr(struct enetc_bdr_resource *res) 1719d4fd0404SClaudiu Manoil { 1720f3ce29e1SVladimir Oltean size_t bd_base_size = res->bd_count * res->bd_size; 1721f3ce29e1SVladimir Oltean 1722f3ce29e1SVladimir Oltean res->bd_base = dma_alloc_coherent(res->dev, bd_base_size, 1723f3ce29e1SVladimir Oltean &res->bd_dma_base, GFP_KERNEL); 1724f3ce29e1SVladimir Oltean if (!res->bd_base) 1725d4fd0404SClaudiu Manoil return -ENOMEM; 1726d4fd0404SClaudiu Manoil 1727d4fd0404SClaudiu Manoil /* h/w requires 128B alignment */ 1728f3ce29e1SVladimir Oltean if (!IS_ALIGNED(res->bd_dma_base, 128)) { 1729f3ce29e1SVladimir Oltean dma_free_coherent(res->dev, bd_base_size, res->bd_base, 1730f3ce29e1SVladimir Oltean res->bd_dma_base); 1731d4fd0404SClaudiu Manoil return -EINVAL; 1732d4fd0404SClaudiu Manoil } 1733d4fd0404SClaudiu Manoil 1734d4fd0404SClaudiu Manoil return 0; 1735d4fd0404SClaudiu Manoil } 1736d4fd0404SClaudiu Manoil 1737f3ce29e1SVladimir Oltean static void enetc_dma_free_bdr(const struct enetc_bdr_resource *res) 17380d6cfd0fSVladimir Oltean { 1739f3ce29e1SVladimir Oltean size_t bd_base_size = res->bd_count * res->bd_size; 1740f3ce29e1SVladimir Oltean 1741f3ce29e1SVladimir Oltean dma_free_coherent(res->dev, bd_base_size, res->bd_base, 1742f3ce29e1SVladimir Oltean res->bd_dma_base); 17430d6cfd0fSVladimir Oltean } 17440d6cfd0fSVladimir Oltean 1745f3ce29e1SVladimir Oltean static int enetc_alloc_tx_resource(struct enetc_bdr_resource *res, 1746f3ce29e1SVladimir Oltean struct device *dev, size_t bd_count) 1747d4fd0404SClaudiu Manoil { 1748d4fd0404SClaudiu Manoil int err; 1749d4fd0404SClaudiu Manoil 1750f3ce29e1SVladimir Oltean res->dev = dev; 1751f3ce29e1SVladimir Oltean res->bd_count = bd_count; 1752f3ce29e1SVladimir Oltean res->bd_size = sizeof(union enetc_tx_bd); 1753f3ce29e1SVladimir Oltean 1754f3ce29e1SVladimir Oltean res->tx_swbd = vzalloc(bd_count * sizeof(*res->tx_swbd)); 1755f3ce29e1SVladimir Oltean if (!res->tx_swbd) 1756d4fd0404SClaudiu Manoil return -ENOMEM; 1757d4fd0404SClaudiu Manoil 1758f3ce29e1SVladimir Oltean err = enetc_dma_alloc_bdr(res); 1759fb8629e2SIoana Ciornei if (err) 1760fb8629e2SIoana Ciornei goto err_alloc_bdr; 1761fb8629e2SIoana Ciornei 1762f3ce29e1SVladimir Oltean res->tso_headers = dma_alloc_coherent(dev, bd_count * TSO_HEADER_SIZE, 1763f3ce29e1SVladimir Oltean &res->tso_headers_dma, 1764fb8629e2SIoana Ciornei GFP_KERNEL); 1765f3ce29e1SVladimir Oltean if (!res->tso_headers) { 1766e79d8264SDan Carpenter err = -ENOMEM; 1767fb8629e2SIoana Ciornei goto err_alloc_tso; 1768e79d8264SDan Carpenter } 1769d4fd0404SClaudiu Manoil 1770d4fd0404SClaudiu Manoil return 0; 1771fb8629e2SIoana Ciornei 1772fb8629e2SIoana Ciornei err_alloc_tso: 1773f3ce29e1SVladimir Oltean enetc_dma_free_bdr(res); 1774fb8629e2SIoana Ciornei err_alloc_bdr: 1775f3ce29e1SVladimir Oltean vfree(res->tx_swbd); 1776f3ce29e1SVladimir Oltean res->tx_swbd = NULL; 1777fb8629e2SIoana Ciornei 1778fb8629e2SIoana Ciornei return err; 1779d4fd0404SClaudiu Manoil } 1780d4fd0404SClaudiu Manoil 1781f3ce29e1SVladimir Oltean static void enetc_free_tx_resource(const struct enetc_bdr_resource *res) 1782d4fd0404SClaudiu Manoil { 1783f3ce29e1SVladimir Oltean dma_free_coherent(res->dev, res->bd_count * TSO_HEADER_SIZE, 1784f3ce29e1SVladimir Oltean res->tso_headers, res->tso_headers_dma); 1785f3ce29e1SVladimir Oltean enetc_dma_free_bdr(res); 1786f3ce29e1SVladimir Oltean vfree(res->tx_swbd); 1787d4fd0404SClaudiu Manoil } 1788d4fd0404SClaudiu Manoil 1789f3ce29e1SVladimir Oltean static struct enetc_bdr_resource * 1790f3ce29e1SVladimir Oltean enetc_alloc_tx_resources(struct enetc_ndev_priv *priv) 1791d4fd0404SClaudiu Manoil { 1792f3ce29e1SVladimir Oltean struct enetc_bdr_resource *tx_res; 1793d4fd0404SClaudiu Manoil int i, err; 1794d4fd0404SClaudiu Manoil 1795f3ce29e1SVladimir Oltean tx_res = kcalloc(priv->num_tx_rings, sizeof(*tx_res), GFP_KERNEL); 1796f3ce29e1SVladimir Oltean if (!tx_res) 1797f3ce29e1SVladimir Oltean return ERR_PTR(-ENOMEM); 1798f3ce29e1SVladimir Oltean 1799d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 1800f3ce29e1SVladimir Oltean struct enetc_bdr *tx_ring = priv->tx_ring[i]; 1801d4fd0404SClaudiu Manoil 1802f3ce29e1SVladimir Oltean err = enetc_alloc_tx_resource(&tx_res[i], tx_ring->dev, 1803f3ce29e1SVladimir Oltean tx_ring->bd_count); 1804d4fd0404SClaudiu Manoil if (err) 1805d4fd0404SClaudiu Manoil goto fail; 1806d4fd0404SClaudiu Manoil } 1807d4fd0404SClaudiu Manoil 1808f3ce29e1SVladimir Oltean return tx_res; 1809d4fd0404SClaudiu Manoil 1810d4fd0404SClaudiu Manoil fail: 1811d4fd0404SClaudiu Manoil while (i-- > 0) 1812f3ce29e1SVladimir Oltean enetc_free_tx_resource(&tx_res[i]); 1813d4fd0404SClaudiu Manoil 1814f3ce29e1SVladimir Oltean kfree(tx_res); 1815f3ce29e1SVladimir Oltean 1816f3ce29e1SVladimir Oltean return ERR_PTR(err); 1817d4fd0404SClaudiu Manoil } 1818d4fd0404SClaudiu Manoil 1819f3ce29e1SVladimir Oltean static void enetc_free_tx_resources(const struct enetc_bdr_resource *tx_res, 1820f3ce29e1SVladimir Oltean size_t num_resources) 1821d4fd0404SClaudiu Manoil { 1822f3ce29e1SVladimir Oltean size_t i; 1823d4fd0404SClaudiu Manoil 1824f3ce29e1SVladimir Oltean for (i = 0; i < num_resources; i++) 1825f3ce29e1SVladimir Oltean enetc_free_tx_resource(&tx_res[i]); 1826f3ce29e1SVladimir Oltean 1827f3ce29e1SVladimir Oltean kfree(tx_res); 1828d4fd0404SClaudiu Manoil } 1829d4fd0404SClaudiu Manoil 1830f3ce29e1SVladimir Oltean static int enetc_alloc_rx_resource(struct enetc_bdr_resource *res, 1831f3ce29e1SVladimir Oltean struct device *dev, size_t bd_count, 1832f3ce29e1SVladimir Oltean bool extended) 1833d4fd0404SClaudiu Manoil { 1834d4fd0404SClaudiu Manoil int err; 1835d4fd0404SClaudiu Manoil 1836f3ce29e1SVladimir Oltean res->dev = dev; 1837f3ce29e1SVladimir Oltean res->bd_count = bd_count; 1838f3ce29e1SVladimir Oltean res->bd_size = sizeof(union enetc_rx_bd); 1839f3ce29e1SVladimir Oltean if (extended) 1840f3ce29e1SVladimir Oltean res->bd_size *= 2; 1841f3ce29e1SVladimir Oltean 1842f3ce29e1SVladimir Oltean res->rx_swbd = vzalloc(bd_count * sizeof(struct enetc_rx_swbd)); 1843f3ce29e1SVladimir Oltean if (!res->rx_swbd) 1844d4fd0404SClaudiu Manoil return -ENOMEM; 1845d4fd0404SClaudiu Manoil 1846f3ce29e1SVladimir Oltean err = enetc_dma_alloc_bdr(res); 1847d4fd0404SClaudiu Manoil if (err) { 1848f3ce29e1SVladimir Oltean vfree(res->rx_swbd); 1849d4fd0404SClaudiu Manoil return err; 1850d4fd0404SClaudiu Manoil } 1851d4fd0404SClaudiu Manoil 1852d4fd0404SClaudiu Manoil return 0; 1853d4fd0404SClaudiu Manoil } 1854d4fd0404SClaudiu Manoil 1855f3ce29e1SVladimir Oltean static void enetc_free_rx_resource(const struct enetc_bdr_resource *res) 1856d4fd0404SClaudiu Manoil { 1857f3ce29e1SVladimir Oltean enetc_dma_free_bdr(res); 1858f3ce29e1SVladimir Oltean vfree(res->rx_swbd); 1859d4fd0404SClaudiu Manoil } 1860d4fd0404SClaudiu Manoil 1861f3ce29e1SVladimir Oltean static struct enetc_bdr_resource * 1862f3ce29e1SVladimir Oltean enetc_alloc_rx_resources(struct enetc_ndev_priv *priv, bool extended) 1863d4fd0404SClaudiu Manoil { 1864f3ce29e1SVladimir Oltean struct enetc_bdr_resource *rx_res; 1865d4fd0404SClaudiu Manoil int i, err; 1866d4fd0404SClaudiu Manoil 1867f3ce29e1SVladimir Oltean rx_res = kcalloc(priv->num_rx_rings, sizeof(*rx_res), GFP_KERNEL); 1868f3ce29e1SVladimir Oltean if (!rx_res) 1869f3ce29e1SVladimir Oltean return ERR_PTR(-ENOMEM); 1870d4fd0404SClaudiu Manoil 1871f3ce29e1SVladimir Oltean for (i = 0; i < priv->num_rx_rings; i++) { 1872f3ce29e1SVladimir Oltean struct enetc_bdr *rx_ring = priv->rx_ring[i]; 1873f3ce29e1SVladimir Oltean 1874f3ce29e1SVladimir Oltean err = enetc_alloc_rx_resource(&rx_res[i], rx_ring->dev, 1875f3ce29e1SVladimir Oltean rx_ring->bd_count, extended); 1876d4fd0404SClaudiu Manoil if (err) 1877d4fd0404SClaudiu Manoil goto fail; 1878d4fd0404SClaudiu Manoil } 1879d4fd0404SClaudiu Manoil 1880f3ce29e1SVladimir Oltean return rx_res; 1881d4fd0404SClaudiu Manoil 1882d4fd0404SClaudiu Manoil fail: 1883d4fd0404SClaudiu Manoil while (i-- > 0) 1884f3ce29e1SVladimir Oltean enetc_free_rx_resource(&rx_res[i]); 1885d4fd0404SClaudiu Manoil 1886f3ce29e1SVladimir Oltean kfree(rx_res); 1887f3ce29e1SVladimir Oltean 1888f3ce29e1SVladimir Oltean return ERR_PTR(err); 1889d4fd0404SClaudiu Manoil } 1890d4fd0404SClaudiu Manoil 1891f3ce29e1SVladimir Oltean static void enetc_free_rx_resources(const struct enetc_bdr_resource *rx_res, 1892f3ce29e1SVladimir Oltean size_t num_resources) 1893f3ce29e1SVladimir Oltean { 1894f3ce29e1SVladimir Oltean size_t i; 1895f3ce29e1SVladimir Oltean 1896f3ce29e1SVladimir Oltean for (i = 0; i < num_resources; i++) 1897f3ce29e1SVladimir Oltean enetc_free_rx_resource(&rx_res[i]); 1898f3ce29e1SVladimir Oltean 1899f3ce29e1SVladimir Oltean kfree(rx_res); 1900f3ce29e1SVladimir Oltean } 1901f3ce29e1SVladimir Oltean 1902f3ce29e1SVladimir Oltean static void enetc_assign_tx_resource(struct enetc_bdr *tx_ring, 1903f3ce29e1SVladimir Oltean const struct enetc_bdr_resource *res) 1904f3ce29e1SVladimir Oltean { 1905f3ce29e1SVladimir Oltean tx_ring->bd_base = res ? res->bd_base : NULL; 1906f3ce29e1SVladimir Oltean tx_ring->bd_dma_base = res ? res->bd_dma_base : 0; 1907f3ce29e1SVladimir Oltean tx_ring->tx_swbd = res ? res->tx_swbd : NULL; 1908f3ce29e1SVladimir Oltean tx_ring->tso_headers = res ? res->tso_headers : NULL; 1909f3ce29e1SVladimir Oltean tx_ring->tso_headers_dma = res ? res->tso_headers_dma : 0; 1910f3ce29e1SVladimir Oltean } 1911f3ce29e1SVladimir Oltean 1912f3ce29e1SVladimir Oltean static void enetc_assign_rx_resource(struct enetc_bdr *rx_ring, 1913f3ce29e1SVladimir Oltean const struct enetc_bdr_resource *res) 1914f3ce29e1SVladimir Oltean { 1915f3ce29e1SVladimir Oltean rx_ring->bd_base = res ? res->bd_base : NULL; 1916f3ce29e1SVladimir Oltean rx_ring->bd_dma_base = res ? res->bd_dma_base : 0; 1917f3ce29e1SVladimir Oltean rx_ring->rx_swbd = res ? res->rx_swbd : NULL; 1918f3ce29e1SVladimir Oltean } 1919f3ce29e1SVladimir Oltean 1920f3ce29e1SVladimir Oltean static void enetc_assign_tx_resources(struct enetc_ndev_priv *priv, 1921f3ce29e1SVladimir Oltean const struct enetc_bdr_resource *res) 1922d4fd0404SClaudiu Manoil { 1923d4fd0404SClaudiu Manoil int i; 1924d4fd0404SClaudiu Manoil 1925f3ce29e1SVladimir Oltean if (priv->tx_res) 1926f3ce29e1SVladimir Oltean enetc_free_tx_resources(priv->tx_res, priv->num_tx_rings); 1927f3ce29e1SVladimir Oltean 1928f3ce29e1SVladimir Oltean for (i = 0; i < priv->num_tx_rings; i++) { 1929f3ce29e1SVladimir Oltean enetc_assign_tx_resource(priv->tx_ring[i], 1930f3ce29e1SVladimir Oltean res ? &res[i] : NULL); 1931f3ce29e1SVladimir Oltean } 1932f3ce29e1SVladimir Oltean 1933f3ce29e1SVladimir Oltean priv->tx_res = res; 1934f3ce29e1SVladimir Oltean } 1935f3ce29e1SVladimir Oltean 1936f3ce29e1SVladimir Oltean static void enetc_assign_rx_resources(struct enetc_ndev_priv *priv, 1937f3ce29e1SVladimir Oltean const struct enetc_bdr_resource *res) 1938f3ce29e1SVladimir Oltean { 1939f3ce29e1SVladimir Oltean int i; 1940f3ce29e1SVladimir Oltean 1941f3ce29e1SVladimir Oltean if (priv->rx_res) 1942f3ce29e1SVladimir Oltean enetc_free_rx_resources(priv->rx_res, priv->num_rx_rings); 1943f3ce29e1SVladimir Oltean 1944f3ce29e1SVladimir Oltean for (i = 0; i < priv->num_rx_rings; i++) { 1945f3ce29e1SVladimir Oltean enetc_assign_rx_resource(priv->rx_ring[i], 1946f3ce29e1SVladimir Oltean res ? &res[i] : NULL); 1947f3ce29e1SVladimir Oltean } 1948f3ce29e1SVladimir Oltean 1949f3ce29e1SVladimir Oltean priv->rx_res = res; 1950d4fd0404SClaudiu Manoil } 1951d4fd0404SClaudiu Manoil 1952d4fd0404SClaudiu Manoil static void enetc_free_tx_ring(struct enetc_bdr *tx_ring) 1953d4fd0404SClaudiu Manoil { 1954d4fd0404SClaudiu Manoil int i; 1955d4fd0404SClaudiu Manoil 1956d4fd0404SClaudiu Manoil for (i = 0; i < tx_ring->bd_count; i++) { 1957d4fd0404SClaudiu Manoil struct enetc_tx_swbd *tx_swbd = &tx_ring->tx_swbd[i]; 1958d4fd0404SClaudiu Manoil 19599d2b68ccSVladimir Oltean enetc_free_tx_frame(tx_ring, tx_swbd); 1960d4fd0404SClaudiu Manoil } 1961d4fd0404SClaudiu Manoil } 1962d4fd0404SClaudiu Manoil 1963d4fd0404SClaudiu Manoil static void enetc_free_rx_ring(struct enetc_bdr *rx_ring) 1964d4fd0404SClaudiu Manoil { 1965d4fd0404SClaudiu Manoil int i; 1966d4fd0404SClaudiu Manoil 1967d4fd0404SClaudiu Manoil for (i = 0; i < rx_ring->bd_count; i++) { 1968d4fd0404SClaudiu Manoil struct enetc_rx_swbd *rx_swbd = &rx_ring->rx_swbd[i]; 1969d4fd0404SClaudiu Manoil 1970d4fd0404SClaudiu Manoil if (!rx_swbd->page) 1971d4fd0404SClaudiu Manoil continue; 1972d4fd0404SClaudiu Manoil 19737ed2bc80SVladimir Oltean dma_unmap_page(rx_ring->dev, rx_swbd->dma, PAGE_SIZE, 19747ed2bc80SVladimir Oltean rx_swbd->dir); 1975d4fd0404SClaudiu Manoil __free_page(rx_swbd->page); 1976d4fd0404SClaudiu Manoil rx_swbd->page = NULL; 1977d4fd0404SClaudiu Manoil } 1978d4fd0404SClaudiu Manoil } 1979d4fd0404SClaudiu Manoil 1980d4fd0404SClaudiu Manoil static void enetc_free_rxtx_rings(struct enetc_ndev_priv *priv) 1981d4fd0404SClaudiu Manoil { 1982d4fd0404SClaudiu Manoil int i; 1983d4fd0404SClaudiu Manoil 1984d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 1985d4fd0404SClaudiu Manoil enetc_free_rx_ring(priv->rx_ring[i]); 1986d4fd0404SClaudiu Manoil 1987d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 1988d4fd0404SClaudiu Manoil enetc_free_tx_ring(priv->tx_ring[i]); 1989d4fd0404SClaudiu Manoil } 1990d4fd0404SClaudiu Manoil 1991d382563fSClaudiu Manoil static int enetc_setup_default_rss_table(struct enetc_si *si, int num_groups) 1992d382563fSClaudiu Manoil { 1993d382563fSClaudiu Manoil int *rss_table; 1994d382563fSClaudiu Manoil int i; 1995d382563fSClaudiu Manoil 1996d382563fSClaudiu Manoil rss_table = kmalloc_array(si->num_rss, sizeof(*rss_table), GFP_KERNEL); 1997d382563fSClaudiu Manoil if (!rss_table) 1998d382563fSClaudiu Manoil return -ENOMEM; 1999d382563fSClaudiu Manoil 2000d382563fSClaudiu Manoil /* Set up RSS table defaults */ 2001d382563fSClaudiu Manoil for (i = 0; i < si->num_rss; i++) 2002d382563fSClaudiu Manoil rss_table[i] = i % num_groups; 2003d382563fSClaudiu Manoil 2004d382563fSClaudiu Manoil enetc_set_rss_table(si, rss_table, si->num_rss); 2005d382563fSClaudiu Manoil 2006d382563fSClaudiu Manoil kfree(rss_table); 2007d382563fSClaudiu Manoil 2008d382563fSClaudiu Manoil return 0; 2009d382563fSClaudiu Manoil } 2010d382563fSClaudiu Manoil 2011c646d10dSVladimir Oltean int enetc_configure_si(struct enetc_ndev_priv *priv) 2012d4fd0404SClaudiu Manoil { 2013d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 2014d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 2015d382563fSClaudiu Manoil int err; 2016d4fd0404SClaudiu Manoil 2017d4fd0404SClaudiu Manoil /* set SI cache attributes */ 2018d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SICAR0, 2019d4fd0404SClaudiu Manoil ENETC_SICAR_RD_COHERENT | ENETC_SICAR_WR_COHERENT); 2020d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SICAR1, ENETC_SICAR_MSI); 2021d4fd0404SClaudiu Manoil /* enable SI */ 2022d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMR, ENETC_SIMR_EN); 2023d4fd0404SClaudiu Manoil 2024d382563fSClaudiu Manoil if (si->num_rss) { 2025d382563fSClaudiu Manoil err = enetc_setup_default_rss_table(si, priv->num_rx_rings); 2026d382563fSClaudiu Manoil if (err) 2027d382563fSClaudiu Manoil return err; 2028d382563fSClaudiu Manoil } 2029d382563fSClaudiu Manoil 2030d4fd0404SClaudiu Manoil return 0; 2031d4fd0404SClaudiu Manoil } 2032d4fd0404SClaudiu Manoil 2033d4fd0404SClaudiu Manoil void enetc_init_si_rings_params(struct enetc_ndev_priv *priv) 2034d4fd0404SClaudiu Manoil { 2035d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 2036d4fd0404SClaudiu Manoil int cpus = num_online_cpus(); 2037d4fd0404SClaudiu Manoil 203802293dd4SClaudiu Manoil priv->tx_bd_count = ENETC_TX_RING_DEFAULT_SIZE; 203902293dd4SClaudiu Manoil priv->rx_bd_count = ENETC_RX_RING_DEFAULT_SIZE; 2040d4fd0404SClaudiu Manoil 2041d4fd0404SClaudiu Manoil /* Enable all available TX rings in order to configure as many 2042d4fd0404SClaudiu Manoil * priorities as possible, when needed. 2043d4fd0404SClaudiu Manoil * TODO: Make # of TX rings run-time configurable 2044d4fd0404SClaudiu Manoil */ 2045d4fd0404SClaudiu Manoil priv->num_rx_rings = min_t(int, cpus, si->num_rx_rings); 2046d4fd0404SClaudiu Manoil priv->num_tx_rings = si->num_tx_rings; 2047d4fd0404SClaudiu Manoil priv->bdr_int_num = cpus; 2048ae0e6a5dSClaudiu Manoil priv->ic_mode = ENETC_IC_RX_ADAPTIVE | ENETC_IC_TX_MANUAL; 2049ae0e6a5dSClaudiu Manoil priv->tx_ictt = ENETC_TXIC_TIMETHR; 2050d4fd0404SClaudiu Manoil } 2051d4fd0404SClaudiu Manoil 2052d4fd0404SClaudiu Manoil int enetc_alloc_si_resources(struct enetc_ndev_priv *priv) 2053d4fd0404SClaudiu Manoil { 2054d4fd0404SClaudiu Manoil struct enetc_si *si = priv->si; 2055d4fd0404SClaudiu Manoil 2056d382563fSClaudiu Manoil priv->cls_rules = kcalloc(si->num_fs_entries, sizeof(*priv->cls_rules), 2057d382563fSClaudiu Manoil GFP_KERNEL); 20584b47c0b8SVladimir Oltean if (!priv->cls_rules) 20594b47c0b8SVladimir Oltean return -ENOMEM; 2060d382563fSClaudiu Manoil 2061d4fd0404SClaudiu Manoil return 0; 2062d4fd0404SClaudiu Manoil } 2063d4fd0404SClaudiu Manoil 2064d4fd0404SClaudiu Manoil void enetc_free_si_resources(struct enetc_ndev_priv *priv) 2065d4fd0404SClaudiu Manoil { 2066d382563fSClaudiu Manoil kfree(priv->cls_rules); 2067d4fd0404SClaudiu Manoil } 2068d4fd0404SClaudiu Manoil 2069d4fd0404SClaudiu Manoil static void enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring) 2070d4fd0404SClaudiu Manoil { 2071d4fd0404SClaudiu Manoil int idx = tx_ring->index; 2072d4fd0404SClaudiu Manoil u32 tbmr; 2073d4fd0404SClaudiu Manoil 2074d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBBAR0, 2075d4fd0404SClaudiu Manoil lower_32_bits(tx_ring->bd_dma_base)); 2076d4fd0404SClaudiu Manoil 2077d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBBAR1, 2078d4fd0404SClaudiu Manoil upper_32_bits(tx_ring->bd_dma_base)); 2079d4fd0404SClaudiu Manoil 2080d4fd0404SClaudiu Manoil WARN_ON(!IS_ALIGNED(tx_ring->bd_count, 64)); /* multiple of 64 */ 2081d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBLENR, 2082d4fd0404SClaudiu Manoil ENETC_RTBLENR_LEN(tx_ring->bd_count)); 2083d4fd0404SClaudiu Manoil 2084d4fd0404SClaudiu Manoil /* clearing PI/CI registers for Tx not supported, adjust sw indexes */ 2085d4fd0404SClaudiu Manoil tx_ring->next_to_use = enetc_txbdr_rd(hw, idx, ENETC_TBPIR); 2086d4fd0404SClaudiu Manoil tx_ring->next_to_clean = enetc_txbdr_rd(hw, idx, ENETC_TBCIR); 2087d4fd0404SClaudiu Manoil 2088d4fd0404SClaudiu Manoil /* enable Tx ints by setting pkt thr to 1 */ 208912460a0aSClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBICR0, ENETC_TBICR0_ICEN | 0x1); 2090d4fd0404SClaudiu Manoil 2091290b5fe0SVladimir Oltean tbmr = ENETC_TBMR_EN | ENETC_TBMR_SET_PRIO(tx_ring->prio); 2092d4fd0404SClaudiu Manoil if (tx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_TX) 2093d4fd0404SClaudiu Manoil tbmr |= ENETC_TBMR_VIH; 2094d4fd0404SClaudiu Manoil 2095d4fd0404SClaudiu Manoil /* enable ring */ 2096d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr); 2097d4fd0404SClaudiu Manoil 2098d4fd0404SClaudiu Manoil tx_ring->tpir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBPIR); 2099d4fd0404SClaudiu Manoil tx_ring->tcir = hw->reg + ENETC_BDR(TX, idx, ENETC_TBCIR); 2100d4fd0404SClaudiu Manoil tx_ring->idr = hw->reg + ENETC_SITXIDR; 2101d4fd0404SClaudiu Manoil } 2102d4fd0404SClaudiu Manoil 2103d075db51SVladimir Oltean static void enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring, 2104d075db51SVladimir Oltean bool extended) 2105d4fd0404SClaudiu Manoil { 2106d4fd0404SClaudiu Manoil int idx = rx_ring->index; 2107d4fd0404SClaudiu Manoil u32 rbmr; 2108d4fd0404SClaudiu Manoil 2109d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0, 2110d4fd0404SClaudiu Manoil lower_32_bits(rx_ring->bd_dma_base)); 2111d4fd0404SClaudiu Manoil 2112d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1, 2113d4fd0404SClaudiu Manoil upper_32_bits(rx_ring->bd_dma_base)); 2114d4fd0404SClaudiu Manoil 2115d4fd0404SClaudiu Manoil WARN_ON(!IS_ALIGNED(rx_ring->bd_count, 64)); /* multiple of 64 */ 2116d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBLENR, 2117d4fd0404SClaudiu Manoil ENETC_RTBLENR_LEN(rx_ring->bd_count)); 2118d4fd0404SClaudiu Manoil 2119d1b15102SVladimir Oltean if (rx_ring->xdp.prog) 2120d1b15102SVladimir Oltean enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE_XDP); 2121d1b15102SVladimir Oltean else 2122d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, ENETC_RXB_DMA_SIZE); 2123d4fd0404SClaudiu Manoil 212484ce1ca3SVladimir Oltean /* Also prepare the consumer index in case page allocation never 212584ce1ca3SVladimir Oltean * succeeds. In that case, hardware will never advance producer index 212684ce1ca3SVladimir Oltean * to match consumer index, and will drop all frames. 212784ce1ca3SVladimir Oltean */ 2128d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0); 212984ce1ca3SVladimir Oltean enetc_rxbdr_wr(hw, idx, ENETC_RBCIR, 1); 2130d4fd0404SClaudiu Manoil 2131d4fd0404SClaudiu Manoil /* enable Rx ints by setting pkt thr to 1 */ 213212460a0aSClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBICR0, ENETC_RBICR0_ICEN | 0x1); 2133d4fd0404SClaudiu Manoil 2134d4fd0404SClaudiu Manoil rbmr = ENETC_RBMR_EN; 2135434cebabSClaudiu Manoil 2136d075db51SVladimir Oltean rx_ring->ext_en = extended; 2137434cebabSClaudiu Manoil if (rx_ring->ext_en) 2138d3982312SY.b. Lu rbmr |= ENETC_RBMR_BDS; 2139434cebabSClaudiu Manoil 2140d4fd0404SClaudiu Manoil if (rx_ring->ndev->features & NETIF_F_HW_VLAN_CTAG_RX) 2141d4fd0404SClaudiu Manoil rbmr |= ENETC_RBMR_VTE; 2142d4fd0404SClaudiu Manoil 2143d4fd0404SClaudiu Manoil rx_ring->rcir = hw->reg + ENETC_BDR(RX, idx, ENETC_RBCIR); 2144d4fd0404SClaudiu Manoil rx_ring->idr = hw->reg + ENETC_SIRXIDR; 2145d4fd0404SClaudiu Manoil 2146fbf1cff9SVladimir Oltean rx_ring->next_to_clean = 0; 2147fbf1cff9SVladimir Oltean rx_ring->next_to_use = 0; 2148fbf1cff9SVladimir Oltean rx_ring->next_to_alloc = 0; 2149fbf1cff9SVladimir Oltean 21507a5222cbSVladimir Oltean enetc_lock_mdio(); 2151d4fd0404SClaudiu Manoil enetc_refill_rx_ring(rx_ring, enetc_bd_unused(rx_ring)); 21527a5222cbSVladimir Oltean enetc_unlock_mdio(); 2153d4fd0404SClaudiu Manoil 2154d4fd0404SClaudiu Manoil /* enable ring */ 2155d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBMR, rbmr); 2156d4fd0404SClaudiu Manoil } 2157d4fd0404SClaudiu Manoil 2158d075db51SVladimir Oltean static void enetc_setup_bdrs(struct enetc_ndev_priv *priv, bool extended) 2159d4fd0404SClaudiu Manoil { 2160715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 2161d4fd0404SClaudiu Manoil int i; 2162d4fd0404SClaudiu Manoil 2163d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2164715bf261SVladimir Oltean enetc_setup_txbdr(hw, priv->tx_ring[i]); 2165d4fd0404SClaudiu Manoil 2166d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2167d075db51SVladimir Oltean enetc_setup_rxbdr(hw, priv->rx_ring[i], extended); 2168d4fd0404SClaudiu Manoil } 2169d4fd0404SClaudiu Manoil 2170d4fd0404SClaudiu Manoil static void enetc_clear_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring) 2171d4fd0404SClaudiu Manoil { 2172d4fd0404SClaudiu Manoil int idx = rx_ring->index; 2173d4fd0404SClaudiu Manoil 2174d4fd0404SClaudiu Manoil /* disable EN bit on ring */ 2175d4fd0404SClaudiu Manoil enetc_rxbdr_wr(hw, idx, ENETC_RBMR, 0); 2176d4fd0404SClaudiu Manoil } 2177d4fd0404SClaudiu Manoil 2178d4fd0404SClaudiu Manoil static void enetc_clear_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring) 2179d4fd0404SClaudiu Manoil { 2180d4fd0404SClaudiu Manoil int delay = 8, timeout = 100; 2181d4fd0404SClaudiu Manoil int idx = tx_ring->index; 2182d4fd0404SClaudiu Manoil 2183d4fd0404SClaudiu Manoil /* disable EN bit on ring */ 2184d4fd0404SClaudiu Manoil enetc_txbdr_wr(hw, idx, ENETC_TBMR, 0); 2185d4fd0404SClaudiu Manoil 2186d4fd0404SClaudiu Manoil /* wait for busy to clear */ 2187d4fd0404SClaudiu Manoil while (delay < timeout && 2188d4fd0404SClaudiu Manoil enetc_txbdr_rd(hw, idx, ENETC_TBSR) & ENETC_TBSR_BUSY) { 2189d4fd0404SClaudiu Manoil msleep(delay); 2190d4fd0404SClaudiu Manoil delay *= 2; 2191d4fd0404SClaudiu Manoil } 2192d4fd0404SClaudiu Manoil 2193d4fd0404SClaudiu Manoil if (delay >= timeout) 2194d4fd0404SClaudiu Manoil netdev_warn(tx_ring->ndev, "timeout for tx ring #%d clear\n", 2195d4fd0404SClaudiu Manoil idx); 2196d4fd0404SClaudiu Manoil } 2197d4fd0404SClaudiu Manoil 2198d4fd0404SClaudiu Manoil static void enetc_clear_bdrs(struct enetc_ndev_priv *priv) 2199d4fd0404SClaudiu Manoil { 2200715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 2201d4fd0404SClaudiu Manoil int i; 2202d4fd0404SClaudiu Manoil 2203d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2204715bf261SVladimir Oltean enetc_clear_txbdr(hw, priv->tx_ring[i]); 2205d4fd0404SClaudiu Manoil 2206d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2207715bf261SVladimir Oltean enetc_clear_rxbdr(hw, priv->rx_ring[i]); 2208d4fd0404SClaudiu Manoil 2209d4fd0404SClaudiu Manoil udelay(1); 2210d4fd0404SClaudiu Manoil } 2211d4fd0404SClaudiu Manoil 2212d4fd0404SClaudiu Manoil static int enetc_setup_irqs(struct enetc_ndev_priv *priv) 2213d4fd0404SClaudiu Manoil { 2214d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 2215715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 2216d4fd0404SClaudiu Manoil int i, j, err; 2217d4fd0404SClaudiu Manoil 2218d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2219d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2220d4fd0404SClaudiu Manoil struct enetc_int_vector *v = priv->int_vector[i]; 2221d4fd0404SClaudiu Manoil int entry = ENETC_BDR_INT_BASE_IDX + i; 2222d4fd0404SClaudiu Manoil 2223d4fd0404SClaudiu Manoil snprintf(v->name, sizeof(v->name), "%s-rxtx%d", 2224d4fd0404SClaudiu Manoil priv->ndev->name, i); 2225d4fd0404SClaudiu Manoil err = request_irq(irq, enetc_msix, 0, v->name, v); 2226d4fd0404SClaudiu Manoil if (err) { 2227d4fd0404SClaudiu Manoil dev_err(priv->dev, "request_irq() failed!\n"); 2228d4fd0404SClaudiu Manoil goto irq_err; 2229d4fd0404SClaudiu Manoil } 2230bbb96dc7SClaudiu Manoil disable_irq(irq); 2231d4fd0404SClaudiu Manoil 2232d4fd0404SClaudiu Manoil v->tbier_base = hw->reg + ENETC_BDR(TX, 0, ENETC_TBIER); 2233d4fd0404SClaudiu Manoil v->rbier = hw->reg + ENETC_BDR(RX, i, ENETC_RBIER); 223491571081SClaudiu Manoil v->ricr1 = hw->reg + ENETC_BDR(RX, i, ENETC_RBICR1); 2235d4fd0404SClaudiu Manoil 2236d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMSIRRV(i), entry); 2237d4fd0404SClaudiu Manoil 2238d4fd0404SClaudiu Manoil for (j = 0; j < v->count_tx_rings; j++) { 2239d4fd0404SClaudiu Manoil int idx = v->tx_ring[j].index; 2240d4fd0404SClaudiu Manoil 2241d4fd0404SClaudiu Manoil enetc_wr(hw, ENETC_SIMSITRV(idx), entry); 2242d4fd0404SClaudiu Manoil } 22437237a494SClaudiu Manoil irq_set_affinity_hint(irq, get_cpu_mask(i % num_online_cpus())); 2244d4fd0404SClaudiu Manoil } 2245d4fd0404SClaudiu Manoil 2246d4fd0404SClaudiu Manoil return 0; 2247d4fd0404SClaudiu Manoil 2248d4fd0404SClaudiu Manoil irq_err: 2249d4fd0404SClaudiu Manoil while (i--) { 2250d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2251d4fd0404SClaudiu Manoil 2252d4fd0404SClaudiu Manoil irq_set_affinity_hint(irq, NULL); 2253d4fd0404SClaudiu Manoil free_irq(irq, priv->int_vector[i]); 2254d4fd0404SClaudiu Manoil } 2255d4fd0404SClaudiu Manoil 2256d4fd0404SClaudiu Manoil return err; 2257d4fd0404SClaudiu Manoil } 2258d4fd0404SClaudiu Manoil 2259d4fd0404SClaudiu Manoil static void enetc_free_irqs(struct enetc_ndev_priv *priv) 2260d4fd0404SClaudiu Manoil { 2261d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 2262d4fd0404SClaudiu Manoil int i; 2263d4fd0404SClaudiu Manoil 2264d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2265d4fd0404SClaudiu Manoil int irq = pci_irq_vector(pdev, ENETC_BDR_INT_BASE_IDX + i); 2266d4fd0404SClaudiu Manoil 2267d4fd0404SClaudiu Manoil irq_set_affinity_hint(irq, NULL); 2268d4fd0404SClaudiu Manoil free_irq(irq, priv->int_vector[i]); 2269d4fd0404SClaudiu Manoil } 2270d4fd0404SClaudiu Manoil } 2271d4fd0404SClaudiu Manoil 2272bbb96dc7SClaudiu Manoil static void enetc_setup_interrupts(struct enetc_ndev_priv *priv) 2273d4fd0404SClaudiu Manoil { 227491571081SClaudiu Manoil struct enetc_hw *hw = &priv->si->hw; 227591571081SClaudiu Manoil u32 icpt, ictt; 2276d4fd0404SClaudiu Manoil int i; 2277d4fd0404SClaudiu Manoil 2278d4fd0404SClaudiu Manoil /* enable Tx & Rx event indication */ 2279ae0e6a5dSClaudiu Manoil if (priv->ic_mode & 2280ae0e6a5dSClaudiu Manoil (ENETC_IC_RX_MANUAL | ENETC_IC_RX_ADAPTIVE)) { 228191571081SClaudiu Manoil icpt = ENETC_RBICR0_SET_ICPT(ENETC_RXIC_PKTTHR); 228291571081SClaudiu Manoil /* init to non-0 minimum, will be adjusted later */ 228391571081SClaudiu Manoil ictt = 0x1; 228491571081SClaudiu Manoil } else { 228591571081SClaudiu Manoil icpt = 0x1; /* enable Rx ints by setting pkt thr to 1 */ 228691571081SClaudiu Manoil ictt = 0; 2287d4fd0404SClaudiu Manoil } 2288d4fd0404SClaudiu Manoil 228991571081SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) { 229091571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBICR1, ictt); 229191571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBICR0, ENETC_RBICR0_ICEN | icpt); 229291571081SClaudiu Manoil enetc_rxbdr_wr(hw, i, ENETC_RBIER, ENETC_RBIER_RXTIE); 229391571081SClaudiu Manoil } 229491571081SClaudiu Manoil 229591571081SClaudiu Manoil if (priv->ic_mode & ENETC_IC_TX_MANUAL) 229691571081SClaudiu Manoil icpt = ENETC_TBICR0_SET_ICPT(ENETC_TXIC_PKTTHR); 229791571081SClaudiu Manoil else 229891571081SClaudiu Manoil icpt = 0x1; /* enable Tx ints by setting pkt thr to 1 */ 229991571081SClaudiu Manoil 2300d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 230191571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBICR1, priv->tx_ictt); 230291571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBICR0, ENETC_TBICR0_ICEN | icpt); 230391571081SClaudiu Manoil enetc_txbdr_wr(hw, i, ENETC_TBIER, ENETC_TBIER_TXTIE); 2304d4fd0404SClaudiu Manoil } 2305d4fd0404SClaudiu Manoil } 2306d4fd0404SClaudiu Manoil 2307bbb96dc7SClaudiu Manoil static void enetc_clear_interrupts(struct enetc_ndev_priv *priv) 2308d4fd0404SClaudiu Manoil { 2309715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 2310d4fd0404SClaudiu Manoil int i; 2311d4fd0404SClaudiu Manoil 2312d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2313715bf261SVladimir Oltean enetc_txbdr_wr(hw, i, ENETC_TBIER, 0); 2314d4fd0404SClaudiu Manoil 2315d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2316715bf261SVladimir Oltean enetc_rxbdr_wr(hw, i, ENETC_RBIER, 0); 2317d4fd0404SClaudiu Manoil } 2318d4fd0404SClaudiu Manoil 231971b77a7aSClaudiu Manoil static int enetc_phylink_connect(struct net_device *ndev) 2320d4fd0404SClaudiu Manoil { 23212e47cb41SPo Liu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2322a6a10d45SYangbo Lu struct ethtool_eee edata; 232371b77a7aSClaudiu Manoil int err; 2324d4fd0404SClaudiu Manoil 2325*598ca0d0SVladimir Oltean if (!priv->phylink) { 2326*598ca0d0SVladimir Oltean /* phy-less mode */ 2327*598ca0d0SVladimir Oltean netif_carrier_on(ndev); 2328*598ca0d0SVladimir Oltean return 0; 2329*598ca0d0SVladimir Oltean } 2330d4fd0404SClaudiu Manoil 233171b77a7aSClaudiu Manoil err = phylink_of_phy_connect(priv->phylink, priv->dev->of_node, 0); 233271b77a7aSClaudiu Manoil if (err) { 2333d4fd0404SClaudiu Manoil dev_err(&ndev->dev, "could not attach to PHY\n"); 233471b77a7aSClaudiu Manoil return err; 2335d4fd0404SClaudiu Manoil } 2336d4fd0404SClaudiu Manoil 2337a6a10d45SYangbo Lu /* disable EEE autoneg, until ENETC driver supports it */ 2338a6a10d45SYangbo Lu memset(&edata, 0, sizeof(struct ethtool_eee)); 233971b77a7aSClaudiu Manoil phylink_ethtool_set_eee(priv->phylink, &edata); 2340a6a10d45SYangbo Lu 2341*598ca0d0SVladimir Oltean phylink_start(priv->phylink); 2342*598ca0d0SVladimir Oltean 2343d4fd0404SClaudiu Manoil return 0; 2344d4fd0404SClaudiu Manoil } 2345d4fd0404SClaudiu Manoil 23467294380cSYangbo Lu static void enetc_tx_onestep_tstamp(struct work_struct *work) 23477294380cSYangbo Lu { 23487294380cSYangbo Lu struct enetc_ndev_priv *priv; 23497294380cSYangbo Lu struct sk_buff *skb; 23507294380cSYangbo Lu 23517294380cSYangbo Lu priv = container_of(work, struct enetc_ndev_priv, tx_onestep_tstamp); 23527294380cSYangbo Lu 23537294380cSYangbo Lu netif_tx_lock(priv->ndev); 23547294380cSYangbo Lu 23557294380cSYangbo Lu clear_bit_unlock(ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS, &priv->flags); 23567294380cSYangbo Lu skb = skb_dequeue(&priv->tx_skbs); 23577294380cSYangbo Lu if (skb) 23587294380cSYangbo Lu enetc_start_xmit(skb, priv->ndev); 23597294380cSYangbo Lu 23607294380cSYangbo Lu netif_tx_unlock(priv->ndev); 23617294380cSYangbo Lu } 23627294380cSYangbo Lu 23637294380cSYangbo Lu static void enetc_tx_onestep_tstamp_init(struct enetc_ndev_priv *priv) 23647294380cSYangbo Lu { 23657294380cSYangbo Lu INIT_WORK(&priv->tx_onestep_tstamp, enetc_tx_onestep_tstamp); 23667294380cSYangbo Lu skb_queue_head_init(&priv->tx_skbs); 23677294380cSYangbo Lu } 23687294380cSYangbo Lu 236991571081SClaudiu Manoil void enetc_start(struct net_device *ndev) 2370bbb96dc7SClaudiu Manoil { 2371bbb96dc7SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2372bbb96dc7SClaudiu Manoil int i; 2373bbb96dc7SClaudiu Manoil 2374bbb96dc7SClaudiu Manoil enetc_setup_interrupts(priv); 2375bbb96dc7SClaudiu Manoil 2376bbb96dc7SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2377bbb96dc7SClaudiu Manoil int irq = pci_irq_vector(priv->si->pdev, 2378bbb96dc7SClaudiu Manoil ENETC_BDR_INT_BASE_IDX + i); 2379bbb96dc7SClaudiu Manoil 2380bbb96dc7SClaudiu Manoil napi_enable(&priv->int_vector[i]->napi); 2381bbb96dc7SClaudiu Manoil enable_irq(irq); 2382bbb96dc7SClaudiu Manoil } 2383bbb96dc7SClaudiu Manoil 2384bbb96dc7SClaudiu Manoil netif_tx_start_all_queues(ndev); 2385bbb96dc7SClaudiu Manoil } 2386bbb96dc7SClaudiu Manoil 2387d4fd0404SClaudiu Manoil int enetc_open(struct net_device *ndev) 2388d4fd0404SClaudiu Manoil { 2389d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2390f3ce29e1SVladimir Oltean struct enetc_bdr_resource *tx_res, *rx_res; 23917eab503bSVladimir Oltean int num_stack_tx_queues; 2392d075db51SVladimir Oltean bool extended; 2393bbb96dc7SClaudiu Manoil int err; 2394d4fd0404SClaudiu Manoil 2395d075db51SVladimir Oltean extended = !!(priv->active_offloads & ENETC_F_RX_TSTAMP); 2396d075db51SVladimir Oltean 2397d4fd0404SClaudiu Manoil err = enetc_setup_irqs(priv); 2398d4fd0404SClaudiu Manoil if (err) 2399d4fd0404SClaudiu Manoil return err; 2400d4fd0404SClaudiu Manoil 240171b77a7aSClaudiu Manoil err = enetc_phylink_connect(ndev); 2402d4fd0404SClaudiu Manoil if (err) 2403d4fd0404SClaudiu Manoil goto err_phy_connect; 2404d4fd0404SClaudiu Manoil 2405f3ce29e1SVladimir Oltean tx_res = enetc_alloc_tx_resources(priv); 2406f3ce29e1SVladimir Oltean if (IS_ERR(tx_res)) { 2407f3ce29e1SVladimir Oltean err = PTR_ERR(tx_res); 2408d4fd0404SClaudiu Manoil goto err_alloc_tx; 2409f3ce29e1SVladimir Oltean } 2410d4fd0404SClaudiu Manoil 2411f3ce29e1SVladimir Oltean rx_res = enetc_alloc_rx_resources(priv, extended); 2412f3ce29e1SVladimir Oltean if (IS_ERR(rx_res)) { 2413f3ce29e1SVladimir Oltean err = PTR_ERR(rx_res); 2414d4fd0404SClaudiu Manoil goto err_alloc_rx; 2415f3ce29e1SVladimir Oltean } 2416d4fd0404SClaudiu Manoil 24177eab503bSVladimir Oltean num_stack_tx_queues = enetc_num_stack_tx_queues(priv); 24187eab503bSVladimir Oltean 24197eab503bSVladimir Oltean err = netif_set_real_num_tx_queues(ndev, num_stack_tx_queues); 2420d4fd0404SClaudiu Manoil if (err) 2421d4fd0404SClaudiu Manoil goto err_set_queues; 2422d4fd0404SClaudiu Manoil 2423d4fd0404SClaudiu Manoil err = netif_set_real_num_rx_queues(ndev, priv->num_rx_rings); 2424d4fd0404SClaudiu Manoil if (err) 2425d4fd0404SClaudiu Manoil goto err_set_queues; 2426d4fd0404SClaudiu Manoil 24277294380cSYangbo Lu enetc_tx_onestep_tstamp_init(priv); 2428f3ce29e1SVladimir Oltean enetc_assign_tx_resources(priv, tx_res); 2429f3ce29e1SVladimir Oltean enetc_assign_rx_resources(priv, rx_res); 2430d075db51SVladimir Oltean enetc_setup_bdrs(priv, extended); 2431bbb96dc7SClaudiu Manoil enetc_start(ndev); 2432d4fd0404SClaudiu Manoil 2433d4fd0404SClaudiu Manoil return 0; 2434d4fd0404SClaudiu Manoil 2435d4fd0404SClaudiu Manoil err_set_queues: 2436f3ce29e1SVladimir Oltean enetc_free_rx_resources(rx_res, priv->num_rx_rings); 2437d4fd0404SClaudiu Manoil err_alloc_rx: 2438f3ce29e1SVladimir Oltean enetc_free_tx_resources(tx_res, priv->num_tx_rings); 2439d4fd0404SClaudiu Manoil err_alloc_tx: 244071b77a7aSClaudiu Manoil if (priv->phylink) 244171b77a7aSClaudiu Manoil phylink_disconnect_phy(priv->phylink); 2442d4fd0404SClaudiu Manoil err_phy_connect: 2443d4fd0404SClaudiu Manoil enetc_free_irqs(priv); 2444d4fd0404SClaudiu Manoil 2445d4fd0404SClaudiu Manoil return err; 2446d4fd0404SClaudiu Manoil } 2447d4fd0404SClaudiu Manoil 244891571081SClaudiu Manoil void enetc_stop(struct net_device *ndev) 2449d4fd0404SClaudiu Manoil { 2450d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2451d4fd0404SClaudiu Manoil int i; 2452d4fd0404SClaudiu Manoil 2453d4fd0404SClaudiu Manoil netif_tx_stop_all_queues(ndev); 2454d4fd0404SClaudiu Manoil 2455d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2456bbb96dc7SClaudiu Manoil int irq = pci_irq_vector(priv->si->pdev, 2457bbb96dc7SClaudiu Manoil ENETC_BDR_INT_BASE_IDX + i); 2458bbb96dc7SClaudiu Manoil 2459bbb96dc7SClaudiu Manoil disable_irq(irq); 2460d4fd0404SClaudiu Manoil napi_synchronize(&priv->int_vector[i]->napi); 2461d4fd0404SClaudiu Manoil napi_disable(&priv->int_vector[i]->napi); 2462d4fd0404SClaudiu Manoil } 2463d4fd0404SClaudiu Manoil 2464bbb96dc7SClaudiu Manoil enetc_clear_interrupts(priv); 2465bbb96dc7SClaudiu Manoil } 2466bbb96dc7SClaudiu Manoil 2467bbb96dc7SClaudiu Manoil int enetc_close(struct net_device *ndev) 2468bbb96dc7SClaudiu Manoil { 2469bbb96dc7SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2470bbb96dc7SClaudiu Manoil 2471bbb96dc7SClaudiu Manoil enetc_stop(ndev); 2472d4fd0404SClaudiu Manoil enetc_clear_bdrs(priv); 2473d4fd0404SClaudiu Manoil 2474*598ca0d0SVladimir Oltean if (priv->phylink) { 2475*598ca0d0SVladimir Oltean phylink_stop(priv->phylink); 247671b77a7aSClaudiu Manoil phylink_disconnect_phy(priv->phylink); 2477*598ca0d0SVladimir Oltean } else { 2478*598ca0d0SVladimir Oltean netif_carrier_off(ndev); 2479*598ca0d0SVladimir Oltean } 2480*598ca0d0SVladimir Oltean 2481d4fd0404SClaudiu Manoil enetc_free_rxtx_rings(priv); 2482f3ce29e1SVladimir Oltean 2483f3ce29e1SVladimir Oltean /* Avoids dangling pointers and also frees old resources */ 2484f3ce29e1SVladimir Oltean enetc_assign_rx_resources(priv, NULL); 2485f3ce29e1SVladimir Oltean enetc_assign_tx_resources(priv, NULL); 2486f3ce29e1SVladimir Oltean 2487d4fd0404SClaudiu Manoil enetc_free_irqs(priv); 2488d4fd0404SClaudiu Manoil 2489d4fd0404SClaudiu Manoil return 0; 2490d4fd0404SClaudiu Manoil } 2491d4fd0404SClaudiu Manoil 24925641c751SVladimir Oltean int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data) 2493cbe9e835SCamelia Groza { 2494cbe9e835SCamelia Groza struct enetc_ndev_priv *priv = netdev_priv(ndev); 2495cbe9e835SCamelia Groza struct tc_mqprio_qopt *mqprio = type_data; 2496715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 2497cbe9e835SCamelia Groza struct enetc_bdr *tx_ring; 24987eab503bSVladimir Oltean int num_stack_tx_queues; 2499cbe9e835SCamelia Groza u8 num_tc; 2500cbe9e835SCamelia Groza int i; 2501cbe9e835SCamelia Groza 25027eab503bSVladimir Oltean num_stack_tx_queues = enetc_num_stack_tx_queues(priv); 2503cbe9e835SCamelia Groza mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 2504cbe9e835SCamelia Groza num_tc = mqprio->num_tc; 2505cbe9e835SCamelia Groza 2506cbe9e835SCamelia Groza if (!num_tc) { 2507cbe9e835SCamelia Groza netdev_reset_tc(ndev); 25087eab503bSVladimir Oltean netif_set_real_num_tx_queues(ndev, num_stack_tx_queues); 2509cbe9e835SCamelia Groza 2510cbe9e835SCamelia Groza /* Reset all ring priorities to 0 */ 2511cbe9e835SCamelia Groza for (i = 0; i < priv->num_tx_rings; i++) { 2512cbe9e835SCamelia Groza tx_ring = priv->tx_ring[i]; 2513290b5fe0SVladimir Oltean tx_ring->prio = 0; 2514290b5fe0SVladimir Oltean enetc_set_bdr_prio(hw, tx_ring->index, tx_ring->prio); 2515cbe9e835SCamelia Groza } 2516cbe9e835SCamelia Groza 2517cbe9e835SCamelia Groza return 0; 2518cbe9e835SCamelia Groza } 2519cbe9e835SCamelia Groza 2520cbe9e835SCamelia Groza /* Check if we have enough BD rings available to accommodate all TCs */ 25217eab503bSVladimir Oltean if (num_tc > num_stack_tx_queues) { 2522cbe9e835SCamelia Groza netdev_err(ndev, "Max %d traffic classes supported\n", 2523cbe9e835SCamelia Groza priv->num_tx_rings); 2524cbe9e835SCamelia Groza return -EINVAL; 2525cbe9e835SCamelia Groza } 2526cbe9e835SCamelia Groza 2527cbe9e835SCamelia Groza /* For the moment, we use only one BD ring per TC. 2528cbe9e835SCamelia Groza * 2529cbe9e835SCamelia Groza * Configure num_tc BD rings with increasing priorities. 2530cbe9e835SCamelia Groza */ 2531cbe9e835SCamelia Groza for (i = 0; i < num_tc; i++) { 2532cbe9e835SCamelia Groza tx_ring = priv->tx_ring[i]; 2533290b5fe0SVladimir Oltean tx_ring->prio = i; 2534290b5fe0SVladimir Oltean enetc_set_bdr_prio(hw, tx_ring->index, tx_ring->prio); 2535cbe9e835SCamelia Groza } 2536cbe9e835SCamelia Groza 2537cbe9e835SCamelia Groza /* Reset the number of netdev queues based on the TC count */ 2538cbe9e835SCamelia Groza netif_set_real_num_tx_queues(ndev, num_tc); 2539cbe9e835SCamelia Groza 2540cbe9e835SCamelia Groza netdev_set_num_tc(ndev, num_tc); 2541cbe9e835SCamelia Groza 2542cbe9e835SCamelia Groza /* Each TC is associated with one netdev queue */ 2543cbe9e835SCamelia Groza for (i = 0; i < num_tc; i++) 2544cbe9e835SCamelia Groza netdev_set_tc_queue(ndev, i, 1, i); 2545cbe9e835SCamelia Groza 2546cbe9e835SCamelia Groza return 0; 2547cbe9e835SCamelia Groza } 2548cbe9e835SCamelia Groza 2549d1b15102SVladimir Oltean static int enetc_setup_xdp_prog(struct net_device *dev, struct bpf_prog *prog, 2550d1b15102SVladimir Oltean struct netlink_ext_ack *extack) 2551d1b15102SVladimir Oltean { 2552d1b15102SVladimir Oltean struct enetc_ndev_priv *priv = netdev_priv(dev); 2553d1b15102SVladimir Oltean struct bpf_prog *old_prog; 2554d1b15102SVladimir Oltean bool is_up; 2555d1b15102SVladimir Oltean int i; 2556d1b15102SVladimir Oltean 2557d1b15102SVladimir Oltean /* The buffer layout is changing, so we need to drain the old 2558d1b15102SVladimir Oltean * RX buffers and seed new ones. 2559d1b15102SVladimir Oltean */ 2560d1b15102SVladimir Oltean is_up = netif_running(dev); 2561d1b15102SVladimir Oltean if (is_up) 2562d1b15102SVladimir Oltean dev_close(dev); 2563d1b15102SVladimir Oltean 2564d1b15102SVladimir Oltean old_prog = xchg(&priv->xdp_prog, prog); 2565d1b15102SVladimir Oltean if (old_prog) 2566d1b15102SVladimir Oltean bpf_prog_put(old_prog); 2567d1b15102SVladimir Oltean 2568d1b15102SVladimir Oltean for (i = 0; i < priv->num_rx_rings; i++) { 2569d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = priv->rx_ring[i]; 2570d1b15102SVladimir Oltean 2571d1b15102SVladimir Oltean rx_ring->xdp.prog = prog; 2572d1b15102SVladimir Oltean 2573d1b15102SVladimir Oltean if (prog) 2574d1b15102SVladimir Oltean rx_ring->buffer_offset = XDP_PACKET_HEADROOM; 2575d1b15102SVladimir Oltean else 2576d1b15102SVladimir Oltean rx_ring->buffer_offset = ENETC_RXB_PAD; 2577d1b15102SVladimir Oltean } 2578d1b15102SVladimir Oltean 2579d1b15102SVladimir Oltean if (is_up) 2580d1b15102SVladimir Oltean return dev_open(dev, extack); 2581d1b15102SVladimir Oltean 2582d1b15102SVladimir Oltean return 0; 2583d1b15102SVladimir Oltean } 2584d1b15102SVladimir Oltean 2585d1b15102SVladimir Oltean int enetc_setup_bpf(struct net_device *dev, struct netdev_bpf *xdp) 2586d1b15102SVladimir Oltean { 2587d1b15102SVladimir Oltean switch (xdp->command) { 2588d1b15102SVladimir Oltean case XDP_SETUP_PROG: 2589d1b15102SVladimir Oltean return enetc_setup_xdp_prog(dev, xdp->prog, xdp->extack); 2590d1b15102SVladimir Oltean default: 2591d1b15102SVladimir Oltean return -EINVAL; 2592d1b15102SVladimir Oltean } 2593d1b15102SVladimir Oltean 2594d1b15102SVladimir Oltean return 0; 2595d1b15102SVladimir Oltean } 2596d1b15102SVladimir Oltean 2597d4fd0404SClaudiu Manoil struct net_device_stats *enetc_get_stats(struct net_device *ndev) 2598d4fd0404SClaudiu Manoil { 2599d4fd0404SClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2600d4fd0404SClaudiu Manoil struct net_device_stats *stats = &ndev->stats; 2601d4fd0404SClaudiu Manoil unsigned long packets = 0, bytes = 0; 2602285e8dedSPo Liu unsigned long tx_dropped = 0; 2603d4fd0404SClaudiu Manoil int i; 2604d4fd0404SClaudiu Manoil 2605d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) { 2606d4fd0404SClaudiu Manoil packets += priv->rx_ring[i]->stats.packets; 2607d4fd0404SClaudiu Manoil bytes += priv->rx_ring[i]->stats.bytes; 2608d4fd0404SClaudiu Manoil } 2609d4fd0404SClaudiu Manoil 2610d4fd0404SClaudiu Manoil stats->rx_packets = packets; 2611d4fd0404SClaudiu Manoil stats->rx_bytes = bytes; 2612d4fd0404SClaudiu Manoil bytes = 0; 2613d4fd0404SClaudiu Manoil packets = 0; 2614d4fd0404SClaudiu Manoil 2615d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) { 2616d4fd0404SClaudiu Manoil packets += priv->tx_ring[i]->stats.packets; 2617d4fd0404SClaudiu Manoil bytes += priv->tx_ring[i]->stats.bytes; 2618285e8dedSPo Liu tx_dropped += priv->tx_ring[i]->stats.win_drop; 2619d4fd0404SClaudiu Manoil } 2620d4fd0404SClaudiu Manoil 2621d4fd0404SClaudiu Manoil stats->tx_packets = packets; 2622d4fd0404SClaudiu Manoil stats->tx_bytes = bytes; 2623285e8dedSPo Liu stats->tx_dropped = tx_dropped; 2624d4fd0404SClaudiu Manoil 2625d4fd0404SClaudiu Manoil return stats; 2626d4fd0404SClaudiu Manoil } 2627d4fd0404SClaudiu Manoil 2628d382563fSClaudiu Manoil static int enetc_set_rss(struct net_device *ndev, int en) 2629d382563fSClaudiu Manoil { 2630d382563fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2631d382563fSClaudiu Manoil struct enetc_hw *hw = &priv->si->hw; 2632d382563fSClaudiu Manoil u32 reg; 2633d382563fSClaudiu Manoil 2634d382563fSClaudiu Manoil enetc_wr(hw, ENETC_SIRBGCR, priv->num_rx_rings); 2635d382563fSClaudiu Manoil 2636d382563fSClaudiu Manoil reg = enetc_rd(hw, ENETC_SIMR); 2637d382563fSClaudiu Manoil reg &= ~ENETC_SIMR_RSSE; 2638d382563fSClaudiu Manoil reg |= (en) ? ENETC_SIMR_RSSE : 0; 2639d382563fSClaudiu Manoil enetc_wr(hw, ENETC_SIMR, reg); 2640d382563fSClaudiu Manoil 2641d382563fSClaudiu Manoil return 0; 2642d382563fSClaudiu Manoil } 2643d382563fSClaudiu Manoil 26449deba33fSClaudiu Manoil static void enetc_enable_rxvlan(struct net_device *ndev, bool en) 26459deba33fSClaudiu Manoil { 26469deba33fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2647715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 26489deba33fSClaudiu Manoil int i; 26499deba33fSClaudiu Manoil 26509deba33fSClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2651715bf261SVladimir Oltean enetc_bdr_enable_rxvlan(hw, i, en); 26529deba33fSClaudiu Manoil } 26539deba33fSClaudiu Manoil 26549deba33fSClaudiu Manoil static void enetc_enable_txvlan(struct net_device *ndev, bool en) 26559deba33fSClaudiu Manoil { 26569deba33fSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2657715bf261SVladimir Oltean struct enetc_hw *hw = &priv->si->hw; 26589deba33fSClaudiu Manoil int i; 26599deba33fSClaudiu Manoil 26609deba33fSClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2661715bf261SVladimir Oltean enetc_bdr_enable_txvlan(hw, i, en); 26629deba33fSClaudiu Manoil } 26639deba33fSClaudiu Manoil 2664fed38e64SVladimir Oltean void enetc_set_features(struct net_device *ndev, netdev_features_t features) 2665d382563fSClaudiu Manoil { 2666d382563fSClaudiu Manoil netdev_features_t changed = ndev->features ^ features; 2667d382563fSClaudiu Manoil 2668d382563fSClaudiu Manoil if (changed & NETIF_F_RXHASH) 2669d382563fSClaudiu Manoil enetc_set_rss(ndev, !!(features & NETIF_F_RXHASH)); 2670d382563fSClaudiu Manoil 26719deba33fSClaudiu Manoil if (changed & NETIF_F_HW_VLAN_CTAG_RX) 26729deba33fSClaudiu Manoil enetc_enable_rxvlan(ndev, 26739deba33fSClaudiu Manoil !!(features & NETIF_F_HW_VLAN_CTAG_RX)); 26749deba33fSClaudiu Manoil 26759deba33fSClaudiu Manoil if (changed & NETIF_F_HW_VLAN_CTAG_TX) 26769deba33fSClaudiu Manoil enetc_enable_txvlan(ndev, 26779deba33fSClaudiu Manoil !!(features & NETIF_F_HW_VLAN_CTAG_TX)); 2678d382563fSClaudiu Manoil } 2679d382563fSClaudiu Manoil 2680434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 2681d3982312SY.b. Lu static int enetc_hwtstamp_set(struct net_device *ndev, struct ifreq *ifr) 2682d3982312SY.b. Lu { 2683d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2684d3982312SY.b. Lu struct hwtstamp_config config; 2685434cebabSClaudiu Manoil int ao; 2686d3982312SY.b. Lu 2687d3982312SY.b. Lu if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 2688d3982312SY.b. Lu return -EFAULT; 2689d3982312SY.b. Lu 2690d3982312SY.b. Lu switch (config.tx_type) { 2691d3982312SY.b. Lu case HWTSTAMP_TX_OFF: 26927294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 2693d3982312SY.b. Lu break; 2694d3982312SY.b. Lu case HWTSTAMP_TX_ON: 26957294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 2696d3982312SY.b. Lu priv->active_offloads |= ENETC_F_TX_TSTAMP; 2697d3982312SY.b. Lu break; 26987294380cSYangbo Lu case HWTSTAMP_TX_ONESTEP_SYNC: 26997294380cSYangbo Lu priv->active_offloads &= ~ENETC_F_TX_TSTAMP_MASK; 27007294380cSYangbo Lu priv->active_offloads |= ENETC_F_TX_ONESTEP_SYNC_TSTAMP; 27017294380cSYangbo Lu break; 2702d3982312SY.b. Lu default: 2703d3982312SY.b. Lu return -ERANGE; 2704d3982312SY.b. Lu } 2705d3982312SY.b. Lu 2706434cebabSClaudiu Manoil ao = priv->active_offloads; 2707d3982312SY.b. Lu switch (config.rx_filter) { 2708d3982312SY.b. Lu case HWTSTAMP_FILTER_NONE: 2709d3982312SY.b. Lu priv->active_offloads &= ~ENETC_F_RX_TSTAMP; 2710d3982312SY.b. Lu break; 2711d3982312SY.b. Lu default: 2712d3982312SY.b. Lu priv->active_offloads |= ENETC_F_RX_TSTAMP; 2713d3982312SY.b. Lu config.rx_filter = HWTSTAMP_FILTER_ALL; 2714d3982312SY.b. Lu } 2715d3982312SY.b. Lu 2716434cebabSClaudiu Manoil if (netif_running(ndev) && ao != priv->active_offloads) { 2717434cebabSClaudiu Manoil enetc_close(ndev); 2718434cebabSClaudiu Manoil enetc_open(ndev); 2719434cebabSClaudiu Manoil } 2720434cebabSClaudiu Manoil 2721d3982312SY.b. Lu return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2722d3982312SY.b. Lu -EFAULT : 0; 2723d3982312SY.b. Lu } 2724d3982312SY.b. Lu 2725d3982312SY.b. Lu static int enetc_hwtstamp_get(struct net_device *ndev, struct ifreq *ifr) 2726d3982312SY.b. Lu { 2727d3982312SY.b. Lu struct enetc_ndev_priv *priv = netdev_priv(ndev); 2728d3982312SY.b. Lu struct hwtstamp_config config; 2729d3982312SY.b. Lu 2730d3982312SY.b. Lu config.flags = 0; 2731d3982312SY.b. Lu 27327294380cSYangbo Lu if (priv->active_offloads & ENETC_F_TX_ONESTEP_SYNC_TSTAMP) 27337294380cSYangbo Lu config.tx_type = HWTSTAMP_TX_ONESTEP_SYNC; 27347294380cSYangbo Lu else if (priv->active_offloads & ENETC_F_TX_TSTAMP) 2735d3982312SY.b. Lu config.tx_type = HWTSTAMP_TX_ON; 2736d3982312SY.b. Lu else 2737d3982312SY.b. Lu config.tx_type = HWTSTAMP_TX_OFF; 2738d3982312SY.b. Lu 2739d3982312SY.b. Lu config.rx_filter = (priv->active_offloads & ENETC_F_RX_TSTAMP) ? 2740d3982312SY.b. Lu HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 2741d3982312SY.b. Lu 2742d3982312SY.b. Lu return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 2743d3982312SY.b. Lu -EFAULT : 0; 2744d3982312SY.b. Lu } 2745d3982312SY.b. Lu #endif 2746d3982312SY.b. Lu 2747d3982312SY.b. Lu int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 2748d3982312SY.b. Lu { 274971b77a7aSClaudiu Manoil struct enetc_ndev_priv *priv = netdev_priv(ndev); 2750434cebabSClaudiu Manoil #ifdef CONFIG_FSL_ENETC_PTP_CLOCK 2751d3982312SY.b. Lu if (cmd == SIOCSHWTSTAMP) 2752d3982312SY.b. Lu return enetc_hwtstamp_set(ndev, rq); 2753d3982312SY.b. Lu if (cmd == SIOCGHWTSTAMP) 2754d3982312SY.b. Lu return enetc_hwtstamp_get(ndev, rq); 2755d3982312SY.b. Lu #endif 2756a613bafeSMichael Walle 275771b77a7aSClaudiu Manoil if (!priv->phylink) 2758c55b810aSMichael Walle return -EOPNOTSUPP; 275971b77a7aSClaudiu Manoil 276071b77a7aSClaudiu Manoil return phylink_mii_ioctl(priv->phylink, rq, cmd); 2761d3982312SY.b. Lu } 2762d3982312SY.b. Lu 2763d4fd0404SClaudiu Manoil int enetc_alloc_msix(struct enetc_ndev_priv *priv) 2764d4fd0404SClaudiu Manoil { 2765d4fd0404SClaudiu Manoil struct pci_dev *pdev = priv->si->pdev; 27667eab503bSVladimir Oltean int first_xdp_tx_ring; 2767d4fd0404SClaudiu Manoil int i, n, err, nvec; 27687eab503bSVladimir Oltean int v_tx_rings; 2769d4fd0404SClaudiu Manoil 2770d4fd0404SClaudiu Manoil nvec = ENETC_BDR_INT_BASE_IDX + priv->bdr_int_num; 2771d4fd0404SClaudiu Manoil /* allocate MSIX for both messaging and Rx/Tx interrupts */ 2772d4fd0404SClaudiu Manoil n = pci_alloc_irq_vectors(pdev, nvec, nvec, PCI_IRQ_MSIX); 2773d4fd0404SClaudiu Manoil 2774d4fd0404SClaudiu Manoil if (n < 0) 2775d4fd0404SClaudiu Manoil return n; 2776d4fd0404SClaudiu Manoil 2777d4fd0404SClaudiu Manoil if (n != nvec) 2778d4fd0404SClaudiu Manoil return -EPERM; 2779d4fd0404SClaudiu Manoil 2780d4fd0404SClaudiu Manoil /* # of tx rings per int vector */ 2781d4fd0404SClaudiu Manoil v_tx_rings = priv->num_tx_rings / priv->bdr_int_num; 2782d4fd0404SClaudiu Manoil 2783d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2784d4fd0404SClaudiu Manoil struct enetc_int_vector *v; 2785d4fd0404SClaudiu Manoil struct enetc_bdr *bdr; 2786d4fd0404SClaudiu Manoil int j; 2787d4fd0404SClaudiu Manoil 27881260e772SGustavo A. R. Silva v = kzalloc(struct_size(v, tx_ring, v_tx_rings), GFP_KERNEL); 2789d4fd0404SClaudiu Manoil if (!v) { 2790d4fd0404SClaudiu Manoil err = -ENOMEM; 2791d4fd0404SClaudiu Manoil goto fail; 2792d4fd0404SClaudiu Manoil } 2793d4fd0404SClaudiu Manoil 2794d4fd0404SClaudiu Manoil priv->int_vector[i] = v; 2795d4fd0404SClaudiu Manoil 2796d1b15102SVladimir Oltean bdr = &v->rx_ring; 2797d1b15102SVladimir Oltean bdr->index = i; 2798d1b15102SVladimir Oltean bdr->ndev = priv->ndev; 2799d1b15102SVladimir Oltean bdr->dev = priv->dev; 2800d1b15102SVladimir Oltean bdr->bd_count = priv->rx_bd_count; 2801d1b15102SVladimir Oltean bdr->buffer_offset = ENETC_RXB_PAD; 2802d1b15102SVladimir Oltean priv->rx_ring[i] = bdr; 2803d1b15102SVladimir Oltean 2804d1b15102SVladimir Oltean err = xdp_rxq_info_reg(&bdr->xdp.rxq, priv->ndev, i, 0); 2805d1b15102SVladimir Oltean if (err) { 2806d1b15102SVladimir Oltean kfree(v); 2807d1b15102SVladimir Oltean goto fail; 2808d1b15102SVladimir Oltean } 2809d1b15102SVladimir Oltean 2810d1b15102SVladimir Oltean err = xdp_rxq_info_reg_mem_model(&bdr->xdp.rxq, 2811d1b15102SVladimir Oltean MEM_TYPE_PAGE_SHARED, NULL); 2812d1b15102SVladimir Oltean if (err) { 2813d1b15102SVladimir Oltean xdp_rxq_info_unreg(&bdr->xdp.rxq); 2814d1b15102SVladimir Oltean kfree(v); 2815d1b15102SVladimir Oltean goto fail; 2816d1b15102SVladimir Oltean } 2817d1b15102SVladimir Oltean 2818ae0e6a5dSClaudiu Manoil /* init defaults for adaptive IC */ 2819ae0e6a5dSClaudiu Manoil if (priv->ic_mode & ENETC_IC_RX_ADAPTIVE) { 2820ae0e6a5dSClaudiu Manoil v->rx_ictt = 0x1; 2821ae0e6a5dSClaudiu Manoil v->rx_dim_en = true; 2822ae0e6a5dSClaudiu Manoil } 2823ae0e6a5dSClaudiu Manoil INIT_WORK(&v->rx_dim.work, enetc_rx_dim_work); 2824b48b89f9SJakub Kicinski netif_napi_add(priv->ndev, &v->napi, enetc_poll); 2825d4fd0404SClaudiu Manoil v->count_tx_rings = v_tx_rings; 2826d4fd0404SClaudiu Manoil 2827d4fd0404SClaudiu Manoil for (j = 0; j < v_tx_rings; j++) { 2828d4fd0404SClaudiu Manoil int idx; 2829d4fd0404SClaudiu Manoil 2830d4fd0404SClaudiu Manoil /* default tx ring mapping policy */ 28316c5e6b4cSClaudiu Manoil idx = priv->bdr_int_num * j + i; 2832d4fd0404SClaudiu Manoil __set_bit(idx, &v->tx_rings_map); 2833d4fd0404SClaudiu Manoil bdr = &v->tx_ring[j]; 2834d4fd0404SClaudiu Manoil bdr->index = idx; 2835d4fd0404SClaudiu Manoil bdr->ndev = priv->ndev; 2836d4fd0404SClaudiu Manoil bdr->dev = priv->dev; 2837d4fd0404SClaudiu Manoil bdr->bd_count = priv->tx_bd_count; 2838d4fd0404SClaudiu Manoil priv->tx_ring[idx] = bdr; 2839d4fd0404SClaudiu Manoil } 2840d4fd0404SClaudiu Manoil } 2841d4fd0404SClaudiu Manoil 28427eab503bSVladimir Oltean first_xdp_tx_ring = priv->num_tx_rings - num_possible_cpus(); 28437eab503bSVladimir Oltean priv->xdp_tx_ring = &priv->tx_ring[first_xdp_tx_ring]; 28447eab503bSVladimir Oltean 2845d4fd0404SClaudiu Manoil return 0; 2846d4fd0404SClaudiu Manoil 2847d4fd0404SClaudiu Manoil fail: 2848d4fd0404SClaudiu Manoil while (i--) { 2849d1b15102SVladimir Oltean struct enetc_int_vector *v = priv->int_vector[i]; 2850d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 2851d1b15102SVladimir Oltean 2852d1b15102SVladimir Oltean xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq); 2853d1b15102SVladimir Oltean xdp_rxq_info_unreg(&rx_ring->xdp.rxq); 2854d1b15102SVladimir Oltean netif_napi_del(&v->napi); 2855d1b15102SVladimir Oltean cancel_work_sync(&v->rx_dim.work); 2856d1b15102SVladimir Oltean kfree(v); 2857d4fd0404SClaudiu Manoil } 2858d4fd0404SClaudiu Manoil 2859d4fd0404SClaudiu Manoil pci_free_irq_vectors(pdev); 2860d4fd0404SClaudiu Manoil 2861d4fd0404SClaudiu Manoil return err; 2862d4fd0404SClaudiu Manoil } 2863d4fd0404SClaudiu Manoil 2864d4fd0404SClaudiu Manoil void enetc_free_msix(struct enetc_ndev_priv *priv) 2865d4fd0404SClaudiu Manoil { 2866d4fd0404SClaudiu Manoil int i; 2867d4fd0404SClaudiu Manoil 2868d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2869d4fd0404SClaudiu Manoil struct enetc_int_vector *v = priv->int_vector[i]; 2870d1b15102SVladimir Oltean struct enetc_bdr *rx_ring = &v->rx_ring; 2871d4fd0404SClaudiu Manoil 2872d1b15102SVladimir Oltean xdp_rxq_info_unreg_mem_model(&rx_ring->xdp.rxq); 2873d1b15102SVladimir Oltean xdp_rxq_info_unreg(&rx_ring->xdp.rxq); 2874d4fd0404SClaudiu Manoil netif_napi_del(&v->napi); 2875ae0e6a5dSClaudiu Manoil cancel_work_sync(&v->rx_dim.work); 2876d4fd0404SClaudiu Manoil } 2877d4fd0404SClaudiu Manoil 2878d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_rx_rings; i++) 2879d4fd0404SClaudiu Manoil priv->rx_ring[i] = NULL; 2880d4fd0404SClaudiu Manoil 2881d4fd0404SClaudiu Manoil for (i = 0; i < priv->num_tx_rings; i++) 2882d4fd0404SClaudiu Manoil priv->tx_ring[i] = NULL; 2883d4fd0404SClaudiu Manoil 2884d4fd0404SClaudiu Manoil for (i = 0; i < priv->bdr_int_num; i++) { 2885d4fd0404SClaudiu Manoil kfree(priv->int_vector[i]); 2886d4fd0404SClaudiu Manoil priv->int_vector[i] = NULL; 2887d4fd0404SClaudiu Manoil } 2888d4fd0404SClaudiu Manoil 2889d4fd0404SClaudiu Manoil /* disable all MSIX for this device */ 2890d4fd0404SClaudiu Manoil pci_free_irq_vectors(priv->si->pdev); 2891d4fd0404SClaudiu Manoil } 2892d4fd0404SClaudiu Manoil 2893d4fd0404SClaudiu Manoil static void enetc_kfree_si(struct enetc_si *si) 2894d4fd0404SClaudiu Manoil { 2895d4fd0404SClaudiu Manoil char *p = (char *)si - si->pad; 2896d4fd0404SClaudiu Manoil 2897d4fd0404SClaudiu Manoil kfree(p); 2898d4fd0404SClaudiu Manoil } 2899d4fd0404SClaudiu Manoil 2900d4fd0404SClaudiu Manoil static void enetc_detect_errata(struct enetc_si *si) 2901d4fd0404SClaudiu Manoil { 2902d4fd0404SClaudiu Manoil if (si->pdev->revision == ENETC_REV1) 290382728b91SClaudiu Manoil si->errata = ENETC_ERR_VLAN_ISOL | ENETC_ERR_UCMCSWP; 2904d4fd0404SClaudiu Manoil } 2905d4fd0404SClaudiu Manoil 2906d4fd0404SClaudiu Manoil int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv) 2907d4fd0404SClaudiu Manoil { 2908d4fd0404SClaudiu Manoil struct enetc_si *si, *p; 2909d4fd0404SClaudiu Manoil struct enetc_hw *hw; 2910d4fd0404SClaudiu Manoil size_t alloc_size; 2911d4fd0404SClaudiu Manoil int err, len; 2912d4fd0404SClaudiu Manoil 2913d4fd0404SClaudiu Manoil pcie_flr(pdev); 2914d4fd0404SClaudiu Manoil err = pci_enable_device_mem(pdev); 2915a72691eeSCai Huoqing if (err) 2916a72691eeSCai Huoqing return dev_err_probe(&pdev->dev, err, "device enable failed\n"); 2917d4fd0404SClaudiu Manoil 2918d4fd0404SClaudiu Manoil /* set up for high or low dma */ 2919d4fd0404SClaudiu Manoil err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 2920d4fd0404SClaudiu Manoil if (err) { 2921cfcfc8f5SChristophe JAILLET dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err); 2922d4fd0404SClaudiu Manoil goto err_dma; 2923d4fd0404SClaudiu Manoil } 2924d4fd0404SClaudiu Manoil 2925d4fd0404SClaudiu Manoil err = pci_request_mem_regions(pdev, name); 2926d4fd0404SClaudiu Manoil if (err) { 2927d4fd0404SClaudiu Manoil dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err); 2928d4fd0404SClaudiu Manoil goto err_pci_mem_reg; 2929d4fd0404SClaudiu Manoil } 2930d4fd0404SClaudiu Manoil 2931d4fd0404SClaudiu Manoil pci_set_master(pdev); 2932d4fd0404SClaudiu Manoil 2933d4fd0404SClaudiu Manoil alloc_size = sizeof(struct enetc_si); 2934d4fd0404SClaudiu Manoil if (sizeof_priv) { 2935d4fd0404SClaudiu Manoil /* align priv to 32B */ 2936d4fd0404SClaudiu Manoil alloc_size = ALIGN(alloc_size, ENETC_SI_ALIGN); 2937d4fd0404SClaudiu Manoil alloc_size += sizeof_priv; 2938d4fd0404SClaudiu Manoil } 2939d4fd0404SClaudiu Manoil /* force 32B alignment for enetc_si */ 2940d4fd0404SClaudiu Manoil alloc_size += ENETC_SI_ALIGN - 1; 2941d4fd0404SClaudiu Manoil 2942d4fd0404SClaudiu Manoil p = kzalloc(alloc_size, GFP_KERNEL); 2943d4fd0404SClaudiu Manoil if (!p) { 2944d4fd0404SClaudiu Manoil err = -ENOMEM; 2945d4fd0404SClaudiu Manoil goto err_alloc_si; 2946d4fd0404SClaudiu Manoil } 2947d4fd0404SClaudiu Manoil 2948d4fd0404SClaudiu Manoil si = PTR_ALIGN(p, ENETC_SI_ALIGN); 2949d4fd0404SClaudiu Manoil si->pad = (char *)si - (char *)p; 2950d4fd0404SClaudiu Manoil 2951d4fd0404SClaudiu Manoil pci_set_drvdata(pdev, si); 2952d4fd0404SClaudiu Manoil si->pdev = pdev; 2953d4fd0404SClaudiu Manoil hw = &si->hw; 2954d4fd0404SClaudiu Manoil 2955d4fd0404SClaudiu Manoil len = pci_resource_len(pdev, ENETC_BAR_REGS); 2956d4fd0404SClaudiu Manoil hw->reg = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len); 2957d4fd0404SClaudiu Manoil if (!hw->reg) { 2958d4fd0404SClaudiu Manoil err = -ENXIO; 2959d4fd0404SClaudiu Manoil dev_err(&pdev->dev, "ioremap() failed\n"); 2960d4fd0404SClaudiu Manoil goto err_ioremap; 2961d4fd0404SClaudiu Manoil } 2962d4fd0404SClaudiu Manoil if (len > ENETC_PORT_BASE) 2963d4fd0404SClaudiu Manoil hw->port = hw->reg + ENETC_PORT_BASE; 2964d4fd0404SClaudiu Manoil if (len > ENETC_GLOBAL_BASE) 2965d4fd0404SClaudiu Manoil hw->global = hw->reg + ENETC_GLOBAL_BASE; 2966d4fd0404SClaudiu Manoil 2967d4fd0404SClaudiu Manoil enetc_detect_errata(si); 2968d4fd0404SClaudiu Manoil 2969d4fd0404SClaudiu Manoil return 0; 2970d4fd0404SClaudiu Manoil 2971d4fd0404SClaudiu Manoil err_ioremap: 2972d4fd0404SClaudiu Manoil enetc_kfree_si(si); 2973d4fd0404SClaudiu Manoil err_alloc_si: 2974d4fd0404SClaudiu Manoil pci_release_mem_regions(pdev); 2975d4fd0404SClaudiu Manoil err_pci_mem_reg: 2976d4fd0404SClaudiu Manoil err_dma: 2977d4fd0404SClaudiu Manoil pci_disable_device(pdev); 2978d4fd0404SClaudiu Manoil 2979d4fd0404SClaudiu Manoil return err; 2980d4fd0404SClaudiu Manoil } 2981d4fd0404SClaudiu Manoil 2982d4fd0404SClaudiu Manoil void enetc_pci_remove(struct pci_dev *pdev) 2983d4fd0404SClaudiu Manoil { 2984d4fd0404SClaudiu Manoil struct enetc_si *si = pci_get_drvdata(pdev); 2985d4fd0404SClaudiu Manoil struct enetc_hw *hw = &si->hw; 2986d4fd0404SClaudiu Manoil 2987d4fd0404SClaudiu Manoil iounmap(hw->reg); 2988d4fd0404SClaudiu Manoil enetc_kfree_si(si); 2989d4fd0404SClaudiu Manoil pci_release_mem_regions(pdev); 2990d4fd0404SClaudiu Manoil pci_disable_device(pdev); 2991d4fd0404SClaudiu Manoil } 2992