ocelot.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Microsemi Ocelot Switch driver
  4. *
  5. * Copyright (c) 2017 Microsemi Corporation
  6. */
  7. #include <linux/etherdevice.h>
  8. #include <linux/ethtool.h>
  9. #include <linux/if_bridge.h>
  10. #include <linux/if_ether.h>
  11. #include <linux/if_vlan.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/phy.h>
  17. #include <linux/skbuff.h>
  18. #include <net/arp.h>
  19. #include <net/netevent.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/switchdev.h>
  22. #include "ocelot.h"
  23. /* MAC table entry types.
  24. * ENTRYTYPE_NORMAL is subject to aging.
  25. * ENTRYTYPE_LOCKED is not subject to aging.
  26. * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
  27. * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
  28. */
  29. enum macaccess_entry_type {
  30. ENTRYTYPE_NORMAL = 0,
  31. ENTRYTYPE_LOCKED,
  32. ENTRYTYPE_MACv4,
  33. ENTRYTYPE_MACv6,
  34. };
  35. struct ocelot_mact_entry {
  36. u8 mac[ETH_ALEN];
  37. u16 vid;
  38. enum macaccess_entry_type type;
  39. };
  40. static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
  41. {
  42. unsigned int val, timeout = 10;
  43. /* Wait for the issued mac table command to be completed, or timeout.
  44. * When the command read from ANA_TABLES_MACACCESS is
  45. * MACACCESS_CMD_IDLE, the issued command completed successfully.
  46. */
  47. do {
  48. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  49. val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
  50. } while (val != MACACCESS_CMD_IDLE && timeout--);
  51. if (!timeout)
  52. return -ETIMEDOUT;
  53. return 0;
  54. }
  55. static void ocelot_mact_select(struct ocelot *ocelot,
  56. const unsigned char mac[ETH_ALEN],
  57. unsigned int vid)
  58. {
  59. u32 macl = 0, mach = 0;
  60. /* Set the MAC address to handle and the vlan associated in a format
  61. * understood by the hardware.
  62. */
  63. mach |= vid << 16;
  64. mach |= mac[0] << 8;
  65. mach |= mac[1] << 0;
  66. macl |= mac[2] << 24;
  67. macl |= mac[3] << 16;
  68. macl |= mac[4] << 8;
  69. macl |= mac[5] << 0;
  70. ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
  71. ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
  72. }
  73. static int ocelot_mact_learn(struct ocelot *ocelot, int port,
  74. const unsigned char mac[ETH_ALEN],
  75. unsigned int vid,
  76. enum macaccess_entry_type type)
  77. {
  78. ocelot_mact_select(ocelot, mac, vid);
  79. /* Issue a write command */
  80. ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
  81. ANA_TABLES_MACACCESS_DEST_IDX(port) |
  82. ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
  83. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
  84. ANA_TABLES_MACACCESS);
  85. return ocelot_mact_wait_for_completion(ocelot);
  86. }
  87. static int ocelot_mact_forget(struct ocelot *ocelot,
  88. const unsigned char mac[ETH_ALEN],
  89. unsigned int vid)
  90. {
  91. ocelot_mact_select(ocelot, mac, vid);
  92. /* Issue a forget command */
  93. ocelot_write(ocelot,
  94. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
  95. ANA_TABLES_MACACCESS);
  96. return ocelot_mact_wait_for_completion(ocelot);
  97. }
  98. static void ocelot_mact_init(struct ocelot *ocelot)
  99. {
  100. /* Configure the learning mode entries attributes:
  101. * - Do not copy the frame to the CPU extraction queues.
  102. * - Use the vlan and mac_cpoy for dmac lookup.
  103. */
  104. ocelot_rmw(ocelot, 0,
  105. ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
  106. | ANA_AGENCTRL_LEARN_FWD_KILL
  107. | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
  108. ANA_AGENCTRL);
  109. /* Clear the MAC table */
  110. ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
  111. }
  112. static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
  113. {
  114. unsigned int val, timeout = 10;
  115. /* Wait for the issued mac table command to be completed, or timeout.
  116. * When the command read from ANA_TABLES_MACACCESS is
  117. * MACACCESS_CMD_IDLE, the issued command completed successfully.
  118. */
  119. do {
  120. val = ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
  121. val &= ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M;
  122. } while (val != ANA_TABLES_VLANACCESS_CMD_IDLE && timeout--);
  123. if (!timeout)
  124. return -ETIMEDOUT;
  125. return 0;
  126. }
  127. static void ocelot_vlan_init(struct ocelot *ocelot)
  128. {
  129. /* Clear VLAN table, by default all ports are members of all VLANs */
  130. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
  131. ANA_TABLES_VLANACCESS);
  132. ocelot_vlant_wait_for_completion(ocelot);
  133. }
  134. /* Watermark encode
  135. * Bit 8: Unit; 0:1, 1:16
  136. * Bit 7-0: Value to be multiplied with unit
  137. */
  138. static u16 ocelot_wm_enc(u16 value)
  139. {
  140. if (value >= BIT(8))
  141. return BIT(8) | (value / 16);
  142. return value;
  143. }
  144. static void ocelot_port_adjust_link(struct net_device *dev)
  145. {
  146. struct ocelot_port *port = netdev_priv(dev);
  147. struct ocelot *ocelot = port->ocelot;
  148. u8 p = port->chip_port;
  149. int speed, atop_wm, mode = 0;
  150. switch (dev->phydev->speed) {
  151. case SPEED_10:
  152. speed = OCELOT_SPEED_10;
  153. break;
  154. case SPEED_100:
  155. speed = OCELOT_SPEED_100;
  156. break;
  157. case SPEED_1000:
  158. speed = OCELOT_SPEED_1000;
  159. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  160. break;
  161. case SPEED_2500:
  162. speed = OCELOT_SPEED_2500;
  163. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  164. break;
  165. default:
  166. netdev_err(dev, "Unsupported PHY speed: %d\n",
  167. dev->phydev->speed);
  168. return;
  169. }
  170. phy_print_status(dev->phydev);
  171. if (!dev->phydev->link)
  172. return;
  173. /* Only full duplex supported for now */
  174. ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
  175. mode, DEV_MAC_MODE_CFG);
  176. /* Set MAC IFG Gaps
  177. * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
  178. * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
  179. */
  180. ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
  181. /* Load seed (0) and set MAC HDX late collision */
  182. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
  183. DEV_MAC_HDX_CFG_SEED_LOAD,
  184. DEV_MAC_HDX_CFG);
  185. mdelay(1);
  186. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
  187. DEV_MAC_HDX_CFG);
  188. /* Disable HDX fast control */
  189. ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
  190. /* SGMII only for now */
  191. ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
  192. ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
  193. /* Enable PCS */
  194. ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
  195. /* No aneg on SGMII */
  196. ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
  197. /* No loopback */
  198. ocelot_port_writel(port, 0, PCS1G_LB_CFG);
  199. /* Set Max Length and maximum tags allowed */
  200. ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
  201. ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
  202. DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
  203. DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
  204. DEV_MAC_TAGS_CFG);
  205. /* Enable MAC module */
  206. ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
  207. DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
  208. /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
  209. * reset */
  210. ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
  211. DEV_CLOCK_CFG);
  212. /* Set SMAC of Pause frame (00:00:00:00:00:00) */
  213. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
  214. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
  215. /* No PFC */
  216. ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
  217. ANA_PFC_PFC_CFG, p);
  218. /* Set Pause WM hysteresis
  219. * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  220. * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  221. */
  222. ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
  223. SYS_PAUSE_CFG_PAUSE_STOP(101) |
  224. SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
  225. /* Core: Enable port for frame transfer */
  226. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  227. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  228. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  229. QSYS_SWITCH_PORT_MODE, p);
  230. /* Flow control */
  231. ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
  232. SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
  233. SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
  234. SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
  235. SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
  236. SYS_MAC_FC_CFG, p);
  237. ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
  238. /* Tail dropping watermark */
  239. atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
  240. ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
  241. SYS_ATOP, p);
  242. ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
  243. }
  244. static int ocelot_port_open(struct net_device *dev)
  245. {
  246. struct ocelot_port *port = netdev_priv(dev);
  247. struct ocelot *ocelot = port->ocelot;
  248. int err;
  249. /* Enable receiving frames on the port, and activate auto-learning of
  250. * MAC addresses.
  251. */
  252. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
  253. ANA_PORT_PORT_CFG_RECV_ENA |
  254. ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
  255. ANA_PORT_PORT_CFG, port->chip_port);
  256. err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
  257. PHY_INTERFACE_MODE_NA);
  258. if (err) {
  259. netdev_err(dev, "Could not attach to PHY\n");
  260. return err;
  261. }
  262. dev->phydev = port->phy;
  263. phy_attached_info(port->phy);
  264. phy_start(port->phy);
  265. return 0;
  266. }
  267. static int ocelot_port_stop(struct net_device *dev)
  268. {
  269. struct ocelot_port *port = netdev_priv(dev);
  270. phy_disconnect(port->phy);
  271. dev->phydev = NULL;
  272. ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
  273. ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
  274. QSYS_SWITCH_PORT_MODE, port->chip_port);
  275. return 0;
  276. }
  277. /* Generate the IFH for frame injection
  278. *
  279. * The IFH is a 128bit-value
  280. * bit 127: bypass the analyzer processing
  281. * bit 56-67: destination mask
  282. * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
  283. * bit 20-27: cpu extraction queue mask
  284. * bit 16: tag type 0: C-tag, 1: S-tag
  285. * bit 0-11: VID
  286. */
  287. static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
  288. {
  289. ifh[0] = IFH_INJ_BYPASS;
  290. ifh[1] = (0xff00 & info->port) >> 8;
  291. ifh[2] = (0xff & info->port) << 24;
  292. ifh[3] = IFH_INJ_POP_CNT_DISABLE | (info->cpuq << 20) |
  293. (info->tag_type << 16) | info->vid;
  294. return 0;
  295. }
  296. static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
  297. {
  298. struct ocelot_port *port = netdev_priv(dev);
  299. struct ocelot *ocelot = port->ocelot;
  300. u32 val, ifh[IFH_LEN];
  301. struct frame_info info = {};
  302. u8 grp = 0; /* Send everything on CPU group 0 */
  303. unsigned int i, count, last;
  304. val = ocelot_read(ocelot, QS_INJ_STATUS);
  305. if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
  306. (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
  307. return NETDEV_TX_BUSY;
  308. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  309. QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
  310. info.port = BIT(port->chip_port);
  311. info.cpuq = 0xff;
  312. ocelot_gen_ifh(ifh, &info);
  313. for (i = 0; i < IFH_LEN; i++)
  314. ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
  315. count = (skb->len + 3) / 4;
  316. last = skb->len % 4;
  317. for (i = 0; i < count; i++) {
  318. ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
  319. }
  320. /* Add padding */
  321. while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
  322. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  323. i++;
  324. }
  325. /* Indicate EOF and valid bytes in last word */
  326. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  327. QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
  328. QS_INJ_CTRL_EOF,
  329. QS_INJ_CTRL, grp);
  330. /* Add dummy CRC */
  331. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  332. skb_tx_timestamp(skb);
  333. dev->stats.tx_packets++;
  334. dev->stats.tx_bytes += skb->len;
  335. dev_kfree_skb_any(skb);
  336. return NETDEV_TX_OK;
  337. }
  338. static void ocelot_mact_mc_reset(struct ocelot_port *port)
  339. {
  340. struct ocelot *ocelot = port->ocelot;
  341. struct netdev_hw_addr *ha, *n;
  342. /* Free and forget all the MAC addresses stored in the port private mc
  343. * list. These are mc addresses that were previously added by calling
  344. * ocelot_mact_mc_add().
  345. */
  346. list_for_each_entry_safe(ha, n, &port->mc, list) {
  347. ocelot_mact_forget(ocelot, ha->addr, port->pvid);
  348. list_del(&ha->list);
  349. kfree(ha);
  350. }
  351. }
  352. static int ocelot_mact_mc_add(struct ocelot_port *port,
  353. struct netdev_hw_addr *hw_addr)
  354. {
  355. struct ocelot *ocelot = port->ocelot;
  356. struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_KERNEL);
  357. if (!ha)
  358. return -ENOMEM;
  359. memcpy(ha, hw_addr, sizeof(*ha));
  360. list_add_tail(&ha->list, &port->mc);
  361. ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
  362. ENTRYTYPE_LOCKED);
  363. return 0;
  364. }
  365. static void ocelot_set_rx_mode(struct net_device *dev)
  366. {
  367. struct ocelot_port *port = netdev_priv(dev);
  368. struct ocelot *ocelot = port->ocelot;
  369. struct netdev_hw_addr *ha;
  370. int i;
  371. u32 val;
  372. /* This doesn't handle promiscuous mode because the bridge core is
  373. * setting IFF_PROMISC on all slave interfaces and all frames would be
  374. * forwarded to the CPU port.
  375. */
  376. val = GENMASK(ocelot->num_phys_ports - 1, 0);
  377. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
  378. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  379. /* Handle the device multicast addresses. First remove all the
  380. * previously installed addresses and then add the latest ones to the
  381. * mac table.
  382. */
  383. ocelot_mact_mc_reset(port);
  384. netdev_for_each_mc_addr(ha, dev)
  385. ocelot_mact_mc_add(port, ha);
  386. }
  387. static int ocelot_port_get_phys_port_name(struct net_device *dev,
  388. char *buf, size_t len)
  389. {
  390. struct ocelot_port *port = netdev_priv(dev);
  391. int ret;
  392. ret = snprintf(buf, len, "p%d", port->chip_port);
  393. if (ret >= len)
  394. return -EINVAL;
  395. return 0;
  396. }
  397. static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
  398. {
  399. struct ocelot_port *port = netdev_priv(dev);
  400. struct ocelot *ocelot = port->ocelot;
  401. const struct sockaddr *addr = p;
  402. /* Learn the new net device MAC address in the mac table. */
  403. ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
  404. ENTRYTYPE_LOCKED);
  405. /* Then forget the previous one. */
  406. ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
  407. ether_addr_copy(dev->dev_addr, addr->sa_data);
  408. return 0;
  409. }
  410. static void ocelot_get_stats64(struct net_device *dev,
  411. struct rtnl_link_stats64 *stats)
  412. {
  413. struct ocelot_port *port = netdev_priv(dev);
  414. struct ocelot *ocelot = port->ocelot;
  415. /* Configure the port to read the stats from */
  416. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
  417. SYS_STAT_CFG);
  418. /* Get Rx stats */
  419. stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
  420. stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
  421. ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
  422. ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
  423. ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
  424. ocelot_read(ocelot, SYS_COUNT_RX_64) +
  425. ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
  426. ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
  427. ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
  428. ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
  429. ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
  430. stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
  431. stats->rx_dropped = dev->stats.rx_dropped;
  432. /* Get Tx stats */
  433. stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
  434. stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
  435. ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
  436. ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
  437. ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
  438. ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
  439. ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
  440. stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
  441. ocelot_read(ocelot, SYS_COUNT_TX_AGING);
  442. stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
  443. }
  444. static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  445. struct net_device *dev, const unsigned char *addr,
  446. u16 vid, u16 flags)
  447. {
  448. struct ocelot_port *port = netdev_priv(dev);
  449. struct ocelot *ocelot = port->ocelot;
  450. return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
  451. ENTRYTYPE_NORMAL);
  452. }
  453. static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  454. struct net_device *dev,
  455. const unsigned char *addr, u16 vid)
  456. {
  457. struct ocelot_port *port = netdev_priv(dev);
  458. struct ocelot *ocelot = port->ocelot;
  459. return ocelot_mact_forget(ocelot, addr, vid);
  460. }
  461. struct ocelot_dump_ctx {
  462. struct net_device *dev;
  463. struct sk_buff *skb;
  464. struct netlink_callback *cb;
  465. int idx;
  466. };
  467. static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
  468. struct ocelot_dump_ctx *dump)
  469. {
  470. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  471. u32 seq = dump->cb->nlh->nlmsg_seq;
  472. struct nlmsghdr *nlh;
  473. struct ndmsg *ndm;
  474. if (dump->idx < dump->cb->args[2])
  475. goto skip;
  476. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  477. sizeof(*ndm), NLM_F_MULTI);
  478. if (!nlh)
  479. return -EMSGSIZE;
  480. ndm = nlmsg_data(nlh);
  481. ndm->ndm_family = AF_BRIDGE;
  482. ndm->ndm_pad1 = 0;
  483. ndm->ndm_pad2 = 0;
  484. ndm->ndm_flags = NTF_SELF;
  485. ndm->ndm_type = 0;
  486. ndm->ndm_ifindex = dump->dev->ifindex;
  487. ndm->ndm_state = NUD_REACHABLE;
  488. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
  489. goto nla_put_failure;
  490. if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
  491. goto nla_put_failure;
  492. nlmsg_end(dump->skb, nlh);
  493. skip:
  494. dump->idx++;
  495. return 0;
  496. nla_put_failure:
  497. nlmsg_cancel(dump->skb, nlh);
  498. return -EMSGSIZE;
  499. }
  500. static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
  501. struct ocelot_mact_entry *entry)
  502. {
  503. struct ocelot *ocelot = port->ocelot;
  504. char mac[ETH_ALEN];
  505. u32 val, dst, macl, mach;
  506. /* Set row and column to read from */
  507. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
  508. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
  509. /* Issue a read command */
  510. ocelot_write(ocelot,
  511. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
  512. ANA_TABLES_MACACCESS);
  513. if (ocelot_mact_wait_for_completion(ocelot))
  514. return -ETIMEDOUT;
  515. /* Read the entry flags */
  516. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  517. if (!(val & ANA_TABLES_MACACCESS_VALID))
  518. return -EINVAL;
  519. /* If the entry read has another port configured as its destination,
  520. * do not report it.
  521. */
  522. dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
  523. if (dst != port->chip_port)
  524. return -EINVAL;
  525. /* Get the entry's MAC address and VLAN id */
  526. macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
  527. mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
  528. mac[0] = (mach >> 8) & 0xff;
  529. mac[1] = (mach >> 0) & 0xff;
  530. mac[2] = (macl >> 24) & 0xff;
  531. mac[3] = (macl >> 16) & 0xff;
  532. mac[4] = (macl >> 8) & 0xff;
  533. mac[5] = (macl >> 0) & 0xff;
  534. entry->vid = (mach >> 16) & 0xfff;
  535. ether_addr_copy(entry->mac, mac);
  536. return 0;
  537. }
  538. static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  539. struct net_device *dev,
  540. struct net_device *filter_dev, int *idx)
  541. {
  542. struct ocelot_port *port = netdev_priv(dev);
  543. int i, j, ret = 0;
  544. struct ocelot_dump_ctx dump = {
  545. .dev = dev,
  546. .skb = skb,
  547. .cb = cb,
  548. .idx = *idx,
  549. };
  550. struct ocelot_mact_entry entry;
  551. /* Loop through all the mac tables entries. There are 1024 rows of 4
  552. * entries.
  553. */
  554. for (i = 0; i < 1024; i++) {
  555. for (j = 0; j < 4; j++) {
  556. ret = ocelot_mact_read(port, i, j, &entry);
  557. /* If the entry is invalid (wrong port, invalid...),
  558. * skip it.
  559. */
  560. if (ret == -EINVAL)
  561. continue;
  562. else if (ret)
  563. goto end;
  564. ret = ocelot_fdb_do_dump(&entry, &dump);
  565. if (ret)
  566. goto end;
  567. }
  568. }
  569. end:
  570. *idx = dump.idx;
  571. return ret;
  572. }
  573. static const struct net_device_ops ocelot_port_netdev_ops = {
  574. .ndo_open = ocelot_port_open,
  575. .ndo_stop = ocelot_port_stop,
  576. .ndo_start_xmit = ocelot_port_xmit,
  577. .ndo_set_rx_mode = ocelot_set_rx_mode,
  578. .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
  579. .ndo_set_mac_address = ocelot_port_set_mac_address,
  580. .ndo_get_stats64 = ocelot_get_stats64,
  581. .ndo_fdb_add = ocelot_fdb_add,
  582. .ndo_fdb_del = ocelot_fdb_del,
  583. .ndo_fdb_dump = ocelot_fdb_dump,
  584. };
  585. static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  586. {
  587. struct ocelot_port *port = netdev_priv(netdev);
  588. struct ocelot *ocelot = port->ocelot;
  589. int i;
  590. if (sset != ETH_SS_STATS)
  591. return;
  592. for (i = 0; i < ocelot->num_stats; i++)
  593. memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
  594. ETH_GSTRING_LEN);
  595. }
  596. static void ocelot_check_stats(struct work_struct *work)
  597. {
  598. struct delayed_work *del_work = to_delayed_work(work);
  599. struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
  600. int i, j;
  601. mutex_lock(&ocelot->stats_lock);
  602. for (i = 0; i < ocelot->num_phys_ports; i++) {
  603. /* Configure the port to read the stats from */
  604. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
  605. for (j = 0; j < ocelot->num_stats; j++) {
  606. u32 val;
  607. unsigned int idx = i * ocelot->num_stats + j;
  608. val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
  609. ocelot->stats_layout[j].offset);
  610. if (val < (ocelot->stats[idx] & U32_MAX))
  611. ocelot->stats[idx] += (u64)1 << 32;
  612. ocelot->stats[idx] = (ocelot->stats[idx] &
  613. ~(u64)U32_MAX) + val;
  614. }
  615. }
  616. cancel_delayed_work(&ocelot->stats_work);
  617. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  618. OCELOT_STATS_CHECK_DELAY);
  619. mutex_unlock(&ocelot->stats_lock);
  620. }
  621. static void ocelot_get_ethtool_stats(struct net_device *dev,
  622. struct ethtool_stats *stats, u64 *data)
  623. {
  624. struct ocelot_port *port = netdev_priv(dev);
  625. struct ocelot *ocelot = port->ocelot;
  626. int i;
  627. /* check and update now */
  628. ocelot_check_stats(&ocelot->stats_work.work);
  629. /* Copy all counters */
  630. for (i = 0; i < ocelot->num_stats; i++)
  631. *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
  632. }
  633. static int ocelot_get_sset_count(struct net_device *dev, int sset)
  634. {
  635. struct ocelot_port *port = netdev_priv(dev);
  636. struct ocelot *ocelot = port->ocelot;
  637. if (sset != ETH_SS_STATS)
  638. return -EOPNOTSUPP;
  639. return ocelot->num_stats;
  640. }
  641. static const struct ethtool_ops ocelot_ethtool_ops = {
  642. .get_strings = ocelot_get_strings,
  643. .get_ethtool_stats = ocelot_get_ethtool_stats,
  644. .get_sset_count = ocelot_get_sset_count,
  645. };
  646. static int ocelot_port_attr_get(struct net_device *dev,
  647. struct switchdev_attr *attr)
  648. {
  649. struct ocelot_port *ocelot_port = netdev_priv(dev);
  650. struct ocelot *ocelot = ocelot_port->ocelot;
  651. switch (attr->id) {
  652. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  653. attr->u.ppid.id_len = sizeof(ocelot->base_mac);
  654. memcpy(&attr->u.ppid.id, &ocelot->base_mac,
  655. attr->u.ppid.id_len);
  656. break;
  657. default:
  658. return -EOPNOTSUPP;
  659. }
  660. return 0;
  661. }
  662. static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
  663. struct switchdev_trans *trans,
  664. u8 state)
  665. {
  666. struct ocelot *ocelot = ocelot_port->ocelot;
  667. u32 port_cfg;
  668. int port, i;
  669. if (switchdev_trans_ph_prepare(trans))
  670. return 0;
  671. if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
  672. return 0;
  673. port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
  674. ocelot_port->chip_port);
  675. switch (state) {
  676. case BR_STATE_FORWARDING:
  677. ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
  678. /* Fallthrough */
  679. case BR_STATE_LEARNING:
  680. port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
  681. break;
  682. default:
  683. port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
  684. ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
  685. break;
  686. }
  687. ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
  688. ocelot_port->chip_port);
  689. /* Apply FWD mask. The loop is needed to add/remove the current port as
  690. * a source for the other ports.
  691. */
  692. for (port = 0; port < ocelot->num_phys_ports; port++) {
  693. if (ocelot->bridge_fwd_mask & BIT(port)) {
  694. unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
  695. for (i = 0; i < ocelot->num_phys_ports; i++) {
  696. unsigned long bond_mask = ocelot->lags[i];
  697. if (!bond_mask)
  698. continue;
  699. if (bond_mask & BIT(port)) {
  700. mask &= ~bond_mask;
  701. break;
  702. }
  703. }
  704. ocelot_write_rix(ocelot,
  705. BIT(ocelot->num_phys_ports) | mask,
  706. ANA_PGID_PGID, PGID_SRC + port);
  707. } else {
  708. /* Only the CPU port, this is compatible with link
  709. * aggregation.
  710. */
  711. ocelot_write_rix(ocelot,
  712. BIT(ocelot->num_phys_ports),
  713. ANA_PGID_PGID, PGID_SRC + port);
  714. }
  715. }
  716. return 0;
  717. }
  718. static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
  719. unsigned long ageing_clock_t)
  720. {
  721. struct ocelot *ocelot = ocelot_port->ocelot;
  722. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
  723. u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
  724. ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
  725. ANA_AUTOAGE);
  726. }
  727. static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
  728. {
  729. struct ocelot *ocelot = port->ocelot;
  730. u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
  731. port->chip_port);
  732. if (mc)
  733. val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  734. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  735. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
  736. else
  737. val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  738. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  739. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
  740. ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
  741. }
  742. static int ocelot_port_attr_set(struct net_device *dev,
  743. const struct switchdev_attr *attr,
  744. struct switchdev_trans *trans)
  745. {
  746. struct ocelot_port *ocelot_port = netdev_priv(dev);
  747. int err = 0;
  748. switch (attr->id) {
  749. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  750. ocelot_port_attr_stp_state_set(ocelot_port, trans,
  751. attr->u.stp_state);
  752. break;
  753. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  754. ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
  755. break;
  756. case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
  757. ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
  758. break;
  759. default:
  760. err = -EOPNOTSUPP;
  761. break;
  762. }
  763. return err;
  764. }
  765. static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
  766. const unsigned char *addr,
  767. u16 vid)
  768. {
  769. struct ocelot_multicast *mc;
  770. list_for_each_entry(mc, &ocelot->multicast, list) {
  771. if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
  772. return mc;
  773. }
  774. return NULL;
  775. }
  776. static int ocelot_port_obj_add_mdb(struct net_device *dev,
  777. const struct switchdev_obj_port_mdb *mdb,
  778. struct switchdev_trans *trans)
  779. {
  780. struct ocelot_port *port = netdev_priv(dev);
  781. struct ocelot *ocelot = port->ocelot;
  782. struct ocelot_multicast *mc;
  783. unsigned char addr[ETH_ALEN];
  784. u16 vid = mdb->vid;
  785. bool new = false;
  786. if (!vid)
  787. vid = 1;
  788. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  789. if (!mc) {
  790. mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
  791. if (!mc)
  792. return -ENOMEM;
  793. memcpy(mc->addr, mdb->addr, ETH_ALEN);
  794. mc->vid = vid;
  795. list_add_tail(&mc->list, &ocelot->multicast);
  796. new = true;
  797. }
  798. memcpy(addr, mc->addr, ETH_ALEN);
  799. addr[0] = 0;
  800. if (!new) {
  801. addr[2] = mc->ports << 0;
  802. addr[1] = mc->ports << 8;
  803. ocelot_mact_forget(ocelot, addr, vid);
  804. }
  805. mc->ports |= BIT(port->chip_port);
  806. addr[2] = mc->ports << 0;
  807. addr[1] = mc->ports << 8;
  808. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  809. }
  810. static int ocelot_port_obj_del_mdb(struct net_device *dev,
  811. const struct switchdev_obj_port_mdb *mdb)
  812. {
  813. struct ocelot_port *port = netdev_priv(dev);
  814. struct ocelot *ocelot = port->ocelot;
  815. struct ocelot_multicast *mc;
  816. unsigned char addr[ETH_ALEN];
  817. u16 vid = mdb->vid;
  818. if (!vid)
  819. vid = 1;
  820. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  821. if (!mc)
  822. return -ENOENT;
  823. memcpy(addr, mc->addr, ETH_ALEN);
  824. addr[2] = mc->ports << 0;
  825. addr[1] = mc->ports << 8;
  826. addr[0] = 0;
  827. ocelot_mact_forget(ocelot, addr, vid);
  828. mc->ports &= ~BIT(port->chip_port);
  829. if (!mc->ports) {
  830. list_del(&mc->list);
  831. devm_kfree(ocelot->dev, mc);
  832. return 0;
  833. }
  834. addr[2] = mc->ports << 0;
  835. addr[1] = mc->ports << 8;
  836. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  837. }
  838. static int ocelot_port_obj_add(struct net_device *dev,
  839. const struct switchdev_obj *obj,
  840. struct switchdev_trans *trans)
  841. {
  842. int ret = 0;
  843. switch (obj->id) {
  844. case SWITCHDEV_OBJ_ID_PORT_MDB:
  845. ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
  846. trans);
  847. break;
  848. default:
  849. return -EOPNOTSUPP;
  850. }
  851. return ret;
  852. }
  853. static int ocelot_port_obj_del(struct net_device *dev,
  854. const struct switchdev_obj *obj)
  855. {
  856. int ret = 0;
  857. switch (obj->id) {
  858. case SWITCHDEV_OBJ_ID_PORT_MDB:
  859. ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
  860. break;
  861. default:
  862. return -EOPNOTSUPP;
  863. }
  864. return ret;
  865. }
  866. static const struct switchdev_ops ocelot_port_switchdev_ops = {
  867. .switchdev_port_attr_get = ocelot_port_attr_get,
  868. .switchdev_port_attr_set = ocelot_port_attr_set,
  869. .switchdev_port_obj_add = ocelot_port_obj_add,
  870. .switchdev_port_obj_del = ocelot_port_obj_del,
  871. };
  872. static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
  873. struct net_device *bridge)
  874. {
  875. struct ocelot *ocelot = ocelot_port->ocelot;
  876. if (!ocelot->bridge_mask) {
  877. ocelot->hw_bridge_dev = bridge;
  878. } else {
  879. if (ocelot->hw_bridge_dev != bridge)
  880. /* This is adding the port to a second bridge, this is
  881. * unsupported */
  882. return -ENODEV;
  883. }
  884. ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
  885. return 0;
  886. }
  887. static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
  888. struct net_device *bridge)
  889. {
  890. struct ocelot *ocelot = ocelot_port->ocelot;
  891. ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
  892. if (!ocelot->bridge_mask)
  893. ocelot->hw_bridge_dev = NULL;
  894. }
  895. /* Checks if the net_device instance given to us originate from our driver. */
  896. static bool ocelot_netdevice_dev_check(const struct net_device *dev)
  897. {
  898. return dev->netdev_ops == &ocelot_port_netdev_ops;
  899. }
  900. static int ocelot_netdevice_port_event(struct net_device *dev,
  901. unsigned long event,
  902. struct netdev_notifier_changeupper_info *info)
  903. {
  904. struct ocelot_port *ocelot_port = netdev_priv(dev);
  905. int err = 0;
  906. if (!ocelot_netdevice_dev_check(dev))
  907. return 0;
  908. switch (event) {
  909. case NETDEV_CHANGEUPPER:
  910. if (netif_is_bridge_master(info->upper_dev)) {
  911. if (info->linking)
  912. err = ocelot_port_bridge_join(ocelot_port,
  913. info->upper_dev);
  914. else
  915. ocelot_port_bridge_leave(ocelot_port,
  916. info->upper_dev);
  917. }
  918. break;
  919. default:
  920. break;
  921. }
  922. return err;
  923. }
  924. static int ocelot_netdevice_event(struct notifier_block *unused,
  925. unsigned long event, void *ptr)
  926. {
  927. struct netdev_notifier_changeupper_info *info = ptr;
  928. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  929. int ret = 0;
  930. if (netif_is_lag_master(dev)) {
  931. struct net_device *slave;
  932. struct list_head *iter;
  933. netdev_for_each_lower_dev(dev, slave, iter) {
  934. ret = ocelot_netdevice_port_event(slave, event, info);
  935. if (ret)
  936. goto notify;
  937. }
  938. } else {
  939. ret = ocelot_netdevice_port_event(dev, event, info);
  940. }
  941. notify:
  942. return notifier_from_errno(ret);
  943. }
  944. struct notifier_block ocelot_netdevice_nb __read_mostly = {
  945. .notifier_call = ocelot_netdevice_event,
  946. };
  947. EXPORT_SYMBOL(ocelot_netdevice_nb);
  948. int ocelot_probe_port(struct ocelot *ocelot, u8 port,
  949. void __iomem *regs,
  950. struct phy_device *phy)
  951. {
  952. struct ocelot_port *ocelot_port;
  953. struct net_device *dev;
  954. int err;
  955. dev = alloc_etherdev(sizeof(struct ocelot_port));
  956. if (!dev)
  957. return -ENOMEM;
  958. SET_NETDEV_DEV(dev, ocelot->dev);
  959. ocelot_port = netdev_priv(dev);
  960. ocelot_port->dev = dev;
  961. ocelot_port->ocelot = ocelot;
  962. ocelot_port->regs = regs;
  963. ocelot_port->chip_port = port;
  964. ocelot_port->phy = phy;
  965. INIT_LIST_HEAD(&ocelot_port->mc);
  966. ocelot->ports[port] = ocelot_port;
  967. dev->netdev_ops = &ocelot_port_netdev_ops;
  968. dev->ethtool_ops = &ocelot_ethtool_ops;
  969. dev->switchdev_ops = &ocelot_port_switchdev_ops;
  970. memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
  971. dev->dev_addr[ETH_ALEN - 1] += port;
  972. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
  973. ENTRYTYPE_LOCKED);
  974. err = register_netdev(dev);
  975. if (err) {
  976. dev_err(ocelot->dev, "register_netdev failed\n");
  977. goto err_register_netdev;
  978. }
  979. return 0;
  980. err_register_netdev:
  981. free_netdev(dev);
  982. return err;
  983. }
  984. EXPORT_SYMBOL(ocelot_probe_port);
  985. int ocelot_init(struct ocelot *ocelot)
  986. {
  987. u32 port;
  988. int i, cpu = ocelot->num_phys_ports;
  989. char queue_name[32];
  990. ocelot->stats = devm_kcalloc(ocelot->dev,
  991. ocelot->num_phys_ports * ocelot->num_stats,
  992. sizeof(u64), GFP_KERNEL);
  993. if (!ocelot->stats)
  994. return -ENOMEM;
  995. mutex_init(&ocelot->stats_lock);
  996. snprintf(queue_name, sizeof(queue_name), "%s-stats",
  997. dev_name(ocelot->dev));
  998. ocelot->stats_queue = create_singlethread_workqueue(queue_name);
  999. if (!ocelot->stats_queue)
  1000. return -ENOMEM;
  1001. ocelot_mact_init(ocelot);
  1002. ocelot_vlan_init(ocelot);
  1003. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1004. /* Clear all counters (5 groups) */
  1005. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
  1006. SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
  1007. SYS_STAT_CFG);
  1008. }
  1009. /* Only use S-Tag */
  1010. ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
  1011. /* Aggregation mode */
  1012. ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
  1013. ANA_AGGR_CFG_AC_DMAC_ENA |
  1014. ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
  1015. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
  1016. /* Set MAC age time to default value. The entry is aged after
  1017. * 2*AGE_PERIOD
  1018. */
  1019. ocelot_write(ocelot,
  1020. ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
  1021. ANA_AUTOAGE);
  1022. /* Disable learning for frames discarded by VLAN ingress filtering */
  1023. regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
  1024. /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
  1025. ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
  1026. SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
  1027. /* Setup flooding PGIDs */
  1028. ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
  1029. ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
  1030. ANA_FLOODING_FLD_UNICAST(PGID_UC),
  1031. ANA_FLOODING, 0);
  1032. ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
  1033. ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
  1034. ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
  1035. ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
  1036. ANA_FLOODING_IPMC);
  1037. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1038. /* Transmit the frame to the local port. */
  1039. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1040. /* Do not forward BPDU frames to the front ports. */
  1041. ocelot_write_gix(ocelot,
  1042. ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
  1043. ANA_PORT_CPU_FWD_BPDU_CFG,
  1044. port);
  1045. /* Ensure bridging is disabled */
  1046. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
  1047. }
  1048. /* Configure and enable the CPU port. */
  1049. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
  1050. ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
  1051. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
  1052. ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
  1053. ANA_PORT_PORT_CFG, cpu);
  1054. /* Allow broadcast MAC frames. */
  1055. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
  1056. u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
  1057. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  1058. }
  1059. ocelot_write_rix(ocelot,
  1060. ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
  1061. ANA_PGID_PGID, PGID_MC);
  1062. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
  1063. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
  1064. /* CPU port Injection/Extraction configuration */
  1065. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  1066. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  1067. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  1068. QSYS_SWITCH_PORT_MODE, cpu);
  1069. ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
  1070. SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
  1071. /* Allow manual injection via DEVCPU_QS registers, and byte swap these
  1072. * registers endianness.
  1073. */
  1074. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
  1075. QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
  1076. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
  1077. QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
  1078. ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
  1079. ANA_CPUQ_CFG_CPUQ_LRN(2) |
  1080. ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
  1081. ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
  1082. ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
  1083. ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
  1084. ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
  1085. ANA_CPUQ_CFG_CPUQ_IGMP(6) |
  1086. ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
  1087. for (i = 0; i < 16; i++)
  1088. ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
  1089. ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
  1090. ANA_CPUQ_8021_CFG, i);
  1091. INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
  1092. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  1093. OCELOT_STATS_CHECK_DELAY);
  1094. return 0;
  1095. }
  1096. EXPORT_SYMBOL(ocelot_init);
  1097. void ocelot_deinit(struct ocelot *ocelot)
  1098. {
  1099. destroy_workqueue(ocelot->stats_queue);
  1100. mutex_destroy(&ocelot->stats_lock);
  1101. }
  1102. EXPORT_SYMBOL(ocelot_deinit);
  1103. MODULE_LICENSE("Dual MIT/GPL");