summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/huawei/hinic3/hinic3_rss.c
blob: 4ff1b2f79838aab275b55467347c8f131fc101dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.

#include <linux/ethtool.h>

#include "hinic3_cmdq.h"
#include "hinic3_hwdev.h"
#include "hinic3_hwif.h"
#include "hinic3_mbox.h"
#include "hinic3_nic_cfg.h"
#include "hinic3_nic_dev.h"
#include "hinic3_rss.h"

static void hinic3_fillout_indir_tbl(struct net_device *netdev, u16 *indir)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
	u16 i, num_qps;

	num_qps = nic_dev->q_params.num_qps;
	for (i = 0; i < L2NIC_RSS_INDIR_SIZE; i++)
		indir[i] = ethtool_rxfh_indir_default(i, num_qps);
}

static int hinic3_rss_cfg(struct hinic3_hwdev *hwdev, u8 rss_en, u16 num_qps)
{
	struct mgmt_msg_params msg_params = {};
	struct l2nic_cmd_cfg_rss rss_cfg = {};
	int err;

	rss_cfg.func_id = hinic3_global_func_id(hwdev);
	rss_cfg.rss_en = rss_en;
	rss_cfg.rq_priority_number = 0;
	rss_cfg.num_qps = num_qps;

	mgmt_msg_params_init_default(&msg_params, &rss_cfg, sizeof(rss_cfg));

	err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
				       L2NIC_CMD_CFG_RSS, &msg_params);
	if (err || rss_cfg.msg_head.status) {
		dev_err(hwdev->dev, "Failed to set rss cfg, err: %d, status: 0x%x\n",
			err, rss_cfg.msg_head.status);
		return -EINVAL;
	}

	return 0;
}

static void hinic3_init_rss_parameters(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);

	nic_dev->rss_hash_type = HINIC3_RSS_HASH_ENGINE_TYPE_XOR;
	nic_dev->rss_type.tcp_ipv6_ext = 1;
	nic_dev->rss_type.ipv6_ext = 1;
	nic_dev->rss_type.tcp_ipv6 = 1;
	nic_dev->rss_type.ipv6 = 1;
	nic_dev->rss_type.tcp_ipv4 = 1;
	nic_dev->rss_type.ipv4 = 1;
	nic_dev->rss_type.udp_ipv6 = 1;
	nic_dev->rss_type.udp_ipv4 = 1;
}

static void decide_num_qps(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
	unsigned int dev_cpus;

	dev_cpus = netif_get_num_default_rss_queues();
	nic_dev->q_params.num_qps = min(dev_cpus, nic_dev->max_qps);
}

static int alloc_rss_resource(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);

	nic_dev->rss_hkey = kmalloc_array(L2NIC_RSS_KEY_SIZE,
					  sizeof(nic_dev->rss_hkey[0]),
					  GFP_KERNEL);
	if (!nic_dev->rss_hkey)
		return -ENOMEM;

	netdev_rss_key_fill(nic_dev->rss_hkey, L2NIC_RSS_KEY_SIZE);

	nic_dev->rss_indir = kcalloc(L2NIC_RSS_INDIR_SIZE, sizeof(u16),
				     GFP_KERNEL);
	if (!nic_dev->rss_indir) {
		kfree(nic_dev->rss_hkey);
		nic_dev->rss_hkey = NULL;
		return -ENOMEM;
	}

	return 0;
}

