ahb.c revision 1.11 1 1.11 thorpej /* $NetBSD: ahb.c,v 1.11 1997/10/29 00:18:21 thorpej Exp $ */
2 1.1 mycroft
3 1.1 mycroft #undef AHBDEBUG
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.9 thorpej /*-
11 1.9 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
12 1.9 thorpej * All rights reserved.
13 1.9 thorpej *
14 1.9 thorpej * This code is derived from software contributed to The NetBSD Foundation
15 1.9 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
16 1.9 thorpej * NASA Ames Research Center.
17 1.9 thorpej *
18 1.9 thorpej * Redistribution and use in source and binary forms, with or without
19 1.9 thorpej * modification, are permitted provided that the following conditions
20 1.9 thorpej * are met:
21 1.9 thorpej * 1. Redistributions of source code must retain the above copyright
22 1.9 thorpej * notice, this list of conditions and the following disclaimer.
23 1.9 thorpej * 2. Redistributions in binary form must reproduce the above copyright
24 1.9 thorpej * notice, this list of conditions and the following disclaimer in the
25 1.9 thorpej * documentation and/or other materials provided with the distribution.
26 1.9 thorpej * 3. All advertising materials mentioning features or use of this software
27 1.9 thorpej * must display the following acknowledgement:
28 1.9 thorpej * This product includes software developed by the NetBSD
29 1.9 thorpej * Foundation, Inc. and its contributors.
30 1.9 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
31 1.9 thorpej * contributors may be used to endorse or promote products derived
32 1.9 thorpej * from this software without specific prior written permission.
33 1.9 thorpej *
34 1.9 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 1.9 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 1.9 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 1.9 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 1.9 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 1.9 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 1.9 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 1.9 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 1.9 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 1.9 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 1.9 thorpej * POSSIBILITY OF SUCH DAMAGE.
45 1.9 thorpej */
46 1.9 thorpej
47 1.1 mycroft /*
48 1.8 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.10 bouyer #include <dev/scsipi/scsi_all.h>
107 1.10 bouyer #include <dev/scsipi/scsipi_all.h>
108 1.10 bouyer #include <dev/scsipi/scsiconf.h>
109 1.1 mycroft
110 1.1 mycroft #include <dev/eisa/eisareg.h>
111 1.1 mycroft #include <dev/eisa/eisavar.h>
112 1.1 mycroft #include <dev/eisa/eisadevs.h>
113 1.1 mycroft #include <dev/eisa/ahbreg.h>
114 1.1 mycroft
115 1.1 mycroft #ifndef DDB
116 1.1 mycroft #define Debugger() panic("should call debugger here (aha1742.c)")
117 1.1 mycroft #endif /* ! DDB */
118 1.1 mycroft
119 1.1 mycroft #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
120 1.1 mycroft #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
121 1.1 mycroft #define ECB_HASH_SHIFT 9
122 1.1 mycroft #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
123 1.1 mycroft
124 1.9 thorpej #define AHB_MAXXFER ((AHB_NSEG - 1) << PGSHIFT)
125 1.1 mycroft
126 1.1 mycroft struct ahb_softc {
127 1.1 mycroft struct device sc_dev;
128 1.8 mycroft
129 1.6 thorpej bus_space_tag_t sc_iot;
130 1.6 thorpej bus_space_handle_t sc_ioh;
131 1.9 thorpej bus_dma_tag_t sc_dmat;
132 1.1 mycroft void *sc_ih;
133 1.1 mycroft
134 1.1 mycroft struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
135 1.1 mycroft TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
136 1.1 mycroft struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
137 1.1 mycroft int sc_numecbs;
138 1.10 bouyer struct scsipi_link sc_link;
139 1.1 mycroft };
140 1.1 mycroft
141 1.8 mycroft struct ahb_probe_data {
142 1.8 mycroft int sc_irq;
143 1.8 mycroft int sc_scsi_dev;
144 1.8 mycroft };
145 1.8 mycroft
146 1.8 mycroft void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
147 1.8 mycroft void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
148 1.8 mycroft int ahbintr __P((void *));
149 1.8 mycroft void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
150 1.8 mycroft struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
151 1.8 mycroft struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
152 1.8 mycroft void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
153 1.8 mycroft int ahb_find __P((bus_space_tag_t, bus_space_handle_t, struct ahb_probe_data *));
154 1.8 mycroft void ahb_init __P((struct ahb_softc *));
155 1.8 mycroft void ahbminphys __P((struct buf *));
156 1.10 bouyer int ahb_scsi_cmd __P((struct scsipi_xfer *));
157 1.10 bouyer int ahb_poll __P((struct ahb_softc *, struct scsipi_xfer *, int));
158 1.8 mycroft void ahb_timeout __P((void *));
159 1.9 thorpej int ahb_create_ecbs __P((struct ahb_softc *));
160 1.3 thorpej
161 1.3 thorpej integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
162 1.11 thorpej integrate int ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
163 1.1 mycroft
164 1.10 bouyer struct scsipi_adapter ahb_switch = {
165 1.1 mycroft ahb_scsi_cmd,
166 1.1 mycroft ahbminphys,
167 1.1 mycroft 0,
168 1.1 mycroft 0,
169 1.1 mycroft };
170 1.1 mycroft
171 1.1 mycroft /* the below structure is so we have a default dev struct for our link struct */
172 1.10 bouyer struct scsipi_device ahb_dev = {
173 1.1 mycroft NULL, /* Use default error handler */
174 1.1 mycroft NULL, /* have a queue, served by this */
175 1.1 mycroft NULL, /* have no async handler */
176 1.1 mycroft NULL, /* Use default 'done' routine */
177 1.1 mycroft };
178 1.1 mycroft
179 1.9 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
180 1.1 mycroft int ahbmatch __P((struct device *, void *, void *));
181 1.9 thorpej #else
182 1.9 thorpej int ahbmatch __P((struct device *, struct cfdata *, void *));
183 1.9 thorpej #endif
184 1.1 mycroft void ahbattach __P((struct device *, struct device *, void *));
185 1.1 mycroft
186 1.1 mycroft struct cfattach ahb_ca = {
187 1.1 mycroft sizeof(struct ahb_softc), ahbmatch, ahbattach
188 1.1 mycroft };
189 1.1 mycroft
190 1.1 mycroft struct cfdriver ahb_cd = {
191 1.1 mycroft NULL, "ahb", DV_DULL
192 1.1 mycroft };
193 1.1 mycroft
194 1.1 mycroft #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
195 1.1 mycroft
196 1.9 thorpej /* XXX Should put this in a better place. */
197 1.9 thorpej #define offsetof(type, member) ((size_t)(&((type *)0)->member))
198 1.9 thorpej
199 1.1 mycroft /*
200 1.1 mycroft * Check the slots looking for a board we recognise
201 1.1 mycroft * If we find one, note it's address (slot) and call
202 1.1 mycroft * the actual probe routine to check it out.
203 1.1 mycroft */
204 1.1 mycroft int
205 1.1 mycroft ahbmatch(parent, match, aux)
206 1.1 mycroft struct device *parent;
207 1.9 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
208 1.9 thorpej void *match;
209 1.9 thorpej #else
210 1.9 thorpej struct cfdata *match;
211 1.9 thorpej #endif
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 ahb_init(sc);
277 1.1 mycroft TAILQ_INIT(&sc->sc_free_ecb);
278 1.1 mycroft
279 1.1 mycroft /*
280 1.10 bouyer * fill in the prototype scsipi_link.
281 1.1 mycroft */
282 1.10 bouyer sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
283 1.1 mycroft sc->sc_link.adapter_softc = sc;
284 1.10 bouyer sc->sc_link.scsipi_scsi.adapter_target = apd.sc_scsi_dev;
285 1.1 mycroft sc->sc_link.adapter = &ahb_switch;
286 1.1 mycroft sc->sc_link.device = &ahb_dev;
287 1.1 mycroft sc->sc_link.openings = 4;
288 1.10 bouyer sc->sc_link.scsipi_scsi.max_target = 7;
289 1.10 bouyer sc->sc_link.type = BUS_SCSI;
290 1.1 mycroft
291 1.8 mycroft if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
292 1.5 christos printf("%s: couldn't map interrupt (%d)\n",
293 1.8 mycroft sc->sc_dev.dv_xname, apd.sc_irq);
294 1.1 mycroft return;
295 1.1 mycroft }
296 1.1 mycroft intrstr = eisa_intr_string(ec, ih);
297 1.1 mycroft sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
298 1.1 mycroft ahbintr, sc);
299 1.1 mycroft if (sc->sc_ih == NULL) {
300 1.5 christos printf("%s: couldn't establish interrupt",
301 1.1 mycroft sc->sc_dev.dv_xname);
302 1.1 mycroft if (intrstr != NULL)
303 1.5 christos printf(" at %s", intrstr);
304 1.5 christos printf("\n");
305 1.1 mycroft return;
306 1.1 mycroft }
307 1.1 mycroft if (intrstr != NULL)
308 1.5 christos printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
309 1.1 mycroft intrstr);
310 1.1 mycroft
311 1.1 mycroft /*
312 1.1 mycroft * ask the adapter what subunits are present
313 1.1 mycroft */
314 1.1 mycroft config_found(self, &sc->sc_link, scsiprint);
315 1.1 mycroft }
316 1.1 mycroft
317 1.1 mycroft /*
318 1.1 mycroft * Function to send a command out through a mailbox
319 1.1 mycroft */
320 1.1 mycroft void
321 1.1 mycroft ahb_send_mbox(sc, opcode, ecb)
322 1.1 mycroft struct ahb_softc *sc;
323 1.1 mycroft int opcode;
324 1.1 mycroft struct ahb_ecb *ecb;
325 1.1 mycroft {
326 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
327 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
328 1.1 mycroft int wait = 300; /* 1ms should be enough */
329 1.1 mycroft
330 1.1 mycroft while (--wait) {
331 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
332 1.1 mycroft == (G2STAT_MBOX_EMPTY))
333 1.1 mycroft break;
334 1.1 mycroft delay(10);
335 1.1 mycroft }
336 1.1 mycroft if (!wait) {
337 1.5 christos printf("%s: board not responding\n", sc->sc_dev.dv_xname);
338 1.1 mycroft Debugger();
339 1.1 mycroft }
340 1.1 mycroft
341 1.9 thorpej /*
342 1.9 thorpej * don't know if this will work.
343 1.9 thorpej * XXX WHAT DOES THIS COMMENT MEAN?! --thorpej
344 1.9 thorpej */
345 1.9 thorpej bus_space_write_4(iot, ioh, MBOXOUT0,
346 1.9 thorpej ecb->dmamap_self->dm_segs[0].ds_addr);
347 1.10 bouyer bus_space_write_1(iot, ioh, ATTN, opcode |
348 1.10 bouyer ecb->xs->sc_link->scsipi_scsi.target);
349 1.1 mycroft
350 1.1 mycroft if ((ecb->xs->flags & SCSI_POLL) == 0)
351 1.1 mycroft timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
352 1.1 mycroft }
353 1.1 mycroft
354 1.1 mycroft /*
355 1.1 mycroft * Function to send an immediate type command to the adapter
356 1.1 mycroft */
357 1.1 mycroft void
358 1.1 mycroft ahb_send_immed(sc, cmd, ecb)
359 1.1 mycroft struct ahb_softc *sc;
360 1.1 mycroft u_long cmd;
361 1.1 mycroft struct ahb_ecb *ecb;
362 1.1 mycroft {
363 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
364 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
365 1.1 mycroft int wait = 100; /* 1 ms enough? */
366 1.1 mycroft
367 1.1 mycroft while (--wait) {
368 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
369 1.1 mycroft == (G2STAT_MBOX_EMPTY))
370 1.1 mycroft break;
371 1.1 mycroft delay(10);
372 1.1 mycroft }
373 1.1 mycroft if (!wait) {
374 1.5 christos printf("%s: board not responding\n", sc->sc_dev.dv_xname);
375 1.1 mycroft Debugger();
376 1.1 mycroft }
377 1.1 mycroft
378 1.6 thorpej bus_space_write_4(iot, ioh, MBOXOUT0, cmd); /* don't know this will work */
379 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
380 1.10 bouyer bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
381 1.10 bouyer ecb->xs->sc_link->scsipi_scsi.target);
382 1.1 mycroft
383 1.1 mycroft if ((ecb->xs->flags & SCSI_POLL) == 0)
384 1.1 mycroft timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
385 1.1 mycroft }
386 1.1 mycroft
387 1.1 mycroft /*
388 1.1 mycroft * Catch an interrupt from the adaptor
389 1.1 mycroft */
390 1.1 mycroft int
391 1.1 mycroft ahbintr(arg)
392 1.1 mycroft void *arg;
393 1.1 mycroft {
394 1.1 mycroft struct ahb_softc *sc = arg;
395 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
396 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
397 1.1 mycroft struct ahb_ecb *ecb;
398 1.1 mycroft u_char ahbstat;
399 1.1 mycroft u_long mboxval;
400 1.1 mycroft
401 1.1 mycroft #ifdef AHBDEBUG
402 1.5 christos printf("%s: ahbintr ", sc->sc_dev.dv_xname);
403 1.1 mycroft #endif /* AHBDEBUG */
404 1.1 mycroft
405 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
406 1.1 mycroft return 0;
407 1.1 mycroft
408 1.1 mycroft for (;;) {
409 1.1 mycroft /*
410 1.1 mycroft * First get all the information and then
411 1.1 mycroft * acknowlege the interrupt
412 1.1 mycroft */
413 1.6 thorpej ahbstat = bus_space_read_1(iot, ioh, G2INTST);
414 1.6 thorpej mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
415 1.6 thorpej bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
416 1.1 mycroft
417 1.1 mycroft #ifdef AHBDEBUG
418 1.5 christos printf("status = 0x%x ", ahbstat);
419 1.1 mycroft #endif /* AHBDEBUG */
420 1.1 mycroft
421 1.1 mycroft /*
422 1.1 mycroft * Process the completed operation
423 1.1 mycroft */
424 1.1 mycroft switch (ahbstat & G2INTST_INT_STAT) {
425 1.1 mycroft case AHB_ECB_OK:
426 1.1 mycroft case AHB_ECB_RECOVERED:
427 1.1 mycroft case AHB_ECB_ERR:
428 1.1 mycroft ecb = ahb_ecb_phys_kv(sc, mboxval);
429 1.1 mycroft if (!ecb) {
430 1.5 christos printf("%s: BAD ECB RETURNED!\n",
431 1.1 mycroft sc->sc_dev.dv_xname);
432 1.1 mycroft goto next; /* whatever it was, it'll timeout */
433 1.1 mycroft }
434 1.1 mycroft break;
435 1.1 mycroft
436 1.1 mycroft case AHB_IMMED_ERR:
437 1.1 mycroft ecb = sc->sc_immed_ecb;
438 1.1 mycroft sc->sc_immed_ecb = 0;
439 1.1 mycroft ecb->flags |= ECB_IMMED_FAIL;
440 1.1 mycroft break;
441 1.1 mycroft
442 1.1 mycroft case AHB_IMMED_OK:
443 1.1 mycroft ecb = sc->sc_immed_ecb;
444 1.1 mycroft sc->sc_immed_ecb = 0;
445 1.1 mycroft break;
446 1.1 mycroft
447 1.1 mycroft default:
448 1.5 christos printf("%s: unexpected interrupt %x\n",
449 1.1 mycroft sc->sc_dev.dv_xname, ahbstat);
450 1.1 mycroft goto next;
451 1.1 mycroft }
452 1.1 mycroft
453 1.1 mycroft untimeout(ahb_timeout, ecb);
454 1.1 mycroft ahb_done(sc, ecb);
455 1.1 mycroft
456 1.1 mycroft next:
457 1.6 thorpej if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
458 1.1 mycroft return 1;
459 1.1 mycroft }
460 1.1 mycroft }
461 1.1 mycroft
462 1.1 mycroft integrate void
463 1.1 mycroft ahb_reset_ecb(sc, ecb)
464 1.1 mycroft struct ahb_softc *sc;
465 1.1 mycroft struct ahb_ecb *ecb;
466 1.1 mycroft {
467 1.1 mycroft
468 1.1 mycroft ecb->flags = 0;
469 1.1 mycroft }
470 1.1 mycroft
471 1.1 mycroft /*
472 1.1 mycroft * A ecb (and hence a mbx-out is put onto the
473 1.1 mycroft * free list.
474 1.1 mycroft */
475 1.1 mycroft void
476 1.1 mycroft ahb_free_ecb(sc, ecb)
477 1.1 mycroft struct ahb_softc *sc;
478 1.1 mycroft struct ahb_ecb *ecb;
479 1.1 mycroft {
480 1.1 mycroft int s;
481 1.1 mycroft
482 1.1 mycroft s = splbio();
483 1.1 mycroft
484 1.1 mycroft ahb_reset_ecb(sc, ecb);
485 1.1 mycroft TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
486 1.1 mycroft
487 1.1 mycroft /*
488 1.1 mycroft * If there were none, wake anybody waiting for one to come free,
489 1.1 mycroft * starting with queued entries.
490 1.1 mycroft */
491 1.1 mycroft if (ecb->chain.tqe_next == 0)
492 1.1 mycroft wakeup(&sc->sc_free_ecb);
493 1.1 mycroft
494 1.1 mycroft splx(s);
495 1.1 mycroft }
496 1.1 mycroft
497 1.9 thorpej /*
498 1.9 thorpej * Create a set of ecbs and add them to the free list.
499 1.9 thorpej */
500 1.11 thorpej integrate int
501 1.1 mycroft ahb_init_ecb(sc, ecb)
502 1.1 mycroft struct ahb_softc *sc;
503 1.1 mycroft struct ahb_ecb *ecb;
504 1.1 mycroft {
505 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
506 1.11 thorpej int hashnum, error;
507 1.1 mycroft
508 1.9 thorpej /*
509 1.9 thorpej * XXX Should we put a DIAGNOSTIC check for multiple
510 1.9 thorpej * XXX ECB inits here?
511 1.9 thorpej */
512 1.9 thorpej
513 1.1 mycroft bzero(ecb, sizeof(struct ahb_ecb));
514 1.9 thorpej
515 1.9 thorpej /*
516 1.9 thorpej * Create the DMA maps for this ECB.
517 1.9 thorpej */
518 1.11 thorpej error = bus_dmamap_create(dmat, sizeof(struct ahb_ecb), 1,
519 1.11 thorpej sizeof(struct ahb_ecb), 0, BUS_DMA_NOWAIT, &ecb->dmamap_self);
520 1.11 thorpej if (error) {
521 1.11 thorpej printf("%s: can't create ecb dmamap_self\n",
522 1.11 thorpej sc->sc_dev.dv_xname);
523 1.11 thorpej return (error);
524 1.11 thorpej }
525 1.9 thorpej
526 1.11 thorpej error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
527 1.11 thorpej 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
528 1.11 thorpej if (error) {
529 1.11 thorpej printf("%s: can't create ecb dmamap_xfer\n",
530 1.11 thorpej sc->sc_dev.dv_xname);
531 1.11 thorpej return (error);
532 1.11 thorpej }
533 1.9 thorpej
534 1.9 thorpej /*
535 1.9 thorpej * Load the permanent DMA maps.
536 1.9 thorpej */
537 1.11 thorpej error = bus_dmamap_load(dmat, ecb->dmamap_self, ecb,
538 1.11 thorpej sizeof(struct ahb_ecb), NULL, BUS_DMA_NOWAIT);
539 1.11 thorpej if (error) {
540 1.11 thorpej printf("%s: can't load ecb dmamap_self\n",
541 1.11 thorpej sc->sc_dev.dv_xname);
542 1.11 thorpej bus_dmamap_destroy(dmat, ecb->dmamap_self);
543 1.11 thorpej bus_dmamap_destroy(dmat, ecb->dmamap_xfer);
544 1.11 thorpej return (error);
545 1.11 thorpej }
546 1.9 thorpej
547 1.1 mycroft /*
548 1.1 mycroft * put in the phystokv hash table
549 1.1 mycroft * Never gets taken out.
550 1.1 mycroft */
551 1.9 thorpej ecb->hashkey = ecb->dmamap_self->dm_segs[0].ds_addr;
552 1.1 mycroft hashnum = ECB_HASH(ecb->hashkey);
553 1.1 mycroft ecb->nexthash = sc->sc_ecbhash[hashnum];
554 1.1 mycroft sc->sc_ecbhash[hashnum] = ecb;
555 1.1 mycroft ahb_reset_ecb(sc, ecb);
556 1.11 thorpej return (0);
557 1.1 mycroft }
558 1.1 mycroft
559 1.9 thorpej int
560 1.9 thorpej ahb_create_ecbs(sc)
561 1.9 thorpej struct ahb_softc *sc;
562 1.9 thorpej {
563 1.9 thorpej bus_dma_segment_t seg;
564 1.9 thorpej bus_size_t size;
565 1.9 thorpej struct ahb_ecb *ecb;
566 1.9 thorpej int rseg, error;
567 1.9 thorpej
568 1.9 thorpej size = NBPG;
569 1.9 thorpej error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1, &rseg,
570 1.9 thorpej BUS_DMA_NOWAIT);
571 1.11 thorpej if (error) {
572 1.11 thorpej printf("%s: can't allocate memory for ecbs\n",
573 1.11 thorpej sc->sc_dev.dv_xname);
574 1.9 thorpej return (error);
575 1.11 thorpej }
576 1.9 thorpej
577 1.9 thorpej error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
578 1.9 thorpej (caddr_t *)&ecb, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC);
579 1.9 thorpej if (error) {
580 1.11 thorpej printf("%s: can't map memory for ecbs\n",
581 1.11 thorpej sc->sc_dev.dv_xname);
582 1.9 thorpej bus_dmamem_free(sc->sc_dmat, &seg, rseg);
583 1.9 thorpej return (error);
584 1.9 thorpej }
585 1.9 thorpej
586 1.9 thorpej bzero(ecb, size);
587 1.9 thorpej while (size > sizeof(struct ahb_ecb)) {
588 1.11 thorpej error = ahb_init_ecb(sc, ecb);
589 1.11 thorpej if (error) {
590 1.11 thorpej printf("%s: can't initialize ecb\n",
591 1.11 thorpej sc->sc_dev.dv_xname);
592 1.11 thorpej return (error);
593 1.11 thorpej }
594 1.9 thorpej TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
595 1.9 thorpej (caddr_t)ecb += ALIGN(sizeof(struct ahb_ecb));
596 1.9 thorpej size -= ALIGN(sizeof(struct ahb_ecb));
597 1.11 thorpej sc->sc_numecbs++;
598 1.9 thorpej }
599 1.9 thorpej
600 1.9 thorpej return (0);
601 1.9 thorpej }
602 1.9 thorpej
603 1.1 mycroft /*
604 1.1 mycroft * Get a free ecb
605 1.1 mycroft *
606 1.1 mycroft * If there are none, see if we can allocate a new one. If so, put it in the
607 1.1 mycroft * hash table too otherwise either return an error or sleep.
608 1.1 mycroft */
609 1.1 mycroft struct ahb_ecb *
610 1.1 mycroft ahb_get_ecb(sc, flags)
611 1.1 mycroft struct ahb_softc *sc;
612 1.1 mycroft int flags;
613 1.1 mycroft {
614 1.1 mycroft struct ahb_ecb *ecb;
615 1.1 mycroft int s;
616 1.1 mycroft
617 1.1 mycroft s = splbio();
618 1.1 mycroft
619 1.1 mycroft /*
620 1.1 mycroft * If we can and have to, sleep waiting for one to come free
621 1.1 mycroft * but only if we can't allocate a new one.
622 1.1 mycroft */
623 1.1 mycroft for (;;) {
624 1.1 mycroft ecb = sc->sc_free_ecb.tqh_first;
625 1.1 mycroft if (ecb) {
626 1.1 mycroft TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
627 1.1 mycroft break;
628 1.1 mycroft }
629 1.1 mycroft if (sc->sc_numecbs < AHB_ECB_MAX) {
630 1.11 thorpej /*
631 1.11 thorpej * ahb_create_ecbs() might have managed to create
632 1.11 thorpej * one before it failed. If so, don't abort,
633 1.11 thorpej * just grab it and continue to hobble along.
634 1.11 thorpej */
635 1.11 thorpej if (ahb_create_ecbs(sc) != 0 &&
636 1.11 thorpej sc->sc_free_ecb.tqh_first == NULL) {
637 1.9 thorpej printf("%s: can't allocate ecbs\n",
638 1.1 mycroft sc->sc_dev.dv_xname);
639 1.1 mycroft goto out;
640 1.1 mycroft }
641 1.9 thorpej continue;
642 1.1 mycroft }
643 1.1 mycroft if ((flags & SCSI_NOSLEEP) != 0)
644 1.1 mycroft goto out;
645 1.1 mycroft tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
646 1.1 mycroft }
647 1.1 mycroft
648 1.1 mycroft ecb->flags |= ECB_ALLOC;
649 1.1 mycroft
650 1.1 mycroft out:
651 1.1 mycroft splx(s);
652 1.1 mycroft return ecb;
653 1.1 mycroft }
654 1.1 mycroft
655 1.1 mycroft /*
656 1.1 mycroft * given a physical address, find the ecb that it corresponds to.
657 1.1 mycroft */
658 1.1 mycroft struct ahb_ecb *
659 1.1 mycroft ahb_ecb_phys_kv(sc, ecb_phys)
660 1.1 mycroft struct ahb_softc *sc;
661 1.1 mycroft physaddr ecb_phys;
662 1.1 mycroft {
663 1.1 mycroft int hashnum = ECB_HASH(ecb_phys);
664 1.1 mycroft struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
665 1.1 mycroft
666 1.1 mycroft while (ecb) {
667 1.1 mycroft if (ecb->hashkey == ecb_phys)
668 1.1 mycroft break;
669 1.1 mycroft ecb = ecb->nexthash;
670 1.1 mycroft }
671 1.1 mycroft return ecb;
672 1.1 mycroft }
673 1.1 mycroft
674 1.1 mycroft /*
675 1.1 mycroft * We have a ecb which has been processed by the adaptor, now we look to see
676 1.1 mycroft * how the operation went.
677 1.1 mycroft */
678 1.1 mycroft void
679 1.1 mycroft ahb_done(sc, ecb)
680 1.1 mycroft struct ahb_softc *sc;
681 1.1 mycroft struct ahb_ecb *ecb;
682 1.1 mycroft {
683 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
684 1.10 bouyer struct scsipi_sense_data *s1, *s2;
685 1.10 bouyer struct scsipi_xfer *xs = ecb->xs;
686 1.1 mycroft
687 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
688 1.9 thorpej
689 1.9 thorpej /*
690 1.9 thorpej * If we were a data transfer, unload the map that described
691 1.9 thorpej * the data buffer.
692 1.9 thorpej */
693 1.9 thorpej if (xs->datalen) {
694 1.9 thorpej bus_dmamap_sync(dmat, ecb->dmamap_xfer,
695 1.9 thorpej (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
696 1.9 thorpej BUS_DMASYNC_POSTWRITE);
697 1.9 thorpej bus_dmamap_unload(dmat, ecb->dmamap_xfer);
698 1.9 thorpej }
699 1.9 thorpej
700 1.1 mycroft /*
701 1.1 mycroft * Otherwise, put the results of the operation
702 1.1 mycroft * into the xfer and call whoever started it
703 1.1 mycroft */
704 1.1 mycroft if ((ecb->flags & ECB_ALLOC) == 0) {
705 1.5 christos printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
706 1.1 mycroft Debugger();
707 1.1 mycroft }
708 1.1 mycroft if (ecb->flags & ECB_IMMED) {
709 1.1 mycroft if (ecb->flags & ECB_IMMED_FAIL)
710 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
711 1.1 mycroft goto done;
712 1.1 mycroft }
713 1.1 mycroft if (xs->error == XS_NOERROR) {
714 1.1 mycroft if (ecb->ecb_status.host_stat != HS_OK) {
715 1.1 mycroft switch (ecb->ecb_status.host_stat) {
716 1.1 mycroft case HS_TIMED_OUT: /* No response */
717 1.1 mycroft xs->error = XS_SELTIMEOUT;
718 1.1 mycroft break;
719 1.1 mycroft default: /* Other scsi protocol messes */
720 1.5 christos printf("%s: host_stat %x\n",
721 1.1 mycroft sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
722 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
723 1.1 mycroft }
724 1.1 mycroft } else if (ecb->ecb_status.target_stat != SCSI_OK) {
725 1.1 mycroft switch (ecb->ecb_status.target_stat) {
726 1.1 mycroft case SCSI_CHECK:
727 1.1 mycroft s1 = &ecb->ecb_sense;
728 1.10 bouyer s2 = &xs->sense.scsi_sense;
729 1.1 mycroft *s2 = *s1;
730 1.1 mycroft xs->error = XS_SENSE;
731 1.1 mycroft break;
732 1.1 mycroft case SCSI_BUSY:
733 1.1 mycroft xs->error = XS_BUSY;
734 1.1 mycroft break;
735 1.1 mycroft default:
736 1.5 christos printf("%s: target_stat %x\n",
737 1.1 mycroft sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
738 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
739 1.1 mycroft }
740 1.1 mycroft } else
741 1.1 mycroft xs->resid = 0;
742 1.1 mycroft }
743 1.1 mycroft done:
744 1.1 mycroft ahb_free_ecb(sc, ecb);
745 1.1 mycroft xs->flags |= ITSDONE;
746 1.10 bouyer scsipi_done(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.1 mycroft void
844 1.1 mycroft ahb_init(sc)
845 1.1 mycroft struct ahb_softc *sc;
846 1.1 mycroft {
847 1.1 mycroft
848 1.1 mycroft }
849 1.1 mycroft
850 1.1 mycroft void
851 1.1 mycroft ahbminphys(bp)
852 1.1 mycroft struct buf *bp;
853 1.1 mycroft {
854 1.1 mycroft
855 1.9 thorpej if (bp->b_bcount > AHB_MAXXFER)
856 1.9 thorpej bp->b_bcount = AHB_MAXXFER;
857 1.1 mycroft minphys(bp);
858 1.1 mycroft }
859 1.1 mycroft
860 1.1 mycroft /*
861 1.1 mycroft * start a scsi operation given the command and the data address. Also needs
862 1.1 mycroft * the unit, target and lu.
863 1.1 mycroft */
864 1.1 mycroft int
865 1.1 mycroft ahb_scsi_cmd(xs)
866 1.10 bouyer struct scsipi_xfer *xs;
867 1.1 mycroft {
868 1.10 bouyer struct scsipi_link *sc_link = xs->sc_link;
869 1.1 mycroft struct ahb_softc *sc = sc_link->adapter_softc;
870 1.9 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
871 1.1 mycroft struct ahb_ecb *ecb;
872 1.9 thorpej int error, seg, flags, s;
873 1.1 mycroft
874 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
875 1.9 thorpej
876 1.1 mycroft /*
877 1.1 mycroft * get a ecb (mbox-out) to use. If the transfer
878 1.1 mycroft * is from a buf (possibly from interrupt time)
879 1.1 mycroft * then we can't allow it to sleep
880 1.1 mycroft */
881 1.1 mycroft flags = xs->flags;
882 1.1 mycroft if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
883 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
884 1.1 mycroft return TRY_AGAIN_LATER;
885 1.1 mycroft }
886 1.1 mycroft ecb->xs = xs;
887 1.1 mycroft ecb->timeout = xs->timeout;
888 1.1 mycroft
889 1.1 mycroft /*
890 1.1 mycroft * If it's a reset, we need to do an 'immediate'
891 1.1 mycroft * command, and store its ecb for later
892 1.1 mycroft * if there is already an immediate waiting,
893 1.1 mycroft * then WE must wait
894 1.1 mycroft */
895 1.1 mycroft if (flags & SCSI_RESET) {
896 1.1 mycroft ecb->flags |= ECB_IMMED;
897 1.1 mycroft if (sc->sc_immed_ecb)
898 1.1 mycroft return TRY_AGAIN_LATER;
899 1.1 mycroft sc->sc_immed_ecb = ecb;
900 1.1 mycroft
901 1.1 mycroft s = splbio();
902 1.1 mycroft ahb_send_immed(sc, AHB_TARG_RESET, ecb);
903 1.1 mycroft splx(s);
904 1.1 mycroft
905 1.1 mycroft if ((flags & SCSI_POLL) == 0)
906 1.1 mycroft return SUCCESSFULLY_QUEUED;
907 1.1 mycroft
908 1.1 mycroft /*
909 1.1 mycroft * If we can't use interrupts, poll on completion
910 1.1 mycroft */
911 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout))
912 1.1 mycroft ahb_timeout(ecb);
913 1.1 mycroft return COMPLETE;
914 1.1 mycroft }
915 1.1 mycroft
916 1.1 mycroft /*
917 1.1 mycroft * Put all the arguments for the xfer in the ecb
918 1.1 mycroft */
919 1.1 mycroft ecb->opcode = ECB_SCSI_OP;
920 1.1 mycroft ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
921 1.10 bouyer ecb->opt2 = sc_link->scsipi_scsi.lun | ECB_NRB;
922 1.2 mycroft bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
923 1.9 thorpej ecb->sense_ptr = ecb->dmamap_self->dm_segs[0].ds_addr +
924 1.9 thorpej offsetof(struct ahb_ecb, ecb_sense);
925 1.1 mycroft ecb->req_sense_length = sizeof(ecb->ecb_sense);
926 1.9 thorpej ecb->status = ecb->dmamap_self->dm_segs[0].ds_addr +
927 1.9 thorpej offsetof(struct ahb_ecb, ecb_status);
928 1.1 mycroft ecb->ecb_status.host_stat = 0x00;
929 1.1 mycroft ecb->ecb_status.target_stat = 0x00;
930 1.1 mycroft
931 1.1 mycroft if (xs->datalen) {
932 1.9 thorpej /*
933 1.9 thorpej * Map the DMA transfer.
934 1.9 thorpej */
935 1.9 thorpej #ifdef TFS
936 1.1 mycroft if (flags & SCSI_DATA_UIO) {
937 1.9 thorpej error = bus_dmamap_load_uio(sc->sc_dmat,
938 1.9 thorpej ecb->dmamap_xfer, (struct uio *)xs->data,
939 1.9 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
940 1.9 thorpej BUS_DMA_WAITOK);
941 1.9 thorpej } else
942 1.9 thorpej #endif /* TFS */
943 1.9 thorpej {
944 1.9 thorpej error = bus_dmamap_load(sc->sc_dmat,
945 1.9 thorpej ecb->dmamap_xfer, xs->data, xs->datalen, NULL,
946 1.9 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
947 1.9 thorpej BUS_DMA_WAITOK);
948 1.1 mycroft }
949 1.9 thorpej
950 1.9 thorpej if (error) {
951 1.9 thorpej if (error == EFBIG) {
952 1.9 thorpej printf("%s: ahb_scsi_cmd, more than %d"
953 1.9 thorpej " dma segments\n",
954 1.9 thorpej sc->sc_dev.dv_xname, AHB_NSEG);
955 1.9 thorpej } else {
956 1.9 thorpej printf("%s: ahb_scsi_cmd, error %d loading"
957 1.9 thorpej " dma map\n",
958 1.9 thorpej sc->sc_dev.dv_xname, error);
959 1.1 mycroft }
960 1.9 thorpej goto bad;
961 1.1 mycroft }
962 1.9 thorpej
963 1.9 thorpej bus_dmamap_sync(dmat, ecb->dmamap_xfer,
964 1.9 thorpej (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
965 1.9 thorpej BUS_DMASYNC_PREWRITE);
966 1.9 thorpej
967 1.9 thorpej /*
968 1.9 thorpej * Load the hardware scatter/gather map with the
969 1.9 thorpej * contents of the DMA map.
970 1.9 thorpej */
971 1.9 thorpej for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
972 1.9 thorpej ecb->ahb_dma[seg].seg_addr =
973 1.9 thorpej ecb->dmamap_xfer->dm_segs[seg].ds_addr;
974 1.9 thorpej ecb->ahb_dma[seg].seg_len =
975 1.9 thorpej ecb->dmamap_xfer->dm_segs[seg].ds_len;
976 1.1 mycroft }
977 1.9 thorpej
978 1.9 thorpej ecb->data_addr = ecb->dmamap_self->dm_segs[0].ds_addr +
979 1.9 thorpej offsetof(struct ahb_ecb, ahb_dma);
980 1.9 thorpej ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
981 1.9 thorpej sizeof(struct ahb_dma_seg);
982 1.1 mycroft ecb->opt1 |= ECB_S_G;
983 1.1 mycroft } else { /* No data xfer, use non S/G values */
984 1.1 mycroft ecb->data_addr = (physaddr)0;
985 1.1 mycroft ecb->data_length = 0;
986 1.1 mycroft }
987 1.1 mycroft ecb->link_addr = (physaddr)0;
988 1.1 mycroft
989 1.1 mycroft s = splbio();
990 1.1 mycroft ahb_send_mbox(sc, OP_START_ECB, ecb);
991 1.1 mycroft splx(s);
992 1.1 mycroft
993 1.1 mycroft /*
994 1.1 mycroft * Usually return SUCCESSFULLY QUEUED
995 1.1 mycroft */
996 1.1 mycroft if ((flags & SCSI_POLL) == 0)
997 1.1 mycroft return SUCCESSFULLY_QUEUED;
998 1.1 mycroft
999 1.1 mycroft /*
1000 1.1 mycroft * If we can't use interrupts, poll on completion
1001 1.1 mycroft */
1002 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout)) {
1003 1.1 mycroft ahb_timeout(ecb);
1004 1.1 mycroft if (ahb_poll(sc, xs, ecb->timeout))
1005 1.1 mycroft ahb_timeout(ecb);
1006 1.1 mycroft }
1007 1.1 mycroft return COMPLETE;
1008 1.1 mycroft
1009 1.1 mycroft bad:
1010 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
1011 1.1 mycroft ahb_free_ecb(sc, ecb);
1012 1.1 mycroft return COMPLETE;
1013 1.1 mycroft }
1014 1.1 mycroft
1015 1.1 mycroft /*
1016 1.1 mycroft * Function to poll for command completion when in poll mode
1017 1.1 mycroft */
1018 1.1 mycroft int
1019 1.1 mycroft ahb_poll(sc, xs, count)
1020 1.1 mycroft struct ahb_softc *sc;
1021 1.10 bouyer struct scsipi_xfer *xs;
1022 1.1 mycroft int count;
1023 1.1 mycroft { /* in msec */
1024 1.6 thorpej bus_space_tag_t iot = sc->sc_iot;
1025 1.6 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1026 1.1 mycroft
1027 1.1 mycroft while (count) {
1028 1.1 mycroft /*
1029 1.1 mycroft * If we had interrupts enabled, would we
1030 1.1 mycroft * have got an interrupt?
1031 1.1 mycroft */
1032 1.6 thorpej if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
1033 1.1 mycroft ahbintr(sc);
1034 1.1 mycroft if (xs->flags & ITSDONE)
1035 1.1 mycroft return 0;
1036 1.1 mycroft delay(1000);
1037 1.1 mycroft count--;
1038 1.1 mycroft }
1039 1.1 mycroft return 1;
1040 1.1 mycroft }
1041 1.1 mycroft
1042 1.1 mycroft void
1043 1.1 mycroft ahb_timeout(arg)
1044 1.1 mycroft void *arg;
1045 1.1 mycroft {
1046 1.1 mycroft struct ahb_ecb *ecb = arg;
1047 1.10 bouyer struct scsipi_xfer *xs = ecb->xs;
1048 1.10 bouyer struct scsipi_link *sc_link = xs->sc_link;
1049 1.1 mycroft struct ahb_softc *sc = sc_link->adapter_softc;
1050 1.1 mycroft int s;
1051 1.1 mycroft
1052 1.10 bouyer scsi_print_addr(sc_link);
1053 1.5 christos printf("timed out");
1054 1.1 mycroft
1055 1.1 mycroft s = splbio();
1056 1.1 mycroft
1057 1.1 mycroft if (ecb->flags & ECB_IMMED) {
1058 1.5 christos printf("\n");
1059 1.1 mycroft ecb->flags |= ECB_IMMED_FAIL;
1060 1.1 mycroft /* XXX Must reset! */
1061 1.1 mycroft } else
1062 1.1 mycroft
1063 1.1 mycroft /*
1064 1.1 mycroft * If it has been through before, then
1065 1.1 mycroft * a previous abort has failed, don't
1066 1.1 mycroft * try abort again
1067 1.1 mycroft */
1068 1.1 mycroft if (ecb->flags & ECB_ABORT) {
1069 1.1 mycroft /* abort timed out */
1070 1.5 christos printf(" AGAIN\n");
1071 1.1 mycroft /* XXX Must reset! */
1072 1.1 mycroft } else {
1073 1.1 mycroft /* abort the operation that has timed out */
1074 1.5 christos printf("\n");
1075 1.1 mycroft ecb->xs->error = XS_TIMEOUT;
1076 1.1 mycroft ecb->timeout = AHB_ABORT_TIMEOUT;
1077 1.1 mycroft ecb->flags |= ECB_ABORT;
1078 1.1 mycroft ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
1079 1.1 mycroft }
1080 1.1 mycroft
1081 1.1 mycroft splx(s);
1082 1.1 mycroft }
1083