summaryrefslogtreecommitdiff
path: root/drivers/scsi/lpfc/lpfc_ct.c
blob: baae1f8279e0cbe66128d815974803b56783f09e (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
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
 * Fibre Channel Host Bus Adapters.                                *
 * Copyright (C) 2017-2023 Broadcom. All Rights Reserved. The term *
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
 * EMULEX and SLI are trademarks of Emulex.                        *
 * www.broadcom.com                                                *
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
 * modify it under the terms of version 2 of the GNU General       *
 * Public License as published by the Free Software Foundation.    *
 * This program is distributed in the hope that it will be useful. *
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 * more details, a copy of which can be found in the file COPYING  *
 * included with this package.                                     *
 *******************************************************************/

/*
 * Fibre Channel SCSI LAN Device Driver CT support: FC Generic Services FC-GS
 */

#include <linux/blkdev.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/utsname.h>

#include <scsi/scsi.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport_fc.h>
#include <scsi/fc/fc_fs.h>

#include "lpfc_hw4.h"
#include "lpfc_hw.h"
#include "lpfc_sli.h"
#include "lpfc_sli4.h"
#include "lpfc_nl.h"
#include "lpfc_disc.h"
#include "lpfc.h"
#include "lpfc_scsi.h"
#include "lpfc_logmsg.h"
#include "lpfc_crtn.h"
#include "lpfc_version.h"
#include "lpfc_vport.h"
#include "lpfc_debugfs.h"

/* FDMI Port Speed definitions - FC-GS-7 */
#define HBA_PORTSPEED_1GFC		0x00000001	/* 1G FC */
#define HBA_PORTSPEED_2GFC		0x00000002	/* 2G FC */
#define HBA_PORTSPEED_4GFC		0x00000008	/* 4G FC */
#define HBA_PORTSPEED_10GFC		0x00000004	/* 10G FC */
#define HBA_PORTSPEED_8GFC		0x00000010	/* 8G FC */
#define HBA_PORTSPEED_16GFC		0x00000020	/* 16G FC */
#define HBA_PORTSPEED_32GFC		0x00000040	/* 32G FC */
#define HBA_PORTSPEED_20GFC		0x00000080	/* 20G FC */
#define HBA_PORTSPEED_40GFC		0x00000100	/* 40G FC */
#define HBA_PORTSPEED_128GFC		0x00000200	/* 128G FC */
#define HBA_PORTSPEED_64GFC		0x00000400	/* 64G FC */
#define HBA_PORTSPEED_256GFC		0x00000800	/* 256G FC */
#define HBA_PORTSPEED_UNKNOWN		0x00008000	/* Unknown */
#define HBA_PORTSPEED_10GE		0x00010000	/* 10G E */
#define HBA_PORTSPEED_40GE		0x00020000	/* 40G E */
#define HBA_PORTSPEED_100GE		0x00040000	/* 100G E */
#define HBA_PORTSPEED_25GE		0x00080000	/* 25G E */
#define HBA_PORTSPEED_50GE		0x00100000	/* 50G E */
#define HBA_PORTSPEED_400GE		0x00200000	/* 400G E */

#define FOURBYTES	4


static char *lpfc_release_version = LPFC_DRIVER_VERSION;
static void
lpfc_cmpl_ct_cmd_vmid(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		      struct lpfc_iocbq *rspiocb);

static void
lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
			  struct lpfc_dmabuf *mp, uint32_t size)
{
	if (!mp) {
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
				"0146 Ignoring unsolicited CT No HBQ "
				"status = x%x\n",
				get_job_ulpstatus(phba, piocbq));
	}
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
			"0145 Ignoring unsolicited CT HBQ Size:%d "
			"status = x%x\n",
			size, get_job_ulpstatus(phba, piocbq));
}

static void
lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
		     struct lpfc_dmabuf *mp, uint32_t size)
{
	lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
}

/**
 * lpfc_ct_unsol_cmpl : Completion callback function for unsol ct commands
 * @phba : pointer to lpfc hba data structure.
 * @cmdiocb : pointer to lpfc command iocb data structure.
 * @rspiocb : pointer to lpfc response iocb data structure.
 *
 * This routine is the callback function for issuing unsol ct reject command.
 * The memory allocated in the reject command path is freed up here.
 **/
static void
lpfc_ct_unsol_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		   struct lpfc_iocbq *rspiocb)
{
	struct lpfc_nodelist *ndlp;
	struct lpfc_dmabuf *mp, *bmp;

	ndlp = cmdiocb->ndlp;
	if (ndlp)
		lpfc_nlp_put(ndlp);

	mp = cmdiocb->rsp_dmabuf;
	bmp = cmdiocb->bpl_dmabuf;
	if (mp) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
		cmdiocb->rsp_dmabuf = NULL;
	}

	if (bmp) {
		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
		kfree(bmp);
		cmdiocb->bpl_dmabuf = NULL;
	}

	lpfc_sli_release_iocbq(phba, cmdiocb);
}

/**
 * lpfc_ct_reject_event - Issue reject for unhandled CT MIB commands
 * @ndlp: pointer to a node-list data structure.
 * @ct_req: pointer to the CT request data structure.
 * @ulp_context: context of received UNSOL CT command
 * @ox_id: ox_id of the UNSOL CT command
 *
 * This routine is invoked by the lpfc_ct_handle_mibreq routine for sending
 * a reject response. Reject response is sent for the unhandled commands.
 **/
static void
lpfc_ct_reject_event(struct lpfc_nodelist *ndlp,
		     struct lpfc_sli_ct_request *ct_req,
		     u16 ulp_context, u16 ox_id)
{
	struct lpfc_vport *vport = ndlp->vport;
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_sli_ct_request *ct_rsp;
	struct lpfc_iocbq *cmdiocbq = NULL;
	struct lpfc_dmabuf *bmp = NULL;
	struct lpfc_dmabuf *mp = NULL;
	struct ulp_bde64 *bpl;
	u8 rc = 0;
	u32 tmo;

	/* fill in BDEs for command */
	mp = kmalloc(sizeof(*mp), GFP_KERNEL);
	if (!mp) {
		rc = 1;
		goto ct_exit;
	}

	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp->phys);
	if (!mp->virt) {
		rc = 2;
		goto ct_free_mp;
	}

	/* Allocate buffer for Buffer ptr list */
	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
	if (!bmp) {
		rc = 3;
		goto ct_free_mpvirt;
	}

	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &bmp->phys);
	if (!bmp->virt) {
		rc = 4;
		goto ct_free_bmp;
	}

	INIT_LIST_HEAD(&mp->list);
	INIT_LIST_HEAD(&bmp->list);

	bpl = (struct ulp_bde64 *)bmp->virt;
	memset(bpl, 0, sizeof(struct ulp_bde64));
	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
	bpl->tus.f.bdeSize = (LPFC_CT_PREAMBLE - 4);
	bpl->tus.w = le32_to_cpu(bpl->tus.w);

	ct_rsp = (struct lpfc_sli_ct_request *)mp->virt;
	memset(ct_rsp, 0, sizeof(struct lpfc_sli_ct_request));

	ct_rsp->RevisionId.bits.Revision = SLI_CT_REVISION;
	ct_rsp->RevisionId.bits.InId = 0;
	ct_rsp->FsType = ct_req->FsType;
	ct_rsp->FsSubType = ct_req->FsSubType;
	ct_rsp->CommandResponse.bits.Size = 0;
	ct_rsp->CommandResponse.bits.CmdRsp =
		cpu_to_be16(SLI_CT_RESPONSE_FS_RJT);
	ct_rsp->ReasonCode = SLI_CT_REQ_NOT_SUPPORTED;
	ct_rsp->Explanation = SLI_CT_NO_ADDITIONAL_EXPL;

	cmdiocbq = lpfc_sli_get_iocbq(phba);
	if (!cmdiocbq) {
		rc = 5;
		goto ct_free_bmpvirt;
	}

	if (phba->sli_rev == LPFC_SLI_REV4) {
		lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, bmp,
					 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
					 ox_id, 1, FC_RCTL_DD_SOL_CTL, 1,
					 CMD_XMIT_SEQUENCE64_WQE);
	} else {
		lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, bmp, 0, ulp_context, 1,
					 FC_RCTL_DD_SOL_CTL, 1,
					 CMD_XMIT_SEQUENCE64_CX);
	}

	/* Save for completion so we can release these resources */
	cmdiocbq->rsp_dmabuf = mp;
	cmdiocbq->bpl_dmabuf = bmp;
	cmdiocbq->cmd_cmpl = lpfc_ct_unsol_cmpl;
	tmo = (3 * phba->fc_ratov);

	cmdiocbq->retry = 0;
	cmdiocbq->vport = vport;
	cmdiocbq->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;

	cmdiocbq->ndlp = lpfc_nlp_get(ndlp);
	if (!cmdiocbq->ndlp)
		goto ct_no_ndlp;

	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
	if (rc) {
		lpfc_nlp_put(ndlp);
		goto ct_no_ndlp;
	}
	return;

ct_no_ndlp:
	rc = 6;
	lpfc_sli_release_iocbq(phba, cmdiocbq);
ct_free_bmpvirt:
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
ct_free_bmp:
	kfree(bmp);
ct_free_mpvirt:
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
ct_free_mp:
	kfree(mp);
ct_exit:
	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
			 "6440 Unsol CT: Rsp err %d Data: x%x\n",
			 rc, vport->fc_flag);
}

/**
 * lpfc_ct_handle_mibreq - Process an unsolicited CT MIB request data buffer
 * @phba: pointer to lpfc hba data structure.
 * @ctiocbq: pointer to lpfc CT command iocb data structure.
 *
 * This routine is used for processing the IOCB associated with a unsolicited
 * CT MIB request. It first determines whether there is an existing ndlp that
 * matches the DID from the unsolicited IOCB. If not, it will return.
 **/
static void
lpfc_ct_handle_mibreq(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocbq)
{
	struct lpfc_sli_ct_request *ct_req;
	struct lpfc_nodelist *ndlp = NULL;
	struct lpfc_vport *vport = ctiocbq->vport;
	u32 ulp_status = get_job_ulpstatus(phba, ctiocbq);
	u32 ulp_word4 = get_job_word4(phba, ctiocbq);
	u32 did;
	u16 mi_cmd;

	did = bf_get(els_rsp64_sid, &ctiocbq->wqe.xmit_els_rsp);
	if (ulp_status) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "6438 Unsol CT: status:x%x/x%x did : x%x\n",
				 ulp_status, ulp_word4, did);
		return;
	}

	/* Ignore traffic received during vport shutdown */
	if (vport->fc_flag & FC_UNLOADING)
		return;

	ndlp = lpfc_findnode_did(vport, did);
	if (!ndlp) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "6439 Unsol CT: NDLP Not Found for DID : x%x",
				 did);
		return;
	}

	ct_req = (struct lpfc_sli_ct_request *)ctiocbq->cmd_dmabuf->virt;

	mi_cmd = be16_to_cpu(ct_req->CommandResponse.bits.CmdRsp);
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "6442 : MI Cmd : x%x Not Supported\n", mi_cmd);
	lpfc_ct_reject_event(ndlp, ct_req,
			     bf_get(wqe_ctxt_tag,
				    &ctiocbq->wqe.xmit_els_rsp.wqe_com),
			     bf_get(wqe_rcvoxid,
				    &ctiocbq->wqe.xmit_els_rsp.wqe_com));
}

/**
 * lpfc_ct_unsol_event - Process an unsolicited event from a ct sli ring
 * @phba: pointer to lpfc hba data structure.
 * @pring: pointer to a SLI ring.
 * @ctiocbq: pointer to lpfc ct iocb data structure.
 *
 * This routine is used to process an unsolicited event received from a SLI
 * (Service Level Interface) ring. The actual processing of the data buffer
 * associated with the unsolicited event is done by invoking appropriate routine
 * after properly set up the iocb buffer from the SLI ring on which the
 * unsolicited event was received.
 **/
void
lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
		    struct lpfc_iocbq *ctiocbq)
{
	struct lpfc_dmabuf *mp = NULL;
	IOCB_t *icmd = &ctiocbq->iocb;
	int i;
	struct lpfc_iocbq *iocbq;
	struct lpfc_iocbq *iocb;
	dma_addr_t dma_addr;
	uint32_t size;
	struct list_head head;
	struct lpfc_sli_ct_request *ct_req;
	struct lpfc_dmabuf *bdeBuf1 = ctiocbq->cmd_dmabuf;
	struct lpfc_dmabuf *bdeBuf2 = ctiocbq->bpl_dmabuf;
	u32 status, parameter, bde_count = 0;
	struct lpfc_wcqe_complete *wcqe_cmpl = NULL;

	ctiocbq->cmd_dmabuf = NULL;
	ctiocbq->rsp_dmabuf = NULL;
	ctiocbq->bpl_dmabuf = NULL;

	wcqe_cmpl = &ctiocbq->wcqe_cmpl;
	status = get_job_ulpstatus(phba, ctiocbq);
	parameter = get_job_word4(phba, ctiocbq);
	if (phba->sli_rev == LPFC_SLI_REV4)
		bde_count = wcqe_cmpl->word3;
	else
		bde_count = icmd->ulpBdeCount;

	if (unlikely(status == IOSTAT_NEED_BUFFER)) {
		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
	} else if ((status == IOSTAT_LOCAL_REJECT) &&
		   ((parameter & IOERR_PARAM_MASK) ==
		   IOERR_RCV_BUFFER_WAITING)) {
		/* Not enough posted buffers; Try posting more buffers */
		phba->fc_stat.NoRcvBuf++;
		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
			lpfc_sli3_post_buffer(phba, pring, 2);
		return;
	}

	/* If there are no BDEs associated
	 * with this IOCB, there is nothing to do.
	 */
	if (bde_count == 0)
		return;

	ctiocbq->cmd_dmabuf = bdeBuf1;
	if (bde_count == 2)
		ctiocbq->bpl_dmabuf = bdeBuf2;

	ct_req = (struct lpfc_sli_ct_request *)ctiocbq->cmd_dmabuf->virt;

