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