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