slave.c 31 KB

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