summaryrefslogtreecommitdiff
path: root/drivers/scsi/elx/efct/efct_xport.c
blob: 9495cedcc0b95af9e6d18815e14e86914cb5e597 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
 */

#include "efct_driver.h"
#include "efct_unsol.h"

static struct dentry *efct_debugfs_root;
static atomic_t efct_debugfs_count;

static struct scsi_host_template efct_template = {
	.module			= THIS_MODULE,
	.name			= EFCT_DRIVER_NAME,
	.supported_mode		= MODE_TARGET,
};

/* globals */
static struct fc_function_template efct_xport_functions;
static struct fc_function_template efct_vport_functions;

static struct scsi_transport_template *efct_xport_fc_tt;
static struct scsi_transport_template *efct_vport_fc_tt;

struct efct_xport *
efct_xport_alloc(struct efct *efct)
{
	struct efct_xport *xport;

	xport = kzalloc(sizeof(*xport), GFP_KERNEL);
	if (!xport)
		return xport;

	xport->efct = efct;
	return xport;
}

static int
efct_xport_init_debugfs(struct efct *efct)
{
	/* Setup efct debugfs root directory */
	if (!efct_debugfs_root) {
		efct_debugfs_root = debugfs_create_dir("efct", NULL);
		atomic_set(&efct_debugfs_count, 0);
	}

	/* Create a directory for sessions in root */
	if (!efct->sess_debugfs_dir) {
		efct->sess_debugfs_dir = debugfs_create_dir("sessions",
							efct_debugfs_root);
		if (IS_ERR(efct->sess_debugfs_dir)) {
			efc_log_err(efct,
				    "failed to create debugfs entry for sessions\n");
			goto debugfs_fail;
		}
		atomic_inc(&efct_debugfs_count);
	}

	return 0;

debugfs_fail:
	return -EIO;
}

static void efct_xport_delete_debugfs(struct efct *efct)
{
	/* Remove session debugfs directory */
	debugfs_remove(efct->sess_debugfs_dir);
	efct->sess_debugfs_dir = NULL;
	atomic_dec(&efct_debugfs_count);

	if (atomic_read(&efct_debugfs_count) == 0) {
		/* remove root debugfs directory */
		debugfs_remove(efct_debugfs_root);
		efct_debugfs_root = NULL;
	}
}

int
efct_xport_attach(struct efct_xport *xport)
{
	struct efct *efct = xport->efct;
	int rc;

	rc = efct_hw_setup(&efct->hw, efct, efct->pci);
	if (rc) {
		efc_log_err(efct, "%s: Can't setup hardware\n", efct->desc);
		return rc;
	}

	efct_hw_parse_filter(&efct->hw, (void *)efct->filter_def);

	xport->io_pool = efct_io_pool_create(efct, efct->hw.config.n_sgl);
	if (!xport->io_pool) {
		efc_log_err(efct, "Can't allocate IO pool\n");
		return -ENOMEM;
	}

	return 0;
}

static void
efct_xport_link_stats_cb(int status, u32 num_counters,
			 struct efct_hw_link_stat_counts *counters, void *arg)
{
	union efct_xport_stats_u *result = arg;

	result->stats.link_stats.link_failure_error_count =
		counters[EFCT_HW_LINK_STAT_LINK_FAILURE_COUNT].counter;
	result->stats.link_stats.loss_of_sync_error_count =
		counters[EFCT_HW_LINK_STAT_LOSS_OF_SYNC_COUNT].counter;
	result->stats.link_stats.primitive_sequence_error_count =
		counters[EFCT_HW_LINK_STAT_PRIMITIVE_SEQ_COUNT].counter;
	result->stats.link_stats.invalid_transmission_word_error_count =
		counters[EFCT_HW_LINK_STAT_INVALID_XMIT_WORD_COUNT].counter;
	result->stats.link_stats.crc_error_count =
		counters[EFCT_HW_LINK_STAT_CRC_COUNT].counter;

	complete(&result->stats.done);
}

static void
efct_xport_host_stats_cb(int status, u32 num_counters,
			 struct efct_hw_host_stat_counts *counters, void *arg)
{
	union efct_xport_stats_u *result = arg;

	result->stats.host_stats.transmit_kbyte_count =
		counters[EFCT_HW_HOST_STAT_TX_KBYTE_COUNT].counter;
	result->stats.host_stats.receive_kbyte_count =
		counters[EFCT_HW_HOST_STAT_RX_KBYTE_COUNT].counter;
	result->stats.host_stats.transmit_frame_count =
		counters[EFCT_HW_HOST_STAT_TX_FRAME_COUNT].counter;
	result->stats.host_stats.receive_frame_count =
		counters[EFCT_HW_HOST_STAT_RX_FRAME_COUNT].counter;

