enetc.h 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
/* Copyright 2017-2019 NXP */

#include <linux/timer.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/dma-mapping.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>
12
#include <linux/phylink.h>
13
#include <linux/dim.h>
14 15 16 17 18 19 20

#include "enetc_hw.h"

#define ENETC_MAC_MAXFRM_SIZE	9600
#define ENETC_MAX_MTU		(ENETC_MAC_MAXFRM_SIZE - \
				(ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN))

21 22
#define ENETC_CBD_DATA_MEM_ALIGN 64

23
struct enetc_tx_swbd {
24 25 26 27
	union {
		struct sk_buff *skb;
		struct xdp_frame *xdp_frame;
	};
28
	dma_addr_t dma;
29 30
	struct page *page;	/* valid only if is_xdp_tx */
	u16 page_offset;	/* valid only if is_xdp_tx */
31
	u16 len;
32
	enum dma_data_direction dir;
33 34
	u8 is_dma_page:1;
	u8 check_wb:1;
35
	u8 do_twostep_tstamp:1;
36
	u8 is_eof:1;
37
	u8 is_xdp_tx:1;
38
	u8 is_xdp_redirect:1;
39
	u8 qbv_en:1;
40 41 42 43 44 45 46
};

#define ENETC_RX_MAXFRM_SIZE	ENETC_MAC_MAXFRM_SIZE
#define ENETC_RXB_TRUESIZE	2048 /* PAGE_SIZE >> 1 */
#define ENETC_RXB_PAD		NET_SKB_PAD /* add extra space if needed */
#define ENETC_RXB_DMA_SIZE	\
	(SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - ENETC_RXB_PAD)
47 48
#define ENETC_RXB_DMA_SIZE_XDP	\
	(SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - XDP_PACKET_HEADROOM)
49 50 51 52 53

struct enetc_rx_swbd {
	dma_addr_t dma;
	struct page *page;
	u16 page_offset;
54 55
	enum dma_data_direction dir;
	u16 len;
56 57
};

58 59 60 61 62 63
/* ENETC overhead: optional extension BD + 1 BD gap */
#define ENETC_TXBDS_NEEDED(val)	((val) + 2)
/* max # of chained Tx BDs is 15, including head and extension BD */
#define ENETC_MAX_SKB_FRAGS	13
#define ENETC_TXBDS_MAX_NEEDED	ENETC_TXBDS_NEEDED(ENETC_MAX_SKB_FRAGS + 1)

64 65 66 67
struct enetc_ring_stats {
	unsigned int packets;
	unsigned int bytes;
	unsigned int rx_alloc_errs;
68
	unsigned int xdp_drops;
69 70
	unsigned int xdp_tx;
	unsigned int xdp_tx_drops;
71 72 73
	unsigned int xdp_redirect;
	unsigned int xdp_redirect_failures;
	unsigned int xdp_redirect_sg;
74 75
	unsigned int recycles;
	unsigned int recycle_failures;
76
	unsigned int win_drop;
77 78 79 80 81
};

struct enetc_xdp_data {
	struct xdp_rxq_info rxq;
	struct bpf_prog *prog;
82
	int xdp_tx_in_flight;
83 84
};

85
#define ENETC_RX_RING_DEFAULT_SIZE	2048
86
#define ENETC_TX_RING_DEFAULT_SIZE	2048
87
#define ENETC_DEFAULT_TX_WORK		(ENETC_TX_RING_DEFAULT_SIZE / 2)
88 89 90 91 92 93 94 95 96 97

struct enetc_bdr {
	struct device *dev; /* for DMA mapping */
	struct net_device *ndev;
	void *bd_base; /* points to Rx or Tx BD ring */
	union {
		void __iomem *tpir;
		void __iomem *rcir;
	};
	u16 index;
98
	u16 prio;
99 100 101 102 103 104 105 106 107 108 109 110 111
	int bd_count; /* # of BDs */
	int next_to_use;
	int next_to_clean;
	union {
		struct enetc_tx_swbd *tx_swbd;
		struct enetc_rx_swbd *rx_swbd;
	};
	union {
		void __iomem *tcir; /* Tx */
		int next_to_alloc; /* Rx */
	};
	void __iomem *idr; /* Interrupt Detect Register pointer */

112 113 114
	int buffer_offset;
	struct enetc_xdp_data xdp;

115 116 117
	struct enetc_ring_stats stats;

