summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/libdiskstatus/common/ds_scsi.c
blob: 2f7e6c63e5293de4904de882b711c5100e03313a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
/*
 * 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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
 */

#include <assert.h>
#include <errno.h>
#include <libdiskstatus.h>
#include <limits.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/fm/io/scsi.h>

#include "ds_scsi.h"
#include "ds_scsi_sim.h"
#include "ds_scsi_uscsi.h"

typedef struct ds_scsi_info {
	disk_status_t		*si_dsp;
	void			*si_sim;
	int			si_cdblen;
	int			si_supp_mode;
	int			si_supp_log;
	int			si_extensions;
	int			si_reftemp;
	scsi_ms_hdrs_t		si_hdrs;
	scsi_ie_page_t		si_iec_current;
	scsi_ie_page_t		si_iec_changeable;
	nvlist_t		*si_state_modepage;
	nvlist_t		*si_state_logpage;
	nvlist_t		*si_state_iec;
} ds_scsi_info_t;

#define	scsi_set_errno(sip, errno)	(ds_set_errno((sip)->si_dsp, (errno)))

/*
 * Table to validate log pages
 */
typedef int (*logpage_validation_fn_t)(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int, nvlist_t *);
typedef int (*logpage_analyze_fn_t)(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int);

typedef struct logpage_validation_entry {
	uchar_t			ve_code;
	int			ve_supported;
	const char		*ve_desc;
	logpage_validation_fn_t	ve_validate;
	logpage_analyze_fn_t	ve_analyze;
} logpage_validation_entry_t;

static int logpage_ie_verify(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int, nvlist_t *);
static int logpage_temp_verify(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int, nvlist_t *);
static int logpage_selftest_verify(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int, nvlist_t *);
static int logpage_ssm_verify(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int, nvlist_t *);

static int logpage_ie_analyze(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int);
static int logpage_temp_analyze(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int);
static int logpage_selftest_analyze(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int);
static int logpage_ssm_analyze(ds_scsi_info_t *,
    scsi_log_parameter_header_t *, int);

static struct logpage_validation_entry log_validation[] = {
	{ LOGPAGE_IE,		LOGPAGE_SUPP_IE,
	    "informational-exceptions",
	    logpage_ie_verify,	logpage_ie_analyze },
	{ LOGPAGE_TEMP,		LOGPAGE_SUPP_TEMP,
	    "temperature",
	    logpage_temp_verify, logpage_temp_analyze },
	{ LOGPAGE_SELFTEST,	LOGPAGE_SUPP_SELFTEST,
	    "self-test",
	    logpage_selftest_verify, logpage_selftest_analyze },
	{ LOGPAGE_SSM,		LOGPAGE_SUPP_SSM,
	    FM_EREPORT_SCSI_SSMWEAROUT,
	    logpage_ssm_verify, logpage_ssm_analyze }
};

#define	NLOG_VALIDATION	(sizeof (log_validation) / sizeof (log_validation[0]))

/*
 * Given an extended sense page, retrieves the sense key, as well as the
 * additional sense code information.
 */
static void
scsi_translate_error(struct scsi_extended_sense *rq, uint_t *skeyp,
    uint_t *ascp, uint_t *ascqp)
{
	struct scsi_descr_sense_hdr *sdsp =
	    (struct scsi_descr_sense_hdr *)rq;

	*skeyp = rq->es_key;

	/*
	 * Get asc, ascq and info field from sense data.  There are two
	 * possible formats (fixed sense data and descriptor sense data)
	 * depending on the value of es_code.
	 */
	switch (rq->es_code) {
	case CODE_FMT_DESCR_CURRENT:
	case CODE_FMT_DESCR_DEFERRED:

		*ascp = sdsp->ds_add_code;
		*ascqp = sdsp->ds_qual_code;
		break;

	case CODE_FMT_FIXED_CURRENT:
	case CODE_FMT_FIXED_DEFERRED:
	default:

		if (rq->es_add_len >= 6) {
			*ascp = rq->es_add_code;
			*ascqp = rq->es_qual_code;
		} else {
			*ascp = 0xff;
			*ascqp = 0xff;
		}
		break;
	}
}

/*
 * Routines built atop the bare uscsi commands, which take into account the
 * command length, automatically translate any scsi errors, and transparently
 * call into the simulator if active.
 */
static int
scsi_mode_select(ds_scsi_info_t *sip, uchar_t page_code, int options,
    void *buf, uint_t buflen, scsi_ms_hdrs_t *headers, uint_t *skp,
    uint_t *ascp, uint_t *ascqp)
{
	int result;
	struct scsi_extended_sense sense;
	int senselen = sizeof (struct scsi_extended_sense);
	struct mode_page *mp = (struct mode_page *)buf;

	assert(sip->si_cdblen == MODE_CMD_LEN_6 ||
	    sip->si_cdblen == MODE_CMD_LEN_10);
	assert(headers->ms_length == sip->si_cdblen);

	bzero(&sense, sizeof (struct scsi_extended_sense));

	if (mp->ps) {
		options |= MODE_SELECT_SP;
		mp->ps = 0;
	} else {
		options &= ~MODE_SELECT_SP;
	}

	if (sip->si_cdblen == MODE_CMD_LEN_6) {
		/* The following fields are reserved during mode select: */
		headers->ms_hdr.g0.ms_header.length = 0;
		headers->ms_hdr.g0.ms_header.device_specific = 0;

		if (sip->si_sim)
			result = simscsi_mode_select(sip->si_sim,
			    page_code, options, buf, buflen,
			    &headers->ms_hdr.g0, &sense, &senselen);
		else
			result = uscsi_mode_select(sip->si_dsp->ds_fd,
			    page_code, options, buf, buflen,
			    &headers->ms_hdr.g0, &sense, &senselen);
	} else {
		/* The following fields are reserved during mode select: */
		headers->ms_hdr.g1.ms_header.length = 0;
		headers->ms_hdr.g1.ms_header.device_specific = 0;

		if (sip->si_sim)
			result = simscsi_mode_select_10(sip->si_sim,
			    page_code, options, buf, buflen,
			    &headers->ms_hdr.g1, &sense, &senselen);
		else
			result = uscsi_mode_select_10(sip->si_dsp->ds_fd,
			    page_code, options, buf, buflen,
			    &headers->ms_hdr.g1, &sense, &senselen);
	}

	if (result != 0)
		scsi_translate_error(&sense, skp, ascp, ascqp);

	return (result);
}

static int
scsi_mode_sense(ds_scsi_info_t *sip, uchar_t page_code, uchar_t pc,
    void *buf, uint_t buflen, scsi_ms_hdrs_t *headers, uint_t *skp,
    uint_t *ascp, uint_t *ascqp)
{
	int result;
	struct scsi_extended_sense sense;
	int senselen = sizeof (struct scsi_extended_sense);

	assert(sip->si_cdblen == MODE_CMD_LEN_6 ||
	    sip->si_cdblen == MODE_CMD_LEN_10);

	bzero(&sense, sizeof (struct scsi_extended_sense));

	bzero(headers, sizeof (scsi_ms_hdrs_t));
	headers->ms_length = sip->si_cdblen;

	if (sip->si_cdblen == MODE_CMD_LEN_6) {
		if (sip->si_sim)
			result = simscsi_mode_sense(sip->si_sim,
			    page_code, pc, buf, buflen, &headers->ms_hdr.g0,
			    &sense, &senselen);
		else
			result = uscsi_mode_sense(sip->si_dsp->ds_fd, page_code,
			    pc, buf, buflen, &headers->ms_hdr.g0, &sense,
			    &senselen);
	} else {
		if (sip->si_sim)
			result = simscsi_mode_sense_10(sip->si_sim,
			    page_code, pc, buf, buflen, &headers->ms_hdr.g1,
			    &sense, &senselen);
		else
			result = uscsi_mode_sense_10(sip->si_dsp->ds_fd,
			    page_code, pc, buf, buflen, &headers->ms_hdr.g1,
			    &sense, &senselen);
	}

	if (result != 0)
		scsi_translate_error(&sense, skp, ascp, ascqp);

	return (result);
}

static int
scsi_request_sense(ds_scsi_info_t *sip, uint_t *skp, uint_t *ascp,
    uint_t *ascqp)
{
	struct scsi_extended_sense sense, sensebuf;
	int senselen = sizeof (struct scsi_extended_sense);
	int sensebuflen = sizeof (struct scsi_extended_sense);
	int result;

	bzero(&sense, sizeof (struct scsi_extended_sense));
	bzero(&sensebuf, sizeof (struct scsi_extended_sense));

	if (sip->si_sim)
		result = simscsi_request_sense(sip->si_sim,
		    (caddr_t)&sensebuf, sensebuflen, &sense, &senselen);
	else
		result = uscsi_request_sense(sip->si_dsp->ds_fd,
		    (caddr_t)&sensebuf, sensebuflen, &sense, &senselen);

	if (result == 0)
		scsi_translate_error(&sensebuf, skp, ascp, ascqp);
	else
		scsi_translate_error(&sense, skp, ascp, ascqp);

	return (result);
}

static int
scsi_log_sense(ds_scsi_info_t *sip, int page_code, int page_control,
    caddr_t page_data, int page_size, uint_t *skp, uint_t *ascp, uint_t *ascqp)
{
	int result;
	struct scsi_extended_sense sense;
	int senselen = sizeof (struct scsi_extended_sense);

	if (sip->si_sim)
		result = simscsi_log_sense(sip->si_sim,
		    page_code, page_control, page_data, page_size, &sense,
		    &senselen);
	else
		result = uscsi_log_sense(sip->si_dsp->ds_fd,
		    page_code, page_control, page_data, page_size, &sense,
		    &senselen);

	if (result != 0)
		scsi_translate_error(&sense, skp, ascp, ascqp);

	return (result);
}

/*
 * Given a list of supported mode pages, determine if the given page is present.
 */
static boolean_t
mode_page_present(uchar_t *pgdata, uint_t pgdatalen, uchar_t pagecode)
{
	uint_t i = 0;
	struct mode_page *pg;
	boolean_t found = B_FALSE;

	/*
	 * The mode page list contains all mode pages supported by the device,
	 * one after the other.
	 */
	while (i < pgdatalen) {
		pg = (struct mode_page *)&pgdata[i];

		if (pg->code == pagecode) {
			found = B_TRUE;
			break;
		}

		i += MODESENSE_PAGE_LEN(pg);
	}

	return (found);
}

/*
 * Load mode pages and check that the appropriate pages are supported.
 *
 * As part of this process, we determine which form of the MODE SENSE / MODE
 * SELECT command to use (the 6-byte or 10-byte version) by executing a MODE
 * SENSE command for a page that should be implemented by the device.
 */
static int
load_modepages(ds_scsi_info_t *sip)
{
	int allpages_buflen;
	uchar_t *allpages;
	scsi_ms_hdrs_t headers;
	int result;
	uint_t skey, asc, ascq;
	int datalength = 0;
	scsi_ms_header_t *smh = &headers.ms_hdr.g0;
	scsi_ms_header_g1_t *smh_g1 = &headers.ms_hdr.g1;
	nvlist_t *nvl;

	allpages_buflen = MAX_BUFLEN(scsi_ms_header_g1_t);
	if ((allpages = calloc(allpages_buflen, 1)) == NULL)
		return (scsi_set_errno(sip, EDS_NOMEM));

	bzero(&headers, sizeof (headers));

	/*
	 * Attempt a mode sense(6).  If that fails, try a mode sense(10)
	 *
	 * allpages is allocated to be of the maximum size for either a mode
	 * sense(6) or mode sense(10) MODEPAGE_ALLPAGES response.
	 *
	 * Note that the length passed into uscsi_mode_sense should be set to
	 * the maximum size of the parameter response, which in this case is
	 * UCHAR_MAX - the size of the headers/block descriptors.
	 */
	sip->si_cdblen = MODE_CMD_LEN_6;
	if ((result = scsi_mode_sense(sip, MODEPAGE_ALLPAGES, PC_CURRENT,
	    (caddr_t)allpages, UCHAR_MAX - sizeof (scsi_ms_header_t),
	    &headers, &skey, &asc, &ascq)) == 0) {
		/*
		 * Compute the data length of the page that contains all mode
		 * sense pages.  This is a bit tricky because the format of the
		 * response from the lun is:
		 *
		 * header: <length> <medium type byte> <dev specific byte>
		 *	   <block descriptor length>
		 *	   [<optional block descriptor>]
		 * data:   [<mode page data> <mode page data> ...]
		 *
		 * Since the length field in the header describes the length of
		 * the entire response.  This includes the header, but NOT
		 * the length field itself, which is 1 or 2 bytes depending on
		 * which mode sense type (6- or 10- byte) is being executed.
		 *
		 * So, the data length equals the length value in the header
		 * plus 1 (because the length byte was not included in the
		 * length count), minus [[the sum of the length of the header
		 * and the length of the block descriptor]].
		 */
		datalength = (smh->ms_header.length +
		    sizeof (smh->ms_header.length)) -
		    (sizeof (struct mode_header) +
		    smh->ms_header.bdesc_length);
	} else if (SCSI_INVALID_OPCODE(skey, asc, ascq)) {
		/*
		 * Fallback and try the 10-byte version of the command.
		 */
		sip->si_cdblen = MODE_CMD_LEN_10;
		result = scsi_mode_sense(sip, MODEPAGE_ALLPAGES,
		    PC_CURRENT, (caddr_t)allpages, allpages_buflen,
		    &headers, &skey, &asc, &ascq);

		if (result == 0) {
			datalength = (BE_16(smh_g1->ms_header.length) +
			    sizeof (smh_g1->ms_header.length)) -
			    (sizeof (struct mode_header_g1) +
			    BE_16(smh_g1->ms_header.bdesc_length));

		}
	}

	if (result == 0 && datalength >= 0) {
		if (nvlist_add_int8(sip->si_dsp->ds_state, "command-length",
		    sip->si_cdblen == MODE_CMD_LEN_6 ? 6 : 10) != 0) {
			free(allpages);
			return (scsi_set_errno(sip, EDS_NOMEM));
		}

		nvl = NULL;
		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
		    nvlist_add_nvlist(sip->si_dsp->ds_state, "modepages",
		    nvl) != 0) {
			free(allpages);
			nvlist_free(nvl);
			return (scsi_set_errno(sip, EDS_NOMEM));
		}

		nvlist_free(nvl);
		result = nvlist_lookup_nvlist(sip->si_dsp->ds_state,
		    "modepages", &sip->si_state_modepage);
		assert(result == 0);

		/*
		 * One of the sets of the commands (above) succeeded, so now
		 * look for the mode pages we need and record them appropriately
		 */
		if (mode_page_present(allpages, datalength,
		    MODEPAGE_INFO_EXCPT)) {

			nvl = NULL;
			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
			    nvlist_add_nvlist(sip->si_state_modepage,
			    "informational-exceptions", nvl) != 0) {
				free(allpages);
				nvlist_free(nvl);
				return (scsi_set_errno(sip, EDS_NOMEM));
			}
			nvlist_free(nvl);
			sip->si_supp_mode |= MODEPAGE_SUPP_IEC;
			result = nvlist_lookup_nvlist(sip->si_state_modepage,
			    "informational-exceptions", &sip->si_state_iec);
			assert(result == 0);
		}

	} else {
		/*
		 * If the device failed to respond to one of the basic commands,
		 * then assume it's not a SCSI device or otherwise doesn't
		 * support the necessary transport.
		 */
		if (datalength < 0)
			dprintf("command returned invalid data length (%d)\n",
			    datalength);
		else
			dprintf("failed to load modepages (KEY=0x%x "
			    "ASC=0x%x ASCQ=0x%x)\n", skey, asc, ascq);

		result = scsi_set_errno(sip, EDS_NO_TRANSPORT);
	}

	free(allpages);
	return (result);
}

/*
 * Verify a single logpage.  This will do some generic validation and then call
 * the logpage-specific function for further verification.
 */
static int
verify_logpage(ds_scsi_info_t *sip, logpage_validation_entry_t *lp)
{
	scsi_log_header_t *lhp;
	struct scsi_extended_sense sense;
	int buflen;
	int log_length;
	int result = 0;
	uint_t kp, asc, ascq;
	nvlist_t *nvl;

	buflen = MAX_BUFLEN(scsi_log_header_t);
	if ((lhp = calloc(buflen, 1)) == NULL)
		return (scsi_set_errno(sip, EDS_NOMEM));
	bzero(&sense, sizeof (struct scsi_extended_sense));

	nvl = NULL;
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
	    nvlist_add_nvlist(sip->si_state_logpage, lp->ve_desc, nvl) != 0) {
		nvlist_free(nvl);
		free(lhp);
		return (scsi_set_errno(sip, EDS_NOMEM));
	}
	nvlist_free(nvl);
	result = nvlist_lookup_nvlist(sip->si_state_logpage, lp->ve_desc, &nvl);
	assert(result == 0);

	result = scsi_log_sense(sip, lp->ve_code,
	    PC_CUMULATIVE, (caddr_t)lhp, buflen, &kp, &asc, &ascq);

	if (result == 0) {
		log_length = BE_16(lhp->lh_length);
		if (nvlist_add_uint16(nvl, "length", log_length) != 0) {
			free(lhp);
			return (scsi_set_errno(sip, EDS_NOMEM));
		}

		if (lp->ve_validate(sip, (scsi_log_parameter_header_t *)
		    (((char *)lhp) + sizeof (scsi_log_header_t)),
		    log_length, nvl) != 0) {
			free(lhp);
			return (-1);
		}
	} else {
		dprintf("failed to load %s log page (KEY=0x%x "
		    "ASC=0x%x ASCQ=0x%x)\n", lp->ve_desc, kp, asc, ascq);
	}

	free(lhp);
	return (0);
}

/*
 * Load log pages and determine which pages are supported.
 */
static int
load_logpages(ds_scsi_info_t *sip)
{
	int buflen;
	scsi_supported_log_pages_t *sp;
	struct scsi_extended_sense sense;
	int result;
	uint_t sk, asc, ascq;
	int i, j;
	nvlist_t *nvl;

	buflen = MAX_BUFLEN(scsi_log_header_t);
	if ((sp = calloc(buflen, 1)) == NULL)
		return (scsi_set_errno(sip, EDS_NOMEM));

	bzero(&sense, sizeof (struct scsi_extended_sense));

	if ((result = scsi_log_sense(sip, LOGPAGE_SUPP_LIST,
	    PC_CUMULATIVE, (caddr_t)sp, buflen, &sk, &asc, &ascq)) == 0) {
		int pagecount = BE_16(sp->slp_hdr.lh_length);

		for (i = 0; i < pagecount; i++) {
			for (j = 0; j < NLOG_VALIDATION; j++) {
				if (log_validation[j].ve_code ==
				    sp->slp_pages[i])
					sip->si_supp_log |=
					    log_validation[j].ve_supported;
			}
		}
	}

	free(sp);
	if (result == 0) {
		nvl = NULL;
		if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
		    nvlist_add_nvlist(sip->si_dsp->ds_state, "logpages",
		    nvl) != 0) {
			nvlist_free(nvl);
			return (scsi_set_errno(sip, EDS_NOMEM));
		}

		nvlist_free(nvl);
		result = nvlist_lookup_nvlist(sip->si_dsp->ds_state,
		    "logpages", &sip->si_state_logpage);
		assert(result == 0);

		/*
		 * Validate the logpage contents.
		 */
		for (i = 0; i < NLOG_VALIDATION; i++) {
			if ((sip->si_supp_log &
			    log_validation[i].ve_supported) == 0)
				continue;

			/*
			 * verify_logpage will clear the supported bit if
			 * verification fails.
			 */
			if (verify_logpage(sip, &log_validation[i]) != 0)
				return (-1);
		}

	} else {
		dprintf("failed to get log pages "
		    "(KEY=0x%x ASC=0x%x ASCq=0x%x)\n", sk, asc, ascq);
	}

	/*
	 * We always return 0 here, even if the required log pages aren't
	 * supported.
	 */
	return (0);
}

/*
 * Verify that the IE log page is sane.  This log page is potentially chock-full
 * of vendor specific information that we do not know how to access.  All we can
 * do is check for the generic predictive failure bit.  If this log page is not
 * well-formed, then bail out.
 */
static int
logpage_ie_verify(ds_scsi_info_t *sip, scsi_log_parameter_header_t *lphp,
    int log_length, nvlist_t *nvl)
{
	int i, plen = 0;
	boolean_t seen = B_FALSE;
	scsi_ie_log_param_t *iep =
	    (scsi_ie_log_param_t *)lphp;

	for (i = 0; i < log_length; i += plen) {
		iep = (scsi_ie_log_param_t *)((char *)iep + plen);

		if (BE_16(iep->ie_hdr.lph_param) == LOGPARAM_IE) {
			if (nvlist_add_boolean_value(nvl, "general",
			    B_TRUE) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));

			if (lphp->lph_length < LOGPARAM_IE_MIN_LEN) {
				if (nvlist_add_uint8(nvl,
				    "invalid-length", lphp->lph_length) != 0)
					return (scsi_set_errno(sip, EDS_NOMEM));
			} else {
				seen = B_TRUE;
			}
			break;
		}

		plen = iep->ie_hdr.lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	if (!seen) {
		sip->si_supp_log &= ~LOGPAGE_SUPP_IE;
		dprintf("IE logpage validation failed\n");
	}

	return (0);
}

/*
 * Verify the contents of the temperature log page.  The temperature log page
 * contains two log parameters: the current temperature, and (optionally) the
 * reference temperature.  For the verification phase, we check that the two
 * parameters we care about are well-formed.  If there is no reference
 * temperature, then we cannot use the page for monitoring purposes.
 */
static int
logpage_temp_verify(ds_scsi_info_t *sip,
    scsi_log_parameter_header_t *lphp, int log_length, nvlist_t *nvl)
{
	int i, plen = 0;
	boolean_t has_reftemp = B_FALSE;
	boolean_t bad_length = B_FALSE;
	ushort_t param_code;

	for (i = 0; i < log_length; i += plen) {
		lphp = (scsi_log_parameter_header_t *)((char *)lphp + plen);
		param_code = BE_16(lphp->lph_param);

		switch (param_code) {
		case LOGPARAM_TEMP_CURTEMP:
			if (nvlist_add_boolean_value(nvl, "current-temperature",
			    B_TRUE) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			if (lphp->lph_length != LOGPARAM_TEMP_LEN) {
				if (nvlist_add_uint8(nvl,
				    "invalid-length", lphp->lph_length) != 0)
					return (scsi_set_errno(sip, EDS_NOMEM));
				bad_length = B_TRUE;
			}
			break;

		case LOGPARAM_TEMP_REFTEMP:
			if (nvlist_add_boolean_value(nvl,
			    "reference-temperature", B_TRUE) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			if (lphp->lph_length != LOGPARAM_TEMP_LEN) {
				if (nvlist_add_uint8(nvl,
				    "invalid-length", lphp->lph_length) != 0)
					return (scsi_set_errno(sip, EDS_NOMEM));
				bad_length = B_TRUE;
			}
			has_reftemp = B_TRUE;
			break;
		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	if (bad_length || !has_reftemp) {
		sip->si_supp_log &= ~LOGPAGE_SUPP_TEMP;
		dprintf("temperature logpage validation failed\n");
	}

	return (0);
}

/*
 * Verify the contents of the self test log page.  The log supports a maximum of
 * 20 entries, where each entry's parameter code is its index in the log.  We
 * check that the parameter codes fall within this range, and that the size of
 * each page is what we expect.  It's perfectly acceptable for there to be no
 * entries in this log, so we must also be sure to validate the contents as part
 * of the analysis phase.
 */
static int
logpage_selftest_verify(ds_scsi_info_t *sip,
    scsi_log_parameter_header_t *lphp, int log_length, nvlist_t *nvl)
{
	int i, plen = 0;
	boolean_t bad = B_FALSE;
	int entries = 0;
	ushort_t param_code;

	for (i = 0; i < log_length; i += plen, entries++) {
		lphp = (scsi_log_parameter_header_t *)((char *)lphp + plen);
		param_code = BE_16(lphp->lph_param);

		if (param_code < LOGPAGE_SELFTEST_MIN_PARAM_CODE ||
		    param_code > LOGPAGE_SELFTEST_MAX_PARAM_CODE) {
			if (nvlist_add_uint16(nvl, "invalid-param-code",
			    param_code) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			bad = B_TRUE;
			break;
		}

		if (lphp->lph_length != LOGPAGE_SELFTEST_PARAM_LEN) {
			if (nvlist_add_uint8(nvl, "invalid-length",
			    lphp->lph_length) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			bad = B_TRUE;
			break;

		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	if (bad) {
		sip->si_supp_log &= ~LOGPAGE_SUPP_SELFTEST;
		dprintf("selftest logpage validation failed\n");
	}

	return (0);
}

/*
 * Verify the contents of the Solid State Media (SSM) log page.
 * As of SBC3r36 SSM log page contains one log parameter:
 * "Percentage Used Endurance Indicator" which is mandatory.
 * For the verification phase, we sanity check this parameter
 * by making sure it's present and it's length is set to 0x04.
 */
static int
logpage_ssm_verify(ds_scsi_info_t *sip,
    scsi_log_parameter_header_t *lphp, int log_length, nvlist_t *nvl)
{
	ushort_t param_code;
	int i, plen = 0;

	for (i = 0; i < log_length; i += plen) {
		lphp = (scsi_log_parameter_header_t *)((char *)lphp + plen);
		param_code = BE_16(lphp->lph_param);

		switch (param_code) {
		case LOGPARAM_PRCNT_USED:
			if (nvlist_add_boolean_value(nvl,
			    FM_EREPORT_SCSI_SSMWEAROUT, B_TRUE) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			if (lphp->lph_length != LOGPARAM_PRCNT_USED_PARAM_LEN) {
				if (nvlist_add_uint8(nvl,
				    "invalid-length", lphp->lph_length) != 0)
					return (scsi_set_errno(sip, EDS_NOMEM));

				dprintf("solid state media logpage bad len\n");
				break;
			}

			/* verification succeded */
			return (0);
		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	/* verification failed */
	sip->si_supp_log &= ~LOGPAGE_SUPP_SSM;
	return (0);
}

/*
 * Load the current IE mode pages
 */
static int
load_ie_modepage(ds_scsi_info_t *sip)
{
	struct scsi_ms_hdrs junk_hdrs;
	int result;
	uint_t skey, asc, ascq;

	if (!(sip->si_supp_mode & MODEPAGE_SUPP_IEC))
		return (0);

	bzero(&sip->si_iec_current, sizeof (sip->si_iec_current));
	bzero(&sip->si_iec_changeable, sizeof (sip->si_iec_changeable));

	if ((result = scsi_mode_sense(sip,
	    MODEPAGE_INFO_EXCPT, PC_CURRENT, &sip->si_iec_current,
	    MODEPAGE_INFO_EXCPT_LEN, &sip->si_hdrs, &skey, &asc,
	    &ascq)) == 0) {
		result = scsi_mode_sense(sip,
		    MODEPAGE_INFO_EXCPT, PC_CHANGEABLE,
		    &sip->si_iec_changeable,
		    MODEPAGE_INFO_EXCPT_LEN, &junk_hdrs, &skey, &asc, &ascq);
	}

	if (result != 0) {
		dprintf("failed to get IEC modepage (KEY=0x%x "
		    "ASC=0x%x ASCQ=0x%x)", skey, asc, ascq);
		sip->si_supp_mode &= ~MODEPAGE_SUPP_IEC;
	} else  {
		if (nvlist_add_boolean_value(sip->si_state_iec,
		    "dexcpt", sip->si_iec_current.ie_dexcpt) != 0 ||
		    nvlist_add_boolean_value(sip->si_state_iec,
		    "logerr", sip->si_iec_current.ie_logerr) != 0 ||
		    nvlist_add_uint8(sip->si_state_iec,
		    "mrie", sip->si_iec_current.ie_mrie) != 0 ||
		    nvlist_add_boolean_value(sip->si_state_iec,
		    "test", sip->si_iec_current.ie_test) != 0 ||
		    nvlist_add_boolean_value(sip->si_state_iec,
		    "ewasc", sip->si_iec_current.ie_ewasc) != 0 ||
		    nvlist_add_boolean_value(sip->si_state_iec,
		    "perf", sip->si_iec_current.ie_perf) != 0 ||
		    nvlist_add_boolean_value(sip->si_state_iec,
		    "ebf", sip->si_iec_current.ie_ebf) != 0 ||
		    nvlist_add_uint32(sip->si_state_iec,
		    "interval-timer",
		    BE_32(sip->si_iec_current.ie_interval_timer)) != 0 ||
		    nvlist_add_uint32(sip->si_state_iec,
		    "report-count",
		    BE_32(sip->si_iec_current.ie_report_count)) != 0)
			return (scsi_set_errno(sip, EDS_NOMEM));
	}

	return (0);
}

/*
 * Enable IE reporting.  We prefer the following settings:
 *
 * (1) DEXCPT = 0
 * (3) MRIE = 6 (IE_REPORT_ON_REQUEST)
 * (4) EWASC = 1
 * (6) REPORT COUNT = 0x00000001
 * (7) LOGERR = 1
 *
 * However, not all drives support changing these values, and the current state
 * may be useful enough as-is.  For example, some drives support IE logging, but
 * don't support changing the MRIE.  In this case, we can still use the
 * information provided by the log page.
 */
static int
scsi_enable_ie(ds_scsi_info_t *sip, boolean_t *changed)
{
	scsi_ie_page_t new_iec_page;
	scsi_ms_hdrs_t hdrs;
	uint_t skey, asc, ascq;

	if (!(sip->si_supp_mode & MODEPAGE_SUPP_IEC))
		return (0);

	bzero(&new_iec_page, sizeof (new_iec_page));
	bzero(&hdrs, sizeof (hdrs));

	(void) memcpy(&new_iec_page, &sip->si_iec_current,
	    sizeof (new_iec_page));

	if (IEC_IE_CHANGEABLE(sip->si_iec_changeable))
		new_iec_page.ie_dexcpt = 0;

	if (IEC_MRIE_CHANGEABLE(sip->si_iec_changeable))
		new_iec_page.ie_mrie = IE_REPORT_ON_REQUEST;

	/*
	 * We only want to enable warning reporting if we are able to change the
	 * mrie to report on request.  Otherwise, we risk unnecessarily
	 * interrupting normal SCSI commands with a CHECK CONDITION code.
	 */
	if (IEC_EWASC_CHANGEABLE(sip->si_iec_changeable)) {
		if (new_iec_page.ie_mrie == IE_REPORT_ON_REQUEST)
			new_iec_page.ie_ewasc = 1;
		else
			new_iec_page.ie_ewasc = 0;
	}

	if (IEC_RPTCNT_CHANGEABLE(sip->si_iec_changeable))
		new_iec_page.ie_report_count = BE_32(1);

	if (IEC_LOGERR_CHANGEABLE(sip->si_iec_changeable))
		new_iec_page.ie_logerr = 1;

	/*
	 * Now compare the new mode page with the existing one.
	 * if there's no difference, there's no need for a mode select
	 */
	if (memcmp(&new_iec_page, &sip->si_iec_current,
	    MODEPAGE_INFO_EXCPT_LEN) == 0) {
		*changed = B_FALSE;
	} else {
		(void) memcpy(&hdrs, &sip->si_hdrs, sizeof (sip->si_hdrs));

		if (scsi_mode_select(sip,
		    MODEPAGE_INFO_EXCPT, MODE_SELECT_PF, &new_iec_page,
		    MODEPAGE_INFO_EXCPT_LEN, &hdrs, &skey, &asc, &ascq) == 0) {
			*changed = B_TRUE;
		} else {
			dprintf("failed to enable IE (KEY=0x%x "
			    "ASC=0x%x ASCQ=0x%x)\n", skey, asc, ascq);
			*changed = B_FALSE;
		}
	}

	if (nvlist_add_boolean_value(sip->si_state_iec, "changed",
	    *changed) != 0)
		return (scsi_set_errno(sip, EDS_NOMEM));

	return (0);
}

/*
 * Clear the GLTSD bit, indicating log pages should be saved to non-volatile
 * storage.
 */
static int
clear_gltsd(ds_scsi_info_t *sip)
{
	scsi_ms_hdrs_t hdrs, junk_hdrs;
	struct mode_control_scsi3 control_pg_cur, control_pg_chg;
	int result;
	uint_t skey, asc, ascq;

	bzero(&hdrs, sizeof (hdrs));
	bzero(&control_pg_cur, sizeof (control_pg_cur));
	bzero(&control_pg_chg, sizeof (control_pg_chg));

	result = scsi_mode_sense(sip,
	    MODEPAGE_CTRL_MODE, PC_CURRENT, &control_pg_cur,
	    MODEPAGE_CTRL_MODE_LEN, &hdrs, &skey, &asc, &ascq);

	if (result != 0) {
		dprintf("failed to read Control mode page (KEY=0x%x "
		    "ASC=0x%x ASCQ=0x%x)\n", skey, asc, ascq);
	} else if (control_pg_cur.mode_page.length !=
	    PAGELENGTH_MODE_CONTROL_SCSI3) {
		dprintf("SCSI-3 control mode page not supported\n");
	} else if ((result = scsi_mode_sense(sip,
	    MODEPAGE_CTRL_MODE, PC_CHANGEABLE, &control_pg_chg,
	    MODEPAGE_CTRL_MODE_LEN, &junk_hdrs, &skey, &asc, &ascq))
	    != 0) {
		dprintf("failed to read changeable Control mode page (KEY=0x%x "
		    "ASC=0x%x ASCQ=0x%x)\n", skey, asc, ascq);
	} else if (control_pg_cur.gltsd && !GLTSD_CHANGEABLE(control_pg_chg)) {
		dprintf("gltsd is set and not changeable\n");
		if (nvlist_add_boolean_value(sip->si_dsp->ds_state,
		    "gltsd", control_pg_cur.gltsd) != 0)
			return (scsi_set_errno(sip, EDS_NOMEM));
	} else if (control_pg_cur.gltsd) {
		control_pg_cur.gltsd = 0;
		result = scsi_mode_select(sip,
		    MODEPAGE_CTRL_MODE, MODE_SELECT_PF, &control_pg_cur,
		    MODEPAGE_CTRL_MODE_LEN, &hdrs, &skey, &asc, &ascq);
		if (result != 0)
			dprintf("failed to enable GLTSD (KEY=0x%x "
			    "ASC=0x%x ASCQ=0x%x\n", skey, asc, ascq);
		if (nvlist_add_boolean_value(sip->si_dsp->ds_state,
		    "gltsd", control_pg_cur.gltsd) != 0)
			return (scsi_set_errno(sip, EDS_NOMEM));
	}

	return (0);
}

/*
 * Fetch the contents of the logpage, and then call the logpage-specific
 * analysis function.  The analysis function is responsible for detecting any
 * faults and filling in the details.
 */
static int
analyze_one_logpage(ds_scsi_info_t *sip, logpage_validation_entry_t *entry)
{
	scsi_log_header_t *lhp;
	scsi_log_parameter_header_t *lphp;
	int buflen;
	int log_length;
	uint_t skey, asc, ascq;
	int result;

	buflen = MAX_BUFLEN(scsi_log_header_t);
	if ((lhp = calloc(buflen, 1)) == NULL)
		return (scsi_set_errno(sip, EDS_NOMEM));

	result = scsi_log_sense(sip, entry->ve_code,
	    PC_CUMULATIVE, (caddr_t)lhp, buflen, &skey, &asc, &ascq);

	if (result == 0) {
		log_length = BE_16(lhp->lh_length);
		lphp = (scsi_log_parameter_header_t *)(((uchar_t *)lhp) +
		    sizeof (scsi_log_header_t));

		result = entry->ve_analyze(sip, lphp, log_length);
	} else {
		result = scsi_set_errno(sip, EDS_IO);
	}

	free(lhp);
	return (result);
}

/*
 * Analyze the IE logpage.  If we find an IE log record with a non-zero 'asc',
 * then we have a fault.
 */
static int
logpage_ie_analyze(ds_scsi_info_t *sip, scsi_log_parameter_header_t *lphp,
    int log_length)
{
	int i, plen = 0;
	scsi_ie_log_param_t *iep = (scsi_ie_log_param_t *)lphp;
	nvlist_t *nvl;

	assert(sip->si_dsp->ds_predfail == NULL);
	if (nvlist_alloc(&sip->si_dsp->ds_predfail, NV_UNIQUE_NAME, 0) != 0)
		return (scsi_set_errno(sip, EDS_NOMEM));
	nvl = sip->si_dsp->ds_predfail;

	for (i = 0; i < log_length; i += plen) {
		iep = (scsi_ie_log_param_t *)((char *)iep + plen);

		/*
		 * Even though we validated the length during the initial phase,
		 * never trust the device.
		 */
		if (BE_16(iep->ie_hdr.lph_param) == LOGPARAM_IE &&
		    iep->ie_hdr.lph_length >= LOGPARAM_IE_MIN_LEN) {
			if (nvlist_add_uint8(nvl, FM_EREPORT_PAYLOAD_SCSI_ASC,
			    iep->ie_asc) != 0 ||
			    nvlist_add_uint8(nvl, FM_EREPORT_PAYLOAD_SCSI_ASCQ,
			    iep->ie_ascq) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));

			if (iep->ie_asc != 0)
				sip->si_dsp->ds_faults |=
				    DS_FAULT_PREDFAIL;
			break;
		}
		plen = iep->ie_hdr.lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	return (0);
}

static int
logpage_temp_analyze(ds_scsi_info_t *sip, scsi_log_parameter_header_t *lphp,
    int log_length)
{
	int i, plen = 0;
	uint8_t reftemp, curtemp;
	ushort_t param_code;
	scsi_temp_log_param_t *temp;
	nvlist_t *nvl;

	assert(sip->si_dsp->ds_overtemp == NULL);
	if (nvlist_alloc(&sip->si_dsp->ds_overtemp, NV_UNIQUE_NAME, 0) != 0)
		return (scsi_set_errno(sip, EDS_NOMEM));
	nvl = sip->si_dsp->ds_overtemp;

	reftemp = curtemp = INVALID_TEMPERATURE;
	for (i = 0; i < log_length; i += plen) {
		lphp = (scsi_log_parameter_header_t *)((char *)lphp + plen);
		param_code = BE_16(lphp->lph_param);
		temp = (scsi_temp_log_param_t *)lphp;

		switch (param_code) {
		case LOGPARAM_TEMP_CURTEMP:
			if (lphp->lph_length != LOGPARAM_TEMP_LEN)
				break;

			if (nvlist_add_uint8(nvl,
			    FM_EREPORT_PAYLOAD_SCSI_CURTEMP,
			    temp->t_temp) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			curtemp = temp->t_temp;
			break;

		case LOGPARAM_TEMP_REFTEMP:
			if (lphp->lph_length != LOGPARAM_TEMP_LEN)
				break;

			if (nvlist_add_uint8(nvl,
			    FM_EREPORT_PAYLOAD_SCSI_THRESHTEMP,
			    temp->t_temp) != 0)
				return (scsi_set_errno(sip, EDS_NOMEM));
			reftemp = temp->t_temp;
			break;
		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	if (reftemp != INVALID_TEMPERATURE && curtemp != INVALID_TEMPERATURE &&
	    curtemp > reftemp)
		sip->si_dsp->ds_faults |= DS_FAULT_OVERTEMP;

	return (0);
}

static int
logpage_selftest_analyze(ds_scsi_info_t *sip, scsi_log_parameter_header_t *lphp,
    int log_length)
{
	int i, plen = 0;
	int entries = 0;
	ushort_t param_code;
	scsi_selftest_log_param_t *stp;
	nvlist_t *nvl;

	assert(sip->si_dsp->ds_testfail == NULL);
	if (nvlist_alloc(&sip->si_dsp->ds_testfail, NV_UNIQUE_NAME, 0) != 0)
		return (scsi_set_errno(sip, EDS_NOMEM));
	nvl = sip->si_dsp->ds_testfail;

	for (i = 0; i < log_length; i += plen, entries++) {
		lphp = (scsi_log_parameter_header_t *)((char *)lphp + plen);
		param_code = BE_16(lphp->lph_param);
		stp = (scsi_selftest_log_param_t *)lphp;

		if (param_code >= LOGPAGE_SELFTEST_MIN_PARAM_CODE &&
		    param_code <= LOGPAGE_SELFTEST_MAX_PARAM_CODE &&
		    lphp->lph_length >= LOGPAGE_SELFTEST_PARAM_LEN) {
			/*
			 * We always log the last result, or the result of the
			 * last completed test.
			 */
			if ((param_code == 1 ||
			    SELFTEST_COMPLETE(stp->st_results))) {
				if (nvlist_add_uint8(nvl,
				    FM_EREPORT_PAYLOAD_SCSI_RESULTCODE,
				    stp->st_results) != 0 ||
				    nvlist_add_uint16(nvl,
				    FM_EREPORT_PAYLOAD_SCSI_TIMESTAMP,
				    BE_16(stp->st_timestamp)) != 0 ||
				    nvlist_add_uint8(nvl,
				    FM_EREPORT_PAYLOAD_SCSI_SEGMENT,
				    stp->st_number) != 0 ||
				    nvlist_add_uint64(nvl,
				    FM_EREPORT_PAYLOAD_SCSI_ADDRESS,
				    BE_64(stp->st_lba)) != 0)
					return (scsi_set_errno(sip,
					    EDS_NOMEM));

				if (SELFTEST_COMPLETE(stp->st_results)) {
					if (stp->st_results != SELFTEST_OK)
						sip->si_dsp->ds_faults |=
						    DS_FAULT_TESTFAIL;
					return (0);
				}
			}
		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	return (0);
}

/*
 * Analyze the contents of the Solid State Media (SSM) log page's
 * "Percentage Used Endurance Indicator" log parameter.
 * We generate a fault if the percentage used is equal to or over
 * PRCNT_USED_FAULT_THRSH
 */
static int
logpage_ssm_analyze(ds_scsi_info_t *sip, scsi_log_parameter_header_t *lphp,
    int log_length)
{
	uint16_t param_code;
	scsi_ssm_log_param_t *ssm;
	nvlist_t *nvl;
	int i, plen = 0;

	assert(sip->si_dsp->ds_ssmwearout == NULL);
	if (nvlist_alloc(&sip->si_dsp->ds_ssmwearout, NV_UNIQUE_NAME, 0) != 0)
		return (scsi_set_errno(sip, EDS_NOMEM));
	nvl = sip->si_dsp->ds_ssmwearout;

	for (i = 0; i < log_length; i += plen) {
		lphp = (scsi_log_parameter_header_t *)((uint8_t *)lphp + plen);
		param_code = BE_16(lphp->lph_param);
		ssm = (scsi_ssm_log_param_t *)lphp;

		switch (param_code) {
		case LOGPARAM_PRCNT_USED:
			if (lphp->lph_length != LOGPARAM_PRCNT_USED_PARAM_LEN)
				break;

			if ((nvlist_add_uint8(nvl,
			    FM_EREPORT_PAYLOAD_SCSI_CURSSMWEAROUT,
			    ssm->ssm_prcnt_used) != 0) ||
			    (nvlist_add_uint8(nvl,
			    FM_EREPORT_PAYLOAD_SCSI_THRSHSSMWEAROUT,
			    PRCNT_USED_FAULT_THRSH) != 0))
				return (scsi_set_errno(sip, EDS_NOMEM));

			if (ssm->ssm_prcnt_used >= PRCNT_USED_FAULT_THRSH)
				sip->si_dsp->ds_faults |= DS_FAULT_SSMWEAROUT;

			return (0);
		}

		plen = lphp->lph_length +
		    sizeof (scsi_log_parameter_header_t);
	}

	/*
	 * If we got this far we didn't see LOGPARAM_PRCNT_USED
	 * which is strange since we verified that it's there
	 */
	dprintf("solid state media logpage analyze failed\n");
#if DEBUG
	abort();
#endif
	return (scsi_set_errno(sip, EDS_NOT_SUPPORTED));
}

/*
 * Analyze the IE mode sense page explicitly.  This is only needed if the IE log
 * page is not supported.
 */
static int
analyze_ie_sense(ds_scsi_info_t *sip)
{
	uint_t skey, asc, ascq;
	nvlist_t *nvl;

	/*
	 * Don't bother checking if we weren't able to set our MRIE correctly.
	 */
	if (sip->si_iec_current.ie_mrie != IE_REPORT_ON_REQUEST)
		return (0);

	if (scsi_request_sense(sip, &skey, &asc, &ascq) != 0) {
		dprintf("failed to request IE page (KEY=0x%x ASC=0x%x "
		    "ASCQ=0x%x)\n", skey, asc, ascq);
		return (scsi_set_errno(sip, EDS_IO));
	} else if (skey == KEY_NO_SENSE) {
		assert(sip->si_dsp->ds_predfail == NULL);
		if (nvlist_alloc(&sip->si_dsp->ds_predfail,
		    NV_UNIQUE_NAME, 0) != 0)
			return (scsi_set_errno(sip, EDS_NOMEM));
		nvl = sip->si_dsp->ds_predfail;

		if (nvlist_add_uint8(nvl,
		    FM_EREPORT_PAYLOAD_SCSI_ASC, asc) != 0 ||
		    nvlist_add_uint8(nvl,
		    FM_EREPORT_PAYLOAD_SCSI_ASCQ, ascq) != 0) {
			nvlist_free(nvl);
			return (scsi_set_errno(sip, EDS_NOMEM));
		}

		if (asc != 0)
			sip->si_dsp->ds_faults |= DS_FAULT_PREDFAIL;
	}

	return (0);
}

/*
 * Clean up the scsi-specific information structure.
 */
static void
ds_scsi_close(void *arg)
{
	ds_scsi_info_t *sip = arg;
	if (sip->si_sim)
		(void) dlclose(sip->si_sim);

	free(sip);
}

/*
 * Initialize a single disk.  Initialization consists of:
 *
 * 1. Check to see if the IE mechanism is enabled via MODE SENSE for the IE
 *    Control page (page 0x1C).
 *
 * 2. If the IE page is available, try to set the following parameters:
 *
 *    	DEXCPT		0	Enable exceptions
 *    	MRIE		6	Only report IE information on request
 *    	EWASC		1	Enable warning reporting
 *    	REPORT COUNT	1	Only report an IE exception once
 *    	LOGERR		1	Enable logging of errors
 *
 *    The remaining fields are left as-is, preserving the current values.  If we
 *    cannot set some of these fields, then we do our best.  Some drives may
 *    have a static configuration which still allows for some monitoring.
 *
 * 3. Check to see if the IE log page (page 0x2F) is supported by issuing a
 *    LOG SENSE command.
 *
 * 4. Check to see if the self-test log page (page 0x10) is supported.
 *
 * 5. Check to see if the temperature log page (page 0x0D) is supported, and
 *    contains a reference temperature.
 *
 * 6. Clear the GLTSD bit in control mode page 0xA.  This will allow the drive
 *    to save each of the log pages described above to nonvolatile storage.
 *    This is essential if the drive is to remember its failures across
 *    loss of power.
 */
static void *
ds_scsi_open_common(disk_status_t *dsp, ds_scsi_info_t *sip)
{
	boolean_t changed;

	sip->si_dsp = dsp;

	/* Load and validate mode pages */
	if (load_modepages(sip) != 0) {
		ds_scsi_close(sip);
		return (NULL);
	}

	/* Load and validate log pages */
	if (load_logpages(sip) != 0) {
		ds_scsi_close(sip);
		return (NULL);
	}

	/* Load IE state */
	if (load_ie_modepage(sip) != 0 ||
	    scsi_enable_ie(sip, &changed) != 0 ||
	    (changed && load_ie_modepage(sip) != 0)) {
		ds_scsi_close(sip);
		return (NULL);
	}

	/* Clear the GLTSD bit in the control page */
	if (sip->si_supp_log != 0 && clear_gltsd(sip) != 0) {
		ds_scsi_close(sip);
		return (NULL);
	}

	return (sip);
}

static void *
ds_scsi_open_uscsi(disk_status_t *dsp)
{
	ds_scsi_info_t *sip;

	if ((sip = calloc(sizeof (ds_scsi_info_t), 1)) == NULL) {
		(void) ds_set_errno(dsp, EDS_NOMEM);
		return (NULL);
	}

	return (ds_scsi_open_common(dsp, sip));
}

static void *
ds_scsi_open_sim(disk_status_t *dsp)
{
	ds_scsi_info_t *sip;

	if ((sip = calloc(sizeof (ds_scsi_info_t), 1)) == NULL) {
		(void) ds_set_errno(dsp, EDS_NOMEM);
		return (NULL);
	}

	if ((sip->si_sim = dlopen(dsp->ds_path, RTLD_LAZY)) == NULL) {
		(void) ds_set_errno(dsp, EDS_NO_TRANSPORT);
		free(sip);
		return (NULL);
	}

	return (ds_scsi_open_common(dsp, sip));
}


/*
 * Scan for any faults.  The following steps are performed:
 *
 * 1. If the temperature log page is supported, check the current temperature
 *    and threshold.  If the current temperature exceeds the threshold, report
 *    and overtemp fault.
 *
 * 2. If the selftest log page is supported, check to the last completed self
 *    test.  If the last completed test resulted in failure, report a selftest
 *    fault.
 *
 * 3. If the IE log page is supported, check to see if failure is predicted.  If
 *    so, indicate a predictive failure fault.
 *
 * 4. If the IE log page is not supported, but the mode page supports report on
 *    request mode, then issue a REQUEST SENSE for the mode page.  Indicate a
 *    predictive failure fault if necessary.
 */
static int
ds_scsi_scan(void *arg)
{
	ds_scsi_info_t *sip = arg;
	int i;

	for (i = 0; i < NLOG_VALIDATION; i++) {
		if ((sip->si_supp_log & log_validation[i].ve_supported) == 0)
			continue;

		if (analyze_one_logpage(sip, &log_validation[i]) != 0)
			return (-1);
	}

	if (!(sip->si_supp_log & LOGPAGE_SUPP_IE) &&
	    (sip->si_supp_mode & MODEPAGE_SUPP_IEC) &&
	    analyze_ie_sense(sip) != 0)
		return (-1);

	return (0);
}

ds_transport_t ds_scsi_uscsi_transport = {
	ds_scsi_open_uscsi,
	ds_scsi_close,
	ds_scsi_scan
};

ds_transport_t ds_scsi_sim_transport = {
	ds_scsi_open_sim,
	ds_scsi_close,
	ds_scsi_scan
};