summaryrefslogtreecommitdiff
path: root/drivers/scsi/isci/core/scic_sds_phy.c
blob: 7d012b571b5b8f0cca1be0bc95e0662bf5ab8364 (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
/*
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 *
 * 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, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * BSD LICENSE
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "intel_ata.h"
#include "intel_sata.h"
#include "sci_base_state.h"
#include "sci_base_state_machine.h"
#include "scic_phy.h"
#include "scic_sds_controller.h"
#include "scic_sds_phy.h"
#include "scic_sds_phy_registers.h"
#include "scic_sds_port.h"
#include "scic_user_callback.h"
#include "sci_environment.h"
#include "sci_util.h"
#include "scu_event_codes.h"

#define SCIC_SDS_PHY_MIN_TIMER_COUNT  (SCI_MAX_PHYS)
#define SCIC_SDS_PHY_MAX_TIMER_COUNT  (SCI_MAX_PHYS)

/* Maximum arbitration wait time in micro-seconds */
#define SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME  (700)

/*
 * *****************************************************************************
 * * SCIC SDS PHY Internal Methods
 * ***************************************************************************** */

/**
 * This method will initialize the phy link layer registers
 * @this_phy:
 * @link_layer_registers:
 *
 * enum sci_status
 */
static enum sci_status scic_sds_phy_link_layer_initialization(
	struct scic_sds_phy *this_phy,
	struct scu_link_layer_registers *link_layer_registers)
{
	u32 phy_configuration;
	struct sas_capabilities phy_capabilities;
	u32 parity_check = 0;
	u32 parity_count = 0;
	u32 link_layer_control;

	this_phy->link_layer_registers = link_layer_registers;

	/* Set our IDENTIFY frame data */
   #define SCI_END_DEVICE 0x01

	SCU_SAS_TIID_WRITE(
		this_phy,
		(SCU_SAS_TIID_GEN_BIT(SMP_INITIATOR)
		 | SCU_SAS_TIID_GEN_BIT(SSP_INITIATOR)
		 | SCU_SAS_TIID_GEN_BIT(STP_INITIATOR)
		 | SCU_SAS_TIID_GEN_BIT(DA_SATA_HOST)
		 | SCU_SAS_TIID_GEN_VAL(DEVICE_TYPE, SCI_END_DEVICE))
		);

	/* Write the device SAS Address */
	SCU_SAS_TIDNH_WRITE(this_phy, 0xFEDCBA98);
	SCU_SAS_TIDNL_WRITE(this_phy, this_phy->phy_index);

	/* Write the source SAS Address */
	SCU_SAS_TISSAH_WRITE(
		this_phy,
		this_phy->owning_port->owning_controller->oem_parameters.sds1.phys[
			this_phy->phy_index].sas_address.high
		);
	SCU_SAS_TISSAL_WRITE(
		this_phy,
		this_phy->owning_port->owning_controller->oem_parameters.sds1.phys[
			this_phy->phy_index].sas_address.low
		);

	/* Clear and Set the PHY Identifier */
	SCU_SAS_TIPID_WRITE(this_phy, 0x00000000);
	SCU_SAS_TIPID_WRITE(this_phy, SCU_SAS_TIPID_GEN_VALUE(ID, this_phy->phy_index));

	/* Change the initial state of the phy configuration register */
	phy_configuration = SCU_SAS_PCFG_READ(this_phy);

	/* Hold OOB state machine in reset */
	phy_configuration |=  SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	SCU_SAS_PCFG_WRITE(this_phy, phy_configuration);

	/* Configure the SNW capabilities */
	phy_capabilities.u.all = 0;
	phy_capabilities.u.bits.start                      = 1;
	phy_capabilities.u.bits.gen3_without_ssc_supported = 1;
	phy_capabilities.u.bits.gen2_without_ssc_supported = 1;
	phy_capabilities.u.bits.gen1_without_ssc_supported = 1;
	if (this_phy->owning_port->owning_controller->oem_parameters.sds1.
	    controller.do_enable_ssc == true) {
		phy_capabilities.u.bits.gen3_with_ssc_supported = 1;
		phy_capabilities.u.bits.gen2_with_ssc_supported = 1;
		phy_capabilities.u.bits.gen1_with_ssc_supported = 1;
	}

	/*
	 * The SAS specification indicates that the phy_capabilities that
	 * are transmitted shall have an even parity.  Calculate the parity. */
	parity_check = phy_capabilities.u.all;
	while (parity_check != 0) {
		if (parity_check & 0x1)
			parity_count++;
		parity_check >>= 1;
	}

	/*
	 * If parity indicates there are an odd number of bits set, then
	 * set the parity bit to 1 in the phy capabilities. */
	if ((parity_count % 2) != 0)
		phy_capabilities.u.bits.parity = 1;

	SCU_SAS_PHYCAP_WRITE(this_phy, phy_capabilities.u.all);

	/* Set the enable spinup period but disable the ability to send notify enable spinup */
	SCU_SAS_ENSPINUP_WRITE(this_phy, SCU_ENSPINUP_GEN_VAL(COUNT, 0x33));

#if defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2) || defined(CONFIG_PBG_HBA_BETA)
	/* / @todo Provide a way to write this register correctly */
	scu_link_layer_register_write(this_phy, afe_lookup_table_control, 0x02108421);
#else
	/* / @todo Provide a way to write this register correctly */
	scu_link_layer_register_write(this_phy, afe_lookup_table_control, 0x0e739ce7);
#endif

	link_layer_control = SCU_SAS_LLCTL_GEN_VAL(
		NO_OUTBOUND_TASK_TIMEOUT,
		(u8)this_phy->owning_port->owning_controller->
		user_parameters.sds1.no_outbound_task_timeout
		);

/* #define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1 */
/* #define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2 */
#define COMPILED_MAX_LINK_RATE SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN3

	if (this_phy->owning_port->owning_controller->user_parameters.sds1.
	    phys[this_phy->phy_index].max_speed_generation == SCIC_SDS_PARM_GEN3_SPEED) {
		link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
			MAX_LINK_RATE, COMPILED_MAX_LINK_RATE
			);
	} else if (this_phy->owning_port->owning_controller->user_parameters.sds1.
		   phys[this_phy->phy_index].max_speed_generation == SCIC_SDS_PARM_GEN2_SPEED) {
		link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
			MAX_LINK_RATE,
			min(
				SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN2,
				COMPILED_MAX_LINK_RATE)
			);
	} else {
		link_layer_control |= SCU_SAS_LLCTL_GEN_VAL(
			MAX_LINK_RATE,
			min(
				SCU_SAS_LINK_LAYER_CONTROL_MAX_LINK_RATE_GEN1,
				COMPILED_MAX_LINK_RATE)
			);
	}

	scu_link_layer_register_write(
		this_phy, link_layer_control, link_layer_control
		);

	/*
	 * Program the max ARB time for the PHY to 700us so we inter-operate with
	 * the PMC expander which shuts down PHYs if the expander PHY generates too
	 * many breaks.  This time value will guarantee that the initiator PHY will
	 * generate the break. */
#if defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2)
	scu_link_layer_register_write(
		this_phy,
		maximum_arbitration_wait_timer_timeout,
		SCIC_SDS_PHY_MAX_ARBITRATION_WAIT_TIME
		);
#endif  /* defined(CONFIG_PBG_HBA_A0) || defined(CONFIG_PBG_HBA_A2) */

	/*
	 * Set the link layer hang detection to 500ms (0x1F4) from its default
	 * value of 128ms.  Max value is 511 ms. */
	scu_link_layer_register_write(
		this_phy, link_layer_hang_detection_timeout, 0x1F4
		);

	/* We can exit the initial state to the stopped state */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_STOPPED
		);

	return SCI_SUCCESS;
}

