hellcreek.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * DSA driver for:
  4. * Hirschmann Hellcreek TSN switch.
  5. *
  6. * Copyright (C) 2019-2021 Linutronix GmbH
  7. * Author Kurt Kanzenbach <kurt@linutronix.de>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/of.h>
  13. #include <linux/of_mdio.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/bitops.h>
  16. #include <linux/if_bridge.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/random.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/mutex.h>
  22. #include <linux/delay.h>
  23. #include <net/dsa.h>
  24. #include "hellcreek.h"
  25. #include "hellcreek_ptp.h"
  26. #include "hellcreek_hwtstamp.h"
  27. static const struct hellcreek_counter hellcreek_counter[] = {
  28. { 0x00, "RxFiltered", },
  29. { 0x01, "RxOctets1k", },
  30. { 0x02, "RxVTAG", },
  31. { 0x03, "RxL2BAD", },
  32. { 0x04, "RxOverloadDrop", },
  33. { 0x05, "RxUC", },
  34. { 0x06, "RxMC", },
  35. { 0x07, "RxBC", },
  36. { 0x08, "RxRS<64", },
  37. { 0x09, "RxRS64", },
  38. { 0x0a, "RxRS65_127", },
  39. { 0x0b, "RxRS128_255", },
  40. { 0x0c, "RxRS256_511", },
  41. { 0x0d, "RxRS512_1023", },
  42. { 0x0e, "RxRS1024_1518", },
  43. { 0x0f, "RxRS>1518", },
  44. { 0x10, "TxTailDropQueue0", },
  45. { 0x11, "TxTailDropQueue1", },
  46. { 0x12, "TxTailDropQueue2", },
  47. { 0x13, "TxTailDropQueue3", },
  48. { 0x14, "TxTailDropQueue4", },
  49. { 0x15, "TxTailDropQueue5", },
  50. { 0x16, "TxTailDropQueue6", },
  51. { 0x17, "TxTailDropQueue7", },
  52. { 0x18, "RxTrafficClass0", },
  53. { 0x19, "RxTrafficClass1", },
  54. { 0x1a, "RxTrafficClass2", },
  55. { 0x1b, "RxTrafficClass3", },
  56. { 0x1c, "RxTrafficClass4", },
  57. { 0x1d, "RxTrafficClass5", },
  58. { 0x1e, "RxTrafficClass6", },
  59. { 0x1f, "RxTrafficClass7", },
  60. { 0x21, "TxOctets1k", },
  61. { 0x22, "TxVTAG", },
  62. { 0x23, "TxL2BAD", },
  63. { 0x25, "TxUC", },
  64. { 0x26, "TxMC", },
  65. { 0x27, "TxBC", },
  66. { 0x28, "TxTS<64", },
  67. { 0x29, "TxTS64", },
  68. { 0x2a, "TxTS65_127", },
  69. { 0x2b, "TxTS128_255", },
  70. { 0x2c, "TxTS256_511", },
  71. { 0x2d, "TxTS512_1023", },
  72. { 0x2e, "TxTS1024_1518", },
  73. { 0x2f, "TxTS>1518", },
  74. { 0x30, "TxTrafficClassOverrun0", },
  75. { 0x31, "TxTrafficClassOverrun1", },
  76. { 0x32, "TxTrafficClassOverrun2", },
  77. { 0x33, "TxTrafficClassOverrun3", },
  78. { 0x34, "TxTrafficClassOverrun4", },
  79. { 0x35, "TxTrafficClassOverrun5", },
  80. { 0x36, "TxTrafficClassOverrun6", },
  81. { 0x37, "TxTrafficClassOverrun7", },
  82. { 0x38, "TxTrafficClass0", },
  83. { 0x39, "TxTrafficClass1", },
  84. { 0x3a, "TxTrafficClass2", },
  85. { 0x3b, "TxTrafficClass3", },
  86. { 0x3c, "TxTrafficClass4", },
  87. { 0x3d, "TxTrafficClass5", },
  88. { 0x3e, "TxTrafficClass6", },
  89. { 0x3f, "TxTrafficClass7", },
  90. };
  91. static u16 hellcreek_read(struct hellcreek *hellcreek, unsigned int offset)
  92. {
  93. return readw(hellcreek->base + offset);
  94. }
  95. static u16 hellcreek_read_ctrl(struct hellcreek *hellcreek)
  96. {
  97. return readw(hellcreek->base + HR_CTRL_C);
  98. }
  99. static u16 hellcreek_read_stat(struct hellcreek *hellcreek)
  100. {
  101. return readw(hellcreek->base + HR_SWSTAT);
  102. }
  103. static void hellcreek_write(struct hellcreek *hellcreek, u16 data,
  104. unsigned int offset)
  105. {
  106. writew(data, hellcreek->base + offset);
  107. }
  108. static void hellcreek_select_port(struct hellcreek *hellcreek, int port)
  109. {
  110. u16 val = port << HR_PSEL_PTWSEL_SHIFT;
  111. hellcreek_write(hellcreek, val, HR_PSEL);
  112. }
  113. static void hellcreek_select_prio(struct hellcreek *hellcreek, int prio)
  114. {
  115. u16 val = prio << HR_PSEL_PRTCWSEL_SHIFT;
  116. hellcreek_write(hellcreek, val, HR_PSEL);
  117. }
  118. static void hellcreek_select_port_prio(struct hellcreek *hellcreek, int port,
  119. int prio)
  120. {
  121. u16 val = port << HR_PSEL_PTWSEL_SHIFT;
  122. val |= prio << HR_PSEL_PRTCWSEL_SHIFT;
  123. hellcreek_write(hellcreek, val, HR_PSEL);
  124. }
  125. static void hellcreek_select_counter(struct hellcreek *hellcreek, int counter)
  126. {
  127. u16 val = counter << HR_CSEL_SHIFT;
  128. hellcreek_write(hellcreek, val, HR_CSEL);
  129. /* Data sheet states to wait at least 20 internal clock cycles */
  130. ndelay(200);
  131. }
  132. static void hellcreek_select_vlan(struct hellcreek *hellcreek, int vid,
  133. bool pvid)
  134. {
  135. u16 val = 0;
  136. /* Set pvid bit first */
  137. if (pvid)
  138. val |= HR_VIDCFG_PVID;
  139. hellcreek_write(hellcreek, val, HR_VIDCFG);
  140. /* Set vlan */
  141. val |= vid << HR_VIDCFG_VID_SHIFT;
  142. hellcreek_write(hellcreek, val, HR_VIDCFG);
  143. }
  144. static void hellcreek_select_tgd(struct hellcreek *hellcreek, int port)
  145. {
  146. u16 val = port << TR_TGDSEL_TDGSEL_SHIFT;
  147. hellcreek_write(hellcreek, val, TR_TGDSEL);
  148. }
  149. static int hellcreek_wait_until_ready(struct hellcreek *hellcreek)
  150. {
  151. u16 val;
  152. /* Wait up to 1ms, although 3 us should be enough */
  153. return readx_poll_timeout(hellcreek_read_ctrl, hellcreek,
  154. val, val & HR_CTRL_C_READY,
  155. 3, 1000);
  156. }
  157. static int hellcreek_wait_until_transitioned(struct hellcreek *hellcreek)
  158. {
  159. u16 val;
  160. return readx_poll_timeout_atomic(hellcreek_read_ctrl, hellcreek,
  161. val, !(val & HR_CTRL_C_TRANSITION),
  162. 1, 1000);
  163. }
  164. static int hellcreek_wait_fdb_ready(struct hellcreek *hellcreek)
  165. {
  166. u16 val;
  167. return readx_poll_timeout_atomic(hellcreek_read_stat, hellcreek,
  168. val, !(val & HR_SWSTAT_BUSY),
  169. 1, 1000);
  170. }
  171. static int hellcreek_detect(struct hellcreek *hellcreek)
  172. {
  173. u16 id, rel_low, rel_high, date_low, date_high, tgd_ver;
  174. u8 tgd_maj, tgd_min;
  175. u32 rel, date;
  176. id = hellcreek_read(hellcreek, HR_MODID_C);
  177. rel_low = hellcreek_read(hellcreek, HR_REL_L_C);
  178. rel_high = hellcreek_read(hellcreek, HR_REL_H_C);
  179. date_low = hellcreek_read(hellcreek, HR_BLD_L_C);
  180. date_high = hellcreek_read(hellcreek, HR_BLD_H_C);
  181. tgd_ver = hellcreek_read(hellcreek, TR_TGDVER);
  182. if (id != hellcreek->pdata->module_id)
  183. return -ENODEV;
  184. rel = rel_low | (rel_high << 16);
  185. date = date_low | (date_high << 16);
  186. tgd_maj = (tgd_ver & TR_TGDVER_REV_MAJ_MASK) >> TR_TGDVER_REV_MAJ_SHIFT;
  187. tgd_min = (tgd_ver & TR_TGDVER_REV_MIN_MASK) >> TR_TGDVER_REV_MIN_SHIFT;
  188. dev_info(hellcreek->dev, "Module ID=%02x Release=%04x Date=%04x TGD Version=%02x.%02x\n",
  189. id, rel, date, tgd_maj, tgd_min);
  190. return 0;
  191. }
  192. static void hellcreek_feature_detect(struct hellcreek *hellcreek)
  193. {
  194. u16 features;
  195. features = hellcreek_read(hellcreek, HR_FEABITS0);
  196. /* Only detect the size of the FDB table. The size and current
  197. * utilization can be queried via devlink.
  198. */
  199. hellcreek->fdb_entries = ((features & HR_FEABITS0_FDBBINS_MASK) >>
  200. HR_FEABITS0_FDBBINS_SHIFT) * 32;
  201. }
  202. static enum dsa_tag_protocol hellcreek_get_tag_protocol(struct dsa_switch *ds,
  203. int port,
  204. enum dsa_tag_protocol mp)
  205. {
  206. return DSA_TAG_PROTO_HELLCREEK;
  207. }
  208. static int hellcreek_port_enable(struct dsa_switch *ds, int port,
  209. struct phy_device *phy)
  210. {
  211. struct hellcreek *hellcreek = ds->priv;
  212. struct hellcreek_port *hellcreek_port;
  213. u16 val;
  214. hellcreek_port = &hellcreek->ports[port];
  215. dev_dbg(hellcreek->dev, "Enable port %d\n", port);
  216. mutex_lock(&hellcreek->reg_lock);
  217. hellcreek_select_port(hellcreek, port);
  218. val = hellcreek_port->ptcfg;
  219. val |= HR_PTCFG_ADMIN_EN;
  220. hellcreek_write(hellcreek, val, HR_PTCFG);
  221. hellcreek_port->ptcfg = val;
  222. mutex_unlock(&hellcreek->reg_lock);
  223. return 0;
  224. }
  225. static void hellcreek_port_disable(struct dsa_switch *ds, int port)
  226. {
  227. struct hellcreek *hellcreek = ds->priv;
  228. struct hellcreek_port *hellcreek_port;
  229. u16 val;
  230. hellcreek_port = &hellcreek->ports[port];
  231. dev_dbg(hellcreek->dev, "Disable port %d\n", port);
  232. mutex_lock(&hellcreek->reg_lock);
  233. hellcreek_select_port(hellcreek, port);
  234. val = hellcreek_port->ptcfg;
  235. val &= ~HR_PTCFG_ADMIN_EN;
  236. hellcreek_write(hellcreek, val, HR_PTCFG);
  237. hellcreek_port->ptcfg = val;
  238. mutex_unlock(&hellcreek->reg_lock);
  239. }
  240. static void hellcreek_get_strings(struct dsa_switch *ds, int port,
  241. u32 stringset, uint8_t *data)
  242. {
  243. int i;
  244. for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
  245. const struct hellcreek_counter *counter = &hellcreek_counter[i];
  246. strscpy(data + i * ETH_GSTRING_LEN,
  247. counter->name, ETH_GSTRING_LEN);
  248. }
  249. }
  250. static int hellcreek_get_sset_count(struct dsa_switch *ds, int port, int sset)
  251. {
  252. if (sset != ETH_SS_STATS)
  253. return 0;
  254. return ARRAY_SIZE(hellcreek_counter);
  255. }
  256. static void hellcreek_get_ethtool_stats(struct dsa_switch *ds, int port,
  257. uint64_t *data)
  258. {
  259. struct hellcreek *hellcreek = ds->priv;
  260. struct hellcreek_port *hellcreek_port;
  261. int i;
  262. hellcreek_port = &hellcreek->ports[port];
  263. for (i = 0; i < ARRAY_SIZE(hellcreek_counter); ++i) {
  264. const struct hellcreek_counter *counter = &hellcreek_counter[i];
  265. u8 offset = counter->offset + port * 64;
  266. u16 high, low;
  267. u64 value;
  268. mutex_lock(&hellcreek->reg_lock);
  269. hellcreek_select_counter(hellcreek, offset);
  270. /* The registers are locked internally by selecting the
  271. * counter. So low and high can be read without reading high
  272. * again.
  273. */
  274. high = hellcreek_read(hellcreek, HR_CRDH);
  275. low = hellcreek_read(hellcreek, HR_CRDL);
  276. value = ((u64)high << 16) | low;
  277. hellcreek_port->counter_values[i] += value;
  278. data[i] = hellcreek_port->counter_values[i];
  279. mutex_unlock(&hellcreek->reg_lock);
  280. }
  281. }
  282. static u16 hellcreek_private_vid(int port)
  283. {
  284. return VLAN_N_VID - port + 1;
  285. }
  286. static int hellcreek_vlan_prepare(struct dsa_switch *ds, int port,
  287. const struct switchdev_obj_port_vlan *vlan,
  288. struct netlink_ext_ack *extack)
  289. {
  290. struct hellcreek *hellcreek = ds->priv;
  291. int i;
  292. dev_dbg(hellcreek->dev, "VLAN prepare for port %d\n", port);
  293. /* Restriction: Make sure that nobody uses the "private" VLANs. These
  294. * VLANs are internally used by the driver to ensure port
  295. * separation. Thus, they cannot be used by someone else.
  296. */
  297. for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
  298. const u16 restricted_vid = hellcreek_private_vid(i);
  299. if (!dsa_is_user_port(ds, i))
  300. continue;
  301. if (vlan->vid == restricted_vid) {
  302. NL_SET_ERR_MSG_MOD(extack, "VID restricted by driver");
  303. return -EBUSY;
  304. }
  305. }
  306. return 0;
  307. }
  308. static void hellcreek_select_vlan_params(struct hellcreek *hellcreek, int port,
  309. int *shift, int *mask)
  310. {
  311. switch (port) {
  312. case 0:
  313. *shift = HR_VIDMBRCFG_P0MBR_SHIFT;
  314. *mask = HR_VIDMBRCFG_P0MBR_MASK;
  315. break;
  316. case 1:
  317. *shift = HR_VIDMBRCFG_P1MBR_SHIFT;
  318. *mask = HR_VIDMBRCFG_P1MBR_MASK;
  319. break;
  320. case 2:
  321. *shift = HR_VIDMBRCFG_P2MBR_SHIFT;
  322. *mask = HR_VIDMBRCFG_P2MBR_MASK;
  323. break;
  324. case 3:
  325. *shift = HR_VIDMBRCFG_P3MBR_SHIFT;
  326. *mask = HR_VIDMBRCFG_P3MBR_MASK;
  327. break;
  328. default:
  329. *shift = *mask = 0;
  330. dev_err(hellcreek->dev, "Unknown port %d selected!\n", port);
  331. }
  332. }
  333. static void hellcreek_apply_vlan(struct hellcreek *hellcreek, int port, u16 vid,
  334. bool pvid, bool untagged)
  335. {
  336. int shift, mask;
  337. u16 val;
  338. dev_dbg(hellcreek->dev, "Apply VLAN: port=%d vid=%u pvid=%d untagged=%d",
  339. port, vid, pvid, untagged);
  340. mutex_lock(&hellcreek->reg_lock);
  341. hellcreek_select_port(hellcreek, port);
  342. hellcreek_select_vlan(hellcreek, vid, pvid);
  343. /* Setup port vlan membership */
  344. hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
  345. val = hellcreek->vidmbrcfg[vid];
  346. val &= ~mask;
  347. if (untagged)
  348. val |= HELLCREEK_VLAN_UNTAGGED_MEMBER << shift;
  349. else
  350. val |= HELLCREEK_VLAN_TAGGED_MEMBER << shift;
  351. hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
  352. hellcreek->vidmbrcfg[vid] = val;
  353. mutex_unlock(&hellcreek->reg_lock);
  354. }
  355. static void hellcreek_unapply_vlan(struct hellcreek *hellcreek, int port,
  356. u16 vid)
  357. {
  358. int shift, mask;
  359. u16 val;
  360. dev_dbg(hellcreek->dev, "Unapply VLAN: port=%d vid=%u\n", port, vid);
  361. mutex_lock(&hellcreek->reg_lock);
  362. hellcreek_select_vlan(hellcreek, vid, false);
  363. /* Setup port vlan membership */
  364. hellcreek_select_vlan_params(hellcreek, port, &shift, &mask);
  365. val = hellcreek->vidmbrcfg[vid];
  366. val &= ~mask;
  367. val |= HELLCREEK_VLAN_NO_MEMBER << shift;
  368. hellcreek_write(hellcreek, val, HR_VIDMBRCFG);
  369. hellcreek->vidmbrcfg[vid] = val;
  370. mutex_unlock(&hellcreek->reg_lock);
  371. }
  372. static int hellcreek_vlan_add(struct dsa_switch *ds, int port,
  373. const struct switchdev_obj_port_vlan *vlan,
  374. struct netlink_ext_ack *extack)
  375. {
  376. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  377. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  378. struct hellcreek *hellcreek = ds->priv;
  379. int err;
  380. err = hellcreek_vlan_prepare(ds, port, vlan, extack);
  381. if (err)
  382. return err;
  383. dev_dbg(hellcreek->dev, "Add VLAN %d on port %d, %s, %s\n",
  384. vlan->vid, port, untagged ? "untagged" : "tagged",
  385. pvid ? "PVID" : "no PVID");
  386. hellcreek_apply_vlan(hellcreek, port, vlan->vid, pvid, untagged);
  387. return 0;
  388. }
  389. static int hellcreek_vlan_del(struct dsa_switch *ds, int port,
  390. const struct switchdev_obj_port_vlan *vlan)
  391. {
  392. struct hellcreek *hellcreek = ds->priv;
  393. dev_dbg(hellcreek->dev, "Remove VLAN %d on port %d\n", vlan->vid, port);
  394. hellcreek_unapply_vlan(hellcreek, port, vlan->vid);
  395. return 0;
  396. }
  397. static void hellcreek_port_stp_state_set(struct dsa_switch *ds, int port,
  398. u8 state)
  399. {
  400. struct hellcreek *hellcreek = ds->priv;
  401. struct hellcreek_port *hellcreek_port;
  402. const char *new_state;
  403. u16 val;
  404. mutex_lock(&hellcreek->reg_lock);
  405. hellcreek_port = &hellcreek->ports[port];
  406. val = hellcreek_port->ptcfg;
  407. switch (state) {
  408. case BR_STATE_DISABLED:
  409. new_state = "DISABLED";
  410. val |= HR_PTCFG_BLOCKED;
  411. val &= ~HR_PTCFG_LEARNING_EN;
  412. break;
  413. case BR_STATE_BLOCKING:
  414. new_state = "BLOCKING";
  415. val |= HR_PTCFG_BLOCKED;
  416. val &= ~HR_PTCFG_LEARNING_EN;
  417. break;
  418. case BR_STATE_LISTENING:
  419. new_state = "LISTENING";
  420. val |= HR_PTCFG_BLOCKED;
  421. val &= ~HR_PTCFG_LEARNING_EN;
  422. break;
  423. case BR_STATE_LEARNING:
  424. new_state = "LEARNING";
  425. val |= HR_PTCFG_BLOCKED;
  426. val |= HR_PTCFG_LEARNING_EN;
  427. break;
  428. case BR_STATE_FORWARDING:
  429. new_state = "FORWARDING";
  430. val &= ~HR_PTCFG_BLOCKED;
  431. val |= HR_PTCFG_LEARNING_EN;
  432. break;
  433. default:
  434. new_state = "UNKNOWN";
  435. }
  436. hellcreek_select_port(hellcreek, port);
  437. hellcreek_write(hellcreek, val, HR_PTCFG);
  438. hellcreek_port->ptcfg = val;
  439. mutex_unlock(&hellcreek->reg_lock);
  440. dev_dbg(hellcreek->dev, "Configured STP state for port %d: %s\n",
  441. port, new_state);
  442. }
  443. static void hellcreek_setup_ingressflt(struct hellcreek *hellcreek, int port,
  444. bool enable)
  445. {
  446. struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
  447. u16 ptcfg;
  448. mutex_lock(&hellcreek->reg_lock);
  449. ptcfg = hellcreek_port->ptcfg;
  450. if (enable)
  451. ptcfg |= HR_PTCFG_INGRESSFLT;
  452. else
  453. ptcfg &= ~HR_PTCFG_INGRESSFLT;
  454. hellcreek_select_port(hellcreek, port);
  455. hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
  456. hellcreek_port->ptcfg = ptcfg;
  457. mutex_unlock(&hellcreek->reg_lock);
  458. }
  459. static void hellcreek_setup_vlan_awareness(struct hellcreek *hellcreek,
  460. bool enable)
  461. {
  462. u16 swcfg;
  463. mutex_lock(&hellcreek->reg_lock);
  464. swcfg = hellcreek->swcfg;
  465. if (enable)
  466. swcfg |= HR_SWCFG_VLAN_UNAWARE;
  467. else
  468. swcfg &= ~HR_SWCFG_VLAN_UNAWARE;
  469. hellcreek_write(hellcreek, swcfg, HR_SWCFG);
  470. mutex_unlock(&hellcreek->reg_lock);
  471. }
  472. /* Default setup for DSA: VLAN <X>: CPU and Port <X> egress untagged. */
  473. static void hellcreek_setup_vlan_membership(struct dsa_switch *ds, int port,
  474. bool enabled)
  475. {
  476. const u16 vid = hellcreek_private_vid(port);
  477. int upstream = dsa_upstream_port(ds, port);
  478. struct hellcreek *hellcreek = ds->priv;
  479. /* Apply vid to port as egress untagged and port vlan id */
  480. if (enabled)
  481. hellcreek_apply_vlan(hellcreek, port, vid, true, true);
  482. else
  483. hellcreek_unapply_vlan(hellcreek, port, vid);
  484. /* Apply vid to cpu port as well */
  485. if (enabled)
  486. hellcreek_apply_vlan(hellcreek, upstream, vid, false, true);
  487. else
  488. hellcreek_unapply_vlan(hellcreek, upstream, vid);
  489. }
  490. static void hellcreek_port_set_ucast_flood(struct hellcreek *hellcreek,
  491. int port, bool enable)
  492. {
  493. struct hellcreek_port *hellcreek_port;
  494. u16 val;
  495. hellcreek_port = &hellcreek->ports[port];
  496. dev_dbg(hellcreek->dev, "%s unicast flooding on port %d\n",
  497. enable ? "Enable" : "Disable", port);
  498. mutex_lock(&hellcreek->reg_lock);
  499. hellcreek_select_port(hellcreek, port);
  500. val = hellcreek_port->ptcfg;
  501. if (enable)
  502. val &= ~HR_PTCFG_UUC_FLT;
  503. else
  504. val |= HR_PTCFG_UUC_FLT;
  505. hellcreek_write(hellcreek, val, HR_PTCFG);
  506. hellcreek_port->ptcfg = val;
  507. mutex_unlock(&hellcreek->reg_lock);
  508. }
  509. static void hellcreek_port_set_mcast_flood(struct hellcreek *hellcreek,
  510. int port, bool enable)
  511. {
  512. struct hellcreek_port *hellcreek_port;
  513. u16 val;
  514. hellcreek_port = &hellcreek->ports[port];
  515. dev_dbg(hellcreek->dev, "%s multicast flooding on port %d\n",
  516. enable ? "Enable" : "Disable", port);
  517. mutex_lock(&hellcreek->reg_lock);
  518. hellcreek_select_port(hellcreek, port);
  519. val = hellcreek_port->ptcfg;
  520. if (enable)
  521. val &= ~HR_PTCFG_UMC_FLT;
  522. else
  523. val |= HR_PTCFG_UMC_FLT;
  524. hellcreek_write(hellcreek, val, HR_PTCFG);
  525. hellcreek_port->ptcfg = val;
  526. mutex_unlock(&hellcreek->reg_lock);
  527. }
  528. static int hellcreek_pre_bridge_flags(struct dsa_switch *ds, int port,
  529. struct switchdev_brport_flags flags,
  530. struct netlink_ext_ack *extack)
  531. {
  532. if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD))
  533. return -EINVAL;
  534. return 0;
  535. }
  536. static int hellcreek_bridge_flags(struct dsa_switch *ds, int port,
  537. struct switchdev_brport_flags flags,
  538. struct netlink_ext_ack *extack)
  539. {
  540. struct hellcreek *hellcreek = ds->priv;
  541. if (flags.mask & BR_FLOOD)
  542. hellcreek_port_set_ucast_flood(hellcreek, port,
  543. !!(flags.val & BR_FLOOD));
  544. if (flags.mask & BR_MCAST_FLOOD)
  545. hellcreek_port_set_mcast_flood(hellcreek, port,
  546. !!(flags.val & BR_MCAST_FLOOD));
  547. return 0;
  548. }
  549. static int hellcreek_port_bridge_join(struct dsa_switch *ds, int port,
  550. struct dsa_bridge bridge,
  551. bool *tx_fwd_offload,
  552. struct netlink_ext_ack *extack)
  553. {
  554. struct hellcreek *hellcreek = ds->priv;
  555. dev_dbg(hellcreek->dev, "Port %d joins a bridge\n", port);
  556. /* When joining a vlan_filtering bridge, keep the switch VLAN aware */
  557. if (!ds->vlan_filtering)
  558. hellcreek_setup_vlan_awareness(hellcreek, false);
  559. /* Drop private vlans */
  560. hellcreek_setup_vlan_membership(ds, port, false);
  561. return 0;
  562. }
  563. static void hellcreek_port_bridge_leave(struct dsa_switch *ds, int port,
  564. struct dsa_bridge bridge)
  565. {
  566. struct hellcreek *hellcreek = ds->priv;
  567. dev_dbg(hellcreek->dev, "Port %d leaves a bridge\n", port);
  568. /* Enable VLAN awareness */
  569. hellcreek_setup_vlan_awareness(hellcreek, true);
  570. /* Enable private vlans */
  571. hellcreek_setup_vlan_membership(ds, port, true);
  572. }
  573. static int __hellcreek_fdb_add(struct hellcreek *hellcreek,
  574. const struct hellcreek_fdb_entry *entry)
  575. {
  576. u16 meta = 0;
  577. dev_dbg(hellcreek->dev, "Add static FDB entry: MAC=%pM, MASK=0x%02x, "
  578. "OBT=%d, PASS_BLOCKED=%d, REPRIO_EN=%d, PRIO=%d\n", entry->mac,
  579. entry->portmask, entry->is_obt, entry->pass_blocked,
  580. entry->reprio_en, entry->reprio_tc);
  581. /* Add mac address */
  582. hellcreek_write(hellcreek, entry->mac[1] | (entry->mac[0] << 8), HR_FDBWDH);
  583. hellcreek_write(hellcreek, entry->mac[3] | (entry->mac[2] << 8), HR_FDBWDM);
  584. hellcreek_write(hellcreek, entry->mac[5] | (entry->mac[4] << 8), HR_FDBWDL);
  585. /* Meta data */
  586. meta |= entry->portmask << HR_FDBWRM0_PORTMASK_SHIFT;
  587. if (entry->is_obt)
  588. meta |= HR_FDBWRM0_OBT;
  589. if (entry->pass_blocked)
  590. meta |= HR_FDBWRM0_PASS_BLOCKED;
  591. if (entry->reprio_en) {
  592. meta |= HR_FDBWRM0_REPRIO_EN;
  593. meta |= entry->reprio_tc << HR_FDBWRM0_REPRIO_TC_SHIFT;
  594. }
  595. hellcreek_write(hellcreek, meta, HR_FDBWRM0);
  596. /* Commit */
  597. hellcreek_write(hellcreek, 0x00, HR_FDBWRCMD);
  598. /* Wait until done */
  599. return hellcreek_wait_fdb_ready(hellcreek);
  600. }
  601. static int __hellcreek_fdb_del(struct hellcreek *hellcreek,
  602. const struct hellcreek_fdb_entry *entry)
  603. {
  604. dev_dbg(hellcreek->dev, "Delete FDB entry: MAC=%pM!\n", entry->mac);
  605. /* Delete by matching idx */
  606. hellcreek_write(hellcreek, entry->idx | HR_FDBWRCMD_FDBDEL, HR_FDBWRCMD);
  607. /* Wait until done */
  608. return hellcreek_wait_fdb_ready(hellcreek);
  609. }
  610. static void hellcreek_populate_fdb_entry(struct hellcreek *hellcreek,
  611. struct hellcreek_fdb_entry *entry,
  612. size_t idx)
  613. {
  614. unsigned char addr[ETH_ALEN];
  615. u16 meta, mac;
  616. /* Read values */
  617. meta = hellcreek_read(hellcreek, HR_FDBMDRD);
  618. mac = hellcreek_read(hellcreek, HR_FDBRDL);
  619. addr[5] = mac & 0xff;
  620. addr[4] = (mac & 0xff00) >> 8;
  621. mac = hellcreek_read(hellcreek, HR_FDBRDM);
  622. addr[3] = mac & 0xff;
  623. addr[2] = (mac & 0xff00) >> 8;
  624. mac = hellcreek_read(hellcreek, HR_FDBRDH);
  625. addr[1] = mac & 0xff;
  626. addr[0] = (mac & 0xff00) >> 8;
  627. /* Populate @entry */
  628. memcpy(entry->mac, addr, sizeof(addr));
  629. entry->idx = idx;
  630. entry->portmask = (meta & HR_FDBMDRD_PORTMASK_MASK) >>
  631. HR_FDBMDRD_PORTMASK_SHIFT;
  632. entry->age = (meta & HR_FDBMDRD_AGE_MASK) >>
  633. HR_FDBMDRD_AGE_SHIFT;
  634. entry->is_obt = !!(meta & HR_FDBMDRD_OBT);
  635. entry->pass_blocked = !!(meta & HR_FDBMDRD_PASS_BLOCKED);
  636. entry->is_static = !!(meta & HR_FDBMDRD_STATIC);
  637. entry->reprio_tc = (meta & HR_FDBMDRD_REPRIO_TC_MASK) >>
  638. HR_FDBMDRD_REPRIO_TC_SHIFT;
  639. entry->reprio_en = !!(meta & HR_FDBMDRD_REPRIO_EN);
  640. }
  641. /* Retrieve the index of a FDB entry by mac address. Currently we search through
  642. * the complete table in hardware. If that's too slow, we might have to cache
  643. * the complete FDB table in software.
  644. */
  645. static int hellcreek_fdb_get(struct hellcreek *hellcreek,
  646. const unsigned char *dest,
  647. struct hellcreek_fdb_entry *entry)
  648. {
  649. size_t i;
  650. /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
  651. * should reset the internal pointer. But, that doesn't work. The vendor
  652. * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
  653. */
  654. hellcreek_read(hellcreek, HR_FDBMAX);
  655. hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
  656. /* We have to read the complete table, because the switch/driver might
  657. * enter new entries anywhere.
  658. */
  659. for (i = 0; i < hellcreek->fdb_entries; ++i) {
  660. struct hellcreek_fdb_entry tmp = { 0 };
  661. /* Read entry */
  662. hellcreek_populate_fdb_entry(hellcreek, &tmp, i);
  663. /* Force next entry */
  664. hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
  665. if (memcmp(tmp.mac, dest, ETH_ALEN))
  666. continue;
  667. /* Match found */
  668. memcpy(entry, &tmp, sizeof(*entry));
  669. return 0;
  670. }
  671. return -ENOENT;
  672. }
  673. static int hellcreek_fdb_add(struct dsa_switch *ds, int port,
  674. const unsigned char *addr, u16 vid,
  675. struct dsa_db db)
  676. {
  677. struct hellcreek_fdb_entry entry = { 0 };
  678. struct hellcreek *hellcreek = ds->priv;
  679. int ret;
  680. dev_dbg(hellcreek->dev, "Add FDB entry for MAC=%pM\n", addr);
  681. mutex_lock(&hellcreek->reg_lock);
  682. ret = hellcreek_fdb_get(hellcreek, addr, &entry);
  683. if (ret) {
  684. /* Not found */
  685. memcpy(entry.mac, addr, sizeof(entry.mac));
  686. entry.portmask = BIT(port);
  687. ret = __hellcreek_fdb_add(hellcreek, &entry);
  688. if (ret) {
  689. dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
  690. goto out;
  691. }
  692. } else {
  693. /* Found */
  694. ret = __hellcreek_fdb_del(hellcreek, &entry);
  695. if (ret) {
  696. dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
  697. goto out;
  698. }
  699. entry.portmask |= BIT(port);
  700. ret = __hellcreek_fdb_add(hellcreek, &entry);
  701. if (ret) {
  702. dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
  703. goto out;
  704. }
  705. }
  706. out:
  707. mutex_unlock(&hellcreek->reg_lock);
  708. return ret;
  709. }
  710. static int hellcreek_fdb_del(struct dsa_switch *ds, int port,
  711. const unsigned char *addr, u16 vid,
  712. struct dsa_db db)
  713. {
  714. struct hellcreek_fdb_entry entry = { 0 };
  715. struct hellcreek *hellcreek = ds->priv;
  716. int ret;
  717. dev_dbg(hellcreek->dev, "Delete FDB entry for MAC=%pM\n", addr);
  718. mutex_lock(&hellcreek->reg_lock);
  719. ret = hellcreek_fdb_get(hellcreek, addr, &entry);
  720. if (ret) {
  721. /* Not found */
  722. dev_err(hellcreek->dev, "FDB entry for deletion not found!\n");
  723. } else {
  724. /* Found */
  725. ret = __hellcreek_fdb_del(hellcreek, &entry);
  726. if (ret) {
  727. dev_err(hellcreek->dev, "Failed to delete FDB entry!\n");
  728. goto out;
  729. }
  730. entry.portmask &= ~BIT(port);
  731. if (entry.portmask != 0x00) {
  732. ret = __hellcreek_fdb_add(hellcreek, &entry);
  733. if (ret) {
  734. dev_err(hellcreek->dev, "Failed to add FDB entry!\n");
  735. goto out;
  736. }
  737. }
  738. }
  739. out:
  740. mutex_unlock(&hellcreek->reg_lock);
  741. return ret;
  742. }
  743. static int hellcreek_fdb_dump(struct dsa_switch *ds, int port,
  744. dsa_fdb_dump_cb_t *cb, void *data)
  745. {
  746. struct hellcreek *hellcreek = ds->priv;
  747. u16 entries;
  748. int ret = 0;
  749. size_t i;
  750. mutex_lock(&hellcreek->reg_lock);
  751. /* Set read pointer to zero: The read of HR_FDBMAX (read-only register)
  752. * should reset the internal pointer. But, that doesn't work. The vendor
  753. * suggested a subsequent write as workaround. Same for HR_FDBRDH below.
  754. */
  755. entries = hellcreek_read(hellcreek, HR_FDBMAX);
  756. hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
  757. dev_dbg(hellcreek->dev, "FDB dump for port %d, entries=%d!\n", port, entries);
  758. /* Read table */
  759. for (i = 0; i < hellcreek->fdb_entries; ++i) {
  760. struct hellcreek_fdb_entry entry = { 0 };
  761. /* Read entry */
  762. hellcreek_populate_fdb_entry(hellcreek, &entry, i);
  763. /* Force next entry */
  764. hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
  765. /* Check valid */
  766. if (is_zero_ether_addr(entry.mac))
  767. continue;
  768. /* Check port mask */
  769. if (!(entry.portmask & BIT(port)))
  770. continue;
  771. ret = cb(entry.mac, 0, entry.is_static, data);
  772. if (ret)
  773. break;
  774. }
  775. mutex_unlock(&hellcreek->reg_lock);
  776. return ret;
  777. }
  778. static int hellcreek_vlan_filtering(struct dsa_switch *ds, int port,
  779. bool vlan_filtering,
  780. struct netlink_ext_ack *extack)
  781. {
  782. struct hellcreek *hellcreek = ds->priv;
  783. dev_dbg(hellcreek->dev, "%s VLAN filtering on port %d\n",
  784. vlan_filtering ? "Enable" : "Disable", port);
  785. /* Configure port to drop packages with not known vids */
  786. hellcreek_setup_ingressflt(hellcreek, port, vlan_filtering);
  787. /* Enable VLAN awareness on the switch. This save due to
  788. * ds->vlan_filtering_is_global.
  789. */
  790. hellcreek_setup_vlan_awareness(hellcreek, vlan_filtering);
  791. return 0;
  792. }
  793. static int hellcreek_enable_ip_core(struct hellcreek *hellcreek)
  794. {
  795. int ret;
  796. u16 val;
  797. mutex_lock(&hellcreek->reg_lock);
  798. val = hellcreek_read(hellcreek, HR_CTRL_C);
  799. val |= HR_CTRL_C_ENABLE;
  800. hellcreek_write(hellcreek, val, HR_CTRL_C);
  801. ret = hellcreek_wait_until_transitioned(hellcreek);
  802. mutex_unlock(&hellcreek->reg_lock);
  803. return ret;
  804. }
  805. static void hellcreek_setup_cpu_and_tunnel_port(struct hellcreek *hellcreek)
  806. {
  807. struct hellcreek_port *tunnel_port = &hellcreek->ports[TUNNEL_PORT];
  808. struct hellcreek_port *cpu_port = &hellcreek->ports[CPU_PORT];
  809. u16 ptcfg = 0;
  810. ptcfg |= HR_PTCFG_LEARNING_EN | HR_PTCFG_ADMIN_EN;
  811. mutex_lock(&hellcreek->reg_lock);
  812. hellcreek_select_port(hellcreek, CPU_PORT);
  813. hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
  814. hellcreek_select_port(hellcreek, TUNNEL_PORT);
  815. hellcreek_write(hellcreek, ptcfg, HR_PTCFG);
  816. cpu_port->ptcfg = ptcfg;
  817. tunnel_port->ptcfg = ptcfg;
  818. mutex_unlock(&hellcreek->reg_lock);
  819. }
  820. static void hellcreek_setup_tc_identity_mapping(struct hellcreek *hellcreek)
  821. {
  822. int i;
  823. /* The switch has multiple egress queues per port. The queue is selected
  824. * via the PCP field in the VLAN header. The switch internally deals
  825. * with traffic classes instead of PCP values and this mapping is
  826. * configurable.
  827. *
  828. * The default mapping is (PCP - TC):
  829. * 7 - 7
  830. * 6 - 6
  831. * 5 - 5
  832. * 4 - 4
  833. * 3 - 3
  834. * 2 - 1
  835. * 1 - 0
  836. * 0 - 2
  837. *
  838. * The default should be an identity mapping.
  839. */
  840. for (i = 0; i < 8; ++i) {
  841. mutex_lock(&hellcreek->reg_lock);
  842. hellcreek_select_prio(hellcreek, i);
  843. hellcreek_write(hellcreek,
  844. i << HR_PRTCCFG_PCP_TC_MAP_SHIFT,
  845. HR_PRTCCFG);
  846. mutex_unlock(&hellcreek->reg_lock);
  847. }
  848. }
  849. static int hellcreek_setup_fdb(struct hellcreek *hellcreek)
  850. {
  851. static struct hellcreek_fdb_entry l2_ptp = {
  852. /* MAC: 01-1B-19-00-00-00 */
  853. .mac = { 0x01, 0x1b, 0x19, 0x00, 0x00, 0x00 },
  854. .portmask = 0x03, /* Management ports */
  855. .age = 0,
  856. .is_obt = 0,
  857. .pass_blocked = 0,
  858. .is_static = 1,
  859. .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
  860. .reprio_en = 1,
  861. };
  862. static struct hellcreek_fdb_entry udp4_ptp = {
  863. /* MAC: 01-00-5E-00-01-81 */
  864. .mac = { 0x01, 0x00, 0x5e, 0x00, 0x01, 0x81 },
  865. .portmask = 0x03, /* Management ports */
  866. .age = 0,
  867. .is_obt = 0,
  868. .pass_blocked = 0,
  869. .is_static = 1,
  870. .reprio_tc = 6,
  871. .reprio_en = 1,
  872. };
  873. static struct hellcreek_fdb_entry udp6_ptp = {
  874. /* MAC: 33-33-00-00-01-81 */
  875. .mac = { 0x33, 0x33, 0x00, 0x00, 0x01, 0x81 },
  876. .portmask = 0x03, /* Management ports */
  877. .age = 0,
  878. .is_obt = 0,
  879. .pass_blocked = 0,
  880. .is_static = 1,
  881. .reprio_tc = 6,
  882. .reprio_en = 1,
  883. };
  884. static struct hellcreek_fdb_entry l2_p2p = {
  885. /* MAC: 01-80-C2-00-00-0E */
  886. .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e },
  887. .portmask = 0x03, /* Management ports */
  888. .age = 0,
  889. .is_obt = 0,
  890. .pass_blocked = 1,
  891. .is_static = 1,
  892. .reprio_tc = 6, /* TC: 6 as per IEEE 802.1AS */
  893. .reprio_en = 1,
  894. };
  895. static struct hellcreek_fdb_entry udp4_p2p = {
  896. /* MAC: 01-00-5E-00-00-6B */
  897. .mac = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x6b },
  898. .portmask = 0x03, /* Management ports */
  899. .age = 0,
  900. .is_obt = 0,
  901. .pass_blocked = 1,
  902. .is_static = 1,
  903. .reprio_tc = 6,
  904. .reprio_en = 1,
  905. };
  906. static struct hellcreek_fdb_entry udp6_p2p = {
  907. /* MAC: 33-33-00-00-00-6B */
  908. .mac = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x6b },
  909. .portmask = 0x03, /* Management ports */
  910. .age = 0,
  911. .is_obt = 0,
  912. .pass_blocked = 1,
  913. .is_static = 1,
  914. .reprio_tc = 6,
  915. .reprio_en = 1,
  916. };
  917. static struct hellcreek_fdb_entry stp = {
  918. /* MAC: 01-80-C2-00-00-00 */
  919. .mac = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 },
  920. .portmask = 0x03, /* Management ports */
  921. .age = 0,
  922. .is_obt = 0,
  923. .pass_blocked = 1,
  924. .is_static = 1,
  925. .reprio_tc = 6,
  926. .reprio_en = 1,
  927. };
  928. int ret;
  929. mutex_lock(&hellcreek->reg_lock);
  930. ret = __hellcreek_fdb_add(hellcreek, &l2_ptp);
  931. if (ret)
  932. goto out;
  933. ret = __hellcreek_fdb_add(hellcreek, &udp4_ptp);
  934. if (ret)
  935. goto out;
  936. ret = __hellcreek_fdb_add(hellcreek, &udp6_ptp);
  937. if (ret)
  938. goto out;
  939. ret = __hellcreek_fdb_add(hellcreek, &l2_p2p);
  940. if (ret)
  941. goto out;
  942. ret = __hellcreek_fdb_add(hellcreek, &udp4_p2p);
  943. if (ret)
  944. goto out;
  945. ret = __hellcreek_fdb_add(hellcreek, &udp6_p2p);
  946. if (ret)
  947. goto out;
  948. ret = __hellcreek_fdb_add(hellcreek, &stp);
  949. out:
  950. mutex_unlock(&hellcreek->reg_lock);
  951. return ret;
  952. }
  953. static int hellcreek_devlink_info_get(struct dsa_switch *ds,
  954. struct devlink_info_req *req,
  955. struct netlink_ext_ack *extack)
  956. {
  957. struct hellcreek *hellcreek = ds->priv;
  958. return devlink_info_version_fixed_put(req,
  959. DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
  960. hellcreek->pdata->name);
  961. }
  962. static u64 hellcreek_devlink_vlan_table_get(void *priv)
  963. {
  964. struct hellcreek *hellcreek = priv;
  965. u64 count = 0;
  966. int i;
  967. mutex_lock(&hellcreek->reg_lock);
  968. for (i = 0; i < VLAN_N_VID; ++i)
  969. if (hellcreek->vidmbrcfg[i])
  970. count++;
  971. mutex_unlock(&hellcreek->reg_lock);
  972. return count;
  973. }
  974. static u64 hellcreek_devlink_fdb_table_get(void *priv)
  975. {
  976. struct hellcreek *hellcreek = priv;
  977. u64 count = 0;
  978. /* Reading this register has side effects. Synchronize against the other
  979. * FDB operations.
  980. */
  981. mutex_lock(&hellcreek->reg_lock);
  982. count = hellcreek_read(hellcreek, HR_FDBMAX);
  983. mutex_unlock(&hellcreek->reg_lock);
  984. return count;
  985. }
  986. static int hellcreek_setup_devlink_resources(struct dsa_switch *ds)
  987. {
  988. struct devlink_resource_size_params size_vlan_params;
  989. struct devlink_resource_size_params size_fdb_params;
  990. struct hellcreek *hellcreek = ds->priv;
  991. int err;
  992. devlink_resource_size_params_init(&size_vlan_params, VLAN_N_VID,
  993. VLAN_N_VID,
  994. 1, DEVLINK_RESOURCE_UNIT_ENTRY);
  995. devlink_resource_size_params_init(&size_fdb_params,
  996. hellcreek->fdb_entries,
  997. hellcreek->fdb_entries,
  998. 1, DEVLINK_RESOURCE_UNIT_ENTRY);
  999. err = dsa_devlink_resource_register(ds, "VLAN", VLAN_N_VID,
  1000. HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
  1001. DEVLINK_RESOURCE_ID_PARENT_TOP,
  1002. &size_vlan_params);
  1003. if (err)
  1004. goto out;
  1005. err = dsa_devlink_resource_register(ds, "FDB", hellcreek->fdb_entries,
  1006. HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
  1007. DEVLINK_RESOURCE_ID_PARENT_TOP,
  1008. &size_fdb_params);
  1009. if (err)
  1010. goto out;
  1011. dsa_devlink_resource_occ_get_register(ds,
  1012. HELLCREEK_DEVLINK_PARAM_ID_VLAN_TABLE,
  1013. hellcreek_devlink_vlan_table_get,
  1014. hellcreek);
  1015. dsa_devlink_resource_occ_get_register(ds,
  1016. HELLCREEK_DEVLINK_PARAM_ID_FDB_TABLE,
  1017. hellcreek_devlink_fdb_table_get,
  1018. hellcreek);
  1019. return 0;
  1020. out:
  1021. dsa_devlink_resources_unregister(ds);
  1022. return err;
  1023. }
  1024. static int hellcreek_devlink_region_vlan_snapshot(struct devlink *dl,
  1025. const struct devlink_region_ops *ops,
  1026. struct netlink_ext_ack *extack,
  1027. u8 **data)
  1028. {
  1029. struct hellcreek_devlink_vlan_entry *table, *entry;
  1030. struct dsa_switch *ds = dsa_devlink_to_ds(dl);
  1031. struct hellcreek *hellcreek = ds->priv;
  1032. int i;
  1033. table = kcalloc(VLAN_N_VID, sizeof(*entry), GFP_KERNEL);
  1034. if (!table)
  1035. return -ENOMEM;
  1036. entry = table;
  1037. mutex_lock(&hellcreek->reg_lock);
  1038. for (i = 0; i < VLAN_N_VID; ++i, ++entry) {
  1039. entry->member = hellcreek->vidmbrcfg[i];
  1040. entry->vid = i;
  1041. }
  1042. mutex_unlock(&hellcreek->reg_lock);
  1043. *data = (u8 *)table;
  1044. return 0;
  1045. }
  1046. static int hellcreek_devlink_region_fdb_snapshot(struct devlink *dl,
  1047. const struct devlink_region_ops *ops,
  1048. struct netlink_ext_ack *extack,
  1049. u8 **data)
  1050. {
  1051. struct dsa_switch *ds = dsa_devlink_to_ds(dl);
  1052. struct hellcreek_fdb_entry *table, *entry;
  1053. struct hellcreek *hellcreek = ds->priv;
  1054. size_t i;
  1055. table = kcalloc(hellcreek->fdb_entries, sizeof(*entry), GFP_KERNEL);
  1056. if (!table)
  1057. return -ENOMEM;
  1058. entry = table;
  1059. mutex_lock(&hellcreek->reg_lock);
  1060. /* Start table read */
  1061. hellcreek_read(hellcreek, HR_FDBMAX);
  1062. hellcreek_write(hellcreek, 0x00, HR_FDBMAX);
  1063. for (i = 0; i < hellcreek->fdb_entries; ++i, ++entry) {
  1064. /* Read current entry */
  1065. hellcreek_populate_fdb_entry(hellcreek, entry, i);
  1066. /* Advance read pointer */
  1067. hellcreek_write(hellcreek, 0x00, HR_FDBRDH);
  1068. }
  1069. mutex_unlock(&hellcreek->reg_lock);
  1070. *data = (u8 *)table;
  1071. return 0;
  1072. }
  1073. static struct devlink_region_ops hellcreek_region_vlan_ops = {
  1074. .name = "vlan",
  1075. .snapshot = hellcreek_devlink_region_vlan_snapshot,
  1076. .destructor = kfree,
  1077. };
  1078. static struct devlink_region_ops hellcreek_region_fdb_ops = {
  1079. .name = "fdb",
  1080. .snapshot = hellcreek_devlink_region_fdb_snapshot,
  1081. .destructor = kfree,
  1082. };
  1083. static int hellcreek_setup_devlink_regions(struct dsa_switch *ds)
  1084. {
  1085. struct hellcreek *hellcreek = ds->priv;
  1086. struct devlink_region_ops *ops;
  1087. struct devlink_region *region;
  1088. u64 size;
  1089. int ret;
  1090. /* VLAN table */
  1091. size = VLAN_N_VID * sizeof(struct hellcreek_devlink_vlan_entry);
  1092. ops = &hellcreek_region_vlan_ops;
  1093. region = dsa_devlink_region_create(ds, ops, 1, size);
  1094. if (IS_ERR(region))
  1095. return PTR_ERR(region);
  1096. hellcreek->vlan_region = region;
  1097. /* FDB table */
  1098. size = hellcreek->fdb_entries * sizeof(struct hellcreek_fdb_entry);
  1099. ops = &hellcreek_region_fdb_ops;
  1100. region = dsa_devlink_region_create(ds, ops, 1, size);
  1101. if (IS_ERR(region)) {
  1102. ret = PTR_ERR(region);
  1103. goto err_fdb;
  1104. }
  1105. hellcreek->fdb_region = region;
  1106. return 0;
  1107. err_fdb:
  1108. dsa_devlink_region_destroy(hellcreek->vlan_region);
  1109. return ret;
  1110. }
  1111. static void hellcreek_teardown_devlink_regions(struct dsa_switch *ds)
  1112. {
  1113. struct hellcreek *hellcreek = ds->priv;
  1114. dsa_devlink_region_destroy(hellcreek->fdb_region);
  1115. dsa_devlink_region_destroy(hellcreek->vlan_region);
  1116. }
  1117. static int hellcreek_setup(struct dsa_switch *ds)
  1118. {
  1119. struct hellcreek *hellcreek = ds->priv;
  1120. u16 swcfg = 0;
  1121. int ret, i;
  1122. dev_dbg(hellcreek->dev, "Set up the switch\n");
  1123. /* Let's go */
  1124. ret = hellcreek_enable_ip_core(hellcreek);
  1125. if (ret) {
  1126. dev_err(hellcreek->dev, "Failed to enable IP core!\n");
  1127. return ret;
  1128. }
  1129. /* Enable CPU/Tunnel ports */
  1130. hellcreek_setup_cpu_and_tunnel_port(hellcreek);
  1131. /* Switch config: Keep defaults, enable FDB aging and learning and tag
  1132. * each frame from/to cpu port for DSA tagging. Also enable the length
  1133. * aware shaping mode. This eliminates the need for Qbv guard bands.
  1134. */
  1135. swcfg |= HR_SWCFG_FDBAGE_EN |
  1136. HR_SWCFG_FDBLRN_EN |
  1137. HR_SWCFG_ALWAYS_OBT |
  1138. (HR_SWCFG_LAS_ON << HR_SWCFG_LAS_MODE_SHIFT);
  1139. hellcreek->swcfg = swcfg;
  1140. hellcreek_write(hellcreek, swcfg, HR_SWCFG);
  1141. /* Initial vlan membership to reflect port separation */
  1142. for (i = 0; i < ds->num_ports; ++i) {
  1143. if (!dsa_is_user_port(ds, i))
  1144. continue;
  1145. hellcreek_setup_vlan_membership(ds, i, true);
  1146. }
  1147. /* Configure PCP <-> TC mapping */
  1148. hellcreek_setup_tc_identity_mapping(hellcreek);
  1149. /* The VLAN awareness is a global switch setting. Therefore, mixed vlan
  1150. * filtering setups are not supported.
  1151. */
  1152. ds->vlan_filtering_is_global = true;
  1153. ds->needs_standalone_vlan_filtering = true;
  1154. /* Intercept _all_ PTP multicast traffic */
  1155. ret = hellcreek_setup_fdb(hellcreek);
  1156. if (ret) {
  1157. dev_err(hellcreek->dev,
  1158. "Failed to insert static PTP FDB entries\n");
  1159. return ret;
  1160. }
  1161. /* Register devlink resources with DSA */
  1162. ret = hellcreek_setup_devlink_resources(ds);
  1163. if (ret) {
  1164. dev_err(hellcreek->dev,
  1165. "Failed to setup devlink resources!\n");
  1166. return ret;
  1167. }
  1168. ret = hellcreek_setup_devlink_regions(ds);
  1169. if (ret) {
  1170. dev_err(hellcreek->dev,
  1171. "Failed to setup devlink regions!\n");
  1172. goto err_regions;
  1173. }
  1174. return 0;
  1175. err_regions:
  1176. dsa_devlink_resources_unregister(ds);
  1177. return ret;
  1178. }
  1179. static void hellcreek_teardown(struct dsa_switch *ds)
  1180. {
  1181. hellcreek_teardown_devlink_regions(ds);
  1182. dsa_devlink_resources_unregister(ds);
  1183. }
  1184. static void hellcreek_phylink_get_caps(struct dsa_switch *ds, int port,
  1185. struct phylink_config *config)
  1186. {
  1187. struct hellcreek *hellcreek = ds->priv;
  1188. __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
  1189. __set_bit(PHY_INTERFACE_MODE_RGMII, config->supported_interfaces);
  1190. /* Include GMII - the hardware does not support this interface
  1191. * mode, but it's the default interface mode for phylib, so we
  1192. * need it for compatibility with existing DT.
  1193. */
  1194. __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
  1195. /* The MAC settings are a hardware configuration option and cannot be
  1196. * changed at run time or by strapping. Therefore the attached PHYs
  1197. * should be programmed to only advertise settings which are supported
  1198. * by the hardware.
  1199. */
  1200. if (hellcreek->pdata->is_100_mbits)
  1201. config->mac_capabilities = MAC_100FD;
  1202. else
  1203. config->mac_capabilities = MAC_1000FD;
  1204. }
  1205. static int
  1206. hellcreek_port_prechangeupper(struct dsa_switch *ds, int port,
  1207. struct netdev_notifier_changeupper_info *info)
  1208. {
  1209. struct hellcreek *hellcreek = ds->priv;
  1210. bool used = true;
  1211. int ret = -EBUSY;
  1212. u16 vid;
  1213. int i;
  1214. dev_dbg(hellcreek->dev, "Pre change upper for port %d\n", port);
  1215. /*
  1216. * Deny VLAN devices on top of lan ports with the same VLAN ids, because
  1217. * it breaks the port separation due to the private VLANs. Example:
  1218. *
  1219. * lan0.100 *and* lan1.100 cannot be used in parallel. However, lan0.99
  1220. * and lan1.100 works.
  1221. */
  1222. if (!is_vlan_dev(info->upper_dev))
  1223. return 0;
  1224. vid = vlan_dev_vlan_id(info->upper_dev);
  1225. /* For all ports, check bitmaps */
  1226. mutex_lock(&hellcreek->vlan_lock);
  1227. for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
  1228. if (!dsa_is_user_port(ds, i))
  1229. continue;
  1230. if (port == i)
  1231. continue;
  1232. used = used && test_bit(vid, hellcreek->ports[i].vlan_dev_bitmap);
  1233. }
  1234. if (used)
  1235. goto out;
  1236. /* Update bitmap */
  1237. set_bit(vid, hellcreek->ports[port].vlan_dev_bitmap);
  1238. ret = 0;
  1239. out:
  1240. mutex_unlock(&hellcreek->vlan_lock);
  1241. return ret;
  1242. }
  1243. static void hellcreek_setup_maxsdu(struct hellcreek *hellcreek, int port,
  1244. const struct tc_taprio_qopt_offload *schedule)
  1245. {
  1246. int tc;
  1247. for (tc = 0; tc < 8; ++tc) {
  1248. u32 max_sdu = schedule->max_sdu[tc] + VLAN_ETH_HLEN - ETH_FCS_LEN;
  1249. u16 val;
  1250. if (!schedule->max_sdu[tc])
  1251. continue;
  1252. dev_dbg(hellcreek->dev, "Configure max-sdu %u for tc %d on port %d\n",
  1253. max_sdu, tc, port);
  1254. hellcreek_select_port_prio(hellcreek, port, tc);
  1255. val = (max_sdu & HR_PTPRTCCFG_MAXSDU_MASK) << HR_PTPRTCCFG_MAXSDU_SHIFT;
  1256. hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
  1257. }
  1258. }
  1259. static void hellcreek_reset_maxsdu(struct hellcreek *hellcreek, int port)
  1260. {
  1261. int tc;
  1262. for (tc = 0; tc < 8; ++tc) {
  1263. u16 val;
  1264. hellcreek_select_port_prio(hellcreek, port, tc);
  1265. val = (HELLCREEK_DEFAULT_MAX_SDU & HR_PTPRTCCFG_MAXSDU_MASK)
  1266. << HR_PTPRTCCFG_MAXSDU_SHIFT;
  1267. hellcreek_write(hellcreek, val, HR_PTPRTCCFG);
  1268. }
  1269. }
  1270. static void hellcreek_setup_gcl(struct hellcreek *hellcreek, int port,
  1271. const struct tc_taprio_qopt_offload *schedule)
  1272. {
  1273. const struct tc_taprio_sched_entry *cur, *initial, *next;
  1274. size_t i;
  1275. cur = initial = &schedule->entries[0];
  1276. next = cur + 1;
  1277. for (i = 1; i <= schedule->num_entries; ++i) {
  1278. u16 data;
  1279. u8 gates;
  1280. if (i == schedule->num_entries)
  1281. gates = initial->gate_mask ^
  1282. cur->gate_mask;
  1283. else
  1284. gates = next->gate_mask ^
  1285. cur->gate_mask;
  1286. data = gates;
  1287. if (i == schedule->num_entries)
  1288. data |= TR_GCLDAT_GCLWRLAST;
  1289. /* Gates states */
  1290. hellcreek_write(hellcreek, data, TR_GCLDAT);
  1291. /* Time interval */
  1292. hellcreek_write(hellcreek,
  1293. cur->interval & 0x0000ffff,
  1294. TR_GCLTIL);
  1295. hellcreek_write(hellcreek,
  1296. (cur->interval & 0xffff0000) >> 16,
  1297. TR_GCLTIH);
  1298. /* Commit entry */
  1299. data = ((i - 1) << TR_GCLCMD_GCLWRADR_SHIFT) |
  1300. (initial->gate_mask <<
  1301. TR_GCLCMD_INIT_GATE_STATES_SHIFT);
  1302. hellcreek_write(hellcreek, data, TR_GCLCMD);
  1303. cur++;
  1304. next++;
  1305. }
  1306. }
  1307. static void hellcreek_set_cycle_time(struct hellcreek *hellcreek,
  1308. const struct tc_taprio_qopt_offload *schedule)
  1309. {
  1310. u32 cycle_time = schedule->cycle_time;
  1311. hellcreek_write(hellcreek, cycle_time & 0x0000ffff, TR_CTWRL);
  1312. hellcreek_write(hellcreek, (cycle_time & 0xffff0000) >> 16, TR_CTWRH);
  1313. }
  1314. static void hellcreek_switch_schedule(struct hellcreek *hellcreek,
  1315. ktime_t start_time)
  1316. {
  1317. struct timespec64 ts = ktime_to_timespec64(start_time);
  1318. /* Start schedule at this point of time */
  1319. hellcreek_write(hellcreek, ts.tv_nsec & 0x0000ffff, TR_ESTWRL);
  1320. hellcreek_write(hellcreek, (ts.tv_nsec & 0xffff0000) >> 16, TR_ESTWRH);
  1321. /* Arm timer, set seconds and switch schedule */
  1322. hellcreek_write(hellcreek, TR_ESTCMD_ESTARM | TR_ESTCMD_ESTSWCFG |
  1323. ((ts.tv_sec & TR_ESTCMD_ESTSEC_MASK) <<
  1324. TR_ESTCMD_ESTSEC_SHIFT), TR_ESTCMD);
  1325. }
  1326. static bool hellcreek_schedule_startable(struct hellcreek *hellcreek, int port)
  1327. {
  1328. struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
  1329. s64 base_time_ns, current_ns;
  1330. /* The switch allows a schedule to be started only eight seconds within
  1331. * the future. Therefore, check the current PTP time if the schedule is
  1332. * startable or not.
  1333. */
  1334. /* Use the "cached" time. That should be alright, as it's updated quite
  1335. * frequently in the PTP code.
  1336. */
  1337. mutex_lock(&hellcreek->ptp_lock);
  1338. current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
  1339. mutex_unlock(&hellcreek->ptp_lock);
  1340. /* Calculate difference to admin base time */
  1341. base_time_ns = ktime_to_ns(hellcreek_port->current_schedule->base_time);
  1342. return base_time_ns - current_ns < (s64)4 * NSEC_PER_SEC;
  1343. }
  1344. static void hellcreek_start_schedule(struct hellcreek *hellcreek, int port)
  1345. {
  1346. struct hellcreek_port *hellcreek_port = &hellcreek->ports[port];
  1347. ktime_t base_time, current_time;
  1348. s64 current_ns;
  1349. u32 cycle_time;
  1350. /* First select port */
  1351. hellcreek_select_tgd(hellcreek, port);
  1352. /* Forward base time into the future if needed */
  1353. mutex_lock(&hellcreek->ptp_lock);
  1354. current_ns = hellcreek->seconds * NSEC_PER_SEC + hellcreek->last_ts;
  1355. mutex_unlock(&hellcreek->ptp_lock);
  1356. current_time = ns_to_ktime(current_ns);
  1357. base_time = hellcreek_port->current_schedule->base_time;
  1358. cycle_time = hellcreek_port->current_schedule->cycle_time;
  1359. if (ktime_compare(current_time, base_time) > 0) {
  1360. s64 n;
  1361. n = div64_s64(ktime_sub_ns(current_time, base_time),
  1362. cycle_time);
  1363. base_time = ktime_add_ns(base_time, (n + 1) * cycle_time);
  1364. }
  1365. /* Set admin base time and switch schedule */
  1366. hellcreek_switch_schedule(hellcreek, base_time);
  1367. taprio_offload_free(hellcreek_port->current_schedule);
  1368. hellcreek_port->current_schedule = NULL;
  1369. dev_dbg(hellcreek->dev, "Armed EST timer for port %d\n",
  1370. hellcreek_port->port);
  1371. }
  1372. static void hellcreek_check_schedule(struct work_struct *work)
  1373. {
  1374. struct delayed_work *dw = to_delayed_work(work);
  1375. struct hellcreek_port *hellcreek_port;
  1376. struct hellcreek *hellcreek;
  1377. bool startable;
  1378. hellcreek_port = dw_to_hellcreek_port(dw);
  1379. hellcreek = hellcreek_port->hellcreek;
  1380. mutex_lock(&hellcreek->reg_lock);
  1381. /* Check starting time */
  1382. startable = hellcreek_schedule_startable(hellcreek,
  1383. hellcreek_port->port);
  1384. if (startable) {
  1385. hellcreek_start_schedule(hellcreek, hellcreek_port->port);
  1386. mutex_unlock(&hellcreek->reg_lock);
  1387. return;
  1388. }
  1389. mutex_unlock(&hellcreek->reg_lock);
  1390. /* Reschedule */
  1391. schedule_delayed_work(&hellcreek_port->schedule_work,
  1392. HELLCREEK_SCHEDULE_PERIOD);
  1393. }
  1394. static int hellcreek_port_set_schedule(struct dsa_switch *ds, int port,
  1395. struct tc_taprio_qopt_offload *taprio)
  1396. {
  1397. struct hellcreek *hellcreek = ds->priv;
  1398. struct hellcreek_port *hellcreek_port;
  1399. bool startable;
  1400. u16 ctrl;
  1401. hellcreek_port = &hellcreek->ports[port];
  1402. dev_dbg(hellcreek->dev, "Configure traffic schedule on port %d\n",
  1403. port);
  1404. /* First cancel delayed work */
  1405. cancel_delayed_work_sync(&hellcreek_port->schedule_work);
  1406. mutex_lock(&hellcreek->reg_lock);
  1407. if (hellcreek_port->current_schedule) {
  1408. taprio_offload_free(hellcreek_port->current_schedule);
  1409. hellcreek_port->current_schedule = NULL;
  1410. }
  1411. hellcreek_port->current_schedule = taprio_offload_get(taprio);
  1412. /* Configure max sdu */
  1413. hellcreek_setup_maxsdu(hellcreek, port, hellcreek_port->current_schedule);
  1414. /* Select tdg */
  1415. hellcreek_select_tgd(hellcreek, port);
  1416. /* Enable gating and keep defaults */
  1417. ctrl = (0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT) | TR_TGDCTRL_GATE_EN;
  1418. hellcreek_write(hellcreek, ctrl, TR_TGDCTRL);
  1419. /* Cancel pending schedule */
  1420. hellcreek_write(hellcreek, 0x00, TR_ESTCMD);
  1421. /* Setup a new schedule */
  1422. hellcreek_setup_gcl(hellcreek, port, hellcreek_port->current_schedule);
  1423. /* Configure cycle time */
  1424. hellcreek_set_cycle_time(hellcreek, hellcreek_port->current_schedule);
  1425. /* Check starting time */
  1426. startable = hellcreek_schedule_startable(hellcreek, port);
  1427. if (startable) {
  1428. hellcreek_start_schedule(hellcreek, port);
  1429. mutex_unlock(&hellcreek->reg_lock);
  1430. return 0;
  1431. }
  1432. mutex_unlock(&hellcreek->reg_lock);
  1433. /* Schedule periodic schedule check */
  1434. schedule_delayed_work(&hellcreek_port->schedule_work,
  1435. HELLCREEK_SCHEDULE_PERIOD);
  1436. return 0;
  1437. }
  1438. static int hellcreek_port_del_schedule(struct dsa_switch *ds, int port)
  1439. {
  1440. struct hellcreek *hellcreek = ds->priv;
  1441. struct hellcreek_port *hellcreek_port;
  1442. hellcreek_port = &hellcreek->ports[port];
  1443. dev_dbg(hellcreek->dev, "Remove traffic schedule on port %d\n", port);
  1444. /* First cancel delayed work */
  1445. cancel_delayed_work_sync(&hellcreek_port->schedule_work);
  1446. mutex_lock(&hellcreek->reg_lock);
  1447. if (hellcreek_port->current_schedule) {
  1448. taprio_offload_free(hellcreek_port->current_schedule);
  1449. hellcreek_port->current_schedule = NULL;
  1450. }
  1451. /* Reset max sdu */
  1452. hellcreek_reset_maxsdu(hellcreek, port);
  1453. /* Select tgd */
  1454. hellcreek_select_tgd(hellcreek, port);
  1455. /* Disable gating and return to regular switching flow */
  1456. hellcreek_write(hellcreek, 0xff << TR_TGDCTRL_ADMINGATESTATES_SHIFT,
  1457. TR_TGDCTRL);
  1458. mutex_unlock(&hellcreek->reg_lock);
  1459. return 0;
  1460. }
  1461. static bool hellcreek_validate_schedule(struct hellcreek *hellcreek,
  1462. struct tc_taprio_qopt_offload *schedule)
  1463. {
  1464. size_t i;
  1465. /* Does this hellcreek version support Qbv in hardware? */
  1466. if (!hellcreek->pdata->qbv_support)
  1467. return false;
  1468. /* cycle time can only be 32bit */
  1469. if (schedule->cycle_time > (u32)-1)
  1470. return false;
  1471. /* cycle time extension is not supported */
  1472. if (schedule->cycle_time_extension)
  1473. return false;
  1474. /* Only set command is supported */
  1475. for (i = 0; i < schedule->num_entries; ++i)
  1476. if (schedule->entries[i].command != TC_TAPRIO_CMD_SET_GATES)
  1477. return false;
  1478. return true;
  1479. }
  1480. static int hellcreek_tc_query_caps(struct tc_query_caps_base *base)
  1481. {
  1482. switch (base->type) {
  1483. case TC_SETUP_QDISC_TAPRIO: {
  1484. struct tc_taprio_caps *caps = base->caps;
  1485. caps->supports_queue_max_sdu = true;
  1486. return 0;
  1487. }
  1488. default:
  1489. return -EOPNOTSUPP;
  1490. }
  1491. }
  1492. static int hellcreek_port_setup_tc(struct dsa_switch *ds, int port,
  1493. enum tc_setup_type type, void *type_data)
  1494. {
  1495. struct hellcreek *hellcreek = ds->priv;
  1496. switch (type) {
  1497. case TC_QUERY_CAPS:
  1498. return hellcreek_tc_query_caps(type_data);
  1499. case TC_SETUP_QDISC_TAPRIO: {
  1500. struct tc_taprio_qopt_offload *taprio = type_data;
  1501. switch (taprio->cmd) {
  1502. case TAPRIO_CMD_REPLACE:
  1503. if (!hellcreek_validate_schedule(hellcreek, taprio))
  1504. return -EOPNOTSUPP;
  1505. return hellcreek_port_set_schedule(ds, port, taprio);
  1506. case TAPRIO_CMD_DESTROY:
  1507. return hellcreek_port_del_schedule(ds, port);
  1508. default:
  1509. return -EOPNOTSUPP;
  1510. }
  1511. }
  1512. default:
  1513. return -EOPNOTSUPP;
  1514. }
  1515. }
  1516. static const struct dsa_switch_ops hellcreek_ds_ops = {
  1517. .devlink_info_get = hellcreek_devlink_info_get,
  1518. .get_ethtool_stats = hellcreek_get_ethtool_stats,
  1519. .get_sset_count = hellcreek_get_sset_count,
  1520. .get_strings = hellcreek_get_strings,
  1521. .get_tag_protocol = hellcreek_get_tag_protocol,
  1522. .get_ts_info = hellcreek_get_ts_info,
  1523. .phylink_get_caps = hellcreek_phylink_get_caps,
  1524. .port_bridge_flags = hellcreek_bridge_flags,
  1525. .port_bridge_join = hellcreek_port_bridge_join,
  1526. .port_bridge_leave = hellcreek_port_bridge_leave,
  1527. .port_disable = hellcreek_port_disable,
  1528. .port_enable = hellcreek_port_enable,
  1529. .port_fdb_add = hellcreek_fdb_add,
  1530. .port_fdb_del = hellcreek_fdb_del,
  1531. .port_fdb_dump = hellcreek_fdb_dump,
  1532. .port_hwtstamp_set = hellcreek_port_hwtstamp_set,
  1533. .port_hwtstamp_get = hellcreek_port_hwtstamp_get,
  1534. .port_pre_bridge_flags = hellcreek_pre_bridge_flags,
  1535. .port_prechangeupper = hellcreek_port_prechangeupper,
  1536. .port_rxtstamp = hellcreek_port_rxtstamp,
  1537. .port_setup_tc = hellcreek_port_setup_tc,
  1538. .port_stp_state_set = hellcreek_port_stp_state_set,
  1539. .port_txtstamp = hellcreek_port_txtstamp,
  1540. .port_vlan_add = hellcreek_vlan_add,
  1541. .port_vlan_del = hellcreek_vlan_del,
  1542. .port_vlan_filtering = hellcreek_vlan_filtering,
  1543. .setup = hellcreek_setup,
  1544. .teardown = hellcreek_teardown,
  1545. };
  1546. static int hellcreek_probe(struct platform_device *pdev)
  1547. {
  1548. struct device *dev = &pdev->dev;
  1549. struct hellcreek *hellcreek;
  1550. struct resource *res;
  1551. int ret, i;
  1552. hellcreek = devm_kzalloc(dev, sizeof(*hellcreek), GFP_KERNEL);
  1553. if (!hellcreek)
  1554. return -ENOMEM;
  1555. hellcreek->vidmbrcfg = devm_kcalloc(dev, VLAN_N_VID,
  1556. sizeof(*hellcreek->vidmbrcfg),
  1557. GFP_KERNEL);
  1558. if (!hellcreek->vidmbrcfg)
  1559. return -ENOMEM;
  1560. hellcreek->pdata = of_device_get_match_data(dev);
  1561. hellcreek->ports = devm_kcalloc(dev, hellcreek->pdata->num_ports,
  1562. sizeof(*hellcreek->ports),
  1563. GFP_KERNEL);
  1564. if (!hellcreek->ports)
  1565. return -ENOMEM;
  1566. for (i = 0; i < hellcreek->pdata->num_ports; ++i) {
  1567. struct hellcreek_port *port = &hellcreek->ports[i];
  1568. port->counter_values =
  1569. devm_kcalloc(dev,
  1570. ARRAY_SIZE(hellcreek_counter),
  1571. sizeof(*port->counter_values),
  1572. GFP_KERNEL);
  1573. if (!port->counter_values)
  1574. return -ENOMEM;
  1575. port->vlan_dev_bitmap = devm_bitmap_zalloc(dev, VLAN_N_VID,
  1576. GFP_KERNEL);
  1577. if (!port->vlan_dev_bitmap)
  1578. return -ENOMEM;
  1579. port->hellcreek = hellcreek;
  1580. port->port = i;
  1581. INIT_DELAYED_WORK(&port->schedule_work,
  1582. hellcreek_check_schedule);
  1583. }
  1584. mutex_init(&hellcreek->reg_lock);
  1585. mutex_init(&hellcreek->vlan_lock);
  1586. mutex_init(&hellcreek->ptp_lock);
  1587. hellcreek->dev = dev;
  1588. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tsn");
  1589. if (!res) {
  1590. dev_err(dev, "No memory region provided!\n");
  1591. return -ENODEV;
  1592. }
  1593. hellcreek->base = devm_ioremap_resource(dev, res);
  1594. if (IS_ERR(hellcreek->base))
  1595. return PTR_ERR(hellcreek->base);
  1596. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ptp");
  1597. if (!res) {
  1598. dev_err(dev, "No PTP memory region provided!\n");
  1599. return -ENODEV;
  1600. }
  1601. hellcreek->ptp_base = devm_ioremap_resource(dev, res);
  1602. if (IS_ERR(hellcreek->ptp_base))
  1603. return PTR_ERR(hellcreek->ptp_base);
  1604. ret = hellcreek_detect(hellcreek);
  1605. if (ret) {
  1606. dev_err(dev, "No (known) chip found!\n");
  1607. return ret;
  1608. }
  1609. ret = hellcreek_wait_until_ready(hellcreek);
  1610. if (ret) {
  1611. dev_err(dev, "Switch didn't become ready!\n");
  1612. return ret;
  1613. }
  1614. hellcreek_feature_detect(hellcreek);
  1615. hellcreek->ds = devm_kzalloc(dev, sizeof(*hellcreek->ds), GFP_KERNEL);
  1616. if (!hellcreek->ds)
  1617. return -ENOMEM;
  1618. hellcreek->ds->dev = dev;
  1619. hellcreek->ds->priv = hellcreek;
  1620. hellcreek->ds->ops = &hellcreek_ds_ops;
  1621. hellcreek->ds->num_ports = hellcreek->pdata->num_ports;
  1622. hellcreek->ds->num_tx_queues = HELLCREEK_NUM_EGRESS_QUEUES;
  1623. ret = dsa_register_switch(hellcreek->ds);
  1624. if (ret) {
  1625. dev_err_probe(dev, ret, "Unable to register switch\n");
  1626. return ret;
  1627. }
  1628. ret = hellcreek_ptp_setup(hellcreek);
  1629. if (ret) {
  1630. dev_err(dev, "Failed to setup PTP!\n");
  1631. goto err_ptp_setup;
  1632. }
  1633. ret = hellcreek_hwtstamp_setup(hellcreek);
  1634. if (ret) {
  1635. dev_err(dev, "Failed to setup hardware timestamping!\n");
  1636. goto err_tstamp_setup;
  1637. }
  1638. platform_set_drvdata(pdev, hellcreek);
  1639. return 0;
  1640. err_tstamp_setup:
  1641. hellcreek_ptp_free(hellcreek);
  1642. err_ptp_setup:
  1643. dsa_unregister_switch(hellcreek->ds);
  1644. return ret;
  1645. }
  1646. static void hellcreek_remove(struct platform_device *pdev)
  1647. {
  1648. struct hellcreek *hellcreek = platform_get_drvdata(pdev);
  1649. if (!hellcreek)
  1650. return;
  1651. hellcreek_hwtstamp_free(hellcreek);
  1652. hellcreek_ptp_free(hellcreek);
  1653. dsa_unregister_switch(hellcreek->ds);
  1654. }
  1655. static void hellcreek_shutdown(struct platform_device *pdev)
  1656. {
  1657. struct hellcreek *hellcreek = platform_get_drvdata(pdev);
  1658. if (!hellcreek)
  1659. return;
  1660. dsa_switch_shutdown(hellcreek->ds);
  1661. platform_set_drvdata(pdev, NULL);
  1662. }
  1663. static const struct hellcreek_platform_data de1soc_r1_pdata = {
  1664. .name = "r4c30",
  1665. .num_ports = 4,
  1666. .is_100_mbits = 1,
  1667. .qbv_support = 1,
  1668. .qbv_on_cpu_port = 1,
  1669. .qbu_support = 0,
  1670. .module_id = 0x4c30,
  1671. };
  1672. static const struct of_device_id hellcreek_of_match[] = {
  1673. {
  1674. .compatible = "hirschmann,hellcreek-de1soc-r1",
  1675. .data = &de1soc_r1_pdata,
  1676. },
  1677. { /* sentinel */ },
  1678. };
  1679. MODULE_DEVICE_TABLE(of, hellcreek_of_match);
  1680. static struct platform_driver hellcreek_driver = {
  1681. .probe = hellcreek_probe,
  1682. .remove_new = hellcreek_remove,
  1683. .shutdown = hellcreek_shutdown,
  1684. .driver = {
  1685. .name = "hellcreek",
  1686. .of_match_table = hellcreek_of_match,
  1687. },
  1688. };
  1689. module_platform_driver(hellcreek_driver);
  1690. MODULE_AUTHOR("Kurt Kanzenbach <kurt@linutronix.de>");
  1691. MODULE_DESCRIPTION("Hirschmann Hellcreek driver");
  1692. MODULE_LICENSE("Dual MIT/GPL");