slave.c 10 KB

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