1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
8 #include <linux/ptp_clock_kernel.h>
9 #include <linux/timecounter.h>
10 #include <linux/dsa/sja1105.h>
11 #include <linux/dsa/8021q.h>
13 #include <linux/mutex.h>
14 #include "sja1105_static_config.h"
16 #define SJA1105_NUM_PORTS 5
17 #define SJA1105_MAX_NUM_PORTS SJA1105_NUM_PORTS
18 #define SJA1105_NUM_TC 8
19 #define SJA1105ET_FDB_BIN_SIZE 4
20 /* The hardware value is in multiples of 10 ms.
21 * The passed parameter is in multiples of 1 ms.
23 #define SJA1105_AGEING_TIME_MS(ms) ((ms) / 10)
24 #define SJA1105_NUM_L2_POLICERS 45
29 } sja1105_spi_rw_mode_t;
31 #include "sja1105_tas.h"
32 #include "sja1105_ptp.h"
34 enum sja1105_stats_area {
39 __MAX_SJA1105_STATS_AREA,
42 /* Keeps the different addresses between E/T and P/Q/R/S */
60 u64 ptpegr_ts[SJA1105_MAX_NUM_PORTS];
61 u64 pad_mii_tx[SJA1105_MAX_NUM_PORTS];
62 u64 pad_mii_rx[SJA1105_MAX_NUM_PORTS];
63 u64 pad_mii_id[SJA1105_MAX_NUM_PORTS];
64 u64 cgu_idiv[SJA1105_MAX_NUM_PORTS];
65 u64 mii_tx_clk[SJA1105_MAX_NUM_PORTS];
66 u64 mii_rx_clk[SJA1105_MAX_NUM_PORTS];
67 u64 mii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
68 u64 mii_ext_rx_clk[SJA1105_MAX_NUM_PORTS];
69 u64 rgmii_tx_clk[SJA1105_MAX_NUM_PORTS];
70 u64 rmii_ref_clk[SJA1105_MAX_NUM_PORTS];
71 u64 rmii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
72 u64 stats[__MAX_SJA1105_STATS_AREA][SJA1105_MAX_NUM_PORTS];
77 /* Needed for distinction between P and R, and between Q and S
78 * (since the parts with/without SGMII share the same
79 * switch core and device_id)
82 /* E/T and P/Q/R/S have partial timestamps of different sizes.
83 * They must be reconstructed on both families anyway to get the full
87 /* Also SPI commands are of different sizes to retrieve
88 * the egress timestamps.
93 const struct sja1105_dynamic_table_ops *dyn_ops;
94 const struct sja1105_table_ops *static_ops;
95 const struct sja1105_regs *regs;
96 /* Both E/T and P/Q/R/S have quirks when it comes to popping the S-Tag
97 * from double-tagged frames. E/T will pop it only when it's equal to
98 * TPID from the General Parameters Table, while P/Q/R/S will only
99 * pop it when it's equal to TPID2.
102 bool can_limit_mcast_flood;
103 int (*reset_cmd)(struct dsa_switch *ds);
104 int (*setup_rgmii_delay)(const void *ctx, int port);
105 /* Prototypes from include/net/dsa.h */
106 int (*fdb_add_cmd)(struct dsa_switch *ds, int port,
107 const unsigned char *addr, u16 vid);
108 int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
109 const unsigned char *addr, u16 vid);
110 void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
112 int (*clocking_setup)(struct sja1105_private *priv);
114 bool supports_mii[SJA1105_MAX_NUM_PORTS];
115 bool supports_rmii[SJA1105_MAX_NUM_PORTS];
116 bool supports_rgmii[SJA1105_MAX_NUM_PORTS];
117 bool supports_sgmii[SJA1105_MAX_NUM_PORTS];
118 bool supports_2500basex[SJA1105_MAX_NUM_PORTS];
121 enum sja1105_key_type {
124 SJA1105_KEY_VLAN_UNAWARE_VL,
125 SJA1105_KEY_VLAN_AWARE_VL,
129 enum sja1105_key_type type;
137 /* SJA1105_KEY_VLAN_UNAWARE_VL */
138 /* SJA1105_KEY_VLAN_AWARE_VL */
147 enum sja1105_rule_type {
148 SJA1105_RULE_BCAST_POLICER,
149 SJA1105_RULE_TC_POLICER,
153 enum sja1105_vl_type {
154 SJA1105_VL_NONCRITICAL,
155 SJA1105_VL_RATE_CONSTRAINED,
156 SJA1105_VL_TIME_TRIGGERED,
159 struct sja1105_rule {
160 struct list_head list;
161 unsigned long cookie;
162 unsigned long port_mask;
163 struct sja1105_key key;
164 enum sja1105_rule_type type;
168 /* SJA1105_RULE_BCAST_POLICER */
173 /* SJA1105_RULE_TC_POLICER */
178 /* SJA1105_RULE_VL */
180 enum sja1105_vl_type type;
181 unsigned long destports;
188 struct action_gate_entry *entries;
189 struct flow_stats stats;
194 struct sja1105_flow_block {
195 struct list_head rules;
196 bool l2_policer_used[SJA1105_NUM_L2_POLICERS];
197 int num_virtual_links;
200 struct sja1105_bridge_vlan {
201 struct list_head list;
208 enum sja1105_vlan_state {
209 SJA1105_VLAN_UNAWARE,
210 SJA1105_VLAN_BEST_EFFORT,
211 SJA1105_VLAN_FILTERING_FULL,
214 struct sja1105_private {
215 struct sja1105_static_config static_config;
216 bool rgmii_rx_delay[SJA1105_MAX_NUM_PORTS];
217 bool rgmii_tx_delay[SJA1105_MAX_NUM_PORTS];
218 phy_interface_t phy_mode[SJA1105_MAX_NUM_PORTS];
219 bool best_effort_vlan_filtering;
220 unsigned long learn_ena;
221 unsigned long ucast_egress_floods;
222 unsigned long bcast_egress_floods;
223 const struct sja1105_info *info;
225 struct gpio_desc *reset_gpio;
226 struct spi_device *spidev;
227 struct dsa_switch *ds;
228 struct list_head dsa_8021q_vlans;
229 struct list_head bridge_vlans;
230 struct sja1105_flow_block flow_block;
231 struct sja1105_port ports[SJA1105_MAX_NUM_PORTS];
232 /* Serializes transmission of management frames so that
233 * the switch doesn't confuse them with one another.
235 struct mutex mgmt_lock;
236 struct dsa_8021q_context *dsa_8021q_ctx;
237 enum sja1105_vlan_state vlan_state;
238 struct devlink_region **regions;
239 struct sja1105_cbs_entry *cbs;
240 struct sja1105_tagger_data tagger_data;
241 struct sja1105_ptp_data ptp_data;
242 struct sja1105_tas_data tas_data;
245 #include "sja1105_dynamic_config.h"
247 struct sja1105_spi_message {
253 /* From sja1105_main.c */
254 enum sja1105_reset_reason {
255 SJA1105_VLAN_FILTERING = 0,
256 SJA1105_RX_HWTSTAMPING,
259 SJA1105_BEST_EFFORT_POLICING,
260 SJA1105_VIRTUAL_LINKS,
263 int sja1105_static_config_reload(struct sja1105_private *priv,
264 enum sja1105_reset_reason reason);
265 int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
266 struct netlink_ext_ack *extack);
267 void sja1105_frame_memory_partitioning(struct sja1105_private *priv);
269 /* From sja1105_devlink.c */
270 int sja1105_devlink_setup(struct dsa_switch *ds);
271 void sja1105_devlink_teardown(struct dsa_switch *ds);
272 int sja1105_devlink_param_get(struct dsa_switch *ds, u32 id,
273 struct devlink_param_gset_ctx *ctx);
274 int sja1105_devlink_param_set(struct dsa_switch *ds, u32 id,
275 struct devlink_param_gset_ctx *ctx);
276 int sja1105_devlink_info_get(struct dsa_switch *ds,
277 struct devlink_info_req *req,
278 struct netlink_ext_ack *extack);
280 /* From sja1105_spi.c */
281 int sja1105_xfer_buf(const struct sja1105_private *priv,
282 sja1105_spi_rw_mode_t rw, u64 reg_addr,
283 u8 *buf, size_t len);
284 int sja1105_xfer_u32(const struct sja1105_private *priv,
285 sja1105_spi_rw_mode_t rw, u64 reg_addr, u32 *value,
286 struct ptp_system_timestamp *ptp_sts);
287 int sja1105_xfer_u64(const struct sja1105_private *priv,
288 sja1105_spi_rw_mode_t rw, u64 reg_addr, u64 *value,
289 struct ptp_system_timestamp *ptp_sts);
290 int static_config_buf_prepare_for_upload(struct sja1105_private *priv,
291 void *config_buf, int buf_len);
292 int sja1105_static_config_upload(struct sja1105_private *priv);
293 int sja1105_inhibit_tx(const struct sja1105_private *priv,
294 unsigned long port_bitmap, bool tx_inhibited);
296 extern const struct sja1105_info sja1105e_info;
297 extern const struct sja1105_info sja1105t_info;
298 extern const struct sja1105_info sja1105p_info;
299 extern const struct sja1105_info sja1105q_info;
300 extern const struct sja1105_info sja1105r_info;
301 extern const struct sja1105_info sja1105s_info;
303 /* From sja1105_clocking.c */
308 } sja1105_mii_role_t;
315 } sja1105_phy_interface_t;
318 SJA1105_SPEED_10MBPS = 3,
319 SJA1105_SPEED_100MBPS = 2,
320 SJA1105_SPEED_1000MBPS = 1,
321 SJA1105_SPEED_AUTO = 0,
324 int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
325 int sja1105_clocking_setup_port(struct sja1105_private *priv, int port);
326 int sja1105_clocking_setup(struct sja1105_private *priv);
328 /* From sja1105_ethtool.c */
329 void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
330 void sja1105_get_strings(struct dsa_switch *ds, int port,
331 u32 stringset, u8 *data);
332 int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset);
334 /* From sja1105_dynamic_config.c */
335 int sja1105_dynamic_config_read(struct sja1105_private *priv,
336 enum sja1105_blk_idx blk_idx,
337 int index, void *entry);
338 int sja1105_dynamic_config_write(struct sja1105_private *priv,
339 enum sja1105_blk_idx blk_idx,
340 int index, void *entry, bool keep);
343 SJA1105_C_TAG = 0, /* Inner VLAN header */
344 SJA1105_S_TAG = 1, /* Outer VLAN header */
347 u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid);
348 int sja1105et_fdb_add(struct dsa_switch *ds, int port,
349 const unsigned char *addr, u16 vid);
350 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
351 const unsigned char *addr, u16 vid);
352 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
353 const unsigned char *addr, u16 vid);
354 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
355 const unsigned char *addr, u16 vid);
357 /* From sja1105_flower.c */
358 int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
359 struct flow_cls_offload *cls, bool ingress);
360 int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
361 struct flow_cls_offload *cls, bool ingress);
362 int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
363 struct flow_cls_offload *cls, bool ingress);
364 void sja1105_flower_setup(struct dsa_switch *ds);
365 void sja1105_flower_teardown(struct dsa_switch *ds);
366 struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
367 unsigned long cookie);