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