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]
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
43 #include <sys/param.h>
45 #include <libdevinfo.h>
46 #include <libdevice.h>
48 #define CFGA_PLUGIN_LIB
50 #include <config_admin.h>
53 #include <sys/types.h>
55 #include <sys/dditypes.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"
76 #define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
79 * Set the version number
81 int cfga_version
= CFGA_HSL_V2
;
87 #if !defined(TEXT_DOMAIN)
88 #define TEXT_DOMAIN "SYS_TEST"
94 * External routines: 1 - 2
95 * Internal routines: 3 - 4
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; }
104 #define DBG(level, args) /* nothing */
105 #define DBG_F(level, args) /* nothing */
108 #define CMD_ACQUIRE 0
109 #define CMD_GETSTAT 1
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
119 #define ERR_CMD_INVAL 11
120 #define ERR_AP_INVAL 12
121 #define ERR_AP_ERR 13
122 #define ERR_OPT_INVAL 14
127 /* n */ "get-status ",
130 /* n */ "disconnect ",
131 /* n */ "configure ",
132 /* n */ "unconfigure ",
137 /* y */ "invalid command ",
138 /* y */ "invalid attachment point ",
139 /* y */ "invalid transition ",
140 /* y */ "invalid option ",
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
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
);
170 "\nPCI hotplug specific commands:",
171 "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
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: ",
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
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
;
205 /* n */ "???", /* PCIEHPC_BOARD_UNKNOWN */
206 /* n */ "hp", /* PCIEHPC_BOARD_PCI_HOTPLUG */
215 /* n */ "enable_slot",
216 /* n */ "disable_slot",
217 /* n */ "enable_autoconfig",
218 /* n */ "disable_autoconfig",
229 /* n */ "fault", /* PCIEHPC_FAULT_LED */
230 /* n */ "power", /* PCIEHPC_POWER_LED */
231 /* n */ "attn", /* PCIEHPC_ATTN_LED */
232 /* n */ "active", /* PCIEHPC_ACTIVE_LED */
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 */
252 /* n */ "off", /* OFF */
253 /* n */ "on", /* ON */
254 /* n */ "blink", /* BLINK */
262 #define cfga_errstrs(i) cfga_errstrs[(i)]
264 #define cfga_eid(a, b) (((a) << 8) + (b))
274 char slotnames
[MAXDEVS
][MAXNAMELEN
];
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.
287 physpath2node(const char *physpath
, char **errstring
, hp_node_t
*nodep
)
295 if (getuid() != 0 && geteuid() != 0)
298 if ((rpath
= malloc(strlen(physpath
) + 1)) == NULL
)
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
) {
315 /* Remove minor name (if any) */
316 if ((cp
= strrchr(rpath
, ':')) == NULL
) {
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
);
331 return (CFGA_NOTSUPP
);
343 typedef struct error_size_cb_arg
{
347 } error_size_cb_arg_t
;
350 * Callback function for hp_traverse(), to sum up the
351 * maximum length for error message display.
354 error_sizeup_cb(hp_node_t node
, void *arg
)
356 error_size_cb_arg_t
*sizearg
= (error_size_cb_arg_t
*)arg
;
359 /* Only process USAGE nodes */
360 if (hp_type(node
) != HP_NODE_USAGE
)
361 return (HP_WALK_CONTINUE
);
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
{
381 } error_sum_cb_arg_t
;
384 * Callback function for hp_traverse(), to add the error
385 * message to the table.
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.
410 pci_rcm_info_table(hp_node_t node
, char **table
)
417 size_t table_size
= 0;
422 static char format
[MAX_FORMAT
];
424 error_size_cb_arg_t sizearg
;
425 error_sum_cb_arg_t sumarg
;
427 /* Protect against invalid arguments */
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
);
439 (void) hp_traverse(node
, &sizearg
, error_sizeup_cb
);
441 /* If nothing was sized up above, stop early */
442 if (sizearg
.cnt
== 0)
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
)
452 else if ((w_rsrc
- w
) % 2)
454 if ((w
= strlen(info
)) > w_info
)
456 else if ((w_info
- w
) % 2)
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));
473 newtable
= realloc(*table
, strlen(*table
) + table_size
);
474 if (newtable
== NULL
)
480 /* Place a table header into the string */
482 /* The resource header */
483 (void) strcat(*table
, "\n");
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
, " ");
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.
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
;
528 case CFGA_CMD_DISCONNECT
:
529 *state
= DDI_HP_CN_STATE_PRESENT
;
531 case CFGA_CMD_CONFIGURE
:
532 *state
= DDI_HP_CN_STATE_ENABLED
;
534 case CFGA_CMD_UNCONFIGURE
:
535 *state
= DDI_HP_CN_STATE_POWERED
;
545 * Translate kernel state to cfgadm receptacle state and occupant state.
548 cfga_get_state(hp_node_t connector
, ap_rstate_t
*rs
, ap_ostate_t
*os
)
553 state
= hp_state(connector
);
555 /* Receptacle state */
557 case DDI_HP_CN_STATE_EMPTY
:
558 *rs
= AP_RSTATE_EMPTY
;
560 case DDI_HP_CN_STATE_PRESENT
:
561 *rs
= AP_RSTATE_DISCONNECTED
;
563 case DDI_HP_CN_STATE_POWERED
:
564 case DDI_HP_CN_STATE_ENABLED
:
565 *rs
= AP_RSTATE_CONNECTED
;
568 * Connector state can only be one of
569 * Empty, Present, Powered, Enabled.
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
)
590 port
= hp_sibling(port
);
594 *os
= AP_OSTATE_CONFIGURED
;
596 *os
= AP_OSTATE_UNCONFIGURED
;
602 * Transitional Diagram:
605 * (remove) ^| (physically insert card)
607 * disconnect configure
608 * "-c DISCONNECT" ^| "-c CONNECT"
610 * connect unconfigure -> connect configure
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
;
624 hp_node_t results
= NULL
;
626 if ((rv
= check_options(options
)) != CFGA_OK
) {
630 if (errstring
!= NULL
)
634 DBG(1, ("cfga_change_state:(%s)\n", ap_id
));
636 rv
= physpath2node(ap_id
, errstring
, &node
);
641 * Check for the FORCE flag. It is only used
642 * for DISCONNECT or UNCONFIGURE state changes.
644 if (flags
& CFGA_FLAG_FORCE
)
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
) {
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);
668 } else if (state
== DDI_HP_CN_STATE_PRESENT
) {
669 /* Connect the slot */
670 if (hp_set_state(node
, 0, new_state
, &results
) != 0) {
672 cfga_err(errstring
, CMD_SLOT_CONNECT
, 0);
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);
682 } else if (state
> DDI_HP_CN_STATE_PRESENT
) {
683 /* Disconnect the slot */
684 rv
= hp_set_state(node
, hpflags
, new_state
, &results
);
692 pci_rcm_info_table(results
, errstring
);
696 CMD_SLOT_DISCONNECT
, 0);
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);
712 } else if (hp_set_state(node
, 0, new_state
, &results
) != 0) {
714 cfga_err(errstring
, CMD_SLOT_CONFIGURE
, 0);
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);
723 } else if (state
>= DDI_HP_CN_STATE_ENABLED
) {
724 rv
= hp_set_state(node
, hpflags
, new_state
, &results
);
732 pci_rcm_info_table(results
, errstring
);
736 CMD_SLOT_UNCONFIGURE
, 0);
740 DBG(1, ("unconfigure rv:(%i)\n", rv
));
744 /* do nothing, just produce error msg as is */
745 if (state
< DDI_HP_CN_STATE_POWERED
) {
747 cfga_err(errstring
, CMD_SLOT_INSERT
, 0);
749 cfga_err(errstring
, ERR_AP_ERR
, 0);
754 case CFGA_CMD_UNLOAD
:
755 /* do nothing, just produce error msg as is */
756 if (state
< DDI_HP_CN_STATE_POWERED
) {
758 cfga_err(errstring
, CMD_SLOT_REMOVE
, 0);
760 cfga_err(errstring
, ERR_AP_ERR
, 0);
775 get_val_from_result(char *result
)
779 tmp
= strchr(result
, '=');
788 prt_led_mode(const char *ap_id
, int repeat
, char **errstring
,
789 struct cfga_msg
*msgp
)
795 char *cp
, line
[MAXLINE
];
802 pciehpc_led_t states
[] = {
809 DBG(1, ("prt_led_mod function\n"));
811 cfga_msg(msgp
, "Ap_Id\t\t\tLed");
813 rv
= physpath2node(ap_id
, errstring
, &node
);
817 if ((buff
= malloc(MAXPATHLEN
)) == NULL
) {
819 cfga_err(errstring
, "malloc ", 0);
823 (void) memset(buff
, 0, MAXPATHLEN
);
825 if (fix_ap_name(buff
, ap_id
, hp_name(node
),
826 errstring
) != CFGA_OK
) {
833 (void) snprintf(cp
, len
, "%s\t\t", buff
);
839 n
= sizeof (states
)/sizeof (pciehpc_led_t
);
840 for (i
= 0; i
< n
; 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
]);
849 DBG(1, ("%s:%s\n", led_strs
[led
], cfga_strs
[UNKNOWN
]));
852 * hp_get_private() will return back things like
853 * "led_fault=off", transform it to cfgadm desired
856 tmp
= get_val_from_result(result
);
863 (void) snprintf(cp
, len
, format
,
867 DBG(1, ("%s:%s\n", led_strs
[led
], tmp
));
872 cfga_msg(msgp
, line
); /* print the message */
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
)
886 int len
, fd
, i
= 0, repeat
= 0;
887 char buf
[MAXNAMELEN
];
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
) {
904 if (private_check
== confp
)
907 private_check
= (void*)confp
;
909 for (i
= 0, str
= func_strs
[i
], len
= strlen(str
);
910 func_strs
[i
] != NULL
; i
++) {
913 if (strncmp(function
, str
, len
) == 0)
922 case DISABLE_AUTOCNF
:
923 /* no action needed */
928 ptr
= function
[len
++];
930 str
= (char *)function
;
931 for (str
= (str
+len
++), i
= 0; *str
!= ',';
933 if (i
== (MAXNAMELEN
- 1))
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
++) {
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
);
974 memset(buf
, 0, sizeof (buf
));
975 snprintf(buf
, sizeof (buf
), "%s=%s",
977 buf
[MAXNAMELEN
- 1] = '\0';
980 } else if (ptr
== '\0') {
982 DBG(1, ("Print mode\n"));
983 return (prt_led_mode(ap_id
, repeat
, errstring
,
987 DBG(1, ("default\n"));
992 rv
= physpath2node(ap_id
, errstring
, &node
);
996 if (hp_set_private(node
, buf
, &result
) != 0) {
1006 cfga_err_t
cfga_test(const char *ap_id
, const char *options
,
1007 struct cfga_msg
*msgp
, char **errstring
, cfga_flags_t flags
)
1010 if (errstring
!= NULL
)
1013 if ((rv
= check_options(options
)) != CFGA_OK
) {
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
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
);
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
));
1052 return (DI_WALK_TERMINATE
);
1054 if ((*intp
>> i
) & 1) {
1056 DBG(2, ("slot-name: %s \n", tmptr
));
1057 if (i
== slotarg
->minor
)
1058 (void) strcpy(slotarg
->slotnames
[i
],
1060 /* wind tmptr to next \0 */
1061 while (*tmptr
!= '\0') {
1066 /* point at unknown string */
1067 if (i
== slotarg
->minor
)
1068 (void) strcpy(slotarg
->slotnames
[i
],
1073 return (DI_WALK_TERMINATE
);
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
;
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
))
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
))
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
);
1136 return (DI_WALK_CONTINUE
);
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
))
1148 DBG(1, ("di_init() failed\n"));
1152 slotarg
->devpath
= (char *)devcomp
;
1154 if ((slotarg
->promp
= di_prom_init()) == DI_PROM_HANDLE_NIL
) {
1155 DBG(1, ("di_prom_init() failed\n"));
1160 (void) di_walk_minor(root_node
, "ddi_ctl:attachment_point:pci",
1161 0, (void *)slotarg
, find_slotname
);
1163 di_prom_fini(slotarg
->promp
);
1165 if (slotarg
->slotnames
[0] != NULL
)
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)
1179 if (strcmp(cardtype
, "unknown") == 0) {
1187 if (strcmp(boardtype
, PCIEHPC_PROP_VALUE_PCIHOTPLUG
) == 0)
1188 TPCT(board_strs
[PCIEHPC_BOARD_PCI_HOTPLUG
]);
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
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
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
);
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')
1236 return (CFGA_ERROR
);
1240 * most of this is needed to compensate for
1241 * differences between various platforms
1244 fix_ap_name(char *ap_log_id
, const char *ap_id
, char *slot_name
,
1253 ap_log_id
[0] = '\0';
1255 if (check_devlinks(ap_log_id
, ap_id
) == 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
, ':');
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
));
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
1305 findlink(char *ap_phys_id
)
1307 di_devlink_handle_t hdl
;
1310 hdl
= di_devlink_init(NULL
, 0);
1312 if (strncmp("/devices/", ap_phys_id
, 9) == 0)
1315 (void) di_devlink_walk(hdl
, "^cfg/.+$", ap_phys_id
, DI_PRIMARY_LINK
,
1316 (void *)&path
, findlink_cb
);
1318 (void) di_devlink_fini(&hdl
);
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>
1328 get_dli(char *dlpath
, char *ap_info
, int ap_info_sz
)
1332 fd
= di_dli_openr(dlpath
);
1334 return (CFGA_ERROR
);
1336 (void) read(fd
, ap_info
, ap_info_sz
);
1337 ap_info
[ap_info_sz
- 1] = '\0';
1344 cfga_get_condition(hp_node_t node
, ap_condition_t
*cond
)
1348 /* "condition" bus specific commands */
1349 if (hp_get_private(node
, PCIEHPC_PROP_SLOT_CONDITION
,
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)
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
;
1368 return (CFGA_ERROR
);
1375 cfga_list_ext(const char *ap_id
, cfga_list_data_t
**cs
,
1376 int *nlist
, const char *options
, const char *listopts
, char **errstring
,
1381 char *tmpb
= NULL
, *tmpc
= NULL
;
1382 struct searcharg slotname_arg
;
1385 char *dlpath
= NULL
;
1389 ap_condition_t cond
;
1391 if ((rv
= check_options(options
)) != CFGA_OK
) {
1395 if (errstring
!= NULL
)
1398 DBG(1, ("cfga_list_ext:(%s)\n", ap_id
));
1400 if (cs
== NULL
|| nlist
== NULL
) {
1407 if ((*cs
= malloc(sizeof (cfga_list_data_t
))) == NULL
) {
1408 cfga_err(errstring
, "malloc ", 0);
1409 DBG(1, ("malloc failed\n"));
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"));
1421 if (cfga_get_state(node
, &rs
, &os
) != CFGA_OK
) {
1422 DBG(1, ("cfga_get_state failed\n"));
1424 return (CFGA_ERROR
);
1428 case AP_RSTATE_EMPTY
:
1429 (*cs
)->ap_r_state
= CFGA_STAT_EMPTY
;
1430 DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1432 case AP_RSTATE_DISCONNECTED
:
1433 (*cs
)->ap_r_state
= CFGA_STAT_DISCONNECTED
;
1434 DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1436 case AP_RSTATE_CONNECTED
:
1437 (*cs
)->ap_r_state
= CFGA_STAT_CONNECTED
;
1438 DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1441 cfga_err(errstring
, CMD_GETSTAT
, ap_id
, 0);
1448 case AP_OSTATE_CONFIGURED
:
1449 (*cs
)->ap_o_state
= CFGA_STAT_CONFIGURED
;
1450 DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1452 case AP_OSTATE_UNCONFIGURED
:
1453 (*cs
)->ap_o_state
= CFGA_STAT_UNCONFIGURED
;
1454 DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1457 cfga_err(errstring
, CMD_GETSTAT
, ap_id
, 0);
1463 (void) cfga_get_condition(node
, &cond
);
1467 (*cs
)->ap_cond
= CFGA_COND_OK
;
1468 DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1470 case AP_COND_FAILING
:
1471 (*cs
)->ap_cond
= CFGA_COND_FAILING
;
1472 DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1474 case AP_COND_FAILED
:
1475 (*cs
)->ap_cond
= CFGA_COND_FAILED
;
1476 DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1478 case AP_COND_UNUSABLE
:
1479 (*cs
)->ap_cond
= CFGA_COND_UNUSABLE
;
1480 DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1482 case AP_COND_UNKNOWN
:
1483 (*cs
)->ap_cond
= CFGA_COND_UNKNOWN
;
1484 DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1487 cfga_err(errstring
, CMD_GETSTAT
, ap_id
, 0);
1493 * We're not busy since the entrance into the kernel has been
1494 * sync'ed via libhotplug.
1499 (*cs
)->ap_status_time
= hp_last_change(node
);
1502 if (hp_get_private(node
, PCIEHPC_PROP_BOARD_TYPE
, &tmpb
) != 0)
1503 boardtype
= PCIEHPC_PROP_VALUE_UNKNOWN
;
1505 boardtype
= get_val_from_result(tmpb
);
1508 if (hp_get_private(node
, PCIEHPC_PROP_CARD_TYPE
, &tmpc
) != 0)
1509 cardtype
= PCIEHPC_PROP_VALUE_UNKNOWN
;
1511 cardtype
= get_val_from_result(tmpc
);
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 */
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';
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"));
1552 * This routine prints a single line of help message
1555 cfga_msg(struct cfga_msg
*msgp
, const char *str
)
1557 DBG(2, ("<%s>", str
));
1559 if (msgp
== NULL
|| msgp
->message_routine
== NULL
)
1562 (*msgp
->message_routine
)(msgp
->appdata_ptr
, str
);
1563 (*msgp
->message_routine
)(msgp
->appdata_ptr
, "\n");
1567 check_options(const char *options
)
1569 struct cfga_msg
*msgp
= NULL
;
1572 cfga_msg(msgp
, dgettext(TEXT_DOMAIN
, cfga_strs
[HELP_UNKNOWN
]));
1573 cfga_msg(msgp
, options
);
1574 return (CFGA_INVAL
);
1581 cfga_help(struct cfga_msg
*msgp
, const char *options
, cfga_flags_t flags
)
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
]);
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.
1605 cfga_err(char **errstring
, ...)
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
) {
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
++) {
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
);
1641 s
[++n
] = cfga_strs
[FAILED
];
1643 DBG(2, ("<%s>", p
));
1644 DBG(2, (cfga_strs
[FAILED
]));
1653 p
= dgettext(TEXT_DOMAIN
,
1654 cfga_errstrs
[ERR_CMD_INVAL
]);
1657 p
= dgettext(TEXT_DOMAIN
,
1658 cfga_errstrs
[ERR_AP_INVAL
]);
1661 p
= dgettext(TEXT_DOMAIN
,
1662 cfga_errstrs
[ERR_OPT_INVAL
]);
1665 p
= dgettext(TEXT_DOMAIN
,
1666 cfga_errstrs
[ERR_AP_ERR
]);
1670 if ((q
= va_arg(ap
, char *)) != NULL
) {
1671 len
+= (strlen(p
) + strlen(q
));
1674 DBG(2, ("<%s>", p
));
1675 DBG(2, ("<%s>", q
));
1682 DBG(2, ("<%s>", p
));
1694 if ((p
= calloc(len
+ 1, 1)) == NULL
)
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
]));
1703 DBG(2, ("%s\n", *errstring
));
1707 * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm