slave.c 37 KB

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