blob: dc357454ae3b82e0f049e2681f0d169bde409e96 [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;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700133 u32 phys_mii_mask;
Ben Hutchingsc8f0b862011-11-27 17:06:08 +0000134 struct mii_bus *slave_mii_bus;
135 struct net_device *ports[DSA_MAX_PORTS];
136};
137
138static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
139{
140 return !!(ds->index == ds->dst->cpu_switch && p == ds->dst->cpu_port);
141}
142
143static inline u8 dsa_upstream_port(struct dsa_switch *ds)
144{
145 struct dsa_switch_tree *dst = ds->dst;
146
147 /*
148 * If this is the root switch (i.e. the switch that connects
149 * to the CPU), return the cpu port number on this switch.
150 * Else return the (DSA) port number that connects to the
151 * switch that is one hop closer to the cpu.
152 */
153 if (dst->cpu_switch == ds->index)
154 return dst->cpu_port;
155 else
156 return ds->pd->rtable[dst->cpu_switch];
157}
158
159struct dsa_switch_driver {
160 struct list_head list;
161
162 __be16 tag_protocol;
163 int priv_size;
164
165 /*
166 * Probing and setup.
167 */
168 char *(*probe)(struct mii_bus *bus, int sw_addr);
169 int (*setup)(struct dsa_switch *ds);
170 int (*set_addr)(struct dsa_switch *ds, u8 *addr);
171
172 /*
173 * Access to the switch's PHY registers.
174 */
175 int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
176 int (*phy_write)(struct dsa_switch *ds, int port,
177 int regnum, u16 val);
178
179 /*
180 * Link state polling and IRQ handling.
181 */
182 void (*poll_link)(struct dsa_switch *ds);
183
184 /*
185 * ethtool hardware statistics.
186 */
187 void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
188 void (*get_ethtool_stats)(struct dsa_switch *ds,
189 int port, uint64_t *data);
190 int (*get_sset_count)(struct dsa_switch *ds);
191};
192
193void register_switch_driver(struct dsa_switch_driver *type);
194void unregister_switch_driver(struct dsa_switch_driver *type);
195
Florian Fainelli7fa857e2014-04-28 11:14:27 -0700196static inline void *ds_to_priv(struct dsa_switch *ds)
197{
198 return (void *)(ds + 1);
199}
200
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201#endif