sd.c revision 1.142 1 /* $NetBSD: sd.c,v 1.142 1999/02/08 16:33:18 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.
480 */
481 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) {
482 bp->b_error = EINVAL;
483 goto bad;
484 }
485 /*
486 * If it's a null transfer, return immediatly
487 */
488 if (bp->b_bcount == 0)
489 goto done;
490
491 /*
492 * Do bounds checking, adjust transfer. if error, process.
493 * If end of partition, just return.
494 */
495 if (SDPART(bp->b_dev) != RAW_PART &&
496 bounds_check_with_label(bp, sd->sc_dk.dk_label,
497 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
498 goto done;
499
500 s = splbio();
501
502 /*
503 * Place it in the queue of disk activities for this disk
504 */
505 disksort(&sd->buf_queue, bp);
506
507 /*
508 * Tell the device to get going on the transfer if it's
509 * not doing anything, otherwise just wait for completion
510 */
511 sdstart(sd);
512
513 splx(s);
514 return;
515
516 bad:
517 bp->b_flags |= B_ERROR;
518 done:
519 /*
520 * Correctly set the buf to indicate a completed xfer
521 */
522 bp->b_resid = bp->b_bcount;
523 biodone(bp);
524 }
525
526 /*
527 * sdstart looks to see if there is a buf waiting for the device
528 * and that the device is not already busy. If both are true,
529 * It dequeues the buf and creates a scsi command to perform the
530 * transfer in the buf. The transfer request will call scsipi_done
531 * on completion, which will in turn call this routine again
532 * so that the next queued transfer is performed.
533 * The bufs are queued by the strategy routine (sdstrategy)
534 *
535 * This routine is also called after other non-queued requests
536 * have been made of the scsi driver, to ensure that the queue
537 * continues to be drained.
538 *
539 * must be called at the correct (highish) spl level
540 * sdstart() is called at splbio from sdstrategy and scsipi_done
541 */
542 void
543 sdstart(v)
544 register void *v;
545 {
546 register struct sd_softc *sd = v;
547 register struct scsipi_link *sc_link = sd->sc_link;
548 struct disklabel *lp = sd->sc_dk.dk_label;
549 struct buf *bp = 0;
550 struct buf *dp;
551 struct scsipi_rw_big cmd_big;
552 #if NSD_SCSIBUS > 0
553 struct scsi_rw cmd_small;
554 #endif
555 struct scsipi_generic *cmdp;
556 int blkno, nblks, cmdlen, error;
557 struct partition *p;
558
559 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
560 /*
561 * Check if the device has room for another command
562 */
563 while (sc_link->openings > 0) {
564 /*
565 * there is excess capacity, but a special waits
566 * It'll need the adapter as soon as we clear out of the
567 * way and let it run (user level wait).
568 */
569 if (sc_link->flags & SDEV_WAITING) {
570 sc_link->flags &= ~SDEV_WAITING;
571 wakeup((caddr_t)sc_link);
572 return;
573 }
574
575 /*
576 * See if there is a buf with work for us to do..
577 */
578 dp = &sd->buf_queue;
579 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
580 return;
581 dp->b_actf = bp->b_actf;
582
583 /*
584 * If the device has become invalid, abort all the
585 * reads and writes until all files have been closed and
586 * re-opened
587 */
588 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
589 bp->b_error = EIO;
590 bp->b_flags |= B_ERROR;
591 bp->b_resid = bp->b_bcount;
592 biodone(bp);
593 continue;
594 }
595
596 /*
597 * We have a buf, now we should make a command
598 *
599 * First, translate the block to absolute and put it in terms
600 * of the logical blocksize of the device.
601 */
602 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
603 if (SDPART(bp->b_dev) != RAW_PART) {
604 p = &lp->d_partitions[SDPART(bp->b_dev)];
605 blkno += p->p_offset;
606 }
607 nblks = howmany(bp->b_bcount, lp->d_secsize);
608
609 #if NSD_SCSIBUS > 0
610 /*
611 * Fill out the scsi command. If the transfer will
612 * fit in a "small" cdb, use it.
613 */
614 if (((blkno & 0x1fffff) == blkno) &&
615 ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) {
616 /*
617 * We can fit in a small cdb.
618 */
619 bzero(&cmd_small, sizeof(cmd_small));
620 cmd_small.opcode = (bp->b_flags & B_READ) ?
621 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
622 _lto3b(blkno, cmd_small.addr);
623 cmd_small.length = nblks & 0xff;
624 cmdlen = sizeof(cmd_small);
625 cmdp = (struct scsipi_generic *)&cmd_small;
626 } else
627 #endif
628 {
629 /*
630 * Need a large cdb.
631 */
632 bzero(&cmd_big, sizeof(cmd_big));
633 cmd_big.opcode = (bp->b_flags & B_READ) ?
634 READ_BIG : WRITE_BIG;
635 _lto4b(blkno, cmd_big.addr);
636 _lto2b(nblks, cmd_big.length);
637 cmdlen = sizeof(cmd_big);
638 cmdp = (struct scsipi_generic *)&cmd_big;
639 }
640
641 /* Instrumentation. */
642 disk_busy(&sd->sc_dk);
643
644 /*
645 * Mark the disk dirty so that the cache will be
646 * flushed on close.
647 */
648 if ((bp->b_flags & B_READ) == 0)
649 sd->flags |= SDF_DIRTY;
650
651 /*
652 * Call the routine that chats with the adapter.
653 * Note: we cannot sleep as we may be an interrupt
654 */
655 error = scsipi_command(sc_link, cmdp, cmdlen,
656 (u_char *)bp->b_data, bp->b_bcount,
657 SDRETRIES, 60000, bp, SCSI_NOSLEEP |
658 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT));
659 if (error) {
660 disk_unbusy(&sd->sc_dk, 0);
661 printf("%s: not queued, error %d\n",
662 sd->sc_dev.dv_xname, error);
663 }
664 }
665 }
666
667 void
668 sddone(xs)
669 struct scsipi_xfer *xs;
670 {
671 struct sd_softc *sd = xs->sc_link->device_softc;
672
673 if (sd->flags & SDF_FLUSHING) {
674 /* Flush completed, no longer dirty. */
675 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
676 }
677
678 if (xs->bp != NULL) {
679 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
680 #if NRND > 0
681 rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno);
682 #endif
683 }
684 }
685
686 void
687 sdminphys(bp)
688 struct buf *bp;
689 {
690 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
691 long max;
692
693 /*
694 * If the device is ancient, we want to make sure that
695 * the transfer fits into a 6-byte cdb.
696 *
697 * XXX Note that the SCSI-I spec says that 256-block transfers
698 * are allowed in a 6-byte read/write, and are specified
699 * by settng the "length" to 0. However, we're conservative
700 * here, allowing only 255-block transfers in case an
701 * ancient device gets confused by length == 0. A length of 0
702 * in a 10-byte read/write actually means 0 blocks.
703 */
704 if (sd->flags & SDF_ANCIENT) {
705 max = sd->sc_dk.dk_label->d_secsize * 0xff;
706
707 if (bp->b_bcount > max)
708 bp->b_bcount = max;
709 }
710
711 (*sd->sc_link->adapter->scsipi_minphys)(bp);
712 }
713
714 int
715 sdread(dev, uio, ioflag)
716 dev_t dev;
717 struct uio *uio;
718 int ioflag;
719 {
720
721 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
722 }
723
724 int
725 sdwrite(dev, uio, ioflag)
726 dev_t dev;
727 struct uio *uio;
728 int ioflag;
729 {
730
731 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
732 }
733
734 /*
735 * Perform special action on behalf of the user
736 * Knows about the internals of this device
737 */
738 int
739 sdioctl(dev, cmd, addr, flag, p)
740 dev_t dev;
741 u_long cmd;
742 caddr_t addr;
743 int flag;
744 struct proc *p;
745 {
746 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
747 int part = SDPART(dev);
748 int error;
749
750 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
751
752 /*
753 * If the device is not valid, some IOCTLs can still be
754 * handled on the raw partition. Check this here.
755 */
756 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
757 switch (cmd) {
758 case DIOCWLABEL:
759 case DIOCLOCK:
760 case DIOCEJECT:
761 case ODIOCEJECT:
762 case SCIOCIDENTIFY:
763 case OSCIOCIDENTIFY:
764 case SCIOCCOMMAND:
765 case SCIOCDEBUG:
766 if (part == RAW_PART)
767 break;
768 /* FALLTHROUGH */
769 default:
770 if ((sd->sc_link->flags & SDEV_OPEN) == 0)
771 return (ENODEV);
772 else
773 return (EIO);
774 }
775 }
776
777 switch (cmd) {
778 case DIOCGDINFO:
779 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
780 return (0);
781
782 case DIOCGPART:
783 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
784 ((struct partinfo *)addr)->part =
785 &sd->sc_dk.dk_label->d_partitions[part];
786 return (0);
787
788 case DIOCWDINFO:
789 case DIOCSDINFO:
790 if ((flag & FWRITE) == 0)
791 return (EBADF);
792
793 if ((error = sdlock(sd)) != 0)
794 return (error);
795 sd->flags |= SDF_LABELLING;
796
797 error = setdisklabel(sd->sc_dk.dk_label,
798 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
799 sd->sc_dk.dk_cpulabel);
800 if (error == 0) {
801 if (cmd == DIOCWDINFO)
802 error = writedisklabel(SDLABELDEV(dev),
803 sdstrategy, sd->sc_dk.dk_label,
804 sd->sc_dk.dk_cpulabel);
805 }
806
807 sd->flags &= ~SDF_LABELLING;
808 sdunlock(sd);
809 return (error);
810
811 case DIOCWLABEL:
812 if ((flag & FWRITE) == 0)
813 return (EBADF);
814 if (*(int *)addr)
815 sd->flags |= SDF_WLABEL;
816 else
817 sd->flags &= ~SDF_WLABEL;
818 return (0);
819
820 case DIOCLOCK:
821 return (scsipi_prevent(sd->sc_link,
822 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
823
824 case DIOCEJECT:
825 if ((sd->sc_link->flags & SDEV_REMOVABLE) == 0)
826 return (ENOTTY);
827 if (*(int *)addr == 0) {
828 /*
829 * Don't force eject: check that we are the only
830 * partition open. If so, unlock it.
831 */
832 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
833 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
834 sd->sc_dk.dk_openmask) {
835 error = scsipi_prevent(sd->sc_link, PR_ALLOW,
836 SCSI_IGNORE_NOT_READY);
837 if (error)
838 return (error);
839 } else {
840 return (EBUSY);
841 }
842 }
843 /* FALLTHROUGH */
844 case ODIOCEJECT:
845 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
846 scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
847
848 case DIOCGDEFLABEL:
849 sdgetdefaultlabel(sd, (struct disklabel *)addr);
850 return (0);
851
852 default:
853 if (part != RAW_PART)
854 return (ENOTTY);
855 return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p));
856 }
857
858 #ifdef DIAGNOSTIC
859 panic("sdioctl: impossible");
860 #endif
861 }
862
863 void
864 sdgetdefaultlabel(sd, lp)
865 struct sd_softc *sd;
866 struct disklabel *lp;
867 {
868
869 bzero(lp, sizeof(struct disklabel));
870
871 lp->d_secsize = sd->params.blksize;
872 lp->d_ntracks = sd->params.heads;
873 lp->d_nsectors = sd->params.sectors;
874 lp->d_ncylinders = sd->params.cyls;
875 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
876
877 switch (sd->sc_link->type) {
878 #if NSD_SCSIBUS > 0
879 case BUS_SCSI:
880 lp->d_type = DTYPE_SCSI;
881 break;
882 #endif
883 #if NSD_ATAPIBUS > 0
884 case BUS_ATAPI:
885 lp->d_type = DTYPE_ATAPI;
886 break;
887 #endif
888 }
889 strncpy(lp->d_typename, sd->name, 16);
890 strncpy(lp->d_packname, "fictitious", 16);
891 lp->d_secperunit = sd->params.disksize;
892 lp->d_rpm = sd->params.rot_rate;
893 lp->d_interleave = 1;
894 lp->d_flags = 0;
895
896 lp->d_partitions[RAW_PART].p_offset = 0;
897 lp->d_partitions[RAW_PART].p_size =
898 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
899 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
900 lp->d_npartitions = RAW_PART + 1;
901
902 lp->d_magic = DISKMAGIC;
903 lp->d_magic2 = DISKMAGIC;
904 lp->d_checksum = dkcksum(lp);
905 }
906
907
908 /*
909 * Load the label information on the named device
910 */
911 void
912 sdgetdisklabel(sd)
913 struct sd_softc *sd;
914 {
915 struct disklabel *lp = sd->sc_dk.dk_label;
916 char *errstring;
917
918 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
919
920 sdgetdefaultlabel(sd, lp);
921
922 if (lp->d_secpercyl == 0) {
923 lp->d_secpercyl = 100;
924 /* as long as it's not 0 - readdisklabel divides by it (?) */
925 }
926
927 /*
928 * Call the generic disklabel extraction routine
929 */
930 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
931 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
932 if (errstring) {
933 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
934 return;
935 }
936 }
937
938 void
939 sd_shutdown(arg)
940 void *arg;
941 {
942 struct sd_softc *sd = arg;
943
944 /*
945 * If the disk cache needs to be flushed, and the disk supports
946 * it, flush it. We're cold at this point, so we poll for
947 * completion.
948 */
949 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL)
950 (*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF);
951 }
952
953 /*
954 * Tell the device to map out a defective block
955 */
956 int
957 sd_reassign_blocks(sd, blkno)
958 struct sd_softc *sd;
959 u_long blkno;
960 {
961 struct scsi_reassign_blocks scsipi_cmd;
962 struct scsi_reassign_blocks_data rbdata;
963
964 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
965 bzero(&rbdata, sizeof(rbdata));
966 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
967
968 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
969 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
970
971 return (scsipi_command(sd->sc_link,
972 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
973 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
974 SCSI_DATA_OUT));
975 }
976
977 /*
978 * Check Errors
979 */
980 int
981 sd_interpret_sense(xs)
982 struct scsipi_xfer *xs;
983 {
984 struct scsipi_link *sc_link = xs->sc_link;
985 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
986 struct sd_softc *sd = sc_link->device_softc;
987 int retval = SCSIRET_CONTINUE;
988
989 /*
990 * If the device is not open yet, let the generic code handle it.
991 */
992 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
993 return (retval);
994 }
995
996 /*
997 * If it isn't a extended or extended/deferred error, let
998 * the generic code handle it.
999 */
1000 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1001 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
1002 return (retval);
1003 }
1004
1005 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1006 sense->add_sense_code == 0x4) {
1007 if (sense->add_sense_code_qual == 0x01) {
1008 printf("%s: ..is spinning up...waiting\n",
1009 sd->sc_dev.dv_xname);
1010 /*
1011 * I really need a sdrestart function I can call here.
1012 */
1013 delay(1000000 * 5); /* 5 seconds */
1014 retval = SCSIRET_RETRY;
1015 } else if ((sense->add_sense_code_qual == 0x2) &&
1016 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
1017 if (sd->sc_link->flags & SDEV_REMOVABLE) {
1018 printf("%s: removable disk stopped- not "
1019 "restarting\n", sd->sc_dev.dv_xname);
1020 retval = EIO;
1021 } else {
1022 printf("%s: respinning up disk\n",
1023 sd->sc_dev.dv_xname);
1024 retval = scsipi_start(sd->sc_link, SSS_START,
1025 SCSI_URGENT | SCSI_NOSLEEP);
1026 if (retval != 0) {
1027 printf("%s: respin of disk failed-%d\n",
1028 sd->sc_dev.dv_xname, retval);
1029 retval = EIO;
1030 } else {
1031 retval = SCSIRET_RETRY;
1032 }
1033 }
1034 }
1035 }
1036 return (retval);
1037 }
1038
1039
1040 int
1041 sdsize(dev)
1042 dev_t dev;
1043 {
1044 struct sd_softc *sd;
1045 int part, unit, omask;
1046 int size;
1047
1048 unit = SDUNIT(dev);
1049 if (unit >= sd_cd.cd_ndevs)
1050 return (-1);
1051 sd = sd_cd.cd_devs[unit];
1052 if (sd == NULL)
1053 return (-1);
1054
1055 part = SDPART(dev);
1056 omask = sd->sc_dk.dk_openmask & (1 << part);
1057
1058 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1059 return (-1);
1060 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
1061 size = -1;
1062 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1063 size = -1;
1064 else
1065 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1066 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1067 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1068 return (-1);
1069 return (size);
1070 }
1071
1072 #ifndef __BDEVSW_DUMP_OLD_TYPE
1073 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1074 static struct scsipi_xfer sx;
1075 static int sddoingadump;
1076
1077 /*
1078 * dump all of physical memory into the partition specified, starting
1079 * at offset 'dumplo' into the partition.
1080 */
1081 int
1082 sddump(dev, blkno, va, size)
1083 dev_t dev;
1084 daddr_t blkno;
1085 caddr_t va;
1086 size_t size;
1087 {
1088 struct sd_softc *sd; /* disk unit to do the I/O */
1089 struct disklabel *lp; /* disk's disklabel */
1090 int unit, part;
1091 int sectorsize; /* size of a disk sector */
1092 int nsects; /* number of sectors in partition */
1093 int sectoff; /* sector offset of partition */
1094 int totwrt; /* total number of sectors left to write */
1095 int nwrt; /* current number of sectors to write */
1096 struct scsipi_rw_big cmd; /* write command */
1097 struct scsipi_xfer *xs; /* ... convenience */
1098 int retval;
1099
1100 /* Check if recursive dump; if so, punt. */
1101 if (sddoingadump)
1102 return (EFAULT);
1103
1104 /* Mark as active early. */
1105 sddoingadump = 1;
1106
1107 unit = SDUNIT(dev); /* Decompose unit & partition. */
1108 part = SDPART(dev);
1109
1110 /* Check for acceptable drive number. */
1111 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1112 return (ENXIO);
1113
1114 /* Make sure it was initialized. */
1115 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1116 return (ENXIO);
1117
1118 /* Convert to disk sectors. Request must be a multiple of size. */
1119 lp = sd->sc_dk.dk_label;
1120 sectorsize = lp->d_secsize;
1121 if ((size % sectorsize) != 0)
1122 return (EFAULT);
1123 totwrt = size / sectorsize;
1124 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1125
1126 nsects = lp->d_partitions[part].p_size;
1127 sectoff = lp->d_partitions[part].p_offset;
1128
1129 /* Check transfer bounds against partition size. */
1130 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1131 return (EINVAL);
1132
1133 /* Offset block number to start of partition. */
1134 blkno += sectoff;
1135
1136 xs = &sx;
1137
1138 while (totwrt > 0) {
1139 nwrt = totwrt; /* XXX */
1140 #ifndef SD_DUMP_NOT_TRUSTED
1141 /*
1142 * Fill out the scsi command
1143 */
1144 bzero(&cmd, sizeof(cmd));
1145 cmd.opcode = WRITE_BIG;
1146 _lto4b(blkno, cmd.addr);
1147 _lto2b(nwrt, cmd.length);
1148 /*
1149 * Fill out the scsipi_xfer structure
1150 * Note: we cannot sleep as we may be an interrupt
1151 * don't use scsipi_command() as it may want to wait
1152 * for an xs.
1153 */
1154 bzero(xs, sizeof(sx));
1155 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1156 xs->sc_link = sd->sc_link;
1157 xs->retries = SDRETRIES;
1158 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1159 xs->cmd = (struct scsipi_generic *)&cmd;
1160 xs->cmdlen = sizeof(cmd);
1161 xs->resid = nwrt * sectorsize;
1162 xs->error = XS_NOERROR;
1163 xs->bp = 0;
1164 xs->data = va;
1165 xs->datalen = nwrt * sectorsize;
1166
1167 /*
1168 * Pass all this info to the scsi driver.
1169 */
1170 retval = scsipi_command_direct(xs);
1171 if (retval != COMPLETE)
1172 return (ENXIO);
1173 #else /* SD_DUMP_NOT_TRUSTED */
1174 /* Let's just talk about this first... */
1175 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1176 delay(500 * 1000); /* half a second */
1177 #endif /* SD_DUMP_NOT_TRUSTED */
1178
1179 /* update block count */
1180 totwrt -= nwrt;
1181 blkno += nwrt;
1182 va += sectorsize * nwrt;
1183 }
1184 sddoingadump = 0;
1185 return (0);
1186 }
1187 #else /* __BDEVSW_DUMP_NEW_TYPE */
1188 int
1189 sddump(dev, blkno, va, size)
1190 dev_t dev;
1191 daddr_t blkno;
1192 caddr_t va;
1193 size_t size;
1194 {
1195
1196 /* Not implemented. */
1197 return (ENXIO);
1198 }
1199 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1200