	if (ct_req->FsType == SLI_CT_MANAGEMENT_SERVICE &&
	    ct_req->FsSubType == SLI_CT_MIB_Subtypes) {
		lpfc_ct_handle_mibreq(phba, ctiocbq);
	} else {
		if (!lpfc_bsg_ct_unsol_event(phba, pring, ctiocbq))
			return;
	}

	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
		INIT_LIST_HEAD(&head);
		list_add_tail(&head, &ctiocbq->list);
		list_for_each_entry(iocb, &head, list) {
			if (phba->sli_rev == LPFC_SLI_REV4)
				bde_count = iocb->wcqe_cmpl.word3;
			else
				bde_count = iocb->iocb.ulpBdeCount;

			if (!bde_count)
				continue;
			bdeBuf1 = iocb->cmd_dmabuf;
			iocb->cmd_dmabuf = NULL;
			if (phba->sli_rev == LPFC_SLI_REV4)
				size = iocb->wqe.gen_req.bde.tus.f.bdeSize;
			else
				size  = iocb->iocb.un.cont64[0].tus.f.bdeSize;
			lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf1, size);
			lpfc_in_buf_free(phba, bdeBuf1);
			if (bde_count == 2) {
				bdeBuf2 = iocb->bpl_dmabuf;
				iocb->bpl_dmabuf = NULL;
				if (phba->sli_rev == LPFC_SLI_REV4)
					size = iocb->unsol_rcv_len;
				else
					size = iocb->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize;
				lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf2,
						     size);
				lpfc_in_buf_free(phba, bdeBuf2);
			}
		}
		list_del(&head);
	} else {
		INIT_LIST_HEAD(&head);
		list_add_tail(&head, &ctiocbq->list);
		list_for_each_entry(iocbq, &head, list) {
			icmd = &iocbq->iocb;
			if (icmd->ulpBdeCount == 0)
				lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
			for (i = 0; i < icmd->ulpBdeCount; i++) {
				dma_addr = getPaddr(icmd->un.cont64[i].addrHigh,
						    icmd->un.cont64[i].addrLow);
				mp = lpfc_sli_ringpostbuf_get(phba, pring,
							      dma_addr);
				size = icmd->un.cont64[i].tus.f.bdeSize;
				lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
				lpfc_in_buf_free(phba, mp);
			}
			lpfc_sli3_post_buffer(phba, pring, i);
		}
		list_del(&head);
	}
}

/**
 * lpfc_ct_handle_unsol_abort - ct upper level protocol abort handler
 * @phba: Pointer to HBA context object.
 * @dmabuf: pointer to a dmabuf that describes the FC sequence
 *
 * This function serves as the upper level protocol abort handler for CT
 * protocol.
 *
 * Return 1 if abort has been handled, 0 otherwise.
 **/
int
lpfc_ct_handle_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
{
	int handled;

	/* CT upper level goes through BSG */
	handled = lpfc_bsg_ct_unsol_abort(phba, dmabuf);

	return handled;
}

static void
lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
{
	struct lpfc_dmabuf *mlast, *next_mlast;

	list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
		list_del(&mlast->list);
		lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
		kfree(mlast);
	}
	lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
	kfree(mlist);
	return;
}

static struct lpfc_dmabuf *
lpfc_alloc_ct_rsp(struct lpfc_hba *phba, __be16 cmdcode, struct ulp_bde64 *bpl,
		  uint32_t size, int *entries)
{
	struct lpfc_dmabuf *mlist = NULL;
	struct lpfc_dmabuf *mp;
	int cnt, i = 0;

	/* We get chunks of FCELSSIZE */
	cnt = size > FCELSSIZE ? FCELSSIZE: size;

	while (size) {
		/* Allocate buffer for rsp payload */
		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
		if (!mp) {
			if (mlist)
				lpfc_free_ct_rsp(phba, mlist);
			return NULL;
		}

		INIT_LIST_HEAD(&mp->list);

		if (be16_to_cpu(cmdcode) == SLI_CTNS_GID_FT ||
		    be16_to_cpu(cmdcode) == SLI_CTNS_GFF_ID)
			mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
		else
			mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));

		if (!mp->virt) {
			kfree(mp);
			if (mlist)
				lpfc_free_ct_rsp(phba, mlist);
			return NULL;
		}

		/* Queue it to a linked list */
		if (!mlist)
			mlist = mp;
		else
			list_add_tail(&mp->list, &mlist->list);

		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
		/* build buffer ptr list for IOCB */
		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
		bpl->tus.f.bdeSize = (uint16_t) cnt;
		bpl->tus.w = le32_to_cpu(bpl->tus.w);
		bpl++;

		i++;
		size -= cnt;
	}

	*entries = i;
	return mlist;
}

int
lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
{
	struct lpfc_dmabuf *buf_ptr;

	/* IOCBQ job structure gets cleaned during release.  Just release
	 * the dma buffers here.
	 */
	if (ctiocb->cmd_dmabuf) {
		buf_ptr = ctiocb->cmd_dmabuf;
		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
		kfree(buf_ptr);
		ctiocb->cmd_dmabuf = NULL;
	}
	if (ctiocb->rsp_dmabuf) {
		lpfc_free_ct_rsp(phba, ctiocb->rsp_dmabuf);
		ctiocb->rsp_dmabuf = NULL;
	}

	if (ctiocb->bpl_dmabuf) {
		buf_ptr = ctiocb->bpl_dmabuf;
		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
		kfree(buf_ptr);
		ctiocb->bpl_dmabuf = NULL;
	}
	lpfc_sli_release_iocbq(phba, ctiocb);
	return 0;
}

/*
 * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
 * @vport: pointer to a host virtual N_Port data structure.
 * @bmp: Pointer to BPL for SLI command
 * @inp: Pointer to data buffer for response data.
 * @outp: Pointer to data buffer that hold the CT command.
 * @cmpl: completion routine to call when command completes
 * @ndlp: Destination NPort nodelist entry
 *
 * This function as the final part for issuing a CT command.
 */
static int
lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
	     struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
	     void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
			  struct lpfc_iocbq *),
	     struct lpfc_nodelist *ndlp, uint32_t event_tag, uint32_t num_entry,
	     uint32_t tmo, uint8_t retry)
{
	struct lpfc_hba  *phba = vport->phba;
	struct lpfc_iocbq *geniocb;
	int rc;
	u16 ulp_context;

	/* Allocate buffer for  command iocb */
	geniocb = lpfc_sli_get_iocbq(phba);

	if (geniocb == NULL)
		return 1;

	/* Update the num_entry bde count */
	geniocb->num_bdes = num_entry;

	geniocb->bpl_dmabuf = bmp;

	/* Save for completion so we can release these resources */
	geniocb->cmd_dmabuf = inp;
	geniocb->rsp_dmabuf = outp;

	geniocb->event_tag = event_tag;

	if (!tmo) {
		 /* FC spec states we need 3 * ratov for CT requests */
		tmo = (3 * phba->fc_ratov);
	}

	if (phba->sli_rev == LPFC_SLI_REV4)
		ulp_context = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
	else
		ulp_context = ndlp->nlp_rpi;

	lpfc_sli_prep_gen_req(phba, geniocb, bmp, ulp_context, num_entry, tmo);

	/* Issue GEN REQ IOCB for NPORT <did> */
	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
			 "0119 Issue GEN REQ IOCB to NPORT x%x "
			 "Data: x%x x%x\n",
			 ndlp->nlp_DID, geniocb->iotag,
			 vport->port_state);
	geniocb->cmd_cmpl = cmpl;
	geniocb->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
	geniocb->vport = vport;
	geniocb->retry = retry;
	geniocb->ndlp = lpfc_nlp_get(ndlp);
	if (!geniocb->ndlp)
		goto out;

	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
	if (rc == IOCB_ERROR) {
		lpfc_nlp_put(ndlp);
		goto out;
	}

	return 0;
out:
	lpfc_sli_release_iocbq(phba, geniocb);
	return 1;
}

/*
 * lpfc_ct_cmd - Build and issue a CT command
 * @vport: pointer to a host virtual N_Port data structure.
 * @inmp: Pointer to data buffer for response data.
 * @bmp: Pointer to BPL for SLI command
 * @ndlp: Destination NPort nodelist entry
 * @cmpl: completion routine to call when command completes
 *
 * This function is called for issuing a CT command.
 */
static int
lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
	    struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
	    void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
			  struct lpfc_iocbq *),
	    uint32_t rsp_size, uint8_t retry)
{
	struct lpfc_hba  *phba = vport->phba;
	struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
	struct lpfc_dmabuf *outmp;
	int cnt = 0, status;
	__be16 cmdcode = ((struct lpfc_sli_ct_request *)inmp->virt)->
		CommandResponse.bits.CmdRsp;

	bpl++;			/* Skip past ct request */

	/* Put buffer(s) for ct rsp in bpl */
	outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
	if (!outmp)
		return -ENOMEM;
	/*
	 * Form the CT IOCB.  The total number of BDEs in this IOCB
	 * is the single command plus response count from
	 * lpfc_alloc_ct_rsp.
	 */
	cnt += 1;
	status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp,
			phba->fc_eventTag, cnt, 0, retry);
	if (status) {
		lpfc_free_ct_rsp(phba, outmp);
		return -ENOMEM;
	}
	return 0;
}

struct lpfc_vport *
lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
	struct lpfc_vport *vport_curr;
	unsigned long flags;

	spin_lock_irqsave(&phba->port_list_lock, flags);
	list_for_each_entry(vport_curr, &phba->port_list, listentry) {
		if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
			spin_unlock_irqrestore(&phba->port_list_lock, flags);
			return vport_curr;
		}
	}
	spin_unlock_irqrestore(&phba->port_list_lock, flags);
	return NULL;
}

static void
lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
{
	struct lpfc_nodelist *ndlp;

	if ((vport->port_type != LPFC_NPIV_PORT) ||
	    !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {

		ndlp = lpfc_setup_disc_node(vport, Did);

		if (ndlp) {
			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"Parse GID_FTrsp: did:x%x flg:x%x x%x",
				Did, ndlp->nlp_flag, vport->fc_flag);

			/* By default, the driver expects to support FCP FC4 */
			if (fc4_type == FC_TYPE_FCP)
				ndlp->nlp_fc4_type |= NLP_FC4_FCP;

			if (fc4_type == FC_TYPE_NVME)
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;

			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0238 Process x%06x NameServer Rsp "
					 "Data: x%x x%x x%x x%x x%x\n", Did,
					 ndlp->nlp_flag, ndlp->nlp_fc4_type,
					 ndlp->nlp_state, vport->fc_flag,
					 vport->fc_rscn_id_cnt);

			/* if ndlp needs to be discovered and prior
			 * state of ndlp hit devloss, change state to
			 * allow rediscovery.
			 */
			if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
			    ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
				lpfc_nlp_set_state(vport, ndlp,
						   NLP_STE_NPR_NODE);
			}
		} else {
			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
				Did, vport->fc_flag, vport->fc_rscn_id_cnt);

			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0239 Skip x%06x NameServer Rsp "
					 "Data: x%x x%x x%px\n",
					 Did, vport->fc_flag,
					 vport->fc_rscn_id_cnt, ndlp);
		}
	} else {
		if (!(vport->fc_flag & FC_RSCN_MODE) ||
		    lpfc_rscn_payload_check(vport, Did)) {
			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
				Did, vport->fc_flag, vport->fc_rscn_id_cnt);

			/*
			 * This NPortID was previously a FCP/NVMe target,
			 * Don't even bother to send GFF_ID.
			 */
			ndlp = lpfc_findnode_did(vport, Did);
			if (ndlp &&
			    (ndlp->nlp_type &
			    (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
				if (fc4_type == FC_TYPE_FCP)
					ndlp->nlp_fc4_type |= NLP_FC4_FCP;
				if (fc4_type == FC_TYPE_NVME)
					ndlp->nlp_fc4_type |= NLP_FC4_NVME;
				lpfc_setup_disc_node(vport, Did);
			} else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
				   0, Did) == 0)
				vport->num_disc_nodes++;
			else
				lpfc_setup_disc_node(vport, Did);
		} else {
			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
				Did, vport->fc_flag, vport->fc_rscn_id_cnt);

			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0245 Skip x%06x NameServer Rsp "
					 "Data: x%x x%x\n", Did,
					 vport->fc_flag,
					 vport->fc_rscn_id_cnt);
		}
	}
}

static void
lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
{
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_nodelist *ndlp = NULL;
	char *str;

	if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
		str = "GID_FT";
	else
		str = "GID_PT";
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "6430 Process %s rsp for %08x type %x %s %s\n",
			 str, Did, fc4_type,
			 (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
			 (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
	/*
	 * To conserve rpi's, filter out addresses for other
	 * vports on the same physical HBAs.
	 */
	if (Did != vport->fc_myDID &&
	    (!lpfc_find_vport_by_did(phba, Did) ||
	     vport->cfg_peer_port_login)) {
		if (!phba->nvmet_support) {
			/* FCPI/NVMEI path. Process Did */
			lpfc_prep_node_fc4type(vport, Did, fc4_type);
			return;
		}
		/* NVMET path.  NVMET only cares about NVMEI nodes. */
		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
			if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
			    ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
				continue;
			spin_lock_irq(&ndlp->lock);
			if (ndlp->nlp_DID == Did)
				ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
			else
				ndlp->nlp_flag |= NLP_NVMET_RECOV;
			spin_unlock_irq(&ndlp->lock);
		}
	}
}

static int
lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
	    uint32_t Size)
{
	struct lpfc_sli_ct_request *Response =
		(struct lpfc_sli_ct_request *) mp->virt;
	struct lpfc_dmabuf *mlast, *next_mp;
	uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
	uint32_t Did, CTentry;
	int Cnt;
	struct list_head head;
	struct lpfc_nodelist *ndlp = NULL;

	lpfc_set_disctmo(vport);
	vport->num_disc_nodes = 0;
	vport->fc_ns_retry = 0;


	list_add_tail(&head, &mp->list);
	list_for_each_entry_safe(mp, next_mp, &head, list) {
		mlast = mp;

		Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;

		Size -= Cnt;

		if (!ctptr) {
			ctptr = (uint32_t *) mlast->virt;
		} else
			Cnt -= 16;	/* subtract length of CT header */

		/* Loop through entire NameServer list of DIDs */
		while (Cnt >= sizeof(uint32_t)) {
			/* Get next DID from NameServer List */
			CTentry = *ctptr++;
			Did = ((be32_to_cpu(CTentry)) & Mask_DID);
			lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
			if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
				goto nsout1;

			Cnt -= sizeof(uint32_t);
		}
		ctptr = NULL;

	}

	/* All GID_FT entries processed.  If the driver is running in
	 * in target mode, put impacted nodes into recovery and drop
	 * the RPI to flush outstanding IO.
	 */
	if (vport->phba->nvmet_support) {
		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
			if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
				continue;
			lpfc_disc_state_machine(vport, ndlp, NULL,
						NLP_EVT_DEVICE_RECOVERY);
			spin_lock_irq(&ndlp->lock);
			ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
			spin_unlock_irq(&ndlp->lock);
		}
	}

