slave.c 38 KB

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