ocelot.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  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 vlan table command to be completed, or timeout.
  116. * When the command read from ANA_TABLES_VLANACCESS is
  117. * VLANACCESS_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 int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
  128. {
  129. /* Select the VID to configure */
  130. ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
  131. ANA_TABLES_VLANTIDX);
  132. /* Set the vlan port members mask and issue a write command */
  133. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
  134. ANA_TABLES_VLANACCESS_CMD_WRITE,
  135. ANA_TABLES_VLANACCESS);
  136. return ocelot_vlant_wait_for_completion(ocelot);
  137. }
  138. static void ocelot_vlan_mode(struct ocelot_port *port,
  139. netdev_features_t features)
  140. {
  141. struct ocelot *ocelot = port->ocelot;
  142. u8 p = port->chip_port;
  143. u32 val;
  144. /* Filtering */
  145. val = ocelot_read(ocelot, ANA_VLANMASK);
  146. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  147. val |= BIT(p);
  148. else
  149. val &= ~BIT(p);
  150. ocelot_write(ocelot, val, ANA_VLANMASK);
  151. }
  152. static void ocelot_vlan_port_apply(struct ocelot *ocelot,
  153. struct ocelot_port *port)
  154. {
  155. u32 val;
  156. /* Ingress clasification (ANA_PORT_VLAN_CFG) */
  157. /* Default vlan to clasify for untagged frames (may be zero) */
  158. val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
  159. if (port->vlan_aware)
  160. val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  161. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
  162. ocelot_rmw_gix(ocelot, val,
  163. ANA_PORT_VLAN_CFG_VLAN_VID_M |
  164. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  165. ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
  166. ANA_PORT_VLAN_CFG, port->chip_port);
  167. /* Drop frames with multicast source address */
  168. val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
  169. if (port->vlan_aware && !port->vid)
  170. /* If port is vlan-aware and tagged, drop untagged and priority
  171. * tagged frames.
  172. */
  173. val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
  174. ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
  175. ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
  176. ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
  177. /* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
  178. val = REW_TAG_CFG_TAG_TPID_CFG(0);
  179. if (port->vlan_aware) {
  180. if (port->vid)
  181. /* Tag all frames except when VID == DEFAULT_VLAN */
  182. val |= REW_TAG_CFG_TAG_CFG(1);
  183. else
  184. /* Tag all frames */
  185. val |= REW_TAG_CFG_TAG_CFG(3);
  186. }
  187. ocelot_rmw_gix(ocelot, val,
  188. REW_TAG_CFG_TAG_TPID_CFG_M |
  189. REW_TAG_CFG_TAG_CFG_M,
  190. REW_TAG_CFG, port->chip_port);
  191. /* Set default VLAN and tag type to 8021Q. */
  192. val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
  193. REW_PORT_VLAN_CFG_PORT_VID(port->vid);
  194. ocelot_rmw_gix(ocelot, val,
  195. REW_PORT_VLAN_CFG_PORT_TPID_M |
  196. REW_PORT_VLAN_CFG_PORT_VID_M,
  197. REW_PORT_VLAN_CFG, port->chip_port);
  198. }
  199. static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
  200. bool untagged)
  201. {
  202. struct ocelot_port *port = netdev_priv(dev);
  203. struct ocelot *ocelot = port->ocelot;
  204. int ret;
  205. /* Add the port MAC address to with the right VLAN information */
  206. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
  207. ENTRYTYPE_LOCKED);
  208. /* Make the port a member of the VLAN */
  209. ocelot->vlan_mask[vid] |= BIT(port->chip_port);
  210. ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  211. if (ret)
  212. return ret;
  213. /* Default ingress vlan classification */
  214. if (pvid)
  215. port->pvid = vid;
  216. /* Untagged egress vlan clasification */
  217. if (untagged && port->vid != vid) {
  218. if (port->vid) {
  219. dev_err(ocelot->dev,
  220. "Port already has a native VLAN: %d\n",
  221. port->vid);
  222. return -EBUSY;
  223. }
  224. port->vid = vid;
  225. }
  226. ocelot_vlan_port_apply(ocelot, port);
  227. return 0;
  228. }
  229. static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
  230. {
  231. struct ocelot_port *port = netdev_priv(dev);
  232. struct ocelot *ocelot = port->ocelot;
  233. int ret;
  234. /* 8021q removes VID 0 on module unload for all interfaces
  235. * with VLAN filtering feature. We need to keep it to receive
  236. * untagged traffic.
  237. */
  238. if (vid == 0)
  239. return 0;
  240. /* Del the port MAC address to with the right VLAN information */
  241. ocelot_mact_forget(ocelot, dev->dev_addr, vid);
  242. /* Stop the port from being a member of the vlan */
  243. ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
  244. ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  245. if (ret)
  246. return ret;
  247. /* Ingress */
  248. if (port->pvid == vid)
  249. port->pvid = 0;
  250. /* Egress */
  251. if (port->vid == vid)
  252. port->vid = 0;
  253. ocelot_vlan_port_apply(ocelot, port);
  254. return 0;
  255. }
  256. static void ocelot_vlan_init(struct ocelot *ocelot)
  257. {
  258. u16 port, vid;
  259. /* Clear VLAN table, by default all ports are members of all VLANs */
  260. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
  261. ANA_TABLES_VLANACCESS);
  262. ocelot_vlant_wait_for_completion(ocelot);
  263. /* Configure the port VLAN memberships */
  264. for (vid = 1; vid < VLAN_N_VID; vid++) {
  265. ocelot->vlan_mask[vid] = 0;
  266. ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  267. }
  268. /* Because VLAN filtering is enabled, we need VID 0 to get untagged
  269. * traffic. It is added automatically if 8021q module is loaded, but
  270. * we can't rely on it since module may be not loaded.
  271. */
  272. ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
  273. ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
  274. /* Configure the CPU port to be VLAN aware */
  275. ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
  276. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  277. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
  278. ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
  279. /* Set vlan ingress filter mask to all ports but the CPU port by
  280. * default.
  281. */
  282. ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
  283. for (port = 0; port < ocelot->num_phys_ports; port++) {
  284. ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
  285. ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
  286. }
  287. }
  288. /* Watermark encode
  289. * Bit 8: Unit; 0:1, 1:16
  290. * Bit 7-0: Value to be multiplied with unit
  291. */
  292. static u16 ocelot_wm_enc(u16 value)
  293. {
  294. if (value >= BIT(8))
  295. return BIT(8) | (value / 16);
  296. return value;
  297. }
  298. static void ocelot_port_adjust_link(struct net_device *dev)
  299. {
  300. struct ocelot_port *port = netdev_priv(dev);
  301. struct ocelot *ocelot = port->ocelot;
  302. u8 p = port->chip_port;
  303. int speed, atop_wm, mode = 0;
  304. switch (dev->phydev->speed) {
  305. case SPEED_10:
  306. speed = OCELOT_SPEED_10;
  307. break;
  308. case SPEED_100:
  309. speed = OCELOT_SPEED_100;
  310. break;
  311. case SPEED_1000:
  312. speed = OCELOT_SPEED_1000;
  313. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  314. break;
  315. case SPEED_2500:
  316. speed = OCELOT_SPEED_2500;
  317. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  318. break;
  319. default:
  320. netdev_err(dev, "Unsupported PHY speed: %d\n",
  321. dev->phydev->speed);
  322. return;
  323. }
  324. phy_print_status(dev->phydev);
  325. if (!dev->phydev->link)
  326. return;
  327. /* Only full duplex supported for now */
  328. ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
  329. mode, DEV_MAC_MODE_CFG);
  330. /* Set MAC IFG Gaps
  331. * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
  332. * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
  333. */
  334. ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
  335. /* Load seed (0) and set MAC HDX late collision */
  336. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
  337. DEV_MAC_HDX_CFG_SEED_LOAD,
  338. DEV_MAC_HDX_CFG);
  339. mdelay(1);
  340. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
  341. DEV_MAC_HDX_CFG);
  342. /* Disable HDX fast control */
  343. ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
  344. /* SGMII only for now */
  345. ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
  346. ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
  347. /* Enable PCS */
  348. ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
  349. /* No aneg on SGMII */
  350. ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
  351. /* No loopback */
  352. ocelot_port_writel(port, 0, PCS1G_LB_CFG);
  353. /* Set Max Length and maximum tags allowed */
  354. ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
  355. ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
  356. DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
  357. DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
  358. DEV_MAC_TAGS_CFG);
  359. /* Enable MAC module */
  360. ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
  361. DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
  362. /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
  363. * reset */
  364. ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
  365. DEV_CLOCK_CFG);
  366. /* Set SMAC of Pause frame (00:00:00:00:00:00) */
  367. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
  368. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
  369. /* No PFC */
  370. ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
  371. ANA_PFC_PFC_CFG, p);
  372. /* Set Pause WM hysteresis
  373. * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  374. * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  375. */
  376. ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
  377. SYS_PAUSE_CFG_PAUSE_STOP(101) |
  378. SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
  379. /* Core: Enable port for frame transfer */
  380. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  381. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  382. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  383. QSYS_SWITCH_PORT_MODE, p);
  384. /* Flow control */
  385. ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
  386. SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
  387. SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
  388. SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
  389. SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
  390. SYS_MAC_FC_CFG, p);
  391. ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
  392. /* Tail dropping watermark */
  393. atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
  394. ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
  395. SYS_ATOP, p);
  396. ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
  397. }
  398. static int ocelot_port_open(struct net_device *dev)
  399. {
  400. struct ocelot_port *port = netdev_priv(dev);
  401. struct ocelot *ocelot = port->ocelot;
  402. int err;
  403. /* Enable receiving frames on the port, and activate auto-learning of
  404. * MAC addresses.
  405. */
  406. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
  407. ANA_PORT_PORT_CFG_RECV_ENA |
  408. ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
  409. ANA_PORT_PORT_CFG, port->chip_port);
  410. err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
  411. PHY_INTERFACE_MODE_NA);
  412. if (err) {
  413. netdev_err(dev, "Could not attach to PHY\n");
  414. return err;
  415. }
  416. dev->phydev = port->phy;
  417. phy_attached_info(port->phy);
  418. phy_start(port->phy);
  419. return 0;
  420. }
  421. static int ocelot_port_stop(struct net_device *dev)
  422. {
  423. struct ocelot_port *port = netdev_priv(dev);
  424. phy_disconnect(port->phy);
  425. dev->phydev = NULL;
  426. ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
  427. ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
  428. QSYS_SWITCH_PORT_MODE, port->chip_port);
  429. return 0;
  430. }
  431. /* Generate the IFH for frame injection
  432. *
  433. * The IFH is a 128bit-value
  434. * bit 127: bypass the analyzer processing
  435. * bit 56-67: destination mask
  436. * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
  437. * bit 20-27: cpu extraction queue mask
  438. * bit 16: tag type 0: C-tag, 1: S-tag
  439. * bit 0-11: VID
  440. */
  441. static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
  442. {
  443. ifh[0] = IFH_INJ_BYPASS;
  444. ifh[1] = (0xf00 & info->port) >> 8;
  445. ifh[2] = (0xff & info->port) << 24;
  446. ifh[3] = (info->tag_type << 16) | info->vid;
  447. return 0;
  448. }
  449. static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
  450. {
  451. struct ocelot_port *port = netdev_priv(dev);
  452. struct ocelot *ocelot = port->ocelot;
  453. u32 val, ifh[IFH_LEN];
  454. struct frame_info info = {};
  455. u8 grp = 0; /* Send everything on CPU group 0 */
  456. unsigned int i, count, last;
  457. val = ocelot_read(ocelot, QS_INJ_STATUS);
  458. if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
  459. (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
  460. return NETDEV_TX_BUSY;
  461. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  462. QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
  463. info.port = BIT(port->chip_port);
  464. info.tag_type = IFH_TAG_TYPE_C;
  465. info.vid = skb_vlan_tag_get(skb);
  466. ocelot_gen_ifh(ifh, &info);
  467. for (i = 0; i < IFH_LEN; i++)
  468. ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
  469. QS_INJ_WR, grp);
  470. count = (skb->len + 3) / 4;
  471. last = skb->len % 4;
  472. for (i = 0; i < count; i++) {
  473. ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
  474. }
  475. /* Add padding */
  476. while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
  477. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  478. i++;
  479. }
  480. /* Indicate EOF and valid bytes in last word */
  481. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  482. QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
  483. QS_INJ_CTRL_EOF,
  484. QS_INJ_CTRL, grp);
  485. /* Add dummy CRC */
  486. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  487. skb_tx_timestamp(skb);
  488. dev->stats.tx_packets++;
  489. dev->stats.tx_bytes += skb->len;
  490. dev_kfree_skb_any(skb);
  491. return NETDEV_TX_OK;
  492. }
  493. static void ocelot_mact_mc_reset(struct ocelot_port *port)
  494. {
  495. struct ocelot *ocelot = port->ocelot;
  496. struct netdev_hw_addr *ha, *n;
  497. /* Free and forget all the MAC addresses stored in the port private mc
  498. * list. These are mc addresses that were previously added by calling
  499. * ocelot_mact_mc_add().
  500. */
  501. list_for_each_entry_safe(ha, n, &port->mc, list) {
  502. ocelot_mact_forget(ocelot, ha->addr, port->pvid);
  503. list_del(&ha->list);
  504. kfree(ha);
  505. }
  506. }
  507. static int ocelot_mact_mc_add(struct ocelot_port *port,
  508. struct netdev_hw_addr *hw_addr)
  509. {
  510. struct ocelot *ocelot = port->ocelot;
  511. struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_ATOMIC);
  512. if (!ha)
  513. return -ENOMEM;
  514. memcpy(ha, hw_addr, sizeof(*ha));
  515. list_add_tail(&ha->list, &port->mc);
  516. ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
  517. ENTRYTYPE_LOCKED);
  518. return 0;
  519. }
  520. static void ocelot_set_rx_mode(struct net_device *dev)
  521. {
  522. struct ocelot_port *port = netdev_priv(dev);
  523. struct ocelot *ocelot = port->ocelot;
  524. struct netdev_hw_addr *ha;
  525. int i;
  526. u32 val;
  527. /* This doesn't handle promiscuous mode because the bridge core is
  528. * setting IFF_PROMISC on all slave interfaces and all frames would be
  529. * forwarded to the CPU port.
  530. */
  531. val = GENMASK(ocelot->num_phys_ports - 1, 0);
  532. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
  533. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  534. /* Handle the device multicast addresses. First remove all the
  535. * previously installed addresses and then add the latest ones to the
  536. * mac table.
  537. */
  538. ocelot_mact_mc_reset(port);
  539. netdev_for_each_mc_addr(ha, dev)
  540. ocelot_mact_mc_add(port, ha);
  541. }
  542. static int ocelot_port_get_phys_port_name(struct net_device *dev,
  543. char *buf, size_t len)
  544. {
  545. struct ocelot_port *port = netdev_priv(dev);
  546. int ret;
  547. ret = snprintf(buf, len, "p%d", port->chip_port);
  548. if (ret >= len)
  549. return -EINVAL;
  550. return 0;
  551. }
  552. static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
  553. {
  554. struct ocelot_port *port = netdev_priv(dev);
  555. struct ocelot *ocelot = port->ocelot;
  556. const struct sockaddr *addr = p;
  557. /* Learn the new net device MAC address in the mac table. */
  558. ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
  559. ENTRYTYPE_LOCKED);
  560. /* Then forget the previous one. */
  561. ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
  562. ether_addr_copy(dev->dev_addr, addr->sa_data);
  563. return 0;
  564. }
  565. static void ocelot_get_stats64(struct net_device *dev,
  566. struct rtnl_link_stats64 *stats)
  567. {
  568. struct ocelot_port *port = netdev_priv(dev);
  569. struct ocelot *ocelot = port->ocelot;
  570. /* Configure the port to read the stats from */
  571. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
  572. SYS_STAT_CFG);
  573. /* Get Rx stats */
  574. stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
  575. stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
  576. ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
  577. ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
  578. ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
  579. ocelot_read(ocelot, SYS_COUNT_RX_64) +
  580. ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
  581. ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
  582. ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
  583. ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
  584. ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
  585. stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
  586. stats->rx_dropped = dev->stats.rx_dropped;
  587. /* Get Tx stats */
  588. stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
  589. stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
  590. ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
  591. ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
  592. ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
  593. ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
  594. ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
  595. stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
  596. ocelot_read(ocelot, SYS_COUNT_TX_AGING);
  597. stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
  598. }
  599. static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  600. struct net_device *dev, const unsigned char *addr,
  601. u16 vid, u16 flags)
  602. {
  603. struct ocelot_port *port = netdev_priv(dev);
  604. struct ocelot *ocelot = port->ocelot;
  605. if (!vid) {
  606. if (!port->vlan_aware)
  607. /* If the bridge is not VLAN aware and no VID was
  608. * provided, set it to pvid to ensure the MAC entry
  609. * matches incoming untagged packets
  610. */
  611. vid = port->pvid;
  612. else
  613. /* If the bridge is VLAN aware a VID must be provided as
  614. * otherwise the learnt entry wouldn't match any frame.
  615. */
  616. return -EINVAL;
  617. }
  618. return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
  619. ENTRYTYPE_LOCKED);
  620. }
  621. static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  622. struct net_device *dev,
  623. const unsigned char *addr, u16 vid)
  624. {
  625. struct ocelot_port *port = netdev_priv(dev);
  626. struct ocelot *ocelot = port->ocelot;
  627. return ocelot_mact_forget(ocelot, addr, vid);
  628. }
  629. struct ocelot_dump_ctx {
  630. struct net_device *dev;
  631. struct sk_buff *skb;
  632. struct netlink_callback *cb;
  633. int idx;
  634. };
  635. static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
  636. struct ocelot_dump_ctx *dump)
  637. {
  638. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  639. u32 seq = dump->cb->nlh->nlmsg_seq;
  640. struct nlmsghdr *nlh;
  641. struct ndmsg *ndm;
  642. if (dump->idx < dump->cb->args[2])
  643. goto skip;
  644. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  645. sizeof(*ndm), NLM_F_MULTI);
  646. if (!nlh)
  647. return -EMSGSIZE;
  648. ndm = nlmsg_data(nlh);
  649. ndm->ndm_family = AF_BRIDGE;
  650. ndm->ndm_pad1 = 0;
  651. ndm->ndm_pad2 = 0;
  652. ndm->ndm_flags = NTF_SELF;
  653. ndm->ndm_type = 0;
  654. ndm->ndm_ifindex = dump->dev->ifindex;
  655. ndm->ndm_state = NUD_REACHABLE;
  656. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
  657. goto nla_put_failure;
  658. if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
  659. goto nla_put_failure;
  660. nlmsg_end(dump->skb, nlh);
  661. skip:
  662. dump->idx++;
  663. return 0;
  664. nla_put_failure:
  665. nlmsg_cancel(dump->skb, nlh);
  666. return -EMSGSIZE;
  667. }
  668. static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
  669. struct ocelot_mact_entry *entry)
  670. {
  671. struct ocelot *ocelot = port->ocelot;
  672. char mac[ETH_ALEN];
  673. u32 val, dst, macl, mach;
  674. /* Set row and column to read from */
  675. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
  676. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
  677. /* Issue a read command */
  678. ocelot_write(ocelot,
  679. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
  680. ANA_TABLES_MACACCESS);
  681. if (ocelot_mact_wait_for_completion(ocelot))
  682. return -ETIMEDOUT;
  683. /* Read the entry flags */
  684. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  685. if (!(val & ANA_TABLES_MACACCESS_VALID))
  686. return -EINVAL;
  687. /* If the entry read has another port configured as its destination,
  688. * do not report it.
  689. */
  690. dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
  691. if (dst != port->chip_port)
  692. return -EINVAL;
  693. /* Get the entry's MAC address and VLAN id */
  694. macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
  695. mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
  696. mac[0] = (mach >> 8) & 0xff;
  697. mac[1] = (mach >> 0) & 0xff;
  698. mac[2] = (macl >> 24) & 0xff;
  699. mac[3] = (macl >> 16) & 0xff;
  700. mac[4] = (macl >> 8) & 0xff;
  701. mac[5] = (macl >> 0) & 0xff;
  702. entry->vid = (mach >> 16) & 0xfff;
  703. ether_addr_copy(entry->mac, mac);
  704. return 0;
  705. }
  706. static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  707. struct net_device *dev,
  708. struct net_device *filter_dev, int *idx)
  709. {
  710. struct ocelot_port *port = netdev_priv(dev);
  711. int i, j, ret = 0;
  712. struct ocelot_dump_ctx dump = {
  713. .dev = dev,
  714. .skb = skb,
  715. .cb = cb,
  716. .idx = *idx,
  717. };
  718. struct ocelot_mact_entry entry;
  719. /* Loop through all the mac tables entries. There are 1024 rows of 4
  720. * entries.
  721. */
  722. for (i = 0; i < 1024; i++) {
  723. for (j = 0; j < 4; j++) {
  724. ret = ocelot_mact_read(port, i, j, &entry);
  725. /* If the entry is invalid (wrong port, invalid...),
  726. * skip it.
  727. */
  728. if (ret == -EINVAL)
  729. continue;
  730. else if (ret)
  731. goto end;
  732. ret = ocelot_fdb_do_dump(&entry, &dump);
  733. if (ret)
  734. goto end;
  735. }
  736. }
  737. end:
  738. *idx = dump.idx;
  739. return ret;
  740. }
  741. static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
  742. u16 vid)
  743. {
  744. return ocelot_vlan_vid_add(dev, vid, false, false);
  745. }
  746. static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  747. u16 vid)
  748. {
  749. return ocelot_vlan_vid_del(dev, vid);
  750. }
  751. static int ocelot_set_features(struct net_device *dev,
  752. netdev_features_t features)
  753. {
  754. struct ocelot_port *port = netdev_priv(dev);
  755. netdev_features_t changed = dev->features ^ features;
  756. if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
  757. ocelot_vlan_mode(port, features);
  758. return 0;
  759. }
  760. static const struct net_device_ops ocelot_port_netdev_ops = {
  761. .ndo_open = ocelot_port_open,
  762. .ndo_stop = ocelot_port_stop,
  763. .ndo_start_xmit = ocelot_port_xmit,
  764. .ndo_set_rx_mode = ocelot_set_rx_mode,
  765. .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
  766. .ndo_set_mac_address = ocelot_port_set_mac_address,
  767. .ndo_get_stats64 = ocelot_get_stats64,
  768. .ndo_fdb_add = ocelot_fdb_add,
  769. .ndo_fdb_del = ocelot_fdb_del,
  770. .ndo_fdb_dump = ocelot_fdb_dump,
  771. .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
  772. .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
  773. .ndo_set_features = ocelot_set_features,
  774. };
  775. static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  776. {
  777. struct ocelot_port *port = netdev_priv(netdev);
  778. struct ocelot *ocelot = port->ocelot;
  779. int i;
  780. if (sset != ETH_SS_STATS)
  781. return;
  782. for (i = 0; i < ocelot->num_stats; i++)
  783. memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
  784. ETH_GSTRING_LEN);
  785. }
  786. static void ocelot_check_stats(struct work_struct *work)
  787. {
  788. struct delayed_work *del_work = to_delayed_work(work);
  789. struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
  790. int i, j;
  791. mutex_lock(&ocelot->stats_lock);
  792. for (i = 0; i < ocelot->num_phys_ports; i++) {
  793. /* Configure the port to read the stats from */
  794. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
  795. for (j = 0; j < ocelot->num_stats; j++) {
  796. u32 val;
  797. unsigned int idx = i * ocelot->num_stats + j;
  798. val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
  799. ocelot->stats_layout[j].offset);
  800. if (val < (ocelot->stats[idx] & U32_MAX))
  801. ocelot->stats[idx] += (u64)1 << 32;
  802. ocelot->stats[idx] = (ocelot->stats[idx] &
  803. ~(u64)U32_MAX) + val;
  804. }
  805. }
  806. cancel_delayed_work(&ocelot->stats_work);
  807. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  808. OCELOT_STATS_CHECK_DELAY);
  809. mutex_unlock(&ocelot->stats_lock);
  810. }
  811. static void ocelot_get_ethtool_stats(struct net_device *dev,
  812. struct ethtool_stats *stats, u64 *data)
  813. {
  814. struct ocelot_port *port = netdev_priv(dev);
  815. struct ocelot *ocelot = port->ocelot;
  816. int i;
  817. /* check and update now */
  818. ocelot_check_stats(&ocelot->stats_work.work);
  819. /* Copy all counters */
  820. for (i = 0; i < ocelot->num_stats; i++)
  821. *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
  822. }
  823. static int ocelot_get_sset_count(struct net_device *dev, int sset)
  824. {
  825. struct ocelot_port *port = netdev_priv(dev);
  826. struct ocelot *ocelot = port->ocelot;
  827. if (sset != ETH_SS_STATS)
  828. return -EOPNOTSUPP;
  829. return ocelot->num_stats;
  830. }
  831. static const struct ethtool_ops ocelot_ethtool_ops = {
  832. .get_strings = ocelot_get_strings,
  833. .get_ethtool_stats = ocelot_get_ethtool_stats,
  834. .get_sset_count = ocelot_get_sset_count,
  835. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  836. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  837. };
  838. static int ocelot_port_attr_get(struct net_device *dev,
  839. struct switchdev_attr *attr)
  840. {
  841. struct ocelot_port *ocelot_port = netdev_priv(dev);
  842. struct ocelot *ocelot = ocelot_port->ocelot;
  843. switch (attr->id) {
  844. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  845. attr->u.ppid.id_len = sizeof(ocelot->base_mac);
  846. memcpy(&attr->u.ppid.id, &ocelot->base_mac,
  847. attr->u.ppid.id_len);
  848. break;
  849. default:
  850. return -EOPNOTSUPP;
  851. }
  852. return 0;
  853. }
  854. static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
  855. struct switchdev_trans *trans,
  856. u8 state)
  857. {
  858. struct ocelot *ocelot = ocelot_port->ocelot;
  859. u32 port_cfg;
  860. int port, i;
  861. if (switchdev_trans_ph_prepare(trans))
  862. return 0;
  863. if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
  864. return 0;
  865. port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
  866. ocelot_port->chip_port);
  867. switch (state) {
  868. case BR_STATE_FORWARDING:
  869. ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
  870. /* Fallthrough */
  871. case BR_STATE_LEARNING:
  872. port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
  873. break;
  874. default:
  875. port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
  876. ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
  877. break;
  878. }
  879. ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
  880. ocelot_port->chip_port);
  881. /* Apply FWD mask. The loop is needed to add/remove the current port as
  882. * a source for the other ports.
  883. */
  884. for (port = 0; port < ocelot->num_phys_ports; port++) {
  885. if (ocelot->bridge_fwd_mask & BIT(port)) {
  886. unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
  887. for (i = 0; i < ocelot->num_phys_ports; i++) {
  888. unsigned long bond_mask = ocelot->lags[i];
  889. if (!bond_mask)
  890. continue;
  891. if (bond_mask & BIT(port)) {
  892. mask &= ~bond_mask;
  893. break;
  894. }
  895. }
  896. ocelot_write_rix(ocelot,
  897. BIT(ocelot->num_phys_ports) | mask,
  898. ANA_PGID_PGID, PGID_SRC + port);
  899. } else {
  900. /* Only the CPU port, this is compatible with link
  901. * aggregation.
  902. */
  903. ocelot_write_rix(ocelot,
  904. BIT(ocelot->num_phys_ports),
  905. ANA_PGID_PGID, PGID_SRC + port);
  906. }
  907. }
  908. return 0;
  909. }
  910. static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
  911. unsigned long ageing_clock_t)
  912. {
  913. struct ocelot *ocelot = ocelot_port->ocelot;
  914. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
  915. u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
  916. ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
  917. ANA_AUTOAGE);
  918. }
  919. static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
  920. {
  921. struct ocelot *ocelot = port->ocelot;
  922. u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
  923. port->chip_port);
  924. if (mc)
  925. val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  926. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  927. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
  928. else
  929. val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  930. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  931. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
  932. ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
  933. }
  934. static int ocelot_port_attr_set(struct net_device *dev,
  935. const struct switchdev_attr *attr,
  936. struct switchdev_trans *trans)
  937. {
  938. struct ocelot_port *ocelot_port = netdev_priv(dev);
  939. int err = 0;
  940. switch (attr->id) {
  941. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  942. ocelot_port_attr_stp_state_set(ocelot_port, trans,
  943. attr->u.stp_state);
  944. break;
  945. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  946. ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
  947. break;
  948. case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
  949. ocelot_port->vlan_aware = attr->u.vlan_filtering;
  950. ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
  951. break;
  952. case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
  953. ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
  954. break;
  955. default:
  956. err = -EOPNOTSUPP;
  957. break;
  958. }
  959. return err;
  960. }
  961. static int ocelot_port_obj_add_vlan(struct net_device *dev,
  962. const struct switchdev_obj_port_vlan *vlan,
  963. struct switchdev_trans *trans)
  964. {
  965. int ret;
  966. u16 vid;
  967. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  968. ret = ocelot_vlan_vid_add(dev, vid,
  969. vlan->flags & BRIDGE_VLAN_INFO_PVID,
  970. vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
  971. if (ret)
  972. return ret;
  973. }
  974. return 0;
  975. }
  976. static int ocelot_port_vlan_del_vlan(struct net_device *dev,
  977. const struct switchdev_obj_port_vlan *vlan)
  978. {
  979. int ret;
  980. u16 vid;
  981. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  982. ret = ocelot_vlan_vid_del(dev, vid);
  983. if (ret)
  984. return ret;
  985. }
  986. return 0;
  987. }
  988. static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
  989. const unsigned char *addr,
  990. u16 vid)
  991. {
  992. struct ocelot_multicast *mc;
  993. list_for_each_entry(mc, &ocelot->multicast, list) {
  994. if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
  995. return mc;
  996. }
  997. return NULL;
  998. }
  999. static int ocelot_port_obj_add_mdb(struct net_device *dev,
  1000. const struct switchdev_obj_port_mdb *mdb,
  1001. struct switchdev_trans *trans)
  1002. {
  1003. struct ocelot_port *port = netdev_priv(dev);
  1004. struct ocelot *ocelot = port->ocelot;
  1005. struct ocelot_multicast *mc;
  1006. unsigned char addr[ETH_ALEN];
  1007. u16 vid = mdb->vid;
  1008. bool new = false;
  1009. if (!vid)
  1010. vid = port->pvid;
  1011. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1012. if (!mc) {
  1013. mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
  1014. if (!mc)
  1015. return -ENOMEM;
  1016. memcpy(mc->addr, mdb->addr, ETH_ALEN);
  1017. mc->vid = vid;
  1018. list_add_tail(&mc->list, &ocelot->multicast);
  1019. new = true;
  1020. }
  1021. memcpy(addr, mc->addr, ETH_ALEN);
  1022. addr[0] = 0;
  1023. if (!new) {
  1024. addr[2] = mc->ports << 0;
  1025. addr[1] = mc->ports << 8;
  1026. ocelot_mact_forget(ocelot, addr, vid);
  1027. }
  1028. mc->ports |= BIT(port->chip_port);
  1029. addr[2] = mc->ports << 0;
  1030. addr[1] = mc->ports << 8;
  1031. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  1032. }
  1033. static int ocelot_port_obj_del_mdb(struct net_device *dev,
  1034. const struct switchdev_obj_port_mdb *mdb)
  1035. {
  1036. struct ocelot_port *port = netdev_priv(dev);
  1037. struct ocelot *ocelot = port->ocelot;
  1038. struct ocelot_multicast *mc;
  1039. unsigned char addr[ETH_ALEN];
  1040. u16 vid = mdb->vid;
  1041. if (!vid)
  1042. vid = port->pvid;
  1043. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1044. if (!mc)
  1045. return -ENOENT;
  1046. memcpy(addr, mc->addr, ETH_ALEN);
  1047. addr[2] = mc->ports << 0;
  1048. addr[1] = mc->ports << 8;
  1049. addr[0] = 0;
  1050. ocelot_mact_forget(ocelot, addr, vid);
  1051. mc->ports &= ~BIT(port->chip_port);
  1052. if (!mc->ports) {
  1053. list_del(&mc->list);
  1054. devm_kfree(ocelot->dev, mc);
  1055. return 0;
  1056. }
  1057. addr[2] = mc->ports << 0;
  1058. addr[1] = mc->ports << 8;
  1059. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  1060. }
  1061. static int ocelot_port_obj_add(struct net_device *dev,
  1062. const struct switchdev_obj *obj,
  1063. struct switchdev_trans *trans)
  1064. {
  1065. int ret = 0;
  1066. switch (obj->id) {
  1067. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1068. ret = ocelot_port_obj_add_vlan(dev,
  1069. SWITCHDEV_OBJ_PORT_VLAN(obj),
  1070. trans);
  1071. break;
  1072. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1073. ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
  1074. trans);
  1075. break;
  1076. default:
  1077. return -EOPNOTSUPP;
  1078. }
  1079. return ret;
  1080. }
  1081. static int ocelot_port_obj_del(struct net_device *dev,
  1082. const struct switchdev_obj *obj)
  1083. {
  1084. int ret = 0;
  1085. switch (obj->id) {
  1086. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1087. ret = ocelot_port_vlan_del_vlan(dev,
  1088. SWITCHDEV_OBJ_PORT_VLAN(obj));
  1089. break;
  1090. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1091. ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
  1092. break;
  1093. default:
  1094. return -EOPNOTSUPP;
  1095. }
  1096. return ret;
  1097. }
  1098. static const struct switchdev_ops ocelot_port_switchdev_ops = {
  1099. .switchdev_port_attr_get = ocelot_port_attr_get,
  1100. .switchdev_port_attr_set = ocelot_port_attr_set,
  1101. .switchdev_port_obj_add = ocelot_port_obj_add,
  1102. .switchdev_port_obj_del = ocelot_port_obj_del,
  1103. };
  1104. static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
  1105. struct net_device *bridge)
  1106. {
  1107. struct ocelot *ocelot = ocelot_port->ocelot;
  1108. if (!ocelot->bridge_mask) {
  1109. ocelot->hw_bridge_dev = bridge;
  1110. } else {
  1111. if (ocelot->hw_bridge_dev != bridge)
  1112. /* This is adding the port to a second bridge, this is
  1113. * unsupported */
  1114. return -ENODEV;
  1115. }
  1116. ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
  1117. return 0;
  1118. }
  1119. static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
  1120. struct net_device *bridge)
  1121. {
  1122. struct ocelot *ocelot = ocelot_port->ocelot;
  1123. ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
  1124. if (!ocelot->bridge_mask)
  1125. ocelot->hw_bridge_dev = NULL;
  1126. /* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
  1127. ocelot_port->vlan_aware = 0;
  1128. ocelot_port->pvid = 0;
  1129. ocelot_port->vid = 0;
  1130. }
  1131. static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
  1132. {
  1133. int i, port, lag;
  1134. /* Reset destination and aggregation PGIDS */
  1135. for (port = 0; port < ocelot->num_phys_ports; port++)
  1136. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1137. for (i = PGID_AGGR; i < PGID_SRC; i++)
  1138. ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
  1139. ANA_PGID_PGID, i);
  1140. /* Now, set PGIDs for each LAG */
  1141. for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
  1142. unsigned long bond_mask;
  1143. int aggr_count = 0;
  1144. u8 aggr_idx[16];
  1145. bond_mask = ocelot->lags[lag];
  1146. if (!bond_mask)
  1147. continue;
  1148. for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
  1149. // Destination mask
  1150. ocelot_write_rix(ocelot, bond_mask,
  1151. ANA_PGID_PGID, port);
  1152. aggr_idx[aggr_count] = port;
  1153. aggr_count++;
  1154. }
  1155. for (i = PGID_AGGR; i < PGID_SRC; i++) {
  1156. u32 ac;
  1157. ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
  1158. ac &= ~bond_mask;
  1159. ac |= BIT(aggr_idx[i % aggr_count]);
  1160. ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
  1161. }
  1162. }
  1163. }
  1164. static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
  1165. {
  1166. unsigned long bond_mask = ocelot->lags[lag];
  1167. unsigned int p;
  1168. for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
  1169. u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
  1170. port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
  1171. /* Use lag port as logical port for port i */
  1172. ocelot_write_gix(ocelot, port_cfg |
  1173. ANA_PORT_PORT_CFG_PORTID_VAL(lag),
  1174. ANA_PORT_PORT_CFG, p);
  1175. }
  1176. }
  1177. static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
  1178. struct net_device *bond)
  1179. {
  1180. struct ocelot *ocelot = ocelot_port->ocelot;
  1181. int p = ocelot_port->chip_port;
  1182. int lag, lp;
  1183. struct net_device *ndev;
  1184. u32 bond_mask = 0;
  1185. rcu_read_lock();
  1186. for_each_netdev_in_bond_rcu(bond, ndev) {
  1187. struct ocelot_port *port = netdev_priv(ndev);
  1188. bond_mask |= BIT(port->chip_port);
  1189. }
  1190. rcu_read_unlock();
  1191. lp = __ffs(bond_mask);
  1192. /* If the new port is the lowest one, use it as the logical port from
  1193. * now on
  1194. */
  1195. if (p == lp) {
  1196. lag = p;
  1197. ocelot->lags[p] = bond_mask;
  1198. bond_mask &= ~BIT(p);
  1199. if (bond_mask) {
  1200. lp = __ffs(bond_mask);
  1201. ocelot->lags[lp] = 0;
  1202. }
  1203. } else {
  1204. lag = lp;
  1205. ocelot->lags[lp] |= BIT(p);
  1206. }
  1207. ocelot_setup_lag(ocelot, lag);
  1208. ocelot_set_aggr_pgids(ocelot);
  1209. return 0;
  1210. }
  1211. static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
  1212. struct net_device *bond)
  1213. {
  1214. struct ocelot *ocelot = ocelot_port->ocelot;
  1215. int p = ocelot_port->chip_port;
  1216. u32 port_cfg;
  1217. int i;
  1218. /* Remove port from any lag */
  1219. for (i = 0; i < ocelot->num_phys_ports; i++)
  1220. ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
  1221. /* if it was the logical port of the lag, move the lag config to the
  1222. * next port
  1223. */
  1224. if (ocelot->lags[p]) {
  1225. int n = __ffs(ocelot->lags[p]);
  1226. ocelot->lags[n] = ocelot->lags[p];
  1227. ocelot->lags[p] = 0;
  1228. ocelot_setup_lag(ocelot, n);
  1229. }
  1230. port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
  1231. port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
  1232. ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
  1233. ANA_PORT_PORT_CFG, p);
  1234. ocelot_set_aggr_pgids(ocelot);
  1235. }
  1236. /* Checks if the net_device instance given to us originate from our driver. */
  1237. static bool ocelot_netdevice_dev_check(const struct net_device *dev)
  1238. {
  1239. return dev->netdev_ops == &ocelot_port_netdev_ops;
  1240. }
  1241. static int ocelot_netdevice_port_event(struct net_device *dev,
  1242. unsigned long event,
  1243. struct netdev_notifier_changeupper_info *info)
  1244. {
  1245. struct ocelot_port *ocelot_port = netdev_priv(dev);
  1246. int err = 0;
  1247. switch (event) {
  1248. case NETDEV_CHANGEUPPER:
  1249. if (netif_is_bridge_master(info->upper_dev)) {
  1250. if (info->linking)
  1251. err = ocelot_port_bridge_join(ocelot_port,
  1252. info->upper_dev);
  1253. else
  1254. ocelot_port_bridge_leave(ocelot_port,
  1255. info->upper_dev);
  1256. ocelot_vlan_port_apply(ocelot_port->ocelot,
  1257. ocelot_port);
  1258. }
  1259. if (netif_is_lag_master(info->upper_dev)) {
  1260. if (info->linking)
  1261. err = ocelot_port_lag_join(ocelot_port,
  1262. info->upper_dev);
  1263. else
  1264. ocelot_port_lag_leave(ocelot_port,
  1265. info->upper_dev);
  1266. }
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. return err;
  1272. }
  1273. static int ocelot_netdevice_event(struct notifier_block *unused,
  1274. unsigned long event, void *ptr)
  1275. {
  1276. struct netdev_notifier_changeupper_info *info = ptr;
  1277. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1278. int ret = 0;
  1279. if (!ocelot_netdevice_dev_check(dev))
  1280. return 0;
  1281. if (event == NETDEV_PRECHANGEUPPER &&
  1282. netif_is_lag_master(info->upper_dev)) {
  1283. struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
  1284. struct netlink_ext_ack *extack;
  1285. if (lag_upper_info &&
  1286. lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  1287. extack = netdev_notifier_info_to_extack(&info->info);
  1288. NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
  1289. ret = -EINVAL;
  1290. goto notify;
  1291. }
  1292. }
  1293. if (netif_is_lag_master(dev)) {
  1294. struct net_device *slave;
  1295. struct list_head *iter;
  1296. netdev_for_each_lower_dev(dev, slave, iter) {
  1297. ret = ocelot_netdevice_port_event(slave, event, info);
  1298. if (ret)
  1299. goto notify;
  1300. }
  1301. } else {
  1302. ret = ocelot_netdevice_port_event(dev, event, info);
  1303. }
  1304. notify:
  1305. return notifier_from_errno(ret);
  1306. }
  1307. struct notifier_block ocelot_netdevice_nb __read_mostly = {
  1308. .notifier_call = ocelot_netdevice_event,
  1309. };
  1310. EXPORT_SYMBOL(ocelot_netdevice_nb);
  1311. int ocelot_probe_port(struct ocelot *ocelot, u8 port,
  1312. void __iomem *regs,
  1313. struct phy_device *phy)
  1314. {
  1315. struct ocelot_port *ocelot_port;
  1316. struct net_device *dev;
  1317. int err;
  1318. dev = alloc_etherdev(sizeof(struct ocelot_port));
  1319. if (!dev)
  1320. return -ENOMEM;
  1321. SET_NETDEV_DEV(dev, ocelot->dev);
  1322. ocelot_port = netdev_priv(dev);
  1323. ocelot_port->dev = dev;
  1324. ocelot_port->ocelot = ocelot;
  1325. ocelot_port->regs = regs;
  1326. ocelot_port->chip_port = port;
  1327. ocelot_port->phy = phy;
  1328. INIT_LIST_HEAD(&ocelot_port->mc);
  1329. ocelot->ports[port] = ocelot_port;
  1330. dev->netdev_ops = &ocelot_port_netdev_ops;
  1331. dev->ethtool_ops = &ocelot_ethtool_ops;
  1332. dev->switchdev_ops = &ocelot_port_switchdev_ops;
  1333. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1334. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1335. memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
  1336. dev->dev_addr[ETH_ALEN - 1] += port;
  1337. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
  1338. ENTRYTYPE_LOCKED);
  1339. err = register_netdev(dev);
  1340. if (err) {
  1341. dev_err(ocelot->dev, "register_netdev failed\n");
  1342. goto err_register_netdev;
  1343. }
  1344. /* Basic L2 initialization */
  1345. ocelot_vlan_port_apply(ocelot, ocelot_port);
  1346. return 0;
  1347. err_register_netdev:
  1348. free_netdev(dev);
  1349. return err;
  1350. }
  1351. EXPORT_SYMBOL(ocelot_probe_port);
  1352. int ocelot_init(struct ocelot *ocelot)
  1353. {
  1354. u32 port;
  1355. int i, cpu = ocelot->num_phys_ports;
  1356. char queue_name[32];
  1357. ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
  1358. sizeof(u32), GFP_KERNEL);
  1359. if (!ocelot->lags)
  1360. return -ENOMEM;
  1361. ocelot->stats = devm_kcalloc(ocelot->dev,
  1362. ocelot->num_phys_ports * ocelot->num_stats,
  1363. sizeof(u64), GFP_KERNEL);
  1364. if (!ocelot->stats)
  1365. return -ENOMEM;
  1366. mutex_init(&ocelot->stats_lock);
  1367. snprintf(queue_name, sizeof(queue_name), "%s-stats",
  1368. dev_name(ocelot->dev));
  1369. ocelot->stats_queue = create_singlethread_workqueue(queue_name);
  1370. if (!ocelot->stats_queue)
  1371. return -ENOMEM;
  1372. ocelot_mact_init(ocelot);
  1373. ocelot_vlan_init(ocelot);
  1374. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1375. /* Clear all counters (5 groups) */
  1376. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
  1377. SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
  1378. SYS_STAT_CFG);
  1379. }
  1380. /* Only use S-Tag */
  1381. ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
  1382. /* Aggregation mode */
  1383. ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
  1384. ANA_AGGR_CFG_AC_DMAC_ENA |
  1385. ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
  1386. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
  1387. /* Set MAC age time to default value. The entry is aged after
  1388. * 2*AGE_PERIOD
  1389. */
  1390. ocelot_write(ocelot,
  1391. ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
  1392. ANA_AUTOAGE);
  1393. /* Disable learning for frames discarded by VLAN ingress filtering */
  1394. regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
  1395. /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
  1396. ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
  1397. SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
  1398. /* Setup flooding PGIDs */
  1399. ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
  1400. ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
  1401. ANA_FLOODING_FLD_UNICAST(PGID_UC),
  1402. ANA_FLOODING, 0);
  1403. ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
  1404. ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
  1405. ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
  1406. ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
  1407. ANA_FLOODING_IPMC);
  1408. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1409. /* Transmit the frame to the local port. */
  1410. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1411. /* Do not forward BPDU frames to the front ports. */
  1412. ocelot_write_gix(ocelot,
  1413. ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
  1414. ANA_PORT_CPU_FWD_BPDU_CFG,
  1415. port);
  1416. /* Ensure bridging is disabled */
  1417. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
  1418. }
  1419. /* Configure and enable the CPU port. */
  1420. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
  1421. ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
  1422. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
  1423. ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
  1424. ANA_PORT_PORT_CFG, cpu);
  1425. /* Allow broadcast MAC frames. */
  1426. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
  1427. u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
  1428. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  1429. }
  1430. ocelot_write_rix(ocelot,
  1431. ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
  1432. ANA_PGID_PGID, PGID_MC);
  1433. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
  1434. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
  1435. /* CPU port Injection/Extraction configuration */
  1436. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  1437. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  1438. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  1439. QSYS_SWITCH_PORT_MODE, cpu);
  1440. ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
  1441. SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
  1442. /* Allow manual injection via DEVCPU_QS registers, and byte swap these
  1443. * registers endianness.
  1444. */
  1445. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
  1446. QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
  1447. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
  1448. QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
  1449. ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
  1450. ANA_CPUQ_CFG_CPUQ_LRN(2) |
  1451. ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
  1452. ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
  1453. ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
  1454. ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
  1455. ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
  1456. ANA_CPUQ_CFG_CPUQ_IGMP(6) |
  1457. ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
  1458. for (i = 0; i < 16; i++)
  1459. ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
  1460. ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
  1461. ANA_CPUQ_8021_CFG, i);
  1462. INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
  1463. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  1464. OCELOT_STATS_CHECK_DELAY);
  1465. return 0;
  1466. }
  1467. EXPORT_SYMBOL(ocelot_init);
  1468. void ocelot_deinit(struct ocelot *ocelot)
  1469. {
  1470. cancel_delayed_work(&ocelot->stats_work);
  1471. destroy_workqueue(ocelot->stats_queue);
  1472. mutex_destroy(&ocelot->stats_lock);
  1473. }
  1474. EXPORT_SYMBOL(ocelot_deinit);
  1475. MODULE_LICENSE("Dual MIT/GPL");