blob: 597875d3f69e1736bd3601505c551cd9cfe79252 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#ifndef __LINUX_NET_DSA_H
12#define __LINUX_NET_DSA_H
13
Axel Linea1f51b2011-11-30 22:07:18 +000014#include <linux/if_ether.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000015#include <linux/list.h>
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000016#include <linux/timer.h>
17#include <linux/workqueue.h>
Florian Fainellifa981d92014-08-27 17:04:49 -070018#include <linux/of.h>
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000019
Lennert Buytenheke84665c2009-03-20 09:52:09 +000020#define DSA_MAX_SWITCHES 4
21#define DSA_MAX_PORTS 12
22
23struct dsa_chip_data {
24 /*
25 * How to access the switch configuration registers.
26 */
27 struct device *mii_bus;
28 int sw_addr;
29
Florian Fainellifa981d92014-08-27 17:04:49 -070030 /* Device tree node pointer for this specific switch chip
31 * used during switch setup in case additional properties
32 * and resources needs to be used
33 */
34 struct device_node *of_node;
35
Lennert Buytenheke84665c2009-03-20 09:52:09 +000036 /*
37 * The names of the switch's ports. Use "cpu" to
38 * designate the switch port that the cpu is connected to,
39 * "dsa" to indicate that this port is a DSA link to
40 * another switch, NULL to indicate the port is unused,
41 * or any other string to indicate this is a physical port.
42 */
43 char *port_names[DSA_MAX_PORTS];
Florian Fainellibd474972014-08-27 17:04:50 -070044 struct device_node *port_dn[DSA_MAX_PORTS];
Lennert Buytenheke84665c2009-03-20 09:52:09 +000045
46 /*
47 * An array (with nr_chips elements) of which element [a]
48 * indicates which port on this switch should be used to
49 * send packets to that are destined for switch a. Can be
50 * NULL if there is only one switch chip.
51 */
52 s8 *rtable;
53};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000054
55struct dsa_platform_data {
56 /*
57 * Reference to a Linux network interface that connects
Lennert Buytenheke84665c2009-03-20 09:52:09 +000058 * to the root switch chip of the tree.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000059 */
60 struct device *netdev;
61
62 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +000063 * Info structs describing each of the switch chips
64 * connected via this network interface.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +000066 int nr_chips;
67 struct dsa_chip_data *chip;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068};
69
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070070struct dsa_device_ops;
71
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000072struct dsa_switch_tree {
73 /*
74 * Configuration data for the platform device that owns
75 * this dsa switch tree instance.
76 */
77 struct dsa_platform_data *pd;
Lennert Buytenhekcf85d082008-10-07 13:45:02 +000078
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000079 /*
80 * Reference to network device to use, and which tagging
81 * protocol to use.
82 */
83 struct net_device *master_netdev;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070084 const struct dsa_device_ops *ops;
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000085 __be16 tag_protocol;
86
87 /*
88 * The switch and port to which the CPU is attached.
89 */
90 s8 cpu_switch;
91 s8 cpu_port;
92
93 /*
94 * Link state polling.
95 */
96 int link_poll_needed;
97 struct work_struct link_poll_work;
98 struct timer_list link_poll_timer;
99
100 /*
101 * Data for the individual switch chips.
102 */
103 struct dsa_switch *ds[DSA_MAX_SWITCHES];
104};
105
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000106struct dsa_switch {
107 /*
108 * Parent switch tree, and switch index.
109 */
110 struct dsa_switch_tree *dst;
111 int index;
112
113 /*
114 * Configuration data for this switch.
115 */
116 struct dsa_chip_data *pd;
117
118 /*
119 * The used switch driver.
120 */
121 struct dsa_switch_driver *drv;
122
123 /*
124 * Reference to mii bus to use.
125 */
126 struct mii_bus *master_mii_bus;
127
128 /*
129 * Slave mii_bus and devices for the individual ports.
130 */
131 u32 dsa_port_mask;
132 u32 phys_port_mask;
133 struct mii_bus *slave_mii_bus;
134 struct net_device *ports[DSA_MAX_PORTS];
135};
136
137static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
138{
139 return !!(ds->index == ds->dst->cpu_switch && p == ds->dst->cpu_port);
140}
141
142static inline u8 dsa_upstream_port(struct dsa_switch *ds)
143{
144 struct dsa_switch_tree *dst = ds->dst;
145
146 /*
147 * If this is the root switch (i.e. the switch that connects
148 * to the CPU), return the cpu port number on this switch.
149 * Else return the (DSA) port number that connects to the
150 * switch that is one hop closer to the cpu.
151 */
152 if (dst->cpu_switch == ds->index)
153 return dst->cpu_port;
154 else
155 return ds->pd->rtable[dst->cpu_switch];
156}
157
158struct dsa_switch_driver {
159 struct list_head list;
160
161 __be16 tag_protocol;
162 int priv_size;
163
164 /*
165 * Probing and setup.
166 */
167 char *(*probe)(struct mii_bus *bus, int sw_addr);
168 int (*setup)(struct dsa_switch *ds);
169 int (*set_addr)(struct dsa_switch *ds, u8 *addr);
170
171 /*
172 * Access to the switch's PHY registers.
173 */
174 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
175 int (*phy_write)(struct dsa_switch *ds, int port,
176 int regnum, u16 val);
177
178 /*
179 * Link state polling and IRQ handling.
180 */
181 void (*poll_link)(struct dsa_switch *ds);
182
183 /*
184 * ethtool hardware statistics.
185 */
186 void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
187 void (*get_ethtool_stats)(struct dsa_switch *ds,
188 int port, uint64_t *data);
189 int (*get_sset_count)(struct dsa_switch *ds);
190};
191
192void register_switch_driver(struct dsa_switch_driver *type);
193void unregister_switch_driver(struct dsa_switch_driver *type);
194
Florian Fainelli7fa857e2014-04-28 11:14:27 -0700195static inline void *ds_to_priv(struct dsa_switch *ds)
196{
197 return (void *)(ds + 1);
198}
199
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000200#endif