slave.c 36 KB

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