sja1105.h 10.9 KB
Newer Older
1 2
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3 4 5 6 7
 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
 */
#ifndef _SJA1105_H
#define _SJA1105_H

8 9
#include <linux/ptp_clock_kernel.h>
#include <linux/timecounter.h>
10
#include <linux/dsa/sja1105.h>
11
#include <linux/dsa/8021q.h>
12
#include <net/dsa.h>
13
#include <linux/mutex.h>
14 15 16
#include "sja1105_static_config.h"

#define SJA1105ET_FDB_BIN_SIZE		4
17 18 19 20
/* The hardware value is in multiples of 10 ms.
 * The passed parameter is in multiples of 1 ms.
 */
#define SJA1105_AGEING_TIME_MS(ms)	((ms) / 10)
21
#define SJA1105_NUM_L2_POLICERS		SJA1110_MAX_L2_POLICING_COUNT
22

23 24 25 26 27
typedef enum {
	SPI_READ = 0,
	SPI_WRITE = 1,
} sja1105_spi_rw_mode_t;

28
#include "sja1105_tas.h"
29
#include "sja1105_ptp.h"
30

31 32 33 34 35 36 37 38
enum sja1105_stats_area {
	MAC,
	HL1,
	HL2,
	ETHER,
	__MAX_SJA1105_STATS_AREA,
};

39 40 41 42 43
/* Keeps the different addresses between E/T and P/Q/R/S */
struct sja1105_regs {
	u64 device_id;
	u64 prod_id;
	u64 status;
44
	u64 port_control;
45
	u64 rgu;
46
	u64 vl_status;
47 48
	u64 config;
	u64 rmii_pll1;
49 50
	u64 ptppinst;
	u64 ptppindur;
51
	u64 ptp_control;
52
	u64 ptpclkval;
53
	u64 ptpclkrate;
54
	u64 ptpclkcorp;
55
	u64 ptpsyncts;
56
	u64 ptpschtm;
57 58 59 60 61 62 63 64 65 66 67 68 69
	u64 ptpegr_ts[SJA1105_MAX_NUM_PORTS];
	u64 pad_mii_tx[SJA1105_MAX_NUM_PORTS];
	u64 pad_mii_rx[SJA1105_MAX_NUM_PORTS];
	u64 pad_mii_id[SJA1105_MAX_NUM_PORTS];
	u64 cgu_idiv[SJA1105_MAX_NUM_PORTS];
	u64 mii_tx_clk[SJA1105_MAX_NUM_PORTS];
	u64 mii_rx_clk[SJA1105_MAX_NUM_PORTS];
	u64 mii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
	u64 mii_ext_rx_clk[SJA1105_MAX_NUM_PORTS];
	u64 rgmii_tx_clk[SJA1105_MAX_NUM_PORTS];
	u64 rmii_ref_clk[SJA1105_MAX_NUM_PORTS];
	u64 rmii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
	u64 stats[__MAX_SJA1105_STATS_AREA][SJA1105_MAX_NUM_PORTS];
70 71
};

72 73 74 75 76 77 78 79 80
enum {
	SJA1105_SPEED_AUTO,
	SJA1105_SPEED_10MBPS,
	SJA1105_SPEED_100MBPS,
	SJA1105_SPEED_1000MBPS,
	SJA1105_SPEED_2500MBPS,
	SJA1105_SPEED_MAX,
};

