Home | History | Annotate | Line # | Download | only in ata
ata_recovery.c revision 1.3
      1 /*	$NetBSD: ata_recovery.c,v 1.3 2020/04/04 22:30:02 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.3 2020/04/04 22:30:02 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 	/* Stop the timeout callout, recovery will requeue once done */
    175 	callout_stop(&chp->c_timo_callout);
    176 
    177 	KASSERT(drive < chp->ch_ndrives);
    178 	drvp = &chp->ch_drive[drive];
    179 
    180 	/* Drop the lock for the READ LOG EXT request */
    181 	ata_channel_unlock(chp);
    182 
    183 	/*
    184 	 * When running NCQ commands, READ LOG EXT is necessary to clear the
    185 	 * error condition and unblock the device.
    186 	 */
    187 	error = ata_read_log_ext_ncq(drvp, flags, &eslot, &st, &err);
    188 
    189 	ata_channel_lock(chp);
    190 	ata_queue_unhold(chp);
    191 	ata_channel_unlock(chp);
    192 
    193 	switch (error) {
    194 	case 0:
    195 		/* Error out the particular NCQ xfer, then requeue the others */
    196 		if ((ata_queue_active(chp) & (1U << eslot)) != 0) {
    197 			xfer = ata_queue_hwslot_to_xfer(chp, eslot);
    198 			xfer->c_flags |= C_RECOVERED;
    199 			xfer->ops->c_intr(chp, xfer, ATACH_ERR_ST(err, st));
    200 		}
    201 		break;
    202 
    203 	case EOPNOTSUPP:
    204 		/*
    205 		 * Non-NCQ command error, just find the slot and end with
    206 		 * the error.
    207 		 */
    208 		for (slot = 0; slot < ch_openings; slot++) {
    209 			if ((ata_queue_active(chp) & (1U << slot)) != 0) {
    210 				xfer = ata_queue_hwslot_to_xfer(chp, slot);
    211 				xfer->ops->c_intr(chp, xfer, tfd);
    212 			}
    213 		}
    214 		break;
    215 
    216 	case EAGAIN:
    217 		/*
    218 		 * Failed to get resources to run the recovery command, must
    219 		 * reset the drive. This will also kill all still outstanding
    220 		 * transfers.
    221 		 */
    222 		ata_channel_lock(chp);
    223 		ata_thread_run(chp, ATACH_TH_RESET, ATACH_NODRIVE, flags);
    224 		ata_channel_unlock(chp);
    225 		goto out;
    226 		/* NOTREACHED */
    227 
    228 	default:
    229 		/*
    230 		 * The command to get the slot failed. Kill outstanding
    231 		 * commands for the same drive only. No need to reset
    232 		 * the drive, it's unblocked nevertheless.
    233 		 */
    234 		break;
    235 	}
    236 
    237 	/* Requeue all unfinished commands for same drive as failed command */
    238 	for (slot = 0; slot < ch_openings; slot++) {
    239 		if ((ata_queue_active(chp) & (1U << slot)) == 0)
    240 			continue;
    241 
    242 		xfer = ata_queue_hwslot_to_xfer(chp, slot);
    243 		if (drive != xfer->c_drive)
    244 			continue;
    245 
    246 		xfer->ops->c_kill_xfer(chp, xfer,
    247 		    (error == 0) ? KILL_REQUEUE : KILL_RESET);
    248 	}
    249 
    250 out:
    251 	/* Nothing more to do */
    252 	ata_channel_lock(chp);
    253 }
    254