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