1a4ae997aSVladimir Oltean // SPDX-License-Identifier: (GPL-2.0 OR MIT)
23c9cfb52SVladimir Oltean /* Copyright 2020-2021 NXP
3a4ae997aSVladimir Oltean */
4a4ae997aSVladimir Oltean #include <net/devlink.h>
5a4ae997aSVladimir Oltean #include "ocelot.h"
6a4ae997aSVladimir Oltean
7a4ae997aSVladimir Oltean /* The queue system tracks four resource consumptions:
8a4ae997aSVladimir Oltean * Resource 0: Memory tracked per source port
9a4ae997aSVladimir Oltean * Resource 1: Frame references tracked per source port
10a4ae997aSVladimir Oltean * Resource 2: Memory tracked per destination port
11a4ae997aSVladimir Oltean * Resource 3: Frame references tracked per destination port
12a4ae997aSVladimir Oltean */
13a4ae997aSVladimir Oltean #define OCELOT_RESOURCE_SZ 256
14a4ae997aSVladimir Oltean #define OCELOT_NUM_RESOURCES 4
15a4ae997aSVladimir Oltean
16a4ae997aSVladimir Oltean #define BUF_xxxx_I (0 * OCELOT_RESOURCE_SZ)
17a4ae997aSVladimir Oltean #define REF_xxxx_I (1 * OCELOT_RESOURCE_SZ)
18a4ae997aSVladimir Oltean #define BUF_xxxx_E (2 * OCELOT_RESOURCE_SZ)
19a4ae997aSVladimir Oltean #define REF_xxxx_E (3 * OCELOT_RESOURCE_SZ)
20a4ae997aSVladimir Oltean
21a4ae997aSVladimir Oltean /* For each resource type there are 4 types of watermarks:
22a4ae997aSVladimir Oltean * Q_RSRV: reservation per QoS class per port
23a4ae997aSVladimir Oltean * PRIO_SHR: sharing watermark per QoS class across all ports
24a4ae997aSVladimir Oltean * P_RSRV: reservation per port
25a4ae997aSVladimir Oltean * COL_SHR: sharing watermark per color (drop precedence) across all ports
26a4ae997aSVladimir Oltean */
27a4ae997aSVladimir Oltean #define xxx_Q_RSRV_x 0
28a4ae997aSVladimir Oltean #define xxx_PRIO_SHR_x 216
29a4ae997aSVladimir Oltean #define xxx_P_RSRV_x 224
30a4ae997aSVladimir Oltean #define xxx_COL_SHR_x 254
31a4ae997aSVladimir Oltean
32a4ae997aSVladimir Oltean /* Reservation Watermarks
33a4ae997aSVladimir Oltean * ----------------------
34a4ae997aSVladimir Oltean *
35a4ae997aSVladimir Oltean * For setting up the reserved areas, egress watermarks exist per port and per
36a4ae997aSVladimir Oltean * QoS class for both ingress and egress.
37a4ae997aSVladimir Oltean */
38a4ae997aSVladimir Oltean
39a4ae997aSVladimir Oltean /* Amount of packet buffer
40a4ae997aSVladimir Oltean * | per QoS class
41a4ae997aSVladimir Oltean * | | reserved
42a4ae997aSVladimir Oltean * | | | per egress port
43a4ae997aSVladimir Oltean * | | | |
44a4ae997aSVladimir Oltean * V V v v
45a4ae997aSVladimir Oltean * BUF_Q_RSRV_E
46a4ae997aSVladimir Oltean */
47a4ae997aSVladimir Oltean #define BUF_Q_RSRV_E(port, prio) \
48a4ae997aSVladimir Oltean (BUF_xxxx_E + xxx_Q_RSRV_x + OCELOT_NUM_TC * (port) + (prio))
49a4ae997aSVladimir Oltean
50a4ae997aSVladimir Oltean /* Amount of packet buffer
51a4ae997aSVladimir Oltean * | for all port's traffic classes
52a4ae997aSVladimir Oltean * | | reserved
53a4ae997aSVladimir Oltean * | | | per egress port
54a4ae997aSVladimir Oltean * | | | |
55a4ae997aSVladimir Oltean * V V v v
56a4ae997aSVladimir Oltean * BUF_P_RSRV_E
57a4ae997aSVladimir Oltean */
58a4ae997aSVladimir Oltean #define BUF_P_RSRV_E(port) \
59a4ae997aSVladimir Oltean (BUF_xxxx_E + xxx_P_RSRV_x + (port))
60a4ae997aSVladimir Oltean
61a4ae997aSVladimir Oltean /* Amount of packet buffer
62a4ae997aSVladimir Oltean * | per QoS class
63a4ae997aSVladimir Oltean * | | reserved
64a4ae997aSVladimir Oltean * | | | per ingress port
65a4ae997aSVladimir Oltean * | | | |
66a4ae997aSVladimir Oltean * V V v v
67a4ae997aSVladimir Oltean * BUF_Q_RSRV_I
68a4ae997aSVladimir Oltean */
69a4ae997aSVladimir Oltean #define BUF_Q_RSRV_I(port, prio) \
70a4ae997aSVladimir Oltean (BUF_xxxx_I + xxx_Q_RSRV_x + OCELOT_NUM_TC * (port) + (prio))
71a4ae997aSVladimir Oltean
72a4ae997aSVladimir Oltean /* Amount of packet buffer
73a4ae997aSVladimir Oltean * | for all port's traffic classes
74a4ae997aSVladimir Oltean * | | reserved
75a4ae997aSVladimir Oltean * | | | per ingress port
76a4ae997aSVladimir Oltean * | | | |
77a4ae997aSVladimir Oltean * V V v v
78a4ae997aSVladimir Oltean * BUF_P_RSRV_I
79a4ae997aSVladimir Oltean */
80a4ae997aSVladimir Oltean #define BUF_P_RSRV_I(port) \
81a4ae997aSVladimir Oltean (BUF_xxxx_I + xxx_P_RSRV_x + (port))
82a4ae997aSVladimir Oltean
83a4ae997aSVladimir Oltean /* Amount of frame references
84a4ae997aSVladimir Oltean * | per QoS class
85a4ae997aSVladimir Oltean * | | reserved
86a4ae997aSVladimir Oltean * | | | per egress port
87a4ae997aSVladimir Oltean * | | | |
88a4ae997aSVladimir Oltean * V V v v
89a4ae997aSVladimir Oltean * REF_Q_RSRV_E
90a4ae997aSVladimir Oltean */
91a4ae997aSVladimir Oltean #define REF_Q_RSRV_E(port, prio) \
92a4ae997aSVladimir Oltean (REF_xxxx_E + xxx_Q_RSRV_x + OCELOT_NUM_TC * (port) + (prio))
93a4ae997aSVladimir Oltean
94a4ae997aSVladimir Oltean /* Amount of frame references
95a4ae997aSVladimir Oltean * | for all port's traffic classes
96a4ae997aSVladimir Oltean * | | reserved
97a4ae997aSVladimir Oltean * | | | per egress port
98a4ae997aSVladimir Oltean * | | | |
99a4ae997aSVladimir Oltean * V V v v
100a4ae997aSVladimir Oltean * REF_P_RSRV_E
101a4ae997aSVladimir Oltean */
102a4ae997aSVladimir Oltean #define REF_P_RSRV_E(port) \
103a4ae997aSVladimir Oltean (REF_xxxx_E + xxx_P_RSRV_x + (port))
104a4ae997aSVladimir Oltean
105a4ae997aSVladimir Oltean /* Amount of frame references
106a4ae997aSVladimir Oltean * | per QoS class
107a4ae997aSVladimir Oltean * | | reserved
108a4ae997aSVladimir Oltean * | | | per ingress port
109a4ae997aSVladimir Oltean * | | | |
110a4ae997aSVladimir Oltean * V V v v
111a4ae997aSVladimir Oltean * REF_Q_RSRV_I
112a4ae997aSVladimir Oltean */
113a4ae997aSVladimir Oltean #define REF_Q_RSRV_I(port, prio) \
114a4ae997aSVladimir Oltean (REF_xxxx_I + xxx_Q_RSRV_x + OCELOT_NUM_TC * (port) + (prio))
115a4ae997aSVladimir Oltean
116a4ae997aSVladimir Oltean /* Amount of frame references
117a4ae997aSVladimir Oltean * | for all port's traffic classes
118a4ae997aSVladimir Oltean * | | reserved
119a4ae997aSVladimir Oltean * | | | per ingress port
120a4ae997aSVladimir Oltean * | | | |
121a4ae997aSVladimir Oltean * V V v v
122a4ae997aSVladimir Oltean * REF_P_RSRV_I
123a4ae997aSVladimir Oltean */
124a4ae997aSVladimir Oltean #define REF_P_RSRV_I(port) \
125a4ae997aSVladimir Oltean (REF_xxxx_I + xxx_P_RSRV_x + (port))
126a4ae997aSVladimir Oltean
127a4ae997aSVladimir Oltean /* Sharing Watermarks
128a4ae997aSVladimir Oltean * ------------------
129a4ae997aSVladimir Oltean *
130a4ae997aSVladimir Oltean * The shared memory area is shared between all ports.
131a4ae997aSVladimir Oltean */
132a4ae997aSVladimir Oltean
133a4ae997aSVladimir Oltean /* Amount of buffer
134a4ae997aSVladimir Oltean * | per QoS class
135a4ae997aSVladimir Oltean * | | from the shared memory area
136a4ae997aSVladimir Oltean * | | | for egress traffic
137a4ae997aSVladimir Oltean * | | | |
138a4ae997aSVladimir Oltean * V V v v
139a4ae997aSVladimir Oltean * BUF_PRIO_SHR_E
140a4ae997aSVladimir Oltean */
141a4ae997aSVladimir Oltean #define BUF_PRIO_SHR_E(prio) \
142a4ae997aSVladimir Oltean (BUF_xxxx_E + xxx_PRIO_SHR_x + (prio))
143a4ae997aSVladimir Oltean
144a4ae997aSVladimir Oltean /* Amount of buffer
145a4ae997aSVladimir Oltean * | per color (drop precedence level)
146a4ae997aSVladimir Oltean * | | from the shared memory area
147a4ae997aSVladimir Oltean * | | | for egress traffic
148a4ae997aSVladimir Oltean * | | | |
149a4ae997aSVladimir Oltean * V V v v
150a4ae997aSVladimir Oltean * BUF_COL_SHR_E
151a4ae997aSVladimir Oltean */
152a4ae997aSVladimir Oltean #define BUF_COL_SHR_E(dp) \
153a4ae997aSVladimir Oltean (BUF_xxxx_E + xxx_COL_SHR_x + (1 - (dp)))
154a4ae997aSVladimir Oltean
155a4ae997aSVladimir Oltean /* Amount of buffer
156a4ae997aSVladimir Oltean * | per QoS class
157a4ae997aSVladimir Oltean * | | from the shared memory area
158a4ae997aSVladimir Oltean * | | | for ingress traffic
159a4ae997aSVladimir Oltean * | | | |
160a4ae997aSVladimir Oltean * V V v v
161a4ae997aSVladimir Oltean * BUF_PRIO_SHR_I
162a4ae997aSVladimir Oltean */
163a4ae997aSVladimir Oltean #define BUF_PRIO_SHR_I(prio) \
164a4ae997aSVladimir Oltean (BUF_xxxx_I + xxx_PRIO_SHR_x + (prio))
165a4ae997aSVladimir Oltean
166a4ae997aSVladimir Oltean /* Amount of buffer
167a4ae997aSVladimir Oltean * | per color (drop precedence level)
168a4ae997aSVladimir Oltean * | | from the shared memory area
169a4ae997aSVladimir Oltean * | | | for ingress traffic
170a4ae997aSVladimir Oltean * | | | |
171a4ae997aSVladimir Oltean * V V v v
172a4ae997aSVladimir Oltean * BUF_COL_SHR_I
173a4ae997aSVladimir Oltean */
174a4ae997aSVladimir Oltean #define BUF_COL_SHR_I(dp) \
175a4ae997aSVladimir Oltean (BUF_xxxx_I + xxx_COL_SHR_x + (1 - (dp)))
176a4ae997aSVladimir Oltean
177a4ae997aSVladimir Oltean /* Amount of frame references
178a4ae997aSVladimir Oltean * | per QoS class
179a4ae997aSVladimir Oltean * | | from the shared area
180a4ae997aSVladimir Oltean * | | | for egress traffic
181a4ae997aSVladimir Oltean * | | | |
182a4ae997aSVladimir Oltean * V V v v
183a4ae997aSVladimir Oltean * REF_PRIO_SHR_E
184a4ae997aSVladimir Oltean */
185a4ae997aSVladimir Oltean #define REF_PRIO_SHR_E(prio) \
186a4ae997aSVladimir Oltean (REF_xxxx_E + xxx_PRIO_SHR_x + (prio))
187a4ae997aSVladimir Oltean
188a4ae997aSVladimir Oltean /* Amount of frame references
189a4ae997aSVladimir Oltean * | per color (drop precedence level)
190a4ae997aSVladimir Oltean * | | from the shared area
191a4ae997aSVladimir Oltean * | | | for egress traffic
192a4ae997aSVladimir Oltean * | | | |
193a4ae997aSVladimir Oltean * V V v v
194a4ae997aSVladimir Oltean * REF_COL_SHR_E
195a4ae997aSVladimir Oltean */
196a4ae997aSVladimir Oltean #define REF_COL_SHR_E(dp) \
197a4ae997aSVladimir Oltean (REF_xxxx_E + xxx_COL_SHR_x + (1 - (dp)))
198a4ae997aSVladimir Oltean
199a4ae997aSVladimir Oltean /* Amount of frame references
200a4ae997aSVladimir Oltean * | per QoS class
201a4ae997aSVladimir Oltean * | | from the shared area
202a4ae997aSVladimir Oltean * | | | for ingress traffic
203a4ae997aSVladimir Oltean * | | | |
204a4ae997aSVladimir Oltean * V V v v
205a4ae997aSVladimir Oltean * REF_PRIO_SHR_I
206a4ae997aSVladimir Oltean */
207a4ae997aSVladimir Oltean #define REF_PRIO_SHR_I(prio) \
208a4ae997aSVladimir Oltean (REF_xxxx_I + xxx_PRIO_SHR_x + (prio))
209a4ae997aSVladimir Oltean
210a4ae997aSVladimir Oltean /* Amount of frame references
211a4ae997aSVladimir Oltean * | per color (drop precedence level)
212a4ae997aSVladimir Oltean * | | from the shared area
213a4ae997aSVladimir Oltean * | | | for ingress traffic
214a4ae997aSVladimir Oltean * | | | |
215a4ae997aSVladimir Oltean * V V v v
216a4ae997aSVladimir Oltean * REF_COL_SHR_I
217a4ae997aSVladimir Oltean */
218a4ae997aSVladimir Oltean #define REF_COL_SHR_I(dp) \
219a4ae997aSVladimir Oltean (REF_xxxx_I + xxx_COL_SHR_x + (1 - (dp)))
220a4ae997aSVladimir Oltean
ocelot_wm_read(struct ocelot * ocelot,int index)221a4ae997aSVladimir Oltean static u32 ocelot_wm_read(struct ocelot *ocelot, int index)
222a4ae997aSVladimir Oltean {
223a4ae997aSVladimir Oltean int wm = ocelot_read_gix(ocelot, QSYS_RES_CFG, index);
224a4ae997aSVladimir Oltean
225a4ae997aSVladimir Oltean return ocelot->ops->wm_dec(wm);
226a4ae997aSVladimir Oltean }
227a4ae997aSVladimir Oltean
ocelot_wm_write(struct ocelot * ocelot,int index,u32 val)228a4ae997aSVladimir Oltean static void ocelot_wm_write(struct ocelot *ocelot, int index, u32 val)
229a4ae997aSVladimir Oltean {
230a4ae997aSVladimir Oltean u32 wm = ocelot->ops->wm_enc(val);
231a4ae997aSVladimir Oltean
232a4ae997aSVladimir Oltean ocelot_write_gix(ocelot, wm, QSYS_RES_CFG, index);
233a4ae997aSVladimir Oltean }
234a4ae997aSVladimir Oltean
ocelot_wm_status(struct ocelot * ocelot,int index,u32 * inuse,u32 * maxuse)235f59fd9caSVladimir Oltean static void ocelot_wm_status(struct ocelot *ocelot, int index, u32 *inuse,
236f59fd9caSVladimir Oltean u32 *maxuse)
237f59fd9caSVladimir Oltean {
238f59fd9caSVladimir Oltean int res_stat = ocelot_read_gix(ocelot, QSYS_RES_STAT, index);
239f59fd9caSVladimir Oltean
240f59fd9caSVladimir Oltean return ocelot->ops->wm_stat(res_stat, inuse, maxuse);
241f59fd9caSVladimir Oltean }
242f59fd9caSVladimir Oltean
243a4ae997aSVladimir Oltean /* The hardware comes out of reset with strange defaults: the sum of all
244a4ae997aSVladimir Oltean * reservations for frame memory is larger than the total buffer size.
245a4ae997aSVladimir Oltean * One has to wonder how can the reservation watermarks still guarantee
246a4ae997aSVladimir Oltean * anything under congestion.
247a4ae997aSVladimir Oltean * Bring some sense into the hardware by changing the defaults to disable all
248a4ae997aSVladimir Oltean * reservations and rely only on the sharing watermark for frames with drop
249a4ae997aSVladimir Oltean * precedence 0. The user can still explicitly request reservations per port
250a4ae997aSVladimir Oltean * and per port-tc through devlink-sb.
251a4ae997aSVladimir Oltean */
ocelot_disable_reservation_watermarks(struct ocelot * ocelot,int port)252a4ae997aSVladimir Oltean static void ocelot_disable_reservation_watermarks(struct ocelot *ocelot,
253a4ae997aSVladimir Oltean int port)
254a4ae997aSVladimir Oltean {
255a4ae997aSVladimir Oltean int prio;
256a4ae997aSVladimir Oltean
257a4ae997aSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
258a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_Q_RSRV_I(port, prio), 0);
259a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_Q_RSRV_E(port, prio), 0);
260a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_Q_RSRV_I(port, prio), 0);
261a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_Q_RSRV_E(port, prio), 0);
262a4ae997aSVladimir Oltean }
263a4ae997aSVladimir Oltean
264a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_P_RSRV_I(port), 0);
265a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_P_RSRV_E(port), 0);
266a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_P_RSRV_I(port), 0);
267a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_P_RSRV_E(port), 0);
268a4ae997aSVladimir Oltean }
269a4ae997aSVladimir Oltean
270a4ae997aSVladimir Oltean /* We want the sharing watermarks to consume all nonreserved resources, for
271a4ae997aSVladimir Oltean * efficient resource utilization (a single traffic flow should be able to use
272a4ae997aSVladimir Oltean * up the entire buffer space and frame resources as long as there's no
273a4ae997aSVladimir Oltean * interference).
274a4ae997aSVladimir Oltean * The switch has 10 sharing watermarks per lookup: 8 per traffic class and 2
275a4ae997aSVladimir Oltean * per color (drop precedence).
276a4ae997aSVladimir Oltean * The trouble with configuring these sharing watermarks is that:
277a4ae997aSVladimir Oltean * (1) There's a risk that we overcommit the resources if we configure
278a4ae997aSVladimir Oltean * (a) all 8 per-TC sharing watermarks to the max
279a4ae997aSVladimir Oltean * (b) all 2 per-color sharing watermarks to the max
280a4ae997aSVladimir Oltean * (2) There's a risk that we undercommit the resources if we configure
281a4ae997aSVladimir Oltean * (a) all 8 per-TC sharing watermarks to "max / 8"
282a4ae997aSVladimir Oltean * (b) all 2 per-color sharing watermarks to "max / 2"
283a4ae997aSVladimir Oltean * So for Linux, let's just disable the sharing watermarks per traffic class
284a4ae997aSVladimir Oltean * (setting them to 0 will make them always exceeded), and rely only on the
285a4ae997aSVladimir Oltean * sharing watermark for drop priority 0. So frames with drop priority set to 1
286a4ae997aSVladimir Oltean * by QoS classification or policing will still be allowed, but only as long as
287a4ae997aSVladimir Oltean * the port and port-TC reservations are not exceeded.
288a4ae997aSVladimir Oltean */
ocelot_disable_tc_sharing_watermarks(struct ocelot * ocelot)289a4ae997aSVladimir Oltean static void ocelot_disable_tc_sharing_watermarks(struct ocelot *ocelot)
290a4ae997aSVladimir Oltean {
291a4ae997aSVladimir Oltean int prio;
292a4ae997aSVladimir Oltean
293a4ae997aSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
294a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_PRIO_SHR_I(prio), 0);
295a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_PRIO_SHR_E(prio), 0);
296a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_PRIO_SHR_I(prio), 0);
297a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_PRIO_SHR_E(prio), 0);
298a4ae997aSVladimir Oltean }
299a4ae997aSVladimir Oltean }
300a4ae997aSVladimir Oltean
ocelot_get_buf_rsrv(struct ocelot * ocelot,u32 * buf_rsrv_i,u32 * buf_rsrv_e)301a4ae997aSVladimir Oltean static void ocelot_get_buf_rsrv(struct ocelot *ocelot, u32 *buf_rsrv_i,
302a4ae997aSVladimir Oltean u32 *buf_rsrv_e)
303a4ae997aSVladimir Oltean {
304a4ae997aSVladimir Oltean int port, prio;
305a4ae997aSVladimir Oltean
306a4ae997aSVladimir Oltean *buf_rsrv_i = 0;
307a4ae997aSVladimir Oltean *buf_rsrv_e = 0;
308a4ae997aSVladimir Oltean
309a4ae997aSVladimir Oltean for (port = 0; port <= ocelot->num_phys_ports; port++) {
310a4ae997aSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
311a4ae997aSVladimir Oltean *buf_rsrv_i += ocelot_wm_read(ocelot,
312a4ae997aSVladimir Oltean BUF_Q_RSRV_I(port, prio));
313a4ae997aSVladimir Oltean *buf_rsrv_e += ocelot_wm_read(ocelot,
314a4ae997aSVladimir Oltean BUF_Q_RSRV_E(port, prio));
315a4ae997aSVladimir Oltean }
316a4ae997aSVladimir Oltean
317a4ae997aSVladimir Oltean *buf_rsrv_i += ocelot_wm_read(ocelot, BUF_P_RSRV_I(port));
318a4ae997aSVladimir Oltean *buf_rsrv_e += ocelot_wm_read(ocelot, BUF_P_RSRV_E(port));
319a4ae997aSVladimir Oltean }
320a4ae997aSVladimir Oltean
321a4ae997aSVladimir Oltean *buf_rsrv_i *= OCELOT_BUFFER_CELL_SZ;
322a4ae997aSVladimir Oltean *buf_rsrv_e *= OCELOT_BUFFER_CELL_SZ;
323a4ae997aSVladimir Oltean }
324a4ae997aSVladimir Oltean
ocelot_get_ref_rsrv(struct ocelot * ocelot,u32 * ref_rsrv_i,u32 * ref_rsrv_e)325a4ae997aSVladimir Oltean static void ocelot_get_ref_rsrv(struct ocelot *ocelot, u32 *ref_rsrv_i,
326a4ae997aSVladimir Oltean u32 *ref_rsrv_e)
327a4ae997aSVladimir Oltean {
328a4ae997aSVladimir Oltean int port, prio;
329a4ae997aSVladimir Oltean
330a4ae997aSVladimir Oltean *ref_rsrv_i = 0;
331a4ae997aSVladimir Oltean *ref_rsrv_e = 0;
332a4ae997aSVladimir Oltean
333a4ae997aSVladimir Oltean for (port = 0; port <= ocelot->num_phys_ports; port++) {
334a4ae997aSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
335a4ae997aSVladimir Oltean *ref_rsrv_i += ocelot_wm_read(ocelot,
336a4ae997aSVladimir Oltean REF_Q_RSRV_I(port, prio));
337a4ae997aSVladimir Oltean *ref_rsrv_e += ocelot_wm_read(ocelot,
338a4ae997aSVladimir Oltean REF_Q_RSRV_E(port, prio));
339a4ae997aSVladimir Oltean }
340a4ae997aSVladimir Oltean
341a4ae997aSVladimir Oltean *ref_rsrv_i += ocelot_wm_read(ocelot, REF_P_RSRV_I(port));
342a4ae997aSVladimir Oltean *ref_rsrv_e += ocelot_wm_read(ocelot, REF_P_RSRV_E(port));
343a4ae997aSVladimir Oltean }
344a4ae997aSVladimir Oltean }
345a4ae997aSVladimir Oltean
346a4ae997aSVladimir Oltean /* Calculate all reservations, then set up the sharing watermark for DP=0 to
347a4ae997aSVladimir Oltean * consume the remaining resources up to the pool's configured size.
348a4ae997aSVladimir Oltean */
ocelot_setup_sharing_watermarks(struct ocelot * ocelot)349a4ae997aSVladimir Oltean static void ocelot_setup_sharing_watermarks(struct ocelot *ocelot)
350a4ae997aSVladimir Oltean {
351a4ae997aSVladimir Oltean u32 buf_rsrv_i, buf_rsrv_e;
352a4ae997aSVladimir Oltean u32 ref_rsrv_i, ref_rsrv_e;
353a4ae997aSVladimir Oltean u32 buf_shr_i, buf_shr_e;
354a4ae997aSVladimir Oltean u32 ref_shr_i, ref_shr_e;
355a4ae997aSVladimir Oltean
356a4ae997aSVladimir Oltean ocelot_get_buf_rsrv(ocelot, &buf_rsrv_i, &buf_rsrv_e);
357a4ae997aSVladimir Oltean ocelot_get_ref_rsrv(ocelot, &ref_rsrv_i, &ref_rsrv_e);
358a4ae997aSVladimir Oltean
359f59fd9caSVladimir Oltean buf_shr_i = ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_ING] -
360f59fd9caSVladimir Oltean buf_rsrv_i;
361f59fd9caSVladimir Oltean buf_shr_e = ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_EGR] -
362f59fd9caSVladimir Oltean buf_rsrv_e;
363f59fd9caSVladimir Oltean ref_shr_i = ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_ING] -
364f59fd9caSVladimir Oltean ref_rsrv_i;
365f59fd9caSVladimir Oltean ref_shr_e = ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_EGR] -
366f59fd9caSVladimir Oltean ref_rsrv_e;
367a4ae997aSVladimir Oltean
368a4ae997aSVladimir Oltean buf_shr_i /= OCELOT_BUFFER_CELL_SZ;
369a4ae997aSVladimir Oltean buf_shr_e /= OCELOT_BUFFER_CELL_SZ;
370a4ae997aSVladimir Oltean
371a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_COL_SHR_I(0), buf_shr_i);
372a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_COL_SHR_E(0), buf_shr_e);
373a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_COL_SHR_E(0), ref_shr_e);
374a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_COL_SHR_I(0), ref_shr_i);
375a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_COL_SHR_I(1), 0);
376a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, BUF_COL_SHR_E(1), 0);
377a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_COL_SHR_E(1), 0);
378a4ae997aSVladimir Oltean ocelot_wm_write(ocelot, REF_COL_SHR_I(1), 0);
379a4ae997aSVladimir Oltean }
380a4ae997aSVladimir Oltean
381f59fd9caSVladimir Oltean /* Ensure that all reservations can be enforced */
ocelot_watermark_validate(struct ocelot * ocelot,struct netlink_ext_ack * extack)382f59fd9caSVladimir Oltean static int ocelot_watermark_validate(struct ocelot *ocelot,
383f59fd9caSVladimir Oltean struct netlink_ext_ack *extack)
384f59fd9caSVladimir Oltean {
385f59fd9caSVladimir Oltean u32 buf_rsrv_i, buf_rsrv_e;
386f59fd9caSVladimir Oltean u32 ref_rsrv_i, ref_rsrv_e;
387f59fd9caSVladimir Oltean
388f59fd9caSVladimir Oltean ocelot_get_buf_rsrv(ocelot, &buf_rsrv_i, &buf_rsrv_e);
389f59fd9caSVladimir Oltean ocelot_get_ref_rsrv(ocelot, &ref_rsrv_i, &ref_rsrv_e);
390f59fd9caSVladimir Oltean
391f59fd9caSVladimir Oltean if (buf_rsrv_i > ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_ING]) {
392f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
393f59fd9caSVladimir Oltean "Ingress frame reservations exceed pool size");
394f59fd9caSVladimir Oltean return -ERANGE;
395f59fd9caSVladimir Oltean }
396f59fd9caSVladimir Oltean if (buf_rsrv_e > ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_EGR]) {
397f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
398f59fd9caSVladimir Oltean "Egress frame reservations exceed pool size");
399f59fd9caSVladimir Oltean return -ERANGE;
400f59fd9caSVladimir Oltean }
401f59fd9caSVladimir Oltean if (ref_rsrv_i > ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_ING]) {
402f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
403f59fd9caSVladimir Oltean "Ingress reference reservations exceed pool size");
404f59fd9caSVladimir Oltean return -ERANGE;
405f59fd9caSVladimir Oltean }
406f59fd9caSVladimir Oltean if (ref_rsrv_e > ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_EGR]) {
407f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
408f59fd9caSVladimir Oltean "Egress reference reservations exceed pool size");
409f59fd9caSVladimir Oltean return -ERANGE;
410f59fd9caSVladimir Oltean }
411f59fd9caSVladimir Oltean
412f59fd9caSVladimir Oltean return 0;
413f59fd9caSVladimir Oltean }
414f59fd9caSVladimir Oltean
415a4ae997aSVladimir Oltean /* The hardware works like this:
416a4ae997aSVladimir Oltean *
417a4ae997aSVladimir Oltean * Frame forwarding decision taken
418a4ae997aSVladimir Oltean * |
419a4ae997aSVladimir Oltean * v
420a4ae997aSVladimir Oltean * +--------------------+--------------------+--------------------+
421a4ae997aSVladimir Oltean * | | | |
422a4ae997aSVladimir Oltean * v v v v
423a4ae997aSVladimir Oltean * Ingress memory Egress memory Ingress frame Egress frame
424a4ae997aSVladimir Oltean * check check reference check reference check
425a4ae997aSVladimir Oltean * | | | |
426a4ae997aSVladimir Oltean * v v v v
427a4ae997aSVladimir Oltean * BUF_Q_RSRV_I ok BUF_Q_RSRV_E ok REF_Q_RSRV_I ok REF_Q_RSRV_E ok
428a4ae997aSVladimir Oltean *(src port, prio) -+ (dst port, prio) -+ (src port, prio) -+ (dst port, prio) -+
429a4ae997aSVladimir Oltean * | | | | | | | |
430a4ae997aSVladimir Oltean * |exceeded | |exceeded | |exceeded | |exceeded |
431a4ae997aSVladimir Oltean * v | v | v | v |
432a4ae997aSVladimir Oltean * BUF_P_RSRV_I ok| BUF_P_RSRV_E ok| REF_P_RSRV_I ok| REF_P_RSRV_E ok|
433a4ae997aSVladimir Oltean * (src port) ----+ (dst port) ----+ (src port) ----+ (dst port) -----+
434a4ae997aSVladimir Oltean * | | | | | | | |
435a4ae997aSVladimir Oltean * |exceeded | |exceeded | |exceeded | |exceeded |
436a4ae997aSVladimir Oltean * v | v | v | v |
437a4ae997aSVladimir Oltean * BUF_PRIO_SHR_I ok| BUF_PRIO_SHR_E ok| REF_PRIO_SHR_I ok| REF_PRIO_SHR_E ok|
438a4ae997aSVladimir Oltean * (prio) ------+ (prio) ------+ (prio) ------+ (prio) -------+
439a4ae997aSVladimir Oltean * | | | | | | | |
440a4ae997aSVladimir Oltean * |exceeded | |exceeded | |exceeded | |exceeded |
441a4ae997aSVladimir Oltean * v | v | v | v |
442a4ae997aSVladimir Oltean * BUF_COL_SHR_I ok| BUF_COL_SHR_E ok| REF_COL_SHR_I ok| REF_COL_SHR_E ok|
443a4ae997aSVladimir Oltean * (dp) -------+ (dp) -------+ (dp) -------+ (dp) --------+
444a4ae997aSVladimir Oltean * | | | | | | | |
445a4ae997aSVladimir Oltean * |exceeded | |exceeded | |exceeded | |exceeded |
446a4ae997aSVladimir Oltean * v v v v v v v v
447a4ae997aSVladimir Oltean * fail success fail success fail success fail success
448a4ae997aSVladimir Oltean * | | | | | | | |
449a4ae997aSVladimir Oltean * v v v v v v v v
450a4ae997aSVladimir Oltean * +-----+----+ +-----+----+ +-----+----+ +-----+-----+
451a4ae997aSVladimir Oltean * | | | |
452a4ae997aSVladimir Oltean * +-------> OR <-------+ +-------> OR <-------+
453a4ae997aSVladimir Oltean * | |
454a4ae997aSVladimir Oltean * v v
455a4ae997aSVladimir Oltean * +----------------> AND <-----------------+
456a4ae997aSVladimir Oltean * |
457a4ae997aSVladimir Oltean * v
458a4ae997aSVladimir Oltean * FIFO drop / accept
459a4ae997aSVladimir Oltean *
460a4ae997aSVladimir Oltean * We are modeling each of the 4 parallel lookups as a devlink-sb pool.
461a4ae997aSVladimir Oltean * At least one (ingress or egress) memory pool and one (ingress or egress)
462a4ae997aSVladimir Oltean * frame reference pool need to have resources for frame acceptance to succeed.
463a4ae997aSVladimir Oltean *
464a4ae997aSVladimir Oltean * The following watermarks are controlled explicitly through devlink-sb:
465a4ae997aSVladimir Oltean * BUF_Q_RSRV_I, BUF_Q_RSRV_E, REF_Q_RSRV_I, REF_Q_RSRV_E
466a4ae997aSVladimir Oltean * BUF_P_RSRV_I, BUF_P_RSRV_E, REF_P_RSRV_I, REF_P_RSRV_E
467a4ae997aSVladimir Oltean * The following watermarks are controlled implicitly through devlink-sb:
468a4ae997aSVladimir Oltean * BUF_COL_SHR_I, BUF_COL_SHR_E, REF_COL_SHR_I, REF_COL_SHR_E
469a4ae997aSVladimir Oltean * The following watermarks are unused and disabled:
470a4ae997aSVladimir Oltean * BUF_PRIO_SHR_I, BUF_PRIO_SHR_E, REF_PRIO_SHR_I, REF_PRIO_SHR_E
471a4ae997aSVladimir Oltean *
472a4ae997aSVladimir Oltean * This function overrides the hardware defaults with more sane ones (no
473a4ae997aSVladimir Oltean * reservations by default, let sharing use all resources) and disables the
474a4ae997aSVladimir Oltean * unused watermarks.
475a4ae997aSVladimir Oltean */
ocelot_watermark_init(struct ocelot * ocelot)476f59fd9caSVladimir Oltean static void ocelot_watermark_init(struct ocelot *ocelot)
477a4ae997aSVladimir Oltean {
478a4ae997aSVladimir Oltean int all_tcs = GENMASK(OCELOT_NUM_TC - 1, 0);
479a4ae997aSVladimir Oltean int port;
480a4ae997aSVladimir Oltean
481a4ae997aSVladimir Oltean ocelot_write(ocelot, all_tcs, QSYS_RES_QOS_MODE);
482a4ae997aSVladimir Oltean
483a4ae997aSVladimir Oltean for (port = 0; port <= ocelot->num_phys_ports; port++)
484a4ae997aSVladimir Oltean ocelot_disable_reservation_watermarks(ocelot, port);
485a4ae997aSVladimir Oltean
486a4ae997aSVladimir Oltean ocelot_disable_tc_sharing_watermarks(ocelot);
487a4ae997aSVladimir Oltean ocelot_setup_sharing_watermarks(ocelot);
488a4ae997aSVladimir Oltean }
489f59fd9caSVladimir Oltean
490*c6a9321bSColin Foster /* Watermark encode
491*c6a9321bSColin Foster * Bit 8: Unit; 0:1, 1:16
492*c6a9321bSColin Foster * Bit 7-0: Value to be multiplied with unit
493*c6a9321bSColin Foster */
ocelot_wm_enc(u16 value)494*c6a9321bSColin Foster u16 ocelot_wm_enc(u16 value)
495*c6a9321bSColin Foster {
496*c6a9321bSColin Foster WARN_ON(value >= 16 * BIT(8));
497*c6a9321bSColin Foster
498*c6a9321bSColin Foster if (value >= BIT(8))
499*c6a9321bSColin Foster return BIT(8) | (value / 16);
500*c6a9321bSColin Foster
501*c6a9321bSColin Foster return value;
502*c6a9321bSColin Foster }
503*c6a9321bSColin Foster EXPORT_SYMBOL(ocelot_wm_enc);
504*c6a9321bSColin Foster
ocelot_wm_dec(u16 wm)505*c6a9321bSColin Foster u16 ocelot_wm_dec(u16 wm)
506*c6a9321bSColin Foster {
507*c6a9321bSColin Foster if (wm & BIT(8))
508*c6a9321bSColin Foster return (wm & GENMASK(7, 0)) * 16;
509*c6a9321bSColin Foster
510*c6a9321bSColin Foster return wm;
511*c6a9321bSColin Foster }
512*c6a9321bSColin Foster EXPORT_SYMBOL(ocelot_wm_dec);
513*c6a9321bSColin Foster
ocelot_wm_stat(u32 val,u32 * inuse,u32 * maxuse)514*c6a9321bSColin Foster void ocelot_wm_stat(u32 val, u32 *inuse, u32 *maxuse)
515*c6a9321bSColin Foster {
516*c6a9321bSColin Foster *inuse = (val & GENMASK(23, 12)) >> 12;
517*c6a9321bSColin Foster *maxuse = val & GENMASK(11, 0);
518*c6a9321bSColin Foster }
519*c6a9321bSColin Foster EXPORT_SYMBOL(ocelot_wm_stat);
520*c6a9321bSColin Foster
521f59fd9caSVladimir Oltean /* Pool size and type are fixed up at runtime. Keeping this structure to
522f59fd9caSVladimir Oltean * look up the cell size multipliers.
523f59fd9caSVladimir Oltean */
524f59fd9caSVladimir Oltean static const struct devlink_sb_pool_info ocelot_sb_pool[] = {
525f59fd9caSVladimir Oltean [OCELOT_SB_BUF] = {
526f59fd9caSVladimir Oltean .cell_size = OCELOT_BUFFER_CELL_SZ,
527f59fd9caSVladimir Oltean .threshold_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC,
528f59fd9caSVladimir Oltean },
529f59fd9caSVladimir Oltean [OCELOT_SB_REF] = {
530f59fd9caSVladimir Oltean .cell_size = 1,
531f59fd9caSVladimir Oltean .threshold_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC,
532f59fd9caSVladimir Oltean },
533f59fd9caSVladimir Oltean };
534f59fd9caSVladimir Oltean
535f59fd9caSVladimir Oltean /* Returns the pool size configured through ocelot_sb_pool_set */
ocelot_sb_pool_get(struct ocelot * ocelot,unsigned int sb_index,u16 pool_index,struct devlink_sb_pool_info * pool_info)536f59fd9caSVladimir Oltean int ocelot_sb_pool_get(struct ocelot *ocelot, unsigned int sb_index,
537f59fd9caSVladimir Oltean u16 pool_index,
538f59fd9caSVladimir Oltean struct devlink_sb_pool_info *pool_info)
539f59fd9caSVladimir Oltean {
540f59fd9caSVladimir Oltean if (sb_index >= OCELOT_SB_NUM)
541f59fd9caSVladimir Oltean return -ENODEV;
542f59fd9caSVladimir Oltean if (pool_index >= OCELOT_SB_POOL_NUM)
543f59fd9caSVladimir Oltean return -ENODEV;
544f59fd9caSVladimir Oltean
545f59fd9caSVladimir Oltean *pool_info = ocelot_sb_pool[sb_index];
546f59fd9caSVladimir Oltean pool_info->size = ocelot->pool_size[sb_index][pool_index];
547f59fd9caSVladimir Oltean if (pool_index)
548f59fd9caSVladimir Oltean pool_info->pool_type = DEVLINK_SB_POOL_TYPE_INGRESS;
549f59fd9caSVladimir Oltean else
550f59fd9caSVladimir Oltean pool_info->pool_type = DEVLINK_SB_POOL_TYPE_EGRESS;
551f59fd9caSVladimir Oltean
552f59fd9caSVladimir Oltean return 0;
553f59fd9caSVladimir Oltean }
554f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_pool_get);
555f59fd9caSVladimir Oltean
556f59fd9caSVladimir Oltean /* The pool size received here configures the total amount of resources used on
557f59fd9caSVladimir Oltean * ingress (or on egress, depending upon the pool index). The pool size, minus
558f59fd9caSVladimir Oltean * the values for the port and port-tc reservations, is written into the
559f59fd9caSVladimir Oltean * COL_SHR(dp=0) sharing watermark.
560f59fd9caSVladimir Oltean */
ocelot_sb_pool_set(struct ocelot * ocelot,unsigned int sb_index,u16 pool_index,u32 size,enum devlink_sb_threshold_type threshold_type,struct netlink_ext_ack * extack)561f59fd9caSVladimir Oltean int ocelot_sb_pool_set(struct ocelot *ocelot, unsigned int sb_index,
562f59fd9caSVladimir Oltean u16 pool_index, u32 size,
563f59fd9caSVladimir Oltean enum devlink_sb_threshold_type threshold_type,
564f59fd9caSVladimir Oltean struct netlink_ext_ack *extack)
565f59fd9caSVladimir Oltean {
566f59fd9caSVladimir Oltean u32 old_pool_size;
567f59fd9caSVladimir Oltean int err;
568f59fd9caSVladimir Oltean
569f59fd9caSVladimir Oltean if (sb_index >= OCELOT_SB_NUM) {
570f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
571f59fd9caSVladimir Oltean "Invalid sb, use 0 for buffers and 1 for frame references");
572f59fd9caSVladimir Oltean return -ENODEV;
573f59fd9caSVladimir Oltean }
574f59fd9caSVladimir Oltean if (pool_index >= OCELOT_SB_POOL_NUM) {
575f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
576f59fd9caSVladimir Oltean "Invalid pool, use 0 for ingress and 1 for egress");
577f59fd9caSVladimir Oltean return -ENODEV;
578f59fd9caSVladimir Oltean }
579f59fd9caSVladimir Oltean if (threshold_type != DEVLINK_SB_THRESHOLD_TYPE_STATIC) {
580f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack,
581f59fd9caSVladimir Oltean "Only static threshold supported");
582f59fd9caSVladimir Oltean return -EOPNOTSUPP;
583f59fd9caSVladimir Oltean }
584f59fd9caSVladimir Oltean
585f59fd9caSVladimir Oltean old_pool_size = ocelot->pool_size[sb_index][pool_index];
586f59fd9caSVladimir Oltean ocelot->pool_size[sb_index][pool_index] = size;
587f59fd9caSVladimir Oltean
588f59fd9caSVladimir Oltean err = ocelot_watermark_validate(ocelot, extack);
589f59fd9caSVladimir Oltean if (err) {
590f59fd9caSVladimir Oltean ocelot->pool_size[sb_index][pool_index] = old_pool_size;
591f59fd9caSVladimir Oltean return err;
592f59fd9caSVladimir Oltean }
593f59fd9caSVladimir Oltean
594f59fd9caSVladimir Oltean ocelot_setup_sharing_watermarks(ocelot);
595f59fd9caSVladimir Oltean
596f59fd9caSVladimir Oltean return 0;
597f59fd9caSVladimir Oltean }
598f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_pool_set);
599f59fd9caSVladimir Oltean
600f59fd9caSVladimir Oltean /* This retrieves the configuration made with ocelot_sb_port_pool_set */
ocelot_sb_port_pool_get(struct ocelot * ocelot,int port,unsigned int sb_index,u16 pool_index,u32 * p_threshold)601f59fd9caSVladimir Oltean int ocelot_sb_port_pool_get(struct ocelot *ocelot, int port,
602f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index,
603f59fd9caSVladimir Oltean u32 *p_threshold)
604f59fd9caSVladimir Oltean {
605f59fd9caSVladimir Oltean int wm_index;
606f59fd9caSVladimir Oltean
607f59fd9caSVladimir Oltean switch (sb_index) {
608f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
609f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
610f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_I(port);
611f59fd9caSVladimir Oltean else
612f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_E(port);
613f59fd9caSVladimir Oltean break;
614f59fd9caSVladimir Oltean case OCELOT_SB_REF:
615f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
616f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_I(port);
617f59fd9caSVladimir Oltean else
618f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_E(port);
619f59fd9caSVladimir Oltean break;
620f59fd9caSVladimir Oltean default:
621f59fd9caSVladimir Oltean return -ENODEV;
622f59fd9caSVladimir Oltean }
623f59fd9caSVladimir Oltean
624f59fd9caSVladimir Oltean *p_threshold = ocelot_wm_read(ocelot, wm_index);
625f59fd9caSVladimir Oltean *p_threshold *= ocelot_sb_pool[sb_index].cell_size;
626f59fd9caSVladimir Oltean
627f59fd9caSVladimir Oltean return 0;
628f59fd9caSVladimir Oltean }
629f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_port_pool_get);
630f59fd9caSVladimir Oltean
631f59fd9caSVladimir Oltean /* This configures the P_RSRV per-port reserved resource watermark */
ocelot_sb_port_pool_set(struct ocelot * ocelot,int port,unsigned int sb_index,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)632f59fd9caSVladimir Oltean int ocelot_sb_port_pool_set(struct ocelot *ocelot, int port,
633f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index,
634f59fd9caSVladimir Oltean u32 threshold, struct netlink_ext_ack *extack)
635f59fd9caSVladimir Oltean {
636f59fd9caSVladimir Oltean int wm_index, err;
637f59fd9caSVladimir Oltean u32 old_thr;
638f59fd9caSVladimir Oltean
639f59fd9caSVladimir Oltean switch (sb_index) {
640f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
641f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
642f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_I(port);
643f59fd9caSVladimir Oltean else
644f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_E(port);
645f59fd9caSVladimir Oltean break;
646f59fd9caSVladimir Oltean case OCELOT_SB_REF:
647f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
648f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_I(port);
649f59fd9caSVladimir Oltean else
650f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_E(port);
651f59fd9caSVladimir Oltean break;
652f59fd9caSVladimir Oltean default:
653f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Invalid shared buffer");
654f59fd9caSVladimir Oltean return -ENODEV;
655f59fd9caSVladimir Oltean }
656f59fd9caSVladimir Oltean
657f59fd9caSVladimir Oltean threshold /= ocelot_sb_pool[sb_index].cell_size;
658f59fd9caSVladimir Oltean
659f59fd9caSVladimir Oltean old_thr = ocelot_wm_read(ocelot, wm_index);
660f59fd9caSVladimir Oltean ocelot_wm_write(ocelot, wm_index, threshold);
661f59fd9caSVladimir Oltean
662f59fd9caSVladimir Oltean err = ocelot_watermark_validate(ocelot, extack);
663f59fd9caSVladimir Oltean if (err) {
664f59fd9caSVladimir Oltean ocelot_wm_write(ocelot, wm_index, old_thr);
665f59fd9caSVladimir Oltean return err;
666f59fd9caSVladimir Oltean }
667f59fd9caSVladimir Oltean
668f59fd9caSVladimir Oltean ocelot_setup_sharing_watermarks(ocelot);
669f59fd9caSVladimir Oltean
670f59fd9caSVladimir Oltean return 0;
671f59fd9caSVladimir Oltean }
672f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_port_pool_set);
673f59fd9caSVladimir Oltean
674f59fd9caSVladimir Oltean /* This retrieves the configuration done by ocelot_sb_tc_pool_bind_set */
ocelot_sb_tc_pool_bind_get(struct ocelot * ocelot,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 * p_pool_index,u32 * p_threshold)675f59fd9caSVladimir Oltean int ocelot_sb_tc_pool_bind_get(struct ocelot *ocelot, int port,
676f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index,
677f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type,
678f59fd9caSVladimir Oltean u16 *p_pool_index, u32 *p_threshold)
679f59fd9caSVladimir Oltean {
680f59fd9caSVladimir Oltean int wm_index;
681f59fd9caSVladimir Oltean
682f59fd9caSVladimir Oltean switch (sb_index) {
683f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
684f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
685f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_I(port, tc_index);
686f59fd9caSVladimir Oltean else
687f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_E(port, tc_index);
688f59fd9caSVladimir Oltean break;
689f59fd9caSVladimir Oltean case OCELOT_SB_REF:
690f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
691f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_I(port, tc_index);
692f59fd9caSVladimir Oltean else
693f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_E(port, tc_index);
694f59fd9caSVladimir Oltean break;
695f59fd9caSVladimir Oltean default:
696f59fd9caSVladimir Oltean return -ENODEV;
697f59fd9caSVladimir Oltean }
698f59fd9caSVladimir Oltean
699f59fd9caSVladimir Oltean *p_threshold = ocelot_wm_read(ocelot, wm_index);
700f59fd9caSVladimir Oltean *p_threshold *= ocelot_sb_pool[sb_index].cell_size;
701f59fd9caSVladimir Oltean
702f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
703f59fd9caSVladimir Oltean *p_pool_index = 0;
704f59fd9caSVladimir Oltean else
705f59fd9caSVladimir Oltean *p_pool_index = 1;
706f59fd9caSVladimir Oltean
707f59fd9caSVladimir Oltean return 0;
708f59fd9caSVladimir Oltean }
709f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_tc_pool_bind_get);
710f59fd9caSVladimir Oltean
711f59fd9caSVladimir Oltean /* This configures the Q_RSRV per-port-tc reserved resource watermark */
ocelot_sb_tc_pool_bind_set(struct ocelot * ocelot,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)712f59fd9caSVladimir Oltean int ocelot_sb_tc_pool_bind_set(struct ocelot *ocelot, int port,
713f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index,
714f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type,
715f59fd9caSVladimir Oltean u16 pool_index, u32 threshold,
716f59fd9caSVladimir Oltean struct netlink_ext_ack *extack)
717f59fd9caSVladimir Oltean {
718f59fd9caSVladimir Oltean int wm_index, err;
719f59fd9caSVladimir Oltean u32 old_thr;
720f59fd9caSVladimir Oltean
721f59fd9caSVladimir Oltean /* Paranoid check? */
722f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING &&
723f59fd9caSVladimir Oltean pool_type != DEVLINK_SB_POOL_TYPE_INGRESS)
724f59fd9caSVladimir Oltean return -EINVAL;
725f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_EGR &&
726f59fd9caSVladimir Oltean pool_type != DEVLINK_SB_POOL_TYPE_EGRESS)
727f59fd9caSVladimir Oltean return -EINVAL;
728f59fd9caSVladimir Oltean
729f59fd9caSVladimir Oltean switch (sb_index) {
730f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
731f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
732f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_I(port, tc_index);
733f59fd9caSVladimir Oltean else
734f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_E(port, tc_index);
735f59fd9caSVladimir Oltean break;
736f59fd9caSVladimir Oltean case OCELOT_SB_REF:
737f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
738f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_I(port, tc_index);
739f59fd9caSVladimir Oltean else
740f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_E(port, tc_index);
741f59fd9caSVladimir Oltean break;
742f59fd9caSVladimir Oltean default:
743f59fd9caSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Invalid shared buffer");
744f59fd9caSVladimir Oltean return -ENODEV;
745f59fd9caSVladimir Oltean }
746f59fd9caSVladimir Oltean
747f59fd9caSVladimir Oltean threshold /= ocelot_sb_pool[sb_index].cell_size;
748f59fd9caSVladimir Oltean
749f59fd9caSVladimir Oltean old_thr = ocelot_wm_read(ocelot, wm_index);
750f59fd9caSVladimir Oltean ocelot_wm_write(ocelot, wm_index, threshold);
751f59fd9caSVladimir Oltean err = ocelot_watermark_validate(ocelot, extack);
752f59fd9caSVladimir Oltean if (err) {
753f59fd9caSVladimir Oltean ocelot_wm_write(ocelot, wm_index, old_thr);
754f59fd9caSVladimir Oltean return err;
755f59fd9caSVladimir Oltean }
756f59fd9caSVladimir Oltean
757f59fd9caSVladimir Oltean ocelot_setup_sharing_watermarks(ocelot);
758f59fd9caSVladimir Oltean
759f59fd9caSVladimir Oltean return 0;
760f59fd9caSVladimir Oltean }
761f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_tc_pool_bind_set);
762f59fd9caSVladimir Oltean
763f59fd9caSVladimir Oltean /* The hardware does not support atomic snapshots, we'll read out the
764f59fd9caSVladimir Oltean * occupancy registers individually and have this as just a stub.
765f59fd9caSVladimir Oltean */
ocelot_sb_occ_snapshot(struct ocelot * ocelot,unsigned int sb_index)766f59fd9caSVladimir Oltean int ocelot_sb_occ_snapshot(struct ocelot *ocelot, unsigned int sb_index)
767f59fd9caSVladimir Oltean {
768f59fd9caSVladimir Oltean return 0;
769f59fd9caSVladimir Oltean }
770f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_occ_snapshot);
771f59fd9caSVladimir Oltean
772f59fd9caSVladimir Oltean /* The watermark occupancy registers are cleared upon read,
773f59fd9caSVladimir Oltean * so let's read them.
774f59fd9caSVladimir Oltean */
ocelot_sb_occ_max_clear(struct ocelot * ocelot,unsigned int sb_index)775f59fd9caSVladimir Oltean int ocelot_sb_occ_max_clear(struct ocelot *ocelot, unsigned int sb_index)
776f59fd9caSVladimir Oltean {
777f59fd9caSVladimir Oltean u32 inuse, maxuse;
778f59fd9caSVladimir Oltean int port, prio;
779f59fd9caSVladimir Oltean
780f59fd9caSVladimir Oltean switch (sb_index) {
781f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
782f59fd9caSVladimir Oltean for (port = 0; port <= ocelot->num_phys_ports; port++) {
783f59fd9caSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
784f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, BUF_Q_RSRV_I(port, prio),
785f59fd9caSVladimir Oltean &inuse, &maxuse);
786f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, BUF_Q_RSRV_E(port, prio),
787f59fd9caSVladimir Oltean &inuse, &maxuse);
788f59fd9caSVladimir Oltean }
789f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, BUF_P_RSRV_I(port),
790f59fd9caSVladimir Oltean &inuse, &maxuse);
791f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, BUF_P_RSRV_E(port),
792f59fd9caSVladimir Oltean &inuse, &maxuse);
793f59fd9caSVladimir Oltean }
794f59fd9caSVladimir Oltean break;
795f59fd9caSVladimir Oltean case OCELOT_SB_REF:
796f59fd9caSVladimir Oltean for (port = 0; port <= ocelot->num_phys_ports; port++) {
797f59fd9caSVladimir Oltean for (prio = 0; prio < OCELOT_NUM_TC; prio++) {
798f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, REF_Q_RSRV_I(port, prio),
799f59fd9caSVladimir Oltean &inuse, &maxuse);
800f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, REF_Q_RSRV_E(port, prio),
801f59fd9caSVladimir Oltean &inuse, &maxuse);
802f59fd9caSVladimir Oltean }
803f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, REF_P_RSRV_I(port),
804f59fd9caSVladimir Oltean &inuse, &maxuse);
805f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, REF_P_RSRV_E(port),
806f59fd9caSVladimir Oltean &inuse, &maxuse);
807f59fd9caSVladimir Oltean }
808f59fd9caSVladimir Oltean break;
809f59fd9caSVladimir Oltean default:
810f59fd9caSVladimir Oltean return -ENODEV;
811f59fd9caSVladimir Oltean }
812f59fd9caSVladimir Oltean
813f59fd9caSVladimir Oltean return 0;
814f59fd9caSVladimir Oltean }
815f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_occ_max_clear);
816f59fd9caSVladimir Oltean
817f59fd9caSVladimir Oltean /* This retrieves the watermark occupancy for per-port P_RSRV watermarks */
ocelot_sb_occ_port_pool_get(struct ocelot * ocelot,int port,unsigned int sb_index,u16 pool_index,u32 * p_cur,u32 * p_max)818f59fd9caSVladimir Oltean int ocelot_sb_occ_port_pool_get(struct ocelot *ocelot, int port,
819f59fd9caSVladimir Oltean unsigned int sb_index, u16 pool_index,
820f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max)
821f59fd9caSVladimir Oltean {
822f59fd9caSVladimir Oltean int wm_index;
823f59fd9caSVladimir Oltean
824f59fd9caSVladimir Oltean switch (sb_index) {
825f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
826f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
827f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_I(port);
828f59fd9caSVladimir Oltean else
829f59fd9caSVladimir Oltean wm_index = BUF_P_RSRV_E(port);
830f59fd9caSVladimir Oltean break;
831f59fd9caSVladimir Oltean case OCELOT_SB_REF:
832f59fd9caSVladimir Oltean if (pool_index == OCELOT_SB_POOL_ING)
833f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_I(port);
834f59fd9caSVladimir Oltean else
835f59fd9caSVladimir Oltean wm_index = REF_P_RSRV_E(port);
836f59fd9caSVladimir Oltean break;
837f59fd9caSVladimir Oltean default:
838f59fd9caSVladimir Oltean return -ENODEV;
839f59fd9caSVladimir Oltean }
840f59fd9caSVladimir Oltean
841f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, wm_index, p_cur, p_max);
842f59fd9caSVladimir Oltean *p_cur *= ocelot_sb_pool[sb_index].cell_size;
843f59fd9caSVladimir Oltean *p_max *= ocelot_sb_pool[sb_index].cell_size;
844f59fd9caSVladimir Oltean
845f59fd9caSVladimir Oltean return 0;
846f59fd9caSVladimir Oltean }
847f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_occ_port_pool_get);
848f59fd9caSVladimir Oltean
849f59fd9caSVladimir Oltean /* This retrieves the watermark occupancy for per-port-tc Q_RSRV watermarks */
ocelot_sb_occ_tc_port_bind_get(struct ocelot * ocelot,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u32 * p_cur,u32 * p_max)850f59fd9caSVladimir Oltean int ocelot_sb_occ_tc_port_bind_get(struct ocelot *ocelot, int port,
851f59fd9caSVladimir Oltean unsigned int sb_index, u16 tc_index,
852f59fd9caSVladimir Oltean enum devlink_sb_pool_type pool_type,
853f59fd9caSVladimir Oltean u32 *p_cur, u32 *p_max)
854f59fd9caSVladimir Oltean {
855f59fd9caSVladimir Oltean int wm_index;
856f59fd9caSVladimir Oltean
857f59fd9caSVladimir Oltean switch (sb_index) {
858f59fd9caSVladimir Oltean case OCELOT_SB_BUF:
859f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
860f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_I(port, tc_index);
861f59fd9caSVladimir Oltean else
862f59fd9caSVladimir Oltean wm_index = BUF_Q_RSRV_E(port, tc_index);
863f59fd9caSVladimir Oltean break;
864f59fd9caSVladimir Oltean case OCELOT_SB_REF:
865f59fd9caSVladimir Oltean if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
866f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_I(port, tc_index);
867f59fd9caSVladimir Oltean else
868f59fd9caSVladimir Oltean wm_index = REF_Q_RSRV_E(port, tc_index);
869f59fd9caSVladimir Oltean break;
870f59fd9caSVladimir Oltean default:
871f59fd9caSVladimir Oltean return -ENODEV;
872f59fd9caSVladimir Oltean }
873f59fd9caSVladimir Oltean
874f59fd9caSVladimir Oltean ocelot_wm_status(ocelot, wm_index, p_cur, p_max);
875f59fd9caSVladimir Oltean *p_cur *= ocelot_sb_pool[sb_index].cell_size;
876f59fd9caSVladimir Oltean *p_max *= ocelot_sb_pool[sb_index].cell_size;
877f59fd9caSVladimir Oltean
878f59fd9caSVladimir Oltean return 0;
879f59fd9caSVladimir Oltean }
880f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_sb_occ_tc_port_bind_get);
881f59fd9caSVladimir Oltean
ocelot_devlink_sb_register(struct ocelot * ocelot)882f59fd9caSVladimir Oltean int ocelot_devlink_sb_register(struct ocelot *ocelot)
883f59fd9caSVladimir Oltean {
884f59fd9caSVladimir Oltean int err;
885f59fd9caSVladimir Oltean
886f59fd9caSVladimir Oltean err = devlink_sb_register(ocelot->devlink, OCELOT_SB_BUF,
887f59fd9caSVladimir Oltean ocelot->packet_buffer_size, 1, 1,
888f59fd9caSVladimir Oltean OCELOT_NUM_TC, OCELOT_NUM_TC);
889f59fd9caSVladimir Oltean if (err)
890f59fd9caSVladimir Oltean return err;
891f59fd9caSVladimir Oltean
892f59fd9caSVladimir Oltean err = devlink_sb_register(ocelot->devlink, OCELOT_SB_REF,
893f59fd9caSVladimir Oltean ocelot->num_frame_refs, 1, 1,
894f59fd9caSVladimir Oltean OCELOT_NUM_TC, OCELOT_NUM_TC);
895f59fd9caSVladimir Oltean if (err) {
896f59fd9caSVladimir Oltean devlink_sb_unregister(ocelot->devlink, OCELOT_SB_BUF);
897f59fd9caSVladimir Oltean return err;
898f59fd9caSVladimir Oltean }
899f59fd9caSVladimir Oltean
900f59fd9caSVladimir Oltean ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_ING] = ocelot->packet_buffer_size;
901f59fd9caSVladimir Oltean ocelot->pool_size[OCELOT_SB_BUF][OCELOT_SB_POOL_EGR] = ocelot->packet_buffer_size;
902f59fd9caSVladimir Oltean ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_ING] = ocelot->num_frame_refs;
903f59fd9caSVladimir Oltean ocelot->pool_size[OCELOT_SB_REF][OCELOT_SB_POOL_EGR] = ocelot->num_frame_refs;
904f59fd9caSVladimir Oltean
905f59fd9caSVladimir Oltean ocelot_watermark_init(ocelot);
906f59fd9caSVladimir Oltean
907f59fd9caSVladimir Oltean return 0;
908f59fd9caSVladimir Oltean }
909f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_devlink_sb_register);
910f59fd9caSVladimir Oltean
ocelot_devlink_sb_unregister(struct ocelot * ocelot)911f59fd9caSVladimir Oltean void ocelot_devlink_sb_unregister(struct ocelot *ocelot)
912f59fd9caSVladimir Oltean {
913f59fd9caSVladimir Oltean devlink_sb_unregister(ocelot->devlink, OCELOT_SB_BUF);
914f59fd9caSVladimir Oltean devlink_sb_unregister(ocelot->devlink, OCELOT_SB_REF);
915f59fd9caSVladimir Oltean }
916f59fd9caSVladimir Oltean EXPORT_SYMBOL(ocelot_devlink_sb_unregister);
917