slave.c 38 KB

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