slave.c 34 KB

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