File: ofpathname

package info (click to toggle)
powerpc-utils 1.3.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,044 kB
  • sloc: ansic: 18,549; sh: 4,697; perl: 980; makefile: 239
file content (1991 lines) | stat: -rwxr-xr-x 49,213 bytes parent folder | download | duplicates (2)
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
#! /bin/bash

# Copyright (c) 2004 International Business Machines
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Author Nathan Fontenot <nfont@linux.vnet.ibm.com>

#
# ofpathname - This utility provides a mechanism for converting a logical
# device name to an open firmware device path, and vice versa.
#
# TODO: This script doesn't handle floppy drives and token ring devices,
#       perhaps they should be added in at some point.
#

OFPATHNAME="ofpathname"
VERSION="0.5"
FIND=/usr/bin/find
CAT=/bin/cat
LSPROP=/sbin/lsprop
PSERIES_PLATFORM=$(dirname $0)/pseries_platform

# Find out what platfrom we are running on.  Hopefully this
# list will get expanded with time.
PLATFORM=$(sed /proc/cpuinfo -ne "s/^machine\t*: \(.*\)/\1/p")
case $PLATFORM in
    EFIKA5K2\ *)	PLATFORM=efika ;;
    PowerBook*)		PLATFORM=mac ;;
    PowerMac*)		PLATFORM=mac ;;
esac

# Usage statemnet
usage()
{
    echo "Usage: $OFPATHNAME [OPTION] DEVICE"
    echo "Provide logical device names <==> Open Firmware Device Path Conversion"
    echo ""
    echo "Optional arguments."
    echo "  -l               Convert Open Firmware device pathname to"
    echo "                   logical device name."
    echo "  -a               Find matching Open Firmware device alias[es]."
    echo "  -q, --quiet      Do not report failures, exit quietly"
    echo "  -V, --version    Display version information and exit"
    echo "  -h, --help       Display this help information and exit"
    echo ""
}

show_version()
{
    echo "$OFPATHNAME: Version $VERSION"
    echo "Written by: Nathan Fontenot <nfont@linux.vnet.ibm.com>"
}

#
# err
# Common routine to print error messages for ofpathname.  Since most of the
# error messages can be generated in multiple places, we put all the text
# here to avoid errors in duplicating the messages.
#
# The first and only parameteris the error message number, all of which
# are defined below as ERR_*.
#
ERR_NO_OFPATH=1
ERR_NO_SYSFS=2
ERR_NO_SYSFS_DEVINFO=3
ERR_NOT_CONFIG=4
ERR_NO_LOGDEV=5
err()
{
    local emsg=$1

    if [[ -n $be_quiet ]]; then
        exit 1
    fi

    case $emsg in
        1)  echo "$OFPATHNAME: Could not retrieve Open Firmware device path" 1>&2
            echo "            for logical device \"$DEVNAME_ARG\"." 1>&2 ;;

        2)  echo "$OFPATHNAME: sysfs (/sys) is needed and does not appear" 1>&2
            echo "            to be mounted on this system." 1>&2 ;;

        3)  echo "$OFPATHNAME: Could not find sysfs information for logical" 1>&2
            echo "            device \"$DEVNAME_ARG\"." 1>&2 ;;

        4)  echo "$OFPATHANME: Logical device \"$DEVNAME_ARG\" does not appear" 1>&2
            echo "            to be configured." 1>&2 ;;

        5)  echo "$OFPATHNAME: Could not retrieve logical device name for" 1>&2
            echo "            Open Firmware path \"$DEVNAME_ARG\"." 1>&2 ;;
    esac

    exit 1
}