	complete(&result->stats.done);
}

static void
efct_xport_async_link_stats_cb(int status, u32 num_counters,
			       struct efct_hw_link_stat_counts *counters,
			       void *arg)
{
	union efct_xport_stats_u *result = arg;

	result->stats.link_stats.link_failure_error_count =
		counters[EFCT_HW_LINK_STAT_LINK_FAILURE_COUNT].counter;
	result->stats.link_stats.loss_of_sync_error_count =
		counters[EFCT_HW_LINK_STAT_LOSS_OF_SYNC_COUNT].counter;
	result->stats.link_stats.primitive_sequence_error_count =
		counters[EFCT_HW_LINK_STAT_PRIMITIVE_SEQ_COUNT].counter;
	result->stats.link_stats.invalid_transmission_word_error_count =
		counters[EFCT_HW_LINK_STAT_INVALID_XMIT_WORD_COUNT].counter;
	result->stats.link_stats.crc_error_count =
		counters[EFCT_HW_LINK_STAT_CRC_COUNT].counter;
}

static void
efct_xport_async_host_stats_cb(int status, u32 num_counters,
			       struct efct_hw_host_stat_counts *counters,
			       void *arg)
{
	union efct_xport_stats_u *result = arg;

	result->stats.host_stats.transmit_kbyte_count =
		counters[EFCT_HW_HOST_STAT_TX_KBYTE_COUNT].counter;
	result->stats.host_stats.receive_kbyte_count =
		counters[EFCT_HW_HOST_STAT_RX_KBYTE_COUNT].counter;
	result->stats.host_stats.transmit_frame_count =
		counters[EFCT_HW_HOST_STAT_TX_FRAME_COUNT].counter;
	result->stats.host_stats.receive_frame_count =
		counters[EFCT_HW_HOST_STAT_RX_FRAME_COUNT].counter;
}

static void
efct_xport_config_stats_timer(struct efct *efct);

static void
efct_xport_stats_timer_cb(struct timer_list *t)
{
	struct efct_xport *xport = from_timer(xport, t, stats_timer);
	struct efct *efct = xport->efct;

	efct_xport_config_stats_timer(efct);
}

static void
efct_xport_config_stats_timer(struct efct *efct)
{
	u32 timeout = 3 * 1000;
	struct efct_xport *xport = NULL;

	if (!efct) {
		pr_err("%s: failed to locate EFCT device\n", __func__);
		return;
	}

	xport = efct->xport;
	efct_hw_get_link_stats(&efct->hw, 0, 0, 0,
			       efct_xport_async_link_stats_cb,
			       &xport->fc_xport_stats);
	efct_hw_get_host_stats(&efct->hw, 0, efct_xport_async_host_stats_cb,
			       &xport->fc_xport_stats);

	timer_setup(&xport->stats_timer,
		    &efct_xport_stats_timer_cb, 0);
	mod_timer(&xport->stats_timer,
		  jiffies + msecs_to_jiffies(timeout));
}

int
efct_xport_initialize(struct efct_xport *xport)
{
	struct efct *efct = xport->efct;
	int rc = 0;

	/* Initialize io lists */
	spin_lock_init(&xport->io_pending_lock);
	INIT_LIST_HEAD(&xport->io_pending_list);
	atomic_set(&xport->io_active_count, 0);
	atomic_set(&xport->io_pending_count, 0);
	atomic_set(&xport->io_total_free, 0);
	atomic_set(&xport->io_total_pending, 0);
	atomic_set(&xport->io_alloc_failed_count, 0);
	atomic_set(&xport->io_pending_recursing, 0);

	rc = efct_hw_init(&efct->hw);
	if (rc) {
		efc_log_err(efct, "efct_hw_init failure\n");
		goto out;
	}

	rc = efct_scsi_tgt_new_device(efct);
	if (rc) {
		efc_log_err(efct, "failed to initialize target\n");
		goto hw_init_out;
	}

	rc = efct_scsi_new_device(efct);
	if (rc) {
		efc_log_err(efct, "failed to initialize initiator\n");
		goto tgt_dev_out;
	}

	/* Get FC link and host statistics perodically*/
	efct_xport_config_stats_timer(efct);

	efct_xport_init_debugfs(efct);

	return rc;

tgt_dev_out:
	efct_scsi_tgt_del_device(efct);

hw_init_out:
	efct_hw_teardown(&efct->hw);
out:
	return rc;
}