81 82 83 84 85 86 87
struct sja1105_info {
	u64 device_id;
	/* Needed for distinction between P and R, and between Q and S
	 * (since the parts with/without SGMII share the same
	 * switch core and device_id)
	 */
	u64 part_no;
88 89 90 91 92 93 94 95 96
	/* E/T and P/Q/R/S have partial timestamps of different sizes.
	 * They must be reconstructed on both families anyway to get the full
	 * 64-bit values back.
	 */
	int ptp_ts_bits;
	/* Also SPI commands are of different sizes to retrieve
	 * the egress timestamps.
	 */
	int ptpegr_ts_bytes;
97
	int num_cbs_shapers;
98
	int max_frame_mem;
99
	int num_ports;
100 101 102
	const struct sja1105_dynamic_table_ops *dyn_ops;
	const struct sja1105_table_ops *static_ops;
	const struct sja1105_regs *regs;
103 104 105 106 107 108
	/* Both E/T and P/Q/R/S have quirks when it comes to popping the S-Tag
	 * from double-tagged frames. E/T will pop it only when it's equal to
	 * TPID from the General Parameters Table, while P/Q/R/S will only
	 * pop it when it's equal to TPID2.
	 */
	u16 qinq_tpid;
109
	bool can_limit_mcast_flood;
110
	int (*reset_cmd)(struct dsa_switch *ds);
111
	int (*setup_rgmii_delay)(const void *ctx, int port);
112 113 114 115 116
	/* Prototypes from include/net/dsa.h */
	int (*fdb_add_cmd)(struct dsa_switch *ds, int port,
			   const unsigned char *addr, u16 vid);
	int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
			   const unsigned char *addr, u16 vid);
117 118
	void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
				enum packing_op op);
119
	int (*clocking_setup)(struct sja1105_private *priv);
120
	const char *name;
121 122 123 124 125
	bool supports_mii[SJA1105_MAX_NUM_PORTS];
	bool supports_rmii[SJA1105_MAX_NUM_PORTS];
	bool supports_rgmii[SJA1105_MAX_NUM_PORTS];
	bool supports_sgmii[SJA1105_MAX_NUM_PORTS];
	bool supports_2500basex[SJA1105_MAX_NUM_PORTS];
126
	const u64 port_speed[SJA1105_SPEED_MAX];
127 128
};

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
enum sja1105_key_type {
	SJA1105_KEY_BCAST,
	SJA1105_KEY_TC,
	SJA1105_KEY_VLAN_UNAWARE_VL,
	SJA1105_KEY_VLAN_AWARE_VL,
};

struct sja1105_key {
	enum sja1105_key_type type;

	union {
		/* SJA1105_KEY_TC */
		struct {
			int pcp;
		} tc;

		/* SJA1105_KEY_VLAN_UNAWARE_VL */
		/* SJA1105_KEY_VLAN_AWARE_VL */
		struct {
			u64 dmac;
			u16 vid;
			u16 pcp;
		} vl;
	};
};

155 156 157
enum sja1105_rule_type {
	SJA1105_RULE_BCAST_POLICER,
	SJA1105_RULE_TC_POLICER,
158 159 160 161 162 163 164
	SJA1105_RULE_VL,
};

enum sja1105_vl_type {
	SJA1105_VL_NONCRITICAL,
	SJA1105_VL_RATE_CONSTRAINED,
	SJA1105_VL_TIME_TRIGGERED,
165 166 167 168 169 170
};

struct sja1105_rule {
	struct list_head list;
	unsigned long cookie;
	unsigned long port_mask;
171
	struct sja1105_key key;
172 173
	enum sja1105_rule_type type;

174
	/* Action */
175 176 177 178 179 180 181 182 183 184
	union {
		/* SJA1105_RULE_BCAST_POLICER */
		struct {
			int sharindx;
		} bcast_pol;

		/* SJA1105_RULE_TC_POLICER */
		struct {
			int sharindx;
		} tc_pol;
185 186 187 188

		/* SJA1105_RULE_VL */
		struct {
			enum sja1105_vl_type type;
189 190 191 192 193 194 195 196 197
			unsigned long destports;
			int sharindx;
			int maxlen;
			int ipv;
			u64 base_time;
			u64 cycle_time;
			int num_entries;
			struct action_gate_entry *entries;
			struct flow_stats stats;
198
		} vl;
199 200 201 202 203 204
	};
};

struct sja1105_flow_block {
	struct list_head rules;
	bool l2_policer_used[SJA1105_NUM_L2_POLICERS];
205
	int num_virtual_links;
206 207
};

