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