summaryrefslogtreecommitdiff
path: root/sys/dev/ic/ncr5380.c
blob: bbc68d483f45f476d23900495adda6baf6cd011b (plain)
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
/*	$NetBSD: ncr5380.c,v 1.3 1995/09/26 21:04:27 pk Exp $	*/

/*
 * Copyright (C) 1994 Adam Glass, Gordon W. Ross
 * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
 *			Michael L. Finch, Bradley A. Grantham, and
 *			Lawrence A. Kesteloot
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Alice Group.
 * 4. The names of the Alice Group or any of its members may not be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * NOTE: This is 99% Sun 3 `si' driver.  I've made the probe and attach
 * routines a little minimalistic, and adjusted them slightly for the Sun 4.
 * Also, fixed some logic in ncr5380_select_target().
 *	Jason R. Thorpe <thorpej@nas.nasa.gov>
 */

static int
ncr5380_scsi_cmd(xs)
	struct scsi_xfer *xs;
{
	int flags, s, r;

	flags = xs->flags;
	if (xs->bp) flags |= (SCSI_NOSLEEP);
	if ( flags & ITSDONE ) {
		printf("Already done?");
		xs->flags &= ~ITSDONE;
	}
	if ( ! ( flags & INUSE ) ) {
		printf("Not in use?");
		xs->flags |= INUSE;
	}

	s = splbio();

	if ( flags & SCSI_RESET ) {
		printf("flags & SCSIRESET.\n");
		ncr5380_reset_scsibus(xs->sc_link->adapter_softc);
		r = COMPLETE;
	} else {
		r = ncr5380_send_cmd(xs);
		xs->flags |= ITSDONE;
		scsi_done(xs);
	}

	splx(s);

	switch(r) {
	case COMPLETE:
	case SUCCESSFULLY_QUEUED:
		r = SUCCESSFULLY_QUEUED;
		if (xs->flags & SCSI_POLL)
			r = COMPLETE;
		break;
	default:
		break;
	}
	return r;
}

#ifdef	DEBUG
static int
ncr5380_show_scsi_cmd(xs)
	struct scsi_xfer *xs;
{
	u_char	*b = (u_char *) xs->cmd;
	int	i  = 0;

	if ( ! ( xs->flags & SCSI_RESET ) ) {
		printf("si(%d:%d:%d)-",
			   xs->sc_link->scsibus,
			   xs->sc_link->target,
			   xs->sc_link->lun);
		while (i < xs->cmdlen) {
			if (i) printf(",");
			printf("%x",b[i++]);
		}
		printf("-\n");
	} else {
		printf("si(%d:%d:%d)-RESET-\n",
			   xs->sc_link->scsibus,
			   xs->sc_link->target,
			   xs->sc_link->lun);
	}
}
#endif

/*
 * Actual chip control.
 */

static void
ncr5380_sbc_intr(ncr5380)
	struct ncr5380_softc *ncr5380;
{
	volatile sci_regmap_t *regs = ncr5380->sc_regs;

	if ((regs->sci_csr & SCI_CSR_INT) == 0) {
#ifdef	DEBUG
		printf (" ncr5380_sbc_intr: spurious\n");
#endif
		return;
	}

	SCI_CLR_INTR(regs);
#ifdef	DEBUG
	printf (" ncr5380_sbc_intr\n");
#endif
}

static int
ncr5380_reset_scsibus(ncr5380)
	struct ncr5380_softc *ncr5380;
{
	volatile sci_regmap_t *regs = ncr5380->sc_regs;

#ifdef	DEBUG
	if (ncr5380_debug) {
		printf("ncr5380_reset_scsibus\n");
	}
#endif

	regs->sci_icmd = SCI_ICMD_RST;
	delay(100);
	regs->sci_icmd = 0;

	regs->sci_mode = 0;
	regs->sci_tcmd = SCI_PHASE_DISC;
	regs->sci_sel_enb = 0;

	SCI_CLR_INTR(regs);
	/* XXX - Need long delay here! */
}

static int
ncr5380_poll(adapter, timeout)
	int adapter, timeout;
{
}

