slave.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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/netdevice.h>
  13. #include <linux/phy.h>
  14. #include <linux/phy_fixed.h>
  15. #include <linux/of_net.h>
  16. #include <linux/of_mdio.h>
  17. #include <net/rtnetlink.h>
  18. #include <net/switchdev.h>
  19. #include <linux/if_bridge.h>
  20. #include "dsa_priv.h"
  21. /* slave mii_bus handling ***************************************************/
  22. static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
  23. {
  24. struct dsa_switch *ds = bus->priv;
  25. if (ds->phys_mii_mask & (1 << addr))
  26. return ds->drv->phy_read(ds, addr, reg);
  27. return 0xffff;
  28. }
  29. static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
  30. {
  31. struct dsa_switch *ds = bus->priv;
  32. if (ds->phys_mii_mask & (1 << addr))
  33. return ds->drv->phy_write(ds, addr, reg, val);
  34. return 0;
  35. }
  36. void dsa_slave_mii_bus_init(struct dsa_switch *ds)
  37. {
  38. ds->slave_mii_bus->priv = (void *)ds;
  39. ds->slave_mii_bus->name = "dsa slave smi";
  40. ds->slave_mii_bus->read = dsa_slave_phy_read;
  41. ds->slave_mii_bus->write = dsa_slave_phy_write;
  42. snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d:%.2x",
  43. ds->index, ds->pd->sw_addr);
  44. ds->slave_mii_bus->parent = ds->master_dev;
  45. ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
  46. }
  47. /* slave device handling ****************************************************/
  48. static int dsa_slave_get_iflink(const struct net_device *dev)
  49. {
  50. struct dsa_slave_priv *p = netdev_priv(dev);
  51. return p->parent->dst->master_netdev->ifindex;
  52. }
  53. static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
  54. {
  55. return !!p->bridge_dev;
  56. }
  57. static int dsa_slave_open(struct net_device *dev)
  58. {
  59. struct dsa_slave_priv *p = netdev_priv(dev);
  60. struct net_device *master = p->parent->dst->master_netdev;
  61. struct dsa_switch *ds = p->parent;
  62. u8 stp_state = dsa_port_is_bridged(p) ?
  63. BR_STATE_BLOCKING : BR_STATE_FORWARDING;
  64. int err;
  65. if (!(master->flags & IFF_UP))
  66. return -ENETDOWN;
  67. if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
  68. err = dev_uc_add(master, dev->dev_addr);
  69. if (err < 0)
  70. goto out;
  71. }
  72. if (dev->flags & IFF_ALLMULTI) {
  73. err = dev_set_allmulti(master, 1);
  74. if (err < 0)
  75. goto del_unicast;
  76. }
  77. if (dev->flags & IFF_PROMISC) {
  78. err = dev_set_promiscuity(master, 1);
  79. if (err < 0)
  80. goto clear_allmulti;
  81. }
  82. if (ds->drv->port_enable) {
  83. err = ds->drv->port_enable(ds, p->port, p->phy);
  84. if (err)
  85. goto clear_promisc;
  86. }
  87. if (ds->drv->port_stp_update)
  88. ds->drv->port_stp_update(ds, p->port, stp_state);
  89. if (p->phy)
  90. phy_start(p->phy);
  91. return 0;
  92. clear_promisc:
  93. if (dev->flags & IFF_PROMISC)
  94. dev_set_promiscuity(master, 0);
  95. clear_allmulti:
  96. if (dev->flags & IFF_ALLMULTI)
  97. dev_set_allmulti(master, -1);
  98. del_unicast:
  99. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  100. dev_uc_del(master, dev->dev_addr);
  101. out:
  102. return err;
  103. }
  104. static int dsa_slave_close(struct net_device *dev)
  105. {
  106. struct dsa_slave_priv *p = netdev_priv(dev);
  107. struct net_device *master = p->parent->dst->master_netdev;
  108. struct dsa_switch *ds = p->parent;
  109. if (p->phy)
  110. phy_stop(p->phy);
  111. dev_mc_unsync(master, dev);
  112. dev_uc_unsync(master, dev);
  113. if (dev->flags & IFF_ALLMULTI)
  114. dev_set_allmulti(master, -1);
  115. if (dev->flags & IFF_PROMISC)
  116. dev_set_promiscuity(master, -1);
  117. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  118. dev_uc_del(master, dev->dev_addr);
  119. if (ds->drv->port_disable)
  120. ds->drv->port_disable(ds, p->port, p->phy);
  121. if (ds->drv->port_stp_update)
  122. ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
  123. return 0;
  124. }
  125. static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
  126. {
  127. struct dsa_slave_priv *p = netdev_priv(dev);
  128. struct net_device *master = p->parent->dst->master_netdev;
  129. if (change & IFF_ALLMULTI)
  130. dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
  131. if (change & IFF_PROMISC)
  132. dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
  133. }
  134. static void dsa_slave_set_rx_mode(struct net_device *dev)
  135. {
  136. struct dsa_slave_priv *p = netdev_priv(dev);
  137. struct net_device *master = p->parent->dst->master_netdev;
  138. dev_mc_sync(master, dev);
  139. dev_uc_sync(master, dev);
  140. }
  141. static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
  142. {
  143. struct dsa_slave_priv *p = netdev_priv(dev);
  144. struct net_device *master = p->parent->dst->master_netdev;
  145. struct sockaddr *addr = a;
  146. int err;
  147. if (!is_valid_ether_addr(addr->sa_data))
  148. return -EADDRNOTAVAIL;
  149. if (!(dev->flags & IFF_UP))
  150. goto out;
  151. if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
  152. err = dev_uc_add(master, addr->sa_data);
  153. if (err < 0)
  154. return err;
  155. }
  156. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  157. dev_uc_del(master, dev->dev_addr);
  158. out:
  159. ether_addr_copy(dev->dev_addr, addr->sa_data);
  160. return 0;
  161. }
  162. static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  163. struct net_device *dev,
  164. const unsigned char *addr, u16 vid, u16 nlm_flags)
  165. {
  166. struct dsa_slave_priv *p = netdev_priv(dev);
  167. struct dsa_switch *ds = p->parent;
  168. int ret = -EOPNOTSUPP;
  169. if (ds->drv->fdb_add)
  170. ret = ds->drv->fdb_add(ds, p->port, addr, vid);
  171. return ret;
  172. }
  173. static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  174. struct net_device *dev,
  175. const unsigned char *addr, u16 vid)
  176. {
  177. struct dsa_slave_priv *p = netdev_priv(dev);
  178. struct dsa_switch *ds = p->parent;
  179. int ret = -EOPNOTSUPP;
  180. if (ds->drv->fdb_del)
  181. ret = ds->drv->fdb_del(ds, p->port, addr, vid);
  182. return ret;
  183. }
  184. static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
  185. const unsigned char *addr, u16 vid,
  186. bool is_static,
  187. u32 portid, u32 seq, int type,
  188. unsigned int flags)
  189. {
  190. struct nlmsghdr *nlh;
  191. struct ndmsg *ndm;
  192. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  193. if (!nlh)
  194. return -EMSGSIZE;
  195. ndm = nlmsg_data(nlh);
  196. ndm->ndm_family = AF_BRIDGE;
  197. ndm->ndm_pad1 = 0;
  198. ndm->ndm_pad2 = 0;
  199. ndm->ndm_flags = NTF_EXT_LEARNED;
  200. ndm->ndm_type = 0;
  201. ndm->ndm_ifindex = dev->ifindex;
  202. ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
  203. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
  204. goto nla_put_failure;
  205. if (vid && nla_put_u16(skb, NDA_VLAN, vid))
  206. goto nla_put_failure;
  207. nlmsg_end(skb, nlh);
  208. return 0;
  209. nla_put_failure:
  210. nlmsg_cancel(skb, nlh);
  211. return -EMSGSIZE;
  212. }
  213. /* Dump information about entries, in response to GETNEIGH */
  214. static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  215. struct net_device *dev,
  216. struct net_device *filter_dev, int idx)
  217. {
  218. struct dsa_slave_priv *p = netdev_priv(dev);
  219. struct dsa_switch *ds = p->parent;
  220. unsigned char addr[ETH_ALEN] = { 0 };
  221. int ret;
  222. if (!ds->drv->fdb_getnext)
  223. return -EOPNOTSUPP;
  224. for (; ; idx++) {
  225. bool is_static;
  226. ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
  227. if (ret < 0)
  228. break;
  229. if (idx < cb->args[0])
  230. continue;
  231. ret = dsa_slave_fill_info(dev, skb, addr, 0,
  232. is_static,
  233. NETLINK_CB(cb->skb).portid,
  234. cb->nlh->nlmsg_seq,
  235. RTM_NEWNEIGH, NLM_F_MULTI);
  236. if (ret < 0)
  237. break;
  238. }
  239. return idx;
  240. }
  241. static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  242. {
  243. struct dsa_slave_priv *p = netdev_priv(dev);
  244. if (p->phy != NULL)
  245. return phy_mii_ioctl(p->phy, ifr, cmd);
  246. return -EOPNOTSUPP;
  247. }
  248. /* Return a bitmask of all ports being currently bridged within a given bridge
  249. * device. Note that on leave, the mask will still return the bitmask of ports
  250. * currently bridged, prior to port removal, and this is exactly what we want.
  251. */
  252. static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
  253. struct net_device *bridge)
  254. {
  255. struct dsa_slave_priv *p;
  256. unsigned int port;
  257. u32 mask = 0;
  258. for (port = 0; port < DSA_MAX_PORTS; port++) {
  259. if (!dsa_is_port_initialized(ds, port))
  260. continue;
  261. p = netdev_priv(ds->ports[port]);
  262. if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
  263. p->bridge_dev == bridge)
  264. mask |= 1 << port;
  265. }
  266. return mask;
  267. }
  268. static int dsa_slave_stp_update(struct net_device *dev, u8 state)
  269. {
  270. struct dsa_slave_priv *p = netdev_priv(dev);
  271. struct dsa_switch *ds = p->parent;
  272. int ret = -EOPNOTSUPP;
  273. if (ds->drv->port_stp_update)
  274. ret = ds->drv->port_stp_update(ds, p->port, state);
  275. return ret;
  276. }
  277. static int dsa_slave_bridge_port_join(struct net_device *dev,
  278. struct net_device *br)
  279. {
  280. struct dsa_slave_priv *p = netdev_priv(dev);
  281. struct dsa_switch *ds = p->parent;
  282. int ret = -EOPNOTSUPP;
  283. p->bridge_dev = br;
  284. if (ds->drv->port_join_bridge)
  285. ret = ds->drv->port_join_bridge(ds, p->port,
  286. dsa_slave_br_port_mask(ds, br));
  287. return ret;
  288. }
  289. static int dsa_slave_bridge_port_leave(struct net_device *dev)
  290. {
  291. struct dsa_slave_priv *p = netdev_priv(dev);
  292. struct dsa_switch *ds = p->parent;
  293. int ret = -EOPNOTSUPP;
  294. if (ds->drv->port_leave_bridge)
  295. ret = ds->drv->port_leave_bridge(ds, p->port,
  296. dsa_slave_br_port_mask(ds, p->bridge_dev));
  297. p->bridge_dev = NULL;
  298. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  299. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  300. */
  301. dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
  302. return ret;
  303. }
  304. static int dsa_slave_parent_id_get(struct net_device *dev,
  305. struct netdev_phys_item_id *psid)
  306. {
  307. struct dsa_slave_priv *p = netdev_priv(dev);
  308. struct dsa_switch *ds = p->parent;
  309. psid->id_len = sizeof(ds->index);
  310. memcpy(&psid->id, &ds->index, psid->id_len);
  311. return 0;
  312. }
  313. static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
  314. {
  315. struct dsa_slave_priv *p = netdev_priv(dev);
  316. return p->xmit(skb, dev);
  317. }
  318. static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
  319. struct net_device *dev)
  320. {
  321. struct dsa_slave_priv *p = netdev_priv(dev);
  322. skb->dev = p->parent->dst->master_netdev;
  323. dev_queue_xmit(skb);
  324. return NETDEV_TX_OK;
  325. }
  326. /* ethtool operations *******************************************************/
  327. static int
  328. dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  329. {
  330. struct dsa_slave_priv *p = netdev_priv(dev);
  331. int err;
  332. err = -EOPNOTSUPP;
  333. if (p->phy != NULL) {
  334. err = phy_read_status(p->phy);
  335. if (err == 0)
  336. err = phy_ethtool_gset(p->phy, cmd);
  337. }
  338. return err;
  339. }
  340. static int
  341. dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  342. {
  343. struct dsa_slave_priv *p = netdev_priv(dev);
  344. if (p->phy != NULL)
  345. return phy_ethtool_sset(p->phy, cmd);
  346. return -EOPNOTSUPP;
  347. }
  348. static void dsa_slave_get_drvinfo(struct net_device *dev,
  349. struct ethtool_drvinfo *drvinfo)
  350. {
  351. strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
  352. strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
  353. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  354. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  355. }
  356. static int dsa_slave_get_regs_len(struct net_device *dev)
  357. {
  358. struct dsa_slave_priv *p = netdev_priv(dev);
  359. struct dsa_switch *ds = p->parent;
  360. if (ds->drv->get_regs_len)
  361. return ds->drv->get_regs_len(ds, p->port);
  362. return -EOPNOTSUPP;
  363. }
  364. static void
  365. dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
  366. {
  367. struct dsa_slave_priv *p = netdev_priv(dev);
  368. struct dsa_switch *ds = p->parent;
  369. if (ds->drv->get_regs)
  370. ds->drv->get_regs(ds, p->port, regs, _p);
  371. }
  372. static int dsa_slave_nway_reset(struct net_device *dev)
  373. {
  374. struct dsa_slave_priv *p = netdev_priv(dev);
  375. if (p->phy != NULL)
  376. return genphy_restart_aneg(p->phy);
  377. return -EOPNOTSUPP;
  378. }
  379. static u32 dsa_slave_get_link(struct net_device *dev)
  380. {
  381. struct dsa_slave_priv *p = netdev_priv(dev);
  382. if (p->phy != NULL) {
  383. genphy_update_link(p->phy);
  384. return p->phy->link;
  385. }
  386. return -EOPNOTSUPP;
  387. }
  388. static int dsa_slave_get_eeprom_len(struct net_device *dev)
  389. {
  390. struct dsa_slave_priv *p = netdev_priv(dev);
  391. struct dsa_switch *ds = p->parent;
  392. if (ds->pd->eeprom_len)
  393. return ds->pd->eeprom_len;
  394. if (ds->drv->get_eeprom_len)
  395. return ds->drv->get_eeprom_len(ds);
  396. return 0;
  397. }
  398. static int dsa_slave_get_eeprom(struct net_device *dev,
  399. struct ethtool_eeprom *eeprom, u8 *data)
  400. {
  401. struct dsa_slave_priv *p = netdev_priv(dev);
  402. struct dsa_switch *ds = p->parent;
  403. if (ds->drv->get_eeprom)
  404. return ds->drv->get_eeprom(ds, eeprom, data);
  405. return -EOPNOTSUPP;
  406. }
  407. static int dsa_slave_set_eeprom(struct net_device *dev,
  408. struct ethtool_eeprom *eeprom, u8 *data)
  409. {
  410. struct dsa_slave_priv *p = netdev_priv(dev);
  411. struct dsa_switch *ds = p->parent;
  412. if (ds->drv->set_eeprom)
  413. return ds->drv->set_eeprom(ds, eeprom, data);
  414. return -EOPNOTSUPP;
  415. }
  416. static void dsa_slave_get_strings(struct net_device *dev,
  417. uint32_t stringset, uint8_t *data)
  418. {
  419. struct dsa_slave_priv *p = netdev_priv(dev);
  420. struct dsa_switch *ds = p->parent;
  421. if (stringset == ETH_SS_STATS) {
  422. int len = ETH_GSTRING_LEN;
  423. strncpy(data, "tx_packets", len);
  424. strncpy(data + len, "tx_bytes", len);
  425. strncpy(data + 2 * len, "rx_packets", len);
  426. strncpy(data + 3 * len, "rx_bytes", len);
  427. if (ds->drv->get_strings != NULL)
  428. ds->drv->get_strings(ds, p->port, data + 4 * len);
  429. }
  430. }
  431. static void dsa_slave_get_ethtool_stats(struct net_device *dev,
  432. struct ethtool_stats *stats,
  433. uint64_t *data)
  434. {
  435. struct dsa_slave_priv *p = netdev_priv(dev);
  436. struct dsa_switch *ds = p->parent;
  437. data[0] = p->dev->stats.tx_packets;
  438. data[1] = p->dev->stats.tx_bytes;
  439. data[2] = p->dev->stats.rx_packets;
  440. data[3] = p->dev->stats.rx_bytes;
  441. if (ds->drv->get_ethtool_stats != NULL)
  442. ds->drv->get_ethtool_stats(ds, p->port, data + 4);
  443. }
  444. static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
  445. {
  446. struct dsa_slave_priv *p = netdev_priv(dev);
  447. struct dsa_switch *ds = p->parent;
  448. if (sset == ETH_SS_STATS) {
  449. int count;
  450. count = 4;
  451. if (ds->drv->get_sset_count != NULL)
  452. count += ds->drv->get_sset_count(ds);
  453. return count;
  454. }
  455. return -EOPNOTSUPP;
  456. }
  457. static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  458. {
  459. struct dsa_slave_priv *p = netdev_priv(dev);
  460. struct dsa_switch *ds = p->parent;
  461. if (ds->drv->get_wol)
  462. ds->drv->get_wol(ds, p->port, w);
  463. }
  464. static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  465. {
  466. struct dsa_slave_priv *p = netdev_priv(dev);
  467. struct dsa_switch *ds = p->parent;
  468. int ret = -EOPNOTSUPP;
  469. if (ds->drv->set_wol)
  470. ret = ds->drv->set_wol(ds, p->port, w);
  471. return ret;
  472. }
  473. static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
  474. {
  475. struct dsa_slave_priv *p = netdev_priv(dev);
  476. struct dsa_switch *ds = p->parent;
  477. int ret;
  478. if (!ds->drv->set_eee)
  479. return -EOPNOTSUPP;
  480. ret = ds->drv->set_eee(ds, p->port, p->phy, e);
  481. if (ret)
  482. return ret;
  483. if (p->phy)
  484. ret = phy_ethtool_set_eee(p->phy, e);
  485. return ret;
  486. }
  487. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  488. {
  489. struct dsa_slave_priv *p = netdev_priv(dev);
  490. struct dsa_switch *ds = p->parent;
  491. int ret;
  492. if (!ds->drv->get_eee)
  493. return -EOPNOTSUPP;
  494. ret = ds->drv->get_eee(ds, p->port, e);
  495. if (ret)
  496. return ret;
  497. if (p->phy)
  498. ret = phy_ethtool_get_eee(p->phy, e);
  499. return ret;
  500. }
  501. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  502. .get_settings = dsa_slave_get_settings,
  503. .set_settings = dsa_slave_set_settings,
  504. .get_drvinfo = dsa_slave_get_drvinfo,
  505. .get_regs_len = dsa_slave_get_regs_len,
  506. .get_regs = dsa_slave_get_regs,
  507. .nway_reset = dsa_slave_nway_reset,
  508. .get_link = dsa_slave_get_link,
  509. .get_eeprom_len = dsa_slave_get_eeprom_len,
  510. .get_eeprom = dsa_slave_get_eeprom,
  511. .set_eeprom = dsa_slave_set_eeprom,
  512. .get_strings = dsa_slave_get_strings,
  513. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  514. .get_sset_count = dsa_slave_get_sset_count,
  515. .set_wol = dsa_slave_set_wol,
  516. .get_wol = dsa_slave_get_wol,
  517. .set_eee = dsa_slave_set_eee,
  518. .get_eee = dsa_slave_get_eee,
  519. };
  520. static const struct net_device_ops dsa_slave_netdev_ops = {
  521. .ndo_open = dsa_slave_open,
  522. .ndo_stop = dsa_slave_close,
  523. .ndo_start_xmit = dsa_slave_xmit,
  524. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  525. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  526. .ndo_set_mac_address = dsa_slave_set_mac_address,
  527. .ndo_fdb_add = dsa_slave_fdb_add,
  528. .ndo_fdb_del = dsa_slave_fdb_del,
  529. .ndo_fdb_dump = dsa_slave_fdb_dump,
  530. .ndo_do_ioctl = dsa_slave_ioctl,
  531. .ndo_get_iflink = dsa_slave_get_iflink,
  532. };
  533. static const struct swdev_ops dsa_slave_swdev_ops = {
  534. .swdev_parent_id_get = dsa_slave_parent_id_get,
  535. .swdev_port_stp_update = dsa_slave_stp_update,
  536. };
  537. static void dsa_slave_adjust_link(struct net_device *dev)
  538. {
  539. struct dsa_slave_priv *p = netdev_priv(dev);
  540. struct dsa_switch *ds = p->parent;
  541. unsigned int status_changed = 0;
  542. if (p->old_link != p->phy->link) {
  543. status_changed = 1;
  544. p->old_link = p->phy->link;
  545. }
  546. if (p->old_duplex != p->phy->duplex) {
  547. status_changed = 1;
  548. p->old_duplex = p->phy->duplex;
  549. }
  550. if (p->old_pause != p->phy->pause) {
  551. status_changed = 1;
  552. p->old_pause = p->phy->pause;
  553. }
  554. if (ds->drv->adjust_link && status_changed)
  555. ds->drv->adjust_link(ds, p->port, p->phy);
  556. if (status_changed)
  557. phy_print_status(p->phy);
  558. }
  559. static int dsa_slave_fixed_link_update(struct net_device *dev,
  560. struct fixed_phy_status *status)
  561. {
  562. struct dsa_slave_priv *p = netdev_priv(dev);
  563. struct dsa_switch *ds = p->parent;
  564. if (ds->drv->fixed_link_update)
  565. ds->drv->fixed_link_update(ds, p->port, status);
  566. return 0;
  567. }
  568. /* slave device setup *******************************************************/
  569. static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
  570. struct net_device *slave_dev,
  571. int addr)
  572. {
  573. struct dsa_switch *ds = p->parent;
  574. p->phy = ds->slave_mii_bus->phy_map[addr];
  575. if (!p->phy)
  576. return -ENODEV;
  577. /* Use already configured phy mode */
  578. p->phy_interface = p->phy->interface;
  579. phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
  580. p->phy_interface);
  581. return 0;
  582. }
  583. static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
  584. struct net_device *slave_dev)
  585. {
  586. struct dsa_switch *ds = p->parent;
  587. struct dsa_chip_data *cd = ds->pd;
  588. struct device_node *phy_dn, *port_dn;
  589. bool phy_is_fixed = false;
  590. u32 phy_flags = 0;
  591. int mode, ret;
  592. port_dn = cd->port_dn[p->port];
  593. mode = of_get_phy_mode(port_dn);
  594. if (mode < 0)
  595. mode = PHY_INTERFACE_MODE_NA;
  596. p->phy_interface = mode;
  597. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  598. if (of_phy_is_fixed_link(port_dn)) {
  599. /* In the case of a fixed PHY, the DT node associated
  600. * to the fixed PHY is the Port DT node
  601. */
  602. ret = of_phy_register_fixed_link(port_dn);
  603. if (ret) {
  604. netdev_err(slave_dev, "failed to register fixed PHY\n");
  605. return ret;
  606. }
  607. phy_is_fixed = true;
  608. phy_dn = port_dn;
  609. }
  610. if (ds->drv->get_phy_flags)
  611. phy_flags = ds->drv->get_phy_flags(ds, p->port);
  612. if (phy_dn) {
  613. ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
  614. /* If this PHY address is part of phys_mii_mask, which means
  615. * that we need to divert reads and writes to/from it, then we
  616. * want to bind this device using the slave MII bus created by
  617. * DSA to make that happen.
  618. */
  619. if (!phy_is_fixed && ret >= 0 &&
  620. (ds->phys_mii_mask & (1 << ret))) {
  621. ret = dsa_slave_phy_connect(p, slave_dev, ret);
  622. if (ret)
  623. return ret;
  624. } else {
  625. p->phy = of_phy_connect(slave_dev, phy_dn,
  626. dsa_slave_adjust_link,
  627. phy_flags,
  628. p->phy_interface);
  629. }
  630. }
  631. if (p->phy && phy_is_fixed)
  632. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  633. /* We could not connect to a designated PHY, so use the switch internal
  634. * MDIO bus instead
  635. */
  636. if (!p->phy) {
  637. ret = dsa_slave_phy_connect(p, slave_dev, p->port);
  638. if (ret)
  639. return ret;
  640. } else {
  641. netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
  642. p->phy->addr, p->phy->drv->name);
  643. }
  644. return 0;
  645. }
  646. int dsa_slave_suspend(struct net_device *slave_dev)
  647. {
  648. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  649. netif_device_detach(slave_dev);
  650. if (p->phy) {
  651. phy_stop(p->phy);
  652. p->old_pause = -1;
  653. p->old_link = -1;
  654. p->old_duplex = -1;
  655. phy_suspend(p->phy);
  656. }
  657. return 0;
  658. }
  659. int dsa_slave_resume(struct net_device *slave_dev)
  660. {
  661. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  662. netif_device_attach(slave_dev);
  663. if (p->phy) {
  664. phy_resume(p->phy);
  665. phy_start(p->phy);
  666. }
  667. return 0;
  668. }
  669. int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
  670. int port, char *name)
  671. {
  672. struct net_device *master = ds->dst->master_netdev;
  673. struct net_device *slave_dev;
  674. struct dsa_slave_priv *p;
  675. int ret;
  676. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  677. NET_NAME_UNKNOWN, ether_setup);
  678. if (slave_dev == NULL)
  679. return -ENOMEM;
  680. slave_dev->features = master->vlan_features;
  681. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  682. eth_hw_addr_inherit(slave_dev, master);
  683. slave_dev->tx_queue_len = 0;
  684. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  685. slave_dev->swdev_ops = &dsa_slave_swdev_ops;
  686. SET_NETDEV_DEV(slave_dev, parent);
  687. slave_dev->dev.of_node = ds->pd->port_dn[port];
  688. slave_dev->vlan_features = master->vlan_features;
  689. p = netdev_priv(slave_dev);
  690. p->dev = slave_dev;
  691. p->parent = ds;
  692. p->port = port;
  693. switch (ds->dst->tag_protocol) {
  694. #ifdef CONFIG_NET_DSA_TAG_DSA
  695. case DSA_TAG_PROTO_DSA:
  696. p->xmit = dsa_netdev_ops.xmit;
  697. break;
  698. #endif
  699. #ifdef CONFIG_NET_DSA_TAG_EDSA
  700. case DSA_TAG_PROTO_EDSA:
  701. p->xmit = edsa_netdev_ops.xmit;
  702. break;
  703. #endif
  704. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  705. case DSA_TAG_PROTO_TRAILER:
  706. p->xmit = trailer_netdev_ops.xmit;
  707. break;
  708. #endif
  709. #ifdef CONFIG_NET_DSA_TAG_BRCM
  710. case DSA_TAG_PROTO_BRCM:
  711. p->xmit = brcm_netdev_ops.xmit;
  712. break;
  713. #endif
  714. default:
  715. p->xmit = dsa_slave_notag_xmit;
  716. break;
  717. }
  718. p->old_pause = -1;
  719. p->old_link = -1;
  720. p->old_duplex = -1;
  721. ret = dsa_slave_phy_setup(p, slave_dev);
  722. if (ret) {
  723. free_netdev(slave_dev);
  724. return ret;
  725. }
  726. ds->ports[port] = slave_dev;
  727. ret = register_netdev(slave_dev);
  728. if (ret) {
  729. netdev_err(master, "error %d registering interface %s\n",
  730. ret, slave_dev->name);
  731. phy_disconnect(p->phy);
  732. ds->ports[port] = NULL;
  733. free_netdev(slave_dev);
  734. return ret;
  735. }
  736. netif_carrier_off(slave_dev);
  737. return 0;
  738. }
  739. static bool dsa_slave_dev_check(struct net_device *dev)
  740. {
  741. return dev->netdev_ops == &dsa_slave_netdev_ops;
  742. }
  743. static int dsa_slave_master_changed(struct net_device *dev)
  744. {
  745. struct net_device *master = netdev_master_upper_dev_get(dev);
  746. struct dsa_slave_priv *p = netdev_priv(dev);
  747. int err = 0;
  748. if (master && master->rtnl_link_ops &&
  749. !strcmp(master->rtnl_link_ops->kind, "bridge"))
  750. err = dsa_slave_bridge_port_join(dev, master);
  751. else if (dsa_port_is_bridged(p))
  752. err = dsa_slave_bridge_port_leave(dev);
  753. return err;
  754. }
  755. int dsa_slave_netdevice_event(struct notifier_block *unused,
  756. unsigned long event, void *ptr)
  757. {
  758. struct net_device *dev;
  759. int err = 0;
  760. switch (event) {
  761. case NETDEV_CHANGEUPPER:
  762. dev = netdev_notifier_info_to_dev(ptr);
  763. if (!dsa_slave_dev_check(dev))
  764. goto out;
  765. err = dsa_slave_master_changed(dev);
  766. if (err)
  767. netdev_warn(dev, "failed to reflect master change\n");
  768. break;
  769. }
  770. out:
  771. return NOTIFY_DONE;
  772. }