208 209 210 211 212 213 214 215
struct sja1105_bridge_vlan {
	struct list_head list;
	int port;
	u16 vid;
	bool pvid;
	bool untagged;
};

216 217
enum sja1105_vlan_state {
	SJA1105_VLAN_UNAWARE,
218
	SJA1105_VLAN_BEST_EFFORT,
219 220 221
	SJA1105_VLAN_FILTERING_FULL,
};

222 223
struct sja1105_private {
	struct sja1105_static_config static_config;
224 225
	bool rgmii_rx_delay[SJA1105_MAX_NUM_PORTS];
	bool rgmii_tx_delay[SJA1105_MAX_NUM_PORTS];
226
	phy_interface_t phy_mode[SJA1105_MAX_NUM_PORTS];
227
	bool fixed_link[SJA1105_MAX_NUM_PORTS];
228
	bool best_effort_vlan_filtering;
229
	unsigned long learn_ena;
230 231
	unsigned long ucast_egress_floods;
	unsigned long bcast_egress_floods;
232
	const struct sja1105_info *info;
233
	size_t max_xfer_len;
234 235 236
	struct gpio_desc *reset_gpio;
	struct spi_device *spidev;
	struct dsa_switch *ds;
237 238
	struct list_head dsa_8021q_vlans;
	struct list_head bridge_vlans;
239
	struct sja1105_flow_block flow_block;
240
	struct sja1105_port ports[SJA1105_MAX_NUM_PORTS];
241 242 243 244
	/* Serializes transmission of management frames so that
	 * the switch doesn't confuse them with one another.
	 */
	struct mutex mgmt_lock;
245
	struct dsa_8021q_context *dsa_8021q_ctx;
246
	enum sja1105_vlan_state vlan_state;
247
	struct devlink_region **regions;
248
	struct sja1105_cbs_entry *cbs;
249
	struct sja1105_tagger_data tagger_data;
250
	struct sja1105_ptp_data ptp_data;
251
	struct sja1105_tas_data tas_data;
252 253 254 255 256 257 258 259 260 261
};

#include "sja1105_dynamic_config.h"

struct sja1105_spi_message {
	u64 access;
	u64 read_count;
	u64 address;
};

262
/* From sja1105_main.c */
263 264 265 266 267
enum sja1105_reset_reason {
	SJA1105_VLAN_FILTERING = 0,
	SJA1105_RX_HWTSTAMPING,
	SJA1105_AGEING_TIME,
	SJA1105_SCHEDULING,
268
	SJA1105_BEST_EFFORT_POLICING,
269
	SJA1105_VIRTUAL_LINKS,
270 271 272 273
};

int sja1105_static_config_reload(struct sja1105_private *priv,
				 enum sja1105_reset_reason reason);
274 275
int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
			   struct netlink_ext_ack *extack);
276 277
void sja1105_frame_memory_partitioning(struct sja1105_private *priv);

278 279 280 281 282 283 284
/* From sja1105_devlink.c */
int sja1105_devlink_setup(struct dsa_switch *ds);
void sja1105_devlink_teardown(struct dsa_switch *ds);
int sja1105_devlink_param_get(struct dsa_switch *ds, u32 id,
			      struct devlink_param_gset_ctx *ctx);
int sja1105_devlink_param_set(struct dsa_switch *ds, u32 id,
			      struct devlink_param_gset_ctx *ctx);
285 286 287
int sja1105_devlink_info_get(struct dsa_switch *ds,
			     struct devlink_info_req *req,
			     struct netlink_ext_ack *extack);
288

289
/* From sja1105_spi.c */
290 291
int sja1105_xfer_buf(const struct sja1105_private *priv,
		     sja1105_spi_rw_mode_t rw, u64 reg_addr,
292
		     u8 *buf, size_t len);
293
int sja1105_xfer_u32(const struct sja1105_private *priv,
294 295
		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u32 *value,
		     struct ptp_system_timestamp *ptp_sts);
296
int sja1105_xfer_u64(const struct sja1105_private *priv,
297 298
		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u64 *value,
		     struct ptp_system_timestamp *ptp_sts);
