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