File: scsi-unixware.c

package info (click to toggle)
cdrkit 9:1.1.11-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 7,464 kB
  • sloc: ansic: 107,182; perl: 968; sh: 481; makefile: 229; sed: 4
file content (922 lines) | stat: -rw-r--r-- 21,571 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
 * This file has been modified for the cdrkit suite.
 *
 * The behaviour and appearence of the program code below can differ to a major
 * extent from the version distributed by the original author(s).
 *
 * For details, see Changelog file distributed with the cdrkit package. If you
 * received this file from another source then ask the distributing person for
 * a log of modifications.
 *
 */

/* @(#)scsi-unixware.c	1.36 04/01/15 Copyright 1998 J. Schilling, Santa Cruz Operation */
/*
 *	Interface for the SCO UnixWare SCSI implementation.
 *
 *	Warning: you may change this source, but if you do that
 *	you need to change the _usal_version and _usal_auth* string below.
 *	You may not return "schily" for an SCG_AUTHOR request anymore.
 *	Choose your name instead of "schily" and make clear that the version
 *	string is related to a modified source.
 *
 *	Copyright (c) 1998 J. Schilling, Santa Cruz Operation
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#undef	sense
#undef	SC_PARITY
#undef	scb

#include <sys/sysmacros.h>	/* XXX Falsch, richtig -> sys/mkdev.h */
#include <sys/scsi.h>
#include <sys/sdi_edt.h>
#include <sys/sdi.h>

/*
 *	Warning: you may change this source, but if you do that
 *	you need to change the _usal_version and _usal_auth* string below.
 *	You may not return "schily" for an SCG_AUTHOR request anymore.
 *	Choose your name instead of "schily" and make clear that the version
 *	string is related to a modified source.
 */
static	char	_usal_trans_version[] = "scsi-unixware.c-1.36";	/* The version for this transport*/

/* Max. number of usal scsibusses.  The real limit would be		*/
/* MAX_HBA * MAX_BUS (which would be 32 * 8 on UnixWare 2.1/7.x),	*/
/* but given that we will hardly see such a beast, lets take 32		*/

#define	MAX_SCG		32

	/* maximum defines for UnixWare 2.x/7.x from <sys/sdi_edt.h> */

#define	MAX_TGT		MAX_EXTCS	/* Max # of target id's		*/
#define	MAX_LUN		MAX_EXLUS	/* Max # of lun's		*/

#define	MAX_DMA		(32*1024)
#ifdef	__WHAT_TODO__
#define	MAX_DMA		(16*1024)	/* On UnixWare 2.1.x w/ AHA2940 HBA */
					/* the max DMA size is 16KB.	    */
#endif

#define	MAXLINE		80
#define	MAXPATH		256

#define	DEV_DIR		"/tmp"
#define	DEV_NAME	"usal.s%1dt%1dl%1d"

#define	SCAN_HBA	"%d:%d,%d,%d:%7s : %n"
#define	SCAN_DEV	"%d,%d,%d:%7s : %n"

#define	PRIM_HBA	"/dev/hba/hba1"
#define	SCSI_CFG	"LC_ALL=C /etc/scsi/pdiconfig -l"

#define	SCAN_ALL	"LIBSCG_SCAN_ALL"

#define	SDI_VALID	0x01	/* Entry may be used (non disk)	   */
#define	SDI_ATAPI	0x02	/* Connected via IDE HBA	   */
#define	SDI_INITIATOR	0x04	/* This is the initiator target ID */

typedef struct usal2sdi {
	short	open;
	short	flags;
	short	fd;
	char	hba;
	char	bus;
	char	tgt;
	char	lun;

	dev_t	node;
	dev_t	major;
	dev_t	minor;
/*#define	SCG_DEBUG*/
#ifdef	SCG_DEBUG
	char	type[20];
	char	vend[40];
	char	devn[32];
#endif
} usal2sdi_t;