299 300
int static_config_buf_prepare_for_upload(struct sja1105_private *priv,
					 void *config_buf, int buf_len);
301
int sja1105_static_config_upload(struct sja1105_private *priv);
302 303
int sja1105_inhibit_tx(const struct sja1105_private *priv,
		       unsigned long port_bitmap, bool tx_inhibited);
304

305 306 307 308 309 310
extern const struct sja1105_info sja1105e_info;
extern const struct sja1105_info sja1105t_info;
extern const struct sja1105_info sja1105p_info;
extern const struct sja1105_info sja1105q_info;
extern const struct sja1105_info sja1105r_info;
extern const struct sja1105_info sja1105s_info;
311 312 313 314
extern const struct sja1105_info sja1110a_info;
extern const struct sja1105_info sja1110b_info;
extern const struct sja1105_info sja1110c_info;
extern const struct sja1105_info sja1110d_info;
315 316 317 318 319 320 321 322 323 324 325 326

/* From sja1105_clocking.c */

typedef enum {
	XMII_MAC = 0,
	XMII_PHY = 1,
} sja1105_mii_role_t;

typedef enum {
	XMII_MODE_MII		= 0,
	XMII_MODE_RMII		= 1,
	XMII_MODE_RGMII		= 2,
327
	XMII_MODE_SGMII		= 3,
328 329
} sja1105_phy_interface_t;

330
int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
331
int sja1110_setup_rgmii_delay(const void *ctx, int port);
332 333
int sja1105_clocking_setup_port(struct sja1105_private *priv, int port);
int sja1105_clocking_setup(struct sja1105_private *priv);
334
int sja1110_clocking_setup(struct sja1105_private *priv);
335

336 337 338 339 340
/* From sja1105_ethtool.c */
void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
void sja1105_get_strings(struct dsa_switch *ds, int port,
			 u32 stringset, u8 *data);
int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset);
341

342
/* From sja1105_dynamic_config.c */
343 344 345 346 347 348 349
int sja1105_dynamic_config_read(struct sja1105_private *priv,
				enum sja1105_blk_idx blk_idx,
				int index, void *entry);
int sja1105_dynamic_config_write(struct sja1105_private *priv,
				 enum sja1105_blk_idx blk_idx,
				 int index, void *entry, bool keep);

350 351 352 353 354
enum sja1105_iotag {
	SJA1105_C_TAG = 0, /* Inner VLAN header */
	SJA1105_S_TAG = 1, /* Outer VLAN header */
};

355 356 357 358 359 360 361 362 363 364 365 366
enum sja1110_vlan_type {
	SJA1110_VLAN_INVALID = 0,
	SJA1110_VLAN_C_TAG = 1, /* Single inner VLAN tag */
	SJA1110_VLAN_S_TAG = 2, /* Single outer VLAN tag */
	SJA1110_VLAN_D_TAG = 3, /* Double tagged, use outer tag for lookup */
};

enum sja1110_shaper_type {
	SJA1110_LEAKY_BUCKET_SHAPER = 0,
	SJA1110_CBS_SHAPER = 1,
};

367 368 369 370 371 372 373 374 375
u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid);
int sja1105et_fdb_add(struct dsa_switch *ds, int port,
		      const unsigned char *addr, u16 vid);
int sja1105et_fdb_del(struct dsa_switch *ds, int port,
		      const unsigned char *addr, u16 vid);
int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
			const unsigned char *addr, u16 vid);
int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
			const unsigned char *addr, u16 vid);
376

377 378 379 380 381
/* From sja1105_flower.c */
int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
			   struct flow_cls_offload *cls, bool ingress);
int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
			   struct flow_cls_offload *cls, bool ingress);
382 383
int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
			     struct flow_cls_offload *cls, bool ingress);
384 385
void sja1105_flower_setup(struct dsa_switch *ds);
void sja1105_flower_teardown(struct dsa_switch *ds);
386 387
struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
				       unsigned long cookie);
388

389
#endif