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