static	usal2sdi_t	sdidevs [MAX_SCG][MAX_TGT][MAX_LUN];
static	BOOL		sdiinit = FALSE;

struct usal_local {
	short	usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
};
#define	usallocal(p)	((struct usal_local *)((p)->local))

static	int	unixware_init(SCSI *usalp);
static	int	do_usal_cmd(SCSI *usalp, struct usal_cmd *sp);
static	int	do_usal_sense(SCSI *usalp, struct usal_cmd *sp);
static	FILE	*xpopen(char *cmd, char *type);
static	int	xpclose(FILE *f);

/*
 * -------------------------------------------------------------------------
 * SCO UnixWare 2.1.x / UnixWare 7 provides a scsi pass-through mechanism,
 * which can be used to access any configured scsi device.
 *
 * NOTE: The libusal UnixWare passthrough routines have changed with
 *       cdrecord-1.8 to enable the -scanbus, -load, -eject option
 *	 regardless of the status of media and the addressing
 *       scheme is now the same as used on many other platforms like
 *       Solaris, Linux etc.
 *
 *      ===============================================================
 *	RUN 'cdrecord -scanbus' TO SEE THE DEVICE ADDRESSES YOU CAN USE
 *	===============================================================
 */

/*
 * Return version information for the low level SCSI transport code.
 * This has been introduced to make it easier to trace down problems
 * in applications.
 *
 */
static char *
usalo_version(SCSI *usalp, int what)
{
	if (usalp != (SCSI *)0) {
		switch (what) {

		case SCG_VERSION:
			return (_usal_trans_version);
		/*
		 * If you changed this source, you are not allowed to
		 * return "schily" for the SCG_AUTHOR request.
		 */
		case SCG_AUTHOR:
			return (_usal_auth_cdrkit);
		case SCG_SCCS_ID:
			return (__sccsid);
		}
	}
	return ((char *)0);
}


static int
usalo_help(SCSI *usalp, FILE *f)
{
	__usal_help(f, "SDI_SEND", "Generic SCSI",
		"", "bus,target,lun", "1,2,0", TRUE, FALSE);
	return (0);
}

/*
 * ---------------------------------------------------------------
 * This routine is introduced to create all device nodes necessary
 * to access any detected scsi device. It parses the output of
 * /etc/scsi/pdiconfig -l and creates passthru device node for each
 * found scsi device apart from the listed hba's.
 *
 */

