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