slave.c 38 KB

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