	dma_addr_t bd_dma_base;
118
	u8 tsd_enable; /* Time specific departure */
119
	bool ext_en; /* enable h/w descriptor extensions */
120 121 122 123

	/* DMA buffer for TSO headers */
	char *tso_headers;
	dma_addr_t tso_headers_dma;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
} ____cacheline_aligned_in_smp;

static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
{
	if (unlikely(++*i == bdr->bd_count))
		*i = 0;
}

static inline int enetc_bd_unused(struct enetc_bdr *bdr)
{
	if (bdr->next_to_clean > bdr->next_to_use)
		return bdr->next_to_clean - bdr->next_to_use - 1;

	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
}

140 141 142 143 144 145 146 147
static inline int enetc_swbd_unused(struct enetc_bdr *bdr)
{
	if (bdr->next_to_clean > bdr->next_to_alloc)
		return bdr->next_to_clean - bdr->next_to_alloc - 1;

	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_alloc - 1;
}

148 149 150 151 152 153
/* Control BD ring */
#define ENETC_CBDR_DEFAULT_SIZE	64
struct enetc_cbdr {
	void *bd_base; /* points to Rx or Tx BD ring */
	void __iomem *pir;
	void __iomem *cir;
154
	void __iomem *mr; /* mode register */
155 156 157 158 159 160

	int bd_count; /* # of BDs */
	int next_to_use;
	int next_to_clean;

	dma_addr_t bd_dma_base;
161
	struct device *dma_dev;
162 163 164
};

#define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
C
Claudiu Manoil 已提交
165 166 167

static inline union enetc_rx_bd *enetc_rxbd(struct enetc_bdr *rx_ring, int i)
{
168 169 170 171 172 173 174
	int hw_idx = i;

#ifdef CONFIG_FSL_ENETC_PTP_CLOCK
	if (rx_ring->ext_en)
		hw_idx = 2 * i;
#endif
	return &(((union enetc_rx_bd *)rx_ring->bd_base)[hw_idx]);
C
Claudiu Manoil 已提交
175 176
}

177 178
static inline void enetc_rxbd_next(struct enetc_bdr *rx_ring,
				   union enetc_rx_bd **old_rxbd, int *old_index)
C
Claudiu Manoil 已提交
179
{
180 181 182 183 184
	union enetc_rx_bd *new_rxbd = *old_rxbd;
	int new_index = *old_index;

	new_rxbd++;

185 186
#ifdef CONFIG_FSL_ENETC_PTP_CLOCK
	if (rx_ring->ext_en)
187
		new_rxbd++;
188
#endif
C
Claudiu Manoil 已提交
189

190 191 192 193 194 195 196
	if (unlikely(++new_index == rx_ring->bd_count)) {
		new_rxbd = rx_ring->bd_base;
		new_index = 0;
	}

	*old_rxbd = new_rxbd;
	*old_index = new_index;
C
Claudiu Manoil 已提交
197
}
198

199 200 201 202 203
static inline union enetc_rx_bd *enetc_rxbd_ext(union enetc_rx_bd *rxbd)
{
	return ++rxbd;
}

204 205 206 207 208 209
struct enetc_msg_swbd {
	void *vaddr;
	dma_addr_t dma;
	int size;
};

210 211
#define ENETC_REV1	0x1
enum enetc_errata {
212 213
	ENETC_ERR_VLAN_ISOL	= BIT(0),
	ENETC_ERR_UCMCSWP	= BIT(1),
214 215
};

216
#define ENETC_SI_F_QBV BIT(0)
217
#define ENETC_SI_F_PSFP BIT(1)
218

219 220 221 222 223 224 225 226 227 228 229 230
/* PCI IEP device data */
struct enetc_si {
	struct pci_dev *pdev;
	struct enetc_hw hw;
	enum enetc_errata errata;

	struct net_device *ndev; /* back ref. */

	struct enetc_cbdr cbd_ring;

