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