1 /* $OpenBSD: ata.c,v 1.37 2024/05/26 10:01:01 jsg Exp $ */ 2 /* $NetBSD: ata.c,v 1.9 1999/04/15 09:41:09 bouyer Exp $ */ 3 /* 4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/pool.h> 30 31 #include <dev/ata/atareg.h> 32 #include <dev/ata/atavar.h> 33 #include <dev/ic/wdcreg.h> 34 35 #define DEBUG_FUNCS 0x08 36 #define DEBUG_PROBE 0x10 37 #ifdef WDCDEBUG 38 extern int wdcdebug_mask; /* init'ed in wdc.c */ 39 #define WDCDEBUG_PRINT(args, level) do { \ 40 if ((wdcdebug_mask & (level)) != 0) \ 41 printf args; \ 42 } while (0) 43 #else 44 #define WDCDEBUG_PRINT(args, level) 45 #endif 46 47 #define ATAPARAMS_SIZE 512 48 49 /* Get the disk's parameters */ 50 int 51 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags, 52 struct ataparams *prms) 53 { 54 char *tb; 55 struct wdc_command wdc_c; 56 int i; 57 u_int16_t *p; 58 int ret; 59 60 WDCDEBUG_PRINT(("ata_get_params\n"), DEBUG_FUNCS); 61 62 bzero(&wdc_c, sizeof(struct wdc_command)); 63 64 if (drvp->drive_flags & DRIVE_ATA) { 65 wdc_c.r_command = WDCC_IDENTIFY; 66 wdc_c.r_st_bmask = WDCS_DRDY; 67 wdc_c.r_st_pmask = 0; 68 wdc_c.timeout = 3000; /* 3s */ 69 } else if (drvp->drive_flags & DRIVE_ATAPI) { 70 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE; 71 wdc_c.r_st_bmask = 0; 72 wdc_c.r_st_pmask = 0; 73 wdc_c.timeout = 10000; /* 10s */ 74 } else { 75 WDCDEBUG_PRINT(("ata_get_params: no disks\n"), 76 DEBUG_FUNCS|DEBUG_PROBE); 77 return CMD_ERR; 78 } 79 80 tb = dma_alloc(ATAPARAMS_SIZE, PR_NOWAIT | PR_ZERO); 81 if (tb == NULL) 82 return CMD_AGAIN; 83 wdc_c.flags = AT_READ | flags; 84 wdc_c.data = tb; 85 wdc_c.bcount = ATAPARAMS_SIZE; 86 87 if ((ret = wdc_exec_command(drvp, &wdc_c)) != WDC_COMPLETE) { 88 WDCDEBUG_PRINT(("%s: wdc_exec_command failed: %d\n", 89 __func__, ret), DEBUG_PROBE); 90 dma_free(tb, ATAPARAMS_SIZE); 91 return CMD_AGAIN; 92 } 93 94 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 95 WDCDEBUG_PRINT(("%s: IDENTIFY failed: 0x%x\n", __func__, 96 wdc_c.flags), DEBUG_PROBE); 97 98 dma_free(tb, ATAPARAMS_SIZE); 99 return CMD_ERR; 100 } else { 101 #if BYTE_ORDER == BIG_ENDIAN 102 /* All the fields in the params structure are 16-bit 103 integers except for the ID strings which are char 104 strings. The 16-bit integers are currently in 105 memory in little-endian, regardless of architecture. 106 So, they need to be swapped on big-endian architectures 107 before they are accessed through the ataparams structure. 108 109 The swaps below avoid touching the char strings. 110 */ 111 112 swap16_multi((u_int16_t *)tb, 10); 113 swap16_multi((u_int16_t *)tb + 20, 3); 114 swap16_multi((u_int16_t *)tb + 47, ATAPARAMS_SIZE / 2 - 47); 115 #endif 116 /* Read in parameter block. */ 117 bcopy(tb, prms, sizeof(struct ataparams)); 118 119 /* 120 * Shuffle string byte order. 121 * ATAPI Mitsumi and NEC drives don't need this. 122 */ 123 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) == 124 WDC_CFG_ATAPI && 125 ((prms->atap_model[0] == 'N' && 126 prms->atap_model[1] == 'E') || 127 (prms->atap_model[0] == 'F' && 128 prms->atap_model[1] == 'X'))) { 129 dma_free(tb, ATAPARAMS_SIZE); 130 return CMD_OK; 131 } 132 for (i = 0; i < sizeof(prms->atap_model); i += 2) { 133 p = (u_short *)(prms->atap_model + i); 134 *p = swap16(*p); 135 } 136 for (i = 0; i < sizeof(prms->atap_serial); i += 2) { 137 p = (u_short *)(prms->atap_serial + i); 138 *p = swap16(*p); 139 } 140 for (i = 0; i < sizeof(prms->atap_revision); i += 2) { 141 p = (u_short *)(prms->atap_revision + i); 142 *p = swap16(*p); 143 } 144 145 dma_free(tb, ATAPARAMS_SIZE); 146 return CMD_OK; 147 } 148 } 149 150 int 151 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags) 152 { 153 struct wdc_command wdc_c; 154 155 WDCDEBUG_PRINT(("%s: mode=0x%x, flags=0x%x\n", __func__, 156 mode, flags), DEBUG_PROBE); 157 158 bzero(&wdc_c, sizeof(struct wdc_command)); 159 160 wdc_c.r_command = SET_FEATURES; 161 wdc_c.r_st_bmask = 0; 162 wdc_c.r_st_pmask = 0; 163 wdc_c.r_features = WDSF_SET_MODE; 164 wdc_c.r_count = mode; 165 wdc_c.flags = flags; 166 wdc_c.timeout = 1000; /* 1s */ 167 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) 168 return CMD_AGAIN; 169 170 WDCDEBUG_PRINT(("%s: after wdc_exec_command() wdc_c.flags=0x%x\n", 171 __func__, wdc_c.flags), DEBUG_PROBE); 172 173 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 174 return CMD_ERR; 175 } 176 return CMD_OK; 177 } 178 179 void 180 ata_dmaerr(struct ata_drive_datas *drvp) 181 { 182 /* 183 * Downgrade decision: if we get NERRS_MAX in NXFER. 184 * We start with n_dmaerrs set to NERRS_MAX-1 so that the 185 * first error within the first NXFER ops will immediately trigger 186 * a downgrade. 187 * If we got an error and n_xfers is bigger than NXFER reset counters. 188 */ 189 drvp->n_dmaerrs++; 190 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) { 191 wdc_downgrade_mode(drvp); 192 drvp->n_dmaerrs = NERRS_MAX-1; 193 drvp->n_xfers = 0; 194 return; 195 } 196 if (drvp->n_xfers > NXFER) { 197 drvp->n_dmaerrs = 1; /* just got an error */ 198 drvp->n_xfers = 1; /* restart counting from this error */ 199 } 200 } 201 202 void 203 ata_perror(struct ata_drive_datas *drvp, int errno, char *buf, size_t buf_len) 204 { 205 static char *errstr0_3[] = {"address mark not found", 206 "track 0 not found", "aborted command", "media change requested", 207 "id not found", "media changed", "uncorrectable data error", 208 "bad block detected"}; 209 static char *errstr4_5[] = {"", 210 "no media/write protected", "aborted command", 211 "media change requested", "id not found", "media changed", 212 "uncorrectable data error", "interface CRC error"}; 213 char **errstr; 214 int i; 215 char *sep = ""; 216 size_t len = 0; 217 218 if (drvp->ata_vers >= 4) 219 errstr = errstr4_5; 220 else 221 errstr = errstr0_3; 222 223 if (errno == 0) { 224 snprintf(buf, buf_len, "error not notified"); 225 } 226 227 for (i = 0; i < 8; i++) { 228 if (errno & (1 << i)) { 229 snprintf(buf + len, buf_len - len, "%s%s", sep, 230 errstr[i]); 231 len = strlen(buf); 232 sep = ", "; 233 } 234 } 235 } 236