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