sd.c revision 1.224 1 /* $NetBSD: sd.c,v 1.224 2004/09/17 23:10:51 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.224 2004/09/17 23:10:51 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_command(periph, xs, cmdp, cmdlen,
896 (u_char *)bp->b_data, bp->b_bcount,
897 SDRETRIES, SD_IO_TIMEOUT, bp, flags);
898 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
899 KASSERT(error == 0);
900 }
901 }
902
903 static void
904 sdrestart(void *v)
905 {
906 int s = splbio();
907 sdstart((struct scsipi_periph *)v);
908 splx(s);
909 }
910
911 static void
912 sddone(struct scsipi_xfer *xs, int error)
913 {
914 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
915 struct buf *bp = xs->bp;
916
917 if (sd->flags & SDF_FLUSHING) {
918 /* Flush completed, no longer dirty. */
919 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
920 }
921
922 if (bp) {
923 bp->b_error = error;
924 bp->b_resid = xs->resid;
925 if (error)
926 bp->b_flags |= B_ERROR;
927
928 disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
929 (bp->b_flags & B_READ));
930 #if NRND > 0
931 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
932 #endif
933
934 biodone(bp);
935 }
936 }
937
938 static void
939 sdminphys(struct buf *bp)
940 {
941 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
942 long max;
943
944 /*
945 * If the device is ancient, we want to make sure that
946 * the transfer fits into a 6-byte cdb.
947 *
948 * XXX Note that the SCSI-I spec says that 256-block transfers
949 * are allowed in a 6-byte read/write, and are specified
950 * by settng the "length" to 0. However, we're conservative
951 * here, allowing only 255-block transfers in case an
952 * ancient device gets confused by length == 0. A length of 0
953 * in a 10-byte read/write actually means 0 blocks.
954 */
955 if ((sd->flags & SDF_ANCIENT) &&
956 ((sd->sc_periph->periph_flags &
957 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
958 max = sd->sc_dk.dk_label->d_secsize * 0xff;
959
960 if (bp->b_bcount > max)
961 bp->b_bcount = max;
962 }
963
964 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
965 }
966
967 static int
968 sdread(dev_t dev, struct uio *uio, int ioflag)
969 {
970
971 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
972 }
973
974 static int
975 sdwrite(dev_t dev, struct uio *uio, int ioflag)
976 {
977
978 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
979 }
980
981 /*
982 * Perform special action on behalf of the user
983 * Knows about the internals of this device
984 */
985 static int
986 sdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
987 {
988 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
989 struct scsipi_periph *periph = sd->sc_periph;
990 int part = SDPART(dev);
991 int error = 0;
992 #ifdef __HAVE_OLD_DISKLABEL
993 struct disklabel *newlabel = NULL;
994 #endif
995
996 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
997
998 /*
999 * If the device is not valid, some IOCTLs can still be
1000 * handled on the raw partition. Check this here.
1001 */
1002 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1003 switch (cmd) {
1004 case DIOCKLABEL:
1005 case DIOCWLABEL:
1006 case DIOCLOCK:
1007 case DIOCEJECT:
1008 case ODIOCEJECT:
1009 case DIOCGCACHE:
1010 case DIOCSCACHE:
1011 case SCIOCIDENTIFY:
1012 case OSCIOCIDENTIFY:
1013 case SCIOCCOMMAND:
1014 case SCIOCDEBUG:
1015 if (part == RAW_PART)
1016 break;
1017 /* FALLTHROUGH */
1018 default:
1019 if ((periph->periph_flags & PERIPH_OPEN) == 0)
1020 return (ENODEV);
1021 else
1022 return (EIO);
1023 }
1024 }
1025
1026 switch (cmd) {
1027 case DIOCGDINFO:
1028 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1029 return (0);
1030
1031 #ifdef __HAVE_OLD_DISKLABEL
1032 case ODIOCGDINFO:
1033 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1034 if (newlabel == NULL)
1035 return EIO;
1036 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1037 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1038 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1039 else
1040 error = ENOTTY;
1041 free(newlabel, M_TEMP);
1042 return error;
1043 #endif
1044
1045 case DIOCGPART:
1046 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1047 ((struct partinfo *)addr)->part =
1048 &sd->sc_dk.dk_label->d_partitions[part];
1049 return (0);
1050
1051 case DIOCWDINFO:
1052 case DIOCSDINFO:
1053 #ifdef __HAVE_OLD_DISKLABEL
1054 case ODIOCWDINFO:
1055 case ODIOCSDINFO:
1056 #endif
1057 {
1058 struct disklabel *lp;
1059
1060 if ((flag & FWRITE) == 0)
1061 return (EBADF);
1062
1063 #ifdef __HAVE_OLD_DISKLABEL
1064 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1065 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1066 if (newlabel == NULL)
1067 return EIO;
1068 memset(newlabel, 0, sizeof newlabel);
1069 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1070 lp = newlabel;
1071 } else
1072 #endif
1073 lp = (struct disklabel *)addr;
1074
1075 if ((error = lockmgr(&sd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
1076 goto bad;
1077 sd->flags |= SDF_LABELLING;
1078
1079 error = setdisklabel(sd->sc_dk.dk_label,
1080 lp, /*sd->sc_dk.dk_openmask : */0,
1081 sd->sc_dk.dk_cpulabel);
1082 if (error == 0) {
1083 if (cmd == DIOCWDINFO
1084 #ifdef __HAVE_OLD_DISKLABEL
1085 || cmd == ODIOCWDINFO
1086 #endif
1087 )
1088 error = writedisklabel(SDLABELDEV(dev),
1089 sdstrategy, sd->sc_dk.dk_label,
1090 sd->sc_dk.dk_cpulabel);
1091 }
1092
1093 sd->flags &= ~SDF_LABELLING;
1094 lockmgr(&sd->sc_lock, LK_RELEASE, NULL);
1095 bad:
1096 #ifdef __HAVE_OLD_DISKLABEL
1097 if (newlabel != NULL)
1098 free(newlabel, M_TEMP);
1099 #endif
1100 return (error);
1101 }
1102
1103 case DIOCKLABEL:
1104 if (*(int *)addr)
1105 periph->periph_flags |= PERIPH_KEEP_LABEL;
1106 else
1107 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1108 return (0);
1109
1110 case DIOCWLABEL:
1111 if ((flag & FWRITE) == 0)
1112 return (EBADF);
1113 if (*(int *)addr)
1114 sd->flags |= SDF_WLABEL;
1115 else
1116 sd->flags &= ~SDF_WLABEL;
1117 return (0);
1118
1119 case DIOCLOCK:
1120 return (scsipi_prevent(periph,
1121 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1122
1123 case DIOCEJECT:
1124 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1125 return (ENOTTY);
1126 if (*(int *)addr == 0) {
1127 /*
1128 * Don't force eject: check that we are the only
1129 * partition open. If so, unlock it.
1130 */
1131 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1132 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1133 sd->sc_dk.dk_openmask) {
1134 error = scsipi_prevent(periph, PR_ALLOW,
1135 XS_CTL_IGNORE_NOT_READY);
1136 if (error)
1137 return (error);
1138 } else {
1139 return (EBUSY);
1140 }
1141 }
1142 /* FALLTHROUGH */
1143 case ODIOCEJECT:
1144 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1145 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1146
1147 case DIOCGDEFLABEL:
1148 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1149 return (0);
1150
1151 #ifdef __HAVE_OLD_DISKLABEL
1152 case ODIOCGDEFLABEL:
1153 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1154 if (newlabel == NULL)
1155 return EIO;
1156 sdgetdefaultlabel(sd, newlabel);
1157 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1158 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1159 else
1160 error = ENOTTY;
1161 free(newlabel, M_TEMP);
1162 return error;
1163 #endif
1164
1165 case DIOCGCACHE:
1166 return (sd_getcache(sd, (int *) addr));
1167
1168 case DIOCSCACHE:
1169 if ((flag & FWRITE) == 0)
1170 return (EBADF);
1171 return (sd_setcache(sd, *(int *) addr));
1172
1173 case DIOCCACHESYNC:
1174 /*
1175 * XXX Do we really need to care about having a writable
1176 * file descriptor here?
1177 */
1178 if ((flag & FWRITE) == 0)
1179 return (EBADF);
1180 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1181 error = sd_flush(sd, 0);
1182 if (error)
1183 sd->flags &= ~SDF_FLUSHING;
1184 else
1185 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1186 } else
1187 error = 0;
1188 return (error);
1189
1190 default:
1191 if (part != RAW_PART)
1192 return (ENOTTY);
1193 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1194 }
1195
1196 #ifdef DIAGNOSTIC
1197 panic("sdioctl: impossible");
1198 #endif
1199 }
1200
1201 static void
1202 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1203 {
1204
1205 memset(lp, 0, sizeof(struct disklabel));
1206
1207 lp->d_secsize = sd->params.blksize;
1208 lp->d_ntracks = sd->params.heads;
1209 lp->d_nsectors = sd->params.sectors;
1210 lp->d_ncylinders = sd->params.cyls;
1211 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1212
1213 switch (scsipi_periph_bustype(sd->sc_periph)) {
1214 case SCSIPI_BUSTYPE_SCSI:
1215 lp->d_type = DTYPE_SCSI;
1216 break;
1217 case SCSIPI_BUSTYPE_ATAPI:
1218 lp->d_type = DTYPE_ATAPI;
1219 break;
1220 }
1221 /*
1222 * XXX
1223 * We could probe the mode pages to figure out what kind of disc it is.
1224 * Is this worthwhile?
1225 */
1226 strncpy(lp->d_typename, "mydisk", 16);
1227 strncpy(lp->d_packname, "fictitious", 16);
1228 lp->d_secperunit = sd->params.disksize;
1229 lp->d_rpm = sd->params.rot_rate;
1230 lp->d_interleave = 1;
1231 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1232 D_REMOVABLE : 0;
1233
1234 lp->d_partitions[RAW_PART].p_offset = 0;
1235 lp->d_partitions[RAW_PART].p_size =
1236 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1237 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1238 lp->d_npartitions = RAW_PART + 1;
1239
1240 lp->d_magic = DISKMAGIC;
1241 lp->d_magic2 = DISKMAGIC;
1242 lp->d_checksum = dkcksum(lp);
1243 }
1244
1245
1246 /*
1247 * Load the label information on the named device
1248 */
1249 static void
1250 sdgetdisklabel(struct sd_softc *sd)
1251 {
1252 struct disklabel *lp = sd->sc_dk.dk_label;
1253 const char *errstring;
1254
1255 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1256
1257 sdgetdefaultlabel(sd, lp);
1258
1259 if (lp->d_secpercyl == 0) {
1260 lp->d_secpercyl = 100;
1261 /* as long as it's not 0 - readdisklabel divides by it (?) */
1262 }
1263
1264 /*
1265 * Call the generic disklabel extraction routine
1266 */
1267 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
1268 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1269 if (errstring) {
1270 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1271 return;
1272 }
1273 }
1274
1275 static void
1276 sd_shutdown(void *arg)
1277 {
1278 struct sd_softc *sd = arg;
1279
1280 /*
1281 * If the disk cache needs to be flushed, and the disk supports
1282 * it, flush it. We're cold at this point, so we poll for
1283 * completion.
1284 */
1285 if ((sd->flags & SDF_DIRTY) != 0) {
1286 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1287 printf("%s: cache synchronization failed\n",
1288 sd->sc_dev.dv_xname);
1289 sd->flags &= ~SDF_FLUSHING;
1290 } else
1291 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1292 }
1293 }
1294
1295 /*
1296 * Check Errors
1297 */
1298 static int
1299 sd_interpret_sense(struct scsipi_xfer *xs)
1300 {
1301 struct scsipi_periph *periph = xs->xs_periph;
1302 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1303 struct sd_softc *sd = (void *)periph->periph_dev;
1304 int s, error, retval = EJUSTRETURN;
1305
1306 /*
1307 * If the periph is already recovering, just do the normal
1308 * error processing.
1309 */
1310 if (periph->periph_flags & PERIPH_RECOVERING)
1311 return (retval);
1312
1313 /*
1314 * If the device is not open yet, let the generic code handle it.
1315 */
1316 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1317 return (retval);
1318
1319 /*
1320 * If it isn't a extended or extended/deferred error, let
1321 * the generic code handle it.
1322 */
1323 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1324 (sense->error_code & SSD_ERRCODE) != 0x71)
1325 return (retval);
1326
1327 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1328 sense->add_sense_code == 0x4) {
1329 if (sense->add_sense_code_qual == 0x01) {
1330 /*
1331 * Unit In The Process Of Becoming Ready.
1332 */
1333 printf("%s: waiting for pack to spin up...\n",
1334 sd->sc_dev.dv_xname);
1335 if (!callout_pending(&periph->periph_callout))
1336 scsipi_periph_freeze(periph, 1);
1337 callout_reset(&periph->periph_callout,
1338 5 * hz, scsipi_periph_timed_thaw, periph);
1339 retval = ERESTART;
1340 } else if (sense->add_sense_code_qual == 0x02) {
1341 printf("%s: pack is stopped, restarting...\n",
1342 sd->sc_dev.dv_xname);
1343 s = splbio();
1344 periph->periph_flags |= PERIPH_RECOVERING;
1345 splx(s);
1346 error = scsipi_start(periph, SSS_START,
1347 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1348 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1349 if (error) {
1350 printf("%s: unable to restart pack\n",
1351 sd->sc_dev.dv_xname);
1352 retval = error;
1353 } else
1354 retval = ERESTART;
1355 s = splbio();
1356 periph->periph_flags &= ~PERIPH_RECOVERING;
1357 splx(s);
1358 }
1359 }
1360 if ((sense->flags & SSD_KEY) == SKEY_MEDIUM_ERROR &&
1361 sense->add_sense_code == 0x31 &&
1362 sense->add_sense_code_qual == 0x00) { /* maybe for any asq ? */
1363 /* Medium Format Corrupted */
1364 retval = EFTYPE;
1365 }
1366 return (retval);
1367 }
1368
1369
1370 static int
1371 sdsize(dev_t dev)
1372 {
1373 struct sd_softc *sd;
1374 int part, unit, omask;
1375 int size;
1376
1377 unit = SDUNIT(dev);
1378 if (unit >= sd_cd.cd_ndevs)
1379 return (-1);
1380 sd = sd_cd.cd_devs[unit];
1381 if (sd == NULL)
1382 return (-1);
1383
1384 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1385 return (-1);
1386
1387 part = SDPART(dev);
1388 omask = sd->sc_dk.dk_openmask & (1 << part);
1389
1390 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1391 return (-1);
1392 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1393 size = -1;
1394 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1395 size = -1;
1396 else
1397 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1398 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1399 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1400 return (-1);
1401 return (size);
1402 }
1403
1404 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1405 static struct scsipi_xfer sx;
1406 static int sddoingadump;
1407
1408 /*
1409 * dump all of physical memory into the partition specified, starting
1410 * at offset 'dumplo' into the partition.
1411 */
1412 static int
1413 sddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1414 {
1415 struct sd_softc *sd; /* disk unit to do the I/O */
1416 struct disklabel *lp; /* disk's disklabel */
1417 int unit, part;
1418 int sectorsize; /* size of a disk sector */
1419 int nsects; /* number of sectors in partition */
1420 int sectoff; /* sector offset of partition */
1421 int totwrt; /* total number of sectors left to write */
1422 int nwrt; /* current number of sectors to write */
1423 struct scsipi_rw_big cmd; /* write command */
1424 struct scsipi_xfer *xs; /* ... convenience */
1425 struct scsipi_periph *periph;
1426 struct scsipi_channel *chan;
1427
1428 /* Check if recursive dump; if so, punt. */
1429 if (sddoingadump)
1430 return (EFAULT);
1431
1432 /* Mark as active early. */
1433 sddoingadump = 1;
1434
1435 unit = SDUNIT(dev); /* Decompose unit & partition. */
1436 part = SDPART(dev);
1437
1438 /* Check for acceptable drive number. */
1439 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1440 return (ENXIO);
1441
1442 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1443 return (ENODEV);
1444
1445 periph = sd->sc_periph;
1446 chan = periph->periph_channel;
1447
1448 /* Make sure it was initialized. */
1449 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1450 return (ENXIO);
1451
1452 /* Convert to disk sectors. Request must be a multiple of size. */
1453 lp = sd->sc_dk.dk_label;
1454 sectorsize = lp->d_secsize;
1455 if ((size % sectorsize) != 0)
1456 return (EFAULT);
1457 totwrt = size / sectorsize;
1458 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1459
1460 nsects = lp->d_partitions[part].p_size;
1461 sectoff = lp->d_partitions[part].p_offset;
1462
1463 /* Check transfer bounds against partition size. */
1464 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1465 return (EINVAL);
1466
1467 /* Offset block number to start of partition. */
1468 blkno += sectoff;
1469
1470 xs = &sx;
1471
1472 while (totwrt > 0) {
1473 nwrt = totwrt; /* XXX */
1474 #ifndef SD_DUMP_NOT_TRUSTED
1475 /*
1476 * Fill out the scsi command
1477 */
1478 memset(&cmd, 0, sizeof(cmd));
1479 cmd.opcode = WRITE_BIG;
1480 _lto4b(blkno, cmd.addr);
1481 _lto2b(nwrt, cmd.length);
1482 /*
1483 * Fill out the scsipi_xfer structure
1484 * Note: we cannot sleep as we may be an interrupt
1485 * don't use scsipi_command() as it may want to wait
1486 * for an xs.
1487 */
1488 memset(xs, 0, sizeof(sx));
1489 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1490 XS_CTL_DATA_OUT;
1491 xs->xs_status = 0;
1492 xs->xs_periph = periph;
1493 xs->xs_retries = SDRETRIES;
1494 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1495 xs->cmd = (struct scsipi_generic *)&cmd;
1496 xs->cmdlen = sizeof(cmd);
1497 xs->resid = nwrt * sectorsize;
1498 xs->error = XS_NOERROR;
1499 xs->bp = 0;
1500 xs->data = va;
1501 xs->datalen = nwrt * sectorsize;
1502
1503 /*
1504 * Pass all this info to the scsi driver.
1505 */
1506 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1507 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1508 xs->error != XS_NOERROR)
1509 return (EIO);
1510 #else /* SD_DUMP_NOT_TRUSTED */
1511 /* Let's just talk about this first... */
1512 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1513 delay(500 * 1000); /* half a second */
1514 #endif /* SD_DUMP_NOT_TRUSTED */
1515
1516 /* update block count */
1517 totwrt -= nwrt;
1518 blkno += nwrt;
1519 va += sectorsize * nwrt;
1520 }
1521 sddoingadump = 0;
1522 return (0);
1523 }
1524
1525 static int
1526 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1527 int page, int flags, int *big)
1528 {
1529
1530 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1531 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1532 *big = 1;
1533 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1534 size + sizeof(struct scsipi_mode_header_big),
1535 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1536 } else {
1537 *big = 0;
1538 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1539 size + sizeof(struct scsipi_mode_header),
1540 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1541 }
1542 }
1543
1544 static int
1545 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1546 int flags, int big)
1547 {
1548
1549 if (big) {
1550 struct scsipi_mode_header_big *header = sense;
1551
1552 _lto2b(0, header->data_length);
1553 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1554 size + sizeof(struct scsipi_mode_header_big),
1555 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1556 } else {
1557 struct scsipi_mode_header *header = sense;
1558
1559 header->data_length = 0;
1560 return scsipi_mode_select(sd->sc_periph, byte2, sense,
1561 size + sizeof(struct scsipi_mode_header),
1562 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1563 }
1564 }
1565
1566 static int
1567 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1568 {
1569 struct {
1570 struct scsipi_mode_header header;
1571 /* no block descriptor */
1572 u_int8_t pg_code; /* page code (should be 6) */
1573 u_int8_t pg_length; /* page length (should be 11) */
1574 u_int8_t wcd; /* bit0: cache disable */
1575 u_int8_t lbs[2]; /* logical block size */
1576 u_int8_t size[5]; /* number of log. blocks */
1577 u_int8_t pp; /* power/performance */
1578 u_int8_t flags;
1579 u_int8_t resvd;
1580 } scsipi_sense;
1581 u_int64_t sectors;
1582 int error;
1583
1584 /*
1585 * scsipi_size (ie "read capacity") and mode sense page 6
1586 * give the same information. Do both for now, and check
1587 * for consistency.
1588 * XXX probably differs for removable media
1589 */
1590 dp->blksize = 512;
1591 if ((sectors = scsipi_size(sd->sc_periph, flags)) == 0)
1592 return (SDGP_RESULT_OFFLINE); /* XXX? */
1593
1594 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1595 &scsipi_sense.header, sizeof(scsipi_sense),
1596 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1597
1598 if (error != 0)
1599 return (SDGP_RESULT_OFFLINE); /* XXX? */
1600
1601 dp->blksize = _2btol(scsipi_sense.lbs);
1602 if (dp->blksize == 0)
1603 dp->blksize = 512;
1604
1605 /*
1606 * Create a pseudo-geometry.
1607 */
1608 dp->heads = 64;
1609 dp->sectors = 32;
1610 dp->cyls = sectors / (dp->heads * dp->sectors);
1611 dp->disksize = _5btol(scsipi_sense.size);
1612 if (dp->disksize <= UINT32_MAX && dp->disksize != sectors) {
1613 printf("RBC size: mode sense=%llu, get cap=%llu\n",
1614 (unsigned long long)dp->disksize,
1615 (unsigned long long)sectors);
1616 dp->disksize = sectors;
1617 }
1618 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1619
1620 return (SDGP_RESULT_OK);
1621 }
1622
1623 /*
1624 * Get the scsi driver to send a full inquiry to the * device and use the
1625 * results to fill out the disk parameter structure.
1626 */
1627 static int
1628 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1629 {
1630 u_int64_t sectors;
1631 int error;
1632 #if 0
1633 int i;
1634 u_int8_t *p;
1635 #endif
1636
1637 dp->disksize = sectors = scsipi_size(sd->sc_periph, flags);
1638 if (sectors == 0) {
1639 struct scsipi_read_format_capacities cmd;
1640 struct {
1641 struct scsipi_capacity_list_header header;
1642 struct scsipi_capacity_descriptor desc;
1643 } __attribute__((packed)) data;
1644
1645 memset(&cmd, 0, sizeof(cmd));
1646 memset(&data, 0, sizeof(data));
1647 cmd.opcode = READ_FORMAT_CAPACITIES;
1648 _lto2b(sizeof(data), cmd.length);
1649
1650 error = scsipi_command(sd->sc_periph, NULL,
1651 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1652 SDRETRIES, 20000, NULL,
1653 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1654 if (error == EFTYPE) {
1655 /* Medium Format Corrupted, handle as not formatted */
1656 return (SDGP_RESULT_UNFORMATTED);
1657 }
1658 if (error || data.header.length == 0)
1659 return (SDGP_RESULT_OFFLINE);
1660
1661 #if 0
1662 printf("rfc: length=%d\n", data.header.length);
1663 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");
1664 #endif
1665 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1666 case SCSIPI_CAP_DESC_CODE_RESERVED:
1667 case SCSIPI_CAP_DESC_CODE_FORMATTED:
1668 break;
1669
1670 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1671 return (SDGP_RESULT_UNFORMATTED);
1672
1673 case SCSIPI_CAP_DESC_CODE_NONE:
1674 return (SDGP_RESULT_OFFLINE);
1675 }
1676
1677 dp->disksize = sectors = _4btol(data.desc.nblks);
1678 if (sectors == 0)
1679 return (SDGP_RESULT_OFFLINE); /* XXX? */
1680
1681 dp->blksize = _3btol(data.desc.blklen);
1682 if (dp->blksize == 0)
1683 dp->blksize = 512;
1684 } else {
1685 struct sd_mode_sense_data scsipi_sense;
1686 int big, bsize;
1687 struct scsi_blk_desc *bdesc;
1688
1689 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1690 error = sd_mode_sense(sd, 0, &scsipi_sense,
1691 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1692 dp->blksize = 512;
1693 if (!error) {
1694 if (big) {
1695 bdesc = (void *)(&scsipi_sense.header.big + 1);
1696 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1697 } else {
1698 bdesc = (void *)(&scsipi_sense.header.small + 1);
1699 bsize = scsipi_sense.header.small.blk_desc_len;
1700 }
1701
1702 #if 0
1703 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1704 printf("page 0 bsize=%d\n", bsize);
1705 printf("page 0 ok\n");
1706 #endif
1707
1708 if (bsize >= 8) {
1709 dp->blksize = _3btol(bdesc->blklen);
1710 if (dp->blksize == 0)
1711 dp->blksize = 512;
1712 }
1713 }
1714 }
1715
1716 dp->disksize512 = (sectors * dp->blksize) / DEV_BSIZE;
1717 return (0);
1718 }
1719
1720 static int
1721 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1722 {
1723 struct sd_mode_sense_data scsipi_sense;
1724 int error;
1725 int big, poffset, byte2;
1726 union scsi_disk_pages *pages;
1727 #if 0
1728 int i;
1729 u_int8_t *p;
1730 #endif
1731
1732 byte2 = SMS_DBD;
1733 again:
1734 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1735 error = sd_mode_sense(sd, byte2, &scsipi_sense,
1736 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1737 sizeof(scsipi_sense.pages.rigid_geometry), 4,
1738 flags | XS_CTL_SILENT, &big);
1739 if (error) {
1740 if (byte2 == SMS_DBD) {
1741 /* No result; try once more with DBD off */
1742 byte2 = 0;
1743 goto again;
1744 }
1745 return (error);
1746 }
1747
1748 if (big) {
1749 poffset = sizeof scsipi_sense.header.big;
1750 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1751 } else {
1752 poffset = sizeof scsipi_sense.header.small;
1753 poffset += scsipi_sense.header.small.blk_desc_len;
1754 }
1755
1756 pages = (void *)((u_long)&scsipi_sense + poffset);
1757 #if 0
1758 printf("page 4 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1759 printf("page 4 pg_code=%d sense=%p/%p\n", pages->rigid_geometry.pg_code, &scsipi_sense, pages);
1760 #endif
1761
1762 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
1763 return (ERESTART);
1764
1765 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1766 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1767 _3btol(pages->rigid_geometry.ncyl),
1768 pages->rigid_geometry.nheads,
1769 _2btol(pages->rigid_geometry.st_cyl_wp),
1770 _2btol(pages->rigid_geometry.st_cyl_rwc),
1771 _2btol(pages->rigid_geometry.land_zone)));
1772
1773 /*
1774 * KLUDGE!! (for zone recorded disks)
1775 * give a number of sectors so that sec * trks * cyls
1776 * is <= disk_size
1777 * can lead to wasted space! THINK ABOUT THIS !
1778 */
1779 dp->heads = pages->rigid_geometry.nheads;
1780 dp->cyls = _3btol(pages->rigid_geometry.ncyl);
1781 if (dp->heads == 0 || dp->cyls == 0)
1782 return (ERESTART);
1783 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
1784
1785 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1786 if (dp->rot_rate == 0)
1787 dp->rot_rate = 3600;
1788
1789 #if 0
1790 printf("page 4 ok\n");
1791 #endif
1792 return (0);
1793 }
1794
1795 static int
1796 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
1797 {
1798 struct sd_mode_sense_data scsipi_sense;
1799 int error;
1800 int big, poffset, byte2;
1801 union scsi_disk_pages *pages;
1802 #if 0
1803 int i;
1804 u_int8_t *p;
1805 #endif
1806
1807 byte2 = SMS_DBD;
1808 again:
1809 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1810 error = sd_mode_sense(sd, 0, &scsipi_sense,
1811 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1812 sizeof(scsipi_sense.pages.flex_geometry), 5,
1813 flags | XS_CTL_SILENT, &big);
1814 if (error) {
1815 if (byte2 == SMS_DBD) {
1816 /* No result; try once more with DBD off */
1817 byte2 = 0;
1818 goto again;
1819 }
1820 return (error);
1821 }
1822
1823 if (big) {
1824 poffset = sizeof scsipi_sense.header.big;
1825 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1826 } else {
1827 poffset = sizeof scsipi_sense.header.small;
1828 poffset += scsipi_sense.header.small.blk_desc_len;
1829 }
1830
1831 pages = (void *)((u_long)&scsipi_sense + poffset);
1832 #if 0
1833 printf("page 5 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1834 printf("page 5 pg_code=%d sense=%p/%p\n", pages->flex_geometry.pg_code, &scsipi_sense, pages);
1835 #endif
1836
1837 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
1838 return (ERESTART);
1839
1840 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1841 ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
1842 _3btol(pages->flex_geometry.ncyl),
1843 pages->flex_geometry.nheads,
1844 pages->flex_geometry.ph_sec_tr,
1845 _2btol(pages->flex_geometry.bytes_s)));
1846
1847 dp->heads = pages->flex_geometry.nheads;
1848 dp->cyls = _2btol(pages->flex_geometry.ncyl);
1849 dp->sectors = pages->flex_geometry.ph_sec_tr;
1850 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
1851 return (ERESTART);
1852
1853 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1854 if (dp->rot_rate == 0)
1855 dp->rot_rate = 3600;
1856
1857 #if 0
1858 printf("page 5 ok\n");
1859 #endif
1860 return (0);
1861 }
1862
1863 static int
1864 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1865 {
1866 int error;
1867
1868 /*
1869 * If offline, the SDEV_MEDIA_LOADED flag will be
1870 * cleared by the caller if necessary.
1871 */
1872 if (sd->type == T_SIMPLE_DIRECT)
1873 return (sd_get_simplifiedparms(sd, dp, flags));
1874
1875 error = sd_get_capacity(sd, dp, flags);
1876 if (error)
1877 return (error);
1878
1879 if (sd->type == T_OPTICAL)
1880 goto page0;
1881
1882 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
1883 if (!sd_get_parms_page5(sd, dp, flags) ||
1884 !sd_get_parms_page4(sd, dp, flags))
1885 return (SDGP_RESULT_OK);
1886 } else {
1887 if (!sd_get_parms_page4(sd, dp, flags) ||
1888 !sd_get_parms_page5(sd, dp, flags))
1889 return (SDGP_RESULT_OK);
1890 }
1891
1892 page0:
1893 printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname);
1894 /* Try calling driver's method for figuring out geometry. */
1895 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
1896 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
1897 (sd->sc_periph, dp, dp->disksize)) {
1898 /*
1899 * Use adaptec standard fictitious geometry
1900 * this depends on which controller (e.g. 1542C is
1901 * different. but we have to put SOMETHING here..)
1902 */
1903 dp->heads = 64;
1904 dp->sectors = 32;
1905 dp->cyls = dp->disksize / (64 * 32);
1906 }
1907 dp->rot_rate = 3600;
1908 return (SDGP_RESULT_OK);
1909 }
1910
1911 static int
1912 sd_flush(struct sd_softc *sd, int flags)
1913 {
1914 struct scsipi_periph *periph = sd->sc_periph;
1915 struct scsi_synchronize_cache cmd;
1916
1917 /*
1918 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
1919 * We issue with address 0 length 0, which should be
1920 * interpreted by the device as "all remaining blocks
1921 * starting at address 0". We ignore ILLEGAL REQUEST
1922 * in the event that the command is not supported by
1923 * the device, and poll for completion so that we know
1924 * that the cache has actually been flushed.
1925 *
1926 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
1927 * command, as indicated by our quirks flags.
1928 *
1929 * XXX What about older devices?
1930 */
1931 if (periph->periph_version < 2 ||
1932 (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
1933 return (0);
1934
1935 sd->flags |= SDF_FLUSHING;
1936 memset(&cmd, 0, sizeof(cmd));
1937 cmd.opcode = SCSI_SYNCHRONIZE_CACHE;
1938
1939 return (scsipi_command(periph, NULL, (void *)&cmd, sizeof(cmd), 0, 0,
1940 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
1941 }
1942
1943 static int
1944 sd_getcache(struct sd_softc *sd, int *bitsp)
1945 {
1946 struct scsipi_periph *periph = sd->sc_periph;
1947 struct sd_mode_sense_data scsipi_sense;
1948 int error, bits = 0;
1949 int big;
1950 union scsi_disk_pages *pages;
1951
1952 if (periph->periph_version < 2)
1953 return (EOPNOTSUPP);
1954
1955 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1956 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
1957 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
1958 if (error)
1959 return (error);
1960
1961 if (big)
1962 pages = (void *)(&scsipi_sense.header.big + 1);
1963 else
1964 pages = (void *)(&scsipi_sense.header.small + 1);
1965
1966 if ((pages->caching_params.flags & CACHING_RCD) == 0)
1967 bits |= DKCACHE_READ;
1968 if (pages->caching_params.flags & CACHING_WCE)
1969 bits |= DKCACHE_WRITE;
1970 if (pages->caching_params.pg_code & PGCODE_PS)
1971 bits |= DKCACHE_SAVE;
1972
1973 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1974 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
1975 sizeof(scsipi_sense.pages.caching_params),
1976 SMS_PAGE_CTRL_CHANGEABLE|8, 0, &big);
1977 if (error == 0) {
1978 if (big)
1979 pages = (void *)(&scsipi_sense.header.big + 1);
1980 else
1981 pages = (void *)(&scsipi_sense.header.small + 1);
1982
1983 if (pages->caching_params.flags & CACHING_RCD)
1984 bits |= DKCACHE_RCHANGE;
1985 if (pages->caching_params.flags & CACHING_WCE)
1986 bits |= DKCACHE_WCHANGE;
1987 }
1988
1989 *bitsp = bits;
1990
1991 return (0);
1992 }
1993
1994 static int
1995 sd_setcache(struct sd_softc *sd, int bits)
1996 {
1997 struct scsipi_periph *periph = sd->sc_periph;
1998 struct sd_mode_sense_data scsipi_sense;
1999 int error;
2000 uint8_t oflags, byte2 = 0;
2001 int big;
2002 union scsi_disk_pages *pages;
2003
2004 if (periph->periph_version < 2)
2005 return (EOPNOTSUPP);
2006
2007 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2008 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2009 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2010 if (error)
2011 return (error);
2012
2013 if (big)
2014 pages = (void *)(&scsipi_sense.header.big + 1);
2015 else
2016 pages = (void *)(&scsipi_sense.header.small + 1);
2017
2018 oflags = pages->caching_params.flags;
2019
2020 if (bits & DKCACHE_READ)
2021 pages->caching_params.flags &= ~CACHING_RCD;
2022 else
2023 pages->caching_params.flags |= CACHING_RCD;
2024
2025 if (bits & DKCACHE_WRITE)
2026 pages->caching_params.flags |= CACHING_WCE;
2027 else
2028 pages->caching_params.flags &= ~CACHING_WCE;
2029
2030 if (oflags == pages->caching_params.flags)
2031 return (0);
2032
2033 pages->caching_params.pg_code &= PGCODE_MASK;
2034
2035 if (bits & DKCACHE_SAVE)
2036 byte2 |= SMS_SP;
2037
2038 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2039 sizeof(struct scsipi_mode_page_header) +
2040 pages->caching_params.pg_length, 0, big));
2041 }
2042