rtl931x.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  3. #include <linux/etherdevice.h>
  4. #include "rtl83xx.h"
  5. #define RTL931X_VLAN_PORT_TAG_STS_INTERNAL 0x0
  6. #define RTL931X_VLAN_PORT_TAG_STS_UNTAG 0x1
  7. #define RTL931X_VLAN_PORT_TAG_STS_TAGGED 0x2
  8. #define RTL931X_VLAN_PORT_TAG_STS_PRIORITY_TAGGED 0x3
  9. #define RTL931X_VLAN_PORT_TAG_CTRL_BASE 0x4860
  10. /* port 0-56 */
  11. #define RTL931X_VLAN_PORT_TAG_CTRL(port) \
  12. RTL931X_VLAN_PORT_TAG_CTRL_BASE + (port << 2)
  13. #define RTL931X_VLAN_PORT_TAG_EGR_OTAG_STS_MASK GENMASK(13,12)
  14. #define RTL931X_VLAN_PORT_TAG_EGR_ITAG_STS_MASK GENMASK(11,10)
  15. #define RTL931X_VLAN_PORT_TAG_EGR_OTAG_KEEP_MASK GENMASK(9,9)
  16. #define RTL931X_VLAN_PORT_TAG_EGR_ITAG_KEEP_MASK GENMASK(8,8)
  17. #define RTL931X_VLAN_PORT_TAG_IGR_OTAG_KEEP_MASK GENMASK(7,7)
  18. #define RTL931X_VLAN_PORT_TAG_IGR_ITAG_KEEP_MASK GENMASK(6,6)
  19. #define RTL931X_VLAN_PORT_TAG_OTPID_IDX_MASK GENMASK(5,4)
  20. #define RTL931X_VLAN_PORT_TAG_OTPID_KEEP_MASK GENMASK(3,3)
  21. #define RTL931X_VLAN_PORT_TAG_ITPID_IDX_MASK GENMASK(2,1)
  22. #define RTL931X_VLAN_PORT_TAG_ITPID_KEEP_MASK GENMASK(0,0)
  23. extern struct mutex smi_lock;
  24. extern struct rtl83xx_soc_info soc_info;
  25. /* Definition of the RTL931X-specific template field IDs as used in the PIE */
  26. enum template_field_id {
  27. TEMPLATE_FIELD_SPM0 = 1,
  28. TEMPLATE_FIELD_SPM1 = 2,
  29. TEMPLATE_FIELD_SPM2 = 3,
  30. TEMPLATE_FIELD_SPM3 = 4,
  31. TEMPLATE_FIELD_DMAC0 = 9,
  32. TEMPLATE_FIELD_DMAC1 = 10,
  33. TEMPLATE_FIELD_DMAC2 = 11,
  34. TEMPLATE_FIELD_SMAC0 = 12,
  35. TEMPLATE_FIELD_SMAC1 = 13,
  36. TEMPLATE_FIELD_SMAC2 = 14,
  37. TEMPLATE_FIELD_ETHERTYPE = 15,
  38. TEMPLATE_FIELD_OTAG = 16,
  39. TEMPLATE_FIELD_ITAG = 17,
  40. TEMPLATE_FIELD_SIP0 = 18,
  41. TEMPLATE_FIELD_SIP1 = 19,
  42. TEMPLATE_FIELD_DIP0 = 20,
  43. TEMPLATE_FIELD_DIP1 = 21,
  44. TEMPLATE_FIELD_IP_TOS_PROTO = 22,
  45. TEMPLATE_FIELD_L4_SPORT = 23,
  46. TEMPLATE_FIELD_L4_DPORT = 24,
  47. TEMPLATE_FIELD_L34_HEADER = 25,
  48. TEMPLATE_FIELD_TCP_INFO = 26,
  49. TEMPLATE_FIELD_SIP2 = 34,
  50. TEMPLATE_FIELD_SIP3 = 35,
  51. TEMPLATE_FIELD_SIP4 = 36,
  52. TEMPLATE_FIELD_SIP5 = 37,
  53. TEMPLATE_FIELD_SIP6 = 38,
  54. TEMPLATE_FIELD_SIP7 = 39,
  55. TEMPLATE_FIELD_DIP2 = 42,
  56. TEMPLATE_FIELD_DIP3 = 43,
  57. TEMPLATE_FIELD_DIP4 = 44,
  58. TEMPLATE_FIELD_DIP5 = 45,
  59. TEMPLATE_FIELD_DIP6 = 46,
  60. TEMPLATE_FIELD_DIP7 = 47,
  61. TEMPLATE_FIELD_FLOW_LABEL = 49,
  62. TEMPLATE_FIELD_DSAP_SSAP = 50,
  63. TEMPLATE_FIELD_FWD_VID = 52,
  64. TEMPLATE_FIELD_RANGE_CHK = 53,
  65. TEMPLATE_FIELD_SLP = 55,
  66. TEMPLATE_FIELD_DLP = 56,
  67. TEMPLATE_FIELD_META_DATA = 57,
  68. TEMPLATE_FIELD_FIRST_MPLS1 = 60,
  69. TEMPLATE_FIELD_FIRST_MPLS2 = 61,
  70. TEMPLATE_FIELD_DPM3 = 8,
  71. };
  72. /* The meaning of TEMPLATE_FIELD_VLAN depends on phase and the configuration in
  73. * RTL931X_PIE_CTRL. We use always the same definition and map to the inner VLAN tag:
  74. */
  75. #define TEMPLATE_FIELD_VLAN TEMPLATE_FIELD_ITAG
  76. /* Number of fixed templates predefined in the RTL9300 SoC */
  77. #define N_FIXED_TEMPLATES 5
  78. /* RTL931x specific predefined templates */
  79. static enum template_field_id fixed_templates[N_FIXED_TEMPLATES][N_FIXED_FIELDS_RTL931X] =
  80. {
  81. {
  82. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  83. TEMPLATE_FIELD_SMAC0, TEMPLATE_FIELD_SMAC1, TEMPLATE_FIELD_SMAC2,
  84. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_DSAP_SSAP,
  85. TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  86. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  87. }, {
  88. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  89. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_TCP_INFO,
  90. TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT, TEMPLATE_FIELD_VLAN,
  91. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  92. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  93. }, {
  94. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  95. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_IP_TOS_PROTO,
  96. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  97. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT,
  98. TEMPLATE_FIELD_META_DATA, TEMPLATE_FIELD_SLP
  99. }, {
  100. TEMPLATE_FIELD_DIP0, TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_DIP2,
  101. TEMPLATE_FIELD_DIP3, TEMPLATE_FIELD_DIP4, TEMPLATE_FIELD_DIP5,
  102. TEMPLATE_FIELD_DIP6, TEMPLATE_FIELD_DIP7, TEMPLATE_FIELD_IP_TOS_PROTO,
  103. TEMPLATE_FIELD_TCP_INFO, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT,
  104. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SLP
  105. }, {
  106. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_SIP2,
  107. TEMPLATE_FIELD_SIP3, TEMPLATE_FIELD_SIP4, TEMPLATE_FIELD_SIP5,
  108. TEMPLATE_FIELD_SIP6, TEMPLATE_FIELD_SIP7, TEMPLATE_FIELD_META_DATA,
  109. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  110. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  111. },
  112. };
  113. inline void rtl931x_exec_tbl0_cmd(u32 cmd)
  114. {
  115. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_0);
  116. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_0) & (1 << 20));
  117. }
  118. inline void rtl931x_exec_tbl1_cmd(u32 cmd)
  119. {
  120. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_1);
  121. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_1) & (1 << 17));
  122. }
  123. inline int rtl931x_tbl_access_data_0(int i)
  124. {
  125. return RTL931X_TBL_ACCESS_DATA_0(i);
  126. }
  127. void rtl931x_vlan_profile_dump(int index)
  128. {
  129. u64 profile[4];
  130. if (index < 0 || index > 15)
  131. return;
  132. profile[0] = sw_r32(RTL931X_VLAN_PROFILE_SET(index));
  133. profile[1] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 4) & 0x1FFFFFFFULL) << 32 |
  134. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 8) & 0xFFFFFFFF);
  135. profile[2] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 16) & 0x1FFFFFFFULL) << 32 |
  136. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 12) & 0xFFFFFFFF);
  137. profile[3] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 20) & 0x1FFFFFFFULL) << 32 |
  138. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 24) & 0xFFFFFFFF);
  139. pr_info("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %llx, \
  140. IPv4 Unknown MultiCast Field %llx, IPv6 Unknown MultiCast Field: %llx",
  141. index, (u32) (profile[0] & (3 << 14)), profile[1], profile[2], profile[3]);
  142. }
  143. static void rtl931x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  144. {
  145. u32 cmd = 1 << 20 | /* Execute cmd */
  146. 0 << 19 | /* Read */
  147. 5 << 15 | /* Table type 0b101 */
  148. (msti & 0x3fff);
  149. priv->r->exec_tbl0_cmd(cmd);
  150. for (int i = 0; i < 4; i++)
  151. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  152. }
  153. static void rtl931x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  154. {
  155. u32 cmd = 1 << 20 | /* Execute cmd */
  156. 1 << 19 | /* Write */
  157. 5 << 15 | /* Table type 0b101 */
  158. (msti & 0x3fff);
  159. for (int i = 0; i < 4; i++)
  160. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  161. priv->r->exec_tbl0_cmd(cmd);
  162. }
  163. inline static int rtl931x_trk_mbr_ctr(int group)
  164. {
  165. return RTL931X_TRK_MBR_CTRL + (group << 2);
  166. }
  167. static void rtl931x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  168. {
  169. u32 v, w, x, y;
  170. /* Read VLAN table (3) via register 0 */
  171. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  172. rtl_table_read(r, vlan);
  173. v = sw_r32(rtl_table_data(r, 0));
  174. w = sw_r32(rtl_table_data(r, 1));
  175. x = sw_r32(rtl_table_data(r, 2));
  176. y = sw_r32(rtl_table_data(r, 3));
  177. rtl_table_release(r);
  178. pr_debug("VLAN_READ %d: %08x %08x %08x %08x\n", vlan, v, w, x, y);
  179. info->tagged_ports = ((u64) v) << 25 | (w >> 7);
  180. info->profile_id = (x >> 16) & 0xf;
  181. info->fid = w & 0x7f; /* AKA MSTI depending on context */
  182. info->hash_uc_fid = !!(x & BIT(31));
  183. info->hash_mc_fid = !!(x & BIT(30));
  184. info->if_id = (x >> 20) & 0x3ff;
  185. info->profile_id = (x >> 16) & 0xf;
  186. info->multicast_grp_mask = x & 0xffff;
  187. if (x & BIT(31))
  188. info->l2_tunnel_list_id = y >> 18;
  189. else
  190. info->l2_tunnel_list_id = -1;
  191. pr_debug("%s read tagged %016llx, profile-id %d, uc %d, mc %d, intf-id %d\n", __func__,
  192. info->tagged_ports, info->profile_id, info->hash_uc_fid, info->hash_mc_fid,
  193. info->if_id);
  194. /* Read UNTAG table via table register 3 */
  195. r = rtl_table_get(RTL9310_TBL_3, 0);
  196. rtl_table_read(r, vlan);
  197. v = ((u64)sw_r32(rtl_table_data(r, 0))) << 25;
  198. v |= sw_r32(rtl_table_data(r, 1)) >> 7;
  199. rtl_table_release(r);
  200. info->untagged_ports = v;
  201. }
  202. static void rtl931x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  203. {
  204. u32 v, w, x, y;
  205. /* Access VLAN table (1) via register 0 */
  206. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  207. v = info->tagged_ports >> 25;
  208. w = (info->tagged_ports & 0x1fffff) << 7;
  209. w |= info->fid & 0x7f;
  210. x = info->hash_uc_fid ? BIT(31) : 0;
  211. x |= info->hash_mc_fid ? BIT(30) : 0;
  212. x |= info->if_id & 0x3ff << 20;
  213. x |= (info->profile_id & 0xf) << 16;
  214. x |= info->multicast_grp_mask & 0xffff;
  215. if (info->l2_tunnel_list_id >= 0) {
  216. y = info->l2_tunnel_list_id << 18;
  217. y |= BIT(31);
  218. } else {
  219. y = 0;
  220. }
  221. sw_w32(v, rtl_table_data(r, 0));
  222. sw_w32(w, rtl_table_data(r, 1));
  223. sw_w32(x, rtl_table_data(r, 2));
  224. sw_w32(y, rtl_table_data(r, 3));
  225. rtl_table_write(r, vlan);
  226. rtl_table_release(r);
  227. }
  228. static void rtl931x_vlan_set_untagged(u32 vlan, u64 portmask)
  229. {
  230. struct table_reg *r = rtl_table_get(RTL9310_TBL_3, 0);
  231. rtl839x_set_port_reg_be(portmask << 7, rtl_table_data(r, 0));
  232. rtl_table_write(r, vlan);
  233. rtl_table_release(r);
  234. }
  235. static inline int rtl931x_mac_force_mode_ctrl(int p)
  236. {
  237. return RTL931X_MAC_FORCE_MODE_CTRL + (p << 2);
  238. }
  239. static inline int rtl931x_mac_link_spd_sts(int p)
  240. {
  241. return RTL931X_MAC_LINK_SPD_STS + (((p >> 3) << 2));
  242. }
  243. static inline int rtl931x_mac_port_ctrl(int p)
  244. {
  245. return RTL931X_MAC_L2_PORT_CTRL + (p << 7);
  246. }
  247. static inline int rtl931x_l2_port_new_salrn(int p)
  248. {
  249. return RTL931X_L2_PORT_NEW_SALRN(p);
  250. }
  251. static inline int rtl931x_l2_port_new_sa_fwd(int p)
  252. {
  253. return RTL931X_L2_PORT_NEW_SA_FWD(p);
  254. }
  255. irqreturn_t rtl931x_switch_irq(int irq, void *dev_id)
  256. {
  257. struct dsa_switch *ds = dev_id;
  258. u32 status = sw_r32(RTL931X_ISR_GLB_SRC);
  259. u64 ports = rtl839x_get_port_reg_le(RTL931X_ISR_PORT_LINK_STS_CHG);
  260. u64 link;
  261. /* Clear status */
  262. rtl839x_set_port_reg_le(ports, RTL931X_ISR_PORT_LINK_STS_CHG);
  263. pr_debug("RTL931X Link change: status: %x, ports %016llx\n", status, ports);
  264. link = rtl839x_get_port_reg_le(RTL931X_MAC_LINK_STS);
  265. /* Must re-read this to get correct status */
  266. link = rtl839x_get_port_reg_le(RTL931X_MAC_LINK_STS);
  267. pr_debug("RTL931X Link change: status: %x, link status %016llx\n", status, link);
  268. for (int i = 0; i < 56; i++) {
  269. if (ports & BIT_ULL(i)) {
  270. if (link & BIT_ULL(i)) {
  271. pr_info("%s port %d up\n", __func__, i);
  272. dsa_port_phylink_mac_change(ds, i, true);
  273. } else {
  274. pr_info("%s port %d down\n", __func__, i);
  275. dsa_port_phylink_mac_change(ds, i, false);
  276. }
  277. }
  278. }
  279. return IRQ_HANDLED;
  280. }
  281. int rtl931x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  282. {
  283. u32 v;
  284. int err = 0;
  285. val &= 0xffff;
  286. if (port > 63 || page > 4095 || reg > 31)
  287. return -ENOTSUPP;
  288. mutex_lock(&smi_lock);
  289. pr_debug("%s: writing to phy %d %d %d %d\n", __func__, port, page, reg, val);
  290. /* Clear both port registers */
  291. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2);
  292. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2 + 4);
  293. sw_w32_mask(0, BIT(port % 32), RTL931X_SMI_INDRT_ACCESS_CTRL_2 + (port / 32) * 4);
  294. sw_w32_mask(0xffff, val, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  295. v = reg << 6 | page << 11 ;
  296. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  297. sw_w32(0x1ff, RTL931X_SMI_INDRT_ACCESS_CTRL_1);
  298. v |= BIT(4) | 1; /* Write operation and execute */
  299. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  300. do {
  301. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  302. if (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x2)
  303. err = -EIO;
  304. mutex_unlock(&smi_lock);
  305. return err;
  306. }
  307. int rtl931x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  308. {
  309. u32 v;
  310. if (port > 63 || page > 4095 || reg > 31)
  311. return -ENOTSUPP;
  312. mutex_lock(&smi_lock);
  313. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  314. v = reg << 6 | page << 11 | 1;
  315. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  316. do {
  317. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  318. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  319. *val = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  320. *val = (*val & 0xffff0000) >> 16;
  321. pr_debug("%s: port %d, page: %d, reg: %x, val: %x, v: %08x\n",
  322. __func__, port, page, reg, *val, v);
  323. mutex_unlock(&smi_lock);
  324. return 0;
  325. }
  326. /* Read an mmd register of the PHY */
  327. int rtl931x_read_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 *val)
  328. {
  329. int err = 0;
  330. u32 v;
  331. /* Select PHY register type
  332. * If select 1G/10G MMD register type, registers EXT_PAGE, MAIN_PAGE and REG settings are don’t care.
  333. * 0x0 Normal register (Clause 22)
  334. * 0x1: 1G MMD register (MMD via Clause 22 registers 13 and 14)
  335. * 0x2: 10G MMD register (MMD via Clause 45)
  336. */
  337. int type = (regnum & MII_ADDR_C45)?2:1;
  338. mutex_lock(&smi_lock);
  339. /* Set PHY to access via port-number */
  340. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  341. /* Set MMD device number and register to write to */
  342. sw_w32(devnum << 16 | mdiobus_c45_regad(regnum), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  343. v = type << 2 | BIT(0); /* MMD-access-type | EXEC */
  344. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  345. do {
  346. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  347. } while (v & BIT(0));
  348. /* Check for error condition */
  349. if (v & BIT(1))
  350. err = -EIO;
  351. *val = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3) >> 16;
  352. pr_debug("%s: port %d, dev: %x, regnum: %x, val: %x (err %d)\n", __func__,
  353. port, devnum, mdiobus_c45_regad(regnum), *val, err);
  354. mutex_unlock(&smi_lock);
  355. return err;
  356. }
  357. /* Write to an mmd register of the PHY */
  358. int rtl931x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
  359. {
  360. int err = 0;
  361. u32 v;
  362. int type = (regnum & MII_ADDR_C45)?2:1;
  363. u64 pm;
  364. mutex_lock(&smi_lock);
  365. /* Set PHY to access via port-mask */
  366. pm = (u64)1 << port;
  367. sw_w32((u32)pm, RTL931X_SMI_INDRT_ACCESS_CTRL_2);
  368. sw_w32((u32)(pm >> 32), RTL931X_SMI_INDRT_ACCESS_CTRL_2 + 4);
  369. /* Set data to write */
  370. sw_w32_mask(0xffff, val, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  371. /* Set MMD device number and register to write to */
  372. sw_w32(devnum << 16 | mdiobus_c45_regad(regnum), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  373. v = BIT(4) | type << 2 | BIT(0); /* WRITE | MMD-access-type | EXEC */
  374. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  375. do {
  376. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  377. } while (v & BIT(0));
  378. pr_debug("%s: port %d, dev: %x, regnum: %x, val: %x (err %d)\n", __func__,
  379. port, devnum, mdiobus_c45_regad(regnum), val, err);
  380. mutex_unlock(&smi_lock);
  381. return err;
  382. }
  383. void rtl931x_print_matrix(void)
  384. {
  385. volatile u64 *ptr = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  386. for (int i = 0; i < 52; i += 4)
  387. pr_info("> %16llx %16llx %16llx %16llx\n",
  388. ptr[i + 0], ptr[i + 1], ptr[i + 2], ptr[i + 3]);
  389. pr_info("CPU_PORT> %16llx\n", ptr[52]);
  390. }
  391. void rtl931x_set_receive_management_action(int port, rma_ctrl_t type, action_type_t action)
  392. {
  393. u32 value = 0;
  394. /* hack for value mapping */
  395. if (type == GRATARP && action == COPY2CPU)
  396. action = TRAP2MASTERCPU;
  397. switch(action) {
  398. case FORWARD:
  399. value = 0;
  400. break;
  401. case DROP:
  402. value = 1;
  403. break;
  404. case TRAP2CPU:
  405. value = 2;
  406. break;
  407. case TRAP2MASTERCPU:
  408. value = 3;
  409. break;
  410. case FLOODALL:
  411. value = 4;
  412. break;
  413. default:
  414. break;
  415. }
  416. switch(type) {
  417. case BPDU:
  418. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_BPDU_CTRL + ((port / 10) << 2));
  419. break;
  420. case PTP:
  421. /* udp */
  422. sw_w32_mask(3 << 2, value << 2, RTL931X_RMA_PTP_CTRL + (port << 2));
  423. /* eth2 */
  424. sw_w32_mask(3, value, RTL931X_RMA_PTP_CTRL + (port << 2));
  425. break;
  426. case PTP_UDP:
  427. sw_w32_mask(3 << 2, value << 2, RTL931X_RMA_PTP_CTRL + (port << 2));
  428. break;
  429. case PTP_ETH2:
  430. sw_w32_mask(3, value, RTL931X_RMA_PTP_CTRL + (port << 2));
  431. break;
  432. case LLTP:
  433. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_LLTP_CTRL + ((port / 10) << 2));
  434. break;
  435. case EAPOL:
  436. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_EAPOL_CTRL + ((port / 10) << 2));
  437. break;
  438. case GRATARP:
  439. sw_w32_mask(3 << ((port & 0xf) << 1), value << ((port & 0xf) << 1), RTL931X_TRAP_ARP_GRAT_PORT_ACT + ((port >> 4) << 2));
  440. break;
  441. }
  442. }
  443. u64 rtl931x_traffic_get(int source)
  444. {
  445. u32 v;
  446. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  447. rtl_table_read(r, source);
  448. v = sw_r32(rtl_table_data(r, 0));
  449. rtl_table_release(r);
  450. v = v >> 3;
  451. return v;
  452. }
  453. /* Enable traffic between a source port and a destination port matrix */
  454. void rtl931x_traffic_set(int source, u64 dest_matrix)
  455. {
  456. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  457. sw_w32((dest_matrix << 3), rtl_table_data(r, 0));
  458. rtl_table_write(r, source);
  459. rtl_table_release(r);
  460. }
  461. void rtl931x_traffic_enable(int source, int dest)
  462. {
  463. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  464. rtl_table_read(r, source);
  465. sw_w32_mask(0, BIT(dest + 3), rtl_table_data(r, 0));
  466. rtl_table_write(r, source);
  467. rtl_table_release(r);
  468. }
  469. void rtl931x_traffic_disable(int source, int dest)
  470. {
  471. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  472. rtl_table_read(r, source);
  473. sw_w32_mask(BIT(dest + 3), 0, rtl_table_data(r, 0));
  474. rtl_table_write(r, source);
  475. rtl_table_release(r);
  476. }
  477. static u64 rtl931x_l2_hash_seed(u64 mac, u32 vid)
  478. {
  479. u64 v = vid;
  480. v <<= 48;
  481. v |= mac;
  482. return v;
  483. }
  484. /* Calculate both the block 0 and the block 1 hash by applyingthe same hash
  485. * algorithm as the one used currently by the ASIC to the seed, and return
  486. * both hashes in the lower and higher word of the return value since only 12 bit of
  487. * the hash are significant.
  488. */
  489. static u32 rtl931x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  490. {
  491. u32 h, h0, h1, h2, h3, h4, k0, k1;
  492. h0 = seed & 0xfff;
  493. h1 = (seed >> 12) & 0xfff;
  494. h2 = (seed >> 24) & 0xfff;
  495. h3 = (seed >> 36) & 0xfff;
  496. h4 = (seed >> 48) & 0xfff;
  497. h4 = ((h4 & 0x7) << 9) | ((h4 >> 3) & 0x1ff);
  498. k0 = h0 ^ h1 ^ h2 ^ h3 ^ h4;
  499. h0 = seed & 0xfff;
  500. h0 = ((h0 & 0x1ff) << 3) | ((h0 >> 9) & 0x7);
  501. h1 = (seed >> 12) & 0xfff;
  502. h1 = ((h1 & 0x3f) << 6) | ((h1 >> 6) & 0x3f);
  503. h2 = (seed >> 24) & 0xfff;
  504. h3 = (seed >> 36) & 0xfff;
  505. h3 = ((h3 & 0x3f) << 6) | ((h3 >> 6) & 0x3f);
  506. h4 = (seed >> 48) & 0xfff;
  507. k1 = h0 ^ h1 ^ h2 ^ h3 ^ h4;
  508. /* Algorithm choice for block 0 */
  509. if (sw_r32(RTL931X_L2_CTRL) & BIT(0))
  510. h = k1;
  511. else
  512. h = k0;
  513. /* Algorithm choice for block 1
  514. * Since k0 and k1 are < 4096, adding 4096 will offset the hash into the second
  515. * half of hash-space
  516. * 4096 is in fact the hash-table size 32768 divided by 4 hashes per bucket
  517. * divided by 2 to divide the hash space in 2
  518. */
  519. if (sw_r32(RTL931X_L2_CTRL) & BIT(1))
  520. h |= (k1 + 4096) << 16;
  521. else
  522. h |= (k0 + 4096) << 16;
  523. return h;
  524. }
  525. /* Fills an L2 entry structure from the SoC registers */
  526. static void rtl931x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  527. {
  528. pr_debug("In %s valid?\n", __func__);
  529. e->valid = !!(r[0] & BIT(31));
  530. if (!e->valid)
  531. return;
  532. pr_debug("%s: entry valid, raw: %08x %08x %08x %08x\n", __func__, r[0], r[1], r[2], r[3]);
  533. e->is_ip_mc = false;
  534. e->is_ipv6_mc = false;
  535. e->mac[0] = r[0] >> 8;
  536. e->mac[1] = r[0];
  537. e->mac[2] = r[1] >> 24;
  538. e->mac[3] = r[1] >> 16;
  539. e->mac[4] = r[1] >> 8;
  540. e->mac[5] = r[1];
  541. e->is_open_flow = !!(r[0] & BIT(30));
  542. e->is_pe_forward = !!(r[0] & BIT(29));
  543. e->next_hop = !!(r[2] & BIT(30));
  544. e->rvid = (r[0] >> 16) & 0xfff;
  545. /* Is it a unicast entry? check multicast bit */
  546. if (!(e->mac[0] & 1)) {
  547. e->type = L2_UNICAST;
  548. e->is_l2_tunnel = !!(r[2] & BIT(31));
  549. e->is_static = !!(r[2] & BIT(13));
  550. e->port = (r[2] >> 19) & 0x3ff;
  551. /* Check for trunk port */
  552. if (r[2] & BIT(29)) {
  553. e->is_trunk = true;
  554. e->stack_dev = (e->port >> 9) & 1;
  555. e->trunk = e->port & 0x3f;
  556. } else {
  557. e->is_trunk = false;
  558. e->stack_dev = (e->port >> 6) & 0xf;
  559. e->port = e->port & 0x3f;
  560. }
  561. e->block_da = !!(r[2] & BIT(14));
  562. e->block_sa = !!(r[2] & BIT(15));
  563. e->suspended = !!(r[2] & BIT(12));
  564. e->age = (r[2] >> 16) & 3;
  565. /* the UC_VID field in hardware is used for the VID or for the route id */
  566. if (e->next_hop) {
  567. e->nh_route_id = r[2] & 0x7ff;
  568. e->vid = 0;
  569. } else {
  570. e->vid = r[2] & 0xfff;
  571. e->nh_route_id = 0;
  572. }
  573. if (e->is_l2_tunnel)
  574. e->l2_tunnel_id = ((r[2] & 0xff) << 4) | (r[3] >> 28);
  575. /* TODO: Implement VLAN conversion */
  576. } else {
  577. e->type = L2_MULTICAST;
  578. e->is_local_forward = !!(r[2] & BIT(31));
  579. e->is_remote_forward = !!(r[2] & BIT(17));
  580. e->mc_portmask_index = (r[2] >> 18) & 0xfff;
  581. e->l2_tunnel_list_id = (r[2] >> 4) & 0x1fff;
  582. }
  583. }
  584. /* Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry */
  585. static void rtl931x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  586. {
  587. u32 port;
  588. if (!e->valid) {
  589. r[0] = r[1] = r[2] = 0;
  590. return;
  591. }
  592. r[2] = BIT(31); /* Set valid bit */
  593. r[0] = ((u32)e->mac[0]) << 24 |
  594. ((u32)e->mac[1]) << 16 |
  595. ((u32)e->mac[2]) << 8 |
  596. ((u32)e->mac[3]);
  597. r[1] = ((u32)e->mac[4]) << 24 |
  598. ((u32)e->mac[5]) << 16;
  599. r[2] |= e->next_hop ? BIT(12) : 0;
  600. if (e->type == L2_UNICAST) {
  601. r[2] |= e->is_static ? BIT(14) : 0;
  602. r[1] |= e->rvid & 0xfff;
  603. r[2] |= (e->port & 0x3ff) << 20;
  604. if (e->is_trunk) {
  605. r[2] |= BIT(30);
  606. port = e->stack_dev << 9 | (e->port & 0x3f);
  607. } else {
  608. port = (e->stack_dev & 0xf) << 6;
  609. port |= e->port & 0x3f;
  610. }
  611. r[2] |= port << 20;
  612. r[2] |= e->block_da ? BIT(15) : 0;
  613. r[2] |= e->block_sa ? BIT(17) : 0;
  614. r[2] |= e->suspended ? BIT(13) : 0;
  615. r[2] |= (e->age & 0x3) << 17;
  616. /* the UC_VID field in hardware is used for the VID or for the route id */
  617. if (e->next_hop)
  618. r[2] |= e->nh_route_id & 0x7ff;
  619. else
  620. r[2] |= e->vid & 0xfff;
  621. } else { /* L2_MULTICAST */
  622. r[2] |= (e->mc_portmask_index & 0x3ff) << 16;
  623. r[2] |= e->mc_mac_index & 0x7ff;
  624. }
  625. }
  626. /* Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  627. * hash is the id of the bucket and pos is the position of the entry in that bucket
  628. * The data read from the SoC is filled into rtl838x_l2_entry
  629. */
  630. static u64 rtl931x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  631. {
  632. u32 r[4];
  633. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 0);
  634. u32 idx;
  635. u64 mac;
  636. u64 seed;
  637. pr_debug("%s: hash %08x, pos: %d\n", __func__, hash, pos);
  638. /* On the RTL93xx, 2 different hash algorithms are used making it a total of
  639. * 8 buckets that need to be searched, 4 for each hash-half
  640. * Use second hash space when bucket is between 4 and 8
  641. */
  642. if (pos >= 4) {
  643. pos -= 4;
  644. hash >>= 16;
  645. } else {
  646. hash &= 0xffff;
  647. }
  648. idx = (0 << 14) | (hash << 2) | pos; /* Search SRAM, with hash and at pos in bucket */
  649. pr_debug("%s: NOW hash %08x, pos: %d\n", __func__, hash, pos);
  650. rtl_table_read(q, idx);
  651. for (int i = 0; i < 4; i++)
  652. r[i] = sw_r32(rtl_table_data(q, i));
  653. rtl_table_release(q);
  654. rtl931x_fill_l2_entry(r, e);
  655. pr_debug("%s: valid: %d, nh: %d\n", __func__, e->valid, e->next_hop);
  656. if (!e->valid)
  657. return 0;
  658. mac = ((u64)e->mac[0]) << 40 |
  659. ((u64)e->mac[1]) << 32 |
  660. ((u64)e->mac[2]) << 24 |
  661. ((u64)e->mac[3]) << 16 |
  662. ((u64)e->mac[4]) << 8 |
  663. ((u64)e->mac[5]);
  664. seed = rtl931x_l2_hash_seed(mac, e->rvid);
  665. pr_debug("%s: mac %016llx, seed %016llx\n", __func__, mac, seed);
  666. /* return vid with concatenated mac as unique id */
  667. return seed;
  668. }
  669. static u64 rtl931x_read_cam(int idx, struct rtl838x_l2_entry *e)
  670. {
  671. return 0;
  672. }
  673. static void rtl931x_write_cam(int idx, struct rtl838x_l2_entry *e)
  674. {
  675. }
  676. static void rtl931x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  677. {
  678. u32 r[4];
  679. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 0);
  680. u32 idx = (0 << 14) | (hash << 2) | pos; /* Access SRAM, with hash and at pos in bucket */
  681. pr_info("%s: hash %d, pos %d\n", __func__, hash, pos);
  682. pr_info("%s: index %d -> mac %02x:%02x:%02x:%02x:%02x:%02x\n", __func__, idx,
  683. e->mac[0], e->mac[1], e->mac[2], e->mac[3],e->mac[4],e->mac[5]);
  684. rtl931x_fill_l2_row(r, e);
  685. pr_info("%s: %d: %08x %08x %08x\n", __func__, idx, r[0], r[1], r[2]);
  686. for (int i = 0; i < 4; i++)
  687. sw_w32(r[i], rtl_table_data(q, i));
  688. rtl_table_write(q, idx);
  689. rtl_table_release(q);
  690. }
  691. static void rtl931x_vlan_fwd_on_inner(int port, bool is_set)
  692. {
  693. /* Always set all tag modes to fwd based on either inner or outer tag */
  694. if (is_set)
  695. sw_w32_mask(0, 0xf, RTL931X_VLAN_PORT_FWD + (port << 2));
  696. else
  697. sw_w32_mask(0xf, 0, RTL931X_VLAN_PORT_FWD + (port << 2));
  698. }
  699. static void rtl931x_vlan_profile_setup(int profile)
  700. {
  701. u32 p[7];
  702. pr_info("In %s\n", __func__);
  703. if (profile > 15)
  704. return;
  705. p[0] = sw_r32(RTL931X_VLAN_PROFILE_SET(profile));
  706. /* Enable routing of Ipv4/6 Unicast and IPv4/6 Multicast traffic */
  707. /* p[0] |= BIT(17) | BIT(16) | BIT(13) | BIT(12); */
  708. p[0] |= 0x3 << 11; /* COPY2CPU */
  709. p[1] = 0x1FFFFFF; /* L2 unknwon MC flooding portmask all ports, including the CPU-port */
  710. p[2] = 0xFFFFFFFF;
  711. p[3] = 0x1FFFFFF; /* IPv4 unknwon MC flooding portmask */
  712. p[4] = 0xFFFFFFFF;
  713. p[5] = 0x1FFFFFF; /* IPv6 unknwon MC flooding portmask */
  714. p[6] = 0xFFFFFFFF;
  715. for (int i = 0; i < 7; i++)
  716. sw_w32(p[i], RTL931X_VLAN_PROFILE_SET(profile) + i * 4);
  717. pr_info("Leaving %s\n", __func__);
  718. }
  719. static void rtl931x_l2_learning_setup(void)
  720. {
  721. /* Portmask for flooding broadcast traffic */
  722. rtl839x_set_port_reg_be(0x1FFFFFFFFFFFFFF, RTL931X_L2_BC_FLD_PMSK);
  723. /* Portmask for flooding unicast traffic with unknown destination */
  724. rtl839x_set_port_reg_be(0x1FFFFFFFFFFFFFF, RTL931X_L2_UNKN_UC_FLD_PMSK);
  725. /* Limit learning to maximum: 64k entries, after that just flood (bits 0-2) */
  726. sw_w32((0xffff << 3) | FORWARD, RTL931X_L2_LRN_CONSTRT_CTRL);
  727. }
  728. static u64 rtl931x_read_mcast_pmask(int idx)
  729. {
  730. u64 portmask;
  731. /* Read MC_PMSK (2) via register RTL9310_TBL_0 */
  732. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 2);
  733. rtl_table_read(q, idx);
  734. portmask = sw_r32(rtl_table_data(q, 0));
  735. portmask <<= 32;
  736. portmask |= sw_r32(rtl_table_data(q, 1));
  737. portmask >>= 7;
  738. rtl_table_release(q);
  739. pr_debug("%s: Index idx %d has portmask %016llx\n", __func__, idx, portmask);
  740. return portmask;
  741. }
  742. static void rtl931x_write_mcast_pmask(int idx, u64 portmask)
  743. {
  744. u64 pm = portmask;
  745. /* Access MC_PMSK (2) via register RTL9310_TBL_0 */
  746. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 2);
  747. pr_debug("%s: Index idx %d has portmask %016llx\n", __func__, idx, pm);
  748. pm <<= 7;
  749. sw_w32((u32)(pm >> 32), rtl_table_data(q, 0));
  750. sw_w32((u32)pm, rtl_table_data(q, 1));
  751. rtl_table_write(q, idx);
  752. rtl_table_release(q);
  753. }
  754. static int rtl931x_set_ageing_time(unsigned long msec)
  755. {
  756. int t = sw_r32(RTL931X_L2_AGE_CTRL);
  757. t &= 0x1FFFFF;
  758. t = (t * 8) / 10;
  759. pr_debug("L2 AGING time: %d sec\n", t);
  760. t = (msec / 100 + 7) / 8;
  761. t = t > 0x1FFFFF ? 0x1FFFFF : t;
  762. sw_w32_mask(0x1FFFFF, t, RTL931X_L2_AGE_CTRL);
  763. pr_debug("Dynamic aging for ports: %x\n", sw_r32(RTL931X_L2_PORT_AGE_CTRL));
  764. return 0;
  765. }
  766. void rtl931x_sw_init(struct rtl838x_switch_priv *priv)
  767. {
  768. /* rtl931x_sds_init(priv); */
  769. }
  770. static void rtl931x_pie_lookup_enable(struct rtl838x_switch_priv *priv, int index)
  771. {
  772. int block = index / PIE_BLOCK_SIZE;
  773. sw_w32_mask(0, BIT(block), RTL931X_PIE_BLK_LOOKUP_CTRL);
  774. }
  775. /* Fills the data in the intermediate representation in the pie_rule structure
  776. * into a data field for a given template field field_type
  777. * TODO: This function looks very similar to the function of the rtl9300, but
  778. * since it uses the physical template_field_id, which are different for each
  779. * SoC and there are other field types, it is actually not. If we would also use
  780. * an intermediate representation for a field type, we would could have one
  781. * pie_data_fill function for all SoCs, provided we have also for each SoC a
  782. * function to map between physical and intermediate field type
  783. */
  784. int rtl931x_pie_data_fill(enum template_field_id field_type, struct pie_rule *pr, u16 *data, u16 *data_m)
  785. {
  786. *data = *data_m = 0;
  787. switch (field_type) {
  788. case TEMPLATE_FIELD_SPM0:
  789. *data = pr->spm;
  790. *data_m = pr->spm_m;
  791. break;
  792. case TEMPLATE_FIELD_SPM1:
  793. *data = pr->spm >> 16;
  794. *data_m = pr->spm_m >> 16;
  795. break;
  796. case TEMPLATE_FIELD_OTAG:
  797. *data = pr->otag;
  798. *data_m = pr->otag_m;
  799. break;
  800. case TEMPLATE_FIELD_SMAC0:
  801. *data = pr->smac[4];
  802. *data = (*data << 8) | pr->smac[5];
  803. *data_m = pr->smac_m[4];
  804. *data_m = (*data_m << 8) | pr->smac_m[5];
  805. break;
  806. case TEMPLATE_FIELD_SMAC1:
  807. *data = pr->smac[2];
  808. *data = (*data << 8) | pr->smac[3];
  809. *data_m = pr->smac_m[2];
  810. *data_m = (*data_m << 8) | pr->smac_m[3];
  811. break;
  812. case TEMPLATE_FIELD_SMAC2:
  813. *data = pr->smac[0];
  814. *data = (*data << 8) | pr->smac[1];
  815. *data_m = pr->smac_m[0];
  816. *data_m = (*data_m << 8) | pr->smac_m[1];
  817. break;
  818. case TEMPLATE_FIELD_DMAC0:
  819. *data = pr->dmac[4];
  820. *data = (*data << 8) | pr->dmac[5];
  821. *data_m = pr->dmac_m[4];
  822. *data_m = (*data_m << 8) | pr->dmac_m[5];
  823. break;
  824. case TEMPLATE_FIELD_DMAC1:
  825. *data = pr->dmac[2];
  826. *data = (*data << 8) | pr->dmac[3];
  827. *data_m = pr->dmac_m[2];
  828. *data_m = (*data_m << 8) | pr->dmac_m[3];
  829. break;
  830. case TEMPLATE_FIELD_DMAC2:
  831. *data = pr->dmac[0];
  832. *data = (*data << 8) | pr->dmac[1];
  833. *data_m = pr->dmac_m[0];
  834. *data_m = (*data_m << 8) | pr->dmac_m[1];
  835. break;
  836. case TEMPLATE_FIELD_ETHERTYPE:
  837. *data = pr->ethertype;
  838. *data_m = pr->ethertype_m;
  839. break;
  840. case TEMPLATE_FIELD_ITAG:
  841. *data = pr->itag;
  842. *data_m = pr->itag_m;
  843. break;
  844. case TEMPLATE_FIELD_SIP0:
  845. if (pr->is_ipv6) {
  846. *data = pr->sip6.s6_addr16[7];
  847. *data_m = pr->sip6_m.s6_addr16[7];
  848. } else {
  849. *data = pr->sip;
  850. *data_m = pr->sip_m;
  851. }
  852. break;
  853. case TEMPLATE_FIELD_SIP1:
  854. if (pr->is_ipv6) {
  855. *data = pr->sip6.s6_addr16[6];
  856. *data_m = pr->sip6_m.s6_addr16[6];
  857. } else {
  858. *data = pr->sip >> 16;
  859. *data_m = pr->sip_m >> 16;
  860. }
  861. break;
  862. case TEMPLATE_FIELD_SIP2:
  863. case TEMPLATE_FIELD_SIP3:
  864. case TEMPLATE_FIELD_SIP4:
  865. case TEMPLATE_FIELD_SIP5:
  866. case TEMPLATE_FIELD_SIP6:
  867. case TEMPLATE_FIELD_SIP7:
  868. *data = pr->sip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  869. *data_m = pr->sip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  870. break;
  871. case TEMPLATE_FIELD_DIP0:
  872. if (pr->is_ipv6) {
  873. *data = pr->dip6.s6_addr16[7];
  874. *data_m = pr->dip6_m.s6_addr16[7];
  875. } else {
  876. *data = pr->dip;
  877. *data_m = pr->dip_m;
  878. }
  879. break;
  880. case TEMPLATE_FIELD_DIP1:
  881. if (pr->is_ipv6) {
  882. *data = pr->dip6.s6_addr16[6];
  883. *data_m = pr->dip6_m.s6_addr16[6];
  884. } else {
  885. *data = pr->dip >> 16;
  886. *data_m = pr->dip_m >> 16;
  887. }
  888. break;
  889. case TEMPLATE_FIELD_DIP2:
  890. case TEMPLATE_FIELD_DIP3:
  891. case TEMPLATE_FIELD_DIP4:
  892. case TEMPLATE_FIELD_DIP5:
  893. case TEMPLATE_FIELD_DIP6:
  894. case TEMPLATE_FIELD_DIP7:
  895. *data = pr->dip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  896. *data_m = pr->dip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  897. break;
  898. case TEMPLATE_FIELD_IP_TOS_PROTO:
  899. *data = pr->tos_proto;
  900. *data_m = pr->tos_proto_m;
  901. break;
  902. case TEMPLATE_FIELD_L4_SPORT:
  903. *data = pr->sport;
  904. *data_m = pr->sport_m;
  905. break;
  906. case TEMPLATE_FIELD_L4_DPORT:
  907. *data = pr->dport;
  908. *data_m = pr->dport_m;
  909. break;
  910. case TEMPLATE_FIELD_DSAP_SSAP:
  911. *data = pr->dsap_ssap;
  912. *data_m = pr->dsap_ssap_m;
  913. break;
  914. case TEMPLATE_FIELD_TCP_INFO:
  915. *data = pr->tcp_info;
  916. *data_m = pr->tcp_info_m;
  917. break;
  918. case TEMPLATE_FIELD_RANGE_CHK:
  919. pr_info("TEMPLATE_FIELD_RANGE_CHK: not configured\n");
  920. break;
  921. default:
  922. pr_info("%s: unknown field %d\n", __func__, field_type);
  923. return -1;
  924. }
  925. return 0;
  926. }
  927. /* Reads the intermediate representation of the templated match-fields of the
  928. * PIE rule in the pie_rule structure and fills in the raw data fields in the
  929. * raw register space r[].
  930. * The register space configuration size is identical for the RTL8380/90 and RTL9300,
  931. * however the RTL931X has 2 more registers / fields and the physical field-ids are different
  932. * on all SoCs
  933. * On the RTL9300 the mask fields are not word-aligend!
  934. */
  935. static void rtl931x_write_pie_templated(u32 r[], struct pie_rule *pr, enum template_field_id t[])
  936. {
  937. for (int i = 0; i < N_FIXED_FIELDS; i++) {
  938. u16 data, data_m;
  939. rtl931x_pie_data_fill(t[i], pr, &data, &data_m);
  940. /* On the RTL9300, the mask fields are not word aligned! */
  941. if (!(i % 2)) {
  942. r[5 - i / 2] = data;
  943. r[12 - i / 2] |= ((u32)data_m << 8);
  944. } else {
  945. r[5 - i / 2] |= ((u32)data) << 16;
  946. r[12 - i / 2] |= ((u32)data_m) << 24;
  947. r[11 - i / 2] |= ((u32)data_m) >> 8;
  948. }
  949. }
  950. }
  951. // Currently unused
  952. // static void rtl931x_read_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  953. // {
  954. // pr->mgnt_vlan = r[7] & BIT(31);
  955. // if (pr->phase == PHASE_IACL)
  956. // pr->dmac_hit_sw = r[7] & BIT(30);
  957. // else /* TODO: EACL/VACL phase handling */
  958. // pr->content_too_deep = r[7] & BIT(30);
  959. // pr->not_first_frag = r[7] & BIT(29);
  960. // pr->frame_type_l4 = (r[7] >> 26) & 7;
  961. // pr->frame_type = (r[7] >> 24) & 3;
  962. // pr->otag_fmt = (r[7] >> 23) & 1;
  963. // pr->itag_fmt = (r[7] >> 22) & 1;
  964. // pr->otag_exist = (r[7] >> 21) & 1;
  965. // pr->itag_exist = (r[7] >> 20) & 1;
  966. // pr->frame_type_l2 = (r[7] >> 18) & 3;
  967. // pr->igr_normal_port = (r[7] >> 17) & 1;
  968. // pr->tid = (r[7] >> 16) & 1;
  969. // pr->mgnt_vlan_m = r[14] & BIT(15);
  970. // if (pr->phase == PHASE_IACL)
  971. // pr->dmac_hit_sw_m = r[14] & BIT(14);
  972. // else
  973. // pr->content_too_deep_m = r[14] & BIT(14);
  974. // pr->not_first_frag_m = r[14] & BIT(13);
  975. // pr->frame_type_l4_m = (r[14] >> 10) & 7;
  976. // pr->frame_type_m = (r[14] >> 8) & 3;
  977. // pr->otag_fmt_m = r[14] & BIT(7);
  978. // pr->itag_fmt_m = r[14] & BIT(6);
  979. // pr->otag_exist_m = r[14] & BIT(5);
  980. // pr->itag_exist_m = r[14] & BIT (4);
  981. // pr->frame_type_l2_m = (r[14] >> 2) & 3;
  982. // pr->igr_normal_port_m = r[14] & BIT(1);
  983. // pr->tid_m = r[14] & 1;
  984. // pr->valid = r[15] & BIT(31);
  985. // pr->cond_not = r[15] & BIT(30);
  986. // pr->cond_and1 = r[15] & BIT(29);
  987. // pr->cond_and2 = r[15] & BIT(28);
  988. // }
  989. static void rtl931x_write_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  990. {
  991. r[7] |= pr->mgnt_vlan ? BIT(31) : 0;
  992. if (pr->phase == PHASE_IACL)
  993. r[7] |= pr->dmac_hit_sw ? BIT(30) : 0;
  994. else
  995. r[7] |= pr->content_too_deep ? BIT(30) : 0;
  996. r[7] |= pr->not_first_frag ? BIT(29) : 0;
  997. r[7] |= ((u32) (pr->frame_type_l4 & 0x7)) << 26;
  998. r[7] |= ((u32) (pr->frame_type & 0x3)) << 24;
  999. r[7] |= pr->otag_fmt ? BIT(23) : 0;
  1000. r[7] |= pr->itag_fmt ? BIT(22) : 0;
  1001. r[7] |= pr->otag_exist ? BIT(21) : 0;
  1002. r[7] |= pr->itag_exist ? BIT(20) : 0;
  1003. r[7] |= ((u32) (pr->frame_type_l2 & 0x3)) << 18;
  1004. r[7] |= pr->igr_normal_port ? BIT(17) : 0;
  1005. r[7] |= ((u32) (pr->tid & 0x1)) << 16;
  1006. r[14] |= pr->mgnt_vlan_m ? BIT(15) : 0;
  1007. if (pr->phase == PHASE_IACL)
  1008. r[14] |= pr->dmac_hit_sw_m ? BIT(14) : 0;
  1009. else
  1010. r[14] |= pr->content_too_deep_m ? BIT(14) : 0;
  1011. r[14] |= pr->not_first_frag_m ? BIT(13) : 0;
  1012. r[14] |= ((u32) (pr->frame_type_l4_m & 0x7)) << 10;
  1013. r[14] |= ((u32) (pr->frame_type_m & 0x3)) << 8;
  1014. r[14] |= pr->otag_fmt_m ? BIT(7) : 0;
  1015. r[14] |= pr->itag_fmt_m ? BIT(6) : 0;
  1016. r[14] |= pr->otag_exist_m ? BIT(5) : 0;
  1017. r[14] |= pr->itag_exist_m ? BIT(4) : 0;
  1018. r[14] |= ((u32) (pr->frame_type_l2_m & 0x3)) << 2;
  1019. r[14] |= pr->igr_normal_port_m ? BIT(1) : 0;
  1020. r[14] |= (u32) (pr->tid_m & 0x1);
  1021. r[15] |= pr->valid ? BIT(31) : 0;
  1022. r[15] |= pr->cond_not ? BIT(30) : 0;
  1023. r[15] |= pr->cond_and1 ? BIT(29) : 0;
  1024. r[15] |= pr->cond_and2 ? BIT(28) : 0;
  1025. }
  1026. static void rtl931x_write_pie_action(u32 r[], struct pie_rule *pr)
  1027. {
  1028. /* Either drop or forward */
  1029. if (pr->drop) {
  1030. r[15] |= BIT(11) | BIT(12) | BIT(13); /* Do Green, Yellow and Red drops */
  1031. /* Actually DROP, not PERMIT in Green / Yellow / Red */
  1032. r[16] |= BIT(27) | BIT(28) | BIT(29);
  1033. } else {
  1034. r[15] |= pr->fwd_sel ? BIT(14) : 0;
  1035. r[16] |= pr->fwd_act << 24;
  1036. r[16] |= BIT(21); /* We overwrite any drop */
  1037. }
  1038. if (pr->phase == PHASE_VACL)
  1039. r[16] |= pr->fwd_sa_lrn ? BIT(22) : 0;
  1040. r[15] |= pr->bypass_sel ? BIT(10) : 0;
  1041. r[15] |= pr->nopri_sel ? BIT(21) : 0;
  1042. r[15] |= pr->tagst_sel ? BIT(20) : 0;
  1043. r[15] |= pr->ovid_sel ? BIT(18) : 0;
  1044. r[15] |= pr->ivid_sel ? BIT(16) : 0;
  1045. r[15] |= pr->meter_sel ? BIT(27) : 0;
  1046. r[15] |= pr->mir_sel ? BIT(15) : 0;
  1047. r[15] |= pr->log_sel ? BIT(26) : 0;
  1048. r[16] |= ((u32)(pr->fwd_data & 0xfff)) << 9;
  1049. /* r[15] |= pr->log_octets ? BIT(31) : 0; */
  1050. r[15] |= (u32)(pr->meter_data) >> 2;
  1051. r[16] |= (((u32)(pr->meter_data) >> 7) & 0x3) << 29;
  1052. r[16] |= ((u32)(pr->ivid_act & 0x3)) << 21;
  1053. r[15] |= ((u32)(pr->ivid_data & 0xfff)) << 9;
  1054. r[16] |= ((u32)(pr->ovid_act & 0x3)) << 30;
  1055. r[16] |= ((u32)(pr->ovid_data & 0xfff)) << 16;
  1056. r[16] |= ((u32)(pr->mir_data & 0x3)) << 6;
  1057. r[17] |= ((u32)(pr->tagst_data & 0xf)) << 28;
  1058. r[17] |= ((u32)(pr->nopri_data & 0x7)) << 25;
  1059. r[17] |= pr->bypass_ibc_sc ? BIT(16) : 0;
  1060. }
  1061. void rtl931x_pie_rule_dump_raw(u32 r[])
  1062. {
  1063. pr_info("Raw IACL table entry:\n");
  1064. pr_info("r 0 - 7: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1065. r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]);
  1066. pr_info("r 8 - 15: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1067. r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]);
  1068. pr_info("r 16 - 18: %08x %08x %08x\n", r[16], r[17], r[18]);
  1069. pr_info("Match : %08x %08x %08x %08x %08x %08x\n", r[0], r[1], r[2], r[3], r[4], r[5]);
  1070. pr_info("Fixed : %06x\n", r[6] >> 8);
  1071. pr_info("Match M: %08x %08x %08x %08x %08x %08x\n",
  1072. (r[6] << 24) | (r[7] >> 8), (r[7] << 24) | (r[8] >> 8), (r[8] << 24) | (r[9] >> 8),
  1073. (r[9] << 24) | (r[10] >> 8), (r[10] << 24) | (r[11] >> 8),
  1074. (r[11] << 24) | (r[12] >> 8));
  1075. pr_info("R[13]: %08x\n", r[13]);
  1076. pr_info("Fixed M: %06x\n", ((r[12] << 16) | (r[13] >> 16)) & 0xffffff);
  1077. pr_info("Valid / not / and1 / and2 : %1x\n", (r[13] >> 12) & 0xf);
  1078. pr_info("r 13-16: %08x %08x %08x %08x\n", r[13], r[14], r[15], r[16]);
  1079. }
  1080. static int rtl931x_pie_rule_write(struct rtl838x_switch_priv *priv, int idx, struct pie_rule *pr)
  1081. {
  1082. /* Access IACL table (0) via register 1, the table size is 4096 */
  1083. struct table_reg *q = rtl_table_get(RTL9310_TBL_1, 0);
  1084. u32 r[22];
  1085. int block = idx / PIE_BLOCK_SIZE;
  1086. u32 t_select = sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block));
  1087. pr_info("%s: %d, t_select: %08x\n", __func__, idx, t_select);
  1088. for (int i = 0; i < 22; i++)
  1089. r[i] = 0;
  1090. if (!pr->valid) {
  1091. rtl_table_write(q, idx);
  1092. rtl_table_release(q);
  1093. return 0;
  1094. }
  1095. rtl931x_write_pie_fixed_fields(r, pr);
  1096. pr_info("%s: template %d\n", __func__, (t_select >> (pr->tid * 4)) & 0xf);
  1097. rtl931x_write_pie_templated(r, pr, fixed_templates[(t_select >> (pr->tid * 4)) & 0xf]);
  1098. rtl931x_write_pie_action(r, pr);
  1099. rtl931x_pie_rule_dump_raw(r);
  1100. for (int i = 0; i < 22; i++)
  1101. sw_w32(r[i], rtl_table_data(q, i));
  1102. rtl_table_write(q, idx);
  1103. rtl_table_release(q);
  1104. return 0;
  1105. }
  1106. static bool rtl931x_pie_templ_has(int t, enum template_field_id field_type)
  1107. {
  1108. for (int i = 0; i < N_FIXED_FIELDS_RTL931X; i++) {
  1109. enum template_field_id ft = fixed_templates[t][i];
  1110. if (field_type == ft)
  1111. return true;
  1112. }
  1113. return false;
  1114. }
  1115. /* Verify that the rule pr is compatible with a given template t in block block
  1116. * Note that this function is SoC specific since the values of e.g. TEMPLATE_FIELD_SIP0
  1117. * depend on the SoC
  1118. */
  1119. static int rtl931x_pie_verify_template(struct rtl838x_switch_priv *priv,
  1120. struct pie_rule *pr, int t, int block)
  1121. {
  1122. int i;
  1123. if (!pr->is_ipv6 && pr->sip_m && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SIP0))
  1124. return -1;
  1125. if (!pr->is_ipv6 && pr->dip_m && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DIP0))
  1126. return -1;
  1127. if (pr->is_ipv6) {
  1128. if ((pr->sip6_m.s6_addr32[0] ||
  1129. pr->sip6_m.s6_addr32[1] ||
  1130. pr->sip6_m.s6_addr32[2] ||
  1131. pr->sip6_m.s6_addr32[3]) &&
  1132. !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SIP2))
  1133. return -1;
  1134. if ((pr->dip6_m.s6_addr32[0] ||
  1135. pr->dip6_m.s6_addr32[1] ||
  1136. pr->dip6_m.s6_addr32[2] ||
  1137. pr->dip6_m.s6_addr32[3]) &&
  1138. !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DIP2))
  1139. return -1;
  1140. }
  1141. if (ether_addr_to_u64(pr->smac) && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SMAC0))
  1142. return -1;
  1143. if (ether_addr_to_u64(pr->dmac) && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DMAC0))
  1144. return -1;
  1145. /* TODO: Check more */
  1146. i = find_first_zero_bit(&priv->pie_use_bm[block * 4], PIE_BLOCK_SIZE);
  1147. if (i >= PIE_BLOCK_SIZE)
  1148. return -1;
  1149. return i + PIE_BLOCK_SIZE * block;
  1150. }
  1151. static int rtl931x_pie_rule_add(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1152. {
  1153. int idx, block, j;
  1154. int min_block = 0;
  1155. int max_block = priv->n_pie_blocks / 2;
  1156. if (pr->is_egress) {
  1157. min_block = max_block;
  1158. max_block = priv->n_pie_blocks;
  1159. }
  1160. pr_info("In %s\n", __func__);
  1161. mutex_lock(&priv->pie_mutex);
  1162. for (block = min_block; block < max_block; block++) {
  1163. for (j = 0; j < 2; j++) {
  1164. int t = (sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block)) >> (j * 4)) & 0xf;
  1165. pr_info("Testing block %d, template %d, template id %d\n", block, j, t);
  1166. pr_info("%s: %08x\n",
  1167. __func__, sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block)));
  1168. idx = rtl931x_pie_verify_template(priv, pr, t, block);
  1169. if (idx >= 0)
  1170. break;
  1171. }
  1172. if (j < 2)
  1173. break;
  1174. }
  1175. if (block >= priv->n_pie_blocks) {
  1176. mutex_unlock(&priv->pie_mutex);
  1177. return -EOPNOTSUPP;
  1178. }
  1179. pr_info("Using block: %d, index %d, template-id %d\n", block, idx, j);
  1180. set_bit(idx, priv->pie_use_bm);
  1181. pr->valid = true;
  1182. pr->tid = j; /* Mapped to template number */
  1183. pr->tid_m = 0x1;
  1184. pr->id = idx;
  1185. rtl931x_pie_lookup_enable(priv, idx);
  1186. rtl931x_pie_rule_write(priv, idx, pr);
  1187. mutex_unlock(&priv->pie_mutex);
  1188. return 0;
  1189. }
  1190. /* Delete a range of Packet Inspection Engine rules */
  1191. static int rtl931x_pie_rule_del(struct rtl838x_switch_priv *priv, int index_from, int index_to)
  1192. {
  1193. u32 v = (index_from << 1)| (index_to << 13 ) | BIT(0);
  1194. pr_info("%s: from %d to %d\n", __func__, index_from, index_to);
  1195. mutex_lock(&priv->reg_mutex);
  1196. /* Write from-to and execute bit into control register */
  1197. sw_w32(v, RTL931X_PIE_CLR_CTRL);
  1198. /* Wait until command has completed */
  1199. do {
  1200. } while (sw_r32(RTL931X_PIE_CLR_CTRL) & BIT(0));
  1201. mutex_unlock(&priv->reg_mutex);
  1202. return 0;
  1203. }
  1204. static void rtl931x_pie_rule_rm(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1205. {
  1206. int idx = pr->id;
  1207. rtl931x_pie_rule_del(priv, idx, idx);
  1208. clear_bit(idx, priv->pie_use_bm);
  1209. }
  1210. static void rtl931x_pie_init(struct rtl838x_switch_priv *priv)
  1211. {
  1212. u32 template_selectors;
  1213. mutex_init(&priv->pie_mutex);
  1214. pr_info("%s\n", __func__);
  1215. /* Enable ACL lookup on all ports, including CPU_PORT */
  1216. for (int i = 0; i <= priv->cpu_port; i++)
  1217. sw_w32(1, RTL931X_ACL_PORT_LOOKUP_CTRL(i));
  1218. /* Include IPG in metering */
  1219. sw_w32_mask(0, 1, RTL931X_METER_GLB_CTRL);
  1220. /* Delete all present rules, block size is 128 on all SoC families */
  1221. rtl931x_pie_rule_del(priv, 0, priv->n_pie_blocks * 128 - 1);
  1222. /* Assign first half blocks 0-7 to VACL phase, second half to IACL */
  1223. /* 3 bits are used for each block, values for PIE blocks are */
  1224. /* 6: Disabled, 0: VACL, 1: IACL, 2: EACL */
  1225. /* And for OpenFlow Flow blocks: 3: Ingress Flow table 0, */
  1226. /* 4: Ingress Flow Table 3, 5: Egress flow table 0 */
  1227. for (int i = 0; i < priv->n_pie_blocks; i++) {
  1228. int pos = (i % 10) * 3;
  1229. u32 r = RTL931X_PIE_BLK_PHASE_CTRL + 4 * (i / 10);
  1230. if (i < priv->n_pie_blocks / 2)
  1231. sw_w32_mask(0x7 << pos, 0, r);
  1232. else
  1233. sw_w32_mask(0x7 << pos, 1 << pos, r);
  1234. }
  1235. /* Enable predefined templates 0, 1 for first quarter of all blocks */
  1236. template_selectors = 0 | (1 << 4);
  1237. for (int i = 0; i < priv->n_pie_blocks / 4; i++)
  1238. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1239. /* Enable predefined templates 2, 3 for second quarter of all blocks */
  1240. template_selectors = 2 | (3 << 4);
  1241. for (int i = priv->n_pie_blocks / 4; i < priv->n_pie_blocks / 2; i++)
  1242. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1243. /* Enable predefined templates 0, 1 for third quater of all blocks */
  1244. template_selectors = 0 | (1 << 4);
  1245. for (int i = priv->n_pie_blocks / 2; i < priv->n_pie_blocks * 3 / 4; i++)
  1246. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1247. /* Enable predefined templates 2, 3 for fourth quater of all blocks */
  1248. template_selectors = 2 | (3 << 4);
  1249. for (int i = priv->n_pie_blocks * 3 / 4; i < priv->n_pie_blocks; i++)
  1250. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1251. }
  1252. int rtl931x_l3_setup(struct rtl838x_switch_priv *priv)
  1253. {
  1254. return 0;
  1255. }
  1256. void rtl931x_vlan_port_keep_tag_set(int port, bool keep_outer, bool keep_inner)
  1257. {
  1258. sw_w32(FIELD_PREP(RTL931X_VLAN_PORT_TAG_EGR_OTAG_STS_MASK,
  1259. keep_outer ? RTL931X_VLAN_PORT_TAG_STS_TAGGED : RTL931X_VLAN_PORT_TAG_STS_UNTAG) |
  1260. FIELD_PREP(RTL931X_VLAN_PORT_TAG_EGR_ITAG_STS_MASK,
  1261. keep_inner ? RTL931X_VLAN_PORT_TAG_STS_TAGGED : RTL931X_VLAN_PORT_TAG_STS_UNTAG),
  1262. RTL931X_VLAN_PORT_TAG_CTRL(port));
  1263. }
  1264. void rtl931x_vlan_port_pvidmode_set(int port, enum pbvlan_type type, enum pbvlan_mode mode)
  1265. {
  1266. if (type == PBVLAN_TYPE_INNER)
  1267. sw_w32_mask(0x3 << 12, mode << 12, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1268. else
  1269. sw_w32_mask(0x3 << 26, mode << 26, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1270. }
  1271. void rtl931x_vlan_port_pvid_set(int port, enum pbvlan_type type, int pvid)
  1272. {
  1273. if (type == PBVLAN_TYPE_INNER)
  1274. sw_w32_mask(0xfff, pvid, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1275. else
  1276. sw_w32_mask(0xfff << 14, pvid << 14, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1277. }
  1278. static void rtl931x_set_igr_filter(int port, enum igr_filter state)
  1279. {
  1280. sw_w32_mask(0x3 << ((port & 0xf)<<1), state << ((port & 0xf)<<1),
  1281. RTL931X_VLAN_PORT_IGR_FLTR + (((port >> 4) << 2)));
  1282. }
  1283. static void rtl931x_set_egr_filter(int port, enum egr_filter state)
  1284. {
  1285. sw_w32_mask(0x1 << (port % 0x20), state << (port % 0x20),
  1286. RTL931X_VLAN_PORT_EGR_FLTR + (((port >> 5) << 2)));
  1287. }
  1288. void rtl931x_set_distribution_algorithm(int group, int algoidx, u32 algomsk)
  1289. {
  1290. u32 l3shift = 0;
  1291. u32 newmask = 0;
  1292. /* TODO: for now we set algoidx to 0 */
  1293. algoidx = 0;
  1294. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SIP_BIT) {
  1295. l3shift = 4;
  1296. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SIP_BIT;
  1297. }
  1298. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DIP_BIT) {
  1299. l3shift = 4;
  1300. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DIP_BIT;
  1301. }
  1302. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  1303. l3shift = 4;
  1304. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  1305. }
  1306. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  1307. l3shift = 4;
  1308. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  1309. }
  1310. if (l3shift == 4) {
  1311. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  1312. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SMAC_BIT;
  1313. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  1314. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DMAC_BIT;
  1315. } else {
  1316. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  1317. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_SMAC_BIT;
  1318. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  1319. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_DMAC_BIT;
  1320. }
  1321. sw_w32(newmask << l3shift, RTL931X_TRK_HASH_CTRL + (algoidx << 2));
  1322. }
  1323. static void rtl931x_led_init(struct rtl838x_switch_priv *priv)
  1324. {
  1325. u64 pm_copper = 0, pm_fiber = 0;
  1326. struct device_node *node;
  1327. pr_info("%s called\n", __func__);
  1328. node = of_find_compatible_node(NULL, NULL, "realtek,rtl9300-leds");
  1329. if (!node) {
  1330. pr_info("%s No compatible LED node found\n", __func__);
  1331. return;
  1332. }
  1333. for (int i = 0; i < priv->cpu_port; i++) {
  1334. int pos = (i << 1) % 32;
  1335. u32 set;
  1336. u32 v;
  1337. sw_w32_mask(0x3 << pos, 0, RTL931X_LED_PORT_FIB_SET_SEL_CTRL(i));
  1338. sw_w32_mask(0x3 << pos, 0, RTL931X_LED_PORT_COPR_SET_SEL_CTRL(i));
  1339. if (!priv->ports[i].phy)
  1340. continue;
  1341. v = 0x1; /* Found on the EdgeCore, but we do not have any HW description */
  1342. sw_w32_mask(0x3 << pos, v << pos, RTL931X_LED_PORT_NUM_CTRL(i));
  1343. if (priv->ports[i].phy_is_integrated)
  1344. pm_fiber |= BIT_ULL(i);
  1345. else
  1346. pm_copper |= BIT_ULL(i);
  1347. set = priv->ports[i].led_set;
  1348. sw_w32_mask(0, set << pos, RTL931X_LED_PORT_COPR_SET_SEL_CTRL(i));
  1349. sw_w32_mask(0, set << pos, RTL931X_LED_PORT_FIB_SET_SEL_CTRL(i));
  1350. }
  1351. for (int i = 0; i < 4; i++) {
  1352. const __be32 *led_set;
  1353. char set_name[9];
  1354. u32 setlen;
  1355. u32 v;
  1356. sprintf(set_name, "led_set%d", i);
  1357. pr_info(">%s<\n", set_name);
  1358. led_set = of_get_property(node, set_name, &setlen);
  1359. if (!led_set || setlen != 16)
  1360. break;
  1361. v = be32_to_cpup(led_set) << 16 | be32_to_cpup(led_set + 1);
  1362. sw_w32(v, RTL931X_LED_SET0_0_CTRL - 4 - i * 8);
  1363. v = be32_to_cpup(led_set + 2) << 16 | be32_to_cpup(led_set + 3);
  1364. sw_w32(v, RTL931X_LED_SET0_0_CTRL - i * 8);
  1365. }
  1366. /* Set LED mode to serial (0x1) */
  1367. sw_w32_mask(0x3, 0x1, RTL931X_LED_GLB_CTRL);
  1368. rtl839x_set_port_reg_le(pm_copper, RTL931X_LED_PORT_COPR_MASK_CTRL);
  1369. rtl839x_set_port_reg_le(pm_fiber, RTL931X_LED_PORT_FIB_MASK_CTRL);
  1370. rtl839x_set_port_reg_le(pm_copper | pm_fiber, RTL931X_LED_PORT_COMBO_MASK_CTRL);
  1371. for (int i = 0; i < 32; i++)
  1372. pr_info("%s %08x: %08x\n",__func__, 0xbb000600 + i * 4, sw_r32(0x0600 + i * 4));
  1373. }
  1374. const struct rtl838x_reg rtl931x_reg = {
  1375. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  1376. .set_port_reg_be = rtl839x_set_port_reg_be,
  1377. .get_port_reg_be = rtl839x_get_port_reg_be,
  1378. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  1379. .set_port_reg_le = rtl839x_set_port_reg_le,
  1380. .get_port_reg_le = rtl839x_get_port_reg_le,
  1381. .stat_port_rst = RTL931X_STAT_PORT_RST,
  1382. .stat_rst = RTL931X_STAT_RST,
  1383. .stat_port_std_mib = 0, /* Not defined */
  1384. .traffic_enable = rtl931x_traffic_enable,
  1385. .traffic_disable = rtl931x_traffic_disable,
  1386. .traffic_get = rtl931x_traffic_get,
  1387. .traffic_set = rtl931x_traffic_set,
  1388. .l2_ctrl_0 = RTL931X_L2_CTRL,
  1389. .l2_ctrl_1 = RTL931X_L2_AGE_CTRL,
  1390. .l2_port_aging_out = RTL931X_L2_PORT_AGE_CTRL,
  1391. .set_ageing_time = rtl931x_set_ageing_time,
  1392. /* .smi_poll_ctrl does not exist */
  1393. .l2_tbl_flush_ctrl = RTL931X_L2_TBL_FLUSH_CTRL,
  1394. .exec_tbl0_cmd = rtl931x_exec_tbl0_cmd,
  1395. .exec_tbl1_cmd = rtl931x_exec_tbl1_cmd,
  1396. .tbl_access_data_0 = rtl931x_tbl_access_data_0,
  1397. .isr_glb_src = RTL931X_ISR_GLB_SRC,
  1398. .isr_port_link_sts_chg = RTL931X_ISR_PORT_LINK_STS_CHG,
  1399. .imr_port_link_sts_chg = RTL931X_IMR_PORT_LINK_STS_CHG,
  1400. /* imr_glb does not exist on RTL931X */
  1401. .vlan_tables_read = rtl931x_vlan_tables_read,
  1402. .vlan_set_tagged = rtl931x_vlan_set_tagged,
  1403. .vlan_set_untagged = rtl931x_vlan_set_untagged,
  1404. .vlan_profile_dump = rtl931x_vlan_profile_dump,
  1405. .vlan_profile_setup = rtl931x_vlan_profile_setup,
  1406. .vlan_fwd_on_inner = rtl931x_vlan_fwd_on_inner,
  1407. .stp_get = rtl931x_stp_get,
  1408. .stp_set = rtl931x_stp_set,
  1409. .mac_force_mode_ctrl = rtl931x_mac_force_mode_ctrl,
  1410. .mac_port_ctrl = rtl931x_mac_port_ctrl,
  1411. .l2_port_new_salrn = rtl931x_l2_port_new_salrn,
  1412. .l2_port_new_sa_fwd = rtl931x_l2_port_new_sa_fwd,
  1413. .mir_ctrl = RTL931X_MIR_CTRL,
  1414. .mir_dpm = RTL931X_MIR_DPM_CTRL,
  1415. .mir_spm = RTL931X_MIR_SPM_CTRL,
  1416. .mac_link_sts = RTL931X_MAC_LINK_STS,
  1417. .mac_link_dup_sts = RTL931X_MAC_LINK_DUP_STS,
  1418. .mac_link_spd_sts = rtl931x_mac_link_spd_sts,
  1419. .mac_rx_pause_sts = RTL931X_MAC_RX_PAUSE_STS,
  1420. .mac_tx_pause_sts = RTL931X_MAC_TX_PAUSE_STS,
  1421. .read_l2_entry_using_hash = rtl931x_read_l2_entry_using_hash,
  1422. .write_l2_entry_using_hash = rtl931x_write_l2_entry_using_hash,
  1423. .read_cam = rtl931x_read_cam,
  1424. .write_cam = rtl931x_write_cam,
  1425. .vlan_port_keep_tag_set = rtl931x_vlan_port_keep_tag_set,
  1426. .vlan_port_pvidmode_set = rtl931x_vlan_port_pvidmode_set,
  1427. .vlan_port_pvid_set = rtl931x_vlan_port_pvid_set,
  1428. .trk_mbr_ctr = rtl931x_trk_mbr_ctr,
  1429. .set_vlan_igr_filter = rtl931x_set_igr_filter,
  1430. .set_vlan_egr_filter = rtl931x_set_egr_filter,
  1431. .set_distribution_algorithm = rtl931x_set_distribution_algorithm,
  1432. .l2_hash_key = rtl931x_l2_hash_key,
  1433. .read_mcast_pmask = rtl931x_read_mcast_pmask,
  1434. .write_mcast_pmask = rtl931x_write_mcast_pmask,
  1435. .pie_init = rtl931x_pie_init,
  1436. .pie_rule_write = rtl931x_pie_rule_write,
  1437. .pie_rule_add = rtl931x_pie_rule_add,
  1438. .pie_rule_rm = rtl931x_pie_rule_rm,
  1439. .l2_learning_setup = rtl931x_l2_learning_setup,
  1440. .l3_setup = rtl931x_l3_setup,
  1441. .led_init = rtl931x_led_init,
  1442. };