1 /* @(#)scsi-hpux.c	1.31 04/01/15 Copyright 1997 J. Schilling */
2 #ifndef lint
3 static	char __sccsid[] =
4 	"@(#)scsi-hpux.c	1.31 04/01/15 Copyright 1997 J. Schilling";
5 #endif
6 /*
7  *	Interface for the HP-UX generic SCSI implementation.
8  *
9  *	Warning: you may change this source, but if you do that
10  *	you need to change the _scg_version and _scg_auth* string below.
11  *	You may not return "schily" for an SCG_AUTHOR request anymore.
12  *	Choose your name instead of "schily" and make clear that the version
13  *	string is related to a modified source.
14  *
15  *	Copyright (c) 1997 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2, or (at your option)
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License along with
29  * this program; see the file COPYING.  If not, write to the Free Software
30  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31  */
32 
33 #undef	sense
34 #include <sys/scsi.h>
35 
36 /*
37  *	Warning: you may change this source, but if you do that
38  *	you need to change the _scg_version and _scg_auth* string below.
39  *	You may not return "schily" for an SCG_AUTHOR request anymore.
40  *	Choose your name instead of "schily" and make clear that the version
41  *	string is related to a modified source.
42  */
43 LOCAL	char	_scg_trans_version[] = "scsi-hpux.c-1.31";	/* The version for this transport*/
44 
45 #define	MAX_SCG		16	/* Max # of SCSI controllers */
46 #define	MAX_TGT		16
47 #define	MAX_LUN		8
48 
49 struct scg_local {
50 	short	scgfiles[MAX_SCG][MAX_TGT][MAX_LUN];
51 };
52 #define	scglocal(p)	((struct scg_local *)((p)->local))
53 
54 #ifdef	SCSI_MAXPHYS
55 #	define	MAX_DMA_HP	SCSI_MAXPHYS
56 #else
57 #	define	MAX_DMA_HP	(63*1024)	/* Check if this is not too big */
58 #endif
59 
60 
61 /*
62  * Return version information for the low level SCSI transport code.
63  * This has been introduced to make it easier to trace down problems
64  * in applications.
65  */
66 LOCAL char *
scgo_version(scgp,what)67 scgo_version(scgp, what)
68 	SCSI	*scgp;
69 	int	what;
70 {
71 	if (scgp != (SCSI *)0) {
72 		switch (what) {
73 
74 		case SCG_VERSION:
75 			return (_scg_trans_version);
76 		/*
77 		 * If you changed this source, you are not allowed to
78 		 * return "schily" for the SCG_AUTHOR request.
79 		 */
80 		case SCG_AUTHOR:
81 			return (_scg_auth_schily);
82 		case SCG_SCCS_ID:
83 			return (__sccsid);
84 		}
85 	}
86 	return ((char *)0);
87 }
88 
89 LOCAL int
scgo_help(scgp,f)90 scgo_help(scgp, f)
91 	SCSI	*scgp;
92 	FILE	*f;
93 {
94 	__scg_help(f, "SIOC", "Generic SCSI",
95 		"", "bus,target,lun", "1,2,0", TRUE, FALSE);
96 	return (0);
97 }
98 
99 LOCAL int
scgo_open(scgp,device)100 scgo_open(scgp, device)
101 	SCSI	*scgp;
102 	char	*device;
103 {
104 		int	busno	= scg_scsibus(scgp);
105 		int	tgt	= scg_target(scgp);
106 		int	tlun	= scg_lun(scgp);
107 	register int	f;
108 	register int	b;
109 	register int	t;
110 	register int	l;
111 	register int	nopen = 0;
112 	char		devname[64];
113 
114 	if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
115 		errno = EINVAL;
116 		if (scgp->errstr)
117 			js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
118 				"Illegal value for busno, target or lun '%d,%d,%d'",
119 				busno, tgt, tlun);
120 		return (-1);
121 	}
122 
123 	if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
124 		errno = EINVAL;
125 		if (scgp->errstr)
126 			js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
127 				"Open by 'devname' not supported on this OS");
128 		return (-1);
129 	}
130 
131 	if (scgp->local == NULL) {
132 		scgp->local = malloc(sizeof (struct scg_local));
133 		if (scgp->local == NULL)
134 			return (0);
135 
136 		for (b = 0; b < MAX_SCG; b++) {
137 			for (t = 0; t < MAX_TGT; t++) {
138 				for (l = 0; l < MAX_LUN; l++)
139 					scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
140 			}
141 		}
142 	}
143 
144 	if (busno >= 0 && tgt >= 0 && tlun >= 0) {
145 
146 		js_snprintf(devname, sizeof (devname),
147 				"/dev/rscsi/c%xt%xl%x", busno, tgt, tlun);
148 		f = open(devname, O_RDWR);
149 		if (f < 0)
150 			return (-1);
151 		scglocal(scgp)->scgfiles[busno][tgt][tlun] = f;
152 		return (1);
153 	} else {
154 		for (b = 0; b < MAX_SCG; b++) {
155 			for (t = 0; t < MAX_TGT; t++) {
156 /*				for (l = 0; l < MAX_LUN; l++) {*/
157 				for (l = 0; l < 1; l++) {
158 					js_snprintf(devname, sizeof (devname),
159 							"/dev/rscsi/c%xt%xl%x", b, t, l);
160 /*error("name: '%s'\n", devname);*/
161 					f = open(devname, O_RDWR);
162 					if (f >= 0) {
163 						scglocal(scgp)->scgfiles[b][t][l] = (short)f;
164 						nopen++;
165 					} else if (scgp->debug > 0) {
166 						errmsg("open '%s'\n", devname);
167 					}
168 				}
169 			}
170 		}
171 	}
172 	return (nopen);
173 }
174 
175 LOCAL int
scgo_close(scgp)176 scgo_close(scgp)
177 	SCSI	*scgp;
178 {
179 	register int	f;
180 	register int	b;
181 	register int	t;
182 	register int	l;
183 
184 	if (scgp->local == NULL)
185 		return (-1);
186 
187 	for (b = 0; b < MAX_SCG; b++) {
188 		for (t = 0; t < MAX_TGT; t++) {
189 			for (l = 0; l < MAX_LUN; l++) {
190 				f = scglocal(scgp)->scgfiles[b][t][l];
191 				if (f >= 0)
192 					close(f);
193 				scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
194 			}
195 		}
196 	}
197 	return (0);
198 }
199 
200 LOCAL long
scgo_maxdma(scgp,amt)201 scgo_maxdma(scgp, amt)
202 	SCSI	*scgp;
203 	long	amt;
204 {
205 	return	(MAX_DMA_HP);
206 }
207 
208 LOCAL void *
scgo_getbuf(scgp,amt)209 scgo_getbuf(scgp, amt)
210 	SCSI	*scgp;
211 	long	amt;
212 {
213 	if (scgp->debug > 0) {
214 		js_fprintf((FILE *)scgp->errfile,
215 			"scgo_getbuf: %ld bytes\n", amt);
216 	}
217 	scgp->bufbase = valloc((size_t)(amt));
218 	return (scgp->bufbase);
219 }
220 
221 LOCAL void
scgo_freebuf(scgp)222 scgo_freebuf(scgp)
223 	SCSI	*scgp;
224 {
225 	if (scgp->bufbase)
226 		free(scgp->bufbase);
227 	scgp->bufbase = NULL;
228 }
229 
230 LOCAL BOOL
scgo_havebus(scgp,busno)231 scgo_havebus(scgp, busno)
232 	SCSI	*scgp;
233 	int	busno;
234 {
235 	register int	t;
236 	register int	l;
237 
238 	if (busno < 0 || busno >= MAX_SCG)
239 		return (FALSE);
240 
241 	if (scgp->local == NULL)
242 		return (FALSE);
243 
244 	for (t = 0; t < MAX_TGT; t++) {
245 		for (l = 0; l < MAX_LUN; l++)
246 			if (scglocal(scgp)->scgfiles[busno][t][l] >= 0)
247 				return (TRUE);
248 	}
249 	return (FALSE);
250 }
251 
252 LOCAL int
scgo_fileno(scgp,busno,tgt,tlun)253 scgo_fileno(scgp, busno, tgt, tlun)
254 	SCSI	*scgp;
255 	int	busno;
256 	int	tgt;
257 	int	tlun;
258 {
259 	if (busno < 0 || busno >= MAX_SCG ||
260 	    tgt < 0 || tgt >= MAX_TGT ||
261 	    tlun < 0 || tlun >= MAX_LUN)
262 		return (-1);
263 
264 	if (scgp->local == NULL)
265 		return (-1);
266 
267 	return ((int)scglocal(scgp)->scgfiles[busno][tgt][tlun]);
268 }
269 
270 LOCAL int
scgo_initiator_id(scgp)271 scgo_initiator_id(scgp)
272 	SCSI	*scgp;
273 {
274 	return (-1);
275 }
276 
277 LOCAL int
scgo_isatapi(scgp)278 scgo_isatapi(scgp)
279 	SCSI	*scgp;
280 {
281 	return (FALSE);
282 }
283 
284 LOCAL int
scgo_reset(scgp,what)285 scgo_reset(scgp, what)
286 	SCSI	*scgp;
287 	int	what;
288 {
289 	if (what == SCG_RESET_NOP)
290 		return (0);
291 	if (what != SCG_RESET_BUS) {
292 		errno = EINVAL;
293 		return (-1);
294 	}
295 	return (ioctl(scgp->fd, SIOC_RESET_BUS, 0));
296 }
297 
298 LOCAL int
scgo_send(scgp)299 scgo_send(scgp)
300 	SCSI		*scgp;
301 {
302 	struct scg_cmd	*sp = scgp->scmd;
303 	int		ret;
304 	int		flags;
305 	struct sctl_io	sctl_io;
306 
307 	if ((scgp->fd < 0) || (sp->cdb_len > sizeof (sctl_io.cdb))) {
308 		sp->error = SCG_FATAL;
309 		return (0);
310 	}
311 
312 	fillbytes((caddr_t)&sctl_io, sizeof (sctl_io), '\0');
313 
314 	flags = 0;
315 /*	flags = SCTL_INIT_WDTR|SCTL_INIT_SDTR;*/
316 	if (sp->flags & SCG_RECV_DATA)
317 		flags |= SCTL_READ;
318 	if ((sp->flags & SCG_DISRE_ENA) == 0)
319 		flags |= SCTL_NO_ATN;
320 
321 	sctl_io.flags		= flags;
322 
323 	movebytes(&sp->cdb, sctl_io.cdb, sp->cdb_len);
324 	sctl_io.cdb_length	= sp->cdb_len;
325 
326 	sctl_io.data_length	= sp->size;
327 	sctl_io.data		= sp->addr;
328 
329 	if (sp->timeout == 0)
330 		sctl_io.max_msecs = 0;
331 	else
332 		sctl_io.max_msecs = (sp->timeout * 1000) + 500;
333 
334 	errno		= 0;
335 	sp->error	= SCG_NO_ERROR;
336 	sp->sense_count	= 0;
337 	sp->u_scb.cmd_scb[0] = 0;
338 	sp->resid	= 0;
339 
340 	ret = ioctl(scgp->fd, SIOC_IO, &sctl_io);
341 	if (ret < 0) {
342 		sp->error = SCG_FATAL;
343 		sp->ux_errno = errno;
344 		return (ret);
345 	}
346 if (scgp->debug > 0)
347 error("cdb_status: %X, size: %d xfer: %d\n", sctl_io.cdb_status, sctl_io.data_length, sctl_io.data_xfer);
348 
349 	if (sctl_io.cdb_status == 0 || sctl_io.cdb_status == 0x02)
350 		sp->resid = sp->size - sctl_io.data_xfer;
351 
352 	if (sctl_io.cdb_status & SCTL_SELECT_TIMEOUT ||
353 			sctl_io.cdb_status & SCTL_INVALID_REQUEST) {
354 		sp->error = SCG_FATAL;
355 	} else if (sctl_io.cdb_status & SCTL_INCOMPLETE) {
356 		sp->error = SCG_TIMEOUT;
357 	} else if (sctl_io.cdb_status > 0xFF) {
358 		errmsgno(EX_BAD, "SCSI problems: cdb_status: %X\n", sctl_io.cdb_status);
359 
360 	} else if ((sctl_io.cdb_status & 0xFF) != 0) {
361 		sp->error = SCG_RETRYABLE;
362 		sp->ux_errno = EIO;
363 
364 		sp->u_scb.cmd_scb[0] = sctl_io.cdb_status & 0xFF;
365 
366 		sp->sense_count = sctl_io.sense_xfer;
367 		if (sp->sense_count > SCG_MAX_SENSE)
368 			sp->sense_count = SCG_MAX_SENSE;
369 
370 		if (sctl_io.sense_status != S_GOOD) {
371 			sp->sense_count = 0;
372 		} else {
373 			movebytes(sctl_io.sense, sp->u_sense.cmd_sense, sp->sense_count);
374 		}
375 
376 	}
377 	return (ret);
378 }
379 #define	sense	u_sense.Sense
380