slave.c 36 KB

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