xref: /freebsd/sys/dev/ena/ena_rss.c (revision 53b70c86)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015-2021 Amazon.com, Inc. or its affiliates.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_rss.h"
38 
39 #include "ena_rss.h"
40 
41 /*
42  * This function should generate unique key for the whole driver.
43  * If the key was already genereated in the previous call (for example
44  * for another adapter), then it should be returned instead.
45  */
46 void
47 ena_rss_key_fill(void *key, size_t size)
48 {
49 	static bool key_generated;
50 	static uint8_t default_key[ENA_HASH_KEY_SIZE];
51 
52 	KASSERT(size <= ENA_HASH_KEY_SIZE, ("Requested more bytes than ENA RSS key can hold"));
53 
54 	if (!key_generated) {
55 		arc4random_buf(default_key, ENA_HASH_KEY_SIZE);
56 		key_generated = true;
57 	}
58 
59 	memcpy(key, default_key, size);
60 }
61 
62 /*
63  * ENA HW expects the key to be in reverse-byte order.
64  */
65 static void
66 ena_rss_reorder_hash_key(u8 *reordered_key, const u8 *key, size_t key_size)
67 {
68 	int i;
69 
70 	key = key + key_size - 1;
71 
72 	for (i = 0; i < key_size; ++i)
73 		*reordered_key++ = *key--;
74 }
75 
76 int ena_rss_set_hash(struct ena_com_dev *ena_dev, const u8 *key)
77 {
78 	enum ena_admin_hash_functions ena_func = ENA_ADMIN_TOEPLITZ;
79 	u8 hw_key[ENA_HASH_KEY_SIZE];
80 
81 	ena_rss_reorder_hash_key(hw_key, key, ENA_HASH_KEY_SIZE);
82 
83 	return (ena_com_fill_hash_function(ena_dev, ena_func, hw_key,
84 	    ENA_HASH_KEY_SIZE, 0x0));
85 }
86 
87 int ena_rss_get_hash_key(struct ena_com_dev *ena_dev, u8 *key)
88 {
89 	u8 hw_key[ENA_HASH_KEY_SIZE];
90 	int rc;
91 
92 	rc = ena_com_get_hash_key(ena_dev, hw_key);
93 	if (rc != 0)
94 		return rc;
95 
96 	ena_rss_reorder_hash_key(key, hw_key, ENA_HASH_KEY_SIZE);
97 
98 	return (0);
99 }
100 
101 static int
102 ena_rss_init_default(struct ena_adapter *adapter)
103 {
104 	struct ena_com_dev *ena_dev = adapter->ena_dev;
105 	device_t dev = adapter->pdev;
106 	int qid, rc, i;
107 
108 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
109 	if (unlikely(rc != 0)) {
110 		ena_log(dev, ERR, "Cannot init indirect table\n");
111 		return (rc);
112 	}
113 
114 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
115 #ifdef RSS
116 		qid = rss_get_indirection_to_bucket(i) % adapter->num_io_queues;
117 #else
118 		qid = i % adapter->num_io_queues;
119 #endif
120 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
121 		    ENA_IO_RXQ_IDX(qid));
122 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
123 			ena_log(dev, ERR, "Cannot fill indirect table\n");
124 			goto err_rss_destroy;
125 		}
126 	}
127 
128 
129 #ifdef RSS
130 	uint8_t rss_algo = rss_gethashalgo();
131 	if (rss_algo == RSS_HASH_TOEPLITZ) {
132 		uint8_t hash_key[RSS_KEYSIZE];
133 
134 		rss_getkey(hash_key);
135 		rc = ena_rss_set_hash(ena_dev, hash_key);
136 	} else
137 #endif
138 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ, NULL,
139 	    ENA_HASH_KEY_SIZE, 0x0);
140 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
141 		ena_log(dev, ERR, "Cannot fill hash function\n");
142 		goto err_rss_destroy;
143 	}
144 
145 	rc = ena_com_set_default_hash_ctrl(ena_dev);
146 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
147 		ena_log(dev, ERR, "Cannot fill hash control\n");
148 		goto err_rss_destroy;
149 	}
150 
151 	rc = ena_rss_indir_init(adapter);
152 
153 	return (rc == EOPNOTSUPP ? 0 : rc);
154 
155 err_rss_destroy:
156 	ena_com_rss_destroy(ena_dev);
157 	return (rc);
158 }
159 
160 /* Configure the Rx forwarding */
161 int
162 ena_rss_configure(struct ena_adapter *adapter)
163 {
164 	struct ena_com_dev *ena_dev = adapter->ena_dev;
165 	int rc;
166 
167 	/* In case the RSS table was destroyed */
168 	if (!ena_dev->rss.tbl_log_size) {
169 		rc = ena_rss_init_default(adapter);
170 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
171 			ena_log(adapter->pdev, ERR,
172 			    "WARNING: RSS was not properly re-initialized,"
173 			    " it will affect bandwidth\n");
174 			ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
175 			return (rc);
176 		}
177 	}
178 
179 	/* Set indirect table */
180 	rc = ena_com_indirect_table_set(ena_dev);
181 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
182 		return (rc);
183 
184 	/* Configure hash function (if supported) */
185 	rc = ena_com_set_hash_function(ena_dev);
186 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
187 		return (rc);
188 
189 	/* Configure hash inputs (if supported) */
190 	rc = ena_com_set_hash_ctrl(ena_dev);
191 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
192 		return (rc);
193 
194 	return (0);
195 }
196 
197 static void
198 ena_rss_init_default_deferred(void *arg)
199 {
200 	struct ena_adapter *adapter;
201 	devclass_t dc;
202 	int max;
203 	int rc;
204 
205 	dc = devclass_find("ena");
206 	if (unlikely(dc == NULL)) {
207 		ena_log_raw(ERR, "SYSINIT: %s: No devclass ena\n", __func__);
208 		return;
209 	}
210 
211 	max = devclass_get_maxunit(dc);
212 	while (max-- >= 0) {
213 		adapter = devclass_get_softc(dc, max);
214 		if (adapter != NULL) {
215 			rc = ena_rss_init_default(adapter);
216 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
217 			if (unlikely(rc != 0)) {
218 				ena_log(adapter->pdev, WARN,
219 				    "WARNING: RSS was not properly initialized,"
220 				    " it will affect bandwidth\n");
221 				ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
222 			}
223 		}
224 	}
225 }
226 SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
227 
228 int
229 ena_rss_indir_get(struct ena_adapter *adapter, uint32_t *table)
230 {
231 	int rc, i;
232 
233 	rc = ena_com_indirect_table_get(adapter->ena_dev, table);
234 	if (rc != 0) {
235 		if (rc == EOPNOTSUPP)
236 			device_printf(adapter->pdev,
237 			    "Reading from indirection table not supported\n");
238 		else
239 			device_printf(adapter->pdev,
240 			    "Unable to get indirection table\n");
241 		return (rc);
242 	}
243 
244 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; ++i)
245 		table[i] = ENA_IO_RXQ_IDX_TO_COMBINED_IDX(table[i]);
246 
247 	return (0);
248 }
249 
250 int
251 ena_rss_indir_set(struct ena_adapter *adapter, uint32_t *table)
252 {
253 	int rc, i;
254 
255 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; ++i) {
256 		rc = ena_com_indirect_table_fill_entry(adapter->ena_dev, i,
257 		    ENA_IO_RXQ_IDX(table[i]));
258 		if (rc != 0) {
259 			device_printf(adapter->pdev,
260 			    "Cannot fill indirection table entry %d\n", i);
261 			return (rc);
262 		}
263 	}
264 
265 	rc = ena_com_indirect_table_set(adapter->ena_dev);
266 	if (rc == EOPNOTSUPP)
267 		device_printf(adapter->pdev,
268 		    "Writing to indirection table not supported\n");
269 	else if (rc != 0)
270 		device_printf(adapter->pdev,
271 		    "Cannot set indirection table\n");
272 
273 	return (rc);
274 }
275 
276 int
277 ena_rss_indir_init(struct ena_adapter *adapter)
278 {
279 	struct ena_indir *indir = adapter->rss_indir;
280 	int rc;
281 
282 	if (indir == NULL) {
283 		adapter->rss_indir = indir = malloc(sizeof(struct ena_indir),
284 		    M_DEVBUF, M_WAITOK | M_ZERO);
285 		if (indir == NULL)
286 			return (ENOMEM);
287 	}
288 
289 	rc = ena_rss_indir_get(adapter, indir->table);
290 	if (rc != 0) {
291 		free(adapter->rss_indir, M_DEVBUF);
292 		adapter->rss_indir = NULL;
293 
294 		return (rc);
295 	}
296 
297 	ena_rss_copy_indir_buf(indir->sysctl_buf, indir->table);
298 
299 	return (0);
300 }
301