1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1995-03-04 20:51:10 +00:00
|
|
|
* worm: Write Once device driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, HD Associates, Inc.
|
|
|
|
* PO Box 276
|
|
|
|
* Pepperell, MA 01463
|
|
|
|
* 508 433 5266
|
|
|
|
* dufault@hda.com
|
|
|
|
*
|
|
|
|
* This code is contributed to the University of California at Berkeley:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1996-01-05 20:12:53 +00:00
|
|
|
* $Id: worm.c,v 1.17 1996/01/02 15:44:00 joerg Exp $
|
1995-03-04 20:51:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* XXX This is PRELIMINARY.
|
|
|
|
*
|
|
|
|
* We need the "kern devconf" stuff, but I'm not
|
|
|
|
* going to add it until it is done in a simple way that provides
|
|
|
|
* base behavior in scsi_driver.c
|
|
|
|
*
|
|
|
|
* Until Bruce finishes the slice stuff there will be no partitions.
|
|
|
|
* When it is finished I hope to hoist the partition code up into
|
|
|
|
* "scsi_driver" and use common code for all devices.
|
|
|
|
*/
|
|
|
|
|
1996-01-05 20:12:53 +00:00
|
|
|
#include "opt_bounce.h"
|
|
|
|
|
1995-03-04 20:51:10 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
1995-04-23 07:39:21 +00:00
|
|
|
#include <sys/proc.h>
|
1995-11-29 10:49:16 +00:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#ifdef DEVFS
|
|
|
|
#include <sys/devfsext.h>
|
|
|
|
#endif /*DEVFS*/
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <scsi/scsi_all.h>
|
|
|
|
#include <scsi/scsiconf.h>
|
|
|
|
#include <scsi/scsi_disk.h>
|
1995-11-29 10:49:16 +00:00
|
|
|
|
|
|
|
|
1995-03-04 20:51:10 +00:00
|
|
|
struct scsi_data {
|
1995-11-19 22:22:35 +00:00
|
|
|
struct buf_queue_head buf_queue;
|
1995-03-04 20:51:10 +00:00
|
|
|
u_int32 n_blks; /* Number of blocks (0 for bogus) */
|
|
|
|
u_int32 blk_size; /* Size of each blocks */
|
1995-12-08 11:19:42 +00:00
|
|
|
#ifdef DEVFS
|
|
|
|
void *devfs_token; /* more elaborate later */
|
|
|
|
#endif
|
1995-03-04 20:51:10 +00:00
|
|
|
};
|
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static void wormstart(u_int32 unit, u_int32 flags);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static errval worm_open(dev_t dev, int flags, int fmt, struct proc *p,
|
|
|
|
struct scsi_link *sc_link);
|
|
|
|
static errval worm_ioctl(dev_t dev, int cmd, caddr_t addr, int flag,
|
1995-03-04 20:51:10 +00:00
|
|
|
struct proc *p, struct scsi_link *sc_link);
|
1995-10-21 23:13:11 +00:00
|
|
|
static errval worm_close(dev_t dev, int flag, int fmt, struct proc *p,
|
1995-03-04 20:51:10 +00:00
|
|
|
struct scsi_link *sc_link);
|
1995-10-21 23:13:11 +00:00
|
|
|
static void worm_strategy(struct buf *bp, struct scsi_link *sc_link);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t wormopen;
|
|
|
|
static d_close_t wormclose;
|
|
|
|
static d_ioctl_t wormioctl;
|
1995-12-14 09:55:16 +00:00
|
|
|
static d_strategy_t wormstrategy;
|
1995-12-08 11:19:42 +00:00
|
|
|
|
|
|
|
#define CDEV_MAJOR 62
|
1995-12-08 23:23:00 +00:00
|
|
|
static struct cdevsw worm_cdevsw =
|
1995-12-08 11:19:42 +00:00
|
|
|
{ wormopen, wormclose, rawread, rawwrite, /*62*/
|
|
|
|
wormioctl, nostop, nullreset, nodevtotty,/* worm */
|
|
|
|
seltrue, nommap, wormstrategy };
|
|
|
|
|
|
|
|
|
1996-01-02 15:44:00 +00:00
|
|
|
static int
|
|
|
|
wormunit(dev_t dev) {
|
|
|
|
return (minor(dev) & ~(SCSI_FIXED_MASK|SCSI_CONTROL_MASK));
|
|
|
|
}
|
|
|
|
|
1995-03-04 20:51:10 +00:00
|
|
|
SCSI_DEVICE_ENTRIES(worm)
|
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static struct scsi_device worm_switch =
|
1995-03-04 20:51:10 +00:00
|
|
|
{
|
1995-12-08 11:19:42 +00:00
|
|
|
NULL,
|
|
|
|
wormstart, /* we have a queue, and this is how we service it */
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"worm",
|
|
|
|
0,
|
1995-03-04 20:51:10 +00:00
|
|
|
{0, 0},
|
|
|
|
SDEV_ONCE_ONLY, /* Only one open allowed */
|
|
|
|
wormattach,
|
|
|
|
"Write-Once",
|
|
|
|
wormopen,
|
1995-12-08 11:19:42 +00:00
|
|
|
sizeof(struct scsi_data),
|
1995-03-04 20:51:10 +00:00
|
|
|
T_WORM,
|
1996-01-02 15:44:00 +00:00
|
|
|
wormunit,
|
1995-03-04 20:51:10 +00:00
|
|
|
0,
|
|
|
|
worm_open,
|
|
|
|
0,
|
|
|
|
worm_close,
|
|
|
|
worm_strategy,
|
|
|
|
};
|
|
|
|
|
1995-10-09 15:15:01 +00:00
|
|
|
static int worm_size(struct scsi_link *sc_link, int flags)
|
1995-03-04 20:51:10 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct scsi_data *worm = sc_link->sd;
|
|
|
|
|
|
|
|
worm->n_blks = scsi_read_capacity(sc_link, &worm->blk_size,
|
1995-10-09 15:15:01 +00:00
|
|
|
flags);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
1995-10-09 15:15:01 +00:00
|
|
|
if(worm->blk_size == 0)
|
|
|
|
/* XXX */
|
|
|
|
worm->blk_size = 2048;
|
1995-03-04 20:51:10 +00:00
|
|
|
if (worm->n_blks)
|
|
|
|
{
|
|
|
|
sc_link->flags |= SDEV_MEDIA_LOADED;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sc_link->flags &= ~SDEV_MEDIA_LOADED;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static errval
|
1995-03-04 20:51:10 +00:00
|
|
|
wormattach(struct scsi_link *sc_link)
|
|
|
|
{
|
|
|
|
struct scsi_data *worm = sc_link->sd;
|
|
|
|
|
1995-11-19 22:22:35 +00:00
|
|
|
TAILQ_INIT(&worm->buf_queue);
|
|
|
|
|
1995-03-04 20:51:10 +00:00
|
|
|
printf("- UNTESTED ");
|
|
|
|
|
1995-10-09 15:15:01 +00:00
|
|
|
if (worm_size(sc_link, SCSI_NOSLEEP | SCSI_NOMASK) == 0)
|
1995-03-15 14:22:12 +00:00
|
|
|
printf("- can't get capacity.");
|
1995-03-04 20:51:10 +00:00
|
|
|
else
|
1995-12-08 11:19:42 +00:00
|
|
|
printf("with %ld %ld byte blocks.",
|
|
|
|
worm->n_blks, worm->blk_size);
|
|
|
|
#ifdef DEVFS
|
|
|
|
|
|
|
|
worm->devfs_token = devfs_add_devsw( "/", "rworm", &worm_cdevsw, 0,
|
|
|
|
DV_CHR, 0, 0, 0600);
|
|
|
|
#endif
|
1995-03-04 20:51:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wormstart looks to see if there is a buf waiting for the device
|
|
|
|
* and that the device is not already busy. If both are true,
|
|
|
|
* It dequeues the buf and creates a scsi command to perform the
|
|
|
|
* transfer required. The transfer request will call scsi_done
|
|
|
|
* on completion, which will in turn call this routine again
|
|
|
|
* so that the next queued transfer is performed.
|
|
|
|
* The bufs are queued by the strategy routine (wormstrategy)
|
|
|
|
*
|
|
|
|
* This routine is also called after other non-queued requests
|
|
|
|
* have been made of the scsi driver, to ensure that the queue
|
|
|
|
* continues to be drained.
|
|
|
|
* wormstart() is called at splbio
|
|
|
|
*
|
|
|
|
* XXX It looks like we need a "scsistart" to hoist common code up
|
|
|
|
* into. In particular, the removable media checking should be
|
|
|
|
* handled in one place.
|
|
|
|
*/
|
1995-10-21 23:13:11 +00:00
|
|
|
static void
|
1995-04-23 22:07:56 +00:00
|
|
|
wormstart(unit, flags)
|
1995-03-04 20:51:10 +00:00
|
|
|
u_int32 unit;
|
1995-04-23 22:07:56 +00:00
|
|
|
u_int32 flags;
|
1995-03-04 20:51:10 +00:00
|
|
|
{
|
|
|
|
struct scsi_link *sc_link = SCSI_LINK(&worm_switch, unit);
|
|
|
|
struct scsi_data *worm = sc_link->sd;
|
|
|
|
register struct buf *bp = 0;
|
1995-12-05 07:14:27 +00:00
|
|
|
struct scsi_rw_big cmd;
|
1995-03-04 20:51:10 +00:00
|
|
|
|
|
|
|
u_int32 lba; /* Logical block address */
|
|
|
|
u_int32 tl; /* Transfer length */
|
|
|
|
|
|
|
|
SC_DEBUG(sc_link, SDEV_DB2, ("wormstart "));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should reject all queued entries if SDEV_MEDIA_LOADED is not true.
|
|
|
|
*/
|
|
|
|
if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
|
|
|
|
goto badnews; /* no I/O.. media changed or something */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if there is a buf to do and we are not already
|
|
|
|
* doing one
|
|
|
|
*/
|
|
|
|
while (sc_link->opennings != 0) {
|
|
|
|
|
|
|
|
/* if a special awaits, let it proceed first */
|
|
|
|
if (sc_link->flags & SDEV_WAITING) {
|
|
|
|
sc_link->flags &= ~SDEV_WAITING;
|
1995-04-23 07:39:21 +00:00
|
|
|
wakeup(sc_link);
|
1995-03-04 20:51:10 +00:00
|
|
|
return;
|
|
|
|
}
|
1995-11-19 22:22:35 +00:00
|
|
|
|
|
|
|
bp = worm->buf_queue.tqh_first;
|
|
|
|
if (bp == NULL) { /* yes, an assign */
|
|
|
|
return;
|
1995-03-04 20:51:10 +00:00
|
|
|
}
|
1995-11-19 22:22:35 +00:00
|
|
|
TAILQ_REMOVE( &worm->buf_queue, bp, b_act);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill out the scsi command
|
|
|
|
*/
|
|
|
|
bzero(&cmd, sizeof(cmd));
|
|
|
|
if ((bp->b_flags & B_READ) == B_WRITE) {
|
|
|
|
cmd.op_code = WRITE_BIG;
|
1995-04-23 22:07:56 +00:00
|
|
|
flags |= SCSI_DATA_OUT;
|
1995-03-04 20:51:10 +00:00
|
|
|
} else {
|
|
|
|
cmd.op_code = READ_BIG;
|
1995-04-23 22:07:56 +00:00
|
|
|
flags |= SCSI_DATA_IN;
|
1995-03-04 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lba = bp->b_blkno / (worm->blk_size / DEV_BSIZE);
|
|
|
|
tl = bp->b_bcount / worm->blk_size;
|
|
|
|
|
1995-12-05 07:14:27 +00:00
|
|
|
scsi_uto4b(lba, &cmd.addr_3);
|
|
|
|
scsi_uto2b(tl, &cmd.length2);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* go ask the adapter to do all this for us
|
|
|
|
*/
|
|
|
|
if (scsi_scsi_cmd(sc_link,
|
|
|
|
(struct scsi_generic *) &cmd,
|
|
|
|
sizeof(cmd),
|
|
|
|
(u_char *) bp->b_un.b_addr,
|
|
|
|
bp->b_bcount,
|
|
|
|
0, /* can't retry a read on a tape really */
|
|
|
|
100000,
|
|
|
|
bp,
|
|
|
|
flags | SCSI_NOSLEEP) == SUCCESSFULLY_QUEUED) {
|
|
|
|
} else {
|
1995-05-03 23:38:20 +00:00
|
|
|
printf("worm%ld: oops not queued\n", unit);
|
1995-10-09 15:15:01 +00:00
|
|
|
if (bp) {
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EIO;
|
|
|
|
biodone(bp);
|
|
|
|
}
|
1995-03-04 20:51:10 +00:00
|
|
|
}
|
|
|
|
} /* go back and see if we can cram more work in.. */
|
1995-12-05 07:14:27 +00:00
|
|
|
badnews:
|
1995-03-04 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static void
|
1995-03-04 20:51:10 +00:00
|
|
|
worm_strategy(struct buf *bp, struct scsi_link *sc_link)
|
|
|
|
{
|
|
|
|
unsigned char unit;
|
|
|
|
u_int32 opri;
|
|
|
|
struct scsi_data *worm;
|
|
|
|
|
1995-05-03 23:53:32 +00:00
|
|
|
unit = minor((bp->b_dev));
|
1995-03-04 20:51:10 +00:00
|
|
|
worm = sc_link->sd;
|
|
|
|
|
|
|
|
/* XXX: Can't we move this check up to "scsi_strategy"?
|
|
|
|
*/
|
|
|
|
if (!(sc_link->flags & SDEV_MEDIA_LOADED) ||
|
|
|
|
bp->b_blkno > worm->n_blks ||
|
|
|
|
bp->b_bcount & (worm->blk_size - 1)) {
|
|
|
|
bp->b_error = EIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
opri = splbio();
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1995-03-04 20:51:10 +00:00
|
|
|
* Use a bounce buffer if necessary
|
|
|
|
* XXX: How can we move this up?
|
1995-05-30 08:16:23 +00:00
|
|
|
*/
|
1995-03-04 20:51:10 +00:00
|
|
|
#ifdef BOUNCE_BUFFERS
|
|
|
|
if (sc_link->flags & SDEV_BOUNCE)
|
|
|
|
vm_bounce_alloc(bp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Place it in the queue of activities for this device
|
|
|
|
* at the end.
|
|
|
|
*/
|
1995-11-19 22:22:35 +00:00
|
|
|
TAILQ_INSERT_TAIL(&worm->buf_queue, bp, b_act);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
1995-04-23 22:07:56 +00:00
|
|
|
wormstart(unit, 0);
|
1995-03-04 20:51:10 +00:00
|
|
|
|
|
|
|
splx(opri);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the device. XXX: I'm completely guessing at this sequence.
|
|
|
|
*/
|
1995-10-21 23:13:11 +00:00
|
|
|
static int
|
1995-03-04 20:51:10 +00:00
|
|
|
worm_open(dev_t dev, int flags, int fmt, struct proc *p,
|
|
|
|
struct scsi_link *sc_link)
|
|
|
|
{
|
|
|
|
if (sc_link->flags & SDEV_OPEN)
|
|
|
|
return EBUSY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that it is still responding and ok.
|
|
|
|
* if the media has been changed this will result in a
|
|
|
|
* "unit attention" error which the error code will
|
|
|
|
* disregard because the SDEV_OPEN flag is not yet set
|
|
|
|
*
|
|
|
|
* XXX This should REALLY be hoisted up. As soon as Bruce
|
|
|
|
* finishes that slice stuff. (Add a different flag,
|
|
|
|
* and then do a "scsi_test_unit_ready" with the "ignore
|
|
|
|
* unit attention" thing set. Then all this replicated
|
|
|
|
* test unit ready code can be pulled up.
|
|
|
|
*/
|
|
|
|
scsi_test_unit_ready(sc_link, SCSI_SILENT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next time actually take notice of error returns
|
|
|
|
*/
|
|
|
|
sc_link->flags |= SDEV_OPEN; /* unit attn errors are now errors */
|
|
|
|
|
|
|
|
if (scsi_test_unit_ready(sc_link, SCSI_SILENT) != 0) {
|
|
|
|
SC_DEBUG(sc_link, SDEV_DB3, ("not ready\n"));
|
|
|
|
sc_link->flags &= ~SDEV_OPEN;
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
scsi_start_unit(sc_link, SCSI_SILENT);
|
|
|
|
|
|
|
|
scsi_prevent(sc_link, PR_PREVENT, SCSI_SILENT);
|
|
|
|
|
1995-10-09 15:15:01 +00:00
|
|
|
if (worm_size(sc_link, 0) == 0) {
|
1995-03-04 20:51:10 +00:00
|
|
|
scsi_stop_unit(sc_link, 0, SCSI_SILENT);
|
|
|
|
scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
|
|
|
|
sc_link->flags &= ~SDEV_OPEN;
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-10-21 23:13:11 +00:00
|
|
|
static int
|
1995-03-04 20:51:10 +00:00
|
|
|
worm_close(dev_t dev, int flag, int fmt, struct proc *p,
|
|
|
|
struct scsi_link *sc_link)
|
|
|
|
{
|
|
|
|
scsi_stop_unit(sc_link, 0, SCSI_SILENT);
|
|
|
|
scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
|
|
|
|
sc_link->flags &= ~SDEV_OPEN;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1995-11-29 10:49:16 +00:00
|
|
|
|
|
|
|
static worm_devsw_installed = 0;
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static void
|
|
|
|
worm_drvinit(void *unused)
|
1995-11-29 10:49:16 +00:00
|
|
|
{
|
|
|
|
dev_t dev;
|
|
|
|
|
|
|
|
if( ! worm_devsw_installed ) {
|
|
|
|
dev = makedev(CDEV_MAJOR,0);
|
|
|
|
cdevsw_add(&dev,&worm_cdevsw,NULL);
|
|
|
|
worm_devsw_installed = 1;
|
1995-11-29 14:41:20 +00:00
|
|
|
}
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(wormdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,worm_drvinit,NULL)
|
|
|
|
|
|
|
|
|