int
efct_xport_status(struct efct_xport *xport, enum efct_xport_status cmd,
		  union efct_xport_stats_u *result)
{
	int rc = 0;
	struct efct *efct = NULL;
	union efct_xport_stats_u value;

	efct = xport->efct;

	switch (cmd) {
	case EFCT_XPORT_CONFIG_PORT_STATUS:
		if (xport->configured_link_state == 0) {
			/*
			 * Initial state is offline. configured_link_state is
			 * set to online explicitly when port is brought online
			 */
			xport->configured_link_state = EFCT_XPORT_PORT_OFFLINE;
		}
		result->value = xport->configured_link_state;
		break;

	case EFCT_XPORT_PORT_STATUS:
		/* Determine port status based on link speed. */
		value.value = efct_hw_get_link_speed(&efct->hw);
		if (value.value == 0)
			result->value = EFCT_XPORT_PORT_OFFLINE;
		else
			result->value = EFCT_XPORT_PORT_ONLINE;
		break;

	case EFCT_XPORT_LINK_SPEED:
		result->value = efct_hw_get_link_speed(&efct->hw);
		break;

	case EFCT_XPORT_LINK_STATISTICS:
		memcpy((void *)result, &efct->xport->fc_xport_stats,
		       sizeof(union efct_xport_stats_u));
		break;
	case EFCT_XPORT_LINK_STAT_RESET: {
		/* Create a completion to synchronize the stat reset process */
		init_completion(&result->stats.done);

		/* First reset the link stats */
		rc = efct_hw_get_link_stats(&efct->hw, 0, 1, 1,
					    efct_xport_link_stats_cb, result);
		if (rc)
			break;

		/* Wait for completion to be signaled when the cmd completes */
		if (wait_for_completion_interruptible(&result->stats.done)) {
			/* Undefined failure */
			efc_log_debug(efct, "sem wait failed\n");
			rc = -EIO;
			break;
		}

		/* Next reset the host stats */
		rc = efct_hw_get_host_stats(&efct->hw, 1,
					    efct_xport_host_stats_cb, result);

		if (rc)
			break;

		/* Wait for completion to be signaled when the cmd completes */
		if (wait_for_completion_interruptible(&result->stats.done)) {
			/* Undefined failure */
			efc_log_debug(efct, "sem wait failed\n");
			rc = -EIO;
			break;
		}
		break;
	}
	default:
		rc = -EIO;
		break;
	}

	return rc;
}

static int
efct_get_link_supported_speeds(struct efct *efct)
{
	u32 supported_speeds = 0;
	u32 link_module_type, i;
	struct {
		u32 lmt_speed;
		u32 speed;
	} supported_speed_list[] = {
		{SLI4_LINK_MODULE_TYPE_1GB, FC_PORTSPEED_1GBIT},
		{SLI4_LINK_MODULE_TYPE_2GB, FC_PORTSPEED_2GBIT},
		{SLI4_LINK_MODULE_TYPE_4GB, FC_PORTSPEED_4GBIT},
		{SLI4_LINK_MODULE_TYPE_8GB, FC_PORTSPEED_8GBIT},
		{SLI4_LINK_MODULE_TYPE_16GB, FC_PORTSPEED_16GBIT},
		{SLI4_LINK_MODULE_TYPE_32GB, FC_PORTSPEED_32GBIT},
		{SLI4_LINK_MODULE_TYPE_64GB, FC_PORTSPEED_64GBIT},
		{SLI4_LINK_MODULE_TYPE_128GB, FC_PORTSPEED_128GBIT},
	};

	link_module_type = sli_get_lmt(&efct->hw.sli);

	/* populate link supported speeds */
	for (i = 0; i < ARRAY_SIZE(supported_speed_list); i++) {
		if (link_module_type & supported_speed_list[i].lmt_speed)
			supported_speeds |= supported_speed_list[i].speed;
	}

	return supported_speeds;
}

