summaryrefslogtreecommitdiffstats
path: root/sys/scsi/su.c
blob: fffe45dcbe798354acd59c568d7750381dbefd9f (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* su: SCSI Universal. This is a universal SCSI device that
 * has a fixed minor number format.  This allows you to refer
 * to your devices by BUS, ID, LUN instead of st0, st1, ...
 *
 * This code looks up the underlying device for a given SCSI
 * target and uses that driver.
 *
 *Begin copyright
 *
 * Copyright (C) 1993, 1994, 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.
 *End copyright
 *
 *      $Id:$
 *
 * Tabstops 4
 */

#include <sys/types.h>
#include <sys/conf.h>
#include <scsi/scsiconf.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/systm.h>

/* bnxio, cnxio: non existent device entries (with ugly casts to quiet gcc).
 */
static struct bdevsw bnxio = {
	(int	(*)	__P((dev_t dev, int oflags, int devtype,
				     struct proc *p)))					enxio,
	(int	(*)	__P((dev_t dev, int fflag, int devtype,
				     struct proc *p)))					enxio,
	(d_strategy_t *)									enxio,
	(int	(*)	__P((dev_t dev, int cmd, caddr_t data,
				     int fflag, struct proc *p)))		enxio,
	(int	(*)	())										enxio,
	(int	(*)	__P((dev_t dev)))						0,
	(int	)											0
};

static struct cdevsw cnxio = {
	(int	(*)	__P((dev_t dev, int oflags, int devtype,
				     struct proc *p)))							enxio,
	(int	(*)	__P((dev_t dev, int fflag, int devtype,
				     struct proc *)))							enxio,
	(int	(*)	__P((dev_t dev, struct uio *uio, int ioflag)))	enxio,
	(int	(*)	__P((dev_t dev, struct uio *uio, int ioflag)))	enxio,
	(int	(*)	__P((dev_t dev, int cmd, caddr_t data,
				     int fflag, struct proc *p)))				enxio,
	(int	(*)	__P((struct tty *tp, int rw)))					nullop,
	(int	(*)	__P((int uban)))								enxio,
	(struct	tty *)												0,
	(int	(*)	__P((dev_t dev, int which, struct proc *p)))	enxio,
	(int	(*)	__P(()))										enxio,
	(d_strategy_t *)											enxio
};

/* getsws: Look up the base dev switch for a given new style
 * device.
 */
static int
getsws(dev_t dev, int type,
	struct bdevsw **bdevp, struct cdevsw **cdevp, dev_t *base)
{
	int ret = 0;
	struct scsi_link *scsi_link;
	int chr_dev, blk_dev;

	struct cdevsw *cdev;
	struct bdevsw *bdev;

	int bus = SCSI_BUS(dev),
	    lun = SCSI_LUN(dev),
	    id =  SCSI_ID(dev);

	/* Try to look up the base device by finding the major number in
	 * the scsi_link structure:
	 */
	if ((scsi_link = scsi_link_get(bus, id, lun)) == 0 ||
	scsi_link->dev == NODEV)
	{
		ret = ENXIO;

		/* XXX This assumes that you always have a character device if you
		 *     have a block device.  That seems reasonable.
		 */
		cdev = &cnxio;
		chr_dev = NODEV;
		bdev = &bnxio;
		blk_dev = NODEV;
	}
	else
	{
		int bmaj, cmaj;

		cmaj = major(scsi_link->dev);
		cdev = cdevsw + cmaj;
		chr_dev = OLD_DEV(dev, scsi_link->dev);

		bmaj = chrtoblk(cmaj);
		bdev = (bmaj == NODEV) ? &bnxio : bdevsw + bmaj;
		blk_dev = OLD_DEV(dev, makedev(bmaj, minor(scsi_link->dev)));
	}

	if (cdevp)
		*cdevp = cdev;
	if (bdevp)
		*bdevp = bdev;

	if (type == S_IFCHR)
		*base = chr_dev;
	else
		*base = blk_dev;

	return ret;
}

int
suopen(dev_t dev, int flag, int type, struct proc *p)
{
	struct cdevsw *cdev;
	struct bdevsw *bdev;
	dev_t base;

	if (getsws(dev, type, &bdev, &cdev, &base))
	{
		/* Device not configured?  Reprobe then try again.
		 */
		int bus = SCSI_BUS(dev), lun = SCSI_LUN(dev), id =  SCSI_ID(dev);

		if (scsi_probe_bus(bus, id, lun) || getsws(dev, type, &bdev, &cdev,
		&base))
			return ENXIO;
	}

	/* There is a properly configured underlying device.
	 * Synthesize an appropriate device number:
	 */
	if (type == S_IFCHR)
		return (*cdev->d_open)(base, flag, S_IFCHR, p);
	else
		return (*bdev->d_open)(base, flag, S_IFBLK, p);
}

int suclose(dev_t dev, int fflag, int type, struct proc *p)
{
	struct cdevsw *cdev;
	struct bdevsw *bdev;
	dev_t base;

	(void)getsws(dev, type, &bdev, &cdev, &base);

	if (type == S_IFCHR)
		return (*cdev->d_close)(base, fflag, S_IFCHR, p);
	else
		return (*bdev->d_open)(base, fflag, S_IFBLK, p);
}

void sustrategy(struct buf *bp)
{
	dev_t base;
	struct bdevsw *bdev;
	dev_t dev = bp->b_dev;

	/* XXX: I have no way of knowing if this was through the
	 * block or the character entry point.
	 */
	(void)getsws(dev, S_IFBLK, &bdev, 0, &base);

	bp->b_dev = base;

	(*bdev->d_strategy)(bp);

	bp->b_dev = dev;
}

int suioctl(dev_t dev, int cmd, caddr_t data, int fflag, struct proc *p)
{
	struct cdevsw *cdev;
	dev_t base;

	/* XXX: I have no way of knowing if this was through the
	 * block or the character entry point.
	 */
	(void)getsws(dev, S_IFCHR, 0, &cdev, &base);

	return (*cdev->d_ioctl)(base, cmd, data, fflag, p);
}

int sudump(dev_t dev)
{
	dev_t base;
	struct bdevsw *bdev;

	(void)getsws(dev, S_IFBLK, &bdev, 0, &base);

	return (*bdev->d_dump)(base);
}

int supsize(dev_t dev)
{
	dev_t base;
	struct bdevsw *bdev;

	(void)getsws(dev, S_IFBLK, &bdev, 0, &base);

	return (*bdev->d_psize)(base);
}

int suread(dev_t dev, struct uio *uio, int ioflag)
{
	dev_t base;
	struct cdevsw *cdev;

	(void)getsws(dev, S_IFCHR, 0, &cdev, &base);

	return (*cdev->d_read)(base, uio, ioflag);
}

int suwrite(dev_t dev, struct uio *uio, int ioflag)
{
	dev_t base;
	struct cdevsw *cdev;

	(void)getsws(dev, S_IFCHR, 0, &cdev, &base);

	return (*cdev->d_write)(base, uio, ioflag);
}

int suselect(dev_t dev, int which, struct proc *p)
{
	dev_t base;
	struct cdevsw *cdev;

	(void)getsws(dev, S_IFCHR, 0, &cdev, &base);

	return (*cdev->d_select)(base, which, p);
}
OpenPOWER on IntegriCloud