subr_bus.c 145 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
4
 * Copyright (c) 1997,1998,2003 Doug Rabson
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
 */

David E. O'Brien's avatar
David E. O'Brien committed
29
30
31
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

32
#include "opt_bus.h"
33
#include "opt_ddb.h"
34

35
#include <sys/param.h>
36
#include <sys/conf.h>
37
#include <sys/domainset.h>
38
#include <sys/eventhandler.h>
39
40
#include <sys/filio.h>
#include <sys/lock.h>
41
#include <sys/kernel.h>
42
#include <sys/kobj.h>
43
#include <sys/limits.h>
44
45
#include <sys/malloc.h>
#include <sys/module.h>
46
47
#include <sys/mutex.h>
#include <sys/poll.h>
48
#include <sys/priv.h>
49
50
#include <sys/proc.h>
#include <sys/condvar.h>
51
#include <sys/queue.h>
52
#include <machine/bus.h>
53
#include <sys/random.h>
54
#include <sys/refcount.h>
55
#include <sys/rman.h>
56
#include <sys/sbuf.h>
57
58
#include <sys/selinfo.h>
#include <sys/signalvar.h>
59
#include <sys/smp.h>
60
61
#include <sys/sysctl.h>
#include <sys/systm.h>
62
#include <sys/uio.h>
63
#include <sys/bus.h>
64
#include <sys/cpuset.h>
65

Craig Rodrigues's avatar
Craig Rodrigues committed
66
67
#include <net/vnet.h>

68
#include <machine/cpu.h>
69
70
71
#include <machine/stdarg.h>

#include <vm/uma.h>
72
#include <vm/vm.h>
73

74
75
#include <ddb/ddb.h>

76
77
78
79
SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
    NULL);
SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
    NULL);
Warner Losh's avatar
Warner Losh committed
80

81
82
83
84
85
/*
 * Used to attach drivers to devclasses.
 */
typedef struct driverlink *driverlink_t;
struct driverlink {
86
87
	kobj_class_t	driver;
	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
88
	int		pass;
Warner Losh's avatar
Warner Losh committed
89
90
	int		flags;
#define DL_DEFERRED_PROBE	1	/* Probe deferred on this */
91
	TAILQ_ENTRY(driverlink) passlink;
92
93
94
95
96
97
98
};

/*
 * Forward declarations
 */
typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
99
typedef TAILQ_HEAD(device_list, _device) device_list_t;
100
101
102

struct devclass {
	TAILQ_ENTRY(devclass) link;
103
	devclass_t	parent;		/* parent in devclass hierarchy */
104
	driver_list_t	drivers;	/* bus devclasses store drivers for bus */
105
106
107
	char		*name;
	device_t	*devices;	/* array of devices indexed by unit */
	int		maxunit;	/* size of devices array */
108
109
	int		flags;
#define DC_HAS_CHILDREN		1
110
111
112

	struct sysctl_ctx_list sysctl_ctx;
	struct sysctl_oid *sysctl_tree;
113
114
};

115
/**
116
117
118
119
 * @brief Implementation of _device.
 *
 * The structure is named "_device" instead of "device" to avoid type confusion
 * caused by other subsystems defining a (struct device).
120
 */
121
struct _device {
122
123
124
125
126
127
128
129
130
	/*
	 * A device is a kernel object. The first field must be the
	 * current ops table for the object.
	 */
	KOBJ_FIELDS;

	/*
	 * Device hierarchy.
	 */
131
132
	TAILQ_ENTRY(_device)	link;	/**< list of devices in parent */
	TAILQ_ENTRY(_device)	devlink; /**< global device list membership */
133
134
	device_t	parent;		/**< parent of this device  */
	device_list_t	children;	/**< list of child devices */
135
136
137
138

	/*
	 * Details of this device.
	 */
139
140
141
142
143
	driver_t	*driver;	/**< current driver */
	devclass_t	devclass;	/**< current device class */
	int		unit;		/**< current unit number */
	char*		nameunit;	/**< name+unit e.g. foodev0 */
	char*		desc;		/**< driver specific description */
144
	u_int		busy;		/**< count of calls to device_busy() */
145
	device_state_t	state;		/**< current device state  */
146
	uint32_t	devflags;	/**< api level flags for device_get_flags() */
147
148
	u_int		flags;		/**< internal device flags  */
	u_int	order;			/**< order from device_add_child_ordered() */
149
150
	void	*ivars;			/**< instance variables  */
	void	*softc;			/**< current driver's variables  */
151

152
153
	struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables  */
	struct sysctl_oid *sysctl_tree;	/**< state for sysctl variables */
154
155
};