/**
 * This function will handle the sata SIGNATURE FIS timeout condition.  It will
 * restart the starting substate machine since we dont know what has actually
 * happening.
 */
static void scic_sds_phy_sata_timeout(void *phy)
{
	struct scic_sds_phy *sci_phy = phy;

	dev_dbg(sciphy_to_dev(sci_phy),
		 "%s: SCIC SDS Phy 0x%p did not receive signature fis before "
		 "timeout.\n",
		 __func__,
		 sci_phy);

	sci_base_state_machine_stop(
		scic_sds_phy_get_starting_substate_machine(sci_phy));

	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(sci_phy),
		SCI_BASE_PHY_STATE_STARTING
		);
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY External Methods
 * ***************************************************************************** */

/**
 * This method returns the object size for a phy object.
 *
 * u32
 */

/**
 * This method returns the minimum number of timers required for a phy object.
 *
 * u32
 */

/**
 * This method returns the maximum number of timers required for a phy object.
 *
 * u32
 */

#ifdef SCIC_DEBUG_ENABLED
/**
 * scic_sds_phy_observe_state_change() -
 * @our_observer:
 *
 * Debug code to record the state transitions in the phy
 */
void scic_sds_phy_observe_state_change(
	struct sci_base_observer *our_observer,
	struct sci_base_subject *the_subject)
{
	struct scic_sds_phy *this_phy;
	struct sci_base_state_machine *the_state_machine;

	u8 transition_requestor;
	u32 base_state_id;
	u32 starting_substate_id;

	the_state_machine = (struct sci_base_state_machine *)the_subject;
	this_phy = (struct scic_sds_phy *)the_state_machine->state_machine_owner;

	if (the_state_machine == &this_phy->parent.state_machine) {
		transition_requestor = 0x01;
	} else if (the_state_machine == &this_phy->starting_substate_machine) {
		transition_requestor = 0x02;
	} else {
		transition_requestor = 0xFF;
	}

	base_state_id =
		sci_base_state_machine_get_state(&this_phy->parent.state_machine);
	starting_substate_id =
		sci_base_state_machine_get_state(&this_phy->starting_substate_machine);

	this_phy->state_record.state_transition_table[
		this_phy->state_record.index++] = ((transition_requestor << 24)
						   | ((u8)base_state_id << 8)
						   | ((u8)starting_substate_id));

	this_phy->state_record.index =
		this_phy->state_record.index & (MAX_STATE_TRANSITION_RECORD - 1);

}
#endif /* SCIC_DEBUG_ENABLED */

#ifdef SCIC_DEBUG_ENABLED
/**
 * scic_sds_phy_initialize_state_recording() -
 *
 * This method initializes the state record debug information for the phy
 * object. The state machines for the phy object must be constructed before
 * this function is called.
 */
void scic_sds_phy_initialize_state_recording(
	struct scic_sds_phy *this_phy)
{
	this_phy->state_record.index = 0;

	sci_base_observer_initialize(
		&this_phy->state_record.base_state_observer,
		scic_sds_phy_observe_state_change,
		&this_phy->parent.state_machine.parent
		);

	sci_base_observer_initialize(
		&this_phy->state_record.starting_state_observer,
		scic_sds_phy_observe_state_change,
		&this_phy->starting_substate_machine.parent
		);
}
#endif /* SCIC_DEBUG_ENABLED */

/**
 * This method will construct the struct scic_sds_phy object
 * @this_phy:
 * @owning_port:
 * @phy_index:
 *
 */
void scic_sds_phy_construct(
	struct scic_sds_phy *this_phy,
	struct scic_sds_port *owning_port,
	u8 phy_index)
{
	/*
	 * Call the base constructor first
	 */
	sci_base_phy_construct(
		&this_phy->parent,
		scic_sds_phy_state_table
		);

	/* Copy the rest of the input data to our locals */
	this_phy->owning_port = owning_port;
	this_phy->phy_index = phy_index;
	this_phy->bcn_received_while_port_unassigned = false;
	this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	this_phy->link_layer_registers = NULL;
	this_phy->max_negotiated_speed = SCI_SAS_NO_LINK_RATE;

	/* Clear out the identification buffer data */
	memset(&this_phy->phy_type, 0, sizeof(this_phy->phy_type));

	/* Initialize the the substate machines */
	sci_base_state_machine_construct(
		&this_phy->starting_substate_machine,
		&this_phy->parent.parent,
		scic_sds_phy_starting_substates,
		SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL
		);

   #ifdef SCIC_DEBUG_ENABLED
	scic_sds_phy_initialize_state_recording(this_phy);
   #endif /* SCIC_DEBUG_ENABLED */
}

/**
 * This method returns the port currently containing this phy. If the phy is
 *    currently contained by the dummy port, then the phy is considered to not
 *    be part of a port.
 * @this_phy: This parameter specifies the phy for which to retrieve the
 *    containing port.
 *
 * This method returns a handle to a port that contains the supplied phy.
 * SCI_INVALID_HANDLE This value is returned if the phy is not part of a real
 * port (i.e. it's contained in the dummy port). !SCI_INVALID_HANDLE All other
 * values indicate a handle/pointer to the port containing the phy.
 */
struct scic_sds_port *scic_sds_phy_get_port(
	struct scic_sds_phy *this_phy)
{
	if (scic_sds_port_get_index(this_phy->owning_port) == SCIC_SDS_DUMMY_PORT)
		return SCI_INVALID_HANDLE;

	return this_phy->owning_port;
}

/**
 * This method will assign a port to the phy object.
 * @out]: this_phy This parameter specifies the phy for which to assign a port
 *    object.
 *
 *
 */
void scic_sds_phy_set_port(
	struct scic_sds_phy *this_phy,
	struct scic_sds_port *the_port)
{
	this_phy->owning_port = the_port;

	if (this_phy->bcn_received_while_port_unassigned) {
		this_phy->bcn_received_while_port_unassigned = false;
		scic_sds_port_broadcast_change_received(this_phy->owning_port, this_phy);
	}
}

/**
 * This method will initialize the constructed phy
 * @sci_phy:
 * @link_layer_registers:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_initialize(
	struct scic_sds_phy *sci_phy,
	struct scu_link_layer_registers *link_layer_registers)
{
	/* Create the SIGNATURE FIS Timeout timer for this phy */
	sci_phy->sata_timeout_timer = scic_cb_timer_create(
		scic_sds_phy_get_controller(sci_phy),
		scic_sds_phy_sata_timeout,
		sci_phy
		);

	/* Perofrm the initialization of the PE hardware */
	scic_sds_phy_link_layer_initialization(sci_phy, link_layer_registers);

	/*
	 * There is nothing that needs to be done in this state just
	 * transition to the stopped state. */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(sci_phy),
		SCI_BASE_PHY_STATE_STOPPED
		);

	return SCI_SUCCESS;
}


/**
 *
 * @this_phy: The phy object to be suspended.
 *
 * This function will perform the register reads/writes to suspend the SCU
 * hardware protocol engine. none
 */
void scic_sds_phy_suspend(
	struct scic_sds_phy *this_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);

	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);

	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
}

/**
 *
 * @this_phy: The phy object to resume.
 *
 * This function will perform the register reads/writes required to resume the
 * SCU hardware protocol engine. none
 */
void scic_sds_phy_resume(
	struct scic_sds_phy *this_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);

	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE);

	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
}

