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