sd.c revision 1.128 1 /* $NetBSD: sd.c,v 1.128 1998/07/30 00:49:21 mjacob 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 printf("%s: not queued, error %d\n",
624 sd->sc_dev.dv_xname, error);
625 }
626 }
627
628 void
629 sddone(xs)
630 struct scsipi_xfer *xs;
631 {
632 struct sd_softc *sd = xs->sc_link->device_softc;
633
634 if (xs->bp != NULL) {
635 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
636 #if NRND > 0
637 rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno);
638 #endif
639 }
640 }
641
642 void
643 sdminphys(bp)
644 struct buf *bp;
645 {
646 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
647 long max;
648
649 /*
650 * If the device is ancient, we want to make sure that
651 * the transfer fits into a 6-byte cdb.
652 *
653 * XXX Note that the SCSI-I spec says that 256-block transfers
654 * are allowed in a 6-byte read/write, and are specified
655 * by settng the "length" to 0. However, we're conservative
656 * here, allowing only 255-block transfers in case an
657 * ancient device gets confused by length == 0. A length of 0
658 * in a 10-byte read/write actually means 0 blocks.
659 */
660 if (sd->flags & SDF_ANCIENT) {
661 max = sd->sc_dk.dk_label->d_secsize * 0xff;
662
663 if (bp->b_bcount > max)
664 bp->b_bcount = max;
665 }
666
667 (*sd->sc_link->adapter->scsipi_minphys)(bp);
668 }
669
670 int
671 sdread(dev, uio, ioflag)
672 dev_t dev;
673 struct uio *uio;
674 int ioflag;
675 {
676
677 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
678 }
679
680 int
681 sdwrite(dev, uio, ioflag)
682 dev_t dev;
683 struct uio *uio;
684 int ioflag;
685 {
686
687 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
688 }
689
690 /*
691 * Perform special action on behalf of the user
692 * Knows about the internals of this device
693 */
694 int
695 sdioctl(dev, cmd, addr, flag, p)
696 dev_t dev;
697 u_long cmd;
698 caddr_t addr;
699 int flag;
700 struct proc *p;
701 {
702 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
703 int error;
704
705 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
706
707 /*
708 * If the device is not valid.. abandon ship
709 */
710 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
711 return (EIO);
712
713 switch (cmd) {
714 case DIOCGDINFO:
715 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
716 return (0);
717
718 case DIOCGPART:
719 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
720 ((struct partinfo *)addr)->part =
721 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
722 return (0);
723
724 case DIOCWDINFO:
725 case DIOCSDINFO:
726 if ((flag & FWRITE) == 0)
727 return (EBADF);
728
729 if ((error = sdlock(sd)) != 0)
730 return (error);
731 sd->flags |= SDF_LABELLING;
732
733 error = setdisklabel(sd->sc_dk.dk_label,
734 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
735 sd->sc_dk.dk_cpulabel);
736 if (error == 0) {
737 if (cmd == DIOCWDINFO)
738 error = writedisklabel(SDLABELDEV(dev),
739 sdstrategy, sd->sc_dk.dk_label,
740 sd->sc_dk.dk_cpulabel);
741 }
742
743 sd->flags &= ~SDF_LABELLING;
744 sdunlock(sd);
745 return (error);
746
747 case DIOCWLABEL:
748 if ((flag & FWRITE) == 0)
749 return (EBADF);
750 if (*(int *)addr)
751 sd->flags |= SDF_WLABEL;
752 else
753 sd->flags &= ~SDF_WLABEL;
754 return (0);
755
756 case DIOCLOCK:
757 return (scsipi_prevent(sd->sc_link,
758 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
759
760 case DIOCEJECT:
761 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
762 scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
763
764 case DIOCGDEFLABEL:
765 sdgetdefaultlabel(sd, (struct disklabel *)addr);
766 return (0);
767
768 default:
769 if (SDPART(dev) != RAW_PART)
770 return (ENOTTY);
771 return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p));
772 }
773
774 #ifdef DIAGNOSTIC
775 panic("sdioctl: impossible");
776 #endif
777 }
778
779 void
780 sdgetdefaultlabel(sd, lp)
781 struct sd_softc *sd;
782 struct disklabel *lp;
783 {
784
785 bzero(lp, sizeof(struct disklabel));
786
787 lp->d_secsize = sd->params.blksize;
788 lp->d_ntracks = sd->params.heads;
789 lp->d_nsectors = sd->params.sectors;
790 lp->d_ncylinders = sd->params.cyls;
791 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
792
793 if (sd->type == T_OPTICAL)
794 strncpy(lp->d_typename, "SCSI optical", 16);
795 else
796 strncpy(lp->d_typename, "SCSI disk", 16);
797 lp->d_type = DTYPE_SCSI;
798 strncpy(lp->d_packname, "fictitious", 16);
799 lp->d_secperunit = sd->params.disksize;
800 lp->d_rpm = sd->params.rot_rate;
801 lp->d_interleave = 1;
802 lp->d_flags = 0;
803
804 lp->d_partitions[RAW_PART].p_offset = 0;
805 lp->d_partitions[RAW_PART].p_size =
806 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
807 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
808 lp->d_npartitions = RAW_PART + 1;
809
810 lp->d_magic = DISKMAGIC;
811 lp->d_magic2 = DISKMAGIC;
812 lp->d_checksum = dkcksum(lp);
813 }
814
815
816 /*
817 * Load the label information on the named device
818 */
819 void
820 sdgetdisklabel(sd)
821 struct sd_softc *sd;
822 {
823 struct disklabel *lp = sd->sc_dk.dk_label;
824 char *errstring;
825
826 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
827
828 sdgetdefaultlabel(sd, lp);
829
830 if (lp->d_secpercyl == 0) {
831 lp->d_secpercyl = 100;
832 /* as long as it's not 0 - readdisklabel divides by it (?) */
833 }
834
835 /*
836 * Call the generic disklabel extraction routine
837 */
838 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
839 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
840 if (errstring) {
841 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
842 return;
843 }
844 }
845
846 /*
847 * Tell the device to map out a defective block
848 */
849 int
850 sd_reassign_blocks(sd, blkno)
851 struct sd_softc *sd;
852 u_long blkno;
853 {
854 struct scsi_reassign_blocks scsipi_cmd;
855 struct scsi_reassign_blocks_data rbdata;
856
857 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
858 bzero(&rbdata, sizeof(rbdata));
859 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
860
861 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
862 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
863
864 return (scsipi_command(sd->sc_link,
865 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
866 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
867 SCSI_DATA_OUT));
868 }
869
870 /*
871 * Check Errors
872 */
873 int
874 sd_interpret_sense(xs)
875 struct scsipi_xfer *xs;
876 {
877 struct scsipi_link *sc_link = xs->sc_link;
878 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
879 struct sd_softc *sd = sc_link->device_softc;
880 int retval = SCSIRET_CONTINUE;
881
882 /*
883 * If the device is not open yet, let the generic code handle it.
884 */
885 if ((sc_link->flags & SDEV_OPEN) == 0) {
886 return (retval);
887 }
888
889 /*
890 * If it isn't a extended or extended/deferred error, let
891 * the generic code handle it.
892 */
893 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
894 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
895 return (retval);
896 }
897
898 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
899 sense->add_sense_code == 0x4) {
900 if (sense->add_sense_code_qual == 0x01) {
901 printf("%s: ..is spinning up...waiting\n",
902 sd->sc_dev.dv_xname);
903 /*
904 * I really need a sdrestart function I can call here.
905 */
906 delay(1000000 * 5); /* 5 seconds */
907 retval = SCSIRET_RETRY;
908 } else if ((sense->add_sense_code_qual == 0x2) &&
909 (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
910 if (sd->sc_link->flags & SDEV_REMOVABLE) {
911 printf("%s: removable disk stopped- not "
912 "restarting\n", sd->sc_dev.dv_xname);
913 retval = EIO;
914 } else {
915 printf("%s: respinning up disk\n",
916 sd->sc_dev.dv_xname);
917 retval = scsipi_start(sd->sc_link, SSS_START,
918 SCSI_URGENT | SCSI_NOSLEEP | SCSI_POLL);
919 if (retval != 0) {
920 printf("%s: respin of disk failed-%d\n",
921 sd->sc_dev.dv_xname, retval);
922 retval = EIO;
923 } else {
924 retval = SCSIRET_RETRY;
925 }
926 }
927 }
928 }
929 return (retval);
930 }
931
932
933 int
934 sdsize(dev)
935 dev_t dev;
936 {
937 struct sd_softc *sd;
938 int part, unit, omask;
939 int size;
940
941 unit = SDUNIT(dev);
942 if (unit >= sd_cd.cd_ndevs)
943 return (-1);
944 sd = sd_cd.cd_devs[unit];
945 if (sd == NULL)
946 return (-1);
947
948 part = SDPART(dev);
949 omask = sd->sc_dk.dk_openmask & (1 << part);
950
951 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
952 return (-1);
953 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
954 size = -1;
955 else
956 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
957 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
958 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
959 return (-1);
960 return (size);
961 }
962
963 #ifndef __BDEVSW_DUMP_OLD_TYPE
964 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
965 static struct scsipi_xfer sx;
966 static int sddoingadump;
967
968 /*
969 * dump all of physical memory into the partition specified, starting
970 * at offset 'dumplo' into the partition.
971 */
972 int
973 sddump(dev, blkno, va, size)
974 dev_t dev;
975 daddr_t blkno;
976 caddr_t va;
977 size_t size;
978 {
979 struct sd_softc *sd; /* disk unit to do the I/O */
980 struct disklabel *lp; /* disk's disklabel */
981 int unit, part;
982 int sectorsize; /* size of a disk sector */
983 int nsects; /* number of sectors in partition */
984 int sectoff; /* sector offset of partition */
985 int totwrt; /* total number of sectors left to write */
986 int nwrt; /* current number of sectors to write */
987 struct scsipi_rw_big cmd; /* write command */
988 struct scsipi_xfer *xs; /* ... convenience */
989 int retval;
990
991 /* Check if recursive dump; if so, punt. */
992 if (sddoingadump)
993 return (EFAULT);
994
995 /* Mark as active early. */
996 sddoingadump = 1;
997
998 unit = SDUNIT(dev); /* Decompose unit & partition. */
999 part = SDPART(dev);
1000
1001 /* Check for acceptable drive number. */
1002 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1003 return (ENXIO);
1004
1005 /*
1006 * XXX Can't do this check, since the media might have been
1007 * XXX marked `invalid' by successful unmounting of all
1008 * XXX filesystems.
1009 */
1010 #if 0
1011 /* Make sure it was initialized. */
1012 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1013 return (ENXIO);
1014 #endif
1015
1016 /* Convert to disk sectors. Request must be a multiple of size. */
1017 lp = sd->sc_dk.dk_label;
1018 sectorsize = lp->d_secsize;
1019 if ((size % sectorsize) != 0)
1020 return (EFAULT);
1021 totwrt = size / sectorsize;
1022 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1023
1024 nsects = lp->d_partitions[part].p_size;
1025 sectoff = lp->d_partitions[part].p_offset;
1026
1027 /* Check transfer bounds against partition size. */
1028 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1029 return (EINVAL);
1030
1031 /* Offset block number to start of partition. */
1032 blkno += sectoff;
1033
1034 xs = &sx;
1035
1036 while (totwrt > 0) {
1037 nwrt = totwrt; /* XXX */
1038 #ifndef SD_DUMP_NOT_TRUSTED
1039 /*
1040 * Fill out the scsi command
1041 */
1042 bzero(&cmd, sizeof(cmd));
1043 cmd.opcode = WRITE_BIG;
1044 _lto4b(blkno, cmd.addr);
1045 _lto2b(nwrt, cmd.length);
1046 /*
1047 * Fill out the scsipi_xfer structure
1048 * Note: we cannot sleep as we may be an interrupt
1049 * don't use scsipi_command() as it may want to wait
1050 * for an xs.
1051 */
1052 bzero(xs, sizeof(sx));
1053 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1054 xs->sc_link = sd->sc_link;
1055 xs->retries = SDRETRIES;
1056 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1057 xs->cmd = (struct scsipi_generic *)&cmd;
1058 xs->cmdlen = sizeof(cmd);
1059 xs->resid = nwrt * sectorsize;
1060 xs->error = XS_NOERROR;
1061 xs->bp = 0;
1062 xs->data = va;
1063 xs->datalen = nwrt * sectorsize;
1064
1065 /*
1066 * Pass all this info to the scsi driver.
1067 */
1068 retval = scsipi_command_direct(xs);
1069 if (retval != COMPLETE)
1070 return (ENXIO);
1071 #else /* SD_DUMP_NOT_TRUSTED */
1072 /* Let's just talk about this first... */
1073 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1074 delay(500 * 1000); /* half a second */
1075 #endif /* SD_DUMP_NOT_TRUSTED */
1076
1077 /* update block count */
1078 totwrt -= nwrt;
1079 blkno += nwrt;
1080 va += sectorsize * nwrt;
1081 }
1082 sddoingadump = 0;
1083 return (0);
1084 }
1085 #else /* __BDEVSW_DUMP_NEW_TYPE */
1086 int
1087 sddump(dev, blkno, va, size)
1088 dev_t dev;
1089 daddr_t blkno;
1090 caddr_t va;
1091 size_t size;
1092 {
1093
1094 /* Not implemented. */
1095 return (ENXIO);
1096 }
1097 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1098