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