1 /* $NetBSD: satafis_subr.c,v 1.9 2019/08/15 09:00:23 gson 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.9 2019/08/15 09:00:23 gson 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
satafis_rhd_construct_cmd(struct ata_command * ata_c,uint8_t * fis)70 satafis_rhd_construct_cmd(struct ata_command *ata_c, uint8_t *fis)
71 {
72
73 memset(fis, 0, RHD_FISLEN);
74
75 fis[fis_type] = RHD_FISTYPE;
76 fis[rhd_c] = RHD_C;
77 fis[rhd_command] = ata_c->r_command;
78 fis[rhd_features0] = (ata_c->r_features >> 0) & 0xff;
79
80 fis[rhd_lba0] = (ata_c->r_lba >> 0) & 0xff;
81 fis[rhd_lba1] = (ata_c->r_lba >> 8) & 0xff;
82 fis[rhd_lba2] = (ata_c->r_lba >> 16) & 0xff;
83 if ((ata_c->flags & AT_LBA48) != 0) {
84 fis[rhd_dh] = ata_c->r_device;
85 fis[rhd_lba3] = (ata_c->r_lba >> 24) & 0xff;
86 fis[rhd_lba4] = (ata_c->r_lba >> 32) & 0xff;
87 fis[rhd_lba5] = (ata_c->r_lba >> 40) & 0xff;
88 fis[rhd_features1] = (ata_c->r_features >> 8) & 0xff;
89 } else {
90 fis[rhd_dh] = (ata_c->r_device & 0xf0) |
91 ((ata_c->r_lba >> 24) & 0x0f);
92 }
93
94 fis[rhd_count0] = (ata_c->r_count >> 0) & 0xff;
95 if ((ata_c->flags & AT_LBA48) != 0) {
96 fis[rhd_count1] = (ata_c->r_count >> 8) & 0xff;
97 }
98 }
99
100 void
satafis_rhd_construct_bio(struct ata_xfer * xfer,uint8_t * fis)101 satafis_rhd_construct_bio(struct ata_xfer *xfer, uint8_t *fis)
102 {
103 struct ata_bio *ata_bio = &xfer->c_bio;
104 struct ata_drive_datas *drvp = &xfer->c_chp->ch_drive[xfer->c_drive];
105 uint16_t count, features;
106 uint8_t device;
107
108 count = xfer->c_bcount / drvp->lp->d_secsize;
109 features = 0;
110 device = WDSD_LBA;
111
112 memset(fis, 0, RHD_FISLEN);
113
114 fis[fis_type] = RHD_FISTYPE;
115 fis[rhd_c] = RHD_C;
116 if (ata_bio->flags & ATA_LBA48) {
117 fis[rhd_command] = (ata_bio->flags & ATA_READ) ?
118 WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
119
120 atacmd_toncq(xfer, &fis[rhd_command], &count, &features,
121 &device);
122 } else {
123 fis[rhd_command] =
124 (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA;
125 }
126 fis[rhd_features0] = (features >> 0) & 0xff;
127
128 fis[rhd_lba0] = (ata_bio->blkno >> 0) & 0xff;
129 fis[rhd_lba1] = (ata_bio->blkno >> 8) & 0xff;
130 fis[rhd_lba2] = (ata_bio->blkno >> 16) & 0xff;
131 if ((ata_bio->flags & ATA_LBA48) != 0) {
132 fis[rhd_dh] = device;
133 fis[rhd_lba3] = (ata_bio->blkno >> 24) & 0xff;
134 fis[rhd_lba4] = (ata_bio->blkno >> 32) & 0xff;
135 fis[rhd_lba5] = (ata_bio->blkno >> 40) & 0xff;
136 fis[rhd_features1] = (features >> 8) & 0xff;
137 } else {
138 fis[rhd_dh] = ((ata_bio->blkno >> 24) & 0x0f) |
139 (((ata_bio->flags & ATA_LBA) != 0) ? WDSD_LBA : 0);
140 }
141
142 fis[rhd_count0] = count & 0xff;
143 if ((ata_bio->flags & ATA_LBA48) != 0) {
144 fis[rhd_count1] = (count >> 8) & 0xff;
145 }
146 }
147
148 #if NATAPIBUS > 0
149 void
satafis_rhd_construct_atapi(struct ata_xfer * xfer,uint8_t * fis)150 satafis_rhd_construct_atapi(struct ata_xfer *xfer, uint8_t *fis)
151 {
152 int bcount16;
153
154 memset(fis, 0, RHD_FISLEN);
155
156 fis[fis_type] = RHD_FISTYPE;
157 fis[rhd_c] = RHD_C;
158 fis[rhd_command] = ATAPI_PKT_CMD;
159 bcount16 = xfer->c_bcount <= 0xffff ? xfer->c_bcount : 0xffff;
160 fis[rhd_lba1] = (bcount16 >> 0) & 0xff;
161 fis[rhd_lba2] = (bcount16 >> 8) & 0xff;
162 fis[rhd_features0] = (xfer->c_flags & C_DMA) ?
163 ATAPI_PKT_CMD_FTRE_DMA : 0;
164 }
165 #endif /* NATAPIBUS */
166
167 int
satafis_rdh_parse(struct ata_channel * chp,const uint8_t * fis)168 satafis_rdh_parse(struct ata_channel *chp, const uint8_t *fis)
169 {
170
171 return ATACH_ERR_ST(fis[rdh_error], fis[rdh_status]);
172 }
173
174 void
satafis_rdh_cmd_readreg(struct ata_command * ata_c,const uint8_t * fis)175 satafis_rdh_cmd_readreg(struct ata_command *ata_c, const uint8_t *fis)
176 {
177
178 ata_c->r_lba = (uint64_t)fis[rdh_lba0] << 0;
179 ata_c->r_lba |= (uint64_t)fis[rdh_lba1] << 8;
180 ata_c->r_lba |= (uint64_t)fis[rdh_lba2] << 16;
181 if ((ata_c->flags & AT_LBA48) != 0) {
182 ata_c->r_lba |= (uint64_t)fis[rdh_lba3] << 24;
183 ata_c->r_lba |= (uint64_t)fis[rdh_lba4] << 32;
184 ata_c->r_lba |= (uint64_t)fis[rdh_lba5] << 40;
185 ata_c->r_device = fis[rdh_dh];
186 } else {
187 ata_c->r_lba |= (uint64_t)(fis[rdh_dh] & 0x0f) << 24;
188 ata_c->r_device = fis[rdh_dh] & 0xf0;
189 }
190
191 ata_c->r_count = fis[rdh_count0] << 0;
192 if ((ata_c->flags & AT_LBA48) != 0) {
193 ata_c->r_count |= fis[rdh_count1] << 8;
194 }
195
196 ata_c->r_error = fis[rdh_error];
197 ata_c->r_status = fis[rdh_status];
198 }
199