summaryrefslogtreecommitdiffstats
path: root/sys/scsi/uk.c
blob: 7544d1e8810ee592f802d6cfbb42647f9ceea9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* 
 * Dummy driver for a device we can't identify.
 * by Julian Elischer (julian@tfs.com)
 *
 *      $Id: uk.c,v 1.5 1994/12/16 06:03:28 phk Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>

/*
 * This driver is so simple it uses all the default services
 */
struct scsi_device uk_switch =
{
    NULL,
    NULL,
    NULL,
    NULL,
    "uk",
    0,
    0, 0
};

struct uk_data {
	u_int32 flags;
#define UK_KNOWN	0x02
	struct scsi_link *sc_link;	/* all the inter level info */
};

struct uk_driver {
	u_int32		size;
	struct uk_data	**uk_data;
} uk_driver;

static u_int32 next_uk_unit = 0;

errval ukopen();
/*
 * The routine called by the low level scsi routine when it discovers
 * a device suitable for this driver.
 */
errval 
ukattach(sc_link)
	struct scsi_link *sc_link;
{
	u_int32 unit, i, stat;
	struct uk_data *uk, **ukrealloc;
	unsigned char *tbl;

	SC_DEBUG(sc_link, SDEV_DB2, ("ukattach: "));

	/*
	 * allocate the resources for another drive
	 * if we have already allocate a uk_data pointer we must
	 * copy the old pointers into a new region that is
	 * larger and release the old region, aka realloc
	 */
	/* XXX
	 * This if will always be true for now, but future code may
	 * preallocate more units to reduce overhead.  This would be
	 * done by changing the malloc to be (next_uk_unit * x) and
	 * the uk_driver.size++ to be +x
	 */
	unit = next_uk_unit++;
	if (unit >= uk_driver.size) {
		ukrealloc =
			malloc(sizeof(uk_driver.uk_data) * next_uk_unit,
			M_DEVBUF, M_NOWAIT);
		if (!ukrealloc) {
			printf("uk%ld: malloc failed for ukrealloc\n", unit);
			return (0);
		}
		/* Make sure we have something to copy before we copy it */
		bzero(ukrealloc, sizeof(uk_driver.uk_data) * next_uk_unit);
		if (uk_driver.size) {
			bcopy(uk_driver.uk_data, ukrealloc,
				sizeof(uk_driver.uk_data) * uk_driver.size);
			free(uk_driver.uk_data, M_DEVBUF);
		}
		uk_driver.uk_data = ukrealloc;
		uk_driver.uk_data[unit] = NULL;
		uk_driver.size++;
	}
	if (uk_driver.uk_data[unit]) {
		printf("uk%ld: Already has storage!\n", unit);
		return (0);
	}
	/*
	 * alloate the per drive data area
	 */
	uk = uk_driver.uk_data[unit] =
		malloc(sizeof(struct uk_data), M_DEVBUF, M_NOWAIT);
	if (!uk) {
		printf("uk%ld: malloc failed for uk_data\n", unit);
		return (0);
	}
	bzero(uk, sizeof(struct uk_data));

	/*
	 * Store information needed to contact our base driver
	 */
	uk->sc_link = sc_link;
	sc_link->device = &uk_switch;
	sc_link->dev_unit = unit;
	sc_link->dev = UKSETUNIT(scsi_dev_lookup(ukopen), unit);

	printf("uk%d: unknown device\n", unit);
	uk->flags = UK_KNOWN;

	return 1;		/* XXX ??? */
}

/*
 *    open the device.
 */
errval 
ukopen(dev)
	dev_t dev;
{
	errval  errcode = 0;
	u_int32 unit, mode;
	struct uk_data *uk;
	struct scsi_link *sc_link;
	unit = UKUNIT(dev);

	/*
	 * Check the unit is legal 
	 */
	if (unit >= uk_driver.size) {
		printf("uk%d: uk %d  > %d\n", unit, unit, uk_driver.size);
		return ENXIO;
	}
	uk = uk_driver.uk_data[unit];

	/*
	 * Make sure the device has been initialised
	 */
	if((!uk) || (!(uk->flags & UK_KNOWN))) {
		printf("uk%d: not set up\n", unit);
		return ENXIO;
	}
		
	/*
	 * Only allow one at a time
	 */
	sc_link = uk->sc_link;
	if (sc_link->flags & SDEV_OPEN) {
		printf("uk%d: already open\n", unit);
		return ENXIO;
	}
	sc_link->flags |= SDEV_OPEN;
	SC_DEBUG(sc_link, SDEV_DB1, ("ukopen: dev=0x%x (unit %d (of %d))\n",
		dev, unit, uk_driver.size));
	/*
	 * Catch any unit attention errors.
	 */
	return 0;
}

/*
 * close the device.. only called if we are the LAST
 * occurence of an open device
 */
errval 
ukclose(dev)
	dev_t dev;
{
	u_int32 unit = minor(dev);
	unsigned mode; /* XXX !!! XXX FIXME!!! 0??? */
	struct uk_data *uk;
	struct scsi_link *sc_link;

	uk = uk_driver.uk_data[unit];
	sc_link = uk->sc_link;

	SC_DEBUG(sc_link, SDEV_DB1, ("Closing device"));
	sc_link->flags &= ~SDEV_OPEN;
	return (0);
}

/*
 * Perform special action on behalf of the user
 * Only does generic scsi ioctls.
 */
errval 
ukioctl(dev, cmd, arg, mode)
	dev_t   dev;
	u_int32 cmd;
	caddr_t arg;
	int mode;
{
	unsigned char unit;
	struct uk_data *uk;
	struct scsi_link *sc_link;

	/*
	 * Find the device that the user is talking about
	 */
 	unit = UKUNIT(dev);
	uk = uk_driver.uk_data[unit];
	sc_link = uk->sc_link;
	return(scsi_do_ioctl(dev,sc_link,cmd,arg,mode));
}

OpenPOWER on IntegriCloud