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