|
| 1 | +/* |
| 2 | + * drivers/net/ethernet/mellanox/mlxsw/spectrum_qdisc.c |
| 3 | + * Copyright (c) 2017 Mellanox Technologies. All rights reserved. |
| 4 | + * Copyright (c) 2017 Nogah Frankel <nogahf@mellanox.com> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the names of the copyright holders nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * Alternatively, this software may be distributed under the terms of the |
| 19 | + * GNU General Public License ("GPL") version 2 as published by the Free |
| 20 | + * Software Foundation. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 26 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | + |
| 35 | +#include <linux/kernel.h> |
| 36 | +#include <linux/errno.h> |
| 37 | +#include <linux/netdevice.h> |
| 38 | +#include <net/pkt_cls.h> |
| 39 | + |
| 40 | +#include "spectrum.h" |
| 41 | +#include "reg.h" |
| 42 | + |
| 43 | +static int |
| 44 | +mlxsw_sp_tclass_congestion_enable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 45 | + int tclass_num, u32 min, u32 max, |
| 46 | + u32 probability, bool is_ecn) |
| 47 | +{ |
| 48 | + char cwtp_cmd[max_t(u8, MLXSW_REG_CWTP_LEN, MLXSW_REG_CWTPM_LEN)]; |
| 49 | + struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 50 | + int err; |
| 51 | + |
| 52 | + mlxsw_reg_cwtp_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num); |
| 53 | + mlxsw_reg_cwtp_profile_pack(cwtp_cmd, MLXSW_REG_CWTP_DEFAULT_PROFILE, |
| 54 | + roundup(min, MLXSW_REG_CWTP_MIN_VALUE), |
| 55 | + roundup(max, MLXSW_REG_CWTP_MIN_VALUE), |
| 56 | + probability); |
| 57 | + |
| 58 | + err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtp), cwtp_cmd); |
| 59 | + if (err) |
| 60 | + return err; |
| 61 | + |
| 62 | + mlxsw_reg_cwtpm_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num, |
| 63 | + MLXSW_REG_CWTP_DEFAULT_PROFILE, true, is_ecn); |
| 64 | + |
| 65 | + return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtp_cmd); |
| 66 | +} |
| 67 | + |
| 68 | +static int |
| 69 | +mlxsw_sp_tclass_congestion_disable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 70 | + int tclass_num) |
| 71 | +{ |
| 72 | + struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 73 | + char cwtpm_cmd[MLXSW_REG_CWTPM_LEN]; |
| 74 | + |
| 75 | + mlxsw_reg_cwtpm_pack(cwtpm_cmd, mlxsw_sp_port->local_port, tclass_num, |
| 76 | + MLXSW_REG_CWTPM_RESET_PROFILE, false, false); |
| 77 | + return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtpm_cmd); |
| 78 | +} |
| 79 | + |
| 80 | +static int |
| 81 | +mlxsw_sp_qdisc_red_destroy(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle, |
| 82 | + struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, |
| 83 | + int tclass_num) |
| 84 | +{ |
| 85 | + int err; |
| 86 | + |
| 87 | + if (mlxsw_sp_qdisc->handle != handle) |
| 88 | + return 0; |
| 89 | + |
| 90 | + err = mlxsw_sp_tclass_congestion_disable(mlxsw_sp_port, tclass_num); |
| 91 | + mlxsw_sp_qdisc->handle = TC_H_UNSPEC; |
| 92 | + mlxsw_sp_qdisc->type = MLXSW_SP_QDISC_NO_QDISC; |
| 93 | + |
| 94 | + return err; |
| 95 | +} |
| 96 | + |
| 97 | +static int |
| 98 | +mlxsw_sp_qdisc_red_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle, |
| 99 | + struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, |
| 100 | + int tclass_num, |
| 101 | + struct tc_red_qopt_offload_params *p) |
| 102 | +{ |
| 103 | + struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 104 | + u32 min, max; |
| 105 | + u64 prob; |
| 106 | + int err = 0; |
| 107 | + |
| 108 | + if (p->min > p->max) { |
| 109 | + dev_err(mlxsw_sp->bus_info->dev, |
| 110 | + "spectrum: RED: min %u is bigger then max %u\n", p->min, |
| 111 | + p->max); |
| 112 | + goto err_bad_param; |
| 113 | + } |
| 114 | + if (p->max > MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_BUFFER_SIZE)) { |
| 115 | + dev_err(mlxsw_sp->bus_info->dev, |
| 116 | + "spectrum: RED: max value %u is too big\n", p->max); |
| 117 | + goto err_bad_param; |
| 118 | + } |
| 119 | + if (p->min == 0 || p->max == 0) { |
| 120 | + dev_err(mlxsw_sp->bus_info->dev, |
| 121 | + "spectrum: RED: 0 value is illegal for min and max\n"); |
| 122 | + goto err_bad_param; |
| 123 | + } |
| 124 | + |
| 125 | + /* calculate probability in percentage */ |
| 126 | + prob = p->probability; |
| 127 | + prob *= 100; |
| 128 | + prob = DIV_ROUND_UP(prob, 1 << 16); |
| 129 | + prob = DIV_ROUND_UP(prob, 1 << 16); |
| 130 | + min = mlxsw_sp_bytes_cells(mlxsw_sp, p->min); |
| 131 | + max = mlxsw_sp_bytes_cells(mlxsw_sp, p->max); |
| 132 | + err = mlxsw_sp_tclass_congestion_enable(mlxsw_sp_port, tclass_num, min, |
| 133 | + max, prob, p->is_ecn); |
| 134 | + if (err) |
| 135 | + goto err_config; |
| 136 | + |
| 137 | + mlxsw_sp_qdisc->type = MLXSW_SP_QDISC_RED; |
| 138 | + mlxsw_sp_qdisc->handle = handle; |
| 139 | + return 0; |
| 140 | + |
| 141 | +err_bad_param: |
| 142 | + err = -EINVAL; |
| 143 | +err_config: |
| 144 | + mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, mlxsw_sp_qdisc->handle, |
| 145 | + mlxsw_sp_qdisc, tclass_num); |
| 146 | + return err; |
| 147 | +} |
| 148 | + |
| 149 | +#define MLXSW_SP_PORT_DEFAULT_TCLASS 0 |
| 150 | + |
| 151 | +int mlxsw_sp_setup_tc_red(struct mlxsw_sp_port *mlxsw_sp_port, |
| 152 | + struct tc_red_qopt_offload *p) |
| 153 | +{ |
| 154 | + struct mlxsw_sp_qdisc *mlxsw_sp_qdisc; |
| 155 | + int tclass_num; |
| 156 | + |
| 157 | + if (p->parent != TC_H_ROOT) |
| 158 | + return -EOPNOTSUPP; |
| 159 | + |
| 160 | + mlxsw_sp_qdisc = &mlxsw_sp_port->root_qdisc; |
| 161 | + tclass_num = MLXSW_SP_PORT_DEFAULT_TCLASS; |
| 162 | + |
| 163 | + switch (p->command) { |
| 164 | + case TC_RED_REPLACE: |
| 165 | + return mlxsw_sp_qdisc_red_replace(mlxsw_sp_port, p->handle, |
| 166 | + mlxsw_sp_qdisc, tclass_num, |
| 167 | + &p->set); |
| 168 | + case TC_RED_DESTROY: |
| 169 | + return mlxsw_sp_qdisc_red_destroy(mlxsw_sp_port, p->handle, |
| 170 | + mlxsw_sp_qdisc, tclass_num); |
| 171 | + default: |
| 172 | + return -EOPNOTSUPP; |
| 173 | + } |
| 174 | +} |
0 commit comments