slave.c 33 KB

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