ncr5380.c revision 1.19 1 /* $NetBSD: ncr5380.c,v 1.19 1996/04/26 06:50:16 leo 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 else {
1018 /*
1019 * Same data-phase. If same error give up
1020 */
1021 if ((reqp->msgout == MSG_ABORT)
1022 && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
1023 busy &= ~(1 << reqp->targ_id);
1024 connected = NULL;
1025 finish_req(reqp);
1026 scsi_reset_verbose(sc, "Failure to abort command");
1027 return (0);
1028 }
1029 }
1030
1031 switch (phase) {
1032 case PH_DATAOUT:
1033 #ifdef DBG_NOWRITE
1034 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1035 reqp->msgout = MSG_ABORT;
1036 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1037 return (-1);
1038 #endif /* DBG_NOWRITE */
1039 /*
1040 * If this is the first write using DMA, fill
1041 * the bounce buffer.
1042 */
1043 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1044 if (reqp->dr_flag & DRIVER_BOUNCING)
1045 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
1046 }
1047
1048 case PH_DATAIN:
1049 if (reqp->xdata_len <= 0) {
1050 /*
1051 * Target keeps requesting data. Try to get into
1052 * message-out phase by feeding/taking 100 byte.
1053 */
1054 ncr_tprint(reqp, "Target requests too much data\n");
1055 reqp->msgout = MSG_ABORT;
1056 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1057 reach_msg_out(sc, 100);
1058 return (-1);
1059 }
1060 #ifdef REAL_DMA
1061 if (reqp->dr_flag & DRIVER_DMAOK) {
1062 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1063 transfer_dma(reqp, phase, poll);
1064 if (!poll)
1065 return (0);
1066 }
1067 else
1068 #endif
1069 {
1070 PID("info_transf3");
1071 len = reqp->xdata_len;
1072 #ifdef USE_PDMA
1073 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1074 return (0);
1075 #else
1076 transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1077 #endif
1078 reqp->xdata_ptr += reqp->xdata_len - len;
1079 reqp->xdata_len = len;
1080 }
1081 return (-1);
1082 case PH_MSGIN:
1083 /*
1084 * We only expect single byte messages here.
1085 */
1086 len = 1;
1087 transfer_pio(&phase, &tmp, &len, 1);
1088 reqp->message = tmp;
1089 return (handle_message(reqp, tmp));
1090 case PH_MSGOUT:
1091 len = 1;
1092 transfer_pio(&phase, &reqp->msgout, &len, 0);
1093 if (reqp->msgout == MSG_ABORT) {
1094 busy &= ~(1 << reqp->targ_id);
1095 connected = NULL;
1096 if (!reqp->xs->error)
1097 reqp->xs->error = XS_DRIVER_STUFFUP;
1098 finish_req(reqp);
1099 PID("info_transf4");
1100 return (0);
1101 }
1102 reqp->msgout = MSG_NOOP;
1103 return (-1);
1104 case PH_CMD :
1105 len = command_size(reqp->xcmd.opcode);
1106 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1107 PID("info_transf5");
1108 return (-1);
1109 case PH_STATUS:
1110 len = 1;
1111 transfer_pio(&phase, &tmp, &len, 0);
1112 reqp->status = tmp;
1113 PID("info_transf6");
1114 return (-1);
1115 default :
1116 ncr_tprint(reqp, "Unknown phase\n");
1117 }
1118 PID("info_transf7");
1119 return (-1);
1120 }
1121
1122 /*
1123 * Handle the message 'msg' send to us by the target.
1124 * Return values:
1125 * 0 : The current command has completed.
1126 * -1 : Get on to the next phase.
1127 */
1128 static int
1129 handle_message(reqp, msg)
1130 SC_REQ *reqp;
1131 u_int msg;
1132 {
1133 int sps;
1134 SC_REQ *prev, *req;
1135
1136 PID("hmessage1");
1137 switch (msg) {
1138 /*
1139 * Linking lets us reduce the time required to get
1140 * the next command to the device, skipping the arbitration
1141 * and selection time. In the current implementation,
1142 * we merely have to start the next command pointed
1143 * to by 'next_link'.
1144 */
1145 case MSG_LINK_CMD_COMPLETE:
1146 case MSG_LINK_CMD_COMPLETEF:
1147 if (reqp->link == NULL) {
1148 ncr_tprint(reqp, "No link for linked command");
1149 nack_message(reqp, MSG_ABORT);
1150 PID("hmessage2");
1151 return (-1);
1152 }
1153 ack_message();
1154 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1155 reqp->xs->resid = reqp->xdata_len;
1156 reqp->xs->error = 0;
1157 }
1158
1159 #ifdef AUTO_SENSE
1160 if (check_autosense(reqp, 1) == -1)
1161 return (-1);
1162 #endif /* AUTO_SENSE */
1163
1164 #ifdef DBG_REQ
1165 if (dbg_target_mask & (1 << reqp->targ_id))
1166 show_request(reqp->link, "LINK");
1167 #endif
1168 connected = reqp->link;
1169
1170 /*
1171 * Unlink the 'linked' request from the issue_q
1172 */
1173 sps = splbio();
1174 prev = NULL;
1175 req = issue_q;
1176 for (; req != NULL; prev = req, req = req->next) {
1177 if (req == connected)
1178 break;
1179 }
1180 if (req == NULL)
1181 panic("Inconsistent issue_q");
1182 if (prev == NULL)
1183 issue_q = req->next;
1184 else prev->next = req->next;
1185 req->next = NULL;
1186 splx(sps);
1187
1188 finish_req(reqp);
1189 PID("hmessage3");
1190 return (-1);
1191 case MSG_ABORT:
1192 case MSG_CMDCOMPLETE:
1193 ack_message();
1194 connected = NULL;
1195 busy &= ~(1 << reqp->targ_id);
1196 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1197 reqp->xs->resid = reqp->xdata_len;
1198 reqp->xs->error = 0;
1199 }
1200
1201 #ifdef AUTO_SENSE
1202 if (check_autosense(reqp, 0) == -1) {
1203 PID("hmessage4");
1204 return (0);
1205 }
1206 #endif /* AUTO_SENSE */
1207
1208 finish_req(reqp);
1209 PID("hmessage5");
1210 return (0);
1211 case MSG_MESSAGE_REJECT:
1212 ack_message();
1213 PID("hmessage6");
1214 return (-1);
1215 case MSG_DISCONNECT:
1216 ack_message();
1217 #ifdef DBG_REQ
1218 if (dbg_target_mask & (1 << reqp->targ_id))
1219 show_request(reqp, "DISCON");
1220 #endif
1221 sps = splbio();
1222 connected = NULL;
1223 reqp->next = discon_q;
1224 discon_q = reqp;
1225 splx(sps);
1226 PID("hmessage7");
1227 return (0);
1228 case MSG_SAVEDATAPOINTER:
1229 case MSG_RESTOREPOINTERS:
1230 /*
1231 * We save pointers implicitely at disconnect.
1232 * So we can ignore these messages.
1233 */
1234 ack_message();
1235 PID("hmessage8");
1236 return (-1);
1237 case MSG_EXTENDED:
1238 nack_message(reqp, MSG_MESSAGE_REJECT);
1239 PID("hmessage9");
1240 return (-1);
1241 default:
1242 ncr_tprint(reqp, "Unknown message %x\n", msg);
1243 return (-1);
1244 }
1245 PID("hmessage10");
1246 return (-1);
1247 }
1248
1249 /*
1250 * Handle reselection. If a valid reconnection occurs, connected
1251 * points at the reconnected command. The command is removed from the
1252 * disconnected queue.
1253 */
1254 static void
1255 reselect(sc)
1256 struct ncr_softc *sc;
1257 {
1258 u_char phase;
1259 u_long len;
1260 u_char msg;
1261 u_char target_mask;
1262 int abort = 0;
1263 SC_REQ *tmp = NULL, *prev;
1264
1265 PID("reselect1");
1266 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1267
1268 /*
1269 * At this point, we have detected that our SCSI-id is on the bus,
1270 * SEL is true and BSY was false for at least one bus settle
1271 * delay (400 ns.).
1272 * We must assert BSY ourselves, until the target drops the SEL signal.
1273 * The SCSI-spec specifies no maximum time for this, so we have to
1274 * choose something long enough to suit all targets.
1275 */
1276 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1277 len = 250000;
1278 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1279 delay(1);
1280 len--;
1281 }
1282 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1283 /* Damn SEL isn't dropping */
1284 scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1285 return;
1286 }
1287
1288 SET_5380_REG(NCR5380_ICOM, 0);
1289
1290 /*
1291 * Check if the reselection is still valid. Check twice because
1292 * of possible line glitches - cheaper than delay(1) and we need
1293 * only a few nanoseconds.
1294 */
1295 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1296 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1297 ncr_aprint(sc, "Stepped into the reselection timeout\n");
1298 return;
1299 }
1300 }
1301
1302 /*
1303 * Get the expected identify message.
1304 */
1305 phase = PH_MSGIN;
1306 len = 1;
1307 transfer_pio(&phase, &msg, &len, 0);
1308 if (len || !MSG_ISIDENTIFY(msg)) {
1309 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1310 abort = 1;
1311 }
1312 else {
1313 /*
1314 * Find the command reconnecting
1315 */
1316 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1317 if (target_mask == (1 << tmp->targ_id)) {
1318 if (prev)
1319 prev->next = tmp->next;
1320 else discon_q = tmp->next;
1321 tmp->next = NULL;
1322 break;
1323 }
1324 }
1325 if (tmp == NULL) {
1326 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1327 target_mask);
1328 abort = 1;
1329 }
1330 }
1331 if (abort) {
1332 msg = MSG_ABORT;
1333 len = 1;
1334 phase = PH_MSGOUT;
1335
1336 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1337 if (transfer_pio(&phase, &msg, &len, 0) || len)
1338 scsi_reset_verbose(sc, "Failure to abort reselection");
1339 }
1340 else {
1341 connected = tmp;
1342 #ifdef DBG_REQ
1343 if (dbg_target_mask & (1 << tmp->targ_id))
1344 show_request(tmp, "RECON");
1345 #endif
1346 }
1347 PID("reselect2");
1348 }
1349
1350 /*
1351 * Transfer data in a given phase using programmed I/O.
1352 * Returns -1 when a different phase is entered without transferring the
1353 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1354 * phase.
1355 */
1356 static int
1357 transfer_pio(phase, data, len, dont_drop_ack)
1358 u_char *phase;
1359 u_char *data;
1360 u_long *len;
1361 int dont_drop_ack;
1362 {
1363 u_int cnt = *len;
1364 u_char ph = *phase;
1365 u_char tmp, new_icom;
1366
1367 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1368 PID("tpio1");
1369 SET_5380_REG(NCR5380_TCOM, ph);
1370 do {
1371 if (!wait_req_true()) {
1372 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1373 break;
1374 }
1375 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1376 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1377 break;
1378 }
1379 if (PH_IN(ph)) {
1380 *data++ = GET_5380_REG(NCR5380_DATA);
1381 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1382 if ((cnt == 1) && dont_drop_ack)
1383 new_icom = SC_A_ACK;
1384 else new_icom = 0;
1385 }
1386 else {
1387 SET_5380_REG(NCR5380_DATA, *data++);
1388
1389 /*
1390 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1391 * the initiator should drop ATN on the last byte of the
1392 * message phase after REQ has been asserted for the handshake
1393 * but before the initiator raises ACK.
1394 */
1395 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1396 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1397 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1398 new_icom = 0;
1399 }
1400 else {
1401 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1402 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1403 new_icom = SC_A_ATN;
1404 }
1405 }
1406 if (!wait_req_false()) {
1407 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1408 break;
1409 }
1410 SET_5380_REG(NCR5380_ICOM, new_icom);
1411
1412 } while (--cnt);
1413
1414 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1415 *phase = (tmp >> 2) & 7;
1416 else *phase = NR_PHASE;
1417 *len = cnt;
1418 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1419 *phase, cnt);
1420 PID("tpio2");
1421 if (!cnt || (*phase == ph))
1422 return (0);
1423 return (-1);
1424 }
1425
1426 #ifdef REAL_DMA
1427 /*
1428 * Start a DMA-transfer on the device using the current pointers.
1429 * If 'poll' is true, the function busy-waits until DMA has completed.
1430 */
1431 static void
1432 transfer_dma(reqp, phase, poll)
1433 SC_REQ *reqp;
1434 u_int phase;
1435 int poll;
1436 {
1437 int dma_done;
1438 u_char mbase = 0;
1439 int sps;
1440
1441 again:
1442 PID("tdma1");
1443
1444 /*
1445 * We should be in phase, otherwise we are not allowed to
1446 * drive the bus.
1447 */
1448 SET_5380_REG(NCR5380_TCOM, phase);
1449
1450 /*
1451 * Defer interrupts until DMA is fully running.
1452 */
1453 sps = splbio();
1454
1455 /*
1456 * Clear pending interrupts and parity errors.
1457 */
1458 scsi_clr_ipend();
1459
1460 if (!poll) {
1461 /*
1462 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1463 * to the interrupts we want enabled.
1464 */
1465 scsi_ienable();
1466 reqp->dr_flag |= DRIVER_IN_DMA;
1467 mbase = SC_E_EOPI | SC_MON_BSY;
1468 }
1469 else scsi_idisable();
1470 mbase |= IMODE_BASE | SC_M_DMA;
1471 scsi_dma_setup(reqp, phase, mbase);
1472
1473 splx(sps);
1474
1475 if (poll) {
1476 /*
1477 * On polled-dma transfers, we wait here until the
1478 * 'end-of-dma' condition occurs.
1479 */
1480 poll_edma(reqp);
1481 if (!(dma_done = dma_ready()))
1482 goto again;
1483 }
1484 PID("tdma2");
1485 }
1486
1487 /*
1488 * Check results of a DMA data-transfer.
1489 */
1490 static int
1491 dma_ready()
1492 {
1493 SC_REQ *reqp = connected;
1494 int dmstat, is_edma;
1495 long bytes_left, bytes_done;
1496
1497 is_edma = get_dma_result(reqp, &bytes_left);
1498 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1499
1500 /*
1501 * Check if the call is sensible and not caused by any spurious
1502 * interrupt.
1503 */
1504 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1505 && (dmstat & SC_PHS_MTCH) ) {
1506 ncr_tprint(reqp, "dma_ready: spurious call "
1507 "(dm:%x,last_hit: %s)\n",
1508 #ifdef DBG_PID
1509 dmstat, last_hit[DBG_PID-1]);
1510 #else
1511 dmstat, "unknown");
1512 #endif
1513 return (0);
1514 }
1515
1516 /*
1517 * Clear all (pending) interrupts.
1518 */
1519 scsi_clr_ipend();
1520
1521 /*
1522 * Update various transfer-pointers/lengths
1523 */
1524 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1525
1526 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1527 /*
1528 * Copy the bytes read until now from the bounce buffer
1529 * to the 'real' destination. Flush the data-cache
1530 * before copying.
1531 */
1532 PCIA();
1533 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1534 reqp->bouncerp += bytes_done;
1535 }
1536
1537 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1538 reqp->xdata_len -= bytes_done; /* XXX */
1539 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1540 reqp->dm_cur++;
1541 else reqp->dm_cur->dm_addr += bytes_done;
1542
1543 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1544 if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1545 ncr_tprint(reqp, "parity error in data-phase\n");
1546 reqp->xs->error = XS_TIMEOUT;
1547 }
1548 }
1549
1550 /*
1551 * DMA mode should always be reset even when we will continue with the
1552 * next chain. It is also essential to clear the MON_BUSY because
1553 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1554 * the bus....
1555 */
1556 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1557
1558
1559 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1560 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1561
1562 /*
1563 * Tell interrupt functions DMA mode has ended.
1564 */
1565 reqp->dr_flag &= ~DRIVER_IN_DMA;
1566
1567 /*
1568 * Clear mode and icom
1569 */
1570 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1571 SET_5380_REG(NCR5380_ICOM, 0);
1572
1573 if (dmstat & SC_BSY_ERR) {
1574 if (!reqp->xs->error)
1575 reqp->xs->error = XS_TIMEOUT;
1576 finish_req(reqp);
1577 PID("dma_ready1");
1578 return (1);
1579 }
1580
1581 if (reqp->xs->error != 0) {
1582 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1583 reqp->msgout = MSG_ABORT;
1584 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1585 }
1586 PID("dma_ready2");
1587 return (1);
1588 }
1589 return (0);
1590 }
1591 #endif /* REAL_DMA */
1592
1593 static int
1594 check_autosense(reqp, linked)
1595 SC_REQ *reqp;
1596 int linked;
1597 {
1598 int sps;
1599
1600 /*
1601 * If this is the driver's Link Check for this target, ignore
1602 * the results of the command. All we care about is whether we
1603 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1604 */
1605 PID("linkcheck");
1606 if (reqp->dr_flag & DRIVER_LINKCHK) {
1607 if (linked)
1608 ncr_will_link |= 1<<reqp->targ_id;
1609 else ncr_tprint(reqp, "Does not support linked commands\n");
1610 return (0);
1611 }
1612 /*
1613 * If we not executing an auto-sense and the status code
1614 * is request-sense, we automatically issue a request
1615 * sense command.
1616 */
1617 PID("cautos1");
1618 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1619 switch (reqp->status & SCSMASK) {
1620 case SCSCHKC:
1621 bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
1622 reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
1623 reqp->xdata_len = sizeof(reqp->xs->sense);
1624 reqp->dr_flag |= DRIVER_AUTOSEN;
1625 reqp->dr_flag &= ~DRIVER_DMAOK;
1626 if (!linked) {
1627 sps = splbio();
1628 reqp->next = issue_q;
1629 issue_q = reqp;
1630 splx(sps);
1631 }
1632 else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1633
1634 #ifdef DBG_REQ
1635 bzero(reqp->xdata_ptr, reqp->xdata_len);
1636 if (dbg_target_mask & (1 << reqp->targ_id))
1637 show_request(reqp, "AUTO-SENSE");
1638 #endif
1639 PID("cautos2");
1640 return (-1);
1641 case SCSBUSY:
1642 reqp->xs->error = XS_BUSY;
1643 return (0);
1644 }
1645 }
1646 else {
1647 /*
1648 * An auto-sense has finished
1649 */
1650 if ((reqp->status & SCSMASK) != SCSGOOD)
1651 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1652 else reqp->xs->error = XS_SENSE;
1653 reqp->status = SCSCHKC;
1654 }
1655 PID("cautos3");
1656 return (0);
1657 }
1658
1659 static int
1660 reach_msg_out(sc, len)
1661 struct ncr_softc *sc;
1662 u_long len;
1663 {
1664 u_char phase;
1665 u_char data;
1666 u_long n = len;
1667
1668 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1669 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1670 phase = (phase >> 2) & 7;
1671 else return (-1);
1672 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1673 if (phase == PH_MSGOUT)
1674 return (0);
1675
1676 SET_5380_REG(NCR5380_TCOM, phase);
1677
1678 do {
1679 if (!wait_req_true())
1680 break;
1681 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1682 break;
1683 if (PH_IN(phase)) {
1684 data = GET_5380_REG(NCR5380_DATA);
1685 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1686 }
1687 else {
1688 SET_5380_REG(NCR5380_DATA, 0);
1689 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1690 }
1691 if (!wait_req_false())
1692 break;
1693 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1694 } while (--n);
1695
1696 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1697 phase = (phase >> 2) & 7;
1698 if (phase == PH_MSGOUT) {
1699 ncr_aprint(sc, "Message-out phase reached after "
1700 "%ld bytes.\n", len - n);
1701 return (0);
1702 }
1703 }
1704 return (-1);
1705 }
1706
1707 void
1708 scsi_reset()
1709 {
1710 SC_REQ *tmp, *next;
1711 int sps;
1712
1713 PID("scsi_reset1");
1714 sps = splbio();
1715 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1716 delay(100);
1717 SET_5380_REG(NCR5380_ICOM, 0);
1718 scsi_clr_ipend();
1719
1720 /*
1721 * None of the jobs in the discon_q will ever be reconnected,
1722 * notify this to the higher level code.
1723 */
1724 for (tmp = discon_q; tmp ;) {
1725 next = tmp->next;
1726 tmp->next = NULL;
1727 tmp->xs->error = XS_TIMEOUT;
1728 busy &= ~(1 << tmp->targ_id);
1729 finish_req(tmp);
1730 tmp = next;
1731 }
1732 discon_q = NULL;
1733
1734 /*
1735 * The current job will never finish either.
1736 * The problem is that we can't finish the job because an instance
1737 * of main is running on it. Our best guess is that the job is currently
1738 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1739 * the job because it detects BSY-loss.
1740 */
1741 if ((tmp = connected) != NULL) {
1742 if (tmp->dr_flag & DRIVER_IN_DMA) {
1743 tmp->xs->error = XS_DRIVER_STUFFUP;
1744 #ifdef REAL_DMA
1745 dma_ready();
1746 #endif
1747 }
1748 }
1749 splx(sps);
1750 PID("scsi_reset2");
1751
1752 /*
1753 * Give the attached devices some time to handle the reset. This
1754 * value is arbitrary but should be relatively long.
1755 */
1756 delay(100000);
1757 }
1758
1759 static void
1760 scsi_reset_verbose(sc, why)
1761 struct ncr_softc *sc;
1762 const char *why;
1763 {
1764 ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1765
1766 scsi_reset();
1767 }
1768
1769 /*
1770 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1771 * the appropriate action is carried out (reselection or DMA ready) and
1772 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1773 * and INTR_SPURIOUS is returned.
1774 */
1775 static int
1776 check_intr(sc)
1777 struct ncr_softc *sc;
1778 {
1779 SC_REQ *reqp;
1780
1781 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1782 ==(SC_S_SEL|SC_S_IO))
1783 return (INTR_RESEL);
1784 else {
1785 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1786 reqp->dr_flag &= ~DRIVER_IN_DMA;
1787 return (INTR_DMA);
1788 }
1789 }
1790 scsi_clr_ipend();
1791 printf("-->");
1792 scsi_show();
1793 ncr_aprint(sc, "Spurious interrupt.\n");
1794 return (INTR_SPURIOUS);
1795 }
1796
1797 #ifdef REAL_DMA
1798 /*
1799 * Check if DMA can be used for this request. This function also builds
1800 * the dma-chain.
1801 */
1802 static int
1803 scsi_dmaok(reqp)
1804 SC_REQ *reqp;
1805 {
1806 u_long phy_buf;
1807 u_long phy_len;
1808 void *req_addr;
1809 u_long req_len;
1810 struct dma_chain *dm;
1811
1812 /*
1813 * Initialize locals and requests' DMA-chain.
1814 */
1815 req_len = reqp->xdata_len;
1816 req_addr = (void*)reqp->xdata_ptr;
1817 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1818 dm->dm_count = dm->dm_addr = 0;
1819 reqp->dr_flag &= ~DRIVER_BOUNCING;
1820
1821 /*
1822 * Do not accept zero length DMA.
1823 */
1824 if (req_len == 0)
1825 return (0);
1826
1827 /*
1828 * LWP: I think that this restriction is not strictly nessecary.
1829 */
1830 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1831 return (0);
1832
1833 /*
1834 * Build the DMA-chain.
1835 */
1836 dm->dm_addr = phy_buf = kvtop(req_addr);
1837 while (req_len) {
1838 if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1839 phy_len = req_len;
1840
1841 req_addr += phy_len;
1842 req_len -= phy_len;
1843 dm->dm_count += phy_len;
1844
1845 if (req_len) {
1846 u_long tmp = kvtop(req_addr);
1847
1848 if ((phy_buf + phy_len) != tmp) {
1849 if (wrong_dma_range(reqp, dm)) {
1850 if (reqp->dr_flag & DRIVER_BOUNCING)
1851 goto bounceit;
1852 return (0);
1853 }
1854
1855 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1856 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1857 return (0);
1858 }
1859 dm->dm_count = 0;
1860 dm->dm_addr = tmp;
1861 }
1862 phy_buf = tmp;
1863 }
1864 }
1865 if (wrong_dma_range(reqp, dm)) {
1866 if (reqp->dr_flag & DRIVER_BOUNCING)
1867 goto bounceit;
1868 return (0);
1869 }
1870 reqp->dm_last = dm;
1871 return (1);
1872
1873 bounceit:
1874 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1875 /*
1876 * If we can't get a bounce buffer, forget DMA
1877 */
1878 reqp->dr_flag &= ~DRIVER_BOUNCING;
1879 return(0);
1880 }
1881 /*
1882 * Initialize a single DMA-range containing the bounced request
1883 */
1884 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1885 dm->dm_addr = kvtop(reqp->bounceb);
1886 dm->dm_count = reqp->xdata_len;
1887 reqp->bouncerp = reqp->bounceb;
1888
1889 return (1);
1890 }
1891 #endif /* REAL_DMA */
1892
1893 static void
1894 run_main(sc)
1895 struct ncr_softc *sc;
1896 {
1897 int sps = splbio();
1898
1899 if (!main_running) {
1900 /*
1901 * If shared resources are required, claim them
1902 * before entering 'scsi_main'. If we can't get them
1903 * now, assume 'run_main' will be called when the resource
1904 * becomes available.
1905 */
1906 if (!claimed_dma()) {
1907 splx(sps);
1908 return;
1909 }
1910 main_running = 1;
1911 splx(sps);
1912 scsi_main(sc);
1913 }
1914 else splx(sps);
1915 }
1916
1917 /*
1918 * Prefix message with full target info.
1919 */
1920 static void
1921 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1922 {
1923 va_list ap;
1924
1925 va_start(ap, fmt);
1926 sc_print_addr(reqp->xs->sc_link);
1927 printf("%:", fmt, ap);
1928 va_end(ap);
1929 }
1930
1931 /*
1932 * Prefix message with adapter info.
1933 */
1934 static void
1935 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1936 {
1937 va_list ap;
1938
1939 va_start(ap, fmt);
1940 printf("%s : %:", sc->sc_dev.dv_xname, fmt, ap);
1941 va_end(ap);
1942 }
1943 /****************************************************************************
1944 * Start Debugging Functions *
1945 ****************************************************************************/
1946 static void
1947 show_data_sense(xs)
1948 struct scsi_xfer *xs;
1949 {
1950 u_char *p1, *p2;
1951 int i;
1952 int sz;
1953
1954 p1 = (u_char *) xs->cmd;
1955 p2 = (u_char *)&xs->sense;
1956 if(*p2 == 0)
1957 return; /* No(n)sense */
1958 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
1959 for (i = 0; i < sz; i++)
1960 printf("%x ", p1[i]);
1961 printf("\nsense: ");
1962 for (i = 0; i < sizeof(xs->sense); i++)
1963 printf("%x ", p2[i]);
1964 printf("\n");
1965 }
1966
1967 static void
1968 show_request(reqp, qtxt)
1969 SC_REQ *reqp;
1970 char *qtxt;
1971 {
1972 printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
1973 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1974 reqp->xcmd.opcode, reqp->status, reqp->message,
1975 reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
1976 reqp->link ? "L":"");
1977 if (reqp->status == SCSCHKC)
1978 show_data_sense(reqp->xs);
1979 }
1980
1981 static char *sig_names[] = {
1982 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1983 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1984 };
1985
1986 static void
1987 show_signals(dmstat, idstat)
1988 u_char dmstat, idstat;
1989 {
1990 u_short tmp, mask;
1991 int j, need_pipe;
1992
1993 tmp = idstat | ((dmstat & 3) << 8);
1994 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1995 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1996 if (tmp & mask)
1997 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1998 }
1999 printf("\nDma status (%02x): ", dmstat);
2000 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
2001 if (dmstat & mask)
2002 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
2003 }
2004 printf("\n");
2005 }
2006
2007 void
2008 scsi_show()
2009 {
2010 SC_REQ *tmp;
2011 int sps = splhigh();
2012 u_char idstat, dmstat;
2013 int i;
2014
2015 printf("scsi_show: scsi_main is%s running\n",
2016 main_running ? "" : " not");
2017 for (tmp = issue_q; tmp; tmp = tmp->next)
2018 show_request(tmp, "ISSUED");
2019 for (tmp = discon_q; tmp; tmp = tmp->next)
2020 show_request(tmp, "DISCONNECTED");
2021 if (connected)
2022 show_request(connected, "CONNECTED");
2023 idstat = GET_5380_REG(NCR5380_IDSTAT);
2024 dmstat = GET_5380_REG(NCR5380_DMSTAT);
2025 show_signals(dmstat, idstat);
2026 if (connected)
2027 printf("phase = %d, ", connected->phase);
2028 printf("busy:%x, spl:%04x\n", busy, sps);
2029 #ifdef DBG_PID
2030 for (i=0; i<DBG_PID; i++)
2031 printf("\t%d\t%s\n", i, last_hit[i]);
2032 #endif
2033
2034 splx(sps);
2035 }
2036