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