sd.c revision 1.226 1 /* $NetBSD: sd.c,v 1.226 2004/09/17 23:43:17 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (at) dialix.oz.au)
41 * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 *
43 * TRW Financial Systems, in accordance with their agreement with Carnegie
44 * Mellon University, makes this software available to CMU to distribute
45 * or use in any manner that they see fit as long as this message is kept with
46 * the software. For this reason TFS also grants any other persons or
47 * organisations permission to use or modify this software.
48 *
49 * TFS supplies this software to be publicly redistributed
50 * on the understanding that TFS is not responsible for the correct
51 * functioning of this software in any circumstances.
52 *
53 * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.226 2004/09/17 23:43:17 mycroft Exp $");
58
59 #include "opt_scsi.h"
60 #include "rnd.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/file.h>
66 #include <sys/stat.h>
67 #include <sys/ioctl.h>
68 #include <sys/scsiio.h>
69 #include <sys/buf.h>
70 #include <sys/uio.h>
71 #include <sys/malloc.h>
72 #include <sys/errno.h>
73 #include <sys/device.h>
74 #include <sys/disklabel.h>
75 #include <sys/disk.h>
76 #include <sys/proc.h>
77 #include <sys/conf.h>
78 #include <sys/vnode.h>
79 #if NRND > 0
80 #include <sys/rnd.h>
81 #endif
82
83 #include <dev/scsipi/scsipi_all.h>
84 #include <dev/scsipi/scsi_all.h>
85 #include <dev/scsipi/scsipi_disk.h>
86 #include <dev/scsipi/scsi_disk.h>
87 #include <dev/scsipi/scsiconf.h>
88 #include <dev/scsipi/scsipi_base.h>
89 #include <dev/scsipi/sdvar.h>
90
91 #define SDUNIT(dev) DISKUNIT(dev)
92 #define SDPART(dev) DISKPART(dev)
93 #define SDMINOR(unit, part) DISKMINOR(unit, part)
94 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
95
96 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
97
98 static void sdminphys(struct buf *);
99 static void sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
100 static void sdgetdisklabel(struct sd_softc *);
101 static void sdstart(struct scsipi_periph *);
102 static void sdrestart(void *);
103 static void sddone(struct scsipi_xfer *, int);
104 static void sd_shutdown(void *);
105 static int sd_interpret_sense(struct scsipi_xfer *);
106
107 static int sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int,
108 int, int *);
109 static int sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int,
110 int);
111 static int sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *,
112 int);
113 static int sd_get_capacity(struct sd_softc *, struct disk_parms *, int);
114 static int sd_get_parms(struct sd_softc *, struct disk_parms *, int);
115 static int sd_get_parms_page4(struct sd_softc *, struct disk_parms *,
116 int);
117 static int sd_get_parms_page5(struct sd_softc *, struct disk_parms *,
118 int);
119
120 static int sd_flush(struct sd_softc *, int);
121 static int sd_getcache(struct sd_softc *, int *);
122 static int sd_setcache(struct sd_softc *, int);
123
124 static int sdmatch(struct device *, struct cfdata *, void *);
125 static void sdattach(struct device *, struct device *, void *);
126 static int sdactivate(struct device *, enum devact);
127 static int sddetach(struct device *, int);
128
129 CFATTACH_DECL(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
130 sdactivate);
131
132 extern struct cfdriver sd_cd;
133
134 static const struct scsipi_inquiry_pattern sd_patterns[] = {
135 {T_DIRECT, T_FIXED,
136 "", "", ""},
137 {T_DIRECT, T_REMOV,
138 "", "", ""},
139 {T_OPTICAL, T_FIXED,
140 "", "", ""},
141 {T_OPTICAL, T_REMOV,
142 "", "", ""},
143 {T_SIMPLE_DIRECT, T_FIXED,
144 "", "", ""},
145 {T_SIMPLE_DIRECT, T_REMOV,
146 "", "", ""},
147 };
148
149 static dev_type_open(sdopen);
150 static dev_type_close(sdclose);
151 static dev_type_read(sdread);
152 static dev_type_write(sdwrite);
153 static dev_type_ioctl(sdioctl);
154 static dev_type_strategy(sdstrategy);
155 static dev_type_dump(sddump);
156 static dev_type_size(sdsize);
157
158 const struct bdevsw sd_bdevsw = {
159 sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
160 };
161
162 const struct cdevsw sd_cdevsw = {
163 sdopen, sdclose, sdread, sdwrite, sdioctl,
164 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
165 };
166
167 static struct dkdriver sddkdriver = { sdstrategy };
168
169 static const struct scsipi_periphsw sd_switch = {
170 sd_interpret_sense, /* check our error handler first */
171 sdstart, /* have a queue, served by this */
172 NULL, /* have no async handler */
173 sddone, /* deal with stats at interrupt time */
174 };
175
176 struct sd_mode_sense_data {
177 /*
178 * XXX
179 * We are not going to parse this as-is -- it just has to be large
180 * enough.
181 */
182 union {
183 struct scsipi_mode_header small;
184 struct scsipi_mode_header_big big;
185 } header;
186 struct scsi_blk_desc blk_desc;
187 union scsi_disk_pages pages;
188 };
189
190 /*
191 * The routine called by the low level scsi routine when it discovers
192 * A device suitable for this driver
193 */
194 static int
195 sdmatch(struct device *parent, struct cfdata *match, void *aux)
196 {
197 struct scsipibus_attach_args *sa = aux;
198 int priority;
199
200 (void)scsipi_inqmatch(&sa->sa_inqbuf,
201 (caddr_t)sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
202 sizeof(sd_patterns[0]), &priority);
203
204 return (priority);
205 }
206
207 /*
208 * Attach routine common to atapi & scsi.
209 */
210 static void
211 sdattach(struct device *parent, struct device *self, void *aux)
212 {
213 struct sd_softc *sd = (void *)self;
214 struct scsipibus_attach_args *sa = aux;
215 struct scsipi_periph *periph = sa->sa_periph;
216 int error, result;
217 struct disk_parms *dp = &sd->params;
218 char pbuf[9];
219
220 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
221
222 lockinit(&sd->sc_lock, PRIBIO | PCATCH, "sdlock", 0, 0);
223
224 sd->type = (sa->sa_inqbuf.type & SID_TYPE);
225 if (sd->type == T_SIMPLE_DIRECT)
226 periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE;
227
228 if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
229 periph->periph_version == 0)
230 sd->flags |= SDF_ANCIENT;
231
232 bufq_alloc(&sd->buf_queue,
233 BUFQ_DISK_DEFAULT_STRAT()|BUFQ_SORT_RAWBLOCK);
234
235 callout_init(&sd->sc_callout);
236
237 /*
238 * Store information needed to contact our base driver
239 */
240 sd->sc_periph = periph;
241
242 periph->periph_dev = &sd->sc_dev;
243 periph->periph_switch = &sd_switch;
244
245 /*
246 * Increase our openings to the maximum-per-periph
247 * supported by the adapter. This will either be
248 * clamped down or grown by the adapter if necessary.
249 */
250 periph->periph_openings =
251 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
252 periph->periph_flags |= PERIPH_GROW_OPENINGS;
253
254 /*
255 * Initialize and attach the disk structure.
256 */
257 sd->sc_dk.dk_driver = &sddkdriver;
258 sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
259 disk_attach(&sd->sc_dk);
260
261 /*
262 * Use the subdriver to request information regarding the drive.
263 */
264 aprint_naive("\n");
265 aprint_normal("\n");
266
267 error = scsipi_test_unit_ready(periph,
268 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
269 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
270
271 if (error)
272 result = SDGP_RESULT_OFFLINE;
273 else
274 result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY);
275 aprint_normal("%s: ", sd->sc_dev.dv_xname);
276 switch (result) {
277 case SDGP_RESULT_OK:
278 format_bytes(pbuf, sizeof(pbuf),
279 (u_int64_t)dp->disksize * dp->blksize);
280 aprint_normal(
281 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors",
282 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
283 (unsigned long long)dp->disksize);
284 break;
285
286 case SDGP_RESULT_OFFLINE:
287 aprint_normal("drive offline");
288 break;
289
290 case SDGP_RESULT_UNFORMATTED:
291 aprint_normal("unformatted media");
292 break;
293
294 #ifdef DIAGNOSTIC
295 default:
296 panic("sdattach: unknown result from get_parms");
297 break;
298 #endif
299 }
300 aprint_normal("\n");
301
302 /*
303 * Establish a shutdown hook so that we can ensure that
304 * our data has actually made it onto the platter at
305 * shutdown time. Note that this relies on the fact
306 * that the shutdown hook code puts us at the head of
307 * the list (thus guaranteeing that our hook runs before
308 * our ancestors').
309 */
310 if ((sd->sc_sdhook =
311 shutdownhook_establish(sd_shutdown, sd)) == NULL)
312 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
313 sd->sc_dev.dv_xname);
314
315 #if NRND > 0
316 /*
317 * attach the device into the random source list
318 */
319 rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
320 RND_TYPE_DISK, 0);
321 #endif
322 }
323
324 static int
325 sdactivate(struct device *self, enum devact act)
326 {
327 int rv = 0;
328
329 switch (act) {
330 case DVACT_ACTIVATE:
331 rv = EOPNOTSUPP;
332 break;
333
334 case DVACT_DEACTIVATE:
335 /*
336 * Nothing to do; we key off the device's DVF_ACTIVE.
337 */
338 break;
339 }
340 return (rv);
341 }
342
343 static int
344 sddetach(struct device *self, int flags)
345 {
346 struct sd_softc *sd = (struct sd_softc *) self;
347 struct buf *bp;
348 int s, bmaj, cmaj, i, mn;
349
350 /* locate the major number */
351 bmaj = bdevsw_lookup_major(&sd_bdevsw);
352 cmaj = cdevsw_lookup_major(&sd_cdevsw);
353
354 /* Nuke the vnodes for any open instances */
355 for (i = 0; i < MAXPARTITIONS; i++) {
356 mn = SDMINOR(self->dv_unit, i);
357 vdevgone(bmaj, mn, mn, VBLK);
358 vdevgone(cmaj, mn, mn, VCHR);
359 }
360
361 /* kill any pending restart */
362 callout_stop(&sd->sc_callout);
363
364 s = splbio();
365
366 /* Kill off any queued buffers. */
367 while ((bp = BUFQ_GET(&sd->buf_queue)) != NULL) {
368 bp->b_error = EIO;
369 bp->b_flags |= B_ERROR;
370 bp->b_resid = bp->b_bcount;
371 biodone(bp);
372 }
373
374 bufq_free(&sd->buf_queue);
375
376 /* Kill off any pending commands. */
377 scsipi_kill_pending(sd->sc_periph);
378
379 splx(s);
380
381 lockmgr(&sd->sc_lock, LK_DRAIN, 0);
382
383 /* Detach from the disk list. */
384 disk_detach(&sd->sc_dk);
385
386 /* Get rid of the shutdown hook. */
387 shutdownhook_disestablish(sd->sc_sdhook);
388
389 #if NRND > 0
390 /* Unhook the entropy source. */
391 rnd_detach_source(&sd->rnd_source);
392 #endif
393
394 return (0);
395 }
396
397 /*
398 * open the device. Make sure the partition info is a up-to-date as can be.
399 */
400 static int
401 sdopen(dev_t dev, int flag, int fmt, struct proc *p)
402 {
403 struct sd_softc *sd;
404 struct scsipi_periph *periph;
405 struct scsipi_adapter *adapt;
406 int unit, part;
407 int error;
408
409 unit = SDUNIT(dev);
410 if (unit >= sd_cd.cd_ndevs)
411 return (ENXIO);
412 sd = sd_cd.cd_devs[unit];
413 if (sd == NULL)
414 return (ENXIO);
415
416 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
417 return (ENODEV);
418
419 periph = sd->sc_periph;
420 adapt = periph->periph_channel->chan_adapter;
421 part = SDPART(dev);
422
423 SC_DEBUG(periph, SCSIPI_DB1,
424 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
425 sd_cd.cd_ndevs, part));
426
427 /*
428 * If this is the first open of this device, add a reference
429 * to the adapter.
430 */
431 if (sd->sc_dk.dk_openmask == 0 &&
432 (error = scsipi_adapter_addref(adapt)) != 0)
433 return (error);
434
435 if ((error = lockmgr(&sd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
436 goto bad4;
437
438 if ((periph->periph_flags & PERIPH_OPEN) != 0) {
439 /*
440 * If any partition is open, but the disk has been invalidated,
441 * disallow further opens of non-raw partition
442 */
443 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
444 (part != RAW_PART || fmt != S_IFCHR)) {
445 error = EIO;
446 goto bad3;
447 }
448 } else {
449 int silent;
450
451 if (part == RAW_PART && fmt == S_IFCHR)
452 silent = XS_CTL_SILENT;
453 else
454 silent = 0;
455
456 /* Check that it is still responding and ok. */
457 error = scsipi_test_unit_ready(periph,
458 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
459 silent);
460
461 /*
462 * Start the pack spinning if necessary. Always allow the
463 * raw parition to be opened, for raw IOCTLs. Data transfers
464 * will check for SDEV_MEDIA_LOADED.
465 */
466 if (error == EIO) {
467 int error2;
468
469 error2 = scsipi_start(periph, SSS_START, silent);
470 switch (error2) {
471 case 0:
472 error = 0;
473 break;
474 case EIO:
475 case EINVAL:
476 break;
477 default:
478 error = error2;
479 break;
480 }
481 }
482 if (error) {
483 if (silent)
484 goto out;
485 goto bad3;
486 }
487
488 periph->periph_flags |= PERIPH_OPEN;
489
490 if (periph->periph_flags & PERIPH_REMOVABLE) {
491 /* Lock the pack in. */
492 error = scsipi_prevent(periph, PR_PREVENT,
493 XS_CTL_IGNORE_ILLEGAL_REQUEST |
494 XS_CTL_IGNORE_MEDIA_CHANGE);
495 if (error)
496 goto bad;
497 }
498
499 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
500 int param_error;
501 periph->periph_flags |= PERIPH_MEDIA_LOADED;
502
503 /*
504 * Load the physical device parameters.
505 *
506 * Note that if media is present but unformatted,
507 * we allow the open (so that it can be formatted!).
508 * The drive should refuse real I/O, if the media is
509 * unformatted.
510 */
511 if ((param_error = sd_get_parms(sd, &sd->params, 0))
512 == SDGP_RESULT_OFFLINE) {
513 error = ENXIO;
514 goto bad2;
515 }
516 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
517
518 /* Load the partition info if not already loaded. */
519 if (param_error == 0) {
520 sdgetdisklabel(sd);
521 SC_DEBUG(periph, SCSIPI_DB3,
522 ("Disklabel loaded "));
523 }
524 }
525 }
526
527 /* Check that the partition exists. */
528 if (part != RAW_PART &&
529 (part >= sd->sc_dk.dk_label->d_npartitions ||
530 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
531 error = ENXIO;
532 goto bad;
533 }
534
535 out: /* Insure only one open at a time. */
536 switch (fmt) {
537 case S_IFCHR:
538 sd->sc_dk.dk_copenmask |= (1 << part);
539 break;
540 case S_IFBLK:
541 sd->sc_dk.dk_bopenmask |= (1 << part);
542 break;
543 }
544 sd->sc_dk.dk_openmask =
545 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
546
547 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
548 lockmgr(&sd->sc_lock, LK_RELEASE, NULL);
549 return (0);
550
551 bad2:
552 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
553
554 bad:
555 if (sd->sc_dk.dk_openmask == 0) {
556 if (periph->periph_flags & PERIPH_REMOVABLE)
557 scsipi_prevent(periph, PR_ALLOW,
558 XS_CTL_IGNORE_ILLEGAL_REQUEST |
559 XS_CTL_IGNORE_MEDIA_CHANGE);
560 periph->periph_flags &= ~PERIPH_OPEN;
561 }
562
563 bad3:
564 lockmgr(&sd->sc_lock, LK_RELEASE, NULL);
565 bad4:
566 if (sd->sc_dk.dk_openmask == 0)
567 scsipi_adapter_delref(adapt);
568 return (error);
569 }
570
571 /*
572 * close the device.. only called if we are the LAST occurence of an open
573 * device. Convenient now but usually a pain.
574 */
575 static int
576 sdclose(dev_t dev, int flag, int fmt, struct proc *p)
577 {
578 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
579 struct scsipi_periph *periph = sd->sc_periph;
580 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
581 int part = SDPART(dev);
582 int error;
583
584 if ((error = lockmgr(&sd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
585 return (error);
586
587 switch (fmt) {
588 case S_IFCHR:
589 sd->sc_dk.dk_copenmask &= ~(1 << part);
590 break;
591 case S_IFBLK:
592 sd->sc_dk.dk_bopenmask &= ~(1 << part);
593 break;
594 }
595 sd->sc_dk.dk_openmask =
596 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
597
598 if (sd->sc_dk.dk_openmask == 0) {
599 /*
600 * If the disk cache needs flushing, and the disk supports
601 * it, do it now.
602 */
603 if ((sd->flags & SDF_DIRTY) != 0) {
604 if (sd_flush(sd, 0)) {
605 printf("%s: cache synchronization failed\n",
606 sd->sc_dev.dv_xname);
607 sd->flags &= ~SDF_FLUSHING;
608 } else
609 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
610 }
611
612 if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
613 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
614
615 scsipi_wait_drain(periph);
616
617 if (periph->periph_flags & PERIPH_REMOVABLE)
618 scsipi_prevent(periph, PR_ALLOW,
619 XS_CTL_IGNORE_ILLEGAL_REQUEST |
620 XS_CTL_IGNORE_NOT_READY);
621 periph->periph_flags &= ~PERIPH_OPEN;
622
623 scsipi_wait_drain(periph);
624
625 scsipi_adapter_delref(adapt);
626 }
627
628 lockmgr(&sd->sc_lock, LK_RELEASE, NULL);
629 return (0);
630 }
631
632 /*
633 * Actually translate the requested transfer into one the physical driver
634 * can understand. The transfer is described by a buf and will include
635 * only one physical transfer.
636 */
637 static void
638 sdstrategy(struct buf *bp)
639 {
640 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
641 struct scsipi_periph *periph = sd->sc_periph;
642 struct disklabel *lp;
643 daddr_t blkno;
644 int s;
645 boolean_t sector_aligned;
646
647 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
648 SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
649 ("%ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
650 /*
651 * If the device has been made invalid, error out
652 */
653 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
654 (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
655 if (periph->periph_flags & PERIPH_OPEN)
656 bp->b_error = EIO;
657 else
658 bp->b_error = ENODEV;
659 goto bad;
660 }
661
662 lp = sd->sc_dk.dk_label;
663
664 /*
665 * The transfer must be a whole number of blocks, offset must not be
666 * negative.
667 */
668 if (lp->d_secsize == DEV_BSIZE) {
669 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
670 } else {
671 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
672 }
673 if (!sector_aligned || bp->b_blkno < 0) {
674 bp->b_error = EINVAL;
675 goto bad;
676 }
677 /*
678 * If it's a null transfer, return immediatly
679 */
680 if (bp->b_bcount == 0)
681 goto done;
682
683 /*
684 * Do bounds checking, adjust transfer. if error, process.
685 * If end of partition, just return.
686 */
687 if (SDPART(bp->b_dev) == RAW_PART) {
688 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
689 sd->params.disksize512) <= 0)
690 goto done;
691 } else {
692 if (bounds_check_with_label(&sd->sc_dk, bp,
693 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
694 goto done;
695 }
696
697 /*
698 * Now convert the block number to absolute and put it in
699 * terms of the device's logical block size.
700 */
701 if (lp->d_secsize == DEV_BSIZE)
702 blkno = bp->b_blkno;
703 else if (lp->d_secsize > DEV_BSIZE)
704 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
705 else
706 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
707
708 if (SDPART(bp->b_dev) != RAW_PART)
709 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
710
711 bp->b_rawblkno = blkno;
712
713 s = splbio();
714
715 /*
716 * Place it in the queue of disk activities for this disk.
717 *
718 * XXX Only do disksort() if the current operating mode does not
719 * XXX include tagged queueing.
720 */
721 BUFQ_PUT(&sd->buf_queue, bp);
722
723 /*
724 * Tell the device to get going on the transfer if it's
725 * not doing anything, otherwise just wait for completion
726 */
727 sdstart(sd->sc_periph);
728
729 splx(s);
730 return;
731
732 bad:
733 bp->b_flags |= B_ERROR;
734 done:
735 /*
736 * Correctly set the buf to indicate a completed xfer
737 */
738 bp->b_resid = bp->b_bcount;
739 biodone(bp);
740 }
741
742 /*
743 * sdstart looks to see if there is a buf waiting for the device
744 * and that the device is not already busy. If both are true,
745 * It dequeues the buf and creates a scsi command to perform the
746 * transfer in the buf. The transfer request will call scsipi_done
747 * on completion, which will in turn call this routine again
748 * so that the next queued transfer is performed.
749 * The bufs are queued by the strategy routine (sdstrategy)
750 *
751 * This routine is also called after other non-queued requests
752 * have been made of the scsi driver, to ensure that the queue
753 * continues to be drained.
754 *
755 * must be called at the correct (highish) spl level
756 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
757 */
758 static void
759 sdstart(struct scsipi_periph *periph)
760 {
761 struct sd_softc *sd = (void *)periph->periph_dev;
762 struct disklabel *lp = sd->sc_dk.dk_label;
763 struct buf *bp = 0;
764 struct scsipi_rw_big cmd_big;
765 struct scsi_rw cmd_small;
766 struct scsipi_generic *cmdp;
767 struct scsipi_xfer *xs;
768 int nblks, cmdlen, error, flags;
769
770 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
771 /*
772 * Check if the device has room for another command
773 */
774 while (periph->periph_active < periph->periph_openings) {
775 /*
776 * there is excess capacity, but a special waits
777 * It'll need the adapter as soon as we clear out of the
778 * way and let it run (user level wait).
779 */
780 if (periph->periph_flags & PERIPH_WAITING) {
781 periph->periph_flags &= ~PERIPH_WAITING;
782 wakeup((caddr_t)periph);
783 return;
784 }
785
786 /*
787 * If the device has become invalid, abort all the
788 * reads and writes until all files have been closed and
789 * re-opened
790 */
791 if (__predict_false(
792 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
793 if ((bp = BUFQ_GET(&sd->buf_queue)) != NULL) {
794 bp->b_error = EIO;
795 bp->b_flags |= B_ERROR;
796 bp->b_resid = bp->b_bcount;
797 biodone(bp);
798 continue;
799 } else {
800 return;
801 }
802 }
803
804 /*
805 * See if there is a buf with work for us to do..
806 */
807 if ((bp = BUFQ_PEEK(&sd->buf_queue)) == NULL)
808 return;
809
810 /*
811 * We have a buf, now we should make a command.
812 */
813
814 if (lp->d_secsize == DEV_BSIZE)
815 nblks = bp->b_bcount >> DEV_BSHIFT;
816 else
817 nblks = howmany(bp->b_bcount, lp->d_secsize);
818
819 /*
820 * Fill out the scsi command. If the transfer will
821 * fit in a "small" cdb, use it.
822 */
823 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
824 ((nblks & 0xff) == nblks) &&
825 !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
826 /*
827 * We can fit in a small cdb.
828 */
829 memset(&cmd_small, 0, sizeof(cmd_small));
830 cmd_small.opcode = (bp->b_flags & B_READ) ?
831 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
832 _lto3b(bp->b_rawblkno, cmd_small.addr);
833 cmd_small.length = nblks & 0xff;
834 cmdlen = sizeof(cmd_small);
835 cmdp = (struct scsipi_generic *)&cmd_small;
836 } else {
837 /*
838 * Need a large cdb.
839 */
840 memset(&cmd_big, 0, sizeof(cmd_big));
841 cmd_big.opcode = (bp->b_flags & B_READ) ?
842 READ_BIG : WRITE_BIG;
843 _lto4b(bp->b_rawblkno, cmd_big.addr);
844 _lto2b(nblks, cmd_big.length);
845 cmdlen = sizeof(cmd_big);
846 cmdp = (struct scsipi_generic *)&cmd_big;
847 }
848
849 /* Instrumentation. */
850 disk_busy(&sd->sc_dk);
851
852 /*
853 * Mark the disk dirty so that the cache will be
854 * flushed on close.
855 */
856 if ((bp->b_flags & B_READ) == 0)
857 sd->flags |= SDF_DIRTY;
858
859 /*
860 * Figure out what flags to use.
861 */
862 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
863 if (bp->b_flags & B_READ)
864 flags |= XS_CTL_DATA_IN;
865 else
866 flags |= XS_CTL_DATA_OUT;
867
868 /*
869 * Call the routine that chats with the adapter.
870 * Note: we cannot sleep as we may be an interrupt
871 */
872 xs = scsipi_make_xs(periph, cmdp, cmdlen,
873 (u_char *)bp->b_data, bp->b_bcount,
874 SDRETRIES, SD_IO_TIMEOUT, bp, flags);
875 if (__predict_false(xs == NULL)) {
876 /*
877 * out of memory. Keep this buffer in the queue, and
878 * retry later.
879 */
880 callout_reset(&sd->sc_callout, hz / 2, sdrestart,
881 periph);
882 return;
883 }
884 /*
885 * need to dequeue the buffer before queuing the command,
886 * because cdstart may be called recursively from the
887 * HBA driver
888 */
889 #ifdef DIAGNOSTIC
890 if (BUFQ_GET(&sd->buf_queue) != bp)
891 panic("sdstart(): dequeued wrong buf");
892 #else
893 BUFQ_GET(&sd->buf_queue);
894 #endif
895 error = scsipi_execute_xs(xs);
896 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
897 KASSERT(error == 0);
898 }
899 }
900
901 static void
902 sdrestart(void *v)
903 {
904 int s = splbio();
905 sdstart((struct scsipi_periph *)v);
906 splx(s);
907 }
908
909 static void
910 sddone(struct scsipi_xfer *xs, int error)
911 {
912 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
913 struct buf *bp = xs->bp;
914
915 if (sd->flags & SDF_FLUSHING) {
916 /* Flush completed, no longer dirty. */
917 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
918 }
919
920 if (bp) {
921 bp->b_error = error;
922 bp->b_resid = xs->resid;
923 if (error)
924 bp->b_flags |= B_ERROR;
925
926 disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
927 (bp->b_flags & B_READ));
928 #if NRND > 0
929 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
930 #endif
931
932 biodone(bp);
933 }
934 }
935
936 static void
937 sdminphys(struct buf *bp)
938 {
939 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
940 long max;
941
942 /*
943 * If the device is ancient, we want to make sure that
944 * the transfer fits into a 6-byte cdb.
945 *
946 * XXX Note that the SCSI-I spec says that 256-block transfers
947 * are allowed in a 6-byte read/write, and are specified
948 * by settng the "length" to 0. However, we're conservative
949 * here, allowing only 255-block transfers in case an
950 * ancient device gets confused by length == 0. A length of 0
951 * in a 10-byte read/write actually means 0 blocks.
952 */
953 if ((sd->flags & SDF_ANCIENT) &&
954 ((sd->sc_periph->periph_flags &
955 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
956 max = sd->sc_dk.dk_label->d_secsize * 0xff;
957
958 if (bp->b_bcount > max)
959 bp->b_bcount = max;
960 }
961
962 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
963 }
964
965 static int
966 sdread(dev_t dev, struct uio *uio, int ioflag)
967 {
968
969 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
970 }
971
972 static int
973 sdwrite(dev_t dev, struct uio *uio, int ioflag)
974 {
975
976 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
977 }
978
979 /*
980 * Perform special action on behalf of the user
981 * Knows about the internals of this device
982 */
983 static int
984 sdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
985 {
986 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
987 struct scsipi_periph *periph = sd->sc_periph;
988 int part = SDPART(dev);
989 int error = 0;
990 #ifdef __HAVE_OLD_DISKLABEL
991 struct disklabel *newlabel = NULL;
992 #endif
993
994 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
995
996 /*
997 * If the device is not valid, some IOCTLs can still be
998 * handled on the raw partition. Check this here.
999 */
1000 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1001 switch (cmd) {
1002 case DIOCKLABEL:
1003 case DIOCWLABEL:
1004 case DIOCLOCK:
1005 case DIOCEJECT:
1006 case ODIOCEJECT:
1007 case DIOCGCACHE:
1008 case DIOCSCACHE:
1009 case SCIOCIDENTIFY:
1010 case OSCIOCIDENTIFY:
1011 case SCIOCCOMMAND:
1012 case SCIOCDEBUG:
1013 if (part == RAW_PART)
1014 break;
1015 /* FALLTHROUGH */
1016 default:
1017 if ((periph->periph_flags & PERIPH_OPEN) == 0)
1018 return (ENODEV);
1019 else
1020 return (EIO);
1021 }
1022 }
1023
1024 switch (cmd) {
1025 case DIOCGDINFO:
1026 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1027 return (0);
1028
1029 #ifdef __HAVE_OLD_DISKLABEL
1030 case ODIOCGDINFO:
1031 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1032 if (newlabel == NULL)
1033 return EIO;
1034 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1035 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1036 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1037 else
1038 error = ENOTTY;
1039 free(newlabel, M_TEMP);
1040 return error;
1041 #endif
1042
1043 case DIOCGPART:
1044 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1045 ((struct partinfo *)addr)->part =
1046 &sd->sc_dk.dk_label->d_partitions[part];
1047 return (0);
1048
1049 case DIOCWDINFO:
1050 case DIOCSDINFO:
1051 #ifdef __HAVE_OLD_DISKLABEL
1052 case ODIOCWDINFO:
1053 case ODIOCSDINFO:
1054 #endif
1055 {
1056 struct disklabel *lp;
1057
1058 if ((flag & FWRITE) == 0)
1059 return (EBADF);
1060
1061 #ifdef __HAVE_OLD_DISKLABEL
1062 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1063 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1064 if (newlabel == NULL)
1065 return EIO;
1066 memset(newlabel, 0, sizeof newlabel);
1067 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1068 lp = newlabel;
1069 } else
1070 #endif
1071 lp = (struct disklabel *)addr;
1072
1073 if ((error = lockmgr(&sd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
1074 goto bad;
1075 sd->flags |= SDF_LABELLING;
1076
1077 error = setdisklabel(sd->sc_dk.dk_label,
1078 lp, /*sd->sc_dk.dk_openmask : */0,
1079 sd->sc_dk.dk_cpulabel);
1080 if (error == 0) {
1081 if (cmd == DIOCWDINFO
1082 #ifdef __HAVE_OLD_DISKLABEL
1083 || cmd == ODIOCWDINFO
1084 #endif
1085 )
1086 error = writedisklabel(SDLABELDEV(dev),
1087 sdstrategy, sd->sc_dk.dk_label,
1088 sd->sc_dk.dk_cpulabel);
1089 }
1090
1091 sd->flags &= ~SDF_LABELLING;
1092 lockmgr(&sd->sc_lock, LK_RELEASE, NULL);
1093 bad:
1094 #ifdef __HAVE_OLD_DISKLABEL
1095 if (newlabel != NULL)
1096 free(newlabel, M_TEMP);
1097 #endif
1098 return (error);
1099 }
1100
1101 case DIOCKLABEL:
1102 if (*(int *)addr)
1103 periph->periph_flags |= PERIPH_KEEP_LABEL;
1104 else
1105 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1106 return (0);
1107
1108 case DIOCWLABEL:
1109 if ((flag & FWRITE) == 0)
1110 return (EBADF);
1111 if (*(int *)addr)
1112 sd->flags |= SDF_WLABEL;
1113 else
1114 sd->flags &= ~SDF_WLABEL;
1115 return (0);
1116
1117 case DIOCLOCK:
1118 return (scsipi_prevent(periph,
1119 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1120
1121 case DIOCEJECT:
1122 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1123 return (ENOTTY);
1124 if (*(int *)addr == 0) {
1125 /*
1126 * Don't force eject: check that we are the only
1127 * partition open. If so, unlock it.
1128 */
1129 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1130 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1131 sd->sc_dk.dk_openmask) {
1132 error = scsipi_prevent(periph, PR_ALLOW,
1133 XS_CTL_IGNORE_NOT_READY);
1134 if (error)
1135 return (error);
1136 } else {
1137 return (EBUSY);
1138 }
1139 }
1140 /* FALLTHROUGH */
1141 case ODIOCEJECT:
1142 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1143 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1144
1145 case DIOCGDEFLABEL:
1146 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1147 return (0);
1148
1149 #ifdef __HAVE_OLD_DISKLABEL
1150 case ODIOCGDEFLABEL:
1151 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1152 if (newlabel == NULL)
1153 return EIO;
1154 sdgetdefaultlabel(sd, newlabel);
1155 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1156 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1157 else
1158 error = ENOTTY;
1159 free(newlabel, M_TEMP);
1160 return error;
1161 #endif
1162
1163 case DIOCGCACHE:
1164 return (sd_getcache(sd, (int *) addr));
1165
1166 case DIOCSCACHE:
1167 if ((flag & FWRITE) == 0)
1168 return (EBADF);
1169 return (sd_setcache(sd, *(int *) addr));
1170
1171 case DIOCCACHESYNC:
1172 /*
1173 * XXX Do we really need to care about having a writable
1174 * file descriptor here?
1175 */
1176 if ((flag & FWRITE) == 0)
1177 return (EBADF);
1178 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1179 error = sd_flush(sd, 0);
1180 if (error)
1181 sd->flags &= ~SDF_FLUSHING;
1182 else
1183 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1184 } else
1185 error = 0;
1186 return (error);
1187
1188 default:
1189 if (part != RAW_PART)
1190 return (ENOTTY);
1191 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1192 }
1193
1194 #ifdef DIAGNOSTIC
1195 panic("sdioctl: impossible");
1196 #endif
1197 }
1198
1199 static void
1200 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1201 {
1202
1203 memset(lp, 0, sizeof(struct disklabel));
1204
1205 lp->d_secsize = sd->params.blksize;
1206 lp->d_ntracks = sd->params.heads;
1207 lp->d_nsectors = sd->params.sectors;
1208 lp->d_ncylinders = sd->params.cyls;
1209 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1210
1211 switch (scsipi_periph_bustype(sd->sc_periph)) {
1212 case SCSIPI_BUSTYPE_SCSI:
1213 lp->d_type = DTYPE_SCSI;
1214 break;
1215 case SCSIPI_BUSTYPE_ATAPI:
1216 lp->d_type = DTYPE_ATAPI;
1217 break;
1218 }
1219 /*
1220 * XXX
1221 * We could probe the mode pages to figure out what kind of disc it is.
1222 * Is this worthwhile?
1223 */
1224 strncpy(lp->d_typename, "mydisk", 16);
1225 strncpy(lp->d_packname, "fictitious", 16);
1226 lp->d_secperunit = sd->params.disksize;
1227 lp->d_rpm = sd->params.rot_rate;
1228 lp->d_interleave = 1;
1229 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1230 D_REMOVABLE : 0;
1231
1232 lp->d_partitions[RAW_PART].p_offset = 0;
1233 lp->d_partitions[RAW_PART].p_size =
1234 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1235 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1236 lp->d_npartitions = RAW_PART + 1;
1237
1238 lp->d_magic = DISKMAGIC;
1239 lp->d_magic2 = DISKMAGIC;
1240 lp->d_checksum = dkcksum(lp);
1241 }
1242
1243
1244 /*
1245 * Load the label information on the named device
1246 */
1247 static void
1248 sdgetdisklabel(struct sd_softc *sd)
1249 {
1250 struct disklabel *lp = sd->sc_dk.dk_label;
1251 const char *errstring;
1252
1253 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1254
1255 sdgetdefaultlabel(sd, lp);
1256
1257 if (lp->d_secpercyl == 0) {
1258 lp->d_secpercyl = 100;
1259 /* as long as it's not 0 - readdisklabel divides by it (?) */
1260 }
1261
1262 /*
1263 * Call the generic disklabel extraction routine
1264 */
1265 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
1266 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1267 if (errstring) {
1268 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1269 return;
1270 }
1271 }
1272
1273 static void
1274 sd_shutdown(void *arg)
1275 {
1276 struct sd_softc *sd = arg;
1277
1278 /*
1279 * If the disk cache needs to be flushed, and the disk supports
1280 * it, flush it. We're cold at this point, so we poll for
1281 * completion.
1282 */
1283 if ((sd->flags & SDF_DIRTY) != 0) {
1284 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1285 printf("%s: cache synchronization failed\n",
1286 sd->sc_dev.dv_xname);
1287 sd->flags &= ~SDF_FLUSHING;
1288 } else
1289 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1290 }
1291 }
1292
1293 /*
1294 * Check Errors
1295 */
1296 static int
1297 sd_interpret_sense(struct scsipi_xfer *xs)
1298 {
1299 struct scsipi_periph *periph = xs->xs_periph;
1300 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1301 struct sd_softc *sd = (void *)periph->periph_dev;
1302 int s, error, retval = EJUSTRETURN;
1303
1304 /*
1305 * If the periph is already recovering, just do the normal
1306 * error processing.
1307 */
1308 if (periph->periph_flags & PERIPH_RECOVERING)
1309 return (retval);
1310
1311 /*
1312 * If the device is not open yet, let the generic code handle it.
1313 */
1314 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1315 return (retval);
1316
1317 /*
1318 * If it isn't a extended or extended/deferred error, let
1319 * the generic code handle it.
1320 */
1321 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1322 (sense->error_code & SSD_ERRCODE) != 0x71)
1323 return (retval);
1324
1325 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1326 sense->add_sense_code == 0x4) {
1327 if (sense->add_sense_code_qual == 0x01) {
1328 /*
1329 * Unit In The Process Of Becoming Ready.
1330 */
1331 printf("%s: waiting for pack to spin up...\n",
1332 sd->sc_dev.dv_xname);
1333 if (!callout_pending(&periph->periph_callout))
1334 scsipi_periph_freeze(periph, 1);
1335 callout_reset(&periph->periph_callout,
1336 5 * hz, scsipi_periph_timed_thaw, periph);
1337 retval = ERESTART;
1338 } else if (sense->add_sense_code_qual == 0x02) {
1339 printf("%s: pack is stopped, restarting...\n",
1340 sd->sc_dev.dv_xname);
1341 s = splbio();
1342 periph->periph_flags |= PERIPH_RECOVERING;
1343 splx(s);
1344 error = scsipi_start(periph, SSS_START,
1345 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1346 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1347 if (error) {
1348 printf("%s: unable to restart pack\n",
1349 sd->sc_dev.dv_xname);
1350 retval = error;
1351 } else
1352 retval = ERESTART;
1353 s = splbio();
1354 periph->periph_flags &= ~PERIPH_RECOVERING;
1355 splx(s);
1356 }
1357 }
1358 if ((sense->flags & SSD_KEY) == SKEY_MEDIUM_ERROR &&
1359 sense->add_sense_code == 0x31 &&
1360 sense->add_sense_code_qual == 0x00) { /* maybe for any asq ? */
1361 /* Medium Format Corrupted */
1362 retval = EFTYPE;
1363 }
1364 return (retval);
1365 }
1366
1367
1368 static int
1369 sdsize(dev_t dev)
1370 {
1371 struct sd_softc *sd;
1372 int part, unit, omask;
1373 int size;
1374
1375 unit = SDUNIT(dev);
1376 if (unit >= sd_cd.cd_ndevs)
1377 return (-1);
1378 sd = sd_cd.cd_devs[unit];
1379 if (sd == NULL)
1380 return (-1);
1381
1382 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1383 return (-1);
1384
1385 part = SDPART(dev);
1386 omask = sd->sc_dk.dk_openmask & (1 << part);
1387
1388 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1389 return (-1);
1390 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1391 size = -1;
1392 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1393 size = -1;
1394 else
1395 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1396 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1397 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1398 return (-1);
1399 return (size);
1400 }
1401
1402 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1403 static struct scsipi_xfer sx;
1404 static int sddoingadump;
1405
1406 /*
1407 * dump all of physical memory into the partition specified, starting
1408 * at offset 'dumplo' into the partition.
1409 */
1410 static int
1411 sddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1412 {
1413 struct sd_softc *sd; /* disk unit to do the I/O */
1414 struct disklabel *lp; /* disk's disklabel */
1415 int unit, part;
1416 int sectorsize; /* size of a disk sector */
1417 int nsects; /* number of sectors in partition */
1418 int sectoff; /* sector offset of partition */
1419 int totwrt; /* total number of sectors left to write */
1420 int nwrt; /* current number of sectors to write */
1421 struct scsipi_rw_big cmd; /* write command */
1422 struct scsipi_xfer *xs; /* ... convenience */
1423 struct scsipi_periph *periph;
1424 struct scsipi_channel *chan;
1425
1426 /* Check if recursive dump; if so, punt. */
1427 if (sddoingadump)
1428 return (EFAULT);
1429
1430 /* Mark as active early. */
1431 sddoingadump = 1;
1432
1433 unit = SDUNIT(dev); /* Decompose unit & partition. */
1434 part = SDPART(dev);
1435
1436 /* Check for acceptable drive number. */
1437 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1438 return (ENXIO);
1439
1440 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1441 return (ENODEV);
1442
1443 periph = sd->sc_periph;
1444 chan = periph->periph_channel;
1445
1446 /* Make sure it was initialized. */
1447 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1448 return (ENXIO);
1449
1450 /* Convert to disk sectors. Request must be a multiple of size. */
1451 lp = sd->sc_dk.dk_label;
1452 sectorsize = lp->d_secsize;
1453 if ((size % sectorsize) != 0)
1454 return (EFAULT);
1455 totwrt = size / sectorsize;
1456 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1457
1458 nsects = lp->d_partitions[part].p_size;
1459 sectoff = lp->d_partitions[part].p_offset;
1460
1461 /* Check transfer bounds against partition size. */
1462 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1463 return (EINVAL);
1464
1465 /* Offset block number to start of partition. */
1466 blkno += sectoff;
1467
1468 xs = &sx;
1469
1470 while (totwrt > 0) {
1471 nwrt = totwrt; /* XXX */
1472 #ifndef SD_DUMP_NOT_TRUSTED
1473 /*
1474 * Fill out the scsi command
1475 */
1476 memset(&cmd, 0, sizeof(cmd));
1477 cmd.opcode = WRITE_BIG;
1478 _lto4b(blkno, cmd.addr);
1479 _lto2b(nwrt, cmd.length);
1480 /*
1481 * Fill out the scsipi_xfer structure
1482 * Note: we cannot sleep as we may be an interrupt
1483 * don't use scsipi_command() as it may want to wait
1484 * for an xs.
1485 */
1486 memset(xs, 0, sizeof(sx));
1487 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1488 XS_CTL_DATA_OUT;
1489 xs->xs_status = 0;
1490 xs->xs_periph = periph;
1491 xs->xs_retries = SDRETRIES;
1492 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1493 xs->cmd = (struct scsipi_generic *)&cmd;
1494 xs->cmdlen = sizeof(cmd);
1495 xs->resid = nwrt * sectorsize;
1496 xs->error = XS_NOERROR;
1497 xs->bp = 0;
1498 xs->data = va;
1499 xs->datalen = nwrt * sectorsize;
1500
1501 /*
1502 * Pass all this info to the scsi driver.
1503 */
1504 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1505 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1506 xs->error != XS_NOERROR)
1507 return (EIO);
1508 #else /* SD_DUMP_NOT_TRUSTED */
1509 /* Let's just talk about this first... */
1510 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1511 delay(500 * 1000); /* half a second */
1512 #endif /* SD_DUMP_NOT_TRUSTED */
1513
1514 /* update block count */
1515 totwrt -= nwrt;
1516 blkno += nwrt;
1517 va += sectorsize * nwrt;
1518 }
1519 sddoingadump = 0;
1520 return (0);
1521 }
1522
1523 static int
1524 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1525 int page, int flags, int *big)
1526 {
1527
1528 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1529 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1530 *big = 1;
1531 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1532 size + sizeof(struct scsipi_mode_header_big),
1533 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1534 } else {
1535 *big = 0;
1536 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1537 size + sizeof(struct scsipi_mode_header),
1538 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1539 }
1540 }
1541
1542 static int
1543 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1544 int flags, int big)
1545 {
1546
1547 if (big) {
1548 struct scsipi_mode_header_big *header = sense;
1549
1550 _lto2b(0, header->data_length);
1551 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1552 size + sizeof(struct scsipi_mode_header_big),
1553 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1554 } else {
1555 struct scsipi_mode_header *header = sense;
1556
1557 header->data_length = 0;
1558 return scsipi_mode_select(sd->sc_periph, byte2, sense,
1559 size + sizeof(struct scsipi_mode_header),
1560 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1561 }
1562 }
1563
1564 static int
1565 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1566 {
1567 struct {
1568 struct scsipi_mode_header header;
1569 /* no block descriptor */
1570 u_int8_t pg_code; /* page code (should be 6) */
1571 u_int8_t pg_length; /* page length (should be 11) */
1572 u_int8_t wcd; /* bit0: cache disable */
1573 u_int8_t lbs[2]; /* logical block size */
1574 u_int8_t size[5]; /* number of log. blocks */
1575 u_int8_t pp; /* power/performance */
1576 u_int8_t flags;
1577 u_int8_t resvd;
1578 } scsipi_sense;
1579 u_int64_t sectors;
1580 int error;
1581
1582 /*
1583 * scsipi_size (ie "read capacity") and mode sense page 6
1584 * give the same information. Do both for now, and check
1585 * for consistency.
1586 * XXX probably differs for removable media
1587 */
1588 dp->blksize = 512;
1589 if ((sectors = scsipi_size(sd->sc_periph, flags)) == 0)
1590 return (SDGP_RESULT_OFFLINE); /* XXX? */
1591
1592 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1593 &scsipi_sense.header, sizeof(scsipi_sense),
1594 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1595
1596 if (error != 0)
1597 return (SDGP_RESULT_OFFLINE); /* XXX? */
1598
1599 dp->blksize = _2btol(scsipi_sense.lbs);
1600 if (dp->blksize == 0)
1601 dp->blksize = 512;
1602
1603 /*
1604 * Create a pseudo-geometry.
1605 */
1606 dp->heads = 64;
1607 dp->sectors = 32;
1608 dp->cyls = sectors / (dp->heads * dp->sectors);
1609 dp->disksize = _5btol(scsipi_sense.size);
1610 if (dp->disksize <= UINT32_MAX && dp->disksize != sectors) {
1611 printf("RBC size: mode sense=%llu, get cap=%llu\n",
1612 (unsigned long long)dp->disksize,
1613 (unsigned long long)sectors);
1614 dp->disksize = sectors;
1615 }
1616 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1617
1618 return (SDGP_RESULT_OK);
1619 }
1620
1621 /*
1622 * Get the scsi driver to send a full inquiry to the * device and use the
1623 * results to fill out the disk parameter structure.
1624 */
1625 static int
1626 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1627 {
1628 u_int64_t sectors;
1629 int error;
1630 #if 0
1631 int i;
1632 u_int8_t *p;
1633 #endif
1634
1635 dp->disksize = sectors = scsipi_size(sd->sc_periph, flags);
1636 if (sectors == 0) {
1637 struct scsipi_read_format_capacities cmd;
1638 struct {
1639 struct scsipi_capacity_list_header header;
1640 struct scsipi_capacity_descriptor desc;
1641 } __attribute__((packed)) data;
1642
1643 memset(&cmd, 0, sizeof(cmd));
1644 memset(&data, 0, sizeof(data));
1645 cmd.opcode = READ_FORMAT_CAPACITIES;
1646 _lto2b(sizeof(data), cmd.length);
1647
1648 error = scsipi_command(sd->sc_periph,
1649 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1650 SDRETRIES, 20000, NULL,
1651 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1652 if (error == EFTYPE) {
1653 /* Medium Format Corrupted, handle as not formatted */
1654 return (SDGP_RESULT_UNFORMATTED);
1655 }
1656 if (error || data.header.length == 0)
1657 return (SDGP_RESULT_OFFLINE);
1658
1659 #if 0
1660 printf("rfc: length=%d\n", data.header.length);
1661 printf("rfc result:"); for (i = sizeof(struct scsipi_capacity_list_header) + data.header.length, p = (void *)&data; i; i--, p++) printf(" %02x", *p); printf("\n");
1662 #endif
1663 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1664 case SCSIPI_CAP_DESC_CODE_RESERVED:
1665 case SCSIPI_CAP_DESC_CODE_FORMATTED:
1666 break;
1667
1668 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1669 return (SDGP_RESULT_UNFORMATTED);
1670
1671 case SCSIPI_CAP_DESC_CODE_NONE:
1672 return (SDGP_RESULT_OFFLINE);
1673 }
1674
1675 dp->disksize = sectors = _4btol(data.desc.nblks);
1676 if (sectors == 0)
1677 return (SDGP_RESULT_OFFLINE); /* XXX? */
1678
1679 dp->blksize = _3btol(data.desc.blklen);
1680 if (dp->blksize == 0)
1681 dp->blksize = 512;
1682 } else {
1683 struct sd_mode_sense_data scsipi_sense;
1684 int big, bsize;
1685 struct scsi_blk_desc *bdesc;
1686
1687 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1688 error = sd_mode_sense(sd, 0, &scsipi_sense,
1689 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1690 dp->blksize = 512;
1691 if (!error) {
1692 if (big) {
1693 bdesc = (void *)(&scsipi_sense.header.big + 1);
1694 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1695 } else {
1696 bdesc = (void *)(&scsipi_sense.header.small + 1);
1697 bsize = scsipi_sense.header.small.blk_desc_len;
1698 }
1699
1700 #if 0
1701 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1702 printf("page 0 bsize=%d\n", bsize);
1703 printf("page 0 ok\n");
1704 #endif
1705
1706 if (bsize >= 8) {
1707 dp->blksize = _3btol(bdesc->blklen);
1708 if (dp->blksize == 0)
1709 dp->blksize = 512;
1710 }
1711 }
1712 }
1713
1714 dp->disksize512 = (sectors * dp->blksize) / DEV_BSIZE;
1715 return (0);
1716 }
1717
1718 static int
1719 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1720 {
1721 struct sd_mode_sense_data scsipi_sense;
1722 int error;
1723 int big, poffset, byte2;
1724 union scsi_disk_pages *pages;
1725 #if 0
1726 int i;
1727 u_int8_t *p;
1728 #endif
1729
1730 byte2 = SMS_DBD;
1731 again:
1732 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1733 error = sd_mode_sense(sd, byte2, &scsipi_sense,
1734 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1735 sizeof(scsipi_sense.pages.rigid_geometry), 4,
1736 flags | XS_CTL_SILENT, &big);
1737 if (error) {
1738 if (byte2 == SMS_DBD) {
1739 /* No result; try once more with DBD off */
1740 byte2 = 0;
1741 goto again;
1742 }
1743 return (error);
1744 }
1745
1746 if (big) {
1747 poffset = sizeof scsipi_sense.header.big;
1748 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1749 } else {
1750 poffset = sizeof scsipi_sense.header.small;
1751 poffset += scsipi_sense.header.small.blk_desc_len;
1752 }
1753
1754 pages = (void *)((u_long)&scsipi_sense + poffset);
1755 #if 0
1756 printf("page 4 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1757 printf("page 4 pg_code=%d sense=%p/%p\n", pages->rigid_geometry.pg_code, &scsipi_sense, pages);
1758 #endif
1759
1760 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
1761 return (ERESTART);
1762
1763 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1764 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1765 _3btol(pages->rigid_geometry.ncyl),
1766 pages->rigid_geometry.nheads,
1767 _2btol(pages->rigid_geometry.st_cyl_wp),
1768 _2btol(pages->rigid_geometry.st_cyl_rwc),
1769 _2btol(pages->rigid_geometry.land_zone)));
1770
1771 /*
1772 * KLUDGE!! (for zone recorded disks)
1773 * give a number of sectors so that sec * trks * cyls
1774 * is <= disk_size
1775 * can lead to wasted space! THINK ABOUT THIS !
1776 */
1777 dp->heads = pages->rigid_geometry.nheads;
1778 dp->cyls = _3btol(pages->rigid_geometry.ncyl);
1779 if (dp->heads == 0 || dp->cyls == 0)
1780 return (ERESTART);
1781 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
1782
1783 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1784 if (dp->rot_rate == 0)
1785 dp->rot_rate = 3600;
1786
1787 #if 0
1788 printf("page 4 ok\n");
1789 #endif
1790 return (0);
1791 }
1792
1793 static int
1794 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
1795 {
1796 struct sd_mode_sense_data scsipi_sense;
1797 int error;
1798 int big, poffset, byte2;
1799 union scsi_disk_pages *pages;
1800 #if 0
1801 int i;
1802 u_int8_t *p;
1803 #endif
1804
1805 byte2 = SMS_DBD;
1806 again:
1807 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1808 error = sd_mode_sense(sd, 0, &scsipi_sense,
1809 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1810 sizeof(scsipi_sense.pages.flex_geometry), 5,
1811 flags | XS_CTL_SILENT, &big);
1812 if (error) {
1813 if (byte2 == SMS_DBD) {
1814 /* No result; try once more with DBD off */
1815 byte2 = 0;
1816 goto again;
1817 }
1818 return (error);
1819 }
1820
1821 if (big) {
1822 poffset = sizeof scsipi_sense.header.big;
1823 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1824 } else {
1825 poffset = sizeof scsipi_sense.header.small;
1826 poffset += scsipi_sense.header.small.blk_desc_len;
1827 }
1828
1829 pages = (void *)((u_long)&scsipi_sense + poffset);
1830 #if 0
1831 printf("page 5 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1832 printf("page 5 pg_code=%d sense=%p/%p\n", pages->flex_geometry.pg_code, &scsipi_sense, pages);
1833 #endif
1834
1835 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
1836 return (ERESTART);
1837
1838 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1839 ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
1840 _3btol(pages->flex_geometry.ncyl),
1841 pages->flex_geometry.nheads,
1842 pages->flex_geometry.ph_sec_tr,
1843 _2btol(pages->flex_geometry.bytes_s)));
1844
1845 dp->heads = pages->flex_geometry.nheads;
1846 dp->cyls = _2btol(pages->flex_geometry.ncyl);
1847 dp->sectors = pages->flex_geometry.ph_sec_tr;
1848 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
1849 return (ERESTART);
1850
1851 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1852 if (dp->rot_rate == 0)
1853 dp->rot_rate = 3600;
1854
1855 #if 0
1856 printf("page 5 ok\n");
1857 #endif
1858 return (0);
1859 }
1860
1861 static int
1862 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1863 {
1864 int error;
1865
1866 /*
1867 * If offline, the SDEV_MEDIA_LOADED flag will be
1868 * cleared by the caller if necessary.
1869 */
1870 if (sd->type == T_SIMPLE_DIRECT)
1871 return (sd_get_simplifiedparms(sd, dp, flags));
1872
1873 error = sd_get_capacity(sd, dp, flags);
1874 if (error)
1875 return (error);
1876
1877 if (sd->type == T_OPTICAL)
1878 goto page0;
1879
1880 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
1881 if (!sd_get_parms_page5(sd, dp, flags) ||
1882 !sd_get_parms_page4(sd, dp, flags))
1883 return (SDGP_RESULT_OK);
1884 } else {
1885 if (!sd_get_parms_page4(sd, dp, flags) ||
1886 !sd_get_parms_page5(sd, dp, flags))
1887 return (SDGP_RESULT_OK);
1888 }
1889
1890 page0:
1891 printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname);
1892 /* Try calling driver's method for figuring out geometry. */
1893 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
1894 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
1895 (sd->sc_periph, dp, dp->disksize)) {
1896 /*
1897 * Use adaptec standard fictitious geometry
1898 * this depends on which controller (e.g. 1542C is
1899 * different. but we have to put SOMETHING here..)
1900 */
1901 dp->heads = 64;
1902 dp->sectors = 32;
1903 dp->cyls = dp->disksize / (64 * 32);
1904 }
1905 dp->rot_rate = 3600;
1906 return (SDGP_RESULT_OK);
1907 }
1908
1909 static int
1910 sd_flush(struct sd_softc *sd, int flags)
1911 {
1912 struct scsipi_periph *periph = sd->sc_periph;
1913 struct scsi_synchronize_cache cmd;
1914
1915 /*
1916 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
1917 * We issue with address 0 length 0, which should be
1918 * interpreted by the device as "all remaining blocks
1919 * starting at address 0". We ignore ILLEGAL REQUEST
1920 * in the event that the command is not supported by
1921 * the device, and poll for completion so that we know
1922 * that the cache has actually been flushed.
1923 *
1924 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
1925 * command, as indicated by our quirks flags.
1926 *
1927 * XXX What about older devices?
1928 */
1929 if (periph->periph_version < 2 ||
1930 (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
1931 return (0);
1932
1933 sd->flags |= SDF_FLUSHING;
1934 memset(&cmd, 0, sizeof(cmd));
1935 cmd.opcode = SCSI_SYNCHRONIZE_CACHE;
1936
1937 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
1938 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
1939 }
1940
1941 static int
1942 sd_getcache(struct sd_softc *sd, int *bitsp)
1943 {
1944 struct scsipi_periph *periph = sd->sc_periph;
1945 struct sd_mode_sense_data scsipi_sense;
1946 int error, bits = 0;
1947 int big;
1948 union scsi_disk_pages *pages;
1949
1950 if (periph->periph_version < 2)
1951 return (EOPNOTSUPP);
1952
1953 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1954 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
1955 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
1956 if (error)
1957 return (error);
1958
1959 if (big)
1960 pages = (void *)(&scsipi_sense.header.big + 1);
1961 else
1962 pages = (void *)(&scsipi_sense.header.small + 1);
1963
1964 if ((pages->caching_params.flags & CACHING_RCD) == 0)
1965 bits |= DKCACHE_READ;
1966 if (pages->caching_params.flags & CACHING_WCE)
1967 bits |= DKCACHE_WRITE;
1968 if (pages->caching_params.pg_code & PGCODE_PS)
1969 bits |= DKCACHE_SAVE;
1970
1971 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1972 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
1973 sizeof(scsipi_sense.pages.caching_params),
1974 SMS_PAGE_CTRL_CHANGEABLE|8, 0, &big);
1975 if (error == 0) {
1976 if (big)
1977 pages = (void *)(&scsipi_sense.header.big + 1);
1978 else
1979 pages = (void *)(&scsipi_sense.header.small + 1);
1980
1981 if (pages->caching_params.flags & CACHING_RCD)
1982 bits |= DKCACHE_RCHANGE;
1983 if (pages->caching_params.flags & CACHING_WCE)
1984 bits |= DKCACHE_WCHANGE;
1985 }
1986
1987 *bitsp = bits;
1988
1989 return (0);
1990 }
1991
1992 static int
1993 sd_setcache(struct sd_softc *sd, int bits)
1994 {
1995 struct scsipi_periph *periph = sd->sc_periph;
1996 struct sd_mode_sense_data scsipi_sense;
1997 int error;
1998 uint8_t oflags, byte2 = 0;
1999 int big;
2000 union scsi_disk_pages *pages;
2001
2002 if (periph->periph_version < 2)
2003 return (EOPNOTSUPP);
2004
2005 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2006 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2007 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2008 if (error)
2009 return (error);
2010
2011 if (big)
2012 pages = (void *)(&scsipi_sense.header.big + 1);
2013 else
2014 pages = (void *)(&scsipi_sense.header.small + 1);
2015
2016 oflags = pages->caching_params.flags;
2017
2018 if (bits & DKCACHE_READ)
2019 pages->caching_params.flags &= ~CACHING_RCD;
2020 else
2021 pages->caching_params.flags |= CACHING_RCD;
2022
2023 if (bits & DKCACHE_WRITE)
2024 pages->caching_params.flags |= CACHING_WCE;
2025 else
2026 pages->caching_params.flags &= ~CACHING_WCE;
2027
2028 if (oflags == pages->caching_params.flags)
2029 return (0);
2030
2031 pages->caching_params.pg_code &= PGCODE_MASK;
2032
2033 if (bits & DKCACHE_SAVE)
2034 byte2 |= SMS_SP;
2035
2036 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2037 sizeof(struct scsipi_mode_page_header) +
2038 pages->caching_params.pg_length, 0, big));
2039 }
2040