slave.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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, -1);
  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_port_attr_set(struct net_device *dev,
  278. struct switchdev_attr *attr)
  279. {
  280. int ret = 0;
  281. switch (attr->id) {
  282. case SWITCHDEV_ATTR_PORT_STP_STATE:
  283. if (attr->trans == SWITCHDEV_TRANS_COMMIT)
  284. ret = dsa_slave_stp_update(dev, attr->u.stp_state);
  285. break;
  286. default:
  287. ret = -EOPNOTSUPP;
  288. break;
  289. }
  290. return ret;
  291. }
  292. static int dsa_slave_bridge_port_join(struct net_device *dev,
  293. struct net_device *br)
  294. {
  295. struct dsa_slave_priv *p = netdev_priv(dev);
  296. struct dsa_switch *ds = p->parent;
  297. int ret = -EOPNOTSUPP;
  298. p->bridge_dev = br;
  299. if (ds->drv->port_join_bridge)
  300. ret = ds->drv->port_join_bridge(ds, p->port,
  301. dsa_slave_br_port_mask(ds, br));
  302. return ret;
  303. }
  304. static int dsa_slave_bridge_port_leave(struct net_device *dev)
  305. {
  306. struct dsa_slave_priv *p = netdev_priv(dev);
  307. struct dsa_switch *ds = p->parent;
  308. int ret = -EOPNOTSUPP;
  309. if (ds->drv->port_leave_bridge)
  310. ret = ds->drv->port_leave_bridge(ds, p->port,
  311. dsa_slave_br_port_mask(ds, p->bridge_dev));
  312. p->bridge_dev = NULL;
  313. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  314. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  315. */
  316. dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
  317. return ret;
  318. }
  319. static int dsa_slave_port_attr_get(struct net_device *dev,
  320. struct switchdev_attr *attr)
  321. {
  322. struct dsa_slave_priv *p = netdev_priv(dev);
  323. struct dsa_switch *ds = p->parent;
  324. switch (attr->id) {
  325. case SWITCHDEV_ATTR_PORT_PARENT_ID:
  326. attr->u.ppid.id_len = sizeof(ds->index);
  327. memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
  328. break;
  329. default:
  330. return -EOPNOTSUPP;
  331. }
  332. return 0;
  333. }
  334. static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
  335. {
  336. struct dsa_slave_priv *p = netdev_priv(dev);
  337. return p->xmit(skb, dev);
  338. }
  339. static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
  340. struct net_device *dev)
  341. {
  342. struct dsa_slave_priv *p = netdev_priv(dev);
  343. skb->dev = p->parent->dst->master_netdev;
  344. dev_queue_xmit(skb);
  345. return NETDEV_TX_OK;
  346. }
  347. /* ethtool operations *******************************************************/
  348. static int
  349. dsa_slave_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  350. {
  351. struct dsa_slave_priv *p = netdev_priv(dev);
  352. int err;
  353. err = -EOPNOTSUPP;
  354. if (p->phy != NULL) {
  355. err = phy_read_status(p->phy);
  356. if (err == 0)
  357. err = phy_ethtool_gset(p->phy, cmd);
  358. }
  359. return err;
  360. }
  361. static int
  362. dsa_slave_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  363. {
  364. struct dsa_slave_priv *p = netdev_priv(dev);
  365. if (p->phy != NULL)
  366. return phy_ethtool_sset(p->phy, cmd);
  367. return -EOPNOTSUPP;
  368. }
  369. static void dsa_slave_get_drvinfo(struct net_device *dev,
  370. struct ethtool_drvinfo *drvinfo)
  371. {
  372. strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
  373. strlcpy(drvinfo->version, dsa_driver_version, sizeof(drvinfo->version));
  374. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  375. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  376. }
  377. static int dsa_slave_get_regs_len(struct net_device *dev)
  378. {
  379. struct dsa_slave_priv *p = netdev_priv(dev);
  380. struct dsa_switch *ds = p->parent;
  381. if (ds->drv->get_regs_len)
  382. return ds->drv->get_regs_len(ds, p->port);
  383. return -EOPNOTSUPP;
  384. }
  385. static void
  386. dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
  387. {
  388. struct dsa_slave_priv *p = netdev_priv(dev);
  389. struct dsa_switch *ds = p->parent;
  390. if (ds->drv->get_regs)
  391. ds->drv->get_regs(ds, p->port, regs, _p);
  392. }
  393. static int dsa_slave_nway_reset(struct net_device *dev)
  394. {
  395. struct dsa_slave_priv *p = netdev_priv(dev);
  396. if (p->phy != NULL)
  397. return genphy_restart_aneg(p->phy);
  398. return -EOPNOTSUPP;
  399. }
  400. static u32 dsa_slave_get_link(struct net_device *dev)
  401. {
  402. struct dsa_slave_priv *p = netdev_priv(dev);
  403. if (p->phy != NULL) {
  404. genphy_update_link(p->phy);
  405. return p->phy->link;
  406. }
  407. return -EOPNOTSUPP;
  408. }
  409. static int dsa_slave_get_eeprom_len(struct net_device *dev)
  410. {
  411. struct dsa_slave_priv *p = netdev_priv(dev);
  412. struct dsa_switch *ds = p->parent;
  413. if (ds->pd->eeprom_len)
  414. return ds->pd->eeprom_len;
  415. if (ds->drv->get_eeprom_len)
  416. return ds->drv->get_eeprom_len(ds);
  417. return 0;
  418. }
  419. static int dsa_slave_get_eeprom(struct net_device *dev,
  420. struct ethtool_eeprom *eeprom, u8 *data)
  421. {
  422. struct dsa_slave_priv *p = netdev_priv(dev);
  423. struct dsa_switch *ds = p->parent;
  424. if (ds->drv->get_eeprom)
  425. return ds->drv->get_eeprom(ds, eeprom, data);
  426. return -EOPNOTSUPP;
  427. }
  428. static int dsa_slave_set_eeprom(struct net_device *dev,
  429. struct ethtool_eeprom *eeprom, u8 *data)
  430. {
  431. struct dsa_slave_priv *p = netdev_priv(dev);
  432. struct dsa_switch *ds = p->parent;
  433. if (ds->drv->set_eeprom)
  434. return ds->drv->set_eeprom(ds, eeprom, data);
  435. return -EOPNOTSUPP;
  436. }
  437. static void dsa_slave_get_strings(struct net_device *dev,
  438. uint32_t stringset, uint8_t *data)
  439. {
  440. struct dsa_slave_priv *p = netdev_priv(dev);
  441. struct dsa_switch *ds = p->parent;
  442. if (stringset == ETH_SS_STATS) {
  443. int len = ETH_GSTRING_LEN;
  444. strncpy(data, "tx_packets", len);
  445. strncpy(data + len, "tx_bytes", len);
  446. strncpy(data + 2 * len, "rx_packets", len);
  447. strncpy(data + 3 * len, "rx_bytes", len);
  448. if (ds->drv->get_strings != NULL)
  449. ds->drv->get_strings(ds, p->port, data + 4 * len);
  450. }
  451. }
  452. static void dsa_slave_get_ethtool_stats(struct net_device *dev,
  453. struct ethtool_stats *stats,
  454. uint64_t *data)
  455. {
  456. struct dsa_slave_priv *p = netdev_priv(dev);
  457. struct dsa_switch *ds = p->parent;
  458. data[0] = p->dev->stats.tx_packets;
  459. data[1] = p->dev->stats.tx_bytes;
  460. data[2] = p->dev->stats.rx_packets;
  461. data[3] = p->dev->stats.rx_bytes;
  462. if (ds->drv->get_ethtool_stats != NULL)
  463. ds->drv->get_ethtool_stats(ds, p->port, data + 4);
  464. }
  465. static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
  466. {
  467. struct dsa_slave_priv *p = netdev_priv(dev);
  468. struct dsa_switch *ds = p->parent;
  469. if (sset == ETH_SS_STATS) {
  470. int count;
  471. count = 4;
  472. if (ds->drv->get_sset_count != NULL)
  473. count += ds->drv->get_sset_count(ds);
  474. return count;
  475. }
  476. return -EOPNOTSUPP;
  477. }
  478. static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  479. {
  480. struct dsa_slave_priv *p = netdev_priv(dev);
  481. struct dsa_switch *ds = p->parent;
  482. if (ds->drv->get_wol)
  483. ds->drv->get_wol(ds, p->port, w);
  484. }
  485. static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  486. {
  487. struct dsa_slave_priv *p = netdev_priv(dev);
  488. struct dsa_switch *ds = p->parent;
  489. int ret = -EOPNOTSUPP;
  490. if (ds->drv->set_wol)
  491. ret = ds->drv->set_wol(ds, p->port, w);
  492. return ret;
  493. }
  494. static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
  495. {
  496. struct dsa_slave_priv *p = netdev_priv(dev);
  497. struct dsa_switch *ds = p->parent;
  498. int ret;
  499. if (!ds->drv->set_eee)
  500. return -EOPNOTSUPP;
  501. ret = ds->drv->set_eee(ds, p->port, p->phy, e);
  502. if (ret)
  503. return ret;
  504. if (p->phy)
  505. ret = phy_ethtool_set_eee(p->phy, e);
  506. return ret;
  507. }
  508. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  509. {
  510. struct dsa_slave_priv *p = netdev_priv(dev);
  511. struct dsa_switch *ds = p->parent;
  512. int ret;
  513. if (!ds->drv->get_eee)
  514. return -EOPNOTSUPP;
  515. ret = ds->drv->get_eee(ds, p->port, e);
  516. if (ret)
  517. return ret;
  518. if (p->phy)
  519. ret = phy_ethtool_get_eee(p->phy, e);
  520. return ret;
  521. }
  522. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  523. .get_settings = dsa_slave_get_settings,
  524. .set_settings = dsa_slave_set_settings,
  525. .get_drvinfo = dsa_slave_get_drvinfo,
  526. .get_regs_len = dsa_slave_get_regs_len,
  527. .get_regs = dsa_slave_get_regs,
  528. .nway_reset = dsa_slave_nway_reset,
  529. .get_link = dsa_slave_get_link,
  530. .get_eeprom_len = dsa_slave_get_eeprom_len,
  531. .get_eeprom = dsa_slave_get_eeprom,
  532. .set_eeprom = dsa_slave_set_eeprom,
  533. .get_strings = dsa_slave_get_strings,
  534. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  535. .get_sset_count = dsa_slave_get_sset_count,
  536. .set_wol = dsa_slave_set_wol,
  537. .get_wol = dsa_slave_get_wol,
  538. .set_eee = dsa_slave_set_eee,
  539. .get_eee = dsa_slave_get_eee,
  540. };
  541. static const struct net_device_ops dsa_slave_netdev_ops = {
  542. .ndo_open = dsa_slave_open,
  543. .ndo_stop = dsa_slave_close,
  544. .ndo_start_xmit = dsa_slave_xmit,
  545. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  546. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  547. .ndo_set_mac_address = dsa_slave_set_mac_address,
  548. .ndo_fdb_add = dsa_slave_fdb_add,
  549. .ndo_fdb_del = dsa_slave_fdb_del,
  550. .ndo_fdb_dump = dsa_slave_fdb_dump,
  551. .ndo_do_ioctl = dsa_slave_ioctl,
  552. .ndo_get_iflink = dsa_slave_get_iflink,
  553. };
  554. static const struct switchdev_ops dsa_slave_switchdev_ops = {
  555. .switchdev_port_attr_get = dsa_slave_port_attr_get,
  556. .switchdev_port_attr_set = dsa_slave_port_attr_set,
  557. };
  558. static void dsa_slave_adjust_link(struct net_device *dev)
  559. {
  560. struct dsa_slave_priv *p = netdev_priv(dev);
  561. struct dsa_switch *ds = p->parent;
  562. unsigned int status_changed = 0;
  563. if (p->old_link != p->phy->link) {
  564. status_changed = 1;
  565. p->old_link = p->phy->link;
  566. }
  567. if (p->old_duplex != p->phy->duplex) {
  568. status_changed = 1;
  569. p->old_duplex = p->phy->duplex;
  570. }
  571. if (p->old_pause != p->phy->pause) {
  572. status_changed = 1;
  573. p->old_pause = p->phy->pause;
  574. }
  575. if (ds->drv->adjust_link && status_changed)
  576. ds->drv->adjust_link(ds, p->port, p->phy);
  577. if (status_changed)
  578. phy_print_status(p->phy);
  579. }
  580. static int dsa_slave_fixed_link_update(struct net_device *dev,
  581. struct fixed_phy_status *status)
  582. {
  583. struct dsa_slave_priv *p = netdev_priv(dev);
  584. struct dsa_switch *ds = p->parent;
  585. if (ds->drv->fixed_link_update)
  586. ds->drv->fixed_link_update(ds, p->port, status);
  587. return 0;
  588. }
  589. /* slave device setup *******************************************************/
  590. static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
  591. struct net_device *slave_dev,
  592. int addr)
  593. {
  594. struct dsa_switch *ds = p->parent;
  595. p->phy = ds->slave_mii_bus->phy_map[addr];
  596. if (!p->phy)
  597. return -ENODEV;
  598. /* Use already configured phy mode */
  599. p->phy_interface = p->phy->interface;
  600. phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
  601. p->phy_interface);
  602. return 0;
  603. }
  604. static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
  605. struct net_device *slave_dev)
  606. {
  607. struct dsa_switch *ds = p->parent;
  608. struct dsa_chip_data *cd = ds->pd;
  609. struct device_node *phy_dn, *port_dn;
  610. bool phy_is_fixed = false;
  611. u32 phy_flags = 0;
  612. int mode, ret;
  613. port_dn = cd->port_dn[p->port];
  614. mode = of_get_phy_mode(port_dn);
  615. if (mode < 0)
  616. mode = PHY_INTERFACE_MODE_NA;
  617. p->phy_interface = mode;
  618. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  619. if (of_phy_is_fixed_link(port_dn)) {
  620. /* In the case of a fixed PHY, the DT node associated
  621. * to the fixed PHY is the Port DT node
  622. */
  623. ret = of_phy_register_fixed_link(port_dn);
  624. if (ret) {
  625. netdev_err(slave_dev, "failed to register fixed PHY\n");
  626. return ret;
  627. }
  628. phy_is_fixed = true;
  629. phy_dn = port_dn;
  630. }
  631. if (ds->drv->get_phy_flags)
  632. phy_flags = ds->drv->get_phy_flags(ds, p->port);
  633. if (phy_dn) {
  634. ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
  635. /* If this PHY address is part of phys_mii_mask, which means
  636. * that we need to divert reads and writes to/from it, then we
  637. * want to bind this device using the slave MII bus created by
  638. * DSA to make that happen.
  639. */
  640. if (!phy_is_fixed && ret >= 0 &&
  641. (ds->phys_mii_mask & (1 << ret))) {
  642. ret = dsa_slave_phy_connect(p, slave_dev, ret);
  643. if (ret)
  644. return ret;
  645. } else {
  646. p->phy = of_phy_connect(slave_dev, phy_dn,
  647. dsa_slave_adjust_link,
  648. phy_flags,
  649. p->phy_interface);
  650. }
  651. }
  652. if (p->phy && phy_is_fixed)
  653. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  654. /* We could not connect to a designated PHY, so use the switch internal
  655. * MDIO bus instead
  656. */
  657. if (!p->phy) {
  658. ret = dsa_slave_phy_connect(p, slave_dev, p->port);
  659. if (ret)
  660. return ret;
  661. } else {
  662. netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
  663. p->phy->addr, p->phy->drv->name);
  664. }
  665. return 0;
  666. }
  667. static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
  668. static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
  669. struct netdev_queue *txq,
  670. void *_unused)
  671. {
  672. lockdep_set_class(&txq->_xmit_lock,
  673. &dsa_slave_netdev_xmit_lock_key);
  674. }
  675. int dsa_slave_suspend(struct net_device *slave_dev)
  676. {
  677. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  678. if (p->phy) {
  679. phy_stop(p->phy);
  680. p->old_pause = -1;
  681. p->old_link = -1;
  682. p->old_duplex = -1;
  683. phy_suspend(p->phy);
  684. }
  685. return 0;
  686. }
  687. int dsa_slave_resume(struct net_device *slave_dev)
  688. {
  689. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  690. netif_device_attach(slave_dev);
  691. if (p->phy) {
  692. phy_resume(p->phy);
  693. phy_start(p->phy);
  694. }
  695. return 0;
  696. }
  697. int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
  698. int port, char *name)
  699. {
  700. struct net_device *master = ds->dst->master_netdev;
  701. struct net_device *slave_dev;
  702. struct dsa_slave_priv *p;
  703. int ret;
  704. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  705. NET_NAME_UNKNOWN, ether_setup);
  706. if (slave_dev == NULL)
  707. return -ENOMEM;
  708. slave_dev->features = master->vlan_features;
  709. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  710. eth_hw_addr_inherit(slave_dev, master);
  711. slave_dev->tx_queue_len = 0;
  712. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  713. slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
  714. netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
  715. NULL);
  716. SET_NETDEV_DEV(slave_dev, parent);
  717. slave_dev->dev.of_node = ds->pd->port_dn[port];
  718. slave_dev->vlan_features = master->vlan_features;
  719. p = netdev_priv(slave_dev);
  720. p->dev = slave_dev;
  721. p->parent = ds;
  722. p->port = port;
  723. switch (ds->dst->tag_protocol) {
  724. #ifdef CONFIG_NET_DSA_TAG_DSA
  725. case DSA_TAG_PROTO_DSA:
  726. p->xmit = dsa_netdev_ops.xmit;
  727. break;
  728. #endif
  729. #ifdef CONFIG_NET_DSA_TAG_EDSA
  730. case DSA_TAG_PROTO_EDSA:
  731. p->xmit = edsa_netdev_ops.xmit;
  732. break;
  733. #endif
  734. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  735. case DSA_TAG_PROTO_TRAILER:
  736. p->xmit = trailer_netdev_ops.xmit;
  737. break;
  738. #endif
  739. #ifdef CONFIG_NET_DSA_TAG_BRCM
  740. case DSA_TAG_PROTO_BRCM:
  741. p->xmit = brcm_netdev_ops.xmit;
  742. break;
  743. #endif
  744. default:
  745. p->xmit = dsa_slave_notag_xmit;
  746. break;
  747. }
  748. p->old_pause = -1;
  749. p->old_link = -1;
  750. p->old_duplex = -1;
  751. ret = dsa_slave_phy_setup(p, slave_dev);
  752. if (ret) {
  753. free_netdev(slave_dev);
  754. return ret;
  755. }
  756. ds->ports[port] = slave_dev;
  757. ret = register_netdev(slave_dev);
  758. if (ret) {
  759. netdev_err(master, "error %d registering interface %s\n",
  760. ret, slave_dev->name);
  761. phy_disconnect(p->phy);
  762. ds->ports[port] = NULL;
  763. free_netdev(slave_dev);
  764. return ret;
  765. }
  766. netif_carrier_off(slave_dev);
  767. return 0;
  768. }
  769. static bool dsa_slave_dev_check(struct net_device *dev)
  770. {
  771. return dev->netdev_ops == &dsa_slave_netdev_ops;
  772. }
  773. static int dsa_slave_master_changed(struct net_device *dev)
  774. {
  775. struct net_device *master = netdev_master_upper_dev_get(dev);
  776. struct dsa_slave_priv *p = netdev_priv(dev);
  777. int err = 0;
  778. if (master && master->rtnl_link_ops &&
  779. !strcmp(master->rtnl_link_ops->kind, "bridge"))
  780. err = dsa_slave_bridge_port_join(dev, master);
  781. else if (dsa_port_is_bridged(p))
  782. err = dsa_slave_bridge_port_leave(dev);
  783. return err;
  784. }
  785. int dsa_slave_netdevice_event(struct notifier_block *unused,
  786. unsigned long event, void *ptr)
  787. {
  788. struct net_device *dev;
  789. int err = 0;
  790. switch (event) {
  791. case NETDEV_CHANGEUPPER:
  792. dev = netdev_notifier_info_to_dev(ptr);
  793. if (!dsa_slave_dev_check(dev))
  794. goto out;
  795. err = dsa_slave_master_changed(dev);
  796. if (err)
  797. netdev_warn(dev, "failed to reflect master change\n");
  798. break;
  799. }
  800. out:
  801. return NOTIFY_DONE;
  802. }