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