ata_recovery.c revision 1.2 1 1.2 jdolecek /* $NetBSD: ata_recovery.c,v 1.2 2018/10/22 20:13:47 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.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: ata_recovery.c,v 1.2 2018/10/22 20:13:47 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.2 jdolecek if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
107 1.2 jdolecek xfer) != ATACMD_COMPLETE) {
108 1.2 jdolecek rv = EAGAIN;
109 1.2 jdolecek goto out;
110 1.2 jdolecek }
111 1.2 jdolecek if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
112 1.2 jdolecek rv = EINVAL;
113 1.2 jdolecek goto out;
114 1.2 jdolecek }
115 1.2 jdolecek
116 1.2 jdolecek cksum = 0;
117 1.2 jdolecek for (int i = 0; i < sizeof(chp->recovery_blk); i++)
118 1.2 jdolecek cksum += tb[i];
119 1.2 jdolecek if (cksum != 0) {
120 1.2 jdolecek device_printf(drvp->drv_softc,
121 1.2 jdolecek "invalid checksum %x for READ LOG EXT page %x\n",
122 1.2 jdolecek cksum, page);
123 1.2 jdolecek rv = EINVAL;
124 1.2 jdolecek goto out;
125 1.2 jdolecek }
126 1.2 jdolecek
127 1.2 jdolecek if (tb[0] & WDCC_LOG_NQ) {
128 1.2 jdolecek /* not queued command */
129 1.2 jdolecek rv = EOPNOTSUPP;
130 1.2 jdolecek goto out;
131 1.2 jdolecek }
132 1.2 jdolecek
133 1.2 jdolecek *slot = tb[0] & 0x1f;
134 1.2 jdolecek *status = tb[2];
135 1.2 jdolecek *err = tb[3];
136 1.2 jdolecek
137 1.2 jdolecek if ((*status & WDCS_ERR) == 0) {
138 1.2 jdolecek /*
139 1.2 jdolecek * We expect error here. Normal physical drives always
140 1.2 jdolecek * do, it's part of ATA standard. However, QEMU AHCI emulation
141 1.2 jdolecek * mishandles READ LOG EXT in a way that the command itself
142 1.2 jdolecek * returns without error, but no data is transferred.
143 1.2 jdolecek */
144 1.2 jdolecek device_printf(drvp->drv_softc,
145 1.2 jdolecek "READ LOG EXT page %x failed to report error: "
146 1.2 jdolecek "slot %d err %x status %x\n",
147 1.2 jdolecek page, *slot, *err, *status);
148 1.2 jdolecek rv = EOPNOTSUPP;
149 1.2 jdolecek goto out;
150 1.2 jdolecek }
151 1.2 jdolecek
152 1.2 jdolecek rv = 0;
153 1.2 jdolecek
154 1.2 jdolecek out:
155 1.2 jdolecek return rv;
156 1.2 jdolecek }
157 1.2 jdolecek
158 1.2 jdolecek /*
159 1.2 jdolecek * Must be called without channel lock, and with interrupts blocked.
160 1.2 jdolecek */
161 1.2 jdolecek void
162 1.2 jdolecek ata_recovery_resume(struct ata_channel *chp, int drive, int tfd, int flags)
163 1.2 jdolecek {
164 1.2 jdolecek struct ata_drive_datas *drvp;
165 1.2 jdolecek uint8_t slot, eslot, st, err;
166 1.2 jdolecek int error;
167 1.2 jdolecek struct ata_xfer *xfer;
168 1.2 jdolecek const uint8_t ch_openings = ata_queue_openings(chp);
169 1.2 jdolecek
170 1.2 jdolecek ata_channel_lock_owned(chp);
171 1.2 jdolecek
172 1.2 jdolecek ata_queue_hold(chp);
173 1.2 jdolecek
174 1.2 jdolecek KASSERT(drive < chp->ch_ndrives);
175 1.2 jdolecek drvp = &chp->ch_drive[drive];
176 1.2 jdolecek
177 1.2 jdolecek /* Drop the lock for the READ LOG EXT request */
178 1.2 jdolecek ata_channel_unlock(chp);
179 1.2 jdolecek
180 1.2 jdolecek /*
181 1.2 jdolecek * When running NCQ commands, READ LOG EXT is necessary to clear the
182 1.2 jdolecek * error condition and unblock the device.
183 1.2 jdolecek */
184 1.2 jdolecek error = ata_read_log_ext_ncq(drvp, flags, &eslot, &st, &err);
185 1.2 jdolecek
186 1.2 jdolecek ata_channel_lock(chp);
187 1.2 jdolecek ata_queue_unhold(chp);
188 1.2 jdolecek ata_channel_unlock(chp);
189 1.2 jdolecek
190 1.2 jdolecek switch (error) {
191 1.2 jdolecek case 0:
192 1.2 jdolecek /* Error out the particular NCQ xfer, then requeue the others */
193 1.2 jdolecek if ((ata_queue_active(chp) & (1U << eslot)) != 0) {
194 1.2 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, eslot);
195 1.2 jdolecek xfer->c_flags |= C_RECOVERED;
196 1.2 jdolecek xfer->ops->c_intr(chp, xfer, ATACH_ERR_ST(err, st));
197 1.2 jdolecek }
198 1.2 jdolecek break;
199 1.2 jdolecek
200 1.2 jdolecek case EOPNOTSUPP:
201 1.2 jdolecek /*
202 1.2 jdolecek * Non-NCQ command error, just find the slot and end with
203 1.2 jdolecek * the error.
204 1.2 jdolecek */
205 1.2 jdolecek for (slot = 0; slot < ch_openings; slot++) {
206 1.2 jdolecek if ((ata_queue_active(chp) & (1U << slot)) != 0) {
207 1.2 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, slot);
208 1.2 jdolecek xfer->ops->c_intr(chp, xfer, tfd);
209 1.2 jdolecek }
210 1.2 jdolecek }
211 1.2 jdolecek break;
212 1.2 jdolecek
213 1.2 jdolecek case EAGAIN:
214 1.2 jdolecek /*
215 1.2 jdolecek * Failed to get resources to run the recovery command, must
216 1.2 jdolecek * reset the drive. This will also kill all still outstanding
217 1.2 jdolecek * transfers.
218 1.2 jdolecek */
219 1.2 jdolecek ata_channel_lock(chp);
220 1.2 jdolecek ata_thread_run(chp, ATACH_TH_RESET, ATACH_NODRIVE, flags);
221 1.2 jdolecek ata_channel_unlock(chp);
222 1.2 jdolecek goto out;
223 1.2 jdolecek /* NOTREACHED */
224 1.2 jdolecek
225 1.2 jdolecek default:
226 1.2 jdolecek /*
227 1.2 jdolecek * The command to get the slot failed. Kill outstanding
228 1.2 jdolecek * commands for the same drive only. No need to reset
229 1.2 jdolecek * the drive, it's unblocked nevertheless.
230 1.2 jdolecek */
231 1.2 jdolecek break;
232 1.2 jdolecek }
233 1.2 jdolecek
234 1.2 jdolecek /* Requeue all unfinished commands for same drive as failed command */
235 1.2 jdolecek for (slot = 0; slot < ch_openings; slot++) {
236 1.2 jdolecek if ((ata_queue_active(chp) & (1U << slot)) == 0)
237 1.2 jdolecek continue;
238 1.2 jdolecek
239 1.2 jdolecek xfer = ata_queue_hwslot_to_xfer(chp, slot);
240 1.2 jdolecek if (drive != xfer->c_drive)
241 1.2 jdolecek continue;
242 1.2 jdolecek
243 1.2 jdolecek xfer->ops->c_kill_xfer(chp, xfer,
244 1.2 jdolecek (error == 0) ? KILL_REQUEUE : KILL_RESET);
245 1.2 jdolecek }
246 1.2 jdolecek
247 1.2 jdolecek out:
248 1.2 jdolecek /* Nothing more to do */
249 1.2 jdolecek ata_channel_lock(chp);
250 1.2 jdolecek }
251