sd.c revision 1.147 1 /* $NetBSD: sd.c,v 1.147 1999/09/11 21:42:58 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (at) dialix.oz.au)
41 * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 *
43 * TRW Financial Systems, in accordance with their agreement with Carnegie
44 * Mellon University, makes this software available to CMU to distribute
45 * or use in any manner that they see fit as long as this message is kept with
46 * the software. For this reason TFS also grants any other persons or
47 * organisations permission to use or modify this software.
48 *
49 * TFS supplies this software to be publicly redistributed
50 * on the understanding that TFS is not responsible for the correct
51 * functioning of this software in any circumstances.
52 *
53 * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
54 */
55
56 #include "opt_scsi.h"
57 #include "rnd.h"
58
59 #include <sys/types.h>
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/file.h>
64 #include <sys/stat.h>
65 #include <sys/ioctl.h>
66 #include <sys/scsiio.h>
67 #include <sys/buf.h>
68 #include <sys/uio.h>
69 #include <sys/malloc.h>
70 #include <sys/errno.h>
71 #include <sys/device.h>
72 #include <sys/disklabel.h>
73 #include <sys/disk.h>
74 #include <sys/proc.h>
75 #include <sys/conf.h>
76 #include <sys/vnode.h>
77 #if NRND > 0
78 #include <sys/rnd.h>
79 #endif
80
81 #include <dev/scsipi/scsipi_all.h>
82 #include <dev/scsipi/scsi_all.h>
83 #include <dev/scsipi/scsipi_disk.h>
84 #include <dev/scsipi/scsi_disk.h>
85 #include <dev/scsipi/scsiconf.h>
86 #include <dev/scsipi/sdvar.h>
87
88 #include "sd.h" /* NSD_SCSIBUS and NSD_ATAPIBUS come from here */
89
90 #ifndef SDOUTSTANDING
91 #define SDOUTSTANDING 4
92 #endif
93
94 #define SDUNIT(dev) DISKUNIT(dev)
95 #define SDPART(dev) DISKPART(dev)
96 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
97
98 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
99
100 int sdlock __P((struct sd_softc *));
101 void sdunlock __P((struct sd_softc *));
102 void sdminphys __P((struct buf *));
103 void sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *));
104 void sdgetdisklabel __P((struct sd_softc *));
105 void sdstart __P((void *));
106 void sddone __P((struct scsipi_xfer *));
107 void sd_shutdown __P((void *));
108 int sd_reassign_blocks __P((struct sd_softc *, u_long));
109 int sd_interpret_sense __P((struct scsipi_xfer *));
110
111 extern struct cfdriver sd_cd;
112
113 struct dkdriver sddkdriver = { sdstrategy };
114
115 struct scsipi_device sd_switch = {
116 sd_interpret_sense, /* check our error handler first */
117 sdstart, /* have a queue, served by this */
118 NULL, /* have no async handler */
119 sddone, /* deal with stats at interrupt time */
120 };
121
122 /*
123 * The routine called by the low level scsi routine when it discovers
124 * a device suitable for this driver.
125 */
126 void
127 sdattach(parent, sd, sc_link, ops)
128 struct device *parent;
129 struct sd_softc *sd;
130 struct scsipi_link *sc_link;
131 const struct sd_ops *ops;
132 {
133 int error, result;
134 struct disk_parms *dp = &sd->params;
135 char pbuf[9];
136
137 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
138
139 /*
140 * Store information needed to contact our base driver
141 */
142 sd->sc_link = sc_link;
143 sd->sc_ops = ops;
144 sc_link->device = &sd_switch;
145 sc_link->device_softc = sd;
146 if (sc_link->openings > SDOUTSTANDING)
147 sc_link->openings = SDOUTSTANDING;
148
149 /*
150 * Initialize and attach the disk structure.
151 */
152 sd->sc_dk.dk_driver = &sddkdriver;
153 sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
154 disk_attach(&sd->sc_dk);
155
156 #if !defined(i386)
157 dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */
158 #endif
159
160 /*
161 * Use the subdriver to request information regarding
162 * the drive. We cannot use interrupts yet, so the
163 * request must specify this.
164 */
165 printf("\n");
166
167 error = scsipi_start(sd->sc_link, SSS_START,
168 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST |
169 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
170
171 if (error)
172 result = SDGP_RESULT_OFFLINE;
173 else
174 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
175 SCSI_AUTOCONF);
176 printf("%s: ", sd->sc_dev.dv_xname);
177 switch (result) {
178 case SDGP_RESULT_OK:
179 format_bytes(pbuf, sizeof(pbuf),
180 (u_int64_t)dp->disksize * dp->blksize);
181 printf(
182 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %ld sectors",
183 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
184 dp->disksize);
185 break;
186
187 case SDGP_RESULT_OFFLINE:
188 printf("drive offline");
189 break;
190
191 case SDGP_RESULT_UNFORMATTED:
192 printf("unformatted media");
193 break;
194
195 #ifdef DIAGNOSTIC
196 default:
197 panic("sdattach: unknown result from get_parms");
198 break;
199 #endif
200 }
201 printf("\n");
202
203 /*
204 * Establish a shutdown hook so that we can ensure that
205 * our data has actually made it onto the platter at
206 * shutdown time. Note that this relies on the fact
207 * that the shutdown hook code puts us at the head of
208 * the list (thus guaranteeing that our hook runs before
209 * our ancestors').
210 */
211 if ((sd->sc_sdhook =
212 shutdownhook_establish(sd_shutdown, sd)) == NULL)
213 printf("%s: WARNING: unable to establish shutdown hook\n",
214 sd->sc_dev.dv_xname);
215
216 #if NRND > 0
217 /*
218 * attach the device into the random source list
219 */
220 rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
221 RND_TYPE_DISK, 0);
222 #endif
223 }
224
225 int
226 sdactivate(self, act)
227 struct device *self;
228 enum devact act;
229 {
230 int rv = 0;
231
232 switch (act) {
233 case DVACT_ACTIVATE:
234 rv = EOPNOTSUPP;
235 break;
236
237 case DVACT_DEACTIVATE:
238 /*
239 * Nothing to do; we key off the device's DVF_ACTIVE.
240 */
241 break;
242 }
243 return (rv);
244 }
245
246 int
247 sddetach(self, flags)
248 struct device *self;
249 int flags;
250 {
251 struct sd_softc *sd = (struct sd_softc *) self;
252 struct buf *bp;
253 int s, bmaj, cmaj, mn;
254
255 /* locate the major number */
256 for (bmaj = 0; bmaj <= nblkdev; bmaj++)
257 if (bdevsw[bmaj].d_open == sdopen)
258 break;
259 for (cmaj = 0; cmaj <= nchrdev; cmaj++)
260 if (cdevsw[cmaj].d_open == sdopen)
261 break;
262
263 s = splbio();
264
265 /* Kill off any queued buffers. */
266 while ((bp = sd->buf_queue.b_actf) != NULL) {
267 sd->buf_queue.b_actf = bp->b_actf;
268 bp->b_error = EIO;
269 bp->b_flags |= B_ERROR;
270 bp->b_resid = bp->b_bcount;
271 biodone(bp);
272 }
273
274 /* Kill off any pending commands. */
275 scsipi_kill_pending(sd->sc_link);
276
277 splx(s);
278
279 /* Nuke the the vnodes for any open instances */
280 mn = self->dv_unit;
281 vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
282 vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
283
284 /* Detach from the disk list. */
285 disk_detach(&sd->sc_dk);
286
287 /* Get rid of the shutdown hook. */
288 shutdownhook_disestablish(sd->sc_sdhook);
289
290 #if NRND > 0
291 /* Unhook the entropy source. */
292 rnd_detach_source(&sd->rnd_source);
293 #endif
294
295 return (0);
296 }
297
298 /*
299 * Wait interruptibly for an exclusive lock.
300 *
301 * XXX
302 * Several drivers do this; it should be abstracted and made MP-safe.
303 */
304 int
305 sdlock(sd)
306 struct sd_softc *sd;
307 {
308 int error;
309
310 while ((sd->flags & SDF_LOCKED) != 0) {
311 sd->flags |= SDF_WANTED;
312 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
313 return (error);
314 }
315 sd->flags |= SDF_LOCKED;
316 return (0);
317 }
318
319 /*
320 * Unlock and wake up any waiters.
321 */
322 void
323 sdunlock(sd)
324 struct sd_softc *sd;
325 {
326
327 sd->flags &= ~SDF_LOCKED;
328 if ((sd->flags & SDF_WANTED) != 0) {
329 sd->flags &= ~SDF_WANTED;
330 wakeup(sd);
331 }
332 }
333
334 /*
335 * open the device. Make sure the partition info is a up-to-date as can be.
336 */
337 int
338 sdopen(dev, flag, fmt, p)
339 dev_t dev;
340 int flag, fmt;
341 struct proc *p;
342 {
343 struct sd_softc *sd;
344 struct scsipi_link *sc_link;
345 int unit, part;
346 int error;
347
348 unit = SDUNIT(dev);
349 if (unit >= sd_cd.cd_ndevs)
350 return (ENXIO);
351 sd = sd_cd.cd_devs[unit];
352 if (sd == NULL)
353 return (ENXIO);
354
355 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
356 return (ENODEV);
357
358 sc_link = sd->sc_link;
359 part = SDPART(dev);
360
361 SC_DEBUG(sc_link, SDEV_DB1,
362 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
363 sd_cd.cd_ndevs, part));
364
365 /*
366 * If this is the first open of this device, add a reference
367 * to the adapter.
368 */
369 if (sd->sc_dk.dk_openmask == 0 &&
370 (error = scsipi_adapter_addref(sc_link)) != 0)
371 return (error);
372
373 if ((error = sdlock(sd)) != 0)
374 goto bad4;
375
376 if ((sc_link->flags & SDEV_OPEN) != 0) {
377 /*
378 * If any partition is open, but the disk has been invalidated,
379 * disallow further opens of non-raw partition
380 */
381 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0 &&
382 (part != RAW_PART || fmt != S_IFCHR)) {
383 error = EIO;
384 goto bad3;
385 }
386 } else {
387 /* Check that it is still responding and ok. */
388 error = scsipi_test_unit_ready(sc_link,
389 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
390 SCSI_IGNORE_NOT_READY);
391 if (error)
392 goto bad3;
393
394 /*
395 * Start the pack spinning if necessary. Always allow the
396 * raw parition to be opened, for raw IOCTLs. Data transfers
397 * will check for SDEV_MEDIA_LOADED.
398 */
399 error = scsipi_start(sc_link, SSS_START,
400 SCSI_IGNORE_ILLEGAL_REQUEST |
401 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
402 if (error) {
403 if (part != RAW_PART || fmt != S_IFCHR)
404 goto bad3;
405 else
406 goto out;
407 }
408
409 sc_link->flags |= SDEV_OPEN;
410
411 /* Lock the pack in. */
412 error = scsipi_prevent(sc_link, PR_PREVENT,
413 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
414 if (error)
415 goto bad;
416
417 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
418 sc_link->flags |= SDEV_MEDIA_LOADED;
419
420 /*
421 * Load the physical device parameters.
422 *
423 * Note that if media is present but unformatted,
424 * we allow the open (so that it can be formatted!).
425 * The drive should refuse real I/O, if the media is
426 * unformatted.
427 */
428 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
429 0) == SDGP_RESULT_OFFLINE) {
430 error = ENXIO;
431 goto bad2;
432 }
433 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
434
435 /* Load the partition info if not already loaded. */
436 sdgetdisklabel(sd);
437 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
438 }
439 }
440
441 /* Check that the partition exists. */
442 if (part != RAW_PART &&
443 (part >= sd->sc_dk.dk_label->d_npartitions ||
444 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
445 error = ENXIO;
446 goto bad;
447 }
448
449 out: /* Insure only one open at a time. */
450 switch (fmt) {
451 case S_IFCHR:
452 sd->sc_dk.dk_copenmask |= (1 << part);
453 break;
454 case S_IFBLK:
455 sd->sc_dk.dk_bopenmask |= (1 << part);
456 break;
457 }
458 sd->sc_dk.dk_openmask =
459 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
460
461 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
462 sdunlock(sd);
463 return (0);
464
465 bad2:
466 sc_link->flags &= ~SDEV_MEDIA_LOADED;
467
468 bad:
469 if (sd->sc_dk.dk_openmask == 0) {
470 scsipi_prevent(sc_link, PR_ALLOW,
471 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
472 sc_link->flags &= ~SDEV_OPEN;
473 }
474
475 bad3:
476 sdunlock(sd);
477 bad4:
478 if (sd->sc_dk.dk_openmask == 0)
479 scsipi_adapter_delref(sc_link);
480 return (error);
481 }
482
483 /*
484 * close the device.. only called if we are the LAST occurence of an open
485 * device. Convenient now but usually a pain.
486 */
487 int
488 sdclose(dev, flag, fmt, p)
489 dev_t dev;
490 int flag, fmt;
491 struct proc *p;
492 {
493 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
494 int part = SDPART(dev);
495 int error;
496
497 if ((error = sdlock(sd)) != 0)
498 return (error);
499
500 switch (fmt) {
501 case S_IFCHR:
502 sd->sc_dk.dk_copenmask &= ~(1 << part);
503 break;
504 case S_IFBLK:
505 sd->sc_dk.dk_bopenmask &= ~(1 << part);
506 break;
507 }
508 sd->sc_dk.dk_openmask =
509 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
510
511 if (sd->sc_dk.dk_openmask == 0) {
512 /*
513 * If the disk cache needs flushing, and the disk supports
514 * it, do it now.
515 */
516 if ((sd->flags & SDF_DIRTY) != 0 &&
517 sd->sc_ops->sdo_flush != NULL) {
518 if ((*sd->sc_ops->sdo_flush)(sd, 0)) {
519 printf("%s: cache synchronization failed\n",
520 sd->sc_dev.dv_xname);
521 sd->flags &= ~SDF_FLUSHING;
522 } else
523 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
524 }
525
526 scsipi_wait_drain(sd->sc_link);
527
528 scsipi_prevent(sd->sc_link, PR_ALLOW,
529 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
530 sd->sc_link->flags &= ~SDEV_OPEN;
531
532 scsipi_wait_drain(sd->sc_link);
533
534 scsipi_adapter_delref(sd->sc_link);
535 }
536
537 sdunlock(sd);
538 return (0);
539 }
540
541 /*
542 * Actually translate the requested transfer into one the physical driver
543 * can understand. The transfer is described by a buf and will include
544 * only one physical transfer.
545 */
546 void
547 sdstrategy(bp)
548 struct buf *bp;
549 {
550 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
551 int s;
552
553 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
554 SC_DEBUG(sd->sc_link, SDEV_DB1,
555 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
556 /*
557 * If the device has been made invalid, error out
558 */
559 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0 ||
560 (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
561 if (sd->sc_link->flags & SDEV_OPEN)
562 bp->b_error = EIO;
563 else
564 bp->b_error = ENODEV;
565 goto bad;
566 }
567 /*
568 * The transfer must be a whole number of blocks, offset must not be
569 * negative.
570 */
571 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0 ||
572 bp->b_blkno < 0) {
573 bp->b_error = EINVAL;
574 goto bad;
575 }
576 /*
577 * If it's a null transfer, return immediatly
578 */
579 if (bp->b_bcount == 0)
580 goto done;
581
582 /*
583 * Do bounds checking, adjust transfer. if error, process.
584 * If end of partition, just return.
585 */
586 if (SDPART(bp->b_dev) != RAW_PART &&
587 bounds_check_with_label(bp, sd->sc_dk.dk_label,
588 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
589 goto done;
590
591 s = splbio();
592
593 /*
594 * Place it in the queue of disk activities for this disk
595 */
596 disksort(&sd->buf_queue, bp);
597
598 /*
599 * Tell the device to get going on the transfer if it's
600 * not doing anything, otherwise just wait for completion
601 */
602 sdstart(sd);
603
604 splx(s);
605 return;
606
607 bad:
608 bp->b_flags |= B_ERROR;
609 done:
610 /*
611 * Correctly set the buf to indicate a completed xfer
612 */
613 bp->b_resid = bp->b_bcount;
614 biodone(bp);
615 }
616
617 /*
618 * sdstart looks to see if there is a buf waiting for the device
619 * and that the device is not already busy. If both are true,
620 * It dequeues the buf and creates a scsi command to perform the
621 * transfer in the buf. The transfer request will call scsipi_done
622 * on completion, which will in turn call this routine again
623 * so that the next queued transfer is performed.
624 * The bufs are queued by the strategy routine (sdstrategy)
625 *
626 * This routine is also called after other non-queued requests
627 * have been made of the scsi driver, to ensure that the queue
628 * continues to be drained.
629 *
630 * must be called at the correct (highish) spl level
631 * sdstart() is called at splbio from sdstrategy and scsipi_done
632 */
633 void
634 sdstart(v)
635 register void *v;
636 {
637 register struct sd_softc *sd = v;
638 register struct scsipi_link *sc_link = sd->sc_link;
639 struct disklabel *lp = sd->sc_dk.dk_label;
640 struct buf *bp = 0;
641 struct buf *dp;
642 struct scsipi_rw_big cmd_big;
643 #if NSD_SCSIBUS > 0
644 struct scsi_rw cmd_small;
645 #endif
646 struct scsipi_generic *cmdp;
647 int blkno, nblks, cmdlen, error;
648 struct partition *p;
649
650 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
651 /*
652 * Check if the device has room for another command
653 */
654 while (sc_link->openings > 0) {
655 /*
656 * there is excess capacity, but a special waits
657 * It'll need the adapter as soon as we clear out of the
658 * way and let it run (user level wait).
659 */
660 if (sc_link->flags & SDEV_WAITING) {
661 sc_link->flags &= ~SDEV_WAITING;
662 wakeup((caddr_t)sc_link);
663 return;
664 }
665
666 /*
667 * See if there is a buf with work for us to do..
668 */
669 dp = &sd->buf_queue;
670 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
671 return;
672 dp->b_actf = bp->b_actf;
673
674 /*
675 * If the device has become invalid, abort all the
676 * reads and writes until all files have been closed and
677 * re-opened
678 */
679 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
680 bp->b_error = EIO;
681 bp->b_flags |= B_ERROR;
682 bp->b_resid = bp->b_bcount;
683 biodone(bp);
684 continue;
685 }
686
687 /*
688 * We have a buf, now we should make a command
689 *
690 * First, translate the block to absolute and put it in terms
691 * of the logical blocksize of the device.
692 */
693 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
694 if (SDPART(bp->b_dev) != RAW_PART) {
695 p = &lp->d_partitions[SDPART(bp->b_dev)];
696 blkno += p->p_offset;
697 }
698 nblks = howmany(bp->b_bcount, lp->d_secsize);
699
700 #if NSD_SCSIBUS > 0
701 /*
702 * Fill out the scsi command. If the transfer will
703 * fit in a "small" cdb, use it.
704 */
705 if (((blkno & 0x1fffff) == blkno) &&
706 ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) {
707 /*
708 * We can fit in a small cdb.
709 */
710 bzero(&cmd_small, sizeof(cmd_small));
711 cmd_small.opcode = (bp->b_flags & B_READ) ?
712 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
713 _lto3b(blkno, cmd_small.addr);
714 cmd_small.length = nblks & 0xff;
715 cmdlen = sizeof(cmd_small);
716 cmdp = (struct scsipi_generic *)&cmd_small;
717 } else
718 #endif
719 {
720 /*
721 * Need a large cdb.
722 */
723 bzero(&cmd_big, sizeof(cmd_big));
724 cmd_big.opcode = (bp->b_flags & B_READ) ?
725 READ_BIG : WRITE_BIG;
726 _lto4b(blkno, cmd_big.addr);
727 _lto2b(nblks, cmd_big.length);
728 cmdlen = sizeof(cmd_big);
729 cmdp = (struct scsipi_generic *)&cmd_big;
730 }
731
732 /* Instrumentation. */
733 disk_busy(&sd->sc_dk);
734
735 /*
736 * Mark the disk dirty so that the cache will be
737 * flushed on close.
738 */
739 if ((bp->b_flags & B_READ) == 0)
740 sd->flags |= SDF_DIRTY;
741
742 /*
743 * Call the routine that chats with the adapter.
744 * Note: we cannot sleep as we may be an interrupt
745 */
746 error = scsipi_command(sc_link, cmdp, cmdlen,
747 (u_char *)bp->b_data, bp->b_bcount,
748 SDRETRIES, 60000, bp, SCSI_NOSLEEP |
749 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT));
750 if (error) {
751 disk_unbusy(&sd->sc_dk, 0);
752 printf("%s: not queued, error %d\n",
753 sd->sc_dev.dv_xname, error);
754 }
755 }
756 }
757
758 void
759 sddone(xs)
760 struct scsipi_xfer *xs;
761 {
762 struct sd_softc *sd = xs->sc_link->device_softc;
763
764 if (sd->flags & SDF_FLUSHING) {
765 /* Flush completed, no longer dirty. */
766 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
767 }
768
769 if (xs->bp != NULL) {
770 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
771 #if NRND > 0
772 rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno);
773 #endif
774 }
775 }
776
777 void
778 sdminphys(bp)
779 struct buf *bp;
780 {
781 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
782 long max;
783
784 /*
785 * If the device is ancient, we want to make sure that
786 * the transfer fits into a 6-byte cdb.
787 *
788 * XXX Note that the SCSI-I spec says that 256-block transfers
789 * are allowed in a 6-byte read/write, and are specified
790 * by settng the "length" to 0. However, we're conservative
791 * here, allowing only 255-block transfers in case an
792 * ancient device gets confused by length == 0. A length of 0
793 * in a 10-byte read/write actually means 0 blocks.
794 */
795 if (sd->flags & SDF_ANCIENT) {
796 max = sd->sc_dk.dk_label->d_secsize * 0xff;
797
798 if (bp->b_bcount > max)
799 bp->b_bcount = max;
800 }
801
802 (*sd->sc_link->adapter->scsipi_minphys)(bp);
803 }
804
805 int
806 sdread(dev, uio, ioflag)
807 dev_t dev;
808 struct uio *uio;
809 int ioflag;
810 {
811
812 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
813 }
814
815 int
816 sdwrite(dev, uio, ioflag)
817 dev_t dev;
818 struct uio *uio;
819 int ioflag;
820 {
821
822 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
823 }
824
825 /*
826 * Perform special action on behalf of the user
827 * Knows about the internals of this device
828 */
829 int
830 sdioctl(dev, cmd, addr, flag, p)
831 dev_t dev;
832 u_long cmd;
833 caddr_t addr;
834 int flag;
835 struct proc *p;
836 {
837 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
838 int part = SDPART(dev);
839 int error;
840
841 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
842
843 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
844 return (ENODEV);
845
846 /*
847 * If the device is not valid, some IOCTLs can still be
848 * handled on the raw partition. Check this here.
849 */
850 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
851 switch (cmd) {
852 case DIOCWLABEL:
853 case DIOCLOCK:
854 case DIOCEJECT:
855 case ODIOCEJECT:
856 case SCIOCIDENTIFY:
857 case OSCIOCIDENTIFY:
858 case SCIOCCOMMAND:
859 case SCIOCDEBUG:
860 if (part == RAW_PART)
861 break;
862 /* FALLTHROUGH */
863 default:
864 if ((sd->sc_link->flags & SDEV_OPEN) == 0)
865 return (ENODEV);
866 else
867 return (EIO);
868 }
869 }
870
871 switch (cmd) {
872 case DIOCGDINFO:
873 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
874 return (0);
875
876 case DIOCGPART:
877 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
878 ((struct partinfo *)addr)->part =
879 &sd->sc_dk.dk_label->d_partitions[part];
880 return (0);
881
882 case DIOCWDINFO:
883 case DIOCSDINFO:
884 if ((flag & FWRITE) == 0)
885 return (EBADF);
886
887 if ((error = sdlock(sd)) != 0)
888 return (error);
889 sd->flags |= SDF_LABELLING;
890
891 error = setdisklabel(sd->sc_dk.dk_label,
892 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
893 sd->sc_dk.dk_cpulabel);
894 if (error == 0) {
895 if (cmd == DIOCWDINFO)
896 error = writedisklabel(SDLABELDEV(dev),
897 sdstrategy, sd->sc_dk.dk_label,
898 sd->sc_dk.dk_cpulabel);
899 }
900
901 sd->flags &= ~SDF_LABELLING;
902 sdunlock(sd);
903 return (error);
904
905 case DIOCWLABEL:
906 if ((flag & FWRITE) == 0)
907 return (EBADF);
908 if (*(int *)addr)
909 sd->flags |= SDF_WLABEL;
910 else
911 sd->flags &= ~SDF_WLABEL;
912 return (0);
913
914 case DIOCLOCK:
915 return (scsipi_prevent(sd->sc_link,
916 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
917
918 case DIOCEJECT:
919 if ((sd->sc_link->flags & SDEV_REMOVABLE) == 0)
920 return (ENOTTY);
921 if (*(int *)addr == 0) {
922 /*
923 * Don't force eject: check that we are the only
924 * partition open. If so, unlock it.
925 */
926 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
927 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
928 sd->sc_dk.dk_openmask) {
929 error = scsipi_prevent(sd->sc_link, PR_ALLOW,
930 SCSI_IGNORE_NOT_READY);
931 if (error)
932 return (error);
933 } else {
934 return (EBUSY);
935 }
936 }
937 /* FALLTHROUGH */
938 case ODIOCEJECT:
939 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
940 scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
941
942 case DIOCGDEFLABEL:
943 sdgetdefaultlabel(sd, (struct disklabel *)addr);
944 return (0);
945
946 default:
947 if (part != RAW_PART)
948 return (ENOTTY);
949 return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p));
950 }
951
952 #ifdef DIAGNOSTIC
953 panic("sdioctl: impossible");
954 #endif
955 }
956
957 void
958 sdgetdefaultlabel(sd, lp)
959 struct sd_softc *sd;
960 struct disklabel *lp;
961 {
962
963 bzero(lp, sizeof(struct disklabel));
964
965 lp->d_secsize = sd->params.blksize;
966 lp->d_ntracks = sd->params.heads;
967 lp->d_nsectors = sd->params.sectors;
968 lp->d_ncylinders = sd->params.cyls;
969 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
970
971 switch (sd->sc_link->type) {
972 #if NSD_SCSIBUS > 0
973 case BUS_SCSI:
974 lp->d_type = DTYPE_SCSI;
975 break;
976 #endif
977 #if NSD_ATAPIBUS > 0
978 case BUS_ATAPI:
979 lp->d_type = DTYPE_ATAPI;
980 break;
981 #endif
982 }
983 strncpy(lp->d_typename, sd->name, 16);
984 strncpy(lp->d_packname, "fictitious", 16);
985 lp->d_secperunit = sd->params.disksize;
986 lp->d_rpm = sd->params.rot_rate;
987 lp->d_interleave = 1;
988 lp->d_flags = 0;
989
990 lp->d_partitions[RAW_PART].p_offset = 0;
991 lp->d_partitions[RAW_PART].p_size =
992 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
993 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
994 lp->d_npartitions = RAW_PART + 1;
995
996 lp->d_magic = DISKMAGIC;
997 lp->d_magic2 = DISKMAGIC;
998 lp->d_checksum = dkcksum(lp);
999 }
1000
1001
1002 /*
1003 * Load the label information on the named device
1004 */
1005 void
1006 sdgetdisklabel(sd)
1007 struct sd_softc *sd;
1008 {
1009 struct disklabel *lp = sd->sc_dk.dk_label;
1010 char *errstring;
1011
1012 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
1013
1014 sdgetdefaultlabel(sd, lp);
1015
1016 if (lp->d_secpercyl == 0) {
1017 lp->d_secpercyl = 100;
1018 /* as long as it's not 0 - readdisklabel divides by it (?) */
1019 }
1020
1021 /*
1022 * Call the generic disklabel extraction routine
1023 */
1024 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
1025 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1026 if (errstring) {
1027 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1028 return;
1029 }
1030 }
1031
1032 void
1033 sd_shutdown(arg)
1034 void *arg;
1035 {
1036 struct sd_softc *sd = arg;
1037
1038 /*
1039 * If the disk cache needs to be flushed, and the disk supports
1040 * it, flush it. We're cold at this point, so we poll for
1041 * completion.
1042 */
1043 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) {
1044 if ((*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF)) {
1045 printf("%s: cache synchronization failed\n",
1046 sd->sc_dev.dv_xname);
1047 sd->flags &= ~SDF_FLUSHING;
1048 } else
1049 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1050 }
1051 }
1052
1053 /*
1054 * Tell the device to map out a defective block
1055 */
1056 int
1057 sd_reassign_blocks(sd, blkno)
1058 struct sd_softc *sd;
1059 u_long blkno;
1060 {
1061 struct scsi_reassign_blocks scsipi_cmd;
1062 struct scsi_reassign_blocks_data rbdata;
1063
1064 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
1065 bzero(&rbdata, sizeof(rbdata));
1066 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
1067
1068 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
1069 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
1070
1071 return (scsipi_command(sd->sc_link,
1072 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1073 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
1074 SCSI_DATA_OUT));
1075 }
1076
1077 /*
1078 * Check Errors
1079 */
1080 int
1081 sd_interpret_sense(xs)
1082 struct scsipi_xfer *xs;
1083 {
1084 struct scsipi_link *sc_link = xs->sc_link;
1085 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1086 struct sd_softc *sd = sc_link->device_softc;
1087 int retval = SCSIRET_CONTINUE;
1088
1089 /*
1090 * If the device is not open yet, let the generic code handle it.
1091 */
1092 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
1093 return (retval);
1094 }
1095
1096 /*
1097 * If it isn't a extended or extended/deferred error, let
1098 * the generic code handle it.
1099 */
1100 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1101 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
1102 return (retval);
1103 }
1104
1105 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1106 sense->add_sense_code == 0x4) {
1107 if (sense->add_sense_code_qual == 0x01) {
1108 printf("%s: ..is spinning up...waiting\n",
1109 sd->sc_dev.dv_xname);
1110 /*
1111 * I really need a sdrestart function I can call here.
1112 */
1113 delay(1000000 * 5); /* 5 seconds */
1114 retval = SCSIRET_RETRY;
1115 } else if ((sense->add_sense_code_qual == 0x2) &&
1116 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
1117 if (sd->sc_link->flags & SDEV_REMOVABLE) {
1118 printf(
1119 "%s: removable disk stopped - not restarting\n",
1120 sd->sc_dev.dv_xname);
1121 retval = EIO;
1122 } else {
1123 printf("%s: respinning up disk\n",
1124 sd->sc_dev.dv_xname);
1125 retval = scsipi_start(sd->sc_link, SSS_START,
1126 SCSI_URGENT | SCSI_NOSLEEP);
1127 if (retval != 0) {
1128 printf(
1129 "%s: respin of disk failed - %d\n",
1130 sd->sc_dev.dv_xname, retval);
1131 retval = EIO;
1132 } else {
1133 retval = SCSIRET_RETRY;
1134 }
1135 }
1136 }
1137 }
1138 return (retval);
1139 }
1140
1141
1142 int
1143 sdsize(dev)
1144 dev_t dev;
1145 {
1146 struct sd_softc *sd;
1147 int part, unit, omask;
1148 int size;
1149
1150 unit = SDUNIT(dev);
1151 if (unit >= sd_cd.cd_ndevs)
1152 return (-1);
1153 sd = sd_cd.cd_devs[unit];
1154 if (sd == NULL)
1155 return (-1);
1156
1157 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1158 return (-1);
1159
1160 part = SDPART(dev);
1161 omask = sd->sc_dk.dk_openmask & (1 << part);
1162
1163 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1164 return (-1);
1165 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
1166 size = -1;
1167 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1168 size = -1;
1169 else
1170 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1171 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1172 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1173 return (-1);
1174 return (size);
1175 }
1176
1177 #ifndef __BDEVSW_DUMP_OLD_TYPE
1178 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1179 static struct scsipi_xfer sx;
1180 static int sddoingadump;
1181
1182 /*
1183 * dump all of physical memory into the partition specified, starting
1184 * at offset 'dumplo' into the partition.
1185 */
1186 int
1187 sddump(dev, blkno, va, size)
1188 dev_t dev;
1189 daddr_t blkno;
1190 caddr_t va;
1191 size_t size;
1192 {
1193 struct sd_softc *sd; /* disk unit to do the I/O */
1194 struct disklabel *lp; /* disk's disklabel */
1195 int unit, part;
1196 int sectorsize; /* size of a disk sector */
1197 int nsects; /* number of sectors in partition */
1198 int sectoff; /* sector offset of partition */
1199 int totwrt; /* total number of sectors left to write */
1200 int nwrt; /* current number of sectors to write */
1201 struct scsipi_rw_big cmd; /* write command */
1202 struct scsipi_xfer *xs; /* ... convenience */
1203 int retval;
1204
1205 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1206 return (ENODEV);
1207
1208 /* Check if recursive dump; if so, punt. */
1209 if (sddoingadump)
1210 return (EFAULT);
1211
1212 /* Mark as active early. */
1213 sddoingadump = 1;
1214
1215 unit = SDUNIT(dev); /* Decompose unit & partition. */
1216 part = SDPART(dev);
1217
1218 /* Check for acceptable drive number. */
1219 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1220 return (ENXIO);
1221
1222 /* Make sure it was initialized. */
1223 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1224 return (ENXIO);
1225
1226 /* Convert to disk sectors. Request must be a multiple of size. */
1227 lp = sd->sc_dk.dk_label;
1228 sectorsize = lp->d_secsize;
1229 if ((size % sectorsize) != 0)
1230 return (EFAULT);
1231 totwrt = size / sectorsize;
1232 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1233
1234 nsects = lp->d_partitions[part].p_size;
1235 sectoff = lp->d_partitions[part].p_offset;
1236
1237 /* Check transfer bounds against partition size. */
1238 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1239 return (EINVAL);
1240
1241 /* Offset block number to start of partition. */
1242 blkno += sectoff;
1243
1244 xs = &sx;
1245
1246 while (totwrt > 0) {
1247 nwrt = totwrt; /* XXX */
1248 #ifndef SD_DUMP_NOT_TRUSTED
1249 /*
1250 * Fill out the scsi command
1251 */
1252 bzero(&cmd, sizeof(cmd));
1253 cmd.opcode = WRITE_BIG;
1254 _lto4b(blkno, cmd.addr);
1255 _lto2b(nwrt, cmd.length);
1256 /*
1257 * Fill out the scsipi_xfer structure
1258 * Note: we cannot sleep as we may be an interrupt
1259 * don't use scsipi_command() as it may want to wait
1260 * for an xs.
1261 */
1262 bzero(xs, sizeof(sx));
1263 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1264 xs->sc_link = sd->sc_link;
1265 xs->retries = SDRETRIES;
1266 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1267 xs->cmd = (struct scsipi_generic *)&cmd;
1268 xs->cmdlen = sizeof(cmd);
1269 xs->resid = nwrt * sectorsize;
1270 xs->error = XS_NOERROR;
1271 xs->bp = 0;
1272 xs->data = va;
1273 xs->datalen = nwrt * sectorsize;
1274
1275 /*
1276 * Pass all this info to the scsi driver.
1277 */
1278 retval = scsipi_command_direct(xs);
1279 if (retval != COMPLETE)
1280 return (ENXIO);
1281 #else /* SD_DUMP_NOT_TRUSTED */
1282 /* Let's just talk about this first... */
1283 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1284 delay(500 * 1000); /* half a second */
1285 #endif /* SD_DUMP_NOT_TRUSTED */
1286
1287 /* update block count */
1288 totwrt -= nwrt;
1289 blkno += nwrt;
1290 va += sectorsize * nwrt;
1291 }
1292 sddoingadump = 0;
1293 return (0);
1294 }
1295 #else /* __BDEVSW_DUMP_NEW_TYPE */
1296 int
1297 sddump(dev, blkno, va, size)
1298 dev_t dev;
1299 daddr_t blkno;
1300 caddr_t va;
1301 size_t size;
1302 {
1303
1304 /* Not implemented. */
1305 return (ENXIO);
1306 }
1307 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1308