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