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