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