slave.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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_async(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. call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
  1181. &fdb_info->info);
  1182. break;
  1183. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1184. fdb_info = &switchdev_work->fdb_info;
  1185. if (!fdb_info->added_by_user)
  1186. break;
  1187. err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
  1188. if (err) {
  1189. netdev_dbg(dev, "fdb del failed err=%d\n", err);
  1190. dev_close(dev);
  1191. }
  1192. break;
  1193. }
  1194. rtnl_unlock();
  1195. kfree(switchdev_work->fdb_info.addr);
  1196. kfree(switchdev_work);
  1197. dev_put(dev);
  1198. }
  1199. static int
  1200. dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
  1201. switchdev_work,
  1202. const struct switchdev_notifier_fdb_info *
  1203. fdb_info)
  1204. {
  1205. memcpy(&switchdev_work->fdb_info, fdb_info,
  1206. sizeof(switchdev_work->fdb_info));
  1207. switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
  1208. if (!switchdev_work->fdb_info.addr)
  1209. return -ENOMEM;
  1210. ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
  1211. fdb_info->addr);
  1212. return 0;
  1213. }
  1214. /* Called under rcu_read_lock() */
  1215. static int dsa_slave_switchdev_event(struct notifier_block *unused,
  1216. unsigned long event, void *ptr)
  1217. {
  1218. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1219. struct dsa_switchdev_event_work *switchdev_work;
  1220. if (!dsa_slave_dev_check(dev))
  1221. return NOTIFY_DONE;
  1222. switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
  1223. if (!switchdev_work)
  1224. return NOTIFY_BAD;
  1225. INIT_WORK(&switchdev_work->work,
  1226. dsa_slave_switchdev_event_work);
  1227. switchdev_work->dev = dev;
  1228. switchdev_work->event = event;
  1229. switch (event) {
  1230. case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
  1231. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1232. if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
  1233. goto err_fdb_work_init;
  1234. dev_hold(dev);
  1235. break;
  1236. default:
  1237. kfree(switchdev_work);
  1238. return NOTIFY_DONE;
  1239. }
  1240. dsa_schedule_work(&switchdev_work->work);
  1241. return NOTIFY_OK;
  1242. err_fdb_work_init:
  1243. kfree(switchdev_work);
  1244. return NOTIFY_BAD;
  1245. }
  1246. static struct notifier_block dsa_slave_nb __read_mostly = {
  1247. .notifier_call = dsa_slave_netdevice_event,
  1248. };
  1249. static struct notifier_block dsa_slave_switchdev_notifier = {
  1250. .notifier_call = dsa_slave_switchdev_event,
  1251. };
  1252. int dsa_slave_register_notifier(void)
  1253. {
  1254. int err;
  1255. err = register_netdevice_notifier(&dsa_slave_nb);
  1256. if (err)
  1257. return err;
  1258. err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
  1259. if (err)
  1260. goto err_switchdev_nb;
  1261. return 0;
  1262. err_switchdev_nb:
  1263. unregister_netdevice_notifier(&dsa_slave_nb);
  1264. return err;
  1265. }
  1266. void dsa_slave_unregister_notifier(void)
  1267. {
  1268. int err;
  1269. err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
  1270. if (err)
  1271. pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
  1272. err = unregister_netdevice_notifier(&dsa_slave_nb);
  1273. if (err)
  1274. pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
  1275. }