ocelot.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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/ptp_clock_kernel.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/iopoll.h>
  20. #include <net/arp.h>
  21. #include <net/netevent.h>
  22. #include <net/rtnetlink.h>
  23. #include <net/switchdev.h>
  24. #include "ocelot.h"
  25. #include "ocelot_ace.h"
  26. #define TABLE_UPDATE_SLEEP_US 10
  27. #define TABLE_UPDATE_TIMEOUT_US 100000
  28. /* MAC table entry types.
  29. * ENTRYTYPE_NORMAL is subject to aging.
  30. * ENTRYTYPE_LOCKED is not subject to aging.
  31. * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
  32. * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
  33. */
  34. enum macaccess_entry_type {
  35. ENTRYTYPE_NORMAL = 0,
  36. ENTRYTYPE_LOCKED,
  37. ENTRYTYPE_MACv4,
  38. ENTRYTYPE_MACv6,
  39. };
  40. struct ocelot_mact_entry {
  41. u8 mac[ETH_ALEN];
  42. u16 vid;
  43. enum macaccess_entry_type type;
  44. };
  45. static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
  46. {
  47. return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  48. }
  49. static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
  50. {
  51. u32 val;
  52. return readx_poll_timeout(ocelot_mact_read_macaccess,
  53. ocelot, val,
  54. (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
  55. MACACCESS_CMD_IDLE,
  56. TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
  57. }
  58. static void ocelot_mact_select(struct ocelot *ocelot,
  59. const unsigned char mac[ETH_ALEN],
  60. unsigned int vid)
  61. {
  62. u32 macl = 0, mach = 0;
  63. /* Set the MAC address to handle and the vlan associated in a format
  64. * understood by the hardware.
  65. */
  66. mach |= vid << 16;
  67. mach |= mac[0] << 8;
  68. mach |= mac[1] << 0;
  69. macl |= mac[2] << 24;
  70. macl |= mac[3] << 16;
  71. macl |= mac[4] << 8;
  72. macl |= mac[5] << 0;
  73. ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
  74. ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
  75. }
  76. static int ocelot_mact_learn(struct ocelot *ocelot, int port,
  77. const unsigned char mac[ETH_ALEN],
  78. unsigned int vid,
  79. enum macaccess_entry_type type)
  80. {
  81. ocelot_mact_select(ocelot, mac, vid);
  82. /* Issue a write command */
  83. ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
  84. ANA_TABLES_MACACCESS_DEST_IDX(port) |
  85. ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
  86. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
  87. ANA_TABLES_MACACCESS);
  88. return ocelot_mact_wait_for_completion(ocelot);
  89. }
  90. static int ocelot_mact_forget(struct ocelot *ocelot,
  91. const unsigned char mac[ETH_ALEN],
  92. unsigned int vid)
  93. {
  94. ocelot_mact_select(ocelot, mac, vid);
  95. /* Issue a forget command */
  96. ocelot_write(ocelot,
  97. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
  98. ANA_TABLES_MACACCESS);
  99. return ocelot_mact_wait_for_completion(ocelot);
  100. }
  101. static void ocelot_mact_init(struct ocelot *ocelot)
  102. {
  103. /* Configure the learning mode entries attributes:
  104. * - Do not copy the frame to the CPU extraction queues.
  105. * - Use the vlan and mac_cpoy for dmac lookup.
  106. */
  107. ocelot_rmw(ocelot, 0,
  108. ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
  109. | ANA_AGENCTRL_LEARN_FWD_KILL
  110. | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
  111. ANA_AGENCTRL);
  112. /* Clear the MAC table */
  113. ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
  114. }
  115. static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
  116. {
  117. ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
  118. ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
  119. ANA_PORT_VCAP_S2_CFG, port->chip_port);
  120. }
  121. static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
  122. {
  123. return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
  124. }
  125. static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
  126. {
  127. u32 val;
  128. return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
  129. ocelot,
  130. val,
  131. (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
  132. ANA_TABLES_VLANACCESS_CMD_IDLE,
  133. TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
  134. }
  135. static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
  136. {
  137. /* Select the VID to configure */
  138. ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
  139. ANA_TABLES_VLANTIDX);
  140. /* Set the vlan port members mask and issue a write command */
  141. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
  142. ANA_TABLES_VLANACCESS_CMD_WRITE,
  143. ANA_TABLES_VLANACCESS);
  144. return ocelot_vlant_wait_for_completion(ocelot);
  145. }
  146. static void ocelot_vlan_mode(struct ocelot_port *port,
  147. netdev_features_t features)
  148. {
  149. struct ocelot *ocelot = port->ocelot;
  150. u8 p = port->chip_port;
  151. u32 val;
  152. /* Filtering */
  153. val = ocelot_read(ocelot, ANA_VLANMASK);
  154. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  155. val |= BIT(p);
  156. else
  157. val &= ~BIT(p);
  158. ocelot_write(ocelot, val, ANA_VLANMASK);
  159. }
  160. static void ocelot_vlan_port_apply(struct ocelot *ocelot,
  161. struct ocelot_port *port)
  162. {
  163. u32 val;
  164. /* Ingress clasification (ANA_PORT_VLAN_CFG) */
  165. /* Default vlan to clasify for untagged frames (may be zero) */
  166. val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
  167. if (port->vlan_aware)
  168. val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  169. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
  170. ocelot_rmw_gix(ocelot, val,
  171. ANA_PORT_VLAN_CFG_VLAN_VID_M |
  172. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  173. ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
  174. ANA_PORT_VLAN_CFG, port->chip_port);
  175. /* Drop frames with multicast source address */
  176. val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
  177. if (port->vlan_aware && !port->vid)
  178. /* If port is vlan-aware and tagged, drop untagged and priority
  179. * tagged frames.
  180. */
  181. val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
  182. ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
  183. ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
  184. ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
  185. /* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
  186. val = REW_TAG_CFG_TAG_TPID_CFG(0);
  187. if (port->vlan_aware) {
  188. if (port->vid)
  189. /* Tag all frames except when VID == DEFAULT_VLAN */
  190. val |= REW_TAG_CFG_TAG_CFG(1);
  191. else
  192. /* Tag all frames */
  193. val |= REW_TAG_CFG_TAG_CFG(3);
  194. }
  195. ocelot_rmw_gix(ocelot, val,
  196. REW_TAG_CFG_TAG_TPID_CFG_M |
  197. REW_TAG_CFG_TAG_CFG_M,
  198. REW_TAG_CFG, port->chip_port);
  199. /* Set default VLAN and tag type to 8021Q. */
  200. val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
  201. REW_PORT_VLAN_CFG_PORT_VID(port->vid);
  202. ocelot_rmw_gix(ocelot, val,
  203. REW_PORT_VLAN_CFG_PORT_TPID_M |
  204. REW_PORT_VLAN_CFG_PORT_VID_M,
  205. REW_PORT_VLAN_CFG, port->chip_port);
  206. }
  207. static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
  208. bool untagged)
  209. {
  210. struct ocelot_port *port = netdev_priv(dev);
  211. struct ocelot *ocelot = port->ocelot;
  212. int ret;
  213. /* Add the port MAC address to with the right VLAN information */
  214. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
  215. ENTRYTYPE_LOCKED);
  216. /* Make the port a member of the VLAN */
  217. ocelot->vlan_mask[vid] |= BIT(port->chip_port);
  218. ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  219. if (ret)
  220. return ret;
  221. /* Default ingress vlan classification */
  222. if (pvid)
  223. port->pvid = vid;
  224. /* Untagged egress vlan clasification */
  225. if (untagged && port->vid != vid) {
  226. if (port->vid) {
  227. dev_err(ocelot->dev,
  228. "Port already has a native VLAN: %d\n",
  229. port->vid);
  230. return -EBUSY;
  231. }
  232. port->vid = vid;
  233. }
  234. ocelot_vlan_port_apply(ocelot, port);
  235. return 0;
  236. }
  237. static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
  238. {
  239. struct ocelot_port *port = netdev_priv(dev);
  240. struct ocelot *ocelot = port->ocelot;
  241. int ret;
  242. /* 8021q removes VID 0 on module unload for all interfaces
  243. * with VLAN filtering feature. We need to keep it to receive
  244. * untagged traffic.
  245. */
  246. if (vid == 0)
  247. return 0;
  248. /* Del the port MAC address to with the right VLAN information */
  249. ocelot_mact_forget(ocelot, dev->dev_addr, vid);
  250. /* Stop the port from being a member of the vlan */
  251. ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
  252. ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  253. if (ret)
  254. return ret;
  255. /* Ingress */
  256. if (port->pvid == vid)
  257. port->pvid = 0;
  258. /* Egress */
  259. if (port->vid == vid)
  260. port->vid = 0;
  261. ocelot_vlan_port_apply(ocelot, port);
  262. return 0;
  263. }
  264. static void ocelot_vlan_init(struct ocelot *ocelot)
  265. {
  266. u16 port, vid;
  267. /* Clear VLAN table, by default all ports are members of all VLANs */
  268. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
  269. ANA_TABLES_VLANACCESS);
  270. ocelot_vlant_wait_for_completion(ocelot);
  271. /* Configure the port VLAN memberships */
  272. for (vid = 1; vid < VLAN_N_VID; vid++) {
  273. ocelot->vlan_mask[vid] = 0;
  274. ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
  275. }
  276. /* Because VLAN filtering is enabled, we need VID 0 to get untagged
  277. * traffic. It is added automatically if 8021q module is loaded, but
  278. * we can't rely on it since module may be not loaded.
  279. */
  280. ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
  281. ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
  282. /* Configure the CPU port to be VLAN aware */
  283. ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
  284. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  285. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
  286. ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
  287. /* Set vlan ingress filter mask to all ports but the CPU port by
  288. * default.
  289. */
  290. ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
  291. for (port = 0; port < ocelot->num_phys_ports; port++) {
  292. ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
  293. ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
  294. }
  295. }
  296. /* Watermark encode
  297. * Bit 8: Unit; 0:1, 1:16
  298. * Bit 7-0: Value to be multiplied with unit
  299. */
  300. static u16 ocelot_wm_enc(u16 value)
  301. {
  302. if (value >= BIT(8))
  303. return BIT(8) | (value / 16);
  304. return value;
  305. }
  306. static void ocelot_port_adjust_link(struct net_device *dev)
  307. {
  308. struct ocelot_port *port = netdev_priv(dev);
  309. struct ocelot *ocelot = port->ocelot;
  310. u8 p = port->chip_port;
  311. int speed, atop_wm, mode = 0;
  312. switch (dev->phydev->speed) {
  313. case SPEED_10:
  314. speed = OCELOT_SPEED_10;
  315. break;
  316. case SPEED_100:
  317. speed = OCELOT_SPEED_100;
  318. break;
  319. case SPEED_1000:
  320. speed = OCELOT_SPEED_1000;
  321. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  322. break;
  323. case SPEED_2500:
  324. speed = OCELOT_SPEED_2500;
  325. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  326. break;
  327. default:
  328. netdev_err(dev, "Unsupported PHY speed: %d\n",
  329. dev->phydev->speed);
  330. return;
  331. }
  332. phy_print_status(dev->phydev);
  333. if (!dev->phydev->link)
  334. return;
  335. /* Only full duplex supported for now */
  336. ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
  337. mode, DEV_MAC_MODE_CFG);
  338. /* Set MAC IFG Gaps
  339. * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
  340. * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
  341. */
  342. ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
  343. /* Load seed (0) and set MAC HDX late collision */
  344. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
  345. DEV_MAC_HDX_CFG_SEED_LOAD,
  346. DEV_MAC_HDX_CFG);
  347. mdelay(1);
  348. ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
  349. DEV_MAC_HDX_CFG);
  350. /* Disable HDX fast control */
  351. ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
  352. /* SGMII only for now */
  353. ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
  354. ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
  355. /* Enable PCS */
  356. ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
  357. /* No aneg on SGMII */
  358. ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
  359. /* No loopback */
  360. ocelot_port_writel(port, 0, PCS1G_LB_CFG);
  361. /* Set Max Length and maximum tags allowed */
  362. ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
  363. ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
  364. DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
  365. DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
  366. DEV_MAC_TAGS_CFG);
  367. /* Enable MAC module */
  368. ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
  369. DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
  370. /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
  371. * reset */
  372. ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
  373. DEV_CLOCK_CFG);
  374. /* Set SMAC of Pause frame (00:00:00:00:00:00) */
  375. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
  376. ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
  377. /* No PFC */
  378. ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
  379. ANA_PFC_PFC_CFG, p);
  380. /* Set Pause WM hysteresis
  381. * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  382. * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
  383. */
  384. ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
  385. SYS_PAUSE_CFG_PAUSE_STOP(101) |
  386. SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
  387. /* Core: Enable port for frame transfer */
  388. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  389. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  390. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  391. QSYS_SWITCH_PORT_MODE, p);
  392. /* Flow control */
  393. ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
  394. SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
  395. SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
  396. SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
  397. SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
  398. SYS_MAC_FC_CFG, p);
  399. ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
  400. /* Tail dropping watermark */
  401. atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
  402. ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
  403. SYS_ATOP, p);
  404. ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
  405. }
  406. static int ocelot_port_open(struct net_device *dev)
  407. {
  408. struct ocelot_port *port = netdev_priv(dev);
  409. struct ocelot *ocelot = port->ocelot;
  410. int err;
  411. /* Enable receiving frames on the port, and activate auto-learning of
  412. * MAC addresses.
  413. */
  414. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
  415. ANA_PORT_PORT_CFG_RECV_ENA |
  416. ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
  417. ANA_PORT_PORT_CFG, port->chip_port);
  418. if (port->serdes) {
  419. err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
  420. port->phy_mode);
  421. if (err) {
  422. netdev_err(dev, "Could not set mode of SerDes\n");
  423. return err;
  424. }
  425. }
  426. err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
  427. port->phy_mode);
  428. if (err) {
  429. netdev_err(dev, "Could not attach to PHY\n");
  430. return err;
  431. }
  432. dev->phydev = port->phy;
  433. phy_attached_info(port->phy);
  434. phy_start(port->phy);
  435. return 0;
  436. }
  437. static int ocelot_port_stop(struct net_device *dev)
  438. {
  439. struct ocelot_port *port = netdev_priv(dev);
  440. phy_disconnect(port->phy);
  441. dev->phydev = NULL;
  442. ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
  443. ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
  444. QSYS_SWITCH_PORT_MODE, port->chip_port);
  445. return 0;
  446. }
  447. /* Generate the IFH for frame injection
  448. *
  449. * The IFH is a 128bit-value
  450. * bit 127: bypass the analyzer processing
  451. * bit 56-67: destination mask
  452. * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
  453. * bit 20-27: cpu extraction queue mask
  454. * bit 16: tag type 0: C-tag, 1: S-tag
  455. * bit 0-11: VID
  456. */
  457. static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
  458. {
  459. ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
  460. ifh[1] = (0xf00 & info->port) >> 8;
  461. ifh[2] = (0xff & info->port) << 24;
  462. ifh[3] = (info->tag_type << 16) | info->vid;
  463. return 0;
  464. }
  465. static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
  466. {
  467. struct skb_shared_info *shinfo = skb_shinfo(skb);
  468. struct ocelot_port *port = netdev_priv(dev);
  469. struct ocelot *ocelot = port->ocelot;
  470. u32 val, ifh[IFH_LEN];
  471. struct frame_info info = {};
  472. u8 grp = 0; /* Send everything on CPU group 0 */
  473. unsigned int i, count, last;
  474. val = ocelot_read(ocelot, QS_INJ_STATUS);
  475. if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
  476. (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
  477. return NETDEV_TX_BUSY;
  478. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  479. QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
  480. info.port = BIT(port->chip_port);
  481. info.tag_type = IFH_TAG_TYPE_C;
  482. info.vid = skb_vlan_tag_get(skb);
  483. /* Check if timestamping is needed */
  484. if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
  485. info.rew_op = port->ptp_cmd;
  486. if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
  487. info.rew_op |= (port->ts_id % 4) << 3;
  488. }
  489. ocelot_gen_ifh(ifh, &info);
  490. for (i = 0; i < IFH_LEN; i++)
  491. ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
  492. QS_INJ_WR, grp);
  493. count = (skb->len + 3) / 4;
  494. last = skb->len % 4;
  495. for (i = 0; i < count; i++) {
  496. ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
  497. }
  498. /* Add padding */
  499. while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
  500. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  501. i++;
  502. }
  503. /* Indicate EOF and valid bytes in last word */
  504. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  505. QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
  506. QS_INJ_CTRL_EOF,
  507. QS_INJ_CTRL, grp);
  508. /* Add dummy CRC */
  509. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  510. skb_tx_timestamp(skb);
  511. dev->stats.tx_packets++;
  512. dev->stats.tx_bytes += skb->len;
  513. if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
  514. port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
  515. struct ocelot_skb *oskb =
  516. kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
  517. if (unlikely(!oskb))
  518. goto out;
  519. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  520. oskb->skb = skb;
  521. oskb->id = port->ts_id % 4;
  522. port->ts_id++;
  523. list_add_tail(&oskb->head, &port->skbs);
  524. return NETDEV_TX_OK;
  525. }
  526. out:
  527. dev_kfree_skb_any(skb);
  528. return NETDEV_TX_OK;
  529. }
  530. void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
  531. {
  532. unsigned long flags;
  533. u32 val;
  534. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  535. /* Read current PTP time to get seconds */
  536. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  537. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  538. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
  539. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  540. ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  541. /* Read packet HW timestamp from FIFO */
  542. val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
  543. ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
  544. /* Sec has incremented since the ts was registered */
  545. if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
  546. ts->tv_sec--;
  547. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  548. }
  549. EXPORT_SYMBOL(ocelot_get_hwtimestamp);
  550. static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
  551. {
  552. struct ocelot_port *port = netdev_priv(dev);
  553. return ocelot_mact_forget(port->ocelot, addr, port->pvid);
  554. }
  555. static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
  556. {
  557. struct ocelot_port *port = netdev_priv(dev);
  558. return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
  559. ENTRYTYPE_LOCKED);
  560. }
  561. static void ocelot_set_rx_mode(struct net_device *dev)
  562. {
  563. struct ocelot_port *port = netdev_priv(dev);
  564. struct ocelot *ocelot = port->ocelot;
  565. int i;
  566. u32 val;
  567. /* This doesn't handle promiscuous mode because the bridge core is
  568. * setting IFF_PROMISC on all slave interfaces and all frames would be
  569. * forwarded to the CPU port.
  570. */
  571. val = GENMASK(ocelot->num_phys_ports - 1, 0);
  572. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
  573. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  574. __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
  575. }
  576. static int ocelot_port_get_phys_port_name(struct net_device *dev,
  577. char *buf, size_t len)
  578. {
  579. struct ocelot_port *port = netdev_priv(dev);
  580. int ret;
  581. ret = snprintf(buf, len, "p%d", port->chip_port);
  582. if (ret >= len)
  583. return -EINVAL;
  584. return 0;
  585. }
  586. static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
  587. {
  588. struct ocelot_port *port = netdev_priv(dev);
  589. struct ocelot *ocelot = port->ocelot;
  590. const struct sockaddr *addr = p;
  591. /* Learn the new net device MAC address in the mac table. */
  592. ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
  593. ENTRYTYPE_LOCKED);
  594. /* Then forget the previous one. */
  595. ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
  596. ether_addr_copy(dev->dev_addr, addr->sa_data);
  597. return 0;
  598. }
  599. static void ocelot_get_stats64(struct net_device *dev,
  600. struct rtnl_link_stats64 *stats)
  601. {
  602. struct ocelot_port *port = netdev_priv(dev);
  603. struct ocelot *ocelot = port->ocelot;
  604. /* Configure the port to read the stats from */
  605. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
  606. SYS_STAT_CFG);
  607. /* Get Rx stats */
  608. stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
  609. stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
  610. ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
  611. ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
  612. ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
  613. ocelot_read(ocelot, SYS_COUNT_RX_64) +
  614. ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
  615. ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
  616. ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
  617. ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
  618. ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
  619. stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
  620. stats->rx_dropped = dev->stats.rx_dropped;
  621. /* Get Tx stats */
  622. stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
  623. stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
  624. ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
  625. ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
  626. ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
  627. ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
  628. ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
  629. stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
  630. ocelot_read(ocelot, SYS_COUNT_TX_AGING);
  631. stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
  632. }
  633. static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  634. struct net_device *dev, const unsigned char *addr,
  635. u16 vid, u16 flags,
  636. struct netlink_ext_ack *extack)
  637. {
  638. struct ocelot_port *port = netdev_priv(dev);
  639. struct ocelot *ocelot = port->ocelot;
  640. if (!vid) {
  641. if (!port->vlan_aware)
  642. /* If the bridge is not VLAN aware and no VID was
  643. * provided, set it to pvid to ensure the MAC entry
  644. * matches incoming untagged packets
  645. */
  646. vid = port->pvid;
  647. else
  648. /* If the bridge is VLAN aware a VID must be provided as
  649. * otherwise the learnt entry wouldn't match any frame.
  650. */
  651. return -EINVAL;
  652. }
  653. return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
  654. ENTRYTYPE_LOCKED);
  655. }
  656. static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  657. struct net_device *dev,
  658. const unsigned char *addr, u16 vid)
  659. {
  660. struct ocelot_port *port = netdev_priv(dev);
  661. struct ocelot *ocelot = port->ocelot;
  662. return ocelot_mact_forget(ocelot, addr, vid);
  663. }
  664. struct ocelot_dump_ctx {
  665. struct net_device *dev;
  666. struct sk_buff *skb;
  667. struct netlink_callback *cb;
  668. int idx;
  669. };
  670. static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
  671. struct ocelot_dump_ctx *dump)
  672. {
  673. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  674. u32 seq = dump->cb->nlh->nlmsg_seq;
  675. struct nlmsghdr *nlh;
  676. struct ndmsg *ndm;
  677. if (dump->idx < dump->cb->args[2])
  678. goto skip;
  679. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  680. sizeof(*ndm), NLM_F_MULTI);
  681. if (!nlh)
  682. return -EMSGSIZE;
  683. ndm = nlmsg_data(nlh);
  684. ndm->ndm_family = AF_BRIDGE;
  685. ndm->ndm_pad1 = 0;
  686. ndm->ndm_pad2 = 0;
  687. ndm->ndm_flags = NTF_SELF;
  688. ndm->ndm_type = 0;
  689. ndm->ndm_ifindex = dump->dev->ifindex;
  690. ndm->ndm_state = NUD_REACHABLE;
  691. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
  692. goto nla_put_failure;
  693. if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
  694. goto nla_put_failure;
  695. nlmsg_end(dump->skb, nlh);
  696. skip:
  697. dump->idx++;
  698. return 0;
  699. nla_put_failure:
  700. nlmsg_cancel(dump->skb, nlh);
  701. return -EMSGSIZE;
  702. }
  703. static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
  704. struct ocelot_mact_entry *entry)
  705. {
  706. struct ocelot *ocelot = port->ocelot;
  707. char mac[ETH_ALEN];
  708. u32 val, dst, macl, mach;
  709. /* Set row and column to read from */
  710. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
  711. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
  712. /* Issue a read command */
  713. ocelot_write(ocelot,
  714. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
  715. ANA_TABLES_MACACCESS);
  716. if (ocelot_mact_wait_for_completion(ocelot))
  717. return -ETIMEDOUT;
  718. /* Read the entry flags */
  719. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  720. if (!(val & ANA_TABLES_MACACCESS_VALID))
  721. return -EINVAL;
  722. /* If the entry read has another port configured as its destination,
  723. * do not report it.
  724. */
  725. dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
  726. if (dst != port->chip_port)
  727. return -EINVAL;
  728. /* Get the entry's MAC address and VLAN id */
  729. macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
  730. mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
  731. mac[0] = (mach >> 8) & 0xff;
  732. mac[1] = (mach >> 0) & 0xff;
  733. mac[2] = (macl >> 24) & 0xff;
  734. mac[3] = (macl >> 16) & 0xff;
  735. mac[4] = (macl >> 8) & 0xff;
  736. mac[5] = (macl >> 0) & 0xff;
  737. entry->vid = (mach >> 16) & 0xfff;
  738. ether_addr_copy(entry->mac, mac);
  739. return 0;
  740. }
  741. static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  742. struct net_device *dev,
  743. struct net_device *filter_dev, int *idx)
  744. {
  745. struct ocelot_port *port = netdev_priv(dev);
  746. int i, j, ret = 0;
  747. struct ocelot_dump_ctx dump = {
  748. .dev = dev,
  749. .skb = skb,
  750. .cb = cb,
  751. .idx = *idx,
  752. };
  753. struct ocelot_mact_entry entry;
  754. /* Loop through all the mac tables entries. There are 1024 rows of 4
  755. * entries.
  756. */
  757. for (i = 0; i < 1024; i++) {
  758. for (j = 0; j < 4; j++) {
  759. ret = ocelot_mact_read(port, i, j, &entry);
  760. /* If the entry is invalid (wrong port, invalid...),
  761. * skip it.
  762. */
  763. if (ret == -EINVAL)
  764. continue;
  765. else if (ret)
  766. goto end;
  767. ret = ocelot_fdb_do_dump(&entry, &dump);
  768. if (ret)
  769. goto end;
  770. }
  771. }
  772. end:
  773. *idx = dump.idx;
  774. return ret;
  775. }
  776. static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
  777. u16 vid)
  778. {
  779. return ocelot_vlan_vid_add(dev, vid, false, false);
  780. }
  781. static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  782. u16 vid)
  783. {
  784. return ocelot_vlan_vid_del(dev, vid);
  785. }
  786. static int ocelot_set_features(struct net_device *dev,
  787. netdev_features_t features)
  788. {
  789. struct ocelot_port *port = netdev_priv(dev);
  790. netdev_features_t changed = dev->features ^ features;
  791. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
  792. port->tc.offload_cnt) {
  793. netdev_err(dev,
  794. "Cannot disable HW TC offload while offloads active\n");
  795. return -EBUSY;
  796. }
  797. if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
  798. ocelot_vlan_mode(port, features);
  799. return 0;
  800. }
  801. static int ocelot_get_port_parent_id(struct net_device *dev,
  802. struct netdev_phys_item_id *ppid)
  803. {
  804. struct ocelot_port *ocelot_port = netdev_priv(dev);
  805. struct ocelot *ocelot = ocelot_port->ocelot;
  806. ppid->id_len = sizeof(ocelot->base_mac);
  807. memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
  808. return 0;
  809. }
  810. static int ocelot_hwstamp_get(struct ocelot_port *port, struct ifreq *ifr)
  811. {
  812. struct ocelot *ocelot = port->ocelot;
  813. return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
  814. sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
  815. }
  816. static int ocelot_hwstamp_set(struct ocelot_port *port, struct ifreq *ifr)
  817. {
  818. struct ocelot *ocelot = port->ocelot;
  819. struct hwtstamp_config cfg;
  820. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  821. return -EFAULT;
  822. /* reserved for future extensions */
  823. if (cfg.flags)
  824. return -EINVAL;
  825. /* Tx type sanity check */
  826. switch (cfg.tx_type) {
  827. case HWTSTAMP_TX_ON:
  828. port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
  829. break;
  830. case HWTSTAMP_TX_ONESTEP_SYNC:
  831. /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
  832. * need to update the origin time.
  833. */
  834. port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
  835. break;
  836. case HWTSTAMP_TX_OFF:
  837. port->ptp_cmd = 0;
  838. break;
  839. default:
  840. return -ERANGE;
  841. }
  842. mutex_lock(&ocelot->ptp_lock);
  843. switch (cfg.rx_filter) {
  844. case HWTSTAMP_FILTER_NONE:
  845. break;
  846. case HWTSTAMP_FILTER_ALL:
  847. case HWTSTAMP_FILTER_SOME:
  848. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  849. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  850. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  851. case HWTSTAMP_FILTER_NTP_ALL:
  852. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  853. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  854. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  855. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  856. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  857. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  858. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  859. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  860. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  861. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  862. break;
  863. default:
  864. mutex_unlock(&ocelot->ptp_lock);
  865. return -ERANGE;
  866. }
  867. /* Commit back the result & save it */
  868. memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
  869. mutex_unlock(&ocelot->ptp_lock);
  870. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  871. }
  872. static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  873. {
  874. struct ocelot_port *port = netdev_priv(dev);
  875. struct ocelot *ocelot = port->ocelot;
  876. /* The function is only used for PTP operations for now */
  877. if (!ocelot->ptp)
  878. return -EOPNOTSUPP;
  879. switch (cmd) {
  880. case SIOCSHWTSTAMP:
  881. return ocelot_hwstamp_set(port, ifr);
  882. case SIOCGHWTSTAMP:
  883. return ocelot_hwstamp_get(port, ifr);
  884. default:
  885. return -EOPNOTSUPP;
  886. }
  887. }
  888. static const struct net_device_ops ocelot_port_netdev_ops = {
  889. .ndo_open = ocelot_port_open,
  890. .ndo_stop = ocelot_port_stop,
  891. .ndo_start_xmit = ocelot_port_xmit,
  892. .ndo_set_rx_mode = ocelot_set_rx_mode,
  893. .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
  894. .ndo_set_mac_address = ocelot_port_set_mac_address,
  895. .ndo_get_stats64 = ocelot_get_stats64,
  896. .ndo_fdb_add = ocelot_fdb_add,
  897. .ndo_fdb_del = ocelot_fdb_del,
  898. .ndo_fdb_dump = ocelot_fdb_dump,
  899. .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
  900. .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
  901. .ndo_set_features = ocelot_set_features,
  902. .ndo_get_port_parent_id = ocelot_get_port_parent_id,
  903. .ndo_setup_tc = ocelot_setup_tc,
  904. .ndo_do_ioctl = ocelot_ioctl,
  905. };
  906. static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
  907. {
  908. struct ocelot_port *port = netdev_priv(netdev);
  909. struct ocelot *ocelot = port->ocelot;
  910. int i;
  911. if (sset != ETH_SS_STATS)
  912. return;
  913. for (i = 0; i < ocelot->num_stats; i++)
  914. memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
  915. ETH_GSTRING_LEN);
  916. }
  917. static void ocelot_update_stats(struct ocelot *ocelot)
  918. {
  919. int i, j;
  920. mutex_lock(&ocelot->stats_lock);
  921. for (i = 0; i < ocelot->num_phys_ports; i++) {
  922. /* Configure the port to read the stats from */
  923. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
  924. for (j = 0; j < ocelot->num_stats; j++) {
  925. u32 val;
  926. unsigned int idx = i * ocelot->num_stats + j;
  927. val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
  928. ocelot->stats_layout[j].offset);
  929. if (val < (ocelot->stats[idx] & U32_MAX))
  930. ocelot->stats[idx] += (u64)1 << 32;
  931. ocelot->stats[idx] = (ocelot->stats[idx] &
  932. ~(u64)U32_MAX) + val;
  933. }
  934. }
  935. mutex_unlock(&ocelot->stats_lock);
  936. }
  937. static void ocelot_check_stats_work(struct work_struct *work)
  938. {
  939. struct delayed_work *del_work = to_delayed_work(work);
  940. struct ocelot *ocelot = container_of(del_work, struct ocelot,
  941. stats_work);
  942. ocelot_update_stats(ocelot);
  943. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  944. OCELOT_STATS_CHECK_DELAY);
  945. }
  946. static void ocelot_get_ethtool_stats(struct net_device *dev,
  947. struct ethtool_stats *stats, u64 *data)
  948. {
  949. struct ocelot_port *port = netdev_priv(dev);
  950. struct ocelot *ocelot = port->ocelot;
  951. int i;
  952. /* check and update now */
  953. ocelot_update_stats(ocelot);
  954. /* Copy all counters */
  955. for (i = 0; i < ocelot->num_stats; i++)
  956. *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
  957. }
  958. static int ocelot_get_sset_count(struct net_device *dev, int sset)
  959. {
  960. struct ocelot_port *port = netdev_priv(dev);
  961. struct ocelot *ocelot = port->ocelot;
  962. if (sset != ETH_SS_STATS)
  963. return -EOPNOTSUPP;
  964. return ocelot->num_stats;
  965. }
  966. static int ocelot_get_ts_info(struct net_device *dev,
  967. struct ethtool_ts_info *info)
  968. {
  969. struct ocelot_port *ocelot_port = netdev_priv(dev);
  970. struct ocelot *ocelot = ocelot_port->ocelot;
  971. if (!ocelot->ptp)
  972. return ethtool_op_get_ts_info(dev, info);
  973. info->phc_index = ocelot->ptp_clock ?
  974. ptp_clock_index(ocelot->ptp_clock) : -1;
  975. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
  976. SOF_TIMESTAMPING_RX_SOFTWARE |
  977. SOF_TIMESTAMPING_SOFTWARE |
  978. SOF_TIMESTAMPING_TX_HARDWARE |
  979. SOF_TIMESTAMPING_RX_HARDWARE |
  980. SOF_TIMESTAMPING_RAW_HARDWARE;
  981. info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
  982. BIT(HWTSTAMP_TX_ONESTEP_SYNC);
  983. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
  984. return 0;
  985. }
  986. static const struct ethtool_ops ocelot_ethtool_ops = {
  987. .get_strings = ocelot_get_strings,
  988. .get_ethtool_stats = ocelot_get_ethtool_stats,
  989. .get_sset_count = ocelot_get_sset_count,
  990. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  991. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  992. .get_ts_info = ocelot_get_ts_info,
  993. };
  994. static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
  995. struct switchdev_trans *trans,
  996. u8 state)
  997. {
  998. struct ocelot *ocelot = ocelot_port->ocelot;
  999. u32 port_cfg;
  1000. int port, i;
  1001. if (switchdev_trans_ph_prepare(trans))
  1002. return 0;
  1003. if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
  1004. return 0;
  1005. port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
  1006. ocelot_port->chip_port);
  1007. switch (state) {
  1008. case BR_STATE_FORWARDING:
  1009. ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
  1010. /* Fallthrough */
  1011. case BR_STATE_LEARNING:
  1012. port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
  1013. break;
  1014. default:
  1015. port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
  1016. ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
  1017. break;
  1018. }
  1019. ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
  1020. ocelot_port->chip_port);
  1021. /* Apply FWD mask. The loop is needed to add/remove the current port as
  1022. * a source for the other ports.
  1023. */
  1024. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1025. if (ocelot->bridge_fwd_mask & BIT(port)) {
  1026. unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
  1027. for (i = 0; i < ocelot->num_phys_ports; i++) {
  1028. unsigned long bond_mask = ocelot->lags[i];
  1029. if (!bond_mask)
  1030. continue;
  1031. if (bond_mask & BIT(port)) {
  1032. mask &= ~bond_mask;
  1033. break;
  1034. }
  1035. }
  1036. ocelot_write_rix(ocelot,
  1037. BIT(ocelot->num_phys_ports) | mask,
  1038. ANA_PGID_PGID, PGID_SRC + port);
  1039. } else {
  1040. /* Only the CPU port, this is compatible with link
  1041. * aggregation.
  1042. */
  1043. ocelot_write_rix(ocelot,
  1044. BIT(ocelot->num_phys_ports),
  1045. ANA_PGID_PGID, PGID_SRC + port);
  1046. }
  1047. }
  1048. return 0;
  1049. }
  1050. static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
  1051. unsigned long ageing_clock_t)
  1052. {
  1053. struct ocelot *ocelot = ocelot_port->ocelot;
  1054. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
  1055. u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
  1056. ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
  1057. ANA_AUTOAGE);
  1058. }
  1059. static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
  1060. {
  1061. struct ocelot *ocelot = port->ocelot;
  1062. u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
  1063. port->chip_port);
  1064. if (mc)
  1065. val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  1066. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  1067. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
  1068. else
  1069. val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  1070. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  1071. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
  1072. ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
  1073. }
  1074. static int ocelot_port_attr_set(struct net_device *dev,
  1075. const struct switchdev_attr *attr,
  1076. struct switchdev_trans *trans)
  1077. {
  1078. struct ocelot_port *ocelot_port = netdev_priv(dev);
  1079. int err = 0;
  1080. switch (attr->id) {
  1081. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  1082. ocelot_port_attr_stp_state_set(ocelot_port, trans,
  1083. attr->u.stp_state);
  1084. break;
  1085. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  1086. ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
  1087. break;
  1088. case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
  1089. ocelot_port->vlan_aware = attr->u.vlan_filtering;
  1090. ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
  1091. break;
  1092. case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
  1093. ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
  1094. break;
  1095. default:
  1096. err = -EOPNOTSUPP;
  1097. break;
  1098. }
  1099. return err;
  1100. }
  1101. static int ocelot_port_obj_add_vlan(struct net_device *dev,
  1102. const struct switchdev_obj_port_vlan *vlan,
  1103. struct switchdev_trans *trans)
  1104. {
  1105. int ret;
  1106. u16 vid;
  1107. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  1108. ret = ocelot_vlan_vid_add(dev, vid,
  1109. vlan->flags & BRIDGE_VLAN_INFO_PVID,
  1110. vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
  1111. if (ret)
  1112. return ret;
  1113. }
  1114. return 0;
  1115. }
  1116. static int ocelot_port_vlan_del_vlan(struct net_device *dev,
  1117. const struct switchdev_obj_port_vlan *vlan)
  1118. {
  1119. int ret;
  1120. u16 vid;
  1121. for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
  1122. ret = ocelot_vlan_vid_del(dev, vid);
  1123. if (ret)
  1124. return ret;
  1125. }
  1126. return 0;
  1127. }
  1128. static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
  1129. const unsigned char *addr,
  1130. u16 vid)
  1131. {
  1132. struct ocelot_multicast *mc;
  1133. list_for_each_entry(mc, &ocelot->multicast, list) {
  1134. if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
  1135. return mc;
  1136. }
  1137. return NULL;
  1138. }
  1139. static int ocelot_port_obj_add_mdb(struct net_device *dev,
  1140. const struct switchdev_obj_port_mdb *mdb,
  1141. struct switchdev_trans *trans)
  1142. {
  1143. struct ocelot_port *port = netdev_priv(dev);
  1144. struct ocelot *ocelot = port->ocelot;
  1145. struct ocelot_multicast *mc;
  1146. unsigned char addr[ETH_ALEN];
  1147. u16 vid = mdb->vid;
  1148. bool new = false;
  1149. if (!vid)
  1150. vid = port->pvid;
  1151. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1152. if (!mc) {
  1153. mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
  1154. if (!mc)
  1155. return -ENOMEM;
  1156. memcpy(mc->addr, mdb->addr, ETH_ALEN);
  1157. mc->vid = vid;
  1158. list_add_tail(&mc->list, &ocelot->multicast);
  1159. new = true;
  1160. }
  1161. memcpy(addr, mc->addr, ETH_ALEN);
  1162. addr[0] = 0;
  1163. if (!new) {
  1164. addr[2] = mc->ports << 0;
  1165. addr[1] = mc->ports << 8;
  1166. ocelot_mact_forget(ocelot, addr, vid);
  1167. }
  1168. mc->ports |= BIT(port->chip_port);
  1169. addr[2] = mc->ports << 0;
  1170. addr[1] = mc->ports << 8;
  1171. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  1172. }
  1173. static int ocelot_port_obj_del_mdb(struct net_device *dev,
  1174. const struct switchdev_obj_port_mdb *mdb)
  1175. {
  1176. struct ocelot_port *port = netdev_priv(dev);
  1177. struct ocelot *ocelot = port->ocelot;
  1178. struct ocelot_multicast *mc;
  1179. unsigned char addr[ETH_ALEN];
  1180. u16 vid = mdb->vid;
  1181. if (!vid)
  1182. vid = port->pvid;
  1183. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1184. if (!mc)
  1185. return -ENOENT;
  1186. memcpy(addr, mc->addr, ETH_ALEN);
  1187. addr[2] = mc->ports << 0;
  1188. addr[1] = mc->ports << 8;
  1189. addr[0] = 0;
  1190. ocelot_mact_forget(ocelot, addr, vid);
  1191. mc->ports &= ~BIT(port->chip_port);
  1192. if (!mc->ports) {
  1193. list_del(&mc->list);
  1194. devm_kfree(ocelot->dev, mc);
  1195. return 0;
  1196. }
  1197. addr[2] = mc->ports << 0;
  1198. addr[1] = mc->ports << 8;
  1199. return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
  1200. }
  1201. static int ocelot_port_obj_add(struct net_device *dev,
  1202. const struct switchdev_obj *obj,
  1203. struct switchdev_trans *trans,
  1204. struct netlink_ext_ack *extack)
  1205. {
  1206. int ret = 0;
  1207. switch (obj->id) {
  1208. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1209. ret = ocelot_port_obj_add_vlan(dev,
  1210. SWITCHDEV_OBJ_PORT_VLAN(obj),
  1211. trans);
  1212. break;
  1213. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1214. ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
  1215. trans);
  1216. break;
  1217. default:
  1218. return -EOPNOTSUPP;
  1219. }
  1220. return ret;
  1221. }
  1222. static int ocelot_port_obj_del(struct net_device *dev,
  1223. const struct switchdev_obj *obj)
  1224. {
  1225. int ret = 0;
  1226. switch (obj->id) {
  1227. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1228. ret = ocelot_port_vlan_del_vlan(dev,
  1229. SWITCHDEV_OBJ_PORT_VLAN(obj));
  1230. break;
  1231. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1232. ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
  1233. break;
  1234. default:
  1235. return -EOPNOTSUPP;
  1236. }
  1237. return ret;
  1238. }
  1239. static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
  1240. struct net_device *bridge)
  1241. {
  1242. struct ocelot *ocelot = ocelot_port->ocelot;
  1243. if (!ocelot->bridge_mask) {
  1244. ocelot->hw_bridge_dev = bridge;
  1245. } else {
  1246. if (ocelot->hw_bridge_dev != bridge)
  1247. /* This is adding the port to a second bridge, this is
  1248. * unsupported */
  1249. return -ENODEV;
  1250. }
  1251. ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
  1252. return 0;
  1253. }
  1254. static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
  1255. struct net_device *bridge)
  1256. {
  1257. struct ocelot *ocelot = ocelot_port->ocelot;
  1258. ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
  1259. if (!ocelot->bridge_mask)
  1260. ocelot->hw_bridge_dev = NULL;
  1261. /* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
  1262. ocelot_port->vlan_aware = 0;
  1263. ocelot_port->pvid = 0;
  1264. ocelot_port->vid = 0;
  1265. }
  1266. static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
  1267. {
  1268. int i, port, lag;
  1269. /* Reset destination and aggregation PGIDS */
  1270. for (port = 0; port < ocelot->num_phys_ports; port++)
  1271. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1272. for (i = PGID_AGGR; i < PGID_SRC; i++)
  1273. ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
  1274. ANA_PGID_PGID, i);
  1275. /* Now, set PGIDs for each LAG */
  1276. for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
  1277. unsigned long bond_mask;
  1278. int aggr_count = 0;
  1279. u8 aggr_idx[16];
  1280. bond_mask = ocelot->lags[lag];
  1281. if (!bond_mask)
  1282. continue;
  1283. for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
  1284. // Destination mask
  1285. ocelot_write_rix(ocelot, bond_mask,
  1286. ANA_PGID_PGID, port);
  1287. aggr_idx[aggr_count] = port;
  1288. aggr_count++;
  1289. }
  1290. for (i = PGID_AGGR; i < PGID_SRC; i++) {
  1291. u32 ac;
  1292. ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
  1293. ac &= ~bond_mask;
  1294. ac |= BIT(aggr_idx[i % aggr_count]);
  1295. ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
  1296. }
  1297. }
  1298. }
  1299. static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
  1300. {
  1301. unsigned long bond_mask = ocelot->lags[lag];
  1302. unsigned int p;
  1303. for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
  1304. u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
  1305. port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
  1306. /* Use lag port as logical port for port i */
  1307. ocelot_write_gix(ocelot, port_cfg |
  1308. ANA_PORT_PORT_CFG_PORTID_VAL(lag),
  1309. ANA_PORT_PORT_CFG, p);
  1310. }
  1311. }
  1312. static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
  1313. struct net_device *bond)
  1314. {
  1315. struct ocelot *ocelot = ocelot_port->ocelot;
  1316. int p = ocelot_port->chip_port;
  1317. int lag, lp;
  1318. struct net_device *ndev;
  1319. u32 bond_mask = 0;
  1320. rcu_read_lock();
  1321. for_each_netdev_in_bond_rcu(bond, ndev) {
  1322. struct ocelot_port *port = netdev_priv(ndev);
  1323. bond_mask |= BIT(port->chip_port);
  1324. }
  1325. rcu_read_unlock();
  1326. lp = __ffs(bond_mask);
  1327. /* If the new port is the lowest one, use it as the logical port from
  1328. * now on
  1329. */
  1330. if (p == lp) {
  1331. lag = p;
  1332. ocelot->lags[p] = bond_mask;
  1333. bond_mask &= ~BIT(p);
  1334. if (bond_mask) {
  1335. lp = __ffs(bond_mask);
  1336. ocelot->lags[lp] = 0;
  1337. }
  1338. } else {
  1339. lag = lp;
  1340. ocelot->lags[lp] |= BIT(p);
  1341. }
  1342. ocelot_setup_lag(ocelot, lag);
  1343. ocelot_set_aggr_pgids(ocelot);
  1344. return 0;
  1345. }
  1346. static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
  1347. struct net_device *bond)
  1348. {
  1349. struct ocelot *ocelot = ocelot_port->ocelot;
  1350. int p = ocelot_port->chip_port;
  1351. u32 port_cfg;
  1352. int i;
  1353. /* Remove port from any lag */
  1354. for (i = 0; i < ocelot->num_phys_ports; i++)
  1355. ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
  1356. /* if it was the logical port of the lag, move the lag config to the
  1357. * next port
  1358. */
  1359. if (ocelot->lags[p]) {
  1360. int n = __ffs(ocelot->lags[p]);
  1361. ocelot->lags[n] = ocelot->lags[p];
  1362. ocelot->lags[p] = 0;
  1363. ocelot_setup_lag(ocelot, n);
  1364. }
  1365. port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
  1366. port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
  1367. ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
  1368. ANA_PORT_PORT_CFG, p);
  1369. ocelot_set_aggr_pgids(ocelot);
  1370. }
  1371. /* Checks if the net_device instance given to us originate from our driver. */
  1372. static bool ocelot_netdevice_dev_check(const struct net_device *dev)
  1373. {
  1374. return dev->netdev_ops == &ocelot_port_netdev_ops;
  1375. }
  1376. static int ocelot_netdevice_port_event(struct net_device *dev,
  1377. unsigned long event,
  1378. struct netdev_notifier_changeupper_info *info)
  1379. {
  1380. struct ocelot_port *ocelot_port = netdev_priv(dev);
  1381. int err = 0;
  1382. switch (event) {
  1383. case NETDEV_CHANGEUPPER:
  1384. if (netif_is_bridge_master(info->upper_dev)) {
  1385. if (info->linking)
  1386. err = ocelot_port_bridge_join(ocelot_port,
  1387. info->upper_dev);
  1388. else
  1389. ocelot_port_bridge_leave(ocelot_port,
  1390. info->upper_dev);
  1391. ocelot_vlan_port_apply(ocelot_port->ocelot,
  1392. ocelot_port);
  1393. }
  1394. if (netif_is_lag_master(info->upper_dev)) {
  1395. if (info->linking)
  1396. err = ocelot_port_lag_join(ocelot_port,
  1397. info->upper_dev);
  1398. else
  1399. ocelot_port_lag_leave(ocelot_port,
  1400. info->upper_dev);
  1401. }
  1402. break;
  1403. default:
  1404. break;
  1405. }
  1406. return err;
  1407. }
  1408. static int ocelot_netdevice_event(struct notifier_block *unused,
  1409. unsigned long event, void *ptr)
  1410. {
  1411. struct netdev_notifier_changeupper_info *info = ptr;
  1412. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1413. int ret = 0;
  1414. if (event == NETDEV_PRECHANGEUPPER &&
  1415. ocelot_netdevice_dev_check(dev) &&
  1416. netif_is_lag_master(info->upper_dev)) {
  1417. struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
  1418. struct netlink_ext_ack *extack;
  1419. if (lag_upper_info &&
  1420. lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  1421. extack = netdev_notifier_info_to_extack(&info->info);
  1422. NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
  1423. ret = -EINVAL;
  1424. goto notify;
  1425. }
  1426. }
  1427. if (netif_is_lag_master(dev)) {
  1428. struct net_device *slave;
  1429. struct list_head *iter;
  1430. netdev_for_each_lower_dev(dev, slave, iter) {
  1431. ret = ocelot_netdevice_port_event(slave, event, info);
  1432. if (ret)
  1433. goto notify;
  1434. }
  1435. } else {
  1436. ret = ocelot_netdevice_port_event(dev, event, info);
  1437. }
  1438. notify:
  1439. return notifier_from_errno(ret);
  1440. }
  1441. struct notifier_block ocelot_netdevice_nb __read_mostly = {
  1442. .notifier_call = ocelot_netdevice_event,
  1443. };
  1444. EXPORT_SYMBOL(ocelot_netdevice_nb);
  1445. static int ocelot_switchdev_event(struct notifier_block *unused,
  1446. unsigned long event, void *ptr)
  1447. {
  1448. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1449. int err;
  1450. switch (event) {
  1451. case SWITCHDEV_PORT_ATTR_SET:
  1452. err = switchdev_handle_port_attr_set(dev, ptr,
  1453. ocelot_netdevice_dev_check,
  1454. ocelot_port_attr_set);
  1455. return notifier_from_errno(err);
  1456. }
  1457. return NOTIFY_DONE;
  1458. }
  1459. struct notifier_block ocelot_switchdev_nb __read_mostly = {
  1460. .notifier_call = ocelot_switchdev_event,
  1461. };
  1462. EXPORT_SYMBOL(ocelot_switchdev_nb);
  1463. static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
  1464. unsigned long event, void *ptr)
  1465. {
  1466. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1467. int err;
  1468. switch (event) {
  1469. /* Blocking events. */
  1470. case SWITCHDEV_PORT_OBJ_ADD:
  1471. err = switchdev_handle_port_obj_add(dev, ptr,
  1472. ocelot_netdevice_dev_check,
  1473. ocelot_port_obj_add);
  1474. return notifier_from_errno(err);
  1475. case SWITCHDEV_PORT_OBJ_DEL:
  1476. err = switchdev_handle_port_obj_del(dev, ptr,
  1477. ocelot_netdevice_dev_check,
  1478. ocelot_port_obj_del);
  1479. return notifier_from_errno(err);
  1480. case SWITCHDEV_PORT_ATTR_SET:
  1481. err = switchdev_handle_port_attr_set(dev, ptr,
  1482. ocelot_netdevice_dev_check,
  1483. ocelot_port_attr_set);
  1484. return notifier_from_errno(err);
  1485. }
  1486. return NOTIFY_DONE;
  1487. }
  1488. struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
  1489. .notifier_call = ocelot_switchdev_blocking_event,
  1490. };
  1491. EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
  1492. int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
  1493. {
  1494. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  1495. unsigned long flags;
  1496. time64_t s;
  1497. u32 val;
  1498. s64 ns;
  1499. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  1500. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  1501. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  1502. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
  1503. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  1504. s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
  1505. s <<= 32;
  1506. s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  1507. ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  1508. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  1509. /* Deal with negative values */
  1510. if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
  1511. s--;
  1512. ns &= 0xf;
  1513. ns += 999999984;
  1514. }
  1515. set_normalized_timespec64(ts, s, ns);
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL(ocelot_ptp_gettime64);
  1519. static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
  1520. const struct timespec64 *ts)
  1521. {
  1522. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  1523. unsigned long flags;
  1524. u32 val;
  1525. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  1526. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  1527. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  1528. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
  1529. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  1530. ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
  1531. TOD_ACC_PIN);
  1532. ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
  1533. TOD_ACC_PIN);
  1534. ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  1535. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  1536. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  1537. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
  1538. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  1539. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  1540. return 0;
  1541. }
  1542. static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1543. {
  1544. if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
  1545. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  1546. unsigned long flags;
  1547. u32 val;
  1548. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  1549. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  1550. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  1551. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
  1552. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  1553. ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  1554. ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
  1555. ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  1556. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  1557. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  1558. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
  1559. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  1560. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  1561. } else {
  1562. /* Fall back using ocelot_ptp_settime64 which is not exact. */
  1563. struct timespec64 ts;
  1564. u64 now;
  1565. ocelot_ptp_gettime64(ptp, &ts);
  1566. now = ktime_to_ns(timespec64_to_ktime(ts));
  1567. ts = ns_to_timespec64(now + delta);
  1568. ocelot_ptp_settime64(ptp, &ts);
  1569. }
  1570. return 0;
  1571. }
  1572. static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  1573. {
  1574. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  1575. u32 unit = 0, direction = 0;
  1576. unsigned long flags;
  1577. u64 adj = 0;
  1578. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  1579. if (!scaled_ppm)
  1580. goto disable_adj;
  1581. if (scaled_ppm < 0) {
  1582. direction = PTP_CFG_CLK_ADJ_CFG_DIR;
  1583. scaled_ppm = -scaled_ppm;
  1584. }
  1585. adj = PSEC_PER_SEC << 16;
  1586. do_div(adj, scaled_ppm);
  1587. do_div(adj, 1000);
  1588. /* If the adjustment value is too large, use ns instead */
  1589. if (adj >= (1L << 30)) {
  1590. unit = PTP_CFG_CLK_ADJ_FREQ_NS;
  1591. do_div(adj, 1000);
  1592. }
  1593. /* Still too big */
  1594. if (adj >= (1L << 30))
  1595. goto disable_adj;
  1596. ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
  1597. ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
  1598. PTP_CLK_CFG_ADJ_CFG);
  1599. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  1600. return 0;
  1601. disable_adj:
  1602. ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
  1603. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  1604. return 0;
  1605. }
  1606. static struct ptp_clock_info ocelot_ptp_clock_info = {
  1607. .owner = THIS_MODULE,
  1608. .name = "ocelot ptp",
  1609. .max_adj = 0x7fffffff,
  1610. .n_alarm = 0,
  1611. .n_ext_ts = 0,
  1612. .n_per_out = 0,
  1613. .n_pins = 0,
  1614. .pps = 0,
  1615. .gettime64 = ocelot_ptp_gettime64,
  1616. .settime64 = ocelot_ptp_settime64,
  1617. .adjtime = ocelot_ptp_adjtime,
  1618. .adjfine = ocelot_ptp_adjfine,
  1619. };
  1620. static int ocelot_init_timestamp(struct ocelot *ocelot)
  1621. {
  1622. struct ptp_clock *ptp_clock;
  1623. ocelot->ptp_info = ocelot_ptp_clock_info;
  1624. ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
  1625. if (IS_ERR(ptp_clock))
  1626. return PTR_ERR(ptp_clock);
  1627. /* Check if PHC support is missing at the configuration level */
  1628. if (!ptp_clock)
  1629. return 0;
  1630. ocelot->ptp_clock = ptp_clock;
  1631. ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
  1632. ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
  1633. ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
  1634. ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
  1635. /* There is no device reconfiguration, PTP Rx stamping is always
  1636. * enabled.
  1637. */
  1638. ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1639. return 0;
  1640. }
  1641. int ocelot_probe_port(struct ocelot *ocelot, u8 port,
  1642. void __iomem *regs,
  1643. struct phy_device *phy)
  1644. {
  1645. struct ocelot_port *ocelot_port;
  1646. struct net_device *dev;
  1647. int err;
  1648. dev = alloc_etherdev(sizeof(struct ocelot_port));
  1649. if (!dev)
  1650. return -ENOMEM;
  1651. SET_NETDEV_DEV(dev, ocelot->dev);
  1652. ocelot_port = netdev_priv(dev);
  1653. ocelot_port->dev = dev;
  1654. ocelot_port->ocelot = ocelot;
  1655. ocelot_port->regs = regs;
  1656. ocelot_port->chip_port = port;
  1657. ocelot_port->phy = phy;
  1658. ocelot->ports[port] = ocelot_port;
  1659. dev->netdev_ops = &ocelot_port_netdev_ops;
  1660. dev->ethtool_ops = &ocelot_ethtool_ops;
  1661. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
  1662. NETIF_F_HW_TC;
  1663. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
  1664. memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
  1665. dev->dev_addr[ETH_ALEN - 1] += port;
  1666. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
  1667. ENTRYTYPE_LOCKED);
  1668. INIT_LIST_HEAD(&ocelot_port->skbs);
  1669. err = register_netdev(dev);
  1670. if (err) {
  1671. dev_err(ocelot->dev, "register_netdev failed\n");
  1672. goto err_register_netdev;
  1673. }
  1674. /* Basic L2 initialization */
  1675. ocelot_vlan_port_apply(ocelot, ocelot_port);
  1676. /* Enable vcap lookups */
  1677. ocelot_vcap_enable(ocelot, ocelot_port);
  1678. return 0;
  1679. err_register_netdev:
  1680. free_netdev(dev);
  1681. return err;
  1682. }
  1683. EXPORT_SYMBOL(ocelot_probe_port);
  1684. int ocelot_init(struct ocelot *ocelot)
  1685. {
  1686. u32 port;
  1687. int i, ret, cpu = ocelot->num_phys_ports;
  1688. char queue_name[32];
  1689. ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
  1690. sizeof(u32), GFP_KERNEL);
  1691. if (!ocelot->lags)
  1692. return -ENOMEM;
  1693. ocelot->stats = devm_kcalloc(ocelot->dev,
  1694. ocelot->num_phys_ports * ocelot->num_stats,
  1695. sizeof(u64), GFP_KERNEL);
  1696. if (!ocelot->stats)
  1697. return -ENOMEM;
  1698. mutex_init(&ocelot->stats_lock);
  1699. mutex_init(&ocelot->ptp_lock);
  1700. spin_lock_init(&ocelot->ptp_clock_lock);
  1701. snprintf(queue_name, sizeof(queue_name), "%s-stats",
  1702. dev_name(ocelot->dev));
  1703. ocelot->stats_queue = create_singlethread_workqueue(queue_name);
  1704. if (!ocelot->stats_queue)
  1705. return -ENOMEM;
  1706. ocelot_mact_init(ocelot);
  1707. ocelot_vlan_init(ocelot);
  1708. ocelot_ace_init(ocelot);
  1709. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1710. /* Clear all counters (5 groups) */
  1711. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
  1712. SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
  1713. SYS_STAT_CFG);
  1714. }
  1715. /* Only use S-Tag */
  1716. ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
  1717. /* Aggregation mode */
  1718. ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
  1719. ANA_AGGR_CFG_AC_DMAC_ENA |
  1720. ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
  1721. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
  1722. /* Set MAC age time to default value. The entry is aged after
  1723. * 2*AGE_PERIOD
  1724. */
  1725. ocelot_write(ocelot,
  1726. ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
  1727. ANA_AUTOAGE);
  1728. /* Disable learning for frames discarded by VLAN ingress filtering */
  1729. regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
  1730. /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
  1731. ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
  1732. SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
  1733. /* Setup flooding PGIDs */
  1734. ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
  1735. ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
  1736. ANA_FLOODING_FLD_UNICAST(PGID_UC),
  1737. ANA_FLOODING, 0);
  1738. ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
  1739. ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
  1740. ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
  1741. ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
  1742. ANA_FLOODING_IPMC);
  1743. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1744. /* Transmit the frame to the local port. */
  1745. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1746. /* Do not forward BPDU frames to the front ports. */
  1747. ocelot_write_gix(ocelot,
  1748. ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
  1749. ANA_PORT_CPU_FWD_BPDU_CFG,
  1750. port);
  1751. /* Ensure bridging is disabled */
  1752. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
  1753. }
  1754. /* Configure and enable the CPU port. */
  1755. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
  1756. ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
  1757. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
  1758. ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
  1759. ANA_PORT_PORT_CFG, cpu);
  1760. /* Allow broadcast MAC frames. */
  1761. for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
  1762. u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
  1763. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  1764. }
  1765. ocelot_write_rix(ocelot,
  1766. ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
  1767. ANA_PGID_PGID, PGID_MC);
  1768. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
  1769. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
  1770. /* CPU port Injection/Extraction configuration */
  1771. ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
  1772. QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
  1773. QSYS_SWITCH_PORT_MODE_PORT_ENA,
  1774. QSYS_SWITCH_PORT_MODE, cpu);
  1775. ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
  1776. SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
  1777. /* Allow manual injection via DEVCPU_QS registers, and byte swap these
  1778. * registers endianness.
  1779. */
  1780. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
  1781. QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
  1782. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
  1783. QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
  1784. ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
  1785. ANA_CPUQ_CFG_CPUQ_LRN(2) |
  1786. ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
  1787. ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
  1788. ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
  1789. ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
  1790. ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
  1791. ANA_CPUQ_CFG_CPUQ_IGMP(6) |
  1792. ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
  1793. for (i = 0; i < 16; i++)
  1794. ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
  1795. ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
  1796. ANA_CPUQ_8021_CFG, i);
  1797. INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
  1798. queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
  1799. OCELOT_STATS_CHECK_DELAY);
  1800. if (ocelot->ptp) {
  1801. ret = ocelot_init_timestamp(ocelot);
  1802. if (ret) {
  1803. dev_err(ocelot->dev,
  1804. "Timestamp initialization failed\n");
  1805. return ret;
  1806. }
  1807. }
  1808. return 0;
  1809. }
  1810. EXPORT_SYMBOL(ocelot_init);
  1811. void ocelot_deinit(struct ocelot *ocelot)
  1812. {
  1813. struct list_head *pos, *tmp;
  1814. struct ocelot_port *port;
  1815. struct ocelot_skb *entry;
  1816. int i;
  1817. cancel_delayed_work(&ocelot->stats_work);
  1818. destroy_workqueue(ocelot->stats_queue);
  1819. mutex_destroy(&ocelot->stats_lock);
  1820. ocelot_ace_deinit();
  1821. if (ocelot->ptp_clock)
  1822. ptp_clock_unregister(ocelot->ptp_clock);
  1823. for (i = 0; i < ocelot->num_phys_ports; i++) {
  1824. port = ocelot->ports[i];
  1825. list_for_each_safe(pos, tmp, &port->skbs) {
  1826. entry = list_entry(pos, struct ocelot_skb, head);
  1827. list_del(pos);
  1828. dev_kfree_skb_any(entry->skb);
  1829. kfree(entry);
  1830. }
  1831. }
  1832. }
  1833. EXPORT_SYMBOL(ocelot_deinit);
  1834. MODULE_LICENSE("Dual MIT/GPL");