bha.c revision 1.29 1 1.29 thorpej /* $NetBSD: bha.c,v 1.29 1998/12/09 08:47:19 thorpej Exp $ */
2 1.1 mycroft
3 1.24 jonathan #include "opt_ddb.h"
4 1.1 mycroft #undef BHADIAG
5 1.1 mycroft #ifdef DDB
6 1.1 mycroft #define integrate
7 1.1 mycroft #else
8 1.1 mycroft #define integrate static inline
9 1.1 mycroft #endif
10 1.1 mycroft
11 1.13 thorpej /*-
12 1.21 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
13 1.13 thorpej * All rights reserved.
14 1.13 thorpej *
15 1.13 thorpej * This code is derived from software contributed to The NetBSD Foundation
16 1.25 mycroft * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
17 1.25 mycroft * Simulation Facility, NASA Ames Research Center.
18 1.13 thorpej *
19 1.13 thorpej * Redistribution and use in source and binary forms, with or without
20 1.13 thorpej * modification, are permitted provided that the following conditions
21 1.13 thorpej * are met:
22 1.13 thorpej * 1. Redistributions of source code must retain the above copyright
23 1.13 thorpej * notice, this list of conditions and the following disclaimer.
24 1.13 thorpej * 2. Redistributions in binary form must reproduce the above copyright
25 1.13 thorpej * notice, this list of conditions and the following disclaimer in the
26 1.13 thorpej * documentation and/or other materials provided with the distribution.
27 1.13 thorpej * 3. All advertising materials mentioning features or use of this software
28 1.13 thorpej * must display the following acknowledgement:
29 1.13 thorpej * This product includes software developed by the NetBSD
30 1.13 thorpej * Foundation, Inc. and its contributors.
31 1.13 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
32 1.13 thorpej * contributors may be used to endorse or promote products derived
33 1.13 thorpej * from this software without specific prior written permission.
34 1.13 thorpej *
35 1.13 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36 1.13 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37 1.13 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 1.13 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39 1.13 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 1.13 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 1.13 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 1.13 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 1.13 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 1.13 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 1.13 thorpej * POSSIBILITY OF SUCH DAMAGE.
46 1.1 mycroft */
47 1.1 mycroft
48 1.1 mycroft /*
49 1.1 mycroft * Originally written by Julian Elischer (julian (at) tfs.com)
50 1.1 mycroft * for TRW Financial Systems for use under the MACH(2.5) operating system.
51 1.1 mycroft *
52 1.1 mycroft * TRW Financial Systems, in accordance with their agreement with Carnegie
53 1.1 mycroft * Mellon University, makes this software available to CMU to distribute
54 1.1 mycroft * or use in any manner that they see fit as long as this message is kept with
55 1.1 mycroft * the software. For this reason TFS also grants any other persons or
56 1.1 mycroft * organisations permission to use or modify this software.
57 1.1 mycroft *
58 1.1 mycroft * TFS supplies this software to be publicly redistributed
59 1.1 mycroft * on the understanding that TFS is not responsible for the correct
60 1.1 mycroft * functioning of this software in any circumstances.
61 1.1 mycroft */
62 1.1 mycroft
63 1.1 mycroft #include <sys/types.h>
64 1.1 mycroft #include <sys/param.h>
65 1.1 mycroft #include <sys/systm.h>
66 1.1 mycroft #include <sys/kernel.h>
67 1.1 mycroft #include <sys/errno.h>
68 1.1 mycroft #include <sys/ioctl.h>
69 1.1 mycroft #include <sys/device.h>
70 1.1 mycroft #include <sys/malloc.h>
71 1.1 mycroft #include <sys/buf.h>
72 1.1 mycroft #include <sys/proc.h>
73 1.1 mycroft #include <sys/user.h>
74 1.1 mycroft
75 1.1 mycroft #include <machine/bus.h>
76 1.1 mycroft #include <machine/intr.h>
77 1.1 mycroft
78 1.14 bouyer #include <dev/scsipi/scsi_all.h>
79 1.14 bouyer #include <dev/scsipi/scsipi_all.h>
80 1.14 bouyer #include <dev/scsipi/scsiconf.h>
81 1.1 mycroft
82 1.1 mycroft #include <dev/ic/bhareg.h>
83 1.1 mycroft #include <dev/ic/bhavar.h>
84 1.1 mycroft
85 1.1 mycroft #ifndef DDB
86 1.1 mycroft #define Debugger() panic("should call debugger here (bha.c)")
87 1.1 mycroft #endif /* ! DDB */
88 1.1 mycroft
89 1.13 thorpej #define BHA_MAXXFER ((BHA_NSEG - 1) << PGSHIFT)
90 1.1 mycroft
91 1.1 mycroft #ifdef BHADEBUG
92 1.1 mycroft int bha_debug = 0;
93 1.1 mycroft #endif /* BHADEBUG */
94 1.1 mycroft
95 1.9 mycroft int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
96 1.9 mycroft int, u_char *, int, u_char *));
97 1.1 mycroft integrate void bha_finish_ccbs __P((struct bha_softc *));
98 1.1 mycroft integrate void bha_reset_ccb __P((struct bha_softc *, struct bha_ccb *));
99 1.1 mycroft void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
100 1.16 thorpej integrate int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
101 1.1 mycroft struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
102 1.1 mycroft struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, u_long));
103 1.1 mycroft void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
104 1.1 mycroft void bha_collect_mbo __P((struct bha_softc *));
105 1.1 mycroft void bha_start_ccbs __P((struct bha_softc *));
106 1.1 mycroft void bha_done __P((struct bha_softc *, struct bha_ccb *));
107 1.22 thorpej int bha_init __P((struct bha_softc *));
108 1.1 mycroft void bhaminphys __P((struct buf *));
109 1.14 bouyer int bha_scsi_cmd __P((struct scsipi_xfer *));
110 1.14 bouyer int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
111 1.1 mycroft void bha_timeout __P((void *arg));
112 1.22 thorpej int bha_create_ccbs __P((struct bha_softc *, struct bha_ccb *, int));
113 1.1 mycroft
114 1.1 mycroft /* the below structure is so we have a default dev struct for out link struct */
115 1.14 bouyer struct scsipi_device bha_dev = {
116 1.1 mycroft NULL, /* Use default error handler */
117 1.1 mycroft NULL, /* have a queue, served by this */
118 1.1 mycroft NULL, /* have no async handler */
119 1.1 mycroft NULL, /* Use default 'done' routine */
120 1.1 mycroft };
121 1.1 mycroft
122 1.1 mycroft #define BHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
123 1.1 mycroft #define BHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
124 1.13 thorpej
125 1.1 mycroft /*
126 1.4 thorpej * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
127 1.1 mycroft *
128 1.1 mycroft * Activate Adapter command
129 1.1 mycroft * icnt: number of args (outbound bytes including opcode)
130 1.1 mycroft * ibuf: argument buffer
131 1.1 mycroft * ocnt: number of expected returned bytes
132 1.1 mycroft * obuf: result buffer
133 1.1 mycroft * wait: number of seconds to wait for response
134 1.1 mycroft *
135 1.1 mycroft * Performs an adapter command through the ports. Not to be confused with a
136 1.1 mycroft * scsi command, which is read in via the dma; one of the adapter commands
137 1.1 mycroft * tells it to read in a scsi command.
138 1.1 mycroft */
139 1.1 mycroft int
140 1.4 thorpej bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
141 1.4 thorpej bus_space_tag_t iot;
142 1.4 thorpej bus_space_handle_t ioh;
143 1.1 mycroft struct bha_softc *sc;
144 1.1 mycroft int icnt, ocnt;
145 1.1 mycroft u_char *ibuf, *obuf;
146 1.1 mycroft {
147 1.1 mycroft const char *name;
148 1.1 mycroft register int i;
149 1.1 mycroft int wait;
150 1.1 mycroft u_char sts;
151 1.1 mycroft u_char opcode = ibuf[0];
152 1.1 mycroft
153 1.1 mycroft if (sc != NULL)
154 1.1 mycroft name = sc->sc_dev.dv_xname;
155 1.1 mycroft else
156 1.1 mycroft name = "(bha probe)";
157 1.1 mycroft
158 1.1 mycroft /*
159 1.1 mycroft * Calculate a reasonable timeout for the command.
160 1.1 mycroft */
161 1.1 mycroft switch (opcode) {
162 1.1 mycroft case BHA_INQUIRE_DEVICES:
163 1.8 thorpej case BHA_INQUIRE_DEVICES_2:
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 != BHA_MBO_INTR_EN) {
176 1.1 mycroft for (i = 20000; i; i--) { /* 1 sec? */
177 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
178 1.1 mycroft if (sts & BHA_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.3 christos printf("%s: bha_cmd, host not idle(0x%x)\n",
184 1.1 mycroft name, sts);
185 1.9 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.4 thorpej while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
194 1.4 thorpej BHA_STAT_DF)
195 1.4 thorpej bus_space_read_1(iot, ioh, BHA_DATA_PORT);
196 1.1 mycroft }
197 1.1 mycroft /*
198 1.1 mycroft * Output the command and the number of arguments given
199 1.1 mycroft * for each byte, first check the port is empty.
200 1.1 mycroft */
201 1.1 mycroft while (icnt--) {
202 1.1 mycroft for (i = wait; i; i--) {
203 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
204 1.1 mycroft if (!(sts & BHA_STAT_CDF))
205 1.1 mycroft break;
206 1.1 mycroft delay(50);
207 1.1 mycroft }
208 1.1 mycroft if (!i) {
209 1.1 mycroft if (opcode != BHA_INQUIRE_REVISION)
210 1.4 thorpej printf("%s: bha_cmd, cmd/data port full\n",
211 1.4 thorpej name);
212 1.9 mycroft goto bad;
213 1.1 mycroft }
214 1.4 thorpej bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
215 1.1 mycroft }
216 1.1 mycroft /*
217 1.1 mycroft * If we expect input, loop that many times, each time,
218 1.1 mycroft * looking for the data register to have valid data
219 1.1 mycroft */
220 1.9 mycroft while (ocnt--) {
221 1.1 mycroft for (i = wait; i; i--) {
222 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
223 1.1 mycroft if (sts & BHA_STAT_DF)
224 1.1 mycroft break;
225 1.1 mycroft delay(50);
226 1.1 mycroft }
227 1.1 mycroft if (!i) {
228 1.1 mycroft if (opcode != BHA_INQUIRE_REVISION)
229 1.3 christos printf("%s: bha_cmd, cmd/data port empty %d\n",
230 1.1 mycroft name, ocnt);
231 1.9 mycroft goto bad;
232 1.1 mycroft }
233 1.4 thorpej *obuf++ = bus_space_read_1(iot, ioh, BHA_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.6 thorpej if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
241 1.1 mycroft for (i = 20000; i; i--) { /* 1 sec? */
242 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
243 1.1 mycroft /* XXX Need to save this in the interrupt handler? */
244 1.1 mycroft if (sts & BHA_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.3 christos printf("%s: bha_cmd, host not finished(0x%x)\n",
250 1.1 mycroft name, sts);
251 1.9 mycroft return (1);
252 1.1 mycroft }
253 1.1 mycroft }
254 1.4 thorpej bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
255 1.9 mycroft return (0);
256 1.9 mycroft
257 1.9 mycroft bad:
258 1.9 mycroft bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
259 1.9 mycroft return (1);
260 1.1 mycroft }
261 1.1 mycroft
262 1.1 mycroft /*
263 1.1 mycroft * Attach all the sub-devices we can find
264 1.1 mycroft */
265 1.1 mycroft void
266 1.11 mycroft bha_attach(sc, bpd)
267 1.1 mycroft struct bha_softc *sc;
268 1.11 mycroft struct bha_probe_data *bpd;
269 1.1 mycroft {
270 1.1 mycroft
271 1.1 mycroft /*
272 1.27 thorpej * Fill in the adapter.
273 1.27 thorpej */
274 1.27 thorpej sc->sc_adapter.scsipi_cmd = bha_scsi_cmd;
275 1.27 thorpej sc->sc_adapter.scsipi_minphys = bhaminphys;
276 1.27 thorpej
277 1.27 thorpej /*
278 1.14 bouyer * fill in the prototype scsipi_link.
279 1.1 mycroft */
280 1.14 bouyer sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
281 1.1 mycroft sc->sc_link.adapter_softc = sc;
282 1.14 bouyer sc->sc_link.scsipi_scsi.adapter_target = bpd->sc_scsi_dev;
283 1.27 thorpej sc->sc_link.adapter = &sc->sc_adapter;
284 1.1 mycroft sc->sc_link.device = &bha_dev;
285 1.1 mycroft sc->sc_link.openings = 4;
286 1.14 bouyer sc->sc_link.scsipi_scsi.max_target = bpd->sc_iswide ? 15 : 7;
287 1.28 mjacob sc->sc_link.scsipi_scsi.max_lun = 7;
288 1.14 bouyer sc->sc_link.type = BUS_SCSI;
289 1.12 thorpej
290 1.13 thorpej TAILQ_INIT(&sc->sc_free_ccb);
291 1.13 thorpej TAILQ_INIT(&sc->sc_waiting_ccb);
292 1.29 thorpej TAILQ_INIT(&sc->sc_queue);
293 1.13 thorpej
294 1.12 thorpej bha_inquire_setup_information(sc);
295 1.13 thorpej
296 1.13 thorpej printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
297 1.13 thorpej sc->sc_model, sc->sc_firmware);
298 1.13 thorpej
299 1.22 thorpej if (bha_init(sc) != 0) {
300 1.22 thorpej /* Error during initialization! */
301 1.22 thorpej return;
302 1.22 thorpej }
303 1.1 mycroft
304 1.1 mycroft /*
305 1.1 mycroft * ask the adapter what subunits are present
306 1.1 mycroft */
307 1.1 mycroft config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
308 1.1 mycroft }
309 1.1 mycroft
310 1.1 mycroft integrate void
311 1.1 mycroft bha_finish_ccbs(sc)
312 1.1 mycroft struct bha_softc *sc;
313 1.1 mycroft {
314 1.1 mycroft struct bha_mbx_in *wmbi;
315 1.1 mycroft struct bha_ccb *ccb;
316 1.1 mycroft int i;
317 1.1 mycroft
318 1.1 mycroft wmbi = wmbx->tmbi;
319 1.1 mycroft
320 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
321 1.22 thorpej BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
322 1.22 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
323 1.22 thorpej
324 1.1 mycroft if (wmbi->stat == BHA_MBI_FREE) {
325 1.1 mycroft for (i = 0; i < BHA_MBX_SIZE; i++) {
326 1.1 mycroft if (wmbi->stat != BHA_MBI_FREE) {
327 1.3 christos printf("%s: mbi not in round-robin order\n",
328 1.1 mycroft sc->sc_dev.dv_xname);
329 1.1 mycroft goto AGAIN;
330 1.1 mycroft }
331 1.1 mycroft bha_nextmbx(wmbi, wmbx, mbi);
332 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
333 1.22 thorpej BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
334 1.22 thorpej BUS_DMASYNC_POSTREAD);
335 1.1 mycroft }
336 1.1 mycroft #ifdef BHADIAGnot
337 1.3 christos printf("%s: mbi interrupt with no full mailboxes\n",
338 1.1 mycroft sc->sc_dev.dv_xname);
339 1.1 mycroft #endif
340 1.1 mycroft return;
341 1.1 mycroft }
342 1.1 mycroft
343 1.1 mycroft AGAIN:
344 1.1 mycroft do {
345 1.1 mycroft ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
346 1.1 mycroft if (!ccb) {
347 1.3 christos printf("%s: bad mbi ccb pointer; skipping\n",
348 1.1 mycroft sc->sc_dev.dv_xname);
349 1.1 mycroft goto next;
350 1.1 mycroft }
351 1.1 mycroft
352 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
353 1.22 thorpej BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
354 1.22 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
355 1.22 thorpej
356 1.1 mycroft #ifdef BHADEBUG
357 1.1 mycroft if (bha_debug) {
358 1.1 mycroft u_char *cp = &ccb->scsi_cmd;
359 1.3 christos printf("op=%x %x %x %x %x %x\n",
360 1.1 mycroft cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
361 1.3 christos printf("stat %x for mbi addr = 0x%08x, ",
362 1.1 mycroft wmbi->stat, wmbi);
363 1.3 christos printf("ccb addr = 0x%x\n", ccb);
364 1.1 mycroft }
365 1.1 mycroft #endif /* BHADEBUG */
366 1.1 mycroft
367 1.1 mycroft switch (wmbi->stat) {
368 1.1 mycroft case BHA_MBI_OK:
369 1.1 mycroft case BHA_MBI_ERROR:
370 1.1 mycroft if ((ccb->flags & CCB_ABORT) != 0) {
371 1.1 mycroft /*
372 1.1 mycroft * If we already started an abort, wait for it
373 1.1 mycroft * to complete before clearing the CCB. We
374 1.1 mycroft * could instead just clear CCB_SENDING, but
375 1.1 mycroft * what if the mailbox was already received?
376 1.1 mycroft * The worst that happens here is that we clear
377 1.1 mycroft * the CCB a bit later than we need to. BFD.
378 1.1 mycroft */
379 1.1 mycroft goto next;
380 1.1 mycroft }
381 1.1 mycroft break;
382 1.1 mycroft
383 1.1 mycroft case BHA_MBI_ABORT:
384 1.1 mycroft case BHA_MBI_UNKNOWN:
385 1.1 mycroft /*
386 1.1 mycroft * Even if the CCB wasn't found, we clear it anyway.
387 1.1 mycroft * See preceeding comment.
388 1.1 mycroft */
389 1.1 mycroft break;
390 1.1 mycroft
391 1.1 mycroft default:
392 1.3 christos printf("%s: bad mbi status %02x; skipping\n",
393 1.1 mycroft sc->sc_dev.dv_xname, wmbi->stat);
394 1.1 mycroft goto next;
395 1.1 mycroft }
396 1.1 mycroft
397 1.1 mycroft untimeout(bha_timeout, ccb);
398 1.1 mycroft bha_done(sc, ccb);
399 1.1 mycroft
400 1.1 mycroft next:
401 1.1 mycroft wmbi->stat = BHA_MBI_FREE;
402 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
403 1.22 thorpej BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
404 1.22 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
405 1.1 mycroft bha_nextmbx(wmbi, wmbx, mbi);
406 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
407 1.22 thorpej BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
408 1.22 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
409 1.1 mycroft } while (wmbi->stat != BHA_MBI_FREE);
410 1.1 mycroft
411 1.1 mycroft wmbx->tmbi = wmbi;
412 1.1 mycroft }
413 1.1 mycroft
414 1.1 mycroft /*
415 1.1 mycroft * Catch an interrupt from the adaptor
416 1.1 mycroft */
417 1.1 mycroft int
418 1.1 mycroft bha_intr(arg)
419 1.1 mycroft void *arg;
420 1.1 mycroft {
421 1.1 mycroft struct bha_softc *sc = arg;
422 1.4 thorpej bus_space_tag_t iot = sc->sc_iot;
423 1.4 thorpej bus_space_handle_t ioh = sc->sc_ioh;
424 1.1 mycroft u_char sts;
425 1.1 mycroft
426 1.1 mycroft #ifdef BHADEBUG
427 1.3 christos printf("%s: bha_intr ", sc->sc_dev.dv_xname);
428 1.1 mycroft #endif /* BHADEBUG */
429 1.1 mycroft
430 1.1 mycroft /*
431 1.1 mycroft * First acknowlege the interrupt, Then if it's not telling about
432 1.1 mycroft * a completed operation just return.
433 1.1 mycroft */
434 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
435 1.1 mycroft if ((sts & BHA_INTR_ANYINTR) == 0)
436 1.1 mycroft return (0);
437 1.4 thorpej bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
438 1.1 mycroft
439 1.1 mycroft #ifdef BHADIAG
440 1.1 mycroft /* Make sure we clear CCB_SENDING before finishing a CCB. */
441 1.1 mycroft bha_collect_mbo(sc);
442 1.1 mycroft #endif
443 1.1 mycroft
444 1.1 mycroft /* Mail box out empty? */
445 1.1 mycroft if (sts & BHA_INTR_MBOA) {
446 1.1 mycroft struct bha_toggle toggle;
447 1.1 mycroft
448 1.1 mycroft toggle.cmd.opcode = BHA_MBO_INTR_EN;
449 1.1 mycroft toggle.cmd.enable = 0;
450 1.4 thorpej bha_cmd(iot, ioh, sc,
451 1.1 mycroft sizeof(toggle.cmd), (u_char *)&toggle.cmd,
452 1.1 mycroft 0, (u_char *)0);
453 1.1 mycroft bha_start_ccbs(sc);
454 1.1 mycroft }
455 1.1 mycroft
456 1.1 mycroft /* Mail box in full? */
457 1.1 mycroft if (sts & BHA_INTR_MBIF)
458 1.1 mycroft bha_finish_ccbs(sc);
459 1.1 mycroft
460 1.1 mycroft return (1);
461 1.1 mycroft }
462 1.1 mycroft
463 1.1 mycroft integrate void
464 1.1 mycroft bha_reset_ccb(sc, ccb)
465 1.1 mycroft struct bha_softc *sc;
466 1.1 mycroft struct bha_ccb *ccb;
467 1.1 mycroft {
468 1.1 mycroft
469 1.1 mycroft ccb->flags = 0;
470 1.1 mycroft }
471 1.1 mycroft
472 1.1 mycroft /*
473 1.1 mycroft * A ccb is put onto the free list.
474 1.1 mycroft */
475 1.1 mycroft void
476 1.1 mycroft bha_free_ccb(sc, ccb)
477 1.1 mycroft struct bha_softc *sc;
478 1.1 mycroft struct bha_ccb *ccb;
479 1.1 mycroft {
480 1.1 mycroft int s;
481 1.1 mycroft
482 1.1 mycroft s = splbio();
483 1.1 mycroft
484 1.1 mycroft bha_reset_ccb(sc, ccb);
485 1.1 mycroft TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
486 1.1 mycroft
487 1.1 mycroft /*
488 1.1 mycroft * If there were none, wake anybody waiting for one to come free,
489 1.1 mycroft * starting with queued entries.
490 1.1 mycroft */
491 1.1 mycroft if (ccb->chain.tqe_next == 0)
492 1.1 mycroft wakeup(&sc->sc_free_ccb);
493 1.1 mycroft
494 1.1 mycroft splx(s);
495 1.1 mycroft }
496 1.1 mycroft
497 1.16 thorpej integrate int
498 1.1 mycroft bha_init_ccb(sc, ccb)
499 1.1 mycroft struct bha_softc *sc;
500 1.1 mycroft struct bha_ccb *ccb;
501 1.1 mycroft {
502 1.13 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
503 1.16 thorpej int hashnum, error;
504 1.1 mycroft
505 1.13 thorpej /*
506 1.22 thorpej * Create the DMA map for this CCB.
507 1.13 thorpej */
508 1.16 thorpej error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
509 1.13 thorpej 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
510 1.16 thorpej &ccb->dmamap_xfer);
511 1.16 thorpej if (error) {
512 1.22 thorpej printf("%s: unable to create ccb DMA map, error = %d\n",
513 1.22 thorpej sc->sc_dev.dv_xname, error);
514 1.16 thorpej return (error);
515 1.16 thorpej }
516 1.13 thorpej
517 1.1 mycroft /*
518 1.1 mycroft * put in the phystokv hash table
519 1.1 mycroft * Never gets taken out.
520 1.1 mycroft */
521 1.22 thorpej ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
522 1.22 thorpej BHA_CCB_OFF(ccb);
523 1.1 mycroft hashnum = CCB_HASH(ccb->hashkey);
524 1.1 mycroft ccb->nexthash = sc->sc_ccbhash[hashnum];
525 1.1 mycroft sc->sc_ccbhash[hashnum] = ccb;
526 1.1 mycroft bha_reset_ccb(sc, ccb);
527 1.16 thorpej return (0);
528 1.1 mycroft }
529 1.1 mycroft
530 1.1 mycroft /*
531 1.22 thorpej * Create a set of ccbs and add them to the free list. Called once
532 1.22 thorpej * by bha_init(). We return the number of CCBs successfully created.
533 1.13 thorpej */
534 1.13 thorpej int
535 1.22 thorpej bha_create_ccbs(sc, ccbstore, count)
536 1.13 thorpej struct bha_softc *sc;
537 1.22 thorpej struct bha_ccb *ccbstore;
538 1.22 thorpej int count;
539 1.13 thorpej {
540 1.13 thorpej struct bha_ccb *ccb;
541 1.22 thorpej int i, error;
542 1.13 thorpej
543 1.22 thorpej bzero(ccbstore, sizeof(struct bha_ccb) * count);
544 1.22 thorpej for (i = 0; i < count; i++) {
545 1.22 thorpej ccb = &ccbstore[i];
546 1.22 thorpej if ((error = bha_init_ccb(sc, ccb)) != 0) {
547 1.22 thorpej printf("%s: unable to initialize ccb, error = %d\n",
548 1.22 thorpej sc->sc_dev.dv_xname, error);
549 1.22 thorpej goto out;
550 1.16 thorpej }
551 1.13 thorpej TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
552 1.13 thorpej }
553 1.22 thorpej out:
554 1.22 thorpej return (i);
555 1.13 thorpej }
556 1.13 thorpej
557 1.13 thorpej /*
558 1.1 mycroft * Get a free ccb
559 1.1 mycroft *
560 1.1 mycroft * If there are none, see if we can allocate a new one. If so, put it in
561 1.1 mycroft * the hash table too otherwise either return an error or sleep.
562 1.1 mycroft */
563 1.1 mycroft struct bha_ccb *
564 1.1 mycroft bha_get_ccb(sc, flags)
565 1.1 mycroft struct bha_softc *sc;
566 1.1 mycroft int flags;
567 1.1 mycroft {
568 1.1 mycroft struct bha_ccb *ccb;
569 1.1 mycroft int s;
570 1.1 mycroft
571 1.1 mycroft s = splbio();
572 1.1 mycroft
573 1.1 mycroft /*
574 1.1 mycroft * If we can and have to, sleep waiting for one to come free
575 1.1 mycroft * but only if we can't allocate a new one.
576 1.1 mycroft */
577 1.1 mycroft for (;;) {
578 1.1 mycroft ccb = sc->sc_free_ccb.tqh_first;
579 1.1 mycroft if (ccb) {
580 1.1 mycroft TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
581 1.1 mycroft break;
582 1.1 mycroft }
583 1.1 mycroft if ((flags & SCSI_NOSLEEP) != 0)
584 1.1 mycroft goto out;
585 1.1 mycroft tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
586 1.1 mycroft }
587 1.1 mycroft
588 1.1 mycroft ccb->flags |= CCB_ALLOC;
589 1.1 mycroft
590 1.1 mycroft out:
591 1.1 mycroft splx(s);
592 1.1 mycroft return (ccb);
593 1.1 mycroft }
594 1.1 mycroft
595 1.1 mycroft /*
596 1.1 mycroft * Given a physical address, find the ccb that it corresponds to.
597 1.1 mycroft */
598 1.1 mycroft struct bha_ccb *
599 1.1 mycroft bha_ccb_phys_kv(sc, ccb_phys)
600 1.1 mycroft struct bha_softc *sc;
601 1.1 mycroft u_long ccb_phys;
602 1.1 mycroft {
603 1.1 mycroft int hashnum = CCB_HASH(ccb_phys);
604 1.1 mycroft struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
605 1.1 mycroft
606 1.1 mycroft while (ccb) {
607 1.1 mycroft if (ccb->hashkey == ccb_phys)
608 1.1 mycroft break;
609 1.1 mycroft ccb = ccb->nexthash;
610 1.1 mycroft }
611 1.1 mycroft return (ccb);
612 1.1 mycroft }
613 1.1 mycroft
614 1.1 mycroft /*
615 1.1 mycroft * Queue a CCB to be sent to the controller, and send it if possible.
616 1.1 mycroft */
617 1.1 mycroft void
618 1.1 mycroft bha_queue_ccb(sc, ccb)
619 1.1 mycroft struct bha_softc *sc;
620 1.1 mycroft struct bha_ccb *ccb;
621 1.1 mycroft {
622 1.1 mycroft
623 1.1 mycroft TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
624 1.1 mycroft bha_start_ccbs(sc);
625 1.1 mycroft }
626 1.1 mycroft
627 1.1 mycroft /*
628 1.1 mycroft * Garbage collect mailboxes that are no longer in use.
629 1.1 mycroft */
630 1.1 mycroft void
631 1.1 mycroft bha_collect_mbo(sc)
632 1.1 mycroft struct bha_softc *sc;
633 1.1 mycroft {
634 1.1 mycroft struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
635 1.2 christos #ifdef BHADIAG
636 1.1 mycroft struct bha_ccb *ccb;
637 1.2 christos #endif
638 1.1 mycroft
639 1.1 mycroft wmbo = wmbx->cmbo;
640 1.1 mycroft
641 1.1 mycroft while (sc->sc_mbofull > 0) {
642 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
643 1.22 thorpej BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
644 1.22 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
645 1.1 mycroft if (wmbo->cmd != BHA_MBO_FREE)
646 1.1 mycroft break;
647 1.1 mycroft
648 1.1 mycroft #ifdef BHADIAG
649 1.1 mycroft ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
650 1.1 mycroft ccb->flags &= ~CCB_SENDING;
651 1.1 mycroft #endif
652 1.1 mycroft
653 1.1 mycroft --sc->sc_mbofull;
654 1.1 mycroft bha_nextmbx(wmbo, wmbx, mbo);
655 1.1 mycroft }
656 1.1 mycroft
657 1.1 mycroft wmbx->cmbo = wmbo;
658 1.1 mycroft }
659 1.1 mycroft
660 1.1 mycroft /*
661 1.1 mycroft * Send as many CCBs as we have empty mailboxes for.
662 1.1 mycroft */
663 1.1 mycroft void
664 1.1 mycroft bha_start_ccbs(sc)
665 1.1 mycroft struct bha_softc *sc;
666 1.1 mycroft {
667 1.4 thorpej bus_space_tag_t iot = sc->sc_iot;
668 1.4 thorpej bus_space_handle_t ioh = sc->sc_ioh;
669 1.1 mycroft struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
670 1.1 mycroft struct bha_ccb *ccb;
671 1.1 mycroft
672 1.1 mycroft wmbo = wmbx->tmbo;
673 1.1 mycroft
674 1.1 mycroft while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
675 1.1 mycroft if (sc->sc_mbofull >= BHA_MBX_SIZE) {
676 1.1 mycroft bha_collect_mbo(sc);
677 1.1 mycroft if (sc->sc_mbofull >= BHA_MBX_SIZE) {
678 1.1 mycroft struct bha_toggle toggle;
679 1.1 mycroft
680 1.1 mycroft toggle.cmd.opcode = BHA_MBO_INTR_EN;
681 1.1 mycroft toggle.cmd.enable = 1;
682 1.4 thorpej bha_cmd(iot, ioh, sc,
683 1.1 mycroft sizeof(toggle.cmd), (u_char *)&toggle.cmd,
684 1.1 mycroft 0, (u_char *)0);
685 1.1 mycroft break;
686 1.1 mycroft }
687 1.1 mycroft }
688 1.1 mycroft
689 1.1 mycroft TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
690 1.1 mycroft #ifdef BHADIAG
691 1.1 mycroft ccb->flags |= CCB_SENDING;
692 1.1 mycroft #endif
693 1.1 mycroft
694 1.1 mycroft /* Link ccb to mbo. */
695 1.22 thorpej ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
696 1.22 thorpej BHA_CCB_OFF(ccb), wmbo->ccb_addr);
697 1.1 mycroft if (ccb->flags & CCB_ABORT)
698 1.1 mycroft wmbo->cmd = BHA_MBO_ABORT;
699 1.1 mycroft else
700 1.1 mycroft wmbo->cmd = BHA_MBO_START;
701 1.1 mycroft
702 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
703 1.22 thorpej BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
704 1.22 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
705 1.22 thorpej
706 1.1 mycroft /* Tell the card to poll immediately. */
707 1.4 thorpej bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
708 1.1 mycroft
709 1.1 mycroft if ((ccb->xs->flags & SCSI_POLL) == 0)
710 1.1 mycroft timeout(bha_timeout, ccb, (ccb->timeout * hz) / 1000);
711 1.1 mycroft
712 1.1 mycroft ++sc->sc_mbofull;
713 1.1 mycroft bha_nextmbx(wmbo, wmbx, mbo);
714 1.1 mycroft }
715 1.1 mycroft
716 1.1 mycroft wmbx->tmbo = wmbo;
717 1.1 mycroft }
718 1.1 mycroft
719 1.1 mycroft /*
720 1.1 mycroft * We have a ccb which has been processed by the
721 1.1 mycroft * adaptor, now we look to see how the operation
722 1.1 mycroft * went. Wake up the owner if waiting
723 1.1 mycroft */
724 1.1 mycroft void
725 1.1 mycroft bha_done(sc, ccb)
726 1.1 mycroft struct bha_softc *sc;
727 1.1 mycroft struct bha_ccb *ccb;
728 1.1 mycroft {
729 1.13 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
730 1.14 bouyer struct scsipi_sense_data *s1, *s2;
731 1.14 bouyer struct scsipi_xfer *xs = ccb->xs;
732 1.1 mycroft
733 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
734 1.13 thorpej
735 1.13 thorpej /*
736 1.13 thorpej * If we were a data transfer, unload the map that described
737 1.13 thorpej * the data buffer.
738 1.13 thorpej */
739 1.13 thorpej if (xs->datalen) {
740 1.21 thorpej bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
741 1.21 thorpej ccb->dmamap_xfer->dm_mapsize,
742 1.13 thorpej (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
743 1.13 thorpej BUS_DMASYNC_POSTWRITE);
744 1.13 thorpej bus_dmamap_unload(dmat, ccb->dmamap_xfer);
745 1.13 thorpej }
746 1.13 thorpej
747 1.1 mycroft /*
748 1.1 mycroft * Otherwise, put the results of the operation
749 1.1 mycroft * into the xfer and call whoever started it
750 1.1 mycroft */
751 1.1 mycroft #ifdef BHADIAG
752 1.1 mycroft if (ccb->flags & CCB_SENDING) {
753 1.4 thorpej printf("%s: exiting ccb still in transit!\n",
754 1.4 thorpej sc->sc_dev.dv_xname);
755 1.1 mycroft Debugger();
756 1.1 mycroft return;
757 1.1 mycroft }
758 1.1 mycroft #endif
759 1.1 mycroft if ((ccb->flags & CCB_ALLOC) == 0) {
760 1.4 thorpej printf("%s: exiting ccb not allocated!\n",
761 1.4 thorpej sc->sc_dev.dv_xname);
762 1.1 mycroft Debugger();
763 1.1 mycroft return;
764 1.1 mycroft }
765 1.1 mycroft if (xs->error == XS_NOERROR) {
766 1.1 mycroft if (ccb->host_stat != BHA_OK) {
767 1.1 mycroft switch (ccb->host_stat) {
768 1.1 mycroft case BHA_SEL_TIMEOUT: /* No response */
769 1.1 mycroft xs->error = XS_SELTIMEOUT;
770 1.1 mycroft break;
771 1.1 mycroft default: /* Other scsi protocol messes */
772 1.3 christos printf("%s: host_stat %x\n",
773 1.1 mycroft sc->sc_dev.dv_xname, ccb->host_stat);
774 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
775 1.1 mycroft break;
776 1.1 mycroft }
777 1.1 mycroft } else if (ccb->target_stat != SCSI_OK) {
778 1.1 mycroft switch (ccb->target_stat) {
779 1.1 mycroft case SCSI_CHECK:
780 1.1 mycroft s1 = &ccb->scsi_sense;
781 1.14 bouyer s2 = &xs->sense.scsi_sense;
782 1.1 mycroft *s2 = *s1;
783 1.1 mycroft xs->error = XS_SENSE;
784 1.1 mycroft break;
785 1.1 mycroft case SCSI_BUSY:
786 1.1 mycroft xs->error = XS_BUSY;
787 1.1 mycroft break;
788 1.1 mycroft default:
789 1.3 christos printf("%s: target_stat %x\n",
790 1.1 mycroft sc->sc_dev.dv_xname, ccb->target_stat);
791 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
792 1.1 mycroft break;
793 1.1 mycroft }
794 1.1 mycroft } else
795 1.1 mycroft xs->resid = 0;
796 1.1 mycroft }
797 1.1 mycroft bha_free_ccb(sc, ccb);
798 1.1 mycroft xs->flags |= ITSDONE;
799 1.14 bouyer scsipi_done(xs);
800 1.17 thorpej
801 1.17 thorpej /*
802 1.17 thorpej * If there are queue entries in the software queue, try to
803 1.17 thorpej * run the first one. We should be more or less guaranteed
804 1.17 thorpej * to succeed, since we just freed a CCB.
805 1.17 thorpej *
806 1.17 thorpej * NOTE: bha_scsi_cmd() relies on our calling it with
807 1.17 thorpej * the first entry in the queue.
808 1.17 thorpej */
809 1.29 thorpej if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
810 1.17 thorpej (void) bha_scsi_cmd(xs);
811 1.1 mycroft }
812 1.1 mycroft
813 1.1 mycroft /*
814 1.1 mycroft * Find the board and find it's irq/drq
815 1.1 mycroft */
816 1.1 mycroft int
817 1.4 thorpej bha_find(iot, ioh, sc)
818 1.4 thorpej bus_space_tag_t iot;
819 1.4 thorpej bus_space_handle_t ioh;
820 1.11 mycroft struct bha_probe_data *sc;
821 1.1 mycroft {
822 1.8 thorpej int i, iswide;
823 1.1 mycroft u_char sts;
824 1.1 mycroft struct bha_extended_inquire inquire;
825 1.1 mycroft struct bha_config config;
826 1.1 mycroft int irq, drq;
827 1.1 mycroft
828 1.5 jonathan /* Check something is at the ports we need to access */
829 1.5 jonathan sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
830 1.5 jonathan if (sts == 0xFF)
831 1.5 jonathan return (0);
832 1.5 jonathan
833 1.1 mycroft /*
834 1.5 jonathan * Reset board, If it doesn't respond, assume
835 1.1 mycroft * that it's not there.. good for the probe
836 1.1 mycroft */
837 1.1 mycroft
838 1.4 thorpej bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
839 1.4 thorpej BHA_CTRL_HRST | BHA_CTRL_SRST);
840 1.1 mycroft
841 1.1 mycroft delay(100);
842 1.1 mycroft for (i = BHA_RESET_TIMEOUT; i; i--) {
843 1.4 thorpej sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
844 1.1 mycroft if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
845 1.1 mycroft break;
846 1.1 mycroft delay(1000);
847 1.1 mycroft }
848 1.1 mycroft if (!i) {
849 1.1 mycroft #ifdef BHADEBUG
850 1.1 mycroft if (bha_debug)
851 1.3 christos printf("bha_find: No answer from buslogic board\n");
852 1.1 mycroft #endif /* BHADEBUG */
853 1.1 mycroft return (0);
854 1.1 mycroft }
855 1.1 mycroft
856 1.1 mycroft /*
857 1.5 jonathan * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
858 1.5 jonathan * interface. The native bha interface is not compatible with
859 1.5 jonathan * an aha. 1542. We need to ensure that we never match an
860 1.5 jonathan * Adaptec 1542. We must also avoid sending Adaptec-compatible
861 1.5 jonathan * commands to a real bha, lest it go into 1542 emulation mode.
862 1.5 jonathan * (On an indirect bus like ISA, we should always probe for BusLogic
863 1.5 jonathan * interfaces before Adaptec interfaces).
864 1.5 jonathan */
865 1.5 jonathan
866 1.5 jonathan /*
867 1.5 jonathan * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
868 1.5 jonathan * for an extended-geometry register. The 1542[AB] don't have one.
869 1.5 jonathan */
870 1.5 jonathan sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
871 1.5 jonathan if (sts == 0xFF)
872 1.5 jonathan return (0);
873 1.5 jonathan
874 1.5 jonathan /*
875 1.1 mycroft * Check that we actually know how to use this board.
876 1.1 mycroft */
877 1.1 mycroft delay(1000);
878 1.1 mycroft inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
879 1.1 mycroft inquire.cmd.len = sizeof(inquire.reply);
880 1.11 mycroft i = bha_cmd(iot, ioh, (struct bha_softc *)0,
881 1.9 mycroft sizeof(inquire.cmd), (u_char *)&inquire.cmd,
882 1.9 mycroft sizeof(inquire.reply), (u_char *)&inquire.reply);
883 1.5 jonathan
884 1.5 jonathan /*
885 1.5 jonathan * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
886 1.5 jonathan * have the extended-geometry register and also respond to
887 1.5 jonathan * BHA_INQUIRE_EXTENDED. Make sure we never match such cards,
888 1.5 jonathan * by checking the size of the reply is what a BusLogic card returns.
889 1.5 jonathan */
890 1.9 mycroft if (i) {
891 1.5 jonathan #ifdef BHADEBUG
892 1.5 jonathan printf("bha_find: board returned %d instead of %d to %s\n",
893 1.5 jonathan i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
894 1.5 jonathan #endif
895 1.5 jonathan return (0);
896 1.5 jonathan }
897 1.5 jonathan
898 1.5 jonathan /* OK, we know we've found a buslogic adaptor. */
899 1.5 jonathan
900 1.1 mycroft switch (inquire.reply.bus_type) {
901 1.1 mycroft case BHA_BUS_TYPE_24BIT:
902 1.1 mycroft case BHA_BUS_TYPE_32BIT:
903 1.1 mycroft break;
904 1.1 mycroft case BHA_BUS_TYPE_MCA:
905 1.1 mycroft /* We don't grok MicroChannel (yet). */
906 1.1 mycroft return (0);
907 1.1 mycroft default:
908 1.4 thorpej printf("bha_find: illegal bus type %c\n",
909 1.4 thorpej inquire.reply.bus_type);
910 1.1 mycroft return (0);
911 1.1 mycroft }
912 1.1 mycroft
913 1.8 thorpej /* Note if we have a wide bus. */
914 1.8 thorpej iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
915 1.8 thorpej
916 1.1 mycroft /*
917 1.1 mycroft * Assume we have a board at this stage setup dma channel from
918 1.1 mycroft * jumpers and save int level
919 1.1 mycroft */
920 1.1 mycroft delay(1000);
921 1.1 mycroft config.cmd.opcode = BHA_INQUIRE_CONFIG;
922 1.11 mycroft bha_cmd(iot, ioh, (struct bha_softc *)0,
923 1.1 mycroft sizeof(config.cmd), (u_char *)&config.cmd,
924 1.1 mycroft sizeof(config.reply), (u_char *)&config.reply);
925 1.1 mycroft switch (config.reply.chan) {
926 1.1 mycroft case EISADMA:
927 1.1 mycroft drq = -1;
928 1.1 mycroft break;
929 1.1 mycroft case CHAN0:
930 1.1 mycroft drq = 0;
931 1.1 mycroft break;
932 1.1 mycroft case CHAN5:
933 1.1 mycroft drq = 5;
934 1.1 mycroft break;
935 1.1 mycroft case CHAN6:
936 1.1 mycroft drq = 6;
937 1.1 mycroft break;
938 1.1 mycroft case CHAN7:
939 1.1 mycroft drq = 7;
940 1.1 mycroft break;
941 1.1 mycroft default:
942 1.4 thorpej printf("bha_find: illegal drq setting %x\n",
943 1.4 thorpej config.reply.chan);
944 1.1 mycroft return (0);
945 1.1 mycroft }
946 1.1 mycroft
947 1.1 mycroft switch (config.reply.intr) {
948 1.1 mycroft case INT9:
949 1.1 mycroft irq = 9;
950 1.1 mycroft break;
951 1.1 mycroft case INT10:
952 1.1 mycroft irq = 10;
953 1.1 mycroft break;
954 1.1 mycroft case INT11:
955 1.1 mycroft irq = 11;
956 1.1 mycroft break;
957 1.1 mycroft case INT12:
958 1.1 mycroft irq = 12;
959 1.1 mycroft break;
960 1.1 mycroft case INT14:
961 1.1 mycroft irq = 14;
962 1.1 mycroft break;
963 1.1 mycroft case INT15:
964 1.1 mycroft irq = 15;
965 1.1 mycroft break;
966 1.1 mycroft default:
967 1.4 thorpej printf("bha_find: illegal irq setting %x\n",
968 1.4 thorpej config.reply.intr);
969 1.1 mycroft return (0);
970 1.1 mycroft }
971 1.1 mycroft
972 1.1 mycroft /* if we want to fill in softc, do so now */
973 1.1 mycroft if (sc != NULL) {
974 1.1 mycroft sc->sc_irq = irq;
975 1.1 mycroft sc->sc_drq = drq;
976 1.1 mycroft sc->sc_scsi_dev = config.reply.scsi_dev;
977 1.8 thorpej sc->sc_iswide = iswide;
978 1.1 mycroft }
979 1.1 mycroft
980 1.1 mycroft return (1);
981 1.1 mycroft }
982 1.5 jonathan
983 1.5 jonathan
984 1.5 jonathan /*
985 1.5 jonathan * Disable the ISA-compatiblity ioports on PCI bha devices,
986 1.5 jonathan * to ensure they're not autoconfigured a second time as an ISA bha.
987 1.5 jonathan */
988 1.5 jonathan int
989 1.5 jonathan bha_disable_isacompat(sc)
990 1.5 jonathan struct bha_softc *sc;
991 1.5 jonathan {
992 1.5 jonathan struct bha_isadisable isa_disable;
993 1.5 jonathan
994 1.5 jonathan isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
995 1.5 jonathan isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
996 1.5 jonathan bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
997 1.5 jonathan sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
998 1.6 thorpej 0, (u_char *)0);
999 1.5 jonathan return (0);
1000 1.5 jonathan }
1001 1.5 jonathan
1002 1.1 mycroft
1003 1.1 mycroft /*
1004 1.1 mycroft * Start the board, ready for normal operation
1005 1.1 mycroft */
1006 1.22 thorpej int
1007 1.1 mycroft bha_init(sc)
1008 1.1 mycroft struct bha_softc *sc;
1009 1.1 mycroft {
1010 1.4 thorpej bus_space_tag_t iot = sc->sc_iot;
1011 1.4 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1012 1.13 thorpej bus_dma_segment_t seg;
1013 1.1 mycroft struct bha_devices devices;
1014 1.1 mycroft struct bha_setup setup;
1015 1.1 mycroft struct bha_mailbox mailbox;
1016 1.1 mycroft struct bha_period period;
1017 1.22 thorpej int error, i, j, initial_ccbs, rlen, rseg;
1018 1.1 mycroft
1019 1.1 mycroft /* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1020 1.1 mycroft if (strcmp(sc->sc_firmware, "3.31") >= 0) {
1021 1.1 mycroft struct bha_toggle toggle;
1022 1.1 mycroft
1023 1.1 mycroft toggle.cmd.opcode = BHA_ROUND_ROBIN;
1024 1.1 mycroft toggle.cmd.enable = 1;
1025 1.4 thorpej bha_cmd(iot, ioh, sc,
1026 1.1 mycroft sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1027 1.1 mycroft 0, (u_char *)0);
1028 1.1 mycroft }
1029 1.1 mycroft
1030 1.8 thorpej /*
1031 1.8 thorpej * Inquire installed devices (to force synchronous negotiation).
1032 1.8 thorpej */
1033 1.8 thorpej
1034 1.8 thorpej /*
1035 1.8 thorpej * Poll targets 0 - 7.
1036 1.8 thorpej */
1037 1.1 mycroft devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1038 1.4 thorpej bha_cmd(iot, ioh, sc,
1039 1.1 mycroft sizeof(devices.cmd), (u_char *)&devices.cmd,
1040 1.1 mycroft sizeof(devices.reply), (u_char *)&devices.reply);
1041 1.1 mycroft
1042 1.15 thorpej /* Count installed units. */
1043 1.15 thorpej initial_ccbs = 0;
1044 1.15 thorpej for (i = 0; i < 8; i++) {
1045 1.15 thorpej for (j = 0; j < 8; j++) {
1046 1.15 thorpej if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1047 1.15 thorpej initial_ccbs++;
1048 1.15 thorpej }
1049 1.15 thorpej }
1050 1.15 thorpej
1051 1.8 thorpej /*
1052 1.8 thorpej * Poll targets 8 - 15 if we have a wide bus.
1053 1.8 thorpej */
1054 1.11 mycroft if (ISWIDE(sc)) {
1055 1.8 thorpej devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1056 1.8 thorpej bha_cmd(iot, ioh, sc,
1057 1.8 thorpej sizeof(devices.cmd), (u_char *)&devices.cmd,
1058 1.8 thorpej sizeof(devices.reply), (u_char *)&devices.reply);
1059 1.15 thorpej
1060 1.15 thorpej for (i = 0; i < 8; i++) {
1061 1.15 thorpej for (j = 0; j < 8; j++) {
1062 1.15 thorpej if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1063 1.15 thorpej initial_ccbs++;
1064 1.15 thorpej }
1065 1.15 thorpej }
1066 1.8 thorpej }
1067 1.8 thorpej
1068 1.15 thorpej initial_ccbs *= sc->sc_link.openings;
1069 1.22 thorpej if (initial_ccbs > BHA_CCB_MAX)
1070 1.22 thorpej initial_ccbs = BHA_CCB_MAX;
1071 1.23 mjacob if (initial_ccbs == 0) /* yes, this can happen */
1072 1.23 mjacob initial_ccbs = sc->sc_link.openings;
1073 1.15 thorpej
1074 1.1 mycroft /* Obtain setup information from. */
1075 1.8 thorpej rlen = sizeof(setup.reply) +
1076 1.11 mycroft (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
1077 1.1 mycroft setup.cmd.opcode = BHA_INQUIRE_SETUP;
1078 1.8 thorpej setup.cmd.len = rlen;
1079 1.4 thorpej bha_cmd(iot, ioh, sc,
1080 1.1 mycroft sizeof(setup.cmd), (u_char *)&setup.cmd,
1081 1.8 thorpej rlen, (u_char *)&setup.reply);
1082 1.1 mycroft
1083 1.3 christos printf("%s: %s, %s\n",
1084 1.1 mycroft sc->sc_dev.dv_xname,
1085 1.1 mycroft setup.reply.sync_neg ? "sync" : "async",
1086 1.1 mycroft setup.reply.parity ? "parity" : "no parity");
1087 1.1 mycroft
1088 1.1 mycroft for (i = 0; i < 8; i++)
1089 1.1 mycroft period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
1090 1.11 mycroft if (ISWIDE(sc)) {
1091 1.8 thorpej for (i = 0; i < 8; i++)
1092 1.8 thorpej period.reply_w.period[i] =
1093 1.8 thorpej setup.reply_w.sync[i].period * 5 + 20;
1094 1.8 thorpej }
1095 1.1 mycroft
1096 1.1 mycroft if (sc->sc_firmware[0] >= '3') {
1097 1.8 thorpej rlen = sizeof(period.reply) +
1098 1.11 mycroft (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
1099 1.1 mycroft period.cmd.opcode = BHA_INQUIRE_PERIOD;
1100 1.8 thorpej period.cmd.len = rlen;
1101 1.4 thorpej bha_cmd(iot, ioh, sc,
1102 1.1 mycroft sizeof(period.cmd), (u_char *)&period.cmd,
1103 1.8 thorpej rlen, (u_char *)&period.reply);
1104 1.1 mycroft }
1105 1.1 mycroft
1106 1.1 mycroft for (i = 0; i < 8; i++) {
1107 1.1 mycroft if (!setup.reply.sync[i].valid ||
1108 1.4 thorpej (!setup.reply.sync[i].offset &&
1109 1.4 thorpej !setup.reply.sync[i].period))
1110 1.1 mycroft continue;
1111 1.3 christos printf("%s targ %d: sync, offset %d, period %dnsec\n",
1112 1.1 mycroft sc->sc_dev.dv_xname, i,
1113 1.1 mycroft setup.reply.sync[i].offset, period.reply.period[i] * 10);
1114 1.8 thorpej }
1115 1.11 mycroft if (ISWIDE(sc)) {
1116 1.8 thorpej for (i = 0; i < 8; i++) {
1117 1.8 thorpej if (!setup.reply_w.sync[i].valid ||
1118 1.8 thorpej (!setup.reply_w.sync[i].offset &&
1119 1.8 thorpej !setup.reply_w.sync[i].period))
1120 1.8 thorpej continue;
1121 1.8 thorpej printf("%s targ %d: sync, offset %d, period %dnsec\n",
1122 1.8 thorpej sc->sc_dev.dv_xname, i + 8,
1123 1.8 thorpej setup.reply_w.sync[i].offset,
1124 1.8 thorpej period.reply_w.period[i] * 10);
1125 1.8 thorpej }
1126 1.1 mycroft }
1127 1.1 mycroft
1128 1.1 mycroft /*
1129 1.22 thorpej * Allocate the mailbox and control blocks.
1130 1.13 thorpej */
1131 1.22 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct bha_control),
1132 1.22 thorpej NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
1133 1.22 thorpej printf("%s: unable to allocate control structures, "
1134 1.22 thorpej "error = %d\n", sc->sc_dev.dv_xname, error);
1135 1.22 thorpej return (error);
1136 1.22 thorpej }
1137 1.22 thorpej if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
1138 1.22 thorpej sizeof(struct bha_control), (caddr_t *)&sc->sc_control,
1139 1.22 thorpej BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
1140 1.22 thorpej printf("%s: unable to map control structures, error = %d\n",
1141 1.22 thorpej sc->sc_dev.dv_xname, error);
1142 1.22 thorpej return (error);
1143 1.22 thorpej }
1144 1.13 thorpej
1145 1.13 thorpej /*
1146 1.22 thorpej * Create and load the DMA map used for the mailbox and
1147 1.22 thorpej * control blocks.
1148 1.13 thorpej */
1149 1.22 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_control),
1150 1.22 thorpej 1, sizeof(struct bha_control), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
1151 1.22 thorpej &sc->sc_dmamap_control)) != 0) {
1152 1.22 thorpej printf("%s: unable to create control DMA map, error = %d\n",
1153 1.22 thorpej sc->sc_dev.dv_xname, error);
1154 1.22 thorpej return (error);
1155 1.22 thorpej }
1156 1.22 thorpej if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
1157 1.22 thorpej sc->sc_control, sizeof(struct bha_control), NULL,
1158 1.22 thorpej BUS_DMA_NOWAIT)) != 0) {
1159 1.22 thorpej printf("%s: unable to load control DMA map, error = %d\n",
1160 1.22 thorpej sc->sc_dev.dv_xname, error);
1161 1.22 thorpej return (error);
1162 1.22 thorpej }
1163 1.13 thorpej
1164 1.13 thorpej /*
1165 1.22 thorpej * Initialize the control blocks.
1166 1.13 thorpej */
1167 1.22 thorpej i = bha_create_ccbs(sc, sc->sc_control->bc_ccbs, initial_ccbs);
1168 1.22 thorpej if (i == 0) {
1169 1.22 thorpej printf("%s: unable to create control blocks\n",
1170 1.22 thorpej sc->sc_dev.dv_xname);
1171 1.22 thorpej return (ENOMEM);
1172 1.22 thorpej } else if (i != initial_ccbs) {
1173 1.22 thorpej printf("%s: WARNING: only %d of %d control blocks created\n",
1174 1.22 thorpej sc->sc_dev.dv_xname, i, initial_ccbs);
1175 1.22 thorpej }
1176 1.13 thorpej
1177 1.13 thorpej /*
1178 1.1 mycroft * Set up initial mail box for round-robin operation.
1179 1.1 mycroft */
1180 1.1 mycroft for (i = 0; i < BHA_MBX_SIZE; i++) {
1181 1.1 mycroft wmbx->mbo[i].cmd = BHA_MBO_FREE;
1182 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1183 1.22 thorpej BHA_MBO_OFF(&wmbx->mbo[i]), sizeof(struct bha_mbx_out),
1184 1.22 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1185 1.1 mycroft wmbx->mbi[i].stat = BHA_MBI_FREE;
1186 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1187 1.22 thorpej BHA_MBI_OFF(&wmbx->mbi[i]), sizeof(struct bha_mbx_in),
1188 1.22 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1189 1.1 mycroft }
1190 1.1 mycroft wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1191 1.1 mycroft wmbx->tmbi = &wmbx->mbi[0];
1192 1.1 mycroft sc->sc_mbofull = 0;
1193 1.1 mycroft
1194 1.1 mycroft /* Initialize mail box. */
1195 1.1 mycroft mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1196 1.1 mycroft mailbox.cmd.nmbx = BHA_MBX_SIZE;
1197 1.22 thorpej ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1198 1.22 thorpej offsetof(struct bha_control, bc_mbx), mailbox.cmd.addr);
1199 1.4 thorpej bha_cmd(iot, ioh, sc,
1200 1.1 mycroft sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1201 1.1 mycroft 0, (u_char *)0);
1202 1.22 thorpej return (0);
1203 1.1 mycroft }
1204 1.1 mycroft
1205 1.1 mycroft void
1206 1.1 mycroft bha_inquire_setup_information(sc)
1207 1.1 mycroft struct bha_softc *sc;
1208 1.1 mycroft {
1209 1.4 thorpej bus_space_tag_t iot = sc->sc_iot;
1210 1.4 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1211 1.1 mycroft struct bha_model model;
1212 1.1 mycroft struct bha_revision revision;
1213 1.1 mycroft struct bha_digit digit;
1214 1.1 mycroft char *p;
1215 1.1 mycroft
1216 1.1 mycroft /*
1217 1.1 mycroft * Get the firmware revision.
1218 1.1 mycroft */
1219 1.1 mycroft p = sc->sc_firmware;
1220 1.1 mycroft revision.cmd.opcode = BHA_INQUIRE_REVISION;
1221 1.4 thorpej bha_cmd(iot, ioh, sc,
1222 1.1 mycroft sizeof(revision.cmd), (u_char *)&revision.cmd,
1223 1.1 mycroft sizeof(revision.reply), (u_char *)&revision.reply);
1224 1.1 mycroft *p++ = revision.reply.firm_revision;
1225 1.1 mycroft *p++ = '.';
1226 1.1 mycroft *p++ = revision.reply.firm_version;
1227 1.1 mycroft digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1228 1.4 thorpej bha_cmd(iot, ioh, sc,
1229 1.1 mycroft sizeof(digit.cmd), (u_char *)&digit.cmd,
1230 1.1 mycroft sizeof(digit.reply), (u_char *)&digit.reply);
1231 1.1 mycroft *p++ = digit.reply.digit;
1232 1.1 mycroft if (revision.reply.firm_revision >= '3' ||
1233 1.4 thorpej (revision.reply.firm_revision == '3' &&
1234 1.4 thorpej revision.reply.firm_version >= '3')) {
1235 1.1 mycroft digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1236 1.4 thorpej bha_cmd(iot, ioh, sc,
1237 1.1 mycroft sizeof(digit.cmd), (u_char *)&digit.cmd,
1238 1.1 mycroft sizeof(digit.reply), (u_char *)&digit.reply);
1239 1.1 mycroft *p++ = digit.reply.digit;
1240 1.1 mycroft }
1241 1.1 mycroft while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1242 1.1 mycroft p--;
1243 1.1 mycroft *p = '\0';
1244 1.1 mycroft
1245 1.1 mycroft /*
1246 1.1 mycroft * Get the model number.
1247 1.1 mycroft */
1248 1.1 mycroft if (revision.reply.firm_revision >= '3') {
1249 1.1 mycroft p = sc->sc_model;
1250 1.1 mycroft model.cmd.opcode = BHA_INQUIRE_MODEL;
1251 1.1 mycroft model.cmd.len = sizeof(model.reply);
1252 1.4 thorpej bha_cmd(iot, ioh, sc,
1253 1.1 mycroft sizeof(model.cmd), (u_char *)&model.cmd,
1254 1.1 mycroft sizeof(model.reply), (u_char *)&model.reply);
1255 1.1 mycroft *p++ = model.reply.id[0];
1256 1.1 mycroft *p++ = model.reply.id[1];
1257 1.1 mycroft *p++ = model.reply.id[2];
1258 1.1 mycroft *p++ = model.reply.id[3];
1259 1.1 mycroft while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1260 1.1 mycroft p--;
1261 1.1 mycroft *p++ = model.reply.version[0];
1262 1.1 mycroft *p++ = model.reply.version[1];
1263 1.1 mycroft while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1264 1.1 mycroft p--;
1265 1.1 mycroft *p = '\0';
1266 1.1 mycroft } else
1267 1.1 mycroft strcpy(sc->sc_model, "542B");
1268 1.1 mycroft }
1269 1.1 mycroft
1270 1.1 mycroft void
1271 1.1 mycroft bhaminphys(bp)
1272 1.1 mycroft struct buf *bp;
1273 1.1 mycroft {
1274 1.1 mycroft
1275 1.13 thorpej if (bp->b_bcount > BHA_MAXXFER)
1276 1.13 thorpej bp->b_bcount = BHA_MAXXFER;
1277 1.1 mycroft minphys(bp);
1278 1.1 mycroft }
1279 1.1 mycroft
1280 1.1 mycroft /*
1281 1.1 mycroft * start a scsi operation given the command and the data address. Also needs
1282 1.1 mycroft * the unit, target and lu.
1283 1.1 mycroft */
1284 1.1 mycroft int
1285 1.1 mycroft bha_scsi_cmd(xs)
1286 1.14 bouyer struct scsipi_xfer *xs;
1287 1.1 mycroft {
1288 1.14 bouyer struct scsipi_link *sc_link = xs->sc_link;
1289 1.1 mycroft struct bha_softc *sc = sc_link->adapter_softc;
1290 1.13 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
1291 1.1 mycroft struct bha_ccb *ccb;
1292 1.13 thorpej int error, seg, flags, s;
1293 1.17 thorpej int fromqueue = 0, dontqueue = 0;
1294 1.1 mycroft
1295 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
1296 1.17 thorpej
1297 1.17 thorpej s = splbio(); /* protect the queue */
1298 1.17 thorpej
1299 1.17 thorpej /*
1300 1.17 thorpej * If we're running the queue from bha_done(), we've been
1301 1.17 thorpej * called with the first queue entry as our argument.
1302 1.17 thorpej */
1303 1.29 thorpej if (xs == TAILQ_FIRST(&sc->sc_queue)) {
1304 1.29 thorpej TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
1305 1.17 thorpej fromqueue = 1;
1306 1.17 thorpej goto get_ccb;
1307 1.17 thorpej }
1308 1.17 thorpej
1309 1.17 thorpej /* Polled requests can't be queued for later. */
1310 1.17 thorpej dontqueue = xs->flags & SCSI_POLL;
1311 1.17 thorpej
1312 1.17 thorpej /*
1313 1.17 thorpej * If there are jobs in the queue, run them first.
1314 1.17 thorpej */
1315 1.29 thorpej if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
1316 1.17 thorpej /*
1317 1.17 thorpej * If we can't queue, we have to abort, since
1318 1.17 thorpej * we have to preserve order.
1319 1.17 thorpej */
1320 1.17 thorpej if (dontqueue) {
1321 1.17 thorpej splx(s);
1322 1.17 thorpej xs->error = XS_DRIVER_STUFFUP;
1323 1.17 thorpej return (TRY_AGAIN_LATER);
1324 1.17 thorpej }
1325 1.17 thorpej
1326 1.17 thorpej /*
1327 1.17 thorpej * Swap with the first queue entry.
1328 1.17 thorpej */
1329 1.29 thorpej TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
1330 1.29 thorpej xs = TAILQ_FIRST(&sc->sc_queue);
1331 1.29 thorpej TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
1332 1.17 thorpej fromqueue = 1;
1333 1.17 thorpej }
1334 1.17 thorpej
1335 1.17 thorpej get_ccb:
1336 1.1 mycroft /*
1337 1.1 mycroft * get a ccb to use. If the transfer
1338 1.1 mycroft * is from a buf (possibly from interrupt time)
1339 1.1 mycroft * then we can't allow it to sleep
1340 1.1 mycroft */
1341 1.1 mycroft flags = xs->flags;
1342 1.1 mycroft if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
1343 1.17 thorpej /*
1344 1.17 thorpej * If we can't queue, we lose.
1345 1.17 thorpej */
1346 1.17 thorpej if (dontqueue) {
1347 1.17 thorpej splx(s);
1348 1.17 thorpej xs->error = XS_DRIVER_STUFFUP;
1349 1.17 thorpej return (TRY_AGAIN_LATER);
1350 1.17 thorpej }
1351 1.17 thorpej
1352 1.17 thorpej /*
1353 1.17 thorpej * Stuff ourselves into the queue, in front
1354 1.17 thorpej * if we came off in the first place.
1355 1.17 thorpej */
1356 1.29 thorpej if (fromqueue)
1357 1.29 thorpej TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
1358 1.29 thorpej else
1359 1.29 thorpej TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
1360 1.17 thorpej splx(s);
1361 1.17 thorpej return (SUCCESSFULLY_QUEUED);
1362 1.1 mycroft }
1363 1.17 thorpej
1364 1.17 thorpej splx(s); /* done playing with the queue */
1365 1.17 thorpej
1366 1.1 mycroft ccb->xs = xs;
1367 1.1 mycroft ccb->timeout = xs->timeout;
1368 1.1 mycroft
1369 1.1 mycroft /*
1370 1.1 mycroft * Put all the arguments for the xfer in the ccb
1371 1.1 mycroft */
1372 1.1 mycroft if (flags & SCSI_RESET) {
1373 1.1 mycroft ccb->opcode = BHA_RESET_CCB;
1374 1.1 mycroft ccb->scsi_cmd_length = 0;
1375 1.1 mycroft } else {
1376 1.1 mycroft /* can't use S/G if zero length */
1377 1.1 mycroft ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
1378 1.1 mycroft : BHA_INITIATOR_CCB);
1379 1.1 mycroft bcopy(xs->cmd, &ccb->scsi_cmd,
1380 1.1 mycroft ccb->scsi_cmd_length = xs->cmdlen);
1381 1.1 mycroft }
1382 1.1 mycroft
1383 1.1 mycroft if (xs->datalen) {
1384 1.13 thorpej /*
1385 1.13 thorpej * Map the DMA transfer.
1386 1.13 thorpej */
1387 1.13 thorpej #ifdef TFS
1388 1.1 mycroft if (flags & SCSI_DATA_UIO) {
1389 1.13 thorpej error = bus_dmamap_load_uio(dmat,
1390 1.13 thorpej ccb->dmamap_xfer, (struct uio *)xs->data,
1391 1.13 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1392 1.13 thorpej BUS_DMA_WAITOK);
1393 1.1 mycroft } else
1394 1.13 thorpej #endif /* TFS */
1395 1.1 mycroft {
1396 1.13 thorpej error = bus_dmamap_load(dmat,
1397 1.13 thorpej ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
1398 1.13 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1399 1.13 thorpej BUS_DMA_WAITOK);
1400 1.13 thorpej }
1401 1.1 mycroft
1402 1.13 thorpej if (error) {
1403 1.13 thorpej if (error == EFBIG) {
1404 1.13 thorpej printf("%s: bha_scsi_cmd, more than %d"
1405 1.13 thorpej " dma segments\n",
1406 1.13 thorpej sc->sc_dev.dv_xname, BHA_NSEG);
1407 1.13 thorpej } else {
1408 1.13 thorpej printf("%s: bha_scsi_cmd, error %d loading"
1409 1.13 thorpej " dma map\n",
1410 1.13 thorpej sc->sc_dev.dv_xname, error);
1411 1.1 mycroft }
1412 1.13 thorpej goto bad;
1413 1.1 mycroft }
1414 1.13 thorpej
1415 1.21 thorpej bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
1416 1.21 thorpej ccb->dmamap_xfer->dm_mapsize,
1417 1.13 thorpej (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1418 1.13 thorpej BUS_DMASYNC_PREWRITE);
1419 1.13 thorpej
1420 1.13 thorpej /*
1421 1.13 thorpej * Load the hardware scatter/gather map with the
1422 1.13 thorpej * contents of the DMA map.
1423 1.13 thorpej */
1424 1.13 thorpej for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
1425 1.13 thorpej ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
1426 1.13 thorpej ccb->scat_gath[seg].seg_addr);
1427 1.13 thorpej ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
1428 1.13 thorpej ccb->scat_gath[seg].seg_len);
1429 1.1 mycroft }
1430 1.13 thorpej
1431 1.22 thorpej ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1432 1.22 thorpej BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scat_gath),
1433 1.22 thorpej ccb->data_addr);
1434 1.13 thorpej ltophys(ccb->dmamap_xfer->dm_nsegs *
1435 1.13 thorpej sizeof(struct bha_scat_gath), ccb->data_length);
1436 1.13 thorpej } else {
1437 1.13 thorpej /*
1438 1.13 thorpej * No data xfer, use non S/G values.
1439 1.13 thorpej */
1440 1.1 mycroft ltophys(0, ccb->data_addr);
1441 1.1 mycroft ltophys(0, ccb->data_length);
1442 1.1 mycroft }
1443 1.1 mycroft
1444 1.1 mycroft ccb->data_out = 0;
1445 1.1 mycroft ccb->data_in = 0;
1446 1.14 bouyer ccb->target = sc_link->scsipi_scsi.target;
1447 1.14 bouyer ccb->lun = sc_link->scsipi_scsi.lun;
1448 1.22 thorpej ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1449 1.22 thorpej BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scsi_sense),
1450 1.22 thorpej ccb->sense_ptr);
1451 1.1 mycroft ccb->req_sense_length = sizeof(ccb->scsi_sense);
1452 1.1 mycroft ccb->host_stat = 0x00;
1453 1.1 mycroft ccb->target_stat = 0x00;
1454 1.1 mycroft ccb->link_id = 0;
1455 1.1 mycroft ltophys(0, ccb->link_addr);
1456 1.22 thorpej
1457 1.22 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1458 1.22 thorpej BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
1459 1.22 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1460 1.1 mycroft
1461 1.1 mycroft s = splbio();
1462 1.1 mycroft bha_queue_ccb(sc, ccb);
1463 1.1 mycroft splx(s);
1464 1.1 mycroft
1465 1.1 mycroft /*
1466 1.1 mycroft * Usually return SUCCESSFULLY QUEUED
1467 1.1 mycroft */
1468 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1469 1.1 mycroft if ((flags & SCSI_POLL) == 0)
1470 1.1 mycroft return (SUCCESSFULLY_QUEUED);
1471 1.1 mycroft
1472 1.1 mycroft /*
1473 1.1 mycroft * If we can't use interrupts, poll on completion
1474 1.1 mycroft */
1475 1.1 mycroft if (bha_poll(sc, xs, ccb->timeout)) {
1476 1.1 mycroft bha_timeout(ccb);
1477 1.1 mycroft if (bha_poll(sc, xs, ccb->timeout))
1478 1.1 mycroft bha_timeout(ccb);
1479 1.1 mycroft }
1480 1.1 mycroft return (COMPLETE);
1481 1.1 mycroft
1482 1.1 mycroft bad:
1483 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
1484 1.1 mycroft bha_free_ccb(sc, ccb);
1485 1.1 mycroft return (COMPLETE);
1486 1.1 mycroft }
1487 1.1 mycroft
1488 1.1 mycroft /*
1489 1.1 mycroft * Poll a particular unit, looking for a particular xs
1490 1.1 mycroft */
1491 1.1 mycroft int
1492 1.1 mycroft bha_poll(sc, xs, count)
1493 1.1 mycroft struct bha_softc *sc;
1494 1.14 bouyer struct scsipi_xfer *xs;
1495 1.1 mycroft int count;
1496 1.1 mycroft {
1497 1.4 thorpej bus_space_tag_t iot = sc->sc_iot;
1498 1.4 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1499 1.1 mycroft
1500 1.1 mycroft /* timeouts are in msec, so we loop in 1000 usec cycles */
1501 1.1 mycroft while (count) {
1502 1.1 mycroft /*
1503 1.1 mycroft * If we had interrupts enabled, would we
1504 1.1 mycroft * have got an interrupt?
1505 1.1 mycroft */
1506 1.4 thorpej if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
1507 1.4 thorpej BHA_INTR_ANYINTR)
1508 1.1 mycroft bha_intr(sc);
1509 1.1 mycroft if (xs->flags & ITSDONE)
1510 1.1 mycroft return (0);
1511 1.1 mycroft delay(1000); /* only happens in boot so ok */
1512 1.1 mycroft count--;
1513 1.1 mycroft }
1514 1.1 mycroft return (1);
1515 1.1 mycroft }
1516 1.1 mycroft
1517 1.1 mycroft void
1518 1.1 mycroft bha_timeout(arg)
1519 1.1 mycroft void *arg;
1520 1.1 mycroft {
1521 1.1 mycroft struct bha_ccb *ccb = arg;
1522 1.14 bouyer struct scsipi_xfer *xs = ccb->xs;
1523 1.14 bouyer struct scsipi_link *sc_link = xs->sc_link;
1524 1.1 mycroft struct bha_softc *sc = sc_link->adapter_softc;
1525 1.1 mycroft int s;
1526 1.1 mycroft
1527 1.14 bouyer scsi_print_addr(sc_link);
1528 1.3 christos printf("timed out");
1529 1.1 mycroft
1530 1.1 mycroft s = splbio();
1531 1.1 mycroft
1532 1.1 mycroft #ifdef BHADIAG
1533 1.1 mycroft /*
1534 1.1 mycroft * If the ccb's mbx is not free, then the board has gone Far East?
1535 1.1 mycroft */
1536 1.1 mycroft bha_collect_mbo(sc);
1537 1.1 mycroft if (ccb->flags & CCB_SENDING) {
1538 1.3 christos printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1539 1.1 mycroft Debugger();
1540 1.1 mycroft }
1541 1.1 mycroft #endif
1542 1.1 mycroft
1543 1.1 mycroft /*
1544 1.1 mycroft * If it has been through before, then
1545 1.1 mycroft * a previous abort has failed, don't
1546 1.1 mycroft * try abort again
1547 1.1 mycroft */
1548 1.1 mycroft if (ccb->flags & CCB_ABORT) {
1549 1.1 mycroft /* abort timed out */
1550 1.3 christos printf(" AGAIN\n");
1551 1.1 mycroft /* XXX Must reset! */
1552 1.1 mycroft } else {
1553 1.1 mycroft /* abort the operation that has timed out */
1554 1.3 christos printf("\n");
1555 1.1 mycroft ccb->xs->error = XS_TIMEOUT;
1556 1.1 mycroft ccb->timeout = BHA_ABORT_TIMEOUT;
1557 1.1 mycroft ccb->flags |= CCB_ABORT;
1558 1.1 mycroft bha_queue_ccb(sc, ccb);
1559 1.1 mycroft }
1560 1.1 mycroft
1561 1.1 mycroft splx(s);
1562 1.1 mycroft }
1563