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