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