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