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