# is_hbtl
# return true if the link is in HBTL (Host:Bus:Target ID:LUN) format
is_hbtl()
{
        local ln_name=$1;
        local tmp="${ln_name//[^:]}"
        if [[ ${#tmp} = 3 ]]; then
              echo 1
        else
              echo 0
	fi
}
#
# get_link
# return the directory path that a link points to.
# The only parameter is the link name.
#
get_link()
{
    local ln_name=$1;
    echo `ls -l $ln_name 2>/dev/null | awk -F"->" '{print $2}'`
}

#
# get_hbtl
# Given a path that ends in an HBTL (Host:Bus:Target ID:LUN), break it apart
# into its constituent parts in the global vars HOST, BUS, TARGET and LUN
#
# #1 path ending in HBTL
#
get_hbtl()
{
    local hbtl

    HBTL=${1##*/}
    hbtl=$HBTL

    HOST=${hbtl%%:*}
    hbtl=${hbtl#*:}
    BUS=`printf '%x' "${hbtl%%:*}"`
    hbtl=${hbtl#*:}
    ID=`printf '%x' "${hbtl%%:*}"`
    LUN=`printf '%x' "${hbtl#*:}"`
}

#
# get_scsi_disk_no
# Given a path that ends in an HBTL, convert the HBTL values into a
# virtual disk number (not sure what the real terminology is for it).
# To do the conversion, the HBTL (A:B:C:D) is split apart and
# calculated as;
#	no = (0x1000000 | C << 16 | D)
#
# $1 path ending in HBTL
get_scsi_disk_no()
{
    get_hbtl $1

    local C D

    C=$((0x$ID << 16))
    D=$((0x$LUN))

    local vdiskno vdisk
    typeset -i vdiskno
    vdiskno=$((0x1000000 | $C | $D ))
    vdisk=`printf '%X' "${vdiskno#-}"`

    local extrazeroes="00000000"
    echo $vdisk$extrazeroes
}

#
# get_vdisk_no
# Given a path that ends in an HBTL, convert the HBTL values into a
# virtual disk number (not sure what the real terminology is for it).
# To do the conversion, the HBTL (A:B:C:D) is split apart and
# calculated as;
#     no = (0x8000 | B << 8 | C << 5 | D) * 1000000000000
#
# $1 path ending in HBTL
#
get_vdisk_no()
{
    get_hbtl $1

    local B C D
    typeset -i B C D

    B=$((0x$ID << 8))
    C=$((0x$BUS << 5))
    D=$((0x$LUN))

    local vdiskno vdisk
    typeset -i vdiskno
    vdiskno=$((0x8000 | $B | $C | $D ))
    vdisk=`printf '%X' "${vdiskno#-}"`

    local extrazeroes="000000000000"
    echo $vdisk$extrazeroes
}

#
# get_usb_vdisk_no
# Given a path that ends in an HBTL, convert the HBTL values into a
# virtual disk number (not sure what the real terminology is for it).
# To do the conversion, the HBTL (A:B:C:D) is split apart and
# calculated as;
#     no = (0x1000000 | (usb_port << 16) | D);
#
# $1 path ending in HBTL
#
get_usb_vdisk_no()
{
        get_hbtl $1
        local usb_port=$2
        local B

        B=$((0x$usb_port << 16))

        local vdiskno vdisk
        vdiskno=$((0x1000000 | $B | $LUN ))
        vdisk=`printf '%X' "${vdiskno#-}"`

        local extrazeroes="00000000"
        echo $vdisk$extrazeroes
}

#
# get_usb_storage_no
# Get usb device storage (port) number which is captured in
# devpath file
#
# $1 starting directory to look for devpath file
get_usb_storage_no()
{
    for dir in `$FIND /sys -name $1`; do
        # Move up until we find one with a devpath link
        goto_dir $dir "devpath" 0
        if [ $? -eq 0 ]; then
            break;
        fi
    done;

    if [ -f $PWD/devpath ]; then
	echo `$CAT $PWD/devpath`
    else
        err $ERR_NOT_CONFIG
    fi

}

#
# goto_dir
# This looks for a given file in a given directory or any parents of the
# given directory.
#
# $1 starting directory
# $2 file to search for
# $3 on_exit behavior on error
goto_dir()
{
    local start_dir=$(readlink -f $1)
    local fname=$2
    local found=0
    local on_exit=1

    if [[ $# -eq 3 ]]; then
	on_exit=$3
    fi

    cd $start_dir
    while [[ $PWD != "/" ]]; do
	ls $fname >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            found=1
	    break
        fi
        cd ..
    done

    if [[ $found -eq 0 ]]; then
	if [[ $on_exit -eq 1 ]]; then
	    err $ERR_NO_SYSFS_DEVINFO
	else
	    return 1
	fi
    fi
}

#
# find_dir
# This looks for a given file in a given directory or any parents of the
# given directory.  This differs from goto_dir in that we don't actually try
# to cd to the directories, we just return the directory name if found.
#
# $1 starting directory
# $2 file to search for
# $3 on_exit behavior on error
find_dir()
{
    local dir=$1
    local fname=$2

    while [[ -n $dir ]]; do
	/bin/ls $dir/$fname >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
	    echo $dir
	    return
	fi

	dir=${dir%/*}
    done
}

#
# is_pata_dev
# Check to see if this is a PATA device
#
is_pata_dev()
{
    local this_dir=$PWD
    local sysfs_dir
    local udev_path
    local udevinfo="/usr/bin/udevinfo"
    local udevadm="/usr/bin/udevadm"

    if [[ -a $udevadm ]]; then
        udev_path=`$udevadm info --query=path --name=$DEVNAME`
    elif [[ -a $udevinfo ]]; then
        udev_path=`$udevinfo -q path -n $DEVNAME`
    else
        echo "no"
        return
    fi

    if [[ -z $udev_path ]]; then
        echo "no"
    else
	sysfs_dir=`get_link -f /sys/$udev_path/device`
	if [[ ! -d $sysfs_dir ]]; then
	    echo "no"
	else
	    goto_dir $sysfs_dir devspec
	    DEVTYPE=$(cat /proc/device-tree/$(cat $PWD/devspec)/device_type)

	    if [[ $DEVTYPE = "ata" ]]; then
	        echo "yes"
	    else
	        echo "no"
	    fi
	fi
    fi

    cd $this_dir
}

#
# print_aliases
# Print the aliases from /proc/device-tree/aliases for the specified device
#
print_aliases()
{
    dev=$1
    local found=0

    shopt -s nullglob
    for i in /proc/device-tree/aliases/*; do
        if sed -e "s/\x00$//g" $i | grep -qx "$dev" ; then
	    echo ${i##*/}
	    found=1
	fi
    done

    if [[ $found = "0" ]]; then
        echo "No aliases found."
	exit 1
    else
	exit 0
    fi
}

get_slave()
{
	cd /sys/class/*/$1

	while [[ -n "`ls slaves 2> /dev/null`" ]]; do
		cd `echo slaves/* | head -n1 | cut -d " " -f1`;
	done

	$FIND /dev -name "`basename $PWD`"
}

#
# is_net_interface
# Check to see if this is a network interface
#
is_net_interface()
{
    local res

    res=`$FIND /sys/class/net -name $1`
    if [[ ${#res} = 0 ]]; then
	echo "no"
    else
	echo "yes"
    fi
}

# is_nvmf_device
# Check to see if this is a nvmf device
#
is_nvmf_device()
{
    local res

    res=`$FIND /sys/devices/virtual/nvme-fabrics -name $1 2>/dev/null`
    if [[ ${#res} = 0 ]]; then
	    echo "no"
    else
	    echo "yes"
    fi
}

#
# logical_to_ofpathname
# Conversion for logical device name to an Open Firmware device path
#
logical_to_ofpathname()
{
    local is_cdrom

    # follow any links to the real device name
    while [[ -L $DEVNAME ]]; do
        DEVNAME=`get_link $DEVNAME`
    done

    while [[ -L /dev/$DEVNAME ]]; do
        DEVNAME=`get_link /dev/$DEVNAME`
    done

    DEVICE=${DEVNAME##*/}
    DEVNODE=${DEVICE%%[0-9]*}

    # try to determine if this is a cdrom device
    if [[ ${DEVNAME_ARG##*/} = cdrom ]]; then
        is_cdrom=yes
    elif [[ `get_link /dev/cdrom` = /dev/$DEVICE ]]; then
        is_cdrom=yes
    else
        is_cdrom=no
    fi

    case $DEVICE in
        eth*)       l2of_ethernet ;;
        hf*)        l2of_hfi ;;
        sd* | sr*)  # PATA devices appear as sd*, but should be converted
		    # using the ide logic
		    is_pata=$(is_pata_dev $DEVNAME)
		    if [[ $is_pata = "yes" ]]; then
		        l2of_ide
		    else
			l2of_scsi
		    fi ;;
        hd*)        l2of_ide ;;
	vd*)	    l2of_vd ;;
        fd*)        echo "no fd support yet" ;;
        mpath*)
                    DEVNAME=$(printf "dm-%d" `stat -c "%T" /dev/mapper/$DEVICE`)
                    logical_to_ofpathname
                    ;;
        dm-*)
                    DEVNAME=`get_slave $DEVICE`
                    logical_to_ofpathname
                    exit
                    ;;
        nvme*)     #check if the device is a nvmf device
                    local ctrl_name="${DEVICE%n[0-9]*}"
                    is_nvmf=$(is_nvmf_device $ctrl_name)
                    if [[ $is_nvmf = "yes" ]]; then
                        l2of_nvmf
                    else
                        l2of_nvme
                    fi ;;
	*)         # check if the device is a network interface
                   is_net=$(is_net_interface $DEVICE)
                   if [[ $is_net = "yes" ]]; then
                      l2of_ethernet
                   fi ;;
    esac

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    if [[ $is_cdrom = yes ]]; then
        OF_PATH=$OFPATH:1\\ppc\\bootinfo.txt
    fi

    if [[ $do_alias = "1" ]]; then
        print_aliases $OF_PATH
    else
        echo $OF_PATH
    fi
}

