sd.c revision 1.298.2.2 1 /* $NetBSD: sd.c,v 1.298.2.2 2013/06/23 06:20:21 tls 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.298.2.2 2013/06/23 06:20:21 tls 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 /* Impose any restrictions inherited down the device tree */
975 xmax = sd->sc_dev->dv_maxphys;
976 if (bp->b_bcount > xmax)
977 bp->b_bcount = xmax;
978
979 /* Adapters could enforce their own limits on the
980 attached scsibuses via the device tree, but existing
981 drivers mostly do it in their own minphys routines. */
982 scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
983 }
984
985 static int
986 sdread(dev_t dev, struct uio *uio, int ioflag)
987 {
988
989 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
990 }
991
992 static int
993 sdwrite(dev_t dev, struct uio *uio, int ioflag)
994 {
995
996 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
997 }
998
999 /*
1000 * Perform special action on behalf of the user
1001 * Knows about the internals of this device
1002 */
1003 static int
1004 sdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1005 {
1006 struct sd_softc *sd = device_lookup_private(&sd_cd, SDUNIT(dev));
1007 struct scsipi_periph *periph = sd->sc_periph;
1008 int part = SDPART(dev);
1009 int error = 0;
1010 int s;
1011 #ifdef __HAVE_OLD_DISKLABEL
1012 struct disklabel *newlabel = NULL;
1013 #endif
1014
1015 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
1016
1017 /*
1018 * If the device is not valid, some IOCTLs can still be
1019 * handled on the raw partition. Check this here.
1020 */
1021 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1022 switch (cmd) {
1023 case DIOCKLABEL:
1024 case DIOCWLABEL:
1025 case DIOCLOCK:
1026 case DIOCEJECT:
1027 case ODIOCEJECT:
1028 case DIOCGCACHE:
1029 case DIOCSCACHE:
1030 case DIOCGSTRATEGY:
1031 case DIOCSSTRATEGY:
1032 case SCIOCIDENTIFY:
1033 case OSCIOCIDENTIFY:
1034 case SCIOCCOMMAND:
1035 case SCIOCDEBUG:
1036 if (part == RAW_PART)
1037 break;
1038 /* FALLTHROUGH */
1039 default:
1040 if ((periph->periph_flags & PERIPH_OPEN) == 0)
1041 return (ENODEV);
1042 else
1043 return (EIO);
1044 }
1045 }
1046
1047 error = disk_ioctl(&sd->sc_dk, cmd, addr, flag, l);
1048 if (error != EPASSTHROUGH)
1049 return (error);
1050
1051 switch (cmd) {
1052 case DIOCGDINFO:
1053 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
1054 return (0);
1055
1056 #ifdef __HAVE_OLD_DISKLABEL
1057 case ODIOCGDINFO:
1058 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1059 if (newlabel == NULL)
1060 return EIO;
1061 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
1062 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1063 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1064 else
1065 error = ENOTTY;
1066 free(newlabel, M_TEMP);
1067 return error;
1068 #endif
1069
1070 case DIOCGPART:
1071 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
1072 ((struct partinfo *)addr)->part =
1073 &sd->sc_dk.dk_label->d_partitions[part];
1074 return (0);
1075
1076 case DIOCWDINFO:
1077 case DIOCSDINFO:
1078 #ifdef __HAVE_OLD_DISKLABEL
1079 case ODIOCWDINFO:
1080 case ODIOCSDINFO:
1081 #endif
1082 {
1083 struct disklabel *lp;
1084
1085 if ((flag & FWRITE) == 0)
1086 return (EBADF);
1087
1088 #ifdef __HAVE_OLD_DISKLABEL
1089 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1090 newlabel = malloc(sizeof *newlabel, M_TEMP,
1091 M_WAITOK | M_ZERO);
1092 if (newlabel == NULL)
1093 return EIO;
1094 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1095 lp = newlabel;
1096 } else
1097 #endif
1098 lp = (struct disklabel *)addr;
1099
1100 mutex_enter(&sd->sc_dk.dk_openlock);
1101 sd->flags |= SDF_LABELLING;
1102
1103 error = setdisklabel(sd->sc_dk.dk_label,
1104 lp, /*sd->sc_dk.dk_openmask : */0,
1105 sd->sc_dk.dk_cpulabel);
1106 if (error == 0) {
1107 if (cmd == DIOCWDINFO
1108 #ifdef __HAVE_OLD_DISKLABEL
1109 || cmd == ODIOCWDINFO
1110 #endif
1111 )
1112 error = writedisklabel(SDLABELDEV(dev),
1113 sdstrategy, sd->sc_dk.dk_label,
1114 sd->sc_dk.dk_cpulabel);
1115 }
1116
1117 sd->flags &= ~SDF_LABELLING;
1118 mutex_exit(&sd->sc_dk.dk_openlock);
1119 #ifdef __HAVE_OLD_DISKLABEL
1120 if (newlabel != NULL)
1121 free(newlabel, M_TEMP);
1122 #endif
1123 return (error);
1124 }
1125
1126 case DIOCKLABEL:
1127 if (*(int *)addr)
1128 periph->periph_flags |= PERIPH_KEEP_LABEL;
1129 else
1130 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1131 return (0);
1132
1133 case DIOCWLABEL:
1134 if ((flag & FWRITE) == 0)
1135 return (EBADF);
1136 if (*(int *)addr)
1137 sd->flags |= SDF_WLABEL;
1138 else
1139 sd->flags &= ~SDF_WLABEL;
1140 return (0);
1141
1142 case DIOCLOCK:
1143 if (periph->periph_flags & PERIPH_REMOVABLE)
1144 return (scsipi_prevent(periph,
1145 (*(int *)addr) ?
1146 SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
1147 else
1148 return (ENOTTY);
1149
1150 case DIOCEJECT:
1151 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1152 return (ENOTTY);
1153 if (*(int *)addr == 0) {
1154 /*
1155 * Don't force eject: check that we are the only
1156 * partition open. If so, unlock it.
1157 */
1158 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1159 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1160 sd->sc_dk.dk_openmask) {
1161 error = scsipi_prevent(periph, SPAMR_ALLOW,
1162 XS_CTL_IGNORE_NOT_READY);
1163 if (error)
1164 return (error);
1165 } else {
1166 return (EBUSY);
1167 }
1168 }
1169 /* FALLTHROUGH */
1170 case ODIOCEJECT:
1171 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1172 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1173
1174 case DIOCGDEFLABEL:
1175 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1176 return (0);
1177
1178 #ifdef __HAVE_OLD_DISKLABEL
1179 case ODIOCGDEFLABEL:
1180 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1181 if (newlabel == NULL)
1182 return EIO;
1183 sdgetdefaultlabel(sd, newlabel);
1184 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1185 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1186 else
1187 error = ENOTTY;
1188 free(newlabel, M_TEMP);
1189 return error;
1190 #endif
1191
1192 case DIOCGCACHE:
1193 return (sd_getcache(sd, (int *) addr));
1194
1195 case DIOCSCACHE:
1196 if ((flag & FWRITE) == 0)
1197 return (EBADF);
1198 return (sd_setcache(sd, *(int *) addr));
1199
1200 case DIOCCACHESYNC:
1201 /*
1202 * XXX Do we really need to care about having a writable
1203 * file descriptor here?
1204 */
1205 if ((flag & FWRITE) == 0)
1206 return (EBADF);
1207 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
1208 error = sd_flush(sd, 0);
1209 if (error)
1210 sd->flags &= ~SDF_FLUSHING;
1211 else
1212 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1213 } else
1214 error = 0;
1215 return (error);
1216
1217 case DIOCAWEDGE:
1218 {
1219 struct dkwedge_info *dkw = (void *) addr;
1220
1221 if ((flag & FWRITE) == 0)
1222 return (EBADF);
1223
1224 /* If the ioctl happens here, the parent is us. */
1225 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev),
1226 sizeof(dkw->dkw_parent));
1227 return (dkwedge_add(dkw));
1228 }
1229
1230 case DIOCDWEDGE:
1231 {
1232 struct dkwedge_info *dkw = (void *) addr;
1233
1234 if ((flag & FWRITE) == 0)
1235 return (EBADF);
1236
1237 /* If the ioctl happens here, the parent is us. */
1238 strlcpy(dkw->dkw_parent, device_xname(sd->sc_dev),
1239 sizeof(dkw->dkw_parent));
1240 return (dkwedge_del(dkw));
1241 }
1242
1243 case DIOCLWEDGES:
1244 {
1245 struct dkwedge_list *dkwl = (void *) addr;
1246
1247 return (dkwedge_list(&sd->sc_dk, dkwl, l));
1248 }
1249
1250 case DIOCGSTRATEGY:
1251 {
1252 struct disk_strategy *dks = addr;
1253
1254 s = splbio();
1255 strlcpy(dks->dks_name, bufq_getstrategyname(sd->buf_queue),
1256 sizeof(dks->dks_name));
1257 splx(s);
1258 dks->dks_paramlen = 0;
1259
1260 return 0;
1261 }
1262
1263 case DIOCSSTRATEGY:
1264 {
1265 struct disk_strategy *dks = addr;
1266 struct bufq_state *new;
1267 struct bufq_state *old;
1268
1269 if ((flag & FWRITE) == 0) {
1270 return EBADF;
1271 }
1272
1273 if (dks->dks_param != NULL) {
1274 return EINVAL;
1275 }
1276 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1277 error = bufq_alloc(&new, dks->dks_name,
1278 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1279 if (error) {
1280 return error;
1281 }
1282 s = splbio();
1283 old = sd->buf_queue;
1284 bufq_move(new, old);
1285 sd->buf_queue = new;
1286 splx(s);
1287 bufq_free(old);
1288
1289 return 0;
1290 }
1291
1292 default:
1293 if (part != RAW_PART)
1294 return (ENOTTY);
1295 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, l));
1296 }
1297
1298 #ifdef DIAGNOSTIC
1299 panic("sdioctl: impossible");
1300 #endif
1301 }
1302
1303 static void
1304 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
1305 {
1306
1307 memset(lp, 0, sizeof(struct disklabel));
1308
1309 lp->d_secsize = sd->params.blksize;
1310 lp->d_ntracks = sd->params.heads;
1311 lp->d_nsectors = sd->params.sectors;
1312 lp->d_ncylinders = sd->params.cyls;
1313 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1314
1315 switch (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sd->sc_periph))) {
1316 case SCSIPI_BUSTYPE_SCSI:
1317 lp->d_type = DTYPE_SCSI;
1318 break;
1319 case SCSIPI_BUSTYPE_ATAPI:
1320 lp->d_type = DTYPE_ATAPI;
1321 break;
1322 }
1323 /*
1324 * XXX
1325 * We could probe the mode pages to figure out what kind of disc it is.
1326 * Is this worthwhile?
1327 */
1328 strncpy(lp->d_typename, sd->name, 16);
1329 strncpy(lp->d_packname, "fictitious", 16);
1330 if (sd->params.disksize > UINT32_MAX)
1331 lp->d_secperunit = UINT32_MAX;
1332 else
1333 lp->d_secperunit = sd->params.disksize;
1334 lp->d_rpm = sd->params.rot_rate;
1335 lp->d_interleave = 1;
1336 lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
1337 D_REMOVABLE : 0;
1338
1339 lp->d_partitions[RAW_PART].p_offset = 0;
1340 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1341 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1342 lp->d_npartitions = RAW_PART + 1;
1343
1344 lp->d_magic = DISKMAGIC;
1345 lp->d_magic2 = DISKMAGIC;
1346 lp->d_checksum = dkcksum(lp);
1347 }
1348
1349
1350 /*
1351 * Load the label information on the named device
1352 */
1353 static int
1354 sdgetdisklabel(struct sd_softc *sd)
1355 {
1356 struct disklabel *lp = sd->sc_dk.dk_label;
1357 const char *errstring;
1358
1359 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1360
1361 sdgetdefaultlabel(sd, lp);
1362
1363 if (lp->d_secpercyl == 0) {
1364 lp->d_secpercyl = 100;
1365 /* as long as it's not 0 - readdisklabel divides by it (?) */
1366 }
1367
1368 /*
1369 * Call the generic disklabel extraction routine
1370 */
1371 errstring = readdisklabel(MAKESDDEV(0, device_unit(sd->sc_dev),
1372 RAW_PART), sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1373 if (errstring) {
1374 aprint_error_dev(sd->sc_dev, "%s\n", errstring);
1375 return EIO;
1376 }
1377 return 0;
1378 }
1379
1380 static bool
1381 sd_shutdown(device_t self, int how)
1382 {
1383 struct sd_softc *sd = device_private(self);
1384
1385 /*
1386 * If the disk cache needs to be flushed, and the disk supports
1387 * it, flush it. We're cold at this point, so we poll for
1388 * completion.
1389 */
1390 if ((sd->flags & SDF_DIRTY) != 0) {
1391 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1392 aprint_error_dev(sd->sc_dev,
1393 "cache synchronization failed\n");
1394 sd->flags &= ~SDF_FLUSHING;
1395 } else
1396 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1397 }
1398 return true;
1399 }
1400
1401 static bool
1402 sd_suspend(device_t dv, const pmf_qual_t *qual)
1403 {
1404 return sd_shutdown(dv, boothowto); /* XXX no need to poll */
1405 }
1406
1407 /*
1408 * Check Errors
1409 */
1410 static int
1411 sd_interpret_sense(struct scsipi_xfer *xs)
1412 {
1413 struct scsipi_periph *periph = xs->xs_periph;
1414 struct scsi_sense_data *sense = &xs->sense.scsi_sense;
1415 struct sd_softc *sd = device_private(periph->periph_dev);
1416 int s, error, retval = EJUSTRETURN;
1417
1418 /*
1419 * If the periph is already recovering, just do the normal
1420 * error processing.
1421 */
1422 if (periph->periph_flags & PERIPH_RECOVERING)
1423 return (retval);
1424
1425 /*
1426 * Ignore errors from accessing illegal fields (e.g. trying to
1427 * lock the door of a digicam, which doesn't have a door that
1428 * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
1429 */
1430 if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
1431 SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
1432 sense->asc == 0x24 &&
1433 sense->ascq == 0x00) { /* Illegal field in CDB */
1434 if (!(xs->xs_control & XS_CTL_SILENT)) {
1435 scsipi_printaddr(periph);
1436 printf("no door lock\n");
1437 }
1438 xs->xs_control |= XS_CTL_IGNORE_ILLEGAL_REQUEST;
1439 return (retval);
1440 }
1441
1442
1443
1444 /*
1445 * If the device is not open yet, let the generic code handle it.
1446 */
1447 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1448 return (retval);
1449
1450 /*
1451 * If it isn't a extended or extended/deferred error, let
1452 * the generic code handle it.
1453 */
1454 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
1455 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
1456 return (retval);
1457
1458 if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
1459 sense->asc == 0x4) {
1460 if (sense->ascq == 0x01) {
1461 /*
1462 * Unit In The Process Of Becoming Ready.
1463 */
1464 printf("%s: waiting for pack to spin up...\n",
1465 device_xname(sd->sc_dev));
1466 if (!callout_pending(&periph->periph_callout))
1467 scsipi_periph_freeze(periph, 1);
1468 callout_reset(&periph->periph_callout,
1469 5 * hz, scsipi_periph_timed_thaw, periph);
1470 retval = ERESTART;
1471 } else if (sense->ascq == 0x02) {
1472 printf("%s: pack is stopped, restarting...\n",
1473 device_xname(sd->sc_dev));
1474 s = splbio();
1475 periph->periph_flags |= PERIPH_RECOVERING;
1476 splx(s);
1477 error = scsipi_start(periph, SSS_START,
1478 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1479 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1480 if (error) {
1481 aprint_error_dev(sd->sc_dev,
1482 "unable to restart pack\n");
1483 retval = error;
1484 } else
1485 retval = ERESTART;
1486 s = splbio();
1487 periph->periph_flags &= ~PERIPH_RECOVERING;
1488 splx(s);
1489 }
1490 }
1491 if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
1492 sense->asc == 0x31 &&
1493 sense->ascq == 0x00) { /* maybe for any asq ? */
1494 /* Medium Format Corrupted */
1495 retval = EFTYPE;
1496 }
1497 return (retval);
1498 }
1499
1500
1501 static int
1502 sdsize(dev_t dev)
1503 {
1504 struct sd_softc *sd;
1505 int part, unit, omask;
1506 int size;
1507
1508 unit = SDUNIT(dev);
1509 sd = device_lookup_private(&sd_cd, unit);
1510 if (sd == NULL)
1511 return (-1);
1512
1513 if (!device_is_active(sd->sc_dev))
1514 return (-1);
1515
1516 part = SDPART(dev);
1517 omask = sd->sc_dk.dk_openmask & (1 << part);
1518
1519 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1520 return (-1);
1521 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1522 size = -1;
1523 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1524 size = -1;
1525 else
1526 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1527 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1528 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1529 return (-1);
1530 return (size);
1531 }
1532
1533 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1534 static struct scsipi_xfer sx;
1535 static int sddoingadump;
1536
1537 /*
1538 * dump all of physical memory into the partition specified, starting
1539 * at offset 'dumplo' into the partition.
1540 */
1541 static int
1542 sddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1543 {
1544 struct sd_softc *sd; /* disk unit to do the I/O */
1545 struct disklabel *lp; /* disk's disklabel */
1546 int unit, part;
1547 int sectorsize; /* size of a disk sector */
1548 int nsects; /* number of sectors in partition */
1549 int sectoff; /* sector offset of partition */
1550 int totwrt; /* total number of sectors left to write */
1551 int nwrt; /* current number of sectors to write */
1552 struct scsipi_rw_10 cmd; /* write command */
1553 struct scsipi_xfer *xs; /* ... convenience */
1554 struct scsipi_periph *periph;
1555 struct scsipi_channel *chan;
1556
1557 /* Check if recursive dump; if so, punt. */
1558 if (sddoingadump)
1559 return (EFAULT);
1560
1561 /* Mark as active early. */
1562 sddoingadump = 1;
1563
1564 unit = SDUNIT(dev); /* Decompose unit & partition. */
1565 part = SDPART(dev);
1566
1567 /* Check for acceptable drive number. */
1568 sd = device_lookup_private(&sd_cd, unit);
1569 if (sd == NULL)
1570 return (ENXIO);
1571
1572 if (!device_is_active(sd->sc_dev))
1573 return (ENODEV);
1574
1575 periph = sd->sc_periph;
1576 chan = periph->periph_channel;
1577
1578 /* Make sure it was initialized. */
1579 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1580 return (ENXIO);
1581
1582 /* Convert to disk sectors. Request must be a multiple of size. */
1583 lp = sd->sc_dk.dk_label;
1584 sectorsize = lp->d_secsize;
1585 if ((size % sectorsize) != 0)
1586 return (EFAULT);
1587 totwrt = size / sectorsize;
1588 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1589
1590 nsects = lp->d_partitions[part].p_size;
1591 sectoff = lp->d_partitions[part].p_offset;
1592
1593 /* Check transfer bounds against partition size. */
1594 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1595 return (EINVAL);
1596
1597 /* Offset block number to start of partition. */
1598 blkno += sectoff;
1599
1600 xs = &sx;
1601
1602 while (totwrt > 0) {
1603 nwrt = totwrt; /* XXX */
1604 #ifndef SD_DUMP_NOT_TRUSTED
1605 /*
1606 * Fill out the scsi command
1607 */
1608 memset(&cmd, 0, sizeof(cmd));
1609 cmd.opcode = WRITE_10;
1610 _lto4b(blkno, cmd.addr);
1611 _lto2b(nwrt, cmd.length);
1612 /*
1613 * Fill out the scsipi_xfer structure
1614 * Note: we cannot sleep as we may be an interrupt
1615 * don't use scsipi_command() as it may want to wait
1616 * for an xs.
1617 */
1618 memset(xs, 0, sizeof(sx));
1619 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1620 XS_CTL_DATA_OUT;
1621 xs->xs_status = 0;
1622 xs->xs_periph = periph;
1623 xs->xs_retries = SDRETRIES;
1624 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1625 xs->cmd = (struct scsipi_generic *)&cmd;
1626 xs->cmdlen = sizeof(cmd);
1627 xs->resid = nwrt * sectorsize;
1628 xs->error = XS_NOERROR;
1629 xs->bp = 0;
1630 xs->data = va;
1631 xs->datalen = nwrt * sectorsize;
1632 callout_init(&xs->xs_callout, 0);
1633
1634 /*
1635 * Pass all this info to the scsi driver.
1636 */
1637 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1638 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1639 xs->error != XS_NOERROR)
1640 return (EIO);
1641 #else /* SD_DUMP_NOT_TRUSTED */
1642 /* Let's just talk about this first... */
1643 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1644 delay(500 * 1000); /* half a second */
1645 #endif /* SD_DUMP_NOT_TRUSTED */
1646
1647 /* update block count */
1648 totwrt -= nwrt;
1649 blkno += nwrt;
1650 va = (char *)va + sectorsize * nwrt;
1651 }
1652 sddoingadump = 0;
1653 return (0);
1654 }
1655
1656 static int
1657 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1658 int page, int flags, int *big)
1659 {
1660
1661 if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
1662 !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
1663 *big = 1;
1664 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
1665 size + sizeof(struct scsi_mode_parameter_header_10),
1666 flags, SDRETRIES, 6000);
1667 } else {
1668 *big = 0;
1669 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
1670 size + sizeof(struct scsi_mode_parameter_header_6),
1671 flags, SDRETRIES, 6000);
1672 }
1673 }
1674
1675 static int
1676 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
1677 int flags, int big)
1678 {
1679
1680 if (big) {
1681 struct scsi_mode_parameter_header_10 *header = sense;
1682
1683 _lto2b(0, header->data_length);
1684 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
1685 size + sizeof(struct scsi_mode_parameter_header_10),
1686 flags, SDRETRIES, 6000);
1687 } else {
1688 struct scsi_mode_parameter_header_6 *header = sense;
1689
1690 header->data_length = 0;
1691 return scsipi_mode_select(sd->sc_periph, byte2, sense,
1692 size + sizeof(struct scsi_mode_parameter_header_6),
1693 flags, SDRETRIES, 6000);
1694 }
1695 }
1696
1697 /*
1698 * sd_validate_blksize:
1699 *
1700 * Validate the block size. Print error if periph is specified,
1701 */
1702 static int
1703 sd_validate_blksize(struct scsipi_periph *periph, int len)
1704 {
1705
1706 switch (len) {
1707 case 256:
1708 case 512:
1709 case 1024:
1710 case 2048:
1711 case 4096:
1712 return 1;
1713 }
1714
1715 if (periph) {
1716 scsipi_printaddr(periph);
1717 printf("%s sector size: 0x%x. Defaulting to %d bytes.\n",
1718 (len ^ (1 << (ffs(len) - 1))) ?
1719 "preposterous" : "unsupported",
1720 len, SD_DEFAULT_BLKSIZE);
1721 }
1722
1723 return 0;
1724 }
1725
1726 /*
1727 * sd_read_capacity:
1728 *
1729 * Find out from the device what its capacity is.
1730 */
1731 static u_int64_t
1732 sd_read_capacity(struct scsipi_periph *periph, int *blksize, int flags)
1733 {
1734 union {
1735 struct scsipi_read_capacity_10 cmd;
1736 struct scsipi_read_capacity_16 cmd16;
1737 } cmd;
1738 union {
1739 struct scsipi_read_capacity_10_data data;
1740 struct scsipi_read_capacity_16_data data16;
1741 } *datap;
1742 uint64_t rv;
1743
1744 memset(&cmd, 0, sizeof(cmd));
1745 cmd.cmd.opcode = READ_CAPACITY_10;
1746
1747 /*
1748 * Don't allocate data buffer on stack;
1749 * The lower driver layer might use the same stack and
1750 * if it uses region which is in the same cacheline,
1751 * cache flush ops against the data buffer won't work properly.
1752 */
1753 datap = malloc(sizeof(*datap), M_TEMP, M_WAITOK);
1754 if (datap == NULL)
1755 return 0;
1756
1757 /*
1758 * If the command works, interpret the result as a 4 byte
1759 * number of blocks
1760 */
1761 rv = 0;
1762 memset(datap, 0, sizeof(datap->data));
1763 if (scsipi_command(periph, (void *)&cmd.cmd, sizeof(cmd.cmd),
1764 (void *)datap, sizeof(datap->data), SCSIPIRETRIES, 20000, NULL,
1765 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1766 goto out;
1767
1768 if (_4btol(datap->data.addr) != 0xffffffff) {
1769 *blksize = _4btol(datap->data.length);
1770 rv = _4btol(datap->data.addr) + 1;
1771 goto out;
1772 }
1773
1774 /*
1775 * Device is larger than can be reflected by READ CAPACITY (10).
1776 * Try READ CAPACITY (16).
1777 */
1778
1779 memset(&cmd, 0, sizeof(cmd));
1780 cmd.cmd16.opcode = READ_CAPACITY_16;
1781 cmd.cmd16.byte2 = SRC16_SERVICE_ACTION;
1782 _lto4b(sizeof(datap->data16), cmd.cmd16.len);
1783
1784 memset(datap, 0, sizeof(datap->data16));
1785 if (scsipi_command(periph, (void *)&cmd.cmd16, sizeof(cmd.cmd16),
1786 (void *)datap, sizeof(datap->data16), SCSIPIRETRIES, 20000, NULL,
1787 flags | XS_CTL_DATA_IN | XS_CTL_SILENT) != 0)
1788 goto out;
1789
1790 *blksize = _4btol(datap->data16.length);
1791 rv = _8btol(datap->data16.addr) + 1;
1792
1793 out:
1794 free(datap, M_TEMP);
1795 return rv;
1796 }
1797
1798 static int
1799 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
1800 {
1801 struct {
1802 struct scsi_mode_parameter_header_6 header;
1803 /* no block descriptor */
1804 u_int8_t pg_code; /* page code (should be 6) */
1805 u_int8_t pg_length; /* page length (should be 11) */
1806 u_int8_t wcd; /* bit0: cache disable */
1807 u_int8_t lbs[2]; /* logical block size */
1808 u_int8_t size[5]; /* number of log. blocks */
1809 u_int8_t pp; /* power/performance */
1810 u_int8_t flags;
1811 u_int8_t resvd;
1812 } scsipi_sense;
1813 u_int64_t blocks;
1814 int error, blksize;
1815
1816 /*
1817 * sd_read_capacity (ie "read capacity") and mode sense page 6
1818 * give the same information. Do both for now, and check
1819 * for consistency.
1820 * XXX probably differs for removable media
1821 */
1822 dp->blksize = SD_DEFAULT_BLKSIZE;
1823 if ((blocks = sd_read_capacity(sd->sc_periph, &blksize, flags)) == 0)
1824 return (SDGP_RESULT_OFFLINE); /* XXX? */
1825
1826 error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
1827 &scsipi_sense.header, sizeof(scsipi_sense),
1828 flags, SDRETRIES, 6000);
1829
1830 if (error != 0)
1831 return (SDGP_RESULT_OFFLINE); /* XXX? */
1832
1833 dp->blksize = blksize;
1834 if (!sd_validate_blksize(NULL, dp->blksize))
1835 dp->blksize = _2btol(scsipi_sense.lbs);
1836 if (!sd_validate_blksize(sd->sc_periph, dp->blksize))
1837 dp->blksize = SD_DEFAULT_BLKSIZE;
1838
1839 /*
1840 * Create a pseudo-geometry.
1841 */
1842 dp->heads = 64;
1843 dp->sectors = 32;
1844 dp->cyls = blocks / (dp->heads * dp->sectors);
1845 dp->disksize = _5btol(scsipi_sense.size);
1846 if (dp->disksize <= UINT32_MAX && dp->disksize != blocks) {
1847 printf("RBC size: mode sense=%llu, get cap=%llu\n",
1848 (unsigned long long)dp->disksize,
1849 (unsigned long long)blocks);
1850 dp->disksize = blocks;
1851 }
1852 dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
1853
1854 return (SDGP_RESULT_OK);
1855 }
1856
1857 /*
1858 * Get the scsi driver to send a full inquiry to the * device and use the
1859 * results to fill out the disk parameter structure.
1860 */
1861 static int
1862 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
1863 {
1864 u_int64_t blocks;
1865 int error, blksize;
1866 #if 0
1867 int i;
1868 u_int8_t *p;
1869 #endif
1870
1871 dp->disksize = blocks = sd_read_capacity(sd->sc_periph, &blksize,
1872 flags);
1873 if (blocks == 0) {
1874 struct scsipi_read_format_capacities cmd;
1875 struct {
1876 struct scsipi_capacity_list_header header;
1877 struct scsipi_capacity_descriptor desc;
1878 } __packed data;
1879
1880 memset(&cmd, 0, sizeof(cmd));
1881 memset(&data, 0, sizeof(data));
1882 cmd.opcode = READ_FORMAT_CAPACITIES;
1883 _lto2b(sizeof(data), cmd.length);
1884
1885 error = scsipi_command(sd->sc_periph,
1886 (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
1887 SDRETRIES, 20000, NULL,
1888 flags | XS_CTL_DATA_IN);
1889 if (error == EFTYPE) {
1890 /* Medium Format Corrupted, handle as not formatted */
1891 return (SDGP_RESULT_UNFORMATTED);
1892 }
1893 if (error || data.header.length == 0)
1894 return (SDGP_RESULT_OFFLINE);
1895
1896 #if 0
1897 printf("rfc: length=%d\n", data.header.length);
1898 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");
1899 #endif
1900 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
1901 case SCSIPI_CAP_DESC_CODE_RESERVED:
1902 case SCSIPI_CAP_DESC_CODE_FORMATTED:
1903 break;
1904
1905 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
1906 return (SDGP_RESULT_UNFORMATTED);
1907
1908 case SCSIPI_CAP_DESC_CODE_NONE:
1909 return (SDGP_RESULT_OFFLINE);
1910 }
1911
1912 dp->disksize = blocks = _4btol(data.desc.nblks);
1913 if (blocks == 0)
1914 return (SDGP_RESULT_OFFLINE); /* XXX? */
1915
1916 blksize = _3btol(data.desc.blklen);
1917
1918 } else if (!sd_validate_blksize(NULL, blksize)) {
1919 struct sd_mode_sense_data scsipi_sense;
1920 int big, bsize;
1921 struct scsi_general_block_descriptor *bdesc;
1922
1923 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1924 error = sd_mode_sense(sd, 0, &scsipi_sense,
1925 sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
1926 if (!error) {
1927 if (big) {
1928 bdesc = (void *)(&scsipi_sense.header.big + 1);
1929 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
1930 } else {
1931 bdesc = (void *)(&scsipi_sense.header.small + 1);
1932 bsize = scsipi_sense.header.small.blk_desc_len;
1933 }
1934
1935 #if 0
1936 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
1937 printf("page 0 bsize=%d\n", bsize);
1938 printf("page 0 ok\n");
1939 #endif
1940
1941 if (bsize >= 8) {
1942 blksize = _3btol(bdesc->blklen);
1943 }
1944 }
1945 }
1946
1947 if (!sd_validate_blksize(sd->sc_periph, blksize))
1948 blksize = SD_DEFAULT_BLKSIZE;
1949
1950 dp->blksize = blksize;
1951 dp->disksize512 = (blocks * dp->blksize) / DEV_BSIZE;
1952 return (0);
1953 }
1954
1955 static int
1956 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
1957 {
1958 struct sd_mode_sense_data scsipi_sense;
1959 int error;
1960 int big, byte2;
1961 size_t poffset;
1962 union scsi_disk_pages *pages;
1963
1964 byte2 = SMS_DBD;
1965 again:
1966 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
1967 error = sd_mode_sense(sd, byte2, &scsipi_sense,
1968 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
1969 sizeof(scsipi_sense.pages.rigid_geometry), 4,
1970 flags | XS_CTL_SILENT, &big);
1971 if (error) {
1972 if (byte2 == SMS_DBD) {
1973 /* No result; try once more with DBD off */
1974 byte2 = 0;
1975 goto again;
1976 }
1977 return (error);
1978 }
1979
1980 if (big) {
1981 poffset = sizeof scsipi_sense.header.big;
1982 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
1983 } else {
1984 poffset = sizeof scsipi_sense.header.small;
1985 poffset += scsipi_sense.header.small.blk_desc_len;
1986 }
1987
1988 if (poffset > sizeof(scsipi_sense) - sizeof(pages->rigid_geometry))
1989 return ERESTART;
1990
1991 pages = (void *)((u_long)&scsipi_sense + poffset);
1992 #if 0
1993 {
1994 size_t i;
1995 u_int8_t *p;
1996
1997 printf("page 4 sense:");
1998 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
1999 i--, p++)
2000 printf(" %02x", *p);
2001 printf("\n");
2002 printf("page 4 pg_code=%d sense=%p/%p\n",
2003 pages->rigid_geometry.pg_code, &scsipi_sense, pages);
2004 }
2005 #endif
2006
2007 if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
2008 return (ERESTART);
2009
2010 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
2011 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
2012 _3btol(pages->rigid_geometry.ncyl),
2013 pages->rigid_geometry.nheads,
2014 _2btol(pages->rigid_geometry.st_cyl_wp),
2015 _2btol(pages->rigid_geometry.st_cyl_rwc),
2016 _2btol(pages->rigid_geometry.land_zone)));
2017
2018 /*
2019 * KLUDGE!! (for zone recorded disks)
2020 * give a number of sectors so that sec * trks * cyls
2021 * is <= disk_size
2022 * can lead to wasted space! THINK ABOUT THIS !
2023 */
2024 dp->heads = pages->rigid_geometry.nheads;
2025 dp->cyls = _3btol(pages->rigid_geometry.ncyl);
2026 if (dp->heads == 0 || dp->cyls == 0)
2027 return (ERESTART);
2028 dp->sectors = dp->disksize / (dp->heads * dp->cyls); /* XXX */
2029
2030 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
2031 if (dp->rot_rate == 0)
2032 dp->rot_rate = 3600;
2033
2034 #if 0
2035 printf("page 4 ok\n");
2036 #endif
2037 return (0);
2038 }
2039
2040 static int
2041 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
2042 {
2043 struct sd_mode_sense_data scsipi_sense;
2044 int error;
2045 int big, byte2;
2046 size_t poffset;
2047 union scsi_disk_pages *pages;
2048
2049 byte2 = SMS_DBD;
2050 again:
2051 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2052 error = sd_mode_sense(sd, 0, &scsipi_sense,
2053 (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
2054 sizeof(scsipi_sense.pages.flex_geometry), 5,
2055 flags | XS_CTL_SILENT, &big);
2056 if (error) {
2057 if (byte2 == SMS_DBD) {
2058 /* No result; try once more with DBD off */
2059 byte2 = 0;
2060 goto again;
2061 }
2062 return (error);
2063 }
2064
2065 if (big) {
2066 poffset = sizeof scsipi_sense.header.big;
2067 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
2068 } else {
2069 poffset = sizeof scsipi_sense.header.small;
2070 poffset += scsipi_sense.header.small.blk_desc_len;
2071 }
2072
2073 if (poffset > sizeof(scsipi_sense) - sizeof(pages->flex_geometry))
2074 return ERESTART;
2075
2076 pages = (void *)((u_long)&scsipi_sense + poffset);
2077 #if 0
2078 {
2079 size_t i;
2080 u_int8_t *p;
2081
2082 printf("page 5 sense:");
2083 for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i;
2084 i--, p++)
2085 printf(" %02x", *p);
2086 printf("\n");
2087 printf("page 5 pg_code=%d sense=%p/%p\n",
2088 pages->flex_geometry.pg_code, &scsipi_sense, pages);
2089 }
2090 #endif
2091
2092 if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
2093 return (ERESTART);
2094
2095 SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
2096 ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
2097 _3btol(pages->flex_geometry.ncyl),
2098 pages->flex_geometry.nheads,
2099 pages->flex_geometry.ph_sec_tr,
2100 _2btol(pages->flex_geometry.bytes_s)));
2101
2102 dp->heads = pages->flex_geometry.nheads;
2103 dp->cyls = _2btol(pages->flex_geometry.ncyl);
2104 dp->sectors = pages->flex_geometry.ph_sec_tr;
2105 if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
2106 return (ERESTART);
2107
2108 dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
2109 if (dp->rot_rate == 0)
2110 dp->rot_rate = 3600;
2111
2112 #if 0
2113 printf("page 5 ok\n");
2114 #endif
2115 return (0);
2116 }
2117
2118 static int
2119 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
2120 {
2121 int error;
2122
2123 /*
2124 * If offline, the SDEV_MEDIA_LOADED flag will be
2125 * cleared by the caller if necessary.
2126 */
2127 if (sd->type == T_SIMPLE_DIRECT) {
2128 error = sd_get_simplifiedparms(sd, dp, flags);
2129 if (!error)
2130 disk_blocksize(&sd->sc_dk, dp->blksize);
2131 return (error);
2132 }
2133
2134 error = sd_get_capacity(sd, dp, flags);
2135 if (error)
2136 return (error);
2137
2138 disk_blocksize(&sd->sc_dk, dp->blksize);
2139
2140 if (sd->type == T_OPTICAL)
2141 goto page0;
2142
2143 if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
2144 if (!sd_get_parms_page5(sd, dp, flags) ||
2145 !sd_get_parms_page4(sd, dp, flags))
2146 goto setprops;
2147 } else {
2148 if (!sd_get_parms_page4(sd, dp, flags) ||
2149 !sd_get_parms_page5(sd, dp, flags))
2150 goto setprops;
2151 }
2152
2153 page0:
2154 printf("%s: fabricating a geometry\n", device_xname(sd->sc_dev));
2155 /* Try calling driver's method for figuring out geometry. */
2156 if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
2157 !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
2158 (sd->sc_periph, dp, dp->disksize)) {
2159 /*
2160 * Use adaptec standard fictitious geometry
2161 * this depends on which controller (e.g. 1542C is
2162 * different. but we have to put SOMETHING here..)
2163 */
2164 dp->heads = 64;
2165 dp->sectors = 32;
2166 dp->cyls = dp->disksize / (64 * 32);
2167 }
2168 dp->rot_rate = 3600;
2169
2170 setprops:
2171 sd_set_geometry(sd);
2172
2173 return (SDGP_RESULT_OK);
2174 }
2175
2176 static int
2177 sd_flush(struct sd_softc *sd, int flags)
2178 {
2179 struct scsipi_periph *periph = sd->sc_periph;
2180 struct scsi_synchronize_cache_10 cmd;
2181
2182 /*
2183 * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
2184 * We issue with address 0 length 0, which should be
2185 * interpreted by the device as "all remaining blocks
2186 * starting at address 0". We ignore ILLEGAL REQUEST
2187 * in the event that the command is not supported by
2188 * the device, and poll for completion so that we know
2189 * that the cache has actually been flushed.
2190 *
2191 * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
2192 * command, as indicated by our quirks flags.
2193 *
2194 * XXX What about older devices?
2195 */
2196 if (periph->periph_version < 2 ||
2197 (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
2198 return (0);
2199
2200 sd->flags |= SDF_FLUSHING;
2201 memset(&cmd, 0, sizeof(cmd));
2202 cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
2203
2204 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
2205 SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
2206 }
2207
2208 static int
2209 sd_getcache(struct sd_softc *sd, int *bitsp)
2210 {
2211 struct scsipi_periph *periph = sd->sc_periph;
2212 struct sd_mode_sense_data scsipi_sense;
2213 int error, bits = 0;
2214 int big;
2215 union scsi_disk_pages *pages;
2216
2217 if (periph->periph_version < 2)
2218 return (EOPNOTSUPP);
2219
2220 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2221 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2222 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2223 if (error)
2224 return (error);
2225
2226 if (big)
2227 pages = (void *)(&scsipi_sense.header.big + 1);
2228 else
2229 pages = (void *)(&scsipi_sense.header.small + 1);
2230
2231 if ((pages->caching_params.flags & CACHING_RCD) == 0)
2232 bits |= DKCACHE_READ;
2233 if (pages->caching_params.flags & CACHING_WCE)
2234 bits |= DKCACHE_WRITE;
2235 if (pages->caching_params.pg_code & PGCODE_PS)
2236 bits |= DKCACHE_SAVE;
2237
2238 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2239 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2240 sizeof(scsipi_sense.pages.caching_params),
2241 SMS_PCTRL_CHANGEABLE|8, 0, &big);
2242 if (error == 0) {
2243 if (big)
2244 pages = (void *)(&scsipi_sense.header.big + 1);
2245 else
2246 pages = (void *)(&scsipi_sense.header.small + 1);
2247
2248 if (pages->caching_params.flags & CACHING_RCD)
2249 bits |= DKCACHE_RCHANGE;
2250 if (pages->caching_params.flags & CACHING_WCE)
2251 bits |= DKCACHE_WCHANGE;
2252 }
2253
2254 *bitsp = bits;
2255
2256 return (0);
2257 }
2258
2259 static int
2260 sd_setcache(struct sd_softc *sd, int bits)
2261 {
2262 struct scsipi_periph *periph = sd->sc_periph;
2263 struct sd_mode_sense_data scsipi_sense;
2264 int error;
2265 uint8_t oflags, byte2 = 0;
2266 int big;
2267 union scsi_disk_pages *pages;
2268
2269 if (periph->periph_version < 2)
2270 return (EOPNOTSUPP);
2271
2272 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
2273 error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
2274 sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
2275 if (error)
2276 return (error);
2277
2278 if (big)
2279 pages = (void *)(&scsipi_sense.header.big + 1);
2280 else
2281 pages = (void *)(&scsipi_sense.header.small + 1);
2282
2283 oflags = pages->caching_params.flags;
2284
2285 if (bits & DKCACHE_READ)
2286 pages->caching_params.flags &= ~CACHING_RCD;
2287 else
2288 pages->caching_params.flags |= CACHING_RCD;
2289
2290 if (bits & DKCACHE_WRITE)
2291 pages->caching_params.flags |= CACHING_WCE;
2292 else
2293 pages->caching_params.flags &= ~CACHING_WCE;
2294
2295 if (oflags == pages->caching_params.flags)
2296 return (0);
2297
2298 pages->caching_params.pg_code &= PGCODE_MASK;
2299
2300 if (bits & DKCACHE_SAVE)
2301 byte2 |= SMS_SP;
2302
2303 return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
2304 sizeof(struct scsi_mode_page_header) +
2305 pages->caching_params.pg_length, 0, big));
2306 }
2307
2308 static void
2309 sd_set_geometry(struct sd_softc *sd)
2310 {
2311 struct disk_geom *dg = &sd->sc_dk.dk_geom;
2312
2313 memset(dg, 0, sizeof(*dg));
2314
2315 dg->dg_secperunit = sd->params.disksize;
2316 dg->dg_secsize = sd->params.blksize;
2317 dg->dg_nsectors = sd->params.sectors;
2318 dg->dg_ntracks = sd->params.heads;
2319 dg->dg_ncylinders = sd->params.cyls;
2320
2321 disk_set_info(sd->sc_dev, &sd->sc_dk, NULL);
2322 }
2323