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