Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libusal / scsi-bsd-os.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
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).
6  *
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.
10  *
11  */
12
13 /* @(#)scsi-bsd-os.c    1.28 04/01/15 Copyright 1997 J. Schilling */
14 /*
15  *      Interface for the BSD/OS user-land raw SCSI implementation.
16  *
17  *      This is a hack, that tries to emulate the functionality
18  *      of the usal driver.
19  *
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.
25  *
26  *      Copyright (c) 1997 J. Schilling
27  */
28 /*
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.
32  *
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.
37  *
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.
41  */
42
43 #undef  sense
44
45 #define scsi_sense      bsdi_scsi_sense
46 #define scsi_inquiry    bsdi_scsi_inquiry
47
48 /*
49  * Must use -I/sys...
50  * The next two files are in /sys/dev/scsi
51  */
52 #include <dev/scsi/scsi.h>
53 #include <dev/scsi/scsi_ioctl.h>
54
55 /*
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.
61  */
62 static  char    _usal_trans_version[] = "scsi-bsd-os.c-1.28";   /* The version for this transport*/
63
64 #define MAX_SCG         16      /* Max # of SCSI controllers */
65 #define MAX_TGT         16
66 #define MAX_LUN         8
67
68 struct usal_local {
69         short   usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
70 };
71 #define usallocal(p)    ((struct usal_local *)((p)->local))
72
73 #include <machine/param.h>
74
75 #define MAX_DMA_BSDI    MAXPHYS         /* More makes problems */
76
77
78 static  BOOL    usal_setup(SCSI *usalp, int f, int busno, int tgt, int tlun);
79
80 /*
81  * Return version information for the low level SCSI transport code.
82  * This has been introduced to make it easier to trace down problems
83  * in applications.
84  */
85 static char *
86 usalo_version(SCSI *usalp, int what)
87 {
88         if (usalp != (SCSI *)0) {
89                 switch (what) {
90
91                 case SCG_VERSION:
92                         return (_usal_trans_version);
93                 /*
94                  * If you changed this source, you are not allowed to
95                  * return "schily" for the SCG_AUTHOR request.
96                  */
97                 case SCG_AUTHOR:
98                         return (_usal_auth_cdrkit);
99                 case SCG_SCCS_ID:
100                         return (__sccsid);
101                 }
102         }
103         return ((char *)0);
104 }
105
106 static int
107 usalo_help(SCSI *usalp, FILE *f)
108 {
109         __usal_help(f, "SCSIRAWCDB", "Generic SCSI for devices known by BSDi",
110                 "", "devname:@,lun", "/dev/rsr0a:@,0", FALSE, TRUE);
111         return (0);
112 }
113
114 static int
115 usalo_open(SCSI *usalp, char *device)
116 {
117                 int     busno   = usal_scsibus(usalp);
118                 int     tgt     = usal_target(usalp);
119                 int     tlun    = usal_lun(usalp);
120         register int    f;
121         register int    b;
122         register int    t;
123         register int    l;
124         register int    nopen = 0;
125         char            devname[64];
126
127         if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
128                 errno = EINVAL;
129                 if (usalp->errstr)
130                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
131                                 "Illegal value for busno, target or lun '%d,%d,%d'",
132                                 busno, tgt, tlun);
133                 return (-1);
134         }
135
136         if (usalp->local == NULL) {
137                 usalp->local = malloc(sizeof (struct usal_local));
138                 if (usalp->local == NULL)
139                         return (0);
140
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;
145                         }
146                 }
147         }
148
149         if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2))
150                 goto openbydev;
151
152         if (busno >= 0 && tgt >= 0 && tlun >= 0) {
153
154                 snprintf(devname, sizeof (devname),
155                                         "/dev/su%d-%d-%d", busno, tgt, tlun);
156                 f = open(devname, O_RDWR|O_NONBLOCK);
157                 if (f < 0) {
158                         goto openbydev;
159                 }
160                 usallocal(usalp)->usalfiles[busno][tgt][tlun] = f;
161                 return (1);
162
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);*/
170
171                                 if (f < 0) {
172                                         if (errno != ENOENT &&
173                                             errno != ENXIO &&
174                                             errno != ENODEV) {
175                                                 if (usalp->errstr)
176                                                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
177                                                                 "Cannot open '%s'",
178                                                                 devname);
179                                                 return (0);
180                                         }
181                                 } else {
182                                         if (usal_setup(usalp, f, b, t, l))
183                                                 nopen++;
184                                 }
185                         }
186                 }
187         }
188         /*
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.
191          */
192 openbydev:
193         if (nopen == 0) {
194                 if (device == NULL || device[0] == '\0')
195                         return (0);
196                 f = open(device, O_RDWR|O_NONBLOCK);
197                 if (f < 0) {
198                         if (usalp->errstr)
199                                 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
200                                         "Cannot open '%s'",
201                                         device);
202                         return (0);
203                 }
204                 if (tlun == -2) {       /* If 'lun' is not known, we reject */
205                         close(f);
206                         errno = EINVAL;
207                         return (0);
208                 }
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))
214                         nopen++;
215         }
216         return (nopen);
217 }
218
219 static int
220 usalo_close(SCSI *usalp)
221 {
222         register int    f;
223         register int    b;
224         register int    t;
225         register int    l;
226
227         if (usalp->local == NULL)
228                 return (-1);
229
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];
234                                 if (f >= 0)
235                                         close(f);
236                                 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
237                         }
238                 }
239         }
240         return (0);
241 }
242
243 static BOOL
244 usal_setup(SCSI *usalp, int f, int busno, int tgt, int tlun)
245 {
246         int     Bus;
247         int     Target;
248         int     Lun;
249         BOOL    onetarget = FALSE;
250
251         if (usal_scsibus(usalp) >= 0 && usal_target(usalp) >= 0 && usal_lun(usalp) >= 0)
252                 onetarget = TRUE;
253
254         /*
255          * Unfortunately there is no way to get the right values from kernel.
256          */
257         Bus     = busno;
258         Target  = tgt;
259         Lun     = tlun;
260
261         if (usalp->debug > 0) {
262                 fprintf((FILE *)usalp->errfile,
263                         "Bus: %d Target: %d Lun: %d\n", Bus, Target, Lun);
264         }
265
266         if (Bus >= MAX_SCG || Target >= MAX_TGT || Lun >= MAX_LUN) {
267                 close(f);
268                 return (FALSE);
269         }
270
271         if (usallocal(usalp)->usalfiles[Bus][Target][Lun] == (short)-1)
272                 usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)f;
273
274         if (onetarget) {
275                 if (Bus == busno && Target == tgt && Lun == tlun) {
276                         return (TRUE);
277                 } else {
278                         usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)-1;
279                         close(f);
280                 }
281         }
282         return (FALSE);
283 }
284
285 static long
286 usalo_maxdma(SCSI *usalp, long amt)
287 {
288         long maxdma = MAX_DMA_BSDI;
289
290         return (maxdma);
291 }
292
293 static void *
294 usalo_getbuf(SCSI *usalp, long amt)
295 {
296         if (usalp->debug > 0) {
297                 fprintf((FILE *)usalp->errfile,
298                         "usalo_getbuf: %ld bytes\n", amt);
299         }
300         usalp->bufbase = malloc((size_t)(amt));
301         return (usalp->bufbase);
302 }
303
304 static void
305 usalo_freebuf(SCSI *usalp)
306 {
307         if (usalp->bufbase)
308                 free(usalp->bufbase);
309         usalp->bufbase = NULL;
310 }
311
312 static BOOL
313 usalo_havebus(SCSI *usalp, int busno)
314 {
315         register int    t;
316         register int    l;
317
318         if (busno < 0 || busno >= MAX_SCG)
319                 return (FALSE);
320
321         if (usalp->local == NULL)
322                 return (FALSE);
323
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)
327                                 return (TRUE);
328         }
329         return (FALSE);
330 }
331
332 static int
333 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
334 {
335         if (busno < 0 || busno >= MAX_SCG ||
336             tgt < 0 || tgt >= MAX_TGT ||
337             tlun < 0 || tlun >= MAX_LUN)
338                 return (-1);
339
340         if (usalp->local == NULL)
341                 return (-1);
342
343         return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
344 }
345
346 static int
347 usalo_initiator_id(SCSI *usalp)
348 {
349         return (-1);
350 }
351
352 static int
353 usalo_isatapi(SCSI *usalp)
354 {
355         return (FALSE);
356 }
357
358 static int
359 usalo_reset(SCSI *usalp, int what)
360 {
361         /*
362          * Cannot reset on BSD/OS
363          */
364         errno = EINVAL;
365         return (-1);
366 }
367
368 static int
369 usalo_send(SCSI *usalp)
370 {
371         struct usal_cmd *sp = usalp->scmd;
372         scsi_user_cdb_t suc;
373         int             ret = 0;
374
375 /*      fprintf((FILE *)usalp->errfile, "f: %d\n", f);*/
376         if (usalp->fd < 0) {
377                 sp->error = SCG_FATAL;
378                 return (0);
379         }
380
381         /* Zero the structure... */
382         fillbytes(&suc, sizeof (suc), '\0');
383
384         /* Read or write? */
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;
389         }
390
391         suc.suc_timeout = sp->timeout;
392
393         suc.suc_cdblen = sp->cdb_len;
394         movebytes(sp->cdb.cmd_cdb, suc.suc_cdb, suc.suc_cdblen);
395
396         suc.suc_datalen = sp->size;
397         suc.suc_data = sp->addr;
398
399         if (ioctl(usalp->fd, SCSIRAWCDB, &suc) < 0) {
400                 ret  = -1;
401                 sp->ux_errno = geterrno();
402                 if (sp->ux_errno != ENOTTY)
403                         ret = 0;
404         } else {
405                 sp->ux_errno = 0;
406                 if (suc.suc_sus.sus_status != STS_GOOD)
407                         sp->ux_errno = EIO;
408         }
409         fillbytes(&sp->scb, sizeof (sp->scb), '\0');
410         fillbytes(&sp->u_sense.cmd_sense, sizeof (sp->u_sense.cmd_sense), '\0');
411 #if 0
412         /*
413          * Unfortunalety, BSD/OS has no idea of DMA residual count.
414          */
415         sp->resid = req.datalen - req.datalen_used;
416         sp->sense_count = req.senselen_used;
417 #else
418         sp->resid = 0;
419         sp->sense_count = sizeof (suc.suc_sus.sus_sense);
420 #endif
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;
425
426         switch (suc.suc_sus.sus_status) {
427
428         case STS_GOOD:
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;
435         }
436
437         return (ret);
438 }
439
440 #define sense   u_sense.Sense
441
442 #undef scsi_sense
443 #undef scsi_inquiry