/**
 * This method returns the local sas address assigned to this phy.
 * @this_phy: This parameter specifies the phy for which to retrieve the local
 *    SAS address.
 * @sas_address: This parameter specifies the location into which to copy the
 *    local SAS address.
 *
 */
void scic_sds_phy_get_sas_address(
	struct scic_sds_phy *this_phy,
	struct sci_sas_address *sas_address)
{
	sas_address->high = SCU_SAS_TISSAH_READ(this_phy);
	sas_address->low  = SCU_SAS_TISSAL_READ(this_phy);
}

/**
 * This method returns the remote end-point (i.e. attached) sas address
 *    assigned to this phy.
 * @this_phy: This parameter specifies the phy for which to retrieve the remote
 *    end-point SAS address.
 * @sas_address: This parameter specifies the location into which to copy the
 *    remote end-point SAS address.
 *
 */
void scic_sds_phy_get_attached_sas_address(
	struct scic_sds_phy *this_phy,
	struct sci_sas_address *sas_address)
{
	sas_address->high
		= this_phy->phy_type.sas.identify_address_frame_buffer.sas_address.high;
	sas_address->low
		= this_phy->phy_type.sas.identify_address_frame_buffer.sas_address.low;
}

/**
 * This method returns the supported protocols assigned to this phy
 * @this_phy:
 *
 *
 */
void scic_sds_phy_get_protocols(
	struct scic_sds_phy *this_phy,
	struct sci_sas_identify_address_frame_protocols *protocols)
{
	protocols->u.all = (u16)(SCU_SAS_TIID_READ(this_phy) & 0x0000FFFF);
}

/**
 *
 * @this_phy: The parameter is the phy object for which the attached phy
 *    protcols are to be returned.
 *
 * This method returns the supported protocols for the attached phy.  If this
 * is a SAS phy the protocols are returned from the identify address frame. If
 * this is a SATA phy then protocols are made up and the target phy is an STP
 * target phy. The caller will get the entire set of bits for the protocol
 * value.
 */
void scic_sds_phy_get_attached_phy_protocols(
	struct scic_sds_phy *this_phy,
	struct sci_sas_identify_address_frame_protocols *protocols)
{
	protocols->u.all = 0;

	if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		protocols->u.all =
			this_phy->phy_type.sas.identify_address_frame_buffer.protocols.u.all;
	} else if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
		protocols->u.bits.stp_target = 1;
	}
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY Handler Redirects
 * ***************************************************************************** */

/**
 * This method will attempt to start the phy object. This request is only valid
 *    when the phy is in the stopped state
 * @this_phy:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_start(
	struct scic_sds_phy *this_phy)
{
	return this_phy->state_handlers->parent.start_handler(&this_phy->parent);
}

/**
 * This method will attempt to stop the phy object.
 * @this_phy:
 *
 * enum sci_status SCI_SUCCESS if the phy is going to stop SCI_INVALID_STATE if the
 * phy is not in a valid state to stop
 */
enum sci_status scic_sds_phy_stop(
	struct scic_sds_phy *this_phy)
{
	return this_phy->state_handlers->parent.stop_handler(&this_phy->parent);
}

/**
 * This method will attempt to reset the phy.  This request is only valid when
 *    the phy is in an ready state
 * @this_phy:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_reset(
	struct scic_sds_phy *this_phy)
{
	return this_phy->state_handlers->parent.reset_handler(
		       &this_phy->parent
		       );
}

/**
 * This method will process the event code received.
 * @this_phy:
 * @event_code:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	return this_phy->state_handlers->event_handler(this_phy, event_code);
}

/**
 * This method will process the frame index received.
 * @this_phy:
 * @frame_index:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_frame_handler(
	struct scic_sds_phy *this_phy,
	u32 frame_index)
{
	return this_phy->state_handlers->frame_handler(this_phy, frame_index);
}

/**
 * This method will give the phy permission to consume power
 * @this_phy:
 *
 * enum sci_status
 */
enum sci_status scic_sds_phy_consume_power_handler(
	struct scic_sds_phy *this_phy)
{
	return this_phy->state_handlers->consume_power_handler(this_phy);
}

/*
 * *****************************************************************************
 * * SCIC PHY Public Methods
 * ***************************************************************************** */


enum sci_status scic_sas_phy_get_properties(
	struct scic_sds_phy *sci_phy,
	struct scic_sas_phy_properties *properties)
{
	if (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		memcpy(
			&properties->received_iaf,
			&sci_phy->phy_type.sas.identify_address_frame_buffer,
			sizeof(struct sci_sas_identify_address_frame)
			);

		properties->received_capabilities.u.all
			= SCU_SAS_RECPHYCAP_READ(sci_phy);

		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}


enum sci_status scic_sata_phy_get_properties(
	struct scic_sds_phy *sci_phy,
	struct scic_sata_phy_properties *properties)
{
	if (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
		memcpy(
			&properties->signature_fis,
			&sci_phy->phy_type.sata.signature_fis_buffer,
			sizeof(struct sata_fis_reg_d2h)
			);

		/* / @todo add support for port selectors. */
		properties->is_port_selector_present = false;

		return SCI_SUCCESS;
	}

	return SCI_FAILURE;
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY HELPER FUNCTIONS
 * ***************************************************************************** */


/**
 *
 * @this_phy: The phy object that received SAS PHY DETECTED.
 *
 * This method continues the link training for the phy as if it were a SAS PHY
 * instead of a SATA PHY. This is done because the completion queue had a SAS
 * PHY DETECTED event when the state machine was expecting a SATA PHY event.
 * none
 */
static void scic_sds_phy_start_sas_link_training(
	struct scic_sds_phy *this_phy)
{
	u32 phy_control;

	phy_control = SCU_SAS_PCFG_READ(this_phy);
	phy_control |= SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD);
	SCU_SAS_PCFG_WRITE(this_phy, phy_control);

	sci_base_state_machine_change_state(
		&this_phy->starting_substate_machine,
		SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN
		);

	this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SAS;
}

/**
 *
 * @this_phy: The phy object that received a SATA SPINUP HOLD event
 *
 * This method continues the link training for the phy as if it were a SATA PHY
 * instead of a SAS PHY.  This is done because the completion queue had a SATA
 * SPINUP HOLD event when the state machine was expecting a SAS PHY event. none
 */
static void scic_sds_phy_start_sata_link_training(
	struct scic_sds_phy *this_phy)
{
	sci_base_state_machine_change_state(
		&this_phy->starting_substate_machine,
		SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER
		);

	this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;
}

/**
 * This method performs processing common to all protocols upon completion of
 *    link training.
 * @this_phy: This parameter specifies the phy object for which link training
 *    has completed.
 * @max_link_rate: This parameter specifies the maximum link rate to be
 *    associated with this phy.
 * @next_state: This parameter specifies the next state for the phy's starting
 *    sub-state machine.
 *
 */
static void scic_sds_phy_complete_link_training(
	struct scic_sds_phy *this_phy,
	enum sci_sas_link_rate max_link_rate,
	u32 next_state)
{
	this_phy->max_negotiated_speed = max_link_rate;

	sci_base_state_machine_change_state(
		scic_sds_phy_get_starting_substate_machine(this_phy), next_state
		);
}

/**
 *
 * @this_phy: The struct scic_sds_phy object to restart.
 *
 * This method restarts the struct scic_sds_phy objects base state machine in the
 * starting state from any starting substate. none
 */
static void scic_sds_phy_restart_starting_state(
	struct scic_sds_phy *this_phy)
{
	/* Stop the current substate machine */
	sci_base_state_machine_stop(
		scic_sds_phy_get_starting_substate_machine(this_phy)
		);

