mv88e6xxx.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*
  2. * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
  3. * Copyright (c) 2008 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/delay.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/if_bridge.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/phy.h>
  18. #include <net/dsa.h>
  19. #include "mv88e6xxx.h"
  20. /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
  21. * use all 32 SMI bus addresses on its SMI bus, and all switch registers
  22. * will be directly accessible on some {device address,register address}
  23. * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
  24. * will only respond to SMI transactions to that specific address, and
  25. * an indirect addressing mechanism needs to be used to access its
  26. * registers.
  27. */
  28. static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
  29. {
  30. int ret;
  31. int i;
  32. for (i = 0; i < 16; i++) {
  33. ret = mdiobus_read(bus, sw_addr, SMI_CMD);
  34. if (ret < 0)
  35. return ret;
  36. if ((ret & SMI_CMD_BUSY) == 0)
  37. return 0;
  38. }
  39. return -ETIMEDOUT;
  40. }
  41. int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
  42. {
  43. int ret;
  44. if (sw_addr == 0)
  45. return mdiobus_read(bus, addr, reg);
  46. /* Wait for the bus to become free. */
  47. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  48. if (ret < 0)
  49. return ret;
  50. /* Transmit the read command. */
  51. ret = mdiobus_write(bus, sw_addr, SMI_CMD,
  52. SMI_CMD_OP_22_READ | (addr << 5) | reg);
  53. if (ret < 0)
  54. return ret;
  55. /* Wait for the read command to complete. */
  56. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  57. if (ret < 0)
  58. return ret;
  59. /* Read the data. */
  60. ret = mdiobus_read(bus, sw_addr, SMI_DATA);
  61. if (ret < 0)
  62. return ret;
  63. return ret & 0xffff;
  64. }
  65. /* Must be called with SMI mutex held */
  66. static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
  67. {
  68. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  69. int ret;
  70. if (bus == NULL)
  71. return -EINVAL;
  72. ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
  73. if (ret < 0)
  74. return ret;
  75. dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
  76. addr, reg, ret);
  77. return ret;
  78. }
  79. int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
  80. {
  81. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  82. int ret;
  83. mutex_lock(&ps->smi_mutex);
  84. ret = _mv88e6xxx_reg_read(ds, addr, reg);
  85. mutex_unlock(&ps->smi_mutex);
  86. return ret;
  87. }
  88. int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
  89. int reg, u16 val)
  90. {
  91. int ret;
  92. if (sw_addr == 0)
  93. return mdiobus_write(bus, addr, reg, val);
  94. /* Wait for the bus to become free. */
  95. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  96. if (ret < 0)
  97. return ret;
  98. /* Transmit the data to write. */
  99. ret = mdiobus_write(bus, sw_addr, SMI_DATA, val);
  100. if (ret < 0)
  101. return ret;
  102. /* Transmit the write command. */
  103. ret = mdiobus_write(bus, sw_addr, SMI_CMD,
  104. SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
  105. if (ret < 0)
  106. return ret;
  107. /* Wait for the write command to complete. */
  108. ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
  109. if (ret < 0)
  110. return ret;
  111. return 0;
  112. }
  113. /* Must be called with SMI mutex held */
  114. static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
  115. u16 val)
  116. {
  117. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  118. if (bus == NULL)
  119. return -EINVAL;
  120. dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
  121. addr, reg, val);
  122. return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
  123. }
  124. int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  125. {
  126. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  127. int ret;
  128. mutex_lock(&ps->smi_mutex);
  129. ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
  130. mutex_unlock(&ps->smi_mutex);
  131. return ret;
  132. }
  133. int mv88e6xxx_config_prio(struct dsa_switch *ds)
  134. {
  135. /* Configure the IP ToS mapping registers. */
  136. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
  137. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
  138. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
  139. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
  140. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
  141. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
  142. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
  143. REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
  144. /* Configure the IEEE 802.1p priority mapping register. */
  145. REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
  146. return 0;
  147. }
  148. int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
  149. {
  150. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
  151. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
  152. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
  153. return 0;
  154. }
  155. int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
  156. {
  157. int i;
  158. int ret;
  159. for (i = 0; i < 6; i++) {
  160. int j;
  161. /* Write the MAC address byte. */
  162. REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
  163. GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
  164. /* Wait for the write to complete. */
  165. for (j = 0; j < 16; j++) {
  166. ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
  167. if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
  168. break;
  169. }
  170. if (j == 16)
  171. return -ETIMEDOUT;
  172. }
  173. return 0;
  174. }
  175. /* Must be called with phy mutex held */
  176. static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
  177. {
  178. if (addr >= 0)
  179. return mv88e6xxx_reg_read(ds, addr, regnum);
  180. return 0xffff;
  181. }
  182. /* Must be called with phy mutex held */
  183. static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
  184. u16 val)
  185. {
  186. if (addr >= 0)
  187. return mv88e6xxx_reg_write(ds, addr, regnum, val);
  188. return 0;
  189. }
  190. #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
  191. static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
  192. {
  193. int ret;
  194. unsigned long timeout;
  195. ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
  196. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
  197. ret & ~GLOBAL_CONTROL_PPU_ENABLE);
  198. timeout = jiffies + 1 * HZ;
  199. while (time_before(jiffies, timeout)) {
  200. ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
  201. usleep_range(1000, 2000);
  202. if ((ret & GLOBAL_STATUS_PPU_MASK) !=
  203. GLOBAL_STATUS_PPU_POLLING)
  204. return 0;
  205. }
  206. return -ETIMEDOUT;
  207. }
  208. static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
  209. {
  210. int ret;
  211. unsigned long timeout;
  212. ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
  213. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
  214. timeout = jiffies + 1 * HZ;
  215. while (time_before(jiffies, timeout)) {
  216. ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
  217. usleep_range(1000, 2000);
  218. if ((ret & GLOBAL_STATUS_PPU_MASK) ==
  219. GLOBAL_STATUS_PPU_POLLING)
  220. return 0;
  221. }
  222. return -ETIMEDOUT;
  223. }
  224. static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
  225. {
  226. struct mv88e6xxx_priv_state *ps;
  227. ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
  228. if (mutex_trylock(&ps->ppu_mutex)) {
  229. struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
  230. if (mv88e6xxx_ppu_enable(ds) == 0)
  231. ps->ppu_disabled = 0;
  232. mutex_unlock(&ps->ppu_mutex);
  233. }
  234. }
  235. static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
  236. {
  237. struct mv88e6xxx_priv_state *ps = (void *)_ps;
  238. schedule_work(&ps->ppu_work);
  239. }
  240. static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
  241. {
  242. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  243. int ret;
  244. mutex_lock(&ps->ppu_mutex);
  245. /* If the PHY polling unit is enabled, disable it so that
  246. * we can access the PHY registers. If it was already
  247. * disabled, cancel the timer that is going to re-enable
  248. * it.
  249. */
  250. if (!ps->ppu_disabled) {
  251. ret = mv88e6xxx_ppu_disable(ds);
  252. if (ret < 0) {
  253. mutex_unlock(&ps->ppu_mutex);
  254. return ret;
  255. }
  256. ps->ppu_disabled = 1;
  257. } else {
  258. del_timer(&ps->ppu_timer);
  259. ret = 0;
  260. }
  261. return ret;
  262. }
  263. static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
  264. {
  265. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  266. /* Schedule a timer to re-enable the PHY polling unit. */
  267. mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
  268. mutex_unlock(&ps->ppu_mutex);
  269. }
  270. void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
  271. {
  272. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  273. mutex_init(&ps->ppu_mutex);
  274. INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
  275. init_timer(&ps->ppu_timer);
  276. ps->ppu_timer.data = (unsigned long)ps;
  277. ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
  278. }
  279. int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
  280. {
  281. int ret;
  282. ret = mv88e6xxx_ppu_access_get(ds);
  283. if (ret >= 0) {
  284. ret = mv88e6xxx_reg_read(ds, addr, regnum);
  285. mv88e6xxx_ppu_access_put(ds);
  286. }
  287. return ret;
  288. }
  289. int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
  290. int regnum, u16 val)
  291. {
  292. int ret;
  293. ret = mv88e6xxx_ppu_access_get(ds);
  294. if (ret >= 0) {
  295. ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
  296. mv88e6xxx_ppu_access_put(ds);
  297. }
  298. return ret;
  299. }
  300. #endif
  301. void mv88e6xxx_poll_link(struct dsa_switch *ds)
  302. {
  303. int i;
  304. for (i = 0; i < DSA_MAX_PORTS; i++) {
  305. struct net_device *dev;
  306. int uninitialized_var(port_status);
  307. int link;
  308. int speed;
  309. int duplex;
  310. int fc;
  311. dev = ds->ports[i];
  312. if (dev == NULL)
  313. continue;
  314. link = 0;
  315. if (dev->flags & IFF_UP) {
  316. port_status = mv88e6xxx_reg_read(ds, REG_PORT(i),
  317. PORT_STATUS);
  318. if (port_status < 0)
  319. continue;
  320. link = !!(port_status & PORT_STATUS_LINK);
  321. }
  322. if (!link) {
  323. if (netif_carrier_ok(dev)) {
  324. netdev_info(dev, "link down\n");
  325. netif_carrier_off(dev);
  326. }
  327. continue;
  328. }
  329. switch (port_status & PORT_STATUS_SPEED_MASK) {
  330. case PORT_STATUS_SPEED_10:
  331. speed = 10;
  332. break;
  333. case PORT_STATUS_SPEED_100:
  334. speed = 100;
  335. break;
  336. case PORT_STATUS_SPEED_1000:
  337. speed = 1000;
  338. break;
  339. default:
  340. speed = -1;
  341. break;
  342. }
  343. duplex = (port_status & PORT_STATUS_DUPLEX) ? 1 : 0;
  344. fc = (port_status & PORT_STATUS_PAUSE_EN) ? 1 : 0;
  345. if (!netif_carrier_ok(dev)) {
  346. netdev_info(dev,
  347. "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  348. speed,
  349. duplex ? "full" : "half",
  350. fc ? "en" : "dis");
  351. netif_carrier_on(dev);
  352. }
  353. }
  354. }
  355. static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
  356. {
  357. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  358. switch (ps->id) {
  359. case PORT_SWITCH_ID_6352:
  360. case PORT_SWITCH_ID_6172:
  361. case PORT_SWITCH_ID_6176:
  362. return true;
  363. }
  364. return false;
  365. }
  366. static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
  367. {
  368. int ret;
  369. int i;
  370. for (i = 0; i < 10; i++) {
  371. ret = REG_READ(REG_GLOBAL, GLOBAL_STATS_OP);
  372. if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
  373. return 0;
  374. }
  375. return -ETIMEDOUT;
  376. }
  377. static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
  378. {
  379. int ret;
  380. if (mv88e6xxx_6352_family(ds))
  381. port = (port + 1) << 5;
  382. /* Snapshot the hardware statistics counters for this port. */
  383. REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP,
  384. GLOBAL_STATS_OP_CAPTURE_PORT |
  385. GLOBAL_STATS_OP_HIST_RX_TX | port);
  386. /* Wait for the snapshotting to complete. */
  387. ret = mv88e6xxx_stats_wait(ds);
  388. if (ret < 0)
  389. return ret;
  390. return 0;
  391. }
  392. static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
  393. {
  394. u32 _val;
  395. int ret;
  396. *val = 0;
  397. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
  398. GLOBAL_STATS_OP_READ_CAPTURED |
  399. GLOBAL_STATS_OP_HIST_RX_TX | stat);
  400. if (ret < 0)
  401. return;
  402. ret = mv88e6xxx_stats_wait(ds);
  403. if (ret < 0)
  404. return;
  405. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
  406. if (ret < 0)
  407. return;
  408. _val = ret << 16;
  409. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
  410. if (ret < 0)
  411. return;
  412. *val = _val | ret;
  413. }
  414. static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
  415. { "in_good_octets", 8, 0x00, },
  416. { "in_bad_octets", 4, 0x02, },
  417. { "in_unicast", 4, 0x04, },
  418. { "in_broadcasts", 4, 0x06, },
  419. { "in_multicasts", 4, 0x07, },
  420. { "in_pause", 4, 0x16, },
  421. { "in_undersize", 4, 0x18, },
  422. { "in_fragments", 4, 0x19, },
  423. { "in_oversize", 4, 0x1a, },
  424. { "in_jabber", 4, 0x1b, },
  425. { "in_rx_error", 4, 0x1c, },
  426. { "in_fcs_error", 4, 0x1d, },
  427. { "out_octets", 8, 0x0e, },
  428. { "out_unicast", 4, 0x10, },
  429. { "out_broadcasts", 4, 0x13, },
  430. { "out_multicasts", 4, 0x12, },
  431. { "out_pause", 4, 0x15, },
  432. { "excessive", 4, 0x11, },
  433. { "collisions", 4, 0x1e, },
  434. { "deferred", 4, 0x05, },
  435. { "single", 4, 0x14, },
  436. { "multiple", 4, 0x17, },
  437. { "out_fcs_error", 4, 0x03, },
  438. { "late", 4, 0x1f, },
  439. { "hist_64bytes", 4, 0x08, },
  440. { "hist_65_127bytes", 4, 0x09, },
  441. { "hist_128_255bytes", 4, 0x0a, },
  442. { "hist_256_511bytes", 4, 0x0b, },
  443. { "hist_512_1023bytes", 4, 0x0c, },
  444. { "hist_1024_max_bytes", 4, 0x0d, },
  445. /* Not all devices have the following counters */
  446. { "sw_in_discards", 4, 0x110, },
  447. { "sw_in_filtered", 2, 0x112, },
  448. { "sw_out_filtered", 2, 0x113, },
  449. };
  450. static bool have_sw_in_discards(struct dsa_switch *ds)
  451. {
  452. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  453. switch (ps->id) {
  454. case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161:
  455. case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171:
  456. case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176:
  457. case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185:
  458. case PORT_SWITCH_ID_6352:
  459. return true;
  460. default:
  461. return false;
  462. }
  463. }
  464. static void _mv88e6xxx_get_strings(struct dsa_switch *ds,
  465. int nr_stats,
  466. struct mv88e6xxx_hw_stat *stats,
  467. int port, uint8_t *data)
  468. {
  469. int i;
  470. for (i = 0; i < nr_stats; i++) {
  471. memcpy(data + i * ETH_GSTRING_LEN,
  472. stats[i].string, ETH_GSTRING_LEN);
  473. }
  474. }
  475. static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
  476. int nr_stats,
  477. struct mv88e6xxx_hw_stat *stats,
  478. int port, uint64_t *data)
  479. {
  480. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  481. int ret;
  482. int i;
  483. mutex_lock(&ps->stats_mutex);
  484. ret = mv88e6xxx_stats_snapshot(ds, port);
  485. if (ret < 0) {
  486. mutex_unlock(&ps->stats_mutex);
  487. return;
  488. }
  489. /* Read each of the counters. */
  490. for (i = 0; i < nr_stats; i++) {
  491. struct mv88e6xxx_hw_stat *s = stats + i;
  492. u32 low;
  493. u32 high = 0;
  494. if (s->reg >= 0x100) {
  495. ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
  496. s->reg - 0x100);
  497. if (ret < 0)
  498. goto error;
  499. low = ret;
  500. if (s->sizeof_stat == 4) {
  501. ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
  502. s->reg - 0x100 + 1);
  503. if (ret < 0)
  504. goto error;
  505. high = ret;
  506. }
  507. data[i] = (((u64)high) << 16) | low;
  508. continue;
  509. }
  510. mv88e6xxx_stats_read(ds, s->reg, &low);
  511. if (s->sizeof_stat == 8)
  512. mv88e6xxx_stats_read(ds, s->reg + 1, &high);
  513. data[i] = (((u64)high) << 32) | low;
  514. }
  515. error:
  516. mutex_unlock(&ps->stats_mutex);
  517. }
  518. /* All the statistics in the table */
  519. void
  520. mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  521. {
  522. if (have_sw_in_discards(ds))
  523. _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
  524. mv88e6xxx_hw_stats, port, data);
  525. else
  526. _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
  527. mv88e6xxx_hw_stats, port, data);
  528. }
  529. int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
  530. {
  531. if (have_sw_in_discards(ds))
  532. return ARRAY_SIZE(mv88e6xxx_hw_stats);
  533. return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
  534. }
  535. void
  536. mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
  537. int port, uint64_t *data)
  538. {
  539. if (have_sw_in_discards(ds))
  540. _mv88e6xxx_get_ethtool_stats(
  541. ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
  542. mv88e6xxx_hw_stats, port, data);
  543. else
  544. _mv88e6xxx_get_ethtool_stats(
  545. ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
  546. mv88e6xxx_hw_stats, port, data);
  547. }
  548. int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
  549. {
  550. return 32 * sizeof(u16);
  551. }
  552. void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
  553. struct ethtool_regs *regs, void *_p)
  554. {
  555. u16 *p = _p;
  556. int i;
  557. regs->version = 0;
  558. memset(p, 0xff, 32 * sizeof(u16));
  559. for (i = 0; i < 32; i++) {
  560. int ret;
  561. ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
  562. if (ret >= 0)
  563. p[i] = ret;
  564. }
  565. }
  566. #ifdef CONFIG_NET_DSA_HWMON
  567. int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
  568. {
  569. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  570. int ret;
  571. int val;
  572. *temp = 0;
  573. mutex_lock(&ps->phy_mutex);
  574. ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
  575. if (ret < 0)
  576. goto error;
  577. /* Enable temperature sensor */
  578. ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
  579. if (ret < 0)
  580. goto error;
  581. ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
  582. if (ret < 0)
  583. goto error;
  584. /* Wait for temperature to stabilize */
  585. usleep_range(10000, 12000);
  586. val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
  587. if (val < 0) {
  588. ret = val;
  589. goto error;
  590. }
  591. /* Disable temperature sensor */
  592. ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
  593. if (ret < 0)
  594. goto error;
  595. *temp = ((val & 0x1f) - 5) * 5;
  596. error:
  597. _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
  598. mutex_unlock(&ps->phy_mutex);
  599. return ret;
  600. }
  601. #endif /* CONFIG_NET_DSA_HWMON */
  602. static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
  603. {
  604. unsigned long timeout = jiffies + HZ / 10;
  605. while (time_before(jiffies, timeout)) {
  606. int ret;
  607. ret = REG_READ(reg, offset);
  608. if (!(ret & mask))
  609. return 0;
  610. usleep_range(1000, 2000);
  611. }
  612. return -ETIMEDOUT;
  613. }
  614. int mv88e6xxx_phy_wait(struct dsa_switch *ds)
  615. {
  616. return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
  617. GLOBAL2_SMI_OP_BUSY);
  618. }
  619. int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
  620. {
  621. return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
  622. GLOBAL2_EEPROM_OP_LOAD);
  623. }
  624. int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
  625. {
  626. return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
  627. GLOBAL2_EEPROM_OP_BUSY);
  628. }
  629. /* Must be called with SMI lock held */
  630. static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
  631. {
  632. unsigned long timeout = jiffies + HZ / 10;
  633. while (time_before(jiffies, timeout)) {
  634. int ret;
  635. ret = _mv88e6xxx_reg_read(ds, reg, offset);
  636. if (ret < 0)
  637. return ret;
  638. if (!(ret & mask))
  639. return 0;
  640. usleep_range(1000, 2000);
  641. }
  642. return -ETIMEDOUT;
  643. }
  644. /* Must be called with SMI lock held */
  645. static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
  646. {
  647. return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
  648. GLOBAL_ATU_OP_BUSY);
  649. }
  650. /* Must be called with phy mutex held */
  651. static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
  652. int regnum)
  653. {
  654. int ret;
  655. REG_WRITE(REG_GLOBAL2, GLOBAL2_SMI_OP,
  656. GLOBAL2_SMI_OP_22_READ | (addr << 5) | regnum);
  657. ret = mv88e6xxx_phy_wait(ds);
  658. if (ret < 0)
  659. return ret;
  660. return REG_READ(REG_GLOBAL2, GLOBAL2_SMI_DATA);
  661. }
  662. /* Must be called with phy mutex held */
  663. static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
  664. int regnum, u16 val)
  665. {
  666. REG_WRITE(REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
  667. REG_WRITE(REG_GLOBAL2, GLOBAL2_SMI_OP,
  668. GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | regnum);
  669. return mv88e6xxx_phy_wait(ds);
  670. }
  671. int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
  672. {
  673. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  674. int reg;
  675. mutex_lock(&ps->phy_mutex);
  676. reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
  677. if (reg < 0)
  678. goto out;
  679. e->eee_enabled = !!(reg & 0x0200);
  680. e->tx_lpi_enabled = !!(reg & 0x0100);
  681. reg = mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
  682. if (reg < 0)
  683. goto out;
  684. e->eee_active = !!(reg & PORT_STATUS_EEE);
  685. reg = 0;
  686. out:
  687. mutex_unlock(&ps->phy_mutex);
  688. return reg;
  689. }
  690. int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
  691. struct phy_device *phydev, struct ethtool_eee *e)
  692. {
  693. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  694. int reg;
  695. int ret;
  696. mutex_lock(&ps->phy_mutex);
  697. ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
  698. if (ret < 0)
  699. goto out;
  700. reg = ret & ~0x0300;
  701. if (e->eee_enabled)
  702. reg |= 0x0200;
  703. if (e->tx_lpi_enabled)
  704. reg |= 0x0100;
  705. ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
  706. out:
  707. mutex_unlock(&ps->phy_mutex);
  708. return ret;
  709. }
  710. static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd)
  711. {
  712. int ret;
  713. ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x01, fid);
  714. if (ret < 0)
  715. return ret;
  716. ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
  717. if (ret < 0)
  718. return ret;
  719. return _mv88e6xxx_atu_wait(ds);
  720. }
  721. static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid)
  722. {
  723. int ret;
  724. ret = _mv88e6xxx_atu_wait(ds);
  725. if (ret < 0)
  726. return ret;
  727. return _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_FLUSH_NON_STATIC_DB);
  728. }
  729. static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
  730. {
  731. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  732. int reg, ret = 0;
  733. u8 oldstate;
  734. mutex_lock(&ps->smi_mutex);
  735. reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
  736. if (reg < 0) {
  737. ret = reg;
  738. goto abort;
  739. }
  740. oldstate = reg & PORT_CONTROL_STATE_MASK;
  741. if (oldstate != state) {
  742. /* Flush forwarding database if we're moving a port
  743. * from Learning or Forwarding state to Disabled or
  744. * Blocking or Listening state.
  745. */
  746. if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
  747. state <= PORT_CONTROL_STATE_BLOCKING) {
  748. ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]);
  749. if (ret)
  750. goto abort;
  751. }
  752. reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
  753. ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
  754. reg);
  755. }
  756. abort:
  757. mutex_unlock(&ps->smi_mutex);
  758. return ret;
  759. }
  760. /* Must be called with smi lock held */
  761. static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port)
  762. {
  763. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  764. u8 fid = ps->fid[port];
  765. u16 reg = fid << 12;
  766. if (dsa_is_cpu_port(ds, port))
  767. reg |= ds->phys_port_mask;
  768. else
  769. reg |= (ps->bridge_mask[fid] |
  770. (1 << dsa_upstream_port(ds))) & ~(1 << port);
  771. return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
  772. }
  773. /* Must be called with smi lock held */
  774. static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid)
  775. {
  776. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  777. int port;
  778. u32 mask;
  779. int ret;
  780. mask = ds->phys_port_mask;
  781. while (mask) {
  782. port = __ffs(mask);
  783. mask &= ~(1 << port);
  784. if (ps->fid[port] != fid)
  785. continue;
  786. ret = _mv88e6xxx_update_port_config(ds, port);
  787. if (ret)
  788. return ret;
  789. }
  790. return _mv88e6xxx_flush_fid(ds, fid);
  791. }
  792. /* Bridge handling functions */
  793. int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
  794. {
  795. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  796. int ret = 0;
  797. u32 nmask;
  798. int fid;
  799. /* If the bridge group is not empty, join that group.
  800. * Otherwise create a new group.
  801. */
  802. fid = ps->fid[port];
  803. nmask = br_port_mask & ~(1 << port);
  804. if (nmask)
  805. fid = ps->fid[__ffs(nmask)];
  806. nmask = ps->bridge_mask[fid] | (1 << port);
  807. if (nmask != br_port_mask) {
  808. netdev_err(ds->ports[port],
  809. "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
  810. fid, br_port_mask, nmask);
  811. return -EINVAL;
  812. }
  813. mutex_lock(&ps->smi_mutex);
  814. ps->bridge_mask[fid] = br_port_mask;
  815. if (fid != ps->fid[port]) {
  816. ps->fid_mask |= 1 << ps->fid[port];
  817. ps->fid[port] = fid;
  818. ret = _mv88e6xxx_update_bridge_config(ds, fid);
  819. }
  820. mutex_unlock(&ps->smi_mutex);
  821. return ret;
  822. }
  823. int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
  824. {
  825. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  826. u8 fid, newfid;
  827. int ret;
  828. fid = ps->fid[port];
  829. if (ps->bridge_mask[fid] != br_port_mask) {
  830. netdev_err(ds->ports[port],
  831. "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
  832. fid, br_port_mask, ps->bridge_mask[fid]);
  833. return -EINVAL;
  834. }
  835. /* If the port was the last port of a bridge, we are done.
  836. * Otherwise assign a new fid to the port, and fix up
  837. * the bridge configuration.
  838. */
  839. if (br_port_mask == (1 << port))
  840. return 0;
  841. mutex_lock(&ps->smi_mutex);
  842. newfid = __ffs(ps->fid_mask);
  843. ps->fid[port] = newfid;
  844. ps->fid_mask &= (1 << newfid);
  845. ps->bridge_mask[fid] &= ~(1 << port);
  846. ps->bridge_mask[newfid] = 1 << port;
  847. ret = _mv88e6xxx_update_bridge_config(ds, fid);
  848. if (!ret)
  849. ret = _mv88e6xxx_update_bridge_config(ds, newfid);
  850. mutex_unlock(&ps->smi_mutex);
  851. return ret;
  852. }
  853. int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
  854. {
  855. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  856. int stp_state;
  857. switch (state) {
  858. case BR_STATE_DISABLED:
  859. stp_state = PORT_CONTROL_STATE_DISABLED;
  860. break;
  861. case BR_STATE_BLOCKING:
  862. case BR_STATE_LISTENING:
  863. stp_state = PORT_CONTROL_STATE_BLOCKING;
  864. break;
  865. case BR_STATE_LEARNING:
  866. stp_state = PORT_CONTROL_STATE_LEARNING;
  867. break;
  868. case BR_STATE_FORWARDING:
  869. default:
  870. stp_state = PORT_CONTROL_STATE_FORWARDING;
  871. break;
  872. }
  873. netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
  874. /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
  875. * so we can not update the port state directly but need to schedule it.
  876. */
  877. ps->port_state[port] = stp_state;
  878. set_bit(port, &ps->port_state_update_mask);
  879. schedule_work(&ps->bridge_work);
  880. return 0;
  881. }
  882. static int __mv88e6xxx_write_addr(struct dsa_switch *ds,
  883. const unsigned char *addr)
  884. {
  885. int i, ret;
  886. for (i = 0; i < 3; i++) {
  887. ret = _mv88e6xxx_reg_write(
  888. ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
  889. (addr[i * 2] << 8) | addr[i * 2 + 1]);
  890. if (ret < 0)
  891. return ret;
  892. }
  893. return 0;
  894. }
  895. static int __mv88e6xxx_read_addr(struct dsa_switch *ds, unsigned char *addr)
  896. {
  897. int i, ret;
  898. for (i = 0; i < 3; i++) {
  899. ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
  900. GLOBAL_ATU_MAC_01 + i);
  901. if (ret < 0)
  902. return ret;
  903. addr[i * 2] = ret >> 8;
  904. addr[i * 2 + 1] = ret & 0xff;
  905. }
  906. return 0;
  907. }
  908. static int __mv88e6xxx_port_fdb_cmd(struct dsa_switch *ds, int port,
  909. const unsigned char *addr, int state)
  910. {
  911. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  912. u8 fid = ps->fid[port];
  913. int ret;
  914. ret = _mv88e6xxx_atu_wait(ds);
  915. if (ret < 0)
  916. return ret;
  917. ret = __mv88e6xxx_write_addr(ds, addr);
  918. if (ret < 0)
  919. return ret;
  920. ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA,
  921. (0x10 << port) | state);
  922. if (ret)
  923. return ret;
  924. ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_LOAD_DB);
  925. return ret;
  926. }
  927. int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
  928. const unsigned char *addr, u16 vid)
  929. {
  930. int state = is_multicast_ether_addr(addr) ?
  931. GLOBAL_ATU_DATA_STATE_MC_STATIC :
  932. GLOBAL_ATU_DATA_STATE_UC_STATIC;
  933. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  934. int ret;
  935. mutex_lock(&ps->smi_mutex);
  936. ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr, state);
  937. mutex_unlock(&ps->smi_mutex);
  938. return ret;
  939. }
  940. int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
  941. const unsigned char *addr, u16 vid)
  942. {
  943. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  944. int ret;
  945. mutex_lock(&ps->smi_mutex);
  946. ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr,
  947. GLOBAL_ATU_DATA_STATE_UNUSED);
  948. mutex_unlock(&ps->smi_mutex);
  949. return ret;
  950. }
  951. static int __mv88e6xxx_port_getnext(struct dsa_switch *ds, int port,
  952. unsigned char *addr, bool *is_static)
  953. {
  954. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  955. u8 fid = ps->fid[port];
  956. int ret, state;
  957. ret = _mv88e6xxx_atu_wait(ds);
  958. if (ret < 0)
  959. return ret;
  960. ret = __mv88e6xxx_write_addr(ds, addr);
  961. if (ret < 0)
  962. return ret;
  963. do {
  964. ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
  965. if (ret < 0)
  966. return ret;
  967. ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
  968. if (ret < 0)
  969. return ret;
  970. state = ret & GLOBAL_ATU_DATA_STATE_MASK;
  971. if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
  972. return -ENOENT;
  973. } while (!(((ret >> 4) & 0xff) & (1 << port)));
  974. ret = __mv88e6xxx_read_addr(ds, addr);
  975. if (ret < 0)
  976. return ret;
  977. *is_static = state == (is_multicast_ether_addr(addr) ?
  978. GLOBAL_ATU_DATA_STATE_MC_STATIC :
  979. GLOBAL_ATU_DATA_STATE_UC_STATIC);
  980. return 0;
  981. }
  982. /* get next entry for port */
  983. int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port,
  984. unsigned char *addr, bool *is_static)
  985. {
  986. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  987. int ret;
  988. mutex_lock(&ps->smi_mutex);
  989. ret = __mv88e6xxx_port_getnext(ds, port, addr, is_static);
  990. mutex_unlock(&ps->smi_mutex);
  991. return ret;
  992. }
  993. static void mv88e6xxx_bridge_work(struct work_struct *work)
  994. {
  995. struct mv88e6xxx_priv_state *ps;
  996. struct dsa_switch *ds;
  997. int port;
  998. ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
  999. ds = ((struct dsa_switch *)ps) - 1;
  1000. while (ps->port_state_update_mask) {
  1001. port = __ffs(ps->port_state_update_mask);
  1002. clear_bit(port, &ps->port_state_update_mask);
  1003. mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
  1004. }
  1005. }
  1006. int mv88e6xxx_setup_port_common(struct dsa_switch *ds, int port)
  1007. {
  1008. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1009. int ret, fid;
  1010. mutex_lock(&ps->smi_mutex);
  1011. /* Port Control 1: disable trunking, disable sending
  1012. * learning messages to this port.
  1013. */
  1014. ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
  1015. if (ret)
  1016. goto abort;
  1017. /* Port based VLAN map: give each port its own address
  1018. * database, allow the CPU port to talk to each of the 'real'
  1019. * ports, and allow each of the 'real' ports to only talk to
  1020. * the upstream port.
  1021. */
  1022. fid = __ffs(ps->fid_mask);
  1023. ps->fid[port] = fid;
  1024. ps->fid_mask &= ~(1 << fid);
  1025. if (!dsa_is_cpu_port(ds, port))
  1026. ps->bridge_mask[fid] = 1 << port;
  1027. ret = _mv88e6xxx_update_port_config(ds, port);
  1028. if (ret)
  1029. goto abort;
  1030. /* Default VLAN ID and priority: don't set a default VLAN
  1031. * ID, and set the default packet priority to zero.
  1032. */
  1033. ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
  1034. 0x0000);
  1035. abort:
  1036. mutex_unlock(&ps->smi_mutex);
  1037. return ret;
  1038. }
  1039. int mv88e6xxx_setup_common(struct dsa_switch *ds)
  1040. {
  1041. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1042. mutex_init(&ps->smi_mutex);
  1043. mutex_init(&ps->stats_mutex);
  1044. mutex_init(&ps->phy_mutex);
  1045. ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
  1046. ps->fid_mask = (1 << DSA_MAX_PORTS) - 1;
  1047. INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
  1048. return 0;
  1049. }
  1050. int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
  1051. {
  1052. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1053. u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
  1054. unsigned long timeout;
  1055. int ret;
  1056. int i;
  1057. /* Set all ports to the disabled state. */
  1058. for (i = 0; i < ps->num_ports; i++) {
  1059. ret = REG_READ(REG_PORT(i), PORT_CONTROL);
  1060. REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
  1061. }
  1062. /* Wait for transmit queues to drain. */
  1063. usleep_range(2000, 4000);
  1064. /* Reset the switch. Keep the PPU active if requested. The PPU
  1065. * needs to be active to support indirect phy register access
  1066. * through global registers 0x18 and 0x19.
  1067. */
  1068. if (ppu_active)
  1069. REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
  1070. else
  1071. REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
  1072. /* Wait up to one second for reset to complete. */
  1073. timeout = jiffies + 1 * HZ;
  1074. while (time_before(jiffies, timeout)) {
  1075. ret = REG_READ(REG_GLOBAL, 0x00);
  1076. if ((ret & is_reset) == is_reset)
  1077. break;
  1078. usleep_range(1000, 2000);
  1079. }
  1080. if (time_after(jiffies, timeout))
  1081. return -ETIMEDOUT;
  1082. return 0;
  1083. }
  1084. int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
  1085. {
  1086. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1087. int ret;
  1088. mutex_lock(&ps->phy_mutex);
  1089. ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
  1090. if (ret < 0)
  1091. goto error;
  1092. ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
  1093. error:
  1094. _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
  1095. mutex_unlock(&ps->phy_mutex);
  1096. return ret;
  1097. }
  1098. int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
  1099. int reg, int val)
  1100. {
  1101. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1102. int ret;
  1103. mutex_lock(&ps->phy_mutex);
  1104. ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
  1105. if (ret < 0)
  1106. goto error;
  1107. ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
  1108. error:
  1109. _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
  1110. mutex_unlock(&ps->phy_mutex);
  1111. return ret;
  1112. }
  1113. static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
  1114. {
  1115. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1116. if (port >= 0 && port < ps->num_ports)
  1117. return port;
  1118. return -EINVAL;
  1119. }
  1120. int
  1121. mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
  1122. {
  1123. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1124. int addr = mv88e6xxx_port_to_phy_addr(ds, port);
  1125. int ret;
  1126. if (addr < 0)
  1127. return addr;
  1128. mutex_lock(&ps->phy_mutex);
  1129. ret = _mv88e6xxx_phy_read(ds, addr, regnum);
  1130. mutex_unlock(&ps->phy_mutex);
  1131. return ret;
  1132. }
  1133. int
  1134. mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  1135. {
  1136. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1137. int addr = mv88e6xxx_port_to_phy_addr(ds, port);
  1138. int ret;
  1139. if (addr < 0)
  1140. return addr;
  1141. mutex_lock(&ps->phy_mutex);
  1142. ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
  1143. mutex_unlock(&ps->phy_mutex);
  1144. return ret;
  1145. }
  1146. int
  1147. mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
  1148. {
  1149. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1150. int addr = mv88e6xxx_port_to_phy_addr(ds, port);
  1151. int ret;
  1152. if (addr < 0)
  1153. return addr;
  1154. mutex_lock(&ps->phy_mutex);
  1155. ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
  1156. mutex_unlock(&ps->phy_mutex);
  1157. return ret;
  1158. }
  1159. int
  1160. mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
  1161. u16 val)
  1162. {
  1163. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  1164. int addr = mv88e6xxx_port_to_phy_addr(ds, port);
  1165. int ret;
  1166. if (addr < 0)
  1167. return addr;
  1168. mutex_lock(&ps->phy_mutex);
  1169. ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
  1170. mutex_unlock(&ps->phy_mutex);
  1171. return ret;
  1172. }
  1173. static int __init mv88e6xxx_init(void)
  1174. {
  1175. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  1176. register_switch_driver(&mv88e6131_switch_driver);
  1177. #endif
  1178. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  1179. register_switch_driver(&mv88e6123_61_65_switch_driver);
  1180. #endif
  1181. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  1182. register_switch_driver(&mv88e6352_switch_driver);
  1183. #endif
  1184. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
  1185. register_switch_driver(&mv88e6171_switch_driver);
  1186. #endif
  1187. return 0;
  1188. }
  1189. module_init(mv88e6xxx_init);
  1190. static void __exit mv88e6xxx_cleanup(void)
  1191. {
  1192. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
  1193. unregister_switch_driver(&mv88e6171_switch_driver);
  1194. #endif
  1195. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  1196. unregister_switch_driver(&mv88e6352_switch_driver);
  1197. #endif
  1198. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
  1199. unregister_switch_driver(&mv88e6123_61_65_switch_driver);
  1200. #endif
  1201. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
  1202. unregister_switch_driver(&mv88e6131_switch_driver);
  1203. #endif
  1204. }
  1205. module_exit(mv88e6xxx_cleanup);
  1206. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  1207. MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
  1208. MODULE_LICENSE("GPL");