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