slave.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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 <linux/mdio.h>
  18. #include <linux/list.h>
  19. #include <net/rtnetlink.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/tc_act/tc_mirred.h>
  22. #include <linux/if_bridge.h>
  23. #include <linux/netpoll.h>
  24. #include "dsa_priv.h"
  25. static bool dsa_slave_dev_check(struct net_device *dev);
  26. /* slave mii_bus handling ***************************************************/
  27. static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
  28. {
  29. struct dsa_switch *ds = bus->priv;
  30. if (ds->phys_mii_mask & (1 << addr))
  31. return ds->ops->phy_read(ds, addr, reg);
  32. return 0xffff;
  33. }
  34. static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
  35. {
  36. struct dsa_switch *ds = bus->priv;
  37. if (ds->phys_mii_mask & (1 << addr))
  38. return ds->ops->phy_write(ds, addr, reg, val);
  39. return 0;
  40. }
  41. void dsa_slave_mii_bus_init(struct dsa_switch *ds)
  42. {
  43. ds->slave_mii_bus->priv = (void *)ds;
  44. ds->slave_mii_bus->name = "dsa slave smi";
  45. ds->slave_mii_bus->read = dsa_slave_phy_read;
  46. ds->slave_mii_bus->write = dsa_slave_phy_write;
  47. snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
  48. ds->dst->tree, ds->index);
  49. ds->slave_mii_bus->parent = ds->dev;
  50. ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
  51. }
  52. /* slave device handling ****************************************************/
  53. static int dsa_slave_get_iflink(const struct net_device *dev)
  54. {
  55. struct dsa_slave_priv *p = netdev_priv(dev);
  56. return dsa_master_netdev(p)->ifindex;
  57. }
  58. static int dsa_slave_open(struct net_device *dev)
  59. {
  60. struct dsa_slave_priv *p = netdev_priv(dev);
  61. struct dsa_port *dp = p->dp;
  62. struct dsa_switch *ds = dp->ds;
  63. struct net_device *master = dsa_master_netdev(p);
  64. u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING;
  65. int err;
  66. if (!(master->flags & IFF_UP))
  67. return -ENETDOWN;
  68. if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
  69. err = dev_uc_add(master, dev->dev_addr);
  70. if (err < 0)
  71. goto out;
  72. }
  73. if (dev->flags & IFF_ALLMULTI) {
  74. err = dev_set_allmulti(master, 1);
  75. if (err < 0)
  76. goto del_unicast;
  77. }
  78. if (dev->flags & IFF_PROMISC) {
  79. err = dev_set_promiscuity(master, 1);
  80. if (err < 0)
  81. goto clear_allmulti;
  82. }
  83. if (ds->ops->port_enable) {
  84. err = ds->ops->port_enable(ds, p->dp->index, p->phy);
  85. if (err)
  86. goto clear_promisc;
  87. }
  88. dsa_port_set_state_now(p->dp, 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 = dsa_master_netdev(p);
  108. struct dsa_switch *ds = p->dp->ds;
  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->ops->port_disable)
  120. ds->ops->port_disable(ds, p->dp->index, p->phy);
  121. dsa_port_set_state_now(p->dp, BR_STATE_DISABLED);
  122. return 0;
  123. }
  124. static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
  125. {
  126. struct dsa_slave_priv *p = netdev_priv(dev);
  127. struct net_device *master = dsa_master_netdev(p);
  128. if (change & IFF_ALLMULTI)
  129. dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
  130. if (change & IFF_PROMISC)
  131. dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
  132. }
  133. static void dsa_slave_set_rx_mode(struct net_device *dev)
  134. {
  135. struct dsa_slave_priv *p = netdev_priv(dev);
  136. struct net_device *master = dsa_master_netdev(p);
  137. dev_mc_sync(master, dev);
  138. dev_uc_sync(master, dev);
  139. }
  140. static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
  141. {
  142. struct dsa_slave_priv *p = netdev_priv(dev);
  143. struct net_device *master = dsa_master_netdev(p);
  144. struct sockaddr *addr = a;
  145. int err;
  146. if (!is_valid_ether_addr(addr->sa_data))
  147. return -EADDRNOTAVAIL;
  148. if (!(dev->flags & IFF_UP))
  149. goto out;
  150. if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
  151. err = dev_uc_add(master, addr->sa_data);
  152. if (err < 0)
  153. return err;
  154. }
  155. if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
  156. dev_uc_del(master, dev->dev_addr);
  157. out:
  158. ether_addr_copy(dev->dev_addr, addr->sa_data);
  159. return 0;
  160. }
  161. static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  162. {
  163. struct dsa_slave_priv *p = netdev_priv(dev);
  164. if (p->phy != NULL)
  165. return phy_mii_ioctl(p->phy, ifr, cmd);
  166. return -EOPNOTSUPP;
  167. }
  168. static int dsa_slave_port_attr_set(struct net_device *dev,
  169. const struct switchdev_attr *attr,
  170. struct switchdev_trans *trans)
  171. {
  172. struct dsa_slave_priv *p = netdev_priv(dev);
  173. struct dsa_port *dp = p->dp;
  174. int ret;
  175. switch (attr->id) {
  176. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  177. ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
  178. break;
  179. case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
  180. ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
  181. trans);
  182. break;
  183. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  184. ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
  185. break;
  186. default:
  187. ret = -EOPNOTSUPP;
  188. break;
  189. }
  190. return ret;
  191. }
  192. static int dsa_slave_port_obj_add(struct net_device *dev,
  193. const struct switchdev_obj *obj,
  194. struct switchdev_trans *trans)
  195. {
  196. struct dsa_slave_priv *p = netdev_priv(dev);
  197. struct dsa_port *dp = p->dp;
  198. int err;
  199. /* For the prepare phase, ensure the full set of changes is feasable in
  200. * one go in order to signal a failure properly. If an operation is not
  201. * supported, return -EOPNOTSUPP.
  202. */
  203. switch (obj->id) {
  204. case SWITCHDEV_OBJ_ID_PORT_FDB:
  205. err = dsa_port_fdb_add(dp, SWITCHDEV_OBJ_PORT_FDB(obj), trans);
  206. break;
  207. case SWITCHDEV_OBJ_ID_PORT_MDB:
  208. err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
  209. break;
  210. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  211. err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
  212. trans);
  213. break;
  214. default:
  215. err = -EOPNOTSUPP;
  216. break;
  217. }
  218. return err;
  219. }
  220. static int dsa_slave_port_obj_del(struct net_device *dev,
  221. const struct switchdev_obj *obj)
  222. {
  223. struct dsa_slave_priv *p = netdev_priv(dev);
  224. struct dsa_port *dp = p->dp;
  225. int err;
  226. switch (obj->id) {
  227. case SWITCHDEV_OBJ_ID_PORT_FDB:
  228. err = dsa_port_fdb_del(dp, SWITCHDEV_OBJ_PORT_FDB(obj));
  229. break;
  230. case SWITCHDEV_OBJ_ID_PORT_MDB:
  231. err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
  232. break;
  233. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  234. err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
  235. break;
  236. default:
  237. err = -EOPNOTSUPP;
  238. break;
  239. }
  240. return err;
  241. }
  242. static int dsa_slave_port_obj_dump(struct net_device *dev,
  243. struct switchdev_obj *obj,
  244. switchdev_obj_dump_cb_t *cb)
  245. {
  246. struct dsa_slave_priv *p = netdev_priv(dev);
  247. struct dsa_port *dp = p->dp;
  248. int err;
  249. switch (obj->id) {
  250. case SWITCHDEV_OBJ_ID_PORT_FDB:
  251. err = dsa_port_fdb_dump(dp, SWITCHDEV_OBJ_PORT_FDB(obj), cb);
  252. break;
  253. case SWITCHDEV_OBJ_ID_PORT_MDB:
  254. err = dsa_port_mdb_dump(dp, SWITCHDEV_OBJ_PORT_MDB(obj), cb);
  255. break;
  256. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  257. err = dsa_port_vlan_dump(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
  258. break;
  259. default:
  260. err = -EOPNOTSUPP;
  261. break;
  262. }
  263. return err;
  264. }
  265. static int dsa_slave_port_attr_get(struct net_device *dev,
  266. struct switchdev_attr *attr)
  267. {
  268. struct dsa_slave_priv *p = netdev_priv(dev);
  269. struct dsa_switch *ds = p->dp->ds;
  270. switch (attr->id) {
  271. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  272. attr->u.ppid.id_len = sizeof(ds->index);
  273. memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len);
  274. break;
  275. default:
  276. return -EOPNOTSUPP;
  277. }
  278. return 0;
  279. }
  280. static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p,
  281. struct sk_buff *skb)
  282. {
  283. #ifdef CONFIG_NET_POLL_CONTROLLER
  284. if (p->netpoll)
  285. netpoll_send_skb(p->netpoll, skb);
  286. #else
  287. BUG();
  288. #endif
  289. return NETDEV_TX_OK;
  290. }
  291. static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
  292. {
  293. struct dsa_slave_priv *p = netdev_priv(dev);
  294. struct sk_buff *nskb;
  295. u64_stats_update_begin(&p->stats64.syncp);
  296. p->stats64.tx_packets++;
  297. p->stats64.tx_bytes += skb->len;
  298. u64_stats_update_end(&p->stats64.syncp);
  299. /* Transmit function may have to reallocate the original SKB,
  300. * in which case it must have freed it. Only free it here on error.
  301. */
  302. nskb = p->xmit(skb, dev);
  303. if (!nskb) {
  304. kfree_skb(skb);
  305. return NETDEV_TX_OK;
  306. }
  307. /* SKB for netpoll still need to be mangled with the protocol-specific
  308. * tag to be successfully transmitted
  309. */
  310. if (unlikely(netpoll_tx_running(dev)))
  311. return dsa_netpoll_send_skb(p, nskb);
  312. /* Queue the SKB for transmission on the parent interface, but
  313. * do not modify its EtherType
  314. */
  315. nskb->dev = dsa_master_netdev(p);
  316. dev_queue_xmit(nskb);
  317. return NETDEV_TX_OK;
  318. }
  319. /* ethtool operations *******************************************************/
  320. static int
  321. dsa_slave_get_link_ksettings(struct net_device *dev,
  322. struct ethtool_link_ksettings *cmd)
  323. {
  324. struct dsa_slave_priv *p = netdev_priv(dev);
  325. if (!p->phy)
  326. return -EOPNOTSUPP;
  327. phy_ethtool_ksettings_get(p->phy, cmd);
  328. return 0;
  329. }
  330. static int
  331. dsa_slave_set_link_ksettings(struct net_device *dev,
  332. const struct ethtool_link_ksettings *cmd)
  333. {
  334. struct dsa_slave_priv *p = netdev_priv(dev);
  335. if (p->phy != NULL)
  336. return phy_ethtool_ksettings_set(p->phy, cmd);
  337. return -EOPNOTSUPP;
  338. }
  339. static void dsa_slave_get_drvinfo(struct net_device *dev,
  340. struct ethtool_drvinfo *drvinfo)
  341. {
  342. strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
  343. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  344. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  345. }
  346. static int dsa_slave_get_regs_len(struct net_device *dev)
  347. {
  348. struct dsa_slave_priv *p = netdev_priv(dev);
  349. struct dsa_switch *ds = p->dp->ds;
  350. if (ds->ops->get_regs_len)
  351. return ds->ops->get_regs_len(ds, p->dp->index);
  352. return -EOPNOTSUPP;
  353. }
  354. static void
  355. dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
  356. {
  357. struct dsa_slave_priv *p = netdev_priv(dev);
  358. struct dsa_switch *ds = p->dp->ds;
  359. if (ds->ops->get_regs)
  360. ds->ops->get_regs(ds, p->dp->index, regs, _p);
  361. }
  362. static int dsa_slave_nway_reset(struct net_device *dev)
  363. {
  364. struct dsa_slave_priv *p = netdev_priv(dev);
  365. if (p->phy != NULL)
  366. return genphy_restart_aneg(p->phy);
  367. return -EOPNOTSUPP;
  368. }
  369. static u32 dsa_slave_get_link(struct net_device *dev)
  370. {
  371. struct dsa_slave_priv *p = netdev_priv(dev);
  372. if (p->phy != NULL) {
  373. genphy_update_link(p->phy);
  374. return p->phy->link;
  375. }
  376. return -EOPNOTSUPP;
  377. }
  378. static int dsa_slave_get_eeprom_len(struct net_device *dev)
  379. {
  380. struct dsa_slave_priv *p = netdev_priv(dev);
  381. struct dsa_switch *ds = p->dp->ds;
  382. if (ds->cd && ds->cd->eeprom_len)
  383. return ds->cd->eeprom_len;
  384. if (ds->ops->get_eeprom_len)
  385. return ds->ops->get_eeprom_len(ds);
  386. return 0;
  387. }
  388. static int dsa_slave_get_eeprom(struct net_device *dev,
  389. struct ethtool_eeprom *eeprom, u8 *data)
  390. {
  391. struct dsa_slave_priv *p = netdev_priv(dev);
  392. struct dsa_switch *ds = p->dp->ds;
  393. if (ds->ops->get_eeprom)
  394. return ds->ops->get_eeprom(ds, eeprom, data);
  395. return -EOPNOTSUPP;
  396. }
  397. static int dsa_slave_set_eeprom(struct net_device *dev,
  398. struct ethtool_eeprom *eeprom, u8 *data)
  399. {
  400. struct dsa_slave_priv *p = netdev_priv(dev);
  401. struct dsa_switch *ds = p->dp->ds;
  402. if (ds->ops->set_eeprom)
  403. return ds->ops->set_eeprom(ds, eeprom, data);
  404. return -EOPNOTSUPP;
  405. }
  406. static void dsa_slave_get_strings(struct net_device *dev,
  407. uint32_t stringset, uint8_t *data)
  408. {
  409. struct dsa_slave_priv *p = netdev_priv(dev);
  410. struct dsa_switch *ds = p->dp->ds;
  411. if (stringset == ETH_SS_STATS) {
  412. int len = ETH_GSTRING_LEN;
  413. strncpy(data, "tx_packets", len);
  414. strncpy(data + len, "tx_bytes", len);
  415. strncpy(data + 2 * len, "rx_packets", len);
  416. strncpy(data + 3 * len, "rx_bytes", len);
  417. if (ds->ops->get_strings)
  418. ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
  419. }
  420. }
  421. static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
  422. struct ethtool_stats *stats,
  423. uint64_t *data)
  424. {
  425. struct dsa_switch_tree *dst = dev->dsa_ptr;
  426. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  427. struct dsa_switch *ds = cpu_dp->ds;
  428. s8 cpu_port = cpu_dp->index;
  429. int count = 0;
  430. if (cpu_dp->ethtool_ops.get_sset_count) {
  431. count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
  432. cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data);
  433. }
  434. if (ds->ops->get_ethtool_stats)
  435. ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
  436. }
  437. static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
  438. {
  439. struct dsa_switch_tree *dst = dev->dsa_ptr;
  440. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  441. struct dsa_switch *ds = cpu_dp->ds;
  442. int count = 0;
  443. if (cpu_dp->ethtool_ops.get_sset_count)
  444. count += cpu_dp->ethtool_ops.get_sset_count(dev, sset);
  445. if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
  446. count += ds->ops->get_sset_count(ds);
  447. return count;
  448. }
  449. static void dsa_cpu_port_get_strings(struct net_device *dev,
  450. uint32_t stringset, uint8_t *data)
  451. {
  452. struct dsa_switch_tree *dst = dev->dsa_ptr;
  453. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  454. struct dsa_switch *ds = cpu_dp->ds;
  455. s8 cpu_port = cpu_dp->index;
  456. int len = ETH_GSTRING_LEN;
  457. int mcount = 0, count;
  458. unsigned int i;
  459. uint8_t pfx[4];
  460. uint8_t *ndata;
  461. snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
  462. /* We do not want to be NULL-terminated, since this is a prefix */
  463. pfx[sizeof(pfx) - 1] = '_';
  464. if (cpu_dp->ethtool_ops.get_sset_count) {
  465. mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
  466. cpu_dp->ethtool_ops.get_strings(dev, stringset, data);
  467. }
  468. if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
  469. ndata = data + mcount * len;
  470. /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
  471. * the output after to prepend our CPU port prefix we
  472. * constructed earlier
  473. */
  474. ds->ops->get_strings(ds, cpu_port, ndata);
  475. count = ds->ops->get_sset_count(ds);
  476. for (i = 0; i < count; i++) {
  477. memmove(ndata + (i * len + sizeof(pfx)),
  478. ndata + i * len, len - sizeof(pfx));
  479. memcpy(ndata + i * len, pfx, sizeof(pfx));
  480. }
  481. }
  482. }
  483. static void dsa_slave_get_ethtool_stats(struct net_device *dev,
  484. struct ethtool_stats *stats,
  485. uint64_t *data)
  486. {
  487. struct dsa_slave_priv *p = netdev_priv(dev);
  488. struct dsa_switch *ds = p->dp->ds;
  489. unsigned int start;
  490. do {
  491. start = u64_stats_fetch_begin_irq(&p->stats64.syncp);
  492. data[0] = p->stats64.tx_packets;
  493. data[1] = p->stats64.tx_bytes;
  494. data[2] = p->stats64.rx_packets;
  495. data[3] = p->stats64.rx_bytes;
  496. } while (u64_stats_fetch_retry_irq(&p->stats64.syncp, start));
  497. if (ds->ops->get_ethtool_stats)
  498. ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
  499. }
  500. static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
  501. {
  502. struct dsa_slave_priv *p = netdev_priv(dev);
  503. struct dsa_switch *ds = p->dp->ds;
  504. if (sset == ETH_SS_STATS) {
  505. int count;
  506. count = 4;
  507. if (ds->ops->get_sset_count)
  508. count += ds->ops->get_sset_count(ds);
  509. return count;
  510. }
  511. return -EOPNOTSUPP;
  512. }
  513. static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  514. {
  515. struct dsa_slave_priv *p = netdev_priv(dev);
  516. struct dsa_switch *ds = p->dp->ds;
  517. if (ds->ops->get_wol)
  518. ds->ops->get_wol(ds, p->dp->index, w);
  519. }
  520. static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  521. {
  522. struct dsa_slave_priv *p = netdev_priv(dev);
  523. struct dsa_switch *ds = p->dp->ds;
  524. int ret = -EOPNOTSUPP;
  525. if (ds->ops->set_wol)
  526. ret = ds->ops->set_wol(ds, p->dp->index, w);
  527. return ret;
  528. }
  529. static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
  530. {
  531. struct dsa_slave_priv *p = netdev_priv(dev);
  532. struct dsa_switch *ds = p->dp->ds;
  533. int ret;
  534. /* Port's PHY and MAC both need to be EEE capable */
  535. if (!p->phy)
  536. return -ENODEV;
  537. if (!ds->ops->set_mac_eee)
  538. return -EOPNOTSUPP;
  539. ret = ds->ops->set_mac_eee(ds, p->dp->index, e);
  540. if (ret)
  541. return ret;
  542. if (e->eee_enabled) {
  543. ret = phy_init_eee(p->phy, 0);
  544. if (ret)
  545. return ret;
  546. }
  547. return phy_ethtool_set_eee(p->phy, e);
  548. }
  549. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  550. {
  551. struct dsa_slave_priv *p = netdev_priv(dev);
  552. struct dsa_switch *ds = p->dp->ds;
  553. int ret;
  554. /* Port's PHY and MAC both need to be EEE capable */
  555. if (!p->phy)
  556. return -ENODEV;
  557. if (!ds->ops->get_mac_eee)
  558. return -EOPNOTSUPP;
  559. ret = ds->ops->get_mac_eee(ds, p->dp->index, e);
  560. if (ret)
  561. return ret;
  562. return phy_ethtool_get_eee(p->phy, e);
  563. }
  564. #ifdef CONFIG_NET_POLL_CONTROLLER
  565. static int dsa_slave_netpoll_setup(struct net_device *dev,
  566. struct netpoll_info *ni)
  567. {
  568. struct dsa_slave_priv *p = netdev_priv(dev);
  569. struct net_device *master = dsa_master_netdev(p);
  570. struct netpoll *netpoll;
  571. int err = 0;
  572. netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
  573. if (!netpoll)
  574. return -ENOMEM;
  575. err = __netpoll_setup(netpoll, master);
  576. if (err) {
  577. kfree(netpoll);
  578. goto out;
  579. }
  580. p->netpoll = netpoll;
  581. out:
  582. return err;
  583. }
  584. static void dsa_slave_netpoll_cleanup(struct net_device *dev)
  585. {
  586. struct dsa_slave_priv *p = netdev_priv(dev);
  587. struct netpoll *netpoll = p->netpoll;
  588. if (!netpoll)
  589. return;
  590. p->netpoll = NULL;
  591. __netpoll_free_async(netpoll);
  592. }
  593. static void dsa_slave_poll_controller(struct net_device *dev)
  594. {
  595. }
  596. #endif
  597. static int dsa_slave_get_phys_port_name(struct net_device *dev,
  598. char *name, size_t len)
  599. {
  600. struct dsa_slave_priv *p = netdev_priv(dev);
  601. if (snprintf(name, len, "p%d", p->dp->index) >= len)
  602. return -EINVAL;
  603. return 0;
  604. }
  605. static struct dsa_mall_tc_entry *
  606. dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
  607. unsigned long cookie)
  608. {
  609. struct dsa_mall_tc_entry *mall_tc_entry;
  610. list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
  611. if (mall_tc_entry->cookie == cookie)
  612. return mall_tc_entry;
  613. return NULL;
  614. }
  615. static int dsa_slave_add_cls_matchall(struct net_device *dev,
  616. __be16 protocol,
  617. struct tc_cls_matchall_offload *cls,
  618. bool ingress)
  619. {
  620. struct dsa_slave_priv *p = netdev_priv(dev);
  621. struct dsa_mall_tc_entry *mall_tc_entry;
  622. struct dsa_switch *ds = p->dp->ds;
  623. struct net *net = dev_net(dev);
  624. struct dsa_slave_priv *to_p;
  625. struct net_device *to_dev;
  626. const struct tc_action *a;
  627. int err = -EOPNOTSUPP;
  628. LIST_HEAD(actions);
  629. int ifindex;
  630. if (!ds->ops->port_mirror_add)
  631. return err;
  632. if (!tc_single_action(cls->exts))
  633. return err;
  634. tcf_exts_to_list(cls->exts, &actions);
  635. a = list_first_entry(&actions, struct tc_action, list);
  636. if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
  637. struct dsa_mall_mirror_tc_entry *mirror;
  638. ifindex = tcf_mirred_ifindex(a);
  639. to_dev = __dev_get_by_index(net, ifindex);
  640. if (!to_dev)
  641. return -EINVAL;
  642. if (!dsa_slave_dev_check(to_dev))
  643. return -EOPNOTSUPP;
  644. mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
  645. if (!mall_tc_entry)
  646. return -ENOMEM;
  647. mall_tc_entry->cookie = cls->cookie;
  648. mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
  649. mirror = &mall_tc_entry->mirror;
  650. to_p = netdev_priv(to_dev);
  651. mirror->to_local_port = to_p->dp->index;
  652. mirror->ingress = ingress;
  653. err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
  654. ingress);
  655. if (err) {
  656. kfree(mall_tc_entry);
  657. return err;
  658. }
  659. list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
  660. }
  661. return 0;
  662. }
  663. static void dsa_slave_del_cls_matchall(struct net_device *dev,
  664. struct tc_cls_matchall_offload *cls)
  665. {
  666. struct dsa_slave_priv *p = netdev_priv(dev);
  667. struct dsa_mall_tc_entry *mall_tc_entry;
  668. struct dsa_switch *ds = p->dp->ds;
  669. if (!ds->ops->port_mirror_del)
  670. return;
  671. mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
  672. if (!mall_tc_entry)
  673. return;
  674. list_del(&mall_tc_entry->list);
  675. switch (mall_tc_entry->type) {
  676. case DSA_PORT_MALL_MIRROR:
  677. ds->ops->port_mirror_del(ds, p->dp->index,
  678. &mall_tc_entry->mirror);
  679. break;
  680. default:
  681. WARN_ON(1);
  682. }
  683. kfree(mall_tc_entry);
  684. }
  685. static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
  686. u32 chain_index, __be16 protocol,
  687. struct tc_to_netdev *tc)
  688. {
  689. bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
  690. if (chain_index)
  691. return -EOPNOTSUPP;
  692. switch (tc->type) {
  693. case TC_SETUP_MATCHALL:
  694. switch (tc->cls_mall->command) {
  695. case TC_CLSMATCHALL_REPLACE:
  696. return dsa_slave_add_cls_matchall(dev, protocol,
  697. tc->cls_mall,
  698. ingress);
  699. case TC_CLSMATCHALL_DESTROY:
  700. dsa_slave_del_cls_matchall(dev, tc->cls_mall);
  701. return 0;
  702. }
  703. default:
  704. return -EOPNOTSUPP;
  705. }
  706. }
  707. static void dsa_slave_get_stats64(struct net_device *dev,
  708. struct rtnl_link_stats64 *stats)
  709. {
  710. struct dsa_slave_priv *p = netdev_priv(dev);
  711. unsigned int start;
  712. netdev_stats_to_stats64(stats, &dev->stats);
  713. do {
  714. start = u64_stats_fetch_begin_irq(&p->stats64.syncp);
  715. stats->tx_packets = p->stats64.tx_packets;
  716. stats->tx_bytes = p->stats64.tx_bytes;
  717. stats->rx_packets = p->stats64.rx_packets;
  718. stats->rx_bytes = p->stats64.rx_bytes;
  719. } while (u64_stats_fetch_retry_irq(&p->stats64.syncp, start));
  720. }
  721. void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
  722. {
  723. ops->get_sset_count = dsa_cpu_port_get_sset_count;
  724. ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
  725. ops->get_strings = dsa_cpu_port_get_strings;
  726. }
  727. static int dsa_slave_get_rxnfc(struct net_device *dev,
  728. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  729. {
  730. struct dsa_slave_priv *p = netdev_priv(dev);
  731. struct dsa_switch *ds = p->dp->ds;
  732. if (!ds->ops->get_rxnfc)
  733. return -EOPNOTSUPP;
  734. return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
  735. }
  736. static int dsa_slave_set_rxnfc(struct net_device *dev,
  737. struct ethtool_rxnfc *nfc)
  738. {
  739. struct dsa_slave_priv *p = netdev_priv(dev);
  740. struct dsa_switch *ds = p->dp->ds;
  741. if (!ds->ops->set_rxnfc)
  742. return -EOPNOTSUPP;
  743. return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
  744. }
  745. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  746. .get_drvinfo = dsa_slave_get_drvinfo,
  747. .get_regs_len = dsa_slave_get_regs_len,
  748. .get_regs = dsa_slave_get_regs,
  749. .nway_reset = dsa_slave_nway_reset,
  750. .get_link = dsa_slave_get_link,
  751. .get_eeprom_len = dsa_slave_get_eeprom_len,
  752. .get_eeprom = dsa_slave_get_eeprom,
  753. .set_eeprom = dsa_slave_set_eeprom,
  754. .get_strings = dsa_slave_get_strings,
  755. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  756. .get_sset_count = dsa_slave_get_sset_count,
  757. .set_wol = dsa_slave_set_wol,
  758. .get_wol = dsa_slave_get_wol,
  759. .set_eee = dsa_slave_set_eee,
  760. .get_eee = dsa_slave_get_eee,
  761. .get_link_ksettings = dsa_slave_get_link_ksettings,
  762. .set_link_ksettings = dsa_slave_set_link_ksettings,
  763. .get_rxnfc = dsa_slave_get_rxnfc,
  764. .set_rxnfc = dsa_slave_set_rxnfc,
  765. };
  766. static const struct net_device_ops dsa_slave_netdev_ops = {
  767. .ndo_open = dsa_slave_open,
  768. .ndo_stop = dsa_slave_close,
  769. .ndo_start_xmit = dsa_slave_xmit,
  770. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  771. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  772. .ndo_set_mac_address = dsa_slave_set_mac_address,
  773. .ndo_fdb_add = switchdev_port_fdb_add,
  774. .ndo_fdb_del = switchdev_port_fdb_del,
  775. .ndo_fdb_dump = switchdev_port_fdb_dump,
  776. .ndo_do_ioctl = dsa_slave_ioctl,
  777. .ndo_get_iflink = dsa_slave_get_iflink,
  778. #ifdef CONFIG_NET_POLL_CONTROLLER
  779. .ndo_netpoll_setup = dsa_slave_netpoll_setup,
  780. .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
  781. .ndo_poll_controller = dsa_slave_poll_controller,
  782. #endif
  783. .ndo_bridge_getlink = switchdev_port_bridge_getlink,
  784. .ndo_bridge_setlink = switchdev_port_bridge_setlink,
  785. .ndo_bridge_dellink = switchdev_port_bridge_dellink,
  786. .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
  787. .ndo_setup_tc = dsa_slave_setup_tc,
  788. .ndo_get_stats64 = dsa_slave_get_stats64,
  789. };
  790. static const struct switchdev_ops dsa_slave_switchdev_ops = {
  791. .switchdev_port_attr_get = dsa_slave_port_attr_get,
  792. .switchdev_port_attr_set = dsa_slave_port_attr_set,
  793. .switchdev_port_obj_add = dsa_slave_port_obj_add,
  794. .switchdev_port_obj_del = dsa_slave_port_obj_del,
  795. .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
  796. };
  797. static struct device_type dsa_type = {
  798. .name = "dsa",
  799. };
  800. static void dsa_slave_adjust_link(struct net_device *dev)
  801. {
  802. struct dsa_slave_priv *p = netdev_priv(dev);
  803. struct dsa_switch *ds = p->dp->ds;
  804. unsigned int status_changed = 0;
  805. if (p->old_link != p->phy->link) {
  806. status_changed = 1;
  807. p->old_link = p->phy->link;
  808. }
  809. if (p->old_duplex != p->phy->duplex) {
  810. status_changed = 1;
  811. p->old_duplex = p->phy->duplex;
  812. }
  813. if (p->old_pause != p->phy->pause) {
  814. status_changed = 1;
  815. p->old_pause = p->phy->pause;
  816. }
  817. if (ds->ops->adjust_link && status_changed)
  818. ds->ops->adjust_link(ds, p->dp->index, p->phy);
  819. if (status_changed)
  820. phy_print_status(p->phy);
  821. }
  822. static int dsa_slave_fixed_link_update(struct net_device *dev,
  823. struct fixed_phy_status *status)
  824. {
  825. struct dsa_slave_priv *p;
  826. struct dsa_switch *ds;
  827. if (dev) {
  828. p = netdev_priv(dev);
  829. ds = p->dp->ds;
  830. if (ds->ops->fixed_link_update)
  831. ds->ops->fixed_link_update(ds, p->dp->index, status);
  832. }
  833. return 0;
  834. }
  835. /* slave device setup *******************************************************/
  836. static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
  837. struct net_device *slave_dev,
  838. int addr)
  839. {
  840. struct dsa_switch *ds = p->dp->ds;
  841. p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
  842. if (!p->phy) {
  843. netdev_err(slave_dev, "no phy at %d\n", addr);
  844. return -ENODEV;
  845. }
  846. /* Use already configured phy mode */
  847. if (p->phy_interface == PHY_INTERFACE_MODE_NA)
  848. p->phy_interface = p->phy->interface;
  849. return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
  850. p->phy_interface);
  851. }
  852. static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
  853. struct net_device *slave_dev)
  854. {
  855. struct dsa_switch *ds = p->dp->ds;
  856. struct device_node *phy_dn, *port_dn;
  857. bool phy_is_fixed = false;
  858. u32 phy_flags = 0;
  859. int mode, ret;
  860. port_dn = p->dp->dn;
  861. mode = of_get_phy_mode(port_dn);
  862. if (mode < 0)
  863. mode = PHY_INTERFACE_MODE_NA;
  864. p->phy_interface = mode;
  865. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  866. if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
  867. /* In the case of a fixed PHY, the DT node associated
  868. * to the fixed PHY is the Port DT node
  869. */
  870. ret = of_phy_register_fixed_link(port_dn);
  871. if (ret) {
  872. netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
  873. return ret;
  874. }
  875. phy_is_fixed = true;
  876. phy_dn = of_node_get(port_dn);
  877. }
  878. if (ds->ops->get_phy_flags)
  879. phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
  880. if (phy_dn) {
  881. int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
  882. /* If this PHY address is part of phys_mii_mask, which means
  883. * that we need to divert reads and writes to/from it, then we
  884. * want to bind this device using the slave MII bus created by
  885. * DSA to make that happen.
  886. */
  887. if (!phy_is_fixed && phy_id >= 0 &&
  888. (ds->phys_mii_mask & (1 << phy_id))) {
  889. ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
  890. if (ret) {
  891. netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
  892. of_node_put(phy_dn);
  893. return ret;
  894. }
  895. } else {
  896. p->phy = of_phy_connect(slave_dev, phy_dn,
  897. dsa_slave_adjust_link,
  898. phy_flags,
  899. p->phy_interface);
  900. }
  901. of_node_put(phy_dn);
  902. }
  903. if (p->phy && phy_is_fixed)
  904. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  905. /* We could not connect to a designated PHY, so use the switch internal
  906. * MDIO bus instead
  907. */
  908. if (!p->phy) {
  909. ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
  910. if (ret) {
  911. netdev_err(slave_dev, "failed to connect to port %d: %d\n",
  912. p->dp->index, ret);
  913. if (phy_is_fixed)
  914. of_phy_deregister_fixed_link(port_dn);
  915. return ret;
  916. }
  917. }
  918. phy_attached_info(p->phy);
  919. return 0;
  920. }
  921. static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
  922. static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
  923. struct netdev_queue *txq,
  924. void *_unused)
  925. {
  926. lockdep_set_class(&txq->_xmit_lock,
  927. &dsa_slave_netdev_xmit_lock_key);
  928. }
  929. int dsa_slave_suspend(struct net_device *slave_dev)
  930. {
  931. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  932. netif_device_detach(slave_dev);
  933. if (p->phy) {
  934. phy_stop(p->phy);
  935. p->old_pause = -1;
  936. p->old_link = -1;
  937. p->old_duplex = -1;
  938. phy_suspend(p->phy);
  939. }
  940. return 0;
  941. }
  942. int dsa_slave_resume(struct net_device *slave_dev)
  943. {
  944. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  945. netif_device_attach(slave_dev);
  946. if (p->phy) {
  947. phy_resume(p->phy);
  948. phy_start(p->phy);
  949. }
  950. return 0;
  951. }
  952. int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
  953. int port, const char *name)
  954. {
  955. struct dsa_switch_tree *dst = ds->dst;
  956. struct net_device *master;
  957. struct net_device *slave_dev;
  958. struct dsa_slave_priv *p;
  959. struct dsa_port *cpu_dp;
  960. int ret;
  961. cpu_dp = ds->dst->cpu_dp;
  962. master = cpu_dp->netdev;
  963. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  964. NET_NAME_UNKNOWN, ether_setup);
  965. if (slave_dev == NULL)
  966. return -ENOMEM;
  967. slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
  968. slave_dev->hw_features |= NETIF_F_HW_TC;
  969. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  970. eth_hw_addr_inherit(slave_dev, master);
  971. slave_dev->priv_flags |= IFF_NO_QUEUE;
  972. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  973. slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
  974. slave_dev->min_mtu = 0;
  975. slave_dev->max_mtu = ETH_MAX_MTU;
  976. SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
  977. netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
  978. NULL);
  979. SET_NETDEV_DEV(slave_dev, parent);
  980. slave_dev->dev.of_node = ds->ports[port].dn;
  981. slave_dev->vlan_features = master->vlan_features;
  982. p = netdev_priv(slave_dev);
  983. u64_stats_init(&p->stats64.syncp);
  984. p->dp = &ds->ports[port];
  985. INIT_LIST_HEAD(&p->mall_tc_list);
  986. p->xmit = dst->tag_ops->xmit;
  987. p->old_pause = -1;
  988. p->old_link = -1;
  989. p->old_duplex = -1;
  990. ds->ports[port].netdev = slave_dev;
  991. ret = register_netdev(slave_dev);
  992. if (ret) {
  993. netdev_err(master, "error %d registering interface %s\n",
  994. ret, slave_dev->name);
  995. ds->ports[port].netdev = NULL;
  996. free_netdev(slave_dev);
  997. return ret;
  998. }
  999. netif_carrier_off(slave_dev);
  1000. ret = dsa_slave_phy_setup(p, slave_dev);
  1001. if (ret) {
  1002. netdev_err(master, "error %d setting up slave phy\n", ret);
  1003. unregister_netdev(slave_dev);
  1004. free_netdev(slave_dev);
  1005. return ret;
  1006. }
  1007. return 0;
  1008. }
  1009. void dsa_slave_destroy(struct net_device *slave_dev)
  1010. {
  1011. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  1012. struct device_node *port_dn;
  1013. port_dn = p->dp->dn;
  1014. netif_carrier_off(slave_dev);
  1015. if (p->phy) {
  1016. phy_disconnect(p->phy);
  1017. if (of_phy_is_fixed_link(port_dn))
  1018. of_phy_deregister_fixed_link(port_dn);
  1019. }
  1020. unregister_netdev(slave_dev);
  1021. free_netdev(slave_dev);
  1022. }
  1023. static bool dsa_slave_dev_check(struct net_device *dev)
  1024. {
  1025. return dev->netdev_ops == &dsa_slave_netdev_ops;
  1026. }
  1027. static int dsa_slave_changeupper(struct net_device *dev,
  1028. struct netdev_notifier_changeupper_info *info)
  1029. {
  1030. struct dsa_slave_priv *p = netdev_priv(dev);
  1031. struct dsa_port *dp = p->dp;
  1032. int err = NOTIFY_DONE;
  1033. if (netif_is_bridge_master(info->upper_dev)) {
  1034. if (info->linking) {
  1035. err = dsa_port_bridge_join(dp, info->upper_dev);
  1036. err = notifier_from_errno(err);
  1037. } else {
  1038. dsa_port_bridge_leave(dp, info->upper_dev);
  1039. err = NOTIFY_OK;
  1040. }
  1041. }
  1042. return err;
  1043. }
  1044. static int dsa_slave_netdevice_event(struct notifier_block *nb,
  1045. unsigned long event, void *ptr)
  1046. {
  1047. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1048. if (dev->netdev_ops != &dsa_slave_netdev_ops)
  1049. return NOTIFY_DONE;
  1050. if (event == NETDEV_CHANGEUPPER)
  1051. return dsa_slave_changeupper(dev, ptr);
  1052. return NOTIFY_DONE;
  1053. }
  1054. static struct notifier_block dsa_slave_nb __read_mostly = {
  1055. .notifier_call = dsa_slave_netdevice_event,
  1056. };
  1057. int dsa_slave_register_notifier(void)
  1058. {
  1059. return register_netdevice_notifier(&dsa_slave_nb);
  1060. }
  1061. void dsa_slave_unregister_notifier(void)
  1062. {
  1063. int err;
  1064. err = unregister_netdevice_notifier(&dsa_slave_nb);
  1065. if (err)
  1066. pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
  1067. }