#
# l2of_ide
# Conversion routine for logical => OF path of ide devices
#
l2of_ide()
{
    cd /sys/block/$DEVICE
    local link=`get_link "device"`
    if [[ -z $link ]]; then
        err $ERR_NO_SYSFS_DEVINFO
    fi

    # partition number: N in sd*N
    local devpart="${DEVICE##*[a-z]}"
    if [[ $devpart = $DEVICE ]]; then
        devpart='' # no partition number
    fi

    cd $link

    # get the device number
    local devdir=${PWD##/*/}
    local channelno=${devdir%%\.*}
    local devno=${devdir##*\.}

    goto_dir $PWD "devspec"

    OF_PATH=`$CAT $PWD/devspec`
    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    # PCI ATA controller nodes (found on some Macs) have one child per IDE
    # channel.
    case `$CAT "/proc/device-tree/$OF_PATH/device_type"` in
        pci-ata | pci-ide)  OF_PATH=$OF_PATH/@$channelno ;;
    esac

    # On Efika, obtained devno "0:0:0:0" doesn't match actual device.
    # Note: according to vendor, "0,0" means primary master.  Secondary
    # channel is not present, and primary slave is rare enough that we
    # can reasonably ignore it.
    if [ "$PLATFORM" = "efika" ] ; then
        devno=0,0
    fi

    if [ "$PLATFORM" = "mac" ] ; then
        OF_PATH=$OF_PATH/@$devno
    else
        OF_PATH=$OF_PATH/disk@$devno
    fi
}

#
# l2of_vd
# Conversion routine for logical => OF path of virtio block devices
#
l2of_vd()
{
    local found=0

    # There may be many instances of DEVICE under /sys
    for dir in `$FIND /sys -name $DEVICE`; do
	# Move up until we find one with a device link
	goto_dir $dir "device" 0
	if [ $? -eq 0 ]; then
	    found=1;
	    break;
	fi
    done;

    if [ $found -eq 0 ]; then
        err $ERR_NOT_CONFIG
    fi

    # partition number: N in vd*N
    local devpart="${DEVICE##*[a-z]}"
    if [[ $devpart = $DEVICE ]]; then
        devpart='' # no partition number
    fi

    local link=`get_link "device"`
    if [[ -z $link ]]; then
        err $ERR_NO_SYSFS_DEVINFO
    fi
    cd $link

    goto_dir $PWD "devspec"

    OF_PATH=`$CAT $PWD/devspec`
    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    # No partition specified.
    if [[ -z $devpart ]]; then
        return
    fi

    OF_PATH="${OF_PATH}:${devpart}"
}

#
# l2of_ethernet
# Conversion routine for logical => OF path of ethernet devices
#
l2of_ethernet()
{
    for syspath in `$FIND /sys -name $DEVICE 2> /dev/null`; do
        if [[ -e $syspath/device/devspec ]]; then
            OF_PATH=`$CAT $syspath/device/devspec`
            break
        fi
    done

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi
}

#
# l2of_hfi
# Conversion routine for logical => OF path of HFI devices
#
l2of_hfi()
{
    local hfnum=${DEVICE##hf}
    local hfpath

    if [[ $hfnum = "0" || $hfnum = "2" ]]; then
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | head -n 1`
    elif [[ $hfnum = "1" || $hfnum = "3" ]]; then
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | tail -n 1`
    else
        err $ERR_NO_OFPATH
    fi

    OF_PATH=${hfpath##/proc/device-tree}
}

#
# l2of_nvme
# Conversion routine for logical => OF path of nvme devices
#
l2of_nvme()
{
    # OF path: <devspec>/namespace@<namespace-id>:<partition-number>

    # disk: nvmeX, nvmeXnY; not nvmeXnYpZ
    local devdisk="${DEVICE%p[0-9]*}"

    local dev_ctrl=${devdisk%n[0-9]*}
    # namespace id: Y in nvmeXnY, nvmeXnYpZ
    local devnsid="${devdisk#nvme[0-9]*n}"
    if [[ $devnsid = $devdisk ]]; then
        devnsid='' # no namespace id
    fi

    # partition number: Z in nvmeXnYpZ
    local devpart="${DEVICE##*p}"
    if [[ $devpart = $DEVICE ]]; then
        devpart='' # no partition number
    fi

    # Get the device-tree device specification (devspec).
    local dir
    local found=0

    for dir in `$FIND /sys/devices -name "$dev_ctrl"`; do
        cd $dir

        goto_dir $PWD "device/devspec"

        devspec=`$CAT $PWD/device/devspec | tr -d '\000'`
        if [[ -n $devspec ]]; then
            found=1
            if [[ -n $devnsid ]]; then
                # Linux logical nsid might not match nvme controller nsid
                for nsid_dir in `$FIND /sys/devices -name "DEVICE"`; do
                    goto_dir $dir_dir "nsid"
                    devnsid=`$CAT $PWD/nsid | tr -d '\000'`
                done
            fi
            break
        fi
    done

    if [[ $found -eq 0 ]]; then
        err $ERR_NO_SYSFS_DEVINFO
    fi

    if [[ -z $devspec ]]; then
        err $ERR_NO_OFPATH
    fi

    # OF path: <devspec>/namespace@<namespace-id>:<partition-number>
    OF_PATH="$devspec"

    # No namespace id (nY) specified.
    if [[ -z $devnsid ]]; then
        return
    fi

    # Device type is usually 'namespace'.
    # Get it from device-tree just in case.
    devtype=`$CAT /proc/device-tree${devspec}/namespace/name | tr -d '\000'` 2> /dev/null
    if [[ -z $devtype ]]; then
        err $ERR_NO_OFPATH
    fi

    devnsid=$(printf "%x" $devnsid)
    OF_PATH="$OF_PATH/$devtype@$devnsid"

    # No partition (pZ) specified.
    if [[ -z $devpart ]]; then
        return
    fi

    OF_PATH="${OF_PATH}:${devpart}"
}

#
# l2of_nvmf
# Conversion routine for logical => OF path of nvme devices
#
l2of_nvmf()
{

    # OF path: <devspec>/nvme-of/controller@<target-wwpn>,<ctrl-id>:
    #          nqn=<tgt-subsystem-nqn> /namespace@<namespace-id>:
    #          <disk-label-args>

    # disk: nvmeX, nvmeXnY; not nvmeXnYpZ
    local devdisk="${DEVICE%p[0-9]*}"

    # namespace id: Y in nvmeXnY, nvmeXnYpZ
    local devnsid="${devdisk#nvme[0-9]*n}"
    if [[ $devnsid = $devdisk ]]; then
        devnsid='' # no namespace id
    fi

    # partition number: Z in nvmeXnYpZ
    local devpart="${DEVICE##*p}"
    if [[ $devpart = $DEVICE ]]; then
        devpart='' # no partition number
    fi

    # controller name: nvmeX
    local ctrl_name="${DEVICE%n[0-9]*}"

    # Get the device-tree device specification (devspec).
    local dir

    for dir in `$FIND /sys/devices/virtual/nvme-fabrics -name "$ctrl_name"`; do
        cd $dir
        if [[ -f $PWD/address ]]; then
            h_wwpn=`$CAT $PWD/address | cut -d "-" -f 5`
            h_wwpn="${h_wwpn#0x}"
            t_wwpn=`$CAT $PWD/address | cut -d "-" -f 3`
            t_wwpn="${t_wwpn#0x}"
            t_wwpn="${t_wwpn%,*}"
            nqn=`$CAT $PWD/subsysnqn`
            cntlid=`echo ffff`
            if [[ -n $h_wwpn ]]; then
                for f in `$FIND /sys/devices -name "port_name"`; do
                    sys_wwpn=`$CAT $f 2>/dev/null`
                    sys_wwpn="${sys_wwpn#0x}"
                    if [[ $h_wwpn = $sys_wwpn ]] && \
                    [[ "$f" == *"fc_host"* ]]; then
                        f=${f%/*}
                        goto_dir $f "device" 0
                        link=`get_link "device"`
                        cd $link
                        goto_dir $PWD "devspec"
                        if [[ -e $PWD/devspec ]]; then
                            OF_PATH=`$CAT $PWD/devspec`
                            break
                        fi
                    fi
                done
            fi
        fi
    done

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    OF_PATH="${OF_PATH}/nvme-of/controller@${t_wwpn},${cntlid}:nqn=${nqn}"

    # No namespace id (nY) specified.
    if [[ -n $devnsid ]]; then
        res=`$FIND /sys/devices/virtual -name ${devdisk}`
        if [[ ${#res} = 0 ]]; then
            OF_PATH=""
        else
            goto_dir $res
            devnsid=`$CAT $PWD/nsid | tr -d '\000'`
	    devnsid=$(printf "%x" $devnsid)
            OF_PATH="$OF_PATH/namespace@$devnsid"
        fi
    fi

    # No partition (pZ) specified.
    if [[ -n $devpart ]]; then
        res=`$FIND /sys/devices/virtual -name ${devdisk}p${devpart}`
        if [[ ${#res} = 0 ]]; then
            OF_PATH=""
        else
            goto_dir $res
            devpart=`$CAT $PWD/partition | tr -d '\000'`
            OF_PATH="${OF_PATH}:${devpart}"
        fi
    fi
}

#
# int_to_scsilun
# Conversion routine for SCSI HBTL LUN => SCSI LUN name
#
int_to_scsilun()
{
    local lunint=$1
    local A B C D

    A=$(( ($lunint >> 8) & 0xff ))
    B=$(($lunint & 0xff))
    C=$(( ($lunint >> 24) & 0xff ))
    D=$(( ($lunint >> 16) & 0xff ))

    local lunstr=$(printf "%02x%02x%02x%02x00000000" $A $B $C $D)
    lunstr=`echo $lunstr | sed 's/^[0]*//'`
    echo "$lunstr"
}

#
# scsilun_to_int
# Conversion routine for SCSI LUN name => SCSI HBTL LUN
#
scsilun_to_int()
{
    local lunstr=$1
    local A B C D L

    L=$(( 0x${lunstr/00000000} ))

    A=$(( ($L >> 8) & 0xff ))
    B=$(($L & 0xff))
    C=$(( ($L >> 24) & 0xff ))
    D=$(( ($L >> 16) & 0xff ))

    L=$(( (($A << 24) | ($B << 16) | ($C << 8) | $D) ))
    echo "$L"
}

get_fc_scsilun()
{
    local lun=$1
    local L=$(( 0x$lun ))

    local fc_lun=`int_to_scsilun $L`
    echo "$fc_lun"
}

get_fc_wwpn()
{
    local start_dir=$1

    for f in `$FIND -H $start_dir -maxdepth 2 -name port_name`; do
        local wwpn=`$CAT $f`
	break
    done

    # strip the leading 0x
    wwpn=${wwpn:2}

    echo "$wwpn"
}

#
# l2of_scsi
# Converion routine for logical => OF path of scsi devices
#
l2of_scsi()
{
    local found=0
    local devtype

    # There may be many instances of DEVICE under /sys
    for dir in `$FIND /sys/class/block -name $DEVICE`; do
	# Move up until we find one with a device link
	goto_dir $dir "device" 0
	if [ $? -eq 0 ]; then
	    found=1;
	    break;
	fi
    done;

    if [ $found -eq 0 ]; then
        err $ERR_NOT_CONFIG
    fi

    # partition number: N in sd*N
    local devpart="${DEVICE##*[a-z]}"
    if [[ $devpart = $DEVICE ]]; then
        devpart='' # no partition number
    fi

    # follow the 'device' link
    local link=`get_link "device"`
    if [[ -z $link ]]; then
	# device may not be a link
	link=device
    fi

    get_hbtl $link
    cd $link

    # save the name of the current directory, we may need it later...
    local device_dir=${PWD##/*/}
    local device_path=$PWD

    # move up directories until we find one with devspec information
    goto_dir $PWD "devspec"

    if [[ -e $PWD/devspec ]]; then
        OF_PATH=`$CAT $PWD/devspec`
        SYS_PATH=$PWD
    else
        err $ERR_NO_OFPATH
    fi

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    elif [[ ! -e /proc/device-tree/$OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    local vdev=${OF_PATH%/*}
    local fc=${OF_PATH%@*}
    fc=${fc##/*/}

    if [[ -e /proc/device-tree$OF_PATH/device_type ]]; then
        devtype=`tr -d '\0' < /proc/device-tree$OF_PATH/device_type`;
        if [[ $devtype = "fcp" || $devtype = "scsi-fcp" ]]; then
                fc="fibre-channel";
        fi
    fi

    if [[ $fc = "usb" ]]; then
	 local hub_no storage_no disk_no

        storage_no=`get_usb_storage_no $DEVICE`
        if [[ $storage_no = *.* ]]; then
                hub_no=${storage_no%%.*}
                storage_no=${storage_no##*.}
        fi

        disk_no=`get_usb_vdisk_no $device_dir $storage_no`

        if [[ -z $hub_no ]]; then
                OF_PATH=$OF_PATH/storage\@$storage_no/disk\@$disk_no
        else
                OF_PATH=$OF_PATH/hub\@$hub_no/storage\@$storage_no/disk\@$disk_no
        fi

   elif [[ $fc = "fibre-channel" ]]; then
	local wwpn=`get_fc_wwpn "$device_path/../../fc_remote_ports*"`

        if [[ ! -e /proc/device-tree$OF_PATH/disk ]]; then
                for dir in `$FIND /proc/device-tree$OF_PATH -type d`; do
                        if [[ -e $dir/disk ]]; then
                                OF_PATH=${dir##/proc/device-tree}
                                break;
                        fi
                done
        fi

        OF_PATH=$(printf "%s/disk@%s" $OF_PATH $wwpn)

        if [[ $LUN != "0" ]]; then
                local fc_lun=`get_fc_scsilun $LUN`
                OF_PATH=$(printf "%s,%s" $OF_PATH $fc_lun)
        fi
    elif [[ $vdev = "/vdevice" ]]; then
        # get the v-device data
        local tmp=${OF_PATH//\/vdevice/}
        local vdevtype=${tmp%@*}

        if [[ $vdevtype = "/vfc-client" ]]; then
                local vfc_lun=`get_fc_scsilun $LUN`
		local wwpn=`get_fc_wwpn "$device_path/../../fc_remote_ports*"`
                OF_PATH=$(printf "%s/disk@%s,%s" $OF_PATH $wwpn $vfc_lun)
        else
                local i vdiskno
		goto_dir $device_path $device_dir
                vdiskno=`get_vdisk_no $device_dir`

                OF_PATH=$OF_PATH/disk\@$vdiskno
        fi
    elif [[ -d /proc/device-tree$OF_PATH/sas ]]; then
        local vendor_id sas_id

	vendor_id=`od -tx /proc/device-tree/$OF_PATH/vendor-id`
	vendor_id=`echo $vendor_id | cut -d " " -f 2`
	if [[ $vendor_id = "00001000" ]]; then
	    local dev_id
	    goto_dir $device_path "sas_end_device*"
	    dev_id=${PWD##*-}
	    sas_id=`cat /sys/class/sas_device/end_device-$dev_id/sas_address`
	    sas_id=${sas_id##0x}

	    OF_PATH=$(printf "%s/sas/disk@%s" $OF_PATH $sas_id)
	    if [[ $LUN != "0" ]]; then
                local LUNSTR=`int_to_scsilun $LUN`
                OF_PATH=$(printf "%s,%s" $OF_PATH $LUNSTR)
	    fi
	else
            local B T L
            local fwtype="0"

            if [[ -e /sys/class/scsi_host/host$HOST/fw_type ]]; then
                fwtype=`$CAT /sys/class/scsi_host/host$HOST/fw_type`
            fi

            if [[ $fwtype = "1" ]]; then
                goto_dir $device_path "device_id"
                sas_id=`$CAT $PWD/device_id`
                sas_id=${sas_id##0x}

	        OF_PATH=$(printf "%s/sas/disk@%s" $OF_PATH $sas_id)
                if [[ $LUN != "0" ]]; then
                    local LUNSTR=`int_to_scsilun $LUN`
                    OF_PATH=$(printf "%s,%s" $OF_PATH $LUNSTR)
                fi
            else
                B=$(( 0x$BUS ))
                T=$(( 0x$ID ))
                L=$(( 0x$LUN ))

                sas_id=$(( ($B << 16) | ($T << 8) | $L ))

	        OF_PATH=$(printf "%s/sas/disk@%x" $OF_PATH $sas_id)
                if [[ $LUN != "0" ]]; then
                    OF_PATH=$(printf "%s,%x" $OF_PATH $LUN)
                fi
            fi
	fi
    else

        plug_id=$(ls -dv $SYS_PATH/*/host* 2>/dev/null | grep -n "/host$HOST$")
        [ -z "$plug_id" ] && {
                plug_id=$(ls -dv $SYS_PATH/host* 2>/dev/null | grep -n "/host$HOST$")
        }
        plug_id=$((${plug_id%%:*}-1))

        # make sure the "scsi" information is on the end of the path
        local scsi_name=${OF_PATH##/*/}
        scsi_name=${scsi_name%%@*}
        if [[ $scsi_name != "scsi" && "$PLATFORM" != "mac" ]]; then
            scsi_name="scsi@$BUS"
            OF_PATH=$OF_PATH/$scsi_name
        elif [[ $scsi_name != "scsi" && "$PLATFORM" = "mac" && $devtype != "ata" ]]; then
            scsi_name="@$plug_id"
            OF_PATH=$OF_PATH/$scsi_name
        fi

        local modalias=""
        goto_dir $device_path "device"
        if [ $? -eq 0 ]; then
              modalias=`$CAT $PWD/modalias`
        fi

        if [[ $modalias =~ "virtio" ]]; then
             local diskno
             diskno=`get_scsi_disk_no $device_dir`
             OF_PATH=$OF_PATH/disk\@$diskno
        else
             if [ "$PLATFORM" = "mac" ] ; then
                 OF_PATH=$OF_PATH/@$ID
             else
                 OF_PATH=$OF_PATH/sd@$ID,$LUN
             fi
       fi
    fi

    # No partition specified.
    if [[ -z $devpart ]]; then
        return
    fi

    OF_PATH="${OF_PATH}:${devpart}"
}

#
# ofpathname_to_logical
# Conversion for Open Firmware device paths to logical device names
#
ofpathname_to_logical()
{
    DEVPATH=${DEVNAME%/*}
    DEVICE=${DEVNAME##/*/}
    DEVTYPE=${DEVICE%\@*}
    SAS=${DEVPATH##/*/}
    FC=${SAS%%\@*}

    if [[ $do_alias = "1" ]]; then
        print_aliases $OF_PATH
    fi

    if [[ $DEVTYPE = "disk" && $FC = "v-scsi" ]]; then
        DEVTYPE="v-scsi"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "scsi" ]]; then
        DEVTYPE="scsi"
    fi

    if [[ $DEVTYPE = "disk" && $SAS = "sas" ]]; then
        DEVTYPE="sas"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "vfc-client" ]]; then
        DEVTYPE="vfc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "fibre-channel" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "QLGC,qlc" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "fp" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "storage" ]]; then
        local devpath=$DEVPATH
        DEVPATH=${devpath%/*}
        DEVTYPE="usb"

        if [[ $DEVNAME = *hub* ]]; then
            devpath=$DEVPATH
            DEVPATH=${devpath%/*}
        fi
    fi

    if [[ $DEVTYPE = "namespace" ]]; then
        DEVTYPE="nvme"
    fi

    if [[ "$DEVPATH" == *"nvme-of"* ]]; then
        DEVTYPE="nvmf"
    fi


    # Remove any possible cdrom data from DEVICE
    if [[ ${DEVICE##*,} = "\ppc\bootinfo.txt" ||
          ${DEVICE##*,} = \ppc\bootinfo.txt ]]; then
        DEVICE=${DEVICE%,*}
    fi

    # Remove any possible yaboot suffix from DEVICE
    if [[ ${DEVICE##*,} = "yaboot" ]]; then
	DEVICE=${DEVICE%,*}
    fi

    # Remove any possible partition reference
    PART=$(expr "$DEVICE" : '.*\(:[0-9]\)')
    if [[ -n $PART ]] && \
    [[ $DEVTYPE != "nvme" ]] && \
    [[ $DEVTYPE != "nvmf" ]]; then
        PART=${PART:1}
        DEVICE=${DEVICE%:[0-9]}
    fi

    case $DEVTYPE in
        sd* | scsi*   )  of2l_scsi ;;
        sas           )  of2l_sas ;;
        vfc           )  of2l_vfc ;;
        fc            )  of2l_fc ;;
        v-scsi | disk )  of2l_vscsi
                         if [[ -z $LOGICAL_DEVNAME && $DEVTYPE = disk ]]; then
                             of2l_ide
                          fi ;;
        eth* | l-lan  )  of2l_ethernet ;;
	vnic          )  of2l_ethernet ;;
        hfi-ethernet* )  of2l_hfi ;;
        disk*         )  of2l_ide ;;
        usb           )  of2l_usb ;;
        nvme          )  of2l_nvme ;;
        nvmf          )  of2l_nvmf ;;
    esac

    if [[ -z $LOGICAL_DEVNAME ]]; then
        err $ERR_NO_LOGDEV
    fi

    # Add any previously stripped partition reference
    if [[ -n $PART ]] && \
    [[ $DEVTYPE != "nvme" ]] && \
    [[ $DEVTYPE != "nvmf" ]]; then
        LOGICAL_DEVNAME=$LOGICAL_DEVNAME$PART
    fi

    # See if this device is the cdrom
    if [[ `get_link "/dev/cdrom"` = $LOGICAL_DEVNAME ]]; then
        LOGICAL_DEVNAME="cdrom"
    fi

    echo $LOGICAL_DEVNAME
}

#
# of2l_ide
# Conversion routine for OF path => logical name for ide devices
#
of2l_ide()
{
    local dir

    for dir in `$FIND /sys/block -name 'hd*'`; do
        # get devno
        local devno=${DEVICE##*@}
        devno=${devno%%:*}

        cd $dir
        local link=`get_link "device"`
        if [[ -n $link ]]; then
            cd $link

            # see if this is the correct device
            local this_devno=${PWD##*\.}
            if [[ $devno -eq $this_devno ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    break
                fi
            fi
        fi
    done
}

#
# of2l_ethernet
# Conversion routine for OF path => logical names of ethernet devices
#
of2l_ethernet()
{
    local dir

    # strip off ip info if present
    local devname=${DEVNAME%%:*}
    local netdir="/sys/class/net"

    for dir in `ls $netdir`; do
	local devdir=`find_dir $netdir/$dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        cd $link
        local devspec=`$CAT ./devspec 2>/dev/null`

        if [[ $devspec = $devname ]]; then
            LOGICAL_DEVNAME="${dir##*/}"
            return
        fi
    done
}

#
# of2l_hfi
# Conversion routine for OF path => logical names of HFI devices
#
of2l_hfi()
{
    # strip off ip info if present
    local devname=${DEVNAME%%:*}
    local hfpath

    hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | head -n 1`
    hfpath=${hfpath##/proc/device-tree}

    if [[ $hfpath = $devname ]] ; then
        LOGICAL_DEVNAME="hf0"
    else
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | tail -n 1`
        hfpath=${hfpath##/proc/device-tree}

        if [[ $hfpath = $devname ]] ; then
            LOGICAL_DEVNAME="hf1"
        else
            err $ERR_NO_LOGDEV
        fi
    fi
}

#
# of2l_usb
# Conversion routine for OF path => logical names of usb devices
#
of2l_usb()
{
    DEV_HBTL_NO=${DEVICE##*\@}

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
        local devdir=`find_dir $dir device`
        if [[ -z $devdir ]]; then
            continue
        fi

        cd $devdir

        local link=`get_link "device"`
        local vdisk_no

        if [[ -n $link ]]; then
           local port_no storage_no target

           target=${link##*/}
           port_no=`get_usb_storage_no $target`
           storage_no=${port_no##*.}

           vdisk_no=`get_usb_vdisk_no $target $storage_no`

           cd $link
           if [[ $vdisk_no = $DEV_HBTL_NO ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME=${dir##/*/}
                    return
                fi
            fi
        fi
    done
}

#
# of2l_vscsi
# Conversion routine for OF path => logical names of virtual scsi devices
#
of2l_vscsi()
{
    DEV_HBTL_NO=${DEVICE##*\@}

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -n $link ]]; then
            local vdiskno=`get_vdisk_no $link`

            cd $link
            if [[ $vdiskno = $DEV_HBTL_NO ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME=${dir##/*/}
                    return
                fi
            fi
	fi
    done
}

#
# of2l_scsi
# Conversion routine for OF path => logical names of scsi devices
#
of2l_scsi()
{
    DEV_HBTL_NO=${DEVICE##*\@}
    DEV_TARGET=${DEVICE##*\@}
    DEV_TARGET=${DEV_TARGET%%,*}
    DEV_LUN=${DEVICE##*,}

    # At this point DEV_LUN may be in the form X:Y, we're only interested
    # in the X component.
    DEV_LUN=${DEV_LUN%%:*}

    local dir
    for dir in `$FIND /sys/block -name '[sv][dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        local hbtl=`is_hbtl $link`
        local diskno
        # Do not call get_hbtl for virtio block devices
        if [[ $hbtl = 1 ]]; then
               get_hbtl $link
               diskno=`get_scsi_disk_no $link`
	fi
        cd $link

        # save the name of the current directory, we may need it later...
        local device_dir=${PWD##/*/}

        if [[ $hbtl = 0 || $diskno = $DEV_HBTL_NO ||
		($ID = $DEV_TARGET && $LUN = $DEV_LUN) ]]; then
            goto_dir $PWD "devspec"
            local devspec=`$CAT ./devspec 2>/dev/null`

            # Handle virtio block devices
            if [[ $hbtl = 0 && $devspec = $DEVNAME ]]; then
                LOGICAL_DEVNAME="${dir##*/}"
                return
            fi

            if [[ $devspec = $DEVPATH ]]; then
                LOGICAL_DEVNAME="${dir##*/}"
                return
            fi

            local scsi_name=${devspec##/*/}
            scsi_name=${scsi_name%%@*}

            if [[ $scsi_name != "scsi" ]]; then
                scsi_name="scsi@$BUS"
                devspec=$devspec/$scsi_name

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        fi
    done
}

#
# of2l_sas
# Conversion routine for OF path => logical names of sas devices
#
of2l_sas()
{
    local matchtype dir

    DEV_NAME=${DEVICE##*\@}
    DEV_ID=${DEV_NAME%%,*}

    LUN=${DEV_NAME/$DEV_ID}
    LUN=${LUN/,/}

    if [[ ${#LUN} = 0 ]]; then
        LUN="0"
    fi

    lunint=`scsilun_to_int $LUN`

    PCI_ID=${DEVPATH%%/sas*}
    vendor_id=`od -tx /proc/device-tree/$PCI_ID/vendor-id`
    vendor_id=`echo $vendor_id | cut -d " " -f 2`

    if [[ $vendor_id = "00001000" ]]; then
        matchtype="libsas"
    else
        matchtype="ipr32"
    fi

    for dir in `$FIND /sys/class/scsi_host -maxdepth 1 -name 'host*'`; do
        cd $dir
        local link=`get_link "device"`

        cd $link
        cd ..

        local devspec=`$CAT ./devspec 2>/dev/null`
        if [[ $devspec = $PCI_ID ]]; then
                # check for ipr64
                if [[ -e $dir/fw_type ]]; then
                        local fwtype=`$CAT $dir/fw_type`
                        if [[ $fwtype = "1" ]]; then
                                matchtype="ipr64"
                                break
                        fi
                fi

                break
        fi
    done

    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        get_hbtl $link
        cd $link

        # save the name of the current directory, we may need it later...
        local device_dir=$PWD

        local B T L
        B=$(( 0x$BUS ))
        T=$(( 0x$ID ))
        L=$(( 0x$LUN ))

        if [[ $matchtype = "ipr32" ]]; then
            local sas_id=$((($B << 16) | ($T << 8) | $L))
            sas_id=`printf '%x' "$sas_id"`

            if [[ $sas_id = $DEV_ID ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec/sas = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        elif [[ $matchtype = "ipr64" ]]; then
            if [[ -e $devdir/device/device_id ]]; then
                local deviceid=`$CAT $devdir/device/device_id`
                deviceid=${deviceid##0x}

                if [[ $deviceid != $DEV_ID ]]; then
                    continue
                fi

                if [[ $L = $lunint ]]; then
                    goto_dir $PWD "devspec"
                    local devspec=`$CAT ./devspec 2>/dev/null`

                    if [[ $devspec/sas = $DEVPATH ]]; then
                        LOGICAL_DEVNAME="${dir##*/}"
                        return
                    fi
                fi
            fi
        elif [[ $matchtype = "libsas" ]]; then
            local dev_id
	    goto_dir $device_dir "sas_end_device*" 0
	    dev_id=${PWD##*-}

            if [[ ! -e /sys/class/sas_device/end_device-$dev_id/sas_address ]]; then
                continue
            fi

	    sas_id=`cat /sys/class/sas_device/end_device-$dev_id/sas_address`
	    sas_id=${sas_id##0x}

            if [[ $sas_id != $DEV_ID ]]; then
                continue
            fi

            if [[ $L = $lunint ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec/sas = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        else
                err $ERR_NO_LOGDEV
        fi

    done
}

#
# of2l_vfc
# Conversion routine for OF path => logical names of vFC devices
#
of2l_vfc()
{
    DEV_ID=${DEVICE##*\@}
    OF_WWPN=${DEV_ID%%,*}
    OF_LUN=${DEV_ID##$OF_WWPN}
    OF_LUN=${OF_LUN#,}
    OF_LUN=`echo $OF_LUN | sed 's/^[0]*//'`

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            continue
        fi

        get_hbtl $link
        cd $link
        link=$PWD

        local device_dir=${PWD##/*/}
        goto_dir $PWD "devspec" 0
	if [[ $? -eq 1 ]]; then
		continue;
	fi
        OF_PATH=`$CAT $PWD/devspec`

        if [[ -z $OF_PATH ]]; then
		err $ERR_NO_LOGDEV
        fi

        # Skip if this is not the correct FC port
        if [[ $OF_PATH != $DEVPATH ]] ; then
                continue;
        fi

        local vdev=${OF_PATH%/*}
        local tmp=${OF_PATH//\/vdevice/}
        local vdevtype=${tmp%@*}

        if [[ $vdevtype != "/vfc-client" ]]; then
                continue
        fi

        local vfc_lun=`get_fc_scsilun $LUN`
	local wwpn=`get_fc_wwpn "$link/../../fc_remote_ports*"`

        if [[ $vfc_lun = $OF_LUN && $wwpn = $OF_WWPN ]]; then
            if [[ $OF_PATH = $DEVPATH ]]; then
            LOGICAL_DEVNAME="${dir##*/}"
            return
            fi
        fi
    done
}

#
# of2l_fc
# Conversion routine for OF path => logical names of FC devices
#
of2l_fc()
{
    DEV_ID=${DEVICE##*\@}
    OF_WWPN=${DEV_ID%%,*}
    OF_LUN=${DEV_ID/$OF_WWPN}
    OF_LUN=${OF_LUN/,/}

    if [[ ${#OF_LUN} = 0 ]]; then
        OF_LUN="0"
    fi

    lunint=`scsilun_to_int $OF_LUN`
# Need to correlate the node_name in fc_host with the port-wwn in the device tree
# to be sure we have the right port

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            continue
        fi

        get_hbtl $link
        cd $link
        link=$PWD

	# find device_path, not all dirs will have a fc_remote_ports
	goto_dir $PWD "fc_remote_ports*" 0
	if [[ $? -eq 1 ]]; then
	    continue
	fi
	device_path=$PWD

	cd $link
        local device_dir=${PWD##/*/}
        goto_dir $PWD "devspec" 0
	if [[ $? -eq 1 ]]; then
		continue;
	fi
        OF_PATH=`$CAT devspec`

        if [[ -z $OF_PATH ]]; then
		err $ERR_NO_LOGDEV;
        fi

        # Skip if this is not the correct FC port
        if [[ $OF_PATH != $DEVPATH ]] ; then
                continue;
        fi

	local wwpn=`get_fc_wwpn "$device_path/fc_remote_ports*"`

        if [[ $wwpn = $OF_WWPN ]]; then
                local L=$(( 0x$LUN ))

                if [[ $L = $lunint ]]; then
                        LOGICAL_DEVNAME="${dir##*/}"
                        return
                fi
        fi
    done
}

#
# of2l_nvme
# Conversion routine for OF path => logical name for nvme devices
#
of2l_nvme()
{
    # get namespace id and partition number
    local nsid_part=${DEVICE##*@} # <namespace-id>[:partition-number]
    local nsid=${nsid_part%:*} # namespace id
    local part # partition number

    # set partition number only if ':' is present
    case "${nsid_part}" in
    *:*)
        part=${nsid_part#*:}
        ;;
    esac

    local dir
    local link

    for dir in `$FIND /sys/class/nvme -name "nvme[0-9]*"`; do
        cd $dir

        link=`get_link "device"` # points to pci address dir
        if [[ -n $link ]]; then
            cd $link
        else
            continue
        fi

        if [[ "$PWD" == *"nvme-fabrics"* ]]; then
            continue
        fi

        goto_dir $PWD "devspec"
        local devspec=`$CAT ./devspec 2>/dev/null`

        if [[ $devspec = $DEVPATH ]]; then
            LOGICAL_DEVNAME="${dir##*/}"
            break
        fi
    done

    for dir in `$FIND /sys/block -name "${LOGICAL_DEVNAME}n[0-9]*"`; do
	    cd $dir

	    local devnsid=`$CAT ./nsid 2>/dev/null`
	    devnsid=$(printf "%x" $devnsid)
	    if [[ $devnsid = $nsid ]]; then
		    LOGICAL_DEVNAME="${dir##*/}"
		    break
	    fi
    done

    if [[ -n $LOGICAL_DEVNAME ]] \
    && [[ -n $part ]]; then

        if [[ -d "/sys/block/${LOGICAL_DEVNAME}/${LOGICAL_DEVNAME}p${part}" ]]; then
            LOGICAL_DEVNAME="${LOGICAL_DEVNAME}p${part}"
        else
            LOGICAL_DEVNAME=''
        fi
    fi
}

# of2l_nvmf
# Conversion routine for OF path => logical name for nvme devices
#
of2l_nvmf()
{
    # get namespace id and partition number
    DEVICE=${DEVNAME##/*/}
    if [[ "$DEVICE" == *"namespace"* ]]; then
        local nsid_part=${DEVICE##*@} # <namespace-id>[:partition-number]
        local nsid=${nsid_part%:*} # namespace id
    fi
    of_path_addr=`echo $DEVNAME | cut -d "/" -f 2,3`
    ctrl_name=`echo $DEVNAME | cut -d "/" -f 5`
    OF_WWPN=${ctrl_name%,*}
    OF_WWPN=${OF_WWPN#*@}
    OF_NQN=`echo $ctrl_name | cut -d "=" -f 2`
    # set partition number only if ':' is present
    case "${nsid_part}" in
    *:*)
        part=${nsid_part#*:}
        ;;
    esac
    local dir
    local found=0

    for dir in `$FIND /sys/devices/virtual/nvme-fabrics -name "nvme[0-9]*"`; do
        cd $dir
        if [[ -f $PWD/address ]]; then
            t_wwpn=`$CAT $PWD/address | cut  -f 3 -d "-"`
            t_wwpn="${t_wwpn#0x}"
            t_wwpn="${t_wwpn%,*}"
            h_wwpn=`$CAT $PWD/address | cut  -f 5 -d "-"`
            h_wwpn="${h_wwpn#0x}"
            nqn=`$CAT $PWD/subsysnqn`
            if [[ $t_wwpn = $OF_WWPN ]] && \
                [[ $nqn == $OF_NQN ]]; then
                for f in `$FIND /sys/devices -name "port_name"`; do
                    sys_wwpn=`$CAT $f 2>/dev/null`
                    sys_wwpn="${sys_wwpn#0x}"
                    if [[ $h_wwpn = $sys_wwpn ]] && \
                      [[ "$f" == *"fc_host"* ]]; then
                        f=${f%/*}
                        goto_dir $f "device" 0
                        link=`get_link "device"`
                        cd $link
                        goto_dir $PWD "devspec"
                        if [[ -e $PWD/devspec ]]; then
                            pci_addr=`$CAT $PWD/devspec`
                            pci_addr=${pci_addr#*/}
                        fi
                        if [[ $of_path_addr = $pci_addr ]]; then
                            LOGICAL_DEVNAME="${dir##*/}"
                            break
                        fi
                    fi
                done
            fi
        fi
    done

    local ana_dir
    if [[ -n $LOGICAL_DEVNAME ]] && \
       [[ -n $nsid ]]; then
        for ana_dir in `$FIND /sys/devices/virtual/nvme-fabrics -name "$LOGICAL_DEVNAME"`; do
            cd $ana_dir
            link=`get_link "device"`
            cd $link
            for ana_name in `ls -d $PWD/nvme*`; do
                for dev_name in `$FIND /sys/block -name "${ana_name##*/}n[0-9]*"`; do
                    cd $dev_name
                    local devnsid=`$CAT ./nsid 2>/dev/null`
		    devnsid=$(printf "%x" $devnsid)
                    if [[ $devnsid = $nsid ]]; then
                        found=1
                        NS_ID="${dev_name##*n}"
                        LOGICAL_DEVNAME="${LOGICAL_DEVNAME}n${NS_ID}"
                        break
                    fi
                 done
            done
        done
        
    	 if [[ $found -eq 0 ]]; then
            LOGICAL_DEVNAME=""
         fi
    fi

    if [[ -n $LOGICAL_DEVNAME ]] && \
       [[ -n $part ]]; then
        res=`$FIND /sys/devices/virtual -name ${LOGICAL_DEVNAME}p${part}`
        if [[ ${#res} = 0 ]]; then
            LOGICAL_DEVNAME=''
        else
            LOGICAL_DEVNAME="${LOGICAL_DEVNAME}p${part}"
        fi
    fi
}

#
# Main
#
. $PSERIES_PLATFORM
if [[ $platform = $PLATFORM_POWERNV ]]; then
	echo "$OFPATHNAME: is not supported on the $platform_name platform" 1>&2
	exit 1
fi

if [[ "$#" -eq 0 ]]; then
    usage
    exit 0
fi

# default: convert logical => OFpath
do_of2l=0

# default: do not do alias lookups
do_alias=0

getopt -o "l:Vqh" -l "help,version,quiet" $@ > /dev/null 2>&1
while [[ -n $1 ]]; do
    case "$1" in
	-a)		do_alias=1 ;;

        -l)             do_of2l=1
                        DEVNAME_ARG=$2
                        shift ;;

	-V | --version) show_version
                        exit 0 ;;

        -q | --quiet)   be_quiet=1 ;;

        -h | --help)    usage
                        exit 0 ;;
	*)              DEVNAME_ARG=$1 ;;
    esac

    shift
done

DEVNAME=$DEVNAME_ARG

# double check device name
if [[ -z $DEVNAME ]]; then
    usage
    exit 1
fi

# We need sysfs
if [[ ! -d "/sys" ]]; then
    err $ERR_NO_SYSFS
    exit 1
fi


if [[ $do_of2l = "0" ]]; then
    # logical devname => OF pathname
    logical_to_ofpathname
else
    # OF pathnmae => logical devname
    ofpathname_to_logical
fi

exit 0