slave.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. dev->stats.tx_packets++;
  296. dev->stats.tx_bytes += skb->len;
  297. /* Transmit function may have to reallocate the original SKB,
  298. * in which case it must have freed it. Only free it here on error.
  299. */
  300. nskb = p->xmit(skb, dev);
  301. if (!nskb) {
  302. kfree_skb(skb);
  303. return NETDEV_TX_OK;
  304. }
  305. /* SKB for netpoll still need to be mangled with the protocol-specific
  306. * tag to be successfully transmitted
  307. */
  308. if (unlikely(netpoll_tx_running(dev)))
  309. return dsa_netpoll_send_skb(p, nskb);
  310. /* Queue the SKB for transmission on the parent interface, but
  311. * do not modify its EtherType
  312. */
  313. nskb->dev = dsa_master_netdev(p);
  314. dev_queue_xmit(nskb);
  315. return NETDEV_TX_OK;
  316. }
  317. /* ethtool operations *******************************************************/
  318. static int
  319. dsa_slave_get_link_ksettings(struct net_device *dev,
  320. struct ethtool_link_ksettings *cmd)
  321. {
  322. struct dsa_slave_priv *p = netdev_priv(dev);
  323. if (!p->phy)
  324. return -EOPNOTSUPP;
  325. phy_ethtool_ksettings_get(p->phy, cmd);
  326. return 0;
  327. }
  328. static int
  329. dsa_slave_set_link_ksettings(struct net_device *dev,
  330. const struct ethtool_link_ksettings *cmd)
  331. {
  332. struct dsa_slave_priv *p = netdev_priv(dev);
  333. if (p->phy != NULL)
  334. return phy_ethtool_ksettings_set(p->phy, cmd);
  335. return -EOPNOTSUPP;
  336. }
  337. static void dsa_slave_get_drvinfo(struct net_device *dev,
  338. struct ethtool_drvinfo *drvinfo)
  339. {
  340. strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
  341. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  342. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  343. }
  344. static int dsa_slave_get_regs_len(struct net_device *dev)
  345. {
  346. struct dsa_slave_priv *p = netdev_priv(dev);
  347. struct dsa_switch *ds = p->dp->ds;
  348. if (ds->ops->get_regs_len)
  349. return ds->ops->get_regs_len(ds, p->dp->index);
  350. return -EOPNOTSUPP;
  351. }
  352. static void
  353. dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
  354. {
  355. struct dsa_slave_priv *p = netdev_priv(dev);
  356. struct dsa_switch *ds = p->dp->ds;
  357. if (ds->ops->get_regs)
  358. ds->ops->get_regs(ds, p->dp->index, regs, _p);
  359. }
  360. static int dsa_slave_nway_reset(struct net_device *dev)
  361. {
  362. struct dsa_slave_priv *p = netdev_priv(dev);
  363. if (p->phy != NULL)
  364. return genphy_restart_aneg(p->phy);
  365. return -EOPNOTSUPP;
  366. }
  367. static u32 dsa_slave_get_link(struct net_device *dev)
  368. {
  369. struct dsa_slave_priv *p = netdev_priv(dev);
  370. if (p->phy != NULL) {
  371. genphy_update_link(p->phy);
  372. return p->phy->link;
  373. }
  374. return -EOPNOTSUPP;
  375. }
  376. static int dsa_slave_get_eeprom_len(struct net_device *dev)
  377. {
  378. struct dsa_slave_priv *p = netdev_priv(dev);
  379. struct dsa_switch *ds = p->dp->ds;
  380. if (ds->cd && ds->cd->eeprom_len)
  381. return ds->cd->eeprom_len;
  382. if (ds->ops->get_eeprom_len)
  383. return ds->ops->get_eeprom_len(ds);
  384. return 0;
  385. }
  386. static int dsa_slave_get_eeprom(struct net_device *dev,
  387. struct ethtool_eeprom *eeprom, u8 *data)
  388. {
  389. struct dsa_slave_priv *p = netdev_priv(dev);
  390. struct dsa_switch *ds = p->dp->ds;
  391. if (ds->ops->get_eeprom)
  392. return ds->ops->get_eeprom(ds, eeprom, data);
  393. return -EOPNOTSUPP;
  394. }
  395. static int dsa_slave_set_eeprom(struct net_device *dev,
  396. struct ethtool_eeprom *eeprom, u8 *data)
  397. {
  398. struct dsa_slave_priv *p = netdev_priv(dev);
  399. struct dsa_switch *ds = p->dp->ds;
  400. if (ds->ops->set_eeprom)
  401. return ds->ops->set_eeprom(ds, eeprom, data);
  402. return -EOPNOTSUPP;
  403. }
  404. static void dsa_slave_get_strings(struct net_device *dev,
  405. uint32_t stringset, uint8_t *data)
  406. {
  407. struct dsa_slave_priv *p = netdev_priv(dev);
  408. struct dsa_switch *ds = p->dp->ds;
  409. if (stringset == ETH_SS_STATS) {
  410. int len = ETH_GSTRING_LEN;
  411. strncpy(data, "tx_packets", len);
  412. strncpy(data + len, "tx_bytes", len);
  413. strncpy(data + 2 * len, "rx_packets", len);
  414. strncpy(data + 3 * len, "rx_bytes", len);
  415. if (ds->ops->get_strings)
  416. ds->ops->get_strings(ds, p->dp->index, data + 4 * len);
  417. }
  418. }
  419. static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev,
  420. struct ethtool_stats *stats,
  421. uint64_t *data)
  422. {
  423. struct dsa_switch_tree *dst = dev->dsa_ptr;
  424. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  425. struct dsa_switch *ds = cpu_dp->ds;
  426. s8 cpu_port = cpu_dp->index;
  427. int count = 0;
  428. if (cpu_dp->ethtool_ops.get_sset_count) {
  429. count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
  430. cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data);
  431. }
  432. if (ds->ops->get_ethtool_stats)
  433. ds->ops->get_ethtool_stats(ds, cpu_port, data + count);
  434. }
  435. static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset)
  436. {
  437. struct dsa_switch_tree *dst = dev->dsa_ptr;
  438. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  439. struct dsa_switch *ds = cpu_dp->ds;
  440. int count = 0;
  441. if (cpu_dp->ethtool_ops.get_sset_count)
  442. count += cpu_dp->ethtool_ops.get_sset_count(dev, sset);
  443. if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
  444. count += ds->ops->get_sset_count(ds);
  445. return count;
  446. }
  447. static void dsa_cpu_port_get_strings(struct net_device *dev,
  448. uint32_t stringset, uint8_t *data)
  449. {
  450. struct dsa_switch_tree *dst = dev->dsa_ptr;
  451. struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
  452. struct dsa_switch *ds = cpu_dp->ds;
  453. s8 cpu_port = cpu_dp->index;
  454. int len = ETH_GSTRING_LEN;
  455. int mcount = 0, count;
  456. unsigned int i;
  457. uint8_t pfx[4];
  458. uint8_t *ndata;
  459. snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port);
  460. /* We do not want to be NULL-terminated, since this is a prefix */
  461. pfx[sizeof(pfx) - 1] = '_';
  462. if (cpu_dp->ethtool_ops.get_sset_count) {
  463. mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS);
  464. cpu_dp->ethtool_ops.get_strings(dev, stringset, data);
  465. }
  466. if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
  467. ndata = data + mcount * len;
  468. /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
  469. * the output after to prepend our CPU port prefix we
  470. * constructed earlier
  471. */
  472. ds->ops->get_strings(ds, cpu_port, ndata);
  473. count = ds->ops->get_sset_count(ds);
  474. for (i = 0; i < count; i++) {
  475. memmove(ndata + (i * len + sizeof(pfx)),
  476. ndata + i * len, len - sizeof(pfx));
  477. memcpy(ndata + i * len, pfx, sizeof(pfx));
  478. }
  479. }
  480. }
  481. static void dsa_slave_get_ethtool_stats(struct net_device *dev,
  482. struct ethtool_stats *stats,
  483. uint64_t *data)
  484. {
  485. struct dsa_slave_priv *p = netdev_priv(dev);
  486. struct dsa_switch *ds = p->dp->ds;
  487. data[0] = dev->stats.tx_packets;
  488. data[1] = dev->stats.tx_bytes;
  489. data[2] = dev->stats.rx_packets;
  490. data[3] = dev->stats.rx_bytes;
  491. if (ds->ops->get_ethtool_stats)
  492. ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4);
  493. }
  494. static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
  495. {
  496. struct dsa_slave_priv *p = netdev_priv(dev);
  497. struct dsa_switch *ds = p->dp->ds;
  498. if (sset == ETH_SS_STATS) {
  499. int count;
  500. count = 4;
  501. if (ds->ops->get_sset_count)
  502. count += ds->ops->get_sset_count(ds);
  503. return count;
  504. }
  505. return -EOPNOTSUPP;
  506. }
  507. static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  508. {
  509. struct dsa_slave_priv *p = netdev_priv(dev);
  510. struct dsa_switch *ds = p->dp->ds;
  511. if (ds->ops->get_wol)
  512. ds->ops->get_wol(ds, p->dp->index, w);
  513. }
  514. static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
  515. {
  516. struct dsa_slave_priv *p = netdev_priv(dev);
  517. struct dsa_switch *ds = p->dp->ds;
  518. int ret = -EOPNOTSUPP;
  519. if (ds->ops->set_wol)
  520. ret = ds->ops->set_wol(ds, p->dp->index, w);
  521. return ret;
  522. }
  523. static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
  524. {
  525. struct dsa_slave_priv *p = netdev_priv(dev);
  526. struct dsa_switch *ds = p->dp->ds;
  527. int ret;
  528. /* Port's PHY and MAC both need to be EEE capable */
  529. if (!p->phy)
  530. return -ENODEV;
  531. if (!ds->ops->set_eee)
  532. return -EOPNOTSUPP;
  533. ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
  534. if (ret)
  535. return ret;
  536. return phy_ethtool_set_eee(p->phy, e);
  537. }
  538. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  539. {
  540. struct dsa_slave_priv *p = netdev_priv(dev);
  541. struct dsa_switch *ds = p->dp->ds;
  542. int ret;
  543. /* Port's PHY and MAC both need to be EEE capable */
  544. if (!p->phy)
  545. return -ENODEV;
  546. if (!ds->ops->get_eee)
  547. return -EOPNOTSUPP;
  548. ret = ds->ops->get_eee(ds, p->dp->index, e);
  549. if (ret)
  550. return ret;
  551. return phy_ethtool_get_eee(p->phy, e);
  552. }
  553. #ifdef CONFIG_NET_POLL_CONTROLLER
  554. static int dsa_slave_netpoll_setup(struct net_device *dev,
  555. struct netpoll_info *ni)
  556. {
  557. struct dsa_slave_priv *p = netdev_priv(dev);
  558. struct net_device *master = dsa_master_netdev(p);
  559. struct netpoll *netpoll;
  560. int err = 0;
  561. netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
  562. if (!netpoll)
  563. return -ENOMEM;
  564. err = __netpoll_setup(netpoll, master);
  565. if (err) {
  566. kfree(netpoll);
  567. goto out;
  568. }
  569. p->netpoll = netpoll;
  570. out:
  571. return err;
  572. }
  573. static void dsa_slave_netpoll_cleanup(struct net_device *dev)
  574. {
  575. struct dsa_slave_priv *p = netdev_priv(dev);
  576. struct netpoll *netpoll = p->netpoll;
  577. if (!netpoll)
  578. return;
  579. p->netpoll = NULL;
  580. __netpoll_free_async(netpoll);
  581. }
  582. static void dsa_slave_poll_controller(struct net_device *dev)
  583. {
  584. }
  585. #endif
  586. static int dsa_slave_get_phys_port_name(struct net_device *dev,
  587. char *name, size_t len)
  588. {
  589. struct dsa_slave_priv *p = netdev_priv(dev);
  590. if (snprintf(name, len, "p%d", p->dp->index) >= len)
  591. return -EINVAL;
  592. return 0;
  593. }
  594. static struct dsa_mall_tc_entry *
  595. dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p,
  596. unsigned long cookie)
  597. {
  598. struct dsa_mall_tc_entry *mall_tc_entry;
  599. list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
  600. if (mall_tc_entry->cookie == cookie)
  601. return mall_tc_entry;
  602. return NULL;
  603. }
  604. static int dsa_slave_add_cls_matchall(struct net_device *dev,
  605. __be16 protocol,
  606. struct tc_cls_matchall_offload *cls,
  607. bool ingress)
  608. {
  609. struct dsa_slave_priv *p = netdev_priv(dev);
  610. struct dsa_mall_tc_entry *mall_tc_entry;
  611. struct dsa_switch *ds = p->dp->ds;
  612. struct net *net = dev_net(dev);
  613. struct dsa_slave_priv *to_p;
  614. struct net_device *to_dev;
  615. const struct tc_action *a;
  616. int err = -EOPNOTSUPP;
  617. LIST_HEAD(actions);
  618. int ifindex;
  619. if (!ds->ops->port_mirror_add)
  620. return err;
  621. if (!tc_single_action(cls->exts))
  622. return err;
  623. tcf_exts_to_list(cls->exts, &actions);
  624. a = list_first_entry(&actions, struct tc_action, list);
  625. if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
  626. struct dsa_mall_mirror_tc_entry *mirror;
  627. ifindex = tcf_mirred_ifindex(a);
  628. to_dev = __dev_get_by_index(net, ifindex);
  629. if (!to_dev)
  630. return -EINVAL;
  631. if (!dsa_slave_dev_check(to_dev))
  632. return -EOPNOTSUPP;
  633. mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
  634. if (!mall_tc_entry)
  635. return -ENOMEM;
  636. mall_tc_entry->cookie = cls->cookie;
  637. mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
  638. mirror = &mall_tc_entry->mirror;
  639. to_p = netdev_priv(to_dev);
  640. mirror->to_local_port = to_p->dp->index;
  641. mirror->ingress = ingress;
  642. err = ds->ops->port_mirror_add(ds, p->dp->index, mirror,
  643. ingress);
  644. if (err) {
  645. kfree(mall_tc_entry);
  646. return err;
  647. }
  648. list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
  649. }
  650. return 0;
  651. }
  652. static void dsa_slave_del_cls_matchall(struct net_device *dev,
  653. struct tc_cls_matchall_offload *cls)
  654. {
  655. struct dsa_slave_priv *p = netdev_priv(dev);
  656. struct dsa_mall_tc_entry *mall_tc_entry;
  657. struct dsa_switch *ds = p->dp->ds;
  658. if (!ds->ops->port_mirror_del)
  659. return;
  660. mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie);
  661. if (!mall_tc_entry)
  662. return;
  663. list_del(&mall_tc_entry->list);
  664. switch (mall_tc_entry->type) {
  665. case DSA_PORT_MALL_MIRROR:
  666. ds->ops->port_mirror_del(ds, p->dp->index,
  667. &mall_tc_entry->mirror);
  668. break;
  669. default:
  670. WARN_ON(1);
  671. }
  672. kfree(mall_tc_entry);
  673. }
  674. static int dsa_slave_setup_tc(struct net_device *dev, u32 handle,
  675. u32 chain_index, __be16 protocol,
  676. struct tc_to_netdev *tc)
  677. {
  678. bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
  679. if (chain_index)
  680. return -EOPNOTSUPP;
  681. switch (tc->type) {
  682. case TC_SETUP_MATCHALL:
  683. switch (tc->cls_mall->command) {
  684. case TC_CLSMATCHALL_REPLACE:
  685. return dsa_slave_add_cls_matchall(dev, protocol,
  686. tc->cls_mall,
  687. ingress);
  688. case TC_CLSMATCHALL_DESTROY:
  689. dsa_slave_del_cls_matchall(dev, tc->cls_mall);
  690. return 0;
  691. }
  692. default:
  693. return -EOPNOTSUPP;
  694. }
  695. }
  696. void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
  697. {
  698. ops->get_sset_count = dsa_cpu_port_get_sset_count;
  699. ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
  700. ops->get_strings = dsa_cpu_port_get_strings;
  701. }
  702. static int dsa_slave_get_rxnfc(struct net_device *dev,
  703. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  704. {
  705. struct dsa_slave_priv *p = netdev_priv(dev);
  706. struct dsa_switch *ds = p->dp->ds;
  707. if (!ds->ops->get_rxnfc)
  708. return -EOPNOTSUPP;
  709. return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
  710. }
  711. static int dsa_slave_set_rxnfc(struct net_device *dev,
  712. struct ethtool_rxnfc *nfc)
  713. {
  714. struct dsa_slave_priv *p = netdev_priv(dev);
  715. struct dsa_switch *ds = p->dp->ds;
  716. if (!ds->ops->set_rxnfc)
  717. return -EOPNOTSUPP;
  718. return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
  719. }
  720. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  721. .get_drvinfo = dsa_slave_get_drvinfo,
  722. .get_regs_len = dsa_slave_get_regs_len,
  723. .get_regs = dsa_slave_get_regs,
  724. .nway_reset = dsa_slave_nway_reset,
  725. .get_link = dsa_slave_get_link,
  726. .get_eeprom_len = dsa_slave_get_eeprom_len,
  727. .get_eeprom = dsa_slave_get_eeprom,
  728. .set_eeprom = dsa_slave_set_eeprom,
  729. .get_strings = dsa_slave_get_strings,
  730. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  731. .get_sset_count = dsa_slave_get_sset_count,
  732. .set_wol = dsa_slave_set_wol,
  733. .get_wol = dsa_slave_get_wol,
  734. .set_eee = dsa_slave_set_eee,
  735. .get_eee = dsa_slave_get_eee,
  736. .get_link_ksettings = dsa_slave_get_link_ksettings,
  737. .set_link_ksettings = dsa_slave_set_link_ksettings,
  738. .get_rxnfc = dsa_slave_get_rxnfc,
  739. .set_rxnfc = dsa_slave_set_rxnfc,
  740. };
  741. static const struct net_device_ops dsa_slave_netdev_ops = {
  742. .ndo_open = dsa_slave_open,
  743. .ndo_stop = dsa_slave_close,
  744. .ndo_start_xmit = dsa_slave_xmit,
  745. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  746. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  747. .ndo_set_mac_address = dsa_slave_set_mac_address,
  748. .ndo_fdb_add = switchdev_port_fdb_add,
  749. .ndo_fdb_del = switchdev_port_fdb_del,
  750. .ndo_fdb_dump = switchdev_port_fdb_dump,
  751. .ndo_do_ioctl = dsa_slave_ioctl,
  752. .ndo_get_iflink = dsa_slave_get_iflink,
  753. #ifdef CONFIG_NET_POLL_CONTROLLER
  754. .ndo_netpoll_setup = dsa_slave_netpoll_setup,
  755. .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
  756. .ndo_poll_controller = dsa_slave_poll_controller,
  757. #endif
  758. .ndo_bridge_getlink = switchdev_port_bridge_getlink,
  759. .ndo_bridge_setlink = switchdev_port_bridge_setlink,
  760. .ndo_bridge_dellink = switchdev_port_bridge_dellink,
  761. .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
  762. .ndo_setup_tc = dsa_slave_setup_tc,
  763. };
  764. static const struct switchdev_ops dsa_slave_switchdev_ops = {
  765. .switchdev_port_attr_get = dsa_slave_port_attr_get,
  766. .switchdev_port_attr_set = dsa_slave_port_attr_set,
  767. .switchdev_port_obj_add = dsa_slave_port_obj_add,
  768. .switchdev_port_obj_del = dsa_slave_port_obj_del,
  769. .switchdev_port_obj_dump = dsa_slave_port_obj_dump,
  770. };
  771. static struct device_type dsa_type = {
  772. .name = "dsa",
  773. };
  774. static void dsa_slave_adjust_link(struct net_device *dev)
  775. {
  776. struct dsa_slave_priv *p = netdev_priv(dev);
  777. struct dsa_switch *ds = p->dp->ds;
  778. unsigned int status_changed = 0;
  779. if (p->old_link != p->phy->link) {
  780. status_changed = 1;
  781. p->old_link = p->phy->link;
  782. }
  783. if (p->old_duplex != p->phy->duplex) {
  784. status_changed = 1;
  785. p->old_duplex = p->phy->duplex;
  786. }
  787. if (p->old_pause != p->phy->pause) {
  788. status_changed = 1;
  789. p->old_pause = p->phy->pause;
  790. }
  791. if (ds->ops->adjust_link && status_changed)
  792. ds->ops->adjust_link(ds, p->dp->index, p->phy);
  793. if (status_changed)
  794. phy_print_status(p->phy);
  795. }
  796. static int dsa_slave_fixed_link_update(struct net_device *dev,
  797. struct fixed_phy_status *status)
  798. {
  799. struct dsa_slave_priv *p;
  800. struct dsa_switch *ds;
  801. if (dev) {
  802. p = netdev_priv(dev);
  803. ds = p->dp->ds;
  804. if (ds->ops->fixed_link_update)
  805. ds->ops->fixed_link_update(ds, p->dp->index, status);
  806. }
  807. return 0;
  808. }
  809. /* slave device setup *******************************************************/
  810. static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
  811. struct net_device *slave_dev,
  812. int addr)
  813. {
  814. struct dsa_switch *ds = p->dp->ds;
  815. p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
  816. if (!p->phy) {
  817. netdev_err(slave_dev, "no phy at %d\n", addr);
  818. return -ENODEV;
  819. }
  820. /* Use already configured phy mode */
  821. if (p->phy_interface == PHY_INTERFACE_MODE_NA)
  822. p->phy_interface = p->phy->interface;
  823. return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
  824. p->phy_interface);
  825. }
  826. static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
  827. struct net_device *slave_dev)
  828. {
  829. struct dsa_switch *ds = p->dp->ds;
  830. struct device_node *phy_dn, *port_dn;
  831. bool phy_is_fixed = false;
  832. u32 phy_flags = 0;
  833. int mode, ret;
  834. port_dn = p->dp->dn;
  835. mode = of_get_phy_mode(port_dn);
  836. if (mode < 0)
  837. mode = PHY_INTERFACE_MODE_NA;
  838. p->phy_interface = mode;
  839. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  840. if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
  841. /* In the case of a fixed PHY, the DT node associated
  842. * to the fixed PHY is the Port DT node
  843. */
  844. ret = of_phy_register_fixed_link(port_dn);
  845. if (ret) {
  846. netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
  847. return ret;
  848. }
  849. phy_is_fixed = true;
  850. phy_dn = of_node_get(port_dn);
  851. }
  852. if (ds->ops->get_phy_flags)
  853. phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
  854. if (phy_dn) {
  855. int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
  856. /* If this PHY address is part of phys_mii_mask, which means
  857. * that we need to divert reads and writes to/from it, then we
  858. * want to bind this device using the slave MII bus created by
  859. * DSA to make that happen.
  860. */
  861. if (!phy_is_fixed && phy_id >= 0 &&
  862. (ds->phys_mii_mask & (1 << phy_id))) {
  863. ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
  864. if (ret) {
  865. netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
  866. of_node_put(phy_dn);
  867. return ret;
  868. }
  869. } else {
  870. p->phy = of_phy_connect(slave_dev, phy_dn,
  871. dsa_slave_adjust_link,
  872. phy_flags,
  873. p->phy_interface);
  874. }
  875. of_node_put(phy_dn);
  876. }
  877. if (p->phy && phy_is_fixed)
  878. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  879. /* We could not connect to a designated PHY, so use the switch internal
  880. * MDIO bus instead
  881. */
  882. if (!p->phy) {
  883. ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
  884. if (ret) {
  885. netdev_err(slave_dev, "failed to connect to port %d: %d\n",
  886. p->dp->index, ret);
  887. if (phy_is_fixed)
  888. of_phy_deregister_fixed_link(port_dn);
  889. return ret;
  890. }
  891. }
  892. phy_attached_info(p->phy);
  893. return 0;
  894. }
  895. static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
  896. static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
  897. struct netdev_queue *txq,
  898. void *_unused)
  899. {
  900. lockdep_set_class(&txq->_xmit_lock,
  901. &dsa_slave_netdev_xmit_lock_key);
  902. }
  903. int dsa_slave_suspend(struct net_device *slave_dev)
  904. {
  905. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  906. netif_device_detach(slave_dev);
  907. if (p->phy) {
  908. phy_stop(p->phy);
  909. p->old_pause = -1;
  910. p->old_link = -1;
  911. p->old_duplex = -1;
  912. phy_suspend(p->phy);
  913. }
  914. return 0;
  915. }
  916. int dsa_slave_resume(struct net_device *slave_dev)
  917. {
  918. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  919. netif_device_attach(slave_dev);
  920. if (p->phy) {
  921. phy_resume(p->phy);
  922. phy_start(p->phy);
  923. }
  924. return 0;
  925. }
  926. int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
  927. int port, const char *name)
  928. {
  929. struct dsa_switch_tree *dst = ds->dst;
  930. struct net_device *master;
  931. struct net_device *slave_dev;
  932. struct dsa_slave_priv *p;
  933. struct dsa_port *cpu_dp;
  934. int ret;
  935. cpu_dp = ds->dst->cpu_dp;
  936. master = cpu_dp->netdev;
  937. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  938. NET_NAME_UNKNOWN, ether_setup);
  939. if (slave_dev == NULL)
  940. return -ENOMEM;
  941. slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
  942. slave_dev->hw_features |= NETIF_F_HW_TC;
  943. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  944. eth_hw_addr_inherit(slave_dev, master);
  945. slave_dev->priv_flags |= IFF_NO_QUEUE;
  946. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  947. slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
  948. slave_dev->min_mtu = 0;
  949. slave_dev->max_mtu = ETH_MAX_MTU;
  950. SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
  951. netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
  952. NULL);
  953. SET_NETDEV_DEV(slave_dev, parent);
  954. slave_dev->dev.of_node = ds->ports[port].dn;
  955. slave_dev->vlan_features = master->vlan_features;
  956. p = netdev_priv(slave_dev);
  957. p->dp = &ds->ports[port];
  958. INIT_LIST_HEAD(&p->mall_tc_list);
  959. p->xmit = dst->tag_ops->xmit;
  960. p->old_pause = -1;
  961. p->old_link = -1;
  962. p->old_duplex = -1;
  963. ds->ports[port].netdev = slave_dev;
  964. ret = register_netdev(slave_dev);
  965. if (ret) {
  966. netdev_err(master, "error %d registering interface %s\n",
  967. ret, slave_dev->name);
  968. ds->ports[port].netdev = NULL;
  969. free_netdev(slave_dev);
  970. return ret;
  971. }
  972. netif_carrier_off(slave_dev);
  973. ret = dsa_slave_phy_setup(p, slave_dev);
  974. if (ret) {
  975. netdev_err(master, "error %d setting up slave phy\n", ret);
  976. unregister_netdev(slave_dev);
  977. free_netdev(slave_dev);
  978. return ret;
  979. }
  980. return 0;
  981. }
  982. void dsa_slave_destroy(struct net_device *slave_dev)
  983. {
  984. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  985. struct device_node *port_dn;
  986. port_dn = p->dp->dn;
  987. netif_carrier_off(slave_dev);
  988. if (p->phy) {
  989. phy_disconnect(p->phy);
  990. if (of_phy_is_fixed_link(port_dn))
  991. of_phy_deregister_fixed_link(port_dn);
  992. }
  993. unregister_netdev(slave_dev);
  994. free_netdev(slave_dev);
  995. }
  996. static bool dsa_slave_dev_check(struct net_device *dev)
  997. {
  998. return dev->netdev_ops == &dsa_slave_netdev_ops;
  999. }
  1000. static int dsa_slave_changeupper(struct net_device *dev,
  1001. struct netdev_notifier_changeupper_info *info)
  1002. {
  1003. struct dsa_slave_priv *p = netdev_priv(dev);
  1004. struct dsa_port *dp = p->dp;
  1005. int err = NOTIFY_DONE;
  1006. if (netif_is_bridge_master(info->upper_dev)) {
  1007. if (info->linking) {
  1008. err = dsa_port_bridge_join(dp, info->upper_dev);
  1009. err = notifier_from_errno(err);
  1010. } else {
  1011. dsa_port_bridge_leave(dp, info->upper_dev);
  1012. err = NOTIFY_OK;
  1013. }
  1014. }
  1015. return err;
  1016. }
  1017. static int dsa_slave_netdevice_event(struct notifier_block *nb,
  1018. unsigned long event, void *ptr)
  1019. {
  1020. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1021. if (dev->netdev_ops != &dsa_slave_netdev_ops)
  1022. return NOTIFY_DONE;
  1023. if (event == NETDEV_CHANGEUPPER)
  1024. return dsa_slave_changeupper(dev, ptr);
  1025. return NOTIFY_DONE;
  1026. }
  1027. static struct notifier_block dsa_slave_nb __read_mostly = {
  1028. .notifier_call = dsa_slave_netdevice_event,
  1029. };
  1030. int dsa_slave_register_notifier(void)
  1031. {
  1032. return register_netdevice_notifier(&dsa_slave_nb);
  1033. }
  1034. void dsa_slave_unregister_notifier(void)
  1035. {
  1036. int err;
  1037. err = unregister_netdevice_notifier(&dsa_slave_nb);
  1038. if (err)
  1039. pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
  1040. }