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