1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/dcbnl.h>
7 #include <linux/if_ether.h>
8 #include <linux/list.h>
9 #include <linux/netlink.h>
16 struct mlxsw_sp_sb_pr
{
17 enum mlxsw_reg_sbpr_mode mode
;
23 struct mlxsw_cp_sb_occ
{
28 struct mlxsw_sp_sb_cm
{
32 struct mlxsw_cp_sb_occ occ
;
37 #define MLXSW_SP_SB_INFI -1U
38 #define MLXSW_SP_SB_REST -2U
40 struct mlxsw_sp_sb_pm
{
43 struct mlxsw_cp_sb_occ occ
;
46 struct mlxsw_sp_sb_mm
{
52 struct mlxsw_sp_sb_pool_des
{
53 enum mlxsw_reg_sbxx_dir dir
;
57 #define MLXSW_SP_SB_POOL_ING 0
58 #define MLXSW_SP_SB_POOL_EGR 4
59 #define MLXSW_SP_SB_POOL_EGR_MC 8
60 #define MLXSW_SP_SB_POOL_ING_CPU 9
61 #define MLXSW_SP_SB_POOL_EGR_CPU 10
63 static const struct mlxsw_sp_sb_pool_des mlxsw_sp1_sb_pool_dess
[] = {
64 {MLXSW_REG_SBXX_DIR_INGRESS
, 0},
65 {MLXSW_REG_SBXX_DIR_INGRESS
, 1},
66 {MLXSW_REG_SBXX_DIR_INGRESS
, 2},
67 {MLXSW_REG_SBXX_DIR_INGRESS
, 3},
68 {MLXSW_REG_SBXX_DIR_EGRESS
, 0},
69 {MLXSW_REG_SBXX_DIR_EGRESS
, 1},
70 {MLXSW_REG_SBXX_DIR_EGRESS
, 2},
71 {MLXSW_REG_SBXX_DIR_EGRESS
, 3},
72 {MLXSW_REG_SBXX_DIR_EGRESS
, 15},
73 {MLXSW_REG_SBXX_DIR_INGRESS
, 4},
74 {MLXSW_REG_SBXX_DIR_EGRESS
, 4},
77 static const struct mlxsw_sp_sb_pool_des mlxsw_sp2_sb_pool_dess
[] = {
78 {MLXSW_REG_SBXX_DIR_INGRESS
, 0},
79 {MLXSW_REG_SBXX_DIR_INGRESS
, 1},
80 {MLXSW_REG_SBXX_DIR_INGRESS
, 2},
81 {MLXSW_REG_SBXX_DIR_INGRESS
, 3},
82 {MLXSW_REG_SBXX_DIR_EGRESS
, 0},
83 {MLXSW_REG_SBXX_DIR_EGRESS
, 1},
84 {MLXSW_REG_SBXX_DIR_EGRESS
, 2},
85 {MLXSW_REG_SBXX_DIR_EGRESS
, 3},
86 {MLXSW_REG_SBXX_DIR_EGRESS
, 15},
87 {MLXSW_REG_SBXX_DIR_INGRESS
, 4},
88 {MLXSW_REG_SBXX_DIR_EGRESS
, 4},
91 #define MLXSW_SP_SB_ING_TC_COUNT 8
92 #define MLXSW_SP_SB_EG_TC_COUNT 16
94 struct mlxsw_sp_sb_port
{
95 struct mlxsw_sp_sb_cm ing_cms
[MLXSW_SP_SB_ING_TC_COUNT
];
96 struct mlxsw_sp_sb_cm eg_cms
[MLXSW_SP_SB_EG_TC_COUNT
];
97 struct mlxsw_sp_sb_pm
*pms
;
101 struct mlxsw_sp_sb_pr
*prs
;
102 struct mlxsw_sp_sb_port
*ports
;
104 u32 max_headroom_cells
;
108 struct mlxsw_sp_sb_vals
{
109 unsigned int pool_count
;
110 const struct mlxsw_sp_sb_pool_des
*pool_dess
;
111 const struct mlxsw_sp_sb_pm
*pms
;
112 const struct mlxsw_sp_sb_pm
*pms_cpu
;
113 const struct mlxsw_sp_sb_pr
*prs
;
114 const struct mlxsw_sp_sb_mm
*mms
;
115 const struct mlxsw_sp_sb_cm
*cms_ingress
;
116 const struct mlxsw_sp_sb_cm
*cms_egress
;
117 const struct mlxsw_sp_sb_cm
*cms_cpu
;
118 unsigned int mms_count
;
119 unsigned int cms_ingress_count
;
120 unsigned int cms_egress_count
;
121 unsigned int cms_cpu_count
;
124 struct mlxsw_sp_sb_ops
{
125 u32 (*int_buf_size_get
)(int mtu
, u32 speed
);
128 u32
mlxsw_sp_cells_bytes(const struct mlxsw_sp
*mlxsw_sp
, u32 cells
)
130 return mlxsw_sp
->sb
->cell_size
* cells
;
133 u32
mlxsw_sp_bytes_cells(const struct mlxsw_sp
*mlxsw_sp
, u32 bytes
)
135 return DIV_ROUND_UP(bytes
, mlxsw_sp
->sb
->cell_size
);
138 static u32
mlxsw_sp_port_headroom_8x_adjust(const struct mlxsw_sp_port
*mlxsw_sp_port
,
141 /* Ports with eight lanes use two headroom buffers between which the
142 * configured headroom size is split. Therefore, multiply the calculated
143 * headroom size by two.
145 return mlxsw_sp_port
->mapping
.width
== 8 ? 2 * size_cells
: size_cells
;
148 static struct mlxsw_sp_sb_pr
*mlxsw_sp_sb_pr_get(struct mlxsw_sp
*mlxsw_sp
,
151 return &mlxsw_sp
->sb
->prs
[pool_index
];
154 static bool mlxsw_sp_sb_cm_exists(u8 pg_buff
, enum mlxsw_reg_sbxx_dir dir
)
156 if (dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
157 return pg_buff
< MLXSW_SP_SB_ING_TC_COUNT
;
159 return pg_buff
< MLXSW_SP_SB_EG_TC_COUNT
;
162 static struct mlxsw_sp_sb_cm
*mlxsw_sp_sb_cm_get(struct mlxsw_sp
*mlxsw_sp
,
163 u8 local_port
, u8 pg_buff
,
164 enum mlxsw_reg_sbxx_dir dir
)
166 struct mlxsw_sp_sb_port
*sb_port
= &mlxsw_sp
->sb
->ports
[local_port
];
168 WARN_ON(!mlxsw_sp_sb_cm_exists(pg_buff
, dir
));
169 if (dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
170 return &sb_port
->ing_cms
[pg_buff
];
172 return &sb_port
->eg_cms
[pg_buff
];
175 static struct mlxsw_sp_sb_pm
*mlxsw_sp_sb_pm_get(struct mlxsw_sp
*mlxsw_sp
,
176 u8 local_port
, u16 pool_index
)
178 return &mlxsw_sp
->sb
->ports
[local_port
].pms
[pool_index
];
181 static int mlxsw_sp_sb_pr_write(struct mlxsw_sp
*mlxsw_sp
, u16 pool_index
,
182 enum mlxsw_reg_sbpr_mode mode
,
183 u32 size
, bool infi_size
)
185 const struct mlxsw_sp_sb_pool_des
*des
=
186 &mlxsw_sp
->sb_vals
->pool_dess
[pool_index
];
187 char sbpr_pl
[MLXSW_REG_SBPR_LEN
];
188 struct mlxsw_sp_sb_pr
*pr
;
191 mlxsw_reg_sbpr_pack(sbpr_pl
, des
->pool
, des
->dir
, mode
,
193 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbpr
), sbpr_pl
);
198 size
= mlxsw_sp_bytes_cells(mlxsw_sp
, mlxsw_sp
->sb
->sb_size
);
199 pr
= mlxsw_sp_sb_pr_get(mlxsw_sp
, pool_index
);
205 static int mlxsw_sp_sb_cm_write(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
206 u8 pg_buff
, u32 min_buff
, u32 max_buff
,
207 bool infi_max
, u16 pool_index
)
209 const struct mlxsw_sp_sb_pool_des
*des
=
210 &mlxsw_sp
->sb_vals
->pool_dess
[pool_index
];
211 char sbcm_pl
[MLXSW_REG_SBCM_LEN
];
212 struct mlxsw_sp_sb_cm
*cm
;
215 mlxsw_reg_sbcm_pack(sbcm_pl
, local_port
, pg_buff
, des
->dir
,
216 min_buff
, max_buff
, infi_max
, des
->pool
);
217 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbcm
), sbcm_pl
);
221 if (mlxsw_sp_sb_cm_exists(pg_buff
, des
->dir
)) {
223 max_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
,
224 mlxsw_sp
->sb
->sb_size
);
226 cm
= mlxsw_sp_sb_cm_get(mlxsw_sp
, local_port
, pg_buff
,
228 cm
->min_buff
= min_buff
;
229 cm
->max_buff
= max_buff
;
230 cm
->pool_index
= pool_index
;
235 static int mlxsw_sp_sb_pm_write(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
236 u16 pool_index
, u32 min_buff
, u32 max_buff
)
238 const struct mlxsw_sp_sb_pool_des
*des
=
239 &mlxsw_sp
->sb_vals
->pool_dess
[pool_index
];
240 char sbpm_pl
[MLXSW_REG_SBPM_LEN
];
241 struct mlxsw_sp_sb_pm
*pm
;
244 mlxsw_reg_sbpm_pack(sbpm_pl
, local_port
, des
->pool
, des
->dir
, false,
246 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbpm
), sbpm_pl
);
250 pm
= mlxsw_sp_sb_pm_get(mlxsw_sp
, local_port
, pool_index
);
251 pm
->min_buff
= min_buff
;
252 pm
->max_buff
= max_buff
;
256 static int mlxsw_sp_sb_pm_occ_clear(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
257 u16 pool_index
, struct list_head
*bulk_list
)
259 const struct mlxsw_sp_sb_pool_des
*des
=
260 &mlxsw_sp
->sb_vals
->pool_dess
[pool_index
];
261 char sbpm_pl
[MLXSW_REG_SBPM_LEN
];
263 if (local_port
== MLXSW_PORT_CPU_PORT
&&
264 des
->dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
267 mlxsw_reg_sbpm_pack(sbpm_pl
, local_port
, des
->pool
, des
->dir
,
269 return mlxsw_reg_trans_query(mlxsw_sp
->core
, MLXSW_REG(sbpm
), sbpm_pl
,
273 static void mlxsw_sp_sb_pm_occ_query_cb(struct mlxsw_core
*mlxsw_core
,
274 char *sbpm_pl
, size_t sbpm_pl_len
,
275 unsigned long cb_priv
)
277 struct mlxsw_sp_sb_pm
*pm
= (struct mlxsw_sp_sb_pm
*) cb_priv
;
279 mlxsw_reg_sbpm_unpack(sbpm_pl
, &pm
->occ
.cur
, &pm
->occ
.max
);
282 static int mlxsw_sp_sb_pm_occ_query(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
283 u16 pool_index
, struct list_head
*bulk_list
)
285 const struct mlxsw_sp_sb_pool_des
*des
=
286 &mlxsw_sp
->sb_vals
->pool_dess
[pool_index
];
287 char sbpm_pl
[MLXSW_REG_SBPM_LEN
];
288 struct mlxsw_sp_sb_pm
*pm
;
290 if (local_port
== MLXSW_PORT_CPU_PORT
&&
291 des
->dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
294 pm
= mlxsw_sp_sb_pm_get(mlxsw_sp
, local_port
, pool_index
);
295 mlxsw_reg_sbpm_pack(sbpm_pl
, local_port
, des
->pool
, des
->dir
,
297 return mlxsw_reg_trans_query(mlxsw_sp
->core
, MLXSW_REG(sbpm
), sbpm_pl
,
299 mlxsw_sp_sb_pm_occ_query_cb
,
303 void mlxsw_sp_hdroom_prios_reset_buf_idx(struct mlxsw_sp_hdroom
*hdroom
)
307 for (prio
= 0; prio
< IEEE_8021QAZ_MAX_TCS
; prio
++) {
308 switch (hdroom
->mode
) {
309 case MLXSW_SP_HDROOM_MODE_DCB
:
310 hdroom
->prios
.prio
[prio
].buf_idx
= hdroom
->prios
.prio
[prio
].ets_buf_idx
;
312 case MLXSW_SP_HDROOM_MODE_TC
:
313 hdroom
->prios
.prio
[prio
].buf_idx
= hdroom
->prios
.prio
[prio
].set_buf_idx
;
319 void mlxsw_sp_hdroom_bufs_reset_lossiness(struct mlxsw_sp_hdroom
*hdroom
)
324 for (i
= 0; i
< DCBX_MAX_BUFFERS
; i
++)
325 hdroom
->bufs
.buf
[i
].lossy
= true;
327 for (prio
= 0; prio
< IEEE_8021Q_MAX_PRIORITIES
; prio
++) {
328 if (!hdroom
->prios
.prio
[prio
].lossy
)
329 hdroom
->bufs
.buf
[hdroom
->prios
.prio
[prio
].buf_idx
].lossy
= false;
333 static u16
mlxsw_sp_hdroom_buf_threshold_get(const struct mlxsw_sp
*mlxsw_sp
, int mtu
)
335 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp
, mtu
);
338 static void mlxsw_sp_hdroom_buf_pack(char *pbmc_pl
, int index
, u16 size
, u16 thres
, bool lossy
)
341 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl
, index
, size
);
343 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl
, index
, size
,
347 static u16
mlxsw_sp_hdroom_buf_delay_get(const struct mlxsw_sp
*mlxsw_sp
,
348 const struct mlxsw_sp_hdroom
*hdroom
)
352 delay_cells
= mlxsw_sp_bytes_cells(mlxsw_sp
, hdroom
->delay_bytes
);
354 /* In the worst case scenario the delay will be made up of packets that
355 * are all of size CELL_SIZE + 1, which means each packet will require
356 * almost twice its true size when buffered in the switch. We therefore
357 * multiply this value by the "cell factor", which is close to 2.
359 * Another MTU is added in case the transmitting host already started
360 * transmitting a maximum length frame when the PFC packet was received.
362 return 2 * delay_cells
+ mlxsw_sp_bytes_cells(mlxsw_sp
, hdroom
->mtu
);
365 static u32
mlxsw_sp_hdroom_int_buf_size_get(struct mlxsw_sp
*mlxsw_sp
, int mtu
, u32 speed
)
367 u32 buffsize
= mlxsw_sp
->sb_ops
->int_buf_size_get(speed
, mtu
);
369 return mlxsw_sp_bytes_cells(mlxsw_sp
, buffsize
) + 1;
372 static bool mlxsw_sp_hdroom_buf_is_used(const struct mlxsw_sp_hdroom
*hdroom
, int buf
)
376 for (prio
= 0; prio
< IEEE_8021QAZ_MAX_TCS
; prio
++) {
377 if (hdroom
->prios
.prio
[prio
].buf_idx
== buf
)
383 void mlxsw_sp_hdroom_bufs_reset_sizes(struct mlxsw_sp_port
*mlxsw_sp_port
,
384 struct mlxsw_sp_hdroom
*hdroom
)
386 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
390 /* Internal buffer. */
391 reserve_cells
= mlxsw_sp_hdroom_int_buf_size_get(mlxsw_sp
, mlxsw_sp_port
->max_speed
,
392 mlxsw_sp_port
->max_mtu
);
393 reserve_cells
= mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port
, reserve_cells
);
394 hdroom
->int_buf
.reserve_cells
= reserve_cells
;
396 if (hdroom
->int_buf
.enable
)
397 hdroom
->int_buf
.size_cells
= reserve_cells
;
399 hdroom
->int_buf
.size_cells
= 0;
402 for (i
= 0; i
< DCBX_MAX_BUFFERS
; i
++) {
403 struct mlxsw_sp_hdroom_buf
*buf
= &hdroom
->bufs
.buf
[i
];
407 if (!mlxsw_sp_hdroom_buf_is_used(hdroom
, i
)) {
410 } else if (buf
->lossy
) {
411 thres_cells
= mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp
, hdroom
->mtu
);
414 thres_cells
= mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp
, hdroom
->mtu
);
415 delay_cells
= mlxsw_sp_hdroom_buf_delay_get(mlxsw_sp
, hdroom
);
418 thres_cells
= mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port
, thres_cells
);
419 delay_cells
= mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port
, delay_cells
);
421 buf
->thres_cells
= thres_cells
;
422 if (hdroom
->mode
== MLXSW_SP_HDROOM_MODE_DCB
) {
423 buf
->size_cells
= thres_cells
+ delay_cells
;
425 /* Do not allow going below the minimum size, even if
426 * the user requested it.
428 buf
->size_cells
= max(buf
->set_size_cells
, buf
->thres_cells
);
433 #define MLXSW_SP_PB_UNUSED 8
435 static int mlxsw_sp_hdroom_configure_buffers(struct mlxsw_sp_port
*mlxsw_sp_port
,
436 const struct mlxsw_sp_hdroom
*hdroom
, bool force
)
438 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
439 char pbmc_pl
[MLXSW_REG_PBMC_LEN
];
444 dirty
= memcmp(&mlxsw_sp_port
->hdroom
->bufs
, &hdroom
->bufs
, sizeof(hdroom
->bufs
));
445 if (!dirty
&& !force
)
448 mlxsw_reg_pbmc_pack(pbmc_pl
, mlxsw_sp_port
->local_port
, 0xffff, 0xffff / 2);
449 for (i
= 0; i
< MLXSW_SP_PB_COUNT
; i
++) {
450 const struct mlxsw_sp_hdroom_buf
*buf
= &hdroom
->bufs
.buf
[i
];
452 if (i
== MLXSW_SP_PB_UNUSED
)
455 mlxsw_sp_hdroom_buf_pack(pbmc_pl
, i
, buf
->size_cells
, buf
->thres_cells
, buf
->lossy
);
458 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl
, MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX
, 0);
459 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pbmc
), pbmc_pl
);
463 mlxsw_sp_port
->hdroom
->bufs
= hdroom
->bufs
;
467 static int mlxsw_sp_hdroom_configure_priomap(struct mlxsw_sp_port
*mlxsw_sp_port
,
468 const struct mlxsw_sp_hdroom
*hdroom
, bool force
)
470 char pptb_pl
[MLXSW_REG_PPTB_LEN
];
475 dirty
= memcmp(&mlxsw_sp_port
->hdroom
->prios
, &hdroom
->prios
, sizeof(hdroom
->prios
));
476 if (!dirty
&& !force
)
479 mlxsw_reg_pptb_pack(pptb_pl
, mlxsw_sp_port
->local_port
);
480 for (prio
= 0; prio
< IEEE_8021QAZ_MAX_TCS
; prio
++)
481 mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl
, prio
, hdroom
->prios
.prio
[prio
].buf_idx
);
483 err
= mlxsw_reg_write(mlxsw_sp_port
->mlxsw_sp
->core
, MLXSW_REG(pptb
), pptb_pl
);
487 mlxsw_sp_port
->hdroom
->prios
= hdroom
->prios
;
491 static int mlxsw_sp_hdroom_configure_int_buf(struct mlxsw_sp_port
*mlxsw_sp_port
,
492 const struct mlxsw_sp_hdroom
*hdroom
, bool force
)
494 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
498 dirty
= memcmp(&mlxsw_sp_port
->hdroom
->int_buf
, &hdroom
->int_buf
, sizeof(hdroom
->int_buf
));
499 if (!dirty
&& !force
)
502 mlxsw_reg_sbib_pack(sbib_pl
, mlxsw_sp_port
->local_port
, hdroom
->int_buf
.size_cells
);
503 err
= mlxsw_reg_write(mlxsw_sp_port
->mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
507 mlxsw_sp_port
->hdroom
->int_buf
= hdroom
->int_buf
;
511 static bool mlxsw_sp_hdroom_bufs_fit(struct mlxsw_sp
*mlxsw_sp
,
512 const struct mlxsw_sp_hdroom
*hdroom
)
514 u32 taken_headroom_cells
= 0;
517 for (i
= 0; i
< MLXSW_SP_PB_COUNT
; i
++)
518 taken_headroom_cells
+= hdroom
->bufs
.buf
[i
].size_cells
;
520 taken_headroom_cells
+= hdroom
->int_buf
.reserve_cells
;
521 return taken_headroom_cells
<= mlxsw_sp
->sb
->max_headroom_cells
;
524 static int __mlxsw_sp_hdroom_configure(struct mlxsw_sp_port
*mlxsw_sp_port
,
525 const struct mlxsw_sp_hdroom
*hdroom
, bool force
)
527 struct mlxsw_sp_hdroom orig_hdroom
;
528 struct mlxsw_sp_hdroom tmp_hdroom
;
532 /* Port buffers need to be configured in three steps. First, all buffers
533 * with non-zero size are configured. Then, prio-to-buffer map is
534 * updated, allowing traffic to flow to the now non-zero buffers.
535 * Finally, zero-sized buffers are configured, because now no traffic
536 * should be directed to them anymore. This way, in a non-congested
537 * system, no packet drops are introduced by the reconfiguration.
540 orig_hdroom
= *mlxsw_sp_port
->hdroom
;
541 tmp_hdroom
= orig_hdroom
;
542 for (i
= 0; i
< MLXSW_SP_PB_COUNT
; i
++) {
543 if (hdroom
->bufs
.buf
[i
].size_cells
)
544 tmp_hdroom
.bufs
.buf
[i
] = hdroom
->bufs
.buf
[i
];
547 if (!mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port
->mlxsw_sp
, &tmp_hdroom
) ||
548 !mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port
->mlxsw_sp
, hdroom
))
551 err
= mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port
, &tmp_hdroom
, force
);
555 err
= mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port
, hdroom
, force
);
557 goto err_configure_priomap
;
559 err
= mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port
, hdroom
, false);
561 goto err_configure_buffers
;
563 err
= mlxsw_sp_hdroom_configure_int_buf(mlxsw_sp_port
, hdroom
, false);
565 goto err_configure_int_buf
;
567 *mlxsw_sp_port
->hdroom
= *hdroom
;
570 err_configure_int_buf
:
571 mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port
, &tmp_hdroom
, false);
572 err_configure_buffers
:
573 mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port
, &tmp_hdroom
, false);
574 err_configure_priomap
:
575 mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port
, &orig_hdroom
, false);
579 int mlxsw_sp_hdroom_configure(struct mlxsw_sp_port
*mlxsw_sp_port
,
580 const struct mlxsw_sp_hdroom
*hdroom
)
582 return __mlxsw_sp_hdroom_configure(mlxsw_sp_port
, hdroom
, false);
585 static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
587 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
588 struct mlxsw_sp_hdroom hdroom
= {};
592 hdroom
.mtu
= mlxsw_sp_port
->dev
->mtu
;
593 hdroom
.mode
= MLXSW_SP_HDROOM_MODE_DCB
;
594 for (prio
= 0; prio
< IEEE_8021QAZ_MAX_TCS
; prio
++)
595 hdroom
.prios
.prio
[prio
].lossy
= true;
597 mlxsw_sp_hdroom_bufs_reset_lossiness(&hdroom
);
598 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port
, &hdroom
);
600 /* Buffer 9 is used for control traffic. */
601 size9
= mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port
, mlxsw_sp_port
->max_mtu
);
602 hdroom
.bufs
.buf
[9].size_cells
= mlxsw_sp_bytes_cells(mlxsw_sp
, size9
);
604 return __mlxsw_sp_hdroom_configure(mlxsw_sp_port
, &hdroom
, true);
607 static int mlxsw_sp_sb_port_init(struct mlxsw_sp
*mlxsw_sp
,
608 struct mlxsw_sp_sb_port
*sb_port
)
610 struct mlxsw_sp_sb_pm
*pms
;
612 pms
= kcalloc(mlxsw_sp
->sb_vals
->pool_count
, sizeof(*pms
),
620 static void mlxsw_sp_sb_port_fini(struct mlxsw_sp_sb_port
*sb_port
)
625 static int mlxsw_sp_sb_ports_init(struct mlxsw_sp
*mlxsw_sp
)
627 unsigned int max_ports
= mlxsw_core_max_ports(mlxsw_sp
->core
);
628 struct mlxsw_sp_sb_pr
*prs
;
632 mlxsw_sp
->sb
->ports
= kcalloc(max_ports
,
633 sizeof(struct mlxsw_sp_sb_port
),
635 if (!mlxsw_sp
->sb
->ports
)
638 prs
= kcalloc(mlxsw_sp
->sb_vals
->pool_count
, sizeof(*prs
),
644 mlxsw_sp
->sb
->prs
= prs
;
646 for (i
= 0; i
< max_ports
; i
++) {
647 err
= mlxsw_sp_sb_port_init(mlxsw_sp
, &mlxsw_sp
->sb
->ports
[i
]);
649 goto err_sb_port_init
;
655 for (i
--; i
>= 0; i
--)
656 mlxsw_sp_sb_port_fini(&mlxsw_sp
->sb
->ports
[i
]);
657 kfree(mlxsw_sp
->sb
->prs
);
659 kfree(mlxsw_sp
->sb
->ports
);
663 static void mlxsw_sp_sb_ports_fini(struct mlxsw_sp
*mlxsw_sp
)
665 int max_ports
= mlxsw_core_max_ports(mlxsw_sp
->core
);
668 for (i
= max_ports
- 1; i
>= 0; i
--)
669 mlxsw_sp_sb_port_fini(&mlxsw_sp
->sb
->ports
[i
]);
670 kfree(mlxsw_sp
->sb
->prs
);
671 kfree(mlxsw_sp
->sb
->ports
);
674 #define MLXSW_SP_SB_PR(_mode, _size) \
680 #define MLXSW_SP_SB_PR_EXT(_mode, _size, _freeze_mode, _freeze_size) \
684 .freeze_mode = _freeze_mode, \
685 .freeze_size = _freeze_size, \
688 #define MLXSW_SP1_SB_PR_CPU_SIZE (256 * 1000)
690 /* Order according to mlxsw_sp1_sb_pool_dess */
691 static const struct mlxsw_sp_sb_pr mlxsw_sp1_sb_prs
[] = {
692 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, MLXSW_SP_SB_REST
),
693 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
694 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
695 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
696 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
, MLXSW_SP_SB_REST
,
698 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
699 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
700 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, 0),
701 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC
, MLXSW_SP_SB_INFI
,
703 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
,
704 MLXSW_SP1_SB_PR_CPU_SIZE
, true, false),
705 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
,
706 MLXSW_SP1_SB_PR_CPU_SIZE
, true, false),
709 #define MLXSW_SP2_SB_PR_CPU_SIZE (256 * 1000)
711 /* Order according to mlxsw_sp2_sb_pool_dess */
712 static const struct mlxsw_sp_sb_pr mlxsw_sp2_sb_prs
[] = {
713 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC
, MLXSW_SP_SB_REST
),
714 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
715 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
716 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
717 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
, MLXSW_SP_SB_REST
,
719 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
720 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
721 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC
, 0),
722 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC
, MLXSW_SP_SB_INFI
,
724 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
,
725 MLXSW_SP2_SB_PR_CPU_SIZE
, true, false),
726 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC
,
727 MLXSW_SP2_SB_PR_CPU_SIZE
, true, false),
730 static int mlxsw_sp_sb_prs_init(struct mlxsw_sp
*mlxsw_sp
,
731 const struct mlxsw_sp_sb_pr
*prs
,
732 const struct mlxsw_sp_sb_pool_des
*pool_dess
,
735 /* Round down, unlike mlxsw_sp_bytes_cells(). */
736 u32 sb_cells
= div_u64(mlxsw_sp
->sb
->sb_size
, mlxsw_sp
->sb
->cell_size
);
737 u32 rest_cells
[2] = {sb_cells
, sb_cells
};
741 /* Calculate how much space to give to the "REST" pools in either
744 for (i
= 0; i
< prs_len
; i
++) {
745 enum mlxsw_reg_sbxx_dir dir
= pool_dess
[i
].dir
;
746 u32 size
= prs
[i
].size
;
749 if (size
== MLXSW_SP_SB_INFI
|| size
== MLXSW_SP_SB_REST
)
752 size_cells
= mlxsw_sp_bytes_cells(mlxsw_sp
, size
);
753 if (WARN_ON_ONCE(size_cells
> rest_cells
[dir
]))
756 rest_cells
[dir
] -= size_cells
;
759 for (i
= 0; i
< prs_len
; i
++) {
760 u32 size
= prs
[i
].size
;
763 if (size
== MLXSW_SP_SB_INFI
) {
764 err
= mlxsw_sp_sb_pr_write(mlxsw_sp
, i
, prs
[i
].mode
,
766 } else if (size
== MLXSW_SP_SB_REST
) {
767 size_cells
= rest_cells
[pool_dess
[i
].dir
];
768 err
= mlxsw_sp_sb_pr_write(mlxsw_sp
, i
, prs
[i
].mode
,
771 size_cells
= mlxsw_sp_bytes_cells(mlxsw_sp
, size
);
772 err
= mlxsw_sp_sb_pr_write(mlxsw_sp
, i
, prs
[i
].mode
,
781 #define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool) \
783 .min_buff = _min_buff, \
784 .max_buff = _max_buff, \
785 .pool_index = _pool, \
788 #define MLXSW_SP_SB_CM_ING(_min_buff, _max_buff) \
790 .min_buff = _min_buff, \
791 .max_buff = _max_buff, \
792 .pool_index = MLXSW_SP_SB_POOL_ING, \
795 #define MLXSW_SP_SB_CM_EGR(_min_buff, _max_buff) \
797 .min_buff = _min_buff, \
798 .max_buff = _max_buff, \
799 .pool_index = MLXSW_SP_SB_POOL_EGR, \
802 #define MLXSW_SP_SB_CM_EGR_MC(_min_buff, _max_buff) \
804 .min_buff = _min_buff, \
805 .max_buff = _max_buff, \
806 .pool_index = MLXSW_SP_SB_POOL_EGR_MC, \
807 .freeze_pool = true, \
808 .freeze_thresh = true, \
811 static const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_ingress
[] = {
812 MLXSW_SP_SB_CM_ING(10000, 8),
813 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
814 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
815 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
816 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
817 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
818 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
819 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
820 MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
821 MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU
),
824 static const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_ingress
[] = {
825 MLXSW_SP_SB_CM_ING(0, 7),
826 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
827 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
828 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
829 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
830 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
831 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
832 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
833 MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
834 MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU
),
837 static const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_egress
[] = {
838 MLXSW_SP_SB_CM_EGR(1500, 9),
839 MLXSW_SP_SB_CM_EGR(1500, 9),
840 MLXSW_SP_SB_CM_EGR(1500, 9),
841 MLXSW_SP_SB_CM_EGR(1500, 9),
842 MLXSW_SP_SB_CM_EGR(1500, 9),
843 MLXSW_SP_SB_CM_EGR(1500, 9),
844 MLXSW_SP_SB_CM_EGR(1500, 9),
845 MLXSW_SP_SB_CM_EGR(1500, 9),
846 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
847 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
848 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
849 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
850 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
851 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
852 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
853 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
854 MLXSW_SP_SB_CM_EGR(1, 0xff),
857 static const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_egress
[] = {
858 MLXSW_SP_SB_CM_EGR(0, 7),
859 MLXSW_SP_SB_CM_EGR(0, 7),
860 MLXSW_SP_SB_CM_EGR(0, 7),
861 MLXSW_SP_SB_CM_EGR(0, 7),
862 MLXSW_SP_SB_CM_EGR(0, 7),
863 MLXSW_SP_SB_CM_EGR(0, 7),
864 MLXSW_SP_SB_CM_EGR(0, 7),
865 MLXSW_SP_SB_CM_EGR(0, 7),
866 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
867 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
868 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
869 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
870 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
871 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
872 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
873 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI
),
874 MLXSW_SP_SB_CM_EGR(1, 0xff),
877 #define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, MLXSW_SP_SB_POOL_EGR_CPU)
879 static const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms
[] = {
880 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
881 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
882 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
883 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
884 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
885 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
886 MLXSW_SP_CPU_PORT_SB_CM
,
887 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU
),
888 MLXSW_SP_CPU_PORT_SB_CM
,
889 MLXSW_SP_CPU_PORT_SB_CM
,
890 MLXSW_SP_CPU_PORT_SB_CM
,
891 MLXSW_SP_CPU_PORT_SB_CM
,
892 MLXSW_SP_CPU_PORT_SB_CM
,
893 MLXSW_SP_CPU_PORT_SB_CM
,
894 MLXSW_SP_CPU_PORT_SB_CM
,
895 MLXSW_SP_CPU_PORT_SB_CM
,
896 MLXSW_SP_CPU_PORT_SB_CM
,
897 MLXSW_SP_CPU_PORT_SB_CM
,
898 MLXSW_SP_CPU_PORT_SB_CM
,
899 MLXSW_SP_CPU_PORT_SB_CM
,
900 MLXSW_SP_CPU_PORT_SB_CM
,
901 MLXSW_SP_CPU_PORT_SB_CM
,
902 MLXSW_SP_CPU_PORT_SB_CM
,
903 MLXSW_SP_CPU_PORT_SB_CM
,
904 MLXSW_SP_CPU_PORT_SB_CM
,
905 MLXSW_SP_CPU_PORT_SB_CM
,
906 MLXSW_SP_CPU_PORT_SB_CM
,
907 MLXSW_SP_CPU_PORT_SB_CM
,
908 MLXSW_SP_CPU_PORT_SB_CM
,
909 MLXSW_SP_CPU_PORT_SB_CM
,
910 MLXSW_SP_CPU_PORT_SB_CM
,
911 MLXSW_SP_CPU_PORT_SB_CM
,
915 mlxsw_sp_sb_pool_is_static(struct mlxsw_sp
*mlxsw_sp
, u16 pool_index
)
917 struct mlxsw_sp_sb_pr
*pr
= mlxsw_sp_sb_pr_get(mlxsw_sp
, pool_index
);
919 return pr
->mode
== MLXSW_REG_SBPR_MODE_STATIC
;
922 static int __mlxsw_sp_sb_cms_init(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
923 enum mlxsw_reg_sbxx_dir dir
,
924 const struct mlxsw_sp_sb_cm
*cms
,
927 const struct mlxsw_sp_sb_vals
*sb_vals
= mlxsw_sp
->sb_vals
;
931 for (i
= 0; i
< cms_len
; i
++) {
932 const struct mlxsw_sp_sb_cm
*cm
;
936 if (i
== 8 && dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
937 continue; /* PG number 8 does not exist, skip it */
939 if (WARN_ON(sb_vals
->pool_dess
[cm
->pool_index
].dir
!= dir
))
942 min_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
, cm
->min_buff
);
943 max_buff
= cm
->max_buff
;
944 if (max_buff
== MLXSW_SP_SB_INFI
) {
945 err
= mlxsw_sp_sb_cm_write(mlxsw_sp
, local_port
, i
,
947 true, cm
->pool_index
);
949 if (mlxsw_sp_sb_pool_is_static(mlxsw_sp
,
951 max_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
,
953 err
= mlxsw_sp_sb_cm_write(mlxsw_sp
, local_port
, i
,
955 false, cm
->pool_index
);
963 static int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
965 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
968 err
= __mlxsw_sp_sb_cms_init(mlxsw_sp
,
969 mlxsw_sp_port
->local_port
,
970 MLXSW_REG_SBXX_DIR_INGRESS
,
971 mlxsw_sp
->sb_vals
->cms_ingress
,
972 mlxsw_sp
->sb_vals
->cms_ingress_count
);
975 return __mlxsw_sp_sb_cms_init(mlxsw_sp_port
->mlxsw_sp
,
976 mlxsw_sp_port
->local_port
,
977 MLXSW_REG_SBXX_DIR_EGRESS
,
978 mlxsw_sp
->sb_vals
->cms_egress
,
979 mlxsw_sp
->sb_vals
->cms_egress_count
);
982 static int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp
*mlxsw_sp
)
984 return __mlxsw_sp_sb_cms_init(mlxsw_sp
, 0, MLXSW_REG_SBXX_DIR_EGRESS
,
985 mlxsw_sp
->sb_vals
->cms_cpu
,
986 mlxsw_sp
->sb_vals
->cms_cpu_count
);
989 #define MLXSW_SP_SB_PM(_min_buff, _max_buff) \
991 .min_buff = _min_buff, \
992 .max_buff = _max_buff, \
995 /* Order according to mlxsw_sp1_sb_pool_dess */
996 static const struct mlxsw_sp_sb_pm mlxsw_sp1_sb_pms
[] = {
997 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX
),
998 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
999 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1000 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1001 MLXSW_SP_SB_PM(0, 7),
1002 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1003 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1004 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1005 MLXSW_SP_SB_PM(10000, 90000),
1006 MLXSW_SP_SB_PM(0, 8), /* 50% occupancy */
1007 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1010 /* Order according to mlxsw_sp2_sb_pool_dess */
1011 static const struct mlxsw_sp_sb_pm mlxsw_sp2_sb_pms
[] = {
1012 MLXSW_SP_SB_PM(0, 7),
1013 MLXSW_SP_SB_PM(0, 0),
1014 MLXSW_SP_SB_PM(0, 0),
1015 MLXSW_SP_SB_PM(0, 0),
1016 MLXSW_SP_SB_PM(0, 7),
1017 MLXSW_SP_SB_PM(0, 0),
1018 MLXSW_SP_SB_PM(0, 0),
1019 MLXSW_SP_SB_PM(0, 0),
1020 MLXSW_SP_SB_PM(10000, 90000),
1021 MLXSW_SP_SB_PM(0, 8), /* 50% occupancy */
1022 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
),
1025 /* Order according to mlxsw_sp*_sb_pool_dess */
1026 static const struct mlxsw_sp_sb_pm mlxsw_sp_cpu_port_sb_pms
[] = {
1027 MLXSW_SP_SB_PM(0, 0),
1028 MLXSW_SP_SB_PM(0, 0),
1029 MLXSW_SP_SB_PM(0, 0),
1030 MLXSW_SP_SB_PM(0, 0),
1031 MLXSW_SP_SB_PM(0, 0),
1032 MLXSW_SP_SB_PM(0, 0),
1033 MLXSW_SP_SB_PM(0, 0),
1034 MLXSW_SP_SB_PM(0, 0),
1035 MLXSW_SP_SB_PM(0, 90000),
1036 MLXSW_SP_SB_PM(0, 0),
1037 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX
),
1040 static int mlxsw_sp_sb_pms_init(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
1041 const struct mlxsw_sp_sb_pm
*pms
,
1046 for (i
= 0; i
< mlxsw_sp
->sb_vals
->pool_count
; i
++) {
1047 const struct mlxsw_sp_sb_pm
*pm
= &pms
[i
];
1048 const struct mlxsw_sp_sb_pool_des
*des
;
1052 des
= &mlxsw_sp
->sb_vals
->pool_dess
[i
];
1053 if (skip_ingress
&& des
->dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
1056 min_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
, pm
->min_buff
);
1057 max_buff
= pm
->max_buff
;
1058 if (mlxsw_sp_sb_pool_is_static(mlxsw_sp
, i
))
1059 max_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
, max_buff
);
1060 err
= mlxsw_sp_sb_pm_write(mlxsw_sp
, local_port
, i
, min_buff
,
1068 static int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
1070 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1072 return mlxsw_sp_sb_pms_init(mlxsw_sp
, mlxsw_sp_port
->local_port
,
1073 mlxsw_sp
->sb_vals
->pms
, false);
1076 static int mlxsw_sp_cpu_port_sb_pms_init(struct mlxsw_sp
*mlxsw_sp
)
1078 return mlxsw_sp_sb_pms_init(mlxsw_sp
, 0, mlxsw_sp
->sb_vals
->pms_cpu
,
1082 #define MLXSW_SP_SB_MM(_min_buff, _max_buff) \
1084 .min_buff = _min_buff, \
1085 .max_buff = _max_buff, \
1086 .pool_index = MLXSW_SP_SB_POOL_EGR, \
1089 static const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms
[] = {
1090 MLXSW_SP_SB_MM(0, 6),
1091 MLXSW_SP_SB_MM(0, 6),
1092 MLXSW_SP_SB_MM(0, 6),
1093 MLXSW_SP_SB_MM(0, 6),
1094 MLXSW_SP_SB_MM(0, 6),
1095 MLXSW_SP_SB_MM(0, 6),
1096 MLXSW_SP_SB_MM(0, 6),
1097 MLXSW_SP_SB_MM(0, 6),
1098 MLXSW_SP_SB_MM(0, 6),
1099 MLXSW_SP_SB_MM(0, 6),
1100 MLXSW_SP_SB_MM(0, 6),
1101 MLXSW_SP_SB_MM(0, 6),
1102 MLXSW_SP_SB_MM(0, 6),
1103 MLXSW_SP_SB_MM(0, 6),
1104 MLXSW_SP_SB_MM(0, 6),
1107 static int mlxsw_sp_sb_mms_init(struct mlxsw_sp
*mlxsw_sp
)
1109 char sbmm_pl
[MLXSW_REG_SBMM_LEN
];
1113 for (i
= 0; i
< mlxsw_sp
->sb_vals
->mms_count
; i
++) {
1114 const struct mlxsw_sp_sb_pool_des
*des
;
1115 const struct mlxsw_sp_sb_mm
*mc
;
1118 mc
= &mlxsw_sp
->sb_vals
->mms
[i
];
1119 des
= &mlxsw_sp
->sb_vals
->pool_dess
[mc
->pool_index
];
1120 /* All pools used by sb_mm's are initialized using dynamic
1121 * thresholds, therefore 'max_buff' isn't specified in cells.
1123 min_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
, mc
->min_buff
);
1124 mlxsw_reg_sbmm_pack(sbmm_pl
, i
, min_buff
, mc
->max_buff
,
1126 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbmm
), sbmm_pl
);
1133 static void mlxsw_sp_pool_count(struct mlxsw_sp
*mlxsw_sp
,
1134 u16
*p_ingress_len
, u16
*p_egress_len
)
1138 for (i
= 0; i
< mlxsw_sp
->sb_vals
->pool_count
; ++i
) {
1139 if (mlxsw_sp
->sb_vals
->pool_dess
[i
].dir
==
1140 MLXSW_REG_SBXX_DIR_INGRESS
)
1146 WARN(*p_egress_len
== 0, "No egress pools\n");
1149 const struct mlxsw_sp_sb_vals mlxsw_sp1_sb_vals
= {
1150 .pool_count
= ARRAY_SIZE(mlxsw_sp1_sb_pool_dess
),
1151 .pool_dess
= mlxsw_sp1_sb_pool_dess
,
1152 .pms
= mlxsw_sp1_sb_pms
,
1153 .pms_cpu
= mlxsw_sp_cpu_port_sb_pms
,
1154 .prs
= mlxsw_sp1_sb_prs
,
1155 .mms
= mlxsw_sp_sb_mms
,
1156 .cms_ingress
= mlxsw_sp1_sb_cms_ingress
,
1157 .cms_egress
= mlxsw_sp1_sb_cms_egress
,
1158 .cms_cpu
= mlxsw_sp_cpu_port_sb_cms
,
1159 .mms_count
= ARRAY_SIZE(mlxsw_sp_sb_mms
),
1160 .cms_ingress_count
= ARRAY_SIZE(mlxsw_sp1_sb_cms_ingress
),
1161 .cms_egress_count
= ARRAY_SIZE(mlxsw_sp1_sb_cms_egress
),
1162 .cms_cpu_count
= ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms
),
1165 const struct mlxsw_sp_sb_vals mlxsw_sp2_sb_vals
= {
1166 .pool_count
= ARRAY_SIZE(mlxsw_sp2_sb_pool_dess
),
1167 .pool_dess
= mlxsw_sp2_sb_pool_dess
,
1168 .pms
= mlxsw_sp2_sb_pms
,
1169 .pms_cpu
= mlxsw_sp_cpu_port_sb_pms
,
1170 .prs
= mlxsw_sp2_sb_prs
,
1171 .mms
= mlxsw_sp_sb_mms
,
1172 .cms_ingress
= mlxsw_sp2_sb_cms_ingress
,
1173 .cms_egress
= mlxsw_sp2_sb_cms_egress
,
1174 .cms_cpu
= mlxsw_sp_cpu_port_sb_cms
,
1175 .mms_count
= ARRAY_SIZE(mlxsw_sp_sb_mms
),
1176 .cms_ingress_count
= ARRAY_SIZE(mlxsw_sp2_sb_cms_ingress
),
1177 .cms_egress_count
= ARRAY_SIZE(mlxsw_sp2_sb_cms_egress
),
1178 .cms_cpu_count
= ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms
),
1181 static u32
mlxsw_sp1_pb_int_buf_size_get(int mtu
, u32 speed
)
1186 static u32
__mlxsw_sp_pb_int_buf_size_get(int mtu
, u32 speed
, u32 buffer_factor
)
1188 return 3 * mtu
+ buffer_factor
* speed
/ 1000;
1191 #define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38
1193 static u32
mlxsw_sp2_pb_int_buf_size_get(int mtu
, u32 speed
)
1195 int factor
= MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR
;
1197 return __mlxsw_sp_pb_int_buf_size_get(mtu
, speed
, factor
);
1200 #define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50
1202 static u32
mlxsw_sp3_pb_int_buf_size_get(int mtu
, u32 speed
)
1204 int factor
= MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR
;
1206 return __mlxsw_sp_pb_int_buf_size_get(mtu
, speed
, factor
);
1209 const struct mlxsw_sp_sb_ops mlxsw_sp1_sb_ops
= {
1210 .int_buf_size_get
= mlxsw_sp1_pb_int_buf_size_get
,
1213 const struct mlxsw_sp_sb_ops mlxsw_sp2_sb_ops
= {
1214 .int_buf_size_get
= mlxsw_sp2_pb_int_buf_size_get
,
1217 const struct mlxsw_sp_sb_ops mlxsw_sp3_sb_ops
= {
1218 .int_buf_size_get
= mlxsw_sp3_pb_int_buf_size_get
,
1221 int mlxsw_sp_buffers_init(struct mlxsw_sp
*mlxsw_sp
)
1223 u32 max_headroom_size
;
1224 u16 ing_pool_count
= 0;
1225 u16 eg_pool_count
= 0;
1228 if (!MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, CELL_SIZE
))
1231 if (!MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, GUARANTEED_SHARED_BUFFER
))
1234 if (!MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, MAX_HEADROOM_SIZE
))
1237 mlxsw_sp
->sb
= kzalloc(sizeof(*mlxsw_sp
->sb
), GFP_KERNEL
);
1240 mlxsw_sp
->sb
->cell_size
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
, CELL_SIZE
);
1241 mlxsw_sp
->sb
->sb_size
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1242 GUARANTEED_SHARED_BUFFER
);
1243 max_headroom_size
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1245 /* Round down, because this limit must not be overstepped. */
1246 mlxsw_sp
->sb
->max_headroom_cells
= max_headroom_size
/
1247 mlxsw_sp
->sb
->cell_size
;
1249 err
= mlxsw_sp_sb_ports_init(mlxsw_sp
);
1251 goto err_sb_ports_init
;
1252 err
= mlxsw_sp_sb_prs_init(mlxsw_sp
, mlxsw_sp
->sb_vals
->prs
,
1253 mlxsw_sp
->sb_vals
->pool_dess
,
1254 mlxsw_sp
->sb_vals
->pool_count
);
1256 goto err_sb_prs_init
;
1257 err
= mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp
);
1259 goto err_sb_cpu_port_sb_cms_init
;
1260 err
= mlxsw_sp_cpu_port_sb_pms_init(mlxsw_sp
);
1262 goto err_sb_cpu_port_pms_init
;
1263 err
= mlxsw_sp_sb_mms_init(mlxsw_sp
);
1265 goto err_sb_mms_init
;
1266 mlxsw_sp_pool_count(mlxsw_sp
, &ing_pool_count
, &eg_pool_count
);
1267 err
= devlink_sb_register(priv_to_devlink(mlxsw_sp
->core
), 0,
1268 mlxsw_sp
->sb
->sb_size
,
1271 MLXSW_SP_SB_ING_TC_COUNT
,
1272 MLXSW_SP_SB_EG_TC_COUNT
);
1274 goto err_devlink_sb_register
;
1278 err_devlink_sb_register
:
1280 err_sb_cpu_port_pms_init
:
1281 err_sb_cpu_port_sb_cms_init
:
1283 mlxsw_sp_sb_ports_fini(mlxsw_sp
);
1285 kfree(mlxsw_sp
->sb
);
1289 void mlxsw_sp_buffers_fini(struct mlxsw_sp
*mlxsw_sp
)
1291 devlink_sb_unregister(priv_to_devlink(mlxsw_sp
->core
), 0);
1292 mlxsw_sp_sb_ports_fini(mlxsw_sp
);
1293 kfree(mlxsw_sp
->sb
);
1296 int mlxsw_sp_port_buffers_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
1300 mlxsw_sp_port
->hdroom
= kzalloc(sizeof(*mlxsw_sp_port
->hdroom
), GFP_KERNEL
);
1301 if (!mlxsw_sp_port
->hdroom
)
1303 mlxsw_sp_port
->hdroom
->mtu
= mlxsw_sp_port
->dev
->mtu
;
1305 err
= mlxsw_sp_port_headroom_init(mlxsw_sp_port
);
1307 goto err_headroom_init
;
1308 err
= mlxsw_sp_port_sb_cms_init(mlxsw_sp_port
);
1310 goto err_port_sb_cms_init
;
1311 err
= mlxsw_sp_port_sb_pms_init(mlxsw_sp_port
);
1313 goto err_port_sb_pms_init
;
1316 err_port_sb_pms_init
:
1317 err_port_sb_cms_init
:
1319 kfree(mlxsw_sp_port
->hdroom
);
1323 void mlxsw_sp_port_buffers_fini(struct mlxsw_sp_port
*mlxsw_sp_port
)
1325 kfree(mlxsw_sp_port
->hdroom
);
1328 int mlxsw_sp_sb_pool_get(struct mlxsw_core
*mlxsw_core
,
1329 unsigned int sb_index
, u16 pool_index
,
1330 struct devlink_sb_pool_info
*pool_info
)
1332 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
1333 enum mlxsw_reg_sbxx_dir dir
;
1334 struct mlxsw_sp_sb_pr
*pr
;
1336 dir
= mlxsw_sp
->sb_vals
->pool_dess
[pool_index
].dir
;
1337 pr
= mlxsw_sp_sb_pr_get(mlxsw_sp
, pool_index
);
1338 pool_info
->pool_type
= (enum devlink_sb_pool_type
) dir
;
1339 pool_info
->size
= mlxsw_sp_cells_bytes(mlxsw_sp
, pr
->size
);
1340 pool_info
->threshold_type
= (enum devlink_sb_threshold_type
) pr
->mode
;
1341 pool_info
->cell_size
= mlxsw_sp
->sb
->cell_size
;
1345 int mlxsw_sp_sb_pool_set(struct mlxsw_core
*mlxsw_core
,
1346 unsigned int sb_index
, u16 pool_index
, u32 size
,
1347 enum devlink_sb_threshold_type threshold_type
,
1348 struct netlink_ext_ack
*extack
)
1350 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
1351 u32 pool_size
= mlxsw_sp_bytes_cells(mlxsw_sp
, size
);
1352 const struct mlxsw_sp_sb_pr
*pr
;
1353 enum mlxsw_reg_sbpr_mode mode
;
1355 mode
= (enum mlxsw_reg_sbpr_mode
) threshold_type
;
1356 pr
= &mlxsw_sp
->sb_vals
->prs
[pool_index
];
1358 if (size
> MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
1359 GUARANTEED_SHARED_BUFFER
)) {
1360 NL_SET_ERR_MSG_MOD(extack
, "Exceeded shared buffer size");
1364 if (pr
->freeze_mode
&& pr
->mode
!= mode
) {
1365 NL_SET_ERR_MSG_MOD(extack
, "Changing this pool's threshold type is forbidden");
1369 if (pr
->freeze_size
&& pr
->size
!= size
) {
1370 NL_SET_ERR_MSG_MOD(extack
, "Changing this pool's size is forbidden");
1374 return mlxsw_sp_sb_pr_write(mlxsw_sp
, pool_index
, mode
,
1378 #define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */
1380 static u32
mlxsw_sp_sb_threshold_out(struct mlxsw_sp
*mlxsw_sp
, u16 pool_index
,
1383 struct mlxsw_sp_sb_pr
*pr
= mlxsw_sp_sb_pr_get(mlxsw_sp
, pool_index
);
1385 if (pr
->mode
== MLXSW_REG_SBPR_MODE_DYNAMIC
)
1386 return max_buff
- MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET
;
1387 return mlxsw_sp_cells_bytes(mlxsw_sp
, max_buff
);
1390 static int mlxsw_sp_sb_threshold_in(struct mlxsw_sp
*mlxsw_sp
, u16 pool_index
,
1391 u32 threshold
, u32
*p_max_buff
,
1392 struct netlink_ext_ack
*extack
)
1394 struct mlxsw_sp_sb_pr
*pr
= mlxsw_sp_sb_pr_get(mlxsw_sp
, pool_index
);
1396 if (pr
->mode
== MLXSW_REG_SBPR_MODE_DYNAMIC
) {
1399 val
= threshold
+ MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET
;
1400 if (val
< MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN
||
1401 val
> MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX
) {
1402 NL_SET_ERR_MSG_MOD(extack
, "Invalid dynamic threshold value");
1407 *p_max_buff
= mlxsw_sp_bytes_cells(mlxsw_sp
, threshold
);
1412 int mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port
*mlxsw_core_port
,
1413 unsigned int sb_index
, u16 pool_index
,
1416 struct mlxsw_sp_port
*mlxsw_sp_port
=
1417 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1418 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1419 u8 local_port
= mlxsw_sp_port
->local_port
;
1420 struct mlxsw_sp_sb_pm
*pm
= mlxsw_sp_sb_pm_get(mlxsw_sp
, local_port
,
1423 *p_threshold
= mlxsw_sp_sb_threshold_out(mlxsw_sp
, pool_index
,
1428 int mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port
*mlxsw_core_port
,
1429 unsigned int sb_index
, u16 pool_index
,
1430 u32 threshold
, struct netlink_ext_ack
*extack
)
1432 struct mlxsw_sp_port
*mlxsw_sp_port
=
1433 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1434 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1435 u8 local_port
= mlxsw_sp_port
->local_port
;
1439 if (local_port
== MLXSW_PORT_CPU_PORT
) {
1440 NL_SET_ERR_MSG_MOD(extack
, "Changing CPU port's threshold is forbidden");
1444 err
= mlxsw_sp_sb_threshold_in(mlxsw_sp
, pool_index
,
1445 threshold
, &max_buff
, extack
);
1449 return mlxsw_sp_sb_pm_write(mlxsw_sp
, local_port
, pool_index
,
1453 int mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port
*mlxsw_core_port
,
1454 unsigned int sb_index
, u16 tc_index
,
1455 enum devlink_sb_pool_type pool_type
,
1456 u16
*p_pool_index
, u32
*p_threshold
)
1458 struct mlxsw_sp_port
*mlxsw_sp_port
=
1459 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1460 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1461 u8 local_port
= mlxsw_sp_port
->local_port
;
1462 u8 pg_buff
= tc_index
;
1463 enum mlxsw_reg_sbxx_dir dir
= (enum mlxsw_reg_sbxx_dir
) pool_type
;
1464 struct mlxsw_sp_sb_cm
*cm
= mlxsw_sp_sb_cm_get(mlxsw_sp
, local_port
,
1467 *p_threshold
= mlxsw_sp_sb_threshold_out(mlxsw_sp
, cm
->pool_index
,
1469 *p_pool_index
= cm
->pool_index
;
1473 int mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port
*mlxsw_core_port
,
1474 unsigned int sb_index
, u16 tc_index
,
1475 enum devlink_sb_pool_type pool_type
,
1476 u16 pool_index
, u32 threshold
,
1477 struct netlink_ext_ack
*extack
)
1479 struct mlxsw_sp_port
*mlxsw_sp_port
=
1480 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1481 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1482 u8 local_port
= mlxsw_sp_port
->local_port
;
1483 const struct mlxsw_sp_sb_cm
*cm
;
1484 u8 pg_buff
= tc_index
;
1485 enum mlxsw_reg_sbxx_dir dir
= (enum mlxsw_reg_sbxx_dir
) pool_type
;
1489 if (local_port
== MLXSW_PORT_CPU_PORT
) {
1490 NL_SET_ERR_MSG_MOD(extack
, "Changing CPU port's binding is forbidden");
1494 if (dir
!= mlxsw_sp
->sb_vals
->pool_dess
[pool_index
].dir
) {
1495 NL_SET_ERR_MSG_MOD(extack
, "Binding egress TC to ingress pool and vice versa is forbidden");
1499 if (dir
== MLXSW_REG_SBXX_DIR_INGRESS
)
1500 cm
= &mlxsw_sp
->sb_vals
->cms_ingress
[tc_index
];
1502 cm
= &mlxsw_sp
->sb_vals
->cms_egress
[tc_index
];
1504 if (cm
->freeze_pool
&& cm
->pool_index
!= pool_index
) {
1505 NL_SET_ERR_MSG_MOD(extack
, "Binding this TC to a different pool is forbidden");
1509 if (cm
->freeze_thresh
&& cm
->max_buff
!= threshold
) {
1510 NL_SET_ERR_MSG_MOD(extack
, "Changing this TC's threshold is forbidden");
1514 err
= mlxsw_sp_sb_threshold_in(mlxsw_sp
, pool_index
,
1515 threshold
, &max_buff
, extack
);
1519 return mlxsw_sp_sb_cm_write(mlxsw_sp
, local_port
, pg_buff
,
1520 0, max_buff
, false, pool_index
);
1523 #define MASKED_COUNT_MAX \
1524 (MLXSW_REG_SBSR_REC_MAX_COUNT / \
1525 (MLXSW_SP_SB_ING_TC_COUNT + MLXSW_SP_SB_EG_TC_COUNT))
1527 struct mlxsw_sp_sb_sr_occ_query_cb_ctx
{
1532 static void mlxsw_sp_sb_sr_occ_query_cb(struct mlxsw_core
*mlxsw_core
,
1533 char *sbsr_pl
, size_t sbsr_pl_len
,
1534 unsigned long cb_priv
)
1536 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
1537 struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx
;
1541 struct mlxsw_sp_sb_cm
*cm
;
1544 memcpy(&cb_ctx
, &cb_priv
, sizeof(cb_ctx
));
1547 for (local_port
= cb_ctx
.local_port_1
;
1548 local_port
< mlxsw_core_max_ports(mlxsw_core
); local_port
++) {
1549 if (!mlxsw_sp
->ports
[local_port
])
1551 if (local_port
== MLXSW_PORT_CPU_PORT
) {
1552 /* Ingress quotas are not supported for the CPU port */
1556 for (i
= 0; i
< MLXSW_SP_SB_ING_TC_COUNT
; i
++) {
1557 cm
= mlxsw_sp_sb_cm_get(mlxsw_sp
, local_port
, i
,
1558 MLXSW_REG_SBXX_DIR_INGRESS
);
1559 mlxsw_reg_sbsr_rec_unpack(sbsr_pl
, rec_index
++,
1560 &cm
->occ
.cur
, &cm
->occ
.max
);
1562 if (++masked_count
== cb_ctx
.masked_count
)
1566 for (local_port
= cb_ctx
.local_port_1
;
1567 local_port
< mlxsw_core_max_ports(mlxsw_core
); local_port
++) {
1568 if (!mlxsw_sp
->ports
[local_port
])
1570 for (i
= 0; i
< MLXSW_SP_SB_EG_TC_COUNT
; i
++) {
1571 cm
= mlxsw_sp_sb_cm_get(mlxsw_sp
, local_port
, i
,
1572 MLXSW_REG_SBXX_DIR_EGRESS
);
1573 mlxsw_reg_sbsr_rec_unpack(sbsr_pl
, rec_index
++,
1574 &cm
->occ
.cur
, &cm
->occ
.max
);
1576 if (++masked_count
== cb_ctx
.masked_count
)
1581 int mlxsw_sp_sb_occ_snapshot(struct mlxsw_core
*mlxsw_core
,
1582 unsigned int sb_index
)
1584 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
1585 struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx
;
1586 unsigned long cb_priv
;
1587 LIST_HEAD(bulk_list
);
1596 sbsr_pl
= kmalloc(MLXSW_REG_SBSR_LEN
, GFP_KERNEL
);
1600 local_port
= MLXSW_PORT_CPU_PORT
;
1602 local_port_1
= local_port
;
1604 mlxsw_reg_sbsr_pack(sbsr_pl
, false);
1605 for (i
= 0; i
< MLXSW_SP_SB_ING_TC_COUNT
; i
++)
1606 mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl
, i
, 1);
1607 for (i
= 0; i
< MLXSW_SP_SB_EG_TC_COUNT
; i
++)
1608 mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl
, i
, 1);
1609 for (; local_port
< mlxsw_core_max_ports(mlxsw_core
); local_port
++) {
1610 if (!mlxsw_sp
->ports
[local_port
])
1612 if (local_port
!= MLXSW_PORT_CPU_PORT
) {
1613 /* Ingress quotas are not supported for the CPU port */
1614 mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl
,
1617 mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl
, local_port
, 1);
1618 for (i
= 0; i
< mlxsw_sp
->sb_vals
->pool_count
; i
++) {
1619 err
= mlxsw_sp_sb_pm_occ_query(mlxsw_sp
, local_port
, i
,
1624 if (++masked_count
== MASKED_COUNT_MAX
)
1629 cb_ctx
.masked_count
= masked_count
;
1630 cb_ctx
.local_port_1
= local_port_1
;
1631 memcpy(&cb_priv
, &cb_ctx
, sizeof(cb_ctx
));
1632 err
= mlxsw_reg_trans_query(mlxsw_core
, MLXSW_REG(sbsr
), sbsr_pl
,
1633 &bulk_list
, mlxsw_sp_sb_sr_occ_query_cb
,
1637 if (local_port
< mlxsw_core_max_ports(mlxsw_core
)) {
1643 err2
= mlxsw_reg_trans_bulk_wait(&bulk_list
);
1650 int mlxsw_sp_sb_occ_max_clear(struct mlxsw_core
*mlxsw_core
,
1651 unsigned int sb_index
)
1653 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
1654 LIST_HEAD(bulk_list
);
1656 unsigned int masked_count
;
1662 sbsr_pl
= kmalloc(MLXSW_REG_SBSR_LEN
, GFP_KERNEL
);
1666 local_port
= MLXSW_PORT_CPU_PORT
;
1669 mlxsw_reg_sbsr_pack(sbsr_pl
, true);
1670 for (i
= 0; i
< MLXSW_SP_SB_ING_TC_COUNT
; i
++)
1671 mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl
, i
, 1);
1672 for (i
= 0; i
< MLXSW_SP_SB_EG_TC_COUNT
; i
++)
1673 mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl
, i
, 1);
1674 for (; local_port
< mlxsw_core_max_ports(mlxsw_core
); local_port
++) {
1675 if (!mlxsw_sp
->ports
[local_port
])
1677 if (local_port
!= MLXSW_PORT_CPU_PORT
) {
1678 /* Ingress quotas are not supported for the CPU port */
1679 mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl
,
1682 mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl
, local_port
, 1);
1683 for (i
= 0; i
< mlxsw_sp
->sb_vals
->pool_count
; i
++) {
1684 err
= mlxsw_sp_sb_pm_occ_clear(mlxsw_sp
, local_port
, i
,
1689 if (++masked_count
== MASKED_COUNT_MAX
)
1694 err
= mlxsw_reg_trans_query(mlxsw_core
, MLXSW_REG(sbsr
), sbsr_pl
,
1695 &bulk_list
, NULL
, 0);
1698 if (local_port
< mlxsw_core_max_ports(mlxsw_core
)) {
1704 err2
= mlxsw_reg_trans_bulk_wait(&bulk_list
);
1711 int mlxsw_sp_sb_occ_port_pool_get(struct mlxsw_core_port
*mlxsw_core_port
,
1712 unsigned int sb_index
, u16 pool_index
,
1713 u32
*p_cur
, u32
*p_max
)
1715 struct mlxsw_sp_port
*mlxsw_sp_port
=
1716 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1717 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1718 u8 local_port
= mlxsw_sp_port
->local_port
;
1719 struct mlxsw_sp_sb_pm
*pm
= mlxsw_sp_sb_pm_get(mlxsw_sp
, local_port
,
1722 *p_cur
= mlxsw_sp_cells_bytes(mlxsw_sp
, pm
->occ
.cur
);
1723 *p_max
= mlxsw_sp_cells_bytes(mlxsw_sp
, pm
->occ
.max
);
1727 int mlxsw_sp_sb_occ_tc_port_bind_get(struct mlxsw_core_port
*mlxsw_core_port
,
1728 unsigned int sb_index
, u16 tc_index
,
1729 enum devlink_sb_pool_type pool_type
,
1730 u32
*p_cur
, u32
*p_max
)
1732 struct mlxsw_sp_port
*mlxsw_sp_port
=
1733 mlxsw_core_port_driver_priv(mlxsw_core_port
);
1734 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1735 u8 local_port
= mlxsw_sp_port
->local_port
;
1736 u8 pg_buff
= tc_index
;
1737 enum mlxsw_reg_sbxx_dir dir
= (enum mlxsw_reg_sbxx_dir
) pool_type
;
1738 struct mlxsw_sp_sb_cm
*cm
= mlxsw_sp_sb_cm_get(mlxsw_sp
, local_port
,
1741 *p_cur
= mlxsw_sp_cells_bytes(mlxsw_sp
, cm
->occ
.cur
);
1742 *p_max
= mlxsw_sp_cells_bytes(mlxsw_sp
, cm
->occ
.max
);