static int
ncr5380_send_cmd(xs)
	struct scsi_xfer *xs;
{
	int	sense;

#ifdef	DEBUG
	if (ncr5380_debug & 2)
		ncr5380_show_scsi_cmd(xs);
#endif

	sense = ncr5380_generic( xs->sc_link->adapter_softc,
	    xs->sc_link->target, xs->sc_link->lun, xs->cmd,
	    xs->cmdlen, xs->data, xs->datalen );

	switch (sense) {
	case 0:	/* success */
		xs->resid = 0;
		xs->error = XS_NOERROR;
		break;

	case 0x02:	/* Check condition */
#ifdef	DEBUG
		if (ncr5380_debug)
			printf("check cond. target %d.\n",
				   xs->sc_link->target);
#endif
		delay(10);	/* Phil's fix for slow devices. */
		ncr5380_group0(xs->sc_link->adapter_softc,
				  xs->sc_link->target,
				  xs->sc_link->lun,
				  0x3, 0x0,
				  sizeof(struct scsi_sense_data),
				  0, (caddr_t) &(xs->sense),
				  sizeof(struct scsi_sense_data));
		xs->error = XS_SENSE;
		break;
	case 0x08:	/* Busy - common code will delay, retry. */
		xs->error = XS_BUSY;
		break;
	default:	/* Dead - tell common code to give up. */
		xs->error = XS_DRIVER_STUFFUP;
		break;

	}
	return (COMPLETE);
}

static int
ncr5380_select_target(regs, myid, tid, with_atn)
	register volatile sci_regmap_t *regs;
	u_char myid, tid;
	int with_atn;
{
	register u_char	bid, icmd;
	int		ret = SCSI_RET_RETRY;
	int 	arb_retries, arb_wait;
	int i;

	/* for our purposes.. */
	myid = 1 << myid;
	tid = 1 << tid;

	regs->sci_sel_enb = 0; /* we don't want any interrupts. */
	regs->sci_tcmd = 0;	/* get into a harmless state */

	arb_retries = ARBITRATION_RETRIES;

retry_arbitration:
	regs->sci_mode = 0;	/* get into a harmless state */
wait_for_bus_free:
	if (--arb_retries <= 0) {
#ifdef	DEBUG
		if (ncr5380_debug) {
			printf("ncr5380_select: arb_retries expended; resetting...\n");
		}
#endif
		ret = SCSI_RET_NEED_RESET;
		goto nosel;
	}

	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);

	if (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) {
		/* Something is sitting on the SCSI bus... */
#ifdef DEBUG
		/* Only complain once (the last time through). */
		if (ncr5380_debug && (arb_retries <= 1)) {
			printf("si_select_target: still BSY+SEL\n");
		}
#endif
		/* Give it a little time, then try again. */
		delay(10);
		goto wait_for_bus_free;
	}

	regs->sci_odata = myid;
	regs->sci_mode = SCI_MODE_ARB;
/*	regs->sci_mode |= SCI_MODE_ARB;	XXX? */

	/* AIP might not set if BSY went true after we checked */
	/* Wait up to about 100 usec. for it to appear. */
	arb_wait = 50;	/* X2 */
	do {
		if (regs->sci_icmd & SCI_ICMD_AIP)
			goto got_aip;
		delay(2);
	} while (--arb_wait > 0);
	/* XXX - Could have missed it? */
#ifdef	DEBUG
	if (ncr5380_debug)
		printf("ncr5380_select_target: API did not appear\n");
#endif
	goto retry_arbitration;

	got_aip:
#ifdef	DEBUG
	if (ncr5380_debug & 4) {
		printf("ncr5380_select_target: API after %d tries (last wait %d)\n",
			   ARBITRATION_RETRIES - arb_retries,
			   (50 - arb_wait));
	}
