xref: /netbsd/sys/dev/ic/siisata.c (revision 6550d01e)
1 /* $NetBSD: siisata.c,v 1.13 2010/11/13 09:00:00 uebayasi Exp $ */
2 
3 /* from ahcisata_core.c */
4 
5 /*
6  * Copyright (c) 2006 Manuel Bouyer.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 /* from atapi_wdc.c */
31 
32 /*
33  * Copyright (c) 1998, 2001 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55 
56 /*
57  * Copyright (c) 2007, 2008, 2009, 2010 Jonathan A. Kollasch.
58  * All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  * 1. Redistributions of source code must retain the above copyright
64  *    notice, this list of conditions and the following disclaimer.
65  * 2. Redistributions in binary form must reproduce the above copyright
66  *    notice, this list of conditions and the following disclaimer in the
67  *    documentation and/or other materials provided with the distribution.
68  *
69  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
70  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
71  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
72  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
73  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
74  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
75  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
76  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
77  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
78  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79  */
80 
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: siisata.c,v 1.13 2010/11/13 09:00:00 uebayasi Exp $");
83 
84 #include <sys/types.h>
85 #include <sys/malloc.h>
86 #include <sys/param.h>
87 #include <sys/kernel.h>
88 #include <sys/systm.h>
89 #include <sys/syslog.h>
90 #include <sys/disklabel.h>
91 #include <sys/buf.h>
92 #include <sys/proc.h>
93 
94 #include <dev/ata/atareg.h>
95 #include <dev/ata/satavar.h>
96 #include <dev/ata/satareg.h>
97 #include <dev/ata/satafisvar.h>
98 #include <dev/ata/satafisreg.h>
99 #include <dev/ic/siisatavar.h>
100 #include <dev/ic/siisatareg.h>
101 
102 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
103 
104 #include "atapibus.h"
105 
106 #ifdef SIISATA_DEBUG
107 int siisata_debug_mask = 0;
108 #endif
109 
110 #define ATA_DELAY 10000		/* 10s for a drive I/O */
111 
112 static void siisata_attach_port(struct siisata_softc *, int);
113 static void siisata_intr_port(struct siisata_channel *);
114 
115 void siisata_probe_drive(struct ata_channel *);
116 void siisata_setup_channel(struct ata_channel *);
117 
118 int siisata_ata_bio(struct ata_drive_datas *, struct ata_bio *);
119 void siisata_reset_drive(struct ata_drive_datas *, int);
120 void siisata_reset_channel(struct ata_channel *, int);
121 int siisata_ata_addref(struct ata_drive_datas *);
122 void siisata_ata_delref(struct ata_drive_datas *);
123 void siisata_killpending(struct ata_drive_datas *);
124 
125 void siisata_cmd_start(struct ata_channel *, struct ata_xfer *);
126 int siisata_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
127 void siisata_cmd_done(struct ata_channel *, struct ata_xfer *, int);
128 void siisata_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
129 
130 void siisata_bio_start(struct ata_channel *, struct ata_xfer *);
131 int siisata_bio_complete(struct ata_channel *, struct ata_xfer *, int);
132 void siisata_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
133 int siisata_exec_command(struct ata_drive_datas *, struct ata_command *);
134 
135 void siisata_timeout(void *);
136 
137 static void siisata_reinit_port(struct ata_channel *);
138 static void siisata_device_reset(struct ata_channel *);
139 static void siisata_activate_prb(struct siisata_channel *, int);
140 static void siisata_deactivate_prb(struct siisata_channel *, int);
141 static int siisata_dma_setup(struct ata_channel *chp, int slot,
142     void *data, size_t, int);
143 
144 #if NATAPIBUS > 0
145 void siisata_atapibus_attach(struct atabus_softc *);
146 void siisata_atapi_probe_device(struct atapibus_softc *, int);
147 void siisata_atapi_minphys(struct buf *);
148 void siisata_atapi_start(struct ata_channel *,struct ata_xfer *);
149 int siisata_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
150 void siisata_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
151 void siisata_atapi_done(struct ata_channel *, struct ata_xfer *, int);
152 void siisata_atapi_scsipi_request(struct scsipi_channel *,
153     scsipi_adapter_req_t, void *);
154 void siisata_atapi_kill_pending(struct scsipi_periph *);
155 #endif /* NATAPIBUS */
156 
157 const struct ata_bustype siisata_ata_bustype = {
158 	SCSIPI_BUSTYPE_ATA,
159 	siisata_ata_bio,
160 	siisata_reset_drive,
161 	siisata_reset_channel,
162 	siisata_exec_command,
163 	ata_get_params,
164 	siisata_ata_addref,
165 	siisata_ata_delref,
166 	siisata_killpending
167 };
168 
169 #if NATAPIBUS > 0
170 static const struct scsipi_bustype siisata_atapi_bustype = {
171 	SCSIPI_BUSTYPE_ATAPI,
172 	atapi_scsipi_cmd,
173 	atapi_interpret_sense,
174 	atapi_print_addr,
175 	siisata_atapi_kill_pending
176 };
177 #endif /* NATAPIBUS */
178 
179 
180 void
181 siisata_attach(struct siisata_softc *sc)
182 {
183 	int i;
184 
185 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
186 	    SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)), DEBUG_FUNCS);
187 
188 	sc->sc_atac.atac_cap = ATAC_CAP_DMA | ATAC_CAP_UDMA;
189 	sc->sc_atac.atac_pio_cap = 4;
190 	sc->sc_atac.atac_dma_cap = 2;
191 	sc->sc_atac.atac_udma_cap = 6;
192 	sc->sc_atac.atac_channels = sc->sc_chanarray;
193 	sc->sc_atac.atac_probe = siisata_probe_drive;
194 	sc->sc_atac.atac_bustype_ata = &siisata_ata_bustype;
195 	sc->sc_atac.atac_set_modes = siisata_setup_channel;
196 #if NATAPIBUS > 0
197 	sc->sc_atac.atac_atapibus_attach = siisata_atapibus_attach;
198 #endif
199 
200 	/* come out of reset state */
201 	GRWRITE(sc, GR_GC, 0);
202 
203 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
204 		siisata_attach_port(sc, i);
205 	}
206 
207 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
208 	    SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)),
209 	    DEBUG_FUNCS);
210 	return;
211 }
212 
213 static void
214 siisata_init_port(struct siisata_softc *sc, int port)
215 {
216 	struct siisata_channel *schp;
217 	struct ata_channel *chp;
218 
219 	schp = &sc->sc_channels[port];
220 	chp = (struct ata_channel *)schp;
221 
222 	/* come out of reset, 64-bit activation */
223 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCC),
224 	    PR_PC_32BA | PR_PC_PORT_RESET);
225 	/* initialize port */
226 	siisata_reinit_port(chp);
227 	/* clear any interrupts */
228 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
229 	/* enable CmdErrr+CmdCmpl interrupting */
230 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIES),
231 	    PR_PIS_CMDERRR | PR_PIS_CMDCMPL);
232 	/* enable port interrupt */
233 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
234 }
235 
236 static void
237 siisata_attach_port(struct siisata_softc *sc, int port)
238 {
239 	int j;
240 	int dmasize;
241 	int error;
242 	void *prbp;
243 	struct siisata_channel *schp;
244 	struct ata_channel *chp;
245 
246 	schp = &sc->sc_channels[port];
247 	chp = (struct ata_channel *)schp;
248 	sc->sc_chanarray[port] = chp;
249 	chp->ch_channel = port;
250 	chp->ch_atac = &sc->sc_atac;
251 	chp->ch_queue = malloc(sizeof(struct ata_queue),
252 			       M_DEVBUF, M_NOWAIT);
253 	if (chp->ch_queue == NULL) {
254 		aprint_error_dev(sc->sc_atac.atac_dev,
255 		    "port %d: can't allocate memory "
256 		    "for command queue\n", chp->ch_channel);
257 		return;
258 	}
259 
260 	dmasize = SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS;
261 
262 	SIISATA_DEBUG_PRINT(("%s: %s: dmasize: %d\n", SIISATANAME(sc),
263 	    __func__, dmasize), DEBUG_FUNCS);
264 
265 	error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
266 	    &schp->sch_prb_seg, 1, &schp->sch_prb_nseg, BUS_DMA_NOWAIT);
267 	if (error) {
268 		aprint_error_dev(sc->sc_atac.atac_dev,
269 		    "unable to allocate PRB table memory, "
270 		    "error=%d\n", error);
271 		return;
272 	}
273 
274 	error = bus_dmamem_map(sc->sc_dmat,
275 	    &schp->sch_prb_seg, schp->sch_prb_nseg,
276 	    dmasize, &prbp, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
277 	if (error) {
278 		aprint_error_dev(sc->sc_atac.atac_dev,
279 		    "unable to map PRB table memory, "
280 		    "error=%d\n", error);
281 		bus_dmamem_free(sc->sc_dmat,
282 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
283 		return;
284 	}
285 
286 	error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
287 	    BUS_DMA_NOWAIT, &schp->sch_prbd);
288 	if (error) {
289 		aprint_error_dev(sc->sc_atac.atac_dev,
290 		    "unable to create PRB table map, "
291 		    "error=%d\n", error);
292 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
293 		bus_dmamem_free(sc->sc_dmat,
294 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
295 		return;
296 	}
297 
298 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_prbd,
299 	    prbp, dmasize, NULL, BUS_DMA_NOWAIT);
300 	if (error) {
301 		aprint_error_dev(sc->sc_atac.atac_dev,
302 		    "unable to load PRB table map, "
303 		    "error=%d\n", error);
304 		bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
305 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
306 		bus_dmamem_free(sc->sc_dmat,
307 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
308 		return;
309 	}
310 
311 	for (j = 0; j < SIISATA_MAX_SLOTS; j++) {
312 		schp->sch_prb[j] = (struct siisata_prb *)
313 		    ((char *)prbp + SIISATA_CMD_SIZE * j);
314 		schp->sch_bus_prb[j] =
315 		    schp->sch_prbd->dm_segs[0].ds_addr +
316 		    SIISATA_CMD_SIZE * j;
317 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
318 		    SIISATA_NSGE, MAXPHYS, 0,
319 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
320 		    &schp->sch_datad[j]);
321 		if (error) {
322 			aprint_error_dev(sc->sc_atac.atac_dev,
323 			    "couldn't create xfer DMA map, error=%d\n",
324 			    error);
325 			return;
326 		}
327 	}
328 
329 	chp->ch_ndrive = 1;
330 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
331 	    PRX(chp->ch_channel, PRO_SSTATUS), 4, &schp->sch_sstatus) != 0) {
332 		aprint_error_dev(sc->sc_atac.atac_dev,
333 		    "couldn't map port %d SStatus regs\n",
334 		    chp->ch_channel);
335 		return;
336 	}
337 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
338 	    PRX(chp->ch_channel, PRO_SCONTROL), 4, &schp->sch_scontrol) != 0) {
339 		aprint_error_dev(sc->sc_atac.atac_dev,
340 		    "couldn't map port %d SControl regs\n",
341 		    chp->ch_channel);
342 		return;
343 	}
344 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
345 	    PRX(chp->ch_channel, PRO_SERROR), 4, &schp->sch_serror) != 0) {
346 		aprint_error_dev(sc->sc_atac.atac_dev,
347 		    "couldn't map port %d SError regs\n",
348 		    chp->ch_channel);
349 		return;
350 	}
351 
352 	siisata_init_port(sc, port);
353 
354 	ata_channel_attach(chp);
355 
356 	return;
357 }
358 
359 int
360 siisata_detach(struct siisata_softc *sc, int flags)
361 {
362 	struct atac_softc *atac = &sc->sc_atac;
363 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
364 	struct siisata_channel *schp;
365 	struct ata_channel *chp;
366 	int i, j, error;
367 
368 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
369 		schp = &sc->sc_channels[i];
370 		chp = sc->sc_chanarray[i];
371 
372 		if (chp->atabus == NULL)
373 			continue;
374 		if ((error = config_detach(chp->atabus, flags)) != 0)
375 			return error;
376 
377 		for (j = 0; j < SIISATA_MAX_SLOTS; j++)
378 			bus_dmamap_destroy(sc->sc_dmat, schp->sch_datad[j]);
379 
380 		bus_dmamap_unload(sc->sc_dmat, schp->sch_prbd);
381 		bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
382 		bus_dmamem_unmap(sc->sc_dmat, schp->sch_prb[0],
383 		    SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS);
384 		bus_dmamem_free(sc->sc_dmat,
385 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
386 
387 		free(chp->ch_queue, M_DEVBUF);
388 		chp->atabus = NULL;
389 	}
390 
391 	if (adapt->adapt_refcnt != 0)
392 		return EBUSY;
393 
394 	/* leave the chip in reset */
395 	GRWRITE(sc, GR_GC, GR_GC_GLBLRST);
396 
397 	return 0;
398 }
399 
400 void
401 siisata_resume(struct siisata_softc *sc)
402 {
403 	int i;
404 
405 	/* come out of reset state */
406 	GRWRITE(sc, GR_GC, 0);
407 
408 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
409 		siisata_init_port(sc, i);
410 	}
411 
412 }
413 
414 int
415 siisata_intr(void *v)
416 {
417 	struct siisata_softc *sc = v;
418 	uint32_t is;
419 	int i, r = 0;
420 	while ((is = GRREAD(sc, GR_GIS))) {
421 		SIISATA_DEBUG_PRINT(("%s: %s: GR_GIS: 0x%08x\n",
422 		    SIISATANAME(sc), __func__, is), DEBUG_INTR);
423 		r = 1;
424 		for (i = 0; i < sc->sc_atac.atac_nchannels; i++)
425 			if (is & GR_GIS_PXIS(i))
426 				siisata_intr_port(&sc->sc_channels[i]);
427 	}
428 	return r;
429 }
430 
431 static void
432 siisata_intr_port(struct siisata_channel *schp)
433 {
434 	struct siisata_softc *sc;
435 	struct ata_channel *chp;
436 	struct ata_xfer *xfer;
437 	int slot;
438 	uint32_t pss, pis;
439 	uint32_t prbfis;
440 
441 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
442 	chp = &schp->ata_channel;
443 	xfer = chp->ch_queue->active_xfer;
444 	slot = SIISATA_NON_NCQ_SLOT;
445 
446 	SIISATA_DEBUG_PRINT(("%s: %s port %d\n",
447 	    SIISATANAME(sc), __func__, chp->ch_channel), DEBUG_INTR);
448 
449 	pis = PRREAD(sc, PRX(chp->ch_channel, PRO_PIS));
450 
451 	if (pis & PR_PIS_CMDCMPL) {
452 		/* get slot status, clearing completion interrupt */
453 		pss = PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
454 		/* is this expected? */
455 		/* XXX improve */
456 		if ((schp->sch_active_slots & __BIT(slot)) == 0) {
457 			log(LOG_WARNING, "%s: unexpected command "
458 			    "completion on port %d\n",
459 			    SIISATANAME(sc), chp->ch_channel);
460 			return;
461 		}
462 	} else if (pis & PR_PIS_CMDERRR) {
463 		uint32_t ec;
464 
465 		/* emulate a CRC error by default */
466 		chp->ch_status = WDCS_ERR;
467 		chp->ch_error = WDCE_CRC;
468 
469 		ec = PRREAD(sc, PRX(chp->ch_channel, PRO_PCE));
470 		if (ec <= PR_PCE_DATAFISERROR) {
471 			if (ec == PR_PCE_DEVICEERROR) {
472 				/* read in specific information about error */
473 				prbfis = bus_space_read_stream_4(
474 				    sc->sc_prt, sc->sc_prh,
475 		    		    PRSX(chp->ch_channel, slot, PRSO_FIS));
476 				/* set ch_status and ch_error */
477 				satafis_rdh_parse(chp, (uint8_t *)&prbfis);
478 			}
479 			siisata_reinit_port(chp);
480 		} else {
481 			/* okay, we have a "Fatal Error" */
482 			siisata_device_reset(chp);
483 		}
484 	}
485 
486 	/* clear some (ok, all) ints */
487 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
488 
489 	KASSERT(xfer != NULL);
490 	KASSERT(xfer->c_intr != NULL);
491 	xfer->c_intr(chp, xfer, slot);
492 
493 	return;
494 }
495 
496 void
497 siisata_reset_drive(struct ata_drive_datas *drvp, int flags)
498 {
499 	struct ata_channel *chp = drvp->chnl_softc;
500 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
501 	struct siisata_channel *schp = (struct siisata_channel *)chp;
502 	struct siisata_prb *prb;
503 	int slot = SIISATA_NON_NCQ_SLOT;
504 	int i;
505 
506 	/* wait for ready */
507 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
508 		DELAY(10);
509 
510 	prb = schp->sch_prb[slot];
511 	memset(prb, 0, sizeof(struct siisata_prb));
512 	prb->prb_control =
513 	    htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
514 
515 	siisata_activate_prb(schp, slot);
516 
517 	for(i = 0; i < 31000; i++) {
518 		if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
519 		    PR_PXSS(slot))
520 			DELAY(1000);
521 		else
522 			break;
523 	}
524 
525 	siisata_deactivate_prb(schp, slot);
526 
527 	log(LOG_DEBUG, "%s: port %d: ch_status %x ch_error %x\n",
528 	    __func__, chp->ch_channel, chp->ch_status, chp->ch_error);
529 
530 #if 1
531 	/* attempt to downgrade signaling in event of CRC error */
532 	/* XXX should be part of the MI (S)ATA subsystem */
533 	if (chp->ch_status == 0x51 && chp->ch_error == 0x84) {
534 		bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
535 		    SControl_IPM_NONE | SControl_SPD_G1 | SControl_DET_INIT);
536 		DELAY(10);
537 		bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
538 		    SControl_IPM_NONE | SControl_SPD_G1);
539 		DELAY(10);
540 		for (;;) {
541 			if ((bus_space_read_4(sc->sc_prt, schp->sch_sstatus, 0)
542 			    & SStatus_DET_mask) == SStatus_DET_DEV)
543 				break;
544 			DELAY(10);
545 		}
546 	}
547 #endif
548 
549 #if 1
550 	chp->ch_status = 0;
551 	chp->ch_error = 0;
552 #endif
553 
554 	return;
555 }
556 
557 void
558 siisata_reset_channel(struct ata_channel *chp, int flags)
559 {
560 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
561 	struct siisata_channel *schp = (struct siisata_channel *)chp;
562 
563 	SIISATA_DEBUG_PRINT(("%s: %s\n", SIISATANAME(sc), __func__),
564 	    DEBUG_FUNCS);
565 
566 	if (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
567 	    schp->sch_sstatus) != SStatus_DET_DEV) {
568 		log(LOG_CRIT, "%s port %d: reset failed\n",
569 		    SIISATANAME(sc), chp->ch_channel);
570 		/* XXX and then ? */
571 	}
572 	/* wait for ready */
573 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
574 		DELAY(10);
575 	PRWRITE(sc, PRX(chp->ch_channel, PRO_SERROR),
576 	    PRREAD(sc, PRX(chp->ch_channel, PRO_SERROR)));
577 	if (chp->ch_queue->active_xfer) {
578 		chp->ch_queue->active_xfer->c_kill_xfer(chp,
579 		    chp->ch_queue->active_xfer, KILL_RESET);
580 	}
581 
582 	return;
583 }
584 
585 int
586 siisata_ata_addref(struct ata_drive_datas *drvp)
587 {
588 	return 0;
589 }
590 
591 void
592 siisata_ata_delref(struct ata_drive_datas *drvp)
593 {
594 	return;
595 }
596 
597 void
598 siisata_killpending(struct ata_drive_datas *drvp)
599 {
600 	return;
601 }
602 
603 void
604 siisata_probe_drive(struct ata_channel *chp)
605 {
606 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
607 	struct siisata_channel *schp = (struct siisata_channel *)chp;
608 	int i;
609 	int s;
610 	uint32_t sig;
611 	int slot = SIISATA_NON_NCQ_SLOT;
612 	struct siisata_prb *prb;
613 
614 	SIISATA_DEBUG_PRINT(("%s: %s: port %d start\n", SIISATANAME(sc),
615 	    __func__, chp->ch_channel), DEBUG_FUNCS);
616 
617 	/* XXX This should be done by other code. */
618 	for (i = 0; i < chp->ch_ndrive; i++) {
619 		chp->ch_drive[i].chnl_softc = chp;
620 		chp->ch_drive[i].drive = i;
621 	}
622 
623 	switch (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
624 		schp->sch_sstatus)) {
625 	case SStatus_DET_DEV:
626 		/* wait for ready */
627 		while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS))
628 		    & PR_PS_PORT_READY))
629 			DELAY(10);
630 
631 		prb = schp->sch_prb[slot];
632 		memset(prb, 0, sizeof(struct siisata_prb));
633 		prb->prb_control =
634 		    htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
635 
636 		siisata_activate_prb(schp, slot);
637 
638 		for(i = 0; i < 31000; i++) {
639 			if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
640 			    PR_PXSS(slot))
641 				DELAY(1000);
642 			else
643 				break;
644 		}
645 
646 		siisata_deactivate_prb(schp, slot);
647 
648 		/* read the signature out of the FIS */
649 		sig = 0;
650 		sig |= (PRREAD(sc, PRSX(chp->ch_channel, slot,
651 		    PRSO_FIS+0x4)) & 0x00ffffff) << 8;
652 		sig |= PRREAD(sc, PRSX(chp->ch_channel, slot,
653 		    PRSO_FIS+0xc)) & 0xff;
654 
655 		SIISATA_DEBUG_PRINT(("%s: %s: sig=0x%08x\n", SIISATANAME(sc),
656 		    __func__, sig), DEBUG_PROBE);
657 
658 		/* some ATAPI devices have bogus lower two bytes, sigh */
659 		if ((sig & 0xffff0000) == 0xeb140000) {
660 			sig &= 0xffff0000;
661 			sig |= 0x00000101;
662 		}
663 
664 		s = splbio();
665 		switch (sig) {
666 		case 0xeb140101:
667 			chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
668 			break;
669 		case 0x00000101:
670 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
671 			break;
672 		default:
673 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
674 			aprint_verbose_dev(sc->sc_atac.atac_dev,
675 			    "Unrecognized signature 0x%08x on port %d. "
676 			    "Assuming it's a disk.\n", sig, chp->ch_channel);
677 			break;
678 		}
679 		splx(s);
680 		break;
681 	default:
682 		break;
683 	}
684 
685 	SIISATA_DEBUG_PRINT(("%s: %s: port %d done\n", SIISATANAME(sc),
686 	    __func__, chp->ch_channel), DEBUG_PROBE);
687 	return;
688 }
689 
690 void
691 siisata_setup_channel(struct ata_channel *chp)
692 {
693 	return;
694 }
695 
696 int
697 siisata_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
698 {
699 	struct ata_channel *chp = drvp->chnl_softc;
700 	struct ata_xfer *xfer;
701 	int ret;
702 	int s;
703 
704 	SIISATA_DEBUG_PRINT(("%s: %s begins\n",
705 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
706 	    DEBUG_FUNCS);
707 
708 	xfer = ata_get_xfer(ata_c->flags & AT_WAIT ?
709 	    ATAXF_CANSLEEP : ATAXF_NOSLEEP);
710 	if (xfer == NULL)
711 		return ATACMD_TRY_AGAIN;
712 	if (ata_c->flags & AT_POLL)
713 		xfer->c_flags |= C_POLL;
714 	if (ata_c->flags & AT_WAIT)
715 		xfer->c_flags |= C_WAIT;
716 	xfer->c_drive = drvp->drive;
717 	xfer->c_databuf = ata_c->data;
718 	xfer->c_bcount = ata_c->bcount;
719 	xfer->c_cmd = ata_c;
720 	xfer->c_start = siisata_cmd_start;
721 	xfer->c_intr = siisata_cmd_complete;
722 	xfer->c_kill_xfer = siisata_cmd_kill_xfer;
723 	s = splbio();
724 	ata_exec_xfer(chp, xfer);
725 #ifdef DIAGNOSTIC
726 	if ((ata_c->flags & AT_POLL) != 0 &&
727 	    (ata_c->flags & AT_DONE) == 0)
728 		panic("%s: polled command not done", __func__);
729 #endif
730 	if (ata_c->flags & AT_DONE) {
731 		ret = ATACMD_COMPLETE;
732 	} else {
733 		if (ata_c->flags & AT_WAIT) {
734 			while ((ata_c->flags & AT_DONE) == 0) {
735 				SIISATA_DEBUG_PRINT(("%s: %s: sleeping\n",
736 				    SIISATANAME(
737 				    (struct siisata_softc *)chp->ch_atac),
738 				    __func__), DEBUG_FUNCS);
739 				tsleep(ata_c, PRIBIO, "siicmd", 0);
740 			}
741 			ret = ATACMD_COMPLETE;
742 		} else {
743 			ret = ATACMD_QUEUED;
744 		}
745 	}
746 	splx(s);
747 	SIISATA_DEBUG_PRINT( ("%s: %s ends\n",
748 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
749 	    DEBUG_FUNCS);
750 	return ret;
751 }
752 
753 void
754 siisata_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
755 {
756 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
757 	struct siisata_channel *schp = (struct siisata_channel *)chp;
758 	struct ata_command *ata_c = xfer->c_cmd;
759 	int slot = SIISATA_NON_NCQ_SLOT;
760 	struct siisata_prb *prb;
761 	int i;
762 
763 	SIISATA_DEBUG_PRINT(("%s: %s port %d, slot %d\n",
764 	    SIISATANAME(sc), __func__, chp->ch_channel, slot), DEBUG_FUNCS);
765 
766 	chp->ch_status = 0;
767 	chp->ch_error = 0;
768 
769 	prb = schp->sch_prb[slot];
770 	memset(prb, 0, sizeof(struct siisata_prb));
771 
772 	satafis_rhd_construct_cmd(ata_c, prb->prb_fis);
773 
774 	memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
775 
776 	if (siisata_dma_setup(chp, slot,
777 	    (ata_c->flags & (AT_READ | AT_WRITE)) ? ata_c->data : NULL,
778 	    ata_c->bcount,
779 	    (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
780 		ata_c->flags |= AT_DF;
781 		siisata_cmd_complete(chp, xfer, slot);
782 		return;
783 	}
784 
785 	if (xfer->c_flags & C_POLL) {
786 		/* polled command, disable interrupts */
787 		prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
788 	}
789 
790 	/* go for it */
791 	siisata_activate_prb(schp, slot);
792 
793 	if ((ata_c->flags & AT_POLL) == 0) {
794 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
795 		callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
796 		    siisata_timeout, chp);
797 		goto out;
798 	}
799 
800 	/*
801 	 * polled command
802 	 */
803 	for (i = 0; i < ata_c->timeout / 10; i++) {
804 		if (ata_c->flags & AT_DONE)
805 			break;
806 		siisata_intr_port(schp);
807 		DELAY(1000);
808 	}
809 
810 	if ((ata_c->flags & AT_DONE) == 0) {
811 		ata_c->flags |= AT_TIMEOU;
812 		siisata_cmd_complete(chp, xfer, slot);
813 	}
814 
815 	/* reenable interrupts */
816 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
817 out:
818 	SIISATA_DEBUG_PRINT(
819 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
820 	return;
821 }
822 
823 void
824 siisata_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
825     int reason)
826 {
827 	int slot = SIISATA_NON_NCQ_SLOT;
828 
829 	struct ata_command *ata_c = xfer->c_cmd;
830 	switch (reason) {
831 	case KILL_GONE:
832 		ata_c->flags |= AT_GONE;
833 		break;
834 	case KILL_RESET:
835 		ata_c->flags |= AT_RESET;
836 		break;
837 	default:
838 		panic("%s: port %d: unknown reason %d",
839 		   __func__, chp->ch_channel, reason);
840 	}
841 	siisata_cmd_done(chp, xfer, slot);
842 }
843 
844 int
845 siisata_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
846 {
847 	struct ata_command *ata_c = xfer->c_cmd;
848 #ifdef SIISATA_DEBUG
849 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
850 #endif
851 
852 	SIISATA_DEBUG_PRINT(
853 	    ("%s: %s\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
854 
855 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
856 	if (xfer->c_flags & C_TIMEOU)
857 		ata_c->flags |= AT_TIMEOU;
858 	else
859 		callout_stop(&chp->ch_callout);
860 
861 	if (chp->ch_status & WDCS_BSY) {
862 		ata_c->flags |= AT_TIMEOU;
863 	} else if (chp->ch_status & WDCS_ERR) {
864 		ata_c->r_error = chp->ch_error;
865 		ata_c->flags |= AT_ERROR;
866 	}
867 
868 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
869 		siisata_cmd_kill_xfer(chp, xfer, KILL_GONE);
870 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
871 		wakeup(&chp->ch_queue->active_xfer);
872 		return 0;
873 	} else
874 		siisata_cmd_done(chp, xfer, slot);
875 
876 	return 0;
877 }
878 
879 void
880 siisata_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
881 {
882 	uint32_t fis[howmany(RDH_FISLEN,sizeof(uint32_t))];
883 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
884 	struct siisata_channel *schp = (struct siisata_channel *)chp;
885 	struct ata_command *ata_c = xfer->c_cmd;
886 	uint16_t *idwordbuf;
887 	int i;
888 
889 	SIISATA_DEBUG_PRINT(
890 	    ("%s: %s.\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
891 
892 	siisata_deactivate_prb(schp, slot);
893 
894 	if (ata_c->flags & (AT_READ | AT_WRITE)) {
895 		bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
896 		    schp->sch_datad[slot]->dm_mapsize,
897 		    (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
898 		    BUS_DMASYNC_POSTWRITE);
899 		bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
900 	}
901 
902 	if (ata_c->flags & AT_READREG) {
903 		bus_space_read_region_stream_4(sc->sc_prt, sc->sc_prh,
904 		    PRSX(chp->ch_channel, slot, PRSO_FIS),
905 		    fis, __arraycount(fis));
906 		satafis_rdh_cmd_readreg(ata_c, (uint8_t *)fis);
907 	}
908 
909 	/* correct the endianess of IDENTIFY data */
910 	if (ata_c->r_command == WDCC_IDENTIFY ||
911 	    ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
912 		idwordbuf = xfer->c_databuf;
913 		for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
914 			idwordbuf[i] = le16toh(idwordbuf[i]);
915 		}
916 	}
917 
918 	ata_c->flags |= AT_DONE;
919 	if (PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC)))
920 		ata_c->flags |= AT_XFDONE;
921 
922 	chp->ch_queue->active_xfer = NULL;
923 	ata_free_xfer(chp, xfer);
924 	if (ata_c->flags & AT_WAIT)
925 		wakeup(ata_c);
926 	else if (ata_c->callback)
927 		ata_c->callback(ata_c->callback_arg);
928 	atastart(chp);
929 	return;
930 }
931 
932 int
933 siisata_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
934 {
935 	struct ata_channel *chp = drvp->chnl_softc;
936 	struct ata_xfer *xfer;
937 
938 	SIISATA_DEBUG_PRINT( ("%s: %s.\n",
939 	    SIISATANAME((struct siisata_softc *)chp->ch_atac),
940 	    __func__), DEBUG_FUNCS);
941 
942 	xfer = ata_get_xfer(ATAXF_NOSLEEP);
943 	if (xfer == NULL)
944 		return ATACMD_TRY_AGAIN;
945 	if (ata_bio->flags & ATA_POLL)
946 		xfer->c_flags |= C_POLL;
947 	xfer->c_drive = drvp->drive;
948 	xfer->c_cmd = ata_bio;
949 	xfer->c_databuf = ata_bio->databuf;
950 	xfer->c_bcount = ata_bio->bcount;
951 	xfer->c_start = siisata_bio_start;
952 	xfer->c_intr = siisata_bio_complete;
953 	xfer->c_kill_xfer = siisata_bio_kill_xfer;
954 	ata_exec_xfer(chp, xfer);
955 	return (ata_bio->flags & ATA_ITSDONE) ?
956 	    ATACMD_COMPLETE : ATACMD_QUEUED;
957 }
958 
959 void
960 siisata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
961 {
962 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
963 	struct siisata_channel *schp = (struct siisata_channel *)chp;
964 	struct siisata_prb *prb;
965 	struct ata_bio *ata_bio = xfer->c_cmd;
966 	int slot = SIISATA_NON_NCQ_SLOT;
967 	int i;
968 
969 	SIISATA_DEBUG_PRINT(
970 	    ("%s: %s port %d, slot %d\n",
971 	    SIISATANAME(sc), __func__, chp->ch_channel, slot),
972 	    DEBUG_FUNCS);
973 
974 	chp->ch_status = 0;
975 	chp->ch_error = 0;
976 
977 	prb = schp->sch_prb[slot];
978 	memset(prb, 0, sizeof(struct siisata_prb));
979 
980 	satafis_rhd_construct_bio(xfer, prb->prb_fis);
981 
982 	memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
983 
984 	if (siisata_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
985 	    (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
986 		ata_bio->error = ERR_DMA;
987 		ata_bio->r_error = 0;
988 		siisata_bio_complete(chp, xfer, slot);
989 		return;
990 	}
991 
992 	if (xfer->c_flags & C_POLL) {
993 		/* polled command, disable interrupts */
994 		prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
995 	}
996 
997 	siisata_activate_prb(schp, slot);
998 
999 	if ((ata_bio->flags & ATA_POLL) == 0) {
1000 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1001 		callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
1002 		    siisata_timeout, chp);
1003 		goto out;
1004 	}
1005 
1006 	/*
1007 	 * polled command
1008 	 */
1009 	for (i = 0; i < ATA_DELAY / 10; i++) {
1010 		if (ata_bio->flags & ATA_ITSDONE)
1011 			break;
1012 		siisata_intr_port(schp);
1013 		DELAY(1000);
1014 	}
1015 
1016 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
1017 out:
1018 	SIISATA_DEBUG_PRINT(
1019 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
1020 	return;
1021 }
1022 
1023 void
1024 siisata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1025     int reason)
1026 {
1027 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1028 	struct ata_bio *ata_bio = xfer->c_cmd;
1029 	int drive = xfer->c_drive;
1030 	int slot = SIISATA_NON_NCQ_SLOT;
1031 
1032 	SIISATA_DEBUG_PRINT(("%s: %s: port %d\n",
1033 	    SIISATANAME((struct siisata_softc *)chp->ch_atac),
1034 	    __func__, chp->ch_channel), DEBUG_FUNCS);
1035 
1036 	siisata_deactivate_prb(schp, slot);
1037 
1038 	ata_free_xfer(chp, xfer);
1039 	ata_bio->flags |= ATA_ITSDONE;
1040 	switch (reason) {
1041 	case KILL_GONE:
1042 		ata_bio->error = ERR_NODEV;
1043 		break;
1044 	case KILL_RESET:
1045 		ata_bio->error = ERR_RESET;
1046 		break;
1047 	default:
1048 		panic("%s: port %d: unknown reason %d",
1049 		   __func__, chp->ch_channel, reason);
1050 	}
1051 	ata_bio->r_error = WDCE_ABRT;
1052 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1053 }
1054 
1055 int
1056 siisata_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
1057 {
1058 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1059 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1060 	struct ata_bio *ata_bio = xfer->c_cmd;
1061 	int drive = xfer->c_drive;
1062 
1063 	schp->sch_active_slots &= ~__BIT(slot);
1064 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
1065 	if (xfer->c_flags & C_TIMEOU) {
1066 		ata_bio->error = TIMEOUT;
1067 	} else {
1068 		callout_stop(&chp->ch_callout);
1069 		ata_bio->error = NOERROR;
1070 	}
1071 
1072 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1073 	    schp->sch_datad[slot]->dm_mapsize,
1074 	    (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1075 	    BUS_DMASYNC_POSTWRITE);
1076 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
1077 
1078 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1079 		siisata_bio_kill_xfer(chp, xfer, KILL_GONE);
1080 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1081 		wakeup(&chp->ch_queue->active_xfer);
1082 		return 0;
1083 	}
1084 
1085 	chp->ch_queue->active_xfer = NULL;
1086 	ata_free_xfer(chp, xfer);
1087 	ata_bio->flags |= ATA_ITSDONE;
1088 	if (chp->ch_status & WDCS_DWF) {
1089 		ata_bio->error = ERR_DF;
1090 	} else if (chp->ch_status & WDCS_ERR) {
1091 		ata_bio->error = ERROR;
1092 		ata_bio->r_error = chp->ch_error;
1093 	} else if (chp->ch_status & WDCS_CORR)
1094 		ata_bio->flags |= ATA_CORR;
1095 
1096 	SIISATA_DEBUG_PRINT(("%s: %s bcount: %ld", SIISATANAME(sc),
1097 	    __func__, ata_bio->bcount), DEBUG_XFERS);
1098 	if (ata_bio->error == NOERROR) {
1099 		if (ata_bio->flags & ATA_READ)
1100 			ata_bio->bcount -=
1101 			    PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC));
1102 		else
1103 			ata_bio->bcount = 0;
1104 	}
1105 	SIISATA_DEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1106 	if (ata_bio->flags & ATA_POLL)
1107 		return 1;
1108 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1109 	atastart(chp);
1110 	return 0;
1111 }
1112 
1113 void
1114 siisata_timeout(void *v)
1115 {
1116 	struct ata_channel *chp = (struct ata_channel *)v;
1117 	struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1118 	int slot = SIISATA_NON_NCQ_SLOT;
1119 	int s = splbio();
1120 	SIISATA_DEBUG_PRINT(("%s: %p\n", __func__, xfer), DEBUG_INTR);
1121 	if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1122 		xfer->c_flags |= C_TIMEOU;
1123 		xfer->c_intr(chp, xfer, slot);
1124 	}
1125 	splx(s);
1126 }
1127 
1128 static int
1129 siisata_dma_setup(struct ata_channel *chp, int slot, void *data,
1130     size_t count, int op)
1131 {
1132 
1133 	int error, seg;
1134 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1135 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1136 
1137 	struct siisata_prb *prbp;
1138 
1139 	prbp = schp->sch_prb[slot];
1140 
1141 	if (data == NULL) {
1142 		goto end;
1143 	}
1144 
1145 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_datad[slot],
1146 	    data, count, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1147 	if (error) {
1148 		log(LOG_ERR, "%s port %d: "
1149 		    "failed to load xfer in slot %d: error %d\n",
1150 		    SIISATANAME(sc), chp->ch_channel, slot, error);
1151 		return error;
1152 	}
1153 
1154 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1155 	    schp->sch_datad[slot]->dm_mapsize,
1156 	    (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1157 
1158 	/* make sure it's clean */
1159 	memset(prbp->prb_sge, 0, SIISATA_NSGE * sizeof(struct siisata_prb));
1160 
1161 	SIISATA_DEBUG_PRINT(("%s: %d segs, %ld count\n", __func__,
1162 	    schp->sch_datad[slot]->dm_nsegs, (long unsigned int) count),
1163 	    DEBUG_FUNCS | DEBUG_DEBUG);
1164 
1165 	for (seg = 0; seg < schp->sch_datad[slot]->dm_nsegs; seg++) {
1166 		prbp->prb_sge[seg].sge_da =
1167 		    htole64(schp->sch_datad[slot]->dm_segs[seg].ds_addr);
1168 		prbp->prb_sge[seg].sge_dc =
1169 		    htole32(schp->sch_datad[slot]->dm_segs[seg].ds_len);
1170 		prbp->prb_sge[seg].sge_flags = htole32(0);
1171 	}
1172 	prbp->prb_sge[seg - 1].sge_flags |= htole32(SGE_FLAG_TRM);
1173 end:
1174 	return 0;
1175 }
1176 
1177 static void
1178 siisata_activate_prb(struct siisata_channel *schp, int slot)
1179 {
1180 	struct siisata_softc *sc;
1181 	bus_size_t offset;
1182 	uint64_t pprb;
1183 
1184 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1185 
1186 	KASSERTMSG((schp->sch_active_slots & __BIT(slot)) != __BIT(slot),
1187 	    ("%s: trying to activate active slot %d", SIISATANAME(sc), slot));
1188 
1189 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_PREWRITE);
1190 	/* keep track of what's going on */
1191 	schp->sch_active_slots |= __BIT(slot);
1192 
1193 	offset = PRO_CARX(schp->ata_channel.ch_channel, slot);
1194 
1195 	pprb = schp->sch_bus_prb[slot];
1196 
1197 	PRWRITE(sc, offset + 0, pprb >>  0);
1198 	PRWRITE(sc, offset + 4, pprb >> 32);
1199 }
1200 
1201 static void
1202 siisata_deactivate_prb(struct siisata_channel *schp, int slot)
1203 {
1204 	struct siisata_softc *sc;
1205 
1206 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1207 
1208 	KASSERTMSG((schp->sch_active_slots & __BIT(slot)) != 0,
1209 	    ("%s: trying to deactivate inactive slot %d", SIISATANAME(sc),
1210 	    slot));
1211 
1212 	schp->sch_active_slots &= ~__BIT(slot); /* mark free */
1213 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_POSTWRITE);
1214 }
1215 
1216 static void
1217 siisata_reinit_port(struct ata_channel *chp)
1218 {
1219 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1220 
1221 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_PORT_INITIALIZE);
1222 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
1223 		DELAY(10);
1224 }
1225 
1226 static void
1227 siisata_device_reset(struct ata_channel *chp)
1228 {
1229 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1230 
1231 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_DEVICE_RESET);
1232 	while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
1233 		DELAY(10);
1234 }
1235 
1236 
1237 #if NATAPIBUS > 0
1238 void
1239 siisata_atapibus_attach(struct atabus_softc *ata_sc)
1240 {
1241 	struct ata_channel *chp = ata_sc->sc_chan;
1242 	struct atac_softc *atac = chp->ch_atac;
1243 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1244 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
1245 
1246 	/*
1247 	 * Fill in the scsipi_adapter.
1248 	 */
1249 	adapt->adapt_dev = atac->atac_dev;
1250 	adapt->adapt_nchannels = atac->atac_nchannels;
1251 	adapt->adapt_request = siisata_atapi_scsipi_request;
1252 	adapt->adapt_minphys = siisata_atapi_minphys;
1253 	atac->atac_atapi_adapter.atapi_probe_device =
1254 	    siisata_atapi_probe_device;
1255 
1256 	/*
1257 	 * Fill in the scsipi_channel.
1258 	 */
1259 	memset(chan, 0, sizeof(*chan));
1260 	chan->chan_adapter = adapt;
1261 	chan->chan_bustype = &siisata_atapi_bustype;
1262 	chan->chan_channel = chp->ch_channel;
1263 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
1264 	chan->chan_openings = 1;
1265 	chan->chan_max_periph = 1;
1266 	chan->chan_ntargets = 1;
1267 	chan->chan_nluns = 1;
1268 
1269 	chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
1270 	    atapiprint);
1271 }
1272 
1273 void
1274 siisata_atapi_minphys(struct buf *bp)
1275 {
1276 	if (bp->b_bcount > MAXPHYS)
1277 		bp->b_bcount = MAXPHYS;
1278 	minphys(bp);
1279 }
1280 
1281 /*
1282  * Kill off all pending xfers for a periph.
1283  *
1284  * Must be called at splbio().
1285  */
1286 void
1287 siisata_atapi_kill_pending(struct scsipi_periph *periph)
1288 {
1289 	struct atac_softc *atac =
1290 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
1291 	struct ata_channel *chp =
1292 	    atac->atac_channels[periph->periph_channel->chan_channel];
1293 
1294 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
1295 }
1296 
1297 void
1298 siisata_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1299     int reason)
1300 {
1301 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1302 
1303 	/* remove this command from xfer queue */
1304 	switch (reason) {
1305 	case KILL_GONE:
1306 		sc_xfer->error = XS_DRIVER_STUFFUP;
1307 		break;
1308 	case KILL_RESET:
1309 		sc_xfer->error = XS_RESET;
1310 		break;
1311 	default:
1312 		panic("%s: port %d: unknown reason %d",
1313 		   __func__, chp->ch_channel, reason);
1314 	}
1315 	ata_free_xfer(chp, xfer);
1316 	scsipi_done(sc_xfer);
1317 }
1318 
1319 void
1320 siisata_atapi_probe_device(struct atapibus_softc *sc, int target)
1321 {
1322 	struct scsipi_channel *chan = sc->sc_channel;
1323 	struct scsipi_periph *periph;
1324 	struct ataparams ids;
1325 	struct ataparams *id = &ids;
1326 	struct siisata_softc *siic =
1327 	    device_private(chan->chan_adapter->adapt_dev);
1328 	struct atac_softc *atac = &siic->sc_atac;
1329 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
1330 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
1331 	struct scsipibus_attach_args sa;
1332 	char serial_number[21], model[41], firmware_revision[9];
1333 	int s;
1334 
1335 	/* skip if already attached */
1336 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
1337 		return;
1338 
1339 	/* if no ATAPI device detected at attach time, skip */
1340 	if ((drvp->drive_flags & DRIVE_ATAPI) == 0) {
1341 		SIISATA_DEBUG_PRINT(("%s: drive %d "
1342 		    "not present\n", __func__, target), DEBUG_PROBE);
1343 		return;
1344 	}
1345 
1346 	/* Some ATAPI devices need a bit more time after software reset. */
1347 	DELAY(5000);
1348 	if (ata_get_params(drvp, AT_WAIT, id) == 0) {
1349 #ifdef ATAPI_DEBUG_PROBE
1350 		log(LOG_DEBUG, "%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
1351 		    device_xname(sc->sc_dev), target,
1352 		    id->atap_config & ATAPI_CFG_CMD_MASK,
1353 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
1354 #endif
1355 		periph = scsipi_alloc_periph(M_NOWAIT);
1356 		if (periph == NULL) {
1357 			aprint_error_dev(sc->sc_dev,
1358 			    "%s: unable to allocate periph for "
1359 			    "channel %d drive %d\n", __func__,
1360 			    chp->ch_channel, target);
1361 			return;
1362 		}
1363 		periph->periph_dev = NULL;
1364 		periph->periph_channel = chan;
1365 		periph->periph_switch = &atapi_probe_periphsw;
1366 		periph->periph_target = target;
1367 		periph->periph_lun = 0;
1368 		periph->periph_quirks = PQUIRK_ONLYBIG;
1369 
1370 #ifdef SCSIPI_DEBUG
1371 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
1372 		    SCSIPI_DEBUG_TARGET == target)
1373 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
1374 #endif
1375 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
1376 		if (id->atap_config & ATAPI_CFG_REMOV)
1377 			periph->periph_flags |= PERIPH_REMOVABLE;
1378 		if (periph->periph_type == T_SEQUENTIAL) {
1379 			s = splbio();
1380 			drvp->drive_flags |= DRIVE_ATAPIST;
1381 			splx(s);
1382 		}
1383 
1384 		sa.sa_periph = periph;
1385 		sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
1386 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
1387 		    T_REMOV : T_FIXED;
1388 		scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
1389 		scsipi_strvis((u_char *)serial_number, 20,
1390 		    id->atap_serial, 20);
1391 		scsipi_strvis((u_char *)firmware_revision, 8,
1392 		    id->atap_revision, 8);
1393 		sa.sa_inqbuf.vendor = model;
1394 		sa.sa_inqbuf.product = serial_number;
1395 		sa.sa_inqbuf.revision = firmware_revision;
1396 
1397 		/*
1398 		 * Determine the operating mode capabilities of the device.
1399 		 */
1400 		if ((id->atap_config & ATAPI_CFG_CMD_MASK)
1401 		    == ATAPI_CFG_CMD_16) {
1402 			periph->periph_cap |= PERIPH_CAP_CMD16;
1403 
1404 			/* configure port for packet length */
1405 			PRWRITE(siic, PRX(chp->ch_channel, PRO_PCS),
1406 			    PR_PC_PACKET_LENGTH);
1407 		} else {
1408 			PRWRITE(siic, PRX(chp->ch_channel, PRO_PCC),
1409 			    PR_PC_PACKET_LENGTH);
1410 		}
1411 
1412 		/* XXX This is gross. */
1413 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
1414 
1415 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
1416 
1417 		if (drvp->drv_softc)
1418 			ata_probe_caps(drvp);
1419 		else {
1420 			s = splbio();
1421 			drvp->drive_flags &= ~DRIVE_ATAPI;
1422 			splx(s);
1423 		}
1424 	} else {
1425 		SIISATA_DEBUG_PRINT(("%s: ATAPI_IDENTIFY_DEVICE "
1426 		    "failed for drive %s:%d:%d: error 0x%x\n",
1427 		    __func__, SIISATANAME(siic), chp->ch_channel, target,
1428 		    chp->ch_error), DEBUG_PROBE);
1429 		s = splbio();
1430 		drvp->drive_flags &= ~DRIVE_ATAPI;
1431 		splx(s);
1432 	}
1433 }
1434 
1435 void
1436 siisata_atapi_scsipi_request(struct scsipi_channel *chan,
1437     scsipi_adapter_req_t req, void *arg)
1438 {
1439 	struct scsipi_adapter *adapt = chan->chan_adapter;
1440 	struct scsipi_periph *periph;
1441 	struct scsipi_xfer *sc_xfer;
1442 	struct siisata_softc *sc = device_private(adapt->adapt_dev);
1443 	struct atac_softc *atac = &sc->sc_atac;
1444 	struct ata_xfer *xfer;
1445 	int channel = chan->chan_channel;
1446 	int drive, s;
1447 
1448 	switch (req) {
1449 	case ADAPTER_REQ_RUN_XFER:
1450 		sc_xfer = arg;
1451 		periph = sc_xfer->xs_periph;
1452 		drive = periph->periph_target;
1453 
1454 		SIISATA_DEBUG_PRINT(("%s: %s:%d:%d\n", __func__,
1455 		    device_xname(atac->atac_dev), channel, drive),
1456 		    DEBUG_XFERS);
1457 
1458 		if (!device_is_active(atac->atac_dev)) {
1459 			sc_xfer->error = XS_DRIVER_STUFFUP;
1460 			scsipi_done(sc_xfer);
1461 			return;
1462 		}
1463 		xfer = ata_get_xfer(ATAXF_NOSLEEP);
1464 		if (xfer == NULL) {
1465 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
1466 			scsipi_done(sc_xfer);
1467 			return;
1468 		}
1469 
1470 		if (sc_xfer->xs_control & XS_CTL_POLL)
1471 			xfer->c_flags |= C_POLL;
1472 		xfer->c_drive = drive;
1473 		xfer->c_flags |= C_ATAPI;
1474 		xfer->c_cmd = sc_xfer;
1475 		xfer->c_databuf = sc_xfer->data;
1476 		xfer->c_bcount = sc_xfer->datalen;
1477 		xfer->c_start = siisata_atapi_start;
1478 		xfer->c_intr = siisata_atapi_complete;
1479 		xfer->c_kill_xfer = siisata_atapi_kill_xfer;
1480 		xfer->c_dscpoll = 0;
1481 		s = splbio();
1482 		ata_exec_xfer(atac->atac_channels[channel], xfer);
1483 #ifdef DIAGNOSTIC
1484 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
1485 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
1486 			panic("%s: polled command not done", __func__);
1487 #endif
1488 		splx(s);
1489 		return;
1490 
1491 	default:
1492 		/* Not supported, nothing to do. */
1493 		;
1494 	}
1495 }
1496 
1497 void
1498 siisata_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
1499 {
1500 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1501 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1502 	struct siisata_prb *prbp;
1503 
1504 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1505 
1506 	int slot = SIISATA_NON_NCQ_SLOT;
1507 	int i;
1508 
1509 	SIISATA_DEBUG_PRINT( ("%s: %s:%d:%d, scsi flags 0x%x\n", __func__,
1510 	    SIISATANAME(sc), chp->ch_channel,
1511 	    chp->ch_drive[xfer->c_drive].drive, sc_xfer->xs_control),
1512 	    DEBUG_XFERS);
1513 
1514 	chp->ch_status = 0;
1515 	chp->ch_error = 0;
1516 
1517 	prbp = schp->sch_prb[slot];
1518 	memset(prbp, 0, sizeof(struct siisata_prb));
1519 
1520 
1521 	/* fill in direction for ATAPI command */
1522 	if ((sc_xfer->xs_control & XS_CTL_DATA_IN))
1523 		prbp->prb_control |= htole16(PRB_CF_PACKET_READ);
1524 	if ((sc_xfer->xs_control & XS_CTL_DATA_OUT))
1525 		prbp->prb_control |= htole16(PRB_CF_PACKET_WRITE);
1526 
1527 	satafis_rhd_construct_atapi(xfer, prbp->prb_fis);
1528 
1529 	/* copy over ATAPI command */
1530 	memcpy(prbp->prb_atapi, sc_xfer->cmd, sc_xfer->cmdlen);
1531 
1532 	if (siisata_dma_setup(chp, slot,
1533 		(sc_xfer->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ?
1534 		xfer->c_databuf : NULL,
1535 		xfer->c_bcount,
1536 		(sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1537 		BUS_DMA_READ : BUS_DMA_WRITE)
1538 	)
1539 		panic("%s", __func__);
1540 
1541 	if (xfer->c_flags & C_POLL) {
1542 		/* polled command, disable interrupts */
1543 		prbp->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
1544 	}
1545 
1546 	siisata_activate_prb(schp, slot);
1547 
1548 	if ((xfer->c_flags & C_POLL) == 0) {
1549 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1550 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
1551 		    siisata_timeout, chp);
1552 		goto out;
1553 	}
1554 
1555 	/*
1556 	 * polled command
1557 	 */
1558 	for (i = 0; i < ATA_DELAY / 10; i++) {
1559 		if (sc_xfer->xs_status & XS_STS_DONE)
1560 			break;
1561 		siisata_intr_port(schp);
1562 		DELAY(1000);
1563 	}
1564 	if ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
1565 		sc_xfer->error = XS_TIMEOUT;
1566 		siisata_atapi_complete(chp, xfer, slot);
1567 	}
1568 	/* reenable interrupts */
1569 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
1570 out:
1571 	SIISATA_DEBUG_PRINT(
1572 	    ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
1573 	return;
1574 }
1575 
1576 int
1577 siisata_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer,
1578     int slot)
1579 {
1580 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1581 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1582 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1583 
1584 	SIISATA_DEBUG_PRINT(
1585 	    ("%s: %s()\n", SIISATANAME(sc), __func__), DEBUG_INTR);
1586 
1587 	/* this comamnd is not active any more */
1588 	schp->sch_active_slots &= ~__BIT(slot);
1589 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
1590 	if (xfer->c_flags & C_TIMEOU) {
1591 		sc_xfer->error = XS_TIMEOUT;
1592 	} else {
1593 		callout_stop(&chp->ch_callout);
1594 		sc_xfer->error = XS_NOERROR;
1595 	}
1596 
1597 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1598 	    schp->sch_datad[slot]->dm_mapsize,
1599 	    (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1600 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1601 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
1602 
1603 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1604 		siisata_atapi_kill_xfer(chp, xfer, KILL_GONE);
1605 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1606 		wakeup(&chp->ch_queue->active_xfer);
1607 		return 0; /* XXX verify */
1608 	}
1609 
1610 	chp->ch_queue->active_xfer = NULL;
1611 	ata_free_xfer(chp, xfer);
1612 	sc_xfer->resid = sc_xfer->datalen;
1613 	sc_xfer->resid -= PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC));
1614 	SIISATA_DEBUG_PRINT(("%s: %s datalen %d resid %d\n", SIISATANAME(sc),
1615 	    __func__, sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
1616 	if ((chp->ch_status & WDCS_ERR) &&
1617 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1618 	    sc_xfer->resid == sc_xfer->datalen)) {
1619 		sc_xfer->error = XS_SHORTSENSE;
1620 		sc_xfer->sense.atapi_sense = chp->ch_error;
1621 		if ((sc_xfer->xs_periph->periph_quirks &
1622 		    PQUIRK_NOSENSE) == 0) {
1623 			/* request sense */
1624 			sc_xfer->error = XS_BUSY;
1625 			sc_xfer->status = SCSI_CHECK;
1626 		}
1627 	}
1628 	scsipi_done(sc_xfer);
1629 	atastart(chp);
1630 	return 0; /* XXX verify */
1631 }
1632 
1633 #endif /* NATAPIBUS */
1634