Home | History | Annotate | Line # | Download | only in ata
satafis_subr.c revision 1.7.28.1
      1  1.7.28.1  jdolecek /* $NetBSD: satafis_subr.c,v 1.7.28.1 2017/04/15 12:01:23 jdolecek Exp $ */
      2       1.1  jakllsch 
      3       1.1  jakllsch /*-
      4       1.1  jakllsch  * Copyright (c) 2009 Jonathan A. Kollasch.
      5       1.1  jakllsch  * All rights reserved.
      6       1.1  jakllsch  *
      7       1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      8       1.1  jakllsch  * modification, are permitted provided that the following conditions
      9       1.1  jakllsch  * are met:
     10       1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     11       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     12       1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     15       1.1  jakllsch  *
     16       1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1  jakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1  jakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1  jakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1  jakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1  jakllsch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1  jakllsch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1  jakllsch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1  jakllsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1  jakllsch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1  jakllsch  */
     27       1.1  jakllsch 
     28       1.1  jakllsch /*
     29       1.1  jakllsch  * Copyright (c) 2006 Manuel Bouyer.
     30       1.1  jakllsch  *
     31       1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
     32       1.1  jakllsch  * modification, are permitted provided that the following conditions
     33       1.1  jakllsch  * are met:
     34       1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     35       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     36       1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     37       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     38       1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     39       1.1  jakllsch  *
     40       1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     41       1.1  jakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     42       1.1  jakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     43       1.1  jakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     44       1.1  jakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     45       1.1  jakllsch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     46       1.1  jakllsch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     47       1.1  jakllsch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     48       1.1  jakllsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     49       1.1  jakllsch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     50       1.1  jakllsch  *
     51       1.1  jakllsch  */
     52       1.1  jakllsch 
     53       1.1  jakllsch #include <sys/cdefs.h>
     54  1.7.28.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: satafis_subr.c,v 1.7.28.1 2017/04/15 12:01:23 jdolecek Exp $");
     55       1.1  jakllsch 
     56       1.1  jakllsch #include <sys/param.h>
     57       1.2  jakllsch #include <sys/systm.h>
     58       1.1  jakllsch 
     59       1.1  jakllsch #include <sys/disklabel.h>
     60       1.1  jakllsch 
     61       1.1  jakllsch #include <dev/ata/atareg.h>
     62       1.1  jakllsch #include <dev/ata/atavar.h>
     63       1.1  jakllsch 
     64       1.1  jakllsch #include <dev/ata/satafisreg.h>
     65       1.1  jakllsch #include <dev/ata/satafisvar.h>
     66       1.1  jakllsch 
     67       1.1  jakllsch #include "atapibus.h"
     68       1.1  jakllsch 
     69       1.1  jakllsch void
     70       1.1  jakllsch satafis_rhd_construct_cmd(struct ata_command *ata_c, uint8_t *fis)
     71       1.1  jakllsch {
     72       1.6  jakllsch 
     73       1.2  jakllsch 	memset(fis, 0, RHD_FISLEN);
     74       1.2  jakllsch 
     75       1.1  jakllsch 	fis[fis_type] = RHD_FISTYPE;
     76       1.5  jakllsch 	fis[rhd_c] = RHD_C;
     77       1.1  jakllsch 	fis[rhd_command] = ata_c->r_command;
     78       1.6  jakllsch 	fis[rhd_features0] = (ata_c->r_features >> 0) & 0xff;
     79       1.6  jakllsch 
     80       1.6  jakllsch 	fis[rhd_lba0] = (ata_c->r_lba >> 0) & 0xff;
     81       1.6  jakllsch 	fis[rhd_lba1] = (ata_c->r_lba >> 8) & 0xff;
     82       1.6  jakllsch 	fis[rhd_lba2] = (ata_c->r_lba >> 16) & 0xff;
     83       1.6  jakllsch 	if ((ata_c->flags & AT_LBA48) != 0) {
     84       1.7  jakllsch 		fis[rhd_dh] = ata_c->r_device;
     85       1.6  jakllsch 		fis[rhd_lba3] = (ata_c->r_lba >> 24) & 0xff;
     86       1.6  jakllsch 		fis[rhd_lba4] = (ata_c->r_lba >> 32) & 0xff;
     87       1.6  jakllsch 		fis[rhd_lba5] = (ata_c->r_lba >> 40) & 0xff;
     88       1.6  jakllsch 		fis[rhd_features1] = (ata_c->r_features >> 8) & 0xff;
     89       1.6  jakllsch 	} else {
     90       1.7  jakllsch 		fis[rhd_dh] = (ata_c->r_device & 0xf0) |
     91       1.7  jakllsch 		    ((ata_c->r_lba >> 24) & 0x0f);
     92       1.6  jakllsch 	}
     93       1.6  jakllsch 
     94       1.6  jakllsch 	fis[rhd_count0] = (ata_c->r_count >> 0) & 0xff;
     95       1.6  jakllsch 	if ((ata_c->flags & AT_LBA48) != 0) {
     96       1.6  jakllsch 		fis[rhd_count1] = (ata_c->r_count >> 8) & 0xff;
     97       1.6  jakllsch 	}
     98       1.1  jakllsch }
     99       1.1  jakllsch 
    100       1.1  jakllsch void
    101       1.1  jakllsch satafis_rhd_construct_bio(struct ata_xfer *xfer, uint8_t *fis)
    102       1.1  jakllsch {
    103  1.7.28.1  jdolecek 	struct ata_bio *ata_bio = &xfer->c_bio;
    104  1.7.28.1  jdolecek 	struct ata_drive_datas *drvp = &xfer->c_chp->ch_drive[xfer->c_drive];
    105       1.1  jakllsch 	int nblks;
    106       1.1  jakllsch 
    107  1.7.28.1  jdolecek 	nblks = xfer->c_bcount / drvp->lp->d_secsize;
    108       1.1  jakllsch 
    109       1.2  jakllsch 	memset(fis, 0, RHD_FISLEN);
    110       1.2  jakllsch 
    111       1.1  jakllsch 	fis[fis_type] = RHD_FISTYPE;
    112       1.5  jakllsch 	fis[rhd_c] = RHD_C;
    113       1.1  jakllsch 	if (ata_bio->flags & ATA_LBA48) {
    114       1.1  jakllsch 		fis[rhd_command] = (ata_bio->flags & ATA_READ) ?
    115       1.1  jakllsch 		    WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
    116       1.1  jakllsch 	} else {
    117       1.1  jakllsch 		fis[rhd_command] =
    118       1.1  jakllsch 		    (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA;
    119       1.1  jakllsch 	}
    120       1.6  jakllsch 
    121       1.6  jakllsch 	fis[rhd_lba0] = (ata_bio->blkno >> 0) & 0xff;
    122       1.6  jakllsch 	fis[rhd_lba1] = (ata_bio->blkno >> 8) & 0xff;
    123       1.6  jakllsch 	fis[rhd_lba2] = (ata_bio->blkno >> 16) & 0xff;
    124       1.6  jakllsch 	if ((ata_bio->flags & ATA_LBA48) != 0) {
    125       1.1  jakllsch 		fis[rhd_dh] = WDSD_LBA;
    126       1.6  jakllsch 		fis[rhd_lba3] = (ata_bio->blkno >> 24) & 0xff;
    127       1.6  jakllsch 		fis[rhd_lba4] = (ata_bio->blkno >> 32) & 0xff;
    128       1.6  jakllsch 		fis[rhd_lba5] = (ata_bio->blkno >> 40) & 0xff;
    129       1.1  jakllsch 	} else {
    130       1.6  jakllsch 		fis[rhd_dh] = ((ata_bio->blkno >> 24) & 0x0f) |
    131       1.6  jakllsch 		    (((ata_bio->flags & ATA_LBA) != 0) ? WDSD_LBA : 0);
    132       1.6  jakllsch 	}
    133       1.6  jakllsch 
    134       1.6  jakllsch 	fis[rhd_count0] = nblks & 0xff;
    135       1.6  jakllsch 	if ((ata_bio->flags & ATA_LBA48) != 0) {
    136       1.6  jakllsch 		fis[rhd_count1] = (nblks >> 8) & 0xff;
    137       1.1  jakllsch 	}
    138       1.1  jakllsch }
    139       1.1  jakllsch 
    140       1.1  jakllsch #if NATAPIBUS > 0
    141       1.1  jakllsch void
    142       1.1  jakllsch satafis_rhd_construct_atapi(struct ata_xfer *xfer, uint8_t *fis)
    143       1.1  jakllsch {
    144       1.6  jakllsch 
    145       1.2  jakllsch 	memset(fis, 0, RHD_FISLEN);
    146       1.2  jakllsch 
    147       1.1  jakllsch 	fis[fis_type] = RHD_FISTYPE;
    148       1.5  jakllsch 	fis[rhd_c] = RHD_C;
    149       1.1  jakllsch 	fis[rhd_command] = ATAPI_PKT_CMD;
    150       1.6  jakllsch 	fis[rhd_features0] = (xfer->c_flags & C_DMA) ?
    151       1.1  jakllsch 	    ATAPI_PKT_CMD_FTRE_DMA : 0;
    152       1.1  jakllsch }
    153       1.1  jakllsch #endif /* NATAPIBUS */
    154       1.1  jakllsch 
    155       1.1  jakllsch void
    156       1.5  jakllsch satafis_rdh_parse(struct ata_channel *chp, const uint8_t *fis)
    157       1.1  jakllsch {
    158       1.6  jakllsch 
    159       1.3  jakllsch 	chp->ch_status = fis[rdh_status];
    160       1.3  jakllsch 	chp->ch_error = fis[rdh_error];
    161       1.5  jakllsch }
    162       1.1  jakllsch 
    163       1.5  jakllsch void
    164       1.5  jakllsch satafis_rdh_cmd_readreg(struct ata_command *ata_c, const uint8_t *fis)
    165       1.5  jakllsch {
    166       1.6  jakllsch 
    167       1.6  jakllsch 	ata_c->r_lba = (uint64_t)fis[rdh_lba0] << 0;
    168       1.6  jakllsch 	ata_c->r_lba |= (uint64_t)fis[rdh_lba1] << 8;
    169       1.6  jakllsch 	ata_c->r_lba |= (uint64_t)fis[rdh_lba2] << 16;
    170       1.6  jakllsch 	if ((ata_c->flags & AT_LBA48) != 0) {
    171       1.6  jakllsch 		ata_c->r_lba |= (uint64_t)fis[rdh_lba3] << 24;
    172       1.6  jakllsch 		ata_c->r_lba |= (uint64_t)fis[rdh_lba4] << 32;
    173       1.6  jakllsch 		ata_c->r_lba |= (uint64_t)fis[rdh_lba5] << 40;
    174       1.7  jakllsch 		ata_c->r_device = fis[rdh_dh];
    175       1.6  jakllsch 	} else {
    176       1.6  jakllsch 		ata_c->r_lba |= (uint64_t)(fis[rdh_dh] & 0x0f) << 24;
    177       1.7  jakllsch 		ata_c->r_device = fis[rdh_dh] & 0xf0;
    178       1.6  jakllsch 	}
    179       1.6  jakllsch 
    180       1.6  jakllsch 	ata_c->r_count = fis[rdh_count0] << 0;
    181       1.6  jakllsch 	if ((ata_c->flags & AT_LBA48) != 0) {
    182       1.6  jakllsch 		ata_c->r_count |= fis[rdh_count1] << 8;
    183       1.6  jakllsch 	}
    184       1.6  jakllsch 
    185       1.5  jakllsch 	ata_c->r_error = fis[rdh_error];
    186       1.6  jakllsch 	ata_c->r_status = fis[rdh_status];
    187       1.1  jakllsch }
    188