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