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