xref: /netbsd-src/sys/dev/ata/satafis_subr.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /* $NetBSD: satafis_subr.c,v 1.5 2010/04/07 17:51:16 jakllsch Exp $ */
2 
3 /*-
4  * Copyright (c) 2009 Jonathan A. Kollasch.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * Copyright (c) 2006 Manuel Bouyer.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
41  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
44  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
49  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: satafis_subr.c,v 1.5 2010/04/07 17:51:16 jakllsch Exp $");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 
59 #include <sys/disklabel.h>
60 
61 #include <dev/ata/atareg.h>
62 #include <dev/ata/atavar.h>
63 
64 #include <dev/ata/satafisreg.h>
65 #include <dev/ata/satafisvar.h>
66 
67 #include "atapibus.h"
68 
69 void
70 satafis_rhd_construct_cmd(struct ata_command *ata_c, uint8_t *fis)
71 {
72 	memset(fis, 0, RHD_FISLEN);
73 
74 	fis[fis_type] = RHD_FISTYPE;
75 	fis[rhd_c] = RHD_C;
76 	fis[rhd_command] = ata_c->r_command;
77 	fis[rhd_features] = ata_c->r_features;
78 	fis[rhd_sector] = ata_c->r_sector;
79 	fis[rhd_cyl_lo] = ata_c->r_cyl & 0xff;
80 	fis[rhd_cyl_hi] = (ata_c->r_cyl >> 8) & 0xff;
81 	fis[rhd_dh] = ata_c->r_head & 0x0f;
82 	fis[rhd_seccnt] = ata_c->r_count;
83 }
84 
85 void
86 satafis_rhd_construct_bio(struct ata_xfer *xfer, uint8_t *fis)
87 {
88 	struct ata_bio *ata_bio = xfer->c_cmd;
89 	int nblks;
90 
91 	nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
92 
93 	memset(fis, 0, RHD_FISLEN);
94 
95 	fis[fis_type] = RHD_FISTYPE;
96 	fis[rhd_c] = RHD_C;
97 	if (ata_bio->flags & ATA_LBA48) {
98 		fis[rhd_command] = (ata_bio->flags & ATA_READ) ?
99 		    WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
100 	} else {
101 		fis[rhd_command] =
102 		    (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA;
103 	}
104 	fis[rhd_sector] = ata_bio->blkno & 0xff;
105 	fis[rhd_cyl_lo] = (ata_bio->blkno >> 8) & 0xff;
106 	fis[rhd_cyl_hi] = (ata_bio->blkno >> 16) & 0xff;
107 	if (ata_bio->flags & ATA_LBA48) {
108 		fis[rhd_dh] = WDSD_LBA;
109 		fis[rhd_sector_exp] = (ata_bio->blkno >> 24) & 0xff;
110 		fis[rhd_cyl_lo_exp] = (ata_bio->blkno >> 32) & 0xff;
111 		fis[rhd_cyl_hi_exp] = (ata_bio->blkno >> 40) & 0xff;
112 	} else {
113 		fis[rhd_dh] = ((ata_bio->blkno >> 24) & 0x0f) | WDSD_LBA;
114 	}
115 	fis[rhd_seccnt] = nblks & 0xff;
116 	fis[rhd_seccnt_exp] = (ata_bio->flags & ATA_LBA48) ?
117 	    ((nblks >> 8) & 0xff) : 0;
118 }
119 
120 #if NATAPIBUS > 0
121 void
122 satafis_rhd_construct_atapi(struct ata_xfer *xfer, uint8_t *fis)
123 {
124 	memset(fis, 0, RHD_FISLEN);
125 
126 	fis[fis_type] = RHD_FISTYPE;
127 	fis[rhd_c] = RHD_C;
128 	fis[rhd_command] = ATAPI_PKT_CMD;
129 	fis[rhd_features] = (xfer->c_flags & C_DMA) ?
130 	    ATAPI_PKT_CMD_FTRE_DMA : 0;
131 
132 	return;
133 }
134 #endif /* NATAPIBUS */
135 
136 void
137 satafis_rdh_parse(struct ata_channel *chp, const uint8_t *fis)
138 {
139 	chp->ch_status = fis[rdh_status];
140 	chp->ch_error = fis[rdh_error];
141 }
142 
143 void
144 satafis_rdh_cmd_readreg(struct ata_command *ata_c, const uint8_t *fis)
145 {
146 	ata_c->r_command = fis[rdh_status];
147 	ata_c->r_features = fis[rdh_error];
148 	ata_c->r_error = fis[rdh_error];
149 	ata_c->r_sector = fis[rdh_sector];
150 	ata_c->r_cyl = fis[rdh_cyl_hi] << 8 | fis[rdh_cyl_lo];
151 	ata_c->r_head = fis[rdh_dh];
152 	ata_c->r_count = fis[rdh_seccnt];
153 }
154