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