	/* Re-enter the base state machine starting state */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_STARTING
		);
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY EVENT_HANDLERS
 * ***************************************************************************** */

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. -
 * decode the event - sas phy detected causes a state transition to the wait
 * for speed event notification. - any other events log a warning message and
 * set a failure status enum sci_status SCI_SUCCESS on any valid event notification
 * SCI_FAILURE on any unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_ossp_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_SAS_PHY_DETECTED:
		scic_sds_phy_start_sas_link_training(this_phy);
		this_phy->is_in_link_training = true;
		break;

	case SCU_EVENT_SATA_SPINUP_HOLD:
		scic_sds_phy_start_sata_link_training(this_phy);
		this_phy->is_in_link_training = true;
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. -
 * decode the event - sas phy detected returns us back to this state. - speed
 * event detected causes a state transition to the wait for iaf. - identify
 * timeout is an un-expected event and the state machine is restarted. - link
 * failure events restart the starting state machine - any other events log a
 * warning message and set a failure status enum sci_status SCI_SUCCESS on any valid
 * event notification SCI_FAILURE on any unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sas_phy_speed_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_SAS_PHY_DETECTED:
		/*
		 * Why is this being reported again by the controller?
		 * We would re-enter this state so just stay here */
		break;

	case SCU_EVENT_SAS_15:
	case SCU_EVENT_SAS_15_SSC:
		scic_sds_phy_complete_link_training(
			this_phy, SCI_SAS_150_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
			);
		break;

	case SCU_EVENT_SAS_30:
	case SCU_EVENT_SAS_30_SSC:
		scic_sds_phy_complete_link_training(
			this_phy, SCI_SAS_300_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
			);
		break;

	case SCU_EVENT_SAS_60:
	case SCU_EVENT_SAS_60_SSC:
		scic_sds_phy_complete_link_training(
			this_phy, SCI_SAS_600_GB, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
			);
		break;

	case SCU_EVENT_SATA_SPINUP_HOLD:
		/*
		 * We were doing SAS PHY link training and received a SATA PHY event
		 * continue OOB/SN as if this were a SATA PHY */
		scic_sds_phy_start_sata_link_training(this_phy);
		break;

	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. -
 * decode the event - sas phy detected event backs up the state machine to the
 * await speed notification. - identify timeout is an un-expected event and the
 * state machine is restarted. - link failure events restart the starting state
 * machine - any other events log a warning message and set a failure status
 * enum sci_status SCI_SUCCESS on any valid event notification SCI_FAILURE on any
 * unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_iaf_uf_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_SAS_PHY_DETECTED:
		/* Backup the state machine */
		scic_sds_phy_start_sas_link_training(this_phy);
		break;

	case SCU_EVENT_SATA_SPINUP_HOLD:
		/*
		 * We were doing SAS PHY link training and received a SATA PHY event
		 * continue OOB/SN as if this were a SATA PHY */
		scic_sds_phy_start_sata_link_training(this_phy);
		break;

	case SCU_EVENT_RECEIVED_IDENTIFY_TIMEOUT:
	case SCU_EVENT_LINK_FAILURE:
	case SCU_EVENT_HARD_RESET_RECEIVED:
		/* Start the oob/sn state machine over again */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_POWER. -
 * decode the event - link failure events restart the starting state machine -
 * any other events log a warning message and set a failure status enum sci_status
 * SCI_SUCCESS on a link failure event SCI_FAILURE on any unexpected event
 * notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sas_power_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			"%s: PHY starting substate machine received unexpected "
			"event_code %x\n",
			__func__,
			event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. -
 * decode the event - link failure events restart the starting state machine -
 * sata spinup hold events are ignored since they are expected - any other
 * events log a warning message and set a failure status enum sci_status SCI_SUCCESS
 * on a link failure event SCI_FAILURE on any unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sata_power_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	case SCU_EVENT_SATA_SPINUP_HOLD:
		/* These events are received every 10ms and are expected while in this state */
		break;

	case SCU_EVENT_SAS_PHY_DETECTED:
		/*
		 * There has been a change in the phy type before OOB/SN for the
		 * SATA finished start down the SAS link traning path. */
		scic_sds_phy_start_sas_link_training(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. -
 * decode the event - link failure events restart the starting state machine -
 * sata spinup hold events are ignored since they are expected - sata phy
 * detected event change to the wait speed event - any other events log a
 * warning message and set a failure status enum sci_status SCI_SUCCESS on a link
 * failure event SCI_FAILURE on any unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sata_phy_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	case SCU_EVENT_SATA_SPINUP_HOLD:
		/*
		 * These events might be received since we dont know how many may be in
		 * the completion queue while waiting for power */
		break;

	case SCU_EVENT_SATA_PHY_DETECTED:
		this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_SATA;

		/* We have received the SATA PHY notification change state */
		sci_base_state_machine_change_state(
			scic_sds_phy_get_starting_substate_machine(this_phy),
			SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
			);
		break;

	case SCU_EVENT_SAS_PHY_DETECTED:
		/*
		 * There has been a change in the phy type before OOB/SN for the
		 * SATA finished start down the SAS link traning path. */
		scic_sds_phy_start_sas_link_training(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN.
 * - decode the event - sata phy detected returns us back to this state. -
 * speed event detected causes a state transition to the wait for signature. -
 * link failure events restart the starting state machine - any other events
 * log a warning message and set a failure status enum sci_status SCI_SUCCESS on any
 * valid event notification SCI_FAILURE on any unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sata_speed_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_SATA_PHY_DETECTED:
		/*
		 * The hardware reports multiple SATA PHY detected events
		 * ignore the extras */
		break;

	case SCU_EVENT_SATA_15:
	case SCU_EVENT_SATA_15_SSC:
		scic_sds_phy_complete_link_training(
			this_phy,
			SCI_SAS_150_GB,
			SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
			);
		break;

	case SCU_EVENT_SATA_30:
	case SCU_EVENT_SATA_30_SSC:
		scic_sds_phy_complete_link_training(
			this_phy,
			SCI_SAS_300_GB,
			SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
			);
		break;

	case SCU_EVENT_SATA_60:
	case SCU_EVENT_SATA_60_SSC:
		scic_sds_phy_complete_link_training(
			this_phy,
			SCI_SAS_600_GB,
			SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
			);
		break;

	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	case SCU_EVENT_SAS_PHY_DETECTED:
		/*
		 * There has been a change in the phy type before OOB/SN for the
		 * SATA finished start down the SAS link traning path. */
		scic_sds_phy_start_sas_link_training(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}

/**
 *
 * @phy: This struct scic_sds_phy object which has received an event.
 * @event_code: This is the event code which the phy object is to decode.
 *
 * This method is called when an event notification is received for the phy
 * object when in the state SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. -
 * decode the event - sas phy detected event backs up the state machine to the
 * await speed notification. - identify timeout is an un-expected event and the
 * state machine is restarted. - link failure events restart the starting state
 * machine - any other events log a warning message and set a failure status
 * enum sci_status SCI_SUCCESS on any valid event notification SCI_FAILURE on any
 * unexpected event notifation
 */
static enum sci_status scic_sds_phy_starting_substate_await_sig_fis_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	u32 result = SCI_SUCCESS;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_SATA_PHY_DETECTED:
		/* Backup the state machine */
		sci_base_state_machine_change_state(
			scic_sds_phy_get_starting_substate_machine(this_phy),
			SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
			);
		break;

	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		scic_sds_phy_restart_starting_state(this_phy);
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 event_code);

		result = SCI_FAILURE;
		break;
	}

