// SPDX-License-Identifier: (GPL-2.0 OR MIT) /* Copyright 2020-2021 NXP */ #include <net/devlink.h> #include "ocelot.h" /* The queue system tracks four resource consumptions: * Resource 0: Memory tracked per source port * Resource 1: Frame references tracked per source port * Resource 2: Memory tracked per destination port * Resource 3: Frame references tracked per destination port */ #define OCELOT_RESOURCE_SZ … #define OCELOT_NUM_RESOURCES … #define BUF_xxxx_I … #define REF_xxxx_I … #define BUF_xxxx_E … #define REF_xxxx_E … /* For each resource type there are 4 types of watermarks: * Q_RSRV: reservation per QoS class per port * PRIO_SHR: sharing watermark per QoS class across all ports * P_RSRV: reservation per port * COL_SHR: sharing watermark per color (drop precedence) across all ports */ #define xxx_Q_RSRV_x … #define xxx_PRIO_SHR_x … #define xxx_P_RSRV_x … #define xxx_COL_SHR_x … /* Reservation Watermarks * ---------------------- * * For setting up the reserved areas, egress watermarks exist per port and per * QoS class for both ingress and egress. */ /* Amount of packet buffer * | per QoS class * | | reserved * | | | per egress port * | | | | * V V v v * BUF_Q_RSRV_E */ #define BUF_Q_RSRV_E(port, prio) … /* Amount of packet buffer * | for all port's traffic classes * | | reserved * | | | per egress port * | | | | * V V v v * BUF_P_RSRV_E */ #define BUF_P_RSRV_E(port) … /* Amount of packet buffer * | per QoS class * | | reserved * | | | per ingress port * | | | | * V V v v * BUF_Q_RSRV_I */ #define BUF_Q_RSRV_I(port, prio) … /* Amount of packet buffer * | for all port's traffic classes * | | reserved * | | | per ingress port * | | | | * V V v v * BUF_P_RSRV_I */ #define BUF_P_RSRV_I(port) … /* Amount of frame references * | per QoS class * | | reserved * | | | per egress port * | | | | * V V v v * REF_Q_RSRV_E */ #define REF_Q_RSRV_E(port, prio) … /* Amount of frame references * | for all port's traffic classes * | | reserved * | | | per egress port * | | | | * V V v v * REF_P_RSRV_E */ #define REF_P_RSRV_E(port) … /* Amount of frame references * | per QoS class * | | reserved * | | | per ingress port * | | | | * V V v v * REF_Q_RSRV_I */ #define REF_Q_RSRV_I(port, prio) … /* Amount of frame references * | for all port's traffic classes * | | reserved * | | | per ingress port * | | | | * V V v v * REF_P_RSRV_I */ #define REF_P_RSRV_I(port) … /* Sharing Watermarks * ------------------ * * The shared memory area is shared between all ports. */ /* Amount of buffer * | per QoS class * | | from the shared memory area * | | | for egress traffic * | | | | * V V v v * BUF_PRIO_SHR_E */ #define BUF_PRIO_SHR_E(prio) … /* Amount of buffer * | per color (drop precedence level) * | | from the shared memory area * | | | for egress traffic * | | | | * V V v v * BUF_COL_SHR_E */ #define BUF_COL_SHR_E(dp) … /* Amount of buffer * | per QoS class * | | from the shared memory area * | | | for ingress traffic * | | | | * V V v v * BUF_PRIO_SHR_I */ #define BUF_PRIO_SHR_I(prio) … /* Amount of buffer * | per color (drop precedence level) * | | from the shared memory area * | | | for ingress traffic * | | | | * V V v v * BUF_COL_SHR_I */ #define BUF_COL_SHR_I(dp) … /* Amount of frame references * | per QoS class * | | from the shared area * | | | for egress traffic * | | | | * V V v v * REF_PRIO_SHR_E */ #define REF_PRIO_SHR_E(prio) … /* Amount of frame references * | per color (drop precedence level) * | | from the shared area * | | | for egress traffic * | | | | * V V v v * REF_COL_SHR_E */ #define REF_COL_SHR_E(dp) … /* Amount of frame references * | per QoS class * | | from the shared area * | | | for ingress traffic * | | | | * V V v v * REF_PRIO_SHR_I */ #define REF_PRIO_SHR_I(prio) … /* Amount of frame references * | per color (drop precedence level) * | | from the shared area * | | | for ingress traffic * | | | | * V V v v * REF_COL_SHR_I */ #define REF_COL_SHR_I(dp) … static u32 ocelot_wm_read(struct ocelot *ocelot, int index) { … } static void ocelot_wm_write(struct ocelot *ocelot, int index, u32 val) { … } static void ocelot_wm_status(struct ocelot *ocelot, int index, u32 *inuse, u32 *maxuse) { … } /* The hardware comes out of reset with strange defaults: the sum of all * reservations for frame memory is larger than the total buffer size. * One has to wonder how can the reservation watermarks still guarantee * anything under congestion. * Bring some sense into the hardware by changing the defaults to disable all * reservations and rely only on the sharing watermark for frames with drop * precedence 0. The user can still explicitly request reservations per port * and per port-tc through devlink-sb. */ static void ocelot_disable_reservation_watermarks(struct ocelot *ocelot, int port) { … } /* We want the sharing watermarks to consume all nonreserved resources, for * efficient resource utilization (a single traffic flow should be able to use * up the entire buffer space and frame resources as long as there's no * interference). * The switch has 10 sharing watermarks per lookup: 8 per traffic class and 2 * per color (drop precedence). * The trouble with configuring these sharing watermarks is that: * (1) There's a risk that we overcommit the resources if we configure * (a) all 8 per-TC sharing watermarks to the max * (b) all 2 per-color sharing watermarks to the max * (2) There's a risk that we undercommit the resources if we configure * (a) all 8 per-TC sharing watermarks to "max / 8" * (b) all 2 per-color sharing watermarks to "max / 2" * So for Linux, let's just disable the sharing watermarks per traffic class * (setting them to 0 will make them always exceeded), and rely only on the * sharing watermark for drop priority 0. So frames with drop priority set to 1 * by QoS classification or policing will still be allowed, but only as long as * the port and port-TC reservations are not exceeded. */ static void ocelot_disable_tc_sharing_watermarks(struct ocelot *ocelot) { … } static void ocelot_get_buf_rsrv(struct ocelot *ocelot, u32 *buf_rsrv_i, u32 *buf_rsrv_e) { … } static void ocelot_get_ref_rsrv(struct ocelot *ocelot, u32 *ref_rsrv_i, u32 *ref_rsrv_e) { … } /* Calculate all reservations, then set up the sharing watermark for DP=0 to * consume the remaining resources up to the pool's configured size. */ static void ocelot_setup_sharing_watermarks(struct ocelot *ocelot) { … } /* Ensure that all reservations can be enforced */ static int ocelot_watermark_validate(struct ocelot *ocelot, struct netlink_ext_ack *extack) { … } /* The hardware works like this: * * Frame forwarding decision taken * | * v * +--------------------+--------------------+--------------------+ * | | | | * v v v v * Ingress memory Egress memory Ingress frame Egress frame * check check reference check reference check * | | | | * v v v v * BUF_Q_RSRV_I ok BUF_Q_RSRV_E ok REF_Q_RSRV_I ok REF_Q_RSRV_E ok *(src port, prio) -+ (dst port, prio) -+ (src port, prio) -+ (dst port, prio) -+ * | | | | | | | | * |exceeded | |exceeded | |exceeded | |exceeded | * v | v | v | v | * BUF_P_RSRV_I ok| BUF_P_RSRV_E ok| REF_P_RSRV_I ok| REF_P_RSRV_E ok| * (src port) ----+ (dst port) ----+ (src port) ----+ (dst port) -----+ * | | | | | | | | * |exceeded | |exceeded | |exceeded | |exceeded | * v | v | v | v | * BUF_PRIO_SHR_I ok| BUF_PRIO_SHR_E ok| REF_PRIO_SHR_I ok| REF_PRIO_SHR_E ok| * (prio) ------+ (prio) ------+ (prio) ------+ (prio) -------+ * | | | | | | | | * |exceeded | |exceeded | |exceeded | |exceeded | * v | v | v | v | * BUF_COL_SHR_I ok| BUF_COL_SHR_E ok| REF_COL_SHR_I ok| REF_COL_SHR_E ok| * (dp) -------+ (dp) -------+ (dp) -------+ (dp) --------+ * | | | | | | | | * |exceeded | |exceeded | |exceeded | |exceeded | * v v v v v v v v * fail success fail success fail success fail success * | | | | | | | | * v v v v v v v v * +-----+----+ +-----+----+ +-----+----+ +-----+-----+ * | | | | * +-------> OR <-------+ +-------> OR <-------+ * | | * v v * +----------------> AND <-----------------+ * | * v * FIFO drop / accept * * We are modeling each of the 4 parallel lookups as a devlink-sb pool. * At least one (ingress or egress) memory pool and one (ingress or egress) * frame reference pool need to have resources for frame acceptance to succeed. * * The following watermarks are controlled explicitly through devlink-sb: * BUF_Q_RSRV_I, BUF_Q_RSRV_E, REF_Q_RSRV_I, REF_Q_RSRV_E * BUF_P_RSRV_I, BUF_P_RSRV_E, REF_P_RSRV_I, REF_P_RSRV_E * The following watermarks are controlled implicitly through devlink-sb: * BUF_COL_SHR_I, BUF_COL_SHR_E, REF_COL_SHR_I, REF_COL_SHR_E * The following watermarks are unused and disabled: * BUF_PRIO_SHR_I, BUF_PRIO_SHR_E, REF_PRIO_SHR_I, REF_PRIO_SHR_E * * This function overrides the hardware defaults with more sane ones (no * reservations by default, let sharing use all resources) and disables the * unused watermarks. */ static void ocelot_watermark_init(struct ocelot *ocelot) { … } /* Watermark encode * Bit 8: Unit; 0:1, 1:16 * Bit 7-0: Value to be multiplied with unit */ u16 ocelot_wm_enc(u16 value) { … } EXPORT_SYMBOL(…); u16 ocelot_wm_dec(u16 wm) { … } EXPORT_SYMBOL(…); void ocelot_wm_stat(u32 val, u32 *inuse, u32 *maxuse) { … } EXPORT_SYMBOL(…); /* Pool size and type are fixed up at runtime. Keeping this structure to * look up the cell size multipliers. */ static const struct devlink_sb_pool_info ocelot_sb_pool[] = …; /* Returns the pool size configured through ocelot_sb_pool_set */ int ocelot_sb_pool_get(struct ocelot *ocelot, unsigned int sb_index, u16 pool_index, struct devlink_sb_pool_info *pool_info) { … } EXPORT_SYMBOL(…); /* The pool size received here configures the total amount of resources used on * ingress (or on egress, depending upon the pool index). The pool size, minus * the values for the port and port-tc reservations, is written into the * COL_SHR(dp=0) sharing watermark. */ int ocelot_sb_pool_set(struct ocelot *ocelot, unsigned int sb_index, u16 pool_index, u32 size, enum devlink_sb_threshold_type threshold_type, struct netlink_ext_ack *extack) { … } EXPORT_SYMBOL(…); /* This retrieves the configuration made with ocelot_sb_port_pool_set */ int ocelot_sb_port_pool_get(struct ocelot *ocelot, int port, unsigned int sb_index, u16 pool_index, u32 *p_threshold) { … } EXPORT_SYMBOL(…); /* This configures the P_RSRV per-port reserved resource watermark */ int ocelot_sb_port_pool_set(struct ocelot *ocelot, int port, unsigned int sb_index, u16 pool_index, u32 threshold, struct netlink_ext_ack *extack) { … } EXPORT_SYMBOL(…); /* This retrieves the configuration done by ocelot_sb_tc_pool_bind_set */ int ocelot_sb_tc_pool_bind_get(struct ocelot *ocelot, int port, unsigned int sb_index, u16 tc_index, enum devlink_sb_pool_type pool_type, u16 *p_pool_index, u32 *p_threshold) { … } EXPORT_SYMBOL(…); /* This configures the Q_RSRV per-port-tc reserved resource watermark */ int ocelot_sb_tc_pool_bind_set(struct ocelot *ocelot, int port, unsigned int sb_index, u16 tc_index, enum devlink_sb_pool_type pool_type, u16 pool_index, u32 threshold, struct netlink_ext_ack *extack) { … } EXPORT_SYMBOL(…); /* The hardware does not support atomic snapshots, we'll read out the * occupancy registers individually and have this as just a stub. */ int ocelot_sb_occ_snapshot(struct ocelot *ocelot, unsigned int sb_index) { … } EXPORT_SYMBOL(…); /* The watermark occupancy registers are cleared upon read, * so let's read them. */ int ocelot_sb_occ_max_clear(struct ocelot *ocelot, unsigned int sb_index) { … } EXPORT_SYMBOL(…); /* This retrieves the watermark occupancy for per-port P_RSRV watermarks */ int ocelot_sb_occ_port_pool_get(struct ocelot *ocelot, int port, unsigned int sb_index, u16 pool_index, u32 *p_cur, u32 *p_max) { … } EXPORT_SYMBOL(…); /* This retrieves the watermark occupancy for per-port-tc Q_RSRV watermarks */ int ocelot_sb_occ_tc_port_bind_get(struct ocelot *ocelot, int port, unsigned int sb_index, u16 tc_index, enum devlink_sb_pool_type pool_type, u32 *p_cur, u32 *p_max) { … } EXPORT_SYMBOL(…); int ocelot_devlink_sb_register(struct ocelot *ocelot) { … } EXPORT_SYMBOL(…); void ocelot_devlink_sb_unregister(struct ocelot *ocelot) { … } EXPORT_SYMBOL(…);