slave.c 37 KB

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