13ce5372Robert Mustacchi/*
23ce5372Robert Mustacchi * This file and its contents are supplied under the terms of the
33ce5372Robert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
43ce5372Robert Mustacchi * You may only use this file in accordance with the terms of version
53ce5372Robert Mustacchi * 1.0 of the CDDL.
63ce5372Robert Mustacchi *
73ce5372Robert Mustacchi * A full copy of the text of the CDDL should have accompanied this
83ce5372Robert Mustacchi * source.  A copy of the CDDL is also available via the Internet at
93ce5372Robert Mustacchi * http://www.illumos.org/license/CDDL.
103ce5372Robert Mustacchi */
113ce5372Robert Mustacchi
123ce5372Robert Mustacchi/*
133ce5372Robert Mustacchi * Copyright 2020 Oxide Computer Company
143ce5372Robert Mustacchi */
153ce5372Robert Mustacchi
163ce5372Robert Mustacchi/*
173ce5372Robert Mustacchi * Kernel Sensor Framework
183ce5372Robert Mustacchi *
193ce5372Robert Mustacchi * The kernel sensor framework exists to provide a simple and straightforward
203ce5372Robert Mustacchi * means for various parts of the system to declare and instantiate sensor
213ce5372Robert Mustacchi * information. Between this and the ksensor character device
223ce5372Robert Mustacchi * (uts/common/io/ksensor/ksensor_drv.c) this exposes per-device sensors and
233ce5372Robert Mustacchi * character devices.
243ce5372Robert Mustacchi *
253ce5372Robert Mustacchi * --------------------------
263ce5372Robert Mustacchi * Driver and User Interfaces
273ce5372Robert Mustacchi * --------------------------
283ce5372Robert Mustacchi *
293ce5372Robert Mustacchi * Each sensor that is registered with the framework is exposed as a character
303ce5372Robert Mustacchi * device under /dev/sensors. The device class and node name are often ':'
313ce5372Robert Mustacchi * delineated and must begin with 'ddi_sensor'. Everything after 'ddi_sensor'
323ce5372Robert Mustacchi * will be created in a directory under /dev/sensors. So for example the Intel
333ce5372Robert Mustacchi * PCH driver uses a class "ddi_sensor:temperature:pch" and a node name of
343ce5372Robert Mustacchi * 'ts.%d'. This creates the node /dev/sensors/temperature/pch/ts.0. The
353ce5372Robert Mustacchi * devfsadm plugin automatically handles the creation of directories which makes
363ce5372Robert Mustacchi * the addition of additional sensor types easy to create.
373ce5372Robert Mustacchi *
383ce5372Robert Mustacchi * Strictly speaking, any device can manage their own sensors and minor nodes by
393ce5372Robert Mustacchi * using the appropriate class and implementing the corresponding ioctls. That
403ce5372Robert Mustacchi * was how the first kernel sensors were written; however, there are a lot of
413ce5372Robert Mustacchi * issues with that which led to this:
423ce5372Robert Mustacchi *
433ce5372Robert Mustacchi * 1. Every driver had to actually implement character devices.
443ce5372Robert Mustacchi *
453ce5372Robert Mustacchi * 2. Every driver had to duplicate a lot of the logic around open(9E),
463ce5372Robert Mustacchi *    close(9E), and ioctl(9E).
473ce5372Robert Mustacchi *
483ce5372Robert Mustacchi * 3. Drivers that tied into frameworks like mac(9E) or SCSAv3 needed a lot more
493ce5372Robert Mustacchi *    work to fit into this model. For example, because the minor state is
503ce5372Robert Mustacchi *    shared between all the instances and the frameworks, they would have
513ce5372Robert Mustacchi *    required shared, global state that they don't have today.
523ce5372Robert Mustacchi *
533ce5372Robert Mustacchi * Ultimately, having an operations vector and a callback argument makes work a
543ce5372Robert Mustacchi * lot simpler for the producers of sensor data and that simplicity makes it
553ce5372Robert Mustacchi * worthwhile to take on additional effort and work here.
563ce5372Robert Mustacchi *
573ce5372Robert Mustacchi * ----------
583ce5372Robert Mustacchi * Components
593ce5372Robert Mustacchi * ----------
603ce5372Robert Mustacchi *
613ce5372Robert Mustacchi * The ksensor framework is made of a couple of different pieces:
623ce5372Robert Mustacchi *
633ce5372Robert Mustacchi * 1. This glue that is a part of genunix.
643ce5372Robert Mustacchi * 2. The ksensor character device driver.
653ce5372Robert Mustacchi * 3. Sensor providers, which are generally drivers that register with the
663ce5372Robert Mustacchi *    ksensor framework.
673ce5372Robert Mustacchi *
683ce5372Robert Mustacchi * The implementation of (1) is all in this file. The implementation of (2) is
693ce5372Robert Mustacchi * in uts/common/io/ksensor/ksensor_drv.c. The implementation of (3) is found in
703ce5372Robert Mustacchi * all of the different leaf devices. Examples of (3) include pchtemp(7D) and
713ce5372Robert Mustacchi * igb(7D).
723ce5372Robert Mustacchi *
733ce5372Robert Mustacchi * We separate numbers one and two into two different components for a few
743ce5372Robert Mustacchi * reasons. The most important thing is that drivers that provide sensors should
753ce5372Robert Mustacchi * not be dependent on some other part of the system having been loaded. This
763ce5372Robert Mustacchi * makes a compelling argument for it being a part of the core kernel. However,
773ce5372Robert Mustacchi * like other subsystems (e.g. kstats, smbios, etc.), it's useful to separate
783ce5372Robert Mustacchi * out the thing that provides the interface to users with the thing that is
793ce5372Robert Mustacchi * used to glue together providers in the kernel. There's the added benefit that
803ce5372Robert Mustacchi * it's practically simpler to spin up a pseudo-device through a module.
813ce5372Robert Mustacchi *
823ce5372Robert Mustacchi * The ksensor character device driver (2) registers with the main genunix
833ce5372Robert Mustacchi * ksensor code (1) when it attaches and when it detaches. The kernel only
843ce5372Robert Mustacchi * allows a single driver to be attached to it. When that character device
853ce5372Robert Mustacchi * driver attaches, the ksensor framework will walk through all of the currently
863ce5372Robert Mustacchi * registered sensors and inform the character device driver of the nodes that
873ce5372Robert Mustacchi * it needs to create. While the character device driver is attached, the
883ce5372Robert Mustacchi * ksensor framework will also call back into it when a sensor needs to be
893ce5372Robert Mustacchi * removed.
903ce5372Robert Mustacchi *
913ce5372Robert Mustacchi * Generally speaking, this distinction of responsibilities allows the kernel
923ce5372Robert Mustacchi * sensor character device driver to attach and detach without impact to the
933ce5372Robert Mustacchi * sensor providers or them even being notified at all, it's all transparent to
943ce5372Robert Mustacchi * them.
953ce5372Robert Mustacchi *
963ce5372Robert Mustacchi * ------------------------------
973ce5372Robert Mustacchi * Sensor Lifetime and detach(9E)
983ce5372Robert Mustacchi * ------------------------------
993ce5372Robert Mustacchi *
1003ce5372Robert Mustacchi * Traditionally, a device driver may be detached by the broader kernel whenever
1013ce5372Robert Mustacchi * the kernel desires it. On debug builds this happens by a dedicated thread. On
1023ce5372Robert Mustacchi * a non-debug build this may happen due to memory pressure or as an attempt to
1033ce5372Robert Mustacchi * reclaim idle resources (though this is much less common). However, when the
1043ce5372Robert Mustacchi * module is detached, the system remembers that minor nodes previously existed
1053ce5372Robert Mustacchi * and that entries in /devices had been created. When something proceeds to
1063ce5372Robert Mustacchi * access an entry in /devices again, the system will use that to bring a driver
1073ce5372Robert Mustacchi * back to life. It doesn't matter whether it's a pseudo-device driver or
1083ce5372Robert Mustacchi * something else, this can happen.
1093ce5372Robert Mustacchi *
1103ce5372Robert Mustacchi * One downside to the sensor framework, is that we need to emulate this
1113ce5372Robert Mustacchi * behavior which leads to some amount of complexity here. But this is a
1123ce5372Robert Mustacchi * worthwhile tradeoff as it makes things much simpler for providers and it's
1133ce5372Robert Mustacchi * not too hard for us to emulate this behavior.
1143ce5372Robert Mustacchi *
1153ce5372Robert Mustacchi * When a sensor provider registers the sensor, the sensor becomes available to
1163ce5372Robert Mustacchi * the system. When the sensor provider unregisters with the system, which
1173ce5372Robert Mustacchi * happens during its detach routine, then we note that it has been detached;
1183ce5372Robert Mustacchi * however, we don't delete its minor node and if something accesses it, we
1193ce5372Robert Mustacchi * attempt to load the driver again, the same way that devfs (the file system
1203ce5372Robert Mustacchi * behind /devices) does.
1213ce5372Robert Mustacchi *
1223ce5372Robert Mustacchi * For each dev_info_t that registers a sensor we register a callback such that
1233ce5372Robert Mustacchi * when the device is removed, e.g. someone called rem_drv or physically pulls
1243ce5372Robert Mustacchi * the device, then we'll be able to finally clean up the device. This lifetime
1253ce5372Robert Mustacchi * can be represented in the following image:
1263ce5372Robert Mustacchi *
1273ce5372Robert Mustacchi *         |
1283ce5372Robert Mustacchi *         |
1293ce5372Robert Mustacchi *         +-----<-------------------------------------+
1303ce5372Robert Mustacchi *         |                                           |
1313ce5372Robert Mustacchi *         | . . call ksensor_create()                 |
1323ce5372Robert Mustacchi *         v                                           |
1333ce5372Robert Mustacchi *     +-------+                                       |
1343ce5372Robert Mustacchi *     | Valid |                                       |
1353ce5372Robert Mustacchi *     +-------+                                       |
1363ce5372Robert Mustacchi *         |                                           ^
1373ce5372Robert Mustacchi *         | . . call ksensor_remove()                 |
1383ce5372Robert Mustacchi *         v                                           |
1393ce5372Robert Mustacchi *    +---------+                                      |
1403ce5372Robert Mustacchi *    | Invalid |                                      |
1413ce5372Robert Mustacchi *    +---------+                                      |
1423ce5372Robert Mustacchi *      |     |                                        |
1433ce5372Robert Mustacchi *      |     | . . user uses sensor again             |
1443ce5372Robert Mustacchi *      |     |                                        |
1453ce5372Robert Mustacchi *      |     +-------------------+                    |
1463ce5372Robert Mustacchi *      |                         |                    |
1473ce5372Robert Mustacchi *      |                         v                    |
1483ce5372Robert Mustacchi *      |                 +---------------+            |
1493ce5372Robert Mustacchi *      |                 | Attatching... |-->---------+
1503ce5372Robert Mustacchi *      |                 +---------------+
1513ce5372Robert Mustacchi *      | . . ddi unbind cb       |
1523ce5372Robert Mustacchi *      |                         |
1533ce5372Robert Mustacchi *      v                         | . . attatch fails or
1543ce5372Robert Mustacchi *   +---------+                  |     no call to ksensor_create()
1553ce5372Robert Mustacchi *   | Deleted |--<---------------+     again
1563ce5372Robert Mustacchi *   +---------+
1573ce5372Robert Mustacchi *
1583ce5372Robert Mustacchi * When the DDI unbind callback is called, we know that the device is going to
1593ce5372Robert Mustacchi * be removed. However, this happens within a subtle context with a majority of
1603ce5372Robert Mustacchi * the device tree held (at least the dip's parent). In particular, another
1613ce5372Robert Mustacchi * thread may be trying to obtain a hold on it and be blocked in
1623ce5372Robert Mustacchi * ndi_devi_enter(). As the callback thread holds that, that could lead to a
1633ce5372Robert Mustacchi * deadlock. As a result, we clean things up in two phases. One during the
1643ce5372Robert Mustacchi * synchronous callback and the other via a taskq. In the first phase we
1653ce5372Robert Mustacchi * logically do the following:
1663ce5372Robert Mustacchi *
1673ce5372Robert Mustacchi *  o Remove the dip from the list of ksensor dips and set the flag that
1683ce5372Robert Mustacchi *    indicates that it's been removed.
1693ce5372Robert Mustacchi *  o Remove all of the sensors from the global avl to make sure that new
1703ce5372Robert Mustacchi *    threads cannot look it up.
1713ce5372Robert Mustacchi *
1723ce5372Robert Mustacchi * Then, after the taskq is dispatched, we do the following in taskq context:
1733ce5372Robert Mustacchi *
1743ce5372Robert Mustacchi *  o Tell the ksensor driver that it should remove the minor node.
1753ce5372Robert Mustacchi *  o Block on each sensor until it is no-longer busy and then clean it up.
1763ce5372Robert Mustacchi *  o Clean up the ksensor_dip_t.
1773ce5372Robert Mustacchi *
1783ce5372Robert Mustacchi * ------------------
1793ce5372Robert Mustacchi * Accessing a Sensor
1803ce5372Robert Mustacchi * ------------------
1813ce5372Robert Mustacchi *
1823ce5372Robert Mustacchi * Access to a particular sensor is serialized in the system. In addition to
1833ce5372Robert Mustacchi * that, a number of steps are required to access one that is not unlike
1843ce5372Robert Mustacchi * accessing a character device. When a given sensor is held the KSENSOR_F_BUSY
1853ce5372Robert Mustacchi * flag is set in the ksensor_flags member. In addition, as part of taking a
1863ce5372Robert Mustacchi * hold a number of side effects occur that ensure that the sensor provider's
1873ce5372Robert Mustacchi * dev_info_t is considered busy and can't be detached.
1883ce5372Robert Mustacchi *
1893ce5372Robert Mustacchi * To obtain a hold on a sensor the following logical steps are required (see
1903ce5372Robert Mustacchi * ksensor_hold_by_id() for the implementation):
1913ce5372Robert Mustacchi *
1923ce5372Robert Mustacchi *  1. Map the minor to the ksensor_t via the avl tree
1933ce5372Robert Mustacchi *  2. Check that the ksensor's dip is valid
1943ce5372Robert Mustacchi *  3. If the sensor is busy, wait until it is no longer so, and restart from
1953ce5372Robert Mustacchi *     the top. Otherwise, mark the sensor as busy.
1963ce5372Robert Mustacchi *  4. Enter the parent and place a hold on the sensor provider's dip.
1973ce5372Robert Mustacchi *  5. Once again check if the dip is removed or not because we have to drop
1983ce5372Robert Mustacchi *     locks during that operation.
1993ce5372Robert Mustacchi *  6. Check if the ksensor has the valid flag set. If not, attempt to configure
2003ce5372Robert Mustacchi *     the dip.
2013ce5372Robert Mustacchi *  7. Assuming the sensor is now valid, we can return it.
2023ce5372Robert Mustacchi *
2033ce5372Robert Mustacchi * After this point, the sensor is considered valid for use. Once the consumer
2043ce5372Robert Mustacchi * is finished with the sensor, it should be released by calling
2053ce5372Robert Mustacchi * ksensor_release().
2063ce5372Robert Mustacchi *
2073ce5372Robert Mustacchi * An important aspect of the above scheme is that the KSENSOR_F_BUSY flag is
2083ce5372Robert Mustacchi * required to progress through the validation and holding of the device. This
2093ce5372Robert Mustacchi * makes sure that only one thread is attempting to attach it at a given time. A
2103ce5372Robert Mustacchi * reasonable future optimization would be to amortize this cost in open(9E)
2113ce5372Robert Mustacchi * and close(9E) of the minor and to bump a count as it being referenced as long
2123ce5372Robert Mustacchi * as it is open.
2133ce5372Robert Mustacchi *
2143ce5372Robert Mustacchi * -----------------------------
2153ce5372Robert Mustacchi * Character Device Registration
2163ce5372Robert Mustacchi * -----------------------------
2173ce5372Robert Mustacchi *
2183ce5372Robert Mustacchi * The 'ksensor' character device driver can come and go. To support this, the
2193ce5372Robert Mustacchi * ksensor framework communicates with the ksensor character device by a
2203ce5372Robert Mustacchi * well-defined set of callbacks, used to indicate sensor addition and removal.
2213ce5372Robert Mustacchi * The ksensor character device is found in uts/common/io/ksensor/ksensor_drv.c.
2223ce5372Robert Mustacchi * The ksensor character device is responsible for creating and destroying minor
2233ce5372Robert Mustacchi * nodes.
2243ce5372Robert Mustacchi *
2253ce5372Robert Mustacchi * Each ksensor_t has a flag, KSENSOR_F_NOTIFIED, that is used to indicate
2263ce5372Robert Mustacchi * whether or not the registered driver has been notified of the sensor. When a
2273ce5372Robert Mustacchi * callback is first registered, we'll walk through the entire list of nodes to
2283ce5372Robert Mustacchi * make sure that its minor has been created. When unregistering, the minor node
2293ce5372Robert Mustacchi * remove callback will not be called; however, this can generally by dealt with
2303ce5372Robert Mustacchi * by calling something like ddi_remove_minor_node(dip, NULL).
2313ce5372Robert Mustacchi *
2323ce5372Robert Mustacchi * -------
2333ce5372Robert Mustacchi * Locking
2343ce5372Robert Mustacchi * -------
2353ce5372Robert Mustacchi *
2363ce5372Robert Mustacchi * The following rules apply to dealing with lock ordering:
2373ce5372Robert Mustacchi *
2383ce5372Robert Mustacchi * 1. The global ksensor_g_mutex protects all global data and must be taken
2393ce5372Robert Mustacchi *    before a ksensor_t's individual mutex.
2403ce5372Robert Mustacchi *
2413ce5372Robert Mustacchi * 2. A thread should not hold any two ksensor_t's mutex at any time.
2423ce5372Robert Mustacchi *
2433ce5372Robert Mustacchi * 3. No locks should be held when attempting to grab or manipulate a
2443ce5372Robert Mustacchi *    dev_info_t, e.g. ndi_devi_enter().
2453ce5372Robert Mustacchi *
2463ce5372Robert Mustacchi * 4. Unless the ksensor is actively being held, whenever a ksensor is found,
2473ce5372Robert Mustacchi *    one must check whether the ksensor_dip_t flag KSENSOR_DIP_F_REMOVED is
2483ce5372Robert Mustacchi *    set or not and whether the ksensor_t's KSENSOR_F_VALID flag is set.
2493ce5372Robert Mustacchi */
2503ce5372Robert Mustacchi
2513ce5372Robert Mustacchi#include <sys/types.h>
2523ce5372Robert Mustacchi#include <sys/file.h>
2533ce5372Robert Mustacchi#include <sys/errno.h>
2543ce5372Robert Mustacchi#include <sys/cred.h>
2553ce5372Robert Mustacchi#include <sys/ddi.h>
2563ce5372Robert Mustacchi#include <sys/stat.h>
2573ce5372Robert Mustacchi#include <sys/sunddi.h>
2583ce5372Robert Mustacchi#include <sys/sunndi.h>
2593ce5372Robert Mustacchi#include <sys/esunddi.h>
2603ce5372Robert Mustacchi#include <sys/ksensor_impl.h>
2613ce5372Robert Mustacchi#include <sys/ddi_impldefs.h>
2623ce5372Robert Mustacchi#include <sys/pci.h>
2633ce5372Robert Mustacchi#include <sys/avl.h>
2643ce5372Robert Mustacchi#include <sys/list.h>
2653ce5372Robert Mustacchi#include <sys/stddef.h>
2663ce5372Robert Mustacchi#include <sys/sysmacros.h>
2673ce5372Robert Mustacchi#include <sys/fs/dv_node.h>
2683ce5372Robert Mustacchi
2693ce5372Robert Mustacchitypedef enum {
2703ce5372Robert Mustacchi	/*
2713ce5372Robert Mustacchi	 * This flag indicates that the subscribing ksensor character device has
2723ce5372Robert Mustacchi	 * been notified about this flag.
2733ce5372Robert Mustacchi	 */
2743ce5372Robert Mustacchi	KSENSOR_F_NOTIFIED	= 1 << 0,
2753ce5372Robert Mustacchi	/*
2763ce5372Robert Mustacchi	 * This indicates that the sensor is currently valid, meaning that the
2773ce5372Robert Mustacchi	 * ops vector and argument are safe to use. This is removed when a
2783ce5372Robert Mustacchi	 * driver with a sensor is detached.
2793ce5372Robert Mustacchi	 */
2803ce5372Robert Mustacchi	KSENSOR_F_VALID		= 1 << 1,
2813ce5372Robert Mustacchi	/*
2823ce5372Robert Mustacchi	 * Indicates that a client has a hold on the sensor for some purpose.
2833ce5372Robert Mustacchi	 * This must be set before trying to get an NDI hold. Once this is set
2843ce5372Robert Mustacchi	 * and a NDI hold is in place, it is safe to use the operations vector
2853ce5372Robert Mustacchi	 * and argument.
2863ce5372Robert Mustacchi	 */
2873ce5372Robert Mustacchi	KSENSOR_F_BUSY		= 1 << 2,
2883ce5372Robert Mustacchi} ksensor_flags_t;
2893ce5372Robert Mustacchi
2903ce5372Robert Mustacchitypedef enum {
2913ce5372Robert Mustacchi	KSENSOR_DIP_F_REMOVED	= 1 << 0
2923ce5372Robert Mustacchi} ksensor_dip_flags_t;
2933ce5372Robert Mustacchi
2943ce5372Robert Mustacchitypedef struct {
2953ce5372Robert Mustacchi	list_node_t ksdip_link;
2963ce5372Robert Mustacchi	ksensor_dip_flags_t ksdip_flags;
2973ce5372Robert Mustacchi	dev_info_t *ksdip_dip;
2983ce5372Robert Mustacchi	ddi_unbind_callback_t ksdip_cb;
2993ce5372Robert Mustacchi	list_t ksdip_sensors;
3003ce5372Robert Mustacchi} ksensor_dip_t;
3013ce5372Robert Mustacchi
3023ce5372Robert Mustacchitypedef struct {
3033ce5372Robert Mustacchi	kmutex_t ksensor_mutex;
3043ce5372Robert Mustacchi	kcondvar_t ksensor_cv;
3053ce5372Robert Mustacchi	ksensor_flags_t ksensor_flags;
3063ce5372Robert Mustacchi	list_node_t ksensor_dip_list;
3073ce5372Robert Mustacchi	avl_node_t ksensor_id_avl;
3083ce5372Robert Mustacchi	uint_t ksensor_nwaiters;
3093ce5372Robert Mustacchi	ksensor_dip_t *ksensor_ksdip;
3103ce5372Robert Mustacchi	char *ksensor_name;
3113ce5372Robert Mustacchi	char *ksensor_class;
3123ce5372Robert Mustacchi	id_t ksensor_id;
3133ce5372Robert Mustacchi	const ksensor_ops_t *ksensor_ops;
3143ce5372Robert Mustacchi	void *ksensor_arg;
3153ce5372Robert Mustacchi} ksensor_t;
3163ce5372Robert Mustacchi
3173ce5372Robert Mustacchistatic kmutex_t ksensor_g_mutex;
3183ce5372Robert Mustacchistatic id_space_t *ksensor_ids;
3193ce5372Robert Mustacchistatic list_t ksensor_dips;
3203ce5372Robert Mustacchistatic avl_tree_t ksensor_avl;
3213ce5372Robert Mustacchistatic dev_info_t *ksensor_cb_dip;
3223ce5372Robert Mustacchistatic ksensor_create_f ksensor_cb_create;
3233ce5372Robert Mustacchistatic ksensor_remove_f ksensor_cb_remove;
3243ce5372Robert Mustacchi
3253ce5372Robert Mustacchistatic int
3263ce5372Robert Mustacchiksensor_avl_compare(const void *l, const void *r)
3273ce5372Robert Mustacchi{
3283ce5372Robert Mustacchi	const ksensor_t *kl = l;
3293ce5372Robert Mustacchi	const ksensor_t *kr = r;
3303ce5372Robert Mustacchi
3313ce5372Robert Mustacchi	if (kl->ksensor_id > kr->ksensor_id) {
3323ce5372Robert Mustacchi		return (1);
3333ce5372Robert Mustacchi	} else if (kl->ksensor_id < kr->ksensor_id) {
3343ce5372Robert Mustacchi		return (-1);
3353ce5372Robert Mustacchi	} else {
3363ce5372Robert Mustacchi		return (0);
3373ce5372Robert Mustacchi	}
3383ce5372Robert Mustacchi}
3393ce5372Robert Mustacchi
3403ce5372Robert Mustacchistatic ksensor_t *
3413ce5372Robert Mustacchiksensor_find_by_id(id_t id)
3423ce5372Robert Mustacchi{
3433ce5372Robert Mustacchi	ksensor_t k, *ret;
3443ce5372Robert Mustacchi
3453ce5372Robert Mustacchi	ASSERT(MUTEX_HELD(&ksensor_g_mutex));
3463ce5372Robert Mustacchi
3473ce5372Robert Mustacchi	k.ksensor_id = id;
3483ce5372Robert Mustacchi	return (avl_find(&ksensor_avl, &k, NULL));
3493ce5372Robert Mustacchi
3503ce5372Robert Mustacchi}
3513ce5372Robert Mustacchi
3523ce5372Robert Mustacchistatic ksensor_t *
3533ce5372Robert Mustacchiksensor_search_ksdip(ksensor_dip_t *ksdip, const char *name, const char *class)
3543ce5372Robert Mustacchi{
3553ce5372Robert Mustacchi	ksensor_t *s;
3563ce5372Robert Mustacchi
3573ce5372Robert Mustacchi	ASSERT(MUTEX_HELD(&ksensor_g_mutex));
3583ce5372Robert Mustacchi
3593ce5372Robert Mustacchi	for (s = list_head(&ksdip->ksdip_sensors); s != NULL;
3603ce5372Robert Mustacchi	    s = list_next(&ksdip->ksdip_sensors, s)) {
3613ce5372Robert Mustacchi		if (strcmp(s->ksensor_name, name) == 0 &&
3623ce5372Robert Mustacchi		    strcmp(s->ksensor_class, class) == 0) {
3633ce5372Robert Mustacchi			return (s);
3643ce5372Robert Mustacchi		}
3653ce5372Robert Mustacchi	}
3663ce5372Robert Mustacchi
3673ce5372Robert Mustacchi	return (NULL);
3683ce5372Robert Mustacchi}
3693ce5372Robert Mustacchi
3703ce5372Robert Mustacchistatic void
3713ce5372Robert Mustacchiksensor_free_sensor(ksensor_t *sensor)
3723ce5372Robert Mustacchi{
3733ce5372Robert Mustacchi	strfree(sensor->ksensor_name);
3743ce5372Robert Mustacchi	strfree(sensor->ksensor_class);
3753ce5372Robert Mustacchi	id_free(ksensor_ids, sensor->ksensor_id);
3763ce5372Robert Mustacchi	mutex_destroy(&sensor->ksensor_mutex);
3773ce5372Robert Mustacchi	kmem_free(sensor, sizeof (ksensor_t));
3783ce5372Robert Mustacchi}
3793ce5372Robert Mustacchi
3803ce5372Robert Mustacchistatic void
3813ce5372Robert Mustacchiksensor_free_dip(ksensor_dip_t *ksdip)
3823ce5372Robert Mustacchi{
3833ce5372Robert Mustacchi	list_destroy(&ksdip->ksdip_sensors);
3843ce5372Robert Mustacchi	kmem_free(ksdip, sizeof (ksensor_dip_t));
3853ce5372Robert Mustacchi}
3863ce5372Robert Mustacchi
3873ce5372Robert Mustacchistatic void
3883ce5372Robert Mustacchiksensor_dip_unbind_taskq(void *arg)
3893ce5372Robert Mustacchi{
3903ce5372Robert Mustacchi	ksensor_dip_t *k = arg;
3913ce5372Robert Mustacchi	ksensor_t *sensor;
3923ce5372Robert Mustacchi
3933ce5372Robert Mustacchi	/*
3943ce5372Robert Mustacchi	 * First notify an attached driver that the nodes are going away
3953ce5372Robert Mustacchi	 * before we block and wait on them.
3963ce5372Robert Mustacchi	 */
3973ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
3983ce5372Robert Mustacchi	for (sensor = list_head(&k->ksdip_sensors); sensor != NULL;
3993ce5372Robert Mustacchi	    sensor = list_next(&k->ksdip_sensors, sensor)) {
4003ce5372Robert Mustacchi		mutex_enter(&sensor->ksensor_mutex);
4013ce5372Robert Mustacchi		if (sensor->ksensor_flags & KSENSOR_F_NOTIFIED) {
4023ce5372Robert Mustacchi			ksensor_cb_remove(sensor->ksensor_id,
4033ce5372Robert Mustacchi			    sensor->ksensor_name);
4043ce5372Robert Mustacchi			sensor->ksensor_flags &= ~KSENSOR_F_NOTIFIED;
4053ce5372Robert Mustacchi		}
4063ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
4073ce5372Robert Mustacchi	}
4083ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
4093ce5372Robert Mustacchi
4103ce5372Robert Mustacchi	/*
4113ce5372Robert Mustacchi	 * Now that the driver has destroyed its minor, wait for anything that's
4123ce5372Robert Mustacchi	 * still there.
4133ce5372Robert Mustacchi	 */
4143ce5372Robert Mustacchi	while ((sensor = list_remove_head(&k->ksdip_sensors)) != NULL) {
4153ce5372Robert Mustacchi		mutex_enter(&sensor->ksensor_mutex);
4163ce5372Robert Mustacchi		while ((sensor->ksensor_flags & KSENSOR_F_BUSY) != 0 ||
4173ce5372Robert Mustacchi		    sensor->ksensor_nwaiters > 0) {
4183ce5372Robert Mustacchi			cv_wait(&sensor->ksensor_cv, &sensor->ksensor_mutex);
4193ce5372Robert Mustacchi		}
4203ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
4213ce5372Robert Mustacchi		ksensor_free_sensor(sensor);
4223ce5372Robert Mustacchi	}
4233ce5372Robert Mustacchi	ksensor_free_dip(k);
4243ce5372Robert Mustacchi}
4253ce5372Robert Mustacchi
4263ce5372Robert Mustacchistatic void
4273ce5372Robert Mustacchiksensor_dip_unbind_cb(void *arg, dev_info_t *dip)
4283ce5372Robert Mustacchi{
4293ce5372Robert Mustacchi	ksensor_dip_t *k = arg;
4303ce5372Robert Mustacchi	ksensor_t *sensor;
4313ce5372Robert Mustacchi
4323ce5372Robert Mustacchi	/*
4333ce5372Robert Mustacchi	 * Remove the dip and the associated sensors from global visibility.
4343ce5372Robert Mustacchi	 * This will ensure that no new clients can find this; however, others
4353ce5372Robert Mustacchi	 * may have extent attempts to grab it (but lost the race in an NDI
4363ce5372Robert Mustacchi	 * hold).
4373ce5372Robert Mustacchi	 */
4383ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
4393ce5372Robert Mustacchi	list_remove(&ksensor_dips, k);
4403ce5372Robert Mustacchi	k->ksdip_flags |= KSENSOR_DIP_F_REMOVED;
4413ce5372Robert Mustacchi	for (sensor = list_head(&k->ksdip_sensors); sensor != NULL;
4423ce5372Robert Mustacchi	    sensor = list_next(&k->ksdip_sensors, sensor)) {
4433ce5372Robert Mustacchi		avl_remove(&ksensor_avl, sensor);
4443ce5372Robert Mustacchi	}
4453ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
4463ce5372Robert Mustacchi
4473ce5372Robert Mustacchi	(void) taskq_dispatch(system_taskq, ksensor_dip_unbind_taskq, k,
4483ce5372Robert Mustacchi	    TQ_SLEEP);
4493ce5372Robert Mustacchi}
4503ce5372Robert Mustacchi
4513ce5372Robert Mustacchistatic ksensor_dip_t *
4523ce5372Robert Mustacchiksensor_dip_create(dev_info_t *dip)
4533ce5372Robert Mustacchi{
4543ce5372Robert Mustacchi	ksensor_dip_t *k;
4553ce5372Robert Mustacchi
4563ce5372Robert Mustacchi	k = kmem_zalloc(sizeof (ksensor_dip_t), KM_SLEEP);
4573ce5372Robert Mustacchi	k->ksdip_dip = dip;
4583ce5372Robert Mustacchi	k->ksdip_cb.ddiub_cb = ksensor_dip_unbind_cb;
4593ce5372Robert Mustacchi	k->ksdip_cb.ddiub_arg = k;
4603ce5372Robert Mustacchi	list_create(&k->ksdip_sensors, sizeof (ksensor_t),
4613ce5372Robert Mustacchi	    offsetof(ksensor_t, ksensor_dip_list));
4623ce5372Robert Mustacchi	e_ddi_register_unbind_callback(dip, &k->ksdip_cb);
4633ce5372Robert Mustacchi
4643ce5372Robert Mustacchi	return (k);
4653ce5372Robert Mustacchi}
4663ce5372Robert Mustacchi
4673ce5372Robert Mustacchistatic ksensor_dip_t *
4683ce5372Robert Mustacchiksensor_dip_find(dev_info_t *dip)
4693ce5372Robert Mustacchi{
4703ce5372Robert Mustacchi	ksensor_dip_t *k;
4713ce5372Robert Mustacchi
4723ce5372Robert Mustacchi	ASSERT(MUTEX_HELD(&ksensor_g_mutex));
4733ce5372Robert Mustacchi	for (k = list_head(&ksensor_dips); k != NULL;
4743ce5372Robert Mustacchi	    k = list_next(&ksensor_dips, k)) {
4753ce5372Robert Mustacchi		if (dip == k->ksdip_dip) {
4763ce5372Robert Mustacchi			return (k);
4773ce5372Robert Mustacchi		}
4783ce5372Robert Mustacchi	}
4793ce5372Robert Mustacchi
4803ce5372Robert Mustacchi	return (NULL);
4813ce5372Robert Mustacchi}
4823ce5372Robert Mustacchi
4833ce5372Robert Mustacchiint
4843ce5372Robert Mustacchiksensor_create(dev_info_t *dip, const ksensor_ops_t *ops, void *arg,
4853ce5372Robert Mustacchi    const char *name, const char *class, id_t *idp)
4863ce5372Robert Mustacchi{
4873ce5372Robert Mustacchi	ksensor_dip_t *ksdip;
4883ce5372Robert Mustacchi	ksensor_t *sensor;
4893ce5372Robert Mustacchi
4903ce5372Robert Mustacchi	if (dip == NULL || ops == NULL || name == NULL || class == NULL ||
4913ce5372Robert Mustacchi	    idp == NULL) {
4923ce5372Robert Mustacchi		return (EINVAL);
4933ce5372Robert Mustacchi	}
4943ce5372Robert Mustacchi
4953ce5372Robert Mustacchi	if (!DEVI_IS_ATTACHING(dip)) {
4963ce5372Robert Mustacchi		return (EAGAIN);
4973ce5372Robert Mustacchi	}
4983ce5372Robert Mustacchi
4993ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
5003ce5372Robert Mustacchi	ksdip = ksensor_dip_find(dip);
5013ce5372Robert Mustacchi	if (ksdip == NULL) {
5023ce5372Robert Mustacchi		ksdip = ksensor_dip_create(dip);
5033ce5372Robert Mustacchi		list_insert_tail(&ksensor_dips, ksdip);
5043ce5372Robert Mustacchi	}
5053ce5372Robert Mustacchi
5063ce5372Robert Mustacchi	sensor = ksensor_search_ksdip(ksdip, name, class);
5073ce5372Robert Mustacchi	if (sensor != NULL) {
5083ce5372Robert Mustacchi		ASSERT3P(sensor->ksensor_ksdip, ==, ksdip);
5093ce5372Robert Mustacchi		if ((sensor->ksensor_flags & KSENSOR_F_VALID) != 0) {
5103ce5372Robert Mustacchi			mutex_exit(&ksensor_g_mutex);
5113ce5372Robert Mustacchi			dev_err(dip, CE_WARN, "tried to create sensor %s:%s "
5123ce5372Robert Mustacchi			    "which is currently active", class, name);
5133ce5372Robert Mustacchi			return (EEXIST);
5143ce5372Robert Mustacchi		}
5153ce5372Robert Mustacchi
5163ce5372Robert Mustacchi		sensor->ksensor_ops = ops;
5173ce5372Robert Mustacchi		sensor->ksensor_arg = arg;
5183ce5372Robert Mustacchi	} else {
5193ce5372Robert Mustacchi		sensor = kmem_zalloc(sizeof (ksensor_t), KM_SLEEP);
5203ce5372Robert Mustacchi		sensor->ksensor_ksdip = ksdip;
5213ce5372Robert Mustacchi		sensor->ksensor_name = ddi_strdup(name, KM_SLEEP);
5223ce5372Robert Mustacchi		sensor->ksensor_class = ddi_strdup(class, KM_SLEEP);
5233ce5372Robert Mustacchi		sensor->ksensor_id = id_alloc(ksensor_ids);
5243ce5372Robert Mustacchi		sensor->ksensor_ops = ops;
5253ce5372Robert Mustacchi		sensor->ksensor_arg = arg;
5263ce5372Robert Mustacchi		list_insert_tail(&ksdip->ksdip_sensors, sensor);
5273ce5372Robert Mustacchi		avl_add(&ksensor_avl, sensor);
5283ce5372Robert Mustacchi	}
5293ce5372Robert Mustacchi
5303ce5372Robert Mustacchi	sensor->ksensor_flags |= KSENSOR_F_VALID;
5313ce5372Robert Mustacchi
5323ce5372Robert Mustacchi	if (ksensor_cb_create != NULL) {
5333ce5372Robert Mustacchi
5343ce5372Robert Mustacchi		if (ksensor_cb_create(sensor->ksensor_id, sensor->ksensor_class,
5353ce5372Robert Mustacchi		    sensor->ksensor_name) == 0) {
5363ce5372Robert Mustacchi			sensor->ksensor_flags |= KSENSOR_F_NOTIFIED;
5373ce5372Robert Mustacchi		}
5383ce5372Robert Mustacchi	}
5393ce5372Robert Mustacchi
5403ce5372Robert Mustacchi	*idp = sensor->ksensor_id;
5413ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
5423ce5372Robert Mustacchi
5433ce5372Robert Mustacchi	return (0);
5443ce5372Robert Mustacchi}
5453ce5372Robert Mustacchi
5463ce5372Robert Mustacchiint
5471045e13Robert Mustacchiksensor_create_scalar_pcidev(dev_info_t *dip, uint_t kind,
5481045e13Robert Mustacchi    const ksensor_ops_t *ops, void *arg, const char *name, id_t *idp)
5493ce5372Robert Mustacchi{
5503ce5372Robert Mustacchi	char *pci_name, *type;
5511045e13Robert Mustacchi	const char *class;
5523ce5372Robert Mustacchi	int *regs, ret;
5533ce5372Robert Mustacchi	uint_t nregs;
5543ce5372Robert Mustacchi	uint16_t bus, dev;
5553ce5372Robert Mustacchi
5561045e13Robert Mustacchi	switch (kind) {
5571045e13Robert Mustacchi	case SENSOR_KIND_TEMPERATURE:
5581045e13Robert Mustacchi		class = "ddi_sensor:temperature:pci";
5591045e13Robert Mustacchi		break;
5601045e13Robert Mustacchi	case SENSOR_KIND_VOLTAGE:
5611045e13Robert Mustacchi		class = "ddi_sensor:voltage:pci";
5621045e13Robert Mustacchi		break;
5631045e13Robert Mustacchi	case SENSOR_KIND_CURRENT:
5641045e13Robert Mustacchi		class = "ddi_sensor:current:pci";
5651045e13Robert Mustacchi		break;
5661045e13Robert Mustacchi	default:
5671045e13Robert Mustacchi		return (ENOTSUP);
5681045e13Robert Mustacchi	}
5691045e13Robert Mustacchi
5703ce5372Robert Mustacchi	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, "device_type",
5713ce5372Robert Mustacchi	    &type) != DDI_PROP_SUCCESS) {
5723ce5372Robert Mustacchi		return (EINVAL);
5733ce5372Robert Mustacchi	}
5743ce5372Robert Mustacchi
5753ce5372Robert Mustacchi	if (strcmp(type, "pciex") != 0 && strcmp(type, "pci") != 0) {
5763ce5372Robert Mustacchi		ddi_prop_free(type);
5773ce5372Robert Mustacchi		return (EINVAL);
5783ce5372Robert Mustacchi	}
5793ce5372Robert Mustacchi	ddi_prop_free(type);
5803ce5372Robert Mustacchi
5813ce5372Robert Mustacchi	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 0, "reg",
5823ce5372Robert Mustacchi	    &regs, &nregs) != DDI_PROP_SUCCESS) {
5833ce5372Robert Mustacchi		return (EINVAL);
5843ce5372Robert Mustacchi	}
5853ce5372Robert Mustacchi
5863ce5372Robert Mustacchi	if (nregs < 1) {
5873ce5372Robert Mustacchi		ddi_prop_free(regs);
5883ce5372Robert Mustacchi		return (EIO);
5893ce5372Robert Mustacchi	}
5903ce5372Robert Mustacchi
5913ce5372Robert Mustacchi	bus = PCI_REG_BUS_G(regs[0]);
5923ce5372Robert Mustacchi	dev = PCI_REG_DEV_G(regs[0]);
5933ce5372Robert Mustacchi	ddi_prop_free(regs);
5943ce5372Robert Mustacchi
5953ce5372Robert Mustacchi	pci_name = kmem_asprintf("%x.%x:%s", bus, dev, name);
5963ce5372Robert Mustacchi
5971045e13Robert Mustacchi	ret = ksensor_create(dip, ops, arg, pci_name, class, idp);
5983ce5372Robert Mustacchi	strfree(pci_name);
5993ce5372Robert Mustacchi	return (ret);
6003ce5372Robert Mustacchi}
6013ce5372Robert Mustacchi
6023ce5372Robert Mustacchi/*
6033ce5372Robert Mustacchi * When a driver removes a sensor, we basically mark it as invalid. This happens
6043ce5372Robert Mustacchi * because drivers can detach and we will need to reattach them when the sensor
6053ce5372Robert Mustacchi * is used again.
6063ce5372Robert Mustacchi */
6073ce5372Robert Mustacchiint
6083ce5372Robert Mustacchiksensor_remove(dev_info_t *dip, id_t id)
6093ce5372Robert Mustacchi{
6103ce5372Robert Mustacchi	ksensor_dip_t *kdip;
6113ce5372Robert Mustacchi	ksensor_t *sensor;
6123ce5372Robert Mustacchi
6133ce5372Robert Mustacchi	if (!DEVI_IS_ATTACHING(dip) && !DEVI_IS_DETACHING(dip)) {
6143ce5372Robert Mustacchi		return (EAGAIN);
6153ce5372Robert Mustacchi	}
6163ce5372Robert Mustacchi
6173ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
6183ce5372Robert Mustacchi	kdip = ksensor_dip_find(dip);
6193ce5372Robert Mustacchi	if (kdip == NULL) {
6203ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
6213ce5372Robert Mustacchi		return (ENOENT);
6223ce5372Robert Mustacchi	}
6233ce5372Robert Mustacchi
6243ce5372Robert Mustacchi	for (sensor = list_head(&kdip->ksdip_sensors); sensor != NULL;
6253ce5372Robert Mustacchi	    sensor = list_next(&kdip->ksdip_sensors, sensor)) {
6263ce5372Robert Mustacchi		if (sensor->ksensor_id == id || id == KSENSOR_ALL_IDS) {
6273ce5372Robert Mustacchi			mutex_enter(&sensor->ksensor_mutex);
6283ce5372Robert Mustacchi			sensor->ksensor_flags &= ~KSENSOR_F_VALID;
6293ce5372Robert Mustacchi			sensor->ksensor_ops = NULL;
6303ce5372Robert Mustacchi			sensor->ksensor_arg = NULL;
6313ce5372Robert Mustacchi			mutex_exit(&sensor->ksensor_mutex);
6323ce5372Robert Mustacchi		}
6333ce5372Robert Mustacchi	}
6343ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
6353ce5372Robert Mustacchi	return (0);
6363ce5372Robert Mustacchi}
6373ce5372Robert Mustacchi
6383ce5372Robert Mustacchistatic void
6393ce5372Robert Mustacchiksensor_release(ksensor_t *sensor)
6403ce5372Robert Mustacchi{
6413ce5372Robert Mustacchi	int circ;
6423ce5372Robert Mustacchi	dev_info_t *pdip;
6433ce5372Robert Mustacchi
6443ce5372Robert Mustacchi	ddi_release_devi(sensor->ksensor_ksdip->ksdip_dip);
6453ce5372Robert Mustacchi
6463ce5372Robert Mustacchi	mutex_enter(&sensor->ksensor_mutex);
6473ce5372Robert Mustacchi	sensor->ksensor_flags &= ~KSENSOR_F_BUSY;
6483ce5372Robert Mustacchi	cv_broadcast(&sensor->ksensor_cv);
6493ce5372Robert Mustacchi	mutex_exit(&sensor->ksensor_mutex);
6503ce5372Robert Mustacchi}
6513ce5372Robert Mustacchi
6523ce5372Robert Mustacchistatic int
6533ce5372Robert Mustacchiksensor_hold_by_id(id_t id, ksensor_t **outp)
6543ce5372Robert Mustacchi{
6553ce5372Robert Mustacchi	int circ;
6563ce5372Robert Mustacchi	ksensor_t *sensor;
6573ce5372Robert Mustacchi	dev_info_t *pdip;
6583ce5372Robert Mustacchi
6593ce5372Robert Mustacchirestart:
6603ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
6613ce5372Robert Mustacchi	sensor = ksensor_find_by_id(id);
6623ce5372Robert Mustacchi	if (sensor == NULL) {
6633ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
6643ce5372Robert Mustacchi		*outp = NULL;
6653ce5372Robert Mustacchi		return (ESTALE);
6663ce5372Robert Mustacchi	}
6673ce5372Robert Mustacchi
6683ce5372Robert Mustacchi	if ((sensor->ksensor_ksdip->ksdip_flags & KSENSOR_DIP_F_REMOVED) != 0) {
6693ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
6703ce5372Robert Mustacchi		*outp = NULL;
6713ce5372Robert Mustacchi		return (ESTALE);
6723ce5372Robert Mustacchi	}
6733ce5372Robert Mustacchi
6743ce5372Robert Mustacchi	mutex_enter(&sensor->ksensor_mutex);
6753ce5372Robert Mustacchi	if ((sensor->ksensor_flags & KSENSOR_F_BUSY) != 0) {
6763ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
6773ce5372Robert Mustacchi		sensor->ksensor_nwaiters++;
6783ce5372Robert Mustacchi		while ((sensor->ksensor_flags & KSENSOR_F_BUSY) != 0) {
6793ce5372Robert Mustacchi			int cv = cv_wait_sig(&sensor->ksensor_cv,
6803ce5372Robert Mustacchi			    &sensor->ksensor_mutex);
6813ce5372Robert Mustacchi			if (cv == 0) {
6823ce5372Robert Mustacchi				sensor->ksensor_nwaiters--;
6833ce5372Robert Mustacchi				cv_broadcast(&sensor->ksensor_cv);
6843ce5372Robert Mustacchi				mutex_exit(&sensor->ksensor_mutex);
6853ce5372Robert Mustacchi				*outp = NULL;
6863ce5372Robert Mustacchi				return (EINTR);
6873ce5372Robert Mustacchi			}
6883ce5372Robert Mustacchi		}
6893ce5372Robert Mustacchi		sensor->ksensor_nwaiters--;
6903ce5372Robert Mustacchi		cv_broadcast(&sensor->ksensor_cv);
6913ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
6923ce5372Robert Mustacchi		goto restart;
6933ce5372Robert Mustacchi	}
6943ce5372Robert Mustacchi
6953ce5372Robert Mustacchi	/*
6963ce5372Robert Mustacchi	 * We have obtained ownership of the sensor. At this point, we should
6973ce5372Robert Mustacchi	 * check to see if it's valid or not.
6983ce5372Robert Mustacchi	 */
6993ce5372Robert Mustacchi	sensor->ksensor_flags |= KSENSOR_F_BUSY;
7003ce5372Robert Mustacchi	pdip = ddi_get_parent(sensor->ksensor_ksdip->ksdip_dip);
7013ce5372Robert Mustacchi	mutex_exit(&sensor->ksensor_mutex);
7023ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
7033ce5372Robert Mustacchi
7043ce5372Robert Mustacchi	/*
7053ce5372Robert Mustacchi	 * Grab a reference on the device node to ensure that it won't go away.
7063ce5372Robert Mustacchi	 */
7073ce5372Robert Mustacchi	ndi_devi_enter(pdip, &circ);
7083ce5372Robert Mustacchi	e_ddi_hold_devi(sensor->ksensor_ksdip->ksdip_dip);
7093ce5372Robert Mustacchi	ndi_devi_exit(pdip, circ);
7103ce5372Robert Mustacchi
7113ce5372Robert Mustacchi	/*
7123ce5372Robert Mustacchi	 * Now that we have an NDI hold, check if it's valid or not. It may have
7133ce5372Robert Mustacchi	 * become invalid while we were waiting due to a race.
7143ce5372Robert Mustacchi	 */
7153ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
7163ce5372Robert Mustacchi	if ((sensor->ksensor_ksdip->ksdip_flags & KSENSOR_DIP_F_REMOVED) != 0) {
7173ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
7183ce5372Robert Mustacchi		ksensor_release(sensor);
7193ce5372Robert Mustacchi		return (ESTALE);
7203ce5372Robert Mustacchi	}
7213ce5372Robert Mustacchi
7223ce5372Robert Mustacchi	mutex_enter(&sensor->ksensor_mutex);
7233ce5372Robert Mustacchi	if ((sensor->ksensor_flags & KSENSOR_F_VALID) == 0) {
7243ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
7253ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
7263ce5372Robert Mustacchi		(void) ndi_devi_config(pdip, NDI_NO_EVENT);
7273ce5372Robert Mustacchi		mutex_enter(&ksensor_g_mutex);
7283ce5372Robert Mustacchi		mutex_enter(&sensor->ksensor_mutex);
7293ce5372Robert Mustacchi
7303ce5372Robert Mustacchi		/*
7313ce5372Robert Mustacchi		 * If we attempted to reattach it and it isn't now valid, fail
7323ce5372Robert Mustacchi		 * this request.
7333ce5372Robert Mustacchi		 */
7343ce5372Robert Mustacchi		if ((sensor->ksensor_ksdip->ksdip_flags &
7353ce5372Robert Mustacchi		    KSENSOR_DIP_F_REMOVED) != 0 ||
7363ce5372Robert Mustacchi		    (sensor->ksensor_flags & KSENSOR_F_VALID) == 0) {
7373ce5372Robert Mustacchi			mutex_exit(&sensor->ksensor_mutex);
7383ce5372Robert Mustacchi			mutex_exit(&ksensor_g_mutex);
7393ce5372Robert Mustacchi			ksensor_release(sensor);
7403ce5372Robert Mustacchi			return (ESTALE);
7413ce5372Robert Mustacchi		}
7423ce5372Robert Mustacchi	}
7433ce5372Robert Mustacchi	mutex_exit(&sensor->ksensor_mutex);
7443ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
7453ce5372Robert Mustacchi	*outp = sensor;
7463ce5372Robert Mustacchi
7473ce5372Robert Mustacchi	return (0);
7483ce5372Robert Mustacchi}
7493ce5372Robert Mustacchi
7503ce5372Robert Mustacchiint
7513ce5372Robert Mustacchiksensor_op_kind(id_t id, sensor_ioctl_kind_t *kind)
7523ce5372Robert Mustacchi{
7533ce5372Robert Mustacchi	int ret;
7543ce5372Robert Mustacchi	ksensor_t *sensor;
7553ce5372Robert Mustacchi
7563ce5372Robert Mustacchi	if ((ret = ksensor_hold_by_id(id, &sensor)) != 0) {
7573ce5372Robert Mustacchi		return (ret);
7583ce5372Robert Mustacchi	}
7593ce5372Robert Mustacchi
7603ce5372Robert Mustacchi	ret = sensor->ksensor_ops->kso_kind(sensor->ksensor_arg, kind);
7613ce5372Robert Mustacchi	ksensor_release(sensor);
7623ce5372Robert Mustacchi
7633ce5372Robert Mustacchi	return (ret);
7643ce5372Robert Mustacchi}
7653ce5372Robert Mustacchi
7663ce5372Robert Mustacchiint
7671045e13Robert Mustacchiksensor_op_scalar(id_t id, sensor_ioctl_scalar_t *scalar)
7683ce5372Robert Mustacchi{
7693ce5372Robert Mustacchi	int ret;
7703ce5372Robert Mustacchi	ksensor_t *sensor;
7713ce5372Robert Mustacchi
7723ce5372Robert Mustacchi	if ((ret = ksensor_hold_by_id(id, &sensor)) != 0) {
7733ce5372Robert Mustacchi		return (ret);
7743ce5372Robert Mustacchi	}
7753ce5372Robert Mustacchi
7761045e13Robert Mustacchi	ret = sensor->ksensor_ops->kso_scalar(sensor->ksensor_arg, scalar);
7773ce5372Robert Mustacchi	ksensor_release(sensor);
7783ce5372Robert Mustacchi
7793ce5372Robert Mustacchi	return (ret);
7803ce5372Robert Mustacchi}
7813ce5372Robert Mustacchi
7823ce5372Robert Mustacchivoid
7833ce5372Robert Mustacchiksensor_unregister(dev_info_t *reg_dip)
7843ce5372Robert Mustacchi{
7853ce5372Robert Mustacchi	ksensor_t *sensor;
7863ce5372Robert Mustacchi
7873ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
7883ce5372Robert Mustacchi	if (ksensor_cb_dip != reg_dip) {
7893ce5372Robert Mustacchi		dev_err(reg_dip, CE_PANIC, "asked to unregister illegal dip");
7903ce5372Robert Mustacchi	}
7913ce5372Robert Mustacchi
7923ce5372Robert Mustacchi	for (sensor = avl_first(&ksensor_avl); sensor != NULL; sensor =
7933ce5372Robert Mustacchi	    AVL_NEXT(&ksensor_avl, sensor)) {
7943ce5372Robert Mustacchi		mutex_enter(&sensor->ksensor_mutex);
7953ce5372Robert Mustacchi		sensor->ksensor_flags &= ~KSENSOR_F_NOTIFIED;
7963ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
7973ce5372Robert Mustacchi	}
7983ce5372Robert Mustacchi
7993ce5372Robert Mustacchi	ksensor_cb_dip = NULL;
8003ce5372Robert Mustacchi	ksensor_cb_create = NULL;
8013ce5372Robert Mustacchi	ksensor_cb_remove = NULL;
8023ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
8033ce5372Robert Mustacchi}
8043ce5372Robert Mustacchi
8053ce5372Robert Mustacchiint
8063ce5372Robert Mustacchiksensor_register(dev_info_t *reg_dip, ksensor_create_f create,
8073ce5372Robert Mustacchi    ksensor_remove_f remove)
8083ce5372Robert Mustacchi{
8093ce5372Robert Mustacchi	ksensor_t *sensor;
8103ce5372Robert Mustacchi
8113ce5372Robert Mustacchi	mutex_enter(&ksensor_g_mutex);
8123ce5372Robert Mustacchi	if (ksensor_cb_dip != NULL) {
8133ce5372Robert Mustacchi		dev_err(reg_dip, CE_WARN, "kernel sensors are already "
8143ce5372Robert Mustacchi		    "registered");
8153ce5372Robert Mustacchi		mutex_exit(&ksensor_g_mutex);
8163ce5372Robert Mustacchi		return (EEXIST);
8173ce5372Robert Mustacchi	}
8183ce5372Robert Mustacchi
8193ce5372Robert Mustacchi	ksensor_cb_dip = reg_dip;
8203ce5372Robert Mustacchi	ksensor_cb_create = create;
8213ce5372Robert Mustacchi	ksensor_cb_remove = remove;
8223ce5372Robert Mustacchi
8233ce5372Robert Mustacchi	for (sensor = avl_first(&ksensor_avl); sensor != NULL; sensor =
8243ce5372Robert Mustacchi	    AVL_NEXT(&ksensor_avl, sensor)) {
8253ce5372Robert Mustacchi		mutex_enter(&sensor->ksensor_mutex);
8263ce5372Robert Mustacchi		ASSERT0(sensor->ksensor_flags & KSENSOR_F_NOTIFIED);
8273ce5372Robert Mustacchi
8283ce5372Robert Mustacchi		if (ksensor_cb_create(sensor->ksensor_id, sensor->ksensor_class,
8293ce5372Robert Mustacchi		    sensor->ksensor_name) == 0) {
8303ce5372Robert Mustacchi			sensor->ksensor_flags |= KSENSOR_F_NOTIFIED;
8313ce5372Robert Mustacchi		}
8323ce5372Robert Mustacchi
8333ce5372Robert Mustacchi		mutex_exit(&sensor->ksensor_mutex);
8343ce5372Robert Mustacchi	}
8353ce5372Robert Mustacchi
8363ce5372Robert Mustacchi	mutex_exit(&ksensor_g_mutex);
8373ce5372Robert Mustacchi
8383ce5372Robert Mustacchi	return (0);
8393ce5372Robert Mustacchi}
8403ce5372Robert Mustacchi
8413ce5372Robert Mustacchiint
8423ce5372Robert Mustacchiksensor_kind_temperature(void *unused, sensor_ioctl_kind_t *k)
8433ce5372Robert Mustacchi{
8443ce5372Robert Mustacchi	k->sik_kind = SENSOR_KIND_TEMPERATURE;
8453ce5372Robert Mustacchi	return (0);
8463ce5372Robert Mustacchi}
8473ce5372Robert Mustacchi
8481045e13Robert Mustacchiint
8491045e13Robert Mustacchiksensor_kind_current(void *unused, sensor_ioctl_kind_t *k)
8501045e13Robert Mustacchi{
8511045e13Robert Mustacchi	k->sik_kind = SENSOR_KIND_CURRENT;
8521045e13Robert Mustacchi	return (0);
8531045e13Robert Mustacchi}
8541045e13Robert Mustacchi
8551045e13Robert Mustacchiint
8561045e13Robert Mustacchiksensor_kind_voltage(void *unused, sensor_ioctl_kind_t *k)
8571045e13Robert Mustacchi{
8581045e13Robert Mustacchi	k->sik_kind = SENSOR_KIND_VOLTAGE;
859