satapmp_subr.c revision 1.15 1 /* $NetBSD: satapmp_subr.c,v 1.15 2018/10/22 20:13:47 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 2012 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: satapmp_subr.c,v 1.15 2018/10/22 20:13:47 jdolecek Exp $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/fcntl.h>
36 #include <sys/proc.h>
37 #include <sys/errno.h>
38 #include <sys/kmem.h>
39 #include <sys/intr.h>
40
41 #include <dev/ata/ataconf.h>
42 #include <dev/ata/atareg.h>
43 #include <dev/ata/atavar.h>
44
45 #include <dev/ata/satapmpvar.h>
46 #include <dev/ata/satapmpreg.h>
47 #include <dev/ata/satavar.h>
48 #include <dev/ata/satareg.h>
49
50 static int
51 satapmp_read_8(struct ata_channel *chp, int port, int reg, uint64_t *value,
52 struct ata_xfer *xfer)
53 {
54 struct atac_softc *atac = chp->ch_atac;
55 struct ata_drive_datas *drvp;
56 int error = 0;
57
58 KASSERT(port < PMP_MAX_DRIVES);
59 KASSERT(reg < PMP_GSCR_NREGS);
60 KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
61 drvp = &chp->ch_drive[PMP_PORT_CTL];
62 KASSERT(drvp->drive == PMP_PORT_CTL);
63 ata_channel_lock_owned(chp);
64
65 memset(xfer, 0, sizeof(*xfer));
66 xfer->c_ata_c.r_command = PMPC_READ_PORT;
67 xfer->c_ata_c.r_features = reg;
68 xfer->c_ata_c.r_device = port;
69 xfer->c_ata_c.timeout = 3000; /* 3s */
70 xfer->c_ata_c.r_st_bmask = 0;
71 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
72 xfer->c_ata_c.flags = AT_LBA48 | AT_READREG | AT_WAIT;
73
74 ata_channel_unlock(chp);
75 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
76 xfer) != ATACMD_COMPLETE) {
77 aprint_error_dev(chp->atabus,
78 "PMP port %d register %d read failed\n", port, reg);
79 error = EIO;
80 goto out;
81 }
82 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
83 aprint_error_dev(chp->atabus,
84 "PMP port %d register %d read failed, flags 0x%x\n",
85 port, reg, xfer->c_ata_c.flags);
86 error = EIO;
87 goto out;
88 }
89 if (xfer->c_ata_c.flags & AT_ERROR) {
90 aprint_verbose_dev(chp->atabus,
91 "PMP port %d register %d read failed, error 0x%x\n",
92 port, reg, xfer->c_ata_c.r_error);
93 error = EIO;
94 goto out;
95 }
96
97 *value = ((uint64_t)((xfer->c_ata_c.r_lba >> 24) & 0xffffff) << 40) |
98 ((uint64_t)((xfer->c_ata_c.r_count >> 8) & 0xff) << 32) |
99 ((uint64_t)((xfer->c_ata_c.r_lba >> 0) & 0xffffff) << 8) |
100 ((uint64_t)((xfer->c_ata_c.r_count >> 0) & 0xff) << 0);
101
102 out:
103 ata_channel_lock(chp);
104 return error;
105 }
106
107 static inline int
108 satapmp_read(struct ata_channel *chp, int port, int reg, uint32_t *value,
109 struct ata_xfer *xfer)
110 {
111 uint64_t value64;
112 int ret;
113
114 ret = satapmp_read_8(chp, port, reg, &value64, xfer);
115 if (ret)
116 return ret;
117
118 *value = value64 & 0xffffffff;
119 return ret;
120 }
121
122 static int
123 satapmp_write_8(struct ata_channel *chp, int port, int reg, uint64_t value,
124 struct ata_xfer *xfer)
125 {
126 struct atac_softc *atac = chp->ch_atac;
127 struct ata_drive_datas *drvp;
128 int error = 0;
129
130 KASSERT(port < PMP_MAX_DRIVES);
131 KASSERT(reg < PMP_GSCR_NREGS);
132 KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
133 drvp = &chp->ch_drive[PMP_PORT_CTL];
134 KASSERT(drvp->drive == PMP_PORT_CTL);
135 ata_channel_lock_owned(chp);
136
137 memset(xfer, 0, sizeof(*xfer));
138 xfer->c_ata_c.r_command = PMPC_WRITE_PORT;
139 xfer->c_ata_c.r_features = reg;
140 xfer->c_ata_c.r_device = port;
141 xfer->c_ata_c.r_lba = (((value >> 40) & 0xffffff) << 24) |
142 (((value >> 8) & 0xffffff) << 0);
143 xfer->c_ata_c.r_count = (((value >> 32) & 0xff) << 8) |
144 (((value >> 0) & 0xff) << 0);
145 xfer->c_ata_c.timeout = 3000; /* 3s */
146 xfer->c_ata_c.r_st_bmask = 0;
147 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
148 xfer->c_ata_c.flags = AT_LBA48 | AT_WAIT;
149
150 ata_channel_unlock(chp);
151 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
152 xfer) != ATACMD_COMPLETE) {
153 aprint_error_dev(chp->atabus,
154 "PMP port %d register %d write failed\n", port, reg);
155 error = EIO;
156 goto out;
157 }
158 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
159 aprint_error_dev(chp->atabus,
160 "PMP port %d register %d write failed, flags 0x%x\n",
161 port, reg, xfer->c_ata_c.flags);
162 error = EIO;
163 goto out;
164 }
165 if (xfer->c_ata_c.flags & AT_ERROR) {
166 aprint_verbose_dev(chp->atabus,
167 "PMP port %d register %d write failed, error 0x%x\n",
168 port, reg, xfer->c_ata_c.r_error);
169 error = EIO;
170 goto out;
171 }
172
173 out:
174 ata_channel_lock(chp);
175 return error;
176 }
177
178 static inline int
179 satapmp_write(struct ata_channel *chp, int port, int reg, uint32_t value,
180 struct ata_xfer *xfer)
181 {
182 return satapmp_write_8(chp, port, reg, value, xfer);
183 }
184
185 /*
186 * Reset one port's PHY and bring it online
187 * XXX duplicate of sata_reset_interface()
188 */
189 static uint32_t
190 satapmp_reset_device_port(struct ata_channel *chp, int port,
191 struct ata_xfer *xfer)
192 {
193 uint32_t scontrol, sstatus;
194 int i;
195
196 ata_channel_lock_owned(chp);
197
198 /* bring the PHY online */
199 scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
200 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
201 return 0;
202
203 ata_delay(chp, 50, "sataup", AT_WAIT);
204 scontrol &= ~SControl_DET_INIT;
205 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
206 return 0;
207 ata_delay(chp, 50, "sataup", AT_WAIT);
208
209 /* wait up to 1s for device to come up */
210 for (i = 0; i < 100; i++) {
211
212 if (satapmp_read(chp, port, PMP_PSCR_SStatus, &sstatus,
213 xfer) != 0)
214 return 0;
215 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
216 break;
217 ata_delay(chp, 10, "sataup", AT_WAIT);
218 }
219
220 switch (sstatus & SStatus_DET_mask) {
221 case SStatus_DET_NODEV:
222 /* No Device; be silent. */
223 break;
224 case SStatus_DET_DEV_NE:
225 aprint_error("%s PMP port %d: device connected, but "
226 "communication not established\n",
227 device_xname(chp->atabus), port);
228 break;
229 case SStatus_DET_OFFLINE:
230 aprint_error("%s PMP port %d: PHY offline\n",
231 device_xname(chp->atabus), port);
232 break;
233 case SStatus_DET_DEV:
234 aprint_normal("%s PMP port %d: device present, speed: %s\n",
235 device_xname(chp->atabus), port, sata_speed(sstatus));
236 break;
237 default:
238 aprint_error("%s PMP port %d: unknown SStatus: 0x%08x\n",
239 device_xname(chp->atabus), port, sstatus);
240 }
241 return(sstatus & SStatus_DET_mask);
242 }
243
244 static void __noinline
245 satapmp_rescan(struct ata_channel *chp, struct ata_xfer *xfer)
246 {
247 int i;
248 uint32_t sig;
249
250 KASSERT(chp->ch_satapmp_nports <= PMP_PORT_CTL);
251 KASSERT(chp->ch_satapmp_nports <= chp->ch_ndrives);
252 ata_channel_lock_owned(chp);
253
254 for (i = 0; i < chp->ch_satapmp_nports; i++) {
255 if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE ||
256 satapmp_reset_device_port(chp, i, xfer)
257 != SStatus_DET_DEV) {
258 continue;
259 }
260 if (satapmp_write(chp, i, PMP_PSCR_SError, 0xffffffff, xfer)
261 != 0) {
262 aprint_error("%s PMP port %d: can't write SError\n",
263 device_xname(chp->atabus), i);
264 continue;
265 }
266
267 ata_channel_lock_owned(chp);
268 chp->ch_atac->atac_bustype_ata->ata_reset_drive(
269 &chp->ch_drive[i], AT_WAIT, &sig);
270
271 sata_interpret_sig(chp, i, sig);
272 }
273 }
274
275 void
276 satapmp_attach(struct ata_channel *chp)
277 {
278 uint32_t id, rev, inf;
279 struct ata_xfer *xfer;
280
281 xfer = ata_get_xfer(chp, false);
282 if (xfer == NULL) {
283 aprint_normal_dev(chp->atabus, "no available xfer\n");
284 return;
285 }
286
287 ata_channel_lock(chp);
288
289 if (satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_ID, &id, xfer) != 0 ||
290 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_REV, &rev, xfer) != 0 ||
291 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_INF, &inf, xfer) != 0) {
292 aprint_normal_dev(chp->atabus, "can't read PMP registers\n");
293 goto out;
294 }
295
296 aprint_normal("%s at %s channel %d: SATA port multiplier, %d ports\n",
297 device_xname(chp->atabus),
298 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
299 PMP_INF_NPORTS(inf));
300 aprint_verbose_dev(chp->atabus,
301 "vendor 0x%04x, product 0x%04x",
302 PMP_ID_VEND(id), PMP_ID_DEV(id));
303 if (rev & PMP_REV_SPEC_11) {
304 aprint_verbose(", revision 1.1");
305 } else if (rev & PMP_REV_SPEC_10) {
306 aprint_verbose(", revision 1.0");
307 } else {
308 aprint_verbose(", unknown revision 0x%x", rev & 0x0f);
309 }
310 aprint_verbose(", level %d\n", PMP_REV_LEVEL(rev));
311
312 chp->ch_satapmp_nports = PMP_INF_NPORTS(inf);
313
314 /* reset and bring up PHYs */
315 satapmp_rescan(chp, xfer);
316
317 out:
318 ata_channel_unlock(chp);
319 ata_free_xfer(chp, xfer);
320 }
321