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