sd.c revision 1.99 1 /* $NetBSD: sd.c,v 1.99 1996/05/14 00:10:37 thorpej 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, error;
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 error = 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 if (error)
598 printf("%s: not queued, error %d\n",
599 sd->sc_dev.dv_xname, error);
600 }
601 }
602
603 int
604 sddone(xs, complete)
605 struct scsi_xfer *xs;
606 int complete;
607 {
608 struct sd_softc *sd = xs->sc_link->device_softc;
609
610 if (complete && (xs->bp != NULL))
611 disk_unbusy(&sd->sc_dk, (xs->bp->b_bcount - xs->bp->b_resid));
612
613 return (0);
614 }
615
616 void
617 sdminphys(bp)
618 struct buf *bp;
619 {
620 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
621 long max;
622
623 /*
624 * If the device is ancient, we want to make sure that
625 * the transfer fits into a 6-byte cdb.
626 *
627 * XXX Note that the SCSI-I spec says that 256-block transfers
628 * are allowed in a 6-byte read/write, and are specified
629 * by settng the "length" to 0. However, we're conservative
630 * here, allowing only 255-block transfers in case an
631 * ancient device gets confused by length == 0. A length of 0
632 * in a 10-byte read/write actually means 0 blocks.
633 */
634 if (sd->flags & SDF_ANCIENT) {
635 max = sd->sc_dk.dk_label->d_secsize * 0xff;
636
637 if (bp->b_bcount > max)
638 bp->b_bcount = max;
639 }
640
641 (*sd->sc_link->adapter->scsi_minphys)(bp);
642 }
643
644 int
645 sdread(dev, uio, ioflag)
646 dev_t dev;
647 struct uio *uio;
648 int ioflag;
649 {
650
651 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
652 }
653
654 int
655 sdwrite(dev, uio, ioflag)
656 dev_t dev;
657 struct uio *uio;
658 int ioflag;
659 {
660
661 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
662 }
663
664 /*
665 * Perform special action on behalf of the user
666 * Knows about the internals of this device
667 */
668 int
669 sdioctl(dev, cmd, addr, flag, p)
670 dev_t dev;
671 u_long cmd;
672 caddr_t addr;
673 int flag;
674 struct proc *p;
675 {
676 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
677 int error;
678
679 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
680
681 /*
682 * If the device is not valid.. abandon ship
683 */
684 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
685 return EIO;
686
687 switch (cmd) {
688 case DIOCGDINFO:
689 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
690 return 0;
691
692 case DIOCGPART:
693 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
694 ((struct partinfo *)addr)->part =
695 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
696 return 0;
697
698 case DIOCWDINFO:
699 case DIOCSDINFO:
700 if ((flag & FWRITE) == 0)
701 return EBADF;
702
703 if ((error = sdlock(sd)) != 0)
704 return error;
705 sd->flags |= SDF_LABELLING;
706
707 error = setdisklabel(sd->sc_dk.dk_label,
708 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
709 sd->sc_dk.dk_cpulabel);
710 if (error == 0) {
711 if (cmd == DIOCWDINFO)
712 error = writedisklabel(SDLABELDEV(dev),
713 sdstrategy, sd->sc_dk.dk_label,
714 sd->sc_dk.dk_cpulabel);
715 }
716
717 sd->flags &= ~SDF_LABELLING;
718 sdunlock(sd);
719 return error;
720
721 case DIOCWLABEL:
722 if ((flag & FWRITE) == 0)
723 return EBADF;
724 if (*(int *)addr)
725 sd->flags |= SDF_WLABEL;
726 else
727 sd->flags &= ~SDF_WLABEL;
728 return 0;
729
730 case DIOCLOCK:
731 return scsi_prevent(sd->sc_link,
732 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
733
734 case DIOCEJECT:
735 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
736 scsi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
737
738 default:
739 if (SDPART(dev) != RAW_PART)
740 return ENOTTY;
741 return scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p);
742 }
743
744 #ifdef DIAGNOSTIC
745 panic("sdioctl: impossible");
746 #endif
747 }
748
749 /*
750 * Load the label information on the named device
751 */
752 void
753 sdgetdisklabel(sd)
754 struct sd_softc *sd;
755 {
756 struct disklabel *lp = sd->sc_dk.dk_label;
757 char *errstring;
758
759 bzero(lp, sizeof(struct disklabel));
760 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
761
762 lp->d_secsize = sd->params.blksize;
763 lp->d_ntracks = sd->params.heads;
764 lp->d_nsectors = sd->params.sectors;
765 lp->d_ncylinders = sd->params.cyls;
766 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
767 if (lp->d_secpercyl == 0) {
768 lp->d_secpercyl = 100;
769 /* as long as it's not 0 - readdisklabel divides by it (?) */
770 }
771
772 strncpy(lp->d_typename, "SCSI disk", 16);
773 lp->d_type = DTYPE_SCSI;
774 strncpy(lp->d_packname, "fictitious", 16);
775 lp->d_secperunit = sd->params.disksize;
776 lp->d_rpm = 3600;
777 lp->d_interleave = 1;
778 lp->d_flags = 0;
779
780 lp->d_partitions[RAW_PART].p_offset = 0;
781 lp->d_partitions[RAW_PART].p_size =
782 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
783 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
784 lp->d_npartitions = RAW_PART + 1;
785
786 lp->d_magic = DISKMAGIC;
787 lp->d_magic2 = DISKMAGIC;
788 lp->d_checksum = dkcksum(lp);
789
790 /*
791 * Call the generic disklabel extraction routine
792 */
793 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
794 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
795 if (errstring) {
796 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
797 return;
798 }
799 }
800
801 /*
802 * Tell the device to map out a defective block
803 */
804 int
805 sd_reassign_blocks(sd, blkno)
806 struct sd_softc *sd;
807 u_long blkno;
808 {
809 struct scsi_reassign_blocks scsi_cmd;
810 struct scsi_reassign_blocks_data rbdata;
811
812 bzero(&scsi_cmd, sizeof(scsi_cmd));
813 bzero(&rbdata, sizeof(rbdata));
814 scsi_cmd.opcode = REASSIGN_BLOCKS;
815
816 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
817 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
818
819 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
820 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES,
821 5000, NULL, SCSI_DATA_OUT);
822 }
823
824 /*
825 * Get the scsi driver to send a full inquiry to the * device and use the
826 * results to fill out the disk parameter structure.
827 */
828 int
829 sd_get_parms(sd, flags)
830 struct sd_softc *sd;
831 int flags;
832 {
833 struct disk_parms *dp = &sd->params;
834 struct scsi_mode_sense scsi_cmd;
835 struct scsi_mode_sense_data {
836 struct scsi_mode_header header;
837 struct scsi_blk_desc blk_desc;
838 union disk_pages pages;
839 } scsi_sense;
840 u_long sectors;
841
842 /*
843 * do a "mode sense page 4"
844 */
845 bzero(&scsi_cmd, sizeof(scsi_cmd));
846 scsi_cmd.opcode = MODE_SENSE;
847 scsi_cmd.page = 4;
848 scsi_cmd.length = 0x20;
849 /*
850 * If the command worked, use the results to fill out
851 * the parameter structure
852 */
853 if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
854 sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
855 SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN) != 0) {
856 printf("%s: could not mode sense (4)", sd->sc_dev.dv_xname);
857 fake_it:
858 printf("; using fictitious geometry\n");
859 /*
860 * use adaptec standard fictitious geometry
861 * this depends on which controller (e.g. 1542C is
862 * different. but we have to put SOMETHING here..)
863 */
864 sectors = scsi_size(sd->sc_link, flags);
865 dp->heads = 64;
866 dp->sectors = 32;
867 dp->cyls = sectors / (64 * 32);
868 dp->blksize = 512;
869 dp->disksize = sectors;
870 } else {
871 SC_DEBUG(sd->sc_link, SDEV_DB3,
872 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
873 _3btol(scsi_sense.pages.rigid_geometry.ncyl),
874 scsi_sense.pages.rigid_geometry.nheads,
875 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
876 _2btol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
877 _2btol(scsi_sense.pages.rigid_geometry.land_zone)));
878
879 /*
880 * KLUDGE!! (for zone recorded disks)
881 * give a number of sectors so that sec * trks * cyls
882 * is <= disk_size
883 * can lead to wasted space! THINK ABOUT THIS !
884 */
885 dp->heads = scsi_sense.pages.rigid_geometry.nheads;
886 dp->cyls = _3btol(scsi_sense.pages.rigid_geometry.ncyl);
887 dp->blksize = _3btol(scsi_sense.blk_desc.blklen);
888
889 if (dp->heads == 0 || dp->cyls == 0) {
890 printf("%s: mode sense (4) returned nonsense",
891 sd->sc_dev.dv_xname);
892 goto fake_it;
893 }
894
895 if (dp->blksize == 0)
896 dp->blksize = 512;
897
898 sectors = scsi_size(sd->sc_link, flags);
899 dp->disksize = sectors;
900 sectors /= (dp->heads * dp->cyls);
901 dp->sectors = sectors; /* XXX dubious on SCSI */
902 }
903
904 return 0;
905 }
906
907 int
908 sdsize(dev)
909 dev_t dev;
910 {
911 struct sd_softc *sd;
912 int part;
913 int size;
914
915 if (sdopen(dev, 0, S_IFBLK, NULL) != 0)
916 return -1;
917 sd = sd_cd.cd_devs[SDUNIT(dev)];
918 part = SDPART(dev);
919 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
920 size = -1;
921 else
922 size = sd->sc_dk.dk_label->d_partitions[part].p_size;
923 if (sdclose(dev, 0, S_IFBLK, NULL) != 0)
924 return -1;
925 return size;
926 }
927
928 #ifndef __BDEVSW_DUMP_OLD_TYPE
929 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
930 static struct scsi_xfer sx;
931 static int sddoingadump;
932
933 /*
934 * dump all of physical memory into the partition specified, starting
935 * at offset 'dumplo' into the partition.
936 */
937 int
938 sddump(dev, blkno, va, size)
939 dev_t dev;
940 daddr_t blkno;
941 caddr_t va;
942 size_t size;
943 {
944 struct sd_softc *sd; /* disk unit to do the I/O */
945 struct disklabel *lp; /* disk's disklabel */
946 int unit, part;
947 int sectorsize; /* size of a disk sector */
948 int nsects; /* number of sectors in partition */
949 int sectoff; /* sector offset of partition */
950 int totwrt; /* total number of sectors left to write */
951 int nwrt; /* current number of sectors to write */
952 struct scsi_rw_big cmd; /* write command */
953 struct scsi_xfer *xs; /* ... convenience */
954 int retval;
955
956 /* Check if recursive dump; if so, punt. */
957 if (sddoingadump)
958 return EFAULT;
959
960 /* Mark as active early. */
961 sddoingadump = 1;
962
963 unit = SDUNIT(dev); /* Decompose unit & partition. */
964 part = SDPART(dev);
965
966 /* Check for acceptable drive number. */
967 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
968 return ENXIO;
969
970 /* Make sure it was initialized. */
971 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
972 return ENXIO;
973
974 /* Convert to disk sectors. Request must be a multiple of size. */
975 lp = sd->sc_dk.dk_label;
976 sectorsize = lp->d_secsize;
977 if ((size % sectorsize) != 0)
978 return EFAULT;
979 totwrt = size / sectorsize;
980 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
981
982 nsects = lp->d_partitions[part].p_size;
983 sectoff = lp->d_partitions[part].p_offset;
984
985 /* Check transfer bounds against partition size. */
986 if ((blkno < 0) || ((blkno + totwrt) > nsects))
987 return EINVAL;
988
989 /* Offset block number to start of partition. */
990 blkno += sectoff;
991
992 xs = &sx;
993
994 while (totwrt > 0) {
995 nwrt = totwrt; /* XXX */
996 #ifndef SD_DUMP_NOT_TRUSTED
997 /*
998 * Fill out the scsi command
999 */
1000 bzero(&cmd, sizeof(cmd));
1001 cmd.opcode = WRITE_BIG;
1002 _lto4b(blkno, cmd.addr);
1003 _lto2b(nwrt, cmd.length);
1004 /*
1005 * Fill out the scsi_xfer structure
1006 * Note: we cannot sleep as we may be an interrupt
1007 * don't use scsi_scsi_cmd() as it may want
1008 * to wait for an xs.
1009 */
1010 bzero(xs, sizeof(sx));
1011 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1012 xs->sc_link = sd->sc_link;
1013 xs->retries = SDRETRIES;
1014 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1015 xs->cmd = (struct scsi_generic *)&cmd;
1016 xs->cmdlen = sizeof(cmd);
1017 xs->resid = nwrt * sectorsize;
1018 xs->error = XS_NOERROR;
1019 xs->bp = 0;
1020 xs->data = va;
1021 xs->datalen = nwrt * sectorsize;
1022
1023 /*
1024 * Pass all this info to the scsi driver.
1025 */
1026 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
1027 if (retval != COMPLETE)
1028 return ENXIO;
1029 #else /* SD_DUMP_NOT_TRUSTED */
1030 /* Let's just talk about this first... */
1031 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1032 delay(500 * 1000); /* half a second */
1033 #endif /* SD_DUMP_NOT_TRUSTED */
1034
1035 /* update block count */
1036 totwrt -= nwrt;
1037 blkno += nwrt;
1038 va += sectorsize * nwrt;
1039 }
1040 sddoingadump = 0;
1041 return 0;
1042 }
1043 #else /* __BDEVSW_DUMP_NEW_TYPE */
1044 int
1045 sddump(dev, blkno, va, size)
1046 dev_t dev;
1047 daddr_t blkno;
1048 caddr_t va;
1049 size_t size;
1050 {
1051
1052 /* Not implemented. */
1053 return ENXIO;
1054 }
1055 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1056