slave.c 37 KB

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