ahb.c revision 1.20 1 1.20 jonathan /* $NetBSD: ahb.c,v 1.20 1998/07/04 22:18:48 jonathan Exp $ */
2 1.20 jonathan
3 1.20 jonathan #include "opt_ddb.h"
4 1.1 mycroft
5 1.1 mycroft #undef AHBDEBUG
6 1.1 mycroft #ifdef DDB
7 1.1 mycroft #define integrate
8 1.1 mycroft #else
9 1.1 mycroft #define integrate static inline
10 1.1 mycroft #endif
11 1.1 mycroft
12 1.9 thorpej /*-
13 1.17 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
14 1.9 thorpej * All rights reserved.
15 1.9 thorpej *
16 1.9 thorpej * This code is derived from software contributed to The NetBSD Foundation
17 1.9 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
18 1.9 thorpej * NASA Ames Research Center.
19 1.9 thorpej *
20 1.9 thorpej * Redistribution and use in source and binary forms, with or without
21 1.9 thorpej * modification, are permitted provided that the following conditions
22 1.9 thorpej * are met:
23 1.9 thorpej * 1. Redistributions of source code must retain the above copyright
24 1.9 thorpej * notice, this list of conditions and the following disclaimer.
25 1.9 thorpej * 2. Redistributions in binary form must reproduce the above copyright
26 1.9 thorpej * notice, this list of conditions and the following disclaimer in the
27 1.9 thorpej * documentation and/or other materials provided with the distribution.
28 1.9 thorpej * 3. All advertising materials mentioning features or use of this software
29 1.9 thorpej * must display the following acknowledgement:
30 1.9 thorpej * This product includes software developed by the NetBSD
31 1.9 thorpej * Foundation, Inc. and its contributors.
32 1.9 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
33 1.9 thorpej * contributors may be used to endorse or promote products derived
34 1.9 thorpej * from this software without specific prior written permission.
35 1.9 thorpej *
36 1.9 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 1.9 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 1.9 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 1.9 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 1.9 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 1.9 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 1.9 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 1.9 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 1.9 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 1.9 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 1.9 thorpej * POSSIBILITY OF SUCH DAMAGE.
47 1.9 thorpej */
48 1.9 thorpej
49 1.1 mycroft /*
50 1.8 mycroft * Copyright (c) 1994, 1996, 1997 Charles M. Hannum. All rights reserved.
51 1.1 mycroft *
52 1.1 mycroft * Redistribution and use in source and binary forms, with or without
53 1.1 mycroft * modification, are permitted provided that the following conditions
54 1.1 mycroft * are met:
55 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
56 1.1 mycroft * notice, this list of conditions and the following disclaimer.
57 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
58 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
59 1.1 mycroft * documentation and/or other materials provided with the distribution.
60 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
61 1.1 mycroft * must display the following acknowledgement:
62 1.1 mycroft * This product includes software developed by Charles M. Hannum.
63 1.1 mycroft * 4. The name of the author may not be used to endorse or promote products
64 1.1 mycroft * derived from this software without specific prior written permission.
65 1.1 mycroft *
66 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
67 1.1 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
68 1.1 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
69 1.1 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
70 1.1 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
71 1.1 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
72 1.1 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
73 1.1 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
74 1.1 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
75 1.1 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 1.1 mycroft */
77 1.1 mycroft
78 1.1 mycroft /*
79 1.1 mycroft * Originally written by Julian Elischer (julian (at) tfs.com)
80 1.1 mycroft * for TRW Financial Systems for use under the MACH(2.5) operating system.
81 1.1 mycroft *
82 1.1 mycroft * TRW Financial Systems, in accordance with their agreement with Carnegie
83 1.1 mycroft * Mellon University, makes this software available to CMU to distribute
84 1.1 mycroft * or use in any manner that they see fit as long as this message is kept with
85 1.1 mycroft * the software. For this reason TFS also grants any other persons or
86 1.1 mycroft * organisations permission to use or modify this software.
87 1.1 mycroft *
88 1.1 mycroft * TFS supplies this software to be publicly redistributed
89 1.1 mycroft * on the understanding that TFS is not responsible for the correct
90 1.1 mycroft * functioning of this software in any circumstances.
91 1.1 mycroft */
92 1.1 mycroft
93 1.1 mycroft #include <sys/types.h>
94 1.1 mycroft #include <sys/param.h>
95 1.1 mycroft #include <sys/systm.h>
96 1.1 mycroft #include <sys/kernel.h>
97 1.1 mycroft #include <sys/errno.h>
98 1.1 mycroft #include <sys/ioctl.h>
99 1.1 mycroft #include <sys/device.h>
100 1.1 mycroft #include <sys/malloc.h>
101 1.1 mycroft #include <sys/buf.h>
102 1.1 mycroft #include <sys/proc.h>
103 1.1 mycroft #include <sys/user.h>
104 1.1 mycroft
105 1.1 mycroft #include <machine/bus.h>
106 1.1 mycroft #include <machine/intr.h>
107 1.1 mycroft
108 1.10 bouyer #include <dev/scsipi/scsi_all.h>
109 1.10 bouyer #include <dev/scsipi/scsipi_all.h>
110 1.10 bouyer #include <dev/scsipi/scsiconf.h>
111 1.1 mycroft
112 1.1 mycroft #include <dev/eisa/eisareg.h>
113 1.1 mycroft #include <dev/eisa/eisavar.h>
114 1.1 mycroft #include <dev/eisa/eisadevs.h>
115 1.1 mycroft #include <dev/eisa/ahbreg.h>
116 1.1 mycroft
117 1.1 mycroft #ifndef DDB
118 1.1 mycroft #define Debugger() panic("should call debugger here (aha1742.c)")
119 1.1 mycroft #endif /* ! DDB */
120 1.1 mycroft
121 1.1 mycroft #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
122 1.1 mycroft #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
123 1.1 mycroft #define ECB_HASH_SHIFT 9
124 1.1 mycroft #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
125 1.1 mycroft
126 1.9 thorpej #define AHB_MAXXFER ((AHB_NSEG - 1) << PGSHIFT)
127 1.1 mycroft
128 1.1 mycroft struct ahb_softc {
129 1.1 mycroft struct device sc_dev;
130 1.8 mycroft
131 1.6 thorpej bus_space_tag_t sc_iot;
132 1.6 thorpej bus_space_handle_t sc_ioh;
133 1.9 thorpej bus_dma_tag_t sc_dmat;
134 1.1 mycroft void *sc_ih;
135 1.1 mycroft
136 1.18 thorpej bus_dmamap_t sc_dmamap_ecb; /* maps the ecbs */
137 1.18 thorpej struct ahb_ecb *sc_ecbs; /* all our ecbs */
138 1.18 thorpej
139 1.1 mycroft struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
140 1.1 mycroft TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
141 1.1 mycroft struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
142 1.1 mycroft int sc_numecbs;
143 1.10 bouyer struct scsipi_link sc_link;
144 1.12 thorpej
145 1.12 thorpej LIST_HEAD(, scsipi_xfer) sc_queue;
146 1.12 thorpej struct scsipi_xfer *sc_queuelast;
147 1.1 mycroft };
148 1.1 mycroft
149 1.18 thorpej /*
150 1.18 thorpej * Offset of an ECB from the beginning of the ECB DMA mapping.
151 1.18 thorpej */
152 1.18 thorpej #define AHB_ECB_OFF(e) (((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
153 1.18 thorpej
154 1.8 mycroft struct ahb_probe_data {
155 1.8 mycroft int sc_irq;
156 1.8 mycroft int sc_scsi_dev;
157 1.8 mycroft };
158 1.8 mycroft
159 1.8 mycroft void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
160 1.8 mycroft void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
161 1.8 mycroft int ahbintr __P((void *));
162 1.8 mycroft void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
163 1.8 mycroft struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
164 1.8 mycroft struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
165 1.8 mycroft void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
166 1.8 mycroft int ahb_find __P((bus_space_tag_t, bus_space_handle_t, struct ahb_probe_data *));
167 1.18 thorpej int ahb_init __P((struct ahb_softc *));
168 1.8 mycroft void ahbminphys __P((struct buf *));
169 1.10 bouyer int ahb_scsi_cmd __P((struct scsipi_xfer *));
170 1.10 bouyer int ahb_poll __P((struct ahb_softc *, struct scsipi_xfer *, int));
171 1.8 mycroft void ahb_timeout __P((void *));
172 1.18 thorpej int ahb_create_ecbs __P((struct ahb_softc *, struct ahb_ecb *, int));
173 1.12 thorpej void ahb_enqueue __P((struct ahb_softc *, struct scsipi_xfer *, int));
174 1.12 thorpej struct scsipi_xfer *ahb_dequeue __P((struct ahb_softc *));
175 1.3 thorpej
176 1.3 thorpej integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
177 1.11 thorpej integrate int ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
178 1.1 mycroft
179 1.10 bouyer struct scsipi_adapter ahb_switch = {
180 1.1 mycroft ahb_scsi_cmd,
181 1.1 mycroft ahbminphys,
182 1.1 mycroft 0,
183 1.1 mycroft 0,
184 1.1 mycroft };
185 1.1 mycroft
186 1.1 mycroft /* the below structure is so we have a default dev struct for our link struct */
187 1.10 bouyer struct scsipi_device ahb_dev = {
188 1.1 mycroft NULL, /* Use default error handler */
189 1.1 mycroft NULL, /* have a queue, served by this */
190 1.1 mycroft NULL, /* have no async handler */
191 1.1 mycroft NULL, /* Use default 'done' routine */
192 1.1 mycroft };
193 1.1 mycroft
194 1.9 thorpej int ahbmatch __P((struct device *, struct cfdata *, void *));
195 1.1 mycroft void ahbattach __P((struct device *, struct device *, void *));
196 1.1 mycroft
197 1.1 mycroft struct cfattach ahb_ca = {
198 1.1 mycroft sizeof(struct ahb_softc), ahbmatch, ahbattach
199 1.1 mycroft };
200 1.1 mycroft
201 1.1 mycroft #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
202 1.9 thorpej
203 1.1 mycroft /*
204 1.1 mycroft * Check the slots looking for a board we recognise
205 1.1 mycroft * If we find one, note it's address (slot) and call
206 1.1 mycroft * the actual probe routine to check it out.
207 1.1 mycroft */
208 1.1 mycroft int
209 1.1 mycroft ahbmatch(parent, match, aux)
210 1.1 mycroft struct device *parent;
211 1.9 thorpej struct cfdata *match;
212 1.9 thorpej void *aux;
213 1.1 mycroft {
214 1.1 mycroft struct eisa_attach_args *ea = aux;
215 1.6 thorpej bus_space_tag_t iot = ea->ea_iot;
216 1.6 thorpej bus_space_handle_t ioh;
217 1.1 mycroft int rv;
218 1.1 mycroft
219 1.1 mycroft /* must match one of our known ID strings */
220 1.1 mycroft if (strcmp(ea->ea_idstring, "ADP0000") &&
221 1.1 mycroft strcmp(ea->ea_idstring, "ADP0001") &&
222 1.1 mycroft strcmp(ea->ea_idstring, "ADP0002") &&
223 1.1 mycroft strcmp(ea->ea_idstring, "ADP0400"))
224 1.1 mycroft return (0);
225 1.1 mycroft
226 1.6 thorpej if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot),
227 1.6 thorpej EISA_SLOT_SIZE, 0, &ioh))
228 1.1 mycroft return (0);
229 1.1 mycroft
230 1.6 thorpej rv = !ahb_find(iot, ioh, NULL);
231 1.1 mycroft
232 1.6 thorpej bus_space_unmap(iot, ioh, EISA_SLOT_SIZE);
233 1.1 mycroft
234 1.1 mycroft return (rv);
235 1.1 mycroft }
236 1.1 mycroft
237 1.1 mycroft /*
238 1.1 mycroft * Attach all the sub-devices we can find
239 1.1 mycroft */
240 1.1 mycroft void
241 1.1 mycroft ahbattach(parent, self, aux)
242 1.1 mycroft struct device *parent, *self;
243 1.1 mycroft void *aux;
244 1.1 mycroft {
245 1.1 mycroft struct eisa_attach_args *ea = aux;
246 1.1 mycroft struct ahb_softc *sc = (void *)self;
247 1.6 thorpej bus_space_tag_t iot = ea->ea_iot;
248 1.6 thorpej bus_space_handle_t ioh;
249 1.1 mycroft eisa_chipset_tag_t ec = ea->ea_ec;
250 1.1 mycroft eisa_intr_handle_t ih;
251 1.1 mycroft const char *model, *intrstr;
252 1.8 mycroft struct ahb_probe_data apd;
253 1.1 mycroft
254 1.1 mycroft if (!strcmp(ea->ea_idstring, "ADP0000"))
255 1.1 mycroft model = EISA_PRODUCT_ADP0000;
256 1.1 mycroft else if (!strcmp(ea->ea_idstring, "ADP0001"))
257 1.1 mycroft model = EISA_PRODUCT_ADP0001;
258 1.1 mycroft else if (!strcmp(ea->ea_idstring, "ADP0002"))
259 1.1 mycroft model = EISA_PRODUCT_ADP0002;
260 1.1 mycroft else if (!strcmp(ea->ea_idstring, "ADP0400"))
261 1.1 mycroft model = EISA_PRODUCT_ADP0400;
262 1.1 mycroft else
263 1.1 mycroft model = "unknown model!";
264 1.5 christos printf(": %s\n", model);
265 1.1 mycroft
266 1.6 thorpej if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot),
267 1.6 thorpej EISA_SLOT_SIZE, 0, &ioh))
268 1.1 mycroft panic("ahbattach: could not map I/O addresses");
269 1.1 mycroft
270 1.6 thorpej sc->sc_iot = iot;
271 1.1 mycroft sc->sc_ioh = ioh;
272 1.9 thorpej sc->sc_dmat = ea->ea_dmat;
273 1.8 mycroft if (ahb_find(iot, ioh, &apd))
274 1.1 mycroft panic("ahbattach: ahb_find failed!");
275 1.1 mycroft
276 1.1 mycroft TAILQ_INIT(&sc->sc_free_ecb);
277 1.12 thorpej LIST_INIT(&sc->sc_queue);
278 1.1 mycroft
279 1.18 thorpej if (ahb_init(sc) != 0) {
280 1.18 thorpej /* Error during initialization! */
281 1.18 thorpej return;
282 1.18 thorpej }
283 1.18 thorpej
284 1.1 mycroft /*
285 1.10 bouyer * fill in the prototype scsipi_link.
286 1.1 mycroft */
287 1.10 bouyer sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
288 1.1 mycroft sc->sc_link.adapter_softc = sc;
289 1.10 bouyer sc->sc_link.scsipi_scsi.adapter_target = apd.sc_scsi_dev;
290 1.1 mycroft sc->sc_link.adapter = &ahb_switch;
291 1.1 mycroft sc->sc_link.device = &ahb_dev;
292 1.1 mycroft sc->sc_link.openings = 4;
293 1.10 bouyer sc->sc_link.scsipi_scsi.max_target = 7;
294 1.10 bouyer sc->sc_link.type = BUS_SCSI;
295 1.1 mycroft
296 1.8 mycroft if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
297 1.5 christos printf("%s: couldn't map interrupt (%d)\n",
298 1.8 mycroft sc->sc_dev.dv_xname, apd.sc_irq);
299 1.1 mycroft return;
300 1.1 mycroft }
301 1.1 mycroft intrstr = eisa_intr_string(ec, ih);
302 1.1 mycroft sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
303 1.1 mycroft ahbintr, sc);
304 1.1 mycroft if (sc->sc_ih == NULL) {
305 1.5 christos printf("%s: couldn't establish interrupt",
306 1.1 mycroft sc->sc_dev.dv_xname);
307 1.1 mycroft if (intrstr != NULL)
308 1.5 christos printf(" at %s", intrstr);
309 1.5 christos printf("\n");
310 1.1 mycroft return;
311 1.1 mycroft }
312 1.1 mycroft if (intrstr != NULL)
313 1.5 christos printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
314 1.1 mycroft intrstr);
315 1.1 mycroft
316 1.1 mycroft /*
317 1.1 mycroft * ask the adapter what subunits are present
318 1.1 mycroft */
319 1.1 mycroft config_found(self, &sc->sc_link, scsiprint);
320 1.1 mycroft }
321 1.1 mycroft
322 1.1 mycroft /*
323 1.12 thorpej * Insert a scsipi_xfer into the software queue. We overload xs->free_list
324 1.12 thorpej * to avoid having to allocate additional resources (since we're used
325 1.12 thorpej * only during resource shortages anyhow.
326 1.12 thorpej */
327 1.12 thorpej void
328 1.12 thorpej ahb_enqueue(sc, xs, infront)
329 1.12 thorpej struct ahb_softc *sc;
330 1.12 thorpej struct scsipi_xfer *xs;
331 1.12 thorpej int infront;
332 1.12 thorpej {
333 1.12 thorpej
334 1.12 thorpej if (infront || sc->sc_queue.lh_first == NULL) {
335 1.12 thorpej if (sc->sc_queue.lh_first == NULL)
336 1.12 thorpej sc->sc_queuelast = xs;
337 1.12 thorpej LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
338 1.12 thorpej return;
339 1.12 thorpej }
340 1.12 thorpej
341 1.12 thorpej LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
342 1.12 thorpej sc->sc_queuelast = xs;
343 1.12 thorpej }
344 1.12 thorpej
345 1.12 thorpej /*
346 1.12 thorpej * Pull a scsipi_xfer off the front of the software queue.
347 1.12 thorpej */
348 1.12 thorpej struct scsipi_xfer *
349 1.12 thorpej ahb_dequeue(sc)
350 1.12 thorpej struct ahb_softc *sc;
351 1.12 thorpej {
352 1.12 thorpej struct scsipi_xfer *xs;
353 1.12 thorpej
354 1.12 thorpej xs = sc->sc_queue.lh_first;
355 1.12 thorpej LIST_REMOVE(xs, free_list);
356 1.12 thorpej
357 1.12 thorpej if (sc->sc_queue.lh_first == NULL)
358 1.12 thorpej sc->sc_queuelast = NULL;
359 1.12 thorpej
360 1.12 thorpej return (xs);
361 1.12 thorpej }
362 1.12 thorpej
363 1.12 thorpej /*
364 1.1 mycroft * Function to send a command out through a mailbox
365 1.1 mycroft */
366 1.1 mycroft void
367 1.1 mycroft ahb_send_mbox(sc, opcode, ecb)
368 1.1 mycroft struct ahb_softc *sc;
369 1.1 mycroft int opcode;
370 1.1 mycroft struct ahb_ecb *ecb;
371 1.1 mycroft {
372 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
373 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
374 1.1 mycroft int wait = 300; /* 1ms should be enough */
375 1.1 mycroft
376 1.1 mycroft while (--wait) {
377 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
378 1.1 mycroft == (G2STAT_MBOX_EMPTY))
379 1.1 mycroft break;
380 1.1 mycroft delay(10);
381 1.1 mycroft }
382 1.1 mycroft if (!wait) {
383 1.5 christos printf("%s: board not responding\n", sc->sc_dev.dv_xname);
384 1.1 mycroft Debugger();
385 1.1 mycroft }
386 1.1 mycroft
387 1.9 thorpej /*
388 1.9 thorpej * don't know if this will work.
389 1.9 thorpej * XXX WHAT DOES THIS COMMENT MEAN?! --thorpej
390 1.9 thorpej */
391 1.9 thorpej bus_space_write_4(iot, ioh, MBOXOUT0,
392 1.18 thorpej sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
393 1.10 bouyer bus_space_write_1(iot, ioh, ATTN, opcode |
394 1.10 bouyer ecb->xs->sc_link->scsipi_scsi.target);
395 1.1 mycroft
396 1.1 mycroft if ((ecb->xs->flags & SCSI_POLL) == 0)
397 1.1 mycroft timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
398 1.1 mycroft }
399 1.1 mycroft
400 1.1 mycroft /*
401 1.1 mycroft * Function to send an immediate type command to the adapter
402 1.1 mycroft */
403 1.1 mycroft void
404 1.1 mycroft ahb_send_immed(sc, cmd, ecb)
405 1.1 mycroft struct ahb_softc *sc;
406 1.1 mycroft u_long cmd;
407 1.1 mycroft struct ahb_ecb *ecb;
408 1.1 mycroft {
409 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
410 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
411 1.1 mycroft int wait = 100; /* 1 ms enough? */
412 1.1 mycroft
413 1.1 mycroft while (--wait) {
414 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
415 1.1 mycroft == (G2STAT_MBOX_EMPTY))
416 1.1 mycroft break;
417 1.1 mycroft delay(10);
418 1.1 mycroft }
419 1.1 mycroft if (!wait) {
420 1.5 christos printf("%s: board not responding\n", sc->sc_dev.dv_xname);
421 1.1 mycroft Debugger();
422 1.1 mycroft }
423 1.1 mycroft
424 1.6 thorpej bus_space_write_4(iot, ioh, MBOXOUT0, cmd); /* don't know this will work */
425 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
426 1.10 bouyer bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
427 1.10 bouyer ecb->xs->sc_link->scsipi_scsi.target);
428 1.1 mycroft
429 1.1 mycroft if ((ecb->xs->flags & SCSI_POLL) == 0)
430 1.1 mycroft timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
431 1.1 mycroft }
432 1.1 mycroft
433 1.1 mycroft /*
434 1.1 mycroft * Catch an interrupt from the adaptor
435 1.1 mycroft */
436 1.1 mycroft int
437 1.1 mycroft ahbintr(arg)
438 1.1 mycroft void *arg;
439 1.1 mycroft {
440 1.1 mycroft struct ahb_softc *sc = arg;
441 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
442 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
443 1.1 mycroft struct ahb_ecb *ecb;
444 1.1 mycroft u_char ahbstat;
445 1.1 mycroft u_long mboxval;
446 1.1 mycroft
447 1.1 mycroft #ifdef AHBDEBUG
448 1.5 christos printf("%s: ahbintr ", sc->sc_dev.dv_xname);
449 1.1 mycroft #endif /* AHBDEBUG */
450 1.1 mycroft
451 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
452 1.1 mycroft return 0;
453 1.1 mycroft
454 1.1 mycroft for (;;) {
455 1.1 mycroft /*
456 1.1 mycroft * First get all the information and then
457 1.1 mycroft * acknowlege the interrupt
458 1.1 mycroft */
459 1.6 thorpej ahbstat = bus_space_read_1(iot, ioh, G2INTST);
460 1.6 thorpej mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
461 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
462 1.1 mycroft
463 1.1 mycroft #ifdef AHBDEBUG
464 1.5 christos printf("status = 0x%x ", ahbstat);
465 1.1 mycroft #endif /* AHBDEBUG */
466 1.1 mycroft
467 1.1 mycroft /*
468 1.1 mycroft * Process the completed operation
469 1.1 mycroft */
470 1.1 mycroft switch (ahbstat & G2INTST_INT_STAT) {
471 1.1 mycroft case AHB_ECB_OK:
472 1.1 mycroft case AHB_ECB_RECOVERED:
473 1.1 mycroft case AHB_ECB_ERR:
474 1.1 mycroft ecb = ahb_ecb_phys_kv(sc, mboxval);
475 1.1 mycroft if (!ecb) {
476 1.5 christos printf("%s: BAD ECB RETURNED!\n",
477 1.1 mycroft sc->sc_dev.dv_xname);
478 1.1 mycroft goto next; /* whatever it was, it'll timeout */
479 1.1 mycroft }
480 1.1 mycroft break;
481 1.1 mycroft
482 1.1 mycroft case AHB_IMMED_ERR:
483 1.1 mycroft ecb = sc->sc_immed_ecb;
484 1.1 mycroft sc->sc_immed_ecb = 0;
485 1.1 mycroft ecb->flags |= ECB_IMMED_FAIL;
486 1.1 mycroft break;
487 1.1 mycroft
488 1.1 mycroft case AHB_IMMED_OK:
489 1.1 mycroft ecb = sc->sc_immed_ecb;
490 1.1 mycroft sc->sc_immed_ecb = 0;
491 1.1 mycroft break;
492 1.1 mycroft
493 1.1 mycroft default:
494 1.5 christos printf("%s: unexpected interrupt %x\n",
495 1.1 mycroft sc->sc_dev.dv_xname, ahbstat);
496 1.1 mycroft goto next;
497 1.1 mycroft }
498 1.1 mycroft
499 1.1 mycroft untimeout(ahb_timeout, ecb);
500 1.1 mycroft ahb_done(sc, ecb);
501 1.1 mycroft
502 1.1 mycroft next:
503 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
504 1.1 mycroft return 1;
505 1.1 mycroft }
506 1.1 mycroft }
507 1.1 mycroft
508 1.1 mycroft integrate void
509 1.1 mycroft ahb_reset_ecb(sc, ecb)
510 1.1 mycroft struct ahb_softc *sc;
511 1.1 mycroft struct ahb_ecb *ecb;
512 1.1 mycroft {
513 1.1 mycroft
514 1.1 mycroft ecb->flags = 0;
515 1.1 mycroft }
516 1.1 mycroft
517 1.1 mycroft /*
518 1.1 mycroft * A ecb (and hence a mbx-out is put onto the
519 1.1 mycroft * free list.
520 1.1 mycroft */
521 1.1 mycroft void
522 1.1 mycroft ahb_free_ecb(sc, ecb)
523 1.1 mycroft struct ahb_softc *sc;
524 1.1 mycroft struct ahb_ecb *ecb;
525 1.1 mycroft {
526 1.1 mycroft int s;
527 1.1 mycroft
528 1.1 mycroft s = splbio();
529 1.1 mycroft
530 1.1 mycroft ahb_reset_ecb(sc, ecb);
531 1.1 mycroft TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
532 1.1 mycroft
533 1.1 mycroft /*
534 1.1 mycroft * If there were none, wake anybody waiting for one to come free,
535 1.1 mycroft * starting with queued entries.
536 1.1 mycroft */
537 1.1 mycroft if (ecb->chain.tqe_next == 0)
538 1.1 mycroft wakeup(&sc->sc_free_ecb);
539 1.1 mycroft
540 1.1 mycroft splx(s);
541 1.1 mycroft }
542 1.1 mycroft
543 1.9 thorpej /*
544 1.9 thorpej * Create a set of ecbs and add them to the free list.
545 1.9 thorpej */
546 1.11 thorpej integrate int
547 1.1 mycroft ahb_init_ecb(sc, ecb)
548 1.1 mycroft struct ahb_softc *sc;
549 1.1 mycroft struct ahb_ecb *ecb;
550 1.1 mycroft {
551 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
552 1.11 thorpej int hashnum, error;
553 1.1 mycroft
554 1.9 thorpej /*
555 1.18 thorpej * Create the DMA map for this ECB.
556 1.9 thorpej */
557 1.11 thorpej error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
558 1.11 thorpej 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
559 1.11 thorpej if (error) {
560 1.11 thorpej printf("%s: can't create ecb dmamap_xfer\n",
561 1.11 thorpej sc->sc_dev.dv_xname);
562 1.11 thorpej return (error);
563 1.11 thorpej }
564 1.9 thorpej
565 1.9 thorpej /*
566 1.1 mycroft * put in the phystokv hash table
567 1.1 mycroft * Never gets taken out.
568 1.1 mycroft */
569 1.18 thorpej ecb->hashkey = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
570 1.18 thorpej AHB_ECB_OFF(ecb);
571 1.1 mycroft hashnum = ECB_HASH(ecb->hashkey);
572 1.1 mycroft ecb->nexthash = sc->sc_ecbhash[hashnum];
573 1.1 mycroft sc->sc_ecbhash[hashnum] = ecb;
574 1.1 mycroft ahb_reset_ecb(sc, ecb);
575 1.11 thorpej return (0);
576 1.1 mycroft }
577 1.1 mycroft
578 1.9 thorpej int
579 1.18 thorpej ahb_create_ecbs(sc, ecbstore, count)
580 1.9 thorpej struct ahb_softc *sc;
581 1.18 thorpej struct ahb_ecb *ecbstore;
582 1.18 thorpej int count;
583 1.9 thorpej {
584 1.9 thorpej struct ahb_ecb *ecb;
585 1.18 thorpej int i, error;
586 1.9 thorpej
587 1.18 thorpej bzero(ecbstore, sizeof(struct ahb_ecb) * count);
588 1.18 thorpej for (i = 0; i < count; i++) {
589 1.18 thorpej ecb = &ecbstore[i];
590 1.18 thorpej if ((error = ahb_init_ecb(sc, ecb)) != 0) {
591 1.18 thorpej printf("%s: unable to initialize ecb, error = %d\n",
592 1.18 thorpej sc->sc_dev.dv_xname, error);
593 1.18 thorpej goto out;
594 1.11 thorpej }
595 1.9 thorpej TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
596 1.9 thorpej }
597 1.18 thorpej out:
598 1.18 thorpej return (i);
599 1.9 thorpej }
600 1.9 thorpej
601 1.1 mycroft /*
602 1.1 mycroft * Get a free ecb
603 1.1 mycroft *
604 1.1 mycroft * If there are none, see if we can allocate a new one. If so, put it in the
605 1.1 mycroft * hash table too otherwise either return an error or sleep.
606 1.1 mycroft */
607 1.1 mycroft struct ahb_ecb *
608 1.1 mycroft ahb_get_ecb(sc, flags)
609 1.1 mycroft struct ahb_softc *sc;
610 1.1 mycroft int flags;
611 1.1 mycroft {
612 1.1 mycroft struct ahb_ecb *ecb;
613 1.1 mycroft int s;
614 1.1 mycroft
615 1.1 mycroft s = splbio();
616 1.1 mycroft
617 1.1 mycroft /*
618 1.1 mycroft * If we can and have to, sleep waiting for one to come free
619 1.1 mycroft * but only if we can't allocate a new one.
620 1.1 mycroft */
621 1.1 mycroft for (;;) {
622 1.1 mycroft ecb = sc->sc_free_ecb.tqh_first;
623 1.1 mycroft if (ecb) {
624 1.1 mycroft TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
625 1.1 mycroft break;
626 1.1 mycroft }
627 1.1 mycroft if ((flags & SCSI_NOSLEEP) != 0)
628 1.1 mycroft goto out;
629 1.1 mycroft tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
630 1.1 mycroft }
631 1.1 mycroft
632 1.1 mycroft ecb->flags |= ECB_ALLOC;
633 1.1 mycroft
634 1.1 mycroft out:
635 1.1 mycroft splx(s);
636 1.1 mycroft return ecb;
637 1.1 mycroft }
638 1.1 mycroft
639 1.1 mycroft /*
640 1.1 mycroft * given a physical address, find the ecb that it corresponds to.
641 1.1 mycroft */
642 1.1 mycroft struct ahb_ecb *
643 1.1 mycroft ahb_ecb_phys_kv(sc, ecb_phys)
644 1.1 mycroft struct ahb_softc *sc;
645 1.1 mycroft physaddr ecb_phys;
646 1.1 mycroft {
647 1.1 mycroft int hashnum = ECB_HASH(ecb_phys);
648 1.1 mycroft struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
649 1.1 mycroft
650 1.1 mycroft while (ecb) {
651 1.1 mycroft if (ecb->hashkey == ecb_phys)
652 1.1 mycroft break;
653 1.1 mycroft ecb = ecb->nexthash;
654 1.1 mycroft }
655 1.1 mycroft return ecb;
656 1.1 mycroft }
657 1.1 mycroft
658 1.1 mycroft /*
659 1.1 mycroft * We have a ecb which has been processed by the adaptor, now we look to see
660 1.1 mycroft * how the operation went.
661 1.1 mycroft */
662 1.1 mycroft void
663 1.1 mycroft ahb_done(sc, ecb)
664 1.1 mycroft struct ahb_softc *sc;
665 1.1 mycroft struct ahb_ecb *ecb;
666 1.1 mycroft {
667 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
668 1.10 bouyer struct scsipi_sense_data *s1, *s2;
669 1.10 bouyer struct scsipi_xfer *xs = ecb->xs;
670 1.1 mycroft
671 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
672 1.9 thorpej
673 1.18 thorpej bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
674 1.18 thorpej AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
675 1.18 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
676 1.18 thorpej
677 1.9 thorpej /*
678 1.9 thorpej * If we were a data transfer, unload the map that described
679 1.9 thorpej * the data buffer.
680 1.9 thorpej */
681 1.9 thorpej if (xs->datalen) {
682 1.17 thorpej bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
683 1.17 thorpej ecb->dmamap_xfer->dm_mapsize,
684 1.9 thorpej (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
685 1.9 thorpej BUS_DMASYNC_POSTWRITE);
686 1.9 thorpej bus_dmamap_unload(dmat, ecb->dmamap_xfer);
687 1.9 thorpej }
688 1.9 thorpej
689 1.1 mycroft /*
690 1.1 mycroft * Otherwise, put the results of the operation
691 1.1 mycroft * into the xfer and call whoever started it
692 1.1 mycroft */
693 1.1 mycroft if ((ecb->flags & ECB_ALLOC) == 0) {
694 1.5 christos printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
695 1.1 mycroft Debugger();
696 1.1 mycroft }
697 1.1 mycroft if (ecb->flags & ECB_IMMED) {
698 1.1 mycroft if (ecb->flags & ECB_IMMED_FAIL)
699 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
700 1.1 mycroft goto done;
701 1.1 mycroft }
702 1.1 mycroft if (xs->error == XS_NOERROR) {
703 1.1 mycroft if (ecb->ecb_status.host_stat != HS_OK) {
704 1.1 mycroft switch (ecb->ecb_status.host_stat) {
705 1.1 mycroft case HS_TIMED_OUT: /* No response */
706 1.1 mycroft xs->error = XS_SELTIMEOUT;
707 1.1 mycroft break;
708 1.1 mycroft default: /* Other scsi protocol messes */
709 1.5 christos printf("%s: host_stat %x\n",
710 1.1 mycroft sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
711 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
712 1.1 mycroft }
713 1.1 mycroft } else if (ecb->ecb_status.target_stat != SCSI_OK) {
714 1.1 mycroft switch (ecb->ecb_status.target_stat) {
715 1.1 mycroft case SCSI_CHECK:
716 1.1 mycroft s1 = &ecb->ecb_sense;
717 1.10 bouyer s2 = &xs->sense.scsi_sense;
718 1.1 mycroft *s2 = *s1;
719 1.1 mycroft xs->error = XS_SENSE;
720 1.1 mycroft break;
721 1.1 mycroft case SCSI_BUSY:
722 1.1 mycroft xs->error = XS_BUSY;
723 1.1 mycroft break;
724 1.1 mycroft default:
725 1.5 christos printf("%s: target_stat %x\n",
726 1.1 mycroft sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
727 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
728 1.1 mycroft }
729 1.1 mycroft } else
730 1.1 mycroft xs->resid = 0;
731 1.1 mycroft }
732 1.1 mycroft done:
733 1.1 mycroft ahb_free_ecb(sc, ecb);
734 1.1 mycroft xs->flags |= ITSDONE;
735 1.10 bouyer scsipi_done(xs);
736 1.12 thorpej
737 1.12 thorpej /*
738 1.12 thorpej * If there are queue entries in the software queue, try to
739 1.12 thorpej * run the first one. We should be more or less guaranteed
740 1.12 thorpej * to succeed, since we just freed an ECB.
741 1.12 thorpej *
742 1.12 thorpej * NOTE: ahb_scsi_cmd() relies on our calling it with
743 1.12 thorpej * the first entry in the queue.
744 1.12 thorpej */
745 1.12 thorpej if ((xs = sc->sc_queue.lh_first) != NULL)
746 1.12 thorpej (void) ahb_scsi_cmd(xs);
747 1.1 mycroft }
748 1.1 mycroft
749 1.1 mycroft /*
750 1.1 mycroft * Start the board, ready for normal operation
751 1.1 mycroft */
752 1.1 mycroft int
753 1.6 thorpej ahb_find(iot, ioh, sc)
754 1.6 thorpej bus_space_tag_t iot;
755 1.6 thorpej bus_space_handle_t ioh;
756 1.8 mycroft struct ahb_probe_data *sc;
757 1.1 mycroft {
758 1.1 mycroft u_char intdef;
759 1.1 mycroft int i, irq, busid;
760 1.1 mycroft int wait = 1000; /* 1 sec enough? */
761 1.1 mycroft
762 1.6 thorpej bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
763 1.1 mycroft
764 1.1 mycroft #define NO_NO 1
765 1.1 mycroft #ifdef NO_NO
766 1.1 mycroft /*
767 1.1 mycroft * reset board, If it doesn't respond, assume
768 1.1 mycroft * that it's not there.. good for the probe
769 1.1 mycroft */
770 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
771 1.1 mycroft delay(1000);
772 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, 0);
773 1.1 mycroft delay(10000);
774 1.1 mycroft while (--wait) {
775 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
776 1.1 mycroft break;
777 1.1 mycroft delay(1000);
778 1.1 mycroft }
779 1.1 mycroft if (!wait) {
780 1.1 mycroft #ifdef AHBDEBUG
781 1.5 christos printf("ahb_find: No answer from aha1742 board\n");
782 1.1 mycroft #endif /* AHBDEBUG */
783 1.1 mycroft return ENXIO;
784 1.1 mycroft }
785 1.6 thorpej i = bus_space_read_1(iot, ioh, MBOXIN0);
786 1.1 mycroft if (i) {
787 1.5 christos printf("self test failed, val = 0x%x\n", i);
788 1.1 mycroft return EIO;
789 1.1 mycroft }
790 1.1 mycroft
791 1.1 mycroft /* Set it again, just to be sure. */
792 1.6 thorpej bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
793 1.1 mycroft #endif
794 1.1 mycroft
795 1.6 thorpej while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
796 1.5 christos printf(".");
797 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
798 1.1 mycroft delay(10000);
799 1.1 mycroft }
800 1.1 mycroft
801 1.6 thorpej intdef = bus_space_read_1(iot, ioh, INTDEF);
802 1.1 mycroft switch (intdef & 0x07) {
803 1.1 mycroft case INT9:
804 1.1 mycroft irq = 9;
805 1.1 mycroft break;
806 1.1 mycroft case INT10:
807 1.1 mycroft irq = 10;
808 1.1 mycroft break;
809 1.1 mycroft case INT11:
810 1.1 mycroft irq = 11;
811 1.1 mycroft break;
812 1.1 mycroft case INT12:
813 1.1 mycroft irq = 12;
814 1.1 mycroft break;
815 1.1 mycroft case INT14:
816 1.1 mycroft irq = 14;
817 1.1 mycroft break;
818 1.1 mycroft case INT15:
819 1.1 mycroft irq = 15;
820 1.1 mycroft break;
821 1.1 mycroft default:
822 1.5 christos printf("illegal int setting %x\n", intdef);
823 1.1 mycroft return EIO;
824 1.1 mycroft }
825 1.1 mycroft
826 1.6 thorpej bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
827 1.1 mycroft
828 1.1 mycroft /* who are we on the scsi bus? */
829 1.6 thorpej busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
830 1.1 mycroft
831 1.8 mycroft /* if we want to return data, do so now */
832 1.8 mycroft if (sc) {
833 1.1 mycroft sc->sc_irq = irq;
834 1.1 mycroft sc->sc_scsi_dev = busid;
835 1.1 mycroft }
836 1.1 mycroft
837 1.1 mycroft /*
838 1.1 mycroft * Note that we are going and return (to probe)
839 1.1 mycroft */
840 1.1 mycroft return 0;
841 1.1 mycroft }
842 1.1 mycroft
843 1.18 thorpej int
844 1.1 mycroft ahb_init(sc)
845 1.1 mycroft struct ahb_softc *sc;
846 1.1 mycroft {
847 1.18 thorpej bus_dma_segment_t seg;
848 1.18 thorpej int i, error, rseg;
849 1.18 thorpej
850 1.18 thorpej #define ECBSIZE (AHB_ECB_MAX * sizeof(struct ahb_ecb))
851 1.18 thorpej
852 1.18 thorpej /*
853 1.18 thorpej * Allocate the ECBs.
854 1.18 thorpej */
855 1.18 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
856 1.18 thorpej NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
857 1.18 thorpej printf("%s: unable to allocate ecbs, error = %d\n",
858 1.18 thorpej sc->sc_dev.dv_xname, error);
859 1.18 thorpej return (error);
860 1.18 thorpej }
861 1.18 thorpej if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
862 1.18 thorpej ECBSIZE, (caddr_t *)&sc->sc_ecbs,
863 1.18 thorpej BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
864 1.18 thorpej printf("%s: unable to map ecbs, error = %d\n",
865 1.18 thorpej sc->sc_dev.dv_xname, error);
866 1.18 thorpej return (error);
867 1.18 thorpej }
868 1.18 thorpej
869 1.18 thorpej /*
870 1.18 thorpej * Create and load the DMA map used for the ecbs.
871 1.18 thorpej */
872 1.18 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
873 1.18 thorpej 1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
874 1.18 thorpej printf("%s: unable to create ecb DMA map, error = %d\n",
875 1.18 thorpej sc->sc_dev.dv_xname, error);
876 1.18 thorpej return (error);
877 1.18 thorpej }
878 1.18 thorpej if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
879 1.18 thorpej sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
880 1.18 thorpej printf("%s: unable to load ecb DMA map, error = %d\n",
881 1.18 thorpej sc->sc_dev.dv_xname, error);
882 1.18 thorpej return (error);
883 1.18 thorpej }
884 1.1 mycroft
885 1.18 thorpej #undef ECBSIZE
886 1.18 thorpej
887 1.18 thorpej /*
888 1.18 thorpej * Initialize the ecbs.
889 1.18 thorpej */
890 1.18 thorpej i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
891 1.18 thorpej if (i == 0) {
892 1.18 thorpej printf("%s: unable to create ecbs\n",
893 1.18 thorpej sc->sc_dev.dv_xname);
894 1.18 thorpej return (ENOMEM);
895 1.18 thorpej } else if (i != AHB_ECB_MAX) {
896 1.18 thorpej printf("%s: WARNING: only %d of %d ecbs created\n",
897 1.18 thorpej sc->sc_dev.dv_xname, i, AHB_ECB_MAX);
898 1.18 thorpej }
899 1.18 thorpej
900 1.18 thorpej return (0);
901 1.1 mycroft }
902 1.1 mycroft
903 1.1 mycroft void
904 1.1 mycroft ahbminphys(bp)
905 1.1 mycroft struct buf *bp;
906 1.1 mycroft {
907 1.1 mycroft
908 1.9 thorpej if (bp->b_bcount > AHB_MAXXFER)
909 1.9 thorpej bp->b_bcount = AHB_MAXXFER;
910 1.1 mycroft minphys(bp);
911 1.1 mycroft }
912 1.1 mycroft
913 1.1 mycroft /*
914 1.1 mycroft * start a scsi operation given the command and the data address. Also needs
915 1.1 mycroft * the unit, target and lu.
916 1.1 mycroft */
917 1.1 mycroft int
918 1.1 mycroft ahb_scsi_cmd(xs)
919 1.10 bouyer struct scsipi_xfer *xs;
920 1.1 mycroft {
921 1.10 bouyer struct scsipi_link *sc_link = xs->sc_link;
922 1.1 mycroft struct ahb_softc *sc = sc_link->adapter_softc;
923 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
924 1.1 mycroft struct ahb_ecb *ecb;
925 1.9 thorpej int error, seg, flags, s;
926 1.12 thorpej int fromqueue = 0, dontqueue = 0;
927 1.1 mycroft
928 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
929 1.9 thorpej
930 1.12 thorpej s = splbio(); /* protect the queue */
931 1.12 thorpej
932 1.12 thorpej /*
933 1.12 thorpej * If we're running the queue from ahb_done(), we've been
934 1.12 thorpej * called with the first queue entry as our argument.
935 1.12 thorpej */
936 1.12 thorpej if (xs == sc->sc_queue.lh_first) {
937 1.12 thorpej xs = ahb_dequeue(sc);
938 1.12 thorpej fromqueue = 1;
939 1.12 thorpej goto get_ecb;
940 1.12 thorpej }
941 1.12 thorpej
942 1.12 thorpej /* Polled requests can't be queued for later. */
943 1.12 thorpej dontqueue = xs->flags & SCSI_POLL;
944 1.12 thorpej
945 1.12 thorpej /*
946 1.12 thorpej * If there are jobs in the queue, run them first.
947 1.12 thorpej */
948 1.12 thorpej if (sc->sc_queue.lh_first != NULL) {
949 1.12 thorpej /*
950 1.12 thorpej * If we can't queue, we have to abort, since
951 1.12 thorpej * we have to preserve order.
952 1.12 thorpej */
953 1.12 thorpej if (dontqueue) {
954 1.12 thorpej splx(s);
955 1.12 thorpej xs->error = XS_DRIVER_STUFFUP;
956 1.12 thorpej return (TRY_AGAIN_LATER);
957 1.12 thorpej }
958 1.12 thorpej
959 1.12 thorpej /*
960 1.12 thorpej * Swap with the first queue entry.
961 1.12 thorpej */
962 1.12 thorpej ahb_enqueue(sc, xs, 0);
963 1.12 thorpej xs = ahb_dequeue(sc);
964 1.12 thorpej fromqueue = 1;
965 1.12 thorpej }
966 1.12 thorpej
967 1.12 thorpej get_ecb:
968 1.1 mycroft /*
969 1.1 mycroft * get a ecb (mbox-out) to use. If the transfer
970 1.1 mycroft * is from a buf (possibly from interrupt time)
971 1.1 mycroft * then we can't allow it to sleep
972 1.1 mycroft */
973 1.1 mycroft flags = xs->flags;
974 1.1 mycroft if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
975 1.12 thorpej /*
976 1.12 thorpej * If we can't queue, we lose.
977 1.12 thorpej */
978 1.12 thorpej if (dontqueue) {
979 1.12 thorpej splx(s);
980 1.12 thorpej xs->error = XS_DRIVER_STUFFUP;
981 1.12 thorpej return (TRY_AGAIN_LATER);
982 1.12 thorpej }
983 1.12 thorpej
984 1.12 thorpej /*
985 1.12 thorpej * Stuff ourselves into the queue, in front
986 1.12 thorpej * if we came off in the first place.
987 1.12 thorpej */
988 1.12 thorpej ahb_enqueue(sc, xs, fromqueue);
989 1.12 thorpej splx(s);
990 1.12 thorpej return (SUCCESSFULLY_QUEUED);
991 1.1 mycroft }
992 1.12 thorpej
993 1.12 thorpej splx(s); /* done playing with the queue */
994 1.12 thorpej
995 1.1 mycroft ecb->xs = xs;
996 1.1 mycroft ecb->timeout = xs->timeout;
997 1.1 mycroft
998 1.1 mycroft /*
999 1.1 mycroft * If it's a reset, we need to do an 'immediate'
1000 1.1 mycroft * command, and store its ecb for later
1001 1.1 mycroft * if there is already an immediate waiting,
1002 1.1 mycroft * then WE must wait
1003 1.1 mycroft */
1004 1.1 mycroft if (flags & SCSI_RESET) {
1005 1.1 mycroft ecb->flags |= ECB_IMMED;
1006 1.1 mycroft if (sc->sc_immed_ecb)
1007 1.1 mycroft return TRY_AGAIN_LATER;
1008 1.1 mycroft sc->sc_immed_ecb = ecb;
1009 1.1 mycroft
1010 1.1 mycroft s = splbio();
1011 1.1 mycroft ahb_send_immed(sc, AHB_TARG_RESET, ecb);
1012 1.1 mycroft splx(s);
1013 1.1 mycroft
1014 1.1 mycroft if ((flags & SCSI_POLL) == 0)
1015 1.1 mycroft return SUCCESSFULLY_QUEUED;
1016 1.1 mycroft
1017 1.1 mycroft /*
1018 1.1 mycroft * If we can't use interrupts, poll on completion
1019 1.1 mycroft */
1020 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout))
1021 1.1 mycroft ahb_timeout(ecb);
1022 1.1 mycroft return COMPLETE;
1023 1.1 mycroft }
1024 1.1 mycroft
1025 1.1 mycroft /*
1026 1.1 mycroft * Put all the arguments for the xfer in the ecb
1027 1.1 mycroft */
1028 1.1 mycroft ecb->opcode = ECB_SCSI_OP;
1029 1.1 mycroft ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
1030 1.10 bouyer ecb->opt2 = sc_link->scsipi_scsi.lun | ECB_NRB;
1031 1.2 mycroft bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
1032 1.18 thorpej ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
1033 1.18 thorpej AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
1034 1.1 mycroft ecb->req_sense_length = sizeof(ecb->ecb_sense);
1035 1.18 thorpej ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
1036 1.18 thorpej AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
1037 1.1 mycroft ecb->ecb_status.host_stat = 0x00;
1038 1.1 mycroft ecb->ecb_status.target_stat = 0x00;
1039 1.1 mycroft
1040 1.1 mycroft if (xs->datalen) {
1041 1.9 thorpej /*
1042 1.9 thorpej * Map the DMA transfer.
1043 1.9 thorpej */
1044 1.9 thorpej #ifdef TFS
1045 1.1 mycroft if (flags & SCSI_DATA_UIO) {
1046 1.9 thorpej error = bus_dmamap_load_uio(sc->sc_dmat,
1047 1.9 thorpej ecb->dmamap_xfer, (struct uio *)xs->data,
1048 1.9 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1049 1.9 thorpej BUS_DMA_WAITOK);
1050 1.9 thorpej } else
1051 1.9 thorpej #endif /* TFS */
1052 1.9 thorpej {
1053 1.9 thorpej error = bus_dmamap_load(sc->sc_dmat,
1054 1.9 thorpej ecb->dmamap_xfer, xs->data, xs->datalen, NULL,
1055 1.9 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1056 1.9 thorpej BUS_DMA_WAITOK);
1057 1.1 mycroft }
1058 1.9 thorpej
1059 1.9 thorpej if (error) {
1060 1.9 thorpej if (error == EFBIG) {
1061 1.9 thorpej printf("%s: ahb_scsi_cmd, more than %d"
1062 1.9 thorpej " dma segments\n",
1063 1.9 thorpej sc->sc_dev.dv_xname, AHB_NSEG);
1064 1.9 thorpej } else {
1065 1.9 thorpej printf("%s: ahb_scsi_cmd, error %d loading"
1066 1.9 thorpej " dma map\n",
1067 1.9 thorpej sc->sc_dev.dv_xname, error);
1068 1.1 mycroft }
1069 1.9 thorpej goto bad;
1070 1.1 mycroft }
1071 1.9 thorpej
1072 1.17 thorpej bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
1073 1.17 thorpej ecb->dmamap_xfer->dm_mapsize,
1074 1.9 thorpej (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1075 1.9 thorpej BUS_DMASYNC_PREWRITE);
1076 1.9 thorpej
1077 1.9 thorpej /*
1078 1.9 thorpej * Load the hardware scatter/gather map with the
1079 1.9 thorpej * contents of the DMA map.
1080 1.9 thorpej */
1081 1.9 thorpej for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
1082 1.9 thorpej ecb->ahb_dma[seg].seg_addr =
1083 1.9 thorpej ecb->dmamap_xfer->dm_segs[seg].ds_addr;
1084 1.9 thorpej ecb->ahb_dma[seg].seg_len =
1085 1.9 thorpej ecb->dmamap_xfer->dm_segs[seg].ds_len;
1086 1.1 mycroft }
1087 1.9 thorpej
1088 1.18 thorpej ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
1089 1.18 thorpej AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ahb_dma);
1090 1.9 thorpej ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
1091 1.9 thorpej sizeof(struct ahb_dma_seg);
1092 1.1 mycroft ecb->opt1 |= ECB_S_G;
1093 1.1 mycroft } else { /* No data xfer, use non S/G values */
1094 1.1 mycroft ecb->data_addr = (physaddr)0;
1095 1.1 mycroft ecb->data_length = 0;
1096 1.1 mycroft }
1097 1.1 mycroft ecb->link_addr = (physaddr)0;
1098 1.18 thorpej
1099 1.18 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
1100 1.18 thorpej AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
1101 1.18 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1102 1.1 mycroft
1103 1.1 mycroft s = splbio();
1104 1.1 mycroft ahb_send_mbox(sc, OP_START_ECB, ecb);
1105 1.1 mycroft splx(s);
1106 1.1 mycroft
1107 1.1 mycroft /*
1108 1.1 mycroft * Usually return SUCCESSFULLY QUEUED
1109 1.1 mycroft */
1110 1.1 mycroft if ((flags & SCSI_POLL) == 0)
1111 1.1 mycroft return SUCCESSFULLY_QUEUED;
1112 1.1 mycroft
1113 1.1 mycroft /*
1114 1.1 mycroft * If we can't use interrupts, poll on completion
1115 1.1 mycroft */
1116 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout)) {
1117 1.1 mycroft ahb_timeout(ecb);
1118 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout))
1119 1.1 mycroft ahb_timeout(ecb);
1120 1.1 mycroft }
1121 1.1 mycroft return COMPLETE;
1122 1.1 mycroft
1123 1.1 mycroft bad:
1124 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
1125 1.1 mycroft ahb_free_ecb(sc, ecb);
1126 1.1 mycroft return COMPLETE;
1127 1.1 mycroft }
1128 1.1 mycroft
1129 1.1 mycroft /*
1130 1.1 mycroft * Function to poll for command completion when in poll mode
1131 1.1 mycroft */
1132 1.1 mycroft int
1133 1.1 mycroft ahb_poll(sc, xs, count)
1134 1.1 mycroft struct ahb_softc *sc;
1135 1.10 bouyer struct scsipi_xfer *xs;
1136 1.1 mycroft int count;
1137 1.1 mycroft { /* in msec */
1138 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
1139 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1140 1.1 mycroft
1141 1.1 mycroft while (count) {
1142 1.1 mycroft /*
1143 1.1 mycroft * If we had interrupts enabled, would we
1144 1.1 mycroft * have got an interrupt?
1145 1.1 mycroft */
1146 1.6 thorpej if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
1147 1.1 mycroft ahbintr(sc);
1148 1.1 mycroft if (xs->flags & ITSDONE)
1149 1.1 mycroft return 0;
1150 1.1 mycroft delay(1000);
1151 1.1 mycroft count--;
1152 1.1 mycroft }
1153 1.1 mycroft return 1;
1154 1.1 mycroft }
1155 1.1 mycroft
1156 1.1 mycroft void
1157 1.1 mycroft ahb_timeout(arg)
1158 1.1 mycroft void *arg;
1159 1.1 mycroft {
1160 1.1 mycroft struct ahb_ecb *ecb = arg;
1161 1.10 bouyer struct scsipi_xfer *xs = ecb->xs;
1162 1.10 bouyer struct scsipi_link *sc_link = xs->sc_link;
1163 1.1 mycroft struct ahb_softc *sc = sc_link->adapter_softc;
1164 1.1 mycroft int s;
1165 1.1 mycroft
1166 1.10 bouyer scsi_print_addr(sc_link);
1167 1.5 christos printf("timed out");
1168 1.1 mycroft
1169 1.1 mycroft s = splbio();
1170 1.1 mycroft
1171 1.1 mycroft if (ecb->flags & ECB_IMMED) {
1172 1.5 christos printf("\n");
1173 1.1 mycroft ecb->flags |= ECB_IMMED_FAIL;
1174 1.1 mycroft /* XXX Must reset! */
1175 1.1 mycroft } else
1176 1.1 mycroft
1177 1.1 mycroft /*
1178 1.1 mycroft * If it has been through before, then
1179 1.1 mycroft * a previous abort has failed, don't
1180 1.1 mycroft * try abort again
1181 1.1 mycroft */
1182 1.1 mycroft if (ecb->flags & ECB_ABORT) {
1183 1.1 mycroft /* abort timed out */
1184 1.5 christos printf(" AGAIN\n");
1185 1.1 mycroft /* XXX Must reset! */
1186 1.1 mycroft } else {
1187 1.1 mycroft /* abort the operation that has timed out */
1188 1.5 christos printf("\n");
1189 1.1 mycroft ecb->xs->error = XS_TIMEOUT;
1190 1.1 mycroft ecb->timeout = AHB_ABORT_TIMEOUT;
1191 1.1 mycroft ecb->flags |= ECB_ABORT;
1192 1.1 mycroft ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1193 1.1 mycroft }
1194 1.1 mycroft
1195 1.1 mycroft splx(s);
1196 1.1 mycroft }
1197