int
efct_scsi_new_device(struct efct *efct)
{
	struct Scsi_Host *shost = NULL;
	int error = 0;
	struct efct_vport *vport = NULL;

	shost = scsi_host_alloc(&efct_template, sizeof(*vport));
	if (!shost) {
		efc_log_err(efct, "failed to allocate Scsi_Host struct\n");
		return -ENOMEM;
	}

	/* save shost to initiator-client context */
	efct->shost = shost;

	/* save efct information to shost LLD-specific space */
	vport = (struct efct_vport *)shost->hostdata;
	vport->efct = efct;

	/*
	 * Set initial can_queue value to the max SCSI IOs. This is the maximum
	 * global queue depth (as opposed to the per-LUN queue depth --
	 * .cmd_per_lun This may need to be adjusted for I+T mode.
	 */
	shost->can_queue = efct->hw.config.n_io;
	shost->max_cmd_len = 16; /* 16-byte CDBs */
	shost->max_id = 0xffff;
	shost->max_lun = 0xffffffff;

	/*
	 * can only accept (from mid-layer) as many SGEs as we've
	 * pre-registered
	 */
	shost->sg_tablesize = sli_get_max_sgl(&efct->hw.sli);

	/* attach FC Transport template to shost */
	shost->transportt = efct_xport_fc_tt;
	efc_log_debug(efct, "transport template=%p\n", efct_xport_fc_tt);

	/* get pci_dev structure and add host to SCSI ML */
	error = scsi_add_host_with_dma(shost, &efct->pci->dev,
				       &efct->pci->dev);
	if (error) {
		efc_log_debug(efct, "failed scsi_add_host_with_dma\n");
		return -EIO;
	}

	/* Set symbolic name for host port */
	snprintf(fc_host_symbolic_name(shost),
		 sizeof(fc_host_symbolic_name(shost)),
		     "Emulex %s FV%s DV%s", efct->model,
		     efct->hw.sli.fw_name[0], EFCT_DRIVER_VERSION);

	/* Set host port supported classes */
	fc_host_supported_classes(shost) = FC_COS_CLASS3;

	fc_host_supported_speeds(shost) = efct_get_link_supported_speeds(efct);

	fc_host_node_name(shost) = efct_get_wwnn(&efct->hw);
	fc_host_port_name(shost) = efct_get_wwpn(&efct->hw);
	fc_host_max_npiv_vports(shost) = 128;

	return 0;
}

struct scsi_transport_template *
efct_attach_fc_transport(void)
{
	struct scsi_transport_template *efct_fc_template = NULL;

	efct_fc_template = fc_attach_transport(&efct_xport_functions);

	if (!efct_fc_template)
		pr_err("failed to attach EFCT with fc transport\n");

	return efct_fc_template;
}

struct scsi_transport_template *
efct_attach_vport_fc_transport(void)
{
	struct scsi_transport_template *efct_fc_template = NULL;

	efct_fc_template = fc_attach_transport(&efct_vport_functions);

	if (!efct_fc_template)
		pr_err("failed to attach EFCT with fc transport\n");

	return efct_fc_template;
}

int
efct_scsi_reg_fc_transport(void)
{
	/* attach to appropriate scsi_tranport_* module */
	efct_xport_fc_tt = efct_attach_fc_transport();
	if (!efct_xport_fc_tt) {
		pr_err("%s: failed to attach to scsi_transport_*", __func__);
		return -EIO;
	}

	efct_vport_fc_tt = efct_attach_vport_fc_transport();
	if (!efct_vport_fc_tt) {
		pr_err("%s: failed to attach to scsi_transport_*", __func__);
		efct_release_fc_transport(efct_xport_fc_tt);
		efct_xport_fc_tt = NULL;
		return -EIO;
	}

	return 0;
}

void
efct_scsi_release_fc_transport(void)
{
	/* detach from scsi_transport_* */
	efct_release_fc_transport(efct_xport_fc_tt);
	efct_xport_fc_tt = NULL;
	if (efct_vport_fc_tt)
		efct_release_fc_transport(efct_vport_fc_tt);

	efct_vport_fc_tt = NULL;
}

void
efct_xport_detach(struct efct_xport *xport)
{
	struct efct *efct = xport->efct;

	/* free resources associated with target-server and initiator-client */
	efct_scsi_tgt_del_device(efct);

	efct_scsi_del_device(efct);

	/*Shutdown FC Statistics timer*/
	if (timer_pending(&xport->stats_timer))
		del_timer(&xport->stats_timer);

	efct_hw_teardown(&efct->hw);

	efct_xport_delete_debugfs(efct);
}

static void
efct_xport_domain_free_cb(struct efc *efc, void *arg)
{
	struct completion *done = arg;

	complete(done);
}

