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