summaryrefslogtreecommitdiff
path: root/sys/dev/ata/ata_recovery.c
blob: 25828f6efb161db78ff64c69e1f5b446bb0be4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*	$NetBSD: ata_recovery.c,v 1.4 2020/04/13 10:49:34 jdolecek Exp $	*/

/*-
 * Copyright (c) 2018 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ata_recovery.c,v 1.4 2020/04/13 10:49:34 jdolecek Exp $");

#include "opt_ata.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/errno.h>
#include <sys/ataio.h>
#include <sys/kmem.h>
#include <sys/intr.h>
#include <sys/bus.h>
#include <sys/bitops.h>

#include <dev/ata/ataconf.h>
#include <dev/ata/atareg.h>
#include <dev/ata/atavar.h>

#define DEBUG_FUNCS  0x08
#define DEBUG_PROBE  0x10
#define DEBUG_DETACH 0x20
#define	DEBUG_XFERS  0x40
#ifdef ATADEBUG
extern int atadebug_mask;
#define ATADEBUG_PRINT(args, level) \
	if (atadebug_mask & (level)) \
		printf args
#else
#define ATADEBUG_PRINT(args, level)
#endif

int
ata_read_log_ext_ncq(struct ata_drive_datas *drvp, uint8_t flags,
    uint8_t *slot, uint8_t *status, uint8_t *err)
{
	int rv;
	struct ata_channel *chp = drvp->chnl_softc;
	struct ata_xfer *xfer = &chp->recovery_xfer;
	struct atac_softc *atac = chp->ch_atac;
	uint8_t *tb, cksum, page;

	ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS);

	/* Only NCQ ATA drives support/need this */
	if (drvp->drive_type != ATA_DRIVET_ATA ||
	    (drvp->drive_flags & ATA_DRIVE_NCQ) == 0)
		return EOPNOTSUPP;

	memset(xfer, 0, sizeof(*xfer));

	tb = chp->recovery_blk;
	memset(tb, 0, sizeof(chp->recovery_blk));

	/*
	 * We could use READ LOG DMA EXT if drive supports it (i.e.
	 * when it supports Streaming feature) to avoid PIO command,
	 * and to make this a little faster. Realistically, it
	 * should not matter.
	 */
	xfer->c_flags |= C_SKIP_QUEUE;
	xfer->c_ata_c.r_command = WDCC_READ_LOG_EXT;
	xfer->c_ata_c.r_lba = page = WDCC_LOG_PAGE_NCQ;
	xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
	xfer->c_ata_c.r_count = 1;
	xfer->c_ata_c.r_device = WDSD_LBA;
	xfer->c_ata_c.flags = AT_READ | AT_LBA | AT_LBA48 | flags;
	xfer->c_ata_c.timeout = 1000; /* 1s */
	xfer->c_ata_c.data = tb;
	xfer->c_ata_c.bcount = sizeof(chp->recovery_blk);

	(*atac->atac_bustype_ata->ata_exec_command)(drvp, xfer);
	ata_wait_cmd(chp, xfer);

	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
		rv = EINVAL;
		goto out;
	}

	cksum = 0;
	for (int i = 0; i < sizeof(chp->recovery_blk); i++)
		cksum += tb[i];
	if (cksum != 0) {
		device_printf(drvp->drv_softc,
		    "invalid checksum %x for READ LOG EXT page %x\n",
		    cksum, page);
		rv = EINVAL;
		goto out;
	}

	if (tb[0] & WDCC_LOG_NQ) {
		/* not queued command */
		rv = EOPNOTSUPP;
		goto out;
	}

	*slot = tb[0] & 0x1f;
	*status = tb[2];
	*err = tb[3];

	if ((*status & WDCS_ERR) == 0) {
		/*
		 * We expect error here. Normal physical drives always
		 * do, it's part of ATA standard. However, QEMU AHCI emulation
		 * mishandles READ LOG EXT in a way that the command itself
		 * returns without error, but no data is transferred.
		 */
		device_printf(drvp->drv_softc,
		    "READ LOG EXT page %x failed to report error: "
		    "slot %d err %x status %x\n",
		    page, *slot, *err, *status);
		rv = EOPNOTSUPP;
		goto out;
	}

	rv = 0;

out:
	return rv;
}

/*
 * Must be called without channel lock, and with interrupts blocked.
 */
void
ata_recovery_resume(struct ata_channel *chp, int drive, int tfd, int flags)
{
	struct ata_drive_datas *drvp;
	uint8_t slot, eslot, st, err;
	int error;
	struct ata_xfer *xfer;
	const uint8_t ch_openings = ata_queue_openings(chp);

	ata_channel_lock_owned(chp);

	ata_queue_hold(chp);

	/* Stop the timeout callout, recovery will requeue once done */
	callout_stop(&chp->c_timo_callout);

	KASSERT(drive < chp->ch_ndrives);
	drvp = &chp->ch_drive[drive];

	/* Drop the lock for the READ LOG EXT request */
	ata_channel_unlock(chp);

	/*
	 * When running NCQ commands, READ LOG EXT is necessary to clear the
	 * error condition and unblock the device.
	 */
	error = ata_read_log_ext_ncq(drvp, flags, &eslot, &st, &err);

	ata_channel_lock(chp);
	ata_queue_unhold(chp);
	ata_channel_unlock(chp);

	switch (error) {
	case 0:
		/* Error out the particular NCQ xfer, then requeue the others */
		if ((ata_queue_active(chp) & (1U << eslot)) != 0) {
			xfer = ata_queue_hwslot_to_xfer(chp, eslot);
			xfer->c_flags |= C_RECOVERED;
			xfer->ops->c_intr(chp, xfer, ATACH_ERR_ST(err, st));
		}
		break;

	case EOPNOTSUPP:
		/*
		 * Non-NCQ command error, just find the slot and end with
		 * the error.
		 */
		for (slot = 0; slot < ch_openings; slot++) {
			if ((ata_queue_active(chp) & (1U << slot)) != 0) {
				xfer = ata_queue_hwslot_to_xfer(chp, slot);
				xfer->ops->c_intr(chp, xfer, tfd);
			}
		}
		break;

	case EAGAIN:
		/*
		 * Failed to get resources to run the recovery command, must
		 * reset the drive. This will also kill all still outstanding
		 * transfers.
		 */
		ata_channel_lock(chp);
		ata_thread_run(chp, ATACH_TH_RESET, ATACH_NODRIVE, flags);
		ata_channel_unlock(chp);
		goto out;
		/* NOTREACHED */

	default:
		/*
		 * The command to get the slot failed. Kill outstanding
		 * commands for the same drive only. No need to reset
		 * the drive, it's unblocked nevertheless.
		 */
		break;
	}

	/* Requeue all unfinished commands for same drive as failed command */ 
	for (slot = 0; slot < ch_openings; slot++) {
		if ((ata_queue_active(chp) & (1U << slot)) == 0)
			continue;

		xfer = ata_queue_hwslot_to_xfer(chp, slot);
		if (drive != xfer->c_drive)
			continue;

		xfer->ops->c_kill_xfer(chp, xfer,
		    (error == 0) ? KILL_REQUEUE : KILL_RESET);
	}

out:
	/* Nothing more to do */
	ata_channel_lock(chp);
}