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