slave.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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 = TC_H_MAJ(cls->common.handle) == TC_H_MAJ(TC_H_INGRESS);
  739. if (cls->common.chain_index)
  740. return -EOPNOTSUPP;
  741. switch (cls->command) {
  742. case TC_CLSMATCHALL_REPLACE:
  743. return dsa_slave_add_cls_matchall(dev, cls, ingress);
  744. case TC_CLSMATCHALL_DESTROY:
  745. dsa_slave_del_cls_matchall(dev, cls);
  746. return 0;
  747. default:
  748. return -EOPNOTSUPP;
  749. }
  750. }
  751. static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
  752. void *type_data)
  753. {
  754. switch (type) {
  755. case TC_SETUP_CLSMATCHALL:
  756. return dsa_slave_setup_tc_cls_matchall(dev, type_data);
  757. default:
  758. return -EOPNOTSUPP;
  759. }
  760. }
  761. static void dsa_slave_get_stats64(struct net_device *dev,
  762. struct rtnl_link_stats64 *stats)
  763. {
  764. struct dsa_slave_priv *p = netdev_priv(dev);
  765. struct pcpu_sw_netstats *s;
  766. unsigned int start;
  767. int i;
  768. netdev_stats_to_stats64(stats, &dev->stats);
  769. for_each_possible_cpu(i) {
  770. u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
  771. s = per_cpu_ptr(p->stats64, i);
  772. do {
  773. start = u64_stats_fetch_begin_irq(&s->syncp);
  774. tx_packets = s->tx_packets;
  775. tx_bytes = s->tx_bytes;
  776. rx_packets = s->rx_packets;
  777. rx_bytes = s->rx_bytes;
  778. } while (u64_stats_fetch_retry_irq(&s->syncp, start));
  779. stats->tx_packets += tx_packets;
  780. stats->tx_bytes += tx_bytes;
  781. stats->rx_packets += rx_packets;
  782. stats->rx_bytes += rx_bytes;
  783. }
  784. }
  785. void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops)
  786. {
  787. ops->get_sset_count = dsa_cpu_port_get_sset_count;
  788. ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats;
  789. ops->get_strings = dsa_cpu_port_get_strings;
  790. }
  791. static int dsa_slave_get_rxnfc(struct net_device *dev,
  792. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  793. {
  794. struct dsa_slave_priv *p = netdev_priv(dev);
  795. struct dsa_switch *ds = p->dp->ds;
  796. if (!ds->ops->get_rxnfc)
  797. return -EOPNOTSUPP;
  798. return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs);
  799. }
  800. static int dsa_slave_set_rxnfc(struct net_device *dev,
  801. struct ethtool_rxnfc *nfc)
  802. {
  803. struct dsa_slave_priv *p = netdev_priv(dev);
  804. struct dsa_switch *ds = p->dp->ds;
  805. if (!ds->ops->set_rxnfc)
  806. return -EOPNOTSUPP;
  807. return ds->ops->set_rxnfc(ds, p->dp->index, nfc);
  808. }
  809. static const struct ethtool_ops dsa_slave_ethtool_ops = {
  810. .get_drvinfo = dsa_slave_get_drvinfo,
  811. .get_regs_len = dsa_slave_get_regs_len,
  812. .get_regs = dsa_slave_get_regs,
  813. .nway_reset = dsa_slave_nway_reset,
  814. .get_link = dsa_slave_get_link,
  815. .get_eeprom_len = dsa_slave_get_eeprom_len,
  816. .get_eeprom = dsa_slave_get_eeprom,
  817. .set_eeprom = dsa_slave_set_eeprom,
  818. .get_strings = dsa_slave_get_strings,
  819. .get_ethtool_stats = dsa_slave_get_ethtool_stats,
  820. .get_sset_count = dsa_slave_get_sset_count,
  821. .set_wol = dsa_slave_set_wol,
  822. .get_wol = dsa_slave_get_wol,
  823. .set_eee = dsa_slave_set_eee,
  824. .get_eee = dsa_slave_get_eee,
  825. .get_link_ksettings = dsa_slave_get_link_ksettings,
  826. .set_link_ksettings = dsa_slave_set_link_ksettings,
  827. .get_rxnfc = dsa_slave_get_rxnfc,
  828. .set_rxnfc = dsa_slave_set_rxnfc,
  829. };
  830. static const struct net_device_ops dsa_slave_netdev_ops = {
  831. .ndo_open = dsa_slave_open,
  832. .ndo_stop = dsa_slave_close,
  833. .ndo_start_xmit = dsa_slave_xmit,
  834. .ndo_change_rx_flags = dsa_slave_change_rx_flags,
  835. .ndo_set_rx_mode = dsa_slave_set_rx_mode,
  836. .ndo_set_mac_address = dsa_slave_set_mac_address,
  837. .ndo_fdb_add = dsa_legacy_fdb_add,
  838. .ndo_fdb_del = dsa_legacy_fdb_del,
  839. .ndo_fdb_dump = dsa_slave_fdb_dump,
  840. .ndo_do_ioctl = dsa_slave_ioctl,
  841. .ndo_get_iflink = dsa_slave_get_iflink,
  842. #ifdef CONFIG_NET_POLL_CONTROLLER
  843. .ndo_netpoll_setup = dsa_slave_netpoll_setup,
  844. .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
  845. .ndo_poll_controller = dsa_slave_poll_controller,
  846. #endif
  847. .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
  848. .ndo_setup_tc = dsa_slave_setup_tc,
  849. .ndo_get_stats64 = dsa_slave_get_stats64,
  850. };
  851. static const struct switchdev_ops dsa_slave_switchdev_ops = {
  852. .switchdev_port_attr_get = dsa_slave_port_attr_get,
  853. .switchdev_port_attr_set = dsa_slave_port_attr_set,
  854. .switchdev_port_obj_add = dsa_slave_port_obj_add,
  855. .switchdev_port_obj_del = dsa_slave_port_obj_del,
  856. };
  857. static struct device_type dsa_type = {
  858. .name = "dsa",
  859. };
  860. static void dsa_slave_adjust_link(struct net_device *dev)
  861. {
  862. struct dsa_slave_priv *p = netdev_priv(dev);
  863. struct dsa_switch *ds = p->dp->ds;
  864. unsigned int status_changed = 0;
  865. if (p->old_link != p->phy->link) {
  866. status_changed = 1;
  867. p->old_link = p->phy->link;
  868. }
  869. if (p->old_duplex != p->phy->duplex) {
  870. status_changed = 1;
  871. p->old_duplex = p->phy->duplex;
  872. }
  873. if (p->old_pause != p->phy->pause) {
  874. status_changed = 1;
  875. p->old_pause = p->phy->pause;
  876. }
  877. if (ds->ops->adjust_link && status_changed)
  878. ds->ops->adjust_link(ds, p->dp->index, p->phy);
  879. if (status_changed)
  880. phy_print_status(p->phy);
  881. }
  882. static int dsa_slave_fixed_link_update(struct net_device *dev,
  883. struct fixed_phy_status *status)
  884. {
  885. struct dsa_slave_priv *p;
  886. struct dsa_switch *ds;
  887. if (dev) {
  888. p = netdev_priv(dev);
  889. ds = p->dp->ds;
  890. if (ds->ops->fixed_link_update)
  891. ds->ops->fixed_link_update(ds, p->dp->index, status);
  892. }
  893. return 0;
  894. }
  895. /* slave device setup *******************************************************/
  896. static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
  897. struct net_device *slave_dev,
  898. int addr)
  899. {
  900. struct dsa_switch *ds = p->dp->ds;
  901. p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr);
  902. if (!p->phy) {
  903. netdev_err(slave_dev, "no phy at %d\n", addr);
  904. return -ENODEV;
  905. }
  906. /* Use already configured phy mode */
  907. if (p->phy_interface == PHY_INTERFACE_MODE_NA)
  908. p->phy_interface = p->phy->interface;
  909. return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
  910. p->phy_interface);
  911. }
  912. static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
  913. struct net_device *slave_dev)
  914. {
  915. struct dsa_switch *ds = p->dp->ds;
  916. struct device_node *phy_dn, *port_dn;
  917. bool phy_is_fixed = false;
  918. u32 phy_flags = 0;
  919. int mode, ret;
  920. port_dn = p->dp->dn;
  921. mode = of_get_phy_mode(port_dn);
  922. if (mode < 0)
  923. mode = PHY_INTERFACE_MODE_NA;
  924. p->phy_interface = mode;
  925. phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
  926. if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
  927. /* In the case of a fixed PHY, the DT node associated
  928. * to the fixed PHY is the Port DT node
  929. */
  930. ret = of_phy_register_fixed_link(port_dn);
  931. if (ret) {
  932. netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
  933. return ret;
  934. }
  935. phy_is_fixed = true;
  936. phy_dn = of_node_get(port_dn);
  937. }
  938. if (ds->ops->get_phy_flags)
  939. phy_flags = ds->ops->get_phy_flags(ds, p->dp->index);
  940. if (phy_dn) {
  941. int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
  942. /* If this PHY address is part of phys_mii_mask, which means
  943. * that we need to divert reads and writes to/from it, then we
  944. * want to bind this device using the slave MII bus created by
  945. * DSA to make that happen.
  946. */
  947. if (!phy_is_fixed && phy_id >= 0 &&
  948. (ds->phys_mii_mask & (1 << phy_id))) {
  949. ret = dsa_slave_phy_connect(p, slave_dev, phy_id);
  950. if (ret) {
  951. netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret);
  952. of_node_put(phy_dn);
  953. return ret;
  954. }
  955. } else {
  956. p->phy = of_phy_connect(slave_dev, phy_dn,
  957. dsa_slave_adjust_link,
  958. phy_flags,
  959. p->phy_interface);
  960. }
  961. of_node_put(phy_dn);
  962. }
  963. if (p->phy && phy_is_fixed)
  964. fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
  965. /* We could not connect to a designated PHY, so use the switch internal
  966. * MDIO bus instead
  967. */
  968. if (!p->phy) {
  969. ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index);
  970. if (ret) {
  971. netdev_err(slave_dev, "failed to connect to port %d: %d\n",
  972. p->dp->index, ret);
  973. if (phy_is_fixed)
  974. of_phy_deregister_fixed_link(port_dn);
  975. return ret;
  976. }
  977. }
  978. phy_attached_info(p->phy);
  979. return 0;
  980. }
  981. static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
  982. static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
  983. struct netdev_queue *txq,
  984. void *_unused)
  985. {
  986. lockdep_set_class(&txq->_xmit_lock,
  987. &dsa_slave_netdev_xmit_lock_key);
  988. }
  989. int dsa_slave_suspend(struct net_device *slave_dev)
  990. {
  991. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  992. netif_device_detach(slave_dev);
  993. if (p->phy) {
  994. phy_stop(p->phy);
  995. p->old_pause = -1;
  996. p->old_link = -1;
  997. p->old_duplex = -1;
  998. phy_suspend(p->phy);
  999. }
  1000. return 0;
  1001. }
  1002. int dsa_slave_resume(struct net_device *slave_dev)
  1003. {
  1004. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  1005. netif_device_attach(slave_dev);
  1006. if (p->phy) {
  1007. phy_resume(p->phy);
  1008. phy_start(p->phy);
  1009. }
  1010. return 0;
  1011. }
  1012. int dsa_slave_create(struct dsa_port *port, const char *name)
  1013. {
  1014. struct dsa_switch *ds = port->ds;
  1015. struct dsa_switch_tree *dst = ds->dst;
  1016. struct net_device *master;
  1017. struct net_device *slave_dev;
  1018. struct dsa_slave_priv *p;
  1019. struct dsa_port *cpu_dp;
  1020. int ret;
  1021. cpu_dp = ds->dst->cpu_dp;
  1022. master = cpu_dp->netdev;
  1023. slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
  1024. NET_NAME_UNKNOWN, ether_setup);
  1025. if (slave_dev == NULL)
  1026. return -ENOMEM;
  1027. slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
  1028. slave_dev->hw_features |= NETIF_F_HW_TC;
  1029. slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
  1030. eth_hw_addr_inherit(slave_dev, master);
  1031. slave_dev->priv_flags |= IFF_NO_QUEUE;
  1032. slave_dev->netdev_ops = &dsa_slave_netdev_ops;
  1033. slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
  1034. slave_dev->min_mtu = 0;
  1035. slave_dev->max_mtu = ETH_MAX_MTU;
  1036. SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
  1037. netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
  1038. NULL);
  1039. SET_NETDEV_DEV(slave_dev, port->ds->dev);
  1040. slave_dev->dev.of_node = port->dn;
  1041. slave_dev->vlan_features = master->vlan_features;
  1042. p = netdev_priv(slave_dev);
  1043. p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1044. if (!p->stats64) {
  1045. free_netdev(slave_dev);
  1046. return -ENOMEM;
  1047. }
  1048. p->dp = port;
  1049. INIT_LIST_HEAD(&p->mall_tc_list);
  1050. p->xmit = dst->tag_ops->xmit;
  1051. p->old_pause = -1;
  1052. p->old_link = -1;
  1053. p->old_duplex = -1;
  1054. port->netdev = slave_dev;
  1055. ret = register_netdev(slave_dev);
  1056. if (ret) {
  1057. netdev_err(master, "error %d registering interface %s\n",
  1058. ret, slave_dev->name);
  1059. port->netdev = NULL;
  1060. free_percpu(p->stats64);
  1061. free_netdev(slave_dev);
  1062. return ret;
  1063. }
  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. unregister_netdev(slave_dev);
  1069. free_percpu(p->stats64);
  1070. free_netdev(slave_dev);
  1071. return ret;
  1072. }
  1073. return 0;
  1074. }
  1075. void dsa_slave_destroy(struct net_device *slave_dev)
  1076. {
  1077. struct dsa_slave_priv *p = netdev_priv(slave_dev);
  1078. struct device_node *port_dn;
  1079. port_dn = p->dp->dn;
  1080. netif_carrier_off(slave_dev);
  1081. if (p->phy) {
  1082. phy_disconnect(p->phy);
  1083. if (of_phy_is_fixed_link(port_dn))
  1084. of_phy_deregister_fixed_link(port_dn);
  1085. }
  1086. unregister_netdev(slave_dev);
  1087. free_percpu(p->stats64);
  1088. free_netdev(slave_dev);
  1089. }
  1090. static bool dsa_slave_dev_check(struct net_device *dev)
  1091. {
  1092. return dev->netdev_ops == &dsa_slave_netdev_ops;
  1093. }
  1094. static int dsa_slave_changeupper(struct net_device *dev,
  1095. struct netdev_notifier_changeupper_info *info)
  1096. {
  1097. struct dsa_slave_priv *p = netdev_priv(dev);
  1098. struct dsa_port *dp = p->dp;
  1099. int err = NOTIFY_DONE;
  1100. if (netif_is_bridge_master(info->upper_dev)) {
  1101. if (info->linking) {
  1102. err = dsa_port_bridge_join(dp, info->upper_dev);
  1103. err = notifier_from_errno(err);
  1104. } else {
  1105. dsa_port_bridge_leave(dp, info->upper_dev);
  1106. err = NOTIFY_OK;
  1107. }
  1108. }
  1109. return err;
  1110. }
  1111. static int dsa_slave_netdevice_event(struct notifier_block *nb,
  1112. unsigned long event, void *ptr)
  1113. {
  1114. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1115. if (dev->netdev_ops != &dsa_slave_netdev_ops)
  1116. return NOTIFY_DONE;
  1117. if (event == NETDEV_CHANGEUPPER)
  1118. return dsa_slave_changeupper(dev, ptr);
  1119. return NOTIFY_DONE;
  1120. }
  1121. struct dsa_switchdev_event_work {
  1122. struct work_struct work;
  1123. struct switchdev_notifier_fdb_info fdb_info;
  1124. struct net_device *dev;
  1125. unsigned long event;
  1126. };
  1127. static void dsa_slave_switchdev_event_work(struct work_struct *work)
  1128. {
  1129. struct dsa_switchdev_event_work *switchdev_work =
  1130. container_of(work, struct dsa_switchdev_event_work, work);
  1131. struct net_device *dev = switchdev_work->dev;
  1132. struct switchdev_notifier_fdb_info *fdb_info;
  1133. struct dsa_slave_priv *p = netdev_priv(dev);
  1134. int err;
  1135. rtnl_lock();
  1136. switch (switchdev_work->event) {
  1137. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  1138. fdb_info = &switchdev_work->fdb_info;
  1139. err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid);
  1140. if (err) {
  1141. netdev_dbg(dev, "fdb add failed err=%d\n", err);
  1142. break;
  1143. }
  1144. call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
  1145. &fdb_info->info);
  1146. break;
  1147. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1148. fdb_info = &switchdev_work->fdb_info;
  1149. err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid);
  1150. if (err) {
  1151. netdev_dbg(dev, "fdb del failed err=%d\n", err);
  1152. dev_close(dev);
  1153. }
  1154. break;
  1155. }
  1156. rtnl_unlock();
  1157. kfree(switchdev_work->fdb_info.addr);
  1158. kfree(switchdev_work);
  1159. dev_put(dev);
  1160. }
  1161. static int
  1162. dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
  1163. switchdev_work,
  1164. const struct switchdev_notifier_fdb_info *
  1165. fdb_info)
  1166. {
  1167. memcpy(&switchdev_work->fdb_info, fdb_info,
  1168. sizeof(switchdev_work->fdb_info));
  1169. switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
  1170. if (!switchdev_work->fdb_info.addr)
  1171. return -ENOMEM;
  1172. ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
  1173. fdb_info->addr);
  1174. return 0;
  1175. }
  1176. /* Called under rcu_read_lock() */
  1177. static int dsa_slave_switchdev_event(struct notifier_block *unused,
  1178. unsigned long event, void *ptr)
  1179. {
  1180. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1181. struct dsa_switchdev_event_work *switchdev_work;
  1182. if (!dsa_slave_dev_check(dev))
  1183. return NOTIFY_DONE;
  1184. switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
  1185. if (!switchdev_work)
  1186. return NOTIFY_BAD;
  1187. INIT_WORK(&switchdev_work->work,
  1188. dsa_slave_switchdev_event_work);
  1189. switchdev_work->dev = dev;
  1190. switchdev_work->event = event;
  1191. switch (event) {
  1192. case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
  1193. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1194. if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
  1195. ptr))
  1196. goto err_fdb_work_init;
  1197. dev_hold(dev);
  1198. break;
  1199. default:
  1200. kfree(switchdev_work);
  1201. return NOTIFY_DONE;
  1202. }
  1203. dsa_schedule_work(&switchdev_work->work);
  1204. return NOTIFY_OK;
  1205. err_fdb_work_init:
  1206. kfree(switchdev_work);
  1207. return NOTIFY_BAD;
  1208. }
  1209. static struct notifier_block dsa_slave_nb __read_mostly = {
  1210. .notifier_call = dsa_slave_netdevice_event,
  1211. };
  1212. static struct notifier_block dsa_slave_switchdev_notifier = {
  1213. .notifier_call = dsa_slave_switchdev_event,
  1214. };
  1215. int dsa_slave_register_notifier(void)
  1216. {
  1217. int err;
  1218. err = register_netdevice_notifier(&dsa_slave_nb);
  1219. if (err)
  1220. return err;
  1221. err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
  1222. if (err)
  1223. goto err_switchdev_nb;
  1224. return 0;
  1225. err_switchdev_nb:
  1226. unregister_netdevice_notifier(&dsa_slave_nb);
  1227. return err;
  1228. }
  1229. void dsa_slave_unregister_notifier(void)
  1230. {
  1231. int err;
  1232. err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
  1233. if (err)
  1234. pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
  1235. err = unregister_netdevice_notifier(&dsa_slave_nb);
  1236. if (err)
  1237. pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
  1238. }