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