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