slave.c 33 KB

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