	return result;
}


/*
 * *****************************************************************************
 * *  SCIC SDS PHY FRAME_HANDLERS
 * ***************************************************************************** */

/**
 *
 * @phy: This is struct scic_sds_phy object which is being requested to decode the
 *    frame data.
 * @frame_index: This is the index of the unsolicited frame which was received
 *    for this phy.
 *
 * This method decodes the unsolicited frame when the struct scic_sds_phy is in the
 * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. - Get the UF Header - If the UF
 * is an IAF - Copy IAF data to local phy object IAF data buffer. - Change
 * starting substate to wait power. - else - log warning message of unexpected
 * unsolicted frame - release frame buffer enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_starting_substate_await_iaf_uf_frame_handler(
	struct scic_sds_phy *this_phy,
	u32 frame_index)
{
	enum sci_status result;
	u32 *frame_words;
	struct sci_sas_identify_address_frame *identify_frame;

	result = scic_sds_unsolicited_frame_control_get_header(
		&(scic_sds_phy_get_controller(this_phy)->uf_control),
		frame_index,
		(void **)&frame_words);

	if (result != SCI_SUCCESS) {
		return result;
	}

	frame_words[0] = SCIC_SWAP_DWORD(frame_words[0]);
	identify_frame = (struct sci_sas_identify_address_frame *)frame_words;

	if (identify_frame->address_frame_type == 0) {
		/*
		 * Byte swap the rest of the frame so we can make
		 * a copy of the buffer */
		frame_words[1] = SCIC_SWAP_DWORD(frame_words[1]);
		frame_words[2] = SCIC_SWAP_DWORD(frame_words[2]);
		frame_words[3] = SCIC_SWAP_DWORD(frame_words[3]);
		frame_words[4] = SCIC_SWAP_DWORD(frame_words[4]);
		frame_words[5] = SCIC_SWAP_DWORD(frame_words[5]);

		memcpy(
			&this_phy->phy_type.sas.identify_address_frame_buffer,
			identify_frame,
			sizeof(struct sci_sas_identify_address_frame)
			);

		if (identify_frame->protocols.u.bits.smp_target) {
			/*
			 * We got the IAF for an expander PHY go to the final state since
			 * there are no power requirements for expander phys. */
			sci_base_state_machine_change_state(
				scic_sds_phy_get_starting_substate_machine(this_phy),
				SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
				);
		} else {
			/* We got the IAF we can now go to the await spinup semaphore state */
			sci_base_state_machine_change_state(
				scic_sds_phy_get_starting_substate_machine(this_phy),
				SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER
				);
		}

		result = SCI_SUCCESS;
	} else
		dev_warn(sciphy_to_dev(this_phy),
			"%s: PHY starting substate machine received "
			"unexpected frame id %x\n",
			__func__,
			frame_index);

	/* Regardless of the result release this frame since we are done with it */
	scic_sds_controller_release_frame(
		scic_sds_phy_get_controller(this_phy), frame_index
		);

	return result;
}

/**
 *
 * @phy: This is struct scic_sds_phy object which is being requested to decode the
 *    frame data.
 * @frame_index: This is the index of the unsolicited frame which was received
 *    for this phy.
 *
 * This method decodes the unsolicited frame when the struct scic_sds_phy is in the
 * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Get the UF Header - If
 * the UF is an SIGNATURE FIS - Copy IAF data to local phy object SIGNATURE FIS
 * data buffer. - else - log warning message of unexpected unsolicted frame -
 * release frame buffer enum sci_status SCI_SUCCESS Must decode the SIGNATURE FIS
 * data
 */
static enum sci_status scic_sds_phy_starting_substate_await_sig_fis_frame_handler(
	struct scic_sds_phy *this_phy,
	u32 frame_index)
{
	enum sci_status result;
	u32 *frame_words;
	struct sata_fis_header *fis_frame_header;
	u32 *fis_frame_data;

	result = scic_sds_unsolicited_frame_control_get_header(
		&(scic_sds_phy_get_controller(this_phy)->uf_control),
		frame_index,
		(void **)&frame_words);

	if (result != SCI_SUCCESS) {
		return result;
	}

	fis_frame_header = (struct sata_fis_header *)frame_words;

	if (
		(fis_frame_header->fis_type == SATA_FIS_TYPE_REGD2H)
		&& !(fis_frame_header->status & ATA_STATUS_REG_BSY_BIT)
		) {
		scic_sds_unsolicited_frame_control_get_buffer(
			&(scic_sds_phy_get_controller(this_phy)->uf_control),
			frame_index,
			(void **)&fis_frame_data
			);

		scic_sds_controller_copy_sata_response(
			&this_phy->phy_type.sata.signature_fis_buffer,
			frame_words,
			fis_frame_data
			);

		/* We got the IAF we can now go to the await spinup semaphore state */
		sci_base_state_machine_change_state(
			scic_sds_phy_get_starting_substate_machine(this_phy),
			SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
			);

		result = SCI_SUCCESS;
	} else
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: PHY starting substate machine received "
			 "unexpected frame id %x\n",
			 __func__,
			 frame_index);

	/* Regardless of the result release this frame since we are done with it */
	scic_sds_controller_release_frame(
		scic_sds_phy_get_controller(this_phy), frame_index
		);

	return result;
}

/*
 * *****************************************************************************
 * * SCIC SDS PHY POWER_HANDLERS
 * ***************************************************************************** */

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method is called by the struct scic_sds_controller when the phy object is
 * granted power. - The notify enable spinups are turned on for this phy object
 * - The phy state machine is transitioned to the
 * SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_starting_substate_await_sas_power_consume_power_handler(
	struct scic_sds_phy *this_phy)
{
	u32 enable_spinup;

	enable_spinup = SCU_SAS_ENSPINUP_READ(this_phy);
	enable_spinup |= SCU_ENSPINUP_GEN_BIT(ENABLE);
	SCU_SAS_ENSPINUP_WRITE(this_phy, enable_spinup);

	/* Change state to the final state this substate machine has run to completion */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_starting_substate_machine(this_phy),
		SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
		);

	return SCI_SUCCESS;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method is called by the struct scic_sds_controller when the phy object is
 * granted power. - The phy state machine is transitioned to the
 * SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_starting_substate_await_sata_power_consume_power_handler(
	struct scic_sds_phy *this_phy)
{
	u32 scu_sas_pcfg_value;

	/* Release the spinup hold state and reset the OOB state machine */
	scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
	scu_sas_pcfg_value &=
		~(SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD) | SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE));
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);

	/* Now restart the OOB operation */
	scu_sas_pcfg_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);

	/* Change state to the final state this substate machine has run to completion */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_starting_substate_machine(this_phy),
		SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN
		);

	return SCI_SUCCESS;
}

/* --------------------------------------------------------------------------- */

struct scic_sds_phy_state_handler
scic_sds_phy_starting_substate_handler_table[SCIC_SDS_PHY_STARTING_MAX_SUBSTATES] =
{
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_ossp_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_sas_phy_speed_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_starting_substate_await_iaf_uf_frame_handler,
		scic_sds_phy_starting_substate_await_iaf_uf_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_sas_power_event_handler,
		scic_sds_phy_starting_substate_await_sas_power_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER, */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_sata_power_event_handler,
		scic_sds_phy_starting_substate_await_sata_power_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN, */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_sata_phy_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN, */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_starting_substate_await_sata_speed_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF, */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_starting_substate_await_sig_fis_frame_handler,
		scic_sds_phy_starting_substate_await_sig_fis_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	}
};

