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