si.c revision 1.13 1 /* $NetBSD: si.c,v 1.13 1995/01/10 16:45:26 gwr Exp $ */
2
3 /*
4 * Copyright (C) 1994 Adam Glass, Gordon W. Ross
5 * Copyright (C) 1993 Allen K. Briggs, Chris P. Caputo,
6 * Michael L. Finch, Bradley A. Grantham, and
7 * Lawrence A. Kesteloot
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the Alice Group.
21 * 4. The names of the Alice Group or any of its members may not be used
22 * to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /* #define DEBUG 1 */
38
39 /* XXX - Need to add support for real DMA. -gwr */
40 /* #define PSEUDO_DMA 1 (broken) */
41
42 static int si_debug=0;
43
44 #include <sys/types.h>
45 #include <sys/malloc.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/device.h>
53
54 #include <machine/autoconf.h>
55 #include <machine/isr.h>
56 #include <machine/obio.h>
57
58 #include <scsi/scsi_all.h>
59 #include <scsi/scsi_debug.h>
60 #include <scsi/scsiconf.h>
61
62 #include "scsi_defs.h"
63 #include "scsi_5380.h"
64
65 #define SCI_PHASE_DISC 0 /* sort of ... */
66 #define SCI_CLR_INTR(regs) ((volatile)(regs->sci_iack))
67 #define SCI_ACK(ptr,phase) (ptr)->sci_tcmd = (phase)
68 #define SCSI_TIMEOUT_VAL 10000000
69 #define WAIT_FOR_NOT_REQ(ptr) { \
70 int scsi_timeout = SCSI_TIMEOUT_VAL; \
71 while ( ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
72 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
73 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
74 (--scsi_timeout) ); \
75 if (!scsi_timeout) { \
76 printf("scsi timeout--WAIT_FOR_NOT_REQ---%s, line %d.\n", \
77 __FILE__, __LINE__); \
78 goto scsi_timeout_error; \
79 } \
80 }
81 #define WAIT_FOR_REQ(ptr) { \
82 int scsi_timeout = SCSI_TIMEOUT_VAL; \
83 while ( (((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
84 (((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
85 (((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
86 (--scsi_timeout) ); \
87 if (!scsi_timeout) { \
88 printf("scsi timeout--WAIT_FOR_REQ---%s, line %d.\n", \
89 __FILE__, __LINE__); \
90 goto scsi_timeout_error; \
91 } \
92 }
93 #define WAIT_FOR_BSY(ptr) { \
94 int scsi_timeout = SCSI_TIMEOUT_VAL; \
95 while ( (((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
96 (((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
97 (((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
98 (--scsi_timeout) ); \
99 if (!scsi_timeout) { \
100 printf("scsi timeout--WAIT_FOR_BSY---%s, line %d.\n", \
101 __FILE__, __LINE__); \
102 goto scsi_timeout_error; \
103 } \
104 }
105
106 #ifdef DDB
107 int Debugger();
108 #else
109 #define Debugger() panic("Should call Debugger here %s:%d", \
110 __FILE__, __LINE__)
111 #endif
112
113 struct ncr5380_softc {
114 struct device sc_dev;
115 volatile void *sc_regs;
116 int sc_adapter_target;
117 struct scsi_link sc_link;
118 };
119
120 static void ncr5380_minphys(struct buf *bp);
121 static int ncr5380_scsi_cmd(struct scsi_xfer *xs);
122
123 static int ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
124 static int ncr5380_reset_target(int adapter, int target);
125 static int ncr5380_poll(int adapter, int timeout);
126 static int ncr5380_send_cmd(struct scsi_xfer *xs);
127
128 int ncr5380_intr(void *);
129
130 static int si_generic(int adapter, int id, int lun,
131 struct scsi_generic *cmd, int cmdlen,
132 void *databuf, int datalen);
133 static int si_group0(int adapter, int id, int lun,
134 int opcode, int addr, int len,
135 int flags, caddr_t databuf, int datalen);
136
137 static char scsi_name[] = "si";
138
139 struct scsi_adapter ncr5380_switch = {
140 ncr5380_scsi_cmd, /* scsi_cmd() */
141 ncr5380_minphys, /* scsi_minphys() */
142 NULL, /* open_target_lu() */
143 NULL, /* close_target_lu() */
144 };
145
146 /* This is copied from julian's bt driver */
147 /* "so we have a default dev struct for our link struct." */
148 struct scsi_device ncr_dev = {
149 NULL, /* Use default error handler. */
150 NULL, /* Use default start handler. */
151 NULL, /* Use default async handler. */
152 NULL, /* Use default "done" routine. */
153 };
154
155 static int si_match();
156 static void si_attach();
157
158 struct cfdriver sicd = {
159 NULL, "si", si_match, si_attach, DV_DULL,
160 sizeof(struct ncr5380_softc), NULL, 0,
161 };
162
163 static int
164 si_print(aux, name)
165 void *aux;
166 char *name;
167 {
168 }
169
170 static int
171 si_match(parent, vcf, args)
172 struct device *parent;
173 void *vcf, *args;
174 {
175 struct cfdata *cf = vcf;
176 struct confargs *ca = args;
177 int x;
178
179 /* Allow default address for OBIO only. */
180 switch (ca->ca_bustype) {
181 case BUS_OBIO:
182 if (ca->ca_paddr == -1)
183 ca->ca_paddr = OBIO_NCR_SCSI;
184 break;
185 case BUS_VME16:
186 if (ca->ca_paddr == -1)
187 return (0);
188 break;
189 default:
190 return (0);
191 }
192
193 /* Default interrupt priority always splbio==2 */
194 if (ca->ca_intpri == -1)
195 ca->ca_intpri = 2;
196
197 x = bus_peek(ca->ca_bustype, ca->ca_paddr, 1);
198 return (x != -1);
199 }
200
201 static void
202 si_attach(parent, self, args)
203 struct device *parent, *self;
204 void *args;
205 {
206 struct ncr5380_softc *ncr5380 = (struct ncr5380_softc *) self;
207 register volatile sci_regmap_t *regs;
208 struct confargs *ca = args;
209
210 switch (ca->ca_bustype) {
211
212 case BUS_OBIO:
213 regs = (sci_regmap_t *)
214 obio_alloc(ca->ca_paddr, sizeof(*regs));
215 isr_add_autovect(ncr5380_intr, (void *)ncr5380,
216 ca->ca_intpri);
217 break;
218
219 case BUS_VME16:
220 regs = (sci_regmap_t *)
221 bus_mapin(ca->ca_bustype, ca->ca_paddr, sizeof(*regs));
222 isr_add_vectored(ncr5380_intr, (void *)ncr5380,
223 ca->ca_intpri, ca->ca_intvec);
224 break;
225
226 default:
227 printf("unknown\n");
228 return;
229 }
230
231 ncr5380->sc_regs = regs;
232
233 /*
234 * fill in the prototype scsi_link.
235 */
236 ncr5380->sc_link.adapter_softc = ncr5380;
237 ncr5380->sc_link.adapter_target = 7;
238 ncr5380->sc_link.adapter = &ncr5380_switch;
239 ncr5380->sc_link.device = &ncr_dev;
240 ncr5380->sc_link.openings = 1;
241 #ifdef DEBUG
242 ncr5380->sc_link.flags |= si_debug;
243 #endif
244
245 printf("\n");
246 config_found(self, &(ncr5380->sc_link), si_print);
247 }
248
249 #define MIN_PHYS 65536 /*BARF!!!!*/
250 static void
251 ncr5380_minphys(struct buf *bp)
252 {
253 if (bp->b_bcount > MIN_PHYS) {
254 printf("Uh-oh... ncr5380_minphys setting bp->b_bcount = %x.\n", MIN_PHYS);
255 bp->b_bcount = MIN_PHYS;
256 }
257 }
258 #undef MIN_PHYS
259
260 static int
261 ncr5380_scsi_cmd(struct scsi_xfer *xs)
262 {
263 int flags, s, r;
264
265 flags = xs->flags;
266 if (xs->bp) flags |= (SCSI_NOSLEEP);
267 if ( flags & ITSDONE ) {
268 printf("Already done?");
269 xs->flags &= ~ITSDONE;
270 }
271 if ( ! ( flags & INUSE ) ) {
272 printf("Not in use?");
273 xs->flags |= INUSE;
274 }
275
276 if ( flags & SCSI_RESET ) {
277 printf("flags & SCSIRESET.\n");
278 if ( ! ( flags & SCSI_NOSLEEP ) ) {
279 s = splbio();
280 ncr5380_reset_target(xs->sc_link->scsibus, xs->sc_link->target);
281 splx(s);
282 return(SUCCESSFULLY_QUEUED);
283 } else {
284 ncr5380_reset_target(xs->sc_link->scsibus, xs->sc_link->target);
285 #if 0
286 if (ncr5380_poll(xs->sc_link->scsibus, xs->timeout)) {
287 return (COMPLETE);
288 }
289 #endif
290 return (COMPLETE);
291 }
292 }
293 #if 0
294 /*
295 * OK. Now that that's over with, let's pack up that
296 * SCSI puppy and send it off. If we can, we'll just
297 * queue and go; otherwise, we'll wait for the command
298 * to finish.
299 */
300 if ( ! ( flags & SCSI_NOSLEEP ) ) {
301 s = splbio();
302 ncr5380_send_cmd(xs);
303 splx(s);
304 return(SUCCESSFULLY_QUEUED);
305 }
306 #endif
307
308 r = ncr5380_send_cmd(xs);
309 xs->flags |= ITSDONE;
310 scsi_done(xs);
311 switch(r) {
312 case COMPLETE:
313 case SUCCESSFULLY_QUEUED:
314 r = SUCCESSFULLY_QUEUED;
315 if (xs->flags & SCSI_POLL)
316 r = COMPLETE;
317 break;
318 default:
319 break;
320 }
321 return r;
322 #if 0
323 do {
324 if (ncr5380_poll(xs->sc_link->scsibus, xs->timeout)) {
325 if ( ! ( xs->flags & SCSI_SILENT ) )
326 printf("cmd fail.\n");
327 cmd_cleanup
328 xs->error = XS_DRIVER_STUFFUP;
329 splx(s);
330 }
331 } while ( ! ( xs->flags & ITSDONE ) );
332 #endif
333 }
334
335 static int
336 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
337 {
338 u_char *b = (u_char *) xs->cmd;
339 int i = 0;
340
341 if ( ! ( xs->flags & SCSI_RESET ) ) {
342 printf("si(%d:%d:%d)-",
343 xs->sc_link->scsibus,
344 xs->sc_link->target,
345 xs->sc_link->lun);
346 while (i < xs->cmdlen) {
347 if (i) printf(",");
348 printf("%x",b[i++]);
349 }
350 printf("-\n");
351 } else {
352 printf("si(%d:%d:%d)-RESET-\n",
353 xs->sc_link->scsibus,
354 xs->sc_link->target,
355 xs->sc_link->lun);
356 }
357 }
358
359 /*
360 * Actual chip control.
361 */
362
363 int
364 ncr5380_intr(void *arg)
365 {
366 register struct ncr5380_softc *ncr5380 = arg;
367 register volatile sci_regmap_t *regs = ncr5380->sc_regs;
368
369 if (regs->sci_csr & SCI_CSR_INT) {
370 SCI_CLR_INTR(regs);
371 #ifdef DEBUG
372 printf (" ncr_intr\n");
373 #endif
374 return (1); /* we handled it */
375 }
376 return (0);
377 }
378
379 int
380 scsi_irq_intr(void)
381 {
382 #if 0 /* XXX - no way to get regs */
383 register volatile sci_regmap_t *regs = ncr;
384 if (regs->sci_csr != SCI_CSR_PHASE_MATCH)
385 printf("scsi_irq_intr called (not just phase match -- "
386 "csr = 0x%x, bus_csr = 0x%x).\n",
387 regs->sci_csr, regs->sci_bus_csr);
388 ncr5380_intr(0);
389 #endif
390 return 1;
391 }
392
393 int
394 scsi_drq_intr(void)
395 {
396 #if 0
397 printf("scsi_drq_intr called.\n");
398 ncr5380_intr(0);
399 #endif
400 return 1;
401 }
402
403 static int
404 ncr5380_reset_target(int adapter, int target)
405 {
406 register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
407 register volatile sci_regmap_t *regs = ncr5380->sc_regs;
408 int dummy;
409
410 regs->sci_icmd = SCI_ICMD_TEST;
411 regs->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
412 delay(2500);
413 regs->sci_icmd = 0;
414
415 regs->sci_mode = 0;
416 regs->sci_tcmd = SCI_PHASE_DISC;
417 regs->sci_sel_enb = 0;
418
419 SCI_CLR_INTR(regs);
420 SCI_CLR_INTR(regs);
421 }
422
423 static int
424 ncr5380_poll(int adapter, int timeout)
425 {
426 }
427
428 static int
429 ncr5380_send_cmd(struct scsi_xfer *xs)
430 {
431 int s;
432 int sense;
433
434 /* ncr5380_show_scsi_cmd(xs); */
435 /* XXX - At autoconfig time, this lowers the spl... */
436 s = splbio();
437 sense = si_generic( xs->sc_link->scsibus, xs->sc_link->target,
438 xs->sc_link->lun, xs->cmd, xs->cmdlen,
439 xs->data, xs->datalen );
440 splx(s);
441 switch (sense) {
442 case 0: /* success */
443 xs->resid = 0;
444 xs->error = XS_NOERROR;
445 break;
446
447 case 0x02: /* Check condition */
448 #ifdef DEBUG
449 printf("check cond. target %d.\n",
450 xs->sc_link->target);
451 #endif
452 delay(10); /* Phil's fix for slow devices. */
453 s = splbio();
454 si_group0(xs->sc_link->scsibus,
455 xs->sc_link->target,
456 xs->sc_link->lun,
457 0x3, 0x0,
458 sizeof(struct scsi_sense_data),
459 0, (caddr_t) &(xs->sense),
460 sizeof(struct scsi_sense_data));
461 splx(s);
462 xs->error = XS_SENSE;
463 break;
464 case 0x08: /* Busy */
465 xs->error = XS_BUSY;
466 break;
467 default:
468 xs->error = XS_DRIVER_STUFFUP;
469 break;
470
471 }
472 return (COMPLETE);
473 }
474
475 static int
476 si_select_target(register volatile sci_regmap_t *regs,
477 u_char myid, u_char tid, int with_atn)
478 {
479 register u_char bid, icmd;
480 int ret = SCSI_RET_RETRY;
481
482 if ((regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
483 (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
484 (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
485 return ret;
486
487 /* for our purposes.. */
488 myid = 1 << myid;
489 tid = 1 << tid;
490
491 regs->sci_sel_enb = 0; /* we don't want any interrupts. */
492 regs->sci_tcmd = 0; /* get into a harmless state */
493 regs->sci_mode = 0; /* get into a harmless state */
494
495 regs->sci_odata = myid;
496 regs->sci_mode = SCI_MODE_ARB;
497 /* regs->sci_mode |= SCI_MODE_ARB; XXX? */
498 /* AIP might not set if BSY went true after we checked */
499 for (bid = 0; bid < 20; bid++) /* 20usec circa */
500 if (regs->sci_icmd & SCI_ICMD_AIP)
501 break;
502 if ((regs->sci_icmd & SCI_ICMD_AIP) == 0) {
503 goto lost;
504 }
505
506 delay(2200); /* 2.2 millisecond arbitration delay */
507
508 if (regs->sci_icmd & SCI_ICMD_LST) {
509 #ifdef DEBUG
510 printf ("lost 1\n");
511 #endif
512 goto lost;
513 }
514
515 regs->sci_mode &= ~SCI_MODE_PAR_CHK;
516 bid = regs->sci_data;
517
518 if ((bid & ~myid) > myid) {
519 #ifdef DEBUG
520 printf ("lost 2\n");
521 #endif
522 goto lost;
523 }
524 if (regs->sci_icmd & SCI_ICMD_LST) {
525 #ifdef DEBUG
526 printf ("lost 3\n");
527 #endif
528 goto lost;
529 }
530
531 /* Won arbitration, enter selection phase now */
532 icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
533 icmd |= (with_atn ? (SCI_ICMD_SEL|SCI_ICMD_ATN) : SCI_ICMD_SEL);
534 icmd |= SCI_ICMD_BSY;
535 regs->sci_icmd = icmd;
536
537 if (regs->sci_icmd & SCI_ICMD_LST) {
538 #ifdef DEBUG
539 printf ("nosel\n");
540 #endif
541 goto nosel;
542 }
543
544 /* XXX a target that violates specs might still drive the bus XXX */
545 /* XXX should put our id out, and after the delay check nothi XXX */
546 /* XXX ng else is out there. XXX */
547
548 delay2us();
549
550 regs->sci_tcmd = 0;
551 regs->sci_odata = myid | tid;
552 regs->sci_sel_enb = 0;
553
554 /* regs->sci_mode &= ~SCI_MODE_ARB; 2 deskew delays, too */
555 regs->sci_mode = 0; /* 2 deskew delays, too */
556
557 icmd |= SCI_ICMD_DATA;
558 icmd &= ~(SCI_ICMD_BSY);
559
560 regs->sci_icmd = icmd;
561
562 /* bus settle delay, 400ns */
563 delay2us(); /* too much (was 2) ? */
564
565 /* regs->sci_mode |= SCI_MODE_PAR_CHK; */
566
567 {
568 register int timeo = 2500;/* 250 msecs in 100 usecs chunks */
569 while ((regs->sci_bus_csr & SCI_BUS_BSY) == 0) {
570 if (--timeo > 0) {
571 delay(100);
572 } else {
573 goto nodev;
574 }
575 }
576 }
577
578 icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL);
579 regs->sci_icmd = icmd;
580 /* regs->sci_sel_enb = myid;*/ /* looks like we should NOT have it */
581 return SCSI_RET_SUCCESS;
582 nodev:
583 ret = SCSI_RET_DEVICE_DOWN;
584 regs->sci_sel_enb = myid;
585 nosel:
586 icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL|SCI_ICMD_ATN);
587 regs->sci_icmd = icmd;
588 lost:
589 regs->sci_mode = 0;
590
591 return ret;
592 }
593
594 sci_data_out(regs, phase, count, data)
595 register volatile sci_regmap_t *regs;
596 unsigned char *data;
597 {
598 register unsigned char icmd;
599 register int cnt=0;
600
601 /* ..checks.. */
602
603 icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
604 loop:
605 if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
606 return cnt;
607
608 WAIT_FOR_REQ(regs);
609 icmd |= SCI_ICMD_DATA;
610 regs->sci_icmd = icmd;
611 regs->sci_odata = *data++;
612 icmd |= SCI_ICMD_ACK;
613 regs->sci_icmd = icmd;
614
615 icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_ACK);
616 WAIT_FOR_NOT_REQ(regs);
617 regs->sci_icmd = icmd;
618 ++cnt;
619 if (--count > 0)
620 goto loop;
621 scsi_timeout_error:
622 return cnt;
623 }
624
625 sci_data_in(regs, phase, count, data)
626 register volatile sci_regmap_t *regs;
627 unsigned char *data;
628 {
629 register unsigned char icmd;
630 register int cnt=0;
631
632 /* ..checks.. */
633
634 icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
635
636 loop:
637 if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
638 return cnt;
639
640 WAIT_FOR_REQ(regs);
641 *data++ = regs->sci_data;
642 icmd |= SCI_ICMD_ACK;
643 regs->sci_icmd = icmd;
644
645 icmd &= ~SCI_ICMD_ACK;
646 WAIT_FOR_NOT_REQ(regs);
647 regs->sci_icmd = icmd;
648 ++cnt;
649 if (--count > 0)
650 goto loop;
651
652 scsi_timeout_error:
653 return cnt;
654 }
655
656 static int
657 si_command_transfer(register volatile sci_regmap_t *regs,
658 int maxlen, u_char *data, u_char *status, u_char *msg)
659 {
660 int xfer=0, phase;
661
662 /* printf("command_transfer called for 0x%x.\n", *data); */
663
664 regs->sci_icmd = 0;
665
666 while (1) {
667
668 WAIT_FOR_REQ(regs);
669
670 phase = SCI_CUR_PHASE(regs->sci_bus_csr);
671
672 switch (phase) {
673 case SCSI_PHASE_CMD:
674 SCI_ACK(regs,SCSI_PHASE_CMD);
675 xfer += sci_data_out(regs, SCSI_PHASE_CMD,
676 maxlen, data);
677 return xfer;
678 case SCSI_PHASE_DATA_IN:
679 printf("Data in phase in command_transfer?\n");
680 return 0;
681 case SCSI_PHASE_DATA_OUT:
682 printf("Data out phase in command_transfer?\n");
683 return 0;
684 case SCSI_PHASE_STATUS:
685 SCI_ACK(regs,SCSI_PHASE_STATUS);
686 printf("status in command_transfer.\n");
687 sci_data_in(regs, SCSI_PHASE_STATUS,
688 1, status);
689 break;
690 case SCSI_PHASE_MESSAGE_IN:
691 SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
692 printf("msgin in command_transfer.\n");
693 sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
694 1, msg);
695 break;
696 case SCSI_PHASE_MESSAGE_OUT:
697 SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
698 sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
699 1, msg);
700 break;
701 default:
702 printf("Unexpected phase 0x%x in "
703 "command_transfer().\n", phase);
704 scsi_timeout_error:
705 return xfer;
706 break;
707 }
708 }
709 }
710
711 static int
712 si_data_transfer(register volatile sci_regmap_t *regs,
713 int maxlen, u_char *data, u_char *status, u_char *msg)
714 {
715 int retlen = 0, xfer, phase;
716
717 regs->sci_icmd = 0;
718
719 *status = 0;
720
721 while (1) {
722
723 WAIT_FOR_REQ(regs);
724
725 phase = SCI_CUR_PHASE(regs->sci_bus_csr);
726
727 switch (phase) {
728 case SCSI_PHASE_CMD:
729 printf("Command phase in data_transfer().\n");
730 return retlen;
731 case SCSI_PHASE_DATA_IN:
732 SCI_ACK(regs,SCSI_PHASE_DATA_IN);
733 #if PSEUDO_DMA
734 xfer = sci_pdma_in(regs, SCSI_PHASE_DATA_IN,
735 maxlen, data);
736 #else
737 xfer = sci_data_in(regs, SCSI_PHASE_DATA_IN,
738 maxlen, data);
739 #endif
740 retlen += xfer;
741 maxlen -= xfer;
742 break;
743 case SCSI_PHASE_DATA_OUT:
744 SCI_ACK(regs,SCSI_PHASE_DATA_OUT);
745 #if PSEUDO_DMA
746 xfer = sci_pdma_out(regs, SCSI_PHASE_DATA_OUT,
747 maxlen, data);
748 #else
749 xfer = sci_data_out(regs, SCSI_PHASE_DATA_OUT,
750 maxlen, data);
751 #endif
752 retlen += xfer;
753 maxlen -= xfer;
754 break;
755 case SCSI_PHASE_STATUS:
756 SCI_ACK(regs,SCSI_PHASE_STATUS);
757 sci_data_in(regs, SCSI_PHASE_STATUS,
758 1, status);
759 break;
760 case SCSI_PHASE_MESSAGE_IN:
761 SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
762 sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
763 1, msg);
764 if (*msg == 0) {
765 return retlen;
766 } else {
767 printf( "message 0x%x in "
768 "data_transfer.\n", *msg);
769 }
770 break;
771 case SCSI_PHASE_MESSAGE_OUT:
772 SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
773 sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
774 1, msg);
775 break;
776 default:
777 printf( "Unexpected phase 0x%x in "
778 "data_transfer().\n", phase);
779 scsi_timeout_error:
780 return retlen;
781 break;
782 }
783 }
784 }
785
786 static int
787 si_dorequest(register volatile sci_regmap_t *regs,
788 int target, int lun, u_char *cmd, int cmdlen,
789 char *databuf, int datalen, int *sent, int *ret)
790 {
791 /* Returns 0 on success, -1 on internal error, or the status byte */
792 int cmd_bytes_sent, r;
793 u_char stat, msg, c;
794
795 *sent = 0;
796
797 if ( ( r = si_select_target(regs, 7, target, 1) ) != SCSI_RET_SUCCESS) {
798 *ret = r;
799 SCI_CLR_INTR(regs);
800 switch (r) {
801 case SCSI_RET_RETRY:
802 return 0x08;
803 default:
804 printf("si_select_target(target %d, lun %d) failed(%d).\n",
805 target, lun, r);
806 case SCSI_RET_DEVICE_DOWN:
807 return -1;
808 }
809 }
810
811 c = 0x80 | lun;
812
813 if ((cmd_bytes_sent = si_command_transfer(regs, cmdlen,
814 (u_char *) cmd, &stat, &c))
815 != cmdlen) {
816 SCI_CLR_INTR(regs);
817 *ret = SCSI_RET_COMMAND_FAIL;
818 printf("Data underrun sending CCB (%d bytes of %d, sent).\n",
819 cmd_bytes_sent, cmdlen);
820 return -1;
821 }
822
823 *sent=si_data_transfer(regs, datalen, (u_char *)databuf,
824 &stat, &msg);
825
826 *ret = 0;
827 return stat;
828 }
829
830 static int
831 si_generic(int adapter, int id, int lun, struct scsi_generic *cmd,
832 int cmdlen, void *databuf, int datalen)
833 {
834 register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
835 register volatile sci_regmap_t *regs = ncr5380->sc_regs;
836 int i,j,sent,ret;
837
838 if (cmd->opcode == TEST_UNIT_READY) /* XXX */
839 cmd->bytes[0] = ((u_char) lun << 5);
840
841 i = si_dorequest(regs, id, lun, (u_char *) cmd, cmdlen,
842 databuf, datalen, &sent, &ret);
843
844 return i;
845 }
846
847 static int
848 si_group0(int adapter, int id, int lun, int opcode, int addr, int len,
849 int flags, caddr_t databuf, int datalen)
850 {
851 register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
852 register volatile sci_regmap_t *regs = ncr5380->sc_regs;
853 unsigned char cmd[6];
854 int i,j,sent,ret;
855
856 cmd[0] = opcode; /* Operation code */
857 cmd[1] = (lun << 5) | ((addr >> 16) & 0x1F); /* Lun & MSB of addr */
858 cmd[2] = (addr >> 8) & 0xFF; /* addr */
859 cmd[3] = addr & 0xFF; /* LSB of addr */
860 cmd[4] = len; /* Allocation length */
861 cmd[5] = flags; /* Link/Flag */
862
863 i = si_dorequest(regs, id, lun, cmd, 6, databuf, datalen, &sent, &ret);
864
865 return i;
866 }
867