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-unixware.c 1.36 04/01/15 Copyright 1998 J. Schilling, Santa Cruz Operation */
15 * Interface for the SCO UnixWare SCSI implementation.
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.
23 * Copyright (c) 1998 J. Schilling, Santa Cruz Operation
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.
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.
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.
44 #include <sys/sysmacros.h> /* XXX Falsch, richtig -> sys/mkdev.h */
46 #include <sys/sdi_edt.h>
50 * Warning: you may change this source, but if you do that
51 * you need to change the _usal_version and _usal_auth* string below.
52 * You may not return "schily" for an SCG_AUTHOR request anymore.
53 * Choose your name instead of "schily" and make clear that the version
54 * string is related to a modified source.
56 static char _usal_trans_version[] = "scsi-unixware.c-1.36"; /* The version for this transport*/
58 /* Max. number of usal scsibusses. The real limit would be */
59 /* MAX_HBA * MAX_BUS (which would be 32 * 8 on UnixWare 2.1/7.x), */
60 /* but given that we will hardly see such a beast, lets take 32 */
64 /* maximum defines for UnixWare 2.x/7.x from <sys/sdi_edt.h> */
66 #define MAX_TGT MAX_EXTCS /* Max # of target id's */
67 #define MAX_LUN MAX_EXLUS /* Max # of lun's */
69 #define MAX_DMA (32*1024)
71 #define MAX_DMA (16*1024) /* On UnixWare 2.1.x w/ AHA2940 HBA */
72 /* the max DMA size is 16KB. */
78 #define DEV_DIR "/tmp"
79 #define DEV_NAME "usal.s%1dt%1dl%1d"
81 #define SCAN_HBA "%d:%d,%d,%d:%7s : %n"
82 #define SCAN_DEV "%d,%d,%d:%7s : %n"
84 #define PRIM_HBA "/dev/hba/hba1"
85 #define SCSI_CFG "LC_ALL=C /etc/scsi/pdiconfig -l"
87 #define SCAN_ALL "LIBSCG_SCAN_ALL"
89 #define SDI_VALID 0x01 /* Entry may be used (non disk) */
90 #define SDI_ATAPI 0x02 /* Connected via IDE HBA */
91 #define SDI_INITIATOR 0x04 /* This is the initiator target ID */
93 typedef struct usal2sdi {
105 /*#define SCG_DEBUG*/
113 static usal2sdi_t sdidevs [MAX_SCG][MAX_TGT][MAX_LUN];
114 static BOOL sdiinit = FALSE;
117 short usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
119 #define usallocal(p) ((struct usal_local *)((p)->local))
121 static int unixware_init(SCSI *usalp);
122 static int do_usal_cmd(SCSI *usalp, struct usal_cmd *sp);
123 static int do_usal_sense(SCSI *usalp, struct usal_cmd *sp);
124 static FILE *xpopen(char *cmd, char *type);
125 static int xpclose(FILE *f);
128 * -------------------------------------------------------------------------
129 * SCO UnixWare 2.1.x / UnixWare 7 provides a scsi pass-through mechanism,
130 * which can be used to access any configured scsi device.
132 * NOTE: The libusal UnixWare passthrough routines have changed with
133 * cdrecord-1.8 to enable the -scanbus, -load, -eject option
134 * regardless of the status of media and the addressing
135 * scheme is now the same as used on many other platforms like
136 * Solaris, Linux etc.
138 * ===============================================================
139 * RUN 'cdrecord -scanbus' TO SEE THE DEVICE ADDRESSES YOU CAN USE
140 * ===============================================================
144 * Return version information for the low level SCSI transport code.
145 * This has been introduced to make it easier to trace down problems
150 usalo_version(SCSI *usalp, int what)
152 if (usalp != (SCSI *)0) {
156 return (_usal_trans_version);
158 * If you changed this source, you are not allowed to
159 * return "schily" for the SCG_AUTHOR request.
162 return (_usal_auth_cdrkit);
172 usalo_help(SCSI *usalp, FILE *f)
174 __usal_help(f, "SDI_SEND", "Generic SCSI",
175 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
180 * ---------------------------------------------------------------
181 * This routine is introduced to create all device nodes necessary
182 * to access any detected scsi device. It parses the output of
183 * /etc/scsi/pdiconfig -l and creates passthru device node for each
184 * found scsi device apart from the listed hba's.
189 unixware_init(SCSI *usalp)
192 int hba = 0, bus = 0, usal = 0, tgt = 0, lun = 0;
193 int nusal = -1, lhba = -1, lbus = 0;
194 int atapi, fd, nopen = 0, pos = 0, len = 0;
203 dev_t ptdev, major, minor, node;
205 extern char **environ;
207 /* Check for validity of primary hostbus adapter node */
209 if (stat(PRIM_HBA, &stbuf) < 0) {
211 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
212 "Can not stat() primary hba (%s)",
217 if (!S_ISCHR(stbuf.st_mode)) {
219 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
220 "Primary hba (%s) not a character device",
225 major = getmajor(stbuf.st_rdev);
228 * Check whether we want to scan all devices
230 if (getenv(SCAN_ALL) != NULL) {
236 /* read pdiconfig output and get all attached scsi devices ! */
240 if ((cmd = xpopen(SCSI_CFG, "r")) == NULL) {
242 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
243 "Error popen() for \"%s\"",
252 if (fgets(lines, MAXLINE, cmd) == NULL)
255 memset(class, '\0', sizeof (class));
256 memset(ident, '\0', sizeof (ident));
258 if (lines[0] == ' ') {
259 sscanf(lines, SCAN_DEV, &bus, &tgt, &lun, class, &pos);
262 sscanf(lines, SCAN_HBA, &hba, &bus, &tgt, &lun, class, &pos);
268 /* We can't sscanf() the ident string of the device */
269 /* as it may contain characters sscanf() will */
270 /* recognize as a delimiter. So do a strcpy() instead ! */
272 len = strlen(lines) - pos - 1; /* don't copy the '\n' */
274 strncpy(ident, &lines[pos], len);
276 if (usalp->debug > 0) {
277 fprintf((FILE *)usalp->errfile,
278 "SDI -> %d:%d,%d,%d: %-7s : %s\n",
279 hba, bus, tgt, lun, class, ident);
288 /* check whether we have a HBA or a SCSI device, don't */
289 /* let HBA's be valid device for cdrecord, but mark */
290 /* them as a controller (initiator = 1). */
292 /* Don't detect disks, opening a mounted disk can hang */
293 /* the disk subsystem !!! So unless we set an */
294 /* environment variable LIBSCG_SCAN_ALL, we will ignore */
297 if (strstr(class, "HBA") == NULL) {
298 if (strstr(class, "DISK") != NULL) {
300 sdidevs[usal][tgt][lun].flags |= SDI_VALID;
302 sdidevs[usal][tgt][lun].flags &= ~SDI_VALID;
304 sdidevs[usal][tgt][lun].flags |= SDI_VALID;
307 sdidevs[usal][tgt][lun].flags |= SDI_INITIATOR;
311 /* There is no real flag that shows a HBA as an ATAPI */
312 /* controller, so as we know the driver is called 'ide' */
313 /* we can check the ident string for the occurence of it*/
315 if (strstr(ident, "(ide,") != NULL) {
320 * Fill the sdidevs array with all we know now.
321 * Do not overwrite fields that may contain old state like
322 * sdidevs[usal][tgt][lun].open
326 sdidevs[usal][tgt][lun].flags |= SDI_ATAPI;
328 sdidevs[usal][tgt][lun].flags &= ~SDI_ATAPI;
330 sdidevs[usal][tgt][lun].hba = hba;
331 sdidevs[usal][tgt][lun].bus = bus;
332 sdidevs[usal][tgt][lun].tgt = tgt;
333 sdidevs[usal][tgt][lun].lun = lun;
336 strcpy(sdidevs[usal][tgt][lun].type, class);
337 strcpy(sdidevs[usal][tgt][lun].vend, ident);
339 snprintf(sdidevs[usal][tgt][lun].devn,
340 sizeof (sdidevs[usal][tgt][lun].devn),
341 DEV_NAME, usal, tgt, lun);
343 snprintf(devnm, sizeof (devnm),
344 DEV_NAME, usal, tgt, lun);
346 minor = SDI_MINOR(hba, tgt, lun, bus);
347 node = makedevice(major, minor);
349 sdidevs[usal][tgt][lun].major = major;
350 sdidevs[usal][tgt][lun].minor = minor;
351 sdidevs[usal][tgt][lun].node = node;
353 if (usalp->debug > 0) {
355 fprintf((FILE *)usalp->errfile,
356 "h = %d; b = %d, s = %d, t = %d, l = %d, a = %d, ma = %d, mi = %2d, dev = '%s', id = '%s'\n",
357 hba, bus, usal, tgt, lun,
358 (sdidevs[usal][tgt][lun].flags & SDI_ATAPI) != 0,
359 sdidevs[usal][tgt][lun].major,
360 sdidevs[usal][tgt][lun].minor,
368 if (xpclose(cmd) == -1) {
370 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
371 "Error pclose() for \"%s\"",
377 /* create all temporary device nodes */
379 for (s = 0; s < MAX_SCG; s++) {
380 for (t = 0; t < MAX_TGT; t++) {
381 for (l = 0; l < MAX_LUN; l++) {
383 if ((sdidevs[s][t][l].flags & SDI_VALID) == 0) {
384 if (sdidevs[s][t][l].fd >= 0) {
385 close(sdidevs[s][t][l].fd);
387 sdidevs[s][t][l].fd = -1;
388 sdidevs[s][t][l].open = 0;
392 /* Make pass-through interface device node */
398 snprintf(dname, sizeof (dname),
399 "%s/%s", DEV_DIR, devnm);
401 ptdev = sdidevs[s][t][l].node;
403 if (mknod(dname, S_IFCHR | 0700, ptdev) < 0) {
404 if (errno == EEXIST) {
407 if (mknod(dname, S_IFCHR | 0700, ptdev) < 0) {
409 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
410 "mknod() error for \"%s\"", dname);
415 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
416 "mknod() error for \"%s\"", dname);
421 /* Open pass-through device node */
423 if ((fd = open(dname, O_RDONLY)) < 0) {
424 if (errno == EBUSY && sdidevs[s][t][l].open > 0) {
426 * Device has already been opened, just
427 * return the saved file desc.
429 fd = sdidevs[s][t][l].fd;
432 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
433 "can not open pass-through %s", dname);
439 * If for whatever reason we may open a pass through
440 * device more than once, this will waste fs's as we
441 * do not check for sdidevs[s][t][l].fd == -1.
443 sdidevs[s][t][l].fd = fd;
444 sdidevs[s][t][l].open++;
446 usallocal(usalp)->usalfiles[s][t][l] = (short) fd;
448 if (usalp->debug > 0) {
450 fprintf((FILE *)usalp->errfile,
451 "s = %d, t = %d, l = %d, dev = %s, fd = %d\n",
454 sdidevs[s][t][l].fd);
466 usalo_open(SCSI *usalp, char *device)
468 int busno = usal_scsibus(usalp);
469 int tgt = usal_target(usalp);
470 int tlun = usal_lun(usalp);
473 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
476 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
477 "Illegal value for busno, target or lun '%d,%d,%d'",
482 if (usalp->local == NULL) {
483 usalp->local = malloc(sizeof (struct usal_local));
484 if (usalp->local == NULL)
487 for (b = 0; b < MAX_SCG; b++) {
488 for (t = 0; t < MAX_TGT; t++) {
489 for (l = 0; l < MAX_LUN; l++)
490 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
497 memset(sdidevs, 0, sizeof (sdidevs)); /* init tmp_structure */
498 for (b = 0; b < MAX_SCG; b++) {
499 for (t = 0; t < MAX_TGT; t++) {
500 for (l = 0; l < MAX_LUN; l++) {
502 sdidevs[b][t][l].flags = 0;
503 sdidevs[b][t][l].fd = -1;
504 sdidevs[b][t][l].open = 0;
510 if (*device != '\0') { /* we don't allow old dev usage */
513 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
514 "Open by 'devname' no longer supported on this OS");
516 } else { /* this is the new stuff */
517 /* it will do the initialisation */
518 /* and return the number of */
519 /* detected devices to be used */
520 /* with the new addressing */
523 return (unixware_init(usalp));
530 usalo_close(SCSI *usalp)
537 if (usalp->local == NULL)
540 for (b = 0; b < MAX_SCG; b++) {
541 for (t = 0; t < MAX_TGT; t++) {
542 for (l = 0; l < MAX_LUN; l++) {
544 f = usallocal(usalp)->usalfiles[b][t][l];
546 if (sdidevs[b][t][l].open > 0)
547 sdidevs[b][t][l].open--;
548 if (sdidevs[b][t][l].open <= 0) {
549 if (sdidevs[b][t][l].fd >= 0)
550 close(sdidevs[b][t][l].fd);
551 sdidevs[b][t][l].fd = -1;
552 sdidevs[b][t][l].flags &= ~SDI_VALID;
555 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
563 usalo_maxdma(SCSI *usalp, long amt)
570 usalo_getbuf(SCSI *usalp, long amt)
572 if (usalp->debug > 0) {
573 fprintf((FILE *)usalp->errfile,
574 "usalo_getbuf: %ld bytes\n", amt);
576 usalp->bufbase = (void *) valloc((size_t)(amt));
578 return (usalp->bufbase);
582 usalo_freebuf(SCSI *usalp)
585 free(usalp->bufbase);
586 usalp->bufbase = NULL;
590 usalo_havebus(SCSI *usalp, int busno)
595 if (busno < 0 || busno >= MAX_SCG)
598 if (usalp->local == NULL)
601 for (t = 0; t < MAX_TGT; t++) {
602 for (l = 0; l < MAX_LUN; l++)
603 if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
610 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
612 if (busno < 0 || busno >= MAX_SCG ||
613 tgt < 0 || tgt >= MAX_TGT ||
614 tlun < 0 || tlun >= MAX_LUN)
617 if (usalp->local == NULL)
620 return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
624 usalo_initiator_id(SCSI *usalp)
630 busno = usal_scsibus(usalp);
632 if (busno < 0 || busno >= MAX_SCG)
635 for (t = 0; t < MAX_TGT; t++) {
636 for (l = 0; l < MAX_LUN; l++)
637 if ((sdidevs[busno][t][l].flags & SDI_INITIATOR) != 0) {
638 if (usalp->debug > 0) {
639 fprintf((FILE *)usalp->errfile,
640 "usalo_initiator_id: id = %d\n", t);
650 usalo_isatapi(SCSI *usalp)
652 /* if the new address method is used we know if this is ATAPI */
654 return ((sdidevs[usal_scsibus(usalp)][usal_target(usalp)][usal_lun(usalp)].flags & SDI_ATAPI) != 0);
658 usalo_reset(SCSI *usalp, int what)
664 #if defined(SDI_TRESET) || defined(SDI_BRESET)
665 if (what == SCG_RESET_NOP) {
671 if (what == SCG_RESET_TGT) {
673 if (ioctl(f, SDI_TRESET, 0) >= 0)
679 if (what == SCG_RESET_BUS) {
681 if (ioctl(f, SDI_BRESET, 0) >= 0)
686 #endif /* defined(SDI_TRESET) || defined(SDI_BRESET) */
692 do_usal_cmd(SCSI *usalp, struct usal_cmd *sp)
699 memset(&scsi_cmd, 0, sizeof (scsi_cmd));
701 scsi_cmd.sb_type = ISCB_TYPE;
702 scbp = &scsi_cmd.SCB;
704 scbp->sc_cmdpt = (caddr_t) sp->cdb.cmd_cdb;
705 scbp->sc_cmdsz = sp->cdb_len;
707 scbp->sc_datapt = sp->addr;
708 scbp->sc_datasz = sp->size;
710 if (!(sp->flags & SCG_RECV_DATA) && (sp->size > 0))
711 scbp->sc_mode = SCB_WRITE;
713 scbp->sc_mode = SCB_READ;
715 scbp->sc_time = sp->timeout;
717 sp->error = SCG_NO_ERROR;
720 if ((ret = ioctl(usalp->fd, SDI_SEND, &scsi_cmd)) < 0) {
721 if (errno == EAGAIN) {
726 sp->ux_errno = errno;
729 sp->error = SCG_RETRYABLE;
732 if (errno == ENOTTY || errno == EINVAL ||
741 sp->ux_errno = errno;
742 sp->resid = scbp->sc_resid;
743 memset(&sp->u_scb.Scb, 0, sizeof (sp->u_scb.Scb));
744 sp->u_scb.cmd_scb[0] = scbp->sc_status;
746 if (sp->u_scb.cmd_scb[0] & 0x02) {
747 if (sp->ux_errno == 0)
751 switch (scbp->sc_comp_code) {
753 case SDI_ASW : /* Job completed normally */
754 case SDI_LINKF0 : /* Linked command done without flag */
755 case SDI_LINKF1 : /* Linked command done with flag */
757 sp->error = SCG_NO_ERROR;
760 case SDI_CKSTAT : /* Check the status byte */
762 sp->error = SCG_NO_ERROR;
765 case SDI_NOALLOC : /* This block is not allocated */
766 case SDI_NOTEQ : /* Addressed device not present */
767 case SDI_OOS : /* Device is out of service */
768 case SDI_NOSELE : /* The SCSI bus select failed */
769 case SDI_SBRESC : /* SCSI bus reservation conflict */
771 sp->error = SCG_FATAL;
772 if (sp->ux_errno == 0)
776 case SDI_QFLUSH : /* Job was flushed */
777 case SDI_ABORT : /* Command was aborted */
778 case SDI_RESET : /* Reset was detected on the bus */
779 case SDI_CRESET : /* Reset was caused by this unit */
780 case SDI_V2PERR : /* vtop failed */
781 case SDI_HAERR : /* Host adapter error */
782 case SDI_MEMERR : /* Memory fault */
783 case SDI_SBUSER : /* SCSI bus error */
784 case SDI_SCBERR : /* SCB error */
785 case SDI_MISMAT : /* parameter mismatch */
787 case SDI_PROGRES : /* Job in progress */
788 case SDI_UNUSED : /* Job not in use */
790 case SDI_ONEIC : /* More than one immediate request */
791 case SDI_SFBERR : /* SFB error */
792 case SDI_TCERR : /* Target protocol error detected */
794 sp->error = SCG_RETRYABLE;
795 if (sp->ux_errno == 0)
799 case SDI_TIME : /* Job timed out */
800 case SDI_TIME_NOABORT : /* Job timed out, but could not be aborted */
802 sp->error = SCG_TIMEOUT;
803 if (sp->ux_errno == 0)
812 do_usal_sense(SCSI *usalp, struct usal_cmd *sp)
815 struct usal_cmd s_cmd;
817 memset((caddr_t)&s_cmd, 0, sizeof (s_cmd));
819 s_cmd.addr = (caddr_t) sp->u_sense.cmd_sense;
820 s_cmd.size = sp->sense_len;
821 s_cmd.flags = SCG_RECV_DATA|SCG_DISRE_ENA;
822 s_cmd.cdb_len = SC_G0_CDBLEN;
823 s_cmd.sense_len = CCS_SENSE_LEN;
825 s_cmd.cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
826 s_cmd.cdb.g0_cdb.lun = sp->cdb.g0_cdb.lun;
827 s_cmd.cdb.g0_cdb.count = sp->sense_len;
829 ret = do_usal_cmd(usalp, &s_cmd);
834 sp->sense_count = sp->sense_len - s_cmd.resid;
839 usalo_send(SCSI *usalp)
841 struct usal_cmd *sp = usalp->scmd;
845 sp->error = SCG_FATAL;
849 ret = do_usal_cmd(usalp, sp);
853 if (sp->u_scb.cmd_scb[0] & S_CKCON)
854 ret = do_usal_sense(usalp, sp);
859 #define sense u_sense.Sense
861 #define SC_PARITY 0x09
862 #define scb u_scb.Scb
864 /*--------------------------------------------------------------------------*/
866 #include <waitdefs.h>
868 * Simplified version of popen()
869 * This version of popen() is not usable more than once at a time.
870 * Needed because /etc/scsi/pdiconfig will not work if euid != uid
875 xpopen(char *cmd, char *type)
881 return ((FILE *)NULL);
884 return ((FILE *)NULL);
887 return ((FILE *)NULL);
890 if ((po_pid = fork()) == 0) {
894 (void) rols_fexecl("/bin/sh", stdin, pp[1], stderr,
895 "sh", "-c", cmd, (char *)0);
900 if (po_pid == (pid_t)-1) {
902 return ((FILE *)NULL);
917 if (waitpid(po_pid, &ret, 0) < 0)