1 // SPDX-License-Identifier: GPL-2.0-only
3 * QLogic iSCSI Offload Driver
4 * Copyright (c) 2016 Cavium Inc.
7 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/if_arp.h>
11 #include <scsi/iscsi_if.h>
12 #include <linux/inet.h>
14 #include <linux/list.h>
15 #include <linux/kthread.h>
17 #include <linux/if_vlan.h>
18 #include <linux/cpu.h>
19 #include <linux/iscsi_boot_sysfs.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_eh.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi.h>
29 #include "qedi_iscsi.h"
31 static uint qedi_qed_debug;
32 module_param(qedi_qed_debug, uint, 0644);
33 MODULE_PARM_DESC(qedi_qed_debug, " QED debug level 0 (default)");
35 static uint qedi_fw_debug;
36 module_param(qedi_fw_debug, uint, 0644);
37 MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
39 uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
40 module_param(qedi_dbg_log, uint, 0644);
41 MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
44 module_param(qedi_io_tracing, uint, 0644);
45 MODULE_PARM_DESC(qedi_io_tracing,
46 " Enable logging of SCSI requests/completions into trace buffer. (default off).");
48 static uint qedi_ll2_buf_size = 0x400;
49 module_param(qedi_ll2_buf_size, uint, 0644);
50 MODULE_PARM_DESC(qedi_ll2_buf_size,
51 "parameter to set ping packet size, default - 0x400, Jumbo packets - 0x2400.");
53 const struct qed_iscsi_ops *qedi_ops;
54 static struct scsi_transport_template *qedi_scsi_transport;
55 static struct pci_driver qedi_pci_driver;
56 static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
57 static LIST_HEAD(qedi_udev_list);
58 /* Static function declaration */
59 static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
60 static void qedi_free_global_queues(struct qedi_ctx *qedi);
61 static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
62 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
63 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
64 static struct nvm_iscsi_block *qedi_get_nvram_block(struct qedi_ctx *qedi);
65 static void qedi_recovery_handler(struct work_struct *work);
67 static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
69 struct qedi_ctx *qedi;
70 struct qedi_endpoint *qedi_ep;
71 struct iscsi_eqe_data *data;
74 if (!context || !fw_handle) {
75 QEDI_ERR(NULL, "Recv event with ctx NULL\n");
79 qedi = (struct qedi_ctx *)context;
80 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
81 "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
83 data = (struct iscsi_eqe_data *)fw_handle;
84 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
85 "icid=0x%x conn_id=0x%x err-code=0x%x error-pdu-opcode-reserved=0x%x\n",
86 data->icid, data->conn_id, data->error_code,
87 data->error_pdu_opcode_reserved);
89 qedi_ep = qedi->ep_tbl[data->icid];
92 QEDI_WARN(&qedi->dbg_ctx,
93 "Cannot process event, ep already disconnected, cid=0x%x\n",
99 switch (fw_event_code) {
100 case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
101 if (qedi_ep->state == EP_STATE_OFLDCONN_START)
102 qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
104 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
106 case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
107 qedi_ep->state = EP_STATE_DISCONN_COMPL;
108 wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
110 case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
111 qedi_process_iscsi_error(qedi_ep, data);
113 case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
114 case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
115 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
116 case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
117 case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
118 case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
119 case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
120 qedi_process_tcp_error(qedi_ep, data);
123 QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
130 static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
132 struct qedi_uio_dev *udev = uinfo->priv;
133 struct qedi_ctx *qedi = udev->qedi;
135 if (!capable(CAP_NET_ADMIN))
138 if (udev->uio_dev != -1)
142 udev->uio_dev = iminor(inode);
143 qedi_reset_uio_rings(udev);
144 set_bit(UIO_DEV_OPENED, &qedi->flags);
150 static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
152 struct qedi_uio_dev *udev = uinfo->priv;
153 struct qedi_ctx *qedi = udev->qedi;
156 clear_bit(UIO_DEV_OPENED, &qedi->flags);
157 qedi_ll2_free_skbs(qedi);
161 static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
164 free_page((unsigned long)udev->uctrl);
168 if (udev->ll2_ring) {
169 free_page((unsigned long)udev->ll2_ring);
170 udev->ll2_ring = NULL;
174 free_pages((unsigned long)udev->ll2_buf, 2);
175 udev->ll2_buf = NULL;
179 static void __qedi_free_uio(struct qedi_uio_dev *udev)
181 uio_unregister_device(&udev->qedi_uinfo);
183 __qedi_free_uio_rings(udev);
185 pci_dev_put(udev->pdev);
189 static void qedi_free_uio(struct qedi_uio_dev *udev)
194 list_del_init(&udev->list);
195 __qedi_free_uio(udev);
198 static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
200 struct qedi_ctx *qedi = NULL;
201 struct qedi_uio_ctrl *uctrl = NULL;
206 spin_lock_bh(&qedi->ll2_lock);
207 uctrl->host_rx_cons = 0;
208 uctrl->hw_rx_prod = 0;
209 uctrl->hw_rx_bd_prod = 0;
210 uctrl->host_rx_bd_cons = 0;
212 memset(udev->ll2_ring, 0, udev->ll2_ring_size);
213 memset(udev->ll2_buf, 0, udev->ll2_buf_size);
214 spin_unlock_bh(&qedi->ll2_lock);
217 static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
221 if (udev->ll2_ring || udev->ll2_buf)
224 /* Memory for control area. */
225 udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL);
229 /* Allocating memory for LL2 ring */
230 udev->ll2_ring_size = QEDI_PAGE_SIZE;
231 udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
232 if (!udev->ll2_ring) {
234 goto exit_alloc_ring;
237 /* Allocating memory for Tx/Rx pkt buffer */
238 udev->ll2_buf_size = TX_RX_RING * qedi_ll2_buf_size;
239 udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
240 udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
242 if (!udev->ll2_buf) {
249 free_page((unsigned long)udev->ll2_ring);
250 udev->ll2_ring = NULL;
255 static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
257 struct qedi_uio_dev *udev = NULL;
260 list_for_each_entry(udev, &qedi_udev_list, list) {
261 if (udev->pdev == qedi->pdev) {
263 if (__qedi_alloc_uio_rings(udev)) {
272 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
281 udev->pdev = qedi->pdev;
283 rc = __qedi_alloc_uio_rings(udev);
287 list_add(&udev->list, &qedi_udev_list);
289 pci_dev_get(udev->pdev);
292 udev->tx_pkt = udev->ll2_buf;
293 udev->rx_pkt = udev->ll2_buf + qedi_ll2_buf_size;
302 static int qedi_init_uio(struct qedi_ctx *qedi)
304 struct qedi_uio_dev *udev = qedi->udev;
305 struct uio_info *uinfo;
311 uinfo = &udev->qedi_uinfo;
313 uinfo->mem[0].addr = (unsigned long)udev->uctrl;
314 uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
315 uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
317 uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
318 uinfo->mem[1].size = udev->ll2_ring_size;
319 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
321 uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
322 uinfo->mem[2].size = udev->ll2_buf_size;
323 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
325 uinfo->name = "qedi_uio";
326 uinfo->version = QEDI_MODULE_VERSION;
327 uinfo->irq = UIO_IRQ_CUSTOM;
329 uinfo->open = qedi_uio_open;
330 uinfo->release = qedi_uio_close;
332 if (udev->uio_dev == -1) {
336 ret = uio_register_device(&udev->pdev->dev, uinfo);
338 QEDI_ERR(&qedi->dbg_ctx,
339 "UIO registration failed\n");
347 static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
348 struct qed_sb_info *sb_info, u16 sb_id)
350 struct status_block_e4 *sb_virt;
354 sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
355 sizeof(struct status_block_e4), &sb_phys,
358 QEDI_ERR(&qedi->dbg_ctx,
359 "Status block allocation failed for id = %d.\n",
364 ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
365 sb_id, QED_SB_TYPE_STORAGE);
367 QEDI_ERR(&qedi->dbg_ctx,
368 "Status block initialization failed for id = %d.\n",
376 static void qedi_free_sb(struct qedi_ctx *qedi)
378 struct qed_sb_info *sb_info;
381 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
382 sb_info = &qedi->sb_array[id];
383 if (sb_info->sb_virt)
384 dma_free_coherent(&qedi->pdev->dev,
385 sizeof(*sb_info->sb_virt),
386 (void *)sb_info->sb_virt,
391 static void qedi_free_fp(struct qedi_ctx *qedi)
393 kfree(qedi->fp_array);
394 kfree(qedi->sb_array);
397 static void qedi_destroy_fp(struct qedi_ctx *qedi)
403 static int qedi_alloc_fp(struct qedi_ctx *qedi)
407 qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
408 sizeof(struct qedi_fastpath), GFP_KERNEL);
409 if (!qedi->fp_array) {
410 QEDI_ERR(&qedi->dbg_ctx,
411 "fastpath fp array allocation failed.\n");
415 qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
416 sizeof(struct qed_sb_info), GFP_KERNEL);
417 if (!qedi->sb_array) {
418 QEDI_ERR(&qedi->dbg_ctx,
419 "fastpath sb array allocation failed.\n");
431 static void qedi_int_fp(struct qedi_ctx *qedi)
433 struct qedi_fastpath *fp;
436 memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
437 sizeof(*qedi->fp_array));
438 memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
439 sizeof(*qedi->sb_array));
441 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
442 fp = &qedi->fp_array[id];
443 fp->sb_info = &qedi->sb_array[id];
446 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
449 /* fp_array[i] ---- irq cookie
450 * So init data which is needed in int ctx
455 static int qedi_prepare_fp(struct qedi_ctx *qedi)
457 struct qedi_fastpath *fp;
460 ret = qedi_alloc_fp(qedi);
466 for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
467 fp = &qedi->fp_array[id];
468 ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
470 QEDI_ERR(&qedi->dbg_ctx,
471 "SB allocation and initialization failed.\n");
486 static int qedi_setup_cid_que(struct qedi_ctx *qedi)
490 qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
491 sizeof(u32), GFP_KERNEL);
492 if (!qedi->cid_que.cid_que_base)
495 qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
496 sizeof(struct qedi_conn *),
498 if (!qedi->cid_que.conn_cid_tbl) {
499 kfree(qedi->cid_que.cid_que_base);
500 qedi->cid_que.cid_que_base = NULL;
504 qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
505 qedi->cid_que.cid_q_prod_idx = 0;
506 qedi->cid_que.cid_q_cons_idx = 0;
507 qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
508 qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
510 for (i = 0; i < qedi->max_active_conns; i++) {
511 qedi->cid_que.cid_que[i] = i;
512 qedi->cid_que.conn_cid_tbl[i] = NULL;
518 static void qedi_release_cid_que(struct qedi_ctx *qedi)
520 kfree(qedi->cid_que.cid_que_base);
521 qedi->cid_que.cid_que_base = NULL;
523 kfree(qedi->cid_que.conn_cid_tbl);
524 qedi->cid_que.conn_cid_tbl = NULL;
527 static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
528 u16 start_id, u16 next)
530 id_tbl->start = start_id;
533 spin_lock_init(&id_tbl->lock);
534 id_tbl->table = kcalloc(BITS_TO_LONGS(size), sizeof(long), GFP_KERNEL);
541 static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
543 kfree(id_tbl->table);
544 id_tbl->table = NULL;
547 int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
552 if (id >= id_tbl->max)
555 spin_lock(&id_tbl->lock);
556 if (!test_bit(id, id_tbl->table)) {
557 set_bit(id, id_tbl->table);
560 spin_unlock(&id_tbl->lock);
564 u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
568 spin_lock(&id_tbl->lock);
569 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
570 if (id >= id_tbl->max) {
571 id = QEDI_LOCAL_PORT_INVALID;
572 if (id_tbl->next != 0) {
573 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
574 if (id >= id_tbl->next)
575 id = QEDI_LOCAL_PORT_INVALID;
579 if (id < id_tbl->max) {
580 set_bit(id, id_tbl->table);
581 id_tbl->next = (id + 1) & (id_tbl->max - 1);
585 spin_unlock(&id_tbl->lock);
590 void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
592 if (id == QEDI_LOCAL_PORT_INVALID)
596 if (id >= id_tbl->max)
599 clear_bit(id, id_tbl->table);
602 static void qedi_cm_free_mem(struct qedi_ctx *qedi)
606 qedi_free_id_tbl(&qedi->lcl_port_tbl);
609 static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
613 qedi->ep_tbl = kzalloc((qedi->max_active_conns *
614 sizeof(struct qedi_endpoint *)), GFP_KERNEL);
617 port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
618 if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
619 QEDI_LOCAL_PORT_MIN, port_id)) {
620 qedi_cm_free_mem(qedi);
627 static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
629 struct Scsi_Host *shost;
630 struct qedi_ctx *qedi = NULL;
632 shost = iscsi_host_alloc(&qedi_host_template,
633 sizeof(struct qedi_ctx), 0);
635 QEDI_ERR(NULL, "Could not allocate shost\n");
636 goto exit_setup_shost;
639 shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA;
640 shost->max_channel = 0;
642 shost->max_cmd_len = 16;
643 shost->transportt = qedi_scsi_transport;
645 qedi = iscsi_host_priv(shost);
646 memset(qedi, 0, sizeof(*qedi));
648 qedi->dbg_ctx.host_no = shost->host_no;
650 qedi->dbg_ctx.pdev = pdev;
651 qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
652 qedi->max_sqes = QEDI_SQ_SIZE;
654 shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
656 pci_set_drvdata(pdev, qedi);
662 static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
664 struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
665 struct skb_work_list *work;
669 QEDI_ERR(NULL, "qedi is NULL\n");
673 if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
674 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
675 "UIO DEV is not opened\n");
680 eh = (struct ethhdr *)skb->data;
681 /* Undo VLAN encapsulation */
682 if (eh->h_proto == htons(ETH_P_8021Q)) {
683 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
684 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
685 skb_reset_mac_header(skb);
688 /* Filter out non FIP/FCoE frames here to free them faster */
689 if (eh->h_proto != htons(ETH_P_ARP) &&
690 eh->h_proto != htons(ETH_P_IP) &&
691 eh->h_proto != htons(ETH_P_IPV6)) {
692 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
693 "Dropping frame ethertype [0x%x] len [0x%x].\n",
694 eh->h_proto, skb->len);
699 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
700 "Allowed frame ethertype [0x%x] len [0x%x].\n",
701 eh->h_proto, skb->len);
703 work = kzalloc(sizeof(*work), GFP_ATOMIC);
705 QEDI_WARN(&qedi->dbg_ctx,
706 "Could not allocate work so dropping frame.\n");
711 INIT_LIST_HEAD(&work->list);
714 if (skb_vlan_tag_present(skb))
715 work->vlan_id = skb_vlan_tag_get(skb);
718 __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
720 spin_lock_bh(&qedi->ll2_lock);
721 list_add_tail(&work->list, &qedi->ll2_skb_list);
722 spin_unlock_bh(&qedi->ll2_lock);
724 wake_up_process(qedi->ll2_recv_thread);
729 /* map this skb to iscsiuio mmaped region */
730 static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
733 struct qedi_uio_dev *udev = NULL;
734 struct qedi_uio_ctrl *uctrl = NULL;
735 struct qedi_rx_bd rxbd;
736 struct qedi_rx_bd *p_rxbd;
743 QEDI_ERR(NULL, "qedi is NULL\n");
750 ++uctrl->hw_rx_prod_cnt;
751 prod = (uctrl->hw_rx_prod + 1) % RX_RING;
753 pkt = udev->rx_pkt + (prod * qedi_ll2_buf_size);
754 len = min_t(u32, skb->len, (u32)qedi_ll2_buf_size);
755 memcpy(pkt, skb->data, len);
757 memset(&rxbd, 0, sizeof(rxbd));
758 rxbd.rx_pkt_index = prod;
759 rxbd.rx_pkt_len = len;
760 rxbd.vlan_id = vlan_id;
762 uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
763 rx_bd_prod = uctrl->hw_rx_bd_prod;
764 p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
765 p_rxbd += rx_bd_prod;
767 memcpy(p_rxbd, &rxbd, sizeof(rxbd));
769 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
770 "hw_rx_prod [%d] prod [%d] hw_rx_bd_prod [%d] rx_pkt_idx [%d] rx_len [%d].\n",
771 uctrl->hw_rx_prod, prod, uctrl->hw_rx_bd_prod,
772 rxbd.rx_pkt_index, rxbd.rx_pkt_len);
773 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
774 "host_rx_cons [%d] hw_rx_bd_cons [%d].\n",
775 uctrl->host_rx_cons, uctrl->host_rx_bd_cons);
777 uctrl->hw_rx_prod = prod;
779 /* notify the iscsiuio about new packet */
780 uio_event_notify(&udev->qedi_uinfo);
785 static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
787 struct skb_work_list *work, *work_tmp;
789 spin_lock_bh(&qedi->ll2_lock);
790 list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
791 list_del(&work->list);
793 kfree_skb(work->skb);
796 spin_unlock_bh(&qedi->ll2_lock);
799 static int qedi_ll2_recv_thread(void *arg)
801 struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
802 struct skb_work_list *work, *work_tmp;
804 set_user_nice(current, -20);
806 while (!kthread_should_stop()) {
807 spin_lock_bh(&qedi->ll2_lock);
808 list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
810 list_del(&work->list);
811 qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
812 kfree_skb(work->skb);
815 set_current_state(TASK_INTERRUPTIBLE);
816 spin_unlock_bh(&qedi->ll2_lock);
820 __set_current_state(TASK_RUNNING);
824 static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
831 num_sq_pages = (MAX_OUTSTANDING_TASKS_PER_CON * 8) / QEDI_PAGE_SIZE;
833 qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
835 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
836 "Number of CQ count is %d\n", qedi->num_queues);
838 memset(&qedi->pf_params.iscsi_pf_params, 0,
839 sizeof(qedi->pf_params.iscsi_pf_params));
841 qedi->p_cpuq = dma_alloc_coherent(&qedi->pdev->dev,
842 qedi->num_queues * sizeof(struct qedi_glbl_q_params),
843 &qedi->hw_p_cpuq, GFP_KERNEL);
845 QEDI_ERR(&qedi->dbg_ctx, "dma_alloc_coherent fail\n");
850 rval = qedi_alloc_global_queues(qedi);
852 QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
857 qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
858 qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
859 qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
860 qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
861 qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
862 qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
863 qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
864 qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
865 qedi->pf_params.iscsi_pf_params.two_msl_timer = 4000;
866 qedi->pf_params.iscsi_pf_params.max_fin_rt = 2;
868 for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
869 if ((1 << log_page_size) == QEDI_PAGE_SIZE)
872 qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
874 qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
875 (u64)qedi->hw_p_cpuq;
877 /* RQ BDQ initializations.
878 * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
879 * rqe_log_size: 8 for 256B RQE
881 qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
882 /* BDQ address and size */
883 qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
884 qedi->bdq_pbl_list_dma;
885 qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
886 qedi->bdq_pbl_list_num_entries;
887 qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
889 /* cq_num_entries: num_tasks + rq_num_entries */
890 qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
892 qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
893 qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
899 /* Free DMA coherent memory for array of queue pointers we pass to qed */
900 static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
905 size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
906 dma_free_coherent(&qedi->pdev->dev, size, qedi->p_cpuq,
910 qedi_free_global_queues(qedi);
912 kfree(qedi->global_queues);
915 static void qedi_get_boot_tgt_info(struct nvm_iscsi_block *block,
916 struct qedi_boot_target *tgt, u8 index)
920 ipv6_en = !!(block->generic.ctrl_flags &
921 NVM_ISCSI_CFG_GEN_IPV6_ENABLED);
923 snprintf(tgt->iscsi_name, sizeof(tgt->iscsi_name), "%s",
924 block->target[index].target_name.byte);
926 tgt->ipv6_en = ipv6_en;
929 snprintf(tgt->ip_addr, IPV6_LEN, "%pI6\n",
930 block->target[index].ipv6_addr.byte);
932 snprintf(tgt->ip_addr, IPV4_LEN, "%pI4\n",
933 block->target[index].ipv4_addr.byte);
936 static int qedi_find_boot_info(struct qedi_ctx *qedi,
937 struct qed_mfw_tlv_iscsi *iscsi,
938 struct nvm_iscsi_block *block)
940 struct qedi_boot_target *pri_tgt = NULL, *sec_tgt = NULL;
941 u32 pri_ctrl_flags = 0, sec_ctrl_flags = 0, found = 0;
942 struct iscsi_cls_session *cls_sess;
943 struct iscsi_cls_conn *cls_conn;
944 struct qedi_conn *qedi_conn;
945 struct iscsi_session *sess;
946 struct iscsi_conn *conn;
950 pri_ctrl_flags = !!(block->target[0].ctrl_flags &
951 NVM_ISCSI_CFG_TARGET_ENABLED);
952 if (pri_ctrl_flags) {
953 pri_tgt = kzalloc(sizeof(*pri_tgt), GFP_KERNEL);
956 qedi_get_boot_tgt_info(block, pri_tgt, 0);
959 sec_ctrl_flags = !!(block->target[1].ctrl_flags &
960 NVM_ISCSI_CFG_TARGET_ENABLED);
961 if (sec_ctrl_flags) {
962 sec_tgt = kzalloc(sizeof(*sec_tgt), GFP_KERNEL);
967 qedi_get_boot_tgt_info(block, sec_tgt, 1);
970 for (i = 0; i < qedi->max_active_conns; i++) {
971 qedi_conn = qedi_get_conn_from_id(qedi, i);
975 if (qedi_conn->ep->ip_type == TCP_IPV4)
976 snprintf(ep_ip_addr, IPV4_LEN, "%pI4\n",
977 qedi_conn->ep->dst_addr);
979 snprintf(ep_ip_addr, IPV6_LEN, "%pI6\n",
980 qedi_conn->ep->dst_addr);
982 cls_conn = qedi_conn->cls_conn;
983 conn = cls_conn->dd_data;
984 cls_sess = iscsi_conn_to_session(cls_conn);
985 sess = cls_sess->dd_data;
987 if (!iscsi_is_session_online(cls_sess))
990 if (!sess->targetname)
993 if (pri_ctrl_flags) {
994 if (!strcmp(pri_tgt->iscsi_name, sess->targetname) &&
995 !strcmp(pri_tgt->ip_addr, ep_ip_addr)) {
1001 if (sec_ctrl_flags) {
1002 if (!strcmp(sec_tgt->iscsi_name, sess->targetname) &&
1003 !strcmp(sec_tgt->ip_addr, ep_ip_addr)) {
1011 if (conn->hdrdgst_en) {
1012 iscsi->header_digest_set = true;
1013 iscsi->header_digest = 1;
1016 if (conn->datadgst_en) {
1017 iscsi->data_digest_set = true;
1018 iscsi->data_digest = 1;
1020 iscsi->boot_taget_portal_set = true;
1021 iscsi->boot_taget_portal = sess->tpgt;
1036 static void qedi_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
1038 struct qedi_ctx *qedi;
1041 QEDI_INFO(NULL, QEDI_LOG_EVT,
1042 "dev is NULL so ignoring get_generic_tlv_data request.\n");
1045 qedi = (struct qedi_ctx *)dev;
1047 memset(data, 0, sizeof(struct qed_generic_tlvs));
1048 ether_addr_copy(data->mac[0], qedi->mac);
1052 * Protocol TLV handler
1054 static void qedi_get_protocol_tlv_data(void *dev, void *data)
1056 struct qed_mfw_tlv_iscsi *iscsi = data;
1057 struct qed_iscsi_stats *fw_iscsi_stats;
1058 struct nvm_iscsi_block *block = NULL;
1059 u32 chap_en = 0, mchap_en = 0;
1060 struct qedi_ctx *qedi = dev;
1063 fw_iscsi_stats = kmalloc(sizeof(*fw_iscsi_stats), GFP_KERNEL);
1064 if (!fw_iscsi_stats) {
1065 QEDI_ERR(&qedi->dbg_ctx,
1066 "Could not allocate memory for fw_iscsi_stats.\n");
1070 mutex_lock(&qedi->stats_lock);
1071 /* Query firmware for offload stats */
1072 qedi_ops->get_stats(qedi->cdev, fw_iscsi_stats);
1073 mutex_unlock(&qedi->stats_lock);
1075 iscsi->rx_frames_set = true;
1076 iscsi->rx_frames = fw_iscsi_stats->iscsi_rx_packet_cnt;
1077 iscsi->rx_bytes_set = true;
1078 iscsi->rx_bytes = fw_iscsi_stats->iscsi_rx_bytes_cnt;
1079 iscsi->tx_frames_set = true;
1080 iscsi->tx_frames = fw_iscsi_stats->iscsi_tx_packet_cnt;
1081 iscsi->tx_bytes_set = true;
1082 iscsi->tx_bytes = fw_iscsi_stats->iscsi_tx_bytes_cnt;
1083 iscsi->frame_size_set = true;
1084 iscsi->frame_size = qedi->ll2_mtu;
1085 block = qedi_get_nvram_block(qedi);
1087 chap_en = !!(block->generic.ctrl_flags &
1088 NVM_ISCSI_CFG_GEN_CHAP_ENABLED);
1089 mchap_en = !!(block->generic.ctrl_flags &
1090 NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED);
1092 iscsi->auth_method_set = (chap_en || mchap_en) ? true : false;
1093 iscsi->auth_method = 1;
1095 iscsi->auth_method = 2;
1097 iscsi->auth_method = 3;
1099 iscsi->tx_desc_size_set = true;
1100 iscsi->tx_desc_size = QEDI_SQ_SIZE;
1101 iscsi->rx_desc_size_set = true;
1102 iscsi->rx_desc_size = QEDI_CQ_SIZE;
1104 /* tpgt, hdr digest, data digest */
1105 rval = qedi_find_boot_info(qedi, iscsi, block);
1107 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1108 "Boot target not set");
1111 kfree(fw_iscsi_stats);
1116 static void qedi_schedule_recovery_handler(void *dev)
1118 struct qedi_ctx *qedi = dev;
1120 QEDI_ERR(&qedi->dbg_ctx, "Recovery handler scheduled.\n");
1122 if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags))
1125 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1127 schedule_delayed_work(&qedi->recovery_work, 0);
1130 static void qedi_link_update(void *dev, struct qed_link_output *link)
1132 struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
1134 if (link->link_up) {
1135 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
1136 atomic_set(&qedi->link_state, QEDI_LINK_UP);
1138 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1139 "Link Down event.\n");
1140 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1144 static struct qed_iscsi_cb_ops qedi_cb_ops = {
1146 .link_update = qedi_link_update,
1147 .schedule_recovery_handler = qedi_schedule_recovery_handler,
1148 .get_protocol_tlv_data = qedi_get_protocol_tlv_data,
1149 .get_generic_tlv_data = qedi_get_generic_tlv_data,
1153 static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
1154 u16 que_idx, struct qedi_percpu_s *p)
1156 struct qedi_work *qedi_work;
1157 struct qedi_conn *q_conn;
1158 struct qedi_cmd *qedi_cmd;
1162 iscsi_cid = cqe->cqe_common.conn_id;
1163 q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
1165 QEDI_WARN(&qedi->dbg_ctx,
1166 "Session no longer exists for cid=0x%x!!\n",
1171 switch (cqe->cqe_common.cqe_type) {
1172 case ISCSI_CQE_TYPE_SOLICITED:
1173 case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
1174 qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
1179 INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
1180 qedi_cmd->cqe_work.qedi = qedi;
1181 memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
1182 qedi_cmd->cqe_work.que_idx = que_idx;
1183 qedi_cmd->cqe_work.is_solicited = true;
1184 list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
1186 case ISCSI_CQE_TYPE_UNSOLICITED:
1187 case ISCSI_CQE_TYPE_DUMMY:
1188 case ISCSI_CQE_TYPE_TASK_CLEANUP:
1189 qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
1194 INIT_LIST_HEAD(&qedi_work->list);
1195 qedi_work->qedi = qedi;
1196 memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
1197 qedi_work->que_idx = que_idx;
1198 qedi_work->is_solicited = false;
1199 list_add_tail(&qedi_work->list, &p->work_list);
1203 QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
1208 static bool qedi_process_completions(struct qedi_fastpath *fp)
1210 struct qedi_ctx *qedi = fp->qedi;
1211 struct qed_sb_info *sb_info = fp->sb_info;
1212 struct status_block_e4 *sb = sb_info->sb_virt;
1213 struct qedi_percpu_s *p = NULL;
1214 struct global_queue *que;
1216 unsigned long flags;
1217 union iscsi_cqe *cqe;
1221 /* Get the current firmware producer index */
1222 prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1224 if (prod_idx >= QEDI_CQ_SIZE)
1225 prod_idx = prod_idx % QEDI_CQ_SIZE;
1227 que = qedi->global_queues[fp->sb_id];
1228 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1229 "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
1230 que, prod_idx, que->cq_cons_idx, fp->sb_id);
1232 qedi->intr_cpu = fp->sb_id;
1233 cpu = smp_processor_id();
1234 p = &per_cpu(qedi_percpu, cpu);
1236 if (unlikely(!p->iothread))
1239 spin_lock_irqsave(&p->p_work_lock, flags);
1240 while (que->cq_cons_idx != prod_idx) {
1241 cqe = &que->cq[que->cq_cons_idx];
1243 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1244 "cqe=%p prod_idx=%d cons_idx=%d.\n",
1245 cqe, prod_idx, que->cq_cons_idx);
1247 ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
1249 QEDI_WARN(&qedi->dbg_ctx,
1250 "Dropping CQE 0x%x for cid=0x%x.\n",
1251 que->cq_cons_idx, cqe->cqe_common.conn_id);
1254 if (que->cq_cons_idx == QEDI_CQ_SIZE)
1255 que->cq_cons_idx = 0;
1257 wake_up_process(p->iothread);
1258 spin_unlock_irqrestore(&p->p_work_lock, flags);
1263 static bool qedi_fp_has_work(struct qedi_fastpath *fp)
1265 struct qedi_ctx *qedi = fp->qedi;
1266 struct global_queue *que;
1267 struct qed_sb_info *sb_info = fp->sb_info;
1268 struct status_block_e4 *sb = sb_info->sb_virt;
1273 /* Get the current firmware producer index */
1274 prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1276 /* Get the pointer to the global CQ this completion is on */
1277 que = qedi->global_queues[fp->sb_id];
1279 /* prod idx wrap around uint16 */
1280 if (prod_idx >= QEDI_CQ_SIZE)
1281 prod_idx = prod_idx % QEDI_CQ_SIZE;
1283 return (que->cq_cons_idx != prod_idx);
1286 /* MSI-X fastpath handler code */
1287 static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
1289 struct qedi_fastpath *fp = dev_id;
1290 struct qedi_ctx *qedi = fp->qedi;
1291 bool wake_io_thread = true;
1293 qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
1296 wake_io_thread = qedi_process_completions(fp);
1297 if (wake_io_thread) {
1298 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1299 "process already running\n");
1302 if (!qedi_fp_has_work(fp))
1303 qed_sb_update_sb_idx(fp->sb_info);
1305 /* Check for more work */
1308 if (!qedi_fp_has_work(fp))
1309 qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1316 /* simd handler for MSI/INTa */
1317 static void qedi_simd_int_handler(void *cookie)
1319 /* Cookie is qedi_ctx struct */
1320 struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
1322 QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
1325 #define QEDI_SIMD_HANDLER_NUM 0
1326 static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
1331 if (qedi->int_info.msix_cnt) {
1332 for (i = 0; i < qedi->int_info.used_cnt; i++) {
1333 idx = i * qedi->dev_info.common.num_hwfns +
1334 qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1336 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1337 "Freeing IRQ #%d vector_idx=%d.\n", i, idx);
1339 synchronize_irq(qedi->int_info.msix[idx].vector);
1340 irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1342 free_irq(qedi->int_info.msix[idx].vector,
1343 &qedi->fp_array[i]);
1346 qedi_ops->common->simd_handler_clean(qedi->cdev,
1347 QEDI_SIMD_HANDLER_NUM);
1350 qedi->int_info.used_cnt = 0;
1351 qedi_ops->common->set_fp_int(qedi->cdev, 0);
1354 static int qedi_request_msix_irq(struct qedi_ctx *qedi)
1359 cpu = cpumask_first(cpu_online_mask);
1360 for (i = 0; i < qedi->int_info.msix_cnt; i++) {
1361 idx = i * qedi->dev_info.common.num_hwfns +
1362 qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1364 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1365 "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
1366 qedi->dev_info.common.num_hwfns,
1367 qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
1369 rc = request_irq(qedi->int_info.msix[idx].vector,
1370 qedi_msix_handler, 0, "qedi",
1371 &qedi->fp_array[i]);
1373 QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
1374 qedi_sync_free_irqs(qedi);
1377 qedi->int_info.used_cnt++;
1378 rc = irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1380 cpu = cpumask_next(cpu, cpu_online_mask);
1386 static int qedi_setup_int(struct qedi_ctx *qedi)
1390 rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus());
1391 rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
1393 goto exit_setup_int;
1395 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1396 "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
1397 qedi->int_info.msix_cnt, num_online_cpus());
1399 if (qedi->int_info.msix_cnt) {
1400 rc = qedi_request_msix_irq(qedi);
1401 goto exit_setup_int;
1403 qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
1404 QEDI_SIMD_HANDLER_NUM,
1405 qedi_simd_int_handler);
1406 qedi->int_info.used_cnt = 1;
1413 static void qedi_free_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1415 if (qedi->iscsi_image)
1416 dma_free_coherent(&qedi->pdev->dev,
1417 sizeof(struct qedi_nvm_iscsi_image),
1418 qedi->iscsi_image, qedi->nvm_buf_dma);
1421 static int qedi_alloc_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1423 qedi->iscsi_image = dma_alloc_coherent(&qedi->pdev->dev,
1424 sizeof(struct qedi_nvm_iscsi_image),
1425 &qedi->nvm_buf_dma, GFP_KERNEL);
1426 if (!qedi->iscsi_image) {
1427 QEDI_ERR(&qedi->dbg_ctx, "Could not allocate NVM BUF.\n");
1430 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1431 "NVM BUF addr=0x%p dma=0x%llx.\n", qedi->iscsi_image,
1437 static void qedi_free_bdq(struct qedi_ctx *qedi)
1441 if (qedi->bdq_pbl_list)
1442 dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
1443 qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
1446 dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
1447 qedi->bdq_pbl, qedi->bdq_pbl_dma);
1449 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1450 if (qedi->bdq[i].buf_addr) {
1451 dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
1452 qedi->bdq[i].buf_addr,
1453 qedi->bdq[i].buf_dma);
1458 static void qedi_free_global_queues(struct qedi_ctx *qedi)
1461 struct global_queue **gl = qedi->global_queues;
1463 for (i = 0; i < qedi->num_queues; i++) {
1468 dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
1469 gl[i]->cq, gl[i]->cq_dma);
1471 dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
1472 gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
1476 qedi_free_bdq(qedi);
1477 qedi_free_nvm_iscsi_cfg(qedi);
1480 static int qedi_alloc_bdq(struct qedi_ctx *qedi)
1483 struct scsi_bd *pbl;
1487 /* Alloc dma memory for BDQ buffers */
1488 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1489 qedi->bdq[i].buf_addr =
1490 dma_alloc_coherent(&qedi->pdev->dev,
1492 &qedi->bdq[i].buf_dma,
1494 if (!qedi->bdq[i].buf_addr) {
1495 QEDI_ERR(&qedi->dbg_ctx,
1496 "Could not allocate BDQ buffer %d.\n", i);
1501 /* Alloc dma memory for BDQ page buffer list */
1502 qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
1503 qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, QEDI_PAGE_SIZE);
1504 qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
1506 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
1507 qedi->rq_num_entries);
1509 qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1510 qedi->bdq_pbl_mem_size,
1511 &qedi->bdq_pbl_dma, GFP_KERNEL);
1512 if (!qedi->bdq_pbl) {
1513 QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
1518 * Populate BDQ PBL with physical and virtual address of individual
1521 pbl = (struct scsi_bd *)qedi->bdq_pbl;
1522 for (i = 0; i < QEDI_BDQ_NUM; i++) {
1524 cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
1526 cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
1527 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1528 "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
1529 pbl, pbl->address.hi, pbl->address.lo, i);
1530 pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
1531 pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
1532 pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
1533 pbl->opaque.iscsi_opaque.opaque = cpu_to_le16(i);
1537 /* Allocate list of PBL pages */
1538 qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
1540 &qedi->bdq_pbl_list_dma,
1542 if (!qedi->bdq_pbl_list) {
1543 QEDI_ERR(&qedi->dbg_ctx,
1544 "Could not allocate list of PBL pages.\n");
1549 * Now populate PBL list with pages that contain pointers to the
1550 * individual buffers.
1552 qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size /
1554 list = (u64 *)qedi->bdq_pbl_list;
1555 page = qedi->bdq_pbl_list_dma;
1556 for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
1557 *list = qedi->bdq_pbl_dma;
1559 page += QEDI_PAGE_SIZE;
1565 static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
1575 * Number of global queues (CQ / RQ). This should
1576 * be <= number of available MSIX vectors for the PF
1578 if (!qedi->num_queues) {
1579 QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
1583 /* Make sure we allocated the PBL that will contain the physical
1584 * addresses of our queues
1586 if (!qedi->p_cpuq) {
1588 goto mem_alloc_failure;
1591 qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
1592 qedi->num_queues), GFP_KERNEL);
1593 if (!qedi->global_queues) {
1594 QEDI_ERR(&qedi->dbg_ctx,
1595 "Unable to allocate global queues array ptr memory\n");
1598 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1599 "qedi->global_queues=%p.\n", qedi->global_queues);
1601 /* Allocate DMA coherent buffers for BDQ */
1602 rc = qedi_alloc_bdq(qedi);
1604 goto mem_alloc_failure;
1606 /* Allocate DMA coherent buffers for NVM_ISCSI_CFG */
1607 rc = qedi_alloc_nvm_iscsi_cfg(qedi);
1609 goto mem_alloc_failure;
1611 /* Allocate a CQ and an associated PBL for each MSI-X
1614 for (i = 0; i < qedi->num_queues; i++) {
1615 qedi->global_queues[i] =
1616 kzalloc(sizeof(*qedi->global_queues[0]),
1618 if (!qedi->global_queues[i]) {
1619 QEDI_ERR(&qedi->dbg_ctx,
1620 "Unable to allocation global queue %d.\n", i);
1621 goto mem_alloc_failure;
1624 qedi->global_queues[i]->cq_mem_size =
1625 (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
1626 qedi->global_queues[i]->cq_mem_size =
1627 (qedi->global_queues[i]->cq_mem_size +
1628 (QEDI_PAGE_SIZE - 1));
1630 qedi->global_queues[i]->cq_pbl_size =
1631 (qedi->global_queues[i]->cq_mem_size /
1632 QEDI_PAGE_SIZE) * sizeof(void *);
1633 qedi->global_queues[i]->cq_pbl_size =
1634 (qedi->global_queues[i]->cq_pbl_size +
1635 (QEDI_PAGE_SIZE - 1));
1637 qedi->global_queues[i]->cq = dma_alloc_coherent(&qedi->pdev->dev,
1638 qedi->global_queues[i]->cq_mem_size,
1639 &qedi->global_queues[i]->cq_dma,
1642 if (!qedi->global_queues[i]->cq) {
1643 QEDI_WARN(&qedi->dbg_ctx,
1644 "Could not allocate cq.\n");
1646 goto mem_alloc_failure;
1648 qedi->global_queues[i]->cq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1649 qedi->global_queues[i]->cq_pbl_size,
1650 &qedi->global_queues[i]->cq_pbl_dma,
1653 if (!qedi->global_queues[i]->cq_pbl) {
1654 QEDI_WARN(&qedi->dbg_ctx,
1655 "Could not allocate cq PBL.\n");
1657 goto mem_alloc_failure;
1661 num_pages = qedi->global_queues[i]->cq_mem_size /
1663 page = qedi->global_queues[i]->cq_dma;
1664 pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
1666 while (num_pages--) {
1669 *pbl = (u32)((u64)page >> 32);
1671 page += QEDI_PAGE_SIZE;
1675 list = (u32 *)qedi->p_cpuq;
1678 * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
1679 * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
1680 * to the physical address which contains an array of pointers to the
1681 * physical addresses of the specific queue pages.
1683 for (i = 0; i < qedi->num_queues; i++) {
1684 *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
1686 *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
1691 *list = (u32)((u64)0 >> 32);
1698 qedi_free_global_queues(qedi);
1702 int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1712 /* Calculate appropriate queue and PBL sizes */
1713 ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
1714 ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
1716 ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
1717 ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
1719 ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
1720 &ep->sq_dma, GFP_KERNEL);
1722 QEDI_WARN(&qedi->dbg_ctx,
1723 "Could not allocate send queue.\n");
1727 ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
1728 &ep->sq_pbl_dma, GFP_KERNEL);
1730 QEDI_WARN(&qedi->dbg_ctx,
1731 "Could not allocate send queue PBL.\n");
1737 num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
1739 pbl = (u32 *)ep->sq_pbl;
1741 while (num_pages--) {
1744 *pbl = (u32)((u64)page >> 32);
1746 page += QEDI_PAGE_SIZE;
1752 dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1758 void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1761 dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
1764 dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1768 int qedi_get_task_idx(struct qedi_ctx *qedi)
1773 tmp_idx = find_first_zero_bit(qedi->task_idx_map,
1774 MAX_ISCSI_TASK_ENTRIES);
1776 if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
1777 QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
1782 if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
1789 void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
1791 if (!test_and_clear_bit(idx, qedi->task_idx_map))
1792 QEDI_ERR(&qedi->dbg_ctx,
1793 "FW task context, already cleared, tid=0x%x\n", idx);
1796 void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
1797 struct qedi_cmd *cmd)
1799 qedi->itt_map[tid].itt = proto_itt;
1800 qedi->itt_map[tid].p_cmd = cmd;
1802 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1803 "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
1804 qedi->itt_map[tid].itt);
1807 void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
1811 for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
1812 if (qedi->itt_map[i].itt == itt) {
1814 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1815 "Ref itt=0x%x, found at tid=0x%x\n",
1824 void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
1826 *proto_itt = qedi->itt_map[tid].itt;
1827 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1828 "Get itt map tid [0x%x with proto itt[0x%x]",
1832 struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
1834 struct qedi_cmd *cmd = NULL;
1836 if (tid >= MAX_ISCSI_TASK_ENTRIES)
1839 cmd = qedi->itt_map[tid].p_cmd;
1840 if (cmd->task_id != tid)
1843 qedi->itt_map[tid].p_cmd = NULL;
1848 static int qedi_alloc_itt(struct qedi_ctx *qedi)
1850 qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
1851 sizeof(struct qedi_itt_map), GFP_KERNEL);
1852 if (!qedi->itt_map) {
1853 QEDI_ERR(&qedi->dbg_ctx,
1854 "Unable to allocate itt map array memory\n");
1860 static void qedi_free_itt(struct qedi_ctx *qedi)
1862 kfree(qedi->itt_map);
1865 static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
1866 .rx_cb = qedi_ll2_rx,
1870 static int qedi_percpu_io_thread(void *arg)
1872 struct qedi_percpu_s *p = arg;
1873 struct qedi_work *work, *tmp;
1874 unsigned long flags;
1875 LIST_HEAD(work_list);
1877 set_user_nice(current, -20);
1879 while (!kthread_should_stop()) {
1880 spin_lock_irqsave(&p->p_work_lock, flags);
1881 while (!list_empty(&p->work_list)) {
1882 list_splice_init(&p->work_list, &work_list);
1883 spin_unlock_irqrestore(&p->p_work_lock, flags);
1885 list_for_each_entry_safe(work, tmp, &work_list, list) {
1886 list_del_init(&work->list);
1887 qedi_fp_process_cqes(work);
1888 if (!work->is_solicited)
1892 spin_lock_irqsave(&p->p_work_lock, flags);
1894 set_current_state(TASK_INTERRUPTIBLE);
1895 spin_unlock_irqrestore(&p->p_work_lock, flags);
1898 __set_current_state(TASK_RUNNING);
1903 static int qedi_cpu_online(unsigned int cpu)
1905 struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1906 struct task_struct *thread;
1908 thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
1910 "qedi_thread/%d", cpu);
1912 return PTR_ERR(thread);
1914 kthread_bind(thread, cpu);
1915 p->iothread = thread;
1916 wake_up_process(thread);
1920 static int qedi_cpu_offline(unsigned int cpu)
1922 struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1923 struct qedi_work *work, *tmp;
1924 struct task_struct *thread;
1926 spin_lock_bh(&p->p_work_lock);
1927 thread = p->iothread;
1930 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
1931 list_del_init(&work->list);
1932 qedi_fp_process_cqes(work);
1933 if (!work->is_solicited)
1937 spin_unlock_bh(&p->p_work_lock);
1939 kthread_stop(thread);
1943 void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
1945 struct qed_ll2_params params;
1947 qedi_recover_all_conns(qedi);
1949 qedi_ops->ll2->stop(qedi->cdev);
1950 qedi_ll2_free_skbs(qedi);
1952 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
1953 qedi->ll2_mtu, mtu);
1954 memset(¶ms, 0, sizeof(params));
1955 qedi->ll2_mtu = mtu;
1956 params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
1957 params.drop_ttl0_packets = 0;
1958 params.rx_vlan_stripping = 1;
1959 ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
1960 qedi_ops->ll2->start(qedi->cdev, ¶ms);
1964 * qedi_get_nvram_block: - Scan through the iSCSI NVRAM block (while accounting
1965 * for gaps) for the matching absolute-pf-id of the QEDI device.
1967 static struct nvm_iscsi_block *
1968 qedi_get_nvram_block(struct qedi_ctx *qedi)
1973 struct nvm_iscsi_block *block;
1975 pf = qedi->dev_info.common.abs_pf_id;
1976 block = &qedi->iscsi_image->iscsi_cfg.block[0];
1977 for (i = 0; i < NUM_OF_ISCSI_PF_SUPPORTED; i++, block++) {
1978 flags = ((block->id) & NVM_ISCSI_CFG_BLK_CTRL_FLAG_MASK) >>
1979 NVM_ISCSI_CFG_BLK_CTRL_FLAG_OFFSET;
1980 if (flags & (NVM_ISCSI_CFG_BLK_CTRL_FLAG_IS_NOT_EMPTY |
1981 NVM_ISCSI_CFG_BLK_CTRL_FLAG_PF_MAPPED) &&
1982 (pf == (block->id & NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_MASK)
1983 >> NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_OFFSET))
1989 static ssize_t qedi_show_boot_eth_info(void *data, int type, char *buf)
1991 struct qedi_ctx *qedi = data;
1992 struct nvm_iscsi_initiator *initiator;
1994 u32 ipv6_en, dhcp_en, ip_len;
1995 struct nvm_iscsi_block *block;
1996 char *fmt, *ip, *sub, *gw;
1998 block = qedi_get_nvram_block(qedi);
2002 initiator = &block->initiator;
2003 ipv6_en = block->generic.ctrl_flags &
2004 NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2005 dhcp_en = block->generic.ctrl_flags &
2006 NVM_ISCSI_CFG_GEN_DHCP_TCPIP_CONFIG_ENABLED;
2007 /* Static IP assignments. */
2008 fmt = ipv6_en ? "%pI6\n" : "%pI4\n";
2009 ip = ipv6_en ? initiator->ipv6.addr.byte : initiator->ipv4.addr.byte;
2010 ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2011 sub = ipv6_en ? initiator->ipv6.subnet_mask.byte :
2012 initiator->ipv4.subnet_mask.byte;
2013 gw = ipv6_en ? initiator->ipv6.gateway.byte :
2014 initiator->ipv4.gateway.byte;
2015 /* DHCP IP adjustments. */
2016 fmt = dhcp_en ? "%s\n" : fmt;
2018 ip = ipv6_en ? "0::0" : "0.0.0.0";
2021 ip_len = ipv6_en ? 5 : 8;
2025 case ISCSI_BOOT_ETH_IP_ADDR:
2026 rc = snprintf(buf, ip_len, fmt, ip);
2028 case ISCSI_BOOT_ETH_SUBNET_MASK:
2029 rc = snprintf(buf, ip_len, fmt, sub);
2031 case ISCSI_BOOT_ETH_GATEWAY:
2032 rc = snprintf(buf, ip_len, fmt, gw);
2034 case ISCSI_BOOT_ETH_FLAGS:
2035 rc = snprintf(buf, 3, "%hhd\n",
2036 SYSFS_FLAG_FW_SEL_BOOT);
2038 case ISCSI_BOOT_ETH_INDEX:
2039 rc = snprintf(buf, 3, "0\n");
2041 case ISCSI_BOOT_ETH_MAC:
2042 rc = sysfs_format_mac(buf, qedi->mac, ETH_ALEN);
2044 case ISCSI_BOOT_ETH_VLAN:
2045 rc = snprintf(buf, 12, "%d\n",
2046 GET_FIELD2(initiator->generic_cont0,
2047 NVM_ISCSI_CFG_INITIATOR_VLAN));
2049 case ISCSI_BOOT_ETH_ORIGIN:
2051 rc = snprintf(buf, 3, "3\n");
2061 static umode_t qedi_eth_get_attr_visibility(void *data, int type)
2066 case ISCSI_BOOT_ETH_FLAGS:
2067 case ISCSI_BOOT_ETH_MAC:
2068 case ISCSI_BOOT_ETH_INDEX:
2069 case ISCSI_BOOT_ETH_IP_ADDR:
2070 case ISCSI_BOOT_ETH_SUBNET_MASK:
2071 case ISCSI_BOOT_ETH_GATEWAY:
2072 case ISCSI_BOOT_ETH_ORIGIN:
2073 case ISCSI_BOOT_ETH_VLAN:
2083 static ssize_t qedi_show_boot_ini_info(void *data, int type, char *buf)
2085 struct qedi_ctx *qedi = data;
2086 struct nvm_iscsi_initiator *initiator;
2088 struct nvm_iscsi_block *block;
2090 block = qedi_get_nvram_block(qedi);
2094 initiator = &block->initiator;
2097 case ISCSI_BOOT_INI_INITIATOR_NAME:
2098 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2099 initiator->initiator_name.byte);
2108 static umode_t qedi_ini_get_attr_visibility(void *data, int type)
2113 case ISCSI_BOOT_INI_INITIATOR_NAME:
2124 qedi_show_boot_tgt_info(struct qedi_ctx *qedi, int type,
2125 char *buf, enum qedi_nvm_tgts idx)
2128 u32 ctrl_flags, ipv6_en, chap_en, mchap_en, ip_len;
2129 struct nvm_iscsi_block *block;
2130 char *chap_name, *chap_secret;
2131 char *mchap_name, *mchap_secret;
2133 block = qedi_get_nvram_block(qedi);
2135 goto exit_show_tgt_info;
2137 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2138 "Port:%d, tgt_idx:%d\n",
2139 GET_FIELD2(block->id, NVM_ISCSI_CFG_BLK_MAPPED_PF_ID), idx);
2141 ctrl_flags = block->target[idx].ctrl_flags &
2142 NVM_ISCSI_CFG_TARGET_ENABLED;
2145 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2146 "Target disabled\n");
2147 goto exit_show_tgt_info;
2150 ipv6_en = block->generic.ctrl_flags &
2151 NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2152 ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2153 chap_en = block->generic.ctrl_flags &
2154 NVM_ISCSI_CFG_GEN_CHAP_ENABLED;
2155 chap_name = chap_en ? block->initiator.chap_name.byte : NULL;
2156 chap_secret = chap_en ? block->initiator.chap_password.byte : NULL;
2158 mchap_en = block->generic.ctrl_flags &
2159 NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED;
2160 mchap_name = mchap_en ? block->target[idx].chap_name.byte : NULL;
2161 mchap_secret = mchap_en ? block->target[idx].chap_password.byte : NULL;
2164 case ISCSI_BOOT_TGT_NAME:
2165 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2166 block->target[idx].target_name.byte);
2168 case ISCSI_BOOT_TGT_IP_ADDR:
2170 rc = snprintf(buf, ip_len, "%pI6\n",
2171 block->target[idx].ipv6_addr.byte);
2173 rc = snprintf(buf, ip_len, "%pI4\n",
2174 block->target[idx].ipv4_addr.byte);
2176 case ISCSI_BOOT_TGT_PORT:
2177 rc = snprintf(buf, 12, "%d\n",
2178 GET_FIELD2(block->target[idx].generic_cont0,
2179 NVM_ISCSI_CFG_TARGET_TCP_PORT));
2181 case ISCSI_BOOT_TGT_LUN:
2182 rc = snprintf(buf, 22, "%.*d\n",
2183 block->target[idx].lun.value[1],
2184 block->target[idx].lun.value[0]);
2186 case ISCSI_BOOT_TGT_CHAP_NAME:
2187 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2190 case ISCSI_BOOT_TGT_CHAP_SECRET:
2191 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2194 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2195 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2198 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2199 rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2202 case ISCSI_BOOT_TGT_FLAGS:
2203 rc = snprintf(buf, 3, "%hhd\n", SYSFS_FLAG_FW_SEL_BOOT);
2205 case ISCSI_BOOT_TGT_NIC_ASSOC:
2206 rc = snprintf(buf, 3, "0\n");
2217 static ssize_t qedi_show_boot_tgt_pri_info(void *data, int type, char *buf)
2219 struct qedi_ctx *qedi = data;
2221 return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_PRI);
2224 static ssize_t qedi_show_boot_tgt_sec_info(void *data, int type, char *buf)
2226 struct qedi_ctx *qedi = data;
2228 return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_SEC);
2231 static umode_t qedi_tgt_get_attr_visibility(void *data, int type)
2236 case ISCSI_BOOT_TGT_NAME:
2237 case ISCSI_BOOT_TGT_IP_ADDR:
2238 case ISCSI_BOOT_TGT_PORT:
2239 case ISCSI_BOOT_TGT_LUN:
2240 case ISCSI_BOOT_TGT_CHAP_NAME:
2241 case ISCSI_BOOT_TGT_CHAP_SECRET:
2242 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2243 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2244 case ISCSI_BOOT_TGT_NIC_ASSOC:
2245 case ISCSI_BOOT_TGT_FLAGS:
2255 static void qedi_boot_release(void *data)
2257 struct qedi_ctx *qedi = data;
2259 scsi_host_put(qedi->shost);
2262 static int qedi_get_boot_info(struct qedi_ctx *qedi)
2266 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2267 "Get NVM iSCSI CFG image\n");
2268 ret = qedi_ops->common->nvm_get_image(qedi->cdev,
2269 QED_NVM_IMAGE_ISCSI_CFG,
2270 (char *)qedi->iscsi_image,
2271 sizeof(struct qedi_nvm_iscsi_image));
2273 QEDI_ERR(&qedi->dbg_ctx,
2274 "Could not get NVM image. ret = %d\n", ret);
2279 static int qedi_setup_boot_info(struct qedi_ctx *qedi)
2281 struct iscsi_boot_kobj *boot_kobj;
2283 if (qedi_get_boot_info(qedi))
2286 qedi->boot_kset = iscsi_boot_create_host_kset(qedi->shost->host_no);
2287 if (!qedi->boot_kset)
2290 if (!scsi_host_get(qedi->shost))
2293 boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 0, qedi,
2294 qedi_show_boot_tgt_pri_info,
2295 qedi_tgt_get_attr_visibility,
2300 if (!scsi_host_get(qedi->shost))
2303 boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 1, qedi,
2304 qedi_show_boot_tgt_sec_info,
2305 qedi_tgt_get_attr_visibility,
2310 if (!scsi_host_get(qedi->shost))
2313 boot_kobj = iscsi_boot_create_initiator(qedi->boot_kset, 0, qedi,
2314 qedi_show_boot_ini_info,
2315 qedi_ini_get_attr_visibility,
2320 if (!scsi_host_get(qedi->shost))
2323 boot_kobj = iscsi_boot_create_ethernet(qedi->boot_kset, 0, qedi,
2324 qedi_show_boot_eth_info,
2325 qedi_eth_get_attr_visibility,
2333 scsi_host_put(qedi->shost);
2335 iscsi_boot_destroy_kset(qedi->boot_kset);
2339 static void __qedi_remove(struct pci_dev *pdev, int mode)
2341 struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2344 if (mode == QEDI_MODE_SHUTDOWN)
2345 iscsi_host_for_each_session(qedi->shost,
2346 qedi_clear_session_ctx);
2348 if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2349 if (qedi->tmf_thread) {
2350 flush_workqueue(qedi->tmf_thread);
2351 destroy_workqueue(qedi->tmf_thread);
2352 qedi->tmf_thread = NULL;
2355 if (qedi->offload_thread) {
2356 flush_workqueue(qedi->offload_thread);
2357 destroy_workqueue(qedi->offload_thread);
2358 qedi->offload_thread = NULL;
2362 #ifdef CONFIG_DEBUG_FS
2363 qedi_dbg_host_exit(&qedi->dbg_ctx);
2365 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
2366 qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2368 qedi_sync_free_irqs(qedi);
2370 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2371 qedi_ops->stop(qedi->cdev);
2372 qedi_ops->ll2->stop(qedi->cdev);
2375 qedi_free_iscsi_pf_param(qedi);
2377 rval = qedi_ops->common->update_drv_state(qedi->cdev, false);
2379 QEDI_ERR(&qedi->dbg_ctx, "Failed to send drv state to MFW\n");
2381 if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2382 qedi_ops->common->slowpath_stop(qedi->cdev);
2383 qedi_ops->common->remove(qedi->cdev);
2386 qedi_destroy_fp(qedi);
2388 if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2389 qedi_release_cid_que(qedi);
2390 qedi_cm_free_mem(qedi);
2391 qedi_free_uio(qedi->udev);
2392 qedi_free_itt(qedi);
2394 if (qedi->ll2_recv_thread) {
2395 kthread_stop(qedi->ll2_recv_thread);
2396 qedi->ll2_recv_thread = NULL;
2398 qedi_ll2_free_skbs(qedi);
2400 if (qedi->boot_kset)
2401 iscsi_boot_destroy_kset(qedi->boot_kset);
2403 iscsi_host_remove(qedi->shost);
2404 iscsi_host_free(qedi->shost);
2408 static void qedi_shutdown(struct pci_dev *pdev)
2410 struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2412 QEDI_ERR(&qedi->dbg_ctx, "%s: Shutdown qedi\n", __func__);
2413 if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
2415 __qedi_remove(pdev, QEDI_MODE_SHUTDOWN);
2418 static int __qedi_probe(struct pci_dev *pdev, int mode)
2420 struct qedi_ctx *qedi;
2421 struct qed_ll2_params params;
2425 struct qed_link_params link_params;
2426 struct qed_slowpath_params sp_params;
2427 struct qed_probe_params qed_params;
2428 void *task_start, *task_end;
2431 if (mode != QEDI_MODE_RECOVERY) {
2432 qedi = qedi_host_alloc(pdev);
2438 qedi = pci_get_drvdata(pdev);
2441 memset(&qed_params, 0, sizeof(qed_params));
2442 qed_params.protocol = QED_PROTOCOL_ISCSI;
2443 qed_params.dp_module = qedi_qed_debug;
2444 qed_params.dp_level = dp_level;
2445 qed_params.is_vf = is_vf;
2446 qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
2449 QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
2453 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2455 rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2459 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2460 "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
2461 qedi->dev_info.common.num_hwfns,
2462 qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
2464 rc = qedi_set_iscsi_pf_param(qedi);
2467 QEDI_ERR(&qedi->dbg_ctx,
2468 "Set iSCSI pf param fail\n");
2472 qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2474 rc = qedi_prepare_fp(qedi);
2476 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
2477 goto free_pf_params;
2480 /* Start the Slowpath-process */
2481 memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
2482 sp_params.int_mode = QED_INT_MODE_MSIX;
2483 sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
2484 sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
2485 sp_params.drv_rev = QEDI_DRIVER_REV_VER;
2486 sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
2487 strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
2488 rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
2490 QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
2494 /* update_pf_params needs to be called before and after slowpath
2497 qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2499 rc = qedi_setup_int(qedi);
2501 goto stop_iscsi_func;
2503 qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2505 /* Learn information crucial for qedi to progress */
2506 rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2508 goto stop_iscsi_func;
2510 /* Record BDQ producer doorbell addresses */
2511 qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
2512 qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
2513 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2514 "BDQ primary_prod=%p secondary_prod=%p.\n",
2515 qedi->bdq_primary_prod,
2516 qedi->bdq_secondary_prod);
2519 * We need to write the number of BDs in the BDQ we've preallocated so
2520 * the f/w will do a prefetch and we'll get an unsolicited CQE when a
2523 qedi->bdq_prod_idx = QEDI_BDQ_NUM;
2524 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2525 "Writing %d to primary and secondary BDQ doorbell registers.\n",
2526 qedi->bdq_prod_idx);
2527 writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
2528 readw(qedi->bdq_primary_prod);
2529 writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
2530 readw(qedi->bdq_secondary_prod);
2532 ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
2533 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
2536 sprintf(host_buf, "host_%d", qedi->shost->host_no);
2537 qedi_ops->common->set_name(qedi->cdev, host_buf);
2539 qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
2541 memset(¶ms, 0, sizeof(params));
2542 params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
2543 qedi->ll2_mtu = DEF_PATH_MTU;
2544 params.drop_ttl0_packets = 0;
2545 params.rx_vlan_stripping = 1;
2546 ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
2548 if (mode != QEDI_MODE_RECOVERY) {
2549 /* set up rx path */
2550 INIT_LIST_HEAD(&qedi->ll2_skb_list);
2551 spin_lock_init(&qedi->ll2_lock);
2552 /* start qedi context */
2553 spin_lock_init(&qedi->hba_lock);
2554 spin_lock_init(&qedi->task_idx_lock);
2555 mutex_init(&qedi->stats_lock);
2557 qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
2558 qedi_ops->ll2->start(qedi->cdev, ¶ms);
2560 if (mode != QEDI_MODE_RECOVERY) {
2561 qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
2566 rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
2567 qedi, qedi_iscsi_event_cb);
2570 QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
2574 task_start = qedi_get_task_mem(&qedi->tasks, 0);
2575 task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
2576 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2577 "Task context start=%p, end=%p block_size=%u.\n",
2578 task_start, task_end, qedi->tasks.size);
2580 memset(&link_params, 0, sizeof(link_params));
2581 link_params.link_up = true;
2582 rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
2584 QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
2585 atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2588 #ifdef CONFIG_DEBUG_FS
2589 qedi_dbg_host_init(&qedi->dbg_ctx, qedi_debugfs_ops,
2592 QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2593 "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
2594 QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
2595 FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
2597 if (mode == QEDI_MODE_NORMAL) {
2598 if (iscsi_host_add(qedi->shost, &pdev->dev)) {
2599 QEDI_ERR(&qedi->dbg_ctx,
2600 "Could not add iscsi host\n");
2605 /* Allocate uio buffers */
2606 rc = qedi_alloc_uio_rings(qedi);
2608 QEDI_ERR(&qedi->dbg_ctx,
2609 "UIO alloc ring failed err=%d\n", rc);
2613 rc = qedi_init_uio(qedi);
2615 QEDI_ERR(&qedi->dbg_ctx,
2616 "UIO init failed, err=%d\n", rc);
2620 /* host the array on iscsi_conn */
2621 rc = qedi_setup_cid_que(qedi);
2623 QEDI_ERR(&qedi->dbg_ctx,
2624 "Could not setup cid que\n");
2628 rc = qedi_cm_alloc_mem(qedi);
2630 QEDI_ERR(&qedi->dbg_ctx,
2631 "Could not alloc cm memory\n");
2635 rc = qedi_alloc_itt(qedi);
2637 QEDI_ERR(&qedi->dbg_ctx,
2638 "Could not alloc itt memory\n");
2642 sprintf(host_buf, "host_%d", qedi->shost->host_no);
2643 qedi->tmf_thread = create_singlethread_workqueue(host_buf);
2644 if (!qedi->tmf_thread) {
2645 QEDI_ERR(&qedi->dbg_ctx,
2646 "Unable to start tmf thread!\n");
2651 sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
2652 qedi->offload_thread = create_workqueue(host_buf);
2653 if (!qedi->offload_thread) {
2654 QEDI_ERR(&qedi->dbg_ctx,
2655 "Unable to start offload thread!\n");
2660 INIT_DELAYED_WORK(&qedi->recovery_work, qedi_recovery_handler);
2662 /* F/w needs 1st task context memory entry for performance */
2663 set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
2664 atomic_set(&qedi->num_offloads, 0);
2666 if (qedi_setup_boot_info(qedi))
2667 QEDI_ERR(&qedi->dbg_ctx,
2668 "No iSCSI boot target configured\n");
2670 rc = qedi_ops->common->update_drv_state(qedi->cdev, true);
2672 QEDI_ERR(&qedi->dbg_ctx,
2673 "Failed to send drv state to MFW\n");
2680 qedi_release_cid_que(qedi);
2682 qedi_free_uio(qedi->udev);
2684 #ifdef CONFIG_DEBUG_FS
2685 qedi_dbg_host_exit(&qedi->dbg_ctx);
2687 iscsi_host_remove(qedi->shost);
2689 qedi_ops->stop(qedi->cdev);
2691 qedi_ops->common->slowpath_stop(qedi->cdev);
2693 qedi_ops->common->remove(qedi->cdev);
2695 qedi_free_iscsi_pf_param(qedi);
2697 iscsi_host_free(qedi->shost);
2702 static void qedi_mark_conn_recovery(struct iscsi_cls_session *cls_session)
2704 struct iscsi_session *session = cls_session->dd_data;
2705 struct iscsi_conn *conn = session->leadconn;
2706 struct qedi_conn *qedi_conn = conn->dd_data;
2708 iscsi_conn_failure(qedi_conn->cls_conn->dd_data, ISCSI_ERR_CONN_FAILED);
2711 static void qedi_recovery_handler(struct work_struct *work)
2713 struct qedi_ctx *qedi =
2714 container_of(work, struct qedi_ctx, recovery_work.work);
2716 iscsi_host_for_each_session(qedi->shost, qedi_mark_conn_recovery);
2718 /* Call common_ops->recovery_prolog to allow the MFW to quiesce
2719 * any PCI transactions.
2721 qedi_ops->common->recovery_prolog(qedi->cdev);
2723 __qedi_remove(qedi->pdev, QEDI_MODE_RECOVERY);
2724 __qedi_probe(qedi->pdev, QEDI_MODE_RECOVERY);
2725 clear_bit(QEDI_IN_RECOVERY, &qedi->flags);
2728 static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2730 return __qedi_probe(pdev, QEDI_MODE_NORMAL);
2733 static void qedi_remove(struct pci_dev *pdev)
2735 __qedi_remove(pdev, QEDI_MODE_NORMAL);
2738 static struct pci_device_id qedi_pci_tbl[] = {
2739 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
2740 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
2743 MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
2745 static enum cpuhp_state qedi_cpuhp_state;
2747 static struct pci_driver qedi_pci_driver = {
2748 .name = QEDI_MODULE_NAME,
2749 .id_table = qedi_pci_tbl,
2750 .probe = qedi_probe,
2751 .remove = qedi_remove,
2752 .shutdown = qedi_shutdown,
2755 static int __init qedi_init(void)
2757 struct qedi_percpu_s *p;
2760 qedi_ops = qed_get_iscsi_ops();
2762 QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
2766 #ifdef CONFIG_DEBUG_FS
2767 qedi_dbg_init("qedi");
2770 qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
2771 if (!qedi_scsi_transport) {
2772 QEDI_ERR(NULL, "Could not register qedi transport");
2774 goto exit_qedi_init_1;
2777 for_each_possible_cpu(cpu) {
2778 p = &per_cpu(qedi_percpu, cpu);
2779 INIT_LIST_HEAD(&p->work_list);
2780 spin_lock_init(&p->p_work_lock);
2784 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
2785 qedi_cpu_online, qedi_cpu_offline);
2787 goto exit_qedi_init_2;
2788 qedi_cpuhp_state = rc;
2790 rc = pci_register_driver(&qedi_pci_driver);
2792 QEDI_ERR(NULL, "Failed to register driver\n");
2799 cpuhp_remove_state(qedi_cpuhp_state);
2801 iscsi_unregister_transport(&qedi_iscsi_transport);
2803 #ifdef CONFIG_DEBUG_FS
2806 qed_put_iscsi_ops();
2810 static void __exit qedi_cleanup(void)
2812 pci_unregister_driver(&qedi_pci_driver);
2813 cpuhp_remove_state(qedi_cpuhp_state);
2814 iscsi_unregister_transport(&qedi_iscsi_transport);
2816 #ifdef CONFIG_DEBUG_FS
2819 qed_put_iscsi_ops();
2822 MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
2823 MODULE_LICENSE("GPL");
2824 MODULE_AUTHOR("QLogic Corporation");
2825 MODULE_VERSION(QEDI_MODULE_VERSION);
2826 module_init(qedi_init);
2827 module_exit(qedi_cleanup);