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