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