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