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