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