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