2 * This file is part of the Chelsio FCoE driver for Linux.
4 * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/pci.h>
41 #include <linux/aer.h>
43 #include <linux/notifier.h>
44 #include <linux/kdebug.h>
45 #include <linux/seq_file.h>
46 #include <linux/debugfs.h>
47 #include <linux/string.h>
48 #include <linux/export.h>
50 #include "csio_init.h"
51 #include "csio_defs.h"
53 #define CSIO_MIN_MEMPOOL_SZ 64
55 static struct dentry
*csio_debugfs_root
;
57 static struct scsi_transport_template
*csio_fcoe_transport
;
58 static struct scsi_transport_template
*csio_fcoe_transport_vport
;
64 csio_mem_open(struct inode
*inode
, struct file
*file
)
66 file
->private_data
= inode
->i_private
;
71 csio_mem_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
74 loff_t avail
= file
->f_path
.dentry
->d_inode
->i_size
;
75 unsigned int mem
= (uintptr_t)file
->private_data
& 3;
76 struct csio_hw
*hw
= file
->private_data
- mem
;
82 if (count
> avail
- pos
)
91 ret
= csio_hw_mc_read(hw
, pos
, data
, NULL
);
93 ret
= csio_hw_edc_read(hw
, mem
, pos
, data
, NULL
);
97 ofst
= pos
% sizeof(data
);
98 len
= min(count
, sizeof(data
) - ofst
);
99 if (copy_to_user(buf
, (u8
*)data
+ ofst
, len
))
111 static const struct file_operations csio_mem_debugfs_fops
= {
112 .owner
= THIS_MODULE
,
113 .open
= csio_mem_open
,
114 .read
= csio_mem_read
,
115 .llseek
= default_llseek
,
118 static void csio_add_debugfs_mem(struct csio_hw
*hw
, const char *name
,
119 unsigned int idx
, unsigned int size_mb
)
123 de
= debugfs_create_file(name
, S_IRUSR
, hw
->debugfs_root
,
124 (void *)hw
+ idx
, &csio_mem_debugfs_fops
);
125 if (de
&& de
->d_inode
)
126 de
->d_inode
->i_size
= size_mb
<< 20;
129 static int csio_setup_debugfs(struct csio_hw
*hw
)
133 if (IS_ERR_OR_NULL(hw
->debugfs_root
))
136 i
= csio_rd_reg32(hw
, MA_TARGET_MEM_ENABLE
);
137 if (i
& EDRAM0_ENABLE
)
138 csio_add_debugfs_mem(hw
, "edc0", MEM_EDC0
, 5);
139 if (i
& EDRAM1_ENABLE
)
140 csio_add_debugfs_mem(hw
, "edc1", MEM_EDC1
, 5);
141 if (i
& EXT_MEM_ENABLE
)
142 csio_add_debugfs_mem(hw
, "mc", MEM_MC
,
143 EXT_MEM_SIZE_GET(csio_rd_reg32(hw
, MA_EXT_MEMORY_BAR
)));
148 * csio_dfs_create - Creates and sets up per-hw debugfs.
152 csio_dfs_create(struct csio_hw
*hw
)
154 if (csio_debugfs_root
) {
155 hw
->debugfs_root
= debugfs_create_dir(pci_name(hw
->pdev
),
157 csio_setup_debugfs(hw
);
164 * csio_dfs_destroy - Destroys per-hw debugfs.
167 csio_dfs_destroy(struct csio_hw
*hw
)
169 if (hw
->debugfs_root
)
170 debugfs_remove_recursive(hw
->debugfs_root
);
176 * csio_dfs_init - Debug filesystem initialization for the module.
182 csio_debugfs_root
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
183 if (!csio_debugfs_root
)
184 pr_warn("Could not create debugfs entry, continuing\n");
190 * csio_dfs_exit - debugfs cleanup for the module.
195 debugfs_remove(csio_debugfs_root
);
199 * csio_pci_init - PCI initialization.
201 * @bars: Bitmask of bars to be requested.
203 * Initializes the PCI function by enabling MMIO, setting bus
204 * mastership and setting DMA mask.
207 csio_pci_init(struct pci_dev
*pdev
, int *bars
)
211 *bars
= pci_select_bars(pdev
, IORESOURCE_MEM
);
213 if (pci_enable_device_mem(pdev
))
216 if (pci_request_selected_regions(pdev
, *bars
, KBUILD_MODNAME
))
217 goto err_disable_device
;
219 pci_set_master(pdev
);
220 pci_try_set_mwi(pdev
);
222 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
223 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
224 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
225 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
227 dev_err(&pdev
->dev
, "No suitable DMA available.\n");
228 goto err_release_regions
;
234 pci_release_selected_regions(pdev
, *bars
);
236 pci_disable_device(pdev
);
243 * csio_pci_exit - PCI unitialization.
245 * @bars: Bars to be released.
249 csio_pci_exit(struct pci_dev
*pdev
, int *bars
)
251 pci_release_selected_regions(pdev
, *bars
);
252 pci_disable_device(pdev
);
256 * csio_hw_init_workers - Initialize the HW module's worker threads.
261 csio_hw_init_workers(struct csio_hw
*hw
)
263 INIT_WORK(&hw
->evtq_work
, csio_evtq_worker
);
267 csio_hw_exit_workers(struct csio_hw
*hw
)
269 cancel_work_sync(&hw
->evtq_work
);
270 flush_scheduled_work();
274 csio_create_queues(struct csio_hw
*hw
)
277 struct csio_mgmtm
*mgmtm
= csio_hw_to_mgmtm(hw
);
279 struct csio_scsi_cpu_info
*info
;
281 if (hw
->flags
& CSIO_HWF_Q_FW_ALLOCED
)
284 if (hw
->intr_mode
!= CSIO_IM_MSIX
) {
285 rv
= csio_wr_iq_create(hw
, NULL
, hw
->intr_iq_idx
,
286 0, hw
->pport
[0].portid
, false, NULL
);
288 csio_err(hw
, " Forward Interrupt IQ failed!: %d\n", rv
);
294 rv
= csio_wr_iq_create(hw
, NULL
, hw
->fwevt_iq_idx
,
295 csio_get_fwevt_intr_idx(hw
),
296 hw
->pport
[0].portid
, true, NULL
);
298 csio_err(hw
, "FW event IQ config failed!: %d\n", rv
);
302 /* Create mgmt queue */
303 rv
= csio_wr_eq_create(hw
, NULL
, mgmtm
->eq_idx
,
304 mgmtm
->iq_idx
, hw
->pport
[0].portid
, NULL
);
307 csio_err(hw
, "Mgmt EQ create failed!: %d\n", rv
);
311 /* Create SCSI queues */
312 for (i
= 0; i
< hw
->num_pports
; i
++) {
313 info
= &hw
->scsi_cpu_info
[i
];
315 for (j
= 0; j
< info
->max_cpus
; j
++) {
316 struct csio_scsi_qset
*sqset
= &hw
->sqset
[i
][j
];
318 rv
= csio_wr_iq_create(hw
, NULL
, sqset
->iq_idx
,
319 sqset
->intr_idx
, i
, false, NULL
);
322 "SCSI module IQ config failed [%d][%d]:%d\n",
326 rv
= csio_wr_eq_create(hw
, NULL
, sqset
->eq_idx
,
327 sqset
->iq_idx
, i
, NULL
);
330 "SCSI module EQ config failed [%d][%d]:%d\n",
335 } /* For all ports */
337 hw
->flags
|= CSIO_HWF_Q_FW_ALLOCED
;
340 csio_wr_destroy_queues(hw
, true);
345 * csio_config_queues - Configure the DMA queues.
348 * Allocates memory for queues are registers them with FW.
351 csio_config_queues(struct csio_hw
*hw
)
353 int i
, j
, idx
, k
= 0;
355 struct csio_scsi_qset
*sqset
;
356 struct csio_mgmtm
*mgmtm
= csio_hw_to_mgmtm(hw
);
357 struct csio_scsi_qset
*orig
;
358 struct csio_scsi_cpu_info
*info
;
360 if (hw
->flags
& CSIO_HWF_Q_MEM_ALLOCED
)
361 return csio_create_queues(hw
);
363 /* Calculate number of SCSI queues for MSIX we would like */
364 hw
->num_scsi_msix_cpus
= num_online_cpus();
365 hw
->num_sqsets
= num_online_cpus() * hw
->num_pports
;
367 if (hw
->num_sqsets
> CSIO_MAX_SCSI_QSETS
) {
368 hw
->num_sqsets
= CSIO_MAX_SCSI_QSETS
;
369 hw
->num_scsi_msix_cpus
= CSIO_MAX_SCSI_CPU
;
372 /* Initialize max_cpus, may get reduced during msix allocations */
373 for (i
= 0; i
< hw
->num_pports
; i
++)
374 hw
->scsi_cpu_info
[i
].max_cpus
= hw
->num_scsi_msix_cpus
;
376 csio_dbg(hw
, "nsqsets:%d scpus:%d\n",
377 hw
->num_sqsets
, hw
->num_scsi_msix_cpus
);
379 csio_intr_enable(hw
);
381 if (hw
->intr_mode
!= CSIO_IM_MSIX
) {
383 /* Allocate Forward interrupt iq. */
384 hw
->intr_iq_idx
= csio_wr_alloc_q(hw
, CSIO_INTR_IQSIZE
,
385 CSIO_INTR_WRSIZE
, CSIO_INGRESS
,
386 (void *)hw
, 0, 0, NULL
);
387 if (hw
->intr_iq_idx
== -1) {
389 "Forward interrupt queue creation failed\n");
394 /* Allocate the FW evt queue */
395 hw
->fwevt_iq_idx
= csio_wr_alloc_q(hw
, CSIO_FWEVT_IQSIZE
,
397 CSIO_INGRESS
, (void *)hw
,
398 CSIO_FWEVT_FLBUFS
, 0,
399 csio_fwevt_intx_handler
);
400 if (hw
->fwevt_iq_idx
== -1) {
401 csio_err(hw
, "FW evt queue creation failed\n");
405 /* Allocate the mgmt queue */
406 mgmtm
->eq_idx
= csio_wr_alloc_q(hw
, CSIO_MGMT_EQSIZE
,
408 CSIO_EGRESS
, (void *)hw
, 0, 0, NULL
);
409 if (mgmtm
->eq_idx
== -1) {
410 csio_err(hw
, "Failed to alloc egress queue for mgmt module\n");
414 /* Use FW IQ for MGMT req completion */
415 mgmtm
->iq_idx
= hw
->fwevt_iq_idx
;
417 /* Allocate SCSI queues */
418 for (i
= 0; i
< hw
->num_pports
; i
++) {
419 info
= &hw
->scsi_cpu_info
[i
];
421 for (j
= 0; j
< hw
->num_scsi_msix_cpus
; j
++) {
422 sqset
= &hw
->sqset
[i
][j
];
424 if (j
>= info
->max_cpus
) {
425 k
= j
% info
->max_cpus
;
426 orig
= &hw
->sqset
[i
][k
];
427 sqset
->eq_idx
= orig
->eq_idx
;
428 sqset
->iq_idx
= orig
->iq_idx
;
432 idx
= csio_wr_alloc_q(hw
, csio_scsi_eqsize
, 0,
433 CSIO_EGRESS
, (void *)hw
, 0, 0,
436 csio_err(hw
, "EQ creation failed for idx:%d\n",
443 idx
= csio_wr_alloc_q(hw
, CSIO_SCSI_IQSIZE
,
444 CSIO_SCSI_IQ_WRSZ
, CSIO_INGRESS
,
446 csio_scsi_intx_handler
);
448 csio_err(hw
, "IQ creation failed for idx:%d\n",
454 } /* For all ports */
456 hw
->flags
|= CSIO_HWF_Q_MEM_ALLOCED
;
458 rv
= csio_create_queues(hw
);
463 * Now request IRQs for the vectors. In the event of a failure,
464 * cleanup is handled internally by this function.
466 rv
= csio_request_irqs(hw
);
473 csio_intr_disable(hw
, false);
479 csio_resource_alloc(struct csio_hw
*hw
)
481 struct csio_wrm
*wrm
= csio_hw_to_wrm(hw
);
484 wrm
->num_q
= ((CSIO_MAX_SCSI_QSETS
* 2) + CSIO_HW_NIQ
+
485 CSIO_HW_NEQ
+ CSIO_HW_NFLQ
+ CSIO_HW_NINTXQ
);
487 hw
->mb_mempool
= mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ
,
488 sizeof(struct csio_mb
));
492 hw
->rnode_mempool
= mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ
,
493 sizeof(struct csio_rnode
));
494 if (!hw
->rnode_mempool
)
495 goto err_free_mb_mempool
;
497 hw
->scsi_pci_pool
= pci_pool_create("csio_scsi_pci_pool", hw
->pdev
,
498 CSIO_SCSI_RSP_LEN
, 8, 0);
499 if (!hw
->scsi_pci_pool
)
500 goto err_free_rn_pool
;
505 mempool_destroy(hw
->rnode_mempool
);
506 hw
->rnode_mempool
= NULL
;
508 mempool_destroy(hw
->mb_mempool
);
509 hw
->mb_mempool
= NULL
;
515 csio_resource_free(struct csio_hw
*hw
)
517 pci_pool_destroy(hw
->scsi_pci_pool
);
518 hw
->scsi_pci_pool
= NULL
;
519 mempool_destroy(hw
->rnode_mempool
);
520 hw
->rnode_mempool
= NULL
;
521 mempool_destroy(hw
->mb_mempool
);
522 hw
->mb_mempool
= NULL
;
526 * csio_hw_alloc - Allocate and initialize the HW module.
529 * Allocates HW structure, DMA, memory resources, maps BARS to
530 * host memory and initializes HW module.
532 static struct csio_hw
*csio_hw_alloc(struct pci_dev
*pdev
)
536 hw
= kzalloc(sizeof(struct csio_hw
), GFP_KERNEL
);
541 strncpy(hw
->drv_version
, CSIO_DRV_VERSION
, 32);
543 /* memory pool/DMA pool allocation */
544 if (csio_resource_alloc(hw
))
547 /* Get the start address of registers from BAR 0 */
548 hw
->regstart
= ioremap_nocache(pci_resource_start(pdev
, 0),
549 pci_resource_len(pdev
, 0));
551 csio_err(hw
, "Could not map BAR 0, regstart = %p\n",
553 goto err_resource_free
;
556 csio_hw_init_workers(hw
);
558 if (csio_hw_init(hw
))
563 csio_dbg(hw
, "hw:%p\n", hw
);
568 csio_hw_exit_workers(hw
);
569 iounmap(hw
->regstart
);
571 csio_resource_free(hw
);
579 * csio_hw_free - Uninitialize and free the HW module.
582 * Disable interrupts, uninit the HW module, free resources, free hw.
585 csio_hw_free(struct csio_hw
*hw
)
587 csio_intr_disable(hw
, true);
588 csio_hw_exit_workers(hw
);
590 iounmap(hw
->regstart
);
591 csio_dfs_destroy(hw
);
592 csio_resource_free(hw
);
597 * csio_shost_init - Create and initialize the lnode module.
598 * @hw: The HW module.
599 * @dev: The device associated with this invocation.
600 * @probe: Called from probe context or not?
601 * @os_pln: Parent lnode if any.
603 * Allocates lnode structure via scsi_host_alloc, initializes
604 * shost, initializes lnode module and registers with SCSI ML
605 * via scsi_host_add. This function is shared between physical and
606 * virtual node ports.
609 csio_shost_init(struct csio_hw
*hw
, struct device
*dev
,
610 bool probe
, struct csio_lnode
*pln
)
612 struct Scsi_Host
*shost
= NULL
;
613 struct csio_lnode
*ln
;
615 csio_fcoe_shost_template
.cmd_per_lun
= csio_lun_qdepth
;
616 csio_fcoe_shost_vport_template
.cmd_per_lun
= csio_lun_qdepth
;
619 * hw->pdev is the physical port's PCI dev structure,
620 * which will be different from the NPIV dev structure.
622 if (dev
== &hw
->pdev
->dev
)
623 shost
= scsi_host_alloc(
624 &csio_fcoe_shost_template
,
625 sizeof(struct csio_lnode
));
627 shost
= scsi_host_alloc(
628 &csio_fcoe_shost_vport_template
,
629 sizeof(struct csio_lnode
));
634 ln
= shost_priv(shost
);
635 memset(ln
, 0, sizeof(struct csio_lnode
));
637 /* Link common lnode to this lnode */
638 ln
->dev_num
= (shost
->host_no
<< 16);
640 shost
->can_queue
= CSIO_MAX_QUEUE
;
642 shost
->unique_id
= shost
->host_no
;
643 shost
->max_cmd_len
= 16; /* Max CDB length supported */
644 shost
->max_id
= min_t(uint32_t, csio_fcoe_rnodes
,
645 hw
->fres_info
.max_ssns
);
646 shost
->max_lun
= CSIO_MAX_LUN
;
647 if (dev
== &hw
->pdev
->dev
)
648 shost
->transportt
= csio_fcoe_transport
;
650 shost
->transportt
= csio_fcoe_transport_vport
;
656 /* Other initialization here: Common, Transport specific */
657 if (csio_lnode_init(ln
, hw
, pln
))
660 if (scsi_add_host(shost
, dev
))
668 scsi_host_put(shost
);
674 * csio_shost_exit - De-instantiate the shost.
675 * @ln: The lnode module corresponding to the shost.
679 csio_shost_exit(struct csio_lnode
*ln
)
681 struct Scsi_Host
*shost
= csio_ln_to_shost(ln
);
682 struct csio_hw
*hw
= csio_lnode_to_hw(ln
);
684 /* Inform transport */
685 fc_remove_host(shost
);
688 scsi_remove_host(shost
);
690 /* Flush all the events, so that any rnode removal events
691 * already queued are all handled, before we remove the lnode.
693 spin_lock_irq(&hw
->lock
);
695 spin_unlock_irq(&hw
->lock
);
698 scsi_host_put(shost
);
702 csio_lnode_alloc(struct csio_hw
*hw
)
704 return csio_shost_init(hw
, &hw
->pdev
->dev
, false, NULL
);
708 csio_lnodes_block_request(struct csio_hw
*hw
)
710 struct Scsi_Host
*shost
;
711 struct csio_lnode
*sln
;
712 struct csio_lnode
*ln
;
713 struct list_head
*cur_ln
, *cur_cln
;
714 struct csio_lnode
**lnode_list
;
717 lnode_list
= kzalloc((sizeof(struct csio_lnode
*) * hw
->num_lns
),
720 csio_err(hw
, "Failed to allocate lnodes_list");
724 spin_lock_irq(&hw
->lock
);
725 /* Traverse sibling lnodes */
726 list_for_each(cur_ln
, &hw
->sln_head
) {
727 sln
= (struct csio_lnode
*) cur_ln
;
728 lnode_list
[cur_cnt
++] = sln
;
730 /* Traverse children lnodes */
731 list_for_each(cur_cln
, &sln
->cln_head
)
732 lnode_list
[cur_cnt
++] = (struct csio_lnode
*) cur_cln
;
734 spin_unlock_irq(&hw
->lock
);
736 for (ii
= 0; ii
< cur_cnt
; ii
++) {
737 csio_dbg(hw
, "Blocking IOs on lnode: %p\n", lnode_list
[ii
]);
739 shost
= csio_ln_to_shost(ln
);
740 scsi_block_requests(shost
);
747 csio_lnodes_unblock_request(struct csio_hw
*hw
)
749 struct csio_lnode
*ln
;
750 struct Scsi_Host
*shost
;
751 struct csio_lnode
*sln
;
752 struct list_head
*cur_ln
, *cur_cln
;
753 struct csio_lnode
**lnode_list
;
756 lnode_list
= kzalloc((sizeof(struct csio_lnode
*) * hw
->num_lns
),
759 csio_err(hw
, "Failed to allocate lnodes_list");
763 spin_lock_irq(&hw
->lock
);
764 /* Traverse sibling lnodes */
765 list_for_each(cur_ln
, &hw
->sln_head
) {
766 sln
= (struct csio_lnode
*) cur_ln
;
767 lnode_list
[cur_cnt
++] = sln
;
769 /* Traverse children lnodes */
770 list_for_each(cur_cln
, &sln
->cln_head
)
771 lnode_list
[cur_cnt
++] = (struct csio_lnode
*) cur_cln
;
773 spin_unlock_irq(&hw
->lock
);
775 for (ii
= 0; ii
< cur_cnt
; ii
++) {
776 csio_dbg(hw
, "unblocking IOs on lnode: %p\n", lnode_list
[ii
]);
778 shost
= csio_ln_to_shost(ln
);
779 scsi_unblock_requests(shost
);
785 csio_lnodes_block_by_port(struct csio_hw
*hw
, uint8_t portid
)
787 struct csio_lnode
*ln
;
788 struct Scsi_Host
*shost
;
789 struct csio_lnode
*sln
;
790 struct list_head
*cur_ln
, *cur_cln
;
791 struct csio_lnode
**lnode_list
;
794 lnode_list
= kzalloc((sizeof(struct csio_lnode
*) * hw
->num_lns
),
797 csio_err(hw
, "Failed to allocate lnodes_list");
801 spin_lock_irq(&hw
->lock
);
802 /* Traverse sibling lnodes */
803 list_for_each(cur_ln
, &hw
->sln_head
) {
804 sln
= (struct csio_lnode
*) cur_ln
;
805 if (sln
->portid
!= portid
)
808 lnode_list
[cur_cnt
++] = sln
;
810 /* Traverse children lnodes */
811 list_for_each(cur_cln
, &sln
->cln_head
)
812 lnode_list
[cur_cnt
++] = (struct csio_lnode
*) cur_cln
;
814 spin_unlock_irq(&hw
->lock
);
816 for (ii
= 0; ii
< cur_cnt
; ii
++) {
817 csio_dbg(hw
, "Blocking IOs on lnode: %p\n", lnode_list
[ii
]);
819 shost
= csio_ln_to_shost(ln
);
820 scsi_block_requests(shost
);
826 csio_lnodes_unblock_by_port(struct csio_hw
*hw
, uint8_t portid
)
828 struct csio_lnode
*ln
;
829 struct Scsi_Host
*shost
;
830 struct csio_lnode
*sln
;
831 struct list_head
*cur_ln
, *cur_cln
;
832 struct csio_lnode
**lnode_list
;
835 lnode_list
= kzalloc((sizeof(struct csio_lnode
*) * hw
->num_lns
),
838 csio_err(hw
, "Failed to allocate lnodes_list");
842 spin_lock_irq(&hw
->lock
);
843 /* Traverse sibling lnodes */
844 list_for_each(cur_ln
, &hw
->sln_head
) {
845 sln
= (struct csio_lnode
*) cur_ln
;
846 if (sln
->portid
!= portid
)
848 lnode_list
[cur_cnt
++] = sln
;
850 /* Traverse children lnodes */
851 list_for_each(cur_cln
, &sln
->cln_head
)
852 lnode_list
[cur_cnt
++] = (struct csio_lnode
*) cur_cln
;
854 spin_unlock_irq(&hw
->lock
);
856 for (ii
= 0; ii
< cur_cnt
; ii
++) {
857 csio_dbg(hw
, "unblocking IOs on lnode: %p\n", lnode_list
[ii
]);
859 shost
= csio_ln_to_shost(ln
);
860 scsi_unblock_requests(shost
);
866 csio_lnodes_exit(struct csio_hw
*hw
, bool npiv
)
868 struct csio_lnode
*sln
;
869 struct csio_lnode
*ln
;
870 struct list_head
*cur_ln
, *cur_cln
;
871 struct csio_lnode
**lnode_list
;
874 lnode_list
= kzalloc((sizeof(struct csio_lnode
*) * hw
->num_lns
),
877 csio_err(hw
, "lnodes_exit: Failed to allocate lnodes_list.\n");
881 /* Get all child lnodes(NPIV ports) */
882 spin_lock_irq(&hw
->lock
);
883 list_for_each(cur_ln
, &hw
->sln_head
) {
884 sln
= (struct csio_lnode
*) cur_ln
;
886 /* Traverse children lnodes */
887 list_for_each(cur_cln
, &sln
->cln_head
)
888 lnode_list
[cur_cnt
++] = (struct csio_lnode
*) cur_cln
;
890 spin_unlock_irq(&hw
->lock
);
892 /* Delete NPIV lnodes */
893 for (ii
= 0; ii
< cur_cnt
; ii
++) {
894 csio_dbg(hw
, "Deleting child lnode: %p\n", lnode_list
[ii
]);
896 fc_vport_terminate(ln
->fc_vport
);
899 /* Delete only npiv lnodes */
904 /* Get all physical lnodes */
905 spin_lock_irq(&hw
->lock
);
906 /* Traverse sibling lnodes */
907 list_for_each(cur_ln
, &hw
->sln_head
) {
908 sln
= (struct csio_lnode
*) cur_ln
;
909 lnode_list
[cur_cnt
++] = sln
;
911 spin_unlock_irq(&hw
->lock
);
913 /* Delete physical lnodes */
914 for (ii
= 0; ii
< cur_cnt
; ii
++) {
915 csio_dbg(hw
, "Deleting parent lnode: %p\n", lnode_list
[ii
]);
916 csio_shost_exit(lnode_list
[ii
]);
924 * csio_lnode_init_post: Set lnode attributes after starting HW.
929 csio_lnode_init_post(struct csio_lnode
*ln
)
931 struct Scsi_Host
*shost
= csio_ln_to_shost(ln
);
933 csio_fchost_attr_init(ln
);
935 scsi_scan_host(shost
);
939 * csio_probe_one - Instantiate this function.
943 * This is the .probe() callback of the driver. This function:
944 * - Initializes the PCI function by enabling MMIO, setting bus
945 * mastership and setting DMA mask.
946 * - Allocates HW structure, DMA, memory resources, maps BARS to
947 * host memory and initializes HW module.
948 * - Allocates lnode structure via scsi_host_alloc, initializes
949 * shost, initialized lnode module and registers with SCSI ML
951 * - Enables interrupts, and starts the chip by kicking off the
953 * - Once hardware is ready, initiated scan of the host via
956 static int csio_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
962 struct csio_lnode
*ln
;
964 rv
= csio_pci_init(pdev
, &bars
);
968 hw
= csio_hw_alloc(pdev
);
974 pci_set_drvdata(pdev
, hw
);
976 if (csio_hw_start(hw
) != 0) {
978 "Failed to start FW, continuing in debug mode.\n");
982 sprintf(hw
->fwrev_str
, "%u.%u.%u.%u\n",
983 FW_HDR_FW_VER_MAJOR_GET(hw
->fwrev
),
984 FW_HDR_FW_VER_MINOR_GET(hw
->fwrev
),
985 FW_HDR_FW_VER_MICRO_GET(hw
->fwrev
),
986 FW_HDR_FW_VER_BUILD_GET(hw
->fwrev
));
988 for (i
= 0; i
< hw
->num_pports
; i
++) {
989 ln
= csio_shost_init(hw
, &pdev
->dev
, true, NULL
);
994 /* Initialize portid */
995 ln
->portid
= hw
->pport
[i
].portid
;
997 spin_lock_irq(&hw
->lock
);
998 if (csio_lnode_start(ln
) != 0)
1000 spin_unlock_irq(&hw
->lock
);
1005 csio_lnode_init_post(ln
);
1009 goto err_lnode_exit
;
1014 csio_lnodes_block_request(hw
);
1015 spin_lock_irq(&hw
->lock
);
1017 spin_unlock_irq(&hw
->lock
);
1018 csio_lnodes_unblock_request(hw
);
1019 pci_set_drvdata(hw
->pdev
, NULL
);
1020 csio_lnodes_exit(hw
, 0);
1023 csio_pci_exit(pdev
, &bars
);
1025 dev_err(&pdev
->dev
, "probe of device failed: %d\n", rv
);
1030 * csio_remove_one - Remove one instance of the driver at this PCI function.
1033 * Used during hotplug operation.
1035 static void csio_remove_one(struct pci_dev
*pdev
)
1037 struct csio_hw
*hw
= pci_get_drvdata(pdev
);
1038 int bars
= pci_select_bars(pdev
, IORESOURCE_MEM
);
1040 csio_lnodes_block_request(hw
);
1041 spin_lock_irq(&hw
->lock
);
1043 /* Stops lnode, Rnode s/m
1045 * All sessions with remote ports are unregistered.
1048 spin_unlock_irq(&hw
->lock
);
1049 csio_lnodes_unblock_request(hw
);
1051 csio_lnodes_exit(hw
, 0);
1053 pci_set_drvdata(pdev
, NULL
);
1054 csio_pci_exit(pdev
, &bars
);
1058 * csio_pci_error_detected - PCI error was detected
1062 static pci_ers_result_t
1063 csio_pci_error_detected(struct pci_dev
*pdev
, pci_channel_state_t state
)
1065 struct csio_hw
*hw
= pci_get_drvdata(pdev
);
1067 csio_lnodes_block_request(hw
);
1068 spin_lock_irq(&hw
->lock
);
1070 /* Post PCI error detected evt to HW s/m
1071 * HW s/m handles this evt by quiescing IOs, unregisters rports
1072 * and finally takes the device to offline.
1074 csio_post_event(&hw
->sm
, CSIO_HWE_PCIERR_DETECTED
);
1075 spin_unlock_irq(&hw
->lock
);
1076 csio_lnodes_unblock_request(hw
);
1077 csio_lnodes_exit(hw
, 0);
1078 csio_intr_disable(hw
, true);
1079 pci_disable_device(pdev
);
1080 return state
== pci_channel_io_perm_failure
?
1081 PCI_ERS_RESULT_DISCONNECT
: PCI_ERS_RESULT_NEED_RESET
;
1085 * csio_pci_slot_reset - PCI slot has been reset.
1089 static pci_ers_result_t
1090 csio_pci_slot_reset(struct pci_dev
*pdev
)
1092 struct csio_hw
*hw
= pci_get_drvdata(pdev
);
1095 if (pci_enable_device(pdev
)) {
1096 dev_err(&pdev
->dev
, "cannot re-enable device in slot reset\n");
1097 return PCI_ERS_RESULT_DISCONNECT
;
1100 pci_set_master(pdev
);
1101 pci_restore_state(pdev
);
1102 pci_save_state(pdev
);
1103 pci_cleanup_aer_uncorrect_error_status(pdev
);
1105 /* Bring HW s/m to ready state.
1106 * but don't resume IOs.
1108 spin_lock_irq(&hw
->lock
);
1109 csio_post_event(&hw
->sm
, CSIO_HWE_PCIERR_SLOT_RESET
);
1110 ready
= csio_is_hw_ready(hw
);
1111 spin_unlock_irq(&hw
->lock
);
1114 return PCI_ERS_RESULT_RECOVERED
;
1116 dev_err(&pdev
->dev
, "Can't initialize HW when in slot reset\n");
1117 return PCI_ERS_RESULT_DISCONNECT
;
1122 * csio_pci_resume - Resume normal operations
1127 csio_pci_resume(struct pci_dev
*pdev
)
1129 struct csio_hw
*hw
= pci_get_drvdata(pdev
);
1130 struct csio_lnode
*ln
;
1134 /* Bring the LINK UP and Resume IO */
1136 for (i
= 0; i
< hw
->num_pports
; i
++) {
1137 ln
= csio_shost_init(hw
, &pdev
->dev
, true, NULL
);
1142 /* Initialize portid */
1143 ln
->portid
= hw
->pport
[i
].portid
;
1145 spin_lock_irq(&hw
->lock
);
1146 if (csio_lnode_start(ln
) != 0)
1148 spin_unlock_irq(&hw
->lock
);
1153 csio_lnode_init_post(ln
);
1157 goto err_resume_exit
;
1162 csio_lnodes_block_request(hw
);
1163 spin_lock_irq(&hw
->lock
);
1165 spin_unlock_irq(&hw
->lock
);
1166 csio_lnodes_unblock_request(hw
);
1167 csio_lnodes_exit(hw
, 0);
1169 dev_err(&pdev
->dev
, "resume of device failed: %d\n", rv
);
1172 static struct pci_error_handlers csio_err_handler
= {
1173 .error_detected
= csio_pci_error_detected
,
1174 .slot_reset
= csio_pci_slot_reset
,
1175 .resume
= csio_pci_resume
,
1178 static DEFINE_PCI_DEVICE_TABLE(csio_pci_tbl
) = {
1179 CSIO_DEVICE(CSIO_DEVID_T440DBG_FCOE
, 0), /* T440DBG FCOE */
1180 CSIO_DEVICE(CSIO_DEVID_T420CR_FCOE
, 0), /* T420CR FCOE */
1181 CSIO_DEVICE(CSIO_DEVID_T422CR_FCOE
, 0), /* T422CR FCOE */
1182 CSIO_DEVICE(CSIO_DEVID_T440CR_FCOE
, 0), /* T440CR FCOE */
1183 CSIO_DEVICE(CSIO_DEVID_T420BCH_FCOE
, 0), /* T420BCH FCOE */
1184 CSIO_DEVICE(CSIO_DEVID_T440BCH_FCOE
, 0), /* T440BCH FCOE */
1185 CSIO_DEVICE(CSIO_DEVID_T440CH_FCOE
, 0), /* T440CH FCOE */
1186 CSIO_DEVICE(CSIO_DEVID_T420SO_FCOE
, 0), /* T420SO FCOE */
1187 CSIO_DEVICE(CSIO_DEVID_T420CX_FCOE
, 0), /* T420CX FCOE */
1188 CSIO_DEVICE(CSIO_DEVID_T420BT_FCOE
, 0), /* T420BT FCOE */
1189 CSIO_DEVICE(CSIO_DEVID_T404BT_FCOE
, 0), /* T404BT FCOE */
1190 CSIO_DEVICE(CSIO_DEVID_B420_FCOE
, 0), /* B420 FCOE */
1191 CSIO_DEVICE(CSIO_DEVID_B404_FCOE
, 0), /* B404 FCOE */
1192 CSIO_DEVICE(CSIO_DEVID_T480CR_FCOE
, 0), /* T480 CR FCOE */
1193 CSIO_DEVICE(CSIO_DEVID_T440LPCR_FCOE
, 0), /* T440 LP-CR FCOE */
1194 CSIO_DEVICE(CSIO_DEVID_PE10K
, 0), /* PE10K FCOE */
1195 CSIO_DEVICE(CSIO_DEVID_PE10K_PF1
, 0), /* PE10K FCOE on PF1 */
1196 { 0, 0, 0, 0, 0, 0, 0 }
1200 static struct pci_driver csio_pci_driver
= {
1201 .name
= KBUILD_MODNAME
,
1203 .owner
= THIS_MODULE
,
1205 .id_table
= csio_pci_tbl
,
1206 .probe
= csio_probe_one
,
1207 .remove
= csio_remove_one
,
1208 .err_handler
= &csio_err_handler
,
1212 * csio_init - Chelsio storage driver initialization function.
1220 pr_info("%s %s\n", CSIO_DRV_DESC
, CSIO_DRV_VERSION
);
1224 csio_fcoe_transport
= fc_attach_transport(&csio_fc_transport_funcs
);
1225 if (!csio_fcoe_transport
)
1228 csio_fcoe_transport_vport
=
1229 fc_attach_transport(&csio_fc_transport_vport_funcs
);
1230 if (!csio_fcoe_transport_vport
)
1233 rv
= pci_register_driver(&csio_pci_driver
);
1240 fc_release_transport(csio_fcoe_transport_vport
);
1242 fc_release_transport(csio_fcoe_transport
);
1249 * csio_exit - Chelsio storage driver uninitialization .
1251 * Function that gets called in the unload path.
1256 pci_unregister_driver(&csio_pci_driver
);
1258 fc_release_transport(csio_fcoe_transport_vport
);
1259 fc_release_transport(csio_fcoe_transport
);
1262 module_init(csio_init
);
1263 module_exit(csio_exit
);
1264 MODULE_AUTHOR(CSIO_DRV_AUTHOR
);
1265 MODULE_DESCRIPTION(CSIO_DRV_DESC
);
1266 MODULE_LICENSE(CSIO_DRV_LICENSE
);
1267 MODULE_DEVICE_TABLE(pci
, csio_pci_tbl
);
1268 MODULE_VERSION(CSIO_DRV_VERSION
);
1269 MODULE_FIRMWARE(CSIO_FW_FNAME
);