Commit | Line | Data |
---|---|---|
1466cc5b YP |
1 | /* |
2 | * Copyright (c) 2013-2016, Mellanox Technologies. All rights reserved. | |
3 | * | |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
33 | #include <linux/kernel.h> | |
34 | #include <linux/module.h> | |
35 | #include <linux/mlx5/driver.h> | |
36 | #include <linux/mlx5/cmd.h> | |
37 | #include "mlx5_core.h" | |
38 | ||
39 | /* Finds an entry where we can register the given rate | |
40 | * If the rate already exists, return the entry where it is registered, | |
41 | * otherwise return the first available entry. | |
42 | * If the table is full, return NULL | |
43 | */ | |
44 | static struct mlx5_rl_entry *find_rl_entry(struct mlx5_rl_table *table, | |
45 | u32 rate) | |
46 | { | |
47 | struct mlx5_rl_entry *ret_entry = NULL; | |
48 | bool empty_found = false; | |
49 | int i; | |
50 | ||
51 | for (i = 0; i < table->max_size; i++) { | |
52 | if (table->rl_entry[i].rate == rate) | |
53 | return &table->rl_entry[i]; | |
54 | if (!empty_found && !table->rl_entry[i].rate) { | |
55 | empty_found = true; | |
56 | ret_entry = &table->rl_entry[i]; | |
57 | } | |
58 | } | |
59 | ||
60 | return ret_entry; | |
61 | } | |
62 | ||
63 | static int mlx5_set_rate_limit_cmd(struct mlx5_core_dev *dev, | |
64 | u32 rate, u16 index) | |
65 | { | |
c4f287c4 SM |
66 | u32 in[MLX5_ST_SZ_DW(set_rate_limit_in)] = {0}; |
67 | u32 out[MLX5_ST_SZ_DW(set_rate_limit_out)] = {0}; | |
1466cc5b YP |
68 | |
69 | MLX5_SET(set_rate_limit_in, in, opcode, | |
70 | MLX5_CMD_OP_SET_RATE_LIMIT); | |
71 | MLX5_SET(set_rate_limit_in, in, rate_limit_index, index); | |
72 | MLX5_SET(set_rate_limit_in, in, rate_limit, rate); | |
c4f287c4 | 73 | return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
1466cc5b YP |
74 | } |
75 | ||
76 | bool mlx5_rl_is_in_range(struct mlx5_core_dev *dev, u32 rate) | |
77 | { | |
78 | struct mlx5_rl_table *table = &dev->priv.rl_table; | |
79 | ||
80 | return (rate <= table->max_rate && rate >= table->min_rate); | |
81 | } | |
82 | EXPORT_SYMBOL(mlx5_rl_is_in_range); | |
83 | ||
84 | int mlx5_rl_add_rate(struct mlx5_core_dev *dev, u32 rate, u16 *index) | |
85 | { | |
86 | struct mlx5_rl_table *table = &dev->priv.rl_table; | |
87 | struct mlx5_rl_entry *entry; | |
88 | int err = 0; | |
89 | ||
90 | mutex_lock(&table->rl_lock); | |
91 | ||
92 | if (!rate || !mlx5_rl_is_in_range(dev, rate)) { | |
93 | mlx5_core_err(dev, "Invalid rate: %u, should be %u to %u\n", | |
94 | rate, table->min_rate, table->max_rate); | |
95 | err = -EINVAL; | |
96 | goto out; | |
97 | } | |
98 | ||
99 | entry = find_rl_entry(table, rate); | |
100 | if (!entry) { | |
101 | mlx5_core_err(dev, "Max number of %u rates reached\n", | |
102 | table->max_size); | |
103 | err = -ENOSPC; | |
104 | goto out; | |
105 | } | |
106 | if (entry->refcount) { | |
107 | /* rate already configured */ | |
108 | entry->refcount++; | |
109 | } else { | |
110 | /* new rate limit */ | |
111 | err = mlx5_set_rate_limit_cmd(dev, rate, entry->index); | |
112 | if (err) { | |
113 | mlx5_core_err(dev, "Failed configuring rate: %u (%d)\n", | |
114 | rate, err); | |
115 | goto out; | |
116 | } | |
117 | entry->rate = rate; | |
118 | entry->refcount = 1; | |
119 | } | |
120 | *index = entry->index; | |
121 | ||
122 | out: | |
123 | mutex_unlock(&table->rl_lock); | |
124 | return err; | |
125 | } | |
126 | EXPORT_SYMBOL(mlx5_rl_add_rate); | |
127 | ||
128 | void mlx5_rl_remove_rate(struct mlx5_core_dev *dev, u32 rate) | |
129 | { | |
130 | struct mlx5_rl_table *table = &dev->priv.rl_table; | |
131 | struct mlx5_rl_entry *entry = NULL; | |
132 | ||
133 | /* 0 is a reserved value for unlimited rate */ | |
134 | if (rate == 0) | |
135 | return; | |
136 | ||
137 | mutex_lock(&table->rl_lock); | |
138 | entry = find_rl_entry(table, rate); | |
139 | if (!entry || !entry->refcount) { | |
140 | mlx5_core_warn(dev, "Rate %u is not configured\n", rate); | |
141 | goto out; | |
142 | } | |
143 | ||
144 | entry->refcount--; | |
145 | if (!entry->refcount) { | |
146 | /* need to remove rate */ | |
147 | mlx5_set_rate_limit_cmd(dev, 0, entry->index); | |
148 | entry->rate = 0; | |
149 | } | |
150 | ||
151 | out: | |
152 | mutex_unlock(&table->rl_lock); | |
153 | } | |
154 | EXPORT_SYMBOL(mlx5_rl_remove_rate); | |
155 | ||
156 | int mlx5_init_rl_table(struct mlx5_core_dev *dev) | |
157 | { | |
158 | struct mlx5_rl_table *table = &dev->priv.rl_table; | |
159 | int i; | |
160 | ||
161 | mutex_init(&table->rl_lock); | |
162 | if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, packet_pacing)) { | |
163 | table->max_size = 0; | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /* First entry is reserved for unlimited rate */ | |
168 | table->max_size = MLX5_CAP_QOS(dev, packet_pacing_rate_table_size) - 1; | |
169 | table->max_rate = MLX5_CAP_QOS(dev, packet_pacing_max_rate); | |
170 | table->min_rate = MLX5_CAP_QOS(dev, packet_pacing_min_rate); | |
171 | ||
172 | table->rl_entry = kcalloc(table->max_size, sizeof(struct mlx5_rl_entry), | |
173 | GFP_KERNEL); | |
174 | if (!table->rl_entry) | |
175 | return -ENOMEM; | |
176 | ||
177 | /* The index represents the index in HW rate limit table | |
178 | * Index 0 is reserved for unlimited rate | |
179 | */ | |
180 | for (i = 0; i < table->max_size; i++) | |
181 | table->rl_entry[i].index = i + 1; | |
182 | ||
183 | /* Index 0 is reserved */ | |
184 | mlx5_core_info(dev, "Rate limit: %u rates are supported, range: %uMbps to %uMbps\n", | |
185 | table->max_size, | |
186 | table->min_rate >> 10, | |
187 | table->max_rate >> 10); | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | void mlx5_cleanup_rl_table(struct mlx5_core_dev *dev) | |
193 | { | |
194 | struct mlx5_rl_table *table = &dev->priv.rl_table; | |
195 | int i; | |
196 | ||
197 | /* Clear all configured rates */ | |
198 | for (i = 0; i < table->max_size; i++) | |
199 | if (table->rl_entry[i].rate) | |
200 | mlx5_set_rate_limit_cmd(dev, 0, | |
201 | table->rl_entry[i].index); | |
202 | ||
203 | kfree(dev->priv.rl_table.rl_entry); | |
204 | } |