2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi-bsd-os.c 1.28 04/01/15 Copyright 1997 J. Schilling */
15 * Interface for the BSD/OS user-land raw SCSI implementation.
17 * This is a hack, that tries to emulate the functionality
20 * Warning: you may change this source, but if you do that
21 * you need to change the _usal_version and _usal_auth* string below.
22 * You may not return "schily" for an SCG_AUTHOR request anymore.
23 * Choose your name instead of "schily" and make clear that the version
24 * string is related to a modified source.
26 * Copyright (c) 1997 J. Schilling
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2
31 * as published by the Free Software Foundation.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License along with
39 * this program; see the file COPYING. If not, write to the Free Software
40 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 #define scsi_sense bsdi_scsi_sense
46 #define scsi_inquiry bsdi_scsi_inquiry
50 * The next two files are in /sys/dev/scsi
52 #include <dev/scsi/scsi.h>
53 #include <dev/scsi/scsi_ioctl.h>
56 * Warning: you may change this source, but if you do that
57 * you need to change the _usal_version and _usal_auth* string below.
58 * You may not return "schily" for an SCG_AUTHOR request anymore.
59 * Choose your name instead of "schily" and make clear that the version
60 * string is related to a modified source.
62 static char _usal_trans_version[] = "scsi-bsd-os.c-1.28"; /* The version for this transport*/
64 #define MAX_SCG 16 /* Max # of SCSI controllers */
69 short usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
71 #define usallocal(p) ((struct usal_local *)((p)->local))
73 #include <machine/param.h>
75 #define MAX_DMA_BSDI MAXPHYS /* More makes problems */
78 static BOOL usal_setup(SCSI *usalp, int f, int busno, int tgt, int tlun);
81 * Return version information for the low level SCSI transport code.
82 * This has been introduced to make it easier to trace down problems
86 usalo_version(SCSI *usalp, int what)
88 if (usalp != (SCSI *)0) {
92 return (_usal_trans_version);
94 * If you changed this source, you are not allowed to
95 * return "schily" for the SCG_AUTHOR request.
98 return (_usal_auth_cdrkit);
107 usalo_help(SCSI *usalp, FILE *f)
109 __usal_help(f, "SCSIRAWCDB", "Generic SCSI for devices known by BSDi",
110 "", "devname:@,lun", "/dev/rsr0a:@,0", FALSE, TRUE);
115 usalo_open(SCSI *usalp, char *device)
117 int busno = usal_scsibus(usalp);
118 int tgt = usal_target(usalp);
119 int tlun = usal_lun(usalp);
124 register int nopen = 0;
127 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
130 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
131 "Illegal value for busno, target or lun '%d,%d,%d'",
136 if (usalp->local == NULL) {
137 usalp->local = malloc(sizeof (struct usal_local));
138 if (usalp->local == NULL)
141 for (b = 0; b < MAX_SCG; b++) {
142 for (t = 0; t < MAX_TGT; t++) {
143 for (l = 0; l < MAX_LUN; l++)
144 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
149 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2))
152 if (busno >= 0 && tgt >= 0 && tlun >= 0) {
154 snprintf(devname, sizeof (devname),
155 "/dev/su%d-%d-%d", busno, tgt, tlun);
156 f = open(devname, O_RDWR|O_NONBLOCK);
160 usallocal(usalp)->usalfiles[busno][tgt][tlun] = f;
163 } else for (b = 0; b < MAX_SCG; b++) {
164 for (t = 0; t < MAX_TGT; t++) {
165 for (l = 0; l < MAX_LUN; l++) {
166 snprintf(devname, sizeof (devname),
167 "/dev/su%d-%d-%d", b, t, l);
168 f = open(devname, O_RDWR|O_NONBLOCK);
169 /* fprintf(stderr, "open (%s) = %d\n", devname, f);*/
172 if (errno != ENOENT &&
176 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
182 if (usal_setup(usalp, f, b, t, l))
189 * Could not open /dev/su-* or got dev=devname:b,l,l / dev=devname:@,l
190 * We do the apropriate tests and try our best.
194 if (device == NULL || device[0] == '\0')
196 f = open(device, O_RDWR|O_NONBLOCK);
199 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
204 if (tlun == -2) { /* If 'lun' is not known, we reject */
209 busno = 0; /* use fake number, we cannot get it */
210 tgt = 0; /* use fake number, we cannot get it */
211 usal_settarget(usalp, busno, tgt, tlun);
212 /* 'lun' has been specified on command line */
213 if (usal_setup(usalp, f, busno, tgt, tlun))
220 usalo_close(SCSI *usalp)
227 if (usalp->local == NULL)
230 for (b = 0; b < MAX_SCG; b++) {
231 for (t = 0; t < MAX_TGT; t++) {
232 for (l = 0; l < MAX_LUN; l++) {
233 f = usallocal(usalp)->usalfiles[b][t][l];
236 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
244 usal_setup(SCSI *usalp, int f, int busno, int tgt, int tlun)
249 BOOL onetarget = FALSE;
251 if (usal_scsibus(usalp) >= 0 && usal_target(usalp) >= 0 && usal_lun(usalp) >= 0)
255 * Unfortunately there is no way to get the right values from kernel.
261 if (usalp->debug > 0) {
262 fprintf((FILE *)usalp->errfile,
263 "Bus: %d Target: %d Lun: %d\n", Bus, Target, Lun);
266 if (Bus >= MAX_SCG || Target >= MAX_TGT || Lun >= MAX_LUN) {
271 if (usallocal(usalp)->usalfiles[Bus][Target][Lun] == (short)-1)
272 usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)f;
275 if (Bus == busno && Target == tgt && Lun == tlun) {
278 usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)-1;
286 usalo_maxdma(SCSI *usalp, long amt)
288 long maxdma = MAX_DMA_BSDI;
294 usalo_getbuf(SCSI *usalp, long amt)
296 if (usalp->debug > 0) {
297 fprintf((FILE *)usalp->errfile,
298 "usalo_getbuf: %ld bytes\n", amt);
300 usalp->bufbase = malloc((size_t)(amt));
301 return (usalp->bufbase);
305 usalo_freebuf(SCSI *usalp)
308 free(usalp->bufbase);
309 usalp->bufbase = NULL;
313 usalo_havebus(SCSI *usalp, int busno)
318 if (busno < 0 || busno >= MAX_SCG)
321 if (usalp->local == NULL)
324 for (t = 0; t < MAX_TGT; t++) {
325 for (l = 0; l < MAX_LUN; l++)
326 if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
333 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
335 if (busno < 0 || busno >= MAX_SCG ||
336 tgt < 0 || tgt >= MAX_TGT ||
337 tlun < 0 || tlun >= MAX_LUN)
340 if (usalp->local == NULL)
343 return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
347 usalo_initiator_id(SCSI *usalp)
353 usalo_isatapi(SCSI *usalp)
359 usalo_reset(SCSI *usalp, int what)
362 * Cannot reset on BSD/OS
369 usalo_send(SCSI *usalp)
371 struct usal_cmd *sp = usalp->scmd;
375 /* fprintf((FILE *)usalp->errfile, "f: %d\n", f);*/
377 sp->error = SCG_FATAL;
381 /* Zero the structure... */
382 fillbytes(&suc, sizeof (suc), '\0');
385 if (sp->flags & SCG_RECV_DATA) {
386 suc.suc_flags |= SUC_READ;
387 } else if (sp->size > 0) {
388 suc.suc_flags |= SUC_WRITE;
391 suc.suc_timeout = sp->timeout;
393 suc.suc_cdblen = sp->cdb_len;
394 movebytes(sp->cdb.cmd_cdb, suc.suc_cdb, suc.suc_cdblen);
396 suc.suc_datalen = sp->size;
397 suc.suc_data = sp->addr;
399 if (ioctl(usalp->fd, SCSIRAWCDB, &suc) < 0) {
401 sp->ux_errno = geterrno();
402 if (sp->ux_errno != ENOTTY)
406 if (suc.suc_sus.sus_status != STS_GOOD)
409 fillbytes(&sp->scb, sizeof (sp->scb), '\0');
410 fillbytes(&sp->u_sense.cmd_sense, sizeof (sp->u_sense.cmd_sense), '\0');
413 * Unfortunalety, BSD/OS has no idea of DMA residual count.
415 sp->resid = req.datalen - req.datalen_used;
416 sp->sense_count = req.senselen_used;
419 sp->sense_count = sizeof (suc.suc_sus.sus_sense);
421 if (sp->sense_count > SCG_MAX_SENSE)
422 sp->sense_count = SCG_MAX_SENSE;
423 movebytes(suc.suc_sus.sus_sense, sp->u_sense.cmd_sense, sp->sense_count);
424 sp->u_scb.cmd_scb[0] = suc.suc_sus.sus_status;
426 switch (suc.suc_sus.sus_status) {
429 sp->error = SCG_NO_ERROR; break;
430 case STS_CMD_TERMINATED:sp->error = SCG_TIMEOUT; break;
431 case STS_BUSY: sp->error = SCG_RETRYABLE; break;
432 case STS_CHECKCOND: sp->error = SCG_RETRYABLE; break;
433 case STS_QUEUE_FULL: sp->error = SCG_RETRYABLE; break;
434 default: sp->error = SCG_FATAL; break;
440 #define sense u_sense.Sense