slave.c 35 KB

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