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