xref: /onnv-gate/usr/src/cmd/pcidr/plugins/default/pcidr_plugin.c (revision 881:e6bc7f4b8a33)
1*881Sjohnny /*
2*881Sjohnny  * CDDL HEADER START
3*881Sjohnny  *
4*881Sjohnny  * The contents of this file are subject to the terms of the
5*881Sjohnny  * Common Development and Distribution License, Version 1.0 only
6*881Sjohnny  * (the "License").  You may not use this file except in compliance
7*881Sjohnny  * with the License.
8*881Sjohnny  *
9*881Sjohnny  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*881Sjohnny  * or http://www.opensolaris.org/os/licensing.
11*881Sjohnny  * See the License for the specific language governing permissions
12*881Sjohnny  * and limitations under the License.
13*881Sjohnny  *
14*881Sjohnny  * When distributing Covered Code, include this CDDL HEADER in each
15*881Sjohnny  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*881Sjohnny  * If applicable, add the following below this CDDL HEADER, with the
17*881Sjohnny  * fields enclosed by brackets "[]" replaced with your own identifying
18*881Sjohnny  * information: Portions Copyright [yyyy] [name of copyright owner]
19*881Sjohnny  *
20*881Sjohnny  * CDDL HEADER END
21*881Sjohnny  */
22*881Sjohnny /*
23*881Sjohnny  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*881Sjohnny  * Use is subject to license terms.
25*881Sjohnny  */
26*881Sjohnny 
27*881Sjohnny #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*881Sjohnny 
29*881Sjohnny #include <stdio.h>
30*881Sjohnny #include <stdlib.h>
31*881Sjohnny #include <unistd.h>
32*881Sjohnny #include <strings.h>
33*881Sjohnny #include <errno.h>
34*881Sjohnny #include <sys/param.h>
35*881Sjohnny #include <sys/systeminfo.h>
36*881Sjohnny #include <sys/sysevent/eventdefs.h>
37*881Sjohnny #include <sys/sysevent/dr.h>
38*881Sjohnny #include <syslog.h>
39*881Sjohnny #include <libnvpair.h>
40*881Sjohnny #include <stdarg.h>
41*881Sjohnny #include <assert.h>
42*881Sjohnny #include <sys/stat.h>
43*881Sjohnny #include <dlfcn.h>
44*881Sjohnny #include <pcidr.h>
45*881Sjohnny #include <pcidr_cfga.h>
46*881Sjohnny 
47*881Sjohnny 
PCIDR_PLUGIN_PROTO(attrlistp,optp)48*881Sjohnny PCIDR_PLUGIN_PROTO(attrlistp, optp)
49*881Sjohnny {
50*881Sjohnny 	char *fn = PCIDR_PLUGIN_SYMSTR;
51*881Sjohnny 	int rv = 0;
52*881Sjohnny 	char *cfga_errstr = NULL;
53*881Sjohnny 	char *str, *apid;
54*881Sjohnny 	cfga_list_data_t *cfga_listp = NULL;
55*881Sjohnny 	cfga_cmd_t cmd;
56*881Sjohnny 	int cfga_list_len;
57*881Sjohnny 	pcidr_attrs_t dr;
58*881Sjohnny 
59*881Sjohnny 	pcidr_set_logopt(&optp->logopt);
60*881Sjohnny 
61*881Sjohnny 	if (pcidr_get_attrs(attrlistp, &dr) != 0 ||
62*881Sjohnny 	    pcidr_check_attrs(&dr) != 0) {
63*881Sjohnny 		dprint(DWARN, "%s: invalid or missing attributes\n", fn);
64*881Sjohnny 		return (EINVAL);
65*881Sjohnny 	}
66*881Sjohnny 
67*881Sjohnny 	/*
68*881Sjohnny 	 * get state of APID; enforce the cfgadm pci plugin implementation of
69*881Sjohnny 	 * returning one matching AP per supplied apid string
70*881Sjohnny 	 */
71*881Sjohnny 	rv = config_list_ext(1, &dr.dr_ap_id, &cfga_listp, &cfga_list_len,
72*881Sjohnny 	    NULL, NULL, &cfga_errstr, CFGA_FLAG_LIST_ALL);
73*881Sjohnny 	if (rv != CFGA_OK) {
74*881Sjohnny 		str = pcidr_cfga_err_name(rv);
75*881Sjohnny 		if (str == NULL)
76*881Sjohnny 			str = "unrecognized rv!";
77*881Sjohnny 		dprint(DDEBUG, "%s: config_list_ext() on apid = \"%s\" "
78*881Sjohnny 		    "failed: rv = %d (%s)", fn, dr.dr_ap_id, rv, str);
79*881Sjohnny 
80*881Sjohnny 		if (cfga_errstr != NULL) {
81*881Sjohnny 			dprint(DDEBUG, ", error string = \"%s\"",
82*881Sjohnny 			    cfga_errstr);
83*881Sjohnny 			free(cfga_errstr);
84*881Sjohnny 		}
85*881Sjohnny 		dprint(DDEBUG, "\n");
86*881Sjohnny 		rv = EINVAL;
87*881Sjohnny 		goto OUT;
88*881Sjohnny 	}
89*881Sjohnny 	if (cfga_list_len != 1) {
90*881Sjohnny 		dprint(DWARN, "%s: invalid condition - more than one AP was "
91*881Sjohnny 		    "found for the APID \"%s\"\n", fn, dr.dr_ap_id);
92*881Sjohnny 		rv = EINVAL;
93*881Sjohnny 		goto OUT;
94*881Sjohnny 	}
95*881Sjohnny 
96*881Sjohnny 	/*
97*881Sjohnny 	 * perform DR
98*881Sjohnny 	 */
99*881Sjohnny 	dprint(DINFO, "%s: showing info and performing DR on APID(s) "
100*881Sjohnny 	    "matching \"%s\"\n", fn, dr.dr_ap_id);
101*881Sjohnny 
102*881Sjohnny 	cmd = CFGA_CMD_NONE;
103*881Sjohnny 	dprint(DINFO, "===========================================\n", fn);
104*881Sjohnny 	pcidr_print_cfga(DINFO, &cfga_listp[0], "  .. ");
105*881Sjohnny 	apid = cfga_listp[0].ap_phys_id;
106*881Sjohnny 
107*881Sjohnny 	if (strcmp(dr.dr_req_type, DR_REQ_OUTGOING_RES) == 0) {
108*881Sjohnny 		cmd = CFGA_CMD_DISCONNECT;
109*881Sjohnny 		dprint(DINFO, "%s: disconnecting ...\n", fn, apid);
110*881Sjohnny 
111*881Sjohnny 		rv = pcidr_cfga_do_cmd(cmd, &cfga_listp[0]);
112*881Sjohnny 		if (rv < 0) {
113*881Sjohnny 			dprint(DINFO, "%s: disconnect FAILED\n", fn);
114*881Sjohnny 			rv = EIO;
115*881Sjohnny 		}
116*881Sjohnny 		else
117*881Sjohnny 			dprint(DINFO, "%s: disconnect OK\n", fn);
118*881Sjohnny 
119*881Sjohnny 		goto OUT;
120*881Sjohnny 	}
121*881Sjohnny 	if (strcmp(dr.dr_req_type, DR_REQ_INCOMING_RES) == 0) {
122*881Sjohnny 		cmd = CFGA_CMD_CONFIGURE;
123*881Sjohnny 		dprint(DINFO, "%s: configuring ...\n", fn, apid);
124*881Sjohnny 
125*881Sjohnny 		rv = pcidr_cfga_do_cmd(cmd, &cfga_listp[0]);
126*881Sjohnny 		if (rv < 0) {
127*881Sjohnny 			dprint(DINFO, "%s: configure FAILED\n", fn);
128*881Sjohnny 			rv = EIO;
129*881Sjohnny 		} else
130*881Sjohnny 			dprint(DINFO, "%s: configure OK\n", fn);
131*881Sjohnny 
132*881Sjohnny 		goto OUT;
133*881Sjohnny 	}
134*881Sjohnny 
135*881Sjohnny 	/* we should not get here if pcidr_check_attrs() is correct */
136*881Sjohnny 	dprint(DWARN, "%s: invalid dr_req_type = %s\n", fn, dr.dr_req_type);
137*881Sjohnny 	assert(cmd != CFGA_CMD_NONE);
138*881Sjohnny 	return (EINVAL);
139*881Sjohnny 	/*NOTREACHED*/
140*881Sjohnny OUT:
141*881Sjohnny 	if (cfga_listp != NULL)
142*881Sjohnny 		free(cfga_listp);
143*881Sjohnny 	return (rv);
144*881Sjohnny }
145