1 /* @(#)scsi-linux-pg.c	1.43 04/01/15 Copyright 1997 J. Schilling */
2 #ifndef lint
3 static	char ___sccsid[] =
4 	"@(#)scsi-linux-pg.c	1.43 04/01/15 Copyright 1997 J. Schilling";
5 #endif
6 /*
7  *	Interface for the Linux PARIDE implementation.
8  *
9  *	We emulate the functionality of the scg driver, via the pg driver.
10  *
11  *	Warning: you may change this source, but if you do that
12  *	you need to change the _scg_version and _scg_auth* string below.
13  *	You may not return "schily" for an SCG_AUTHOR request anymore.
14  *	Choose your name instead of "schily" and make clear that the version
15  *	string is related to a modified source.
16  *
17  *	Copyright (c) 1997  J. Schilling
18  *	Copyright (c) 1998  Grant R. Guenther	<grant@torque.net>
19  *			    Under the terms of the GNU public license.
20  */
21 /*
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2, or (at your option)
25  * any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License along with
33  * this program; see the file COPYING.  If not, write to the Free Software
34  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35  */
36 
37 #include <string.h>
38 #ifdef	HAVE_LINUX_PG_H
39 #include <linux/pg.h>
40 #else
41 #include "pg.h"		/* Use local version as Linux sometimes doesn't have */
42 #endif			/* installed. Now libscg always supports PP SCSI    */
43 
44 /*
45  *	Warning: you may change this source, but if you do that
46  *	you need to change the _scg_version and _scg_auth* string below.
47  *	You may not return "schily" for an SCG_AUTHOR request anymore.
48  *	Choose your name instead of "schily" and make clear that the version
49  *	string is related to a modified source.
50  */
51 LOCAL	char	_scg_trans_version_pg[] = "scsi-linux-pg.c-1.43";	/* The version for this transport*/
52 
53 #ifdef	USE_PG_ONLY
54 
55 #define	MAX_SCG		1	/* Max # of SCSI controllers */
56 #define	MAX_TGT		8
57 #define	MAX_LUN		8
58 
59 struct scg_local {
60 	short	scgfiles[MAX_SCG][MAX_TGT][MAX_LUN];
61 	short	buscookies[MAX_SCG];
62 	int	pgbus;
63 	char	*SCSIbuf;
64 };
65 #define	scglocal(p)	((struct scg_local *)((p)->local))
66 
67 #else
68 
69 #define	scgo_version	pg_version
70 #define	scgo_help	pg_help
71 #define	scgo_open	pg_open
72 #define	scgo_close	pg_close
73 #define	scgo_send	pg_send
74 #define	scgo_maxdma	pg_maxdma
75 #define	scgo_initiator_id pg_initiator_id
76 #define	scgo_isatapi	pg_isatapi
77 #define	scgo_reset	pg_reset
78 
79 LOCAL	char	*pg_version	__PR((SCSI *scgp, int what));
80 LOCAL	int	pg_help		__PR((SCSI *scgp, FILE *f));
81 LOCAL	int	pg_open		__PR((SCSI *scgp, char *device));
82 LOCAL	int	pg_close	__PR((SCSI *scgp));
83 LOCAL	long	pg_maxdma	__PR((SCSI *scgp, long amt));
84 LOCAL	int 	pg_initiator_id	__PR((SCSI *scgp));
85 LOCAL	int 	pg_isatapi	__PR((SCSI *scgp));
86 LOCAL	int	pg_reset	__PR((SCSI *scgp, int what));
87 LOCAL	int	pg_send		__PR((SCSI *scgp));
88 
89 #endif
90 
91 LOCAL	int	do_scg_cmd	__PR((SCSI *scgp, struct scg_cmd *sp));
92 LOCAL	int	do_scg_sense	__PR((SCSI *scgp, struct scg_cmd *sp));
93 
94 
95 /*
96  * Return version information for the low level SCSI transport code.
97  * This has been introduced to make it easier to trace down problems
98  * in applications.
99  */
100 LOCAL char *
scgo_version(scgp,what)101 scgo_version(scgp, what)
102 	SCSI	*scgp;
103 	int	what;
104 {
105 	if (scgp != (SCSI *)0) {
106 		switch (what) {
107 
108 		case SCG_VERSION:
109 			return (_scg_trans_version_pg);
110 		/*
111 		 * If you changed this source, you are not allowed to
112 		 * return "schily" for the SCG_AUTHOR request.
113 		 */
114 		case SCG_AUTHOR:
115 			return (_scg_auth_schily);
116 		case SCG_SCCS_ID:
117 			return (___sccsid);
118 		}
119 	}
120 	return ((char *)0);
121 }
122 
123 LOCAL int
scgo_help(scgp,f)124 scgo_help(scgp, f)
125 	SCSI	*scgp;
126 	FILE	*f;
127 {
128 	__scg_help(f, "pg", "SCSI transport for ATAPI over Parallel Port",
129 		"", "bus,target,lun", "1,2,0", TRUE, FALSE);
130 	return (0);
131 }
132 
133 #include <glob.h>
134 
135 LOCAL int
scgo_open(scgp,device)136 scgo_open(scgp, device)
137 	SCSI	*scgp;
138 	char	*device;
139 {
140 		int	busno	= scg_scsibus(scgp);
141 		int	tgt	= scg_target(scgp);
142 		int	tlun	= scg_lun(scgp);
143 	register int	f;
144 	register int	b;
145 #ifdef	USE_PG_ONLY
146 	register int	t;
147 	register int	l;
148 #endif
149 	register int	nopen = 0;
150 	char		devname[32];
151         glob_t globbuf;
152         int i;
153 
154 	if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
155 		errno = EINVAL;
156 		if (scgp->errstr)
157 			js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
158 				"Illegal value for busno, target or lun '%d,%d,%d'",
159 				busno, tgt, tlun);
160 		return (-1);
161 	}
162 
163 #ifndef	USE_PG_ONLY
164 	/*
165 	 * We need to find a fake bus number for the parallel port interface.
166 	 * Unfortunatly, the buscookie array may contain holes if
167 	 * SCSI_IOCTL_GET_BUS_NUMBER works, so we are searching backwards
168 	 * for some place for us.
169 	 * XXX Should add extra space in buscookies for a "PP bus".
170 	 */
171 
172 	if (scglocal(scgp)->buscookies[MAX_SCG-1] != (short)-1)
173 		return (0);			/* No space for pgbus */
174 
175 	for (b = MAX_SCG-1; b >= 0; b--) {
176 		if (scglocal(scgp)->buscookies[b] != (short)-1) {
177 			scglocal(scgp)->pgbus = ++b;
178 			break;
179 		}
180 	}
181 	if (scgp->debug > 0) {
182 		js_fprintf((FILE *)scgp->errfile,
183 			"PP Bus: %d\n", scglocal(scgp)->pgbus);
184 	}
185 #else
186 	if (scgp->local == NULL) {
187 		scgp->local = malloc(sizeof (struct scg_local));
188 		if (scgp->local == NULL)
189 			return (0);
190 
191 		scglocal(scgp)->pgbus = -2;
192 		scglocal(scgp)->SCSIbuf = (char *)-1;
193 
194 		for (b = 0; b < MAX_SCG; b++) {
195 			for (t = 0; t < MAX_TGT; t++) {
196 				for (l = 0; l < MAX_LUN; l++)
197 					scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
198 			}
199 		}
200 	}
201 #endif
202 	if (scglocal(scgp)->pgbus < 0)
203 		scglocal(scgp)->pgbus = 0;
204 
205 	if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2))
206 		goto openbydev;
207 
208 	if (busno >= 0 && tgt >= 0 && tlun >= 0) {
209 #ifndef	USE_PG_ONLY
210 		if (scglocal(scgp)->pgbus != busno)
211 			return (0);
212 #endif
213 		js_snprintf(devname, sizeof (devname), "/dev/pg%d", tgt);
214 		f = open(devname, O_RDWR | O_NONBLOCK);
215 		if (f < 0) {
216 			if (scgp->errstr)
217 				js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
218 						"Cannot open '%s'", devname);
219 			return (0);
220 		}
221 		scglocal(scgp)->scgfiles[busno][tgt][tlun] = f;
222 		return (1);
223 	} else {
224 		const char *dev;
225 		tlun = 0;
226 		glob("/dev/pg[0-9]", GLOB_NOSORT, NULL, &globbuf);
227 		glob("/dev/pg[0-9][0-9]", GLOB_NOSORT|GLOB_APPEND, NULL, &globbuf);
228 		for (i = 0; globbuf.gl_pathv && globbuf.gl_pathv[i] != NULL ; i++) {
229 		        dev = globbuf.gl_pathv[i];
230 			tgt = atoi(&dev[7]);
231 			f = open(dev, O_RDWR | O_NONBLOCK);
232 			if (f < 0) {
233 				/*
234 				 * Set up error string but let us clear it later
235 				 * if at least one open succeeded.
236 				 */
237 				if (scgp->errstr)
238 					js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
239 							"Cannot open '/dev/pg*'");
240 				if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
241 					if (scgp->errstr)
242 						js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
243 							"Cannot open '%s'", dev);
244 					globfree(&globbuf);
245 					return (0);
246 				}
247 			} else {
248 				scglocal(scgp)->scgfiles[scglocal(scgp)->pgbus][tgt][tlun] = f;
249 				nopen++;
250 			}
251 		}
252 		globfree(&globbuf);
253 
254 	}
255 	if (nopen > 0 && scgp->errstr)
256 		scgp->errstr[0] = '\0';
257 
258 openbydev:
259 	if (device != NULL && *device != '\0') {
260 		char	*p;
261 
262 		if (tlun < 0)
263 			return (0);
264 		f = open(device, O_RDWR | O_NONBLOCK);
265 /*		if (f < 0 && errno == ENOENT) {*/
266 		if (f < 0) {
267 			if (scgp->errstr)
268 				js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
269 					"Cannot open '%s'",
270 					device);
271 			return (0);
272 		}
273 
274 		p = device + strlen(device) -1;
275 		tgt = *p - '0';
276 		if (tgt < 0 || tgt > 9)
277 			return (0);
278 		scglocal(scgp)->scgfiles[scglocal(scgp)->pgbus][tgt][tlun] = f;
279 		scg_settarget(scgp, scglocal(scgp)->pgbus, tgt, tlun);
280 
281 		return (++nopen);
282 	}
283 	return (nopen);
284 }
285 
286 LOCAL int
scgo_close(scgp)287 scgo_close(scgp)
288 	SCSI	*scgp;
289 {
290 	register int	f;
291 	register int	b;
292 	register int	t;
293 	register int	l;
294 
295 	if (scgp->local == NULL)
296 		return (-1);
297 	if (scglocal(scgp)->pgbus < 0)
298 		return (0);
299 	b = scglocal(scgp)->pgbus;
300 	scglocal(scgp)->buscookies[b] = (short)-1;
301 
302 	for (t = 0; t < MAX_TGT; t++) {
303 		for (l = 0; l < MAX_LUN; l++) {
304 			f = scglocal(scgp)->scgfiles[b][t][l];
305 			if (f >= 0)
306 				close(f);
307 			scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
308 		}
309 	}
310 	return (0);
311 }
312 
313 LOCAL long
scgo_maxdma(scgp,amt)314 scgo_maxdma(scgp, amt)
315 	SCSI	*scgp;
316 	long	amt;
317 {
318 	return (PG_MAX_DATA);
319 }
320 
321 #ifdef	USE_PG_ONLY
322 
323 LOCAL void *
scgo_getbuf(scgp,amt)324 scgo_getbuf(scgp, amt)
325 	SCSI	*scgp;
326 	long	amt;
327 {
328 	char    *ret;
329 
330 	if (scgp->debug > 0) {
331 		js_fprintf((FILE *)scgp->errfile,
332 			"scgo_getbuf: %ld bytes\n", amt);
333 	}
334 	ret = valloc((size_t)(amt+getpagesize()));
335 	if (ret == NULL)
336 		return (ret);
337 	scgp->bufbase = ret;
338 	ret += getpagesize();
339 	scglocal(scgp)->SCSIbuf = ret;
340 	return ((void *)ret);
341 
342 }
343 
344 LOCAL void
scgo_freebuf(scgp)345 scgo_freebuf(scgp)
346 	SCSI	*scgp;
347 {
348 	if (scgp->bufbase)
349 		free(scgp->bufbase);
350 	scgp->bufbase = NULL;
351 }
352 
353 LOCAL BOOL
scgo_havebus(scgp,busno)354 scgo_havebus(scgp, busno)
355 	SCSI	*scgp;
356 	int	busno;
357 {
358 	register int	t;
359 	register int	l;
360 
361 	if (busno < 0 || busno >= MAX_SCG)
362 		return (FALSE);
363 
364 	if (scgp->local == NULL)
365 		return (FALSE);
366 
367 	for (t = 0; t < MAX_TGT; t++) {
368 		for (l = 0; l < MAX_LUN; l++)
369 			if (scglocal(scgp)->scgfiles[busno][t][l] >= 0)
370 				return (TRUE);
371 	}
372 	return (FALSE);
373 }
374 
375 LOCAL int
scgo_fileno(scgp,busno,tgt,tlun)376 scgo_fileno(scgp, busno, tgt, tlun)
377 	SCSI	*scgp;
378 	int	busno;
379 	int	tgt;
380 	int	tlun;
381 {
382 	if (busno < 0 || busno >= MAX_SCG ||
383 	    tgt < 0 || tgt >= MAX_TGT ||
384 	    tlun < 0 || tlun >= MAX_LUN)
385 		return (-1);
386 
387 	if (scgp->local == NULL)
388 		return (-1);
389 
390 	return ((int)scglocal(scgp)->scgfiles[busno][tgt][tlun]);
391 }
392 #endif	/* USE_PG_ONLY */
393 
394 LOCAL int
scgo_initiator_id(scgp)395 scgo_initiator_id(scgp)
396 	SCSI	*scgp;
397 {
398 	return (-1);
399 }
400 
401 LOCAL int
scgo_isatapi(scgp)402 scgo_isatapi(scgp)
403 	SCSI	*scgp;
404 {
405 	return (TRUE);
406 }
407 
408 LOCAL int
scgo_reset(scgp,what)409 scgo_reset(scgp, what)
410 	SCSI	*scgp;
411 	int	what;
412 {
413 	struct pg_write_hdr hdr = {PG_MAGIC, PG_RESET, 0};
414 
415 	if (what == SCG_RESET_NOP)
416 		return (0);
417 	if (what != SCG_RESET_BUS) {
418 		errno = EINVAL;
419 		return (-1);
420 	}
421 	/*
422 	 * XXX Does this reset TGT or BUS ???
423 	 */
424 	return (write(scgp->fd, (char *)&hdr, sizeof (hdr)));
425 
426 }
427 
428 #ifndef MAX
429 #define	MAX(a, b)	((a) > (b) ? (a):(b))
430 #endif
431 
432 #define	RHSIZE	sizeof (struct pg_read_hdr)
433 #define	WHSIZE  sizeof (struct pg_write_hdr)
434 #define	LEAD	MAX(RHSIZE, WHSIZE)
435 
436 LOCAL int
do_scg_cmd(scgp,sp)437 do_scg_cmd(scgp, sp)
438 	SCSI	*scgp;
439 	struct scg_cmd	*sp;
440 {
441 
442 	char	local[LEAD+PG_MAX_DATA];
443 	int	use_local, i, r;
444 	int	inward = (sp->flags & SCG_RECV_DATA);
445 
446 	struct pg_write_hdr *whp;
447 	struct pg_read_hdr  *rhp;
448 	char		    *dbp;
449 
450 	if (sp->cdb_len > 12)
451 		comerrno(EX_BAD, "Can't do %d byte command.\n", sp->cdb_len);
452 
453 	if (sp->addr == scglocal(scgp)->SCSIbuf) {
454 		use_local = 0;
455 		dbp = sp->addr;
456 	} else {
457 		use_local = 1;
458 		dbp = &local[LEAD];
459 		if (!inward)
460 			movebytes(sp->addr, dbp, sp->size);
461 	}
462 
463 	whp = (struct pg_write_hdr *)(dbp - WHSIZE);
464 	rhp = (struct pg_read_hdr *)(dbp - RHSIZE);
465 
466 	whp->magic   = PG_MAGIC;
467 	whp->func    = PG_COMMAND;
468 	whp->dlen    = sp->size;
469 	whp->timeout = sp->timeout;
470 
471 	for (i = 0; i < 12; i++) {
472 		if (i < sp->cdb_len)
473 			whp->packet[i] = sp->cdb.cmd_cdb[i];
474 		else
475 			whp->packet[i] = 0;
476 	}
477 
478 	i = WHSIZE;
479 	if (!inward)
480 		i += sp->size;
481 
482 	r = write(scgp->fd, (char *)whp, i);
483 
484 	if (r < 0) {				/* command was not sent */
485 		sp->ux_errno = geterrno();
486 		if (sp->ux_errno == ETIME) {
487 			/*
488 			 * If the previous command timed out, we cannot send
489 			 * any further command until the command in the drive
490 			 * is ready. So we behave as if the drive did not
491 			 * respond to the command.
492 			 */
493 			sp->error = SCG_FATAL;
494 			return (0);
495 		}
496 		return (-1);
497 	}
498 
499 	if (r != i)
500 		errmsg("scgo_send(%s) wrote %d bytes (expected %d).\n",
501 			scgp->cmdname, r, i);
502 
503 	sp->ux_errno = 0;
504 	sp->sense_count = 0;
505 
506 	r = read(scgp->fd, (char *)rhp, RHSIZE+sp->size);
507 
508 	if (r < 0) {
509 		sp->ux_errno = geterrno();
510 		if (sp->ux_errno == ETIME) {
511 			sp->error = SCG_TIMEOUT;
512 			return (0);
513 		}
514 		sp->error = SCG_FATAL;
515 		return (-1);
516 	}
517 
518 	i = rhp->dlen;
519 	if (i > sp->size) {
520 		/*
521 		 * "DMA overrun" should be handled in the kernel.
522 		 * However this may happen with flaky PP adapters.
523 		 */
524 		errmsgno(EX_BAD,
525 			"DMA (read) overrun by %d bytes (requested %d bytes).\n",
526 			i - sp->size, sp->size);
527 		sp->resid = sp->size - i;
528 		sp->error = SCG_RETRYABLE;
529 		i = sp->size;
530 	} else {
531 		sp->resid = sp->size - i;
532 	}
533 
534 	if (use_local && inward)
535 		movebytes(dbp, sp->addr, i);
536 
537 	fillbytes(&sp->scb, sizeof (sp->scb), '\0');
538 	fillbytes(&sp->u_sense.cmd_sense, sizeof (sp->u_sense.cmd_sense), '\0');
539 
540 	sp->error = SCG_NO_ERROR;
541 	i = rhp->scsi?2:0;
542 /*	i = rhp->scsi;*/
543 	sp->u_scb.cmd_scb[0] = i;
544 	if (i & 2) {
545 		if (sp->ux_errno == 0)
546 			sp->ux_errno = EIO;
547 		/*
548 		 * If there is no DMA overrun and there is a
549 		 * SCSI Status byte != 0 then the SCSI cdb transport was OK
550 		 * and sp->error must be SCG_NO_ERROR.
551 		 */
552 /*		sp->error = SCG_RETRYABLE;*/
553 	}
554 
555 	return (0);
556 
557 }
558 
559 LOCAL int
do_scg_sense(scgp,sp)560 do_scg_sense(scgp, sp)
561 	SCSI	*scgp;
562 	struct scg_cmd	*sp;
563 {
564 	int		ret;
565 	struct scg_cmd 	s_cmd;
566 
567 	fillbytes((caddr_t)&s_cmd, sizeof (s_cmd), '\0');
568 	s_cmd.addr = (caddr_t)sp->u_sense.cmd_sense;
569 	s_cmd.size = sp->sense_len;
570 	s_cmd.flags = SCG_RECV_DATA|SCG_DISRE_ENA;
571 	s_cmd.cdb_len = SC_G0_CDBLEN;
572 	s_cmd.sense_len = CCS_SENSE_LEN;
573 	s_cmd.cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
574 	s_cmd.cdb.g0_cdb.lun = sp->cdb.g0_cdb.lun;
575 	s_cmd.cdb.g0_cdb.count = sp->sense_len;
576 	ret = do_scg_cmd(scgp, &s_cmd);
577 
578 	if (ret < 0)
579 		return (ret);
580 
581 	sp->sense_count = sp->sense_len - s_cmd.resid;
582 	return (ret);
583 }
584 
585 LOCAL int
scgo_send(scgp)586 scgo_send(scgp)
587 	SCSI		*scgp;
588 {
589 	struct scg_cmd	*sp = scgp->scmd;
590 	int	ret;
591 
592 	if (scgp->fd < 0) {
593 		sp->error = SCG_FATAL;
594 		return (0);
595 	}
596 	ret = do_scg_cmd(scgp, sp);
597 	if (ret < 0)
598 		return (ret);
599 	if (sp->u_scb.cmd_scb[0] & 2)
600 		ret = do_scg_sense(scgp, sp);
601 	return (ret);
602 }
603 
604 /* end of scsi-linux-pg.c */
605 
606 #ifndef	USE_PG_ONLY
607 
608 #undef	scgo_version
609 #undef	scgo_help
610 #undef	scgo_open
611 #undef	scgo_close
612 #undef	scgo_send
613 #undef	scgo_maxdma
614 #undef	scgo_initiator_id
615 #undef	scgo_isatapi
616 #undef	scgo_reset
617 
618 #endif
619