static int
unixware_init(SCSI *usalp)
{
	FILE		*cmd;
	int		hba = 0, bus = 0, usal = 0, tgt = 0, lun = 0;
	int		nusal = -1, lhba = -1, lbus = 0;
	int		atapi, fd, nopen = 0, pos = 0, len = 0;
	int		s, t, l;
	int		scan_disks;
	char		lines[MAXLINE];
	char		class[MAXLINE];
	char		ident[MAXLINE];
	char		devnm[MAXPATH];
	char		dname[MAXPATH];
	struct stat 	stbuf;
	dev_t		ptdev, major, minor, node;
	char		**evsave;
extern	char		**environ;

	/* Check for validity of primary hostbus adapter node */

	if (stat(PRIM_HBA, &stbuf) < 0) {
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
				"Can not stat() primary hba (%s)",
				PRIM_HBA);
		return (-1);
	}

	if (!S_ISCHR(stbuf.st_mode)) {
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
				"Primary hba (%s) not a character device",
				PRIM_HBA);
		return (-1);
	}

	major = getmajor(stbuf.st_rdev);

	/*
	 * Check whether we want to scan all devices
	 */
	if (getenv(SCAN_ALL) != NULL) {
		scan_disks = 1;
	} else {
		scan_disks = 0;
	}

	/* read pdiconfig output and get all attached scsi devices ! */

	evsave = environ;
	environ = 0;
	if ((cmd = xpopen(SCSI_CFG, "r")) == NULL) {
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
				"Error popen() for \"%s\"",
				SCSI_CFG);
		environ = evsave;
		return (-1);
	}
	environ = evsave;


	for (;;) {
		if (fgets(lines, MAXLINE, cmd) == NULL)
			break;

		memset(class, '\0', sizeof (class));
		memset(ident, '\0', sizeof (ident));

		if (lines[0] == ' ') {
			sscanf(lines, SCAN_DEV, &bus, &tgt, &lun, class, &pos);
			hba = lhba;
		} else {
			sscanf(lines, SCAN_HBA, &hba, &bus, &tgt, &lun, class, &pos);
			nusal++;
			lhba = hba;
			atapi = 0;
		}

		/* We can't sscanf() the ident string of the device	*/
		/* as it may contain characters sscanf() will		*/
		/* recognize as a delimiter. So do a strcpy() instead !	*/

		len = strlen(lines) - pos - 1; /* don't copy the '\n' */

		strncpy(ident, &lines[pos], len);

		if (usalp->debug > 0) {
			fprintf((FILE *)usalp->errfile,
				"SDI -> %d:%d,%d,%d: %-7s : %s\n",
				hba, bus, tgt, lun, class, ident);
		}
		if (bus != lbus) {
			nusal++;
			lbus = bus;
		}

		usal = nusal;

		/* check whether we have a HBA or a SCSI device, don't 	*/
		/* let HBA's be valid device for cdrecord, but mark	*/
		/* them as a controller (initiator = 1).		*/

		/* Don't detect disks, opening a mounted disk can hang	*/
		/* the disk subsystem !!! So unless we set an		*/
		/* environment variable LIBSCG_SCAN_ALL, we will ignore	*/
		/* disks						*/

		if (strstr(class, "HBA") == NULL) {
			if (strstr(class, "DISK") != NULL) {
				if (scan_disks)
					sdidevs[usal][tgt][lun].flags |= SDI_VALID;
				else
					sdidevs[usal][tgt][lun].flags &= ~SDI_VALID;
			} else {
				sdidevs[usal][tgt][lun].flags |= SDI_VALID;
			}
		} else {
			sdidevs[usal][tgt][lun].flags |= SDI_INITIATOR;
		}


		/* There is no real flag that shows a HBA as an ATAPI	*/
		/* controller, so as we know the driver is called 'ide'	*/
		/* we can check the ident string for the occurence of it*/

		if (strstr(ident, "(ide,") != NULL) {
			atapi = 1;
		}

		/*
		 * Fill the sdidevs array with all we know now.
		 * Do not overwrite fields that may contain old state like
		 * sdidevs[usal][tgt][lun].open
		 */

		if (atapi)
			sdidevs[usal][tgt][lun].flags |= SDI_ATAPI;
		else
			sdidevs[usal][tgt][lun].flags &= ~SDI_ATAPI;

		sdidevs[usal][tgt][lun].hba = hba;
		sdidevs[usal][tgt][lun].bus = bus;
		sdidevs[usal][tgt][lun].tgt = tgt;
		sdidevs[usal][tgt][lun].lun = lun;

#ifdef	SCG_DEBUG
		strcpy(sdidevs[usal][tgt][lun].type, class);
		strcpy(sdidevs[usal][tgt][lun].vend, ident);

		snprintf(sdidevs[usal][tgt][lun].devn,
				sizeof (sdidevs[usal][tgt][lun].devn),
				DEV_NAME, usal, tgt, lun);
#endif
		snprintf(devnm, sizeof (devnm),
				DEV_NAME, usal, tgt, lun);

		minor = SDI_MINOR(hba, tgt, lun, bus);
		node  = makedevice(major, minor);

		sdidevs[usal][tgt][lun].major = major;
		sdidevs[usal][tgt][lun].minor = minor;
		sdidevs[usal][tgt][lun].node  = node;

		if (usalp->debug > 0) {

			fprintf((FILE *)usalp->errfile,
			"h = %d; b = %d, s = %d, t = %d, l = %d, a = %d, ma = %d, mi = %2d, dev = '%s', id = '%s'\n",
			hba, bus, usal, tgt, lun,
			(sdidevs[usal][tgt][lun].flags & SDI_ATAPI) != 0,
			sdidevs[usal][tgt][lun].major,
			sdidevs[usal][tgt][lun].minor,
			devnm,
			ident);
		}


	}

	if (xpclose(cmd) == -1) {
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
				"Error pclose() for \"%s\"",
				SCSI_CFG);
		return (-1);
	}


	/* create all temporary device nodes */

	for (s = 0; s < MAX_SCG; s++) {
		for (t = 0; t < MAX_TGT; t++) {
			for (l = 0; l < MAX_LUN; l++) {

				if ((sdidevs[s][t][l].flags & SDI_VALID) == 0) {
					if (sdidevs[s][t][l].fd >= 0) {
						close(sdidevs[s][t][l].fd);
					}
					sdidevs[s][t][l].fd = -1;
					sdidevs[s][t][l].open = 0;
					continue;
				}

				/* Make pass-through interface device node */

				snprintf(devnm,
					sizeof (devnm),
					DEV_NAME, s, t, l);

				snprintf(dname, sizeof (dname),
					"%s/%s", DEV_DIR, devnm);

				ptdev = sdidevs[s][t][l].node;

				if (mknod(dname, S_IFCHR | 0700, ptdev) < 0) {
					if (errno == EEXIST) {
						unlink(dname);

						if (mknod(dname, S_IFCHR | 0700, ptdev) < 0) {
							if (usalp->errstr)
								snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
									"mknod() error for \"%s\"", dname);
							return (-1);
						}
					} else {
						if (usalp->errstr)
							snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
								"mknod() error for \"%s\"", dname);
						return (-1);
					}
				}

				/* Open pass-through device node */

				if ((fd = open(dname, O_RDONLY)) < 0) {
					if (errno == EBUSY && sdidevs[s][t][l].open > 0) {
						/*
						 * Device has already been opened, just
						 * return the saved file desc.
						 */
						fd = sdidevs[s][t][l].fd;
					} else {
						if (usalp->errstr)
							snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
								"can not open pass-through %s", dname);
						return (-1);
					}
				}

				/*
				 * If for whatever reason we may open a pass through
				 * device more than once, this will waste fs's as we
				 * do not check for sdidevs[s][t][l].fd == -1.
				 */
				sdidevs[s][t][l].fd   = fd;
				sdidevs[s][t][l].open++;
				nopen++;
				usallocal(usalp)->usalfiles[s][t][l] = (short) fd;

				if (usalp->debug > 0) {

					fprintf((FILE *)usalp->errfile,
						"s = %d, t = %d, l = %d, dev = %s, fd = %d\n",
						s, t, l,
						devnm,
						sdidevs[s][t][l].fd);
				}

			}
		}
	}

	return (nopen);
}


