| 1 | /* $NetBSD: wd.c,v 1.427 2016/11/20 02:35:19 pgoyette Exp $ */ |
| 2 | |
| 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 | /*- |
| 28 | * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. |
| 29 | * All rights reserved. |
| 30 | * |
| 31 | * This code is derived from software contributed to The NetBSD Foundation |
| 32 | * by Charles M. Hannum and by Onno van der Linden. |
| 33 | * |
| 34 | * Redistribution and use in source and binary forms, with or without |
| 35 | * modification, are permitted provided that the following conditions |
| 36 | * are met: |
| 37 | * 1. Redistributions of source code must retain the above copyright |
| 38 | * notice, this list of conditions and the following disclaimer. |
| 39 | * 2. Redistributions in binary form must reproduce the above copyright |
| 40 | * notice, this list of conditions and the following disclaimer in the |
| 41 | * documentation and/or other materials provided with the distribution. |
| 42 | * |
| 43 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 44 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 45 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 46 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 47 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 48 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 49 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 50 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 51 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 52 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 53 | * POSSIBILITY OF SUCH DAMAGE. |
| 54 | */ |
| 55 | |
| 56 | #include <sys/cdefs.h> |
| 57 | __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.427 2016/11/20 02:35:19 pgoyette Exp $" ); |
| 58 | |
| 59 | #include "opt_ata.h" |
| 60 | |
| 61 | #include <sys/param.h> |
| 62 | #include <sys/systm.h> |
| 63 | #include <sys/kernel.h> |
| 64 | #include <sys/conf.h> |
| 65 | #include <sys/file.h> |
| 66 | #include <sys/stat.h> |
| 67 | #include <sys/ioctl.h> |
| 68 | #include <sys/buf.h> |
| 69 | #include <sys/bufq.h> |
| 70 | #include <sys/uio.h> |
| 71 | #include <sys/malloc.h> |
| 72 | #include <sys/device.h> |
| 73 | #include <sys/disklabel.h> |
| 74 | #include <sys/disk.h> |
| 75 | #include <sys/syslog.h> |
| 76 | #include <sys/proc.h> |
| 77 | #include <sys/reboot.h> |
| 78 | #include <sys/vnode.h> |
| 79 | #include <sys/rndsource.h> |
| 80 | |
| 81 | #include <sys/intr.h> |
| 82 | #include <sys/bus.h> |
| 83 | |
| 84 | #include <dev/ata/atareg.h> |
| 85 | #include <dev/ata/atavar.h> |
| 86 | #include <dev/ata/wdvar.h> |
| 87 | #include <dev/ic/wdcreg.h> |
| 88 | #include <sys/ataio.h> |
| 89 | #include "locators.h" |
| 90 | |
| 91 | #include <prop/proplib.h> |
| 92 | |
| 93 | #define WDIORETRIES_SINGLE 4 /* number of retries before single-sector */ |
| 94 | #define WDIORETRIES 5 /* number of retries before giving up */ |
| 95 | #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */ |
| 96 | |
| 97 | #define WDUNIT(dev) DISKUNIT(dev) |
| 98 | #define WDPART(dev) DISKPART(dev) |
| 99 | #define WDMINOR(unit, part) DISKMINOR(unit, part) |
| 100 | #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part) |
| 101 | |
| 102 | #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART)) |
| 103 | |
| 104 | #define DEBUG_INTR 0x01 |
| 105 | #define DEBUG_XFERS 0x02 |
| 106 | #define DEBUG_STATUS 0x04 |
| 107 | #define DEBUG_FUNCS 0x08 |
| 108 | #define DEBUG_PROBE 0x10 |
| 109 | #ifdef ATADEBUG |
| 110 | int wdcdebug_wd_mask = 0x0; |
| 111 | #define ATADEBUG_PRINT(args, level) \ |
| 112 | if (wdcdebug_wd_mask & (level)) \ |
| 113 | printf args |
| 114 | #else |
| 115 | #define ATADEBUG_PRINT(args, level) |
| 116 | #endif |
| 117 | |
| 118 | int wdprobe(device_t, cfdata_t, void *); |
| 119 | void wdattach(device_t, device_t, void *); |
| 120 | int wddetach(device_t, int); |
| 121 | int wdprint(void *, char *); |
| 122 | void wdperror(const struct wd_softc *); |
| 123 | |
| 124 | static void wdminphys(struct buf *); |
| 125 | |
| 126 | static int wdlastclose(device_t); |
| 127 | static bool wd_suspend(device_t, const pmf_qual_t *); |
| 128 | static int wd_standby(struct wd_softc *, int); |
| 129 | |
| 130 | CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc), |
| 131 | wdprobe, wdattach, wddetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); |
| 132 | |
| 133 | extern struct cfdriver wd_cd; |
| 134 | |
| 135 | dev_type_open(wdopen); |
| 136 | dev_type_close(wdclose); |
| 137 | dev_type_read(wdread); |
| 138 | dev_type_write(wdwrite); |
| 139 | dev_type_ioctl(wdioctl); |
| 140 | dev_type_strategy(wdstrategy); |
| 141 | dev_type_dump(wddump); |
| 142 | dev_type_size(wdsize); |
| 143 | static dev_type_discard(wddiscard); |
| 144 | |
| 145 | const struct bdevsw wd_bdevsw = { |
| 146 | .d_open = wdopen, |
| 147 | .d_close = wdclose, |
| 148 | .d_strategy = wdstrategy, |
| 149 | .d_ioctl = wdioctl, |
| 150 | .d_dump = wddump, |
| 151 | .d_psize = wdsize, |
| 152 | .d_discard = wddiscard, |
| 153 | .d_flag = D_DISK |
| 154 | }; |
| 155 | |
| 156 | const struct cdevsw wd_cdevsw = { |
| 157 | .d_open = wdopen, |
| 158 | .d_close = wdclose, |
| 159 | .d_read = wdread, |
| 160 | .d_write = wdwrite, |
| 161 | .d_ioctl = wdioctl, |
| 162 | .d_stop = nostop, |
| 163 | .d_tty = notty, |
| 164 | .d_poll = nopoll, |
| 165 | .d_mmap = nommap, |
| 166 | .d_kqfilter = nokqfilter, |
| 167 | .d_discard = wddiscard, |
| 168 | .d_flag = D_DISK |
| 169 | }; |
| 170 | |
| 171 | /* |
| 172 | * Glue necessary to hook WDCIOCCOMMAND into physio |
| 173 | */ |
| 174 | |
| 175 | struct wd_ioctl { |
| 176 | LIST_ENTRY(wd_ioctl) wi_list; |
| 177 | struct buf wi_bp; |
| 178 | struct uio wi_uio; |
| 179 | struct iovec wi_iov; |
| 180 | atareq_t wi_atareq; |
| 181 | struct wd_softc *wi_softc; |
| 182 | }; |
| 183 | |
| 184 | LIST_HEAD(, wd_ioctl) wi_head; |
| 185 | |
| 186 | struct wd_ioctl *wi_find(struct buf *); |
| 187 | void wi_free(struct wd_ioctl *); |
| 188 | struct wd_ioctl *wi_get(void); |
| 189 | void wdioctlstrategy(struct buf *); |
| 190 | |
| 191 | void wdgetdefaultlabel(struct wd_softc *, struct disklabel *); |
| 192 | void wdgetdisklabel(struct wd_softc *); |
| 193 | void wdstart(void *); |
| 194 | void wdstart1(struct wd_softc*, struct buf *); |
| 195 | void wdrestart(void *); |
| 196 | void wddone(void *); |
| 197 | static void wd_params_to_properties(struct wd_softc *); |
| 198 | int wd_get_params(struct wd_softc *, uint8_t, struct ataparams *); |
| 199 | int wd_flushcache(struct wd_softc *, int); |
| 200 | int wd_trim(struct wd_softc *, int, daddr_t, long); |
| 201 | bool wd_shutdown(device_t, int); |
| 202 | |
| 203 | int wd_getcache(struct wd_softc *, int *); |
| 204 | int wd_setcache(struct wd_softc *, int); |
| 205 | |
| 206 | struct dkdriver wddkdriver = { |
| 207 | .d_strategy = wdstrategy, |
| 208 | .d_minphys = wdminphys |
| 209 | }; |
| 210 | |
| 211 | #ifdef HAS_BAD144_HANDLING |
| 212 | static void bad144intern(struct wd_softc *); |
| 213 | #endif |
| 214 | |
| 215 | #define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */ |
| 216 | |
| 217 | #define WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE\2FORCE_LBA48" |
| 218 | |
| 219 | /* |
| 220 | * Quirk table for IDE drives. Put more-specific matches first, since |
| 221 | * a simple globing routine is used for matching. |
| 222 | */ |
| 223 | static const struct wd_quirk { |
| 224 | const char *wdq_match; /* inquiry pattern to match */ |
| 225 | int wdq_quirks; /* drive quirks */ |
| 226 | } wd_quirk_table[] = { |
| 227 | /* |
| 228 | * Some Seagate S-ATA drives have a PHY which can get confused |
| 229 | * with the way data is packetized by some S-ATA controllers. |
| 230 | * |
| 231 | * The work-around is to split in two any write transfer whose |
| 232 | * sector count % 15 == 1 (assuming 512 byte sectors). |
| 233 | * |
| 234 | * XXX This is an incomplete list. There are at least a couple |
| 235 | * XXX more model numbers. If you have trouble with such transfers |
| 236 | * XXX (8K is the most common) on Seagate S-ATA drives, please |
| 237 | * XXX notify thorpej@NetBSD.org. |
| 238 | * |
| 239 | * The ST360015AS has not yet been confirmed to have this |
| 240 | * issue, however, it is the only other drive in the |
| 241 | * Seagate Barracuda Serial ATA V family. |
| 242 | * |
| 243 | */ |
| 244 | { "ST3120023AS" , |
| 245 | WD_QUIRK_SPLIT_MOD15_WRITE }, |
| 246 | { "ST380023AS" , |
| 247 | WD_QUIRK_SPLIT_MOD15_WRITE }, |
| 248 | { "ST360015AS" , |
| 249 | WD_QUIRK_SPLIT_MOD15_WRITE }, |
| 250 | { NULL, |
| 251 | 0 } |
| 252 | }; |
| 253 | |
| 254 | static const struct wd_quirk * |
| 255 | wd_lookup_quirks(const char *name) |
| 256 | { |
| 257 | const struct wd_quirk *wdq; |
| 258 | const char *estr; |
| 259 | |
| 260 | for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) { |
| 261 | /* |
| 262 | * We only want exact matches (which include matches |
| 263 | * against globbing characters). |
| 264 | */ |
| 265 | if (pmatch(name, wdq->wdq_match, &estr) == 2) |
| 266 | return (wdq); |
| 267 | } |
| 268 | return (NULL); |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | wdprobe(device_t parent, cfdata_t match, void *aux) |
| 273 | { |
| 274 | struct ata_device *adev = aux; |
| 275 | |
| 276 | if (adev == NULL) |
| 277 | return 0; |
| 278 | if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA) |
| 279 | return 0; |
| 280 | |
| 281 | if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT && |
| 282 | match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive) |
| 283 | return 0; |
| 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | wdattach(device_t parent, device_t self, void *aux) |
| 289 | { |
| 290 | struct wd_softc *wd = device_private(self); |
| 291 | struct ata_device *adev= aux; |
| 292 | int i, blank; |
| 293 | char tbuf[41], pbuf[9], c, *p, *q; |
| 294 | const struct wd_quirk *wdq; |
| 295 | |
| 296 | wd->sc_dev = self; |
| 297 | |
| 298 | ATADEBUG_PRINT(("wdattach\n" ), DEBUG_FUNCS | DEBUG_PROBE); |
| 299 | callout_init(&wd->sc_restart_ch, 0); |
| 300 | bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK); |
| 301 | #ifdef WD_SOFTBADSECT |
| 302 | SLIST_INIT(&wd->sc_bslist); |
| 303 | #endif |
| 304 | wd->atabus = adev->adev_bustype; |
| 305 | wd->openings = adev->adev_openings; |
| 306 | wd->drvp = adev->adev_drv_data; |
| 307 | |
| 308 | wd->drvp->drv_done = wddone; |
| 309 | wd->drvp->drv_softc = wd->sc_dev; /* done in atabusconfig_thread() |
| 310 | but too late */ |
| 311 | |
| 312 | aprint_naive("\n" ); |
| 313 | aprint_normal("\n" ); |
| 314 | |
| 315 | /* read our drive info */ |
| 316 | if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) { |
| 317 | aprint_error_dev(self, "IDENTIFY failed\n" ); |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0; |
| 322 | i < sizeof(wd->sc_params.atap_model); i++) { |
| 323 | c = *p++; |
| 324 | if (c == '\0') |
| 325 | break; |
| 326 | if (c != ' ') { |
| 327 | if (blank) { |
| 328 | *q++ = ' '; |
| 329 | blank = 0; |
| 330 | } |
| 331 | *q++ = c; |
| 332 | } else |
| 333 | blank = 1; |
| 334 | } |
| 335 | *q++ = '\0'; |
| 336 | |
| 337 | aprint_normal_dev(self, "<%s>\n" , tbuf); |
| 338 | |
| 339 | wdq = wd_lookup_quirks(tbuf); |
| 340 | if (wdq != NULL) |
| 341 | wd->sc_quirks = wdq->wdq_quirks; |
| 342 | |
| 343 | if (wd->sc_quirks != 0) { |
| 344 | char sbuf[sizeof(WD_QUIRK_FMT) + 64]; |
| 345 | snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks); |
| 346 | aprint_normal_dev(self, "quirks %s\n" , sbuf); |
| 347 | } |
| 348 | |
| 349 | if ((wd->sc_params.atap_multi & 0xff) > 1) { |
| 350 | wd->sc_multi = wd->sc_params.atap_multi & 0xff; |
| 351 | } else { |
| 352 | wd->sc_multi = 1; |
| 353 | } |
| 354 | |
| 355 | aprint_verbose_dev(self, "drive supports %d-sector PIO transfers," , |
| 356 | wd->sc_multi); |
| 357 | |
| 358 | /* 48-bit LBA addressing */ |
| 359 | if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) |
| 360 | wd->sc_flags |= WDF_LBA48; |
| 361 | |
| 362 | /* Prior to ATA-4, LBA was optional. */ |
| 363 | if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) |
| 364 | wd->sc_flags |= WDF_LBA; |
| 365 | #if 0 |
| 366 | /* ATA-4 requires LBA. */ |
| 367 | if (wd->sc_params.atap_ataversion != 0xffff && |
| 368 | wd->sc_params.atap_ataversion >= WDC_VER_ATA4) |
| 369 | wd->sc_flags |= WDF_LBA; |
| 370 | #endif |
| 371 | |
| 372 | if ((wd->sc_flags & WDF_LBA48) != 0) { |
| 373 | aprint_verbose(" LBA48 addressing\n" ); |
| 374 | wd->sc_capacity = |
| 375 | ((uint64_t) wd->sc_params.atap_max_lba[3] << 48) | |
| 376 | ((uint64_t) wd->sc_params.atap_max_lba[2] << 32) | |
| 377 | ((uint64_t) wd->sc_params.atap_max_lba[1] << 16) | |
| 378 | ((uint64_t) wd->sc_params.atap_max_lba[0] << 0); |
| 379 | wd->sc_capacity28 = |
| 380 | (wd->sc_params.atap_capacity[1] << 16) | |
| 381 | wd->sc_params.atap_capacity[0]; |
| 382 | } else if ((wd->sc_flags & WDF_LBA) != 0) { |
| 383 | aprint_verbose(" LBA addressing\n" ); |
| 384 | wd->sc_capacity28 = wd->sc_capacity = |
| 385 | (wd->sc_params.atap_capacity[1] << 16) | |
| 386 | wd->sc_params.atap_capacity[0]; |
| 387 | } else { |
| 388 | aprint_verbose(" chs addressing\n" ); |
| 389 | wd->sc_capacity28 = wd->sc_capacity = |
| 390 | wd->sc_params.atap_cylinders * |
| 391 | wd->sc_params.atap_heads * |
| 392 | wd->sc_params.atap_sectors; |
| 393 | } |
| 394 | if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID |
| 395 | && ((wd->sc_params.atap_secsz & ATA_SECSZ_LLS) != 0)) { |
| 396 | wd->sc_blksize = 2ULL * |
| 397 | ((uint32_t)((wd->sc_params.atap_lls_secsz[1] << 16) | |
| 398 | wd->sc_params.atap_lls_secsz[0])); |
| 399 | } else { |
| 400 | wd->sc_blksize = 512; |
| 401 | } |
| 402 | wd->sc_capacity512 = (wd->sc_capacity * wd->sc_blksize) / DEV_BSIZE; |
| 403 | format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * wd->sc_blksize); |
| 404 | aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, " |
| 405 | "%d bytes/sect x %llu sectors\n" , |
| 406 | pbuf, |
| 407 | (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity / |
| 408 | (wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) : |
| 409 | wd->sc_params.atap_cylinders, |
| 410 | wd->sc_params.atap_heads, wd->sc_params.atap_sectors, |
| 411 | wd->sc_blksize, (unsigned long long)wd->sc_capacity); |
| 412 | |
| 413 | ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n" , |
| 414 | device_xname(self), wd->sc_params.atap_dmatiming_mimi, |
| 415 | wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE); |
| 416 | |
| 417 | if (wd->sc_blksize <= 0 || !powerof2(wd->sc_blksize) || |
| 418 | wd->sc_blksize < DEV_BSIZE || wd->sc_blksize > MAXPHYS) { |
| 419 | aprint_normal_dev(self, "WARNING: block size %u " |
| 420 | "might not actually work\n" , wd->sc_blksize); |
| 421 | } |
| 422 | out: |
| 423 | /* |
| 424 | * Initialize and attach the disk structure. |
| 425 | */ |
| 426 | /* we fill in dk_info later */ |
| 427 | disk_init(&wd->sc_dk, device_xname(wd->sc_dev), &wddkdriver); |
| 428 | disk_attach(&wd->sc_dk); |
| 429 | wd->sc_wdc_bio.lp = wd->sc_dk.dk_label; |
| 430 | wd_params_to_properties(wd); |
| 431 | rnd_attach_source(&wd->rnd_source, device_xname(wd->sc_dev), |
| 432 | RND_TYPE_DISK, RND_FLAG_DEFAULT); |
| 433 | |
| 434 | /* Discover wedges on this disk. */ |
| 435 | dkwedge_discover(&wd->sc_dk); |
| 436 | |
| 437 | if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown)) |
| 438 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 439 | } |
| 440 | |
| 441 | static bool |
| 442 | wd_suspend(device_t dv, const pmf_qual_t *qual) |
| 443 | { |
| 444 | struct wd_softc *sc = device_private(dv); |
| 445 | |
| 446 | /* the adapter needs to be enabled */ |
| 447 | if (sc->atabus->ata_addref(sc->drvp)) |
| 448 | return true; /* no need to complain */ |
| 449 | |
| 450 | wd_flushcache(sc, AT_WAIT); |
| 451 | wd_standby(sc, AT_WAIT); |
| 452 | |
| 453 | sc->atabus->ata_delref(sc->drvp); |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | int |
| 458 | wddetach(device_t self, int flags) |
| 459 | { |
| 460 | struct wd_softc *sc = device_private(self); |
| 461 | int bmaj, cmaj, i, mn, rc, s; |
| 462 | |
| 463 | if ((rc = disk_begindetach(&sc->sc_dk, wdlastclose, self, flags)) != 0) |
| 464 | return rc; |
| 465 | |
| 466 | /* locate the major number */ |
| 467 | bmaj = bdevsw_lookup_major(&wd_bdevsw); |
| 468 | cmaj = cdevsw_lookup_major(&wd_cdevsw); |
| 469 | |
| 470 | /* Nuke the vnodes for any open instances. */ |
| 471 | for (i = 0; i < MAXPARTITIONS; i++) { |
| 472 | mn = WDMINOR(device_unit(self), i); |
| 473 | vdevgone(bmaj, mn, mn, VBLK); |
| 474 | vdevgone(cmaj, mn, mn, VCHR); |
| 475 | } |
| 476 | |
| 477 | /* Delete all of our wedges. */ |
| 478 | dkwedge_delall(&sc->sc_dk); |
| 479 | |
| 480 | s = splbio(); |
| 481 | |
| 482 | /* Kill off any queued buffers. */ |
| 483 | bufq_drain(sc->sc_q); |
| 484 | |
| 485 | sc->atabus->ata_killpending(sc->drvp); |
| 486 | if (flags & DETACH_POWEROFF) |
| 487 | wd_standby(sc, AT_POLL); |
| 488 | |
| 489 | splx(s); |
| 490 | bufq_free(sc->sc_q); |
| 491 | |
| 492 | /* Detach disk. */ |
| 493 | disk_detach(&sc->sc_dk); |
| 494 | disk_destroy(&sc->sc_dk); |
| 495 | |
| 496 | #ifdef WD_SOFTBADSECT |
| 497 | /* Clean out the bad sector list */ |
| 498 | while (!SLIST_EMPTY(&sc->sc_bslist)) { |
| 499 | void *head = SLIST_FIRST(&sc->sc_bslist); |
| 500 | SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next); |
| 501 | free(head, M_TEMP); |
| 502 | } |
| 503 | sc->sc_bscount = 0; |
| 504 | #endif |
| 505 | |
| 506 | pmf_device_deregister(self); |
| 507 | |
| 508 | /* Unhook the entropy source. */ |
| 509 | rnd_detach_source(&sc->rnd_source); |
| 510 | |
| 511 | callout_destroy(&sc->sc_restart_ch); |
| 512 | |
| 513 | sc->drvp->drive_type = ATA_DRIVET_NONE; /* no drive any more here */ |
| 514 | sc->drvp->drive_flags = 0; |
| 515 | |
| 516 | return (0); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Read/write routine for a buffer. Validates the arguments and schedules the |
| 521 | * transfer. Does not wait for the transfer to complete. |
| 522 | */ |
| 523 | void |
| 524 | wdstrategy(struct buf *bp) |
| 525 | { |
| 526 | struct wd_softc *wd = |
| 527 | device_lookup_private(&wd_cd, WDUNIT(bp->b_dev)); |
| 528 | struct disklabel *lp = wd->sc_dk.dk_label; |
| 529 | daddr_t blkno; |
| 530 | int s; |
| 531 | |
| 532 | ATADEBUG_PRINT(("wdstrategy (%s)\n" , device_xname(wd->sc_dev)), |
| 533 | DEBUG_XFERS); |
| 534 | |
| 535 | /* Valid request? */ |
| 536 | if (bp->b_blkno < 0 || |
| 537 | (bp->b_bcount % lp->d_secsize) != 0 || |
| 538 | (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) { |
| 539 | bp->b_error = EINVAL; |
| 540 | goto done; |
| 541 | } |
| 542 | |
| 543 | /* If device invalidated (e.g. media change, door open, |
| 544 | * device detachment), then error. |
| 545 | */ |
| 546 | if ((wd->sc_flags & WDF_LOADED) == 0 || |
| 547 | !device_is_enabled(wd->sc_dev)) { |
| 548 | bp->b_error = EIO; |
| 549 | goto done; |
| 550 | } |
| 551 | |
| 552 | /* If it's a null transfer, return immediately. */ |
| 553 | if (bp->b_bcount == 0) |
| 554 | goto done; |
| 555 | |
| 556 | /* |
| 557 | * Do bounds checking, adjust transfer. if error, process. |
| 558 | * If end of partition, just return. |
| 559 | */ |
| 560 | if (WDPART(bp->b_dev) == RAW_PART) { |
| 561 | if (bounds_check_with_mediasize(bp, DEV_BSIZE, |
| 562 | wd->sc_capacity512) <= 0) |
| 563 | goto done; |
| 564 | } else { |
| 565 | if (bounds_check_with_label(&wd->sc_dk, bp, |
| 566 | (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0) |
| 567 | goto done; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Now convert the block number to absolute and put it in |
| 572 | * terms of the device's logical block size. |
| 573 | */ |
| 574 | if (lp->d_secsize >= DEV_BSIZE) |
| 575 | blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE); |
| 576 | else |
| 577 | blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize); |
| 578 | |
| 579 | if (WDPART(bp->b_dev) != RAW_PART) |
| 580 | blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset; |
| 581 | |
| 582 | bp->b_rawblkno = blkno; |
| 583 | |
| 584 | #ifdef WD_SOFTBADSECT |
| 585 | /* |
| 586 | * If the transfer about to be attempted contains only a block that |
| 587 | * is known to be bad then return an error for the transfer without |
| 588 | * even attempting to start a transfer up under the premis that we |
| 589 | * will just end up doing more retries for a transfer that will end |
| 590 | * up failing again. |
| 591 | * XXX:SMP - mutex required to protect with DIOCBSFLUSH |
| 592 | */ |
| 593 | if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) { |
| 594 | struct disk_badsectors *dbs; |
| 595 | daddr_t maxblk = blkno + (bp->b_bcount / wd->sc_blksize) - 1; |
| 596 | |
| 597 | SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) |
| 598 | if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) || |
| 599 | (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){ |
| 600 | bp->b_error = EIO; |
| 601 | goto done; |
| 602 | } |
| 603 | } |
| 604 | #endif |
| 605 | |
| 606 | /* Queue transfer on drive, activate drive and controller if idle. */ |
| 607 | s = splbio(); |
| 608 | bufq_put(wd->sc_q, bp); |
| 609 | wdstart(wd); |
| 610 | splx(s); |
| 611 | return; |
| 612 | done: |
| 613 | /* Toss transfer; we're done early. */ |
| 614 | bp->b_resid = bp->b_bcount; |
| 615 | biodone(bp); |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Queue a drive for I/O. |
| 620 | */ |
| 621 | void |
| 622 | wdstart(void *arg) |
| 623 | { |
| 624 | struct wd_softc *wd = arg; |
| 625 | struct buf *bp = NULL; |
| 626 | |
| 627 | ATADEBUG_PRINT(("wdstart %s\n" , device_xname(wd->sc_dev)), |
| 628 | DEBUG_XFERS); |
| 629 | |
| 630 | if (!device_is_active(wd->sc_dev)) |
| 631 | return; |
| 632 | |
| 633 | while (wd->openings > 0) { |
| 634 | |
| 635 | /* Is there a buf for us ? */ |
| 636 | if ((bp = bufq_get(wd->sc_q)) == NULL) |
| 637 | return; |
| 638 | |
| 639 | /* |
| 640 | * Make the command. First lock the device |
| 641 | */ |
| 642 | wd->openings--; |
| 643 | |
| 644 | wd->retries = 0; |
| 645 | wdstart1(wd, bp); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static void |
| 650 | wd_split_mod15_write(struct buf *bp) |
| 651 | { |
| 652 | struct buf *obp = bp->b_private; |
| 653 | struct wd_softc *sc = |
| 654 | device_lookup_private(&wd_cd, DISKUNIT(obp->b_dev)); |
| 655 | int s; |
| 656 | |
| 657 | if (__predict_false(bp->b_error != 0)) { |
| 658 | /* |
| 659 | * Propagate the error. If this was the first half of |
| 660 | * the original transfer, make sure to account for that |
| 661 | * in the residual. |
| 662 | */ |
| 663 | if (bp->b_data == obp->b_data) |
| 664 | bp->b_resid += bp->b_bcount; |
| 665 | goto done; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * If this was the second half of the transfer, we're all done! |
| 670 | */ |
| 671 | if (bp->b_data != obp->b_data) |
| 672 | goto done; |
| 673 | |
| 674 | /* |
| 675 | * Advance the pointer to the second half and issue that command |
| 676 | * using the same opening. |
| 677 | */ |
| 678 | bp->b_flags = obp->b_flags; |
| 679 | bp->b_oflags = obp->b_oflags; |
| 680 | bp->b_cflags = obp->b_cflags; |
| 681 | bp->b_data = (char *)bp->b_data + bp->b_bcount; |
| 682 | bp->b_blkno += (bp->b_bcount / DEV_BSIZE); |
| 683 | bp->b_rawblkno += (bp->b_bcount / sc->sc_blksize); |
| 684 | s = splbio(); |
| 685 | wdstart1(sc, bp); |
| 686 | splx(s); |
| 687 | return; |
| 688 | |
| 689 | done: |
| 690 | obp->b_error = bp->b_error; |
| 691 | obp->b_resid = bp->b_resid; |
| 692 | s = splbio(); |
| 693 | putiobuf(bp); |
| 694 | biodone(obp); |
| 695 | sc->openings++; |
| 696 | splx(s); |
| 697 | /* wddone() will call wdstart() */ |
| 698 | } |
| 699 | |
| 700 | void |
| 701 | wdstart1(struct wd_softc *wd, struct buf *bp) |
| 702 | { |
| 703 | |
| 704 | /* |
| 705 | * Deal with the "split mod15 write" quirk. We just divide the |
| 706 | * transfer in two, doing the first half and then then second half |
| 707 | * with the same command opening. |
| 708 | * |
| 709 | * Note we MUST do this here, because we can't let insertion |
| 710 | * into the bufq cause the transfers to be re-merged. |
| 711 | */ |
| 712 | if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 && |
| 713 | (bp->b_flags & B_READ) == 0 && |
| 714 | bp->b_bcount > 512 && |
| 715 | ((bp->b_bcount / 512) % 15) == 1)) { |
| 716 | struct buf *nbp; |
| 717 | |
| 718 | /* already at splbio */ |
| 719 | nbp = getiobuf(NULL, false); |
| 720 | if (__predict_false(nbp == NULL)) { |
| 721 | /* No memory -- fail the iop. */ |
| 722 | bp->b_error = ENOMEM; |
| 723 | bp->b_resid = bp->b_bcount; |
| 724 | biodone(bp); |
| 725 | wd->openings++; |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | nbp->b_error = 0; |
| 730 | nbp->b_proc = bp->b_proc; |
| 731 | nbp->b_dev = bp->b_dev; |
| 732 | |
| 733 | nbp->b_bcount = bp->b_bcount / 2; |
| 734 | nbp->b_bufsize = bp->b_bcount / 2; |
| 735 | nbp->b_data = bp->b_data; |
| 736 | |
| 737 | nbp->b_blkno = bp->b_blkno; |
| 738 | nbp->b_rawblkno = bp->b_rawblkno; |
| 739 | |
| 740 | nbp->b_flags = bp->b_flags; |
| 741 | nbp->b_oflags = bp->b_oflags; |
| 742 | nbp->b_cflags = bp->b_cflags; |
| 743 | nbp->b_iodone = wd_split_mod15_write; |
| 744 | |
| 745 | /* Put ptr to orig buf in b_private and use new buf */ |
| 746 | nbp->b_private = bp; |
| 747 | |
| 748 | BIO_COPYPRIO(nbp, bp); |
| 749 | |
| 750 | bp = nbp; |
| 751 | } |
| 752 | |
| 753 | wd->sc_wdc_bio.blkno = bp->b_rawblkno; |
| 754 | wd->sc_wdc_bio.bcount = bp->b_bcount; |
| 755 | wd->sc_wdc_bio.databuf = bp->b_data; |
| 756 | wd->sc_wdc_bio.blkdone =0; |
| 757 | KASSERT(bp == wd->sc_bp || wd->sc_bp == NULL); |
| 758 | wd->sc_bp = bp; |
| 759 | /* |
| 760 | * If we're retrying, retry in single-sector mode. This will give us |
| 761 | * the sector number of the problem, and will eventually allow the |
| 762 | * transfer to succeed. |
| 763 | */ |
| 764 | if (wd->retries >= WDIORETRIES_SINGLE) |
| 765 | wd->sc_wdc_bio.flags = ATA_SINGLE; |
| 766 | else |
| 767 | wd->sc_wdc_bio.flags = 0; |
| 768 | if (wd->sc_flags & WDF_LBA48 && |
| 769 | (wd->sc_wdc_bio.blkno + |
| 770 | wd->sc_wdc_bio.bcount / wd->sc_dk.dk_label->d_secsize) > |
| 771 | wd->sc_capacity28) |
| 772 | wd->sc_wdc_bio.flags |= ATA_LBA48; |
| 773 | if (wd->sc_flags & WDF_LBA) |
| 774 | wd->sc_wdc_bio.flags |= ATA_LBA; |
| 775 | if (bp->b_flags & B_READ) |
| 776 | wd->sc_wdc_bio.flags |= ATA_READ; |
| 777 | /* Instrumentation. */ |
| 778 | disk_busy(&wd->sc_dk); |
| 779 | switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) { |
| 780 | case ATACMD_TRY_AGAIN: |
| 781 | callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd); |
| 782 | break; |
| 783 | case ATACMD_QUEUED: |
| 784 | case ATACMD_COMPLETE: |
| 785 | break; |
| 786 | default: |
| 787 | panic("wdstart1: bad return code from ata_bio()" ); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | void |
| 792 | wddone(void *v) |
| 793 | { |
| 794 | struct wd_softc *wd = device_private(v); |
| 795 | struct buf *bp = wd->sc_bp; |
| 796 | const char *errmsg; |
| 797 | int do_perror = 0; |
| 798 | |
| 799 | ATADEBUG_PRINT(("wddone %s\n" , device_xname(wd->sc_dev)), |
| 800 | DEBUG_XFERS); |
| 801 | if (bp == NULL) |
| 802 | return; |
| 803 | bp->b_resid = wd->sc_wdc_bio.bcount; |
| 804 | switch (wd->sc_wdc_bio.error) { |
| 805 | case ERR_DMA: |
| 806 | errmsg = "DMA error" ; |
| 807 | goto retry; |
| 808 | case ERR_DF: |
| 809 | errmsg = "device fault" ; |
| 810 | goto retry; |
| 811 | case TIMEOUT: |
| 812 | errmsg = "device timeout" ; |
| 813 | goto retry; |
| 814 | case ERR_RESET: |
| 815 | errmsg = "channel reset" ; |
| 816 | goto retry2; |
| 817 | case ERROR: |
| 818 | /* Don't care about media change bits */ |
| 819 | if (wd->sc_wdc_bio.r_error != 0 && |
| 820 | (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0) |
| 821 | goto noerror; |
| 822 | errmsg = "error" ; |
| 823 | do_perror = 1; |
| 824 | retry: /* Just reset and retry. Can we do more ? */ |
| 825 | (*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD, NULL); |
| 826 | retry2: |
| 827 | diskerr(bp, "wd" , errmsg, LOG_PRINTF, |
| 828 | wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label); |
| 829 | if (wd->retries < WDIORETRIES) |
| 830 | printf(", retrying" ); |
| 831 | printf("\n" ); |
| 832 | if (do_perror) |
| 833 | wdperror(wd); |
| 834 | if (wd->retries < WDIORETRIES) { |
| 835 | wd->retries++; |
| 836 | callout_reset(&wd->sc_restart_ch, RECOVERYTIME, |
| 837 | wdrestart, wd); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | #ifdef WD_SOFTBADSECT |
| 842 | /* |
| 843 | * Not all errors indicate a failed block but those that do, |
| 844 | * put the block on the bad-block list for the device. Only |
| 845 | * do this for reads because the drive should do it for writes, |
| 846 | * itself, according to Manuel. |
| 847 | */ |
| 848 | if ((bp->b_flags & B_READ) && |
| 849 | ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) || |
| 850 | (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) { |
| 851 | struct disk_badsectors *dbs; |
| 852 | |
| 853 | dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK); |
| 854 | dbs->dbs_min = bp->b_rawblkno; |
| 855 | dbs->dbs_max = dbs->dbs_min + |
| 856 | (bp->b_bcount /wd->sc_blksize) - 1; |
| 857 | microtime(&dbs->dbs_failedat); |
| 858 | SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next); |
| 859 | wd->sc_bscount++; |
| 860 | } |
| 861 | #endif |
| 862 | bp->b_error = EIO; |
| 863 | break; |
| 864 | case NOERROR: |
| 865 | noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0) |
| 866 | aprint_error_dev(wd->sc_dev, |
| 867 | "soft error (corrected)\n" ); |
| 868 | break; |
| 869 | case ERR_NODEV: |
| 870 | bp->b_error = EIO; |
| 871 | break; |
| 872 | } |
| 873 | if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) { |
| 874 | /* |
| 875 | * the disk or controller sometimes report a complete |
| 876 | * xfer, when there has been an error. This is wrong, |
| 877 | * assume nothing got transfered in this case |
| 878 | */ |
| 879 | bp->b_resid = bp->b_bcount; |
| 880 | } |
| 881 | disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid), |
| 882 | (bp->b_flags & B_READ)); |
| 883 | rnd_add_uint32(&wd->rnd_source, bp->b_blkno); |
| 884 | /* XXX Yuck, but we don't want to increment openings in this case */ |
| 885 | if (__predict_false(bp->b_iodone == wd_split_mod15_write)) |
| 886 | biodone(bp); |
| 887 | else { |
| 888 | biodone(bp); |
| 889 | wd->openings++; |
| 890 | } |
| 891 | KASSERT(wd->sc_bp != NULL); |
| 892 | wd->sc_bp = NULL; |
| 893 | wdstart(wd); |
| 894 | } |
| 895 | |
| 896 | void |
| 897 | wdrestart(void *v) |
| 898 | { |
| 899 | struct wd_softc *wd = v; |
| 900 | struct buf *bp = wd->sc_bp; |
| 901 | int s; |
| 902 | |
| 903 | ATADEBUG_PRINT(("wdrestart %s\n" , device_xname(wd->sc_dev)), |
| 904 | DEBUG_XFERS); |
| 905 | s = splbio(); |
| 906 | wdstart1(v, bp); |
| 907 | splx(s); |
| 908 | } |
| 909 | |
| 910 | static void |
| 911 | wdminphys(struct buf *bp) |
| 912 | { |
| 913 | const struct wd_softc * const wd = |
| 914 | device_lookup_private(&wd_cd, WDUNIT(bp->b_dev)); |
| 915 | |
| 916 | if (bp->b_bcount > (wd->sc_blksize * 128)) { |
| 917 | bp->b_bcount = (wd->sc_blksize * 128); |
| 918 | } |
| 919 | minphys(bp); |
| 920 | } |
| 921 | |
| 922 | int |
| 923 | wdread(dev_t dev, struct uio *uio, int flags) |
| 924 | { |
| 925 | |
| 926 | ATADEBUG_PRINT(("wdread\n" ), DEBUG_XFERS); |
| 927 | return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio)); |
| 928 | } |
| 929 | |
| 930 | int |
| 931 | wdwrite(dev_t dev, struct uio *uio, int flags) |
| 932 | { |
| 933 | |
| 934 | ATADEBUG_PRINT(("wdwrite\n" ), DEBUG_XFERS); |
| 935 | return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio)); |
| 936 | } |
| 937 | |
| 938 | int |
| 939 | wdopen(dev_t dev, int flag, int fmt, struct lwp *l) |
| 940 | { |
| 941 | struct wd_softc *wd; |
| 942 | int part, error; |
| 943 | |
| 944 | ATADEBUG_PRINT(("wdopen\n" ), DEBUG_FUNCS); |
| 945 | wd = device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 946 | if (wd == NULL) |
| 947 | return (ENXIO); |
| 948 | |
| 949 | if (! device_is_active(wd->sc_dev)) |
| 950 | return (ENODEV); |
| 951 | |
| 952 | if (wd->sc_capacity == 0) |
| 953 | return (ENODEV); |
| 954 | |
| 955 | part = WDPART(dev); |
| 956 | |
| 957 | mutex_enter(&wd->sc_dk.dk_openlock); |
| 958 | |
| 959 | /* |
| 960 | * If there are wedges, and this is not RAW_PART, then we |
| 961 | * need to fail. |
| 962 | */ |
| 963 | if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) { |
| 964 | error = EBUSY; |
| 965 | goto bad1; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * If this is the first open of this device, add a reference |
| 970 | * to the adapter. |
| 971 | */ |
| 972 | if (wd->sc_dk.dk_openmask == 0 && |
| 973 | (error = wd->atabus->ata_addref(wd->drvp)) != 0) |
| 974 | goto bad1; |
| 975 | |
| 976 | if (wd->sc_dk.dk_openmask != 0) { |
| 977 | /* |
| 978 | * If any partition is open, but the disk has been invalidated, |
| 979 | * disallow further opens. |
| 980 | */ |
| 981 | if ((wd->sc_flags & WDF_LOADED) == 0) { |
| 982 | error = EIO; |
| 983 | goto bad2; |
| 984 | } |
| 985 | } else { |
| 986 | if ((wd->sc_flags & WDF_LOADED) == 0) { |
| 987 | |
| 988 | /* Load the physical device parameters. */ |
| 989 | if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) { |
| 990 | aprint_error_dev(wd->sc_dev, |
| 991 | "IDENTIFY failed\n" ); |
| 992 | error = EIO; |
| 993 | goto bad2; |
| 994 | } |
| 995 | wd->sc_flags |= WDF_LOADED; |
| 996 | /* Load the partition info if not already loaded. */ |
| 997 | wdgetdisklabel(wd); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /* Check that the partition exists. */ |
| 1002 | if (part != RAW_PART && |
| 1003 | (part >= wd->sc_dk.dk_label->d_npartitions || |
| 1004 | wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) { |
| 1005 | error = ENXIO; |
| 1006 | goto bad2; |
| 1007 | } |
| 1008 | |
| 1009 | /* Insure only one open at a time. */ |
| 1010 | switch (fmt) { |
| 1011 | case S_IFCHR: |
| 1012 | wd->sc_dk.dk_copenmask |= (1 << part); |
| 1013 | break; |
| 1014 | case S_IFBLK: |
| 1015 | wd->sc_dk.dk_bopenmask |= (1 << part); |
| 1016 | break; |
| 1017 | } |
| 1018 | wd->sc_dk.dk_openmask = |
| 1019 | wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask; |
| 1020 | |
| 1021 | mutex_exit(&wd->sc_dk.dk_openlock); |
| 1022 | return 0; |
| 1023 | |
| 1024 | bad2: |
| 1025 | if (wd->sc_dk.dk_openmask == 0) |
| 1026 | wd->atabus->ata_delref(wd->drvp); |
| 1027 | bad1: |
| 1028 | mutex_exit(&wd->sc_dk.dk_openlock); |
| 1029 | return error; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Caller must hold wd->sc_dk.dk_openlock. |
| 1034 | */ |
| 1035 | static int |
| 1036 | wdlastclose(device_t self) |
| 1037 | { |
| 1038 | struct wd_softc *wd = device_private(self); |
| 1039 | |
| 1040 | wd_flushcache(wd, AT_WAIT); |
| 1041 | |
| 1042 | if (! (wd->sc_flags & WDF_KLABEL)) |
| 1043 | wd->sc_flags &= ~WDF_LOADED; |
| 1044 | |
| 1045 | wd->atabus->ata_delref(wd->drvp); |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | int |
| 1051 | wdclose(dev_t dev, int flag, int fmt, struct lwp *l) |
| 1052 | { |
| 1053 | struct wd_softc *wd = |
| 1054 | device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 1055 | int part = WDPART(dev); |
| 1056 | |
| 1057 | ATADEBUG_PRINT(("wdclose\n" ), DEBUG_FUNCS); |
| 1058 | |
| 1059 | mutex_enter(&wd->sc_dk.dk_openlock); |
| 1060 | |
| 1061 | switch (fmt) { |
| 1062 | case S_IFCHR: |
| 1063 | wd->sc_dk.dk_copenmask &= ~(1 << part); |
| 1064 | break; |
| 1065 | case S_IFBLK: |
| 1066 | wd->sc_dk.dk_bopenmask &= ~(1 << part); |
| 1067 | break; |
| 1068 | } |
| 1069 | wd->sc_dk.dk_openmask = |
| 1070 | wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask; |
| 1071 | |
| 1072 | if (wd->sc_dk.dk_openmask == 0) |
| 1073 | wdlastclose(wd->sc_dev); |
| 1074 | |
| 1075 | mutex_exit(&wd->sc_dk.dk_openlock); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | void |
| 1080 | wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp) |
| 1081 | { |
| 1082 | |
| 1083 | ATADEBUG_PRINT(("wdgetdefaultlabel\n" ), DEBUG_FUNCS); |
| 1084 | memset(lp, 0, sizeof(struct disklabel)); |
| 1085 | |
| 1086 | lp->d_secsize = wd->sc_blksize; |
| 1087 | lp->d_ntracks = wd->sc_params.atap_heads; |
| 1088 | lp->d_nsectors = wd->sc_params.atap_sectors; |
| 1089 | lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity / |
| 1090 | (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) : |
| 1091 | wd->sc_params.atap_cylinders; |
| 1092 | lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; |
| 1093 | |
| 1094 | if (strcmp(wd->sc_params.atap_model, "ST506" ) == 0) |
| 1095 | lp->d_type = DKTYPE_ST506; |
| 1096 | else |
| 1097 | lp->d_type = DKTYPE_ESDI; |
| 1098 | |
| 1099 | strncpy(lp->d_typename, wd->sc_params.atap_model, 16); |
| 1100 | strncpy(lp->d_packname, "fictitious" , 16); |
| 1101 | if (wd->sc_capacity > UINT32_MAX) |
| 1102 | lp->d_secperunit = UINT32_MAX; |
| 1103 | else |
| 1104 | lp->d_secperunit = wd->sc_capacity; |
| 1105 | lp->d_rpm = 3600; |
| 1106 | lp->d_interleave = 1; |
| 1107 | lp->d_flags = 0; |
| 1108 | |
| 1109 | lp->d_partitions[RAW_PART].p_offset = 0; |
| 1110 | lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; |
| 1111 | lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; |
| 1112 | lp->d_npartitions = RAW_PART + 1; |
| 1113 | |
| 1114 | lp->d_magic = DISKMAGIC; |
| 1115 | lp->d_magic2 = DISKMAGIC; |
| 1116 | lp->d_checksum = dkcksum(lp); |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Fabricate a default disk label, and try to read the correct one. |
| 1121 | */ |
| 1122 | void |
| 1123 | wdgetdisklabel(struct wd_softc *wd) |
| 1124 | { |
| 1125 | struct disklabel *lp = wd->sc_dk.dk_label; |
| 1126 | const char *errstring; |
| 1127 | int s; |
| 1128 | |
| 1129 | ATADEBUG_PRINT(("wdgetdisklabel\n" ), DEBUG_FUNCS); |
| 1130 | |
| 1131 | memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel)); |
| 1132 | |
| 1133 | wdgetdefaultlabel(wd, lp); |
| 1134 | |
| 1135 | wd->sc_badsect[0] = -1; |
| 1136 | |
| 1137 | if (wd->drvp->state > RESET) { |
| 1138 | s = splbio(); |
| 1139 | wd->drvp->drive_flags |= ATA_DRIVE_RESET; |
| 1140 | splx(s); |
| 1141 | } |
| 1142 | errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev), |
| 1143 | RAW_PART), wdstrategy, lp, |
| 1144 | wd->sc_dk.dk_cpulabel); |
| 1145 | if (errstring) { |
| 1146 | /* |
| 1147 | * This probably happened because the drive's default |
| 1148 | * geometry doesn't match the DOS geometry. We |
| 1149 | * assume the DOS geometry is now in the label and try |
| 1150 | * again. XXX This is a kluge. |
| 1151 | */ |
| 1152 | if (wd->drvp->state > RESET) { |
| 1153 | s = splbio(); |
| 1154 | wd->drvp->drive_flags |= ATA_DRIVE_RESET; |
| 1155 | splx(s); |
| 1156 | } |
| 1157 | errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev), |
| 1158 | RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel); |
| 1159 | } |
| 1160 | if (errstring) { |
| 1161 | aprint_error_dev(wd->sc_dev, "%s\n" , errstring); |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | if (wd->drvp->state > RESET) { |
| 1166 | s = splbio(); |
| 1167 | wd->drvp->drive_flags |= ATA_DRIVE_RESET; |
| 1168 | splx(s); |
| 1169 | } |
| 1170 | #ifdef HAS_BAD144_HANDLING |
| 1171 | if ((lp->d_flags & D_BADSECT) != 0) |
| 1172 | bad144intern(wd); |
| 1173 | #endif |
| 1174 | } |
| 1175 | |
| 1176 | void |
| 1177 | wdperror(const struct wd_softc *wd) |
| 1178 | { |
| 1179 | static const char *const errstr0_3[] = {"address mark not found" , |
| 1180 | "track 0 not found" , "aborted command" , "media change requested" , |
| 1181 | "id not found" , "media changed" , "uncorrectable data error" , |
| 1182 | "bad block detected" }; |
| 1183 | static const char *const errstr4_5[] = { |
| 1184 | "obsolete (address mark not found)" , |
| 1185 | "no media/write protected" , "aborted command" , |
| 1186 | "media change requested" , "id not found" , "media changed" , |
| 1187 | "uncorrectable data error" , "interface CRC error" }; |
| 1188 | const char *const *errstr; |
| 1189 | int i; |
| 1190 | const char *sep = "" ; |
| 1191 | |
| 1192 | const char *devname = device_xname(wd->sc_dev); |
| 1193 | struct ata_drive_datas *drvp = wd->drvp; |
| 1194 | int errno = wd->sc_wdc_bio.r_error; |
| 1195 | |
| 1196 | if (drvp->ata_vers >= 4) |
| 1197 | errstr = errstr4_5; |
| 1198 | else |
| 1199 | errstr = errstr0_3; |
| 1200 | |
| 1201 | printf("%s: (" , devname); |
| 1202 | |
| 1203 | if (errno == 0) |
| 1204 | printf("error not notified" ); |
| 1205 | |
| 1206 | for (i = 0; i < 8; i++) { |
| 1207 | if (errno & (1 << i)) { |
| 1208 | printf("%s%s" , sep, errstr[i]); |
| 1209 | sep = ", " ; |
| 1210 | } |
| 1211 | } |
| 1212 | printf(")\n" ); |
| 1213 | } |
| 1214 | |
| 1215 | int |
| 1216 | wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l) |
| 1217 | { |
| 1218 | struct wd_softc *wd = |
| 1219 | device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 1220 | int error, s; |
| 1221 | #ifdef __HAVE_OLD_DISKLABEL |
| 1222 | struct disklabel *newlabel = NULL; |
| 1223 | #endif |
| 1224 | |
| 1225 | ATADEBUG_PRINT(("wdioctl\n" ), DEBUG_FUNCS); |
| 1226 | |
| 1227 | if ((wd->sc_flags & WDF_LOADED) == 0) |
| 1228 | return EIO; |
| 1229 | |
| 1230 | error = disk_ioctl(&wd->sc_dk, dev, xfer, addr, flag, l); |
| 1231 | if (error != EPASSTHROUGH) |
| 1232 | return error; |
| 1233 | |
| 1234 | error = 0; |
| 1235 | switch (xfer) { |
| 1236 | #ifdef HAS_BAD144_HANDLING |
| 1237 | case DIOCSBAD: |
| 1238 | if ((flag & FWRITE) == 0) |
| 1239 | return EBADF; |
| 1240 | wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr; |
| 1241 | wd->sc_dk.dk_label->d_flags |= D_BADSECT; |
| 1242 | bad144intern(wd); |
| 1243 | return 0; |
| 1244 | #endif |
| 1245 | #ifdef WD_SOFTBADSECT |
| 1246 | case DIOCBSLIST : |
| 1247 | { |
| 1248 | uint32_t count, missing, skip; |
| 1249 | struct disk_badsecinfo dbsi; |
| 1250 | struct disk_badsectors *dbs; |
| 1251 | size_t available; |
| 1252 | uint8_t *laddr; |
| 1253 | |
| 1254 | dbsi = *(struct disk_badsecinfo *)addr; |
| 1255 | missing = wd->sc_bscount; |
| 1256 | count = 0; |
| 1257 | available = dbsi.dbsi_bufsize; |
| 1258 | skip = dbsi.dbsi_skip; |
| 1259 | laddr = (uint8_t *)dbsi.dbsi_buffer; |
| 1260 | |
| 1261 | /* |
| 1262 | * We start this loop with the expectation that all of the |
| 1263 | * entries will be missed and decrement this counter each |
| 1264 | * time we either skip over one (already copied out) or |
| 1265 | * we actually copy it back to user space. The structs |
| 1266 | * holding the bad sector information are copied directly |
| 1267 | * back to user space whilst the summary is returned via |
| 1268 | * the struct passed in via the ioctl. |
| 1269 | */ |
| 1270 | SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) { |
| 1271 | if (skip > 0) { |
| 1272 | missing--; |
| 1273 | skip--; |
| 1274 | continue; |
| 1275 | } |
| 1276 | if (available < sizeof(*dbs)) |
| 1277 | break; |
| 1278 | available -= sizeof(*dbs); |
| 1279 | copyout(dbs, laddr, sizeof(*dbs)); |
| 1280 | laddr += sizeof(*dbs); |
| 1281 | missing--; |
| 1282 | count++; |
| 1283 | } |
| 1284 | dbsi.dbsi_left = missing; |
| 1285 | dbsi.dbsi_copied = count; |
| 1286 | *(struct disk_badsecinfo *)addr = dbsi; |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | case DIOCBSFLUSH : |
| 1291 | /* Clean out the bad sector list */ |
| 1292 | while (!SLIST_EMPTY(&wd->sc_bslist)) { |
| 1293 | void *head = SLIST_FIRST(&wd->sc_bslist); |
| 1294 | SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next); |
| 1295 | free(head, M_TEMP); |
| 1296 | } |
| 1297 | wd->sc_bscount = 0; |
| 1298 | return 0; |
| 1299 | #endif |
| 1300 | |
| 1301 | case DIOCWDINFO: |
| 1302 | case DIOCSDINFO: |
| 1303 | #ifdef __HAVE_OLD_DISKLABEL |
| 1304 | case ODIOCWDINFO: |
| 1305 | case ODIOCSDINFO: |
| 1306 | #endif |
| 1307 | { |
| 1308 | struct disklabel *lp; |
| 1309 | |
| 1310 | if ((flag & FWRITE) == 0) |
| 1311 | return EBADF; |
| 1312 | |
| 1313 | #ifdef __HAVE_OLD_DISKLABEL |
| 1314 | if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) { |
| 1315 | newlabel = malloc(sizeof *newlabel, M_TEMP, |
| 1316 | M_WAITOK | M_ZERO); |
| 1317 | if (newlabel == NULL) |
| 1318 | return EIO; |
| 1319 | memcpy(newlabel, addr, sizeof (struct olddisklabel)); |
| 1320 | lp = newlabel; |
| 1321 | } else |
| 1322 | #endif |
| 1323 | lp = (struct disklabel *)addr; |
| 1324 | |
| 1325 | mutex_enter(&wd->sc_dk.dk_openlock); |
| 1326 | wd->sc_flags |= WDF_LABELLING; |
| 1327 | |
| 1328 | error = setdisklabel(wd->sc_dk.dk_label, |
| 1329 | lp, /*wd->sc_dk.dk_openmask : */0, |
| 1330 | wd->sc_dk.dk_cpulabel); |
| 1331 | if (error == 0) { |
| 1332 | if (wd->drvp->state > RESET) { |
| 1333 | s = splbio(); |
| 1334 | wd->drvp->drive_flags |= ATA_DRIVE_RESET; |
| 1335 | splx(s); |
| 1336 | } |
| 1337 | if (xfer == DIOCWDINFO |
| 1338 | #ifdef __HAVE_OLD_DISKLABEL |
| 1339 | || xfer == ODIOCWDINFO |
| 1340 | #endif |
| 1341 | ) |
| 1342 | error = writedisklabel(WDLABELDEV(dev), |
| 1343 | wdstrategy, wd->sc_dk.dk_label, |
| 1344 | wd->sc_dk.dk_cpulabel); |
| 1345 | } |
| 1346 | |
| 1347 | wd->sc_flags &= ~WDF_LABELLING; |
| 1348 | mutex_exit(&wd->sc_dk.dk_openlock); |
| 1349 | #ifdef __HAVE_OLD_DISKLABEL |
| 1350 | if (newlabel != NULL) |
| 1351 | free(newlabel, M_TEMP); |
| 1352 | #endif |
| 1353 | return error; |
| 1354 | } |
| 1355 | |
| 1356 | case DIOCKLABEL: |
| 1357 | if (*(int *)addr) |
| 1358 | wd->sc_flags |= WDF_KLABEL; |
| 1359 | else |
| 1360 | wd->sc_flags &= ~WDF_KLABEL; |
| 1361 | return 0; |
| 1362 | |
| 1363 | case DIOCWLABEL: |
| 1364 | if ((flag & FWRITE) == 0) |
| 1365 | return EBADF; |
| 1366 | if (*(int *)addr) |
| 1367 | wd->sc_flags |= WDF_WLABEL; |
| 1368 | else |
| 1369 | wd->sc_flags &= ~WDF_WLABEL; |
| 1370 | return 0; |
| 1371 | |
| 1372 | case DIOCGDEFLABEL: |
| 1373 | wdgetdefaultlabel(wd, (struct disklabel *)addr); |
| 1374 | return 0; |
| 1375 | #ifdef __HAVE_OLD_DISKLABEL |
| 1376 | case ODIOCGDEFLABEL: |
| 1377 | newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK); |
| 1378 | if (newlabel == NULL) |
| 1379 | return EIO; |
| 1380 | wdgetdefaultlabel(wd, newlabel); |
| 1381 | if (newlabel->d_npartitions <= OLDMAXPARTITIONS) |
| 1382 | memcpy(addr, &newlabel, sizeof (struct olddisklabel)); |
| 1383 | else |
| 1384 | error = ENOTTY; |
| 1385 | free(newlabel, M_TEMP); |
| 1386 | return error; |
| 1387 | #endif |
| 1388 | |
| 1389 | #ifdef notyet |
| 1390 | case DIOCWFORMAT: |
| 1391 | if ((flag & FWRITE) == 0) |
| 1392 | return EBADF; |
| 1393 | { |
| 1394 | register struct format_op *fop; |
| 1395 | struct iovec aiov; |
| 1396 | struct uio auio; |
| 1397 | |
| 1398 | fop = (struct format_op *)addr; |
| 1399 | aiov.iov_base = fop->df_buf; |
| 1400 | aiov.iov_len = fop->df_count; |
| 1401 | auio.uio_iov = &aiov; |
| 1402 | auio.uio_iovcnt = 1; |
| 1403 | auio.uio_resid = fop->df_count; |
| 1404 | auio.uio_offset = |
| 1405 | fop->df_startblk * wd->sc_dk.dk_label->d_secsize; |
| 1406 | auio.uio_vmspace = l->l_proc->p_vmspace; |
| 1407 | error = physio(wdformat, NULL, dev, B_WRITE, wdminphys, |
| 1408 | &auio); |
| 1409 | fop->df_count -= auio.uio_resid; |
| 1410 | fop->df_reg[0] = wdc->sc_status; |
| 1411 | fop->df_reg[1] = wdc->sc_error; |
| 1412 | return error; |
| 1413 | } |
| 1414 | #endif |
| 1415 | case DIOCGCACHE: |
| 1416 | return wd_getcache(wd, (int *)addr); |
| 1417 | |
| 1418 | case DIOCSCACHE: |
| 1419 | return wd_setcache(wd, *(int *)addr); |
| 1420 | |
| 1421 | case DIOCCACHESYNC: |
| 1422 | return wd_flushcache(wd, AT_WAIT); |
| 1423 | |
| 1424 | case ATAIOCCOMMAND: |
| 1425 | /* |
| 1426 | * Make sure this command is (relatively) safe first |
| 1427 | */ |
| 1428 | if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 && |
| 1429 | (flag & FWRITE) == 0) |
| 1430 | return (EBADF); |
| 1431 | { |
| 1432 | struct wd_ioctl *wi; |
| 1433 | atareq_t *atareq = (atareq_t *) addr; |
| 1434 | int error1; |
| 1435 | |
| 1436 | wi = wi_get(); |
| 1437 | wi->wi_softc = wd; |
| 1438 | wi->wi_atareq = *atareq; |
| 1439 | |
| 1440 | if (atareq->datalen && atareq->flags & |
| 1441 | (ATACMD_READ | ATACMD_WRITE)) { |
| 1442 | void *tbuf; |
| 1443 | if (atareq->datalen < DEV_BSIZE |
| 1444 | && atareq->command == WDCC_IDENTIFY) { |
| 1445 | tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK); |
| 1446 | wi->wi_iov.iov_base = tbuf; |
| 1447 | wi->wi_iov.iov_len = DEV_BSIZE; |
| 1448 | UIO_SETUP_SYSSPACE(&wi->wi_uio); |
| 1449 | } else { |
| 1450 | tbuf = NULL; |
| 1451 | wi->wi_iov.iov_base = atareq->databuf; |
| 1452 | wi->wi_iov.iov_len = atareq->datalen; |
| 1453 | wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace; |
| 1454 | } |
| 1455 | wi->wi_uio.uio_iov = &wi->wi_iov; |
| 1456 | wi->wi_uio.uio_iovcnt = 1; |
| 1457 | wi->wi_uio.uio_resid = atareq->datalen; |
| 1458 | wi->wi_uio.uio_offset = 0; |
| 1459 | wi->wi_uio.uio_rw = |
| 1460 | (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE; |
| 1461 | error1 = physio(wdioctlstrategy, &wi->wi_bp, dev, |
| 1462 | (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE, |
| 1463 | wdminphys, &wi->wi_uio); |
| 1464 | if (tbuf != NULL && error1 == 0) { |
| 1465 | error1 = copyout(tbuf, atareq->databuf, |
| 1466 | atareq->datalen); |
| 1467 | free(tbuf, M_TEMP); |
| 1468 | } |
| 1469 | } else { |
| 1470 | /* No need to call physio if we don't have any |
| 1471 | user data */ |
| 1472 | wi->wi_bp.b_flags = 0; |
| 1473 | wi->wi_bp.b_data = 0; |
| 1474 | wi->wi_bp.b_bcount = 0; |
| 1475 | wi->wi_bp.b_dev = 0; |
| 1476 | wi->wi_bp.b_proc = l->l_proc; |
| 1477 | wdioctlstrategy(&wi->wi_bp); |
| 1478 | error1 = wi->wi_bp.b_error; |
| 1479 | } |
| 1480 | *atareq = wi->wi_atareq; |
| 1481 | wi_free(wi); |
| 1482 | return(error1); |
| 1483 | } |
| 1484 | |
| 1485 | case DIOCGSTRATEGY: |
| 1486 | { |
| 1487 | struct disk_strategy *dks = (void *)addr; |
| 1488 | |
| 1489 | s = splbio(); |
| 1490 | strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q), |
| 1491 | sizeof(dks->dks_name)); |
| 1492 | splx(s); |
| 1493 | dks->dks_paramlen = 0; |
| 1494 | |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | case DIOCSSTRATEGY: |
| 1499 | { |
| 1500 | struct disk_strategy *dks = (void *)addr; |
| 1501 | struct bufq_state *new; |
| 1502 | struct bufq_state *old; |
| 1503 | |
| 1504 | if ((flag & FWRITE) == 0) { |
| 1505 | return EBADF; |
| 1506 | } |
| 1507 | if (dks->dks_param != NULL) { |
| 1508 | return EINVAL; |
| 1509 | } |
| 1510 | dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */ |
| 1511 | error = bufq_alloc(&new, dks->dks_name, |
| 1512 | BUFQ_EXACT|BUFQ_SORT_RAWBLOCK); |
| 1513 | if (error) { |
| 1514 | return error; |
| 1515 | } |
| 1516 | s = splbio(); |
| 1517 | old = wd->sc_q; |
| 1518 | bufq_move(new, old); |
| 1519 | wd->sc_q = new; |
| 1520 | splx(s); |
| 1521 | bufq_free(old); |
| 1522 | |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | default: |
| 1527 | return ENOTTY; |
| 1528 | } |
| 1529 | |
| 1530 | #ifdef DIAGNOSTIC |
| 1531 | panic("wdioctl: impossible" ); |
| 1532 | #endif |
| 1533 | } |
| 1534 | |
| 1535 | static int |
| 1536 | wddiscard(dev_t dev, off_t pos, off_t len) |
| 1537 | { |
| 1538 | struct wd_softc *wd = device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 1539 | daddr_t bno; |
| 1540 | long size, done; |
| 1541 | long maxatonce, amount; |
| 1542 | int result; |
| 1543 | |
| 1544 | if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7) |
| 1545 | || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) { |
| 1546 | /* not supported; ignore request */ |
| 1547 | ATADEBUG_PRINT(("wddiscard (unsupported)\n" ), DEBUG_FUNCS); |
| 1548 | return 0; |
| 1549 | } |
| 1550 | maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/ |
| 1551 | |
| 1552 | ATADEBUG_PRINT(("wddiscard\n" ), DEBUG_FUNCS); |
| 1553 | |
| 1554 | if ((wd->sc_flags & WDF_LOADED) == 0) |
| 1555 | return EIO; |
| 1556 | |
| 1557 | /* round the start up and the end down */ |
| 1558 | bno = (pos + wd->sc_blksize - 1) / wd->sc_blksize; |
| 1559 | size = ((pos + len) / wd->sc_blksize) - bno; |
| 1560 | |
| 1561 | done = 0; |
| 1562 | while (done < size) { |
| 1563 | amount = size - done; |
| 1564 | if (amount > maxatonce) { |
| 1565 | amount = maxatonce; |
| 1566 | } |
| 1567 | result = wd_trim(wd, WDPART(dev), bno + done, amount); |
| 1568 | if (result) { |
| 1569 | return result; |
| 1570 | } |
| 1571 | done += amount; |
| 1572 | } |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | #ifdef B_FORMAT |
| 1577 | int |
| 1578 | wdformat(struct buf *bp) |
| 1579 | { |
| 1580 | |
| 1581 | bp->b_flags |= B_FORMAT; |
| 1582 | return wdstrategy(bp); |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
| 1586 | int |
| 1587 | wdsize(dev_t dev) |
| 1588 | { |
| 1589 | struct wd_softc *wd; |
| 1590 | int part, omask; |
| 1591 | int size; |
| 1592 | |
| 1593 | ATADEBUG_PRINT(("wdsize\n" ), DEBUG_FUNCS); |
| 1594 | |
| 1595 | wd = device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 1596 | if (wd == NULL) |
| 1597 | return (-1); |
| 1598 | |
| 1599 | part = WDPART(dev); |
| 1600 | omask = wd->sc_dk.dk_openmask & (1 << part); |
| 1601 | |
| 1602 | if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0) |
| 1603 | return (-1); |
| 1604 | if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP) |
| 1605 | size = -1; |
| 1606 | else |
| 1607 | size = wd->sc_dk.dk_label->d_partitions[part].p_size * |
| 1608 | (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE); |
| 1609 | if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0) |
| 1610 | return (-1); |
| 1611 | return (size); |
| 1612 | } |
| 1613 | |
| 1614 | /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */ |
| 1615 | static int wddoingadump = 0; |
| 1616 | static int wddumprecalibrated = 0; |
| 1617 | |
| 1618 | /* |
| 1619 | * Dump core after a system crash. |
| 1620 | */ |
| 1621 | int |
| 1622 | wddump(dev_t dev, daddr_t blkno, void *va, size_t size) |
| 1623 | { |
| 1624 | struct wd_softc *wd; /* disk unit to do the I/O */ |
| 1625 | struct disklabel *lp; /* disk's disklabel */ |
| 1626 | int part, err; |
| 1627 | int nblks; /* total number of sectors left to write */ |
| 1628 | |
| 1629 | /* Check if recursive dump; if so, punt. */ |
| 1630 | if (wddoingadump) |
| 1631 | return EFAULT; |
| 1632 | wddoingadump = 1; |
| 1633 | |
| 1634 | wd = device_lookup_private(&wd_cd, WDUNIT(dev)); |
| 1635 | if (wd == NULL) |
| 1636 | return (ENXIO); |
| 1637 | |
| 1638 | part = WDPART(dev); |
| 1639 | |
| 1640 | /* Convert to disk sectors. Request must be a multiple of size. */ |
| 1641 | lp = wd->sc_dk.dk_label; |
| 1642 | if ((size % lp->d_secsize) != 0) |
| 1643 | return EFAULT; |
| 1644 | nblks = size / lp->d_secsize; |
| 1645 | blkno = blkno / (lp->d_secsize / DEV_BSIZE); |
| 1646 | |
| 1647 | /* Check transfer bounds against partition size. */ |
| 1648 | if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size)) |
| 1649 | return EINVAL; |
| 1650 | |
| 1651 | /* Offset block number to start of partition. */ |
| 1652 | blkno += lp->d_partitions[part].p_offset; |
| 1653 | |
| 1654 | /* Recalibrate, if first dump transfer. */ |
| 1655 | if (wddumprecalibrated == 0) { |
| 1656 | wddumprecalibrated = 1; |
| 1657 | (*wd->atabus->ata_reset_drive)(wd->drvp, |
| 1658 | AT_POLL | AT_RST_EMERG, NULL); |
| 1659 | wd->drvp->state = RESET; |
| 1660 | } |
| 1661 | |
| 1662 | wd->sc_bp = NULL; |
| 1663 | wd->sc_wdc_bio.blkno = blkno; |
| 1664 | wd->sc_wdc_bio.flags = ATA_POLL; |
| 1665 | if (wd->sc_flags & WDF_LBA48 && |
| 1666 | (wd->sc_wdc_bio.blkno + nblks) > wd->sc_capacity28) |
| 1667 | wd->sc_wdc_bio.flags |= ATA_LBA48; |
| 1668 | if (wd->sc_flags & WDF_LBA) |
| 1669 | wd->sc_wdc_bio.flags |= ATA_LBA; |
| 1670 | wd->sc_wdc_bio.bcount = nblks * lp->d_secsize; |
| 1671 | wd->sc_wdc_bio.databuf = va; |
| 1672 | #ifndef WD_DUMP_NOT_TRUSTED |
| 1673 | switch (err = wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) { |
| 1674 | case ATACMD_TRY_AGAIN: |
| 1675 | panic("wddump: try again" ); |
| 1676 | break; |
| 1677 | case ATACMD_QUEUED: |
| 1678 | panic("wddump: polled command has been queued" ); |
| 1679 | break; |
| 1680 | case ATACMD_COMPLETE: |
| 1681 | break; |
| 1682 | default: |
| 1683 | panic("wddump: unknown atacmd code %d" , err); |
| 1684 | } |
| 1685 | switch(err = wd->sc_wdc_bio.error) { |
| 1686 | case TIMEOUT: |
| 1687 | printf("wddump: device timed out" ); |
| 1688 | err = EIO; |
| 1689 | break; |
| 1690 | case ERR_DF: |
| 1691 | printf("wddump: drive fault" ); |
| 1692 | err = EIO; |
| 1693 | break; |
| 1694 | case ERR_DMA: |
| 1695 | printf("wddump: DMA error" ); |
| 1696 | err = EIO; |
| 1697 | break; |
| 1698 | case ERROR: |
| 1699 | printf("wddump: " ); |
| 1700 | wdperror(wd); |
| 1701 | err = EIO; |
| 1702 | break; |
| 1703 | case NOERROR: |
| 1704 | err = 0; |
| 1705 | break; |
| 1706 | default: |
| 1707 | panic("wddump: unknown error type %d" , err); |
| 1708 | } |
| 1709 | if (err != 0) { |
| 1710 | printf("\n" ); |
| 1711 | return err; |
| 1712 | } |
| 1713 | #else /* WD_DUMP_NOT_TRUSTED */ |
| 1714 | /* Let's just talk about this first... */ |
| 1715 | printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n" , |
| 1716 | unit, va, cylin, head, sector); |
| 1717 | delay(500 * 1000); /* half a second */ |
| 1718 | #endif |
| 1719 | |
| 1720 | wddoingadump = 0; |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
| 1724 | #ifdef HAS_BAD144_HANDLING |
| 1725 | /* |
| 1726 | * Internalize the bad sector table. |
| 1727 | */ |
| 1728 | void |
| 1729 | bad144intern(struct wd_softc *wd) |
| 1730 | { |
| 1731 | struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad; |
| 1732 | struct disklabel *lp = wd->sc_dk.dk_label; |
| 1733 | int i = 0; |
| 1734 | |
| 1735 | ATADEBUG_PRINT(("bad144intern\n" ), DEBUG_XFERS); |
| 1736 | |
| 1737 | for (; i < NBT_BAD; i++) { |
| 1738 | if (bt->bt_bad[i].bt_cyl == 0xffff) |
| 1739 | break; |
| 1740 | wd->sc_badsect[i] = |
| 1741 | bt->bt_bad[i].bt_cyl * lp->d_secpercyl + |
| 1742 | (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors + |
| 1743 | (bt->bt_bad[i].bt_trksec & 0xff); |
| 1744 | } |
| 1745 | for (; i < NBT_BAD+1; i++) |
| 1746 | wd->sc_badsect[i] = -1; |
| 1747 | } |
| 1748 | #endif |
| 1749 | |
| 1750 | static void |
| 1751 | wd_params_to_properties(struct wd_softc *wd) |
| 1752 | { |
| 1753 | struct disk_geom *dg = &wd->sc_dk.dk_geom; |
| 1754 | |
| 1755 | memset(dg, 0, sizeof(*dg)); |
| 1756 | |
| 1757 | dg->dg_secperunit = wd->sc_capacity; |
| 1758 | dg->dg_secsize = wd->sc_blksize; |
| 1759 | dg->dg_nsectors = wd->sc_params.atap_sectors; |
| 1760 | dg->dg_ntracks = wd->sc_params.atap_heads; |
| 1761 | if ((wd->sc_flags & WDF_LBA) == 0) |
| 1762 | dg->dg_ncylinders = wd->sc_params.atap_cylinders; |
| 1763 | |
| 1764 | /* XXX Should have a case for ATA here, too. */ |
| 1765 | const char *cp = strcmp(wd->sc_params.atap_model, "ST506" ) ? |
| 1766 | "ST506" : "ESDI" ; |
| 1767 | |
| 1768 | disk_set_info(wd->sc_dev, &wd->sc_dk, cp); |
| 1769 | } |
| 1770 | |
| 1771 | int |
| 1772 | wd_get_params(struct wd_softc *wd, uint8_t flags, struct ataparams *params) |
| 1773 | { |
| 1774 | |
| 1775 | switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) { |
| 1776 | case CMD_AGAIN: |
| 1777 | return 1; |
| 1778 | case CMD_ERR: |
| 1779 | if (wd->drvp->drive_type != ATA_DRIVET_OLD) |
| 1780 | return 1; |
| 1781 | /* |
| 1782 | * We `know' there's a drive here; just assume it's old. |
| 1783 | * This geometry is only used to read the MBR and print a |
| 1784 | * (false) attach message. |
| 1785 | */ |
| 1786 | strncpy(params->atap_model, "ST506" , |
| 1787 | sizeof params->atap_model); |
| 1788 | params->atap_config = ATA_CFG_FIXED; |
| 1789 | params->atap_cylinders = 1024; |
| 1790 | params->atap_heads = 8; |
| 1791 | params->atap_sectors = 17; |
| 1792 | params->atap_multi = 1; |
| 1793 | params->atap_capabilities1 = params->atap_capabilities2 = 0; |
| 1794 | wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */ |
| 1795 | /* FALLTHROUGH */ |
| 1796 | case CMD_OK: |
| 1797 | return 0; |
| 1798 | default: |
| 1799 | panic("wd_get_params: bad return code from ata_get_params" ); |
| 1800 | /* NOTREACHED */ |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | int |
| 1805 | wd_getcache(struct wd_softc *wd, int *bitsp) |
| 1806 | { |
| 1807 | struct ataparams params; |
| 1808 | |
| 1809 | if (wd_get_params(wd, AT_WAIT, ¶ms) != 0) |
| 1810 | return EIO; |
| 1811 | if (params.atap_cmd_set1 == 0x0000 || |
| 1812 | params.atap_cmd_set1 == 0xffff || |
| 1813 | (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) { |
| 1814 | *bitsp = 0; |
| 1815 | return 0; |
| 1816 | } |
| 1817 | *bitsp = DKCACHE_WCHANGE | DKCACHE_READ; |
| 1818 | if (params.atap_cmd1_en & WDC_CMD1_CACHE) |
| 1819 | *bitsp |= DKCACHE_WRITE; |
| 1820 | |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF" ; |
| 1825 | |
| 1826 | int |
| 1827 | wd_setcache(struct wd_softc *wd, int bits) |
| 1828 | { |
| 1829 | struct ataparams params; |
| 1830 | struct ata_command ata_c; |
| 1831 | |
| 1832 | if (wd_get_params(wd, AT_WAIT, ¶ms) != 0) |
| 1833 | return EIO; |
| 1834 | |
| 1835 | if (params.atap_cmd_set1 == 0x0000 || |
| 1836 | params.atap_cmd_set1 == 0xffff || |
| 1837 | (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) |
| 1838 | return EOPNOTSUPP; |
| 1839 | |
| 1840 | if ((bits & DKCACHE_READ) == 0 || |
| 1841 | (bits & DKCACHE_SAVE) != 0) |
| 1842 | return EOPNOTSUPP; |
| 1843 | |
| 1844 | memset(&ata_c, 0, sizeof(struct ata_command)); |
| 1845 | ata_c.r_command = SET_FEATURES; |
| 1846 | ata_c.r_st_bmask = 0; |
| 1847 | ata_c.r_st_pmask = 0; |
| 1848 | ata_c.timeout = 30000; /* 30s timeout */ |
| 1849 | ata_c.flags = AT_WAIT; |
| 1850 | if (bits & DKCACHE_WRITE) |
| 1851 | ata_c.r_features = WDSF_WRITE_CACHE_EN; |
| 1852 | else |
| 1853 | ata_c.r_features = WDSF_WRITE_CACHE_DS; |
| 1854 | if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { |
| 1855 | aprint_error_dev(wd->sc_dev, |
| 1856 | "wd_setcache command not complete\n" ); |
| 1857 | return EIO; |
| 1858 | } |
| 1859 | if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { |
| 1860 | char sbuf[sizeof(at_errbits) + 64]; |
| 1861 | snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); |
| 1862 | aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n" , sbuf); |
| 1863 | return EIO; |
| 1864 | } |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | static int |
| 1869 | wd_standby(struct wd_softc *wd, int flags) |
| 1870 | { |
| 1871 | struct ata_command ata_c; |
| 1872 | |
| 1873 | memset(&ata_c, 0, sizeof(struct ata_command)); |
| 1874 | ata_c.r_command = WDCC_STANDBY_IMMED; |
| 1875 | ata_c.r_st_bmask = WDCS_DRDY; |
| 1876 | ata_c.r_st_pmask = WDCS_DRDY; |
| 1877 | ata_c.flags = flags; |
| 1878 | ata_c.timeout = 30000; /* 30s timeout */ |
| 1879 | if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { |
| 1880 | aprint_error_dev(wd->sc_dev, |
| 1881 | "standby immediate command didn't complete\n" ); |
| 1882 | return EIO; |
| 1883 | } |
| 1884 | if (ata_c.flags & AT_ERROR) { |
| 1885 | if (ata_c.r_error == WDCE_ABRT) /* command not supported */ |
| 1886 | return ENODEV; |
| 1887 | } |
| 1888 | if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { |
| 1889 | char sbuf[sizeof(at_errbits) + 64]; |
| 1890 | snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); |
| 1891 | aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n" , sbuf); |
| 1892 | return EIO; |
| 1893 | } |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | int |
| 1898 | wd_flushcache(struct wd_softc *wd, int flags) |
| 1899 | { |
| 1900 | struct ata_command ata_c; |
| 1901 | |
| 1902 | /* |
| 1903 | * WDCC_FLUSHCACHE is here since ATA-4, but some drives report |
| 1904 | * only ATA-2 and still support it. |
| 1905 | */ |
| 1906 | if (wd->drvp->ata_vers < 4 && |
| 1907 | ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 || |
| 1908 | wd->sc_params.atap_cmd_set2 == 0xffff)) |
| 1909 | return ENODEV; |
| 1910 | memset(&ata_c, 0, sizeof(struct ata_command)); |
| 1911 | if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 && |
| 1912 | (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) { |
| 1913 | ata_c.r_command = WDCC_FLUSHCACHE_EXT; |
| 1914 | flags |= AT_LBA48; |
| 1915 | } else |
| 1916 | ata_c.r_command = WDCC_FLUSHCACHE; |
| 1917 | ata_c.r_st_bmask = WDCS_DRDY; |
| 1918 | ata_c.r_st_pmask = WDCS_DRDY; |
| 1919 | ata_c.flags = flags | AT_READREG; |
| 1920 | ata_c.timeout = 300000; /* 5m timeout */ |
| 1921 | if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { |
| 1922 | aprint_error_dev(wd->sc_dev, |
| 1923 | "flush cache command didn't complete\n" ); |
| 1924 | return EIO; |
| 1925 | } |
| 1926 | if (ata_c.flags & AT_ERROR) { |
| 1927 | if (ata_c.r_error == WDCE_ABRT) /* command not supported */ |
| 1928 | return ENODEV; |
| 1929 | } |
| 1930 | if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { |
| 1931 | char sbuf[sizeof(at_errbits) + 64]; |
| 1932 | snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); |
| 1933 | aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n" , |
| 1934 | sbuf); |
| 1935 | return EIO; |
| 1936 | } |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | int |
| 1941 | wd_trim(struct wd_softc *wd, int part, daddr_t bno, long size) |
| 1942 | { |
| 1943 | struct ata_command ata_c; |
| 1944 | unsigned char *req; |
| 1945 | |
| 1946 | if (part != RAW_PART) |
| 1947 | bno += wd->sc_dk.dk_label->d_partitions[part].p_offset;; |
| 1948 | |
| 1949 | req = kmem_zalloc(512, KM_SLEEP); |
| 1950 | req[0] = bno & 0xff; |
| 1951 | req[1] = (bno >> 8) & 0xff; |
| 1952 | req[2] = (bno >> 16) & 0xff; |
| 1953 | req[3] = (bno >> 24) & 0xff; |
| 1954 | req[4] = (bno >> 32) & 0xff; |
| 1955 | req[5] = (bno >> 40) & 0xff; |
| 1956 | req[6] = size & 0xff; |
| 1957 | req[7] = (size >> 8) & 0xff; |
| 1958 | |
| 1959 | memset(&ata_c, 0, sizeof(struct ata_command)); |
| 1960 | ata_c.r_command = ATA_DATA_SET_MANAGEMENT; |
| 1961 | ata_c.r_count = 1; |
| 1962 | ata_c.r_features = ATA_SUPPORT_DSM_TRIM; |
| 1963 | ata_c.r_st_bmask = WDCS_DRDY; |
| 1964 | ata_c.r_st_pmask = WDCS_DRDY; |
| 1965 | ata_c.timeout = 30000; /* 30s timeout */ |
| 1966 | ata_c.data = req; |
| 1967 | ata_c.bcount = 512; |
| 1968 | ata_c.flags |= AT_WRITE | AT_WAIT; |
| 1969 | if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) { |
| 1970 | aprint_error_dev(wd->sc_dev, |
| 1971 | "trim command didn't complete\n" ); |
| 1972 | kmem_free(req, 512); |
| 1973 | return EIO; |
| 1974 | } |
| 1975 | kmem_free(req, 512); |
| 1976 | if (ata_c.flags & AT_ERROR) { |
| 1977 | if (ata_c.r_error == WDCE_ABRT) /* command not supported */ |
| 1978 | return ENODEV; |
| 1979 | } |
| 1980 | if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { |
| 1981 | char sbuf[sizeof(at_errbits) + 64]; |
| 1982 | snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags); |
| 1983 | aprint_error_dev(wd->sc_dev, "wd_trim: status=%s\n" , |
| 1984 | sbuf); |
| 1985 | return EIO; |
| 1986 | } |
| 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | bool |
| 1991 | wd_shutdown(device_t dev, int how) |
| 1992 | { |
| 1993 | struct wd_softc *wd = device_private(dev); |
| 1994 | |
| 1995 | /* the adapter needs to be enabled */ |
| 1996 | if (wd->atabus->ata_addref(wd->drvp)) |
| 1997 | return true; /* no need to complain */ |
| 1998 | |
| 1999 | wd_flushcache(wd, AT_POLL); |
| 2000 | if ((how & RB_POWERDOWN) == RB_POWERDOWN) |
| 2001 | wd_standby(wd, AT_POLL); |
| 2002 | return true; |
| 2003 | } |
| 2004 | |
| 2005 | /* |
| 2006 | * Allocate space for a ioctl queue structure. Mostly taken from |
| 2007 | * scsipi_ioctl.c |
| 2008 | */ |
| 2009 | struct wd_ioctl * |
| 2010 | wi_get(void) |
| 2011 | { |
| 2012 | struct wd_ioctl *wi; |
| 2013 | int s; |
| 2014 | |
| 2015 | wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO); |
| 2016 | buf_init(&wi->wi_bp); |
| 2017 | s = splbio(); |
| 2018 | LIST_INSERT_HEAD(&wi_head, wi, wi_list); |
| 2019 | splx(s); |
| 2020 | return (wi); |
| 2021 | } |
| 2022 | |
| 2023 | /* |
| 2024 | * Free an ioctl structure and remove it from our list |
| 2025 | */ |
| 2026 | |
| 2027 | void |
| 2028 | wi_free(struct wd_ioctl *wi) |
| 2029 | { |
| 2030 | int s; |
| 2031 | |
| 2032 | s = splbio(); |
| 2033 | LIST_REMOVE(wi, wi_list); |
| 2034 | splx(s); |
| 2035 | buf_destroy(&wi->wi_bp); |
| 2036 | free(wi, M_TEMP); |
| 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * Find a wd_ioctl structure based on the struct buf. |
| 2041 | */ |
| 2042 | |
| 2043 | struct wd_ioctl * |
| 2044 | wi_find(struct buf *bp) |
| 2045 | { |
| 2046 | struct wd_ioctl *wi; |
| 2047 | int s; |
| 2048 | |
| 2049 | s = splbio(); |
| 2050 | for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next) |
| 2051 | if (bp == &wi->wi_bp) |
| 2052 | break; |
| 2053 | splx(s); |
| 2054 | return (wi); |
| 2055 | } |
| 2056 | |
| 2057 | static uint |
| 2058 | wi_sector_size(const struct wd_ioctl * const wi) |
| 2059 | { |
| 2060 | switch (wi->wi_atareq.command) { |
| 2061 | case WDCC_READ: |
| 2062 | case WDCC_WRITE: |
| 2063 | case WDCC_READMULTI: |
| 2064 | case WDCC_WRITEMULTI: |
| 2065 | case WDCC_READDMA: |
| 2066 | case WDCC_WRITEDMA: |
| 2067 | case WDCC_READ_EXT: |
| 2068 | case WDCC_WRITE_EXT: |
| 2069 | case WDCC_READMULTI_EXT: |
| 2070 | case WDCC_WRITEMULTI_EXT: |
| 2071 | case WDCC_READDMA_EXT: |
| 2072 | case WDCC_WRITEDMA_EXT: |
| 2073 | case WDCC_READ_FPDMA_QUEUED: |
| 2074 | case WDCC_WRITE_FPDMA_QUEUED: |
| 2075 | return wi->wi_softc->sc_blksize; |
| 2076 | default: |
| 2077 | return 512; |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | /* |
| 2082 | * Ioctl pseudo strategy routine |
| 2083 | * |
| 2084 | * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What |
| 2085 | * happens here is: |
| 2086 | * |
| 2087 | * - wdioctl() queues a wd_ioctl structure. |
| 2088 | * |
| 2089 | * - wdioctl() calls physio/wdioctlstrategy based on whether or not |
| 2090 | * user space I/O is required. If physio() is called, physio() eventually |
| 2091 | * calls wdioctlstrategy(). |
| 2092 | * |
| 2093 | * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command() |
| 2094 | * to perform the actual command |
| 2095 | * |
| 2096 | * The reason for the use of the pseudo strategy routine is because |
| 2097 | * when doing I/O to/from user space, physio _really_ wants to be in |
| 2098 | * the loop. We could put the entire buffer into the ioctl request |
| 2099 | * structure, but that won't scale if we want to do things like download |
| 2100 | * microcode. |
| 2101 | */ |
| 2102 | |
| 2103 | void |
| 2104 | wdioctlstrategy(struct buf *bp) |
| 2105 | { |
| 2106 | struct wd_ioctl *wi; |
| 2107 | struct ata_command ata_c; |
| 2108 | int error = 0; |
| 2109 | |
| 2110 | wi = wi_find(bp); |
| 2111 | if (wi == NULL) { |
| 2112 | printf("wdioctlstrategy: " |
| 2113 | "No matching ioctl request found in queue\n" ); |
| 2114 | error = EINVAL; |
| 2115 | goto bad; |
| 2116 | } |
| 2117 | |
| 2118 | memset(&ata_c, 0, sizeof(ata_c)); |
| 2119 | |
| 2120 | /* |
| 2121 | * Abort if physio broke up the transfer |
| 2122 | */ |
| 2123 | |
| 2124 | if (bp->b_bcount != wi->wi_atareq.datalen) { |
| 2125 | printf("physio split wd ioctl request... cannot proceed\n" ); |
| 2126 | error = EIO; |
| 2127 | goto bad; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * Abort if we didn't get a buffer size that was a multiple of |
| 2132 | * our sector size (or overflows CHS/LBA28 sector count) |
| 2133 | */ |
| 2134 | |
| 2135 | if ((bp->b_bcount % wi_sector_size(wi)) != 0 || |
| 2136 | (bp->b_bcount / wi_sector_size(wi)) >= |
| 2137 | (1 << NBBY)) { |
| 2138 | error = EINVAL; |
| 2139 | goto bad; |
| 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * Make sure a timeout was supplied in the ioctl request |
| 2144 | */ |
| 2145 | |
| 2146 | if (wi->wi_atareq.timeout == 0) { |
| 2147 | error = EINVAL; |
| 2148 | goto bad; |
| 2149 | } |
| 2150 | |
| 2151 | if (wi->wi_atareq.flags & ATACMD_READ) |
| 2152 | ata_c.flags |= AT_READ; |
| 2153 | else if (wi->wi_atareq.flags & ATACMD_WRITE) |
| 2154 | ata_c.flags |= AT_WRITE; |
| 2155 | |
| 2156 | if (wi->wi_atareq.flags & ATACMD_READREG) |
| 2157 | ata_c.flags |= AT_READREG; |
| 2158 | |
| 2159 | if ((wi->wi_atareq.flags & ATACMD_LBA) != 0) |
| 2160 | ata_c.flags |= AT_LBA; |
| 2161 | |
| 2162 | ata_c.flags |= AT_WAIT; |
| 2163 | |
| 2164 | ata_c.timeout = wi->wi_atareq.timeout; |
| 2165 | ata_c.r_command = wi->wi_atareq.command; |
| 2166 | ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) | |
| 2167 | (wi->wi_atareq.cylinder << 8) | |
| 2168 | wi->wi_atareq.sec_num; |
| 2169 | ata_c.r_count = wi->wi_atareq.sec_count; |
| 2170 | ata_c.r_features = wi->wi_atareq.features; |
| 2171 | ata_c.r_st_bmask = WDCS_DRDY; |
| 2172 | ata_c.r_st_pmask = WDCS_DRDY; |
| 2173 | ata_c.data = wi->wi_bp.b_data; |
| 2174 | ata_c.bcount = wi->wi_bp.b_bcount; |
| 2175 | |
| 2176 | if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c) |
| 2177 | != ATACMD_COMPLETE) { |
| 2178 | wi->wi_atareq.retsts = ATACMD_ERROR; |
| 2179 | goto bad; |
| 2180 | } |
| 2181 | |
| 2182 | if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) { |
| 2183 | if (ata_c.flags & AT_ERROR) { |
| 2184 | wi->wi_atareq.retsts = ATACMD_ERROR; |
| 2185 | wi->wi_atareq.error = ata_c.r_error; |
| 2186 | } else if (ata_c.flags & AT_DF) |
| 2187 | wi->wi_atareq.retsts = ATACMD_DF; |
| 2188 | else |
| 2189 | wi->wi_atareq.retsts = ATACMD_TIMEOUT; |
| 2190 | } else { |
| 2191 | wi->wi_atareq.retsts = ATACMD_OK; |
| 2192 | if (wi->wi_atareq.flags & ATACMD_READREG) { |
| 2193 | wi->wi_atareq.command = ata_c.r_status; |
| 2194 | wi->wi_atareq.features = ata_c.r_error; |
| 2195 | wi->wi_atareq.sec_count = ata_c.r_count; |
| 2196 | wi->wi_atareq.sec_num = ata_c.r_lba & 0xff; |
| 2197 | wi->wi_atareq.head = (ata_c.r_device & 0xf0) | |
| 2198 | ((ata_c.r_lba >> 24) & 0x0f); |
| 2199 | wi->wi_atareq.cylinder = (ata_c.r_lba >> 8) & 0xffff; |
| 2200 | wi->wi_atareq.error = ata_c.r_error; |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | bp->b_error = 0; |
| 2205 | biodone(bp); |
| 2206 | return; |
| 2207 | bad: |
| 2208 | bp->b_error = error; |
| 2209 | bp->b_resid = bp->b_bcount; |
| 2210 | biodone(bp); |
| 2211 | } |
| 2212 | |