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