nsout1:
	list_del(&head);
	return 0;
}

static void
lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_dmabuf *outp;
	struct lpfc_dmabuf *inp;
	struct lpfc_sli_ct_request *CTrsp;
	struct lpfc_sli_ct_request *CTreq;
	struct lpfc_nodelist *ndlp;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);
	int rc, type;

	/* First save ndlp, before we overwrite it */
	ndlp = cmdiocb->ndlp;

	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->rsp_iocb = rspiocb;
	inp = cmdiocb->cmd_dmabuf;
	outp = cmdiocb->rsp_dmabuf;

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
		 "GID_FT cmpl:     status:x%x/x%x rtry:%d",
		ulp_status, ulp_word4, vport->fc_ns_retry);

	/* Ignore response if link flipped after this request was made */
	if (cmdiocb->event_tag != phba->fc_eventTag) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "9043 Event tag mismatch. Ignoring NS rsp\n");
		goto out;
	}

	/* Don't bother processing response if vport is being torn down. */
	if (vport->load_flag & FC_UNLOADING) {
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		goto out;
	}

	if (lpfc_els_chk_latt(vport)) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0216 Link event during NS query\n");
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
		goto out;
	}
	if (lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0226 NS query failed due to link event: "
				 "ulp_status x%x ulp_word4 x%x fc_flag x%x "
				 "port_state x%x gidft_inp x%x\n",
				 ulp_status, ulp_word4, vport->fc_flag,
				 vport->port_state, vport->gidft_inp);
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		if (vport->gidft_inp)
			vport->gidft_inp--;
		goto out;
	}

	spin_lock_irq(shost->host_lock);
	if (vport->fc_flag & FC_RSCN_DEFERRED) {
		vport->fc_flag &= ~FC_RSCN_DEFERRED;
		spin_unlock_irq(shost->host_lock);

		/* This is a GID_FT completing so the gidft_inp counter was
		 * incremented before the GID_FT was issued to the wire.
		 */
		if (vport->gidft_inp)
			vport->gidft_inp--;

		/*
		 * Skip processing the NS response
		 * Re-issue the NS cmd
		 */
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "0151 Process Deferred RSCN Data: x%x x%x\n",
				 vport->fc_flag, vport->fc_rscn_id_cnt);
		lpfc_els_handle_rscn(vport);

		goto out;
	}
	spin_unlock_irq(shost->host_lock);

	if (ulp_status) {
		/* Check for retry */
		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
			if (ulp_status != IOSTAT_LOCAL_REJECT ||
			    (ulp_word4 & IOERR_PARAM_MASK) !=
			    IOERR_NO_RESOURCES)
				vport->fc_ns_retry++;

			type = lpfc_get_gidft_type(vport, cmdiocb);
			if (type == 0)
				goto out;

			/* CT command is being retried */
			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
					 vport->fc_ns_retry, type);
			if (rc == 0)
				goto out;
			else { /* Unable to send NS cmd */
				if (vport->gidft_inp)
					vport->gidft_inp--;
			}
		}
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "0257 GID_FT Query error: 0x%x 0x%x\n",
				 ulp_status, vport->fc_ns_retry);
	} else {
		/* Good status, continue checking */
		CTreq = (struct lpfc_sli_ct_request *) inp->virt;
		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
		if (CTrsp->CommandResponse.bits.CmdRsp ==
		    cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "0208 NameServer Rsp Data: x%x x%x "
					 "x%x x%x sz x%x\n",
					 vport->fc_flag,
					 CTreq->un.gid.Fc4Type,
					 vport->num_disc_nodes,
					 vport->gidft_inp,
					 get_job_data_placed(phba, rspiocb));

			lpfc_ns_rsp(vport,
				    outp,
				    CTreq->un.gid.Fc4Type,
				    get_job_data_placed(phba, rspiocb));
		} else if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
			   SLI_CT_RESPONSE_FS_RJT) {
			/* NameServer Rsp Error */
			if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
			    && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
				lpfc_printf_vlog(vport, KERN_INFO,
					LOG_DISCOVERY,
					"0269 No NameServer Entries "
					"Data: x%x x%x x%x x%x\n",
					be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					(uint32_t) CTrsp->ReasonCode,
					(uint32_t) CTrsp->Explanation,
					vport->fc_flag);

				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t) CTrsp->ReasonCode,
				(uint32_t) CTrsp->Explanation);
			} else {
				lpfc_printf_vlog(vport, KERN_INFO,
					LOG_DISCOVERY,
					"0240 NameServer Rsp Error "
					"Data: x%x x%x x%x x%x\n",
					be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					(uint32_t) CTrsp->ReasonCode,
					(uint32_t) CTrsp->Explanation,
					vport->fc_flag);

				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t) CTrsp->ReasonCode,
				(uint32_t) CTrsp->Explanation);
			}


		} else {
			/* NameServer Rsp Error */
			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
					"0241 NameServer Rsp Error "
					"Data: x%x x%x x%x x%x\n",
					be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					(uint32_t) CTrsp->ReasonCode,
					(uint32_t) CTrsp->Explanation,
					vport->fc_flag);

			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
				"GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t) CTrsp->ReasonCode,
				(uint32_t) CTrsp->Explanation);
		}
		if (vport->gidft_inp)
			vport->gidft_inp--;
	}

	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "4216 GID_FT cmpl inp %d disc %d\n",
			 vport->gidft_inp, vport->num_disc_nodes);

	/* Link up / RSCN discovery */
	if ((vport->num_disc_nodes == 0) &&
	    (vport->gidft_inp == 0)) {
		/*
		 * The driver has cycled through all Nports in the RSCN payload.
		 * Complete the handling by cleaning up and marking the
		 * current driver state.
		 */
		if (vport->port_state >= LPFC_DISC_AUTH) {
			if (vport->fc_flag & FC_RSCN_MODE) {
				lpfc_els_flush_rscn(vport);
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
				spin_unlock_irq(shost->host_lock);
			}
			else
				lpfc_els_flush_rscn(vport);
		}

		lpfc_disc_start(vport);
	}
out:
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(ndlp);
	return;
}

static void
lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_dmabuf *outp;
	struct lpfc_dmabuf *inp;
	struct lpfc_sli_ct_request *CTrsp;
	struct lpfc_sli_ct_request *CTreq;
	struct lpfc_nodelist *ndlp;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);
	int rc;

	/* First save ndlp, before we overwrite it */
	ndlp = cmdiocb->ndlp;

	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->rsp_iocb = rspiocb;
	inp = cmdiocb->cmd_dmabuf;
	outp = cmdiocb->rsp_dmabuf;

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
			      "GID_PT cmpl:     status:x%x/x%x rtry:%d",
			      ulp_status, ulp_word4,
			      vport->fc_ns_retry);

	/* Ignore response if link flipped after this request was made */
	if (cmdiocb->event_tag != phba->fc_eventTag) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "9044 Event tag mismatch. Ignoring NS rsp\n");
		goto out;
	}

	/* Don't bother processing response if vport is being torn down. */
	if (vport->load_flag & FC_UNLOADING) {
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		goto out;
	}

	if (lpfc_els_chk_latt(vport)) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "4108 Link event during NS query\n");
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
		goto out;
	}
	if (lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "4166 NS query failed due to link event: "
				 "ulp_status x%x ulp_word4 x%x fc_flag x%x "
				 "port_state x%x gidft_inp x%x\n",
				 ulp_status, ulp_word4, vport->fc_flag,
				 vport->port_state, vport->gidft_inp);
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		if (vport->gidft_inp)
			vport->gidft_inp--;
		goto out;
	}

	spin_lock_irq(shost->host_lock);
	if (vport->fc_flag & FC_RSCN_DEFERRED) {
		vport->fc_flag &= ~FC_RSCN_DEFERRED;
		spin_unlock_irq(shost->host_lock);

		/* This is a GID_PT completing so the gidft_inp counter was
		 * incremented before the GID_PT was issued to the wire.
		 */
		if (vport->gidft_inp)
			vport->gidft_inp--;

		/*
		 * Skip processing the NS response
		 * Re-issue the NS cmd
		 */
		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
				 "4167 Process Deferred RSCN Data: x%x x%x\n",
				 vport->fc_flag, vport->fc_rscn_id_cnt);
		lpfc_els_handle_rscn(vport);

		goto out;
	}
	spin_unlock_irq(shost->host_lock);

	if (ulp_status) {
		/* Check for retry */
		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
			if (ulp_status != IOSTAT_LOCAL_REJECT ||
			    (ulp_word4 & IOERR_PARAM_MASK) !=
			    IOERR_NO_RESOURCES)
				vport->fc_ns_retry++;

			/* CT command is being retried */
			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
					 vport->fc_ns_retry, GID_PT_N_PORT);
			if (rc == 0)
				goto out;
			else { /* Unable to send NS cmd */
				if (vport->gidft_inp)
					vport->gidft_inp--;
			}
		}
		if (vport->fc_flag & FC_RSCN_MODE)
			lpfc_els_flush_rscn(vport);
		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "4103 GID_FT Query error: 0x%x 0x%x\n",
				 ulp_status, vport->fc_ns_retry);
	} else {
		/* Good status, continue checking */
		CTreq = (struct lpfc_sli_ct_request *)inp->virt;
		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC) {
			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
					 "4105 NameServer Rsp Data: x%x x%x "
					 "x%x x%x sz x%x\n",
					 vport->fc_flag,
					 CTreq->un.gid.Fc4Type,
					 vport->num_disc_nodes,
					 vport->gidft_inp,
					 get_job_data_placed(phba, rspiocb));

			lpfc_ns_rsp(vport,
				    outp,
				    CTreq->un.gid.Fc4Type,
				    get_job_data_placed(phba, rspiocb));
		} else if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
			   SLI_CT_RESPONSE_FS_RJT) {
			/* NameServer Rsp Error */
			if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
			    && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
				lpfc_printf_vlog(
					vport, KERN_INFO, LOG_DISCOVERY,
					"4106 No NameServer Entries "
					"Data: x%x x%x x%x x%x\n",
					be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					(uint32_t)CTrsp->ReasonCode,
					(uint32_t)CTrsp->Explanation,
					vport->fc_flag);

				lpfc_debugfs_disc_trc(
				vport, LPFC_DISC_TRC_CT,
				"GID_PT no entry  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t)CTrsp->ReasonCode,
				(uint32_t)CTrsp->Explanation);
			} else {
				lpfc_printf_vlog(
					vport, KERN_INFO, LOG_DISCOVERY,
					"4107 NameServer Rsp Error "
					"Data: x%x x%x x%x x%x\n",
					be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					(uint32_t)CTrsp->ReasonCode,
					(uint32_t)CTrsp->Explanation,
					vport->fc_flag);

				lpfc_debugfs_disc_trc(
				vport, LPFC_DISC_TRC_CT,
				"GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t)CTrsp->ReasonCode,
				(uint32_t)CTrsp->Explanation);
			}
		} else {
			/* NameServer Rsp Error */
			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
					 "4109 NameServer Rsp Error "
					 "Data: x%x x%x x%x x%x\n",
					 be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
					 (uint32_t)CTrsp->ReasonCode,
					 (uint32_t)CTrsp->Explanation,
					 vport->fc_flag);

			lpfc_debugfs_disc_trc(
				vport, LPFC_DISC_TRC_CT,
				"GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
				be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
				(uint32_t)CTrsp->ReasonCode,
				(uint32_t)CTrsp->Explanation);
		}
		if (vport->gidft_inp)
			vport->gidft_inp--;
	}

	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "6450 GID_PT cmpl inp %d disc %d\n",
			 vport->gidft_inp, vport->num_disc_nodes);

	/* Link up / RSCN discovery */
	if ((vport->num_disc_nodes == 0) &&
	    (vport->gidft_inp == 0)) {
		/*
		 * The driver has cycled through all Nports in the RSCN payload.
		 * Complete the handling by cleaning up and marking the
		 * current driver state.
		 */
		if (vport->port_state >= LPFC_DISC_AUTH) {
			if (vport->fc_flag & FC_RSCN_MODE) {
				lpfc_els_flush_rscn(vport);
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
				spin_unlock_irq(shost->host_lock);
			} else {
				lpfc_els_flush_rscn(vport);
			}
		}

		lpfc_disc_start(vport);
	}
out:
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(ndlp);
}

