aha.c revision 1.4 1 1.4 mycroft /* $NetBSD: aha.c,v 1.4 1997/03/28 23:47:08 mycroft Exp $ */
2 1.1 mycroft
3 1.1 mycroft #undef AHADIAG
4 1.1 mycroft #ifdef DDB
5 1.1 mycroft #define integrate
6 1.1 mycroft #else
7 1.1 mycroft #define integrate static inline
8 1.1 mycroft #endif
9 1.1 mycroft
10 1.1 mycroft /*
11 1.4 mycroft * Copyright (c) 1994, 1996, 1997 Charles M. Hannum. All rights reserved.
12 1.1 mycroft *
13 1.1 mycroft * Redistribution and use in source and binary forms, with or without
14 1.1 mycroft * modification, are permitted provided that the following conditions
15 1.1 mycroft * are met:
16 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
17 1.1 mycroft * notice, this list of conditions and the following disclaimer.
18 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
20 1.1 mycroft * documentation and/or other materials provided with the distribution.
21 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
22 1.1 mycroft * must display the following acknowledgement:
23 1.1 mycroft * This product includes software developed by Charles M. Hannum.
24 1.1 mycroft * 4. The name of the author may not be used to endorse or promote products
25 1.1 mycroft * derived from this software without specific prior written permission.
26 1.1 mycroft *
27 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 1.1 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 1.1 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 1.1 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 1.1 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 1.1 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 1.1 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 1.1 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 1.1 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 1.1 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 1.1 mycroft */
38 1.1 mycroft
39 1.1 mycroft /*
40 1.1 mycroft * Originally written by Julian Elischer (julian (at) tfs.com)
41 1.1 mycroft * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 1.1 mycroft *
43 1.1 mycroft * TRW Financial Systems, in accordance with their agreement with Carnegie
44 1.1 mycroft * Mellon University, makes this software available to CMU to distribute
45 1.1 mycroft * or use in any manner that they see fit as long as this message is kept with
46 1.1 mycroft * the software. For this reason TFS also grants any other persons or
47 1.1 mycroft * organisations permission to use or modify this software.
48 1.1 mycroft *
49 1.1 mycroft * TFS supplies this software to be publicly redistributed
50 1.1 mycroft * on the understanding that TFS is not responsible for the correct
51 1.1 mycroft * functioning of this software in any circumstances.
52 1.1 mycroft */
53 1.1 mycroft
54 1.1 mycroft #include <sys/types.h>
55 1.1 mycroft #include <sys/param.h>
56 1.1 mycroft #include <sys/systm.h>
57 1.1 mycroft #include <sys/kernel.h>
58 1.1 mycroft #include <sys/errno.h>
59 1.1 mycroft #include <sys/ioctl.h>
60 1.1 mycroft #include <sys/device.h>
61 1.1 mycroft #include <sys/malloc.h>
62 1.1 mycroft #include <sys/buf.h>
63 1.1 mycroft #include <sys/proc.h>
64 1.1 mycroft #include <sys/user.h>
65 1.1 mycroft
66 1.1 mycroft #include <machine/bus.h>
67 1.1 mycroft #include <machine/intr.h>
68 1.1 mycroft
69 1.1 mycroft #include <scsi/scsi_all.h>
70 1.1 mycroft #include <scsi/scsiconf.h>
71 1.1 mycroft
72 1.1 mycroft #include <dev/ic/ahareg.h>
73 1.1 mycroft #include <dev/ic/ahavar.h>
74 1.1 mycroft
75 1.1 mycroft #ifndef DDB
76 1.1 mycroft #define Debugger() panic("should call debugger here (aha1542.c)")
77 1.1 mycroft #endif /* ! DDB */
78 1.1 mycroft
79 1.1 mycroft #define KVTOPHYS(x) vtophys(x)
80 1.1 mycroft
81 1.1 mycroft #ifdef AHADEBUG
82 1.1 mycroft int aha_debug = 1;
83 1.1 mycroft #endif /* AHADEBUG */
84 1.1 mycroft
85 1.1 mycroft int aha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct aha_softc *, int,
86 1.1 mycroft u_char *, int, u_char *));
87 1.1 mycroft integrate void aha_finish_ccbs __P((struct aha_softc *));
88 1.1 mycroft integrate void aha_reset_ccb __P((struct aha_softc *, struct aha_ccb *));
89 1.1 mycroft void aha_free_ccb __P((struct aha_softc *, struct aha_ccb *));
90 1.1 mycroft integrate void aha_init_ccb __P((struct aha_softc *, struct aha_ccb *));
91 1.1 mycroft struct aha_ccb *aha_get_ccb __P((struct aha_softc *, int));
92 1.1 mycroft struct aha_ccb *aha_ccb_phys_kv __P((struct aha_softc *, u_long));
93 1.1 mycroft void aha_queue_ccb __P((struct aha_softc *, struct aha_ccb *));
94 1.1 mycroft void aha_collect_mbo __P((struct aha_softc *));
95 1.1 mycroft void aha_start_ccbs __P((struct aha_softc *));
96 1.1 mycroft void aha_done __P((struct aha_softc *, struct aha_ccb *));
97 1.1 mycroft void aha_init __P((struct aha_softc *));
98 1.1 mycroft void aha_inquire_setup_information __P((struct aha_softc *));
99 1.1 mycroft void ahaminphys __P((struct buf *));
100 1.1 mycroft int aha_scsi_cmd __P((struct scsi_xfer *));
101 1.1 mycroft int aha_poll __P((struct aha_softc *, struct scsi_xfer *, int));
102 1.1 mycroft void aha_timeout __P((void *arg));
103 1.1 mycroft
104 1.1 mycroft struct scsi_adapter aha_switch = {
105 1.1 mycroft aha_scsi_cmd,
106 1.1 mycroft ahaminphys,
107 1.1 mycroft 0,
108 1.1 mycroft 0,
109 1.1 mycroft };
110 1.1 mycroft
111 1.1 mycroft /* the below structure is so we have a default dev struct for out link struct */
112 1.1 mycroft struct scsi_device aha_dev = {
113 1.1 mycroft NULL, /* Use default error handler */
114 1.1 mycroft NULL, /* have a queue, served by this */
115 1.1 mycroft NULL, /* have no async handler */
116 1.1 mycroft NULL, /* Use default 'done' routine */
117 1.1 mycroft };
118 1.1 mycroft
119 1.1 mycroft struct cfdriver aha_cd = {
120 1.1 mycroft NULL, "aha", DV_DULL
121 1.1 mycroft };
122 1.1 mycroft
123 1.1 mycroft #define AHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
124 1.1 mycroft #define AHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
125 1.1 mycroft
126 1.1 mycroft /*
127 1.1 mycroft * aha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
128 1.1 mycroft *
129 1.1 mycroft * Activate Adapter command
130 1.1 mycroft * icnt: number of args (outbound bytes including opcode)
131 1.1 mycroft * ibuf: argument buffer
132 1.1 mycroft * ocnt: number of expected returned bytes
133 1.1 mycroft * obuf: result buffer
134 1.1 mycroft * wait: number of seconds to wait for response
135 1.1 mycroft *
136 1.1 mycroft * Performs an adapter command through the ports. Not to be confused with a
137 1.1 mycroft * scsi command, which is read in via the dma; one of the adapter commands
138 1.1 mycroft * tells it to read in a scsi command.
139 1.1 mycroft */
140 1.1 mycroft int
141 1.1 mycroft aha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
142 1.1 mycroft bus_space_tag_t iot;
143 1.1 mycroft bus_space_handle_t ioh;
144 1.1 mycroft struct aha_softc *sc;
145 1.1 mycroft int icnt, ocnt;
146 1.1 mycroft u_char *ibuf, *obuf;
147 1.1 mycroft {
148 1.1 mycroft const char *name;
149 1.1 mycroft register int i;
150 1.1 mycroft int wait;
151 1.1 mycroft u_char sts;
152 1.1 mycroft u_char opcode = ibuf[0];
153 1.1 mycroft
154 1.1 mycroft if (sc != NULL)
155 1.1 mycroft name = sc->sc_dev.dv_xname;
156 1.1 mycroft else
157 1.1 mycroft name = "(aha probe)";
158 1.1 mycroft
159 1.1 mycroft /*
160 1.1 mycroft * Calculate a reasonable timeout for the command.
161 1.1 mycroft */
162 1.1 mycroft switch (opcode) {
163 1.1 mycroft case AHA_INQUIRE_DEVICES:
164 1.1 mycroft wait = 90 * 20000;
165 1.1 mycroft break;
166 1.1 mycroft default:
167 1.1 mycroft wait = 1 * 20000;
168 1.1 mycroft break;
169 1.1 mycroft }
170 1.1 mycroft
171 1.1 mycroft /*
172 1.1 mycroft * Wait for the adapter to go idle, unless it's one of
173 1.1 mycroft * the commands which don't need this
174 1.1 mycroft */
175 1.1 mycroft if (opcode != AHA_MBO_INTR_EN) {
176 1.1 mycroft for (i = 20000; i; i--) { /* 1 sec? */
177 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
178 1.1 mycroft if (sts & AHA_STAT_IDLE)
179 1.1 mycroft break;
180 1.1 mycroft delay(50);
181 1.1 mycroft }
182 1.1 mycroft if (!i) {
183 1.1 mycroft printf("%s: aha_cmd, host not idle(0x%x)\n",
184 1.1 mycroft name, sts);
185 1.1 mycroft return (1);
186 1.1 mycroft }
187 1.1 mycroft }
188 1.1 mycroft /*
189 1.1 mycroft * Now that it is idle, if we expect output, preflush the
190 1.1 mycroft * queue feeding to us.
191 1.1 mycroft */
192 1.1 mycroft if (ocnt) {
193 1.1 mycroft while ((bus_space_read_1(iot, ioh, AHA_STAT_PORT)) & AHA_STAT_DF)
194 1.1 mycroft bus_space_read_1(iot, ioh, AHA_DATA_PORT);
195 1.1 mycroft }
196 1.1 mycroft /*
197 1.1 mycroft * Output the command and the number of arguments given
198 1.1 mycroft * for each byte, first check the port is empty.
199 1.1 mycroft */
200 1.1 mycroft while (icnt--) {
201 1.1 mycroft for (i = wait; i; i--) {
202 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
203 1.1 mycroft if (!(sts & AHA_STAT_CDF))
204 1.1 mycroft break;
205 1.1 mycroft delay(50);
206 1.1 mycroft }
207 1.1 mycroft if (!i) {
208 1.1 mycroft if (opcode != AHA_INQUIRE_REVISION)
209 1.1 mycroft printf("%s: aha_cmd, cmd/data port full\n", name);
210 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_SRST);
211 1.1 mycroft return (1);
212 1.1 mycroft }
213 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CMD_PORT, *ibuf++);
214 1.1 mycroft }
215 1.1 mycroft /*
216 1.1 mycroft * If we expect input, loop that many times, each time,
217 1.1 mycroft * looking for the data register to have valid data
218 1.1 mycroft */
219 1.1 mycroft while (ocnt--) {
220 1.1 mycroft for (i = wait; i; i--) {
221 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
222 1.1 mycroft if (sts & AHA_STAT_DF)
223 1.1 mycroft break;
224 1.1 mycroft delay(50);
225 1.1 mycroft }
226 1.1 mycroft if (!i) {
227 1.1 mycroft if (opcode != AHA_INQUIRE_REVISION)
228 1.1 mycroft printf("%s: aha_cmd, cmd/data port empty %d\n",
229 1.1 mycroft name, ocnt);
230 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_SRST);
231 1.1 mycroft return (1);
232 1.1 mycroft }
233 1.1 mycroft *obuf++ = bus_space_read_1(iot, ioh, AHA_DATA_PORT);
234 1.1 mycroft }
235 1.1 mycroft /*
236 1.1 mycroft * Wait for the board to report a finished instruction.
237 1.1 mycroft * We may get an extra interrupt for the HACC signal, but this is
238 1.1 mycroft * unimportant.
239 1.1 mycroft */
240 1.1 mycroft if (opcode != AHA_MBO_INTR_EN) {
241 1.1 mycroft for (i = 20000; i; i--) { /* 1 sec? */
242 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_INTR_PORT);
243 1.1 mycroft /* XXX Need to save this in the interrupt handler? */
244 1.1 mycroft if (sts & AHA_INTR_HACC)
245 1.1 mycroft break;
246 1.1 mycroft delay(50);
247 1.1 mycroft }
248 1.1 mycroft if (!i) {
249 1.1 mycroft printf("%s: aha_cmd, host not finished(0x%x)\n",
250 1.1 mycroft name, sts);
251 1.1 mycroft return (1);
252 1.1 mycroft }
253 1.1 mycroft }
254 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_IRST);
255 1.1 mycroft return (0);
256 1.1 mycroft }
257 1.1 mycroft
258 1.1 mycroft void
259 1.4 mycroft aha_attach(sc, apd)
260 1.1 mycroft struct aha_softc *sc;
261 1.4 mycroft struct aha_probe_data *apd;
262 1.1 mycroft {
263 1.1 mycroft
264 1.1 mycroft aha_inquire_setup_information(sc);
265 1.1 mycroft aha_init(sc);
266 1.1 mycroft TAILQ_INIT(&sc->sc_free_ccb);
267 1.1 mycroft TAILQ_INIT(&sc->sc_waiting_ccb);
268 1.1 mycroft
269 1.1 mycroft /*
270 1.1 mycroft * fill in the prototype scsi_link.
271 1.1 mycroft */
272 1.1 mycroft sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
273 1.1 mycroft sc->sc_link.adapter_softc = sc;
274 1.4 mycroft sc->sc_link.adapter_target = apd->sc_scsi_dev;
275 1.1 mycroft sc->sc_link.adapter = &aha_switch;
276 1.1 mycroft sc->sc_link.device = &aha_dev;
277 1.1 mycroft sc->sc_link.openings = 2;
278 1.1 mycroft sc->sc_link.max_target = 7;
279 1.1 mycroft
280 1.1 mycroft /*
281 1.1 mycroft * ask the adapter what subunits are present
282 1.1 mycroft */
283 1.1 mycroft config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
284 1.1 mycroft }
285 1.1 mycroft
286 1.1 mycroft integrate void
287 1.1 mycroft aha_finish_ccbs(sc)
288 1.1 mycroft struct aha_softc *sc;
289 1.1 mycroft {
290 1.1 mycroft struct aha_mbx_in *wmbi;
291 1.1 mycroft struct aha_ccb *ccb;
292 1.1 mycroft int i;
293 1.1 mycroft
294 1.1 mycroft wmbi = wmbx->tmbi;
295 1.1 mycroft
296 1.1 mycroft if (wmbi->stat == AHA_MBI_FREE) {
297 1.1 mycroft for (i = 0; i < AHA_MBX_SIZE; i++) {
298 1.1 mycroft if (wmbi->stat != AHA_MBI_FREE) {
299 1.1 mycroft printf("%s: mbi not in round-robin order\n",
300 1.1 mycroft sc->sc_dev.dv_xname);
301 1.1 mycroft goto AGAIN;
302 1.1 mycroft }
303 1.1 mycroft aha_nextmbx(wmbi, wmbx, mbi);
304 1.1 mycroft }
305 1.1 mycroft #ifdef AHADIAGnot
306 1.1 mycroft printf("%s: mbi interrupt with no full mailboxes\n",
307 1.1 mycroft sc->sc_dev.dv_xname);
308 1.1 mycroft #endif
309 1.1 mycroft return;
310 1.1 mycroft }
311 1.1 mycroft
312 1.1 mycroft AGAIN:
313 1.1 mycroft do {
314 1.1 mycroft ccb = aha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
315 1.1 mycroft if (!ccb) {
316 1.1 mycroft printf("%s: bad mbi ccb pointer; skipping\n",
317 1.1 mycroft sc->sc_dev.dv_xname);
318 1.1 mycroft goto next;
319 1.1 mycroft }
320 1.1 mycroft
321 1.1 mycroft #ifdef AHADEBUG
322 1.1 mycroft if (aha_debug) {
323 1.1 mycroft u_char *cp = &ccb->scsi_cmd;
324 1.1 mycroft printf("op=%x %x %x %x %x %x\n",
325 1.1 mycroft cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
326 1.1 mycroft printf("stat %x for mbi addr = 0x%08x, ",
327 1.1 mycroft wmbi->stat, wmbi);
328 1.1 mycroft printf("ccb addr = 0x%x\n", ccb);
329 1.1 mycroft }
330 1.1 mycroft #endif /* AHADEBUG */
331 1.1 mycroft
332 1.1 mycroft switch (wmbi->stat) {
333 1.1 mycroft case AHA_MBI_OK:
334 1.1 mycroft case AHA_MBI_ERROR:
335 1.1 mycroft if ((ccb->flags & CCB_ABORT) != 0) {
336 1.1 mycroft /*
337 1.1 mycroft * If we already started an abort, wait for it
338 1.1 mycroft * to complete before clearing the CCB. We
339 1.1 mycroft * could instead just clear CCB_SENDING, but
340 1.1 mycroft * what if the mailbox was already received?
341 1.1 mycroft * The worst that happens here is that we clear
342 1.1 mycroft * the CCB a bit later than we need to. BFD.
343 1.1 mycroft */
344 1.1 mycroft goto next;
345 1.1 mycroft }
346 1.1 mycroft break;
347 1.1 mycroft
348 1.1 mycroft case AHA_MBI_ABORT:
349 1.1 mycroft case AHA_MBI_UNKNOWN:
350 1.1 mycroft /*
351 1.1 mycroft * Even if the CCB wasn't found, we clear it anyway.
352 1.1 mycroft * See preceeding comment.
353 1.1 mycroft */
354 1.1 mycroft break;
355 1.1 mycroft
356 1.1 mycroft default:
357 1.1 mycroft printf("%s: bad mbi status %02x; skipping\n",
358 1.1 mycroft sc->sc_dev.dv_xname, wmbi->stat);
359 1.1 mycroft goto next;
360 1.1 mycroft }
361 1.1 mycroft
362 1.1 mycroft untimeout(aha_timeout, ccb);
363 1.1 mycroft aha_done(sc, ccb);
364 1.1 mycroft
365 1.1 mycroft next:
366 1.1 mycroft wmbi->stat = AHA_MBI_FREE;
367 1.1 mycroft aha_nextmbx(wmbi, wmbx, mbi);
368 1.1 mycroft } while (wmbi->stat != AHA_MBI_FREE);
369 1.1 mycroft
370 1.1 mycroft wmbx->tmbi = wmbi;
371 1.1 mycroft }
372 1.1 mycroft
373 1.1 mycroft /*
374 1.1 mycroft * Catch an interrupt from the adaptor
375 1.1 mycroft */
376 1.1 mycroft int
377 1.1 mycroft aha_intr(arg)
378 1.1 mycroft void *arg;
379 1.1 mycroft {
380 1.1 mycroft struct aha_softc *sc = arg;
381 1.1 mycroft bus_space_tag_t iot = sc->sc_iot;
382 1.1 mycroft bus_space_handle_t ioh = sc->sc_ioh;
383 1.1 mycroft u_char sts;
384 1.1 mycroft
385 1.1 mycroft #ifdef AHADEBUG
386 1.1 mycroft printf("%s: aha_intr ", sc->sc_dev.dv_xname);
387 1.1 mycroft #endif /*AHADEBUG */
388 1.1 mycroft
389 1.1 mycroft /*
390 1.1 mycroft * First acknowlege the interrupt, Then if it's not telling about
391 1.1 mycroft * a completed operation just return.
392 1.1 mycroft */
393 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_INTR_PORT);
394 1.1 mycroft if ((sts & AHA_INTR_ANYINTR) == 0)
395 1.1 mycroft return (0);
396 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_IRST);
397 1.1 mycroft
398 1.1 mycroft #ifdef AHADIAG
399 1.1 mycroft /* Make sure we clear CCB_SENDING before finishing a CCB. */
400 1.1 mycroft aha_collect_mbo(sc);
401 1.1 mycroft #endif
402 1.1 mycroft
403 1.1 mycroft /* Mail box out empty? */
404 1.1 mycroft if (sts & AHA_INTR_MBOA) {
405 1.1 mycroft struct aha_toggle toggle;
406 1.1 mycroft
407 1.1 mycroft toggle.cmd.opcode = AHA_MBO_INTR_EN;
408 1.1 mycroft toggle.cmd.enable = 0;
409 1.1 mycroft aha_cmd(iot, ioh, sc,
410 1.1 mycroft sizeof(toggle.cmd), (u_char *)&toggle.cmd,
411 1.1 mycroft 0, (u_char *)0);
412 1.1 mycroft aha_start_ccbs(sc);
413 1.1 mycroft }
414 1.1 mycroft
415 1.1 mycroft /* Mail box in full? */
416 1.1 mycroft if (sts & AHA_INTR_MBIF)
417 1.1 mycroft aha_finish_ccbs(sc);
418 1.1 mycroft
419 1.1 mycroft return (1);
420 1.1 mycroft }
421 1.1 mycroft
422 1.1 mycroft integrate void
423 1.1 mycroft aha_reset_ccb(sc, ccb)
424 1.1 mycroft struct aha_softc *sc;
425 1.1 mycroft struct aha_ccb *ccb;
426 1.1 mycroft {
427 1.1 mycroft
428 1.1 mycroft ccb->flags = 0;
429 1.1 mycroft }
430 1.1 mycroft
431 1.1 mycroft /*
432 1.1 mycroft * A ccb is put onto the free list.
433 1.1 mycroft */
434 1.1 mycroft void
435 1.1 mycroft aha_free_ccb(sc, ccb)
436 1.1 mycroft struct aha_softc *sc;
437 1.1 mycroft struct aha_ccb *ccb;
438 1.1 mycroft {
439 1.1 mycroft int s;
440 1.1 mycroft
441 1.1 mycroft s = splbio();
442 1.1 mycroft
443 1.1 mycroft aha_reset_ccb(sc, ccb);
444 1.1 mycroft TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
445 1.1 mycroft
446 1.1 mycroft /*
447 1.1 mycroft * If there were none, wake anybody waiting for one to come free,
448 1.1 mycroft * starting with queued entries.
449 1.1 mycroft */
450 1.1 mycroft if (ccb->chain.tqe_next == 0)
451 1.1 mycroft wakeup(&sc->sc_free_ccb);
452 1.1 mycroft
453 1.1 mycroft splx(s);
454 1.1 mycroft }
455 1.1 mycroft
456 1.1 mycroft integrate void
457 1.1 mycroft aha_init_ccb(sc, ccb)
458 1.1 mycroft struct aha_softc *sc;
459 1.1 mycroft struct aha_ccb *ccb;
460 1.1 mycroft {
461 1.1 mycroft int hashnum;
462 1.1 mycroft
463 1.1 mycroft bzero(ccb, sizeof(struct aha_ccb));
464 1.1 mycroft /*
465 1.1 mycroft * put in the phystokv hash table
466 1.1 mycroft * Never gets taken out.
467 1.1 mycroft */
468 1.1 mycroft ccb->hashkey = KVTOPHYS(ccb);
469 1.1 mycroft hashnum = CCB_HASH(ccb->hashkey);
470 1.1 mycroft ccb->nexthash = sc->sc_ccbhash[hashnum];
471 1.1 mycroft sc->sc_ccbhash[hashnum] = ccb;
472 1.1 mycroft aha_reset_ccb(sc, ccb);
473 1.1 mycroft }
474 1.1 mycroft
475 1.1 mycroft /*
476 1.1 mycroft * Get a free ccb
477 1.1 mycroft *
478 1.1 mycroft * If there are none, see if we can allocate a new one. If so, put it in
479 1.1 mycroft * the hash table too otherwise either return an error or sleep.
480 1.1 mycroft */
481 1.1 mycroft struct aha_ccb *
482 1.1 mycroft aha_get_ccb(sc, flags)
483 1.1 mycroft struct aha_softc *sc;
484 1.1 mycroft int flags;
485 1.1 mycroft {
486 1.1 mycroft struct aha_ccb *ccb;
487 1.1 mycroft int s;
488 1.1 mycroft
489 1.1 mycroft s = splbio();
490 1.1 mycroft
491 1.1 mycroft /*
492 1.1 mycroft * If we can and have to, sleep waiting for one to come free
493 1.1 mycroft * but only if we can't allocate a new one.
494 1.1 mycroft */
495 1.1 mycroft for (;;) {
496 1.1 mycroft ccb = sc->sc_free_ccb.tqh_first;
497 1.1 mycroft if (ccb) {
498 1.1 mycroft TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
499 1.1 mycroft break;
500 1.1 mycroft }
501 1.1 mycroft if (sc->sc_numccbs < AHA_CCB_MAX) {
502 1.1 mycroft ccb = (struct aha_ccb *) malloc(sizeof(struct aha_ccb),
503 1.1 mycroft M_TEMP, M_NOWAIT);
504 1.1 mycroft if (!ccb) {
505 1.1 mycroft printf("%s: can't malloc ccb\n",
506 1.1 mycroft sc->sc_dev.dv_xname);
507 1.1 mycroft goto out;
508 1.1 mycroft }
509 1.1 mycroft aha_init_ccb(sc, ccb);
510 1.1 mycroft sc->sc_numccbs++;
511 1.1 mycroft break;
512 1.1 mycroft }
513 1.1 mycroft if ((flags & SCSI_NOSLEEP) != 0)
514 1.1 mycroft goto out;
515 1.1 mycroft tsleep(&sc->sc_free_ccb, PRIBIO, "ahaccb", 0);
516 1.1 mycroft }
517 1.1 mycroft
518 1.1 mycroft ccb->flags |= CCB_ALLOC;
519 1.1 mycroft
520 1.1 mycroft out:
521 1.1 mycroft splx(s);
522 1.1 mycroft return (ccb);
523 1.1 mycroft }
524 1.1 mycroft
525 1.1 mycroft /*
526 1.1 mycroft * Given a physical address, find the ccb that it corresponds to.
527 1.1 mycroft */
528 1.1 mycroft struct aha_ccb *
529 1.1 mycroft aha_ccb_phys_kv(sc, ccb_phys)
530 1.1 mycroft struct aha_softc *sc;
531 1.1 mycroft u_long ccb_phys;
532 1.1 mycroft {
533 1.1 mycroft int hashnum = CCB_HASH(ccb_phys);
534 1.1 mycroft struct aha_ccb *ccb = sc->sc_ccbhash[hashnum];
535 1.1 mycroft
536 1.1 mycroft while (ccb) {
537 1.1 mycroft if (ccb->hashkey == ccb_phys)
538 1.1 mycroft break;
539 1.1 mycroft ccb = ccb->nexthash;
540 1.1 mycroft }
541 1.1 mycroft return (ccb);
542 1.1 mycroft }
543 1.1 mycroft
544 1.1 mycroft /*
545 1.1 mycroft * Queue a CCB to be sent to the controller, and send it if possible.
546 1.1 mycroft */
547 1.1 mycroft void
548 1.1 mycroft aha_queue_ccb(sc, ccb)
549 1.1 mycroft struct aha_softc *sc;
550 1.1 mycroft struct aha_ccb *ccb;
551 1.1 mycroft {
552 1.1 mycroft
553 1.1 mycroft TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
554 1.1 mycroft aha_start_ccbs(sc);
555 1.1 mycroft }
556 1.1 mycroft
557 1.1 mycroft /*
558 1.1 mycroft * Garbage collect mailboxes that are no longer in use.
559 1.1 mycroft */
560 1.1 mycroft void
561 1.1 mycroft aha_collect_mbo(sc)
562 1.1 mycroft struct aha_softc *sc;
563 1.1 mycroft {
564 1.1 mycroft struct aha_mbx_out *wmbo; /* Mail Box Out pointer */
565 1.1 mycroft #ifdef AHADIAG
566 1.1 mycroft struct aha_ccb *ccb;
567 1.1 mycroft #endif
568 1.1 mycroft
569 1.1 mycroft wmbo = wmbx->cmbo;
570 1.1 mycroft
571 1.1 mycroft while (sc->sc_mbofull > 0) {
572 1.1 mycroft if (wmbo->cmd != AHA_MBO_FREE)
573 1.1 mycroft break;
574 1.1 mycroft
575 1.1 mycroft #ifdef AHADIAG
576 1.1 mycroft ccb = aha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
577 1.1 mycroft ccb->flags &= ~CCB_SENDING;
578 1.1 mycroft #endif
579 1.1 mycroft
580 1.1 mycroft --sc->sc_mbofull;
581 1.1 mycroft aha_nextmbx(wmbo, wmbx, mbo);
582 1.1 mycroft }
583 1.1 mycroft
584 1.1 mycroft wmbx->cmbo = wmbo;
585 1.1 mycroft }
586 1.1 mycroft
587 1.1 mycroft /*
588 1.1 mycroft * Send as many CCBs as we have empty mailboxes for.
589 1.1 mycroft */
590 1.1 mycroft void
591 1.1 mycroft aha_start_ccbs(sc)
592 1.1 mycroft struct aha_softc *sc;
593 1.1 mycroft {
594 1.1 mycroft bus_space_tag_t iot = sc->sc_iot;
595 1.1 mycroft bus_space_handle_t ioh = sc->sc_ioh;
596 1.1 mycroft struct aha_mbx_out *wmbo; /* Mail Box Out pointer */
597 1.1 mycroft struct aha_ccb *ccb;
598 1.1 mycroft
599 1.1 mycroft wmbo = wmbx->tmbo;
600 1.1 mycroft
601 1.1 mycroft while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
602 1.1 mycroft if (sc->sc_mbofull >= AHA_MBX_SIZE) {
603 1.1 mycroft aha_collect_mbo(sc);
604 1.1 mycroft if (sc->sc_mbofull >= AHA_MBX_SIZE) {
605 1.1 mycroft struct aha_toggle toggle;
606 1.1 mycroft
607 1.1 mycroft toggle.cmd.opcode = AHA_MBO_INTR_EN;
608 1.1 mycroft toggle.cmd.enable = 1;
609 1.1 mycroft aha_cmd(iot, ioh, sc,
610 1.1 mycroft sizeof(toggle.cmd), (u_char *)&toggle.cmd,
611 1.1 mycroft 0, (u_char *)0);
612 1.1 mycroft break;
613 1.1 mycroft }
614 1.1 mycroft }
615 1.1 mycroft
616 1.1 mycroft TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
617 1.1 mycroft #ifdef AHADIAG
618 1.1 mycroft ccb->flags |= CCB_SENDING;
619 1.1 mycroft #endif
620 1.1 mycroft
621 1.1 mycroft /* Link ccb to mbo. */
622 1.1 mycroft ltophys(KVTOPHYS(ccb), wmbo->ccb_addr);
623 1.1 mycroft if (ccb->flags & CCB_ABORT)
624 1.1 mycroft wmbo->cmd = AHA_MBO_ABORT;
625 1.1 mycroft else
626 1.1 mycroft wmbo->cmd = AHA_MBO_START;
627 1.1 mycroft
628 1.1 mycroft /* Tell the card to poll immediately. */
629 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CMD_PORT, AHA_START_SCSI);
630 1.1 mycroft
631 1.1 mycroft if ((ccb->xs->flags & SCSI_POLL) == 0)
632 1.1 mycroft timeout(aha_timeout, ccb, (ccb->timeout * hz) / 1000);
633 1.1 mycroft
634 1.1 mycroft ++sc->sc_mbofull;
635 1.1 mycroft aha_nextmbx(wmbo, wmbx, mbo);
636 1.1 mycroft }
637 1.1 mycroft
638 1.1 mycroft wmbx->tmbo = wmbo;
639 1.1 mycroft }
640 1.1 mycroft
641 1.1 mycroft /*
642 1.1 mycroft * We have a ccb which has been processed by the
643 1.1 mycroft * adaptor, now we look to see how the operation
644 1.1 mycroft * went. Wake up the owner if waiting
645 1.1 mycroft */
646 1.1 mycroft void
647 1.1 mycroft aha_done(sc, ccb)
648 1.1 mycroft struct aha_softc *sc;
649 1.1 mycroft struct aha_ccb *ccb;
650 1.1 mycroft {
651 1.1 mycroft struct scsi_sense_data *s1, *s2;
652 1.1 mycroft struct scsi_xfer *xs = ccb->xs;
653 1.1 mycroft
654 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB2, ("aha_done\n"));
655 1.1 mycroft /*
656 1.1 mycroft * Otherwise, put the results of the operation
657 1.1 mycroft * into the xfer and call whoever started it
658 1.1 mycroft */
659 1.1 mycroft #ifdef AHADIAG
660 1.1 mycroft if (ccb->flags & CCB_SENDING) {
661 1.1 mycroft printf("%s: exiting ccb still in transit!\n", sc->sc_dev.dv_xname);
662 1.1 mycroft Debugger();
663 1.1 mycroft return;
664 1.1 mycroft }
665 1.1 mycroft #endif
666 1.1 mycroft if ((ccb->flags & CCB_ALLOC) == 0) {
667 1.1 mycroft printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
668 1.1 mycroft Debugger();
669 1.1 mycroft return;
670 1.1 mycroft }
671 1.1 mycroft if (xs->error == XS_NOERROR) {
672 1.1 mycroft if (ccb->host_stat != AHA_OK) {
673 1.1 mycroft switch (ccb->host_stat) {
674 1.1 mycroft case AHA_SEL_TIMEOUT: /* No response */
675 1.1 mycroft xs->error = XS_SELTIMEOUT;
676 1.1 mycroft break;
677 1.1 mycroft default: /* Other scsi protocol messes */
678 1.1 mycroft printf("%s: host_stat %x\n",
679 1.1 mycroft sc->sc_dev.dv_xname, ccb->host_stat);
680 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
681 1.1 mycroft break;
682 1.1 mycroft }
683 1.1 mycroft } else if (ccb->target_stat != SCSI_OK) {
684 1.1 mycroft switch (ccb->target_stat) {
685 1.1 mycroft case SCSI_CHECK:
686 1.1 mycroft s1 = (struct scsi_sense_data *) (((char *) (&ccb->scsi_cmd)) +
687 1.1 mycroft ccb->scsi_cmd_length);
688 1.1 mycroft s2 = &xs->sense;
689 1.1 mycroft *s2 = *s1;
690 1.1 mycroft xs->error = XS_SENSE;
691 1.1 mycroft break;
692 1.1 mycroft case SCSI_BUSY:
693 1.1 mycroft xs->error = XS_BUSY;
694 1.1 mycroft break;
695 1.1 mycroft default:
696 1.1 mycroft printf("%s: target_stat %x\n",
697 1.1 mycroft sc->sc_dev.dv_xname, ccb->target_stat);
698 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
699 1.1 mycroft break;
700 1.1 mycroft }
701 1.1 mycroft } else
702 1.1 mycroft xs->resid = 0;
703 1.1 mycroft }
704 1.1 mycroft aha_free_ccb(sc, ccb);
705 1.1 mycroft xs->flags |= ITSDONE;
706 1.1 mycroft scsi_done(xs);
707 1.1 mycroft }
708 1.1 mycroft
709 1.1 mycroft /*
710 1.1 mycroft * Find the board and find its irq/drq
711 1.1 mycroft */
712 1.1 mycroft int
713 1.1 mycroft aha_find(iot, ioh, sc)
714 1.1 mycroft bus_space_tag_t iot;
715 1.1 mycroft bus_space_handle_t ioh;
716 1.4 mycroft struct aha_probe_data *sc;
717 1.1 mycroft {
718 1.1 mycroft int i;
719 1.1 mycroft u_char sts;
720 1.1 mycroft struct aha_config config;
721 1.1 mycroft int irq, drq;
722 1.1 mycroft
723 1.1 mycroft /*
724 1.1 mycroft * reset board, If it doesn't respond, assume
725 1.1 mycroft * that it's not there.. good for the probe
726 1.1 mycroft */
727 1.1 mycroft
728 1.1 mycroft bus_space_write_1(iot, ioh, AHA_CTRL_PORT, AHA_CTRL_HRST | AHA_CTRL_SRST);
729 1.1 mycroft
730 1.1 mycroft delay(100);
731 1.1 mycroft for (i = AHA_RESET_TIMEOUT; i; i--) {
732 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
733 1.1 mycroft if (sts == (AHA_STAT_IDLE | AHA_STAT_INIT))
734 1.1 mycroft break;
735 1.1 mycroft delay(1000); /* calibrated in msec */
736 1.1 mycroft }
737 1.1 mycroft if (!i) {
738 1.1 mycroft #ifdef AHADEBUG
739 1.1 mycroft if (aha_debug)
740 1.1 mycroft printf("aha_find: No answer from adaptec board\n");
741 1.1 mycroft #endif /* AHADEBUG */
742 1.1 mycroft return (0);
743 1.1 mycroft }
744 1.1 mycroft
745 1.1 mycroft /*
746 1.1 mycroft * setup dma channel from jumpers and save int
747 1.1 mycroft * level
748 1.1 mycroft */
749 1.1 mycroft delay(1000); /* for Bustek 545 */
750 1.1 mycroft config.cmd.opcode = AHA_INQUIRE_CONFIG;
751 1.4 mycroft aha_cmd(iot, ioh, (struct aha_softc *)0,
752 1.1 mycroft sizeof(config.cmd), (u_char *)&config.cmd,
753 1.1 mycroft sizeof(config.reply), (u_char *)&config.reply);
754 1.1 mycroft switch (config.reply.chan) {
755 1.1 mycroft case EISADMA:
756 1.1 mycroft drq = -1;
757 1.1 mycroft break;
758 1.1 mycroft case CHAN0:
759 1.1 mycroft drq = 0;
760 1.1 mycroft break;
761 1.1 mycroft case CHAN5:
762 1.1 mycroft drq = 5;
763 1.1 mycroft break;
764 1.1 mycroft case CHAN6:
765 1.1 mycroft drq = 6;
766 1.1 mycroft break;
767 1.1 mycroft case CHAN7:
768 1.1 mycroft drq = 7;
769 1.1 mycroft break;
770 1.1 mycroft default:
771 1.1 mycroft printf("aha_find: illegal drq setting %x\n", config.reply.chan);
772 1.1 mycroft return (0);
773 1.1 mycroft }
774 1.1 mycroft
775 1.1 mycroft switch (config.reply.intr) {
776 1.1 mycroft case INT9:
777 1.1 mycroft irq = 9;
778 1.1 mycroft break;
779 1.1 mycroft case INT10:
780 1.1 mycroft irq = 10;
781 1.1 mycroft break;
782 1.1 mycroft case INT11:
783 1.1 mycroft irq = 11;
784 1.1 mycroft break;
785 1.1 mycroft case INT12:
786 1.1 mycroft irq = 12;
787 1.1 mycroft break;
788 1.1 mycroft case INT14:
789 1.1 mycroft irq = 14;
790 1.1 mycroft break;
791 1.1 mycroft case INT15:
792 1.1 mycroft irq = 15;
793 1.1 mycroft break;
794 1.1 mycroft default:
795 1.1 mycroft printf("aha_find: illegal irq setting %x\n", config.reply.intr);
796 1.1 mycroft return (0);
797 1.1 mycroft }
798 1.1 mycroft
799 1.4 mycroft if (sc) {
800 1.1 mycroft sc->sc_irq = irq;
801 1.1 mycroft sc->sc_drq = drq;
802 1.1 mycroft sc->sc_scsi_dev = config.reply.scsi_dev;
803 1.1 mycroft }
804 1.1 mycroft
805 1.1 mycroft return (1);
806 1.1 mycroft }
807 1.1 mycroft
808 1.1 mycroft /*
809 1.1 mycroft * Start the board, ready for normal operation
810 1.1 mycroft */
811 1.1 mycroft void
812 1.1 mycroft aha_init(sc)
813 1.1 mycroft struct aha_softc *sc;
814 1.1 mycroft {
815 1.1 mycroft bus_space_tag_t iot = sc->sc_iot;
816 1.1 mycroft bus_space_handle_t ioh = sc->sc_ioh;
817 1.1 mycroft struct aha_devices devices;
818 1.1 mycroft struct aha_setup setup;
819 1.1 mycroft struct aha_mailbox mailbox;
820 1.1 mycroft int i;
821 1.1 mycroft
822 1.1 mycroft /*
823 1.1 mycroft * XXX
824 1.1 mycroft * If we are a 1542C or later, disable the extended BIOS so that the
825 1.1 mycroft * mailbox interface is unlocked.
826 1.1 mycroft * No need to check the extended BIOS flags as some of the
827 1.1 mycroft * extensions that cause us problems are not flagged in that byte.
828 1.1 mycroft */
829 1.1 mycroft if (!strncmp(sc->sc_model, "1542C", 5)) {
830 1.1 mycroft struct aha_extbios extbios;
831 1.1 mycroft struct aha_unlock unlock;
832 1.1 mycroft
833 1.1 mycroft printf("%s: unlocking mailbox interface\n", sc->sc_dev.dv_xname);
834 1.1 mycroft extbios.cmd.opcode = AHA_EXT_BIOS;
835 1.1 mycroft aha_cmd(iot, ioh, sc,
836 1.1 mycroft sizeof(extbios.cmd), (u_char *)&extbios.cmd,
837 1.1 mycroft sizeof(extbios.reply), (u_char *)&extbios.reply);
838 1.1 mycroft
839 1.1 mycroft #ifdef AHADEBUG
840 1.1 mycroft printf("%s: flags=%02x, mailboxlock=%02x\n",
841 1.1 mycroft sc->sc_dev.dv_xname,
842 1.1 mycroft extbios.reply.flags, extbios.reply.mailboxlock);
843 1.1 mycroft #endif /* AHADEBUG */
844 1.1 mycroft
845 1.1 mycroft unlock.cmd.opcode = AHA_MBX_ENABLE;
846 1.1 mycroft unlock.cmd.junk = 0;
847 1.1 mycroft unlock.cmd.magic = extbios.reply.mailboxlock;
848 1.1 mycroft aha_cmd(iot, ioh, sc,
849 1.1 mycroft sizeof(unlock.cmd), (u_char *)&unlock.cmd,
850 1.1 mycroft 0, (u_char *)0);
851 1.1 mycroft }
852 1.1 mycroft
853 1.1 mycroft #if 0
854 1.1 mycroft /*
855 1.1 mycroft * Change the bus on/off times to not clash with other dma users.
856 1.1 mycroft */
857 1.1 mycroft aha_cmd(iot, ioh, 1, 0, 0, 0, AHA_BUS_ON_TIME_SET, 7);
858 1.1 mycroft aha_cmd(iot, ioh, 1, 0, 0, 0, AHA_BUS_OFF_TIME_SET, 4);
859 1.1 mycroft #endif
860 1.1 mycroft
861 1.1 mycroft /* Inquire Installed Devices (to force synchronous negotiation). */
862 1.1 mycroft devices.cmd.opcode = AHA_INQUIRE_DEVICES;
863 1.1 mycroft aha_cmd(iot, ioh, sc,
864 1.1 mycroft sizeof(devices.cmd), (u_char *)&devices.cmd,
865 1.1 mycroft sizeof(devices.reply), (u_char *)&devices.reply);
866 1.1 mycroft
867 1.1 mycroft /* Obtain setup information from. */
868 1.1 mycroft setup.cmd.opcode = AHA_INQUIRE_SETUP;
869 1.1 mycroft setup.cmd.len = sizeof(setup.reply);
870 1.1 mycroft aha_cmd(iot, ioh, sc,
871 1.1 mycroft sizeof(setup.cmd), (u_char *)&setup.cmd,
872 1.1 mycroft sizeof(setup.reply), (u_char *)&setup.reply);
873 1.1 mycroft
874 1.1 mycroft printf("%s: %s, %s\n",
875 1.1 mycroft sc->sc_dev.dv_xname,
876 1.1 mycroft setup.reply.sync_neg ? "sync" : "async",
877 1.1 mycroft setup.reply.parity ? "parity" : "no parity");
878 1.1 mycroft
879 1.1 mycroft for (i = 0; i < 8; i++) {
880 1.1 mycroft if (!setup.reply.sync[i].valid ||
881 1.1 mycroft (!setup.reply.sync[i].offset && !setup.reply.sync[i].period))
882 1.1 mycroft continue;
883 1.1 mycroft printf("%s targ %d: sync, offset %d, period %dnsec\n",
884 1.1 mycroft sc->sc_dev.dv_xname, i,
885 1.1 mycroft setup.reply.sync[i].offset, setup.reply.sync[i].period * 50 + 200);
886 1.1 mycroft }
887 1.1 mycroft
888 1.1 mycroft /*
889 1.1 mycroft * Set up initial mail box for round-robin operation.
890 1.1 mycroft */
891 1.1 mycroft for (i = 0; i < AHA_MBX_SIZE; i++) {
892 1.1 mycroft wmbx->mbo[i].cmd = AHA_MBO_FREE;
893 1.1 mycroft wmbx->mbi[i].stat = AHA_MBI_FREE;
894 1.1 mycroft }
895 1.1 mycroft wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
896 1.1 mycroft wmbx->tmbi = &wmbx->mbi[0];
897 1.1 mycroft sc->sc_mbofull = 0;
898 1.1 mycroft
899 1.1 mycroft /* Initialize mail box. */
900 1.1 mycroft mailbox.cmd.opcode = AHA_MBX_INIT;
901 1.1 mycroft mailbox.cmd.nmbx = AHA_MBX_SIZE;
902 1.1 mycroft ltophys(KVTOPHYS(wmbx), mailbox.cmd.addr);
903 1.1 mycroft aha_cmd(iot, ioh, sc,
904 1.1 mycroft sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
905 1.1 mycroft 0, (u_char *)0);
906 1.1 mycroft }
907 1.1 mycroft
908 1.1 mycroft void
909 1.1 mycroft aha_inquire_setup_information(sc)
910 1.1 mycroft struct aha_softc *sc;
911 1.1 mycroft {
912 1.1 mycroft bus_space_tag_t iot = sc->sc_iot;
913 1.1 mycroft bus_space_handle_t ioh = sc->sc_ioh;
914 1.1 mycroft struct aha_revision revision;
915 1.1 mycroft u_char sts;
916 1.1 mycroft int i;
917 1.1 mycroft char *p;
918 1.1 mycroft
919 1.1 mycroft strcpy(sc->sc_model, "unknown");
920 1.1 mycroft
921 1.1 mycroft /*
922 1.1 mycroft * Assume we have a board at this stage, do an adapter inquire
923 1.1 mycroft * to find out what type of controller it is. If the command
924 1.1 mycroft * fails, we assume it's either a crusty board or an old 1542
925 1.1 mycroft * clone, and skip the board-specific stuff.
926 1.1 mycroft */
927 1.1 mycroft revision.cmd.opcode = AHA_INQUIRE_REVISION;
928 1.1 mycroft if (aha_cmd(iot, ioh, sc,
929 1.1 mycroft sizeof(revision.cmd), (u_char *)&revision.cmd,
930 1.1 mycroft sizeof(revision.reply), (u_char *)&revision.reply)) {
931 1.1 mycroft /*
932 1.1 mycroft * aha_cmd() already started the reset. It's not clear we
933 1.1 mycroft * even need to bother here.
934 1.1 mycroft */
935 1.1 mycroft for (i = AHA_RESET_TIMEOUT; i; i--) {
936 1.1 mycroft sts = bus_space_read_1(iot, ioh, AHA_STAT_PORT);
937 1.1 mycroft if (sts == (AHA_STAT_IDLE | AHA_STAT_INIT))
938 1.1 mycroft break;
939 1.1 mycroft delay(1000);
940 1.1 mycroft }
941 1.1 mycroft if (!i) {
942 1.1 mycroft #ifdef AHADEBUG
943 1.1 mycroft printf("aha_init: soft reset failed\n");
944 1.1 mycroft #endif /* AHADEBUG */
945 1.1 mycroft return;
946 1.1 mycroft }
947 1.1 mycroft #ifdef AHADEBUG
948 1.1 mycroft printf("aha_init: inquire command failed\n");
949 1.1 mycroft #endif /* AHADEBUG */
950 1.1 mycroft goto noinquire;
951 1.1 mycroft }
952 1.1 mycroft
953 1.1 mycroft #ifdef AHADEBUG
954 1.1 mycroft printf("%s: inquire %x, %x, %x, %x\n",
955 1.1 mycroft sc->sc_dev.dv_xname,
956 1.1 mycroft revision.reply.boardid, revision.reply.spec_opts,
957 1.1 mycroft revision.reply.revision_1, revision.reply.revision_2);
958 1.1 mycroft #endif /* AHADEBUG */
959 1.1 mycroft
960 1.1 mycroft switch (revision.reply.boardid) {
961 1.1 mycroft case 0x31:
962 1.1 mycroft strcpy(sc->sc_model, "1540");
963 1.1 mycroft break;
964 1.1 mycroft case 0x41:
965 1.1 mycroft strcpy(sc->sc_model, "1540A/1542A/1542B");
966 1.1 mycroft break;
967 1.1 mycroft case 0x42:
968 1.1 mycroft strcpy(sc->sc_model, "1640");
969 1.1 mycroft break;
970 1.1 mycroft case 0x43:
971 1.1 mycroft strcpy(sc->sc_model, "1542C");
972 1.1 mycroft break;
973 1.1 mycroft case 0x44:
974 1.1 mycroft case 0x45:
975 1.1 mycroft strcpy(sc->sc_model, "1542CF");
976 1.1 mycroft break;
977 1.1 mycroft case 0x46:
978 1.1 mycroft strcpy(sc->sc_model, "1542CP");
979 1.1 mycroft break;
980 1.1 mycroft }
981 1.1 mycroft
982 1.1 mycroft p = sc->sc_firmware;
983 1.1 mycroft *p++ = revision.reply.revision_1;
984 1.1 mycroft *p++ = '.';
985 1.1 mycroft *p++ = revision.reply.revision_2;
986 1.1 mycroft *p = '\0';
987 1.1 mycroft
988 1.1 mycroft noinquire:
989 1.2 sommerfe printf("%s: model AHA-%s, firmware %s\n",
990 1.2 sommerfe sc->sc_dev.dv_xname,
991 1.2 sommerfe sc->sc_model, sc->sc_firmware);
992 1.1 mycroft }
993 1.1 mycroft
994 1.1 mycroft void
995 1.1 mycroft ahaminphys(bp)
996 1.1 mycroft struct buf *bp;
997 1.1 mycroft {
998 1.1 mycroft
999 1.1 mycroft if (bp->b_bcount > ((AHA_NSEG - 1) << PGSHIFT))
1000 1.1 mycroft bp->b_bcount = ((AHA_NSEG - 1) << PGSHIFT);
1001 1.1 mycroft minphys(bp);
1002 1.1 mycroft }
1003 1.1 mycroft
1004 1.1 mycroft /*
1005 1.1 mycroft * start a scsi operation given the command and the data address. Also needs
1006 1.1 mycroft * the unit, target and lu.
1007 1.1 mycroft */
1008 1.1 mycroft int
1009 1.1 mycroft aha_scsi_cmd(xs)
1010 1.1 mycroft struct scsi_xfer *xs;
1011 1.1 mycroft {
1012 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
1013 1.1 mycroft struct aha_softc *sc = sc_link->adapter_softc;
1014 1.1 mycroft struct aha_ccb *ccb;
1015 1.1 mycroft struct aha_scat_gath *sg;
1016 1.1 mycroft int seg; /* scatter gather seg being worked on */
1017 1.1 mycroft u_long thiskv, thisphys, nextphys;
1018 1.1 mycroft int bytes_this_seg, bytes_this_page, datalen, flags;
1019 1.1 mycroft #ifdef TFS
1020 1.1 mycroft struct iovec *iovp;
1021 1.1 mycroft #endif
1022 1.1 mycroft int s;
1023 1.1 mycroft
1024 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("aha_scsi_cmd\n"));
1025 1.1 mycroft /*
1026 1.1 mycroft * get a ccb to use. If the transfer
1027 1.1 mycroft * is from a buf (possibly from interrupt time)
1028 1.1 mycroft * then we can't allow it to sleep
1029 1.1 mycroft */
1030 1.1 mycroft flags = xs->flags;
1031 1.1 mycroft if ((ccb = aha_get_ccb(sc, flags)) == NULL) {
1032 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
1033 1.1 mycroft return (TRY_AGAIN_LATER);
1034 1.1 mycroft }
1035 1.1 mycroft ccb->xs = xs;
1036 1.1 mycroft ccb->timeout = xs->timeout;
1037 1.1 mycroft
1038 1.1 mycroft /*
1039 1.1 mycroft * Put all the arguments for the xfer in the ccb
1040 1.1 mycroft */
1041 1.1 mycroft if (flags & SCSI_RESET) {
1042 1.1 mycroft ccb->opcode = AHA_RESET_CCB;
1043 1.1 mycroft ccb->scsi_cmd_length = 0;
1044 1.1 mycroft } else {
1045 1.1 mycroft /* can't use S/G if zero length */
1046 1.1 mycroft ccb->opcode = (xs->datalen ? AHA_INIT_SCAT_GATH_CCB
1047 1.1 mycroft : AHA_INITIATOR_CCB);
1048 1.1 mycroft bcopy(xs->cmd, &ccb->scsi_cmd,
1049 1.1 mycroft ccb->scsi_cmd_length = xs->cmdlen);
1050 1.1 mycroft }
1051 1.1 mycroft
1052 1.1 mycroft if (xs->datalen) {
1053 1.1 mycroft sg = ccb->scat_gath;
1054 1.1 mycroft seg = 0;
1055 1.1 mycroft #ifdef TFS
1056 1.1 mycroft if (flags & SCSI_DATA_UIO) {
1057 1.1 mycroft iovp = ((struct uio *)xs->data)->uio_iov;
1058 1.1 mycroft datalen = ((struct uio *)xs->data)->uio_iovcnt;
1059 1.1 mycroft xs->datalen = 0;
1060 1.1 mycroft while (datalen && seg < AHA_NSEG) {
1061 1.1 mycroft ltophys(iovp->iov_base, sg->seg_addr);
1062 1.1 mycroft ltophys(iovp->iov_len, sg->seg_len);
1063 1.1 mycroft xs->datalen += iovp->iov_len;
1064 1.1 mycroft SC_DEBUGN(sc_link, SDEV_DB4, ("UIO(0x%x@0x%x)",
1065 1.1 mycroft iovp->iov_len, iovp->iov_base));
1066 1.1 mycroft sg++;
1067 1.1 mycroft iovp++;
1068 1.1 mycroft seg++;
1069 1.1 mycroft datalen--;
1070 1.1 mycroft }
1071 1.1 mycroft } else
1072 1.1 mycroft #endif /* TFS */
1073 1.1 mycroft {
1074 1.1 mycroft /*
1075 1.1 mycroft * Set up the scatter-gather block.
1076 1.1 mycroft */
1077 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB4,
1078 1.1 mycroft ("%d @0x%x:- ", xs->datalen, xs->data));
1079 1.1 mycroft
1080 1.1 mycroft datalen = xs->datalen;
1081 1.1 mycroft thiskv = (int)xs->data;
1082 1.1 mycroft thisphys = KVTOPHYS(thiskv);
1083 1.1 mycroft
1084 1.1 mycroft while (datalen && seg < AHA_NSEG) {
1085 1.1 mycroft bytes_this_seg = 0;
1086 1.1 mycroft
1087 1.1 mycroft /* put in the base address */
1088 1.1 mycroft ltophys(thisphys, sg->seg_addr);
1089 1.1 mycroft
1090 1.1 mycroft SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
1091 1.1 mycroft
1092 1.1 mycroft /* do it at least once */
1093 1.1 mycroft nextphys = thisphys;
1094 1.1 mycroft while (datalen && thisphys == nextphys) {
1095 1.1 mycroft /*
1096 1.1 mycroft * This page is contiguous (physically)
1097 1.1 mycroft * with the the last, just extend the
1098 1.1 mycroft * length
1099 1.1 mycroft */
1100 1.1 mycroft /* check it fits on the ISA bus */
1101 1.1 mycroft if (thisphys > 0xFFFFFF) {
1102 1.1 mycroft printf("%s: DMA beyond"
1103 1.1 mycroft " end of ISA\n",
1104 1.1 mycroft sc->sc_dev.dv_xname);
1105 1.1 mycroft goto bad;
1106 1.1 mycroft }
1107 1.1 mycroft /* how far to the end of the page */
1108 1.1 mycroft nextphys = (thisphys & ~PGOFSET) + NBPG;
1109 1.1 mycroft bytes_this_page = nextphys - thisphys;
1110 1.1 mycroft /**** or the data ****/
1111 1.1 mycroft bytes_this_page = min(bytes_this_page,
1112 1.1 mycroft datalen);
1113 1.1 mycroft bytes_this_seg += bytes_this_page;
1114 1.1 mycroft datalen -= bytes_this_page;
1115 1.1 mycroft
1116 1.1 mycroft /* get more ready for the next page */
1117 1.1 mycroft thiskv = (thiskv & ~PGOFSET) + NBPG;
1118 1.1 mycroft if (datalen)
1119 1.1 mycroft thisphys = KVTOPHYS(thiskv);
1120 1.1 mycroft }
1121 1.1 mycroft /*
1122 1.1 mycroft * next page isn't contiguous, finish the seg
1123 1.1 mycroft */
1124 1.1 mycroft SC_DEBUGN(sc_link, SDEV_DB4,
1125 1.1 mycroft ("(0x%x)", bytes_this_seg));
1126 1.1 mycroft ltophys(bytes_this_seg, sg->seg_len);
1127 1.1 mycroft sg++;
1128 1.1 mycroft seg++;
1129 1.1 mycroft }
1130 1.1 mycroft }
1131 1.1 mycroft /* end of iov/kv decision */
1132 1.1 mycroft SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
1133 1.1 mycroft if (datalen) {
1134 1.1 mycroft /*
1135 1.1 mycroft * there's still data, must have run out of segs!
1136 1.1 mycroft */
1137 1.1 mycroft printf("%s: aha_scsi_cmd, more than %d dma segs\n",
1138 1.1 mycroft sc->sc_dev.dv_xname, AHA_NSEG);
1139 1.1 mycroft goto bad;
1140 1.1 mycroft }
1141 1.1 mycroft ltophys(KVTOPHYS(ccb->scat_gath), ccb->data_addr);
1142 1.1 mycroft ltophys(seg * sizeof(struct aha_scat_gath), ccb->data_length);
1143 1.1 mycroft } else { /* No data xfer, use non S/G values */
1144 1.1 mycroft ltophys(0, ccb->data_addr);
1145 1.1 mycroft ltophys(0, ccb->data_length);
1146 1.1 mycroft }
1147 1.1 mycroft
1148 1.1 mycroft ccb->data_out = 0;
1149 1.1 mycroft ccb->data_in = 0;
1150 1.1 mycroft ccb->target = sc_link->target;
1151 1.1 mycroft ccb->lun = sc_link->lun;
1152 1.1 mycroft ccb->req_sense_length = sizeof(ccb->scsi_sense);
1153 1.1 mycroft ccb->host_stat = 0x00;
1154 1.1 mycroft ccb->target_stat = 0x00;
1155 1.1 mycroft ccb->link_id = 0;
1156 1.1 mycroft ltophys(0, ccb->link_addr);
1157 1.1 mycroft
1158 1.1 mycroft s = splbio();
1159 1.1 mycroft aha_queue_ccb(sc, ccb);
1160 1.1 mycroft splx(s);
1161 1.1 mycroft
1162 1.1 mycroft /*
1163 1.1 mycroft * Usually return SUCCESSFULLY QUEUED
1164 1.1 mycroft */
1165 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1166 1.1 mycroft if ((flags & SCSI_POLL) == 0)
1167 1.1 mycroft return (SUCCESSFULLY_QUEUED);
1168 1.1 mycroft
1169 1.1 mycroft /*
1170 1.1 mycroft * If we can't use interrupts, poll on completion
1171 1.1 mycroft */
1172 1.1 mycroft if (aha_poll(sc, xs, ccb->timeout)) {
1173 1.1 mycroft aha_timeout(ccb);
1174 1.1 mycroft if (aha_poll(sc, xs, ccb->timeout))
1175 1.1 mycroft aha_timeout(ccb);
1176 1.1 mycroft }
1177 1.1 mycroft return (COMPLETE);
1178 1.1 mycroft
1179 1.1 mycroft bad:
1180 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
1181 1.1 mycroft aha_free_ccb(sc, ccb);
1182 1.1 mycroft return (COMPLETE);
1183 1.1 mycroft }
1184 1.1 mycroft
1185 1.1 mycroft /*
1186 1.1 mycroft * Poll a particular unit, looking for a particular xs
1187 1.1 mycroft */
1188 1.1 mycroft int
1189 1.1 mycroft aha_poll(sc, xs, count)
1190 1.1 mycroft struct aha_softc *sc;
1191 1.1 mycroft struct scsi_xfer *xs;
1192 1.1 mycroft int count;
1193 1.1 mycroft {
1194 1.1 mycroft bus_space_tag_t iot = sc->sc_iot;
1195 1.1 mycroft bus_space_handle_t ioh = sc->sc_ioh;
1196 1.1 mycroft
1197 1.1 mycroft /* timeouts are in msec, so we loop in 1000 usec cycles */
1198 1.1 mycroft while (count) {
1199 1.1 mycroft /*
1200 1.1 mycroft * If we had interrupts enabled, would we
1201 1.1 mycroft * have got an interrupt?
1202 1.1 mycroft */
1203 1.1 mycroft if (bus_space_read_1(iot, ioh, AHA_INTR_PORT) & AHA_INTR_ANYINTR)
1204 1.1 mycroft aha_intr(sc);
1205 1.1 mycroft if (xs->flags & ITSDONE)
1206 1.1 mycroft return (0);
1207 1.1 mycroft delay(1000); /* only happens in boot so ok */
1208 1.1 mycroft count--;
1209 1.1 mycroft }
1210 1.1 mycroft return (1);
1211 1.1 mycroft }
1212 1.1 mycroft
1213 1.1 mycroft void
1214 1.1 mycroft aha_timeout(arg)
1215 1.1 mycroft void *arg;
1216 1.1 mycroft {
1217 1.1 mycroft struct aha_ccb *ccb = arg;
1218 1.1 mycroft struct scsi_xfer *xs = ccb->xs;
1219 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
1220 1.1 mycroft struct aha_softc *sc = sc_link->adapter_softc;
1221 1.1 mycroft int s;
1222 1.1 mycroft
1223 1.1 mycroft sc_print_addr(sc_link);
1224 1.1 mycroft printf("timed out");
1225 1.1 mycroft
1226 1.1 mycroft s = splbio();
1227 1.1 mycroft
1228 1.1 mycroft #ifdef AHADIAG
1229 1.1 mycroft /*
1230 1.1 mycroft * If The ccb's mbx is not free, then the board has gone south?
1231 1.1 mycroft */
1232 1.1 mycroft aha_collect_mbo(sc);
1233 1.1 mycroft if (ccb->flags & CCB_SENDING) {
1234 1.1 mycroft printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1235 1.1 mycroft Debugger();
1236 1.1 mycroft }
1237 1.1 mycroft #endif
1238 1.1 mycroft
1239 1.1 mycroft /*
1240 1.1 mycroft * If it has been through before, then
1241 1.1 mycroft * a previous abort has failed, don't
1242 1.1 mycroft * try abort again
1243 1.1 mycroft */
1244 1.1 mycroft if (ccb->flags & CCB_ABORT) {
1245 1.1 mycroft /* abort timed out */
1246 1.1 mycroft printf(" AGAIN\n");
1247 1.1 mycroft /* XXX Must reset! */
1248 1.1 mycroft } else {
1249 1.1 mycroft /* abort the operation that has timed out */
1250 1.1 mycroft printf("\n");
1251 1.1 mycroft ccb->xs->error = XS_TIMEOUT;
1252 1.1 mycroft ccb->timeout = AHA_ABORT_TIMEOUT;
1253 1.1 mycroft ccb->flags |= CCB_ABORT;
1254 1.1 mycroft aha_queue_ccb(sc, ccb);
1255 1.1 mycroft }
1256 1.1 mycroft
1257 1.1 mycroft splx(s);
1258 1.1 mycroft }
1259