static int
usalo_open(SCSI *usalp, char *device)
{
	int	busno	= usal_scsibus(usalp);
	int	tgt	= usal_target(usalp);
	int	tlun	= usal_lun(usalp);
	int	b, t, l;

	if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
		errno = EINVAL;
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
				"Illegal value for busno, target or lun '%d,%d,%d'",
				busno, tgt, tlun);
		return (-1);
	}

	if (usalp->local == NULL) {
		usalp->local = malloc(sizeof (struct usal_local));
		if (usalp->local == NULL)
			return (0);

		for (b = 0; b < MAX_SCG; b++) {
			for (t = 0; t < MAX_TGT; t++) {
				for (l = 0; l < MAX_LUN; l++)
					usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
			}
		}
	}

	if (!sdiinit) {
		sdiinit = TRUE;
		memset(sdidevs, 0, sizeof (sdidevs));	/* init tmp_structure */
		for (b = 0; b < MAX_SCG; b++) {
			for (t = 0; t < MAX_TGT; t++) {
				for (l = 0; l < MAX_LUN; l++) {

					sdidevs[b][t][l].flags = 0;
					sdidevs[b][t][l].fd = -1;
					sdidevs[b][t][l].open = 0;
				}
			}
		}
	}

	if (*device != '\0') {		/* we don't allow old dev usage */
		errno = EINVAL;
		if (usalp->errstr)
			snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
			"Open by 'devname' no longer supported on this OS");
		return (-1);
	} else {			/* this is the new stuff	 */
					/* it will do the initialisation */
					/* and return the number of	 */
					/* detected devices to be used	 */
					/* with the new addressing	 */
					/* scheme.			 */

		return (unixware_init(usalp));
	}

}


