uha.c revision 1.19 1 1.19 thorpej /* $NetBSD: uha.c,v 1.19 1998/11/19 21:53:00 thorpej Exp $ */
2 1.1 mycroft
3 1.1 mycroft #undef UHADEBUG
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.8 thorpej /*-
11 1.15 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
12 1.8 thorpej * All rights reserved.
13 1.8 thorpej *
14 1.8 thorpej * This code is derived from software contributed to The NetBSD Foundation
15 1.17 mycroft * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
16 1.17 mycroft * Simulation Facility, NASA Ames Research Center.
17 1.8 thorpej *
18 1.8 thorpej * Redistribution and use in source and binary forms, with or without
19 1.8 thorpej * modification, are permitted provided that the following conditions
20 1.8 thorpej * are met:
21 1.8 thorpej * 1. Redistributions of source code must retain the above copyright
22 1.8 thorpej * notice, this list of conditions and the following disclaimer.
23 1.8 thorpej * 2. Redistributions in binary form must reproduce the above copyright
24 1.8 thorpej * notice, this list of conditions and the following disclaimer in the
25 1.8 thorpej * documentation and/or other materials provided with the distribution.
26 1.8 thorpej * 3. All advertising materials mentioning features or use of this software
27 1.8 thorpej * must display the following acknowledgement:
28 1.8 thorpej * This product includes software developed by the NetBSD
29 1.8 thorpej * Foundation, Inc. and its contributors.
30 1.8 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
31 1.8 thorpej * contributors may be used to endorse or promote products derived
32 1.8 thorpej * from this software without specific prior written permission.
33 1.8 thorpej *
34 1.8 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 1.8 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 1.8 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 1.8 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 1.8 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 1.8 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 1.8 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 1.8 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 1.8 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 1.8 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 1.8 thorpej * POSSIBILITY OF SUCH DAMAGE.
45 1.1 mycroft */
46 1.1 mycroft
47 1.1 mycroft /*
48 1.1 mycroft * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
49 1.1 mycroft * Slight fixes to timeouts to run with the 34F
50 1.1 mycroft * Thanks to Julian Elischer for advice and help with this port.
51 1.1 mycroft *
52 1.1 mycroft * Originally written by Julian Elischer (julian (at) tfs.com)
53 1.1 mycroft * for TRW Financial Systems for use under the MACH(2.5) operating system.
54 1.1 mycroft *
55 1.1 mycroft * TRW Financial Systems, in accordance with their agreement with Carnegie
56 1.1 mycroft * Mellon University, makes this software available to CMU to distribute
57 1.1 mycroft * or use in any manner that they see fit as long as this message is kept with
58 1.1 mycroft * the software. For this reason TFS also grants any other persons or
59 1.1 mycroft * organisations permission to use or modify this software.
60 1.1 mycroft *
61 1.1 mycroft * TFS supplies this software to be publicly redistributed
62 1.1 mycroft * on the understanding that TFS is not responsible for the correct
63 1.1 mycroft * functioning of this software in any circumstances.
64 1.1 mycroft *
65 1.1 mycroft * commenced: Sun Sep 27 18:14:01 PDT 1992
66 1.1 mycroft * slight mod to make work with 34F as well: Wed Jun 2 18:05:48 WST 1993
67 1.1 mycroft */
68 1.1 mycroft
69 1.1 mycroft #include <sys/types.h>
70 1.1 mycroft #include <sys/param.h>
71 1.1 mycroft #include <sys/systm.h>
72 1.1 mycroft #include <sys/kernel.h>
73 1.1 mycroft #include <sys/errno.h>
74 1.1 mycroft #include <sys/ioctl.h>
75 1.1 mycroft #include <sys/device.h>
76 1.1 mycroft #include <sys/malloc.h>
77 1.1 mycroft #include <sys/buf.h>
78 1.1 mycroft #include <sys/proc.h>
79 1.1 mycroft #include <sys/user.h>
80 1.1 mycroft
81 1.1 mycroft #include <machine/bus.h>
82 1.1 mycroft #include <machine/intr.h>
83 1.1 mycroft
84 1.9 bouyer #include <dev/scsipi/scsi_all.h>
85 1.9 bouyer #include <dev/scsipi/scsipi_all.h>
86 1.9 bouyer #include <dev/scsipi/scsiconf.h>
87 1.1 mycroft
88 1.1 mycroft #include <dev/ic/uhareg.h>
89 1.1 mycroft #include <dev/ic/uhavar.h>
90 1.1 mycroft
91 1.1 mycroft #ifndef DDB
92 1.5 jonathan #define Debugger() panic("should call debugger here (uha.c)")
93 1.1 mycroft #endif /* ! DDB */
94 1.1 mycroft
95 1.8 thorpej #define UHA_MAXXFER ((UHA_NSEG - 1) << PGSHIFT)
96 1.1 mycroft
97 1.4 mycroft integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
98 1.1 mycroft void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
99 1.10 thorpej integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
100 1.1 mycroft struct uha_mscp *uha_get_mscp __P((struct uha_softc *, int));
101 1.1 mycroft void uhaminphys __P((struct buf *));
102 1.9 bouyer int uha_scsi_cmd __P((struct scsipi_xfer *));
103 1.16 thorpej int uha_create_mscps __P((struct uha_softc *, struct uha_mscp *, int));
104 1.11 thorpej void uha_enqueue __P((struct uha_softc *, struct scsipi_xfer *, int));
105 1.11 thorpej struct scsipi_xfer *uha_dequeue __P((struct uha_softc *));
106 1.1 mycroft
107 1.1 mycroft /* the below structure is so we have a default dev struct for out link struct */
108 1.9 bouyer struct scsipi_device uha_dev = {
109 1.1 mycroft NULL, /* Use default error handler */
110 1.1 mycroft NULL, /* have a queue, served by this */
111 1.1 mycroft NULL, /* have no async handler */
112 1.1 mycroft NULL, /* Use default 'done' routine */
113 1.1 mycroft };
114 1.1 mycroft
115 1.1 mycroft #define UHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
116 1.8 thorpej
117 1.1 mycroft /*
118 1.11 thorpej * Insert a scsipi_xfer into the software queue. We overload xs->free_list
119 1.11 thorpej * to avoid having to allocate additional resources (since we're used
120 1.11 thorpej * only during resource shortages anyhow.
121 1.11 thorpej */
122 1.11 thorpej void
123 1.11 thorpej uha_enqueue(sc, xs, infront)
124 1.11 thorpej struct uha_softc *sc;
125 1.11 thorpej struct scsipi_xfer *xs;
126 1.11 thorpej int infront;
127 1.11 thorpej {
128 1.11 thorpej
129 1.11 thorpej if (infront || sc->sc_queue.lh_first == NULL) {
130 1.11 thorpej if (sc->sc_queue.lh_first == NULL)
131 1.11 thorpej sc->sc_queuelast = xs;
132 1.11 thorpej LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
133 1.11 thorpej return;
134 1.11 thorpej }
135 1.11 thorpej
136 1.11 thorpej LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
137 1.11 thorpej sc->sc_queuelast = xs;
138 1.11 thorpej }
139 1.11 thorpej
140 1.11 thorpej /*
141 1.11 thorpej * Pull a scsipi_xfer off the front of the software queue.
142 1.11 thorpej */
143 1.11 thorpej struct scsipi_xfer *
144 1.11 thorpej uha_dequeue(sc)
145 1.11 thorpej struct uha_softc *sc;
146 1.11 thorpej {
147 1.11 thorpej struct scsipi_xfer *xs;
148 1.11 thorpej
149 1.11 thorpej xs = sc->sc_queue.lh_first;
150 1.11 thorpej LIST_REMOVE(xs, free_list);
151 1.11 thorpej
152 1.11 thorpej if (sc->sc_queue.lh_first == NULL)
153 1.11 thorpej sc->sc_queuelast = NULL;
154 1.11 thorpej
155 1.11 thorpej return (xs);
156 1.11 thorpej }
157 1.11 thorpej
158 1.11 thorpej /*
159 1.1 mycroft * Attach all the sub-devices we can find
160 1.1 mycroft */
161 1.1 mycroft void
162 1.7 mycroft uha_attach(sc, upd)
163 1.1 mycroft struct uha_softc *sc;
164 1.7 mycroft struct uha_probe_data *upd;
165 1.1 mycroft {
166 1.16 thorpej bus_dma_segment_t seg;
167 1.16 thorpej int i, error, rseg;
168 1.1 mycroft
169 1.8 thorpej TAILQ_INIT(&sc->sc_free_mscp);
170 1.11 thorpej LIST_INIT(&sc->sc_queue);
171 1.8 thorpej
172 1.1 mycroft (sc->init)(sc);
173 1.1 mycroft
174 1.1 mycroft /*
175 1.19 thorpej * Fill in the adapter.
176 1.19 thorpej */
177 1.19 thorpej sc->sc_adapter.scsipi_cmd = uha_scsi_cmd;
178 1.19 thorpej sc->sc_adapter.scsipi_minphys = uhaminphys;
179 1.19 thorpej
180 1.19 thorpej /*
181 1.9 bouyer * fill in the prototype scsipi_link.
182 1.1 mycroft */
183 1.9 bouyer sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
184 1.1 mycroft sc->sc_link.adapter_softc = sc;
185 1.9 bouyer sc->sc_link.scsipi_scsi.adapter_target = upd->sc_scsi_dev;
186 1.19 thorpej sc->sc_link.adapter = &sc->sc_adapter;
187 1.1 mycroft sc->sc_link.device = &uha_dev;
188 1.1 mycroft sc->sc_link.openings = 2;
189 1.9 bouyer sc->sc_link.scsipi_scsi.max_target = 7;
190 1.9 bouyer sc->sc_link.type = BUS_SCSI;
191 1.1 mycroft
192 1.16 thorpej #define MSCPSIZE (UHA_MSCP_MAX * sizeof(struct uha_mscp))
193 1.16 thorpej
194 1.16 thorpej /*
195 1.16 thorpej * Allocate the MSCPs.
196 1.16 thorpej */
197 1.16 thorpej if ((error = bus_dmamem_alloc(sc->sc_dmat, MSCPSIZE,
198 1.16 thorpej NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
199 1.16 thorpej printf("%s: unable to allocate mscps, error = %d\n",
200 1.16 thorpej sc->sc_dev.dv_xname, error);
201 1.16 thorpej return;
202 1.16 thorpej }
203 1.16 thorpej if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
204 1.16 thorpej MSCPSIZE, (caddr_t *)&sc->sc_mscps,
205 1.16 thorpej BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
206 1.16 thorpej printf("%s: unable to map mscps, error = %d\n",
207 1.16 thorpej sc->sc_dev.dv_xname, error);
208 1.16 thorpej return;
209 1.16 thorpej }
210 1.16 thorpej
211 1.16 thorpej /*
212 1.16 thorpej * Create and load the DMA map used for the mscps.
213 1.16 thorpej */
214 1.16 thorpej if ((error = bus_dmamap_create(sc->sc_dmat, MSCPSIZE,
215 1.16 thorpej 1, MSCPSIZE, 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
216 1.16 thorpej &sc->sc_dmamap_mscp)) != 0) {
217 1.16 thorpej printf("%s: unable to create mscp DMA map, error = %d\n",
218 1.16 thorpej sc->sc_dev.dv_xname, error);
219 1.16 thorpej return;
220 1.16 thorpej }
221 1.16 thorpej if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mscp,
222 1.16 thorpej sc->sc_mscps, MSCPSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
223 1.16 thorpej printf("%s: unable to load mscp DMA map, error = %d\n",
224 1.16 thorpej sc->sc_dev.dv_xname, error);
225 1.16 thorpej return;
226 1.16 thorpej }
227 1.16 thorpej
228 1.16 thorpej #undef MSCPSIZE
229 1.16 thorpej
230 1.16 thorpej /*
231 1.16 thorpej * Initialize the mscps.
232 1.16 thorpej */
233 1.16 thorpej i = uha_create_mscps(sc, sc->sc_mscps, UHA_MSCP_MAX);
234 1.16 thorpej if (i == 0) {
235 1.16 thorpej printf("%s: unable to create mscps\n",
236 1.16 thorpej sc->sc_dev.dv_xname);
237 1.16 thorpej return;
238 1.16 thorpej } else if (i != UHA_MSCP_MAX) {
239 1.16 thorpej printf("%s: WARNING: only %d of %d mscps created\n",
240 1.16 thorpej sc->sc_dev.dv_xname, i, UHA_MSCP_MAX);
241 1.16 thorpej }
242 1.16 thorpej
243 1.1 mycroft /*
244 1.1 mycroft * ask the adapter what subunits are present
245 1.1 mycroft */
246 1.1 mycroft config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
247 1.1 mycroft }
248 1.1 mycroft
249 1.1 mycroft integrate void
250 1.1 mycroft uha_reset_mscp(sc, mscp)
251 1.1 mycroft struct uha_softc *sc;
252 1.1 mycroft struct uha_mscp *mscp;
253 1.1 mycroft {
254 1.1 mycroft
255 1.1 mycroft mscp->flags = 0;
256 1.1 mycroft }
257 1.1 mycroft
258 1.1 mycroft /*
259 1.1 mycroft * A mscp (and hence a mbx-out) is put onto the free list.
260 1.1 mycroft */
261 1.1 mycroft void
262 1.1 mycroft uha_free_mscp(sc, mscp)
263 1.1 mycroft struct uha_softc *sc;
264 1.1 mycroft struct uha_mscp *mscp;
265 1.1 mycroft {
266 1.1 mycroft int s;
267 1.1 mycroft
268 1.1 mycroft s = splbio();
269 1.1 mycroft
270 1.1 mycroft uha_reset_mscp(sc, mscp);
271 1.1 mycroft TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
272 1.1 mycroft
273 1.1 mycroft /*
274 1.1 mycroft * If there were none, wake anybody waiting for one to come free,
275 1.1 mycroft * starting with queued entries.
276 1.1 mycroft */
277 1.1 mycroft if (mscp->chain.tqe_next == 0)
278 1.1 mycroft wakeup(&sc->sc_free_mscp);
279 1.1 mycroft
280 1.1 mycroft splx(s);
281 1.1 mycroft }
282 1.1 mycroft
283 1.10 thorpej integrate int
284 1.1 mycroft uha_init_mscp(sc, mscp)
285 1.1 mycroft struct uha_softc *sc;
286 1.1 mycroft struct uha_mscp *mscp;
287 1.1 mycroft {
288 1.8 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
289 1.10 thorpej int hashnum, error;
290 1.1 mycroft
291 1.8 thorpej /*
292 1.16 thorpej * Create the DMA map for this MSCP.
293 1.8 thorpej */
294 1.10 thorpej error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
295 1.8 thorpej 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
296 1.10 thorpej &mscp->dmamap_xfer);
297 1.10 thorpej if (error) {
298 1.16 thorpej printf("%s: can't create mscp DMA map, error = %d\n",
299 1.16 thorpej sc->sc_dev.dv_xname, error);
300 1.10 thorpej return (error);
301 1.10 thorpej }
302 1.8 thorpej
303 1.1 mycroft /*
304 1.1 mycroft * put in the phystokv hash table
305 1.1 mycroft * Never gets taken out.
306 1.1 mycroft */
307 1.16 thorpej mscp->hashkey = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
308 1.16 thorpej UHA_MSCP_OFF(mscp);
309 1.1 mycroft hashnum = MSCP_HASH(mscp->hashkey);
310 1.1 mycroft mscp->nexthash = sc->sc_mscphash[hashnum];
311 1.1 mycroft sc->sc_mscphash[hashnum] = mscp;
312 1.1 mycroft uha_reset_mscp(sc, mscp);
313 1.10 thorpej return (0);
314 1.1 mycroft }
315 1.1 mycroft
316 1.1 mycroft /*
317 1.8 thorpej * Create a set of MSCPs and add them to the free list.
318 1.8 thorpej */
319 1.8 thorpej int
320 1.16 thorpej uha_create_mscps(sc, mscpstore, count)
321 1.8 thorpej struct uha_softc *sc;
322 1.16 thorpej struct uha_mscp *mscpstore;
323 1.16 thorpej int count;
324 1.8 thorpej {
325 1.8 thorpej struct uha_mscp *mscp;
326 1.16 thorpej int i, error;
327 1.8 thorpej
328 1.16 thorpej bzero(mscpstore, sizeof(struct uha_mscp) * count);
329 1.16 thorpej for (i = 0; i < count; i++) {
330 1.16 thorpej mscp = &mscpstore[i];
331 1.16 thorpej if ((error = uha_init_mscp(sc, mscp)) != 0) {
332 1.16 thorpej printf("%s: unable to initialize mscp, error = %d\n",
333 1.16 thorpej sc->sc_dev.dv_xname, error);
334 1.16 thorpej goto out;
335 1.10 thorpej }
336 1.8 thorpej TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
337 1.8 thorpej }
338 1.16 thorpej out:
339 1.16 thorpej return (i);
340 1.8 thorpej }
341 1.8 thorpej
342 1.8 thorpej /*
343 1.1 mycroft * Get a free mscp
344 1.1 mycroft *
345 1.1 mycroft * If there are none, see if we can allocate a new one. If so, put it in the
346 1.1 mycroft * hash table too otherwise either return an error or sleep.
347 1.1 mycroft */
348 1.1 mycroft struct uha_mscp *
349 1.1 mycroft uha_get_mscp(sc, flags)
350 1.1 mycroft struct uha_softc *sc;
351 1.1 mycroft int flags;
352 1.1 mycroft {
353 1.1 mycroft struct uha_mscp *mscp;
354 1.1 mycroft int s;
355 1.1 mycroft
356 1.1 mycroft s = splbio();
357 1.1 mycroft
358 1.1 mycroft /*
359 1.1 mycroft * If we can and have to, sleep waiting for one to come free
360 1.1 mycroft * but only if we can't allocate a new one
361 1.1 mycroft */
362 1.1 mycroft for (;;) {
363 1.1 mycroft mscp = sc->sc_free_mscp.tqh_first;
364 1.1 mycroft if (mscp) {
365 1.1 mycroft TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
366 1.1 mycroft break;
367 1.1 mycroft }
368 1.1 mycroft if ((flags & SCSI_NOSLEEP) != 0)
369 1.1 mycroft goto out;
370 1.1 mycroft tsleep(&sc->sc_free_mscp, PRIBIO, "uhamsc", 0);
371 1.1 mycroft }
372 1.1 mycroft
373 1.1 mycroft mscp->flags |= MSCP_ALLOC;
374 1.1 mycroft
375 1.1 mycroft out:
376 1.1 mycroft splx(s);
377 1.1 mycroft return (mscp);
378 1.1 mycroft }
379 1.1 mycroft
380 1.1 mycroft /*
381 1.1 mycroft * given a physical address, find the mscp that it corresponds to.
382 1.1 mycroft */
383 1.1 mycroft struct uha_mscp *
384 1.1 mycroft uha_mscp_phys_kv(sc, mscp_phys)
385 1.1 mycroft struct uha_softc *sc;
386 1.1 mycroft u_long mscp_phys;
387 1.1 mycroft {
388 1.1 mycroft int hashnum = MSCP_HASH(mscp_phys);
389 1.1 mycroft struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
390 1.1 mycroft
391 1.1 mycroft while (mscp) {
392 1.1 mycroft if (mscp->hashkey == mscp_phys)
393 1.1 mycroft break;
394 1.1 mycroft mscp = mscp->nexthash;
395 1.1 mycroft }
396 1.1 mycroft return (mscp);
397 1.1 mycroft }
398 1.1 mycroft
399 1.1 mycroft /*
400 1.1 mycroft * We have a mscp which has been processed by the adaptor, now we look to see
401 1.1 mycroft * how the operation went.
402 1.1 mycroft */
403 1.1 mycroft void
404 1.1 mycroft uha_done(sc, mscp)
405 1.1 mycroft struct uha_softc *sc;
406 1.1 mycroft struct uha_mscp *mscp;
407 1.1 mycroft {
408 1.8 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
409 1.9 bouyer struct scsipi_sense_data *s1, *s2;
410 1.9 bouyer struct scsipi_xfer *xs = mscp->xs;
411 1.1 mycroft
412 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB2, ("uha_done\n"));
413 1.8 thorpej
414 1.16 thorpej bus_dmamap_sync(dmat, sc->sc_dmamap_mscp,
415 1.16 thorpej UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
416 1.16 thorpej BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
417 1.16 thorpej
418 1.8 thorpej /*
419 1.8 thorpej * If we were a data transfer, unload the map that described
420 1.8 thorpej * the data buffer.
421 1.8 thorpej */
422 1.8 thorpej if (xs->datalen) {
423 1.15 thorpej bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
424 1.15 thorpej mscp->dmamap_xfer->dm_mapsize,
425 1.8 thorpej (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
426 1.8 thorpej BUS_DMASYNC_POSTWRITE);
427 1.8 thorpej bus_dmamap_unload(dmat, mscp->dmamap_xfer);
428 1.8 thorpej }
429 1.8 thorpej
430 1.1 mycroft /*
431 1.1 mycroft * Otherwise, put the results of the operation
432 1.1 mycroft * into the xfer and call whoever started it
433 1.1 mycroft */
434 1.1 mycroft if ((mscp->flags & MSCP_ALLOC) == 0) {
435 1.3 christos printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
436 1.1 mycroft Debugger();
437 1.1 mycroft return;
438 1.1 mycroft }
439 1.1 mycroft if (xs->error == XS_NOERROR) {
440 1.1 mycroft if (mscp->host_stat != UHA_NO_ERR) {
441 1.1 mycroft switch (mscp->host_stat) {
442 1.1 mycroft case UHA_SBUS_TIMEOUT: /* No response */
443 1.1 mycroft xs->error = XS_SELTIMEOUT;
444 1.1 mycroft break;
445 1.1 mycroft default: /* Other scsi protocol messes */
446 1.3 christos printf("%s: host_stat %x\n",
447 1.1 mycroft sc->sc_dev.dv_xname, mscp->host_stat);
448 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
449 1.1 mycroft }
450 1.1 mycroft } else if (mscp->target_stat != SCSI_OK) {
451 1.1 mycroft switch (mscp->target_stat) {
452 1.1 mycroft case SCSI_CHECK:
453 1.1 mycroft s1 = &mscp->mscp_sense;
454 1.9 bouyer s2 = &xs->sense.scsi_sense;
455 1.1 mycroft *s2 = *s1;
456 1.1 mycroft xs->error = XS_SENSE;
457 1.1 mycroft break;
458 1.1 mycroft case SCSI_BUSY:
459 1.1 mycroft xs->error = XS_BUSY;
460 1.1 mycroft break;
461 1.1 mycroft default:
462 1.3 christos printf("%s: target_stat %x\n",
463 1.1 mycroft sc->sc_dev.dv_xname, mscp->target_stat);
464 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
465 1.1 mycroft }
466 1.1 mycroft } else
467 1.1 mycroft xs->resid = 0;
468 1.1 mycroft }
469 1.1 mycroft uha_free_mscp(sc, mscp);
470 1.1 mycroft xs->flags |= ITSDONE;
471 1.9 bouyer scsipi_done(xs);
472 1.11 thorpej
473 1.11 thorpej /*
474 1.11 thorpej * If there are queue entries in the software queue, try to
475 1.11 thorpej * run the first one. We should be more or less guaranteed
476 1.11 thorpej * to succeed, since we just freed an MSCP.
477 1.11 thorpej *
478 1.11 thorpej * NOTE: uha_scsi_cmd() relies on our calling it with
479 1.11 thorpej * the first entry in the queue.
480 1.11 thorpej */
481 1.11 thorpej if ((xs = sc->sc_queue.lh_first) != NULL)
482 1.11 thorpej (void) uha_scsi_cmd(xs);
483 1.1 mycroft }
484 1.1 mycroft
485 1.1 mycroft void
486 1.1 mycroft uhaminphys(bp)
487 1.1 mycroft struct buf *bp;
488 1.1 mycroft {
489 1.1 mycroft
490 1.8 thorpej if (bp->b_bcount > UHA_MAXXFER)
491 1.8 thorpej bp->b_bcount = UHA_MAXXFER;
492 1.1 mycroft minphys(bp);
493 1.1 mycroft }
494 1.1 mycroft
495 1.1 mycroft /*
496 1.1 mycroft * start a scsi operation given the command and the data address. Also
497 1.1 mycroft * needs the unit, target and lu.
498 1.1 mycroft */
499 1.1 mycroft int
500 1.1 mycroft uha_scsi_cmd(xs)
501 1.9 bouyer struct scsipi_xfer *xs;
502 1.1 mycroft {
503 1.9 bouyer struct scsipi_link *sc_link = xs->sc_link;
504 1.1 mycroft struct uha_softc *sc = sc_link->adapter_softc;
505 1.8 thorpej bus_dma_tag_t dmat = sc->sc_dmat;
506 1.1 mycroft struct uha_mscp *mscp;
507 1.1 mycroft struct uha_dma_seg *sg;
508 1.8 thorpej int error, seg, flags, s;
509 1.11 thorpej int fromqueue = 0, dontqueue = 0;
510 1.1 mycroft
511 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("uha_scsi_cmd\n"));
512 1.11 thorpej
513 1.11 thorpej s = splbio(); /* protect the queue */
514 1.11 thorpej
515 1.11 thorpej /*
516 1.11 thorpej * If we're running the queue from bha_done(), we've been
517 1.11 thorpej * called with the first queue entry as our argument.
518 1.11 thorpej */
519 1.11 thorpej if (xs == sc->sc_queue.lh_first) {
520 1.11 thorpej xs = uha_dequeue(sc);
521 1.11 thorpej fromqueue = 1;
522 1.11 thorpej goto get_mscp;
523 1.11 thorpej }
524 1.11 thorpej
525 1.11 thorpej /* Polled requests can't be queued for later. */
526 1.11 thorpej dontqueue = xs->flags & SCSI_POLL;
527 1.11 thorpej
528 1.11 thorpej /*
529 1.11 thorpej * If there are jobs in the queue, run them first.
530 1.11 thorpej */
531 1.11 thorpej if (sc->sc_queue.lh_first != NULL) {
532 1.11 thorpej /*
533 1.11 thorpej * If we can't queue, we have to abort, since
534 1.11 thorpej * we have to preserve order.
535 1.11 thorpej */
536 1.11 thorpej if (dontqueue) {
537 1.11 thorpej splx(s);
538 1.11 thorpej xs->error = XS_DRIVER_STUFFUP;
539 1.11 thorpej return (TRY_AGAIN_LATER);
540 1.11 thorpej }
541 1.11 thorpej
542 1.11 thorpej /*
543 1.11 thorpej * Swap with the first queue entry.
544 1.11 thorpej */
545 1.11 thorpej uha_enqueue(sc, xs, 0);
546 1.11 thorpej xs = uha_dequeue(sc);
547 1.11 thorpej fromqueue = 1;
548 1.11 thorpej }
549 1.11 thorpej
550 1.11 thorpej get_mscp:
551 1.1 mycroft /*
552 1.1 mycroft * get a mscp (mbox-out) to use. If the transfer
553 1.1 mycroft * is from a buf (possibly from interrupt time)
554 1.1 mycroft * then we can't allow it to sleep
555 1.1 mycroft */
556 1.1 mycroft flags = xs->flags;
557 1.1 mycroft if ((mscp = uha_get_mscp(sc, flags)) == NULL) {
558 1.11 thorpej /*
559 1.11 thorpej * If we can't queue, we lose.
560 1.11 thorpej */
561 1.11 thorpej if (dontqueue) {
562 1.11 thorpej splx(s);
563 1.11 thorpej xs->error = XS_DRIVER_STUFFUP;
564 1.11 thorpej return (TRY_AGAIN_LATER);
565 1.11 thorpej }
566 1.11 thorpej
567 1.11 thorpej /*
568 1.11 thorpej * Stuff ourselves into the queue, in front
569 1.11 thorpej * if we came off in the first place.
570 1.11 thorpej */
571 1.11 thorpej uha_enqueue(sc, xs, fromqueue);
572 1.11 thorpej splx(s);
573 1.11 thorpej return (SUCCESSFULLY_QUEUED);
574 1.1 mycroft }
575 1.11 thorpej
576 1.11 thorpej splx(s); /* done playing with the queue */
577 1.11 thorpej
578 1.1 mycroft mscp->xs = xs;
579 1.1 mycroft mscp->timeout = xs->timeout;
580 1.1 mycroft
581 1.1 mycroft /*
582 1.1 mycroft * Put all the arguments for the xfer in the mscp
583 1.1 mycroft */
584 1.1 mycroft if (flags & SCSI_RESET) {
585 1.1 mycroft mscp->opcode = UHA_SDR;
586 1.1 mycroft mscp->ca = 0x01;
587 1.1 mycroft } else {
588 1.1 mycroft mscp->opcode = UHA_TSP;
589 1.1 mycroft /* XXX Not for tapes. */
590 1.1 mycroft mscp->ca = 0x01;
591 1.1 mycroft bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length);
592 1.1 mycroft }
593 1.1 mycroft mscp->xdir = UHA_SDET;
594 1.1 mycroft mscp->dcn = 0x00;
595 1.1 mycroft mscp->chan = 0x00;
596 1.9 bouyer mscp->target = sc_link->scsipi_scsi.target;
597 1.9 bouyer mscp->lun = sc_link->scsipi_scsi.lun;
598 1.1 mycroft mscp->scsi_cmd_length = xs->cmdlen;
599 1.16 thorpej mscp->sense_ptr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
600 1.16 thorpej UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, mscp_sense);
601 1.1 mycroft mscp->req_sense_length = sizeof(mscp->mscp_sense);
602 1.1 mycroft mscp->host_stat = 0x00;
603 1.1 mycroft mscp->target_stat = 0x00;
604 1.1 mycroft
605 1.1 mycroft if (xs->datalen) {
606 1.1 mycroft sg = mscp->uha_dma;
607 1.1 mycroft seg = 0;
608 1.1 mycroft #ifdef TFS
609 1.1 mycroft if (flags & SCSI_DATA_UIO) {
610 1.8 thorpej error = bus_dmamap_load_uio(dmat,
611 1.8 thorpej mscp->dmamap_xfer, (struct uio *)xs->data,
612 1.8 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
613 1.8 thorpej BUS_DMA_WAITOK);
614 1.1 mycroft } else
615 1.1 mycroft #endif /*TFS */
616 1.1 mycroft {
617 1.8 thorpej error = bus_dmamap_load(dmat,
618 1.8 thorpej mscp->dmamap_xfer, xs->data, xs->datalen, NULL,
619 1.8 thorpej (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
620 1.8 thorpej BUS_DMA_WAITOK);
621 1.8 thorpej }
622 1.8 thorpej
623 1.8 thorpej if (error) {
624 1.8 thorpej if (error == EFBIG) {
625 1.8 thorpej printf("%s: uha_scsi_cmd, more than %d"
626 1.8 thorpej " dma segments\n",
627 1.8 thorpej sc->sc_dev.dv_xname, UHA_NSEG);
628 1.8 thorpej } else {
629 1.8 thorpej printf("%s: uha_scsi_cmd, error %d loading"
630 1.8 thorpej " dma map\n",
631 1.8 thorpej sc->sc_dev.dv_xname, error);
632 1.1 mycroft }
633 1.8 thorpej goto bad;
634 1.1 mycroft }
635 1.8 thorpej
636 1.15 thorpej bus_dmamap_sync(dmat, mscp->dmamap_xfer, 0,
637 1.15 thorpej mscp->dmamap_xfer->dm_mapsize,
638 1.8 thorpej (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
639 1.8 thorpej BUS_DMASYNC_PREWRITE);
640 1.8 thorpej
641 1.8 thorpej /*
642 1.8 thorpej * Load the hardware scatter/gather map with the
643 1.8 thorpej * contents of the DMA map.
644 1.8 thorpej */
645 1.8 thorpej for (seg = 0; seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
646 1.8 thorpej mscp->uha_dma[seg].seg_addr =
647 1.8 thorpej mscp->dmamap_xfer->dm_segs[seg].ds_addr;
648 1.8 thorpej mscp->uha_dma[seg].seg_len =
649 1.8 thorpej mscp->dmamap_xfer->dm_segs[seg].ds_len;
650 1.1 mycroft }
651 1.8 thorpej
652 1.16 thorpej mscp->data_addr = sc->sc_dmamap_mscp->dm_segs[0].ds_addr +
653 1.16 thorpej UHA_MSCP_OFF(mscp) + offsetof(struct uha_mscp, uha_dma);
654 1.1 mycroft mscp->data_length = xs->datalen;
655 1.1 mycroft mscp->sgth = 0x01;
656 1.1 mycroft mscp->sg_num = seg;
657 1.1 mycroft } else { /* No data xfer, use non S/G values */
658 1.1 mycroft mscp->data_addr = (physaddr)0;
659 1.1 mycroft mscp->data_length = 0;
660 1.1 mycroft mscp->sgth = 0x00;
661 1.1 mycroft mscp->sg_num = 0;
662 1.1 mycroft }
663 1.1 mycroft mscp->link_id = 0;
664 1.1 mycroft mscp->link_addr = (physaddr)0;
665 1.16 thorpej
666 1.16 thorpej bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_mscp,
667 1.16 thorpej UHA_MSCP_OFF(mscp), sizeof(struct uha_mscp),
668 1.16 thorpej BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
669 1.1 mycroft
670 1.1 mycroft s = splbio();
671 1.1 mycroft (sc->start_mbox)(sc, mscp);
672 1.1 mycroft splx(s);
673 1.1 mycroft
674 1.1 mycroft /*
675 1.1 mycroft * Usually return SUCCESSFULLY QUEUED
676 1.1 mycroft */
677 1.1 mycroft if ((flags & SCSI_POLL) == 0)
678 1.1 mycroft return (SUCCESSFULLY_QUEUED);
679 1.1 mycroft
680 1.1 mycroft /*
681 1.1 mycroft * If we can't use interrupts, poll on completion
682 1.1 mycroft */
683 1.1 mycroft if ((sc->poll)(sc, xs, mscp->timeout)) {
684 1.1 mycroft uha_timeout(mscp);
685 1.1 mycroft if ((sc->poll)(sc, xs, mscp->timeout))
686 1.1 mycroft uha_timeout(mscp);
687 1.1 mycroft }
688 1.1 mycroft return (COMPLETE);
689 1.1 mycroft
690 1.1 mycroft bad:
691 1.1 mycroft xs->error = XS_DRIVER_STUFFUP;
692 1.1 mycroft uha_free_mscp(sc, mscp);
693 1.1 mycroft return (COMPLETE);
694 1.1 mycroft }
695 1.1 mycroft
696 1.1 mycroft void
697 1.1 mycroft uha_timeout(arg)
698 1.1 mycroft void *arg;
699 1.1 mycroft {
700 1.1 mycroft struct uha_mscp *mscp = arg;
701 1.9 bouyer struct scsipi_xfer *xs = mscp->xs;
702 1.9 bouyer struct scsipi_link *sc_link = xs->sc_link;
703 1.1 mycroft struct uha_softc *sc = sc_link->adapter_softc;
704 1.1 mycroft int s;
705 1.1 mycroft
706 1.9 bouyer scsi_print_addr(sc_link);
707 1.3 christos printf("timed out");
708 1.1 mycroft
709 1.1 mycroft s = splbio();
710 1.1 mycroft
711 1.1 mycroft if (mscp->flags & MSCP_ABORT) {
712 1.1 mycroft /* abort timed out */
713 1.3 christos printf(" AGAIN\n");
714 1.1 mycroft /* XXX Must reset! */
715 1.1 mycroft } else {
716 1.1 mycroft /* abort the operation that has timed out */
717 1.3 christos printf("\n");
718 1.1 mycroft mscp->xs->error = XS_TIMEOUT;
719 1.1 mycroft mscp->timeout = UHA_ABORT_TIMEOUT;
720 1.1 mycroft mscp->flags |= MSCP_ABORT;
721 1.1 mycroft (sc->start_mbox)(sc, mscp);
722 1.1 mycroft }
723 1.1 mycroft
724 1.1 mycroft splx(s);
725 1.1 mycroft }
726