adv.c revision 1.20.2.4 1 1.20.2.4 nathanw /* $NetBSD: adv.c,v 1.20.2.4 2001/11/14 19:14:11 nathanw Exp $ */
2 1.2 dante
3 1.1 dante /*
4 1.4 dante * Generic driver for the Advanced Systems Inc. Narrow SCSI controllers
5 1.1 dante *
6 1.1 dante * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 1.1 dante * All rights reserved.
8 1.1 dante *
9 1.1 dante * Author: Baldassare Dante Profeta <dante (at) mclink.it>
10 1.1 dante *
11 1.1 dante * Redistribution and use in source and binary forms, with or without
12 1.1 dante * modification, are permitted provided that the following conditions
13 1.1 dante * are met:
14 1.1 dante * 1. Redistributions of source code must retain the above copyright
15 1.1 dante * notice, this list of conditions and the following disclaimer.
16 1.1 dante * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 dante * notice, this list of conditions and the following disclaimer in the
18 1.1 dante * documentation and/or other materials provided with the distribution.
19 1.1 dante * 3. All advertising materials mentioning features or use of this software
20 1.1 dante * must display the following acknowledgement:
21 1.4 dante * This product includes software developed by the NetBSD
22 1.4 dante * Foundation, Inc. and its contributors.
23 1.1 dante * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 dante * contributors may be used to endorse or promote products derived
25 1.1 dante * from this software without specific prior written permission.
26 1.1 dante *
27 1.1 dante * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 dante * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 dante * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 dante * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 dante * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 dante * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 dante * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 dante * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 dante * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 dante * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 dante * POSSIBILITY OF SUCH DAMAGE.
38 1.1 dante */
39 1.20.2.4 nathanw
40 1.20.2.4 nathanw #include <sys/cdefs.h>
41 1.20.2.4 nathanw __KERNEL_RCSID(0, "$NetBSD: adv.c,v 1.20.2.4 2001/11/14 19:14:11 nathanw Exp $");
42 1.1 dante
43 1.1 dante #include <sys/types.h>
44 1.1 dante #include <sys/param.h>
45 1.1 dante #include <sys/systm.h>
46 1.16 thorpej #include <sys/callout.h>
47 1.1 dante #include <sys/kernel.h>
48 1.1 dante #include <sys/errno.h>
49 1.1 dante #include <sys/ioctl.h>
50 1.1 dante #include <sys/device.h>
51 1.1 dante #include <sys/malloc.h>
52 1.1 dante #include <sys/buf.h>
53 1.1 dante #include <sys/proc.h>
54 1.1 dante #include <sys/user.h>
55 1.1 dante
56 1.1 dante #include <machine/bus.h>
57 1.1 dante #include <machine/intr.h>
58 1.1 dante
59 1.18 mrg #include <uvm/uvm_extern.h>
60 1.1 dante
61 1.1 dante #include <dev/scsipi/scsi_all.h>
62 1.1 dante #include <dev/scsipi/scsipi_all.h>
63 1.1 dante #include <dev/scsipi/scsiconf.h>
64 1.1 dante
65 1.10 dante #include <dev/ic/advlib.h>
66 1.1 dante #include <dev/ic/adv.h>
67 1.3 thorpej
68 1.3 thorpej #ifndef DDB
69 1.3 thorpej #define Debugger() panic("should call debugger here (adv.c)")
70 1.3 thorpej #endif /* ! DDB */
71 1.1 dante
72 1.6 dante
73 1.6 dante /* #define ASC_DEBUG */
74 1.6 dante
75 1.1 dante /******************************************************************************/
76 1.1 dante
77 1.1 dante
78 1.13 thorpej static int adv_alloc_control_data __P((ASC_SOFTC *));
79 1.20.2.1 nathanw static void adv_free_control_data __P((ASC_SOFTC *));
80 1.1 dante static int adv_create_ccbs __P((ASC_SOFTC *, ADV_CCB *, int));
81 1.1 dante static void adv_free_ccb __P((ASC_SOFTC *, ADV_CCB *));
82 1.1 dante static void adv_reset_ccb __P((ADV_CCB *));
83 1.1 dante static int adv_init_ccb __P((ASC_SOFTC *, ADV_CCB *));
84 1.20.2.2 nathanw static ADV_CCB *adv_get_ccb __P((ASC_SOFTC *));
85 1.1 dante static void adv_queue_ccb __P((ASC_SOFTC *, ADV_CCB *));
86 1.1 dante static void adv_start_ccbs __P((ASC_SOFTC *));
87 1.1 dante
88 1.1 dante
89 1.20.2.2 nathanw static void adv_scsipi_request __P((struct scsipi_channel *,
90 1.20.2.2 nathanw scsipi_adapter_req_t, void *));
91 1.1 dante static void advminphys __P((struct buf *));
92 1.1 dante static void adv_narrow_isr_callback __P((ASC_SOFTC *, ASC_QDONE_INFO *));
93 1.1 dante
94 1.1 dante static int adv_poll __P((ASC_SOFTC *, struct scsipi_xfer *, int));
95 1.1 dante static void adv_timeout __P((void *));
96 1.1 dante static void adv_watchdog __P((void *));
97 1.1 dante
98 1.1 dante
99 1.1 dante /******************************************************************************/
100 1.1 dante
101 1.1 dante #define ADV_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
102 1.1 dante #define ADV_WATCH_TIMEOUT 1000 /* time to wait for watchdog (mSec) */
103 1.1 dante
104 1.1 dante /******************************************************************************/
105 1.1 dante /* Control Blocks routines */
106 1.1 dante /******************************************************************************/
107 1.1 dante
108 1.1 dante
109 1.1 dante static int
110 1.13 thorpej adv_alloc_control_data(sc)
111 1.1 dante ASC_SOFTC *sc;
112 1.1 dante {
113 1.20.2.1 nathanw int error;
114 1.1 dante
115 1.1 dante /*
116 1.20.2.2 nathanw * Allocate the control blocks.
117 1.20.2.2 nathanw */
118 1.1 dante if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct adv_control),
119 1.20.2.1 nathanw PAGE_SIZE, 0, &sc->sc_control_seg, 1,
120 1.20.2.1 nathanw &sc->sc_control_nsegs, BUS_DMA_NOWAIT)) != 0) {
121 1.1 dante printf("%s: unable to allocate control structures,"
122 1.1 dante " error = %d\n", sc->sc_dev.dv_xname, error);
123 1.1 dante return (error);
124 1.1 dante }
125 1.20.2.1 nathanw if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_control_seg,
126 1.20.2.1 nathanw sc->sc_control_nsegs, sizeof(struct adv_control),
127 1.20.2.1 nathanw (caddr_t *) & sc->sc_control,
128 1.20.2.1 nathanw BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
129 1.1 dante printf("%s: unable to map control structures, error = %d\n",
130 1.1 dante sc->sc_dev.dv_xname, error);
131 1.1 dante return (error);
132 1.1 dante }
133 1.1 dante /*
134 1.20.2.2 nathanw * Create and load the DMA map used for the control blocks.
135 1.20.2.2 nathanw */
136 1.1 dante if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct adv_control),
137 1.1 dante 1, sizeof(struct adv_control), 0, BUS_DMA_NOWAIT,
138 1.1 dante &sc->sc_dmamap_control)) != 0) {
139 1.1 dante printf("%s: unable to create control DMA map, error = %d\n",
140 1.1 dante sc->sc_dev.dv_xname, error);
141 1.1 dante return (error);
142 1.1 dante }
143 1.1 dante if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
144 1.1 dante sc->sc_control, sizeof(struct adv_control), NULL,
145 1.1 dante BUS_DMA_NOWAIT)) != 0) {
146 1.1 dante printf("%s: unable to load control DMA map, error = %d\n",
147 1.1 dante sc->sc_dev.dv_xname, error);
148 1.1 dante return (error);
149 1.1 dante }
150 1.13 thorpej
151 1.13 thorpej /*
152 1.13 thorpej * Initialize the overrun_buf address.
153 1.13 thorpej */
154 1.13 thorpej sc->overrun_buf = sc->sc_dmamap_control->dm_segs[0].ds_addr +
155 1.13 thorpej offsetof(struct adv_control, overrun_buf);
156 1.13 thorpej
157 1.1 dante return (0);
158 1.1 dante }
159 1.1 dante
160 1.20.2.1 nathanw static void
161 1.20.2.1 nathanw adv_free_control_data(sc)
162 1.20.2.1 nathanw ASC_SOFTC *sc;
163 1.20.2.1 nathanw {
164 1.20.2.1 nathanw
165 1.20.2.1 nathanw bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_control);
166 1.20.2.1 nathanw bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_control);
167 1.20.2.1 nathanw sc->sc_dmamap_control = NULL;
168 1.20.2.1 nathanw
169 1.20.2.1 nathanw bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control,
170 1.20.2.1 nathanw sizeof(struct adv_control));
171 1.20.2.1 nathanw bus_dmamem_free(sc->sc_dmat, &sc->sc_control_seg,
172 1.20.2.1 nathanw sc->sc_control_nsegs);
173 1.20.2.1 nathanw }
174 1.1 dante
175 1.1 dante /*
176 1.1 dante * Create a set of ccbs and add them to the free list. Called once
177 1.1 dante * by adv_init(). We return the number of CCBs successfully created.
178 1.1 dante */
179 1.1 dante static int
180 1.1 dante adv_create_ccbs(sc, ccbstore, count)
181 1.1 dante ASC_SOFTC *sc;
182 1.1 dante ADV_CCB *ccbstore;
183 1.1 dante int count;
184 1.1 dante {
185 1.1 dante ADV_CCB *ccb;
186 1.1 dante int i, error;
187 1.1 dante
188 1.20.2.3 nathanw memset(ccbstore, 0, sizeof(ADV_CCB) * count);
189 1.1 dante for (i = 0; i < count; i++) {
190 1.1 dante ccb = &ccbstore[i];
191 1.1 dante if ((error = adv_init_ccb(sc, ccb)) != 0) {
192 1.1 dante printf("%s: unable to initialize ccb, error = %d\n",
193 1.1 dante sc->sc_dev.dv_xname, error);
194 1.1 dante return (i);
195 1.1 dante }
196 1.1 dante TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
197 1.1 dante }
198 1.1 dante
199 1.1 dante return (i);
200 1.1 dante }
201 1.1 dante
202 1.1 dante
203 1.1 dante /*
204 1.1 dante * A ccb is put onto the free list.
205 1.1 dante */
206 1.1 dante static void
207 1.1 dante adv_free_ccb(sc, ccb)
208 1.1 dante ASC_SOFTC *sc;
209 1.1 dante ADV_CCB *ccb;
210 1.1 dante {
211 1.1 dante int s;
212 1.1 dante
213 1.1 dante s = splbio();
214 1.1 dante adv_reset_ccb(ccb);
215 1.1 dante TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
216 1.1 dante splx(s);
217 1.1 dante }
218 1.1 dante
219 1.1 dante
220 1.1 dante static void
221 1.1 dante adv_reset_ccb(ccb)
222 1.1 dante ADV_CCB *ccb;
223 1.1 dante {
224 1.1 dante
225 1.1 dante ccb->flags = 0;
226 1.1 dante }
227 1.1 dante
228 1.1 dante
229 1.1 dante static int
230 1.1 dante adv_init_ccb(sc, ccb)
231 1.1 dante ASC_SOFTC *sc;
232 1.1 dante ADV_CCB *ccb;
233 1.1 dante {
234 1.10 dante int hashnum, error;
235 1.1 dante
236 1.16 thorpej callout_init(&ccb->ccb_watchdog);
237 1.16 thorpej
238 1.1 dante /*
239 1.20.2.2 nathanw * Create the DMA map for this CCB.
240 1.20.2.2 nathanw */
241 1.1 dante error = bus_dmamap_create(sc->sc_dmat,
242 1.1 dante (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
243 1.1 dante ASC_MAX_SG_LIST, (ASC_MAX_SG_LIST - 1) * PAGE_SIZE,
244 1.1 dante 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ccb->dmamap_xfer);
245 1.1 dante if (error) {
246 1.1 dante printf("%s: unable to create DMA map, error = %d\n",
247 1.1 dante sc->sc_dev.dv_xname, error);
248 1.1 dante return (error);
249 1.1 dante }
250 1.10 dante
251 1.10 dante /*
252 1.10 dante * put in the phystokv hash table
253 1.10 dante * Never gets taken out.
254 1.10 dante */
255 1.10 dante ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
256 1.10 dante ADV_CCB_OFF(ccb);
257 1.10 dante hashnum = CCB_HASH(ccb->hashkey);
258 1.10 dante ccb->nexthash = sc->sc_ccbhash[hashnum];
259 1.10 dante sc->sc_ccbhash[hashnum] = ccb;
260 1.10 dante
261 1.1 dante adv_reset_ccb(ccb);
262 1.1 dante return (0);
263 1.1 dante }
264 1.1 dante
265 1.1 dante
266 1.1 dante /*
267 1.1 dante * Get a free ccb
268 1.1 dante *
269 1.1 dante * If there are none, see if we can allocate a new one
270 1.1 dante */
271 1.1 dante static ADV_CCB *
272 1.20.2.2 nathanw adv_get_ccb(sc)
273 1.1 dante ASC_SOFTC *sc;
274 1.1 dante {
275 1.1 dante ADV_CCB *ccb = 0;
276 1.1 dante int s;
277 1.1 dante
278 1.1 dante s = splbio();
279 1.20.2.2 nathanw ccb = TAILQ_FIRST(&sc->sc_free_ccb);
280 1.20.2.2 nathanw if (ccb != NULL) {
281 1.20.2.2 nathanw TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
282 1.20.2.2 nathanw ccb->flags |= CCB_ALLOC;
283 1.1 dante }
284 1.1 dante splx(s);
285 1.1 dante return (ccb);
286 1.1 dante }
287 1.1 dante
288 1.1 dante
289 1.1 dante /*
290 1.10 dante * Given a physical address, find the ccb that it corresponds to.
291 1.10 dante */
292 1.10 dante ADV_CCB *
293 1.10 dante adv_ccb_phys_kv(sc, ccb_phys)
294 1.10 dante ASC_SOFTC *sc;
295 1.10 dante u_long ccb_phys;
296 1.10 dante {
297 1.10 dante int hashnum = CCB_HASH(ccb_phys);
298 1.10 dante ADV_CCB *ccb = sc->sc_ccbhash[hashnum];
299 1.10 dante
300 1.10 dante while (ccb) {
301 1.10 dante if (ccb->hashkey == ccb_phys)
302 1.10 dante break;
303 1.10 dante ccb = ccb->nexthash;
304 1.10 dante }
305 1.10 dante return (ccb);
306 1.10 dante }
307 1.10 dante
308 1.10 dante
309 1.10 dante /*
310 1.1 dante * Queue a CCB to be sent to the controller, and send it if possible.
311 1.1 dante */
312 1.1 dante static void
313 1.1 dante adv_queue_ccb(sc, ccb)
314 1.1 dante ASC_SOFTC *sc;
315 1.1 dante ADV_CCB *ccb;
316 1.1 dante {
317 1.1 dante
318 1.1 dante TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
319 1.1 dante
320 1.1 dante adv_start_ccbs(sc);
321 1.1 dante }
322 1.1 dante
323 1.1 dante
324 1.1 dante static void
325 1.1 dante adv_start_ccbs(sc)
326 1.1 dante ASC_SOFTC *sc;
327 1.1 dante {
328 1.1 dante ADV_CCB *ccb;
329 1.1 dante
330 1.1 dante while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
331 1.1 dante if (ccb->flags & CCB_WATCHDOG)
332 1.16 thorpej callout_stop(&ccb->ccb_watchdog);
333 1.1 dante
334 1.1 dante if (AscExeScsiQueue(sc, &ccb->scsiq) == ASC_BUSY) {
335 1.1 dante ccb->flags |= CCB_WATCHDOG;
336 1.16 thorpej callout_reset(&ccb->ccb_watchdog,
337 1.16 thorpej (ADV_WATCH_TIMEOUT * hz) / 1000,
338 1.16 thorpej adv_watchdog, ccb);
339 1.1 dante break;
340 1.1 dante }
341 1.1 dante TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
342 1.1 dante
343 1.14 thorpej if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
344 1.16 thorpej callout_reset(&ccb->xs->xs_callout,
345 1.20.2.1 nathanw ((u_int64_t)ccb->timeout * (u_int64_t)hz) / 1000,
346 1.16 thorpej adv_timeout, ccb);
347 1.1 dante }
348 1.1 dante }
349 1.1 dante
350 1.1 dante
351 1.1 dante /******************************************************************************/
352 1.1 dante /* SCSI layer interfacing routines */
353 1.1 dante /******************************************************************************/
354 1.1 dante
355 1.1 dante
356 1.1 dante int
357 1.1 dante adv_init(sc)
358 1.1 dante ASC_SOFTC *sc;
359 1.1 dante {
360 1.1 dante int warn;
361 1.1 dante
362 1.12 dante if (!AscFindSignature(sc->sc_iot, sc->sc_ioh)) {
363 1.12 dante printf("adv_init: failed to find signature\n");
364 1.12 dante return (1);
365 1.12 dante }
366 1.1 dante
367 1.4 dante /*
368 1.20.2.2 nathanw * Read the board configuration
369 1.20.2.2 nathanw */
370 1.4 dante AscInitASC_SOFTC(sc);
371 1.4 dante warn = AscInitFromEEP(sc);
372 1.4 dante if (warn) {
373 1.4 dante printf("%s -get: ", sc->sc_dev.dv_xname);
374 1.4 dante switch (warn) {
375 1.4 dante case -1:
376 1.4 dante printf("Chip is not halted\n");
377 1.4 dante break;
378 1.4 dante
379 1.4 dante case -2:
380 1.4 dante printf("Couldn't get MicroCode Start"
381 1.4 dante " address\n");
382 1.4 dante break;
383 1.4 dante
384 1.4 dante case ASC_WARN_IO_PORT_ROTATE:
385 1.4 dante printf("I/O port address modified\n");
386 1.4 dante break;
387 1.4 dante
388 1.4 dante case ASC_WARN_AUTO_CONFIG:
389 1.4 dante printf("I/O port increment switch enabled\n");
390 1.4 dante break;
391 1.4 dante
392 1.4 dante case ASC_WARN_EEPROM_CHKSUM:
393 1.4 dante printf("EEPROM checksum error\n");
394 1.4 dante break;
395 1.4 dante
396 1.4 dante case ASC_WARN_IRQ_MODIFIED:
397 1.4 dante printf("IRQ modified\n");
398 1.4 dante break;
399 1.4 dante
400 1.4 dante case ASC_WARN_CMD_QNG_CONFLICT:
401 1.4 dante printf("tag queuing enabled w/o disconnects\n");
402 1.4 dante break;
403 1.1 dante
404 1.4 dante default:
405 1.4 dante printf("unknown warning %d\n", warn);
406 1.1 dante }
407 1.4 dante }
408 1.4 dante if (sc->scsi_reset_wait > ASC_MAX_SCSI_RESET_WAIT)
409 1.4 dante sc->scsi_reset_wait = ASC_MAX_SCSI_RESET_WAIT;
410 1.4 dante
411 1.4 dante /*
412 1.20.2.2 nathanw * Modify the board configuration
413 1.20.2.2 nathanw */
414 1.4 dante warn = AscInitFromASC_SOFTC(sc);
415 1.4 dante if (warn) {
416 1.4 dante printf("%s -set: ", sc->sc_dev.dv_xname);
417 1.4 dante switch (warn) {
418 1.4 dante case ASC_WARN_CMD_QNG_CONFLICT:
419 1.4 dante printf("tag queuing enabled w/o disconnects\n");
420 1.4 dante break;
421 1.1 dante
422 1.4 dante case ASC_WARN_AUTO_CONFIG:
423 1.4 dante printf("I/O port increment switch enabled\n");
424 1.4 dante break;
425 1.1 dante
426 1.4 dante default:
427 1.4 dante printf("unknown warning %d\n", warn);
428 1.1 dante }
429 1.4 dante }
430 1.11 dante sc->isr_callback = (ASC_CALLBACK) adv_narrow_isr_callback;
431 1.1 dante
432 1.1 dante return (0);
433 1.1 dante }
434 1.1 dante
435 1.1 dante
436 1.1 dante void
437 1.1 dante adv_attach(sc)
438 1.1 dante ASC_SOFTC *sc;
439 1.1 dante {
440 1.20.2.2 nathanw struct scsipi_adapter *adapt = &sc->sc_adapter;
441 1.20.2.2 nathanw struct scsipi_channel *chan = &sc->sc_channel;
442 1.1 dante int i, error;
443 1.1 dante
444 1.4 dante /*
445 1.20.2.2 nathanw * Initialize board RISC chip and enable interrupts.
446 1.20.2.2 nathanw */
447 1.4 dante switch (AscInitDriver(sc)) {
448 1.4 dante case 0:
449 1.4 dante /* AllOK */
450 1.4 dante break;
451 1.1 dante
452 1.4 dante case 1:
453 1.4 dante panic("%s: bad signature", sc->sc_dev.dv_xname);
454 1.4 dante break;
455 1.1 dante
456 1.4 dante case 2:
457 1.4 dante panic("%s: unable to load MicroCode",
458 1.4 dante sc->sc_dev.dv_xname);
459 1.4 dante break;
460 1.1 dante
461 1.4 dante case 3:
462 1.4 dante panic("%s: unable to initialize MicroCode",
463 1.4 dante sc->sc_dev.dv_xname);
464 1.4 dante break;
465 1.1 dante
466 1.4 dante default:
467 1.4 dante panic("%s: unable to initialize board RISC chip",
468 1.4 dante sc->sc_dev.dv_xname);
469 1.1 dante }
470 1.1 dante
471 1.7 thorpej /*
472 1.20.2.2 nathanw * Fill in the scsipi_adapter.
473 1.7 thorpej */
474 1.20.2.2 nathanw memset(adapt, 0, sizeof(*adapt));
475 1.20.2.2 nathanw adapt->adapt_dev = &sc->sc_dev;
476 1.20.2.2 nathanw adapt->adapt_nchannels = 1;
477 1.20.2.2 nathanw /* adapt_openings initialized below */
478 1.20.2.2 nathanw /* adapt_max_periph initialized below */
479 1.20.2.2 nathanw adapt->adapt_request = adv_scsipi_request;
480 1.20.2.2 nathanw adapt->adapt_minphys = advminphys;
481 1.1 dante
482 1.1 dante /*
483 1.20.2.2 nathanw * Fill in the scsipi_channel.
484 1.20.2.2 nathanw */
485 1.20.2.2 nathanw memset(chan, 0, sizeof(*chan));
486 1.20.2.2 nathanw chan->chan_adapter = adapt;
487 1.20.2.2 nathanw chan->chan_bustype = &scsi_bustype;
488 1.20.2.2 nathanw chan->chan_channel = 0;
489 1.20.2.2 nathanw chan->chan_ntargets = 8;
490 1.20.2.2 nathanw chan->chan_nluns = 8;
491 1.20.2.2 nathanw chan->chan_id = sc->chip_scsi_id;
492 1.1 dante
493 1.1 dante TAILQ_INIT(&sc->sc_free_ccb);
494 1.1 dante TAILQ_INIT(&sc->sc_waiting_ccb);
495 1.1 dante
496 1.1 dante /*
497 1.20.2.2 nathanw * Allocate the Control Blocks and the overrun buffer.
498 1.20.2.2 nathanw */
499 1.13 thorpej error = adv_alloc_control_data(sc);
500 1.1 dante if (error)
501 1.12 dante return; /* (error) */
502 1.1 dante
503 1.1 dante /*
504 1.20.2.2 nathanw * Create and initialize the Control Blocks.
505 1.20.2.2 nathanw */
506 1.1 dante i = adv_create_ccbs(sc, sc->sc_control->ccbs, ADV_MAX_CCB);
507 1.1 dante if (i == 0) {
508 1.1 dante printf("%s: unable to create control blocks\n",
509 1.1 dante sc->sc_dev.dv_xname);
510 1.1 dante return; /* (ENOMEM) */ ;
511 1.1 dante } else if (i != ADV_MAX_CCB) {
512 1.1 dante printf("%s: WARNING: only %d of %d control blocks created\n",
513 1.1 dante sc->sc_dev.dv_xname, i, ADV_MAX_CCB);
514 1.1 dante }
515 1.20.2.2 nathanw
516 1.20.2.2 nathanw adapt->adapt_openings = i;
517 1.20.2.2 nathanw adapt->adapt_max_periph = adapt->adapt_openings;
518 1.20.2.2 nathanw
519 1.20.2.2 nathanw sc->sc_child = config_found(&sc->sc_dev, chan, scsiprint);
520 1.1 dante }
521 1.1 dante
522 1.20.2.1 nathanw int
523 1.20.2.1 nathanw adv_detach(sc, flags)
524 1.20.2.1 nathanw ASC_SOFTC *sc;
525 1.20.2.1 nathanw int flags;
526 1.20.2.1 nathanw {
527 1.20.2.1 nathanw int rv = 0;
528 1.20.2.1 nathanw
529 1.20.2.1 nathanw if (sc->sc_child != NULL)
530 1.20.2.1 nathanw rv = config_detach(sc->sc_child, flags);
531 1.20.2.1 nathanw
532 1.20.2.1 nathanw adv_free_control_data(sc);
533 1.20.2.1 nathanw
534 1.20.2.1 nathanw return (rv);
535 1.20.2.1 nathanw }
536 1.1 dante
537 1.1 dante static void
538 1.1 dante advminphys(bp)
539 1.1 dante struct buf *bp;
540 1.1 dante {
541 1.1 dante
542 1.1 dante if (bp->b_bcount > ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE))
543 1.1 dante bp->b_bcount = ((ASC_MAX_SG_LIST - 1) * PAGE_SIZE);
544 1.1 dante minphys(bp);
545 1.1 dante }
546 1.1 dante
547 1.1 dante
548 1.1 dante /*
549 1.1 dante * start a scsi operation given the command and the data address. Also needs
550 1.1 dante * the unit, target and lu.
551 1.1 dante */
552 1.1 dante
553 1.20.2.2 nathanw static void
554 1.20.2.2 nathanw adv_scsipi_request(chan, req, arg)
555 1.20.2.2 nathanw struct scsipi_channel *chan;
556 1.20.2.2 nathanw scsipi_adapter_req_t req;
557 1.20.2.2 nathanw void *arg;
558 1.20.2.2 nathanw {
559 1.20.2.2 nathanw struct scsipi_xfer *xs;
560 1.20.2.2 nathanw struct scsipi_periph *periph;
561 1.20.2.2 nathanw ASC_SOFTC *sc = (void *)chan->chan_adapter->adapt_dev;
562 1.20.2.2 nathanw bus_dma_tag_t dmat = sc->sc_dmat;
563 1.20.2.2 nathanw ADV_CCB *ccb;
564 1.20.2.2 nathanw int s, flags, error, nsegs;
565 1.20.2.2 nathanw
566 1.20.2.2 nathanw switch (req) {
567 1.20.2.2 nathanw case ADAPTER_REQ_RUN_XFER:
568 1.20.2.2 nathanw xs = arg;
569 1.20.2.2 nathanw periph = xs->xs_periph;
570 1.20.2.2 nathanw flags = xs->xs_control;
571 1.20.2.2 nathanw
572 1.20.2.2 nathanw /*
573 1.20.2.2 nathanw * Get a CCB to use.
574 1.20.2.2 nathanw */
575 1.20.2.2 nathanw ccb = adv_get_ccb(sc);
576 1.20.2.2 nathanw #ifdef DIAGNOSTIC
577 1.20.2.2 nathanw /*
578 1.20.2.2 nathanw * This should never happen as we track the resources
579 1.20.2.2 nathanw * in the mid-layer.
580 1.20.2.2 nathanw */
581 1.20.2.2 nathanw if (ccb == NULL) {
582 1.20.2.2 nathanw scsipi_printaddr(periph);
583 1.20.2.2 nathanw printf("unable to allocate ccb\n");
584 1.20.2.2 nathanw panic("adv_scsipi_request");
585 1.20.2.2 nathanw }
586 1.20.2.2 nathanw #endif
587 1.20.2.2 nathanw
588 1.20.2.2 nathanw ccb->xs = xs;
589 1.20.2.2 nathanw ccb->timeout = xs->timeout;
590 1.20.2.2 nathanw
591 1.20.2.2 nathanw /*
592 1.20.2.2 nathanw * Build up the request
593 1.20.2.2 nathanw */
594 1.20.2.2 nathanw memset(&ccb->scsiq, 0, sizeof(ASC_SCSI_Q));
595 1.20.2.2 nathanw
596 1.20.2.2 nathanw ccb->scsiq.q2.ccb_ptr =
597 1.20.2.2 nathanw sc->sc_dmamap_control->dm_segs[0].ds_addr +
598 1.20.2.2 nathanw ADV_CCB_OFF(ccb);
599 1.20.2.2 nathanw
600 1.20.2.2 nathanw ccb->scsiq.cdbptr = &xs->cmd->opcode;
601 1.20.2.2 nathanw ccb->scsiq.q2.cdb_len = xs->cmdlen;
602 1.20.2.2 nathanw ccb->scsiq.q1.target_id =
603 1.20.2.2 nathanw ASC_TID_TO_TARGET_ID(periph->periph_target);
604 1.20.2.2 nathanw ccb->scsiq.q1.target_lun = periph->periph_lun;
605 1.20.2.2 nathanw ccb->scsiq.q2.target_ix =
606 1.20.2.2 nathanw ASC_TIDLUN_TO_IX(periph->periph_target,
607 1.20.2.2 nathanw periph->periph_lun);
608 1.20.2.2 nathanw ccb->scsiq.q1.sense_addr =
609 1.20.2.2 nathanw sc->sc_dmamap_control->dm_segs[0].ds_addr +
610 1.20.2.2 nathanw ADV_CCB_OFF(ccb) + offsetof(struct adv_ccb, scsi_sense);
611 1.20.2.2 nathanw ccb->scsiq.q1.sense_len = sizeof(struct scsipi_sense_data);
612 1.20.2.2 nathanw
613 1.20.2.2 nathanw /*
614 1.20.2.2 nathanw * If there are any outstanding requests for the current
615 1.20.2.2 nathanw * target, then every 255th request send an ORDERED request.
616 1.20.2.2 nathanw * This heuristic tries to retain the benefit of request
617 1.20.2.2 nathanw * sorting while preventing request starvation. 255 is the
618 1.20.2.2 nathanw * max number of tags or pending commands a device may have
619 1.20.2.2 nathanw * outstanding.
620 1.20.2.2 nathanw */
621 1.20.2.2 nathanw sc->reqcnt[periph->periph_target]++;
622 1.20.2.2 nathanw if (((sc->reqcnt[periph->periph_target] > 0) &&
623 1.20.2.2 nathanw (sc->reqcnt[periph->periph_target] % 255) == 0) ||
624 1.20.2.2 nathanw xs->bp == NULL || (xs->bp->b_flags & B_ASYNC) == 0) {
625 1.20.2.2 nathanw ccb->scsiq.q2.tag_code = M2_QTAG_MSG_ORDERED;
626 1.20.2.2 nathanw } else {
627 1.20.2.2 nathanw ccb->scsiq.q2.tag_code = M2_QTAG_MSG_SIMPLE;
628 1.20.2.2 nathanw }
629 1.20.2.2 nathanw
630 1.20.2.2 nathanw if (xs->datalen) {
631 1.20.2.2 nathanw /*
632 1.20.2.2 nathanw * Map the DMA transfer.
633 1.20.2.2 nathanw */
634 1.1 dante #ifdef TFS
635 1.20.2.2 nathanw if (flags & SCSI_DATA_UIO) {
636 1.20.2.2 nathanw error = bus_dmamap_load_uio(dmat,
637 1.20.2.2 nathanw ccb->dmamap_xfer, (struct uio *) xs->data,
638 1.20.2.2 nathanw ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
639 1.20.2.3 nathanw BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
640 1.20.2.3 nathanw ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
641 1.20.2.3 nathanw BUS_DMA_WRITE));
642 1.20.2.2 nathanw } else
643 1.20.2.2 nathanw #endif /* TFS */
644 1.20.2.2 nathanw {
645 1.20.2.2 nathanw error = bus_dmamap_load(dmat, ccb->dmamap_xfer,
646 1.20.2.2 nathanw xs->data, xs->datalen, NULL,
647 1.20.2.2 nathanw ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
648 1.20.2.3 nathanw BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
649 1.20.2.3 nathanw ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
650 1.20.2.3 nathanw BUS_DMA_WRITE));
651 1.20.2.2 nathanw }
652 1.20.2.2 nathanw
653 1.20.2.2 nathanw switch (error) {
654 1.20.2.2 nathanw case 0:
655 1.20.2.2 nathanw break;
656 1.20.2.2 nathanw
657 1.20.2.2 nathanw
658 1.20.2.2 nathanw case ENOMEM:
659 1.20.2.2 nathanw case EAGAIN:
660 1.20.2.2 nathanw xs->error = XS_RESOURCE_SHORTAGE;
661 1.20.2.2 nathanw goto out_bad;
662 1.20.2.2 nathanw
663 1.20.2.2 nathanw default:
664 1.20.2.2 nathanw xs->error = XS_DRIVER_STUFFUP;
665 1.20.2.2 nathanw if (error == EFBIG) {
666 1.20.2.2 nathanw printf("%s: adv_scsi_cmd, more than %d"
667 1.20.2.2 nathanw " dma segments\n",
668 1.20.2.2 nathanw sc->sc_dev.dv_xname,
669 1.20.2.2 nathanw ASC_MAX_SG_LIST);
670 1.20.2.2 nathanw } else {
671 1.20.2.2 nathanw printf("%s: adv_scsi_cmd, error %d"
672 1.20.2.2 nathanw " loading dma map\n",
673 1.20.2.2 nathanw sc->sc_dev.dv_xname, error);
674 1.20.2.2 nathanw }
675 1.20.2.2 nathanw
676 1.20.2.2 nathanw out_bad:
677 1.20.2.2 nathanw adv_free_ccb(sc, ccb);
678 1.20.2.2 nathanw scsipi_done(xs);
679 1.20.2.2 nathanw return;
680 1.20.2.2 nathanw }
681 1.20.2.2 nathanw bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
682 1.20.2.2 nathanw ccb->dmamap_xfer->dm_mapsize,
683 1.20.2.2 nathanw (flags & XS_CTL_DATA_IN) ?
684 1.20.2.2 nathanw BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
685 1.20.2.2 nathanw
686 1.20.2.2 nathanw memset(&ccb->sghead, 0, sizeof(ASC_SG_HEAD));
687 1.20.2.2 nathanw
688 1.20.2.2 nathanw for (nsegs = 0;
689 1.20.2.2 nathanw nsegs < ccb->dmamap_xfer->dm_nsegs; nsegs++) {
690 1.20.2.2 nathanw ccb->sghead.sg_list[nsegs].addr =
691 1.20.2.2 nathanw ccb->dmamap_xfer->dm_segs[nsegs].ds_addr;
692 1.20.2.2 nathanw ccb->sghead.sg_list[nsegs].bytes =
693 1.20.2.2 nathanw ccb->dmamap_xfer->dm_segs[nsegs].ds_len;
694 1.20.2.2 nathanw }
695 1.20.2.2 nathanw
696 1.20.2.2 nathanw ccb->sghead.entry_cnt = ccb->scsiq.q1.sg_queue_cnt =
697 1.20.2.2 nathanw ccb->dmamap_xfer->dm_nsegs;
698 1.20.2.2 nathanw
699 1.20.2.2 nathanw ccb->scsiq.q1.cntl |= ASC_QC_SG_HEAD;
700 1.20.2.2 nathanw ccb->scsiq.sg_head = &ccb->sghead;
701 1.20.2.2 nathanw ccb->scsiq.q1.data_addr = 0;
702 1.20.2.2 nathanw ccb->scsiq.q1.data_cnt = 0;
703 1.20.2.2 nathanw } else {
704 1.20.2.2 nathanw /*
705 1.20.2.2 nathanw * No data xfer, use non S/G values.
706 1.20.2.2 nathanw */
707 1.20.2.2 nathanw ccb->scsiq.q1.data_addr = 0;
708 1.20.2.2 nathanw ccb->scsiq.q1.data_cnt = 0;
709 1.20.2.2 nathanw }
710 1.20.2.2 nathanw
711 1.6 dante #ifdef ASC_DEBUG
712 1.20.2.2 nathanw printf("id = 0, lun = 0, cmd = 0, ccb = 0x0 \n",
713 1.20.2.2 nathanw periph->periph_target,
714 1.20.2.2 nathanw periph->periph_lun, xs->cmd->opcode,
715 1.20.2.2 nathanw (unsigned long)ccb);
716 1.6 dante #endif
717 1.20.2.2 nathanw s = splbio();
718 1.20.2.2 nathanw adv_queue_ccb(sc, ccb);
719 1.20.2.2 nathanw splx(s);
720 1.20.2.2 nathanw
721 1.20.2.2 nathanw if ((flags & XS_CTL_POLL) == 0)
722 1.20.2.2 nathanw return;
723 1.20.2.2 nathanw
724 1.20.2.2 nathanw /* Not allowed to use interrupts, poll for completion. */
725 1.20.2.2 nathanw if (adv_poll(sc, xs, ccb->timeout)) {
726 1.20.2.2 nathanw adv_timeout(ccb);
727 1.20.2.2 nathanw if (adv_poll(sc, xs, ccb->timeout))
728 1.20.2.2 nathanw adv_timeout(ccb);
729 1.20.2.2 nathanw }
730 1.20.2.2 nathanw return;
731 1.20.2.2 nathanw
732 1.20.2.2 nathanw case ADAPTER_REQ_GROW_RESOURCES:
733 1.20.2.2 nathanw /* XXX Not supported. */
734 1.20.2.2 nathanw return;
735 1.20.2.2 nathanw
736 1.20.2.2 nathanw case ADAPTER_REQ_SET_XFER_MODE:
737 1.20.2.2 nathanw {
738 1.20.2.2 nathanw /*
739 1.20.2.2 nathanw * We can't really set the mode, but we know how to
740 1.20.2.2 nathanw * query what the firmware negotiated.
741 1.20.2.2 nathanw */
742 1.20.2.2 nathanw struct scsipi_xfer_mode *xm = arg;
743 1.20.2.2 nathanw u_int8_t sdtr_data;
744 1.20.2.2 nathanw ASC_SCSI_BIT_ID_TYPE tid_bit;
745 1.20.2.2 nathanw
746 1.20.2.2 nathanw tid_bit = ASC_TIX_TO_TARGET_ID(xm->xm_target);
747 1.20.2.2 nathanw
748 1.20.2.2 nathanw xm->xm_mode = 0;
749 1.20.2.2 nathanw xm->xm_period = 0;
750 1.20.2.2 nathanw xm->xm_offset = 0;
751 1.20.2.2 nathanw
752 1.20.2.2 nathanw if (sc->init_sdtr & tid_bit) {
753 1.20.2.2 nathanw xm->xm_mode |= PERIPH_CAP_SYNC;
754 1.20.2.2 nathanw sdtr_data = sc->sdtr_data[xm->xm_target];
755 1.20.2.2 nathanw xm->xm_period =
756 1.20.2.2 nathanw sc->sdtr_period_tbl[(sdtr_data >> 4) &
757 1.20.2.2 nathanw (sc->max_sdtr_index - 1)];
758 1.20.2.2 nathanw xm->xm_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
759 1.20.2.2 nathanw }
760 1.20.2.2 nathanw
761 1.20.2.2 nathanw if (sc->use_tagged_qng & tid_bit)
762 1.20.2.2 nathanw xm->xm_mode |= PERIPH_CAP_TQING;
763 1.20.2.2 nathanw
764 1.20.2.2 nathanw scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
765 1.20.2.2 nathanw return;
766 1.20.2.2 nathanw }
767 1.20.2.2 nathanw }
768 1.1 dante }
769 1.1 dante
770 1.1 dante int
771 1.1 dante adv_intr(arg)
772 1.1 dante void *arg;
773 1.1 dante {
774 1.1 dante ASC_SOFTC *sc = arg;
775 1.1 dante
776 1.6 dante #ifdef ASC_DEBUG
777 1.6 dante int int_pend = FALSE;
778 1.6 dante
779 1.6 dante if(ASC_IS_INT_PENDING(sc->sc_iot, sc->sc_ioh))
780 1.6 dante {
781 1.6 dante int_pend = TRUE;
782 1.6 dante printf("ISR - ");
783 1.6 dante }
784 1.6 dante #endif
785 1.4 dante AscISR(sc);
786 1.6 dante #ifdef ASC_DEBUG
787 1.6 dante if(int_pend)
788 1.6 dante printf("\n");
789 1.6 dante #endif
790 1.1 dante
791 1.1 dante return (1);
792 1.1 dante }
793 1.1 dante
794 1.1 dante
795 1.1 dante /*
796 1.1 dante * Poll a particular unit, looking for a particular xs
797 1.1 dante */
798 1.1 dante static int
799 1.1 dante adv_poll(sc, xs, count)
800 1.1 dante ASC_SOFTC *sc;
801 1.1 dante struct scsipi_xfer *xs;
802 1.1 dante int count;
803 1.1 dante {
804 1.1 dante
805 1.1 dante /* timeouts are in msec, so we loop in 1000 usec cycles */
806 1.1 dante while (count) {
807 1.1 dante adv_intr(sc);
808 1.14 thorpej if (xs->xs_status & XS_STS_DONE)
809 1.1 dante return (0);
810 1.1 dante delay(1000); /* only happens in boot so ok */
811 1.1 dante count--;
812 1.1 dante }
813 1.1 dante return (1);
814 1.1 dante }
815 1.1 dante
816 1.1 dante
817 1.1 dante static void
818 1.1 dante adv_timeout(arg)
819 1.1 dante void *arg;
820 1.1 dante {
821 1.1 dante ADV_CCB *ccb = arg;
822 1.1 dante struct scsipi_xfer *xs = ccb->xs;
823 1.20.2.2 nathanw struct scsipi_periph *periph = xs->xs_periph;
824 1.20.2.2 nathanw ASC_SOFTC *sc =
825 1.20.2.2 nathanw (void *)periph->periph_channel->chan_adapter->adapt_dev;
826 1.1 dante int s;
827 1.1 dante
828 1.20.2.2 nathanw scsipi_printaddr(periph);
829 1.1 dante printf("timed out");
830 1.1 dante
831 1.1 dante s = splbio();
832 1.1 dante
833 1.1 dante /*
834 1.20.2.2 nathanw * If it has been through before, then a previous abort has failed,
835 1.20.2.2 nathanw * don't try abort again, reset the bus instead.
836 1.20.2.2 nathanw */
837 1.1 dante if (ccb->flags & CCB_ABORT) {
838 1.1 dante /* abort timed out */
839 1.1 dante printf(" AGAIN. Resetting Bus\n");
840 1.1 dante /* Lets try resetting the bus! */
841 1.1 dante if (AscResetBus(sc) == ASC_ERROR) {
842 1.1 dante ccb->timeout = sc->scsi_reset_wait;
843 1.1 dante adv_queue_ccb(sc, ccb);
844 1.1 dante }
845 1.1 dante } else {
846 1.1 dante /* abort the operation that has timed out */
847 1.1 dante printf("\n");
848 1.10 dante AscAbortCCB(sc, ccb);
849 1.1 dante ccb->xs->error = XS_TIMEOUT;
850 1.1 dante ccb->timeout = ADV_ABORT_TIMEOUT;
851 1.1 dante ccb->flags |= CCB_ABORT;
852 1.1 dante adv_queue_ccb(sc, ccb);
853 1.1 dante }
854 1.1 dante
855 1.1 dante splx(s);
856 1.1 dante }
857 1.1 dante
858 1.1 dante
859 1.1 dante static void
860 1.1 dante adv_watchdog(arg)
861 1.1 dante void *arg;
862 1.1 dante {
863 1.1 dante ADV_CCB *ccb = arg;
864 1.1 dante struct scsipi_xfer *xs = ccb->xs;
865 1.20.2.2 nathanw struct scsipi_periph *periph = xs->xs_periph;
866 1.20.2.2 nathanw ASC_SOFTC *sc =
867 1.20.2.2 nathanw (void *)periph->periph_channel->chan_adapter->adapt_dev;
868 1.1 dante int s;
869 1.1 dante
870 1.1 dante s = splbio();
871 1.1 dante
872 1.1 dante ccb->flags &= ~CCB_WATCHDOG;
873 1.1 dante adv_start_ccbs(sc);
874 1.1 dante
875 1.1 dante splx(s);
876 1.1 dante }
877 1.1 dante
878 1.1 dante
879 1.1 dante /******************************************************************************/
880 1.10 dante /* NARROW boards Interrupt callbacks */
881 1.1 dante /******************************************************************************/
882 1.1 dante
883 1.1 dante
884 1.1 dante /*
885 1.1 dante * adv_narrow_isr_callback() - Second Level Interrupt Handler called by AscISR()
886 1.1 dante *
887 1.1 dante * Interrupt callback function for the Narrow SCSI Asc Library.
888 1.1 dante */
889 1.1 dante static void
890 1.1 dante adv_narrow_isr_callback(sc, qdonep)
891 1.1 dante ASC_SOFTC *sc;
892 1.1 dante ASC_QDONE_INFO *qdonep;
893 1.1 dante {
894 1.1 dante bus_dma_tag_t dmat = sc->sc_dmat;
895 1.10 dante ADV_CCB *ccb;
896 1.10 dante struct scsipi_xfer *xs;
897 1.1 dante struct scsipi_sense_data *s1, *s2;
898 1.1 dante
899 1.10 dante
900 1.10 dante ccb = adv_ccb_phys_kv(sc, qdonep->d2.ccb_ptr);
901 1.10 dante xs = ccb->xs;
902 1.1 dante
903 1.6 dante #ifdef ASC_DEBUG
904 1.6 dante printf(" - ccb=0x%lx, id=%d, lun=%d, cmd=%d, ",
905 1.6 dante (unsigned long)ccb,
906 1.20.2.2 nathanw xs->xs_periph->periph_target,
907 1.20.2.2 nathanw xs->xs_periph->periph_lun, xs->cmd->opcode);
908 1.6 dante #endif
909 1.16 thorpej callout_stop(&ccb->xs->xs_callout);
910 1.1 dante
911 1.1 dante /*
912 1.20.2.2 nathanw * If we were a data transfer, unload the map that described
913 1.20.2.2 nathanw * the data buffer.
914 1.20.2.2 nathanw */
915 1.1 dante if (xs->datalen) {
916 1.1 dante bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
917 1.1 dante ccb->dmamap_xfer->dm_mapsize,
918 1.14 thorpej (xs->xs_control & XS_CTL_DATA_IN) ?
919 1.14 thorpej BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
920 1.1 dante bus_dmamap_unload(dmat, ccb->dmamap_xfer);
921 1.1 dante }
922 1.1 dante if ((ccb->flags & CCB_ALLOC) == 0) {
923 1.1 dante printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
924 1.1 dante Debugger();
925 1.1 dante return;
926 1.1 dante }
927 1.1 dante /*
928 1.20.2.2 nathanw * 'qdonep' contains the command's ending status.
929 1.20.2.2 nathanw */
930 1.6 dante #ifdef ASC_DEBUG
931 1.6 dante printf("d_s=%d, h_s=%d", qdonep->d3.done_stat, qdonep->d3.host_stat);
932 1.6 dante #endif
933 1.1 dante switch (qdonep->d3.done_stat) {
934 1.1 dante case ASC_QD_NO_ERROR:
935 1.1 dante switch (qdonep->d3.host_stat) {
936 1.1 dante case ASC_QHSTA_NO_ERROR:
937 1.1 dante xs->error = XS_NOERROR;
938 1.1 dante xs->resid = 0;
939 1.1 dante break;
940 1.1 dante
941 1.1 dante default:
942 1.1 dante /* QHSTA error occurred */
943 1.1 dante xs->error = XS_DRIVER_STUFFUP;
944 1.1 dante break;
945 1.1 dante }
946 1.1 dante
947 1.1 dante /*
948 1.20.2.2 nathanw * If an INQUIRY command completed successfully, then call
949 1.20.2.2 nathanw * the AscInquiryHandling() function to patch bugged boards.
950 1.20.2.2 nathanw */
951 1.1 dante if ((xs->cmd->opcode == SCSICMD_Inquiry) &&
952 1.20.2.2 nathanw (xs->xs_periph->periph_lun == 0) &&
953 1.1 dante (xs->datalen - qdonep->remain_bytes) >= 8) {
954 1.1 dante AscInquiryHandling(sc,
955 1.20.2.2 nathanw xs->xs_periph->periph_target & 0x7,
956 1.1 dante (ASC_SCSI_INQUIRY *) xs->data);
957 1.1 dante }
958 1.1 dante break;
959 1.1 dante
960 1.1 dante case ASC_QD_WITH_ERROR:
961 1.1 dante switch (qdonep->d3.host_stat) {
962 1.1 dante case ASC_QHSTA_NO_ERROR:
963 1.1 dante if (qdonep->d3.scsi_stat == SS_CHK_CONDITION) {
964 1.1 dante s1 = &ccb->scsi_sense;
965 1.1 dante s2 = &xs->sense.scsi_sense;
966 1.1 dante *s2 = *s1;
967 1.1 dante xs->error = XS_SENSE;
968 1.4 dante } else {
969 1.1 dante xs->error = XS_DRIVER_STUFFUP;
970 1.4 dante }
971 1.1 dante break;
972 1.1 dante
973 1.20.2.2 nathanw case ASC_QHSTA_M_SEL_TIMEOUT:
974 1.20.2.2 nathanw xs->error = XS_SELTIMEOUT;
975 1.20.2.2 nathanw break;
976 1.20.2.2 nathanw
977 1.1 dante default:
978 1.1 dante /* QHSTA error occurred */
979 1.1 dante xs->error = XS_DRIVER_STUFFUP;
980 1.1 dante break;
981 1.1 dante }
982 1.1 dante break;
983 1.1 dante
984 1.1 dante case ASC_QD_ABORTED_BY_HOST:
985 1.1 dante default:
986 1.1 dante xs->error = XS_DRIVER_STUFFUP;
987 1.1 dante break;
988 1.1 dante }
989 1.1 dante
990 1.1 dante
991 1.1 dante adv_free_ccb(sc, ccb);
992 1.1 dante scsipi_done(xs);
993 1.1 dante }
994