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