static int
usalo_close(SCSI *usalp)
{
	register int	f;
	register int	b;
	register int	t;
	register int	l;

	if (usalp->local == NULL)
		return (-1);

	for (b = 0; b < MAX_SCG; b++) {
		for (t = 0; t < MAX_TGT; t++) {
			for (l = 0; l < MAX_LUN; l++) {

				f = usallocal(usalp)->usalfiles[b][t][l];
				if (f >= 0) {
					if (sdidevs[b][t][l].open > 0)
						sdidevs[b][t][l].open--;
					if (sdidevs[b][t][l].open <= 0) {
						if (sdidevs[b][t][l].fd >= 0)
							close(sdidevs[b][t][l].fd);
						sdidevs[b][t][l].fd    = -1;
						sdidevs[b][t][l].flags &= ~SDI_VALID;
					}
				}
				usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
			}
		}
	}
	return (0);
}

static long
usalo_maxdma(SCSI *usalp, long amt)
{
	return (MAX_DMA);
}


static void *
usalo_getbuf(SCSI *usalp, long amt)
{
	if (usalp->debug > 0) {
		fprintf((FILE *)usalp->errfile,
			"usalo_getbuf: %ld bytes\n", amt);
	}
	usalp->bufbase = (void *) valloc((size_t)(amt));

	return (usalp->bufbase);
}

static void
usalo_freebuf(SCSI *usalp)
{
	if (usalp->bufbase)
		free(usalp->bufbase);
	usalp->bufbase = NULL;
}

static BOOL
usalo_havebus(SCSI *usalp, int busno)
{
	register int	t;
	register int	l;

	if (busno < 0 || busno >= MAX_SCG)
		return (FALSE);

	if (usalp->local == NULL)
		return (FALSE);

	for (t = 0; t < MAX_TGT; t++) {
		for (l = 0; l < MAX_LUN; l++)
			if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
				return (TRUE);
	}
	return (FALSE);
}

static int
usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
{
	if (busno < 0 || busno >= MAX_SCG ||
	    tgt   < 0 || tgt   >= MAX_TGT ||
	    tlun  < 0 || tlun  >= MAX_LUN)
		return (-1);

	if (usalp->local == NULL)
		return (-1);

	return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
}

static int
usalo_initiator_id(SCSI *usalp)
{
	register int	t;
	register int	l;
	register int	busno;

	busno = usal_scsibus(usalp);

	if (busno < 0 || busno >= MAX_SCG)
		return (FALSE);

	for (t = 0; t < MAX_TGT; t++) {
		for (l = 0; l < MAX_LUN; l++)
			if ((sdidevs[busno][t][l].flags & SDI_INITIATOR) != 0) {
				if (usalp->debug > 0) {
					fprintf((FILE *)usalp->errfile,
						"usalo_initiator_id: id = %d\n", t);
				}
				return (t);
			}
	}

	return (-1);
}

static int
usalo_isatapi(SCSI *usalp)
{
	/* if the new address method is used we know if this is ATAPI */

	return ((sdidevs[usal_scsibus(usalp)][usal_target(usalp)][usal_lun(usalp)].flags & SDI_ATAPI) != 0);
}

