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