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-linux-sg.c 1.86 05/11/22 Copyright 1997 J. Schilling */
15 * Interface for Linux generic SCSI implementation (sg).
17 * This is the interface for the broken Linux SCSI generic driver.
18 * This is a hack, that tries to emulate the functionality
21 * Design flaws of the sg driver:
22 * - cannot see if SCSI command could not be send
23 * - cannot get SCSI status byte
24 * - cannot get real dma count of tranfer
25 * - cannot get number of bytes valid in auto sense data
26 * - to few data in auto sense (CCS/SCSI-2/SCSI-3 needs >= 18)
28 * This code contains support for the sg driver version 2 by
29 * H. Eißfeld & J. Schilling
30 * Although this enhanced version has been announced to Linus and Alan,
31 * there was no reaction at all.
33 * About half a year later there occured a version in the official
34 * Linux that was also called version 2. The interface of this version
35 * looks like a playground - the enhancements from this version are
36 * more or less useless for a portable real-world program.
38 * With Linux 2.4 the official version of the sg driver is called 3.x
39 * and seems to be usable again. The main problem now is the curious
40 * interface that is provided to raise the DMA limit from 32 kB to a
41 * more reasonable value. To do this in a reliable way, a lot of actions
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.
50 * Copyright (c) 1997 J. Schilling
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License version 2
55 * as published by the Free Software Foundation.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60 * GNU General Public License for more details.
62 * You should have received a copy of the GNU General Public License along with
63 * this program; see the file COPYING. If not, write to the Free Software
64 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
67 #include <linux/version.h>
68 #include <sys/types.h>
70 #include <sys/utsname.h>
72 #ifndef LINUX_VERSION_CODE /* Very old kernel? */
73 # define LINUX_VERSION_CODE 0
76 #if LINUX_VERSION_CODE >= 0x01031a /* <linux/scsi.h> introduced in 1.3.26 */
77 #if LINUX_VERSION_CODE >= 0x020000 /* <scsi/scsi.h> introduced somewhere. */
78 /* Need to fine tune the ifdef so we get the transition point right. */
79 #include <scsi/scsi.h>
81 #include <linux/scsi.h>
83 #else /* LINUX_VERSION_CODE == 0 Very old kernel? */
84 #define __KERNEL__ /* Some Linux Include files are inconsistent */
85 #include <linux/fs.h> /* From ancient versions, really needed? */
87 #include "block/blk.h" /* From ancient versions, really needed? */
88 #include "scsi/scsi.h"
91 #if defined(HAVE_BROKEN_SCSI_SG_H) || \
92 defined(HAVE_BROKEN_SRC_SCSI_SG_H)
94 * Be very careful in case that the Linux Kernel maintainers
95 * unexpectedly fix the bugs in the Linux Lernel include files.
96 * Only introduce the attempt for a workaround in case the include
97 * files are broken anyway.
102 #if defined(HAVE_BROKEN_SCSI_SG_H) || \
103 defined(HAVE_BROKEN_SRC_SCSI_SG_H)
107 #undef sense /* conflict in struct cdrom_generic_command */
108 #include <linux/cdrom.h>
110 #if defined(CDROM_PACKET_SIZE) && defined(CDROM_SEND_PACKET)
111 #define USE_OLD_ATAPI
117 * Warning: you may change this source, but if you do that
118 * you need to change the _usal_version and _usal_auth* string below.
119 * You may not return "schily" for an SCG_AUTHOR request anymore.
120 * Choose your name instead of "schily" and make clear that the version
121 * string is related to a modified source.
123 static char _usal_trans_version[] = "scsi-linux-sg.c-1.86"; /* The version for this transport*/
125 #ifndef SCSI_IOCTL_GET_BUS_NUMBER
126 #define SCSI_IOCTL_GET_BUS_NUMBER 0x5386
130 * XXX There must be a better way than duplicating things from system include
131 * XXX files. This is stolen from /usr/src/linux/drivers/scsi/scsi.h
134 #define DID_OK 0x00 /* NO error */
135 #define DID_NO_CONNECT 0x01 /* Couldn't connect before timeout period */
136 #define DID_BUS_BUSY 0x02 /* BUS stayed busy through time out period */
137 #define DID_TIME_OUT 0x03 /* TIMED OUT for other reason */
138 #define DID_BAD_TARGET 0x04 /* BAD target. */
139 #define DID_ABORT 0x05 /* Told to abort for some other reason */
140 #define DID_PARITY 0x06 /* Parity error */
141 #define DID_ERROR 0x07 /* Internal error */
142 #define DID_RESET 0x08 /* Reset by somebody. */
143 #define DID_BAD_INTR 0x09 /* Got an interrupt we weren't expecting. */
147 * These indicate the error that occurred, and what is available.
150 #define DRIVER_BUSY 0x01
151 #define DRIVER_SOFT 0x02
152 #define DRIVER_MEDIA 0x03
153 #define DRIVER_ERROR 0x04
155 #define DRIVER_INVALID 0x05
156 #define DRIVER_TIMEOUT 0x06
157 #define DRIVER_HARD 0x07
158 #define DRIVER_SENSE 0x08
162 * XXX Should add extra space in buscookies and usalfiles for a "PP bus"
163 * XXX and for two or more "ATAPI busses".
165 #define MAX_SCG 1256 /* Max # of SCSI controllers */
171 * # of virtual buses (schilly_host number)
173 #define MAX_SCHILLY_HOSTS MAX_SCG
182 int usalfile; /* Used for SG_GET_BUFSIZE ioctl()*/
183 short usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
184 char *filenames[MAX_SCG][MAX_TGT][MAX_LUN];
185 short buscookies[MAX_SCG];
187 int pack_id; /* Should be a random number */
195 ata_buscookies bc[MAX_SCHILLY_HOSTS];
198 #define usallocal(p) ((struct usal_local *)((p)->local))
205 #define MAX_DMA_LINUX SG_BIG_BUFF /* Defined in include/scsi/sg.h */
207 #define MAX_DMA_LINUX (4*1024) /* Old Linux versions */
211 # define SG_MAX_SENSE 16 /* Too small for CCS / SCSI-2 */
212 #endif /* But cannot be changed */
214 #if !defined(__i386) && !defined(i386) && !defined(mc68000)
218 /*#undef SG_GET_BUFSIZE*/
222 static int sg_getint(int *ip);
224 static int usalo_send(SCSI *usalp);
226 static int sg_rwsend(SCSI *usalp);
228 static void sg_clearnblock(int f);
229 static BOOL sg_setup(SCSI *usalp, int f, int busno, int tgt, int tlun,
230 int ataidx, char *origname);
231 static void sg_initdev(SCSI *usalp, int f);
232 static int sg_mapbus(SCSI *usalp, int busno, int ino);
233 static BOOL sg_mapdev(SCSI *usalp, int f, int *busp, int *tgtp, int *lunp,
234 int *chanp, int *inop, int ataidx);
235 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
236 static long sg_raisedma(SCSI *usalp, long newmax);
238 static void sg_settimeout(int f, int timeout);
240 int sg_open_excl(char *device, int mode, BOOL beQuiet);
242 static BOOL get_max_secs(char *dirpath, int *outval);
244 #if defined(USE_PG) && !defined(USE_PG_ONLY)
245 #include "scsi-linux-pg.c"
248 #include "scsi-linux-ata.c"
251 BOOL check_linux_26() {
254 return ( 0==uname( &buf ) && sscanf(buf.release, "%d.%d", &gen, &tmp)>1 && tmp>=6);
257 int sg_open_excl(char *device, int mode, BOOL beQuiet)
261 long interval = beQuiet ? 400000 : 1000000;
263 f = open(device, mode|O_EXCL);
264 /* try to reopen locked/busy devices up to five times */
265 for (i = 0; (i < 5) && (f == -1 && errno == EBUSY); i++) {
267 fprintf(stderr, "Error trying to open %s exclusively (%s)... %s\n",
268 device, strerror(errno),
269 (i<4)?"retrying in 1 second.":"giving up.");
270 usleep(interval + interval * rand()/(RAND_MAX+1.0));
271 f = open(device, mode|O_EXCL);
273 if(i==5 && !beQuiet) {
274 FILE *g = fopen("/proc/mounts", "r");
277 unsigned int len=strlen(device);
278 while(!feof(g) && !ferror(g)) {
279 if(fgets(buf, 79, g) && 0==strncmp(device, buf, len)) {
280 fprintf(stderr, "WARNING: %s seems to be mounted!\n", device);
290 // Dead code, that sysfs parts may become deprecated soon
291 void map_sg_to_block(char *device, int len) {
294 snprintf(globpat, 100, "/sys/class/scsi_generic/%s/device/block:*", device+5);
295 memset(&globbuf, 0, sizeof(glob_t));
296 if(0==glob(globpat, GLOB_DOOFFS | GLOB_NOSORT, NULL, &globbuf)) {
297 char *p = strrchr(globbuf.gl_pathv[0], ':');
298 if(p) snprintf(device, len, "/dev/%s", p+1);
305 * Return version information for the low level SCSI transport code.
306 * This has been introduced to make it easier to trace down problems
310 usalo_version(SCSI *usalp, int what)
312 if (usalp != (SCSI *)0) {
316 * If we only have a Parallel port or only opened a handle
317 * for PP, just return PP version.
319 if (usallocal(usalp)->pgbus == 0 ||
320 (usal_scsibus(usalp) >= 0 &&
321 usal_scsibus(usalp) == usallocal(usalp)->pgbus))
322 return (pg_version(usalp, what));
327 return (_usal_trans_version);
329 * If you changed this source, you are not allowed to
330 * return "schily" for the SCG_AUTHOR request.
333 return (_usal_auth_cdrkit);
341 if (usallocal(usalp)->drvers >= 0) {
342 n = usallocal(usalp)->drvers;
343 snprintf(kv, sizeof (kv),
345 n/10000, (n%10000)/100, n%100);
356 usalo_help(SCSI *usalp, FILE *f)
358 __usal_help(f, "sg", "Generic transport independent SCSI",
359 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
364 usalo_ahelp(usalp, f);
366 __usal_help(f, "ATA", "ATA Packet specific SCSI transport using sg interface",
367 "ATA:", "bus,target,lun", "1,2,0", TRUE, FALSE);
371 #define in_scanmode (busno < 0 && tgt < 0 && tlun < 0)
374 * b/t/l is chopped of the device string.
377 usalo_open(SCSI *usalp, char *device)
379 int busno = usal_scsibus(usalp);
380 int tgt = usal_target(usalp);
381 int tlun = usal_lun(usalp);
387 register int nopen = 0;
391 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
394 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
395 "Illegal value for busno, target or lun '%d,%d,%d'",
401 if(check_linux_26() && 0!=stat("/sys/kernel", &statbuf)) {
402 static int warn_sysfs=1;
405 fprintf(stderr, "\nWarning, sysfs is not mounted on /sys!\n"
406 "It is recommended to mount sysfs to allow better device configuration\n");
411 if (device != NULL && *device != '\0') {
413 if(0==strncmp(device, "OLDATAPI", 8)) {
415 usalp->ops = &ata_ops;
416 return (SCGO_OPEN(usalp, device));
418 else if(0==strncmp(device, "ATAPI", 5)) {
419 if(check_linux_26()) {
422 fprintf(stderr, "WARNING: the ATAPI: method is considered deprecated on modern kernels!\n"
423 "Mapping device specification to ATA: method now.\n"
424 "To force the old ATAPI: method, replace ATAPI: with OLDATAPI:\n");
427 usalp->ops = &ata_ops;
428 return (SCGO_OPEN(usalp, device));
431 else if(0==strncmp(device, "ATA", 3)) {
432 fprintf(stderr, "WARNING: the ATA: method is considered deprecated on modern kernels!\n"
433 "Use --devices to display the native names.\n");
441 else if( ! in_scanmode ) {
442 fprintf(stderr, "WARNING: the deprecated pseudo SCSI syntax found as device specification.\n"
443 "Support for that may cease in the future versions of wodim. For now,\n"
444 "the device will be mapped to a block device file where possible.\n"
445 "Run \"wodim --devices\" for details.\n" );
449 if (usalp->local == NULL) {
450 usalp->local = malloc(sizeof (struct usal_local));
451 if (usalp->local == NULL)
454 usallocal(usalp)->usalfile = -1;
455 usallocal(usalp)->pgbus = -2;
456 usallocal(usalp)->SCSIbuf = (char *)-1;
457 usallocal(usalp)->pack_id = 5;
458 usallocal(usalp)->drvers = -1;
459 usallocal(usalp)->isold = -1;
460 usallocal(usalp)->flags = 0;
461 usallocal(usalp)->xbufsize = 0L;
462 usallocal(usalp)->xbuf = NULL;
464 for (b = 0; b < MAX_SCG; b++) {
465 usallocal(usalp)->buscookies[b] = (short)-1;
466 for (t = 0; t < MAX_TGT; t++) {
467 for (l = 0; l < MAX_LUN; l++) {
468 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
469 usallocal(usalp)->filenames[b][t][l] = NULL;
475 if (device != NULL && *device != '\0')
477 /* open ONE directly */
479 if (device && strncmp(device, "/dev/hd", 7) == 0 && device[8]=='\0') {
484 if(b>=0 && fake_atabus)
487 f = sg_open_excl(device, O_RDWR | O_NONBLOCK, FALSE);
491 * The pg driver has the same rules to decide whether
492 * to use openbydev. If we cannot open the device, it
493 * makes no sense to try the /dev/pg* driver.
496 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
502 /* get some fake SCSI data */
503 sg_mapdev(usalp, f, &busno, &tgt, &tlun, 0, 0, b);
504 usal_settarget(usalp, busno, tgt, tlun);
505 if (sg_setup(usalp, f, busno, tgt, tlun, b, device))
509 /* scan and maybe keep one open, sg_setup decides */
517 for(h=HDX; h <= (fake_atabus ? HDX : SG) ; h++) {
518 char *pattern = NULL;
519 unsigned int first = 0, last = 0;
530 if(!check_linux_26())
532 pattern="/dev/scd%d";
543 * Don't touch it on 2.6 until we have a proper locking scheme
546 fprintf(stderr, "Warning, using /dev/sg* for SG_IO operation. This method is considered harmful.\n");
556 for(i=first; i<=last; i++) {
557 snprintf(devname, sizeof (devname), pattern, i);
558 f = sg_open_excl(devname, O_RDWR | O_NONBLOCK, in_scanmode);
561 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
562 "Cannot open '%s'", devname);
564 if(h == HDX) { // double-check the capabilities on ATAPI devices
567 if (ioctl(f, SG_GET_TIMEOUT, &iparm) < 0) {
569 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
570 "SCSI unsupported with '%s'", devname);
575 sg_clearnblock(f); /* Be very proper about this */
577 /* construct the fake bus number hint, keep it readable */
585 /* sg_setup returns false in scan mode, true if one single target was specified and opened */
586 if (sg_setup(usalp, f, busno, tgt, tlun, b, devname))
594 if (nopen > 0 && usalp->errstr)
595 usalp->errstr[0] = '\0';
597 /* that's crap, should not be reached in non-scan mode.
598 * Let's see whether it can be mapped to an atapi
599 * device to emulate some old cludge's behaviour.
600 if(!in_scanmode && busno < 1000 && busno >=0) {
602 fprintf(stderr, "Unable to open this SCSI ID. Trying to map to old ATA syntax."
603 "This workaround will disappear in the near future. Fix your configuration.");
604 goto retry_scan_open;
610 if (usalp->debug > 0) for (b = 0; b < MAX_SCG; b++) {
611 fprintf((FILE *)usalp->errfile,
612 "Bus: %d cookie: %X\n",
613 b, usallocal(usalp)->buscookies[b]);
614 for (t = 0; t < MAX_TGT; t++) {
615 for (l = 0; l < MAX_LUN; l++) {
616 if (usallocal(usalp)->usalfiles[b][t][l] != (short)-1) {
617 fprintf((FILE *)usalp->errfile,
618 "file (%d,%d,%d): %d\n",
619 b, t, l, usallocal(usalp)->usalfiles[b][t][l]);
629 usalo_close(SCSI *usalp)
636 if (usalp->local == NULL)
639 for (b = 0; b < MAX_SCG; b++) {
640 if (b == usallocal(usalp)->pgbus)
642 usallocal(usalp)->buscookies[b] = (short)-1;
643 for (t = 0; t < MAX_TGT; t++) {
644 for (l = 0; l < MAX_LUN; l++) {
645 f = usallocal(usalp)->usalfiles[b][t][l];
648 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
649 if(usallocal(usalp)->filenames[b][t][l]) {
650 free(usallocal(usalp)->filenames[b][t][l]);
651 usallocal(usalp)->filenames[b][t][l]=NULL;
656 if (usallocal(usalp)->xbuf != NULL) {
657 free(usallocal(usalp)->xbuf);
658 usallocal(usalp)->xbufsize = 0L;
659 usallocal(usalp)->xbuf = NULL;
668 * The Linux kernel becomes more and more unmaintainable.
669 * Every year, a new incompatible SCSI transport interface is added.
670 * Each of them has it's own contradictory constraints.
671 * While you cannot have O_NONBLOCK set during operation, at least one
672 * of the drivers requires O_NONBLOCK to be set during open().
673 * This is used to clear O_NONBLOCK immediately after open() succeeded.
676 sg_clearnblock(int f)
680 n = fcntl(f, F_GETFL);
682 fcntl(f, F_SETFL, n);
687 * Return: TRUE when single target is chosen and was opened successfully, FALSE otherwise (on scans, etc).
691 sg_setup(SCSI *usalp, int f, int busno, int tgt, int tlun, int ataidx, char *origname)
699 BOOL onetarget = FALSE;
701 #ifdef SG_GET_VERSION_NUM
702 if (usallocal(usalp)->drvers < 0) {
703 usallocal(usalp)->drvers = 0;
704 if (ioctl(f, SG_GET_VERSION_NUM, &n) >= 0) {
705 usallocal(usalp)->drvers = n;
706 if (usalp->overbose) {
707 fprintf((FILE *)usalp->errfile,
708 "Linux sg driver version: %d.%d.%d\n",
709 n/10000, (n%10000)/100, n%100);
714 if (usal_scsibus(usalp) >= 0 && usal_target(usalp) >= 0 && usal_lun(usalp) >= 0)
717 sg_mapdev(usalp, f, &Bus, &Target, &Lun, &Chan, &Ino, ataidx);
719 * For old kernels try to make the best guess.
722 n = sg_mapbus(usalp, Bus, Ino);
725 if (usalp->debug > 0) {
726 fprintf((FILE *)usalp->errfile,
727 "SCSI Bus: %d (mapped from %d)\n", Bus, Ino);
731 if (Bus < 0 || Bus >= MAX_SCG || Target < 0 || Target >= MAX_TGT ||
732 Lun < 0 || Lun >= MAX_LUN) {
736 if (usallocal(usalp)->usalfiles[Bus][Target][Lun] == (short)-1)
737 usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)f;
739 if (usallocal(usalp)->filenames[Bus][Target][Lun] == NULL)
740 usallocal(usalp)->filenames[Bus][Target][Lun] = strdup(origname);
743 if (Bus == busno && Target == tgt && Lun == tlun) {
744 sg_initdev(usalp, f);
745 usallocal(usalp)->usalfile = f; /* remember file for ioctl's */
748 usallocal(usalp)->usalfiles[Bus][Target][Lun] = (short)-1;
753 * SCSI bus scanning may cause other generic SCSI activities to
754 * fail because we set the default timeout and clear command
755 * queues (in case of the old sg driver interface).
757 sg_initdev(usalp, f);
758 if (usallocal(usalp)->usalfile < 0)
759 usallocal(usalp)->usalfile = f; /* remember file for ioctl's */
765 sg_initdev(SCSI *usalp, int f)
769 unsigned char rbuf[100];
775 sg_settimeout(f, usalp->deftimeout);
778 * If it's a block device, don't read.... pre Linux-2.4 /dev/sg*
779 * definitely is a character device and we only need to clear the
780 * queue for old /dev/sg* versions. If somebody ever implements
781 * raw disk access for Linux, this test may fail.
783 if (fstat(f, &sb) >= 0 && S_ISBLK(sb.st_mode))
786 /* Eat any unwanted garbage from prior use of this device */
788 n = fcntl(f, F_GETFL); /* Be very proper about this */
789 fcntl(f, F_SETFL, n|O_NONBLOCK);
791 fillbytes((caddr_t)&sg_rep, sizeof (struct sg_header), '\0');
792 sg_rep.hd.reply_len = sizeof (struct sg_header);
795 * This is really ugly.
796 * We come here if 'f' is related to a raw device. If Linux
797 * will ever have raw devices for /dev/hd* we may get problems.
798 * As long as there is no clean way to find out whether the
799 * filedescriptor 'f' is related to an old /dev/sg* or to
800 * /dev/hd*, we must assume that we found an old /dev/sg* and
801 * clean it up. Unfortunately, reading from /dev/hd* will
804 for (i = 0; i < 1000; i++) { /* Read at least 32k from /dev/sg* */
807 ret = read(f, &sg_rep, sizeof (struct sg_rep));
810 if (ret == 0 || errno == EAGAIN || errno == EIO)
812 if (ret < 0 && i > 10) /* Stop on repeated unknown error */
815 fcntl(f, F_SETFL, n);
819 sg_mapbus(SCSI *usalp, int busno, int ino)
823 if (busno >= 0 && busno < MAX_SCG) {
825 * SCSI_IOCTL_GET_BUS_NUMBER worked.
826 * Now we have the problem that Linux does not properly number
827 * SCSI busses. The Bus number that Linux creates really is
828 * the controller (card) number. I case of multi SCSI bus
831 if (usallocal(usalp)->buscookies[busno] == (short)-1) {
832 usallocal(usalp)->buscookies[busno] = ino;
836 * if (usallocal(usalp)->buscookies[busno] != (short)ino)
837 errmsgno(EX_BAD, "Warning Linux Bus mapping botch.\n");
841 } else for (i = 0; i < MAX_SCG; i++) {
842 if (usallocal(usalp)->buscookies[i] == (short)-1) {
843 usallocal(usalp)->buscookies[i] = ino;
847 if (usallocal(usalp)->buscookies[i] == ino)
854 sg_mapdev(SCSI *usalp, int f, int *busp, int *tgtp, int *lunp, int *chanp,
855 int *inop, int ataidx)
858 long l1; /* target | lun << 8 | channel << 16 | low_ino << 24 */
859 long l2; /* Unique id */
869 * The badly designed /dev/hd* interface maps everything
870 * to 0,0,0 so we need to do the mapping ourselves.
872 *busp = (ataidx/1000) * 1000;
881 if (ioctl(f, SCSI_IOCTL_GET_IDLUN, &sg_id))
883 if (usalp->debug > 0) {
884 fprintf((FILE *)usalp->errfile,
885 "l1: 0x%lX l2: 0x%lX\n", sg_id.l1, sg_id.l2);
887 if (ioctl(f, SCSI_IOCTL_GET_BUS_NUMBER, &Bus) < 0) {
891 Target = sg_id.l1 & 0xFF;
892 Lun = (sg_id.l1 >> 8) & 0xFF;
893 Chan = (sg_id.l1 >> 16) & 0xFF;
894 Ino = (sg_id.l1 >> 24) & 0xFF;
895 if (usalp->debug > 0) {
896 fprintf((FILE *)usalp->errfile,
897 "Bus: %d Target: %d Lun: %d Chan: %d Ino: %d\n",
898 Bus, Target, Lun, Chan, Ino);
910 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
912 * The way Linux does DMA resouce management is a bit curious.
913 * It totally deviates from all other OS and forces long ugly code.
914 * If we are opening all drivers for a SCSI bus scan operation, we need
915 * to set the limit for all open devices.
916 * This may use up all kernel memory ... so do the job carefully.
918 * A big problem is that SG_SET_RESERVED_SIZE does not return any hint
919 * on whether the request did fail. The only way to find if it worked
920 * is to use SG_GET_RESERVED_SIZE to read back the current values.
923 sg_raisedma(SCSI *usalp, long newmax)
933 * First try to raise the DMA limit to a moderate value that
934 * most likely does not use up all kernel memory.
938 if (val > MAX_DMA_LINUX) {
939 for (b = 0; b < MAX_SCG; b++) {
940 for (t = 0; t < MAX_TGT; t++) {
941 for (l = 0; l < MAX_LUN; l++) {
942 if ((f = SCGO_FILENO(usalp, b, t, l)) < 0)
945 if (ioctl(f, SG_GET_RESERVED_SIZE, &old) < 0)
948 ioctl(f, SG_SET_RESERVED_SIZE, &val);
955 * Now to raise the DMA limit to what we really need.
959 for (b = 0; b < MAX_SCG; b++) {
960 for (t = 0; t < MAX_TGT; t++) {
961 for (l = 0; l < MAX_LUN; l++) {
962 if ((f = SCGO_FILENO(usalp, b, t, l)) < 0)
965 if (ioctl(f, SG_GET_RESERVED_SIZE, &old) < 0)
968 ioctl(f, SG_SET_RESERVED_SIZE, &val);
975 * To make sure we did not fail (the ioctl does not report errors)
976 * we need to check the DMA limits. We return the smallest value.
978 for (b = 0; b < MAX_SCG; b++) {
979 for (t = 0; t < MAX_TGT; t++) {
980 for (l = 0; l < MAX_LUN; l++) {
981 if ((f = SCGO_FILENO(usalp, b, t, l)) < 0)
983 if (ioctl(f, SG_GET_RESERVED_SIZE, &val) < 0)
985 if (usalp->debug > 0) {
986 fprintf((FILE *)usalp->errfile,
987 "Target (%d,%d,%d): DMA max %d old max: %ld\n",
988 b, t, l, val, newmax);
995 return ((long)newmax);
999 static char *freadstring(char *fn, char *out, int len) {
1001 FILE *fd=fopen(fn, "r");
1003 if(!fd) return NULL;
1004 ret = fgets(out, len, fd);
1010 usalo_maxdma(SCSI *usalp, long amt)
1013 long maxdma = MAX_DMA_LINUX;
1015 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
1017 * Use the curious new kernel interface found on Linux >= 2.2.10
1018 * This interface first appeared in 2.2.6 but it was not working.
1020 if (usallocal(usalp)->drvers >= 20134)
1021 maxdma = sg_raisedma(usalp, amt);
1024 * First try the modern kernel 2.6.1x way to detect the real maximum
1025 * DMA for this specific device, then try the other methods.
1027 if(0==fstat(usallocal(usalp)->usalfile, &stbuf)) {
1028 /* that's ugly, there are so many symlinks in sysfs but none from
1029 * major:minor to the relevant directory */
1030 long int major, minor, i;
1031 major=stbuf.st_rdev>>8;
1032 minor=stbuf.st_rdev&0xFF;
1033 if (usalp->debug > 0)
1034 fprintf(stderr, "Looking for data for major:minor: %ld:%ld\n", major, minor);
1036 memset(&globbuf, 0, sizeof(glob_t));
1037 /* *dev files contain the major:minor strings to compare */
1038 glob("/sys/class/scsi_generic/*/device/block*/queue/max_sectors_kb", GLOB_DOOFFS | GLOB_NOSORT, NULL, &globbuf);
1039 glob("/sys/block/*/device/block*/queue/max_sectors_kb", GLOB_DOOFFS | GLOB_NOSORT | GLOB_APPEND, NULL, &globbuf);
1040 for(i=0;i<globbuf.gl_pathc; i++) {
1043 cut=strstr(globbuf.gl_pathv[i], "/device/")+4;
1045 freadstring(globbuf.gl_pathv[i], buf, sizeof(buf));
1046 if(strtol(buf, &ende, 10) == major && ende && atoi(ende) == minor) {
1048 freadstring(globbuf.gl_pathv[i], buf, sizeof(buf));
1049 return(1024*atoi(buf));
1055 #ifdef SG_GET_BUFSIZE
1057 * We assume that all /dev/sg instances use the same
1058 * maximum buffer size.
1060 maxdma = ioctl(usallocal(usalp)->usalfile, SG_GET_BUFSIZE, 0);
1065 * If we only have a Parallel port, just return PP maxdma.
1067 if (usallocal(usalp)->pgbus == 0)
1068 return (pg_maxdma(usalp, amt));
1070 if (usallocal(usalp)->usalfile >= 0)
1071 maxdma = MAX_DMA_LINUX;
1074 if (usal_scsibus(usalp) == usallocal(usalp)->pgbus)
1075 return (pg_maxdma(usalp, amt));
1076 if ((usal_scsibus(usalp) < 0) && (pg_maxdma(usalp, amt) < maxdma))
1077 return (pg_maxdma(usalp, amt));
1083 usalo_getbuf(SCSI *usalp, long amt)
1087 if (usalp->debug > 0) {
1088 fprintf((FILE *)usalp->errfile,
1089 "usalo_getbuf: %ld bytes\n", amt);
1092 * For performance reason, we allocate pagesize()
1093 * bytes before the SCSI buffer to avoid
1094 * copying the whole buffer contents when
1095 * setting up the /dev/sg data structures.
1097 ret = valloc((size_t)(amt+getpagesize()));
1100 usalp->bufbase = ret;
1101 ret += getpagesize();
1102 usallocal(usalp)->SCSIbuf = ret;
1103 return ((void *)ret);
1107 usalo_freebuf(SCSI *usalp)
1110 free(usalp->bufbase);
1111 usalp->bufbase = NULL;
1115 usalo_havebus(SCSI *usalp, int busno)
1120 if (busno < 0 || busno >= MAX_SCG)
1123 if (usalp->local == NULL)
1126 for (t = 0; t < MAX_TGT; t++) {
1127 for (l = 0; l < MAX_LUN; l++)
1128 if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
1135 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
1137 if (busno < 0 || busno >= MAX_SCG ||
1138 tgt < 0 || tgt >= MAX_TGT ||
1139 tlun < 0 || tlun >= MAX_LUN)
1142 if (usalp->local == NULL)
1145 return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
1149 usalo_initiator_id(SCSI *usalp)
1152 if (usal_scsibus(usalp) == usallocal(usalp)->pgbus)
1153 return (pg_initiator_id(usalp));
1159 usalo_isatapi(SCSI *usalp)
1164 * Who exactly needs this information? Just for some bitching in wodim?
1165 * Is this an _abstraction_ layer or spam layer?
1168 if (usal_scsibus(usalp) == usallocal(usalp)->pgbus)
1169 return (pg_isatapi(usalp));
1173 * The /dev/hd* interface always returns TRUE for SG_EMULATED_HOST.
1174 * So this is completely useless.
1176 if (usallocal(usalp)->flags & LF_ATA)
1179 #ifdef SG_EMULATED_HOST
1181 int emulated = FALSE;
1184 * XXX Should we use this at all?
1185 * XXX The badly designed /dev/hd* interface always
1186 * XXX returns TRUE, even when used with e.g. /dev/sr0.
1188 if (ioctl(usalp->fd, SG_EMULATED_HOST, &emulated) >= 0)
1189 return (emulated != 0);
1197 usalo_reset(SCSI *usalp, int what)
1199 #ifdef SG_SCSI_RESET
1204 if (usal_scsibus(usalp) == usallocal(usalp)->pgbus)
1205 return (pg_reset(usalp, what));
1208 * Do we have a SCSI reset in the Linux sg driver?
1210 #ifdef SG_SCSI_RESET
1212 * Newer Linux sg driver seem to finally implement it...
1214 #ifdef SG_SCSI_RESET_NOTHING
1215 func = SG_SCSI_RESET_NOTHING;
1216 if (ioctl(f, SG_SCSI_RESET, &func) >= 0) {
1217 if (what == SCG_RESET_NOP)
1219 #ifdef SG_SCSI_RESET_DEVICE
1220 if (what == SCG_RESET_TGT) {
1221 func = SG_SCSI_RESET_DEVICE;
1222 if (ioctl(f, SG_SCSI_RESET, &func) >= 0)
1226 #ifdef SG_SCSI_RESET_BUS
1227 if (what == SCG_RESET_BUS) {
1228 func = SG_SCSI_RESET_BUS;
1229 if (ioctl(f, SG_SCSI_RESET, &func) >= 0)
1240 sg_settimeout(int f, int tmo)
1245 HZ = sysconf(_SC_CLK_TCK);
1251 if (ioctl(f, SG_SET_TIMEOUT, &tmo) < 0)
1252 comerr("Cannot set SG_SET_TIMEOUT.\n");
1256 * Get misaligned int.
1257 * Needed for all recent processors (sparc/ppc/alpha)
1258 * because the /dev/sg design forces us to do misaligned
1259 * reads of integers.
1266 register char *cp = (char *)ip;
1267 register char *tp = (char *)&ret;
1270 for (i = sizeof (int); --i >= 0; )
1275 #define GETINT(a) sg_getint(&(a))
1277 #define GETINT(a) (a)
1282 usalo_send(SCSI *usalp)
1284 struct usal_cmd *sp = usalp->scmd;
1289 if (usalp->fd < 0) {
1290 sp->error = SCG_FATAL;
1294 if (usallocal(usalp)->isold > 0) {
1295 return (sg_rwsend(usalp));
1297 fillbytes((caddr_t)&sg_io, sizeof (sg_io), '\0');
1299 sg_io.interface_id = 'S';
1301 if (sp->flags & SCG_RECV_DATA) {
1302 sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
1303 } else if (sp->size > 0) {
1304 sg_io.dxfer_direction = SG_DXFER_TO_DEV;
1306 sg_io.dxfer_direction = SG_DXFER_NONE;
1308 sg_io.cmd_len = sp->cdb_len;
1309 if (sp->sense_len > SG_MAX_SENSE)
1310 sg_io.mx_sb_len = SG_MAX_SENSE;
1312 sg_io.mx_sb_len = sp->sense_len;
1313 sg_io.dxfer_len = sp->size;
1314 sg_io.dxferp = sp->addr;
1315 sg_io.cmdp = sp->cdb.cmd_cdb;
1316 sg_io.sbp = sp->u_sense.cmd_sense;
1317 sg_io.timeout = sp->timeout*1000;
1318 sg_io.flags |= SG_FLAG_DIRECT_IO;
1320 ret = ioctl(usalp->fd, SG_IO, &sg_io);
1321 if (usalp->debug > 0) {
1322 fprintf((FILE *)usalp->errfile,
1323 "ioctl ret: %d\n", ret);
1327 sp->ux_errno = geterrno();
1329 * Check if SCSI command cound not be send at all.
1330 * Linux usually returns EINVAL for an unknoen ioctl.
1331 * In case somebody from the Linux kernel team learns that the
1332 * corect errno would be ENOTTY, we check for this errno too.
1334 if ((sp->ux_errno == ENOTTY || sp->ux_errno == EINVAL) &&
1335 usallocal(usalp)->isold < 0) {
1336 usallocal(usalp)->isold = 1;
1337 return (sg_rwsend(usalp));
1339 if (sp->ux_errno == ENXIO ||
1340 sp->ux_errno == EINVAL || sp->ux_errno == EACCES) {
1345 sp->u_scb.cmd_scb[0] = sg_io.status;
1346 sp->sense_count = sg_io.sb_len_wr;
1348 if (usalp->debug > 0) {
1349 fprintf((FILE *)usalp->errfile,
1350 "host_status: %02X driver_status: %02X\n",
1351 sg_io.host_status, sg_io.driver_status);
1354 switch (sg_io.host_status) {
1358 * If there is no DMA overrun and there is a
1359 * SCSI Status byte != 0 then the SCSI cdb transport
1360 * was OK and sp->error must be SCG_NO_ERROR.
1362 if ((sg_io.driver_status & DRIVER_SENSE) != 0) {
1363 if (sp->ux_errno == 0)
1366 if (sp->u_sense.cmd_sense[0] != 0 &&
1367 sp->u_scb.cmd_scb[0] == 0) {
1369 * The Linux SCSI system up to 2.4.xx
1370 * trashes the status byte in the
1371 * kernel. This is true at least for
1372 * ide-scsi emulation. Until this gets
1373 * fixed, we need this hack.
1375 sp->u_scb.cmd_scb[0] = ST_CHK_COND;
1376 if (sp->sense_count == 0)
1377 sp->sense_count = SG_MAX_SENSE;
1379 if ((sp->u_sense.cmd_sense[2] == 0) &&
1380 (sp->u_sense.cmd_sense[12] == 0) &&
1381 (sp->u_sense.cmd_sense[13] == 0)) {
1383 * The Linux SCSI system will
1384 * send a request sense for
1385 * even a dma underrun error.
1386 * Clear CHECK CONDITION state
1387 * in case of No Sense.
1389 sp->u_scb.cmd_scb[0] = 0;
1390 sp->u_sense.cmd_sense[0] = 0;
1391 sp->sense_count = 0;
1397 case DID_NO_CONNECT: /* Arbitration won, retry NO_CONNECT? */
1398 sp->error = SCG_RETRYABLE;
1400 case DID_BAD_TARGET:
1401 sp->error = SCG_FATAL;
1405 __usal_times(usalp);
1407 if (sp->timeout > 1 && usalp->cmdstop->tv_sec == 0) {
1408 sp->u_scb.cmd_scb[0] = 0;
1409 sp->error = SCG_FATAL; /* a selection timeout */
1411 sp->error = SCG_TIMEOUT;
1416 to.tv_sec = sp->timeout;
1417 to.tv_usec = 500000;
1418 __usal_times(usalp);
1420 if (usalp->cmdstop->tv_sec < to.tv_sec ||
1421 (usalp->cmdstop->tv_sec == to.tv_sec &&
1422 usalp->cmdstop->tv_usec < to.tv_usec)) {
1425 sp->error = SCG_TIMEOUT; /* a timeout */
1427 sp->error = SCG_RETRYABLE;
1431 if (sp->error && sp->ux_errno == 0)
1434 sp->resid = sg_io.resid;
1438 # define sg_rwsend usalo_send
1442 sg_rwsend(SCSI *usalp)
1445 struct usal_cmd *sp = usalp->scmd;
1451 int amt = sp->cdb_len;
1453 struct sg_header hd;
1454 unsigned char buf[MAX_DMA_LINUX+SCG_MAX_CMD];
1456 #ifdef SG_GET_BUFSIZE /* We may use a 'sg' version 2 driver */
1464 sp->error = SCG_FATAL;
1469 if (usal_scsibus(usalp) == usallocal(usalp)->pgbus)
1470 return (pg_send(usalp));
1472 if (sp->timeout != usalp->deftimeout)
1473 sg_settimeout(f, sp->timeout);
1476 sgp2 = sgp = &sg_rq;
1477 if (sp->addr == usallocal(usalp)->SCSIbuf) {
1478 sgp = (struct sg_rq *)
1479 (usallocal(usalp)->SCSIbuf - (sizeof (struct sg_header) + amt));
1480 sgp2 = (struct sg_rq *)
1481 (usallocal(usalp)->SCSIbuf - (sizeof (struct sg_header)));
1483 if (usalp->debug > 0) {
1484 fprintf((FILE *)usalp->errfile,
1485 "DMA addr: 0x%8.8lX size: %d - using copy buffer\n",
1486 (long)sp->addr, sp->size);
1488 if (sp->size > (int)(sizeof (sg_rq.buf) - SCG_MAX_CMD)) {
1490 if (usallocal(usalp)->xbuf == NULL) {
1491 usallocal(usalp)->xbufsize = usalp->maxbuf;
1492 usallocal(usalp)->xbuf =
1493 malloc(usallocal(usalp)->xbufsize +
1495 sizeof (struct sg_header));
1496 if (usalp->debug > 0) {
1497 fprintf((FILE *)usalp->errfile,
1498 "Allocated DMA copy buffer, addr: 0x%8.8lX size: %ld\n",
1499 (long)usallocal(usalp)->xbuf,
1503 if (usallocal(usalp)->xbuf == NULL ||
1504 sp->size > usallocal(usalp)->xbufsize) {
1508 sgp2 = sgp = (struct sg_rq *)usallocal(usalp)->xbuf;
1513 * This is done to avoid misaligned access of sgp->some_int
1515 pack_len = sizeof (struct sg_header) + amt;
1516 reply_len = sizeof (struct sg_header);
1517 if (sp->flags & SCG_RECV_DATA) {
1518 reply_len += sp->size;
1520 pack_len += sp->size;
1525 * sgp->some_int may be misaligned if (sp->addr == SCSIbuf)
1526 * This is no problem on Intel porocessors, however
1527 * all other processors don't like it.
1528 * sizeof (struct sg_header) + amt is usually not a multiple of
1529 * sizeof (int). For this reason, we fill in the values into sg_rq
1530 * which is always corectly aligned and then copy it to the real
1531 * location if this location differs from sg_rq.
1532 * Never read/write directly to sgp->some_int !!!!!
1534 fillbytes((caddr_t)&sg_rq, sizeof (struct sg_header), '\0');
1536 sg_rq.hd.pack_len = pack_len;
1537 sg_rq.hd.reply_len = reply_len;
1538 sg_rq.hd.pack_id = usallocal(usalp)->pack_id++;
1539 /* sg_rq.hd.result = 0; not needed because of fillbytes() */
1541 if ((caddr_t)&sg_rq != (caddr_t)sgp)
1542 movebytes((caddr_t)&sg_rq, (caddr_t)sgp, sizeof (struct sg_header));
1544 fillbytes((caddr_t)sgp, sizeof (struct sg_header), '\0');
1546 sgp->hd.pack_len = pack_len;
1547 sgp->hd.reply_len = reply_len;
1548 sgp->hd.pack_id = usallocal(usalp)->pack_id++;
1549 /* sgp->hd.result = 0; not needed because of fillbytes() */
1552 sgp->hd.twelve_byte = 1;
1555 for (i = 0; i < amt; i++) {
1556 sgp->buf[i] = sp->cdb.cmd_cdb[i];
1558 if (!(sp->flags & SCG_RECV_DATA)) {
1559 if ((void *)sp->addr != (void *)&sgp->buf[amt])
1560 movebytes(sp->addr, &sgp->buf[amt], sp->size);
1563 #ifdef SG_GET_BUFSIZE
1564 sgp->hd.want_new = 1; /* Order new behaviour */
1565 sgp->hd.cdb_len = sp->cdb_len; /* Set CDB length */
1566 if (sp->sense_len > SG_MAX_SENSE)
1567 sgp->hd.sense_len = SG_MAX_SENSE;
1569 sgp->hd.sense_len = sp->sense_len;
1571 i = sizeof (struct sg_header) + amt;
1572 if ((amt = write(f, sgp, i)) < 0) { /* write */
1573 sg_settimeout(f, usalp->deftimeout);
1575 } else if (amt != i) {
1576 errmsg("usalo_send(%s) wrote %d bytes (expected %d).\n",
1577 usalp->cmdname, amt, i);
1580 if (sp->addr == usallocal(usalp)->SCSIbuf) {
1581 movebytes(sgp, sgp2, sizeof (struct sg_header));
1584 sgp->hd.sense_buffer[0] = 0;
1585 if ((amt = read(f, sgp, reply_len)) < 0) { /* read */
1586 sg_settimeout(f, usalp->deftimeout);
1590 if (sp->flags & SCG_RECV_DATA && ((void *)sgp->buf != (void *)sp->addr)) {
1591 movebytes(sgp->buf, sp->addr, sp->size);
1593 sp->ux_errno = GETINT(sgp->hd.result); /* Unaligned read */
1594 sp->error = SCG_NO_ERROR;
1596 #ifdef SG_GET_BUFSIZE
1597 if (sgp->hd.grant_new) {
1598 sp->sense_count = sgp->hd.sense_len;
1599 pack_len = GETINT(sgp->hd.sg_cmd_status); /* Unaligned read */
1600 driver_byte = (pack_len >> 24) & 0xFF;
1601 host_byte = (pack_len >> 16) & 0xFF;
1602 msg_byte = (pack_len >> 8) & 0xFF;
1603 status_byte = pack_len & 0xFF;
1605 switch (host_byte) {
1608 if ((driver_byte & DRIVER_SENSE ||
1609 sgp->hd.sense_buffer[0] != 0) &&
1612 * The Linux SCSI system up to 2.4.xx
1613 * trashes the status byte in the
1614 * kernel. This is true at least for
1615 * ide-scsi emulation. Until this gets
1616 * fixed, we need this hack.
1618 status_byte = ST_CHK_COND;
1619 if (sgp->hd.sense_len == 0)
1620 sgp->hd.sense_len = SG_MAX_SENSE;
1622 if ((sp->u_sense.cmd_sense[2] == 0) &&
1623 (sp->u_sense.cmd_sense[12] == 0) &&
1624 (sp->u_sense.cmd_sense[13] == 0)) {
1626 * The Linux SCSI system will
1627 * send a request sense for
1628 * even a dma underrun error.
1629 * Clear CHECK CONDITION state
1630 * in case of No Sense.
1632 sp->u_scb.cmd_scb[0] = 0;
1633 sp->u_sense.cmd_sense[0] = 0;
1634 sp->sense_count = 0;
1639 case DID_NO_CONNECT: /* Arbitration won, retry NO_CONNECT? */
1640 sp->error = SCG_RETRYABLE;
1643 case DID_BAD_TARGET:
1644 sp->error = SCG_FATAL;
1648 sp->error = SCG_TIMEOUT;
1652 sp->error = SCG_RETRYABLE;
1654 if ((driver_byte & DRIVER_SENSE ||
1655 sgp->hd.sense_buffer[0] != 0) &&
1657 status_byte = ST_CHK_COND;
1658 sp->error = SCG_NO_ERROR;
1660 if (status_byte != 0 && sgp->hd.sense_len == 0) {
1661 sgp->hd.sense_len = SG_MAX_SENSE;
1662 sp->error = SCG_NO_ERROR;
1667 if ((host_byte != DID_OK || status_byte != 0) && sp->ux_errno == 0)
1669 sp->u_scb.cmd_scb[0] = status_byte;
1670 if (status_byte & ST_CHK_COND) {
1671 sp->sense_count = sgp->hd.sense_len;
1672 movebytes(sgp->hd.sense_buffer, sp->u_sense.cmd_sense, sp->sense_count);
1677 if (GETINT(sgp->hd.result) == EBUSY) { /* Unaligned read */
1680 to.tv_sec = sp->timeout;
1681 to.tv_usec = 500000;
1682 __usal_times(usalp);
1684 if (sp->timeout > 1 && usalp->cmdstop->tv_sec == 0) {
1685 sp->u_scb.cmd_scb[0] = 0;
1687 sp->error = SCG_FATAL; /* a selection timeout */
1688 } else if (usalp->cmdstop->tv_sec < to.tv_sec ||
1689 (usalp->cmdstop->tv_sec == to.tv_sec &&
1690 usalp->cmdstop->tv_usec < to.tv_usec)) {
1693 sp->error = SCG_TIMEOUT; /* a timeout */
1695 sp->error = SCG_RETRYABLE; /* may be BUS_BUSY */
1699 if (sp->flags & SCG_RECV_DATA)
1700 sp->resid = (sp->size + sizeof (struct sg_header)) - amt;
1702 sp->resid = 0; /* sg version1 cannot return DMA resid count */
1704 if (sgp->hd.sense_buffer[0] != 0) {
1706 sp->sense_count = SG_MAX_SENSE;
1707 movebytes(sgp->hd.sense_buffer, sp->u_sense.cmd_sense, sp->sense_count);
1708 if (sp->ux_errno == 0)
1713 if (usalp->verbose > 0 && usalp->debug > 0) {
1714 #ifdef SG_GET_BUFSIZE
1715 fprintf((FILE *)usalp->errfile,
1716 "status: 0x%08X pack_len: %d, reply_len: %d pack_id: %d result: %d wn: %d gn: %d cdb_len: %d sense_len: %d sense[0]: %02X\n",
1717 GETINT(sgp->hd.sg_cmd_status),
1718 GETINT(sgp->hd.pack_len),
1719 GETINT(sgp->hd.reply_len),
1720 GETINT(sgp->hd.pack_id),
1721 GETINT(sgp->hd.result),
1726 sgp->hd.sense_buffer[0]);
1728 fprintf((FILE *)usalp->errfile,
1729 "pack_len: %d, reply_len: %d pack_id: %d result: %d sense[0]: %02X\n",
1730 GETINT(sgp->hd.pack_len),
1731 GETINT(sgp->hd.reply_len),
1732 GETINT(sgp->hd.pack_id),
1733 GETINT(sgp->hd.result),
1734 sgp->hd.sense_buffer[0]);
1737 fprintf((FILE *)usalp->errfile, "sense: ");
1738 for (i = 0; i < 16; i++)
1739 fprintf((FILE *)usalp->errfile, "%02X ", sgp->hd.sense_buffer[i]);
1740 fprintf((FILE *)usalp->errfile, "\n");
1744 if (sp->timeout != usalp->deftimeout)
1745 sg_settimeout(f, usalp->deftimeout);
1749 #define HAVE_NAT_NAMES
1750 static char * usalo_natname(SCSI *usalp, int busno, int tgt, int tlun) {
1751 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN)
1753 return usallocal(usalp)->filenames[busno][tgt][tlun];