1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
6 #include <rte_ethdev.h>
8 #include <rte_malloc.h>
9 #include <rte_memcpy.h>
10 #include <rte_spinlock.h>
12 #include "hns3_ethdev.h"
13 #include "hns3_logs.h"
16 * The hash key used for rss initialization.
18 static const uint8_t hns3_hash_key[] = {
19 0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
20 0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
21 0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
22 0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
23 0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
27 * rss_generic_config command function, opcode:0x0D01.
28 * Used to set algorithm, key_offset and hash key of rss.
31 hns3_set_rss_algo_key(struct hns3_hw *hw, uint8_t hash_algo, const uint8_t *key)
33 #define HNS3_KEY_OFFSET_MAX 3
34 #define HNS3_SET_HASH_KEY_BYTE_FOUR 2
36 struct hns3_rss_generic_config_cmd *req;
37 struct hns3_cmd_desc desc;
38 uint32_t key_offset, key_size;
39 const uint8_t *key_cur;
43 req = (struct hns3_rss_generic_config_cmd *)desc.data;
46 * key_offset=0, hash key byte0~15 is set to hardware.
47 * key_offset=1, hash key byte16~31 is set to hardware.
48 * key_offset=2, hash key byte32~39 is set to hardware.
50 for (key_offset = 0; key_offset < HNS3_KEY_OFFSET_MAX; key_offset++) {
51 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_GENERIC_CONFIG,
54 req->hash_config |= (hash_algo & HNS3_RSS_HASH_ALGO_MASK);
55 req->hash_config |= (key_offset << HNS3_RSS_HASH_KEY_OFFSET_B);
57 if (key_offset == HNS3_SET_HASH_KEY_BYTE_FOUR)
58 key_size = HNS3_RSS_KEY_SIZE - HNS3_RSS_HASH_KEY_NUM *
59 HNS3_SET_HASH_KEY_BYTE_FOUR;
61 key_size = HNS3_RSS_HASH_KEY_NUM;
63 cur_offset = key_offset * HNS3_RSS_HASH_KEY_NUM;
64 key_cur = key + cur_offset;
65 memcpy(req->hash_key, key_cur, key_size);
67 ret = hns3_cmd_send(hw, &desc, 1);
69 hns3_err(hw, "Configure RSS algo key failed %d", ret);
73 /* Update the shadow RSS key with user specified */
74 memcpy(hw->rss_info.key, key, HNS3_RSS_KEY_SIZE);
79 * Used to configure the tuple selection for RSS hash input.
82 hns3_set_rss_input_tuple(struct hns3_hw *hw)
84 struct hns3_rss_conf *rss_config = &hw->rss_info;
85 struct hns3_rss_input_tuple_cmd *req;
86 struct hns3_cmd_desc desc_tuple;
89 hns3_cmd_setup_basic_desc(&desc_tuple, HNS3_OPC_RSS_INPUT_TUPLE, false);
91 req = (struct hns3_rss_input_tuple_cmd *)desc_tuple.data;
93 req->ipv4_tcp_en = rss_config->rss_tuple_sets.ipv4_tcp_en;
94 req->ipv4_udp_en = rss_config->rss_tuple_sets.ipv4_udp_en;
95 req->ipv4_sctp_en = rss_config->rss_tuple_sets.ipv4_sctp_en;
96 req->ipv4_fragment_en = rss_config->rss_tuple_sets.ipv4_fragment_en;
97 req->ipv6_tcp_en = rss_config->rss_tuple_sets.ipv6_tcp_en;
98 req->ipv6_udp_en = rss_config->rss_tuple_sets.ipv6_udp_en;
99 req->ipv6_sctp_en = rss_config->rss_tuple_sets.ipv6_sctp_en;
100 req->ipv6_fragment_en = rss_config->rss_tuple_sets.ipv6_fragment_en;
102 ret = hns3_cmd_send(hw, &desc_tuple, 1);
104 hns3_err(hw, "Configure RSS input tuple mode failed %d", ret);
110 * rss_indirection_table command function, opcode:0x0D07.
111 * Used to configure the indirection table of rss.
114 hns3_set_rss_indir_table(struct hns3_hw *hw, uint8_t *indir, uint16_t size)
116 struct hns3_rss_indirection_table_cmd *req;
117 struct hns3_cmd_desc desc;
120 req = (struct hns3_rss_indirection_table_cmd *)desc.data;
122 for (i = 0; i < size / HNS3_RSS_CFG_TBL_SIZE; i++) {
123 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INDIR_TABLE,
125 req->start_table_index =
126 rte_cpu_to_le_16(i * HNS3_RSS_CFG_TBL_SIZE);
127 req->rss_set_bitmap = rte_cpu_to_le_16(HNS3_RSS_SET_BITMAP_MSK);
128 for (j = 0; j < HNS3_RSS_CFG_TBL_SIZE; j++) {
129 num = i * HNS3_RSS_CFG_TBL_SIZE + j;
130 req->rss_result[j] = indir[num] % hw->alloc_rss_size;
132 ret = hns3_cmd_send(hw, &desc, 1);
135 "Sets RSS indirection table failed %d size %u",
141 /* Update redirection table of hw */
142 memcpy(hw->rss_info.rss_indirection_tbl, indir, HNS3_RSS_IND_TBL_SIZE);
148 hns3_rss_reset_indir_table(struct hns3_hw *hw)
153 lut = rte_zmalloc("hns3_rss_lut", HNS3_RSS_IND_TBL_SIZE, 0);
155 hns3_err(hw, "No hns3_rss_lut memory can be allocated");
159 ret = hns3_set_rss_indir_table(hw, lut, HNS3_RSS_IND_TBL_SIZE);
161 hns3_err(hw, "RSS uninit indir table failed: %d", ret);
168 hns3_set_rss_tuple_by_rss_hf(struct hns3_hw *hw,
169 struct hns3_rss_tuple_cfg *tuple, uint64_t rss_hf)
171 struct hns3_rss_input_tuple_cmd *req;
172 struct hns3_cmd_desc desc;
176 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
178 req = (struct hns3_rss_input_tuple_cmd *)desc.data;
180 /* Enable ipv4 or ipv6 tuple by flow type */
181 for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
182 switch (rss_hf & (1ULL << i)) {
183 case ETH_RSS_NONFRAG_IPV4_TCP:
184 req->ipv4_tcp_en = HNS3_RSS_INPUT_TUPLE_OTHER;
186 case ETH_RSS_NONFRAG_IPV4_UDP:
187 req->ipv4_udp_en = HNS3_RSS_INPUT_TUPLE_OTHER;
189 case ETH_RSS_NONFRAG_IPV4_SCTP:
190 req->ipv4_sctp_en = HNS3_RSS_INPUT_TUPLE_SCTP;
192 case ETH_RSS_FRAG_IPV4:
193 req->ipv4_fragment_en |= HNS3_IP_FRAG_BIT_MASK;
195 case ETH_RSS_NONFRAG_IPV4_OTHER:
196 req->ipv4_fragment_en |= HNS3_IP_OTHER_BIT_MASK;
198 case ETH_RSS_NONFRAG_IPV6_TCP:
199 req->ipv6_tcp_en = HNS3_RSS_INPUT_TUPLE_OTHER;
201 case ETH_RSS_NONFRAG_IPV6_UDP:
202 req->ipv6_udp_en = HNS3_RSS_INPUT_TUPLE_OTHER;
204 case ETH_RSS_NONFRAG_IPV6_SCTP:
205 req->ipv6_sctp_en = HNS3_RSS_INPUT_TUPLE_SCTP;
207 case ETH_RSS_FRAG_IPV6:
208 req->ipv6_fragment_en |= HNS3_IP_FRAG_BIT_MASK;
210 case ETH_RSS_NONFRAG_IPV6_OTHER:
211 req->ipv6_fragment_en |= HNS3_IP_OTHER_BIT_MASK;
215 * rss_hf doesn't include unsupported flow types
216 * because the API framework has checked it, and
217 * this branch will never go unless rss_hf is zero.
223 ret = hns3_cmd_send(hw, &desc, 1);
225 hns3_err(hw, "Update RSS flow types tuples failed %d", ret);
229 tuple->ipv4_tcp_en = req->ipv4_tcp_en;
230 tuple->ipv4_udp_en = req->ipv4_udp_en;
231 tuple->ipv4_sctp_en = req->ipv4_sctp_en;
232 tuple->ipv4_fragment_en = req->ipv4_fragment_en;
233 tuple->ipv6_tcp_en = req->ipv6_tcp_en;
234 tuple->ipv6_udp_en = req->ipv6_udp_en;
235 tuple->ipv6_sctp_en = req->ipv6_sctp_en;
236 tuple->ipv6_fragment_en = req->ipv6_fragment_en;
242 * Configure RSS hash protocols and hash key.
244 * Pointer to Ethernet device.
246 * The configuration select of rss key size and tuple flow_types.
248 * 0 on success, a negative errno value otherwise is set.
251 hns3_dev_rss_hash_update(struct rte_eth_dev *dev,
252 struct rte_eth_rss_conf *rss_conf)
254 struct hns3_adapter *hns = dev->data->dev_private;
255 struct hns3_hw *hw = &hns->hw;
256 struct hns3_rss_tuple_cfg *tuple = &hw->rss_info.rss_tuple_sets;
257 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
258 uint8_t key_len = rss_conf->rss_key_len;
260 uint64_t rss_hf = rss_conf->rss_hf;
261 uint8_t *key = rss_conf->rss_key;
264 rte_spinlock_lock(&hw->lock);
265 ret = hns3_set_rss_tuple_by_rss_hf(hw, tuple, rss_hf);
269 if (rss_cfg->conf.types && rss_hf == 0) {
270 /* Disable RSS, reset indirection table by local variable */
271 ret = hns3_rss_reset_indir_table(hw);
274 } else if (rss_hf && rss_cfg->conf.types == 0) {
275 /* Enable RSS, restore indirection table by hw's config */
276 ret = hns3_set_rss_indir_table(hw, rss_cfg->rss_indirection_tbl,
277 HNS3_RSS_IND_TBL_SIZE);
282 /* Update supported flow types when set tuple success */
283 rss_cfg->conf.types = rss_hf;
286 if (key_len != HNS3_RSS_KEY_SIZE) {
287 hns3_err(hw, "The hash key len(%u) is invalid",
292 algo = rss_cfg->conf.func == RTE_ETH_HASH_FUNCTION_SIMPLE_XOR ?
293 HNS3_RSS_HASH_ALGO_SIMPLE : HNS3_RSS_HASH_ALGO_TOEPLITZ;
294 ret = hns3_set_rss_algo_key(hw, algo, key);
298 rte_spinlock_unlock(&hw->lock);
303 rte_spinlock_unlock(&hw->lock);
308 * Get rss key and rss_hf types set of RSS hash configuration.
310 * Pointer to Ethernet device.
312 * The buffer to get rss key size and tuple types.
317 hns3_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
318 struct rte_eth_rss_conf *rss_conf)
320 struct hns3_adapter *hns = dev->data->dev_private;
321 struct hns3_hw *hw = &hns->hw;
322 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
324 rte_spinlock_lock(&hw->lock);
325 rss_conf->rss_hf = rss_cfg->conf.types;
327 /* Get the RSS Key required by the user */
328 if (rss_conf->rss_key)
329 memcpy(rss_conf->rss_key, rss_cfg->key, HNS3_RSS_KEY_SIZE);
330 rte_spinlock_unlock(&hw->lock);
336 * Update rss redirection table of RSS.
338 * Pointer to Ethernet device.
340 * Pointer to the configuration select of mask and redirection tables.
342 * Redirection table size.
344 * 0 on success, a negative errno value otherwise is set.
347 hns3_dev_rss_reta_update(struct rte_eth_dev *dev,
348 struct rte_eth_rss_reta_entry64 *reta_conf,
351 struct hns3_adapter *hns = dev->data->dev_private;
352 struct hns3_hw *hw = &hns->hw;
353 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
354 uint16_t i, indir_size = HNS3_RSS_IND_TBL_SIZE; /* Table size is 512 */
355 uint8_t indirection_tbl[HNS3_RSS_IND_TBL_SIZE];
356 uint16_t idx, shift, allow_rss_queues;
359 if (reta_size != indir_size || reta_size > ETH_RSS_RETA_SIZE_512) {
360 hns3_err(hw, "The size of hash lookup table configured (%u)"
361 "doesn't match the number hardware can supported"
362 "(%u)", reta_size, indir_size);
365 rte_spinlock_lock(&hw->lock);
366 memcpy(indirection_tbl, rss_cfg->rss_indirection_tbl,
367 HNS3_RSS_IND_TBL_SIZE);
368 allow_rss_queues = RTE_MIN(dev->data->nb_rx_queues, hw->rss_size_max);
369 for (i = 0; i < reta_size; i++) {
370 idx = i / RTE_RETA_GROUP_SIZE;
371 shift = i % RTE_RETA_GROUP_SIZE;
372 if (reta_conf[idx].reta[shift] >= allow_rss_queues) {
373 rte_spinlock_unlock(&hw->lock);
374 hns3_err(hw, "Invalid queue id(%u) to be set in "
375 "redirection table, max number of rss "
376 "queues: %u", reta_conf[idx].reta[shift],
381 if (reta_conf[idx].mask & (1ULL << shift))
382 indirection_tbl[i] = reta_conf[idx].reta[shift];
385 ret = hns3_set_rss_indir_table(hw, indirection_tbl,
386 HNS3_RSS_IND_TBL_SIZE);
388 rte_spinlock_unlock(&hw->lock);
393 * Get rss redirection table of RSS hash configuration.
395 * Pointer to Ethernet device.
397 * Pointer to the configuration select of mask and redirection tables.
399 * Redirection table size.
401 * 0 on success, a negative errno value otherwise is set.
404 hns3_dev_rss_reta_query(struct rte_eth_dev *dev,
405 struct rte_eth_rss_reta_entry64 *reta_conf,
408 struct hns3_adapter *hns = dev->data->dev_private;
409 struct hns3_hw *hw = &hns->hw;
410 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
411 uint16_t i, indir_size = HNS3_RSS_IND_TBL_SIZE; /* Table size is 512 */
414 if (reta_size != indir_size || reta_size > ETH_RSS_RETA_SIZE_512) {
415 hns3_err(hw, "The size of hash lookup table configured (%u)"
416 " doesn't match the number hardware can supported"
417 "(%u)", reta_size, indir_size);
420 rte_spinlock_lock(&hw->lock);
421 for (i = 0; i < reta_size; i++) {
422 idx = i / RTE_RETA_GROUP_SIZE;
423 shift = i % RTE_RETA_GROUP_SIZE;
424 if (reta_conf[idx].mask & (1ULL << shift))
425 reta_conf[idx].reta[shift] =
426 rss_cfg->rss_indirection_tbl[i] % hw->alloc_rss_size;
428 rte_spinlock_unlock(&hw->lock);
433 * Used to configure the tc_size and tc_offset.
436 hns3_set_rss_tc_mode(struct hns3_hw *hw)
438 uint16_t rss_size = hw->alloc_rss_size;
439 struct hns3_rss_tc_mode_cmd *req;
440 uint16_t tc_offset[HNS3_MAX_TC_NUM];
441 uint8_t tc_valid[HNS3_MAX_TC_NUM];
442 uint16_t tc_size[HNS3_MAX_TC_NUM];
443 struct hns3_cmd_desc desc;
444 uint16_t roundup_size;
448 req = (struct hns3_rss_tc_mode_cmd *)desc.data;
450 roundup_size = roundup_pow_of_two(rss_size);
451 roundup_size = ilog2(roundup_size);
453 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
454 tc_valid[i] = !!(hw->hw_tc_map & BIT(i));
455 tc_size[i] = roundup_size;
456 tc_offset[i] = rss_size * i;
459 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_TC_MODE, false);
460 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
463 hns3_set_bit(mode, HNS3_RSS_TC_VALID_B, (tc_valid[i] & 0x1));
464 hns3_set_field(mode, HNS3_RSS_TC_SIZE_M, HNS3_RSS_TC_SIZE_S,
466 hns3_set_field(mode, HNS3_RSS_TC_OFFSET_M, HNS3_RSS_TC_OFFSET_S,
469 req->rss_tc_mode[i] = rte_cpu_to_le_16(mode);
471 ret = hns3_cmd_send(hw, &desc, 1);
473 hns3_err(hw, "Sets rss tc mode failed %d", ret);
479 hns3_rss_tuple_uninit(struct hns3_hw *hw)
481 struct hns3_rss_input_tuple_cmd *req;
482 struct hns3_cmd_desc desc;
485 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
487 req = (struct hns3_rss_input_tuple_cmd *)desc.data;
489 memset(req, 0, sizeof(struct hns3_rss_tuple_cfg));
491 ret = hns3_cmd_send(hw, &desc, 1);
493 hns3_err(hw, "RSS uninit tuple failed %d", ret);
499 * Set the default rss configuration in the init of driver.
502 hns3_set_default_rss_args(struct hns3_hw *hw)
504 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
505 uint16_t queue_num = hw->alloc_rss_size;
508 /* Default hash algorithm */
509 rss_cfg->conf.func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
511 /* Default RSS key */
512 memcpy(rss_cfg->key, hns3_hash_key, HNS3_RSS_KEY_SIZE);
514 /* Initialize RSS indirection table */
515 for (i = 0; i < HNS3_RSS_IND_TBL_SIZE; i++)
516 rss_cfg->rss_indirection_tbl[i] = i % queue_num;
520 * RSS initialization for hns3 pmd driver.
523 hns3_config_rss(struct hns3_adapter *hns)
525 struct hns3_hw *hw = &hns->hw;
526 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
528 (hw->rss_info.conf.func == RTE_ETH_HASH_FUNCTION_TOEPLITZ ?
529 HNS3_RSS_HASH_ALGO_TOEPLITZ : HNS3_RSS_HASH_ALGO_SIMPLE);
530 uint8_t *hash_key = rss_cfg->key;
533 enum rte_eth_rx_mq_mode mq_mode = hw->data->dev_conf.rxmode.mq_mode;
535 /* When there is no open RSS, redirect the packet queue 0 */
536 if (((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) == 0)
537 hns3_rss_uninit(hns);
539 /* Configure RSS hash algorithm and hash key offset */
540 ret = hns3_set_rss_algo_key(hw, hash_algo, hash_key);
544 /* Configure the tuple selection for RSS hash input */
545 ret = hns3_set_rss_input_tuple(hw);
549 ret = hns3_set_rss_indir_table(hw, rss_cfg->rss_indirection_tbl,
550 HNS3_RSS_IND_TBL_SIZE);
552 goto rss_tuple_uninit;
554 ret = hns3_set_rss_tc_mode(hw);
556 goto rss_indir_table_uninit;
560 rss_indir_table_uninit:
561 ret1 = hns3_rss_reset_indir_table(hw);
566 hns3_rss_tuple_uninit(hw);
569 hw->rss_info.conf.types = 0;
575 * RSS uninitialization for hns3 pmd driver.
578 hns3_rss_uninit(struct hns3_adapter *hns)
580 struct hns3_hw *hw = &hns->hw;
583 hns3_rss_tuple_uninit(hw);
584 ret = hns3_rss_reset_indir_table(hw);
589 hw->rss_info.conf.types = 0;