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