156
static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
157
static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
158

159
160
161
162
EVENTHANDLER_LIST_DEFINE(device_attach);
EVENTHANDLER_LIST_DEFINE(device_detach);
EVENTHANDLER_LIST_DEFINE(dev_lookup);

163
static void devctl2_init(void);
Warner Losh's avatar
Warner Losh committed
164
static bool device_frozen;
165

166
167
168
#define DRIVERNAME(d)	((d)? d->name : "no driver")
#define DEVCLANAME(d)	((d)? d->name : "no devclass")

169
#ifdef BUS_DEBUG
170
171

static int bus_debug = 1;
172
173
SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
    "Bus debug level");
174
#define PDEBUG(a)	if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
175
176
#define DEVICENAME(d)	((d)? device_get_name(d): "no device")

177
178
/**
 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
179
180
 * prevent syslog from deleting initial spaces
 */
181
#define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
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

static void print_device_short(device_t dev, int indent);
static void print_device(device_t dev, int indent);
void print_device_tree_short(device_t dev, int indent);
void print_device_tree(device_t dev, int indent);
static void print_driver_short(driver_t *driver, int indent);
static void print_driver(driver_t *driver, int indent);
static void print_driver_list(driver_list_t drivers, int indent);
static void print_devclass_short(devclass_t dc, int indent);
static void print_devclass(devclass_t dc, int indent);
void print_devclass_list_short(void);
void print_devclass_list(void);

#else
/* Make the compiler ignore the function calls */
#define PDEBUG(a)			/* nop */
#define DEVICENAME(d)			/* nop */

#define print_device_short(d,i)		/* nop */
#define print_device(d,i)		/* nop */
#define print_device_tree_short(d,i)	/* nop */
#define print_device_tree(d,i)		/* nop */
#define print_driver_short(d,i)		/* nop */
#define print_driver(d,i)		/* nop */
#define print_driver_list(d,i)		/* nop */
#define print_devclass_short(d,i)	/* nop */
#define print_devclass(d,i)		/* nop */
#define print_devclass_list_short()	/* nop */
#define print_devclass_list()		/* nop */
#endif

213
214
215
216
217
/*
 * dev sysctl tree
 */

enum {
218
219
220
221
222
223
224
225
226
227
228
	DEVCLASS_SYSCTL_PARENT,
};

static int
devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
{
	devclass_t dc = (devclass_t)arg1;
	const char *value;

	switch (arg2) {
	case DEVCLASS_SYSCTL_PARENT:
229
		value = dc->parent ? dc->parent->name : "";
230
231
232
233
		break;
	default:
		return (EINVAL);
	}
234
	return (SYSCTL_OUT_STR(req, value));
235
236
237
238
239
240
241
242
243
244
}

static void
devclass_sysctl_init(devclass_t dc)
{
	if (dc->sysctl_tree != NULL)
		return;
	sysctl_ctx_init(&dc->sysctl_ctx);
	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
245
	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
246
	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
247
248
	    OID_AUTO, "%parent",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
249
250
251
252
253
	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
	    "parent class");
}

enum {
254
255
256
257
	DEVICE_SYSCTL_DESC,
	DEVICE_SYSCTL_DRIVER,
	DEVICE_SYSCTL_LOCATION,
	DEVICE_SYSCTL_PNPINFO,
258
	DEVICE_SYSCTL_PARENT,
259
260
261
262
263
};

