sci.c revision 1.25 1 1.25 bouyer /* $NetBSD: sci.c,v 1.25 2001/04/25 17:53:08 bouyer Exp $ */
2 1.8 cgd
3 1.5 chopps /*
4 1.5 chopps * Copyright (c) 1994 Michael L. Hitch
5 1.1 chopps * Copyright (c) 1990 The Regents of the University of California.
6 1.1 chopps * All rights reserved.
7 1.1 chopps *
8 1.1 chopps * This code is derived from software contributed to Berkeley by
9 1.1 chopps * Van Jacobson of Lawrence Berkeley Laboratory.
10 1.1 chopps *
11 1.1 chopps * Redistribution and use in source and binary forms, with or without
12 1.1 chopps * modification, are permitted provided that the following conditions
13 1.1 chopps * are met:
14 1.1 chopps * 1. Redistributions of source code must retain the above copyright
15 1.1 chopps * notice, this list of conditions and the following disclaimer.
16 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 chopps * notice, this list of conditions and the following disclaimer in the
18 1.1 chopps * documentation and/or other materials provided with the distribution.
19 1.1 chopps * 3. All advertising materials mentioning features or use of this software
20 1.1 chopps * must display the following acknowledgement:
21 1.1 chopps * This product includes software developed by the University of
22 1.1 chopps * California, Berkeley and its contributors.
23 1.1 chopps * 4. Neither the name of the University nor the names of its contributors
24 1.1 chopps * may be used to endorse or promote products derived from this software
25 1.1 chopps * without specific prior written permission.
26 1.1 chopps *
27 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 chopps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 chopps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 chopps * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 chopps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 chopps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 chopps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 chopps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 chopps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 chopps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 chopps * SUCH DAMAGE.
38 1.1 chopps *
39 1.5 chopps * @(#)scsi.c 7.5 (Berkeley) 5/4/91
40 1.1 chopps */
41 1.1 chopps
42 1.1 chopps /*
43 1.1 chopps * AMIGA NCR 5380 scsi adaptor driver
44 1.1 chopps */
45 1.1 chopps
46 1.1 chopps #include <sys/param.h>
47 1.1 chopps #include <sys/systm.h>
48 1.5 chopps #include <sys/device.h>
49 1.11 chopps #include <sys/disklabel.h>
50 1.11 chopps #include <sys/dkstat.h>
51 1.1 chopps #include <sys/buf.h>
52 1.20 bouyer #include <dev/scsipi/scsi_all.h>
53 1.20 bouyer #include <dev/scsipi/scsipi_all.h>
54 1.20 bouyer #include <dev/scsipi/scsiconf.h>
55 1.23 mrg #include <uvm/uvm_extern.h>
56 1.1 chopps #include <machine/pmap.h>
57 1.5 chopps #include <machine/cpu.h>
58 1.5 chopps #include <amiga/amiga/device.h>
59 1.5 chopps #include <amiga/amiga/custom.h>
60 1.11 chopps #include <amiga/amiga/isr.h>
61 1.5 chopps #include <amiga/dev/scireg.h>
62 1.1 chopps #include <amiga/dev/scivar.h>
63 1.1 chopps
64 1.1 chopps /*
65 1.1 chopps * SCSI delays
66 1.1 chopps * In u-seconds, primarily for state changes on the SPC.
67 1.1 chopps */
68 1.5 chopps #define SCI_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
69 1.5 chopps #define SCI_DATA_WAIT 50000 /* wait per data in/out step */
70 1.5 chopps #define SCI_INIT_WAIT 50000 /* wait per step (both) during init */
71 1.1 chopps
72 1.5 chopps int sciicmd __P((struct sci_softc *, int, void *, int, void *, int,u_char));
73 1.20 bouyer int scigo __P((struct sci_softc *, struct scsipi_xfer *));
74 1.5 chopps int sciselectbus __P((struct sci_softc *, u_char, u_char));
75 1.5 chopps void sciabort __P((struct sci_softc *, char *));
76 1.5 chopps void scierror __P((struct sci_softc *, u_char));
77 1.5 chopps void scisetdelay __P((int));
78 1.5 chopps void sci_scsidone __P((struct sci_softc *, int));
79 1.5 chopps void sci_donextcmd __P((struct sci_softc *));
80 1.16 veego int sci_ixfer_out __P((struct sci_softc *, int, register u_char *, int));
81 1.16 veego void sci_ixfer_in __P((struct sci_softc *, int, register u_char *, int));
82 1.5 chopps
83 1.5 chopps int sci_cmd_wait = SCI_CMD_WAIT;
84 1.5 chopps int sci_data_wait = SCI_DATA_WAIT;
85 1.5 chopps int sci_init_wait = SCI_INIT_WAIT;
86 1.1 chopps
87 1.5 chopps int sci_no_dma = 0;
88 1.5 chopps
89 1.5 chopps #ifdef DEBUG
90 1.19 christos #define QPRINTF(a) if (sci_debug > 1) printf a
91 1.5 chopps int sci_debug = 0;
92 1.1 chopps #else
93 1.16 veego #define QPRINTF(a)
94 1.1 chopps #endif
95 1.1 chopps
96 1.5 chopps /*
97 1.5 chopps * default minphys routine for sci based controllers
98 1.5 chopps */
99 1.13 mycroft void
100 1.5 chopps sci_minphys(bp)
101 1.5 chopps struct buf *bp;
102 1.5 chopps {
103 1.13 mycroft
104 1.5 chopps /*
105 1.13 mycroft * No max transfer at this level.
106 1.5 chopps */
107 1.13 mycroft minphys(bp);
108 1.5 chopps }
109 1.5 chopps
110 1.5 chopps /*
111 1.5 chopps * used by specific sci controller
112 1.5 chopps *
113 1.5 chopps * it appears that the higher level code does nothing with LUN's
114 1.5 chopps * so I will too. I could plug it in, however so could they
115 1.20 bouyer * in scsi_scsipi_cmd().
116 1.5 chopps */
117 1.25 bouyer void
118 1.25 bouyer sci_scsipi_request(chan, req, arg)
119 1.25 bouyer struct scsipi_channel *chan;
120 1.25 bouyer scsipi_adapter_req_t req;
121 1.25 bouyer void *arg;
122 1.25 bouyer {
123 1.20 bouyer struct scsipi_xfer *xs;
124 1.25 bouyer struct scsipi_periph *periph;
125 1.25 bouyer struct sci_softc *dev = (void *)chan->chan_adapter->adapt_dev;
126 1.5 chopps int flags, s;
127 1.1 chopps
128 1.25 bouyer switch (req) {
129 1.25 bouyer case ADAPTER_REQ_RUN_XFER:
130 1.25 bouyer xs = arg;
131 1.25 bouyer periph = xs->xs_periph;
132 1.25 bouyer flags = xs->xs_control;
133 1.1 chopps
134 1.25 bouyer if (flags & XS_CTL_DATA_UIO)
135 1.5 chopps panic("sci: scsi data uio requested");
136 1.13 mycroft
137 1.25 bouyer if (dev->sc_xs && flags & XS_CTL_POLL)
138 1.25 bouyer panic("sci_scsicmd: busy");
139 1.1 chopps
140 1.25 bouyer #ifdef DIAGNOSTIC
141 1.25 bouyer /*
142 1.25 bouyer * This should never happen as we track the resources
143 1.25 bouyer * in the mid-layer.
144 1.25 bouyer */
145 1.25 bouyer if (dev->sc_xs) {
146 1.25 bouyer scsipi_printaddr(periph);
147 1.25 bouyer printf("unable to allocate scb\n");
148 1.25 bouyer panic("sea_scsipi_request");
149 1.25 bouyer }
150 1.25 bouyer #endif
151 1.25 bouyer
152 1.25 bouyer dev->sc_xs = xs;
153 1.5 chopps splx(s);
154 1.1 chopps
155 1.25 bouyer /*
156 1.25 bouyer * nothing is pending do it now.
157 1.25 bouyer */
158 1.25 bouyer sci_donextcmd(dev);
159 1.25 bouyer
160 1.25 bouyer return;
161 1.1 chopps
162 1.25 bouyer case ADAPTER_REQ_GROW_RESOURCES:
163 1.25 bouyer return;
164 1.1 chopps
165 1.25 bouyer case ADAPTER_REQ_SET_XFER_MODE:
166 1.25 bouyer return;
167 1.25 bouyer }
168 1.5 chopps }
169 1.1 chopps
170 1.5 chopps /*
171 1.5 chopps * entered with dev->sc_xs pointing to the next xfer to perform
172 1.5 chopps */
173 1.5 chopps void
174 1.5 chopps sci_donextcmd(dev)
175 1.5 chopps struct sci_softc *dev;
176 1.5 chopps {
177 1.20 bouyer struct scsipi_xfer *xs;
178 1.25 bouyer struct scsipi_periph *periph;
179 1.5 chopps int flags, phase, stat;
180 1.5 chopps
181 1.5 chopps xs = dev->sc_xs;
182 1.25 bouyer periph = xs->xs_periph;
183 1.21 thorpej flags = xs->xs_control;
184 1.5 chopps
185 1.21 thorpej if (flags & XS_CTL_DATA_IN)
186 1.5 chopps phase = DATA_IN_PHASE;
187 1.21 thorpej else if (flags & XS_CTL_DATA_OUT)
188 1.5 chopps phase = DATA_OUT_PHASE;
189 1.5 chopps else
190 1.5 chopps phase = STATUS_PHASE;
191 1.13 mycroft
192 1.21 thorpej if (flags & XS_CTL_RESET)
193 1.5 chopps scireset(dev);
194 1.1 chopps
195 1.5 chopps dev->sc_stat[0] = -1;
196 1.25 bouyer xs->cmd->bytes[0] |= periph->periph_lun << 5;
197 1.21 thorpej if (phase == STATUS_PHASE || flags & XS_CTL_POLL)
198 1.25 bouyer stat = sciicmd(dev, periph->periph_target, xs->cmd, xs->cmdlen,
199 1.5 chopps xs->data, xs->datalen, phase);
200 1.5 chopps else if (scigo(dev, xs) == 0)
201 1.5 chopps return;
202 1.13 mycroft else
203 1.5 chopps stat = dev->sc_stat[0];
204 1.13 mycroft
205 1.5 chopps sci_scsidone(dev, stat);
206 1.5 chopps }
207 1.1 chopps
208 1.5 chopps void
209 1.5 chopps sci_scsidone(dev, stat)
210 1.5 chopps struct sci_softc *dev;
211 1.5 chopps int stat;
212 1.5 chopps {
213 1.20 bouyer struct scsipi_xfer *xs;
214 1.5 chopps
215 1.5 chopps xs = dev->sc_xs;
216 1.5 chopps #ifdef DIAGNOSTIC
217 1.5 chopps if (xs == NULL)
218 1.5 chopps panic("sci_scsidone");
219 1.11 chopps #endif
220 1.5 chopps xs->status = stat;
221 1.10 chopps if (stat == 0)
222 1.5 chopps xs->resid = 0;
223 1.5 chopps else {
224 1.5 chopps switch(stat) {
225 1.5 chopps case SCSI_CHECK:
226 1.25 bouyer xs->resid = 0;
227 1.25 bouyer /* FALLTHOUGH */
228 1.5 chopps case SCSI_BUSY:
229 1.5 chopps xs->error = XS_BUSY;
230 1.5 chopps break;
231 1.5 chopps default:
232 1.5 chopps xs->error = XS_DRIVER_STUFFUP;
233 1.5 chopps QPRINTF(("sci_scsicmd() bad %x\n", stat));
234 1.5 chopps break;
235 1.5 chopps }
236 1.5 chopps }
237 1.21 thorpej
238 1.20 bouyer scsipi_done(xs);
239 1.1 chopps
240 1.1 chopps }
241 1.1 chopps
242 1.5 chopps void
243 1.5 chopps sciabort(dev, where)
244 1.5 chopps struct sci_softc *dev;
245 1.1 chopps char *where;
246 1.1 chopps {
247 1.19 christos printf ("%s: abort %s: csr = 0x%02x, bus = 0x%02x\n",
248 1.5 chopps dev->sc_dev.dv_xname, where, *dev->sci_csr, *dev->sci_bus_csr);
249 1.1 chopps
250 1.1 chopps if (dev->sc_flags & SCI_SELECTED) {
251 1.1 chopps
252 1.7 chopps /* lets just hope it worked.. */
253 1.7 chopps dev->sc_flags &= ~SCI_SELECTED;
254 1.1 chopps /* XXX */
255 1.5 chopps scireset (dev);
256 1.1 chopps }
257 1.1 chopps }
258 1.1 chopps
259 1.1 chopps /*
260 1.1 chopps * XXX Set/reset long delays.
261 1.1 chopps *
262 1.1 chopps * if delay == 0, reset default delays
263 1.1 chopps * if delay < 0, set both delays to default long initialization values
264 1.1 chopps * if delay > 0, set both delays to this value
265 1.1 chopps *
266 1.1 chopps * Used when a devices is expected to respond slowly (e.g. during
267 1.1 chopps * initialization).
268 1.1 chopps */
269 1.1 chopps void
270 1.5 chopps scisetdelay(del)
271 1.5 chopps int del;
272 1.1 chopps {
273 1.1 chopps static int saved_cmd_wait, saved_data_wait;
274 1.1 chopps
275 1.5 chopps if (del) {
276 1.1 chopps saved_cmd_wait = sci_cmd_wait;
277 1.1 chopps saved_data_wait = sci_data_wait;
278 1.5 chopps if (del > 0)
279 1.5 chopps sci_cmd_wait = sci_data_wait = del;
280 1.1 chopps else
281 1.1 chopps sci_cmd_wait = sci_data_wait = sci_init_wait;
282 1.1 chopps } else {
283 1.1 chopps sci_cmd_wait = saved_cmd_wait;
284 1.1 chopps sci_data_wait = saved_data_wait;
285 1.1 chopps }
286 1.1 chopps }
287 1.1 chopps
288 1.1 chopps void
289 1.5 chopps scireset(dev)
290 1.5 chopps struct sci_softc *dev;
291 1.1 chopps {
292 1.16 veego u_int s;
293 1.16 veego u_char my_id;
294 1.1 chopps
295 1.7 chopps dev->sc_flags &= ~SCI_SELECTED;
296 1.1 chopps if (dev->sc_flags & SCI_ALIVE)
297 1.5 chopps sciabort(dev, "reset");
298 1.1 chopps
299 1.19 christos printf("%s: ", dev->sc_dev.dv_xname);
300 1.1 chopps
301 1.1 chopps s = splbio();
302 1.1 chopps /* preserve our ID for now */
303 1.1 chopps my_id = 7;
304 1.1 chopps
305 1.1 chopps /*
306 1.5 chopps * Reset the chip
307 1.1 chopps */
308 1.1 chopps *dev->sci_icmd = SCI_ICMD_TEST;
309 1.1 chopps *dev->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
310 1.6 chopps delay (25);
311 1.1 chopps *dev->sci_icmd = 0;
312 1.1 chopps
313 1.1 chopps /*
314 1.1 chopps * Set up various chip parameters
315 1.1 chopps */
316 1.1 chopps *dev->sci_icmd = 0;
317 1.1 chopps *dev->sci_tcmd = 0;
318 1.1 chopps *dev->sci_sel_enb = 0;
319 1.1 chopps
320 1.1 chopps /* anything else was zeroed by reset */
321 1.1 chopps
322 1.1 chopps splx (s);
323 1.1 chopps
324 1.19 christos printf("sci id %d\n", my_id);
325 1.1 chopps dev->sc_flags |= SCI_ALIVE;
326 1.1 chopps }
327 1.1 chopps
328 1.5 chopps void
329 1.5 chopps scierror(dev, csr)
330 1.5 chopps struct sci_softc *dev;
331 1.1 chopps u_char csr;
332 1.1 chopps {
333 1.20 bouyer struct scsipi_xfer *xs;
334 1.1 chopps
335 1.5 chopps xs = dev->sc_xs;
336 1.5 chopps
337 1.5 chopps #ifdef DIAGNOSTIC
338 1.5 chopps if (xs == NULL)
339 1.5 chopps panic("scierror");
340 1.5 chopps #endif
341 1.21 thorpej if (xs->xs_control & XS_CTL_SILENT)
342 1.5 chopps return;
343 1.5 chopps
344 1.19 christos printf("%s: ", dev->sc_dev.dv_xname);
345 1.19 christos printf("csr == 0x%02i\n", csr); /* XXX */
346 1.1 chopps }
347 1.1 chopps
348 1.5 chopps /*
349 1.5 chopps * select the bus, return when selected or error.
350 1.5 chopps */
351 1.5 chopps int
352 1.5 chopps sciselectbus(dev, target, our_addr)
353 1.5 chopps struct sci_softc *dev;
354 1.1 chopps u_char target, our_addr;
355 1.1 chopps {
356 1.1 chopps register int timeo = 2500;
357 1.1 chopps
358 1.5 chopps QPRINTF (("sciselectbus %d\n", target));
359 1.1 chopps
360 1.1 chopps /* if we're already selected, return */
361 1.1 chopps if (dev->sc_flags & SCI_SELECTED) /* XXXX */
362 1.1 chopps return 1;
363 1.1 chopps
364 1.1 chopps if ((*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
365 1.1 chopps (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
366 1.1 chopps (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
367 1.1 chopps return 1;
368 1.1 chopps
369 1.1 chopps *dev->sci_tcmd = 0;
370 1.1 chopps *dev->sci_odata = 0x80 + (1 << target);
371 1.1 chopps *dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_SEL;
372 1.1 chopps while ((*dev->sci_bus_csr & SCI_BUS_BSY) == 0) {
373 1.1 chopps if (--timeo > 0) {
374 1.6 chopps delay(100);
375 1.1 chopps } else {
376 1.1 chopps break;
377 1.1 chopps }
378 1.1 chopps }
379 1.1 chopps if (timeo) {
380 1.1 chopps *dev->sci_icmd = 0;
381 1.1 chopps dev->sc_flags |= SCI_SELECTED;
382 1.1 chopps return (0);
383 1.1 chopps }
384 1.1 chopps *dev->sci_icmd = 0;
385 1.1 chopps return (1);
386 1.1 chopps }
387 1.1 chopps
388 1.5 chopps int
389 1.5 chopps sci_ixfer_out(dev, len, buf, phase)
390 1.1 chopps register struct sci_softc *dev;
391 1.1 chopps int len;
392 1.1 chopps register u_char *buf;
393 1.1 chopps int phase;
394 1.1 chopps {
395 1.1 chopps register int wait = sci_data_wait;
396 1.1 chopps u_char csr;
397 1.1 chopps
398 1.5 chopps QPRINTF(("sci_ixfer_out {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
399 1.1 chopps len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
400 1.1 chopps buf[6], buf[7], buf[8], buf[9]));
401 1.1 chopps
402 1.1 chopps *dev->sci_tcmd = phase;
403 1.1 chopps *dev->sci_icmd = SCI_ICMD_DATA;
404 1.1 chopps for (;len > 0; len--) {
405 1.1 chopps csr = *dev->sci_bus_csr;
406 1.1 chopps while (!(csr & SCI_BUS_REQ)) {
407 1.1 chopps if ((csr & SCI_BUS_BSY) == 0 || --wait < 0) {
408 1.1 chopps #ifdef DEBUG
409 1.1 chopps if (sci_debug)
410 1.19 christos printf("sci_ixfer_out fail: l%d i%x w%d\n",
411 1.1 chopps len, csr, wait);
412 1.1 chopps #endif
413 1.1 chopps return (len);
414 1.1 chopps }
415 1.6 chopps delay(1);
416 1.1 chopps csr = *dev->sci_bus_csr;
417 1.1 chopps }
418 1.1 chopps
419 1.1 chopps if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
420 1.1 chopps break;
421 1.1 chopps *dev->sci_odata = *buf;
422 1.1 chopps *dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_ACK;
423 1.1 chopps buf++;
424 1.1 chopps while (*dev->sci_bus_csr & SCI_BUS_REQ);
425 1.1 chopps *dev->sci_icmd = SCI_ICMD_DATA;
426 1.1 chopps }
427 1.1 chopps
428 1.5 chopps QPRINTF(("sci_ixfer_out done\n"));
429 1.1 chopps return (0);
430 1.1 chopps }
431 1.1 chopps
432 1.5 chopps void
433 1.5 chopps sci_ixfer_in(dev, len, buf, phase)
434 1.1 chopps struct sci_softc *dev;
435 1.1 chopps int len;
436 1.1 chopps register u_char *buf;
437 1.1 chopps int phase;
438 1.1 chopps {
439 1.1 chopps int wait = sci_data_wait;
440 1.1 chopps u_char csr;
441 1.1 chopps volatile register u_char *sci_bus_csr = dev->sci_bus_csr;
442 1.1 chopps volatile register u_char *sci_data = dev->sci_data;
443 1.1 chopps volatile register u_char *sci_icmd = dev->sci_icmd;
444 1.16 veego #ifdef DEBUG
445 1.16 veego u_char *obp = buf;
446 1.16 veego #endif
447 1.1 chopps
448 1.1 chopps csr = *sci_bus_csr;
449 1.1 chopps
450 1.5 chopps QPRINTF(("sci_ixfer_in %d, csr=%02x\n", len, csr));
451 1.1 chopps
452 1.1 chopps *dev->sci_tcmd = phase;
453 1.1 chopps *sci_icmd = 0;
454 1.1 chopps for (;len > 0; len--) {
455 1.1 chopps csr = *sci_bus_csr;
456 1.1 chopps while (!(csr & SCI_BUS_REQ)) {
457 1.1 chopps if (!(csr & SCI_BUS_BSY) || --wait < 0) {
458 1.1 chopps #ifdef DEBUG
459 1.1 chopps if (sci_debug)
460 1.19 christos printf("sci_ixfer_in fail: l%d i%x w%d\n",
461 1.1 chopps len, csr, wait);
462 1.1 chopps #endif
463 1.1 chopps return;
464 1.1 chopps }
465 1.1 chopps
466 1.6 chopps delay(1);
467 1.1 chopps csr = *sci_bus_csr;
468 1.1 chopps }
469 1.1 chopps
470 1.1 chopps if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
471 1.1 chopps break;
472 1.1 chopps *buf = *sci_data;
473 1.1 chopps *sci_icmd = SCI_ICMD_ACK;
474 1.1 chopps buf++;
475 1.1 chopps while (*sci_bus_csr & SCI_BUS_REQ);
476 1.1 chopps *sci_icmd = 0;
477 1.1 chopps }
478 1.1 chopps
479 1.5 chopps QPRINTF(("sci_ixfer_in {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
480 1.1 chopps len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
481 1.1 chopps obp[6], obp[7], obp[8], obp[9]));
482 1.1 chopps }
483 1.1 chopps
484 1.1 chopps /*
485 1.1 chopps * SCSI 'immediate' command: issue a command to some SCSI device
486 1.1 chopps * and get back an 'immediate' response (i.e., do programmed xfer
487 1.1 chopps * to get the response data). 'cbuf' is a buffer containing a scsi
488 1.1 chopps * command of length clen bytes. 'buf' is a buffer of length 'len'
489 1.1 chopps * bytes for data. The transfer direction is determined by the device
490 1.1 chopps * (i.e., by the scsi bus data xfer phase). If 'len' is zero, the
491 1.1 chopps * command must supply no data. 'xferphase' is the bus phase the
492 1.1 chopps * caller expects to happen after the command is issued. It should
493 1.1 chopps * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
494 1.1 chopps */
495 1.5 chopps int
496 1.5 chopps sciicmd(dev, target, cbuf, clen, buf, len, xferphase)
497 1.1 chopps struct sci_softc *dev;
498 1.5 chopps void *cbuf, *buf;
499 1.5 chopps int clen, len;
500 1.1 chopps u_char xferphase;
501 1.1 chopps {
502 1.16 veego u_char phase;
503 1.1 chopps register int wait;
504 1.1 chopps
505 1.1 chopps /* select the SCSI bus (it's an error if bus isn't free) */
506 1.5 chopps if (sciselectbus (dev, target, dev->sc_scsi_addr))
507 1.1 chopps return -1;
508 1.1 chopps /*
509 1.1 chopps * Wait for a phase change (or error) then let the device
510 1.1 chopps * sequence us through the various SCSI phases.
511 1.1 chopps */
512 1.1 chopps dev->sc_stat[0] = 0xff;
513 1.1 chopps dev->sc_msg[0] = 0xff;
514 1.1 chopps phase = CMD_PHASE;
515 1.1 chopps while (1) {
516 1.1 chopps wait = sci_cmd_wait;
517 1.1 chopps
518 1.1 chopps while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) == SCI_BUS_BSY);
519 1.1 chopps
520 1.1 chopps QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
521 1.1 chopps if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
522 1.1 chopps return -1;
523 1.1 chopps }
524 1.1 chopps phase = SCI_PHASE(*dev->sci_bus_csr);
525 1.1 chopps
526 1.1 chopps switch (phase) {
527 1.1 chopps case CMD_PHASE:
528 1.5 chopps if (sci_ixfer_out (dev, clen, cbuf, phase))
529 1.1 chopps goto abort;
530 1.1 chopps phase = xferphase;
531 1.1 chopps break;
532 1.1 chopps
533 1.1 chopps case DATA_IN_PHASE:
534 1.1 chopps if (len <= 0)
535 1.1 chopps goto abort;
536 1.1 chopps wait = sci_data_wait;
537 1.5 chopps sci_ixfer_in (dev, len, buf, phase);
538 1.1 chopps phase = STATUS_PHASE;
539 1.1 chopps break;
540 1.1 chopps
541 1.1 chopps case DATA_OUT_PHASE:
542 1.1 chopps if (len <= 0)
543 1.1 chopps goto abort;
544 1.1 chopps wait = sci_data_wait;
545 1.5 chopps if (sci_ixfer_out (dev, len, buf, phase))
546 1.1 chopps goto abort;
547 1.1 chopps phase = STATUS_PHASE;
548 1.1 chopps break;
549 1.1 chopps
550 1.1 chopps case MESG_IN_PHASE:
551 1.1 chopps dev->sc_msg[0] = 0xff;
552 1.5 chopps sci_ixfer_in (dev, 1, dev->sc_msg,phase);
553 1.1 chopps dev->sc_flags &= ~SCI_SELECTED;
554 1.1 chopps while (*dev->sci_bus_csr & SCI_BUS_BSY);
555 1.1 chopps goto out;
556 1.1 chopps break;
557 1.1 chopps
558 1.1 chopps case MESG_OUT_PHASE:
559 1.1 chopps phase = STATUS_PHASE;
560 1.1 chopps break;
561 1.1 chopps
562 1.1 chopps case STATUS_PHASE:
563 1.5 chopps sci_ixfer_in (dev, 1, dev->sc_stat, phase);
564 1.1 chopps phase = MESG_IN_PHASE;
565 1.1 chopps break;
566 1.1 chopps
567 1.1 chopps case BUS_FREE_PHASE:
568 1.1 chopps goto out;
569 1.1 chopps
570 1.1 chopps default:
571 1.19 christos printf("sci: unexpected phase %d in icmd from %d\n",
572 1.1 chopps phase, target);
573 1.1 chopps goto abort;
574 1.1 chopps }
575 1.1 chopps #if 0
576 1.1 chopps if (wait <= 0)
577 1.1 chopps goto abort;
578 1.1 chopps #endif
579 1.1 chopps }
580 1.1 chopps
581 1.1 chopps abort:
582 1.5 chopps sciabort(dev, "icmd");
583 1.1 chopps out:
584 1.1 chopps QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
585 1.1 chopps return (dev->sc_stat[0]);
586 1.1 chopps }
587 1.1 chopps
588 1.1 chopps int
589 1.5 chopps scigo(dev, xs)
590 1.5 chopps struct sci_softc *dev;
591 1.20 bouyer struct scsipi_xfer *xs;
592 1.1 chopps {
593 1.16 veego int count, target;
594 1.16 veego u_char phase, *addr;
595 1.5 chopps
596 1.25 bouyer target = xs->xs_periph->periph_target;
597 1.5 chopps count = xs->datalen;
598 1.5 chopps addr = xs->data;
599 1.1 chopps
600 1.1 chopps if (sci_no_dma) {
601 1.5 chopps sciicmd (dev, target, (u_char *) xs->cmd, xs->cmdlen,
602 1.1 chopps addr, count,
603 1.21 thorpej xs->xs_control & XS_CTL_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE);
604 1.1 chopps
605 1.5 chopps return (1);
606 1.1 chopps }
607 1.1 chopps
608 1.1 chopps /* select the SCSI bus (it's an error if bus isn't free) */
609 1.5 chopps if (sciselectbus (dev, target, dev->sc_scsi_addr))
610 1.1 chopps return -1;
611 1.1 chopps /*
612 1.1 chopps * Wait for a phase change (or error) then let the device
613 1.1 chopps * sequence us through the various SCSI phases.
614 1.1 chopps */
615 1.1 chopps dev->sc_stat[0] = 0xff;
616 1.1 chopps dev->sc_msg[0] = 0xff;
617 1.1 chopps phase = CMD_PHASE;
618 1.1 chopps while (1) {
619 1.1 chopps while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) ==
620 1.1 chopps SCI_BUS_BSY);
621 1.1 chopps
622 1.1 chopps QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
623 1.1 chopps if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
624 1.1 chopps goto abort;
625 1.1 chopps }
626 1.1 chopps phase = SCI_PHASE(*dev->sci_bus_csr);
627 1.1 chopps
628 1.1 chopps switch (phase) {
629 1.1 chopps case CMD_PHASE:
630 1.16 veego if (sci_ixfer_out (dev, xs->cmdlen, (u_char *) xs->cmd, phase))
631 1.1 chopps goto abort;
632 1.21 thorpej phase = xs->xs_control & XS_CTL_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE;
633 1.1 chopps break;
634 1.1 chopps
635 1.1 chopps case DATA_IN_PHASE:
636 1.1 chopps if (count <= 0)
637 1.1 chopps goto abort;
638 1.1 chopps /* XXX use psuedo DMA if available */
639 1.1 chopps if (count >= 128 && dev->dma_xfer_in)
640 1.1 chopps (*dev->dma_xfer_in)(dev, count, addr, phase);
641 1.1 chopps else
642 1.5 chopps sci_ixfer_in (dev, count, addr, phase);
643 1.1 chopps phase = STATUS_PHASE;
644 1.1 chopps break;
645 1.1 chopps
646 1.1 chopps case DATA_OUT_PHASE:
647 1.1 chopps if (count <= 0)
648 1.1 chopps goto abort;
649 1.1 chopps /* XXX use psuedo DMA if available */
650 1.1 chopps if (count >= 128 && dev->dma_xfer_out)
651 1.1 chopps (*dev->dma_xfer_out)(dev, count, addr, phase);
652 1.1 chopps else
653 1.5 chopps if (sci_ixfer_out (dev, count, addr, phase))
654 1.1 chopps goto abort;
655 1.1 chopps phase = STATUS_PHASE;
656 1.1 chopps break;
657 1.1 chopps
658 1.1 chopps case MESG_IN_PHASE:
659 1.1 chopps dev->sc_msg[0] = 0xff;
660 1.5 chopps sci_ixfer_in (dev, 1, dev->sc_msg,phase);
661 1.1 chopps dev->sc_flags &= ~SCI_SELECTED;
662 1.1 chopps while (*dev->sci_bus_csr & SCI_BUS_BSY);
663 1.1 chopps goto out;
664 1.1 chopps break;
665 1.1 chopps
666 1.1 chopps case MESG_OUT_PHASE:
667 1.1 chopps phase = STATUS_PHASE;
668 1.1 chopps break;
669 1.1 chopps
670 1.1 chopps case STATUS_PHASE:
671 1.5 chopps sci_ixfer_in (dev, 1, dev->sc_stat, phase);
672 1.1 chopps phase = MESG_IN_PHASE;
673 1.1 chopps break;
674 1.1 chopps
675 1.1 chopps case BUS_FREE_PHASE:
676 1.1 chopps goto out;
677 1.1 chopps
678 1.1 chopps default:
679 1.19 christos printf("sci: unexpected phase %d in icmd from %d\n",
680 1.5 chopps phase, target);
681 1.1 chopps goto abort;
682 1.1 chopps }
683 1.1 chopps }
684 1.1 chopps
685 1.1 chopps abort:
686 1.5 chopps sciabort(dev, "go");
687 1.1 chopps out:
688 1.1 chopps QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
689 1.5 chopps return (1);
690 1.1 chopps }
691