ncr5380.c revision 1.11 1 /* $NetBSD: ncr5380.c,v 1.11 1995/10/31 15:32:35 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34 #ifdef DBG_NOSTATIC
35 # define static
36 #endif
37 #ifdef DBG_SEL
38 # define DBG_SELPRINT(a,b) printf(a,b)
39 #else
40 # define DBG_SELPRINT(a,b)
41 #endif
42 #ifdef DBG_PIO
43 # define DBG_PIOPRINT(a,b,c) printf(a,b,c)
44 #else
45 # define DBG_PIOPRINT(a,b,c)
46 #endif
47 #ifdef DBG_INF
48 # define DBG_INFPRINT(a,b,c) a(b,c)
49 #else
50 # define DBG_INFPRINT(a,b,c)
51 #endif
52 #ifdef DBG_PID
53 static char *last_hit = NULL, *olast_hit = NULL;
54 # define PID(a) olast_hit = last_hit; last_hit = a;
55 #else
56 # define PID(a)
57 #endif
58
59 /*
60 * Bit mask of targets you want debugging to be shown
61 */
62 u_char dbg_target_mask = 0x7f;
63
64 /*
65 * Set bit for target when parity checking must be disabled.
66 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50%
67 * of all transfers while the data is correct!?
68 */
69 u_char ncr5380_no_parchk = 0xff;
70
71 /*
72 * This is the default sense-command we send.
73 */
74 static u_char sense_cmd[] = {
75 REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
76 };
77
78 /*
79 * True if the main co-routine is running
80 */
81 static volatile int main_running = 0;
82
83 /*
84 * Mask of targets selected
85 */
86 static u_char busy;
87
88 static void ncr5380_minphys(struct buf *bp);
89 static int ncr5380_scsi_cmd(struct scsi_xfer *xs);
90 static int ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
91
92 struct scsi_adapter ncr5380_switch = {
93 ncr5380_scsi_cmd, /* scsi_cmd() */
94 ncr5380_minphys, /* scsi_minphys() */
95 0, /* open_target_lu() */
96 0 /* close_target_lu() */
97 };
98
99 struct scsi_device ncr5380_dev = {
100 NULL, /* use default error handler */
101 NULL, /* do not have a start functio */
102 NULL, /* have no async handler */
103 NULL /* Use default done routine */
104 };
105
106
107 static SC_REQ req_queue[NREQ];
108 static SC_REQ *free_head = NULL; /* Free request structures */
109
110
111 /*
112 * Inline functions:
113 */
114
115 /*
116 * Determine the size of a SCSI command.
117 */
118 extern __inline__ int command_size(opcode)
119 u_char opcode;
120 {
121 switch ((opcode >> 4) & 0xf) {
122 case 0:
123 case 1:
124 return (6);
125 case 2:
126 case 3:
127 return (10);
128 }
129 return (12);
130 }
131
132
133 /*
134 * Wait for request-line to become active. When it doesn't return 0.
135 * Otherwise return != 0.
136 * The timeouts in the 'wait_req_*' functions are arbitrary and rather
137 * large. In 99% of the invocations nearly no timeout is needed but in
138 * some cases (especially when using my tapedrive, a Tandberg 3600) the
139 * device is busy internally and the first SCSI-phase will be delayed.
140 */
141 extern __inline__ int wait_req_true(void)
142 {
143 int timeout = 25000;
144
145 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
146 delay(1);
147 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
148 }
149
150 /*
151 * Wait for request-line to become inactive. When it doesn't return 0.
152 * Otherwise return != 0.
153 */
154 extern __inline__ int wait_req_false(void)
155 {
156 int timeout = 25000;
157
158 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
159 delay(1);
160 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
161 }
162
163 extern __inline__ void ack_message()
164 {
165 SET_5380_REG(NCR5380_ICOM, 0);
166 }
167
168 extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
169 {
170 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
171 reqp->msgout = msg;
172 }
173
174 extern __inline__ void finish_req(SC_REQ *reqp)
175 {
176 int sps;
177 struct scsi_xfer *xs = reqp->xs;
178
179 #ifdef REAL_DMA
180 /*
181 * If we bounced, free the bounce buffer
182 */
183 if (reqp->dr_flag & DRIVER_BOUNCING)
184 free_bounceb(reqp->bounceb);
185 #endif /* REAL_DMA */
186 #ifdef DBG_REQ
187 if (dbg_target_mask & (1 << reqp->targ_id))
188 show_request(reqp, "DONE");
189 #endif
190 #ifdef DBG_ERR_RET
191 if (reqp->xs->error != 0)
192 show_request(reqp, "ERR_RET");
193 #endif
194
195 /*
196 * Return request to free-q
197 */
198 sps = splbio();
199 reqp->next = free_head;
200 free_head = reqp;
201 splx(sps);
202
203 xs->flags |= ITSDONE;
204 scsi_done(xs);
205 }
206
207 /*
208 * Auto config stuff....
209 */
210 int ncr_cprint __P((void *auxp, char *));
211 void ncr_attach __P((struct device *, struct device *, void *));
212 int ncr_match __P((struct device *, struct cfdata *, void *));
213
214 /*
215 * Tricks to make driver-name configurable
216 */
217 #define CFNAME(n) __CONCAT(n,cd)
218 #define CFSTRING(n) __STRING(n)
219
220 struct cfdriver CFNAME(DRNAME) = {
221 NULL, CFSTRING(DRNAME), (cfmatch_t)ncr_match, ncr_attach,
222 DV_DULL, sizeof(struct ncr_softc), NULL, 0 };
223
224 int
225 ncr_match(pdp, cdp, auxp)
226 struct device *pdp;
227 struct cfdata *cdp;
228 void *auxp;
229 {
230 return (machine_match(pdp, cdp, auxp, &CFNAME(DRNAME)));
231 }
232
233 void
234 ncr_attach(pdp, dp, auxp)
235 struct device *pdp, *dp;
236 void *auxp;
237 {
238 struct ncr_softc *sc;
239 int i;
240
241 sc = (struct ncr_softc *)dp;
242
243 sc->sc_link.adapter_softc = sc;
244 sc->sc_link.adapter_target = 7;
245 sc->sc_link.adapter = &ncr5380_switch;
246 sc->sc_link.device = &ncr5380_dev;
247 sc->sc_link.openings = NREQ - 1;
248
249 /*
250 * bitmasks
251 */
252 sc->sc_noselatn = 0;
253 sc->sc_selected = 0;
254
255 /*
256 * Initialize machine-type specific things...
257 */
258 scsi_mach_init(sc);
259 printf("\n");
260
261 /*
262 * Initialize request queue freelist.
263 */
264 for (i = 0; i < NREQ; i++) {
265 req_queue[i].next = free_head;
266 free_head = &req_queue[i];
267 }
268
269 /*
270 * Initialize the host adapter
271 */
272 scsi_idisable();
273 ENABLE_NCR5380(sc);
274 SET_5380_REG(NCR5380_ICOM, 0);
275 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
276 SET_5380_REG(NCR5380_TCOM, 0);
277 SET_5380_REG(NCR5380_IDSTAT, 0);
278 scsi_ienable();
279
280 /*
281 * attach all scsi units on us
282 */
283 config_found(dp, &sc->sc_link, ncr_cprint);
284 }
285
286 /*
287 * print diag if name is NULL else just extra
288 */
289 int
290 ncr_cprint(auxp, name)
291 void *auxp;
292 char *name;
293 {
294 if (name == NULL)
295 return (UNCONF);
296 return (QUIET);
297 }
298 /*
299 * End of auto config stuff....
300 */
301
302 /*
303 * Carry out a request from the high level driver.
304 */
305 static int
306 ncr5380_scsi_cmd(struct scsi_xfer *xs)
307 {
308 int sps;
309 SC_REQ *reqp;
310 int flags = xs->flags;
311
312 /*
313 * We do not queue RESET commands
314 */
315 if (flags & SCSI_RESET) {
316 scsi_reset(xs->sc_link->adapter_softc);
317 return (COMPLETE);
318 }
319
320 /*
321 * Get a request block
322 */
323 sps = splbio();
324 if ((reqp = free_head) == 0) {
325 splx(sps);
326 return (TRY_AGAIN_LATER);
327 }
328 free_head = reqp->next;
329 reqp->next = NULL;
330 splx(sps);
331
332 /*
333 * Initialize our private fields
334 */
335 reqp->dr_flag = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
336 reqp->phase = NR_PHASE;
337 reqp->msgout = MSG_NOOP;
338 reqp->status = SCSGOOD;
339 reqp->link = NULL;
340 reqp->xs = xs;
341 reqp->targ_id = xs->sc_link->target;
342 reqp->targ_lun = xs->sc_link->lun;
343 reqp->xdata_ptr = (u_char*)xs->data;
344 reqp->xdata_len = xs->datalen;
345 memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
346 reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
347
348 /*
349 * Sanity check on flags...
350 */
351 if (flags & ITSDONE) {
352 ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
353 xs->flags &= ~ITSDONE;
354 }
355 if (!(flags & INUSE)) {
356 ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
357 xs->flags |= ~INUSE;
358 }
359
360 #ifdef REAL_DMA
361 /*
362 * Check if DMA can be used on this request
363 */
364 if (scsi_dmaok(reqp))
365 reqp->dr_flag |= DRIVER_DMAOK;
366 #endif /* REAL_DMA */
367
368 /*
369 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
370 * commands are inserted at the head of the queue since any command
371 * will clear the existing contingent allegience condition and the sense
372 * data is only valid while the condition exists.
373 * When possible, link the command to a previous command to the same
374 * target. This is not very sensible when AUTO_SENSE is not defined!
375 * Interrupts are disabled while we are fiddling with the issue-queue.
376 */
377 sps = splbio();
378 if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
379 reqp->next = issue_q;
380 issue_q = reqp;
381 }
382 else {
383 SC_REQ *tmp, *link;
384
385 tmp = issue_q;
386 link = NULL;
387 do {
388 if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
389 link = tmp;
390 } while (tmp->next && (tmp = tmp->next));
391 tmp->next = reqp;
392 #ifdef AUTO_SENSE
393 if (link) {
394 link->link = reqp;
395 link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
396 }
397 #endif
398 }
399 splx(sps);
400
401 #ifdef DBG_REQ
402 if (dbg_target_mask & (1 << reqp->targ_id))
403 show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
404 "HEAD":"TAIL");
405 #endif
406
407 run_main(xs->sc_link->adapter_softc);
408
409 if (xs->flags & (SCSI_POLL|ITSDONE))
410 return (COMPLETE); /* We're booting or run_main has completed */
411 return (SUCCESSFULLY_QUEUED);
412 }
413
414 static void
415 ncr5380_minphys(struct buf *bp)
416 {
417 if (bp->b_bcount > MIN_PHYS)
418 bp->b_bcount = MIN_PHYS;
419 minphys(bp);
420 }
421 #undef MIN_PHYS
422
423 static int
424 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
425 {
426 u_char *b = (u_char *) xs->cmd;
427 int i = 0;
428
429 if (!(xs->flags & SCSI_RESET)) {
430 printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
431 xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
432 while (i < xs->cmdlen) {
433 if (i)
434 printf(",");
435 printf("%x",b[i++]);
436 }
437 printf("-\n");
438 }
439 else {
440 printf("(%d:%d:%d)-RESET-\n",
441 xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
442 }
443 }
444
445 /*
446 * The body of the driver.
447 */
448 static void
449 scsi_main(sc)
450 struct ncr_softc *sc;
451 {
452 SC_REQ *req, *prev;
453 int itype;
454 int sps;
455
456 /*
457 * While running in the driver SCSI-interrupts are disabled.
458 */
459 scsi_idisable();
460 ENABLE_NCR5380(sc);
461
462 PID("scsi_main1");
463 for (;;) {
464 sps = splbio();
465 if (!connected) {
466 /*
467 * Check if it is fair keep any exclusive access to DMA
468 * claimed. If not, stop queueing new jobs so the discon_q
469 * will be eventually drained and DMA can be given up.
470 */
471 if (!fair_to_keep_dma())
472 goto main_exit;
473
474 /*
475 * Search through the issue-queue for a command
476 * destined for a target that isn't busy.
477 */
478 prev = NULL;
479 for (req=issue_q; req != NULL; prev = req, req = req->next) {
480 if (!(busy & (1 << req->targ_id))) {
481 /*
482 * Found one, remove it from the issue queue
483 */
484 if (prev == NULL)
485 issue_q = req->next;
486 else prev->next = req->next;
487 req->next = NULL;
488 break;
489 }
490 }
491
492 /*
493 * When a request has just ended, we get here before an other
494 * device detects that the bus is free and that it can
495 * reconnect. The problem is that when this happens, we always
496 * baffle the device because our (initiator) id is higher. This
497 * can cause a sort of starvation on slow devices. So we check
498 * for a pending reselection here.
499 * Note that 'connected' will be non-null if the reselection
500 * succeeds.
501 */
502 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
503 == (SC_S_SEL|SC_S_IO)){
504 if (req != NULL) {
505 req->next = issue_q;
506 issue_q = req;
507 }
508 splx(sps);
509
510 reselect(sc);
511 scsi_clr_ipend();
512 goto connected;
513 }
514
515 /*
516 * The host is not connected and there is no request
517 * pending, exit.
518 */
519 if (req == NULL) {
520 PID("scsi_main2");
521 goto main_exit;
522 }
523
524 /*
525 * Re-enable interrupts before handling the request.
526 */
527 splx(sps);
528
529 #ifdef DBG_REQ
530 if (dbg_target_mask & (1 << req->targ_id))
531 show_request(req, "TARGET");
532 #endif
533 /*
534 * We found a request. Try to connect to the target. If the
535 * initiator fails arbitration, the command is put back in the
536 * issue queue.
537 */
538 if (scsi_select(req, 0)) {
539 sps = splbio();
540 req->next = issue_q;
541 issue_q = req;
542 splx(sps);
543 #ifdef DBG_REQ
544 if (dbg_target_mask & (1 << req->targ_id))
545 ncr_tprint(req, "Select failed\n");
546 #endif
547 }
548 }
549 else splx(sps);
550 connected:
551 if (connected) {
552 /*
553 * If the host is currently connected but a 'real-dma' transfer
554 * is in progress, the 'end-of-dma' interrupt restarts main.
555 * So quit.
556 */
557 sps = splbio();
558 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
559 PID("scsi_main3");
560 goto main_exit;
561 }
562 splx(sps);
563
564 /*
565 * Let the target guide us through the bus-phases
566 */
567 while (information_transfer() == -1)
568 ;
569 }
570 }
571 /* NEVER TO REACH HERE */
572 panic("ncr5380-SCSI: not designed to come here");
573
574 main_exit:
575 /*
576 * We enter here with interrupts disabled. We are about to exit main
577 * so interrupts should be re-enabled. Because interrupts are edge
578 * triggered, we could already have missed the interrupt. Therefore
579 * we check the IRQ-line here and re-enter when we really missed a
580 * valid interrupt.
581 */
582 PID("scsi_main4");
583 scsi_ienable();
584 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
585 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
586 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
587 scsi_idisable();
588 splx(sps);
589
590 if (itype == INTR_RESEL)
591 reselect(sc);
592 #ifdef REAL_DMA
593 else dma_ready();
594 #else
595 else {
596 if (pdma_ready())
597 goto connected;
598 panic("Got DMA interrupt without DMA");
599 }
600 #endif
601 scsi_clr_ipend();
602 goto connected;
603 }
604 }
605 reconsider_dma();
606
607 main_running = 0;
608 splx(sps);
609 PID("scsi_main5");
610 }
611
612 #ifdef REAL_DMA
613 /*
614 * The SCSI-DMA interrupt.
615 * This interrupt can only be triggered when running in non-polled DMA
616 * mode. When DMA is not active, it will be silently ignored, it is usually
617 * to late because the EOP interrupt of the controller happens just a tiny
618 * bit earlier. It might become usefull when scatter/gather is implemented,
619 * because in that case only part of the DATAIN/DATAOUT transfer is taken
620 * out of a single buffer.
621 */
622 static void
623 ncr_dma_intr(sc)
624 struct ncr_softc *sc;
625 {
626 SC_REQ *reqp;
627 int dma_done;
628
629 PID("ncr_dma_intr");
630 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
631 scsi_idisable();
632 if (!(dma_done = dma_ready())) {
633 transfer_dma(reqp, reqp->phase, 0);
634 return;
635 }
636 run_main(sc);
637 }
638 }
639 #endif /* REAL_DMA */
640
641 /*
642 * The SCSI-controller interrupt. This interrupt occurs on reselections and
643 * at the end of non-polled DMA-interrupts. It is assumed to be called from
644 * the machine-dependent hardware interrupt.
645 */
646 static void
647 ncr_ctrl_intr(sc)
648 struct ncr_softc *sc;
649 {
650 int itype;
651 int dma_done;
652
653 while (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
654 scsi_idisable();
655 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
656 if (itype == INTR_RESEL)
657 reselect(sc);
658 else {
659 #ifdef REAL_DMA
660 if (!(dma_done = dma_ready())) {
661 transfer_dma(connected, connected->phase, 0);
662 return;
663 }
664 #else
665 if (pdma_ready())
666 return;
667 panic("Got DMA interrupt without DMA\n");
668 #endif
669 }
670 scsi_clr_ipend();
671 }
672 run_main(sc);
673 return;
674 }
675 PID("ncr_ctrl_intr1");
676 }
677
678 /*
679 * Initiate a connection path between the host and the target. The function
680 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
681 * is selected and an 'IDENTIFY' message is send.
682 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
683 * the target does not respond (to either selection or 'MESSAGE OUT') the
684 * 'done' function is executed.
685 * The result code given by the driver can be influenced by setting 'code'
686 * to a non-zero value. This is the case when 'select' is called by abort.
687 */
688 static int
689 scsi_select(reqp, code)
690 SC_REQ *reqp;
691 {
692 u_long timeout;
693 u_char tmp[1];
694 u_char phase;
695 u_long cnt;
696 int sps;
697 u_int8_t atn_flag;
698 u_int8_t targ_bit;
699 struct ncr_softc *sc;
700
701 sc = reqp->xs->sc_link->adapter_softc;
702 DBG_SELPRINT ("Starting arbitration\n", 0);
703 PID("scsi_select1");
704
705 sps = splbio();
706
707 /*
708 * Prevent a race condition here. If a reslection interrupt occurred
709 * between the decision to pick a new request and the call to select,
710 * we abort the selection.
711 * Interrupts are lowered when the 5380 is setup to arbitrate for the
712 * bus.
713 */
714 if (connected) {
715 splx(sps);
716 PID("scsi_select2");
717 return (-1);
718 }
719
720 /*
721 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
722 * selection.
723 */
724 SET_5380_REG(NCR5380_TCOM, 0);
725 SET_5380_REG(NCR5380_ICOM, 0);
726
727 /*
728 * Arbitrate for the bus.
729 */
730 SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
731 SET_5380_REG(NCR5380_MODE, SC_ARBIT);
732
733 splx(sps);
734
735 cnt = 10;
736 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
737 delay(1);
738
739 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
740 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
741 SET_5380_REG(NCR5380_ICOM, 0);
742 DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
743 PID("scsi_select3");
744 return (-1);
745 }
746
747 /* The arbitration delay is 2.2 usecs */
748 delay(3);
749
750 /*
751 * Check the result of the arbitration. If we failed, return -1.
752 */
753 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
754 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
755 SET_5380_REG(NCR5380_ICOM, 0);
756 PID("scsi_select4");
757 return (-1);
758 }
759
760 /*
761 * The spec requires that we should read the data register to
762 * check for higher id's and check the SC_LA again.
763 */
764 tmp[0] = GET_5380_REG(NCR5380_DATA);
765 if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
766 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
767 SET_5380_REG(NCR5380_ICOM, 0);
768 DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
769 PID("scsi_select5");
770 return (-1);
771 }
772 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
773 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
774 SET_5380_REG(NCR5380_ICOM, 0);
775 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
776 PID("scsi_select6");
777 return (-1);
778 }
779 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
780 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
781 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
782 SET_5380_REG(NCR5380_ICOM, 0);
783 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
784 PID("scsi_select7");
785 return (-1);
786 }
787 /* Bus settle delay + Bus clear delay = 1.2 usecs */
788 delay(2);
789 DBG_SELPRINT ("Arbitration complete\n", 0);
790
791 /*
792 * Now that we won the arbitration, start the selection.
793 */
794 targ_bit = 1 << reqp->targ_id;
795 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
796
797 if (sc->sc_noselatn & targ_bit)
798 atn_flag = 0;
799 else
800 atn_flag = SC_A_ATN;
801
802 /*
803 * Raise ATN while SEL is true before BSY goes false from arbitration,
804 * since this is the only way to guarantee that we'll get a MESSAGE OUT
805 * phase immediately after the selection.
806 */
807 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
808 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
809
810 /*
811 * Turn off reselection interrupts
812 */
813 SET_5380_REG(NCR5380_IDSTAT, 0);
814
815 /*
816 * Reset BSY. The delay following it, surpresses a glitch in the
817 * 5380 which causes us to see our own BSY signal instead of that of
818 * the target.
819 */
820 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
821 delay(1);
822
823 /*
824 * Wait for the target to react, the specs call for a timeout of
825 * 250 ms.
826 */
827 cnt = 25000;
828 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
829 delay(10);
830
831 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
832 /*
833 * There is no reaction from the target, start the selection
834 * timeout procedure. We release the databus but keep SEL
835 * asserted. After that we wait a 'selection abort time' (200
836 * usecs) and 2 deskew delays (90 ns) and check BSY again.
837 * When BSY is asserted, we assume the selection succeeded,
838 * otherwise we release the bus.
839 */
840 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
841 delay(201);
842 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
843 SET_5380_REG(NCR5380_ICOM, 0);
844 reqp->xs->error = code ? code : XS_SELTIMEOUT;
845 DBG_SELPRINT ("Target %d not responding to sel\n",
846 reqp->targ_id);
847 finish_req(reqp);
848 PID("scsi_select8");
849 return (0);
850 }
851 }
852 SET_5380_REG(NCR5380_ICOM, atn_flag);
853
854 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
855
856 /*
857 * The SCSI-interrupts are disabled while a request is being handled.
858 */
859 scsi_idisable();
860
861 /*
862 * If we did not request ATN, then don't try to send IDENTIFY.
863 */
864 if (atn_flag == 0) {
865 reqp->phase = PH_CMD;
866 goto identify_failed;
867 }
868
869 /*
870 * Here we prepare to send an 'IDENTIFY' message.
871 * Allow disconnect only when interrups are allowed.
872 */
873 tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
874 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
875 cnt = 1;
876 phase = PH_MSGOUT;
877
878 /*
879 * Since we followed the SCSI-spec and raised ATN while SEL was true
880 * but before BSY was false during the selection, a 'MESSAGE OUT'
881 * phase should follow. Unfortunately, this does not happen on
882 * all targets (Asante ethernet devices, for example), so we must
883 * check the actual mode if the message transfer fails--if the
884 * new phase is PH_CMD and has never been successfully selected
885 * w/ATN in the past, then we assume that it is an old device
886 * that doesn't support select w/ATN.
887 */
888 if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
889
890 if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
891 DBG_SELPRINT ("Target %d: not responding to ATN.\n",
892 reqp->targ_id);
893 sc->sc_noselatn |= targ_bit;
894 reqp->phase = PH_CMD;
895 goto identify_failed;
896 }
897
898 DBG_SELPRINT ("Target %d: failed to send identify\n",
899 reqp->targ_id);
900 /*
901 * Try to disconnect from the target. We cannot leave
902 * it just hanging here.
903 */
904 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
905 u_long len = 1;
906 u_char phase = PH_MSGOUT;
907 u_char msg = MSG_ABORT;
908
909 transfer_pio(&phase, &msg, &len, 0);
910 }
911 else scsi_reset(sc);
912
913 SET_5380_REG(NCR5380_ICOM, 0);
914 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
915 finish_req(reqp);
916 PID("scsi_select9");
917 return (0);
918 }
919 reqp->phase = PH_MSGOUT;
920
921 identify_failed:
922 sc->sc_selected |= targ_bit;
923
924 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
925 /*
926 * Command is connected, start timer ticking.
927 */
928 ccb_p->xtimeout = ccb_p->timeout + Lbolt;
929 #endif
930
931 connected = reqp;
932 busy |= targ_bit;
933 PID("scsi_select10");
934 return (0);
935 }
936
937 /*
938 * Return codes:
939 * -1: quit main, trigger on interrupt
940 * 0: keep on running main.
941 */
942 static int
943 information_transfer()
944 {
945 SC_REQ *reqp = connected;
946 u_char tmp, phase;
947 u_long len;
948
949 PID("info_transf1");
950 /*
951 * Clear pending interrupts from 5380-chip.
952 */
953 scsi_clr_ipend();
954
955 /*
956 * We only have a valid SCSI-phase when REQ is asserted. Something
957 * is deadly wrong when BSY has dropped.
958 */
959 tmp = GET_5380_REG(NCR5380_IDSTAT);
960
961 if (!(tmp & SC_S_BSY)) {
962 busy &= ~(1 << reqp->targ_id);
963 connected = NULL;
964 reqp->xs->error = XS_BUSY;
965 finish_req(reqp);
966 PID("info_transf2");
967 return (0);
968 }
969
970 if (tmp & SC_S_REQ) {
971 phase = (tmp >> 2) & 7;
972 if (phase != reqp->phase) {
973 reqp->phase = phase;
974 DBG_INFPRINT(show_phase, reqp, phase);
975 }
976 }
977 else return (-1);
978
979 switch (phase) {
980 case PH_DATAOUT:
981 #ifdef DBG_NOWRITE
982 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
983 reqp->msgout = MSG_ABORT;
984 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
985 return (-1);
986 #endif /* DBG_NOWRITE */
987 /*
988 * If this is the first write using DMA, fill
989 * the bounce buffer.
990 */
991 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
992 if (reqp->dr_flag & DRIVER_BOUNCING)
993 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
994 }
995
996 case PH_DATAIN:
997 #ifdef REAL_DMA
998 if (reqp->dr_flag & DRIVER_DMAOK) {
999 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1000 transfer_dma(reqp, phase, poll);
1001 if (!poll)
1002 return (0);
1003 }
1004 else
1005 #endif
1006 {
1007 PID("info_transf3");
1008 len = reqp->xdata_len;
1009 #ifdef USE_PDMA
1010 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1011 return (0);
1012 #else
1013 transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1014 #endif
1015 reqp->xdata_ptr += reqp->xdata_len - len;
1016 reqp->xdata_len = len;
1017 }
1018 return (-1);
1019 case PH_MSGIN:
1020 /*
1021 * We only expect single byte messages here.
1022 */
1023 len = 1;
1024 transfer_pio(&phase, &tmp, &len, 1);
1025 reqp->message = tmp;
1026 return (handle_message(reqp, tmp));
1027 case PH_MSGOUT:
1028 len = 1;
1029 transfer_pio(&phase, &reqp->msgout, &len, 0);
1030 if (reqp->msgout == MSG_ABORT) {
1031 busy &= ~(1 << reqp->targ_id);
1032 connected = NULL;
1033 reqp->xs->error = XS_DRIVER_STUFFUP;
1034 finish_req(reqp);
1035 PID("info_transf4");
1036 return (0);
1037 }
1038 reqp->msgout = MSG_NOOP;
1039 return (-1);
1040 case PH_CMD :
1041 len = command_size(reqp->xcmd.opcode);
1042 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1043 PID("info_transf5");
1044 return (-1);
1045 case PH_STATUS:
1046 len = 1;
1047 transfer_pio(&phase, &tmp, &len, 0);
1048 reqp->status = tmp;
1049 PID("info_transf6");
1050 return (-1);
1051 default :
1052 ncr_tprint(reqp, "Unknown phase\n");
1053 }
1054 PID("info_transf7");
1055 return (-1);
1056 }
1057
1058 /*
1059 * Handle the message 'msg' send to us by the target.
1060 * Return values:
1061 * 0 : The current command has completed.
1062 * -1 : Get on to the next phase.
1063 */
1064 static int
1065 handle_message(reqp, msg)
1066 SC_REQ *reqp;
1067 u_int msg;
1068 {
1069 int sps;
1070 SC_REQ *prev, *req;
1071
1072 PID("hmessage1");
1073 switch (msg) {
1074 /*
1075 * Linking lets us reduce the time required to get
1076 * the next command to the device, skipping the arbitration
1077 * and selection time. In the current implementation,
1078 * we merely have to start the next command pointed
1079 * to by 'next_link'.
1080 */
1081 case MSG_LINK_CMD_COMPLETE:
1082 case MSG_LINK_CMD_COMPLETEF:
1083 if (reqp->link == NULL) {
1084 ncr_tprint(reqp, "No link for linked command");
1085 nack_message(reqp, MSG_ABORT);
1086 PID("hmessage2");
1087 return (-1);
1088 }
1089 ack_message();
1090 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1091 reqp->xs->resid = reqp->xdata_len;
1092 reqp->xs->error = 0;
1093 }
1094
1095 #ifdef AUTO_SENSE
1096 if (check_autosense(reqp, 1) == -1)
1097 return (-1);
1098 #endif /* AUTO_SENSE */
1099
1100 #ifdef DBG_REQ
1101 if (dbg_target_mask & (1 << reqp->targ_id))
1102 show_request(reqp->link, "LINK");
1103 #endif
1104 connected = reqp->link;
1105
1106 /*
1107 * Unlink the 'linked' request from the issue_q
1108 */
1109 sps = splbio();
1110 prev = NULL;
1111 req = issue_q;
1112 for (; req != NULL; prev = req, req = req->next) {
1113 if (req == connected)
1114 break;
1115 }
1116 if (req == NULL)
1117 panic("Inconsistent issue_q");
1118 if (prev == NULL)
1119 issue_q = req->next;
1120 else prev->next = req->next;
1121 req->next = NULL;
1122 splx(sps);
1123
1124 finish_req(reqp);
1125 PID("hmessage3");
1126 return (-1);
1127 case MSG_ABORT:
1128 case MSG_CMDCOMPLETE:
1129 ack_message();
1130 connected = NULL;
1131 busy &= ~(1 << reqp->targ_id);
1132 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1133 reqp->xs->resid = reqp->xdata_len;
1134 reqp->xs->error = 0;
1135 }
1136
1137 #ifdef AUTO_SENSE
1138 if (check_autosense(reqp, 0) == -1) {
1139 PID("hmessage4");
1140 return (0);
1141 }
1142 #endif /* AUTO_SENSE */
1143
1144 finish_req(reqp);
1145 PID("hmessage5");
1146 return (0);
1147 case MSG_MESSAGE_REJECT:
1148 ack_message();
1149 PID("hmessage6");
1150 return (-1);
1151 case MSG_DISCONNECT:
1152 ack_message();
1153 #ifdef DBG_REQ
1154 if (dbg_target_mask & (1 << reqp->targ_id))
1155 show_request(reqp, "DISCON");
1156 #endif
1157 sps = splbio();
1158 connected = NULL;
1159 reqp->next = discon_q;
1160 discon_q = reqp;
1161 splx(sps);
1162 PID("hmessage7");
1163 return (0);
1164 case MSG_SAVEDATAPOINTER:
1165 case MSG_RESTOREPOINTERS:
1166 /*
1167 * We save pointers implicitely at disconnect.
1168 * So we can ignore these messages.
1169 */
1170 ack_message();
1171 PID("hmessage8");
1172 return (-1);
1173 case MSG_EXTENDED:
1174 nack_message(reqp, MSG_MESSAGE_REJECT);
1175 PID("hmessage9");
1176 return (-1);
1177 default:
1178 ncr_tprint(reqp, "Unknown message %x\n", msg);
1179 return (-1);
1180 }
1181 PID("hmessage10");
1182 return (-1);
1183 }
1184
1185 /*
1186 * Handle reselection. If a valid reconnection occurs, connected
1187 * points at the reconnected command. The command is removed from the
1188 * disconnected queue.
1189 */
1190 static void
1191 reselect(sc)
1192 struct ncr_softc *sc;
1193 {
1194 u_char phase;
1195 u_long len;
1196 u_char msg;
1197 u_char target_mask;
1198 int abort = 0;
1199 SC_REQ *tmp, *prev;
1200
1201 PID("reselect1");
1202 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1203
1204 /*
1205 * At this point, we have detected that our SCSI-id is on the bus,
1206 * SEL is true and BSY was false for at least one bus settle
1207 * delay (400 ns.).
1208 * We must assert BSY ourselves, until the target drops the SEL signal.
1209 * The SCSI-spec specifies no maximum time for this, so we have to
1210 * choose something long enough to suit all targets.
1211 */
1212 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1213 len = 250000;
1214 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1215 delay(1);
1216 len--;
1217 }
1218 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1219 /* Damn SEL isn't dropping */
1220 scsi_reset(sc);
1221 return;
1222 }
1223
1224 SET_5380_REG(NCR5380_ICOM, 0);
1225
1226 /*
1227 * Check if the reselection is still valid. Check twice because
1228 * of possible line glitches - cheaper than delay(1) and we need
1229 * only a few nanoseconds.
1230 */
1231 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1232 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1233 ncr_aprint(sc, "Stepped into the reselection timeout\n");
1234 return;
1235 }
1236 }
1237
1238 /*
1239 * Get the expected identify message.
1240 */
1241 phase = PH_MSGIN;
1242 len = 1;
1243 transfer_pio(&phase, &msg, &len, 0);
1244 if (len || !MSG_ISIDENTIFY(msg)) {
1245 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1246 abort = 1;
1247 }
1248 else {
1249 /*
1250 * Find the command reconnecting
1251 */
1252 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1253 if (target_mask == (1 << tmp->targ_id)) {
1254 if (prev)
1255 prev->next = tmp->next;
1256 else discon_q = tmp->next;
1257 tmp->next = NULL;
1258 break;
1259 }
1260 }
1261 if (tmp == NULL) {
1262 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1263 target_mask);
1264 abort = 1;
1265 }
1266 }
1267 if (abort) {
1268 msg = MSG_ABORT;
1269 len = 1;
1270 phase = PH_MSGOUT;
1271
1272 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1273 transfer_pio(&phase, &msg, &len, 0);
1274 }
1275 else {
1276 connected = tmp;
1277 #ifdef DBG_REQ
1278 if (dbg_target_mask & (1 << tmp->targ_id))
1279 show_request(tmp, "RECON");
1280 #endif
1281 }
1282 PID("reselect2");
1283 }
1284
1285 /*
1286 * Transfer data in a given phase using programmed I/O.
1287 * Returns -1 when a different phase is entered without transferring the
1288 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1289 * phase.
1290 */
1291 static int
1292 transfer_pio(phase, data, len, dont_drop_ack)
1293 u_char *phase;
1294 u_char *data;
1295 u_long *len;
1296 int dont_drop_ack;
1297 {
1298 u_int cnt = *len;
1299 u_char ph = *phase;
1300 u_char tmp, new_icom;
1301
1302 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1303 PID("tpio1");
1304 SET_5380_REG(NCR5380_TCOM, ph);
1305 do {
1306 if (!wait_req_true()) {
1307 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1308 break;
1309 }
1310 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1311 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1312 break;
1313 }
1314 if (PH_IN(ph)) {
1315 *data++ = GET_5380_REG(NCR5380_DATA);
1316 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1317 if ((cnt == 1) && dont_drop_ack)
1318 new_icom = SC_A_ACK;
1319 else new_icom = 0;
1320 }
1321 else {
1322 SET_5380_REG(NCR5380_DATA, *data++);
1323
1324 /*
1325 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1326 * the initiator should drop ATN on the last byte of the
1327 * message phase after REQ has been asserted for the handshake
1328 * but before the initiator raises ACK.
1329 */
1330 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1331 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1332 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1333 new_icom = 0;
1334 }
1335 else {
1336 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1337 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1338 new_icom = SC_A_ATN;
1339 }
1340 }
1341 if (!wait_req_false()) {
1342 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1343 break;
1344 }
1345 SET_5380_REG(NCR5380_ICOM, new_icom);
1346
1347 } while (--cnt);
1348
1349 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1350 *phase = (tmp >> 2) & 7;
1351 else *phase = NR_PHASE;
1352 *len = cnt;
1353 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1354 *phase, cnt);
1355 PID("tpio2");
1356 if (!cnt || (*phase == ph))
1357 return (0);
1358 return (-1);
1359 }
1360
1361 #ifdef REAL_DMA
1362 /*
1363 * Start a DMA-transfer on the device using the current pointers.
1364 * If 'poll' is true, the function busy-waits until DMA has completed.
1365 */
1366 static void
1367 transfer_dma(reqp, phase, poll)
1368 SC_REQ *reqp;
1369 u_int phase;
1370 int poll;
1371 {
1372 int dma_done;
1373 u_char mbase = 0;
1374 int sps;
1375
1376 again:
1377 PID("tdma1");
1378
1379 /*
1380 * We should be in phase, otherwise we are not allowed to
1381 * drive the bus.
1382 */
1383 SET_5380_REG(NCR5380_TCOM, phase);
1384
1385 /*
1386 * Defer interrupts until DMA is fully running.
1387 */
1388 sps = splbio();
1389
1390 /*
1391 * Clear pending interrupts and parity errors.
1392 */
1393 scsi_clr_ipend();
1394
1395 if (!poll) {
1396 /*
1397 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1398 * to the interrupts we want enabled.
1399 */
1400 scsi_ienable();
1401 reqp->dr_flag |= DRIVER_IN_DMA;
1402 mbase = SC_E_EOPI | SC_MON_BSY;
1403 }
1404 else scsi_idisable();
1405 mbase |= IMODE_BASE | SC_M_DMA;
1406 scsi_dma_setup(reqp, phase, mbase);
1407
1408 splx(sps);
1409
1410 if (poll) {
1411 /*
1412 * On polled-dma transfers, we wait here until the
1413 * 'end-of-dma' condition occurs.
1414 */
1415 poll_edma(reqp);
1416 if (!(dma_done = dma_ready()))
1417 goto again;
1418 }
1419 PID("tdma2");
1420 }
1421
1422 /*
1423 * Check results of a DMA data-transfer.
1424 */
1425 static int
1426 dma_ready()
1427 {
1428 SC_REQ *reqp = connected;
1429 int dmstat, is_edma;
1430 long bytes_left, bytes_done;
1431
1432 is_edma = get_dma_result(reqp, &bytes_left);
1433 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1434
1435 /*
1436 * Check if the call is sensible and not caused by any spurious
1437 * interrupt.
1438 */
1439 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1440 && (dmstat & SC_PHS_MTCH) ) {
1441 ncr_tprint(reqp, "dma_ready: spurious call"
1442 "(dm:%x,last_hit: %s)\n",
1443 #ifdef DBG_PID
1444 dmstat, last_hit);
1445 #else
1446 dmstat, "unknown");
1447 #endif
1448 return (0);
1449 }
1450
1451 /*
1452 * Clear all (pending) interrupts.
1453 */
1454 scsi_clr_ipend();
1455
1456 /*
1457 * Update various transfer-pointers/lengths
1458 */
1459 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1460
1461 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1462 /*
1463 * Copy the bytes read until now from the bounce buffer
1464 * to the 'real' destination. Flush the data-cache
1465 * before copying.
1466 */
1467 PCIA();
1468 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1469 reqp->bouncerp += bytes_done;
1470 }
1471
1472 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1473 reqp->xdata_len -= bytes_done; /* XXX */
1474 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1475 reqp->dm_cur++;
1476 else reqp->dm_cur->dm_addr += bytes_done;
1477
1478 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1479 if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
1480 /* XXX: Should be parity error ???? */
1481 reqp->xs->error = XS_DRIVER_STUFFUP;
1482 }
1483
1484 /*
1485 * DMA mode should always be reset even when we will continue with the
1486 * next chain. It is also essential to clear the MON_BUSY because
1487 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1488 * the bus....
1489 */
1490 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1491
1492
1493 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1494 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1495
1496 /*
1497 * Tell interrupt functions DMA mode has ended.
1498 */
1499 reqp->dr_flag &= ~DRIVER_IN_DMA;
1500
1501 /*
1502 * Clear mode and icom
1503 */
1504 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1505 SET_5380_REG(NCR5380_ICOM, 0);
1506
1507 if (dmstat & SC_BSY_ERR) {
1508 if (!reqp->xs->error)
1509 reqp->xs->error = XS_BUSY;
1510 finish_req(reqp);
1511 PID("dma_ready1");
1512 return (1);
1513 }
1514
1515 if (reqp->xs->error != 0) {
1516 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1517 reqp->msgout = MSG_ABORT;
1518 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1519 }
1520 PID("dma_ready2");
1521 return (1);
1522 }
1523 return (0);
1524 }
1525 #endif /* REAL_DMA */
1526
1527 static int
1528 check_autosense(reqp, linked)
1529 SC_REQ *reqp;
1530 int linked;
1531 {
1532 int sps;
1533
1534 /*
1535 * If we not executing an auto-sense and the status code
1536 * is request-sense, we automatically issue a request
1537 * sense command.
1538 */
1539 PID("cautos1");
1540 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1541 switch (reqp->status & SCSMASK) {
1542 case SCSCHKC:
1543 bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
1544 reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
1545 reqp->xdata_len = sizeof(reqp->xs->sense);
1546 reqp->dr_flag |= DRIVER_AUTOSEN;
1547 reqp->dr_flag &= ~DRIVER_DMAOK;
1548 if (!linked) {
1549 sps = splbio();
1550 reqp->next = issue_q;
1551 issue_q = reqp;
1552 splx(sps);
1553 }
1554 else reqp->xcmd.bytes[4] |= 1;
1555
1556 #ifdef DBG_REQ
1557 bzero(reqp->xdata_ptr, reqp->xdata_len);
1558 if (dbg_target_mask & (1 << reqp->targ_id))
1559 show_request(reqp, "AUTO-SENSE");
1560 #endif
1561 PID("cautos2");
1562 return (-1);
1563 case SCSBUSY:
1564 reqp->xs->error = XS_BUSY;
1565 return (0);
1566 }
1567 }
1568 else {
1569 /*
1570 * An auto-sense has finished
1571 */
1572 if ((reqp->status & SCSMASK) != SCSGOOD)
1573 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1574 else reqp->xs->error = XS_SENSE;
1575 reqp->status = SCSCHKC;
1576 }
1577 PID("cautos3");
1578 return (0);
1579 }
1580
1581 static int
1582 reach_msg_out(sc, len)
1583 struct ncr_softc *sc;
1584 u_long len;
1585 {
1586 u_char phase;
1587 u_char data;
1588
1589 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1590 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1591 phase = (phase >> 2) & 7;
1592 else return (-1);
1593 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1594 if (phase == PH_MSGOUT)
1595 return (0);
1596
1597 SET_5380_REG(NCR5380_TCOM, phase);
1598
1599 do {
1600 if (!wait_req_true())
1601 break;
1602 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1603 break;
1604 if (PH_IN(phase)) {
1605 data = GET_5380_REG(NCR5380_DATA);
1606 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1607 }
1608 else {
1609 SET_5380_REG(NCR5380_DATA, 0);
1610 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1611 }
1612 if (!wait_req_false())
1613 break;
1614 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1615 } while (--len);
1616
1617 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1618 phase = (phase >> 2) & 7;
1619 if (phase == PH_MSGOUT) {
1620 ncr_aprint(sc, "Message-out phase reached.\n");
1621 return (0);
1622 }
1623 }
1624 return (-1);
1625 }
1626
1627 static void
1628 scsi_reset(sc)
1629 struct ncr_softc *sc;
1630 {
1631 SC_REQ *tmp, *next;
1632 int sps;
1633
1634 ncr_aprint(sc, "Resetting SCSI-bus\n");
1635
1636 PID("scsi_reset1");
1637 sps = splbio();
1638 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1639 delay(1);
1640 SET_5380_REG(NCR5380_ICOM, 0);
1641
1642 /*
1643 * None of the jobs in the discon_q will ever be reconnected,
1644 * notify this to the higher level code.
1645 */
1646 for (tmp = discon_q; tmp ;) {
1647 next = tmp->next;
1648 tmp->next = NULL;
1649 tmp->xs->error = XS_TIMEOUT;
1650 busy &= ~(1 << tmp->targ_id);
1651 finish_req(tmp);
1652 tmp = next;
1653 }
1654 discon_q = NULL;
1655
1656 /*
1657 * The current job will never finish either.
1658 * The problem is that we can't finish the job because an instance
1659 * of main is running on it. Our best guess is that the job is currently
1660 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1661 * the job because it detects BSY-loss.
1662 */
1663 if (tmp = connected) {
1664 if (tmp->dr_flag & DRIVER_IN_DMA) {
1665 tmp->xs->error = XS_DRIVER_STUFFUP;
1666 #ifdef REAL_DMA
1667 dma_ready();
1668 #endif
1669 }
1670 }
1671 splx(sps);
1672 PID("scsi_reset2");
1673 }
1674
1675 /*
1676 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1677 * the appropriate action is carried out (reselection or DMA ready) and
1678 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1679 * and INTR_SPURIOUS is returned.
1680 */
1681 static int
1682 check_intr(sc)
1683 struct ncr_softc *sc;
1684 {
1685 SC_REQ *reqp;
1686
1687 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1688 ==(SC_S_SEL|SC_S_IO))
1689 return (INTR_RESEL);
1690 else {
1691 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1692 reqp->dr_flag &= ~DRIVER_IN_DMA;
1693 return (INTR_DMA);
1694 }
1695 }
1696 scsi_clr_ipend();
1697 printf("-->");
1698 scsi_show();
1699 ncr_aprint(sc, "Spurious interrupt.\n");
1700 return (INTR_SPURIOUS);
1701 }
1702
1703 #ifdef REAL_DMA
1704 /*
1705 * Check if DMA can be used for this request. This function also builds
1706 * the dma-chain.
1707 */
1708 static int
1709 scsi_dmaok(reqp)
1710 SC_REQ *reqp;
1711 {
1712 u_long phy_buf;
1713 u_long phy_len;
1714 void *req_addr;
1715 u_long req_len;
1716 struct dma_chain *dm;
1717
1718 /*
1719 * Initialize locals and requests' DMA-chain.
1720 */
1721 req_len = reqp->xdata_len;
1722 req_addr = (void*)reqp->xdata_ptr;
1723 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1724 dm->dm_count = dm->dm_addr = 0;
1725 reqp->dr_flag &= ~DRIVER_BOUNCING;
1726
1727 /*
1728 * Do not accept zero length DMA.
1729 */
1730 if (req_len == 0)
1731 return (0);
1732
1733 /*
1734 * LWP: I think that this restriction is not strictly nessecary.
1735 */
1736 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1737 return (0);
1738
1739 /*
1740 * Build the DMA-chain.
1741 */
1742 dm->dm_addr = phy_buf = kvtop(req_addr);
1743 while (req_len) {
1744 if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1745 phy_len = req_len;
1746
1747 req_addr += phy_len;
1748 req_len -= phy_len;
1749 dm->dm_count += phy_len;
1750
1751 if (req_len) {
1752 u_long tmp = kvtop(req_addr);
1753
1754 if ((phy_buf + phy_len) != tmp) {
1755 if (wrong_dma_range(reqp, dm)) {
1756 if (reqp->dr_flag & DRIVER_BOUNCING)
1757 goto bounceit;
1758 return (0);
1759 }
1760
1761 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1762 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1763 return (0);
1764 }
1765 dm->dm_count = 0;
1766 dm->dm_addr = tmp;
1767 }
1768 phy_buf = tmp;
1769 }
1770 }
1771 if (wrong_dma_range(reqp, dm)) {
1772 if (reqp->dr_flag & DRIVER_BOUNCING)
1773 goto bounceit;
1774 return (0);
1775 }
1776 reqp->dm_last = dm;
1777 return (1);
1778
1779 bounceit:
1780 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1781 /*
1782 * If we can't get a bounce buffer, forget DMA
1783 */
1784 reqp->dr_flag &= ~DRIVER_BOUNCING;
1785 return(0);
1786 }
1787 /*
1788 * Initialize a single DMA-range containing the bounced request
1789 */
1790 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1791 dm->dm_addr = kvtop(reqp->bounceb);
1792 dm->dm_count = reqp->xdata_len;
1793 reqp->bouncerp = reqp->bounceb;
1794
1795 return (1);
1796 }
1797 #endif /* REAL_DMA */
1798
1799 static void
1800 run_main(sc)
1801 struct ncr_softc *sc;
1802 {
1803 int sps = splbio();
1804
1805 if (!main_running) {
1806 /*
1807 * If shared resources are required, claim them
1808 * before entering 'scsi_main'. If we can't get them
1809 * now, assume 'run_main' will be called when the resource
1810 * becomes available.
1811 */
1812 if (!claimed_dma()) {
1813 splx(sps);
1814 return;
1815 }
1816 main_running = 1;
1817 splx(sps);
1818 scsi_main(sc);
1819 }
1820 else splx(sps);
1821 }
1822
1823 /*
1824 * Prefix message with full target info.
1825 */
1826 static void
1827 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1828 {
1829 va_list ap;
1830
1831 va_start(ap, fmt);
1832 sc_print_addr(reqp->xs->sc_link);
1833 printf("%r", fmt, ap);
1834 va_end(ap);
1835 }
1836
1837 /*
1838 * Prefix message with adapter info.
1839 */
1840 static void
1841 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1842 {
1843 va_list ap;
1844
1845 va_start(ap, fmt);
1846 printf("%s : %r", sc->sc_dev.dv_xname, fmt, ap);
1847 va_end(ap);
1848 }
1849 /****************************************************************************
1850 * Start Debugging Functions *
1851 ****************************************************************************/
1852 static void
1853 show_data_sense(xs)
1854 struct scsi_xfer *xs;
1855 {
1856 u_char *p1, *p2;
1857 int i;
1858 int sz;
1859
1860 p1 = (u_char *) xs->cmd;
1861 p2 = (u_char *)&xs->sense;
1862 if(*p2 == 0)
1863 return; /* No(n)sense */
1864 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
1865 for (i = 0; i < sz; i++)
1866 printf("%x ", p1[i]);
1867 printf("\nsense: ");
1868 for (i = 0; i < sizeof(xs->sense); i++)
1869 printf("%x ", p2[i]);
1870 printf("\n");
1871 }
1872
1873 static void
1874 show_request(reqp, qtxt)
1875 SC_REQ *reqp;
1876 char *qtxt;
1877 {
1878 printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x resid=%d %s\n",
1879 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1880 reqp->xcmd.opcode, reqp->status, reqp->message,
1881 reqp->xs->error, reqp->xs->resid, reqp->link ? "L":"");
1882 if (reqp->status == SCSCHKC)
1883 show_data_sense(reqp->xs);
1884 }
1885
1886 static char *sig_names[] = {
1887 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1888 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1889 };
1890
1891 static void
1892 show_signals(dmstat, idstat)
1893 u_char dmstat, idstat;
1894 {
1895 u_short tmp, mask;
1896 int i, j, need_pipe;
1897
1898 tmp = idstat | ((dmstat & 3) << 8);
1899 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1900 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1901 if (tmp & mask)
1902 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1903 }
1904 printf("\nDma status (%02x): ", dmstat);
1905 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
1906 if (dmstat & mask)
1907 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1908 }
1909 printf("\n");
1910 }
1911
1912 scsi_show()
1913 {
1914 SC_REQ *tmp;
1915 int sps = splhigh();
1916 u_char idstat, dmstat;
1917
1918 #ifndef DBG_PID
1919 #define last_hit ""
1920 #define olast_hit ""
1921 #endif
1922
1923 for (tmp = issue_q; tmp; tmp = tmp->next)
1924 show_request(tmp, "ISSUED");
1925 for (tmp = discon_q; tmp; tmp = tmp->next)
1926 show_request(tmp, "DISCONNECTED");
1927 if (connected)
1928 show_request(connected, "CONNECTED");
1929 idstat = GET_5380_REG(NCR5380_IDSTAT);
1930 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1931 show_signals(dmstat, idstat);
1932 if (connected)
1933 printf("phase = %d, ", connected->phase);
1934 printf("busy:%x, last_hit:%s, olast_hit:%s spl:%04x\n", busy,
1935 last_hit, olast_hit, sps);
1936
1937 splx(sps);
1938 }
1939