dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / cfgadm_plugins / shp / common / shp.c
blob8bd0df8c36cab2d889aeddd428c4aecb6d6080fe
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
28 * Plugin library for PCI Express and PCI (SHPC) hotplug controller
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>
48 #define CFGA_PLUGIN_LIB
50 #include <config_admin.h>
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>
59 #include <dirent.h>
60 #include <limits.h>
61 #include <sys/mkdev.h>
62 #include "../../../../../../include/sys/hotplug/pci/pcie_hp.h"
63 #include "../../../../common/pci/pci_strings.h"
64 #include <libhotplug.h>
66 extern const struct pci_class_strings_s class_pci[];
67 extern int class_pci_items;
69 #define MSG_HOTPLUG_DISABLED \
70 "Error: hotplug service is probably not running, " \
71 "please use 'svcadm enable hotplug' to enable the service. " \
72 "See cfgadm_shp(1M) for more details."
74 #define DEVICES_DIR "/devices"
75 #define SLASH "/"
76 #define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
79 * Set the version number
81 int cfga_version = CFGA_HSL_V2;
83 #ifdef DEBUG
84 #define SHP_DBG 1
85 #endif
87 #if !defined(TEXT_DOMAIN)
88 #define TEXT_DOMAIN "SYS_TEST"
89 #endif
92 * DEBUGING LEVEL
94 * External routines: 1 - 2
95 * Internal routines: 3 - 4
97 #ifdef SHP_DBG
98 int shp_debug = 1;
99 #define DBG(level, args) \
100 { if (shp_debug >= (level)) printf args; }
101 #define DBG_F(level, args) \
102 { if (shp_debug >= (level)) fprintf args; }
103 #else
104 #define DBG(level, args) /* nothing */
105 #define DBG_F(level, args) /* nothing */
106 #endif
108 #define CMD_ACQUIRE 0
109 #define CMD_GETSTAT 1
110 #define CMD_LIST 2
111 #define CMD_SLOT_CONNECT 3
112 #define CMD_SLOT_DISCONNECT 4
113 #define CMD_SLOT_CONFIGURE 5
114 #define CMD_SLOT_UNCONFIGURE 6
115 #define CMD_SLOT_INSERT 7
116 #define CMD_SLOT_REMOVE 8
117 #define CMD_OPEN 9
118 #define CMD_FSTAT 10
119 #define ERR_CMD_INVAL 11
120 #define ERR_AP_INVAL 12
121 #define ERR_AP_ERR 13
122 #define ERR_OPT_INVAL 14
124 static char *
125 cfga_errstrs[] = {
126 /* n */ "acquire ",
127 /* n */ "get-status ",
128 /* n */ "list ",
129 /* n */ "connect ",
130 /* n */ "disconnect ",
131 /* n */ "configure ",
132 /* n */ "unconfigure ",
133 /* n */ "insert ",
134 /* n */ "remove ",
135 /* n */ "open ",
136 /* n */ "fstat ",
137 /* y */ "invalid command ",
138 /* y */ "invalid attachment point ",
139 /* y */ "invalid transition ",
140 /* y */ "invalid option ",
141 NULL
144 #define HELP_HEADER 1
145 #define HELP_CONFIG 2
146 #define HELP_ENABLE_SLOT 3
147 #define HELP_DISABLE_SLOT 4
148 #define HELP_ENABLE_AUTOCONF 5
149 #define HELP_DISABLE_AUTOCONF 6
150 #define HELP_LED_CNTRL 7
151 #define HELP_UNKNOWN 8
152 #define SUCCESS 9
153 #define FAILED 10
154 #define UNKNOWN 11
156 #define MAXLINE 256
158 extern int errno;
160 static void cfga_err(char **errstring, ...);
161 static cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
162 char *slot_name, char **errstring);
163 static cfga_err_t check_options(const char *options);
164 static void cfga_msg(struct cfga_msg *msgp, const char *str);
165 static char *findlink(char *ap_phys_id);
167 static char *
168 cfga_strs[] = {
169 NULL,
170 "\nPCI hotplug specific commands:",
171 "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
172 "ap_id [ap_id...]",
173 "\t-x enable_slot ap_id [ap_id...]",
174 "\t-x disable_slot ap_id [ap_id...]",
175 "\t-x enable_autoconfig ap_id [ap_id...]",
176 "\t-x disable_autoconfig ap_id [ap_id...]",
177 "\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
178 "\tunknown command or option: ",
179 "success ",
180 "failed ",
181 "unknown",
182 NULL
185 #define MAX_FORMAT 80
187 #define ENABLE_SLOT 0
188 #define DISABLE_SLOT 1
189 #define ENABLE_AUTOCNF 2
190 #define DISABLE_AUTOCNF 3
191 #define LED 4
192 #define MODE 5
194 typedef enum { PCIEHPC_FAULT_LED, PCIEHPC_POWER_LED, PCIEHPC_ATTN_LED,
195 PCIEHPC_ACTIVE_LED} pciehpc_led_t;
197 typedef enum { PCIEHPC_BOARD_UNKNOWN, PCIEHPC_BOARD_PCI_HOTPLUG }
198 pciehpc_board_type_t;
201 * Board Type
203 static char *
204 board_strs[] = {
205 /* n */ "???", /* PCIEHPC_BOARD_UNKNOWN */
206 /* n */ "hp", /* PCIEHPC_BOARD_PCI_HOTPLUG */
207 /* n */ NULL
211 * HW functions
213 static char *
214 func_strs[] = {
215 /* n */ "enable_slot",
216 /* n */ "disable_slot",
217 /* n */ "enable_autoconfig",
218 /* n */ "disable_autoconfig",
219 /* n */ "led",
220 /* n */ "mode",
221 /* n */ NULL
225 * LED strings
227 static char *
228 led_strs[] = {
229 /* n */ "fault", /* PCIEHPC_FAULT_LED */
230 /* n */ "power", /* PCIEHPC_POWER_LED */
231 /* n */ "attn", /* PCIEHPC_ATTN_LED */
232 /* n */ "active", /* PCIEHPC_ACTIVE_LED */
233 /* n */ NULL
236 static char *
237 led_strs2[] = {
238 /* n */ PCIEHPC_PROP_LED_FAULT, /* PCIEHPC_FAULT_LED */
239 /* n */ PCIEHPC_PROP_LED_POWER, /* PCIEHPC_POWER_LED */
240 /* n */ PCIEHPC_PROP_LED_ATTN, /* PCIEHPC_ATTN_LED */
241 /* n */ PCIEHPC_PROP_LED_ACTIVE, /* PCIEHPC_ACTIVE_LED */
242 /* n */ NULL
245 #define FAULT 0
246 #define POWER 1
247 #define ATTN 2
248 #define ACTIVE 3
250 static char *
251 mode_strs[] = {
252 /* n */ "off", /* OFF */
253 /* n */ "on", /* ON */
254 /* n */ "blink", /* BLINK */
255 /* n */ NULL
258 #define OFF 0
259 #define ON 1
260 #define BLINK 2
262 #define cfga_errstrs(i) cfga_errstrs[(i)]
264 #define cfga_eid(a, b) (((a) << 8) + (b))
265 #define MAXDEVS 32
267 typedef enum {
268 SOLARIS_SLT_NAME,
269 PROM_SLT_NAME
270 } slt_name_src_t;
272 struct searcharg {
273 char *devpath;
274 char slotnames[MAXDEVS][MAXNAMELEN];
275 int minor;
276 di_prom_handle_t promp;
277 slt_name_src_t slt_name_src;
280 static void *private_check;
283 * Return the corresponding hp node for a given ap_id, it is the caller's
284 * responsibility to call hp_fini() to free the snapshot.
286 static cfga_err_t
287 physpath2node(const char *physpath, char **errstring, hp_node_t *nodep)
289 char *rpath;
290 char *cp;
291 hp_node_t node;
292 size_t len;
293 char *errmsg;
295 if (getuid() != 0 && geteuid() != 0)
296 return (CFGA_ERROR);
298 if ((rpath = malloc(strlen(physpath) + 1)) == NULL)
299 return (CFGA_ERROR);
301 (void) strcpy(rpath, physpath);
303 /* Remove devices prefix (if any) */
304 len = strlen(DEVICES_DIR);
305 if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
306 (void) memmove(rpath, rpath + len,
307 strlen(rpath + len) + 1);
310 /* Remove dynamic component if any */
311 if ((cp = GET_DYN(rpath)) != NULL) {
312 *cp = '\0';
315 /* Remove minor name (if any) */
316 if ((cp = strrchr(rpath, ':')) == NULL) {
317 free(rpath);
318 return (CFGA_INVAL);
321 *cp = '\0';
322 cp++;
324 DBG(1, ("rpath=%s,cp=%s\n", rpath, cp));
325 if ((node = hp_init(rpath, cp, 0)) == NULL) {
326 if (errno == EBADF) {
327 /* No reponse to operations on the door file. */
328 assert(errstring != NULL);
329 *errstring = strdup(MSG_HOTPLUG_DISABLED);
330 free(rpath);
331 return (CFGA_NOTSUPP);
333 free(rpath);
334 return (CFGA_ERROR);
337 free(rpath);
339 *nodep = node;
340 return (CFGA_OK);
343 typedef struct error_size_cb_arg {
344 size_t rsrc_width;
345 size_t info_width;
346 int cnt;
347 } error_size_cb_arg_t;
350 * Callback function for hp_traverse(), to sum up the
351 * maximum length for error message display.
353 static int
354 error_sizeup_cb(hp_node_t node, void *arg)
356 error_size_cb_arg_t *sizearg = (error_size_cb_arg_t *)arg;
357 size_t len;
359 /* Only process USAGE nodes */
360 if (hp_type(node) != HP_NODE_USAGE)
361 return (HP_WALK_CONTINUE);
363 sizearg->cnt++;
365 /* size up resource name */
366 len = strlen(hp_name(node));
367 if (sizearg->rsrc_width < len)
368 sizearg->rsrc_width = len;
370 /* size up usage description */
371 len = strlen(hp_usage(node));
372 if (sizearg->info_width < len)
373 sizearg->info_width = len;
375 return (HP_WALK_CONTINUE);
378 typedef struct error_sum_cb_arg {
379 char **table;
380 char *format;
381 } error_sum_cb_arg_t;
384 * Callback function for hp_traverse(), to add the error
385 * message to the table.
387 static int
388 error_sumup_cb(hp_node_t node, void *arg)
390 error_sum_cb_arg_t *sumarg = (error_sum_cb_arg_t *)arg;
391 char **table = sumarg->table;
392 char *format = sumarg->format;
394 /* Only process USAGE nodes */
395 if (hp_type(node) != HP_NODE_USAGE)
396 return (HP_WALK_CONTINUE);
398 (void) strcat(*table, "\n");
399 (void) sprintf(&((*table)[strlen(*table)]),
400 format, hp_name(node), hp_usage(node));
402 return (HP_WALK_CONTINUE);
406 * Takes an opaque rcm_info_t pointer and a character pointer, and appends
407 * the rcm_info_t data in the form of a table to the given character pointer.
409 static void
410 pci_rcm_info_table(hp_node_t node, char **table)
412 int i;
413 size_t w;
414 size_t width = 0;
415 size_t w_rsrc = 0;
416 size_t w_info = 0;
417 size_t table_size = 0;
418 uint_t tuples = 0;
419 char *rsrc;
420 char *info;
421 char *newtable;
422 static char format[MAX_FORMAT];
423 const char *infostr;
424 error_size_cb_arg_t sizearg;
425 error_sum_cb_arg_t sumarg;
427 /* Protect against invalid arguments */
428 if (table == NULL)
429 return;
431 /* Set localized table header strings */
432 rsrc = dgettext(TEXT_DOMAIN, "Resource");
433 info = dgettext(TEXT_DOMAIN, "Information");
435 /* A first pass, to size up the RCM information */
436 sizearg.rsrc_width = strlen(rsrc);
437 sizearg.info_width = strlen(info);
438 sizearg.cnt = 0;
439 (void) hp_traverse(node, &sizearg, error_sizeup_cb);
441 /* If nothing was sized up above, stop early */
442 if (sizearg.cnt == 0)
443 return;
445 w_rsrc = sizearg.rsrc_width;
446 w_info = sizearg.info_width;
447 tuples = sizearg.cnt;
449 /* Adjust column widths for column headings */
450 if ((w = strlen(rsrc)) > w_rsrc)
451 w_rsrc = w;
452 else if ((w_rsrc - w) % 2)
453 w_rsrc++;
454 if ((w = strlen(info)) > w_info)
455 w_info = w;
456 else if ((w_info - w) % 2)
457 w_info++;
460 * Compute the total line width of each line,
461 * accounting for intercolumn spacing.
463 width = w_info + w_rsrc + 4;
465 /* Allocate space for the table */
466 table_size = (2 + tuples) * (width + 1) + 2;
467 if (*table == NULL) {
468 /* zero fill for the strcat() call below */
469 *table = calloc(table_size, sizeof (char));
470 if (*table == NULL)
471 return;
472 } else {
473 newtable = realloc(*table, strlen(*table) + table_size);
474 if (newtable == NULL)
475 return;
476 else
477 *table = newtable;
480 /* Place a table header into the string */
482 /* The resource header */
483 (void) strcat(*table, "\n");
484 w = strlen(rsrc);
485 for (i = 0; i < ((w_rsrc - w) / 2); i++)
486 (void) strcat(*table, " ");
487 (void) strcat(*table, rsrc);
488 for (i = 0; i < ((w_rsrc - w) / 2); i++)
489 (void) strcat(*table, " ");
491 /* The information header */
492 (void) strcat(*table, " ");
493 w = strlen(info);
494 for (i = 0; i < ((w_info - w) / 2); i++)
495 (void) strcat(*table, " ");
496 (void) strcat(*table, info);
497 for (i = 0; i < ((w_info - w) / 2); i++)
498 (void) strcat(*table, " ");
499 /* Underline the headers */
500 (void) strcat(*table, "\n");
501 for (i = 0; i < w_rsrc; i++)
502 (void) strcat(*table, "-");
503 (void) strcat(*table, " ");
504 for (i = 0; i < w_info; i++)
505 (void) strcat(*table, "-");
507 /* Construct the format string */
508 (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
509 (int)w_rsrc, (int)w_info);
511 /* Add the tuples to the table string */
512 sumarg.table = table;
513 sumarg.format = format;
514 (void) hp_traverse(node, &sumarg, error_sumup_cb);
518 * Figure out the target kernel state for a given cfgadm
519 * change-state operation.
521 static cfga_err_t
522 cfga_target_state(cfga_cmd_t state_change_cmd, int *state)
524 switch (state_change_cmd) {
525 case CFGA_CMD_CONNECT:
526 *state = DDI_HP_CN_STATE_POWERED;
527 break;
528 case CFGA_CMD_DISCONNECT:
529 *state = DDI_HP_CN_STATE_PRESENT;
530 break;
531 case CFGA_CMD_CONFIGURE:
532 *state = DDI_HP_CN_STATE_ENABLED;
533 break;
534 case CFGA_CMD_UNCONFIGURE:
535 *state = DDI_HP_CN_STATE_POWERED;
536 break;
537 default:
538 return (CFGA_ERROR);
541 return (CFGA_OK);
545 * Translate kernel state to cfgadm receptacle state and occupant state.
547 static cfga_err_t
548 cfga_get_state(hp_node_t connector, ap_rstate_t *rs, ap_ostate_t *os)
550 int state;
551 hp_node_t port;
553 state = hp_state(connector);
555 /* Receptacle state */
556 switch (state) {
557 case DDI_HP_CN_STATE_EMPTY:
558 *rs = AP_RSTATE_EMPTY;
559 break;
560 case DDI_HP_CN_STATE_PRESENT:
561 *rs = AP_RSTATE_DISCONNECTED;
562 break;
563 case DDI_HP_CN_STATE_POWERED:
564 case DDI_HP_CN_STATE_ENABLED:
565 *rs = AP_RSTATE_CONNECTED;
566 break;
568 * Connector state can only be one of
569 * Empty, Present, Powered, Enabled.
571 default:
572 return (CFGA_ERROR);
576 * Occupant state
578 port = hp_child(connector);
579 while (port != NULL) {
580 DBG(1, ("cfga_get_state:(%x)\n", hp_state(port)));
583 * Mark occupant state as "configured" if at least one of the
584 * associated ports is at state "offline" or above. Driver
585 * attach ("online" state) is not necessary here.
587 if (hp_state(port) >= DDI_HP_CN_STATE_OFFLINE)
588 break;
590 port = hp_sibling(port);
593 if (port != NULL)
594 *os = AP_OSTATE_CONFIGURED;
595 else
596 *os = AP_OSTATE_UNCONFIGURED;
598 return (CFGA_OK);
602 * Transitional Diagram:
604 * empty unconfigure
605 * (remove) ^| (physically insert card)
606 * |V
607 * disconnect configure
608 * "-c DISCONNECT" ^| "-c CONNECT"
609 * |V "-c CONFIGURE"
610 * connect unconfigure -> connect configure
611 * <-
612 * "-c UNCONFIGURE"
615 /*ARGSUSED*/
616 cfga_err_t
617 cfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
618 const char *options, struct cfga_confirm *confp,
619 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
621 int rv, state, new_state;
622 uint_t hpflags = 0;
623 hp_node_t node;
624 hp_node_t results = NULL;
626 if ((rv = check_options(options)) != CFGA_OK) {
627 return (rv);
630 if (errstring != NULL)
631 *errstring = NULL;
633 rv = CFGA_OK;
634 DBG(1, ("cfga_change_state:(%s)\n", ap_id));
636 rv = physpath2node(ap_id, errstring, &node);
637 if (rv != CFGA_OK)
638 return (rv);
641 * Check for the FORCE flag. It is only used
642 * for DISCONNECT or UNCONFIGURE state changes.
644 if (flags & CFGA_FLAG_FORCE)
645 hpflags |= HPFORCE;
647 state = hp_state(node);
650 * Which state should we drive to ?
652 if ((state_change_cmd != CFGA_CMD_LOAD) &&
653 (state_change_cmd != CFGA_CMD_UNLOAD)) {
654 if (cfga_target_state(state_change_cmd,
655 &new_state) != CFGA_OK) {
656 hp_fini(node);
657 return (CFGA_ERROR);
661 DBG(1, ("cfga_change_state: state is %d\n", state));
662 switch (state_change_cmd) {
663 case CFGA_CMD_CONNECT:
664 DBG(1, ("connect\n"));
665 if (state == DDI_HP_CN_STATE_EMPTY) {
666 cfga_err(errstring, ERR_AP_ERR, 0);
667 rv = CFGA_INVAL;
668 } else if (state == DDI_HP_CN_STATE_PRESENT) {
669 /* Connect the slot */
670 if (hp_set_state(node, 0, new_state, &results) != 0) {
671 rv = CFGA_ERROR;
672 cfga_err(errstring, CMD_SLOT_CONNECT, 0);
675 break;
677 case CFGA_CMD_DISCONNECT:
678 DBG(1, ("disconnect\n"));
679 if (state == DDI_HP_CN_STATE_EMPTY) {
680 cfga_err(errstring, ERR_AP_ERR, 0);
681 rv = CFGA_INVAL;
682 } else if (state > DDI_HP_CN_STATE_PRESENT) {
683 /* Disconnect the slot */
684 rv = hp_set_state(node, hpflags, new_state, &results);
685 if (rv != 0) {
686 if (rv == EBUSY)
687 rv = CFGA_BUSY;
688 else
689 rv = CFGA_ERROR;
691 if (results) {
692 pci_rcm_info_table(results, errstring);
693 hp_fini(results);
694 } else {
695 cfga_err(errstring,
696 CMD_SLOT_DISCONNECT, 0);
700 break;
702 case CFGA_CMD_CONFIGURE:
704 * for multi-func device we allow multiple
705 * configure on the same slot because one
706 * func can be configured and other one won't
708 DBG(1, ("configure\n"));
709 if (state == DDI_HP_CN_STATE_EMPTY) {
710 cfga_err(errstring, ERR_AP_ERR, 0);
711 rv = CFGA_INVAL;
712 } else if (hp_set_state(node, 0, new_state, &results) != 0) {
713 rv = CFGA_ERROR;
714 cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
716 break;
718 case CFGA_CMD_UNCONFIGURE:
719 DBG(1, ("unconfigure\n"));
720 if (state == DDI_HP_CN_STATE_EMPTY) {
721 cfga_err(errstring, ERR_AP_ERR, 0);
722 rv = CFGA_INVAL;
723 } else if (state >= DDI_HP_CN_STATE_ENABLED) {
724 rv = hp_set_state(node, hpflags, new_state, &results);
725 if (rv != 0) {
726 if (rv == EBUSY)
727 rv = CFGA_BUSY;
728 else
729 rv = CFGA_ERROR;
731 if (results) {
732 pci_rcm_info_table(results, errstring);
733 hp_fini(results);
734 } else {
735 cfga_err(errstring,
736 CMD_SLOT_UNCONFIGURE, 0);
740 DBG(1, ("unconfigure rv:(%i)\n", rv));
741 break;
743 case CFGA_CMD_LOAD:
744 /* do nothing, just produce error msg as is */
745 if (state < DDI_HP_CN_STATE_POWERED) {
746 rv = CFGA_ERROR;
747 cfga_err(errstring, CMD_SLOT_INSERT, 0);
748 } else {
749 cfga_err(errstring, ERR_AP_ERR, 0);
750 rv = CFGA_INVAL;
752 break;
754 case CFGA_CMD_UNLOAD:
755 /* do nothing, just produce error msg as is */
756 if (state < DDI_HP_CN_STATE_POWERED) {
757 rv = CFGA_ERROR;
758 cfga_err(errstring, CMD_SLOT_REMOVE, 0);
759 } else {
760 cfga_err(errstring, ERR_AP_ERR, 0);
761 rv = CFGA_INVAL;
763 break;
765 default:
766 rv = CFGA_OPNOTSUPP;
767 break;
770 hp_fini(node);
771 return (rv);
774 char *
775 get_val_from_result(char *result)
777 char *tmp;
779 tmp = strchr(result, '=');
780 if (tmp == NULL)
781 return (NULL);
783 tmp++;
784 return (tmp);
787 static cfga_err_t
788 prt_led_mode(const char *ap_id, int repeat, char **errstring,
789 struct cfga_msg *msgp)
791 pciehpc_led_t led;
792 hp_node_t node;
793 char *buff;
794 char *buf;
795 char *cp, line[MAXLINE];
796 char *tmp;
797 char *format;
798 char *result;
799 int i, n, rv;
800 int len = MAXLINE;
802 pciehpc_led_t states[] = {
803 PCIEHPC_POWER_LED,
804 PCIEHPC_FAULT_LED,
805 PCIEHPC_ATTN_LED,
806 PCIEHPC_ACTIVE_LED
809 DBG(1, ("prt_led_mod function\n"));
810 if (!repeat)
811 cfga_msg(msgp, "Ap_Id\t\t\tLed");
813 rv = physpath2node(ap_id, errstring, &node);
814 if (rv != CFGA_OK)
815 return (rv);
817 if ((buff = malloc(MAXPATHLEN)) == NULL) {
818 hp_fini(node);
819 cfga_err(errstring, "malloc ", 0);
820 return (CFGA_ERROR);
823 (void) memset(buff, 0, MAXPATHLEN);
825 if (fix_ap_name(buff, ap_id, hp_name(node),
826 errstring) != CFGA_OK) {
827 hp_fini(node);
828 free(buff);
829 return (CFGA_ERROR);
832 cp = line;
833 (void) snprintf(cp, len, "%s\t\t", buff);
834 len -= strlen(cp);
835 cp += strlen(cp);
837 free(buff);
839 n = sizeof (states)/sizeof (pciehpc_led_t);
840 for (i = 0; i < n; i++) {
841 led = states[i];
843 format = (i == n - 1) ? "%s=%s" : "%s=%s,";
844 if (hp_get_private(node, led_strs2[led], &result) != 0) {
845 (void) snprintf(cp, len, format,
846 led_strs[led], cfga_strs[UNKNOWN]);
847 len -= strlen(cp);
848 cp += strlen(cp);
849 DBG(1, ("%s:%s\n", led_strs[led], cfga_strs[UNKNOWN]));
850 } else {
852 * hp_get_private() will return back things like
853 * "led_fault=off", transform it to cfgadm desired
854 * format.
856 tmp = get_val_from_result(result);
857 if (tmp == NULL) {
858 free(result);
859 hp_fini(node);
860 return (CFGA_ERROR);
863 (void) snprintf(cp, len, format,
864 led_strs[led], tmp);
865 len -= strlen(cp);
866 cp += strlen(cp);
867 DBG(1, ("%s:%s\n", led_strs[led], tmp));
868 free(result);
872 cfga_msg(msgp, line); /* print the message */
874 hp_fini(node);
876 return (CFGA_OK);
879 /*ARGSUSED*/
880 cfga_err_t
881 cfga_private_func(const char *function, const char *ap_id,
882 const char *options, struct cfga_confirm *confp,
883 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
885 char *str;
886 int len, fd, i = 0, repeat = 0;
887 char buf[MAXNAMELEN];
888 char ptr;
889 cfga_err_t rv;
890 char *led, *mode;
891 hp_node_t node;
892 char *result;
894 DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
895 DBG(2, (" options: %s\n", (options == NULL)?"null":options));
896 DBG(2, (" confp: %x\n", confp));
897 DBG(2, (" cfga_msg: %x\n", cfga_msg));
898 DBG(2, (" flag: %d\n", flags));
900 if ((rv = check_options(options)) != CFGA_OK) {
901 return (rv);
904 if (private_check == confp)
905 repeat = 1;
906 else
907 private_check = (void*)confp;
909 for (i = 0, str = func_strs[i], len = strlen(str);
910 func_strs[i] != NULL; i++) {
911 str = func_strs[i];
912 len = strlen(str);
913 if (strncmp(function, str, len) == 0)
914 break;
917 switch (i) {
918 case ENABLE_SLOT:
919 case DISABLE_SLOT:
920 /* pass through */
921 case ENABLE_AUTOCNF:
922 case DISABLE_AUTOCNF:
923 /* no action needed */
924 return (CFGA_OK);
925 break;
926 case LED:
927 /* set mode */
928 ptr = function[len++];
929 if (ptr == '=') {
930 str = (char *)function;
931 for (str = (str+len++), i = 0; *str != ',';
932 i++, str++) {
933 if (i == (MAXNAMELEN - 1))
934 break;
936 buf[i] = *str;
937 DBG_F(2, (stdout, "%c\n", buf[i]));
939 buf[i] = '\0'; str++;
940 DBG(2, ("buf = %s\n", buf));
942 /* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
943 if (strcmp(buf, led_strs[POWER]) == 0)
944 led = PCIEHPC_PROP_LED_POWER;
945 else if (strcmp(buf, led_strs[FAULT]) == 0)
946 led = PCIEHPC_PROP_LED_FAULT;
947 else if (strcmp(buf, led_strs[ATTN]) == 0)
948 led = PCIEHPC_PROP_LED_ATTN;
949 else if (strcmp(buf, led_strs[ACTIVE]) == 0)
950 led = PCIEHPC_PROP_LED_ACTIVE;
951 else return (CFGA_INVAL);
953 len = strlen(func_strs[MODE]);
954 if ((strncmp(str, func_strs[MODE], len) == 0) &&
955 (*(str+(len)) == '=')) {
956 for (str = (str+(++len)), i = 0;
957 *str != '\0'; i++, str++) {
958 buf[i] = *str;
961 buf[i] = '\0';
962 DBG(2, ("buf_mode= %s\n", buf));
964 /* ON = 1, OFF = 0 */
965 if (strcmp(buf, mode_strs[ON]) == 0)
966 mode = PCIEHPC_PROP_VALUE_ON;
967 else if (strcmp(buf, mode_strs[OFF]) == 0)
968 mode = PCIEHPC_PROP_VALUE_OFF;
969 else if (strcmp(buf, mode_strs[BLINK]) == 0)
970 mode = PCIEHPC_PROP_VALUE_BLINK;
971 else return (CFGA_INVAL);
973 /* sendin */
974 memset(buf, 0, sizeof (buf));
975 snprintf(buf, sizeof (buf), "%s=%s",
976 led, mode);
977 buf[MAXNAMELEN - 1] = '\0';
979 break;
980 } else if (ptr == '\0') {
981 /* print mode */
982 DBG(1, ("Print mode\n"));
983 return (prt_led_mode(ap_id, repeat, errstring,
984 msgp));
986 default:
987 DBG(1, ("default\n"));
988 errno = EINVAL;
989 return (CFGA_INVAL);
992 rv = physpath2node(ap_id, errstring, &node);
993 if (rv != CFGA_OK)
994 return (rv);
996 if (hp_set_private(node, buf, &result) != 0) {
997 hp_fini(node);
998 return (CFGA_ERROR);
1001 hp_fini(node);
1002 return (CFGA_OK);
1005 /*ARGSUSED*/
1006 cfga_err_t cfga_test(const char *ap_id, const char *options,
1007 struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1009 cfga_err_t rv;
1010 if (errstring != NULL)
1011 *errstring = NULL;
1013 if ((rv = check_options(options)) != CFGA_OK) {
1014 return (rv);
1017 DBG(1, ("cfga_test:(%s)\n", ap_id));
1018 /* will need to implement pci CTRL command */
1019 return (CFGA_NOTSUPP);
1023 * The slot-names property describes the external labeling of add-in slots.
1024 * This property is an encoded array, an integer followed by a list of
1025 * strings. The return value from di_prop_lookup_ints for slot-names is -1.
1026 * The expected return value should be the number of elements.
1027 * Di_prop_decode_common does not decode encoded data from software,
1028 * such as the solaris device tree, unlike from the prom.
1029 * Di_prop_decode_common takes the size of the encoded data and mods
1030 * it with the size of int. The size of the encoded data for slot-names is 9
1031 * and the size of int is 4, yielding a non zero result. A value of -1 is used
1032 * to indicate that the number of elements can not be determined.
1033 * Di_prop_decode_common can be modified to decode encoded data from the solaris
1034 * device tree.
1036 static int
1037 fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
1039 if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
1040 return (DI_WALK_TERMINATE);
1041 } else {
1042 int i;
1043 char *tmptr = (char *)(intp+1);
1044 DBG(1, ("slot-bitmask: %x \n", *intp));
1046 rval = (rval -1) * 4;
1048 for (i = 0; i <= slotarg->minor; i++) {
1049 DBG(2, ("curr slot-name: %s \n", tmptr));
1051 if (i >= MAXDEVS)
1052 return (DI_WALK_TERMINATE);
1054 if ((*intp >> i) & 1) {
1055 /* assign tmptr */
1056 DBG(2, ("slot-name: %s \n", tmptr));
1057 if (i == slotarg->minor)
1058 (void) strcpy(slotarg->slotnames[i],
1059 tmptr);
1060 /* wind tmptr to next \0 */
1061 while (*tmptr != '\0') {
1062 tmptr++;
1064 tmptr++;
1065 } else {
1066 /* point at unknown string */
1067 if (i == slotarg->minor)
1068 (void) strcpy(slotarg->slotnames[i],
1069 "unknown");
1073 return (DI_WALK_TERMINATE);
1076 static int
1077 find_slotname(di_node_t din, di_minor_t dim, void *arg)
1079 struct searcharg *slotarg = (struct searcharg *)arg;
1080 di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
1081 di_prom_prop_t prom_prop;
1082 di_prop_t solaris_prop;
1083 int *intp, rval;
1084 char *devname;
1085 char fulldevname[MAXNAMELEN];
1087 slotarg->minor = dim->dev_minor % 256;
1089 DBG(2, ("minor number:(%i)\n", slotarg->minor));
1090 DBG(2, ("hot plug slots found so far:(%i)\n", 0));
1092 if ((devname = di_devfs_path(din)) != NULL) {
1093 (void) snprintf(fulldevname, MAXNAMELEN,
1094 "/devices%s:%s", devname, di_minor_name(dim));
1095 di_devfs_path_free(devname);
1098 if (strcmp(fulldevname, slotarg->devpath) == 0) {
1101 * Check the Solaris device tree first
1102 * in the case of a DR operation
1104 solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
1105 while (solaris_prop != DI_PROP_NIL) {
1106 if (strcmp("slot-names", di_prop_name(solaris_prop))
1107 == 0) {
1108 rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1109 din, di_prop_name(solaris_prop), &intp);
1110 slotarg->slt_name_src = SOLARIS_SLT_NAME;
1112 return (fixup_slotname(rval, intp, slotarg));
1114 solaris_prop = di_prop_hw_next(din, solaris_prop);
1118 * Check the prom device tree which is populated at boot.
1119 * If this fails, give up and set the slot name to null.
1121 prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
1122 while (prom_prop != DI_PROM_PROP_NIL) {
1123 if (strcmp("slot-names", di_prom_prop_name(prom_prop))
1124 == 0) {
1125 rval = di_prom_prop_lookup_ints(ph,
1126 din, di_prom_prop_name(prom_prop), &intp);
1127 slotarg->slt_name_src = PROM_SLT_NAME;
1129 return (fixup_slotname(rval, intp, slotarg));
1131 prom_prop = di_prom_prop_next(ph, din, prom_prop);
1133 *slotarg->slotnames[slotarg->minor] = '\0';
1134 return (DI_WALK_TERMINATE);
1135 } else
1136 return (DI_WALK_CONTINUE);
1139 static int
1140 find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
1142 di_node_t root_node;
1144 DBG(1, ("find_physical_slot_names\n"));
1146 if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
1147 == DI_NODE_NIL) {
1148 DBG(1, ("di_init() failed\n"));
1149 return (-1);
1152 slotarg->devpath = (char *)devcomp;
1154 if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1155 DBG(1, ("di_prom_init() failed\n"));
1156 di_fini(root_node);
1157 return (-1);
1160 (void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
1161 0, (void *)slotarg, find_slotname);
1163 di_prom_fini(slotarg->promp);
1164 di_fini(root_node);
1165 if (slotarg->slotnames[0] != NULL)
1166 return (0);
1167 else
1168 return (-1);
1171 static void
1172 get_type(const char *boardtype, const char *cardtype, char *buf)
1174 /* for type string assembly in get_type() */
1175 #define TPCT(s) (void) strlcat(buf, (s), CFGA_TYPE_LEN)
1177 int i;
1179 if (strcmp(cardtype, "unknown") == 0) {
1180 TPCT("unknown");
1181 return;
1184 TPCT(cardtype);
1185 TPCT("/");
1187 if (strcmp(boardtype, PCIEHPC_PROP_VALUE_PCIHOTPLUG) == 0)
1188 TPCT(board_strs[PCIEHPC_BOARD_PCI_HOTPLUG]);
1189 else
1190 TPCT(board_strs[PCIEHPC_BOARD_UNKNOWN]);
1194 * call-back function for di_devlink_walk
1195 * if the link lives in /dev/cfg copy its name
1197 static int
1198 found_devlink(di_devlink_t link, void *ap_log_id)
1200 if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
1201 /* copy everything but /dev/cfg/ */
1202 (void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
1203 DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
1204 return (DI_WALK_TERMINATE);
1206 return (DI_WALK_CONTINUE);
1210 * Walk throught the cached /dev link tree looking for links to the ap
1211 * if none are found return an error
1213 static cfga_err_t
1214 check_devlinks(char *ap_log_id, const char *ap_id)
1216 di_devlink_handle_t hdl;
1218 DBG(1, ("check_devlinks: %s\n", ap_id));
1220 hdl = di_devlink_init(NULL, 0);
1222 if (strncmp("/devices/", ap_id, 9) == 0) {
1223 /* ap_id is a valid minor_path with /devices prepended */
1224 (void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
1225 (void *)ap_log_id, found_devlink);
1226 } else {
1227 DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
1228 return (CFGA_ERROR);
1231 (void) di_devlink_fini(&hdl);
1233 if (ap_log_id[0] != '\0')
1234 return (CFGA_OK);
1235 else
1236 return (CFGA_ERROR);
1240 * most of this is needed to compensate for
1241 * differences between various platforms
1243 static cfga_err_t
1244 fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
1245 char **errstring)
1247 char *buf;
1248 char *tmp;
1249 char *ptr;
1251 di_node_t ap_node;
1253 ap_log_id[0] = '\0';
1255 if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
1256 return (CFGA_OK);
1258 DBG(1, ("fix_ap_name: %s\n", ap_id));
1260 if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
1261 DBG(1, ("malloc failed\n"));
1262 return (CFGA_ERROR);
1264 (void) strcpy(buf, ap_id);
1265 tmp = buf + sizeof ("/devices") - 1;
1267 ptr = strchr(tmp, ':');
1268 ptr[0] = '\0';
1270 DBG(1, ("fix_ap_name: %s\n", tmp));
1272 ap_node = di_init(tmp, DINFOMINOR);
1273 if (ap_node == DI_NODE_NIL) {
1274 cfga_err(errstring, "di_init ", 0);
1275 DBG(1, ("fix_ap_name: failed to snapshot node\n"));
1276 return (CFGA_ERROR);
1279 (void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
1280 di_driver_name(ap_node), di_instance(ap_node), slot_name);
1282 DBG(1, ("fix_ap_name: %s\n", ap_log_id));
1284 di_fini(ap_node);
1286 free(buf);
1287 return (CFGA_OK);
1291 static int
1292 findlink_cb(di_devlink_t devlink, void *arg)
1294 (*(char **)arg) = strdup(di_devlink_path(devlink));
1296 return (DI_WALK_TERMINATE);
1300 * returns an allocated string containing the full path to the devlink for
1301 * <ap_phys_id> in the devlink database; we expect only one devlink per
1302 * <ap_phys_id> so we return the first encountered
1304 static char *
1305 findlink(char *ap_phys_id)
1307 di_devlink_handle_t hdl;
1308 char *path = NULL;
1310 hdl = di_devlink_init(NULL, 0);
1312 if (strncmp("/devices/", ap_phys_id, 9) == 0)
1313 ap_phys_id += 8;
1315 (void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
1316 (void *)&path, findlink_cb);
1318 (void) di_devlink_fini(&hdl);
1319 return (path);
1324 * returns CFGA_OK if it can succesfully retrieve the devlink info associated
1325 * with devlink for <ap_phys_id> which will be returned through <ap_info>
1327 cfga_err_t
1328 get_dli(char *dlpath, char *ap_info, int ap_info_sz)
1330 int fd;
1332 fd = di_dli_openr(dlpath);
1333 if (fd < 0)
1334 return (CFGA_ERROR);
1336 (void) read(fd, ap_info, ap_info_sz);
1337 ap_info[ap_info_sz - 1] = '\0';
1339 di_dli_close(fd);
1340 return (CFGA_OK);
1343 static cfga_err_t
1344 cfga_get_condition(hp_node_t node, ap_condition_t *cond)
1346 char *condition;
1348 /* "condition" bus specific commands */
1349 if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
1350 &condition) != 0) {
1351 *cond = AP_COND_UNKNOWN;
1352 return (CFGA_ERROR);
1355 condition = get_val_from_result(condition);
1357 if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
1358 *cond = AP_COND_OK;
1359 else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
1360 *cond = AP_COND_FAILING;
1361 else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
1362 *cond = AP_COND_FAILED;
1363 else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
1364 *cond = AP_COND_UNUSABLE;
1365 else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
1366 *cond = AP_COND_UNKNOWN;
1367 else
1368 return (CFGA_ERROR);
1370 return (CFGA_OK);
1373 /*ARGSUSED*/
1374 cfga_err_t
1375 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1376 int *nlist, const char *options, const char *listopts, char **errstring,
1377 cfga_flags_t flags)
1379 char *boardtype;
1380 char *cardtype;
1381 char *tmpb = NULL, *tmpc = NULL;
1382 struct searcharg slotname_arg;
1383 int fd;
1384 int rv = CFGA_OK;
1385 char *dlpath = NULL;
1386 hp_node_t node;
1387 ap_rstate_t rs;
1388 ap_ostate_t os;
1389 ap_condition_t cond;
1391 if ((rv = check_options(options)) != CFGA_OK) {
1392 return (rv);
1395 if (errstring != NULL)
1396 *errstring = NULL;
1398 DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1400 if (cs == NULL || nlist == NULL) {
1401 rv = CFGA_ERROR;
1402 return (rv);
1405 *nlist = 1;
1407 if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1408 cfga_err(errstring, "malloc ", 0);
1409 DBG(1, ("malloc failed\n"));
1410 rv = CFGA_ERROR;
1411 return (rv);
1413 (void) memset(*cs, 0, sizeof (cfga_list_data_t));
1415 rv = physpath2node(ap_id, errstring, &node);
1416 if (rv != CFGA_OK) {
1417 DBG(1, ("physpath2node failed\n"));
1418 return (rv);
1421 if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
1422 DBG(1, ("cfga_get_state failed\n"));
1423 hp_fini(node);
1424 return (CFGA_ERROR);
1427 switch (rs) {
1428 case AP_RSTATE_EMPTY:
1429 (*cs)->ap_r_state = CFGA_STAT_EMPTY;
1430 DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1431 break;
1432 case AP_RSTATE_DISCONNECTED:
1433 (*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1434 DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1435 break;
1436 case AP_RSTATE_CONNECTED:
1437 (*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1438 DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1439 break;
1440 default:
1441 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1442 rv = CFGA_ERROR;
1443 hp_fini(node);
1444 return (rv);
1447 switch (os) {
1448 case AP_OSTATE_CONFIGURED:
1449 (*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1450 DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1451 break;
1452 case AP_OSTATE_UNCONFIGURED:
1453 (*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1454 DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1455 break;
1456 default:
1457 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1458 rv = CFGA_ERROR;
1459 hp_fini(node);
1460 return (rv);
1463 (void) cfga_get_condition(node, &cond);
1465 switch (cond) {
1466 case AP_COND_OK:
1467 (*cs)->ap_cond = CFGA_COND_OK;
1468 DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1469 break;
1470 case AP_COND_FAILING:
1471 (*cs)->ap_cond = CFGA_COND_FAILING;
1472 DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1473 break;
1474 case AP_COND_FAILED:
1475 (*cs)->ap_cond = CFGA_COND_FAILED;
1476 DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1477 break;
1478 case AP_COND_UNUSABLE:
1479 (*cs)->ap_cond = CFGA_COND_UNUSABLE;
1480 DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1481 break;
1482 case AP_COND_UNKNOWN:
1483 (*cs)->ap_cond = CFGA_COND_UNKNOWN;
1484 DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1485 break;
1486 default:
1487 cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1488 rv = CFGA_ERROR;
1489 hp_fini(node);
1490 return (rv);
1493 * We're not busy since the entrance into the kernel has been
1494 * sync'ed via libhotplug.
1496 (*cs)->ap_busy = 0;
1498 /* last change */
1499 (*cs)->ap_status_time = hp_last_change(node);
1501 /* board type */
1502 if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &tmpb) != 0)
1503 boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1504 else
1505 boardtype = get_val_from_result(tmpb);
1507 /* card type */
1508 if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &tmpc) != 0)
1509 cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1510 else
1511 cardtype = get_val_from_result(tmpc);
1513 /* logical ap_id */
1514 rv = fix_ap_name((*cs)->ap_log_id, ap_id,
1515 hp_name(node), errstring);
1516 DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
1517 /* physical ap_id */
1518 (void) strcpy((*cs)->ap_phys_id, ap_id); /* physical path of AP */
1520 /* information */
1521 dlpath = findlink((*cs)->ap_phys_id);
1522 if (dlpath != NULL) {
1523 if (get_dli(dlpath, (*cs)->ap_info,
1524 sizeof ((*cs)->ap_info)) != CFGA_OK)
1525 (*cs)->ap_info[0] = '\0';
1526 free(dlpath);
1529 if ((*cs)->ap_log_id[0] == '\0')
1530 (void) strcpy((*cs)->ap_log_id, hp_name(node));
1532 if ((*cs)->ap_info[0] == '\0') {
1533 /* slot_names of bus node */
1534 if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1535 (void) strcpy((*cs)->ap_info,
1536 slotname_arg.slotnames[slotname_arg.minor]);
1539 /* class_code/subclass/boardtype */
1540 get_type(boardtype, cardtype, (*cs)->ap_type);
1542 DBG(1, ("cfga_list_ext return success\n"));
1543 rv = CFGA_OK;
1545 free(tmpb);
1546 free(tmpc);
1547 hp_fini(node);
1548 return (rv);
1552 * This routine prints a single line of help message
1554 static void
1555 cfga_msg(struct cfga_msg *msgp, const char *str)
1557 DBG(2, ("<%s>", str));
1559 if (msgp == NULL || msgp->message_routine == NULL)
1560 return;
1562 (*msgp->message_routine)(msgp->appdata_ptr, str);
1563 (*msgp->message_routine)(msgp->appdata_ptr, "\n");
1566 static cfga_err_t
1567 check_options(const char *options)
1569 struct cfga_msg *msgp = NULL;
1571 if (options) {
1572 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1573 cfga_msg(msgp, options);
1574 return (CFGA_INVAL);
1576 return (CFGA_OK);
1579 /*ARGSUSED*/
1580 cfga_err_t
1581 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1583 if (options) {
1584 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1585 cfga_msg(msgp, options);
1587 DBG(1, ("cfga_help\n"));
1589 cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1590 cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1591 cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1592 cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1593 cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1594 cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1595 cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1596 return (CFGA_OK);
1600 * cfga_err() accepts a variable number of message IDs and constructs
1601 * a corresponding error string which is returned via the errstring argument.
1602 * cfga_err() calls gettext() to internationalize proper messages.
1604 static void
1605 cfga_err(char **errstring, ...)
1607 int a;
1608 int i;
1609 int n;
1610 int len;
1611 int flen;
1612 char *p;
1613 char *q;
1614 char *s[32];
1615 char *failed;
1616 va_list ap;
1619 * If errstring is null it means user is not interested in getting
1620 * error status. So we don't do all the work
1622 if (errstring == NULL) {
1623 return;
1625 va_start(ap, errstring);
1627 failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1628 flen = strlen(failed);
1630 for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1631 switch (a) {
1632 case CMD_GETSTAT:
1633 case CMD_LIST:
1634 case CMD_SLOT_CONNECT:
1635 case CMD_SLOT_DISCONNECT:
1636 case CMD_SLOT_CONFIGURE:
1637 case CMD_SLOT_UNCONFIGURE:
1638 p = cfga_errstrs(a);
1639 len += (strlen(p) + flen);
1640 s[n] = p;
1641 s[++n] = cfga_strs[FAILED];
1643 DBG(2, ("<%s>", p));
1644 DBG(2, (cfga_strs[FAILED]));
1645 break;
1647 case ERR_CMD_INVAL:
1648 case ERR_AP_INVAL:
1649 case ERR_OPT_INVAL:
1650 case ERR_AP_ERR:
1651 switch (a) {
1652 case ERR_CMD_INVAL:
1653 p = dgettext(TEXT_DOMAIN,
1654 cfga_errstrs[ERR_CMD_INVAL]);
1655 break;
1656 case ERR_AP_INVAL:
1657 p = dgettext(TEXT_DOMAIN,
1658 cfga_errstrs[ERR_AP_INVAL]);
1659 break;
1660 case ERR_OPT_INVAL:
1661 p = dgettext(TEXT_DOMAIN,
1662 cfga_errstrs[ERR_OPT_INVAL]);
1663 break;
1664 case ERR_AP_ERR:
1665 p = dgettext(TEXT_DOMAIN,
1666 cfga_errstrs[ERR_AP_ERR]);
1667 break;
1670 if ((q = va_arg(ap, char *)) != NULL) {
1671 len += (strlen(p) + strlen(q));
1672 s[n] = p;
1673 s[++n] = q;
1674 DBG(2, ("<%s>", p));
1675 DBG(2, ("<%s>", q));
1676 break;
1677 } else {
1678 len += strlen(p);
1679 s[n] = p;
1682 DBG(2, ("<%s>", p));
1683 break;
1685 default:
1686 n--;
1687 break;
1691 DBG(2, ("\n"));
1692 va_end(ap);
1694 if ((p = calloc(len + 1, 1)) == NULL)
1695 return;
1697 for (i = 0; i < n; i++) {
1698 (void) strlcat(p, s[i], len + 1);
1699 DBG(2, ("i:%d, %s\n", i, s[i]));
1702 *errstring = p;
1703 DBG(2, ("%s\n", *errstring));
1707 * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm