xref: /onnv-gate/usr/src/uts/common/io/scsi/targets/ses_sen.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Enclosure Services Devices, SEN Enclosure Routines
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26*0Sstevel@tonic-gate  * Use is subject to license terms.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <sys/modctl.h>
31*0Sstevel@tonic-gate #include <sys/file.h>
32*0Sstevel@tonic-gate #include <sys/scsi/scsi.h>
33*0Sstevel@tonic-gate #include <sys/stat.h>
34*0Sstevel@tonic-gate #include <sys/scsi/targets/sesio.h>
35*0Sstevel@tonic-gate #include <sys/scsi/targets/ses.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * The SEN unit is wired to support 7 disk units,
40*0Sstevel@tonic-gate  * two power supplies, one fan module, one overtemp sensor,
41*0Sstevel@tonic-gate  * and one alarm.
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate #define	NOBJECTS	(7+2+1+1+1)
44*0Sstevel@tonic-gate #define	DRVOFF	0
45*0Sstevel@tonic-gate #define	SDRVOFF	20
46*0Sstevel@tonic-gate #define	NDRV	7
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	PWROFF	NDRV
49*0Sstevel@tonic-gate #define	SPWROFF	28
50*0Sstevel@tonic-gate #define	NPWR	2
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	FANOFF	(PWROFF + NPWR)
53*0Sstevel@tonic-gate #define	SFANOFF	30
54*0Sstevel@tonic-gate #define	NFAN	1
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	THMOFF	(FANOFF + NFAN)
57*0Sstevel@tonic-gate #define	STHMOFF	31
58*0Sstevel@tonic-gate #define	NTHM	1
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	ALRMOFF	(THMOFF + NTHM)
61*0Sstevel@tonic-gate #define	NALRM	1
62*0Sstevel@tonic-gate #define	SALRMOFF	8
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #define	SENPGINSIZE	32
65*0Sstevel@tonic-gate #define	SENPGOUTSIZE	22
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate int
sen_softc_init(ses_softc_t * ssc,int doinit)68*0Sstevel@tonic-gate sen_softc_init(ses_softc_t *ssc, int doinit)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	int i;
71*0Sstevel@tonic-gate 	if (doinit == 0) {
72*0Sstevel@tonic-gate 		mutex_enter(&ssc->ses_devp->sd_mutex);
73*0Sstevel@tonic-gate 		if (ssc->ses_nobjects) {
74*0Sstevel@tonic-gate 			kmem_free(ssc->ses_objmap,
75*0Sstevel@tonic-gate 			    ssc->ses_nobjects * sizeof (encobj));
76*0Sstevel@tonic-gate 			ssc->ses_objmap = NULL;
77*0Sstevel@tonic-gate 			ssc->ses_nobjects = 0;
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 		mutex_exit(&ssc->ses_devp->sd_mutex);
80*0Sstevel@tonic-gate 		return (0);
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 	mutex_enter(&ssc->ses_devp->sd_mutex);
83*0Sstevel@tonic-gate 	ssc->ses_nobjects = 0;
84*0Sstevel@tonic-gate 	ssc->ses_encstat = 0;
85*0Sstevel@tonic-gate 	ssc->ses_objmap = (encobj *)
86*0Sstevel@tonic-gate 	    kmem_zalloc(NOBJECTS * sizeof (encobj), KM_SLEEP);
87*0Sstevel@tonic-gate 	if (ssc->ses_objmap == NULL) {
88*0Sstevel@tonic-gate 		mutex_exit(&ssc->ses_devp->sd_mutex);
89*0Sstevel@tonic-gate 		return (ENOMEM);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 	for (i = DRVOFF; i < DRVOFF + NDRV; i++) {
92*0Sstevel@tonic-gate 		ssc->ses_objmap[i].enctype = SESTYP_DEVICE;
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 	for (i = PWROFF; i < PWROFF + NPWR; i++) {
95*0Sstevel@tonic-gate 		ssc->ses_objmap[i].enctype = SESTYP_POWER;
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 	for (i = FANOFF; i < FANOFF + NFAN; i++) {
98*0Sstevel@tonic-gate 		ssc->ses_objmap[i].enctype = SESTYP_FAN;
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	for (i = THMOFF; i < THMOFF + NTHM; i++) {
101*0Sstevel@tonic-gate 		ssc->ses_objmap[i].enctype = SESTYP_THERM;
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	for (i = ALRMOFF; i < ALRMOFF + NALRM; i++) {
104*0Sstevel@tonic-gate 		ssc->ses_objmap[i].enctype = SESTYP_ALARM;
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 	ssc->ses_nobjects = NOBJECTS;
107*0Sstevel@tonic-gate 	mutex_exit(&ssc->ses_devp->sd_mutex);
108*0Sstevel@tonic-gate 	return (0);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate int
sen_init_enc(ses_softc_t * ssc)112*0Sstevel@tonic-gate sen_init_enc(ses_softc_t *ssc)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	UNUSED_PARAMETER(ssc);
115*0Sstevel@tonic-gate 	return (0);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static int
sen_rdstat(ses_softc_t * ssc,int slpflag)119*0Sstevel@tonic-gate sen_rdstat(ses_softc_t *ssc, int slpflag)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	int err, i, oid, baseid, tmp;
122*0Sstevel@tonic-gate 	Uscmd local, *lp = &local;
123*0Sstevel@tonic-gate 	char rqbuf[SENSE_LENGTH], *sdata;
124*0Sstevel@tonic-gate 	static char cdb[CDB_GROUP0] =
125*0Sstevel@tonic-gate 	    { SCMD_GDIAG, 0x10, 0x4, 0, SENPGINSIZE, 0 };
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	/*
128*0Sstevel@tonic-gate 	 * Fetch current data
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 	sdata = kmem_alloc(SENPGINSIZE, slpflag);
131*0Sstevel@tonic-gate 	if (sdata == NULL)
132*0Sstevel@tonic-gate 		return (ENOMEM);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	lp->uscsi_flags = USCSI_READ|USCSI_RQENABLE;
135*0Sstevel@tonic-gate 	lp->uscsi_timeout = ses_io_time;
136*0Sstevel@tonic-gate 	lp->uscsi_cdb = cdb;
137*0Sstevel@tonic-gate 	lp->uscsi_bufaddr = sdata;
138*0Sstevel@tonic-gate 	lp->uscsi_buflen = SENPGINSIZE;
139*0Sstevel@tonic-gate 	lp->uscsi_cdblen = sizeof (cdb);
140*0Sstevel@tonic-gate 	lp->uscsi_rqbuf = rqbuf;
141*0Sstevel@tonic-gate 	lp->uscsi_rqlen = sizeof (rqbuf);
142*0Sstevel@tonic-gate 	err = ses_runcmd(ssc, lp);
143*0Sstevel@tonic-gate 	if (err) {
144*0Sstevel@tonic-gate 		kmem_free(sdata, SENPGINSIZE);
145*0Sstevel@tonic-gate 		return (err);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if ((lp->uscsi_buflen - lp->uscsi_resid)  < SENPGINSIZE) {
149*0Sstevel@tonic-gate 		SES_LOG(ssc, CE_NOTE, "sen_rdstat: too little data (%ld)",
150*0Sstevel@tonic-gate 		    lp->uscsi_buflen - lp->uscsi_resid);
151*0Sstevel@tonic-gate 		kmem_free(sdata, SENPGINSIZE);
152*0Sstevel@tonic-gate 		return (EIO);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * Set base SCSI id for drives...
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	if (sdata[10] & 0x80)
159*0Sstevel@tonic-gate 		baseid = 8;
160*0Sstevel@tonic-gate 	else
161*0Sstevel@tonic-gate 		baseid = 0;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	oid = 0;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	mutex_enter(&ssc->ses_devp->sd_mutex);
166*0Sstevel@tonic-gate 	/*
167*0Sstevel@tonic-gate 	 * Invalidate all status bits.
168*0Sstevel@tonic-gate 	 */
169*0Sstevel@tonic-gate 	for (i = 0; i < ssc->ses_nobjects; i++)
170*0Sstevel@tonic-gate 		ssc->ses_objmap[i].svalid = 0;
171*0Sstevel@tonic-gate 	ssc->ses_encstat = 0;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/*
174*0Sstevel@tonic-gate 	 * Do Drives...
175*0Sstevel@tonic-gate 	 */
176*0Sstevel@tonic-gate 	for (i = SDRVOFF; i < SDRVOFF + NDRV; i++) {
177*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[1] = baseid + i - SDRVOFF;
178*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[2] = 0;
179*0Sstevel@tonic-gate 		if (sdata[i] & 0x80) {
180*0Sstevel@tonic-gate 			/*
181*0Sstevel@tonic-gate 			 * Drive is present
182*0Sstevel@tonic-gate 			 */
183*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_OK;
184*0Sstevel@tonic-gate 		} else {
185*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_NOTINSTALLED;
186*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_INFO;
187*0Sstevel@tonic-gate 		}
188*0Sstevel@tonic-gate 		/*
189*0Sstevel@tonic-gate 		 * Is the fault LED lit?
190*0Sstevel@tonic-gate 		 */
191*0Sstevel@tonic-gate 		if (sdata[i] & 0x40) {
192*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_CRIT;
193*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x40;
194*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_CRITICAL;
195*0Sstevel@tonic-gate 		} else {
196*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x0;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		ssc->ses_objmap[oid++].svalid = 1;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	/*
202*0Sstevel@tonic-gate 	 * Do Power Supplies...
203*0Sstevel@tonic-gate 	 *
204*0Sstevel@tonic-gate 	 * Power supply bad, or not installed cannot be distinguished.
205*0Sstevel@tonic-gate 	 * Which one to pick? Let's say 'bad' and make it NONCRITICAL
206*0Sstevel@tonic-gate 	 * if only one is bad but CRITICAL if both are bad.
207*0Sstevel@tonic-gate 	 */
208*0Sstevel@tonic-gate 	for (tmp = 0, i = SPWROFF; i < SPWROFF + NPWR; i++) {
209*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[1] = 0;
210*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[2] = 0;
211*0Sstevel@tonic-gate 		if ((sdata[i] & 0x80) == 0) {
212*0Sstevel@tonic-gate 			/*
213*0Sstevel@tonic-gate 			 * Power supply 'ok'...
214*0Sstevel@tonic-gate 			 */
215*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_OK;
216*0Sstevel@tonic-gate 			tmp++;
217*0Sstevel@tonic-gate 		} else {
218*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_CRIT;
219*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_NONCRITICAL;
220*0Sstevel@tonic-gate 		}
221*0Sstevel@tonic-gate 		ssc->ses_objmap[oid++].svalid = 1;
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 	if (tmp == 0) {
224*0Sstevel@tonic-gate 		ssc->ses_encstat |= ENCSTAT_CRITICAL;
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	/*
228*0Sstevel@tonic-gate 	 *  Do the Fan(s)
229*0Sstevel@tonic-gate 	 */
230*0Sstevel@tonic-gate 	for (i = SFANOFF; i < SFANOFF + NFAN; i++) {
231*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[1] = 0;
232*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[2] = 0;
233*0Sstevel@tonic-gate 		if (sdata[i] & 0x20) {	/* both fans have failed */
234*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_CRIT;
235*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x40;
236*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_CRITICAL;
237*0Sstevel@tonic-gate 		} else if (sdata[i] & 0x80) {	/* one fan has failed */
238*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_NONCRIT;
239*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x41;
240*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_NONCRITICAL;
241*0Sstevel@tonic-gate 		} else {
242*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_OK;
243*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x6;
244*0Sstevel@tonic-gate 		}
245*0Sstevel@tonic-gate 		ssc->ses_objmap[oid++].svalid = 1;
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	/*
249*0Sstevel@tonic-gate 	 * Do the temperature sensor...
250*0Sstevel@tonic-gate 	 */
251*0Sstevel@tonic-gate 	for (i = STHMOFF; i < STHMOFF + NTHM; i++) {
252*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[1] = 0;
253*0Sstevel@tonic-gate 		if (sdata[i] & 0x80) {
254*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_CRIT;
255*0Sstevel@tonic-gate 			/* ssc->ses_objmap[oid].encstat[2] = 0; */
256*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x8;
257*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_CRITICAL;
258*0Sstevel@tonic-gate 		} else {
259*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_OK;
260*0Sstevel@tonic-gate 			/* ssc->ses_objmap[oid].encstat[2] = 0; */
261*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0;
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 		ssc->ses_objmap[oid++].svalid = 1;
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/*
267*0Sstevel@tonic-gate 	 * and last, but not least, check the state of the alarm.
268*0Sstevel@tonic-gate 	 */
269*0Sstevel@tonic-gate 	for (i = SALRMOFF; i < SALRMOFF + NALRM; i++) {
270*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[1] = 0;
271*0Sstevel@tonic-gate 		ssc->ses_objmap[oid].encstat[2] = 0;
272*0Sstevel@tonic-gate 		if (sdata[i]  & 0x80) {	/* Alarm is or was sounding */
273*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_CRIT;
274*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0x2;
275*0Sstevel@tonic-gate 			if ((sdata[i] & 0xf))
276*0Sstevel@tonic-gate 				ssc->ses_objmap[oid].encstat[3] |= 0x40;
277*0Sstevel@tonic-gate 			ssc->ses_encstat |= ENCSTAT_CRITICAL;
278*0Sstevel@tonic-gate 		} else {
279*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[0] = SESSTAT_OK;
280*0Sstevel@tonic-gate 			ssc->ses_objmap[oid].encstat[3] = 0;
281*0Sstevel@tonic-gate 		}
282*0Sstevel@tonic-gate 		ssc->ses_objmap[oid++].svalid = 1;
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 	ssc->ses_encstat |= ENCI_SVALID;
285*0Sstevel@tonic-gate 	mutex_exit(&ssc->ses_devp->sd_mutex);
286*0Sstevel@tonic-gate 	kmem_free(sdata, SENPGINSIZE);
287*0Sstevel@tonic-gate 	return (0);
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate int
sen_get_encstat(ses_softc_t * ssc,int slpflag)291*0Sstevel@tonic-gate sen_get_encstat(ses_softc_t *ssc, int slpflag)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	return (sen_rdstat(ssc, slpflag));
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate int
sen_set_encstat(ses_softc_t * ssc,uchar_t encstat,int slpflag)297*0Sstevel@tonic-gate sen_set_encstat(ses_softc_t *ssc, uchar_t encstat, int slpflag)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	UNUSED_PARAMETER(ssc);
300*0Sstevel@tonic-gate 	UNUSED_PARAMETER(encstat);
301*0Sstevel@tonic-gate 	UNUSED_PARAMETER(slpflag);
302*0Sstevel@tonic-gate 	return (0);
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate int
sen_get_objstat(ses_softc_t * ssc,ses_objarg * obp,int slpflag)306*0Sstevel@tonic-gate sen_get_objstat(ses_softc_t *ssc, ses_objarg *obp, int slpflag)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	int i = (int)obp->obj_id;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	if ((ssc->ses_encstat & ENCI_SVALID) == 0 ||
311*0Sstevel@tonic-gate 	    (ssc->ses_objmap[i].svalid) == 0) {
312*0Sstevel@tonic-gate 		int r = sen_rdstat(ssc, slpflag);
313*0Sstevel@tonic-gate 		if (r)
314*0Sstevel@tonic-gate 			return (r);
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 	obp->cstat[0] = ssc->ses_objmap[i].encstat[0];
317*0Sstevel@tonic-gate 	obp->cstat[1] = ssc->ses_objmap[i].encstat[1];
318*0Sstevel@tonic-gate 	obp->cstat[2] = ssc->ses_objmap[i].encstat[2];
319*0Sstevel@tonic-gate 	obp->cstat[3] = ssc->ses_objmap[i].encstat[3];
320*0Sstevel@tonic-gate 	return (0);
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate int
sen_set_objstat(ses_softc_t * ssc,ses_objarg * obp,int slpflag)325*0Sstevel@tonic-gate sen_set_objstat(ses_softc_t *ssc, ses_objarg *obp, int slpflag)
326*0Sstevel@tonic-gate {
327*0Sstevel@tonic-gate 	encobj *ep;
328*0Sstevel@tonic-gate 	int err, runcmd, idx;
329*0Sstevel@tonic-gate 	Uscmd local, *lp = &local;
330*0Sstevel@tonic-gate 	char rqbuf[SENSE_LENGTH], *sdata;
331*0Sstevel@tonic-gate 	static char cdb[CDB_GROUP0] =
332*0Sstevel@tonic-gate 	    { SCMD_GDIAG, 0x10, 0x4, 0, SENPGINSIZE, 0 };
333*0Sstevel@tonic-gate 	static char cdb1[CDB_GROUP0] =
334*0Sstevel@tonic-gate 	    { SCMD_SDIAG, 0x10, 0, 0, SENPGOUTSIZE, 0 };
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	/*
337*0Sstevel@tonic-gate 	 * If this is clear, we don't do diddly.
338*0Sstevel@tonic-gate 	 */
339*0Sstevel@tonic-gate 	if ((obp->cstat[0] & SESCTL_CSEL) == 0) {
340*0Sstevel@tonic-gate 		return (0);
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 	/*
343*0Sstevel@tonic-gate 	 * Fetch current data
344*0Sstevel@tonic-gate 	 */
345*0Sstevel@tonic-gate 	sdata = kmem_alloc(SENPGINSIZE, slpflag);
346*0Sstevel@tonic-gate 	if (sdata == NULL)
347*0Sstevel@tonic-gate 		return (ENOMEM);
348*0Sstevel@tonic-gate 	lp->uscsi_flags = USCSI_READ|USCSI_RQENABLE;
349*0Sstevel@tonic-gate 	lp->uscsi_timeout = ses_io_time;
350*0Sstevel@tonic-gate 	lp->uscsi_cdb = cdb;
351*0Sstevel@tonic-gate 	lp->uscsi_bufaddr = sdata;
352*0Sstevel@tonic-gate 	lp->uscsi_buflen = SENPGINSIZE;
353*0Sstevel@tonic-gate 	lp->uscsi_cdblen = sizeof (cdb);
354*0Sstevel@tonic-gate 	lp->uscsi_rqbuf = rqbuf;
355*0Sstevel@tonic-gate 	lp->uscsi_rqlen = sizeof (rqbuf);
356*0Sstevel@tonic-gate 	err = ses_runcmd(ssc, lp);
357*0Sstevel@tonic-gate 	if (err) {
358*0Sstevel@tonic-gate 		kmem_free(sdata, SENPGINSIZE);
359*0Sstevel@tonic-gate 		return (err);
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 	if ((lp->uscsi_buflen - lp->uscsi_resid)  < SENPGINSIZE) {
362*0Sstevel@tonic-gate 		SES_LOG(ssc, CE_NOTE, "Too Little Data Returned (%ld)",
363*0Sstevel@tonic-gate 		    lp->uscsi_buflen - lp->uscsi_resid);
364*0Sstevel@tonic-gate 		kmem_free(sdata, SENPGINSIZE);
365*0Sstevel@tonic-gate 		return (EIO);
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 	/*
368*0Sstevel@tonic-gate 	 * Okay, now convert the input page to the output page.
369*0Sstevel@tonic-gate 	 */
370*0Sstevel@tonic-gate 	sdata[1] = 0;
371*0Sstevel@tonic-gate 	sdata[3] = 0x12;
372*0Sstevel@tonic-gate 	sdata[6] = 1;
373*0Sstevel@tonic-gate 	sdata[8] &= ~0x80;
374*0Sstevel@tonic-gate 	sdata[10] = 0;
375*0Sstevel@tonic-gate 	sdata[14] = sdata[20] & ~0x80;
376*0Sstevel@tonic-gate 	sdata[15] = sdata[21] & ~0x80;
377*0Sstevel@tonic-gate 	sdata[16] = sdata[22] & ~0x80;
378*0Sstevel@tonic-gate 	sdata[17] = sdata[23] & ~0x80;
379*0Sstevel@tonic-gate 	sdata[18] = sdata[24] & ~0x80;
380*0Sstevel@tonic-gate 	sdata[19] = sdata[25] & ~0x80;
381*0Sstevel@tonic-gate 	sdata[20] = sdata[26] & ~0x80;
382*0Sstevel@tonic-gate 	sdata[21] = 0;
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	runcmd = 0;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	idx = (int)obp->obj_id;
387*0Sstevel@tonic-gate 	ep = &ssc->ses_objmap[idx];
388*0Sstevel@tonic-gate 	switch (ep->enctype) {
389*0Sstevel@tonic-gate 	case SESTYP_DEVICE:
390*0Sstevel@tonic-gate 		if (idx < 0 || idx >= NDRV) {
391*0Sstevel@tonic-gate 			err = EINVAL;
392*0Sstevel@tonic-gate 		} else if ((obp->cstat[3] & SESCTL_RQSFLT) != 0) {
393*0Sstevel@tonic-gate 			SES_LOG(ssc, SES_CE_DEBUG1, "faulted %d", idx);
394*0Sstevel@tonic-gate 			sdata[14 + idx] |= 0x40;
395*0Sstevel@tonic-gate 			runcmd++;
396*0Sstevel@tonic-gate 		} else {
397*0Sstevel@tonic-gate 			SES_LOG(ssc, SES_CE_DEBUG1, "clrd fault on %d", idx);
398*0Sstevel@tonic-gate 			sdata[14 + idx] &= ~0x40;
399*0Sstevel@tonic-gate 			runcmd++;
400*0Sstevel@tonic-gate 		}
401*0Sstevel@tonic-gate 		break;
402*0Sstevel@tonic-gate 	case SESTYP_POWER:
403*0Sstevel@tonic-gate 		if ((obp->cstat[3] & SESCTL_RQSTFAIL) ||
404*0Sstevel@tonic-gate 		    (obp->cstat[0] & SESCTL_DISABLE)) {
405*0Sstevel@tonic-gate 			SES_LOG(ssc, CE_WARN, "Commanding Off Power Supply!");
406*0Sstevel@tonic-gate 			sdata[10] |= 0x40;	/* Seppuku!!!! */
407*0Sstevel@tonic-gate 			runcmd++;
408*0Sstevel@tonic-gate 		}
409*0Sstevel@tonic-gate 		break;
410*0Sstevel@tonic-gate 	case SESTYP_ALARM:
411*0Sstevel@tonic-gate 		/*
412*0Sstevel@tonic-gate 		 * On all nonzero but the 'muted' bit,
413*0Sstevel@tonic-gate 		 * we turn on the alarm,
414*0Sstevel@tonic-gate 		 */
415*0Sstevel@tonic-gate 		obp->cstat[3] &= ~0xa;
416*0Sstevel@tonic-gate 		if ((obp->cstat[3] & 0x40) ||
417*0Sstevel@tonic-gate 		    (obp->cstat[0] & SESCTL_DISABLE)) {
418*0Sstevel@tonic-gate 			sdata[8] = 0;
419*0Sstevel@tonic-gate 		} else if (obp->cstat[3] != 0) {
420*0Sstevel@tonic-gate 			sdata[8] = 0x40;
421*0Sstevel@tonic-gate 		} else {
422*0Sstevel@tonic-gate 			sdata[8] = 0;
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate 		runcmd++;
425*0Sstevel@tonic-gate 		SES_LOG(ssc, SES_CE_DEBUG1, "%sabling alarm",
426*0Sstevel@tonic-gate 		    (sdata[8] & 0x40)? "en" : "dis");
427*0Sstevel@tonic-gate 		break;
428*0Sstevel@tonic-gate 	default:
429*0Sstevel@tonic-gate 		break;
430*0Sstevel@tonic-gate 	}
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	if (runcmd) {
433*0Sstevel@tonic-gate 		lp->uscsi_flags = USCSI_WRITE|USCSI_RQENABLE;
434*0Sstevel@tonic-gate 		lp->uscsi_timeout = ses_io_time;
435*0Sstevel@tonic-gate 		lp->uscsi_cdb = cdb1;
436*0Sstevel@tonic-gate 		lp->uscsi_bufaddr = sdata;
437*0Sstevel@tonic-gate 		lp->uscsi_buflen = SENPGOUTSIZE;
438*0Sstevel@tonic-gate 		lp->uscsi_cdblen = sizeof (cdb);
439*0Sstevel@tonic-gate 		lp->uscsi_rqbuf = rqbuf;
440*0Sstevel@tonic-gate 		lp->uscsi_rqlen = sizeof (rqbuf);
441*0Sstevel@tonic-gate 		err = ses_runcmd(ssc, lp);
442*0Sstevel@tonic-gate 		/* preserve error across the rest of the action */
443*0Sstevel@tonic-gate 	} else {
444*0Sstevel@tonic-gate 		err = 0;
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	mutex_enter(&ssc->ses_devp->sd_mutex);
448*0Sstevel@tonic-gate 	ep->svalid = 0;
449*0Sstevel@tonic-gate 	mutex_exit(&ssc->ses_devp->sd_mutex);
450*0Sstevel@tonic-gate 	kmem_free(sdata, SENPGINSIZE);
451*0Sstevel@tonic-gate 	return (err);
452*0Sstevel@tonic-gate }
453*0Sstevel@tonic-gate /*
454*0Sstevel@tonic-gate  * mode: c
455*0Sstevel@tonic-gate  * Local variables:
456*0Sstevel@tonic-gate  * c-indent-level: 8
457*0Sstevel@tonic-gate  * c-brace-imaginary-offset: 0
458*0Sstevel@tonic-gate  * c-brace-offset: -8
459*0Sstevel@tonic-gate  * c-argdecl-indent: 8
460*0Sstevel@tonic-gate  * c-label-offset: -8
461*0Sstevel@tonic-gate  * c-continued-statement-offset: 8
462*0Sstevel@tonic-gate  * c-continued-brace-offset: 0
463*0Sstevel@tonic-gate  * End:
464*0Sstevel@tonic-gate  */
465