int
efct_xport_control(struct efct_xport *xport, enum efct_xport_ctrl cmd, ...)
{
	u32 rc = 0;
	struct efct *efct = NULL;
	va_list argp;

	efct = xport->efct;

	switch (cmd) {
	case EFCT_XPORT_PORT_ONLINE: {
		/* Bring the port on-line */
		rc = efct_hw_port_control(&efct->hw, EFCT_HW_PORT_INIT, 0,
					  NULL, NULL);
		if (rc)
			efc_log_err(efct,
				    "%s: Can't init port\n", efct->desc);
		else
			xport->configured_link_state = cmd;
		break;
	}
	case EFCT_XPORT_PORT_OFFLINE: {
		if (efct_hw_port_control(&efct->hw, EFCT_HW_PORT_SHUTDOWN, 0,
					 NULL, NULL))
			efc_log_err(efct, "port shutdown failed\n");
		else
			xport->configured_link_state = cmd;
		break;
	}

	case EFCT_XPORT_SHUTDOWN: {
		struct completion done;
		unsigned long timeout;

		/* if a PHYSDEV reset was performed (e.g. hw dump), will affect
		 * all PCI functions; orderly shutdown won't work,
		 * just force free
		 */
		if (sli_reset_required(&efct->hw.sli)) {
			struct efc_domain *domain = efct->efcport->domain;

			if (domain)
				efc_domain_cb(efct->efcport, EFC_HW_DOMAIN_LOST,
					      domain);
		} else {
			efct_hw_port_control(&efct->hw, EFCT_HW_PORT_SHUTDOWN,
					     0, NULL, NULL);
		}

		init_completion(&done);

		efc_register_domain_free_cb(efct->efcport,
					    efct_xport_domain_free_cb, &done);

		efc_log_debug(efct, "Waiting %d seconds for domain shutdown\n",
			      (EFC_SHUTDOWN_TIMEOUT_USEC / 1000000));

		timeout = usecs_to_jiffies(EFC_SHUTDOWN_TIMEOUT_USEC);
		if (!wait_for_completion_timeout(&done, timeout)) {
			efc_log_err(efct, "Domain shutdown timed out!!\n");
			WARN_ON(1);
		}

		efc_register_domain_free_cb(efct->efcport, NULL, NULL);

		/* Free up any saved virtual ports */
		efc_vport_del_all(efct->efcport);
		break;
	}

	/*
	 * Set wwnn for the port. This will be used instead of the default
	 * provided by FW.
	 */
	case EFCT_XPORT_WWNN_SET: {
		u64 wwnn;

		/* Retrieve arguments */
		va_start(argp, cmd);
		wwnn = va_arg(argp, uint64_t);
		va_end(argp);

		efc_log_debug(efct, " WWNN %016llx\n", wwnn);
		xport->req_wwnn = wwnn;

		break;
	}
	/*
	 * Set wwpn for the port. This will be used instead of the default
	 * provided by FW.
	 */
	case EFCT_XPORT_WWPN_SET: {
		u64 wwpn;

		/* Retrieve arguments */
		va_start(argp, cmd);
		wwpn = va_arg(argp, uint64_t);
		va_end(argp);

		efc_log_debug(efct, " WWPN %016llx\n", wwpn);
		xport->req_wwpn = wwpn;

		break;
	}

	default:
		break;
	}
	return rc;
}

void
efct_xport_free(struct efct_xport *xport)
{
	if (xport) {
		efct_io_pool_free(xport->io_pool);

		kfree(xport);
	}
}

void
efct_release_fc_transport(struct scsi_transport_template *transport_template)
{
	if (transport_template)
		pr_err("releasing transport layer\n");

	/* Releasing FC transport */
	fc_release_transport(transport_template);
}

static void
efct_xport_remove_host(struct Scsi_Host *shost)
{
	fc_remove_host(shost);
}

void
efct_scsi_del_device(struct efct *efct)
{
	if (!efct->shost)
		return;

	efc_log_debug(efct, "Unregistering with Transport Layer\n");
	efct_xport_remove_host(efct->shost);
	efc_log_debug(efct, "Unregistering with SCSI Midlayer\n");
	scsi_remove_host(efct->shost);
	scsi_host_put(efct->shost);
	efct->shost = NULL;
}

static void
efct_get_host_port_id(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	struct efc *efc = efct->efcport;
	struct efc_nport *nport;

	if (efc->domain && efc->domain->nport) {
		nport = efc->domain->nport;
		fc_host_port_id(shost) = nport->fc_id;
	}
}

