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