blob: decc62709acd03773eb751f3c25f65d6ccada881 [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];
44
45 /*
46 * An array (with nr_chips elements) of which element [a]
47 * indicates which port on this switch should be used to
48 * send packets to that are destined for switch a. Can be
49 * NULL if there is only one switch chip.
50 */
51 s8 *rtable;
52};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053
54struct dsa_platform_data {
55 /*
56 * Reference to a Linux network interface that connects
Lennert Buytenheke84665c2009-03-20 09:52:09 +000057 * to the root switch chip of the tree.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058 */
59 struct device *netdev;
60
61 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +000062 * Info structs describing each of the switch chips
63 * connected via this network interface.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000064 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +000065 int nr_chips;
66 struct dsa_chip_data *chip;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067};
68
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070069struct dsa_device_ops;
70
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000071struct dsa_switch_tree {
72 /*
73 * Configuration data for the platform device that owns
74 * this dsa switch tree instance.
75 */
76 struct dsa_platform_data *pd;
Lennert Buytenhekcf85d082008-10-07 13:45:02 +000077
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000078 /*
79 * Reference to network device to use, and which tagging
80 * protocol to use.
81 */
82 struct net_device *master_netdev;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070083 const struct dsa_device_ops *ops;
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000084 __be16 tag_protocol;
85
86 /*
87 * The switch and port to which the CPU is attached.
88 */
89 s8 cpu_switch;
90 s8 cpu_port;
91
92 /*
93 * Link state polling.
94 */
95 int link_poll_needed;
96 struct work_struct link_poll_work;
97 struct timer_list link_poll_timer;
98
99 /*
100 * Data for the individual switch chips.
101 */
102 struct dsa_switch *ds[DSA_MAX_SWITCHES];
103};
104
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000105struct dsa_switch {
106 /*
107 * Parent switch tree, and switch index.
108 */
109 struct dsa_switch_tree *dst;
110 int index;
111
112 /*
113 * Configuration data for this switch.
114 */
115 struct dsa_chip_data *pd;
116
117 /*
118 * The used switch driver.
119 */
120 struct dsa_switch_driver *drv;
121
122 /*
123 * Reference to mii bus to use.
124 */
125 struct mii_bus *master_mii_bus;
126
127 /*
128 * Slave mii_bus and devices for the individual ports.
129 */
130 u32 dsa_port_mask;
131 u32 phys_port_mask;
132 struct mii_bus *slave_mii_bus;
133 struct net_device *ports[DSA_MAX_PORTS];
134};
135
136static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
137{
138 return !!(ds->index == ds->dst->cpu_switch && p == ds->dst->cpu_port);
139}
140
141static inline u8 dsa_upstream_port(struct dsa_switch *ds)
142{
143 struct dsa_switch_tree *dst = ds->dst;
144
145 /*
146 * If this is the root switch (i.e. the switch that connects
147 * to the CPU), return the cpu port number on this switch.
148 * Else return the (DSA) port number that connects to the
149 * switch that is one hop closer to the cpu.
150 */
151 if (dst->cpu_switch == ds->index)
152 return dst->cpu_port;
153 else
154 return ds->pd->rtable[dst->cpu_switch];
155}
156
157struct dsa_switch_driver {
158 struct list_head list;
159
160 __be16 tag_protocol;
161 int priv_size;
162
163 /*
164 * Probing and setup.
165 */
166 char *(*probe)(struct mii_bus *bus, int sw_addr);
167 int (*setup)(struct dsa_switch *ds);
168 int (*set_addr)(struct dsa_switch *ds, u8 *addr);
169
170 /*
171 * Access to the switch's PHY registers.
172 */
173 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
174 int (*phy_write)(struct dsa_switch *ds, int port,
175 int regnum, u16 val);
176
177 /*
178 * Link state polling and IRQ handling.
179 */
180 void (*poll_link)(struct dsa_switch *ds);
181
182 /*
183 * ethtool hardware statistics.
184 */
185 void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
186 void (*get_ethtool_stats)(struct dsa_switch *ds,
187 int port, uint64_t *data);
188 int (*get_sset_count)(struct dsa_switch *ds);
189};
190
191void register_switch_driver(struct dsa_switch_driver *type);
192void unregister_switch_driver(struct dsa_switch_driver *type);
193
Florian Fainelli7fa857e2014-04-28 11:14:27 -0700194static inline void *ds_to_priv(struct dsa_switch *ds)
195{
196 return (void *)(ds + 1);
197}
198
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199#endif