static void
lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	struct lpfc_dmabuf *inp = cmdiocb->cmd_dmabuf;
	struct lpfc_dmabuf *outp = cmdiocb->rsp_dmabuf;
	struct lpfc_sli_ct_request *CTrsp;
	int did, rc, retry;
	uint8_t fbits;
	struct lpfc_nodelist *ndlp = NULL, *free_ndlp = NULL;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);

	did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
	did = be32_to_cpu(did);

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
		"GFF_ID cmpl:     status:x%x/x%x did:x%x",
		ulp_status, ulp_word4, did);

	/* Ignore response if link flipped after this request was made */
	if (cmdiocb->event_tag != phba->fc_eventTag) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "9045 Event tag mismatch. Ignoring NS rsp\n");
		goto iocb_free;
	}

	if (ulp_status == IOSTAT_SUCCESS) {
		/* Good status, continue checking */
		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
		fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];

		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "6431 Process GFF_ID rsp for %08x "
				 "fbits %02x %s %s\n",
				 did, fbits,
				 (fbits & FC4_FEATURE_INIT) ? "Initiator" : " ",
				 (fbits & FC4_FEATURE_TARGET) ? "Target" : " ");

		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC) {
			if ((fbits & FC4_FEATURE_INIT) &&
			    !(fbits & FC4_FEATURE_TARGET)) {
				lpfc_printf_vlog(vport, KERN_INFO,
						 LOG_DISCOVERY,
						 "0270 Skip x%x GFF "
						 "NameServer Rsp Data: (init) "
						 "x%x x%x\n", did, fbits,
						 vport->fc_rscn_id_cnt);
				goto out;
			}
		}
	}
	else {
		/* Check for retry */
		if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
			retry = 1;
			if (ulp_status == IOSTAT_LOCAL_REJECT) {
				switch ((ulp_word4 &
					IOERR_PARAM_MASK)) {

				case IOERR_NO_RESOURCES:
					/* We don't increment the retry
					 * count for this case.
					 */
					break;
				case IOERR_LINK_DOWN:
				case IOERR_SLI_ABORTED:
				case IOERR_SLI_DOWN:
					retry = 0;
					break;
				default:
					cmdiocb->retry++;
				}
			}
			else
				cmdiocb->retry++;

			if (retry) {
				/* CT command is being retried */
				rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
					 cmdiocb->retry, did);
				if (rc == 0) {
					/* success */
					free_ndlp = cmdiocb->ndlp;
					lpfc_ct_free_iocb(phba, cmdiocb);
					lpfc_nlp_put(free_ndlp);
					return;
				}
			}
		}
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "0267 NameServer GFF Rsp "
				 "x%x Error (%d %d) Data: x%x x%x\n",
				 did, ulp_status, ulp_word4,
				 vport->fc_flag, vport->fc_rscn_id_cnt);
	}

	/* This is a target port, unregistered port, or the GFF_ID failed */
	ndlp = lpfc_setup_disc_node(vport, did);
	if (ndlp) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0242 Process x%x GFF "
				 "NameServer Rsp Data: x%x x%x x%x\n",
				 did, ndlp->nlp_flag, vport->fc_flag,
				 vport->fc_rscn_id_cnt);
	} else {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0243 Skip x%x GFF "
				 "NameServer Rsp Data: x%x x%x\n", did,
				 vport->fc_flag, vport->fc_rscn_id_cnt);
	}
out:
	/* Link up / RSCN discovery */
	if (vport->num_disc_nodes)
		vport->num_disc_nodes--;

	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "6451 GFF_ID cmpl inp %d disc %d\n",
			 vport->gidft_inp, vport->num_disc_nodes);

	if (vport->num_disc_nodes == 0) {
		/*
		 * The driver has cycled through all Nports in the RSCN payload.
		 * Complete the handling by cleaning up and marking the
		 * current driver state.
		 */
		if (vport->port_state >= LPFC_DISC_AUTH) {
			if (vport->fc_flag & FC_RSCN_MODE) {
				lpfc_els_flush_rscn(vport);
				spin_lock_irq(shost->host_lock);
				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
				spin_unlock_irq(shost->host_lock);
			}
			else
				lpfc_els_flush_rscn(vport);
		}
		lpfc_disc_start(vport);
	}

iocb_free:
	free_ndlp = cmdiocb->ndlp;
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(free_ndlp);
	return;
}

static void
lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct lpfc_dmabuf *inp = cmdiocb->cmd_dmabuf;
	struct lpfc_dmabuf *outp = cmdiocb->rsp_dmabuf;
	struct lpfc_sli_ct_request *CTrsp;
	int did;
	struct lpfc_nodelist *ndlp = NULL;
	struct lpfc_nodelist *ns_ndlp = cmdiocb->ndlp;
	uint32_t fc4_data_0, fc4_data_1;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);

	did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
	did = be32_to_cpu(did);

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
			      "GFT_ID cmpl: status:x%x/x%x did:x%x",
			      ulp_status, ulp_word4, did);

	/* Ignore response if link flipped after this request was made */
	if ((uint32_t)cmdiocb->event_tag != phba->fc_eventTag) {
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "9046 Event tag mismatch. Ignoring NS rsp\n");
		goto out;
	}

	if (ulp_status == IOSTAT_SUCCESS) {
		/* Good status, continue checking */
		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
		fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
		fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);

		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "6432 Process GFT_ID rsp for %08x "
				 "Data %08x %08x %s %s\n",
				 did, fc4_data_0, fc4_data_1,
				 (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
				  "FCP" : " ",
				 (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
				  "NVME" : " ");

		/* Lookup the NPort_ID queried in the GFT_ID and find the
		 * driver's local node.  It's an error if the driver
		 * doesn't have one.
		 */
		ndlp = lpfc_findnode_did(vport, did);
		if (ndlp) {
			/* The bitmask value for FCP and NVME FCP types is
			 * the same because they are 32 bits distant from
			 * each other in word0 and word0.
			 */
			if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
				ndlp->nlp_fc4_type |= NLP_FC4_FCP;
			if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
			lpfc_printf_vlog(vport, KERN_INFO,
					 LOG_DISCOVERY | LOG_NODE,
					 "3064 Setting ndlp x%px, DID x%06x "
					 "with FC4 x%08x, Data: x%08x x%08x "
					 "%d\n",
					 ndlp, did, ndlp->nlp_fc4_type,
					 FC_TYPE_FCP, FC_TYPE_NVME,
					 ndlp->nlp_state);

			if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
			    ndlp->nlp_fc4_type) {
				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
				/* This is a fabric topology so if discovery
				 * started with an unsolicited PLOGI, don't
				 * send a PRLI.  Targets don't issue PLOGI or
				 * PRLI when acting as a target. Likely this is
				 * an initiator function.
				 */
				if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
					lpfc_nlp_set_state(vport, ndlp,
							   NLP_STE_PRLI_ISSUE);
					lpfc_issue_els_prli(vport, ndlp, 0);
				}
			} else if (!ndlp->nlp_fc4_type) {
				/* If fc4 type is still unknown, then LOGO */
				lpfc_printf_vlog(vport, KERN_INFO,
						 LOG_DISCOVERY | LOG_NODE,
						 "6443 Sending LOGO ndlp x%px,"
						 "DID x%06x with fc4_type: "
						 "x%08x, state: %d\n",
						 ndlp, did, ndlp->nlp_fc4_type,
						 ndlp->nlp_state);
				lpfc_issue_els_logo(vport, ndlp, 0);
				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
				lpfc_nlp_set_state(vport, ndlp,
						   NLP_STE_NPR_NODE);
			}
		}
	} else
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "3065 GFT_ID failed x%08x\n", ulp_status);

out:
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(ns_ndlp);
}

static void
lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
	     struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct lpfc_dmabuf *inp;
	struct lpfc_dmabuf *outp;
	struct lpfc_sli_ct_request *CTrsp;
	struct lpfc_nodelist *ndlp;
	int cmdcode, rc;
	uint8_t retry;
	uint32_t latt;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);

	/* First save ndlp, before we overwrite it */
	ndlp = cmdiocb->ndlp;

	/* we pass cmdiocb to state machine which needs rspiocb as well */
	cmdiocb->rsp_iocb = rspiocb;

	inp = cmdiocb->cmd_dmabuf;
	outp = cmdiocb->rsp_dmabuf;

	cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
					CommandResponse.bits.CmdRsp);
	CTrsp = (struct lpfc_sli_ct_request *) outp->virt;

	latt = lpfc_els_chk_latt(vport);

	/* RFT request completes status <ulp_status> CmdRsp <CmdRsp> */
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0209 CT Request completes, latt %d, "
			 "ulp_status x%x CmdRsp x%x, Context x%x, Tag x%x\n",
			 latt, ulp_status,
			 be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp),
			 get_job_ulpcontext(phba, cmdiocb), cmdiocb->iotag);

	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
		"CT cmd cmpl:     status:x%x/x%x cmd:x%x",
		ulp_status, ulp_word4, cmdcode);

	if (ulp_status) {
		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
				 "0268 NS cmd x%x Error (x%x x%x)\n",
				 cmdcode, ulp_status, ulp_word4);

		if (ulp_status == IOSTAT_LOCAL_REJECT &&
		    (((ulp_word4 & IOERR_PARAM_MASK) ==
		      IOERR_SLI_DOWN) ||
		     ((ulp_word4 & IOERR_PARAM_MASK) ==
		      IOERR_SLI_ABORTED)))
			goto out;

		retry = cmdiocb->retry;
		if (retry >= LPFC_MAX_NS_RETRY)
			goto out;

		retry++;
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0250 Retrying NS cmd %x\n", cmdcode);
		rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
		if (rc == 0)
			goto out;
	}

out:
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(ndlp);
	return;
}

static void
lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);

	if (ulp_status == IOSTAT_SUCCESS) {
		struct lpfc_dmabuf *outp;
		struct lpfc_sli_ct_request *CTrsp;

		outp = cmdiocb->rsp_dmabuf;
		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC)
			vport->ct_flags |= FC_CT_RFT_ID;
	}
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

static void
lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);

	if (ulp_status == IOSTAT_SUCCESS) {
		struct lpfc_dmabuf *outp;
		struct lpfc_sli_ct_request *CTrsp;

		outp = cmdiocb->rsp_dmabuf;
		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC)
			vport->ct_flags |= FC_CT_RNN_ID;
	}
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

static void
lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			 struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);

	if (ulp_status == IOSTAT_SUCCESS) {
		struct lpfc_dmabuf *outp;
		struct lpfc_sli_ct_request *CTrsp;

		outp = cmdiocb->rsp_dmabuf;
		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC)
			vport->ct_flags |= FC_CT_RSPN_ID;
	}
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

static void
lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			 struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);

	if (ulp_status == IOSTAT_SUCCESS) {
		struct lpfc_dmabuf *outp;
		struct lpfc_sli_ct_request *CTrsp;

		outp = cmdiocb->rsp_dmabuf;
		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC)
			vport->ct_flags |= FC_CT_RSNN_NN;
	}
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

static void
lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;

	/* even if it fails we will act as though it succeeded. */
	vport->ct_flags = 0;
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

static void
lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);

	if (ulp_status == IOSTAT_SUCCESS) {
		struct lpfc_dmabuf *outp;
		struct lpfc_sli_ct_request *CTrsp;

		outp = cmdiocb->rsp_dmabuf;
		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
		if (be16_to_cpu(CTrsp->CommandResponse.bits.CmdRsp) ==
		    SLI_CT_RESPONSE_FS_ACC)
			vport->ct_flags |= FC_CT_RFF_ID;
	}
	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
	return;
}

/*
 * Although the symbolic port name is thought to be an integer
 * as of January 18, 2016, leave it as a string until more of
 * the record state becomes defined.
 */
int
lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
	size_t size)
{
	int n;

	/*
	 * Use the lpfc board number as the Symbolic Port
	 * Name object.  NPIV is not in play so this integer
	 * value is sufficient and unique per FC-ID.
	 */
	n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
	return n;
}


int
lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
	size_t size)
{
	char fwrev[FW_REV_STR_SIZE] = {0};
	char tmp[MAXHOSTNAMELEN] = {0};

	memset(symbol, 0, size);

	scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
	if (strlcat(symbol, tmp, size) >= size)
		goto buffer_done;

	lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
	scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
	if (strlcat(symbol, tmp, size) >= size)
		goto buffer_done;

	scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
	if (strlcat(symbol, tmp, size) >= size)
		goto buffer_done;

	scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
	if (strlcat(symbol, tmp, size) >= size)
		goto buffer_done;

	/* Note :- OS name is "Linux" */
	scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
	strlcat(symbol, tmp, size);

buffer_done:
	return strnlen(symbol, size);

}

static uint32_t
lpfc_find_map_node(struct lpfc_vport *vport)
{
	struct lpfc_nodelist *ndlp, *next_ndlp;
	struct Scsi_Host  *shost;
	uint32_t cnt = 0;

	shost = lpfc_shost_from_vport(vport);
	spin_lock_irq(shost->host_lock);
	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
		if (ndlp->nlp_type & NLP_FABRIC)
			continue;
		if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
			cnt++;
	}
	spin_unlock_irq(shost->host_lock);
	return cnt;
}

/*
 * This routine will return the FC4 Type associated with the CT
 * GID_FT command.
 */
int
lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
{
	struct lpfc_sli_ct_request *CtReq;
	struct lpfc_dmabuf *mp;
	uint32_t type;

	mp = cmdiocb->cmd_dmabuf;
	if (mp == NULL)
		return 0;
	CtReq = (struct lpfc_sli_ct_request *)mp->virt;
	type = (uint32_t)CtReq->un.gid.Fc4Type;
	if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
		return 0;
	return type;
}

/*
 * lpfc_ns_cmd
 * Description:
 *    Issue Cmd to NameServer
 *       SLI_CTNS_GID_FT
 *       LI_CTNS_RFT_ID
 */
int
lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
	    uint8_t retry, uint32_t context)
{
	struct lpfc_nodelist * ndlp;
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_dmabuf *mp, *bmp;
	struct lpfc_sli_ct_request *CtReq;
	struct ulp_bde64 *bpl;
	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
		      struct lpfc_iocbq *) = NULL;
	uint32_t *ptr;
	uint32_t rsp_size = 1024;
	size_t   size;
	int rc = 0;

	ndlp = lpfc_findnode_did(vport, NameServer_DID);
	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
		rc=1;
		goto ns_cmd_exit;
	}

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!mp) {
		rc=2;
		goto ns_cmd_exit;
	}

	INIT_LIST_HEAD(&mp->list);
	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
	if (!mp->virt) {
		rc=3;
		goto ns_cmd_free_mp;
	}

	/* Allocate buffer for Buffer ptr list */
	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!bmp) {
		rc=4;
		goto ns_cmd_free_mpvirt;
	}

	INIT_LIST_HEAD(&bmp->list);
	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
	if (!bmp->virt) {
		rc=5;
		goto ns_cmd_free_bmp;
	}

	/* NameServer Req */
	lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
			 "0236 NameServer Req Data: x%x x%x x%x x%x\n",
			 cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
			 context);

	bpl = (struct ulp_bde64 *) bmp->virt;
	memset(bpl, 0, sizeof(struct ulp_bde64));
	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
	bpl->tus.f.bdeFlags = 0;
	if (cmdcode == SLI_CTNS_GID_FT)
		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_GID_PT)
		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_GFF_ID)
		bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_GFT_ID)
		bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_RFT_ID)
		bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_RNN_ID)
		bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_RSPN_ID)
		bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_RSNN_NN)
		bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_DA_ID)
		bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
	else if (cmdcode == SLI_CTNS_RFF_ID)
		bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
	else
		bpl->tus.f.bdeSize = 0;
	bpl->tus.w = le32_to_cpu(bpl->tus.w);

	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
	CtReq->RevisionId.bits.InId = 0;
	CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
	CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
	CtReq->CommandResponse.bits.Size = 0;
	switch (cmdcode) {
	case SLI_CTNS_GID_FT:
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_GID_FT);
		CtReq->un.gid.Fc4Type = context;

		if (vport->port_state < LPFC_NS_QRY)
			vport->port_state = LPFC_NS_QRY;
		lpfc_set_disctmo(vport);
		cmpl = lpfc_cmpl_ct_cmd_gid_ft;
		rsp_size = FC_MAX_NS_RSP;
		break;

	case SLI_CTNS_GID_PT:
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_GID_PT);
		CtReq->un.gid.PortType = context;

		if (vport->port_state < LPFC_NS_QRY)
			vport->port_state = LPFC_NS_QRY;
		lpfc_set_disctmo(vport);
		cmpl = lpfc_cmpl_ct_cmd_gid_pt;
		rsp_size = FC_MAX_NS_RSP;
		break;

	case SLI_CTNS_GFF_ID:
		CtReq->CommandResponse.bits.CmdRsp =
			cpu_to_be16(SLI_CTNS_GFF_ID);
		CtReq->un.gff.PortId = cpu_to_be32(context);
		cmpl = lpfc_cmpl_ct_cmd_gff_id;
		break;

	case SLI_CTNS_GFT_ID:
		CtReq->CommandResponse.bits.CmdRsp =
			cpu_to_be16(SLI_CTNS_GFT_ID);
		CtReq->un.gft.PortId = cpu_to_be32(context);
		cmpl = lpfc_cmpl_ct_cmd_gft_id;
		break;

	case SLI_CTNS_RFT_ID:
		vport->ct_flags &= ~FC_CT_RFT_ID;
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_RFT_ID);
		CtReq->un.rft.port_id = cpu_to_be32(vport->fc_myDID);

		/* Register Application Services type if vmid enabled. */
		if (phba->cfg_vmid_app_header)
			CtReq->un.rft.app_serv_reg =
				cpu_to_be32(RFT_APP_SERV_REG);

		/* Register FC4 FCP type if enabled.  */
		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
		    vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
			CtReq->un.rft.fcp_reg = cpu_to_be32(RFT_FCP_REG);

		/* Register NVME type if enabled. */
		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
		    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
			CtReq->un.rft.nvme_reg = cpu_to_be32(RFT_NVME_REG);

		ptr = (uint32_t *)CtReq;
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "6433 Issue RFT (%s %s %s): %08x %08x %08x "
				 "%08x %08x %08x %08x %08x\n",
				 CtReq->un.rft.fcp_reg ? "FCP" : " ",
				 CtReq->un.rft.nvme_reg ? "NVME" : " ",
				 CtReq->un.rft.app_serv_reg ? "APPS" : " ",
				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
				 *(ptr + 4), *(ptr + 5),
				 *(ptr + 6), *(ptr + 7));
		cmpl = lpfc_cmpl_ct_cmd_rft_id;
		break;

	case SLI_CTNS_RNN_ID:
		vport->ct_flags &= ~FC_CT_RNN_ID;
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_RNN_ID);
		CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
		memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
		       sizeof(struct lpfc_name));
		cmpl = lpfc_cmpl_ct_cmd_rnn_id;
		break;

	case SLI_CTNS_RSPN_ID:
		vport->ct_flags &= ~FC_CT_RSPN_ID;
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_RSPN_ID);
		CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
		size = sizeof(CtReq->un.rspn.symbname);
		CtReq->un.rspn.len =
			lpfc_vport_symbolic_port_name(vport,
			CtReq->un.rspn.symbname, size);
		cmpl = lpfc_cmpl_ct_cmd_rspn_id;
		break;
	case SLI_CTNS_RSNN_NN:
		vport->ct_flags &= ~FC_CT_RSNN_NN;
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_RSNN_NN);
		memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
		       sizeof(struct lpfc_name));
		size = sizeof(CtReq->un.rsnn.symbname);
		CtReq->un.rsnn.len =
			lpfc_vport_symbolic_node_name(vport,
			CtReq->un.rsnn.symbname, size);
		cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
		break;
	case SLI_CTNS_DA_ID:
		/* Implement DA_ID Nameserver request */
		CtReq->CommandResponse.bits.CmdRsp =
			cpu_to_be16(SLI_CTNS_DA_ID);
		CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
		cmpl = lpfc_cmpl_ct_cmd_da_id;
		break;
	case SLI_CTNS_RFF_ID:
		vport->ct_flags &= ~FC_CT_RFF_ID;
		CtReq->CommandResponse.bits.CmdRsp =
		    cpu_to_be16(SLI_CTNS_RFF_ID);
		CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
		CtReq->un.rff.fbits = FC4_FEATURE_INIT;

		/* The driver always supports FC_TYPE_FCP.  However, the
		 * caller can specify NVME (type x28) as well.  But only
		 * these that FC4 type is supported.
		 */
		if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
		     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
		    (context == FC_TYPE_NVME)) {
			if ((vport == phba->pport) && phba->nvmet_support) {
				CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
					FC4_FEATURE_NVME_DISC);
				lpfc_nvmet_update_targetport(phba);
			} else {
				lpfc_nvme_update_localport(vport);
			}
			CtReq->un.rff.type_code = context;

		} else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
			   (context == FC_TYPE_FCP))
			CtReq->un.rff.type_code = context;

		else
			goto ns_cmd_free_bmpvirt;

		ptr = (uint32_t *)CtReq;
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "6434 Issue RFF (%s): %08x %08x %08x %08x "
				 "%08x %08x %08x %08x\n",
				 (context == FC_TYPE_NVME) ? "NVME" : "FCP",
				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
				 *(ptr + 4), *(ptr + 5),
				 *(ptr + 6), *(ptr + 7));
		cmpl = lpfc_cmpl_ct_cmd_rff_id;
		break;
	}
	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
	 * to hold ndlp reference for the corresponding callback function.
	 */
	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
		/* On success, The cmpl function will free the buffers */
		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
			"Issue CT cmd:    cmd:x%x did:x%x",
			cmdcode, ndlp->nlp_DID, 0);
		return 0;
	}
	rc=6;

ns_cmd_free_bmpvirt:
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
ns_cmd_free_bmp:
	kfree(bmp);
ns_cmd_free_mpvirt:
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
ns_cmd_free_mp:
	kfree(mp);
ns_cmd_exit:
	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
			 "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
			 cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
	return 1;
}

/**
 * lpfc_fdmi_rprt_defer - Check for any deferred FDMI RPRT commands
 * @phba: Pointer to HBA context object.
 * @mask: Initial port attributes mask
 *
 * This function checks to see if any vports have deferred their FDMI RPRT.
 * A vports RPRT may be deferred if it is issued before the primary ports
 * RHBA completes.
 */
static void
lpfc_fdmi_rprt_defer(struct lpfc_hba *phba, uint32_t mask)
{
	struct lpfc_vport **vports;
	struct lpfc_vport *vport;
	struct lpfc_nodelist *ndlp;
	int i;

	phba->hba_flag |= HBA_RHBA_CMPL;
	vports = lpfc_create_vport_work_array(phba);
	if (vports) {
		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
			vport = vports[i];
			ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
			if (!ndlp)
				continue;
			if (vport->ct_flags & FC_CT_RPRT_DEFER) {
				vport->ct_flags &= ~FC_CT_RPRT_DEFER;
				vport->fdmi_port_mask = mask;
				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
			}
		}
	}
	lpfc_destroy_vport_work_array(phba, vports);
}

/**
 * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
 * @phba: Pointer to HBA context object.
 * @cmdiocb: Pointer to the command IOCBQ.
 * @rspiocb: Pointer to the response IOCBQ.
 *
 * This function to handle the completion of a driver initiated FDMI
 * CT command issued during discovery.
 */
static void
lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		       struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct lpfc_dmabuf *inp = cmdiocb->cmd_dmabuf;
	struct lpfc_dmabuf *outp = cmdiocb->rsp_dmabuf;
	struct lpfc_sli_ct_request *CTcmd = inp->virt;
	struct lpfc_sli_ct_request *CTrsp = outp->virt;
	__be16 fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
	__be16 fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
	struct lpfc_nodelist *ndlp, *free_ndlp = NULL;
	uint32_t latt, cmd, err;
	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
	u32 ulp_word4 = get_job_word4(phba, rspiocb);

	latt = lpfc_els_chk_latt(vport);
	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
		"FDMI cmpl:       status:x%x/x%x latt:%d",
		ulp_status, ulp_word4, latt);

	if (latt || ulp_status) {

		/* Look for a retryable error */
		if (ulp_status == IOSTAT_LOCAL_REJECT) {
			switch ((ulp_word4 & IOERR_PARAM_MASK)) {
			case IOERR_SLI_ABORTED:
			case IOERR_SLI_DOWN:
				/* Driver aborted this IO.  No retry as error
				 * is likely Offline->Online or some adapter
				 * error.  Recovery will try again.
				 */
				break;
			case IOERR_ABORT_IN_PROGRESS:
			case IOERR_SEQUENCE_TIMEOUT:
			case IOERR_ILLEGAL_FRAME:
			case IOERR_NO_RESOURCES:
			case IOERR_ILLEGAL_COMMAND:
				cmdiocb->retry++;
				if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
					break;

				/* Retry the same FDMI command */
				err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
							  cmdiocb, 0);
				if (err == IOCB_ERROR)
					break;
				return;
			default:
				break;
			}
		}

		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
				 "0229 FDMI cmd %04x failed, latt = %d "
				 "ulp_status: x%x, rid x%x\n",
				 be16_to_cpu(fdmi_cmd), latt, ulp_status,
				 ulp_word4);
	}

	free_ndlp = cmdiocb->ndlp;
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(free_ndlp);

	ndlp = lpfc_findnode_did(vport, FDMI_DID);
	if (!ndlp)
		return;

	/* Check for a CT LS_RJT response */
	cmd =  be16_to_cpu(fdmi_cmd);
	if (be16_to_cpu(fdmi_rsp) == SLI_CT_RESPONSE_FS_RJT) {
		/* FDMI rsp failed */
		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY | LOG_ELS,
				 "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);

		/* Should we fallback to FDMI-2 / FDMI-1 ? */
		switch (cmd) {
		case SLI_MGMT_RHBA:
			if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
				/* Fallback to FDMI-1 for HBA attributes */
				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;

				/* If HBA attributes are FDMI1, so should
				 * port attributes be for consistency.
				 */
				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
				/* Start over */
				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
			}
			return;

		case SLI_MGMT_RPRT:
			if (vport->port_type != LPFC_PHYSICAL_PORT) {
				ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
				if (!ndlp)
					return;
			}
			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
				/* Fallback to FDMI-1 */
				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
				/* Start over */
				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
				return;
			}
			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
				/* Retry the same command */
				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
			}
			return;

		case SLI_MGMT_RPA:
			/* No retry on Vendor, RPA only done on physical port */
			if (phba->link_flag & LS_CT_VEN_RPA) {
				phba->link_flag &= ~LS_CT_VEN_RPA;
				if (phba->cmf_active_mode == LPFC_CFG_OFF)
					return;
				lpfc_printf_log(phba, KERN_WARNING,
						LOG_DISCOVERY | LOG_ELS,
						"6460 VEN FDMI RPA RJT\n");
				return;
			}
			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
				/* Fallback to FDMI-1 */
				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
				/* Start over */
				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
				return;
			}
			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
				/* Retry the same command */
				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
			}
			return;
		}
	}

	/*
	 * On success, need to cycle thru FDMI registration for discovery
	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
	 * DPRT -> RPRT (vports)
	 */
	switch (cmd) {
	case SLI_MGMT_RHBA:
		/* Check for any RPRTs deferred till after RHBA completes */
		lpfc_fdmi_rprt_defer(phba, vport->fdmi_port_mask);

		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
		break;

	case SLI_MGMT_DHBA:
		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
		break;

	case SLI_MGMT_DPRT:
		if (vport->port_type == LPFC_PHYSICAL_PORT) {
			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
		} else {
			ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
			if (!ndlp)
				return;

			/* Only issue a RPRT for the vport if the RHBA
			 * for the physical port completes successfully.
			 * We may have to defer the RPRT accordingly.
			 */
			if (phba->hba_flag & HBA_RHBA_CMPL) {
				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
			} else {
				lpfc_printf_vlog(vport, KERN_INFO,
						 LOG_DISCOVERY,
						 "6078 RPRT deferred\n");
				vport->ct_flags |= FC_CT_RPRT_DEFER;
			}
		}
		break;
	case SLI_MGMT_RPA:
		if (vport->port_type == LPFC_PHYSICAL_PORT &&
		    phba->sli4_hba.pc_sli4_params.mi_ver) {
			/* mi is only for the phyical port, no vports */
			if (phba->link_flag & LS_CT_VEN_RPA) {
				lpfc_printf_vlog(vport, KERN_INFO,
						 LOG_DISCOVERY | LOG_ELS |
						 LOG_CGN_MGMT,
						 "6449 VEN RPA FDMI Success\n");
				phba->link_flag &= ~LS_CT_VEN_RPA;
				break;
			}

			lpfc_printf_log(phba, KERN_INFO,
					LOG_DISCOVERY | LOG_CGN_MGMT,
					"6210 Issue Vendor MI FDMI %x\n",
					phba->sli4_hba.pc_sli4_params.mi_ver);

			/* CGN is only for the physical port, no vports */
			if (lpfc_fdmi_cmd(vport, ndlp, cmd,
					  LPFC_FDMI_VENDOR_ATTR_mi) == 0)
				phba->link_flag |= LS_CT_VEN_RPA;
			lpfc_printf_log(phba, KERN_INFO,
					LOG_DISCOVERY | LOG_ELS,
					"6458 Send MI FDMI:%x Flag x%x\n",
					phba->sli4_hba.pc_sli4_params.mi_ver,
					phba->link_flag);
		} else {
			lpfc_printf_log(phba, KERN_INFO,
					LOG_DISCOVERY | LOG_ELS,
					"6459 No FDMI VEN MI support - "
					"RPA Success\n");
		}
		break;
	}
	return;
}


