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