1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Huawei HiNIC PCI Express Linux driver 4 * Copyright(c) 2017 Huawei Technologies Co., Ltd 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/types.h> 9 #include <linux/pci.h> 10 #include <linux/device.h> 11 #include <linux/errno.h> 12 #include <linux/slab.h> 13 #include <linux/bitops.h> 14 #include <linux/delay.h> 15 #include <linux/jiffies.h> 16 #include <linux/log2.h> 17 #include <linux/err.h> 18 #include <linux/netdevice.h> 19 #include <net/devlink.h> 20 21 #include "hinic_devlink.h" 22 #include "hinic_sriov.h" 23 #include "hinic_dev.h" 24 #include "hinic_hw_if.h" 25 #include "hinic_hw_eqs.h" 26 #include "hinic_hw_mgmt.h" 27 #include "hinic_hw_qp_ctxt.h" 28 #include "hinic_hw_qp.h" 29 #include "hinic_hw_io.h" 30 #include "hinic_hw_dev.h" 31 32 #define IO_STATUS_TIMEOUT 100 33 #define OUTBOUND_STATE_TIMEOUT 100 34 #define DB_STATE_TIMEOUT 100 35 36 #define MAX_IRQS(max_qps, num_aeqs, num_ceqs) \ 37 (2 * (max_qps) + (num_aeqs) + (num_ceqs)) 38 39 #define ADDR_IN_4BYTES(addr) ((addr) >> 2) 40 41 enum intr_type { 42 INTR_MSIX_TYPE, 43 }; 44 45 enum io_status { 46 IO_STOPPED = 0, 47 IO_RUNNING = 1, 48 }; 49 50 /** 51 * get_capability - convert device capabilities to NIC capabilities 52 * @hwdev: the HW device to set and convert device capabilities for 53 * @dev_cap: device capabilities from FW 54 * 55 * Return 0 - Success, negative - Failure 56 **/ 57 static int parse_capability(struct hinic_hwdev *hwdev, 58 struct hinic_dev_cap *dev_cap) 59 { 60 struct hinic_cap *nic_cap = &hwdev->nic_cap; 61 int num_aeqs, num_ceqs, num_irqs; 62 63 if (!HINIC_IS_VF(hwdev->hwif) && dev_cap->intr_type != INTR_MSIX_TYPE) 64 return -EFAULT; 65 66 num_aeqs = HINIC_HWIF_NUM_AEQS(hwdev->hwif); 67 num_ceqs = HINIC_HWIF_NUM_CEQS(hwdev->hwif); 68 num_irqs = HINIC_HWIF_NUM_IRQS(hwdev->hwif); 69 70 /* Each QP has its own (SQ + RQ) interrupts */ 71 nic_cap->num_qps = (num_irqs - (num_aeqs + num_ceqs)) / 2; 72 73 if (nic_cap->num_qps > HINIC_Q_CTXT_MAX) 74 nic_cap->num_qps = HINIC_Q_CTXT_MAX; 75 76 if (!HINIC_IS_VF(hwdev->hwif)) 77 nic_cap->max_qps = dev_cap->max_sqs + 1; 78 else 79 nic_cap->max_qps = dev_cap->max_sqs; 80 81 if (nic_cap->num_qps > nic_cap->max_qps) 82 nic_cap->num_qps = nic_cap->max_qps; 83 84 if (!HINIC_IS_VF(hwdev->hwif)) { 85 nic_cap->max_vf = dev_cap->max_vf; 86 nic_cap->max_vf_qps = dev_cap->max_vf_sqs + 1; 87 } 88 89 hwdev->port_id = dev_cap->port_id; 90 91 return 0; 92 } 93 94 /** 95 * get_cap_from_fw - get device capabilities from FW 96 * @pfhwdev: the PF HW device to get capabilities for 97 * 98 * Return 0 - Success, negative - Failure 99 **/ 100 static int get_capability(struct hinic_pfhwdev *pfhwdev) 101 { 102 struct hinic_hwdev *hwdev = &pfhwdev->hwdev; 103 struct hinic_hwif *hwif = hwdev->hwif; 104 struct pci_dev *pdev = hwif->pdev; 105 struct hinic_dev_cap dev_cap; 106 u16 out_len; 107 int err; 108 109 out_len = sizeof(dev_cap); 110 111 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_CFGM, 112 HINIC_CFG_NIC_CAP, &dev_cap, sizeof(dev_cap), 113 &dev_cap, &out_len, HINIC_MGMT_MSG_SYNC); 114 if (err) { 115 dev_err(&pdev->dev, "Failed to get capability from FW\n"); 116 return err; 117 } 118 119 return parse_capability(hwdev, &dev_cap); 120 } 121 122 /** 123 * get_dev_cap - get device capabilities 124 * @hwdev: the NIC HW device to get capabilities for 125 * 126 * Return 0 - Success, negative - Failure 127 **/ 128 static int get_dev_cap(struct hinic_hwdev *hwdev) 129 { 130 struct hinic_hwif *hwif = hwdev->hwif; 131 struct pci_dev *pdev = hwif->pdev; 132 struct hinic_pfhwdev *pfhwdev; 133 int err; 134 135 switch (HINIC_FUNC_TYPE(hwif)) { 136 case HINIC_PPF: 137 case HINIC_PF: 138 case HINIC_VF: 139 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 140 err = get_capability(pfhwdev); 141 if (err) { 142 dev_err(&pdev->dev, "Failed to get capability\n"); 143 return err; 144 } 145 break; 146 default: 147 dev_err(&pdev->dev, "Unsupported PCI Function type\n"); 148 return -EINVAL; 149 } 150 151 return 0; 152 } 153 154 /** 155 * init_msix - enable the msix and save the entries 156 * @hwdev: the NIC HW device 157 * 158 * Return 0 - Success, negative - Failure 159 **/ 160 static int init_msix(struct hinic_hwdev *hwdev) 161 { 162 struct hinic_hwif *hwif = hwdev->hwif; 163 struct pci_dev *pdev = hwif->pdev; 164 int nr_irqs, num_aeqs, num_ceqs; 165 size_t msix_entries_size; 166 int i, err; 167 168 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif); 169 num_ceqs = HINIC_HWIF_NUM_CEQS(hwif); 170 nr_irqs = MAX_IRQS(HINIC_MAX_QPS, num_aeqs, num_ceqs); 171 if (nr_irqs > HINIC_HWIF_NUM_IRQS(hwif)) 172 nr_irqs = HINIC_HWIF_NUM_IRQS(hwif); 173 174 msix_entries_size = nr_irqs * sizeof(*hwdev->msix_entries); 175 hwdev->msix_entries = devm_kzalloc(&pdev->dev, msix_entries_size, 176 GFP_KERNEL); 177 if (!hwdev->msix_entries) 178 return -ENOMEM; 179 180 for (i = 0; i < nr_irqs; i++) 181 hwdev->msix_entries[i].entry = i; 182 183 err = pci_enable_msix_exact(pdev, hwdev->msix_entries, nr_irqs); 184 if (err) { 185 dev_err(&pdev->dev, "Failed to enable pci msix\n"); 186 return err; 187 } 188 189 return 0; 190 } 191 192 /** 193 * disable_msix - disable the msix 194 * @hwdev: the NIC HW device 195 **/ 196 static void disable_msix(struct hinic_hwdev *hwdev) 197 { 198 struct hinic_hwif *hwif = hwdev->hwif; 199 struct pci_dev *pdev = hwif->pdev; 200 201 pci_disable_msix(pdev); 202 } 203 204 /** 205 * hinic_port_msg_cmd - send port msg to mgmt 206 * @hwdev: the NIC HW device 207 * @cmd: the port command 208 * @buf_in: input buffer 209 * @in_size: input size 210 * @buf_out: output buffer 211 * @out_size: returned output size 212 * 213 * Return 0 - Success, negative - Failure 214 **/ 215 int hinic_port_msg_cmd(struct hinic_hwdev *hwdev, enum hinic_port_cmd cmd, 216 void *buf_in, u16 in_size, void *buf_out, u16 *out_size) 217 { 218 struct hinic_pfhwdev *pfhwdev; 219 220 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 221 222 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC, cmd, 223 buf_in, in_size, buf_out, out_size, 224 HINIC_MGMT_MSG_SYNC); 225 } 226 227 int hinic_hilink_msg_cmd(struct hinic_hwdev *hwdev, enum hinic_hilink_cmd cmd, 228 void *buf_in, u16 in_size, void *buf_out, 229 u16 *out_size) 230 { 231 struct hinic_pfhwdev *pfhwdev; 232 233 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 234 235 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_HILINK, cmd, 236 buf_in, in_size, buf_out, out_size, 237 HINIC_MGMT_MSG_SYNC); 238 } 239 240 /** 241 * init_fw_ctxt- Init Firmware tables before network mgmt and io operations 242 * @hwdev: the NIC HW device 243 * 244 * Return 0 - Success, negative - Failure 245 **/ 246 static int init_fw_ctxt(struct hinic_hwdev *hwdev) 247 { 248 struct hinic_hwif *hwif = hwdev->hwif; 249 struct pci_dev *pdev = hwif->pdev; 250 struct hinic_cmd_fw_ctxt fw_ctxt; 251 u16 out_size = sizeof(fw_ctxt); 252 int err; 253 254 fw_ctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 255 fw_ctxt.rx_buf_sz = HINIC_RX_BUF_SZ; 256 257 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_FWCTXT_INIT, 258 &fw_ctxt, sizeof(fw_ctxt), 259 &fw_ctxt, &out_size); 260 if (err || (out_size != sizeof(fw_ctxt)) || fw_ctxt.status) { 261 dev_err(&pdev->dev, "Failed to init FW ctxt, err: %d, status: 0x%x, out size: 0x%x\n", 262 err, fw_ctxt.status, out_size); 263 return -EIO; 264 } 265 266 return 0; 267 } 268 269 /** 270 * set_hw_ioctxt - set the shape of the IO queues in FW 271 * @hwdev: the NIC HW device 272 * @rq_depth: rq depth 273 * @sq_depth: sq depth 274 * 275 * Return 0 - Success, negative - Failure 276 **/ 277 static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int sq_depth, 278 unsigned int rq_depth) 279 { 280 struct hinic_hwif *hwif = hwdev->hwif; 281 struct hinic_cmd_hw_ioctxt hw_ioctxt; 282 struct hinic_pfhwdev *pfhwdev; 283 284 hw_ioctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 285 hw_ioctxt.ppf_idx = HINIC_HWIF_PPF_IDX(hwif); 286 287 hw_ioctxt.set_cmdq_depth = HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT; 288 hw_ioctxt.cmdq_depth = 0; 289 290 hw_ioctxt.lro_en = 1; 291 292 hw_ioctxt.rq_depth = ilog2(rq_depth); 293 294 hw_ioctxt.rx_buf_sz_idx = HINIC_RX_BUF_SZ_IDX; 295 296 hw_ioctxt.sq_depth = ilog2(sq_depth); 297 298 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 299 300 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 301 HINIC_COMM_CMD_HWCTXT_SET, 302 &hw_ioctxt, sizeof(hw_ioctxt), NULL, 303 NULL, HINIC_MGMT_MSG_SYNC); 304 } 305 306 static int wait_for_outbound_state(struct hinic_hwdev *hwdev) 307 { 308 enum hinic_outbound_state outbound_state; 309 struct hinic_hwif *hwif = hwdev->hwif; 310 struct pci_dev *pdev = hwif->pdev; 311 unsigned long end; 312 313 end = jiffies + msecs_to_jiffies(OUTBOUND_STATE_TIMEOUT); 314 do { 315 outbound_state = hinic_outbound_state_get(hwif); 316 317 if (outbound_state == HINIC_OUTBOUND_ENABLE) 318 return 0; 319 320 msleep(20); 321 } while (time_before(jiffies, end)); 322 323 dev_err(&pdev->dev, "Wait for OUTBOUND - Timeout\n"); 324 return -EFAULT; 325 } 326 327 static int wait_for_db_state(struct hinic_hwdev *hwdev) 328 { 329 struct hinic_hwif *hwif = hwdev->hwif; 330 struct pci_dev *pdev = hwif->pdev; 331 enum hinic_db_state db_state; 332 unsigned long end; 333 334 end = jiffies + msecs_to_jiffies(DB_STATE_TIMEOUT); 335 do { 336 db_state = hinic_db_state_get(hwif); 337 338 if (db_state == HINIC_DB_ENABLE) 339 return 0; 340 341 msleep(20); 342 } while (time_before(jiffies, end)); 343 344 dev_err(&pdev->dev, "Wait for DB - Timeout\n"); 345 return -EFAULT; 346 } 347 348 /** 349 * clear_io_resource - set the IO resources as not active in the NIC 350 * @hwdev: the NIC HW device 351 * 352 * Return 0 - Success, negative - Failure 353 **/ 354 static int clear_io_resources(struct hinic_hwdev *hwdev) 355 { 356 struct hinic_cmd_clear_io_res cmd_clear_io_res; 357 struct hinic_hwif *hwif = hwdev->hwif; 358 struct pci_dev *pdev = hwif->pdev; 359 struct hinic_pfhwdev *pfhwdev; 360 int err; 361 362 /* sleep 100ms to wait for firmware stopping I/O */ 363 msleep(100); 364 365 cmd_clear_io_res.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 366 367 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 368 369 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 370 HINIC_COMM_CMD_IO_RES_CLEAR, &cmd_clear_io_res, 371 sizeof(cmd_clear_io_res), NULL, NULL, 372 HINIC_MGMT_MSG_SYNC); 373 if (err) { 374 dev_err(&pdev->dev, "Failed to clear IO resources\n"); 375 return err; 376 } 377 378 return 0; 379 } 380 381 /** 382 * set_resources_state - set the state of the resources in the NIC 383 * @hwdev: the NIC HW device 384 * @state: the state to set 385 * 386 * Return 0 - Success, negative - Failure 387 **/ 388 static int set_resources_state(struct hinic_hwdev *hwdev, 389 enum hinic_res_state state) 390 { 391 struct hinic_cmd_set_res_state res_state; 392 struct hinic_hwif *hwif = hwdev->hwif; 393 struct hinic_pfhwdev *pfhwdev; 394 395 res_state.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 396 res_state.state = state; 397 398 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 399 400 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, 401 HINIC_MOD_COMM, 402 HINIC_COMM_CMD_RES_STATE_SET, 403 &res_state, sizeof(res_state), NULL, 404 NULL, HINIC_MGMT_MSG_SYNC); 405 } 406 407 /** 408 * get_base_qpn - get the first qp number 409 * @hwdev: the NIC HW device 410 * @base_qpn: returned qp number 411 * 412 * Return 0 - Success, negative - Failure 413 **/ 414 static int get_base_qpn(struct hinic_hwdev *hwdev, u16 *base_qpn) 415 { 416 struct hinic_cmd_base_qpn cmd_base_qpn; 417 struct hinic_hwif *hwif = hwdev->hwif; 418 u16 out_size = sizeof(cmd_base_qpn); 419 struct pci_dev *pdev = hwif->pdev; 420 int err; 421 422 cmd_base_qpn.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 423 424 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_GET_GLOBAL_QPN, 425 &cmd_base_qpn, sizeof(cmd_base_qpn), 426 &cmd_base_qpn, &out_size); 427 if (err || (out_size != sizeof(cmd_base_qpn)) || cmd_base_qpn.status) { 428 dev_err(&pdev->dev, "Failed to get base qpn, err: %d, status: 0x%x, out size: 0x%x\n", 429 err, cmd_base_qpn.status, out_size); 430 return -EIO; 431 } 432 433 *base_qpn = cmd_base_qpn.qpn; 434 return 0; 435 } 436 437 /** 438 * hinic_hwdev_ifup - Preparing the HW for passing IO 439 * @hwdev: the NIC HW device 440 * 441 * Return 0 - Success, negative - Failure 442 **/ 443 int hinic_hwdev_ifup(struct hinic_hwdev *hwdev, u16 sq_depth, u16 rq_depth) 444 { 445 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io; 446 struct hinic_cap *nic_cap = &hwdev->nic_cap; 447 struct hinic_hwif *hwif = hwdev->hwif; 448 int err, num_aeqs, num_ceqs, num_qps; 449 struct msix_entry *ceq_msix_entries; 450 struct msix_entry *sq_msix_entries; 451 struct msix_entry *rq_msix_entries; 452 struct pci_dev *pdev = hwif->pdev; 453 u16 base_qpn; 454 455 err = get_base_qpn(hwdev, &base_qpn); 456 if (err) { 457 dev_err(&pdev->dev, "Failed to get global base qp number\n"); 458 return err; 459 } 460 461 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif); 462 num_ceqs = HINIC_HWIF_NUM_CEQS(hwif); 463 464 ceq_msix_entries = &hwdev->msix_entries[num_aeqs]; 465 func_to_io->hwdev = hwdev; 466 func_to_io->sq_depth = sq_depth; 467 func_to_io->rq_depth = rq_depth; 468 func_to_io->global_qpn = base_qpn; 469 470 err = hinic_io_init(func_to_io, hwif, nic_cap->max_qps, num_ceqs, 471 ceq_msix_entries); 472 if (err) { 473 dev_err(&pdev->dev, "Failed to init IO channel\n"); 474 return err; 475 } 476 477 num_qps = nic_cap->num_qps; 478 sq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs]; 479 rq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs + num_qps]; 480 481 err = hinic_io_create_qps(func_to_io, base_qpn, num_qps, 482 sq_msix_entries, rq_msix_entries); 483 if (err) { 484 dev_err(&pdev->dev, "Failed to create QPs\n"); 485 goto err_create_qps; 486 } 487 488 err = wait_for_db_state(hwdev); 489 if (err) { 490 dev_warn(&pdev->dev, "db - disabled, try again\n"); 491 hinic_db_state_set(hwif, HINIC_DB_ENABLE); 492 } 493 494 err = set_hw_ioctxt(hwdev, sq_depth, rq_depth); 495 if (err) { 496 dev_err(&pdev->dev, "Failed to set HW IO ctxt\n"); 497 goto err_hw_ioctxt; 498 } 499 500 return 0; 501 502 err_hw_ioctxt: 503 hinic_io_destroy_qps(func_to_io, num_qps); 504 505 err_create_qps: 506 hinic_io_free(func_to_io); 507 return err; 508 } 509 510 /** 511 * hinic_hwdev_ifdown - Closing the HW for passing IO 512 * @hwdev: the NIC HW device 513 * 514 **/ 515 void hinic_hwdev_ifdown(struct hinic_hwdev *hwdev) 516 { 517 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io; 518 struct hinic_cap *nic_cap = &hwdev->nic_cap; 519 520 clear_io_resources(hwdev); 521 522 hinic_io_destroy_qps(func_to_io, nic_cap->num_qps); 523 hinic_io_free(func_to_io); 524 } 525 526 /** 527 * hinic_hwdev_cb_register - register callback handler for MGMT events 528 * @hwdev: the NIC HW device 529 * @cmd: the mgmt event 530 * @handle: private data for the handler 531 * @handler: event handler 532 **/ 533 void hinic_hwdev_cb_register(struct hinic_hwdev *hwdev, 534 enum hinic_mgmt_msg_cmd cmd, void *handle, 535 void (*handler)(void *handle, void *buf_in, 536 u16 in_size, void *buf_out, 537 u16 *out_size)) 538 { 539 struct hinic_pfhwdev *pfhwdev; 540 struct hinic_nic_cb *nic_cb; 541 u8 cmd_cb; 542 543 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 544 545 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE; 546 nic_cb = &pfhwdev->nic_cb[cmd_cb]; 547 548 nic_cb->handler = handler; 549 nic_cb->handle = handle; 550 nic_cb->cb_state = HINIC_CB_ENABLED; 551 } 552 553 /** 554 * hinic_hwdev_cb_unregister - unregister callback handler for MGMT events 555 * @hwdev: the NIC HW device 556 * @cmd: the mgmt event 557 **/ 558 void hinic_hwdev_cb_unregister(struct hinic_hwdev *hwdev, 559 enum hinic_mgmt_msg_cmd cmd) 560 { 561 struct hinic_hwif *hwif = hwdev->hwif; 562 struct hinic_pfhwdev *pfhwdev; 563 struct hinic_nic_cb *nic_cb; 564 u8 cmd_cb; 565 566 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) 567 return; 568 569 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 570 571 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE; 572 nic_cb = &pfhwdev->nic_cb[cmd_cb]; 573 574 nic_cb->cb_state &= ~HINIC_CB_ENABLED; 575 576 while (nic_cb->cb_state & HINIC_CB_RUNNING) 577 schedule(); 578 579 nic_cb->handler = NULL; 580 } 581 582 /** 583 * nic_mgmt_msg_handler - nic mgmt event handler 584 * @handle: private data for the handler 585 * @buf_in: input buffer 586 * @in_size: input size 587 * @buf_out: output buffer 588 * @out_size: returned output size 589 **/ 590 static void nic_mgmt_msg_handler(void *handle, u8 cmd, void *buf_in, 591 u16 in_size, void *buf_out, u16 *out_size) 592 { 593 struct hinic_pfhwdev *pfhwdev = handle; 594 enum hinic_cb_state cb_state; 595 struct hinic_nic_cb *nic_cb; 596 struct hinic_hwdev *hwdev; 597 struct hinic_hwif *hwif; 598 struct pci_dev *pdev; 599 u8 cmd_cb; 600 601 hwdev = &pfhwdev->hwdev; 602 hwif = hwdev->hwif; 603 pdev = hwif->pdev; 604 605 if ((cmd < HINIC_MGMT_MSG_CMD_BASE) || 606 (cmd >= HINIC_MGMT_MSG_CMD_MAX)) { 607 dev_err(&pdev->dev, "unknown L2NIC event, cmd = %d\n", cmd); 608 return; 609 } 610 611 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE; 612 613 nic_cb = &pfhwdev->nic_cb[cmd_cb]; 614 615 cb_state = cmpxchg(&nic_cb->cb_state, 616 HINIC_CB_ENABLED, 617 HINIC_CB_ENABLED | HINIC_CB_RUNNING); 618 619 if ((cb_state == HINIC_CB_ENABLED) && (nic_cb->handler)) 620 nic_cb->handler(nic_cb->handle, buf_in, 621 in_size, buf_out, out_size); 622 else 623 dev_err(&pdev->dev, "Unhandled NIC Event %d\n", cmd); 624 625 nic_cb->cb_state &= ~HINIC_CB_RUNNING; 626 } 627 628 static void hinic_comm_recv_mgmt_self_cmd_reg(struct hinic_pfhwdev *pfhwdev, 629 u8 cmd, 630 comm_mgmt_self_msg_proc proc) 631 { 632 u8 cmd_idx; 633 634 cmd_idx = pfhwdev->proc.cmd_num; 635 if (cmd_idx >= HINIC_COMM_SELF_CMD_MAX) { 636 dev_err(&pfhwdev->hwdev.hwif->pdev->dev, 637 "Register recv mgmt process failed, cmd: 0x%x\n", cmd); 638 return; 639 } 640 641 pfhwdev->proc.info[cmd_idx].cmd = cmd; 642 pfhwdev->proc.info[cmd_idx].proc = proc; 643 pfhwdev->proc.cmd_num++; 644 } 645 646 static void hinic_comm_recv_mgmt_self_cmd_unreg(struct hinic_pfhwdev *pfhwdev, 647 u8 cmd) 648 { 649 u8 cmd_idx; 650 651 cmd_idx = pfhwdev->proc.cmd_num; 652 if (cmd_idx >= HINIC_COMM_SELF_CMD_MAX) { 653 dev_err(&pfhwdev->hwdev.hwif->pdev->dev, "Unregister recv mgmt process failed, cmd: 0x%x\n", 654 cmd); 655 return; 656 } 657 658 for (cmd_idx = 0; cmd_idx < HINIC_COMM_SELF_CMD_MAX; cmd_idx++) { 659 if (cmd == pfhwdev->proc.info[cmd_idx].cmd) { 660 pfhwdev->proc.info[cmd_idx].cmd = 0; 661 pfhwdev->proc.info[cmd_idx].proc = NULL; 662 pfhwdev->proc.cmd_num--; 663 } 664 } 665 } 666 667 static void comm_mgmt_msg_handler(void *handle, u8 cmd, void *buf_in, 668 u16 in_size, void *buf_out, u16 *out_size) 669 { 670 struct hinic_pfhwdev *pfhwdev = handle; 671 u8 cmd_idx; 672 673 for (cmd_idx = 0; cmd_idx < pfhwdev->proc.cmd_num; cmd_idx++) { 674 if (cmd == pfhwdev->proc.info[cmd_idx].cmd) { 675 if (!pfhwdev->proc.info[cmd_idx].proc) { 676 dev_warn(&pfhwdev->hwdev.hwif->pdev->dev, 677 "PF recv mgmt comm msg handle null, cmd: 0x%x\n", 678 cmd); 679 } else { 680 pfhwdev->proc.info[cmd_idx].proc 681 (&pfhwdev->hwdev, buf_in, in_size, 682 buf_out, out_size); 683 } 684 685 return; 686 } 687 } 688 689 dev_warn(&pfhwdev->hwdev.hwif->pdev->dev, "Received unknown mgmt cpu event: 0x%x\n", 690 cmd); 691 692 *out_size = 0; 693 } 694 695 /* pf fault report event */ 696 static void pf_fault_event_handler(void *dev, void *buf_in, u16 in_size, 697 void *buf_out, u16 *out_size) 698 { 699 struct hinic_cmd_fault_event *fault_event = buf_in; 700 struct hinic_hwdev *hwdev = dev; 701 702 if (in_size != sizeof(*fault_event)) { 703 dev_err(&hwdev->hwif->pdev->dev, "Invalid fault event report, length: %d, should be %zu\n", 704 in_size, sizeof(*fault_event)); 705 return; 706 } 707 708 if (!hwdev->devlink_dev || IS_ERR_OR_NULL(hwdev->devlink_dev->hw_fault_reporter)) 709 return; 710 711 devlink_health_report(hwdev->devlink_dev->hw_fault_reporter, 712 "HW fatal error reported", &fault_event->event); 713 } 714 715 static void mgmt_watchdog_timeout_event_handler(void *dev, 716 void *buf_in, u16 in_size, 717 void *buf_out, u16 *out_size) 718 { 719 struct hinic_mgmt_watchdog_info *watchdog_info = buf_in; 720 struct hinic_hwdev *hwdev = dev; 721 722 if (in_size != sizeof(*watchdog_info)) { 723 dev_err(&hwdev->hwif->pdev->dev, "Invalid mgmt watchdog report, length: %d, should be %zu\n", 724 in_size, sizeof(*watchdog_info)); 725 return; 726 } 727 728 if (!hwdev->devlink_dev || IS_ERR_OR_NULL(hwdev->devlink_dev->fw_fault_reporter)) 729 return; 730 731 devlink_health_report(hwdev->devlink_dev->fw_fault_reporter, 732 "FW fatal error reported", watchdog_info); 733 } 734 735 /** 736 * init_pfhwdev - Initialize the extended components of PF 737 * @pfhwdev: the HW device for PF 738 * 739 * Return 0 - success, negative - failure 740 **/ 741 static int init_pfhwdev(struct hinic_pfhwdev *pfhwdev) 742 { 743 struct hinic_hwdev *hwdev = &pfhwdev->hwdev; 744 struct hinic_hwif *hwif = hwdev->hwif; 745 struct pci_dev *pdev = hwif->pdev; 746 int err; 747 748 err = hinic_pf_to_mgmt_init(&pfhwdev->pf_to_mgmt, hwif); 749 if (err) { 750 dev_err(&pdev->dev, "Failed to initialize PF to MGMT channel\n"); 751 return err; 752 } 753 754 err = hinic_devlink_register(hwdev->devlink_dev, &pdev->dev); 755 if (err) { 756 dev_err(&hwif->pdev->dev, "Failed to register devlink\n"); 757 hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt); 758 return err; 759 } 760 761 err = hinic_func_to_func_init(hwdev); 762 if (err) { 763 dev_err(&hwif->pdev->dev, "Failed to init mailbox\n"); 764 hinic_devlink_unregister(hwdev->devlink_dev); 765 hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt); 766 return err; 767 } 768 769 if (!HINIC_IS_VF(hwif)) { 770 hinic_register_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, 771 HINIC_MOD_L2NIC, pfhwdev, 772 nic_mgmt_msg_handler); 773 hinic_register_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 774 pfhwdev, comm_mgmt_msg_handler); 775 hinic_comm_recv_mgmt_self_cmd_reg(pfhwdev, 776 HINIC_COMM_CMD_FAULT_REPORT, 777 pf_fault_event_handler); 778 hinic_comm_recv_mgmt_self_cmd_reg 779 (pfhwdev, HINIC_COMM_CMD_WATCHDOG_INFO, 780 mgmt_watchdog_timeout_event_handler); 781 } else { 782 hinic_register_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC, 783 nic_mgmt_msg_handler); 784 } 785 786 hinic_set_pf_action(hwif, HINIC_PF_MGMT_ACTIVE); 787 788 return 0; 789 } 790 791 /** 792 * free_pfhwdev - Free the extended components of PF 793 * @pfhwdev: the HW device for PF 794 **/ 795 static void free_pfhwdev(struct hinic_pfhwdev *pfhwdev) 796 { 797 struct hinic_hwdev *hwdev = &pfhwdev->hwdev; 798 799 hinic_set_pf_action(hwdev->hwif, HINIC_PF_MGMT_INIT); 800 801 if (!HINIC_IS_VF(hwdev->hwif)) { 802 hinic_comm_recv_mgmt_self_cmd_unreg(pfhwdev, 803 HINIC_COMM_CMD_WATCHDOG_INFO); 804 hinic_comm_recv_mgmt_self_cmd_unreg(pfhwdev, 805 HINIC_COMM_CMD_FAULT_REPORT); 806 hinic_unregister_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, 807 HINIC_MOD_COMM); 808 hinic_unregister_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, 809 HINIC_MOD_L2NIC); 810 } else { 811 hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC); 812 } 813 814 hinic_func_to_func_free(hwdev); 815 816 hinic_devlink_unregister(hwdev->devlink_dev); 817 818 hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt); 819 } 820 821 static int hinic_l2nic_reset(struct hinic_hwdev *hwdev) 822 { 823 struct hinic_cmd_l2nic_reset l2nic_reset = {0}; 824 u16 out_size = sizeof(l2nic_reset); 825 struct hinic_pfhwdev *pfhwdev; 826 int err; 827 828 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 829 830 l2nic_reset.func_id = HINIC_HWIF_FUNC_IDX(hwdev->hwif); 831 /* 0 represents standard l2nic reset flow */ 832 l2nic_reset.reset_flag = 0; 833 834 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 835 HINIC_COMM_CMD_L2NIC_RESET, &l2nic_reset, 836 sizeof(l2nic_reset), &l2nic_reset, 837 &out_size, HINIC_MGMT_MSG_SYNC); 838 if (err || !out_size || l2nic_reset.status) { 839 dev_err(&hwdev->hwif->pdev->dev, "Failed to reset L2NIC resources, err: %d, status: 0x%x, out_size: 0x%x\n", 840 err, l2nic_reset.status, out_size); 841 return -EIO; 842 } 843 844 return 0; 845 } 846 847 int hinic_get_interrupt_cfg(struct hinic_hwdev *hwdev, 848 struct hinic_msix_config *interrupt_info) 849 { 850 u16 out_size = sizeof(*interrupt_info); 851 struct hinic_pfhwdev *pfhwdev; 852 int err; 853 854 if (!hwdev || !interrupt_info) 855 return -EINVAL; 856 857 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 858 859 interrupt_info->func_id = HINIC_HWIF_FUNC_IDX(hwdev->hwif); 860 861 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 862 HINIC_COMM_CMD_MSI_CTRL_REG_RD_BY_UP, 863 interrupt_info, sizeof(*interrupt_info), 864 interrupt_info, &out_size, HINIC_MGMT_MSG_SYNC); 865 if (err || !out_size || interrupt_info->status) { 866 dev_err(&hwdev->hwif->pdev->dev, "Failed to get interrupt config, err: %d, status: 0x%x, out size: 0x%x\n", 867 err, interrupt_info->status, out_size); 868 return -EIO; 869 } 870 871 return 0; 872 } 873 874 int hinic_set_interrupt_cfg(struct hinic_hwdev *hwdev, 875 struct hinic_msix_config *interrupt_info) 876 { 877 u16 out_size = sizeof(*interrupt_info); 878 struct hinic_msix_config temp_info; 879 struct hinic_pfhwdev *pfhwdev; 880 int err; 881 882 if (!hwdev) 883 return -EINVAL; 884 885 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 886 887 interrupt_info->func_id = HINIC_HWIF_FUNC_IDX(hwdev->hwif); 888 889 err = hinic_get_interrupt_cfg(hwdev, &temp_info); 890 if (err) 891 return -EINVAL; 892 893 interrupt_info->lli_credit_cnt = temp_info.lli_timer_cnt; 894 interrupt_info->lli_timer_cnt = temp_info.lli_timer_cnt; 895 896 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 897 HINIC_COMM_CMD_MSI_CTRL_REG_WR_BY_UP, 898 interrupt_info, sizeof(*interrupt_info), 899 interrupt_info, &out_size, HINIC_MGMT_MSG_SYNC); 900 if (err || !out_size || interrupt_info->status) { 901 dev_err(&hwdev->hwif->pdev->dev, "Failed to get interrupt config, err: %d, status: 0x%x, out size: 0x%x\n", 902 err, interrupt_info->status, out_size); 903 return -EIO; 904 } 905 906 return 0; 907 } 908 909 /** 910 * hinic_init_hwdev - Initialize the NIC HW 911 * @pdev: the NIC pci device 912 * 913 * Return initialized NIC HW device 914 * 915 * Initialize the NIC HW device and return a pointer to it 916 **/ 917 struct hinic_hwdev *hinic_init_hwdev(struct pci_dev *pdev, struct devlink *devlink) 918 { 919 struct hinic_pfhwdev *pfhwdev; 920 struct hinic_hwdev *hwdev; 921 struct hinic_hwif *hwif; 922 int err, num_aeqs; 923 924 hwif = devm_kzalloc(&pdev->dev, sizeof(*hwif), GFP_KERNEL); 925 if (!hwif) 926 return ERR_PTR(-ENOMEM); 927 928 err = hinic_init_hwif(hwif, pdev); 929 if (err) { 930 dev_err(&pdev->dev, "Failed to init HW interface\n"); 931 return ERR_PTR(err); 932 } 933 934 pfhwdev = devm_kzalloc(&pdev->dev, sizeof(*pfhwdev), GFP_KERNEL); 935 if (!pfhwdev) { 936 err = -ENOMEM; 937 goto err_pfhwdev_alloc; 938 } 939 940 hwdev = &pfhwdev->hwdev; 941 hwdev->hwif = hwif; 942 hwdev->devlink_dev = devlink_priv(devlink); 943 hwdev->devlink_dev->hwdev = hwdev; 944 945 err = init_msix(hwdev); 946 if (err) { 947 dev_err(&pdev->dev, "Failed to init msix\n"); 948 goto err_init_msix; 949 } 950 951 err = wait_for_outbound_state(hwdev); 952 if (err) { 953 dev_warn(&pdev->dev, "outbound - disabled, try again\n"); 954 hinic_outbound_state_set(hwif, HINIC_OUTBOUND_ENABLE); 955 } 956 957 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif); 958 959 err = hinic_aeqs_init(&hwdev->aeqs, hwif, num_aeqs, 960 HINIC_DEFAULT_AEQ_LEN, HINIC_EQ_PAGE_SIZE, 961 hwdev->msix_entries); 962 if (err) { 963 dev_err(&pdev->dev, "Failed to init async event queues\n"); 964 goto err_aeqs_init; 965 } 966 967 err = init_pfhwdev(pfhwdev); 968 if (err) { 969 dev_err(&pdev->dev, "Failed to init PF HW device\n"); 970 goto err_init_pfhwdev; 971 } 972 973 err = hinic_l2nic_reset(hwdev); 974 if (err) 975 goto err_l2nic_reset; 976 977 err = get_dev_cap(hwdev); 978 if (err) { 979 dev_err(&pdev->dev, "Failed to get device capabilities\n"); 980 goto err_dev_cap; 981 } 982 983 mutex_init(&hwdev->func_to_io.nic_cfg.cfg_mutex); 984 985 err = hinic_vf_func_init(hwdev); 986 if (err) { 987 dev_err(&pdev->dev, "Failed to init nic mbox\n"); 988 goto err_vf_func_init; 989 } 990 991 err = init_fw_ctxt(hwdev); 992 if (err) { 993 dev_err(&pdev->dev, "Failed to init function table\n"); 994 goto err_init_fw_ctxt; 995 } 996 997 err = set_resources_state(hwdev, HINIC_RES_ACTIVE); 998 if (err) { 999 dev_err(&pdev->dev, "Failed to set resources state\n"); 1000 goto err_resources_state; 1001 } 1002 1003 return hwdev; 1004 1005 err_resources_state: 1006 err_init_fw_ctxt: 1007 hinic_vf_func_free(hwdev); 1008 err_vf_func_init: 1009 err_l2nic_reset: 1010 err_dev_cap: 1011 free_pfhwdev(pfhwdev); 1012 1013 err_init_pfhwdev: 1014 hinic_aeqs_free(&hwdev->aeqs); 1015 1016 err_aeqs_init: 1017 disable_msix(hwdev); 1018 1019 err_init_msix: 1020 err_pfhwdev_alloc: 1021 hinic_free_hwif(hwif); 1022 if (err > 0) 1023 err = -EIO; 1024 return ERR_PTR(err); 1025 } 1026 1027 /** 1028 * hinic_free_hwdev - Free the NIC HW device 1029 * @hwdev: the NIC HW device 1030 **/ 1031 void hinic_free_hwdev(struct hinic_hwdev *hwdev) 1032 { 1033 struct hinic_pfhwdev *pfhwdev = container_of(hwdev, 1034 struct hinic_pfhwdev, 1035 hwdev); 1036 1037 set_resources_state(hwdev, HINIC_RES_CLEAN); 1038 1039 hinic_vf_func_free(hwdev); 1040 1041 free_pfhwdev(pfhwdev); 1042 1043 hinic_aeqs_free(&hwdev->aeqs); 1044 1045 disable_msix(hwdev); 1046 1047 hinic_free_hwif(hwdev->hwif); 1048 } 1049 1050 int hinic_hwdev_max_num_qps(struct hinic_hwdev *hwdev) 1051 { 1052 struct hinic_cap *nic_cap = &hwdev->nic_cap; 1053 1054 return nic_cap->max_qps; 1055 } 1056 1057 /** 1058 * hinic_hwdev_num_qps - return the number QPs available for use 1059 * @hwdev: the NIC HW device 1060 * 1061 * Return number QPs available for use 1062 **/ 1063 int hinic_hwdev_num_qps(struct hinic_hwdev *hwdev) 1064 { 1065 struct hinic_cap *nic_cap = &hwdev->nic_cap; 1066 1067 return nic_cap->num_qps; 1068 } 1069 1070 /** 1071 * hinic_hwdev_get_sq - get SQ 1072 * @hwdev: the NIC HW device 1073 * @i: the position of the SQ 1074 * 1075 * Return: the SQ in the i position 1076 **/ 1077 struct hinic_sq *hinic_hwdev_get_sq(struct hinic_hwdev *hwdev, int i) 1078 { 1079 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io; 1080 struct hinic_qp *qp = &func_to_io->qps[i]; 1081 1082 if (i >= hinic_hwdev_num_qps(hwdev)) 1083 return NULL; 1084 1085 return &qp->sq; 1086 } 1087 1088 /** 1089 * hinic_hwdev_get_sq - get RQ 1090 * @hwdev: the NIC HW device 1091 * @i: the position of the RQ 1092 * 1093 * Return: the RQ in the i position 1094 **/ 1095 struct hinic_rq *hinic_hwdev_get_rq(struct hinic_hwdev *hwdev, int i) 1096 { 1097 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io; 1098 struct hinic_qp *qp = &func_to_io->qps[i]; 1099 1100 if (i >= hinic_hwdev_num_qps(hwdev)) 1101 return NULL; 1102 1103 return &qp->rq; 1104 } 1105 1106 /** 1107 * hinic_hwdev_msix_cnt_set - clear message attribute counters for msix entry 1108 * @hwdev: the NIC HW device 1109 * @msix_index: msix_index 1110 * 1111 * Return 0 - Success, negative - Failure 1112 **/ 1113 int hinic_hwdev_msix_cnt_set(struct hinic_hwdev *hwdev, u16 msix_index) 1114 { 1115 return hinic_msix_attr_cnt_clear(hwdev->hwif, msix_index); 1116 } 1117 1118 /** 1119 * hinic_hwdev_msix_set - set message attribute for msix entry 1120 * @hwdev: the NIC HW device 1121 * @msix_index: msix_index 1122 * @pending_limit: the maximum pending interrupt events (unit 8) 1123 * @coalesc_timer: coalesc period for interrupt (unit 8 us) 1124 * @lli_timer: replenishing period for low latency credit (unit 8 us) 1125 * @lli_credit_limit: maximum credits for low latency msix messages (unit 8) 1126 * @resend_timer: maximum wait for resending msix (unit coalesc period) 1127 * 1128 * Return 0 - Success, negative - Failure 1129 **/ 1130 int hinic_hwdev_msix_set(struct hinic_hwdev *hwdev, u16 msix_index, 1131 u8 pending_limit, u8 coalesc_timer, 1132 u8 lli_timer_cfg, u8 lli_credit_limit, 1133 u8 resend_timer) 1134 { 1135 return hinic_msix_attr_set(hwdev->hwif, msix_index, 1136 pending_limit, coalesc_timer, 1137 lli_timer_cfg, lli_credit_limit, 1138 resend_timer); 1139 } 1140 1141 /** 1142 * hinic_hwdev_hw_ci_addr_set - set cons idx addr and attributes in HW for sq 1143 * @hwdev: the NIC HW device 1144 * @sq: send queue 1145 * @pending_limit: the maximum pending update ci events (unit 8) 1146 * @coalesc_timer: coalesc period for update ci (unit 8 us) 1147 * 1148 * Return 0 - Success, negative - Failure 1149 **/ 1150 int hinic_hwdev_hw_ci_addr_set(struct hinic_hwdev *hwdev, struct hinic_sq *sq, 1151 u8 pending_limit, u8 coalesc_timer) 1152 { 1153 struct hinic_qp *qp = container_of(sq, struct hinic_qp, sq); 1154 struct hinic_hwif *hwif = hwdev->hwif; 1155 struct hinic_pfhwdev *pfhwdev; 1156 struct hinic_cmd_hw_ci hw_ci; 1157 1158 hw_ci.dma_attr_off = 0; 1159 hw_ci.pending_limit = pending_limit; 1160 hw_ci.coalesc_timer = coalesc_timer; 1161 1162 hw_ci.msix_en = 1; 1163 hw_ci.msix_entry_idx = sq->msix_entry; 1164 1165 hw_ci.func_idx = HINIC_HWIF_FUNC_IDX(hwif); 1166 1167 hw_ci.sq_id = qp->q_id; 1168 1169 hw_ci.ci_addr = ADDR_IN_4BYTES(sq->hw_ci_dma_addr); 1170 1171 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 1172 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, 1173 HINIC_MOD_COMM, 1174 HINIC_COMM_CMD_SQ_HI_CI_SET, 1175 &hw_ci, sizeof(hw_ci), NULL, 1176 NULL, HINIC_MGMT_MSG_SYNC); 1177 } 1178 1179 /** 1180 * hinic_hwdev_set_msix_state- set msix state 1181 * @hwdev: the NIC HW device 1182 * @msix_index: IRQ corresponding index number 1183 * @flag: msix state 1184 * 1185 **/ 1186 void hinic_hwdev_set_msix_state(struct hinic_hwdev *hwdev, u16 msix_index, 1187 enum hinic_msix_state flag) 1188 { 1189 hinic_set_msix_state(hwdev->hwif, msix_index, flag); 1190 } 1191 1192 int hinic_get_board_info(struct hinic_hwdev *hwdev, 1193 struct hinic_comm_board_info *board_info) 1194 { 1195 u16 out_size = sizeof(*board_info); 1196 struct hinic_pfhwdev *pfhwdev; 1197 int err; 1198 1199 if (!hwdev || !board_info) 1200 return -EINVAL; 1201 1202 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev); 1203 1204 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM, 1205 HINIC_COMM_CMD_GET_BOARD_INFO, 1206 board_info, sizeof(*board_info), 1207 board_info, &out_size, HINIC_MGMT_MSG_SYNC); 1208 if (err || board_info->status || !out_size) { 1209 dev_err(&hwdev->hwif->pdev->dev, 1210 "Failed to get board info, err: %d, status: 0x%x, out size: 0x%x\n", 1211 err, board_info->status, out_size); 1212 return -EIO; 1213 } 1214 1215 return 0; 1216 } 1217