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