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