static int
usalo_reset(SCSI *usalp, int what)
{
	int	f = usalp->fd;

	errno = EINVAL;

#if defined(SDI_TRESET) || defined(SDI_BRESET)
	if (what == SCG_RESET_NOP) {
		errno = 0;
		return (0);
	}

#ifdef	SDI_TRESET
	if (what == SCG_RESET_TGT) {
		errno = 0;
		if (ioctl(f, SDI_TRESET, 0) >= 0)
			return (0);
	}
#endif

#ifdef	SDI_BRESET
	if (what == SCG_RESET_BUS) {
		errno = 0;
		if (ioctl(f, SDI_BRESET, 0) >= 0)
			return (0);
	}
#endif

#endif	/* defined(SDI_TRESET) || defined(SDI_BRESET) */

	return (-1);
}

static int
do_usal_cmd(SCSI *usalp, struct usal_cmd *sp)
{
	int			ret;
	int			i;
	struct sb		scsi_cmd;
	struct scb		*scbp;

	memset(&scsi_cmd,  0, sizeof (scsi_cmd));

	scsi_cmd.sb_type = ISCB_TYPE;
	scbp = &scsi_cmd.SCB;

	scbp->sc_cmdpt = (caddr_t) sp->cdb.cmd_cdb;
	scbp->sc_cmdsz = sp->cdb_len;

	scbp->sc_datapt = sp->addr;
	scbp->sc_datasz = sp->size;

	if (!(sp->flags & SCG_RECV_DATA) && (sp->size > 0))
		scbp->sc_mode = SCB_WRITE;
	else
		scbp->sc_mode = SCB_READ;

	scbp->sc_time = sp->timeout;

	sp->error = SCG_NO_ERROR;
	errno = 0;
	for (;;) {
		if ((ret = ioctl(usalp->fd, SDI_SEND, &scsi_cmd)) < 0) {
			if (errno == EAGAIN) {
				sleep(1);
				errno = 0;
				continue;
			}
			sp->ux_errno = errno;
			if (errno == 0)
				sp->ux_errno = EIO;
			sp->error = SCG_RETRYABLE;

#ifdef	__needed__
			if (errno == ENOTTY || errno == EINVAL ||
			    errno == EACCES) {
				return (-1);
			}
#endif
			return (ret);
		}
		break;
	}
	sp->ux_errno = errno;
	sp->resid = scbp->sc_resid;
	memset(&sp->u_scb.Scb, 0, sizeof (sp->u_scb.Scb));
	sp->u_scb.cmd_scb[0] = scbp->sc_status;

	if (sp->u_scb.cmd_scb[0] & 0x02) {
		if (sp->ux_errno == 0)
			sp->ux_errno = EIO;
	}

	switch (scbp->sc_comp_code) {

		case SDI_ASW	 : /* Job completed normally		*/
		case SDI_LINKF0	 : /* Linked command done without flag	*/
		case SDI_LINKF1	 : /* Linked command done with flag	*/

				sp->error = SCG_NO_ERROR;
				break;

		case SDI_CKSTAT	 : /* Check the status byte		*/

				sp->error = SCG_NO_ERROR;
				break;

		case SDI_NOALLOC : /* This block is not allocated	*/
		case SDI_NOTEQ	 : /* Addressed device not present	*/
		case SDI_OOS	 : /* Device is out of service		*/
		case SDI_NOSELE	 : /* The SCSI bus select failed	*/
		case SDI_SBRESC	 : /* SCSI bus reservation conflict	*/

				sp->error = SCG_FATAL;
				if (sp->ux_errno == 0)
					sp->ux_errno = EIO;
				break;

		case SDI_QFLUSH	 : /* Job was flushed			*/
		case SDI_ABORT	 : /* Command was aborted		*/
		case SDI_RESET	 : /* Reset was detected on the bus	*/
		case SDI_CRESET	 : /* Reset was caused by this unit	*/
		case SDI_V2PERR	 : /* vtop failed			*/
		case SDI_HAERR	 : /* Host adapter error		*/
		case SDI_MEMERR	 : /* Memory fault			*/
		case SDI_SBUSER	 : /* SCSI bus error			*/
		case SDI_SCBERR	 : /* SCB error				*/
		case SDI_MISMAT	 : /* parameter mismatch		*/

		case SDI_PROGRES : /* Job in progress			*/
		case SDI_UNUSED	 : /* Job not in use			*/

		case SDI_ONEIC	 : /* More than one immediate request	*/
		case SDI_SFBERR	 : /* SFB error				*/
		case SDI_TCERR	 : /* Target protocol error detected	*/
		default:
				sp->error = SCG_RETRYABLE;
				if (sp->ux_errno == 0)
					sp->ux_errno = EIO;
				break;

		case SDI_TIME	 : /* Job timed out			*/
		case SDI_TIME_NOABORT : /* Job timed out, but could not be aborted */

				sp->error = SCG_TIMEOUT;
				if (sp->ux_errno == 0)
					sp->ux_errno = EIO;
				break;
	}
	return (0);
}


