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