xref: /netbsd-src/sys/dev/ic/ahcisata_core.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: ahcisata_core.c,v 1.26 2010/04/07 17:51:16 jakllsch Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ahcisata_core.c,v 1.26 2010/04/07 17:51:16 jakllsch Exp $");
30 
31 #include <sys/types.h>
32 #include <sys/malloc.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/disklabel.h>
37 #include <sys/proc.h>
38 #include <sys/buf.h>
39 
40 #include <uvm/uvm_extern.h>
41 
42 #include <dev/ata/atareg.h>
43 #include <dev/ata/satavar.h>
44 #include <dev/ata/satareg.h>
45 #include <dev/ata/satafisvar.h>
46 #include <dev/ata/satafisreg.h>
47 #include <dev/ic/ahcisatavar.h>
48 
49 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
50 
51 #include "atapibus.h"
52 
53 #ifdef AHCI_DEBUG
54 int ahcidebug_mask = 0x0;
55 #endif
56 
57 void ahci_probe_drive(struct ata_channel *);
58 void ahci_setup_channel(struct ata_channel *);
59 
60 int  ahci_ata_bio(struct ata_drive_datas *, struct ata_bio *);
61 void ahci_reset_drive(struct ata_drive_datas *, int);
62 void ahci_reset_channel(struct ata_channel *, int);
63 int  ahci_exec_command(struct ata_drive_datas *, struct ata_command *);
64 int  ahci_ata_addref(struct ata_drive_datas *);
65 void ahci_ata_delref(struct ata_drive_datas *);
66 void ahci_killpending(struct ata_drive_datas *);
67 
68 void ahci_cmd_start(struct ata_channel *, struct ata_xfer *);
69 int  ahci_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
70 void ahci_cmd_done(struct ata_channel *, struct ata_xfer *, int);
71 void ahci_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
72 void ahci_bio_start(struct ata_channel *, struct ata_xfer *);
73 int  ahci_bio_complete(struct ata_channel *, struct ata_xfer *, int);
74 void ahci_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int) ;
75 void ahci_channel_stop(struct ahci_softc *, struct ata_channel *, int);
76 void ahci_channel_start(struct ahci_softc *, struct ata_channel *);
77 void ahci_timeout(void *);
78 int  ahci_dma_setup(struct ata_channel *, int, void *, size_t, int);
79 
80 #if NATAPIBUS > 0
81 void ahci_atapibus_attach(struct atabus_softc *);
82 void ahci_atapi_kill_pending(struct scsipi_periph *);
83 void ahci_atapi_minphys(struct buf *);
84 void ahci_atapi_scsipi_request(struct scsipi_channel *,
85     scsipi_adapter_req_t, void *);
86 void ahci_atapi_start(struct ata_channel *, struct ata_xfer *);
87 int  ahci_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
88 void ahci_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
89 void ahci_atapi_probe_device(struct atapibus_softc *, int);
90 
91 static const struct scsipi_bustype ahci_atapi_bustype = {
92 	SCSIPI_BUSTYPE_ATAPI,
93 	atapi_scsipi_cmd,
94 	atapi_interpret_sense,
95 	atapi_print_addr,
96 	ahci_atapi_kill_pending,
97 };
98 #endif /* NATAPIBUS */
99 
100 #define ATA_DELAY 10000 /* 10s for a drive I/O */
101 #define ATA_RESET_DELAY 31000 /* 31s for a drive reset */
102 #define AHCI_RST_WAIT (ATA_RESET_DELAY / 10)
103 
104 const struct ata_bustype ahci_ata_bustype = {
105 	SCSIPI_BUSTYPE_ATA,
106 	ahci_ata_bio,
107 	ahci_reset_drive,
108 	ahci_reset_channel,
109 	ahci_exec_command,
110 	ata_get_params,
111 	ahci_ata_addref,
112 	ahci_ata_delref,
113 	ahci_killpending
114 };
115 
116 void ahci_intr_port(struct ahci_softc *, struct ahci_channel *);
117 
118 static void ahci_setup_port(struct ahci_softc *sc, int i);
119 
120 int
121 ahci_reset(struct ahci_softc *sc)
122 {
123 	int i;
124 
125 	/* reset controller */
126 	AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_HR);
127 	/* wait up to 1s for reset to complete */
128 	for (i = 0; i < 1000; i++) {
129 		delay(1000);
130 		if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR) == 0)
131 			break;
132 	}
133 	if ((AHCI_READ(sc, AHCI_GHC) & AHCI_GHC_HR)) {
134 		aprint_error("%s: reset failed\n", AHCINAME(sc));
135 		return -1;
136 	}
137 	/* enable ahci mode */
138 	AHCI_WRITE(sc, AHCI_GHC, AHCI_GHC_AE);
139 	return 0;
140 }
141 
142 void
143 ahci_setup_ports(struct ahci_softc *sc)
144 {
145 	u_int32_t ahci_ports;
146 	int i, port;
147 
148 	ahci_ports = AHCI_READ(sc, AHCI_PI);
149 	for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
150 		if ((ahci_ports & (1 << i)) == 0)
151 			continue;
152 		if (port >= sc->sc_atac.atac_nchannels) {
153 			aprint_error("%s: more ports than announced\n",
154 			    AHCINAME(sc));
155 			break;
156 		}
157 		ahci_setup_port(sc, i);
158 	}
159 }
160 
161 void
162 ahci_reprobe_drives(struct ahci_softc *sc)
163 {
164 	u_int32_t ahci_ports;
165 	int i, port;
166 	struct ahci_channel *achp;
167 	struct ata_channel *chp;
168 
169 	ahci_ports = AHCI_READ(sc, AHCI_PI);
170 	for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
171 		if ((ahci_ports & (1 << i)) == 0)
172 			continue;
173 		if (port >= sc->sc_atac.atac_nchannels) {
174 			aprint_error("%s: more ports than announced\n",
175 			    AHCINAME(sc));
176 			break;
177 		}
178 		achp = &sc->sc_channels[i];
179 		chp = &achp->ata_channel;
180 
181 		ahci_probe_drive(chp);
182 	}
183 }
184 
185 static void
186 ahci_setup_port(struct ahci_softc *sc, int i)
187 {
188 	struct ahci_channel *achp;
189 
190 	achp = &sc->sc_channels[i];
191 
192 	AHCI_WRITE(sc, AHCI_P_CLB(i), achp->ahcic_bus_cmdh);
193 	AHCI_WRITE(sc, AHCI_P_CLBU(i), 0);
194 	AHCI_WRITE(sc, AHCI_P_FB(i), achp->ahcic_bus_rfis);
195 	AHCI_WRITE(sc, AHCI_P_FBU(i), 0);
196 }
197 
198 void
199 ahci_enable_intrs(struct ahci_softc *sc)
200 {
201 
202 	/* clear interrupts */
203 	AHCI_WRITE(sc, AHCI_IS, AHCI_READ(sc, AHCI_IS));
204 	/* enable interrupts */
205 	AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
206 }
207 
208 void
209 ahci_attach(struct ahci_softc *sc)
210 {
211 	u_int32_t ahci_cap, ahci_rev, ahci_ports;
212 	int i, j, port;
213 	struct ahci_channel *achp;
214 	struct ata_channel *chp;
215 	int error;
216 	bus_dma_segment_t seg;
217 	int rseg;
218 	int dmasize;
219 	void *cmdhp;
220 	void *cmdtblp;
221 
222 	if (ahci_reset(sc) != 0)
223 		return;
224 
225 	ahci_cap = AHCI_READ(sc, AHCI_CAP);
226 	sc->sc_atac.atac_nchannels = (ahci_cap & AHCI_CAP_NPMASK) + 1;
227 	sc->sc_ncmds = ((ahci_cap & AHCI_CAP_NCS) >> 8) + 1;
228 	ahci_rev = AHCI_READ(sc, AHCI_VS);
229 	aprint_normal("%s: AHCI revision ", AHCINAME(sc));
230 	switch(ahci_rev) {
231 	case AHCI_VS_10:
232 		aprint_normal("1.0");
233 		break;
234 	case AHCI_VS_11:
235 		aprint_normal("1.1");
236 		break;
237 	case AHCI_VS_12:
238 		aprint_normal("1.2");
239 		break;
240 	default:
241 		aprint_normal("0x%x", ahci_rev);
242 		break;
243 	}
244 
245 	aprint_normal(", %d ports, %d command slots, features 0x%x\n",
246 	    sc->sc_atac.atac_nchannels, sc->sc_ncmds,
247 	    ahci_cap & ~(AHCI_CAP_NPMASK|AHCI_CAP_NCS));
248 	sc->sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DMA | ATAC_CAP_UDMA;
249 	sc->sc_atac.atac_cap |= sc->sc_atac_capflags;
250 	sc->sc_atac.atac_pio_cap = 4;
251 	sc->sc_atac.atac_dma_cap = 2;
252 	sc->sc_atac.atac_udma_cap = 6;
253 	sc->sc_atac.atac_channels = sc->sc_chanarray;
254 	sc->sc_atac.atac_probe = ahci_probe_drive;
255 	sc->sc_atac.atac_bustype_ata = &ahci_ata_bustype;
256 	sc->sc_atac.atac_set_modes = ahci_setup_channel;
257 #if NATAPIBUS > 0
258 	sc->sc_atac.atac_atapibus_attach = ahci_atapibus_attach;
259 #endif
260 
261 	dmasize =
262 	    (AHCI_RFIS_SIZE + AHCI_CMDH_SIZE) * sc->sc_atac.atac_nchannels;
263 	error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
264 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
265 	if (error) {
266 		aprint_error("%s: unable to allocate command header memory"
267 		    ", error=%d\n", AHCINAME(sc), error);
268 		return;
269 	}
270 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, dmasize,
271 	    &cmdhp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
272 	if (error) {
273 		aprint_error("%s: unable to map command header memory"
274 		    ", error=%d\n", AHCINAME(sc), error);
275 		return;
276 	}
277 	error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
278 	    BUS_DMA_NOWAIT, &sc->sc_cmd_hdrd);
279 	if (error) {
280 		aprint_error("%s: unable to create command header map"
281 		    ", error=%d\n", AHCINAME(sc), error);
282 		return;
283 	}
284 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmd_hdrd,
285 	    cmdhp, dmasize, NULL, BUS_DMA_NOWAIT);
286 	if (error) {
287 		aprint_error("%s: unable to load command header map"
288 		    ", error=%d\n", AHCINAME(sc), error);
289 		return;
290 	}
291 	sc->sc_cmd_hdr = cmdhp;
292 
293 	ahci_enable_intrs(sc);
294 
295 	ahci_ports = AHCI_READ(sc, AHCI_PI);
296 	for (i = 0, port = 0; i < AHCI_MAX_PORTS; i++) {
297 		if ((ahci_ports & (1 << i)) == 0)
298 			continue;
299 		if (port >= sc->sc_atac.atac_nchannels) {
300 			aprint_error("%s: more ports than announced\n",
301 			    AHCINAME(sc));
302 			break;
303 		}
304 		achp = &sc->sc_channels[i];
305 		chp = (struct ata_channel *)achp;
306 		sc->sc_chanarray[i] = chp;
307 		chp->ch_channel = i;
308 		chp->ch_atac = &sc->sc_atac;
309 		chp->ch_queue = malloc(sizeof(struct ata_queue),
310 		    M_DEVBUF, M_NOWAIT);
311 		if (chp->ch_queue == NULL) {
312 			aprint_error("%s port %d: can't allocate memory for "
313 			    "command queue", AHCINAME(sc), i);
314 			break;
315 		}
316 		dmasize = AHCI_CMDTBL_SIZE * sc->sc_ncmds;
317 		error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
318 		    &seg, 1, &rseg, BUS_DMA_NOWAIT);
319 		if (error) {
320 			aprint_error("%s: unable to allocate command table "
321 			    "memory, error=%d\n", AHCINAME(sc), error);
322 			break;
323 		}
324 		error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, dmasize,
325 		    &cmdtblp, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
326 		if (error) {
327 			aprint_error("%s: unable to map command table memory"
328 			    ", error=%d\n", AHCINAME(sc), error);
329 			break;
330 		}
331 		error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
332 		    BUS_DMA_NOWAIT, &achp->ahcic_cmd_tbld);
333 		if (error) {
334 			aprint_error("%s: unable to create command table map"
335 			    ", error=%d\n", AHCINAME(sc), error);
336 			break;
337 		}
338 		error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_cmd_tbld,
339 		    cmdtblp, dmasize, NULL, BUS_DMA_NOWAIT);
340 		if (error) {
341 			aprint_error("%s: unable to load command table map"
342 			    ", error=%d\n", AHCINAME(sc), error);
343 			break;
344 		}
345 		achp->ahcic_cmdh  = (struct ahci_cmd_header *)
346 		    ((char *)cmdhp + AHCI_CMDH_SIZE * port);
347 		achp->ahcic_bus_cmdh = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
348 		    AHCI_CMDH_SIZE * port;
349 		achp->ahcic_rfis = (struct ahci_r_fis *)
350 		    ((char *)cmdhp +
351 		     AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
352 		     AHCI_RFIS_SIZE * port);
353 		achp->ahcic_bus_rfis = sc->sc_cmd_hdrd->dm_segs[0].ds_addr +
354 		     AHCI_CMDH_SIZE * sc->sc_atac.atac_nchannels +
355 		     AHCI_RFIS_SIZE * port;
356 		AHCIDEBUG_PRINT(("port %d cmdh %p (0x%x) rfis %p (0x%x)\n", i,
357 		   achp->ahcic_cmdh, (u_int)achp->ahcic_bus_cmdh,
358 		   achp->ahcic_rfis, (u_int)achp->ahcic_bus_rfis),
359 		   DEBUG_PROBE);
360 
361 		for (j = 0; j < sc->sc_ncmds; j++) {
362 			achp->ahcic_cmd_tbl[j] = (struct ahci_cmd_tbl *)
363 			    ((char *)cmdtblp + AHCI_CMDTBL_SIZE * j);
364 			achp->ahcic_bus_cmd_tbl[j] =
365 			     achp->ahcic_cmd_tbld->dm_segs[0].ds_addr +
366 			     AHCI_CMDTBL_SIZE * j;
367 			achp->ahcic_cmdh[j].cmdh_cmdtba =
368 			    htole32(achp->ahcic_bus_cmd_tbl[j]);
369 			achp->ahcic_cmdh[j].cmdh_cmdtbau = htole32(0);
370 			AHCIDEBUG_PRINT(("port %d/%d tbl %p (0x%x)\n", i, j,
371 			    achp->ahcic_cmd_tbl[j],
372 			    (u_int)achp->ahcic_bus_cmd_tbl[j]), DEBUG_PROBE);
373 			/* The xfer DMA map */
374 			error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
375 			    AHCI_NPRD, 0x400000 /* 4MB */, 0,
376 			    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
377 			    &achp->ahcic_datad[j]);
378 			if (error) {
379 				aprint_error("%s: couldn't alloc xfer DMA map, "
380 				    "error=%d\n", AHCINAME(sc), error);
381 				goto end;
382 			}
383 		}
384 		ahci_setup_port(sc, i);
385 		chp->ch_ndrive = 1;
386 		if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
387 		    AHCI_P_SSTS(i), 4,  &achp->ahcic_sstatus) != 0) {
388 			aprint_error("%s: couldn't map channel %d "
389 			    "sata_status regs\n", AHCINAME(sc), i);
390 			break;
391 		}
392 		if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
393 		    AHCI_P_SCTL(i), 4,  &achp->ahcic_scontrol) != 0) {
394 			aprint_error("%s: couldn't map channel %d "
395 			    "sata_control regs\n", AHCINAME(sc), i);
396 			break;
397 		}
398 		if (bus_space_subregion(sc->sc_ahcit, sc->sc_ahcih,
399 		    AHCI_P_SERR(i), 4,  &achp->ahcic_serror) != 0) {
400 			aprint_error("%s: couldn't map channel %d "
401 			    "sata_error regs\n", AHCINAME(sc), i);
402 			break;
403 		}
404 		ata_channel_attach(chp);
405 		port++;
406 end:
407 		continue;
408 	}
409 }
410 
411 int
412 ahci_intr(void *v)
413 {
414 	struct ahci_softc *sc = v;
415 	u_int32_t is;
416 	int i, r = 0;
417 
418 	while ((is = AHCI_READ(sc, AHCI_IS))) {
419 		AHCIDEBUG_PRINT(("%s ahci_intr 0x%x\n", AHCINAME(sc), is),
420 		    DEBUG_INTR);
421 		r = 1;
422 		AHCI_WRITE(sc, AHCI_IS, is);
423 		for (i = 0; i < AHCI_MAX_PORTS; i++)
424 			if (is & (1 << i))
425 				ahci_intr_port(sc, &sc->sc_channels[i]);
426 	}
427 	return r;
428 }
429 
430 void
431 ahci_intr_port(struct ahci_softc *sc, struct ahci_channel *achp)
432 {
433 	u_int32_t is, tfd;
434 	struct ata_channel *chp = &achp->ata_channel;
435 	struct ata_xfer *xfer = chp->ch_queue->active_xfer;
436 	int slot;
437 
438 	is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
439 	AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), is);
440 	AHCIDEBUG_PRINT(("ahci_intr_port %s port %d is 0x%x CI 0x%x\n", AHCINAME(sc),
441 	    chp->ch_channel, is, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
442 	    DEBUG_INTR);
443 
444 	if (is & (AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
445 	    AHCI_P_IX_OFS | AHCI_P_IX_UFS)) {
446 		slot = (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))
447 			& AHCI_P_CMD_CCS_MASK) >> AHCI_P_CMD_CCS_SHIFT;
448 		if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
449 			return;
450 		/* stop channel */
451 		ahci_channel_stop(sc, chp, 0);
452 		if (slot != 0) {
453 			printf("ahci_intr_port: slot %d\n", slot);
454 			panic("ahci_intr_port");
455 		}
456 		if (is & AHCI_P_IX_TFES) {
457 			tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
458 			chp->ch_error =
459 			    (tfd & AHCI_P_TFD_ERR_MASK) >> AHCI_P_TFD_ERR_SHIFT;
460 			chp->ch_status = (tfd & 0xff);
461 		} else {
462 			/* emulate a CRC error */
463 			chp->ch_error = WDCE_CRC;
464 			chp->ch_status = WDCS_ERR;
465 		}
466 		xfer->c_intr(chp, xfer, is);
467 		/* if channel has not been restarted, do it now */
468 		if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
469 		    == 0)
470 			ahci_channel_start(sc, chp);
471 	} else {
472 		slot = 0; /* XXX */
473 		is = AHCI_READ(sc, AHCI_P_IS(chp->ch_channel));
474 		AHCIDEBUG_PRINT(("ahci_intr_port port %d is 0x%x act 0x%x CI 0x%x\n",
475 		    chp->ch_channel, is, achp->ahcic_cmds_active,
476 		    AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_INTR);
477 		if ((achp->ahcic_cmds_active & (1 << slot)) == 0)
478 			return;
479 		if ((AHCI_READ(sc, AHCI_P_CI(chp->ch_channel)) & (1 << slot))
480 		    == 0) {
481 			xfer->c_intr(chp, xfer, 0);
482 		}
483 	}
484 }
485 
486 void
487 ahci_reset_drive(struct ata_drive_datas *drvp, int flags)
488 {
489 	struct ata_channel *chp = drvp->chnl_softc;
490 	ata_reset_channel(chp, flags);
491 	return;
492 }
493 
494 void
495 ahci_reset_channel(struct ata_channel *chp, int flags)
496 {
497 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
498 	struct ahci_channel *achp = (struct ahci_channel *)chp;
499 	int i, tfd;
500 
501 	ahci_channel_stop(sc, chp, flags);
502 	if (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
503 	    achp->ahcic_sstatus) != SStatus_DET_DEV) {
504 		printf("%s: port reset failed\n", AHCINAME(sc));
505 		/* XXX and then ? */
506 	}
507 	if (chp->ch_queue->active_xfer) {
508 		chp->ch_queue->active_xfer->c_kill_xfer(chp,
509 		    chp->ch_queue->active_xfer, KILL_RESET);
510 	}
511 	tsleep(&sc, PRIBIO, "ahcirst", mstohz(500));
512 	/* clear port interrupt register */
513 	AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
514 	/* clear SErrors and start operations */
515 	ahci_channel_start(sc, chp);
516 	/* wait 31s for BSY to clear */
517 	for (i = 0; i <AHCI_RST_WAIT; i++) {
518 		tfd = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
519 		if ((((tfd & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT)
520 		    & WDCS_BSY) == 0)
521 			break;
522 		tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10));
523 	}
524 	if (i == AHCI_RST_WAIT)
525 		aprint_error("%s: BSY never cleared, TD 0x%x\n",
526 		    AHCINAME(sc), tfd);
527 	AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10),
528 	    DEBUG_PROBE);
529 	/* clear port interrupt register */
530 	AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
531 
532 	return;
533 }
534 
535 int
536 ahci_ata_addref(struct ata_drive_datas *drvp)
537 {
538 	return 0;
539 }
540 
541 void
542 ahci_ata_delref(struct ata_drive_datas *drvp)
543 {
544 	return;
545 }
546 
547 void
548 ahci_killpending(struct ata_drive_datas *drvp)
549 {
550 	return;
551 }
552 
553 void
554 ahci_probe_drive(struct ata_channel *chp)
555 {
556 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
557 	struct ahci_channel *achp = (struct ahci_channel *)chp;
558 	int i, s;
559 	u_int32_t sig;
560 
561 	/* XXX This should be done by other code. */
562 	for (i = 0; i < chp->ch_ndrive; i++) {
563 		chp->ch_drive[i].chnl_softc = chp;
564 		chp->ch_drive[i].drive = i;
565 	}
566 
567 	/* bring interface up, accept FISs, power up and spin up device */
568 	AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
569 	    AHCI_P_CMD_ICC_AC | AHCI_P_CMD_FRE |
570 	    AHCI_P_CMD_POD | AHCI_P_CMD_SUD);
571 	/* reset the PHY and bring online */
572 	switch (sata_reset_interface(chp, sc->sc_ahcit, achp->ahcic_scontrol,
573 	    achp->ahcic_sstatus)) {
574 	case SStatus_DET_DEV:
575 		tsleep(&sc, PRIBIO, "ahcidv", mstohz(500));
576 		/* clear port interrupt register */
577 		AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
578 		/* clear SErrors and start operations */
579 		ahci_channel_start(sc, chp);
580 		/* wait 31s for BSY to clear */
581 		for (i = 0; i <AHCI_RST_WAIT; i++) {
582 			sig = AHCI_READ(sc, AHCI_P_TFD(chp->ch_channel));
583 			if ((((sig & AHCI_P_TFD_ST) >> AHCI_P_TFD_ST_SHIFT)
584 			    & WDCS_BSY) == 0)
585 				break;
586 			tsleep(&sc, PRIBIO, "ahcid2h", mstohz(10));
587 		}
588 		if (i == AHCI_RST_WAIT) {
589 			aprint_error("%s: BSY never cleared, TD 0x%x\n",
590 			    AHCINAME(sc), sig);
591 			return;
592 		}
593 		AHCIDEBUG_PRINT(("%s: BSY took %d ms\n", AHCINAME(sc), i * 10),
594 		    DEBUG_PROBE);
595 		sig = AHCI_READ(sc, AHCI_P_SIG(chp->ch_channel));
596 		AHCIDEBUG_PRINT(("%s: port %d: sig=0x%x CMD=0x%x\n",
597 		    AHCINAME(sc), chp->ch_channel, sig,
598 		    AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel))), DEBUG_PROBE);
599 		/*
600 		 * scnt and sn are supposed to be 0x1 for ATAPI, but in some
601 		 * cases we get wrong values here, so ignore it.
602 		 */
603 		s = splbio();
604 		if ((sig & 0xffff0000) == 0xeb140000) {
605 			chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
606 		} else
607 			chp->ch_drive[0].drive_flags |= DRIVE_ATA;
608 		splx(s);
609 		/* clear port interrupt register */
610 		AHCI_WRITE(sc, AHCI_P_IS(chp->ch_channel), 0xffffffff);
611 		/* and enable interrupts */
612 		AHCI_WRITE(sc, AHCI_P_IE(chp->ch_channel),
613 		    AHCI_P_IX_TFES | AHCI_P_IX_HBFS | AHCI_P_IX_IFS |
614 		    AHCI_P_IX_OFS | AHCI_P_IX_DPS | AHCI_P_IX_UFS |
615 		    AHCI_P_IX_DHRS);
616 		/* wait 500ms before actually starting operations */
617 		tsleep(&sc, PRIBIO, "ahciprb", mstohz(500));
618 		break;
619 
620 	default:
621 		break;
622 	}
623 }
624 
625 void
626 ahci_setup_channel(struct ata_channel *chp)
627 {
628 	return;
629 }
630 
631 int
632 ahci_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
633 {
634 	struct ata_channel *chp = drvp->chnl_softc;
635 	struct ata_xfer *xfer;
636 	int ret;
637 	int s;
638 
639 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
640 	AHCIDEBUG_PRINT(("ahci_exec_command port %d CI 0x%x\n",
641 	    chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
642 	    DEBUG_XFERS);
643 	xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP :
644 	    ATAXF_NOSLEEP);
645 	if (xfer == NULL) {
646 		return ATACMD_TRY_AGAIN;
647 	}
648 	if (ata_c->flags & AT_POLL)
649 		xfer->c_flags |= C_POLL;
650 	if (ata_c->flags & AT_WAIT)
651 		xfer->c_flags |= C_WAIT;
652 	xfer->c_drive = drvp->drive;
653 	xfer->c_databuf = ata_c->data;
654 	xfer->c_bcount = ata_c->bcount;
655 	xfer->c_cmd = ata_c;
656 	xfer->c_start = ahci_cmd_start;
657 	xfer->c_intr = ahci_cmd_complete;
658 	xfer->c_kill_xfer = ahci_cmd_kill_xfer;
659 	s = splbio();
660 	ata_exec_xfer(chp, xfer);
661 #ifdef DIAGNOSTIC
662 	if ((ata_c->flags & AT_POLL) != 0 &&
663 	    (ata_c->flags & AT_DONE) == 0)
664 		panic("ahci_exec_command: polled command not done");
665 #endif
666 	if (ata_c->flags & AT_DONE) {
667 		ret = ATACMD_COMPLETE;
668 	} else {
669 		if (ata_c->flags & AT_WAIT) {
670 			while ((ata_c->flags & AT_DONE) == 0) {
671 				tsleep(ata_c, PRIBIO, "ahcicmd", 0);
672 			}
673 			ret = ATACMD_COMPLETE;
674 		} else {
675 			ret = ATACMD_QUEUED;
676 		}
677 	}
678 	splx(s);
679 	return ret;
680 }
681 
682 void
683 ahci_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
684 {
685 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
686 	struct ahci_channel *achp = (struct ahci_channel *)chp;
687 	struct ata_command *ata_c = xfer->c_cmd;
688 	int slot = 0 /* XXX slot */;
689 	struct ahci_cmd_tbl *cmd_tbl;
690 	struct ahci_cmd_header *cmd_h;
691 	int i;
692 	int channel = chp->ch_channel;
693 
694 	AHCIDEBUG_PRINT(("ahci_cmd_start CI 0x%x\n",
695 	    AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
696 
697 	cmd_tbl = achp->ahcic_cmd_tbl[slot];
698 	AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
699 	      cmd_tbl), DEBUG_XFERS);
700 
701 	satafis_rhd_construct_cmd(ata_c, cmd_tbl->cmdt_cfis);
702 
703 	cmd_h = &achp->ahcic_cmdh[slot];
704 	AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
705 	    chp->ch_channel, cmd_h), DEBUG_XFERS);
706 	if (ahci_dma_setup(chp, slot,
707 	    (ata_c->flags & (AT_READ|AT_WRITE)) ? ata_c->data : NULL,
708 	    ata_c->bcount,
709 	    (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
710 		ata_c->flags |= AT_DF;
711 		ahci_cmd_complete(chp, xfer, slot);
712 		return;
713 	}
714 	cmd_h->cmdh_flags = htole16(
715 	    ((ata_c->flags & AT_WRITE) ? AHCI_CMDH_F_WR : 0) |
716 	    RHD_FISLEN / 4);
717 	cmd_h->cmdh_prdbc = 0;
718 	AHCI_CMDH_SYNC(sc, achp, slot,
719 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
720 
721 	if (ata_c->flags & AT_POLL) {
722 		/* polled command, disable interrupts */
723 		AHCI_WRITE(sc, AHCI_GHC,
724 		    AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
725 	}
726 	chp->ch_flags |= ATACH_IRQ_WAIT;
727 	chp->ch_status = 0;
728 	/* start command */
729 	AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
730 	/* and says we started this command */
731 	achp->ahcic_cmds_active |= 1 << slot;
732 
733 	if ((ata_c->flags & AT_POLL) == 0) {
734 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
735 		callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
736 		    ahci_timeout, chp);
737 		return;
738 	}
739 	/*
740 	 * Polled command.
741 	 */
742 	for (i = 0; i < ata_c->timeout / 10; i++) {
743 		if (ata_c->flags & AT_DONE)
744 			break;
745 		ahci_intr_port(sc, achp);
746 		if (ata_c->flags & AT_WAIT)
747 			tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
748 		else
749 			delay(10000);
750 	}
751 	AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
752 	    AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
753 	    AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
754 	    AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
755 	    AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
756 	    DEBUG_XFERS);
757 	if ((ata_c->flags & AT_DONE) == 0) {
758 		ata_c->flags |= AT_TIMEOU;
759 		ahci_cmd_complete(chp, xfer, slot);
760 	}
761 	/* reenable interrupts */
762 	AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
763 }
764 
765 void
766 ahci_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
767 {
768 	struct ata_command *ata_c = xfer->c_cmd;
769 	AHCIDEBUG_PRINT(("ahci_cmd_kill_xfer channel %d\n", chp->ch_channel),
770 	    DEBUG_FUNCS);
771 
772 	switch (reason) {
773 	case KILL_GONE:
774 		ata_c->flags |= AT_GONE;
775 		break;
776 	case KILL_RESET:
777 		ata_c->flags |= AT_RESET;
778 		break;
779 	default:
780 		printf("ahci_cmd_kill_xfer: unknown reason %d\n", reason);
781 		panic("ahci_cmd_kill_xfer");
782 	}
783 	ahci_cmd_done(chp, xfer, 0 /* XXX slot */);
784 }
785 
786 int
787 ahci_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
788 {
789 	int slot = 0; /* XXX slot */
790 	struct ata_command *ata_c = xfer->c_cmd;
791 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
792 	struct ahci_channel *achp = (struct ahci_channel *)chp;
793 
794 	AHCIDEBUG_PRINT(("ahci_cmd_complete channel %d CMD 0x%x CI 0x%x\n",
795 	    chp->ch_channel, AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)),
796 	    AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
797 	    DEBUG_FUNCS);
798 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
799 	if (xfer->c_flags & C_TIMEOU) {
800 		ata_c->flags |= AT_TIMEOU;
801 	} else
802 		callout_stop(&chp->ch_callout);
803 
804 	chp->ch_queue->active_xfer = NULL;
805 
806 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
807 		ahci_cmd_kill_xfer(chp, xfer, KILL_GONE);
808 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
809 		wakeup(&chp->ch_queue->active_xfer);
810 		return 0;
811 	}
812 
813 	if (chp->ch_status & WDCS_BSY) {
814 		ata_c->flags |= AT_TIMEOU;
815 	} else if (chp->ch_status & WDCS_ERR) {
816 		ata_c->r_error = chp->ch_error;
817 		ata_c->flags |= AT_ERROR;
818 	}
819 
820 	if (ata_c->flags & AT_READREG)
821 		satafis_rdh_cmd_readreg(ata_c, achp->ahcic_rfis->rfis_rfis);
822 
823 	ahci_cmd_done(chp, xfer, slot);
824 	return 0;
825 }
826 
827 void
828 ahci_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
829 {
830 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
831 	struct ahci_channel *achp = (struct ahci_channel *)chp;
832 	struct ata_command *ata_c = xfer->c_cmd;
833 	uint16_t *idwordbuf;
834 	int i;
835 
836 	AHCIDEBUG_PRINT(("ahci_cmd_done channel %d\n", chp->ch_channel),
837 	    DEBUG_FUNCS);
838 
839 	/* this comamnd is not active any more */
840 	achp->ahcic_cmds_active &= ~(1 << slot);
841 
842 	if (ata_c->flags & (AT_READ|AT_WRITE)) {
843 		bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
844 		    achp->ahcic_datad[slot]->dm_mapsize,
845 		    (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
846 		    BUS_DMASYNC_POSTWRITE);
847 		bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
848 	}
849 
850 	AHCI_CMDH_SYNC(sc, achp, slot,
851 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
852 
853 	/* ata(4) expects IDENTIFY data to be in host endianess */
854 	if (ata_c->r_command == WDCC_IDENTIFY ||
855 	    ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
856 		idwordbuf = xfer->c_databuf;
857 		for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
858 			idwordbuf[i] = le16toh(idwordbuf[i]);
859 		}
860 	}
861 
862 	ata_c->flags |= AT_DONE;
863 	if (achp->ahcic_cmdh[slot].cmdh_prdbc)
864 		ata_c->flags |= AT_XFDONE;
865 
866 	ata_free_xfer(chp, xfer);
867 	if (ata_c->flags & AT_WAIT)
868 		wakeup(ata_c);
869 	else if (ata_c->callback)
870 		ata_c->callback(ata_c->callback_arg);
871 	atastart(chp);
872 	return;
873 }
874 
875 int
876 ahci_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
877 {
878 	struct ata_channel *chp = drvp->chnl_softc;
879 	struct ata_xfer *xfer;
880 
881 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
882 	AHCIDEBUG_PRINT(("ahci_ata_bio port %d CI 0x%x\n",
883 	    chp->ch_channel, AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))),
884 	    DEBUG_XFERS);
885 	xfer = ata_get_xfer(ATAXF_NOSLEEP);
886 	if (xfer == NULL) {
887 		return ATACMD_TRY_AGAIN;
888 	}
889 	if (ata_bio->flags & ATA_POLL)
890 		xfer->c_flags |= C_POLL;
891 	xfer->c_drive = drvp->drive;
892 	xfer->c_cmd = ata_bio;
893 	xfer->c_databuf = ata_bio->databuf;
894 	xfer->c_bcount = ata_bio->bcount;
895 	xfer->c_start = ahci_bio_start;
896 	xfer->c_intr = ahci_bio_complete;
897 	xfer->c_kill_xfer = ahci_bio_kill_xfer;
898 	ata_exec_xfer(chp, xfer);
899 	return (ata_bio->flags & ATA_ITSDONE) ? ATACMD_COMPLETE : ATACMD_QUEUED;
900 }
901 
902 void
903 ahci_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
904 {
905 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
906 	struct ahci_channel *achp = (struct ahci_channel *)chp;
907 	struct ata_bio *ata_bio = xfer->c_cmd;
908 	int slot = 0 /* XXX slot */;
909 	struct ahci_cmd_tbl *cmd_tbl;
910 	struct ahci_cmd_header *cmd_h;
911 	int i;
912 	int channel = chp->ch_channel;
913 
914 	AHCIDEBUG_PRINT(("ahci_bio_start CI 0x%x\n",
915 	    AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
916 
917 	cmd_tbl = achp->ahcic_cmd_tbl[slot];
918 	AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
919 	      cmd_tbl), DEBUG_XFERS);
920 
921 	satafis_rhd_construct_bio(xfer, cmd_tbl->cmdt_cfis);
922 
923 	cmd_h = &achp->ahcic_cmdh[slot];
924 	AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
925 	    chp->ch_channel, cmd_h), DEBUG_XFERS);
926 	if (ahci_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
927 	    (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
928 		ata_bio->error = ERR_DMA;
929 		ata_bio->r_error = 0;
930 		ahci_bio_complete(chp, xfer, slot);
931 		return;
932 	}
933 	cmd_h->cmdh_flags = htole16(
934 	    ((ata_bio->flags & ATA_READ) ? 0 :  AHCI_CMDH_F_WR) |
935 	    RHD_FISLEN / 4);
936 	cmd_h->cmdh_prdbc = 0;
937 	AHCI_CMDH_SYNC(sc, achp, slot,
938 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
939 
940 	if (xfer->c_flags & C_POLL) {
941 		/* polled command, disable interrupts */
942 		AHCI_WRITE(sc, AHCI_GHC,
943 		    AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
944 	}
945 	chp->ch_flags |= ATACH_IRQ_WAIT;
946 	chp->ch_status = 0;
947 	/* start command */
948 	AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
949 	/* and says we started this command */
950 	achp->ahcic_cmds_active |= 1 << slot;
951 
952 	if ((xfer->c_flags & C_POLL) == 0) {
953 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
954 		callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
955 		    ahci_timeout, chp);
956 		return;
957 	}
958 	/*
959 	 * Polled command.
960 	 */
961 	for (i = 0; i < ATA_DELAY / 10; i++) {
962 		if (ata_bio->flags & ATA_ITSDONE)
963 			break;
964 		ahci_intr_port(sc, achp);
965 		if (ata_bio->flags & ATA_NOSLEEP)
966 			delay(10000);
967 		else
968 			tsleep(&xfer, PRIBIO, "ahcipl", mstohz(10));
969 	}
970 	AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
971 	    AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
972 	    AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
973 	    AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
974 	    AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
975 	    DEBUG_XFERS);
976 	if ((ata_bio->flags & ATA_ITSDONE) == 0) {
977 		ata_bio->error = TIMEOUT;
978 		ahci_bio_complete(chp, xfer, slot);
979 	}
980 	/* reenable interrupts */
981 	AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
982 }
983 
984 void
985 ahci_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
986 {
987 	int slot = 0;  /* XXX slot */
988 	int drive = xfer->c_drive;
989 	struct ata_bio *ata_bio = xfer->c_cmd;
990 	struct ahci_channel *achp = (struct ahci_channel *)chp;
991 	AHCIDEBUG_PRINT(("ahci_bio_kill_xfer channel %d\n", chp->ch_channel),
992 	    DEBUG_FUNCS);
993 
994 	achp->ahcic_cmds_active &= ~(1 << slot);
995 	ata_free_xfer(chp, xfer);
996 	ata_bio->flags |= ATA_ITSDONE;
997 	switch (reason) {
998 	case KILL_GONE:
999 		ata_bio->error = ERR_NODEV;
1000 		break;
1001 	case KILL_RESET:
1002 		ata_bio->error = ERR_RESET;
1003 		break;
1004 	default:
1005 		printf("ahci_bio_kill_xfer: unknown reason %d\n", reason);
1006 		panic("ahci_bio_kill_xfer");
1007 	}
1008 	ata_bio->r_error = WDCE_ABRT;
1009 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1010 }
1011 
1012 int
1013 ahci_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int is)
1014 {
1015 	int slot = 0; /* XXX slot */
1016 	struct ata_bio *ata_bio = xfer->c_cmd;
1017 	int drive = xfer->c_drive;
1018 	struct ahci_channel *achp = (struct ahci_channel *)chp;
1019 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1020 
1021 	AHCIDEBUG_PRINT(("ahci_bio_complete channel %d\n", chp->ch_channel),
1022 	    DEBUG_FUNCS);
1023 
1024 	achp->ahcic_cmds_active &= ~(1 << slot);
1025 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
1026 	if (xfer->c_flags & C_TIMEOU) {
1027 		ata_bio->error = TIMEOUT;
1028 	} else {
1029 		callout_stop(&chp->ch_callout);
1030 		ata_bio->error = NOERROR;
1031 	}
1032 
1033 	chp->ch_queue->active_xfer = NULL;
1034 	bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1035 	    achp->ahcic_datad[slot]->dm_mapsize,
1036 	    (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1037 	    BUS_DMASYNC_POSTWRITE);
1038 	bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
1039 
1040 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1041 		ahci_bio_kill_xfer(chp, xfer, KILL_GONE);
1042 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1043 		wakeup(&chp->ch_queue->active_xfer);
1044 		return 0;
1045 	}
1046 	ata_free_xfer(chp, xfer);
1047 	ata_bio->flags |= ATA_ITSDONE;
1048 	if (chp->ch_status & WDCS_DWF) {
1049 		ata_bio->error = ERR_DF;
1050 	} else if (chp->ch_status & WDCS_ERR) {
1051 		ata_bio->error = ERROR;
1052 		ata_bio->r_error = chp->ch_error;
1053 	} else if (chp->ch_status & WDCS_CORR)
1054 		ata_bio->flags |= ATA_CORR;
1055 
1056 	AHCI_CMDH_SYNC(sc, achp, slot,
1057 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1058 	AHCIDEBUG_PRINT(("ahci_bio_complete bcount %ld",
1059 	    ata_bio->bcount), DEBUG_XFERS);
1060 	/*
1061 	 * if it was a write, complete data buffer may have been transfered
1062 	 * before error detection; in this case don't use cmdh_prdbc
1063 	 * as it won't reflect what was written to media. Assume nothing
1064 	 * was transfered and leave bcount as-is.
1065 	 */
1066 	if ((ata_bio->flags & ATA_READ) || ata_bio->error == NOERROR)
1067 		ata_bio->bcount -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc);
1068 	AHCIDEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1069 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1070 	atastart(chp);
1071 	return 0;
1072 }
1073 
1074 void
1075 ahci_channel_stop(struct ahci_softc *sc, struct ata_channel *chp, int flags)
1076 {
1077 	int i;
1078 	/* stop channel */
1079 	AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1080 	    AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & ~AHCI_P_CMD_ST);
1081 	/* wait 1s for channel to stop */
1082 	for (i = 0; i <100; i++) {
1083 		if ((AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR)
1084 		    == 0)
1085 			break;
1086 		if (flags & AT_WAIT)
1087 			tsleep(&sc, PRIBIO, "ahcirst", mstohz(10));
1088 		else
1089 			delay(10000);
1090 	}
1091 	if (AHCI_READ(sc, AHCI_P_CMD(chp->ch_channel)) & AHCI_P_CMD_CR) {
1092 		printf("%s: channel wouldn't stop\n", AHCINAME(sc));
1093 		/* XXX controller reset ? */
1094 		return;
1095 	}
1096 }
1097 
1098 void
1099 ahci_channel_start(struct ahci_softc *sc, struct ata_channel *chp)
1100 {
1101 	/* clear error */
1102 	AHCI_WRITE(sc, AHCI_P_SERR(chp->ch_channel),
1103 	    AHCI_READ(sc, AHCI_P_SERR(chp->ch_channel)));
1104 
1105 	/* and start controller */
1106 	AHCI_WRITE(sc, AHCI_P_CMD(chp->ch_channel),
1107 	    AHCI_P_CMD_ICC_AC | AHCI_P_CMD_POD | AHCI_P_CMD_SUD |
1108 	    AHCI_P_CMD_FRE | AHCI_P_CMD_ST);
1109 }
1110 
1111 void
1112 ahci_timeout(void *v)
1113 {
1114 	struct ata_channel *chp = (struct ata_channel *)v;
1115 	struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1116 	int s = splbio();
1117 	AHCIDEBUG_PRINT(("ahci_timeout xfer %p\n", xfer), DEBUG_INTR);
1118 	if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1119 		xfer->c_flags |= C_TIMEOU;
1120 		xfer->c_intr(chp, xfer, 0);
1121 	}
1122 	splx(s);
1123 }
1124 
1125 int
1126 ahci_dma_setup(struct ata_channel *chp, int slot, void *data,
1127     size_t count, int op)
1128 {
1129 	int error, seg;
1130 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1131 	struct ahci_channel *achp = (struct ahci_channel *)chp;
1132 	struct ahci_cmd_tbl *cmd_tbl;
1133 	struct ahci_cmd_header *cmd_h;
1134 
1135 	cmd_h = &achp->ahcic_cmdh[slot];
1136 	cmd_tbl = achp->ahcic_cmd_tbl[slot];
1137 
1138 	if (data == NULL) {
1139 		cmd_h->cmdh_prdtl = 0;
1140 		goto end;
1141 	}
1142 
1143 	error = bus_dmamap_load(sc->sc_dmat, achp->ahcic_datad[slot],
1144 	    data, count, NULL,
1145 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1146 	if (error) {
1147 		printf("%s port %d: failed to load xfer: %d\n",
1148 		    AHCINAME(sc), chp->ch_channel, error);
1149 		return error;
1150 	}
1151 	bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1152 	    achp->ahcic_datad[slot]->dm_mapsize,
1153 	    (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1154 	for (seg = 0; seg <  achp->ahcic_datad[slot]->dm_nsegs; seg++) {
1155 		cmd_tbl->cmdt_prd[seg].prd_dba = htole32(
1156 		     achp->ahcic_datad[slot]->dm_segs[seg].ds_addr);
1157 		cmd_tbl->cmdt_prd[seg].prd_dbau = 0;
1158 		cmd_tbl->cmdt_prd[seg].prd_dbc = htole32(
1159 		    achp->ahcic_datad[slot]->dm_segs[seg].ds_len - 1);
1160 	}
1161 	cmd_tbl->cmdt_prd[seg - 1].prd_dbc |= htole32(AHCI_PRD_DBC_IPC);
1162 	cmd_h->cmdh_prdtl = htole16(achp->ahcic_datad[slot]->dm_nsegs);
1163 end:
1164 	AHCI_CMDTBL_SYNC(sc, achp, slot, BUS_DMASYNC_PREWRITE);
1165 	return 0;
1166 }
1167 
1168 #if NATAPIBUS > 0
1169 void
1170 ahci_atapibus_attach(struct atabus_softc * ata_sc)
1171 {
1172 	struct ata_channel *chp = ata_sc->sc_chan;
1173 	struct atac_softc *atac = chp->ch_atac;
1174 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1175 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
1176 	/*
1177 	 * Fill in the scsipi_adapter.
1178 	 */
1179 	adapt->adapt_dev = atac->atac_dev;
1180 	adapt->adapt_nchannels = atac->atac_nchannels;
1181 	adapt->adapt_request = ahci_atapi_scsipi_request;
1182 	adapt->adapt_minphys = ahci_atapi_minphys;
1183 	atac->atac_atapi_adapter.atapi_probe_device = ahci_atapi_probe_device;
1184 
1185 	/*
1186 	 * Fill in the scsipi_channel.
1187 	 */
1188 	memset(chan, 0, sizeof(*chan));
1189 	chan->chan_adapter = adapt;
1190 	chan->chan_bustype = &ahci_atapi_bustype;
1191 	chan->chan_channel = chp->ch_channel;
1192 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
1193 	chan->chan_openings = 1;
1194 	chan->chan_max_periph = 1;
1195 	chan->chan_ntargets = 1;
1196 	chan->chan_nluns = 1;
1197 	chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
1198 		atapiprint);
1199 }
1200 
1201 void
1202 ahci_atapi_minphys(struct buf *bp)
1203 {
1204 	if (bp->b_bcount > MAXPHYS)
1205 		bp->b_bcount = MAXPHYS;
1206 	minphys(bp);
1207 }
1208 
1209 /*
1210  * Kill off all pending xfers for a periph.
1211  *
1212  * Must be called at splbio().
1213  */
1214 void
1215 ahci_atapi_kill_pending(struct scsipi_periph *periph)
1216 {
1217 	struct atac_softc *atac =
1218 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
1219 	struct ata_channel *chp =
1220 	    atac->atac_channels[periph->periph_channel->chan_channel];
1221 
1222 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
1223 }
1224 
1225 void
1226 ahci_atapi_scsipi_request(struct scsipi_channel *chan,
1227     scsipi_adapter_req_t req, void *arg)
1228 {
1229 	struct scsipi_adapter *adapt = chan->chan_adapter;
1230 	struct scsipi_periph *periph;
1231 	struct scsipi_xfer *sc_xfer;
1232 	struct ahci_softc *sc = device_private(adapt->adapt_dev);
1233 	struct atac_softc *atac = &sc->sc_atac;
1234 	struct ata_xfer *xfer;
1235 	int channel = chan->chan_channel;
1236 	int drive, s;
1237 
1238 	switch (req) {
1239 	case ADAPTER_REQ_RUN_XFER:
1240 		sc_xfer = arg;
1241 		periph = sc_xfer->xs_periph;
1242 		drive = periph->periph_target;
1243 		if (!device_is_active(atac->atac_dev)) {
1244 			sc_xfer->error = XS_DRIVER_STUFFUP;
1245 			scsipi_done(sc_xfer);
1246 			return;
1247 		}
1248 		xfer = ata_get_xfer(ATAXF_NOSLEEP);
1249 		if (xfer == NULL) {
1250 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
1251 			scsipi_done(sc_xfer);
1252 			return;
1253 		}
1254 
1255 		if (sc_xfer->xs_control & XS_CTL_POLL)
1256 			xfer->c_flags |= C_POLL;
1257 		xfer->c_drive = drive;
1258 		xfer->c_flags |= C_ATAPI;
1259 		xfer->c_cmd = sc_xfer;
1260 		xfer->c_databuf = sc_xfer->data;
1261 		xfer->c_bcount = sc_xfer->datalen;
1262 		xfer->c_start = ahci_atapi_start;
1263 		xfer->c_intr = ahci_atapi_complete;
1264 		xfer->c_kill_xfer = ahci_atapi_kill_xfer;
1265 		xfer->c_dscpoll = 0;
1266 		s = splbio();
1267 		ata_exec_xfer(atac->atac_channels[channel], xfer);
1268 #ifdef DIAGNOSTIC
1269 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
1270 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
1271 			panic("ahci_atapi_scsipi_request: polled command "
1272 			    "not done");
1273 #endif
1274 		splx(s);
1275 		return;
1276 	default:
1277 		/* Not supported, nothing to do. */
1278 		;
1279 	}
1280 }
1281 
1282 void
1283 ahci_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
1284 {
1285 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1286 	struct ahci_channel *achp = (struct ahci_channel *)chp;
1287 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1288 	int slot = 0 /* XXX slot */;
1289 	struct ahci_cmd_tbl *cmd_tbl;
1290 	struct ahci_cmd_header *cmd_h;
1291 	int i;
1292 	int channel = chp->ch_channel;
1293 
1294 	AHCIDEBUG_PRINT(("ahci_atapi_start CI 0x%x\n",
1295 	    AHCI_READ(sc, AHCI_P_CI(chp->ch_channel))), DEBUG_XFERS);
1296 
1297 	cmd_tbl = achp->ahcic_cmd_tbl[slot];
1298 	AHCIDEBUG_PRINT(("%s port %d tbl %p\n", AHCINAME(sc), chp->ch_channel,
1299 	      cmd_tbl), DEBUG_XFERS);
1300 
1301 	satafis_rhd_construct_atapi(xfer, cmd_tbl->cmdt_cfis);
1302 	memset(&cmd_tbl->cmdt_acmd, 0, sizeof(cmd_tbl->cmdt_acmd));
1303 	memcpy(cmd_tbl->cmdt_acmd, sc_xfer->cmd, sc_xfer->cmdlen);
1304 
1305 	cmd_h = &achp->ahcic_cmdh[slot];
1306 	AHCIDEBUG_PRINT(("%s port %d header %p\n", AHCINAME(sc),
1307 	    chp->ch_channel, cmd_h), DEBUG_XFERS);
1308 	if (ahci_dma_setup(chp, slot, sc_xfer->datalen ? sc_xfer->data : NULL,
1309 	    sc_xfer->datalen,
1310 	    (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1311 	    BUS_DMA_READ : BUS_DMA_WRITE)) {
1312 		sc_xfer->error = XS_DRIVER_STUFFUP;
1313 		ahci_atapi_complete(chp, xfer, slot);
1314 		return;
1315 	}
1316 	cmd_h->cmdh_flags = htole16(
1317 	    ((sc_xfer->xs_control & XS_CTL_DATA_OUT) ? AHCI_CMDH_F_WR : 0) |
1318 	    RHD_FISLEN / 4 | AHCI_CMDH_F_A);
1319 	cmd_h->cmdh_prdbc = 0;
1320 	AHCI_CMDH_SYNC(sc, achp, slot,
1321 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1322 
1323 	if (xfer->c_flags & C_POLL) {
1324 		/* polled command, disable interrupts */
1325 		AHCI_WRITE(sc, AHCI_GHC,
1326 		    AHCI_READ(sc, AHCI_GHC) & ~AHCI_GHC_IE);
1327 	}
1328 	chp->ch_flags |= ATACH_IRQ_WAIT;
1329 	chp->ch_status = 0;
1330 	/* start command */
1331 	AHCI_WRITE(sc, AHCI_P_CI(chp->ch_channel), 1 << slot);
1332 	/* and says we started this command */
1333 	achp->ahcic_cmds_active |= 1 << slot;
1334 
1335 	if ((xfer->c_flags & C_POLL) == 0) {
1336 		chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1337 		callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
1338 		    ahci_timeout, chp);
1339 		return;
1340 	}
1341 	/*
1342 	 * Polled command.
1343 	 */
1344 	for (i = 0; i < ATA_DELAY / 10; i++) {
1345 		if (sc_xfer->xs_status & XS_STS_DONE)
1346 			break;
1347 		ahci_intr_port(sc, achp);
1348 		delay(10000);
1349 	}
1350 	AHCIDEBUG_PRINT(("%s port %d poll end GHC 0x%x IS 0x%x list 0x%x%x fis 0x%x%x CMD 0x%x CI 0x%x\n", AHCINAME(sc), channel,
1351 	    AHCI_READ(sc, AHCI_GHC), AHCI_READ(sc, AHCI_IS),
1352 	    AHCI_READ(sc, AHCI_P_CLBU(channel)), AHCI_READ(sc, AHCI_P_CLB(channel)),
1353 	    AHCI_READ(sc, AHCI_P_FBU(channel)), AHCI_READ(sc, AHCI_P_FB(channel)),
1354 	    AHCI_READ(sc, AHCI_P_CMD(channel)), AHCI_READ(sc, AHCI_P_CI(channel))),
1355 	    DEBUG_XFERS);
1356 	if ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
1357 		sc_xfer->error = XS_TIMEOUT;
1358 		ahci_atapi_complete(chp, xfer, slot);
1359 	}
1360 	/* reenable interrupts */
1361 	AHCI_WRITE(sc, AHCI_GHC, AHCI_READ(sc, AHCI_GHC) | AHCI_GHC_IE);
1362 }
1363 
1364 int
1365 ahci_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
1366 {
1367 	int slot = 0; /* XXX slot */
1368 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1369 	int drive = xfer->c_drive;
1370 	struct ahci_channel *achp = (struct ahci_channel *)chp;
1371 	struct ahci_softc *sc = (struct ahci_softc *)chp->ch_atac;
1372 
1373 	AHCIDEBUG_PRINT(("ahci_atapi_complete channel %d\n", chp->ch_channel),
1374 	    DEBUG_FUNCS);
1375 
1376 	achp->ahcic_cmds_active &= ~(1 << slot);
1377 	chp->ch_flags &= ~ATACH_IRQ_WAIT;
1378 	if (xfer->c_flags & C_TIMEOU) {
1379 		sc_xfer->error = XS_TIMEOUT;
1380 	} else {
1381 		callout_stop(&chp->ch_callout);
1382 		sc_xfer->error = 0;
1383 	}
1384 
1385 	chp->ch_queue->active_xfer = NULL;
1386 	bus_dmamap_sync(sc->sc_dmat, achp->ahcic_datad[slot], 0,
1387 	    achp->ahcic_datad[slot]->dm_mapsize,
1388 	    (sc_xfer->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
1389 	    BUS_DMASYNC_POSTWRITE);
1390 	bus_dmamap_unload(sc->sc_dmat, achp->ahcic_datad[slot]);
1391 
1392 	if (chp->ch_drive[drive].drive_flags & DRIVE_WAITDRAIN) {
1393 		ahci_atapi_kill_xfer(chp, xfer, KILL_GONE);
1394 		chp->ch_drive[drive].drive_flags &= ~DRIVE_WAITDRAIN;
1395 		wakeup(&chp->ch_queue->active_xfer);
1396 		return 0;
1397 	}
1398 	ata_free_xfer(chp, xfer);
1399 
1400 	AHCI_CMDH_SYNC(sc, achp, slot,
1401 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1402 	sc_xfer->resid = sc_xfer->datalen;
1403 	sc_xfer->resid -= le32toh(achp->ahcic_cmdh[slot].cmdh_prdbc);
1404 	AHCIDEBUG_PRINT(("ahci_atapi_complete datalen %d resid %d\n",
1405 	    sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
1406 	if (chp->ch_status & WDCS_ERR &&
1407 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1408 	    sc_xfer->resid == sc_xfer->datalen)) {
1409 		sc_xfer->error = XS_SHORTSENSE;
1410 		sc_xfer->sense.atapi_sense = chp->ch_error;
1411 		if ((sc_xfer->xs_periph->periph_quirks &
1412 		    PQUIRK_NOSENSE) == 0) {
1413 			/* ask scsipi to send a REQUEST_SENSE */
1414 			sc_xfer->error = XS_BUSY;
1415 			sc_xfer->status = SCSI_CHECK;
1416 		}
1417 	}
1418 	scsipi_done(sc_xfer);
1419 	atastart(chp);
1420 	return 0;
1421 }
1422 
1423 void
1424 ahci_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, int reason)
1425 {
1426 	struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1427 	struct ahci_channel *achp = (struct ahci_channel *)chp;
1428 	int slot = 0; /* XXX slot */
1429 
1430 	achp->ahcic_cmds_active &= ~(1 << slot);
1431 
1432 	/* remove this command from xfer queue */
1433 	switch (reason) {
1434 	case KILL_GONE:
1435 		sc_xfer->error = XS_DRIVER_STUFFUP;
1436 		break;
1437 	case KILL_RESET:
1438 		sc_xfer->error = XS_RESET;
1439 		break;
1440 	default:
1441 		printf("ahci_ata_atapi_kill_xfer: unknown reason %d\n", reason);
1442 		panic("ahci_ata_atapi_kill_xfer");
1443 	}
1444 	ata_free_xfer(chp, xfer);
1445 	scsipi_done(sc_xfer);
1446 }
1447 
1448 void
1449 ahci_atapi_probe_device(struct atapibus_softc *sc, int target)
1450 {
1451 	struct scsipi_channel *chan = sc->sc_channel;
1452 	struct scsipi_periph *periph;
1453 	struct ataparams ids;
1454 	struct ataparams *id = &ids;
1455 	struct ahci_softc *ahcic =
1456 	    device_private(chan->chan_adapter->adapt_dev);
1457 	struct atac_softc *atac = &ahcic->sc_atac;
1458 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
1459 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
1460 	struct scsipibus_attach_args sa;
1461 	char serial_number[21], model[41], firmware_revision[9];
1462 	int s;
1463 
1464 	/* skip if already attached */
1465 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
1466 		return;
1467 
1468 	/* if no ATAPI device detected at attach time, skip */
1469 	if ((drvp->drive_flags & DRIVE_ATAPI) == 0) {
1470 		AHCIDEBUG_PRINT(("ahci_atapi_probe_device: drive %d "
1471 		    "not present\n", target), DEBUG_PROBE);
1472 		return;
1473 	}
1474 
1475 	/* Some ATAPI devices need a bit more time after software reset. */
1476 	delay(5000);
1477 	if (ata_get_params(drvp,  AT_WAIT, id) == 0) {
1478 #ifdef ATAPI_DEBUG_PROBE
1479 		printf("%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
1480 		    AHCINAME(ahcic), target,
1481 		    id->atap_config & ATAPI_CFG_CMD_MASK,
1482 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
1483 #endif
1484 		periph = scsipi_alloc_periph(M_NOWAIT);
1485 		if (periph == NULL) {
1486 			aprint_error_dev(sc->sc_dev,
1487 			    "unable to allocate periph for drive %d\n",
1488 			    target);
1489 			return;
1490 		}
1491 		periph->periph_dev = NULL;
1492 		periph->periph_channel = chan;
1493 		periph->periph_switch = &atapi_probe_periphsw;
1494 		periph->periph_target = target;
1495 		periph->periph_lun = 0;
1496 		periph->periph_quirks = PQUIRK_ONLYBIG;
1497 
1498 #ifdef SCSIPI_DEBUG
1499 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
1500 		    SCSIPI_DEBUG_TARGET == target)
1501 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
1502 #endif
1503 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
1504 		if (id->atap_config & ATAPI_CFG_REMOV)
1505 			periph->periph_flags |= PERIPH_REMOVABLE;
1506 		if (periph->periph_type == T_SEQUENTIAL) {
1507 			s = splbio();
1508 			drvp->drive_flags |= DRIVE_ATAPIST;
1509 			splx(s);
1510 		}
1511 
1512 		sa.sa_periph = periph;
1513 		sa.sa_inqbuf.type =  ATAPI_CFG_TYPE(id->atap_config);
1514 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
1515 		    T_REMOV : T_FIXED;
1516 		scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
1517 		scsipi_strvis((u_char *)serial_number, 20, id->atap_serial,
1518 		    20);
1519 		scsipi_strvis((u_char *)firmware_revision, 8,
1520 		    id->atap_revision, 8);
1521 		sa.sa_inqbuf.vendor = model;
1522 		sa.sa_inqbuf.product = serial_number;
1523 		sa.sa_inqbuf.revision = firmware_revision;
1524 
1525 		/*
1526 		 * Determine the operating mode capabilities of the device.
1527 		 */
1528 		if ((id->atap_config & ATAPI_CFG_CMD_MASK) == ATAPI_CFG_CMD_16)
1529 			periph->periph_cap |= PERIPH_CAP_CMD16;
1530 		/* XXX This is gross. */
1531 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
1532 
1533 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
1534 
1535 		if (drvp->drv_softc)
1536 			ata_probe_caps(drvp);
1537 		else {
1538 			s = splbio();
1539 			drvp->drive_flags &= ~DRIVE_ATAPI;
1540 			splx(s);
1541 		}
1542 	} else {
1543 		AHCIDEBUG_PRINT(("ahci_atapi_get_params: ATAPI_IDENTIFY_DEVICE "
1544 		    "failed for drive %s:%d:%d: error 0x%x\n",
1545 		    AHCINAME(ahcic), chp->ch_channel, target,
1546 		    chp->ch_error), DEBUG_PROBE);
1547 		s = splbio();
1548 		drvp->drive_flags &= ~DRIVE_ATAPI;
1549 		splx(s);
1550 	}
1551 }
1552 #endif /* NATAPIBUS */
1553