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