static int
device_sysctl_handler(SYSCTL_HANDLER_ARGS)
{
264
	struct sbuf sb;
265
266
267
	device_t dev = (device_t)arg1;
	int error;

268
	sbuf_new_for_sysctl(&sb, NULL, 1024, req);
269
	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
270
271
	switch (arg2) {
	case DEVICE_SYSCTL_DESC:
Warner Losh's avatar
Warner Losh committed
272
		sbuf_cat(&sb, dev->desc ? dev->desc : "");
273
274
		break;
	case DEVICE_SYSCTL_DRIVER:
Warner Losh's avatar
Warner Losh committed
275
		sbuf_cat(&sb, dev->driver ? dev->driver->name : "");
276
277
		break;
	case DEVICE_SYSCTL_LOCATION:
278
		bus_child_location(dev, &sb);
279
280
		break;
	case DEVICE_SYSCTL_PNPINFO:
281
		bus_child_pnpinfo(dev, &sb);
282
		break;
283
	case DEVICE_SYSCTL_PARENT:
Warner Losh's avatar
Warner Losh committed
284
		sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
285
		break;
286
	default:
287
		sbuf_delete(&sb);
288
289
		return (EINVAL);
	}
290
291
	error = sbuf_finish(&sb);
	sbuf_delete(&sb);
292
293
294
295
296
297
	return (error);
}

static void
device_sysctl_init(device_t dev)
{
298
	devclass_t dc = dev->devclass;
299
	int domain;
300

301
	if (dev->sysctl_tree != NULL)
302
		return;
303
	devclass_sysctl_init(dc);
304
	sysctl_ctx_init(&dev->sysctl_ctx);
305
	dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
306
307
	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
	    dev->nameunit + strlen(dc->name),
308
	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
309
	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
310
	    OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
311
312
313
	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
	    "device description");
	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
314
315
	    OID_AUTO, "%driver",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
316
317
318
	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
	    "device driver name");
	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
319
320
	    OID_AUTO, "%location",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
321
322
323
	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
	    "device location relative to parent");
	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
324
325
	    OID_AUTO, "%pnpinfo",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
326
327
	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
	    "device identification");
328
	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
329
330
	    OID_AUTO, "%parent",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
331
	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
332
	    "parent device");
333
334
335
336
	if (bus_get_domain(dev, &domain) == 0)
		SYSCTL_ADD_INT(&dev->sysctl_ctx,
		    SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
		    CTLFLAG_RD, NULL, domain, "NUMA domain");
337
338
}

339
340
341
342
343
344
345
346
347
348
static void
device_sysctl_update(device_t dev)
{
	devclass_t dc = dev->devclass;

	if (dev->sysctl_tree == NULL)
		return;
	sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
}

349
350
351
352
353
354
355
356
357
static void
device_sysctl_fini(device_t dev)
{
	if (dev->sysctl_tree == NULL)
		return;
	sysctl_ctx_free(&dev->sysctl_ctx);
	dev->sysctl_tree = NULL;
}

358
359
360
361
362
363
364
365
366
367
368
/*
 * /dev/devctl implementation
 */

/*
 * This design allows only one reader for /dev/devctl.  This is not desirable
 * in the long run, but will get a lot of hair out of this implementation.
 * Maybe we should make this device a clonable device.
 *
 * Also note: we specifically do not attach a device to the device_t tree
 * to avoid potential chicken and egg problems.  One could argue that all
369
 * of this belongs to the root node.
370
371
 */

372
373
374
#define DEVCTL_DEFAULT_QUEUE_LEN 1000
static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
375
SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
Mateusz Guzik's avatar
Mateusz Guzik committed
376
    CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
Warner Losh's avatar
Warner Losh committed
377

378
379
380
381
382
static d_open_t		devopen;
static d_close_t	devclose;
static d_read_t		devread;
static d_ioctl_t	devioctl;
static d_poll_t		devpoll;
David Xu's avatar
David Xu committed
383
static d_kqfilter_t	devkqfilter;
384
385

static struct cdevsw dev_cdevsw = {
Poul-Henning Kamp's avatar
Poul-Henning Kamp committed
386
	.d_version =	D_VERSION,
387
388
389
390
391
	.d_open =	devopen,
	.d_close =	devclose,
	.d_read =	devread,
	.d_ioctl =	devioctl,
	.d_poll =	devpoll,
David Xu's avatar
David Xu committed
392
	.d_kqfilter =	devkqfilter,
393
	.d_name =	"devctl",
394
395
};