/**
 * scic_sds_phy_set_starting_substate_handlers() -
 *
 * This macro sets the starting substate handlers by state_id
 */
#define scic_sds_phy_set_starting_substate_handlers(phy, state_id) \
	scic_sds_phy_set_state_handlers(\
		(phy), \
		&scic_sds_phy_starting_substate_handler_table[(state_id)] \
		)

/*
 * ****************************************************************************
 * *  PHY STARTING SUBSTATE METHODS
 * **************************************************************************** */

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL. - The initial state
 * handlers are put in place for the struct scic_sds_phy object. - The state is
 * changed to the wait phy type event notification. none
 */
static void scic_sds_phy_starting_initial_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL);

	/* This is just an temporary state go off to the starting state */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_starting_substate_machine(this_phy),
		SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_PHY_TYPE_EN. - Set the
 * struct scic_sds_phy object state handlers for this state. none
 */
static void scic_sds_phy_starting_await_ossp_en_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SPEED_EN. - Set the
 * struct scic_sds_phy object state handlers for this state. none
 */
static void scic_sds_phy_starting_await_sas_speed_en_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF. - Set the
 * struct scic_sds_phy object state handlers for this state. none
 */
static void scic_sds_phy_starting_await_iaf_uf_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER. - Set the
 * struct scic_sds_phy object state handlers for this state. - Add this phy object to
 * the power control queue none
 */
static void scic_sds_phy_starting_await_sas_power_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER
		);

	scic_sds_controller_power_control_queue_insert(
		scic_sds_phy_get_controller(this_phy),
		this_phy
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER. - Remove the
 * struct scic_sds_phy object from the power control queue. none
 */
static void scic_sds_phy_starting_await_sas_power_substate_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_controller_power_control_queue_remove(
		scic_sds_phy_get_controller(this_phy), this_phy
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. - Set the
 * struct scic_sds_phy object state handlers for this state. - Add this phy object to
 * the power control queue none
 */
static void scic_sds_phy_starting_await_sata_power_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER
		);

	scic_sds_controller_power_control_queue_insert(
		scic_sds_phy_get_controller(this_phy),
		this_phy
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER. - Remove the
 * struct scic_sds_phy object from the power control queue. none
 */
static void scic_sds_phy_starting_await_sata_power_substate_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_controller_power_control_queue_remove(
		scic_sds_phy_get_controller(this_phy),
		this_phy
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN. - Set the
 * struct scic_sds_phy object state handlers for this state. none
 */
static void scic_sds_phy_starting_await_sata_phy_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN
		);

	scic_cb_timer_start(
		scic_sds_phy_get_controller(this_phy),
		this_phy->sata_timeout_timer,
		SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - stop the timer
 * that was started on entry to await sata phy event notification none
 */
static void scic_sds_phy_starting_await_sata_phy_substate_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_cb_timer_stop(
		scic_sds_phy_get_controller(this_phy),
		this_phy->sata_timeout_timer
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - Set the
 * struct scic_sds_phy object state handlers for this state. none
 */
static void scic_sds_phy_starting_await_sata_speed_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN
		);

	scic_cb_timer_start(
		scic_sds_phy_get_controller(this_phy),
		this_phy->sata_timeout_timer,
		SCIC_SDS_SATA_LINK_TRAINING_TIMEOUT
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN. - stop the timer
 * that was started on entry to await sata phy event notification none
 */
static void scic_sds_phy_starting_await_sata_speed_substate_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_cb_timer_stop(
		scic_sds_phy_get_controller(this_phy),
		this_phy->sata_timeout_timer
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Set the
 * struct scic_sds_phy object state handlers for this state. - Start the SIGNATURE FIS
 * timeout timer none
 */
static void scic_sds_phy_starting_await_sig_fis_uf_substate_enter(
	struct sci_base_object *object)
{
	bool continue_to_ready_state;
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF
		);

	continue_to_ready_state = scic_sds_port_link_detected(
		this_phy->owning_port,
		this_phy
		);

	if (continue_to_ready_state) {
		/*
		 * Clear the PE suspend condition so we can actually receive SIG FIS
		 * The hardware will not respond to the XRDY until the PE suspend
		 * condition is cleared. */
		scic_sds_phy_resume(this_phy);

		scic_cb_timer_start(
			scic_sds_phy_get_controller(this_phy),
			this_phy->sata_timeout_timer,
			SCIC_SDS_SIGNATURE_FIS_TIMEOUT
			);
	} else {
		this_phy->is_in_link_training = false;
	}
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF. - Stop the SIGNATURE
 * FIS timeout timer. none
 */
static void scic_sds_phy_starting_await_sig_fis_uf_substate_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_cb_timer_stop(
		scic_sds_phy_get_controller(this_phy),
		this_phy->sata_timeout_timer
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL. - Set the struct scic_sds_phy
 * object state handlers for this state. - Change base state machine to the
 * ready state. none
 */
static void scic_sds_phy_starting_final_substate_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_starting_substate_handlers(
		this_phy, SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL
		);

	/*
	 * State machine has run to completion so exit out and change
	 * the base state machine to the ready state */
	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_READY);
}

/* --------------------------------------------------------------------------- */

const struct sci_base_state scic_sds_phy_starting_substates[] = {
	[SCIC_SDS_PHY_STARTING_SUBSTATE_INITIAL] = {
		.enter_state = scic_sds_phy_starting_initial_substate_enter,
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_OSSP_EN] = {
		.enter_state = scic_sds_phy_starting_await_ossp_en_substate_enter,
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_SPEED_EN] = {
		.enter_state = scic_sds_phy_starting_await_sas_speed_en_substate_enter,
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_IAF_UF] = {
		.enter_state = scic_sds_phy_starting_await_iaf_uf_substate_enter,
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SAS_POWER] = {
		.enter_state = scic_sds_phy_starting_await_sas_power_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sas_power_substate_exit,
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_POWER] = {
		.enter_state = scic_sds_phy_starting_await_sata_power_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_power_substate_exit
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_PHY_EN] = {
		.enter_state = scic_sds_phy_starting_await_sata_phy_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_phy_substate_exit
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SATA_SPEED_EN] = {
		.enter_state = scic_sds_phy_starting_await_sata_speed_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sata_speed_substate_exit
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_AWAIT_SIG_FIS_UF] = {
		.enter_state = scic_sds_phy_starting_await_sig_fis_uf_substate_enter,
		.exit_state  = scic_sds_phy_starting_await_sig_fis_uf_substate_exit
	},
	[SCIC_SDS_PHY_STARTING_SUBSTATE_FINAL] = {
		.enter_state = scic_sds_phy_starting_final_substate_enter,
	}
};

/*
 * ***************************************************************************
 * *  DEFAULT HANDLERS
 * *************************************************************************** */

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This is the default method for phy a start request.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_start_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p requested to start from invalid "
		 "state %d\n",
		 __func__,
		 this_phy,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;

}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This is the default method for phy a stop request.  It will report a warning
 * and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_stop_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p requested to stop from invalid "
		 "state %d\n",
		 __func__,
		 this_phy,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This is the default method for phy a reset request.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_reset_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p requested to reset from invalid state "
		 "%d\n",
		 __func__,
		 this_phy,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This is the default method for phy a destruct request.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_destroy_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	/* / @todo Implement something for the default */
	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p requested to destroy from invalid "
		 "state %d\n",
		 __func__,
		 this_phy,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 * @frame_index: This is the frame index that was received from the SCU
 *    hardware.
 *
 * This is the default method for a phy frame handling request.  It will report
 * a warning, release the frame and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_frame_handler(
	struct scic_sds_phy *this_phy,
	u32 frame_index)
{
	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p received unexpected frame data %d "
		 "while in state %d\n",
		 __func__,
		 this_phy,
		 frame_index,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	scic_sds_controller_release_frame(
		scic_sds_phy_get_controller(this_phy), frame_index);

	return SCI_FAILURE_INVALID_STATE;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 * @event_code: This is the event code that was received from the SCU hardware.
 *
 * This is the default method for a phy event handler.  It will report a
 * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	dev_warn(sciphy_to_dev(this_phy),
		"%s: SCIC Phy 0x%p received unexpected event status %x "
		"while in state %d\n",
		__func__,
		this_phy,
		event_code,
		sci_base_state_machine_get_state(
			&this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This is the default method for a phy consume power handler.  It will report
 * a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
 */
enum sci_status scic_sds_phy_default_consume_power_handler(
	struct scic_sds_phy *this_phy)
{
	dev_warn(sciphy_to_dev(this_phy),
		 "%s: SCIC Phy 0x%p given unexpected permission to consume "
		 "power while in state %d\n",
		 __func__,
		 this_phy,
		 sci_base_state_machine_get_state(
			 &this_phy->parent.state_machine));

	return SCI_FAILURE_INVALID_STATE;
}

/*
 * ******************************************************************************
 * * PHY STOPPED STATE HANDLERS
 * ****************************************************************************** */

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method takes the struct scic_sds_phy from a stopped state and attempts to
 * start it. - The phy state machine is transitioned to the
 * SCI_BASE_PHY_STATE_STARTING. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_stopped_state_start_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_STARTING
		);

	return SCI_SUCCESS;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method takes the struct scic_sds_phy from a stopped state and destroys it. -
 * This function takes no action. Shouldnt this function transition the
 * struct sci_base_phy::state_machine to the SCI_BASE_PHY_STATE_FINAL? enum sci_status
 * SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_stopped_state_destroy_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	/* / @todo what do we actually need to do here? */
	return SCI_SUCCESS;
}

/*
 * ******************************************************************************
 * * PHY STARTING STATE HANDLERS
 * ****************************************************************************** */

/* All of these state handlers are mapped to the starting sub-state machine */

/*
 * ******************************************************************************
 * * PHY READY STATE HANDLERS
 * ****************************************************************************** */

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method takes the struct scic_sds_phy from a ready state and attempts to stop
 * it. - The phy state machine is transitioned to the
 * SCI_BASE_PHY_STATE_STOPPED. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_ready_state_stop_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_STOPPED
		);

	return SCI_SUCCESS;
}

/**
 *
 * @phy: This is the struct sci_base_phy object which is cast into a struct scic_sds_phy
 *    object.
 *
 * This method takes the struct scic_sds_phy from a ready state and attempts to reset
 * it. - The phy state machine is transitioned to the
 * SCI_BASE_PHY_STATE_STARTING. enum sci_status SCI_SUCCESS
 */
static enum sci_status scic_sds_phy_ready_state_reset_handler(
	struct sci_base_phy *phy)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)phy;

	sci_base_state_machine_change_state(
		scic_sds_phy_get_base_state_machine(this_phy),
		SCI_BASE_PHY_STATE_RESETTING
		);

	return SCI_SUCCESS;
}

