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