Warner Losh's avatar
Warner Losh committed
396
#define DEVCTL_BUFFER (1024 - sizeof(void *))
Warner Losh's avatar
Warner Losh committed
397
struct dev_event_info {
398
	STAILQ_ENTRY(dev_event_info) dei_link;
Warner Losh's avatar
Warner Losh committed
399
	char dei_data[DEVCTL_BUFFER];
400
401
};

402
STAILQ_HEAD(devq, dev_event_info);
403

Warner Losh's avatar
Warner Losh committed
404
static struct dev_softc {
405
406
407
408
409
410
411
412
413
	int		inuse;
	int		nonblock;
	int		queued;
	int		async;
	struct mtx	mtx;
	struct cv	cv;
	struct selinfo	sel;
	struct devq	devq;
	struct sigio	*sigio;
Warner Losh's avatar
Warner Losh committed
414
	uma_zone_t	zone;
415
416
} devsoftc;

David Xu's avatar
David Xu committed
417
418
419
420
421
422
423
424
425
static void	filt_devctl_detach(struct knote *kn);
static int	filt_devctl_read(struct knote *kn, long hint);

struct filterops devctl_rfiltops = {
	.f_isfd = 1,
	.f_detach = filt_devctl_detach,
	.f_event = filt_devctl_read,
};

426
static struct cdev *devctl_dev;
427
428
429
430

static void
devinit(void)
{
431
432
433
	int reserve;
	uma_zone_t z;

434
435
	devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
	    UID_ROOT, GID_WHEEL, 0600, "devctl");
436
437
	mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
	cv_init(&devsoftc.cv, "dev cv");
438
	STAILQ_INIT(&devsoftc.devq);
David Xu's avatar
David Xu committed
439
	knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
Warner Losh's avatar
Warner Losh committed
440
	if (devctl_queue_length > 0) {
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
		/*
		 * Allocate a zone for the messages. Preallocate 2% of these for
		 * a reserve. Allow only devctl_queue_length slabs to cap memory
		 * usage.  The reserve usually allows coverage of surges of
		 * events during memory shortages. Normally we won't have to
		 * re-use events from the queue, but will in extreme shortages.
		 */
		z = devsoftc.zone = uma_zcreate("DEVCTL",
		    sizeof(struct dev_event_info), NULL, NULL, NULL, NULL,
		    UMA_ALIGN_PTR, 0);
		reserve = max(devctl_queue_length / 50, 100);	/* 2% reserve */
		uma_zone_set_max(z, devctl_queue_length);
		uma_zone_set_maxcache(z, 0);
		uma_zone_reserve(z, reserve);
		uma_prealloc(z, reserve);
Warner Losh's avatar
Warner Losh committed
456
	}
457
	devctl2_init();
458
459
460
}

static int
461
devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
462
{
Mateusz Guzik's avatar
Mateusz Guzik committed
463
464
465
466
467
	mtx_lock(&devsoftc.mtx);
	if (devsoftc.inuse) {
		mtx_unlock(&devsoftc.mtx);
		return (EBUSY);
	}
468
469
	/* move to init */
	devsoftc.inuse = 1;
Mateusz Guzik's avatar
Mateusz Guzik committed
470
	mtx_unlock(&devsoftc.mtx);
471
472
473
474
	return (0);
}

static int
475
devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
476
477
{
	mtx_lock(&devsoftc.mtx);
Mateusz Guzik's avatar
Mateusz Guzik committed
478
	devsoftc.inuse = 0;
479
480
	devsoftc.nonblock = 0;
	devsoftc.async = 0;
481
	cv_broadcast(&devsoftc.cv);
482
	funsetown(&devsoftc.sigio);
483
484
485
486
487
488
489
490
491
	mtx_unlock(&devsoftc.mtx);
	return (0);
}

/*
 * The read channel for this device is used to report changes to
 * userland in realtime.  We are required to free the data as well as
 * the n1 object because we allocate them separately.  Also note that
 * we return one record at a time.  If you try to read this device a
492
 * character at a time, you will lose the rest of the data.  Listening
493
494
495
 * programs are expected to cope.
 */
