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_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
67 loff_t avail = file_inode(file)->i_size;
68 unsigned int mem = (uintptr_t)file->private_data & 3;
69 struct csio_hw *hw = file->private_data - mem;
75 if (count > avail - pos)
84 ret = csio_hw_mc_read(hw, pos, data, NULL);
86 ret = csio_hw_edc_read(hw, mem, pos, data, NULL);
90 ofst = pos % sizeof(data);
91 len = min(count, sizeof(data) - ofst);
92 if (copy_to_user(buf, (u8 *)data + ofst, len))
104 static const struct file_operations csio_mem_debugfs_fops = {
105 .owner = THIS_MODULE,
107 .read = csio_mem_read,
108 .llseek = default_llseek,
111 static void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
112 unsigned int idx, unsigned int size_mb)
116 de = debugfs_create_file(name, S_IRUSR, hw->debugfs_root,
117 (void *)hw + idx, &csio_mem_debugfs_fops);
118 if (de && de->d_inode)
119 de->d_inode->i_size = size_mb << 20;
122 static int csio_setup_debugfs(struct csio_hw *hw)
126 if (IS_ERR_OR_NULL(hw->debugfs_root))
129 i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE);
130 if (i & EDRAM0_ENABLE)
131 csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
132 if (i & EDRAM1_ENABLE)
133 csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
134 if (i & EXT_MEM_ENABLE)
135 csio_add_debugfs_mem(hw, "mc", MEM_MC,
136 EXT_MEM_SIZE_GET(csio_rd_reg32(hw, MA_EXT_MEMORY_BAR)));
141 * csio_dfs_create - Creates and sets up per-hw debugfs.
145 csio_dfs_create(struct csio_hw *hw)
147 if (csio_debugfs_root) {
148 hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
150 csio_setup_debugfs(hw);
157 * csio_dfs_destroy - Destroys per-hw debugfs.
160 csio_dfs_destroy(struct csio_hw *hw)
162 if (hw->debugfs_root)
163 debugfs_remove_recursive(hw->debugfs_root);
169 * csio_dfs_init - Debug filesystem initialization for the module.
175 csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
176 if (!csio_debugfs_root)
177 pr_warn("Could not create debugfs entry, continuing\n");
183 * csio_dfs_exit - debugfs cleanup for the module.
188 debugfs_remove(csio_debugfs_root);
192 * csio_pci_init - PCI initialization.
194 * @bars: Bitmask of bars to be requested.
196 * Initializes the PCI function by enabling MMIO, setting bus
197 * mastership and setting DMA mask.
200 csio_pci_init(struct pci_dev *pdev, int *bars)
204 *bars = pci_select_bars(pdev, IORESOURCE_MEM);
206 if (pci_enable_device_mem(pdev))
209 if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
210 goto err_disable_device;
212 pci_set_master(pdev);
213 pci_try_set_mwi(pdev);
215 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
216 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
217 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
218 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
220 dev_err(&pdev->dev, "No suitable DMA available.\n");
221 goto err_release_regions;
227 pci_release_selected_regions(pdev, *bars);
229 pci_disable_device(pdev);
236 * csio_pci_exit - PCI unitialization.
238 * @bars: Bars to be released.
242 csio_pci_exit(struct pci_dev *pdev, int *bars)
244 pci_release_selected_regions(pdev, *bars);
245 pci_disable_device(pdev);
249 * csio_hw_init_workers - Initialize the HW module's worker threads.
254 csio_hw_init_workers(struct csio_hw *hw)
256 INIT_WORK(&hw->evtq_work, csio_evtq_worker);
260 csio_hw_exit_workers(struct csio_hw *hw)
262 cancel_work_sync(&hw->evtq_work);
263 flush_scheduled_work();
267 csio_create_queues(struct csio_hw *hw)
270 struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
272 struct csio_scsi_cpu_info *info;
274 if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
277 if (hw->intr_mode != CSIO_IM_MSIX) {
278 rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
279 0, hw->pport[0].portid, false, NULL);
281 csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
287 rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
288 csio_get_fwevt_intr_idx(hw),
289 hw->pport[0].portid, true, NULL);
291 csio_err(hw, "FW event IQ config failed!: %d\n", rv);
295 /* Create mgmt queue */
296 rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
297 mgmtm->iq_idx, hw->pport[0].portid, NULL);
300 csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
304 /* Create SCSI queues */
305 for (i = 0; i < hw->num_pports; i++) {
306 info = &hw->scsi_cpu_info[i];
308 for (j = 0; j < info->max_cpus; j++) {
309 struct csio_scsi_qset *sqset = &hw->sqset[i][j];
311 rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
312 sqset->intr_idx, i, false, NULL);
315 "SCSI module IQ config failed [%d][%d]:%d\n",
319 rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
320 sqset->iq_idx, i, NULL);
323 "SCSI module EQ config failed [%d][%d]:%d\n",
328 } /* For all ports */
330 hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
333 csio_wr_destroy_queues(hw, true);
338 * csio_config_queues - Configure the DMA queues.
341 * Allocates memory for queues are registers them with FW.
344 csio_config_queues(struct csio_hw *hw)
346 int i, j, idx, k = 0;
348 struct csio_scsi_qset *sqset;
349 struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
350 struct csio_scsi_qset *orig;
351 struct csio_scsi_cpu_info *info;
353 if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
354 return csio_create_queues(hw);
356 /* Calculate number of SCSI queues for MSIX we would like */
357 hw->num_scsi_msix_cpus = num_online_cpus();
358 hw->num_sqsets = num_online_cpus() * hw->num_pports;
360 if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
361 hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
362 hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
365 /* Initialize max_cpus, may get reduced during msix allocations */
366 for (i = 0; i < hw->num_pports; i++)
367 hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
369 csio_dbg(hw, "nsqsets:%d scpus:%d\n",
370 hw->num_sqsets, hw->num_scsi_msix_cpus);
372 csio_intr_enable(hw);
374 if (hw->intr_mode != CSIO_IM_MSIX) {
376 /* Allocate Forward interrupt iq. */
377 hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
378 CSIO_INTR_WRSIZE, CSIO_INGRESS,
379 (void *)hw, 0, 0, NULL);
380 if (hw->intr_iq_idx == -1) {
382 "Forward interrupt queue creation failed\n");
387 /* Allocate the FW evt queue */
388 hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
390 CSIO_INGRESS, (void *)hw,
391 CSIO_FWEVT_FLBUFS, 0,
392 csio_fwevt_intx_handler);
393 if (hw->fwevt_iq_idx == -1) {
394 csio_err(hw, "FW evt queue creation failed\n");
398 /* Allocate the mgmt queue */
399 mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
401 CSIO_EGRESS, (void *)hw, 0, 0, NULL);
402 if (mgmtm->eq_idx == -1) {
403 csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
407 /* Use FW IQ for MGMT req completion */
408 mgmtm->iq_idx = hw->fwevt_iq_idx;
410 /* Allocate SCSI queues */
411 for (i = 0; i < hw->num_pports; i++) {
412 info = &hw->scsi_cpu_info[i];
414 for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
415 sqset = &hw->sqset[i][j];
417 if (j >= info->max_cpus) {
418 k = j % info->max_cpus;
419 orig = &hw->sqset[i][k];
420 sqset->eq_idx = orig->eq_idx;
421 sqset->iq_idx = orig->iq_idx;
425 idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
426 CSIO_EGRESS, (void *)hw, 0, 0,
429 csio_err(hw, "EQ creation failed for idx:%d\n",
436 idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
437 CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
439 csio_scsi_intx_handler);
441 csio_err(hw, "IQ creation failed for idx:%d\n",
447 } /* For all ports */
449 hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
451 rv = csio_create_queues(hw);
456 * Now request IRQs for the vectors. In the event of a failure,
457 * cleanup is handled internally by this function.
459 rv = csio_request_irqs(hw);
466 csio_intr_disable(hw, false);
472 csio_resource_alloc(struct csio_hw *hw)
474 struct csio_wrm *wrm = csio_hw_to_wrm(hw);
477 wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
478 CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
480 hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
481 sizeof(struct csio_mb));
485 hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
486 sizeof(struct csio_rnode));
487 if (!hw->rnode_mempool)
488 goto err_free_mb_mempool;
490 hw->scsi_pci_pool = pci_pool_create("csio_scsi_pci_pool", hw->pdev,
491 CSIO_SCSI_RSP_LEN, 8, 0);
492 if (!hw->scsi_pci_pool)
493 goto err_free_rn_pool;
498 mempool_destroy(hw->rnode_mempool);
499 hw->rnode_mempool = NULL;
501 mempool_destroy(hw->mb_mempool);
502 hw->mb_mempool = NULL;
508 csio_resource_free(struct csio_hw *hw)
510 pci_pool_destroy(hw->scsi_pci_pool);
511 hw->scsi_pci_pool = NULL;
512 mempool_destroy(hw->rnode_mempool);
513 hw->rnode_mempool = NULL;
514 mempool_destroy(hw->mb_mempool);
515 hw->mb_mempool = NULL;
519 * csio_hw_alloc - Allocate and initialize the HW module.
522 * Allocates HW structure, DMA, memory resources, maps BARS to
523 * host memory and initializes HW module.
525 static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
529 hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
534 strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
536 /* memory pool/DMA pool allocation */
537 if (csio_resource_alloc(hw))
540 /* Get the start address of registers from BAR 0 */
541 hw->regstart = ioremap_nocache(pci_resource_start(pdev, 0),
542 pci_resource_len(pdev, 0));
544 csio_err(hw, "Could not map BAR 0, regstart = %p\n",
546 goto err_resource_free;
549 csio_hw_init_workers(hw);
551 if (csio_hw_init(hw))
556 csio_dbg(hw, "hw:%p\n", hw);
561 csio_hw_exit_workers(hw);
562 iounmap(hw->regstart);
564 csio_resource_free(hw);
572 * csio_hw_free - Uninitialize and free the HW module.
575 * Disable interrupts, uninit the HW module, free resources, free hw.
578 csio_hw_free(struct csio_hw *hw)
580 csio_intr_disable(hw, true);
581 csio_hw_exit_workers(hw);
583 iounmap(hw->regstart);
584 csio_dfs_destroy(hw);
585 csio_resource_free(hw);
590 * csio_shost_init - Create and initialize the lnode module.
591 * @hw: The HW module.
592 * @dev: The device associated with this invocation.
593 * @probe: Called from probe context or not?
594 * @os_pln: Parent lnode if any.
596 * Allocates lnode structure via scsi_host_alloc, initializes
597 * shost, initializes lnode module and registers with SCSI ML
598 * via scsi_host_add. This function is shared between physical and
599 * virtual node ports.
602 csio_shost_init(struct csio_hw *hw, struct device *dev,
603 bool probe, struct csio_lnode *pln)
605 struct Scsi_Host *shost = NULL;
606 struct csio_lnode *ln;
608 csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
609 csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
612 * hw->pdev is the physical port's PCI dev structure,
613 * which will be different from the NPIV dev structure.
615 if (dev == &hw->pdev->dev)
616 shost = scsi_host_alloc(
617 &csio_fcoe_shost_template,
618 sizeof(struct csio_lnode));
620 shost = scsi_host_alloc(
621 &csio_fcoe_shost_vport_template,
622 sizeof(struct csio_lnode));
627 ln = shost_priv(shost);
628 memset(ln, 0, sizeof(struct csio_lnode));
630 /* Link common lnode to this lnode */
631 ln->dev_num = (shost->host_no << 16);
633 shost->can_queue = CSIO_MAX_QUEUE;
635 shost->unique_id = shost->host_no;
636 shost->max_cmd_len = 16; /* Max CDB length supported */
637 shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
638 hw->fres_info.max_ssns);
639 shost->max_lun = CSIO_MAX_LUN;
640 if (dev == &hw->pdev->dev)
641 shost->transportt = csio_fcoe_transport;
643 shost->transportt = csio_fcoe_transport_vport;
649 /* Other initialization here: Common, Transport specific */
650 if (csio_lnode_init(ln, hw, pln))
653 if (scsi_add_host(shost, dev))
661 scsi_host_put(shost);
667 * csio_shost_exit - De-instantiate the shost.
668 * @ln: The lnode module corresponding to the shost.
672 csio_shost_exit(struct csio_lnode *ln)
674 struct Scsi_Host *shost = csio_ln_to_shost(ln);
675 struct csio_hw *hw = csio_lnode_to_hw(ln);
677 /* Inform transport */
678 fc_remove_host(shost);
681 scsi_remove_host(shost);
683 /* Flush all the events, so that any rnode removal events
684 * already queued are all handled, before we remove the lnode.
686 spin_lock_irq(&hw->lock);
688 spin_unlock_irq(&hw->lock);
691 scsi_host_put(shost);
695 csio_lnode_alloc(struct csio_hw *hw)
697 return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
701 csio_lnodes_block_request(struct csio_hw *hw)
703 struct Scsi_Host *shost;
704 struct csio_lnode *sln;
705 struct csio_lnode *ln;
706 struct list_head *cur_ln, *cur_cln;
707 struct csio_lnode **lnode_list;
710 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
713 csio_err(hw, "Failed to allocate lnodes_list");
717 spin_lock_irq(&hw->lock);
718 /* Traverse sibling lnodes */
719 list_for_each(cur_ln, &hw->sln_head) {
720 sln = (struct csio_lnode *) cur_ln;
721 lnode_list[cur_cnt++] = sln;
723 /* Traverse children lnodes */
724 list_for_each(cur_cln, &sln->cln_head)
725 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
727 spin_unlock_irq(&hw->lock);
729 for (ii = 0; ii < cur_cnt; ii++) {
730 csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
732 shost = csio_ln_to_shost(ln);
733 scsi_block_requests(shost);
740 csio_lnodes_unblock_request(struct csio_hw *hw)
742 struct csio_lnode *ln;
743 struct Scsi_Host *shost;
744 struct csio_lnode *sln;
745 struct list_head *cur_ln, *cur_cln;
746 struct csio_lnode **lnode_list;
749 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
752 csio_err(hw, "Failed to allocate lnodes_list");
756 spin_lock_irq(&hw->lock);
757 /* Traverse sibling lnodes */
758 list_for_each(cur_ln, &hw->sln_head) {
759 sln = (struct csio_lnode *) cur_ln;
760 lnode_list[cur_cnt++] = sln;
762 /* Traverse children lnodes */
763 list_for_each(cur_cln, &sln->cln_head)
764 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
766 spin_unlock_irq(&hw->lock);
768 for (ii = 0; ii < cur_cnt; ii++) {
769 csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
771 shost = csio_ln_to_shost(ln);
772 scsi_unblock_requests(shost);
778 csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
780 struct csio_lnode *ln;
781 struct Scsi_Host *shost;
782 struct csio_lnode *sln;
783 struct list_head *cur_ln, *cur_cln;
784 struct csio_lnode **lnode_list;
787 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
790 csio_err(hw, "Failed to allocate lnodes_list");
794 spin_lock_irq(&hw->lock);
795 /* Traverse sibling lnodes */
796 list_for_each(cur_ln, &hw->sln_head) {
797 sln = (struct csio_lnode *) cur_ln;
798 if (sln->portid != portid)
801 lnode_list[cur_cnt++] = sln;
803 /* Traverse children lnodes */
804 list_for_each(cur_cln, &sln->cln_head)
805 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
807 spin_unlock_irq(&hw->lock);
809 for (ii = 0; ii < cur_cnt; ii++) {
810 csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
812 shost = csio_ln_to_shost(ln);
813 scsi_block_requests(shost);
819 csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
821 struct csio_lnode *ln;
822 struct Scsi_Host *shost;
823 struct csio_lnode *sln;
824 struct list_head *cur_ln, *cur_cln;
825 struct csio_lnode **lnode_list;
828 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
831 csio_err(hw, "Failed to allocate lnodes_list");
835 spin_lock_irq(&hw->lock);
836 /* Traverse sibling lnodes */
837 list_for_each(cur_ln, &hw->sln_head) {
838 sln = (struct csio_lnode *) cur_ln;
839 if (sln->portid != portid)
841 lnode_list[cur_cnt++] = sln;
843 /* Traverse children lnodes */
844 list_for_each(cur_cln, &sln->cln_head)
845 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
847 spin_unlock_irq(&hw->lock);
849 for (ii = 0; ii < cur_cnt; ii++) {
850 csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
852 shost = csio_ln_to_shost(ln);
853 scsi_unblock_requests(shost);
859 csio_lnodes_exit(struct csio_hw *hw, bool npiv)
861 struct csio_lnode *sln;
862 struct csio_lnode *ln;
863 struct list_head *cur_ln, *cur_cln;
864 struct csio_lnode **lnode_list;
867 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
870 csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
874 /* Get all child lnodes(NPIV ports) */
875 spin_lock_irq(&hw->lock);
876 list_for_each(cur_ln, &hw->sln_head) {
877 sln = (struct csio_lnode *) cur_ln;
879 /* Traverse children lnodes */
880 list_for_each(cur_cln, &sln->cln_head)
881 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
883 spin_unlock_irq(&hw->lock);
885 /* Delete NPIV lnodes */
886 for (ii = 0; ii < cur_cnt; ii++) {
887 csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
889 fc_vport_terminate(ln->fc_vport);
892 /* Delete only npiv lnodes */
897 /* Get all physical lnodes */
898 spin_lock_irq(&hw->lock);
899 /* Traverse sibling lnodes */
900 list_for_each(cur_ln, &hw->sln_head) {
901 sln = (struct csio_lnode *) cur_ln;
902 lnode_list[cur_cnt++] = sln;
904 spin_unlock_irq(&hw->lock);
906 /* Delete physical lnodes */
907 for (ii = 0; ii < cur_cnt; ii++) {
908 csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
909 csio_shost_exit(lnode_list[ii]);
917 * csio_lnode_init_post: Set lnode attributes after starting HW.
922 csio_lnode_init_post(struct csio_lnode *ln)
924 struct Scsi_Host *shost = csio_ln_to_shost(ln);
926 csio_fchost_attr_init(ln);
928 scsi_scan_host(shost);
932 * csio_probe_one - Instantiate this function.
936 * This is the .probe() callback of the driver. This function:
937 * - Initializes the PCI function by enabling MMIO, setting bus
938 * mastership and setting DMA mask.
939 * - Allocates HW structure, DMA, memory resources, maps BARS to
940 * host memory and initializes HW module.
941 * - Allocates lnode structure via scsi_host_alloc, initializes
942 * shost, initialized lnode module and registers with SCSI ML
944 * - Enables interrupts, and starts the chip by kicking off the
946 * - Once hardware is ready, initiated scan of the host via
949 static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
955 struct csio_lnode *ln;
957 rv = csio_pci_init(pdev, &bars);
961 hw = csio_hw_alloc(pdev);
967 pci_set_drvdata(pdev, hw);
969 if (csio_hw_start(hw) != 0) {
971 "Failed to start FW, continuing in debug mode.\n");
975 sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
976 FW_HDR_FW_VER_MAJOR_GET(hw->fwrev),
977 FW_HDR_FW_VER_MINOR_GET(hw->fwrev),
978 FW_HDR_FW_VER_MICRO_GET(hw->fwrev),
979 FW_HDR_FW_VER_BUILD_GET(hw->fwrev));
981 for (i = 0; i < hw->num_pports; i++) {
982 ln = csio_shost_init(hw, &pdev->dev, true, NULL);
987 /* Initialize portid */
988 ln->portid = hw->pport[i].portid;
990 spin_lock_irq(&hw->lock);
991 if (csio_lnode_start(ln) != 0)
993 spin_unlock_irq(&hw->lock);
998 csio_lnode_init_post(ln);
1002 goto err_lnode_exit;
1007 csio_lnodes_block_request(hw);
1008 spin_lock_irq(&hw->lock);
1010 spin_unlock_irq(&hw->lock);
1011 csio_lnodes_unblock_request(hw);
1012 pci_set_drvdata(hw->pdev, NULL);
1013 csio_lnodes_exit(hw, 0);
1016 csio_pci_exit(pdev, &bars);
1018 dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
1023 * csio_remove_one - Remove one instance of the driver at this PCI function.
1026 * Used during hotplug operation.
1028 static void csio_remove_one(struct pci_dev *pdev)
1030 struct csio_hw *hw = pci_get_drvdata(pdev);
1031 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1033 csio_lnodes_block_request(hw);
1034 spin_lock_irq(&hw->lock);
1036 /* Stops lnode, Rnode s/m
1038 * All sessions with remote ports are unregistered.
1041 spin_unlock_irq(&hw->lock);
1042 csio_lnodes_unblock_request(hw);
1044 csio_lnodes_exit(hw, 0);
1046 pci_set_drvdata(pdev, NULL);
1047 csio_pci_exit(pdev, &bars);
1051 * csio_pci_error_detected - PCI error was detected
1055 static pci_ers_result_t
1056 csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1058 struct csio_hw *hw = pci_get_drvdata(pdev);
1060 csio_lnodes_block_request(hw);
1061 spin_lock_irq(&hw->lock);
1063 /* Post PCI error detected evt to HW s/m
1064 * HW s/m handles this evt by quiescing IOs, unregisters rports
1065 * and finally takes the device to offline.
1067 csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
1068 spin_unlock_irq(&hw->lock);
1069 csio_lnodes_unblock_request(hw);
1070 csio_lnodes_exit(hw, 0);
1071 csio_intr_disable(hw, true);
1072 pci_disable_device(pdev);
1073 return state == pci_channel_io_perm_failure ?
1074 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1078 * csio_pci_slot_reset - PCI slot has been reset.
1082 static pci_ers_result_t
1083 csio_pci_slot_reset(struct pci_dev *pdev)
1085 struct csio_hw *hw = pci_get_drvdata(pdev);
1088 if (pci_enable_device(pdev)) {
1089 dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
1090 return PCI_ERS_RESULT_DISCONNECT;
1093 pci_set_master(pdev);
1094 pci_restore_state(pdev);
1095 pci_save_state(pdev);
1096 pci_cleanup_aer_uncorrect_error_status(pdev);
1098 /* Bring HW s/m to ready state.
1099 * but don't resume IOs.
1101 spin_lock_irq(&hw->lock);
1102 csio_post_event(&hw->sm, CSIO_HWE_PCIERR_SLOT_RESET);
1103 ready = csio_is_hw_ready(hw);
1104 spin_unlock_irq(&hw->lock);
1107 return PCI_ERS_RESULT_RECOVERED;
1109 dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
1110 return PCI_ERS_RESULT_DISCONNECT;
1115 * csio_pci_resume - Resume normal operations
1120 csio_pci_resume(struct pci_dev *pdev)
1122 struct csio_hw *hw = pci_get_drvdata(pdev);
1123 struct csio_lnode *ln;
1127 /* Bring the LINK UP and Resume IO */
1129 for (i = 0; i < hw->num_pports; i++) {
1130 ln = csio_shost_init(hw, &pdev->dev, true, NULL);
1135 /* Initialize portid */
1136 ln->portid = hw->pport[i].portid;
1138 spin_lock_irq(&hw->lock);
1139 if (csio_lnode_start(ln) != 0)
1141 spin_unlock_irq(&hw->lock);
1146 csio_lnode_init_post(ln);
1150 goto err_resume_exit;
1155 csio_lnodes_block_request(hw);
1156 spin_lock_irq(&hw->lock);
1158 spin_unlock_irq(&hw->lock);
1159 csio_lnodes_unblock_request(hw);
1160 csio_lnodes_exit(hw, 0);
1162 dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
1165 static struct pci_error_handlers csio_err_handler = {
1166 .error_detected = csio_pci_error_detected,
1167 .slot_reset = csio_pci_slot_reset,
1168 .resume = csio_pci_resume,
1171 static DEFINE_PCI_DEVICE_TABLE(csio_pci_tbl) = {
1172 CSIO_DEVICE(CSIO_DEVID_T440DBG_FCOE, 0), /* T440DBG FCOE */
1173 CSIO_DEVICE(CSIO_DEVID_T420CR_FCOE, 0), /* T420CR FCOE */
1174 CSIO_DEVICE(CSIO_DEVID_T422CR_FCOE, 0), /* T422CR FCOE */
1175 CSIO_DEVICE(CSIO_DEVID_T440CR_FCOE, 0), /* T440CR FCOE */
1176 CSIO_DEVICE(CSIO_DEVID_T420BCH_FCOE, 0), /* T420BCH FCOE */
1177 CSIO_DEVICE(CSIO_DEVID_T440BCH_FCOE, 0), /* T440BCH FCOE */
1178 CSIO_DEVICE(CSIO_DEVID_T440CH_FCOE, 0), /* T440CH FCOE */
1179 CSIO_DEVICE(CSIO_DEVID_T420SO_FCOE, 0), /* T420SO FCOE */
1180 CSIO_DEVICE(CSIO_DEVID_T420CX_FCOE, 0), /* T420CX FCOE */
1181 CSIO_DEVICE(CSIO_DEVID_T420BT_FCOE, 0), /* T420BT FCOE */
1182 CSIO_DEVICE(CSIO_DEVID_T404BT_FCOE, 0), /* T404BT FCOE */
1183 CSIO_DEVICE(CSIO_DEVID_B420_FCOE, 0), /* B420 FCOE */
1184 CSIO_DEVICE(CSIO_DEVID_B404_FCOE, 0), /* B404 FCOE */
1185 CSIO_DEVICE(CSIO_DEVID_T480CR_FCOE, 0), /* T480 CR FCOE */
1186 CSIO_DEVICE(CSIO_DEVID_T440LPCR_FCOE, 0), /* T440 LP-CR FCOE */
1187 CSIO_DEVICE(CSIO_DEVID_PE10K, 0), /* PE10K FCOE */
1188 CSIO_DEVICE(CSIO_DEVID_PE10K_PF1, 0), /* PE10K FCOE on PF1 */
1189 { 0, 0, 0, 0, 0, 0, 0 }
1193 static struct pci_driver csio_pci_driver = {
1194 .name = KBUILD_MODNAME,
1196 .owner = THIS_MODULE,
1198 .id_table = csio_pci_tbl,
1199 .probe = csio_probe_one,
1200 .remove = csio_remove_one,
1201 .err_handler = &csio_err_handler,
1205 * csio_init - Chelsio storage driver initialization function.
1213 pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
1217 csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
1218 if (!csio_fcoe_transport)
1221 csio_fcoe_transport_vport =
1222 fc_attach_transport(&csio_fc_transport_vport_funcs);
1223 if (!csio_fcoe_transport_vport)
1226 rv = pci_register_driver(&csio_pci_driver);
1233 fc_release_transport(csio_fcoe_transport_vport);
1235 fc_release_transport(csio_fcoe_transport);
1242 * csio_exit - Chelsio storage driver uninitialization .
1244 * Function that gets called in the unload path.
1249 pci_unregister_driver(&csio_pci_driver);
1251 fc_release_transport(csio_fcoe_transport_vport);
1252 fc_release_transport(csio_fcoe_transport);
1255 module_init(csio_init);
1256 module_exit(csio_exit);
1257 MODULE_AUTHOR(CSIO_DRV_AUTHOR);
1258 MODULE_DESCRIPTION(CSIO_DRV_DESC);
1259 MODULE_LICENSE(CSIO_DRV_LICENSE);
1260 MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
1261 MODULE_VERSION(CSIO_DRV_VERSION);
1262 MODULE_FIRMWARE(CSIO_FW_FNAME);