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