static int
496
devread(struct cdev *dev, struct uio *uio, int ioflag)
497
498
499
500
501
{
	struct dev_event_info *n1;
	int rv;

	mtx_lock(&devsoftc.mtx);
502
	while (STAILQ_EMPTY(&devsoftc.devq)) {
503
504
505
506
507
508
509
510
511
512
513
514
515
		if (devsoftc.nonblock) {
			mtx_unlock(&devsoftc.mtx);
			return (EAGAIN);
		}
		rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
		if (rv) {
			/*
			 * Need to translate ERESTART to EINTR here? -- jake
			 */
			mtx_unlock(&devsoftc.mtx);
			return (rv);
		}
	}
516
517
	n1 = STAILQ_FIRST(&devsoftc.devq);
	STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
518
	devsoftc.queued--;
519
520
	mtx_unlock(&devsoftc.mtx);
	rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
Warner Losh's avatar
Warner Losh committed
521
	uma_zfree(devsoftc.zone, n1);
522
523
524
525
	return (rv);
}

static	int
526
devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
527
528
529
530
531
532
533
534
535
{
	switch (cmd) {
	case FIONBIO:
		if (*(int*)data)
			devsoftc.nonblock = 1;
		else
			devsoftc.nonblock = 0;
		return (0);
	case FIOASYNC:
Warner Losh's avatar
Warner Losh committed
536
		if (*(int*)data)
537
			devsoftc.async = 1;
Warner Losh's avatar
Warner Losh committed
538
		else
539
540
541
542
543
544
			devsoftc.async = 0;
		return (0);
	case FIOSETOWN:
		return fsetown(*(int *)data, &devsoftc.sigio);
	case FIOGETOWN:
		*(int *)data = fgetown(&devsoftc.sigio);
545
546
547
548
549
550
551
552
553
554
555
556
557
		return (0);

		/* (un)Support for other fcntl() calls. */
	case FIOCLEX:
	case FIONCLEX:
	case FIONREAD:
	default:
		break;
	}
	return (ENOTTY);
}

static	int
558
devpoll(struct cdev *dev, int events, struct thread *td)
559
560
561
562
{
	int	revents = 0;

	mtx_lock(&devsoftc.mtx);
563
	if (events & (POLLIN | POLLRDNORM)) {
564
		if (!STAILQ_EMPTY(&devsoftc.devq))
565
566
567
568
			revents = events & (POLLIN | POLLRDNORM);
		else
			selrecord(td, &devsoftc.sel);
	}
569
570
571
572
573
	mtx_unlock(&devsoftc.mtx);

	return (revents);
}

David Xu's avatar
David Xu committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
static int
devkqfilter(struct cdev *dev, struct knote *kn)
{
	int error;

	if (kn->kn_filter == EVFILT_READ) {
		kn->kn_fop = &devctl_rfiltops;
		knlist_add(&devsoftc.sel.si_note, kn, 0);
		error = 0;
	} else
		error = EINVAL;
	return (error);
}

static void
filt_devctl_detach(struct knote *kn)
{
	knlist_remove(&devsoftc.sel.si_note, kn, 0);
}

static int
filt_devctl_read(struct knote *kn, long hint)
{
	kn->kn_data = devsoftc.queued;
	return (kn->kn_data != 0);
}

601
602
603
/**
 * @brief Return whether the userland process is running
 */
604
bool
605
606
devctl_process_running(void)
{
607
	return (devsoftc.inuse == 1);
608
609
}

Warner Losh's avatar
Warner Losh committed
610
611
static struct dev_event_info *
devctl_alloc_dei(void)
612
{
Warner Losh's avatar
Warner Losh committed
613
	struct dev_event_info *dei = NULL;
614

Warner Losh's avatar
Warner Losh committed
615
	mtx_lock(&devsoftc.mtx);
616
	if (devctl_queue_length == 0)
617
		goto out;
618
619
620
621
622
623
624
625
626
627
628
629
630
631
	dei = uma_zalloc(devsoftc.zone, M_NOWAIT);
	if (dei == NULL)
		dei = uma_zalloc(devsoftc.zone, M_NOWAIT | M_USE_RESERVE);
	if (dei == NULL) {
		/*
		 * Guard against no items in the queue. Normally, this won't
		 * happen, but if lots of events happen all at once and there's
		 * a chance we're out of allocated space but none have yet been
		 * queued when we get here, leaving nothing to steal. This can
		 * also happen with error injection. Fail safe by returning
		 * NULL in that case..
		 */
		if (devsoftc.queued == 0)
			goto out;
Warner Losh's avatar
Warner Losh committed
632
		dei = STAILQ_FIRST(&devsoftc.devq);
633
		STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
634
635
		devsoftc.queued--;
	}
636
	MPASS(dei != NULL);
Warner Losh's avatar
Warner Losh committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
	*dei->dei_data = '\0';
out:
	mtx_unlock(&devsoftc.mtx);
	return (dei);
}

