blob: 2d3835924dd23afa9141c88b5f778e8cea0610e3 [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>
Florian Fainelliec9436b2014-08-27 17:04:53 -070019#include <linux/phy.h>
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000020
Lennert Buytenheke84665c2009-03-20 09:52:09 +000021#define DSA_MAX_SWITCHES 4
22#define DSA_MAX_PORTS 12
23
24struct dsa_chip_data {
25 /*
26 * How to access the switch configuration registers.
27 */
28 struct device *mii_bus;
29 int sw_addr;
30
Florian Fainellifa981d92014-08-27 17:04:49 -070031 /* Device tree node pointer for this specific switch chip
32 * used during switch setup in case additional properties
33 * and resources needs to be used
34 */
35 struct device_node *of_node;
36
Lennert Buytenheke84665c2009-03-20 09:52:09 +000037 /*
38 * The names of the switch's ports. Use "cpu" to
39 * designate the switch port that the cpu is connected to,
40 * "dsa" to indicate that this port is a DSA link to
41 * another switch, NULL to indicate the port is unused,
42 * or any other string to indicate this is a physical port.
43 */
44 char *port_names[DSA_MAX_PORTS];
Florian Fainellibd474972014-08-27 17:04:50 -070045 struct device_node *port_dn[DSA_MAX_PORTS];
Lennert Buytenheke84665c2009-03-20 09:52:09 +000046
47 /*
48 * An array (with nr_chips elements) of which element [a]
49 * indicates which port on this switch should be used to
50 * send packets to that are destined for switch a. Can be
51 * NULL if there is only one switch chip.
52 */
53 s8 *rtable;
54};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055
56struct dsa_platform_data {
57 /*
58 * Reference to a Linux network interface that connects
Lennert Buytenheke84665c2009-03-20 09:52:09 +000059 * to the root switch chip of the tree.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060 */
61 struct device *netdev;
62
63 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +000064 * Info structs describing each of the switch chips
65 * connected via this network interface.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000066 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +000067 int nr_chips;
68 struct dsa_chip_data *chip;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000069};
70
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070071struct dsa_device_ops;
72
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000073struct dsa_switch_tree {
74 /*
75 * Configuration data for the platform device that owns
76 * this dsa switch tree instance.
77 */
78 struct dsa_platform_data *pd;
Lennert Buytenhekcf85d082008-10-07 13:45:02 +000079
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000080 /*
81 * Reference to network device to use, and which tagging
82 * protocol to use.
83 */
84 struct net_device *master_netdev;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -070085 const struct dsa_device_ops *ops;
Ben Hutchingscf50dcc2011-11-25 14:32:52 +000086 __be16 tag_protocol;
87
88 /*
89 * The switch and port to which the CPU is attached.
90 */
91 s8 cpu_switch;
92 s8 cpu_port;
93
94 /*
95 * Link state polling.
96 */
97 int link_poll_needed;
98 struct work_struct link_poll_work;
99 struct timer_list link_poll_timer;
100
101 /*
102 * Data for the individual switch chips.
103 */
104 struct dsa_switch *ds[DSA_MAX_SWITCHES];
105};
106
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000107struct dsa_switch {
108 /*
109 * Parent switch tree, and switch index.
110 */
111 struct dsa_switch_tree *dst;
112 int index;
113
114 /*
115 * Configuration data for this switch.
116 */
117 struct dsa_chip_data *pd;
118
119 /*
120 * The used switch driver.
121 */
122 struct dsa_switch_driver *drv;
123
124 /*
125 * Reference to mii bus to use.
126 */
127 struct mii_bus *master_mii_bus;
128
129 /*
130 * Slave mii_bus and devices for the individual ports.
131 */
132 u32 dsa_port_mask;
133 u32 phys_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700134 u32 phys_mii_mask;
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000135 struct mii_bus *slave_mii_bus;
136 struct net_device *ports[DSA_MAX_PORTS];
137};
138
139static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
140{
141 return !!(ds->index == ds->dst->cpu_switch && p == ds->dst->cpu_port);
142}
143
144static inline u8 dsa_upstream_port(struct dsa_switch *ds)
145{
146 struct dsa_switch_tree *dst = ds->dst;
147
148 /*
149 * If this is the root switch (i.e. the switch that connects
150 * to the CPU), return the cpu port number on this switch.
151 * Else return the (DSA) port number that connects to the
152 * switch that is one hop closer to the cpu.
153 */
154 if (dst->cpu_switch == ds->index)
155 return dst->cpu_port;
156 else
157 return ds->pd->rtable[dst->cpu_switch];
158}
159
160struct dsa_switch_driver {
161 struct list_head list;
162
163 __be16 tag_protocol;
164 int priv_size;
165
166 /*
167 * Probing and setup.
168 */
169 char *(*probe)(struct mii_bus *bus, int sw_addr);
170 int (*setup)(struct dsa_switch *ds);
171 int (*set_addr)(struct dsa_switch *ds, u8 *addr);
172
173 /*
174 * Access to the switch's PHY registers.
175 */
176 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
177 int (*phy_write)(struct dsa_switch *ds, int port,
178 int regnum, u16 val);
179
180 /*
181 * Link state polling and IRQ handling.
182 */
183 void (*poll_link)(struct dsa_switch *ds);
184
185 /*
Florian Fainelliec9436b2014-08-27 17:04:53 -0700186 * Link state adjustment (called from libphy)
187 */
188 void (*adjust_link)(struct dsa_switch *ds, int port,
189 struct phy_device *phydev);
190
191 /*
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000192 * ethtool hardware statistics.
193 */
194 void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
195 void (*get_ethtool_stats)(struct dsa_switch *ds,
196 int port, uint64_t *data);
197 int (*get_sset_count)(struct dsa_switch *ds);
198};
199
200void register_switch_driver(struct dsa_switch_driver *type);
201void unregister_switch_driver(struct dsa_switch_driver *type);
202
Florian Fainelli7fa857e2014-04-28 11:14:27 -0700203static inline void *ds_to_priv(struct dsa_switch *ds)
204{
205 return (void *)(ds + 1);
206}
207
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700208static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
209{
210 return dst->tag_protocol != 0;
211}
212
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213#endif