slave.c 34 KB

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