• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * net/dsa/slave.c - Slave device handling
3  * Copyright (c) 2008 Marvell Semiconductor
4  *
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 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/phy.h>
15 #include "dsa_priv.h"
16 
17 /* slave mii_bus handling ***************************************************/
dsa_slave_phy_read(struct mii_bus * bus,int addr,int reg)18 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
19 {
20 	struct dsa_switch *ds = bus->priv;
21 
22 	if (ds->valid_port_mask & (1 << addr))
23 		return ds->drv->phy_read(ds, addr, reg);
24 
25 	return 0xffff;
26 }
27 
dsa_slave_phy_write(struct mii_bus * bus,int addr,int reg,u16 val)28 static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
29 {
30 	struct dsa_switch *ds = bus->priv;
31 
32 	if (ds->valid_port_mask & (1 << addr))
33 		return ds->drv->phy_write(ds, addr, reg, val);
34 
35 	return 0;
36 }
37 
dsa_slave_mii_bus_init(struct dsa_switch * ds)38 void dsa_slave_mii_bus_init(struct dsa_switch *ds)
39 {
40 	ds->slave_mii_bus->priv = (void *)ds;
41 	ds->slave_mii_bus->name = "dsa slave smi";
42 	ds->slave_mii_bus->read = dsa_slave_phy_read;
43 	ds->slave_mii_bus->write = dsa_slave_phy_write;
44 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s:%.2x",
45 			ds->master_mii_bus->id, ds->pd->sw_addr);
46 	ds->slave_mii_bus->parent = &(ds->master_mii_bus->dev);
47 }
48 
49 
50 /* slave device handling ****************************************************/
dsa_slave_open(struct net_device * dev)51 static int dsa_slave_open(struct net_device *dev)
52 {
53 	struct dsa_slave_priv *p = netdev_priv(dev);
54 	struct net_device *master = p->parent->master_netdev;
55 	int err;
56 
57 	if (!(master->flags & IFF_UP))
58 		return -ENETDOWN;
59 
60 	if (compare_ether_addr(dev->dev_addr, master->dev_addr)) {
61 		err = dev_unicast_add(master, dev->dev_addr, ETH_ALEN);
62 		if (err < 0)
63 			goto out;
64 	}
65 
66 	if (dev->flags & IFF_ALLMULTI) {
67 		err = dev_set_allmulti(master, 1);
68 		if (err < 0)
69 			goto del_unicast;
70 	}
71 	if (dev->flags & IFF_PROMISC) {
72 		err = dev_set_promiscuity(master, 1);
73 		if (err < 0)
74 			goto clear_allmulti;
75 	}
76 
77 	return 0;
78 
79 clear_allmulti:
80 	if (dev->flags & IFF_ALLMULTI)
81 		dev_set_allmulti(master, -1);
82 del_unicast:
83 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
84 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
85 out:
86 	return err;
87 }
88 
dsa_slave_close(struct net_device * dev)89 static int dsa_slave_close(struct net_device *dev)
90 {
91 	struct dsa_slave_priv *p = netdev_priv(dev);
92 	struct net_device *master = p->parent->master_netdev;
93 
94 	dev_mc_unsync(master, dev);
95 	dev_unicast_unsync(master, dev);
96 	if (dev->flags & IFF_ALLMULTI)
97 		dev_set_allmulti(master, -1);
98 	if (dev->flags & IFF_PROMISC)
99 		dev_set_promiscuity(master, -1);
100 
101 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
102 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
103 
104 	return 0;
105 }
106 
dsa_slave_change_rx_flags(struct net_device * dev,int change)107 static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
108 {
109 	struct dsa_slave_priv *p = netdev_priv(dev);
110 	struct net_device *master = p->parent->master_netdev;
111 
112 	if (change & IFF_ALLMULTI)
113 		dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
114 	if (change & IFF_PROMISC)
115 		dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
116 }
117 
dsa_slave_set_rx_mode(struct net_device * dev)118 static void dsa_slave_set_rx_mode(struct net_device *dev)
119 {
120 	struct dsa_slave_priv *p = netdev_priv(dev);
121 	struct net_device *master = p->parent->master_netdev;
122 
123 	dev_mc_sync(master, dev);
124 	dev_unicast_sync(master, dev);
125 }
126 
dsa_slave_set_mac_address(struct net_device * dev,void * a)127 static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
128 {
129 	struct dsa_slave_priv *p = netdev_priv(dev);
130 	struct net_device *master = p->parent->master_netdev;
131 	struct sockaddr *addr = a;
132 	int err;
133 
134 	if (!is_valid_ether_addr(addr->sa_data))
135 		return -EADDRNOTAVAIL;
136 
137 	if (!(dev->flags & IFF_UP))
138 		goto out;
139 
140 	if (compare_ether_addr(addr->sa_data, master->dev_addr)) {
141 		err = dev_unicast_add(master, addr->sa_data, ETH_ALEN);
142 		if (err < 0)
143 			return err;
144 	}
145 
146 	if (compare_ether_addr(dev->dev_addr, master->dev_addr))
147 		dev_unicast_delete(master, dev->dev_addr, ETH_ALEN);
148 
149 out:
150 	memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
151 
152 	return 0;
153 }
154 
dsa_slave_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)155 static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
156 {
157 	struct dsa_slave_priv *p = netdev_priv(dev);
158 	struct mii_ioctl_data *mii_data = if_mii(ifr);
159 
160 	if (p->phy != NULL)
161 		return phy_mii_ioctl(p->phy, mii_data, cmd);
162 
163 	return -EOPNOTSUPP;
164 }
165 
166 
167 /* ethtool operations *******************************************************/
168 static int
dsa_slave_get_settings(struct net_device * dev,struct ethtool_cmd * cmd)169 dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
170 {
171 	struct dsa_slave_priv *p = netdev_priv(dev);
172 	int err;
173 
174 	err = -EOPNOTSUPP;
175 	if (p->phy != NULL) {
176 		err = phy_read_status(p->phy);
177 		if (err == 0)
178 			err = phy_ethtool_gset(p->phy, cmd);
179 	}
180 
181 	return err;
182 }
183 
184 static int
dsa_slave_set_settings(struct net_device * dev,struct ethtool_cmd * cmd)185 dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
186 {
187 	struct dsa_slave_priv *p = netdev_priv(dev);
188 
189 	if (p->phy != NULL)
190 		return phy_ethtool_sset(p->phy, cmd);
191 
192 	return -EOPNOTSUPP;
193 }
194 
dsa_slave_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)195 static void dsa_slave_get_drvinfo(struct net_device *dev,
196 				  struct ethtool_drvinfo *drvinfo)
197 {
198 	strncpy(drvinfo->driver, "dsa", 32);
199 	strncpy(drvinfo->version, dsa_driver_version, 32);
200 	strncpy(drvinfo->fw_version, "N/A", 32);
201 	strncpy(drvinfo->bus_info, "platform", 32);
202 }
203 
dsa_slave_nway_reset(struct net_device * dev)204 static int dsa_slave_nway_reset(struct net_device *dev)
205 {
206 	struct dsa_slave_priv *p = netdev_priv(dev);
207 
208 	if (p->phy != NULL)
209 		return genphy_restart_aneg(p->phy);
210 
211 	return -EOPNOTSUPP;
212 }
213 
dsa_slave_get_link(struct net_device * dev)214 static u32 dsa_slave_get_link(struct net_device *dev)
215 {
216 	struct dsa_slave_priv *p = netdev_priv(dev);
217 
218 	if (p->phy != NULL) {
219 		genphy_update_link(p->phy);
220 		return p->phy->link;
221 	}
222 
223 	return -EOPNOTSUPP;
224 }
225 
dsa_slave_get_strings(struct net_device * dev,uint32_t stringset,uint8_t * data)226 static void dsa_slave_get_strings(struct net_device *dev,
227 				  uint32_t stringset, uint8_t *data)
228 {
229 	struct dsa_slave_priv *p = netdev_priv(dev);
230 	struct dsa_switch *ds = p->parent;
231 
232 	if (stringset == ETH_SS_STATS) {
233 		int len = ETH_GSTRING_LEN;
234 
235 		strncpy(data, "tx_packets", len);
236 		strncpy(data + len, "tx_bytes", len);
237 		strncpy(data + 2 * len, "rx_packets", len);
238 		strncpy(data + 3 * len, "rx_bytes", len);
239 		if (ds->drv->get_strings != NULL)
240 			ds->drv->get_strings(ds, p->port, data + 4 * len);
241 	}
242 }
243 
dsa_slave_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)244 static void dsa_slave_get_ethtool_stats(struct net_device *dev,
245 					struct ethtool_stats *stats,
246 					uint64_t *data)
247 {
248 	struct dsa_slave_priv *p = netdev_priv(dev);
249 	struct dsa_switch *ds = p->parent;
250 
251 	data[0] = p->dev->stats.tx_packets;
252 	data[1] = p->dev->stats.tx_bytes;
253 	data[2] = p->dev->stats.rx_packets;
254 	data[3] = p->dev->stats.rx_bytes;
255 	if (ds->drv->get_ethtool_stats != NULL)
256 		ds->drv->get_ethtool_stats(ds, p->port, data + 4);
257 }
258 
dsa_slave_get_sset_count(struct net_device * dev,int sset)259 static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
260 {
261 	struct dsa_slave_priv *p = netdev_priv(dev);
262 	struct dsa_switch *ds = p->parent;
263 
264 	if (sset == ETH_SS_STATS) {
265 		int count;
266 
267 		count = 4;
268 		if (ds->drv->get_sset_count != NULL)
269 			count += ds->drv->get_sset_count(ds);
270 
271 		return count;
272 	}
273 
274 	return -EOPNOTSUPP;
275 }
276 
277 static const struct ethtool_ops dsa_slave_ethtool_ops = {
278 	.get_settings		= dsa_slave_get_settings,
279 	.set_settings		= dsa_slave_set_settings,
280 	.get_drvinfo		= dsa_slave_get_drvinfo,
281 	.nway_reset		= dsa_slave_nway_reset,
282 	.get_link		= dsa_slave_get_link,
283 	.set_sg			= ethtool_op_set_sg,
284 	.get_strings		= dsa_slave_get_strings,
285 	.get_ethtool_stats	= dsa_slave_get_ethtool_stats,
286 	.get_sset_count		= dsa_slave_get_sset_count,
287 };
288 
289 #ifdef CONFIG_NET_DSA_TAG_DSA
290 static const struct net_device_ops dsa_netdev_ops = {
291 	.ndo_open	 	= dsa_slave_open,
292 	.ndo_stop		= dsa_slave_close,
293 	.ndo_start_xmit		= dsa_xmit,
294 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
295 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
296 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
297 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
298 	.ndo_do_ioctl		= dsa_slave_ioctl,
299 };
300 #endif
301 #ifdef CONFIG_NET_DSA_TAG_EDSA
302 static const struct net_device_ops edsa_netdev_ops = {
303 	.ndo_open	 	= dsa_slave_open,
304 	.ndo_stop		= dsa_slave_close,
305 	.ndo_start_xmit		= edsa_xmit,
306 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
307 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
308 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
309 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
310 	.ndo_do_ioctl		= dsa_slave_ioctl,
311 };
312 #endif
313 #ifdef CONFIG_NET_DSA_TAG_TRAILER
314 static const struct net_device_ops trailer_netdev_ops = {
315 	.ndo_open	 	= dsa_slave_open,
316 	.ndo_stop		= dsa_slave_close,
317 	.ndo_start_xmit		= trailer_xmit,
318 	.ndo_change_rx_flags	= dsa_slave_change_rx_flags,
319 	.ndo_set_rx_mode	= dsa_slave_set_rx_mode,
320 	.ndo_set_multicast_list = dsa_slave_set_rx_mode,
321 	.ndo_set_mac_address	= dsa_slave_set_mac_address,
322 	.ndo_do_ioctl		= dsa_slave_ioctl,
323 };
324 #endif
325 
326 /* slave device setup *******************************************************/
327 struct net_device *
dsa_slave_create(struct dsa_switch * ds,struct device * parent,int port,char * name)328 dsa_slave_create(struct dsa_switch *ds, struct device *parent,
329 		 int port, char *name)
330 {
331 	struct net_device *master = ds->master_netdev;
332 	struct net_device *slave_dev;
333 	struct dsa_slave_priv *p;
334 	int ret;
335 
336 	slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv),
337 				 name, ether_setup);
338 	if (slave_dev == NULL)
339 		return slave_dev;
340 
341 	slave_dev->features = master->vlan_features;
342 	SET_ETHTOOL_OPS(slave_dev, &dsa_slave_ethtool_ops);
343 	memcpy(slave_dev->dev_addr, master->dev_addr, ETH_ALEN);
344 	slave_dev->tx_queue_len = 0;
345 
346 	switch (ds->tag_protocol) {
347 #ifdef CONFIG_NET_DSA_TAG_DSA
348 	case htons(ETH_P_DSA):
349 		slave_dev->netdev_ops = &dsa_netdev_ops;
350 		break;
351 #endif
352 #ifdef CONFIG_NET_DSA_TAG_EDSA
353 	case htons(ETH_P_EDSA):
354 		slave_dev->netdev_ops = &edsa_netdev_ops;
355 		break;
356 #endif
357 #ifdef CONFIG_NET_DSA_TAG_TRAILER
358 	case htons(ETH_P_TRAILER):
359 		slave_dev->netdev_ops = &trailer_netdev_ops;
360 		break;
361 #endif
362 	default:
363 		BUG();
364 	}
365 
366 	SET_NETDEV_DEV(slave_dev, parent);
367 	slave_dev->vlan_features = master->vlan_features;
368 
369 	p = netdev_priv(slave_dev);
370 	p->dev = slave_dev;
371 	p->parent = ds;
372 	p->port = port;
373 	p->phy = ds->slave_mii_bus->phy_map[port];
374 
375 	ret = register_netdev(slave_dev);
376 	if (ret) {
377 		printk(KERN_ERR "%s: error %d registering interface %s\n",
378 				master->name, ret, slave_dev->name);
379 		free_netdev(slave_dev);
380 		return NULL;
381 	}
382 
383 	netif_carrier_off(slave_dev);
384 
385 	if (p->phy != NULL) {
386 		phy_attach(slave_dev, dev_name(&p->phy->dev),
387 			   0, PHY_INTERFACE_MODE_GMII);
388 
389 		p->phy->autoneg = AUTONEG_ENABLE;
390 		p->phy->speed = 0;
391 		p->phy->duplex = 0;
392 		p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
393 		phy_start_aneg(p->phy);
394 	}
395 
396 	return slave_dev;
397 }
398