hellcreek.c 53 KB

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