slave.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/etherdevice.h>
  12. #include <linux/phy.h>
  13. #include <linux/phy_fixed.h>
  14. #include <linux/of_net.h>
  15. #include <linux/of_mdio.h>
  16. #include "dsa_priv.h"
  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. if (ds->phys_mii_mask & (1 << addr))
  22. return ds->drv->phy_read(ds, addr, reg);
  23. return 0xffff;
  24. }
  25. static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
  26. {
  27. struct dsa_switch *ds = bus->priv;
  28. if (ds->phys_mii_mask & (1 << addr))
  29. return ds->drv->phy_write(ds, addr, reg, val);
  30. return 0;
  31. }
  32. void dsa_slave_mii_bus_init(struct dsa_switch *ds)
  33. {
  34. ds->slave_mii_bus->priv = (void *)ds;
  35. ds->slave_mii_bus->name = "dsa slave smi";
  36. ds->slave_mii_bus->read = dsa_slave_phy_read;
  37. ds->slave_mii_bus->write = dsa_slave_phy_write;
  38. snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
  39. ds->index, ds->pd->sw_addr);
  40. ds->slave_mii_bus->parent = ds->master_dev;
  41. }
  42. /* slave device handling ****************************************************/
  43. static int dsa_slave_init(struct net_device *dev)
  44. {
  45. struct dsa_slave_priv *p = netdev_priv(dev);
  46. dev->iflink = p->parent->dst->master_netdev->ifindex;
  47. return 0;
  48. }
  49. static int dsa_slave_open(struct net_device *dev)
  50. {
  51. struct dsa_slave_priv *p = netdev_priv(dev);
  52. struct net_device *master = p->parent->dst->master_netdev;
  53. struct dsa_switch *ds = p->parent;
  54. int err;
  55. if (!(master->flags & IFF_UP))
  56. return -ENETDOWN;
  57. if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
  58. err = dev_uc_add(master, dev->dev_addr);
  59. if (err < 0)
  60. goto out;
  61. }
  62. if (dev->flags & IFF_ALLMULTI) {
  63. err = dev_set_allmulti(master, 1);
  64. if (err < 0)
  65. goto del_unicast;
  66. }
  67. if (dev->flags & IFF_PROMISC) {
  68. err = dev_set_promiscuity(master, 1);
  69. if (err < 0)
  70. goto clear_allmulti;
  71. }
  72. if (ds->drv->port_enable) {
  73. err = ds->drv->port_enable(ds, p->port, p->phy);
  74. if (err)
  75. goto clear_promisc;
  76. }
  77. if (p->phy)
  78. phy_start(p->phy);
  79. return 0;
  80. clear_promisc:
  81. if (dev->flags & IFF_PROMISC)
  82. dev_set_promiscuity(master, 0);
  83. clear_allmulti:
  84. if (dev->flags & IFF_ALLMULTI)
  85. dev_set_allmulti(master, -1);
  86. del_unicast:
  87. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  88. dev_uc_del(master, dev->dev_addr);
  89. out:
  90. return err;
  91. }
  92. static int dsa_slave_close(struct net_device *dev)
  93. {
  94. struct dsa_slave_priv *p = netdev_priv(dev);
  95. struct net_device *master = p->parent->dst->master_netdev;
  96. struct dsa_switch *ds = p->parent;
  97. if (p->phy)
  98. phy_stop(p->phy);
  99. dev_mc_unsync(master, dev);
  100. dev_uc_unsync(master, dev);
  101. if (dev->flags & IFF_ALLMULTI)
  102. dev_set_allmulti(master, -1);
  103. if (dev->flags & IFF_PROMISC)
  104. dev_set_promiscuity(master, -1);
  105. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  106. dev_uc_del(master, dev->dev_addr);
  107. if (ds->drv->port_disable)
  108. ds->drv->port_disable(ds, p->port, p->phy);
  109. return 0;
  110. }
  111. static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
  112. {
  113. struct dsa_slave_priv *p = netdev_priv(dev);
  114. struct net_device *master = p->parent->dst->master_netdev;
  115. if (change & IFF_ALLMULTI)
  116. dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
  117. if (change & IFF_PROMISC)
  118. dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
  119. }
  120. static void dsa_slave_set_rx_mode(struct net_device *dev)
  121. {
  122. struct dsa_slave_priv *p = netdev_priv(dev);
  123. struct net_device *master = p->parent->dst->master_netdev;
  124. dev_mc_sync(master, dev);
  125. dev_uc_sync(master, dev);
  126. }
  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->dst->master_netdev;
  131. struct sockaddr *addr = a;
  132. int err;
  133. if (!is_valid_ether_addr(addr->sa_data))
  134. return -EADDRNOTAVAIL;
  135. if (!(dev->flags & IFF_UP))
  136. goto out;
  137. if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
  138. err = dev_uc_add(master, addr->sa_data);
  139. if (err < 0)
  140. return err;
  141. }
  142. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  143. dev_uc_del(master, dev->dev_addr);
  144. out:
  145. ether_addr_copy(dev->dev_addr, addr->sa_data);
  146. return 0;
  147. }
  148. static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  149. {
  150. struct dsa_slave_priv *p = netdev_priv(dev);
  151. if (p->phy != NULL)
  152. return phy_mii_ioctl(p->phy, ifr, cmd);
  153. return -EOPNOTSUPP;
  154. }
  155. static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
  156. {
  157. struct dsa_slave_priv *p = netdev_priv(dev);
  158. return p->xmit(skb, dev);
  159. }
  160. static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
  161. struct net_device *dev)
  162. {
  163. struct dsa_slave_priv *p = netdev_priv(dev);
  164. skb->dev = p->parent->dst->master_netdev;
  165. dev_queue_xmit(skb);
  166. return NETDEV_TX_OK;
  167. }
  168. /* ethtool operations *******************************************************/
  169. static int
  170. dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  171. {
  172. struct dsa_slave_priv *p = netdev_priv(dev);
  173. int err;
  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. return err;
  181. }
  182. static int
  183. dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  184. {
  185. struct dsa_slave_priv *p = netdev_priv(dev);
  186. if (p->phy != NULL)
  187. return phy_ethtool_sset(p->phy, cmd);
  188. return -EOPNOTSUPP;
  189. }
  190. static void dsa_slave_get_drvinfo(struct net_device *dev,
  191. struct ethtool_drvinfo *drvinfo)
  192. {
  193. strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
  194. strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
  195. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  196. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  197. }
  198. static int dsa_slave_nway_reset(struct net_device *dev)
  199. {
  200. struct dsa_slave_priv *p = netdev_priv(dev);
  201. if (p->phy != NULL)
  202. return genphy_restart_aneg(p->phy);
  203. return -EOPNOTSUPP;
  204. }
  205. static u32 dsa_slave_get_link(struct net_device *dev)
  206. {
  207. struct dsa_slave_priv *p = netdev_priv(dev);
  208. if (p->phy != NULL) {
  209. genphy_update_link(p->phy);
  210. return p->phy->link;
  211. }
  212. return -EOPNOTSUPP;
  213. }
  214. static void dsa_slave_get_strings(struct net_device *dev,
  215. uint32_t stringset, uint8_t *data)
  216. {
  217. struct dsa_slave_priv *p = netdev_priv(dev);
  218. struct dsa_switch *ds = p->parent;
  219. if (stringset == ETH_SS_STATS) {
  220. int len = ETH_GSTRING_LEN;
  221. strncpy(data, "tx_packets", len);
  222. strncpy(data + len, "tx_bytes", len);
  223. strncpy(data + 2 * len, "rx_packets", len);
  224. strncpy(data + 3 * len, "rx_bytes", len);
  225. if (ds->drv->get_strings != NULL)
  226. ds->drv->get_strings(ds, p->port, data + 4 * len);
  227. }
  228. }
  229. static void dsa_slave_get_ethtool_stats(struct net_device *dev,
  230. struct ethtool_stats *stats,
  231. uint64_t *data)
  232. {
  233. struct dsa_slave_priv *p = netdev_priv(dev);
  234. struct dsa_switch *ds = p->parent;
  235. data[0] = p->dev->stats.tx_packets;
  236. data[1] = p->dev->stats.tx_bytes;
  237. data[2] = p->dev->stats.rx_packets;
  238. data[3] = p->dev->stats.rx_bytes;
  239. if (ds->drv->get_ethtool_stats != NULL)
  240. ds->drv->get_ethtool_stats(ds, p->port, data + 4);
  241. }
  242. static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
  243. {
  244. struct dsa_slave_priv *p = netdev_priv(dev);
  245. struct dsa_switch *ds = p->parent;
  246. if (sset == ETH_SS_STATS) {
  247. int count;
  248. count = 4;
  249. if (ds->drv->get_sset_count != NULL)
  250. count += ds->drv->get_sset_count(ds);
  251. return count;
  252. }
  253. return -EOPNOTSUPP;
  254. }
  255. static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  256. {
  257. struct dsa_slave_priv *p = netdev_priv(dev);
  258. struct dsa_switch *ds = p->parent;
  259. if (ds->drv->get_wol)
  260. ds->drv->get_wol(ds, p->port, w);
  261. }
  262. static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  263. {
  264. struct dsa_slave_priv *p = netdev_priv(dev);
  265. struct dsa_switch *ds = p->parent;
  266. int ret = -EOPNOTSUPP;
  267. if (ds->drv->set_wol)
  268. ret = ds->drv->set_wol(ds, p->port, w);
  269. return ret;
  270. }
  271. static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
  272. {
  273. struct dsa_slave_priv *p = netdev_priv(dev);
  274. struct dsa_switch *ds = p->parent;
  275. int ret;
  276. if (!ds->drv->set_eee)
  277. return -EOPNOTSUPP;
  278. ret = ds->drv->set_eee(ds, p->port, p->phy, e);
  279. if (ret)
  280. return ret;
  281. if (p->phy)
  282. ret = phy_ethtool_set_eee(p->phy, e);
  283. return ret;
  284. }
  285. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  286. {
  287. struct dsa_slave_priv *p = netdev_priv(dev);
  288. struct dsa_switch *ds = p->parent;
  289. int ret;
  290. if (!ds->drv->get_eee)
  291. return -EOPNOTSUPP;
  292. ret = ds->drv->get_eee(ds, p->port, e);
  293. if (ret)
  294. return ret;
  295. if (p->phy)
  296. ret = phy_ethtool_get_eee(p->phy, e);
  297. return ret;
  298. }
  299. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  300. .get_settings = dsa_slave_get_settings,
  301. .set_settings = dsa_slave_set_settings,
  302. .get_drvinfo = dsa_slave_get_drvinfo,
  303. .nway_reset = dsa_slave_nway_reset,
  304. .get_link = dsa_slave_get_link,
  305. .get_strings = dsa_slave_get_strings,
  306. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  307. .get_sset_count = dsa_slave_get_sset_count,
  308. .set_wol = dsa_slave_set_wol,
  309. .get_wol = dsa_slave_get_wol,
  310. .set_eee = dsa_slave_set_eee,
  311. .get_eee = dsa_slave_get_eee,
  312. };
  313. static const struct net_device_ops dsa_slave_netdev_ops = {
  314. .ndo_init = dsa_slave_init,
  315. .ndo_open = dsa_slave_open,
  316. .ndo_stop = dsa_slave_close,
  317. .ndo_start_xmit = dsa_slave_xmit,
  318. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  319. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  320. .ndo_set_mac_address = dsa_slave_set_mac_address,
  321. .ndo_do_ioctl = dsa_slave_ioctl,
  322. };
  323. static void dsa_slave_adjust_link(struct net_device *dev)
  324. {
  325. struct dsa_slave_priv *p = netdev_priv(dev);
  326. struct dsa_switch *ds = p->parent;
  327. unsigned int status_changed = 0;
  328. if (p->old_link != p->phy->link) {
  329. status_changed = 1;
  330. p->old_link = p->phy->link;
  331. }
  332. if (p->old_duplex != p->phy->duplex) {
  333. status_changed = 1;
  334. p->old_duplex = p->phy->duplex;
  335. }
  336. if (p->old_pause != p->phy->pause) {
  337. status_changed = 1;
  338. p->old_pause = p->phy->pause;
  339. }
  340. if (ds->drv->adjust_link && status_changed)
  341. ds->drv->adjust_link(ds, p->port, p->phy);
  342. if (status_changed)
  343. phy_print_status(p->phy);
  344. }
  345. static int dsa_slave_fixed_link_update(struct net_device *dev,
  346. struct fixed_phy_status *status)
  347. {
  348. struct dsa_slave_priv *p = netdev_priv(dev);
  349. struct dsa_switch *ds = p->parent;
  350. if (ds->drv->fixed_link_update)
  351. ds->drv->fixed_link_update(ds, p->port, status);
  352. return 0;
  353. }
  354. /* slave device setup *******************************************************/
  355. static void dsa_slave_phy_setup(struct dsa_slave_priv *p,
  356. struct net_device *slave_dev)
  357. {
  358. struct dsa_switch *ds = p->parent;
  359. struct dsa_chip_data *cd = ds->pd;
  360. struct device_node *phy_dn, *port_dn;
  361. bool phy_is_fixed = false;
  362. u32 phy_flags = 0;
  363. int ret;
  364. port_dn = cd->port_dn[p->port];
  365. p->phy_interface = of_get_phy_mode(port_dn);
  366. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  367. if (of_phy_is_fixed_link(port_dn)) {
  368. /* In the case of a fixed PHY, the DT node associated
  369. * to the fixed PHY is the Port DT node
  370. */
  371. ret = of_phy_register_fixed_link(port_dn);
  372. if (ret) {
  373. pr_err("failed to register fixed PHY\n");
  374. return;
  375. }
  376. phy_is_fixed = true;
  377. phy_dn = port_dn;
  378. }
  379. if (ds->drv->get_phy_flags)
  380. phy_flags = ds->drv->get_phy_flags(ds, p->port);
  381. if (phy_dn)
  382. p->phy = of_phy_connect(slave_dev, phy_dn,
  383. dsa_slave_adjust_link, phy_flags,
  384. p->phy_interface);
  385. if (p->phy && phy_is_fixed)
  386. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  387. /* We could not connect to a designated PHY, so use the switch internal
  388. * MDIO bus instead
  389. */
  390. if (!p->phy)
  391. p->phy = ds->slave_mii_bus->phy_map[p->port];
  392. else
  393. pr_info("attached PHY at address %d [%s]\n",
  394. p->phy->addr, p->phy->drv->name);
  395. }
  396. int dsa_slave_suspend(struct net_device *slave_dev)
  397. {
  398. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  399. netif_device_detach(slave_dev);
  400. if (p->phy) {
  401. phy_stop(p->phy);
  402. p->old_pause = -1;
  403. p->old_link = -1;
  404. p->old_duplex = -1;
  405. phy_suspend(p->phy);
  406. }
  407. return 0;
  408. }
  409. int dsa_slave_resume(struct net_device *slave_dev)
  410. {
  411. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  412. netif_device_attach(slave_dev);
  413. if (p->phy) {
  414. phy_resume(p->phy);
  415. phy_start(p->phy);
  416. }
  417. return 0;
  418. }
  419. struct net_device *
  420. dsa_slave_create(struct dsa_switch *ds, struct device *parent,
  421. int port, char *name)
  422. {
  423. struct net_device *master = ds->dst->master_netdev;
  424. struct net_device *slave_dev;
  425. struct dsa_slave_priv *p;
  426. int ret;
  427. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  428. NET_NAME_UNKNOWN, ether_setup);
  429. if (slave_dev == NULL)
  430. return slave_dev;
  431. slave_dev->features = master->vlan_features;
  432. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  433. eth_hw_addr_inherit(slave_dev, master);
  434. slave_dev->tx_queue_len = 0;
  435. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  436. SET_NETDEV_DEV(slave_dev, parent);
  437. slave_dev->dev.of_node = ds->pd->port_dn[port];
  438. slave_dev->vlan_features = master->vlan_features;
  439. p = netdev_priv(slave_dev);
  440. p->dev = slave_dev;
  441. p->parent = ds;
  442. p->port = port;
  443. switch (ds->dst->tag_protocol) {
  444. #ifdef CONFIG_NET_DSA_TAG_DSA
  445. case DSA_TAG_PROTO_DSA:
  446. p->xmit = dsa_netdev_ops.xmit;
  447. break;
  448. #endif
  449. #ifdef CONFIG_NET_DSA_TAG_EDSA
  450. case DSA_TAG_PROTO_EDSA:
  451. p->xmit = edsa_netdev_ops.xmit;
  452. break;
  453. #endif
  454. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  455. case DSA_TAG_PROTO_TRAILER:
  456. p->xmit = trailer_netdev_ops.xmit;
  457. break;
  458. #endif
  459. #ifdef CONFIG_NET_DSA_TAG_BRCM
  460. case DSA_TAG_PROTO_BRCM:
  461. p->xmit = brcm_netdev_ops.xmit;
  462. break;
  463. #endif
  464. default:
  465. p->xmit = dsa_slave_notag_xmit;
  466. break;
  467. }
  468. p->old_pause = -1;
  469. p->old_link = -1;
  470. p->old_duplex = -1;
  471. dsa_slave_phy_setup(p, slave_dev);
  472. ret = register_netdev(slave_dev);
  473. if (ret) {
  474. printk(KERN_ERR "%s: error %d registering interface %s\n",
  475. master->name, ret, slave_dev->name);
  476. free_netdev(slave_dev);
  477. return NULL;
  478. }
  479. netif_carrier_off(slave_dev);
  480. if (p->phy != NULL) {
  481. if (ds->drv->get_phy_flags)
  482. p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);
  483. phy_attach(slave_dev, dev_name(&p->phy->dev),
  484. PHY_INTERFACE_MODE_GMII);
  485. p->phy->autoneg = AUTONEG_ENABLE;
  486. p->phy->speed = 0;
  487. p->phy->duplex = 0;
  488. p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;
  489. }
  490. return slave_dev;
  491. }