sd.c revision 1.261.2.1 1 /* $NetBSD: sd.c,v 1.261.2.1 2007/05/13 17:36:29 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.1 2007/05/13 17:36:29 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);
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, error = 0;
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 error = EIO;
677 else
678 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 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 biodone(bp, error, bp->b_bcount);
757 }
758
759 /*
760 * sdstart looks to see if there is a buf waiting for the device
761 * and that the device is not already busy. If both are true,
762 * It dequeues the buf and creates a scsi command to perform the
763 * transfer in the buf. The transfer request will call scsipi_done
764 * on completion, which will in turn call this routine again
765 * so that the next queued transfer is performed.
766 * The bufs are queued by the strategy routine (sdstrategy)
767 *
768 * This routine is also called after other non-queued requests
769 * have been made of the scsi driver, to ensure that the queue
770 * continues to be drained.
771 *
772 * must be called at the correct (highish) spl level
773 * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
774 */
775 static void
776 sdstart(struct scsipi_periph *periph)
777 {
778 struct sd_softc *sd = (void *)periph->periph_dev;
779 struct disklabel *lp = sd->sc_dk.dk_label;
780 struct buf *bp = 0;
781 struct scsipi_rw_16 cmd16;
782 struct scsipi_rw_10 cmd_big;
783 struct scsi_rw_6 cmd_small;
784 struct scsipi_generic *cmdp;
785 struct scsipi_xfer *xs;
786 int nblks, cmdlen, error, flags;
787
788 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
789 /*
790 * Check if the device has room for another command
791 */
792 while (periph->periph_active < periph->periph_openings) {
793 /*
794 * there is excess capacity, but a special waits
795 * It'll need the adapter as soon as we clear out of the
796 * way and let it run (user level wait).
797 */
798 if (periph->periph_flags & PERIPH_WAITING) {
799 periph->periph_flags &= ~PERIPH_WAITING;
800 wakeup((void *)periph);
801 return;
802 }
803
804 /*
805 * If the device has become invalid, abort all the
806 * reads and writes until all files have been closed and
807 * re-opened
808 */
809 if (__predict_false(
810 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
811 if ((bp = BUFQ_GET(sd->buf_queue)) != NULL) {
812 biodone(bp, EIO, 0);
813 continue;
814 } else {
815 return;
816 }
817 }
818
819 /*
820 * See if there is a buf with work for us to do..
821 */
822 if ((bp = BUFQ_PEEK(sd->buf_queue)) == NULL)
823 return;
824
825 /*
826 * We have a buf, now we should make a command.
827 */
828
829 if (lp->d_secsize == DEV_BSIZE)
830 nblks = bp->b_bcount >> DEV_BSHIFT;
831 else
832 nblks = howmany(bp->b_bcount, lp->d_secsize);
833
834 /*
835 * Fill out the scsi command. Use the smallest CDB possible
836 * (6-byte, 10-byte, or 16-byte).
837 */
838 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
839 ((nblks & 0xff) == nblks) &&
840 !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
841 /* 6-byte CDB */
842 memset(&cmd_small, 0, sizeof(cmd_small));
843 cmd_small.opcode = (bp->b_flags & B_READ) ?
844 SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
845 _lto3b(bp->b_rawblkno, cmd_small.addr);
846 cmd_small.length = nblks & 0xff;
847 cmdlen = sizeof(cmd_small);
848 cmdp = (struct scsipi_generic *)&cmd_small;
849 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
850 /* 10-byte CDB */
851 memset(&cmd_big, 0, sizeof(cmd_big));
852 cmd_big.opcode = (bp->b_flags & B_READ) ?
853 READ_10 : WRITE_10;
854 _lto4b(bp->b_rawblkno, cmd_big.addr);
855 _lto2b(nblks, cmd_big.length);
856 cmdlen = sizeof(cmd_big);
857 cmdp = (struct scsipi_generic *)&cmd_big;
858 } else {
859 /* 16-byte CDB */
860 memset(&cmd16, 0, sizeof(cmd16));
861 cmd16.opcode = (bp->b_flags & B_READ) ?
862 READ_16 : WRITE_16;
863 _lto8b(bp->b_rawblkno, cmd16.addr);
864 _lto4b(nblks, cmd16.length);
865 cmdlen = sizeof(cmd16);
866 cmdp = (struct scsipi_generic *)&cmd16;
867 }
868
869 /* Instrumentation. */
870 disk_busy(&sd->sc_dk);
871
872 /*
873 * Mark the disk dirty so that the cache will be
874 * flushed on close.
875 */
876 if ((bp->b_flags & B_READ) == 0)
877 sd->flags |= SDF_DIRTY;
878
879 /*
880 * Figure out what flags to use.
881 */
882 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
883 if (bp->b_flags & B_READ)
884 flags |= XS_CTL_DATA_IN;
885 else
886 flags |= XS_CTL_DATA_OUT;
887
888 /*
889 * Call the routine that chats with the adapter.
890 * Note: we cannot sleep as we may be an interrupt
891 */
892 xs = scsipi_make_xs(periph, cmdp, cmdlen,
893 (u_char *)bp->b_data, bp->b_bcount,
894 SDRETRIES, SD_IO_TIMEOUT, bp, flags);
895 if (__predict_false(xs == NULL)) {
896 /*
897 * out of memory. Keep this buffer in the queue, and
898 * retry later.
899 */
900 callout_reset(&sd->sc_callout, hz / 2, sdrestart,
901 periph);
902 return;
903 }
904 /*
905 * need to dequeue the buffer before queuing the command,
906 * because cdstart may be called recursively from the
907 * HBA driver
908 */
909 #ifdef DIAGNOSTIC
910 if (BUFQ_GET(sd->buf_queue) != bp)
911 panic("sdstart(): dequeued wrong buf");
912 #else
913 BUFQ_GET(sd->buf_queue);
914 #endif
915 error = scsipi_execute_xs(xs);
916 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
917 KASSERT(error == 0);
918 }
919 }
920
921 static void
922 sdrestart(void *v)
923 {
924 int s = splbio();
925 sdstart((struct scsipi_periph *)v);
926 splx(s);
927 }
928
929 static void
930 sddone(struct scsipi_xfer *xs, int error)
931 {
932 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
933 struct buf *bp = xs->bp;
934
935 if (sd->flags & SDF_FLUSHING) {
936 /* Flush completed, no longer dirty. */
937 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
938 }
939
940 if (bp) {
941 disk_unbusy(&sd->sc_dk, bp->b_bcount - xs->resid,
942 (bp->b_flags & B_READ));
943 #if NRND > 0
944 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
945 #endif
946 biodone(bp, error, xs->resid);
947 }
948 }
949
950 static void
951 sdminphys(struct buf *bp)
952 {
953 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
954 long xmax;
955
956 /*
957 * If the device is ancient, we want to make sure that
958 * the transfer fits into a 6-byte cdb.
959 *
960 * XXX Note that the SCSI-I spec says that 256-block transfers
961 * are allowed in a 6-byte read/write, and are specified
962 * by settng the "length" to 0. However, we're conservative
963 * here, allowing only 255-block transfers in case an
964 * ancient device gets confused by length == 0. A length of 0
965 * in a 10-byte read/write actually means 0 blocks.
966 */
967 if ((sd->flags & SDF_ANCIENT) &&
968 ((sd->sc_periph->periph_flags &
969 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
970 xmax = sd->sc_dk.dk_label->d_secsize * 0xff;
971
972 if (bp->b_bcount > xmax)
973 bp->b_bcount = xmax;
974 }
975
976 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
977 }
978
979 static int
980 sdread(dev_t dev, struct uio *uio, int ioflag)
981 {
982
983 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
984 }
985
986 static int
987 sdwrite(dev_t dev, struct uio *uio, int ioflag)
988 {
989
990 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
991 }
992
993 /*
994 * Perform special action on behalf of the user
995 * Knows about the internals of this device
996 */
997 static int
998 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
999 {
1000 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
1001 struct scsipi_periph *periph = sd->sc_periph;
1002 int part = SDPART(dev);
1003 int error = 0;
1004 #ifdef __HAVE_OLD_DISKLABEL
1005 struct disklabel *newlabel = NULL;
1006 #endif
1007
1008 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
1009
1010 /*
1011 * If the device is not valid, some IOCTLs can still be
1012 * handled on the raw partition. Check this here.
1013 */
1014 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1015 switch (cmd) {
1016 case DIOCKLABEL:
1017 case DIOCWLABEL:
1018 case DIOCLOCK:
1019 case DIOCEJECT:
1020 case ODIOCEJECT:
1021 case DIOCGCACHE:
1022 case DIOCSCACHE:
1023 case SCIOCIDENTIFY:
1024 case OSCIOCIDENTIFY:
1025 case SCIOCCOMMAND:
1026 case SCIOCDEBUG:
1027 if (part == RAW_PART)
1028 break;
1029 /* FALLTHROUGH */
1030 default:
1031 if ((periph->periph_flags & PERIPH_OPEN) == 0)
1032 return (ENODEV);
1033 else
1034 return (EIO);
1035 }
1036 }
1037
1038 switch (cmd) {
1039 case DIOCGDINFO:
1040 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1041 return (0);
1042
1043 #ifdef __HAVE_OLD_DISKLABEL
1044 case ODIOCGDINFO:
1045 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1046 if (newlabel == NULL)
1047 return EIO;
1048 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1049 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1050 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1051 else
1052 error = ENOTTY;
1053 free(newlabel, M_TEMP);
1054 return error;
1055 #endif
1056
1057 case DIOCGPART:
1058 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1059 ((struct partinfo *)addr)->part =
1060 &sd->sc_dk.dk_label->d_partitions[part];
1061 return (0);
1062
1063 case DIOCWDINFO:
1064 case DIOCSDINFO:
1065 #ifdef __HAVE_OLD_DISKLABEL
1066 case ODIOCWDINFO:
1067 case ODIOCSDINFO:
1068 #endif
1069 {
1070 struct disklabel *lp;
1071
1072 if ((flag & FWRITE) == 0)
1073 return (EBADF);
1074
1075 #ifdef __HAVE_OLD_DISKLABEL
1076 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1077 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1078 if (newlabel == NULL)
1079 return EIO;
1080 memset(newlabel, 0, sizeof newlabel);
1081 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1082 lp = newlabel;
1083 } else
1084 #endif
1085 lp = (struct disklabel *)addr;
1086
1087 if ((error = lockmgr(&sd->sc_dk.dk_openlock,
1088 LK_EXCLUSIVE, NULL)) != 0)
1089 goto bad;
1090 sd->flags |= SDF_LABELLING;
1091
1092 error = setdisklabel(sd->sc_dk.dk_label,
1093 lp, /*sd->sc_dk.dk_openmask : */0,
1094 sd->sc_dk.dk_cpulabel);
1095 if (error == 0) {
1096 if (cmd == DIOCWDINFO
1097 #ifdef __HAVE_OLD_DISKLABEL
1098 || cmd == ODIOCWDINFO
1099 #endif
1100 )
1101 error = writedisklabel(SDLABELDEV(dev),
1102 sdstrategy, sd->sc_dk.dk_label,
1103 sd->sc_dk.dk_cpulabel);
1104 }
1105
1106 sd->flags &= ~SDF_LABELLING;
1107 (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL);
1108 bad:
1109 #ifdef __HAVE_OLD_DISKLABEL
1110 if (newlabel != NULL)
1111 free(newlabel, M_TEMP);
1112 #endif
1113 return (error);
1114 }
1115
1116 case DIOCKLABEL:
1117 if (*(int *)addr)
1118 periph->periph_flags |= PERIPH_KEEP_LABEL;
1119 else
1120 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1121 return (0);
1122
1123 case DIOCWLABEL:
1124 if ((flag & FWRITE) == 0)
1125 return (EBADF);
1126 if (*(int *)addr)
1127 sd->flags |= SDF_WLABEL;
1128 else
1129 sd->flags &= ~SDF_WLABEL;
1130 return (0);
1131
1132 case DIOCLOCK:
1133 if (periph->periph_flags & PERIPH_REMOVABLE)
1134 return (scsipi_prevent(periph,
1135 (*(int *)addr) ?
1136 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
1137 else
1138 return (ENOTTY);
1139
1140 case DIOCEJECT:
1141 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1142 return (ENOTTY);
1143 if (*(int *)addr == 0) {
1144 /*
1145 * Don't force eject: check that we are the only
1146 * partition open. If so, unlock it.
1147 */
1148 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1149 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1150 sd->sc_dk.dk_openmask) {
1151 error = scsipi_prevent(periph, SPAMR_ALLOW,
1152 XS_CTL_IGNORE_NOT_READY);
1153 if (error)
1154 return (error);
1155 } else {
1156 return (EBUSY);
1157 }
1158 }
1159 /* FALLTHROUGH */
1160 case ODIOCEJECT:
1161 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1162 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1163
1164 case DIOCGDEFLABEL:
1165 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1166 return (0);
1167
1168 #ifdef __HAVE_OLD_DISKLABEL
1169 case ODIOCGDEFLABEL:
1170 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1171 if (newlabel == NULL)
1172 return EIO;
1173 sdgetdefaultlabel(sd, newlabel);
1174 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1175 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1176 else
1177 error = ENOTTY;
1178 free(newlabel, M_TEMP);
1179 return error;
1180 #endif
1181
1182 case DIOCGCACHE:
1183 return (sd_getcache(sd, (int *) addr));
1184
1185 case DIOCSCACHE:
1186 if ((flag & FWRITE) == 0)
1187 return (EBADF);
1188 return (sd_setcache(sd, *(int *) addr));
1189
1190 case DIOCCACHESYNC:
1191 /*
1192 * XXX Do we really need to care about having a writable
1193 * file descriptor here?
1194 */
1195 if ((flag & FWRITE) == 0)
1196 return (EBADF);
1197 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1198 error = sd_flush(sd, 0);
1199 if (error)
1200 sd->flags &= ~SDF_FLUSHING;
1201 else
1202 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1203 } else
1204 error = 0;
1205 return (error);
1206
1207 case DIOCAWEDGE:
1208 {
1209 struct dkwedge_info *dkw = (void *) addr;
1210
1211 if ((flag & FWRITE) == 0)
1212 return (EBADF);
1213
1214 /* If the ioctl happens here, the parent is us. */
1215 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
1216 return (dkwedge_add(dkw));
1217 }
1218
1219 case DIOCDWEDGE:
1220 {
1221 struct dkwedge_info *dkw = (void *) addr;
1222
1223 if ((flag & FWRITE) == 0)
1224 return (EBADF);
1225
1226 /* If the ioctl happens here, the parent is us. */
1227 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
1228 return (dkwedge_del(dkw));
1229 }
1230
1231 case DIOCLWEDGES:
1232 {
1233 struct dkwedge_list *dkwl = (void *) addr;
1234
1235 return (dkwedge_list(&sd->sc_dk, dkwl, l));
1236 }
1237
1238 default:
1239 if (part != RAW_PART)
1240 return (ENOTTY);
1241 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
1242 }
1243
1244 #ifdef DIAGNOSTIC
1245 panic("sdioctl: impossible");
1246 #endif
1247 }
1248
1249 static void
1250 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1251 {
1252
1253 memset(lp, 0, sizeof(struct disklabel));
1254
1255 lp->d_secsize = sd->params.blksize;
1256 lp->d_ntracks = sd->params.heads;
1257 lp->d_nsectors = sd->params.sectors;
1258 lp->d_ncylinders = sd->params.cyls;
1259 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1260
1261 switch (scsipi_periph_bustype(sd->sc_periph)) {
1262 case SCSIPI_BUSTYPE_SCSI:
1263 lp->d_type = DTYPE_SCSI;
1264 break;
1265 case SCSIPI_BUSTYPE_ATAPI:
1266 lp->d_type = DTYPE_ATAPI;
1267 break;
1268 }
1269 /*
1270 * XXX
1271 * We could probe the mode pages to figure out what kind of disc it is.
1272 * Is this worthwhile?
1273 */
1274 strncpy(lp->d_typename, sd->name, 16);
1275 strncpy(lp->d_packname, "fictitious", 16);
1276 lp->d_secperunit = sd->params.disksize;
1277 lp->d_rpm = sd->params.rot_rate;
1278 lp->d_interleave = 1;
1279 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1280 D_REMOVABLE : 0;
1281
1282 lp->d_partitions[RAW_PART].p_offset = 0;
1283 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1284 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1285 lp->d_npartitions = RAW_PART + 1;
1286
1287 lp->d_magic = DISKMAGIC;
1288 lp->d_magic2 = DISKMAGIC;
1289 lp->d_checksum = dkcksum(lp);
1290 }
1291
1292
1293 /*
1294 * Load the label information on the named device
1295 */
1296 static int
1297 sdgetdisklabel(struct sd_softc *sd)
1298 {
1299 struct disklabel *lp = sd->sc_dk.dk_label;
1300 const char *errstring;
1301
1302 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1303
1304 sdgetdefaultlabel(sd, lp);
1305
1306 if (lp->d_secpercyl == 0) {
1307 lp->d_secpercyl = 100;
1308 /* as long as it's not 0 - readdisklabel divides by it (?) */
1309 }
1310
1311 /*
1312 * Call the generic disklabel extraction routine
1313 */
1314 errstring = readdisklabel(MAKESDDEV(0, device_unit(&sd->sc_dev),
1315 RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1316 if (errstring) {
1317 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1318 return EIO;
1319 }
1320 return 0;
1321 }
1322
1323 static void
1324 sd_shutdown(void *arg)
1325 {
1326 struct sd_softc *sd = arg;
1327
1328 /*
1329 * If the disk cache needs to be flushed, and the disk supports
1330 * it, flush it. We're cold at this point, so we poll for
1331 * completion.
1332 */
1333 if ((sd->flags & SDF_DIRTY) != 0) {
1334 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1335 printf("%s: cache synchronization failed\n",
1336 sd->sc_dev.dv_xname);
1337 sd->flags &= ~SDF_FLUSHING;
1338 } else
1339 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1340 }
1341 }
1342
1343 /*
1344 * Check Errors
1345 */
1346 static int
1347 sd_interpret_sense(struct scsipi_xfer *xs)
1348 {
1349 struct scsipi_periph *periph = xs->xs_periph;
1350 struct scsi_sense_data *sense = &xs->sense.scsi_sense;
1351 struct sd_softc *sd = (void *)periph->periph_dev;
1352 int s, error, retval = EJUSTRETURN;
1353
1354 /*
1355 * If the periph is already recovering, just do the normal
1356 * error processing.
1357 */
1358 if (periph->periph_flags & PERIPH_RECOVERING)
1359 return (retval);
1360
1361 /*
1362 * Ignore errors from accessing illegal fields (e.g. trying to
1363 * lock the door of a digicam, which doesn't have a door that
1364 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
1365 */
1366 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
1367 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
1368 sense->asc == 0x24 &&
1369 sense->ascq == 0x00) { /* Illegal field in CDB */
1370 if (!(xs->xs_control & XS_CTL_SILENT)) {
1371 scsipi_printaddr(periph);
1372 printf("no door lock\n");
1373 }
1374 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
1375 return (retval);
1376 }
1377
1378
1379
1380 /*
1381 * If the device is not open yet, let the generic code handle it.
1382 */
1383 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1384 return (retval);
1385
1386 /*
1387 * If it isn't a extended or extended/deferred error, let
1388 * the generic code handle it.
1389 */
1390 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
1391 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
1392 return (retval);
1393
1394 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
1395 sense->asc == 0x4) {
1396 if (sense->ascq == 0x01) {
1397 /*
1398 * Unit In The Process Of Becoming Ready.
1399 */
1400 printf("%s: waiting for pack to spin up...\n",
1401 sd->sc_dev.dv_xname);
1402 if (!callout_pending(&periph->periph_callout))
1403 scsipi_periph_freeze(periph, 1);
1404 callout_reset(&periph->periph_callout,
1405 5 * hz, scsipi_periph_timed_thaw, periph);
1406 retval = ERESTART;
1407 } else if (sense->ascq == 0x02) {
1408 printf("%s: pack is stopped, restarting...\n",
1409 sd->sc_dev.dv_xname);
1410 s = splbio();
1411 periph->periph_flags |= PERIPH_RECOVERING;
1412 splx(s);
1413 error = scsipi_start(periph, SSS_START,
1414 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1415 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1416 if (error) {
1417 printf("%s: unable to restart pack\n",
1418 sd->sc_dev.dv_xname);
1419 retval = error;
1420 } else
1421 retval = ERESTART;
1422 s = splbio();
1423 periph->periph_flags &= ~PERIPH_RECOVERING;
1424 splx(s);
1425 }
1426 }
1427 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
1428 sense->asc == 0x31 &&
1429 sense->ascq == 0x00) { /* maybe for any asq ? */
1430 /* Medium Format Corrupted */
1431 retval = EFTYPE;
1432 }
1433 return (retval);
1434 }
1435
1436
1437 static int
1438 sdsize(dev_t dev)
1439 {
1440 struct sd_softc *sd;
1441 int part, unit, omask;
1442 int size;
1443
1444 unit = SDUNIT(dev);
1445 if (unit >= sd_cd.cd_ndevs)
1446 return (-1);
1447 sd = sd_cd.cd_devs[unit];
1448 if (sd == NULL)
1449 return (-1);
1450
1451 if (!device_is_active(&sd->sc_dev))
1452 return (-1);
1453
1454 part = SDPART(dev);
1455 omask = sd->sc_dk.dk_openmask & (1 << part);
1456
1457 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1458 return (-1);
1459 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1460 size = -1;
1461 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1462 size = -1;
1463 else
1464 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1465 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1466 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1467 return (-1);
1468 return (size);
1469 }
1470
1471 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1472 static struct scsipi_xfer sx;
1473 static int sddoingadump;
1474
1475 /*
1476 * dump all of physical memory into the partition specified, starting
1477 * at offset 'dumplo' into the partition.
1478 */
1479 static int
1480 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1481 {
1482 struct sd_softc *sd; /* disk unit to do the I/O */
1483 struct disklabel *lp; /* disk's disklabel */
1484 int unit, part;
1485 int sectorsize; /* size of a disk sector */
1486 int nsects; /* number of sectors in partition */
1487 int sectoff; /* sector offset of partition */
1488 int totwrt; /* total number of sectors left to write */
1489 int nwrt; /* current number of sectors to write */
1490 struct scsipi_rw_10 cmd; /* write command */
1491 struct scsipi_xfer *xs; /* ... convenience */
1492 struct scsipi_periph *periph;
1493 struct scsipi_channel *chan;
1494
1495 /* Check if recursive dump; if so, punt. */
1496 if (sddoingadump)
1497 return (EFAULT);
1498
1499 /* Mark as active early. */
1500 sddoingadump = 1;
1501
1502 unit = SDUNIT(dev); /* Decompose unit & partition. */
1503 part = SDPART(dev);
1504
1505 /* Check for acceptable drive number. */
1506 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1507 return (ENXIO);
1508
1509 if (!device_is_active(&sd->sc_dev))
1510 return (ENODEV);
1511
1512 periph = sd->sc_periph;
1513 chan = periph->periph_channel;
1514
1515 /* Make sure it was initialized. */
1516 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1517 return (ENXIO);
1518
1519 /* Convert to disk sectors. Request must be a multiple of size. */
1520 lp = sd->sc_dk.dk_label;
1521 sectorsize = lp->d_secsize;
1522 if ((size % sectorsize) != 0)
1523 return (EFAULT);
1524 totwrt = size / sectorsize;
1525 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1526
1527 nsects = lp->d_partitions[part].p_size;
1528 sectoff = lp->d_partitions[part].p_offset;
1529
1530 /* Check transfer bounds against partition size. */
1531 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1532 return (EINVAL);
1533
1534 /* Offset block number to start of partition. */
1535 blkno += sectoff;
1536
1537 xs = &sx;
1538
1539 while (totwrt > 0) {
1540 nwrt = totwrt; /* XXX */
1541 #ifndef SD_DUMP_NOT_TRUSTED
1542 /*
1543 * Fill out the scsi command
1544 */
1545 memset(&cmd, 0, sizeof(cmd));
1546 cmd.opcode = WRITE_10;
1547 _lto4b(blkno, cmd.addr);
1548 _lto2b(nwrt, cmd.length);
1549 /*
1550 * Fill out the scsipi_xfer structure
1551 * Note: we cannot sleep as we may be an interrupt
1552 * don't use scsipi_command() as it may want to wait
1553 * for an xs.
1554 */
1555 memset(xs, 0, sizeof(sx));
1556 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1557 XS_CTL_DATA_OUT;
1558 xs->xs_status = 0;
1559 xs->xs_periph = periph;
1560 xs->xs_retries = SDRETRIES;
1561 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1562 xs->cmd = (struct scsipi_generic *)&cmd;
1563 xs->cmdlen = sizeof(cmd);
1564 xs->resid = nwrt * sectorsize;
1565 xs->error = XS_NOERROR;
1566 xs->bp = 0;
1567 xs->data = va;
1568 xs->datalen = nwrt * sectorsize;
1569
1570 /*
1571 * Pass all this info to the scsi driver.
1572 */
1573 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1574 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1575 xs->error != XS_NOERROR)
1576 return (EIO);
1577 #else /* SD_DUMP_NOT_TRUSTED */
1578 /* Let's just talk about this first... */
1579 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1580 delay(500 * 1000); /* half a second */
1581 #endif /* SD_DUMP_NOT_TRUSTED */
1582
1583 /* update block count */
1584 totwrt -= nwrt;
1585 blkno += nwrt;
1586 va = (char *)va + sectorsize * nwrt;
1587 }
1588 sddoingadump = 0;
1589 return (0);
1590 }
1591
1592 static int
1593 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1594 int page, int flags, int *big)
1595 {
1596
1597 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1598 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1599 *big = 1;
1600 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1601 size + sizeof(struct scsi_mode_parameter_header_10),
1602 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1603 } else {
1604 *big = 0;
1605 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1606 size + sizeof(struct scsi_mode_parameter_header_6),
1607 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1608 }
1609 }
1610
1611 static int
1612 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1613 int flags, int big)
1614 {
1615
1616 if (big) {
1617 struct scsi_mode_parameter_header_10 *header = sense;
1618
1619 _lto2b(0, header->data_length);
1620 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1621 size + sizeof(struct scsi_mode_parameter_header_10),
1622 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1623 } else {
1624 struct scsi_mode_parameter_header_6 *header = sense;
1625
1626 header->data_length = 0;
1627 return scsipi_mode_select(sd->sc_periph, byte2, sense,
1628 size + sizeof(struct scsi_mode_parameter_header_6),
1629 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1630 }
1631 }
1632
1633 /*
1634 * sd_validate_blksize:
1635 *
1636 * Validate the block size. Print error if periph is specified,
1637 */
1638 static int
1639 sd_validate_blksize(struct scsipi_periph *periph, int len)
1640 {
1641
1642 switch (len) {
1643 case 256:
1644 case 512:
1645 case 1024:
1646 case 2048:
1647 case 4096:
1648 return 1;
1649 }
1650
1651 if (periph) {
1652 scsipi_printaddr(periph);
1653 printf("%s sector size: 0x%x. Defaulting to %d bytes.\n",
1654 (len ^ (1 << (ffs(len) - 1))) ?
1655 "preposterous" : "unsupported",
1656 len, SD_DEFAULT_BLKSIZE);
1657 }
1658
1659 return 0;
1660 }
1661
1662 /*
1663 * sd_read_capacity:
1664 *
1665 * Find out from the device what its capacity is.
1666 */
1667 static u_int64_t
1668 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
1669 {
1670 union {
1671 struct scsipi_read_capacity_10 cmd;
1672 struct scsipi_read_capacity_16 cmd16;
1673 } cmd;
1674 union {
1675 struct scsipi_read_capacity_10_data data;
1676 struct scsipi_read_capacity_16_data data16;
1677 } data;
1678
1679 memset(&cmd, 0, sizeof(cmd));
1680 cmd.cmd.opcode = READ_CAPACITY_10;
1681
1682 /*
1683 * If the command works, interpret the result as a 4 byte
1684 * number of blocks
1685 */
1686 memset(&data.data, 0, sizeof(data.data));
1687 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
1688 (void *)&data.data, sizeof(data.data), SCSIPIRETRIES, 20000, NULL,
1689 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT) != 0)
1690 return (0);
1691
1692 if (_4btol(data.data.addr) != 0xffffffff) {
1693 *blksize = _4btol(data.data.length);
1694 return (_4btol(data.data.addr) + 1);
1695 }
1696
1697 /*
1698 * Device is larger than can be reflected by READ CAPACITY (10).
1699 * Try READ CAPACITY (16).
1700 */
1701
1702 memset(&cmd, 0, sizeof(cmd));
1703 cmd.cmd16.opcode = READ_CAPACITY_16;
1704 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
1705 _lto4b(sizeof(data.data16), cmd.cmd16.len);
1706
1707 memset(&data.data16, 0, sizeof(data.data16));
1708 if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
1709 (void *)&data.data16, sizeof(data.data16), SCSIPIRETRIES, 20000,
1710 NULL,
1711 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK | XS_CTL_SILENT) != 0)
1712 return (0);
1713
1714 *blksize = _4btol(data.data16.length);
1715 return (_8btol(data.data16.addr) + 1);
1716 }
1717
1718 static int
1719 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1720 {
1721 struct {
1722 struct scsi_mode_parameter_header_6 header;
1723 /* no block descriptor */
1724 u_int8_t pg_code; /* page code (should be 6) */
1725 u_int8_t pg_length; /* page length (should be 11) */
1726 u_int8_t wcd; /* bit0: cache disable */
1727 u_int8_t lbs[2]; /* logical block size */
1728 u_int8_t size[5]; /* number of log. blocks */
1729 u_int8_t pp; /* power/performance */
1730 u_int8_t flags;
1731 u_int8_t resvd;
1732 } scsipi_sense;
1733 u_int64_t blocks;
1734 int error, blksize;
1735
1736 /*
1737 * sd_read_capacity (ie "read capacity") and mode sense page 6
1738 * give the same information. Do both for now, and check
1739 * for consistency.
1740 * XXX probably differs for removable media
1741 */
1742 dp->blksize = SD_DEFAULT_BLKSIZE;
1743 if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
1744 return (SDGP_RESULT_OFFLINE); /* XXX? */
1745
1746 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1747 &scsipi_sense.header, sizeof(scsipi_sense),
1748 flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
1749
1750 if (error != 0)
1751 return (SDGP_RESULT_OFFLINE); /* XXX? */
1752
1753 dp->blksize = blksize;
1754 if (!sd_validate_blksize(NULL, dp->blksize))
1755 dp->blksize = _2btol(scsipi_sense.lbs);
1756 if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
1757 dp->blksize = SD_DEFAULT_BLKSIZE;
1758
1759 /*
1760 * Create a pseudo-geometry.
1761 */
1762 dp->heads = 64;
1763 dp->sectors = 32;
1764 dp->cyls = blocks / (dp->heads * dp->sectors);
1765 dp->disksize = _5btol(scsipi_sense.size);
1766 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
1767 printf("RBC size: mode sense=%llu, get cap=%llu\n",
1768 (unsigned long long)dp->disksize,
1769 (unsigned long long)blocks);
1770 dp->disksize = blocks;
1771 }
1772 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1773
1774 return (SDGP_RESULT_OK);
1775 }
1776
1777 /*
1778 * Get the scsi driver to send a full inquiry to the * device and use the
1779 * results to fill out the disk parameter structure.
1780 */
1781 static int
1782 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1783 {
1784 u_int64_t blocks;
1785 int error, blksize;
1786 #if 0
1787 int i;
1788 u_int8_t *p;
1789 #endif
1790
1791 dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
1792 flags);
1793 if (blocks == 0) {
1794 struct scsipi_read_format_capacities cmd;
1795 struct {
1796 struct scsipi_capacity_list_header header;
1797 struct scsipi_capacity_descriptor desc;
1798 } __attribute__((packed)) data;
1799
1800 memset(&cmd, 0, sizeof(cmd));
1801 memset(&data, 0, sizeof(data));
1802 cmd.opcode = READ_FORMAT_CAPACITIES;
1803 _lto2b(sizeof(data), cmd.length);
1804
1805 error = scsipi_command(sd->sc_periph,
1806 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1807 SDRETRIES, 20000, NULL,
1808 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1809 if (error == EFTYPE) {
1810 /* Medium Format Corrupted, handle as not formatted */
1811 return (SDGP_RESULT_UNFORMATTED);
1812 }
1813 if (error || data.header.length == 0)
1814 return (SDGP_RESULT_OFFLINE);
1815
1816 #if 0
1817 printf("rfc: length=%d\n", data.header.length);
1818 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");
1819 #endif
1820 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1821 case SCSIPI_CAP_DESC_CODE_RESERVED:
1822 case SCSIPI_CAP_DESC_CODE_FORMATTED:
1823 break;
1824
1825 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1826 return (SDGP_RESULT_UNFORMATTED);
1827
1828 case SCSIPI_CAP_DESC_CODE_NONE:
1829 return (SDGP_RESULT_OFFLINE);
1830 }
1831
1832 dp->disksize = blocks = _4btol(data.desc.nblks);
1833 if (blocks == 0)
1834 return (SDGP_RESULT_OFFLINE); /* XXX? */
1835
1836 blksize = _3btol(data.desc.blklen);
1837
1838 } else if (!sd_validate_blksize(NULL, blksize)) {
1839 struct sd_mode_sense_data scsipi_sense;
1840 int big, bsize;
1841 struct scsi_general_block_descriptor *bdesc;
1842
1843 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1844 error = sd_mode_sense(sd, 0, &scsipi_sense,
1845 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1846 if (!error) {
1847 if (big) {
1848 bdesc = (void *)(&scsipi_sense.header.big + 1);
1849 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1850 } else {
1851 bdesc = (void *)(&scsipi_sense.header.small + 1);
1852 bsize = scsipi_sense.header.small.blk_desc_len;
1853 }
1854
1855 #if 0
1856 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1857 printf("page 0 bsize=%d\n", bsize);
1858 printf("page 0 ok\n");
1859 #endif
1860
1861 if (bsize >= 8) {
1862 blksize = _3btol(bdesc->blklen);
1863 }
1864 }
1865 }
1866
1867 if (!sd_validate_blksize(sd->sc_periph, blksize))
1868 blksize = SD_DEFAULT_BLKSIZE;
1869
1870 dp->blksize = blksize;
1871 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
1872 return (0);
1873 }
1874
1875 static int
1876 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1877 {
1878 struct sd_mode_sense_data scsipi_sense;
1879 int error;
1880 int big, byte2;
1881 size_t poffset;
1882 union scsi_disk_pages *pages;
1883
1884 byte2 = SMS_DBD;
1885 again:
1886 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1887 error = sd_mode_sense(sd, byte2, &scsipi_sense,
1888 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1889 sizeof(scsipi_sense.pages.rigid_geometry), 4,
1890 flags | XS_CTL_SILENT, &big);
1891 if (error) {
1892 if (byte2 == SMS_DBD) {
1893 /* No result; try once more with DBD off */
1894 byte2 = 0;
1895 goto again;
1896 }
1897 return (error);
1898 }
1899
1900 if (big) {
1901 poffset = sizeof scsipi_sense.header.big;
1902 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1903 } else {
1904 poffset = sizeof scsipi_sense.header.small;
1905 poffset += scsipi_sense.header.small.blk_desc_len;
1906 }
1907
1908 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
1909 return ERESTART;
1910
1911 pages = (void *)((u_long)&scsipi_sense + poffset);
1912 #if 0
1913 {
1914 size_t i;
1915 u_int8_t *p;
1916
1917 printf("page 4 sense:");
1918 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
1919 i--, p++)
1920 printf(" %02x", *p);
1921 printf("\n");
1922 printf("page 4 pg_code=%d sense=%p/%p\n",
1923 pages->rigid_geometry.pg_code, &scsipi_sense, pages);
1924 }
1925 #endif
1926
1927 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
1928 return (ERESTART);
1929
1930 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
1931 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1932 _3btol(pages->rigid_geometry.ncyl),
1933 pages->rigid_geometry.nheads,
1934 _2btol(pages->rigid_geometry.st_cyl_wp),
1935 _2btol(pages->rigid_geometry.st_cyl_rwc),
1936 _2btol(pages->rigid_geometry.land_zone)));
1937
1938 /*
1939 * KLUDGE!! (for zone recorded disks)
1940 * give a number of sectors so that sec * trks * cyls
1941 * is <= disk_size
1942 * can lead to wasted space! THINK ABOUT THIS !
1943 */
1944 dp->heads = pages->rigid_geometry.nheads;
1945 dp->cyls = _3btol(pages->rigid_geometry.ncyl);
1946 if (dp->heads == 0 || dp->cyls == 0)
1947 return (ERESTART);
1948 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
1949
1950 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
1951 if (dp->rot_rate == 0)
1952 dp->rot_rate = 3600;
1953
1954 #if 0
1955 printf("page 4 ok\n");
1956 #endif
1957 return (0);
1958 }
1959
1960 static int
1961 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
1962 {
1963 struct sd_mode_sense_data scsipi_sense;
1964 int error;
1965 int big, byte2;
1966 size_t poffset;
1967 union scsi_disk_pages *pages;
1968
1969 byte2 = SMS_DBD;
1970 again:
1971 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1972 error = sd_mode_sense(sd, 0, &scsipi_sense,
1973 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1974 sizeof(scsipi_sense.pages.flex_geometry), 5,
1975 flags | XS_CTL_SILENT, &big);
1976 if (error) {
1977 if (byte2 == SMS_DBD) {
1978 /* No result; try once more with DBD off */
1979 byte2 = 0;
1980 goto again;
1981 }
1982 return (error);
1983 }
1984
1985 if (big) {
1986 poffset = sizeof scsipi_sense.header.big;
1987 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1988 } else {
1989 poffset = sizeof scsipi_sense.header.small;
1990 poffset += scsipi_sense.header.small.blk_desc_len;
1991 }
1992
1993 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
1994 return ERESTART;
1995
1996 pages = (void *)((u_long)&scsipi_sense + poffset);
1997 #if 0
1998 {
1999 size_t i;
2000 u_int8_t *p;
2001
2002 printf("page 5 sense:");
2003 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
2004 i--, p++)
2005 printf(" %02x", *p);
2006 printf("\n");
2007 printf("page 5 pg_code=%d sense=%p/%p\n",
2008 pages->flex_geometry.pg_code, &scsipi_sense, pages);
2009 }
2010 #endif
2011
2012 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
2013 return (ERESTART);
2014
2015 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
2016 ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
2017 _3btol(pages->flex_geometry.ncyl),
2018 pages->flex_geometry.nheads,
2019 pages->flex_geometry.ph_sec_tr,
2020 _2btol(pages->flex_geometry.bytes_s)));
2021
2022 dp->heads = pages->flex_geometry.nheads;
2023 dp->cyls = _2btol(pages->flex_geometry.ncyl);
2024 dp->sectors = pages->flex_geometry.ph_sec_tr;
2025 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
2026 return (ERESTART);
2027
2028 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
2029 if (dp->rot_rate == 0)
2030 dp->rot_rate = 3600;
2031
2032 #if 0
2033 printf("page 5 ok\n");
2034 #endif
2035 return (0);
2036 }
2037
2038 static int
2039 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
2040 {
2041 int error;
2042
2043 /*
2044 * If offline, the SDEV_MEDIA_LOADED flag will be
2045 * cleared by the caller if necessary.
2046 */
2047 if (sd->type == T_SIMPLE_DIRECT) {
2048 error = sd_get_simplifiedparms(sd, dp, flags);
2049 if (!error)
2050 disk_blocksize(&sd->sc_dk, dp->blksize);
2051 return (error);
2052 }
2053
2054 error = sd_get_capacity(sd, dp, flags);
2055 if (error)
2056 return (error);
2057
2058 disk_blocksize(&sd->sc_dk, dp->blksize);
2059
2060 if (sd->type == T_OPTICAL)
2061 goto page0;
2062
2063 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
2064 if (!sd_get_parms_page5(sd, dp, flags) ||
2065 !sd_get_parms_page4(sd, dp, flags))
2066 return (SDGP_RESULT_OK);
2067 } else {
2068 if (!sd_get_parms_page4(sd, dp, flags) ||
2069 !sd_get_parms_page5(sd, dp, flags))
2070 return (SDGP_RESULT_OK);
2071 }
2072
2073 page0:
2074 printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname);
2075 /* Try calling driver's method for figuring out geometry. */
2076 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
2077 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
2078 (sd->sc_periph, dp, dp->disksize)) {
2079 /*
2080 * Use adaptec standard fictitious geometry
2081 * this depends on which controller (e.g. 1542C is
2082 * different. but we have to put SOMETHING here..)
2083 */
2084 dp->heads = 64;
2085 dp->sectors = 32;
2086 dp->cyls = dp->disksize / (64 * 32);
2087 }
2088 dp->rot_rate = 3600;
2089 return (SDGP_RESULT_OK);
2090 }
2091
2092 static int
2093 sd_flush(struct sd_softc *sd, int flags)
2094 {
2095 struct scsipi_periph *periph = sd->sc_periph;
2096 struct scsi_synchronize_cache_10 cmd;
2097
2098 /*
2099 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
2100 * We issue with address 0 length 0, which should be
2101 * interpreted by the device as "all remaining blocks
2102 * starting at address 0". We ignore ILLEGAL REQUEST
2103 * in the event that the command is not supported by
2104 * the device, and poll for completion so that we know
2105 * that the cache has actually been flushed.
2106 *
2107 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
2108 * command, as indicated by our quirks flags.
2109 *
2110 * XXX What about older devices?
2111 */
2112 if (periph->periph_version < 2 ||
2113 (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
2114 return (0);
2115
2116 sd->flags |= SDF_FLUSHING;
2117 memset(&cmd, 0, sizeof(cmd));
2118 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
2119
2120 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
2121 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
2122 }
2123
2124 static int
2125 sd_getcache(struct sd_softc *sd, int *bitsp)
2126 {
2127 struct scsipi_periph *periph = sd->sc_periph;
2128 struct sd_mode_sense_data scsipi_sense;
2129 int error, bits = 0;
2130 int big;
2131 union scsi_disk_pages *pages;
2132
2133 if (periph->periph_version < 2)
2134 return (EOPNOTSUPP);
2135
2136 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2137 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2138 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2139 if (error)
2140 return (error);
2141
2142 if (big)
2143 pages = (void *)(&scsipi_sense.header.big + 1);
2144 else
2145 pages = (void *)(&scsipi_sense.header.small + 1);
2146
2147 if ((pages->caching_params.flags & CACHING_RCD) == 0)
2148 bits |= DKCACHE_READ;
2149 if (pages->caching_params.flags & CACHING_WCE)
2150 bits |= DKCACHE_WRITE;
2151 if (pages->caching_params.pg_code & PGCODE_PS)
2152 bits |= DKCACHE_SAVE;
2153
2154 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2155 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2156 sizeof(scsipi_sense.pages.caching_params),
2157 SMS_PCTRL_CHANGEABLE|8, 0, &big);
2158 if (error == 0) {
2159 if (big)
2160 pages = (void *)(&scsipi_sense.header.big + 1);
2161 else
2162 pages = (void *)(&scsipi_sense.header.small + 1);
2163
2164 if (pages->caching_params.flags & CACHING_RCD)
2165 bits |= DKCACHE_RCHANGE;
2166 if (pages->caching_params.flags & CACHING_WCE)
2167 bits |= DKCACHE_WCHANGE;
2168 }
2169
2170 *bitsp = bits;
2171
2172 return (0);
2173 }
2174
2175 static int
2176 sd_setcache(struct sd_softc *sd, int bits)
2177 {
2178 struct scsipi_periph *periph = sd->sc_periph;
2179 struct sd_mode_sense_data scsipi_sense;
2180 int error;
2181 uint8_t oflags, byte2 = 0;
2182 int big;
2183 union scsi_disk_pages *pages;
2184
2185 if (periph->periph_version < 2)
2186 return (EOPNOTSUPP);
2187
2188 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2189 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2190 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2191 if (error)
2192 return (error);
2193
2194 if (big)
2195 pages = (void *)(&scsipi_sense.header.big + 1);
2196 else
2197 pages = (void *)(&scsipi_sense.header.small + 1);
2198
2199 oflags = pages->caching_params.flags;
2200
2201 if (bits & DKCACHE_READ)
2202 pages->caching_params.flags &= ~CACHING_RCD;
2203 else
2204 pages->caching_params.flags |= CACHING_RCD;
2205
2206 if (bits & DKCACHE_WRITE)
2207 pages->caching_params.flags |= CACHING_WCE;
2208 else
2209 pages->caching_params.flags &= ~CACHING_WCE;
2210
2211 if (oflags == pages->caching_params.flags)
2212 return (0);
2213
2214 pages->caching_params.pg_code &= PGCODE_MASK;
2215
2216 if (bits & DKCACHE_SAVE)
2217 byte2 |= SMS_SP;
2218
2219 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2220 sizeof(struct scsi_mode_page_header) +
2221 pages->caching_params.pg_length, 0, big));
2222 }
2223
2224 static void
2225 sd_set_properties(struct sd_softc *sd)
2226 {
2227 prop_dictionary_t disk_info, odisk_info, geom;
2228
2229 disk_info = prop_dictionary_create();
2230
2231 geom = prop_dictionary_create();
2232
2233 prop_dictionary_set_uint64(geom, "sectors-per-unit",
2234 sd->params.disksize);
2235
2236 prop_dictionary_set_uint32(geom, "sector-size",
2237 sd->params.blksize);
2238
2239 prop_dictionary_set_uint16(geom, "sectors-per-track",
2240 sd->params.sectors);
2241
2242 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
2243 sd->params.heads);
2244
2245 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
2246 sd->params.cyls);
2247
2248 prop_dictionary_set(disk_info, "geometry", geom);
2249 prop_object_release(geom);
2250
2251 prop_dictionary_set(device_properties(&sd->sc_dev),
2252 "disk-info", disk_info);
2253
2254 /*
2255 * Don't release disk_info here; we keep a reference to it.
2256 * disk_detach() will release it when we go away.
2257 */
2258
2259 odisk_info = sd->sc_dk.dk_info;
2260 sd->sc_dk.dk_info = disk_info;
2261 if (odisk_info)
2262 prop_object_release(odisk_info);
2263 }
2264