17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*39b361b2SRichard Bean * Common Development and Distribution License (the "License").
6*39b361b2SRichard Bean * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*39b361b2SRichard Bean * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * hot-plug services module
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
347c478bd9Sstevel@tonic-gate #include <sys/disp.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/hotplug/hpcsvc.h>
377c478bd9Sstevel@tonic-gate #include <sys/callb.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * debug macros:
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate #if defined(DEBUG)
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate int hpcsvc_debug = 0;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static void debug(char *, uintptr_t, uintptr_t, uintptr_t,
477c478bd9Sstevel@tonic-gate uintptr_t, uintptr_t);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #define DEBUG0(fmt) \
507c478bd9Sstevel@tonic-gate debug(fmt, 0, 0, 0, 0, 0);
517c478bd9Sstevel@tonic-gate #define DEBUG1(fmt, a1) \
527c478bd9Sstevel@tonic-gate debug(fmt, (uintptr_t)(a1), 0, 0, 0, 0);
537c478bd9Sstevel@tonic-gate #define DEBUG2(fmt, a1, a2) \
547c478bd9Sstevel@tonic-gate debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2), 0, 0, 0);
557c478bd9Sstevel@tonic-gate #define DEBUG3(fmt, a1, a2, a3) \
567c478bd9Sstevel@tonic-gate debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2), (uintptr_t)(a3), 0, 0);
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate #define DEBUG0(fmt)
597c478bd9Sstevel@tonic-gate #define DEBUG1(fmt, a1)
607c478bd9Sstevel@tonic-gate #define DEBUG2(fmt, a1, a2)
617c478bd9Sstevel@tonic-gate #define DEBUG3(fmt, a1, a2, a3)
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate * Definitions for the bus node registration list:
667c478bd9Sstevel@tonic-gate *
677c478bd9Sstevel@tonic-gate * The hot-plug service module maintains a linked list of items
687c478bd9Sstevel@tonic-gate * representing the device bus nodes that have been registered via
697c478bd9Sstevel@tonic-gate * hpc_nexus_register, or identified as candidates for registration
707c478bd9Sstevel@tonic-gate * by the bus argument to hpc_slot_register.
717c478bd9Sstevel@tonic-gate *
727c478bd9Sstevel@tonic-gate * The head of the linked listed is stored in hpc_bus_list_head. Insertions
737c478bd9Sstevel@tonic-gate * and removals from the list should be locked with mutex hpc_bus_mutex.
747c478bd9Sstevel@tonic-gate *
757c478bd9Sstevel@tonic-gate * Items in the list are allocated/freed with the macros hpc_alloc_bus_entry()
767c478bd9Sstevel@tonic-gate * and hpc_free_bus_entry().
777c478bd9Sstevel@tonic-gate *
787c478bd9Sstevel@tonic-gate * Each item in the list contains the following fields:
797c478bd9Sstevel@tonic-gate *
807c478bd9Sstevel@tonic-gate * bus_dip - pointer to devinfo node of the registering bus
817c478bd9Sstevel@tonic-gate *
827c478bd9Sstevel@tonic-gate * bus_name - device path name of the bus (ie /pci@1f,4000)
837c478bd9Sstevel@tonic-gate *
847c478bd9Sstevel@tonic-gate * bus_callback - bus nexus driver callback function registered
857c478bd9Sstevel@tonic-gate * with the bus
867c478bd9Sstevel@tonic-gate *
877c478bd9Sstevel@tonic-gate * bus_registered - a boolean value which is true if the bus has
887c478bd9Sstevel@tonic-gate * been registered with hpc_nexus_register, false otherwise
897c478bd9Sstevel@tonic-gate *
907c478bd9Sstevel@tonic-gate * bus_mutex - mutex lock to be held while updating this list entry
917c478bd9Sstevel@tonic-gate *
927c478bd9Sstevel@tonic-gate * bus_slot_list - linked list of the slots registered for this
937c478bd9Sstevel@tonic-gate * bus node (see slot list details below)
947c478bd9Sstevel@tonic-gate *
957c478bd9Sstevel@tonic-gate * bus_thread - kernel thread for running slot event handlers for
967c478bd9Sstevel@tonic-gate * slots associated with this bus
977c478bd9Sstevel@tonic-gate *
987c478bd9Sstevel@tonic-gate * bus_thread_cv - condition variable for synchronization between
997c478bd9Sstevel@tonic-gate * the service routines and the thread for running slot
1007c478bd9Sstevel@tonic-gate * event handlers
1017c478bd9Sstevel@tonic-gate *
1027c478bd9Sstevel@tonic-gate * bus_thread_exit - a boolean value used to instruct the thread
1037c478bd9Sstevel@tonic-gate * for invoking the slot event handlers to exit
1047c478bd9Sstevel@tonic-gate *
1057c478bd9Sstevel@tonic-gate * bus_slot_event_list_head - the head of the linked list of instances
1067c478bd9Sstevel@tonic-gate * of slot event handlers to be run
1077c478bd9Sstevel@tonic-gate * handlers to be invoked
1087c478bd9Sstevel@tonic-gate *
1097c478bd9Sstevel@tonic-gate * bus_next - pointer to next list entry
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate typedef struct hpc_bus_entry hpc_bus_entry_t;
1137c478bd9Sstevel@tonic-gate typedef struct hpc_slot_entry hpc_slot_entry_t;
1147c478bd9Sstevel@tonic-gate typedef struct hpc_event_entry hpc_event_entry_t;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate struct hpc_event_entry {
1177c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
1187c478bd9Sstevel@tonic-gate int event;
1197c478bd9Sstevel@tonic-gate hpc_event_entry_t *next;
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate #define hpc_alloc_event_entry() \
1237c478bd9Sstevel@tonic-gate (hpc_event_entry_t *)kmem_zalloc(sizeof (hpc_event_entry_t), KM_SLEEP)
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate #define hpc_free_event_entry(a) \
1267c478bd9Sstevel@tonic-gate kmem_free((a), sizeof (hpc_event_entry_t))
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate struct hpc_bus_entry {
1297c478bd9Sstevel@tonic-gate dev_info_t *bus_dip;
1307c478bd9Sstevel@tonic-gate char bus_name[MAXPATHLEN + 1];
1317c478bd9Sstevel@tonic-gate boolean_t bus_registered;
1327c478bd9Sstevel@tonic-gate kmutex_t bus_mutex;
1337c478bd9Sstevel@tonic-gate int (* bus_callback)(dev_info_t *dip, hpc_slot_t hdl,
1347c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info, int slot_state);
1357c478bd9Sstevel@tonic-gate hpc_slot_entry_t *bus_slot_list;
1367c478bd9Sstevel@tonic-gate kthread_t *bus_thread;
1377c478bd9Sstevel@tonic-gate kcondvar_t bus_thread_cv;
1387c478bd9Sstevel@tonic-gate boolean_t bus_thread_exit;
1397c478bd9Sstevel@tonic-gate hpc_event_entry_t *bus_slot_event_list_head;
1407c478bd9Sstevel@tonic-gate hpc_bus_entry_t *bus_next;
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate #define hpc_alloc_bus_entry() \
1447c478bd9Sstevel@tonic-gate (hpc_bus_entry_t *)kmem_zalloc(sizeof (hpc_bus_entry_t), KM_SLEEP)
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate #define hpc_free_bus_entry(a) \
1477c478bd9Sstevel@tonic-gate kmem_free((a), sizeof (hpc_bus_entry_t))
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * Definitions for the per-bus node slot registration list:
1527c478bd9Sstevel@tonic-gate *
1537c478bd9Sstevel@tonic-gate * For each bus node in the bus list, the hot-plug service module maintains
1547c478bd9Sstevel@tonic-gate * a doubly linked link list of items representing the slots that have been
1557c478bd9Sstevel@tonic-gate * registered (by hot-plug controllers) for that bus.
1567c478bd9Sstevel@tonic-gate *
1577c478bd9Sstevel@tonic-gate * The head of the linked listed is stored in bus_slot_list field of the bus
1587c478bd9Sstevel@tonic-gate * node. Insertions and removals from this list should locked with the mutex
1597c478bd9Sstevel@tonic-gate * in the bus_mutex field of the bus node.
1607c478bd9Sstevel@tonic-gate *
1617c478bd9Sstevel@tonic-gate * Items in the list are allocated/freed with the macros hpc_alloc_slot_entry()
1627c478bd9Sstevel@tonic-gate * and hpc_free_slot_entry().
1637c478bd9Sstevel@tonic-gate *
1647c478bd9Sstevel@tonic-gate * Each item in the list contains the following fields:
1657c478bd9Sstevel@tonic-gate *
1667c478bd9Sstevel@tonic-gate * slot_handle - handle for slot (hpc_slot_t)
1677c478bd9Sstevel@tonic-gate *
1687c478bd9Sstevel@tonic-gate * slot_info - information registered with the slot (hpc_slot_info_t)
1697c478bd9Sstevel@tonic-gate *
1707c478bd9Sstevel@tonic-gate * slot_ops - ops vector registered with the slot (hpc_slot_ops_t)
1717c478bd9Sstevel@tonic-gate *
1727c478bd9Sstevel@tonic-gate * slot_ops_arg - argument to be passed to ops routines (caddr_t)
1737c478bd9Sstevel@tonic-gate *
1747c478bd9Sstevel@tonic-gate * slot_event_handler - handler registered for slot events
1757c478bd9Sstevel@tonic-gate *
1767c478bd9Sstevel@tonic-gate * slot_event_handler_arg - argument to be passed to event handler
1777c478bd9Sstevel@tonic-gate *
1787c478bd9Sstevel@tonic-gate * slot_event_mask - the set of events for which the event handler
1797c478bd9Sstevel@tonic-gate * gets invoked
1807c478bd9Sstevel@tonic-gate *
1817c478bd9Sstevel@tonic-gate * slot_bus - pointer to bus node for the slot
1827c478bd9Sstevel@tonic-gate *
1837c478bd9Sstevel@tonic-gate * slot_hpc_dip - devinfo node pointer to the HPC driver instance
1847c478bd9Sstevel@tonic-gate * that controls this slot
1857c478bd9Sstevel@tonic-gate *
1867c478bd9Sstevel@tonic-gate * slot_{prev,next} - point to {previous,next} node in the list
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate struct hpc_slot_entry {
1907c478bd9Sstevel@tonic-gate hpc_slot_t slot_handle;
1917c478bd9Sstevel@tonic-gate hpc_slot_info_t slot_info; /* should be static & copied */
1927c478bd9Sstevel@tonic-gate hpc_slot_ops_t slot_ops;
1937c478bd9Sstevel@tonic-gate caddr_t slot_ops_arg;
1947c478bd9Sstevel@tonic-gate int (* slot_event_handler)(caddr_t, uint_t);
1957c478bd9Sstevel@tonic-gate caddr_t slot_event_handler_arg;
1967c478bd9Sstevel@tonic-gate uint_t slot_event_mask;
1977c478bd9Sstevel@tonic-gate hpc_bus_entry_t *slot_bus;
1987c478bd9Sstevel@tonic-gate dev_info_t *slot_hpc_dip;
1997c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slot_next, *slot_prev;
2007c478bd9Sstevel@tonic-gate };
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate #define hpc_alloc_slot_entry() \
2037c478bd9Sstevel@tonic-gate (hpc_slot_entry_t *)kmem_zalloc(sizeof (hpc_slot_entry_t), KM_SLEEP)
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate #define hpc_free_slot_entry(a) \
2067c478bd9Sstevel@tonic-gate kmem_free((a), sizeof (hpc_slot_entry_t))
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate * Definitions for slot registration callback table.
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate typedef struct hpc_callback_entry hpc_callback_entry_t;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate struct hpc_callback_entry {
2167c478bd9Sstevel@tonic-gate int (* callback)(dev_info_t *dip, hpc_slot_t hdl,
2177c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info, int slot_state);
2187c478bd9Sstevel@tonic-gate dev_info_t *dip;
2197c478bd9Sstevel@tonic-gate hpc_slot_t hdl;
2207c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info;
2217c478bd9Sstevel@tonic-gate int slot_state;
2227c478bd9Sstevel@tonic-gate hpc_callback_entry_t *next;
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate #define hpc_alloc_callback_entry() \
2267c478bd9Sstevel@tonic-gate (hpc_callback_entry_t *) \
2277c478bd9Sstevel@tonic-gate kmem_zalloc(sizeof (hpc_callback_entry_t), KM_SLEEP)
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate #define hpc_free_callback_entry(a) \
2307c478bd9Sstevel@tonic-gate kmem_free((a), sizeof (hpc_callback_entry_t))
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate * Mutex lock for bus registration table and table head.
2367c478bd9Sstevel@tonic-gate */
2377c478bd9Sstevel@tonic-gate static kmutex_t hpc_bus_mutex;
2387c478bd9Sstevel@tonic-gate static hpc_bus_entry_t *hpc_bus_list_head;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate * Forward function declarations.
2437c478bd9Sstevel@tonic-gate */
2447c478bd9Sstevel@tonic-gate static hpc_bus_entry_t *hpc_find_bus_by_name(char *name);
2457c478bd9Sstevel@tonic-gate static void hpc_slot_event_dispatcher(hpc_bus_entry_t *busp);
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * loadable module definitions:
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
2547c478bd9Sstevel@tonic-gate &mod_miscops, /* Type of module */
255*39b361b2SRichard Bean "hot-plug controller services"
2567c478bd9Sstevel@tonic-gate };
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2597c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlmisc, NULL
2607c478bd9Sstevel@tonic-gate };
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate int
_init(void)2637c478bd9Sstevel@tonic-gate _init(void)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate int e;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate mutex_init(&hpc_bus_mutex, NULL, MUTEX_DRIVER, NULL);
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * Install the module.
2717c478bd9Sstevel@tonic-gate */
2727c478bd9Sstevel@tonic-gate e = mod_install(&modlinkage);
2737c478bd9Sstevel@tonic-gate if (e != 0) {
2747c478bd9Sstevel@tonic-gate mutex_destroy(&hpc_bus_mutex);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate return (e);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate int
_fini(void)2807c478bd9Sstevel@tonic-gate _fini(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate int e;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate e = mod_remove(&modlinkage);
2857c478bd9Sstevel@tonic-gate if (e == 0) {
2867c478bd9Sstevel@tonic-gate mutex_destroy(&hpc_bus_mutex);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate return (e);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2927c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate hpc_slot_ops_t *
hpc_alloc_slot_ops(int flag)3007c478bd9Sstevel@tonic-gate hpc_alloc_slot_ops(int flag)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate hpc_slot_ops_t *ops;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate ops = (hpc_slot_ops_t *)kmem_zalloc(sizeof (hpc_slot_ops_t), flag);
3057c478bd9Sstevel@tonic-gate return (ops);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate void
hpc_free_slot_ops(hpc_slot_ops_t * ops)3107c478bd9Sstevel@tonic-gate hpc_free_slot_ops(hpc_slot_ops_t *ops)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate kmem_free((void *)ops, sizeof (hpc_slot_ops_t));
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
3177c478bd9Sstevel@tonic-gate int
hpc_nexus_register_bus(dev_info_t * dip,int (* callback)(dev_info_t * dip,hpc_slot_t hdl,hpc_slot_info_t * slot_info,int slot_state),uint_t flags)3187c478bd9Sstevel@tonic-gate hpc_nexus_register_bus(dev_info_t *dip,
3197c478bd9Sstevel@tonic-gate int (* callback)(dev_info_t *dip, hpc_slot_t hdl,
3207c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info, int slot_state), uint_t flags)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
3237c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
3247c478bd9Sstevel@tonic-gate char bus_path[MAXPATHLEN + 1];
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate DEBUG2("hpc_nexus_register_bus: %s%d",
3277c478bd9Sstevel@tonic-gate ddi_node_name(dip), ddi_get_instance(dip));
3287c478bd9Sstevel@tonic-gate mutex_enter(&hpc_bus_mutex);
3297c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, bus_path);
3307c478bd9Sstevel@tonic-gate busp = hpc_find_bus_by_name(bus_path);
3317c478bd9Sstevel@tonic-gate if (busp == NULL) {
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * Initialize the new bus node and link it at the head
3357c478bd9Sstevel@tonic-gate * of the bus list.
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate DEBUG0("hpc_nexus_register_bus: not in bus list");
3387c478bd9Sstevel@tonic-gate busp = hpc_alloc_bus_entry();
3397c478bd9Sstevel@tonic-gate busp->bus_dip = dip;
3407c478bd9Sstevel@tonic-gate busp->bus_registered = B_TRUE;
3417c478bd9Sstevel@tonic-gate (void) strcpy(busp->bus_name, bus_path);
3427c478bd9Sstevel@tonic-gate mutex_init(&busp->bus_mutex, NULL, MUTEX_DRIVER, NULL);
3437c478bd9Sstevel@tonic-gate busp->bus_callback = callback;
3447c478bd9Sstevel@tonic-gate busp->bus_slot_list = NULL;
3457c478bd9Sstevel@tonic-gate busp->bus_next = hpc_bus_list_head;
3467c478bd9Sstevel@tonic-gate hpc_bus_list_head = busp;
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate } else {
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate * The bus is in the bus list but isn't registered yet.
3527c478bd9Sstevel@tonic-gate * Mark it as registered, and run the registration callbacks
3537c478bd9Sstevel@tonic-gate * for it slots.
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate DEBUG0("hpc_nexus_register_bus: in list, but not registered");
3567c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
3577c478bd9Sstevel@tonic-gate if (busp->bus_registered == B_TRUE) {
3587c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
3597c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
3607c478bd9Sstevel@tonic-gate return (HPC_ERR_BUS_DUPLICATE);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate busp->bus_dip = dip;
3637c478bd9Sstevel@tonic-gate busp->bus_callback = callback;
3647c478bd9Sstevel@tonic-gate busp->bus_registered = B_TRUE;
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
3677c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
3687c478bd9Sstevel@tonic-gate if (callback) {
3697c478bd9Sstevel@tonic-gate DEBUG0("hpc_nexus_register_bus: running callbacks");
3707c478bd9Sstevel@tonic-gate for (slotp = busp->bus_slot_list; slotp;
3717c478bd9Sstevel@tonic-gate slotp = slotp->slot_next) {
3727c478bd9Sstevel@tonic-gate (void) callback(dip, slotp, &slotp->slot_info,
3737c478bd9Sstevel@tonic-gate HPC_SLOT_ONLINE);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
3797c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate int
hpc_nexus_unregister_bus(dev_info_t * dip)3847c478bd9Sstevel@tonic-gate hpc_nexus_unregister_bus(dev_info_t *dip)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp, *busp_prev;
3877c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * Search the list for the bus node and remove it.
3917c478bd9Sstevel@tonic-gate */
3927c478bd9Sstevel@tonic-gate DEBUG2("hpc_nexus_unregister_bus: %s%d",
3937c478bd9Sstevel@tonic-gate ddi_node_name(dip), ddi_get_instance(dip));
3947c478bd9Sstevel@tonic-gate mutex_enter(&hpc_bus_mutex);
3957c478bd9Sstevel@tonic-gate for (busp = hpc_bus_list_head; busp != NULL; busp_prev = busp,
3967c478bd9Sstevel@tonic-gate busp = busp->bus_next) {
3977c478bd9Sstevel@tonic-gate if (busp->bus_dip == dip)
3987c478bd9Sstevel@tonic-gate break;
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate if (busp == NULL) {
4017c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
4027c478bd9Sstevel@tonic-gate return (HPC_ERR_BUS_NOTREGISTERED);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate * If the bus has slots, mark the bus as unregistered, otherwise
4077c478bd9Sstevel@tonic-gate * remove the bus entry from the list.
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
4107c478bd9Sstevel@tonic-gate if (busp->bus_slot_list == NULL) {
4117c478bd9Sstevel@tonic-gate if (busp == hpc_bus_list_head)
4127c478bd9Sstevel@tonic-gate hpc_bus_list_head = busp->bus_next;
4137c478bd9Sstevel@tonic-gate else
4147c478bd9Sstevel@tonic-gate busp_prev->bus_next = busp->bus_next;
4157c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
4167c478bd9Sstevel@tonic-gate mutex_destroy(&busp->bus_mutex);
4177c478bd9Sstevel@tonic-gate hpc_free_bus_entry(busp);
4187c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
4197c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * unregister event handlers for all the slots on this bus.
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate for (slotp = busp->bus_slot_list; slotp != NULL;
4267c478bd9Sstevel@tonic-gate slotp = slotp->slot_next) {
4277c478bd9Sstevel@tonic-gate slotp->slot_event_handler = NULL;
4287c478bd9Sstevel@tonic-gate slotp->slot_event_handler_arg = NULL;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate busp->bus_registered = B_FALSE;
4317c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
4327c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
4337c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate /*ARGSUSED5*/
4387c478bd9Sstevel@tonic-gate int
hpc_slot_register(dev_info_t * hpc_dip,char * bus,hpc_slot_info_t * infop,hpc_slot_t * handlep,hpc_slot_ops_t * opsp,caddr_t ops_arg,uint_t flags)4397c478bd9Sstevel@tonic-gate hpc_slot_register(dev_info_t *hpc_dip, char *bus, hpc_slot_info_t *infop,
4407c478bd9Sstevel@tonic-gate hpc_slot_t *handlep, hpc_slot_ops_t *opsp,
4417c478bd9Sstevel@tonic-gate caddr_t ops_arg, uint_t flags)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
4447c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp, *slot_list_head;
4457c478bd9Sstevel@tonic-gate boolean_t run_callback = B_FALSE;
4467c478bd9Sstevel@tonic-gate int (* callback)(dev_info_t *dip, hpc_slot_t hdl,
4477c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info, int slot_state);
4487c478bd9Sstevel@tonic-gate dev_info_t *dip;
4497c478bd9Sstevel@tonic-gate kthread_t *t;
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate * Validate the arguments.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate DEBUG1("hpc_slot_register: %s", bus);
4557c478bd9Sstevel@tonic-gate if (handlep == NULL || infop == NULL || opsp == NULL || hpc_dip == NULL)
4567c478bd9Sstevel@tonic-gate return (HPC_ERR_INVALID);
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * The bus for the slot may or may not be in the bus list. If it's
4607c478bd9Sstevel@tonic-gate * not, we create a node for the bus in the bus list and mark it as
4617c478bd9Sstevel@tonic-gate * not registered.
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate mutex_enter(&hpc_bus_mutex);
4647c478bd9Sstevel@tonic-gate busp = hpc_find_bus_by_name(bus);
4657c478bd9Sstevel@tonic-gate if (busp == NULL) {
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate * Initialize the new bus node and link it at the
4697c478bd9Sstevel@tonic-gate * head of the bus list.
4707c478bd9Sstevel@tonic-gate */
4717c478bd9Sstevel@tonic-gate DEBUG1("hpc_slot_register: %s not in bus list", bus);
4727c478bd9Sstevel@tonic-gate busp = hpc_alloc_bus_entry();
4737c478bd9Sstevel@tonic-gate busp->bus_registered = B_FALSE;
4747c478bd9Sstevel@tonic-gate (void) strcpy(busp->bus_name, bus);
4757c478bd9Sstevel@tonic-gate mutex_init(&busp->bus_mutex, NULL, MUTEX_DRIVER, NULL);
4767c478bd9Sstevel@tonic-gate busp->bus_slot_list = NULL;
4777c478bd9Sstevel@tonic-gate busp->bus_next = hpc_bus_list_head;
4787c478bd9Sstevel@tonic-gate hpc_bus_list_head = busp;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate } else {
4817c478bd9Sstevel@tonic-gate if (busp->bus_registered == B_TRUE) {
4827c478bd9Sstevel@tonic-gate run_callback = B_TRUE;
4837c478bd9Sstevel@tonic-gate callback = busp->bus_callback;
4847c478bd9Sstevel@tonic-gate dip = busp->bus_dip;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
4897c478bd9Sstevel@tonic-gate slot_list_head = busp->bus_slot_list;
4907c478bd9Sstevel@tonic-gate if (slot_list_head == NULL) {
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate * The slot list was empty, so this is the first slot
4947c478bd9Sstevel@tonic-gate * registered for the bus. Create a per-bus thread
4957c478bd9Sstevel@tonic-gate * for running the slot event handlers.
4967c478bd9Sstevel@tonic-gate */
4977c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_register: creating event callback thread");
4987c478bd9Sstevel@tonic-gate cv_init(&busp->bus_thread_cv, NULL, CV_DRIVER, NULL);
4997c478bd9Sstevel@tonic-gate busp->bus_thread_exit = B_FALSE;
5007c478bd9Sstevel@tonic-gate t = thread_create(NULL, 0, hpc_slot_event_dispatcher,
5017c478bd9Sstevel@tonic-gate (caddr_t)busp, 0, &p0, TS_RUN, minclsyspri);
5027c478bd9Sstevel@tonic-gate busp->bus_thread = t;
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate * Create and initialize a new entry in the slot list for the bus.
5077c478bd9Sstevel@tonic-gate */
5087c478bd9Sstevel@tonic-gate slotp = hpc_alloc_slot_entry();
5097c478bd9Sstevel@tonic-gate slotp->slot_handle = (hpc_slot_t)slotp;
5107c478bd9Sstevel@tonic-gate slotp->slot_info = *infop;
5117c478bd9Sstevel@tonic-gate slotp->slot_ops = *opsp;
5127c478bd9Sstevel@tonic-gate slotp->slot_ops_arg = ops_arg;
5137c478bd9Sstevel@tonic-gate slotp->slot_bus = busp;
5147c478bd9Sstevel@tonic-gate slotp->slot_hpc_dip = hpc_dip;
5157c478bd9Sstevel@tonic-gate slotp->slot_prev = NULL;
5167c478bd9Sstevel@tonic-gate busp->bus_slot_list = slotp;
5177c478bd9Sstevel@tonic-gate slotp->slot_next = slot_list_head;
5187c478bd9Sstevel@tonic-gate if (slot_list_head != NULL)
5197c478bd9Sstevel@tonic-gate slot_list_head->slot_prev = slotp;
5207c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
5217c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * Return the handle to the caller prior to return to avoid recursion.
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate *handlep = (hpc_slot_t)slotp;
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate * If the bus was registered, we run the callback registered by
5307c478bd9Sstevel@tonic-gate * the bus node.
5317c478bd9Sstevel@tonic-gate */
5327c478bd9Sstevel@tonic-gate if (run_callback) {
5337c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_register: running callback");
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate (void) callback(dip, slotp, infop, HPC_SLOT_ONLINE);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * Keep hpc driver in memory
5407c478bd9Sstevel@tonic-gate */
5417c478bd9Sstevel@tonic-gate (void) ndi_hold_driver(hpc_dip);
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate int
hpc_slot_unregister(hpc_slot_t * handlep)5487c478bd9Sstevel@tonic-gate hpc_slot_unregister(hpc_slot_t *handlep)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
5517c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp, *busp_prev;
5527c478bd9Sstevel@tonic-gate boolean_t run_callback;
5537c478bd9Sstevel@tonic-gate int (* callback)(dev_info_t *dip, hpc_slot_t hdl,
5547c478bd9Sstevel@tonic-gate hpc_slot_info_t *slot_info, int slot_state);
5557c478bd9Sstevel@tonic-gate int r;
5567c478bd9Sstevel@tonic-gate dev_info_t *dip;
5577c478bd9Sstevel@tonic-gate char *bus_name;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_unregister:");
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate ASSERT(handlep != NULL);
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate /* validate the handle */
5647c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)*handlep;
5657c478bd9Sstevel@tonic-gate if ((slotp == NULL) || slotp->slot_handle != *handlep)
5667c478bd9Sstevel@tonic-gate return (HPC_ERR_INVALID);
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate * Get the bus list entry from the slot to grap the mutex for
5707c478bd9Sstevel@tonic-gate * the slot list of the bus.
5717c478bd9Sstevel@tonic-gate */
5727c478bd9Sstevel@tonic-gate mutex_enter(&hpc_bus_mutex);
5737c478bd9Sstevel@tonic-gate busp = slotp->slot_bus;
5747c478bd9Sstevel@tonic-gate DEBUG2("hpc_slot_unregister: handlep=%x, slotp=%x", handlep, slotp);
5757c478bd9Sstevel@tonic-gate if (busp == NULL) {
5767c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
5777c478bd9Sstevel@tonic-gate return (HPC_ERR_SLOT_NOTREGISTERED);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Determine if we need to run the slot offline callback and
5827c478bd9Sstevel@tonic-gate * save the data necessary to do so.
5837c478bd9Sstevel@tonic-gate */
5847c478bd9Sstevel@tonic-gate callback = busp->bus_callback;
5857c478bd9Sstevel@tonic-gate run_callback = (busp->bus_registered == B_TRUE) && (callback != NULL);
5867c478bd9Sstevel@tonic-gate dip = busp->bus_dip;
5877c478bd9Sstevel@tonic-gate bus_name = busp->bus_name;
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate * Run the slot offline callback if necessary.
5917c478bd9Sstevel@tonic-gate */
5927c478bd9Sstevel@tonic-gate if (run_callback) {
5937c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
5947c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_unregister: running callback");
5957c478bd9Sstevel@tonic-gate r = callback(dip, (hpc_slot_t)slotp, &slotp->slot_info,
5967c478bd9Sstevel@tonic-gate HPC_SLOT_OFFLINE);
5977c478bd9Sstevel@tonic-gate DEBUG1("hpc_slot_unregister: callback returned %x", r);
5987c478bd9Sstevel@tonic-gate if (r != HPC_SUCCESS)
5997c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
6007c478bd9Sstevel@tonic-gate mutex_enter(&hpc_bus_mutex);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate * Remove the slot from list and free the memory associated with it.
6057c478bd9Sstevel@tonic-gate */
6067c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
6077c478bd9Sstevel@tonic-gate DEBUG1("hpc_slot_unregister: freeing slot, bus_slot_list=%x",
6087c478bd9Sstevel@tonic-gate busp->bus_slot_list);
6097c478bd9Sstevel@tonic-gate if (slotp->slot_prev != NULL)
6107c478bd9Sstevel@tonic-gate slotp->slot_prev->slot_next = slotp->slot_next;
6117c478bd9Sstevel@tonic-gate if (slotp->slot_next != NULL)
6127c478bd9Sstevel@tonic-gate slotp->slot_next->slot_prev = slotp->slot_prev;
6137c478bd9Sstevel@tonic-gate if (slotp == busp->bus_slot_list)
6147c478bd9Sstevel@tonic-gate busp->bus_slot_list = slotp->slot_next;
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate * Release hold from slot registration
6187c478bd9Sstevel@tonic-gate */
6197c478bd9Sstevel@tonic-gate ndi_rele_driver(slotp->slot_hpc_dip);
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate /* Free the memory associated with the slot entry structure */
6227c478bd9Sstevel@tonic-gate hpc_free_slot_entry(slotp);
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate * If the slot list is empty then stop the event handler thread.
6267c478bd9Sstevel@tonic-gate */
6277c478bd9Sstevel@tonic-gate if (busp->bus_slot_list == NULL) {
6287c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_unregister: stopping thread");
6297c478bd9Sstevel@tonic-gate busp->bus_thread_exit = B_TRUE;
6307c478bd9Sstevel@tonic-gate cv_signal(&busp->bus_thread_cv);
6317c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_unregister: waiting for thread to exit");
6327c478bd9Sstevel@tonic-gate cv_wait(&busp->bus_thread_cv, &busp->bus_mutex);
6337c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_unregister: thread exit");
6347c478bd9Sstevel@tonic-gate cv_destroy(&busp->bus_thread_cv);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * If the bus is unregistered and this is the last slot for this bus
6397c478bd9Sstevel@tonic-gate * then remove the entry from the bus list.
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate if (busp->bus_registered == B_FALSE && busp->bus_slot_list == NULL) {
6427c478bd9Sstevel@tonic-gate /* locate the previous entry in the bus list */
6437c478bd9Sstevel@tonic-gate for (busp = hpc_bus_list_head; busp != NULL; busp_prev = busp,
6447c478bd9Sstevel@tonic-gate busp = busp->bus_next)
6457c478bd9Sstevel@tonic-gate if (strcmp(bus_name, busp->bus_name) == 0)
6467c478bd9Sstevel@tonic-gate break;
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate if (busp == hpc_bus_list_head)
6497c478bd9Sstevel@tonic-gate hpc_bus_list_head = busp->bus_next;
6507c478bd9Sstevel@tonic-gate else
6517c478bd9Sstevel@tonic-gate busp_prev->bus_next = busp->bus_next;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
6547c478bd9Sstevel@tonic-gate mutex_destroy(&busp->bus_mutex);
6557c478bd9Sstevel@tonic-gate hpc_free_bus_entry(busp);
6567c478bd9Sstevel@tonic-gate } else
6577c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
6587c478bd9Sstevel@tonic-gate mutex_exit(&hpc_bus_mutex);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate * reset the slot handle.
6627c478bd9Sstevel@tonic-gate */
6637c478bd9Sstevel@tonic-gate *handlep = NULL;
6647c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate int
hpc_install_event_handler(hpc_slot_t handle,uint_t event_mask,int (* event_handler)(caddr_t,uint_t),caddr_t arg)6697c478bd9Sstevel@tonic-gate hpc_install_event_handler(hpc_slot_t handle, uint_t event_mask,
6707c478bd9Sstevel@tonic-gate int (*event_handler)(caddr_t, uint_t), caddr_t arg)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
6737c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate DEBUG3("hpc_install_event_handler: handle=%x, mask=%x, arg=%x",
6767c478bd9Sstevel@tonic-gate handle, event_mask, arg);
6777c478bd9Sstevel@tonic-gate ASSERT((handle != NULL) && (event_handler != NULL));
6787c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
6797c478bd9Sstevel@tonic-gate busp = slotp->slot_bus;
6807c478bd9Sstevel@tonic-gate ASSERT(slotp == slotp->slot_handle);
6817c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
6827c478bd9Sstevel@tonic-gate slotp->slot_event_mask = event_mask;
6837c478bd9Sstevel@tonic-gate slotp->slot_event_handler = event_handler;
6847c478bd9Sstevel@tonic-gate slotp->slot_event_handler_arg = arg;
6857c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
6867c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate int
hpc_remove_event_handler(hpc_slot_t handle)6917c478bd9Sstevel@tonic-gate hpc_remove_event_handler(hpc_slot_t handle)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
6947c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate DEBUG1("hpc_remove_event_handler: handle=%x", handle);
6977c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
6987c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
6997c478bd9Sstevel@tonic-gate ASSERT(slotp == slotp->slot_handle);
7007c478bd9Sstevel@tonic-gate busp = slotp->slot_bus;
7017c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
7027c478bd9Sstevel@tonic-gate slotp->slot_event_mask = 0;
7037c478bd9Sstevel@tonic-gate slotp->slot_event_handler = NULL;
7047c478bd9Sstevel@tonic-gate slotp->slot_event_handler_arg = NULL;
7057c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
7067c478bd9Sstevel@tonic-gate return (HPC_SUCCESS);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7117c478bd9Sstevel@tonic-gate int
hpc_slot_event_notify(hpc_slot_t handle,uint_t event,uint_t flags)7127c478bd9Sstevel@tonic-gate hpc_slot_event_notify(hpc_slot_t handle, uint_t event, uint_t flags)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
7157c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
7167c478bd9Sstevel@tonic-gate hpc_event_entry_t *eventp;
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate DEBUG2("hpc_slot_event_notify: handle=%x event=%x", handle, event);
7197c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
7207c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
7217c478bd9Sstevel@tonic-gate ASSERT(slotp == slotp->slot_handle);
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate if (slotp->slot_event_handler == NULL)
7247c478bd9Sstevel@tonic-gate return (HPC_EVENT_UNCLAIMED);
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate * If the request is to handle the event synchronously, then call
7287c478bd9Sstevel@tonic-gate * the event handler without queuing the event.
7297c478bd9Sstevel@tonic-gate */
7307c478bd9Sstevel@tonic-gate if (flags == HPC_EVENT_SYNCHRONOUS) {
7317c478bd9Sstevel@tonic-gate caddr_t arg;
7327c478bd9Sstevel@tonic-gate int (* func)(caddr_t, uint_t);
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate func = slotp->slot_event_handler;
7357c478bd9Sstevel@tonic-gate arg = slotp->slot_event_handler_arg;
7367c478bd9Sstevel@tonic-gate return (func(arg, event));
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate * Insert the event into the bus slot event handler list and
7407c478bd9Sstevel@tonic-gate * signal the bus slot event handler dispatch thread.
7417c478bd9Sstevel@tonic-gate */
7427c478bd9Sstevel@tonic-gate busp = slotp->slot_bus;
7437c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate if (busp->bus_slot_event_list_head == NULL) {
7467c478bd9Sstevel@tonic-gate eventp = busp->bus_slot_event_list_head =
7477c478bd9Sstevel@tonic-gate hpc_alloc_event_entry();
7487c478bd9Sstevel@tonic-gate } else {
7497c478bd9Sstevel@tonic-gate for (eventp = busp->bus_slot_event_list_head;
7507c478bd9Sstevel@tonic-gate eventp->next != NULL; eventp = eventp->next)
7517c478bd9Sstevel@tonic-gate ;
7527c478bd9Sstevel@tonic-gate eventp->next = hpc_alloc_event_entry();
7537c478bd9Sstevel@tonic-gate eventp = eventp->next;
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate eventp->slotp = slotp;
7567c478bd9Sstevel@tonic-gate eventp->event = event;
7577c478bd9Sstevel@tonic-gate eventp->next = NULL;
7587c478bd9Sstevel@tonic-gate DEBUG2("hpc_slot_event_notify: busp=%x event=%x", busp, event);
7597c478bd9Sstevel@tonic-gate cv_signal(&busp->bus_thread_cv);
7607c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
7617c478bd9Sstevel@tonic-gate return (HPC_EVENT_CLAIMED);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate int
hpc_nexus_connect(hpc_slot_t handle,void * data,uint_t flags)7667c478bd9Sstevel@tonic-gate hpc_nexus_connect(hpc_slot_t handle, void *data, uint_t flags)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
7717c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
7727c478bd9Sstevel@tonic-gate if (slotp->slot_ops.hpc_op_connect)
7737c478bd9Sstevel@tonic-gate return (slotp->slot_ops.hpc_op_connect(slotp->slot_ops_arg,
7747c478bd9Sstevel@tonic-gate handle, data, flags));
7757c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate int
hpc_nexus_disconnect(hpc_slot_t handle,void * data,uint_t flags)7807c478bd9Sstevel@tonic-gate hpc_nexus_disconnect(hpc_slot_t handle, void *data, uint_t flags)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
7857c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
7867c478bd9Sstevel@tonic-gate if (slotp->slot_ops.hpc_op_disconnect)
7877c478bd9Sstevel@tonic-gate return (slotp->slot_ops.hpc_op_disconnect(slotp->slot_ops_arg,
7887c478bd9Sstevel@tonic-gate handle, data, flags));
7897c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate int
hpc_nexus_insert(hpc_slot_t handle,void * data,uint_t flags)7947c478bd9Sstevel@tonic-gate hpc_nexus_insert(hpc_slot_t handle, void *data, uint_t flags)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
7997c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
8007c478bd9Sstevel@tonic-gate if (slotp->slot_ops.hpc_op_insert)
8017c478bd9Sstevel@tonic-gate return (slotp->slot_ops.hpc_op_insert(slotp->slot_ops_arg,
8027c478bd9Sstevel@tonic-gate handle, data, flags));
8037c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate int
hpc_nexus_remove(hpc_slot_t handle,void * data,uint_t flags)8087c478bd9Sstevel@tonic-gate hpc_nexus_remove(hpc_slot_t handle, void *data, uint_t flags)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
8137c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
8147c478bd9Sstevel@tonic-gate if (slotp->slot_ops.hpc_op_remove)
8157c478bd9Sstevel@tonic-gate return (slotp->slot_ops.hpc_op_remove(slotp->slot_ops_arg,
8167c478bd9Sstevel@tonic-gate handle, data, flags));
8177c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate int
hpc_nexus_control(hpc_slot_t handle,int request,caddr_t arg)8227c478bd9Sstevel@tonic-gate hpc_nexus_control(hpc_slot_t handle, int request, caddr_t arg)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate ASSERT(handle != NULL);
8277c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)handle;
8287c478bd9Sstevel@tonic-gate if (slotp->slot_ops.hpc_op_control)
8297c478bd9Sstevel@tonic-gate return (slotp->slot_ops.hpc_op_control(slotp->slot_ops_arg,
8307c478bd9Sstevel@tonic-gate handle, request, arg));
8317c478bd9Sstevel@tonic-gate return (HPC_ERR_FAILED);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate * The following function is run from the bus entries slot event handling
8367c478bd9Sstevel@tonic-gate * thread.
8377c478bd9Sstevel@tonic-gate */
8387c478bd9Sstevel@tonic-gate static void
hpc_slot_event_dispatcher(hpc_bus_entry_t * busp)8397c478bd9Sstevel@tonic-gate hpc_slot_event_dispatcher(hpc_bus_entry_t *busp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate hpc_event_entry_t *eventp;
8427c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
8437c478bd9Sstevel@tonic-gate int event;
8447c478bd9Sstevel@tonic-gate caddr_t arg;
8457c478bd9Sstevel@tonic-gate int (* func)(caddr_t, uint_t);
8467c478bd9Sstevel@tonic-gate callb_cpr_t cprinfo;
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate * The creator of this thread is waiting to be signaled that
8507c478bd9Sstevel@tonic-gate * the thread has been started.
8517c478bd9Sstevel@tonic-gate */
8527c478bd9Sstevel@tonic-gate DEBUG1("hpc_slot_event_dispatcher: busp=%x", busp);
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &busp->bus_mutex, callb_generic_cpr,
8557c478bd9Sstevel@tonic-gate "hpc_slot_event_dispatcher");
8567c478bd9Sstevel@tonic-gate
8577c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate * Wait for events to queue and then process them.
8607c478bd9Sstevel@tonic-gate */
8617c478bd9Sstevel@tonic-gate for (;;) {
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate * Note we only hold the mutex while determining
8657c478bd9Sstevel@tonic-gate * the number of entries that have been added to
8667c478bd9Sstevel@tonic-gate * the event list, while updating the event list
8677c478bd9Sstevel@tonic-gate * after processing the event list entries.
8687c478bd9Sstevel@tonic-gate */
8697c478bd9Sstevel@tonic-gate if (busp->bus_slot_event_list_head == NULL) {
8707c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
8717c478bd9Sstevel@tonic-gate cv_wait(&busp->bus_thread_cv, &busp->bus_mutex);
8727c478bd9Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &busp->bus_mutex);
8737c478bd9Sstevel@tonic-gate if (busp->bus_thread_exit)
8747c478bd9Sstevel@tonic-gate break;
8757c478bd9Sstevel@tonic-gate continue;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate * We have an event handler instance in the list to
8807c478bd9Sstevel@tonic-gate * process. Remove the head of the list, saving the
8817c478bd9Sstevel@tonic-gate * information required to run the event handler.
8827c478bd9Sstevel@tonic-gate * Then run the event handler while the bus mutex
8837c478bd9Sstevel@tonic-gate * is released.
8847c478bd9Sstevel@tonic-gate */
8857c478bd9Sstevel@tonic-gate eventp = busp->bus_slot_event_list_head;
8867c478bd9Sstevel@tonic-gate slotp = eventp->slotp;
8877c478bd9Sstevel@tonic-gate event = eventp->event;
8887c478bd9Sstevel@tonic-gate func = slotp->slot_event_handler;
8897c478bd9Sstevel@tonic-gate arg = slotp->slot_event_handler_arg;
8907c478bd9Sstevel@tonic-gate busp->bus_slot_event_list_head = eventp->next;
8917c478bd9Sstevel@tonic-gate hpc_free_event_entry(eventp);
8927c478bd9Sstevel@tonic-gate mutex_exit(&busp->bus_mutex);
8937c478bd9Sstevel@tonic-gate func(arg, event);
8947c478bd9Sstevel@tonic-gate mutex_enter(&busp->bus_mutex);
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate if (busp->bus_thread_exit)
8977c478bd9Sstevel@tonic-gate break;
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate
9007c478bd9Sstevel@tonic-gate DEBUG0("hpc_slot_event_dispatcher: thread_exit");
9017c478bd9Sstevel@tonic-gate cv_signal(&busp->bus_thread_cv);
9027c478bd9Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
9037c478bd9Sstevel@tonic-gate thread_exit();
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate static hpc_bus_entry_t *
hpc_find_bus_by_name(char * path)9087c478bd9Sstevel@tonic-gate hpc_find_bus_by_name(char *path)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate for (busp = hpc_bus_list_head; busp != NULL; busp = busp->bus_next) {
9137c478bd9Sstevel@tonic-gate if (strcmp(path, busp->bus_name) == 0)
9147c478bd9Sstevel@tonic-gate break;
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate return (busp);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate boolean_t
hpc_bus_registered(hpc_slot_t slot_hdl)9207c478bd9Sstevel@tonic-gate hpc_bus_registered(hpc_slot_t slot_hdl)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate hpc_slot_entry_t *slotp;
9237c478bd9Sstevel@tonic-gate hpc_bus_entry_t *busp;
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate slotp = (hpc_slot_entry_t *)slot_hdl;
9267c478bd9Sstevel@tonic-gate busp = slotp->slot_bus;
9277c478bd9Sstevel@tonic-gate return (busp->bus_registered);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate
9317c478bd9Sstevel@tonic-gate #ifdef DEBUG
9327c478bd9Sstevel@tonic-gate
9337c478bd9Sstevel@tonic-gate extern void prom_printf(const char *, ...);
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate static void
debug(char * fmt,uintptr_t a1,uintptr_t a2,uintptr_t a3,uintptr_t a4,uintptr_t a5)9367c478bd9Sstevel@tonic-gate debug(char *fmt, uintptr_t a1, uintptr_t a2, uintptr_t a3,
9377c478bd9Sstevel@tonic-gate uintptr_t a4, uintptr_t a5)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate if (hpcsvc_debug != 0) {
9407c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "hpcsvc: ");
9417c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, fmt, a1, a2, a3, a4, a5);
9427c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "\n");
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate #endif
946