1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2016 by Delphix. All rights reserved.
25  */
26 
27 /*
28  *	Plugin library for PCI Express and PCI (SHPC) hotplug controller
29  */
30 
31 #include <stddef.h>
32 #include <locale.h>
33 #include <ctype.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <locale.h>
41 #include <langinfo.h>
42 #include <time.h>
43 #include <sys/param.h>
44 #include <stdarg.h>
45 #include <libdevinfo.h>
46 #include <libdevice.h>
47 
48 #define	CFGA_PLUGIN_LIB
49 
50 #include <config_admin.h>
51 
52 #include <assert.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/dditypes.h>
56 #include <sys/pci.h>
57 #include <libintl.h>
58 
59 #include <dirent.h>
60 #include <limits.h>
61 #include <sys/mkdev.h>
62 #include "../../../../uts/common/sys/hotplug/pci/pcie_hp.h"
63 #include "../../../../common/pci/pci_strings.h"
64 #include <libhotplug.h>
65 
66 #define	MSG_HOTPLUG_DISABLED \
67 	"Error: hotplug service is probably not running, " \
68 	"please use 'svcadm enable hotplug' to enable the service. " \
69 	"See cfgadm_shp(8) for more details."
70 
71 #define	DEVICES_DIR		"/devices"
72 #define	SLASH			"/"
73 #define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
74 
75 /*
76  * Set the version number
77  */
78 int cfga_version = CFGA_HSL_V2;
79 
80 #ifdef	DEBUG
81 #define	SHP_DBG	1
82 #endif
83 
84 #if !defined(TEXT_DOMAIN)
85 #define	TEXT_DOMAIN	"SYS_TEST"
86 #endif
87 
88 /*
89  *	DEBUGING LEVEL
90  *
91  *	External routines:  1 - 2
92  *	Internal routines:  3 - 4
93  */
94 #ifdef	SHP_DBG
95 int	shp_debug = 1;
96 #define	DBG(level, args) \
97 	{ if (shp_debug >= (level)) printf args; }
98 #define	DBG_F(level, args) \
99 	{ if (shp_debug >= (level)) fprintf args; }
100 #else
101 #define	DBG(level, args)	/* nothing */
102 #define	DBG_F(level, args)	/* nothing */
103 #endif
104 
105 #define	CMD_ACQUIRE		0
106 #define	CMD_GETSTAT		1
107 #define	CMD_LIST		2
108 #define	CMD_SLOT_CONNECT	3
109 #define	CMD_SLOT_DISCONNECT	4
110 #define	CMD_SLOT_CONFIGURE	5
111 #define	CMD_SLOT_UNCONFIGURE	6
112 #define	CMD_SLOT_INSERT		7
113 #define	CMD_SLOT_REMOVE		8
114 #define	CMD_OPEN		9
115 #define	CMD_FSTAT		10
116 #define	ERR_CMD_INVAL		11
117 #define	ERR_AP_INVAL		12
118 #define	ERR_AP_ERR		13
119 #define	ERR_OPT_INVAL		14
120 
121 static char *
122 cfga_errstrs[] = {
123 	/* n */ "acquire ",
124 	/* n */ "get-status ",
125 	/* n */ "list ",
126 	/* n */ "connect ",
127 	/* n */ "disconnect ",
128 	/* n */ "configure ",
129 	/* n */ "unconfigure ",
130 	/* n */ "insert ",
131 	/* n */ "remove ",
132 	/* n */ "open ",
133 	/* n */ "fstat ",
134 	/* y */ "invalid command ",
135 	/* y */ "invalid attachment point ",
136 	/* y */ "invalid transition ",
137 	/* y */ "invalid option ",
138 		NULL
139 };
140 
141 #define	HELP_HEADER		1
142 #define	HELP_CONFIG		2
143 #define	HELP_ENABLE_SLOT	3
144 #define	HELP_DISABLE_SLOT	4
145 #define	HELP_ENABLE_AUTOCONF	5
146 #define	HELP_DISABLE_AUTOCONF	6
147 #define	HELP_LED_CNTRL		7
148 #define	HELP_UNKNOWN		8
149 #define	SUCCESS			9
150 #define	FAILED			10
151 #define	UNKNOWN			11
152 
153 #define	MAXLINE			256
154 
155 extern int errno;
156 
157 static void cfga_err(char **errstring, ...);
158 static cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
159     char *slot_name, char **errstring);
160 static cfga_err_t check_options(const char *options);
161 static void cfga_msg(struct cfga_msg *msgp, const char *str);
162 static char *findlink(char *ap_phys_id);
163 
164 static char *
165 cfga_strs[] = {
166 NULL,
167 "\nPCI hotplug specific commands:",
168 "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
169 "ap_id [ap_id...]",
170 "\t-x enable_slot  ap_id [ap_id...]",
171 "\t-x disable_slot ap_id [ap_id...]",
172 "\t-x enable_autoconfig  ap_id [ap_id...]",
173 "\t-x disable_autoconfig ap_id [ap_id...]",
174 "\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
175 "\tunknown command or option: ",
176 "success   ",
177 "failed   ",
178 "unknown",
179 NULL
180 };
181 
182 #define	MAX_FORMAT 80
183 
184 #define	ENABLE_SLOT	0
185 #define	DISABLE_SLOT	1
186 #define	ENABLE_AUTOCNF	2
187 #define	DISABLE_AUTOCNF	3
188 #define	LED		4
189 #define	MODE		5
190 
191 typedef	enum { PCIEHPC_FAULT_LED, PCIEHPC_POWER_LED, PCIEHPC_ATTN_LED,
192 	PCIEHPC_ACTIVE_LED} pciehpc_led_t;
193 
194 typedef	enum { PCIEHPC_BOARD_UNKNOWN, PCIEHPC_BOARD_PCI_HOTPLUG }
195 	pciehpc_board_type_t;
196 
197 /*
198  * Board Type
199  */
200 static char *
201 board_strs[] = {
202 	/* n */ "???",	/* PCIEHPC_BOARD_UNKNOWN */
203 	/* n */ "hp",	/* PCIEHPC_BOARD_PCI_HOTPLUG */
204 	/* n */ NULL
205 };
206 
207 /*
208  * HW functions
209  */
210 static char *
211 func_strs[] = {
212 	/* n */ "enable_slot",
213 	/* n */ "disable_slot",
214 	/* n */ "enable_autoconfig",
215 	/* n */ "disable_autoconfig",
216 	/* n */ "led",
217 	/* n */ "mode",
218 	/* n */ NULL
219 };
220 
221 /*
222  * LED strings
223  */
224 static char *
225 led_strs[] = {
226 	/* n */ "fault",	/* PCIEHPC_FAULT_LED */
227 	/* n */ "power",	/* PCIEHPC_POWER_LED */
228 	/* n */ "attn",		/* PCIEHPC_ATTN_LED */
229 	/* n */ "active",	/* PCIEHPC_ACTIVE_LED */
230 	/* n */ NULL
231 };
232 
233 static char *
234 led_strs2[] = {
235 	/* n */ PCIEHPC_PROP_LED_FAULT,		/* PCIEHPC_FAULT_LED */
236 	/* n */ PCIEHPC_PROP_LED_POWER,		/* PCIEHPC_POWER_LED */
237 	/* n */ PCIEHPC_PROP_LED_ATTN,		/* PCIEHPC_ATTN_LED */
238 	/* n */ PCIEHPC_PROP_LED_ACTIVE,	/* PCIEHPC_ACTIVE_LED */
239 	/* n */ NULL
240 };
241 
242 #define	FAULT	0
243 #define	POWER	1
244 #define	ATTN	2
245 #define	ACTIVE	3
246 
247 static char *
248 mode_strs[] = {
249 	/* n */ "off",		/* OFF */
250 	/* n */ "on",		/* ON */
251 	/* n */ "blink",	/* BLINK */
252 	/* n */ "default",	/* DEFAULT */
253 	/* n */	NULL
254 };
255 
256 #define	OFF	0
257 #define	ON	1
258 #define	BLINK	2
259 #define	DEFAULT	3
260 
261 #define	cfga_errstrs(i)		cfga_errstrs[(i)]
262 
263 #define	cfga_eid(a, b)		(((a) << 8) + (b))
264 #define	MAXDEVS			32
265 
266 typedef enum {
267 	SOLARIS_SLT_NAME,
268 	PROM_SLT_NAME
269 } slt_name_src_t;
270 
271 struct searcharg {
272 	char	*devpath;
273 	char	slotnames[MAXDEVS][MAXNAMELEN];
274 	int	minor;
275 	di_prom_handle_t	promp;
276 	slt_name_src_t	slt_name_src;
277 };
278 
279 static void *private_check;
280 
281 /*
282  * Return the corresponding hp node for a given ap_id, it is the caller's
283  * responsibility to call hp_fini() to free the snapshot.
284  */
285 static cfga_err_t
physpath2node(const char * physpath,char ** errstring,hp_node_t * nodep)286 physpath2node(const char *physpath, char **errstring, hp_node_t *nodep)
287 {
288 	char *rpath;
289 	char *cp;
290 	hp_node_t node;
291 	size_t len;
292 	char *errmsg;
293 
294 	if (getuid() != 0 && geteuid() != 0)
295 		return (CFGA_ERROR);
296 
297 	if ((rpath = malloc(strlen(physpath) + 1)) == NULL)
298 		return (CFGA_ERROR);
299 
300 	(void) strcpy(rpath, physpath);
301 
302 	/* Remove devices prefix (if any) */
303 	len = strlen(DEVICES_DIR);
304 	if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
305 		(void) memmove(rpath, rpath + len,
306 		    strlen(rpath + len) + 1);
307 	}
308 
309 	/* Remove dynamic component if any */
310 	if ((cp = GET_DYN(rpath)) != NULL) {
311 		*cp = '\0';
312 	}
313 
314 	/* Remove minor name (if any) */
315 	if ((cp = strrchr(rpath, ':')) == NULL) {
316 		free(rpath);
317 		return (CFGA_INVAL);
318 	}
319 
320 	*cp = '\0';
321 	cp++;
322 
323 	DBG(1, ("rpath=%s,cp=%s\n", rpath, cp));
324 	if ((node = hp_init(rpath, cp, 0)) == NULL) {
325 		if (errno == EBADF) {
326 			/* No reponse to operations on the door file. */
327 			assert(errstring != NULL);
328 			*errstring = strdup(MSG_HOTPLUG_DISABLED);
329 			free(rpath);
330 			return (CFGA_NOTSUPP);
331 		}
332 		free(rpath);
333 		return (CFGA_ERROR);
334 	}
335 
336 	free(rpath);
337 
338 	*nodep = node;
339 	return (CFGA_OK);
340 }
341 
342 typedef struct error_size_cb_arg {
343 	size_t	rsrc_width;
344 	size_t	info_width;
345 	int	cnt;
346 } error_size_cb_arg_t;
347 
348 /*
349  * Callback function for hp_traverse(), to sum up the
350  * maximum length for error message display.
351  */
352 static int
error_sizeup_cb(hp_node_t node,void * arg)353 error_sizeup_cb(hp_node_t node, void *arg)
354 {
355 	error_size_cb_arg_t	*sizearg = (error_size_cb_arg_t *)arg;
356 	size_t			len;
357 
358 	/* Only process USAGE nodes */
359 	if (hp_type(node) != HP_NODE_USAGE)
360 		return (HP_WALK_CONTINUE);
361 
362 	sizearg->cnt++;
363 
364 	/* size up resource name */
365 	len = strlen(hp_name(node));
366 	if (sizearg->rsrc_width < len)
367 		sizearg->rsrc_width = len;
368 
369 	/* size up usage description */
370 	len = strlen(hp_usage(node));
371 	if (sizearg->info_width < len)
372 		sizearg->info_width = len;
373 
374 	return (HP_WALK_CONTINUE);
375 }
376 
377 typedef struct error_sum_cb_arg {
378 	char **table;
379 	char *format;
380 } error_sum_cb_arg_t;
381 
382 /*
383  * Callback function for hp_traverse(), to add the error
384  * message to the table.
385  */
386 static int
error_sumup_cb(hp_node_t node,void * arg)387 error_sumup_cb(hp_node_t node, void *arg)
388 {
389 	error_sum_cb_arg_t *sumarg = (error_sum_cb_arg_t *)arg;
390 	char **table = sumarg->table;
391 	char *format = sumarg->format;
392 
393 	/* Only process USAGE nodes */
394 	if (hp_type(node) != HP_NODE_USAGE)
395 		return (HP_WALK_CONTINUE);
396 
397 	(void) strcat(*table, "\n");
398 	(void) sprintf(&((*table)[strlen(*table)]),
399 	    format, hp_name(node), hp_usage(node));
400 
401 	return (HP_WALK_CONTINUE);
402 }
403 
404 /*
405  * Takes an opaque rcm_info_t pointer and a character pointer, and appends
406  * the rcm_info_t data in the form of a table to the given character pointer.
407  */
408 static void
pci_rcm_info_table(hp_node_t node,char ** table)409 pci_rcm_info_table(hp_node_t node, char **table)
410 {
411 	int i;
412 	size_t w;
413 	size_t width = 0;
414 	size_t w_rsrc = 0;
415 	size_t w_info = 0;
416 	size_t table_size = 0;
417 	uint_t tuples = 0;
418 	char *rsrc;
419 	char *info;
420 	char *newtable;
421 	static char format[MAX_FORMAT];
422 	const char *infostr;
423 	error_size_cb_arg_t sizearg;
424 	error_sum_cb_arg_t sumarg;
425 
426 	/* Protect against invalid arguments */
427 	if (table == NULL)
428 		return;
429 
430 	/* Set localized table header strings */
431 	rsrc = dgettext(TEXT_DOMAIN, "Resource");
432 	info = dgettext(TEXT_DOMAIN, "Information");
433 
434 	/* A first pass, to size up the RCM information */
435 	sizearg.rsrc_width = strlen(rsrc);
436 	sizearg.info_width = strlen(info);
437 	sizearg.cnt = 0;
438 	(void) hp_traverse(node, &sizearg, error_sizeup_cb);
439 
440 	/* If nothing was sized up above, stop early */
441 	if (sizearg.cnt == 0)
442 		return;
443 
444 	w_rsrc = sizearg.rsrc_width;
445 	w_info = sizearg.info_width;
446 	tuples = sizearg.cnt;
447 
448 	/* Adjust column widths for column headings */
449 	if ((w = strlen(rsrc)) > w_rsrc)
450 		w_rsrc = w;
451 	else if ((w_rsrc - w) % 2)
452 		w_rsrc++;
453 	if ((w = strlen(info)) > w_info)
454 		w_info = w;
455 	else if ((w_info - w) % 2)
456 		w_info++;
457 
458 	/*
459 	 * Compute the total line width of each line,
460 	 * accounting for intercolumn spacing.
461 	 */
462 	width = w_info + w_rsrc + 4;
463 
464 	/* Allocate space for the table */
465 	table_size = (2 + tuples) * (width + 1) + 2;
466 	if (*table == NULL) {
467 		/* zero fill for the strcat() call below */
468 		*table = calloc(table_size, sizeof (char));
469 		if (*table == NULL)
470 			return;
471 	} else {
472 		newtable = realloc(*table, strlen(*table) + table_size);
473 		if (newtable == NULL)
474 			return;
475 		else
476 			*table = newtable;
477 	}
478 
479 	/* Place a table header into the string */
480 
481 	/* The resource header */
482 	(void) strcat(*table, "\n");
483 	w = strlen(rsrc);
484 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
485 		(void) strcat(*table, " ");
486 	(void) strcat(*table, rsrc);
487 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
488 		(void) strcat(*table, " ");
489 
490 	/* The information header */
491 	(void) strcat(*table, "  ");
492 	w = strlen(info);
493 	for (i = 0; i < ((w_info - w) / 2); i++)
494 		(void) strcat(*table, " ");
495 	(void) strcat(*table, info);
496 	for (i = 0; i < ((w_info - w) / 2); i++)
497 		(void) strcat(*table, " ");
498 	/* Underline the headers */
499 	(void) strcat(*table, "\n");
500 	for (i = 0; i < w_rsrc; i++)
501 		(void) strcat(*table, "-");
502 	(void) strcat(*table, "  ");
503 	for (i = 0; i < w_info; i++)
504 		(void) strcat(*table, "-");
505 
506 	/* Construct the format string */
507 	(void) snprintf(format, MAX_FORMAT, "%%-%ds  %%-%ds",
508 	    (int)w_rsrc, (int)w_info);
509 
510 	/* Add the tuples to the table string */
511 	sumarg.table = table;
512 	sumarg.format = format;
513 	(void) hp_traverse(node, &sumarg, error_sumup_cb);
514 }
515 
516 /*
517  * Figure out the target kernel state for a given cfgadm
518  * change-state operation.
519  */
520 static cfga_err_t
cfga_target_state(cfga_cmd_t state_change_cmd,int * state)521 cfga_target_state(cfga_cmd_t state_change_cmd, int *state)
522 {
523 	switch (state_change_cmd) {
524 	case CFGA_CMD_CONNECT:
525 		*state = DDI_HP_CN_STATE_POWERED;
526 		break;
527 	case CFGA_CMD_DISCONNECT:
528 		*state = DDI_HP_CN_STATE_PRESENT;
529 		break;
530 	case CFGA_CMD_CONFIGURE:
531 		*state = DDI_HP_CN_STATE_ENABLED;
532 		break;
533 	case CFGA_CMD_UNCONFIGURE:
534 		*state = DDI_HP_CN_STATE_POWERED;
535 		break;
536 	default:
537 		return (CFGA_ERROR);
538 	}
539 
540 	return (CFGA_OK);
541 }
542 
543 /*
544  * Translate kernel state to cfgadm receptacle state and occupant state.
545  */
546 static cfga_err_t
cfga_get_state(hp_node_t connector,ap_rstate_t * rs,ap_ostate_t * os)547 cfga_get_state(hp_node_t connector, ap_rstate_t *rs, ap_ostate_t *os)
548 {
549 	int state;
550 	hp_node_t port;
551 
552 	state = hp_state(connector);
553 
554 	/* Receptacle state */
555 	switch (state) {
556 	case DDI_HP_CN_STATE_EMPTY:
557 		*rs = AP_RSTATE_EMPTY;
558 		break;
559 	case DDI_HP_CN_STATE_PRESENT:
560 		*rs = AP_RSTATE_DISCONNECTED;
561 		break;
562 	case DDI_HP_CN_STATE_POWERED:
563 	case DDI_HP_CN_STATE_ENABLED:
564 		*rs = AP_RSTATE_CONNECTED;
565 		break;
566 		/*
567 		 * Connector state can only be one of
568 		 * Empty, Present, Powered, Enabled.
569 		 */
570 	default:
571 		return (CFGA_ERROR);
572 	}
573 
574 	/*
575 	 * Occupant state
576 	 */
577 	port = hp_child(connector);
578 	while (port != NULL) {
579 		DBG(1, ("cfga_get_state:(%x)\n", hp_state(port)));
580 
581 		/*
582 		 * Mark occupant state as "configured" if at least one of the
583 		 * associated ports is at state "offline" or above. Driver
584 		 * attach ("online" state) is not necessary here.
585 		 */
586 		if (hp_state(port) >= DDI_HP_CN_STATE_OFFLINE)
587 			break;
588 
589 		port = hp_sibling(port);
590 	}
591 
592 	if (port != NULL)
593 		*os = AP_OSTATE_CONFIGURED;
594 	else
595 		*os = AP_OSTATE_UNCONFIGURED;
596 
597 	return (CFGA_OK);
598 }
599 
600 /*
601  * Transitional Diagram:
602  *
603  *  empty		unconfigure
604  * (remove)	^|  (physically insert card)
605  *			|V
606  * disconnect	configure
607  * "-c DISCONNECT"	^|	"-c CONNECT"
608  *				|V	"-c CONFIGURE"
609  * connect	unconfigure	->	connect    configure
610  *						<-
611  *					"-c UNCONFIGURE"
612  *
613  */
614 /*ARGSUSED*/
615 cfga_err_t
cfga_change_state(cfga_cmd_t state_change_cmd,const char * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)616 cfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
617     const char *options, struct cfga_confirm *confp,
618     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
619 {
620 	int		rv, state, new_state;
621 	uint_t		hpflags = 0;
622 	hp_node_t	node;
623 	hp_node_t	results = NULL;
624 
625 	if ((rv = check_options(options)) != CFGA_OK) {
626 		return (rv);
627 	}
628 
629 	if (errstring != NULL)
630 		*errstring = NULL;
631 
632 	rv = CFGA_OK;
633 	DBG(1, ("cfga_change_state:(%s)\n", ap_id));
634 
635 	rv = physpath2node(ap_id, errstring, &node);
636 	if (rv != CFGA_OK)
637 		return (rv);
638 
639 	/*
640 	 * Check for the FORCE flag.  It is only used
641 	 * for DISCONNECT or UNCONFIGURE state changes.
642 	 */
643 	if (flags & CFGA_FLAG_FORCE)
644 		hpflags |= HPFORCE;
645 
646 	state = hp_state(node);
647 
648 	/*
649 	 * Which state should we drive to ?
650 	 */
651 	if ((state_change_cmd != CFGA_CMD_LOAD) &&
652 	    (state_change_cmd != CFGA_CMD_UNLOAD)) {
653 		if (cfga_target_state(state_change_cmd,
654 		    &new_state) != CFGA_OK) {
655 			hp_fini(node);
656 			return (CFGA_ERROR);
657 		}
658 	}
659 
660 	DBG(1, ("cfga_change_state: state is %d\n", state));
661 	switch (state_change_cmd) {
662 	case CFGA_CMD_CONNECT:
663 		DBG(1, ("connect\n"));
664 		if (state == DDI_HP_CN_STATE_EMPTY) {
665 			cfga_err(errstring, ERR_AP_ERR, 0);
666 			rv = CFGA_INVAL;
667 		} else if (state == DDI_HP_CN_STATE_PRESENT) {
668 			/* Connect the slot */
669 			if (hp_set_state(node, 0, new_state, &results) != 0) {
670 				rv = CFGA_ERROR;
671 				cfga_err(errstring, CMD_SLOT_CONNECT, 0);
672 			}
673 		}
674 		break;
675 
676 	case CFGA_CMD_DISCONNECT:
677 		DBG(1, ("disconnect\n"));
678 		if (state == DDI_HP_CN_STATE_EMPTY) {
679 			cfga_err(errstring, ERR_AP_ERR, 0);
680 			rv = CFGA_INVAL;
681 		} else if (state > DDI_HP_CN_STATE_PRESENT) {
682 			/* Disconnect the slot */
683 			rv = hp_set_state(node, hpflags, new_state, &results);
684 			if (rv != 0) {
685 				if (rv == EBUSY)
686 					rv = CFGA_BUSY;
687 				else
688 					rv = CFGA_ERROR;
689 
690 				if (results) {
691 					pci_rcm_info_table(results, errstring);
692 					hp_fini(results);
693 				} else {
694 					cfga_err(errstring,
695 					    CMD_SLOT_DISCONNECT, 0);
696 				}
697 			}
698 		}
699 		break;
700 
701 	case CFGA_CMD_CONFIGURE:
702 		/*
703 		 * for multi-func device we allow multiple
704 		 * configure on the same slot because one
705 		 * func can be configured and other one won't
706 		 */
707 		DBG(1, ("configure\n"));
708 		if (state == DDI_HP_CN_STATE_EMPTY) {
709 			cfga_err(errstring, ERR_AP_ERR, 0);
710 			rv = CFGA_INVAL;
711 		} else if (hp_set_state(node, 0, new_state, &results) != 0) {
712 			rv = CFGA_ERROR;
713 			cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
714 		}
715 		break;
716 
717 	case CFGA_CMD_UNCONFIGURE:
718 		DBG(1, ("unconfigure\n"));
719 		if (state == DDI_HP_CN_STATE_EMPTY) {
720 			cfga_err(errstring, ERR_AP_ERR, 0);
721 			rv = CFGA_INVAL;
722 		} else if (state >= DDI_HP_CN_STATE_ENABLED) {
723 			rv = hp_set_state(node, hpflags, new_state, &results);
724 			if (rv != 0) {
725 				if (rv == EBUSY)
726 					rv = CFGA_BUSY;
727 				else
728 					rv = CFGA_ERROR;
729 
730 				if (results) {
731 					pci_rcm_info_table(results, errstring);
732 					hp_fini(results);
733 				} else {
734 					cfga_err(errstring,
735 					    CMD_SLOT_UNCONFIGURE, 0);
736 				}
737 			}
738 		}
739 		DBG(1, ("unconfigure rv:(%i)\n", rv));
740 		break;
741 
742 	case CFGA_CMD_LOAD:
743 		/* do nothing, just produce error msg as is */
744 		if (state < DDI_HP_CN_STATE_POWERED) {
745 			rv = CFGA_ERROR;
746 			cfga_err(errstring, CMD_SLOT_INSERT, 0);
747 		} else {
748 			cfga_err(errstring, ERR_AP_ERR, 0);
749 			rv = CFGA_INVAL;
750 		}
751 		break;
752 
753 	case CFGA_CMD_UNLOAD:
754 		/* do nothing, just produce error msg as is */
755 		if (state < DDI_HP_CN_STATE_POWERED) {
756 			rv = CFGA_ERROR;
757 			cfga_err(errstring, CMD_SLOT_REMOVE, 0);
758 		} else {
759 			cfga_err(errstring, ERR_AP_ERR, 0);
760 			rv = CFGA_INVAL;
761 		}
762 		break;
763 
764 	default:
765 		rv = CFGA_OPNOTSUPP;
766 		break;
767 	}
768 
769 	hp_fini(node);
770 	return (rv);
771 }
772 
773 char *
get_val_from_result(char * result)774 get_val_from_result(char *result)
775 {
776 	char *tmp;
777 
778 	tmp = strchr(result, '=');
779 	if (tmp == NULL)
780 		return (NULL);
781 
782 	tmp++;
783 	return (tmp);
784 }
785 
786 static cfga_err_t
prt_led_mode(const char * ap_id,int repeat,char ** errstring,struct cfga_msg * msgp)787 prt_led_mode(const char *ap_id, int repeat, char **errstring,
788     struct cfga_msg *msgp)
789 {
790 	pciehpc_led_t led;
791 	hp_node_t node;
792 	char *buff;
793 	char *buf;
794 	char *cp, line[MAXLINE];
795 	char *tmp;
796 	char *format;
797 	char *result;
798 	int i, n, rv;
799 	int len = MAXLINE;
800 
801 	pciehpc_led_t states[] = {
802 		PCIEHPC_POWER_LED,
803 		PCIEHPC_FAULT_LED,
804 		PCIEHPC_ATTN_LED,
805 		PCIEHPC_ACTIVE_LED
806 	};
807 
808 	DBG(1, ("prt_led_mod function\n"));
809 	if (!repeat)
810 		cfga_msg(msgp, "Ap_Id\t\t\tLed");
811 
812 	rv = physpath2node(ap_id, errstring, &node);
813 	if (rv != CFGA_OK)
814 		return (rv);
815 
816 	if ((buff = malloc(MAXPATHLEN)) == NULL) {
817 		hp_fini(node);
818 		cfga_err(errstring, "malloc ", 0);
819 		return (CFGA_ERROR);
820 	}
821 
822 	(void) memset(buff, 0, MAXPATHLEN);
823 
824 	if (fix_ap_name(buff, ap_id, hp_name(node),
825 	    errstring) != CFGA_OK) {
826 		hp_fini(node);
827 		free(buff);
828 		return (CFGA_ERROR);
829 	}
830 
831 	cp = line;
832 	(void) snprintf(cp, len, "%s\t\t", buff);
833 	len -= strlen(cp);
834 	cp += strlen(cp);
835 
836 	free(buff);
837 
838 	n = sizeof (states)/sizeof (pciehpc_led_t);
839 	for (i = 0; i < n; i++) {
840 		led = states[i];
841 
842 		format = (i == n - 1) ? "%s=%s" : "%s=%s,";
843 		if (hp_get_private(node, led_strs2[led], &result) != 0) {
844 			(void) snprintf(cp, len, format,
845 			    led_strs[led], cfga_strs[UNKNOWN]);
846 			len -= strlen(cp);
847 			cp += strlen(cp);
848 			DBG(1, ("%s:%s\n", led_strs[led], cfga_strs[UNKNOWN]));
849 		} else {
850 			/*
851 			 * hp_get_private() will return back things like
852 			 * "led_fault=off", transform it to cfgadm desired
853 			 * format.
854 			 */
855 			tmp = get_val_from_result(result);
856 			if (tmp == NULL) {
857 				free(result);
858 				hp_fini(node);
859 				return (CFGA_ERROR);
860 			}
861 
862 			(void) snprintf(cp, len, format,
863 			    led_strs[led], tmp);
864 			len -= strlen(cp);
865 			cp += strlen(cp);
866 			DBG(1, ("%s:%s\n", led_strs[led], tmp));
867 			free(result);
868 		}
869 	}
870 
871 	cfga_msg(msgp, line);	/* print the message */
872 
873 	hp_fini(node);
874 
875 	return (CFGA_OK);
876 }
877 
878 /*ARGSUSED*/
879 cfga_err_t
cfga_private_func(const char * function,const char * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)880 cfga_private_func(const char *function, const char *ap_id,
881     const char *options, struct cfga_confirm *confp,
882     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
883 {
884 	char *str;
885 	int   len, fd, i = 0, repeat = 0;
886 	char buf[MAXNAMELEN];
887 	char ptr;
888 	cfga_err_t rv;
889 	char *led, *mode;
890 	hp_node_t node;
891 	char *result;
892 
893 	DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
894 	DBG(2, ("  options: %s\n", (options == NULL)?"null":options));
895 	DBG(2, ("  confp: %x\n", confp));
896 	DBG(2, ("  cfga_msg: %x\n", cfga_msg));
897 	DBG(2, ("  flag: %d\n", flags));
898 
899 	if ((rv = check_options(options)) != CFGA_OK) {
900 		return (rv);
901 	}
902 
903 	if (private_check == confp)
904 		repeat = 1;
905 	else
906 		private_check = (void*)confp;
907 
908 	for (i = 0, str = func_strs[i], len = strlen(str);
909 	    func_strs[i] != NULL; i++) {
910 		str = func_strs[i];
911 		len = strlen(str);
912 		if (strncmp(function, str, len) == 0)
913 			break;
914 	}
915 
916 	switch (i) {
917 		case ENABLE_SLOT:
918 		case DISABLE_SLOT:
919 			/* pass through */
920 		case ENABLE_AUTOCNF:
921 		case DISABLE_AUTOCNF:
922 			/* no action needed */
923 			return (CFGA_OK);
924 			break;
925 		case LED:
926 			/* set mode */
927 			ptr = function[len++];
928 			if (ptr == '=') {
929 				str = (char *)function;
930 				for (str = (str+len++), i = 0; *str != ',';
931 				    i++, str++) {
932 					if (i == (MAXNAMELEN - 1))
933 						break;
934 
935 					buf[i] = *str;
936 					DBG_F(2, (stdout, "%c\n", buf[i]));
937 				}
938 				buf[i] = '\0'; str++;
939 				DBG(2, ("buf = %s\n", buf));
940 
941 				/* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
942 				if (strcmp(buf, led_strs[POWER]) == 0)
943 					led = PCIEHPC_PROP_LED_POWER;
944 				else if (strcmp(buf, led_strs[FAULT]) == 0)
945 					led = PCIEHPC_PROP_LED_FAULT;
946 				else if (strcmp(buf, led_strs[ATTN]) == 0)
947 					led = PCIEHPC_PROP_LED_ATTN;
948 				else if (strcmp(buf, led_strs[ACTIVE]) == 0)
949 					led = PCIEHPC_PROP_LED_ACTIVE;
950 				else return (CFGA_INVAL);
951 
952 				len = strlen(func_strs[MODE]);
953 				if ((strncmp(str, func_strs[MODE], len) == 0) &&
954 				    (*(str+(len)) == '=')) {
955 					for (str = (str+(++len)), i = 0;
956 					    *str != '\0'; i++, str++) {
957 						buf[i] = *str;
958 					}
959 				}
960 				buf[i] = '\0';
961 				DBG(2, ("buf_mode= %s\n", buf));
962 
963 				/* ON = 1, OFF = 0 */
964 				if (strcmp(buf, mode_strs[ON]) == 0)
965 					mode = PCIEHPC_PROP_VALUE_ON;
966 				else if (strcmp(buf, mode_strs[OFF]) == 0)
967 					mode = PCIEHPC_PROP_VALUE_OFF;
968 				else if (strcmp(buf, mode_strs[BLINK]) == 0)
969 					mode = PCIEHPC_PROP_VALUE_BLINK;
970 				else if (strcmp(buf, mode_strs[DEFAULT]) == 0)
971 					mode = PCIEHPC_PROP_VALUE_DEFAULT;
972 				else return (CFGA_INVAL);
973 
974 				/* sendin  */
975 				memset(buf, 0, sizeof (buf));
976 				snprintf(buf, sizeof (buf), "%s=%s",
977 				    led, mode);
978 				buf[MAXNAMELEN - 1] = '\0';
979 
980 				break;
981 			} else if (ptr == '\0') {
982 				/* print mode */
983 				DBG(1, ("Print mode\n"));
984 				return (prt_led_mode(ap_id, repeat, errstring,
985 				    msgp));
986 			}
987 			/* FALLTHROUGH */
988 		default:
989 			DBG(1, ("default\n"));
990 			errno = EINVAL;
991 			return (CFGA_INVAL);
992 	}
993 
994 	rv = physpath2node(ap_id, errstring, &node);
995 	if (rv != CFGA_OK)
996 		return (rv);
997 
998 	if (hp_set_private(node, buf, &result) != 0) {
999 		hp_fini(node);
1000 		return (CFGA_ERROR);
1001 	}
1002 
1003 	hp_fini(node);
1004 	return (CFGA_OK);
1005 }
1006 
1007 /*ARGSUSED*/
cfga_test(const char * ap_id,const char * options,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)1008 cfga_err_t cfga_test(const char *ap_id, const char *options,
1009     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1010 {
1011 	cfga_err_t rv;
1012 	if (errstring != NULL)
1013 		*errstring = NULL;
1014 
1015 	if ((rv = check_options(options)) != CFGA_OK) {
1016 		return (rv);
1017 	}
1018 
1019 	DBG(1, ("cfga_test:(%s)\n", ap_id));
1020 	/* will need to implement pci CTRL command */
1021 	return (CFGA_NOTSUPP);
1022 }
1023 
1024 /*
1025  * The slot-names property describes the external labeling of add-in slots.
1026  * This property is an encoded array, an integer followed by a list of
1027  * strings. The return value from di_prop_lookup_ints for slot-names is -1.
1028  * The expected return value should be the number of elements.
1029  * Di_prop_decode_common does not decode encoded data from software,
1030  * such as the solaris device tree, unlike from the prom.
1031  * Di_prop_decode_common takes the size of the encoded data and mods
1032  * it with the size of int. The size of the encoded data for slot-names is 9
1033  * and the size of int is 4, yielding a non zero result. A value of -1 is used
1034  * to indicate that the number of elements can not be determined.
1035  * Di_prop_decode_common can be modified to decode encoded data from the solaris
1036  * device tree.
1037  */
1038 static int
fixup_slotname(int rval,int * intp,struct searcharg * slotarg)1039 fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
1040 {
1041 	if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
1042 		return (DI_WALK_TERMINATE);
1043 	} else {
1044 		int i;
1045 		char *tmptr = (char *)(intp+1);
1046 		DBG(1, ("slot-bitmask: %x \n", *intp));
1047 
1048 		rval = (rval -1) * 4;
1049 
1050 		for (i = 0; i <= slotarg->minor; i++) {
1051 			DBG(2, ("curr slot-name: %s \n", tmptr));
1052 
1053 			if (i >= MAXDEVS)
1054 				return (DI_WALK_TERMINATE);
1055 
1056 			if ((*intp >> i) & 1) {
1057 				/* assign tmptr */
1058 				DBG(2, ("slot-name: %s \n", tmptr));
1059 				if (i == slotarg->minor)
1060 					(void) strcpy(slotarg->slotnames[i],
1061 					    tmptr);
1062 				/* wind tmptr to next \0 */
1063 				while (*tmptr != '\0') {
1064 					tmptr++;
1065 				}
1066 				tmptr++;
1067 			} else {
1068 				/* point at unknown string */
1069 				if (i == slotarg->minor)
1070 					(void) strcpy(slotarg->slotnames[i],
1071 					    "unknown");
1072 			}
1073 		}
1074 	}
1075 	return (DI_WALK_TERMINATE);
1076 }
1077 
1078 static int
find_slotname(di_node_t din,di_minor_t dim,void * arg)1079 find_slotname(di_node_t din, di_minor_t dim, void *arg)
1080 {
1081 	struct searcharg *slotarg = (struct searcharg *)arg;
1082 	di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
1083 	di_prom_prop_t	prom_prop;
1084 	di_prop_t	solaris_prop;
1085 	int *intp, rval;
1086 	char *devname;
1087 	char fulldevname[MAXNAMELEN];
1088 
1089 	slotarg->minor = dim->dev_minor % 256;
1090 
1091 	DBG(2, ("minor number:(%i)\n", slotarg->minor));
1092 	DBG(2, ("hot plug slots found so far:(%i)\n", 0));
1093 
1094 	if ((devname = di_devfs_path(din)) != NULL) {
1095 		(void) snprintf(fulldevname, MAXNAMELEN,
1096 		    "/devices%s:%s", devname, di_minor_name(dim));
1097 		di_devfs_path_free(devname);
1098 	}
1099 
1100 	if (strcmp(fulldevname, slotarg->devpath) == 0) {
1101 
1102 		/*
1103 		 * Check the Solaris device tree first
1104 		 * in the case of a DR operation
1105 		 */
1106 		solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
1107 		while (solaris_prop != DI_PROP_NIL) {
1108 			if (strcmp("slot-names", di_prop_name(solaris_prop))
1109 			    == 0) {
1110 				rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1111 				    din, di_prop_name(solaris_prop), &intp);
1112 				slotarg->slt_name_src = SOLARIS_SLT_NAME;
1113 
1114 				return (fixup_slotname(rval, intp, slotarg));
1115 			}
1116 			solaris_prop = di_prop_hw_next(din, solaris_prop);
1117 		}
1118 
1119 		/*
1120 		 * Check the prom device tree which is populated at boot.
1121 		 * If this fails, give up and set the slot name to null.
1122 		 */
1123 		prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
1124 		while (prom_prop != DI_PROM_PROP_NIL) {
1125 			if (strcmp("slot-names", di_prom_prop_name(prom_prop))
1126 			    == 0) {
1127 				rval = di_prom_prop_lookup_ints(ph,
1128 				    din, di_prom_prop_name(prom_prop), &intp);
1129 				slotarg->slt_name_src = PROM_SLT_NAME;
1130 
1131 				return (fixup_slotname(rval, intp, slotarg));
1132 			}
1133 			prom_prop = di_prom_prop_next(ph, din, prom_prop);
1134 		}
1135 		*slotarg->slotnames[slotarg->minor] = '\0';
1136 		return (DI_WALK_TERMINATE);
1137 	} else
1138 		return (DI_WALK_CONTINUE);
1139 }
1140 
1141 static int
find_physical_slot_names(const char * devcomp,struct searcharg * slotarg)1142 find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
1143 {
1144 	di_node_t root_node;
1145 
1146 	DBG(1, ("find_physical_slot_names\n"));
1147 
1148 	if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
1149 	    == DI_NODE_NIL) {
1150 		DBG(1, ("di_init() failed\n"));
1151 		return (-1);
1152 	}
1153 
1154 	slotarg->devpath = (char *)devcomp;
1155 
1156 	if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1157 		DBG(1, ("di_prom_init() failed\n"));
1158 		di_fini(root_node);
1159 		return (-1);
1160 	}
1161 
1162 	(void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
1163 	    0, (void *)slotarg, find_slotname);
1164 
1165 	di_prom_fini(slotarg->promp);
1166 	di_fini(root_node);
1167 	if (*slotarg->slotnames[0] != '\0')
1168 		return (0);
1169 	else
1170 		return (-1);
1171 }
1172 
1173 static void
get_type(const char * boardtype,const char * cardtype,char * buf)1174 get_type(const char *boardtype, const char *cardtype, char *buf)
1175 {
1176 /* for type string assembly in get_type() */
1177 #define	TPCT(s)	(void) strlcat(buf, (s), CFGA_TYPE_LEN)
1178 
1179 	int i;
1180 
1181 	if (strcmp(cardtype, "unknown") == 0) {
1182 		TPCT("unknown");
1183 		return;
1184 	}
1185 
1186 	TPCT(cardtype);
1187 	TPCT("/");
1188 
1189 	if (strcmp(boardtype, PCIEHPC_PROP_VALUE_PCIHOTPLUG) == 0)
1190 		TPCT(board_strs[PCIEHPC_BOARD_PCI_HOTPLUG]);
1191 	else
1192 		TPCT(board_strs[PCIEHPC_BOARD_UNKNOWN]);
1193 }
1194 
1195 /*
1196  * call-back function for di_devlink_walk
1197  * if the link lives in /dev/cfg copy its name
1198  */
1199 static int
found_devlink(di_devlink_t link,void * ap_log_id)1200 found_devlink(di_devlink_t link, void *ap_log_id)
1201 {
1202 	if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
1203 		/* copy everything but /dev/cfg/ */
1204 		(void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
1205 		DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
1206 		return (DI_WALK_TERMINATE);
1207 	}
1208 	return (DI_WALK_CONTINUE);
1209 }
1210 
1211 /*
1212  * Walk throught the cached /dev link tree looking for links to the ap
1213  * if none are found return an error
1214  */
1215 static cfga_err_t
check_devlinks(char * ap_log_id,const char * ap_id)1216 check_devlinks(char *ap_log_id, const char *ap_id)
1217 {
1218 	di_devlink_handle_t hdl;
1219 
1220 	DBG(1, ("check_devlinks: %s\n", ap_id));
1221 
1222 	hdl = di_devlink_init(NULL, 0);
1223 
1224 	if (strncmp("/devices/", ap_id, 9) == 0) {
1225 		/* ap_id is a valid minor_path with /devices prepended */
1226 		(void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
1227 		    (void *)ap_log_id, found_devlink);
1228 	} else {
1229 		DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
1230 		return (CFGA_ERROR);
1231 	}
1232 
1233 	(void) di_devlink_fini(&hdl);
1234 
1235 	if (ap_log_id[0] != '\0')
1236 		return (CFGA_OK);
1237 	else
1238 		return (CFGA_ERROR);
1239 }
1240 
1241 /*
1242  * most of this is needed to compensate for
1243  * differences between various platforms
1244  */
1245 static cfga_err_t
fix_ap_name(char * ap_log_id,const char * ap_id,char * slot_name,char ** errstring)1246 fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
1247     char **errstring)
1248 {
1249 	char *buf;
1250 	char *tmp;
1251 	char *ptr;
1252 
1253 	di_node_t ap_node;
1254 
1255 	ap_log_id[0] = '\0';
1256 
1257 	if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
1258 		return (CFGA_OK);
1259 
1260 	DBG(1, ("fix_ap_name: %s\n", ap_id));
1261 
1262 	if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
1263 		DBG(1, ("malloc failed\n"));
1264 		return (CFGA_ERROR);
1265 	}
1266 	(void) strcpy(buf, ap_id);
1267 	tmp = buf + sizeof ("/devices") - 1;
1268 
1269 	ptr = strchr(tmp, ':');
1270 	ptr[0] = '\0';
1271 
1272 	DBG(1, ("fix_ap_name: %s\n", tmp));
1273 
1274 	ap_node = di_init(tmp, DINFOMINOR);
1275 	if (ap_node == DI_NODE_NIL) {
1276 		cfga_err(errstring, "di_init ", 0);
1277 		DBG(1, ("fix_ap_name: failed to snapshot node\n"));
1278 		return (CFGA_ERROR);
1279 	}
1280 
1281 	(void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
1282 	    di_driver_name(ap_node), di_instance(ap_node), slot_name);
1283 
1284 	DBG(1, ("fix_ap_name: %s\n", ap_log_id));
1285 
1286 	di_fini(ap_node);
1287 
1288 	free(buf);
1289 	return (CFGA_OK);
1290 }
1291 
1292 
1293 static int
findlink_cb(di_devlink_t devlink,void * arg)1294 findlink_cb(di_devlink_t devlink, void *arg)
1295 {
1296 	(*(char **)arg) = strdup(di_devlink_path(devlink));
1297 
1298 	return (DI_WALK_TERMINATE);
1299 }
1300 
1301 /*
1302  * returns an allocated string containing the full path to the devlink for
1303  * <ap_phys_id> in the devlink database; we expect only one devlink per
1304  * <ap_phys_id> so we return the first encountered
1305  */
1306 static char *
findlink(char * ap_phys_id)1307 findlink(char *ap_phys_id)
1308 {
1309 	di_devlink_handle_t hdl;
1310 	char *path = NULL;
1311 
1312 	hdl = di_devlink_init(NULL, 0);
1313 
1314 	if (strncmp("/devices/", ap_phys_id, 9) == 0)
1315 		ap_phys_id += 8;
1316 
1317 	(void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
1318 	    (void *)&path, findlink_cb);
1319 
1320 	(void) di_devlink_fini(&hdl);
1321 	return (path);
1322 }
1323 
1324 
1325 /*
1326  * returns CFGA_OK if it can succesfully retrieve the devlink info associated
1327  * with devlink for <ap_phys_id> which will be returned through <ap_info>
1328  */
1329 cfga_err_t
get_dli(char * dlpath,char * ap_info,int ap_info_sz)1330 get_dli(char *dlpath, char *ap_info, int ap_info_sz)
1331 {
1332 	int fd;
1333 
1334 	fd = di_dli_openr(dlpath);
1335 	if (fd < 0)
1336 		return (CFGA_ERROR);
1337 
1338 	(void) read(fd, ap_info, ap_info_sz);
1339 	ap_info[ap_info_sz - 1] = '\0';
1340 
1341 	di_dli_close(fd);
1342 	return (CFGA_OK);
1343 }
1344 
1345 static cfga_err_t
cfga_get_condition(hp_node_t node,ap_condition_t * cond)1346 cfga_get_condition(hp_node_t node, ap_condition_t *cond)
1347 {
1348 	char *condition;
1349 	char *tmpc;
1350 	cfga_err_t ret = CFGA_OK;
1351 
1352 	/* "condition" bus specific commands */
1353 	if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
1354 	    &tmpc) != 0) {
1355 		*cond = AP_COND_UNKNOWN;
1356 		return (CFGA_ERROR);
1357 	}
1358 
1359 	condition = get_val_from_result(tmpc);
1360 
1361 	if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
1362 		*cond = AP_COND_OK;
1363 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
1364 		*cond = AP_COND_FAILING;
1365 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
1366 		*cond = AP_COND_FAILED;
1367 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
1368 		*cond = AP_COND_UNUSABLE;
1369 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
1370 		*cond = AP_COND_UNKNOWN;
1371 	else
1372 		ret = CFGA_ERROR;
1373 
1374 	free(tmpc);
1375 	return (ret);
1376 }
1377 
1378 /*ARGSUSED*/
1379 cfga_err_t
cfga_list_ext(const char * ap_id,cfga_list_data_t ** cs,int * nlist,const char * options,const char * listopts,char ** errstring,cfga_flags_t flags)1380 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1381     int *nlist, const char *options, const char *listopts, char **errstring,
1382     cfga_flags_t flags)
1383 {
1384 	char			*boardtype;
1385 	char			*cardtype;
1386 	char			*tmpb = NULL, *tmpc = NULL;
1387 	struct	searcharg	slotname_arg;
1388 	int			fd;
1389 	int			rv = CFGA_OK;
1390 	char			*dlpath = NULL;
1391 	hp_node_t		node;
1392 	ap_rstate_t		rs;
1393 	ap_ostate_t		os;
1394 	ap_condition_t		cond;
1395 
1396 	if ((rv = check_options(options)) != CFGA_OK) {
1397 		return (rv);
1398 	}
1399 
1400 	if (errstring != NULL)
1401 		*errstring = NULL;
1402 
1403 	DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1404 
1405 	if (cs == NULL || nlist == NULL) {
1406 		rv = CFGA_ERROR;
1407 		return (rv);
1408 	}
1409 
1410 	*nlist = 1;
1411 
1412 	if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1413 		cfga_err(errstring, "malloc ", 0);
1414 		DBG(1, ("malloc failed\n"));
1415 		rv = CFGA_ERROR;
1416 		return (rv);
1417 	}
1418 	(void) memset(*cs, 0, sizeof (cfga_list_data_t));
1419 
1420 	rv = physpath2node(ap_id, errstring, &node);
1421 	if (rv != CFGA_OK) {
1422 		DBG(1, ("physpath2node failed\n"));
1423 		return (rv);
1424 	}
1425 
1426 	if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
1427 		DBG(1, ("cfga_get_state failed\n"));
1428 		hp_fini(node);
1429 		return (CFGA_ERROR);
1430 	}
1431 
1432 	switch (rs) {
1433 		case AP_RSTATE_EMPTY:
1434 			(*cs)->ap_r_state = CFGA_STAT_EMPTY;
1435 			DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1436 			break;
1437 		case AP_RSTATE_DISCONNECTED:
1438 			(*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1439 			DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1440 			break;
1441 		case AP_RSTATE_CONNECTED:
1442 			(*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1443 			DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1444 			break;
1445 	default:
1446 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1447 		rv = CFGA_ERROR;
1448 		hp_fini(node);
1449 		return (rv);
1450 	}
1451 
1452 	switch (os) {
1453 		case AP_OSTATE_CONFIGURED:
1454 			(*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1455 			DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1456 			break;
1457 		case AP_OSTATE_UNCONFIGURED:
1458 			(*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1459 			DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1460 			break;
1461 	default:
1462 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1463 		rv = CFGA_ERROR;
1464 		hp_fini(node);
1465 		return (rv);
1466 	}
1467 
1468 	(void) cfga_get_condition(node, &cond);
1469 
1470 	switch (cond) {
1471 		case AP_COND_OK:
1472 			(*cs)->ap_cond = CFGA_COND_OK;
1473 			DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1474 			break;
1475 		case AP_COND_FAILING:
1476 			(*cs)->ap_cond = CFGA_COND_FAILING;
1477 			DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1478 			break;
1479 		case AP_COND_FAILED:
1480 			(*cs)->ap_cond = CFGA_COND_FAILED;
1481 			DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1482 			break;
1483 		case AP_COND_UNUSABLE:
1484 			(*cs)->ap_cond = CFGA_COND_UNUSABLE;
1485 			DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1486 			break;
1487 		case AP_COND_UNKNOWN:
1488 			(*cs)->ap_cond = CFGA_COND_UNKNOWN;
1489 			DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1490 			break;
1491 	default:
1492 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1493 		rv = CFGA_ERROR;
1494 		hp_fini(node);
1495 		return (rv);
1496 	}
1497 	/*
1498 	 * We're not busy since the entrance into the kernel has been
1499 	 * sync'ed via libhotplug.
1500 	 */
1501 	(*cs)->ap_busy = 0;
1502 
1503 	/* last change */
1504 	(*cs)->ap_status_time = hp_last_change(node);
1505 
1506 	/* board type */
1507 	if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &tmpb) != 0)
1508 		boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1509 	else
1510 		boardtype = get_val_from_result(tmpb);
1511 
1512 	/* card type */
1513 	if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &tmpc) != 0)
1514 		cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1515 	else
1516 		cardtype = get_val_from_result(tmpc);
1517 
1518 	/* logical ap_id */
1519 	rv = fix_ap_name((*cs)->ap_log_id, ap_id,
1520 	    hp_name(node), errstring);
1521 	DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
1522 	/* physical ap_id */
1523 	(void) strcpy((*cs)->ap_phys_id, ap_id);    /* physical path of AP */
1524 
1525 	/* information */
1526 	dlpath = findlink((*cs)->ap_phys_id);
1527 	if (dlpath != NULL) {
1528 		if (get_dli(dlpath, (*cs)->ap_info,
1529 		    sizeof ((*cs)->ap_info)) != CFGA_OK)
1530 			(*cs)->ap_info[0] = '\0';
1531 		free(dlpath);
1532 	}
1533 
1534 	if ((*cs)->ap_log_id[0] == '\0')
1535 		(void) strcpy((*cs)->ap_log_id, hp_name(node));
1536 
1537 	if ((*cs)->ap_info[0] == '\0') {
1538 		/* slot_names of bus node  */
1539 		memset(&slotname_arg, 0, sizeof (slotname_arg));
1540 		if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1541 			(void) strcpy((*cs)->ap_info,
1542 			    slotname_arg.slotnames[slotname_arg.minor]);
1543 	}
1544 
1545 	/* class_code/subclass/boardtype */
1546 	get_type(boardtype, cardtype, (*cs)->ap_type);
1547 
1548 	DBG(1, ("cfga_list_ext return success\n"));
1549 	rv = CFGA_OK;
1550 
1551 	free(tmpb);
1552 	free(tmpc);
1553 	hp_fini(node);
1554 	return (rv);
1555 }
1556 
1557 /*
1558  * This routine prints a single line of help message
1559  */
1560 static void
cfga_msg(struct cfga_msg * msgp,const char * str)1561 cfga_msg(struct cfga_msg *msgp, const char *str)
1562 {
1563 	DBG(2, ("<%s>", str));
1564 
1565 	if (msgp == NULL || msgp->message_routine == NULL)
1566 		return;
1567 
1568 	(*msgp->message_routine)(msgp->appdata_ptr, str);
1569 	(*msgp->message_routine)(msgp->appdata_ptr, "\n");
1570 }
1571 
1572 static cfga_err_t
check_options(const char * options)1573 check_options(const char *options)
1574 {
1575 	struct cfga_msg *msgp = NULL;
1576 
1577 	if (options) {
1578 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1579 		cfga_msg(msgp, options);
1580 		return (CFGA_INVAL);
1581 	}
1582 	return (CFGA_OK);
1583 }
1584 
1585 /*ARGSUSED*/
1586 cfga_err_t
cfga_help(struct cfga_msg * msgp,const char * options,cfga_flags_t flags)1587 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1588 {
1589 	if (options) {
1590 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1591 		cfga_msg(msgp, options);
1592 	}
1593 	DBG(1, ("cfga_help\n"));
1594 
1595 	cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1596 	cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1597 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1598 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1599 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1600 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1601 	cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1602 	return (CFGA_OK);
1603 }
1604 
1605 /*
1606  * cfga_err() accepts a variable number of message IDs and constructs
1607  * a corresponding error string which is returned via the errstring argument.
1608  * cfga_err() calls gettext() to internationalize proper messages.
1609  */
1610 static void
cfga_err(char ** errstring,...)1611 cfga_err(char **errstring, ...)
1612 {
1613 	int a;
1614 	int i;
1615 	int n;
1616 	int len;
1617 	int flen;
1618 	char *p;
1619 	char *q;
1620 	char *s[32];
1621 	char *failed;
1622 	va_list ap;
1623 
1624 	/*
1625 	 * If errstring is null it means user is not interested in getting
1626 	 * error status. So we don't do all the work
1627 	 */
1628 	if (errstring == NULL) {
1629 		return;
1630 	}
1631 	va_start(ap, errstring);
1632 
1633 	failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1634 	flen = strlen(failed);
1635 
1636 	for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1637 		switch (a) {
1638 		case CMD_GETSTAT:
1639 		case CMD_LIST:
1640 		case CMD_SLOT_CONNECT:
1641 		case CMD_SLOT_DISCONNECT:
1642 		case CMD_SLOT_CONFIGURE:
1643 		case CMD_SLOT_UNCONFIGURE:
1644 			p =  cfga_errstrs(a);
1645 			len += (strlen(p) + flen);
1646 			s[n] = p;
1647 			s[++n] = cfga_strs[FAILED];
1648 
1649 			DBG(2, ("<%s>", p));
1650 			DBG(2, (cfga_strs[FAILED]));
1651 			break;
1652 
1653 		case ERR_CMD_INVAL:
1654 		case ERR_AP_INVAL:
1655 		case ERR_OPT_INVAL:
1656 		case ERR_AP_ERR:
1657 			switch (a) {
1658 			case ERR_CMD_INVAL:
1659 				p = dgettext(TEXT_DOMAIN,
1660 				    cfga_errstrs[ERR_CMD_INVAL]);
1661 				break;
1662 			case ERR_AP_INVAL:
1663 				p = dgettext(TEXT_DOMAIN,
1664 				    cfga_errstrs[ERR_AP_INVAL]);
1665 				break;
1666 			case ERR_OPT_INVAL:
1667 				p = dgettext(TEXT_DOMAIN,
1668 				    cfga_errstrs[ERR_OPT_INVAL]);
1669 				break;
1670 			case ERR_AP_ERR:
1671 				p = dgettext(TEXT_DOMAIN,
1672 				    cfga_errstrs[ERR_AP_ERR]);
1673 				break;
1674 			}
1675 
1676 			if ((q = va_arg(ap, char *)) != NULL) {
1677 				len += (strlen(p) + strlen(q));
1678 				s[n] = p;
1679 				s[++n] = q;
1680 				DBG(2, ("<%s>", p));
1681 				DBG(2, ("<%s>", q));
1682 				break;
1683 			} else {
1684 				len += strlen(p);
1685 				s[n] = p;
1686 
1687 			}
1688 			DBG(2, ("<%s>", p));
1689 			break;
1690 
1691 		default:
1692 			n--;
1693 			break;
1694 		}
1695 	}
1696 
1697 	DBG(2, ("\n"));
1698 	va_end(ap);
1699 
1700 	if ((p = calloc(len + 1, 1)) == NULL)
1701 		return;
1702 
1703 	for (i = 0; i < n; i++) {
1704 		(void) strlcat(p, s[i], len + 1);
1705 		DBG(2, ("i:%d, %s\n", i, s[i]));
1706 	}
1707 
1708 	*errstring = p;
1709 	DBG(2, ("%s\n", *errstring));
1710 }
1711 
1712 /*
1713  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
1714  */
1715