/**
 *
 * @phy: This is the struct scic_sds_phy object which has received the event.
 *
 * This method request the struct scic_sds_phy handle the received event.  The only
 * event that we are interested in while in the ready state is the link failure
 * event. - decoded event is a link failure - transition the struct scic_sds_phy back
 * to the SCI_BASE_PHY_STATE_STARTING state. - any other event recived will
 * report a warning message enum sci_status SCI_SUCCESS if the event received is a
 * link failure SCI_FAILURE_INVALID_STATE for any other event received.
 */
static enum sci_status scic_sds_phy_ready_state_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	enum sci_status result = SCI_FAILURE;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_LINK_FAILURE:
		/* Link failure change state back to the starting state */
		sci_base_state_machine_change_state(
			scic_sds_phy_get_base_state_machine(this_phy),
			SCI_BASE_PHY_STATE_STARTING
			);

		result = SCI_SUCCESS;
		break;

	case SCU_EVENT_BROADCAST_CHANGE:
		/* Broadcast change received. Notify the port. */
		if (scic_sds_phy_get_port(this_phy) != SCI_INVALID_HANDLE)
			scic_sds_port_broadcast_change_received(this_phy->owning_port, this_phy);
		else
			this_phy->bcn_received_while_port_unassigned = true;
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%sP SCIC PHY 0x%p ready state machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 this_phy,
			 event_code);

		result = SCI_FAILURE_INVALID_STATE;
		break;
	}

	return result;
}

/* --------------------------------------------------------------------------- */

/**
 *
 * @this_phy: This is the struct scic_sds_phy object which is receiving the event.
 * @event_code: This is the event code to be processed.
 *
 * This is the resetting state event handler. enum sci_status
 * SCI_FAILURE_INVALID_STATE
 */
static enum sci_status scic_sds_phy_resetting_state_event_handler(
	struct scic_sds_phy *this_phy,
	u32 event_code)
{
	enum sci_status result = SCI_FAILURE;

	switch (scu_get_event_code(event_code)) {
	case SCU_EVENT_HARD_RESET_TRANSMITTED:
		/* Link failure change state back to the starting state */
		sci_base_state_machine_change_state(
			scic_sds_phy_get_base_state_machine(this_phy),
			SCI_BASE_PHY_STATE_STARTING
			);

		result = SCI_SUCCESS;
		break;

	default:
		dev_warn(sciphy_to_dev(this_phy),
			 "%s: SCIC PHY 0x%p resetting state machine received "
			 "unexpected event_code %x\n",
			 __func__,
			 this_phy,
			 event_code);

		result = SCI_FAILURE_INVALID_STATE;
		break;
	}

	return result;
}

/* --------------------------------------------------------------------------- */

struct scic_sds_phy_state_handler
scic_sds_phy_state_handler_table[SCI_BASE_PHY_MAX_STATES] =
{
	/* SCI_BASE_PHY_STATE_INITIAL */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCI_BASE_PHY_STATE_STOPPED */
	{
		{
			scic_sds_phy_stopped_state_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_stopped_state_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCI_BASE_PHY_STATE_STARTING */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCI_BASE_PHY_STATE_READY */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_ready_state_stop_handler,
			scic_sds_phy_ready_state_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_ready_state_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCI_BASE_PHY_STATE_RESETTING */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_resetting_state_event_handler,
		scic_sds_phy_default_consume_power_handler
	},
	/* SCI_BASE_PHY_STATE_FINAL */
	{
		{
			scic_sds_phy_default_start_handler,
			scic_sds_phy_default_stop_handler,
			scic_sds_phy_default_reset_handler,
			scic_sds_phy_default_destroy_handler
		},
		scic_sds_phy_default_frame_handler,
		scic_sds_phy_default_event_handler,
		scic_sds_phy_default_consume_power_handler
	}
};

/*
 * ****************************************************************************
 * *  PHY STATE PRIVATE METHODS
 * **************************************************************************** */

/**
 *
 * @this_phy: This is the struct scic_sds_phy object to stop.
 *
 * This method will stop the struct scic_sds_phy object. This does not reset the
 * protocol engine it just suspends it and places it in a state where it will
 * not cause the end device to power up. none
 */
static void scu_link_layer_stop_protocol_engine(
	struct scic_sds_phy *this_phy)
{
	u32 scu_sas_pcfg_value;
	u32 enable_spinup_value;

	/* Suspend the protocol engine and place it in a sata spinup hold state */
	scu_sas_pcfg_value  = SCU_SAS_PCFG_READ(this_phy);
	scu_sas_pcfg_value |= (
		SCU_SAS_PCFG_GEN_BIT(OOB_RESET)
		| SCU_SAS_PCFG_GEN_BIT(SUSPEND_PROTOCOL_ENGINE)
		| SCU_SAS_PCFG_GEN_BIT(SATA_SPINUP_HOLD)
		);
	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);

	/* Disable the notify enable spinup primitives */
	enable_spinup_value = SCU_SAS_ENSPINUP_READ(this_phy);
	enable_spinup_value &= ~SCU_ENSPINUP_GEN_BIT(ENABLE);
	SCU_SAS_ENSPINUP_WRITE(this_phy, enable_spinup_value);
}

