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