/**
 * lpfc_fdmi_change_check - Check for changed FDMI parameters
 * @vport: pointer to a host virtual N_Port data structure.
 *
 * Check how many mapped NPorts we are connected to
 * Check if our hostname changed
 * Called from hbeat timeout routine to check if any FDMI parameters
 * changed. If so, re-register those Attributes.
 */
void
lpfc_fdmi_change_check(struct lpfc_vport *vport)
{
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_nodelist *ndlp;
	uint16_t cnt;

	if (!lpfc_is_link_up(phba))
		return;

	/* Must be connected to a Fabric */
	if (!(vport->fc_flag & FC_FABRIC))
		return;

	ndlp = lpfc_findnode_did(vport, FDMI_DID);
	if (!ndlp)
		return;

	/* Check if system hostname changed */
	if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
		memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
		scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
			  init_utsname()->nodename);
		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);

		/* Since this effects multiple HBA and PORT attributes, we need
		 * de-register and go thru the whole FDMI registration cycle.
		 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
		 * DPRT -> RPRT (vports)
		 */
		if (vport->port_type == LPFC_PHYSICAL_PORT) {
			/* For extra Vendor RPA */
			phba->link_flag &= ~LS_CT_VEN_RPA;
			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
		} else {
			ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
			if (!ndlp)
				return;
			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
		}

		/* Since this code path registers all the port attributes
		 * we can just return without further checking.
		 */
		return;
	}

	if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
		return;

	/* Check if the number of mapped NPorts changed */
	cnt = lpfc_find_map_node(vport);
	if (cnt == vport->fdmi_num_disc)
		return;

	if (vport->port_type == LPFC_PHYSICAL_PORT) {
		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
			      LPFC_FDMI_PORT_ATTR_num_disc);
	} else {
		ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
		if (!ndlp)
			return;
		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
			      LPFC_FDMI_PORT_ATTR_num_disc);
	}
}

static inline int
lpfc_fdmi_set_attr_u32(void *attr, uint16_t attrtype, uint32_t attrval)
{
	struct lpfc_fdmi_attr_u32 *ae = attr;
	int size = sizeof(*ae);

	ae->type = cpu_to_be16(attrtype);
	ae->len = cpu_to_be16(size);
	ae->value_u32 = cpu_to_be32(attrval);

	return size;
}

static inline int
lpfc_fdmi_set_attr_wwn(void *attr, uint16_t attrtype, struct lpfc_name *wwn)
{
	struct lpfc_fdmi_attr_wwn *ae = attr;
	int size = sizeof(*ae);

	ae->type = cpu_to_be16(attrtype);
	ae->len = cpu_to_be16(size);
	/* WWN's assumed to be bytestreams - Big Endian presentation */
	memcpy(ae->name, wwn,
	       min_t(size_t, sizeof(struct lpfc_name), sizeof(__be64)));

	return size;
}

static inline int
lpfc_fdmi_set_attr_fullwwn(void *attr, uint16_t attrtype,
			   struct lpfc_name *wwnn, struct lpfc_name *wwpn)
{
	struct lpfc_fdmi_attr_fullwwn *ae = attr;
	u8 *nname = ae->nname;
	u8 *pname = ae->pname;
	int size = sizeof(*ae);

	ae->type = cpu_to_be16(attrtype);
	ae->len = cpu_to_be16(size);
	/* WWN's assumed to be bytestreams - Big Endian presentation */
	memcpy(nname, wwnn,
	       min_t(size_t, sizeof(struct lpfc_name), sizeof(__be64)));
	memcpy(pname, wwpn,
	       min_t(size_t, sizeof(struct lpfc_name), sizeof(__be64)));

	return size;
}

static inline int
lpfc_fdmi_set_attr_string(void *attr, uint16_t attrtype, char *attrstring)
{
	struct lpfc_fdmi_attr_string *ae = attr;
	int len, size;

	/*
	 * We are trusting the caller that if a fdmi string field
	 * is capped at 64 bytes, the caller passes in a string of
	 * 64 bytes or less.
	 */

	strncpy(ae->value_string, attrstring, sizeof(ae->value_string));
	len = strnlen(ae->value_string, sizeof(ae->value_string));
	/* round string length to a 32bit boundary. Ensure there's a NULL */
	len += (len & 3) ? (4 - (len & 3)) : 4;
	/* size is Type/Len (4 bytes) plus string length */
	size = FOURBYTES + len;

	ae->type = cpu_to_be16(attrtype);
	ae->len = cpu_to_be16(size);

	return size;
}

/* Bitfields for FC4 Types that can be reported */
#define ATTR_FC4_CT	0x00000001
#define ATTR_FC4_FCP	0x00000002
#define ATTR_FC4_NVME	0x00000004

static inline int
lpfc_fdmi_set_attr_fc4types(void *attr, uint16_t attrtype, uint32_t typemask)
{
	struct lpfc_fdmi_attr_fc4types *ae = attr;
	int size = sizeof(*ae);

	ae->type = cpu_to_be16(attrtype);
	ae->len = cpu_to_be16(size);

	if (typemask & ATTR_FC4_FCP)
		ae->value_types[2] = 0x01; /* Type 0x8 - FCP */

	if (typemask & ATTR_FC4_CT)
		ae->value_types[7] = 0x01; /* Type 0x20 - CT */

	if (typemask & ATTR_FC4_NVME)
		ae->value_types[6] = 0x01; /* Type 0x28 - NVME */

	return size;
}

/* Routines for all individual HBA attributes */
static int
lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_wwn(attr, RHBA_NODENAME,
			&vport->fc_sparam.nodeName);
}

static int
lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport, void *attr)
{
	/* This string MUST be consistent with other FC platforms
	 * supported by Broadcom.
	 */
	return lpfc_fdmi_set_attr_string(attr, RHBA_MANUFACTURER,
			"Emulex Corporation");
}

static int
lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_string(attr, RHBA_SERIAL_NUMBER,
			phba->SerialNumber);
}

static int
lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_string(attr, RHBA_MODEL,
			phba->ModelName);
}

static int
lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_string(attr, RHBA_MODEL_DESCRIPTION,
			phba->ModelDesc);
}

static int
lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	lpfc_vpd_t *vp = &phba->vpd;
	char buf[16] = { 0 };

	snprintf(buf, sizeof(buf), "%08x", vp->rev.biuRev);

	return lpfc_fdmi_set_attr_string(attr, RHBA_HARDWARE_VERSION, buf);
}

static int
lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_string(attr, RHBA_DRIVER_VERSION,
			lpfc_release_version);
}

static int
lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	char buf[64] = { 0 };

	if (phba->sli_rev == LPFC_SLI_REV4) {
		lpfc_decode_firmware_rev(phba, buf, 1);

		return lpfc_fdmi_set_attr_string(attr, RHBA_OPTION_ROM_VERSION,
				buf);
	}

	return lpfc_fdmi_set_attr_string(attr, RHBA_OPTION_ROM_VERSION,
			phba->OptionROMVersion);
}

static int
lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	char buf[64] = { 0 };

	lpfc_decode_firmware_rev(phba, buf, 1);

	return lpfc_fdmi_set_attr_string(attr, RHBA_FIRMWARE_VERSION, buf);
}

static int
lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport, void *attr)
{
	char buf[256] = { 0 };

	snprintf(buf, sizeof(buf), "%s %s %s",
		 init_utsname()->sysname,
		 init_utsname()->release,
		 init_utsname()->version);

	return lpfc_fdmi_set_attr_string(attr, RHBA_OS_NAME_VERSION, buf);
}

static int
lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RHBA_MAX_CT_PAYLOAD_LEN,
			LPFC_MAX_CT_SIZE);
}

static int
lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport, void *attr)
{
	char buf[256] = { 0 };

	lpfc_vport_symbolic_node_name(vport, buf, sizeof(buf));

	return lpfc_fdmi_set_attr_string(attr, RHBA_SYM_NODENAME, buf);
}

static int
lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RHBA_VENDOR_INFO, 0);
}

static int
lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport, void *attr)
{
	/* Each driver instance corresponds to a single port */
	return lpfc_fdmi_set_attr_u32(attr, RHBA_NUM_PORTS, 1);
}

static int
lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_wwn(attr, RHBA_FABRIC_WWNN,
			&vport->fabric_nodename);
}

static int
lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_string(attr, RHBA_BIOS_VERSION,
			phba->BIOSVersion);
}

static int
lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport, void *attr)
{
	/* Driver doesn't have access to this information */
	return lpfc_fdmi_set_attr_u32(attr, RHBA_BIOS_STATE, 0);
}

static int
lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_string(attr, RHBA_VENDOR_ID, "EMULEX");
}

/*
 * Routines for all individual PORT attributes
 */

static int
lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba   *phba = vport->phba;
	u32 fc4types;

	fc4types = (ATTR_FC4_CT | ATTR_FC4_FCP);

	/* Check to see if Firmware supports NVME and on physical port */
	if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
	    phba->sli4_hba.pc_sli4_params.nvme)
		fc4types |= ATTR_FC4_NVME;

	return lpfc_fdmi_set_attr_fc4types(attr, RPRT_SUPPORTED_FC4_TYPES,
			fc4types);
}

static int
lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	u32 speeds = 0;
	u32 tcfg;
	u8 i, cnt;

	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
		cnt = 0;
		if (phba->sli_rev == LPFC_SLI_REV4) {
			tcfg = phba->sli4_hba.conf_trunk;
			for (i = 0; i < 4; i++, tcfg >>= 1)
				if (tcfg & 1)
					cnt++;
		}

		if (cnt > 2) { /* 4 lane trunk group */
			if (phba->lmt & LMT_64Gb)
				speeds |= HBA_PORTSPEED_256GFC;
			if (phba->lmt & LMT_32Gb)
				speeds |= HBA_PORTSPEED_128GFC;
			if (phba->lmt & LMT_16Gb)
				speeds |= HBA_PORTSPEED_64GFC;
		} else if (cnt) { /* 2 lane trunk group */
			if (phba->lmt & LMT_128Gb)
				speeds |= HBA_PORTSPEED_256GFC;
			if (phba->lmt & LMT_64Gb)
				speeds |= HBA_PORTSPEED_128GFC;
			if (phba->lmt & LMT_32Gb)
				speeds |= HBA_PORTSPEED_64GFC;
			if (phba->lmt & LMT_16Gb)
				speeds |= HBA_PORTSPEED_32GFC;
		} else {
			if (phba->lmt & LMT_256Gb)
				speeds |= HBA_PORTSPEED_256GFC;
			if (phba->lmt & LMT_128Gb)
				speeds |= HBA_PORTSPEED_128GFC;
			if (phba->lmt & LMT_64Gb)
				speeds |= HBA_PORTSPEED_64GFC;
			if (phba->lmt & LMT_32Gb)
				speeds |= HBA_PORTSPEED_32GFC;
			if (phba->lmt & LMT_16Gb)
				speeds |= HBA_PORTSPEED_16GFC;
			if (phba->lmt & LMT_10Gb)
				speeds |= HBA_PORTSPEED_10GFC;
			if (phba->lmt & LMT_8Gb)
				speeds |= HBA_PORTSPEED_8GFC;
			if (phba->lmt & LMT_4Gb)
				speeds |= HBA_PORTSPEED_4GFC;
			if (phba->lmt & LMT_2Gb)
				speeds |= HBA_PORTSPEED_2GFC;
			if (phba->lmt & LMT_1Gb)
				speeds |= HBA_PORTSPEED_1GFC;
		}
	} else {
		/* FCoE links support only one speed */
		switch (phba->fc_linkspeed) {
		case LPFC_ASYNC_LINK_SPEED_10GBPS:
			speeds = HBA_PORTSPEED_10GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_25GBPS:
			speeds = HBA_PORTSPEED_25GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_40GBPS:
			speeds = HBA_PORTSPEED_40GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_100GBPS:
			speeds = HBA_PORTSPEED_100GE;
			break;
		}
	}

	return lpfc_fdmi_set_attr_u32(attr, RPRT_SUPPORTED_SPEED, speeds);
}

static int
lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba   *phba = vport->phba;
	u32 speeds = 0;

	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
		switch (phba->fc_linkspeed) {
		case LPFC_LINK_SPEED_1GHZ:
			speeds = HBA_PORTSPEED_1GFC;
			break;
		case LPFC_LINK_SPEED_2GHZ:
			speeds = HBA_PORTSPEED_2GFC;
			break;
		case LPFC_LINK_SPEED_4GHZ:
			speeds = HBA_PORTSPEED_4GFC;
			break;
		case LPFC_LINK_SPEED_8GHZ:
			speeds = HBA_PORTSPEED_8GFC;
			break;
		case LPFC_LINK_SPEED_10GHZ:
			speeds = HBA_PORTSPEED_10GFC;
			break;
		case LPFC_LINK_SPEED_16GHZ:
			speeds = HBA_PORTSPEED_16GFC;
			break;
		case LPFC_LINK_SPEED_32GHZ:
			speeds = HBA_PORTSPEED_32GFC;
			break;
		case LPFC_LINK_SPEED_64GHZ:
			speeds = HBA_PORTSPEED_64GFC;
			break;
		case LPFC_LINK_SPEED_128GHZ:
			speeds = HBA_PORTSPEED_128GFC;
			break;
		case LPFC_LINK_SPEED_256GHZ:
			speeds = HBA_PORTSPEED_256GFC;
			break;
		default:
			speeds = HBA_PORTSPEED_UNKNOWN;
			break;
		}
	} else {
		switch (phba->fc_linkspeed) {
		case LPFC_ASYNC_LINK_SPEED_10GBPS:
			speeds = HBA_PORTSPEED_10GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_25GBPS:
			speeds = HBA_PORTSPEED_25GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_40GBPS:
			speeds = HBA_PORTSPEED_40GE;
			break;
		case LPFC_ASYNC_LINK_SPEED_100GBPS:
			speeds = HBA_PORTSPEED_100GE;
			break;
		default:
			speeds = HBA_PORTSPEED_UNKNOWN;
			break;
		}
	}

	return lpfc_fdmi_set_attr_u32(attr, RPRT_PORT_SPEED, speeds);
}

