slave.c 38 KB

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