#endif

	delay(3);	/* 2.2 uSec. arbitration delay */

	if (regs->sci_icmd & SCI_ICMD_LST) {
#ifdef	DEBUG
		if (ncr5380_debug)
			printf ("lost 1\n");
#endif
		goto retry_arbitration;	/* XXX */
	}

	regs->sci_mode &= ~SCI_MODE_PAR_CHK;
	bid = regs->sci_data;

	if ((bid & ~myid) > myid) {
#ifdef	DEBUG
		if (ncr5380_debug)
			printf ("lost 2\n");
#endif
		/* Trying again will not help. */
		goto lost;
	}
	if (regs->sci_icmd & SCI_ICMD_LST) {
#ifdef	DEBUG
		if (ncr5380_debug)
			printf ("lost 3\n");
#endif
		goto lost;
	}

	/* Won arbitration, enter selection phase now */	
	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
	icmd |= (with_atn ? (SCI_ICMD_SEL|SCI_ICMD_ATN) : SCI_ICMD_SEL);
	regs->sci_icmd = icmd;

	if (regs->sci_icmd & SCI_ICMD_LST) {
#ifdef	DEBUG
		if (ncr5380_debug)
			printf ("nosel\n");
#endif
		goto nosel;
	}

	/* XXX a target that violates specs might still drive the bus XXX */
	/* XXX should put our id out, and after the delay check nothi XXX */
	/* XXX ng else is out there.				      XXX */

	delay(3);

	regs->sci_sel_enb = 0;

	regs->sci_odata = myid | tid;

	icmd |= SCI_ICMD_BSY|SCI_ICMD_DATA;
	regs->sci_icmd = icmd;

/*	regs->sci_mode &= ~SCI_MODE_ARB;	 2 deskew delays, too */
	regs->sci_mode = 0;			/* 2 deskew delays, too */
	
	icmd &= ~SCI_ICMD_BSY;
	regs->sci_icmd = icmd;

	/* bus settle delay, 400ns */
	delay(3);

	regs->sci_mode |= SCI_MODE_PAR_CHK;

	{
		register int timeo  = 2500;/* 250 msecs in 100 usecs chunks */
		while ((regs->sci_bus_csr & SCI_BUS_BSY) == 0) {
			if (--timeo > 0) {
				delay(100);
			} else {
				/* This is the "normal" no-such-device select error. */
#ifdef	DEBUG
				if (ncr5380_debug)
					printf("ncr5380_select: not BSY (nothing there)\n");
#endif
				goto nodev;
			}
		}
	}

	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL);
	regs->sci_icmd = icmd;
/*	regs->sci_sel_enb = myid;*/	/* looks like we should NOT have it */
	/* XXX - SCI_MODE_PAR_CHK ? */
	return SCSI_RET_SUCCESS;

nodev:
	ret = SCSI_RET_DEVICE_DOWN;
	regs->sci_sel_enb = myid;
nosel:
	regs->sci_icmd = 0;
	regs->sci_mode = 0;
	return ret;

lost:
	regs->sci_icmd = 0;
	regs->sci_mode = 0;
#ifdef	DEBUG
	if (ncr5380_debug) {
		printf("ncr5380_select: lost arbitration\n");
	}
#endif
	return ret;
}

sci_data_out(regs, phase, count, data)
	register volatile sci_regmap_t	*regs;
	unsigned char		*data;
{
	register unsigned char	icmd;
	register int		cnt=0;

	/* ..checks.. */

	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
loop:
	/* SCSI bus phase not valid until REQ is true. */
	WAIT_FOR_REQ(regs);
	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
		return cnt;

	icmd |= SCI_ICMD_DATA;
	regs->sci_icmd = icmd;
	regs->sci_odata = *data++;
	icmd |= SCI_ICMD_ACK;
	regs->sci_icmd = icmd;

	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_ACK);
	WAIT_FOR_NOT_REQ(regs);
	regs->sci_icmd = icmd;
	++cnt;
	if (--count > 0)
		goto loop;
scsi_timeout_error:
	return cnt;
}

static int
sci_data_in(regs, phase, count, data)
	register volatile sci_regmap_t	*regs;
	unsigned char			*data;
{
	register unsigned char	icmd;
	register int		cnt=0;

	/* ..checks.. */

	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);