static int hinic3_rss_set_indir_tbl(struct hinic3_hwdev *hwdev,
				    const u16 *indir_table)
{
	struct l2nic_cmd_rss_set_indir_tbl *indir_tbl;
	struct hinic3_cmd_buf *cmd_buf;
	__le64 out_param;
	int err;
	u32 i;

	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
	if (!cmd_buf) {
		dev_err(hwdev->dev, "Failed to allocate cmd buf\n");
		return -ENOMEM;
	}

	cmd_buf->size = cpu_to_le16(sizeof(struct l2nic_cmd_rss_set_indir_tbl));
	indir_tbl = cmd_buf->buf;
	memset(indir_tbl, 0, sizeof(*indir_tbl));

	for (i = 0; i < L2NIC_RSS_INDIR_SIZE; i++)
		indir_tbl->entry[i] = cpu_to_le16(indir_table[i]);

	hinic3_cmdq_buf_swab32(indir_tbl, sizeof(*indir_tbl));

	err = hinic3_cmdq_direct_resp(hwdev, MGMT_MOD_L2NIC,
				      L2NIC_UCODE_CMD_SET_RSS_INDIR_TBL,
				      cmd_buf, &out_param);
	if (err || out_param) {
		dev_err(hwdev->dev, "Failed to set rss indir table\n");
		err = -EFAULT;
	}

	hinic3_free_cmd_buf(hwdev, cmd_buf);

	return err;
}

static int hinic3_set_rss_type(struct hinic3_hwdev *hwdev,
			       struct hinic3_rss_type rss_type)
{
	struct l2nic_cmd_set_rss_ctx_tbl ctx_tbl = {};
	struct mgmt_msg_params msg_params = {};
	u32 ctx;
	int err;

	ctx_tbl.func_id = hinic3_global_func_id(hwdev);
	ctx = L2NIC_RSS_TYPE_SET(1, VALID) |
	      L2NIC_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
	      L2NIC_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
	      L2NIC_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
	      L2NIC_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
	      L2NIC_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
	      L2NIC_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
	      L2NIC_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
	      L2NIC_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
	ctx_tbl.context = ctx;

	mgmt_msg_params_init_default(&msg_params, &ctx_tbl, sizeof(ctx_tbl));

	err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
				       L2NIC_CMD_SET_RSS_CTX_TBL, &msg_params);

	if (ctx_tbl.msg_head.status == MGMT_STATUS_CMD_UNSUPPORTED) {
		return MGMT_STATUS_CMD_UNSUPPORTED;
	} else if (err || ctx_tbl.msg_head.status) {
		dev_err(hwdev->dev, "mgmt Failed to set rss context offload, err: %d, status: 0x%x\n",
			err, ctx_tbl.msg_head.status);
		return -EINVAL;
	}

	return 0;
}

static int hinic3_rss_cfg_hash_type(struct hinic3_hwdev *hwdev, u8 opcode,
				    enum hinic3_rss_hash_type *type)
{
	struct l2nic_cmd_cfg_rss_engine hash_type_cmd = {};
	struct mgmt_msg_params msg_params = {};
	int err;

	hash_type_cmd.func_id = hinic3_global_func_id(hwdev);
	hash_type_cmd.opcode = opcode;

	if (opcode == MGMT_MSG_CMD_OP_SET)
		hash_type_cmd.hash_engine = *type;

	mgmt_msg_params_init_default(&msg_params, &hash_type_cmd,
				     sizeof(hash_type_cmd));

	err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
				       L2NIC_CMD_CFG_RSS_HASH_ENGINE,
				       &msg_params);
	if (err || hash_type_cmd.msg_head.status) {
		dev_err(hwdev->dev, "Failed to %s hash engine, err: %d, status: 0x%x\n",
			opcode == MGMT_MSG_CMD_OP_SET ? "set" : "get",
			err, hash_type_cmd.msg_head.status);
		return -EIO;
	}

	if (opcode == MGMT_MSG_CMD_OP_GET)
		*type = hash_type_cmd.hash_engine;

	return 0;
}

static int hinic3_rss_set_hash_type(struct hinic3_hwdev *hwdev,
				    enum hinic3_rss_hash_type type)
{
	return hinic3_rss_cfg_hash_type(hwdev, MGMT_MSG_CMD_OP_SET, &type);
}

static int hinic3_config_rss_hw_resource(struct net_device *netdev,
					 u16 *indir_tbl)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
	int err;

	err = hinic3_rss_set_indir_tbl(nic_dev->hwdev, indir_tbl);
	if (err)
		return err;

	err = hinic3_set_rss_type(nic_dev->hwdev, nic_dev->rss_type);
	if (err)
		return err;

	return hinic3_rss_set_hash_type(nic_dev->hwdev, nic_dev->rss_hash_type);
}

