2 * Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/pci.h>
19 #include <linux/device.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
23 #include <linux/delay.h>
24 #include <linux/jiffies.h>
25 #include <linux/log2.h>
26 #include <linux/err.h>
28 #include "hinic_hw_if.h"
29 #include "hinic_hw_eqs.h"
30 #include "hinic_hw_mgmt.h"
31 #include "hinic_hw_qp_ctxt.h"
32 #include "hinic_hw_qp.h"
33 #include "hinic_hw_io.h"
34 #include "hinic_hw_dev.h"
36 #define IO_STATUS_TIMEOUT 100
37 #define OUTBOUND_STATE_TIMEOUT 100
38 #define DB_STATE_TIMEOUT 100
40 #define MAX_IRQS(max_qps, num_aeqs, num_ceqs) \
41 (2 * (max_qps) + (num_aeqs) + (num_ceqs))
43 #define ADDR_IN_4BYTES(addr) ((addr) >> 2)
54 enum hw_ioctxt_set_cmdq_depth {
55 HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT,
59 struct hinic_dev_cap {
73 * get_capability - convert device capabilities to NIC capabilities
74 * @hwdev: the HW device to set and convert device capabilities for
75 * @dev_cap: device capabilities from FW
77 * Return 0 - Success, negative - Failure
79 static int get_capability(struct hinic_hwdev *hwdev,
80 struct hinic_dev_cap *dev_cap)
82 struct hinic_cap *nic_cap = &hwdev->nic_cap;
83 int num_aeqs, num_ceqs, num_irqs;
85 if (!HINIC_IS_PF(hwdev->hwif) && !HINIC_IS_PPF(hwdev->hwif))
88 if (dev_cap->intr_type != INTR_MSIX_TYPE)
91 num_aeqs = HINIC_HWIF_NUM_AEQS(hwdev->hwif);
92 num_ceqs = HINIC_HWIF_NUM_CEQS(hwdev->hwif);
93 num_irqs = HINIC_HWIF_NUM_IRQS(hwdev->hwif);
95 /* Each QP has its own (SQ + RQ) interrupts */
96 nic_cap->num_qps = (num_irqs - (num_aeqs + num_ceqs)) / 2;
98 if (nic_cap->num_qps > HINIC_Q_CTXT_MAX)
99 nic_cap->num_qps = HINIC_Q_CTXT_MAX;
101 /* num_qps must be power of 2 */
102 nic_cap->num_qps = BIT(fls(nic_cap->num_qps) - 1);
104 nic_cap->max_qps = dev_cap->max_sqs + 1;
105 if (nic_cap->max_qps != (dev_cap->max_rqs + 1))
108 if (nic_cap->num_qps > nic_cap->max_qps)
109 nic_cap->num_qps = nic_cap->max_qps;
115 * get_cap_from_fw - get device capabilities from FW
116 * @pfhwdev: the PF HW device to get capabilities for
118 * Return 0 - Success, negative - Failure
120 static int get_cap_from_fw(struct hinic_pfhwdev *pfhwdev)
122 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
123 struct hinic_hwif *hwif = hwdev->hwif;
124 struct pci_dev *pdev = hwif->pdev;
125 struct hinic_dev_cap dev_cap;
130 out_len = sizeof(dev_cap);
132 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_CFGM,
133 HINIC_CFG_NIC_CAP, &dev_cap, in_len, &dev_cap,
134 &out_len, HINIC_MGMT_MSG_SYNC);
136 dev_err(&pdev->dev, "Failed to get capability from FW\n");
140 return get_capability(hwdev, &dev_cap);
144 * get_dev_cap - get device capabilities
145 * @hwdev: the NIC HW device to get capabilities for
147 * Return 0 - Success, negative - Failure
149 static int get_dev_cap(struct hinic_hwdev *hwdev)
151 struct hinic_hwif *hwif = hwdev->hwif;
152 struct pci_dev *pdev = hwif->pdev;
153 struct hinic_pfhwdev *pfhwdev;
156 switch (HINIC_FUNC_TYPE(hwif)) {
159 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
161 err = get_cap_from_fw(pfhwdev);
163 dev_err(&pdev->dev, "Failed to get capability from FW\n");
169 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
177 * init_msix - enable the msix and save the entries
178 * @hwdev: the NIC HW device
180 * Return 0 - Success, negative - Failure
182 static int init_msix(struct hinic_hwdev *hwdev)
184 struct hinic_hwif *hwif = hwdev->hwif;
185 struct pci_dev *pdev = hwif->pdev;
186 int nr_irqs, num_aeqs, num_ceqs;
187 size_t msix_entries_size;
190 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
191 num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
192 nr_irqs = MAX_IRQS(HINIC_MAX_QPS, num_aeqs, num_ceqs);
193 if (nr_irqs > HINIC_HWIF_NUM_IRQS(hwif))
194 nr_irqs = HINIC_HWIF_NUM_IRQS(hwif);
196 msix_entries_size = nr_irqs * sizeof(*hwdev->msix_entries);
197 hwdev->msix_entries = devm_kzalloc(&pdev->dev, msix_entries_size,
199 if (!hwdev->msix_entries)
202 for (i = 0; i < nr_irqs; i++)
203 hwdev->msix_entries[i].entry = i;
205 err = pci_enable_msix_exact(pdev, hwdev->msix_entries, nr_irqs);
207 dev_err(&pdev->dev, "Failed to enable pci msix\n");
215 * disable_msix - disable the msix
216 * @hwdev: the NIC HW device
218 static void disable_msix(struct hinic_hwdev *hwdev)
220 struct hinic_hwif *hwif = hwdev->hwif;
221 struct pci_dev *pdev = hwif->pdev;
223 pci_disable_msix(pdev);
227 * hinic_port_msg_cmd - send port msg to mgmt
228 * @hwdev: the NIC HW device
229 * @cmd: the port command
230 * @buf_in: input buffer
231 * @in_size: input size
232 * @buf_out: output buffer
233 * @out_size: returned output size
235 * Return 0 - Success, negative - Failure
237 int hinic_port_msg_cmd(struct hinic_hwdev *hwdev, enum hinic_port_cmd cmd,
238 void *buf_in, u16 in_size, void *buf_out, u16 *out_size)
240 struct hinic_hwif *hwif = hwdev->hwif;
241 struct pci_dev *pdev = hwif->pdev;
242 struct hinic_pfhwdev *pfhwdev;
244 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
245 dev_err(&pdev->dev, "unsupported PCI Function type\n");
249 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
251 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC, cmd,
252 buf_in, in_size, buf_out, out_size,
253 HINIC_MGMT_MSG_SYNC);
257 * init_fw_ctxt- Init Firmware tables before network mgmt and io operations
258 * @hwdev: the NIC HW device
260 * Return 0 - Success, negative - Failure
262 static int init_fw_ctxt(struct hinic_hwdev *hwdev)
264 struct hinic_hwif *hwif = hwdev->hwif;
265 struct pci_dev *pdev = hwif->pdev;
266 struct hinic_cmd_fw_ctxt fw_ctxt;
270 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
271 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
275 fw_ctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
276 fw_ctxt.rx_buf_sz = HINIC_RX_BUF_SZ;
278 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_FWCTXT_INIT,
279 &fw_ctxt, sizeof(fw_ctxt),
280 &fw_ctxt, &out_size);
281 if (err || (out_size != sizeof(fw_ctxt)) || fw_ctxt.status) {
282 dev_err(&pdev->dev, "Failed to init FW ctxt, ret = %d\n",
291 * set_hw_ioctxt - set the shape of the IO queues in FW
292 * @hwdev: the NIC HW device
293 * @rq_depth: rq depth
294 * @sq_depth: sq depth
296 * Return 0 - Success, negative - Failure
298 static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
299 unsigned int sq_depth)
301 struct hinic_hwif *hwif = hwdev->hwif;
302 struct hinic_cmd_hw_ioctxt hw_ioctxt;
303 struct pci_dev *pdev = hwif->pdev;
304 struct hinic_pfhwdev *pfhwdev;
306 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
307 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
311 hw_ioctxt.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
312 hw_ioctxt.ppf_idx = HINIC_HWIF_PPF_IDX(hwif);
314 hw_ioctxt.set_cmdq_depth = HW_IOCTXT_SET_CMDQ_DEPTH_DEFAULT;
315 hw_ioctxt.cmdq_depth = 0;
317 hw_ioctxt.rq_depth = ilog2(rq_depth);
319 hw_ioctxt.rx_buf_sz_idx = HINIC_RX_BUF_SZ_IDX;
321 hw_ioctxt.sq_depth = ilog2(sq_depth);
323 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
325 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
326 HINIC_COMM_CMD_HWCTXT_SET,
327 &hw_ioctxt, sizeof(hw_ioctxt), NULL,
328 NULL, HINIC_MGMT_MSG_SYNC);
331 static int wait_for_outbound_state(struct hinic_hwdev *hwdev)
333 enum hinic_outbound_state outbound_state;
334 struct hinic_hwif *hwif = hwdev->hwif;
335 struct pci_dev *pdev = hwif->pdev;
338 end = jiffies + msecs_to_jiffies(OUTBOUND_STATE_TIMEOUT);
340 outbound_state = hinic_outbound_state_get(hwif);
342 if (outbound_state == HINIC_OUTBOUND_ENABLE)
346 } while (time_before(jiffies, end));
348 dev_err(&pdev->dev, "Wait for OUTBOUND - Timeout\n");
352 static int wait_for_db_state(struct hinic_hwdev *hwdev)
354 struct hinic_hwif *hwif = hwdev->hwif;
355 struct pci_dev *pdev = hwif->pdev;
356 enum hinic_db_state db_state;
359 end = jiffies + msecs_to_jiffies(DB_STATE_TIMEOUT);
361 db_state = hinic_db_state_get(hwif);
363 if (db_state == HINIC_DB_ENABLE)
367 } while (time_before(jiffies, end));
369 dev_err(&pdev->dev, "Wait for DB - Timeout\n");
374 * clear_io_resource - set the IO resources as not active in the NIC
375 * @hwdev: the NIC HW device
377 * Return 0 - Success, negative - Failure
379 static int clear_io_resources(struct hinic_hwdev *hwdev)
381 struct hinic_cmd_clear_io_res cmd_clear_io_res;
382 struct hinic_hwif *hwif = hwdev->hwif;
383 struct pci_dev *pdev = hwif->pdev;
384 struct hinic_pfhwdev *pfhwdev;
387 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
388 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
392 /* sleep 100ms to wait for firmware stopping I/O */
395 cmd_clear_io_res.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
397 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
399 err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_COMM,
400 HINIC_COMM_CMD_IO_RES_CLEAR, &cmd_clear_io_res,
401 sizeof(cmd_clear_io_res), NULL, NULL,
402 HINIC_MGMT_MSG_SYNC);
404 dev_err(&pdev->dev, "Failed to clear IO resources\n");
412 * set_resources_state - set the state of the resources in the NIC
413 * @hwdev: the NIC HW device
414 * @state: the state to set
416 * Return 0 - Success, negative - Failure
418 static int set_resources_state(struct hinic_hwdev *hwdev,
419 enum hinic_res_state state)
421 struct hinic_cmd_set_res_state res_state;
422 struct hinic_hwif *hwif = hwdev->hwif;
423 struct pci_dev *pdev = hwif->pdev;
424 struct hinic_pfhwdev *pfhwdev;
426 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
427 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
431 res_state.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
432 res_state.state = state;
434 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
436 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
438 HINIC_COMM_CMD_RES_STATE_SET,
439 &res_state, sizeof(res_state), NULL,
440 NULL, HINIC_MGMT_MSG_SYNC);
444 * get_base_qpn - get the first qp number
445 * @hwdev: the NIC HW device
446 * @base_qpn: returned qp number
448 * Return 0 - Success, negative - Failure
450 static int get_base_qpn(struct hinic_hwdev *hwdev, u16 *base_qpn)
452 struct hinic_cmd_base_qpn cmd_base_qpn;
453 struct hinic_hwif *hwif = hwdev->hwif;
454 struct pci_dev *pdev = hwif->pdev;
458 cmd_base_qpn.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
460 err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_GET_GLOBAL_QPN,
461 &cmd_base_qpn, sizeof(cmd_base_qpn),
462 &cmd_base_qpn, &out_size);
463 if (err || (out_size != sizeof(cmd_base_qpn)) || cmd_base_qpn.status) {
464 dev_err(&pdev->dev, "Failed to get base qpn, status = %d\n",
465 cmd_base_qpn.status);
469 *base_qpn = cmd_base_qpn.qpn;
474 * hinic_hwdev_ifup - Preparing the HW for passing IO
475 * @hwdev: the NIC HW device
477 * Return 0 - Success, negative - Failure
479 int hinic_hwdev_ifup(struct hinic_hwdev *hwdev)
481 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
482 struct hinic_cap *nic_cap = &hwdev->nic_cap;
483 struct hinic_hwif *hwif = hwdev->hwif;
484 int err, num_aeqs, num_ceqs, num_qps;
485 struct msix_entry *ceq_msix_entries;
486 struct msix_entry *sq_msix_entries;
487 struct msix_entry *rq_msix_entries;
488 struct pci_dev *pdev = hwif->pdev;
491 err = get_base_qpn(hwdev, &base_qpn);
493 dev_err(&pdev->dev, "Failed to get global base qp number\n");
497 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
498 num_ceqs = HINIC_HWIF_NUM_CEQS(hwif);
500 ceq_msix_entries = &hwdev->msix_entries[num_aeqs];
502 err = hinic_io_init(func_to_io, hwif, nic_cap->max_qps, num_ceqs,
505 dev_err(&pdev->dev, "Failed to init IO channel\n");
509 num_qps = nic_cap->num_qps;
510 sq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs];
511 rq_msix_entries = &hwdev->msix_entries[num_aeqs + num_ceqs + num_qps];
513 err = hinic_io_create_qps(func_to_io, base_qpn, num_qps,
514 sq_msix_entries, rq_msix_entries);
516 dev_err(&pdev->dev, "Failed to create QPs\n");
520 err = wait_for_db_state(hwdev);
522 dev_warn(&pdev->dev, "db - disabled, try again\n");
523 hinic_db_state_set(hwif, HINIC_DB_ENABLE);
526 err = set_hw_ioctxt(hwdev, HINIC_SQ_DEPTH, HINIC_RQ_DEPTH);
528 dev_err(&pdev->dev, "Failed to set HW IO ctxt\n");
535 hinic_io_destroy_qps(func_to_io, num_qps);
538 hinic_io_free(func_to_io);
543 * hinic_hwdev_ifdown - Closing the HW for passing IO
544 * @hwdev: the NIC HW device
547 void hinic_hwdev_ifdown(struct hinic_hwdev *hwdev)
549 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
550 struct hinic_cap *nic_cap = &hwdev->nic_cap;
552 clear_io_resources(hwdev);
554 hinic_io_destroy_qps(func_to_io, nic_cap->num_qps);
555 hinic_io_free(func_to_io);
559 * hinic_hwdev_cb_register - register callback handler for MGMT events
560 * @hwdev: the NIC HW device
561 * @cmd: the mgmt event
562 * @handle: private data for the handler
563 * @handler: event handler
565 void hinic_hwdev_cb_register(struct hinic_hwdev *hwdev,
566 enum hinic_mgmt_msg_cmd cmd, void *handle,
567 void (*handler)(void *handle, void *buf_in,
568 u16 in_size, void *buf_out,
571 struct hinic_hwif *hwif = hwdev->hwif;
572 struct pci_dev *pdev = hwif->pdev;
573 struct hinic_pfhwdev *pfhwdev;
574 struct hinic_nic_cb *nic_cb;
577 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
578 dev_err(&pdev->dev, "unsupported PCI Function type\n");
582 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
584 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
585 nic_cb = &pfhwdev->nic_cb[cmd_cb];
587 nic_cb->handler = handler;
588 nic_cb->handle = handle;
589 nic_cb->cb_state = HINIC_CB_ENABLED;
593 * hinic_hwdev_cb_unregister - unregister callback handler for MGMT events
594 * @hwdev: the NIC HW device
595 * @cmd: the mgmt event
597 void hinic_hwdev_cb_unregister(struct hinic_hwdev *hwdev,
598 enum hinic_mgmt_msg_cmd cmd)
600 struct hinic_hwif *hwif = hwdev->hwif;
601 struct pci_dev *pdev = hwif->pdev;
602 struct hinic_pfhwdev *pfhwdev;
603 struct hinic_nic_cb *nic_cb;
606 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
607 dev_err(&pdev->dev, "unsupported PCI Function type\n");
611 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
613 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
614 nic_cb = &pfhwdev->nic_cb[cmd_cb];
616 nic_cb->cb_state &= ~HINIC_CB_ENABLED;
618 while (nic_cb->cb_state & HINIC_CB_RUNNING)
621 nic_cb->handler = NULL;
625 * nic_mgmt_msg_handler - nic mgmt event handler
626 * @handle: private data for the handler
627 * @buf_in: input buffer
628 * @in_size: input size
629 * @buf_out: output buffer
630 * @out_size: returned output size
632 static void nic_mgmt_msg_handler(void *handle, u8 cmd, void *buf_in,
633 u16 in_size, void *buf_out, u16 *out_size)
635 struct hinic_pfhwdev *pfhwdev = handle;
636 enum hinic_cb_state cb_state;
637 struct hinic_nic_cb *nic_cb;
638 struct hinic_hwdev *hwdev;
639 struct hinic_hwif *hwif;
640 struct pci_dev *pdev;
643 hwdev = &pfhwdev->hwdev;
647 if ((cmd < HINIC_MGMT_MSG_CMD_BASE) ||
648 (cmd >= HINIC_MGMT_MSG_CMD_MAX)) {
649 dev_err(&pdev->dev, "unknown L2NIC event, cmd = %d\n", cmd);
653 cmd_cb = cmd - HINIC_MGMT_MSG_CMD_BASE;
655 nic_cb = &pfhwdev->nic_cb[cmd_cb];
657 cb_state = cmpxchg(&nic_cb->cb_state,
659 HINIC_CB_ENABLED | HINIC_CB_RUNNING);
661 if ((cb_state == HINIC_CB_ENABLED) && (nic_cb->handler))
662 nic_cb->handler(nic_cb->handle, buf_in,
663 in_size, buf_out, out_size);
665 dev_err(&pdev->dev, "Unhandled NIC Event %d\n", cmd);
667 nic_cb->cb_state &= ~HINIC_CB_RUNNING;
671 * init_pfhwdev - Initialize the extended components of PF
672 * @pfhwdev: the HW device for PF
674 * Return 0 - success, negative - failure
676 static int init_pfhwdev(struct hinic_pfhwdev *pfhwdev)
678 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
679 struct hinic_hwif *hwif = hwdev->hwif;
680 struct pci_dev *pdev = hwif->pdev;
683 err = hinic_pf_to_mgmt_init(&pfhwdev->pf_to_mgmt, hwif);
685 dev_err(&pdev->dev, "Failed to initialize PF to MGMT channel\n");
689 hinic_register_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
690 pfhwdev, nic_mgmt_msg_handler);
692 hinic_set_pf_action(hwif, HINIC_PF_MGMT_ACTIVE);
697 * free_pfhwdev - Free the extended components of PF
698 * @pfhwdev: the HW device for PF
700 static void free_pfhwdev(struct hinic_pfhwdev *pfhwdev)
702 struct hinic_hwdev *hwdev = &pfhwdev->hwdev;
704 hinic_set_pf_action(hwdev->hwif, HINIC_PF_MGMT_INIT);
706 hinic_unregister_mgmt_msg_cb(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC);
708 hinic_pf_to_mgmt_free(&pfhwdev->pf_to_mgmt);
712 * hinic_init_hwdev - Initialize the NIC HW
713 * @pdev: the NIC pci device
715 * Return initialized NIC HW device
717 * Initialize the NIC HW device and return a pointer to it
719 struct hinic_hwdev *hinic_init_hwdev(struct pci_dev *pdev)
721 struct hinic_pfhwdev *pfhwdev;
722 struct hinic_hwdev *hwdev;
723 struct hinic_hwif *hwif;
726 hwif = devm_kzalloc(&pdev->dev, sizeof(*hwif), GFP_KERNEL);
728 return ERR_PTR(-ENOMEM);
730 err = hinic_init_hwif(hwif, pdev);
732 dev_err(&pdev->dev, "Failed to init HW interface\n");
736 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
737 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
742 pfhwdev = devm_kzalloc(&pdev->dev, sizeof(*pfhwdev), GFP_KERNEL);
745 goto err_pfhwdev_alloc;
748 hwdev = &pfhwdev->hwdev;
751 err = init_msix(hwdev);
753 dev_err(&pdev->dev, "Failed to init msix\n");
757 err = wait_for_outbound_state(hwdev);
759 dev_warn(&pdev->dev, "outbound - disabled, try again\n");
760 hinic_outbound_state_set(hwif, HINIC_OUTBOUND_ENABLE);
763 num_aeqs = HINIC_HWIF_NUM_AEQS(hwif);
765 err = hinic_aeqs_init(&hwdev->aeqs, hwif, num_aeqs,
766 HINIC_DEFAULT_AEQ_LEN, HINIC_EQ_PAGE_SIZE,
767 hwdev->msix_entries);
769 dev_err(&pdev->dev, "Failed to init async event queues\n");
773 err = init_pfhwdev(pfhwdev);
775 dev_err(&pdev->dev, "Failed to init PF HW device\n");
776 goto err_init_pfhwdev;
779 err = get_dev_cap(hwdev);
781 dev_err(&pdev->dev, "Failed to get device capabilities\n");
785 err = init_fw_ctxt(hwdev);
787 dev_err(&pdev->dev, "Failed to init function table\n");
788 goto err_init_fw_ctxt;
791 err = set_resources_state(hwdev, HINIC_RES_ACTIVE);
793 dev_err(&pdev->dev, "Failed to set resources state\n");
794 goto err_resources_state;
802 free_pfhwdev(pfhwdev);
805 hinic_aeqs_free(&hwdev->aeqs);
813 hinic_free_hwif(hwif);
818 * hinic_free_hwdev - Free the NIC HW device
819 * @hwdev: the NIC HW device
821 void hinic_free_hwdev(struct hinic_hwdev *hwdev)
823 struct hinic_pfhwdev *pfhwdev = container_of(hwdev,
824 struct hinic_pfhwdev,
827 set_resources_state(hwdev, HINIC_RES_CLEAN);
829 free_pfhwdev(pfhwdev);
831 hinic_aeqs_free(&hwdev->aeqs);
835 hinic_free_hwif(hwdev->hwif);
839 * hinic_hwdev_num_qps - return the number QPs available for use
840 * @hwdev: the NIC HW device
842 * Return number QPs available for use
844 int hinic_hwdev_num_qps(struct hinic_hwdev *hwdev)
846 struct hinic_cap *nic_cap = &hwdev->nic_cap;
848 return nic_cap->num_qps;
852 * hinic_hwdev_get_sq - get SQ
853 * @hwdev: the NIC HW device
854 * @i: the position of the SQ
856 * Return: the SQ in the i position
858 struct hinic_sq *hinic_hwdev_get_sq(struct hinic_hwdev *hwdev, int i)
860 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
861 struct hinic_qp *qp = &func_to_io->qps[i];
863 if (i >= hinic_hwdev_num_qps(hwdev))
870 * hinic_hwdev_get_sq - get RQ
871 * @hwdev: the NIC HW device
872 * @i: the position of the RQ
874 * Return: the RQ in the i position
876 struct hinic_rq *hinic_hwdev_get_rq(struct hinic_hwdev *hwdev, int i)
878 struct hinic_func_to_io *func_to_io = &hwdev->func_to_io;
879 struct hinic_qp *qp = &func_to_io->qps[i];
881 if (i >= hinic_hwdev_num_qps(hwdev))
888 * hinic_hwdev_msix_cnt_set - clear message attribute counters for msix entry
889 * @hwdev: the NIC HW device
890 * @msix_index: msix_index
892 * Return 0 - Success, negative - Failure
894 int hinic_hwdev_msix_cnt_set(struct hinic_hwdev *hwdev, u16 msix_index)
896 return hinic_msix_attr_cnt_clear(hwdev->hwif, msix_index);
900 * hinic_hwdev_msix_set - set message attribute for msix entry
901 * @hwdev: the NIC HW device
902 * @msix_index: msix_index
903 * @pending_limit: the maximum pending interrupt events (unit 8)
904 * @coalesc_timer: coalesc period for interrupt (unit 8 us)
905 * @lli_timer: replenishing period for low latency credit (unit 8 us)
906 * @lli_credit_limit: maximum credits for low latency msix messages (unit 8)
907 * @resend_timer: maximum wait for resending msix (unit coalesc period)
909 * Return 0 - Success, negative - Failure
911 int hinic_hwdev_msix_set(struct hinic_hwdev *hwdev, u16 msix_index,
912 u8 pending_limit, u8 coalesc_timer,
913 u8 lli_timer_cfg, u8 lli_credit_limit,
916 return hinic_msix_attr_set(hwdev->hwif, msix_index,
917 pending_limit, coalesc_timer,
918 lli_timer_cfg, lli_credit_limit,
923 * hinic_hwdev_hw_ci_addr_set - set cons idx addr and attributes in HW for sq
924 * @hwdev: the NIC HW device
926 * @pending_limit: the maximum pending update ci events (unit 8)
927 * @coalesc_timer: coalesc period for update ci (unit 8 us)
929 * Return 0 - Success, negative - Failure
931 int hinic_hwdev_hw_ci_addr_set(struct hinic_hwdev *hwdev, struct hinic_sq *sq,
932 u8 pending_limit, u8 coalesc_timer)
934 struct hinic_qp *qp = container_of(sq, struct hinic_qp, sq);
935 struct hinic_hwif *hwif = hwdev->hwif;
936 struct pci_dev *pdev = hwif->pdev;
937 struct hinic_pfhwdev *pfhwdev;
938 struct hinic_cmd_hw_ci hw_ci;
940 if (!HINIC_IS_PF(hwif) && !HINIC_IS_PPF(hwif)) {
941 dev_err(&pdev->dev, "Unsupported PCI Function type\n");
945 hw_ci.dma_attr_off = 0;
946 hw_ci.pending_limit = pending_limit;
947 hw_ci.coalesc_timer = coalesc_timer;
950 hw_ci.msix_entry_idx = sq->msix_entry;
952 hw_ci.func_idx = HINIC_HWIF_FUNC_IDX(hwif);
954 hw_ci.sq_id = qp->q_id;
956 hw_ci.ci_addr = ADDR_IN_4BYTES(sq->hw_ci_dma_addr);
958 pfhwdev = container_of(hwdev, struct hinic_pfhwdev, hwdev);
959 return hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt,
961 HINIC_COMM_CMD_SQ_HI_CI_SET,
962 &hw_ci, sizeof(hw_ci), NULL,
963 NULL, HINIC_MGMT_MSG_SYNC);