xref: /netbsd-src/sys/dev/ic/siisata.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* $NetBSD: siisata.c,v 1.39 2018/11/19 19:52:08 jdolecek 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.39 2018/11/19 19:52:08 jdolecek Exp $");
83 
84 #include <sys/types.h>
85 #include <sys/param.h>
86 #include <sys/kernel.h>
87 #include <sys/malloc.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/ata/satapmpreg.h>
100 #include <dev/ic/siisatavar.h>
101 #include <dev/ic/siisatareg.h>
102 
103 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
104 
105 #include "atapibus.h"
106 
107 #ifdef SIISATA_DEBUG
108 int siisata_debug_mask = 0;
109 #endif
110 
111 #define ATA_DELAY 10000		/* 10s for a drive I/O */
112 #define WDC_RESET_WAIT 31000	/* 31s for drive reset */
113 
114 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
115 #if _BYTE_ORDER == _LITTLE_ENDIAN
116 #define bus_space_read_stream_4 bus_space_read_4
117 #define bus_space_read_region_stream_4 bus_space_read_region_4
118 #else
119 static inline uint32_t
120 bus_space_read_stream_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
121 {
122 	return htole32(bus_space_read_4(t, h, o));
123 }
124 
125 static inline void
126 bus_space_read_region_stream_4(bus_space_tag_t t, bus_space_handle_t h,
127     bus_size_t o, uint32_t *p, bus_size_t c)
128 {
129 	bus_space_read_region_4(t, h, o, p, c);
130 	for (bus_size_t i = 0; i < c; i++) {
131 		p[i] = htole32(p[i]);
132 	}
133 }
134 #endif
135 #endif
136 
137 static void siisata_attach_port(struct siisata_softc *, int);
138 static void siisata_intr_port(struct siisata_channel *);
139 
140 void siisata_probe_drive(struct ata_channel *);
141 void siisata_setup_channel(struct ata_channel *);
142 
143 int siisata_ata_bio(struct ata_drive_datas *, struct ata_xfer *);
144 void siisata_reset_drive(struct ata_drive_datas *, int, uint32_t *);
145 void siisata_reset_channel(struct ata_channel *, int);
146 int siisata_ata_addref(struct ata_drive_datas *);
147 void siisata_ata_delref(struct ata_drive_datas *);
148 void siisata_killpending(struct ata_drive_datas *);
149 
150 int siisata_cmd_start(struct ata_channel *, struct ata_xfer *);
151 int siisata_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
152 void siisata_cmd_poll(struct ata_channel *, struct ata_xfer *);
153 void siisata_cmd_abort(struct ata_channel *, struct ata_xfer *);
154 void siisata_cmd_done(struct ata_channel *, struct ata_xfer *, int);
155 static void siisata_cmd_done_end(struct ata_channel *, struct ata_xfer *);
156 void siisata_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
157 
158 int siisata_bio_start(struct ata_channel *, struct ata_xfer *);
159 int siisata_bio_complete(struct ata_channel *, struct ata_xfer *, int);
160 void siisata_bio_poll(struct ata_channel *, struct ata_xfer *);
161 void siisata_bio_abort(struct ata_channel *, struct ata_xfer *);
162 void siisata_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
163 int siisata_exec_command(struct ata_drive_datas *, struct ata_xfer *);
164 
165 static int siisata_reinit_port(struct ata_channel *, int);
166 static void siisata_device_reset(struct ata_channel *);
167 static void siisata_activate_prb(struct siisata_channel *, int);
168 static void siisata_deactivate_prb(struct siisata_channel *, int);
169 static int siisata_dma_setup(struct ata_channel *, int, void *, size_t, int);
170 static void siisata_channel_recover(struct ata_channel *, int, uint32_t);
171 
172 #if NATAPIBUS > 0
173 void siisata_atapibus_attach(struct atabus_softc *);
174 void siisata_atapi_probe_device(struct atapibus_softc *, int);
175 void siisata_atapi_minphys(struct buf *);
176 int siisata_atapi_start(struct ata_channel *,struct ata_xfer *);
177 int siisata_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
178 void siisata_atapi_poll(struct ata_channel *, struct ata_xfer *);
179 void siisata_atapi_abort(struct ata_channel *, struct ata_xfer *);
180 void siisata_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
181 void siisata_atapi_scsipi_request(struct scsipi_channel *,
182     scsipi_adapter_req_t, void *);
183 void siisata_atapi_kill_pending(struct scsipi_periph *);
184 #endif /* NATAPIBUS */
185 
186 const struct ata_bustype siisata_ata_bustype = {
187 	SCSIPI_BUSTYPE_ATA,
188 	siisata_ata_bio,
189 	siisata_reset_drive,
190 	siisata_reset_channel,
191 	siisata_exec_command,
192 	ata_get_params,
193 	siisata_ata_addref,
194 	siisata_ata_delref,
195 	siisata_killpending,
196 	siisata_channel_recover,
197 };
198 
199 #if NATAPIBUS > 0
200 static const struct scsipi_bustype siisata_atapi_bustype = {
201 	SCSIPI_BUSTYPE_ATAPI,
202 	atapi_scsipi_cmd,
203 	atapi_interpret_sense,
204 	atapi_print_addr,
205 	siisata_atapi_kill_pending,
206 	NULL,
207 };
208 #endif /* NATAPIBUS */
209 
210 
211 void
212 siisata_attach(struct siisata_softc *sc)
213 {
214 	int i;
215 
216 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
217 	    SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)), DEBUG_FUNCS);
218 
219 	sc->sc_atac.atac_cap = ATAC_CAP_DMA | ATAC_CAP_UDMA | ATAC_CAP_NCQ;
220 	sc->sc_atac.atac_pio_cap = 4;
221 	sc->sc_atac.atac_dma_cap = 2;
222 	sc->sc_atac.atac_udma_cap = 6;
223 	sc->sc_atac.atac_channels = sc->sc_chanarray;
224 	sc->sc_atac.atac_probe = siisata_probe_drive;
225 	sc->sc_atac.atac_bustype_ata = &siisata_ata_bustype;
226 	sc->sc_atac.atac_set_modes = siisata_setup_channel;
227 #if NATAPIBUS > 0
228 	sc->sc_atac.atac_atapibus_attach = siisata_atapibus_attach;
229 #endif
230 
231 	/* come out of reset state */
232 	GRWRITE(sc, GR_GC, 0);
233 
234 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
235 		siisata_attach_port(sc, i);
236 	}
237 
238 	SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n", SIISATANAME(sc),
239 	    __func__, GRREAD(sc, GR_GC)), DEBUG_FUNCS);
240 	return;
241 }
242 
243 static void
244 siisata_disable_port_interrupt(struct ata_channel *chp)
245 {
246 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
247 
248 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIEC), 0xffffffff);
249 }
250 
251 static void
252 siisata_enable_port_interrupt(struct ata_channel *chp)
253 {
254 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
255 
256 	/* clear any interrupts */
257 	(void)PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
258 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
259 	/* and enable CmdErrr+CmdCmpl interrupting */
260 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIES),
261 	    PR_PIS_CMDERRR | PR_PIS_CMDCMPL);
262 }
263 
264 static int
265 siisata_init_port(struct siisata_softc *sc, int port)
266 {
267 	struct siisata_channel *schp;
268 	struct ata_channel *chp;
269 	int error;
270 
271 	schp = &sc->sc_channels[port];
272 	chp = (struct ata_channel *)schp;
273 
274 	/*
275 	 * Come out of reset. Disable no clearing of PR_PIS_CMDCMPL on read
276 	 * of PR_PSS. Disable 32-bit PRB activation, we use 64-bit activation.
277 	 */
278 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCC),
279 	    PR_PC_32BA | PR_PC_INCOR | PR_PC_PORT_RESET);
280 	/* initialize port */
281 	error = siisata_reinit_port(chp, -1);
282 	/* enable CmdErrr+CmdCmpl interrupting */
283 	siisata_enable_port_interrupt(chp);
284 	/* enable port interrupt */
285 	GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
286 
287 	return error;
288 }
289 
290 static void
291 siisata_attach_port(struct siisata_softc *sc, int port)
292 {
293 	int j;
294 	int dmasize;
295 	int error;
296 	void *prbp;
297 	struct siisata_channel *schp;
298 	struct ata_channel *chp;
299 
300 	schp = &sc->sc_channels[port];
301 	chp = (struct ata_channel *)schp;
302 	sc->sc_chanarray[port] = chp;
303 	chp->ch_channel = port;
304 	chp->ch_atac = &sc->sc_atac;
305 	chp->ch_queue = ata_queue_alloc(SIISATA_MAX_SLOTS);
306 	if (chp->ch_queue == NULL) {
307 		aprint_error_dev(sc->sc_atac.atac_dev,
308 		    "port %d: can't allocate memory "
309 		    "for command queue\n", chp->ch_channel);
310 		return;
311 	}
312 
313 	dmasize = SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS;
314 
315 	SIISATA_DEBUG_PRINT(("%s: %s: dmasize: %d\n", SIISATANAME(sc),
316 	    __func__, dmasize), DEBUG_FUNCS);
317 
318 	error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
319 	    &schp->sch_prb_seg, 1, &schp->sch_prb_nseg, BUS_DMA_NOWAIT);
320 	if (error) {
321 		aprint_error_dev(sc->sc_atac.atac_dev,
322 		    "unable to allocate PRB table memory, "
323 		    "error=%d\n", error);
324 		return;
325 	}
326 
327 	error = bus_dmamem_map(sc->sc_dmat,
328 	    &schp->sch_prb_seg, schp->sch_prb_nseg,
329 	    dmasize, &prbp, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
330 	if (error) {
331 		aprint_error_dev(sc->sc_atac.atac_dev,
332 		    "unable to map PRB table memory, "
333 		    "error=%d\n", error);
334 		bus_dmamem_free(sc->sc_dmat,
335 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
336 		return;
337 	}
338 
339 	error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
340 	    BUS_DMA_NOWAIT, &schp->sch_prbd);
341 	if (error) {
342 		aprint_error_dev(sc->sc_atac.atac_dev,
343 		    "unable to create PRB table map, "
344 		    "error=%d\n", error);
345 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
346 		bus_dmamem_free(sc->sc_dmat,
347 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
348 		return;
349 	}
350 
351 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_prbd,
352 	    prbp, dmasize, NULL, BUS_DMA_NOWAIT);
353 	if (error) {
354 		aprint_error_dev(sc->sc_atac.atac_dev,
355 		    "unable to load PRB table map, "
356 		    "error=%d\n", error);
357 		bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
358 		bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
359 		bus_dmamem_free(sc->sc_dmat,
360 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
361 		return;
362 	}
363 
364 	for (j = 0; j < SIISATA_MAX_SLOTS; j++) {
365 		schp->sch_prb[j] = (struct siisata_prb *)
366 		    ((char *)prbp + SIISATA_CMD_SIZE * j);
367 		schp->sch_bus_prb[j] =
368 		    schp->sch_prbd->dm_segs[0].ds_addr +
369 		    SIISATA_CMD_SIZE * j;
370 		error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
371 		    SIISATA_NSGE, MAXPHYS, 0,
372 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
373 		    &schp->sch_datad[j]);
374 		if (error) {
375 			aprint_error_dev(sc->sc_atac.atac_dev,
376 			    "couldn't create xfer DMA map, error=%d\n",
377 			    error);
378 			return;
379 		}
380 	}
381 
382 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
383 	    PRX(chp->ch_channel, PRO_SSTATUS), 4, &schp->sch_sstatus) != 0) {
384 		aprint_error_dev(sc->sc_atac.atac_dev,
385 		    "couldn't map port %d SStatus regs\n",
386 		    chp->ch_channel);
387 		return;
388 	}
389 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
390 	    PRX(chp->ch_channel, PRO_SCONTROL), 4, &schp->sch_scontrol) != 0) {
391 		aprint_error_dev(sc->sc_atac.atac_dev,
392 		    "couldn't map port %d SControl regs\n",
393 		    chp->ch_channel);
394 		return;
395 	}
396 	if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
397 	    PRX(chp->ch_channel, PRO_SERROR), 4, &schp->sch_serror) != 0) {
398 		aprint_error_dev(sc->sc_atac.atac_dev,
399 		    "couldn't map port %d SError regs\n",
400 		    chp->ch_channel);
401 		return;
402 	}
403 
404 	(void)siisata_init_port(sc, port);
405 
406 	ata_channel_attach(chp);
407 
408 	return;
409 }
410 
411 void
412 siisata_childdetached(struct siisata_softc *sc, device_t child)
413 {
414 	struct ata_channel *chp;
415 
416 	for (int i = 0; i < sc->sc_atac.atac_nchannels; i++) {
417 		chp = sc->sc_chanarray[i];
418 
419 		if (child == chp->atabus)
420 			chp->atabus = NULL;
421 	}
422 }
423 
424 int
425 siisata_detach(struct siisata_softc *sc, int flags)
426 {
427 	struct atac_softc *atac = &sc->sc_atac;
428 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
429 	struct siisata_channel *schp;
430 	struct ata_channel *chp;
431 	int i, j, error;
432 
433 	if (adapt->adapt_refcnt != 0)
434 		return EBUSY;
435 
436 	for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
437 		schp = &sc->sc_channels[i];
438 		chp = sc->sc_chanarray[i];
439 
440 		if (chp->atabus != NULL) {
441 			if ((error = config_detach(chp->atabus, flags)) != 0)
442 				return error;
443 
444 			KASSERT(chp->atabus == NULL);
445 		}
446 
447 		if (chp->ch_flags & ATACH_DETACHED)
448 			continue;
449 
450 		for (j = 0; j < SIISATA_MAX_SLOTS; j++)
451 			bus_dmamap_destroy(sc->sc_dmat, schp->sch_datad[j]);
452 
453 		bus_dmamap_unload(sc->sc_dmat, schp->sch_prbd);
454 		bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
455 		bus_dmamem_unmap(sc->sc_dmat, schp->sch_prb[0],
456 		    SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS);
457 		bus_dmamem_free(sc->sc_dmat,
458 		    &schp->sch_prb_seg, schp->sch_prb_nseg);
459 
460 		ata_channel_detach(chp);
461 	}
462 
463 	/* leave the chip in reset */
464 	GRWRITE(sc, GR_GC, GR_GC_GLBLRST);
465 
466 	return 0;
467 }
468 
469 void
470 siisata_resume(struct siisata_softc *sc)
471 {
472 	/* come out of reset state */
473 	GRWRITE(sc, GR_GC, 0);
474 
475 	for (int port = 0; port < sc->sc_atac.atac_nchannels; port++) {
476 		int error;
477 
478 		error = siisata_init_port(sc, port);
479 		if (error) {
480 			struct siisata_channel *schp = &sc->sc_channels[port];
481 			struct ata_channel *chp = (struct ata_channel *)schp;
482 
483 			ata_channel_lock(chp);
484 			siisata_reset_channel(chp, AT_POLL);
485 			ata_channel_unlock(chp);
486 		}
487 	}
488 }
489 
490 int
491 siisata_intr(void *v)
492 {
493 	struct siisata_softc *sc = v;
494 	uint32_t is;
495 	int i, r = 0;
496 	while ((is = GRREAD(sc, GR_GIS))) {
497 		SIISATA_DEBUG_PRINT(("%s: %s: GR_GIS: 0x%08x\n",
498 		    SIISATANAME(sc), __func__, is), DEBUG_INTR);
499 		r = 1;
500 		for (i = 0; i < sc->sc_atac.atac_nchannels; i++)
501 			if (is & GR_GIS_PXIS(i))
502 				siisata_intr_port(&sc->sc_channels[i]);
503 	}
504 	return r;
505 }
506 
507 static void
508 siisata_intr_port(struct siisata_channel *schp)
509 {
510 	struct siisata_softc *sc =
511 	    (struct siisata_softc *)schp->ata_channel.ch_atac;
512 	struct ata_channel *chp = &schp->ata_channel;
513 	struct ata_xfer *xfer = NULL;
514 	uint32_t pss, pis, tfd = 0;
515 	bool recover = false;
516 
517 	/* get slot status, clearing completion interrupt */
518 	pss = PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
519 
520 	SIISATA_DEBUG_PRINT(("%s: %s port %d, pss 0x%x ",
521 	    SIISATANAME(sc), __func__, chp->ch_channel, pss),
522 	    DEBUG_INTR);
523 
524 	if (__predict_true((pss & PR_PSS_ATTENTION) == 0)) {
525 		SIISATA_DEBUG_PRINT(("no attention"), DEBUG_INTR);
526 		goto process;
527 	}
528 
529 	pis = PRREAD(sc, PRX(chp->ch_channel, PRO_PIS));
530 
531 	SIISATA_DEBUG_PRINT(("pis 0x%x\n", pss), DEBUG_INTR);
532 
533 	if (pis & PR_PIS_CMDERRR) {
534 		uint32_t ec;
535 
536 		ec = PRREAD(sc, PRX(chp->ch_channel, PRO_PCE));
537 		SIISATA_DEBUG_PRINT(("ec %d\n", ec), DEBUG_INTR);
538 
539 		/* emulate a CRC error by default */
540 		tfd = ATACH_ERR_ST(WDCE_CRC, WDCS_ERR);
541 
542 		if (ec <= PR_PCE_DATAFISERROR) {
543 			if (ec == PR_PCE_DEVICEERROR
544 			    && (chp->ch_flags & ATACH_NCQ) == 0) {
545 				xfer = ata_queue_get_active_xfer(chp);
546 
547 				/* read in specific information about error */
548 				uint32_t prbfis = bus_space_read_stream_4(
549 				    sc->sc_prt, sc->sc_prh,
550     				    PRSX(chp->ch_channel, xfer->c_slot,
551 				    PRSO_FIS));
552 
553 				/* get status and error */
554 				int ntfd = satafis_rdh_parse(chp,
555 				    (uint8_t *)&prbfis);
556 
557 				if (ATACH_ST(ntfd) & WDCS_ERR)
558 					tfd = ntfd;
559 			}
560 
561 			/*
562 			 * We don't expect the recovery to trigger error,
563 			 * but handle this just in case.
564 			 */
565 			if (!ISSET(chp->ch_flags, ATACH_RECOVERING))
566 				recover = true;
567 			else {
568 				aprint_error_dev(sc->sc_atac.atac_dev,
569 				    "error ec %x while recovering\n", ec);
570 
571 				/* Command will be marked as errored out */
572 				pss = 0;
573 			}
574 		} else {
575 			aprint_error_dev(sc->sc_atac.atac_dev, "fatal error %d"
576 			    " on port %d (ctx 0x%x), resetting\n",
577 			    ec, chp->ch_channel,
578 			    PRREAD(sc, PRX(chp->ch_channel, PRO_PCR)));
579 
580 			/* okay, we have a "Fatal Error" */
581 			ata_channel_lock(chp);
582 			siisata_device_reset(chp);
583 			ata_channel_unlock(chp);
584 		}
585 	}
586 
587 	/* clear some (ok, all) ints */
588 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
589 
590 	if (__predict_false(recover))
591 		ata_channel_freeze(chp);
592 
593 process:
594 	if (xfer != NULL) {
595 		xfer->ops->c_intr(chp, xfer, tfd);
596 	} else {
597 		/*
598 		 * For NCQ, HBA halts processing when error is notified,
599 		 * and any further D2H FISes are ignored until the error
600 		 * condition is cleared. Hence if a command is inactive,
601 		 * it means it actually already finished successfully.
602 		 * Note: active slots can change as c_intr() callback
603 		 * can activate another command(s), so must only process
604 		 * commands active before we start processing.
605 		 */
606 		uint32_t aslots = ata_queue_active(chp);
607 
608 		for (int slot=0; slot < SIISATA_MAX_SLOTS; slot++) {
609 			if ((aslots & __BIT(slot)) != 0 &&
610 			    (pss & PR_PXSS(slot)) == 0) {
611 				xfer = ata_queue_hwslot_to_xfer(chp, slot);
612 				xfer->ops->c_intr(chp, xfer, 0);
613 			}
614 		}
615 	}
616 
617 	if (__predict_false(recover)) {
618 		ata_channel_lock(chp);
619 		ata_channel_thaw_locked(chp);
620 		ata_thread_run(chp, 0, ATACH_TH_RECOVERY, tfd);
621 		ata_channel_unlock(chp);
622 	}
623 }
624 
625 /* Recover channel after transfer aborted */
626 void
627 siisata_channel_recover(struct ata_channel *chp, int flags, uint32_t tfd)
628 {
629 	struct siisata_channel *schp = (struct siisata_channel *)chp;
630 	struct siisata_softc *sc =
631 	    (struct siisata_softc *)schp->ata_channel.ch_atac;
632 	int drive;
633 
634 	ata_channel_lock_owned(chp);
635 
636 	if (chp->ch_ndrives > PMP_PORT_CTL) {
637 		/* Get PM port number for the device in error */
638 		int pcr = PRREAD(sc, PRX(chp->ch_channel, PRO_PCR));
639 		drive = PRO_PCR_PMP(pcr);
640 	} else
641 		drive = 0;
642 
643 	/*
644 	 * If BSY or DRQ bits are set, must execute COMRESET to return
645 	 * device to idle state. Otherwise, commands can be reissued
646 	 * after reinitalization of port. After that, need to execute
647 	 * READ LOG EXT for NCQ to unblock device processing if COMRESET
648 	 * was not done.
649 	 */
650 	if ((ATACH_ST(tfd) & (WDCS_BSY|WDCS_DRQ)) != 0) {
651 		siisata_device_reset(chp);
652 		goto out;
653 	}
654 
655 	KASSERT(drive >= 0);
656 	(void)siisata_reinit_port(chp, drive);
657 
658 	ata_recovery_resume(chp, drive, tfd, flags);
659 
660 out:
661 	/* Drive unblocked, back to normal operation */
662 	return;
663 }
664 
665 void
666 siisata_reset_drive(struct ata_drive_datas *drvp, int flags, uint32_t *sigp)
667 {
668 	struct ata_channel *chp = drvp->chnl_softc;
669 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
670 	struct siisata_channel *schp = (struct siisata_channel *)chp;
671 	struct siisata_prb *prb;
672 	uint8_t c_slot;
673 	uint32_t pss, pis;
674 	int i;
675 	bool timed_out;
676 
677 	ata_channel_lock_owned(chp);
678 
679 	if (siisata_reinit_port(chp, drvp->drive))
680 		siisata_reset_channel(chp, flags);
681 
682 	/* get a slot for running the command on */
683 	if (!ata_queue_alloc_slot(chp, &c_slot, ATA_MAX_OPENINGS)) {
684 		panic("%s: %s: failed to get xfer for reset, port %d\n",
685 		    device_xname(sc->sc_atac.atac_dev),
686 		    __func__, chp->ch_channel);
687 		/* NOTREACHED */
688 	}
689 
690 	prb = schp->sch_prb[c_slot];
691 	memset(prb, 0, SIISATA_CMD_SIZE);
692 	prb->prb_control =
693 	    htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
694 	KASSERT(drvp->drive <= PMP_PORT_CTL);
695 	prb->prb_fis[rhd_c] = drvp->drive;
696 
697 	siisata_disable_port_interrupt(chp);
698 
699 	siisata_activate_prb(schp, c_slot);
700 
701 	timed_out = true;
702 	for(i = 0; i < WDC_RESET_WAIT / 10; i++) {
703 		pss = PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
704 		if ((pss & PR_PXSS(c_slot)) == 0) {
705 			timed_out = false;
706 			break;
707 		}
708 		if (pss & PR_PSS_ATTENTION)
709 			break;
710 		ata_delay(chp, 10, "siiprb", flags);
711 	}
712 
713 	siisata_deactivate_prb(schp, c_slot);
714 
715 	if ((pss & PR_PSS_ATTENTION) != 0) {
716 		pis = PRREAD(sc, PRX(chp->ch_channel, PRO_PIS));
717 		const uint32_t ps = PRREAD(sc, PRX(chp->ch_channel, PRO_PS));
718 		const u_int slot = PR_PS_ACTIVE_SLOT(ps);
719 
720 		if (slot != c_slot)
721 			device_printf(sc->sc_atac.atac_dev, "%s port %d "
722 			    "drive %d slot %d c_slot %d", __func__,
723 			    chp->ch_channel, drvp->drive, slot, c_slot);
724 
725 		PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), pis &
726 		    PR_PIS_CMDERRR);
727 	}
728 
729 	siisata_enable_port_interrupt(chp);
730 
731 	if (timed_out) {
732 		/* timeout */
733 		siisata_device_reset(chp);	/* XXX is this right? */
734 		if (sigp)
735 			*sigp = 0xffffffff;
736 	} else {
737 		/* read the signature out of the FIS */
738 		if (sigp) {
739 			*sigp = 0;
740 			*sigp |= (PRREAD(sc, PRSX(chp->ch_channel, c_slot,
741 			    PRSO_FIS+0x4)) & 0x00ffffff) << 8;
742 			*sigp |= PRREAD(sc, PRSX(chp->ch_channel, c_slot,
743 			    PRSO_FIS+0xc)) & 0xff;
744 		}
745 	}
746 
747 	ata_queue_free_slot(chp, c_slot);
748 }
749 
750 void
751 siisata_reset_channel(struct ata_channel *chp, int flags)
752 {
753 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
754 	struct siisata_channel *schp = (struct siisata_channel *)chp;
755 
756 	SIISATA_DEBUG_PRINT(("%s: %s port %d\n", SIISATANAME(sc), __func__,
757 	    chp->ch_channel), DEBUG_FUNCS);
758 
759 	ata_channel_lock_owned(chp);
760 
761 	if (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
762 	    schp->sch_sstatus, flags) != SStatus_DET_DEV) {
763 		aprint_error("%s port %d: reset failed\n",
764 		    SIISATANAME(sc), chp->ch_channel);
765 		/* XXX and then ? */
766 	}
767 
768 	siisata_device_reset(chp);
769 
770 	PRWRITE(sc, PRX(chp->ch_channel, PRO_SERROR),
771 	    PRREAD(sc, PRX(chp->ch_channel, PRO_SERROR)));
772 
773 	return;
774 }
775 
776 int
777 siisata_ata_addref(struct ata_drive_datas *drvp)
778 {
779 	return 0;
780 }
781 
782 void
783 siisata_ata_delref(struct ata_drive_datas *drvp)
784 {
785 	return;
786 }
787 
788 void
789 siisata_killpending(struct ata_drive_datas *drvp)
790 {
791 	return;
792 }
793 
794 void
795 siisata_probe_drive(struct ata_channel *chp)
796 {
797 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
798 	struct siisata_channel *schp = (struct siisata_channel *)chp;
799 	int i;
800 	uint32_t sig;
801 	struct siisata_prb *prb;
802 	bool timed_out;
803 	uint8_t c_slot;
804 
805 	SIISATA_DEBUG_PRINT(("%s: %s: port %d start\n", SIISATANAME(sc),
806 	    __func__, chp->ch_channel), DEBUG_FUNCS);
807 
808 	ata_channel_lock(chp);
809 
810 	/* get a slot for running the command on */
811 	if (!ata_queue_alloc_slot(chp, &c_slot, ATA_MAX_OPENINGS)) {
812 		aprint_error_dev(sc->sc_atac.atac_dev,
813 		    "%s: failed to get xfer port %d\n",
814 		    __func__, chp->ch_channel);
815 		ata_channel_unlock(chp);
816 		return;
817 	}
818 
819 	/*
820 	 * disable port interrupt as we're polling for PHY up and
821 	 * prb completion
822 	 */
823 	siisata_disable_port_interrupt(chp);
824 
825 	switch(sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
826 		schp->sch_sstatus, AT_WAIT)) {
827 	case SStatus_DET_DEV:
828 		/* clear any interrupts */
829 		(void)PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
830 		PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
831 
832 		/* wait for ready */
833 		timed_out = 1;
834 		for (i = 0; i < ATA_DELAY / 10; i++) {
835 			if (PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) &
836 			    PR_PS_PORT_READY) {
837 				timed_out = 0;
838 				break;
839 			}
840 
841 			ata_delay(chp, 10, "siiprbrd", AT_WAIT);
842 		}
843 		if (timed_out) {
844 			aprint_error_dev(sc->sc_atac.atac_dev,
845 			    "timed out waiting for PORT_READY on port %d, "
846 			    "reinitializing\n", chp->ch_channel);
847 			if (siisata_reinit_port(chp, -1))
848 				siisata_reset_channel(chp, AT_WAIT);
849 		}
850 
851 		prb = schp->sch_prb[c_slot];
852 		memset(prb, 0, SIISATA_CMD_SIZE);
853 		prb->prb_control = htole16(PRB_CF_SOFT_RESET);
854 		prb->prb_fis[rhd_c] = PMP_PORT_CTL;
855 
856 		siisata_activate_prb(schp, c_slot);
857 
858 		timed_out = 1;
859 		for(i = 0; i < WDC_RESET_WAIT / 10; i++) {
860 			if ((PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
861 			    PR_PXSS(c_slot)) == 0) {
862 				/* prb completed */
863 				timed_out = 0;
864 				break;
865 			}
866 			if (PRREAD(sc, PRX(chp->ch_channel, PRO_PIS)) &
867 			    PR_PIS_CMDERRR) {
868 				/* we got an error; handle as timeout */
869 				break;
870 			}
871 
872 			ata_delay(chp, 10, "siiprb", AT_WAIT);
873 		}
874 
875 		siisata_deactivate_prb(schp, c_slot);
876 
877 		if (timed_out) {
878 			aprint_error_dev(sc->sc_atac.atac_dev,
879 			    "SOFT_RESET failed on port %d (error %d PSS 0x%x PIS 0x%x), "
880 			    "resetting\n", chp->ch_channel,
881 			    PRREAD(sc, PRX(chp->ch_channel, PRO_PCE)),
882 			    PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)),
883 			    PRREAD(sc, PRX(chp->ch_channel, PRO_PIS)));
884 			if (siisata_reinit_port(chp, -1))
885 				siisata_reset_channel(chp, AT_WAIT);
886 			break;
887 		}
888 
889 		/* read the signature out of the FIS */
890 		sig = 0;
891 		sig |= (PRREAD(sc, PRSX(chp->ch_channel, c_slot,
892 		    PRSO_FIS+0x4)) & 0x00ffffff) << 8;
893 		sig |= PRREAD(sc, PRSX(chp->ch_channel, c_slot,
894 		    PRSO_FIS+0xc)) & 0xff;
895 
896 		SIISATA_DEBUG_PRINT(("%s: %s: sig=0x%08x\n", SIISATANAME(sc),
897 		    __func__, sig), DEBUG_PROBE);
898 
899 		if (sig == 0x96690101)
900 			PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS),
901 			    PR_PC_PMP_ENABLE);
902 		sata_interpret_sig(chp, 0, sig);
903 		break;
904 	default:
905 		break;
906 	}
907 
908 	siisata_enable_port_interrupt(chp);
909 
910 	ata_queue_free_slot(chp, c_slot);
911 
912 	ata_channel_unlock(chp);
913 
914 	SIISATA_DEBUG_PRINT(("%s: %s: port %d done\n", SIISATANAME(sc),
915 	    __func__, chp->ch_channel), DEBUG_PROBE);
916 	return;
917 }
918 
919 void
920 siisata_setup_channel(struct ata_channel *chp)
921 {
922 	return;
923 }
924 
925 static const struct ata_xfer_ops siisata_cmd_xfer_ops = {
926 	.c_start = siisata_cmd_start,
927 	.c_intr = siisata_cmd_complete,
928 	.c_poll = siisata_cmd_poll,
929 	.c_abort = siisata_cmd_abort,
930 	.c_kill_xfer = siisata_cmd_kill_xfer,
931 };
932 
933 int
934 siisata_exec_command(struct ata_drive_datas *drvp, struct ata_xfer *xfer)
935 {
936 	struct ata_channel *chp = drvp->chnl_softc;
937 	struct ata_command *ata_c = &xfer->c_ata_c;
938 	int ret;
939 	int s;
940 
941 	SIISATA_DEBUG_PRINT(("%s: %s begins\n",
942 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
943 	    DEBUG_FUNCS);
944 
945 	if (ata_c->flags & AT_POLL)
946 		xfer->c_flags |= C_POLL;
947 	if (ata_c->flags & AT_WAIT)
948 		xfer->c_flags |= C_WAIT;
949 	xfer->c_drive = drvp->drive;
950 	xfer->c_databuf = ata_c->data;
951 	xfer->c_bcount = ata_c->bcount;
952 	xfer->ops = &siisata_cmd_xfer_ops;
953 	s = splbio();
954 	ata_exec_xfer(chp, xfer);
955 #ifdef DIAGNOSTIC
956 	if ((ata_c->flags & AT_POLL) != 0 &&
957 	    (ata_c->flags & AT_DONE) == 0)
958 		panic("%s: polled command not done", __func__);
959 #endif
960 	if (ata_c->flags & AT_DONE) {
961 		ret = ATACMD_COMPLETE;
962 	} else {
963 		if (ata_c->flags & AT_WAIT) {
964 			ata_wait_cmd(chp, xfer);
965 			ret = ATACMD_COMPLETE;
966 		} else {
967 			ret = ATACMD_QUEUED;
968 		}
969 	}
970 	splx(s);
971 	SIISATA_DEBUG_PRINT( ("%s: %s ends\n",
972 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
973 	    DEBUG_FUNCS);
974 	return ret;
975 }
976 
977 int
978 siisata_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
979 {
980 	struct siisata_channel *schp = (struct siisata_channel *)chp;
981 	struct ata_command *ata_c = &xfer->c_ata_c;
982 	struct siisata_prb *prb;
983 
984 	SIISATA_DEBUG_PRINT(("%s: %s port %d drive %d command 0x%x, slot %d\n",
985 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__,
986 	    chp->ch_channel, xfer->c_drive, ata_c->r_command, xfer->c_slot),
987 	    DEBUG_FUNCS|DEBUG_XFERS);
988 
989 	ata_channel_lock_owned(chp);
990 
991 	prb = schp->sch_prb[xfer->c_slot];
992 	memset(prb, 0, SIISATA_CMD_SIZE);
993 
994 	satafis_rhd_construct_cmd(ata_c, prb->prb_fis);
995 	KASSERT(xfer->c_drive <= PMP_PORT_CTL);
996 	prb->prb_fis[rhd_c] |= xfer->c_drive;
997 
998 	if (ata_c->r_command == ATA_DATA_SET_MANAGEMENT) {
999 		prb->prb_control |= htole16(PRB_CF_PROTOCOL_OVERRIDE);
1000 		prb->prb_protocol_override |= htole16(PRB_PO_WRITE);
1001 	}
1002 
1003 	if (siisata_dma_setup(chp, xfer->c_slot,
1004 	    (ata_c->flags & (AT_READ | AT_WRITE)) ? ata_c->data : NULL,
1005 	    ata_c->bcount,
1006 	    (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
1007 		ata_c->flags |= AT_DF;
1008 		return ATASTART_ABORT;
1009 	}
1010 
1011 	if (xfer->c_flags & C_POLL) {
1012 		/* polled command, disable interrupts */
1013 		prb->prb_control |= htole16(PRB_CF_INTERRUPT_MASK);
1014 		siisata_disable_port_interrupt(chp);
1015 	}
1016 
1017 	/* go for it */
1018 	siisata_activate_prb(schp, xfer->c_slot);
1019 
1020 	if ((ata_c->flags & AT_POLL) == 0) {
1021 		callout_reset(&chp->c_timo_callout, mstohz(ata_c->timeout),
1022 		    ata_timeout, chp);
1023 		return ATASTART_STARTED;
1024 	} else
1025 		return ATASTART_POLL;
1026 }
1027 
1028 void
1029 siisata_cmd_poll(struct ata_channel *chp, struct ata_xfer *xfer)
1030 {
1031 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1032 
1033 	/*
1034 	 * polled command
1035 	 */
1036 	for (int i = 0; i < xfer->c_ata_c.timeout * 10; i++) {
1037 		if (xfer->c_ata_c.flags & AT_DONE)
1038 			break;
1039 		siisata_intr_port(schp);
1040 		DELAY(100);
1041 	}
1042 
1043 	if ((xfer->c_ata_c.flags & AT_DONE) == 0) {
1044 		ata_timeout(xfer);
1045 	}
1046 
1047 	/* reenable interrupts */
1048 	siisata_enable_port_interrupt(chp);
1049 
1050 	SIISATA_DEBUG_PRINT(("%s: %s: done\n",
1051 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
1052 	    DEBUG_FUNCS);
1053 }
1054 
1055 void
1056 siisata_cmd_abort(struct ata_channel *chp, struct ata_xfer *xfer)
1057 {
1058 	siisata_cmd_complete(chp, xfer, 0);
1059 }
1060 
1061 void
1062 siisata_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1063     int reason)
1064 {
1065 	struct ata_command *ata_c = &xfer->c_ata_c;
1066 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1067 	bool deactivate = true;
1068 
1069 	switch (reason) {
1070 	case KILL_GONE_INACTIVE:
1071 		deactivate = false;
1072 		/* FALLTHROUGH */
1073 	case KILL_GONE:
1074 		ata_c->flags |= AT_GONE;
1075 		break;
1076 	case KILL_RESET:
1077 		ata_c->flags |= AT_RESET;
1078 		break;
1079 	case KILL_REQUEUE:
1080 		panic("%s: not supposed to be requeued\n", __func__);
1081 		break;
1082 	default:
1083 		panic("%s: port %d: unknown reason %d",
1084 		   __func__, chp->ch_channel, reason);
1085 	}
1086 
1087 	siisata_cmd_done_end(chp, xfer);
1088 
1089 	if (deactivate) {
1090 		siisata_deactivate_prb(schp, xfer->c_slot);
1091 		ata_deactivate_xfer(chp, xfer);
1092 	}
1093 }
1094 
1095 int
1096 siisata_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int tfd)
1097 {
1098 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1099 	struct ata_command *ata_c = &xfer->c_ata_c;
1100 #ifdef SIISATA_DEBUG
1101 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1102 #endif
1103 
1104 	SIISATA_DEBUG_PRINT(("%s: %s: port %d slot %d\n",
1105 	    SIISATANAME(sc), __func__,
1106 	    chp->ch_channel, xfer->c_slot), DEBUG_FUNCS);
1107 	SIISATA_DEBUG_PRINT(("%s: %s\n", SIISATANAME(sc), __func__),
1108 	    DEBUG_FUNCS|DEBUG_XFERS);
1109 
1110 	if (ata_waitdrain_xfer_check(chp, xfer))
1111 		return 0;
1112 
1113 	if (xfer->c_flags & C_TIMEOU)
1114 		ata_c->flags |= AT_TIMEOU;
1115 
1116 	if (ATACH_ST(tfd) & WDCS_BSY) {
1117 		ata_c->flags |= AT_TIMEOU;
1118 	} else if (ATACH_ST(tfd) & WDCS_ERR) {
1119 		ata_c->r_error = ATACH_ERR(tfd);
1120 		ata_c->flags |= AT_ERROR;
1121 	}
1122 
1123 	siisata_cmd_done(chp, xfer, tfd);
1124 
1125 	siisata_deactivate_prb(schp, xfer->c_slot);
1126 	ata_deactivate_xfer(chp, xfer);
1127 
1128 	if ((ata_c->flags & (AT_TIMEOU|AT_ERROR)) == 0)
1129 		atastart(chp);
1130 
1131 	return 0;
1132 }
1133 
1134 void
1135 siisata_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int tfd)
1136 {
1137 	uint32_t fis[howmany(RDH_FISLEN,sizeof(uint32_t))];
1138 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1139 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1140 	struct ata_command *ata_c = &xfer->c_ata_c;
1141 	uint16_t *idwordbuf;
1142 	int i;
1143 
1144 	SIISATA_DEBUG_PRINT(("%s: %s flags 0x%x error 0x%x\n", SIISATANAME(sc),
1145 	    __func__, ata_c->flags, ata_c->r_error), DEBUG_FUNCS|DEBUG_XFERS);
1146 
1147 	if (ata_c->flags & (AT_READ | AT_WRITE)) {
1148 		bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[xfer->c_slot], 0,
1149 		    schp->sch_datad[xfer->c_slot]->dm_mapsize,
1150 		    (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
1151 		    BUS_DMASYNC_POSTWRITE);
1152 		bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[xfer->c_slot]);
1153 	}
1154 
1155 	if (ata_c->flags & AT_READREG) {
1156 		bus_space_read_region_stream_4(sc->sc_prt, sc->sc_prh,
1157 		    PRSX(chp->ch_channel, xfer->c_slot, PRSO_FIS),
1158 		    fis, __arraycount(fis));
1159 		satafis_rdh_cmd_readreg(ata_c, (uint8_t *)fis);
1160 	}
1161 
1162 	/* correct the endianess of IDENTIFY data */
1163 	if (ata_c->r_command == WDCC_IDENTIFY ||
1164 	    ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
1165 		idwordbuf = xfer->c_databuf;
1166 		for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
1167 			idwordbuf[i] = le16toh(idwordbuf[i]);
1168 		}
1169 	}
1170 
1171 	if (PRREAD(sc, PRSX(chp->ch_channel, xfer->c_slot, PRSO_RTC)))
1172 		ata_c->flags |= AT_XFDONE;
1173 
1174 	siisata_cmd_done_end(chp, xfer);
1175 }
1176 
1177 static void
1178 siisata_cmd_done_end(struct ata_channel *chp, struct ata_xfer *xfer)
1179 {
1180 	struct ata_command *ata_c = &xfer->c_ata_c;
1181 
1182 	ata_c->flags |= AT_DONE;
1183 }
1184 
1185 static const struct ata_xfer_ops siisata_bio_xfer_ops = {
1186 	.c_start = siisata_bio_start,
1187 	.c_intr = siisata_bio_complete,
1188 	.c_poll = siisata_bio_poll,
1189 	.c_abort = siisata_bio_abort,
1190 	.c_kill_xfer = siisata_bio_kill_xfer,
1191 };
1192 
1193 int
1194 siisata_ata_bio(struct ata_drive_datas *drvp, struct ata_xfer *xfer)
1195 {
1196 	struct ata_channel *chp = drvp->chnl_softc;
1197 	struct ata_bio *ata_bio = &xfer->c_bio;
1198 
1199 	SIISATA_DEBUG_PRINT(("%s: %s.\n",
1200 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
1201 	    DEBUG_FUNCS);
1202 
1203 	if (xfer == NULL)
1204 		return ATACMD_TRY_AGAIN;
1205 	if (ata_bio->flags & ATA_POLL)
1206 		xfer->c_flags |= C_POLL;
1207 	xfer->c_drive = drvp->drive;
1208 	xfer->c_databuf = ata_bio->databuf;
1209 	xfer->c_bcount = ata_bio->bcount;
1210 	xfer->ops = &siisata_bio_xfer_ops;
1211 	ata_exec_xfer(chp, xfer);
1212 	return (ata_bio->flags & ATA_ITSDONE) ?
1213 	    ATACMD_COMPLETE : ATACMD_QUEUED;
1214 }
1215 
1216 int
1217 siisata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
1218 {
1219 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1220 	struct siisata_prb *prb;
1221 	struct ata_bio *ata_bio = &xfer->c_bio;
1222 
1223 	SIISATA_DEBUG_PRINT(("%s: %s port %d slot %d drive %d\n",
1224 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__,
1225 	    chp->ch_channel, xfer->c_slot, xfer->c_drive), DEBUG_FUNCS);
1226 
1227 	ata_channel_lock_owned(chp);
1228 
1229 	prb = schp->sch_prb[xfer->c_slot];
1230 	memset(prb, 0, SIISATA_CMD_SIZE);
1231 
1232 	satafis_rhd_construct_bio(xfer, prb->prb_fis);
1233 	KASSERT(xfer->c_drive <= PMP_PORT_CTL);
1234 	prb->prb_fis[rhd_c] |= xfer->c_drive;
1235 
1236 	if (siisata_dma_setup(chp, xfer->c_slot, ata_bio->databuf, ata_bio->bcount,
1237 	    (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
1238 		ata_bio->error = ERR_DMA;
1239 		ata_bio->r_error = 0;
1240 		return ATASTART_ABORT;
1241 	}
1242 
1243 	if (xfer->c_flags & C_POLL) {
1244 		/* polled command, disable interrupts */
1245 		prb->prb_control |= htole16(PRB_CF_INTERRUPT_MASK);
1246 		siisata_disable_port_interrupt(chp);
1247 	}
1248 
1249 	siisata_activate_prb(schp, xfer->c_slot);
1250 
1251 	if ((ata_bio->flags & ATA_POLL) == 0) {
1252 		callout_reset(&chp->c_timo_callout, mstohz(ATA_DELAY),
1253 		    ata_timeout, chp);
1254 		return ATASTART_STARTED;
1255 	} else
1256 		return ATASTART_POLL;
1257 }
1258 
1259 void
1260 siisata_bio_poll(struct ata_channel *chp, struct ata_xfer *xfer)
1261 {
1262 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1263 
1264 	/*
1265 	 * polled command
1266 	 */
1267 	for (int i = 0; i < ATA_DELAY * 10; i++) {
1268 		if (xfer->c_bio.flags & ATA_ITSDONE)
1269 			break;
1270 		siisata_intr_port(schp);
1271 		DELAY(100);
1272 	}
1273 
1274 	if ((xfer->c_bio.flags & ATA_ITSDONE) == 0) {
1275 		ata_timeout(xfer);
1276 	}
1277 
1278 	siisata_enable_port_interrupt(chp);
1279 
1280 	SIISATA_DEBUG_PRINT(("%s: %s: done\n",
1281 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
1282 	    DEBUG_FUNCS);
1283 }
1284 
1285 void
1286 siisata_bio_abort(struct ata_channel *chp, struct ata_xfer *xfer)
1287 {
1288 	siisata_cmd_complete(chp, xfer, 0);
1289 }
1290 
1291 void
1292 siisata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1293     int reason)
1294 {
1295 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1296 	struct ata_bio *ata_bio = &xfer->c_bio;
1297 	int drive = xfer->c_drive;
1298 	bool deactivate = true;
1299 
1300 	SIISATA_DEBUG_PRINT(("%s: %s: port %d slot %d\n",
1301 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__,
1302 	    chp->ch_channel, xfer->c_slot), DEBUG_FUNCS);
1303 
1304 	ata_bio->flags |= ATA_ITSDONE;
1305 	switch (reason) {
1306 	case KILL_GONE_INACTIVE:
1307 		deactivate = false;
1308 		/* FALLTHROUGH */
1309 	case KILL_GONE:
1310 		ata_bio->error = ERR_NODEV;
1311 		break;
1312 	case KILL_RESET:
1313 		ata_bio->error = ERR_RESET;
1314 		break;
1315 	case KILL_REQUEUE:
1316 		ata_bio->error = REQUEUE;
1317 		break;
1318 	default:
1319 		panic("%s: port %d: unknown reason %d",
1320 		   __func__, chp->ch_channel, reason);
1321 	}
1322 	ata_bio->r_error = WDCE_ABRT;
1323 
1324 	if (deactivate) {
1325 		siisata_deactivate_prb(schp, xfer->c_slot);
1326 		ata_deactivate_xfer(chp, xfer);
1327 	}
1328 
1329 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
1330 }
1331 
1332 int
1333 siisata_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int tfd)
1334 {
1335 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1336 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1337 	struct ata_bio *ata_bio = &xfer->c_bio;
1338 	int drive = xfer->c_drive;
1339 
1340 	SIISATA_DEBUG_PRINT(("%s: %s: port %d slot %d drive %d tfd %x\n",
1341 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__,
1342 	    chp->ch_channel, xfer->c_slot, xfer->c_drive, tfd), DEBUG_FUNCS);
1343 
1344 	if (ata_waitdrain_xfer_check(chp, xfer))
1345 		return 0;
1346 
1347 	if (xfer->c_flags & C_TIMEOU) {
1348 		ata_bio->error = TIMEOUT;
1349 	}
1350 
1351 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[xfer->c_slot], 0,
1352 	    schp->sch_datad[xfer->c_slot]->dm_mapsize,
1353 	    (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1354 	    BUS_DMASYNC_POSTWRITE);
1355 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[xfer->c_slot]);
1356 
1357 	ata_bio->flags |= ATA_ITSDONE;
1358 	if (ATACH_ST(tfd) & WDCS_DWF) {
1359 		ata_bio->error = ERR_DF;
1360 	} else if (ATACH_ST(tfd) & WDCS_ERR) {
1361 		ata_bio->error = ERROR;
1362 		ata_bio->r_error = ATACH_ERR(tfd);
1363 	} else if (ATACH_ST(tfd) & WDCS_CORR)
1364 		ata_bio->flags |= ATA_CORR;
1365 
1366 	SIISATA_DEBUG_PRINT(("%s: %s bcount: %ld", SIISATANAME(sc), __func__,
1367 	    ata_bio->bcount), DEBUG_XFERS);
1368 	if (ata_bio->error == NOERROR) {
1369 		if ((xfer->c_flags & C_NCQ) != 0 && ata_bio->flags & ATA_READ)
1370 			ata_bio->bcount -=
1371 			    PRREAD(sc, PRSX(chp->ch_channel, xfer->c_slot, PRSO_RTC));
1372 		else
1373 			ata_bio->bcount = 0;
1374 	}
1375 	SIISATA_DEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1376 
1377 	siisata_deactivate_prb(schp, xfer->c_slot);
1378 	ata_deactivate_xfer(chp, xfer);
1379 
1380 	(*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc, xfer);
1381 	if ((ATACH_ST(tfd) & WDCS_ERR) == 0)
1382 		atastart(chp);
1383 	return 0;
1384 }
1385 
1386 static int
1387 siisata_dma_setup(struct ata_channel *chp, int slot, void *data,
1388     size_t count, int op)
1389 {
1390 
1391 	int error, seg;
1392 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1393 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1394 
1395 	struct siisata_prb *prbp;
1396 
1397 	prbp = schp->sch_prb[slot];
1398 
1399 	if (data == NULL) {
1400 		goto end;
1401 	}
1402 
1403 	error = bus_dmamap_load(sc->sc_dmat, schp->sch_datad[slot],
1404 	    data, count, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1405 	if (error) {
1406 		aprint_error("%s port %d: "
1407 		    "failed to load xfer in slot %d: error %d\n",
1408 		    SIISATANAME(sc), chp->ch_channel, slot, error);
1409 		return error;
1410 	}
1411 
1412 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1413 	    schp->sch_datad[slot]->dm_mapsize,
1414 	    (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1415 
1416 	SIISATA_DEBUG_PRINT(("%s: %d segs, %ld count\n", __func__,
1417 	    schp->sch_datad[slot]->dm_nsegs, (long unsigned int) count),
1418 	    DEBUG_FUNCS | DEBUG_DEBUG);
1419 
1420 	for (seg = 0; seg < schp->sch_datad[slot]->dm_nsegs; seg++) {
1421 		prbp->prb_sge[seg].sge_da =
1422 		    htole64(schp->sch_datad[slot]->dm_segs[seg].ds_addr);
1423 		prbp->prb_sge[seg].sge_dc =
1424 		    htole32(schp->sch_datad[slot]->dm_segs[seg].ds_len);
1425 		prbp->prb_sge[seg].sge_flags = htole32(0);
1426 	}
1427 	prbp->prb_sge[seg - 1].sge_flags |= htole32(SGE_FLAG_TRM);
1428 end:
1429 	return 0;
1430 }
1431 
1432 static void
1433 siisata_activate_prb(struct siisata_channel *schp, int slot)
1434 {
1435 	struct siisata_softc *sc;
1436 	bus_size_t offset;
1437 	uint64_t pprb;
1438 
1439 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1440 
1441 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_PREWRITE);
1442 
1443 	offset = PRO_CARX(schp->ata_channel.ch_channel, slot);
1444 
1445 	pprb = schp->sch_bus_prb[slot];
1446 
1447 	PRWRITE(sc, offset + 0, pprb >>  0);
1448 	PRWRITE(sc, offset + 4, pprb >> 32);
1449 }
1450 
1451 static void
1452 siisata_deactivate_prb(struct siisata_channel *schp, int slot)
1453 {
1454 	struct siisata_softc *sc;
1455 
1456 	sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1457 
1458 	SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_POSTWRITE);
1459 }
1460 
1461 static int
1462 siisata_reinit_port(struct ata_channel *chp, int drive)
1463 {
1464 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1465 	int ps;
1466 	int error = 0;
1467 
1468 	if (chp->ch_ndrives > 1) {
1469 		/*
1470 		 * Proper recovery would SET this bit, which makes it
1471 		 * not possible to submit new commands and resume execution
1472 		 * on non-errored drives, then wait for those commands,
1473 		 * to finish, and only then clear the bit and reset the state.
1474 		 * For now this is okay, since we never queue commands for
1475 		 * more than one drive.
1476 		 * XXX FIS-based switching
1477 		 */
1478 		PRWRITE(sc, PRX(chp->ch_channel, PRO_PCC), PR_PC_RESUME);
1479 
1480 	        for (int i = 0; i < chp->ch_ndrives; i++) {
1481 			if (drive >= 0 && i != drive)
1482 				continue;
1483 
1484 			PRWRITE(sc, PRX(chp->ch_channel, PRO_PMPSTS(i)), 0);
1485 			PRWRITE(sc, PRX(chp->ch_channel, PRO_PMPQACT(i)), 0);
1486 		}
1487 	}
1488 
1489 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_PORT_INITIALIZE);
1490 	for (int i = 0; i < ATA_DELAY * 100; i++) {
1491 		ps = PRREAD(sc, PRX(chp->ch_channel, PRO_PS));
1492 		if ((ps & PR_PS_PORT_READY) != 0)
1493 			break;
1494 
1495 		DELAY(10);
1496 	}
1497 	if ((ps & PR_PS_PORT_READY) == 0) {
1498 		printf("%s: timeout waiting for port to be ready\n", __func__);
1499 		error = EBUSY;
1500 	}
1501 
1502 	if (chp->ch_ndrives > 1)
1503 		PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_PMP_ENABLE);
1504 
1505 	return error;
1506 }
1507 
1508 static void
1509 siisata_device_reset(struct ata_channel *chp)
1510 {
1511 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1512 	int ps;
1513 
1514 	ata_channel_lock_owned(chp);
1515 
1516 	/*
1517 	 * This is always called after siisata_reinit_port(), so don't
1518 	 * need to deal with RESUME and clearing device error state.
1519 	 */
1520 
1521 	PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_DEVICE_RESET);
1522 
1523 	for (int i = 0; i < ATA_DELAY * 100; i++) {
1524 		ps = PRREAD(sc, PRX(chp->ch_channel, PRO_PS));
1525 		if ((ps & PR_PS_PORT_READY) != 0)
1526 			break;
1527 
1528 		DELAY(10);
1529 	}
1530 	if ((ps & PR_PS_PORT_READY) == 0) {
1531 		printf("%s: timeout waiting for port to be ready\n", __func__);
1532 		siisata_reset_channel(chp, AT_POLL);
1533 	}
1534 
1535 	ata_kill_active(chp, KILL_RESET, 0);
1536 }
1537 
1538 
1539 #if NATAPIBUS > 0
1540 void
1541 siisata_atapibus_attach(struct atabus_softc *ata_sc)
1542 {
1543 	struct ata_channel *chp = ata_sc->sc_chan;
1544 	struct atac_softc *atac = chp->ch_atac;
1545 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1546 	struct scsipi_channel *chan = &chp->ch_atapi_channel;
1547 
1548 	/*
1549 	 * Fill in the scsipi_adapter.
1550 	 */
1551 	adapt->adapt_dev = atac->atac_dev;
1552 	adapt->adapt_nchannels = atac->atac_nchannels;
1553 	adapt->adapt_request = siisata_atapi_scsipi_request;
1554 	adapt->adapt_minphys = siisata_atapi_minphys;
1555 	atac->atac_atapi_adapter.atapi_probe_device =
1556 	    siisata_atapi_probe_device;
1557 
1558 	/*
1559 	 * Fill in the scsipi_channel.
1560 	 */
1561 	memset(chan, 0, sizeof(*chan));
1562 	chan->chan_adapter = adapt;
1563 	chan->chan_bustype = &siisata_atapi_bustype;
1564 	chan->chan_channel = chp->ch_channel;
1565 	chan->chan_flags = SCSIPI_CHAN_OPENINGS;
1566 	chan->chan_openings = 1;
1567 	chan->chan_max_periph = 1;
1568 	chan->chan_ntargets = 1;
1569 	chan->chan_nluns = 1;
1570 
1571 	chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
1572 	    atapiprint);
1573 }
1574 
1575 void
1576 siisata_atapi_minphys(struct buf *bp)
1577 {
1578 	if (bp->b_bcount > MAXPHYS)
1579 		bp->b_bcount = MAXPHYS;
1580 	minphys(bp);
1581 }
1582 
1583 /*
1584  * Kill off all pending xfers for a periph.
1585  *
1586  * Must be called at splbio().
1587  */
1588 void
1589 siisata_atapi_kill_pending(struct scsipi_periph *periph)
1590 {
1591 	struct atac_softc *atac =
1592 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
1593 	struct ata_channel *chp =
1594 	    atac->atac_channels[periph->periph_channel->chan_channel];
1595 
1596 	ata_kill_pending(&chp->ch_drive[periph->periph_target]);
1597 }
1598 
1599 void
1600 siisata_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1601     int reason)
1602 {
1603 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1604 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1605 	bool deactivate = true;
1606 
1607 	/* remove this command from xfer queue */
1608 	switch (reason) {
1609 	case KILL_GONE_INACTIVE:
1610 		deactivate = false;
1611 		/* FALLTHROUGH */
1612 	case KILL_GONE:
1613 		sc_xfer->error = XS_DRIVER_STUFFUP;
1614 		break;
1615 	case KILL_RESET:
1616 		sc_xfer->error = XS_RESET;
1617 		break;
1618 	case KILL_REQUEUE:
1619 		sc_xfer->error = XS_REQUEUE;
1620 		break;
1621 	default:
1622 		panic("%s: port %d: unknown reason %d",
1623 		   __func__, chp->ch_channel, reason);
1624 	}
1625 
1626 	if (deactivate) {
1627 		siisata_deactivate_prb(schp, xfer->c_slot);
1628 		ata_deactivate_xfer(chp, xfer);
1629 	}
1630 
1631 	ata_free_xfer(chp, xfer);
1632 	scsipi_done(sc_xfer);
1633 }
1634 
1635 void
1636 siisata_atapi_probe_device(struct atapibus_softc *sc, int target)
1637 {
1638 	struct scsipi_channel *chan = sc->sc_channel;
1639 	struct scsipi_periph *periph;
1640 	struct ataparams ids;
1641 	struct ataparams *id = &ids;
1642 	struct siisata_softc *siic =
1643 	    device_private(chan->chan_adapter->adapt_dev);
1644 	struct atac_softc *atac = &siic->sc_atac;
1645 	struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
1646 	struct ata_drive_datas *drvp = &chp->ch_drive[target];
1647 	struct scsipibus_attach_args sa;
1648 	char serial_number[21], model[41], firmware_revision[9];
1649 	int s;
1650 
1651 	/* skip if already attached */
1652 	if (scsipi_lookup_periph(chan, target, 0) != NULL)
1653 		return;
1654 
1655 	/* if no ATAPI device detected at attach time, skip */
1656 	if (drvp->drive_type != ATA_DRIVET_ATAPI) {
1657 		SIISATA_DEBUG_PRINT(("%s: drive %d not present\n", __func__,
1658 		    target), DEBUG_PROBE);
1659 		return;
1660 	}
1661 
1662 	/* Some ATAPI devices need a bit more time after software reset. */
1663 	DELAY(5000);
1664 	if (ata_get_params(drvp, AT_WAIT, id) == 0) {
1665 #ifdef ATAPI_DEBUG_PROBE
1666 		log(LOG_DEBUG, "%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
1667 		    device_xname(sc->sc_dev), target,
1668 		    id->atap_config & ATAPI_CFG_CMD_MASK,
1669 		    id->atap_config & ATAPI_CFG_DRQ_MASK);
1670 #endif
1671 		periph = scsipi_alloc_periph(M_NOWAIT);
1672 		if (periph == NULL) {
1673 			aprint_error_dev(sc->sc_dev,
1674 			    "%s: unable to allocate periph for "
1675 			    "port %d drive %d\n", __func__,
1676 			    chp->ch_channel, target);
1677 			return;
1678 		}
1679 		periph->periph_dev = NULL;
1680 		periph->periph_channel = chan;
1681 		periph->periph_switch = &atapi_probe_periphsw;
1682 		periph->periph_target = target;
1683 		periph->periph_lun = 0;
1684 		periph->periph_quirks = PQUIRK_ONLYBIG;
1685 
1686 #ifdef SCSIPI_DEBUG
1687 		if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
1688 		    SCSIPI_DEBUG_TARGET == target)
1689 			periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
1690 #endif
1691 		periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
1692 		if (id->atap_config & ATAPI_CFG_REMOV)
1693 			periph->periph_flags |= PERIPH_REMOVABLE;
1694 		sa.sa_periph = periph;
1695 		sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
1696 		sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
1697 		    T_REMOV : T_FIXED;
1698 		strnvisx(model, sizeof(model), id->atap_model, 40,
1699 		    VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1700 		strnvisx(serial_number, sizeof(serial_number),
1701 		    id->atap_serial, 20, VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1702 		strnvisx(firmware_revision, sizeof(firmware_revision),
1703 		    id->atap_revision, 8, VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1704 		sa.sa_inqbuf.vendor = model;
1705 		sa.sa_inqbuf.product = serial_number;
1706 		sa.sa_inqbuf.revision = firmware_revision;
1707 
1708 		/*
1709 		 * Determine the operating mode capabilities of the device.
1710 		 */
1711 		if ((id->atap_config & ATAPI_CFG_CMD_MASK)
1712 		    == ATAPI_CFG_CMD_16) {
1713 			periph->periph_cap |= PERIPH_CAP_CMD16;
1714 
1715 			/* configure port for packet length */
1716 			PRWRITE(siic, PRX(chp->ch_channel, PRO_PCS),
1717 			    PR_PC_PACKET_LENGTH);
1718 		} else {
1719 			PRWRITE(siic, PRX(chp->ch_channel, PRO_PCC),
1720 			    PR_PC_PACKET_LENGTH);
1721 		}
1722 
1723 		/* XXX This is gross. */
1724 		periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
1725 
1726 		drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
1727 
1728 		if (drvp->drv_softc)
1729 			ata_probe_caps(drvp);
1730 		else {
1731 			s = splbio();
1732 			drvp->drive_type &= ATA_DRIVET_NONE;
1733 			splx(s);
1734 		}
1735 	} else {
1736 		s = splbio();
1737 		drvp->drive_type &= ATA_DRIVET_NONE;
1738 		splx(s);
1739 	}
1740 }
1741 
1742 static const struct ata_xfer_ops siisata_atapi_xfer_ops = {
1743 	.c_start = siisata_atapi_start,
1744 	.c_intr = siisata_atapi_complete,
1745 	.c_poll = siisata_atapi_poll,
1746 	.c_abort = siisata_atapi_abort,
1747 	.c_kill_xfer = siisata_atapi_kill_xfer,
1748 };
1749 
1750 void
1751 siisata_atapi_scsipi_request(struct scsipi_channel *chan,
1752     scsipi_adapter_req_t req, void *arg)
1753 {
1754 	struct scsipi_adapter *adapt = chan->chan_adapter;
1755 	struct scsipi_periph *periph;
1756 	struct scsipi_xfer *sc_xfer;
1757 	struct siisata_softc *sc = device_private(adapt->adapt_dev);
1758 	struct atac_softc *atac = &sc->sc_atac;
1759 	struct ata_xfer *xfer;
1760 	int channel = chan->chan_channel;
1761 	int drive, s;
1762 
1763 	switch (req) {
1764 	case ADAPTER_REQ_RUN_XFER:
1765 		sc_xfer = arg;
1766 		periph = sc_xfer->xs_periph;
1767 		drive = periph->periph_target;
1768 
1769 		SIISATA_DEBUG_PRINT(("%s: %s:%d:%d\n", __func__,
1770 		    device_xname(atac->atac_dev), channel, drive),
1771 		    DEBUG_XFERS);
1772 
1773 		if (!device_is_active(atac->atac_dev)) {
1774 			sc_xfer->error = XS_DRIVER_STUFFUP;
1775 			scsipi_done(sc_xfer);
1776 			return;
1777 		}
1778 		xfer = ata_get_xfer(atac->atac_channels[channel], false);
1779 		if (xfer == NULL) {
1780 			sc_xfer->error = XS_RESOURCE_SHORTAGE;
1781 			scsipi_done(sc_xfer);
1782 			return;
1783 		}
1784 
1785 		if (sc_xfer->xs_control & XS_CTL_POLL)
1786 			xfer->c_flags |= C_POLL;
1787 		xfer->c_drive = drive;
1788 		xfer->c_flags |= C_ATAPI;
1789 		xfer->c_databuf = sc_xfer->data;
1790 		xfer->c_bcount = sc_xfer->datalen;
1791 		xfer->ops = &siisata_atapi_xfer_ops;
1792 		xfer->c_scsipi = sc_xfer;
1793 		xfer->c_atapi.c_dscpoll = 0;
1794 		s = splbio();
1795 		ata_exec_xfer(atac->atac_channels[channel], xfer);
1796 #ifdef DIAGNOSTIC
1797 		if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
1798 		    (sc_xfer->xs_status & XS_STS_DONE) == 0)
1799 			panic("%s: polled command not done", __func__);
1800 #endif
1801 		splx(s);
1802 		return;
1803 
1804 	default:
1805 		/* Not supported, nothing to do. */
1806 		;
1807 	}
1808 }
1809 
1810 int
1811 siisata_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
1812 {
1813 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1814 	struct siisata_prb *prbp;
1815 
1816 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1817 
1818 	SIISATA_DEBUG_PRINT( ("%s: %s:%d:%d, scsi flags 0x%x\n", __func__,
1819 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), chp->ch_channel,
1820 	    chp->ch_drive[xfer->c_drive].drive, sc_xfer->xs_control),
1821 	    DEBUG_XFERS);
1822 
1823 	ata_channel_lock_owned(chp);
1824 
1825 	prbp = schp->sch_prb[xfer->c_slot];
1826 	memset(prbp, 0, SIISATA_CMD_SIZE);
1827 
1828 	/* fill in direction for ATAPI command */
1829 	if ((sc_xfer->xs_control & XS_CTL_DATA_IN))
1830 		prbp->prb_control |= htole16(PRB_CF_PACKET_READ);
1831 	if ((sc_xfer->xs_control & XS_CTL_DATA_OUT))
1832 		prbp->prb_control |= htole16(PRB_CF_PACKET_WRITE);
1833 
1834 	satafis_rhd_construct_atapi(xfer, prbp->prb_fis);
1835 	KASSERT(xfer->c_drive <= PMP_PORT_CTL);
1836 	prbp->prb_fis[rhd_c] |= xfer->c_drive;
1837 
1838 	/* copy over ATAPI command */
1839 	memcpy(prbp->prb_atapi, sc_xfer->cmd, sc_xfer->cmdlen);
1840 
1841 	if (siisata_dma_setup(chp, xfer->c_slot,
1842 		(sc_xfer->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ?
1843 		xfer->c_databuf : NULL,
1844 		xfer->c_bcount,
1845 		(sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1846 		BUS_DMA_READ : BUS_DMA_WRITE)
1847 	) {
1848 		sc_xfer->error = XS_DRIVER_STUFFUP;
1849 		return ATASTART_ABORT;
1850 	}
1851 
1852 	if (xfer->c_flags & C_POLL) {
1853 		/* polled command, disable interrupts */
1854 		prbp->prb_control |= htole16(PRB_CF_INTERRUPT_MASK);
1855 		siisata_disable_port_interrupt(chp);
1856 	}
1857 
1858 	siisata_activate_prb(schp, xfer->c_slot);
1859 
1860 	if ((xfer->c_flags & C_POLL) == 0) {
1861 		callout_reset(&chp->c_timo_callout, mstohz(sc_xfer->timeout),
1862 		    ata_timeout, chp);
1863 		return ATASTART_STARTED;
1864 	} else
1865 		return ATASTART_POLL;
1866 }
1867 
1868 void
1869 siisata_atapi_poll(struct ata_channel *chp, struct ata_xfer *xfer)
1870 {
1871 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1872 
1873 	/*
1874 	 * polled command
1875 	 */
1876 	for (int i = 0; i < ATA_DELAY * 10; i++) {
1877 		if (xfer->c_scsipi->xs_status & XS_STS_DONE)
1878 			break;
1879 		siisata_intr_port(schp);
1880 		DELAY(100);
1881 	}
1882 	if ((xfer->c_scsipi->xs_status & XS_STS_DONE) == 0) {
1883 		ata_timeout(xfer);
1884 	}
1885 	/* reenable interrupts */
1886 	siisata_enable_port_interrupt(chp);
1887 
1888 	SIISATA_DEBUG_PRINT(("%s: %s: done\n",
1889 	    SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
1890             DEBUG_FUNCS);
1891 }
1892 
1893 void
1894 siisata_atapi_abort(struct ata_channel *chp, struct ata_xfer *xfer)
1895 {
1896 	siisata_atapi_complete(chp, xfer, 0);
1897 }
1898 
1899 int
1900 siisata_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer,
1901     int tfd)
1902 {
1903 	struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1904 	struct siisata_channel *schp = (struct siisata_channel *)chp;
1905 	struct scsipi_xfer *sc_xfer = xfer->c_scsipi;
1906 
1907 	SIISATA_DEBUG_PRINT(("%s: %s()\n", SIISATANAME(sc), __func__),
1908 	    DEBUG_INTR);
1909 
1910 	if (ata_waitdrain_xfer_check(chp, xfer))
1911 		return 0;
1912 
1913 	if (xfer->c_flags & C_TIMEOU) {
1914 		sc_xfer->error = XS_TIMEOUT;
1915 	}
1916 
1917 	bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[xfer->c_slot], 0,
1918 	    schp->sch_datad[xfer->c_slot]->dm_mapsize,
1919 	    (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1920 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1921 	bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[xfer->c_slot]);
1922 
1923 	sc_xfer->resid = sc_xfer->datalen;
1924 	sc_xfer->resid -= PRREAD(sc, PRSX(chp->ch_channel, xfer->c_slot,
1925 	    PRSO_RTC));
1926 	SIISATA_DEBUG_PRINT(("%s: %s datalen %d resid %d\n", SIISATANAME(sc),
1927 	    __func__, sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
1928 	if ((ATACH_ST(tfd) & WDCS_ERR) &&
1929 	    ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1930 	    sc_xfer->resid == sc_xfer->datalen)) {
1931 		sc_xfer->error = XS_SHORTSENSE;
1932 		sc_xfer->sense.atapi_sense = ATACH_ERR(tfd);
1933 		if ((sc_xfer->xs_periph->periph_quirks &
1934 		    PQUIRK_NOSENSE) == 0) {
1935 			/* request sense */
1936 			sc_xfer->error = XS_BUSY;
1937 			sc_xfer->status = SCSI_CHECK;
1938 		}
1939 	}
1940 
1941 	siisata_deactivate_prb(schp, xfer->c_slot);
1942 	ata_deactivate_xfer(chp, xfer);
1943 
1944 	ata_free_xfer(chp, xfer);
1945 	scsipi_done(sc_xfer);
1946 	if ((ATACH_ST(tfd) & WDCS_ERR) == 0)
1947 		atastart(chp);
1948 	return 0;
1949 }
1950 
1951 #endif /* NATAPIBUS */
1952