sbic.c revision 1.44.6.3 1 1.44.6.3 nathanw /* $NetBSD: sbic.c,v 1.44.6.3 2002/06/20 03:37:54 nathanw Exp $ */
2 1.44.6.2 nathanw
3 1.44.6.2 nathanw /*
4 1.44.6.2 nathanw * Copyright (c) 1994 Christian E. Hopps
5 1.44.6.2 nathanw * Copyright (c) 1990 The Regents of the University of California.
6 1.44.6.2 nathanw * All rights reserved.
7 1.44.6.2 nathanw *
8 1.44.6.2 nathanw * This code is derived from software contributed to Berkeley by
9 1.44.6.2 nathanw * Van Jacobson of Lawrence Berkeley Laboratory.
10 1.44.6.2 nathanw *
11 1.44.6.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.44.6.2 nathanw * modification, are permitted provided that the following conditions
13 1.44.6.2 nathanw * are met:
14 1.44.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.44.6.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.44.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.44.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.44.6.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.44.6.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.44.6.2 nathanw * must display the following acknowledgement:
21 1.44.6.2 nathanw * This product includes software developed by the University of
22 1.44.6.2 nathanw * California, Berkeley and its contributors.
23 1.44.6.2 nathanw * 4. Neither the name of the University nor the names of its contributors
24 1.44.6.2 nathanw * may be used to endorse or promote products derived from this software
25 1.44.6.2 nathanw * without specific prior written permission.
26 1.44.6.2 nathanw *
27 1.44.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.44.6.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.44.6.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.44.6.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.44.6.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.44.6.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.44.6.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.44.6.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.44.6.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.44.6.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.44.6.2 nathanw * SUCH DAMAGE.
38 1.44.6.2 nathanw *
39 1.44.6.2 nathanw * @(#)scsi.c 7.5 (Berkeley) 5/4/91
40 1.44.6.2 nathanw */
41 1.44.6.2 nathanw
42 1.44.6.2 nathanw /*
43 1.44.6.2 nathanw * AMIGA AMD 33C93 scsi adaptor driver
44 1.44.6.2 nathanw */
45 1.44.6.2 nathanw
46 1.44.6.2 nathanw #include "opt_ddb.h"
47 1.44.6.2 nathanw
48 1.44.6.2 nathanw #include <sys/cdefs.h>
49 1.44.6.3 nathanw __KERNEL_RCSID(0, "$NetBSD: sbic.c,v 1.44.6.3 2002/06/20 03:37:54 nathanw Exp $");
50 1.44.6.2 nathanw
51 1.44.6.2 nathanw #include <sys/param.h>
52 1.44.6.2 nathanw #include <sys/systm.h>
53 1.44.6.2 nathanw #include <sys/device.h>
54 1.44.6.2 nathanw #include <sys/kernel.h> /* For hz */
55 1.44.6.2 nathanw #include <sys/disklabel.h>
56 1.44.6.2 nathanw #include <sys/dkstat.h>
57 1.44.6.2 nathanw #include <sys/buf.h>
58 1.44.6.2 nathanw #include <dev/scsipi/scsi_all.h>
59 1.44.6.2 nathanw #include <dev/scsipi/scsipi_all.h>
60 1.44.6.2 nathanw #include <dev/scsipi/scsiconf.h>
61 1.44.6.2 nathanw #include <uvm/uvm_extern.h>
62 1.44.6.2 nathanw #include <machine/cpu.h>
63 1.44.6.2 nathanw #include <amiga/amiga/device.h>
64 1.44.6.2 nathanw #include <amiga/amiga/custom.h>
65 1.44.6.2 nathanw #include <amiga/amiga/isr.h>
66 1.44.6.2 nathanw #include <amiga/dev/dmavar.h>
67 1.44.6.2 nathanw #include <amiga/dev/sbicreg.h>
68 1.44.6.2 nathanw #include <amiga/dev/sbicvar.h>
69 1.44.6.2 nathanw
70 1.44.6.2 nathanw /* These are for bounce buffers */
71 1.44.6.2 nathanw #include <amiga/amiga/cc.h>
72 1.44.6.2 nathanw #include <amiga/dev/zbusvar.h>
73 1.44.6.2 nathanw
74 1.44.6.2 nathanw /* Since I can't find this in any other header files */
75 1.44.6.2 nathanw #define SCSI_PHASE(reg) (reg&0x07)
76 1.44.6.2 nathanw
77 1.44.6.2 nathanw /*
78 1.44.6.2 nathanw * SCSI delays
79 1.44.6.2 nathanw * In u-seconds, primarily for state changes on the SPC.
80 1.44.6.2 nathanw */
81 1.44.6.2 nathanw #define SBIC_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
82 1.44.6.2 nathanw #define SBIC_DATA_WAIT 50000 /* wait per data in/out step */
83 1.44.6.2 nathanw #define SBIC_INIT_WAIT 50000 /* wait per step (both) during init */
84 1.44.6.2 nathanw
85 1.44.6.2 nathanw #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
86 1.44.6.2 nathanw
87 1.44.6.2 nathanw int sbicicmd(struct sbic_softc *, int, int, void *, int, void *, int);
88 1.44.6.2 nathanw int sbicgo(struct sbic_softc *, struct scsipi_xfer *);
89 1.44.6.2 nathanw int sbicdmaok(struct sbic_softc *, struct scsipi_xfer *);
90 1.44.6.2 nathanw int sbicwait(sbic_regmap_t, char, int , int);
91 1.44.6.2 nathanw int sbiccheckdmap(void *, u_long, u_long);
92 1.44.6.2 nathanw int sbicselectbus(struct sbic_softc *, sbic_regmap_t, u_char, u_char, u_char);
93 1.44.6.2 nathanw int sbicxfstart(sbic_regmap_t, int, u_char, int);
94 1.44.6.2 nathanw int sbicxfout(sbic_regmap_t regs, int, void *, int);
95 1.44.6.2 nathanw int sbicfromscsiperiod(struct sbic_softc *, sbic_regmap_t, int);
96 1.44.6.2 nathanw int sbictoscsiperiod(struct sbic_softc *, sbic_regmap_t, int);
97 1.44.6.2 nathanw int sbicpoll(struct sbic_softc *);
98 1.44.6.2 nathanw int sbicnextstate(struct sbic_softc *, u_char, u_char);
99 1.44.6.2 nathanw int sbicmsgin(struct sbic_softc *);
100 1.44.6.2 nathanw int sbicxfin(sbic_regmap_t regs, int, void *);
101 1.44.6.2 nathanw int sbicabort(struct sbic_softc *, sbic_regmap_t, char *);
102 1.44.6.2 nathanw void sbicxfdone(struct sbic_softc *, sbic_regmap_t, int);
103 1.44.6.2 nathanw void sbicerror(struct sbic_softc *, sbic_regmap_t, u_char);
104 1.44.6.2 nathanw void sbicstart(struct sbic_softc *);
105 1.44.6.2 nathanw void sbicreset(struct sbic_softc *);
106 1.44.6.2 nathanw void sbic_scsidone(struct sbic_acb *, int);
107 1.44.6.2 nathanw void sbic_sched(struct sbic_softc *);
108 1.44.6.2 nathanw void sbic_save_ptrs(struct sbic_softc *, sbic_regmap_t,int,int);
109 1.44.6.2 nathanw void sbic_load_ptrs(struct sbic_softc *, sbic_regmap_t,int,int);
110 1.44.6.2 nathanw #ifdef DEBUG
111 1.44.6.2 nathanw void sbicdumpstate(void);
112 1.44.6.2 nathanw void sbic_dump_acb(struct sbic_acb *);
113 1.44.6.2 nathanw #endif
114 1.44.6.2 nathanw
115 1.44.6.2 nathanw /*
116 1.44.6.2 nathanw * Synch xfer parameters, and timing conversions
117 1.44.6.2 nathanw */
118 1.44.6.2 nathanw int sbic_min_period = SBIC_SYN_MIN_PERIOD; /* in cycles = f(ICLK,FSn) */
119 1.44.6.2 nathanw int sbic_max_offset = SBIC_SYN_MAX_OFFSET; /* pure number */
120 1.44.6.2 nathanw
121 1.44.6.2 nathanw int sbic_cmd_wait = SBIC_CMD_WAIT;
122 1.44.6.2 nathanw int sbic_data_wait = SBIC_DATA_WAIT;
123 1.44.6.2 nathanw int sbic_init_wait = SBIC_INIT_WAIT;
124 1.44.6.2 nathanw
125 1.44.6.2 nathanw /*
126 1.44.6.2 nathanw * was broken before.. now if you want this you get it for all drives
127 1.44.6.2 nathanw * on sbic controllers.
128 1.44.6.2 nathanw */
129 1.44.6.2 nathanw u_char sbic_inhibit_sync[8];
130 1.44.6.2 nathanw int sbic_enable_reselect = 1;
131 1.44.6.2 nathanw int sbic_clock_override = 0;
132 1.44.6.2 nathanw int sbic_no_dma = 0;
133 1.44.6.2 nathanw int sbic_parallel_operations = 1;
134 1.44.6.2 nathanw
135 1.44.6.2 nathanw #ifdef DEBUG
136 1.44.6.2 nathanw sbic_regmap_t debug_sbic_regs;
137 1.44.6.2 nathanw int sbicdma_ops = 0; /* total DMA operations */
138 1.44.6.2 nathanw int sbicdma_bounces = 0; /* number operations using bounce buffer */
139 1.44.6.2 nathanw int sbicdma_hits = 0; /* number of DMA chains that were contiguous */
140 1.44.6.2 nathanw int sbicdma_misses = 0; /* number of DMA chains that were not contiguous */
141 1.44.6.2 nathanw int sbicdma_saves = 0;
142 1.44.6.2 nathanw #define QPRINTF(a) if (sbic_debug > 1) printf a
143 1.44.6.2 nathanw int sbic_debug = 0;
144 1.44.6.2 nathanw int sync_debug = 0;
145 1.44.6.2 nathanw int sbic_dma_debug = 0;
146 1.44.6.2 nathanw int reselect_debug = 0;
147 1.44.6.2 nathanw int data_pointer_debug = 0;
148 1.44.6.2 nathanw u_char debug_asr, debug_csr, routine;
149 1.44.6.2 nathanw void sbictimeout(struct sbic_softc *dev);
150 1.44.6.2 nathanw
151 1.44.6.2 nathanw #define CSR_TRACE_SIZE 32
152 1.44.6.2 nathanw #if CSR_TRACE_SIZE
153 1.44.6.2 nathanw #define CSR_TRACE(w,c,a,x) do { \
154 1.44.6.2 nathanw int s = splbio(); \
155 1.44.6.2 nathanw csr_trace[csr_traceptr].whr = (w); csr_trace[csr_traceptr].csr = (c); \
156 1.44.6.2 nathanw csr_trace[csr_traceptr].asr = (a); csr_trace[csr_traceptr].xtn = (x); \
157 1.44.6.2 nathanw dma_cachectl((caddr_t)&csr_trace[csr_traceptr], sizeof(csr_trace[0])); \
158 1.44.6.2 nathanw csr_traceptr = (csr_traceptr + 1) & (CSR_TRACE_SIZE - 1); \
159 1.44.6.2 nathanw /* dma_cachectl((caddr_t)&csr_traceptr, sizeof(csr_traceptr));*/ \
160 1.44.6.2 nathanw splx(s); \
161 1.44.6.2 nathanw } while (0)
162 1.44.6.2 nathanw int csr_traceptr;
163 1.44.6.2 nathanw int csr_tracesize = CSR_TRACE_SIZE;
164 1.44.6.2 nathanw struct {
165 1.44.6.2 nathanw u_char whr;
166 1.44.6.2 nathanw u_char csr;
167 1.44.6.2 nathanw u_char asr;
168 1.44.6.2 nathanw u_char xtn;
169 1.44.6.2 nathanw } csr_trace[CSR_TRACE_SIZE];
170 1.44.6.2 nathanw #else
171 1.44.6.2 nathanw #define CSR_TRACE(w,c,a,x)
172 1.44.6.2 nathanw #endif
173 1.44.6.2 nathanw
174 1.44.6.2 nathanw #define SBIC_TRACE_SIZE 0
175 1.44.6.2 nathanw #if SBIC_TRACE_SIZE
176 1.44.6.2 nathanw #define SBIC_TRACE(dev) do { \
177 1.44.6.2 nathanw int s = splbio(); \
178 1.44.6.2 nathanw sbic_trace[sbic_traceptr].sp = &s; \
179 1.44.6.2 nathanw sbic_trace[sbic_traceptr].line = __LINE__; \
180 1.44.6.2 nathanw sbic_trace[sbic_traceptr].sr = s; \
181 1.44.6.2 nathanw sbic_trace[sbic_traceptr].csr = csr_traceptr; \
182 1.44.6.2 nathanw dma_cachectl(&sbic_trace[sbic_traceptr], sizeof(sbic_trace[0])); \
183 1.44.6.2 nathanw sbic_traceptr = (sbic_traceptr + 1) & (SBIC_TRACE_SIZE - 1); \
184 1.44.6.2 nathanw dma_cachectl(&sbic_traceptr, sizeof(sbic_traceptr)); \
185 1.44.6.2 nathanw if (dev) dma_cachectl(dev, sizeof(*dev)); \
186 1.44.6.2 nathanw splx(s); \
187 1.44.6.2 nathanw } while (0)
188 1.44.6.2 nathanw int sbic_traceptr;
189 1.44.6.2 nathanw int sbic_tracesize = SBIC_TRACE_SIZE;
190 1.44.6.2 nathanw struct {
191 1.44.6.2 nathanw void *sp;
192 1.44.6.2 nathanw u_short line;
193 1.44.6.2 nathanw u_short sr;
194 1.44.6.2 nathanw int csr;
195 1.44.6.2 nathanw } sbic_trace[SBIC_TRACE_SIZE];
196 1.44.6.2 nathanw #else
197 1.44.6.2 nathanw #define SBIC_TRACE(dev)
198 1.44.6.2 nathanw #endif
199 1.44.6.2 nathanw
200 1.44.6.2 nathanw #else /* DEBUG */
201 1.44.6.2 nathanw #define QPRINTF(a)
202 1.44.6.2 nathanw #define CSR_TRACE(w,c,a,x)
203 1.44.6.2 nathanw #define SBIC_TRACE(dev)
204 1.44.6.2 nathanw #endif /* DEBUG */
205 1.44.6.2 nathanw
206 1.44.6.2 nathanw /*
207 1.44.6.2 nathanw * default minphys routine for sbic based controllers
208 1.44.6.2 nathanw */
209 1.44.6.2 nathanw void
210 1.44.6.2 nathanw sbic_minphys(struct buf *bp)
211 1.44.6.2 nathanw {
212 1.44.6.2 nathanw
213 1.44.6.2 nathanw /*
214 1.44.6.2 nathanw * No max transfer at this level.
215 1.44.6.2 nathanw */
216 1.44.6.2 nathanw minphys(bp);
217 1.44.6.2 nathanw }
218 1.44.6.2 nathanw
219 1.44.6.2 nathanw /*
220 1.44.6.2 nathanw * Save DMA pointers. Take into account partial transfer. Shut down DMA.
221 1.44.6.2 nathanw */
222 1.44.6.2 nathanw void
223 1.44.6.2 nathanw sbic_save_ptrs(struct sbic_softc *dev, sbic_regmap_t regs, int target, int lun)
224 1.44.6.2 nathanw {
225 1.44.6.2 nathanw int count, asr, s;
226 1.44.6.2 nathanw struct sbic_acb* acb;
227 1.44.6.2 nathanw
228 1.44.6.2 nathanw SBIC_TRACE(dev);
229 1.44.6.2 nathanw if( !dev->sc_cur ) return;
230 1.44.6.2 nathanw if( !(dev->sc_flags & SBICF_INDMA) ) return; /* DMA not active */
231 1.44.6.2 nathanw
232 1.44.6.2 nathanw s = splbio();
233 1.44.6.2 nathanw
234 1.44.6.2 nathanw acb = dev->sc_nexus;
235 1.44.6.2 nathanw count = -1;
236 1.44.6.2 nathanw do {
237 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
238 1.44.6.2 nathanw if( asr & SBIC_ASR_DBR ) {
239 1.44.6.2 nathanw printf("sbic_save_ptrs: asr %02x canceled!\n", asr);
240 1.44.6.2 nathanw splx(s);
241 1.44.6.2 nathanw SBIC_TRACE(dev);
242 1.44.6.2 nathanw return;
243 1.44.6.2 nathanw }
244 1.44.6.2 nathanw } while( asr & (SBIC_ASR_BSY|SBIC_ASR_CIP) );
245 1.44.6.2 nathanw
246 1.44.6.2 nathanw /* Save important state */
247 1.44.6.2 nathanw /* must be done before dmastop */
248 1.44.6.2 nathanw acb->sc_dmacmd = dev->sc_dmacmd;
249 1.44.6.2 nathanw SBIC_TC_GET(regs, count);
250 1.44.6.2 nathanw
251 1.44.6.2 nathanw /* Shut down DMA ====CAREFUL==== */
252 1.44.6.2 nathanw dev->sc_dmastop(dev);
253 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_INDMA;
254 1.44.6.2 nathanw SBIC_TC_PUT(regs, 0);
255 1.44.6.2 nathanw
256 1.44.6.2 nathanw #ifdef DEBUG
257 1.44.6.2 nathanw if(!count && sbic_debug) printf("%dcount0",target);
258 1.44.6.2 nathanw if(data_pointer_debug == -1)
259 1.44.6.2 nathanw printf("SBIC saving target %d data pointers from (%p,%x)%xASR:%02x",
260 1.44.6.2 nathanw target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
261 1.44.6.2 nathanw acb->sc_dmacmd, asr);
262 1.44.6.2 nathanw #endif
263 1.44.6.2 nathanw
264 1.44.6.2 nathanw /* Fixup partial xfers */
265 1.44.6.2 nathanw acb->sc_kv.dc_addr += (dev->sc_tcnt - count);
266 1.44.6.2 nathanw acb->sc_kv.dc_count -= (dev->sc_tcnt - count);
267 1.44.6.2 nathanw acb->sc_pa.dc_addr += (dev->sc_tcnt - count);
268 1.44.6.2 nathanw acb->sc_pa.dc_count -= ((dev->sc_tcnt - count)>>1);
269 1.44.6.2 nathanw
270 1.44.6.2 nathanw acb->sc_tcnt = dev->sc_tcnt = count;
271 1.44.6.2 nathanw #ifdef DEBUG
272 1.44.6.2 nathanw if(data_pointer_debug)
273 1.44.6.2 nathanw printf(" at (%p,%x):%x\n",
274 1.44.6.2 nathanw dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
275 1.44.6.2 nathanw sbicdma_saves++;
276 1.44.6.2 nathanw #endif
277 1.44.6.2 nathanw splx(s);
278 1.44.6.2 nathanw SBIC_TRACE(dev);
279 1.44.6.2 nathanw }
280 1.44.6.2 nathanw
281 1.44.6.2 nathanw
282 1.44.6.2 nathanw /*
283 1.44.6.2 nathanw * DOES NOT RESTART DMA!!!
284 1.44.6.2 nathanw */
285 1.44.6.2 nathanw void
286 1.44.6.2 nathanw sbic_load_ptrs(struct sbic_softc *dev, sbic_regmap_t regs, int target, int lun)
287 1.44.6.2 nathanw {
288 1.44.6.2 nathanw int s, count;
289 1.44.6.2 nathanw char* vaddr, * paddr;
290 1.44.6.2 nathanw struct sbic_acb *acb;
291 1.44.6.2 nathanw
292 1.44.6.2 nathanw SBIC_TRACE(dev);
293 1.44.6.2 nathanw acb = dev->sc_nexus;
294 1.44.6.2 nathanw if( !acb->sc_kv.dc_count ) {
295 1.44.6.2 nathanw /* No data to xfer */
296 1.44.6.2 nathanw SBIC_TRACE(dev);
297 1.44.6.2 nathanw return;
298 1.44.6.2 nathanw }
299 1.44.6.2 nathanw
300 1.44.6.2 nathanw s = splbio();
301 1.44.6.2 nathanw
302 1.44.6.2 nathanw dev->sc_last = dev->sc_cur = &acb->sc_pa;
303 1.44.6.2 nathanw dev->sc_tcnt = acb->sc_tcnt;
304 1.44.6.2 nathanw dev->sc_dmacmd = acb->sc_dmacmd;
305 1.44.6.2 nathanw
306 1.44.6.2 nathanw #ifdef DEBUG
307 1.44.6.2 nathanw sbicdma_ops++;
308 1.44.6.2 nathanw #endif
309 1.44.6.2 nathanw if( !dev->sc_tcnt ) {
310 1.44.6.2 nathanw /* sc_tcnt == 0 implies end of segment */
311 1.44.6.2 nathanw
312 1.44.6.2 nathanw /* do kvm to pa mappings */
313 1.44.6.2 nathanw paddr = acb->sc_pa.dc_addr =
314 1.44.6.2 nathanw (char *) kvtop(acb->sc_kv.dc_addr);
315 1.44.6.2 nathanw
316 1.44.6.2 nathanw vaddr = acb->sc_kv.dc_addr;
317 1.44.6.2 nathanw count = acb->sc_kv.dc_count;
318 1.44.6.2 nathanw for(count = (NBPG - ((int)vaddr & PGOFSET));
319 1.44.6.2 nathanw count < acb->sc_kv.dc_count
320 1.44.6.2 nathanw && (char*)kvtop(vaddr + count + 4) == paddr + count + 4;
321 1.44.6.2 nathanw count += NBPG);
322 1.44.6.2 nathanw /* If it's all contiguous... */
323 1.44.6.2 nathanw if(count > acb->sc_kv.dc_count ) {
324 1.44.6.2 nathanw count = acb->sc_kv.dc_count;
325 1.44.6.2 nathanw #ifdef DEBUG
326 1.44.6.2 nathanw sbicdma_hits++;
327 1.44.6.2 nathanw #endif
328 1.44.6.2 nathanw } else {
329 1.44.6.2 nathanw #ifdef DEBUG
330 1.44.6.2 nathanw sbicdma_misses++;
331 1.44.6.2 nathanw #endif
332 1.44.6.2 nathanw }
333 1.44.6.2 nathanw acb->sc_tcnt = count;
334 1.44.6.2 nathanw acb->sc_pa.dc_count = count >> 1;
335 1.44.6.2 nathanw
336 1.44.6.2 nathanw #ifdef DEBUG
337 1.44.6.2 nathanw if(data_pointer_debug)
338 1.44.6.2 nathanw printf("DMA recalc:kv(%p,%x)pa(%p,%lx)\n",
339 1.44.6.2 nathanw acb->sc_kv.dc_addr,
340 1.44.6.2 nathanw acb->sc_kv.dc_count,
341 1.44.6.2 nathanw acb->sc_pa.dc_addr,
342 1.44.6.2 nathanw acb->sc_tcnt);
343 1.44.6.2 nathanw #endif
344 1.44.6.2 nathanw }
345 1.44.6.2 nathanw splx(s);
346 1.44.6.2 nathanw #ifdef DEBUG
347 1.44.6.2 nathanw if(data_pointer_debug)
348 1.44.6.2 nathanw printf("SBIC restoring target %d data pointers at (%p,%x)%x\n",
349 1.44.6.2 nathanw target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
350 1.44.6.2 nathanw dev->sc_dmacmd);
351 1.44.6.2 nathanw #endif
352 1.44.6.2 nathanw SBIC_TRACE(dev);
353 1.44.6.2 nathanw }
354 1.44.6.2 nathanw
355 1.44.6.2 nathanw /*
356 1.44.6.2 nathanw * used by specific sbic controller
357 1.44.6.2 nathanw *
358 1.44.6.2 nathanw * it appears that the higher level code does nothing with LUN's
359 1.44.6.2 nathanw * so I will too. I could plug it in, however so could they
360 1.44.6.2 nathanw * in scsi_scsipi_cmd().
361 1.44.6.2 nathanw */
362 1.44.6.2 nathanw void
363 1.44.6.2 nathanw sbic_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
364 1.44.6.2 nathanw void *arg)
365 1.44.6.2 nathanw {
366 1.44.6.2 nathanw struct scsipi_xfer *xs;
367 1.44.6.2 nathanw struct scsipi_periph *periph;
368 1.44.6.2 nathanw struct sbic_acb *acb;
369 1.44.6.2 nathanw struct sbic_softc *dev = (void *)chan->chan_adapter->adapt_dev;
370 1.44.6.2 nathanw int flags, s, stat;
371 1.44.6.2 nathanw
372 1.44.6.2 nathanw switch (req) {
373 1.44.6.2 nathanw case ADAPTER_REQ_RUN_XFER:
374 1.44.6.2 nathanw xs = arg;
375 1.44.6.2 nathanw periph = xs->xs_periph;
376 1.44.6.2 nathanw
377 1.44.6.2 nathanw SBIC_TRACE(dev);
378 1.44.6.2 nathanw flags = xs->xs_control;
379 1.44.6.2 nathanw
380 1.44.6.2 nathanw if (flags & XS_CTL_DATA_UIO)
381 1.44.6.2 nathanw panic("sbic: scsi data uio requested");
382 1.44.6.2 nathanw
383 1.44.6.2 nathanw if (dev->sc_nexus && flags & XS_CTL_POLL)
384 1.44.6.2 nathanw panic("sbic_scsipi_request: busy");
385 1.44.6.2 nathanw
386 1.44.6.2 nathanw s = splbio();
387 1.44.6.2 nathanw acb = dev->free_list.tqh_first;
388 1.44.6.2 nathanw if (acb)
389 1.44.6.2 nathanw TAILQ_REMOVE(&dev->free_list, acb, chain);
390 1.44.6.2 nathanw splx(s);
391 1.44.6.2 nathanw
392 1.44.6.2 nathanw #ifdef DIAGNOSTIC
393 1.44.6.2 nathanw if (acb == NULL) {
394 1.44.6.2 nathanw scsipi_printaddr(periph);
395 1.44.6.2 nathanw printf("unable to allocate acb\n");
396 1.44.6.2 nathanw panic("sbic_scsipi_request");
397 1.44.6.2 nathanw }
398 1.44.6.2 nathanw #endif
399 1.44.6.2 nathanw acb->flags = ACB_ACTIVE;
400 1.44.6.2 nathanw if (flags & XS_CTL_DATA_IN)
401 1.44.6.2 nathanw acb->flags |= ACB_DATAIN;
402 1.44.6.2 nathanw acb->xs = xs;
403 1.44.6.2 nathanw bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
404 1.44.6.2 nathanw acb->clen = xs->cmdlen;
405 1.44.6.2 nathanw acb->sc_kv.dc_addr = xs->data;
406 1.44.6.2 nathanw acb->sc_kv.dc_count = xs->datalen;
407 1.44.6.2 nathanw acb->pa_addr = xs->data ? (char *)kvtop(xs->data) : 0; /* XXXX check */
408 1.44.6.2 nathanw
409 1.44.6.2 nathanw if (flags & XS_CTL_POLL) {
410 1.44.6.2 nathanw s = splbio();
411 1.44.6.2 nathanw /*
412 1.44.6.2 nathanw * This has major side effects - it locks up the machine
413 1.44.6.2 nathanw */
414 1.44.6.2 nathanw
415 1.44.6.2 nathanw dev->sc_flags |= SBICF_ICMD;
416 1.44.6.2 nathanw do {
417 1.44.6.2 nathanw while(dev->sc_nexus)
418 1.44.6.2 nathanw sbicpoll(dev);
419 1.44.6.2 nathanw dev->sc_nexus = acb;
420 1.44.6.2 nathanw dev->sc_stat[0] = -1;
421 1.44.6.2 nathanw dev->sc_xs = xs;
422 1.44.6.2 nathanw dev->target = periph->periph_target;
423 1.44.6.2 nathanw dev->lun = periph->periph_lun;
424 1.44.6.2 nathanw stat = sbicicmd(dev, dev->target, dev->lun,
425 1.44.6.2 nathanw &acb->cmd, acb->clen,
426 1.44.6.2 nathanw acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
427 1.44.6.2 nathanw } while (dev->sc_nexus != acb);
428 1.44.6.2 nathanw sbic_scsidone(acb, stat);
429 1.44.6.2 nathanw
430 1.44.6.2 nathanw splx(s);
431 1.44.6.2 nathanw SBIC_TRACE(dev);
432 1.44.6.2 nathanw return;
433 1.44.6.2 nathanw }
434 1.44.6.2 nathanw
435 1.44.6.2 nathanw s = splbio();
436 1.44.6.2 nathanw TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
437 1.44.6.2 nathanw
438 1.44.6.2 nathanw if (dev->sc_nexus) {
439 1.44.6.2 nathanw splx(s);
440 1.44.6.2 nathanw SBIC_TRACE(dev);
441 1.44.6.2 nathanw return;
442 1.44.6.2 nathanw }
443 1.44.6.2 nathanw
444 1.44.6.2 nathanw /*
445 1.44.6.2 nathanw * nothing is active, try to start it now.
446 1.44.6.2 nathanw */
447 1.44.6.2 nathanw sbic_sched(dev);
448 1.44.6.2 nathanw splx(s);
449 1.44.6.2 nathanw
450 1.44.6.2 nathanw SBIC_TRACE(dev);
451 1.44.6.2 nathanw /* TODO: add sbic_poll to do XS_CTL_POLL operations */
452 1.44.6.2 nathanw #if 0
453 1.44.6.2 nathanw if (flags & XS_CTL_POLL)
454 1.44.6.2 nathanw return(COMPLETE);
455 1.44.6.2 nathanw #endif
456 1.44.6.2 nathanw return;
457 1.44.6.2 nathanw
458 1.44.6.2 nathanw case ADAPTER_REQ_GROW_RESOURCES:
459 1.44.6.2 nathanw return;
460 1.44.6.2 nathanw
461 1.44.6.2 nathanw case ADAPTER_REQ_SET_XFER_MODE:
462 1.44.6.2 nathanw return;
463 1.44.6.2 nathanw }
464 1.44.6.2 nathanw }
465 1.44.6.2 nathanw
466 1.44.6.2 nathanw /*
467 1.44.6.2 nathanw * attempt to start the next available command
468 1.44.6.2 nathanw */
469 1.44.6.2 nathanw void
470 1.44.6.2 nathanw sbic_sched(struct sbic_softc *dev)
471 1.44.6.2 nathanw {
472 1.44.6.2 nathanw struct scsipi_xfer *xs;
473 1.44.6.2 nathanw struct scsipi_periph *periph;
474 1.44.6.2 nathanw struct sbic_acb *acb;
475 1.44.6.2 nathanw int flags, /*phase,*/ stat, i;
476 1.44.6.2 nathanw
477 1.44.6.2 nathanw SBIC_TRACE(dev);
478 1.44.6.2 nathanw if (dev->sc_nexus)
479 1.44.6.2 nathanw return; /* a command is current active */
480 1.44.6.2 nathanw
481 1.44.6.2 nathanw SBIC_TRACE(dev);
482 1.44.6.2 nathanw for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
483 1.44.6.2 nathanw periph = acb->xs->xs_periph;
484 1.44.6.2 nathanw i = periph->periph_target;
485 1.44.6.2 nathanw if (!(dev->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
486 1.44.6.2 nathanw struct sbic_tinfo *ti = &dev->sc_tinfo[i];
487 1.44.6.2 nathanw
488 1.44.6.2 nathanw TAILQ_REMOVE(&dev->ready_list, acb, chain);
489 1.44.6.2 nathanw dev->sc_nexus = acb;
490 1.44.6.2 nathanw ti = &dev->sc_tinfo[periph->periph_target];
491 1.44.6.2 nathanw ti->lubusy |= (1 << periph->periph_lun);
492 1.44.6.2 nathanw acb->sc_pa.dc_addr = acb->pa_addr; /* XXXX check */
493 1.44.6.2 nathanw break;
494 1.44.6.2 nathanw }
495 1.44.6.2 nathanw }
496 1.44.6.2 nathanw
497 1.44.6.2 nathanw SBIC_TRACE(dev);
498 1.44.6.2 nathanw if (acb == NULL)
499 1.44.6.2 nathanw return; /* did not find an available command */
500 1.44.6.2 nathanw
501 1.44.6.2 nathanw dev->sc_xs = xs = acb->xs;
502 1.44.6.2 nathanw periph = xs->xs_periph;
503 1.44.6.2 nathanw flags = xs->xs_control;
504 1.44.6.2 nathanw
505 1.44.6.2 nathanw if (flags & XS_CTL_RESET)
506 1.44.6.2 nathanw sbicreset(dev);
507 1.44.6.2 nathanw
508 1.44.6.2 nathanw #ifdef DEBUG
509 1.44.6.2 nathanw if( data_pointer_debug > 1 )
510 1.44.6.2 nathanw printf("sbic_sched(%d,%d)\n", periph->periph_target,
511 1.44.6.2 nathanw periph->periph_lun);
512 1.44.6.2 nathanw #endif
513 1.44.6.2 nathanw dev->sc_stat[0] = -1;
514 1.44.6.2 nathanw dev->target = periph->periph_target;
515 1.44.6.2 nathanw dev->lun = periph->periph_lun;
516 1.44.6.2 nathanw if ( flags & XS_CTL_POLL || ( !sbic_parallel_operations
517 1.44.6.2 nathanw && (sbicdmaok(dev, xs) == 0)))
518 1.44.6.2 nathanw stat = sbicicmd(dev, periph->periph_target,
519 1.44.6.2 nathanw periph->periph_lun, &acb->cmd,
520 1.44.6.2 nathanw acb->clen, acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
521 1.44.6.2 nathanw else if (sbicgo(dev, xs) == 0 && xs->error != XS_SELTIMEOUT) {
522 1.44.6.2 nathanw SBIC_TRACE(dev);
523 1.44.6.2 nathanw return;
524 1.44.6.2 nathanw } else
525 1.44.6.2 nathanw stat = dev->sc_stat[0];
526 1.44.6.2 nathanw
527 1.44.6.2 nathanw sbic_scsidone(acb, stat);
528 1.44.6.2 nathanw SBIC_TRACE(dev);
529 1.44.6.2 nathanw }
530 1.44.6.2 nathanw
531 1.44.6.2 nathanw void
532 1.44.6.2 nathanw sbic_scsidone(struct sbic_acb *acb, int stat)
533 1.44.6.2 nathanw {
534 1.44.6.2 nathanw struct scsipi_xfer *xs;
535 1.44.6.2 nathanw struct scsipi_periph *periph;
536 1.44.6.2 nathanw struct sbic_softc *dev;
537 1.44.6.2 nathanw int dosched = 0;
538 1.44.6.2 nathanw
539 1.44.6.2 nathanw xs = acb->xs;
540 1.44.6.2 nathanw periph = xs->xs_periph;
541 1.44.6.2 nathanw dev = (void *)periph->periph_channel->chan_adapter->adapt_dev;
542 1.44.6.2 nathanw SBIC_TRACE(dev);
543 1.44.6.2 nathanw #ifdef DIAGNOSTIC
544 1.44.6.2 nathanw if (acb == NULL || xs == NULL) {
545 1.44.6.2 nathanw printf("sbic_scsidone -- (%d,%d) no scsi_xfer\n",
546 1.44.6.2 nathanw dev->target, dev->lun);
547 1.44.6.2 nathanw #ifdef DDB
548 1.44.6.2 nathanw Debugger();
549 1.44.6.2 nathanw #endif
550 1.44.6.2 nathanw return;
551 1.44.6.2 nathanw }
552 1.44.6.2 nathanw #endif
553 1.44.6.2 nathanw
554 1.44.6.2 nathanw xs->status = stat;
555 1.44.6.2 nathanw xs->resid = 0; /* XXXX */
556 1.44.6.2 nathanw #ifdef DEBUG
557 1.44.6.2 nathanw if( data_pointer_debug > 1 )
558 1.44.6.2 nathanw printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
559 1.44.6.2 nathanw periph->periph_target, periph->periph_lun,
560 1.44.6.2 nathanw dev->target, dev->lun, stat);
561 1.44.6.2 nathanw if( periph->periph_target ==
562 1.44.6.2 nathanw periph->periph_channel->chan_id)
563 1.44.6.2 nathanw panic("target == hostid");
564 1.44.6.2 nathanw #endif
565 1.44.6.2 nathanw
566 1.44.6.2 nathanw if (xs->error == XS_NOERROR) {
567 1.44.6.2 nathanw if (stat == SCSI_CHECK || stat == SCSI_BUSY)
568 1.44.6.2 nathanw xs->error = XS_BUSY;
569 1.44.6.2 nathanw }
570 1.44.6.2 nathanw
571 1.44.6.2 nathanw /*
572 1.44.6.2 nathanw * Remove the ACB from whatever queue it's on. We have to do a bit of
573 1.44.6.2 nathanw * a hack to figure out which queue it's on. Note that it is *not*
574 1.44.6.2 nathanw * necessary to cdr down the ready queue, but we must cdr down the
575 1.44.6.2 nathanw * nexus queue and see if it's there, so we can mark the unit as no
576 1.44.6.2 nathanw * longer busy. This code is sickening, but it works.
577 1.44.6.2 nathanw */
578 1.44.6.2 nathanw if (acb == dev->sc_nexus) {
579 1.44.6.2 nathanw dev->sc_nexus = NULL;
580 1.44.6.2 nathanw dev->sc_xs = NULL;
581 1.44.6.2 nathanw dev->sc_tinfo[periph->periph_target].lubusy &=
582 1.44.6.2 nathanw ~(1<<periph->periph_lun);
583 1.44.6.2 nathanw if (dev->ready_list.tqh_first)
584 1.44.6.2 nathanw dosched = 1; /* start next command */
585 1.44.6.2 nathanw } else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
586 1.44.6.2 nathanw TAILQ_REMOVE(&dev->ready_list, acb, chain);
587 1.44.6.2 nathanw } else {
588 1.44.6.2 nathanw register struct sbic_acb *acb2;
589 1.44.6.2 nathanw for (acb2 = dev->nexus_list.tqh_first; acb2;
590 1.44.6.2 nathanw acb2 = acb2->chain.tqe_next) {
591 1.44.6.2 nathanw if (acb2 == acb) {
592 1.44.6.2 nathanw TAILQ_REMOVE(&dev->nexus_list, acb, chain);
593 1.44.6.2 nathanw dev->sc_tinfo[periph->periph_target].lubusy
594 1.44.6.2 nathanw &= ~(1<<periph->periph_lun);
595 1.44.6.2 nathanw break;
596 1.44.6.2 nathanw }
597 1.44.6.2 nathanw }
598 1.44.6.2 nathanw if (acb2)
599 1.44.6.2 nathanw ;
600 1.44.6.2 nathanw else if (acb->chain.tqe_next) {
601 1.44.6.2 nathanw TAILQ_REMOVE(&dev->ready_list, acb, chain);
602 1.44.6.2 nathanw } else {
603 1.44.6.2 nathanw printf("%s: can't find matching acb\n",
604 1.44.6.2 nathanw dev->sc_dev.dv_xname);
605 1.44.6.2 nathanw #ifdef DDB
606 1.44.6.2 nathanw Debugger();
607 1.44.6.2 nathanw #endif
608 1.44.6.2 nathanw }
609 1.44.6.2 nathanw }
610 1.44.6.2 nathanw /* Put it on the free list. */
611 1.44.6.2 nathanw acb->flags = ACB_FREE;
612 1.44.6.2 nathanw TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
613 1.44.6.2 nathanw
614 1.44.6.2 nathanw dev->sc_tinfo[periph->periph_target].cmds++;
615 1.44.6.2 nathanw
616 1.44.6.2 nathanw scsipi_done(xs);
617 1.44.6.2 nathanw
618 1.44.6.2 nathanw if (dosched)
619 1.44.6.2 nathanw sbic_sched(dev);
620 1.44.6.2 nathanw SBIC_TRACE(dev);
621 1.44.6.2 nathanw }
622 1.44.6.2 nathanw
623 1.44.6.2 nathanw int
624 1.44.6.2 nathanw sbicdmaok(struct sbic_softc *dev, struct scsipi_xfer *xs)
625 1.44.6.2 nathanw {
626 1.44.6.2 nathanw if (sbic_no_dma || !xs->datalen || xs->datalen & 0x1 ||
627 1.44.6.2 nathanw (u_int)xs->data & 0x3)
628 1.44.6.2 nathanw return(0);
629 1.44.6.2 nathanw /*
630 1.44.6.2 nathanw * controller supports dma to any addresses?
631 1.44.6.2 nathanw */
632 1.44.6.2 nathanw else if ((dev->sc_flags & SBICF_BADDMA) == 0)
633 1.44.6.2 nathanw return(1);
634 1.44.6.2 nathanw /*
635 1.44.6.2 nathanw * this address is ok for dma?
636 1.44.6.2 nathanw */
637 1.44.6.2 nathanw else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
638 1.44.6.2 nathanw return(1);
639 1.44.6.2 nathanw /*
640 1.44.6.2 nathanw * we have a bounce buffer?
641 1.44.6.2 nathanw */
642 1.44.6.2 nathanw else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
643 1.44.6.2 nathanw return(1);
644 1.44.6.2 nathanw /*
645 1.44.6.2 nathanw * try to get one
646 1.44.6.2 nathanw */
647 1.44.6.2 nathanw else if ((dev->sc_tinfo[xs->xs_periph->periph_target].bounce
648 1.44.6.2 nathanw = (char *)alloc_z2mem(MAXPHYS))) {
649 1.44.6.2 nathanw if (isztwomem(dev->sc_tinfo[xs->xs_periph->periph_target].bounce))
650 1.44.6.2 nathanw printf("alloc ZII target %d bounce pa 0x%x\n",
651 1.44.6.2 nathanw xs->xs_periph->periph_target,
652 1.44.6.2 nathanw kvtop(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
653 1.44.6.2 nathanw else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
654 1.44.6.2 nathanw printf("alloc CHIP target %d bounce pa 0x%p\n",
655 1.44.6.2 nathanw xs->xs_periph->periph_target,
656 1.44.6.2 nathanw PREP_DMA_MEM(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
657 1.44.6.2 nathanw return(1);
658 1.44.6.2 nathanw }
659 1.44.6.2 nathanw
660 1.44.6.2 nathanw return(0);
661 1.44.6.2 nathanw }
662 1.44.6.2 nathanw
663 1.44.6.2 nathanw
664 1.44.6.2 nathanw int
665 1.44.6.2 nathanw sbicwait(sbic_regmap_t regs, char until, int timeo, int line)
666 1.44.6.2 nathanw {
667 1.44.6.2 nathanw u_char val;
668 1.44.6.2 nathanw int csr;
669 1.44.6.2 nathanw
670 1.44.6.2 nathanw SBIC_TRACE((struct sbic_softc *)0);
671 1.44.6.2 nathanw if (timeo == 0)
672 1.44.6.2 nathanw timeo = 1000000; /* some large value.. */
673 1.44.6.2 nathanw
674 1.44.6.2 nathanw GET_SBIC_asr(regs,val);
675 1.44.6.2 nathanw while ((val & until) == 0) {
676 1.44.6.2 nathanw if (timeo-- == 0) {
677 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
678 1.44.6.2 nathanw printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
679 1.44.6.2 nathanw line, val, csr);
680 1.44.6.2 nathanw #if defined(DDB) && defined(DEBUG)
681 1.44.6.2 nathanw Debugger();
682 1.44.6.2 nathanw #endif
683 1.44.6.2 nathanw return(val); /* Maybe I should abort */
684 1.44.6.2 nathanw break;
685 1.44.6.2 nathanw }
686 1.44.6.2 nathanw DELAY(1);
687 1.44.6.2 nathanw GET_SBIC_asr(regs,val);
688 1.44.6.2 nathanw }
689 1.44.6.2 nathanw SBIC_TRACE((struct sbic_softc *)0);
690 1.44.6.2 nathanw return(val);
691 1.44.6.2 nathanw }
692 1.44.6.2 nathanw
693 1.44.6.2 nathanw int
694 1.44.6.2 nathanw sbicabort(struct sbic_softc *dev, sbic_regmap_t regs, char *where)
695 1.44.6.2 nathanw {
696 1.44.6.2 nathanw u_char csr, asr;
697 1.44.6.2 nathanw
698 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
699 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
700 1.44.6.2 nathanw
701 1.44.6.2 nathanw printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
702 1.44.6.2 nathanw dev->sc_dev.dv_xname, where, csr, asr);
703 1.44.6.2 nathanw
704 1.44.6.2 nathanw
705 1.44.6.2 nathanw #if 0
706 1.44.6.2 nathanw /* Clean up running command */
707 1.44.6.2 nathanw if (dev->sc_nexus != NULL) {
708 1.44.6.2 nathanw dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
709 1.44.6.2 nathanw sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
710 1.44.6.2 nathanw }
711 1.44.6.2 nathanw while (acb = dev->nexus_list.tqh_first) {
712 1.44.6.2 nathanw acb->xs->error = XS_DRIVER_STUFFUP;
713 1.44.6.2 nathanw sbic_scsidone(acb, -1 /*acb->stat[0]*/);
714 1.44.6.2 nathanw }
715 1.44.6.2 nathanw #endif
716 1.44.6.2 nathanw
717 1.44.6.2 nathanw /* Clean up chip itself */
718 1.44.6.2 nathanw if (dev->sc_flags & SBICF_SELECTED) {
719 1.44.6.2 nathanw while( asr & SBIC_ASR_DBR ) {
720 1.44.6.2 nathanw /* sbic is jammed w/data. need to clear it */
721 1.44.6.2 nathanw /* But we don't know what direction it needs to go */
722 1.44.6.2 nathanw GET_SBIC_data(regs, asr);
723 1.44.6.2 nathanw printf("%s: abort %s: clearing data buffer 0x%02x\n",
724 1.44.6.2 nathanw dev->sc_dev.dv_xname, where, asr);
725 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
726 1.44.6.2 nathanw if( asr & SBIC_ASR_DBR ) /* Not the read direction, then */
727 1.44.6.2 nathanw SET_SBIC_data(regs, asr);
728 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
729 1.44.6.2 nathanw }
730 1.44.6.2 nathanw WAIT_CIP(regs);
731 1.44.6.2 nathanw printf("%s: sbicabort - sending ABORT command\n", dev->sc_dev.dv_xname);
732 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
733 1.44.6.2 nathanw WAIT_CIP(regs);
734 1.44.6.2 nathanw
735 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
736 1.44.6.2 nathanw if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
737 1.44.6.2 nathanw /* ok, get more drastic.. */
738 1.44.6.2 nathanw
739 1.44.6.2 nathanw printf("%s: sbicabort - asr %x, trying to reset\n", dev->sc_dev.dv_xname, asr);
740 1.44.6.2 nathanw sbicreset(dev);
741 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
742 1.44.6.2 nathanw return -1;
743 1.44.6.2 nathanw }
744 1.44.6.2 nathanw printf("%s: sbicabort - sending DISC command\n", dev->sc_dev.dv_xname);
745 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_DISC);
746 1.44.6.2 nathanw
747 1.44.6.2 nathanw do {
748 1.44.6.2 nathanw asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
749 1.44.6.2 nathanw GET_SBIC_csr (regs, csr);
750 1.44.6.2 nathanw CSR_TRACE('a',csr,asr,0);
751 1.44.6.2 nathanw } while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
752 1.44.6.2 nathanw && (csr != SBIC_CSR_CMD_INVALID));
753 1.44.6.2 nathanw
754 1.44.6.2 nathanw /* lets just hope it worked.. */
755 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
756 1.44.6.2 nathanw }
757 1.44.6.2 nathanw return -1;
758 1.44.6.2 nathanw }
759 1.44.6.2 nathanw
760 1.44.6.2 nathanw
761 1.44.6.2 nathanw /*
762 1.44.6.2 nathanw * Initialize driver-private structures
763 1.44.6.2 nathanw */
764 1.44.6.2 nathanw
765 1.44.6.2 nathanw void
766 1.44.6.2 nathanw sbicinit(struct sbic_softc *dev)
767 1.44.6.2 nathanw {
768 1.44.6.2 nathanw sbic_regmap_t regs;
769 1.44.6.2 nathanw u_int i;
770 1.44.6.2 nathanw struct sbic_acb *acb;
771 1.44.6.2 nathanw u_int inhibit_sync;
772 1.44.6.2 nathanw
773 1.44.6.2 nathanw extern u_long scsi_nosync;
774 1.44.6.2 nathanw extern int shift_nosync;
775 1.44.6.2 nathanw
776 1.44.6.2 nathanw regs = dev->sc_sbic;
777 1.44.6.2 nathanw
778 1.44.6.2 nathanw if ((dev->sc_flags & SBICF_ALIVE) == 0) {
779 1.44.6.2 nathanw TAILQ_INIT(&dev->ready_list);
780 1.44.6.2 nathanw TAILQ_INIT(&dev->nexus_list);
781 1.44.6.2 nathanw TAILQ_INIT(&dev->free_list);
782 1.44.6.2 nathanw callout_init(&dev->sc_timo_ch);
783 1.44.6.2 nathanw dev->sc_nexus = NULL;
784 1.44.6.2 nathanw dev->sc_xs = NULL;
785 1.44.6.2 nathanw acb = dev->sc_acb;
786 1.44.6.2 nathanw bzero(acb, sizeof(dev->sc_acb));
787 1.44.6.2 nathanw for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
788 1.44.6.2 nathanw TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
789 1.44.6.2 nathanw acb++;
790 1.44.6.2 nathanw }
791 1.44.6.2 nathanw bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
792 1.44.6.2 nathanw #ifdef DEBUG
793 1.44.6.2 nathanw /* make sure timeout is really not needed */
794 1.44.6.2 nathanw callout_reset(&dev->sc_timo_ch, 30 * hz,
795 1.44.6.2 nathanw (void *)sbictimeout, dev);
796 1.44.6.2 nathanw #endif
797 1.44.6.2 nathanw
798 1.44.6.2 nathanw } else panic("sbic: reinitializing driver!");
799 1.44.6.2 nathanw
800 1.44.6.2 nathanw dev->sc_flags |= SBICF_ALIVE;
801 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
802 1.44.6.2 nathanw
803 1.44.6.2 nathanw /* initialize inhibit array */
804 1.44.6.2 nathanw if (scsi_nosync) {
805 1.44.6.2 nathanw inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
806 1.44.6.2 nathanw shift_nosync += 8;
807 1.44.6.2 nathanw #ifdef DEBUG
808 1.44.6.2 nathanw if (inhibit_sync)
809 1.44.6.2 nathanw printf("%s: Inhibiting synchronous transfer %02x\n",
810 1.44.6.2 nathanw dev->sc_dev.dv_xname, inhibit_sync);
811 1.44.6.2 nathanw #endif
812 1.44.6.2 nathanw for (i = 0; i < 8; ++i)
813 1.44.6.2 nathanw if (inhibit_sync & (1 << i))
814 1.44.6.2 nathanw sbic_inhibit_sync[i] = 1;
815 1.44.6.2 nathanw }
816 1.44.6.2 nathanw
817 1.44.6.2 nathanw sbicreset(dev);
818 1.44.6.2 nathanw }
819 1.44.6.2 nathanw
820 1.44.6.2 nathanw void
821 1.44.6.2 nathanw sbicreset(struct sbic_softc *dev)
822 1.44.6.2 nathanw {
823 1.44.6.2 nathanw sbic_regmap_t regs;
824 1.44.6.2 nathanw u_int my_id, s;
825 1.44.6.2 nathanw u_char csr;
826 1.44.6.2 nathanw #if 0
827 1.44.6.2 nathanw u_int i;
828 1.44.6.2 nathanw struct sbic_acb *acb;
829 1.44.6.2 nathanw #endif
830 1.44.6.2 nathanw
831 1.44.6.2 nathanw regs = dev->sc_sbic;
832 1.44.6.2 nathanw #if 0
833 1.44.6.2 nathanw if (dev->sc_flags & SBICF_ALIVE) {
834 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
835 1.44.6.2 nathanw WAIT_CIP(regs);
836 1.44.6.2 nathanw }
837 1.44.6.2 nathanw #else
838 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
839 1.44.6.2 nathanw WAIT_CIP(regs);
840 1.44.6.2 nathanw #endif
841 1.44.6.2 nathanw s = splbio();
842 1.44.6.2 nathanw my_id = dev->sc_channel.chan_id & SBIC_ID_MASK;
843 1.44.6.2 nathanw
844 1.44.6.2 nathanw /* Enable advanced mode */
845 1.44.6.2 nathanw my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
846 1.44.6.2 nathanw SET_SBIC_myid(regs, my_id);
847 1.44.6.2 nathanw
848 1.44.6.2 nathanw /*
849 1.44.6.2 nathanw * Disable interrupts (in dmainit) then reset the chip
850 1.44.6.2 nathanw */
851 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_RESET);
852 1.44.6.2 nathanw DELAY(25);
853 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
854 1.44.6.2 nathanw GET_SBIC_csr(regs, csr); /* clears interrupt also */
855 1.44.6.2 nathanw
856 1.44.6.2 nathanw if (dev->sc_clkfreq < 110)
857 1.44.6.2 nathanw my_id |= SBIC_ID_FS_8_10;
858 1.44.6.2 nathanw else if (dev->sc_clkfreq < 160)
859 1.44.6.2 nathanw my_id |= SBIC_ID_FS_12_15;
860 1.44.6.2 nathanw else if (dev->sc_clkfreq < 210)
861 1.44.6.2 nathanw my_id |= SBIC_ID_FS_16_20;
862 1.44.6.2 nathanw
863 1.44.6.2 nathanw SET_SBIC_myid(regs, my_id);
864 1.44.6.2 nathanw
865 1.44.6.2 nathanw /*
866 1.44.6.2 nathanw * Set up various chip parameters
867 1.44.6.2 nathanw */
868 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /* | SBIC_CTL_HSP */
869 1.44.6.2 nathanw | SBIC_MACHINE_DMA_MODE);
870 1.44.6.2 nathanw /*
871 1.44.6.2 nathanw * don't allow (re)selection (SBIC_RID_ES)
872 1.44.6.2 nathanw * until we can handle target mode!!
873 1.44.6.2 nathanw */
874 1.44.6.2 nathanw SET_SBIC_rselid(regs, SBIC_RID_ER);
875 1.44.6.2 nathanw SET_SBIC_syn(regs, 0); /* asynch for now */
876 1.44.6.2 nathanw
877 1.44.6.2 nathanw /*
878 1.44.6.2 nathanw * anything else was zeroed by reset
879 1.44.6.2 nathanw */
880 1.44.6.2 nathanw splx(s);
881 1.44.6.2 nathanw
882 1.44.6.2 nathanw #if 0
883 1.44.6.2 nathanw if ((dev->sc_flags & SBICF_ALIVE) == 0) {
884 1.44.6.2 nathanw TAILQ_INIT(&dev->ready_list);
885 1.44.6.2 nathanw TAILQ_INIT(&dev->nexus_list);
886 1.44.6.2 nathanw TAILQ_INIT(&dev->free_list);
887 1.44.6.2 nathanw dev->sc_nexus = NULL;
888 1.44.6.2 nathanw dev->sc_xs = NULL;
889 1.44.6.2 nathanw acb = dev->sc_acb;
890 1.44.6.2 nathanw bzero(acb, sizeof(dev->sc_acb));
891 1.44.6.2 nathanw for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
892 1.44.6.2 nathanw TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
893 1.44.6.2 nathanw acb++;
894 1.44.6.2 nathanw }
895 1.44.6.2 nathanw bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
896 1.44.6.2 nathanw } else {
897 1.44.6.2 nathanw if (dev->sc_nexus != NULL) {
898 1.44.6.2 nathanw dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
899 1.44.6.2 nathanw sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
900 1.44.6.2 nathanw }
901 1.44.6.2 nathanw while (acb = dev->nexus_list.tqh_first) {
902 1.44.6.2 nathanw acb->xs->error = XS_DRIVER_STUFFUP;
903 1.44.6.2 nathanw sbic_scsidone(acb, -1 /*acb->stat[0]*/);
904 1.44.6.2 nathanw }
905 1.44.6.2 nathanw }
906 1.44.6.2 nathanw
907 1.44.6.2 nathanw dev->sc_flags |= SBICF_ALIVE;
908 1.44.6.2 nathanw #endif
909 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
910 1.44.6.2 nathanw }
911 1.44.6.2 nathanw
912 1.44.6.2 nathanw void
913 1.44.6.2 nathanw sbicerror(struct sbic_softc *dev, sbic_regmap_t regs, u_char csr)
914 1.44.6.2 nathanw {
915 1.44.6.2 nathanw struct scsipi_xfer *xs;
916 1.44.6.2 nathanw
917 1.44.6.2 nathanw xs = dev->sc_xs;
918 1.44.6.2 nathanw
919 1.44.6.2 nathanw #ifdef DIAGNOSTIC
920 1.44.6.2 nathanw if (xs == NULL)
921 1.44.6.2 nathanw panic("sbicerror");
922 1.44.6.2 nathanw #endif
923 1.44.6.2 nathanw if (xs->xs_control & XS_CTL_SILENT)
924 1.44.6.2 nathanw return;
925 1.44.6.2 nathanw
926 1.44.6.2 nathanw printf("%s: ", dev->sc_dev.dv_xname);
927 1.44.6.2 nathanw printf("csr == 0x%02x\n", csr); /* XXX */
928 1.44.6.2 nathanw }
929 1.44.6.2 nathanw
930 1.44.6.2 nathanw /*
931 1.44.6.2 nathanw * select the bus, return when selected or error.
932 1.44.6.2 nathanw */
933 1.44.6.2 nathanw int
934 1.44.6.2 nathanw sbicselectbus(struct sbic_softc *dev, sbic_regmap_t regs, u_char target,
935 1.44.6.2 nathanw u_char lun, u_char our_addr)
936 1.44.6.2 nathanw {
937 1.44.6.2 nathanw u_char asr, csr, id;
938 1.44.6.2 nathanw
939 1.44.6.2 nathanw SBIC_TRACE(dev);
940 1.44.6.2 nathanw QPRINTF(("sbicselectbus %d\n", target));
941 1.44.6.2 nathanw
942 1.44.6.2 nathanw /*
943 1.44.6.2 nathanw * if we're already selected, return (XXXX panic maybe?)
944 1.44.6.2 nathanw */
945 1.44.6.2 nathanw if (dev->sc_flags & SBICF_SELECTED) {
946 1.44.6.2 nathanw SBIC_TRACE(dev);
947 1.44.6.2 nathanw return(1);
948 1.44.6.2 nathanw }
949 1.44.6.2 nathanw
950 1.44.6.2 nathanw /*
951 1.44.6.2 nathanw * issue select
952 1.44.6.2 nathanw */
953 1.44.6.2 nathanw SBIC_TC_PUT(regs, 0);
954 1.44.6.2 nathanw SET_SBIC_selid(regs, target);
955 1.44.6.2 nathanw SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
956 1.44.6.2 nathanw
957 1.44.6.2 nathanw /*
958 1.44.6.2 nathanw * set sync or async
959 1.44.6.2 nathanw */
960 1.44.6.2 nathanw if (dev->sc_sync[target].state == SYNC_DONE)
961 1.44.6.2 nathanw SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
962 1.44.6.2 nathanw dev->sc_sync[target].period));
963 1.44.6.2 nathanw else
964 1.44.6.2 nathanw SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
965 1.44.6.2 nathanw
966 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
967 1.44.6.2 nathanw if( asr & (SBIC_ASR_INT|SBIC_ASR_BSY) ) {
968 1.44.6.2 nathanw /* This means we got ourselves reselected upon */
969 1.44.6.2 nathanw /* printf("sbicselectbus: INT/BSY asr %02x\n", asr);*/
970 1.44.6.2 nathanw #ifdef DDB
971 1.44.6.2 nathanw /* Debugger();*/
972 1.44.6.2 nathanw #endif
973 1.44.6.2 nathanw SBIC_TRACE(dev);
974 1.44.6.2 nathanw return 1;
975 1.44.6.2 nathanw }
976 1.44.6.2 nathanw
977 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
978 1.44.6.2 nathanw
979 1.44.6.2 nathanw /*
980 1.44.6.2 nathanw * wait for select (merged from separate function may need
981 1.44.6.2 nathanw * cleanup)
982 1.44.6.2 nathanw */
983 1.44.6.2 nathanw WAIT_CIP(regs);
984 1.44.6.2 nathanw do {
985 1.44.6.2 nathanw asr = SBIC_WAIT(regs, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
986 1.44.6.2 nathanw if (asr & SBIC_ASR_LCI) {
987 1.44.6.2 nathanw #ifdef DEBUG
988 1.44.6.2 nathanw if (reselect_debug)
989 1.44.6.2 nathanw printf("sbicselectbus: late LCI asr %02x\n", asr);
990 1.44.6.2 nathanw #endif
991 1.44.6.2 nathanw SBIC_TRACE(dev);
992 1.44.6.2 nathanw return 1;
993 1.44.6.2 nathanw }
994 1.44.6.2 nathanw GET_SBIC_csr (regs, csr);
995 1.44.6.2 nathanw CSR_TRACE('s',csr,asr,target);
996 1.44.6.2 nathanw QPRINTF(("%02x ", csr));
997 1.44.6.2 nathanw if( csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
998 1.44.6.2 nathanw #ifdef DEBUG
999 1.44.6.2 nathanw if(reselect_debug)
1000 1.44.6.2 nathanw printf("sbicselectbus: reselected asr %02x\n", asr);
1001 1.44.6.2 nathanw #endif
1002 1.44.6.2 nathanw /* We need to handle this now so we don't lock up later */
1003 1.44.6.2 nathanw sbicnextstate(dev, csr, asr);
1004 1.44.6.2 nathanw SBIC_TRACE(dev);
1005 1.44.6.2 nathanw return 1;
1006 1.44.6.2 nathanw }
1007 1.44.6.2 nathanw if( csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
1008 1.44.6.2 nathanw panic("sbicselectbus: target issued select!");
1009 1.44.6.2 nathanw return 1;
1010 1.44.6.2 nathanw }
1011 1.44.6.2 nathanw } while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
1012 1.44.6.2 nathanw && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
1013 1.44.6.2 nathanw
1014 1.44.6.2 nathanw /* Enable (or not) reselection */
1015 1.44.6.2 nathanw if(!sbic_enable_reselect && dev->nexus_list.tqh_first == NULL)
1016 1.44.6.2 nathanw SET_SBIC_rselid (regs, 0);
1017 1.44.6.2 nathanw else
1018 1.44.6.2 nathanw SET_SBIC_rselid (regs, SBIC_RID_ER);
1019 1.44.6.2 nathanw
1020 1.44.6.2 nathanw if (csr == (SBIC_CSR_MIS_2|CMD_PHASE)) {
1021 1.44.6.2 nathanw dev->sc_flags |= SBICF_SELECTED; /* device ignored ATN */
1022 1.44.6.2 nathanw GET_SBIC_selid(regs, id);
1023 1.44.6.2 nathanw dev->target = id;
1024 1.44.6.2 nathanw GET_SBIC_tlun(regs,dev->lun);
1025 1.44.6.2 nathanw if( dev->lun & SBIC_TLUN_VALID )
1026 1.44.6.2 nathanw dev->lun &= SBIC_TLUN_MASK;
1027 1.44.6.2 nathanw else
1028 1.44.6.2 nathanw dev->lun = lun;
1029 1.44.6.2 nathanw } else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
1030 1.44.6.2 nathanw /*
1031 1.44.6.2 nathanw * Send identify message
1032 1.44.6.2 nathanw * (SCSI-2 requires an identify msg (?))
1033 1.44.6.2 nathanw */
1034 1.44.6.2 nathanw GET_SBIC_selid(regs, id);
1035 1.44.6.2 nathanw dev->target = id;
1036 1.44.6.2 nathanw GET_SBIC_tlun(regs,dev->lun);
1037 1.44.6.2 nathanw if( dev->lun & SBIC_TLUN_VALID )
1038 1.44.6.2 nathanw dev->lun &= SBIC_TLUN_MASK;
1039 1.44.6.2 nathanw else
1040 1.44.6.2 nathanw dev->lun = lun;
1041 1.44.6.2 nathanw /*
1042 1.44.6.2 nathanw * handle drives that don't want to be asked
1043 1.44.6.2 nathanw * whether to go sync at all.
1044 1.44.6.2 nathanw */
1045 1.44.6.2 nathanw if (sbic_inhibit_sync[id]
1046 1.44.6.2 nathanw && dev->sc_sync[id].state == SYNC_START) {
1047 1.44.6.2 nathanw #ifdef DEBUG
1048 1.44.6.2 nathanw if (sync_debug)
1049 1.44.6.2 nathanw printf("Forcing target %d asynchronous.\n", id);
1050 1.44.6.2 nathanw #endif
1051 1.44.6.2 nathanw dev->sc_sync[id].offset = 0;
1052 1.44.6.2 nathanw dev->sc_sync[id].period = sbic_min_period;
1053 1.44.6.2 nathanw dev->sc_sync[id].state = SYNC_DONE;
1054 1.44.6.2 nathanw }
1055 1.44.6.2 nathanw
1056 1.44.6.2 nathanw
1057 1.44.6.2 nathanw if (dev->sc_sync[id].state != SYNC_START){
1058 1.44.6.2 nathanw if( dev->sc_xs->xs_control & XS_CTL_POLL
1059 1.44.6.2 nathanw || (dev->sc_flags & SBICF_ICMD)
1060 1.44.6.2 nathanw || !sbic_enable_reselect )
1061 1.44.6.2 nathanw SEND_BYTE (regs, MSG_IDENTIFY | lun);
1062 1.44.6.2 nathanw else
1063 1.44.6.2 nathanw SEND_BYTE (regs, MSG_IDENTIFY_DR | lun);
1064 1.44.6.2 nathanw } else {
1065 1.44.6.2 nathanw /*
1066 1.44.6.2 nathanw * try to initiate a sync transfer.
1067 1.44.6.2 nathanw * So compose the sync message we're going
1068 1.44.6.2 nathanw * to send to the target
1069 1.44.6.2 nathanw */
1070 1.44.6.2 nathanw
1071 1.44.6.2 nathanw #ifdef DEBUG
1072 1.44.6.2 nathanw if (sync_debug)
1073 1.44.6.2 nathanw printf("Sending sync request to target %d ... ",
1074 1.44.6.2 nathanw id);
1075 1.44.6.2 nathanw #endif
1076 1.44.6.2 nathanw /*
1077 1.44.6.2 nathanw * setup scsi message sync message request
1078 1.44.6.2 nathanw */
1079 1.44.6.2 nathanw dev->sc_msg[0] = MSG_IDENTIFY | lun;
1080 1.44.6.2 nathanw dev->sc_msg[1] = MSG_EXT_MESSAGE;
1081 1.44.6.2 nathanw dev->sc_msg[2] = 3;
1082 1.44.6.2 nathanw dev->sc_msg[3] = MSG_SYNC_REQ;
1083 1.44.6.2 nathanw dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
1084 1.44.6.2 nathanw sbic_min_period);
1085 1.44.6.2 nathanw dev->sc_msg[5] = sbic_max_offset;
1086 1.44.6.2 nathanw
1087 1.44.6.2 nathanw if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
1088 1.44.6.2 nathanw sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
1089 1.44.6.2 nathanw
1090 1.44.6.2 nathanw dev->sc_sync[id].state = SYNC_SENT;
1091 1.44.6.2 nathanw #ifdef DEBUG
1092 1.44.6.2 nathanw if (sync_debug)
1093 1.44.6.2 nathanw printf ("sent\n");
1094 1.44.6.2 nathanw #endif
1095 1.44.6.2 nathanw }
1096 1.44.6.2 nathanw
1097 1.44.6.2 nathanw asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
1098 1.44.6.2 nathanw GET_SBIC_csr (regs, csr);
1099 1.44.6.2 nathanw CSR_TRACE('y',csr,asr,target);
1100 1.44.6.2 nathanw QPRINTF(("[%02x]", csr));
1101 1.44.6.2 nathanw #ifdef DEBUG
1102 1.44.6.2 nathanw if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
1103 1.44.6.2 nathanw printf("csr-result of last msgout: 0x%x\n", csr);
1104 1.44.6.2 nathanw #endif
1105 1.44.6.2 nathanw
1106 1.44.6.2 nathanw if (csr != SBIC_CSR_SEL_TIMEO)
1107 1.44.6.2 nathanw dev->sc_flags |= SBICF_SELECTED;
1108 1.44.6.2 nathanw }
1109 1.44.6.2 nathanw if (csr == SBIC_CSR_SEL_TIMEO)
1110 1.44.6.2 nathanw dev->sc_xs->error = XS_SELTIMEOUT;
1111 1.44.6.2 nathanw
1112 1.44.6.2 nathanw QPRINTF(("\n"));
1113 1.44.6.2 nathanw
1114 1.44.6.2 nathanw SBIC_TRACE(dev);
1115 1.44.6.2 nathanw return(csr == SBIC_CSR_SEL_TIMEO);
1116 1.44.6.2 nathanw }
1117 1.44.6.2 nathanw
1118 1.44.6.2 nathanw int
1119 1.44.6.2 nathanw sbicxfstart(sbic_regmap_t regs, int len, u_char phase, int wait)
1120 1.44.6.2 nathanw {
1121 1.44.6.2 nathanw u_char id;
1122 1.44.6.2 nathanw
1123 1.44.6.2 nathanw switch (phase) {
1124 1.44.6.2 nathanw case DATA_IN_PHASE:
1125 1.44.6.2 nathanw case MESG_IN_PHASE:
1126 1.44.6.2 nathanw GET_SBIC_selid (regs, id);
1127 1.44.6.2 nathanw id |= SBIC_SID_FROM_SCSI;
1128 1.44.6.2 nathanw SET_SBIC_selid (regs, id);
1129 1.44.6.2 nathanw SBIC_TC_PUT (regs, (unsigned)len);
1130 1.44.6.2 nathanw break;
1131 1.44.6.2 nathanw case DATA_OUT_PHASE:
1132 1.44.6.2 nathanw case MESG_OUT_PHASE:
1133 1.44.6.2 nathanw case CMD_PHASE:
1134 1.44.6.2 nathanw GET_SBIC_selid (regs, id);
1135 1.44.6.2 nathanw id &= ~SBIC_SID_FROM_SCSI;
1136 1.44.6.2 nathanw SET_SBIC_selid (regs, id);
1137 1.44.6.2 nathanw SBIC_TC_PUT (regs, (unsigned)len);
1138 1.44.6.2 nathanw break;
1139 1.44.6.2 nathanw default:
1140 1.44.6.2 nathanw SBIC_TC_PUT (regs, 0);
1141 1.44.6.2 nathanw }
1142 1.44.6.2 nathanw QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
1143 1.44.6.2 nathanw
1144 1.44.6.2 nathanw return(1);
1145 1.44.6.2 nathanw }
1146 1.44.6.2 nathanw
1147 1.44.6.2 nathanw int
1148 1.44.6.2 nathanw sbicxfout(sbic_regmap_t regs, int len, void *bp, int phase)
1149 1.44.6.2 nathanw {
1150 1.44.6.2 nathanw u_char orig_csr, asr, *buf;
1151 1.44.6.2 nathanw int wait;
1152 1.44.6.2 nathanw
1153 1.44.6.2 nathanw buf = bp;
1154 1.44.6.2 nathanw wait = sbic_data_wait;
1155 1.44.6.2 nathanw
1156 1.44.6.2 nathanw QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
1157 1.44.6.2 nathanw "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
1158 1.44.6.2 nathanw buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
1159 1.44.6.2 nathanw
1160 1.44.6.2 nathanw GET_SBIC_csr (regs, orig_csr);
1161 1.44.6.2 nathanw CSR_TRACE('>',orig_csr,0,0);
1162 1.44.6.2 nathanw
1163 1.44.6.2 nathanw /*
1164 1.44.6.2 nathanw * sigh.. WD-PROTO strikes again.. sending the command in one go
1165 1.44.6.2 nathanw * causes the chip to lock up if talking to certain (misbehaving?)
1166 1.44.6.2 nathanw * targets. Anyway, this procedure should work for all targets, but
1167 1.44.6.2 nathanw * it's slightly slower due to the overhead
1168 1.44.6.2 nathanw */
1169 1.44.6.2 nathanw WAIT_CIP (regs);
1170 1.44.6.2 nathanw SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1171 1.44.6.2 nathanw for (;len > 0; len--) {
1172 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1173 1.44.6.2 nathanw while ((asr & SBIC_ASR_DBR) == 0) {
1174 1.44.6.2 nathanw if ((asr & SBIC_ASR_INT) || --wait < 0) {
1175 1.44.6.2 nathanw #ifdef DEBUG
1176 1.44.6.2 nathanw if (sbic_debug)
1177 1.44.6.2 nathanw printf("sbicxfout fail: l%d i%x w%d\n",
1178 1.44.6.2 nathanw len, asr, wait);
1179 1.44.6.2 nathanw #endif
1180 1.44.6.2 nathanw return (len);
1181 1.44.6.2 nathanw }
1182 1.44.6.2 nathanw /* DELAY(1);*/
1183 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1184 1.44.6.2 nathanw }
1185 1.44.6.2 nathanw
1186 1.44.6.2 nathanw SET_SBIC_data (regs, *buf);
1187 1.44.6.2 nathanw buf++;
1188 1.44.6.2 nathanw }
1189 1.44.6.2 nathanw SBIC_TC_GET(regs, len);
1190 1.44.6.2 nathanw QPRINTF(("sbicxfout done %d bytes\n", len));
1191 1.44.6.2 nathanw /*
1192 1.44.6.2 nathanw * this leaves with one csr to be read
1193 1.44.6.2 nathanw */
1194 1.44.6.2 nathanw return(0);
1195 1.44.6.2 nathanw }
1196 1.44.6.2 nathanw
1197 1.44.6.2 nathanw /* returns # bytes left to read */
1198 1.44.6.2 nathanw int
1199 1.44.6.2 nathanw sbicxfin(sbic_regmap_t regs, int len, void *bp)
1200 1.44.6.2 nathanw {
1201 1.44.6.2 nathanw int wait;
1202 1.44.6.2 nathanw u_char *obp, *buf;
1203 1.44.6.2 nathanw u_char orig_csr, csr, asr;
1204 1.44.6.2 nathanw
1205 1.44.6.2 nathanw wait = sbic_data_wait;
1206 1.44.6.2 nathanw obp = bp;
1207 1.44.6.2 nathanw buf = bp;
1208 1.44.6.2 nathanw
1209 1.44.6.2 nathanw GET_SBIC_csr (regs, orig_csr);
1210 1.44.6.2 nathanw CSR_TRACE('<',orig_csr,0,0);
1211 1.44.6.2 nathanw
1212 1.44.6.2 nathanw QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
1213 1.44.6.2 nathanw
1214 1.44.6.2 nathanw WAIT_CIP (regs);
1215 1.44.6.2 nathanw SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
1216 1.44.6.2 nathanw for (;len > 0; len--) {
1217 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1218 1.44.6.2 nathanw if((asr & SBIC_ASR_PE)) {
1219 1.44.6.2 nathanw #ifdef DEBUG
1220 1.44.6.2 nathanw printf("sbicxfin parity error: l%d i%x w%d\n",
1221 1.44.6.2 nathanw len, asr, wait);
1222 1.44.6.2 nathanw /* return ((unsigned long)buf - (unsigned long)bp); */
1223 1.44.6.2 nathanw #ifdef DDB
1224 1.44.6.2 nathanw Debugger();
1225 1.44.6.2 nathanw #endif
1226 1.44.6.2 nathanw #endif
1227 1.44.6.2 nathanw }
1228 1.44.6.2 nathanw while ((asr & SBIC_ASR_DBR) == 0) {
1229 1.44.6.2 nathanw if ((asr & SBIC_ASR_INT) || --wait < 0) {
1230 1.44.6.2 nathanw #ifdef DEBUG
1231 1.44.6.2 nathanw if (sbic_debug) {
1232 1.44.6.2 nathanw QPRINTF(("sbicxfin fail:{%d} %02x %02x %02x %02x %02x %02x "
1233 1.44.6.2 nathanw "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1234 1.44.6.2 nathanw obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1235 1.44.6.2 nathanw printf("sbicxfin fail: l%d i%x w%d\n",
1236 1.44.6.2 nathanw len, asr, wait);
1237 1.44.6.2 nathanw }
1238 1.44.6.2 nathanw #endif
1239 1.44.6.2 nathanw return len;
1240 1.44.6.2 nathanw }
1241 1.44.6.2 nathanw
1242 1.44.6.2 nathanw if( ! asr & SBIC_ASR_BSY ) {
1243 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1244 1.44.6.2 nathanw CSR_TRACE('<',csr,asr,len);
1245 1.44.6.2 nathanw QPRINTF(("[CSR%02xASR%02x]", csr, asr));
1246 1.44.6.2 nathanw }
1247 1.44.6.2 nathanw
1248 1.44.6.2 nathanw /* DELAY(1);*/
1249 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1250 1.44.6.2 nathanw }
1251 1.44.6.2 nathanw
1252 1.44.6.2 nathanw GET_SBIC_data (regs, *buf);
1253 1.44.6.2 nathanw /* QPRINTF(("asr=%02x, csr=%02x, data=%02x\n", asr, csr, *buf));*/
1254 1.44.6.2 nathanw buf++;
1255 1.44.6.2 nathanw }
1256 1.44.6.2 nathanw
1257 1.44.6.2 nathanw QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
1258 1.44.6.2 nathanw "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
1259 1.44.6.2 nathanw obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
1260 1.44.6.2 nathanw
1261 1.44.6.2 nathanw /* this leaves with one csr to be read */
1262 1.44.6.2 nathanw return len;
1263 1.44.6.2 nathanw }
1264 1.44.6.2 nathanw
1265 1.44.6.2 nathanw /*
1266 1.44.6.2 nathanw * SCSI 'immediate' command: issue a command to some SCSI device
1267 1.44.6.2 nathanw * and get back an 'immediate' response (i.e., do programmed xfer
1268 1.44.6.2 nathanw * to get the response data). 'cbuf' is a buffer containing a scsi
1269 1.44.6.2 nathanw * command of length clen bytes. 'buf' is a buffer of length 'len'
1270 1.44.6.2 nathanw * bytes for data. The transfer direction is determined by the device
1271 1.44.6.2 nathanw * (i.e., by the scsi bus data xfer phase). If 'len' is zero, the
1272 1.44.6.2 nathanw * command must supply no data.
1273 1.44.6.2 nathanw */
1274 1.44.6.2 nathanw int
1275 1.44.6.2 nathanw sbicicmd(struct sbic_softc *dev, int target, int lun, void *cbuf, int clen,
1276 1.44.6.2 nathanw void *buf, int len)
1277 1.44.6.2 nathanw {
1278 1.44.6.2 nathanw sbic_regmap_t regs;
1279 1.44.6.2 nathanw u_char phase, csr, asr;
1280 1.44.6.2 nathanw int wait, i;
1281 1.44.6.2 nathanw struct sbic_acb *acb;
1282 1.44.6.2 nathanw
1283 1.44.6.2 nathanw #define CSR_LOG_BUF_SIZE 0
1284 1.44.6.2 nathanw #if CSR_LOG_BUF_SIZE
1285 1.44.6.2 nathanw int bufptr;
1286 1.44.6.2 nathanw int csrbuf[CSR_LOG_BUF_SIZE];
1287 1.44.6.2 nathanw bufptr=0;
1288 1.44.6.2 nathanw #endif
1289 1.44.6.2 nathanw
1290 1.44.6.2 nathanw SBIC_TRACE(dev);
1291 1.44.6.2 nathanw regs = dev->sc_sbic;
1292 1.44.6.2 nathanw acb = dev->sc_nexus;
1293 1.44.6.2 nathanw
1294 1.44.6.2 nathanw /* Make sure pointers are OK */
1295 1.44.6.2 nathanw dev->sc_last = dev->sc_cur = &acb->sc_pa;
1296 1.44.6.2 nathanw dev->sc_tcnt = acb->sc_tcnt = 0;
1297 1.44.6.2 nathanw acb->sc_pa.dc_count = 0; /* No DMA */
1298 1.44.6.2 nathanw acb->sc_kv.dc_addr = buf;
1299 1.44.6.2 nathanw acb->sc_kv.dc_count = len;
1300 1.44.6.2 nathanw
1301 1.44.6.2 nathanw #ifdef DEBUG
1302 1.44.6.2 nathanw routine = 3;
1303 1.44.6.2 nathanw debug_sbic_regs = regs; /* store this to allow debug calls */
1304 1.44.6.2 nathanw if( data_pointer_debug > 1 )
1305 1.44.6.2 nathanw printf("sbicicmd(%d,%d):%d\n", target, lun,
1306 1.44.6.2 nathanw acb->sc_kv.dc_count);
1307 1.44.6.2 nathanw #endif
1308 1.44.6.2 nathanw
1309 1.44.6.2 nathanw /*
1310 1.44.6.2 nathanw * set the sbic into non-DMA mode
1311 1.44.6.2 nathanw */
1312 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /*| SBIC_CTL_HSP*/);
1313 1.44.6.2 nathanw
1314 1.44.6.2 nathanw dev->sc_stat[0] = 0xff;
1315 1.44.6.2 nathanw dev->sc_msg[0] = 0xff;
1316 1.44.6.2 nathanw i = 1; /* pre-load */
1317 1.44.6.2 nathanw
1318 1.44.6.2 nathanw /* We're stealing the SCSI bus */
1319 1.44.6.2 nathanw dev->sc_flags |= SBICF_ICMD;
1320 1.44.6.2 nathanw
1321 1.44.6.2 nathanw do {
1322 1.44.6.2 nathanw /*
1323 1.44.6.2 nathanw * select the SCSI bus (it's an error if bus isn't free)
1324 1.44.6.2 nathanw */
1325 1.44.6.2 nathanw if (!( dev->sc_flags & SBICF_SELECTED )
1326 1.44.6.2 nathanw && sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr)) {
1327 1.44.6.2 nathanw /* printf("sbicicmd: trying to select busy bus!\n"); */
1328 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_ICMD;
1329 1.44.6.2 nathanw return(-1);
1330 1.44.6.2 nathanw }
1331 1.44.6.2 nathanw
1332 1.44.6.2 nathanw /*
1333 1.44.6.2 nathanw * Wait for a phase change (or error) then let the device sequence
1334 1.44.6.2 nathanw * us through the various SCSI phases.
1335 1.44.6.2 nathanw */
1336 1.44.6.2 nathanw
1337 1.44.6.2 nathanw wait = sbic_cmd_wait;
1338 1.44.6.2 nathanw
1339 1.44.6.3 nathanw GET_SBIC_asr (regs, asr);
1340 1.44.6.2 nathanw GET_SBIC_csr (regs, csr);
1341 1.44.6.2 nathanw CSR_TRACE('I',csr,asr,target);
1342 1.44.6.2 nathanw QPRINTF((">ASR:%02xCSR:%02x<", asr, csr));
1343 1.44.6.2 nathanw
1344 1.44.6.2 nathanw #if CSR_LOG_BUF_SIZE
1345 1.44.6.2 nathanw csrbuf[bufptr++] = csr;
1346 1.44.6.2 nathanw #endif
1347 1.44.6.2 nathanw
1348 1.44.6.2 nathanw
1349 1.44.6.2 nathanw switch (csr) {
1350 1.44.6.2 nathanw case SBIC_CSR_S_XFERRED:
1351 1.44.6.2 nathanw case SBIC_CSR_DISC:
1352 1.44.6.2 nathanw case SBIC_CSR_DISC_1:
1353 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
1354 1.44.6.2 nathanw GET_SBIC_cmd_phase (regs, phase);
1355 1.44.6.2 nathanw if (phase == 0x60) {
1356 1.44.6.2 nathanw GET_SBIC_tlun (regs, dev->sc_stat[0]);
1357 1.44.6.2 nathanw i = 0; /* done */
1358 1.44.6.2 nathanw /* break; */ /* Bypass all the state gobldygook */
1359 1.44.6.2 nathanw } else {
1360 1.44.6.2 nathanw #ifdef DEBUG
1361 1.44.6.2 nathanw if(reselect_debug>1)
1362 1.44.6.2 nathanw printf("sbicicmd: handling disconnect\n");
1363 1.44.6.2 nathanw #endif
1364 1.44.6.2 nathanw i = SBIC_STATE_DISCONNECT;
1365 1.44.6.2 nathanw }
1366 1.44.6.2 nathanw break;
1367 1.44.6.2 nathanw
1368 1.44.6.2 nathanw case SBIC_CSR_XFERRED|CMD_PHASE:
1369 1.44.6.2 nathanw case SBIC_CSR_MIS|CMD_PHASE:
1370 1.44.6.2 nathanw case SBIC_CSR_MIS_1|CMD_PHASE:
1371 1.44.6.2 nathanw case SBIC_CSR_MIS_2|CMD_PHASE:
1372 1.44.6.2 nathanw if (sbicxfstart(regs, clen, CMD_PHASE, sbic_cmd_wait))
1373 1.44.6.2 nathanw if (sbicxfout(regs, clen,
1374 1.44.6.2 nathanw cbuf, CMD_PHASE))
1375 1.44.6.2 nathanw i = sbicabort(dev, regs,"icmd sending cmd");
1376 1.44.6.2 nathanw #if 0
1377 1.44.6.2 nathanw GET_SBIC_csr(regs, csr); /* Lets us reload tcount */
1378 1.44.6.2 nathanw WAIT_CIP(regs);
1379 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1380 1.44.6.2 nathanw CSR_TRACE('I',csr,asr,target);
1381 1.44.6.2 nathanw if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
1382 1.44.6.2 nathanw printf("next: cmd sent asr %02x, csr %02x\n",
1383 1.44.6.2 nathanw asr, csr);
1384 1.44.6.2 nathanw #endif
1385 1.44.6.2 nathanw break;
1386 1.44.6.2 nathanw
1387 1.44.6.2 nathanw #if 0
1388 1.44.6.2 nathanw case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
1389 1.44.6.2 nathanw case SBIC_CSR_XFERRED|DATA_IN_PHASE:
1390 1.44.6.2 nathanw case SBIC_CSR_MIS|DATA_OUT_PHASE:
1391 1.44.6.2 nathanw case SBIC_CSR_MIS|DATA_IN_PHASE:
1392 1.44.6.2 nathanw case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
1393 1.44.6.2 nathanw case SBIC_CSR_MIS_1|DATA_IN_PHASE:
1394 1.44.6.2 nathanw case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
1395 1.44.6.2 nathanw case SBIC_CSR_MIS_2|DATA_IN_PHASE:
1396 1.44.6.2 nathanw if (acb->sc_kv.dc_count <= 0)
1397 1.44.6.2 nathanw i = sbicabort(dev, regs, "icmd out of data");
1398 1.44.6.2 nathanw else {
1399 1.44.6.2 nathanw wait = sbic_data_wait;
1400 1.44.6.2 nathanw if (sbicxfstart(regs,
1401 1.44.6.2 nathanw acb->sc_kv.dc_count,
1402 1.44.6.2 nathanw SBIC_PHASE(csr), wait))
1403 1.44.6.2 nathanw if (csr & 0x01)
1404 1.44.6.2 nathanw /* data in? */
1405 1.44.6.2 nathanw i=sbicxfin(regs,
1406 1.44.6.2 nathanw acb->sc_kv.dc_count,
1407 1.44.6.2 nathanw acb->sc_kv.dc_addr);
1408 1.44.6.2 nathanw else
1409 1.44.6.2 nathanw i=sbicxfout(regs,
1410 1.44.6.2 nathanw acb->sc_kv.dc_count,
1411 1.44.6.2 nathanw acb->sc_kv.dc_addr,
1412 1.44.6.2 nathanw SBIC_PHASE(csr));
1413 1.44.6.2 nathanw acb->sc_kv.dc_addr +=
1414 1.44.6.2 nathanw (acb->sc_kv.dc_count - i);
1415 1.44.6.2 nathanw acb->sc_kv.dc_count = i;
1416 1.44.6.2 nathanw i = 1;
1417 1.44.6.2 nathanw }
1418 1.44.6.2 nathanw break;
1419 1.44.6.2 nathanw
1420 1.44.6.2 nathanw #endif
1421 1.44.6.2 nathanw case SBIC_CSR_XFERRED|STATUS_PHASE:
1422 1.44.6.2 nathanw case SBIC_CSR_MIS|STATUS_PHASE:
1423 1.44.6.2 nathanw case SBIC_CSR_MIS_1|STATUS_PHASE:
1424 1.44.6.2 nathanw case SBIC_CSR_MIS_2|STATUS_PHASE:
1425 1.44.6.2 nathanw /*
1426 1.44.6.2 nathanw * the sbic does the status/cmd-complete reading ok,
1427 1.44.6.2 nathanw * so do this with its hi-level commands.
1428 1.44.6.2 nathanw */
1429 1.44.6.2 nathanw #ifdef DEBUG
1430 1.44.6.2 nathanw if(sbic_debug)
1431 1.44.6.2 nathanw printf("SBICICMD status phase\n");
1432 1.44.6.2 nathanw #endif
1433 1.44.6.2 nathanw SBIC_TC_PUT(regs, 0);
1434 1.44.6.2 nathanw SET_SBIC_cmd_phase(regs, 0x46);
1435 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1436 1.44.6.2 nathanw break;
1437 1.44.6.2 nathanw
1438 1.44.6.2 nathanw #if THIS_IS_A_RESERVED_STATE
1439 1.44.6.2 nathanw case BUS_FREE_PHASE: /* This is not legal */
1440 1.44.6.2 nathanw if( dev->sc_stat[0] != 0xff )
1441 1.44.6.2 nathanw goto out;
1442 1.44.6.2 nathanw break;
1443 1.44.6.2 nathanw #endif
1444 1.44.6.2 nathanw
1445 1.44.6.2 nathanw default:
1446 1.44.6.2 nathanw i = sbicnextstate(dev, csr, asr);
1447 1.44.6.2 nathanw }
1448 1.44.6.2 nathanw
1449 1.44.6.2 nathanw /*
1450 1.44.6.2 nathanw * make sure the last command was taken,
1451 1.44.6.2 nathanw * ie. we're not hunting after an ignored command..
1452 1.44.6.2 nathanw */
1453 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1454 1.44.6.2 nathanw
1455 1.44.6.2 nathanw /* tapes may take a loooong time.. */
1456 1.44.6.2 nathanw while (asr & SBIC_ASR_BSY){
1457 1.44.6.2 nathanw if(asr & SBIC_ASR_DBR) {
1458 1.44.6.2 nathanw printf("sbicicmd: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
1459 1.44.6.2 nathanw csr,asr);
1460 1.44.6.2 nathanw #ifdef DDB
1461 1.44.6.2 nathanw Debugger();
1462 1.44.6.2 nathanw #endif
1463 1.44.6.2 nathanw /* SBIC is jammed */
1464 1.44.6.2 nathanw /* DUNNO which direction */
1465 1.44.6.2 nathanw /* Try old direction */
1466 1.44.6.2 nathanw GET_SBIC_data(regs,i);
1467 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1468 1.44.6.2 nathanw if( asr & SBIC_ASR_DBR) /* Wants us to write */
1469 1.44.6.2 nathanw SET_SBIC_data(regs,i);
1470 1.44.6.2 nathanw }
1471 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1472 1.44.6.2 nathanw }
1473 1.44.6.2 nathanw
1474 1.44.6.2 nathanw /*
1475 1.44.6.2 nathanw * wait for last command to complete
1476 1.44.6.2 nathanw */
1477 1.44.6.2 nathanw if (asr & SBIC_ASR_LCI) {
1478 1.44.6.2 nathanw printf("sbicicmd: last command ignored\n");
1479 1.44.6.2 nathanw }
1480 1.44.6.2 nathanw else if( i == 1 ) /* Bsy */
1481 1.44.6.2 nathanw SBIC_WAIT (regs, SBIC_ASR_INT, wait);
1482 1.44.6.2 nathanw
1483 1.44.6.2 nathanw /*
1484 1.44.6.2 nathanw * do it again
1485 1.44.6.2 nathanw */
1486 1.44.6.2 nathanw } while ( i > 0 && dev->sc_stat[0] == 0xff);
1487 1.44.6.2 nathanw
1488 1.44.6.2 nathanw /* Sometimes we need to do an extra read of the CSR */
1489 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1490 1.44.6.2 nathanw CSR_TRACE('I',csr,asr,0xff);
1491 1.44.6.2 nathanw
1492 1.44.6.2 nathanw #if CSR_LOG_BUF_SIZE
1493 1.44.6.2 nathanw if(reselect_debug>1)
1494 1.44.6.2 nathanw for(i=0; i<bufptr; i++)
1495 1.44.6.2 nathanw printf("CSR:%02x", csrbuf[i]);
1496 1.44.6.2 nathanw #endif
1497 1.44.6.2 nathanw
1498 1.44.6.2 nathanw #ifdef DEBUG
1499 1.44.6.2 nathanw if(data_pointer_debug > 1)
1500 1.44.6.2 nathanw printf("sbicicmd done(%d,%d):%d =%d=\n",
1501 1.44.6.2 nathanw dev->target, lun,
1502 1.44.6.2 nathanw acb->sc_kv.dc_count,
1503 1.44.6.2 nathanw dev->sc_stat[0]);
1504 1.44.6.2 nathanw #endif
1505 1.44.6.2 nathanw
1506 1.44.6.2 nathanw QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
1507 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_ICMD;
1508 1.44.6.2 nathanw
1509 1.44.6.2 nathanw SBIC_TRACE(dev);
1510 1.44.6.2 nathanw return(dev->sc_stat[0]);
1511 1.44.6.2 nathanw }
1512 1.44.6.2 nathanw
1513 1.44.6.2 nathanw /*
1514 1.44.6.2 nathanw * Finish SCSI xfer command: After the completion interrupt from
1515 1.44.6.2 nathanw * a read/write operation, sequence through the final phases in
1516 1.44.6.2 nathanw * programmed i/o. This routine is a lot like sbicicmd except we
1517 1.44.6.2 nathanw * skip (and don't allow) the select, cmd out and data in/out phases.
1518 1.44.6.2 nathanw */
1519 1.44.6.2 nathanw void
1520 1.44.6.2 nathanw sbicxfdone(struct sbic_softc *dev, sbic_regmap_t regs, int target)
1521 1.44.6.2 nathanw {
1522 1.44.6.2 nathanw u_char phase, asr, csr;
1523 1.44.6.2 nathanw int s;
1524 1.44.6.2 nathanw
1525 1.44.6.2 nathanw SBIC_TRACE(dev);
1526 1.44.6.2 nathanw QPRINTF(("{"));
1527 1.44.6.2 nathanw s = splbio();
1528 1.44.6.2 nathanw
1529 1.44.6.2 nathanw /*
1530 1.44.6.2 nathanw * have the sbic complete on its own
1531 1.44.6.2 nathanw */
1532 1.44.6.2 nathanw SBIC_TC_PUT(regs, 0);
1533 1.44.6.2 nathanw SET_SBIC_cmd_phase(regs, 0x46);
1534 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
1535 1.44.6.2 nathanw
1536 1.44.6.2 nathanw do {
1537 1.44.6.2 nathanw asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
1538 1.44.6.2 nathanw GET_SBIC_csr (regs, csr);
1539 1.44.6.2 nathanw CSR_TRACE('f',csr,asr,target);
1540 1.44.6.2 nathanw QPRINTF(("%02x:", csr));
1541 1.44.6.2 nathanw } while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
1542 1.44.6.2 nathanw && (csr != SBIC_CSR_S_XFERRED));
1543 1.44.6.2 nathanw
1544 1.44.6.2 nathanw dev->sc_flags &= ~SBICF_SELECTED;
1545 1.44.6.2 nathanw
1546 1.44.6.2 nathanw GET_SBIC_cmd_phase (regs, phase);
1547 1.44.6.2 nathanw QPRINTF(("}%02x", phase));
1548 1.44.6.2 nathanw if (phase == 0x60)
1549 1.44.6.2 nathanw GET_SBIC_tlun(regs, dev->sc_stat[0]);
1550 1.44.6.2 nathanw else
1551 1.44.6.2 nathanw sbicerror(dev, regs, csr);
1552 1.44.6.2 nathanw
1553 1.44.6.2 nathanw QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
1554 1.44.6.2 nathanw splx(s);
1555 1.44.6.2 nathanw SBIC_TRACE(dev);
1556 1.44.6.2 nathanw }
1557 1.44.6.2 nathanw
1558 1.44.6.2 nathanw /*
1559 1.44.6.2 nathanw * No DMA chains
1560 1.44.6.2 nathanw */
1561 1.44.6.2 nathanw
1562 1.44.6.2 nathanw int
1563 1.44.6.2 nathanw sbicgo(struct sbic_softc *dev, struct scsipi_xfer *xs)
1564 1.44.6.2 nathanw {
1565 1.44.6.2 nathanw int i, dmaflags, count, usedma;
1566 1.44.6.2 nathanw u_char csr, asr, *addr;
1567 1.44.6.2 nathanw sbic_regmap_t regs;
1568 1.44.6.2 nathanw struct sbic_acb *acb;
1569 1.44.6.2 nathanw
1570 1.44.6.2 nathanw SBIC_TRACE(dev);
1571 1.44.6.2 nathanw dev->target = xs->xs_periph->periph_target;
1572 1.44.6.2 nathanw dev->lun = xs->xs_periph->periph_lun;
1573 1.44.6.2 nathanw acb = dev->sc_nexus;
1574 1.44.6.2 nathanw regs = dev->sc_sbic;
1575 1.44.6.2 nathanw
1576 1.44.6.2 nathanw usedma = sbicdmaok(dev, xs);
1577 1.44.6.2 nathanw #ifdef DEBUG
1578 1.44.6.2 nathanw routine = 1;
1579 1.44.6.2 nathanw debug_sbic_regs = regs; /* store this to allow debug calls */
1580 1.44.6.2 nathanw if( data_pointer_debug > 1 )
1581 1.44.6.2 nathanw printf("sbicgo(%d,%d)\n", dev->target, dev->lun);
1582 1.44.6.2 nathanw #endif
1583 1.44.6.2 nathanw
1584 1.44.6.2 nathanw /*
1585 1.44.6.2 nathanw * set the sbic into DMA mode
1586 1.44.6.2 nathanw */
1587 1.44.6.2 nathanw if( usedma )
1588 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
1589 1.44.6.2 nathanw SBIC_MACHINE_DMA_MODE);
1590 1.44.6.2 nathanw else
1591 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
1592 1.44.6.2 nathanw
1593 1.44.6.2 nathanw
1594 1.44.6.2 nathanw /*
1595 1.44.6.2 nathanw * select the SCSI bus (it's an error if bus isn't free)
1596 1.44.6.2 nathanw */
1597 1.44.6.2 nathanw if (sbicselectbus(dev, regs, dev->target, dev->lun,
1598 1.44.6.2 nathanw dev->sc_scsiaddr)) {
1599 1.44.6.2 nathanw /* printf("sbicgo: Trying to select busy bus!\n"); */
1600 1.44.6.2 nathanw SBIC_TRACE(dev);
1601 1.44.6.2 nathanw return(0); /* Not done: needs to be rescheduled */
1602 1.44.6.2 nathanw }
1603 1.44.6.2 nathanw dev->sc_stat[0] = 0xff;
1604 1.44.6.2 nathanw
1605 1.44.6.2 nathanw /*
1606 1.44.6.2 nathanw * Calculate DMA chains now
1607 1.44.6.2 nathanw */
1608 1.44.6.2 nathanw
1609 1.44.6.2 nathanw dmaflags = 0;
1610 1.44.6.2 nathanw if (acb->flags & ACB_DATAIN)
1611 1.44.6.2 nathanw dmaflags |= DMAGO_READ;
1612 1.44.6.2 nathanw
1613 1.44.6.2 nathanw
1614 1.44.6.2 nathanw /*
1615 1.44.6.2 nathanw * Deal w/bounce buffers.
1616 1.44.6.2 nathanw */
1617 1.44.6.2 nathanw
1618 1.44.6.2 nathanw addr = acb->sc_kv.dc_addr;
1619 1.44.6.2 nathanw count = acb->sc_kv.dc_count;
1620 1.44.6.2 nathanw if (count && (char *)kvtop(addr) != acb->sc_pa.dc_addr) { /* XXXX check */
1621 1.44.6.2 nathanw printf("sbic: DMA buffer mapping changed %p->%x\n",
1622 1.44.6.2 nathanw acb->sc_pa.dc_addr, kvtop(addr));
1623 1.44.6.2 nathanw #ifdef DDB
1624 1.44.6.2 nathanw Debugger();
1625 1.44.6.2 nathanw #endif
1626 1.44.6.2 nathanw }
1627 1.44.6.2 nathanw
1628 1.44.6.2 nathanw #ifdef DEBUG
1629 1.44.6.2 nathanw ++sbicdma_ops; /* count total DMA operations */
1630 1.44.6.2 nathanw #endif
1631 1.44.6.2 nathanw if (count && usedma && dev->sc_flags & SBICF_BADDMA &&
1632 1.44.6.2 nathanw sbiccheckdmap(addr, count, dev->sc_dmamask)) {
1633 1.44.6.2 nathanw /*
1634 1.44.6.2 nathanw * need to bounce the dma.
1635 1.44.6.2 nathanw */
1636 1.44.6.2 nathanw if (dmaflags & DMAGO_READ) {
1637 1.44.6.2 nathanw acb->flags |= ACB_BBUF;
1638 1.44.6.2 nathanw acb->sc_dmausrbuf = addr;
1639 1.44.6.2 nathanw acb->sc_dmausrlen = count;
1640 1.44.6.2 nathanw acb->sc_usrbufpa = (u_char *)kvtop(addr);
1641 1.44.6.2 nathanw if(!dev->sc_tinfo[dev->target].bounce) {
1642 1.44.6.2 nathanw printf("sbicgo: HELP! no bounce allocated for %d\n",
1643 1.44.6.2 nathanw dev->target);
1644 1.44.6.2 nathanw printf("xfer: (%p->%p,%lx)\n", acb->sc_dmausrbuf,
1645 1.44.6.2 nathanw acb->sc_usrbufpa, acb->sc_dmausrlen);
1646 1.44.6.2 nathanw dev->sc_tinfo[xs->xs_periph->periph_target].bounce
1647 1.44.6.2 nathanw = (char *)alloc_z2mem(MAXPHYS);
1648 1.44.6.2 nathanw if (isztwomem(dev->sc_tinfo[xs->xs_periph->periph_target].bounce))
1649 1.44.6.2 nathanw printf("alloc ZII target %d bounce pa 0x%x\n",
1650 1.44.6.2 nathanw xs->xs_periph->periph_target,
1651 1.44.6.2 nathanw kvtop(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
1652 1.44.6.2 nathanw else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
1653 1.44.6.2 nathanw printf("alloc CHIP target %d bounce pa 0x%p\n",
1654 1.44.6.2 nathanw xs->xs_periph->periph_target,
1655 1.44.6.2 nathanw PREP_DMA_MEM(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
1656 1.44.6.2 nathanw
1657 1.44.6.2 nathanw printf("Allocating %d bounce at %x\n",
1658 1.44.6.2 nathanw dev->target,
1659 1.44.6.2 nathanw kvtop(dev->sc_tinfo[dev->target].bounce));
1660 1.44.6.2 nathanw }
1661 1.44.6.2 nathanw } else { /* write: copy to dma buffer */
1662 1.44.6.2 nathanw #ifdef DEBUG
1663 1.44.6.2 nathanw if(data_pointer_debug)
1664 1.44.6.2 nathanw printf("sbicgo: copying %x bytes to target %d bounce %x\n",
1665 1.44.6.2 nathanw count, dev->target,
1666 1.44.6.2 nathanw kvtop(dev->sc_tinfo[dev->target].bounce));
1667 1.44.6.2 nathanw #endif
1668 1.44.6.2 nathanw bcopy (addr, dev->sc_tinfo[dev->target].bounce, count);
1669 1.44.6.2 nathanw }
1670 1.44.6.2 nathanw addr = dev->sc_tinfo[dev->target].bounce;/* and use dma buffer */
1671 1.44.6.2 nathanw acb->sc_kv.dc_addr = addr;
1672 1.44.6.2 nathanw #ifdef DEBUG
1673 1.44.6.2 nathanw ++sbicdma_bounces; /* count number of bounced */
1674 1.44.6.2 nathanw #endif
1675 1.44.6.2 nathanw }
1676 1.44.6.2 nathanw
1677 1.44.6.2 nathanw /*
1678 1.44.6.2 nathanw * Allocate the DMA chain
1679 1.44.6.2 nathanw */
1680 1.44.6.2 nathanw
1681 1.44.6.2 nathanw /* Set start KVM addresses */
1682 1.44.6.2 nathanw #if 0
1683 1.44.6.2 nathanw acb->sc_kv.dc_addr = addr;
1684 1.44.6.2 nathanw acb->sc_kv.dc_count = count;
1685 1.44.6.2 nathanw #endif
1686 1.44.6.2 nathanw
1687 1.44.6.2 nathanw /* Mark end of segment */
1688 1.44.6.2 nathanw acb->sc_tcnt = dev->sc_tcnt = 0;
1689 1.44.6.2 nathanw acb->sc_pa.dc_count = 0;
1690 1.44.6.2 nathanw
1691 1.44.6.2 nathanw sbic_load_ptrs(dev, regs, dev->target, dev->lun);
1692 1.44.6.2 nathanw SBIC_TRACE(dev);
1693 1.44.6.2 nathanw /* Enable interrupts but don't do any DMA */
1694 1.44.6.2 nathanw dev->sc_enintr(dev);
1695 1.44.6.2 nathanw if (usedma) {
1696 1.44.6.2 nathanw dev->sc_tcnt = dev->sc_dmago(dev, acb->sc_pa.dc_addr,
1697 1.44.6.2 nathanw acb->sc_pa.dc_count,
1698 1.44.6.2 nathanw dmaflags);
1699 1.44.6.2 nathanw #ifdef DEBUG
1700 1.44.6.2 nathanw dev->sc_dmatimo = dev->sc_tcnt ? 1 : 0;
1701 1.44.6.2 nathanw #endif
1702 1.44.6.2 nathanw } else
1703 1.44.6.2 nathanw dev->sc_dmacmd = 0; /* Don't use DMA */
1704 1.44.6.2 nathanw dev->sc_flags |= SBICF_INDMA;
1705 1.44.6.2 nathanw /* SBIC_TC_PUT(regs, dev->sc_tcnt); */ /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
1706 1.44.6.2 nathanw SBIC_TRACE(dev);
1707 1.44.6.2 nathanw sbic_save_ptrs(dev, regs, dev->target, dev->lun);
1708 1.44.6.2 nathanw
1709 1.44.6.2 nathanw /*
1710 1.44.6.2 nathanw * push the data cache ( I think this won't work (EH))
1711 1.44.6.2 nathanw */
1712 1.44.6.2 nathanw #if defined(M68040) || defined(M68060)
1713 1.44.6.2 nathanw if (mmutype == MMU_68040 && usedma && count) {
1714 1.44.6.2 nathanw dma_cachectl(addr, count);
1715 1.44.6.2 nathanw if (((u_int)addr & 0xF) || (((u_int)addr + count) & 0xF))
1716 1.44.6.2 nathanw dev->sc_flags |= SBICF_DCFLUSH;
1717 1.44.6.2 nathanw }
1718 1.44.6.2 nathanw #endif
1719 1.44.6.2 nathanw
1720 1.44.6.2 nathanw /*
1721 1.44.6.2 nathanw * enintr() also enables interrupts for the sbic
1722 1.44.6.2 nathanw */
1723 1.44.6.2 nathanw #ifdef DEBUG
1724 1.44.6.2 nathanw if( data_pointer_debug > 1 )
1725 1.44.6.2 nathanw printf("sbicgo dmago:%d(%p:%lx)\n",
1726 1.44.6.2 nathanw dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
1727 1.44.6.2 nathanw #if 0
1728 1.44.6.2 nathanw /*
1729 1.44.6.2 nathanw * Hmm - this isn't right: asr and csr haven't been set yet.
1730 1.44.6.2 nathanw */
1731 1.44.6.2 nathanw debug_asr = asr;
1732 1.44.6.2 nathanw debug_csr = csr;
1733 1.44.6.2 nathanw #endif
1734 1.44.6.2 nathanw #endif
1735 1.44.6.2 nathanw
1736 1.44.6.2 nathanw /*
1737 1.44.6.2 nathanw * Lets cycle a while then let the interrupt handler take over
1738 1.44.6.2 nathanw */
1739 1.44.6.2 nathanw
1740 1.44.6.3 nathanw GET_SBIC_asr(regs, asr);
1741 1.44.6.2 nathanw do {
1742 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1743 1.44.6.2 nathanw CSR_TRACE('g',csr,asr,dev->target);
1744 1.44.6.2 nathanw #ifdef DEBUG
1745 1.44.6.2 nathanw debug_csr = csr;
1746 1.44.6.2 nathanw routine = 1;
1747 1.44.6.2 nathanw #endif
1748 1.44.6.2 nathanw QPRINTF(("go[0x%x]", csr));
1749 1.44.6.2 nathanw
1750 1.44.6.2 nathanw i = sbicnextstate(dev, csr, asr);
1751 1.44.6.2 nathanw
1752 1.44.6.2 nathanw WAIT_CIP(regs);
1753 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1754 1.44.6.2 nathanw #ifdef DEBUG
1755 1.44.6.2 nathanw debug_asr = asr;
1756 1.44.6.2 nathanw #endif
1757 1.44.6.2 nathanw if(asr & SBIC_ASR_LCI) printf("sbicgo: LCI asr:%02x csr:%02x\n",
1758 1.44.6.2 nathanw asr,csr);
1759 1.44.6.2 nathanw } while( i == SBIC_STATE_RUNNING
1760 1.44.6.2 nathanw && asr & (SBIC_ASR_INT|SBIC_ASR_LCI) );
1761 1.44.6.2 nathanw
1762 1.44.6.2 nathanw CSR_TRACE('g',csr,asr,i<<4);
1763 1.44.6.2 nathanw SBIC_TRACE(dev);
1764 1.44.6.2 nathanw if (i == SBIC_STATE_DONE && dev->sc_stat[0] == 0xff) printf("sbicgo: done & stat = 0xff\n");
1765 1.44.6.2 nathanw if (i == SBIC_STATE_DONE && dev->sc_stat[0] != 0xff) {
1766 1.44.6.2 nathanw /* if( i == SBIC_STATE_DONE && dev->sc_stat[0] ) { */
1767 1.44.6.2 nathanw /* Did we really finish that fast? */
1768 1.44.6.2 nathanw return 1;
1769 1.44.6.2 nathanw }
1770 1.44.6.2 nathanw return 0;
1771 1.44.6.2 nathanw }
1772 1.44.6.2 nathanw
1773 1.44.6.2 nathanw
1774 1.44.6.2 nathanw int
1775 1.44.6.2 nathanw sbicintr(struct sbic_softc *dev)
1776 1.44.6.2 nathanw {
1777 1.44.6.2 nathanw sbic_regmap_t regs;
1778 1.44.6.2 nathanw u_char asr, csr;
1779 1.44.6.2 nathanw int i;
1780 1.44.6.2 nathanw
1781 1.44.6.2 nathanw regs = dev->sc_sbic;
1782 1.44.6.2 nathanw
1783 1.44.6.2 nathanw /*
1784 1.44.6.2 nathanw * pending interrupt?
1785 1.44.6.2 nathanw */
1786 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1787 1.44.6.2 nathanw if ((asr & SBIC_ASR_INT) == 0)
1788 1.44.6.2 nathanw return(0);
1789 1.44.6.2 nathanw
1790 1.44.6.2 nathanw SBIC_TRACE(dev);
1791 1.44.6.2 nathanw do {
1792 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1793 1.44.6.2 nathanw CSR_TRACE('i',csr,asr,dev->target);
1794 1.44.6.2 nathanw #ifdef DEBUG
1795 1.44.6.2 nathanw debug_csr = csr;
1796 1.44.6.2 nathanw routine = 2;
1797 1.44.6.2 nathanw #endif
1798 1.44.6.2 nathanw QPRINTF(("intr[0x%x]", csr));
1799 1.44.6.2 nathanw
1800 1.44.6.2 nathanw i = sbicnextstate(dev, csr, asr);
1801 1.44.6.2 nathanw
1802 1.44.6.2 nathanw WAIT_CIP(regs);
1803 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1804 1.44.6.2 nathanw #ifdef DEBUG
1805 1.44.6.2 nathanw debug_asr = asr;
1806 1.44.6.2 nathanw #endif
1807 1.44.6.2 nathanw #if 0
1808 1.44.6.2 nathanw if(asr & SBIC_ASR_LCI) printf("sbicintr: LCI asr:%02x csr:%02x\n",
1809 1.44.6.2 nathanw asr,csr);
1810 1.44.6.2 nathanw #endif
1811 1.44.6.2 nathanw } while(i == SBIC_STATE_RUNNING &&
1812 1.44.6.2 nathanw asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
1813 1.44.6.2 nathanw CSR_TRACE('i',csr,asr,i<<4);
1814 1.44.6.2 nathanw SBIC_TRACE(dev);
1815 1.44.6.2 nathanw return(1);
1816 1.44.6.2 nathanw }
1817 1.44.6.2 nathanw
1818 1.44.6.2 nathanw /*
1819 1.44.6.2 nathanw * Run commands and wait for disconnect
1820 1.44.6.2 nathanw */
1821 1.44.6.2 nathanw int
1822 1.44.6.2 nathanw sbicpoll(struct sbic_softc *dev)
1823 1.44.6.2 nathanw {
1824 1.44.6.2 nathanw sbic_regmap_t regs;
1825 1.44.6.2 nathanw u_char asr, csr;
1826 1.44.6.2 nathanw int i;
1827 1.44.6.2 nathanw
1828 1.44.6.2 nathanw SBIC_TRACE(dev);
1829 1.44.6.2 nathanw regs = dev->sc_sbic;
1830 1.44.6.2 nathanw
1831 1.44.6.2 nathanw do {
1832 1.44.6.2 nathanw GET_SBIC_asr (regs, asr);
1833 1.44.6.2 nathanw #ifdef DEBUG
1834 1.44.6.2 nathanw debug_asr = asr;
1835 1.44.6.2 nathanw #endif
1836 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1837 1.44.6.2 nathanw CSR_TRACE('p',csr,asr,dev->target);
1838 1.44.6.2 nathanw #ifdef DEBUG
1839 1.44.6.2 nathanw debug_csr = csr;
1840 1.44.6.2 nathanw routine = 2;
1841 1.44.6.2 nathanw #endif
1842 1.44.6.2 nathanw QPRINTF(("poll[0x%x]", csr));
1843 1.44.6.2 nathanw
1844 1.44.6.2 nathanw i = sbicnextstate(dev, csr, asr);
1845 1.44.6.2 nathanw
1846 1.44.6.2 nathanw WAIT_CIP(regs);
1847 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1848 1.44.6.2 nathanw /* tapes may take a loooong time.. */
1849 1.44.6.2 nathanw while (asr & SBIC_ASR_BSY){
1850 1.44.6.2 nathanw if(asr & SBIC_ASR_DBR) {
1851 1.44.6.2 nathanw printf("sbipoll: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
1852 1.44.6.2 nathanw csr,asr);
1853 1.44.6.2 nathanw #ifdef DDB
1854 1.44.6.2 nathanw Debugger();
1855 1.44.6.2 nathanw #endif
1856 1.44.6.2 nathanw /* SBIC is jammed */
1857 1.44.6.2 nathanw /* DUNNO which direction */
1858 1.44.6.2 nathanw /* Try old direction */
1859 1.44.6.2 nathanw GET_SBIC_data(regs,i);
1860 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1861 1.44.6.2 nathanw if( asr & SBIC_ASR_DBR) /* Wants us to write */
1862 1.44.6.2 nathanw SET_SBIC_data(regs,i);
1863 1.44.6.2 nathanw }
1864 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1865 1.44.6.2 nathanw }
1866 1.44.6.2 nathanw
1867 1.44.6.2 nathanw if(asr & SBIC_ASR_LCI) printf("sbicpoll: LCI asr:%02x csr:%02x\n",
1868 1.44.6.2 nathanw asr,csr);
1869 1.44.6.2 nathanw else if( i == 1 ) /* BSY */
1870 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
1871 1.44.6.2 nathanw } while(i == SBIC_STATE_RUNNING);
1872 1.44.6.2 nathanw CSR_TRACE('p',csr,asr,i<<4);
1873 1.44.6.2 nathanw SBIC_TRACE(dev);
1874 1.44.6.2 nathanw return(1);
1875 1.44.6.2 nathanw }
1876 1.44.6.2 nathanw
1877 1.44.6.2 nathanw /*
1878 1.44.6.2 nathanw * Handle a single msgin
1879 1.44.6.2 nathanw */
1880 1.44.6.2 nathanw
1881 1.44.6.2 nathanw int
1882 1.44.6.2 nathanw sbicmsgin(struct sbic_softc *dev)
1883 1.44.6.2 nathanw {
1884 1.44.6.2 nathanw sbic_regmap_t regs;
1885 1.44.6.2 nathanw int recvlen;
1886 1.44.6.2 nathanw u_char asr, csr, *tmpaddr;
1887 1.44.6.2 nathanw
1888 1.44.6.2 nathanw regs = dev->sc_sbic;
1889 1.44.6.2 nathanw
1890 1.44.6.2 nathanw dev->sc_msg[0] = 0xff;
1891 1.44.6.2 nathanw dev->sc_msg[1] = 0xff;
1892 1.44.6.2 nathanw
1893 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1894 1.44.6.2 nathanw #ifdef DEBUG
1895 1.44.6.2 nathanw if(reselect_debug>1)
1896 1.44.6.2 nathanw printf("sbicmsgin asr=%02x\n", asr);
1897 1.44.6.2 nathanw #endif
1898 1.44.6.2 nathanw
1899 1.44.6.2 nathanw sbic_save_ptrs(dev, regs, dev->target, dev->lun);
1900 1.44.6.2 nathanw
1901 1.44.6.2 nathanw GET_SBIC_selid (regs, csr);
1902 1.44.6.2 nathanw SET_SBIC_selid (regs, csr | SBIC_SID_FROM_SCSI);
1903 1.44.6.2 nathanw
1904 1.44.6.2 nathanw SBIC_TC_PUT(regs, 0);
1905 1.44.6.2 nathanw tmpaddr = dev->sc_msg;
1906 1.44.6.2 nathanw recvlen = 1;
1907 1.44.6.2 nathanw do {
1908 1.44.6.2 nathanw while( recvlen-- ) {
1909 1.44.6.3 nathanw GET_SBIC_asr(regs, asr);
1910 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1911 1.44.6.2 nathanw QPRINTF(("sbicmsgin ready to go (csr,asr)=(%02x,%02x)\n",
1912 1.44.6.2 nathanw csr, asr));
1913 1.44.6.2 nathanw
1914 1.44.6.2 nathanw RECV_BYTE(regs, *tmpaddr);
1915 1.44.6.2 nathanw CSR_TRACE('m',csr,asr,*tmpaddr);
1916 1.44.6.2 nathanw #if 1
1917 1.44.6.2 nathanw /*
1918 1.44.6.2 nathanw * get the command completion interrupt, or we
1919 1.44.6.2 nathanw * can't send a new command (LCI)
1920 1.44.6.2 nathanw */
1921 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1922 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1923 1.44.6.2 nathanw CSR_TRACE('X',csr,asr,dev->target);
1924 1.44.6.2 nathanw #else
1925 1.44.6.2 nathanw WAIT_CIP(regs);
1926 1.44.6.2 nathanw do {
1927 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1928 1.44.6.2 nathanw csr = 0xff;
1929 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1930 1.44.6.2 nathanw CSR_TRACE('X',csr,asr,dev->target);
1931 1.44.6.2 nathanw if( csr == 0xff )
1932 1.44.6.2 nathanw printf("sbicmsgin waiting: csr %02x asr %02x\n", csr, asr);
1933 1.44.6.2 nathanw } while( csr == 0xff );
1934 1.44.6.2 nathanw #endif
1935 1.44.6.2 nathanw #ifdef DEBUG
1936 1.44.6.2 nathanw if(reselect_debug>1)
1937 1.44.6.2 nathanw printf("sbicmsgin: got %02x csr %02x asr %02x\n",
1938 1.44.6.2 nathanw *tmpaddr, csr, asr);
1939 1.44.6.2 nathanw #endif
1940 1.44.6.2 nathanw #if do_parity_check
1941 1.44.6.2 nathanw if( asr & SBIC_ASR_PE ) {
1942 1.44.6.2 nathanw printf ("Parity error");
1943 1.44.6.2 nathanw /* This code simply does not work. */
1944 1.44.6.2 nathanw WAIT_CIP(regs);
1945 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
1946 1.44.6.2 nathanw WAIT_CIP(regs);
1947 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1948 1.44.6.2 nathanw WAIT_CIP(regs);
1949 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
1950 1.44.6.2 nathanw WAIT_CIP(regs);
1951 1.44.6.2 nathanw if( !(asr & SBIC_ASR_LCI) )
1952 1.44.6.2 nathanw /* Target wants to send garbled msg*/
1953 1.44.6.2 nathanw continue;
1954 1.44.6.2 nathanw printf("--fixing\n");
1955 1.44.6.2 nathanw /* loop until a msgout phase occurs on target */
1956 1.44.6.2 nathanw while(csr & 0x07 != MESG_OUT_PHASE) {
1957 1.44.6.2 nathanw while( asr & SBIC_ASR_BSY &&
1958 1.44.6.2 nathanw !(asr & SBIC_ASR_DBR|SBIC_ASR_INT) )
1959 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1960 1.44.6.2 nathanw if( asr & SBIC_ASR_DBR )
1961 1.44.6.2 nathanw panic("msgin: jammed again!\n");
1962 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1963 1.44.6.2 nathanw CSR_TRACE('e',csr,asr,dev->target);
1964 1.44.6.2 nathanw if( csr & 0x07 != MESG_OUT_PHASE ) {
1965 1.44.6.2 nathanw sbicnextstate(dev, csr, asr);
1966 1.44.6.2 nathanw sbic_save_ptrs(dev, regs,
1967 1.44.6.2 nathanw dev->target,
1968 1.44.6.2 nathanw dev->lun);
1969 1.44.6.2 nathanw }
1970 1.44.6.2 nathanw }
1971 1.44.6.2 nathanw /* Should be msg out by now */
1972 1.44.6.2 nathanw SEND_BYTE(regs, MSG_PARITY_ERROR);
1973 1.44.6.2 nathanw }
1974 1.44.6.2 nathanw else
1975 1.44.6.2 nathanw #endif
1976 1.44.6.2 nathanw tmpaddr++;
1977 1.44.6.2 nathanw
1978 1.44.6.2 nathanw if(recvlen) {
1979 1.44.6.2 nathanw /* Clear ACK */
1980 1.44.6.2 nathanw WAIT_CIP(regs);
1981 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
1982 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
1983 1.44.6.2 nathanw CSR_TRACE('X',csr,asr,dev->target);
1984 1.44.6.2 nathanw QPRINTF(("sbicmsgin pre byte CLR_ACK (csr,asr)=(%02x,%02x)\n",
1985 1.44.6.2 nathanw csr, asr));
1986 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
1987 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
1988 1.44.6.2 nathanw }
1989 1.44.6.2 nathanw
1990 1.44.6.2 nathanw };
1991 1.44.6.2 nathanw
1992 1.44.6.2 nathanw if(dev->sc_msg[0] == 0xff) {
1993 1.44.6.2 nathanw printf("sbicmsgin: sbic swallowed our message\n");
1994 1.44.6.2 nathanw break;
1995 1.44.6.2 nathanw }
1996 1.44.6.2 nathanw #ifdef DEBUG
1997 1.44.6.2 nathanw if (sync_debug)
1998 1.44.6.2 nathanw printf("msgin done csr 0x%x asr 0x%x msg 0x%x\n",
1999 1.44.6.2 nathanw csr, asr, dev->sc_msg[0]);
2000 1.44.6.2 nathanw #endif
2001 1.44.6.2 nathanw /*
2002 1.44.6.2 nathanw * test whether this is a reply to our sync
2003 1.44.6.2 nathanw * request
2004 1.44.6.2 nathanw */
2005 1.44.6.2 nathanw if (MSG_ISIDENTIFY(dev->sc_msg[0])) {
2006 1.44.6.2 nathanw QPRINTF(("IFFY"));
2007 1.44.6.2 nathanw #if 0
2008 1.44.6.2 nathanw /* There is an implied load-ptrs here */
2009 1.44.6.2 nathanw sbic_load_ptrs(dev, regs, dev->target, dev->lun);
2010 1.44.6.2 nathanw #endif
2011 1.44.6.2 nathanw /* Got IFFY msg -- ack it */
2012 1.44.6.2 nathanw } else if (dev->sc_msg[0] == MSG_REJECT
2013 1.44.6.2 nathanw && dev->sc_sync[dev->target].state == SYNC_SENT) {
2014 1.44.6.2 nathanw QPRINTF(("REJECT of SYN"));
2015 1.44.6.2 nathanw #ifdef DEBUG
2016 1.44.6.2 nathanw if (sync_debug)
2017 1.44.6.2 nathanw printf("target %d rejected sync, going async\n",
2018 1.44.6.2 nathanw dev->target);
2019 1.44.6.2 nathanw #endif
2020 1.44.6.2 nathanw dev->sc_sync[dev->target].period = sbic_min_period;
2021 1.44.6.2 nathanw dev->sc_sync[dev->target].offset = 0;
2022 1.44.6.2 nathanw dev->sc_sync[dev->target].state = SYNC_DONE;
2023 1.44.6.2 nathanw SET_SBIC_syn(regs,
2024 1.44.6.2 nathanw SBIC_SYN(dev->sc_sync[dev->target].offset,
2025 1.44.6.2 nathanw dev->sc_sync[dev->target].period));
2026 1.44.6.2 nathanw } else if ((dev->sc_msg[0] == MSG_REJECT)) {
2027 1.44.6.2 nathanw QPRINTF(("REJECT"));
2028 1.44.6.2 nathanw /*
2029 1.44.6.2 nathanw * we'll never REJECt a REJECT message..
2030 1.44.6.2 nathanw */
2031 1.44.6.2 nathanw } else if ((dev->sc_msg[0] == MSG_SAVE_DATA_PTR)) {
2032 1.44.6.2 nathanw QPRINTF(("MSG_SAVE_DATA_PTR"));
2033 1.44.6.2 nathanw /*
2034 1.44.6.2 nathanw * don't reject this either.
2035 1.44.6.2 nathanw */
2036 1.44.6.2 nathanw } else if ((dev->sc_msg[0] == MSG_DISCONNECT)) {
2037 1.44.6.2 nathanw QPRINTF(("DISCONNECT"));
2038 1.44.6.2 nathanw #ifdef DEBUG
2039 1.44.6.2 nathanw if( reselect_debug>1 && dev->sc_msg[0] == MSG_DISCONNECT )
2040 1.44.6.2 nathanw printf("sbicmsgin: got disconnect msg %s\n",
2041 1.44.6.2 nathanw (dev->sc_flags & SBICF_ICMD)?"rejecting":"");
2042 1.44.6.2 nathanw #endif
2043 1.44.6.2 nathanw if( dev->sc_flags & SBICF_ICMD ) {
2044 1.44.6.2 nathanw /* We're in immediate mode. Prevent disconnects. */
2045 1.44.6.2 nathanw /* prepare to reject the message, NACK */
2046 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2047 1.44.6.2 nathanw WAIT_CIP(regs);
2048 1.44.6.2 nathanw }
2049 1.44.6.2 nathanw } else if (dev->sc_msg[0] == MSG_CMD_COMPLETE ) {
2050 1.44.6.2 nathanw QPRINTF(("CMD_COMPLETE"));
2051 1.44.6.2 nathanw /* !! KLUDGE ALERT !! quite a few drives don't seem to
2052 1.44.6.2 nathanw * really like the current way of sending the
2053 1.44.6.2 nathanw * sync-handshake together with the ident-message, and
2054 1.44.6.2 nathanw * they react by sending command-complete and
2055 1.44.6.2 nathanw * disconnecting right after returning the valid sync
2056 1.44.6.2 nathanw * handshake. So, all I can do is reselect the drive,
2057 1.44.6.2 nathanw * and hope it won't disconnect again. I don't think
2058 1.44.6.2 nathanw * this is valid behavior, but I can't help fixing a
2059 1.44.6.2 nathanw * problem that apparently exists.
2060 1.44.6.2 nathanw *
2061 1.44.6.2 nathanw * Note: we should not get here on `normal' command
2062 1.44.6.2 nathanw * completion, as that condition is handled by the
2063 1.44.6.2 nathanw * high-level sel&xfer resume command used to walk
2064 1.44.6.2 nathanw * thru status/cc-phase.
2065 1.44.6.2 nathanw */
2066 1.44.6.2 nathanw
2067 1.44.6.2 nathanw #ifdef DEBUG
2068 1.44.6.2 nathanw if (sync_debug)
2069 1.44.6.2 nathanw printf ("GOT MSG %d! target %d acting weird.."
2070 1.44.6.2 nathanw " waiting for disconnect...\n",
2071 1.44.6.2 nathanw dev->sc_msg[0], dev->target);
2072 1.44.6.2 nathanw #endif
2073 1.44.6.2 nathanw /* Check to see if sbic is handling this */
2074 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
2075 1.44.6.2 nathanw if(asr & SBIC_ASR_BSY)
2076 1.44.6.2 nathanw return SBIC_STATE_RUNNING;
2077 1.44.6.2 nathanw
2078 1.44.6.2 nathanw /* Let's try this: Assume it works and set status to 00 */
2079 1.44.6.2 nathanw dev->sc_stat[0] = 0;
2080 1.44.6.2 nathanw } else if (dev->sc_msg[0] == MSG_EXT_MESSAGE
2081 1.44.6.2 nathanw && tmpaddr == &dev->sc_msg[1]) {
2082 1.44.6.2 nathanw QPRINTF(("ExtMSG\n"));
2083 1.44.6.2 nathanw /* Read in whole extended message */
2084 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2085 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2086 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
2087 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
2088 1.44.6.2 nathanw QPRINTF(("CLR ACK asr %02x, csr %02x\n", asr, csr));
2089 1.44.6.2 nathanw RECV_BYTE(regs, *tmpaddr);
2090 1.44.6.2 nathanw CSR_TRACE('x',csr,asr,*tmpaddr);
2091 1.44.6.2 nathanw /* Wait for command completion IRQ */
2092 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2093 1.44.6.2 nathanw recvlen = *tmpaddr++;
2094 1.44.6.2 nathanw QPRINTF(("Recving ext msg, asr %02x csr %02x len %02x\n",
2095 1.44.6.2 nathanw asr, csr, recvlen));
2096 1.44.6.2 nathanw } else if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
2097 1.44.6.2 nathanw && dev->sc_msg[2] == MSG_SYNC_REQ) {
2098 1.44.6.2 nathanw QPRINTF(("SYN"));
2099 1.44.6.2 nathanw dev->sc_sync[dev->target].period =
2100 1.44.6.2 nathanw sbicfromscsiperiod(dev,
2101 1.44.6.2 nathanw regs, dev->sc_msg[3]);
2102 1.44.6.2 nathanw dev->sc_sync[dev->target].offset = dev->sc_msg[4];
2103 1.44.6.2 nathanw dev->sc_sync[dev->target].state = SYNC_DONE;
2104 1.44.6.2 nathanw SET_SBIC_syn(regs,
2105 1.44.6.2 nathanw SBIC_SYN(dev->sc_sync[dev->target].offset,
2106 1.44.6.2 nathanw dev->sc_sync[dev->target].period));
2107 1.44.6.2 nathanw printf("%s: target %d now synchronous,"
2108 1.44.6.2 nathanw " period=%dns, offset=%d.\n",
2109 1.44.6.2 nathanw dev->sc_dev.dv_xname, dev->target,
2110 1.44.6.2 nathanw dev->sc_msg[3] * 4, dev->sc_msg[4]);
2111 1.44.6.2 nathanw } else {
2112 1.44.6.2 nathanw #ifdef DEBUG
2113 1.44.6.2 nathanw if (sbic_debug || sync_debug)
2114 1.44.6.2 nathanw printf ("sbicmsgin: Rejecting message 0x%02x\n",
2115 1.44.6.2 nathanw dev->sc_msg[0]);
2116 1.44.6.2 nathanw #endif
2117 1.44.6.2 nathanw /* prepare to reject the message, NACK */
2118 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
2119 1.44.6.2 nathanw WAIT_CIP(regs);
2120 1.44.6.2 nathanw }
2121 1.44.6.2 nathanw /* Clear ACK */
2122 1.44.6.2 nathanw WAIT_CIP(regs);
2123 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
2124 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
2125 1.44.6.2 nathanw CSR_TRACE('X',csr,asr,dev->target);
2126 1.44.6.2 nathanw QPRINTF(("sbicmsgin pre CLR_ACK (csr,asr)=(%02x,%02x)%d\n",
2127 1.44.6.2 nathanw csr, asr, recvlen));
2128 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2129 1.44.6.2 nathanw SBIC_WAIT(regs, SBIC_ASR_INT, 0);
2130 1.44.6.2 nathanw }
2131 1.44.6.2 nathanw #if 0
2132 1.44.6.2 nathanw while((csr == SBIC_CSR_MSGIN_W_ACK)
2133 1.44.6.2 nathanw || (SBIC_PHASE(csr) == MESG_IN_PHASE));
2134 1.44.6.2 nathanw #else
2135 1.44.6.2 nathanw while (recvlen>0);
2136 1.44.6.2 nathanw #endif
2137 1.44.6.2 nathanw
2138 1.44.6.2 nathanw QPRINTF(("sbicmsgin finished: csr %02x, asr %02x\n",csr, asr));
2139 1.44.6.2 nathanw
2140 1.44.6.2 nathanw /* Should still have one CSR to read */
2141 1.44.6.2 nathanw return SBIC_STATE_RUNNING;
2142 1.44.6.2 nathanw }
2143 1.44.6.2 nathanw
2144 1.44.6.2 nathanw
2145 1.44.6.2 nathanw /*
2146 1.44.6.2 nathanw * sbicnextstate()
2147 1.44.6.2 nathanw * return:
2148 1.44.6.2 nathanw * 0 == done
2149 1.44.6.2 nathanw * 1 == working
2150 1.44.6.2 nathanw * 2 == disconnected
2151 1.44.6.2 nathanw * -1 == error
2152 1.44.6.2 nathanw */
2153 1.44.6.2 nathanw int
2154 1.44.6.2 nathanw sbicnextstate(struct sbic_softc *dev, u_char csr, u_char asr)
2155 1.44.6.2 nathanw {
2156 1.44.6.2 nathanw sbic_regmap_t regs;
2157 1.44.6.2 nathanw struct sbic_acb *acb;
2158 1.44.6.2 nathanw int i, newtarget, newlun, wait;
2159 1.44.6.2 nathanw #if 0
2160 1.44.6.2 nathanw unsigned tcnt;
2161 1.44.6.2 nathanw #endif
2162 1.44.6.2 nathanw
2163 1.44.6.2 nathanw i = 0;
2164 1.44.6.2 nathanw SBIC_TRACE(dev);
2165 1.44.6.2 nathanw regs = dev->sc_sbic;
2166 1.44.6.2 nathanw acb = dev->sc_nexus;
2167 1.44.6.2 nathanw
2168 1.44.6.2 nathanw QPRINTF(("next[%02x,%02x]",asr,csr));
2169 1.44.6.2 nathanw
2170 1.44.6.2 nathanw switch (csr) {
2171 1.44.6.2 nathanw case SBIC_CSR_XFERRED|CMD_PHASE:
2172 1.44.6.2 nathanw case SBIC_CSR_MIS|CMD_PHASE:
2173 1.44.6.2 nathanw case SBIC_CSR_MIS_1|CMD_PHASE:
2174 1.44.6.2 nathanw case SBIC_CSR_MIS_2|CMD_PHASE:
2175 1.44.6.2 nathanw sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2176 1.44.6.2 nathanw if (sbicxfstart(regs, acb->clen, CMD_PHASE, sbic_cmd_wait))
2177 1.44.6.2 nathanw if (sbicxfout(regs, acb->clen,
2178 1.44.6.2 nathanw &acb->cmd, CMD_PHASE))
2179 1.44.6.2 nathanw goto abort;
2180 1.44.6.2 nathanw break;
2181 1.44.6.2 nathanw
2182 1.44.6.2 nathanw case SBIC_CSR_XFERRED|STATUS_PHASE:
2183 1.44.6.2 nathanw case SBIC_CSR_MIS|STATUS_PHASE:
2184 1.44.6.2 nathanw case SBIC_CSR_MIS_1|STATUS_PHASE:
2185 1.44.6.2 nathanw case SBIC_CSR_MIS_2|STATUS_PHASE:
2186 1.44.6.2 nathanw /*
2187 1.44.6.2 nathanw * this should be the normal i/o completion case.
2188 1.44.6.2 nathanw * get the status & cmd complete msg then let the
2189 1.44.6.2 nathanw * device driver look at what happened.
2190 1.44.6.2 nathanw */
2191 1.44.6.2 nathanw sbicxfdone(dev,regs,dev->target);
2192 1.44.6.2 nathanw /*
2193 1.44.6.2 nathanw * check for overlapping cache line, flush if so
2194 1.44.6.2 nathanw */
2195 1.44.6.2 nathanw #if defined(M68040) || defined(M68060)
2196 1.44.6.2 nathanw if (dev->sc_flags & SBICF_DCFLUSH) {
2197 1.44.6.2 nathanw #if 0
2198 1.44.6.2 nathanw printf("sbic: 68040/68060 DMA cache flush needs"
2199 1.44.6.2 nathanw "fixing? %x:%x\n",
2200 1.44.6.2 nathanw dev->sc_xs->data, dev->sc_xs->datalen);
2201 1.44.6.2 nathanw #endif
2202 1.44.6.2 nathanw }
2203 1.44.6.2 nathanw #endif
2204 1.44.6.2 nathanw #ifdef DEBUG
2205 1.44.6.2 nathanw if( data_pointer_debug > 1 )
2206 1.44.6.2 nathanw printf("next dmastop: %d(%p:%lx)\n",
2207 1.44.6.2 nathanw dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2208 1.44.6.2 nathanw dev->sc_dmatimo = 0;
2209 1.44.6.2 nathanw #endif
2210 1.44.6.2 nathanw dev->sc_dmastop(dev); /* was dmafree */
2211 1.44.6.2 nathanw if (acb->flags & ACB_BBUF) {
2212 1.44.6.2 nathanw if ((u_char *)kvtop(acb->sc_dmausrbuf) != acb->sc_usrbufpa)
2213 1.44.6.2 nathanw printf("%s: WARNING - buffer mapping changed %p->%x\n",
2214 1.44.6.2 nathanw dev->sc_dev.dv_xname, acb->sc_usrbufpa,
2215 1.44.6.2 nathanw kvtop(acb->sc_dmausrbuf));
2216 1.44.6.2 nathanw #ifdef DEBUG
2217 1.44.6.2 nathanw if(data_pointer_debug)
2218 1.44.6.2 nathanw printf("sbicgo:copying %lx bytes from target %d bounce %x\n",
2219 1.44.6.2 nathanw acb->sc_dmausrlen,
2220 1.44.6.2 nathanw dev->target,
2221 1.44.6.2 nathanw kvtop(dev->sc_tinfo[dev->target].bounce));
2222 1.44.6.2 nathanw #endif
2223 1.44.6.2 nathanw bcopy(dev->sc_tinfo[dev->target].bounce,
2224 1.44.6.2 nathanw acb->sc_dmausrbuf,
2225 1.44.6.2 nathanw acb->sc_dmausrlen);
2226 1.44.6.2 nathanw }
2227 1.44.6.2 nathanw dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
2228 1.44.6.2 nathanw sbic_scsidone(acb, dev->sc_stat[0]);
2229 1.44.6.2 nathanw SBIC_TRACE(dev);
2230 1.44.6.2 nathanw return SBIC_STATE_DONE;
2231 1.44.6.2 nathanw
2232 1.44.6.2 nathanw case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
2233 1.44.6.2 nathanw case SBIC_CSR_XFERRED|DATA_IN_PHASE:
2234 1.44.6.2 nathanw case SBIC_CSR_MIS|DATA_OUT_PHASE:
2235 1.44.6.2 nathanw case SBIC_CSR_MIS|DATA_IN_PHASE:
2236 1.44.6.2 nathanw case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
2237 1.44.6.2 nathanw case SBIC_CSR_MIS_1|DATA_IN_PHASE:
2238 1.44.6.2 nathanw case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
2239 1.44.6.2 nathanw case SBIC_CSR_MIS_2|DATA_IN_PHASE:
2240 1.44.6.2 nathanw if( dev->sc_xs->xs_control & XS_CTL_POLL || dev->sc_flags & SBICF_ICMD
2241 1.44.6.2 nathanw || acb->sc_dmacmd == 0 ) {
2242 1.44.6.2 nathanw /* Do PIO */
2243 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2244 1.44.6.2 nathanw if (acb->sc_kv.dc_count <= 0) {
2245 1.44.6.2 nathanw printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2246 1.44.6.2 nathanw acb->sc_kv.dc_count, asr, csr);
2247 1.44.6.2 nathanw goto abort;
2248 1.44.6.2 nathanw }
2249 1.44.6.2 nathanw wait = sbic_data_wait;
2250 1.44.6.2 nathanw if( sbicxfstart(regs,
2251 1.44.6.2 nathanw acb->sc_kv.dc_count,
2252 1.44.6.2 nathanw SBIC_PHASE(csr), wait)) {
2253 1.44.6.2 nathanw if( SBIC_PHASE(csr) == DATA_IN_PHASE )
2254 1.44.6.2 nathanw /* data in? */
2255 1.44.6.2 nathanw i=sbicxfin(regs,
2256 1.44.6.2 nathanw acb->sc_kv.dc_count,
2257 1.44.6.2 nathanw acb->sc_kv.dc_addr);
2258 1.44.6.2 nathanw else
2259 1.44.6.2 nathanw i=sbicxfout(regs,
2260 1.44.6.2 nathanw acb->sc_kv.dc_count,
2261 1.44.6.2 nathanw acb->sc_kv.dc_addr,
2262 1.44.6.2 nathanw SBIC_PHASE(csr));
2263 1.44.6.2 nathanw }
2264 1.44.6.2 nathanw acb->sc_kv.dc_addr +=
2265 1.44.6.2 nathanw (acb->sc_kv.dc_count - i);
2266 1.44.6.2 nathanw acb->sc_kv.dc_count = i;
2267 1.44.6.2 nathanw } else {
2268 1.44.6.2 nathanw if (acb->sc_kv.dc_count <= 0) {
2269 1.44.6.2 nathanw printf("sbicnextstate:xfer count %d asr%x csr%x\n",
2270 1.44.6.2 nathanw acb->sc_kv.dc_count, asr, csr);
2271 1.44.6.2 nathanw goto abort;
2272 1.44.6.2 nathanw }
2273 1.44.6.2 nathanw /*
2274 1.44.6.2 nathanw * do scatter-gather dma
2275 1.44.6.2 nathanw * hacking the controller chip, ouch..
2276 1.44.6.2 nathanw */
2277 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
2278 1.44.6.2 nathanw SBIC_MACHINE_DMA_MODE);
2279 1.44.6.2 nathanw /*
2280 1.44.6.2 nathanw * set next dma addr and dec count
2281 1.44.6.2 nathanw */
2282 1.44.6.2 nathanw #if 0
2283 1.44.6.2 nathanw SBIC_TC_GET(regs, tcnt);
2284 1.44.6.2 nathanw dev->sc_cur->dc_count -= ((dev->sc_tcnt - tcnt) >> 1);
2285 1.44.6.2 nathanw dev->sc_cur->dc_addr += (dev->sc_tcnt - tcnt);
2286 1.44.6.2 nathanw dev->sc_tcnt = acb->sc_tcnt = tcnt;
2287 1.44.6.2 nathanw #else
2288 1.44.6.2 nathanw sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2289 1.44.6.2 nathanw sbic_load_ptrs(dev, regs, dev->target, dev->lun);
2290 1.44.6.2 nathanw #endif
2291 1.44.6.2 nathanw #ifdef DEBUG
2292 1.44.6.2 nathanw if( data_pointer_debug > 1 )
2293 1.44.6.2 nathanw printf("next dmanext: %d(%p:%lx)\n",
2294 1.44.6.2 nathanw dev->target,dev->sc_cur->dc_addr,
2295 1.44.6.2 nathanw dev->sc_tcnt);
2296 1.44.6.2 nathanw dev->sc_dmatimo = 1;
2297 1.44.6.2 nathanw #endif
2298 1.44.6.2 nathanw dev->sc_tcnt = dev->sc_dmanext(dev);
2299 1.44.6.2 nathanw SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
2300 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
2301 1.44.6.2 nathanw dev->sc_flags |= SBICF_INDMA;
2302 1.44.6.2 nathanw }
2303 1.44.6.2 nathanw break;
2304 1.44.6.2 nathanw
2305 1.44.6.2 nathanw case SBIC_CSR_XFERRED|MESG_IN_PHASE:
2306 1.44.6.2 nathanw case SBIC_CSR_MIS|MESG_IN_PHASE:
2307 1.44.6.2 nathanw case SBIC_CSR_MIS_1|MESG_IN_PHASE:
2308 1.44.6.2 nathanw case SBIC_CSR_MIS_2|MESG_IN_PHASE:
2309 1.44.6.2 nathanw SBIC_TRACE(dev);
2310 1.44.6.2 nathanw return sbicmsgin(dev);
2311 1.44.6.2 nathanw
2312 1.44.6.2 nathanw case SBIC_CSR_MSGIN_W_ACK:
2313 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK); /* Dunno what I'm ACKing */
2314 1.44.6.2 nathanw printf("Acking unknown msgin CSR:%02x",csr);
2315 1.44.6.2 nathanw break;
2316 1.44.6.2 nathanw
2317 1.44.6.2 nathanw case SBIC_CSR_XFERRED|MESG_OUT_PHASE:
2318 1.44.6.2 nathanw case SBIC_CSR_MIS|MESG_OUT_PHASE:
2319 1.44.6.2 nathanw case SBIC_CSR_MIS_1|MESG_OUT_PHASE:
2320 1.44.6.2 nathanw case SBIC_CSR_MIS_2|MESG_OUT_PHASE:
2321 1.44.6.2 nathanw #ifdef DEBUG
2322 1.44.6.2 nathanw if (sync_debug)
2323 1.44.6.2 nathanw printf ("sending REJECT msg to last msg.\n");
2324 1.44.6.2 nathanw #endif
2325 1.44.6.2 nathanw
2326 1.44.6.2 nathanw sbic_save_ptrs(dev, regs, dev->target, dev->lun);
2327 1.44.6.2 nathanw /*
2328 1.44.6.2 nathanw * should only get here on reject,
2329 1.44.6.2 nathanw * since it's always US that
2330 1.44.6.2 nathanw * initiate a sync transfer
2331 1.44.6.2 nathanw */
2332 1.44.6.2 nathanw SEND_BYTE(regs, MSG_REJECT);
2333 1.44.6.2 nathanw WAIT_CIP(regs);
2334 1.44.6.2 nathanw if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
2335 1.44.6.2 nathanw printf("next: REJECT sent asr %02x\n", asr);
2336 1.44.6.2 nathanw SBIC_TRACE(dev);
2337 1.44.6.2 nathanw return SBIC_STATE_RUNNING;
2338 1.44.6.2 nathanw
2339 1.44.6.2 nathanw case SBIC_CSR_DISC:
2340 1.44.6.2 nathanw case SBIC_CSR_DISC_1:
2341 1.44.6.2 nathanw dev->sc_flags &= ~(SBICF_INDMA|SBICF_SELECTED);
2342 1.44.6.2 nathanw
2343 1.44.6.2 nathanw /* Try to schedule another target */
2344 1.44.6.2 nathanw #ifdef DEBUG
2345 1.44.6.2 nathanw if(reselect_debug>1)
2346 1.44.6.2 nathanw printf("sbicnext target %d disconnected\n", dev->target);
2347 1.44.6.2 nathanw #endif
2348 1.44.6.2 nathanw TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
2349 1.44.6.2 nathanw ++dev->sc_tinfo[dev->target].dconns;
2350 1.44.6.2 nathanw dev->sc_nexus = NULL;
2351 1.44.6.2 nathanw dev->sc_xs = NULL;
2352 1.44.6.2 nathanw
2353 1.44.6.2 nathanw if( acb->xs->xs_control & XS_CTL_POLL
2354 1.44.6.2 nathanw || (dev->sc_flags & SBICF_ICMD)
2355 1.44.6.2 nathanw || !sbic_parallel_operations ) {
2356 1.44.6.2 nathanw SBIC_TRACE(dev);
2357 1.44.6.2 nathanw return SBIC_STATE_DISCONNECT;
2358 1.44.6.2 nathanw }
2359 1.44.6.2 nathanw sbic_sched(dev);
2360 1.44.6.2 nathanw SBIC_TRACE(dev);
2361 1.44.6.2 nathanw return SBIC_STATE_DISCONNECT;
2362 1.44.6.2 nathanw
2363 1.44.6.2 nathanw case SBIC_CSR_RSLT_NI:
2364 1.44.6.2 nathanw case SBIC_CSR_RSLT_IFY:
2365 1.44.6.2 nathanw GET_SBIC_rselid(regs, newtarget);
2366 1.44.6.2 nathanw /* check SBIC_RID_SIV? */
2367 1.44.6.2 nathanw newtarget &= SBIC_RID_MASK;
2368 1.44.6.2 nathanw if (csr == SBIC_CSR_RSLT_IFY) {
2369 1.44.6.2 nathanw /* Read IFY msg to avoid lockup */
2370 1.44.6.2 nathanw GET_SBIC_data(regs, newlun);
2371 1.44.6.2 nathanw WAIT_CIP(regs);
2372 1.44.6.2 nathanw newlun &= SBIC_TLUN_MASK;
2373 1.44.6.2 nathanw CSR_TRACE('r',csr,asr,newtarget);
2374 1.44.6.2 nathanw } else {
2375 1.44.6.2 nathanw /* Need to get IFY message */
2376 1.44.6.2 nathanw for (newlun = 256; newlun; --newlun) {
2377 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
2378 1.44.6.2 nathanw if (asr & SBIC_ASR_INT)
2379 1.44.6.2 nathanw break;
2380 1.44.6.2 nathanw delay(1);
2381 1.44.6.2 nathanw }
2382 1.44.6.2 nathanw newlun = 0; /* XXXX */
2383 1.44.6.2 nathanw if ((asr & SBIC_ASR_INT) == 0) {
2384 1.44.6.2 nathanw #ifdef DEBUG
2385 1.44.6.2 nathanw if (reselect_debug)
2386 1.44.6.2 nathanw printf("RSLT_NI - no IFFY message? asr %x\n", asr);
2387 1.44.6.2 nathanw #endif
2388 1.44.6.2 nathanw } else {
2389 1.44.6.2 nathanw GET_SBIC_csr(regs,csr);
2390 1.44.6.2 nathanw CSR_TRACE('n',csr,asr,newtarget);
2391 1.44.6.2 nathanw if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
2392 1.44.6.2 nathanw csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
2393 1.44.6.2 nathanw csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
2394 1.44.6.2 nathanw sbicmsgin(dev);
2395 1.44.6.2 nathanw newlun = dev->sc_msg[0] & 7;
2396 1.44.6.2 nathanw } else {
2397 1.44.6.2 nathanw printf("RSLT_NI - not MESG_IN_PHASE %x\n",
2398 1.44.6.2 nathanw csr);
2399 1.44.6.2 nathanw }
2400 1.44.6.2 nathanw }
2401 1.44.6.2 nathanw }
2402 1.44.6.2 nathanw #ifdef DEBUG
2403 1.44.6.2 nathanw if(reselect_debug>1 || (reselect_debug && csr==SBIC_CSR_RSLT_NI))
2404 1.44.6.2 nathanw printf("sbicnext: reselect %s from targ %d lun %d\n",
2405 1.44.6.2 nathanw csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
2406 1.44.6.2 nathanw newtarget, newlun);
2407 1.44.6.2 nathanw #endif
2408 1.44.6.2 nathanw if (dev->sc_nexus) {
2409 1.44.6.2 nathanw #ifdef DEBUG
2410 1.44.6.2 nathanw if (reselect_debug > 1)
2411 1.44.6.2 nathanw printf("%s: reselect %s with active command\n",
2412 1.44.6.2 nathanw dev->sc_dev.dv_xname,
2413 1.44.6.2 nathanw csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
2414 1.44.6.2 nathanw #ifdef DDB
2415 1.44.6.2 nathanw /* Debugger();*/
2416 1.44.6.2 nathanw #endif
2417 1.44.6.2 nathanw #endif
2418 1.44.6.2 nathanw TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus, chain);
2419 1.44.6.2 nathanw dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
2420 1.44.6.2 nathanw dev->sc_nexus = NULL;
2421 1.44.6.2 nathanw dev->sc_xs = NULL;
2422 1.44.6.2 nathanw }
2423 1.44.6.2 nathanw /* Reload sync values for this target */
2424 1.44.6.2 nathanw if (dev->sc_sync[newtarget].state == SYNC_DONE)
2425 1.44.6.2 nathanw SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[newtarget].offset,
2426 1.44.6.2 nathanw dev->sc_sync[newtarget].period));
2427 1.44.6.2 nathanw else
2428 1.44.6.2 nathanw SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
2429 1.44.6.2 nathanw for (acb = dev->nexus_list.tqh_first; acb;
2430 1.44.6.2 nathanw acb = acb->chain.tqe_next) {
2431 1.44.6.2 nathanw if (acb->xs->xs_periph->periph_target != newtarget ||
2432 1.44.6.2 nathanw acb->xs->xs_periph->periph_lun != newlun)
2433 1.44.6.2 nathanw continue;
2434 1.44.6.2 nathanw TAILQ_REMOVE(&dev->nexus_list, acb, chain);
2435 1.44.6.2 nathanw dev->sc_nexus = acb;
2436 1.44.6.2 nathanw dev->sc_xs = acb->xs;
2437 1.44.6.2 nathanw dev->sc_flags |= SBICF_SELECTED;
2438 1.44.6.2 nathanw dev->target = newtarget;
2439 1.44.6.2 nathanw dev->lun = newlun;
2440 1.44.6.2 nathanw break;
2441 1.44.6.2 nathanw }
2442 1.44.6.2 nathanw if (acb == NULL) {
2443 1.44.6.2 nathanw printf("%s: reselect %s targ %d not in nexus_list %p\n",
2444 1.44.6.2 nathanw dev->sc_dev.dv_xname,
2445 1.44.6.2 nathanw csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
2446 1.44.6.2 nathanw &dev->nexus_list.tqh_first);
2447 1.44.6.2 nathanw panic("bad reselect in sbic");
2448 1.44.6.2 nathanw }
2449 1.44.6.2 nathanw if (csr == SBIC_CSR_RSLT_IFY)
2450 1.44.6.2 nathanw SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
2451 1.44.6.2 nathanw break;
2452 1.44.6.2 nathanw
2453 1.44.6.2 nathanw default:
2454 1.44.6.2 nathanw abort:
2455 1.44.6.2 nathanw /*
2456 1.44.6.2 nathanw * Something unexpected happened -- deal with it.
2457 1.44.6.2 nathanw */
2458 1.44.6.2 nathanw printf("sbicnextstate: aborting csr %02x asr %02x\n", csr, asr);
2459 1.44.6.2 nathanw #ifdef DDB
2460 1.44.6.2 nathanw Debugger();
2461 1.44.6.2 nathanw #endif
2462 1.44.6.2 nathanw #ifdef DEBUG
2463 1.44.6.2 nathanw if( data_pointer_debug > 1 )
2464 1.44.6.2 nathanw printf("next dmastop: %d(%p:%lx)\n",
2465 1.44.6.2 nathanw dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2466 1.44.6.2 nathanw dev->sc_dmatimo = 0;
2467 1.44.6.2 nathanw #endif
2468 1.44.6.2 nathanw dev->sc_dmastop(dev);
2469 1.44.6.2 nathanw SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
2470 1.44.6.2 nathanw sbicerror(dev, regs, csr);
2471 1.44.6.2 nathanw sbicabort(dev, regs, "next");
2472 1.44.6.2 nathanw if (dev->sc_flags & SBICF_INDMA) {
2473 1.44.6.2 nathanw /*
2474 1.44.6.2 nathanw * check for overlapping cache line, flush if so
2475 1.44.6.2 nathanw */
2476 1.44.6.2 nathanw #if defined(M68040) || defined(M68060)
2477 1.44.6.2 nathanw if (dev->sc_flags & SBICF_DCFLUSH) {
2478 1.44.6.2 nathanw #if 0
2479 1.44.6.2 nathanw printf("sbic: 68040/060 DMA cache flush needs"
2480 1.44.6.2 nathanw "fixing? %x:%x\n",
2481 1.44.6.2 nathanw dev->sc_xs->data, dev->sc_xs->datalen);
2482 1.44.6.2 nathanw #endif
2483 1.44.6.2 nathanw }
2484 1.44.6.2 nathanw #endif
2485 1.44.6.2 nathanw dev->sc_flags &=
2486 1.44.6.2 nathanw ~(SBICF_INDMA | SBICF_DCFLUSH);
2487 1.44.6.2 nathanw #ifdef DEBUG
2488 1.44.6.2 nathanw if( data_pointer_debug > 1 )
2489 1.44.6.2 nathanw printf("next dmastop: %d(%p:%lx)\n",
2490 1.44.6.2 nathanw dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
2491 1.44.6.2 nathanw dev->sc_dmatimo = 0;
2492 1.44.6.2 nathanw #endif
2493 1.44.6.2 nathanw dev->sc_dmastop(dev);
2494 1.44.6.2 nathanw sbic_scsidone(acb, -1);
2495 1.44.6.2 nathanw }
2496 1.44.6.2 nathanw SBIC_TRACE(dev);
2497 1.44.6.2 nathanw return SBIC_STATE_ERROR;
2498 1.44.6.2 nathanw }
2499 1.44.6.2 nathanw
2500 1.44.6.2 nathanw SBIC_TRACE(dev);
2501 1.44.6.2 nathanw return(SBIC_STATE_RUNNING);
2502 1.44.6.2 nathanw }
2503 1.44.6.2 nathanw
2504 1.44.6.2 nathanw
2505 1.44.6.2 nathanw /*
2506 1.44.6.2 nathanw * Check if DMA can not be used with specified buffer
2507 1.44.6.2 nathanw */
2508 1.44.6.2 nathanw
2509 1.44.6.2 nathanw int
2510 1.44.6.2 nathanw sbiccheckdmap(void *bp, u_long len, u_long mask)
2511 1.44.6.2 nathanw {
2512 1.44.6.2 nathanw u_char *buffer;
2513 1.44.6.2 nathanw u_long phy_buf;
2514 1.44.6.2 nathanw u_long phy_len;
2515 1.44.6.2 nathanw
2516 1.44.6.2 nathanw buffer = bp;
2517 1.44.6.2 nathanw
2518 1.44.6.2 nathanw if (len == 0)
2519 1.44.6.2 nathanw return(0);
2520 1.44.6.2 nathanw
2521 1.44.6.2 nathanw while (len) {
2522 1.44.6.2 nathanw phy_buf = kvtop(buffer);
2523 1.44.6.2 nathanw if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
2524 1.44.6.2 nathanw phy_len = len;
2525 1.44.6.2 nathanw if (phy_buf & mask)
2526 1.44.6.2 nathanw return(1);
2527 1.44.6.2 nathanw buffer += phy_len;
2528 1.44.6.2 nathanw len -= phy_len;
2529 1.44.6.2 nathanw }
2530 1.44.6.2 nathanw return(0);
2531 1.44.6.2 nathanw }
2532 1.44.6.2 nathanw
2533 1.44.6.2 nathanw int
2534 1.44.6.2 nathanw sbictoscsiperiod(struct sbic_softc *dev, sbic_regmap_t regs, int a)
2535 1.44.6.2 nathanw {
2536 1.44.6.2 nathanw unsigned int fs;
2537 1.44.6.2 nathanw
2538 1.44.6.2 nathanw /*
2539 1.44.6.2 nathanw * cycle = DIV / (2*CLK)
2540 1.44.6.2 nathanw * DIV = FS+2
2541 1.44.6.2 nathanw * best we can do is 200ns at 20Mhz, 2 cycles
2542 1.44.6.2 nathanw */
2543 1.44.6.2 nathanw
2544 1.44.6.2 nathanw GET_SBIC_myid(regs,fs);
2545 1.44.6.2 nathanw fs = (fs >>6) + 2; /* DIV */
2546 1.44.6.2 nathanw fs = (fs * 10000) / (dev->sc_clkfreq<<1); /* Cycle, in ns */
2547 1.44.6.2 nathanw if (a < 2) a = 8; /* map to Cycles */
2548 1.44.6.2 nathanw return ((fs*a)>>2); /* in 4 ns units */
2549 1.44.6.2 nathanw }
2550 1.44.6.2 nathanw
2551 1.44.6.2 nathanw int
2552 1.44.6.2 nathanw sbicfromscsiperiod(struct sbic_softc *dev, sbic_regmap_t regs, int p)
2553 1.44.6.2 nathanw {
2554 1.44.6.2 nathanw register unsigned int fs, ret;
2555 1.44.6.2 nathanw
2556 1.44.6.2 nathanw /* Just the inverse of the above */
2557 1.44.6.2 nathanw
2558 1.44.6.2 nathanw GET_SBIC_myid(regs,fs);
2559 1.44.6.2 nathanw fs = (fs >>6) + 2; /* DIV */
2560 1.44.6.2 nathanw fs = (fs * 10000) / (dev->sc_clkfreq<<1); /* Cycle, in ns */
2561 1.44.6.2 nathanw
2562 1.44.6.2 nathanw ret = p << 2; /* in ns units */
2563 1.44.6.2 nathanw ret = ret / fs; /* in Cycles */
2564 1.44.6.2 nathanw if (ret < sbic_min_period)
2565 1.44.6.2 nathanw return(sbic_min_period);
2566 1.44.6.2 nathanw
2567 1.44.6.2 nathanw /* verify rounding */
2568 1.44.6.2 nathanw if (sbictoscsiperiod(dev, regs, ret) < p)
2569 1.44.6.2 nathanw ret++;
2570 1.44.6.2 nathanw return (ret >= 8) ? 0 : ret;
2571 1.44.6.2 nathanw }
2572 1.44.6.2 nathanw
2573 1.44.6.2 nathanw #ifdef DEBUG
2574 1.44.6.2 nathanw
2575 1.44.6.2 nathanw void
2576 1.44.6.2 nathanw sbicdumpstate(void)
2577 1.44.6.2 nathanw {
2578 1.44.6.2 nathanw u_char csr, asr;
2579 1.44.6.2 nathanw
2580 1.44.6.2 nathanw GET_SBIC_asr(debug_sbic_regs,asr);
2581 1.44.6.2 nathanw GET_SBIC_csr(debug_sbic_regs,csr);
2582 1.44.6.2 nathanw printf("%s: asr:csr(%02x:%02x)->(%02x:%02x)\n",
2583 1.44.6.2 nathanw (routine==1)?"sbicgo":
2584 1.44.6.2 nathanw (routine==2)?"sbicintr":
2585 1.44.6.2 nathanw (routine==3)?"sbicicmd":
2586 1.44.6.2 nathanw (routine==4)?"sbicnext":"unknown",
2587 1.44.6.2 nathanw debug_asr, debug_csr, asr, csr);
2588 1.44.6.2 nathanw
2589 1.44.6.2 nathanw }
2590 1.44.6.2 nathanw
2591 1.44.6.2 nathanw void
2592 1.44.6.2 nathanw sbictimeout(struct sbic_softc *dev)
2593 1.44.6.2 nathanw {
2594 1.44.6.2 nathanw int s, asr;
2595 1.44.6.2 nathanw
2596 1.44.6.2 nathanw s = splbio();
2597 1.44.6.2 nathanw if (dev->sc_dmatimo) {
2598 1.44.6.2 nathanw if (dev->sc_dmatimo > 1) {
2599 1.44.6.2 nathanw printf("%s: dma timeout #%d\n",
2600 1.44.6.2 nathanw dev->sc_dev.dv_xname, dev->sc_dmatimo - 1);
2601 1.44.6.2 nathanw GET_SBIC_asr(dev->sc_sbic, asr);
2602 1.44.6.2 nathanw if( asr & SBIC_ASR_INT ) {
2603 1.44.6.2 nathanw /* We need to service a missed IRQ */
2604 1.44.6.3 nathanw printf("Servicing a missed int:(%02x,%02x)->(%02x,?)\n",
2605 1.44.6.2 nathanw debug_asr, debug_csr, asr);
2606 1.44.6.2 nathanw sbicintr(dev);
2607 1.44.6.2 nathanw }
2608 1.44.6.2 nathanw sbicdumpstate();
2609 1.44.6.2 nathanw }
2610 1.44.6.2 nathanw dev->sc_dmatimo++;
2611 1.44.6.2 nathanw }
2612 1.44.6.2 nathanw splx(s);
2613 1.44.6.2 nathanw callout_reset(&dev->sc_timo_ch, 30 * hz,
2614 1.44.6.2 nathanw (void *)sbictimeout, dev);
2615 1.44.6.2 nathanw }
2616 1.44.6.2 nathanw
2617 1.44.6.2 nathanw void
2618 1.44.6.2 nathanw sbic_dump_acb(struct sbic_acb *acb)
2619 1.44.6.2 nathanw {
2620 1.44.6.2 nathanw u_char *b = (u_char *) &acb->cmd;
2621 1.44.6.2 nathanw int i;
2622 1.44.6.2 nathanw
2623 1.44.6.2 nathanw printf("acb@%p ", acb);
2624 1.44.6.2 nathanw if (acb->xs == NULL) {
2625 1.44.6.2 nathanw printf("<unused>\n");
2626 1.44.6.2 nathanw return;
2627 1.44.6.2 nathanw }
2628 1.44.6.2 nathanw printf("(%d:%d) flags %2x clen %2d cmd ",
2629 1.44.6.2 nathanw acb->xs->xs_periph->periph_target,
2630 1.44.6.2 nathanw acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
2631 1.44.6.2 nathanw for (i = acb->clen; i; --i)
2632 1.44.6.2 nathanw printf(" %02x", *b++);
2633 1.44.6.2 nathanw printf("\n");
2634 1.44.6.2 nathanw printf(" xs: %8p data %8p:%04x ", acb->xs, acb->xs->data,
2635 1.44.6.2 nathanw acb->xs->datalen);
2636 1.44.6.2 nathanw printf("va %8p:%04x ", acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
2637 1.44.6.2 nathanw printf("pa %8p:%04x tcnt %lx\n", acb->sc_pa.dc_addr, acb->sc_pa.dc_count,
2638 1.44.6.2 nathanw acb->sc_tcnt);
2639 1.44.6.2 nathanw }
2640 1.44.6.2 nathanw
2641 1.44.6.2 nathanw void
2642 1.44.6.2 nathanw sbic_dump(struct sbic_softc *dev)
2643 1.44.6.2 nathanw {
2644 1.44.6.2 nathanw sbic_regmap_t regs;
2645 1.44.6.2 nathanw u_char csr, asr;
2646 1.44.6.2 nathanw struct sbic_acb *acb;
2647 1.44.6.2 nathanw int s;
2648 1.44.6.2 nathanw int i;
2649 1.44.6.2 nathanw
2650 1.44.6.2 nathanw s = splbio();
2651 1.44.6.2 nathanw regs = dev->sc_sbic;
2652 1.44.6.2 nathanw #if CSR_TRACE_SIZE
2653 1.44.6.2 nathanw printf("csr trace: ");
2654 1.44.6.2 nathanw i = csr_traceptr;
2655 1.44.6.2 nathanw do {
2656 1.44.6.2 nathanw printf("%c%02x%02x%02x ", csr_trace[i].whr,
2657 1.44.6.2 nathanw csr_trace[i].csr, csr_trace[i].asr, csr_trace[i].xtn);
2658 1.44.6.2 nathanw switch(csr_trace[i].whr) {
2659 1.44.6.2 nathanw case 'g':
2660 1.44.6.2 nathanw printf("go "); break;
2661 1.44.6.2 nathanw case 's':
2662 1.44.6.2 nathanw printf("select "); break;
2663 1.44.6.2 nathanw case 'y':
2664 1.44.6.2 nathanw printf("select+ "); break;
2665 1.44.6.2 nathanw case 'i':
2666 1.44.6.2 nathanw printf("intr "); break;
2667 1.44.6.2 nathanw case 'f':
2668 1.44.6.2 nathanw printf("finish "); break;
2669 1.44.6.2 nathanw case '>':
2670 1.44.6.2 nathanw printf("out "); break;
2671 1.44.6.2 nathanw case '<':
2672 1.44.6.2 nathanw printf("in "); break;
2673 1.44.6.2 nathanw case 'm':
2674 1.44.6.2 nathanw printf("msgin "); break;
2675 1.44.6.2 nathanw case 'x':
2676 1.44.6.2 nathanw printf("msginx "); break;
2677 1.44.6.2 nathanw case 'X':
2678 1.44.6.2 nathanw printf("msginX "); break;
2679 1.44.6.2 nathanw case 'r':
2680 1.44.6.2 nathanw printf("reselect "); break;
2681 1.44.6.2 nathanw case 'I':
2682 1.44.6.2 nathanw printf("icmd "); break;
2683 1.44.6.2 nathanw case 'a':
2684 1.44.6.2 nathanw printf("abort "); break;
2685 1.44.6.2 nathanw default:
2686 1.44.6.2 nathanw printf("? ");
2687 1.44.6.2 nathanw }
2688 1.44.6.2 nathanw switch(csr_trace[i].csr) {
2689 1.44.6.2 nathanw case 0x11:
2690 1.44.6.2 nathanw printf("INITIATOR"); break;
2691 1.44.6.2 nathanw case 0x16:
2692 1.44.6.2 nathanw printf("S_XFERRED"); break;
2693 1.44.6.2 nathanw case 0x20:
2694 1.44.6.2 nathanw printf("MSGIN_ACK"); break;
2695 1.44.6.2 nathanw case 0x41:
2696 1.44.6.2 nathanw printf("DISC"); break;
2697 1.44.6.2 nathanw case 0x42:
2698 1.44.6.2 nathanw printf("SEL_TIMEO"); break;
2699 1.44.6.2 nathanw case 0x80:
2700 1.44.6.2 nathanw printf("RSLT_NI"); break;
2701 1.44.6.2 nathanw case 0x81:
2702 1.44.6.2 nathanw printf("RSLT_IFY"); break;
2703 1.44.6.2 nathanw case 0x85:
2704 1.44.6.2 nathanw printf("DISC_1"); break;
2705 1.44.6.2 nathanw case 0x18: case 0x19: case 0x1a:
2706 1.44.6.2 nathanw case 0x1b: case 0x1e: case 0x1f:
2707 1.44.6.2 nathanw case 0x28: case 0x29: case 0x2a:
2708 1.44.6.2 nathanw case 0x2b: case 0x2e: case 0x2f:
2709 1.44.6.2 nathanw case 0x48: case 0x49: case 0x4a:
2710 1.44.6.2 nathanw case 0x4b: case 0x4e: case 0x4f:
2711 1.44.6.2 nathanw case 0x88: case 0x89: case 0x8a:
2712 1.44.6.2 nathanw case 0x8b: case 0x8e: case 0x8f:
2713 1.44.6.2 nathanw switch(csr_trace[i].csr & 0xf0) {
2714 1.44.6.2 nathanw case 0x10:
2715 1.44.6.2 nathanw printf("DONE_"); break;
2716 1.44.6.2 nathanw case 0x20:
2717 1.44.6.2 nathanw printf("STOP_"); break;
2718 1.44.6.2 nathanw case 0x40:
2719 1.44.6.2 nathanw printf("ERR_"); break;
2720 1.44.6.2 nathanw case 0x80:
2721 1.44.6.2 nathanw printf("REQ_"); break;
2722 1.44.6.2 nathanw }
2723 1.44.6.2 nathanw switch(csr_trace[i].csr & 7) {
2724 1.44.6.2 nathanw case 0:
2725 1.44.6.2 nathanw printf("DATA_OUT"); break;
2726 1.44.6.2 nathanw case 1:
2727 1.44.6.2 nathanw printf("DATA_IN"); break;
2728 1.44.6.2 nathanw case 2:
2729 1.44.6.2 nathanw printf("CMD"); break;
2730 1.44.6.2 nathanw case 3:
2731 1.44.6.2 nathanw printf("STATUS"); break;
2732 1.44.6.2 nathanw case 6:
2733 1.44.6.2 nathanw printf("MSG_OUT"); break;
2734 1.44.6.2 nathanw case 7:
2735 1.44.6.2 nathanw printf("MSG_IN"); break;
2736 1.44.6.2 nathanw default:
2737 1.44.6.2 nathanw printf("invld phs");
2738 1.44.6.2 nathanw }
2739 1.44.6.2 nathanw break;
2740 1.44.6.2 nathanw default: printf("****"); break;
2741 1.44.6.2 nathanw }
2742 1.44.6.2 nathanw if (csr_trace[i].asr & SBIC_ASR_INT)
2743 1.44.6.2 nathanw printf(" ASR_INT");
2744 1.44.6.2 nathanw if (csr_trace[i].asr & SBIC_ASR_LCI)
2745 1.44.6.2 nathanw printf(" ASR_LCI");
2746 1.44.6.2 nathanw if (csr_trace[i].asr & SBIC_ASR_BSY)
2747 1.44.6.2 nathanw printf(" ASR_BSY");
2748 1.44.6.2 nathanw if (csr_trace[i].asr & SBIC_ASR_CIP)
2749 1.44.6.2 nathanw printf(" ASR_CIP");
2750 1.44.6.2 nathanw printf("\n");
2751 1.44.6.2 nathanw i = (i + 1) & (CSR_TRACE_SIZE - 1);
2752 1.44.6.2 nathanw } while (i != csr_traceptr);
2753 1.44.6.2 nathanw #endif
2754 1.44.6.2 nathanw GET_SBIC_asr(regs, asr);
2755 1.44.6.2 nathanw if ((asr & SBIC_ASR_INT) == 0)
2756 1.44.6.2 nathanw GET_SBIC_csr(regs, csr);
2757 1.44.6.2 nathanw else
2758 1.44.6.2 nathanw csr = 0;
2759 1.44.6.2 nathanw printf("%s@%p regs %p/%p asr %x csr %x\n", dev->sc_dev.dv_xname,
2760 1.44.6.2 nathanw dev, regs.sbic_asr_p, regs.sbic_value_p, asr, csr);
2761 1.44.6.2 nathanw if ((acb = dev->free_list.tqh_first)) {
2762 1.44.6.2 nathanw printf("Free list:\n");
2763 1.44.6.2 nathanw while (acb) {
2764 1.44.6.2 nathanw sbic_dump_acb(acb);
2765 1.44.6.2 nathanw acb = acb->chain.tqe_next;
2766 1.44.6.2 nathanw }
2767 1.44.6.2 nathanw }
2768 1.44.6.2 nathanw if ((acb = dev->ready_list.tqh_first)) {
2769 1.44.6.2 nathanw printf("Ready list:\n");
2770 1.44.6.2 nathanw while (acb) {
2771 1.44.6.2 nathanw sbic_dump_acb(acb);
2772 1.44.6.2 nathanw acb = acb->chain.tqe_next;
2773 1.44.6.2 nathanw }
2774 1.44.6.2 nathanw }
2775 1.44.6.2 nathanw if ((acb = dev->nexus_list.tqh_first)) {
2776 1.44.6.2 nathanw printf("Nexus list:\n");
2777 1.44.6.2 nathanw while (acb) {
2778 1.44.6.2 nathanw sbic_dump_acb(acb);
2779 1.44.6.2 nathanw acb = acb->chain.tqe_next;
2780 1.44.6.2 nathanw }
2781 1.44.6.2 nathanw }
2782 1.44.6.2 nathanw if (dev->sc_nexus) {
2783 1.44.6.2 nathanw printf("nexus:\n");
2784 1.44.6.2 nathanw sbic_dump_acb(dev->sc_nexus);
2785 1.44.6.2 nathanw }
2786 1.44.6.2 nathanw printf("sc_xs %p targ %d lun %d flags %x tcnt %lx dmacmd %x mask %lx\n",
2787 1.44.6.2 nathanw dev->sc_xs, dev->target, dev->lun, dev->sc_flags, dev->sc_tcnt,
2788 1.44.6.2 nathanw dev->sc_dmacmd, dev->sc_dmamask);
2789 1.44.6.2 nathanw for (i = 0; i < 8; ++i) {
2790 1.44.6.2 nathanw if (dev->sc_tinfo[i].cmds > 2) {
2791 1.44.6.2 nathanw printf("tgt %d: cmds %d disc %d lubusy %x\n",
2792 1.44.6.2 nathanw i, dev->sc_tinfo[i].cmds,
2793 1.44.6.2 nathanw dev->sc_tinfo[i].dconns,
2794 1.44.6.2 nathanw dev->sc_tinfo[i].lubusy);
2795 1.44.6.2 nathanw }
2796 1.44.6.2 nathanw }
2797 1.44.6.2 nathanw splx(s);
2798 1.44.6.2 nathanw }
2799 1.44.6.2 nathanw
2800 1.44.6.2 nathanw #endif
2801