drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / scsi / csiostor / csio_init.c
blob9a3f2ed050bd8e059be06ddc5f35904590ced39b
1 /*
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
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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
32 * SOFTWARE.
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/mm.h>
42 #include <linux/notifier.h>
43 #include <linux/kdebug.h>
44 #include <linux/seq_file.h>
45 #include <linux/debugfs.h>
46 #include <linux/string.h>
47 #include <linux/export.h>
49 #include "csio_init.h"
50 #include "csio_defs.h"
52 #define CSIO_MIN_MEMPOOL_SZ 64
54 static struct dentry *csio_debugfs_root;
56 static struct scsi_transport_template *csio_fcoe_transport;
57 static struct scsi_transport_template *csio_fcoe_transport_vport;
60 * debugfs support
62 static ssize_t
63 csio_mem_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
65 loff_t pos = *ppos;
66 loff_t avail = file_inode(file)->i_size;
67 unsigned int mem = (uintptr_t)file->private_data & 3;
68 struct csio_hw *hw = file->private_data - mem;
70 if (pos < 0)
71 return -EINVAL;
72 if (pos >= avail)
73 return 0;
74 if (count > avail - pos)
75 count = avail - pos;
77 while (count) {
78 size_t len;
79 int ret, ofst;
80 __be32 data[16];
82 if (mem == MEM_MC)
83 ret = hw->chip_ops->chip_mc_read(hw, 0, pos,
84 data, NULL);
85 else
86 ret = hw->chip_ops->chip_edc_read(hw, mem, pos,
87 data, NULL);
88 if (ret)
89 return ret;
91 ofst = pos % sizeof(data);
92 len = min(count, sizeof(data) - ofst);
93 if (copy_to_user(buf, (u8 *)data + ofst, len))
94 return -EFAULT;
96 buf += len;
97 pos += len;
98 count -= len;
100 count = pos - *ppos;
101 *ppos = pos;
102 return count;
105 static const struct file_operations csio_mem_debugfs_fops = {
106 .owner = THIS_MODULE,
107 .open = simple_open,
108 .read = csio_mem_read,
109 .llseek = default_llseek,
112 void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
113 unsigned int idx, unsigned int size_mb)
115 debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
116 (void *)hw + idx, &csio_mem_debugfs_fops,
117 size_mb << 20);
120 static int csio_setup_debugfs(struct csio_hw *hw)
122 int i;
124 if (IS_ERR_OR_NULL(hw->debugfs_root))
125 return -1;
127 i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE_A);
128 if (i & EDRAM0_ENABLE_F)
129 csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
130 if (i & EDRAM1_ENABLE_F)
131 csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
133 hw->chip_ops->chip_dfs_create_ext_mem(hw);
134 return 0;
138 * csio_dfs_create - Creates and sets up per-hw debugfs.
141 static int
142 csio_dfs_create(struct csio_hw *hw)
144 if (csio_debugfs_root) {
145 hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
146 csio_debugfs_root);
147 csio_setup_debugfs(hw);
150 return 0;
154 * csio_dfs_destroy - Destroys per-hw debugfs.
156 static void
157 csio_dfs_destroy(struct csio_hw *hw)
159 debugfs_remove_recursive(hw->debugfs_root);
163 * csio_dfs_init - Debug filesystem initialization for the module.
166 static void
167 csio_dfs_init(void)
169 csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
173 * csio_dfs_exit - debugfs cleanup for the module.
175 static void
176 csio_dfs_exit(void)
178 debugfs_remove(csio_debugfs_root);
182 * csio_pci_init - PCI initialization.
183 * @pdev: PCI device.
184 * @bars: Bitmask of bars to be requested.
186 * Initializes the PCI function by enabling MMIO, setting bus
187 * mastership and setting DMA mask.
189 static int
190 csio_pci_init(struct pci_dev *pdev, int *bars)
192 int rv = -ENODEV;
194 *bars = pci_select_bars(pdev, IORESOURCE_MEM);
196 if (pci_enable_device_mem(pdev))
197 goto err;
199 if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
200 goto err_disable_device;
202 pci_set_master(pdev);
203 pci_try_set_mwi(pdev);
205 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
206 if (rv)
207 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
208 if (rv) {
209 rv = -ENODEV;
210 dev_err(&pdev->dev, "No suitable DMA available.\n");
211 goto err_release_regions;
214 return 0;
216 err_release_regions:
217 pci_release_selected_regions(pdev, *bars);
218 err_disable_device:
219 pci_disable_device(pdev);
220 err:
221 return rv;
226 * csio_pci_exit - PCI unitialization.
227 * @pdev: PCI device.
228 * @bars: Bars to be released.
231 static void
232 csio_pci_exit(struct pci_dev *pdev, int *bars)
234 pci_release_selected_regions(pdev, *bars);
235 pci_disable_device(pdev);
239 * csio_hw_init_workers - Initialize the HW module's worker threads.
240 * @hw: HW module.
243 static void
244 csio_hw_init_workers(struct csio_hw *hw)
246 INIT_WORK(&hw->evtq_work, csio_evtq_worker);
249 static void
250 csio_hw_exit_workers(struct csio_hw *hw)
252 cancel_work_sync(&hw->evtq_work);
255 static int
256 csio_create_queues(struct csio_hw *hw)
258 int i, j;
259 struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
260 int rv;
261 struct csio_scsi_cpu_info *info;
263 if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
264 return 0;
266 if (hw->intr_mode != CSIO_IM_MSIX) {
267 rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
268 0, hw->pport[0].portid, false, NULL);
269 if (rv != 0) {
270 csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
271 return rv;
275 /* FW event queue */
276 rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
277 csio_get_fwevt_intr_idx(hw),
278 hw->pport[0].portid, true, NULL);
279 if (rv != 0) {
280 csio_err(hw, "FW event IQ config failed!: %d\n", rv);
281 return rv;
284 /* Create mgmt queue */
285 rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
286 mgmtm->iq_idx, hw->pport[0].portid, NULL);
288 if (rv != 0) {
289 csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
290 goto err;
293 /* Create SCSI queues */
294 for (i = 0; i < hw->num_pports; i++) {
295 info = &hw->scsi_cpu_info[i];
297 for (j = 0; j < info->max_cpus; j++) {
298 struct csio_scsi_qset *sqset = &hw->sqset[i][j];
300 rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
301 sqset->intr_idx, i, false, NULL);
302 if (rv != 0) {
303 csio_err(hw,
304 "SCSI module IQ config failed [%d][%d]:%d\n",
305 i, j, rv);
306 goto err;
308 rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
309 sqset->iq_idx, i, NULL);
310 if (rv != 0) {
311 csio_err(hw,
312 "SCSI module EQ config failed [%d][%d]:%d\n",
313 i, j, rv);
314 goto err;
316 } /* for all CPUs */
317 } /* For all ports */
319 hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
320 return 0;
321 err:
322 csio_wr_destroy_queues(hw, true);
323 return -EINVAL;
327 * csio_config_queues - Configure the DMA queues.
328 * @hw: HW module.
330 * Allocates memory for queues are registers them with FW.
333 csio_config_queues(struct csio_hw *hw)
335 int i, j, idx, k = 0;
336 int rv;
337 struct csio_scsi_qset *sqset;
338 struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
339 struct csio_scsi_qset *orig;
340 struct csio_scsi_cpu_info *info;
342 if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
343 return csio_create_queues(hw);
345 /* Calculate number of SCSI queues for MSIX we would like */
346 hw->num_scsi_msix_cpus = num_online_cpus();
347 hw->num_sqsets = num_online_cpus() * hw->num_pports;
349 if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
350 hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
351 hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
354 /* Initialize max_cpus, may get reduced during msix allocations */
355 for (i = 0; i < hw->num_pports; i++)
356 hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
358 csio_dbg(hw, "nsqsets:%d scpus:%d\n",
359 hw->num_sqsets, hw->num_scsi_msix_cpus);
361 csio_intr_enable(hw);
363 if (hw->intr_mode != CSIO_IM_MSIX) {
365 /* Allocate Forward interrupt iq. */
366 hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
367 CSIO_INTR_WRSIZE, CSIO_INGRESS,
368 (void *)hw, 0, 0, NULL);
369 if (hw->intr_iq_idx == -1) {
370 csio_err(hw,
371 "Forward interrupt queue creation failed\n");
372 goto intr_disable;
376 /* Allocate the FW evt queue */
377 hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
378 CSIO_FWEVT_WRSIZE,
379 CSIO_INGRESS, (void *)hw,
380 CSIO_FWEVT_FLBUFS, 0,
381 csio_fwevt_intx_handler);
382 if (hw->fwevt_iq_idx == -1) {
383 csio_err(hw, "FW evt queue creation failed\n");
384 goto intr_disable;
387 /* Allocate the mgmt queue */
388 mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
389 CSIO_MGMT_EQ_WRSIZE,
390 CSIO_EGRESS, (void *)hw, 0, 0, NULL);
391 if (mgmtm->eq_idx == -1) {
392 csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
393 goto intr_disable;
396 /* Use FW IQ for MGMT req completion */
397 mgmtm->iq_idx = hw->fwevt_iq_idx;
399 /* Allocate SCSI queues */
400 for (i = 0; i < hw->num_pports; i++) {
401 info = &hw->scsi_cpu_info[i];
403 for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
404 sqset = &hw->sqset[i][j];
406 if (j >= info->max_cpus) {
407 k = j % info->max_cpus;
408 orig = &hw->sqset[i][k];
409 sqset->eq_idx = orig->eq_idx;
410 sqset->iq_idx = orig->iq_idx;
411 continue;
414 idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
415 CSIO_EGRESS, (void *)hw, 0, 0,
416 NULL);
417 if (idx == -1) {
418 csio_err(hw, "EQ creation failed for idx:%d\n",
419 idx);
420 goto intr_disable;
423 sqset->eq_idx = idx;
425 idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
426 CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
427 (void *)hw, 0, 0,
428 csio_scsi_intx_handler);
429 if (idx == -1) {
430 csio_err(hw, "IQ creation failed for idx:%d\n",
431 idx);
432 goto intr_disable;
434 sqset->iq_idx = idx;
435 } /* for all CPUs */
436 } /* For all ports */
438 hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
440 rv = csio_create_queues(hw);
441 if (rv != 0)
442 goto intr_disable;
445 * Now request IRQs for the vectors. In the event of a failure,
446 * cleanup is handled internally by this function.
448 rv = csio_request_irqs(hw);
449 if (rv != 0)
450 return -EINVAL;
452 return 0;
454 intr_disable:
455 csio_intr_disable(hw, false);
457 return -EINVAL;
460 static int
461 csio_resource_alloc(struct csio_hw *hw)
463 struct csio_wrm *wrm = csio_hw_to_wrm(hw);
464 int rv = -ENOMEM;
466 wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
467 CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
469 hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
470 sizeof(struct csio_mb));
471 if (!hw->mb_mempool)
472 goto err;
474 hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
475 sizeof(struct csio_rnode));
476 if (!hw->rnode_mempool)
477 goto err_free_mb_mempool;
479 hw->scsi_dma_pool = dma_pool_create("csio_scsi_dma_pool",
480 &hw->pdev->dev, CSIO_SCSI_RSP_LEN,
481 8, 0);
482 if (!hw->scsi_dma_pool)
483 goto err_free_rn_pool;
485 return 0;
487 err_free_rn_pool:
488 mempool_destroy(hw->rnode_mempool);
489 hw->rnode_mempool = NULL;
490 err_free_mb_mempool:
491 mempool_destroy(hw->mb_mempool);
492 hw->mb_mempool = NULL;
493 err:
494 return rv;
497 static void
498 csio_resource_free(struct csio_hw *hw)
500 dma_pool_destroy(hw->scsi_dma_pool);
501 hw->scsi_dma_pool = NULL;
502 mempool_destroy(hw->rnode_mempool);
503 hw->rnode_mempool = NULL;
504 mempool_destroy(hw->mb_mempool);
505 hw->mb_mempool = NULL;
509 * csio_hw_alloc - Allocate and initialize the HW module.
510 * @pdev: PCI device.
512 * Allocates HW structure, DMA, memory resources, maps BARS to
513 * host memory and initializes HW module.
515 static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
517 struct csio_hw *hw;
519 hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
520 if (!hw)
521 goto err;
523 hw->pdev = pdev;
524 strscpy(hw->drv_version, CSIO_DRV_VERSION,
525 sizeof(hw->drv_version));
527 /* memory pool/DMA pool allocation */
528 if (csio_resource_alloc(hw))
529 goto err_free_hw;
531 /* Get the start address of registers from BAR 0 */
532 hw->regstart = ioremap(pci_resource_start(pdev, 0),
533 pci_resource_len(pdev, 0));
534 if (!hw->regstart) {
535 csio_err(hw, "Could not map BAR 0, regstart = %p\n",
536 hw->regstart);
537 goto err_resource_free;
540 csio_hw_init_workers(hw);
542 if (csio_hw_init(hw))
543 goto err_unmap_bar;
545 csio_dfs_create(hw);
547 csio_dbg(hw, "hw:%p\n", hw);
549 return hw;
551 err_unmap_bar:
552 csio_hw_exit_workers(hw);
553 iounmap(hw->regstart);
554 err_resource_free:
555 csio_resource_free(hw);
556 err_free_hw:
557 kfree(hw);
558 err:
559 return NULL;
563 * csio_hw_free - Uninitialize and free the HW module.
564 * @hw: The HW module
566 * Disable interrupts, uninit the HW module, free resources, free hw.
568 static void
569 csio_hw_free(struct csio_hw *hw)
571 csio_intr_disable(hw, true);
572 csio_hw_exit_workers(hw);
573 csio_hw_exit(hw);
574 iounmap(hw->regstart);
575 csio_dfs_destroy(hw);
576 csio_resource_free(hw);
577 kfree(hw);
581 * csio_shost_init - Create and initialize the lnode module.
582 * @hw: The HW module.
583 * @dev: The device associated with this invocation.
584 * @probe: Called from probe context or not?
585 * @pln: Parent lnode if any.
587 * Allocates lnode structure via scsi_host_alloc, initializes
588 * shost, initializes lnode module and registers with SCSI ML
589 * via scsi_host_add. This function is shared between physical and
590 * virtual node ports.
592 struct csio_lnode *
593 csio_shost_init(struct csio_hw *hw, struct device *dev,
594 bool probe, struct csio_lnode *pln)
596 struct Scsi_Host *shost = NULL;
597 struct csio_lnode *ln;
599 csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
600 csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
603 * hw->pdev is the physical port's PCI dev structure,
604 * which will be different from the NPIV dev structure.
606 if (dev == &hw->pdev->dev)
607 shost = scsi_host_alloc(
608 &csio_fcoe_shost_template,
609 sizeof(struct csio_lnode));
610 else
611 shost = scsi_host_alloc(
612 &csio_fcoe_shost_vport_template,
613 sizeof(struct csio_lnode));
615 if (!shost)
616 goto err;
618 ln = shost_priv(shost);
619 memset(ln, 0, sizeof(struct csio_lnode));
621 /* Link common lnode to this lnode */
622 ln->dev_num = (shost->host_no << 16);
624 shost->can_queue = CSIO_MAX_QUEUE;
625 shost->this_id = -1;
626 shost->unique_id = shost->host_no;
627 shost->max_cmd_len = 16; /* Max CDB length supported */
628 shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
629 hw->fres_info.max_ssns);
630 shost->max_lun = CSIO_MAX_LUN;
631 if (dev == &hw->pdev->dev)
632 shost->transportt = csio_fcoe_transport;
633 else
634 shost->transportt = csio_fcoe_transport_vport;
636 /* root lnode */
637 if (!hw->rln)
638 hw->rln = ln;
640 /* Other initialization here: Common, Transport specific */
641 if (csio_lnode_init(ln, hw, pln))
642 goto err_shost_put;
644 if (scsi_add_host_with_dma(shost, dev, &hw->pdev->dev))
645 goto err_lnode_exit;
647 return ln;
649 err_lnode_exit:
650 csio_lnode_exit(ln);
651 err_shost_put:
652 scsi_host_put(shost);
653 err:
654 return NULL;
658 * csio_shost_exit - De-instantiate the shost.
659 * @ln: The lnode module corresponding to the shost.
662 void
663 csio_shost_exit(struct csio_lnode *ln)
665 struct Scsi_Host *shost = csio_ln_to_shost(ln);
666 struct csio_hw *hw = csio_lnode_to_hw(ln);
668 /* Inform transport */
669 fc_remove_host(shost);
671 /* Inform SCSI ML */
672 scsi_remove_host(shost);
674 /* Flush all the events, so that any rnode removal events
675 * already queued are all handled, before we remove the lnode.
677 spin_lock_irq(&hw->lock);
678 csio_evtq_flush(hw);
679 spin_unlock_irq(&hw->lock);
681 csio_lnode_exit(ln);
682 scsi_host_put(shost);
685 struct csio_lnode *
686 csio_lnode_alloc(struct csio_hw *hw)
688 return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
691 void
692 csio_lnodes_block_request(struct csio_hw *hw)
694 struct Scsi_Host *shost;
695 struct csio_lnode *sln;
696 struct csio_lnode *ln;
697 struct list_head *cur_ln, *cur_cln;
698 struct csio_lnode **lnode_list;
699 int cur_cnt = 0, ii;
701 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
702 GFP_KERNEL);
703 if (!lnode_list) {
704 csio_err(hw, "Failed to allocate lnodes_list");
705 return;
708 spin_lock_irq(&hw->lock);
709 /* Traverse sibling lnodes */
710 list_for_each(cur_ln, &hw->sln_head) {
711 sln = (struct csio_lnode *) cur_ln;
712 lnode_list[cur_cnt++] = sln;
714 /* Traverse children lnodes */
715 list_for_each(cur_cln, &sln->cln_head)
716 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
718 spin_unlock_irq(&hw->lock);
720 for (ii = 0; ii < cur_cnt; ii++) {
721 csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
722 ln = lnode_list[ii];
723 shost = csio_ln_to_shost(ln);
724 scsi_block_requests(shost);
727 kfree(lnode_list);
730 void
731 csio_lnodes_unblock_request(struct csio_hw *hw)
733 struct csio_lnode *ln;
734 struct Scsi_Host *shost;
735 struct csio_lnode *sln;
736 struct list_head *cur_ln, *cur_cln;
737 struct csio_lnode **lnode_list;
738 int cur_cnt = 0, ii;
740 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
741 GFP_KERNEL);
742 if (!lnode_list) {
743 csio_err(hw, "Failed to allocate lnodes_list");
744 return;
747 spin_lock_irq(&hw->lock);
748 /* Traverse sibling lnodes */
749 list_for_each(cur_ln, &hw->sln_head) {
750 sln = (struct csio_lnode *) cur_ln;
751 lnode_list[cur_cnt++] = sln;
753 /* Traverse children lnodes */
754 list_for_each(cur_cln, &sln->cln_head)
755 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
757 spin_unlock_irq(&hw->lock);
759 for (ii = 0; ii < cur_cnt; ii++) {
760 csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
761 ln = lnode_list[ii];
762 shost = csio_ln_to_shost(ln);
763 scsi_unblock_requests(shost);
765 kfree(lnode_list);
768 void
769 csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
771 struct csio_lnode *ln;
772 struct Scsi_Host *shost;
773 struct csio_lnode *sln;
774 struct list_head *cur_ln, *cur_cln;
775 struct csio_lnode **lnode_list;
776 int cur_cnt = 0, ii;
778 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
779 GFP_KERNEL);
780 if (!lnode_list) {
781 csio_err(hw, "Failed to allocate lnodes_list");
782 return;
785 spin_lock_irq(&hw->lock);
786 /* Traverse sibling lnodes */
787 list_for_each(cur_ln, &hw->sln_head) {
788 sln = (struct csio_lnode *) cur_ln;
789 if (sln->portid != portid)
790 continue;
792 lnode_list[cur_cnt++] = sln;
794 /* Traverse children lnodes */
795 list_for_each(cur_cln, &sln->cln_head)
796 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
798 spin_unlock_irq(&hw->lock);
800 for (ii = 0; ii < cur_cnt; ii++) {
801 csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
802 ln = lnode_list[ii];
803 shost = csio_ln_to_shost(ln);
804 scsi_block_requests(shost);
806 kfree(lnode_list);
809 void
810 csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
812 struct csio_lnode *ln;
813 struct Scsi_Host *shost;
814 struct csio_lnode *sln;
815 struct list_head *cur_ln, *cur_cln;
816 struct csio_lnode **lnode_list;
817 int cur_cnt = 0, ii;
819 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
820 GFP_KERNEL);
821 if (!lnode_list) {
822 csio_err(hw, "Failed to allocate lnodes_list");
823 return;
826 spin_lock_irq(&hw->lock);
827 /* Traverse sibling lnodes */
828 list_for_each(cur_ln, &hw->sln_head) {
829 sln = (struct csio_lnode *) cur_ln;
830 if (sln->portid != portid)
831 continue;
832 lnode_list[cur_cnt++] = sln;
834 /* Traverse children lnodes */
835 list_for_each(cur_cln, &sln->cln_head)
836 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
838 spin_unlock_irq(&hw->lock);
840 for (ii = 0; ii < cur_cnt; ii++) {
841 csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
842 ln = lnode_list[ii];
843 shost = csio_ln_to_shost(ln);
844 scsi_unblock_requests(shost);
846 kfree(lnode_list);
849 void
850 csio_lnodes_exit(struct csio_hw *hw, bool npiv)
852 struct csio_lnode *sln;
853 struct csio_lnode *ln;
854 struct list_head *cur_ln, *cur_cln;
855 struct csio_lnode **lnode_list;
856 int cur_cnt = 0, ii;
858 lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
859 GFP_KERNEL);
860 if (!lnode_list) {
861 csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
862 return;
865 /* Get all child lnodes(NPIV ports) */
866 spin_lock_irq(&hw->lock);
867 list_for_each(cur_ln, &hw->sln_head) {
868 sln = (struct csio_lnode *) cur_ln;
870 /* Traverse children lnodes */
871 list_for_each(cur_cln, &sln->cln_head)
872 lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
874 spin_unlock_irq(&hw->lock);
876 /* Delete NPIV lnodes */
877 for (ii = 0; ii < cur_cnt; ii++) {
878 csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
879 ln = lnode_list[ii];
880 fc_vport_terminate(ln->fc_vport);
883 /* Delete only npiv lnodes */
884 if (npiv)
885 goto free_lnodes;
887 cur_cnt = 0;
888 /* Get all physical lnodes */
889 spin_lock_irq(&hw->lock);
890 /* Traverse sibling lnodes */
891 list_for_each(cur_ln, &hw->sln_head) {
892 sln = (struct csio_lnode *) cur_ln;
893 lnode_list[cur_cnt++] = sln;
895 spin_unlock_irq(&hw->lock);
897 /* Delete physical lnodes */
898 for (ii = 0; ii < cur_cnt; ii++) {
899 csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
900 csio_shost_exit(lnode_list[ii]);
903 free_lnodes:
904 kfree(lnode_list);
908 * csio_lnode_init_post: Set lnode attributes after starting HW.
909 * @ln: lnode.
912 static void
913 csio_lnode_init_post(struct csio_lnode *ln)
915 struct Scsi_Host *shost = csio_ln_to_shost(ln);
917 csio_fchost_attr_init(ln);
919 scsi_scan_host(shost);
923 * csio_probe_one - Instantiate this function.
924 * @pdev: PCI device
925 * @id: Device ID
927 * This is the .probe() callback of the driver. This function:
928 * - Initializes the PCI function by enabling MMIO, setting bus
929 * mastership and setting DMA mask.
930 * - Allocates HW structure, DMA, memory resources, maps BARS to
931 * host memory and initializes HW module.
932 * - Allocates lnode structure via scsi_host_alloc, initializes
933 * shost, initialized lnode module and registers with SCSI ML
934 * via scsi_host_add.
935 * - Enables interrupts, and starts the chip by kicking off the
936 * HW state machine.
937 * - Once hardware is ready, initiated scan of the host via
938 * scsi_scan_host.
940 static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
942 int rv;
943 int bars;
944 int i;
945 struct csio_hw *hw;
946 struct csio_lnode *ln;
948 /* probe only T5 and T6 cards */
949 if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)) &&
950 !csio_is_t6((pdev->device & CSIO_HW_CHIP_MASK)))
951 return -ENODEV;
953 rv = csio_pci_init(pdev, &bars);
954 if (rv)
955 goto err;
957 hw = csio_hw_alloc(pdev);
958 if (!hw) {
959 rv = -ENODEV;
960 goto err_pci_exit;
963 if (!pcie_relaxed_ordering_enabled(pdev))
964 hw->flags |= CSIO_HWF_ROOT_NO_RELAXED_ORDERING;
966 pci_set_drvdata(pdev, hw);
968 rv = csio_hw_start(hw);
969 if (rv) {
970 if (rv == -EINVAL) {
971 dev_err(&pdev->dev,
972 "Failed to start FW, continuing in debug mode.\n");
973 return 0;
975 goto err_lnode_exit;
978 sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
979 FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
980 FW_HDR_FW_VER_MINOR_G(hw->fwrev),
981 FW_HDR_FW_VER_MICRO_G(hw->fwrev),
982 FW_HDR_FW_VER_BUILD_G(hw->fwrev));
984 for (i = 0; i < hw->num_pports; i++) {
985 ln = csio_shost_init(hw, &pdev->dev, true, NULL);
986 if (!ln) {
987 rv = -ENODEV;
988 break;
990 /* Initialize portid */
991 ln->portid = hw->pport[i].portid;
993 spin_lock_irq(&hw->lock);
994 if (csio_lnode_start(ln) != 0)
995 rv = -ENODEV;
996 spin_unlock_irq(&hw->lock);
998 if (rv)
999 break;
1001 csio_lnode_init_post(ln);
1004 if (rv)
1005 goto err_lnode_exit;
1007 return 0;
1009 err_lnode_exit:
1010 csio_lnodes_block_request(hw);
1011 spin_lock_irq(&hw->lock);
1012 csio_hw_stop(hw);
1013 spin_unlock_irq(&hw->lock);
1014 csio_lnodes_unblock_request(hw);
1015 csio_lnodes_exit(hw, 0);
1016 csio_hw_free(hw);
1017 err_pci_exit:
1018 csio_pci_exit(pdev, &bars);
1019 err:
1020 dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
1021 return rv;
1025 * csio_remove_one - Remove one instance of the driver at this PCI function.
1026 * @pdev: PCI device
1028 * Used during hotplug operation.
1030 static void csio_remove_one(struct pci_dev *pdev)
1032 struct csio_hw *hw = pci_get_drvdata(pdev);
1033 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1035 csio_lnodes_block_request(hw);
1036 spin_lock_irq(&hw->lock);
1038 /* Stops lnode, Rnode s/m
1039 * Quiesce IOs.
1040 * All sessions with remote ports are unregistered.
1042 csio_hw_stop(hw);
1043 spin_unlock_irq(&hw->lock);
1044 csio_lnodes_unblock_request(hw);
1046 csio_lnodes_exit(hw, 0);
1047 csio_hw_free(hw);
1048 csio_pci_exit(pdev, &bars);
1052 * csio_pci_error_detected - PCI error was detected
1053 * @pdev: PCI device
1056 static pci_ers_result_t
1057 csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1059 struct csio_hw *hw = pci_get_drvdata(pdev);
1061 csio_lnodes_block_request(hw);
1062 spin_lock_irq(&hw->lock);
1064 /* Post PCI error detected evt to HW s/m
1065 * HW s/m handles this evt by quiescing IOs, unregisters rports
1066 * and finally takes the device to offline.
1068 csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
1069 spin_unlock_irq(&hw->lock);
1070 csio_lnodes_unblock_request(hw);
1071 csio_lnodes_exit(hw, 0);
1072 csio_intr_disable(hw, true);
1073 pci_disable_device(pdev);
1074 return state == pci_channel_io_perm_failure ?
1075 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1079 * csio_pci_slot_reset - PCI slot has been reset.
1080 * @pdev: PCI device
1083 static pci_ers_result_t
1084 csio_pci_slot_reset(struct pci_dev *pdev)
1086 struct csio_hw *hw = pci_get_drvdata(pdev);
1087 int ready;
1089 if (pci_enable_device(pdev)) {
1090 dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
1091 return PCI_ERS_RESULT_DISCONNECT;
1094 pci_set_master(pdev);
1095 pci_restore_state(pdev);
1096 pci_save_state(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);
1106 if (ready) {
1107 return PCI_ERS_RESULT_RECOVERED;
1108 } else {
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
1116 * @pdev: PCI device
1119 static void
1120 csio_pci_resume(struct pci_dev *pdev)
1122 struct csio_hw *hw = pci_get_drvdata(pdev);
1123 struct csio_lnode *ln;
1124 int rv = 0;
1125 int i;
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);
1131 if (!ln) {
1132 rv = -ENODEV;
1133 break;
1135 /* Initialize portid */
1136 ln->portid = hw->pport[i].portid;
1138 spin_lock_irq(&hw->lock);
1139 if (csio_lnode_start(ln) != 0)
1140 rv = -ENODEV;
1141 spin_unlock_irq(&hw->lock);
1143 if (rv)
1144 break;
1146 csio_lnode_init_post(ln);
1149 if (rv)
1150 goto err_resume_exit;
1152 return;
1154 err_resume_exit:
1155 csio_lnodes_block_request(hw);
1156 spin_lock_irq(&hw->lock);
1157 csio_hw_stop(hw);
1158 spin_unlock_irq(&hw->lock);
1159 csio_lnodes_unblock_request(hw);
1160 csio_lnodes_exit(hw, 0);
1161 csio_hw_free(hw);
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,
1172 * Macros needed to support the PCI Device ID Table ...
1174 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
1175 static const struct pci_device_id csio_pci_tbl[] = {
1176 /* Define for FCoE uses PF6 */
1177 #define CH_PCI_DEVICE_ID_FUNCTION 0x6
1179 #define CH_PCI_ID_TABLE_ENTRY(devid) \
1180 { PCI_VDEVICE(CHELSIO, (devid)), 0 }
1182 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
1184 #include "t4_pci_id_tbl.h"
1186 static struct pci_driver csio_pci_driver = {
1187 .name = KBUILD_MODNAME,
1188 .id_table = csio_pci_tbl,
1189 .probe = csio_probe_one,
1190 .remove = csio_remove_one,
1191 .err_handler = &csio_err_handler,
1195 * csio_init - Chelsio storage driver initialization function.
1198 static int __init
1199 csio_init(void)
1201 int rv = -ENOMEM;
1203 pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
1205 csio_dfs_init();
1207 csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
1208 if (!csio_fcoe_transport)
1209 goto err;
1211 csio_fcoe_transport_vport =
1212 fc_attach_transport(&csio_fc_transport_vport_funcs);
1213 if (!csio_fcoe_transport_vport)
1214 goto err_vport;
1216 rv = pci_register_driver(&csio_pci_driver);
1217 if (rv)
1218 goto err_pci;
1220 return 0;
1222 err_pci:
1223 fc_release_transport(csio_fcoe_transport_vport);
1224 err_vport:
1225 fc_release_transport(csio_fcoe_transport);
1226 err:
1227 csio_dfs_exit();
1228 return rv;
1232 * csio_exit - Chelsio storage driver uninitialization .
1234 * Function that gets called in the unload path.
1236 static void __exit
1237 csio_exit(void)
1239 pci_unregister_driver(&csio_pci_driver);
1240 csio_dfs_exit();
1241 fc_release_transport(csio_fcoe_transport_vport);
1242 fc_release_transport(csio_fcoe_transport);
1245 module_init(csio_init);
1246 module_exit(csio_exit);
1247 MODULE_AUTHOR(CSIO_DRV_AUTHOR);
1248 MODULE_DESCRIPTION(CSIO_DRV_DESC);
1249 MODULE_LICENSE("Dual BSD/GPL");
1250 MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
1251 MODULE_VERSION(CSIO_DRV_VERSION);
1252 MODULE_FIRMWARE(FW_FNAME_T5);
1253 MODULE_FIRMWARE(FW_FNAME_T6);
1254 MODULE_SOFTDEP("pre: cxgb4");