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