/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* Plugin library for PCI Express and PCI (SHPC) hotplug controller
*/
#include <stddef.h>
#include <locale.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <locale.h>
#include <langinfo.h>
#include <time.h>
#include <stdarg.h>
#include <libdevinfo.h>
#include <libdevice.h>
#define CFGA_PLUGIN_LIB
#include <config_admin.h>
#include <assert.h>
#include <sys/dditypes.h>
#include <libintl.h>
#include <dirent.h>
#include <limits.h>
#include "../../../../common/pci/pci_strings.h"
#include <libhotplug.h>
extern const struct pci_class_strings_s class_pci[];
extern int class_pci_items;
#define MSG_HOTPLUG_DISABLED \
"Error: hotplug service is probably not running, " \
"please use 'svcadm enable hotplug' to enable the service. " \
"See cfgadm_shp(1M) for more details."
/*
* Set the version number
*/
#ifdef DEBUG
#endif
#if !defined(TEXT_DOMAIN)
#endif
/*
* DEBUGING LEVEL
*
* External routines: 1 - 2
* Internal routines: 3 - 4
*/
#ifdef SHP_DBG
#else
#endif
#define CMD_ACQUIRE 0
static char *
cfga_errstrs[] = {
/* n */ "acquire ",
/* n */ "get-status ",
/* n */ "list ",
/* n */ "connect ",
/* n */ "disconnect ",
/* n */ "configure ",
/* n */ "unconfigure ",
/* n */ "insert ",
/* n */ "remove ",
/* n */ "open ",
/* n */ "fstat ",
/* y */ "out of memory ",
/* y */ "invalid attachment point ",
/* y */ "invalid transition ",
/* y */ "no device is present ",
};
extern int errno;
static char *findlink(char *ap_phys_id);
static char *
cfga_strs[] = {
NULL,
"\nPCI hotplug specific commands:",
"\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
"ap_id [ap_id...]",
"\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
"\tunknown command or option: ",
"success ",
"failed ",
"unknown",
};
#define ENABLE_SLOT 0
/*
* Board Type
*/
static char *
board_strs[] = {
/* n */ "???", /* PCIEHPC_BOARD_UNKNOWN */
/* n */ "hp", /* PCIEHPC_BOARD_PCI_HOTPLUG */
/* n */ NULL
};
/*
* HW functions
*/
static char *
func_strs[] = {
/* n */ "enable_slot",
/* n */ "disable_slot",
/* n */ "enable_autoconfig",
/* n */ "disable_autoconfig",
/* n */ "led",
/* n */ "mode",
/* n */ NULL
};
/*
* LED strings
*/
static char *
led_strs[] = {
/* n */ "fault", /* PCIEHPC_FAULT_LED */
/* n */ "power", /* PCIEHPC_POWER_LED */
/* n */ "attn", /* PCIEHPC_ATTN_LED */
/* n */ "active", /* PCIEHPC_ACTIVE_LED */
/* n */ NULL
};
static char *
led_strs2[] = {
/* n */ NULL
};
#define FAULT 0
static char *
mode_strs[] = {
/* n */ "off", /* OFF */
/* n */ "on", /* ON */
/* n */ "blink", /* BLINK */
/* n */ NULL
};
#define OFF 0
typedef enum {
struct searcharg {
char *devpath;
int minor;
};
static void *private_check;
/*
* Return the corresponding hp node for a given ap_id, it is the caller's
* responsibility to call hp_fini() to free the snapshot.
*/
static cfga_err_t
{
char *rpath;
char *cp;
return (CFGA_ERROR);
return (CFGA_ERROR);
}
/* Remove devices prefix (if any) */
}
/* Remove dynamic component if any */
*cp = '\0';
}
/* Remove minor name (if any) */
return (CFGA_INVAL);
}
*cp = '\0';
cp++;
/* No reponse to operations on the door file. */
return (CFGA_NOTSUPP);
}
return (CFGA_ERROR);
}
return (CFGA_OK);
}
typedef struct error_size_cb_arg {
int cnt;
/*
* Callback function for hp_traverse(), to sum up the
* maximum length for error message display.
*/
static int
{
/* Only process USAGE nodes */
return (HP_WALK_CONTINUE);
/* size up resource name */
/* size up usage description */
return (HP_WALK_CONTINUE);
}
typedef struct error_sum_cb_arg {
char **table;
char *format;
/*
* Callback function for hp_traverse(), to add the error
* message to he table.
*/
static int
{
/* Only process USAGE nodes */
return (HP_WALK_CONTINUE);
return (HP_WALK_CONTINUE);
}
/*
* Takes an opaque rcm_info_t pointer and a character pointer, and appends
* the rcm_info_t data in the form of a table to the given character pointer.
*/
static void
{
int i;
size_t w;
char *rsrc;
char *info;
char *newtable;
/* Protect against invalid arguments */
return;
/* Set localized table header strings */
/* A first pass, to size up the RCM information */
/* If nothing was sized up above, stop early */
return;
/* Adjust column widths for column headings */
w_rsrc = w;
else if ((w_rsrc - w) % 2)
w_rsrc++;
w_info = w;
else if ((w_info - w) % 2)
w_info++;
/*
* Compute the total line width of each line,
* accounting for intercolumn spacing.
*/
/* Allocate space for the table */
/* zero fill for the strcat() call below */
return;
} else {
return;
else
}
/* Place a table header into the string */
/* The resource header */
for (i = 0; i < ((w_rsrc - w) / 2); i++)
for (i = 0; i < ((w_rsrc - w) / 2); i++)
/* The information header */
for (i = 0; i < ((w_info - w) / 2); i++)
for (i = 0; i < ((w_info - w) / 2); i++)
/* Underline the headers */
for (i = 0; i < w_rsrc; i++)
for (i = 0; i < w_info; i++)
/* Construct the format string */
/* Add the tuples to the table string */
}
/*
* Figure out the target kernel state for a given cfgadm
* change-state operation.
*/
static cfga_err_t
{
switch (state_change_cmd) {
case CFGA_CMD_CONNECT:
break;
case CFGA_CMD_DISCONNECT:
break;
case CFGA_CMD_CONFIGURE:
break;
case CFGA_CMD_UNCONFIGURE:
break;
default:
return (CFGA_ERROR);
}
return (CFGA_OK);
}
/*
* Translate kernel state to cfgadm receptacle state and occupant state.
*/
static cfga_err_t
{
int state;
/* Receptacle state */
switch (state) {
case DDI_HP_CN_STATE_EMPTY:
*rs = AP_RSTATE_EMPTY;
break;
case DDI_HP_CN_STATE_PRESENT:
break;
case DDI_HP_CN_STATE_POWERED:
case DDI_HP_CN_STATE_ENABLED:
break;
/*
* Connector state can only be one of
* Empty, Present, Powered, Enabled.
*/
default:
return (CFGA_ERROR);
}
/*
* Occupant state
*/
/*
* Mark occupant state as "configured" if at least one of the
* associated ports is at state "offline" or above. Driver
* attach ("online" state) is not necessary here.
*/
break;
}
else
return (CFGA_OK);
}
/*
* Transitional Diagram:
*
* empty unconfigure
* (remove) ^| (physically insert card)
* |V
* disconnect configure
* "-c DISCONNECT" ^| "-c CONNECT"
* |V "-c CONFIGURE"
* connect unconfigure -> connect configure
* <-
* "-c UNCONFIGURE"
*
*/
/*ARGSUSED*/
{
return (rv);
}
return (rv);
/*
* Check for the FORCE flag. It is only used
* for DISCONNECT or UNCONFIGURE state changes.
*/
if (flags & CFGA_FLAG_FORCE)
/*
* Which state should we drive to ?
*/
if ((state_change_cmd != CFGA_CMD_LOAD) &&
(state_change_cmd != CFGA_CMD_UNLOAD)) {
return (CFGA_ERROR);
}
}
switch (state_change_cmd) {
case CFGA_CMD_CONNECT:
if (state == DDI_HP_CN_STATE_EMPTY) {
rv = CFGA_INVAL;
} else if (state == DDI_HP_CN_STATE_PRESENT) {
/* Connect the slot */
&results) != 0) {
rv = CFGA_ERROR;
}
}
break;
case CFGA_CMD_DISCONNECT:
if (state == DDI_HP_CN_STATE_EMPTY) {
rv = CFGA_INVAL;
} else if (state > DDI_HP_CN_STATE_PRESENT) {
/* Disconnect the slot */
&results);
if (rv != 0) {
else
rv = CFGA_ERROR;
if (results) {
} else {
}
}
}
break;
case CFGA_CMD_CONFIGURE:
/*
* for multi-func device we allow multiple
* configure on the same slot because one
* func can be configured and other one won't
*/
if (state == DDI_HP_CN_STATE_EMPTY) {
rv = CFGA_INVAL;
&results) != 0) {
rv = CFGA_ERROR;
}
break;
case CFGA_CMD_UNCONFIGURE:
if (state == DDI_HP_CN_STATE_EMPTY) {
rv = CFGA_INVAL;
} else if (state >= DDI_HP_CN_STATE_ENABLED) {
&results);
if (rv != 0) {
else
rv = CFGA_ERROR;
if (results) {
} else {
}
}
}
break;
case CFGA_CMD_LOAD:
/* do nothing, just produce error msg as is */
if (state < DDI_HP_CN_STATE_POWERED) {
rv = CFGA_ERROR;
} else {
rv = CFGA_INVAL;
}
break;
case CFGA_CMD_UNLOAD:
/* do nothing, just produce error msg as is */
if (state < DDI_HP_CN_STATE_POWERED) {
rv = CFGA_ERROR;
} else {
rv = CFGA_INVAL;
}
break;
default:
rv = CFGA_OPNOTSUPP;
break;
}
return (rv);
}
char *
{
char *tmp;
return (NULL);
tmp++;
return (tmp);
}
static cfga_err_t
{
char *buff;
char *tmp;
char *format;
char *result;
int i, n, rv;
};
if (!repeat)
return (rv);
return (CFGA_ERROR);
}
return (CFGA_ERROR);
}
n = sizeof (states)/sizeof (pciehpc_led_t);
for (i = 0; i < n; i++) {
} else {
/*
* hp_get_private() will return back things like
* "led_fault=off", transform it to cfgadm desired
* format.
*/
return (CFGA_ERROR);
}
}
}
return (CFGA_OK);
}
/*ARGSUSED*/
{
char *str;
char ptr;
char *result;
return (rv);
}
if (private_check == confp)
repeat = 1;
else
private_check = (void*)confp;
break;
}
switch (i) {
case ENABLE_SLOT:
case DISABLE_SLOT:
/* pass through */
case ENABLE_AUTOCNF:
case DISABLE_AUTOCNF:
/* no action needed */
return (CFGA_OK);
break;
case LED:
/* set mode */
if (ptr == '=') {
i++, str++) {
if (i == (MAXNAMELEN - 1))
break;
}
/* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
else return (CFGA_INVAL);
}
}
buf[i] = '\0';
/* ON = 1, OFF = 0 */
else return (CFGA_INVAL);
/* sendin */
break;
} else if (ptr == '\0') {
/* print mode */
msgp));
}
default:
return (CFGA_INVAL);
}
return (rv);
return (CFGA_ERROR);
}
return (CFGA_OK);
}
/*ARGSUSED*/
{
return (rv);
}
/* will need to implement pci CTRL command */
return (CFGA_NOTSUPP);
}
/*
* The slot-names property describes the external labeling of add-in slots.
* This property is an encoded array, an integer followed by a list of
* strings. The return value from di_prop_lookup_ints for slot-names is -1.
* The expected return value should be the number of elements.
* Di_prop_decode_common does not decode encoded data from software,
* such as the solaris device tree, unlike from the prom.
* Di_prop_decode_common takes the size of the encoded data and mods
* it with the size of int. The size of the encoded data for slot-names is 9
* and the size of int is 4, yielding a non zero result. A value of -1 is used
* to indicate that the number of elements can not be determined.
* Di_prop_decode_common can be modified to decode encoded data from the solaris
* device tree.
*/
static int
{
return (DI_WALK_TERMINATE);
} else {
int i;
if (i >= MAXDEVS)
return (DI_WALK_TERMINATE);
if ((*intp >> i) & 1) {
/* assign tmptr */
tmptr);
/* wind tmptr to next \0 */
while (*tmptr != '\0') {
tmptr++;
}
tmptr++;
} else {
/* point at unknown string */
"unknown");
}
}
}
return (DI_WALK_TERMINATE);
}
static int
{
char *devname;
}
/*
* Check the Solaris device tree first
* in the case of a DR operation
*/
while (solaris_prop != DI_PROP_NIL) {
== 0) {
}
}
/*
* Check the prom device tree which is populated at boot.
* If this fails, give up and set the slot name to null.
*/
while (prom_prop != DI_PROM_PROP_NIL) {
== 0) {
}
}
return (DI_WALK_TERMINATE);
} else
return (DI_WALK_CONTINUE);
}
static int
{
== DI_NODE_NIL) {
return (-1);
}
return (-1);
}
0, (void *)slotarg, find_slotname);
return (0);
else
return (-1);
}
static void
{
/* for type string assembly in get_type() */
TPCT("unknown");
return;
}
TPCT("/");
else
}
/*
* call-back function for di_devlink_walk
*/
static int
{
return (DI_WALK_TERMINATE);
}
return (DI_WALK_CONTINUE);
}
/*
* Walk throught the cached /dev link tree looking for links to the ap
* if none are found return an error
*/
static cfga_err_t
{
/* ap_id is a valid minor_path with /devices prepended */
(void *)ap_log_id, found_devlink);
} else {
return (CFGA_ERROR);
}
(void) di_devlink_fini(&hdl);
if (ap_log_id[0] != '\0')
return (CFGA_OK);
else
return (CFGA_ERROR);
}
/*
* most of this is needed to compensate for
* differences between various platforms
*/
static cfga_err_t
char **errstring)
{
char *buf;
char *tmp;
char *ptr;
ap_log_id[0] = '\0';
return (CFGA_OK);
return (CFGA_ERROR);
}
ptr[0] = '\0';
if (ap_node == DI_NODE_NIL) {
return (CFGA_ERROR);
}
return (CFGA_OK);
}
static int
{
return (DI_WALK_TERMINATE);
}
/*
* returns an allocated string containing the full path to the devlink for
* <ap_phys_id> in the devlink database; we expect only one devlink per
* <ap_phys_id> so we return the first encountered
*/
static char *
{
ap_phys_id += 8;
(void *)&path, findlink_cb);
(void) di_devlink_fini(&hdl);
return (path);
}
/*
* returns CFGA_OK if it can succesfully retrieve the devlink info associated
* with devlink for <ap_phys_id> which will be returned through <ap_info>
*/
{
int fd;
if (fd < 0)
return (CFGA_ERROR);
return (CFGA_OK);
}
static cfga_err_t
{
char *condition;
/* "condition" bus specific commands */
&condition) != 0) {
*cond = AP_COND_UNKNOWN;
return (CFGA_ERROR);
}
*cond = AP_COND_OK;
*cond = AP_COND_FAILING;
*cond = AP_COND_FAILED;
*cond = AP_COND_UNUSABLE;
*cond = AP_COND_UNKNOWN;
else
return (CFGA_ERROR);
return (CFGA_OK);
}
/*ARGSUSED*/
{
char *boardtype;
char *cardtype;
return (rv);
}
rv = CFGA_ERROR;
return (rv);
}
*nlist = 1;
rv = CFGA_ERROR;
return (rv);
}
return (rv);
}
return (CFGA_ERROR);
}
switch (rs) {
case AP_RSTATE_EMPTY:
break;
case AP_RSTATE_DISCONNECTED:
break;
case AP_RSTATE_CONNECTED:
break;
default:
rv = CFGA_ERROR;
return (rv);
}
switch (os) {
case AP_OSTATE_CONFIGURED:
break;
case AP_OSTATE_UNCONFIGURED:
break;
default:
rv = CFGA_ERROR;
return (rv);
}
switch (cond) {
case AP_COND_OK:
break;
case AP_COND_FAILING:
break;
case AP_COND_FAILED:
break;
case AP_COND_UNUSABLE:
break;
case AP_COND_UNKNOWN:
break;
default:
rv = CFGA_ERROR;
return (rv);
}
/*
* We're not busy since the entrance into the kernel has been
* sync'ed via libhotplug.
*/
/* last change */
/* board type */
else
/* card type */
else
/* logical ap_id */
/* physical ap_id */
/* information */
}
/* slot_names of bus node */
}
/* class_code/subclass/boardtype */
return (rv);
}
/*
* This routine prints a single line of help message
*/
static void
{
return;
}
static cfga_err_t
{
if (options) {
return (CFGA_INVAL);
}
return (CFGA_OK);
}
/*ARGSUSED*/
{
if (options) {
}
return (CFGA_OK);
}
/*
* cfga_err() accepts a command or error code, and converts it to a string.
* cfga_err() calls gettext() to internationalize proper messages.
*/
static void
{
int i;
int len = 0;
char *str;
/* If errstring is null, do nothing. */
return;
/*
* Convert the indicated error code to a string.
*
* Command failures become "<command> failed", without translating
* the command name. Other errors use a full translation of one of
* the defined error strings.
*/
switch (code) {
case CMD_ACQUIRE:
case CMD_GETSTAT:
case CMD_LIST:
case CMD_SLOT_CONNECT:
case CMD_SLOT_DISCONNECT:
case CMD_SLOT_CONFIGURE:
case CMD_SLOT_UNCONFIGURE:
case CMD_SLOT_INSERT:
case CMD_SLOT_REMOVE:
break;
case ERR_NOMEM:
case ERR_AP_INVAL:
case ERR_AP_ERR:
case ERR_NO_DEVICE:
break;
default:
return;
}
/* Compute the length of the error message */
/* Allocate memory for the error message */
return;
/* Concatenate the strings into the error message */
}
/*
* cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
*/