ksz_dcb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2024 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  3. #include <linux/dsa/ksz_common.h>
  4. #include <net/dsa.h>
  5. #include <net/dscp.h>
  6. #include <net/ieee8021q.h>
  7. #include "ksz_common.h"
  8. #include "ksz_dcb.h"
  9. #include "ksz8.h"
  10. #define KSZ8_REG_PORT_1_CTRL_0 0x10
  11. #define KSZ8_PORT_DIFFSERV_ENABLE BIT(6)
  12. #define KSZ8_PORT_802_1P_ENABLE BIT(5)
  13. #define KSZ8_PORT_BASED_PRIO_M GENMASK(4, 3)
  14. #define KSZ88X3_REG_TOS_DSCP_CTRL 0x60
  15. #define KSZ8765_REG_TOS_DSCP_CTRL 0x90
  16. #define KSZ9477_REG_SW_MAC_TOS_CTRL 0x033e
  17. #define KSZ9477_SW_TOS_DSCP_REMAP BIT(0)
  18. #define KSZ9477_SW_TOS_DSCP_DEFAULT_PRIO_M GENMASK(5, 3)
  19. #define KSZ9477_REG_DIFFSERV_PRIO_MAP 0x0340
  20. #define KSZ9477_REG_PORT_MRI_PRIO_CTRL 0x0801
  21. #define KSZ9477_PORT_HIGHEST_PRIO BIT(7)
  22. #define KSZ9477_PORT_OR_PRIO BIT(6)
  23. #define KSZ9477_PORT_MAC_PRIO_ENABLE BIT(4)
  24. #define KSZ9477_PORT_VLAN_PRIO_ENABLE BIT(3)
  25. #define KSZ9477_PORT_802_1P_PRIO_ENABLE BIT(2)
  26. #define KSZ9477_PORT_DIFFSERV_PRIO_ENABLE BIT(1)
  27. #define KSZ9477_PORT_ACL_PRIO_ENABLE BIT(0)
  28. #define KSZ9477_REG_PORT_MRI_MAC_CTRL 0x0802
  29. #define KSZ9477_PORT_BASED_PRIO_M GENMASK(2, 0)
  30. struct ksz_apptrust_map {
  31. u8 apptrust;
  32. u8 bit;
  33. };
  34. static const struct ksz_apptrust_map ksz8_apptrust_map_to_bit[] = {
  35. { DCB_APP_SEL_PCP, KSZ8_PORT_802_1P_ENABLE },
  36. { IEEE_8021QAZ_APP_SEL_DSCP, KSZ8_PORT_DIFFSERV_ENABLE },
  37. };
  38. static const struct ksz_apptrust_map ksz9477_apptrust_map_to_bit[] = {
  39. { DCB_APP_SEL_PCP, KSZ9477_PORT_802_1P_PRIO_ENABLE },
  40. { IEEE_8021QAZ_APP_SEL_DSCP, KSZ9477_PORT_DIFFSERV_PRIO_ENABLE },
  41. };
  42. /* ksz_supported_apptrust[] - Supported apptrust selectors and Priority Order
  43. * of Internal Priority Map (IPM) sources.
  44. *
  45. * This array defines the apptrust selectors supported by the hardware, where
  46. * the index within the array indicates the priority of the selector - lower
  47. * indices correspond to higher priority. This fixed priority scheme is due to
  48. * the hardware's design, which does not support configurable priority among
  49. * different priority sources.
  50. *
  51. * The priority sources, including Tail Tag, ACL, VLAN PCP and DSCP are ordered
  52. * by the hardware's fixed logic, as detailed below. The order reflects a
  53. * non-configurable precedence where certain types of priority information
  54. * override others:
  55. *
  56. * 1. Tail Tag - Highest priority, overrides ACL, VLAN PCP, and DSCP priorities.
  57. * 2. ACL - Overrides VLAN PCP and DSCP priorities.
  58. * 3. VLAN PCP - Overrides DSCP priority.
  59. * 4. DSCP - Lowest priority, does not override any other priority source.
  60. *
  61. * In this context, the array's lower index (higher priority) for
  62. * 'DCB_APP_SEL_PCP' suggests its relative priority over
  63. * 'IEEE_8021QAZ_APP_SEL_DSCP' within the system's fixed priority scheme.
  64. *
  65. * DCB_APP_SEL_PCP - Priority Code Point selector
  66. * IEEE_8021QAZ_APP_SEL_DSCP - Differentiated Services Code Point selector
  67. */
  68. static const u8 ksz_supported_apptrust[] = {
  69. DCB_APP_SEL_PCP,
  70. IEEE_8021QAZ_APP_SEL_DSCP,
  71. };
  72. static const char * const ksz_supported_apptrust_variants[] = {
  73. "empty", "dscp", "pcp", "dscp pcp"
  74. };
  75. static void ksz_get_default_port_prio_reg(struct ksz_device *dev, int *reg,
  76. u8 *mask, int *shift)
  77. {
  78. if (is_ksz8(dev)) {
  79. *reg = KSZ8_REG_PORT_1_CTRL_0;
  80. *mask = KSZ8_PORT_BASED_PRIO_M;
  81. *shift = __bf_shf(KSZ8_PORT_BASED_PRIO_M);
  82. } else {
  83. *reg = KSZ9477_REG_PORT_MRI_MAC_CTRL;
  84. *mask = KSZ9477_PORT_BASED_PRIO_M;
  85. *shift = __bf_shf(KSZ9477_PORT_BASED_PRIO_M);
  86. }
  87. }
  88. /**
  89. * ksz_get_dscp_prio_reg - Retrieves the DSCP-to-priority-mapping register
  90. * @dev: Pointer to the KSZ switch device structure
  91. * @reg: Pointer to the register address to be set
  92. * @per_reg: Pointer to the number of DSCP values per register
  93. * @mask: Pointer to the mask to be set
  94. *
  95. * This function retrieves the DSCP to priority mapping register, the number of
  96. * DSCP values per register, and the mask to be set.
  97. */
  98. static void ksz_get_dscp_prio_reg(struct ksz_device *dev, int *reg,
  99. int *per_reg, u8 *mask)
  100. {
  101. if (ksz_is_ksz87xx(dev) || ksz_is_8895_family(dev)) {
  102. *reg = KSZ8765_REG_TOS_DSCP_CTRL;
  103. *per_reg = 4;
  104. *mask = GENMASK(1, 0);
  105. } else if (ksz_is_ksz88x3(dev)) {
  106. *reg = KSZ88X3_REG_TOS_DSCP_CTRL;
  107. *per_reg = 4;
  108. *mask = GENMASK(1, 0);
  109. } else {
  110. *reg = KSZ9477_REG_DIFFSERV_PRIO_MAP;
  111. *per_reg = 2;
  112. *mask = GENMASK(2, 0);
  113. }
  114. }
  115. /**
  116. * ksz_get_apptrust_map_and_reg - Retrieves the apptrust map and register
  117. * @dev: Pointer to the KSZ switch device structure
  118. * @map: Pointer to the apptrust map to be set
  119. * @reg: Pointer to the register address to be set
  120. * @mask: Pointer to the mask to be set
  121. *
  122. * This function retrieves the apptrust map and register address for the
  123. * apptrust configuration.
  124. */
  125. static void ksz_get_apptrust_map_and_reg(struct ksz_device *dev,
  126. const struct ksz_apptrust_map **map,
  127. int *reg, u8 *mask)
  128. {
  129. if (is_ksz8(dev)) {
  130. *map = ksz8_apptrust_map_to_bit;
  131. *reg = KSZ8_REG_PORT_1_CTRL_0;
  132. *mask = KSZ8_PORT_DIFFSERV_ENABLE | KSZ8_PORT_802_1P_ENABLE;
  133. } else {
  134. *map = ksz9477_apptrust_map_to_bit;
  135. *reg = KSZ9477_REG_PORT_MRI_PRIO_CTRL;
  136. *mask = KSZ9477_PORT_802_1P_PRIO_ENABLE |
  137. KSZ9477_PORT_DIFFSERV_PRIO_ENABLE;
  138. }
  139. }
  140. /**
  141. * ksz_port_get_default_prio - Retrieves the default priority for a port on a
  142. * KSZ switch
  143. * @ds: Pointer to the DSA switch structure
  144. * @port: Port number from which to get the default priority
  145. *
  146. * This function fetches the default priority for the specified port on a KSZ
  147. * switch.
  148. *
  149. * Return: The default priority of the port on success, or a negative error
  150. * code on failure.
  151. */
  152. int ksz_port_get_default_prio(struct dsa_switch *ds, int port)
  153. {
  154. struct ksz_device *dev = ds->priv;
  155. int ret, reg, shift;
  156. u8 data, mask;
  157. ksz_get_default_port_prio_reg(dev, &reg, &mask, &shift);
  158. ret = ksz_pread8(dev, port, reg, &data);
  159. if (ret)
  160. return ret;
  161. return (data & mask) >> shift;
  162. }
  163. /**
  164. * ksz88x3_port_set_default_prio_quirks - Quirks for default priority
  165. * @dev: Pointer to the KSZ switch device structure
  166. * @port: Port number for which to set the default priority
  167. * @prio: Priority value to set
  168. *
  169. * This function implements quirks for setting the default priority on KSZ88x3
  170. * devices. On Port 2, no other priority providers are working
  171. * except of PCP. So, configuring default priority on Port 2 is not possible.
  172. * On Port 1, it is not possible to configure port priority if PCP
  173. * apptrust on Port 2 is disabled. Since we disable multiple queues on the
  174. * switch to disable PCP on Port 2, we need to ensure that the default priority
  175. * configuration on Port 1 is in agreement with the configuration on Port 2.
  176. *
  177. * Return: 0 on success, or a negative error code on failure
  178. */
  179. static int ksz88x3_port_set_default_prio_quirks(struct ksz_device *dev, int port,
  180. u8 prio)
  181. {
  182. if (!prio)
  183. return 0;
  184. if (port == KSZ_PORT_2) {
  185. dev_err(dev->dev, "Port priority configuration is not working on Port 2\n");
  186. return -EINVAL;
  187. } else if (port == KSZ_PORT_1) {
  188. u8 port2_data;
  189. int ret;
  190. ret = ksz_pread8(dev, KSZ_PORT_2, KSZ8_REG_PORT_1_CTRL_0,
  191. &port2_data);
  192. if (ret)
  193. return ret;
  194. if (!(port2_data & KSZ8_PORT_802_1P_ENABLE)) {
  195. dev_err(dev->dev, "Not possible to configure port priority on Port 1 if PCP apptrust on Port 2 is disabled\n");
  196. return -EINVAL;
  197. }
  198. }
  199. return 0;
  200. }
  201. /**
  202. * ksz_port_set_default_prio - Sets the default priority for a port on a KSZ
  203. * switch
  204. * @ds: Pointer to the DSA switch structure
  205. * @port: Port number for which to set the default priority
  206. * @prio: Priority value to set
  207. *
  208. * This function sets the default priority for the specified port on a KSZ
  209. * switch.
  210. *
  211. * Return: 0 on success, or a negative error code on failure.
  212. */
  213. int ksz_port_set_default_prio(struct dsa_switch *ds, int port, u8 prio)
  214. {
  215. struct ksz_device *dev = ds->priv;
  216. int reg, shift, ret;
  217. u8 mask;
  218. if (prio >= dev->info->num_ipms)
  219. return -EINVAL;
  220. if (ksz_is_ksz88x3(dev)) {
  221. ret = ksz88x3_port_set_default_prio_quirks(dev, port, prio);
  222. if (ret)
  223. return ret;
  224. }
  225. ksz_get_default_port_prio_reg(dev, &reg, &mask, &shift);
  226. return ksz_prmw8(dev, port, reg, mask, (prio << shift) & mask);
  227. }
  228. /**
  229. * ksz_port_get_dscp_prio - Retrieves the priority for a DSCP value on a KSZ
  230. * switch
  231. * @ds: Pointer to the DSA switch structure
  232. * @port: Port number for which to get the priority
  233. * @dscp: DSCP value for which to get the priority
  234. *
  235. * This function fetches the priority value from switch global DSCP-to-priorty
  236. * mapping table for the specified DSCP value.
  237. *
  238. * Return: The priority value for the DSCP on success, or a negative error
  239. * code on failure.
  240. */
  241. int ksz_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
  242. {
  243. struct ksz_device *dev = ds->priv;
  244. int reg, per_reg, ret, shift;
  245. u8 data, mask;
  246. ksz_get_dscp_prio_reg(dev, &reg, &per_reg, &mask);
  247. /* If DSCP remapping is disabled, DSCP bits 3-5 are used as Internal
  248. * Priority Map (IPM)
  249. */
  250. if (!is_ksz8(dev)) {
  251. ret = ksz_read8(dev, KSZ9477_REG_SW_MAC_TOS_CTRL, &data);
  252. if (ret)
  253. return ret;
  254. /* If DSCP remapping is disabled, DSCP bits 3-5 are used as
  255. * Internal Priority Map (IPM)
  256. */
  257. if (!(data & KSZ9477_SW_TOS_DSCP_REMAP))
  258. return FIELD_GET(KSZ9477_SW_TOS_DSCP_DEFAULT_PRIO_M,
  259. dscp);
  260. }
  261. /* In case DSCP remapping is enabled, we need to write the DSCP to
  262. * priority mapping table.
  263. */
  264. reg += dscp / per_reg;
  265. ret = ksz_read8(dev, reg, &data);
  266. if (ret)
  267. return ret;
  268. shift = (dscp % per_reg) * (8 / per_reg);
  269. return (data >> shift) & mask;
  270. }
  271. /**
  272. * ksz_set_global_dscp_entry - Sets the global DSCP-to-priority mapping entry
  273. * @dev: Pointer to the KSZ switch device structure
  274. * @dscp: DSCP value for which to set the priority
  275. * @ipm: Priority value to set
  276. *
  277. * This function sets the global DSCP-to-priority mapping entry for the
  278. * specified DSCP value.
  279. *
  280. * Return: 0 on success, or a negative error code on failure.
  281. */
  282. static int ksz_set_global_dscp_entry(struct ksz_device *dev, u8 dscp, u8 ipm)
  283. {
  284. int reg, per_reg, shift;
  285. u8 mask;
  286. ksz_get_dscp_prio_reg(dev, &reg, &per_reg, &mask);
  287. shift = (dscp % per_reg) * (8 / per_reg);
  288. return ksz_rmw8(dev, reg + (dscp / per_reg), mask << shift,
  289. ipm << shift);
  290. }
  291. /**
  292. * ksz_init_global_dscp_map - Initializes the global DSCP-to-priority mapping
  293. * @dev: Pointer to the KSZ switch device structure
  294. *
  295. * This function initializes the global DSCP-to-priority mapping table for the
  296. * switch.
  297. *
  298. * Return: 0 on success, or a negative error code on failure
  299. */
  300. static int ksz_init_global_dscp_map(struct ksz_device *dev)
  301. {
  302. int ret, dscp;
  303. /* On KSZ9xxx variants, DSCP remapping is disabled by default.
  304. * Enable to have, predictable and reproducible behavior across
  305. * different devices.
  306. */
  307. if (!is_ksz8(dev)) {
  308. ret = ksz_rmw8(dev, KSZ9477_REG_SW_MAC_TOS_CTRL,
  309. KSZ9477_SW_TOS_DSCP_REMAP,
  310. KSZ9477_SW_TOS_DSCP_REMAP);
  311. if (ret)
  312. return ret;
  313. }
  314. for (dscp = 0; dscp < DSCP_MAX; dscp++) {
  315. int ipm, tt;
  316. /* Map DSCP to Traffic Type, which is corresponding to the
  317. * Internal Priority Map (IPM) in the switch.
  318. */
  319. if (!is_ksz8(dev)) {
  320. ipm = ietf_dscp_to_ieee8021q_tt(dscp);
  321. } else {
  322. /* On KSZ8xxx variants we do not have IPM to queue
  323. * remapping table. We need to convert DSCP to Traffic
  324. * Type and then to queue.
  325. */
  326. tt = ietf_dscp_to_ieee8021q_tt(dscp);
  327. if (tt < 0)
  328. return tt;
  329. ipm = ieee8021q_tt_to_tc(tt, dev->info->num_tx_queues);
  330. }
  331. if (ipm < 0)
  332. return ipm;
  333. ret = ksz_set_global_dscp_entry(dev, dscp, ipm);
  334. }
  335. return 0;
  336. }
  337. /**
  338. * ksz_port_add_dscp_prio - Adds a DSCP-to-priority mapping entry for a port on
  339. * a KSZ switch.
  340. * @ds: Pointer to the DSA switch structure
  341. * @port: Port number for which to add the DSCP-to-priority mapping entry
  342. * @dscp: DSCP value for which to add the priority
  343. * @prio: Priority value to set
  344. *
  345. * Return: 0 on success, or a negative error code on failure
  346. */
  347. int ksz_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
  348. {
  349. struct ksz_device *dev = ds->priv;
  350. if (prio >= dev->info->num_ipms)
  351. return -ERANGE;
  352. return ksz_set_global_dscp_entry(dev, dscp, prio);
  353. }
  354. /**
  355. * ksz_port_del_dscp_prio - Deletes a DSCP-to-priority mapping entry for a port
  356. * on a KSZ switch.
  357. * @ds: Pointer to the DSA switch structure
  358. * @port: Port number for which to delete the DSCP-to-priority mapping entry
  359. * @dscp: DSCP value for which to delete the priority
  360. * @prio: Priority value to delete
  361. *
  362. * Return: 0 on success, or a negative error code on failure
  363. */
  364. int ksz_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
  365. {
  366. struct ksz_device *dev = ds->priv;
  367. int ipm;
  368. if (ksz_port_get_dscp_prio(ds, port, dscp) != prio)
  369. return 0;
  370. if (is_ksz8(dev)) {
  371. ipm = ieee8021q_tt_to_tc(IEEE8021Q_TT_BE,
  372. dev->info->num_tx_queues);
  373. if (ipm < 0)
  374. return ipm;
  375. } else {
  376. ipm = IEEE8021Q_TT_BE;
  377. }
  378. return ksz_set_global_dscp_entry(dev, dscp, ipm);
  379. }
  380. /**
  381. * ksz_apptrust_error - Prints an error message for an invalid apptrust selector
  382. * @dev: Pointer to the KSZ switch device structure
  383. *
  384. * This function prints an error message when an invalid apptrust selector is
  385. * provided.
  386. */
  387. static void ksz_apptrust_error(struct ksz_device *dev)
  388. {
  389. char supported_apptrust_variants[64];
  390. int i;
  391. supported_apptrust_variants[0] = '\0';
  392. for (i = 0; i < ARRAY_SIZE(ksz_supported_apptrust_variants); i++) {
  393. if (i > 0)
  394. strlcat(supported_apptrust_variants, ", ",
  395. sizeof(supported_apptrust_variants));
  396. strlcat(supported_apptrust_variants,
  397. ksz_supported_apptrust_variants[i],
  398. sizeof(supported_apptrust_variants));
  399. }
  400. dev_err(dev->dev, "Invalid apptrust selector or priority order. Supported: %s\n",
  401. supported_apptrust_variants);
  402. }
  403. /**
  404. * ksz_port_set_apptrust_validate - Validates the apptrust selectors
  405. * @dev: Pointer to the KSZ switch device structure
  406. * @port: Port number for which to set the apptrust selectors
  407. * @sel: Array of apptrust selectors to validate
  408. * @nsel: Number of apptrust selectors in the array
  409. *
  410. * This function validates the apptrust selectors provided and ensures that
  411. * they are in the correct order.
  412. *
  413. * This family of switches supports two apptrust selectors: DCB_APP_SEL_PCP and
  414. * IEEE_8021QAZ_APP_SEL_DSCP. The priority order of the selectors is fixed and
  415. * cannot be changed. The order is as follows:
  416. * 1. DCB_APP_SEL_PCP - Priority Code Point selector (highest priority)
  417. * 2. IEEE_8021QAZ_APP_SEL_DSCP - Differentiated Services Code Point selector
  418. * (lowest priority)
  419. *
  420. * Return: 0 on success, or a negative error code on failure
  421. */
  422. static int ksz_port_set_apptrust_validate(struct ksz_device *dev, int port,
  423. const u8 *sel, int nsel)
  424. {
  425. int i, j, found;
  426. int j_prev = 0;
  427. /* Iterate through the requested selectors */
  428. for (i = 0; i < nsel; i++) {
  429. found = 0;
  430. /* Check if the current selector is supported by the hardware */
  431. for (j = 0; j < sizeof(ksz_supported_apptrust); j++) {
  432. if (sel[i] != ksz_supported_apptrust[j])
  433. continue;
  434. found = 1;
  435. /* Ensure that no higher priority selector (lower index)
  436. * precedes a lower priority one
  437. */
  438. if (i > 0 && j <= j_prev)
  439. goto err_sel_not_vaild;
  440. j_prev = j;
  441. break;
  442. }
  443. if (!found)
  444. goto err_sel_not_vaild;
  445. }
  446. return 0;
  447. err_sel_not_vaild:
  448. ksz_apptrust_error(dev);
  449. return -EINVAL;
  450. }
  451. /**
  452. * ksz88x3_port1_apptrust_quirk - Quirk for apptrust configuration on Port 1
  453. * of KSZ88x3 devices
  454. * @dev: Pointer to the KSZ switch device structure
  455. * @port: Port number for which to set the apptrust selectors
  456. * @reg: Register address for the apptrust configuration
  457. * @port1_data: Data to set for the apptrust configuration
  458. *
  459. * This function implements a quirk for apptrust configuration on Port 1 of
  460. * KSZ88x3 devices. It ensures that apptrust configuration on Port 1 is not
  461. * possible if PCP apptrust on Port 2 is disabled. This is because the Port 2
  462. * seems to be permanently hardwired to PCP classification, so we need to
  463. * do Port 1 configuration always in agreement with Port 2 configuration.
  464. *
  465. * Return: 0 on success, or a negative error code on failure
  466. */
  467. static int ksz88x3_port1_apptrust_quirk(struct ksz_device *dev, int port,
  468. int reg, u8 port1_data)
  469. {
  470. u8 port2_data;
  471. int ret;
  472. /* If no apptrust is requested for Port 1, no need to care about Port 2
  473. * configuration.
  474. */
  475. if (!(port1_data & (KSZ8_PORT_802_1P_ENABLE | KSZ8_PORT_DIFFSERV_ENABLE)))
  476. return 0;
  477. /* We got request to enable any apptrust on Port 1. To make it possible,
  478. * we need to enable multiple queues on the switch. If we enable
  479. * multiqueue support, PCP classification on Port 2 will be
  480. * automatically activated by HW.
  481. */
  482. ret = ksz_pread8(dev, KSZ_PORT_2, reg, &port2_data);
  483. if (ret)
  484. return ret;
  485. /* If KSZ8_PORT_802_1P_ENABLE bit is set on Port 2, the driver showed
  486. * the interest in PCP classification on Port 2. In this case,
  487. * multiqueue support is enabled and we can enable any apptrust on
  488. * Port 1.
  489. * If KSZ8_PORT_802_1P_ENABLE bit is not set on Port 2, the PCP
  490. * classification on Port 2 is still active, but the driver disabled
  491. * multiqueue support and made frame prioritization inactive for
  492. * all ports. In this case, we can't enable any apptrust on Port 1.
  493. */
  494. if (!(port2_data & KSZ8_PORT_802_1P_ENABLE)) {
  495. dev_err(dev->dev, "Not possible to enable any apptrust on Port 1 if PCP apptrust on Port 2 is disabled\n");
  496. return -EINVAL;
  497. }
  498. return 0;
  499. }
  500. /**
  501. * ksz88x3_port2_apptrust_quirk - Quirk for apptrust configuration on Port 2
  502. * of KSZ88x3 devices
  503. * @dev: Pointer to the KSZ switch device structure
  504. * @port: Port number for which to set the apptrust selectors
  505. * @reg: Register address for the apptrust configuration
  506. * @port2_data: Data to set for the apptrust configuration
  507. *
  508. * This function implements a quirk for apptrust configuration on Port 2 of
  509. * KSZ88x3 devices. It ensures that DSCP apptrust is not working on Port 2 and
  510. * that it is not possible to disable PCP on Port 2. The only way to disable PCP
  511. * on Port 2 is to disable multiple queues on the switch.
  512. *
  513. * Return: 0 on success, or a negative error code on failure
  514. */
  515. static int ksz88x3_port2_apptrust_quirk(struct ksz_device *dev, int port,
  516. int reg, u8 port2_data)
  517. {
  518. struct dsa_switch *ds = dev->ds;
  519. u8 port1_data;
  520. int ret;
  521. /* First validate Port 2 configuration. DiffServ/DSCP is not working
  522. * on this port.
  523. */
  524. if (port2_data & KSZ8_PORT_DIFFSERV_ENABLE) {
  525. dev_err(dev->dev, "DSCP apptrust is not working on Port 2\n");
  526. return -EINVAL;
  527. }
  528. /* If PCP support is requested, we need to enable all queues on the
  529. * switch to make PCP priority working on Port 2.
  530. */
  531. if (port2_data & KSZ8_PORT_802_1P_ENABLE)
  532. return ksz8_all_queues_split(dev, dev->info->num_tx_queues);
  533. /* We got request to disable PCP priority on Port 2.
  534. * Now, we need to compare Port 2 configuration with Port 1
  535. * configuration.
  536. */
  537. ret = ksz_pread8(dev, KSZ_PORT_1, reg, &port1_data);
  538. if (ret)
  539. return ret;
  540. /* If Port 1 has any apptrust enabled, we can't disable multiple queues
  541. * on the switch, so we can't disable PCP on Port 2.
  542. */
  543. if (port1_data & (KSZ8_PORT_802_1P_ENABLE | KSZ8_PORT_DIFFSERV_ENABLE)) {
  544. dev_err(dev->dev, "Not possible to disable PCP on Port 2 if any apptrust is enabled on Port 1\n");
  545. return -EINVAL;
  546. }
  547. /* Now we need to ensure that default priority on Port 1 is set to 0
  548. * otherwise we can't disable multiqueue support on the switch.
  549. */
  550. ret = ksz_port_get_default_prio(ds, KSZ_PORT_1);
  551. if (ret < 0) {
  552. return ret;
  553. } else if (ret) {
  554. dev_err(dev->dev, "Not possible to disable PCP on Port 2 if non zero default priority is set on Port 1\n");
  555. return -EINVAL;
  556. }
  557. /* Port 1 has no apptrust or default priority set and we got request to
  558. * disable PCP on Port 2. We can disable multiqueue support to disable
  559. * PCP on Port 2.
  560. */
  561. return ksz8_all_queues_split(dev, 1);
  562. }
  563. /**
  564. * ksz88x3_port_apptrust_quirk - Quirk for apptrust configuration on KSZ88x3
  565. * devices
  566. * @dev: Pointer to the KSZ switch device structure
  567. * @port: Port number for which to set the apptrust selectors
  568. * @reg: Register address for the apptrust configuration
  569. * @data: Data to set for the apptrust configuration
  570. *
  571. * This function implements a quirk for apptrust configuration on KSZ88x3
  572. * devices. It ensures that apptrust configuration on Port 1 and
  573. * Port 2 is done in agreement with each other.
  574. *
  575. * Return: 0 on success, or a negative error code on failure
  576. */
  577. static int ksz88x3_port_apptrust_quirk(struct ksz_device *dev, int port,
  578. int reg, u8 data)
  579. {
  580. if (port == KSZ_PORT_1)
  581. return ksz88x3_port1_apptrust_quirk(dev, port, reg, data);
  582. else if (port == KSZ_PORT_2)
  583. return ksz88x3_port2_apptrust_quirk(dev, port, reg, data);
  584. return 0;
  585. }
  586. /**
  587. * ksz_port_set_apptrust - Sets the apptrust selectors for a port on a KSZ
  588. * switch
  589. * @ds: Pointer to the DSA switch structure
  590. * @port: Port number for which to set the apptrust selectors
  591. * @sel: Array of apptrust selectors to set
  592. * @nsel: Number of apptrust selectors in the array
  593. *
  594. * This function sets the apptrust selectors for the specified port on a KSZ
  595. * switch.
  596. *
  597. * Return: 0 on success, or a negative error code on failure
  598. */
  599. int ksz_port_set_apptrust(struct dsa_switch *ds, int port,
  600. const u8 *sel, int nsel)
  601. {
  602. const struct ksz_apptrust_map *map;
  603. struct ksz_device *dev = ds->priv;
  604. int reg, i, ret;
  605. u8 data = 0;
  606. u8 mask;
  607. ret = ksz_port_set_apptrust_validate(dev, port, sel, nsel);
  608. if (ret)
  609. return ret;
  610. ksz_get_apptrust_map_and_reg(dev, &map, &reg, &mask);
  611. for (i = 0; i < nsel; i++) {
  612. int j;
  613. for (j = 0; j < ARRAY_SIZE(ksz_supported_apptrust); j++) {
  614. if (sel[i] != ksz_supported_apptrust[j])
  615. continue;
  616. data |= map[j].bit;
  617. break;
  618. }
  619. }
  620. if (ksz_is_ksz88x3(dev)) {
  621. ret = ksz88x3_port_apptrust_quirk(dev, port, reg, data);
  622. if (ret)
  623. return ret;
  624. }
  625. return ksz_prmw8(dev, port, reg, mask, data);
  626. }
  627. /**
  628. * ksz_port_get_apptrust - Retrieves the apptrust selectors for a port on a KSZ
  629. * switch
  630. * @ds: Pointer to the DSA switch structure
  631. * @port: Port number for which to get the apptrust selectors
  632. * @sel: Array to store the apptrust selectors
  633. * @nsel: Number of apptrust selectors in the array
  634. *
  635. * This function fetches the apptrust selectors for the specified port on a KSZ
  636. * switch.
  637. *
  638. * Return: 0 on success, or a negative error code on failure
  639. */
  640. int ksz_port_get_apptrust(struct dsa_switch *ds, int port, u8 *sel, int *nsel)
  641. {
  642. const struct ksz_apptrust_map *map;
  643. struct ksz_device *dev = ds->priv;
  644. int reg, i, ret;
  645. u8 data;
  646. u8 mask;
  647. ksz_get_apptrust_map_and_reg(dev, &map, &reg, &mask);
  648. ret = ksz_pread8(dev, port, reg, &data);
  649. if (ret)
  650. return ret;
  651. *nsel = 0;
  652. for (i = 0; i < ARRAY_SIZE(ksz_supported_apptrust); i++) {
  653. if (data & map[i].bit)
  654. sel[(*nsel)++] = ksz_supported_apptrust[i];
  655. }
  656. return 0;
  657. }
  658. /**
  659. * ksz_dcb_init_port - Initializes the DCB configuration for a port on a KSZ
  660. * @dev: Pointer to the KSZ switch device structure
  661. * @port: Port number for which to initialize the DCB configuration
  662. *
  663. * This function initializes the DCB configuration for the specified port on a
  664. * KSZ switch. Particular DCB configuration is set for the port, including the
  665. * default priority and apptrust selectors.
  666. * The default priority is set to Best Effort, and the apptrust selectors are
  667. * set to all supported selectors.
  668. *
  669. * Return: 0 on success, or a negative error code on failure
  670. */
  671. int ksz_dcb_init_port(struct ksz_device *dev, int port)
  672. {
  673. const u8 ksz_default_apptrust[] = { DCB_APP_SEL_PCP };
  674. int ret, ipm;
  675. if (is_ksz8(dev)) {
  676. ipm = ieee8021q_tt_to_tc(IEEE8021Q_TT_BE,
  677. dev->info->num_tx_queues);
  678. if (ipm < 0)
  679. return ipm;
  680. } else {
  681. ipm = IEEE8021Q_TT_BE;
  682. }
  683. /* Set the default priority for the port to Best Effort */
  684. ret = ksz_port_set_default_prio(dev->ds, port, ipm);
  685. if (ret)
  686. return ret;
  687. return ksz_port_set_apptrust(dev->ds, port, ksz_default_apptrust,
  688. ARRAY_SIZE(ksz_default_apptrust));
  689. }
  690. /**
  691. * ksz_dcb_init - Initializes the DCB configuration for a KSZ switch
  692. * @dev: Pointer to the KSZ switch device structure
  693. *
  694. * This function initializes the DCB configuration for a KSZ switch. The global
  695. * DSCP-to-priority mapping table is initialized.
  696. *
  697. * Return: 0 on success, or a negative error code on failure
  698. */
  699. int ksz_dcb_init(struct ksz_device *dev)
  700. {
  701. int ret;
  702. ret = ksz_init_global_dscp_map(dev);
  703. if (ret)
  704. return ret;
  705. /* Enable 802.1p priority control on Port 2 during switch initialization.
  706. * This setup is critical for the apptrust functionality on Port 1, which
  707. * relies on the priority settings of Port 2. Note: Port 1 is naturally
  708. * configured before Port 2, necessitating this configuration order.
  709. */
  710. if (ksz_is_ksz88x3(dev))
  711. return ksz_prmw8(dev, KSZ_PORT_2, KSZ8_REG_PORT_1_CTRL_0,
  712. KSZ8_PORT_802_1P_ENABLE,
  713. KSZ8_PORT_802_1P_ENABLE);
  714. return 0;
  715. }