1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright (c) 2016-2017 Hisilicon Limited. 3 4 #include "hclge_main.h" 5 #include "hclge_dcb.h" 6 #include "hclge_tm.h" 7 #include "hnae3.h" 8 9 #define BW_PERCENT 100 10 11 static int hclge_ieee_ets_to_tm_info(struct hclge_dev *hdev, 12 struct ieee_ets *ets) 13 { 14 u8 i; 15 16 for (i = 0; i < HNAE3_MAX_TC; i++) { 17 switch (ets->tc_tsa[i]) { 18 case IEEE_8021QAZ_TSA_STRICT: 19 hdev->tm_info.tc_info[i].tc_sch_mode = 20 HCLGE_SCH_MODE_SP; 21 hdev->tm_info.pg_info[0].tc_dwrr[i] = 0; 22 break; 23 case IEEE_8021QAZ_TSA_ETS: 24 hdev->tm_info.tc_info[i].tc_sch_mode = 25 HCLGE_SCH_MODE_DWRR; 26 hdev->tm_info.pg_info[0].tc_dwrr[i] = 27 ets->tc_tx_bw[i]; 28 break; 29 default: 30 /* Hardware only supports SP (strict priority) 31 * or ETS (enhanced transmission selection) 32 * algorithms, if we receive some other value 33 * from dcbnl, then throw an error. 34 */ 35 return -EINVAL; 36 } 37 } 38 39 hclge_tm_prio_tc_info_update(hdev, ets->prio_tc); 40 41 return 0; 42 } 43 44 static void hclge_tm_info_to_ieee_ets(struct hclge_dev *hdev, 45 struct ieee_ets *ets) 46 { 47 u32 i; 48 49 memset(ets, 0, sizeof(*ets)); 50 ets->willing = 1; 51 ets->ets_cap = hdev->tc_max; 52 53 for (i = 0; i < HNAE3_MAX_TC; i++) { 54 ets->prio_tc[i] = hdev->tm_info.prio_tc[i]; 55 ets->tc_tx_bw[i] = hdev->tm_info.pg_info[0].tc_dwrr[i]; 56 57 if (hdev->tm_info.tc_info[i].tc_sch_mode == 58 HCLGE_SCH_MODE_SP) 59 ets->tc_tsa[i] = IEEE_8021QAZ_TSA_STRICT; 60 else 61 ets->tc_tsa[i] = IEEE_8021QAZ_TSA_ETS; 62 } 63 } 64 65 /* IEEE std */ 66 static int hclge_ieee_getets(struct hnae3_handle *h, struct ieee_ets *ets) 67 { 68 struct hclge_vport *vport = hclge_get_vport(h); 69 struct hclge_dev *hdev = vport->back; 70 71 hclge_tm_info_to_ieee_ets(hdev, ets); 72 73 return 0; 74 } 75 76 static int hclge_dcb_common_validate(struct hclge_dev *hdev, u8 num_tc, 77 u8 *prio_tc) 78 { 79 int i; 80 81 if (num_tc > hdev->tc_max) { 82 dev_err(&hdev->pdev->dev, 83 "tc num checking failed, %u > tc_max(%u)\n", 84 num_tc, hdev->tc_max); 85 return -EINVAL; 86 } 87 88 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) { 89 if (prio_tc[i] >= num_tc) { 90 dev_err(&hdev->pdev->dev, 91 "prio_tc[%d] checking failed, %u >= num_tc(%u)\n", 92 i, prio_tc[i], num_tc); 93 return -EINVAL; 94 } 95 } 96 97 if (num_tc > hdev->vport[0].alloc_tqps) { 98 dev_err(&hdev->pdev->dev, 99 "allocated tqp checking failed, %u > tqp(%u)\n", 100 num_tc, hdev->vport[0].alloc_tqps); 101 return -EINVAL; 102 } 103 104 return 0; 105 } 106 107 static u8 hclge_ets_tc_changed(struct hclge_dev *hdev, struct ieee_ets *ets, 108 bool *changed) 109 { 110 u8 max_tc_id = 0; 111 u8 i; 112 113 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) { 114 if (ets->prio_tc[i] != hdev->tm_info.prio_tc[i]) 115 *changed = true; 116 117 if (ets->prio_tc[i] > max_tc_id) 118 max_tc_id = ets->prio_tc[i]; 119 } 120 121 /* return max tc number, max tc id need to plus 1 */ 122 return max_tc_id + 1; 123 } 124 125 static int hclge_ets_sch_mode_validate(struct hclge_dev *hdev, 126 struct ieee_ets *ets, bool *changed) 127 { 128 bool has_ets_tc = false; 129 u32 total_ets_bw = 0; 130 u8 i; 131 132 for (i = 0; i < hdev->tc_max; i++) { 133 switch (ets->tc_tsa[i]) { 134 case IEEE_8021QAZ_TSA_STRICT: 135 if (hdev->tm_info.tc_info[i].tc_sch_mode != 136 HCLGE_SCH_MODE_SP) 137 *changed = true; 138 break; 139 case IEEE_8021QAZ_TSA_ETS: 140 if (hdev->tm_info.tc_info[i].tc_sch_mode != 141 HCLGE_SCH_MODE_DWRR) 142 *changed = true; 143 144 total_ets_bw += ets->tc_tx_bw[i]; 145 has_ets_tc = true; 146 break; 147 default: 148 return -EINVAL; 149 } 150 } 151 152 if (has_ets_tc && total_ets_bw != BW_PERCENT) 153 return -EINVAL; 154 155 return 0; 156 } 157 158 static int hclge_ets_validate(struct hclge_dev *hdev, struct ieee_ets *ets, 159 u8 *tc, bool *changed) 160 { 161 u8 tc_num; 162 int ret; 163 164 tc_num = hclge_ets_tc_changed(hdev, ets, changed); 165 166 ret = hclge_dcb_common_validate(hdev, tc_num, ets->prio_tc); 167 if (ret) 168 return ret; 169 170 ret = hclge_ets_sch_mode_validate(hdev, ets, changed); 171 if (ret) 172 return ret; 173 174 *tc = tc_num; 175 if (*tc != hdev->tm_info.num_tc) 176 *changed = true; 177 178 return 0; 179 } 180 181 static int hclge_map_update(struct hclge_dev *hdev) 182 { 183 int ret; 184 185 ret = hclge_tm_schd_setup_hw(hdev); 186 if (ret) 187 return ret; 188 189 ret = hclge_pause_setup_hw(hdev, false); 190 if (ret) 191 return ret; 192 193 ret = hclge_buffer_alloc(hdev); 194 if (ret) 195 return ret; 196 197 hclge_rss_indir_init_cfg(hdev); 198 199 return hclge_rss_init_hw(hdev); 200 } 201 202 static int hclge_notify_down_uinit(struct hclge_dev *hdev) 203 { 204 int ret; 205 206 ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT); 207 if (ret) 208 return ret; 209 210 return hclge_notify_client(hdev, HNAE3_UNINIT_CLIENT); 211 } 212 213 static int hclge_notify_init_up(struct hclge_dev *hdev) 214 { 215 int ret; 216 217 ret = hclge_notify_client(hdev, HNAE3_INIT_CLIENT); 218 if (ret) 219 return ret; 220 221 return hclge_notify_client(hdev, HNAE3_UP_CLIENT); 222 } 223 224 static int hclge_ieee_setets(struct hnae3_handle *h, struct ieee_ets *ets) 225 { 226 struct hclge_vport *vport = hclge_get_vport(h); 227 struct net_device *netdev = h->kinfo.netdev; 228 struct hclge_dev *hdev = vport->back; 229 bool map_changed = false; 230 u8 num_tc = 0; 231 int ret; 232 233 if (!(hdev->dcbx_cap & DCB_CAP_DCBX_VER_IEEE) || 234 hdev->flag & HCLGE_FLAG_MQPRIO_ENABLE) 235 return -EINVAL; 236 237 ret = hclge_ets_validate(hdev, ets, &num_tc, &map_changed); 238 if (ret) 239 return ret; 240 241 if (map_changed) { 242 netif_dbg(h, drv, netdev, "set ets\n"); 243 244 ret = hclge_notify_down_uinit(hdev); 245 if (ret) 246 return ret; 247 } 248 249 hclge_tm_schd_info_update(hdev, num_tc); 250 if (num_tc > 1) 251 hdev->flag |= HCLGE_FLAG_DCB_ENABLE; 252 else 253 hdev->flag &= ~HCLGE_FLAG_DCB_ENABLE; 254 255 ret = hclge_ieee_ets_to_tm_info(hdev, ets); 256 if (ret) 257 goto err_out; 258 259 if (map_changed) { 260 ret = hclge_map_update(hdev); 261 if (ret) 262 goto err_out; 263 264 return hclge_notify_init_up(hdev); 265 } 266 267 return hclge_tm_dwrr_cfg(hdev); 268 269 err_out: 270 if (!map_changed) 271 return ret; 272 273 hclge_notify_init_up(hdev); 274 275 return ret; 276 } 277 278 static int hclge_ieee_getpfc(struct hnae3_handle *h, struct ieee_pfc *pfc) 279 { 280 u64 requests[HNAE3_MAX_TC], indications[HNAE3_MAX_TC]; 281 struct hclge_vport *vport = hclge_get_vport(h); 282 struct hclge_dev *hdev = vport->back; 283 int ret; 284 u8 i; 285 286 memset(pfc, 0, sizeof(*pfc)); 287 pfc->pfc_cap = hdev->pfc_max; 288 pfc->pfc_en = hdev->tm_info.pfc_en; 289 290 ret = hclge_pfc_tx_stats_get(hdev, requests); 291 if (ret) 292 return ret; 293 294 ret = hclge_pfc_rx_stats_get(hdev, indications); 295 if (ret) 296 return ret; 297 298 for (i = 0; i < HCLGE_MAX_TC_NUM; i++) { 299 pfc->requests[i] = requests[i]; 300 pfc->indications[i] = indications[i]; 301 } 302 return 0; 303 } 304 305 static int hclge_ieee_setpfc(struct hnae3_handle *h, struct ieee_pfc *pfc) 306 { 307 struct hclge_vport *vport = hclge_get_vport(h); 308 struct net_device *netdev = h->kinfo.netdev; 309 struct hclge_dev *hdev = vport->back; 310 u8 i, j, pfc_map, *prio_tc; 311 int ret; 312 313 if (!(hdev->dcbx_cap & DCB_CAP_DCBX_VER_IEEE)) 314 return -EINVAL; 315 316 if (pfc->pfc_en == hdev->tm_info.pfc_en) 317 return 0; 318 319 prio_tc = hdev->tm_info.prio_tc; 320 pfc_map = 0; 321 322 for (i = 0; i < hdev->tm_info.num_tc; i++) { 323 for (j = 0; j < HNAE3_MAX_USER_PRIO; j++) { 324 if ((prio_tc[j] == i) && (pfc->pfc_en & BIT(j))) { 325 pfc_map |= BIT(i); 326 break; 327 } 328 } 329 } 330 331 hdev->tm_info.hw_pfc_map = pfc_map; 332 hdev->tm_info.pfc_en = pfc->pfc_en; 333 334 netif_dbg(h, drv, netdev, 335 "set pfc: pfc_en=%x, pfc_map=%x, num_tc=%u\n", 336 pfc->pfc_en, pfc_map, hdev->tm_info.num_tc); 337 338 hclge_tm_pfc_info_update(hdev); 339 340 ret = hclge_pause_setup_hw(hdev, false); 341 if (ret) 342 return ret; 343 344 ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT); 345 if (ret) 346 return ret; 347 348 ret = hclge_buffer_alloc(hdev); 349 if (ret) { 350 hclge_notify_client(hdev, HNAE3_UP_CLIENT); 351 return ret; 352 } 353 354 return hclge_notify_client(hdev, HNAE3_UP_CLIENT); 355 } 356 357 /* DCBX configuration */ 358 static u8 hclge_getdcbx(struct hnae3_handle *h) 359 { 360 struct hclge_vport *vport = hclge_get_vport(h); 361 struct hclge_dev *hdev = vport->back; 362 363 if (hdev->flag & HCLGE_FLAG_MQPRIO_ENABLE) 364 return 0; 365 366 return hdev->dcbx_cap; 367 } 368 369 static u8 hclge_setdcbx(struct hnae3_handle *h, u8 mode) 370 { 371 struct hclge_vport *vport = hclge_get_vport(h); 372 struct net_device *netdev = h->kinfo.netdev; 373 struct hclge_dev *hdev = vport->back; 374 375 netif_dbg(h, drv, netdev, "set dcbx: mode=%u\n", mode); 376 377 /* No support for LLD_MANAGED modes or CEE */ 378 if ((mode & DCB_CAP_DCBX_LLD_MANAGED) || 379 (mode & DCB_CAP_DCBX_VER_CEE) || 380 !(mode & DCB_CAP_DCBX_HOST)) 381 return 1; 382 383 hdev->dcbx_cap = mode; 384 385 return 0; 386 } 387 388 static int hclge_mqprio_qopt_check(struct hclge_dev *hdev, 389 struct tc_mqprio_qopt_offload *mqprio_qopt) 390 { 391 u16 queue_sum = 0; 392 int ret; 393 int i; 394 395 if (!mqprio_qopt->qopt.num_tc) { 396 mqprio_qopt->qopt.num_tc = 1; 397 return 0; 398 } 399 400 ret = hclge_dcb_common_validate(hdev, mqprio_qopt->qopt.num_tc, 401 mqprio_qopt->qopt.prio_tc_map); 402 if (ret) 403 return ret; 404 405 for (i = 0; i < mqprio_qopt->qopt.num_tc; i++) { 406 if (!is_power_of_2(mqprio_qopt->qopt.count[i])) { 407 dev_err(&hdev->pdev->dev, 408 "qopt queue count must be power of 2\n"); 409 return -EINVAL; 410 } 411 412 if (mqprio_qopt->qopt.count[i] > hdev->pf_rss_size_max) { 413 dev_err(&hdev->pdev->dev, 414 "qopt queue count should be no more than %u\n", 415 hdev->pf_rss_size_max); 416 return -EINVAL; 417 } 418 419 if (mqprio_qopt->qopt.offset[i] != queue_sum) { 420 dev_err(&hdev->pdev->dev, 421 "qopt queue offset must start from 0, and being continuous\n"); 422 return -EINVAL; 423 } 424 425 if (mqprio_qopt->min_rate[i] || mqprio_qopt->max_rate[i]) { 426 dev_err(&hdev->pdev->dev, 427 "qopt tx_rate is not supported\n"); 428 return -EOPNOTSUPP; 429 } 430 431 queue_sum = mqprio_qopt->qopt.offset[i]; 432 queue_sum += mqprio_qopt->qopt.count[i]; 433 } 434 if (hdev->vport[0].alloc_tqps < queue_sum) { 435 dev_err(&hdev->pdev->dev, 436 "qopt queue count sum should be less than %u\n", 437 hdev->vport[0].alloc_tqps); 438 return -EINVAL; 439 } 440 441 return 0; 442 } 443 444 static void hclge_sync_mqprio_qopt(struct hnae3_tc_info *tc_info, 445 struct tc_mqprio_qopt_offload *mqprio_qopt) 446 { 447 memset(tc_info, 0, sizeof(*tc_info)); 448 tc_info->num_tc = mqprio_qopt->qopt.num_tc; 449 memcpy(tc_info->prio_tc, mqprio_qopt->qopt.prio_tc_map, 450 sizeof_field(struct hnae3_tc_info, prio_tc)); 451 memcpy(tc_info->tqp_count, mqprio_qopt->qopt.count, 452 sizeof_field(struct hnae3_tc_info, tqp_count)); 453 memcpy(tc_info->tqp_offset, mqprio_qopt->qopt.offset, 454 sizeof_field(struct hnae3_tc_info, tqp_offset)); 455 } 456 457 static int hclge_config_tc(struct hclge_dev *hdev, 458 struct hnae3_tc_info *tc_info) 459 { 460 int i; 461 462 hclge_tm_schd_info_update(hdev, tc_info->num_tc); 463 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) 464 hdev->tm_info.prio_tc[i] = tc_info->prio_tc[i]; 465 466 return hclge_map_update(hdev); 467 } 468 469 /* Set up TC for hardware offloaded mqprio in channel mode */ 470 static int hclge_setup_tc(struct hnae3_handle *h, 471 struct tc_mqprio_qopt_offload *mqprio_qopt) 472 { 473 struct hclge_vport *vport = hclge_get_vport(h); 474 struct hnae3_knic_private_info *kinfo; 475 struct hclge_dev *hdev = vport->back; 476 struct hnae3_tc_info old_tc_info; 477 u8 tc = mqprio_qopt->qopt.num_tc; 478 int ret; 479 480 /* if client unregistered, it's not allowed to change 481 * mqprio configuration, which may cause uninit ring 482 * fail. 483 */ 484 if (!test_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state)) 485 return -EBUSY; 486 487 if (hdev->flag & HCLGE_FLAG_DCB_ENABLE) 488 return -EINVAL; 489 490 ret = hclge_mqprio_qopt_check(hdev, mqprio_qopt); 491 if (ret) { 492 dev_err(&hdev->pdev->dev, 493 "failed to check mqprio qopt params, ret = %d\n", ret); 494 return ret; 495 } 496 497 ret = hclge_notify_down_uinit(hdev); 498 if (ret) 499 return ret; 500 501 kinfo = &vport->nic.kinfo; 502 memcpy(&old_tc_info, &kinfo->tc_info, sizeof(old_tc_info)); 503 hclge_sync_mqprio_qopt(&kinfo->tc_info, mqprio_qopt); 504 kinfo->tc_info.mqprio_active = tc > 0; 505 506 ret = hclge_config_tc(hdev, &kinfo->tc_info); 507 if (ret) 508 goto err_out; 509 510 hdev->flag &= ~HCLGE_FLAG_DCB_ENABLE; 511 512 if (tc > 1) 513 hdev->flag |= HCLGE_FLAG_MQPRIO_ENABLE; 514 else 515 hdev->flag &= ~HCLGE_FLAG_MQPRIO_ENABLE; 516 517 return hclge_notify_init_up(hdev); 518 519 err_out: 520 if (!tc) { 521 dev_warn(&hdev->pdev->dev, 522 "failed to destroy mqprio, will active after reset, ret = %d\n", 523 ret); 524 } else { 525 /* roll-back */ 526 memcpy(&kinfo->tc_info, &old_tc_info, sizeof(old_tc_info)); 527 if (hclge_config_tc(hdev, &kinfo->tc_info)) 528 dev_err(&hdev->pdev->dev, 529 "failed to roll back tc configuration\n"); 530 } 531 hclge_notify_init_up(hdev); 532 533 return ret; 534 } 535 536 static const struct hnae3_dcb_ops hns3_dcb_ops = { 537 .ieee_getets = hclge_ieee_getets, 538 .ieee_setets = hclge_ieee_setets, 539 .ieee_getpfc = hclge_ieee_getpfc, 540 .ieee_setpfc = hclge_ieee_setpfc, 541 .getdcbx = hclge_getdcbx, 542 .setdcbx = hclge_setdcbx, 543 .setup_tc = hclge_setup_tc, 544 }; 545 546 void hclge_dcb_ops_set(struct hclge_dev *hdev) 547 { 548 struct hclge_vport *vport = hdev->vport; 549 struct hnae3_knic_private_info *kinfo; 550 551 /* Hdev does not support DCB or vport is 552 * not a pf, then dcb_ops is not set. 553 */ 554 if (!hnae3_dev_dcb_supported(hdev) || 555 vport->vport_id != 0) 556 return; 557 558 kinfo = &vport->nic.kinfo; 559 kinfo->dcb_ops = &hns3_dcb_ops; 560 hdev->dcbx_cap = DCB_CAP_DCBX_VER_IEEE | DCB_CAP_DCBX_HOST; 561 } 562