	int num_rx_rings; /* how many rings are available in the SI */
	int num_tx_rings;
C
Claudiu Manoil 已提交
231 232
	int num_fs_entries;
	int num_rss; /* number of RSS buckets */
233
	unsigned short pad;
234
	int hw_features;
235 236 237 238 239 240 241 242 243 244 245 246 247 248
};

#define ENETC_SI_ALIGN	32

static inline void *enetc_si_priv(const struct enetc_si *si)
{
	return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
}

static inline bool enetc_si_is_pf(struct enetc_si *si)
{
	return !!(si->hw.port);
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static inline int enetc_pf_to_port(struct pci_dev *pf_pdev)
{
	switch (pf_pdev->devfn) {
	case 0:
		return 0;
	case 1:
		return 1;
	case 2:
		return 2;
	case 6:
		return 3;
	default:
		return -1;
	}
}

265 266 267 268 269 270
#define ENETC_MAX_NUM_TXQS	8
#define ENETC_INT_NAME_MAX	(IFNAMSIZ + 8)

struct enetc_int_vector {
	void __iomem *rbier;
	void __iomem *tbier_base;
271
	void __iomem *ricr1;
272 273
	unsigned long tx_rings_map;
	int count_tx_rings;
274
	u32 rx_ictt;
275 276 277 278
	u16 comp_cnt;
	bool rx_dim_en, rx_napi_work;
	struct napi_struct napi ____cacheline_aligned_in_smp;
	struct dim rx_dim ____cacheline_aligned_in_smp;
279 280
	char name[ENETC_INT_NAME_MAX];

281
	struct enetc_bdr rx_ring;
282
	struct enetc_bdr tx_ring[];
283
} ____cacheline_aligned_in_smp;
284

C
Claudiu Manoil 已提交
285 286 287 288 289
struct enetc_cls_rule {
	struct ethtool_rx_flow_spec fs;
	int used;
};

290
#define ENETC_MAX_BDR_INT	2 /* fixed to max # of available cpus */
291 292 293 294 295 296 297
struct psfp_cap {
	u32 max_streamid;
	u32 max_psfp_filter;
	u32 max_psfp_gate;
	u32 max_psfp_gatelist;
	u32 max_psfp_meter;
};
298

299
#define ENETC_F_TX_TSTAMP_MASK	0xff
300 301
/* TODO: more hardware offloads */
enum enetc_active_offloads {
302
	/* 8 bits reserved for TX timestamp types (hwtstamp_tx_types) */
303 304
	ENETC_F_TX_TSTAMP		= BIT(0),
	ENETC_F_TX_ONESTEP_SYNC_TSTAMP	= BIT(1),
305

306 307 308 309 310 311 312
	ENETC_F_RX_TSTAMP		= BIT(8),
	ENETC_F_QBV			= BIT(9),
	ENETC_F_QCI			= BIT(10),
};

enum enetc_flags_bit {
	ENETC_TX_ONESTEP_TSTAMP_IN_PROGRESS = 0,
313 314
};

315 316 317 318 319 320 321
/* interrupt coalescing modes */
enum enetc_ic_mode {
	/* one interrupt per frame */
	ENETC_IC_NONE = 0,
	/* activated when int coalescing time is set to a non-0 value */
	ENETC_IC_RX_MANUAL = BIT(0),
	ENETC_IC_TX_MANUAL = BIT(1),
322 323
	/* use dynamic interrupt moderation */
	ENETC_IC_RX_ADAPTIVE = BIT(2),
324 325 326 327
};

#define ENETC_RXIC_PKTTHR	min_t(u32, 256, ENETC_RX_RING_DEFAULT_SIZE / 2)
#define ENETC_TXIC_PKTTHR	min_t(u32, 128, ENETC_TX_RING_DEFAULT_SIZE / 2)
328
#define ENETC_TXIC_TIMETHR	enetc_usecs_to_cycles(600)
329

330 331 332 333 334 335 336 337 338 339 340
struct enetc_ndev_priv {
	struct net_device *ndev;
	struct device *dev; /* dma-mapping device */
	struct enetc_si *si;

	int bdr_int_num; /* number of Rx/Tx ring interrupts */
	struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
	u16 num_rx_rings, num_tx_rings;
	u16 rx_bd_count, tx_bd_count;

	u16 msg_enable;
341
	enum enetc_active_offloads active_offloads;
342

343 344
	u32 speed; /* store speed for compare update pspeed */

345
	struct enetc_bdr **xdp_tx_ring;
346 347 348
	struct enetc_bdr *tx_ring[16];
	struct enetc_bdr *rx_ring[16];

C
Claudiu Manoil 已提交
349 350
	struct enetc_cls_rule *cls_rules;

351 352
	struct psfp_cap psfp_cap;

353
	struct phylink *phylink;
354 355
	int ic_mode;
	u32 tx_ictt;
356 357

	struct bpf_prog *xdp_prog;
358 359 360 361 362

	unsigned long flags;

	struct work_struct	tx_onestep_tstamp;
	struct sk_buff_head	tx_skbs;
363 364
};

365 366 367 368 369 370 371 372
/* Messaging */

/* VF-PF set primary MAC address message format */
struct enetc_msg_cmd_set_primary_mac {
	struct enetc_msg_cmd_header header;
	struct sockaddr mac;
};

373 374 375 376
#define ENETC_CBD(R, i)	(&(((struct enetc_cbd *)((R).bd_base))[i]))

#define ENETC_CBDR_TIMEOUT	1000 /* usecs */

377 378 379
/* PTP driver exports */
extern int enetc_phc_index;

380 381 382 383 384 385 386 387 388
/* SI common */
int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
void enetc_pci_remove(struct pci_dev *pdev);
int enetc_alloc_msix(struct enetc_ndev_priv *priv);
void enetc_free_msix(struct enetc_ndev_priv *priv);
void enetc_get_si_caps(struct enetc_si *si);
void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
void enetc_free_si_resources(struct enetc_ndev_priv *priv);
389
int enetc_configure_si(struct enetc_ndev_priv *priv);
390 391 392

int enetc_open(struct net_device *ndev);
int enetc_close(struct net_device *ndev);
393 394
void enetc_start(struct net_device *ndev);
void enetc_stop(struct net_device *ndev);
395 396
netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
struct net_device_stats *enetc_get_stats(struct net_device *ndev);
397
void enetc_set_features(struct net_device *ndev, netdev_features_t features);
398
int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
399
int enetc_setup_tc_mqprio(struct net_device *ndev, void *type_data);
400
int enetc_setup_bpf(struct net_device *dev, struct netdev_bpf *xdp);
401 402
int enetc_xdp_xmit(struct net_device *ndev, int num_frames,
		   struct xdp_frame **frames, u32 flags);
403

404 405 406 407
/* ethtool */
void enetc_set_ethtool_ops(struct net_device *ndev);

/* control buffer descriptor ring (CBDR) */
408
int enetc_setup_cbdr(struct device *dev, struct enetc_hw *hw, int bd_count,
409
		     struct enetc_cbdr *cbdr);
410
void enetc_teardown_cbdr(struct enetc_cbdr *cbdr);
411 412 413
int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
			    char *mac_addr, int si_map);
