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