10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5895Syz147064 * Common Development and Distribution License (the "License").
6*5895Syz147064 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*5895Syz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23*5895Syz147064 * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * DACF (Device Autoconfiguration Framework) client code.
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * DACF has two clients. the first is dacf modules which implement
320Sstevel@tonic-gate * configuration operations; the second is the set of hooks in the kernel
330Sstevel@tonic-gate * which do rule matching and invoke configuration operations.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * This file implements the second part, the kernel hooks.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * Currently implemented are post-attach and pre-detach handlers, and the hook
380Sstevel@tonic-gate * for ddi_create_minor_common() which sets up post-attach and pre-detach
390Sstevel@tonic-gate * reservations.
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * This code depends on the core dacf code (in dacf.c) but the converse should
420Sstevel@tonic-gate * never be true.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * This file also implements '__kernel', the kernel-supplied dacf module.
450Sstevel@tonic-gate * For now, this is pretty much empty, except under DEBUG, in which case it
460Sstevel@tonic-gate * contains some debugging code.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate #include <sys/kmem.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
540Sstevel@tonic-gate #include <sys/pathname.h>
550Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
560Sstevel@tonic-gate #include <sys/sunddi.h>
570Sstevel@tonic-gate #include <sys/autoconf.h>
580Sstevel@tonic-gate #include <sys/modhash.h>
590Sstevel@tonic-gate #include <sys/dacf_impl.h>
600Sstevel@tonic-gate #include <sys/systm.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * dacfc_match_create_minor()
650Sstevel@tonic-gate * Check to see if this minor node creation sequence matches a dacf
660Sstevel@tonic-gate * (device autoconfiguration framework) rule. If so make a reservation
670Sstevel@tonic-gate * for the operation to be invoked at post-attach and/or pre-detach time.
680Sstevel@tonic-gate */
690Sstevel@tonic-gate void
dacfc_match_create_minor(char * name,char * node_type,dev_info_t * dip,struct ddi_minor_data * dmdp,int flag)700Sstevel@tonic-gate dacfc_match_create_minor(char *name, char *node_type, dev_info_t *dip,
710Sstevel@tonic-gate struct ddi_minor_data *dmdp, int flag)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate dacf_rule_t *r;
740Sstevel@tonic-gate char *dev_path, *dev_pathp, *drv_mname = NULL;
750Sstevel@tonic-gate dacf_rsrvlist_t *pa_rsrv, *pd_rsrv;
760Sstevel@tonic-gate
77*5895Syz147064 /*
78*5895Syz147064 * Check the dacf rule for non-clone devices or for network devices.
79*5895Syz147064 */
80*5895Syz147064 if ((flag & CLONE_DEV) && (strcmp(node_type, DDI_NT_NET) != 0)) {
810Sstevel@tonic-gate return;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * Because dacf currently only implements post-attach and pre-detach
860Sstevel@tonic-gate * processing, we only care about minor nodes created during attach.
870Sstevel@tonic-gate * However, there is no restriction on drivers about when to create
880Sstevel@tonic-gate * minor nodes.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate if (!DEVI_IS_ATTACHING(dmdp->dip)) {
910Sstevel@tonic-gate return;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate dev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
950Sstevel@tonic-gate dev_pathp = ddi_pathname(dip, dev_path);
960Sstevel@tonic-gate pa_rsrv = kmem_alloc(sizeof (dacf_rsrvlist_t), KM_SLEEP);
970Sstevel@tonic-gate pd_rsrv = kmem_alloc(sizeof (dacf_rsrvlist_t), KM_SLEEP);
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (name) {
1000Sstevel@tonic-gate const char *drv_name = ddi_driver_name(dip);
1010Sstevel@tonic-gate if (drv_name == NULL)
1020Sstevel@tonic-gate drv_name = "???";
1030Sstevel@tonic-gate drv_mname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1040Sstevel@tonic-gate (void) snprintf(drv_mname, MAXPATHLEN, "%s:%s", drv_name, name);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate mutex_enter(&dacf_lock);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Ensure that we don't wind up in a 'matching loop' against a devinfo
1110Sstevel@tonic-gate * node, which could cause deadlock. This could happen as follows:
1120Sstevel@tonic-gate *
1130Sstevel@tonic-gate * We match (just below)
1140Sstevel@tonic-gate * We invoke a task (later, at the end of devi_attach)
1150Sstevel@tonic-gate * this means we have taken the per-devinfo lock
1160Sstevel@tonic-gate * The task invoke winds up causing the same driver (that has
1170Sstevel@tonic-gate * just finished attaching) to create another minor node.
1180Sstevel@tonic-gate * We try to re-acquire the per-devinfo list lock again in the
1190Sstevel@tonic-gate * process of making another reservation
1200Sstevel@tonic-gate */
1210Sstevel@tonic-gate mutex_enter(&(DEVI(dip)->devi_lock));
1220Sstevel@tonic-gate if (DEVI_IS_INVOKING_DACF(dip)) {
1230Sstevel@tonic-gate mutex_exit(&(DEVI(dip)->devi_lock));
1240Sstevel@tonic-gate cmn_err(CE_WARN,
1250Sstevel@tonic-gate "!dacf detected deadlock, aborting matching procedure\n");
1260Sstevel@tonic-gate mutex_exit(&dacf_lock);
1270Sstevel@tonic-gate kmem_free(pa_rsrv, sizeof (dacf_rsrvlist_t));
1280Sstevel@tonic-gate kmem_free(pd_rsrv, sizeof (dacf_rsrvlist_t));
1290Sstevel@tonic-gate kmem_free(dev_path, MAXPATHLEN);
1300Sstevel@tonic-gate if (drv_mname) {
1310Sstevel@tonic-gate kmem_free(drv_mname, MAXPATHLEN);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate return;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate mutex_exit(&(DEVI(dip)->devi_lock));
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * Do rule matching. It's possible to construct two rules that would
1390Sstevel@tonic-gate * match against the same minor node, so we match from most to least
1400Sstevel@tonic-gate * specific:
1410Sstevel@tonic-gate * device path
1420Sstevel@tonic-gate * minor node name (concatenation of drv_name:name
1430Sstevel@tonic-gate * node type
1440Sstevel@tonic-gate *
1450Sstevel@tonic-gate * Future additions to the set of device-specifiers should be
1460Sstevel@tonic-gate * sensitive to this ordering.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * post-attach matching
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate r = NULL;
1530Sstevel@tonic-gate if (dev_pathp) {
1540Sstevel@tonic-gate r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_DEV_PATH,
1550Sstevel@tonic-gate dev_pathp);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate if (!r && drv_mname) {
1580Sstevel@tonic-gate r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_DRV_MNAME,
1590Sstevel@tonic-gate drv_mname);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate if (!r && node_type) {
1620Sstevel@tonic-gate r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_MIN_NT, node_type);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate if (r) {
1650Sstevel@tonic-gate dacf_rsrv_make(pa_rsrv, r, dmdp, &(DEVI(dip)->devi_dacf_tasks));
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (dacfdebug & DACF_DBG_MSGS)
1680Sstevel@tonic-gate printf("dacf: made 'post-attach' reservation for "
1690Sstevel@tonic-gate "%s, %s, %s\n", name, node_type, dev_pathp);
1700Sstevel@tonic-gate } else {
1710Sstevel@tonic-gate kmem_free(pa_rsrv, sizeof (dacf_rsrvlist_t));
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * pre-detach matching
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate r = NULL;
1780Sstevel@tonic-gate if (dev_pathp) {
1790Sstevel@tonic-gate r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_DEV_PATH,
1800Sstevel@tonic-gate dev_pathp);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate if (!r && drv_mname) {
1830Sstevel@tonic-gate r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_DRV_MNAME,
1840Sstevel@tonic-gate drv_mname);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate if (!r && node_type) {
1870Sstevel@tonic-gate r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_MIN_NT, node_type);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate if (r) {
1900Sstevel@tonic-gate dacf_rsrv_make(pd_rsrv, r, dmdp, &(DEVI(dip)->devi_dacf_tasks));
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (dacfdebug & DACF_DBG_MSGS) {
1930Sstevel@tonic-gate printf("dacf: made 'pre-detach' reservation for "
1940Sstevel@tonic-gate "%s, %s, %s\n", name, node_type, dev_pathp);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate } else {
1970Sstevel@tonic-gate kmem_free(pd_rsrv, sizeof (dacf_rsrvlist_t));
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate mutex_exit(&dacf_lock);
2010Sstevel@tonic-gate kmem_free(dev_path, MAXPATHLEN);
2020Sstevel@tonic-gate if (drv_mname) {
2030Sstevel@tonic-gate kmem_free(drv_mname, MAXPATHLEN);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * dacfc_postattach()
2090Sstevel@tonic-gate * autoconfiguration for post-attach events.
2100Sstevel@tonic-gate *
2110Sstevel@tonic-gate * strategy: try to configure. If some of the configuration operations
2120Sstevel@tonic-gate * fail, emit a warning.
2130Sstevel@tonic-gate */
2140Sstevel@tonic-gate int
dacfc_postattach(dev_info_t * devi)2150Sstevel@tonic-gate dacfc_postattach(dev_info_t *devi)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate int err = DACF_SUCCESS;
2180Sstevel@tonic-gate char *path, *pathp;
2190Sstevel@tonic-gate dacf_rsrvlist_t **opsp, *op;
2200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dacf_lock));
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * Instruct dacf_process_rsrvs() to invoke each POSTATTACH op.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate opsp = &DEVI(devi)->devi_dacf_tasks;
2260Sstevel@tonic-gate dacf_process_rsrvs(opsp, DACF_OPID_POSTATTACH, DACF_PROC_INVOKE);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate * Check to see that all POSTATTACH's succeeded.
2300Sstevel@tonic-gate */
2310Sstevel@tonic-gate for (op = *opsp; op != NULL; op = op->rsrv_next) {
2320Sstevel@tonic-gate if (op->rsrv_rule->r_opid != DACF_OPID_POSTATTACH)
2330Sstevel@tonic-gate continue;
2340Sstevel@tonic-gate if (op->rsrv_result == DACF_SUCCESS)
2350Sstevel@tonic-gate continue;
2360Sstevel@tonic-gate if (dacfdebug & DACF_DBG_DEVI) {
2370Sstevel@tonic-gate cmn_err(CE_WARN, "op failed, err = %d\n",
2380Sstevel@tonic-gate op->rsrv_result);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate err = DACF_FAILURE;
2410Sstevel@tonic-gate break;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * If one or more postattach's failed, give up.
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate if ((err == DACF_FAILURE) && (dacfdebug & DACF_DBG_DEVI)) {
2480Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2490Sstevel@tonic-gate if ((pathp = ddi_pathname(devi, path)) == NULL)
2500Sstevel@tonic-gate pathp = "<unknown>";
2510Sstevel@tonic-gate cmn_err(CE_WARN, "%s attached, but failed to auto-configure",
2520Sstevel@tonic-gate pathp);
2530Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate return (err);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * dacfc_predetach()
2610Sstevel@tonic-gate * auto-unconfiguration for pre-detach events.
2620Sstevel@tonic-gate *
2630Sstevel@tonic-gate * strategy: call the pre-detach operation for all matching reservations.
2640Sstevel@tonic-gate * If any of these fail, make (one) attempt to reconfigure things back
2650Sstevel@tonic-gate * into a sane state. if that fails, our state is uncertain.
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate int
dacfc_predetach(dev_info_t * devi)2680Sstevel@tonic-gate dacfc_predetach(dev_info_t *devi)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate int err = DDI_SUCCESS;
2710Sstevel@tonic-gate char *path, *pathp;
2720Sstevel@tonic-gate dacf_rsrvlist_t **opsp, *op;
2730Sstevel@tonic-gate ASSERT(MUTEX_HELD(&dacf_lock));
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * Instruct dacf_process_rsrvs() to invoke each PREDETACH op.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate opsp = &DEVI(devi)->devi_dacf_tasks;
2790Sstevel@tonic-gate dacf_process_rsrvs(opsp, DACF_OPID_PREDETACH, DACF_PROC_INVOKE);
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Check to see that all PREDETACH's succeeded.
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate for (op = *opsp; op != NULL; op = op->rsrv_next) {
2850Sstevel@tonic-gate if (op->rsrv_rule->r_opid != DACF_OPID_PREDETACH)
2860Sstevel@tonic-gate continue;
2870Sstevel@tonic-gate if (op->rsrv_result == 0)
2880Sstevel@tonic-gate continue;
2890Sstevel@tonic-gate err = DDI_FAILURE;
2900Sstevel@tonic-gate break;
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate * If one or more predetach's failed, make one attempt to fix things
2950Sstevel@tonic-gate * by re-running all of the POST-ATTACH operations. If any of those
2960Sstevel@tonic-gate * fail, give up.
2970Sstevel@tonic-gate */
2980Sstevel@tonic-gate if (err == DDI_FAILURE) {
2990Sstevel@tonic-gate int pa_err;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate if (dacfdebug & DACF_DBG_DEVI) {
3020Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3030Sstevel@tonic-gate if ((pathp = ddi_pathname(devi, path)) == NULL)
3040Sstevel@tonic-gate pathp = "<unknown>";
3050Sstevel@tonic-gate cmn_err(CE_WARN, "%s failed to auto-unconfigure, "
3060Sstevel@tonic-gate "attempting to reconfigure...", pathp);
3070Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate pa_err = dacfc_postattach(devi);
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (dacfdebug & DACF_DBG_DEVI) {
3130Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3140Sstevel@tonic-gate if ((pathp = ddi_pathname(devi, path)) == NULL)
3150Sstevel@tonic-gate pathp = "<unknown>";
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate if (pa_err == DDI_FAILURE) {
3180Sstevel@tonic-gate cmn_err(CE_WARN, "%s failed to "
3190Sstevel@tonic-gate "auto-unconfigure, and could not be "
3200Sstevel@tonic-gate "re-autoconfigured.", pathp);
3210Sstevel@tonic-gate } else {
3220Sstevel@tonic-gate cmn_err(CE_WARN, "%s failed to "
3230Sstevel@tonic-gate "auto-unconfigure, but was successfully "
3240Sstevel@tonic-gate "re-autoconfigured.", pathp);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate kmem_free(path, MAXPATHLEN);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate return (err);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * kmod_dacfsw:
3350Sstevel@tonic-gate * This is the declaration for the kernel-supplied '__kernel' dacf module.
3360Sstevel@tonic-gate * DACF supplies a framework based around loadable modules. However, it
3370Sstevel@tonic-gate * may be convenient (in the future) to have a module provided by the
3380Sstevel@tonic-gate * kernel. This is useful in cases when a module can't be loaded (early in
3390Sstevel@tonic-gate * boot), or for code that would never get unloaded anyway.
3400Sstevel@tonic-gate */
3410Sstevel@tonic-gate #ifdef DEBUG
3420Sstevel@tonic-gate /*ARGSUSED*/
3430Sstevel@tonic-gate static int
kmod_test_postattach(dacf_infohdl_t info_hdl,dacf_arghdl_t arg_hdl,int flags)3440Sstevel@tonic-gate kmod_test_postattach(dacf_infohdl_t info_hdl, dacf_arghdl_t arg_hdl, int flags)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate const char *verbose = dacf_get_arg(arg_hdl, "verbose");
3470Sstevel@tonic-gate if (verbose && (strcmp(verbose, "true") == 0)) {
3480Sstevel@tonic-gate cmn_err(CE_WARN, "got kmod_test_postattach\n");
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate return (0);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate #endif
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate static dacf_op_t kmod_op_test[] = {
3550Sstevel@tonic-gate #ifdef DEBUG
3560Sstevel@tonic-gate { DACF_OPID_POSTATTACH, kmod_test_postattach },
3570Sstevel@tonic-gate #endif
3580Sstevel@tonic-gate { DACF_OPID_END, NULL },
3590Sstevel@tonic-gate };
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate static dacf_opset_t kmod_opsets[] = {
3620Sstevel@tonic-gate #ifdef DEBUG
3630Sstevel@tonic-gate { "kmod_test", kmod_op_test },
3640Sstevel@tonic-gate #endif
3650Sstevel@tonic-gate { NULL, NULL },
3660Sstevel@tonic-gate };
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate struct dacfsw kmod_dacfsw = {
3690Sstevel@tonic-gate DACF_MODREV_1, kmod_opsets
3700Sstevel@tonic-gate };
371