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