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