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