static void
efct_get_host_port_type(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	struct efc *efc = efct->efcport;
	int type = FC_PORTTYPE_UNKNOWN;

	if (efc->domain && efc->domain->nport) {
		if (efc->domain->is_loop) {
			type = FC_PORTTYPE_LPORT;
		} else {
			struct efc_nport *nport = efc->domain->nport;

			if (nport->is_vport)
				type = FC_PORTTYPE_NPIV;
			else if (nport->topology == EFC_NPORT_TOPO_P2P)
				type = FC_PORTTYPE_PTP;
			else if (nport->topology == EFC_NPORT_TOPO_UNKNOWN)
				type = FC_PORTTYPE_UNKNOWN;
			else
				type = FC_PORTTYPE_NPORT;
		}
	}
	fc_host_port_type(shost) = type;
}

static void
efct_get_host_vport_type(struct Scsi_Host *shost)
{
	fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
}

static void
efct_get_host_port_state(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	union efct_xport_stats_u status;
	int rc;

	rc = efct_xport_status(efct->xport, EFCT_XPORT_PORT_STATUS, &status);
	if ((!rc) && (status.value == EFCT_XPORT_PORT_ONLINE))
		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
	else
		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
}

static void
efct_get_host_speed(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	struct efc *efc = efct->efcport;
	union efct_xport_stats_u speed;
	u32 fc_speed = FC_PORTSPEED_UNKNOWN;
	int rc;

	if (!efc->domain || !efc->domain->nport) {
		fc_host_speed(shost) = fc_speed;
		return;
	}

	rc = efct_xport_status(efct->xport, EFCT_XPORT_LINK_SPEED, &speed);
	if (!rc) {
		switch (speed.value) {
		case 1000:
			fc_speed = FC_PORTSPEED_1GBIT;
			break;
		case 2000:
			fc_speed = FC_PORTSPEED_2GBIT;
			break;
		case 4000:
			fc_speed = FC_PORTSPEED_4GBIT;
			break;
		case 8000:
			fc_speed = FC_PORTSPEED_8GBIT;
			break;
		case 10000:
			fc_speed = FC_PORTSPEED_10GBIT;
			break;
		case 16000:
			fc_speed = FC_PORTSPEED_16GBIT;
			break;
		case 32000:
			fc_speed = FC_PORTSPEED_32GBIT;
			break;
		case 64000:
			fc_speed = FC_PORTSPEED_64GBIT;
			break;
		case 128000:
			fc_speed = FC_PORTSPEED_128GBIT;
			break;
		}
	}

	fc_host_speed(shost) = fc_speed;
}

static void
efct_get_host_fabric_name(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	struct efc *efc = efct->efcport;

	if (efc->domain) {
		struct fc_els_flogi  *sp =
			(struct fc_els_flogi  *)
				efc->domain->flogi_service_params;

		fc_host_fabric_name(shost) = be64_to_cpu(sp->fl_wwnn);
	}
}

static struct fc_host_statistics *
efct_get_stats(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	union efct_xport_stats_u stats;
	struct efct_xport *xport = efct->xport;
	int rc = 0;

	rc = efct_xport_status(xport, EFCT_XPORT_LINK_STATISTICS, &stats);
	if (rc) {
		pr_err("efct_xport_status returned non 0 - %d\n", rc);
		return NULL;
	}

	vport->fc_host_stats.loss_of_sync_count =
		stats.stats.link_stats.loss_of_sync_error_count;
	vport->fc_host_stats.link_failure_count =
		stats.stats.link_stats.link_failure_error_count;
	vport->fc_host_stats.prim_seq_protocol_err_count =
		stats.stats.link_stats.primitive_sequence_error_count;
	vport->fc_host_stats.invalid_tx_word_count =
		stats.stats.link_stats.invalid_transmission_word_error_count;
	vport->fc_host_stats.invalid_crc_count =
		stats.stats.link_stats.crc_error_count;
	/* mbox returns kbyte count so we need to convert to words */
	vport->fc_host_stats.tx_words =
		stats.stats.host_stats.transmit_kbyte_count * 256;
	/* mbox returns kbyte count so we need to convert to words */
	vport->fc_host_stats.rx_words =
		stats.stats.host_stats.receive_kbyte_count * 256;
	vport->fc_host_stats.tx_frames =
		stats.stats.host_stats.transmit_frame_count;
	vport->fc_host_stats.rx_frames =
		stats.stats.host_stats.receive_frame_count;

	vport->fc_host_stats.fcp_input_requests =
			xport->fcp_stats.input_requests;
	vport->fc_host_stats.fcp_output_requests =
			xport->fcp_stats.output_requests;
	vport->fc_host_stats.fcp_output_megabytes =
			xport->fcp_stats.output_bytes >> 20;
	vport->fc_host_stats.fcp_input_megabytes =
			xport->fcp_stats.input_bytes >> 20;
	vport->fc_host_stats.fcp_control_requests =
			xport->fcp_stats.control_requests;

	return &vport->fc_host_stats;
}

