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 u16 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 u16 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_pr_desc_write(struct mlxsw_sp *mlxsw_sp,
206 enum mlxsw_reg_sbxx_dir dir,
207 enum mlxsw_reg_sbpr_mode mode,
208 u32 size, bool infi_size)
210 char sbpr_pl[MLXSW_REG_SBPR_LEN];
212 /* The FW default descriptor buffer configuration uses only pool 14 for
215 mlxsw_reg_sbpr_pack(sbpr_pl, 14, dir, mode, size, infi_size);
216 mlxsw_reg_sbpr_desc_set(sbpr_pl, true);
217 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpr), sbpr_pl);
220 static int mlxsw_sp_sb_cm_write(struct mlxsw_sp *mlxsw_sp, u16 local_port,
221 u8 pg_buff, u32 min_buff, u32 max_buff,
222 bool infi_max, u16 pool_index)
224 const struct mlxsw_sp_sb_pool_des *des =
225 &mlxsw_sp->sb_vals->pool_dess[pool_index];
226 char sbcm_pl[MLXSW_REG_SBCM_LEN];
227 struct mlxsw_sp_sb_cm *cm;
230 mlxsw_reg_sbcm_pack(sbcm_pl, local_port, pg_buff, des->dir,
231 min_buff, max_buff, infi_max, des->pool);
232 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbcm), sbcm_pl);
236 if (mlxsw_sp_sb_cm_exists(pg_buff, des->dir)) {
238 max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
239 mlxsw_sp->sb->sb_size);
241 cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, pg_buff,
243 cm->min_buff = min_buff;
244 cm->max_buff = max_buff;
245 cm->pool_index = pool_index;
250 static int mlxsw_sp_sb_pm_write(struct mlxsw_sp *mlxsw_sp, u16 local_port,
251 u16 pool_index, u32 min_buff, u32 max_buff)
253 const struct mlxsw_sp_sb_pool_des *des =
254 &mlxsw_sp->sb_vals->pool_dess[pool_index];
255 char sbpm_pl[MLXSW_REG_SBPM_LEN];
256 struct mlxsw_sp_sb_pm *pm;
259 mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir, false,
261 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl);
265 pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
266 pm->min_buff = min_buff;
267 pm->max_buff = max_buff;
271 static int mlxsw_sp_sb_pm_occ_clear(struct mlxsw_sp *mlxsw_sp, u16 local_port,
272 u16 pool_index, struct list_head *bulk_list)
274 const struct mlxsw_sp_sb_pool_des *des =
275 &mlxsw_sp->sb_vals->pool_dess[pool_index];
276 char sbpm_pl[MLXSW_REG_SBPM_LEN];
278 if (local_port == MLXSW_PORT_CPU_PORT &&
279 des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
282 mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
284 return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
288 static void mlxsw_sp_sb_pm_occ_query_cb(struct mlxsw_core *mlxsw_core,
289 char *sbpm_pl, size_t sbpm_pl_len,
290 unsigned long cb_priv)
292 struct mlxsw_sp_sb_pm *pm = (struct mlxsw_sp_sb_pm *) cb_priv;
294 mlxsw_reg_sbpm_unpack(sbpm_pl, &pm->occ.cur, &pm->occ.max);
297 static int mlxsw_sp_sb_pm_occ_query(struct mlxsw_sp *mlxsw_sp, u16 local_port,
298 u16 pool_index, struct list_head *bulk_list)
300 const struct mlxsw_sp_sb_pool_des *des =
301 &mlxsw_sp->sb_vals->pool_dess[pool_index];
302 char sbpm_pl[MLXSW_REG_SBPM_LEN];
303 struct mlxsw_sp_sb_pm *pm;
305 if (local_port == MLXSW_PORT_CPU_PORT &&
306 des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
309 pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port, pool_index);
310 mlxsw_reg_sbpm_pack(sbpm_pl, local_port, des->pool, des->dir,
312 return mlxsw_reg_trans_query(mlxsw_sp->core, MLXSW_REG(sbpm), sbpm_pl,
314 mlxsw_sp_sb_pm_occ_query_cb,
318 void mlxsw_sp_hdroom_prios_reset_buf_idx(struct mlxsw_sp_hdroom *hdroom)
322 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
323 switch (hdroom->mode) {
324 case MLXSW_SP_HDROOM_MODE_DCB:
325 hdroom->prios.prio[prio].buf_idx = hdroom->prios.prio[prio].ets_buf_idx;
327 case MLXSW_SP_HDROOM_MODE_TC:
328 hdroom->prios.prio[prio].buf_idx = hdroom->prios.prio[prio].set_buf_idx;
334 void mlxsw_sp_hdroom_bufs_reset_lossiness(struct mlxsw_sp_hdroom *hdroom)
339 for (i = 0; i < DCBX_MAX_BUFFERS; i++)
340 hdroom->bufs.buf[i].lossy = true;
342 for (prio = 0; prio < IEEE_8021Q_MAX_PRIORITIES; prio++) {
343 if (!hdroom->prios.prio[prio].lossy)
344 hdroom->bufs.buf[hdroom->prios.prio[prio].buf_idx].lossy = false;
348 static u16 mlxsw_sp_hdroom_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp, int mtu)
350 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
353 static void mlxsw_sp_hdroom_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres, bool lossy)
356 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
358 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
362 static u16 mlxsw_sp_hdroom_buf_delay_get(const struct mlxsw_sp *mlxsw_sp,
363 const struct mlxsw_sp_hdroom *hdroom)
367 delay_cells = mlxsw_sp_bytes_cells(mlxsw_sp, hdroom->delay_bytes);
369 /* In the worst case scenario the delay will be made up of packets that
370 * are all of size CELL_SIZE + 1, which means each packet will require
371 * almost twice its true size when buffered in the switch. We therefore
372 * multiply this value by the "cell factor", which is close to 2.
374 * Another MTU is added in case the transmitting host already started
375 * transmitting a maximum length frame when the PFC packet was received.
377 return 2 * delay_cells + mlxsw_sp_bytes_cells(mlxsw_sp, hdroom->mtu);
380 static u32 mlxsw_sp_hdroom_int_buf_size_get(struct mlxsw_sp *mlxsw_sp, int mtu, u32 speed)
382 u32 buffsize = mlxsw_sp->sb_ops->int_buf_size_get(mtu, speed);
384 return mlxsw_sp_bytes_cells(mlxsw_sp, buffsize) + 1;
387 static bool mlxsw_sp_hdroom_buf_is_used(const struct mlxsw_sp_hdroom *hdroom, int buf)
391 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
392 if (hdroom->prios.prio[prio].buf_idx == buf)
398 void mlxsw_sp_hdroom_bufs_reset_sizes(struct mlxsw_sp_port *mlxsw_sp_port,
399 struct mlxsw_sp_hdroom *hdroom)
401 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
402 unsigned int max_mtu = mlxsw_sp_port->dev->max_mtu;
406 max_mtu += MLXSW_PORT_ETH_FRAME_HDR;
407 /* Internal buffer. */
408 reserve_cells = mlxsw_sp_hdroom_int_buf_size_get(mlxsw_sp, max_mtu,
409 mlxsw_sp_port->max_speed);
410 reserve_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, reserve_cells);
411 hdroom->int_buf.reserve_cells = reserve_cells;
413 if (hdroom->int_buf.enable)
414 hdroom->int_buf.size_cells = reserve_cells;
416 hdroom->int_buf.size_cells = 0;
419 for (i = 0; i < DCBX_MAX_BUFFERS; i++) {
420 struct mlxsw_sp_hdroom_buf *buf = &hdroom->bufs.buf[i];
424 if (!mlxsw_sp_hdroom_buf_is_used(hdroom, i)) {
427 } else if (buf->lossy) {
428 thres_cells = mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp, hdroom->mtu);
431 thres_cells = mlxsw_sp_hdroom_buf_threshold_get(mlxsw_sp, hdroom->mtu);
432 delay_cells = mlxsw_sp_hdroom_buf_delay_get(mlxsw_sp, hdroom);
435 thres_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, thres_cells);
436 delay_cells = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, delay_cells);
438 buf->thres_cells = thres_cells;
439 if (hdroom->mode == MLXSW_SP_HDROOM_MODE_DCB) {
440 buf->size_cells = thres_cells + delay_cells;
442 /* Do not allow going below the minimum size, even if
443 * the user requested it.
445 buf->size_cells = max(buf->set_size_cells, buf->thres_cells);
450 #define MLXSW_SP_PB_UNUSED 8
452 static int mlxsw_sp_hdroom_configure_buffers(struct mlxsw_sp_port *mlxsw_sp_port,
453 const struct mlxsw_sp_hdroom *hdroom, bool force)
455 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
456 char pbmc_pl[MLXSW_REG_PBMC_LEN];
461 dirty = memcmp(&mlxsw_sp_port->hdroom->bufs, &hdroom->bufs, sizeof(hdroom->bufs));
462 if (!dirty && !force)
465 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0xffff, 0xffff / 2);
466 for (i = 0; i < MLXSW_SP_PB_COUNT; i++) {
467 const struct mlxsw_sp_hdroom_buf *buf = &hdroom->bufs.buf[i];
469 if (i == MLXSW_SP_PB_UNUSED)
472 mlxsw_sp_hdroom_buf_pack(pbmc_pl, i, buf->size_cells, buf->thres_cells, buf->lossy);
475 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, MLXSW_REG_PBMC_PORT_SHARED_BUF_IDX, 0);
476 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
480 mlxsw_sp_port->hdroom->bufs = hdroom->bufs;
484 static int mlxsw_sp_hdroom_configure_priomap(struct mlxsw_sp_port *mlxsw_sp_port,
485 const struct mlxsw_sp_hdroom *hdroom, bool force)
487 char pptb_pl[MLXSW_REG_PPTB_LEN];
492 dirty = memcmp(&mlxsw_sp_port->hdroom->prios, &hdroom->prios, sizeof(hdroom->prios));
493 if (!dirty && !force)
496 mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
497 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
498 mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl, prio, hdroom->prios.prio[prio].buf_idx);
500 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb), pptb_pl);
504 mlxsw_sp_port->hdroom->prios = hdroom->prios;
508 static int mlxsw_sp_hdroom_configure_int_buf(struct mlxsw_sp_port *mlxsw_sp_port,
509 const struct mlxsw_sp_hdroom *hdroom, bool force)
511 char sbib_pl[MLXSW_REG_SBIB_LEN];
515 dirty = memcmp(&mlxsw_sp_port->hdroom->int_buf, &hdroom->int_buf, sizeof(hdroom->int_buf));
516 if (!dirty && !force)
519 mlxsw_reg_sbib_pack(sbib_pl, mlxsw_sp_port->local_port, hdroom->int_buf.size_cells);
520 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
524 mlxsw_sp_port->hdroom->int_buf = hdroom->int_buf;
528 static bool mlxsw_sp_hdroom_bufs_fit(struct mlxsw_sp *mlxsw_sp,
529 const struct mlxsw_sp_hdroom *hdroom)
531 u32 taken_headroom_cells = 0;
534 for (i = 0; i < MLXSW_SP_PB_COUNT; i++)
535 taken_headroom_cells += hdroom->bufs.buf[i].size_cells;
537 taken_headroom_cells += hdroom->int_buf.reserve_cells;
538 return taken_headroom_cells <= mlxsw_sp->sb->max_headroom_cells;
541 static int __mlxsw_sp_hdroom_configure(struct mlxsw_sp_port *mlxsw_sp_port,
542 const struct mlxsw_sp_hdroom *hdroom, bool force)
544 struct mlxsw_sp_hdroom orig_hdroom;
545 struct mlxsw_sp_hdroom tmp_hdroom;
549 /* Port buffers need to be configured in three steps. First, all buffers
550 * with non-zero size are configured. Then, prio-to-buffer map is
551 * updated, allowing traffic to flow to the now non-zero buffers.
552 * Finally, zero-sized buffers are configured, because now no traffic
553 * should be directed to them anymore. This way, in a non-congested
554 * system, no packet drops are introduced by the reconfiguration.
557 orig_hdroom = *mlxsw_sp_port->hdroom;
558 tmp_hdroom = orig_hdroom;
559 for (i = 0; i < MLXSW_SP_PB_COUNT; i++) {
560 if (hdroom->bufs.buf[i].size_cells)
561 tmp_hdroom.bufs.buf[i] = hdroom->bufs.buf[i];
564 if (!mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port->mlxsw_sp, &tmp_hdroom) ||
565 !mlxsw_sp_hdroom_bufs_fit(mlxsw_sp_port->mlxsw_sp, hdroom))
568 err = mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &tmp_hdroom, force);
572 err = mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port, hdroom, force);
574 goto err_configure_priomap;
576 err = mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, hdroom, false);
578 goto err_configure_buffers;
580 err = mlxsw_sp_hdroom_configure_int_buf(mlxsw_sp_port, hdroom, false);
582 goto err_configure_int_buf;
584 *mlxsw_sp_port->hdroom = *hdroom;
587 err_configure_int_buf:
588 mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &tmp_hdroom, false);
589 err_configure_buffers:
590 mlxsw_sp_hdroom_configure_priomap(mlxsw_sp_port, &tmp_hdroom, false);
591 err_configure_priomap:
592 mlxsw_sp_hdroom_configure_buffers(mlxsw_sp_port, &orig_hdroom, false);
596 int mlxsw_sp_hdroom_configure(struct mlxsw_sp_port *mlxsw_sp_port,
597 const struct mlxsw_sp_hdroom *hdroom)
599 return __mlxsw_sp_hdroom_configure(mlxsw_sp_port, hdroom, false);
602 static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
604 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
605 struct mlxsw_sp_hdroom hdroom = {};
609 hdroom.mtu = mlxsw_sp_port->dev->mtu;
610 hdroom.mode = MLXSW_SP_HDROOM_MODE_DCB;
611 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
612 hdroom.prios.prio[prio].lossy = true;
614 mlxsw_sp_hdroom_bufs_reset_lossiness(&hdroom);
615 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
617 /* Buffer 9 is used for control traffic. */
618 size9 = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port,
619 mlxsw_sp_port->dev->max_mtu +
620 MLXSW_PORT_ETH_FRAME_HDR);
621 hdroom.bufs.buf[9].size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size9);
623 return __mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom, true);
626 static int mlxsw_sp_sb_port_init(struct mlxsw_sp *mlxsw_sp,
627 struct mlxsw_sp_sb_port *sb_port)
629 struct mlxsw_sp_sb_pm *pms;
631 pms = kcalloc(mlxsw_sp->sb_vals->pool_count, sizeof(*pms),
639 static void mlxsw_sp_sb_port_fini(struct mlxsw_sp_sb_port *sb_port)
644 static int mlxsw_sp_sb_ports_init(struct mlxsw_sp *mlxsw_sp)
646 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
647 struct mlxsw_sp_sb_pr *prs;
651 mlxsw_sp->sb->ports = kcalloc(max_ports,
652 sizeof(struct mlxsw_sp_sb_port),
654 if (!mlxsw_sp->sb->ports)
657 prs = kcalloc(mlxsw_sp->sb_vals->pool_count, sizeof(*prs),
663 mlxsw_sp->sb->prs = prs;
665 for (i = 0; i < max_ports; i++) {
666 err = mlxsw_sp_sb_port_init(mlxsw_sp, &mlxsw_sp->sb->ports[i]);
668 goto err_sb_port_init;
674 for (i--; i >= 0; i--)
675 mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
676 kfree(mlxsw_sp->sb->prs);
678 kfree(mlxsw_sp->sb->ports);
682 static void mlxsw_sp_sb_ports_fini(struct mlxsw_sp *mlxsw_sp)
684 int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
687 for (i = max_ports - 1; i >= 0; i--)
688 mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
689 kfree(mlxsw_sp->sb->prs);
690 kfree(mlxsw_sp->sb->ports);
693 #define MLXSW_SP_SB_PR(_mode, _size) \
699 #define MLXSW_SP_SB_PR_EXT(_mode, _size, _freeze_mode, _freeze_size) \
703 .freeze_mode = _freeze_mode, \
704 .freeze_size = _freeze_size, \
707 #define MLXSW_SP1_SB_PR_CPU_SIZE (256 * 1000)
709 /* Order according to mlxsw_sp1_sb_pool_dess */
710 static const struct mlxsw_sp_sb_pr mlxsw_sp1_sb_prs[] = {
711 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST),
712 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
713 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
714 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
715 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST,
717 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
718 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
719 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, 0),
720 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC, MLXSW_SP_SB_INFI,
722 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
723 MLXSW_SP1_SB_PR_CPU_SIZE, true, false),
724 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
725 MLXSW_SP1_SB_PR_CPU_SIZE, true, false),
728 #define MLXSW_SP2_SB_PR_CPU_SIZE (256 * 1000)
730 /* Order according to mlxsw_sp2_sb_pool_dess */
731 static const struct mlxsw_sp_sb_pr mlxsw_sp2_sb_prs[] = {
732 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST),
733 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
734 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
735 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
736 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC, MLXSW_SP_SB_REST,
738 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
739 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
740 MLXSW_SP_SB_PR(MLXSW_REG_SBPR_MODE_STATIC, 0),
741 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_STATIC, MLXSW_SP_SB_INFI,
743 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
744 MLXSW_SP2_SB_PR_CPU_SIZE, true, false),
745 MLXSW_SP_SB_PR_EXT(MLXSW_REG_SBPR_MODE_DYNAMIC,
746 MLXSW_SP2_SB_PR_CPU_SIZE, true, false),
749 static int mlxsw_sp_sb_prs_init(struct mlxsw_sp *mlxsw_sp,
750 const struct mlxsw_sp_sb_pr *prs,
751 const struct mlxsw_sp_sb_pool_des *pool_dess,
754 /* Round down, unlike mlxsw_sp_bytes_cells(). */
755 u32 sb_cells = div_u64(mlxsw_sp->sb->sb_size, mlxsw_sp->sb->cell_size);
756 u32 rest_cells[2] = {sb_cells, sb_cells};
760 /* Calculate how much space to give to the "REST" pools in either
763 for (i = 0; i < prs_len; i++) {
764 enum mlxsw_reg_sbxx_dir dir = pool_dess[i].dir;
765 u32 size = prs[i].size;
768 if (size == MLXSW_SP_SB_INFI || size == MLXSW_SP_SB_REST)
771 size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size);
772 if (WARN_ON_ONCE(size_cells > rest_cells[dir]))
775 rest_cells[dir] -= size_cells;
778 for (i = 0; i < prs_len; i++) {
779 u32 size = prs[i].size;
782 if (size == MLXSW_SP_SB_INFI) {
783 err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
785 } else if (size == MLXSW_SP_SB_REST) {
786 size_cells = rest_cells[pool_dess[i].dir];
787 err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
790 size_cells = mlxsw_sp_bytes_cells(mlxsw_sp, size);
791 err = mlxsw_sp_sb_pr_write(mlxsw_sp, i, prs[i].mode,
798 err = mlxsw_sp_sb_pr_desc_write(mlxsw_sp, MLXSW_REG_SBXX_DIR_INGRESS,
799 MLXSW_REG_SBPR_MODE_DYNAMIC, 0, true);
803 err = mlxsw_sp_sb_pr_desc_write(mlxsw_sp, MLXSW_REG_SBXX_DIR_EGRESS,
804 MLXSW_REG_SBPR_MODE_DYNAMIC, 0, true);
811 #define MLXSW_SP_SB_CM(_min_buff, _max_buff, _pool) \
813 .min_buff = _min_buff, \
814 .max_buff = _max_buff, \
815 .pool_index = _pool, \
818 #define MLXSW_SP_SB_CM_ING(_min_buff, _max_buff) \
820 .min_buff = _min_buff, \
821 .max_buff = _max_buff, \
822 .pool_index = MLXSW_SP_SB_POOL_ING, \
825 #define MLXSW_SP_SB_CM_EGR(_min_buff, _max_buff) \
827 .min_buff = _min_buff, \
828 .max_buff = _max_buff, \
829 .pool_index = MLXSW_SP_SB_POOL_EGR, \
832 #define MLXSW_SP_SB_CM_EGR_MC(_min_buff, _max_buff) \
834 .min_buff = _min_buff, \
835 .max_buff = _max_buff, \
836 .pool_index = MLXSW_SP_SB_POOL_EGR_MC, \
837 .freeze_pool = true, \
838 .freeze_thresh = true, \
841 static const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_ingress[] = {
842 MLXSW_SP_SB_CM_ING(10000, 8),
843 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
844 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
845 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
846 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
847 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
848 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
849 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
850 MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
851 MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU),
854 static const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_ingress[] = {
855 MLXSW_SP_SB_CM_ING(0, 7),
856 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
857 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
858 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
859 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
860 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
861 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
862 MLXSW_SP_SB_CM_ING(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
863 MLXSW_SP_SB_CM_ING(0, 0), /* dummy, this PG does not exist */
864 MLXSW_SP_SB_CM(10000, 8, MLXSW_SP_SB_POOL_ING_CPU),
867 static const struct mlxsw_sp_sb_cm mlxsw_sp1_sb_cms_egress[] = {
868 MLXSW_SP_SB_CM_EGR(1500, 9),
869 MLXSW_SP_SB_CM_EGR(1500, 9),
870 MLXSW_SP_SB_CM_EGR(1500, 9),
871 MLXSW_SP_SB_CM_EGR(1500, 9),
872 MLXSW_SP_SB_CM_EGR(1500, 9),
873 MLXSW_SP_SB_CM_EGR(1500, 9),
874 MLXSW_SP_SB_CM_EGR(1500, 9),
875 MLXSW_SP_SB_CM_EGR(1500, 9),
876 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
877 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
878 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
879 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
880 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
881 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
882 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
883 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
884 MLXSW_SP_SB_CM_EGR(1, 0xff),
887 static const struct mlxsw_sp_sb_cm mlxsw_sp2_sb_cms_egress[] = {
888 MLXSW_SP_SB_CM_EGR(0, 7),
889 MLXSW_SP_SB_CM_EGR(0, 7),
890 MLXSW_SP_SB_CM_EGR(0, 7),
891 MLXSW_SP_SB_CM_EGR(0, 7),
892 MLXSW_SP_SB_CM_EGR(0, 7),
893 MLXSW_SP_SB_CM_EGR(0, 7),
894 MLXSW_SP_SB_CM_EGR(0, 7),
895 MLXSW_SP_SB_CM_EGR(0, 7),
896 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
897 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
898 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
899 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
900 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
901 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
902 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
903 MLXSW_SP_SB_CM_EGR_MC(0, MLXSW_SP_SB_INFI),
904 MLXSW_SP_SB_CM_EGR(1, 0xff),
907 #define MLXSW_SP_CPU_PORT_SB_CM MLXSW_SP_SB_CM(0, 0, MLXSW_SP_SB_POOL_EGR_CPU)
909 static const struct mlxsw_sp_sb_cm mlxsw_sp_cpu_port_sb_cms[] = {
910 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
911 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
912 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
913 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
914 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
915 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
916 MLXSW_SP_CPU_PORT_SB_CM,
917 MLXSW_SP_SB_CM(1000, 8, MLXSW_SP_SB_POOL_EGR_CPU),
918 MLXSW_SP_CPU_PORT_SB_CM,
919 MLXSW_SP_CPU_PORT_SB_CM,
920 MLXSW_SP_CPU_PORT_SB_CM,
921 MLXSW_SP_CPU_PORT_SB_CM,
922 MLXSW_SP_CPU_PORT_SB_CM,
923 MLXSW_SP_CPU_PORT_SB_CM,
924 MLXSW_SP_CPU_PORT_SB_CM,
925 MLXSW_SP_CPU_PORT_SB_CM,
926 MLXSW_SP_CPU_PORT_SB_CM,
927 MLXSW_SP_CPU_PORT_SB_CM,
928 MLXSW_SP_CPU_PORT_SB_CM,
929 MLXSW_SP_CPU_PORT_SB_CM,
930 MLXSW_SP_CPU_PORT_SB_CM,
931 MLXSW_SP_CPU_PORT_SB_CM,
932 MLXSW_SP_CPU_PORT_SB_CM,
933 MLXSW_SP_CPU_PORT_SB_CM,
934 MLXSW_SP_CPU_PORT_SB_CM,
935 MLXSW_SP_CPU_PORT_SB_CM,
936 MLXSW_SP_CPU_PORT_SB_CM,
937 MLXSW_SP_CPU_PORT_SB_CM,
938 MLXSW_SP_CPU_PORT_SB_CM,
939 MLXSW_SP_CPU_PORT_SB_CM,
940 MLXSW_SP_CPU_PORT_SB_CM,
941 MLXSW_SP_CPU_PORT_SB_CM,
945 mlxsw_sp_sb_pool_is_static(struct mlxsw_sp *mlxsw_sp, u16 pool_index)
947 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
949 return pr->mode == MLXSW_REG_SBPR_MODE_STATIC;
952 static int __mlxsw_sp_sb_cms_init(struct mlxsw_sp *mlxsw_sp, u16 local_port,
953 enum mlxsw_reg_sbxx_dir dir,
954 const struct mlxsw_sp_sb_cm *cms,
957 const struct mlxsw_sp_sb_vals *sb_vals = mlxsw_sp->sb_vals;
961 for (i = 0; i < cms_len; i++) {
962 const struct mlxsw_sp_sb_cm *cm;
966 if (i == 8 && dir == MLXSW_REG_SBXX_DIR_INGRESS)
967 continue; /* PG number 8 does not exist, skip it */
969 if (WARN_ON(sb_vals->pool_dess[cm->pool_index].dir != dir))
972 min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, cm->min_buff);
973 max_buff = cm->max_buff;
974 if (max_buff == MLXSW_SP_SB_INFI) {
975 err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
977 true, cm->pool_index);
979 if (mlxsw_sp_sb_pool_is_static(mlxsw_sp,
981 max_buff = mlxsw_sp_bytes_cells(mlxsw_sp,
983 err = mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, i,
985 false, cm->pool_index);
993 static int mlxsw_sp_port_sb_cms_init(struct mlxsw_sp_port *mlxsw_sp_port)
995 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
998 err = __mlxsw_sp_sb_cms_init(mlxsw_sp,
999 mlxsw_sp_port->local_port,
1000 MLXSW_REG_SBXX_DIR_INGRESS,
1001 mlxsw_sp->sb_vals->cms_ingress,
1002 mlxsw_sp->sb_vals->cms_ingress_count);
1005 return __mlxsw_sp_sb_cms_init(mlxsw_sp_port->mlxsw_sp,
1006 mlxsw_sp_port->local_port,
1007 MLXSW_REG_SBXX_DIR_EGRESS,
1008 mlxsw_sp->sb_vals->cms_egress,
1009 mlxsw_sp->sb_vals->cms_egress_count);
1012 static int mlxsw_sp_cpu_port_sb_cms_init(struct mlxsw_sp *mlxsw_sp)
1014 return __mlxsw_sp_sb_cms_init(mlxsw_sp, 0, MLXSW_REG_SBXX_DIR_EGRESS,
1015 mlxsw_sp->sb_vals->cms_cpu,
1016 mlxsw_sp->sb_vals->cms_cpu_count);
1019 #define MLXSW_SP_SB_PM(_min_buff, _max_buff) \
1021 .min_buff = _min_buff, \
1022 .max_buff = _max_buff, \
1025 /* Order according to mlxsw_sp1_sb_pool_dess */
1026 static const struct mlxsw_sp_sb_pm mlxsw_sp1_sb_pms[] = {
1027 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
1028 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1029 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1030 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1031 MLXSW_SP_SB_PM(0, 7),
1032 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1033 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1034 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1035 MLXSW_SP_SB_PM(10000, 90000),
1036 MLXSW_SP_SB_PM(0, 8), /* 50% occupancy */
1037 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1040 /* Order according to mlxsw_sp2_sb_pool_dess */
1041 static const struct mlxsw_sp_sb_pm mlxsw_sp2_sb_pms[] = {
1042 MLXSW_SP_SB_PM(0, 7),
1043 MLXSW_SP_SB_PM(0, 0),
1044 MLXSW_SP_SB_PM(0, 0),
1045 MLXSW_SP_SB_PM(0, 0),
1046 MLXSW_SP_SB_PM(0, 7),
1047 MLXSW_SP_SB_PM(0, 0),
1048 MLXSW_SP_SB_PM(0, 0),
1049 MLXSW_SP_SB_PM(0, 0),
1050 MLXSW_SP_SB_PM(10000, 90000),
1051 MLXSW_SP_SB_PM(0, 8), /* 50% occupancy */
1052 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN),
1055 /* Order according to mlxsw_sp*_sb_pool_dess */
1056 static const struct mlxsw_sp_sb_pm mlxsw_sp_cpu_port_sb_pms[] = {
1057 MLXSW_SP_SB_PM(0, 0),
1058 MLXSW_SP_SB_PM(0, 0),
1059 MLXSW_SP_SB_PM(0, 0),
1060 MLXSW_SP_SB_PM(0, 0),
1061 MLXSW_SP_SB_PM(0, 0),
1062 MLXSW_SP_SB_PM(0, 0),
1063 MLXSW_SP_SB_PM(0, 0),
1064 MLXSW_SP_SB_PM(0, 0),
1065 MLXSW_SP_SB_PM(0, 90000),
1066 MLXSW_SP_SB_PM(0, 0),
1067 MLXSW_SP_SB_PM(0, MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX),
1070 static int mlxsw_sp_sb_pms_init(struct mlxsw_sp *mlxsw_sp, u16 local_port,
1071 const struct mlxsw_sp_sb_pm *pms,
1076 for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
1077 const struct mlxsw_sp_sb_pm *pm = &pms[i];
1078 const struct mlxsw_sp_sb_pool_des *des;
1082 des = &mlxsw_sp->sb_vals->pool_dess[i];
1083 if (skip_ingress && des->dir == MLXSW_REG_SBXX_DIR_INGRESS)
1086 min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, pm->min_buff);
1087 max_buff = pm->max_buff;
1088 if (mlxsw_sp_sb_pool_is_static(mlxsw_sp, i))
1089 max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, max_buff);
1090 err = mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, i, min_buff,
1098 static int mlxsw_sp_port_sb_pms_init(struct mlxsw_sp_port *mlxsw_sp_port)
1100 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1102 return mlxsw_sp_sb_pms_init(mlxsw_sp, mlxsw_sp_port->local_port,
1103 mlxsw_sp->sb_vals->pms, false);
1106 static int mlxsw_sp_cpu_port_sb_pms_init(struct mlxsw_sp *mlxsw_sp)
1108 return mlxsw_sp_sb_pms_init(mlxsw_sp, 0, mlxsw_sp->sb_vals->pms_cpu,
1112 #define MLXSW_SP_SB_MM(_min_buff, _max_buff) \
1114 .min_buff = _min_buff, \
1115 .max_buff = _max_buff, \
1116 .pool_index = MLXSW_SP_SB_POOL_EGR, \
1119 static const struct mlxsw_sp_sb_mm mlxsw_sp_sb_mms[] = {
1120 MLXSW_SP_SB_MM(0, 6),
1121 MLXSW_SP_SB_MM(0, 6),
1122 MLXSW_SP_SB_MM(0, 6),
1123 MLXSW_SP_SB_MM(0, 6),
1124 MLXSW_SP_SB_MM(0, 6),
1125 MLXSW_SP_SB_MM(0, 6),
1126 MLXSW_SP_SB_MM(0, 6),
1127 MLXSW_SP_SB_MM(0, 6),
1128 MLXSW_SP_SB_MM(0, 6),
1129 MLXSW_SP_SB_MM(0, 6),
1130 MLXSW_SP_SB_MM(0, 6),
1131 MLXSW_SP_SB_MM(0, 6),
1132 MLXSW_SP_SB_MM(0, 6),
1133 MLXSW_SP_SB_MM(0, 6),
1134 MLXSW_SP_SB_MM(0, 6),
1137 static int mlxsw_sp_sb_mms_init(struct mlxsw_sp *mlxsw_sp)
1139 char sbmm_pl[MLXSW_REG_SBMM_LEN];
1143 for (i = 0; i < mlxsw_sp->sb_vals->mms_count; i++) {
1144 const struct mlxsw_sp_sb_pool_des *des;
1145 const struct mlxsw_sp_sb_mm *mc;
1148 mc = &mlxsw_sp->sb_vals->mms[i];
1149 des = &mlxsw_sp->sb_vals->pool_dess[mc->pool_index];
1150 /* All pools used by sb_mm's are initialized using dynamic
1151 * thresholds, therefore 'max_buff' isn't specified in cells.
1153 min_buff = mlxsw_sp_bytes_cells(mlxsw_sp, mc->min_buff);
1154 mlxsw_reg_sbmm_pack(sbmm_pl, i, min_buff, mc->max_buff,
1156 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbmm), sbmm_pl);
1163 static void mlxsw_sp_pool_count(struct mlxsw_sp *mlxsw_sp,
1164 u16 *p_ingress_len, u16 *p_egress_len)
1168 for (i = 0; i < mlxsw_sp->sb_vals->pool_count; ++i) {
1169 if (mlxsw_sp->sb_vals->pool_dess[i].dir ==
1170 MLXSW_REG_SBXX_DIR_INGRESS)
1176 WARN(*p_egress_len == 0, "No egress pools\n");
1179 const struct mlxsw_sp_sb_vals mlxsw_sp1_sb_vals = {
1180 .pool_count = ARRAY_SIZE(mlxsw_sp1_sb_pool_dess),
1181 .pool_dess = mlxsw_sp1_sb_pool_dess,
1182 .pms = mlxsw_sp1_sb_pms,
1183 .pms_cpu = mlxsw_sp_cpu_port_sb_pms,
1184 .prs = mlxsw_sp1_sb_prs,
1185 .mms = mlxsw_sp_sb_mms,
1186 .cms_ingress = mlxsw_sp1_sb_cms_ingress,
1187 .cms_egress = mlxsw_sp1_sb_cms_egress,
1188 .cms_cpu = mlxsw_sp_cpu_port_sb_cms,
1189 .mms_count = ARRAY_SIZE(mlxsw_sp_sb_mms),
1190 .cms_ingress_count = ARRAY_SIZE(mlxsw_sp1_sb_cms_ingress),
1191 .cms_egress_count = ARRAY_SIZE(mlxsw_sp1_sb_cms_egress),
1192 .cms_cpu_count = ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms),
1195 const struct mlxsw_sp_sb_vals mlxsw_sp2_sb_vals = {
1196 .pool_count = ARRAY_SIZE(mlxsw_sp2_sb_pool_dess),
1197 .pool_dess = mlxsw_sp2_sb_pool_dess,
1198 .pms = mlxsw_sp2_sb_pms,
1199 .pms_cpu = mlxsw_sp_cpu_port_sb_pms,
1200 .prs = mlxsw_sp2_sb_prs,
1201 .mms = mlxsw_sp_sb_mms,
1202 .cms_ingress = mlxsw_sp2_sb_cms_ingress,
1203 .cms_egress = mlxsw_sp2_sb_cms_egress,
1204 .cms_cpu = mlxsw_sp_cpu_port_sb_cms,
1205 .mms_count = ARRAY_SIZE(mlxsw_sp_sb_mms),
1206 .cms_ingress_count = ARRAY_SIZE(mlxsw_sp2_sb_cms_ingress),
1207 .cms_egress_count = ARRAY_SIZE(mlxsw_sp2_sb_cms_egress),
1208 .cms_cpu_count = ARRAY_SIZE(mlxsw_sp_cpu_port_sb_cms),
1211 static u32 mlxsw_sp1_pb_int_buf_size_get(int mtu, u32 speed)
1216 static u32 __mlxsw_sp_pb_int_buf_size_get(int mtu, u32 speed, u32 buffer_factor)
1218 return 3 * mtu + buffer_factor * speed / 1000;
1221 #define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38
1223 static u32 mlxsw_sp2_pb_int_buf_size_get(int mtu, u32 speed)
1225 int factor = MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR;
1227 return __mlxsw_sp_pb_int_buf_size_get(mtu, speed, factor);
1230 #define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50
1232 static u32 mlxsw_sp3_pb_int_buf_size_get(int mtu, u32 speed)
1234 int factor = MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR;
1236 return __mlxsw_sp_pb_int_buf_size_get(mtu, speed, factor);
1239 const struct mlxsw_sp_sb_ops mlxsw_sp1_sb_ops = {
1240 .int_buf_size_get = mlxsw_sp1_pb_int_buf_size_get,
1243 const struct mlxsw_sp_sb_ops mlxsw_sp2_sb_ops = {
1244 .int_buf_size_get = mlxsw_sp2_pb_int_buf_size_get,
1247 const struct mlxsw_sp_sb_ops mlxsw_sp3_sb_ops = {
1248 .int_buf_size_get = mlxsw_sp3_pb_int_buf_size_get,
1251 int mlxsw_sp_buffers_init(struct mlxsw_sp *mlxsw_sp)
1253 u32 max_headroom_size;
1254 u16 ing_pool_count = 0;
1255 u16 eg_pool_count = 0;
1258 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, CELL_SIZE))
1261 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, GUARANTEED_SHARED_BUFFER))
1264 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_HEADROOM_SIZE))
1267 mlxsw_sp->sb = kzalloc(sizeof(*mlxsw_sp->sb), GFP_KERNEL);
1270 mlxsw_sp->sb->cell_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, CELL_SIZE);
1271 mlxsw_sp->sb->sb_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1272 GUARANTEED_SHARED_BUFFER);
1273 max_headroom_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1275 /* Round down, because this limit must not be overstepped. */
1276 mlxsw_sp->sb->max_headroom_cells = max_headroom_size /
1277 mlxsw_sp->sb->cell_size;
1279 err = mlxsw_sp_sb_ports_init(mlxsw_sp);
1281 goto err_sb_ports_init;
1282 err = mlxsw_sp_sb_prs_init(mlxsw_sp, mlxsw_sp->sb_vals->prs,
1283 mlxsw_sp->sb_vals->pool_dess,
1284 mlxsw_sp->sb_vals->pool_count);
1286 goto err_sb_prs_init;
1287 err = mlxsw_sp_cpu_port_sb_cms_init(mlxsw_sp);
1289 goto err_sb_cpu_port_sb_cms_init;
1290 err = mlxsw_sp_cpu_port_sb_pms_init(mlxsw_sp);
1292 goto err_sb_cpu_port_pms_init;
1293 err = mlxsw_sp_sb_mms_init(mlxsw_sp);
1295 goto err_sb_mms_init;
1296 mlxsw_sp_pool_count(mlxsw_sp, &ing_pool_count, &eg_pool_count);
1297 err = devl_sb_register(priv_to_devlink(mlxsw_sp->core), 0,
1298 mlxsw_sp->sb->sb_size,
1301 MLXSW_SP_SB_ING_TC_COUNT,
1302 MLXSW_SP_SB_EG_TC_COUNT);
1304 goto err_devlink_sb_register;
1308 err_devlink_sb_register:
1310 err_sb_cpu_port_pms_init:
1311 err_sb_cpu_port_sb_cms_init:
1313 mlxsw_sp_sb_ports_fini(mlxsw_sp);
1315 kfree(mlxsw_sp->sb);
1319 void mlxsw_sp_buffers_fini(struct mlxsw_sp *mlxsw_sp)
1321 devl_sb_unregister(priv_to_devlink(mlxsw_sp->core), 0);
1322 mlxsw_sp_sb_ports_fini(mlxsw_sp);
1323 kfree(mlxsw_sp->sb);
1326 int mlxsw_sp_port_buffers_init(struct mlxsw_sp_port *mlxsw_sp_port)
1330 mlxsw_sp_port->hdroom = kzalloc(sizeof(*mlxsw_sp_port->hdroom), GFP_KERNEL);
1331 if (!mlxsw_sp_port->hdroom)
1333 mlxsw_sp_port->hdroom->mtu = mlxsw_sp_port->dev->mtu;
1335 err = mlxsw_sp_port_headroom_init(mlxsw_sp_port);
1337 goto err_headroom_init;
1338 err = mlxsw_sp_port_sb_cms_init(mlxsw_sp_port);
1340 goto err_port_sb_cms_init;
1341 err = mlxsw_sp_port_sb_pms_init(mlxsw_sp_port);
1343 goto err_port_sb_pms_init;
1346 err_port_sb_pms_init:
1347 err_port_sb_cms_init:
1349 kfree(mlxsw_sp_port->hdroom);
1353 void mlxsw_sp_port_buffers_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1355 kfree(mlxsw_sp_port->hdroom);
1358 int mlxsw_sp_sb_pool_get(struct mlxsw_core *mlxsw_core,
1359 unsigned int sb_index, u16 pool_index,
1360 struct devlink_sb_pool_info *pool_info)
1362 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1363 enum mlxsw_reg_sbxx_dir dir;
1364 struct mlxsw_sp_sb_pr *pr;
1366 dir = mlxsw_sp->sb_vals->pool_dess[pool_index].dir;
1367 pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
1368 pool_info->pool_type = (enum devlink_sb_pool_type) dir;
1369 pool_info->size = mlxsw_sp_cells_bytes(mlxsw_sp, pr->size);
1370 pool_info->threshold_type = (enum devlink_sb_threshold_type) pr->mode;
1371 pool_info->cell_size = mlxsw_sp->sb->cell_size;
1375 int mlxsw_sp_sb_pool_set(struct mlxsw_core *mlxsw_core,
1376 unsigned int sb_index, u16 pool_index, u32 size,
1377 enum devlink_sb_threshold_type threshold_type,
1378 struct netlink_ext_ack *extack)
1380 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1381 u32 pool_size = mlxsw_sp_bytes_cells(mlxsw_sp, size);
1382 const struct mlxsw_sp_sb_pr *pr;
1383 enum mlxsw_reg_sbpr_mode mode;
1385 mode = (enum mlxsw_reg_sbpr_mode) threshold_type;
1386 pr = &mlxsw_sp->sb_vals->prs[pool_index];
1388 if (size > MLXSW_CORE_RES_GET(mlxsw_sp->core,
1389 GUARANTEED_SHARED_BUFFER)) {
1390 NL_SET_ERR_MSG_MOD(extack, "Exceeded shared buffer size");
1394 if (pr->freeze_mode && pr->mode != mode) {
1395 NL_SET_ERR_MSG_MOD(extack, "Changing this pool's threshold type is forbidden");
1399 if (pr->freeze_size && pr->size != size) {
1400 NL_SET_ERR_MSG_MOD(extack, "Changing this pool's size is forbidden");
1404 return mlxsw_sp_sb_pr_write(mlxsw_sp, pool_index, mode,
1408 #define MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET (-2) /* 3->1, 16->14 */
1410 static u32 mlxsw_sp_sb_threshold_out(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
1413 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
1415 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC)
1416 return max_buff - MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
1417 return mlxsw_sp_cells_bytes(mlxsw_sp, max_buff);
1420 static int mlxsw_sp_sb_threshold_in(struct mlxsw_sp *mlxsw_sp, u16 pool_index,
1421 u32 threshold, u32 *p_max_buff,
1422 struct netlink_ext_ack *extack)
1424 struct mlxsw_sp_sb_pr *pr = mlxsw_sp_sb_pr_get(mlxsw_sp, pool_index);
1426 if (pr->mode == MLXSW_REG_SBPR_MODE_DYNAMIC) {
1429 val = threshold + MLXSW_SP_SB_THRESHOLD_TO_ALPHA_OFFSET;
1430 if (val < MLXSW_REG_SBXX_DYN_MAX_BUFF_MIN ||
1431 val > MLXSW_REG_SBXX_DYN_MAX_BUFF_MAX) {
1432 NL_SET_ERR_MSG_MOD(extack, "Invalid dynamic threshold value");
1437 *p_max_buff = mlxsw_sp_bytes_cells(mlxsw_sp, threshold);
1442 int mlxsw_sp_sb_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
1443 unsigned int sb_index, u16 pool_index,
1446 struct mlxsw_sp_port *mlxsw_sp_port =
1447 mlxsw_core_port_driver_priv(mlxsw_core_port);
1448 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1449 u16 local_port = mlxsw_sp_port->local_port;
1450 struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
1453 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, pool_index,
1458 int mlxsw_sp_sb_port_pool_set(struct mlxsw_core_port *mlxsw_core_port,
1459 unsigned int sb_index, u16 pool_index,
1460 u32 threshold, struct netlink_ext_ack *extack)
1462 struct mlxsw_sp_port *mlxsw_sp_port =
1463 mlxsw_core_port_driver_priv(mlxsw_core_port);
1464 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1465 u16 local_port = mlxsw_sp_port->local_port;
1469 if (local_port == MLXSW_PORT_CPU_PORT) {
1470 NL_SET_ERR_MSG_MOD(extack, "Changing CPU port's threshold is forbidden");
1474 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
1475 threshold, &max_buff, extack);
1479 return mlxsw_sp_sb_pm_write(mlxsw_sp, local_port, pool_index,
1483 int mlxsw_sp_sb_tc_pool_bind_get(struct mlxsw_core_port *mlxsw_core_port,
1484 unsigned int sb_index, u16 tc_index,
1485 enum devlink_sb_pool_type pool_type,
1486 u16 *p_pool_index, u32 *p_threshold)
1488 struct mlxsw_sp_port *mlxsw_sp_port =
1489 mlxsw_core_port_driver_priv(mlxsw_core_port);
1490 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1491 u16 local_port = mlxsw_sp_port->local_port;
1492 u8 pg_buff = tc_index;
1493 enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
1494 struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
1497 *p_threshold = mlxsw_sp_sb_threshold_out(mlxsw_sp, cm->pool_index,
1499 *p_pool_index = cm->pool_index;
1503 int mlxsw_sp_sb_tc_pool_bind_set(struct mlxsw_core_port *mlxsw_core_port,
1504 unsigned int sb_index, u16 tc_index,
1505 enum devlink_sb_pool_type pool_type,
1506 u16 pool_index, u32 threshold,
1507 struct netlink_ext_ack *extack)
1509 struct mlxsw_sp_port *mlxsw_sp_port =
1510 mlxsw_core_port_driver_priv(mlxsw_core_port);
1511 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1512 u16 local_port = mlxsw_sp_port->local_port;
1513 const struct mlxsw_sp_sb_cm *cm;
1514 u8 pg_buff = tc_index;
1515 enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
1519 if (local_port == MLXSW_PORT_CPU_PORT) {
1520 NL_SET_ERR_MSG_MOD(extack, "Changing CPU port's binding is forbidden");
1524 if (dir != mlxsw_sp->sb_vals->pool_dess[pool_index].dir) {
1525 NL_SET_ERR_MSG_MOD(extack, "Binding egress TC to ingress pool and vice versa is forbidden");
1529 if (dir == MLXSW_REG_SBXX_DIR_INGRESS)
1530 cm = &mlxsw_sp->sb_vals->cms_ingress[tc_index];
1532 cm = &mlxsw_sp->sb_vals->cms_egress[tc_index];
1534 if (cm->freeze_pool && cm->pool_index != pool_index) {
1535 NL_SET_ERR_MSG_MOD(extack, "Binding this TC to a different pool is forbidden");
1539 if (cm->freeze_thresh && cm->max_buff != threshold) {
1540 NL_SET_ERR_MSG_MOD(extack, "Changing this TC's threshold is forbidden");
1544 err = mlxsw_sp_sb_threshold_in(mlxsw_sp, pool_index,
1545 threshold, &max_buff, extack);
1549 return mlxsw_sp_sb_cm_write(mlxsw_sp, local_port, pg_buff,
1550 0, max_buff, false, pool_index);
1553 #define MASKED_COUNT_MAX \
1554 (MLXSW_REG_SBSR_REC_MAX_COUNT / \
1555 (MLXSW_SP_SB_ING_TC_COUNT + MLXSW_SP_SB_EG_TC_COUNT))
1557 struct mlxsw_sp_sb_sr_occ_query_cb_ctx {
1562 static void mlxsw_sp_sb_sr_occ_query_cb(struct mlxsw_core *mlxsw_core,
1563 char *sbsr_pl, size_t sbsr_pl_len,
1564 unsigned long cb_priv)
1566 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1567 struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
1571 struct mlxsw_sp_sb_cm *cm;
1574 memcpy(&cb_ctx, &cb_priv, sizeof(cb_ctx));
1577 for (local_port = cb_ctx.local_port_1;
1578 local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
1579 if (!mlxsw_sp->ports[local_port])
1581 if (local_port == MLXSW_PORT_CPU_PORT) {
1582 /* Ingress quotas are not supported for the CPU port */
1586 for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++) {
1587 cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
1588 MLXSW_REG_SBXX_DIR_INGRESS);
1589 mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
1590 &cm->occ.cur, &cm->occ.max);
1592 if (++masked_count == cb_ctx.masked_count)
1596 for (local_port = cb_ctx.local_port_1;
1597 local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
1598 if (!mlxsw_sp->ports[local_port])
1600 for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++) {
1601 cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port, i,
1602 MLXSW_REG_SBXX_DIR_EGRESS);
1603 mlxsw_reg_sbsr_rec_unpack(sbsr_pl, rec_index++,
1604 &cm->occ.cur, &cm->occ.max);
1606 if (++masked_count == cb_ctx.masked_count)
1611 int mlxsw_sp_sb_occ_snapshot(struct mlxsw_core *mlxsw_core,
1612 unsigned int sb_index)
1614 u16 local_port, local_port_1, first_local_port, last_local_port;
1615 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1616 struct mlxsw_sp_sb_sr_occ_query_cb_ctx cb_ctx;
1617 u8 masked_count, current_page = 0;
1618 unsigned long cb_priv = 0;
1619 LIST_HEAD(bulk_list);
1625 sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
1629 local_port = MLXSW_PORT_CPU_PORT;
1631 local_port_1 = local_port;
1633 mlxsw_reg_sbsr_pack(sbsr_pl, false);
1634 mlxsw_reg_sbsr_port_page_set(sbsr_pl, current_page);
1635 first_local_port = current_page * MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE;
1636 last_local_port = current_page * MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE +
1637 MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE - 1;
1639 for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
1640 mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
1641 for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
1642 mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
1643 for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
1644 if (!mlxsw_sp->ports[local_port])
1646 if (local_port > last_local_port) {
1650 if (local_port != MLXSW_PORT_CPU_PORT) {
1651 /* Ingress quotas are not supported for the CPU port */
1652 mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl,
1653 local_port - first_local_port,
1656 mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl,
1657 local_port - first_local_port,
1659 for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
1660 err = mlxsw_sp_sb_pm_occ_query(mlxsw_sp, local_port, i,
1665 if (++masked_count == MASKED_COUNT_MAX)
1670 cb_ctx.masked_count = masked_count;
1671 cb_ctx.local_port_1 = local_port_1;
1672 memcpy(&cb_priv, &cb_ctx, sizeof(cb_ctx));
1673 err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
1674 &bulk_list, mlxsw_sp_sb_sr_occ_query_cb,
1678 if (local_port < mlxsw_core_max_ports(mlxsw_core)) {
1684 err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
1691 int mlxsw_sp_sb_occ_max_clear(struct mlxsw_core *mlxsw_core,
1692 unsigned int sb_index)
1694 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
1695 u16 local_port, first_local_port, last_local_port;
1696 LIST_HEAD(bulk_list);
1697 unsigned int masked_count;
1698 u8 current_page = 0;
1704 sbsr_pl = kmalloc(MLXSW_REG_SBSR_LEN, GFP_KERNEL);
1708 local_port = MLXSW_PORT_CPU_PORT;
1711 mlxsw_reg_sbsr_pack(sbsr_pl, true);
1712 mlxsw_reg_sbsr_port_page_set(sbsr_pl, current_page);
1713 first_local_port = current_page * MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE;
1714 last_local_port = current_page * MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE +
1715 MLXSW_REG_SBSR_NUM_PORTS_IN_PAGE - 1;
1717 for (i = 0; i < MLXSW_SP_SB_ING_TC_COUNT; i++)
1718 mlxsw_reg_sbsr_pg_buff_mask_set(sbsr_pl, i, 1);
1719 for (i = 0; i < MLXSW_SP_SB_EG_TC_COUNT; i++)
1720 mlxsw_reg_sbsr_tclass_mask_set(sbsr_pl, i, 1);
1721 for (; local_port < mlxsw_core_max_ports(mlxsw_core); local_port++) {
1722 if (!mlxsw_sp->ports[local_port])
1724 if (local_port > last_local_port) {
1728 if (local_port != MLXSW_PORT_CPU_PORT) {
1729 /* Ingress quotas are not supported for the CPU port */
1730 mlxsw_reg_sbsr_ingress_port_mask_set(sbsr_pl,
1731 local_port - first_local_port,
1734 mlxsw_reg_sbsr_egress_port_mask_set(sbsr_pl,
1735 local_port - first_local_port,
1737 for (i = 0; i < mlxsw_sp->sb_vals->pool_count; i++) {
1738 err = mlxsw_sp_sb_pm_occ_clear(mlxsw_sp, local_port, i,
1743 if (++masked_count == MASKED_COUNT_MAX)
1748 err = mlxsw_reg_trans_query(mlxsw_core, MLXSW_REG(sbsr), sbsr_pl,
1749 &bulk_list, NULL, 0);
1752 if (local_port < mlxsw_core_max_ports(mlxsw_core)) {
1758 err2 = mlxsw_reg_trans_bulk_wait(&bulk_list);
1765 int mlxsw_sp_sb_occ_port_pool_get(struct mlxsw_core_port *mlxsw_core_port,
1766 unsigned int sb_index, u16 pool_index,
1767 u32 *p_cur, u32 *p_max)
1769 struct mlxsw_sp_port *mlxsw_sp_port =
1770 mlxsw_core_port_driver_priv(mlxsw_core_port);
1771 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1772 u16 local_port = mlxsw_sp_port->local_port;
1773 struct mlxsw_sp_sb_pm *pm = mlxsw_sp_sb_pm_get(mlxsw_sp, local_port,
1776 *p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.cur);
1777 *p_max = mlxsw_sp_cells_bytes(mlxsw_sp, pm->occ.max);
1781 int mlxsw_sp_sb_occ_tc_port_bind_get(struct mlxsw_core_port *mlxsw_core_port,
1782 unsigned int sb_index, u16 tc_index,
1783 enum devlink_sb_pool_type pool_type,
1784 u32 *p_cur, u32 *p_max)
1786 struct mlxsw_sp_port *mlxsw_sp_port =
1787 mlxsw_core_port_driver_priv(mlxsw_core_port);
1788 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1789 u16 local_port = mlxsw_sp_port->local_port;
1790 u8 pg_buff = tc_index;
1791 enum mlxsw_reg_sbxx_dir dir = (enum mlxsw_reg_sbxx_dir) pool_type;
1792 struct mlxsw_sp_sb_cm *cm = mlxsw_sp_sb_cm_get(mlxsw_sp, local_port,
1795 *p_cur = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.cur);
1796 *p_max = mlxsw_sp_cells_bytes(mlxsw_sp, cm->occ.max);