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