Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libusal / scsi-vms.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-vms.c       1.33 04/01/15 Copyright 1997 J. Schilling */
14 /*
15  *      Interface for the VMS generic SCSI implementation.
16  *
17  *      The idea for an elegant mapping to VMS device dontroller names
18  *      is from Chip Dancy Chip.Dancy@hp.com. This allows up to
19  *      26 IDE controllers (DQ[A-Z][0-1]).
20  *
21  *      This is a hack, that tries to emulate the functionality
22  *      of the usal driver.
23  *
24  *      Warning: you may change this source, but if you do that
25  *      you need to change the _usal_version and _usal_auth* string below.
26  *      You may not return "schily" for an SCG_AUTHOR request anymore.
27  *      Choose your name instead of "schily" and make clear that the version
28  *      string is related to a modified source.
29  *
30  *      Copyright (c) 1997 J. Schilling
31  */
32 /*
33  * This program is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License version 2
35  * as published by the Free Software Foundation.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License along with
43  * this program; see the file COPYING.  If not, write to the Free Software
44  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45  */
46
47 #include <iodef.h>
48 #include <ssdef.h>
49 #include <descrip.h>
50 #include <starlet.h>
51 #include <string.h>
52 #include <LIB$ROUTINES.H>
53
54 /*
55  *      Warning: you may change this source, but if you do that
56  *      you need to change the _usal_version and _usal_auth* string below.
57  *      You may not return "schily" for an SCG_AUTHOR request anymore.
58  *      Choose your name instead of "schily" and make clear that the version
59  *      string is related to a modified source.
60  */
61 static  char    _usal_trans_version[] = "scsi-vms.c-1.33";      /* The version for this transport*/
62
63 #define VMS_MAX_DK      4               /* DK[A-D] VMS device controllers */
64 #define VMS_MAX_GK      4               /* GK[A-D] VMS device controllers */
65 #define VMS_MAX_DQ      26              /* DQ[A-Z] VMS device controllers */
66
67 #define VMS_DKRANGE_MAX VMS_MAX_DK
68 #define VMS_GKRANGE_MAX (VMS_DKRANGE_MAX + VMS_MAX_GK)
69 #define VMS_DQRANGE_MAX (VMS_GKRANGE_MAX + VMS_MAX_DQ)
70
71 #define MAX_SCG         VMS_DQRANGE_MAX /* Max # of SCSI controllers */
72 #define MAX_TGT         16
73 #define MAX_LUN         8
74
75 #define MAX_DMA_VMS     (63*1024)       /* Check if this is not too big */
76 #define MAX_PHSTMO_VMS  300
77 #define MAX_DSCTMO_VMS  ((64*1024)-1)   /* max value for OpenVMS/AXP 7.1 ehh*/
78
79 /*
80  * Define a mapping from the scsi busno to the three character
81  * VMS device controller.
82  * The valid busno values are broken into three ranges, one for each of
83  * the three supported devices: dk, gk, and dq.
84  * The vmschar[] and vmschar1[] arrays are subscripted by an offset
85  * corresponding to each of the three ranges [0,1,2] to provide the
86  * two characters of the VMS device.
87  * The offset of the busno value within its range is used to define the
88  * third character, using the vmschar2[] array.
89  */
90 static  char    vmschar[]       = {'d', 'g', 'd'};
91 static  char    vmschar1[]      = {'k', 'k', 'q'};
92 static  char    vmschar2[]      = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
93                                     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
94                                     'u', 'v', 'w', 'x', 'y', 'z'};
95
96
97 static  int     do_usal_cmd(SCSI *usalp, struct usal_cmd *sp);
98 static  int     do_usal_sense(SCSI *usalp, struct usal_cmd *sp);
99
100 #define DEVICE_NAMELEN 8
101
102 struct SCSI$DESC {
103         Uint    SCSI$L_OPCODE;          /* SCSI Operation Code */
104         Uint    SCSI$L_FLAGS;           /* SCSI Flags Bit Map */
105         char    *SCSI$A_CMD_ADDR;       /* ->SCSI command buffer */
106         Uint    SCSI$L_CMD_LEN;         /* SCSI command length, bytes */
107         char    *SCSI$A_DATA_ADDR;      /* ->SCSI data buffer */
108         Uint    SCSI$L_DATA_LEN;        /* SCSI data length, bytes */
109         Uint    SCSI$L_PAD_LEN;         /* SCSI pad length, bytes */
110         Uint    SCSI$L_PH_CH_TMOUT;     /* SCSI phase change timeout */
111         Uint    SCSI$L_DISCON_TMOUT;    /* SCSI disconnect timeout */
112         Uint    SCSI$L_RES_1;           /* Reserved */
113         Uint    SCSI$L_RES_2;           /* Reserved */
114         Uint    SCSI$L_RES_3;           /* Reserved */
115         Uint    SCSI$L_RES_4;           /* Reserved */
116         Uint    SCSI$L_RES_5;           /* Reserved */
117         Uint    SCSI$L_RES_6;           /* Reserved */
118 };
119
120 #ifdef __ALPHA
121 #pragma member_alignment save
122 #pragma nomember_alignment
123 #endif
124
125 struct SCSI$IOSB {
126         Ushort  SCSI$W_VMS_STAT;        /* VMS status code */
127         Ulong   SCSI$L_IOSB_TFR_CNT;    /* Actual #bytes transferred */
128         char    SCSI$B_IOSB_FILL_1;
129         Uchar   SCSI$B_IOSB_STS;        /* SCSI device status */
130 };
131
132 #ifdef __ALPHA
133 #pragma member_alignment restore
134 #endif
135
136 #define SCSI$K_GOOD_STATUS              0
137 #define SCSI$K_CHECK_CONDITION          0x2
138 #define SCSI$K_CONDITION_MET            0x4
139 #define SCSI$K_BUSY                     0x8
140 #define SCSI$K_INTERMEDIATE             0x10
141 #define SCSI$K_INTERMEDIATE_C_MET       0x14
142 #define SCSI$K_RESERVATION_CONFLICT     0x18
143 #define SCSI$K_COMMAND_TERMINATED       0x22
144 #define SCSI$K_QUEUE_FULL               0x28
145
146
147 #define SCSI$K_WRITE            0X0     /* direction of transfer=write */
148 #define SCSI$K_READ             0X1     /* direction of transfer=read */
149 #define SCSI$K_FL_ENAB_DIS      0X2     /* enable disconnects */
150 #define SCSI$K_FL_ENAB_SYNC     0X4     /* enable sync */
151 #define GK_EFN                  0       /* Event flag number */
152
153 static char     gk_device[8];           /* XXX JS hoffentlich gibt es keinen Ueberlauf */
154 static Ushort   gk_chan;
155 static Ushort   transfer_length;
156 static int      i;
157 static int      status;
158 static $DESCRIPTOR(gk_device_desc, gk_device);
159 static struct SCSI$IOSB gk_iosb;
160 static struct SCSI$DESC gk_desc;
161 static FILE *fp;
162
163
164 struct usal_local {
165         Ushort  gk_chan;
166 };
167 #define usallocal(p)    ((struct usal_local *)((p)->local))
168
169 /*
170  * Return version information for the low level SCSI transport code.
171  * This has been introduced to make it easier to trace down problems
172  * in applications.
173  */
174 static char *
175 usalo_version(SCSI *usalp, int what)
176 {
177         if (usalp != (SCSI *)0) {
178                 switch (what) {
179
180                 case SCG_VERSION:
181                         return (_usal_trans_version);
182                 /*
183                  * If you changed this source, you are not allowed to
184                  * return "schily" for the SCG_AUTHOR request.
185                  */
186                 case SCG_AUTHOR:
187                         return (_usal_auth_cdrkit);
188                 case SCG_SCCS_ID:
189                         return (__sccsid);
190                 }
191         }
192         return ((char *)0);
193 }
194
195 static int
196 usalo_help(SCSI *usalp, FILE *f)
197 {
198         __usal_help(f, "IO$_DIAGNOSE", "Generic SCSI",
199                 "", "bus,target,lun", "1,2,0", FALSE, FALSE);
200         return (0);
201 }
202
203 static int
204 usalo_open(SCSI *usalp, char *device)
205 {
206         int     busno   = usal_scsibus(usalp);
207         int     tgt     = usal_target(usalp);
208         int     tlun    = usal_lun(usalp);
209         char    devname[DEVICE_NAMELEN];
210         char    buschar;
211         char    buschar1;
212         char    buschar2;
213         int     range;
214         int     range_offset;
215
216         if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
217                 errno = EINVAL;
218                 if (usalp->errstr)
219                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
220                                 "Illegal value for busno, target or lun '%d,%d,%d'",
221                                 busno, tgt, tlun);
222                 return (-1);
223         }
224
225         if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
226                 errno = EINVAL;
227                 if (usalp->errstr)
228                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
229                                 "Open by 'devname' not supported on this OS");
230                 return (-1);
231         }
232         if (busno < 0 || tgt < 0 || tlun < 0) {
233                 /*
234                  * There is no real reason why we cannot scan on VMS,
235                  * but for now it is not possible.
236                  */
237                 if (usalp->errstr)
238                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
239                                 "Unable to scan on VMS");
240                 return (0);
241         }
242
243         if (usalp->local == NULL) {
244                 usalp->local = malloc(sizeof (struct usal_local));
245                 if (usalp->local == NULL)
246                         return (0);
247         }
248
249         if (busno < VMS_DKRANGE_MAX) {                  /* in the dk range?   */
250                 range = 0;
251                 range_offset = busno;
252         } else if (busno < VMS_GKRANGE_MAX) {           /* in the gk range?   */
253                 range = 1;
254                 range_offset = busno - VMS_DKRANGE_MAX;
255         } else if (busno < VMS_DQRANGE_MAX) {           /* in the dq range?   */
256                 range = 2;
257                 range_offset = busno - VMS_GKRANGE_MAX;
258         }
259         buschar = vmschar[range];                       /* get first device char*/
260         buschar1 = vmschar1[range];                     /* get 2nd device char*/
261         buschar2 = vmschar2[range_offset];              /* get controller char*/
262
263         snprintf(devname, sizeof (devname), "%c%c%c%d0%d:",
264                                         buschar, buschar1, buschar2,
265                                         tgt, tlun);
266         strcpy(gk_device, devname);
267         status = sys$assign(&gk_device_desc, &gk_chan, 0, 0);
268         if (!(status & 1)) {
269                 fprintf((FILE *)usalp->errfile,
270                         "Unable to access scsi-device \"%s\"\n", &gk_device[0]);
271                 return (-1);
272         }
273         if (usalp->debug > 0) {
274                 fp = fopen("cdrecord_io.log", "w", "rfm=stmlf", "rat=cr");
275                 if (fp == NULL) {
276                         perror("Failing opening i/o-logfile");
277                         exit(SS$_NORMAL);
278                 }
279         }
280         return (status);
281 }
282
283 static int
284 usalo_close(SCSI *usalp)
285 {
286         /*
287          * XXX close gk_chan ???
288          */
289         /*
290          * sys$dassgn()
291          */
292
293         status = sys$dassgn(gk_chan);
294
295         return (status);
296 }
297
298 static long
299 usalo_maxdma(SCSI *usalp, long amt)
300 {
301         return (MAX_DMA_VMS);
302 }
303
304 static BOOL
305 usalo_havebus(SCSI *usalp, int busno)
306 {
307         if (gk_chan == 0)
308                 return (FALSE);
309         return (TRUE);
310 }
311
312 static int
313 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
314 {
315         if (gk_chan == 0)
316                 return (-1);
317         return (gk_chan);
318 }
319
320 static int
321 usalo_initiator_id(SCSI *usalp)
322 {
323         return (-1);
324 }
325
326 static int
327 usalo_isatapi(SCSI *usalp)
328 {
329         int     busno = usal_scsibus(usalp);
330
331         if (busno >= 8)
332                 return (TRUE);
333
334         return (FALSE);
335 }
336
337 static int
338 usalo_reset(SCSI *usalp, int what)
339 {
340         errno = EINVAL;
341         return (-1);
342 }
343
344 static void *
345 usalo_getbuf(SCSI *usalp, long amt)
346 {
347         if (usalp->debug > 0) {
348                 fprintf((FILE *)usalp->errfile,
349                                 "usalo_getbuf: %ld bytes\n", amt);
350         }
351         usalp->bufbase = malloc((size_t)(amt)); /* XXX JS XXX valloc() ??? */
352         return (usalp->bufbase);
353 }
354
355 static void
356 usalo_freebuf(SCSI *usalp)
357 {
358         if (usalp->bufbase)
359                 free(usalp->bufbase);
360         usalp->bufbase = NULL;
361 }
362
363 static int
364 do_usal_cmd(SCSI *usalp, struct usal_cmd *sp)
365 {
366         char            *cmdadr;
367         int             notcmdretry;
368         int             len;
369         Uchar           scsi_sts;
370         int             severity;
371
372         /* XXX JS XXX This cannot be OK */
373         notcmdretry = (sp->flags & SCG_CMD_RETRY)^SCG_CMD_RETRY;
374         /* error corrected ehh  */
375 /*
376  * XXX JS Wenn das notcmdretry Flag bei VMS auch 0x08 ist und Du darauf hoffst,
377  * XXX  Dasz ich den Wert nie aendere, dann ist das richtig.
378  * XXX Siehe unten: Das gleiche gilt fuer SCG_RECV_DATA und SCG_DISRE_ENA !!!
379  */
380
381         cmdadr = (char *)sp->cdb.cmd_cdb;
382         /* XXX JS XXX This cannot be OK */
383         gk_desc.SCSI$L_FLAGS = ((sp->flags & SCG_RECV_DATA) |
384                                 (sp->flags & SCG_DISRE_ENA)|
385                                 notcmdretry);
386                                 /* XXX siehe oben, das ist ein bitweises oder!!! */
387         gk_desc.SCSI$A_DATA_ADDR = sp->addr;
388         gk_desc.SCSI$L_DATA_LEN = sp->size;
389         gk_desc.SCSI$A_CMD_ADDR = cmdadr;
390         gk_desc.SCSI$L_CMD_LEN = sp->cdb_len;
391         gk_desc.SCSI$L_PH_CH_TMOUT = sp->timeout;
392         gk_desc.SCSI$L_DISCON_TMOUT = sp->timeout;
393         if (gk_desc.SCSI$L_PH_CH_TMOUT > MAX_PHSTMO_VMS)
394             gk_desc.SCSI$L_PH_CH_TMOUT = MAX_PHSTMO_VMS;
395         if (gk_desc.SCSI$L_DISCON_TMOUT > MAX_DSCTMO_VMS)
396             gk_desc.SCSI$L_DISCON_TMOUT = MAX_DSCTMO_VMS;
397         gk_desc.SCSI$L_OPCODE = 1;      /* SCSI Operation Code */
398         gk_desc.SCSI$L_PAD_LEN = 0;     /* SCSI pad length, bytes */
399         gk_desc.SCSI$L_RES_1 = 0;       /* Reserved */
400         gk_desc.SCSI$L_RES_2 = 0;       /* Reserved */
401         gk_desc.SCSI$L_RES_3 = 0;       /* Reserved */
402         gk_desc.SCSI$L_RES_4 = 0;       /* Reserved */
403         gk_desc.SCSI$L_RES_5 = 0;       /* Reserved */
404         gk_desc.SCSI$L_RES_6 = 0;       /* Reserved */
405         if (usalp->debug > 0) {
406                 fprintf(fp, "***********************************************************\n");
407                 fprintf(fp, "SCSI VMS-I/O parameters\n");
408                 fprintf(fp, "OPCODE: %d", gk_desc.SCSI$L_OPCODE);
409                 fprintf(fp, " FLAGS: %d\n", gk_desc.SCSI$L_FLAGS);
410                 fprintf(fp, "CMD:");
411                 for (i = 0; i < gk_desc.SCSI$L_CMD_LEN; i++) {
412                         fprintf(fp, "%x ", sp->cdb.cmd_cdb[i]);
413                 }
414                 fprintf(fp, "\n");
415                 fprintf(fp, "DATA_LEN: %d\n", gk_desc.SCSI$L_DATA_LEN);
416                 fprintf(fp, "PH_CH_TMOUT: %d", gk_desc.SCSI$L_PH_CH_TMOUT);
417                 fprintf(fp, " DISCON_TMOUT: %d\n", gk_desc.SCSI$L_DISCON_TMOUT);
418         }
419         status = sys$qiow(GK_EFN, gk_chan, IO$_DIAGNOSE, &gk_iosb, 0, 0,
420                         &gk_desc, sizeof (gk_desc), 0, 0, 0, 0);
421
422
423         if (usalp->debug > 0) {
424                 fprintf(fp, "qiow-status: %i\n", status);
425                 fprintf(fp, "VMS status code %i\n", gk_iosb.SCSI$W_VMS_STAT);
426                 fprintf(fp, "Actual #bytes transferred %i\n", gk_iosb.SCSI$L_IOSB_TFR_CNT);
427                 fprintf(fp, "SCSI device status %i\n", gk_iosb.SCSI$B_IOSB_STS);
428                 if (gk_iosb.SCSI$L_IOSB_TFR_CNT != gk_desc.SCSI$L_DATA_LEN) {
429                         fprintf(fp, "#bytes transferred != DATA_LEN\n");
430                 }
431         }
432
433         if (!(status & 1)) {            /* Fehlerindikation fuer sys$qiow() */
434                 sp->ux_errno = geterrno();
435                 /* schwerwiegender nicht SCSI bedingter Fehler => return (-1) */
436                 if (sp->ux_errno == ENOTTY || sp->ux_errno == ENXIO ||
437                     sp->ux_errno == EINVAL || sp->ux_errno == EACCES) {
438                         return (-1);
439                 }
440                 if (sp->ux_errno == 0)
441                         sp->ux_errno == EIO;
442         } else {
443                 sp->ux_errno = 0;
444         }
445
446         sp->resid = gk_desc.SCSI$L_DATA_LEN - gk_iosb.SCSI$L_IOSB_TFR_CNT;
447
448         if (usalo_isatapi(usalp)) {
449                 scsi_sts = ((gk_iosb.SCSI$B_IOSB_STS >> 4) & 0x7);
450         } else {
451                 scsi_sts = gk_iosb.SCSI$B_IOSB_STS;
452         }
453
454         if (gk_iosb.SCSI$W_VMS_STAT == SS$_NORMAL && scsi_sts == 0) {
455                 sp->error = SCG_NO_ERROR;
456                 if (usalp->debug > 0) {
457                         fprintf(fp, "scsi_sts == 0\n");
458                         fprintf(fp, "gk_iosb.SCSI$B_IOSB_STS == 0\n");
459                         fprintf(fp, "sp->error %i\n", sp->error);
460                         fprintf(fp, "sp->resid %i\n", sp->resid);
461                 }
462                 return (0);
463         }
464
465         severity = gk_iosb.SCSI$W_VMS_STAT & 0x7;
466
467         if (severity == 4) {
468                 sp->error = SCG_FATAL;
469                 if (usalp->debug > 0) {
470                         fprintf(fp, "scsi_sts & 2\n");
471                         fprintf(fp, "gk_iosb.SCSI$B_IOSB_STS & 2\n");
472                         fprintf(fp, "gk_iosb.SCSI$W_VMS_STAT & 0x7 == SS$_FATAL\n");
473                         fprintf(fp, "sp->error %i\n", sp->error);
474                 }
475                 return (0);
476         }
477         if (gk_iosb.SCSI$W_VMS_STAT == SS$_TIMEOUT) {
478                 sp->error = SCG_TIMEOUT;
479                 if (usalp->debug > 0) {
480                         fprintf(fp, "scsi_sts & 2\n");
481                         fprintf(fp, "gk_iosb.SCSI$B_IOSB_STS & 2\n");
482                         fprintf(fp, "gk_iosb.SCSI$W_VMS_STAT == SS$_TIMEOUT\n");
483                         fprintf(fp, "sp->error %i\n", sp->error);
484                 }
485                 return (0);
486         }
487         sp->error = SCG_RETRYABLE;
488         sp->u_scb.cmd_scb[0] = scsi_sts;
489         if (usalp->debug > 0) {
490                 fprintf(fp, "scsi_sts & 2\n");
491                 fprintf(fp, "gk_iosb.SCSI$B_IOSB_STS & 2\n");
492                 fprintf(fp, "gk_iosb.SCSI$W_VMS_STAT != 0\n");
493                 fprintf(fp, "sp->error %i\n", sp->error);
494         }
495         return (0);
496 }
497
498 static int
499 do_usal_sense(SCSI *usalp, struct usal_cmd *sp)
500 {
501         int             ret;
502         struct usal_cmd s_cmd;
503
504         fillbytes((caddr_t)&s_cmd, sizeof (s_cmd), '\0');
505         s_cmd.addr = (char *)sp->u_sense.cmd_sense;
506         s_cmd.size = sp->sense_len;
507         s_cmd.flags = SCG_RECV_DATA|SCG_DISRE_ENA;
508         s_cmd.cdb_len = SC_G0_CDBLEN;
509         s_cmd.sense_len = CCS_SENSE_LEN;
510         s_cmd.cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
511         s_cmd.cdb.g0_cdb.lun = sp->cdb.g0_cdb.lun;
512         s_cmd.cdb.g0_cdb.count = sp->sense_len;
513         ret = do_usal_cmd(usalp, &s_cmd);
514
515         if (ret < 0)
516                 return (ret);
517         if (s_cmd.u_scb.cmd_scb[0] & 02) {
518                 /* XXX ??? Check condition on request Sense ??? */
519         }
520         sp->sense_count = sp->sense_len - s_cmd.resid;
521         return (ret);
522 }
523
524 static int
525 usalo_send(SCSI *usalp)
526 {
527         struct usal_cmd *sp = usalp->scmd;
528         int     ret;
529
530         if (usalp->fd < 0) {
531                 sp->error = SCG_FATAL;
532                 return (0);
533         }
534         ret = do_usal_cmd(usalp, sp);
535         if (ret < 0)
536                 return (ret);
537         if (sp->u_scb.cmd_scb[0] & 02)
538                 ret = do_usal_sense(usalp, sp);
539         return (ret);
540 }