slave.c 38 KB

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