slave.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. if (!ds->ops->set_eee)
  529. return -EOPNOTSUPP;
  530. ret = ds->ops->set_eee(ds, p->dp->index, p->phy, e);
  531. if (ret)
  532. return ret;
  533. if (p->phy)
  534. ret = phy_ethtool_set_eee(p->phy, e);
  535. return ret;
  536. }
  537. static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
  538. {
  539. struct dsa_slave_priv *p = netdev_priv(dev);
  540. struct dsa_switch *ds = p->dp->ds;
  541. int ret;
  542. if (!ds->ops->get_eee)
  543. return -EOPNOTSUPP;
  544. ret = ds->ops->get_eee(ds, p->dp->index, e);
  545. if (ret)
  546. return ret;
  547. if (p->phy)
  548. ret = phy_ethtool_get_eee(p->phy, e);
  549. return ret;
  550. }
  551. #ifdef CONFIG_NET_POLL_CONTROLLER
  552. static int dsa_slave_netpoll_setup(struct net_device *dev,
  553. struct netpoll_info *ni)
  554. {
  555. struct dsa_slave_priv *p = netdev_priv(dev);
  556. struct net_device *master = dsa_master_netdev(p);
  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. struct dsa_port *cpu_dp;
  932. int ret;
  933. cpu_dp = ds->dst->cpu_dp;
  934. master = cpu_dp->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. }