slave.c 34 KB

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