ata_recovery.c revision 1.1.2.2 1 1.1.2.2 jdolecek /* $NetBSD: ata_recovery.c,v 1.1.2.2 2018/10/15 21:18:53 jdolecek Exp $ */
2 1.1.2.1 jdolecek
3 1.1.2.1 jdolecek /*-
4 1.1.2.1 jdolecek * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1.2.1 jdolecek * All rights reserved.
6 1.1.2.1 jdolecek *
7 1.1.2.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1.2.1 jdolecek * are met:
10 1.1.2.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1.2.1 jdolecek *
16 1.1.2.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.1 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.1 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.1 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.1 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.1 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.1 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.1 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.1 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.1 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.1 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.1 jdolecek */
28 1.1.2.1 jdolecek
29 1.1.2.1 jdolecek #include <sys/cdefs.h>
30 1.1.2.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: ata_recovery.c,v 1.1.2.2 2018/10/15 21:18:53 jdolecek Exp $");
31 1.1.2.1 jdolecek
32 1.1.2.1 jdolecek #include "opt_ata.h"
33 1.1.2.1 jdolecek
34 1.1.2.1 jdolecek #include <sys/param.h>
35 1.1.2.1 jdolecek #include <sys/systm.h>
36 1.1.2.1 jdolecek #include <sys/kernel.h>
37 1.1.2.1 jdolecek #include <sys/device.h>
38 1.1.2.1 jdolecek #include <sys/conf.h>
39 1.1.2.1 jdolecek #include <sys/fcntl.h>
40 1.1.2.1 jdolecek #include <sys/proc.h>
41 1.1.2.1 jdolecek #include <sys/kthread.h>
42 1.1.2.1 jdolecek #include <sys/errno.h>
43 1.1.2.1 jdolecek #include <sys/ataio.h>
44 1.1.2.1 jdolecek #include <sys/kmem.h>
45 1.1.2.1 jdolecek #include <sys/intr.h>
46 1.1.2.1 jdolecek #include <sys/bus.h>
47 1.1.2.1 jdolecek #include <sys/bitops.h>
48 1.1.2.1 jdolecek
49 1.1.2.1 jdolecek #include <dev/ata/ataconf.h>
50 1.1.2.1 jdolecek #include <dev/ata/atareg.h>
51 1.1.2.1 jdolecek #include <dev/ata/atavar.h>
52 1.1.2.1 jdolecek
53 1.1.2.1 jdolecek #define DEBUG_FUNCS 0x08
54 1.1.2.1 jdolecek #define DEBUG_PROBE 0x10
55 1.1.2.1 jdolecek #define DEBUG_DETACH 0x20
56 1.1.2.1 jdolecek #define DEBUG_XFERS 0x40
57 1.1.2.1 jdolecek #ifdef ATADEBUG
58 1.1.2.1 jdolecek extern int atadebug_mask;
59 1.1.2.1 jdolecek #define ATADEBUG_PRINT(args, level) \
60 1.1.2.1 jdolecek if (atadebug_mask & (level)) \
61 1.1.2.1 jdolecek printf args
62 1.1.2.1 jdolecek #else
63 1.1.2.1 jdolecek #define ATADEBUG_PRINT(args, level)
64 1.1.2.1 jdolecek #endif
65 1.1.2.1 jdolecek
66 1.1.2.1 jdolecek int
67 1.1.2.1 jdolecek ata_read_log_ext_ncq(struct ata_drive_datas *drvp, uint8_t flags,
68 1.1.2.1 jdolecek uint8_t *slot, uint8_t *status, uint8_t *err)
69 1.1.2.1 jdolecek {
70 1.1.2.1 jdolecek int rv;
71 1.1.2.1 jdolecek struct ata_channel *chp = drvp->chnl_softc;
72 1.1.2.2 jdolecek struct ata_xfer *xfer = &chp->recovery_xfer;
73 1.1.2.1 jdolecek struct atac_softc *atac = chp->ch_atac;
74 1.1.2.1 jdolecek uint8_t *tb, cksum, page;
75 1.1.2.1 jdolecek
76 1.1.2.1 jdolecek ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS);
77 1.1.2.1 jdolecek
78 1.1.2.1 jdolecek /* Only NCQ ATA drives support/need this */
79 1.1.2.1 jdolecek if (drvp->drive_type != ATA_DRIVET_ATA ||
80 1.1.2.1 jdolecek (drvp->drive_flags & ATA_DRIVE_NCQ) == 0)
81 1.1.2.1 jdolecek return EOPNOTSUPP;
82 1.1.2.1 jdolecek
83 1.1.2.1 jdolecek memset(xfer, 0, sizeof(*xfer));
84 1.1.2.1 jdolecek
85 1.1.2.2 jdolecek tb = chp->recovery_blk;
86 1.1.2.2 jdolecek memset(tb, 0, sizeof(chp->recovery_blk));
87 1.1.2.1 jdolecek
88 1.1.2.1 jdolecek /*
89 1.1.2.1 jdolecek * We could use READ LOG DMA EXT if drive supports it (i.e.
90 1.1.2.1 jdolecek * when it supports Streaming feature) to avoid PIO command,
91 1.1.2.1 jdolecek * and to make this a little faster. Realistically, it
92 1.1.2.1 jdolecek * should not matter.
93 1.1.2.1 jdolecek */
94 1.1.2.1 jdolecek xfer->c_flags |= C_SKIP_QUEUE;
95 1.1.2.1 jdolecek xfer->c_ata_c.r_command = WDCC_READ_LOG_EXT;
96 1.1.2.1 jdolecek xfer->c_ata_c.r_lba = page = WDCC_LOG_PAGE_NCQ;
97 1.1.2.1 jdolecek xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
98 1.1.2.1 jdolecek xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
99 1.1.2.1 jdolecek xfer->c_ata_c.r_count = 1;
100 1.1.2.1 jdolecek xfer->c_ata_c.r_device = WDSD_LBA;
101 1.1.2.1 jdolecek xfer->c_ata_c.flags = AT_READ | AT_LBA | AT_LBA48 | flags;
102 1.1.2.1 jdolecek xfer->c_ata_c.timeout = 1000; /* 1s */
103 1.1.2.1 jdolecek xfer->c_ata_c.data = tb;
104 1.1.2.2 jdolecek xfer->c_ata_c.bcount = sizeof(chp->recovery_blk);
105 1.1.2.1 jdolecek
106 1.1.2.1 jdolecek if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
107 1.1.2.1 jdolecek xfer) != ATACMD_COMPLETE) {
108 1.1.2.1 jdolecek rv = EAGAIN;
109 1.1.2.1 jdolecek goto out;
110 1.1.2.1 jdolecek }
111 1.1.2.1 jdolecek if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
112 1.1.2.1 jdolecek rv = EINVAL;
113 1.1.2.1 jdolecek goto out;
114 1.1.2.1 jdolecek }
115 1.1.2.1 jdolecek
116 1.1.2.1 jdolecek cksum = 0;
117 1.1.2.2 jdolecek for (int i = 0; i < sizeof(chp->recovery_blk); i++)
118 1.1.2.1 jdolecek cksum += tb[i];
119 1.1.2.1 jdolecek if (cksum != 0) {
120 1.1.2.1 jdolecek device_printf(drvp->drv_softc,
121 1.1.2.1 jdolecek "invalid checksum %x for READ LOG EXT page %x\n",
122 1.1.2.1 jdolecek cksum, page);
123 1.1.2.1 jdolecek rv = EINVAL;
124 1.1.2.1 jdolecek goto out;
125 1.1.2.1 jdolecek }
126 1.1.2.1 jdolecek
127 1.1.2.1 jdolecek if (tb[0] & WDCC_LOG_NQ) {
128 1.1.2.1 jdolecek /* not queued command */
129 1.1.2.1 jdolecek rv = EOPNOTSUPP;
130 1.1.2.1 jdolecek goto out;
131 1.1.2.1 jdolecek }
132 1.1.2.1 jdolecek
133 1.1.2.1 jdolecek *slot = tb[0] & 0x1f;
134 1.1.2.1 jdolecek *status = tb[2];
135 1.1.2.1 jdolecek *err = tb[3];
136 1.1.2.1 jdolecek
137 1.1.2.1 jdolecek if ((*status & WDCS_ERR) == 0) {
138 1.1.2.1 jdolecek /*
139 1.1.2.1 jdolecek * We expect error here. Normal physical drives always
140 1.1.2.1 jdolecek * do, it's part of ATA standard. However, QEMU AHCI emulation
141 1.1.2.1 jdolecek * mishandles READ LOG EXT in a way that the command itself
142 1.1.2.1 jdolecek * returns without error, but no data is transferred.
143 1.1.2.1 jdolecek */
144 1.1.2.1 jdolecek device_printf(drvp->drv_softc,
145 1.1.2.1 jdolecek "READ LOG EXT page %x failed to report error: "
146 1.1.2.1 jdolecek "slot %d err %x status %x\n",
147 1.1.2.1 jdolecek page, *slot, *err, *status);
148 1.1.2.1 jdolecek rv = EOPNOTSUPP;
149 1.1.2.1 jdolecek goto out;
150 1.1.2.1 jdolecek }
151 1.1.2.1 jdolecek
152 1.1.2.1 jdolecek rv = 0;
153 1.1.2.1 jdolecek
154 1.1.2.1 jdolecek out:
155 1.1.2.1 jdolecek return rv;
156 1.1.2.1 jdolecek }
157 1.1.2.1 jdolecek
158 1.1.2.1 jdolecek /*
159 1.1.2.1 jdolecek * Must be called without channel lock, and with interrupts blocked.
160 1.1.2.1 jdolecek */
161 1.1.2.1 jdolecek void
162 1.1.2.1 jdolecek ata_recovery_resume(struct ata_channel *chp, int drive, int tfd, int flags)
163 1.1.2.1 jdolecek {
164 1.1.2.1 jdolecek struct ata_drive_datas *drvp;
165 1.1.2.1 jdolecek uint8_t slot, eslot, st, err;
166 1.1.2.1 jdolecek int error;
167 1.1.2.1 jdolecek struct ata_xfer *xfer;
168 1.1.2.1 jdolecek const uint8_t ch_openings = ata_queue_openings(chp);
169 1.1.2.1 jdolecek
170 1.1.2.2 jdolecek ata_channel_lock_owned(chp);
171 1.1.2.2 jdolecek
172 1.1.2.1 jdolecek ata_queue_hold(chp);
173 1.1.2.1 jdolecek
174 1.1.2.1 jdolecek KASSERT(drive < chp->ch_ndrives);
175 1.1.2.1 jdolecek drvp = &chp->ch_drive[drive];
176 1.1.2.1 jdolecek
177 1.1.2.2 jdolecek /* Drop the lock for the READ LOG EXT request */
178 1.1.2.2 jdolecek ata_channel_unlock(chp);
179 1.1.2.2 jdolecek
180 1.1.2.1 jdolecek /*
181 1.1.2.1 jdolecek * When running NCQ commands, READ LOG EXT is necessary to clear the
182 1.1.2.1 jdolecek * error condition and unblock the device.
183 1.1.2.1 jdolecek */
184 1.1.2.1 jdolecek error = ata_read_log_ext_ncq(drvp, flags, &eslot, &st, &err);
185 1.1.2.1 jdolecek
186 1.1.2.1 jdolecek ata_channel_lock(chp);
187 1.1.2.1 jdolecek ata_queue_unhold(chp);
188 1.1.2.1 jdolecek ata_channel_unlock(chp);
189 1.1.2.1 jdolecek
190 1.1.2.1 jdolecek switch (error) {
191 1.1.2.1 jdolecek case 0:
192 1.1.2.1 jdolecek /* Error out the particular NCQ xfer, then requeue the others */
193 1.1.2.1 jdolecek if ((ata_queue_active(chp) & (1U << eslot)) != 0) {
194 1.1.2.1 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, eslot);
195 1.1.2.1 jdolecek xfer->c_flags |= C_RECOVERED;
196 1.1.2.1 jdolecek xfer->ops->c_intr(chp, xfer, ATACH_ERR_ST(err, st));
197 1.1.2.1 jdolecek }
198 1.1.2.1 jdolecek break;
199 1.1.2.1 jdolecek
200 1.1.2.1 jdolecek case EOPNOTSUPP:
201 1.1.2.1 jdolecek /*
202 1.1.2.1 jdolecek * Non-NCQ command error, just find the slot and end with
203 1.1.2.1 jdolecek * the error.
204 1.1.2.1 jdolecek */
205 1.1.2.1 jdolecek for (slot = 0; slot < ch_openings; slot++) {
206 1.1.2.1 jdolecek if ((ata_queue_active(chp) & (1U << slot)) != 0) {
207 1.1.2.1 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, slot);
208 1.1.2.1 jdolecek xfer->ops->c_intr(chp, xfer, tfd);
209 1.1.2.1 jdolecek }
210 1.1.2.1 jdolecek }
211 1.1.2.1 jdolecek break;
212 1.1.2.1 jdolecek
213 1.1.2.1 jdolecek case EAGAIN:
214 1.1.2.1 jdolecek /*
215 1.1.2.1 jdolecek * Failed to get resources to run the recovery command, must
216 1.1.2.1 jdolecek * reset the drive. This will also kill all still outstanding
217 1.1.2.1 jdolecek * transfers.
218 1.1.2.1 jdolecek */
219 1.1.2.1 jdolecek ata_channel_lock(chp);
220 1.1.2.1 jdolecek ata_thread_run(chp, ATACH_TH_RESET, ATACH_NODRIVE, flags);
221 1.1.2.1 jdolecek ata_channel_unlock(chp);
222 1.1.2.1 jdolecek goto out;
223 1.1.2.1 jdolecek /* NOTREACHED */
224 1.1.2.1 jdolecek
225 1.1.2.1 jdolecek default:
226 1.1.2.1 jdolecek /*
227 1.1.2.1 jdolecek * The command to get the slot failed. Kill outstanding
228 1.1.2.1 jdolecek * commands for the same drive only. No need to reset
229 1.1.2.1 jdolecek * the drive, it's unblocked nevertheless.
230 1.1.2.1 jdolecek */
231 1.1.2.1 jdolecek break;
232 1.1.2.1 jdolecek }
233 1.1.2.1 jdolecek
234 1.1.2.1 jdolecek /* Requeue all unfinished commands for same drive as failed command */
235 1.1.2.1 jdolecek for (slot = 0; slot < ch_openings; slot++) {
236 1.1.2.1 jdolecek if ((ata_queue_active(chp) & (1U << slot)) == 0)
237 1.1.2.1 jdolecek continue;
238 1.1.2.1 jdolecek
239 1.1.2.1 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, slot);
240 1.1.2.1 jdolecek if (drive != xfer->c_drive)
241 1.1.2.1 jdolecek continue;
242 1.1.2.1 jdolecek
243 1.1.2.1 jdolecek xfer->ops->c_kill_xfer(chp, xfer,
244 1.1.2.1 jdolecek (error == 0) ? KILL_REQUEUE : KILL_RESET);
245 1.1.2.1 jdolecek }
246 1.1.2.1 jdolecek
247 1.1.2.1 jdolecek out:
248 1.1.2.1 jdolecek /* Nothing more to do */
249 1.1.2.2 jdolecek ata_channel_lock(chp);
250 1.1.2.1 jdolecek }
251