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