static struct dev_event_info *
devctl_alloc_dei_sb(struct sbuf *sb)
{
	struct dev_event_info *dei;

	dei = devctl_alloc_dei();
	if (dei != NULL)
		sbuf_new(sb, dei->dei_data, sizeof(dei->dei_data), SBUF_FIXEDLEN);
	return (dei);
}

static void
devctl_free_dei(struct dev_event_info *dei)
{
	uma_zfree(devsoftc.zone, dei);
}

static void
devctl_queue(struct dev_event_info *dei)
{
	mtx_lock(&devsoftc.mtx);
	STAILQ_INSERT_TAIL(&devsoftc.devq, dei, dei_link);
665
	devsoftc.queued++;
666
	cv_broadcast(&devsoftc.cv);
David Xu's avatar
David Xu committed
667
	KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
668
669
	mtx_unlock(&devsoftc.mtx);
	selwakeup(&devsoftc.sel);
670
671
	if (devsoftc.async && devsoftc.sigio != NULL)
		pgsigio(&devsoftc.sigio, SIGIO, 0);
672
673
}

674
675
/**
 * @brief Send a 'notification' to userland, using standard ways
676
677
 */
void
Warner Losh's avatar
Warner Losh committed
678
679
devctl_notify(const char *system, const char *subsystem, const char *type,
    const char *data)
Warner Losh's avatar
Warner Losh committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
{
	struct dev_event_info *dei;
	struct sbuf sb;

	if (system == NULL || subsystem == NULL || type == NULL)
		return;
	dei = devctl_alloc_dei_sb(&sb);
	if (dei == NULL)
		return;
	sbuf_cpy(&sb, "!system=");
	sbuf_cat(&sb, system);
	sbuf_cat(&sb, " subsystem=");
	sbuf_cat(&sb, subsystem);
	sbuf_cat(&sb, " type=");
	sbuf_cat(&sb, type);
	if (data != NULL) {
		sbuf_putc(&sb, ' ');
		sbuf_cat(&sb, data);
	}
	sbuf_putc(&sb, '\n');
	if (sbuf_finish(&sb) != 0)
		devctl_free_dei(dei);	/* overflow -> drop it */
702
	else
Warner Losh's avatar
Warner Losh committed
703
		devctl_queue(dei);
704
705
}

706
707
708
709
/*
 * Common routine that tries to make sending messages as easy as possible.
 * We allocate memory for the data, copy strings into that, but do not
 * free it unless there's an error.  The dequeue part of the driver should
710
711
712
 * free the data.  We don't send data when the device is disabled.  We do
 * send data, even when we have no listeners, because we wish to avoid
 * races relating to startup and restart of listening applications.
713
714
715
716
717
 *
 * devaddq is designed to string together the type of event, with the
 * object of that event, plus the plug and play info and location info
 * for that event.  This is likely most useful for devices, but less
 * useful for other consumers of this interface.  Those should use
Warner Losh's avatar
Warner Losh committed
718
719
720
721
 * the devctl_notify() interface instead.
 *
 * Output: 
 *	${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev)
722
723
724
725
 */
