xref: /linux/drivers/scsi/csiostor/csio_init.c (revision 9abdb50cda0ffe33bbb2e40cbad97b32fb7ff892)
1 /*
2  * This file is part of the Chelsio FCoE driver for Linux.
3  *
4  * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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.
33  */
34 
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 
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>
42 #include <linux/mm.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>
49 
50 #include "csio_init.h"
51 #include "csio_defs.h"
52 
53 #define CSIO_MIN_MEMPOOL_SZ	64
54 
55 static struct dentry *csio_debugfs_root;
56 
57 static struct scsi_transport_template *csio_fcoe_transport;
58 static struct scsi_transport_template *csio_fcoe_transport_vport;
59 
60 /*
61  * debugfs support
62  */
63 static ssize_t
64 csio_mem_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
65 {
66 	loff_t pos = *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;
70 
71 	if (pos < 0)
72 		return -EINVAL;
73 	if (pos >= avail)
74 		return 0;
75 	if (count > avail - pos)
76 		count = avail - pos;
77 
78 	while (count) {
79 		size_t len;
80 		int ret, ofst;
81 		__be32 data[16];
82 
83 		if (mem == MEM_MC)
84 			ret = hw->chip_ops->chip_mc_read(hw, 0, pos,
85 							 data, NULL);
86 		else
87 			ret = hw->chip_ops->chip_edc_read(hw, mem, pos,
88 							  data, NULL);
89 		if (ret)
90 			return ret;
91 
92 		ofst = pos % sizeof(data);
93 		len = min(count, sizeof(data) - ofst);
94 		if (copy_to_user(buf, (u8 *)data + ofst, len))
95 			return -EFAULT;
96 
97 		buf += len;
98 		pos += len;
99 		count -= len;
100 	}
101 	count = pos - *ppos;
102 	*ppos = pos;
103 	return count;
104 }
105 
106 static const struct file_operations csio_mem_debugfs_fops = {
107 	.owner   = THIS_MODULE,
108 	.open    = simple_open,
109 	.read    = csio_mem_read,
110 	.llseek  = default_llseek,
111 };
112 
113 void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
114 				 unsigned int idx, unsigned int size_mb)
115 {
116 	debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
117 				 (void *)hw + idx, &csio_mem_debugfs_fops,
118 				 size_mb << 20);
119 }
120 
121 static int csio_setup_debugfs(struct csio_hw *hw)
122 {
123 	int i;
124 
125 	if (IS_ERR_OR_NULL(hw->debugfs_root))
126 		return -1;
127 
128 	i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE_A);
129 	if (i & EDRAM0_ENABLE_F)
130 		csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
131 	if (i & EDRAM1_ENABLE_F)
132 		csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
133 
134 	hw->chip_ops->chip_dfs_create_ext_mem(hw);
135 	return 0;
136 }
137 
138 /*
139  * csio_dfs_create - Creates and sets up per-hw debugfs.
140  *
141  */
142 static int
143 csio_dfs_create(struct csio_hw *hw)
144 {
145 	if (csio_debugfs_root) {
146 		hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
147 							csio_debugfs_root);
148 		csio_setup_debugfs(hw);
149 	}
150 
151 	return 0;
152 }
153 
154 /*
155  * csio_dfs_destroy - Destroys per-hw debugfs.
156  */
157 static int
158 csio_dfs_destroy(struct csio_hw *hw)
159 {
160 	if (hw->debugfs_root)
161 		debugfs_remove_recursive(hw->debugfs_root);
162 
163 	return 0;
164 }
165 
166 /*
167  * csio_dfs_init - Debug filesystem initialization for the module.
168  *
169  */
170 static int
171 csio_dfs_init(void)
172 {
173 	csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
174 	if (!csio_debugfs_root)
175 		pr_warn("Could not create debugfs entry, continuing\n");
176 
177 	return 0;
178 }
179 
180 /*
181  * csio_dfs_exit - debugfs cleanup for the module.
182  */
183 static void
184 csio_dfs_exit(void)
185 {
186 	debugfs_remove(csio_debugfs_root);
187 }
188 
189 /*
190  * csio_pci_init - PCI initialization.
191  * @pdev: PCI device.
192  * @bars: Bitmask of bars to be requested.
193  *
194  * Initializes the PCI function by enabling MMIO, setting bus
195  * mastership and setting DMA mask.
196  */
197 static int
198 csio_pci_init(struct pci_dev *pdev, int *bars)
199 {
200 	int rv = -ENODEV;
201 
202 	*bars = pci_select_bars(pdev, IORESOURCE_MEM);
203 
204 	if (pci_enable_device_mem(pdev))
205 		goto err;
206 
207 	if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
208 		goto err_disable_device;
209 
210 	pci_set_master(pdev);
211 	pci_try_set_mwi(pdev);
212 
213 	rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
214 	if (rv)
215 		rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
216 	if (rv) {
217 		rv = -ENODEV;
218 		dev_err(&pdev->dev, "No suitable DMA available.\n");
219 		goto err_release_regions;
220 	}
221 
222 	return 0;
223 
224 err_release_regions:
225 	pci_release_selected_regions(pdev, *bars);
226 err_disable_device:
227 	pci_disable_device(pdev);
228 err:
229 	return rv;
230 
231 }
232 
233 /*
234  * csio_pci_exit - PCI unitialization.
235  * @pdev: PCI device.
236  * @bars: Bars to be released.
237  *
238  */
239 static void
240 csio_pci_exit(struct pci_dev *pdev, int *bars)
241 {
242 	pci_release_selected_regions(pdev, *bars);
243 	pci_disable_device(pdev);
244 }
245 
246 /*
247  * csio_hw_init_workers - Initialize the HW module's worker threads.
248  * @hw: HW module.
249  *
250  */
251 static void
252 csio_hw_init_workers(struct csio_hw *hw)
253 {
254 	INIT_WORK(&hw->evtq_work, csio_evtq_worker);
255 }
256 
257 static void
258 csio_hw_exit_workers(struct csio_hw *hw)
259 {
260 	cancel_work_sync(&hw->evtq_work);
261 }
262 
263 static int
264 csio_create_queues(struct csio_hw *hw)
265 {
266 	int i, j;
267 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
268 	int rv;
269 	struct csio_scsi_cpu_info *info;
270 
271 	if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
272 		return 0;
273 
274 	if (hw->intr_mode != CSIO_IM_MSIX) {
275 		rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
276 					0, hw->pport[0].portid, false, NULL);
277 		if (rv != 0) {
278 			csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
279 			return rv;
280 		}
281 	}
282 
283 	/* FW event queue */
284 	rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
285 			       csio_get_fwevt_intr_idx(hw),
286 			       hw->pport[0].portid, true, NULL);
287 	if (rv != 0) {
288 		csio_err(hw, "FW event IQ config failed!: %d\n", rv);
289 		return rv;
290 	}
291 
292 	/* Create mgmt queue */
293 	rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
294 			mgmtm->iq_idx, hw->pport[0].portid, NULL);
295 
296 	if (rv != 0) {
297 		csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
298 		goto err;
299 	}
300 
301 	/* Create SCSI queues */
302 	for (i = 0; i < hw->num_pports; i++) {
303 		info = &hw->scsi_cpu_info[i];
304 
305 		for (j = 0; j < info->max_cpus; j++) {
306 			struct csio_scsi_qset *sqset = &hw->sqset[i][j];
307 
308 			rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
309 					       sqset->intr_idx, i, false, NULL);
310 			if (rv != 0) {
311 				csio_err(hw,
312 				   "SCSI module IQ config failed [%d][%d]:%d\n",
313 				   i, j, rv);
314 				goto err;
315 			}
316 			rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
317 					       sqset->iq_idx, i, NULL);
318 			if (rv != 0) {
319 				csio_err(hw,
320 				   "SCSI module EQ config failed [%d][%d]:%d\n",
321 				   i, j, rv);
322 				goto err;
323 			}
324 		} /* for all CPUs */
325 	} /* For all ports */
326 
327 	hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
328 	return 0;
329 err:
330 	csio_wr_destroy_queues(hw, true);
331 	return -EINVAL;
332 }
333 
334 /*
335  * csio_config_queues - Configure the DMA queues.
336  * @hw: HW module.
337  *
338  * Allocates memory for queues are registers them with FW.
339  */
340 int
341 csio_config_queues(struct csio_hw *hw)
342 {
343 	int i, j, idx, k = 0;
344 	int rv;
345 	struct csio_scsi_qset *sqset;
346 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
347 	struct csio_scsi_qset *orig;
348 	struct csio_scsi_cpu_info *info;
349 
350 	if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
351 		return csio_create_queues(hw);
352 
353 	/* Calculate number of SCSI queues for MSIX we would like */
354 	hw->num_scsi_msix_cpus = num_online_cpus();
355 	hw->num_sqsets = num_online_cpus() * hw->num_pports;
356 
357 	if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
358 		hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
359 		hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
360 	}
361 
362 	/* Initialize max_cpus, may get reduced during msix allocations */
363 	for (i = 0; i < hw->num_pports; i++)
364 		hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
365 
366 	csio_dbg(hw, "nsqsets:%d scpus:%d\n",
367 		    hw->num_sqsets, hw->num_scsi_msix_cpus);
368 
369 	csio_intr_enable(hw);
370 
371 	if (hw->intr_mode != CSIO_IM_MSIX) {
372 
373 		/* Allocate Forward interrupt iq. */
374 		hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
375 						CSIO_INTR_WRSIZE, CSIO_INGRESS,
376 						(void *)hw, 0, 0, NULL);
377 		if (hw->intr_iq_idx == -1) {
378 			csio_err(hw,
379 				 "Forward interrupt queue creation failed\n");
380 			goto intr_disable;
381 		}
382 	}
383 
384 	/* Allocate the FW evt queue */
385 	hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
386 					   CSIO_FWEVT_WRSIZE,
387 					   CSIO_INGRESS, (void *)hw,
388 					   CSIO_FWEVT_FLBUFS, 0,
389 					   csio_fwevt_intx_handler);
390 	if (hw->fwevt_iq_idx == -1) {
391 		csio_err(hw, "FW evt queue creation failed\n");
392 		goto intr_disable;
393 	}
394 
395 	/* Allocate the mgmt queue */
396 	mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
397 				      CSIO_MGMT_EQ_WRSIZE,
398 				      CSIO_EGRESS, (void *)hw, 0, 0, NULL);
399 	if (mgmtm->eq_idx == -1) {
400 		csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
401 		goto intr_disable;
402 	}
403 
404 	/* Use FW IQ for MGMT req completion */
405 	mgmtm->iq_idx = hw->fwevt_iq_idx;
406 
407 	/* Allocate SCSI queues */
408 	for (i = 0; i < hw->num_pports; i++) {
409 		info = &hw->scsi_cpu_info[i];
410 
411 		for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
412 			sqset = &hw->sqset[i][j];
413 
414 			if (j >= info->max_cpus) {
415 				k = j % info->max_cpus;
416 				orig = &hw->sqset[i][k];
417 				sqset->eq_idx = orig->eq_idx;
418 				sqset->iq_idx = orig->iq_idx;
419 				continue;
420 			}
421 
422 			idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
423 					      CSIO_EGRESS, (void *)hw, 0, 0,
424 					      NULL);
425 			if (idx == -1) {
426 				csio_err(hw, "EQ creation failed for idx:%d\n",
427 					    idx);
428 				goto intr_disable;
429 			}
430 
431 			sqset->eq_idx = idx;
432 
433 			idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
434 					     CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
435 					     (void *)hw, 0, 0,
436 					     csio_scsi_intx_handler);
437 			if (idx == -1) {
438 				csio_err(hw, "IQ creation failed for idx:%d\n",
439 					    idx);
440 				goto intr_disable;
441 			}
442 			sqset->iq_idx = idx;
443 		} /* for all CPUs */
444 	} /* For all ports */
445 
446 	hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
447 
448 	rv = csio_create_queues(hw);
449 	if (rv != 0)
450 		goto intr_disable;
451 
452 	/*
453 	 * Now request IRQs for the vectors. In the event of a failure,
454 	 * cleanup is handled internally by this function.
455 	 */
456 	rv = csio_request_irqs(hw);
457 	if (rv != 0)
458 		return -EINVAL;
459 
460 	return 0;
461 
462 intr_disable:
463 	csio_intr_disable(hw, false);
464 
465 	return -EINVAL;
466 }
467 
468 static int
469 csio_resource_alloc(struct csio_hw *hw)
470 {
471 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
472 	int rv = -ENOMEM;
473 
474 	wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
475 		       CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
476 
477 	hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
478 						  sizeof(struct csio_mb));
479 	if (!hw->mb_mempool)
480 		goto err;
481 
482 	hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
483 						     sizeof(struct csio_rnode));
484 	if (!hw->rnode_mempool)
485 		goto err_free_mb_mempool;
486 
487 	hw->scsi_dma_pool = dma_pool_create("csio_scsi_dma_pool",
488 					    &hw->pdev->dev, CSIO_SCSI_RSP_LEN,
489 					    8, 0);
490 	if (!hw->scsi_dma_pool)
491 		goto err_free_rn_pool;
492 
493 	return 0;
494 
495 err_free_rn_pool:
496 	mempool_destroy(hw->rnode_mempool);
497 	hw->rnode_mempool = NULL;
498 err_free_mb_mempool:
499 	mempool_destroy(hw->mb_mempool);
500 	hw->mb_mempool = NULL;
501 err:
502 	return rv;
503 }
504 
505 static void
506 csio_resource_free(struct csio_hw *hw)
507 {
508 	dma_pool_destroy(hw->scsi_dma_pool);
509 	hw->scsi_dma_pool = NULL;
510 	mempool_destroy(hw->rnode_mempool);
511 	hw->rnode_mempool = NULL;
512 	mempool_destroy(hw->mb_mempool);
513 	hw->mb_mempool = NULL;
514 }
515 
516 /*
517  * csio_hw_alloc - Allocate and initialize the HW module.
518  * @pdev: PCI device.
519  *
520  * Allocates HW structure, DMA, memory resources, maps BARS to
521  * host memory and initializes HW module.
522  */
523 static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
524 {
525 	struct csio_hw *hw;
526 
527 	hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
528 	if (!hw)
529 		goto err;
530 
531 	hw->pdev = pdev;
532 	strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
533 
534 	/* memory pool/DMA pool allocation */
535 	if (csio_resource_alloc(hw))
536 		goto err_free_hw;
537 
538 	/* Get the start address of registers from BAR 0 */
539 	hw->regstart = ioremap_nocache(pci_resource_start(pdev, 0),
540 				       pci_resource_len(pdev, 0));
541 	if (!hw->regstart) {
542 		csio_err(hw, "Could not map BAR 0, regstart = %p\n",
543 			 hw->regstart);
544 		goto err_resource_free;
545 	}
546 
547 	csio_hw_init_workers(hw);
548 
549 	if (csio_hw_init(hw))
550 		goto err_unmap_bar;
551 
552 	csio_dfs_create(hw);
553 
554 	csio_dbg(hw, "hw:%p\n", hw);
555 
556 	return hw;
557 
558 err_unmap_bar:
559 	csio_hw_exit_workers(hw);
560 	iounmap(hw->regstart);
561 err_resource_free:
562 	csio_resource_free(hw);
563 err_free_hw:
564 	kfree(hw);
565 err:
566 	return NULL;
567 }
568 
569 /*
570  * csio_hw_free - Uninitialize and free the HW module.
571  * @hw: The HW module
572  *
573  * Disable interrupts, uninit the HW module, free resources, free hw.
574  */
575 static void
576 csio_hw_free(struct csio_hw *hw)
577 {
578 	csio_intr_disable(hw, true);
579 	csio_hw_exit_workers(hw);
580 	csio_hw_exit(hw);
581 	iounmap(hw->regstart);
582 	csio_dfs_destroy(hw);
583 	csio_resource_free(hw);
584 	kfree(hw);
585 }
586 
587 /**
588  * csio_shost_init - Create and initialize the lnode module.
589  * @hw:		The HW module.
590  * @dev:	The device associated with this invocation.
591  * @probe:	Called from probe context or not?
592  * @os_pln:	Parent lnode if any.
593  *
594  * Allocates lnode structure via scsi_host_alloc, initializes
595  * shost, initializes lnode module and registers with SCSI ML
596  * via scsi_host_add. This function is shared between physical and
597  * virtual node ports.
598  */
599 struct csio_lnode *
600 csio_shost_init(struct csio_hw *hw, struct device *dev,
601 		  bool probe, struct csio_lnode *pln)
602 {
603 	struct Scsi_Host  *shost = NULL;
604 	struct csio_lnode *ln;
605 
606 	csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
607 	csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
608 
609 	/*
610 	 * hw->pdev is the physical port's PCI dev structure,
611 	 * which will be different from the NPIV dev structure.
612 	 */
613 	if (dev == &hw->pdev->dev)
614 		shost = scsi_host_alloc(
615 				&csio_fcoe_shost_template,
616 				sizeof(struct csio_lnode));
617 	else
618 		shost = scsi_host_alloc(
619 				&csio_fcoe_shost_vport_template,
620 				sizeof(struct csio_lnode));
621 
622 	if (!shost)
623 		goto err;
624 
625 	ln = shost_priv(shost);
626 	memset(ln, 0, sizeof(struct csio_lnode));
627 
628 	/* Link common lnode to this lnode */
629 	ln->dev_num = (shost->host_no << 16);
630 
631 	shost->can_queue = CSIO_MAX_QUEUE;
632 	shost->this_id = -1;
633 	shost->unique_id = shost->host_no;
634 	shost->max_cmd_len = 16; /* Max CDB length supported */
635 	shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
636 			      hw->fres_info.max_ssns);
637 	shost->max_lun = CSIO_MAX_LUN;
638 	if (dev == &hw->pdev->dev)
639 		shost->transportt = csio_fcoe_transport;
640 	else
641 		shost->transportt = csio_fcoe_transport_vport;
642 
643 	/* root lnode */
644 	if (!hw->rln)
645 		hw->rln = ln;
646 
647 	/* Other initialization here: Common, Transport specific */
648 	if (csio_lnode_init(ln, hw, pln))
649 		goto err_shost_put;
650 
651 	if (scsi_add_host_with_dma(shost, dev, &hw->pdev->dev))
652 		goto err_lnode_exit;
653 
654 	return ln;
655 
656 err_lnode_exit:
657 	csio_lnode_exit(ln);
658 err_shost_put:
659 	scsi_host_put(shost);
660 err:
661 	return NULL;
662 }
663 
664 /**
665  * csio_shost_exit - De-instantiate the shost.
666  * @ln:		The lnode module corresponding to the shost.
667  *
668  */
669 void
670 csio_shost_exit(struct csio_lnode *ln)
671 {
672 	struct Scsi_Host *shost = csio_ln_to_shost(ln);
673 	struct csio_hw *hw = csio_lnode_to_hw(ln);
674 
675 	/* Inform transport */
676 	fc_remove_host(shost);
677 
678 	/* Inform SCSI ML */
679 	scsi_remove_host(shost);
680 
681 	/* Flush all the events, so that any rnode removal events
682 	 * already queued are all handled, before we remove the lnode.
683 	 */
684 	spin_lock_irq(&hw->lock);
685 	csio_evtq_flush(hw);
686 	spin_unlock_irq(&hw->lock);
687 
688 	csio_lnode_exit(ln);
689 	scsi_host_put(shost);
690 }
691 
692 struct csio_lnode *
693 csio_lnode_alloc(struct csio_hw *hw)
694 {
695 	return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
696 }
697 
698 void
699 csio_lnodes_block_request(struct csio_hw *hw)
700 {
701 	struct Scsi_Host  *shost;
702 	struct csio_lnode *sln;
703 	struct csio_lnode *ln;
704 	struct list_head *cur_ln, *cur_cln;
705 	struct csio_lnode **lnode_list;
706 	int cur_cnt = 0, ii;
707 
708 	lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
709 			GFP_KERNEL);
710 	if (!lnode_list) {
711 		csio_err(hw, "Failed to allocate lnodes_list");
712 		return;
713 	}
714 
715 	spin_lock_irq(&hw->lock);
716 	/* Traverse sibling lnodes */
717 	list_for_each(cur_ln, &hw->sln_head) {
718 		sln = (struct csio_lnode *) cur_ln;
719 		lnode_list[cur_cnt++] = sln;
720 
721 		/* Traverse children lnodes */
722 		list_for_each(cur_cln, &sln->cln_head)
723 			lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
724 	}
725 	spin_unlock_irq(&hw->lock);
726 
727 	for (ii = 0; ii < cur_cnt; ii++) {
728 		csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
729 		ln = lnode_list[ii];
730 		shost = csio_ln_to_shost(ln);
731 		scsi_block_requests(shost);
732 
733 	}
734 	kfree(lnode_list);
735 }
736 
737 void
738 csio_lnodes_unblock_request(struct csio_hw *hw)
739 {
740 	struct csio_lnode *ln;
741 	struct Scsi_Host  *shost;
742 	struct csio_lnode *sln;
743 	struct list_head *cur_ln, *cur_cln;
744 	struct csio_lnode **lnode_list;
745 	int cur_cnt = 0, ii;
746 
747 	lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
748 			GFP_KERNEL);
749 	if (!lnode_list) {
750 		csio_err(hw, "Failed to allocate lnodes_list");
751 		return;
752 	}
753 
754 	spin_lock_irq(&hw->lock);
755 	/* Traverse sibling lnodes */
756 	list_for_each(cur_ln, &hw->sln_head) {
757 		sln = (struct csio_lnode *) cur_ln;
758 		lnode_list[cur_cnt++] = sln;
759 
760 		/* Traverse children lnodes */
761 		list_for_each(cur_cln, &sln->cln_head)
762 			lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
763 	}
764 	spin_unlock_irq(&hw->lock);
765 
766 	for (ii = 0; ii < cur_cnt; ii++) {
767 		csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
768 		ln = lnode_list[ii];
769 		shost = csio_ln_to_shost(ln);
770 		scsi_unblock_requests(shost);
771 	}
772 	kfree(lnode_list);
773 }
774 
775 void
776 csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
777 {
778 	struct csio_lnode *ln;
779 	struct Scsi_Host  *shost;
780 	struct csio_lnode *sln;
781 	struct list_head *cur_ln, *cur_cln;
782 	struct csio_lnode **lnode_list;
783 	int cur_cnt = 0, ii;
784 
785 	lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
786 			GFP_KERNEL);
787 	if (!lnode_list) {
788 		csio_err(hw, "Failed to allocate lnodes_list");
789 		return;
790 	}
791 
792 	spin_lock_irq(&hw->lock);
793 	/* Traverse sibling lnodes */
794 	list_for_each(cur_ln, &hw->sln_head) {
795 		sln = (struct csio_lnode *) cur_ln;
796 		if (sln->portid != portid)
797 			continue;
798 
799 		lnode_list[cur_cnt++] = sln;
800 
801 		/* Traverse children lnodes */
802 		list_for_each(cur_cln, &sln->cln_head)
803 			lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
804 	}
805 	spin_unlock_irq(&hw->lock);
806 
807 	for (ii = 0; ii < cur_cnt; ii++) {
808 		csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
809 		ln = lnode_list[ii];
810 		shost = csio_ln_to_shost(ln);
811 		scsi_block_requests(shost);
812 	}
813 	kfree(lnode_list);
814 }
815 
816 void
817 csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
818 {
819 	struct csio_lnode *ln;
820 	struct Scsi_Host  *shost;
821 	struct csio_lnode *sln;
822 	struct list_head *cur_ln, *cur_cln;
823 	struct csio_lnode **lnode_list;
824 	int cur_cnt = 0, ii;
825 
826 	lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
827 			GFP_KERNEL);
828 	if (!lnode_list) {
829 		csio_err(hw, "Failed to allocate lnodes_list");
830 		return;
831 	}
832 
833 	spin_lock_irq(&hw->lock);
834 	/* Traverse sibling lnodes */
835 	list_for_each(cur_ln, &hw->sln_head) {
836 		sln = (struct csio_lnode *) cur_ln;
837 		if (sln->portid != portid)
838 			continue;
839 		lnode_list[cur_cnt++] = sln;
840 
841 		/* Traverse children lnodes */
842 		list_for_each(cur_cln, &sln->cln_head)
843 			lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
844 	}
845 	spin_unlock_irq(&hw->lock);
846 
847 	for (ii = 0; ii < cur_cnt; ii++) {
848 		csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
849 		ln = lnode_list[ii];
850 		shost = csio_ln_to_shost(ln);
851 		scsi_unblock_requests(shost);
852 	}
853 	kfree(lnode_list);
854 }
855 
856 void
857 csio_lnodes_exit(struct csio_hw *hw, bool npiv)
858 {
859 	struct csio_lnode *sln;
860 	struct csio_lnode *ln;
861 	struct list_head *cur_ln, *cur_cln;
862 	struct csio_lnode **lnode_list;
863 	int cur_cnt = 0, ii;
864 
865 	lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
866 			GFP_KERNEL);
867 	if (!lnode_list) {
868 		csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
869 		return;
870 	}
871 
872 	/* Get all child lnodes(NPIV ports) */
873 	spin_lock_irq(&hw->lock);
874 	list_for_each(cur_ln, &hw->sln_head) {
875 		sln = (struct csio_lnode *) cur_ln;
876 
877 		/* Traverse children lnodes */
878 		list_for_each(cur_cln, &sln->cln_head)
879 			lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
880 	}
881 	spin_unlock_irq(&hw->lock);
882 
883 	/* Delete NPIV lnodes */
884 	for (ii = 0; ii < cur_cnt; ii++) {
885 		csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
886 		ln = lnode_list[ii];
887 		fc_vport_terminate(ln->fc_vport);
888 	}
889 
890 	/* Delete only npiv lnodes */
891 	if (npiv)
892 		goto free_lnodes;
893 
894 	cur_cnt = 0;
895 	/* Get all physical lnodes */
896 	spin_lock_irq(&hw->lock);
897 	/* Traverse sibling lnodes */
898 	list_for_each(cur_ln, &hw->sln_head) {
899 		sln = (struct csio_lnode *) cur_ln;
900 		lnode_list[cur_cnt++] = sln;
901 	}
902 	spin_unlock_irq(&hw->lock);
903 
904 	/* Delete physical lnodes */
905 	for (ii = 0; ii < cur_cnt; ii++) {
906 		csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
907 		csio_shost_exit(lnode_list[ii]);
908 	}
909 
910 free_lnodes:
911 	kfree(lnode_list);
912 }
913 
914 /*
915  * csio_lnode_init_post: Set lnode attributes after starting HW.
916  * @ln: lnode.
917  *
918  */
919 static void
920 csio_lnode_init_post(struct csio_lnode *ln)
921 {
922 	struct Scsi_Host  *shost = csio_ln_to_shost(ln);
923 
924 	csio_fchost_attr_init(ln);
925 
926 	scsi_scan_host(shost);
927 }
928 
929 /*
930  * csio_probe_one - Instantiate this function.
931  * @pdev: PCI device
932  * @id: Device ID
933  *
934  * This is the .probe() callback of the driver. This function:
935  * - Initializes the PCI function by enabling MMIO, setting bus
936  *   mastership and setting DMA mask.
937  * - Allocates HW structure, DMA, memory resources, maps BARS to
938  *   host memory and initializes HW module.
939  * - Allocates lnode structure via scsi_host_alloc, initializes
940  *   shost, initialized lnode module and registers with SCSI ML
941  *   via scsi_host_add.
942  * - Enables interrupts, and starts the chip by kicking off the
943  *   HW state machine.
944  * - Once hardware is ready, initiated scan of the host via
945  *   scsi_scan_host.
946  */
947 static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
948 {
949 	int rv;
950 	int bars;
951 	int i;
952 	struct csio_hw *hw;
953 	struct csio_lnode *ln;
954 
955 	/* probe only T5 and T6 cards */
956 	if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)) &&
957 	    !csio_is_t6((pdev->device & CSIO_HW_CHIP_MASK)))
958 		return -ENODEV;
959 
960 	rv = csio_pci_init(pdev, &bars);
961 	if (rv)
962 		goto err;
963 
964 	hw = csio_hw_alloc(pdev);
965 	if (!hw) {
966 		rv = -ENODEV;
967 		goto err_pci_exit;
968 	}
969 
970 	if (!pcie_relaxed_ordering_enabled(pdev))
971 		hw->flags |= CSIO_HWF_ROOT_NO_RELAXED_ORDERING;
972 
973 	pci_set_drvdata(pdev, hw);
974 
975 	rv = csio_hw_start(hw);
976 	if (rv) {
977 		if (rv == -EINVAL) {
978 			dev_err(&pdev->dev,
979 				"Failed to start FW, continuing in debug mode.\n");
980 			return 0;
981 		}
982 		goto err_lnode_exit;
983 	}
984 
985 	sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
986 		    FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
987 		    FW_HDR_FW_VER_MINOR_G(hw->fwrev),
988 		    FW_HDR_FW_VER_MICRO_G(hw->fwrev),
989 		    FW_HDR_FW_VER_BUILD_G(hw->fwrev));
990 
991 	for (i = 0; i < hw->num_pports; i++) {
992 		ln = csio_shost_init(hw, &pdev->dev, true, NULL);
993 		if (!ln) {
994 			rv = -ENODEV;
995 			break;
996 		}
997 		/* Initialize portid */
998 		ln->portid = hw->pport[i].portid;
999 
1000 		spin_lock_irq(&hw->lock);
1001 		if (csio_lnode_start(ln) != 0)
1002 			rv = -ENODEV;
1003 		spin_unlock_irq(&hw->lock);
1004 
1005 		if (rv)
1006 			break;
1007 
1008 		csio_lnode_init_post(ln);
1009 	}
1010 
1011 	if (rv)
1012 		goto err_lnode_exit;
1013 
1014 	return 0;
1015 
1016 err_lnode_exit:
1017 	csio_lnodes_block_request(hw);
1018 	spin_lock_irq(&hw->lock);
1019 	csio_hw_stop(hw);
1020 	spin_unlock_irq(&hw->lock);
1021 	csio_lnodes_unblock_request(hw);
1022 	csio_lnodes_exit(hw, 0);
1023 	csio_hw_free(hw);
1024 err_pci_exit:
1025 	csio_pci_exit(pdev, &bars);
1026 err:
1027 	dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
1028 	return rv;
1029 }
1030 
1031 /*
1032  * csio_remove_one - Remove one instance of the driver at this PCI function.
1033  * @pdev: PCI device
1034  *
1035  * Used during hotplug operation.
1036  */
1037 static void csio_remove_one(struct pci_dev *pdev)
1038 {
1039 	struct csio_hw *hw = pci_get_drvdata(pdev);
1040 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1041 
1042 	csio_lnodes_block_request(hw);
1043 	spin_lock_irq(&hw->lock);
1044 
1045 	/* Stops lnode, Rnode s/m
1046 	 * Quiesce IOs.
1047 	 * All sessions with remote ports are unregistered.
1048 	 */
1049 	csio_hw_stop(hw);
1050 	spin_unlock_irq(&hw->lock);
1051 	csio_lnodes_unblock_request(hw);
1052 
1053 	csio_lnodes_exit(hw, 0);
1054 	csio_hw_free(hw);
1055 	csio_pci_exit(pdev, &bars);
1056 }
1057 
1058 /*
1059  * csio_pci_error_detected - PCI error was detected
1060  * @pdev: PCI device
1061  *
1062  */
1063 static pci_ers_result_t
1064 csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1065 {
1066 	struct csio_hw *hw = pci_get_drvdata(pdev);
1067 
1068 	csio_lnodes_block_request(hw);
1069 	spin_lock_irq(&hw->lock);
1070 
1071 	/* Post PCI error detected evt to HW s/m
1072 	 * HW s/m handles this evt by quiescing IOs, unregisters rports
1073 	 * and finally takes the device to offline.
1074 	 */
1075 	csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
1076 	spin_unlock_irq(&hw->lock);
1077 	csio_lnodes_unblock_request(hw);
1078 	csio_lnodes_exit(hw, 0);
1079 	csio_intr_disable(hw, true);
1080 	pci_disable_device(pdev);
1081 	return state == pci_channel_io_perm_failure ?
1082 		PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1083 }
1084 
1085 /*
1086  * csio_pci_slot_reset - PCI slot has been reset.
1087  * @pdev: PCI device
1088  *
1089  */
1090 static pci_ers_result_t
1091 csio_pci_slot_reset(struct pci_dev *pdev)
1092 {
1093 	struct csio_hw *hw = pci_get_drvdata(pdev);
1094 	int ready;
1095 
1096 	if (pci_enable_device(pdev)) {
1097 		dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
1098 		return PCI_ERS_RESULT_DISCONNECT;
1099 	}
1100 
1101 	pci_set_master(pdev);
1102 	pci_restore_state(pdev);
1103 	pci_save_state(pdev);
1104 
1105 	/* Bring HW s/m to ready state.
1106 	 * but don't resume IOs.
1107 	 */
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);
1112 
1113 	if (ready) {
1114 		return PCI_ERS_RESULT_RECOVERED;
1115 	} else {
1116 		dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
1117 		return PCI_ERS_RESULT_DISCONNECT;
1118 	}
1119 }
1120 
1121 /*
1122  * csio_pci_resume - Resume normal operations
1123  * @pdev: PCI device
1124  *
1125  */
1126 static void
1127 csio_pci_resume(struct pci_dev *pdev)
1128 {
1129 	struct csio_hw *hw = pci_get_drvdata(pdev);
1130 	struct csio_lnode *ln;
1131 	int rv = 0;
1132 	int i;
1133 
1134 	/* Bring the LINK UP and Resume IO */
1135 
1136 	for (i = 0; i < hw->num_pports; i++) {
1137 		ln = csio_shost_init(hw, &pdev->dev, true, NULL);
1138 		if (!ln) {
1139 			rv = -ENODEV;
1140 			break;
1141 		}
1142 		/* Initialize portid */
1143 		ln->portid = hw->pport[i].portid;
1144 
1145 		spin_lock_irq(&hw->lock);
1146 		if (csio_lnode_start(ln) != 0)
1147 			rv = -ENODEV;
1148 		spin_unlock_irq(&hw->lock);
1149 
1150 		if (rv)
1151 			break;
1152 
1153 		csio_lnode_init_post(ln);
1154 	}
1155 
1156 	if (rv)
1157 		goto err_resume_exit;
1158 
1159 	return;
1160 
1161 err_resume_exit:
1162 	csio_lnodes_block_request(hw);
1163 	spin_lock_irq(&hw->lock);
1164 	csio_hw_stop(hw);
1165 	spin_unlock_irq(&hw->lock);
1166 	csio_lnodes_unblock_request(hw);
1167 	csio_lnodes_exit(hw, 0);
1168 	csio_hw_free(hw);
1169 	dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
1170 }
1171 
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,
1176 };
1177 
1178 /*
1179  *  Macros needed to support the PCI Device ID Table ...
1180  */
1181 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
1182 	static const struct pci_device_id csio_pci_tbl[] = {
1183 /* Define for FCoE uses PF6 */
1184 #define CH_PCI_DEVICE_ID_FUNCTION	0x6
1185 
1186 #define CH_PCI_ID_TABLE_ENTRY(devid) \
1187 		{ PCI_VDEVICE(CHELSIO, (devid)), 0 }
1188 
1189 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
1190 
1191 #include "t4_pci_id_tbl.h"
1192 
1193 static struct pci_driver csio_pci_driver = {
1194 	.name		= KBUILD_MODNAME,
1195 	.driver		= {
1196 		.owner	= THIS_MODULE,
1197 	},
1198 	.id_table	= csio_pci_tbl,
1199 	.probe		= csio_probe_one,
1200 	.remove		= csio_remove_one,
1201 	.err_handler	= &csio_err_handler,
1202 };
1203 
1204 /*
1205  * csio_init - Chelsio storage driver initialization function.
1206  *
1207  */
1208 static int __init
1209 csio_init(void)
1210 {
1211 	int rv = -ENOMEM;
1212 
1213 	pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
1214 
1215 	csio_dfs_init();
1216 
1217 	csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
1218 	if (!csio_fcoe_transport)
1219 		goto err;
1220 
1221 	csio_fcoe_transport_vport =
1222 			fc_attach_transport(&csio_fc_transport_vport_funcs);
1223 	if (!csio_fcoe_transport_vport)
1224 		goto err_vport;
1225 
1226 	rv = pci_register_driver(&csio_pci_driver);
1227 	if (rv)
1228 		goto err_pci;
1229 
1230 	return 0;
1231 
1232 err_pci:
1233 	fc_release_transport(csio_fcoe_transport_vport);
1234 err_vport:
1235 	fc_release_transport(csio_fcoe_transport);
1236 err:
1237 	csio_dfs_exit();
1238 	return rv;
1239 }
1240 
1241 /*
1242  * csio_exit - Chelsio storage driver uninitialization .
1243  *
1244  * Function that gets called in the unload path.
1245  */
1246 static void __exit
1247 csio_exit(void)
1248 {
1249 	pci_unregister_driver(&csio_pci_driver);
1250 	csio_dfs_exit();
1251 	fc_release_transport(csio_fcoe_transport_vport);
1252 	fc_release_transport(csio_fcoe_transport);
1253 }
1254 
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("Dual BSD/GPL");
1260 MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
1261 MODULE_VERSION(CSIO_DRV_VERSION);
1262 MODULE_FIRMWARE(FW_FNAME_T5);
1263 MODULE_FIRMWARE(FW_FNAME_T6);
1264