static int
do_usal_sense(SCSI *usalp, struct usal_cmd *sp)
{
	int		ret;
	struct usal_cmd	s_cmd;

	memset((caddr_t)&s_cmd, 0, sizeof (s_cmd));

	s_cmd.addr	= (caddr_t) sp->u_sense.cmd_sense;
	s_cmd.size	= sp->sense_len;
	s_cmd.flags	= SCG_RECV_DATA|SCG_DISRE_ENA;
	s_cmd.cdb_len	= SC_G0_CDBLEN;
	s_cmd.sense_len	= CCS_SENSE_LEN;

	s_cmd.cdb.g0_cdb.cmd   = SC_REQUEST_SENSE;
	s_cmd.cdb.g0_cdb.lun   = sp->cdb.g0_cdb.lun;
	s_cmd.cdb.g0_cdb.count = sp->sense_len;

	ret = do_usal_cmd(usalp, &s_cmd);

	if (ret < 0)
		return (ret);

	sp->sense_count = sp->sense_len - s_cmd.resid;
	return (ret);
}

static int
usalo_send(SCSI *usalp)
{
	struct usal_cmd	*sp = usalp->scmd;
	int	ret;

	if (usalp->fd < 0) {
		sp->error = SCG_FATAL;
		return (0);
	}

	ret = do_usal_cmd(usalp, sp);
	if (ret < 0)
		return (ret);

	if (sp->u_scb.cmd_scb[0] & S_CKCON)
		ret = do_usal_sense(usalp, sp);

	return (ret);
}

#define	sense	u_sense.Sense
#undef	SC_PARITY
#define	SC_PARITY	0x09
#define	scb		u_scb.Scb

/*--------------------------------------------------------------------------*/
#include <unixstd.h>
#include <waitdefs.h>
/*
 * Simplified version of popen()
 * This version of popen() is not usable more than once at a time.
 * Needed because /etc/scsi/pdiconfig will not work if euid != uid
 */
static pid_t	po_pid;

static FILE *
xpopen(char *cmd, char *type)
{
	FILE	*ret;
	FILE	*pp[2];

	if (po_pid != 0)
		return ((FILE *)NULL);

	if (*type != 'r')
		return ((FILE *)NULL);

	if (fpipe(pp) == 0)
		return ((FILE *)NULL);


	if ((po_pid = fork()) == 0) {
		setuid(0);

		fclose(pp[0]);
		(void) rols_fexecl("/bin/sh", stdin, pp[1], stderr,
					"sh", "-c", cmd, (char *)0);
		_exit(1);
	}
	fclose(pp[1]);

	if (po_pid == (pid_t)-1) {
		fclose(pp[0]);
		return ((FILE *)NULL);
	}
	return (pp[0]);
}

static int
xpclose(FILE *f)
{
	int	ret = 0;

	if (po_pid == 0)
		return (-1);

	fclose(f);

	if (waitpid(po_pid, &ret, 0) < 0)
		ret = -1;

	po_pid = 0;
	return (ret);
}