static void
devaddq(const char *type, const char *what, device_t dev)
{
Warner Losh's avatar
Warner Losh committed
726
	struct dev_event_info *dei;
727
	const char *parstr;
Warner Losh's avatar
Warner Losh committed
728
	struct sbuf sb;
729

Warner Losh's avatar
Warner Losh committed
730
731
	dei = devctl_alloc_dei_sb(&sb);
	if (dei == NULL)
732
		return;
Warner Losh's avatar
Warner Losh committed
733
734
735
	sbuf_cpy(&sb, type);
	sbuf_cat(&sb, what);
	sbuf_cat(&sb, " at ");
736

Warner Losh's avatar
Warner Losh committed
737
	/* Add in the location */
738
	bus_child_location(dev, &sb);
Warner Losh's avatar
Warner Losh committed
739
	sbuf_putc(&sb, ' ');
740

Warner Losh's avatar
Warner Losh committed
741
	/* Add in pnpinfo */
742
	bus_child_pnpinfo(dev, &sb);
743
744

	/* Get the parent of this device, or / if high enough in the tree. */
745
746
747
748
	if (device_get_parent(dev) == NULL)
		parstr = ".";	/* Or '/' ? */
	else
		parstr = device_get_nameunit(device_get_parent(dev));
Warner Losh's avatar
Warner Losh committed
749
750
751
752
753
754
	sbuf_cat(&sb, " on ");
	sbuf_cat(&sb, parstr);
	sbuf_putc(&sb, '\n');
	if (sbuf_finish(&sb) != 0)
		goto bad;
	devctl_queue(dei);
755
	return;
756
bad:
Warner Losh's avatar
Warner Losh committed
757
	devctl_free_dei(dei);
758
759
760
761
762
763
764
765
766
767
768
769
}

/*
 * A device was added to the tree.  We are called just after it successfully
 * attaches (that is, probe and attach success for this device).  No call
 * is made if a device is merely parented into the tree.  See devnomatch
 * if probe fails.  If attach fails, no notification is sent (but maybe
 * we should have a different message for this).
 */
static void
devadded(device_t dev)
{
770
	devaddq("+", device_get_nameunit(dev), dev);
771
772
773
774
775
776
777
778
779
}

/*
 * A device was removed from the tree.  We are called just before this
 * happens.
 */
static void
devremoved(device_t dev)
{
Warner Losh's avatar
Warner Losh committed
780
	devaddq("-", device_get_nameunit(dev), dev);
781
782
783
784
}

/*
 * Called when there's no match for this device.  This is only called
John Baldwin's avatar
John Baldwin committed
785
 * the first time that no match happens, so we don't keep getting this
786
787
 * message.  Should that prove to be undesirable, we can change it.
 * This is called when all drivers that can attach to a given bus
Gavin Atkinson's avatar
Gavin Atkinson committed
788
 * decline to accept this device.  Other errors may not be detected.
789
790
791
792
 */
static void
devnomatch(device_t dev)
{
793
	devaddq("?", "", dev);
794
795
}

796
797
798
799
800
801
802
803
804
805
806
static int
sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
{
	int q, error;

	q = devctl_queue_length;
	error = sysctl_handle_int(oidp, &q, 0, req);
	if (error || !req->newptr)
		return (error);
	if (q < 0)
		return (EINVAL);
Warner Losh's avatar
Warner Losh committed
807
808
809
810
811
812
813
814
815
816

	/*
	 * When set as a tunable, we've not yet initialized the mutex.
	 * It is safe to just assign to devctl_queue_length and return
	 * as we're racing no one. We'll use whatever value set in
	 * devinit.
	 */
	if (!mtx_initialized(&devsoftc.mtx)) {
		devctl_queue_length = q;
		return (0);
Warner Losh's avatar
Warner Losh committed
817
	}
Warner Losh's avatar
Warner Losh committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

	/*
	 * XXX It's hard to grow or shrink the UMA zone. Only allow
	 * disabling the queue size for the moment until underlying
	 * UMA issues can be sorted out.
	 */
	if (q != 0)
		return (EINVAL);
	if (q == devctl_queue_length)
		return (0);
	mtx_lock(&devsoftc.mtx);
	devctl_queue_length = 0;
	uma_zdestroy(devsoftc.zone);
	devsoftc.zone = 0;
	mtx_unlock(&devsoftc.mtx);
Warner Losh's avatar
Warner Losh committed
833
834
835
	return (0);
}

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/**
 * @brief safely quotes strings that might have double quotes in them.
 *
 * The devctl protocol relies on quoted strings having matching quotes.
 * This routine quotes any internal quotes so the resulting string
 * is safe to pass to snprintf to construct, for example pnp info strings.
 *
 * @param sb	sbuf to place the characters into
 * @param src	Original buffer.
 */
