ocelot.c 47 KB

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