rtl931x.c 46 KB

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