void
devctl_safe_quote_sb(struct sbuf *sb, const char *src)
{
	while (*src != '\0') {
		if (*src == '"' || *src == '\\')
			sbuf_putc(sb, '\\');
		sbuf_putc(sb, *src++);
	}
}

856
857
/* End of /dev/devctl code */

858
static struct device_list bus_data_devices;
859
860
static int bus_data_generation = 1;

861
static kobj_method_t null_methods[] = {
862
	KOBJMETHOD_END
863
864
};

865
DEFINE_CLASS(null, null_methods, 0);
866

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
 * Bus pass implementation
 */

static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
int bus_current_pass = BUS_PASS_ROOT;

/**
 * @internal
 * @brief Register the pass level of a new driver attachment
 *
 * Register a new driver attachment's pass level.  If no driver
 * attachment with the same pass level has been added, then @p new
 * will be added to the global passes list.
 *
 * @param new		the new driver attachment
 */
static void
driver_register_pass(struct driverlink *new)
{
	struct driverlink *dl;

	/* We only consider pass numbers during boot. */
	if (bus_current_pass == BUS_PASS_DEFAULT)
		return;

	/*
	 * Walk the passes list.  If we already know about this pass
	 * then there is nothing to do.  If we don't, then insert this
	 * driver link into the list.
	 */
	TAILQ_FOREACH(dl, &passes, passlink) {
		if (dl->pass < new->pass)
			continue;
		if (dl->pass == new->pass)
			return;
		TAILQ_INSERT_BEFORE(dl, new, passlink);
		return;
	}
	TAILQ_INSERT_TAIL(&passes, new, passlink);
}

/**
 * @brief Raise the current bus pass
 *
 * Raise the current bus pass level to @p pass.  Call the BUS_NEW_PASS()
 * method on the root bus to kick off a new device tree scan for each
 * new pass level that has at least one driver.
 */
void
bus_set_pass(int pass)
{
	struct driverlink *dl;

	if (bus_current_pass > pass)
		panic("Attempt to lower bus pass level");

	TAILQ_FOREACH(dl, &passes, passlink) {
		/* Skip pass values below the current pass level. */
		if (dl->pass <= bus_current_pass)
			continue;

		/*
		 * Bail once we hit a driver with a pass level that is
		 * too high.
		 */
		if (dl->pass > pass)
			break;

		/*
		 * Raise the pass level to the next level and rescan
		 * the tree.
		 */
		bus_current_pass = dl->pass;
		BUS_NEW_PASS(root_bus);
	}

	/*
	 * If there isn't a driver registered for the requested pass,
	 * then bus_current_pass might still be less than 'pass'.  Set
	 * it to 'pass' in that case.
	 */
	if (bus_current_pass < pass)
		bus_current_pass = pass;
	KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
}

954
955
956
957
/*
 * Devclass implementation
 */

958
static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
959

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/**
 * @internal
 * @brief Find or create a device class
 *
 * If a device class with the name @p classname exists, return it,
 * otherwise if @p create is non-zero create and return a new device
 * class.
 *
 * If @p parentname is non-NULL, the parent of the devclass is set to
 * the devclass of that name.
 *
 * @param classname	the devclass name to find or create
 * @param parentname	the parent devclass name or @c NULL
 * @param create	non-zero to create a devclass
 */
975
static devclass_t
976
977
devclass_find_internal(const char *classname, const char *parentname,
		       int create)
978
{
979
	devclass_t dc;
980

981
982
983
	PDEBUG(("looking for %s", classname));
	if (!classname)
		return (NULL);
984

985
986
	TAILQ_FOREACH(dc, &devclasses, link) {
		if (!strcmp(dc->name, classname))
987
			break;
988
	}
989

990
991
	if (create && !dc) {
		PDEBUG(("creating %s", classname));
992
		dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
993
		    M_BUS, M_NOWAIT | M_ZERO);
994
995
		if (!dc)
			return (NULL);
996
		dc->parent = NULL;
997
998
999
1000
		dc->name = (char*) (dc + 1);
		strcpy(dc->name, classname);
		TAILQ_INIT(&dc->drivers);
		TAILQ_INSERT_TAIL(&devclasses, dc, link);
For faster browsing, not all history is shown. View entire blame