2 * QLogic iSCSI Offload Driver
3 * Copyright (c) 2016 Cavium Inc.
5 * This software is available under the terms of the GNU General Public License
6 * (GPL) Version 2, available from the file COPYING in the main directory of
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/kernel.h>
13 #include <linux/if_arp.h>
14 #include <scsi/iscsi_if.h>
15 #include <linux/inet.h>
17 #include <linux/list.h>
18 #include <linux/kthread.h>
20 #include <linux/if_vlan.h>
21 #include <linux/cpu.h>
22 #include <linux/iscsi_boot_sysfs.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_eh.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi.h>
32 #include "qedi_iscsi.h"
34 static uint qedi_fw_debug;
35 module_param(qedi_fw_debug, uint, 0644);
36 MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
38 uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
39 module_param(qedi_dbg_log, uint, 0644);
40 MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
43 module_param(qedi_io_tracing, uint, 0644);
44 MODULE_PARM_DESC(qedi_io_tracing,
45 " Enable logging of SCSI requests/completions into trace buffer. (default off).");
47 const struct qed_iscsi_ops *qedi_ops;
48 static struct scsi_transport_template *qedi_scsi_transport;
49 static struct pci_driver qedi_pci_driver;
50 static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
51 static LIST_HEAD(qedi_udev_list);
52 /* Static function declaration */
53 static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
54 static void qedi_free_global_queues(struct qedi_ctx *qedi);
55 static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
56 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
57 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
58 static struct nvm_iscsi_block *qedi_get_nvram_block(struct qedi_ctx *qedi);
60 static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
62 struct qedi_ctx *qedi;
63 struct qedi_endpoint *qedi_ep;
64 struct iscsi_eqe_data *data;
67 if (!context || !fw_handle) {
68 QEDI_ERR(NULL, "Recv event with ctx NULL\n");
72 qedi = (struct qedi_ctx *)context;
73 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
74 "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
76 data = (struct iscsi_eqe_data *)fw_handle;
77 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
78 "icid=0x%x conn_id=0x%x err-code=0x%x error-pdu-opcode-reserved=0x%x\n",
79 data->icid, data->conn_id, data->error_code,
80 data->error_pdu_opcode_reserved);
82 qedi_ep = qedi->ep_tbl[data->icid];
85 QEDI_WARN(&qedi->dbg_ctx,
86 "Cannot process event, ep already disconnected, cid=0x%x\n",
92 switch (fw_event_code) {
93 case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
94 if (qedi_ep->state == EP_STATE_OFLDCONN_START)
95 qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
97 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
99 case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
100 qedi_ep->state = EP_STATE_DISCONN_COMPL;
101 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
103 case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
104 qedi_process_iscsi_error(qedi_ep, data);
106 case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
107 case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
108 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
109 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
110 case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
111 case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
112 case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
113 qedi_process_tcp_error(qedi_ep, data);
116 QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
123 static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
125 struct qedi_uio_dev *udev = uinfo->priv;
126 struct qedi_ctx *qedi = udev->qedi;
128 if (!capable(CAP_NET_ADMIN))
131 if (udev->uio_dev != -1)
135 udev->uio_dev = iminor(inode);
136 qedi_reset_uio_rings(udev);
137 set_bit(UIO_DEV_OPENED, &qedi->flags);
143 static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
145 struct qedi_uio_dev *udev = uinfo->priv;
146 struct qedi_ctx *qedi = udev->qedi;
149 clear_bit(UIO_DEV_OPENED, &qedi->flags);
150 qedi_ll2_free_skbs(qedi);
154 static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
157 free_page((unsigned long)udev->uctrl);
161 if (udev->ll2_ring) {
162 free_page((unsigned long)udev->ll2_ring);
163 udev->ll2_ring = NULL;
167 free_pages((unsigned long)udev->ll2_buf, 2);
168 udev->ll2_buf = NULL;
172 static void __qedi_free_uio(struct qedi_uio_dev *udev)
174 uio_unregister_device(&udev->qedi_uinfo);
176 __qedi_free_uio_rings(udev);
178 pci_dev_put(udev->pdev);
182 static void qedi_free_uio(struct qedi_uio_dev *udev)
187 list_del_init(&udev->list);
188 __qedi_free_uio(udev);
191 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
193 struct qedi_ctx *qedi = NULL;
194 struct qedi_uio_ctrl *uctrl = NULL;
199 spin_lock_bh(&qedi->ll2_lock);
200 uctrl->host_rx_cons = 0;
201 uctrl->hw_rx_prod = 0;
202 uctrl->hw_rx_bd_prod = 0;
203 uctrl->host_rx_bd_cons = 0;
205 memset(udev->ll2_ring, 0, udev->ll2_ring_size);
206 memset(udev->ll2_buf, 0, udev->ll2_buf_size);
207 spin_unlock_bh(&qedi->ll2_lock);
210 static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
214 if (udev->ll2_ring || udev->ll2_buf)
217 /* Memory for control area. */
218 udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL);
222 /* Allocating memory for LL2 ring */
223 udev->ll2_ring_size = QEDI_PAGE_SIZE;
224 udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
225 if (!udev->ll2_ring) {
227 goto exit_alloc_ring;
230 /* Allocating memory for Tx/Rx pkt buffer */
231 udev->ll2_buf_size = TX_RX_RING * LL2_SINGLE_BUF_SIZE;
232 udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
233 udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
235 if (!udev->ll2_buf) {
242 free_page((unsigned long)udev->ll2_ring);
243 udev->ll2_ring = NULL;
248 static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
250 struct qedi_uio_dev *udev = NULL;
253 list_for_each_entry(udev, &qedi_udev_list, list) {
254 if (udev->pdev == qedi->pdev) {
256 if (__qedi_alloc_uio_rings(udev)) {
265 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
274 udev->pdev = qedi->pdev;
276 rc = __qedi_alloc_uio_rings(udev);
280 list_add(&udev->list, &qedi_udev_list);
282 pci_dev_get(udev->pdev);
285 udev->tx_pkt = udev->ll2_buf;
286 udev->rx_pkt = udev->ll2_buf + LL2_SINGLE_BUF_SIZE;
295 static int qedi_init_uio(struct qedi_ctx *qedi)
297 struct qedi_uio_dev *udev = qedi->udev;
298 struct uio_info *uinfo;
304 uinfo = &udev->qedi_uinfo;
306 uinfo->mem[0].addr = (unsigned long)udev->uctrl;
307 uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
308 uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
310 uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
311 uinfo->mem[1].size = udev->ll2_ring_size;
312 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
314 uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
315 uinfo->mem[2].size = udev->ll2_buf_size;
316 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
318 uinfo->name = "qedi_uio";
319 uinfo->version = QEDI_MODULE_VERSION;
320 uinfo->irq = UIO_IRQ_CUSTOM;
322 uinfo->open = qedi_uio_open;
323 uinfo->release = qedi_uio_close;
325 if (udev->uio_dev == -1) {
329 ret = uio_register_device(&udev->pdev->dev, uinfo);
331 QEDI_ERR(&qedi->dbg_ctx,
332 "UIO registration failed\n");
340 static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
341 struct qed_sb_info *sb_info, u16 sb_id)
343 struct status_block_e4 *sb_virt;
347 sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
348 sizeof(struct status_block_e4), &sb_phys,
351 QEDI_ERR(&qedi->dbg_ctx,
352 "Status block allocation failed for id = %d.\n",
357 ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
358 sb_id, QED_SB_TYPE_STORAGE);
360 QEDI_ERR(&qedi->dbg_ctx,
361 "Status block initialization failed for id = %d.\n",
369 static void qedi_free_sb(struct qedi_ctx *qedi)
371 struct qed_sb_info *sb_info;
374 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
375 sb_info = &qedi->sb_array[id];
376 if (sb_info->sb_virt)
377 dma_free_coherent(&qedi->pdev->dev,
378 sizeof(*sb_info->sb_virt),
379 (void *)sb_info->sb_virt,
384 static void qedi_free_fp(struct qedi_ctx *qedi)
386 kfree(qedi->fp_array);
387 kfree(qedi->sb_array);
390 static void qedi_destroy_fp(struct qedi_ctx *qedi)
396 static int qedi_alloc_fp(struct qedi_ctx *qedi)
400 qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
401 sizeof(struct qedi_fastpath), GFP_KERNEL);
402 if (!qedi->fp_array) {
403 QEDI_ERR(&qedi->dbg_ctx,
404 "fastpath fp array allocation failed.\n");
408 qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
409 sizeof(struct qed_sb_info), GFP_KERNEL);
410 if (!qedi->sb_array) {
411 QEDI_ERR(&qedi->dbg_ctx,
412 "fastpath sb array allocation failed.\n");
424 static void qedi_int_fp(struct qedi_ctx *qedi)
426 struct qedi_fastpath *fp;
429 memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
430 sizeof(*qedi->fp_array));
431 memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
432 sizeof(*qedi->sb_array));
434 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
435 fp = &qedi->fp_array[id];
436 fp->sb_info = &qedi->sb_array[id];
439 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
442 /* fp_array[i] ---- irq cookie
443 * So init data which is needed in int ctx
448 static int qedi_prepare_fp(struct qedi_ctx *qedi)
450 struct qedi_fastpath *fp;
453 ret = qedi_alloc_fp(qedi);
459 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
460 fp = &qedi->fp_array[id];
461 ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
463 QEDI_ERR(&qedi->dbg_ctx,
464 "SB allocation and initialization failed.\n");
479 static int qedi_setup_cid_que(struct qedi_ctx *qedi)
483 qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
484 sizeof(u32), GFP_KERNEL);
485 if (!qedi->cid_que.cid_que_base)
488 qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
489 sizeof(struct qedi_conn *),
491 if (!qedi->cid_que.conn_cid_tbl) {
492 kfree(qedi->cid_que.cid_que_base);
493 qedi->cid_que.cid_que_base = NULL;
497 qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
498 qedi->cid_que.cid_q_prod_idx = 0;
499 qedi->cid_que.cid_q_cons_idx = 0;
500 qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
501 qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
503 for (i = 0; i < qedi->max_active_conns; i++) {
504 qedi->cid_que.cid_que[i] = i;
505 qedi->cid_que.conn_cid_tbl[i] = NULL;
511 static void qedi_release_cid_que(struct qedi_ctx *qedi)
513 kfree(qedi->cid_que.cid_que_base);
514 qedi->cid_que.cid_que_base = NULL;
516 kfree(qedi->cid_que.conn_cid_tbl);
517 qedi->cid_que.conn_cid_tbl = NULL;
520 static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
521 u16 start_id, u16 next)
523 id_tbl->start = start_id;
526 spin_lock_init(&id_tbl->lock);
527 id_tbl->table = kcalloc(BITS_TO_LONGS(size), sizeof(long), GFP_KERNEL);
534 static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
536 kfree(id_tbl->table);
537 id_tbl->table = NULL;
540 int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
545 if (id >= id_tbl->max)
548 spin_lock(&id_tbl->lock);
549 if (!test_bit(id, id_tbl->table)) {
550 set_bit(id, id_tbl->table);
553 spin_unlock(&id_tbl->lock);
557 u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
561 spin_lock(&id_tbl->lock);
562 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
563 if (id >= id_tbl->max) {
564 id = QEDI_LOCAL_PORT_INVALID;
565 if (id_tbl->next != 0) {
566 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
567 if (id >= id_tbl->next)
568 id = QEDI_LOCAL_PORT_INVALID;
572 if (id < id_tbl->max) {
573 set_bit(id, id_tbl->table);
574 id_tbl->next = (id + 1) & (id_tbl->max - 1);
578 spin_unlock(&id_tbl->lock);
583 void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
585 if (id == QEDI_LOCAL_PORT_INVALID)
589 if (id >= id_tbl->max)
592 clear_bit(id, id_tbl->table);
595 static void qedi_cm_free_mem(struct qedi_ctx *qedi)
599 qedi_free_id_tbl(&qedi->lcl_port_tbl);
602 static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
606 qedi->ep_tbl = kzalloc((qedi->max_active_conns *
607 sizeof(struct qedi_endpoint *)), GFP_KERNEL);
610 port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
611 if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
612 QEDI_LOCAL_PORT_MIN, port_id)) {
613 qedi_cm_free_mem(qedi);
620 static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
622 struct Scsi_Host *shost;
623 struct qedi_ctx *qedi = NULL;
625 shost = iscsi_host_alloc(&qedi_host_template,
626 sizeof(struct qedi_ctx), 0);
628 QEDI_ERR(NULL, "Could not allocate shost\n");
629 goto exit_setup_shost;
632 shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA - 1;
633 shost->max_channel = 0;
635 shost->max_cmd_len = 16;
636 shost->transportt = qedi_scsi_transport;
638 qedi = iscsi_host_priv(shost);
639 memset(qedi, 0, sizeof(*qedi));
641 qedi->dbg_ctx.host_no = shost->host_no;
643 qedi->dbg_ctx.pdev = pdev;
644 qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
645 qedi->max_sqes = QEDI_SQ_SIZE;
647 if (shost_use_blk_mq(shost))
648 shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
650 pci_set_drvdata(pdev, qedi);
656 static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
658 struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
659 struct qedi_uio_dev *udev;
660 struct qedi_uio_ctrl *uctrl;
661 struct skb_work_list *work;
665 QEDI_ERR(NULL, "qedi is NULL\n");
669 if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
670 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
671 "UIO DEV is not opened\n");
679 work = kzalloc(sizeof(*work), GFP_ATOMIC);
681 QEDI_WARN(&qedi->dbg_ctx,
682 "Could not allocate work so dropping frame.\n");
687 INIT_LIST_HEAD(&work->list);
690 if (skb_vlan_tag_present(skb))
691 work->vlan_id = skb_vlan_tag_get(skb);
694 __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
696 spin_lock_bh(&qedi->ll2_lock);
697 list_add_tail(&work->list, &qedi->ll2_skb_list);
699 ++uctrl->hw_rx_prod_cnt;
700 prod = (uctrl->hw_rx_prod + 1) % RX_RING;
701 if (prod != uctrl->host_rx_cons) {
702 uctrl->hw_rx_prod = prod;
703 spin_unlock_bh(&qedi->ll2_lock);
704 wake_up_process(qedi->ll2_recv_thread);
708 spin_unlock_bh(&qedi->ll2_lock);
712 /* map this skb to iscsiuio mmaped region */
713 static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
716 struct qedi_uio_dev *udev = NULL;
717 struct qedi_uio_ctrl *uctrl = NULL;
718 struct qedi_rx_bd rxbd;
719 struct qedi_rx_bd *p_rxbd;
725 QEDI_ERR(NULL, "qedi is NULL\n");
731 pkt = udev->rx_pkt + (uctrl->hw_rx_prod * LL2_SINGLE_BUF_SIZE);
732 len = min_t(u32, skb->len, (u32)LL2_SINGLE_BUF_SIZE);
733 memcpy(pkt, skb->data, len);
735 memset(&rxbd, 0, sizeof(rxbd));
736 rxbd.rx_pkt_index = uctrl->hw_rx_prod;
737 rxbd.rx_pkt_len = len;
738 rxbd.vlan_id = vlan_id;
740 uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
741 rx_bd_prod = uctrl->hw_rx_bd_prod;
742 p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
743 p_rxbd += rx_bd_prod;
745 memcpy(p_rxbd, &rxbd, sizeof(rxbd));
747 /* notify the iscsiuio about new packet */
748 uio_event_notify(&udev->qedi_uinfo);
753 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
755 struct skb_work_list *work, *work_tmp;
757 spin_lock_bh(&qedi->ll2_lock);
758 list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
759 list_del(&work->list);
761 kfree_skb(work->skb);
764 spin_unlock_bh(&qedi->ll2_lock);
767 static int qedi_ll2_recv_thread(void *arg)
769 struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
770 struct skb_work_list *work, *work_tmp;
772 set_user_nice(current, -20);
774 while (!kthread_should_stop()) {
775 spin_lock_bh(&qedi->ll2_lock);
776 list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
778 list_del(&work->list);
779 qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
780 kfree_skb(work->skb);
783 set_current_state(TASK_INTERRUPTIBLE);
784 spin_unlock_bh(&qedi->ll2_lock);
788 __set_current_state(TASK_RUNNING);
792 static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
799 num_sq_pages = (MAX_OUSTANDING_TASKS_PER_CON * 8) / PAGE_SIZE;
801 qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
803 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
804 "Number of CQ count is %d\n", qedi->num_queues);
806 memset(&qedi->pf_params.iscsi_pf_params, 0,
807 sizeof(qedi->pf_params.iscsi_pf_params));
809 qedi->p_cpuq = pci_alloc_consistent(qedi->pdev,
810 qedi->num_queues * sizeof(struct qedi_glbl_q_params),
813 QEDI_ERR(&qedi->dbg_ctx, "pci_alloc_consistent fail\n");
818 rval = qedi_alloc_global_queues(qedi);
820 QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
825 qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
826 qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
827 qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
828 qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
829 qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
830 qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
831 qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
832 qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
833 qedi->pf_params.iscsi_pf_params.two_msl_timer = 4000;
834 qedi->pf_params.iscsi_pf_params.max_fin_rt = 2;
836 for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
837 if ((1 << log_page_size) == PAGE_SIZE)
840 qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
842 qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
843 (u64)qedi->hw_p_cpuq;
845 /* RQ BDQ initializations.
846 * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
847 * rqe_log_size: 8 for 256B RQE
849 qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
850 /* BDQ address and size */
851 qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
852 qedi->bdq_pbl_list_dma;
853 qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
854 qedi->bdq_pbl_list_num_entries;
855 qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
857 /* cq_num_entries: num_tasks + rq_num_entries */
858 qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
860 qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
861 qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
867 /* Free DMA coherent memory for array of queue pointers we pass to qed */
868 static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
873 size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
874 pci_free_consistent(qedi->pdev, size, qedi->p_cpuq,
878 qedi_free_global_queues(qedi);
880 kfree(qedi->global_queues);
883 static void qedi_get_boot_tgt_info(struct nvm_iscsi_block *block,
884 struct qedi_boot_target *tgt, u8 index)
888 ipv6_en = !!(block->generic.ctrl_flags &
889 NVM_ISCSI_CFG_GEN_IPV6_ENABLED);
891 snprintf(tgt->iscsi_name, sizeof(tgt->iscsi_name), "%s\n",
892 block->target[index].target_name.byte);
894 tgt->ipv6_en = ipv6_en;
897 snprintf(tgt->ip_addr, IPV6_LEN, "%pI6\n",
898 block->target[index].ipv6_addr.byte);
900 snprintf(tgt->ip_addr, IPV4_LEN, "%pI4\n",
901 block->target[index].ipv4_addr.byte);
904 static int qedi_find_boot_info(struct qedi_ctx *qedi,
905 struct qed_mfw_tlv_iscsi *iscsi,
906 struct nvm_iscsi_block *block)
908 struct qedi_boot_target *pri_tgt = NULL, *sec_tgt = NULL;
909 u32 pri_ctrl_flags = 0, sec_ctrl_flags = 0, found = 0;
910 struct iscsi_cls_session *cls_sess;
911 struct iscsi_cls_conn *cls_conn;
912 struct qedi_conn *qedi_conn;
913 struct iscsi_session *sess;
914 struct iscsi_conn *conn;
918 pri_ctrl_flags = !!(block->target[0].ctrl_flags &
919 NVM_ISCSI_CFG_TARGET_ENABLED);
920 if (pri_ctrl_flags) {
921 pri_tgt = kzalloc(sizeof(*pri_tgt), GFP_KERNEL);
924 qedi_get_boot_tgt_info(block, pri_tgt, 0);
927 sec_ctrl_flags = !!(block->target[1].ctrl_flags &
928 NVM_ISCSI_CFG_TARGET_ENABLED);
929 if (sec_ctrl_flags) {
930 sec_tgt = kzalloc(sizeof(*sec_tgt), GFP_KERNEL);
935 qedi_get_boot_tgt_info(block, sec_tgt, 1);
938 for (i = 0; i < qedi->max_active_conns; i++) {
939 qedi_conn = qedi_get_conn_from_id(qedi, i);
943 if (qedi_conn->ep->ip_type == TCP_IPV4)
944 snprintf(ep_ip_addr, IPV4_LEN, "%pI4\n",
945 qedi_conn->ep->dst_addr);
947 snprintf(ep_ip_addr, IPV6_LEN, "%pI6\n",
948 qedi_conn->ep->dst_addr);
950 cls_conn = qedi_conn->cls_conn;
951 conn = cls_conn->dd_data;
952 cls_sess = iscsi_conn_to_session(cls_conn);
953 sess = cls_sess->dd_data;
955 if (!iscsi_is_session_online(cls_sess))
958 if (!sess->targetname)
961 if (pri_ctrl_flags) {
962 if (!strcmp(pri_tgt->iscsi_name, sess->targetname) &&
963 !strcmp(pri_tgt->ip_addr, ep_ip_addr)) {
969 if (sec_ctrl_flags) {
970 if (!strcmp(sec_tgt->iscsi_name, sess->targetname) &&
971 !strcmp(sec_tgt->ip_addr, ep_ip_addr)) {
979 if (conn->hdrdgst_en) {
980 iscsi->header_digest_set = true;
981 iscsi->header_digest = 1;
984 if (conn->datadgst_en) {
985 iscsi->data_digest_set = true;
986 iscsi->data_digest = 1;
988 iscsi->boot_taget_portal_set = true;
989 iscsi->boot_taget_portal = sess->tpgt;
1004 static void qedi_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
1006 struct qedi_ctx *qedi;
1009 QEDI_INFO(NULL, QEDI_LOG_EVT,
1010 "dev is NULL so ignoring get_generic_tlv_data request.\n");
1013 qedi = (struct qedi_ctx *)dev;
1015 memset(data, 0, sizeof(struct qed_generic_tlvs));
1016 ether_addr_copy(data->mac[0], qedi->mac);
1020 * Protocol TLV handler
1022 static void qedi_get_protocol_tlv_data(void *dev, void *data)
1024 struct qed_mfw_tlv_iscsi *iscsi = data;
1025 struct qed_iscsi_stats *fw_iscsi_stats;
1026 struct nvm_iscsi_block *block = NULL;
1027 u32 chap_en = 0, mchap_en = 0;
1028 struct qedi_ctx *qedi = dev;
1031 fw_iscsi_stats = kmalloc(sizeof(*fw_iscsi_stats), GFP_KERNEL);
1032 if (!fw_iscsi_stats) {
1033 QEDI_ERR(&qedi->dbg_ctx,
1034 "Could not allocate memory for fw_iscsi_stats.\n");
1038 mutex_lock(&qedi->stats_lock);
1039 /* Query firmware for offload stats */
1040 qedi_ops->get_stats(qedi->cdev, fw_iscsi_stats);
1041 mutex_unlock(&qedi->stats_lock);
1043 iscsi->rx_frames_set = true;
1044 iscsi->rx_frames = fw_iscsi_stats->iscsi_rx_packet_cnt;
1045 iscsi->rx_bytes_set = true;
1046 iscsi->rx_bytes = fw_iscsi_stats->iscsi_rx_bytes_cnt;
1047 iscsi->tx_frames_set = true;
1048 iscsi->tx_frames = fw_iscsi_stats->iscsi_tx_packet_cnt;
1049 iscsi->tx_bytes_set = true;
1050 iscsi->tx_bytes = fw_iscsi_stats->iscsi_tx_bytes_cnt;
1051 iscsi->frame_size_set = true;
1052 iscsi->frame_size = qedi->ll2_mtu;
1053 block = qedi_get_nvram_block(qedi);
1055 chap_en = !!(block->generic.ctrl_flags &
1056 NVM_ISCSI_CFG_GEN_CHAP_ENABLED);
1057 mchap_en = !!(block->generic.ctrl_flags &
1058 NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED);
1060 iscsi->auth_method_set = (chap_en || mchap_en) ? true : false;
1061 iscsi->auth_method = 1;
1063 iscsi->auth_method = 2;
1065 iscsi->auth_method = 3;
1067 iscsi->tx_desc_size_set = true;
1068 iscsi->tx_desc_size = QEDI_SQ_SIZE;
1069 iscsi->rx_desc_size_set = true;
1070 iscsi->rx_desc_size = QEDI_CQ_SIZE;
1072 /* tpgt, hdr digest, data digest */
1073 rval = qedi_find_boot_info(qedi, iscsi, block);
1075 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1076 "Boot target not set");
1079 kfree(fw_iscsi_stats);
1084 static void qedi_link_update(void *dev, struct qed_link_output *link)
1086 struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
1088 if (link->link_up) {
1089 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
1090 atomic_set(&qedi->link_state, QEDI_LINK_UP);
1092 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1093 "Link Down event.\n");
1094 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1098 static struct qed_iscsi_cb_ops qedi_cb_ops = {
1100 .link_update = qedi_link_update,
1101 .get_protocol_tlv_data = qedi_get_protocol_tlv_data,
1102 .get_generic_tlv_data = qedi_get_generic_tlv_data,
1106 static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
1107 u16 que_idx, struct qedi_percpu_s *p)
1109 struct qedi_work *qedi_work;
1110 struct qedi_conn *q_conn;
1111 struct iscsi_conn *conn;
1112 struct qedi_cmd *qedi_cmd;
1116 iscsi_cid = cqe->cqe_common.conn_id;
1117 q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
1119 QEDI_WARN(&qedi->dbg_ctx,
1120 "Session no longer exists for cid=0x%x!!\n",
1124 conn = q_conn->cls_conn->dd_data;
1126 switch (cqe->cqe_common.cqe_type) {
1127 case ISCSI_CQE_TYPE_SOLICITED:
1128 case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
1129 qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
1134 INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
1135 qedi_cmd->cqe_work.qedi = qedi;
1136 memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
1137 qedi_cmd->cqe_work.que_idx = que_idx;
1138 qedi_cmd->cqe_work.is_solicited = true;
1139 list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
1141 case ISCSI_CQE_TYPE_UNSOLICITED:
1142 case ISCSI_CQE_TYPE_DUMMY:
1143 case ISCSI_CQE_TYPE_TASK_CLEANUP:
1144 qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
1149 INIT_LIST_HEAD(&qedi_work->list);
1150 qedi_work->qedi = qedi;
1151 memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
1152 qedi_work->que_idx = que_idx;
1153 qedi_work->is_solicited = false;
1154 list_add_tail(&qedi_work->list, &p->work_list);
1158 QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
1163 static bool qedi_process_completions(struct qedi_fastpath *fp)
1165 struct qedi_ctx *qedi = fp->qedi;
1166 struct qed_sb_info *sb_info = fp->sb_info;
1167 struct status_block_e4 *sb = sb_info->sb_virt;
1168 struct qedi_percpu_s *p = NULL;
1169 struct global_queue *que;
1171 unsigned long flags;
1172 union iscsi_cqe *cqe;
1176 /* Get the current firmware producer index */
1177 prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1179 if (prod_idx >= QEDI_CQ_SIZE)
1180 prod_idx = prod_idx % QEDI_CQ_SIZE;
1182 que = qedi->global_queues[fp->sb_id];
1183 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1184 "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
1185 que, prod_idx, que->cq_cons_idx, fp->sb_id);
1187 qedi->intr_cpu = fp->sb_id;
1188 cpu = smp_processor_id();
1189 p = &per_cpu(qedi_percpu, cpu);
1191 if (unlikely(!p->iothread))
1194 spin_lock_irqsave(&p->p_work_lock, flags);
1195 while (que->cq_cons_idx != prod_idx) {
1196 cqe = &que->cq[que->cq_cons_idx];
1198 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1199 "cqe=%p prod_idx=%d cons_idx=%d.\n",
1200 cqe, prod_idx, que->cq_cons_idx);
1202 ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
1204 QEDI_WARN(&qedi->dbg_ctx,
1205 "Dropping CQE 0x%x for cid=0x%x.\n",
1206 que->cq_cons_idx, cqe->cqe_common.conn_id);
1209 if (que->cq_cons_idx == QEDI_CQ_SIZE)
1210 que->cq_cons_idx = 0;
1212 wake_up_process(p->iothread);
1213 spin_unlock_irqrestore(&p->p_work_lock, flags);
1218 static bool qedi_fp_has_work(struct qedi_fastpath *fp)
1220 struct qedi_ctx *qedi = fp->qedi;
1221 struct global_queue *que;
1222 struct qed_sb_info *sb_info = fp->sb_info;
1223 struct status_block_e4 *sb = sb_info->sb_virt;
1228 /* Get the current firmware producer index */
1229 prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1231 /* Get the pointer to the global CQ this completion is on */
1232 que = qedi->global_queues[fp->sb_id];
1234 /* prod idx wrap around uint16 */
1235 if (prod_idx >= QEDI_CQ_SIZE)
1236 prod_idx = prod_idx % QEDI_CQ_SIZE;
1238 return (que->cq_cons_idx != prod_idx);
1241 /* MSI-X fastpath handler code */
1242 static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
1244 struct qedi_fastpath *fp = dev_id;
1245 struct qedi_ctx *qedi = fp->qedi;
1246 bool wake_io_thread = true;
1248 qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
1251 wake_io_thread = qedi_process_completions(fp);
1252 if (wake_io_thread) {
1253 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1254 "process already running\n");
1257 if (qedi_fp_has_work(fp) == 0)
1258 qed_sb_update_sb_idx(fp->sb_info);
1260 /* Check for more work */
1263 if (qedi_fp_has_work(fp) == 0)
1264 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1271 /* simd handler for MSI/INTa */
1272 static void qedi_simd_int_handler(void *cookie)
1274 /* Cookie is qedi_ctx struct */
1275 struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
1277 QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
1280 #define QEDI_SIMD_HANDLER_NUM 0
1281 static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
1285 if (qedi->int_info.msix_cnt) {
1286 for (i = 0; i < qedi->int_info.used_cnt; i++) {
1287 synchronize_irq(qedi->int_info.msix[i].vector);
1288 irq_set_affinity_hint(qedi->int_info.msix[i].vector,
1290 free_irq(qedi->int_info.msix[i].vector,
1291 &qedi->fp_array[i]);
1294 qedi_ops->common->simd_handler_clean(qedi->cdev,
1295 QEDI_SIMD_HANDLER_NUM);
1298 qedi->int_info.used_cnt = 0;
1299 qedi_ops->common->set_fp_int(qedi->cdev, 0);
1302 static int qedi_request_msix_irq(struct qedi_ctx *qedi)
1306 cpu = cpumask_first(cpu_online_mask);
1307 for (i = 0; i < MIN_NUM_CPUS_MSIX(qedi); i++) {
1308 rc = request_irq(qedi->int_info.msix[i].vector,
1309 qedi_msix_handler, 0, "qedi",
1310 &qedi->fp_array[i]);
1313 QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
1314 qedi_sync_free_irqs(qedi);
1317 qedi->int_info.used_cnt++;
1318 rc = irq_set_affinity_hint(qedi->int_info.msix[i].vector,
1320 cpu = cpumask_next(cpu, cpu_online_mask);
1326 static int qedi_setup_int(struct qedi_ctx *qedi)
1330 rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus());
1331 rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
1333 goto exit_setup_int;
1335 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1336 "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
1337 qedi->int_info.msix_cnt, num_online_cpus());
1339 if (qedi->int_info.msix_cnt) {
1340 rc = qedi_request_msix_irq(qedi);
1341 goto exit_setup_int;
1343 qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
1344 QEDI_SIMD_HANDLER_NUM,
1345 qedi_simd_int_handler);
1346 qedi->int_info.used_cnt = 1;
1353 static void qedi_free_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1355 if (qedi->iscsi_image)
1356 dma_free_coherent(&qedi->pdev->dev,
1357 sizeof(struct qedi_nvm_iscsi_image),
1358 qedi->iscsi_image, qedi->nvm_buf_dma);
1361 static int qedi_alloc_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1363 struct qedi_nvm_iscsi_image nvm_image;
1365 qedi->iscsi_image = dma_zalloc_coherent(&qedi->pdev->dev,
1369 if (!qedi->iscsi_image) {
1370 QEDI_ERR(&qedi->dbg_ctx, "Could not allocate NVM BUF.\n");
1373 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1374 "NVM BUF addr=0x%p dma=0x%llx.\n", qedi->iscsi_image,
1380 static void qedi_free_bdq(struct qedi_ctx *qedi)
1384 if (qedi->bdq_pbl_list)
1385 dma_free_coherent(&qedi->pdev->dev, PAGE_SIZE,
1386 qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
1389 dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
1390 qedi->bdq_pbl, qedi->bdq_pbl_dma);
1392 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1393 if (qedi->bdq[i].buf_addr) {
1394 dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
1395 qedi->bdq[i].buf_addr,
1396 qedi->bdq[i].buf_dma);
1401 static void qedi_free_global_queues(struct qedi_ctx *qedi)
1404 struct global_queue **gl = qedi->global_queues;
1406 for (i = 0; i < qedi->num_queues; i++) {
1411 dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
1412 gl[i]->cq, gl[i]->cq_dma);
1414 dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
1415 gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
1419 qedi_free_bdq(qedi);
1420 qedi_free_nvm_iscsi_cfg(qedi);
1423 static int qedi_alloc_bdq(struct qedi_ctx *qedi)
1426 struct scsi_bd *pbl;
1430 /* Alloc dma memory for BDQ buffers */
1431 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1432 qedi->bdq[i].buf_addr =
1433 dma_alloc_coherent(&qedi->pdev->dev,
1435 &qedi->bdq[i].buf_dma,
1437 if (!qedi->bdq[i].buf_addr) {
1438 QEDI_ERR(&qedi->dbg_ctx,
1439 "Could not allocate BDQ buffer %d.\n", i);
1444 /* Alloc dma memory for BDQ page buffer list */
1445 qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
1446 qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, PAGE_SIZE);
1447 qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
1449 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
1450 qedi->rq_num_entries);
1452 qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1453 qedi->bdq_pbl_mem_size,
1454 &qedi->bdq_pbl_dma, GFP_KERNEL);
1455 if (!qedi->bdq_pbl) {
1456 QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
1461 * Populate BDQ PBL with physical and virtual address of individual
1464 pbl = (struct scsi_bd *)qedi->bdq_pbl;
1465 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1467 cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
1469 cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
1470 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1471 "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
1472 pbl, pbl->address.hi, pbl->address.lo, i);
1473 pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
1474 pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
1475 pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
1476 pbl->opaque.iscsi_opaque.opaque = cpu_to_le16(i);
1480 /* Allocate list of PBL pages */
1481 qedi->bdq_pbl_list = dma_zalloc_coherent(&qedi->pdev->dev, PAGE_SIZE,
1482 &qedi->bdq_pbl_list_dma,
1484 if (!qedi->bdq_pbl_list) {
1485 QEDI_ERR(&qedi->dbg_ctx,
1486 "Could not allocate list of PBL pages.\n");
1491 * Now populate PBL list with pages that contain pointers to the
1492 * individual buffers.
1494 qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size / PAGE_SIZE;
1495 list = (u64 *)qedi->bdq_pbl_list;
1496 page = qedi->bdq_pbl_list_dma;
1497 for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
1498 *list = qedi->bdq_pbl_dma;
1506 static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
1516 * Number of global queues (CQ / RQ). This should
1517 * be <= number of available MSIX vectors for the PF
1519 if (!qedi->num_queues) {
1520 QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
1524 /* Make sure we allocated the PBL that will contain the physical
1525 * addresses of our queues
1527 if (!qedi->p_cpuq) {
1529 goto mem_alloc_failure;
1532 qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
1533 qedi->num_queues), GFP_KERNEL);
1534 if (!qedi->global_queues) {
1535 QEDI_ERR(&qedi->dbg_ctx,
1536 "Unable to allocate global queues array ptr memory\n");
1539 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1540 "qedi->global_queues=%p.\n", qedi->global_queues);
1542 /* Allocate DMA coherent buffers for BDQ */
1543 status = qedi_alloc_bdq(qedi);
1545 goto mem_alloc_failure;
1547 /* Allocate DMA coherent buffers for NVM_ISCSI_CFG */
1548 status = qedi_alloc_nvm_iscsi_cfg(qedi);
1550 goto mem_alloc_failure;
1552 /* Allocate a CQ and an associated PBL for each MSI-X
1555 for (i = 0; i < qedi->num_queues; i++) {
1556 qedi->global_queues[i] =
1557 kzalloc(sizeof(*qedi->global_queues[0]),
1559 if (!qedi->global_queues[i]) {
1560 QEDI_ERR(&qedi->dbg_ctx,
1561 "Unable to allocation global queue %d.\n", i);
1563 goto mem_alloc_failure;
1566 qedi->global_queues[i]->cq_mem_size =
1567 (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
1568 qedi->global_queues[i]->cq_mem_size =
1569 (qedi->global_queues[i]->cq_mem_size +
1570 (QEDI_PAGE_SIZE - 1));
1572 qedi->global_queues[i]->cq_pbl_size =
1573 (qedi->global_queues[i]->cq_mem_size /
1574 QEDI_PAGE_SIZE) * sizeof(void *);
1575 qedi->global_queues[i]->cq_pbl_size =
1576 (qedi->global_queues[i]->cq_pbl_size +
1577 (QEDI_PAGE_SIZE - 1));
1579 qedi->global_queues[i]->cq = dma_zalloc_coherent(&qedi->pdev->dev,
1580 qedi->global_queues[i]->cq_mem_size,
1581 &qedi->global_queues[i]->cq_dma,
1584 if (!qedi->global_queues[i]->cq) {
1585 QEDI_WARN(&qedi->dbg_ctx,
1586 "Could not allocate cq.\n");
1588 goto mem_alloc_failure;
1590 qedi->global_queues[i]->cq_pbl = dma_zalloc_coherent(&qedi->pdev->dev,
1591 qedi->global_queues[i]->cq_pbl_size,
1592 &qedi->global_queues[i]->cq_pbl_dma,
1595 if (!qedi->global_queues[i]->cq_pbl) {
1596 QEDI_WARN(&qedi->dbg_ctx,
1597 "Could not allocate cq PBL.\n");
1599 goto mem_alloc_failure;
1603 num_pages = qedi->global_queues[i]->cq_mem_size /
1605 page = qedi->global_queues[i]->cq_dma;
1606 pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
1608 while (num_pages--) {
1611 *pbl = (u32)((u64)page >> 32);
1613 page += QEDI_PAGE_SIZE;
1617 list = (u32 *)qedi->p_cpuq;
1620 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
1621 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
1622 * to the physical address which contains an array of pointers to the
1623 * physical addresses of the specific queue pages.
1625 for (i = 0; i < qedi->num_queues; i++) {
1626 *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
1628 *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
1633 *list = (u32)((u64)0 >> 32);
1640 qedi_free_global_queues(qedi);
1644 int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1654 /* Calculate appropriate queue and PBL sizes */
1655 ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
1656 ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
1658 ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
1659 ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
1661 ep->sq = dma_zalloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
1662 &ep->sq_dma, GFP_KERNEL);
1664 QEDI_WARN(&qedi->dbg_ctx,
1665 "Could not allocate send queue.\n");
1669 ep->sq_pbl = dma_zalloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
1670 &ep->sq_pbl_dma, GFP_KERNEL);
1672 QEDI_WARN(&qedi->dbg_ctx,
1673 "Could not allocate send queue PBL.\n");
1679 num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
1681 pbl = (u32 *)ep->sq_pbl;
1683 while (num_pages--) {
1686 *pbl = (u32)((u64)page >> 32);
1688 page += QEDI_PAGE_SIZE;
1694 dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1700 void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1703 dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
1706 dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1710 int qedi_get_task_idx(struct qedi_ctx *qedi)
1715 tmp_idx = find_first_zero_bit(qedi->task_idx_map,
1716 MAX_ISCSI_TASK_ENTRIES);
1718 if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
1719 QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
1724 if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
1731 void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
1733 if (!test_and_clear_bit(idx, qedi->task_idx_map))
1734 QEDI_ERR(&qedi->dbg_ctx,
1735 "FW task context, already cleared, tid=0x%x\n", idx);
1738 void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
1739 struct qedi_cmd *cmd)
1741 qedi->itt_map[tid].itt = proto_itt;
1742 qedi->itt_map[tid].p_cmd = cmd;
1744 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1745 "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
1746 qedi->itt_map[tid].itt);
1749 void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
1753 for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
1754 if (qedi->itt_map[i].itt == itt) {
1756 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1757 "Ref itt=0x%x, found at tid=0x%x\n",
1766 void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
1768 *proto_itt = qedi->itt_map[tid].itt;
1769 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1770 "Get itt map tid [0x%x with proto itt[0x%x]",
1774 struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
1776 struct qedi_cmd *cmd = NULL;
1778 if (tid >= MAX_ISCSI_TASK_ENTRIES)
1781 cmd = qedi->itt_map[tid].p_cmd;
1782 if (cmd->task_id != tid)
1785 qedi->itt_map[tid].p_cmd = NULL;
1790 static int qedi_alloc_itt(struct qedi_ctx *qedi)
1792 qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
1793 sizeof(struct qedi_itt_map), GFP_KERNEL);
1794 if (!qedi->itt_map) {
1795 QEDI_ERR(&qedi->dbg_ctx,
1796 "Unable to allocate itt map array memory\n");
1802 static void qedi_free_itt(struct qedi_ctx *qedi)
1804 kfree(qedi->itt_map);
1807 static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
1808 .rx_cb = qedi_ll2_rx,
1812 static int qedi_percpu_io_thread(void *arg)
1814 struct qedi_percpu_s *p = arg;
1815 struct qedi_work *work, *tmp;
1816 unsigned long flags;
1817 LIST_HEAD(work_list);
1819 set_user_nice(current, -20);
1821 while (!kthread_should_stop()) {
1822 spin_lock_irqsave(&p->p_work_lock, flags);
1823 while (!list_empty(&p->work_list)) {
1824 list_splice_init(&p->work_list, &work_list);
1825 spin_unlock_irqrestore(&p->p_work_lock, flags);
1827 list_for_each_entry_safe(work, tmp, &work_list, list) {
1828 list_del_init(&work->list);
1829 qedi_fp_process_cqes(work);
1830 if (!work->is_solicited)
1834 spin_lock_irqsave(&p->p_work_lock, flags);
1836 set_current_state(TASK_INTERRUPTIBLE);
1837 spin_unlock_irqrestore(&p->p_work_lock, flags);
1840 __set_current_state(TASK_RUNNING);
1845 static int qedi_cpu_online(unsigned int cpu)
1847 struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1848 struct task_struct *thread;
1850 thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
1852 "qedi_thread/%d", cpu);
1854 return PTR_ERR(thread);
1856 kthread_bind(thread, cpu);
1857 p->iothread = thread;
1858 wake_up_process(thread);
1862 static int qedi_cpu_offline(unsigned int cpu)
1864 struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1865 struct qedi_work *work, *tmp;
1866 struct task_struct *thread;
1868 spin_lock_bh(&p->p_work_lock);
1869 thread = p->iothread;
1872 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
1873 list_del_init(&work->list);
1874 qedi_fp_process_cqes(work);
1875 if (!work->is_solicited)
1879 spin_unlock_bh(&p->p_work_lock);
1881 kthread_stop(thread);
1885 void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
1887 struct qed_ll2_params params;
1889 qedi_recover_all_conns(qedi);
1891 qedi_ops->ll2->stop(qedi->cdev);
1892 qedi_ll2_free_skbs(qedi);
1894 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
1895 qedi->ll2_mtu, mtu);
1896 memset(¶ms, 0, sizeof(params));
1897 qedi->ll2_mtu = mtu;
1898 params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
1899 params.drop_ttl0_packets = 0;
1900 params.rx_vlan_stripping = 1;
1901 ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
1902 qedi_ops->ll2->start(qedi->cdev, ¶ms);
1906 * qedi_get_nvram_block: - Scan through the iSCSI NVRAM block (while accounting
1907 * for gaps) for the matching absolute-pf-id of the QEDI device.
1909 static struct nvm_iscsi_block *
1910 qedi_get_nvram_block(struct qedi_ctx *qedi)
1915 struct nvm_iscsi_block *block;
1917 pf = qedi->dev_info.common.abs_pf_id;
1918 block = &qedi->iscsi_image->iscsi_cfg.block[0];
1919 for (i = 0; i < NUM_OF_ISCSI_PF_SUPPORTED; i++, block++) {
1920 flags = ((block->id) & NVM_ISCSI_CFG_BLK_CTRL_FLAG_MASK) >>
1921 NVM_ISCSI_CFG_BLK_CTRL_FLAG_OFFSET;
1922 if (flags & (NVM_ISCSI_CFG_BLK_CTRL_FLAG_IS_NOT_EMPTY |
1923 NVM_ISCSI_CFG_BLK_CTRL_FLAG_PF_MAPPED) &&
1924 (pf == (block->id & NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_MASK)
1925 >> NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_OFFSET))
1931 static ssize_t qedi_show_boot_eth_info(void *data, int type, char *buf)
1933 struct qedi_ctx *qedi = data;
1934 struct nvm_iscsi_initiator *initiator;
1936 u32 ipv6_en, dhcp_en, ip_len;
1937 struct nvm_iscsi_block *block;
1938 char *fmt, *ip, *sub, *gw;
1940 block = qedi_get_nvram_block(qedi);
1944 initiator = &block->initiator;
1945 ipv6_en = block->generic.ctrl_flags &
1946 NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
1947 dhcp_en = block->generic.ctrl_flags &
1948 NVM_ISCSI_CFG_GEN_DHCP_TCPIP_CONFIG_ENABLED;
1949 /* Static IP assignments. */
1950 fmt = ipv6_en ? "%pI6\n" : "%pI4\n";
1951 ip = ipv6_en ? initiator->ipv6.addr.byte : initiator->ipv4.addr.byte;
1952 ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
1953 sub = ipv6_en ? initiator->ipv6.subnet_mask.byte :
1954 initiator->ipv4.subnet_mask.byte;
1955 gw = ipv6_en ? initiator->ipv6.gateway.byte :
1956 initiator->ipv4.gateway.byte;
1957 /* DHCP IP adjustments. */
1958 fmt = dhcp_en ? "%s\n" : fmt;
1960 ip = ipv6_en ? "0::0" : "0.0.0.0";
1963 ip_len = ipv6_en ? 5 : 8;
1967 case ISCSI_BOOT_ETH_IP_ADDR:
1968 rc = snprintf(buf, ip_len, fmt, ip);
1970 case ISCSI_BOOT_ETH_SUBNET_MASK:
1971 rc = snprintf(buf, ip_len, fmt, sub);
1973 case ISCSI_BOOT_ETH_GATEWAY:
1974 rc = snprintf(buf, ip_len, fmt, gw);
1976 case ISCSI_BOOT_ETH_FLAGS:
1977 rc = snprintf(buf, 3, "%hhd\n",
1978 SYSFS_FLAG_FW_SEL_BOOT);
1980 case ISCSI_BOOT_ETH_INDEX:
1981 rc = snprintf(buf, 3, "0\n");
1983 case ISCSI_BOOT_ETH_MAC:
1984 rc = sysfs_format_mac(buf, qedi->mac, ETH_ALEN);
1986 case ISCSI_BOOT_ETH_VLAN:
1987 rc = snprintf(buf, 12, "%d\n",
1988 GET_FIELD2(initiator->generic_cont0,
1989 NVM_ISCSI_CFG_INITIATOR_VLAN));
1991 case ISCSI_BOOT_ETH_ORIGIN:
1993 rc = snprintf(buf, 3, "3\n");
2003 static umode_t qedi_eth_get_attr_visibility(void *data, int type)
2008 case ISCSI_BOOT_ETH_FLAGS:
2009 case ISCSI_BOOT_ETH_MAC:
2010 case ISCSI_BOOT_ETH_INDEX:
2011 case ISCSI_BOOT_ETH_IP_ADDR:
2012 case ISCSI_BOOT_ETH_SUBNET_MASK:
2013 case ISCSI_BOOT_ETH_GATEWAY:
2014 case ISCSI_BOOT_ETH_ORIGIN:
2015 case ISCSI_BOOT_ETH_VLAN:
2025 static ssize_t qedi_show_boot_ini_info(void *data, int type, char *buf)
2027 struct qedi_ctx *qedi = data;
2028 struct nvm_iscsi_initiator *initiator;
2030 struct nvm_iscsi_block *block;
2032 block = qedi_get_nvram_block(qedi);
2036 initiator = &block->initiator;
2039 case ISCSI_BOOT_INI_INITIATOR_NAME:
2040 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2041 initiator->initiator_name.byte);
2050 static umode_t qedi_ini_get_attr_visibility(void *data, int type)
2055 case ISCSI_BOOT_INI_INITIATOR_NAME:
2066 qedi_show_boot_tgt_info(struct qedi_ctx *qedi, int type,
2067 char *buf, enum qedi_nvm_tgts idx)
2070 u32 ctrl_flags, ipv6_en, chap_en, mchap_en, ip_len;
2071 struct nvm_iscsi_block *block;
2072 char *chap_name, *chap_secret;
2073 char *mchap_name, *mchap_secret;
2075 block = qedi_get_nvram_block(qedi);
2077 goto exit_show_tgt_info;
2079 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2080 "Port:%d, tgt_idx:%d\n",
2081 GET_FIELD2(block->id, NVM_ISCSI_CFG_BLK_MAPPED_PF_ID), idx);
2083 ctrl_flags = block->target[idx].ctrl_flags &
2084 NVM_ISCSI_CFG_TARGET_ENABLED;
2087 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2088 "Target disabled\n");
2089 goto exit_show_tgt_info;
2092 ipv6_en = block->generic.ctrl_flags &
2093 NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2094 ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2095 chap_en = block->generic.ctrl_flags &
2096 NVM_ISCSI_CFG_GEN_CHAP_ENABLED;
2097 chap_name = chap_en ? block->initiator.chap_name.byte : NULL;
2098 chap_secret = chap_en ? block->initiator.chap_password.byte : NULL;
2100 mchap_en = block->generic.ctrl_flags &
2101 NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED;
2102 mchap_name = mchap_en ? block->target[idx].chap_name.byte : NULL;
2103 mchap_secret = mchap_en ? block->target[idx].chap_password.byte : NULL;
2106 case ISCSI_BOOT_TGT_NAME:
2107 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2108 block->target[idx].target_name.byte);
2110 case ISCSI_BOOT_TGT_IP_ADDR:
2112 rc = snprintf(buf, ip_len, "%pI6\n",
2113 block->target[idx].ipv6_addr.byte);
2115 rc = snprintf(buf, ip_len, "%pI4\n",
2116 block->target[idx].ipv4_addr.byte);
2118 case ISCSI_BOOT_TGT_PORT:
2119 rc = snprintf(buf, 12, "%d\n",
2120 GET_FIELD2(block->target[idx].generic_cont0,
2121 NVM_ISCSI_CFG_TARGET_TCP_PORT));
2123 case ISCSI_BOOT_TGT_LUN:
2124 rc = snprintf(buf, 22, "%.*d\n",
2125 block->target[idx].lun.value[1],
2126 block->target[idx].lun.value[0]);
2128 case ISCSI_BOOT_TGT_CHAP_NAME:
2129 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2132 case ISCSI_BOOT_TGT_CHAP_SECRET:
2133 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
2136 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2137 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2140 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2141 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
2144 case ISCSI_BOOT_TGT_FLAGS:
2145 rc = snprintf(buf, 3, "%hhd\n", SYSFS_FLAG_FW_SEL_BOOT);
2147 case ISCSI_BOOT_TGT_NIC_ASSOC:
2148 rc = snprintf(buf, 3, "0\n");
2159 static ssize_t qedi_show_boot_tgt_pri_info(void *data, int type, char *buf)
2161 struct qedi_ctx *qedi = data;
2163 return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_PRI);
2166 static ssize_t qedi_show_boot_tgt_sec_info(void *data, int type, char *buf)
2168 struct qedi_ctx *qedi = data;
2170 return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_SEC);
2173 static umode_t qedi_tgt_get_attr_visibility(void *data, int type)
2178 case ISCSI_BOOT_TGT_NAME:
2179 case ISCSI_BOOT_TGT_IP_ADDR:
2180 case ISCSI_BOOT_TGT_PORT:
2181 case ISCSI_BOOT_TGT_LUN:
2182 case ISCSI_BOOT_TGT_CHAP_NAME:
2183 case ISCSI_BOOT_TGT_CHAP_SECRET:
2184 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2185 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2186 case ISCSI_BOOT_TGT_NIC_ASSOC:
2187 case ISCSI_BOOT_TGT_FLAGS:
2197 static void qedi_boot_release(void *data)
2199 struct qedi_ctx *qedi = data;
2201 scsi_host_put(qedi->shost);
2204 static int qedi_get_boot_info(struct qedi_ctx *qedi)
2207 struct qedi_nvm_iscsi_image nvm_image;
2209 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2210 "Get NVM iSCSI CFG image\n");
2211 ret = qedi_ops->common->nvm_get_image(qedi->cdev,
2212 QED_NVM_IMAGE_ISCSI_CFG,
2213 (char *)qedi->iscsi_image,
2216 QEDI_ERR(&qedi->dbg_ctx,
2217 "Could not get NVM image. ret = %d\n", ret);
2222 static int qedi_setup_boot_info(struct qedi_ctx *qedi)
2224 struct iscsi_boot_kobj *boot_kobj;
2226 if (qedi_get_boot_info(qedi))
2229 qedi->boot_kset = iscsi_boot_create_host_kset(qedi->shost->host_no);
2230 if (!qedi->boot_kset)
2233 if (!scsi_host_get(qedi->shost))
2236 boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 0, qedi,
2237 qedi_show_boot_tgt_pri_info,
2238 qedi_tgt_get_attr_visibility,
2243 if (!scsi_host_get(qedi->shost))
2246 boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 1, qedi,
2247 qedi_show_boot_tgt_sec_info,
2248 qedi_tgt_get_attr_visibility,
2253 if (!scsi_host_get(qedi->shost))
2256 boot_kobj = iscsi_boot_create_initiator(qedi->boot_kset, 0, qedi,
2257 qedi_show_boot_ini_info,
2258 qedi_ini_get_attr_visibility,
2263 if (!scsi_host_get(qedi->shost))
2266 boot_kobj = iscsi_boot_create_ethernet(qedi->boot_kset, 0, qedi,
2267 qedi_show_boot_eth_info,
2268 qedi_eth_get_attr_visibility,
2276 scsi_host_put(qedi->shost);
2278 iscsi_boot_destroy_kset(qedi->boot_kset);
2282 static void __qedi_remove(struct pci_dev *pdev, int mode)
2284 struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2287 if (qedi->tmf_thread) {
2288 flush_workqueue(qedi->tmf_thread);
2289 destroy_workqueue(qedi->tmf_thread);
2290 qedi->tmf_thread = NULL;
2293 if (qedi->offload_thread) {
2294 flush_workqueue(qedi->offload_thread);
2295 destroy_workqueue(qedi->offload_thread);
2296 qedi->offload_thread = NULL;
2299 #ifdef CONFIG_DEBUG_FS
2300 qedi_dbg_host_exit(&qedi->dbg_ctx);
2302 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
2303 qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2305 qedi_sync_free_irqs(qedi);
2307 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2308 qedi_ops->stop(qedi->cdev);
2309 qedi_ops->ll2->stop(qedi->cdev);
2312 if (mode == QEDI_MODE_NORMAL)
2313 qedi_free_iscsi_pf_param(qedi);
2315 rval = qedi_ops->common->update_drv_state(qedi->cdev, false);
2317 QEDI_ERR(&qedi->dbg_ctx, "Failed to send drv state to MFW\n");
2319 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2320 qedi_ops->common->slowpath_stop(qedi->cdev);
2321 qedi_ops->common->remove(qedi->cdev);
2324 qedi_destroy_fp(qedi);
2326 if (mode == QEDI_MODE_NORMAL) {
2327 qedi_release_cid_que(qedi);
2328 qedi_cm_free_mem(qedi);
2329 qedi_free_uio(qedi->udev);
2330 qedi_free_itt(qedi);
2332 iscsi_host_remove(qedi->shost);
2333 iscsi_host_free(qedi->shost);
2335 if (qedi->ll2_recv_thread) {
2336 kthread_stop(qedi->ll2_recv_thread);
2337 qedi->ll2_recv_thread = NULL;
2339 qedi_ll2_free_skbs(qedi);
2341 if (qedi->boot_kset)
2342 iscsi_boot_destroy_kset(qedi->boot_kset);
2346 static int __qedi_probe(struct pci_dev *pdev, int mode)
2348 struct qedi_ctx *qedi;
2349 struct qed_ll2_params params;
2354 struct qed_link_params link_params;
2355 struct qed_slowpath_params sp_params;
2356 struct qed_probe_params qed_params;
2357 void *task_start, *task_end;
2361 if (mode != QEDI_MODE_RECOVERY) {
2362 qedi = qedi_host_alloc(pdev);
2368 qedi = pci_get_drvdata(pdev);
2371 memset(&qed_params, 0, sizeof(qed_params));
2372 qed_params.protocol = QED_PROTOCOL_ISCSI;
2373 qed_params.dp_module = dp_module;
2374 qed_params.dp_level = dp_level;
2375 qed_params.is_vf = is_vf;
2376 qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
2379 QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
2383 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2385 rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2389 if (mode != QEDI_MODE_RECOVERY) {
2390 rc = qedi_set_iscsi_pf_param(qedi);
2393 QEDI_ERR(&qedi->dbg_ctx,
2394 "Set iSCSI pf param fail\n");
2399 qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2401 rc = qedi_prepare_fp(qedi);
2403 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
2404 goto free_pf_params;
2407 /* Start the Slowpath-process */
2408 memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
2409 sp_params.int_mode = QED_INT_MODE_MSIX;
2410 sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
2411 sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
2412 sp_params.drv_rev = QEDI_DRIVER_REV_VER;
2413 sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
2414 strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
2415 rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
2417 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
2421 /* update_pf_params needs to be called before and after slowpath
2424 qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2426 rc = qedi_setup_int(qedi);
2428 goto stop_iscsi_func;
2430 qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2432 /* Learn information crucial for qedi to progress */
2433 rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2435 goto stop_iscsi_func;
2437 /* Record BDQ producer doorbell addresses */
2438 qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
2439 qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
2440 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2441 "BDQ primary_prod=%p secondary_prod=%p.\n",
2442 qedi->bdq_primary_prod,
2443 qedi->bdq_secondary_prod);
2446 * We need to write the number of BDs in the BDQ we've preallocated so
2447 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
2450 qedi->bdq_prod_idx = QEDI_BDQ_NUM;
2451 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2452 "Writing %d to primary and secondary BDQ doorbell registers.\n",
2453 qedi->bdq_prod_idx);
2454 writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
2455 tmp = readw(qedi->bdq_primary_prod);
2456 writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
2457 tmp = readw(qedi->bdq_secondary_prod);
2459 ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
2460 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
2463 sprintf(host_buf, "host_%d", qedi->shost->host_no);
2464 qedi_ops->common->set_name(qedi->cdev, host_buf);
2466 qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
2468 memset(¶ms, 0, sizeof(params));
2469 params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
2470 qedi->ll2_mtu = DEF_PATH_MTU;
2471 params.drop_ttl0_packets = 0;
2472 params.rx_vlan_stripping = 1;
2473 ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
2475 if (mode != QEDI_MODE_RECOVERY) {
2476 /* set up rx path */
2477 INIT_LIST_HEAD(&qedi->ll2_skb_list);
2478 spin_lock_init(&qedi->ll2_lock);
2479 /* start qedi context */
2480 spin_lock_init(&qedi->hba_lock);
2481 spin_lock_init(&qedi->task_idx_lock);
2482 mutex_init(&qedi->stats_lock);
2484 qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
2485 qedi_ops->ll2->start(qedi->cdev, ¶ms);
2487 if (mode != QEDI_MODE_RECOVERY) {
2488 qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
2493 rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
2494 qedi, qedi_iscsi_event_cb);
2497 QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
2501 task_start = qedi_get_task_mem(&qedi->tasks, 0);
2502 task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
2503 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2504 "Task context start=%p, end=%p block_size=%u.\n",
2505 task_start, task_end, qedi->tasks.size);
2507 memset(&link_params, 0, sizeof(link_params));
2508 link_params.link_up = true;
2509 rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
2511 QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
2512 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2515 #ifdef CONFIG_DEBUG_FS
2516 qedi_dbg_host_init(&qedi->dbg_ctx, qedi_debugfs_ops,
2519 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2520 "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
2521 QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
2522 FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
2524 if (mode == QEDI_MODE_NORMAL) {
2525 if (iscsi_host_add(qedi->shost, &pdev->dev)) {
2526 QEDI_ERR(&qedi->dbg_ctx,
2527 "Could not add iscsi host\n");
2532 /* Allocate uio buffers */
2533 rc = qedi_alloc_uio_rings(qedi);
2535 QEDI_ERR(&qedi->dbg_ctx,
2536 "UIO alloc ring failed err=%d\n", rc);
2540 rc = qedi_init_uio(qedi);
2542 QEDI_ERR(&qedi->dbg_ctx,
2543 "UIO init failed, err=%d\n", rc);
2547 /* host the array on iscsi_conn */
2548 rc = qedi_setup_cid_que(qedi);
2550 QEDI_ERR(&qedi->dbg_ctx,
2551 "Could not setup cid que\n");
2555 rc = qedi_cm_alloc_mem(qedi);
2557 QEDI_ERR(&qedi->dbg_ctx,
2558 "Could not alloc cm memory\n");
2562 rc = qedi_alloc_itt(qedi);
2564 QEDI_ERR(&qedi->dbg_ctx,
2565 "Could not alloc itt memory\n");
2569 sprintf(host_buf, "host_%d", qedi->shost->host_no);
2570 qedi->tmf_thread = create_singlethread_workqueue(host_buf);
2571 if (!qedi->tmf_thread) {
2572 QEDI_ERR(&qedi->dbg_ctx,
2573 "Unable to start tmf thread!\n");
2578 sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
2579 qedi->offload_thread = create_workqueue(host_buf);
2580 if (!qedi->offload_thread) {
2581 QEDI_ERR(&qedi->dbg_ctx,
2582 "Unable to start offload thread!\n");
2584 goto free_tmf_thread;
2587 /* F/w needs 1st task context memory entry for performance */
2588 set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
2589 atomic_set(&qedi->num_offloads, 0);
2591 if (qedi_setup_boot_info(qedi))
2592 QEDI_ERR(&qedi->dbg_ctx,
2593 "No iSCSI boot target configured\n");
2595 rc = qedi_ops->common->update_drv_state(qedi->cdev, true);
2597 QEDI_ERR(&qedi->dbg_ctx,
2598 "Failed to send drv state to MFW\n");
2605 destroy_workqueue(qedi->tmf_thread);
2607 qedi_release_cid_que(qedi);
2609 qedi_free_uio(qedi->udev);
2611 #ifdef CONFIG_DEBUG_FS
2612 qedi_dbg_host_exit(&qedi->dbg_ctx);
2614 iscsi_host_remove(qedi->shost);
2616 qedi_ops->stop(qedi->cdev);
2618 qedi_ops->common->slowpath_stop(qedi->cdev);
2620 qedi_ops->common->remove(qedi->cdev);
2622 qedi_free_iscsi_pf_param(qedi);
2624 iscsi_host_free(qedi->shost);
2629 static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2631 return __qedi_probe(pdev, QEDI_MODE_NORMAL);
2634 static void qedi_remove(struct pci_dev *pdev)
2636 __qedi_remove(pdev, QEDI_MODE_NORMAL);
2639 static struct pci_device_id qedi_pci_tbl[] = {
2640 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
2641 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
2644 MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
2646 static enum cpuhp_state qedi_cpuhp_state;
2648 static struct pci_driver qedi_pci_driver = {
2649 .name = QEDI_MODULE_NAME,
2650 .id_table = qedi_pci_tbl,
2651 .probe = qedi_probe,
2652 .remove = qedi_remove,
2655 static int __init qedi_init(void)
2657 struct qedi_percpu_s *p;
2660 qedi_ops = qed_get_iscsi_ops();
2662 QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
2666 #ifdef CONFIG_DEBUG_FS
2667 qedi_dbg_init("qedi");
2670 qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
2671 if (!qedi_scsi_transport) {
2672 QEDI_ERR(NULL, "Could not register qedi transport");
2674 goto exit_qedi_init_1;
2677 for_each_possible_cpu(cpu) {
2678 p = &per_cpu(qedi_percpu, cpu);
2679 INIT_LIST_HEAD(&p->work_list);
2680 spin_lock_init(&p->p_work_lock);
2684 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
2685 qedi_cpu_online, qedi_cpu_offline);
2687 goto exit_qedi_init_2;
2688 qedi_cpuhp_state = rc;
2690 rc = pci_register_driver(&qedi_pci_driver);
2692 QEDI_ERR(NULL, "Failed to register driver\n");
2699 cpuhp_remove_state(qedi_cpuhp_state);
2701 iscsi_unregister_transport(&qedi_iscsi_transport);
2703 #ifdef CONFIG_DEBUG_FS
2706 qed_put_iscsi_ops();
2710 static void __exit qedi_cleanup(void)
2712 pci_unregister_driver(&qedi_pci_driver);
2713 cpuhp_remove_state(qedi_cpuhp_state);
2714 iscsi_unregister_transport(&qedi_iscsi_transport);
2716 #ifdef CONFIG_DEBUG_FS
2719 qed_put_iscsi_ops();
2722 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
2723 MODULE_LICENSE("GPL");
2724 MODULE_AUTHOR("QLogic Corporation");
2725 MODULE_VERSION(QEDI_MODULE_VERSION);
2726 module_init(qedi_init);
2727 module_exit(qedi_cleanup);