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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <fcntl.h>
#include <libdevinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stropts.h>
#include <sys/dkio.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <unistd.h>
#include <kstat.h>
#include <errno.h>
#include <devid.h>
#include <dirent.h>
/* included for uscsi */
#include <strings.h>
#include <sys/stat.h>
#include <sys/scsi/impl/types.h>
#include <sys/scsi/impl/uscsi.h>
#include <sys/scsi/generic/commands.h>
#include <sys/scsi/impl/commands.h>
#include <sys/scsi/generic/mode.h>
#include <sys/byteorder.h>
#include "libdiskmgt.h"
#include "disks_private.h"
#define KSTAT_CLASS_DISK "disk"
#define KSTAT_CLASS_ERROR "device_error"
#define SCSIBUFLEN 0xffff
/* byte get macros */
#define b3(a) (((a)>>24) & 0xFF)
#define b2(a) (((a)>>16) & 0xFF)
#define b1(a) (((a)>>8) & 0xFF)
#define b0(a) (((a)>>0) & 0xFF)
static char *kstat_err_names[] = {
"Soft Errors",
"Hard Errors",
"Transport Errors",
"Media Error",
"Device Not Ready",
"No Device",
"Recoverable",
"Illegal Request",
"Predictive Failure Analysis",
NULL
};
static char *err_attr_names[] = {
DM_NSOFTERRS,
DM_NHARDERRS,
DM_NTRANSERRS,
DM_NMEDIAERRS,
DM_NDNRERRS,
DM_NNODEVERRS,
DM_NRECOVERRS,
DM_NILLREQERRS,
DM_FAILING,
NULL
};
/*
* **************** begin uscsi stuff ****************
*/
#if defined(_BIT_FIELDS_LTOH)
#elif defined(_BIT_FIELDS_HTOL)
#else
#error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
#endif
struct conf_feature {
uchar_t feature[2]; /* common to all */
#if defined(_BIT_FIELDS_LTOH)
uchar_t current : 1;
uchar_t persist : 1;
uchar_t version : 4;
uchar_t reserved: 2;
#else
uchar_t reserved: 2;
uchar_t version : 4;
uchar_t persist : 1;
uchar_t current : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t len;
union features {
struct generic {
uchar_t data[1];
} gen;
uchar_t data[1];
struct profile_list {
uchar_t profile[2];
#if defined(_BIT_FIELDS_LTOH)
uchar_t current_p : 1;
uchar_t reserved1 : 7;
#else
uchar_t reserved1 : 7;
uchar_t current_p : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t reserved2;
} plist[1];
struct core {
uchar_t phys[4];
} core;
struct morphing {
#if defined(_BIT_FIELDS_LTOH)
uchar_t async : 1;
uchar_t reserved1 : 7;
#else
uchar_t reserved1 : 7;
uchar_t async : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t reserved[3];
} morphing;
struct removable {
#if defined(_BIT_FIELDS_LTOH)
uchar_t lock : 1;
uchar_t resv1 : 1;
uchar_t pvnt : 1;
uchar_t eject : 1;
uchar_t resv2 : 1;
uchar_t loading : 3;
#else
uchar_t loading : 3;
uchar_t resv2 : 1;
uchar_t eject : 1;
uchar_t pvnt : 1;
uchar_t resv1 : 1;
uchar_t lock : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t reserved[3];
} removable;
struct random_readable {
uchar_t lbsize[4];
uchar_t blocking[2];
#if defined(_BIT_FIELDS_LTOH)
uchar_t pp : 1;
uchar_t reserved1 : 7;
#else
uchar_t reserved1 : 7;
uchar_t pp : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t reserved;
} rread;
struct cd_read {
#if defined(_BIT_FIELDS_LTOH)
uchar_t cdtext : 1;
uchar_t c2flag : 1;
uchar_t reserved1 : 6;
#else
uchar_t reserved1 : 6;
uchar_t c2flag : 1;
uchar_t cdtext : 1;
#endif /* _BIT_FIELDS_LTOH */
} cdread;
struct cd_audio {
#if defined(_BIT_FIELDS_LTOH)
uchar_t sv : 1;
uchar_t scm : 1;
uchar_t scan : 1;
uchar_t resv : 5;
#else
uchar_t resv : 5;
uchar_t scan : 1;
uchar_t scm : 1;
uchar_t sv : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t reserved;
uchar_t numlevels[2];
} audio;
struct dvd_css {
uchar_t reserved[3];
uchar_t version;
} dvdcss;
} features;
};
#define PROF_NON_REMOVABLE 0x0001
#define PROF_REMOVABLE 0x0002
#define PROF_MAGNETO_OPTICAL 0x0003
#define PROF_OPTICAL_WO 0x0004
#define PROF_OPTICAL_ASMO 0x0005
#define PROF_CDROM 0x0008
#define PROF_CDR 0x0009
#define PROF_CDRW 0x000a
#define PROF_DVDROM 0x0010
#define PROF_DVDR 0x0011
#define PROF_DVDRAM 0x0012
#define PROF_DVDRW_REST 0x0013
#define PROF_DVDRW_SEQ 0x0014
#define PROF_DVDRW 0x001a
#define PROF_DDCD_ROM 0x0020
#define PROF_DDCD_R 0x0021
#define PROF_DDCD_RW 0x0022
#define PROF_NON_CONFORMING 0xffff
struct get_configuration {
uchar_t len[4];
uchar_t reserved[2];
uchar_t curprof[2];
struct conf_feature feature;
};
struct capabilities {
#if defined(_BIT_FIELDS_LTOH)
uchar_t pagecode : 6;
uchar_t resv1 : 1;
uchar_t ps : 1;
#else
uchar_t ps : 1;
uchar_t resv1 : 1;
uchar_t pagecode : 6;
#endif /* _BIT_FIELDS_LTOH */
uchar_t pagelen;
#if defined(_BIT_FIELDS_LTOH)
/* read capabilities */
uchar_t cdr_read : 1;
uchar_t cdrw_read : 1;
uchar_t method2 : 1;
uchar_t dvdrom_read : 1;
uchar_t dvdr_read : 1;
uchar_t dvdram_read : 1;
uchar_t resv2 : 2;
#else
uchar_t resv2 : 2;
uchar_t dvdram_read : 1;
uchar_t dvdr_read : 1;
uchar_t dvdrom_read : 1;
uchar_t method2 : 1;
uchar_t cdrw_read : 1;
uchar_t cdr_read : 1;
#endif /* _BIT_FIELDS_LTOH */
#if defined(_BIT_FIELDS_LTOH)
/* write capabilities */
uchar_t cdr_write : 1;
uchar_t cdrw_write : 1;
uchar_t testwrite : 1;
uchar_t resv3 : 1;
uchar_t dvdr_write : 1;
uchar_t dvdram_write : 1;
uchar_t resv4 : 2;
#else
/* write capabilities */
uchar_t resv4 : 2;
uchar_t dvdram_write : 1;
uchar_t dvdr_write : 1;
uchar_t resv3 : 1;
uchar_t testwrite : 1;
uchar_t cdrw_write : 1;
uchar_t cdr_write : 1;
#endif /* _BIT_FIELDS_LTOH */
uchar_t misc0;
uchar_t misc1;
uchar_t misc2;
uchar_t misc3;
uchar_t obsolete0[2];
uchar_t numvlevels[2];
uchar_t bufsize[2];
uchar_t obsolete1[4];
uchar_t resv5;
uchar_t misc4;
uchar_t obsolete2;
uchar_t copymgt[2];
/* there is more to this page, but nothing we care about */
};
struct mode_header_g2 {
uchar_t modelen[2];
uchar_t obsolete;
uchar_t reserved[3];
uchar_t desclen[2];
};
/*
* Mode sense/select page header information
*/
struct scsi_ms_header {
struct mode_header mode_header;
struct block_descriptor block_descriptor;
};
#define MODESENSE_PAGE_LEN(p) (((int)((struct mode_page *)p)->length) + \
sizeof (struct mode_page))
#define MODE_SENSE_PC_CURRENT (0 << 6)
#define MODE_SENSE_PC_DEFAULT (2 << 6)
#define MODE_SENSE_PC_SAVED (3 << 6)
#define MAX_MODE_SENSE_SIZE 255
#define IMPOSSIBLE_SCSI_STATUS 0xff
/*
* ********** end of uscsi stuff ************
*/
static descriptor_t **apply_filter(descriptor_t **drives, int filter[],
int *errp);
static int check_atapi(int fd);
static int conv_drive_type(uint_t drive_type);
static uint64_t convnum(uchar_t *nptr, int len);
static void fill_command_g1(struct uscsi_cmd *cmd,
union scsi_cdb *cdb, caddr_t buff, int blen);
static void fill_general_page_cdb_g1(union scsi_cdb *cdb,
int command, int lun, uchar_t c0, uchar_t c1);
static void fill_mode_page_cdb(union scsi_cdb *cdb, int page);
static descriptor_t **get_assoc_alias(disk_t *diskp, int *errp);
static descriptor_t **get_assoc_controllers(descriptor_t *dp, int *errp);
static descriptor_t **get_assoc_paths(descriptor_t *dp, int *errp);
static int get_attrs(disk_t *diskp, int fd, char *opath,
nvlist_t *nvp);
static int get_cdrom_drvtype(int fd);
static int get_disk_kstats(kstat_ctl_t *kc, char *diskname,
char *classname, nvlist_t *stats);
static void get_drive_type(disk_t *dp, int fd);
static int get_err_kstats(kstat_ctl_t *kc, char *diskname,
nvlist_t *stats);
static int get_io_kstats(kstat_ctl_t *kc, char *diskname,
nvlist_t *stats);
static int get_kstat_vals(kstat_t *ksp, nvlist_t *stats);
static char *get_err_attr_name(char *kstat_name);
static int get_rpm(disk_t *dp, int fd);
static int update_stat64(nvlist_t *stats, char *attr,
uint64_t value);
static int update_stat32(nvlist_t *stats, char *attr,
uint32_t value);
static int uscsi_mode_sense(int fd, int page_code,
int page_control, caddr_t page_data, int page_size,
struct scsi_ms_header *header);
descriptor_t **
drive_get_assoc_descriptors(descriptor_t *dp, dm_desc_type_t type,
int *errp)
{
switch (type) {
case DM_CONTROLLER:
return (get_assoc_controllers(dp, errp));
case DM_PATH:
return (get_assoc_paths(dp, errp));
case DM_ALIAS:
return (get_assoc_alias(dp->p.disk, errp));
case DM_MEDIA:
return (media_get_assocs(dp, errp));
}
*errp = EINVAL;
return (NULL);
}
/*
* Get the drive descriptors for the given media/alias/devpath.
*/
descriptor_t **
drive_get_assocs(descriptor_t *desc, int *errp)
{
descriptor_t **drives;
/* at most one drive is associated with these descriptors */
drives = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
if (drives == NULL) {
*errp = ENOMEM;
return (NULL);
}
drives[0] = cache_get_desc(DM_DRIVE, desc->p.disk, NULL, NULL, errp);
if (*errp != 0) {
cache_free_descriptors(drives);
return (NULL);
}
drives[1] = NULL;
return (drives);
}
nvlist_t *
drive_get_attributes(descriptor_t *dp, int *errp)
{
nvlist_t *attrs = NULL;
int fd;
char opath[MAXPATHLEN];
if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
*errp = ENOMEM;
return (NULL);
}
opath[0] = 0;
fd = drive_open_disk(dp->p.disk, opath, sizeof (opath));
if ((*errp = get_attrs(dp->p.disk, fd, opath, attrs)) != 0) {
nvlist_free(attrs);
attrs = NULL;
}
if (fd >= 0) {
(void) close(fd);
}
return (attrs);
}
/*
* Check if we have the drive in our list, based upon the device id.
* We got the device id from the dev tree walk. This is encoded
* using devid_str_encode(3DEVID). In order to check the device ids we need
* to use the devid_compare(3DEVID) function, so we need to decode the
* string representation of the device id.
*/
descriptor_t *
drive_get_descriptor_by_name(char *name, int *errp)
{
ddi_devid_t devid;
descriptor_t **drives;
descriptor_t *drive = NULL;
int i;
if (name == NULL || devid_str_decode(name, &devid, NULL) != 0) {
*errp = EINVAL;
return (NULL);
}
drives = cache_get_descriptors(DM_DRIVE, errp);
if (*errp != 0) {
devid_free(devid);
return (NULL);
}
/*
* We have to loop through all of them, freeing the ones we don't
* want. Once drive is set, we don't need to compare any more.
*/
for (i = 0; drives[i]; i++) {
if (drive == NULL && drives[i]->p.disk->devid != NULL &&
devid_compare(devid, drives[i]->p.disk->devid) == 0) {
drive = drives[i];
} else {
/* clean up the unused descriptor */
cache_free_descriptor(drives[i]);
}
}
free(drives);
devid_free(devid);
if (drive == NULL) {
*errp = ENODEV;
}
return (drive);
}
descriptor_t **
drive_get_descriptors(int filter[], int *errp)
{
descriptor_t **drives;
drives = cache_get_descriptors(DM_DRIVE, errp);
if (*errp != 0) {
return (NULL);
}
if (filter != NULL && filter[0] != DM_FILTER_END) {
descriptor_t **found;
found = apply_filter(drives, filter, errp);
if (*errp != 0) {
drives = NULL;
} else {
drives = found;
}
}
return (drives);
}
char *
drive_get_name(descriptor_t *dp)
{
return (dp->p.disk->device_id);
}
nvlist_t *
drive_get_stats(descriptor_t *dp, int stat_type, int *errp)
{
disk_t *diskp;
nvlist_t *stats;
diskp = dp->p.disk;
if (nvlist_alloc(&stats, NVATTRS, 0) != 0) {
*errp = ENOMEM;
return (NULL);
}
if (stat_type == DM_DRV_STAT_PERFORMANCE ||
stat_type == DM_DRV_STAT_DIAGNOSTIC) {
alias_t *ap;
kstat_ctl_t *kc;
ap = diskp->aliases;
if (ap == NULL || ap->kstat_name == NULL) {
nvlist_free(stats);
*errp = EACCES;
return (NULL);
}
if ((kc = kstat_open()) == NULL) {
nvlist_free(stats);
*errp = EACCES;
return (NULL);
}
while (ap != NULL) {
int status;
if (ap->kstat_name == NULL) {
continue;
}
if (stat_type == DM_DRV_STAT_PERFORMANCE) {
status = get_io_kstats(kc, ap->kstat_name, stats);
} else {
status = get_err_kstats(kc, ap->kstat_name, stats);
}
if (status != 0) {
nvlist_free(stats);
(void) kstat_close(kc);
*errp = ENOMEM;
return (NULL);
}
ap = ap->next;
}
(void) kstat_close(kc);
*errp = 0;
return (stats);
}
if (stat_type == DM_DRV_STAT_TEMPERATURE) {
int fd;
if ((fd = drive_open_disk(diskp, NULL, 0)) >= 0) {
struct dk_temperature temp;
if (ioctl(fd, DKIOCGTEMPERATURE, &temp) >= 0) {
if (nvlist_add_uint32(stats, DM_TEMPERATURE,
temp.dkt_cur_temp) != 0) {
*errp = ENOMEM;
nvlist_free(stats);
return (NULL);
}
} else {
*errp = errno;
nvlist_free(stats);
return (NULL);
}
(void) close(fd);
} else {
*errp = errno;
nvlist_free(stats);
return (NULL);
}
*errp = 0;
return (stats);
}
nvlist_free(stats);
*errp = EINVAL;
return (NULL);
}
int
drive_make_descriptors()
{
int error;
disk_t *dp;
dp = cache_get_disklist();
while (dp != NULL) {
cache_load_desc(DM_DRIVE, dp, NULL, NULL, &error);
if (error != 0) {
return (error);
}
dp = dp->next;
}
return (0);
}
/*
* This function opens the disk generically (any slice).
*/
int
drive_open_disk(disk_t *diskp, char *opath, int len)
{
/*
* Just open the first devpath.
*/
if (diskp->aliases != NULL && diskp->aliases->devpaths != NULL) {
if (opath != NULL) {
(void) strlcpy(opath, diskp->aliases->devpaths->devpath, len);
}
return (open(diskp->aliases->devpaths->devpath, O_RDONLY|O_NDELAY));
}
return (-1);
}
static descriptor_t **
apply_filter(descriptor_t **drives, int filter[], int *errp)
{
int i;
descriptor_t **found;
int cnt;
int pos;
/* count the number of drives in the snapshot */
for (cnt = 0; drives[cnt]; cnt++);
found = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
if (found == NULL) {
*errp = ENOMEM;
cache_free_descriptors(drives);
return (NULL);
}
pos = 0;
for (i = 0; drives[i]; i++) {
int j;
int match;
/* Make sure the drive type is set */
get_drive_type(drives[i]->p.disk, -1);
match = 0;
for (j = 0; filter[j] != DM_FILTER_END; j++) {
if (drives[i]->p.disk->drv_type == filter[j]) {
found[pos++] = drives[i];
match = 1;
break;
}
}
if (!match) {
cache_free_descriptor(drives[i]);
}
}
found[pos] = NULL;
free(drives);
*errp = 0;
return (found);
}
static int
conv_drive_type(uint_t drive_type)
{
switch (drive_type) {
case DK_UNKNOWN:
return (DM_DT_UNKNOWN);
case DK_MO_ERASABLE:
return (DM_DT_MO_ERASABLE);
case DK_MO_WRITEONCE:
return (DM_DT_MO_WRITEONCE);
case DK_AS_MO:
return (DM_DT_AS_MO);
case DK_CDROM:
return (DM_DT_CDROM);
case DK_CDR:
return (DM_DT_CDR);
case DK_CDRW:
return (DM_DT_CDRW);
case DK_DVDROM:
return (DM_DT_DVDROM);
case DK_DVDR:
return (DM_DT_DVDR);
case DK_DVDRAM:
return (DM_DT_DVDRAM);
case DK_FIXED_DISK:
return (DM_DT_FIXED);
case DK_FLOPPY:
return (DM_DT_FLOPPY);
case DK_ZIP:
return (DM_DT_ZIP);
case DK_JAZ:
return (DM_DT_JAZ);
default:
return (DM_DT_UNKNOWN);
}
}
static descriptor_t **
get_assoc_alias(disk_t *diskp, int *errp)
{
alias_t *aliasp;
uint_t cnt;
descriptor_t **out_array;
int pos;
*errp = 0;
aliasp = diskp->aliases;
cnt = 0;
while (aliasp != NULL) {
if (aliasp->alias != NULL) {
cnt++;
}
aliasp = aliasp->next;
}
/* set up the new array */
out_array = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t));
if (out_array == NULL) {
*errp = ENOMEM;
return (NULL);
}
aliasp = diskp->aliases;
pos = 0;
while (aliasp != NULL) {
if (aliasp->alias != NULL) {
out_array[pos++] = cache_get_desc(DM_ALIAS, diskp,
aliasp->alias, NULL, errp);
if (*errp != 0) {
cache_free_descriptors(out_array);
return (NULL);
}
}
aliasp = aliasp->next;
}
out_array[pos] = NULL;
return (out_array);
}
static descriptor_t **
get_assoc_controllers(descriptor_t *dp, int *errp)
{
disk_t *diskp;
int cnt;
descriptor_t **controllers;
int i;
diskp = dp->p.disk;
/* Count how many we have. */
for (cnt = 0; diskp->controllers[cnt]; cnt++);
/* make the snapshot */
controllers = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
if (controllers == NULL) {
*errp = ENOMEM;
return (NULL);
}
for (i = 0; diskp->controllers[i]; i++) {
controllers[i] = cache_get_desc(DM_CONTROLLER,
diskp->controllers[i], NULL, NULL, errp);
if (*errp != 0) {
cache_free_descriptors(controllers);
return (NULL);
}
}
controllers[i] = NULL;
*errp = 0;
return (controllers);
}
static descriptor_t **
get_assoc_paths(descriptor_t *dp, int *errp)
{
path_t **pp;
int cnt;
descriptor_t **paths;
int i;
pp = dp->p.disk->paths;
/* Count how many we have. */
cnt = 0;
if (pp != NULL) {
for (; pp[cnt]; cnt++);
}
/* make the snapshot */
paths = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
if (paths == NULL) {
*errp = ENOMEM;
return (NULL);
}
/*
* We fill in the name field of the descriptor with the device_id
* when we deal with path descriptors originating from a drive.
* In that way we can use the device id within the path code to
* lookup the path state for this drive.
*/
for (i = 0; i < cnt; i++) {
paths[i] = cache_get_desc(DM_PATH, pp[i], dp->p.disk->device_id,
NULL, errp);
if (*errp != 0) {
cache_free_descriptors(paths);
return (NULL);
}
}
paths[i] = NULL;
*errp = 0;
return (paths);
}
static int
get_attrs(disk_t *diskp, int fd, char *opath, nvlist_t *attrs)
{
if (diskp->removable) {
struct dk_minfo minfo;
if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
return (ENOMEM);
}
/* Make sure media is inserted and spun up. */
if (fd >= 0 && media_read_info(fd, &minfo)) {
if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
return (ENOMEM);
}
}
/* can't tell diff between dead & no media on removable drives */
if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
return (ENOMEM);
}
get_drive_type(diskp, fd);
} else {
struct dk_minfo minfo;
/* check if the fixed drive is up or not */
if (fd >= 0 && media_read_info(fd, &minfo)) {
if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
return (ENOMEM);
}
} else {
if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_DOWN) != 0) {
return (ENOMEM);
}
}
get_drive_type(diskp, fd);
}
if (nvlist_add_uint32(attrs, DM_DRVTYPE, diskp->drv_type) != 0) {
return (ENOMEM);
}
if (diskp->product_id != NULL) {
if (nvlist_add_string(attrs, DM_PRODUCT_ID, diskp->product_id)
!= 0) {
return (ENOMEM);
}
}
if (diskp->vendor_id != NULL) {
if (nvlist_add_string(attrs, DM_VENDOR_ID, diskp->vendor_id) != 0) {
return (ENOMEM);
}
}
if (diskp->sync_speed != -1) {
if (nvlist_add_uint32(attrs, DM_SYNC_SPEED, diskp->sync_speed)
!= 0) {
return (ENOMEM);
}
}
if (diskp->wide == 1) {
if (nvlist_add_boolean(attrs, DM_WIDE) != 0) {
return (ENOMEM);
}
}
if (diskp->rpm == 0) {
diskp->rpm = get_rpm(diskp, fd);
}
if (diskp->rpm > 0) {
if (nvlist_add_uint32(attrs, DM_RPM, diskp->rpm) != 0) {
return (ENOMEM);
}
}
if (diskp->aliases != NULL && diskp->aliases->cluster) {
if (nvlist_add_boolean(attrs, DM_CLUSTERED) != 0) {
return (ENOMEM);
}
}
if (strlen(opath) > 0) {
if (nvlist_add_string(attrs, DM_OPATH, opath) != 0) {
return (ENOMEM);
}
}
return (0);
}
static int
get_disk_kstats(kstat_ctl_t *kc, char *diskname, char *classname,
nvlist_t *stats)
{
kstat_t *ksp;
size_t class_len;
int err = 0;
class_len = strlen(classname);
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
if (strncmp(ksp->ks_class, classname, class_len) == 0) {
char kstat_name[KSTAT_STRLEN];
char *dname = kstat_name;
char *ename = ksp->ks_name;
/* names are format: "sd0,err" - copy chars up to comma */
while (*ename && *ename != ',') {
*dname++ = *ename++;
}
*dname = NULL;
if (libdiskmgt_str_eq(diskname, kstat_name)) {
(void) kstat_read(kc, ksp, NULL);
err = get_kstat_vals(ksp, stats);
break;
}
}
}
return (err);
}
/*
* Getting the drive type depends on if the dev tree walk indicated that the
* drive was a CD-ROM or not. The kernal lumps all of the removable multi-media
* drives (e.g. CD, DVD, MO, etc.) together as CD-ROMS, so we need to use
* a uscsi cmd to check the drive type.
*/
static void
get_drive_type(disk_t *dp, int fd)
{
if (dp->drv_type == DM_DT_UNKNOWN) {
int opened_here = 0;
/* We may have already opened the device. */
if (fd < 0) {
fd = drive_open_disk(dp, NULL, 0);
opened_here = 1;
}
if (fd >= 0) {
if (dp->cd_rom) {
/* use uscsi to determine drive type */
dp->drv_type = get_cdrom_drvtype(fd);
/* if uscsi fails, just call it a cd-rom */
if (dp->drv_type == DM_DT_UNKNOWN) {
dp->drv_type = DM_DT_CDROM;
}
} else {
struct dk_minfo minfo;
if (media_read_info(fd, &minfo)) {
dp->drv_type = conv_drive_type(minfo.dki_media_type);
}
}
if (opened_here) {
(void) close(fd);
}
} else {
/* couldn't open */
if (dp->cd_rom) {
dp->drv_type = DM_DT_CDROM;
}
}
}
}
static char *
get_err_attr_name(char *kstat_name)
{
int i;
for (i = 0; kstat_err_names[i] != NULL; i++) {
if (libdiskmgt_str_eq(kstat_name, kstat_err_names[i])) {
return (err_attr_names[i]);
}
}
return (NULL);
}
static int
get_err_kstats(kstat_ctl_t *kc, char *diskname, nvlist_t *stats)
{
return (get_disk_kstats(kc, diskname, KSTAT_CLASS_ERROR, stats));
}
static int
get_io_kstats(kstat_ctl_t *kc, char *diskname, nvlist_t *stats)
{
return (get_disk_kstats(kc, diskname, KSTAT_CLASS_DISK, stats));
}
static int
get_kstat_vals(kstat_t *ksp, nvlist_t *stats)
{
if (ksp->ks_type == KSTAT_TYPE_IO) {
kstat_io_t *kiop;
kiop = KSTAT_IO_PTR(ksp);
/* see sys/kstat.h kstat_io_t struct for more fields */
if (update_stat64(stats, DM_NBYTESREAD, kiop->nread) != 0) {
return (ENOMEM);
}
if (update_stat64(stats, DM_NBYTESWRITTEN, kiop->nwritten) != 0) {
return (ENOMEM);
}
if (update_stat64(stats, DM_NREADOPS, kiop->reads) != 0) {
return (ENOMEM);
}
if (update_stat64(stats, DM_NWRITEOPS, kiop->writes) != 0) {
return (ENOMEM);
}
} else if (ksp->ks_type == KSTAT_TYPE_NAMED) {
kstat_named_t *knp;
int i;
knp = KSTAT_NAMED_PTR(ksp);
for (i = 0; i < ksp->ks_ndata; i++) {
char *attr_name;
if (knp[i].name[0] == 0)
continue;
if ((attr_name = get_err_attr_name(knp[i].name)) == NULL) {
continue;
}
switch (knp[i].data_type) {
case KSTAT_DATA_UINT32:
if (update_stat32(stats, attr_name, knp[i].value.ui32)
!= 0) {
return (ENOMEM);
}
break;
default:
/* Right now all of the error types are uint32 */
break;
}
}
}
return (0);
}
static int
update_stat32(nvlist_t *stats, char *attr, uint32_t value)
{
int32_t currval;
if (nvlist_lookup_int32(stats, attr, &currval) == 0) {
value += currval;
}
return (nvlist_add_uint32(stats, attr, value));
}
/*
* There can be more than one kstat value when we have multi-path drives
* that are not under mpxio (since there is more than one kstat name for
* the drive in this case). So, we may have merge all of the kstat values
* to give an accurate set of stats for the drive.
*/
static int
update_stat64(nvlist_t *stats, char *attr, uint64_t value)
{
int64_t currval;
if (nvlist_lookup_int64(stats, attr, &currval) == 0) {
value += currval;
}
return (nvlist_add_uint64(stats, attr, value));
}
/*
* uscsi function to get the rpm of the drive
*/
static int
get_rpm(disk_t *dp, int fd)
{
int opened_here = 0;
int rpm = -1;
/* We may have already opened the device. */
if (fd < 0) {
fd = drive_open_disk(dp, NULL, 0);
opened_here = 1;
}
if (fd >= 0) {
int status;
struct mode_geometry *page4;
struct scsi_ms_header header;
union {
struct mode_geometry page4;
char rawbuf[MAX_MODE_SENSE_SIZE];
} u_page4;
page4 = &u_page4.page4;
(void) memset(&u_page4, 0, sizeof (u_page4));
status = uscsi_mode_sense(fd, DAD_MODE_GEOMETRY,
MODE_SENSE_PC_DEFAULT, (caddr_t)page4, MAX_MODE_SENSE_SIZE,
&header);
if (status) {
status = uscsi_mode_sense(fd, DAD_MODE_GEOMETRY,
MODE_SENSE_PC_SAVED, (caddr_t)page4, MAX_MODE_SENSE_SIZE,
&header);
}
if (status) {
status = uscsi_mode_sense(fd, DAD_MODE_GEOMETRY,
MODE_SENSE_PC_CURRENT, (caddr_t)page4, MAX_MODE_SENSE_SIZE,
&header);
}
if (!status) {
#ifdef _LITTLE_ENDIAN
page4->rpm = ntohs(page4->rpm);
#endif /* _LITTLE_ENDIAN */
rpm = page4->rpm;
}
if (opened_here) {
(void) close(fd);
}
}
return (rpm);
}
/*
* ******** the rest of this is uscsi stuff for the drv type ********
*/
/*
* We try a get_configuration uscsi cmd. If that fails, try a
* atapi_capabilities cmd. If both fail then this is an older CD-ROM.
*/
static int
get_cdrom_drvtype(int fd)
{
union scsi_cdb cdb;
struct uscsi_cmd cmd;
uchar_t buff[SCSIBUFLEN];
fill_general_page_cdb_g1(&cdb, SCMD_GET_CONFIGURATION, 0,
b0(sizeof (buff)), b1(sizeof (buff)));
fill_command_g1(&cmd, &cdb, (caddr_t)buff, sizeof (buff));
if (ioctl(fd, USCSICMD, &cmd) >= 0) {
struct get_configuration *config;
struct conf_feature *feature;
int flen;
/* The first profile is the preferred one for the drive. */
config = (struct get_configuration *)buff;
feature = &config->feature;
flen = feature->len / sizeof (struct profile_list);
if (flen > 0) {
int prof_num;
prof_num = (int)convnum(feature->features.plist[0].profile, 2);
if (dm_debug > 1) {
(void) fprintf(stderr, "INFO: uscsi get_configuration %d\n",
prof_num);
}
switch (prof_num) {
case PROF_MAGNETO_OPTICAL:
return (DM_DT_MO_ERASABLE);
case PROF_OPTICAL_WO:
return (DM_DT_MO_WRITEONCE);
case PROF_OPTICAL_ASMO:
return (DM_DT_AS_MO);
case PROF_CDROM:
return (DM_DT_CDROM);
case PROF_CDR:
return (DM_DT_CDR);
case PROF_CDRW:
return (DM_DT_CDRW);
case PROF_DVDROM:
return (DM_DT_DVDROM);
case PROF_DVDRAM:
return (DM_DT_DVDRAM);
case PROF_DVDRW_REST:
return (DM_DT_DVDRW);
case PROF_DVDRW_SEQ:
return (DM_DT_DVDRW);
case PROF_DVDRW:
return (DM_DT_DVDRW);
case PROF_DDCD_ROM:
return (DM_DT_DDCDROM);
case PROF_DDCD_R:
return (DM_DT_DDCDR);
case PROF_DDCD_RW:
return (DM_DT_DDCDRW);
}
}
}
/* see if the atapi capabilities give anything */
return (check_atapi(fd));
}
static int
check_atapi(int fd)
{
union scsi_cdb cdb;
struct uscsi_cmd cmd;
uchar_t buff[SCSIBUFLEN];
fill_mode_page_cdb(&cdb, ATAPI_CAPABILITIES);
fill_command_g1(&cmd, &cdb, (caddr_t)buff, sizeof (buff));
if (ioctl(fd, USCSICMD, &cmd) >= 0) {
int bdesclen;
struct capabilities *cap;
struct mode_header_g2 *mode;
mode = (struct mode_header_g2 *)buff;
bdesclen = (int)convnum(mode->desclen, 2);
cap = (struct capabilities *)
&buff[sizeof (struct mode_header_g2) + bdesclen];
if (dm_debug > 1) {
(void) fprintf(stderr, "INFO: uscsi atapi capabilities\n");
}
/* These are in order of how we want to report the drv type. */
if (cap->dvdram_write) {
return (DM_DT_DVDRAM);
}
if (cap->dvdr_write) {
return (DM_DT_DVDR);
}
if (cap->dvdrom_read) {
return (DM_DT_DVDROM);
}
if (cap->cdrw_write) {
return (DM_DT_CDRW);
}
if (cap->cdr_write) {
return (DM_DT_CDR);
}
if (cap->cdr_read) {
return (DM_DT_CDROM);
}
}
/* everything failed, so this is an older CD-ROM */
if (dm_debug > 1) {
(void) fprintf(stderr, "INFO: uscsi failed\n");
}
return (DM_DT_CDROM);
}
static uint64_t
convnum(uchar_t *nptr, int len)
{
uint64_t value;
for (value = 0; len > 0; len--, nptr++)
value = (value << 8) | *nptr;
return (value);
}
static void
fill_command_g1(struct uscsi_cmd *cmd, union scsi_cdb *cdb,
caddr_t buff, int blen)
{
bzero((caddr_t)cmd, sizeof (struct uscsi_cmd));
bzero(buff, blen);
cmd->uscsi_cdb = (caddr_t)cdb;
cmd->uscsi_cdblen = CDB_GROUP1;
cmd->uscsi_bufaddr = buff;
cmd->uscsi_buflen = blen;
cmd->uscsi_flags = USCSI_DIAGNOSE|USCSI_ISOLATE|USCSI_READ;
}
static void
fill_general_page_cdb_g1(union scsi_cdb *cdb, int command, int lun,
uchar_t c0, uchar_t c1)
{
bzero((caddr_t)cdb, sizeof (union scsi_cdb));
cdb->scc_cmd = command;
cdb->scc_lun = lun;
cdb->g1_count0 = c0; /* max length for page */
cdb->g1_count1 = c1; /* max length for page */
}
static void
fill_mode_page_cdb(union scsi_cdb *cdb, int page)
{
/* group 1 mode page */
bzero((caddr_t)cdb, sizeof (union scsi_cdb));
cdb->scc_cmd = SCMD_MODE_SENSE_G1;
cdb->g1_count0 = 0xff; /* max length for mode page */
cdb->g1_count1 = 0xff; /* max length for mode page */
cdb->g1_addr3 = page;
}
static int
uscsi_mode_sense(int fd, int page_code, int page_control, caddr_t page_data,
int page_size, struct scsi_ms_header *header)
{
caddr_t mode_sense_buf;
struct mode_header *hdr;
struct mode_page *pg;
int nbytes;
struct uscsi_cmd ucmd;
union scsi_cdb cdb;
int status;
int maximum;
char rqbuf[255];
/*
* Allocate a buffer for the mode sense headers
* and mode sense data itself.
*/
nbytes = sizeof (struct block_descriptor) +
sizeof (struct mode_header) + page_size;
nbytes = page_size;
if ((mode_sense_buf = malloc((uint_t)nbytes)) == NULL) {
return (-1);
}
/*
* Build and execute the uscsi ioctl
*/
(void) memset(mode_sense_buf, 0, nbytes);
(void) memset((char *)&ucmd, 0, sizeof (ucmd));
(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
cdb.scc_cmd = SCMD_MODE_SENSE;
FORMG0COUNT(&cdb, (uchar_t)nbytes);
cdb.cdb_opaque[2] = page_control | page_code;
ucmd.uscsi_cdb = (caddr_t)&cdb;
ucmd.uscsi_cdblen = CDB_GROUP0;
ucmd.uscsi_bufaddr = mode_sense_buf;
ucmd.uscsi_buflen = nbytes;
ucmd.uscsi_flags |= USCSI_SILENT;
ucmd.uscsi_flags |= USCSI_READ;
ucmd.uscsi_timeout = 30;
ucmd.uscsi_flags |= USCSI_RQENABLE;
if (ucmd.uscsi_rqbuf == NULL) {
ucmd.uscsi_rqbuf = rqbuf;
ucmd.uscsi_rqlen = sizeof (rqbuf);
ucmd.uscsi_rqresid = sizeof (rqbuf);
}
ucmd.uscsi_rqstatus = IMPOSSIBLE_SCSI_STATUS;
status = ioctl(fd, USCSICMD, &ucmd);
if (status || ucmd.uscsi_status != 0) {
free(mode_sense_buf);
return (-1);
}
/*
* Verify that the returned data looks reasonabled,
* find the actual page data, and copy it into the
* user's buffer. Copy the mode_header and block_descriptor
* into the header structure, which can then be used to
* return the same data to the drive when issuing a mode select.
*/
hdr = (struct mode_header *)mode_sense_buf;
(void) memset((caddr_t)header, 0, sizeof (struct scsi_ms_header));
if (hdr->bdesc_length != sizeof (struct block_descriptor) &&
hdr->bdesc_length != 0) {
free(mode_sense_buf);
return (-1);
}
(void) memcpy((caddr_t)header, mode_sense_buf,
(int) (sizeof (struct mode_header) + hdr->bdesc_length));
pg = (struct mode_page *)((ulong_t)mode_sense_buf +
sizeof (struct mode_header) + hdr->bdesc_length);
if (pg->code != page_code) {
free(mode_sense_buf);
return (-1);
}
/*
* Accept up to "page_size" bytes of mode sense data.
* This allows us to accept both CCS and SCSI-2
* structures, as long as we request the greater
* of the two.
*/
maximum = page_size - sizeof (struct mode_page) - hdr->bdesc_length;
if (((int)pg->length) > maximum) {
free(mode_sense_buf);
return (-1);
}
(void) memcpy(page_data, (caddr_t)pg, MODESENSE_PAGE_LEN(pg));
free(mode_sense_buf);
return (0);
}
|