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