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