1 /* $NetBSD: ata.c,v 1.17 2002/04/09 21:17:53 bouyer Exp $ */ 2 /* 3 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Manuel Bouyer. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.17 2002/04/09 21:17:53 bouyer Exp $"); 33 34 #ifndef WDCDEBUG 35 #define WDCDEBUG 36 #endif /* WDCDEBUG */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/file.h> 42 #include <sys/stat.h> 43 #include <sys/malloc.h> 44 #include <sys/device.h> 45 #include <sys/syslog.h> 46 47 #include <machine/intr.h> 48 #include <machine/bus.h> 49 50 #include <dev/ata/atareg.h> 51 #include <dev/ata/atavar.h> 52 #include <dev/ic/wdcreg.h> 53 #include <dev/ic/wdcvar.h> 54 55 #define DEBUG_FUNCS 0x08 56 #define DEBUG_PROBE 0x10 57 #ifdef WDCDEBUG 58 extern int wdcdebug_mask; /* init'ed in wdc.c */ 59 #define WDCDEBUG_PRINT(args, level) \ 60 if (wdcdebug_mask & (level)) \ 61 printf args 62 #else 63 #define WDCDEBUG_PRINT(args, level) 64 #endif 65 66 /* Get the disk's parameters */ 67 int 68 ata_get_params(drvp, flags, prms) 69 struct ata_drive_datas *drvp; 70 u_int8_t flags; 71 struct ataparams *prms; 72 { 73 char tb[DEV_BSIZE]; 74 struct wdc_command wdc_c; 75 76 #if BYTE_ORDER == LITTLE_ENDIAN 77 int i; 78 u_int16_t *p; 79 #endif 80 81 WDCDEBUG_PRINT(("wdc_ata_get_parms\n"), DEBUG_FUNCS); 82 83 memset(tb, 0, DEV_BSIZE); 84 memset(prms, 0, sizeof(struct ataparams)); 85 memset(&wdc_c, 0, sizeof(struct wdc_command)); 86 87 if (drvp->drive_flags & DRIVE_ATA) { 88 wdc_c.r_command = WDCC_IDENTIFY; 89 wdc_c.r_st_bmask = WDCS_DRDY; 90 wdc_c.r_st_pmask = WDCS_DRQ; 91 wdc_c.timeout = 1000; /* 1s */ 92 } else if (drvp->drive_flags & DRIVE_ATAPI) { 93 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE; 94 wdc_c.r_st_bmask = 0; 95 wdc_c.r_st_pmask = WDCS_DRQ; 96 wdc_c.timeout = 10000; /* 10s */ 97 } else { 98 WDCDEBUG_PRINT(("wdc_ata_get_parms: no disks\n"), 99 DEBUG_FUNCS|DEBUG_PROBE); 100 return CMD_ERR; 101 } 102 wdc_c.flags = AT_READ | flags; 103 wdc_c.data = tb; 104 wdc_c.bcount = DEV_BSIZE; 105 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) { 106 WDCDEBUG_PRINT(("wdc_ata_get_parms: wdc_exec_command failed\n"), 107 DEBUG_FUNCS|DEBUG_PROBE); 108 return CMD_AGAIN; 109 } 110 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 111 WDCDEBUG_PRINT(("wdc_ata_get_parms: wdc_c.flags=0x%x\n", 112 wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE); 113 return CMD_ERR; 114 } else { 115 /* if we didn't read any data something is wrong */ 116 if ((wdc_c.flags & AT_XFDONE) == 0) 117 return CMD_ERR; 118 /* Read in parameter block. */ 119 memcpy(prms, tb, sizeof(struct ataparams)); 120 #if BYTE_ORDER == LITTLE_ENDIAN 121 /* 122 * Shuffle string byte order. 123 * ATAPI Mitsumi and NEC drives don't need this. 124 */ 125 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) == 126 WDC_CFG_ATAPI && 127 ((prms->atap_model[0] == 'N' && 128 prms->atap_model[1] == 'E') || 129 (prms->atap_model[0] == 'F' && 130 prms->atap_model[1] == 'X'))) 131 return 0; 132 for (i = 0; i < sizeof(prms->atap_model); i += 2) { 133 p = (u_short *)(prms->atap_model + i); 134 *p = ntohs(*p); 135 } 136 for (i = 0; i < sizeof(prms->atap_serial); i += 2) { 137 p = (u_short *)(prms->atap_serial + i); 138 *p = ntohs(*p); 139 } 140 for (i = 0; i < sizeof(prms->atap_revision); i += 2) { 141 p = (u_short *)(prms->atap_revision + i); 142 *p = ntohs(*p); 143 } 144 #endif 145 return CMD_OK; 146 } 147 } 148 149 int 150 ata_set_mode(drvp, mode, flags) 151 struct ata_drive_datas *drvp; 152 u_int8_t mode; 153 u_int8_t flags; 154 { 155 struct wdc_command wdc_c; 156 157 WDCDEBUG_PRINT(("wdc_ata_set_mode=0x%x\n", mode), DEBUG_FUNCS); 158 memset(&wdc_c, 0, 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_precomp = WDSF_SET_MODE; 164 wdc_c.r_count = mode; 165 wdc_c.flags = AT_READ | flags; 166 wdc_c.timeout = 1000; /* 1s */ 167 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) 168 return CMD_AGAIN; 169 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { 170 return CMD_ERR; 171 } 172 return CMD_OK; 173 } 174 175 void 176 ata_dmaerr(drvp) 177 struct ata_drive_datas *drvp; 178 { 179 /* 180 * Downgrade decision: if we get NERRS_MAX in NXFER. 181 * We start with n_dmaerrs set to NERRS_MAX-1 so that the 182 * first error within the first NXFER ops will immediatly trigger 183 * a downgrade. 184 * If we got an error and n_xfers is bigger than NXFER reset counters. 185 */ 186 drvp->n_dmaerrs++; 187 if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) { 188 wdc_downgrade_mode(drvp); 189 drvp->n_dmaerrs = NERRS_MAX-1; 190 drvp->n_xfers = 0; 191 return; 192 } 193 if (drvp->n_xfers > NXFER) { 194 drvp->n_dmaerrs = 1; /* just got an error */ 195 drvp->n_xfers = 1; /* restart counting from this error */ 196 } 197 } 198