static int hinic3_rss_cfg_hash_key(struct hinic3_hwdev *hwdev, u8 opcode,
				   u8 *key)
{
	struct l2nic_cmd_cfg_rss_hash_key hash_key = {};
	struct mgmt_msg_params msg_params = {};
	int err;

	hash_key.func_id = hinic3_global_func_id(hwdev);
	hash_key.opcode = opcode;

	if (opcode == MGMT_MSG_CMD_OP_SET)
		memcpy(hash_key.key, key, L2NIC_RSS_KEY_SIZE);

	mgmt_msg_params_init_default(&msg_params, &hash_key, sizeof(hash_key));

	err = hinic3_send_mbox_to_mgmt(hwdev, MGMT_MOD_L2NIC,
				       L2NIC_CMD_CFG_RSS_HASH_KEY, &msg_params);
	if (err || hash_key.msg_head.status) {
		dev_err(hwdev->dev, "Failed to %s hash key, err: %d, status: 0x%x\n",
			opcode == MGMT_MSG_CMD_OP_SET ? "set" : "get",
			err, hash_key.msg_head.status);
		return -EINVAL;
	}

	if (opcode == MGMT_MSG_CMD_OP_GET)
		memcpy(key, hash_key.key, L2NIC_RSS_KEY_SIZE);

	return 0;
}

static int hinic3_rss_set_hash_key(struct hinic3_hwdev *hwdev, u8 *key)
{
	return hinic3_rss_cfg_hash_key(hwdev, MGMT_MSG_CMD_OP_SET, key);
}

static int hinic3_set_hw_rss_parameters(struct net_device *netdev, u8 rss_en)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
	int err;

	err = hinic3_rss_set_hash_key(nic_dev->hwdev, nic_dev->rss_hkey);
	if (err)
		return err;

	hinic3_fillout_indir_tbl(netdev, nic_dev->rss_indir);

	err = hinic3_config_rss_hw_resource(netdev, nic_dev->rss_indir);
	if (err)
		return err;

	err = hinic3_rss_cfg(nic_dev->hwdev, rss_en, nic_dev->q_params.num_qps);
	if (err)
		return err;

	return 0;
}

int hinic3_rss_init(struct net_device *netdev)
{
	return hinic3_set_hw_rss_parameters(netdev, 1);
}

void hinic3_rss_uninit(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);

	hinic3_rss_cfg(nic_dev->hwdev, 0, 0);
}

void hinic3_clear_rss_config(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);

	kfree(nic_dev->rss_hkey);
	nic_dev->rss_hkey = NULL;

	kfree(nic_dev->rss_indir);
	nic_dev->rss_indir = NULL;
}

void hinic3_try_to_enable_rss(struct net_device *netdev)
{
	struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
	struct hinic3_hwdev *hwdev = nic_dev->hwdev;
	int err;

	nic_dev->max_qps = hinic3_func_max_qnum(hwdev);
	if (nic_dev->max_qps <= 1 ||
	    !hinic3_test_support(nic_dev, HINIC3_NIC_F_RSS))
		goto err_reset_q_params;

	err = alloc_rss_resource(netdev);
	if (err) {
		nic_dev->max_qps = 1;
		goto err_reset_q_params;
	}

	set_bit(HINIC3_RSS_ENABLE, &nic_dev->flags);
	decide_num_qps(netdev);
	hinic3_init_rss_parameters(netdev);
	err = hinic3_set_hw_rss_parameters(netdev, 0);
	if (err) {
		dev_err(hwdev->dev, "Failed to set hardware rss parameters\n");
		hinic3_clear_rss_config(netdev);
		nic_dev->max_qps = 1;
		goto err_reset_q_params;
	}

	return;

err_reset_q_params:
	clear_bit(HINIC3_RSS_ENABLE, &nic_dev->flags);
	nic_dev->q_params.num_qps = nic_dev->max_qps;
}