1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997,1998,2003 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include "opt_bus.h"
31 #include "opt_ddb.h"
32
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/domainset.h>
36 #include <sys/eventhandler.h>
37 #include <sys/filio.h>
38 #include <sys/jail.h>
39 #include <sys/lock.h>
40 #include <sys/kernel.h>
41 #include <sys/kobj.h>
42 #include <sys/limits.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/poll.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/condvar.h>
50 #include <sys/queue.h>
51 #include <machine/bus.h>
52 #include <sys/random.h>
53 #include <sys/rman.h>
54 #include <sys/sbuf.h>
55 #include <sys/selinfo.h>
56 #include <sys/signalvar.h>
57 #include <sys/smp.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/uio.h>
61 #include <sys/bus.h>
62 #include <sys/cpuset.h>
63
64 #include <net/vnet.h>
65
66 #include <machine/cpu.h>
67 #include <machine/stdarg.h>
68
69 #include <vm/uma.h>
70 #include <vm/vm.h>
71
72 #include <ddb/ddb.h>
73
74 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
75 NULL);
76 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
77 NULL);
78
79 /*
80 * Used to attach drivers to devclasses.
81 */
82 typedef struct driverlink *driverlink_t;
83 struct driverlink {
84 kobj_class_t driver;
85 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
86 int pass;
87 int flags;
88 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */
89 TAILQ_ENTRY(driverlink) passlink;
90 };
91
92 /*
93 * Forward declarations
94 */
95 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
96 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
97 typedef TAILQ_HEAD(device_list, _device) device_list_t;
98
99 struct devclass {
100 TAILQ_ENTRY(devclass) link;
101 devclass_t parent; /* parent in devclass hierarchy */
102 driver_list_t drivers; /* bus devclasses store drivers for bus */
103 char *name;
104 device_t *devices; /* array of devices indexed by unit */
105 int maxunit; /* size of devices array */
106 int flags;
107 #define DC_HAS_CHILDREN 1
108
109 struct sysctl_ctx_list sysctl_ctx;
110 struct sysctl_oid *sysctl_tree;
111 };
112
113 /**
114 * @brief Implementation of _device.
115 *
116 * The structure is named "_device" instead of "device" to avoid type confusion
117 * caused by other subsystems defining a (struct device).
118 */
119 struct _device {
120 /*
121 * A device is a kernel object. The first field must be the
122 * current ops table for the object.
123 */
124 KOBJ_FIELDS;
125
126 /*
127 * Device hierarchy.
128 */
129 TAILQ_ENTRY(_device) link; /**< list of devices in parent */
130 TAILQ_ENTRY(_device) devlink; /**< global device list membership */
131 device_t parent; /**< parent of this device */
132 device_list_t children; /**< list of child devices */
133
134 /*
135 * Details of this device.
136 */
137 driver_t *driver; /**< current driver */
138 devclass_t devclass; /**< current device class */
139 int unit; /**< current unit number */
140 char* nameunit; /**< name+unit e.g. foodev0 */
141 char* desc; /**< driver specific description */
142 int busy; /**< count of calls to device_busy() */
143 device_state_t state; /**< current device state */
144 uint32_t devflags; /**< api level flags for device_get_flags() */
145 u_int flags; /**< internal device flags */
146 u_int order; /**< order from device_add_child_ordered() */
147 void *ivars; /**< instance variables */
148 void *softc; /**< current driver's variables */
149
150 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
151 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
152 };
153
154 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
155 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
156
157 EVENTHANDLER_LIST_DEFINE(device_attach);
158 EVENTHANDLER_LIST_DEFINE(device_detach);
159 EVENTHANDLER_LIST_DEFINE(dev_lookup);
160
161 static int bus_child_location_sb(device_t child, struct sbuf *sb);
162 static int bus_child_pnpinfo_sb(device_t child, struct sbuf *sb);
163 static void devctl2_init(void);
164 static bool device_frozen;
165
166 #define DRIVERNAME(d) ((d)? d->name : "no driver")
167 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
168
169 #ifdef BUS_DEBUG
170
171 static int bus_debug = 1;
172 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
173 "Bus debug level");
174 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
175 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
176
177 /**
178 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
179 * prevent syslog from deleting initial spaces
180 */
181 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
182
183 static void print_device_short(device_t dev, int indent);
184 static void print_device(device_t dev, int indent);
185 void print_device_tree_short(device_t dev, int indent);
186 void print_device_tree(device_t dev, int indent);
187 static void print_driver_short(driver_t *driver, int indent);
188 static void print_driver(driver_t *driver, int indent);
189 static void print_driver_list(driver_list_t drivers, int indent);
190 static void print_devclass_short(devclass_t dc, int indent);
191 static void print_devclass(devclass_t dc, int indent);
192 void print_devclass_list_short(void);
193 void print_devclass_list(void);
194
195 #else
196 /* Make the compiler ignore the function calls */
197 #define PDEBUG(a) /* nop */
198 #define DEVICENAME(d) /* nop */
199
200 #define print_device_short(d,i) /* nop */
201 #define print_device(d,i) /* nop */
202 #define print_device_tree_short(d,i) /* nop */
203 #define print_device_tree(d,i) /* nop */
204 #define print_driver_short(d,i) /* nop */
205 #define print_driver(d,i) /* nop */
206 #define print_driver_list(d,i) /* nop */
207 #define print_devclass_short(d,i) /* nop */
208 #define print_devclass(d,i) /* nop */
209 #define print_devclass_list_short() /* nop */
210 #define print_devclass_list() /* nop */
211 #endif
212
213 /*
214 * dev sysctl tree
215 */
216
217 enum {
218 DEVCLASS_SYSCTL_PARENT,
219 };
220
221 static int
devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)222 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
223 {
224 devclass_t dc = (devclass_t)arg1;
225 const char *value;
226
227 switch (arg2) {
228 case DEVCLASS_SYSCTL_PARENT:
229 value = dc->parent ? dc->parent->name : "";
230 break;
231 default:
232 return (EINVAL);
233 }
234 return (SYSCTL_OUT_STR(req, value));
235 }
236
237 static void
devclass_sysctl_init(devclass_t dc)238 devclass_sysctl_init(devclass_t dc)
239 {
240 if (dc->sysctl_tree != NULL)
241 return;
242 sysctl_ctx_init(&dc->sysctl_ctx);
243 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
244 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 OID_AUTO, "%parent",
248 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
249 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
250 "parent class");
251 }
252
253 enum {
254 DEVICE_SYSCTL_DESC,
255 DEVICE_SYSCTL_DRIVER,
256 DEVICE_SYSCTL_LOCATION,
257 DEVICE_SYSCTL_PNPINFO,
258 DEVICE_SYSCTL_PARENT,
259 };
260
261 static int
device_sysctl_handler(SYSCTL_HANDLER_ARGS)262 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
263 {
264 struct sbuf sb;
265 device_t dev = (device_t)arg1;
266 int error;
267
268 sbuf_new_for_sysctl(&sb, NULL, 1024, req);
269 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
270 switch (arg2) {
271 case DEVICE_SYSCTL_DESC:
272 sbuf_cat(&sb, dev->desc ? dev->desc : "");
273 break;
274 case DEVICE_SYSCTL_DRIVER:
275 sbuf_cat(&sb, dev->driver ? dev->driver->name : "");
276 break;
277 case DEVICE_SYSCTL_LOCATION:
278 bus_child_location_sb(dev, &sb);
279 break;
280 case DEVICE_SYSCTL_PNPINFO:
281 bus_child_pnpinfo_sb(dev, &sb);
282 break;
283 case DEVICE_SYSCTL_PARENT:
284 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
285 break;
286 default:
287 sbuf_delete(&sb);
288 return (EINVAL);
289 }
290 error = sbuf_finish(&sb);
291 sbuf_delete(&sb);
292 return (error);
293 }
294
295 static void
device_sysctl_init(device_t dev)296 device_sysctl_init(device_t dev)
297 {
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 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
307 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 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
312 "device description");
313 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
314 OID_AUTO, "%driver",
315 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
316 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
317 "device driver name");
318 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
319 OID_AUTO, "%location",
320 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
321 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
322 "device location relative to parent");
323 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
324 OID_AUTO, "%pnpinfo",
325 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
326 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
327 "device identification");
328 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
329 OID_AUTO, "%parent",
330 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
331 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
332 "parent device");
333 if (bus_get_domain(dev, &domain) == 0)
334 SYSCTL_ADD_INT(&dev->sysctl_ctx,
335 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
336 CTLFLAG_RD, NULL, domain, "NUMA domain");
337 }
338
339 static void
device_sysctl_update(device_t dev)340 device_sysctl_update(device_t dev)
341 {
342 devclass_t dc = dev->devclass;
343
344 if (dev->sysctl_tree == NULL)
345 return;
346 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
347 }
348
349 static void
device_sysctl_fini(device_t dev)350 device_sysctl_fini(device_t dev)
351 {
352 if (dev->sysctl_tree == NULL)
353 return;
354 sysctl_ctx_free(&dev->sysctl_ctx);
355 dev->sysctl_tree = NULL;
356 }
357
358 /*
359 * /dev/devctl implementation
360 */
361
362 /*
363 * This design allows only one reader for /dev/devctl. This is not desirable
364 * in the long run, but will get a lot of hair out of this implementation.
365 * Maybe we should make this device a clonable device.
366 *
367 * Also note: we specifically do not attach a device to the device_t tree
368 * to avoid potential chicken and egg problems. One could argue that all
369 * of this belongs to the root node.
370 */
371
372 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
373 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
374 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
375 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
376 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
377
378 static d_open_t devopen;
379 static d_close_t devclose;
380 static d_read_t devread;
381 static d_ioctl_t devioctl;
382 static d_poll_t devpoll;
383 static d_kqfilter_t devkqfilter;
384
385 static struct cdevsw dev_cdevsw = {
386 .d_version = D_VERSION,
387 .d_open = devopen,
388 .d_close = devclose,
389 .d_read = devread,
390 .d_ioctl = devioctl,
391 .d_poll = devpoll,
392 .d_kqfilter = devkqfilter,
393 .d_name = "devctl",
394 };
395
396 #define DEVCTL_BUFFER (1024 - sizeof(void *))
397 struct dev_event_info {
398 STAILQ_ENTRY(dev_event_info) dei_link;
399 char dei_data[DEVCTL_BUFFER];
400 };
401
402 STAILQ_HEAD(devq, dev_event_info);
403
404 static struct dev_softc {
405 int inuse;
406 int nonblock;
407 int queued;
408 int async;
409 struct mtx mtx;
410 struct cv cv;
411 struct selinfo sel;
412 struct devq devq;
413 struct sigio *sigio;
414 uma_zone_t zone;
415 } devsoftc;
416
417 static void filt_devctl_detach(struct knote *kn);
418 static int filt_devctl_read(struct knote *kn, long hint);
419
420 struct filterops devctl_rfiltops = {
421 .f_isfd = 1,
422 .f_detach = filt_devctl_detach,
423 .f_event = filt_devctl_read,
424 };
425
426 static struct cdev *devctl_dev;
427
428 static void
devinit(void)429 devinit(void)
430 {
431 int reserve;
432 uma_zone_t z;
433
434 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
435 UID_ROOT, GID_WHEEL, 0600, "devctl");
436 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
437 cv_init(&devsoftc.cv, "dev cv");
438 STAILQ_INIT(&devsoftc.devq);
439 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
440 if (devctl_queue_length > 0) {
441 /*
442 * Allocate a zone for the messages. Preallocate 2% of these for
443 * a reserve. Allow only devctl_queue_length slabs to cap memory
444 * usage. The reserve usually allows coverage of surges of
445 * events during memory shortages. Normally we won't have to
446 * re-use events from the queue, but will in extreme shortages.
447 */
448 z = devsoftc.zone = uma_zcreate("DEVCTL",
449 sizeof(struct dev_event_info), NULL, NULL, NULL, NULL,
450 UMA_ALIGN_PTR, 0);
451 reserve = max(devctl_queue_length / 50, 100); /* 2% reserve */
452 uma_zone_set_max(z, devctl_queue_length);
453 uma_zone_set_maxcache(z, 0);
454 uma_zone_reserve(z, reserve);
455 uma_prealloc(z, reserve);
456 }
457 devctl2_init();
458 }
459
460 static int
devopen(struct cdev * dev,int oflags,int devtype,struct thread * td)461 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
462 {
463 mtx_lock(&devsoftc.mtx);
464 if (devsoftc.inuse) {
465 mtx_unlock(&devsoftc.mtx);
466 return (EBUSY);
467 }
468 /* move to init */
469 devsoftc.inuse = 1;
470 mtx_unlock(&devsoftc.mtx);
471 return (0);
472 }
473
474 static int
devclose(struct cdev * dev,int fflag,int devtype,struct thread * td)475 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
476 {
477 mtx_lock(&devsoftc.mtx);
478 devsoftc.inuse = 0;
479 devsoftc.nonblock = 0;
480 devsoftc.async = 0;
481 cv_broadcast(&devsoftc.cv);
482 funsetown(&devsoftc.sigio);
483 mtx_unlock(&devsoftc.mtx);
484 return (0);
485 }
486
487 /*
488 * The read channel for this device is used to report changes to
489 * userland in realtime. We are required to free the data as well as
490 * the n1 object because we allocate them separately. Also note that
491 * 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 * programs are expected to cope.
494 */
495 static int
devread(struct cdev * dev,struct uio * uio,int ioflag)496 devread(struct cdev *dev, struct uio *uio, int ioflag)
497 {
498 struct dev_event_info *n1;
499 int rv;
500
501 mtx_lock(&devsoftc.mtx);
502 while (STAILQ_EMPTY(&devsoftc.devq)) {
503 if (devsoftc.nonblock) {
504 mtx_unlock(&devsoftc.mtx);
505 return (EAGAIN);
506 }
507 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
508 if (rv) {
509 /*
510 * Need to translate ERESTART to EINTR here? -- jake
511 */
512 mtx_unlock(&devsoftc.mtx);
513 return (rv);
514 }
515 }
516 n1 = STAILQ_FIRST(&devsoftc.devq);
517 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
518 devsoftc.queued--;
519 mtx_unlock(&devsoftc.mtx);
520 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
521 uma_zfree(devsoftc.zone, n1);
522 return (rv);
523 }
524
525 static int
devioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)526 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
527 {
528 switch (cmd) {
529 case FIONBIO:
530 if (*(int*)data)
531 devsoftc.nonblock = 1;
532 else
533 devsoftc.nonblock = 0;
534 return (0);
535 case FIOASYNC:
536 if (*(int*)data)
537 devsoftc.async = 1;
538 else
539 devsoftc.async = 0;
540 return (0);
541 case FIOSETOWN:
542 return fsetown(*(int *)data, &devsoftc.sigio);
543 case FIOGETOWN:
544 *(int *)data = fgetown(&devsoftc.sigio);
545 return (0);
546
547 /* (un)Support for other fcntl() calls. */
548 case FIOCLEX:
549 case FIONCLEX:
550 case FIONREAD:
551 default:
552 break;
553 }
554 return (ENOTTY);
555 }
556
557 static int
devpoll(struct cdev * dev,int events,struct thread * td)558 devpoll(struct cdev *dev, int events, struct thread *td)
559 {
560 int revents = 0;
561
562 mtx_lock(&devsoftc.mtx);
563 if (events & (POLLIN | POLLRDNORM)) {
564 if (!STAILQ_EMPTY(&devsoftc.devq))
565 revents = events & (POLLIN | POLLRDNORM);
566 else
567 selrecord(td, &devsoftc.sel);
568 }
569 mtx_unlock(&devsoftc.mtx);
570
571 return (revents);
572 }
573
574 static int
devkqfilter(struct cdev * dev,struct knote * kn)575 devkqfilter(struct cdev *dev, struct knote *kn)
576 {
577 int error;
578
579 if (kn->kn_filter == EVFILT_READ) {
580 kn->kn_fop = &devctl_rfiltops;
581 knlist_add(&devsoftc.sel.si_note, kn, 0);
582 error = 0;
583 } else
584 error = EINVAL;
585 return (error);
586 }
587
588 static void
filt_devctl_detach(struct knote * kn)589 filt_devctl_detach(struct knote *kn)
590 {
591 knlist_remove(&devsoftc.sel.si_note, kn, 0);
592 }
593
594 static int
filt_devctl_read(struct knote * kn,long hint)595 filt_devctl_read(struct knote *kn, long hint)
596 {
597 kn->kn_data = devsoftc.queued;
598 return (kn->kn_data != 0);
599 }
600
601 /**
602 * @brief Return whether the userland process is running
603 */
604 bool
devctl_process_running(void)605 devctl_process_running(void)
606 {
607 return (devsoftc.inuse == 1);
608 }
609
610 static struct dev_event_info *
devctl_alloc_dei(void)611 devctl_alloc_dei(void)
612 {
613 struct dev_event_info *dei = NULL;
614
615 mtx_lock(&devsoftc.mtx);
616 if (devctl_queue_length == 0)
617 goto out;
618 dei = uma_zalloc(devsoftc.zone, M_NOWAIT);
619 if (dei == NULL)
620 dei = uma_zalloc(devsoftc.zone, M_NOWAIT | M_USE_RESERVE);
621 if (dei == NULL) {
622 /*
623 * Guard against no items in the queue. Normally, this won't
624 * happen, but if lots of events happen all at once and there's
625 * a chance we're out of allocated space but none have yet been
626 * queued when we get here, leaving nothing to steal. This can
627 * also happen with error injection. Fail safe by returning
628 * NULL in that case..
629 */
630 if (devsoftc.queued == 0)
631 goto out;
632 dei = STAILQ_FIRST(&devsoftc.devq);
633 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
634 devsoftc.queued--;
635 }
636 MPASS(dei != NULL);
637 *dei->dei_data = '\0';
638 out:
639 mtx_unlock(&devsoftc.mtx);
640 return (dei);
641 }
642
643 static struct dev_event_info *
devctl_alloc_dei_sb(struct sbuf * sb)644 devctl_alloc_dei_sb(struct sbuf *sb)
645 {
646 struct dev_event_info *dei;
647
648 dei = devctl_alloc_dei();
649 if (dei != NULL)
650 sbuf_new(sb, dei->dei_data, sizeof(dei->dei_data), SBUF_FIXEDLEN);
651 return (dei);
652 }
653
654 static void
devctl_free_dei(struct dev_event_info * dei)655 devctl_free_dei(struct dev_event_info *dei)
656 {
657 uma_zfree(devsoftc.zone, dei);
658 }
659
660 static void
devctl_queue(struct dev_event_info * dei)661 devctl_queue(struct dev_event_info *dei)
662 {
663 mtx_lock(&devsoftc.mtx);
664 STAILQ_INSERT_TAIL(&devsoftc.devq, dei, dei_link);
665 devsoftc.queued++;
666 cv_broadcast(&devsoftc.cv);
667 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
668 mtx_unlock(&devsoftc.mtx);
669 selwakeup(&devsoftc.sel);
670 if (devsoftc.async && devsoftc.sigio != NULL)
671 pgsigio(&devsoftc.sigio, SIGIO, 0);
672 }
673
674 /**
675 * @brief Send a 'notification' to userland, using standard ways
676 */
677 void
devctl_notify(const char * system,const char * subsystem,const char * type,const char * data)678 devctl_notify(const char *system, const char *subsystem, const char *type,
679 const char *data)
680 {
681 struct dev_event_info *dei;
682 struct sbuf sb;
683
684 if (system == NULL || subsystem == NULL || type == NULL)
685 return;
686 dei = devctl_alloc_dei_sb(&sb);
687 if (dei == NULL)
688 return;
689 sbuf_cpy(&sb, "!system=");
690 sbuf_cat(&sb, system);
691 sbuf_cat(&sb, " subsystem=");
692 sbuf_cat(&sb, subsystem);
693 sbuf_cat(&sb, " type=");
694 sbuf_cat(&sb, type);
695 if (data != NULL) {
696 sbuf_putc(&sb, ' ');
697 sbuf_cat(&sb, data);
698 }
699 sbuf_putc(&sb, '\n');
700 if (sbuf_finish(&sb) != 0)
701 devctl_free_dei(dei); /* overflow -> drop it */
702 else
703 devctl_queue(dei);
704 }
705
706 /*
707 * Common routine that tries to make sending messages as easy as possible.
708 * We allocate memory for the data, copy strings into that, but do not
709 * free it unless there's an error. The dequeue part of the driver should
710 * free the data. We don't send data when the device is disabled. We do
711 * send data, even when we have no listeners, because we wish to avoid
712 * races relating to startup and restart of listening applications.
713 *
714 * devaddq is designed to string together the type of event, with the
715 * object of that event, plus the plug and play info and location info
716 * for that event. This is likely most useful for devices, but less
717 * useful for other consumers of this interface. Those should use
718 * the devctl_notify() interface instead.
719 *
720 * Output:
721 * ${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev)
722 */
723 static void
devaddq(const char * type,const char * what,device_t dev)724 devaddq(const char *type, const char *what, device_t dev)
725 {
726 struct dev_event_info *dei;
727 const char *parstr;
728 struct sbuf sb;
729
730 dei = devctl_alloc_dei_sb(&sb);
731 if (dei == NULL)
732 return;
733 sbuf_cpy(&sb, type);
734 sbuf_cat(&sb, what);
735 sbuf_cat(&sb, " at ");
736
737 /* Add in the location */
738 bus_child_location_sb(dev, &sb);
739 sbuf_putc(&sb, ' ');
740
741 /* Add in pnpinfo */
742 bus_child_pnpinfo_sb(dev, &sb);
743
744 /* Get the parent of this device, or / if high enough in the tree. */
745 if (device_get_parent(dev) == NULL)
746 parstr = "."; /* Or '/' ? */
747 else
748 parstr = device_get_nameunit(device_get_parent(dev));
749 sbuf_cat(&sb, " on ");
750 sbuf_cat(&sb, parstr);
751 sbuf_putc(&sb, '\n');
752 if (sbuf_finish(&sb) != 0)
753 goto bad;
754 devctl_queue(dei);
755 return;
756 bad:
757 devctl_free_dei(dei);
758 }
759
760 /*
761 * A device was added to the tree. We are called just after it successfully
762 * attaches (that is, probe and attach success for this device). No call
763 * is made if a device is merely parented into the tree. See devnomatch
764 * if probe fails. If attach fails, no notification is sent (but maybe
765 * we should have a different message for this).
766 */
767 static void
devadded(device_t dev)768 devadded(device_t dev)
769 {
770 devaddq("+", device_get_nameunit(dev), dev);
771 }
772
773 /*
774 * A device was removed from the tree. We are called just before this
775 * happens.
776 */
777 static void
devremoved(device_t dev)778 devremoved(device_t dev)
779 {
780 devaddq("-", device_get_nameunit(dev), dev);
781 }
782
783 /*
784 * Called when there's no match for this device. This is only called
785 * the first time that no match happens, so we don't keep getting this
786 * message. Should that prove to be undesirable, we can change it.
787 * This is called when all drivers that can attach to a given bus
788 * decline to accept this device. Other errors may not be detected.
789 */
790 static void
devnomatch(device_t dev)791 devnomatch(device_t dev)
792 {
793 devaddq("?", "", dev);
794 }
795
796 static int
sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)797 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
798 {
799 int q, error;
800
801 q = devctl_queue_length;
802 error = sysctl_handle_int(oidp, &q, 0, req);
803 if (error || !req->newptr)
804 return (error);
805 if (q < 0)
806 return (EINVAL);
807
808 /*
809 * When set as a tunable, we've not yet initialized the mutex.
810 * It is safe to just assign to devctl_queue_length and return
811 * as we're racing no one. We'll use whatever value set in
812 * devinit.
813 */
814 if (!mtx_initialized(&devsoftc.mtx)) {
815 devctl_queue_length = q;
816 return (0);
817 }
818
819 /*
820 * XXX It's hard to grow or shrink the UMA zone. Only allow
821 * disabling the queue size for the moment until underlying
822 * UMA issues can be sorted out.
823 */
824 if (q != 0)
825 return (EINVAL);
826 if (q == devctl_queue_length)
827 return (0);
828 mtx_lock(&devsoftc.mtx);
829 devctl_queue_length = 0;
830 uma_zdestroy(devsoftc.zone);
831 devsoftc.zone = 0;
832 mtx_unlock(&devsoftc.mtx);
833 return (0);
834 }
835
836 /**
837 * @brief safely quotes strings that might have double quotes in them.
838 *
839 * The devctl protocol relies on quoted strings having matching quotes.
840 * This routine quotes any internal quotes so the resulting string
841 * is safe to pass to snprintf to construct, for example pnp info strings.
842 *
843 * @param sb sbuf to place the characters into
844 * @param src Original buffer.
845 */
846 void
devctl_safe_quote_sb(struct sbuf * sb,const char * src)847 devctl_safe_quote_sb(struct sbuf *sb, const char *src)
848 {
849 while (*src != '\0') {
850 if (*src == '"' || *src == '\\')
851 sbuf_putc(sb, '\\');
852 sbuf_putc(sb, *src++);
853 }
854 }
855
856 /* End of /dev/devctl code */
857
858 static struct device_list bus_data_devices;
859 static int bus_data_generation = 1;
860
861 static kobj_method_t null_methods[] = {
862 KOBJMETHOD_END
863 };
864
865 DEFINE_CLASS(null, null_methods, 0);
866
867 struct mtx *
bus_topo_mtx(void)868 bus_topo_mtx(void)
869 {
870
871 return (&Giant);
872 }
873
874 void
bus_topo_lock(void)875 bus_topo_lock(void)
876 {
877
878 mtx_lock(bus_topo_mtx());
879 }
880
881 void
bus_topo_unlock(void)882 bus_topo_unlock(void)
883 {
884
885 mtx_unlock(bus_topo_mtx());
886 }
887
888 /*
889 * Bus pass implementation
890 */
891
892 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
893 int bus_current_pass = BUS_PASS_ROOT;
894
895 /**
896 * @internal
897 * @brief Register the pass level of a new driver attachment
898 *
899 * Register a new driver attachment's pass level. If no driver
900 * attachment with the same pass level has been added, then @p new
901 * will be added to the global passes list.
902 *
903 * @param new the new driver attachment
904 */
905 static void
driver_register_pass(struct driverlink * new)906 driver_register_pass(struct driverlink *new)
907 {
908 struct driverlink *dl;
909
910 /* We only consider pass numbers during boot. */
911 if (bus_current_pass == BUS_PASS_DEFAULT)
912 return;
913
914 /*
915 * Walk the passes list. If we already know about this pass
916 * then there is nothing to do. If we don't, then insert this
917 * driver link into the list.
918 */
919 TAILQ_FOREACH(dl, &passes, passlink) {
920 if (dl->pass < new->pass)
921 continue;
922 if (dl->pass == new->pass)
923 return;
924 TAILQ_INSERT_BEFORE(dl, new, passlink);
925 return;
926 }
927 TAILQ_INSERT_TAIL(&passes, new, passlink);
928 }
929
930 /**
931 * @brief Raise the current bus pass
932 *
933 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
934 * method on the root bus to kick off a new device tree scan for each
935 * new pass level that has at least one driver.
936 */
937 void
bus_set_pass(int pass)938 bus_set_pass(int pass)
939 {
940 struct driverlink *dl;
941
942 if (bus_current_pass > pass)
943 panic("Attempt to lower bus pass level");
944
945 TAILQ_FOREACH(dl, &passes, passlink) {
946 /* Skip pass values below the current pass level. */
947 if (dl->pass <= bus_current_pass)
948 continue;
949
950 /*
951 * Bail once we hit a driver with a pass level that is
952 * too high.
953 */
954 if (dl->pass > pass)
955 break;
956
957 /*
958 * Raise the pass level to the next level and rescan
959 * the tree.
960 */
961 bus_current_pass = dl->pass;
962 BUS_NEW_PASS(root_bus);
963 }
964
965 /*
966 * If there isn't a driver registered for the requested pass,
967 * then bus_current_pass might still be less than 'pass'. Set
968 * it to 'pass' in that case.
969 */
970 if (bus_current_pass < pass)
971 bus_current_pass = pass;
972 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
973 }
974
975 /*
976 * Devclass implementation
977 */
978
979 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
980
981 /**
982 * @internal
983 * @brief Find or create a device class
984 *
985 * If a device class with the name @p classname exists, return it,
986 * otherwise if @p create is non-zero create and return a new device
987 * class.
988 *
989 * If @p parentname is non-NULL, the parent of the devclass is set to
990 * the devclass of that name.
991 *
992 * @param classname the devclass name to find or create
993 * @param parentname the parent devclass name or @c NULL
994 * @param create non-zero to create a devclass
995 */
996 static devclass_t
devclass_find_internal(const char * classname,const char * parentname,int create)997 devclass_find_internal(const char *classname, const char *parentname,
998 int create)
999 {
1000 devclass_t dc;
1001
1002 PDEBUG(("looking for %s", classname));
1003 if (!classname)
1004 return (NULL);
1005
1006 TAILQ_FOREACH(dc, &devclasses, link) {
1007 if (!strcmp(dc->name, classname))
1008 break;
1009 }
1010
1011 if (create && !dc) {
1012 PDEBUG(("creating %s", classname));
1013 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
1014 M_BUS, M_NOWAIT | M_ZERO);
1015 if (!dc)
1016 return (NULL);
1017 dc->parent = NULL;
1018 dc->name = (char*) (dc + 1);
1019 strcpy(dc->name, classname);
1020 TAILQ_INIT(&dc->drivers);
1021 TAILQ_INSERT_TAIL(&devclasses, dc, link);
1022
1023 bus_data_generation_update();
1024 }
1025
1026 /*
1027 * If a parent class is specified, then set that as our parent so
1028 * that this devclass will support drivers for the parent class as
1029 * well. If the parent class has the same name don't do this though
1030 * as it creates a cycle that can trigger an infinite loop in
1031 * device_probe_child() if a device exists for which there is no
1032 * suitable driver.
1033 */
1034 if (parentname && dc && !dc->parent &&
1035 strcmp(classname, parentname) != 0) {
1036 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
1037 dc->parent->flags |= DC_HAS_CHILDREN;
1038 }
1039
1040 return (dc);
1041 }
1042
1043 /**
1044 * @brief Create a device class
1045 *
1046 * If a device class with the name @p classname exists, return it,
1047 * otherwise create and return a new device class.
1048 *
1049 * @param classname the devclass name to find or create
1050 */
1051 devclass_t
devclass_create(const char * classname)1052 devclass_create(const char *classname)
1053 {
1054 return (devclass_find_internal(classname, NULL, TRUE));
1055 }
1056
1057 /**
1058 * @brief Find a device class
1059 *
1060 * If a device class with the name @p classname exists, return it,
1061 * otherwise return @c NULL.
1062 *
1063 * @param classname the devclass name to find
1064 */
1065 devclass_t
devclass_find(const char * classname)1066 devclass_find(const char *classname)
1067 {
1068 return (devclass_find_internal(classname, NULL, FALSE));
1069 }
1070
1071 /**
1072 * @brief Register that a device driver has been added to a devclass
1073 *
1074 * Register that a device driver has been added to a devclass. This
1075 * is called by devclass_add_driver to accomplish the recursive
1076 * notification of all the children classes of dc, as well as dc.
1077 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1078 * the devclass.
1079 *
1080 * We do a full search here of the devclass list at each iteration
1081 * level to save storing children-lists in the devclass structure. If
1082 * we ever move beyond a few dozen devices doing this, we may need to
1083 * reevaluate...
1084 *
1085 * @param dc the devclass to edit
1086 * @param driver the driver that was just added
1087 */
1088 static void
devclass_driver_added(devclass_t dc,driver_t * driver)1089 devclass_driver_added(devclass_t dc, driver_t *driver)
1090 {
1091 devclass_t parent;
1092 int i;
1093
1094 /*
1095 * Call BUS_DRIVER_ADDED for any existing buses in this class.
1096 */
1097 for (i = 0; i < dc->maxunit; i++)
1098 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1099 BUS_DRIVER_ADDED(dc->devices[i], driver);
1100
1101 /*
1102 * Walk through the children classes. Since we only keep a
1103 * single parent pointer around, we walk the entire list of
1104 * devclasses looking for children. We set the
1105 * DC_HAS_CHILDREN flag when a child devclass is created on
1106 * the parent, so we only walk the list for those devclasses
1107 * that have children.
1108 */
1109 if (!(dc->flags & DC_HAS_CHILDREN))
1110 return;
1111 parent = dc;
1112 TAILQ_FOREACH(dc, &devclasses, link) {
1113 if (dc->parent == parent)
1114 devclass_driver_added(dc, driver);
1115 }
1116 }
1117
1118 /**
1119 * @brief Add a device driver to a device class
1120 *
1121 * Add a device driver to a devclass. This is normally called
1122 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1123 * all devices in the devclass will be called to allow them to attempt
1124 * to re-probe any unmatched children.
1125 *
1126 * @param dc the devclass to edit
1127 * @param driver the driver to register
1128 */
1129 int
devclass_add_driver(devclass_t dc,driver_t * driver,int pass,devclass_t * dcp)1130 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1131 {
1132 driverlink_t dl;
1133 devclass_t child_dc;
1134 const char *parentname;
1135
1136 PDEBUG(("%s", DRIVERNAME(driver)));
1137
1138 /* Don't allow invalid pass values. */
1139 if (pass <= BUS_PASS_ROOT)
1140 return (EINVAL);
1141
1142 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1143 if (!dl)
1144 return (ENOMEM);
1145
1146 /*
1147 * Compile the driver's methods. Also increase the reference count
1148 * so that the class doesn't get freed when the last instance
1149 * goes. This means we can safely use static methods and avoids a
1150 * double-free in devclass_delete_driver.
1151 */
1152 kobj_class_compile((kobj_class_t) driver);
1153
1154 /*
1155 * If the driver has any base classes, make the
1156 * devclass inherit from the devclass of the driver's
1157 * first base class. This will allow the system to
1158 * search for drivers in both devclasses for children
1159 * of a device using this driver.
1160 */
1161 if (driver->baseclasses)
1162 parentname = driver->baseclasses[0]->name;
1163 else
1164 parentname = NULL;
1165 child_dc = devclass_find_internal(driver->name, parentname, TRUE);
1166 if (dcp != NULL)
1167 *dcp = child_dc;
1168
1169 dl->driver = driver;
1170 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1171 driver->refs++; /* XXX: kobj_mtx */
1172 dl->pass = pass;
1173 driver_register_pass(dl);
1174
1175 if (device_frozen) {
1176 dl->flags |= DL_DEFERRED_PROBE;
1177 } else {
1178 devclass_driver_added(dc, driver);
1179 }
1180 bus_data_generation_update();
1181 return (0);
1182 }
1183
1184 /**
1185 * @brief Register that a device driver has been deleted from a devclass
1186 *
1187 * Register that a device driver has been removed from a devclass.
1188 * This is called by devclass_delete_driver to accomplish the
1189 * recursive notification of all the children classes of busclass, as
1190 * well as busclass. Each layer will attempt to detach the driver
1191 * from any devices that are children of the bus's devclass. The function
1192 * will return an error if a device fails to detach.
1193 *
1194 * We do a full search here of the devclass list at each iteration
1195 * level to save storing children-lists in the devclass structure. If
1196 * we ever move beyond a few dozen devices doing this, we may need to
1197 * reevaluate...
1198 *
1199 * @param busclass the devclass of the parent bus
1200 * @param dc the devclass of the driver being deleted
1201 * @param driver the driver being deleted
1202 */
1203 static int
devclass_driver_deleted(devclass_t busclass,devclass_t dc,driver_t * driver)1204 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1205 {
1206 devclass_t parent;
1207 device_t dev;
1208 int error, i;
1209
1210 /*
1211 * Disassociate from any devices. We iterate through all the
1212 * devices in the devclass of the driver and detach any which are
1213 * using the driver and which have a parent in the devclass which
1214 * we are deleting from.
1215 *
1216 * Note that since a driver can be in multiple devclasses, we
1217 * should not detach devices which are not children of devices in
1218 * the affected devclass.
1219 *
1220 * If we're frozen, we don't generate NOMATCH events. Mark to
1221 * generate later.
1222 */
1223 for (i = 0; i < dc->maxunit; i++) {
1224 if (dc->devices[i]) {
1225 dev = dc->devices[i];
1226 if (dev->driver == driver && dev->parent &&
1227 dev->parent->devclass == busclass) {
1228 if ((error = device_detach(dev)) != 0)
1229 return (error);
1230 if (device_frozen) {
1231 dev->flags &= ~DF_DONENOMATCH;
1232 dev->flags |= DF_NEEDNOMATCH;
1233 } else {
1234 BUS_PROBE_NOMATCH(dev->parent, dev);
1235 devnomatch(dev);
1236 dev->flags |= DF_DONENOMATCH;
1237 }
1238 }
1239 }
1240 }
1241
1242 /*
1243 * Walk through the children classes. Since we only keep a
1244 * single parent pointer around, we walk the entire list of
1245 * devclasses looking for children. We set the
1246 * DC_HAS_CHILDREN flag when a child devclass is created on
1247 * the parent, so we only walk the list for those devclasses
1248 * that have children.
1249 */
1250 if (!(busclass->flags & DC_HAS_CHILDREN))
1251 return (0);
1252 parent = busclass;
1253 TAILQ_FOREACH(busclass, &devclasses, link) {
1254 if (busclass->parent == parent) {
1255 error = devclass_driver_deleted(busclass, dc, driver);
1256 if (error)
1257 return (error);
1258 }
1259 }
1260 return (0);
1261 }
1262
1263 /**
1264 * @brief Delete a device driver from a device class
1265 *
1266 * Delete a device driver from a devclass. This is normally called
1267 * automatically by DRIVER_MODULE().
1268 *
1269 * If the driver is currently attached to any devices,
1270 * devclass_delete_driver() will first attempt to detach from each
1271 * device. If one of the detach calls fails, the driver will not be
1272 * deleted.
1273 *
1274 * @param dc the devclass to edit
1275 * @param driver the driver to unregister
1276 */
1277 int
devclass_delete_driver(devclass_t busclass,driver_t * driver)1278 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1279 {
1280 devclass_t dc = devclass_find(driver->name);
1281 driverlink_t dl;
1282 int error;
1283
1284 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1285
1286 if (!dc)
1287 return (0);
1288
1289 /*
1290 * Find the link structure in the bus' list of drivers.
1291 */
1292 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1293 if (dl->driver == driver)
1294 break;
1295 }
1296
1297 if (!dl) {
1298 PDEBUG(("%s not found in %s list", driver->name,
1299 busclass->name));
1300 return (ENOENT);
1301 }
1302
1303 error = devclass_driver_deleted(busclass, dc, driver);
1304 if (error != 0)
1305 return (error);
1306
1307 TAILQ_REMOVE(&busclass->drivers, dl, link);
1308 free(dl, M_BUS);
1309
1310 /* XXX: kobj_mtx */
1311 driver->refs--;
1312 if (driver->refs == 0)
1313 kobj_class_free((kobj_class_t) driver);
1314
1315 bus_data_generation_update();
1316 return (0);
1317 }
1318
1319 /**
1320 * @brief Quiesces a set of device drivers from a device class
1321 *
1322 * Quiesce a device driver from a devclass. This is normally called
1323 * automatically by DRIVER_MODULE().
1324 *
1325 * If the driver is currently attached to any devices,
1326 * devclass_quiesece_driver() will first attempt to quiesce each
1327 * device.
1328 *
1329 * @param dc the devclass to edit
1330 * @param driver the driver to unregister
1331 */
1332 static int
devclass_quiesce_driver(devclass_t busclass,driver_t * driver)1333 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1334 {
1335 devclass_t dc = devclass_find(driver->name);
1336 driverlink_t dl;
1337 device_t dev;
1338 int i;
1339 int error;
1340
1341 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1342
1343 if (!dc)
1344 return (0);
1345
1346 /*
1347 * Find the link structure in the bus' list of drivers.
1348 */
1349 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1350 if (dl->driver == driver)
1351 break;
1352 }
1353
1354 if (!dl) {
1355 PDEBUG(("%s not found in %s list", driver->name,
1356 busclass->name));
1357 return (ENOENT);
1358 }
1359
1360 /*
1361 * Quiesce all devices. We iterate through all the devices in
1362 * the devclass of the driver and quiesce any which are using
1363 * the driver and which have a parent in the devclass which we
1364 * are quiescing.
1365 *
1366 * Note that since a driver can be in multiple devclasses, we
1367 * should not quiesce devices which are not children of
1368 * devices in the affected devclass.
1369 */
1370 for (i = 0; i < dc->maxunit; i++) {
1371 if (dc->devices[i]) {
1372 dev = dc->devices[i];
1373 if (dev->driver == driver && dev->parent &&
1374 dev->parent->devclass == busclass) {
1375 if ((error = device_quiesce(dev)) != 0)
1376 return (error);
1377 }
1378 }
1379 }
1380
1381 return (0);
1382 }
1383
1384 /**
1385 * @internal
1386 */
1387 static driverlink_t
devclass_find_driver_internal(devclass_t dc,const char * classname)1388 devclass_find_driver_internal(devclass_t dc, const char *classname)
1389 {
1390 driverlink_t dl;
1391
1392 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1393
1394 TAILQ_FOREACH(dl, &dc->drivers, link) {
1395 if (!strcmp(dl->driver->name, classname))
1396 return (dl);
1397 }
1398
1399 PDEBUG(("not found"));
1400 return (NULL);
1401 }
1402
1403 /**
1404 * @brief Return the name of the devclass
1405 */
1406 const char *
devclass_get_name(devclass_t dc)1407 devclass_get_name(devclass_t dc)
1408 {
1409 return (dc->name);
1410 }
1411
1412 /**
1413 * @brief Find a device given a unit number
1414 *
1415 * @param dc the devclass to search
1416 * @param unit the unit number to search for
1417 *
1418 * @returns the device with the given unit number or @c
1419 * NULL if there is no such device
1420 */
1421 device_t
devclass_get_device(devclass_t dc,int unit)1422 devclass_get_device(devclass_t dc, int unit)
1423 {
1424 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1425 return (NULL);
1426 return (dc->devices[unit]);
1427 }
1428
1429 /**
1430 * @brief Find the softc field of a device given a unit number
1431 *
1432 * @param dc the devclass to search
1433 * @param unit the unit number to search for
1434 *
1435 * @returns the softc field of the device with the given
1436 * unit number or @c NULL if there is no such
1437 * device
1438 */
1439 void *
devclass_get_softc(devclass_t dc,int unit)1440 devclass_get_softc(devclass_t dc, int unit)
1441 {
1442 device_t dev;
1443
1444 dev = devclass_get_device(dc, unit);
1445 if (!dev)
1446 return (NULL);
1447
1448 return (device_get_softc(dev));
1449 }
1450
1451 /**
1452 * @brief Get a list of devices in the devclass
1453 *
1454 * An array containing a list of all the devices in the given devclass
1455 * is allocated and returned in @p *devlistp. The number of devices
1456 * in the array is returned in @p *devcountp. The caller should free
1457 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1458 *
1459 * @param dc the devclass to examine
1460 * @param devlistp points at location for array pointer return
1461 * value
1462 * @param devcountp points at location for array size return value
1463 *
1464 * @retval 0 success
1465 * @retval ENOMEM the array allocation failed
1466 */
1467 int
devclass_get_devices(devclass_t dc,device_t ** devlistp,int * devcountp)1468 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1469 {
1470 int count, i;
1471 device_t *list;
1472
1473 count = devclass_get_count(dc);
1474 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1475 if (!list)
1476 return (ENOMEM);
1477
1478 count = 0;
1479 for (i = 0; i < dc->maxunit; i++) {
1480 if (dc->devices[i]) {
1481 list[count] = dc->devices[i];
1482 count++;
1483 }
1484 }
1485
1486 *devlistp = list;
1487 *devcountp = count;
1488
1489 return (0);
1490 }
1491
1492 /**
1493 * @brief Get a list of drivers in the devclass
1494 *
1495 * An array containing a list of pointers to all the drivers in the
1496 * given devclass is allocated and returned in @p *listp. The number
1497 * of drivers in the array is returned in @p *countp. The caller should
1498 * free the array using @c free(p, M_TEMP).
1499 *
1500 * @param dc the devclass to examine
1501 * @param listp gives location for array pointer return value
1502 * @param countp gives location for number of array elements
1503 * return value
1504 *
1505 * @retval 0 success
1506 * @retval ENOMEM the array allocation failed
1507 */
1508 int
devclass_get_drivers(devclass_t dc,driver_t *** listp,int * countp)1509 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1510 {
1511 driverlink_t dl;
1512 driver_t **list;
1513 int count;
1514
1515 count = 0;
1516 TAILQ_FOREACH(dl, &dc->drivers, link)
1517 count++;
1518 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1519 if (list == NULL)
1520 return (ENOMEM);
1521
1522 count = 0;
1523 TAILQ_FOREACH(dl, &dc->drivers, link) {
1524 list[count] = dl->driver;
1525 count++;
1526 }
1527 *listp = list;
1528 *countp = count;
1529
1530 return (0);
1531 }
1532
1533 /**
1534 * @brief Get the number of devices in a devclass
1535 *
1536 * @param dc the devclass to examine
1537 */
1538 int
devclass_get_count(devclass_t dc)1539 devclass_get_count(devclass_t dc)
1540 {
1541 int count, i;
1542
1543 count = 0;
1544 for (i = 0; i < dc->maxunit; i++)
1545 if (dc->devices[i])
1546 count++;
1547 return (count);
1548 }
1549
1550 /**
1551 * @brief Get the maximum unit number used in a devclass
1552 *
1553 * Note that this is one greater than the highest currently-allocated
1554 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1555 * that not even the devclass has been allocated yet.
1556 *
1557 * @param dc the devclass to examine
1558 */
1559 int
devclass_get_maxunit(devclass_t dc)1560 devclass_get_maxunit(devclass_t dc)
1561 {
1562 if (dc == NULL)
1563 return (-1);
1564 return (dc->maxunit);
1565 }
1566
1567 /**
1568 * @brief Find a free unit number in a devclass
1569 *
1570 * This function searches for the first unused unit number greater
1571 * that or equal to @p unit.
1572 *
1573 * @param dc the devclass to examine
1574 * @param unit the first unit number to check
1575 */
1576 int
devclass_find_free_unit(devclass_t dc,int unit)1577 devclass_find_free_unit(devclass_t dc, int unit)
1578 {
1579 if (dc == NULL)
1580 return (unit);
1581 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1582 unit++;
1583 return (unit);
1584 }
1585
1586 /**
1587 * @brief Set the parent of a devclass
1588 *
1589 * The parent class is normally initialised automatically by
1590 * DRIVER_MODULE().
1591 *
1592 * @param dc the devclass to edit
1593 * @param pdc the new parent devclass
1594 */
1595 void
devclass_set_parent(devclass_t dc,devclass_t pdc)1596 devclass_set_parent(devclass_t dc, devclass_t pdc)
1597 {
1598 dc->parent = pdc;
1599 }
1600
1601 /**
1602 * @brief Get the parent of a devclass
1603 *
1604 * @param dc the devclass to examine
1605 */
1606 devclass_t
devclass_get_parent(devclass_t dc)1607 devclass_get_parent(devclass_t dc)
1608 {
1609 return (dc->parent);
1610 }
1611
1612 struct sysctl_ctx_list *
devclass_get_sysctl_ctx(devclass_t dc)1613 devclass_get_sysctl_ctx(devclass_t dc)
1614 {
1615 return (&dc->sysctl_ctx);
1616 }
1617
1618 struct sysctl_oid *
devclass_get_sysctl_tree(devclass_t dc)1619 devclass_get_sysctl_tree(devclass_t dc)
1620 {
1621 return (dc->sysctl_tree);
1622 }
1623
1624 /**
1625 * @internal
1626 * @brief Allocate a unit number
1627 *
1628 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1629 * will do). The allocated unit number is returned in @p *unitp.
1630
1631 * @param dc the devclass to allocate from
1632 * @param unitp points at the location for the allocated unit
1633 * number
1634 *
1635 * @retval 0 success
1636 * @retval EEXIST the requested unit number is already allocated
1637 * @retval ENOMEM memory allocation failure
1638 */
1639 static int
devclass_alloc_unit(devclass_t dc,device_t dev,int * unitp)1640 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1641 {
1642 const char *s;
1643 int unit = *unitp;
1644
1645 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1646
1647 /* Ask the parent bus if it wants to wire this device. */
1648 if (unit == -1)
1649 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1650 &unit);
1651
1652 /* If we were given a wired unit number, check for existing device */
1653 /* XXX imp XXX */
1654 if (unit != -1) {
1655 if (unit >= 0 && unit < dc->maxunit &&
1656 dc->devices[unit] != NULL) {
1657 if (bootverbose)
1658 printf("%s: %s%d already exists; skipping it\n",
1659 dc->name, dc->name, *unitp);
1660 return (EEXIST);
1661 }
1662 } else {
1663 /* Unwired device, find the next available slot for it */
1664 unit = 0;
1665 for (unit = 0;; unit++) {
1666 /* If this device slot is already in use, skip it. */
1667 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1668 continue;
1669
1670 /* If there is an "at" hint for a unit then skip it. */
1671 if (resource_string_value(dc->name, unit, "at", &s) ==
1672 0)
1673 continue;
1674
1675 break;
1676 }
1677 }
1678
1679 /*
1680 * We've selected a unit beyond the length of the table, so let's
1681 * extend the table to make room for all units up to and including
1682 * this one.
1683 */
1684 if (unit >= dc->maxunit) {
1685 device_t *newlist, *oldlist;
1686 int newsize;
1687
1688 oldlist = dc->devices;
1689 newsize = roundup((unit + 1),
1690 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1691 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1692 if (!newlist)
1693 return (ENOMEM);
1694 if (oldlist != NULL)
1695 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1696 bzero(newlist + dc->maxunit,
1697 sizeof(device_t) * (newsize - dc->maxunit));
1698 dc->devices = newlist;
1699 dc->maxunit = newsize;
1700 if (oldlist != NULL)
1701 free(oldlist, M_BUS);
1702 }
1703 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1704
1705 *unitp = unit;
1706 return (0);
1707 }
1708
1709 /**
1710 * @internal
1711 * @brief Add a device to a devclass
1712 *
1713 * A unit number is allocated for the device (using the device's
1714 * preferred unit number if any) and the device is registered in the
1715 * devclass. This allows the device to be looked up by its unit
1716 * number, e.g. by decoding a dev_t minor number.
1717 *
1718 * @param dc the devclass to add to
1719 * @param dev the device to add
1720 *
1721 * @retval 0 success
1722 * @retval EEXIST the requested unit number is already allocated
1723 * @retval ENOMEM memory allocation failure
1724 */
1725 static int
devclass_add_device(devclass_t dc,device_t dev)1726 devclass_add_device(devclass_t dc, device_t dev)
1727 {
1728 int buflen, error;
1729
1730 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1731
1732 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1733 if (buflen < 0)
1734 return (ENOMEM);
1735 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1736 if (!dev->nameunit)
1737 return (ENOMEM);
1738
1739 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1740 free(dev->nameunit, M_BUS);
1741 dev->nameunit = NULL;
1742 return (error);
1743 }
1744 dc->devices[dev->unit] = dev;
1745 dev->devclass = dc;
1746 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1747
1748 return (0);
1749 }
1750
1751 /**
1752 * @internal
1753 * @brief Delete a device from a devclass
1754 *
1755 * The device is removed from the devclass's device list and its unit
1756 * number is freed.
1757
1758 * @param dc the devclass to delete from
1759 * @param dev the device to delete
1760 *
1761 * @retval 0 success
1762 */
1763 static int
devclass_delete_device(devclass_t dc,device_t dev)1764 devclass_delete_device(devclass_t dc, device_t dev)
1765 {
1766 if (!dc || !dev)
1767 return (0);
1768
1769 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1770
1771 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1772 panic("devclass_delete_device: inconsistent device class");
1773 dc->devices[dev->unit] = NULL;
1774 if (dev->flags & DF_WILDCARD)
1775 dev->unit = -1;
1776 dev->devclass = NULL;
1777 free(dev->nameunit, M_BUS);
1778 dev->nameunit = NULL;
1779
1780 return (0);
1781 }
1782
1783 /**
1784 * @internal
1785 * @brief Make a new device and add it as a child of @p parent
1786 *
1787 * @param parent the parent of the new device
1788 * @param name the devclass name of the new device or @c NULL
1789 * to leave the devclass unspecified
1790 * @parem unit the unit number of the new device of @c -1 to
1791 * leave the unit number unspecified
1792 *
1793 * @returns the new device
1794 */
1795 static device_t
make_device(device_t parent,const char * name,int unit)1796 make_device(device_t parent, const char *name, int unit)
1797 {
1798 device_t dev;
1799 devclass_t dc;
1800
1801 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1802
1803 if (name) {
1804 dc = devclass_find_internal(name, NULL, TRUE);
1805 if (!dc) {
1806 printf("make_device: can't find device class %s\n",
1807 name);
1808 return (NULL);
1809 }
1810 } else {
1811 dc = NULL;
1812 }
1813
1814 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1815 if (!dev)
1816 return (NULL);
1817
1818 dev->parent = parent;
1819 TAILQ_INIT(&dev->children);
1820 kobj_init((kobj_t) dev, &null_class);
1821 dev->driver = NULL;
1822 dev->devclass = NULL;
1823 dev->unit = unit;
1824 dev->nameunit = NULL;
1825 dev->desc = NULL;
1826 dev->busy = 0;
1827 dev->devflags = 0;
1828 dev->flags = DF_ENABLED;
1829 dev->order = 0;
1830 if (unit == -1)
1831 dev->flags |= DF_WILDCARD;
1832 if (name) {
1833 dev->flags |= DF_FIXEDCLASS;
1834 if (devclass_add_device(dc, dev)) {
1835 kobj_delete((kobj_t) dev, M_BUS);
1836 return (NULL);
1837 }
1838 }
1839 if (parent != NULL && device_has_quiet_children(parent))
1840 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1841 dev->ivars = NULL;
1842 dev->softc = NULL;
1843
1844 dev->state = DS_NOTPRESENT;
1845
1846 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1847 bus_data_generation_update();
1848
1849 return (dev);
1850 }
1851
1852 /**
1853 * @internal
1854 * @brief Print a description of a device.
1855 */
1856 static int
device_print_child(device_t dev,device_t child)1857 device_print_child(device_t dev, device_t child)
1858 {
1859 int retval = 0;
1860
1861 if (device_is_alive(child))
1862 retval += BUS_PRINT_CHILD(dev, child);
1863 else
1864 retval += device_printf(child, " not found\n");
1865
1866 return (retval);
1867 }
1868
1869 /**
1870 * @brief Create a new device
1871 *
1872 * This creates a new device and adds it as a child of an existing
1873 * parent device. The new device will be added after the last existing
1874 * child with order zero.
1875 *
1876 * @param dev the device which will be the parent of the
1877 * new child device
1878 * @param name devclass name for new device or @c NULL if not
1879 * specified
1880 * @param unit unit number for new device or @c -1 if not
1881 * specified
1882 *
1883 * @returns the new device
1884 */
1885 device_t
device_add_child(device_t dev,const char * name,int unit)1886 device_add_child(device_t dev, const char *name, int unit)
1887 {
1888 return (device_add_child_ordered(dev, 0, name, unit));
1889 }
1890
1891 /**
1892 * @brief Create a new device
1893 *
1894 * This creates a new device and adds it as a child of an existing
1895 * parent device. The new device will be added after the last existing
1896 * child with the same order.
1897 *
1898 * @param dev the device which will be the parent of the
1899 * new child device
1900 * @param order a value which is used to partially sort the
1901 * children of @p dev - devices created using
1902 * lower values of @p order appear first in @p
1903 * dev's list of children
1904 * @param name devclass name for new device or @c NULL if not
1905 * specified
1906 * @param unit unit number for new device or @c -1 if not
1907 * specified
1908 *
1909 * @returns the new device
1910 */
1911 device_t
device_add_child_ordered(device_t dev,u_int order,const char * name,int unit)1912 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1913 {
1914 device_t child;
1915 device_t place;
1916
1917 PDEBUG(("%s at %s with order %u as unit %d",
1918 name, DEVICENAME(dev), order, unit));
1919 KASSERT(name != NULL || unit == -1,
1920 ("child device with wildcard name and specific unit number"));
1921
1922 child = make_device(dev, name, unit);
1923 if (child == NULL)
1924 return (child);
1925 child->order = order;
1926
1927 TAILQ_FOREACH(place, &dev->children, link) {
1928 if (place->order > order)
1929 break;
1930 }
1931
1932 if (place) {
1933 /*
1934 * The device 'place' is the first device whose order is
1935 * greater than the new child.
1936 */
1937 TAILQ_INSERT_BEFORE(place, child, link);
1938 } else {
1939 /*
1940 * The new child's order is greater or equal to the order of
1941 * any existing device. Add the child to the tail of the list.
1942 */
1943 TAILQ_INSERT_TAIL(&dev->children, child, link);
1944 }
1945
1946 bus_data_generation_update();
1947 return (child);
1948 }
1949
1950 /**
1951 * @brief Delete a device
1952 *
1953 * This function deletes a device along with all of its children. If
1954 * the device currently has a driver attached to it, the device is
1955 * detached first using device_detach().
1956 *
1957 * @param dev the parent device
1958 * @param child the device to delete
1959 *
1960 * @retval 0 success
1961 * @retval non-zero a unit error code describing the error
1962 */
1963 int
device_delete_child(device_t dev,device_t child)1964 device_delete_child(device_t dev, device_t child)
1965 {
1966 int error;
1967 device_t grandchild;
1968
1969 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1970
1971 /*
1972 * Detach child. Ideally this cleans up any grandchild
1973 * devices.
1974 */
1975 if ((error = device_detach(child)) != 0)
1976 return (error);
1977
1978 /* Delete any grandchildren left after detach. */
1979 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1980 error = device_delete_child(child, grandchild);
1981 if (error)
1982 return (error);
1983 }
1984
1985 if (child->devclass)
1986 devclass_delete_device(child->devclass, child);
1987 if (child->parent)
1988 BUS_CHILD_DELETED(dev, child);
1989 TAILQ_REMOVE(&dev->children, child, link);
1990 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1991 kobj_delete((kobj_t) child, M_BUS);
1992
1993 bus_data_generation_update();
1994 return (0);
1995 }
1996
1997 /**
1998 * @brief Delete all children devices of the given device, if any.
1999 *
2000 * This function deletes all children devices of the given device, if
2001 * any, using the device_delete_child() function for each device it
2002 * finds. If a child device cannot be deleted, this function will
2003 * return an error code.
2004 *
2005 * @param dev the parent device
2006 *
2007 * @retval 0 success
2008 * @retval non-zero a device would not detach
2009 */
2010 int
device_delete_children(device_t dev)2011 device_delete_children(device_t dev)
2012 {
2013 device_t child;
2014 int error;
2015
2016 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
2017
2018 error = 0;
2019
2020 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
2021 error = device_delete_child(dev, child);
2022 if (error) {
2023 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
2024 break;
2025 }
2026 }
2027 return (error);
2028 }
2029
2030 /**
2031 * @brief Find a device given a unit number
2032 *
2033 * This is similar to devclass_get_devices() but only searches for
2034 * devices which have @p dev as a parent.
2035 *
2036 * @param dev the parent device to search
2037 * @param unit the unit number to search for. If the unit is -1,
2038 * return the first child of @p dev which has name
2039 * @p classname (that is, the one with the lowest unit.)
2040 *
2041 * @returns the device with the given unit number or @c
2042 * NULL if there is no such device
2043 */
2044 device_t
device_find_child(device_t dev,const char * classname,int unit)2045 device_find_child(device_t dev, const char *classname, int unit)
2046 {
2047 devclass_t dc;
2048 device_t child;
2049
2050 dc = devclass_find(classname);
2051 if (!dc)
2052 return (NULL);
2053
2054 if (unit != -1) {
2055 child = devclass_get_device(dc, unit);
2056 if (child && child->parent == dev)
2057 return (child);
2058 } else {
2059 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2060 child = devclass_get_device(dc, unit);
2061 if (child && child->parent == dev)
2062 return (child);
2063 }
2064 }
2065 return (NULL);
2066 }
2067
2068 /**
2069 * @internal
2070 */
2071 static driverlink_t
first_matching_driver(devclass_t dc,device_t dev)2072 first_matching_driver(devclass_t dc, device_t dev)
2073 {
2074 if (dev->devclass)
2075 return (devclass_find_driver_internal(dc, dev->devclass->name));
2076 return (TAILQ_FIRST(&dc->drivers));
2077 }
2078
2079 /**
2080 * @internal
2081 */
2082 static driverlink_t
next_matching_driver(devclass_t dc,device_t dev,driverlink_t last)2083 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2084 {
2085 if (dev->devclass) {
2086 driverlink_t dl;
2087 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2088 if (!strcmp(dev->devclass->name, dl->driver->name))
2089 return (dl);
2090 return (NULL);
2091 }
2092 return (TAILQ_NEXT(last, link));
2093 }
2094
2095 /**
2096 * @internal
2097 */
2098 int
device_probe_child(device_t dev,device_t child)2099 device_probe_child(device_t dev, device_t child)
2100 {
2101 devclass_t dc;
2102 driverlink_t best = NULL;
2103 driverlink_t dl;
2104 int result, pri = 0;
2105 /* We should preserve the devclass (or lack of) set by the bus. */
2106 int hasclass = (child->devclass != NULL);
2107
2108 GIANT_REQUIRED;
2109
2110 dc = dev->devclass;
2111 if (!dc)
2112 panic("device_probe_child: parent device has no devclass");
2113
2114 /*
2115 * If the state is already probed, then return.
2116 */
2117 if (child->state == DS_ALIVE)
2118 return (0);
2119
2120 for (; dc; dc = dc->parent) {
2121 for (dl = first_matching_driver(dc, child);
2122 dl;
2123 dl = next_matching_driver(dc, child, dl)) {
2124 /* If this driver's pass is too high, then ignore it. */
2125 if (dl->pass > bus_current_pass)
2126 continue;
2127
2128 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2129 result = device_set_driver(child, dl->driver);
2130 if (result == ENOMEM)
2131 return (result);
2132 else if (result != 0)
2133 continue;
2134 if (!hasclass) {
2135 if (device_set_devclass(child,
2136 dl->driver->name) != 0) {
2137 char const * devname =
2138 device_get_name(child);
2139 if (devname == NULL)
2140 devname = "(unknown)";
2141 printf("driver bug: Unable to set "
2142 "devclass (class: %s "
2143 "devname: %s)\n",
2144 dl->driver->name,
2145 devname);
2146 (void)device_set_driver(child, NULL);
2147 continue;
2148 }
2149 }
2150
2151 /* Fetch any flags for the device before probing. */
2152 resource_int_value(dl->driver->name, child->unit,
2153 "flags", &child->devflags);
2154
2155 result = DEVICE_PROBE(child);
2156
2157 /*
2158 * If the driver returns SUCCESS, there can be
2159 * no higher match for this device.
2160 */
2161 if (result == 0) {
2162 best = dl;
2163 pri = 0;
2164 break;
2165 }
2166
2167 /* Reset flags and devclass before the next probe. */
2168 child->devflags = 0;
2169 if (!hasclass)
2170 (void)device_set_devclass(child, NULL);
2171
2172 /*
2173 * Reset DF_QUIET in case this driver doesn't
2174 * end up as the best driver.
2175 */
2176 device_verbose(child);
2177
2178 /*
2179 * Probes that return BUS_PROBE_NOWILDCARD or lower
2180 * only match on devices whose driver was explicitly
2181 * specified.
2182 */
2183 if (result <= BUS_PROBE_NOWILDCARD &&
2184 !(child->flags & DF_FIXEDCLASS)) {
2185 result = ENXIO;
2186 }
2187
2188 /*
2189 * The driver returned an error so it
2190 * certainly doesn't match.
2191 */
2192 if (result > 0) {
2193 (void)device_set_driver(child, NULL);
2194 continue;
2195 }
2196
2197 /*
2198 * A priority lower than SUCCESS, remember the
2199 * best matching driver. Initialise the value
2200 * of pri for the first match.
2201 */
2202 if (best == NULL || result > pri) {
2203 best = dl;
2204 pri = result;
2205 continue;
2206 }
2207 }
2208 /*
2209 * If we have an unambiguous match in this devclass,
2210 * don't look in the parent.
2211 */
2212 if (best && pri == 0)
2213 break;
2214 }
2215
2216 if (best == NULL)
2217 return (ENXIO);
2218
2219 /*
2220 * If we found a driver, change state and initialise the devclass.
2221 */
2222 if (pri < 0) {
2223 /* Set the winning driver, devclass, and flags. */
2224 result = device_set_driver(child, best->driver);
2225 if (result != 0)
2226 return (result);
2227 if (!child->devclass) {
2228 result = device_set_devclass(child, best->driver->name);
2229 if (result != 0) {
2230 (void)device_set_driver(child, NULL);
2231 return (result);
2232 }
2233 }
2234 resource_int_value(best->driver->name, child->unit,
2235 "flags", &child->devflags);
2236
2237 /*
2238 * A bit bogus. Call the probe method again to make sure
2239 * that we have the right description.
2240 */
2241 result = DEVICE_PROBE(child);
2242 if (result > 0) {
2243 if (!hasclass)
2244 (void)device_set_devclass(child, NULL);
2245 (void)device_set_driver(child, NULL);
2246 return (result);
2247 }
2248 }
2249
2250 child->state = DS_ALIVE;
2251 bus_data_generation_update();
2252 return (0);
2253 }
2254
2255 /**
2256 * @brief Return the parent of a device
2257 */
2258 device_t
device_get_parent(device_t dev)2259 device_get_parent(device_t dev)
2260 {
2261 return (dev->parent);
2262 }
2263
2264 /**
2265 * @brief Get a list of children of a device
2266 *
2267 * An array containing a list of all the children of the given device
2268 * is allocated and returned in @p *devlistp. The number of devices
2269 * in the array is returned in @p *devcountp. The caller should free
2270 * the array using @c free(p, M_TEMP).
2271 *
2272 * @param dev the device to examine
2273 * @param devlistp points at location for array pointer return
2274 * value
2275 * @param devcountp points at location for array size return value
2276 *
2277 * @retval 0 success
2278 * @retval ENOMEM the array allocation failed
2279 */
2280 int
device_get_children(device_t dev,device_t ** devlistp,int * devcountp)2281 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2282 {
2283 int count;
2284 device_t child;
2285 device_t *list;
2286
2287 count = 0;
2288 TAILQ_FOREACH(child, &dev->children, link) {
2289 count++;
2290 }
2291 if (count == 0) {
2292 *devlistp = NULL;
2293 *devcountp = 0;
2294 return (0);
2295 }
2296
2297 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2298 if (!list)
2299 return (ENOMEM);
2300
2301 count = 0;
2302 TAILQ_FOREACH(child, &dev->children, link) {
2303 list[count] = child;
2304 count++;
2305 }
2306
2307 *devlistp = list;
2308 *devcountp = count;
2309
2310 return (0);
2311 }
2312
2313 /**
2314 * @brief Return the current driver for the device or @c NULL if there
2315 * is no driver currently attached
2316 */
2317 driver_t *
device_get_driver(device_t dev)2318 device_get_driver(device_t dev)
2319 {
2320 return (dev->driver);
2321 }
2322
2323 /**
2324 * @brief Return the current devclass for the device or @c NULL if
2325 * there is none.
2326 */
2327 devclass_t
device_get_devclass(device_t dev)2328 device_get_devclass(device_t dev)
2329 {
2330 return (dev->devclass);
2331 }
2332
2333 /**
2334 * @brief Return the name of the device's devclass or @c NULL if there
2335 * is none.
2336 */
2337 const char *
device_get_name(device_t dev)2338 device_get_name(device_t dev)
2339 {
2340 if (dev != NULL && dev->devclass)
2341 return (devclass_get_name(dev->devclass));
2342 return (NULL);
2343 }
2344
2345 /**
2346 * @brief Return a string containing the device's devclass name
2347 * followed by an ascii representation of the device's unit number
2348 * (e.g. @c "foo2").
2349 */
2350 const char *
device_get_nameunit(device_t dev)2351 device_get_nameunit(device_t dev)
2352 {
2353 return (dev->nameunit);
2354 }
2355
2356 /**
2357 * @brief Return the device's unit number.
2358 */
2359 int
device_get_unit(device_t dev)2360 device_get_unit(device_t dev)
2361 {
2362 return (dev->unit);
2363 }
2364
2365 /**
2366 * @brief Return the device's description string
2367 */
2368 const char *
device_get_desc(device_t dev)2369 device_get_desc(device_t dev)
2370 {
2371 return (dev->desc);
2372 }
2373
2374 /**
2375 * @brief Return the device's flags
2376 */
2377 uint32_t
device_get_flags(device_t dev)2378 device_get_flags(device_t dev)
2379 {
2380 return (dev->devflags);
2381 }
2382
2383 struct sysctl_ctx_list *
device_get_sysctl_ctx(device_t dev)2384 device_get_sysctl_ctx(device_t dev)
2385 {
2386 return (&dev->sysctl_ctx);
2387 }
2388
2389 struct sysctl_oid *
device_get_sysctl_tree(device_t dev)2390 device_get_sysctl_tree(device_t dev)
2391 {
2392 return (dev->sysctl_tree);
2393 }
2394
2395 /**
2396 * @brief Print the name of the device followed by a colon and a space
2397 *
2398 * @returns the number of characters printed
2399 */
2400 int
device_print_prettyname(device_t dev)2401 device_print_prettyname(device_t dev)
2402 {
2403 const char *name = device_get_name(dev);
2404
2405 if (name == NULL)
2406 return (printf("unknown: "));
2407 return (printf("%s%d: ", name, device_get_unit(dev)));
2408 }
2409
2410 /**
2411 * @brief Print the name of the device followed by a colon, a space
2412 * and the result of calling vprintf() with the value of @p fmt and
2413 * the following arguments.
2414 *
2415 * @returns the number of characters printed
2416 */
2417 int
device_printf(device_t dev,const char * fmt,...)2418 device_printf(device_t dev, const char * fmt, ...)
2419 {
2420 char buf[128];
2421 struct sbuf sb;
2422 const char *name;
2423 va_list ap;
2424 size_t retval;
2425
2426 retval = 0;
2427
2428 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2429 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
2430
2431 name = device_get_name(dev);
2432
2433 if (name == NULL)
2434 sbuf_cat(&sb, "unknown: ");
2435 else
2436 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2437
2438 va_start(ap, fmt);
2439 sbuf_vprintf(&sb, fmt, ap);
2440 va_end(ap);
2441
2442 sbuf_finish(&sb);
2443 sbuf_delete(&sb);
2444
2445 return (retval);
2446 }
2447
2448 /**
2449 * @brief Print the name of the device followed by a colon, a space
2450 * and the result of calling log() with the value of @p fmt and
2451 * the following arguments.
2452 *
2453 * @returns the number of characters printed
2454 */
2455 int
device_log(device_t dev,int pri,const char * fmt,...)2456 device_log(device_t dev, int pri, const char * fmt, ...)
2457 {
2458 char buf[128];
2459 struct sbuf sb;
2460 const char *name;
2461 va_list ap;
2462 size_t retval;
2463
2464 retval = 0;
2465
2466 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2467
2468 name = device_get_name(dev);
2469
2470 if (name == NULL)
2471 sbuf_cat(&sb, "unknown: ");
2472 else
2473 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2474
2475 va_start(ap, fmt);
2476 sbuf_vprintf(&sb, fmt, ap);
2477 va_end(ap);
2478
2479 sbuf_finish(&sb);
2480
2481 log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb));
2482 retval = sbuf_len(&sb);
2483
2484 sbuf_delete(&sb);
2485
2486 return (retval);
2487 }
2488
2489 /**
2490 * @internal
2491 */
2492 static void
device_set_desc_internal(device_t dev,const char * desc,int copy)2493 device_set_desc_internal(device_t dev, const char* desc, int copy)
2494 {
2495 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2496 free(dev->desc, M_BUS);
2497 dev->flags &= ~DF_DESCMALLOCED;
2498 dev->desc = NULL;
2499 }
2500
2501 if (copy && desc) {
2502 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2503 if (dev->desc) {
2504 strcpy(dev->desc, desc);
2505 dev->flags |= DF_DESCMALLOCED;
2506 }
2507 } else {
2508 /* Avoid a -Wcast-qual warning */
2509 dev->desc = (char *)(uintptr_t) desc;
2510 }
2511
2512 bus_data_generation_update();
2513 }
2514
2515 /**
2516 * @brief Set the device's description
2517 *
2518 * The value of @c desc should be a string constant that will not
2519 * change (at least until the description is changed in a subsequent
2520 * call to device_set_desc() or device_set_desc_copy()).
2521 */
2522 void
device_set_desc(device_t dev,const char * desc)2523 device_set_desc(device_t dev, const char* desc)
2524 {
2525 device_set_desc_internal(dev, desc, FALSE);
2526 }
2527
2528 /**
2529 * @brief Set the device's description
2530 *
2531 * The string pointed to by @c desc is copied. Use this function if
2532 * the device description is generated, (e.g. with sprintf()).
2533 */
2534 void
device_set_desc_copy(device_t dev,const char * desc)2535 device_set_desc_copy(device_t dev, const char* desc)
2536 {
2537 device_set_desc_internal(dev, desc, TRUE);
2538 }
2539
2540 /**
2541 * @brief Set the device's flags
2542 */
2543 void
device_set_flags(device_t dev,uint32_t flags)2544 device_set_flags(device_t dev, uint32_t flags)
2545 {
2546 dev->devflags = flags;
2547 }
2548
2549 /**
2550 * @brief Return the device's softc field
2551 *
2552 * The softc is allocated and zeroed when a driver is attached, based
2553 * on the size field of the driver.
2554 */
2555 void *
device_get_softc(device_t dev)2556 device_get_softc(device_t dev)
2557 {
2558 return (dev->softc);
2559 }
2560
2561 /**
2562 * @brief Set the device's softc field
2563 *
2564 * Most drivers do not need to use this since the softc is allocated
2565 * automatically when the driver is attached.
2566 */
2567 void
device_set_softc(device_t dev,void * softc)2568 device_set_softc(device_t dev, void *softc)
2569 {
2570 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2571 free(dev->softc, M_BUS_SC);
2572 dev->softc = softc;
2573 if (dev->softc)
2574 dev->flags |= DF_EXTERNALSOFTC;
2575 else
2576 dev->flags &= ~DF_EXTERNALSOFTC;
2577 }
2578
2579 /**
2580 * @brief Free claimed softc
2581 *
2582 * Most drivers do not need to use this since the softc is freed
2583 * automatically when the driver is detached.
2584 */
2585 void
device_free_softc(void * softc)2586 device_free_softc(void *softc)
2587 {
2588 free(softc, M_BUS_SC);
2589 }
2590
2591 /**
2592 * @brief Claim softc
2593 *
2594 * This function can be used to let the driver free the automatically
2595 * allocated softc using "device_free_softc()". This function is
2596 * useful when the driver is refcounting the softc and the softc
2597 * cannot be freed when the "device_detach" method is called.
2598 */
2599 void
device_claim_softc(device_t dev)2600 device_claim_softc(device_t dev)
2601 {
2602 if (dev->softc)
2603 dev->flags |= DF_EXTERNALSOFTC;
2604 else
2605 dev->flags &= ~DF_EXTERNALSOFTC;
2606 }
2607
2608 /**
2609 * @brief Get the device's ivars field
2610 *
2611 * The ivars field is used by the parent device to store per-device
2612 * state (e.g. the physical location of the device or a list of
2613 * resources).
2614 */
2615 void *
device_get_ivars(device_t dev)2616 device_get_ivars(device_t dev)
2617 {
2618 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2619 return (dev->ivars);
2620 }
2621
2622 /**
2623 * @brief Set the device's ivars field
2624 */
2625 void
device_set_ivars(device_t dev,void * ivars)2626 device_set_ivars(device_t dev, void * ivars)
2627 {
2628 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2629 dev->ivars = ivars;
2630 }
2631
2632 /**
2633 * @brief Return the device's state
2634 */
2635 device_state_t
device_get_state(device_t dev)2636 device_get_state(device_t dev)
2637 {
2638 return (dev->state);
2639 }
2640
2641 /**
2642 * @brief Set the DF_ENABLED flag for the device
2643 */
2644 void
device_enable(device_t dev)2645 device_enable(device_t dev)
2646 {
2647 dev->flags |= DF_ENABLED;
2648 }
2649
2650 /**
2651 * @brief Clear the DF_ENABLED flag for the device
2652 */
2653 void
device_disable(device_t dev)2654 device_disable(device_t dev)
2655 {
2656 dev->flags &= ~DF_ENABLED;
2657 }
2658
2659 /**
2660 * @brief Increment the busy counter for the device
2661 */
2662 void
device_busy(device_t dev)2663 device_busy(device_t dev)
2664 {
2665 if (dev->state < DS_ATTACHING)
2666 panic("device_busy: called for unattached device");
2667 if (dev->busy == 0 && dev->parent)
2668 device_busy(dev->parent);
2669 dev->busy++;
2670 if (dev->state == DS_ATTACHED)
2671 dev->state = DS_BUSY;
2672 }
2673
2674 /**
2675 * @brief Decrement the busy counter for the device
2676 */
2677 void
device_unbusy(device_t dev)2678 device_unbusy(device_t dev)
2679 {
2680 if (dev->busy != 0 && dev->state != DS_BUSY &&
2681 dev->state != DS_ATTACHING)
2682 panic("device_unbusy: called for non-busy device %s",
2683 device_get_nameunit(dev));
2684 dev->busy--;
2685 if (dev->busy == 0) {
2686 if (dev->parent)
2687 device_unbusy(dev->parent);
2688 if (dev->state == DS_BUSY)
2689 dev->state = DS_ATTACHED;
2690 }
2691 }
2692
2693 /**
2694 * @brief Set the DF_QUIET flag for the device
2695 */
2696 void
device_quiet(device_t dev)2697 device_quiet(device_t dev)
2698 {
2699 dev->flags |= DF_QUIET;
2700 }
2701
2702 /**
2703 * @brief Set the DF_QUIET_CHILDREN flag for the device
2704 */
2705 void
device_quiet_children(device_t dev)2706 device_quiet_children(device_t dev)
2707 {
2708 dev->flags |= DF_QUIET_CHILDREN;
2709 }
2710
2711 /**
2712 * @brief Clear the DF_QUIET flag for the device
2713 */
2714 void
device_verbose(device_t dev)2715 device_verbose(device_t dev)
2716 {
2717 dev->flags &= ~DF_QUIET;
2718 }
2719
2720 ssize_t
device_get_property(device_t dev,const char * prop,void * val,size_t sz,device_property_type_t type)2721 device_get_property(device_t dev, const char *prop, void *val, size_t sz,
2722 device_property_type_t type)
2723 {
2724 device_t bus = device_get_parent(dev);
2725
2726 switch (type) {
2727 case DEVICE_PROP_ANY:
2728 case DEVICE_PROP_BUFFER:
2729 case DEVICE_PROP_HANDLE: /* Size checks done in implementation. */
2730 break;
2731 case DEVICE_PROP_UINT32:
2732 if (sz % 4 != 0)
2733 return (-1);
2734 break;
2735 case DEVICE_PROP_UINT64:
2736 if (sz % 8 != 0)
2737 return (-1);
2738 break;
2739 default:
2740 return (-1);
2741 }
2742
2743 return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type));
2744 }
2745
2746 bool
device_has_property(device_t dev,const char * prop)2747 device_has_property(device_t dev, const char *prop)
2748 {
2749 return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0);
2750 }
2751
2752 /**
2753 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2754 */
2755 int
device_has_quiet_children(device_t dev)2756 device_has_quiet_children(device_t dev)
2757 {
2758 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2759 }
2760
2761 /**
2762 * @brief Return non-zero if the DF_QUIET flag is set on the device
2763 */
2764 int
device_is_quiet(device_t dev)2765 device_is_quiet(device_t dev)
2766 {
2767 return ((dev->flags & DF_QUIET) != 0);
2768 }
2769
2770 /**
2771 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2772 */
2773 int
device_is_enabled(device_t dev)2774 device_is_enabled(device_t dev)
2775 {
2776 return ((dev->flags & DF_ENABLED) != 0);
2777 }
2778
2779 /**
2780 * @brief Return non-zero if the device was successfully probed
2781 */
2782 int
device_is_alive(device_t dev)2783 device_is_alive(device_t dev)
2784 {
2785 return (dev->state >= DS_ALIVE);
2786 }
2787
2788 /**
2789 * @brief Return non-zero if the device currently has a driver
2790 * attached to it
2791 */
2792 int
device_is_attached(device_t dev)2793 device_is_attached(device_t dev)
2794 {
2795 return (dev->state >= DS_ATTACHED);
2796 }
2797
2798 /**
2799 * @brief Return non-zero if the device is currently suspended.
2800 */
2801 int
device_is_suspended(device_t dev)2802 device_is_suspended(device_t dev)
2803 {
2804 return ((dev->flags & DF_SUSPENDED) != 0);
2805 }
2806
2807 /**
2808 * @brief Set the devclass of a device
2809 * @see devclass_add_device().
2810 */
2811 int
device_set_devclass(device_t dev,const char * classname)2812 device_set_devclass(device_t dev, const char *classname)
2813 {
2814 devclass_t dc;
2815 int error;
2816
2817 if (!classname) {
2818 if (dev->devclass)
2819 devclass_delete_device(dev->devclass, dev);
2820 return (0);
2821 }
2822
2823 if (dev->devclass) {
2824 printf("device_set_devclass: device class already set\n");
2825 return (EINVAL);
2826 }
2827
2828 dc = devclass_find_internal(classname, NULL, TRUE);
2829 if (!dc)
2830 return (ENOMEM);
2831
2832 error = devclass_add_device(dc, dev);
2833
2834 bus_data_generation_update();
2835 return (error);
2836 }
2837
2838 /**
2839 * @brief Set the devclass of a device and mark the devclass fixed.
2840 * @see device_set_devclass()
2841 */
2842 int
device_set_devclass_fixed(device_t dev,const char * classname)2843 device_set_devclass_fixed(device_t dev, const char *classname)
2844 {
2845 int error;
2846
2847 if (classname == NULL)
2848 return (EINVAL);
2849
2850 error = device_set_devclass(dev, classname);
2851 if (error)
2852 return (error);
2853 dev->flags |= DF_FIXEDCLASS;
2854 return (0);
2855 }
2856
2857 /**
2858 * @brief Query the device to determine if it's of a fixed devclass
2859 * @see device_set_devclass_fixed()
2860 */
2861 bool
device_is_devclass_fixed(device_t dev)2862 device_is_devclass_fixed(device_t dev)
2863 {
2864 return ((dev->flags & DF_FIXEDCLASS) != 0);
2865 }
2866
2867 /**
2868 * @brief Set the driver of a device
2869 *
2870 * @retval 0 success
2871 * @retval EBUSY the device already has a driver attached
2872 * @retval ENOMEM a memory allocation failure occurred
2873 */
2874 int
device_set_driver(device_t dev,driver_t * driver)2875 device_set_driver(device_t dev, driver_t *driver)
2876 {
2877 int domain;
2878 struct domainset *policy;
2879
2880 if (dev->state >= DS_ATTACHED)
2881 return (EBUSY);
2882
2883 if (dev->driver == driver)
2884 return (0);
2885
2886 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2887 free(dev->softc, M_BUS_SC);
2888 dev->softc = NULL;
2889 }
2890 device_set_desc(dev, NULL);
2891 kobj_delete((kobj_t) dev, NULL);
2892 dev->driver = driver;
2893 if (driver) {
2894 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2895 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2896 if (bus_get_domain(dev, &domain) == 0)
2897 policy = DOMAINSET_PREF(domain);
2898 else
2899 policy = DOMAINSET_RR();
2900 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2901 policy, M_NOWAIT | M_ZERO);
2902 if (!dev->softc) {
2903 kobj_delete((kobj_t) dev, NULL);
2904 kobj_init((kobj_t) dev, &null_class);
2905 dev->driver = NULL;
2906 return (ENOMEM);
2907 }
2908 }
2909 } else {
2910 kobj_init((kobj_t) dev, &null_class);
2911 }
2912
2913 bus_data_generation_update();
2914 return (0);
2915 }
2916
2917 /**
2918 * @brief Probe a device, and return this status.
2919 *
2920 * This function is the core of the device autoconfiguration
2921 * system. Its purpose is to select a suitable driver for a device and
2922 * then call that driver to initialise the hardware appropriately. The
2923 * driver is selected by calling the DEVICE_PROBE() method of a set of
2924 * candidate drivers and then choosing the driver which returned the
2925 * best value. This driver is then attached to the device using
2926 * device_attach().
2927 *
2928 * The set of suitable drivers is taken from the list of drivers in
2929 * the parent device's devclass. If the device was originally created
2930 * with a specific class name (see device_add_child()), only drivers
2931 * with that name are probed, otherwise all drivers in the devclass
2932 * are probed. If no drivers return successful probe values in the
2933 * parent devclass, the search continues in the parent of that
2934 * devclass (see devclass_get_parent()) if any.
2935 *
2936 * @param dev the device to initialise
2937 *
2938 * @retval 0 success
2939 * @retval ENXIO no driver was found
2940 * @retval ENOMEM memory allocation failure
2941 * @retval non-zero some other unix error code
2942 * @retval -1 Device already attached
2943 */
2944 int
device_probe(device_t dev)2945 device_probe(device_t dev)
2946 {
2947 int error;
2948
2949 GIANT_REQUIRED;
2950
2951 if (dev->state >= DS_ALIVE)
2952 return (-1);
2953
2954 if (!(dev->flags & DF_ENABLED)) {
2955 if (bootverbose && device_get_name(dev) != NULL) {
2956 device_print_prettyname(dev);
2957 printf("not probed (disabled)\n");
2958 }
2959 return (-1);
2960 }
2961 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2962 if (bus_current_pass == BUS_PASS_DEFAULT &&
2963 !(dev->flags & DF_DONENOMATCH)) {
2964 BUS_PROBE_NOMATCH(dev->parent, dev);
2965 devnomatch(dev);
2966 dev->flags |= DF_DONENOMATCH;
2967 }
2968 return (error);
2969 }
2970 return (0);
2971 }
2972
2973 /**
2974 * @brief Probe a device and attach a driver if possible
2975 *
2976 * calls device_probe() and attaches if that was successful.
2977 */
2978 int
device_probe_and_attach(device_t dev)2979 device_probe_and_attach(device_t dev)
2980 {
2981 int error;
2982
2983 GIANT_REQUIRED;
2984
2985 error = device_probe(dev);
2986 if (error == -1)
2987 return (0);
2988 else if (error != 0)
2989 return (error);
2990
2991 return (device_attach(dev));
2992 }
2993
2994 /**
2995 * @brief Attach a device driver to a device
2996 *
2997 * This function is a wrapper around the DEVICE_ATTACH() driver
2998 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2999 * device's sysctl tree, optionally prints a description of the device
3000 * and queues a notification event for user-based device management
3001 * services.
3002 *
3003 * Normally this function is only called internally from
3004 * device_probe_and_attach().
3005 *
3006 * @param dev the device to initialise
3007 *
3008 * @retval 0 success
3009 * @retval ENXIO no driver was found
3010 * @retval ENOMEM memory allocation failure
3011 * @retval non-zero some other unix error code
3012 */
3013 int
device_attach(device_t dev)3014 device_attach(device_t dev)
3015 {
3016 uint64_t attachtime;
3017 uint16_t attachentropy;
3018 int error;
3019
3020 if (resource_disabled(dev->driver->name, dev->unit)) {
3021 /*
3022 * Mostly detach the device, but leave it attached to
3023 * the devclass to reserve the name and unit.
3024 */
3025 device_disable(dev);
3026 (void)device_set_driver(dev, NULL);
3027 dev->state = DS_NOTPRESENT;
3028 if (bootverbose)
3029 device_printf(dev, "disabled via hints entry\n");
3030 return (ENXIO);
3031 }
3032
3033 KASSERT(IS_DEFAULT_VNET(TD_TO_VNET(curthread)),
3034 ("device_attach: curthread is not in default vnet"));
3035 CURVNET_SET_QUIET(TD_TO_VNET(curthread));
3036
3037 device_sysctl_init(dev);
3038 if (!device_is_quiet(dev))
3039 device_print_child(dev->parent, dev);
3040 attachtime = get_cyclecount();
3041 dev->state = DS_ATTACHING;
3042 if ((error = DEVICE_ATTACH(dev)) != 0) {
3043 printf("device_attach: %s%d attach returned %d\n",
3044 dev->driver->name, dev->unit, error);
3045 BUS_CHILD_DETACHED(dev->parent, dev);
3046 if (!(dev->flags & DF_FIXEDCLASS))
3047 devclass_delete_device(dev->devclass, dev);
3048 (void)device_set_driver(dev, NULL);
3049 device_sysctl_fini(dev);
3050 KASSERT(dev->busy == 0, ("attach failed but busy"));
3051 dev->state = DS_NOTPRESENT;
3052 CURVNET_RESTORE();
3053 return (error);
3054 }
3055 CURVNET_RESTORE();
3056 dev->flags |= DF_ATTACHED_ONCE;
3057 /* We only need the low bits of this time, but ranges from tens to thousands
3058 * have been seen, so keep 2 bytes' worth.
3059 */
3060 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
3061 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
3062 device_sysctl_update(dev);
3063 if (dev->busy)
3064 dev->state = DS_BUSY;
3065 else
3066 dev->state = DS_ATTACHED;
3067 dev->flags &= ~DF_DONENOMATCH;
3068 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
3069 devadded(dev);
3070 return (0);
3071 }
3072
3073 /**
3074 * @brief Detach a driver from a device
3075 *
3076 * This function is a wrapper around the DEVICE_DETACH() driver
3077 * method. If the call to DEVICE_DETACH() succeeds, it calls
3078 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
3079 * notification event for user-based device management services and
3080 * cleans up the device's sysctl tree.
3081 *
3082 * @param dev the device to un-initialise
3083 *
3084 * @retval 0 success
3085 * @retval ENXIO no driver was found
3086 * @retval ENOMEM memory allocation failure
3087 * @retval non-zero some other unix error code
3088 */
3089 int
device_detach(device_t dev)3090 device_detach(device_t dev)
3091 {
3092 int error;
3093
3094 GIANT_REQUIRED;
3095
3096 PDEBUG(("%s", DEVICENAME(dev)));
3097 if (dev->state == DS_BUSY)
3098 return (EBUSY);
3099 if (dev->state == DS_ATTACHING) {
3100 device_printf(dev, "device in attaching state! Deferring detach.\n");
3101 return (EBUSY);
3102 }
3103 if (dev->state != DS_ATTACHED)
3104 return (0);
3105
3106 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
3107 if ((error = DEVICE_DETACH(dev)) != 0) {
3108 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
3109 EVHDEV_DETACH_FAILED);
3110 return (error);
3111 } else {
3112 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
3113 EVHDEV_DETACH_COMPLETE);
3114 }
3115 devremoved(dev);
3116 if (!device_is_quiet(dev))
3117 device_printf(dev, "detached\n");
3118 if (dev->parent)
3119 BUS_CHILD_DETACHED(dev->parent, dev);
3120
3121 if (!(dev->flags & DF_FIXEDCLASS))
3122 devclass_delete_device(dev->devclass, dev);
3123
3124 device_verbose(dev);
3125 dev->state = DS_NOTPRESENT;
3126 (void)device_set_driver(dev, NULL);
3127 device_sysctl_fini(dev);
3128
3129 return (0);
3130 }
3131
3132 /**
3133 * @brief Tells a driver to quiesce itself.
3134 *
3135 * This function is a wrapper around the DEVICE_QUIESCE() driver
3136 * method. If the call to DEVICE_QUIESCE() succeeds.
3137 *
3138 * @param dev the device to quiesce
3139 *
3140 * @retval 0 success
3141 * @retval ENXIO no driver was found
3142 * @retval ENOMEM memory allocation failure
3143 * @retval non-zero some other unix error code
3144 */
3145 int
device_quiesce(device_t dev)3146 device_quiesce(device_t dev)
3147 {
3148 PDEBUG(("%s", DEVICENAME(dev)));
3149 if (dev->state == DS_BUSY)
3150 return (EBUSY);
3151 if (dev->state != DS_ATTACHED)
3152 return (0);
3153
3154 return (DEVICE_QUIESCE(dev));
3155 }
3156
3157 /**
3158 * @brief Notify a device of system shutdown
3159 *
3160 * This function calls the DEVICE_SHUTDOWN() driver method if the
3161 * device currently has an attached driver.
3162 *
3163 * @returns the value returned by DEVICE_SHUTDOWN()
3164 */
3165 int
device_shutdown(device_t dev)3166 device_shutdown(device_t dev)
3167 {
3168 if (dev->state < DS_ATTACHED)
3169 return (0);
3170 return (DEVICE_SHUTDOWN(dev));
3171 }
3172
3173 /**
3174 * @brief Set the unit number of a device
3175 *
3176 * This function can be used to override the unit number used for a
3177 * device (e.g. to wire a device to a pre-configured unit number).
3178 */
3179 int
device_set_unit(device_t dev,int unit)3180 device_set_unit(device_t dev, int unit)
3181 {
3182 devclass_t dc;
3183 int err;
3184
3185 if (unit == dev->unit)
3186 return (0);
3187 dc = device_get_devclass(dev);
3188 if (unit < dc->maxunit && dc->devices[unit])
3189 return (EBUSY);
3190 err = devclass_delete_device(dc, dev);
3191 if (err)
3192 return (err);
3193 dev->unit = unit;
3194 err = devclass_add_device(dc, dev);
3195 if (err)
3196 return (err);
3197
3198 bus_data_generation_update();
3199 return (0);
3200 }
3201
3202 /*======================================*/
3203 /*
3204 * Some useful method implementations to make life easier for bus drivers.
3205 */
3206
3207 void
resource_init_map_request_impl(struct resource_map_request * args,size_t sz)3208 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
3209 {
3210 bzero(args, sz);
3211 args->size = sz;
3212 args->memattr = VM_MEMATTR_DEVICE;
3213 }
3214
3215 /**
3216 * @brief Initialise a resource list.
3217 *
3218 * @param rl the resource list to initialise
3219 */
3220 void
resource_list_init(struct resource_list * rl)3221 resource_list_init(struct resource_list *rl)
3222 {
3223 STAILQ_INIT(rl);
3224 }
3225
3226 /**
3227 * @brief Reclaim memory used by a resource list.
3228 *
3229 * This function frees the memory for all resource entries on the list
3230 * (if any).
3231 *
3232 * @param rl the resource list to free
3233 */
3234 void
resource_list_free(struct resource_list * rl)3235 resource_list_free(struct resource_list *rl)
3236 {
3237 struct resource_list_entry *rle;
3238
3239 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3240 if (rle->res)
3241 panic("resource_list_free: resource entry is busy");
3242 STAILQ_REMOVE_HEAD(rl, link);
3243 free(rle, M_BUS);
3244 }
3245 }
3246
3247 /**
3248 * @brief Add a resource entry.
3249 *
3250 * This function adds a resource entry using the given @p type, @p
3251 * start, @p end and @p count values. A rid value is chosen by
3252 * searching sequentially for the first unused rid starting at zero.
3253 *
3254 * @param rl the resource list to edit
3255 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3256 * @param start the start address of the resource
3257 * @param end the end address of the resource
3258 * @param count XXX end-start+1
3259 */
3260 int
resource_list_add_next(struct resource_list * rl,int type,rman_res_t start,rman_res_t end,rman_res_t count)3261 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3262 rman_res_t end, rman_res_t count)
3263 {
3264 int rid;
3265
3266 rid = 0;
3267 while (resource_list_find(rl, type, rid) != NULL)
3268 rid++;
3269 resource_list_add(rl, type, rid, start, end, count);
3270 return (rid);
3271 }
3272
3273 /**
3274 * @brief Add or modify a resource entry.
3275 *
3276 * If an existing entry exists with the same type and rid, it will be
3277 * modified using the given values of @p start, @p end and @p
3278 * count. If no entry exists, a new one will be created using the
3279 * given values. The resource list entry that matches is then returned.
3280 *
3281 * @param rl the resource list to edit
3282 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3283 * @param rid the resource identifier
3284 * @param start the start address of the resource
3285 * @param end the end address of the resource
3286 * @param count XXX end-start+1
3287 */
3288 struct resource_list_entry *
resource_list_add(struct resource_list * rl,int type,int rid,rman_res_t start,rman_res_t end,rman_res_t count)3289 resource_list_add(struct resource_list *rl, int type, int rid,
3290 rman_res_t start, rman_res_t end, rman_res_t count)
3291 {
3292 struct resource_list_entry *rle;
3293
3294 rle = resource_list_find(rl, type, rid);
3295 if (!rle) {
3296 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3297 M_NOWAIT);
3298 if (!rle)
3299 panic("resource_list_add: can't record entry");
3300 STAILQ_INSERT_TAIL(rl, rle, link);
3301 rle->type = type;
3302 rle->rid = rid;
3303 rle->res = NULL;
3304 rle->flags = 0;
3305 }
3306
3307 if (rle->res)
3308 panic("resource_list_add: resource entry is busy");
3309
3310 rle->start = start;
3311 rle->end = end;
3312 rle->count = count;
3313 return (rle);
3314 }
3315
3316 /**
3317 * @brief Determine if a resource entry is busy.
3318 *
3319 * Returns true if a resource entry is busy meaning that it has an
3320 * associated resource that is not an unallocated "reserved" resource.
3321 *
3322 * @param rl the resource list to search
3323 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3324 * @param rid the resource identifier
3325 *
3326 * @returns Non-zero if the entry is busy, zero otherwise.
3327 */
3328 int
resource_list_busy(struct resource_list * rl,int type,int rid)3329 resource_list_busy(struct resource_list *rl, int type, int rid)
3330 {
3331 struct resource_list_entry *rle;
3332
3333 rle = resource_list_find(rl, type, rid);
3334 if (rle == NULL || rle->res == NULL)
3335 return (0);
3336 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3337 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3338 ("reserved resource is active"));
3339 return (0);
3340 }
3341 return (1);
3342 }
3343
3344 /**
3345 * @brief Determine if a resource entry is reserved.
3346 *
3347 * Returns true if a resource entry is reserved meaning that it has an
3348 * associated "reserved" resource. The resource can either be
3349 * allocated or unallocated.
3350 *
3351 * @param rl the resource list to search
3352 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3353 * @param rid the resource identifier
3354 *
3355 * @returns Non-zero if the entry is reserved, zero otherwise.
3356 */
3357 int
resource_list_reserved(struct resource_list * rl,int type,int rid)3358 resource_list_reserved(struct resource_list *rl, int type, int rid)
3359 {
3360 struct resource_list_entry *rle;
3361
3362 rle = resource_list_find(rl, type, rid);
3363 if (rle != NULL && rle->flags & RLE_RESERVED)
3364 return (1);
3365 return (0);
3366 }
3367
3368 /**
3369 * @brief Find a resource entry by type and rid.
3370 *
3371 * @param rl the resource list to search
3372 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3373 * @param rid the resource identifier
3374 *
3375 * @returns the resource entry pointer or NULL if there is no such
3376 * entry.
3377 */
3378 struct resource_list_entry *
resource_list_find(struct resource_list * rl,int type,int rid)3379 resource_list_find(struct resource_list *rl, int type, int rid)
3380 {
3381 struct resource_list_entry *rle;
3382
3383 STAILQ_FOREACH(rle, rl, link) {
3384 if (rle->type == type && rle->rid == rid)
3385 return (rle);
3386 }
3387 return (NULL);
3388 }
3389
3390 /**
3391 * @brief Delete a resource entry.
3392 *
3393 * @param rl the resource list to edit
3394 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3395 * @param rid the resource identifier
3396 */
3397 void
resource_list_delete(struct resource_list * rl,int type,int rid)3398 resource_list_delete(struct resource_list *rl, int type, int rid)
3399 {
3400 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3401
3402 if (rle) {
3403 if (rle->res != NULL)
3404 panic("resource_list_delete: resource has not been released");
3405 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3406 free(rle, M_BUS);
3407 }
3408 }
3409
3410 /**
3411 * @brief Allocate a reserved resource
3412 *
3413 * This can be used by buses to force the allocation of resources
3414 * that are always active in the system even if they are not allocated
3415 * by a driver (e.g. PCI BARs). This function is usually called when
3416 * adding a new child to the bus. The resource is allocated from the
3417 * parent bus when it is reserved. The resource list entry is marked
3418 * with RLE_RESERVED to note that it is a reserved resource.
3419 *
3420 * Subsequent attempts to allocate the resource with
3421 * resource_list_alloc() will succeed the first time and will set
3422 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3423 * resource that has been allocated is released with
3424 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3425 * the actual resource remains allocated. The resource can be released to
3426 * the parent bus by calling resource_list_unreserve().
3427 *
3428 * @param rl the resource list to allocate from
3429 * @param bus the parent device of @p child
3430 * @param child the device for which the resource is being reserved
3431 * @param type the type of resource to allocate
3432 * @param rid a pointer to the resource identifier
3433 * @param start hint at the start of the resource range - pass
3434 * @c 0 for any start address
3435 * @param end hint at the end of the resource range - pass
3436 * @c ~0 for any end address
3437 * @param count hint at the size of range required - pass @c 1
3438 * for any size
3439 * @param flags any extra flags to control the resource
3440 * allocation - see @c RF_XXX flags in
3441 * <sys/rman.h> for details
3442 *
3443 * @returns the resource which was allocated or @c NULL if no
3444 * resource could be allocated
3445 */
3446 struct resource *
resource_list_reserve(struct resource_list * rl,device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)3447 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3448 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3449 {
3450 struct resource_list_entry *rle = NULL;
3451 int passthrough = (device_get_parent(child) != bus);
3452 struct resource *r;
3453
3454 if (passthrough)
3455 panic(
3456 "resource_list_reserve() should only be called for direct children");
3457 if (flags & RF_ACTIVE)
3458 panic(
3459 "resource_list_reserve() should only reserve inactive resources");
3460
3461 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3462 flags);
3463 if (r != NULL) {
3464 rle = resource_list_find(rl, type, *rid);
3465 rle->flags |= RLE_RESERVED;
3466 }
3467 return (r);
3468 }
3469
3470 /**
3471 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3472 *
3473 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3474 * and passing the allocation up to the parent of @p bus. This assumes
3475 * that the first entry of @c device_get_ivars(child) is a struct
3476 * resource_list. This also handles 'passthrough' allocations where a
3477 * child is a remote descendant of bus by passing the allocation up to
3478 * the parent of bus.
3479 *
3480 * Typically, a bus driver would store a list of child resources
3481 * somewhere in the child device's ivars (see device_get_ivars()) and
3482 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3483 * then call resource_list_alloc() to perform the allocation.
3484 *
3485 * @param rl the resource list to allocate from
3486 * @param bus the parent device of @p child
3487 * @param child the device which is requesting an allocation
3488 * @param type the type of resource to allocate
3489 * @param rid a pointer to the resource identifier
3490 * @param start hint at the start of the resource range - pass
3491 * @c 0 for any start address
3492 * @param end hint at the end of the resource range - pass
3493 * @c ~0 for any end address
3494 * @param count hint at the size of range required - pass @c 1
3495 * for any size
3496 * @param flags any extra flags to control the resource
3497 * allocation - see @c RF_XXX flags in
3498 * <sys/rman.h> for details
3499 *
3500 * @returns the resource which was allocated or @c NULL if no
3501 * resource could be allocated
3502 */
3503 struct resource *
resource_list_alloc(struct resource_list * rl,device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)3504 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3505 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3506 {
3507 struct resource_list_entry *rle = NULL;
3508 int passthrough = (device_get_parent(child) != bus);
3509 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3510
3511 if (passthrough) {
3512 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3513 type, rid, start, end, count, flags));
3514 }
3515
3516 rle = resource_list_find(rl, type, *rid);
3517
3518 if (!rle)
3519 return (NULL); /* no resource of that type/rid */
3520
3521 if (rle->res) {
3522 if (rle->flags & RLE_RESERVED) {
3523 if (rle->flags & RLE_ALLOCATED)
3524 return (NULL);
3525 if ((flags & RF_ACTIVE) &&
3526 bus_activate_resource(child, type, *rid,
3527 rle->res) != 0)
3528 return (NULL);
3529 rle->flags |= RLE_ALLOCATED;
3530 return (rle->res);
3531 }
3532 device_printf(bus,
3533 "resource entry %#x type %d for child %s is busy\n", *rid,
3534 type, device_get_nameunit(child));
3535 return (NULL);
3536 }
3537
3538 if (isdefault) {
3539 start = rle->start;
3540 count = ulmax(count, rle->count);
3541 end = ulmax(rle->end, start + count - 1);
3542 }
3543
3544 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3545 type, rid, start, end, count, flags);
3546
3547 /*
3548 * Record the new range.
3549 */
3550 if (rle->res) {
3551 rle->start = rman_get_start(rle->res);
3552 rle->end = rman_get_end(rle->res);
3553 rle->count = count;
3554 }
3555
3556 return (rle->res);
3557 }
3558
3559 /**
3560 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3561 *
3562 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3563 * used with resource_list_alloc().
3564 *
3565 * @param rl the resource list which was allocated from
3566 * @param bus the parent device of @p child
3567 * @param child the device which is requesting a release
3568 * @param type the type of resource to release
3569 * @param rid the resource identifier
3570 * @param res the resource to release
3571 *
3572 * @retval 0 success
3573 * @retval non-zero a standard unix error code indicating what
3574 * error condition prevented the operation
3575 */
3576 int
resource_list_release(struct resource_list * rl,device_t bus,device_t child,int type,int rid,struct resource * res)3577 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3578 int type, int rid, struct resource *res)
3579 {
3580 struct resource_list_entry *rle = NULL;
3581 int passthrough = (device_get_parent(child) != bus);
3582 int error;
3583
3584 if (passthrough) {
3585 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3586 type, rid, res));
3587 }
3588
3589 rle = resource_list_find(rl, type, rid);
3590
3591 if (!rle)
3592 panic("resource_list_release: can't find resource");
3593 if (!rle->res)
3594 panic("resource_list_release: resource entry is not busy");
3595 if (rle->flags & RLE_RESERVED) {
3596 if (rle->flags & RLE_ALLOCATED) {
3597 if (rman_get_flags(res) & RF_ACTIVE) {
3598 error = bus_deactivate_resource(child, type,
3599 rid, res);
3600 if (error)
3601 return (error);
3602 }
3603 rle->flags &= ~RLE_ALLOCATED;
3604 return (0);
3605 }
3606 return (EINVAL);
3607 }
3608
3609 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3610 type, rid, res);
3611 if (error)
3612 return (error);
3613
3614 rle->res = NULL;
3615 return (0);
3616 }
3617
3618 /**
3619 * @brief Release all active resources of a given type
3620 *
3621 * Release all active resources of a specified type. This is intended
3622 * to be used to cleanup resources leaked by a driver after detach or
3623 * a failed attach.
3624 *
3625 * @param rl the resource list which was allocated from
3626 * @param bus the parent device of @p child
3627 * @param child the device whose active resources are being released
3628 * @param type the type of resources to release
3629 *
3630 * @retval 0 success
3631 * @retval EBUSY at least one resource was active
3632 */
3633 int
resource_list_release_active(struct resource_list * rl,device_t bus,device_t child,int type)3634 resource_list_release_active(struct resource_list *rl, device_t bus,
3635 device_t child, int type)
3636 {
3637 struct resource_list_entry *rle;
3638 int error, retval;
3639
3640 retval = 0;
3641 STAILQ_FOREACH(rle, rl, link) {
3642 if (rle->type != type)
3643 continue;
3644 if (rle->res == NULL)
3645 continue;
3646 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3647 RLE_RESERVED)
3648 continue;
3649 retval = EBUSY;
3650 error = resource_list_release(rl, bus, child, type,
3651 rman_get_rid(rle->res), rle->res);
3652 if (error != 0)
3653 device_printf(bus,
3654 "Failed to release active resource: %d\n", error);
3655 }
3656 return (retval);
3657 }
3658
3659 /**
3660 * @brief Fully release a reserved resource
3661 *
3662 * Fully releases a resource reserved via resource_list_reserve().
3663 *
3664 * @param rl the resource list which was allocated from
3665 * @param bus the parent device of @p child
3666 * @param child the device whose reserved resource is being released
3667 * @param type the type of resource to release
3668 * @param rid the resource identifier
3669 * @param res the resource to release
3670 *
3671 * @retval 0 success
3672 * @retval non-zero a standard unix error code indicating what
3673 * error condition prevented the operation
3674 */
3675 int
resource_list_unreserve(struct resource_list * rl,device_t bus,device_t child,int type,int rid)3676 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3677 int type, int rid)
3678 {
3679 struct resource_list_entry *rle = NULL;
3680 int passthrough = (device_get_parent(child) != bus);
3681
3682 if (passthrough)
3683 panic(
3684 "resource_list_unreserve() should only be called for direct children");
3685
3686 rle = resource_list_find(rl, type, rid);
3687
3688 if (!rle)
3689 panic("resource_list_unreserve: can't find resource");
3690 if (!(rle->flags & RLE_RESERVED))
3691 return (EINVAL);
3692 if (rle->flags & RLE_ALLOCATED)
3693 return (EBUSY);
3694 rle->flags &= ~RLE_RESERVED;
3695 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3696 }
3697
3698 /**
3699 * @brief Print a description of resources in a resource list
3700 *
3701 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3702 * The name is printed if at least one resource of the given type is available.
3703 * The format is used to print resource start and end.
3704 *
3705 * @param rl the resource list to print
3706 * @param name the name of @p type, e.g. @c "memory"
3707 * @param type type type of resource entry to print
3708 * @param format printf(9) format string to print resource
3709 * start and end values
3710 *
3711 * @returns the number of characters printed
3712 */
3713 int
resource_list_print_type(struct resource_list * rl,const char * name,int type,const char * format)3714 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3715 const char *format)
3716 {
3717 struct resource_list_entry *rle;
3718 int printed, retval;
3719
3720 printed = 0;
3721 retval = 0;
3722 /* Yes, this is kinda cheating */
3723 STAILQ_FOREACH(rle, rl, link) {
3724 if (rle->type == type) {
3725 if (printed == 0)
3726 retval += printf(" %s ", name);
3727 else
3728 retval += printf(",");
3729 printed++;
3730 retval += printf(format, rle->start);
3731 if (rle->count > 1) {
3732 retval += printf("-");
3733 retval += printf(format, rle->start +
3734 rle->count - 1);
3735 }
3736 }
3737 }
3738 return (retval);
3739 }
3740
3741 /**
3742 * @brief Releases all the resources in a list.
3743 *
3744 * @param rl The resource list to purge.
3745 *
3746 * @returns nothing
3747 */
3748 void
resource_list_purge(struct resource_list * rl)3749 resource_list_purge(struct resource_list *rl)
3750 {
3751 struct resource_list_entry *rle;
3752
3753 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3754 if (rle->res)
3755 bus_release_resource(rman_get_device(rle->res),
3756 rle->type, rle->rid, rle->res);
3757 STAILQ_REMOVE_HEAD(rl, link);
3758 free(rle, M_BUS);
3759 }
3760 }
3761
3762 device_t
bus_generic_add_child(device_t dev,u_int order,const char * name,int unit)3763 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3764 {
3765 return (device_add_child_ordered(dev, order, name, unit));
3766 }
3767
3768 /**
3769 * @brief Helper function for implementing DEVICE_PROBE()
3770 *
3771 * This function can be used to help implement the DEVICE_PROBE() for
3772 * a bus (i.e. a device which has other devices attached to it). It
3773 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3774 * devclass.
3775 */
3776 int
bus_generic_probe(device_t dev)3777 bus_generic_probe(device_t dev)
3778 {
3779 devclass_t dc = dev->devclass;
3780 driverlink_t dl;
3781
3782 TAILQ_FOREACH(dl, &dc->drivers, link) {
3783 /*
3784 * If this driver's pass is too high, then ignore it.
3785 * For most drivers in the default pass, this will
3786 * never be true. For early-pass drivers they will
3787 * only call the identify routines of eligible drivers
3788 * when this routine is called. Drivers for later
3789 * passes should have their identify routines called
3790 * on early-pass buses during BUS_NEW_PASS().
3791 */
3792 if (dl->pass > bus_current_pass)
3793 continue;
3794 DEVICE_IDENTIFY(dl->driver, dev);
3795 }
3796
3797 return (0);
3798 }
3799
3800 /**
3801 * @brief Helper function for implementing DEVICE_ATTACH()
3802 *
3803 * This function can be used to help implement the DEVICE_ATTACH() for
3804 * a bus. It calls device_probe_and_attach() for each of the device's
3805 * children.
3806 */
3807 int
bus_generic_attach(device_t dev)3808 bus_generic_attach(device_t dev)
3809 {
3810 device_t child;
3811
3812 TAILQ_FOREACH(child, &dev->children, link) {
3813 device_probe_and_attach(child);
3814 }
3815
3816 return (0);
3817 }
3818
3819 /**
3820 * @brief Helper function for delaying attaching children
3821 *
3822 * Many buses can't run transactions on the bus which children need to probe and
3823 * attach until after interrupts and/or timers are running. This function
3824 * delays their attach until interrupts and timers are enabled.
3825 */
3826 int
bus_delayed_attach_children(device_t dev)3827 bus_delayed_attach_children(device_t dev)
3828 {
3829 /* Probe and attach the bus children when interrupts are available */
3830 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3831
3832 return (0);
3833 }
3834
3835 /**
3836 * @brief Helper function for implementing DEVICE_DETACH()
3837 *
3838 * This function can be used to help implement the DEVICE_DETACH() for
3839 * a bus. It calls device_detach() for each of the device's
3840 * children.
3841 */
3842 int
bus_generic_detach(device_t dev)3843 bus_generic_detach(device_t dev)
3844 {
3845 device_t child;
3846 int error;
3847
3848 /*
3849 * Detach children in the reverse order.
3850 * See bus_generic_suspend for details.
3851 */
3852 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3853 if ((error = device_detach(child)) != 0)
3854 return (error);
3855 }
3856
3857 return (0);
3858 }
3859
3860 /**
3861 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3862 *
3863 * This function can be used to help implement the DEVICE_SHUTDOWN()
3864 * for a bus. It calls device_shutdown() for each of the device's
3865 * children.
3866 */
3867 int
bus_generic_shutdown(device_t dev)3868 bus_generic_shutdown(device_t dev)
3869 {
3870 device_t child;
3871
3872 /*
3873 * Shut down children in the reverse order.
3874 * See bus_generic_suspend for details.
3875 */
3876 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3877 device_shutdown(child);
3878 }
3879
3880 return (0);
3881 }
3882
3883 /**
3884 * @brief Default function for suspending a child device.
3885 *
3886 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3887 */
3888 int
bus_generic_suspend_child(device_t dev,device_t child)3889 bus_generic_suspend_child(device_t dev, device_t child)
3890 {
3891 int error;
3892
3893 error = DEVICE_SUSPEND(child);
3894
3895 if (error == 0) {
3896 child->flags |= DF_SUSPENDED;
3897 } else {
3898 printf("DEVICE_SUSPEND(%s) failed: %d\n",
3899 device_get_nameunit(child), error);
3900 }
3901
3902 return (error);
3903 }
3904
3905 /**
3906 * @brief Default function for resuming a child device.
3907 *
3908 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3909 */
3910 int
bus_generic_resume_child(device_t dev,device_t child)3911 bus_generic_resume_child(device_t dev, device_t child)
3912 {
3913 DEVICE_RESUME(child);
3914 child->flags &= ~DF_SUSPENDED;
3915
3916 return (0);
3917 }
3918
3919 /**
3920 * @brief Helper function for implementing DEVICE_SUSPEND()
3921 *
3922 * This function can be used to help implement the DEVICE_SUSPEND()
3923 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3924 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3925 * operation is aborted and any devices which were suspended are
3926 * resumed immediately by calling their DEVICE_RESUME() methods.
3927 */
3928 int
bus_generic_suspend(device_t dev)3929 bus_generic_suspend(device_t dev)
3930 {
3931 int error;
3932 device_t child;
3933
3934 /*
3935 * Suspend children in the reverse order.
3936 * For most buses all children are equal, so the order does not matter.
3937 * Other buses, such as acpi, carefully order their child devices to
3938 * express implicit dependencies between them. For such buses it is
3939 * safer to bring down devices in the reverse order.
3940 */
3941 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3942 error = BUS_SUSPEND_CHILD(dev, child);
3943 if (error != 0) {
3944 child = TAILQ_NEXT(child, link);
3945 if (child != NULL) {
3946 TAILQ_FOREACH_FROM(child, &dev->children, link)
3947 BUS_RESUME_CHILD(dev, child);
3948 }
3949 return (error);
3950 }
3951 }
3952 return (0);
3953 }
3954
3955 /**
3956 * @brief Helper function for implementing DEVICE_RESUME()
3957 *
3958 * This function can be used to help implement the DEVICE_RESUME() for
3959 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3960 */
3961 int
bus_generic_resume(device_t dev)3962 bus_generic_resume(device_t dev)
3963 {
3964 device_t child;
3965
3966 TAILQ_FOREACH(child, &dev->children, link) {
3967 BUS_RESUME_CHILD(dev, child);
3968 /* if resume fails, there's nothing we can usefully do... */
3969 }
3970 return (0);
3971 }
3972
3973 /**
3974 * @brief Helper function for implementing BUS_RESET_POST
3975 *
3976 * Bus can use this function to implement common operations of
3977 * re-attaching or resuming the children after the bus itself was
3978 * reset, and after restoring bus-unique state of children.
3979 *
3980 * @param dev The bus
3981 * #param flags DEVF_RESET_*
3982 */
3983 int
bus_helper_reset_post(device_t dev,int flags)3984 bus_helper_reset_post(device_t dev, int flags)
3985 {
3986 device_t child;
3987 int error, error1;
3988
3989 error = 0;
3990 TAILQ_FOREACH(child, &dev->children,link) {
3991 BUS_RESET_POST(dev, child);
3992 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3993 device_probe_and_attach(child) :
3994 BUS_RESUME_CHILD(dev, child);
3995 if (error == 0 && error1 != 0)
3996 error = error1;
3997 }
3998 return (error);
3999 }
4000
4001 static void
bus_helper_reset_prepare_rollback(device_t dev,device_t child,int flags)4002 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
4003 {
4004 child = TAILQ_NEXT(child, link);
4005 if (child == NULL)
4006 return;
4007 TAILQ_FOREACH_FROM(child, &dev->children,link) {
4008 BUS_RESET_POST(dev, child);
4009 if ((flags & DEVF_RESET_DETACH) != 0)
4010 device_probe_and_attach(child);
4011 else
4012 BUS_RESUME_CHILD(dev, child);
4013 }
4014 }
4015
4016 /**
4017 * @brief Helper function for implementing BUS_RESET_PREPARE
4018 *
4019 * Bus can use this function to implement common operations of
4020 * detaching or suspending the children before the bus itself is
4021 * reset, and then save bus-unique state of children that must
4022 * persists around reset.
4023 *
4024 * @param dev The bus
4025 * #param flags DEVF_RESET_*
4026 */
4027 int
bus_helper_reset_prepare(device_t dev,int flags)4028 bus_helper_reset_prepare(device_t dev, int flags)
4029 {
4030 device_t child;
4031 int error;
4032
4033 if (dev->state != DS_ATTACHED)
4034 return (EBUSY);
4035
4036 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
4037 if ((flags & DEVF_RESET_DETACH) != 0) {
4038 error = device_get_state(child) == DS_ATTACHED ?
4039 device_detach(child) : 0;
4040 } else {
4041 error = BUS_SUSPEND_CHILD(dev, child);
4042 }
4043 if (error == 0) {
4044 error = BUS_RESET_PREPARE(dev, child);
4045 if (error != 0) {
4046 if ((flags & DEVF_RESET_DETACH) != 0)
4047 device_probe_and_attach(child);
4048 else
4049 BUS_RESUME_CHILD(dev, child);
4050 }
4051 }
4052 if (error != 0) {
4053 bus_helper_reset_prepare_rollback(dev, child, flags);
4054 return (error);
4055 }
4056 }
4057 return (0);
4058 }
4059
4060 /**
4061 * @brief Helper function for implementing BUS_PRINT_CHILD().
4062 *
4063 * This function prints the first part of the ascii representation of
4064 * @p child, including its name, unit and description (if any - see
4065 * device_set_desc()).
4066 *
4067 * @returns the number of characters printed
4068 */
4069 int
bus_print_child_header(device_t dev,device_t child)4070 bus_print_child_header(device_t dev, device_t child)
4071 {
4072 int retval = 0;
4073
4074 if (device_get_desc(child)) {
4075 retval += device_printf(child, "<%s>", device_get_desc(child));
4076 } else {
4077 retval += printf("%s", device_get_nameunit(child));
4078 }
4079
4080 return (retval);
4081 }
4082
4083 /**
4084 * @brief Helper function for implementing BUS_PRINT_CHILD().
4085 *
4086 * This function prints the last part of the ascii representation of
4087 * @p child, which consists of the string @c " on " followed by the
4088 * name and unit of the @p dev.
4089 *
4090 * @returns the number of characters printed
4091 */
4092 int
bus_print_child_footer(device_t dev,device_t child)4093 bus_print_child_footer(device_t dev, device_t child)
4094 {
4095 return (printf(" on %s\n", device_get_nameunit(dev)));
4096 }
4097
4098 /**
4099 * @brief Helper function for implementing BUS_PRINT_CHILD().
4100 *
4101 * This function prints out the VM domain for the given device.
4102 *
4103 * @returns the number of characters printed
4104 */
4105 int
bus_print_child_domain(device_t dev,device_t child)4106 bus_print_child_domain(device_t dev, device_t child)
4107 {
4108 int domain;
4109
4110 /* No domain? Don't print anything */
4111 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
4112 return (0);
4113
4114 return (printf(" numa-domain %d", domain));
4115 }
4116
4117 /**
4118 * @brief Helper function for implementing BUS_PRINT_CHILD().
4119 *
4120 * This function simply calls bus_print_child_header() followed by
4121 * bus_print_child_footer().
4122 *
4123 * @returns the number of characters printed
4124 */
4125 int
bus_generic_print_child(device_t dev,device_t child)4126 bus_generic_print_child(device_t dev, device_t child)
4127 {
4128 int retval = 0;
4129
4130 retval += bus_print_child_header(dev, child);
4131 retval += bus_print_child_domain(dev, child);
4132 retval += bus_print_child_footer(dev, child);
4133
4134 return (retval);
4135 }
4136
4137 /**
4138 * @brief Stub function for implementing BUS_READ_IVAR().
4139 *
4140 * @returns ENOENT
4141 */
4142 int
bus_generic_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)4143 bus_generic_read_ivar(device_t dev, device_t child, int index,
4144 uintptr_t * result)
4145 {
4146 return (ENOENT);
4147 }
4148
4149 /**
4150 * @brief Stub function for implementing BUS_WRITE_IVAR().
4151 *
4152 * @returns ENOENT
4153 */
4154 int
bus_generic_write_ivar(device_t dev,device_t child,int index,uintptr_t value)4155 bus_generic_write_ivar(device_t dev, device_t child, int index,
4156 uintptr_t value)
4157 {
4158 return (ENOENT);
4159 }
4160
4161 /**
4162 * @brief Helper function for implementing BUS_GET_PROPERTY().
4163 *
4164 * This simply calls the BUS_GET_PROPERTY of the parent of dev,
4165 * until a non-default implementation is found.
4166 */
4167 ssize_t
bus_generic_get_property(device_t dev,device_t child,const char * propname,void * propvalue,size_t size,device_property_type_t type)4168 bus_generic_get_property(device_t dev, device_t child, const char *propname,
4169 void *propvalue, size_t size, device_property_type_t type)
4170 {
4171 if (device_get_parent(dev) != NULL)
4172 return (BUS_GET_PROPERTY(device_get_parent(dev), child,
4173 propname, propvalue, size, type));
4174
4175 return (-1);
4176 }
4177
4178 /**
4179 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
4180 *
4181 * @returns NULL
4182 */
4183 struct resource_list *
bus_generic_get_resource_list(device_t dev,device_t child)4184 bus_generic_get_resource_list(device_t dev, device_t child)
4185 {
4186 return (NULL);
4187 }
4188
4189 /**
4190 * @brief Helper function for implementing BUS_DRIVER_ADDED().
4191 *
4192 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
4193 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
4194 * and then calls device_probe_and_attach() for each unattached child.
4195 */
4196 void
bus_generic_driver_added(device_t dev,driver_t * driver)4197 bus_generic_driver_added(device_t dev, driver_t *driver)
4198 {
4199 device_t child;
4200
4201 DEVICE_IDENTIFY(driver, dev);
4202 TAILQ_FOREACH(child, &dev->children, link) {
4203 if (child->state == DS_NOTPRESENT)
4204 device_probe_and_attach(child);
4205 }
4206 }
4207
4208 /**
4209 * @brief Helper function for implementing BUS_NEW_PASS().
4210 *
4211 * This implementing of BUS_NEW_PASS() first calls the identify
4212 * routines for any drivers that probe at the current pass. Then it
4213 * walks the list of devices for this bus. If a device is already
4214 * attached, then it calls BUS_NEW_PASS() on that device. If the
4215 * device is not already attached, it attempts to attach a driver to
4216 * it.
4217 */
4218 void
bus_generic_new_pass(device_t dev)4219 bus_generic_new_pass(device_t dev)
4220 {
4221 driverlink_t dl;
4222 devclass_t dc;
4223 device_t child;
4224
4225 dc = dev->devclass;
4226 TAILQ_FOREACH(dl, &dc->drivers, link) {
4227 if (dl->pass == bus_current_pass)
4228 DEVICE_IDENTIFY(dl->driver, dev);
4229 }
4230 TAILQ_FOREACH(child, &dev->children, link) {
4231 if (child->state >= DS_ATTACHED)
4232 BUS_NEW_PASS(child);
4233 else if (child->state == DS_NOTPRESENT)
4234 device_probe_and_attach(child);
4235 }
4236 }
4237
4238 /**
4239 * @brief Helper function for implementing BUS_SETUP_INTR().
4240 *
4241 * This simple implementation of BUS_SETUP_INTR() simply calls the
4242 * BUS_SETUP_INTR() method of the parent of @p dev.
4243 */
4244 int
bus_generic_setup_intr(device_t dev,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * intr,void * arg,void ** cookiep)4245 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
4246 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
4247 void **cookiep)
4248 {
4249 /* Propagate up the bus hierarchy until someone handles it. */
4250 if (dev->parent)
4251 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
4252 filter, intr, arg, cookiep));
4253 return (EINVAL);
4254 }
4255
4256 /**
4257 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
4258 *
4259 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
4260 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
4261 */
4262 int
bus_generic_teardown_intr(device_t dev,device_t child,struct resource * irq,void * cookie)4263 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
4264 void *cookie)
4265 {
4266 /* Propagate up the bus hierarchy until someone handles it. */
4267 if (dev->parent)
4268 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
4269 return (EINVAL);
4270 }
4271
4272 /**
4273 * @brief Helper function for implementing BUS_SUSPEND_INTR().
4274 *
4275 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
4276 * BUS_SUSPEND_INTR() method of the parent of @p dev.
4277 */
4278 int
bus_generic_suspend_intr(device_t dev,device_t child,struct resource * irq)4279 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
4280 {
4281 /* Propagate up the bus hierarchy until someone handles it. */
4282 if (dev->parent)
4283 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
4284 return (EINVAL);
4285 }
4286
4287 /**
4288 * @brief Helper function for implementing BUS_RESUME_INTR().
4289 *
4290 * This simple implementation of BUS_RESUME_INTR() simply calls the
4291 * BUS_RESUME_INTR() method of the parent of @p dev.
4292 */
4293 int
bus_generic_resume_intr(device_t dev,device_t child,struct resource * irq)4294 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
4295 {
4296 /* Propagate up the bus hierarchy until someone handles it. */
4297 if (dev->parent)
4298 return (BUS_RESUME_INTR(dev->parent, child, irq));
4299 return (EINVAL);
4300 }
4301
4302 /**
4303 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4304 *
4305 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
4306 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
4307 */
4308 int
bus_generic_adjust_resource(device_t dev,device_t child,int type,struct resource * r,rman_res_t start,rman_res_t end)4309 bus_generic_adjust_resource(device_t dev, device_t child, int type,
4310 struct resource *r, rman_res_t start, rman_res_t end)
4311 {
4312 /* Propagate up the bus hierarchy until someone handles it. */
4313 if (dev->parent)
4314 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
4315 end));
4316 return (EINVAL);
4317 }
4318
4319 /**
4320 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4321 *
4322 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
4323 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4324 */
4325 struct resource *
bus_generic_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)4326 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4327 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4328 {
4329 /* Propagate up the bus hierarchy until someone handles it. */
4330 if (dev->parent)
4331 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4332 start, end, count, flags));
4333 return (NULL);
4334 }
4335
4336 /**
4337 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4338 *
4339 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4340 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4341 */
4342 int
bus_generic_release_resource(device_t dev,device_t child,int type,int rid,struct resource * r)4343 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4344 struct resource *r)
4345 {
4346 /* Propagate up the bus hierarchy until someone handles it. */
4347 if (dev->parent)
4348 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4349 r));
4350 return (EINVAL);
4351 }
4352
4353 /**
4354 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4355 *
4356 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4357 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4358 */
4359 int
bus_generic_activate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)4360 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4361 struct resource *r)
4362 {
4363 /* Propagate up the bus hierarchy until someone handles it. */
4364 if (dev->parent)
4365 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4366 r));
4367 return (EINVAL);
4368 }
4369
4370 /**
4371 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4372 *
4373 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4374 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4375 */
4376 int
bus_generic_deactivate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)4377 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4378 int rid, struct resource *r)
4379 {
4380 /* Propagate up the bus hierarchy until someone handles it. */
4381 if (dev->parent)
4382 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4383 r));
4384 return (EINVAL);
4385 }
4386
4387 /**
4388 * @brief Helper function for implementing BUS_MAP_RESOURCE().
4389 *
4390 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
4391 * BUS_MAP_RESOURCE() method of the parent of @p dev.
4392 */
4393 int
bus_generic_map_resource(device_t dev,device_t child,int type,struct resource * r,struct resource_map_request * args,struct resource_map * map)4394 bus_generic_map_resource(device_t dev, device_t child, int type,
4395 struct resource *r, struct resource_map_request *args,
4396 struct resource_map *map)
4397 {
4398 /* Propagate up the bus hierarchy until someone handles it. */
4399 if (dev->parent)
4400 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
4401 map));
4402 return (EINVAL);
4403 }
4404
4405 /**
4406 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
4407 *
4408 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
4409 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
4410 */
4411 int
bus_generic_unmap_resource(device_t dev,device_t child,int type,struct resource * r,struct resource_map * map)4412 bus_generic_unmap_resource(device_t dev, device_t child, int type,
4413 struct resource *r, struct resource_map *map)
4414 {
4415 /* Propagate up the bus hierarchy until someone handles it. */
4416 if (dev->parent)
4417 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
4418 return (EINVAL);
4419 }
4420
4421 /**
4422 * @brief Helper function for implementing BUS_BIND_INTR().
4423 *
4424 * This simple implementation of BUS_BIND_INTR() simply calls the
4425 * BUS_BIND_INTR() method of the parent of @p dev.
4426 */
4427 int
bus_generic_bind_intr(device_t dev,device_t child,struct resource * irq,int cpu)4428 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4429 int cpu)
4430 {
4431 /* Propagate up the bus hierarchy until someone handles it. */
4432 if (dev->parent)
4433 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4434 return (EINVAL);
4435 }
4436
4437 /**
4438 * @brief Helper function for implementing BUS_CONFIG_INTR().
4439 *
4440 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4441 * BUS_CONFIG_INTR() method of the parent of @p dev.
4442 */
4443 int
bus_generic_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)4444 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4445 enum intr_polarity pol)
4446 {
4447 /* Propagate up the bus hierarchy until someone handles it. */
4448 if (dev->parent)
4449 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4450 return (EINVAL);
4451 }
4452
4453 /**
4454 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4455 *
4456 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4457 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4458 */
4459 int
bus_generic_describe_intr(device_t dev,device_t child,struct resource * irq,void * cookie,const char * descr)4460 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4461 void *cookie, const char *descr)
4462 {
4463 /* Propagate up the bus hierarchy until someone handles it. */
4464 if (dev->parent)
4465 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4466 descr));
4467 return (EINVAL);
4468 }
4469
4470 /**
4471 * @brief Helper function for implementing BUS_GET_CPUS().
4472 *
4473 * This simple implementation of BUS_GET_CPUS() simply calls the
4474 * BUS_GET_CPUS() method of the parent of @p dev.
4475 */
4476 int
bus_generic_get_cpus(device_t dev,device_t child,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)4477 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4478 size_t setsize, cpuset_t *cpuset)
4479 {
4480 /* Propagate up the bus hierarchy until someone handles it. */
4481 if (dev->parent != NULL)
4482 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4483 return (EINVAL);
4484 }
4485
4486 /**
4487 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4488 *
4489 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4490 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4491 */
4492 bus_dma_tag_t
bus_generic_get_dma_tag(device_t dev,device_t child)4493 bus_generic_get_dma_tag(device_t dev, device_t child)
4494 {
4495 /* Propagate up the bus hierarchy until someone handles it. */
4496 if (dev->parent != NULL)
4497 return (BUS_GET_DMA_TAG(dev->parent, child));
4498 return (NULL);
4499 }
4500
4501 /**
4502 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4503 *
4504 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4505 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4506 */
4507 bus_space_tag_t
bus_generic_get_bus_tag(device_t dev,device_t child)4508 bus_generic_get_bus_tag(device_t dev, device_t child)
4509 {
4510 /* Propagate up the bus hierarchy until someone handles it. */
4511 if (dev->parent != NULL)
4512 return (BUS_GET_BUS_TAG(dev->parent, child));
4513 return ((bus_space_tag_t)0);
4514 }
4515
4516 /**
4517 * @brief Helper function for implementing BUS_GET_RESOURCE().
4518 *
4519 * This implementation of BUS_GET_RESOURCE() uses the
4520 * resource_list_find() function to do most of the work. It calls
4521 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4522 * search.
4523 */
4524 int
bus_generic_rl_get_resource(device_t dev,device_t child,int type,int rid,rman_res_t * startp,rman_res_t * countp)4525 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4526 rman_res_t *startp, rman_res_t *countp)
4527 {
4528 struct resource_list * rl = NULL;
4529 struct resource_list_entry * rle = NULL;
4530
4531 rl = BUS_GET_RESOURCE_LIST(dev, child);
4532 if (!rl)
4533 return (EINVAL);
4534
4535 rle = resource_list_find(rl, type, rid);
4536 if (!rle)
4537 return (ENOENT);
4538
4539 if (startp)
4540 *startp = rle->start;
4541 if (countp)
4542 *countp = rle->count;
4543
4544 return (0);
4545 }
4546
4547 /**
4548 * @brief Helper function for implementing BUS_SET_RESOURCE().
4549 *
4550 * This implementation of BUS_SET_RESOURCE() uses the
4551 * resource_list_add() function to do most of the work. It calls
4552 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4553 * edit.
4554 */
4555 int
bus_generic_rl_set_resource(device_t dev,device_t child,int type,int rid,rman_res_t start,rman_res_t count)4556 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4557 rman_res_t start, rman_res_t count)
4558 {
4559 struct resource_list * rl = NULL;
4560
4561 rl = BUS_GET_RESOURCE_LIST(dev, child);
4562 if (!rl)
4563 return (EINVAL);
4564
4565 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4566
4567 return (0);
4568 }
4569
4570 /**
4571 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4572 *
4573 * This implementation of BUS_DELETE_RESOURCE() uses the
4574 * resource_list_delete() function to do most of the work. It calls
4575 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4576 * edit.
4577 */
4578 void
bus_generic_rl_delete_resource(device_t dev,device_t child,int type,int rid)4579 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4580 {
4581 struct resource_list * rl = NULL;
4582
4583 rl = BUS_GET_RESOURCE_LIST(dev, child);
4584 if (!rl)
4585 return;
4586
4587 resource_list_delete(rl, type, rid);
4588
4589 return;
4590 }
4591
4592 /**
4593 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4594 *
4595 * This implementation of BUS_RELEASE_RESOURCE() uses the
4596 * resource_list_release() function to do most of the work. It calls
4597 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4598 */
4599 int
bus_generic_rl_release_resource(device_t dev,device_t child,int type,int rid,struct resource * r)4600 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4601 int rid, struct resource *r)
4602 {
4603 struct resource_list * rl = NULL;
4604
4605 if (device_get_parent(child) != dev)
4606 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4607 type, rid, r));
4608
4609 rl = BUS_GET_RESOURCE_LIST(dev, child);
4610 if (!rl)
4611 return (EINVAL);
4612
4613 return (resource_list_release(rl, dev, child, type, rid, r));
4614 }
4615
4616 /**
4617 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4618 *
4619 * This implementation of BUS_ALLOC_RESOURCE() uses the
4620 * resource_list_alloc() function to do most of the work. It calls
4621 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4622 */
4623 struct resource *
bus_generic_rl_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)4624 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4625 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4626 {
4627 struct resource_list * rl = NULL;
4628
4629 if (device_get_parent(child) != dev)
4630 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4631 type, rid, start, end, count, flags));
4632
4633 rl = BUS_GET_RESOURCE_LIST(dev, child);
4634 if (!rl)
4635 return (NULL);
4636
4637 return (resource_list_alloc(rl, dev, child, type, rid,
4638 start, end, count, flags));
4639 }
4640
4641 /**
4642 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4643 *
4644 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4645 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4646 */
4647 int
bus_generic_child_present(device_t dev,device_t child)4648 bus_generic_child_present(device_t dev, device_t child)
4649 {
4650 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4651 }
4652
4653 int
bus_generic_get_domain(device_t dev,device_t child,int * domain)4654 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4655 {
4656 if (dev->parent)
4657 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4658
4659 return (ENOENT);
4660 }
4661
4662 /**
4663 * @brief Helper function for implementing BUS_RESCAN().
4664 *
4665 * This null implementation of BUS_RESCAN() always fails to indicate
4666 * the bus does not support rescanning.
4667 */
4668 int
bus_null_rescan(device_t dev)4669 bus_null_rescan(device_t dev)
4670 {
4671 return (ENODEV);
4672 }
4673
4674 /*
4675 * Some convenience functions to make it easier for drivers to use the
4676 * resource-management functions. All these really do is hide the
4677 * indirection through the parent's method table, making for slightly
4678 * less-wordy code. In the future, it might make sense for this code
4679 * to maintain some sort of a list of resources allocated by each device.
4680 */
4681
4682 int
bus_alloc_resources(device_t dev,struct resource_spec * rs,struct resource ** res)4683 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4684 struct resource **res)
4685 {
4686 int i;
4687
4688 for (i = 0; rs[i].type != -1; i++)
4689 res[i] = NULL;
4690 for (i = 0; rs[i].type != -1; i++) {
4691 res[i] = bus_alloc_resource_any(dev,
4692 rs[i].type, &rs[i].rid, rs[i].flags);
4693 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4694 bus_release_resources(dev, rs, res);
4695 return (ENXIO);
4696 }
4697 }
4698 return (0);
4699 }
4700
4701 void
bus_release_resources(device_t dev,const struct resource_spec * rs,struct resource ** res)4702 bus_release_resources(device_t dev, const struct resource_spec *rs,
4703 struct resource **res)
4704 {
4705 int i;
4706
4707 for (i = 0; rs[i].type != -1; i++)
4708 if (res[i] != NULL) {
4709 bus_release_resource(
4710 dev, rs[i].type, rs[i].rid, res[i]);
4711 res[i] = NULL;
4712 }
4713 }
4714
4715 /**
4716 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4717 *
4718 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4719 * parent of @p dev.
4720 */
4721 struct resource *
bus_alloc_resource(device_t dev,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)4722 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4723 rman_res_t end, rman_res_t count, u_int flags)
4724 {
4725 struct resource *res;
4726
4727 if (dev->parent == NULL)
4728 return (NULL);
4729 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4730 count, flags);
4731 return (res);
4732 }
4733
4734 /**
4735 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4736 *
4737 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4738 * parent of @p dev.
4739 */
4740 int
bus_adjust_resource(device_t dev,int type,struct resource * r,rman_res_t start,rman_res_t end)4741 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4742 rman_res_t end)
4743 {
4744 if (dev->parent == NULL)
4745 return (EINVAL);
4746 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4747 }
4748
4749 /**
4750 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4751 *
4752 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4753 * parent of @p dev.
4754 */
4755 int
bus_activate_resource(device_t dev,int type,int rid,struct resource * r)4756 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4757 {
4758 if (dev->parent == NULL)
4759 return (EINVAL);
4760 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4761 }
4762
4763 /**
4764 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4765 *
4766 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4767 * parent of @p dev.
4768 */
4769 int
bus_deactivate_resource(device_t dev,int type,int rid,struct resource * r)4770 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4771 {
4772 if (dev->parent == NULL)
4773 return (EINVAL);
4774 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4775 }
4776
4777 /**
4778 * @brief Wrapper function for BUS_MAP_RESOURCE().
4779 *
4780 * This function simply calls the BUS_MAP_RESOURCE() method of the
4781 * parent of @p dev.
4782 */
4783 int
bus_map_resource(device_t dev,int type,struct resource * r,struct resource_map_request * args,struct resource_map * map)4784 bus_map_resource(device_t dev, int type, struct resource *r,
4785 struct resource_map_request *args, struct resource_map *map)
4786 {
4787 if (dev->parent == NULL)
4788 return (EINVAL);
4789 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4790 }
4791
4792 /**
4793 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4794 *
4795 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4796 * parent of @p dev.
4797 */
4798 int
bus_unmap_resource(device_t dev,int type,struct resource * r,struct resource_map * map)4799 bus_unmap_resource(device_t dev, int type, struct resource *r,
4800 struct resource_map *map)
4801 {
4802 if (dev->parent == NULL)
4803 return (EINVAL);
4804 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4805 }
4806
4807 /**
4808 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4809 *
4810 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4811 * parent of @p dev.
4812 */
4813 int
bus_release_resource(device_t dev,int type,int rid,struct resource * r)4814 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4815 {
4816 int rv;
4817
4818 if (dev->parent == NULL)
4819 return (EINVAL);
4820 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4821 return (rv);
4822 }
4823
4824 /**
4825 * @brief Wrapper function for BUS_SETUP_INTR().
4826 *
4827 * This function simply calls the BUS_SETUP_INTR() method of the
4828 * parent of @p dev.
4829 */
4830 int
bus_setup_intr(device_t dev,struct resource * r,int flags,driver_filter_t filter,driver_intr_t handler,void * arg,void ** cookiep)4831 bus_setup_intr(device_t dev, struct resource *r, int flags,
4832 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4833 {
4834 int error;
4835
4836 if (dev->parent == NULL)
4837 return (EINVAL);
4838 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4839 arg, cookiep);
4840 if (error != 0)
4841 return (error);
4842 if (handler != NULL && !(flags & INTR_MPSAFE))
4843 device_printf(dev, "[GIANT-LOCKED]\n");
4844 return (0);
4845 }
4846
4847 /**
4848 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4849 *
4850 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4851 * parent of @p dev.
4852 */
4853 int
bus_teardown_intr(device_t dev,struct resource * r,void * cookie)4854 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4855 {
4856 if (dev->parent == NULL)
4857 return (EINVAL);
4858 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4859 }
4860
4861 /**
4862 * @brief Wrapper function for BUS_SUSPEND_INTR().
4863 *
4864 * This function simply calls the BUS_SUSPEND_INTR() method of the
4865 * parent of @p dev.
4866 */
4867 int
bus_suspend_intr(device_t dev,struct resource * r)4868 bus_suspend_intr(device_t dev, struct resource *r)
4869 {
4870 if (dev->parent == NULL)
4871 return (EINVAL);
4872 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4873 }
4874
4875 /**
4876 * @brief Wrapper function for BUS_RESUME_INTR().
4877 *
4878 * This function simply calls the BUS_RESUME_INTR() method of the
4879 * parent of @p dev.
4880 */
4881 int
bus_resume_intr(device_t dev,struct resource * r)4882 bus_resume_intr(device_t dev, struct resource *r)
4883 {
4884 if (dev->parent == NULL)
4885 return (EINVAL);
4886 return (BUS_RESUME_INTR(dev->parent, dev, r));
4887 }
4888
4889 /**
4890 * @brief Wrapper function for BUS_BIND_INTR().
4891 *
4892 * This function simply calls the BUS_BIND_INTR() method of the
4893 * parent of @p dev.
4894 */
4895 int
bus_bind_intr(device_t dev,struct resource * r,int cpu)4896 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4897 {
4898 if (dev->parent == NULL)
4899 return (EINVAL);
4900 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4901 }
4902
4903 /**
4904 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4905 *
4906 * This function first formats the requested description into a
4907 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4908 * the parent of @p dev.
4909 */
4910 int
bus_describe_intr(device_t dev,struct resource * irq,void * cookie,const char * fmt,...)4911 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4912 const char *fmt, ...)
4913 {
4914 va_list ap;
4915 char descr[MAXCOMLEN + 1];
4916
4917 if (dev->parent == NULL)
4918 return (EINVAL);
4919 va_start(ap, fmt);
4920 vsnprintf(descr, sizeof(descr), fmt, ap);
4921 va_end(ap);
4922 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4923 }
4924
4925 /**
4926 * @brief Wrapper function for BUS_SET_RESOURCE().
4927 *
4928 * This function simply calls the BUS_SET_RESOURCE() method of the
4929 * parent of @p dev.
4930 */
4931 int
bus_set_resource(device_t dev,int type,int rid,rman_res_t start,rman_res_t count)4932 bus_set_resource(device_t dev, int type, int rid,
4933 rman_res_t start, rman_res_t count)
4934 {
4935 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4936 start, count));
4937 }
4938
4939 /**
4940 * @brief Wrapper function for BUS_GET_RESOURCE().
4941 *
4942 * This function simply calls the BUS_GET_RESOURCE() method of the
4943 * parent of @p dev.
4944 */
4945 int
bus_get_resource(device_t dev,int type,int rid,rman_res_t * startp,rman_res_t * countp)4946 bus_get_resource(device_t dev, int type, int rid,
4947 rman_res_t *startp, rman_res_t *countp)
4948 {
4949 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4950 startp, countp));
4951 }
4952
4953 /**
4954 * @brief Wrapper function for BUS_GET_RESOURCE().
4955 *
4956 * This function simply calls the BUS_GET_RESOURCE() method of the
4957 * parent of @p dev and returns the start value.
4958 */
4959 rman_res_t
bus_get_resource_start(device_t dev,int type,int rid)4960 bus_get_resource_start(device_t dev, int type, int rid)
4961 {
4962 rman_res_t start;
4963 rman_res_t count;
4964 int error;
4965
4966 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4967 &start, &count);
4968 if (error)
4969 return (0);
4970 return (start);
4971 }
4972
4973 /**
4974 * @brief Wrapper function for BUS_GET_RESOURCE().
4975 *
4976 * This function simply calls the BUS_GET_RESOURCE() method of the
4977 * parent of @p dev and returns the count value.
4978 */
4979 rman_res_t
bus_get_resource_count(device_t dev,int type,int rid)4980 bus_get_resource_count(device_t dev, int type, int rid)
4981 {
4982 rman_res_t start;
4983 rman_res_t count;
4984 int error;
4985
4986 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4987 &start, &count);
4988 if (error)
4989 return (0);
4990 return (count);
4991 }
4992
4993 /**
4994 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4995 *
4996 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4997 * parent of @p dev.
4998 */
4999 void
bus_delete_resource(device_t dev,int type,int rid)5000 bus_delete_resource(device_t dev, int type, int rid)
5001 {
5002 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
5003 }
5004
5005 /**
5006 * @brief Wrapper function for BUS_CHILD_PRESENT().
5007 *
5008 * This function simply calls the BUS_CHILD_PRESENT() method of the
5009 * parent of @p dev.
5010 */
5011 int
bus_child_present(device_t child)5012 bus_child_present(device_t child)
5013 {
5014 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
5015 }
5016
5017 /**
5018 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
5019 *
5020 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
5021 * parent of @p dev.
5022 */
5023 int
bus_child_pnpinfo_str(device_t child,char * buf,size_t buflen)5024 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
5025 {
5026 device_t parent;
5027
5028 parent = device_get_parent(child);
5029 if (parent == NULL) {
5030 *buf = '\0';
5031 return (0);
5032 }
5033 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
5034 }
5035
5036 /**
5037 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
5038 *
5039 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
5040 * parent of @p dev.
5041 */
5042 int
bus_child_location_str(device_t child,char * buf,size_t buflen)5043 bus_child_location_str(device_t child, char *buf, size_t buflen)
5044 {
5045 device_t parent;
5046
5047 parent = device_get_parent(child);
5048 if (parent == NULL) {
5049 *buf = '\0';
5050 return (0);
5051 }
5052 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
5053 }
5054
5055 /**
5056 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
5057 *
5058 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
5059 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
5060 * accomplish this, however. It is an interim function until we can convert
5061 * this interface more fully.
5062 */
5063 /* Note: we reach inside of sbuf because it's API isn't rich enough to do this */
5064 #define SPACE(s) ((s)->s_size - (s)->s_len)
5065 #define EOB(s) ((s)->s_buf + (s)->s_len)
5066
5067 static int
bus_child_pnpinfo_sb(device_t dev,struct sbuf * sb)5068 bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb)
5069 {
5070 char *p;
5071 ssize_t space;
5072
5073 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
5074 MPASS(sb->s_size >= sb->s_len);
5075 if (sb->s_error != 0)
5076 return (-1);
5077 space = SPACE(sb);
5078 if (space <= 1) {
5079 sb->s_error = ENOMEM;
5080 return (-1);
5081 }
5082 p = EOB(sb);
5083 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
5084 bus_child_pnpinfo_str(dev, p, space);
5085 sb->s_len += strlen(p);
5086 return (0);
5087 }
5088
5089 /**
5090 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
5091 *
5092 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
5093 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
5094 * accomplish this, however. It is an interim function until we can convert
5095 * this interface more fully.
5096 */
5097 static int
bus_child_location_sb(device_t dev,struct sbuf * sb)5098 bus_child_location_sb(device_t dev, struct sbuf *sb)
5099 {
5100 char *p;
5101 ssize_t space;
5102
5103 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
5104 MPASS(sb->s_size >= sb->s_len);
5105 if (sb->s_error != 0)
5106 return (-1);
5107 space = SPACE(sb);
5108 if (space <= 1) {
5109 sb->s_error = ENOMEM;
5110 return (-1);
5111 }
5112 p = EOB(sb);
5113 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
5114 bus_child_location_str(dev, p, space);
5115 sb->s_len += strlen(p);
5116 return (0);
5117 }
5118 #undef SPACE
5119 #undef EOB
5120
5121 /**
5122 * @brief Wrapper function for BUS_GET_CPUS().
5123 *
5124 * This function simply calls the BUS_GET_CPUS() method of the
5125 * parent of @p dev.
5126 */
5127 int
bus_get_cpus(device_t dev,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)5128 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
5129 {
5130 device_t parent;
5131
5132 parent = device_get_parent(dev);
5133 if (parent == NULL)
5134 return (EINVAL);
5135 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
5136 }
5137
5138 /**
5139 * @brief Wrapper function for BUS_GET_DMA_TAG().
5140 *
5141 * This function simply calls the BUS_GET_DMA_TAG() method of the
5142 * parent of @p dev.
5143 */
5144 bus_dma_tag_t
bus_get_dma_tag(device_t dev)5145 bus_get_dma_tag(device_t dev)
5146 {
5147 device_t parent;
5148
5149 parent = device_get_parent(dev);
5150 if (parent == NULL)
5151 return (NULL);
5152 return (BUS_GET_DMA_TAG(parent, dev));
5153 }
5154
5155 /**
5156 * @brief Wrapper function for BUS_GET_BUS_TAG().
5157 *
5158 * This function simply calls the BUS_GET_BUS_TAG() method of the
5159 * parent of @p dev.
5160 */
5161 bus_space_tag_t
bus_get_bus_tag(device_t dev)5162 bus_get_bus_tag(device_t dev)
5163 {
5164 device_t parent;
5165
5166 parent = device_get_parent(dev);
5167 if (parent == NULL)
5168 return ((bus_space_tag_t)0);
5169 return (BUS_GET_BUS_TAG(parent, dev));
5170 }
5171
5172 /**
5173 * @brief Wrapper function for BUS_GET_DOMAIN().
5174 *
5175 * This function simply calls the BUS_GET_DOMAIN() method of the
5176 * parent of @p dev.
5177 */
5178 int
bus_get_domain(device_t dev,int * domain)5179 bus_get_domain(device_t dev, int *domain)
5180 {
5181 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
5182 }
5183
5184 /* Resume all devices and then notify userland that we're up again. */
5185 static int
root_resume(device_t dev)5186 root_resume(device_t dev)
5187 {
5188 int error;
5189
5190 error = bus_generic_resume(dev);
5191 if (error == 0) {
5192 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
5193 devctl_notify("kernel", "power", "resume", NULL);
5194 }
5195 return (error);
5196 }
5197
5198 static int
root_print_child(device_t dev,device_t child)5199 root_print_child(device_t dev, device_t child)
5200 {
5201 int retval = 0;
5202
5203 retval += bus_print_child_header(dev, child);
5204 retval += printf("\n");
5205
5206 return (retval);
5207 }
5208
5209 static int
root_setup_intr(device_t dev,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * intr,void * arg,void ** cookiep)5210 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
5211 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
5212 {
5213 /*
5214 * If an interrupt mapping gets to here something bad has happened.
5215 */
5216 panic("root_setup_intr");
5217 }
5218
5219 /*
5220 * If we get here, assume that the device is permanent and really is
5221 * present in the system. Removable bus drivers are expected to intercept
5222 * this call long before it gets here. We return -1 so that drivers that
5223 * really care can check vs -1 or some ERRNO returned higher in the food
5224 * chain.
5225 */
5226 static int
root_child_present(device_t dev,device_t child)5227 root_child_present(device_t dev, device_t child)
5228 {
5229 return (-1);
5230 }
5231
5232 static int
root_get_cpus(device_t dev,device_t child,enum cpu_sets op,size_t setsize,cpuset_t * cpuset)5233 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
5234 cpuset_t *cpuset)
5235 {
5236 switch (op) {
5237 case INTR_CPUS:
5238 /* Default to returning the set of all CPUs. */
5239 if (setsize != sizeof(cpuset_t))
5240 return (EINVAL);
5241 *cpuset = all_cpus;
5242 return (0);
5243 default:
5244 return (EINVAL);
5245 }
5246 }
5247
5248 static kobj_method_t root_methods[] = {
5249 /* Device interface */
5250 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
5251 KOBJMETHOD(device_suspend, bus_generic_suspend),
5252 KOBJMETHOD(device_resume, root_resume),
5253
5254 /* Bus interface */
5255 KOBJMETHOD(bus_print_child, root_print_child),
5256 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
5257 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
5258 KOBJMETHOD(bus_setup_intr, root_setup_intr),
5259 KOBJMETHOD(bus_child_present, root_child_present),
5260 KOBJMETHOD(bus_get_cpus, root_get_cpus),
5261
5262 KOBJMETHOD_END
5263 };
5264
5265 static driver_t root_driver = {
5266 "root",
5267 root_methods,
5268 1, /* no softc */
5269 };
5270
5271 device_t root_bus;
5272 devclass_t root_devclass;
5273
5274 static int
root_bus_module_handler(module_t mod,int what,void * arg)5275 root_bus_module_handler(module_t mod, int what, void* arg)
5276 {
5277 switch (what) {
5278 case MOD_LOAD:
5279 TAILQ_INIT(&bus_data_devices);
5280 kobj_class_compile((kobj_class_t) &root_driver);
5281 root_bus = make_device(NULL, "root", 0);
5282 root_bus->desc = "System root bus";
5283 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5284 root_bus->driver = &root_driver;
5285 root_bus->state = DS_ATTACHED;
5286 root_devclass = devclass_find_internal("root", NULL, FALSE);
5287 devinit();
5288 return (0);
5289
5290 case MOD_SHUTDOWN:
5291 device_shutdown(root_bus);
5292 return (0);
5293 default:
5294 return (EOPNOTSUPP);
5295 }
5296
5297 return (0);
5298 }
5299
5300 static moduledata_t root_bus_mod = {
5301 "rootbus",
5302 root_bus_module_handler,
5303 NULL
5304 };
5305 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5306
5307 /**
5308 * @brief Automatically configure devices
5309 *
5310 * This function begins the autoconfiguration process by calling
5311 * device_probe_and_attach() for each child of the @c root0 device.
5312 */
5313 void
root_bus_configure(void)5314 root_bus_configure(void)
5315 {
5316 PDEBUG(("."));
5317
5318 /* Eventually this will be split up, but this is sufficient for now. */
5319 bus_set_pass(BUS_PASS_DEFAULT);
5320 }
5321
5322 /**
5323 * @brief Module handler for registering device drivers
5324 *
5325 * This module handler is used to automatically register device
5326 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5327 * devclass_add_driver() for the driver described by the
5328 * driver_module_data structure pointed to by @p arg
5329 */
5330 int
driver_module_handler(module_t mod,int what,void * arg)5331 driver_module_handler(module_t mod, int what, void *arg)
5332 {
5333 struct driver_module_data *dmd;
5334 devclass_t bus_devclass;
5335 kobj_class_t driver;
5336 int error, pass;
5337
5338 dmd = (struct driver_module_data *)arg;
5339 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5340 error = 0;
5341
5342 switch (what) {
5343 case MOD_LOAD:
5344 if (dmd->dmd_chainevh)
5345 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5346
5347 pass = dmd->dmd_pass;
5348 driver = dmd->dmd_driver;
5349 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5350 DRIVERNAME(driver), dmd->dmd_busname, pass));
5351 error = devclass_add_driver(bus_devclass, driver, pass,
5352 dmd->dmd_devclass);
5353 break;
5354
5355 case MOD_UNLOAD:
5356 PDEBUG(("Unloading module: driver %s from bus %s",
5357 DRIVERNAME(dmd->dmd_driver),
5358 dmd->dmd_busname));
5359 error = devclass_delete_driver(bus_devclass,
5360 dmd->dmd_driver);
5361
5362 if (!error && dmd->dmd_chainevh)
5363 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5364 break;
5365 case MOD_QUIESCE:
5366 PDEBUG(("Quiesce module: driver %s from bus %s",
5367 DRIVERNAME(dmd->dmd_driver),
5368 dmd->dmd_busname));
5369 error = devclass_quiesce_driver(bus_devclass,
5370 dmd->dmd_driver);
5371
5372 if (!error && dmd->dmd_chainevh)
5373 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5374 break;
5375 default:
5376 error = EOPNOTSUPP;
5377 break;
5378 }
5379
5380 return (error);
5381 }
5382
5383 /**
5384 * @brief Enumerate all hinted devices for this bus.
5385 *
5386 * Walks through the hints for this bus and calls the bus_hinted_child
5387 * routine for each one it fines. It searches first for the specific
5388 * bus that's being probed for hinted children (eg isa0), and then for
5389 * generic children (eg isa).
5390 *
5391 * @param dev bus device to enumerate
5392 */
5393 void
bus_enumerate_hinted_children(device_t bus)5394 bus_enumerate_hinted_children(device_t bus)
5395 {
5396 int i;
5397 const char *dname, *busname;
5398 int dunit;
5399
5400 /*
5401 * enumerate all devices on the specific bus
5402 */
5403 busname = device_get_nameunit(bus);
5404 i = 0;
5405 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5406 BUS_HINTED_CHILD(bus, dname, dunit);
5407
5408 /*
5409 * and all the generic ones.
5410 */
5411 busname = device_get_name(bus);
5412 i = 0;
5413 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5414 BUS_HINTED_CHILD(bus, dname, dunit);
5415 }
5416
5417 #ifdef BUS_DEBUG
5418
5419 /* the _short versions avoid iteration by not calling anything that prints
5420 * more than oneliners. I love oneliners.
5421 */
5422
5423 static void
print_device_short(device_t dev,int indent)5424 print_device_short(device_t dev, int indent)
5425 {
5426 if (!dev)
5427 return;
5428
5429 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5430 dev->unit, dev->desc,
5431 (dev->parent? "":"no "),
5432 (TAILQ_EMPTY(&dev->children)? "no ":""),
5433 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5434 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5435 (dev->flags&DF_WILDCARD? "wildcard,":""),
5436 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5437 (dev->flags&DF_SUSPENDED? "suspended,":""),
5438 (dev->ivars? "":"no "),
5439 (dev->softc? "":"no "),
5440 dev->busy));
5441 }
5442
5443 static void
print_device(device_t dev,int indent)5444 print_device(device_t dev, int indent)
5445 {
5446 if (!dev)
5447 return;
5448
5449 print_device_short(dev, indent);
5450
5451 indentprintf(("Parent:\n"));
5452 print_device_short(dev->parent, indent+1);
5453 indentprintf(("Driver:\n"));
5454 print_driver_short(dev->driver, indent+1);
5455 indentprintf(("Devclass:\n"));
5456 print_devclass_short(dev->devclass, indent+1);
5457 }
5458
5459 void
print_device_tree_short(device_t dev,int indent)5460 print_device_tree_short(device_t dev, int indent)
5461 /* print the device and all its children (indented) */
5462 {
5463 device_t child;
5464
5465 if (!dev)
5466 return;
5467
5468 print_device_short(dev, indent);
5469
5470 TAILQ_FOREACH(child, &dev->children, link) {
5471 print_device_tree_short(child, indent+1);
5472 }
5473 }
5474
5475 void
print_device_tree(device_t dev,int indent)5476 print_device_tree(device_t dev, int indent)
5477 /* print the device and all its children (indented) */
5478 {
5479 device_t child;
5480
5481 if (!dev)
5482 return;
5483
5484 print_device(dev, indent);
5485
5486 TAILQ_FOREACH(child, &dev->children, link) {
5487 print_device_tree(child, indent+1);
5488 }
5489 }
5490
5491 static void
print_driver_short(driver_t * driver,int indent)5492 print_driver_short(driver_t *driver, int indent)
5493 {
5494 if (!driver)
5495 return;
5496
5497 indentprintf(("driver %s: softc size = %zd\n",
5498 driver->name, driver->size));
5499 }
5500
5501 static void
print_driver(driver_t * driver,int indent)5502 print_driver(driver_t *driver, int indent)
5503 {
5504 if (!driver)
5505 return;
5506
5507 print_driver_short(driver, indent);
5508 }
5509
5510 static void
print_driver_list(driver_list_t drivers,int indent)5511 print_driver_list(driver_list_t drivers, int indent)
5512 {
5513 driverlink_t driver;
5514
5515 TAILQ_FOREACH(driver, &drivers, link) {
5516 print_driver(driver->driver, indent);
5517 }
5518 }
5519
5520 static void
print_devclass_short(devclass_t dc,int indent)5521 print_devclass_short(devclass_t dc, int indent)
5522 {
5523 if ( !dc )
5524 return;
5525
5526 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5527 }
5528
5529 static void
print_devclass(devclass_t dc,int indent)5530 print_devclass(devclass_t dc, int indent)
5531 {
5532 int i;
5533
5534 if ( !dc )
5535 return;
5536
5537 print_devclass_short(dc, indent);
5538 indentprintf(("Drivers:\n"));
5539 print_driver_list(dc->drivers, indent+1);
5540
5541 indentprintf(("Devices:\n"));
5542 for (i = 0; i < dc->maxunit; i++)
5543 if (dc->devices[i])
5544 print_device(dc->devices[i], indent+1);
5545 }
5546
5547 void
print_devclass_list_short(void)5548 print_devclass_list_short(void)
5549 {
5550 devclass_t dc;
5551
5552 printf("Short listing of devclasses, drivers & devices:\n");
5553 TAILQ_FOREACH(dc, &devclasses, link) {
5554 print_devclass_short(dc, 0);
5555 }
5556 }
5557
5558 void
print_devclass_list(void)5559 print_devclass_list(void)
5560 {
5561 devclass_t dc;
5562
5563 printf("Full listing of devclasses, drivers & devices:\n");
5564 TAILQ_FOREACH(dc, &devclasses, link) {
5565 print_devclass(dc, 0);
5566 }
5567 }
5568
5569 #endif
5570
5571 /*
5572 * User-space access to the device tree.
5573 *
5574 * We implement a small set of nodes:
5575 *
5576 * hw.bus Single integer read method to obtain the
5577 * current generation count.
5578 * hw.bus.devices Reads the entire device tree in flat space.
5579 * hw.bus.rman Resource manager interface
5580 *
5581 * We might like to add the ability to scan devclasses and/or drivers to
5582 * determine what else is currently loaded/available.
5583 */
5584
5585 static int
sysctl_bus_info(SYSCTL_HANDLER_ARGS)5586 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5587 {
5588 struct u_businfo ubus;
5589
5590 ubus.ub_version = BUS_USER_VERSION;
5591 ubus.ub_generation = bus_data_generation;
5592
5593 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5594 }
5595 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5596 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5597 "bus-related data");
5598
5599 static int
sysctl_devices(SYSCTL_HANDLER_ARGS)5600 sysctl_devices(SYSCTL_HANDLER_ARGS)
5601 {
5602 struct sbuf sb;
5603 int *name = (int *)arg1;
5604 u_int namelen = arg2;
5605 int index;
5606 device_t dev;
5607 struct u_device *udev;
5608 int error;
5609
5610 if (namelen != 2)
5611 return (EINVAL);
5612
5613 if (bus_data_generation_check(name[0]))
5614 return (EINVAL);
5615
5616 index = name[1];
5617
5618 /*
5619 * Scan the list of devices, looking for the requested index.
5620 */
5621 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5622 if (index-- == 0)
5623 break;
5624 }
5625 if (dev == NULL)
5626 return (ENOENT);
5627
5628 /*
5629 * Populate the return item, careful not to overflow the buffer.
5630 */
5631 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5632 udev->dv_handle = (uintptr_t)dev;
5633 udev->dv_parent = (uintptr_t)dev->parent;
5634 udev->dv_devflags = dev->devflags;
5635 udev->dv_flags = dev->flags;
5636 udev->dv_state = dev->state;
5637 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN);
5638 if (dev->nameunit != NULL)
5639 sbuf_cat(&sb, dev->nameunit);
5640 sbuf_putc(&sb, '\0');
5641 if (dev->desc != NULL)
5642 sbuf_cat(&sb, dev->desc);
5643 sbuf_putc(&sb, '\0');
5644 if (dev->driver != NULL)
5645 sbuf_cat(&sb, dev->driver->name);
5646 sbuf_putc(&sb, '\0');
5647 bus_child_pnpinfo_sb(dev, &sb);
5648 sbuf_putc(&sb, '\0');
5649 bus_child_location_sb(dev, &sb);
5650 sbuf_putc(&sb, '\0');
5651 error = sbuf_finish(&sb);
5652 if (error == 0)
5653 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5654 sbuf_delete(&sb);
5655 free(udev, M_BUS);
5656 return (error);
5657 }
5658
5659 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5660 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5661 "system device tree");
5662
5663 int
bus_data_generation_check(int generation)5664 bus_data_generation_check(int generation)
5665 {
5666 if (generation != bus_data_generation)
5667 return (1);
5668
5669 /* XXX generate optimised lists here? */
5670 return (0);
5671 }
5672
5673 void
bus_data_generation_update(void)5674 bus_data_generation_update(void)
5675 {
5676 atomic_add_int(&bus_data_generation, 1);
5677 }
5678
5679 int
bus_free_resource(device_t dev,int type,struct resource * r)5680 bus_free_resource(device_t dev, int type, struct resource *r)
5681 {
5682 if (r == NULL)
5683 return (0);
5684 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5685 }
5686
5687 device_t
device_lookup_by_name(const char * name)5688 device_lookup_by_name(const char *name)
5689 {
5690 device_t dev;
5691
5692 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5693 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5694 return (dev);
5695 }
5696 return (NULL);
5697 }
5698
5699 /*
5700 * /dev/devctl2 implementation. The existing /dev/devctl device has
5701 * implicit semantics on open, so it could not be reused for this.
5702 * Another option would be to call this /dev/bus?
5703 */
5704 static int
find_device(struct devreq * req,device_t * devp)5705 find_device(struct devreq *req, device_t *devp)
5706 {
5707 device_t dev;
5708
5709 /*
5710 * First, ensure that the name is nul terminated.
5711 */
5712 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5713 return (EINVAL);
5714
5715 /*
5716 * Second, try to find an attached device whose name matches
5717 * 'name'.
5718 */
5719 dev = device_lookup_by_name(req->dr_name);
5720 if (dev != NULL) {
5721 *devp = dev;
5722 return (0);
5723 }
5724
5725 /* Finally, give device enumerators a chance. */
5726 dev = NULL;
5727 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5728 if (dev == NULL)
5729 return (ENOENT);
5730 *devp = dev;
5731 return (0);
5732 }
5733
5734 static bool
driver_exists(device_t bus,const char * driver)5735 driver_exists(device_t bus, const char *driver)
5736 {
5737 devclass_t dc;
5738
5739 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5740 if (devclass_find_driver_internal(dc, driver) != NULL)
5741 return (true);
5742 }
5743 return (false);
5744 }
5745
5746 static void
device_gen_nomatch(device_t dev)5747 device_gen_nomatch(device_t dev)
5748 {
5749 device_t child;
5750
5751 if (dev->flags & DF_NEEDNOMATCH &&
5752 dev->state == DS_NOTPRESENT) {
5753 BUS_PROBE_NOMATCH(dev->parent, dev);
5754 devnomatch(dev);
5755 dev->flags |= DF_DONENOMATCH;
5756 }
5757 dev->flags &= ~DF_NEEDNOMATCH;
5758 TAILQ_FOREACH(child, &dev->children, link) {
5759 device_gen_nomatch(child);
5760 }
5761 }
5762
5763 static void
device_do_deferred_actions(void)5764 device_do_deferred_actions(void)
5765 {
5766 devclass_t dc;
5767 driverlink_t dl;
5768
5769 /*
5770 * Walk through the devclasses to find all the drivers we've tagged as
5771 * deferred during the freeze and call the driver added routines. They
5772 * have already been added to the lists in the background, so the driver
5773 * added routines that trigger a probe will have all the right bidders
5774 * for the probe auction.
5775 */
5776 TAILQ_FOREACH(dc, &devclasses, link) {
5777 TAILQ_FOREACH(dl, &dc->drivers, link) {
5778 if (dl->flags & DL_DEFERRED_PROBE) {
5779 devclass_driver_added(dc, dl->driver);
5780 dl->flags &= ~DL_DEFERRED_PROBE;
5781 }
5782 }
5783 }
5784
5785 /*
5786 * We also defer no-match events during a freeze. Walk the tree and
5787 * generate all the pent-up events that are still relevant.
5788 */
5789 device_gen_nomatch(root_bus);
5790 bus_data_generation_update();
5791 }
5792
5793 static int
devctl2_ioctl(struct cdev * cdev,u_long cmd,caddr_t data,int fflag,struct thread * td)5794 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5795 struct thread *td)
5796 {
5797 struct devreq *req;
5798 device_t dev;
5799 int error, old;
5800
5801 /* Locate the device to control. */
5802 bus_topo_lock();
5803 req = (struct devreq *)data;
5804 switch (cmd) {
5805 case DEV_ATTACH:
5806 case DEV_DETACH:
5807 case DEV_ENABLE:
5808 case DEV_DISABLE:
5809 case DEV_SUSPEND:
5810 case DEV_RESUME:
5811 case DEV_SET_DRIVER:
5812 case DEV_CLEAR_DRIVER:
5813 case DEV_RESCAN:
5814 case DEV_DELETE:
5815 case DEV_RESET:
5816 error = priv_check(td, PRIV_DRIVER);
5817 if (error == 0)
5818 error = find_device(req, &dev);
5819 break;
5820 case DEV_FREEZE:
5821 case DEV_THAW:
5822 error = priv_check(td, PRIV_DRIVER);
5823 break;
5824 default:
5825 error = ENOTTY;
5826 break;
5827 }
5828 if (error) {
5829 bus_topo_unlock();
5830 return (error);
5831 }
5832
5833 /* Perform the requested operation. */
5834 switch (cmd) {
5835 case DEV_ATTACH:
5836 if (device_is_attached(dev))
5837 error = EBUSY;
5838 else if (!device_is_enabled(dev))
5839 error = ENXIO;
5840 else
5841 error = device_probe_and_attach(dev);
5842 break;
5843 case DEV_DETACH:
5844 if (!device_is_attached(dev)) {
5845 error = ENXIO;
5846 break;
5847 }
5848 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5849 error = device_quiesce(dev);
5850 if (error)
5851 break;
5852 }
5853 error = device_detach(dev);
5854 break;
5855 case DEV_ENABLE:
5856 if (device_is_enabled(dev)) {
5857 error = EBUSY;
5858 break;
5859 }
5860
5861 /*
5862 * If the device has been probed but not attached (e.g.
5863 * when it has been disabled by a loader hint), just
5864 * attach the device rather than doing a full probe.
5865 */
5866 device_enable(dev);
5867 if (dev->devclass != NULL) {
5868 /*
5869 * If the device was disabled via a hint, clear
5870 * the hint.
5871 */
5872 if (resource_disabled(dev->devclass->name, dev->unit))
5873 resource_unset_value(dev->devclass->name,
5874 dev->unit, "disabled");
5875
5876 /* Allow any drivers to rebid. */
5877 if (!(dev->flags & DF_FIXEDCLASS))
5878 devclass_delete_device(dev->devclass, dev);
5879 }
5880 error = device_probe_and_attach(dev);
5881 break;
5882 case DEV_DISABLE:
5883 if (!device_is_enabled(dev)) {
5884 error = ENXIO;
5885 break;
5886 }
5887
5888 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5889 error = device_quiesce(dev);
5890 if (error)
5891 break;
5892 }
5893
5894 /*
5895 * Force DF_FIXEDCLASS on around detach to preserve
5896 * the existing name.
5897 */
5898 old = dev->flags;
5899 dev->flags |= DF_FIXEDCLASS;
5900 error = device_detach(dev);
5901 if (!(old & DF_FIXEDCLASS))
5902 dev->flags &= ~DF_FIXEDCLASS;
5903 if (error == 0)
5904 device_disable(dev);
5905 break;
5906 case DEV_SUSPEND:
5907 if (device_is_suspended(dev)) {
5908 error = EBUSY;
5909 break;
5910 }
5911 if (device_get_parent(dev) == NULL) {
5912 error = EINVAL;
5913 break;
5914 }
5915 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5916 break;
5917 case DEV_RESUME:
5918 if (!device_is_suspended(dev)) {
5919 error = EINVAL;
5920 break;
5921 }
5922 if (device_get_parent(dev) == NULL) {
5923 error = EINVAL;
5924 break;
5925 }
5926 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5927 break;
5928 case DEV_SET_DRIVER: {
5929 devclass_t dc;
5930 char driver[128];
5931
5932 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5933 if (error)
5934 break;
5935 if (driver[0] == '\0') {
5936 error = EINVAL;
5937 break;
5938 }
5939 if (dev->devclass != NULL &&
5940 strcmp(driver, dev->devclass->name) == 0)
5941 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5942 break;
5943
5944 /*
5945 * Scan drivers for this device's bus looking for at
5946 * least one matching driver.
5947 */
5948 if (dev->parent == NULL) {
5949 error = EINVAL;
5950 break;
5951 }
5952 if (!driver_exists(dev->parent, driver)) {
5953 error = ENOENT;
5954 break;
5955 }
5956 dc = devclass_create(driver);
5957 if (dc == NULL) {
5958 error = ENOMEM;
5959 break;
5960 }
5961
5962 /* Detach device if necessary. */
5963 if (device_is_attached(dev)) {
5964 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5965 error = device_detach(dev);
5966 else
5967 error = EBUSY;
5968 if (error)
5969 break;
5970 }
5971
5972 /* Clear any previously-fixed device class and unit. */
5973 if (dev->flags & DF_FIXEDCLASS)
5974 devclass_delete_device(dev->devclass, dev);
5975 dev->flags |= DF_WILDCARD;
5976 dev->unit = -1;
5977
5978 /* Force the new device class. */
5979 error = devclass_add_device(dc, dev);
5980 if (error)
5981 break;
5982 dev->flags |= DF_FIXEDCLASS;
5983 error = device_probe_and_attach(dev);
5984 break;
5985 }
5986 case DEV_CLEAR_DRIVER:
5987 if (!(dev->flags & DF_FIXEDCLASS)) {
5988 error = 0;
5989 break;
5990 }
5991 if (device_is_attached(dev)) {
5992 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5993 error = device_detach(dev);
5994 else
5995 error = EBUSY;
5996 if (error)
5997 break;
5998 }
5999
6000 dev->flags &= ~DF_FIXEDCLASS;
6001 dev->flags |= DF_WILDCARD;
6002 devclass_delete_device(dev->devclass, dev);
6003 error = device_probe_and_attach(dev);
6004 break;
6005 case DEV_RESCAN:
6006 if (!device_is_attached(dev)) {
6007 error = ENXIO;
6008 break;
6009 }
6010 error = BUS_RESCAN(dev);
6011 break;
6012 case DEV_DELETE: {
6013 device_t parent;
6014
6015 parent = device_get_parent(dev);
6016 if (parent == NULL) {
6017 error = EINVAL;
6018 break;
6019 }
6020 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
6021 if (bus_child_present(dev) != 0) {
6022 error = EBUSY;
6023 break;
6024 }
6025 }
6026
6027 error = device_delete_child(parent, dev);
6028 break;
6029 }
6030 case DEV_FREEZE:
6031 if (device_frozen)
6032 error = EBUSY;
6033 else
6034 device_frozen = true;
6035 break;
6036 case DEV_THAW:
6037 if (!device_frozen)
6038 error = EBUSY;
6039 else {
6040 device_do_deferred_actions();
6041 device_frozen = false;
6042 }
6043 break;
6044 case DEV_RESET:
6045 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
6046 error = EINVAL;
6047 break;
6048 }
6049 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
6050 req->dr_flags);
6051 break;
6052 }
6053 bus_topo_unlock();
6054 return (error);
6055 }
6056
6057 static struct cdevsw devctl2_cdevsw = {
6058 .d_version = D_VERSION,
6059 .d_ioctl = devctl2_ioctl,
6060 .d_name = "devctl2",
6061 };
6062
6063 static void
devctl2_init(void)6064 devctl2_init(void)
6065 {
6066 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
6067 UID_ROOT, GID_WHEEL, 0600, "devctl2");
6068 }
6069
6070 /*
6071 * APIs to manage deprecation and obsolescence.
6072 */
6073 static int obsolete_panic = 0;
6074 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
6075 "Panic when obsolete features are used (0 = never, 1 = if obsolete, "
6076 "2 = if deprecated)");
6077
6078 static void
gone_panic(int major,int running,const char * msg)6079 gone_panic(int major, int running, const char *msg)
6080 {
6081 switch (obsolete_panic)
6082 {
6083 case 0:
6084 return;
6085 case 1:
6086 if (running < major)
6087 return;
6088 /* FALLTHROUGH */
6089 default:
6090 panic("%s", msg);
6091 }
6092 }
6093
6094 void
_gone_in(int major,const char * msg)6095 _gone_in(int major, const char *msg)
6096 {
6097 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
6098 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
6099 printf("Obsolete code will be removed soon: %s\n", msg);
6100 else
6101 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
6102 major, msg);
6103 }
6104
6105 void
_gone_in_dev(device_t dev,int major,const char * msg)6106 _gone_in_dev(device_t dev, int major, const char *msg)
6107 {
6108 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
6109 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
6110 device_printf(dev,
6111 "Obsolete code will be removed soon: %s\n", msg);
6112 else
6113 device_printf(dev,
6114 "Deprecated code (to be removed in FreeBSD %d): %s\n",
6115 major, msg);
6116 }
6117
6118 #ifdef DDB
DB_SHOW_COMMAND(device,db_show_device)6119 DB_SHOW_COMMAND(device, db_show_device)
6120 {
6121 device_t dev;
6122
6123 if (!have_addr)
6124 return;
6125
6126 dev = (device_t)addr;
6127
6128 db_printf("name: %s\n", device_get_nameunit(dev));
6129 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
6130 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
6131 db_printf(" addr: %p\n", dev);
6132 db_printf(" parent: %p\n", dev->parent);
6133 db_printf(" softc: %p\n", dev->softc);
6134 db_printf(" ivars: %p\n", dev->ivars);
6135 }
6136
DB_SHOW_ALL_COMMAND(devices,db_show_all_devices)6137 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
6138 {
6139 device_t dev;
6140
6141 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
6142 db_show_device((db_expr_t)dev, true, count, modif);
6143 }
6144 }
6145 #endif
6146