static void
efct_reset_stats(struct Scsi_Host *shost)
{
	struct efct_vport *vport = (struct efct_vport *)shost->hostdata;
	struct efct *efct = vport->efct;
	/* argument has no purpose for this action */
	union efct_xport_stats_u dummy;
	int rc;

	rc = efct_xport_status(efct->xport, EFCT_XPORT_LINK_STAT_RESET, &dummy);
	if (rc)
		pr_err("efct_xport_status returned non 0 - %d\n", rc);
}

static int
efct_issue_lip(struct Scsi_Host *shost)
{
	struct efct_vport *vport =
			shost ? (struct efct_vport *)shost->hostdata : NULL;
	struct efct *efct = vport ? vport->efct : NULL;

	if (!shost || !vport || !efct) {
		pr_err("%s: shost=%p vport=%p efct=%p\n", __func__,
		       shost, vport, efct);
		return -EPERM;
	}

	/*
	 * Bring the link down gracefully then re-init the link.
	 * The firmware will re-initialize the Fibre Channel interface as
	 * required. It does not issue a LIP.
	 */

	if (efct_xport_control(efct->xport, EFCT_XPORT_PORT_OFFLINE))
		efc_log_debug(efct, "EFCT_XPORT_PORT_OFFLINE failed\n");

	if (efct_xport_control(efct->xport, EFCT_XPORT_PORT_ONLINE))
		efc_log_debug(efct, "EFCT_XPORT_PORT_ONLINE failed\n");

	return 0;
}

struct efct_vport *
efct_scsi_new_vport(struct efct *efct, struct device *dev)
{
	struct Scsi_Host *shost = NULL;
	int error = 0;
	struct efct_vport *vport = NULL;

	shost = scsi_host_alloc(&efct_template, sizeof(*vport));
	if (!shost) {
		efc_log_err(efct, "failed to allocate Scsi_Host struct\n");
		return NULL;
	}

	/* save efct information to shost LLD-specific space */
	vport = (struct efct_vport *)shost->hostdata;
	vport->efct = efct;
	vport->is_vport = true;

	shost->can_queue = efct->hw.config.n_io;
	shost->max_cmd_len = 16; /* 16-byte CDBs */
	shost->max_id = 0xffff;
	shost->max_lun = 0xffffffff;

	/* can only accept (from mid-layer) as many SGEs as we've pre-regited*/
	shost->sg_tablesize = sli_get_max_sgl(&efct->hw.sli);

	/* attach FC Transport template to shost */
	shost->transportt = efct_vport_fc_tt;
	efc_log_debug(efct, "vport transport template=%p\n",
		      efct_vport_fc_tt);

	/* get pci_dev structure and add host to SCSI ML */
	error = scsi_add_host_with_dma(shost, dev, &efct->pci->dev);
	if (error) {
		efc_log_debug(efct, "failed scsi_add_host_with_dma\n");
		return NULL;
	}

	/* Set symbolic name for host port */
	snprintf(fc_host_symbolic_name(shost),
		 sizeof(fc_host_symbolic_name(shost)),
		 "Emulex %s FV%s DV%s", efct->model, efct->hw.sli.fw_name[0],
		 EFCT_DRIVER_VERSION);

	/* Set host port supported classes */
	fc_host_supported_classes(shost) = FC_COS_CLASS3;

	fc_host_supported_speeds(shost) = efct_get_link_supported_speeds(efct);
	vport->shost = shost;

	return vport;
}

int efct_scsi_del_vport(struct efct *efct, struct Scsi_Host *shost)
{
	if (shost) {
		efc_log_debug(efct,
			      "Unregistering vport with Transport Layer\n");
		efct_xport_remove_host(shost);
		efc_log_debug(efct, "Unregistering vport with SCSI Midlayer\n");
		scsi_remove_host(shost);
		scsi_host_put(shost);
		return 0;
	}
	return -EIO;
}

