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