static int
lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport, void *attr)
{
	struct serv_parm *hsp = (struct serv_parm *)&vport->fc_sparam;

	return lpfc_fdmi_set_attr_u32(attr, RPRT_MAX_FRAME_SIZE,
			(((uint32_t)hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
			  (uint32_t)hsp->cmn.bbRcvSizeLsb);
}

static int
lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport, void *attr)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
	char buf[64] = { 0 };

	snprintf(buf, sizeof(buf), "/sys/class/scsi_host/host%d",
		 shost->host_no);

	return lpfc_fdmi_set_attr_string(attr, RPRT_OS_DEVICE_NAME, buf);
}

static int
lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport, void *attr)
{
	char buf[64] = { 0 };

	scnprintf(buf, sizeof(buf), "%s", vport->phba->os_host_name);

	return lpfc_fdmi_set_attr_string(attr, RPRT_HOST_NAME, buf);
}

static int
lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_wwn(attr, RPRT_NODENAME,
			&vport->fc_sparam.nodeName);
}

static int
lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_wwn(attr, RPRT_PORTNAME,
			&vport->fc_sparam.portName);
}

static int
lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport, void *attr)
{
	char buf[256] = { 0 };

	lpfc_vport_symbolic_port_name(vport, buf, sizeof(buf));

	return lpfc_fdmi_set_attr_string(attr, RPRT_SYM_PORTNAME, buf);
}

static int
lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_u32(attr, RPRT_PORT_TYPE,
			(phba->fc_topology == LPFC_TOPOLOGY_LOOP) ?
				LPFC_FDMI_PORTTYPE_NLPORT :
				LPFC_FDMI_PORTTYPE_NPORT);
}

static int
lpfc_fdmi_port_attr_class(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RPRT_SUPPORTED_CLASS,
			FC_COS_CLASS2 | FC_COS_CLASS3);
}

static int
lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_wwn(attr, RPRT_FABRICNAME,
			&vport->fabric_portname);
}

static int
lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	u32 fc4types;

	fc4types = (ATTR_FC4_CT | ATTR_FC4_FCP);

	/* Check to see if NVME is configured or not */
	if (vport == phba->pport &&
	    phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
		fc4types |= ATTR_FC4_NVME;

	return lpfc_fdmi_set_attr_fc4types(attr, RPRT_ACTIVE_FC4_TYPES,
			fc4types);
}

static int
lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RPRT_PORT_STATE,
			LPFC_FDMI_PORTSTATE_ONLINE);
}

static int
lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport, void *attr)
{
	vport->fdmi_num_disc = lpfc_find_map_node(vport);

	return lpfc_fdmi_set_attr_u32(attr, RPRT_DISC_PORT,
			vport->fdmi_num_disc);
}

static int
lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RPRT_PORT_ID, vport->fc_myDID);
}

static int
lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_string(attr, RPRT_SMART_SERVICE,
			"Smart SAN Initiator");
}

static int
lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_fullwwn(attr, RPRT_SMART_GUID,
			&vport->fc_sparam.nodeName,
			&vport->fc_sparam.portName);
}

static int
lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_string(attr, RPRT_SMART_VERSION,
			"Smart SAN Version 2.0");
}

static int
lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;

	return lpfc_fdmi_set_attr_string(attr, RPRT_SMART_MODEL,
			phba->ModelName);
}

static int
lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport, void *attr)
{
	/* SRIOV (type 3) is not supported */

	return lpfc_fdmi_set_attr_u32(attr, RPRT_SMART_PORT_INFO,
			(vport->vpi) ?  2 /* NPIV */ : 1 /* Physical */);
}

static int
lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RPRT_SMART_QOS, 0);
}

static int
lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport, void *attr)
{
	return lpfc_fdmi_set_attr_u32(attr, RPRT_SMART_SECURITY, 1);
}

static int
lpfc_fdmi_vendor_attr_mi(struct lpfc_vport *vport, void *attr)
{
	struct lpfc_hba *phba = vport->phba;
	char buf[32] = { 0 };

	sprintf(buf, "ELXE2EM:%04d", phba->sli4_hba.pc_sli4_params.mi_ver);

	return lpfc_fdmi_set_attr_string(attr, RPRT_VENDOR_MI, buf);
}

/* RHBA attribute jump table */
static int (*lpfc_fdmi_hba_action[])
	(struct lpfc_vport *vport, void *attrbuf) = {
	/* Action routine                 Mask bit     Attribute type */
	lpfc_fdmi_hba_attr_wwnn,	  /* bit0     RHBA_NODENAME           */
	lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
	lpfc_fdmi_hba_attr_sn,		  /* bit2     RHBA_SERIAL_NUMBER      */
	lpfc_fdmi_hba_attr_model,	  /* bit3     RHBA_MODEL              */
	lpfc_fdmi_hba_attr_description,	  /* bit4     RHBA_MODEL_DESCRIPTION  */
	lpfc_fdmi_hba_attr_hdw_ver,	  /* bit5     RHBA_HARDWARE_VERSION   */
	lpfc_fdmi_hba_attr_drvr_ver,	  /* bit6     RHBA_DRIVER_VERSION     */
	lpfc_fdmi_hba_attr_rom_ver,	  /* bit7     RHBA_OPTION_ROM_VERSION */
	lpfc_fdmi_hba_attr_fmw_ver,	  /* bit8     RHBA_FIRMWARE_VERSION   */
	lpfc_fdmi_hba_attr_os_ver,	  /* bit9     RHBA_OS_NAME_VERSION    */
	lpfc_fdmi_hba_attr_ct_len,	  /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
	lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
	lpfc_fdmi_hba_attr_vendor_info,	  /* bit12    RHBA_VENDOR_INFO        */
	lpfc_fdmi_hba_attr_num_ports,	  /* bit13    RHBA_NUM_PORTS          */
	lpfc_fdmi_hba_attr_fabric_wwnn,	  /* bit14    RHBA_FABRIC_WWNN        */
	lpfc_fdmi_hba_attr_bios_ver,	  /* bit15    RHBA_BIOS_VERSION       */
	lpfc_fdmi_hba_attr_bios_state,	  /* bit16    RHBA_BIOS_STATE         */
	lpfc_fdmi_hba_attr_vendor_id,	  /* bit17    RHBA_VENDOR_ID          */
};

/* RPA / RPRT attribute jump table */
static int (*lpfc_fdmi_port_action[])
	(struct lpfc_vport *vport, void *attrbuf) = {
	/* Action routine                   Mask bit   Attribute type */
	lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
	lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
	lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
	lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
	lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
	lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
	lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
	lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
	lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
	lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
	lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
	lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
	lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
	lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
	lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
	lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
	lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
	lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
	lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
	lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
	lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
	lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
	lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
	lpfc_fdmi_vendor_attr_mi,           /* bit23  RPRT_VENDOR_MI          */
};

/**
 * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
 * @vport: pointer to a host virtual N_Port data structure.
 * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
 * @cmdcode: FDMI command to send
 * @new_mask: Mask of HBA or PORT Attributes to send
 *
 * Builds and sends a FDMI command using the CT subsystem.
 */
int
lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
	      int cmdcode, uint32_t new_mask)
{
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_dmabuf *rq, *rsp;
	struct lpfc_sli_ct_request *CtReq;
	struct ulp_bde64_le *bde;
	uint32_t bit_pos;
	uint32_t size, addsz;
	uint32_t rsp_size;
	uint32_t mask;
	struct lpfc_fdmi_reg_hba *rh;
	struct lpfc_fdmi_port_entry *pe;
	struct lpfc_fdmi_reg_portattr *pab = NULL, *base = NULL;
	struct lpfc_fdmi_attr_block *ab = NULL;
	int  (*func)(struct lpfc_vport *vport, void *attrbuf);
	void (*cmpl)(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		     struct lpfc_iocbq *rspiocb);

	if (!ndlp)
		return 0;

	cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
	rq = kmalloc(sizeof(*rq), GFP_KERNEL);
	if (!rq)
		goto fdmi_cmd_exit;

	rq->virt = lpfc_mbuf_alloc(phba, 0, &rq->phys);
	if (!rq->virt)
		goto fdmi_cmd_free_rq;

	/* Allocate buffer for Buffer ptr list */
	rsp = kmalloc(sizeof(*rsp), GFP_KERNEL);
	if (!rsp)
		goto fdmi_cmd_free_rqvirt;

	rsp->virt = lpfc_mbuf_alloc(phba, 0, &rsp->phys);
	if (!rsp->virt)
		goto fdmi_cmd_free_rsp;

	INIT_LIST_HEAD(&rq->list);
	INIT_LIST_HEAD(&rsp->list);

	/* mbuf buffers are 1K in length - aka LPFC_BPL_SIZE */
	memset(rq->virt, 0, LPFC_BPL_SIZE);
	rsp_size = LPFC_BPL_SIZE;

	/* FDMI request */
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0218 FDMI Request x%x mask x%x Data: x%x x%x x%x\n",
			 cmdcode, new_mask, vport->fdmi_port_mask,
			 vport->fc_flag, vport->port_state);

	CtReq = (struct lpfc_sli_ct_request *)rq->virt;

	/* First populate the CT_IU preamble */
	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
	CtReq->RevisionId.bits.InId = 0;

	CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
	CtReq->FsSubType = SLI_CT_FDMI_Subtypes;

	CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);

	size = 0;

	/* Next fill in the specific FDMI cmd information */
	switch (cmdcode) {
	case SLI_MGMT_RHAT:
	case SLI_MGMT_RHBA:
		rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un;
		/* HBA Identifier */
		memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
		       sizeof(struct lpfc_name));
		size += sizeof(struct lpfc_fdmi_hba_ident);

		if (cmdcode == SLI_MGMT_RHBA) {
			/* Registered Port List */
			/* One entry (port) per adapter */
			rh->rpl.EntryCnt = cpu_to_be32(1);
			memcpy(&rh->rpl.pe.PortName,
			       &phba->pport->fc_sparam.portName,
			       sizeof(struct lpfc_name));
			size += sizeof(struct lpfc_fdmi_reg_port_list);
		}

		ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
		ab->EntryCnt = 0;
		size += FOURBYTES;	/* add length of EntryCnt field */

		bit_pos = 0;
		if (new_mask)
			mask = new_mask;
		else
			mask = vport->fdmi_hba_mask;

		/* Mask will dictate what attributes to build in the request */
		while (mask) {
			if (mask & 0x1) {
				func = lpfc_fdmi_hba_action[bit_pos];
				addsz = func(vport, ((uint8_t *)rh + size));
				if (addsz) {
					ab->EntryCnt++;
					size += addsz;
				}
				/* check if another attribute fits */
				if ((size + FDMI_MAX_ATTRLEN) >
				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
					goto hba_out;
			}
			mask = mask >> 1;
			bit_pos++;
		}
hba_out:
		ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
		/* Total size */
		size += GID_REQUEST_SZ - 4;
		break;

	case SLI_MGMT_RPRT:
		if (vport->port_type != LPFC_PHYSICAL_PORT) {
			ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
			if (!ndlp)
				return 0;
		}
		fallthrough;
	case SLI_MGMT_RPA:
		/* Store base ptr right after preamble */
		base = (struct lpfc_fdmi_reg_portattr *)&CtReq->un;

		if (cmdcode == SLI_MGMT_RPRT) {
			rh = (struct lpfc_fdmi_reg_hba *)base;
			/* HBA Identifier */
			memcpy(&rh->hi.PortName,
			       &phba->pport->fc_sparam.portName,
			       sizeof(struct lpfc_name));
			pab = (struct lpfc_fdmi_reg_portattr *)
				((uint8_t *)base + sizeof(struct lpfc_name));
			size += sizeof(struct lpfc_name);
		} else {
			pab = base;
		}

		memcpy((uint8_t *)&pab->PortName,
		       (uint8_t *)&vport->fc_sparam.portName,
		       sizeof(struct lpfc_name));
		pab->ab.EntryCnt = 0;
		/* add length of name and EntryCnt field */
		size += sizeof(struct lpfc_name) + FOURBYTES;

		bit_pos = 0;
		if (new_mask)
			mask = new_mask;
		else
			mask = vport->fdmi_port_mask;

		/* Mask will dictate what attributes to build in the request */
		while (mask) {
			if (mask & 0x1) {
				func = lpfc_fdmi_port_action[bit_pos];
				addsz = func(vport, ((uint8_t *)base + size));
				if (addsz) {
					pab->ab.EntryCnt++;
					size += addsz;
				}
				/* check if another attribute fits */
				if ((size + FDMI_MAX_ATTRLEN) >
				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
					goto port_out;
			}
			mask = mask >> 1;
			bit_pos++;
		}
port_out:
		pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
		size += GID_REQUEST_SZ - 4;
		break;

	case SLI_MGMT_GHAT:
	case SLI_MGMT_GRPL:
		rsp_size = FC_MAX_NS_RSP;
		fallthrough;
	case SLI_MGMT_DHBA:
	case SLI_MGMT_DHAT:
		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un;
		memcpy((uint8_t *)&pe->PortName,
		       (uint8_t *)&vport->fc_sparam.portName,
		       sizeof(struct lpfc_name));
		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
		break;

	case SLI_MGMT_GPAT:
	case SLI_MGMT_GPAS:
		rsp_size = FC_MAX_NS_RSP;
		fallthrough;
	case SLI_MGMT_DPRT:
		if (vport->port_type != LPFC_PHYSICAL_PORT) {
			ndlp = lpfc_findnode_did(phba->pport, FDMI_DID);
			if (!ndlp)
				return 0;
		}
		fallthrough;
	case SLI_MGMT_DPA:
		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un;
		memcpy((uint8_t *)&pe->PortName,
		       (uint8_t *)&vport->fc_sparam.portName,
		       sizeof(struct lpfc_name));
		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
		break;
	case SLI_MGMT_GRHL:
		size = GID_REQUEST_SZ - 4;
		break;
	default:
		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
				 "0298 FDMI cmdcode x%x not supported\n",
				 cmdcode);
		goto fdmi_cmd_free_rspvirt;
	}
	CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);

	bde = (struct ulp_bde64_le *)rsp->virt;
	bde->addr_high = cpu_to_le32(putPaddrHigh(rq->phys));
	bde->addr_low = cpu_to_le32(putPaddrLow(rq->phys));
	bde->type_size = cpu_to_le32(ULP_BDE64_TYPE_BDE_64 <<
				     ULP_BDE64_TYPE_SHIFT);
	bde->type_size |= cpu_to_le32(size);

	/*
	 * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
	 * to hold ndlp reference for the corresponding callback function.
	 */
	if (!lpfc_ct_cmd(vport, rq, rsp, ndlp, cmpl, rsp_size, 0))
		return 0;

