sd.c revision 1.98 1 /* $NetBSD: sd.c,v 1.98 1996/05/05 19:52:53 christos Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 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 <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/file.h>
54 #include <sys/stat.h>
55 #include <sys/ioctl.h>
56 #include <sys/buf.h>
57 #include <sys/uio.h>
58 #include <sys/malloc.h>
59 #include <sys/errno.h>
60 #include <sys/device.h>
61 #include <sys/disklabel.h>
62 #include <sys/disk.h>
63 #include <sys/proc.h>
64 #include <sys/conf.h>
65
66 #include <scsi/scsi_all.h>
67 #include <scsi/scsi_disk.h>
68 #include <scsi/scsiconf.h>
69
70 #define SDOUTSTANDING 4
71 #define SDRETRIES 4
72
73 #define SDUNIT(dev) DISKUNIT(dev)
74 #define SDPART(dev) DISKPART(dev)
75 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
76
77 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
78
79 struct sd_softc {
80 struct device sc_dev;
81 struct disk sc_dk;
82
83 int flags;
84 #define SDF_LOCKED 0x01
85 #define SDF_WANTED 0x02
86 #define SDF_WLABEL 0x04 /* label is writable */
87 #define SDF_LABELLING 0x08 /* writing label */
88 #define SDF_ANCIENT 0x10 /* disk is ancient; for minphys */
89 struct scsi_link *sc_link; /* contains our targ, lun, etc. */
90 struct disk_parms {
91 u_char heads; /* number of heads */
92 u_short cyls; /* number of cylinders */
93 u_char sectors; /* number of sectors/track */
94 int blksize; /* number of bytes/sector */
95 u_long disksize; /* total number sectors */
96 } params;
97 struct buf buf_queue;
98 };
99
100 int sdmatch __P((struct device *, void *, void *));
101 void sdattach __P((struct device *, struct device *, void *));
102 int sdlock __P((struct sd_softc *));
103 void sdunlock __P((struct sd_softc *));
104 void sdminphys __P((struct buf *));
105 void sdgetdisklabel __P((struct sd_softc *));
106 void sdstart __P((void *));
107 int sddone __P((struct scsi_xfer *, int));
108 int sd_reassign_blocks __P((struct sd_softc *, u_long));
109 int sd_get_parms __P((struct sd_softc *, int));
110
111 struct cfattach sd_ca = {
112 sizeof(struct sd_softc), sdmatch, sdattach
113 };
114
115 struct cfdriver sd_cd = {
116 NULL, "sd", DV_DISK
117 };
118
119 struct dkdriver sddkdriver = { sdstrategy };
120
121 struct scsi_device sd_switch = {
122 NULL, /* Use default error handler */
123 sdstart, /* have a queue, served by this */
124 NULL, /* have no async handler */
125 sddone, /* deal with stats at interrupt time */
126 };
127
128 struct scsi_inquiry_pattern sd_patterns[] = {
129 {T_DIRECT, T_FIXED,
130 "", "", ""},
131 {T_DIRECT, T_REMOV,
132 "", "", ""},
133 {T_OPTICAL, T_FIXED,
134 "", "", ""},
135 {T_OPTICAL, T_REMOV,
136 "", "", ""},
137 };
138
139 int
140 sdmatch(parent, match, aux)
141 struct device *parent;
142 void *match, *aux;
143 {
144 struct scsibus_attach_args *sa = aux;
145 int priority;
146
147 (void)scsi_inqmatch(sa->sa_inqbuf,
148 (caddr_t)sd_patterns, sizeof(sd_patterns)/sizeof(sd_patterns[0]),
149 sizeof(sd_patterns[0]), &priority);
150 return (priority);
151 }
152
153 /*
154 * The routine called by the low level scsi routine when it discovers
155 * a device suitable for this driver.
156 */
157 void
158 sdattach(parent, self, aux)
159 struct device *parent, *self;
160 void *aux;
161 {
162 struct sd_softc *sd = (void *)self;
163 struct disk_parms *dp = &sd->params;
164 struct scsibus_attach_args *sa = aux;
165 struct scsi_link *sc_link = sa->sa_sc_link;
166
167 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
168
169 /*
170 * Store information needed to contact our base driver
171 */
172 sd->sc_link = sc_link;
173 sc_link->device = &sd_switch;
174 sc_link->device_softc = sd;
175 if (sc_link->openings > SDOUTSTANDING)
176 sc_link->openings = SDOUTSTANDING;
177
178 /*
179 * Initialize and attach the disk structure.
180 */
181 sd->sc_dk.dk_driver = &sddkdriver;
182 sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
183 disk_attach(&sd->sc_dk);
184
185 #if !defined(i386)
186 dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */
187 #endif
188
189 /*
190 * Note if this device is ancient. This is used in sdminphys().
191 */
192 if ((sa->sa_inqbuf->version & SID_ANSII) == 0)
193 sd->flags |= SDF_ANCIENT;
194
195 /*
196 * Use the subdriver to request information regarding
197 * the drive. We cannot use interrupts yet, so the
198 * request must specify this.
199 */
200 printf("\n");
201 printf("%s: ", sd->sc_dev.dv_xname);
202 if (scsi_start(sd->sc_link, SSS_START,
203 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT) ||
204 sd_get_parms(sd, SCSI_AUTOCONF) != 0)
205 printf("drive offline\n");
206 else
207 printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
208 dp->disksize / (1048576 / dp->blksize), dp->cyls,
209 dp->heads, dp->sectors, dp->blksize);
210 }
211
212 /*
213 * Wait interruptibly for an exclusive lock.
214 *
215 * XXX
216 * Several drivers do this; it should be abstracted and made MP-safe.
217 */
218 int
219 sdlock(sd)
220 struct sd_softc *sd;
221 {
222 int error;
223
224 while ((sd->flags & SDF_LOCKED) != 0) {
225 sd->flags |= SDF_WANTED;
226 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
227 return error;
228 }
229 sd->flags |= SDF_LOCKED;
230 return 0;
231 }
232
233 /*
234 * Unlock and wake up any waiters.
235 */
236 void
237 sdunlock(sd)
238 struct sd_softc *sd;
239 {
240
241 sd->flags &= ~SDF_LOCKED;
242 if ((sd->flags & SDF_WANTED) != 0) {
243 sd->flags &= ~SDF_WANTED;
244 wakeup(sd);
245 }
246 }
247
248 /*
249 * open the device. Make sure the partition info is a up-to-date as can be.
250 */
251 int
252 sdopen(dev, flag, fmt, p)
253 dev_t dev;
254 int flag, fmt;
255 struct proc *p;
256 {
257 struct sd_softc *sd;
258 struct scsi_link *sc_link;
259 int unit, part;
260 int error;
261
262 unit = SDUNIT(dev);
263 if (unit >= sd_cd.cd_ndevs)
264 return ENXIO;
265 sd = sd_cd.cd_devs[unit];
266 if (!sd)
267 return ENXIO;
268
269 sc_link = sd->sc_link;
270
271 SC_DEBUG(sc_link, SDEV_DB1,
272 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
273 sd_cd.cd_ndevs, SDPART(dev)));
274
275 if ((error = sdlock(sd)) != 0)
276 return error;
277
278 if (sd->sc_dk.dk_openmask != 0) {
279 /*
280 * If any partition is open, but the disk has been invalidated,
281 * disallow further opens.
282 */
283 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
284 error = EIO;
285 goto bad3;
286 }
287 } else {
288 /* Check that it is still responding and ok. */
289 error = scsi_test_unit_ready(sc_link,
290 SCSI_IGNORE_ILLEGAL_REQUEST |
291 SCSI_IGNORE_MEDIA_CHANGE |
292 SCSI_IGNORE_NOT_READY);
293 if (error)
294 goto bad3;
295
296 /* Start the pack spinning if necessary. */
297 error = scsi_start(sc_link, SSS_START,
298 SCSI_IGNORE_ILLEGAL_REQUEST |
299 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
300 if (error)
301 goto bad3;
302
303 sc_link->flags |= SDEV_OPEN;
304
305 /* Lock the pack in. */
306 error = scsi_prevent(sc_link, PR_PREVENT,
307 SCSI_IGNORE_ILLEGAL_REQUEST |
308 SCSI_IGNORE_MEDIA_CHANGE);
309 if (error)
310 goto bad;
311
312 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
313 sc_link->flags |= SDEV_MEDIA_LOADED;
314
315 /* Load the physical device parameters. */
316 if (sd_get_parms(sd, 0) != 0) {
317 error = ENXIO;
318 goto bad2;
319 }
320 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
321
322 /* Load the partition info if not already loaded. */
323 sdgetdisklabel(sd);
324 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
325 }
326 }
327
328 part = SDPART(dev);
329
330 /* Check that the partition exists. */
331 if (part != RAW_PART &&
332 (part >= sd->sc_dk.dk_label->d_npartitions ||
333 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
334 error = ENXIO;
335 goto bad;
336 }
337
338 /* Insure only one open at a time. */
339 switch (fmt) {
340 case S_IFCHR:
341 sd->sc_dk.dk_copenmask |= (1 << part);
342 break;
343 case S_IFBLK:
344 sd->sc_dk.dk_bopenmask |= (1 << part);
345 break;
346 }
347 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
348
349 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
350 sdunlock(sd);
351 return 0;
352
353 bad2:
354 sc_link->flags &= ~SDEV_MEDIA_LOADED;
355
356 bad:
357 if (sd->sc_dk.dk_openmask == 0) {
358 scsi_prevent(sc_link, PR_ALLOW,
359 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
360 sc_link->flags &= ~SDEV_OPEN;
361 }
362
363 bad3:
364 sdunlock(sd);
365 return error;
366 }
367
368 /*
369 * close the device.. only called if we are the LAST occurence of an open
370 * device. Convenient now but usually a pain.
371 */
372 int
373 sdclose(dev, flag, fmt, p)
374 dev_t dev;
375 int flag, fmt;
376 struct proc *p;
377 {
378 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
379 int part = SDPART(dev);
380 int error;
381
382 if ((error = sdlock(sd)) != 0)
383 return error;
384
385 switch (fmt) {
386 case S_IFCHR:
387 sd->sc_dk.dk_copenmask &= ~(1 << part);
388 break;
389 case S_IFBLK:
390 sd->sc_dk.dk_bopenmask &= ~(1 << part);
391 break;
392 }
393 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
394
395 if (sd->sc_dk.dk_openmask == 0) {
396 /* XXXX Must wait for I/O to complete! */
397
398 scsi_prevent(sd->sc_link, PR_ALLOW,
399 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
400 sd->sc_link->flags &= ~SDEV_OPEN;
401 }
402
403 sdunlock(sd);
404 return 0;
405 }
406
407 /*
408 * Actually translate the requested transfer into one the physical driver
409 * can understand. The transfer is described by a buf and will include
410 * only one physical transfer.
411 */
412 void
413 sdstrategy(bp)
414 struct buf *bp;
415 {
416 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
417 int s;
418
419 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
420 SC_DEBUG(sd->sc_link, SDEV_DB1,
421 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
422 /*
423 * The transfer must be a whole number of blocks.
424 */
425 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) {
426 bp->b_error = EINVAL;
427 goto bad;
428 }
429 /*
430 * If the device has been made invalid, error out
431 */
432 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
433 bp->b_error = EIO;
434 goto bad;
435 }
436 /*
437 * If it's a null transfer, return immediatly
438 */
439 if (bp->b_bcount == 0)
440 goto done;
441
442 /*
443 * Do bounds checking, adjust transfer. if error, process.
444 * If end of partition, just return.
445 */
446 if (SDPART(bp->b_dev) != RAW_PART &&
447 bounds_check_with_label(bp, sd->sc_dk.dk_label,
448 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
449 goto done;
450
451 s = splbio();
452
453 /*
454 * Place it in the queue of disk activities for this disk
455 */
456 disksort(&sd->buf_queue, bp);
457
458 /*
459 * Tell the device to get going on the transfer if it's
460 * not doing anything, otherwise just wait for completion
461 */
462 sdstart(sd);
463
464 splx(s);
465 return;
466
467 bad:
468 bp->b_flags |= B_ERROR;
469 done:
470 /*
471 * Correctly set the buf to indicate a completed xfer
472 */
473 bp->b_resid = bp->b_bcount;
474 biodone(bp);
475 }
476
477 /*
478 * sdstart looks to see if there is a buf waiting for the device
479 * and that the device is not already busy. If both are true,
480 * It dequeues the buf and creates a scsi command to perform the
481 * transfer in the buf. The transfer request will call scsi_done
482 * on completion, which will in turn call this routine again
483 * so that the next queued transfer is performed.
484 * The bufs are queued by the strategy routine (sdstrategy)
485 *
486 * This routine is also called after other non-queued requests
487 * have been made of the scsi driver, to ensure that the queue
488 * continues to be drained.
489 *
490 * must be called at the correct (highish) spl level
491 * sdstart() is called at splbio from sdstrategy and scsi_done
492 */
493 void
494 sdstart(v)
495 register void *v;
496 {
497 register struct sd_softc *sd = v;
498 register struct scsi_link *sc_link = sd->sc_link;
499 struct buf *bp = 0;
500 struct buf *dp;
501 struct scsi_rw_big cmd_big;
502 struct scsi_rw cmd_small;
503 struct scsi_generic *cmdp;
504 int blkno, nblks, cmdlen;
505 struct partition *p;
506
507 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
508 /*
509 * Check if the device has room for another command
510 */
511 while (sc_link->openings > 0) {
512 /*
513 * there is excess capacity, but a special waits
514 * It'll need the adapter as soon as we clear out of the
515 * way and let it run (user level wait).
516 */
517 if (sc_link->flags & SDEV_WAITING) {
518 sc_link->flags &= ~SDEV_WAITING;
519 wakeup((caddr_t)sc_link);
520 return;
521 }
522
523 /*
524 * See if there is a buf with work for us to do..
525 */
526 dp = &sd->buf_queue;
527 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
528 return;
529 dp->b_actf = bp->b_actf;
530
531 /*
532 * If the device has become invalid, abort all the
533 * reads and writes until all files have been closed and
534 * re-opened
535 */
536 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
537 bp->b_error = EIO;
538 bp->b_flags |= B_ERROR;
539 biodone(bp);
540 continue;
541 }
542
543 /*
544 * We have a buf, now we should make a command
545 *
546 * First, translate the block to absolute and put it in terms
547 * of the logical blocksize of the device.
548 */
549 blkno =
550 bp->b_blkno / (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
551 if (SDPART(bp->b_dev) != RAW_PART) {
552 p = &sd->sc_dk.dk_label->d_partitions[SDPART(bp->b_dev)];
553 blkno += p->p_offset;
554 }
555 nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize);
556
557 /*
558 * Fill out the scsi command. If the transfer will
559 * fit in a "small" cdb, use it.
560 */
561 if (((blkno & 0x1fffff) == blkno) &&
562 ((nblks & 0xff) == nblks)) {
563 /*
564 * We can fit in a small cdb.
565 */
566 bzero(&cmd_small, sizeof(cmd_small));
567 cmd_small.opcode = (bp->b_flags & B_READ) ?
568 READ_COMMAND : WRITE_COMMAND;
569 _lto3b(blkno, cmd_small.addr);
570 cmd_small.length = nblks & 0xff;
571 cmdlen = sizeof(cmd_small);
572 cmdp = (struct scsi_generic *)&cmd_small;
573 } else {
574 /*
575 * Need a large cdb.
576 */
577 bzero(&cmd_big, sizeof(cmd_big));
578 cmd_big.opcode = (bp->b_flags & B_READ) ?
579 READ_BIG : WRITE_BIG;
580 _lto4b(blkno, cmd_big.addr);
581 _lto2b(nblks, cmd_big.length);
582 cmdlen = sizeof(cmd_big);
583 cmdp = (struct scsi_generic *)&cmd_big;
584 }
585
586 /* Instrumentation. */
587 disk_busy(&sd->sc_dk);
588
589 /*
590 * Call the routine that chats with the adapter.
591 * Note: we cannot sleep as we may be an interrupt
592 */
593 if (scsi_scsi_cmd(sc_link, cmdp, cmdlen,
594 (u_char *)bp->b_data, bp->b_bcount,
595 SDRETRIES, 10000, bp, SCSI_NOSLEEP |
596 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT)))
597 printf("%s: not queued", sd->sc_dev.dv_xname);
598 }
599 }
600
601 int
602 sddone(xs, complete)
603 struct scsi_xfer *xs;
604 int complete;
605 {
606 struct sd_softc *sd = xs->sc_link->device_softc;
607
608 if (complete && (xs->bp != NULL))
609 disk_unbusy(&sd->sc_dk, (xs->bp->b_bcount - xs->bp->b_resid));
610
611 return (0);
612 }
613
614 void
615 sdminphys(bp)
616 struct buf *bp;
617 {
618 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
619 long max;
620
621 /*
622 * If the device is ancient, we want to make sure that
623 * the transfer fits into a 6-byte cdb.
624 *
625 * XXX Note that the SCSI-I spec says that 256-block transfers
626 * are allowed in a 6-byte read/write, and are specified
627 * by settng the "length" to 0. However, we're conservative
628 * here, allowing only 255-block transfers in case an
629 * ancient device gets confused by length == 0. A length of 0
630 * in a 10-byte read/write actually means 0 blocks.
631 */
632 if (sd->flags & SDF_ANCIENT) {
633 max = sd->sc_dk.dk_label->d_secsize * 0xff;
634
635 if (bp->b_bcount > max)
636 bp->b_bcount = max;
637 }
638
639 (*sd->sc_link->adapter->scsi_minphys)(bp);
640 }
641
642 int
643 sdread(dev, uio, ioflag)
644 dev_t dev;
645 struct uio *uio;
646 int ioflag;
647 {
648
649 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
650 }
651
652 int
653 sdwrite(dev, uio, ioflag)
654 dev_t dev;
655 struct uio *uio;
656 int ioflag;
657 {
658
659 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
660 }
661
662 /*
663 * Perform special action on behalf of the user
664 * Knows about the internals of this device
665 */
666 int
667 sdioctl(dev, cmd, addr, flag, p)
668 dev_t dev;
669 u_long cmd;
670 caddr_t addr;
671 int flag;
672 struct proc *p;
673 {
674 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
675 int error;
676
677 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
678
679 /*
680 * If the device is not valid.. abandon ship
681 */
682 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
683 return EIO;
684
685 switch (cmd) {
686 case DIOCGDINFO:
687 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
688 return 0;
689
690 case DIOCGPART:
691 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
692 ((struct partinfo *)addr)->part =
693 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
694 return 0;
695
696 case DIOCWDINFO:
697 case DIOCSDINFO:
698 if ((flag & FWRITE) == 0)
699 return EBADF;
700
701 if ((error = sdlock(sd)) != 0)
702 return error;
703 sd->flags |= SDF_LABELLING;
704
705 error = setdisklabel(sd->sc_dk.dk_label,
706 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
707 sd->sc_dk.dk_cpulabel);
708 if (error == 0) {
709 if (cmd == DIOCWDINFO)
710 error = writedisklabel(SDLABELDEV(dev),
711 sdstrategy, sd->sc_dk.dk_label,
712 sd->sc_dk.dk_cpulabel);
713 }
714
715 sd->flags &= ~SDF_LABELLING;
716 sdunlock(sd);
717 return error;
718
719 case DIOCWLABEL:
720 if ((flag & FWRITE) == 0)
721 return EBADF;
722 if (*(int *)addr)
723 sd->flags |= SDF_WLABEL;
724 else
725 sd->flags &= ~SDF_WLABEL;
726 return 0;
727
728 case DIOCLOCK:
729 return scsi_prevent(sd->sc_link,
730 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
731
732 case DIOCEJECT:
733 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
734 scsi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
735
736 default:
737 if (SDPART(dev) != RAW_PART)
738 return ENOTTY;
739 return scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p);
740 }
741
742 #ifdef DIAGNOSTIC
743 panic("sdioctl: impossible");
744 #endif
745 }
746
747 /*
748 * Load the label information on the named device
749 */
750 void
751 sdgetdisklabel(sd)
752 struct sd_softc *sd;
753 {
754 struct disklabel *lp = sd->sc_dk.dk_label;
755 char *errstring;
756
757 bzero(lp, sizeof(struct disklabel));
758 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
759
760 lp->d_secsize = sd->params.blksize;
761 lp->d_ntracks = sd->params.heads;
762 lp->d_nsectors = sd->params.sectors;
763 lp->d_ncylinders = sd->params.cyls;
764 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
765 if (lp->d_secpercyl == 0) {
766 lp->d_secpercyl = 100;
767 /* as long as it's not 0 - readdisklabel divides by it (?) */
768 }
769
770 strncpy(lp->d_typename, "SCSI disk", 16);
771 lp->d_type = DTYPE_SCSI;
772 strncpy(lp->d_packname, "fictitious", 16);
773 lp->d_secperunit = sd->params.disksize;
774 lp->d_rpm = 3600;
775 lp->d_interleave = 1;
776 lp->d_flags = 0;
777
778 lp->d_partitions[RAW_PART].p_offset = 0;
779 lp->d_partitions[RAW_PART].p_size =
780 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
781 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
782 lp->d_npartitions = RAW_PART + 1;
783
784 lp->d_magic = DISKMAGIC;
785 lp->d_magic2 = DISKMAGIC;
786 lp->d_checksum = dkcksum(lp);
787
788 /*
789 * Call the generic disklabel extraction routine
790 */
791 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
792 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
793 if (errstring) {
794 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
795 return;
796 }
797 }
798
799 /*
800 * Tell the device to map out a defective block
801 */
802 int
803 sd_reassign_blocks(sd, blkno)
804 struct sd_softc *sd;
805 u_long blkno;
806 {
807 struct scsi_reassign_blocks scsi_cmd;
808 struct scsi_reassign_blocks_data rbdata;
809
810 bzero(&scsi_cmd, sizeof(scsi_cmd));
811 bzero(&rbdata, sizeof(rbdata));
812 scsi_cmd.opcode = REASSIGN_BLOCKS;
813
814 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
815 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
816
817 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
818 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES,
819 5000, NULL, SCSI_DATA_OUT);
820 }
821
822 /*
823 * Get the scsi driver to send a full inquiry to the * device and use the
824 * results to fill out the disk parameter structure.
825 */
826 int
827 sd_get_parms(sd, flags)
828 struct sd_softc *sd;
829 int flags;
830 {
831 struct disk_parms *dp = &sd->params;
832 struct scsi_mode_sense scsi_cmd;
833 struct scsi_mode_sense_data {
834 struct scsi_mode_header header;
835 struct scsi_blk_desc blk_desc;
836 union disk_pages pages;
837 } scsi_sense;
838 u_long sectors;
839
840 /*
841 * do a "mode sense page 4"
842 */
843 bzero(&scsi_cmd, sizeof(scsi_cmd));
844 scsi_cmd.opcode = MODE_SENSE;
845 scsi_cmd.page = 4;
846 scsi_cmd.length = 0x20;
847 /*
848 * If the command worked, use the results to fill out
849 * the parameter structure
850 */
851 if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
852 sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
853 SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN) != 0) {
854 printf("%s: could not mode sense (4)", sd->sc_dev.dv_xname);
855 fake_it:
856 printf("; using fictitious geometry\n");
857 /*
858 * use adaptec standard fictitious geometry
859 * this depends on which controller (e.g. 1542C is
860 * different. but we have to put SOMETHING here..)
861 */
862 sectors = scsi_size(sd->sc_link, flags);
863 dp->heads = 64;
864 dp->sectors = 32;
865 dp->cyls = sectors / (64 * 32);
866 dp->blksize = 512;
867 dp->disksize = sectors;
868 } else {
869 SC_DEBUG(sd->sc_link, SDEV_DB3,
870 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
871 _3btol(scsi_sense.pages.rigid_geometry.ncyl),
872 scsi_sense.pages.rigid_geometry.nheads,
873 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
874 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
875 _2btol(scsi_sense.pages.rigid_geometry.land_zone)));
876
877 /*
878 * KLUDGE!! (for zone recorded disks)
879 * give a number of sectors so that sec * trks * cyls
880 * is <= disk_size
881 * can lead to wasted space! THINK ABOUT THIS !
882 */
883 dp->heads = scsi_sense.pages.rigid_geometry.nheads;
884 dp->cyls = _3btol(scsi_sense.pages.rigid_geometry.ncyl);
885 dp->blksize = _3btol(scsi_sense.blk_desc.blklen);
886
887 if (dp->heads == 0 || dp->cyls == 0) {
888 printf("%s: mode sense (4) returned nonsense",
889 sd->sc_dev.dv_xname);
890 goto fake_it;
891 }
892
893 if (dp->blksize == 0)
894 dp->blksize = 512;
895
896 sectors = scsi_size(sd->sc_link, flags);
897 dp->disksize = sectors;
898 sectors /= (dp->heads * dp->cyls);
899 dp->sectors = sectors; /* XXX dubious on SCSI */
900 }
901
902 return 0;
903 }
904
905 int
906 sdsize(dev)
907 dev_t dev;
908 {
909 struct sd_softc *sd;
910 int part;
911 int size;
912
913 if (sdopen(dev, 0, S_IFBLK, NULL) != 0)
914 return -1;
915 sd = sd_cd.cd_devs[SDUNIT(dev)];
916 part = SDPART(dev);
917 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
918 size = -1;
919 else
920 size = sd->sc_dk.dk_label->d_partitions[part].p_size;
921 if (sdclose(dev, 0, S_IFBLK, NULL) != 0)
922 return -1;
923 return size;
924 }
925
926 #ifndef __BDEVSW_DUMP_OLD_TYPE
927 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
928 static struct scsi_xfer sx;
929 static int sddoingadump;
930
931 /*
932 * dump all of physical memory into the partition specified, starting
933 * at offset 'dumplo' into the partition.
934 */
935 int
936 sddump(dev, blkno, va, size)
937 dev_t dev;
938 daddr_t blkno;
939 caddr_t va;
940 size_t size;
941 {
942 struct sd_softc *sd; /* disk unit to do the I/O */
943 struct disklabel *lp; /* disk's disklabel */
944 int unit, part;
945 int sectorsize; /* size of a disk sector */
946 int nsects; /* number of sectors in partition */
947 int sectoff; /* sector offset of partition */
948 int totwrt; /* total number of sectors left to write */
949 int nwrt; /* current number of sectors to write */
950 struct scsi_rw_big cmd; /* write command */
951 struct scsi_xfer *xs; /* ... convenience */
952 int retval;
953
954 /* Check if recursive dump; if so, punt. */
955 if (sddoingadump)
956 return EFAULT;
957
958 /* Mark as active early. */
959 sddoingadump = 1;
960
961 unit = SDUNIT(dev); /* Decompose unit & partition. */
962 part = SDPART(dev);
963
964 /* Check for acceptable drive number. */
965 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
966 return ENXIO;
967
968 /* Make sure it was initialized. */
969 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
970 return ENXIO;
971
972 /* Convert to disk sectors. Request must be a multiple of size. */
973 lp = sd->sc_dk.dk_label;
974 sectorsize = lp->d_secsize;
975 if ((size % sectorsize) != 0)
976 return EFAULT;
977 totwrt = size / sectorsize;
978 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
979
980 nsects = lp->d_partitions[part].p_size;
981 sectoff = lp->d_partitions[part].p_offset;
982
983 /* Check transfer bounds against partition size. */
984 if ((blkno < 0) || ((blkno + totwrt) > nsects))
985 return EINVAL;
986
987 /* Offset block number to start of partition. */
988 blkno += sectoff;
989
990 xs = &sx;
991
992 while (totwrt > 0) {
993 nwrt = totwrt; /* XXX */
994 #ifndef SD_DUMP_NOT_TRUSTED
995 /*
996 * Fill out the scsi command
997 */
998 bzero(&cmd, sizeof(cmd));
999 cmd.opcode = WRITE_BIG;
1000 _lto4b(blkno, cmd.addr);
1001 _lto2b(nwrt, cmd.length);
1002 /*
1003 * Fill out the scsi_xfer structure
1004 * Note: we cannot sleep as we may be an interrupt
1005 * don't use scsi_scsi_cmd() as it may want
1006 * to wait for an xs.
1007 */
1008 bzero(xs, sizeof(sx));
1009 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1010 xs->sc_link = sd->sc_link;
1011 xs->retries = SDRETRIES;
1012 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1013 xs->cmd = (struct scsi_generic *)&cmd;
1014 xs->cmdlen = sizeof(cmd);
1015 xs->resid = nwrt * sectorsize;
1016 xs->error = XS_NOERROR;
1017 xs->bp = 0;
1018 xs->data = va;
1019 xs->datalen = nwrt * sectorsize;
1020
1021 /*
1022 * Pass all this info to the scsi driver.
1023 */
1024 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
1025 if (retval != COMPLETE)
1026 return ENXIO;
1027 #else /* SD_DUMP_NOT_TRUSTED */
1028 /* Let's just talk about this first... */
1029 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1030 delay(500 * 1000); /* half a second */
1031 #endif /* SD_DUMP_NOT_TRUSTED */
1032
1033 /* update block count */
1034 totwrt -= nwrt;
1035 blkno += nwrt;
1036 va += sectorsize * nwrt;
1037 }
1038 sddoingadump = 0;
1039 return 0;
1040 }
1041 #else /* __BDEVSW_DUMP_NEW_TYPE */
1042 int
1043 sddump(dev, blkno, va, size)
1044 dev_t dev;
1045 daddr_t blkno;
1046 caddr_t va;
1047 size_t size;
1048 {
1049
1050 /* Not implemented. */
1051 return ENXIO;
1052 }
1053 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1054