int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
C
Claudiu Manoil 已提交
414 415 416 417 418
int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
		       int index);
void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes);
int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
419 420
int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static inline void *enetc_cbd_alloc_data_mem(struct enetc_si *si,
					     struct enetc_cbd *cbd,
					     int size, dma_addr_t *dma,
					     void **data_align)
{
	struct enetc_cbdr *ring = &si->cbd_ring;
	dma_addr_t dma_align;
	void *data;

	data = dma_alloc_coherent(ring->dma_dev,
				  size + ENETC_CBD_DATA_MEM_ALIGN,
				  dma, GFP_KERNEL);
	if (!data) {
		dev_err(ring->dma_dev, "CBD alloc data memory failed!\n");
		return NULL;
	}

	dma_align = ALIGN(*dma, ENETC_CBD_DATA_MEM_ALIGN);
	*data_align = PTR_ALIGN(data, ENETC_CBD_DATA_MEM_ALIGN);

	cbd->addr[0] = cpu_to_le32(lower_32_bits(dma_align));
	cbd->addr[1] = cpu_to_le32(upper_32_bits(dma_align));
	cbd->length = cpu_to_le16(size);

	return data;
}

static inline void enetc_cbd_free_data_mem(struct enetc_si *si, int size,
					   void *data, dma_addr_t *dma)
{
	struct enetc_cbdr *ring = &si->cbd_ring;

	dma_free_coherent(ring->dma_dev, size + ENETC_CBD_DATA_MEM_ALIGN,
			  data, *dma);
}

