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