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