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