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