fdmi_cmd_free_rspvirt:
	lpfc_mbuf_free(phba, rsp->virt, rsp->phys);
fdmi_cmd_free_rsp:
	kfree(rsp);
fdmi_cmd_free_rqvirt:
	lpfc_mbuf_free(phba, rq->virt, rq->phys);
fdmi_cmd_free_rq:
	kfree(rq);
fdmi_cmd_exit:
	/* Issue FDMI request failed */
	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "0244 Issue FDMI request failed Data: x%x\n",
			 cmdcode);
	return 1;
}

/**
 * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
 * @t: Context object of the timer.
 *
 * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
 * the worker thread.
 **/
void
lpfc_delayed_disc_tmo(struct timer_list *t)
{
	struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
	struct lpfc_hba   *phba = vport->phba;
	uint32_t tmo_posted;
	unsigned long iflag;

	spin_lock_irqsave(&vport->work_port_lock, iflag);
	tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
	if (!tmo_posted)
		vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
	spin_unlock_irqrestore(&vport->work_port_lock, iflag);

	if (!tmo_posted)
		lpfc_worker_wake_up(phba);
	return;
}

/**
 * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
 *      handle delayed discovery.
 * @vport: pointer to a host virtual N_Port data structure.
 *
 * This function start nport discovery of the vport.
 **/
void
lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
{
	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);

	spin_lock_irq(shost->host_lock);
	if (!(vport->fc_flag & FC_DISC_DELAYED)) {
		spin_unlock_irq(shost->host_lock);
		return;
	}
	vport->fc_flag &= ~FC_DISC_DELAYED;
	spin_unlock_irq(shost->host_lock);

	lpfc_do_scr_ns_plogi(vport->phba, vport);
}

void
lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
{
	struct lpfc_sli *psli = &phba->sli;
	lpfc_vpd_t *vp = &phba->vpd;
	uint32_t b1, b2, b3, b4, i, rev;
	char c;
	uint32_t *ptr, str[4];
	uint8_t *fwname;

	if (phba->sli_rev == LPFC_SLI_REV4)
		snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
	else if (vp->rev.rBit) {
		if (psli->sli_flag & LPFC_SLI_ACTIVE)
			rev = vp->rev.sli2FwRev;
		else
			rev = vp->rev.sli1FwRev;

		b1 = (rev & 0x0000f000) >> 12;
		b2 = (rev & 0x00000f00) >> 8;
		b3 = (rev & 0x000000c0) >> 6;
		b4 = (rev & 0x00000030) >> 4;

		switch (b4) {
		case 0:
			c = 'N';
			break;
		case 1:
			c = 'A';
			break;
		case 2:
			c = 'B';
			break;
		case 3:
			c = 'X';
			break;
		default:
			c = 0;
			break;
		}
		b4 = (rev & 0x0000000f);

		if (psli->sli_flag & LPFC_SLI_ACTIVE)
			fwname = vp->rev.sli2FwName;
		else
			fwname = vp->rev.sli1FwName;

		for (i = 0; i < 16; i++)
			if (fwname[i] == 0x20)
				fwname[i] = 0;

		ptr = (uint32_t*)fwname;

		for (i = 0; i < 3; i++)
			str[i] = be32_to_cpu(*ptr++);

		if (c == 0) {
			if (flag)
				sprintf(fwrevision, "%d.%d%d (%s)",
					b1, b2, b3, (char *)str);
			else
				sprintf(fwrevision, "%d.%d%d", b1,
					b2, b3);
		} else {
			if (flag)
				sprintf(fwrevision, "%d.%d%d%c%d (%s)",
					b1, b2, b3, c,
					b4, (char *)str);
			else
				sprintf(fwrevision, "%d.%d%d%c%d",
					b1, b2, b3, c, b4);
		}
	} else {
		rev = vp->rev.smFwRev;

		b1 = (rev & 0xff000000) >> 24;
		b2 = (rev & 0x00f00000) >> 20;
		b3 = (rev & 0x000f0000) >> 16;
		c  = (rev & 0x0000ff00) >> 8;
		b4 = (rev & 0x000000ff);

		sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
	}
	return;
}

static void
lpfc_cmpl_ct_cmd_vmid(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		      struct lpfc_iocbq *rspiocb)
{
	struct lpfc_vport *vport = cmdiocb->vport;
	struct lpfc_dmabuf *inp = cmdiocb->cmd_dmabuf;
	struct lpfc_dmabuf *outp = cmdiocb->rsp_dmabuf;
	struct lpfc_sli_ct_request *ctcmd = inp->virt;
	struct lpfc_sli_ct_request *ctrsp = outp->virt;
	__be16 rsp = ctrsp->CommandResponse.bits.CmdRsp;
	struct app_id_object *app;
	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
	u32 cmd, hash, bucket;
	struct lpfc_vmid *vmp, *cur;
	u8 *data = outp->virt;
	int i;

	cmd = be16_to_cpu(ctcmd->CommandResponse.bits.CmdRsp);
	if (cmd == SLI_CTAS_DALLAPP_ID)
		lpfc_ct_free_iocb(phba, cmdiocb);

	if (lpfc_els_chk_latt(vport) || get_job_ulpstatus(phba, rspiocb)) {
		if (cmd != SLI_CTAS_DALLAPP_ID)
			goto free_res;
	}
	/* Check for a CT LS_RJT response */
	if (be16_to_cpu(rsp) == SLI_CT_RESPONSE_FS_RJT) {
		if (cmd != SLI_CTAS_DALLAPP_ID)
			lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
					 "3306 VMID FS_RJT Data: x%x x%x x%x\n",
					 cmd, ctrsp->ReasonCode,
					 ctrsp->Explanation);
		if ((cmd != SLI_CTAS_DALLAPP_ID) ||
		    (ctrsp->ReasonCode != SLI_CT_UNABLE_TO_PERFORM_REQ) ||
		    (ctrsp->Explanation != SLI_CT_APP_ID_NOT_AVAILABLE)) {
			/* If DALLAPP_ID failed retry later */
			if (cmd == SLI_CTAS_DALLAPP_ID)
				vport->load_flag |= FC_DEREGISTER_ALL_APP_ID;
			goto free_res;
		}
	}

	switch (cmd) {
	case SLI_CTAS_RAPP_IDENT:
		app = (struct app_id_object *)(RAPP_IDENT_OFFSET + data);
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "6712 RAPP_IDENT app id %d  port id x%x id "
				 "len %d\n", be32_to_cpu(app->app_id),
				 be32_to_cpu(app->port_id),
				 app->obj.entity_id_len);

		if (app->obj.entity_id_len == 0 || app->port_id == 0)
			goto free_res;

		hash = lpfc_vmid_hash_fn(app->obj.entity_id,
					 app->obj.entity_id_len);
		vmp = lpfc_get_vmid_from_hashtable(vport, hash,
						  app->obj.entity_id);
		if (vmp) {
			write_lock(&vport->vmid_lock);
			vmp->un.app_id = be32_to_cpu(app->app_id);
			vmp->flag |= LPFC_VMID_REGISTERED;
			vmp->flag &= ~LPFC_VMID_REQ_REGISTER;
			write_unlock(&vport->vmid_lock);
			/* Set IN USE flag */
			vport->vmid_flag |= LPFC_VMID_IN_USE;
		} else {
			lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
					 "6901 No entry found %s hash %d\n",
					 app->obj.entity_id, hash);
		}
		break;
	case SLI_CTAS_DAPP_IDENT:
		app = (struct app_id_object *)(DAPP_IDENT_OFFSET + data);
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "6713 DAPP_IDENT app id %d  port id x%x\n",
				 be32_to_cpu(app->app_id),
				 be32_to_cpu(app->port_id));
		break;
	case SLI_CTAS_DALLAPP_ID:
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "8856 Deregistered all app ids\n");
		read_lock(&vport->vmid_lock);
		for (i = 0; i < phba->cfg_max_vmid; i++) {
			vmp = &vport->vmid[i];
			if (vmp->flag != LPFC_VMID_SLOT_FREE)
				memset(vmp, 0, sizeof(struct lpfc_vmid));
		}
		read_unlock(&vport->vmid_lock);
		/* for all elements in the hash table */
		if (!hash_empty(vport->hash_table))
			hash_for_each(vport->hash_table, bucket, cur, hnode)
				hash_del(&cur->hnode);
		vport->load_flag |= FC_ALLOW_VMID;
		break;
	default:
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "8857 Invalid command code\n");
	}
free_res:
	lpfc_ct_free_iocb(phba, cmdiocb);
	lpfc_nlp_put(ndlp);
}

/**
 * lpfc_vmid_cmd - Build and send a FDMI cmd to the specified NPort
 * @vport: pointer to a host virtual N_Port data structure.
 * @cmdcode: application server command code to send
 * @vmid: pointer to vmid info structure
 *
 * Builds and sends a FDMI command using the CT subsystem.
 */
int
lpfc_vmid_cmd(struct lpfc_vport *vport,
	      int cmdcode, struct lpfc_vmid *vmid)
{
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_dmabuf *mp, *bmp;
	struct lpfc_sli_ct_request *ctreq;
	struct ulp_bde64 *bpl;
	u32 size;
	u32 rsp_size;
	u8 *data;
	struct lpfc_vmid_rapp_ident_list *rap;
	struct lpfc_vmid_dapp_ident_list *dap;
	u8 retry = 0;
	struct lpfc_nodelist *ndlp;

	void (*cmpl)(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		     struct lpfc_iocbq *rspiocb);

	ndlp = lpfc_findnode_did(vport, FDMI_DID);
	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
		return 0;

	cmpl = lpfc_cmpl_ct_cmd_vmid;

	/* fill in BDEs for command */
	/* Allocate buffer for command payload */
	mp = kmalloc(sizeof(*mp), GFP_KERNEL);
	if (!mp)
		goto vmid_free_mp_exit;

	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
	if (!mp->virt)
		goto vmid_free_mp_virt_exit;

	/* Allocate buffer for Buffer ptr list */
	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
	if (!bmp)
		goto vmid_free_bmp_exit;

	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
	if (!bmp->virt)
		goto vmid_free_bmp_virt_exit;

	INIT_LIST_HEAD(&mp->list);
	INIT_LIST_HEAD(&bmp->list);

	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
			 "3275 VMID Request Data: x%x x%x x%x\n",
			 vport->fc_flag, vport->port_state, cmdcode);
	ctreq = (struct lpfc_sli_ct_request *)mp->virt;
	data = mp->virt;
	/* First populate the CT_IU preamble */
	memset(data, 0, LPFC_BPL_SIZE);
	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
	ctreq->RevisionId.bits.InId = 0;

	ctreq->FsType = SLI_CT_MANAGEMENT_SERVICE;
	ctreq->FsSubType = SLI_CT_APP_SEV_Subtypes;

	ctreq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
	rsp_size = LPFC_BPL_SIZE;
	size = 0;

	switch (cmdcode) {
	case SLI_CTAS_RAPP_IDENT:
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "1329 RAPP_IDENT for %s\n", vmid->host_vmid);
		ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
		rap = (struct lpfc_vmid_rapp_ident_list *)
			(DAPP_IDENT_OFFSET + data);
		rap->no_of_objects = cpu_to_be32(1);
		rap->obj[0].entity_id_len = vmid->vmid_len;
		memcpy(rap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
		size = RAPP_IDENT_OFFSET +
		       struct_size(rap, obj, be32_to_cpu(rap->no_of_objects));
		retry = 1;
		break;

	case SLI_CTAS_GALLAPPIA_ID:
		ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
		size = GALLAPPIA_ID_SIZE;
		break;

	case SLI_CTAS_DAPP_IDENT:
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "1469 DAPP_IDENT for %s\n", vmid->host_vmid);
		ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
		dap = (struct lpfc_vmid_dapp_ident_list *)
			(DAPP_IDENT_OFFSET + data);
		dap->no_of_objects = cpu_to_be32(1);
		dap->obj[0].entity_id_len = vmid->vmid_len;
		memcpy(dap->obj[0].entity_id, vmid->host_vmid, vmid->vmid_len);
		size = DAPP_IDENT_OFFSET +
		       struct_size(dap, obj, be32_to_cpu(dap->no_of_objects));
		write_lock(&vport->vmid_lock);
		vmid->flag &= ~LPFC_VMID_REGISTERED;
		write_unlock(&vport->vmid_lock);
		retry = 1;
		break;

	case SLI_CTAS_DALLAPP_ID:
		ctreq->un.PortID = cpu_to_be32(vport->fc_myDID);
		size = DALLAPP_ID_SIZE;
		break;

	default:
		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
				 "7062 VMID cmdcode x%x not supported\n",
				 cmdcode);
		goto vmid_free_all_mem;
	}

	ctreq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);

	bpl = (struct ulp_bde64 *)bmp->virt;
	bpl->addrHigh = putPaddrHigh(mp->phys);
	bpl->addrLow = putPaddrLow(mp->phys);
	bpl->tus.f.bdeFlags = 0;
	bpl->tus.f.bdeSize = size;

	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
	 * to hold ndlp reference for the corresponding callback function.
	 */
	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry))
		return 0;

 vmid_free_all_mem:
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
 vmid_free_bmp_virt_exit:
	kfree(bmp);
 vmid_free_bmp_exit:
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
 vmid_free_mp_virt_exit:
	kfree(mp);
 vmid_free_mp_exit:

	/* Issue CT request failed */
	lpfc_printf_vlog(vport, KERN_DEBUG, LOG_DISCOVERY,
			 "3276 VMID CT request failed Data: x%x\n", cmdcode);
	return -EIO;
}