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