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