2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Plugin library for PCI Express and PCI (SHPC) hotplug controller
2N/A */
2N/A
2N/A#include <stddef.h>
2N/A#include <locale.h>
2N/A#include <ctype.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <errno.h>
2N/A#include <locale.h>
2N/A#include <langinfo.h>
2N/A#include <time.h>
2N/A#include <sys/param.h>
2N/A#include <stdarg.h>
2N/A#include <libdevinfo.h>
2N/A#include <libdevice.h>
2N/A
2N/A#define CFGA_PLUGIN_LIB
2N/A
2N/A#include <config_admin.h>
2N/A
2N/A#include <assert.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/dditypes.h>
2N/A#include <sys/pci.h>
2N/A#include <libintl.h>
2N/A
2N/A#include <dirent.h>
2N/A#include <limits.h>
2N/A#include <sys/mkdev.h>
2N/A#include "../../../../uts/common/sys/hotplug/pci/pcie_hp.h"
2N/A#include "../../../../common/pci/pci_strings.h"
2N/A#include <libhotplug.h>
2N/A
2N/Aextern const struct pci_class_strings_s class_pci[];
2N/Aextern int class_pci_items;
2N/A
2N/A#define MSG_HOTPLUG_DISABLED \
2N/A "Error: hotplug service is probably not running, " \
2N/A "please use 'svcadm enable hotplug' to enable the service. " \
2N/A "See cfgadm_shp(1M) for more details."
2N/A
2N/A#define DEVICES_DIR "/devices"
2N/A#define SLASH "/"
2N/A#define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
2N/A
2N/A/*
2N/A * Set the version number
2N/A */
2N/Aint cfga_version = CFGA_HSL_V2;
2N/A
2N/A#ifdef DEBUG
2N/A#define SHP_DBG 1
2N/A#endif
2N/A
2N/A#if !defined(TEXT_DOMAIN)
2N/A#define TEXT_DOMAIN "SYS_TEST"
2N/A#endif
2N/A
2N/A/*
2N/A * DEBUGING LEVEL
2N/A *
2N/A * External routines: 1 - 2
2N/A * Internal routines: 3 - 4
2N/A */
2N/A#ifdef SHP_DBG
2N/Aint shp_debug = 1;
2N/A#define DBG(level, args) \
2N/A { if (shp_debug >= (level)) printf args; }
2N/A#define DBG_F(level, args) \
2N/A { if (shp_debug >= (level)) fprintf args; }
2N/A#else
2N/A#define DBG(level, args) /* nothing */
2N/A#define DBG_F(level, args) /* nothing */
2N/A#endif
2N/A
2N/A#define CMD_ACQUIRE 0
2N/A#define CMD_GETSTAT 1
2N/A#define CMD_LIST 2
2N/A#define CMD_SLOT_CONNECT 3
2N/A#define CMD_SLOT_DISCONNECT 4
2N/A#define CMD_SLOT_CONFIGURE 5
2N/A#define CMD_SLOT_UNCONFIGURE 6
2N/A#define CMD_SLOT_INSERT 7
2N/A#define CMD_SLOT_REMOVE 8
2N/A#define CMD_OPEN 9
2N/A#define CMD_FSTAT 10
2N/A#define ERR_NOMEM 11
2N/A#define ERR_AP_INVAL 12
2N/A#define ERR_AP_ERR 13
2N/A#define ERR_NO_DEVICE 14
2N/A
2N/Astatic char *
2N/Acfga_errstrs[] = {
2N/A /* n */ "acquire ",
2N/A /* n */ "get-status ",
2N/A /* n */ "list ",
2N/A /* n */ "connect ",
2N/A /* n */ "disconnect ",
2N/A /* n */ "configure ",
2N/A /* n */ "unconfigure ",
2N/A /* n */ "insert ",
2N/A /* n */ "remove ",
2N/A /* n */ "open ",
2N/A /* n */ "fstat ",
2N/A /* y */ "out of memory ",
2N/A /* y */ "invalid attachment point ",
2N/A /* y */ "invalid transition ",
2N/A /* y */ "no device is present ",
2N/A NULL
2N/A};
2N/A
2N/A#define HELP_HEADER 1
2N/A#define HELP_CONFIG 2
2N/A#define HELP_LED_CNTRL 3
2N/A#define HELP_UNKNOWN 4
2N/A#define SUCCESS 5
2N/A#define FAILED 6
2N/A#define UNKNOWN 7
2N/A
2N/A#define MAXLINE 256
2N/A
2N/Aextern int errno;
2N/A
2N/Astatic void cfga_err(char **errstring, int code);
2N/Astatic cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
2N/A char *slot_name, char **errstring);
2N/Astatic cfga_err_t check_options(const char *options);
2N/Astatic void cfga_msg(struct cfga_msg *msgp, const char *str);
2N/Astatic char *findlink(char *ap_phys_id);
2N/A
2N/Astatic char *
2N/Acfga_strs[] = {
2N/ANULL,
2N/A"\nPCI hotplug specific commands:",
2N/A"\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
2N/A"ap_id [ap_id...]",
2N/A"\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
2N/A"\tunknown command or option: ",
2N/A"success ",
2N/A"failed ",
2N/A"unknown",
2N/ANULL
2N/A};
2N/A
2N/A#define MAX_FORMAT 80
2N/A
2N/A#define ENABLE_SLOT 0
2N/A#define DISABLE_SLOT 1
2N/A#define ENABLE_AUTOCNF 2
2N/A#define DISABLE_AUTOCNF 3
2N/A#define LED 4
2N/A#define MODE 5
2N/A
2N/Atypedef enum { PCIEHPC_FAULT_LED, PCIEHPC_POWER_LED, PCIEHPC_ATTN_LED,
2N/A PCIEHPC_ACTIVE_LED} pciehpc_led_t;
2N/A
2N/Atypedef enum { PCIEHPC_BOARD_UNKNOWN, PCIEHPC_BOARD_PCI_HOTPLUG }
2N/A pciehpc_board_type_t;
2N/A
2N/A/*
2N/A * Board Type
2N/A */
2N/Astatic char *
2N/Aboard_strs[] = {
2N/A /* n */ "???", /* PCIEHPC_BOARD_UNKNOWN */
2N/A /* n */ "hp", /* PCIEHPC_BOARD_PCI_HOTPLUG */
2N/A /* n */ NULL
2N/A};
2N/A
2N/A/*
2N/A * HW functions
2N/A */
2N/Astatic char *
2N/Afunc_strs[] = {
2N/A /* n */ "enable_slot",
2N/A /* n */ "disable_slot",
2N/A /* n */ "enable_autoconfig",
2N/A /* n */ "disable_autoconfig",
2N/A /* n */ "led",
2N/A /* n */ "mode",
2N/A /* n */ NULL
2N/A};
2N/A
2N/A/*
2N/A * LED strings
2N/A */
2N/Astatic char *
2N/Aled_strs[] = {
2N/A /* n */ "fault", /* PCIEHPC_FAULT_LED */
2N/A /* n */ "power", /* PCIEHPC_POWER_LED */
2N/A /* n */ "attn", /* PCIEHPC_ATTN_LED */
2N/A /* n */ "active", /* PCIEHPC_ACTIVE_LED */
2N/A /* n */ NULL
2N/A};
2N/A
2N/Astatic char *
2N/Aled_strs2[] = {
2N/A /* n */ PCIEHPC_PROP_LED_FAULT, /* PCIEHPC_FAULT_LED */
2N/A /* n */ PCIEHPC_PROP_LED_POWER, /* PCIEHPC_POWER_LED */
2N/A /* n */ PCIEHPC_PROP_LED_ATTN, /* PCIEHPC_ATTN_LED */
2N/A /* n */ PCIEHPC_PROP_LED_ACTIVE, /* PCIEHPC_ACTIVE_LED */
2N/A /* n */ NULL
2N/A};
2N/A
2N/A#define FAULT 0
2N/A#define POWER 1
2N/A#define ATTN 2
2N/A#define ACTIVE 3
2N/A
2N/Astatic char *
2N/Amode_strs[] = {
2N/A /* n */ "off", /* OFF */
2N/A /* n */ "on", /* ON */
2N/A /* n */ "blink", /* BLINK */
2N/A /* n */ NULL
2N/A};
2N/A
2N/A#define OFF 0
2N/A#define ON 1
2N/A#define BLINK 2
2N/A
2N/A#define cfga_errstrs(i) cfga_errstrs[(i)]
2N/A
2N/A#define cfga_eid(a, b) (((a) << 8) + (b))
2N/A#define MAXDEVS 32
2N/A
2N/Atypedef enum {
2N/A SOLARIS_SLT_NAME,
2N/A PROM_SLT_NAME
2N/A} slt_name_src_t;
2N/A
2N/Astruct searcharg {
2N/A char *devpath;
2N/A char slotnames[MAXDEVS][MAXNAMELEN];
2N/A int minor;
2N/A di_prom_handle_t promp;
2N/A slt_name_src_t slt_name_src;
2N/A};
2N/A
2N/Astatic void *private_check;
2N/A
2N/A/*
2N/A * Return the corresponding hp node for a given ap_id, it is the caller's
2N/A * responsibility to call hp_fini() to free the snapshot.
2N/A */
2N/Astatic cfga_err_t
2N/Aphyspath2node(const char *physpath, char **errstring, hp_node_t *nodep)
2N/A{
2N/A char *rpath;
2N/A char *cp;
2N/A hp_node_t node;
2N/A size_t len;
2N/A
2N/A if (getuid() != 0 && geteuid() != 0)
2N/A return (CFGA_ERROR);
2N/A
2N/A if ((rpath = malloc(strlen(physpath) + 1)) == NULL) {
2N/A cfga_err(errstring, ERR_NOMEM);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A (void) strcpy(rpath, physpath);
2N/A
2N/A /* Remove devices prefix (if any) */
2N/A len = strlen(DEVICES_DIR);
2N/A if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
2N/A (void) memmove(rpath, rpath + len,
2N/A strlen(rpath + len) + 1);
2N/A }
2N/A
2N/A /* Remove dynamic component if any */
2N/A if ((cp = GET_DYN(rpath)) != NULL) {
2N/A *cp = '\0';
2N/A }
2N/A
2N/A /* Remove minor name (if any) */
2N/A if ((cp = strrchr(rpath, ':')) == NULL) {
2N/A free(rpath);
2N/A return (CFGA_INVAL);
2N/A }
2N/A
2N/A *cp = '\0';
2N/A cp++;
2N/A
2N/A DBG(1, ("rpath=%s,cp=%s\n", rpath, cp));
2N/A if ((node = hp_init(rpath, cp, 0)) == NULL) {
2N/A if (errno == EBADF) {
2N/A /* No reponse to operations on the door file. */
2N/A assert(errstring != NULL);
2N/A *errstring = strdup(MSG_HOTPLUG_DISABLED);
2N/A free(rpath);
2N/A return (CFGA_NOTSUPP);
2N/A }
2N/A free(rpath);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A free(rpath);
2N/A
2N/A *nodep = node;
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/Atypedef struct error_size_cb_arg {
2N/A size_t rsrc_width;
2N/A size_t info_width;
2N/A int cnt;
2N/A} error_size_cb_arg_t;
2N/A
2N/A/*
2N/A * Callback function for hp_traverse(), to sum up the
2N/A * maximum length for error message display.
2N/A */
2N/Astatic int
2N/Aerror_sizeup_cb(hp_node_t node, void *arg)
2N/A{
2N/A error_size_cb_arg_t *sizearg = (error_size_cb_arg_t *)arg;
2N/A size_t len;
2N/A
2N/A /* Only process USAGE nodes */
2N/A if (hp_type(node) != HP_NODE_USAGE)
2N/A return (HP_WALK_CONTINUE);
2N/A
2N/A sizearg->cnt++;
2N/A
2N/A /* size up resource name */
2N/A len = strlen(hp_name(node));
2N/A if (sizearg->rsrc_width < len)
2N/A sizearg->rsrc_width = len;
2N/A
2N/A /* size up usage description */
2N/A len = strlen(hp_usage(node));
2N/A if (sizearg->info_width < len)
2N/A sizearg->info_width = len;
2N/A
2N/A return (HP_WALK_CONTINUE);
2N/A}
2N/A
2N/Atypedef struct error_sum_cb_arg {
2N/A char **table;
2N/A char *format;
2N/A} error_sum_cb_arg_t;
2N/A
2N/A/*
2N/A * Callback function for hp_traverse(), to add the error
2N/A * message to he table.
2N/A */
2N/Astatic int
2N/Aerror_sumup_cb(hp_node_t node, void *arg)
2N/A{
2N/A error_sum_cb_arg_t *sumarg = (error_sum_cb_arg_t *)arg;
2N/A char **table = sumarg->table;
2N/A char *format = sumarg->format;
2N/A
2N/A /* Only process USAGE nodes */
2N/A if (hp_type(node) != HP_NODE_USAGE)
2N/A return (HP_WALK_CONTINUE);
2N/A
2N/A (void) strcat(*table, "\n");
2N/A (void) sprintf(&((*table)[strlen(*table)]),
2N/A format, hp_name(node), hp_usage(node));
2N/A
2N/A return (HP_WALK_CONTINUE);
2N/A}
2N/A
2N/A/*
2N/A * Takes an opaque rcm_info_t pointer and a character pointer, and appends
2N/A * the rcm_info_t data in the form of a table to the given character pointer.
2N/A */
2N/Astatic void
2N/Apci_rcm_info_table(hp_node_t node, char **table)
2N/A{
2N/A int i;
2N/A size_t w;
2N/A size_t width = 0;
2N/A size_t w_rsrc = 0;
2N/A size_t w_info = 0;
2N/A size_t table_size = 0;
2N/A uint_t tuples = 0;
2N/A char *rsrc;
2N/A char *info;
2N/A char *newtable;
2N/A static char format[MAX_FORMAT];
2N/A error_size_cb_arg_t sizearg;
2N/A error_sum_cb_arg_t sumarg;
2N/A
2N/A /* Protect against invalid arguments */
2N/A if (table == NULL)
2N/A return;
2N/A
2N/A /* Set localized table header strings */
2N/A rsrc = dgettext(TEXT_DOMAIN, "Resource");
2N/A info = dgettext(TEXT_DOMAIN, "Information");
2N/A
2N/A /* A first pass, to size up the RCM information */
2N/A sizearg.rsrc_width = strlen(rsrc);
2N/A sizearg.info_width = strlen(info);
2N/A sizearg.cnt = 0;
2N/A (void) hp_traverse(node, &sizearg, error_sizeup_cb);
2N/A
2N/A /* If nothing was sized up above, stop early */
2N/A if (sizearg.cnt == 0)
2N/A return;
2N/A
2N/A w_rsrc = sizearg.rsrc_width;
2N/A w_info = sizearg.info_width;
2N/A tuples = sizearg.cnt;
2N/A
2N/A /* Adjust column widths for column headings */
2N/A if ((w = strlen(rsrc)) > w_rsrc)
2N/A w_rsrc = w;
2N/A else if ((w_rsrc - w) % 2)
2N/A w_rsrc++;
2N/A if ((w = strlen(info)) > w_info)
2N/A w_info = w;
2N/A else if ((w_info - w) % 2)
2N/A w_info++;
2N/A
2N/A /*
2N/A * Compute the total line width of each line,
2N/A * accounting for intercolumn spacing.
2N/A */
2N/A width = w_info + w_rsrc + 4;
2N/A
2N/A /* Allocate space for the table */
2N/A table_size = (2 + tuples) * (width + 1) + 2;
2N/A if (*table == NULL) {
2N/A /* zero fill for the strcat() call below */
2N/A *table = calloc(table_size, sizeof (char));
2N/A if (*table == NULL)
2N/A return;
2N/A } else {
2N/A newtable = realloc(*table, strlen(*table) + table_size);
2N/A if (newtable == NULL)
2N/A return;
2N/A else
2N/A *table = newtable;
2N/A }
2N/A
2N/A /* Place a table header into the string */
2N/A
2N/A /* The resource header */
2N/A (void) strcat(*table, "\n");
2N/A w = strlen(rsrc);
2N/A for (i = 0; i < ((w_rsrc - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A (void) strcat(*table, rsrc);
2N/A for (i = 0; i < ((w_rsrc - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A
2N/A /* The information header */
2N/A (void) strcat(*table, " ");
2N/A w = strlen(info);
2N/A for (i = 0; i < ((w_info - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A (void) strcat(*table, info);
2N/A for (i = 0; i < ((w_info - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A /* Underline the headers */
2N/A (void) strcat(*table, "\n");
2N/A for (i = 0; i < w_rsrc; i++)
2N/A (void) strcat(*table, "-");
2N/A (void) strcat(*table, " ");
2N/A for (i = 0; i < w_info; i++)
2N/A (void) strcat(*table, "-");
2N/A
2N/A /* Construct the format string */
2N/A (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
2N/A (int)w_rsrc, (int)w_info);
2N/A
2N/A /* Add the tuples to the table string */
2N/A sumarg.table = table;
2N/A sumarg.format = format;
2N/A (void) hp_traverse(node, &sumarg, error_sumup_cb);
2N/A}
2N/A
2N/A/*
2N/A * Figure out the target kernel state for a given cfgadm
2N/A * change-state operation.
2N/A */
2N/Astatic cfga_err_t
2N/Acfga_target_state(cfga_cmd_t state_change_cmd, int *state)
2N/A{
2N/A switch (state_change_cmd) {
2N/A case CFGA_CMD_CONNECT:
2N/A *state = DDI_HP_CN_STATE_POWERED;
2N/A break;
2N/A case CFGA_CMD_DISCONNECT:
2N/A *state = DDI_HP_CN_STATE_PRESENT;
2N/A break;
2N/A case CFGA_CMD_CONFIGURE:
2N/A *state = DDI_HP_CN_STATE_ENABLED;
2N/A break;
2N/A case CFGA_CMD_UNCONFIGURE:
2N/A *state = DDI_HP_CN_STATE_POWERED;
2N/A break;
2N/A default:
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*
2N/A * Translate kernel state to cfgadm receptacle state and occupant state.
2N/A */
2N/Astatic cfga_err_t
2N/Acfga_get_state(hp_node_t connector, ap_rstate_t *rs, ap_ostate_t *os)
2N/A{
2N/A int state;
2N/A hp_node_t port;
2N/A
2N/A state = hp_state(connector);
2N/A
2N/A /* Receptacle state */
2N/A switch (state) {
2N/A case DDI_HP_CN_STATE_EMPTY:
2N/A *rs = AP_RSTATE_EMPTY;
2N/A break;
2N/A case DDI_HP_CN_STATE_PRESENT:
2N/A *rs = AP_RSTATE_DISCONNECTED;
2N/A break;
2N/A case DDI_HP_CN_STATE_POWERED:
2N/A case DDI_HP_CN_STATE_ENABLED:
2N/A *rs = AP_RSTATE_CONNECTED;
2N/A break;
2N/A /*
2N/A * Connector state can only be one of
2N/A * Empty, Present, Powered, Enabled.
2N/A */
2N/A default:
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * Occupant state
2N/A */
2N/A port = hp_child(connector);
2N/A while (port != NULL) {
2N/A DBG(1, ("cfga_get_state:(%x)\n", hp_state(port)));
2N/A
2N/A /*
2N/A * Mark occupant state as "configured" if at least one of the
2N/A * associated ports is at state "offline" or above. Driver
2N/A * attach ("online" state) is not necessary here.
2N/A */
2N/A if (hp_state(port) >= DDI_HP_CN_STATE_OFFLINE)
2N/A break;
2N/A
2N/A port = hp_sibling(port);
2N/A }
2N/A
2N/A if (port != NULL)
2N/A *os = AP_OSTATE_CONFIGURED;
2N/A else
2N/A *os = AP_OSTATE_UNCONFIGURED;
2N/A
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*
2N/A * Transitional Diagram:
2N/A *
2N/A * empty unconfigure
2N/A * (remove) ^| (physically insert card)
2N/A * |V
2N/A * disconnect configure
2N/A * "-c DISCONNECT" ^| "-c CONNECT"
2N/A * |V "-c CONFIGURE"
2N/A * connect unconfigure -> connect configure
2N/A * <-
2N/A * "-c UNCONFIGURE"
2N/A *
2N/A */
2N/A/*ARGSUSED*/
2N/Acfga_err_t
2N/Acfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
2N/A const char *options, struct cfga_confirm *confp,
2N/A struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
2N/A{
2N/A int rv, state, new_state;
2N/A uint_t hpflags = 0;
2N/A hp_node_t node;
2N/A hp_node_t results = NULL;
2N/A
2N/A if ((rv = check_options(options)) != CFGA_OK) {
2N/A return (rv);
2N/A }
2N/A
2N/A if (errstring != NULL)
2N/A *errstring = NULL;
2N/A
2N/A rv = CFGA_OK;
2N/A DBG(1, ("cfga_change_state:(%s)\n", ap_id));
2N/A
2N/A rv = physpath2node(ap_id, errstring, &node);
2N/A if (rv != CFGA_OK)
2N/A return (rv);
2N/A
2N/A /*
2N/A * Check for the FORCE flag. It is only used
2N/A * for DISCONNECT or UNCONFIGURE state changes.
2N/A */
2N/A if (flags & CFGA_FLAG_FORCE)
2N/A hpflags |= HPFORCE;
2N/A
2N/A state = hp_state(node);
2N/A
2N/A /*
2N/A * Which state should we drive to ?
2N/A */
2N/A if ((state_change_cmd != CFGA_CMD_LOAD) &&
2N/A (state_change_cmd != CFGA_CMD_UNLOAD)) {
2N/A if (cfga_target_state(state_change_cmd,
2N/A &new_state) != CFGA_OK) {
2N/A hp_fini(node);
2N/A return (CFGA_ERROR);
2N/A }
2N/A }
2N/A
2N/A DBG(1, ("cfga_change_state: state is %d\n", state));
2N/A switch (state_change_cmd) {
2N/A case CFGA_CMD_CONNECT:
2N/A DBG(1, ("connect\n"));
2N/A if (state == DDI_HP_CN_STATE_EMPTY) {
2N/A cfga_err(errstring, ERR_NO_DEVICE);
2N/A rv = CFGA_INVAL;
2N/A } else if (state == DDI_HP_CN_STATE_PRESENT) {
2N/A /* Connect the slot */
2N/A if (hp_set_state(node, 0, new_state, NULL,
2N/A &results) != 0) {
2N/A rv = CFGA_ERROR;
2N/A cfga_err(errstring, CMD_SLOT_CONNECT);
2N/A }
2N/A }
2N/A break;
2N/A
2N/A case CFGA_CMD_DISCONNECT:
2N/A DBG(1, ("disconnect\n"));
2N/A if (state == DDI_HP_CN_STATE_EMPTY) {
2N/A cfga_err(errstring, ERR_NO_DEVICE);
2N/A rv = CFGA_INVAL;
2N/A } else if (state > DDI_HP_CN_STATE_PRESENT) {
2N/A /* Disconnect the slot */
2N/A rv = hp_set_state(node, hpflags, new_state, NULL,
2N/A &results);
2N/A if (rv != 0) {
2N/A if (rv == EBUSY)
2N/A rv = CFGA_BUSY;
2N/A else
2N/A rv = CFGA_ERROR;
2N/A
2N/A if (results) {
2N/A pci_rcm_info_table(results, errstring);
2N/A hp_fini(results);
2N/A } else {
2N/A cfga_err(errstring,
2N/A CMD_SLOT_DISCONNECT);
2N/A }
2N/A }
2N/A }
2N/A break;
2N/A
2N/A case CFGA_CMD_CONFIGURE:
2N/A /*
2N/A * for multi-func device we allow multiple
2N/A * configure on the same slot because one
2N/A * func can be configured and other one won't
2N/A */
2N/A DBG(1, ("configure\n"));
2N/A if (state == DDI_HP_CN_STATE_EMPTY) {
2N/A cfga_err(errstring, ERR_NO_DEVICE);
2N/A rv = CFGA_INVAL;
2N/A } else if (hp_set_state(node, 0, new_state, NULL,
2N/A &results) != 0) {
2N/A rv = CFGA_ERROR;
2N/A cfga_err(errstring, CMD_SLOT_CONFIGURE);
2N/A }
2N/A break;
2N/A
2N/A case CFGA_CMD_UNCONFIGURE:
2N/A DBG(1, ("unconfigure\n"));
2N/A if (state == DDI_HP_CN_STATE_EMPTY) {
2N/A cfga_err(errstring, ERR_NO_DEVICE);
2N/A rv = CFGA_INVAL;
2N/A } else if (state >= DDI_HP_CN_STATE_ENABLED) {
2N/A rv = hp_set_state(node, hpflags, new_state, NULL,
2N/A &results);
2N/A if (rv != 0) {
2N/A if (rv == EBUSY)
2N/A rv = CFGA_BUSY;
2N/A else
2N/A rv = CFGA_ERROR;
2N/A
2N/A if (results) {
2N/A pci_rcm_info_table(results, errstring);
2N/A hp_fini(results);
2N/A } else {
2N/A cfga_err(errstring,
2N/A CMD_SLOT_UNCONFIGURE);
2N/A }
2N/A }
2N/A }
2N/A DBG(1, ("unconfigure rv:(%i)\n", rv));
2N/A break;
2N/A
2N/A case CFGA_CMD_LOAD:
2N/A /* do nothing, just produce error msg as is */
2N/A if (state < DDI_HP_CN_STATE_POWERED) {
2N/A rv = CFGA_ERROR;
2N/A cfga_err(errstring, CMD_SLOT_INSERT);
2N/A } else {
2N/A cfga_err(errstring, ERR_AP_ERR);
2N/A rv = CFGA_INVAL;
2N/A }
2N/A break;
2N/A
2N/A case CFGA_CMD_UNLOAD:
2N/A /* do nothing, just produce error msg as is */
2N/A if (state < DDI_HP_CN_STATE_POWERED) {
2N/A rv = CFGA_ERROR;
2N/A cfga_err(errstring, CMD_SLOT_REMOVE);
2N/A } else {
2N/A cfga_err(errstring, ERR_AP_ERR);
2N/A rv = CFGA_INVAL;
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A rv = CFGA_OPNOTSUPP;
2N/A break;
2N/A }
2N/A
2N/A hp_fini(node);
2N/A return (rv);
2N/A}
2N/A
2N/Achar *
2N/Aget_val_from_result(char *result)
2N/A{
2N/A char *tmp;
2N/A
2N/A tmp = strchr(result, '=');
2N/A if (tmp == NULL)
2N/A return (NULL);
2N/A
2N/A tmp++;
2N/A return (tmp);
2N/A}
2N/A
2N/Astatic cfga_err_t
2N/Aprt_led_mode(const char *ap_id, int repeat, char **errstring,
2N/A struct cfga_msg *msgp)
2N/A{
2N/A pciehpc_led_t led;
2N/A hp_node_t node;
2N/A char *buff;
2N/A char *cp, line[MAXLINE];
2N/A char *tmp;
2N/A char *format;
2N/A char *result;
2N/A int i, n, rv;
2N/A int len = MAXLINE;
2N/A
2N/A pciehpc_led_t states[] = {
2N/A PCIEHPC_POWER_LED,
2N/A PCIEHPC_FAULT_LED,
2N/A PCIEHPC_ATTN_LED,
2N/A PCIEHPC_ACTIVE_LED
2N/A };
2N/A
2N/A DBG(1, ("prt_led_mod function\n"));
2N/A if (!repeat)
2N/A cfga_msg(msgp, "Ap_Id\t\t\tLed");
2N/A
2N/A rv = physpath2node(ap_id, errstring, &node);
2N/A if (rv != CFGA_OK)
2N/A return (rv);
2N/A
2N/A if ((buff = malloc(MAXPATHLEN)) == NULL) {
2N/A hp_fini(node);
2N/A cfga_err(errstring, ERR_NOMEM);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A (void) memset(buff, 0, MAXPATHLEN);
2N/A
2N/A if (fix_ap_name(buff, ap_id, hp_name(node),
2N/A errstring) != CFGA_OK) {
2N/A hp_fini(node);
2N/A free(buff);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A cp = line;
2N/A (void) snprintf(cp, len, "%s\t\t", buff);
2N/A len -= strlen(cp);
2N/A cp += strlen(cp);
2N/A
2N/A free(buff);
2N/A
2N/A n = sizeof (states)/sizeof (pciehpc_led_t);
2N/A for (i = 0; i < n; i++) {
2N/A led = states[i];
2N/A
2N/A format = (i == n - 1) ? "%s=%s" : "%s=%s,";
2N/A if (hp_get_private(node, led_strs2[led], &result) != 0) {
2N/A (void) snprintf(cp, len, format,
2N/A led_strs[led], cfga_strs[UNKNOWN]);
2N/A len -= strlen(cp);
2N/A cp += strlen(cp);
2N/A DBG(1, ("%s:%s\n", led_strs[led], cfga_strs[UNKNOWN]));
2N/A } else {
2N/A /*
2N/A * hp_get_private() will return back things like
2N/A * "led_fault=off", transform it to cfgadm desired
2N/A * format.
2N/A */
2N/A tmp = get_val_from_result(result);
2N/A if (tmp == NULL) {
2N/A free(result);
2N/A hp_fini(node);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A (void) snprintf(cp, len, format,
2N/A led_strs[led], tmp);
2N/A len -= strlen(cp);
2N/A cp += strlen(cp);
2N/A DBG(1, ("%s:%s\n", led_strs[led], tmp));
2N/A free(result);
2N/A }
2N/A }
2N/A
2N/A cfga_msg(msgp, line); /* print the message */
2N/A
2N/A hp_fini(node);
2N/A
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Acfga_err_t
2N/Acfga_private_func(const char *function, const char *ap_id,
2N/A const char *options, struct cfga_confirm *confp,
2N/A struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
2N/A{
2N/A char *str;
2N/A int len, i = 0, repeat = 0;
2N/A char buf[MAXNAMELEN];
2N/A char ptr;
2N/A cfga_err_t rv;
2N/A char *led, *mode;
2N/A hp_node_t node;
2N/A char *result;
2N/A
2N/A DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
2N/A DBG(2, (" options: %s\n", (options == NULL)?"null":options));
2N/A DBG(2, (" confp: %x\n", confp));
2N/A DBG(2, (" cfga_msg: %x\n", cfga_msg));
2N/A DBG(2, (" flag: %d\n", flags));
2N/A
2N/A if ((rv = check_options(options)) != CFGA_OK) {
2N/A return (rv);
2N/A }
2N/A
2N/A if (private_check == confp)
2N/A repeat = 1;
2N/A else
2N/A private_check = (void*)confp;
2N/A
2N/A for (i = 0, str = func_strs[i], len = strlen(str);
2N/A func_strs[i] != NULL; i++) {
2N/A str = func_strs[i];
2N/A len = strlen(str);
2N/A if (strncmp(function, str, len) == 0)
2N/A break;
2N/A }
2N/A
2N/A switch (i) {
2N/A case ENABLE_SLOT:
2N/A case DISABLE_SLOT:
2N/A /* pass through */
2N/A case ENABLE_AUTOCNF:
2N/A case DISABLE_AUTOCNF:
2N/A /* no action needed */
2N/A return (CFGA_OK);
2N/A break;
2N/A case LED:
2N/A /* set mode */
2N/A ptr = function[len++];
2N/A if (ptr == '=') {
2N/A str = (char *)function;
2N/A for (str = (str+len++), i = 0; *str != ',';
2N/A i++, str++) {
2N/A if (i == (MAXNAMELEN - 1))
2N/A break;
2N/A
2N/A buf[i] = *str;
2N/A DBG_F(2, (stdout, "%c\n", buf[i]));
2N/A }
2N/A buf[i] = '\0'; str++;
2N/A DBG(2, ("buf = %s\n", buf));
2N/A
2N/A /* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
2N/A if (strcmp(buf, led_strs[POWER]) == 0)
2N/A led = PCIEHPC_PROP_LED_POWER;
2N/A else if (strcmp(buf, led_strs[FAULT]) == 0)
2N/A led = PCIEHPC_PROP_LED_FAULT;
2N/A else if (strcmp(buf, led_strs[ATTN]) == 0)
2N/A led = PCIEHPC_PROP_LED_ATTN;
2N/A else if (strcmp(buf, led_strs[ACTIVE]) == 0)
2N/A led = PCIEHPC_PROP_LED_ACTIVE;
2N/A else return (CFGA_INVAL);
2N/A
2N/A len = strlen(func_strs[MODE]);
2N/A if ((strncmp(str, func_strs[MODE], len) == 0) &&
2N/A (*(str+(len)) == '=')) {
2N/A for (str = (str+(++len)), i = 0;
2N/A *str != NULL; i++, str++) {
2N/A buf[i] = *str;
2N/A }
2N/A }
2N/A buf[i] = '\0';
2N/A DBG(2, ("buf_mode= %s\n", buf));
2N/A
2N/A /* ON = 1, OFF = 0 */
2N/A if (strcmp(buf, mode_strs[ON]) == 0)
2N/A mode = PCIEHPC_PROP_VALUE_ON;
2N/A else if (strcmp(buf, mode_strs[OFF]) == 0)
2N/A mode = PCIEHPC_PROP_VALUE_OFF;
2N/A else if (strcmp(buf, mode_strs[BLINK]) == 0)
2N/A mode = PCIEHPC_PROP_VALUE_BLINK;
2N/A else return (CFGA_INVAL);
2N/A
2N/A /* sendin */
2N/A (void) memset(buf, 0, sizeof (buf));
2N/A (void) snprintf(buf, sizeof (buf), "%s=%s",
2N/A led, mode);
2N/A buf[MAXNAMELEN - 1] = '\0';
2N/A
2N/A break;
2N/A } else if (ptr == '\0') {
2N/A /* print mode */
2N/A DBG(1, ("Print mode\n"));
2N/A return (prt_led_mode(ap_id, repeat, errstring,
2N/A msgp));
2N/A }
2N/A default:
2N/A DBG(1, ("default\n"));
2N/A errno = EINVAL;
2N/A return (CFGA_INVAL);
2N/A }
2N/A
2N/A rv = physpath2node(ap_id, errstring, &node);
2N/A if (rv != CFGA_OK)
2N/A return (rv);
2N/A
2N/A if (hp_set_private(node, buf, &result) != 0) {
2N/A hp_fini(node);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A hp_fini(node);
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Acfga_err_t cfga_test(const char *ap_id, const char *options,
2N/A struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
2N/A{
2N/A cfga_err_t rv;
2N/A if (errstring != NULL)
2N/A *errstring = NULL;
2N/A
2N/A if ((rv = check_options(options)) != CFGA_OK) {
2N/A return (rv);
2N/A }
2N/A
2N/A DBG(1, ("cfga_test:(%s)\n", ap_id));
2N/A /* will need to implement pci CTRL command */
2N/A return (CFGA_NOTSUPP);
2N/A}
2N/A
2N/A/*
2N/A * The slot-names property describes the external labeling of add-in slots.
2N/A * This property is an encoded array, an integer followed by a list of
2N/A * strings. The return value from di_prop_lookup_ints for slot-names is -1.
2N/A * The expected return value should be the number of elements.
2N/A * Di_prop_decode_common does not decode encoded data from software,
2N/A * such as the solaris device tree, unlike from the prom.
2N/A * Di_prop_decode_common takes the size of the encoded data and mods
2N/A * it with the size of int. The size of the encoded data for slot-names is 9
2N/A * and the size of int is 4, yielding a non zero result. A value of -1 is used
2N/A * to indicate that the number of elements can not be determined.
2N/A * Di_prop_decode_common can be modified to decode encoded data from the solaris
2N/A * device tree.
2N/A */
2N/Astatic int
2N/Afixup_slotname(int rval, int *intp, struct searcharg *slotarg)
2N/A{
2N/A if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
2N/A return (DI_WALK_TERMINATE);
2N/A } else {
2N/A int i;
2N/A char *tmptr = (char *)(intp+1);
2N/A DBG(1, ("slot-bitmask: %x \n", *intp));
2N/A
2N/A rval = (rval -1) * 4;
2N/A
2N/A for (i = 0; i <= slotarg->minor; i++) {
2N/A DBG(2, ("curr slot-name: %s \n", tmptr));
2N/A
2N/A if (i >= MAXDEVS)
2N/A return (DI_WALK_TERMINATE);
2N/A
2N/A if ((*intp >> i) & 1) {
2N/A /* assign tmptr */
2N/A DBG(2, ("slot-name: %s \n", tmptr));
2N/A if (i == slotarg->minor)
2N/A (void) strcpy(slotarg->slotnames[i],
2N/A tmptr);
2N/A /* wind tmptr to next \0 */
2N/A while (*tmptr != '\0') {
2N/A tmptr++;
2N/A }
2N/A tmptr++;
2N/A } else {
2N/A /* point at unknown string */
2N/A if (i == slotarg->minor)
2N/A (void) strcpy(slotarg->slotnames[i],
2N/A "unknown");
2N/A }
2N/A }
2N/A }
2N/A return (DI_WALK_TERMINATE);
2N/A}
2N/A
2N/Astatic int
2N/Afind_slotname(di_node_t din, di_minor_t dim, void *arg)
2N/A{
2N/A struct searcharg *slotarg = (struct searcharg *)arg;
2N/A di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
2N/A di_prom_prop_t prom_prop;
2N/A di_prop_t solaris_prop;
2N/A int *intp, rval;
2N/A char *devname;
2N/A char fulldevname[MAXNAMELEN];
2N/A
2N/A slotarg->minor = dim->dev_minor % 256;
2N/A
2N/A DBG(2, ("minor number:(%i)\n", slotarg->minor));
2N/A DBG(2, ("hot plug slots found so far:(%i)\n", 0));
2N/A
2N/A if ((devname = di_devfs_path(din)) != NULL) {
2N/A (void) snprintf(fulldevname, MAXNAMELEN,
2N/A "/devices%s:%s", devname, di_minor_name(dim));
2N/A di_devfs_path_free(devname);
2N/A }
2N/A
2N/A if (strcmp(fulldevname, slotarg->devpath) == 0) {
2N/A
2N/A /*
2N/A * Check the Solaris device tree first
2N/A * in the case of a DR operation
2N/A */
2N/A solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
2N/A while (solaris_prop != DI_PROP_NIL) {
2N/A if (strcmp("slot-names", di_prop_name(solaris_prop))
2N/A == 0) {
2N/A rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
2N/A din, di_prop_name(solaris_prop), &intp);
2N/A slotarg->slt_name_src = SOLARIS_SLT_NAME;
2N/A
2N/A return (fixup_slotname(rval, intp, slotarg));
2N/A }
2N/A solaris_prop = di_prop_hw_next(din, solaris_prop);
2N/A }
2N/A
2N/A /*
2N/A * Check the prom device tree which is populated at boot.
2N/A * If this fails, give up and set the slot name to null.
2N/A */
2N/A prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
2N/A while (prom_prop != DI_PROM_PROP_NIL) {
2N/A if (strcmp("slot-names", di_prom_prop_name(prom_prop))
2N/A == 0) {
2N/A rval = di_prom_prop_lookup_ints(ph,
2N/A din, di_prom_prop_name(prom_prop), &intp);
2N/A slotarg->slt_name_src = PROM_SLT_NAME;
2N/A
2N/A return (fixup_slotname(rval, intp, slotarg));
2N/A }
2N/A prom_prop = di_prom_prop_next(ph, din, prom_prop);
2N/A }
2N/A *slotarg->slotnames[slotarg->minor] = '\0';
2N/A return (DI_WALK_TERMINATE);
2N/A } else
2N/A return (DI_WALK_CONTINUE);
2N/A}
2N/A
2N/Astatic int
2N/Afind_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
2N/A{
2N/A di_node_t root_node;
2N/A
2N/A DBG(1, ("find_physical_slot_names\n"));
2N/A
2N/A if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
2N/A == DI_NODE_NIL) {
2N/A DBG(1, ("di_init() failed\n"));
2N/A return (-1);
2N/A }
2N/A
2N/A slotarg->devpath = (char *)devcomp;
2N/A
2N/A if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
2N/A DBG(1, ("di_prom_init() failed\n"));
2N/A di_fini(root_node);
2N/A return (-1);
2N/A }
2N/A
2N/A (void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
2N/A 0, (void *)slotarg, find_slotname);
2N/A
2N/A di_prom_fini(slotarg->promp);
2N/A di_fini(root_node);
2N/A if (slotarg->slotnames[0] != NULL)
2N/A return (0);
2N/A else
2N/A return (-1);
2N/A}
2N/A
2N/Astatic void
2N/Aget_type(const char *boardtype, const char *cardtype, char *buf)
2N/A{
2N/A/* for type string assembly in get_type() */
2N/A#define TPCT(s) (void) strlcat(buf, (s), CFGA_TYPE_LEN)
2N/A
2N/A if (strcmp(cardtype, "unknown") == 0) {
2N/A TPCT("unknown");
2N/A return;
2N/A }
2N/A
2N/A TPCT(cardtype);
2N/A TPCT("/");
2N/A
2N/A if (strcmp(boardtype, PCIEHPC_PROP_VALUE_PCIHOTPLUG) == 0)
2N/A TPCT(board_strs[PCIEHPC_BOARD_PCI_HOTPLUG]);
2N/A else
2N/A TPCT(board_strs[PCIEHPC_BOARD_UNKNOWN]);
2N/A}
2N/A
2N/A/*
2N/A * call-back function for di_devlink_walk
2N/A * if the link lives in /dev/cfg copy its name
2N/A */
2N/Astatic int
2N/Afound_devlink(di_devlink_t link, void *ap_log_id)
2N/A{
2N/A if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
2N/A /* copy everything but /dev/cfg/ */
2N/A (void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
2N/A DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
2N/A return (DI_WALK_TERMINATE);
2N/A }
2N/A return (DI_WALK_CONTINUE);
2N/A}
2N/A
2N/A/*
2N/A * Walk throught the cached /dev link tree looking for links to the ap
2N/A * if none are found return an error
2N/A */
2N/Astatic cfga_err_t
2N/Acheck_devlinks(char *ap_log_id, const char *ap_id)
2N/A{
2N/A di_devlink_handle_t hdl;
2N/A
2N/A DBG(1, ("check_devlinks: %s\n", ap_id));
2N/A
2N/A hdl = di_devlink_init(NULL, 0);
2N/A
2N/A if (strncmp("/devices/", ap_id, 9) == 0) {
2N/A /* ap_id is a valid minor_path with /devices prepended */
2N/A (void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
2N/A (void *)ap_log_id, found_devlink);
2N/A } else {
2N/A DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A (void) di_devlink_fini(&hdl);
2N/A
2N/A if (ap_log_id[0] != '\0')
2N/A return (CFGA_OK);
2N/A else
2N/A return (CFGA_ERROR);
2N/A}
2N/A
2N/A/*
2N/A * most of this is needed to compensate for
2N/A * differences between various platforms
2N/A */
2N/Astatic cfga_err_t
2N/Afix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
2N/A char **errstring)
2N/A{
2N/A char *buf;
2N/A char *tmp;
2N/A char *ptr;
2N/A
2N/A di_node_t ap_node;
2N/A
2N/A ap_log_id[0] = '\0';
2N/A
2N/A if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
2N/A return (CFGA_OK);
2N/A
2N/A DBG(1, ("fix_ap_name: %s\n", ap_id));
2N/A
2N/A if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
2N/A cfga_err(errstring, ERR_NOMEM);
2N/A DBG(1, ("malloc failed\n"));
2N/A return (CFGA_ERROR);
2N/A }
2N/A (void) strcpy(buf, ap_id);
2N/A tmp = buf + sizeof ("/devices") - 1;
2N/A
2N/A ptr = strchr(tmp, ':');
2N/A ptr[0] = '\0';
2N/A
2N/A DBG(1, ("fix_ap_name: %s\n", tmp));
2N/A
2N/A ap_node = di_init(tmp, DINFOMINOR);
2N/A if (ap_node == DI_NODE_NIL) {
2N/A cfga_err(errstring, ERR_AP_INVAL);
2N/A DBG(1, ("fix_ap_name: failed to snapshot node\n"));
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A (void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
2N/A di_driver_name(ap_node), di_instance(ap_node), slot_name);
2N/A
2N/A DBG(1, ("fix_ap_name: %s\n", ap_log_id));
2N/A
2N/A di_fini(ap_node);
2N/A
2N/A free(buf);
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Afindlink_cb(di_devlink_t devlink, void *arg)
2N/A{
2N/A (*(char **)arg) = strdup(di_devlink_path(devlink));
2N/A
2N/A return (DI_WALK_TERMINATE);
2N/A}
2N/A
2N/A/*
2N/A * returns an allocated string containing the full path to the devlink for
2N/A * <ap_phys_id> in the devlink database; we expect only one devlink per
2N/A * <ap_phys_id> so we return the first encountered
2N/A */
2N/Astatic char *
2N/Afindlink(char *ap_phys_id)
2N/A{
2N/A di_devlink_handle_t hdl;
2N/A char *path = NULL;
2N/A
2N/A hdl = di_devlink_init(NULL, 0);
2N/A
2N/A if (strncmp("/devices/", ap_phys_id, 9) == 0)
2N/A ap_phys_id += 8;
2N/A
2N/A (void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
2N/A (void *)&path, findlink_cb);
2N/A
2N/A (void) di_devlink_fini(&hdl);
2N/A return (path);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * returns CFGA_OK if it can succesfully retrieve the devlink info associated
2N/A * with devlink for <ap_phys_id> which will be returned through <ap_info>
2N/A */
2N/Acfga_err_t
2N/Aget_dli(char *dlpath, char *ap_info, int ap_info_sz)
2N/A{
2N/A int fd;
2N/A
2N/A fd = di_dli_openr(dlpath);
2N/A if (fd < 0)
2N/A return (CFGA_ERROR);
2N/A
2N/A (void) read(fd, ap_info, ap_info_sz);
2N/A ap_info[ap_info_sz - 1] = '\0';
2N/A
2N/A di_dli_close(fd);
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/Astatic cfga_err_t
2N/Acfga_get_condition(hp_node_t node, ap_condition_t *cond)
2N/A{
2N/A char *condition;
2N/A
2N/A /* "condition" bus specific commands */
2N/A if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
2N/A &condition) != 0) {
2N/A *cond = AP_COND_UNKNOWN;
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A condition = get_val_from_result(condition);
2N/A
2N/A if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
2N/A *cond = AP_COND_OK;
2N/A else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
2N/A *cond = AP_COND_FAILING;
2N/A else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
2N/A *cond = AP_COND_FAILED;
2N/A else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
2N/A *cond = AP_COND_UNUSABLE;
2N/A else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
2N/A *cond = AP_COND_UNKNOWN;
2N/A else
2N/A return (CFGA_ERROR);
2N/A
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Acfga_err_t
2N/Acfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
2N/A int *nlist, const char *options, const char *listopts, char **errstring,
2N/A cfga_flags_t flags)
2N/A{
2N/A char *boardtype;
2N/A char *cardtype;
2N/A struct searcharg slotname_arg;
2N/A int rv = CFGA_OK;
2N/A char *dlpath = NULL;
2N/A hp_node_t node;
2N/A ap_rstate_t rs;
2N/A ap_ostate_t os;
2N/A ap_condition_t cond;
2N/A
2N/A if ((rv = check_options(options)) != CFGA_OK) {
2N/A return (rv);
2N/A }
2N/A
2N/A if (errstring != NULL)
2N/A *errstring = NULL;
2N/A
2N/A DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
2N/A
2N/A if (cs == NULL || nlist == NULL) {
2N/A rv = CFGA_ERROR;
2N/A return (rv);
2N/A }
2N/A
2N/A *nlist = 1;
2N/A
2N/A if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
2N/A cfga_err(errstring, ERR_NOMEM);
2N/A DBG(1, ("malloc failed\n"));
2N/A rv = CFGA_ERROR;
2N/A return (rv);
2N/A }
2N/A (void) memset(*cs, 0, sizeof (cfga_list_data_t));
2N/A
2N/A rv = physpath2node(ap_id, errstring, &node);
2N/A if (rv != CFGA_OK) {
2N/A DBG(1, ("physpath2node failed\n"));
2N/A return (rv);
2N/A }
2N/A
2N/A if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
2N/A DBG(1, ("cfga_get_state failed\n"));
2N/A hp_fini(node);
2N/A return (CFGA_ERROR);
2N/A }
2N/A
2N/A switch (rs) {
2N/A case AP_RSTATE_EMPTY:
2N/A (*cs)->ap_r_state = CFGA_STAT_EMPTY;
2N/A DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
2N/A break;
2N/A case AP_RSTATE_DISCONNECTED:
2N/A (*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
2N/A DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
2N/A break;
2N/A case AP_RSTATE_CONNECTED:
2N/A (*cs)->ap_r_state = CFGA_STAT_CONNECTED;
2N/A DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
2N/A break;
2N/A default:
2N/A cfga_err(errstring, CMD_GETSTAT);
2N/A rv = CFGA_ERROR;
2N/A hp_fini(node);
2N/A return (rv);
2N/A }
2N/A
2N/A switch (os) {
2N/A case AP_OSTATE_CONFIGURED:
2N/A (*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
2N/A DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
2N/A break;
2N/A case AP_OSTATE_UNCONFIGURED:
2N/A (*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
2N/A DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
2N/A break;
2N/A default:
2N/A cfga_err(errstring, CMD_GETSTAT);
2N/A rv = CFGA_ERROR;
2N/A hp_fini(node);
2N/A return (rv);
2N/A }
2N/A
2N/A (void) cfga_get_condition(node, &cond);
2N/A
2N/A switch (cond) {
2N/A case AP_COND_OK:
2N/A (*cs)->ap_cond = CFGA_COND_OK;
2N/A DBG(2, ("ap_cond = CFGA_COND_OK\n"));
2N/A break;
2N/A case AP_COND_FAILING:
2N/A (*cs)->ap_cond = CFGA_COND_FAILING;
2N/A DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
2N/A break;
2N/A case AP_COND_FAILED:
2N/A (*cs)->ap_cond = CFGA_COND_FAILED;
2N/A DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
2N/A break;
2N/A case AP_COND_UNUSABLE:
2N/A (*cs)->ap_cond = CFGA_COND_UNUSABLE;
2N/A DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
2N/A break;
2N/A case AP_COND_UNKNOWN:
2N/A (*cs)->ap_cond = CFGA_COND_UNKNOWN;
2N/A DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
2N/A break;
2N/A default:
2N/A cfga_err(errstring, CMD_GETSTAT);
2N/A rv = CFGA_ERROR;
2N/A hp_fini(node);
2N/A return (rv);
2N/A }
2N/A /*
2N/A * We're not busy since the entrance into the kernel has been
2N/A * sync'ed via libhotplug.
2N/A */
2N/A (*cs)->ap_busy = 0;
2N/A
2N/A /* last change */
2N/A (*cs)->ap_status_time = hp_last_change(node);
2N/A
2N/A /* board type */
2N/A if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &boardtype) != 0)
2N/A boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
2N/A else
2N/A boardtype = get_val_from_result(boardtype);
2N/A
2N/A /* card type */
2N/A if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &cardtype) != 0)
2N/A cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
2N/A else
2N/A cardtype = get_val_from_result(cardtype);
2N/A
2N/A /* logical ap_id */
2N/A rv = fix_ap_name((*cs)->ap_log_id, ap_id,
2N/A hp_name(node), errstring);
2N/A DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
2N/A /* physical ap_id */
2N/A (void) strcpy((*cs)->ap_phys_id, ap_id); /* physical path of AP */
2N/A
2N/A /* information */
2N/A dlpath = findlink((*cs)->ap_phys_id);
2N/A if (dlpath != NULL) {
2N/A if (get_dli(dlpath, (*cs)->ap_info,
2N/A sizeof ((*cs)->ap_info)) != CFGA_OK)
2N/A (*cs)->ap_info[0] = '\0';
2N/A free(dlpath);
2N/A }
2N/A
2N/A if ((*cs)->ap_log_id[0] == '\0')
2N/A (void) strcpy((*cs)->ap_log_id, hp_name(node));
2N/A
2N/A if ((*cs)->ap_info[0] == '\0') {
2N/A /* slot_names of bus node */
2N/A if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
2N/A (void) strcpy((*cs)->ap_info,
2N/A slotname_arg.slotnames[slotname_arg.minor]);
2N/A }
2N/A
2N/A /* class_code/subclass/boardtype */
2N/A get_type(boardtype, cardtype, (*cs)->ap_type);
2N/A
2N/A DBG(1, ("cfga_list_ext return success\n"));
2N/A rv = CFGA_OK;
2N/A
2N/A hp_fini(node);
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * This routine prints a single line of help message
2N/A */
2N/Astatic void
2N/Acfga_msg(struct cfga_msg *msgp, const char *str)
2N/A{
2N/A DBG(2, ("<%s>", str));
2N/A
2N/A if (msgp == NULL || msgp->message_routine == NULL)
2N/A return;
2N/A
2N/A (*msgp->message_routine)(msgp->appdata_ptr, str);
2N/A (*msgp->message_routine)(msgp->appdata_ptr, "\n");
2N/A}
2N/A
2N/Astatic cfga_err_t
2N/Acheck_options(const char *options)
2N/A{
2N/A struct cfga_msg *msgp = NULL;
2N/A
2N/A if (options) {
2N/A cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
2N/A cfga_msg(msgp, options);
2N/A return (CFGA_INVAL);
2N/A }
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Acfga_err_t
2N/Acfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
2N/A{
2N/A if (options) {
2N/A cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
2N/A cfga_msg(msgp, options);
2N/A }
2N/A DBG(1, ("cfga_help\n"));
2N/A
2N/A cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
2N/A cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
2N/A cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
2N/A return (CFGA_OK);
2N/A}
2N/A
2N/A/*
2N/A * cfga_err() accepts a command or error code, and converts it to a string.
2N/A * cfga_err() calls gettext() to internationalize proper messages.
2N/A */
2N/Astatic void
2N/Acfga_err(char **errstring, int code)
2N/A{
2N/A int i;
2N/A int len = 0;
2N/A char *str;
2N/A char *strs[3];
2N/A
2N/A /* If errstring is null, do nothing. */
2N/A if (errstring == NULL)
2N/A return;
2N/A
2N/A /*
2N/A * Convert the indicated error code to a string.
2N/A *
2N/A * Command failures become "<command> failed", without translating
2N/A * the command name. Other errors use a full translation of one of
2N/A * the defined error strings.
2N/A */
2N/A switch (code) {
2N/A case CMD_ACQUIRE:
2N/A case CMD_GETSTAT:
2N/A case CMD_LIST:
2N/A case CMD_SLOT_CONNECT:
2N/A case CMD_SLOT_DISCONNECT:
2N/A case CMD_SLOT_CONFIGURE:
2N/A case CMD_SLOT_UNCONFIGURE:
2N/A case CMD_SLOT_INSERT:
2N/A case CMD_SLOT_REMOVE:
2N/A DBG(2, ("<%s> %s\n", cfga_errstrs(code), cfga_strs[FAILED]));
2N/A strs[0] = cfga_errstrs(code);
2N/A strs[1] = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
2N/A strs[2] = NULL;
2N/A break;
2N/A
2N/A case ERR_NOMEM:
2N/A case ERR_AP_INVAL:
2N/A case ERR_AP_ERR:
2N/A case ERR_NO_DEVICE:
2N/A DBG(2, ("<%s>\n", cfga_errstrs[code]));
2N/A strs[0] = dgettext(TEXT_DOMAIN, cfga_errstrs[code]);
2N/A strs[1] = NULL;
2N/A break;
2N/A
2N/A default:
2N/A /* Invalid command/error code */
2N/A return;
2N/A }
2N/A
2N/A /* Compute the length of the error message */
2N/A for (i = 0; strs[i] != NULL; i++)
2N/A len += strlen(strs[i]);
2N/A
2N/A /* Allocate memory for the error message */
2N/A if ((str = calloc(len + 1, 1)) == NULL)
2N/A return;
2N/A
2N/A /* Concatenate the strings into the error message */
2N/A for (i = 0; strs[i] != NULL; i++)
2N/A (void) strlcat(str, strs[i], len + 1);
2N/A
2N/A *errstring = str;
2N/A DBG(2, ("%s\n", *errstring));
2N/A}
2N/A
2N/A/*
2N/A * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
2N/A */