static int
efct_vport_create(struct fc_vport *fc_vport, bool disable)
{
	struct Scsi_Host *shost = fc_vport ? fc_vport->shost : NULL;
	struct efct_vport *pport = shost ?
					(struct efct_vport *)shost->hostdata :
					NULL;
	struct efct *efct = pport ? pport->efct : NULL;
	struct efct_vport *vport = NULL;

	if (!fc_vport || !shost || !efct)
		goto fail;

	vport = efct_scsi_new_vport(efct, &fc_vport->dev);
	if (!vport) {
		efc_log_err(efct, "failed to create vport\n");
		goto fail;
	}

	vport->fc_vport = fc_vport;
	vport->npiv_wwpn = fc_vport->port_name;
	vport->npiv_wwnn = fc_vport->node_name;
	fc_host_node_name(vport->shost) = vport->npiv_wwnn;
	fc_host_port_name(vport->shost) = vport->npiv_wwpn;
	*(struct efct_vport **)fc_vport->dd_data = vport;

	return 0;

fail:
	return -EIO;
}

static int
efct_vport_delete(struct fc_vport *fc_vport)
{
	struct efct_vport *vport = *(struct efct_vport **)fc_vport->dd_data;
	struct Scsi_Host *shost = vport ? vport->shost : NULL;
	struct efct *efct = vport ? vport->efct : NULL;
	int rc;

	rc = efct_scsi_del_vport(efct, shost);

	if (rc)
		pr_err("%s: vport delete failed\n", __func__);

	return rc;
}

static int
efct_vport_disable(struct fc_vport *fc_vport, bool disable)
{
	return 0;
}

static struct fc_function_template efct_xport_functions = {
	.get_host_port_id = efct_get_host_port_id,
	.get_host_port_type = efct_get_host_port_type,
	.get_host_port_state = efct_get_host_port_state,
	.get_host_speed = efct_get_host_speed,
	.get_host_fabric_name = efct_get_host_fabric_name,

	.get_fc_host_stats = efct_get_stats,
	.reset_fc_host_stats = efct_reset_stats,

	.issue_fc_host_lip = efct_issue_lip,

	.vport_disable = efct_vport_disable,

	/* allocation lengths for host-specific data */
	.dd_fcrport_size = sizeof(struct efct_rport_data),
	.dd_fcvport_size = 128, /* should be sizeof(...) */

	/* remote port fixed attributes */
	.show_rport_maxframe_size = 1,
	.show_rport_supported_classes = 1,
	.show_rport_dev_loss_tmo = 1,

	/* target dynamic attributes */
	.show_starget_node_name = 1,
	.show_starget_port_name = 1,
	.show_starget_port_id = 1,

	/* host fixed attributes */
	.show_host_node_name = 1,
	.show_host_port_name = 1,
	.show_host_supported_classes = 1,
	.show_host_supported_fc4s = 1,
	.show_host_supported_speeds = 1,
	.show_host_maxframe_size = 1,

	/* host dynamic attributes */
	.show_host_port_id = 1,
	.show_host_port_type = 1,
	.show_host_port_state = 1,
	/* active_fc4s is shown but doesn't change (thus no get function) */
	.show_host_active_fc4s = 1,
	.show_host_speed = 1,
	.show_host_fabric_name = 1,
	.show_host_symbolic_name = 1,
	.vport_create = efct_vport_create,
	.vport_delete = efct_vport_delete,
};

static struct fc_function_template efct_vport_functions = {
	.get_host_port_id = efct_get_host_port_id,
	.get_host_port_type = efct_get_host_vport_type,
	.get_host_port_state = efct_get_host_port_state,
	.get_host_speed = efct_get_host_speed,
	.get_host_fabric_name = efct_get_host_fabric_name,

	.get_fc_host_stats = efct_get_stats,
	.reset_fc_host_stats = efct_reset_stats,

	.issue_fc_host_lip = efct_issue_lip,

	/* allocation lengths for host-specific data */
	.dd_fcrport_size = sizeof(struct efct_rport_data),
	.dd_fcvport_size = 128, /* should be sizeof(...) */

	/* remote port fixed attributes */
	.show_rport_maxframe_size = 1,
	.show_rport_supported_classes = 1,
	.show_rport_dev_loss_tmo = 1,

	/* target dynamic attributes */
	.show_starget_node_name = 1,
	.show_starget_port_name = 1,
	.show_starget_port_id = 1,

	/* host fixed attributes */
	.show_host_node_name = 1,
	.show_host_port_name = 1,
	.show_host_supported_classes = 1,
	.show_host_supported_fc4s = 1,
	.show_host_supported_speeds = 1,
	.show_host_maxframe_size = 1,

	/* host dynamic attributes */
	.show_host_port_id = 1,
	.show_host_port_type = 1,
	.show_host_port_state = 1,
	/* active_fc4s is shown but doesn't change (thus no get function) */
	.show_host_active_fc4s = 1,
	.show_host_speed = 1,
	.show_host_fabric_name = 1,
	.show_host_symbolic_name = 1,
};