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