loop:
	/* SCSI bus phase not valid until REQ is true. */
	WAIT_FOR_REQ(regs);
	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
		return cnt;

	*data++ = regs->sci_data;
	icmd |= SCI_ICMD_ACK;
	regs->sci_icmd = icmd;

	icmd &= ~SCI_ICMD_ACK;
	WAIT_FOR_NOT_REQ(regs);
	regs->sci_icmd = icmd;
	++cnt;
	if (--count > 0)
		goto loop;

scsi_timeout_error:
	return cnt;
}

/*
 * Return -1 (error) or number of bytes sent (>=0).
 */
static int
ncr5380_command_transfer(regs, maxlen, data, status, msg)
	register volatile sci_regmap_t *regs;
	int maxlen;
	u_char *data;
	u_char *status;
	u_char *msg;
{
	int	xfer, phase;

	xfer = 0;
	regs->sci_icmd = 0;

	while (1) {

		WAIT_FOR_REQ(regs);

		phase = SCI_CUR_PHASE(regs->sci_bus_csr);

		switch (phase) {
			case SCSI_PHASE_CMD:
				SCI_ACK(regs,SCSI_PHASE_CMD);
				xfer += sci_data_out(regs, SCSI_PHASE_CMD,
						   	maxlen, data);
				goto out;

			case SCSI_PHASE_DATA_IN:
				printf("command_transfer: Data in phase?\n");
				goto err;

			case SCSI_PHASE_DATA_OUT:
				printf("command_transfer: Data out phase?\n");
				goto err;

			case SCSI_PHASE_STATUS:
				SCI_ACK(regs,SCSI_PHASE_STATUS);
				printf("command_transfer: status in...\n");
				sci_data_in(regs, SCSI_PHASE_STATUS,
					  	1, status);
				printf("command_transfer: status=0x%x\n", *status);
				goto err;

			case SCSI_PHASE_MESSAGE_IN:
				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
				printf("command_transfer: msg in?\n");
				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
					  	1, msg);
				break;

			case SCSI_PHASE_MESSAGE_OUT:
				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
					  	1, msg);
				break;

			default:
				printf("command_transfer: Unexpected phase 0x%x\n", phase);
				goto err;
		}
	}
scsi_timeout_error:
err:
	xfer = -1;
out:
	return xfer;
}

static int
ncr5380_data_transfer(regs, maxlen, data, status, msg)
	register volatile sci_regmap_t *regs;
	int maxlen;
	u_char *data, *status, *msg;
{
	int	retlen = 0, xfer, phase;

	regs->sci_icmd = 0;

	*status = 0;

	while (1) {

		WAIT_FOR_REQ(regs);

		phase = SCI_CUR_PHASE(regs->sci_bus_csr);

		switch (phase) {
			case SCSI_PHASE_CMD:
				printf("Command phase in data_transfer().\n");
				return retlen;
			case SCSI_PHASE_DATA_IN:
				SCI_ACK(regs,SCSI_PHASE_DATA_IN);
#if PSEUDO_DMA
				xfer = sci_pdma_in(regs, SCSI_PHASE_DATA_IN,
						  	maxlen, data);
#else
				xfer = sci_data_in(regs, SCSI_PHASE_DATA_IN,
						  	maxlen, data);
#endif
				retlen += xfer;
				maxlen -= xfer;
				break;
			case SCSI_PHASE_DATA_OUT:
				SCI_ACK(regs,SCSI_PHASE_DATA_OUT);
#if PSEUDO_DMA
				xfer = sci_pdma_out(regs, SCSI_PHASE_DATA_OUT,
						   	maxlen, data);
#else
				xfer = sci_data_out(regs, SCSI_PHASE_DATA_OUT,
						   	maxlen, data);
#endif
				retlen += xfer;
				maxlen -= xfer;
				break;
			case SCSI_PHASE_STATUS:
				SCI_ACK(regs,SCSI_PHASE_STATUS);
				sci_data_in(regs, SCSI_PHASE_STATUS,
					  	1, status);
				break;
			case SCSI_PHASE_MESSAGE_IN:
				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
					  	1, msg);
				if (*msg == 0) {
					return retlen;
				} else {
					printf( "message 0x%x in "
						"data_transfer.\n", *msg);
				}
				break;
			case SCSI_PHASE_MESSAGE_OUT:
				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
					  	1, msg);
				break;
			default:
				printf( "Unexpected phase 0x%x in "
					"data_transfer().\n", phase);
scsi_timeout_error:
				return retlen;
				break;
		}
	}
}

