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