/**
 *
 *
 * This method will start the OOB/SN state machine for this struct scic_sds_phy object.
 */
static void scu_link_layer_start_oob(
	struct scic_sds_phy *this_phy)
{
	u32 scu_sas_pcfg_value;

	scu_sas_pcfg_value = SCU_SAS_PCFG_READ(this_phy);
	scu_sas_pcfg_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
	scu_sas_pcfg_value &=
		~(SCU_SAS_PCFG_GEN_BIT(OOB_RESET) | SCU_SAS_PCFG_GEN_BIT(HARD_RESET));

	SCU_SAS_PCFG_WRITE(this_phy, scu_sas_pcfg_value);
}

/**
 *
 *
 * This method will transmit a hard reset request on the specified phy. The SCU
 * hardware requires that we reset the OOB state machine and set the hard reset
 * bit in the phy configuration register. We then must start OOB over with the
 * hard reset bit set.
 */
static void scu_link_layer_tx_hard_reset(
	struct scic_sds_phy *this_phy)
{
	u32 phy_configuration_value;

	/*
	 * SAS Phys must wait for the HARD_RESET_TX event notification to transition
	 * to the starting state. */
	phy_configuration_value = SCU_SAS_PCFG_READ(this_phy);
	phy_configuration_value |=
		(SCU_SAS_PCFG_GEN_BIT(HARD_RESET) | SCU_SAS_PCFG_GEN_BIT(OOB_RESET));
	SCU_SAS_PCFG_WRITE(this_phy, phy_configuration_value);

	/* Now take the OOB state machine out of reset */
	phy_configuration_value |= SCU_SAS_PCFG_GEN_BIT(OOB_ENABLE);
	phy_configuration_value &= ~SCU_SAS_PCFG_GEN_BIT(OOB_RESET);
	SCU_SAS_PCFG_WRITE(this_phy, phy_configuration_value);
}

/*
 * ****************************************************************************
 * *  PHY BASE STATE METHODS
 * **************************************************************************** */

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_INITIAL. - This function sets the state
 * handlers for the phy object base state machine initial state. none
 */
static void scic_sds_phy_initial_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STOPPED);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_INITIAL. - This function sets the state
 * handlers for the phy object base state machine initial state. - The SCU
 * hardware is requested to stop the protocol engine. none
 */
static void scic_sds_phy_stopped_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	/* / @todo We need to get to the controller to place this PE in a reset state */

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STOPPED);

	scu_link_layer_stop_protocol_engine(this_phy);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_STARTING. - This function sets the state
 * handlers for the phy object base state machine starting state. - The SCU
 * hardware is requested to start OOB/SN on this protocl engine. - The phy
 * starting substate machine is started. - If the previous state was the ready
 * state then the struct scic_sds_controller is informed that the phy has gone link
 * down. none
 */
static void scic_sds_phy_starting_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_STARTING);

	scu_link_layer_stop_protocol_engine(this_phy);
	scu_link_layer_start_oob(this_phy);

	/* We don't know what kind of phy we are going to be just yet */
	this_phy->protocol = SCIC_SDS_PHY_PROTOCOL_UNKNOWN;
	this_phy->bcn_received_while_port_unassigned = false;

	/* Change over to the starting substate machine to continue */
	sci_base_state_machine_start(&this_phy->starting_substate_machine);

	if (this_phy->parent.state_machine.previous_state_id
	    == SCI_BASE_PHY_STATE_READY) {
		scic_sds_controller_link_down(
			scic_sds_phy_get_controller(this_phy),
			scic_sds_phy_get_port(this_phy),
			this_phy
			);
	}
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_READY. - This function sets the state
 * handlers for the phy object base state machine ready state. - The SCU
 * hardware protocol engine is resumed. - The struct scic_sds_controller is informed
 * that the phy object has gone link up. none
 */
static void scic_sds_phy_ready_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_READY);

	scic_sds_controller_link_up(
		scic_sds_phy_get_controller(this_phy),
		scic_sds_phy_get_port(this_phy),
		this_phy
		);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on exiting
 * the SCI_BASE_PHY_STATE_INITIAL. This function suspends the SCU hardware
 * protocol engine represented by this struct scic_sds_phy object. none
 */
static void scic_sds_phy_ready_state_exit(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_suspend(this_phy);
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_RESETTING. - This function sets the state
 * handlers for the phy object base state machine resetting state. none
 */
static void scic_sds_phy_resetting_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_RESETTING);

	/*
	 * The phy is being reset, therefore deactivate it from the port.
	 * In the resetting state we don't notify the user regarding
	 * link up and link down notifications. */
	scic_sds_port_deactivate_phy(this_phy->owning_port, this_phy, false);

	if (this_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
		scu_link_layer_tx_hard_reset(this_phy);
	} else {
		/*
		 * The SCU does not need to have a descrete reset state so just go back to
		 * the starting state. */
		sci_base_state_machine_change_state(
			&this_phy->parent.state_machine,
			SCI_BASE_PHY_STATE_STARTING
			);
	}
}

/**
 *
 * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
 *
 * This method will perform the actions required by the struct scic_sds_phy on
 * entering the SCI_BASE_PHY_STATE_FINAL. - This function sets the state
 * handlers for the phy object base state machine final state. none
 */
static void scic_sds_phy_final_state_enter(
	struct sci_base_object *object)
{
	struct scic_sds_phy *this_phy;

	this_phy = (struct scic_sds_phy *)object;

	scic_sds_phy_set_base_state_handlers(this_phy, SCI_BASE_PHY_STATE_FINAL);

	/* Nothing to do here */
}

/* --------------------------------------------------------------------------- */

const struct sci_base_state scic_sds_phy_state_table[] = {
	[SCI_BASE_PHY_STATE_INITIAL] = {
		.enter_state = scic_sds_phy_initial_state_enter,
	},
	[SCI_BASE_PHY_STATE_STOPPED] = {
		.enter_state = scic_sds_phy_stopped_state_enter,
	},
	[SCI_BASE_PHY_STATE_STARTING] = {
		.enter_state = scic_sds_phy_starting_state_enter,
	},
	[SCI_BASE_PHY_STATE_READY] = {
		.enter_state = scic_sds_phy_ready_state_enter,
		.exit_state = scic_sds_phy_ready_state_exit,
	},
	[SCI_BASE_PHY_STATE_RESETTING] = {
		.enter_state = scic_sds_phy_resetting_state_enter,
	},
	[SCI_BASE_PHY_STATE_FINAL] = {
		.enter_state = scic_sds_phy_final_state_enter,
	},
};