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