457 458 459
void enetc_reset_ptcmsdur(struct enetc_hw *hw);
void enetc_set_ptcmsdur(struct enetc_hw *hw, u32 *queue_max_sdu);

460
#ifdef CONFIG_FSL_ENETC_QOS
461
int enetc_qos_query_caps(struct net_device *ndev, void *type_data);
462
int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
463
void enetc_sched_speed_set(struct enetc_ndev_priv *priv, int speed);
464
int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
465
int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
466 467 468 469 470
int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
			    void *cb_priv);
int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data);
int enetc_psfp_init(struct enetc_ndev_priv *priv);
int enetc_psfp_clean(struct enetc_ndev_priv *priv);
471
int enetc_set_psfp(struct net_device *ndev, bool en);
472 473 474

static inline void enetc_get_max_cap(struct enetc_ndev_priv *priv)
{
475
	struct enetc_hw *hw = &priv->si->hw;
476 477
	u32 reg;

478
	reg = enetc_port_rd(hw, ENETC_PSIDCAPR);
479 480
	priv->psfp_cap.max_streamid = reg & ENETC_PSIDCAPR_MSK;
	/* Port stream filter capability */
481
	reg = enetc_port_rd(hw, ENETC_PSFCAPR);
482 483
	priv->psfp_cap.max_psfp_filter = reg & ENETC_PSFCAPR_MSK;
	/* Port stream gate capability */
484
	reg = enetc_port_rd(hw, ENETC_PSGCAPR);
485 486 487
	priv->psfp_cap.max_psfp_gate = (reg & ENETC_PSGCAPR_SGIT_MSK);
	priv->psfp_cap.max_psfp_gatelist = (reg & ENETC_PSGCAPR_GCL_MSK) >> 16;
	/* Port flow meter capability */
488
	reg = enetc_port_rd(hw, ENETC_PFMCAPR);
489 490 491
	priv->psfp_cap.max_psfp_meter = reg & ENETC_PFMCAPR_MSK;
}

492
static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
493
{
494 495 496 497 498 499 500 501 502
	struct enetc_hw *hw = &priv->si->hw;
	int err;

	enetc_get_max_cap(priv);

	err = enetc_psfp_init(priv);
	if (err)
		return err;

503 504 505
	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) |
		 ENETC_PPSFPMR_PSFPEN | ENETC_PPSFPMR_VS |
		 ENETC_PPSFPMR_PVC | ENETC_PPSFPMR_PVZC);
506 507

	return 0;
508 509
}

510
static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
511
{
512 513 514 515 516 517 518
	struct enetc_hw *hw = &priv->si->hw;
	int err;

	err = enetc_psfp_clean(priv);
	if (err)
		return err;

519 520 521
	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) &
		 ~ENETC_PPSFPMR_PSFPEN & ~ENETC_PPSFPMR_VS &
		 ~ENETC_PPSFPMR_PVC & ~ENETC_PPSFPMR_PVZC);
522 523 524 525

	memset(&priv->psfp_cap, 0, sizeof(struct psfp_cap));

	return 0;
526
}
527

528
#else
529
#define enetc_qos_query_caps(ndev, type_data) -EOPNOTSUPP
530
#define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
531
#define enetc_sched_speed_set(priv, speed) (void)0
532
#define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
533
#define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
534 535 536
#define enetc_setup_tc_psfp(ndev, type_data) -EOPNOTSUPP
#define enetc_setup_tc_block_cb NULL

537 538 539
#define enetc_get_max_cap(p)		\
	memset(&((p)->psfp_cap), 0, sizeof(struct psfp_cap))

540 541 542 543 544 545 546 547 548
static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
{
	return 0;
}

static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
{
	return 0;
}
549 550 551 552 553

static inline int enetc_set_psfp(struct net_device *ndev, bool en)
{
	return 0;
}
554
#endif