/*
 * Returns 0 on success, -1 on internal error, or the status byte
 */
static int
ncr5380_dorequest(sc, target, lun, cmd, cmdlen, databuf, datalen, sent)
	struct ncr5380_softc *sc;
	int target, lun;
	u_char *cmd;
	int cmdlen;
	char *databuf;
	int datalen, *sent;
{
	register volatile sci_regmap_t *regs = sc->sc_regs;
	int	cmd_bytes_sent, r;
	u_char	stat, msg, c;

#ifdef	DEBUG
	if (ncr5380_debug) {
		printf("ncr5380_dorequest: target=%d, lun=%d\n", target, lun);
	}
#endif

	*sent = 0;

	if ( ( r = ncr5380_select_target(regs, 7, target, 1) ) != SCSI_RET_SUCCESS) {
#ifdef	DEBUG
		if (ncr5380_debug) {
			printf("ncr5380_dorequest: select returned %d\n", r);
		}
#endif

		SCI_CLR_INTR(regs);
		switch (r) {

		case SCSI_RET_NEED_RESET:
			printf("ncr5380_dorequest: target=%d, lun=%d, r=%d resetting...\n",
				   target, lun, r);
			reset_adapter(sc);
			ncr5380_reset_scsibus(sc);
			/* fall through */
		case SCSI_RET_RETRY:
			return 0x08;	/* Busy - tell common code to retry. */

		default:
			printf("ncr5380_dorequest: target=%d, lun=%d, error=%d.\n",
				target, lun, r);
			/* fall through */
		case SCSI_RET_DEVICE_DOWN:
			return -1;	/* Dead - tell common code to give up. */
		}
	}

	c = 0x80 | lun;

	if ((cmd_bytes_sent = ncr5380_command_transfer(regs, cmdlen,
				(u_char *) cmd, &stat, &c)) != cmdlen)
	{
		SCI_CLR_INTR(regs);
		if (cmd_bytes_sent >= 0) {
			printf("Data underrun sending CCB (%d bytes of %d, sent).\n",
				   cmd_bytes_sent, cmdlen);
		}
		return -1;
	}

	*sent = ncr5380_data_transfer(regs, datalen, (u_char *)databuf,
					&stat, &msg);
#ifdef	DEBUG
	if (ncr5380_debug) {
		printf("ncr5380_dorequest: data transfered = %d\n", *sent);
	}
#endif

	return stat;
}

static int
ncr5380_generic(adapter, id, lun, cmd, cmdlen, databuf, datalen)
	void *adapter;
	int id, lun;
	struct scsi_generic *cmd;
	int cmdlen;
	void *databuf;
	int datalen;
{
	register struct ncr5380_softc *sc = adapter;
	int i, j, sent;

	if (cmd->opcode == TEST_UNIT_READY)	/* XXX */
		cmd->bytes[0] = ((u_char) lun << 5);

	i = ncr5380_dorequest(sc, id, lun, (u_char *) cmd, cmdlen,
					 databuf, datalen, &sent);

	return i;
}

static int
ncr5380_group0(adapter, id, lun, opcode, addr, len, flags, databuf, datalen)
	void *adapter;
	int id, lun, opcode, addr, len, flags;
	caddr_t databuf;
	int datalen;
{
	register struct ncr5380_softc *sc = adapter;
	unsigned char cmd[6];
	int i, j, sent;

	cmd[0] = opcode;		/* Operation code */
	cmd[1] = (lun << 5) | ((addr >> 16) & 0x1F); /* Lun & MSB of addr */
	cmd[2] = (addr >> 8) & 0xFF;	/* addr */
	cmd[3] = addr & 0xFF;		/* LSB of addr */
	cmd[4] = len;			/* Allocation length */
	cmd[5] = flags;			/* Link/Flag */

	i = ncr5380_dorequest(sc, id, lun, cmd, 6, databuf, datalen, &sent);

	return i;
}