#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <syslog.h>
#include <string.h>
#include <libintl.h>
#include <picl.h>
#include <picltree.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <mc.h>
#include <libnvpair.h>
#include <limits.h>
#include "piclmemcfg.h"
static void piclmemcfg_register(void);
static void piclmemcfg_init(void);
static void piclmemcfg_fini(void);
static void piclmemcfg_evhandler(const char *ename, const void *earg,
size_t size, void *cookie);
#pragma init(piclmemcfg_register)
static picld_plugin_reg_t my_reg_info = {
PICLD_PLUGIN_VERSION_1,
PICLD_PLUGIN_NON_CRITICAL,
"SUNW_piclmemcfg",
piclmemcfg_init,
piclmemcfg_fini
};
#define EM_INIT_FAILED gettext("SUNW_piclmemcfg init failed!\n")
#define EM_PHYSIC_MEM_TREE_FAILED \
gettext("SUNW_piclmemcfg physical memory tree failed!\n")
#define EM_LOGIC_MEM_TREE_FAILED \
gettext("SUNW_piclmemcfg logical memory tree failed!\n")
#define EM_INIT_MC_FAILED \
gettext("SUNW_piclmemcfg init mc failed!\n")
#define MC_DIR "/dev/mc/"
static int nsegments;
static int nbanks;
static int ndevgrps;
static int ndevs;
static int transfersize;
static picl_nodehdl_t *msegh_info;
typedef struct memmodgrp_info {
int mmgid;
struct memmodgrp_info *next;
picl_nodehdl_t mmgh;
picl_nodehdl_t mch;
} mmodgrp_info_t;
static mmodgrp_info_t *head2mmodgrp;
static void
free_allocated_mem(void)
{
mmodgrp_info_t *mmghdl, *currmmghdl;
mmghdl = head2mmodgrp;
while (mmghdl) {
currmmghdl = mmghdl;
mmghdl = mmghdl->next;
free(currmmghdl);
}
head2mmodgrp = NULL;
}
static void
del_plugout_mmodgrp(picl_nodehdl_t mch)
{
mmodgrp_info_t *mmghdl, *prevmmghdl, *nextmmghdl;
for (mmghdl = head2mmodgrp, prevmmghdl = NULL; mmghdl != NULL;
mmghdl = nextmmghdl) {
nextmmghdl = mmghdl->next;
if (mmghdl->mch == mch) {
if (prevmmghdl == NULL)
head2mmodgrp = nextmmghdl;
else
prevmmghdl->next = nextmmghdl;
free(mmghdl);
} else
prevmmghdl = mmghdl;
}
}
static int
find_mem_mod_grp_hdl(int id, picl_nodehdl_t *mmodgrph)
{
mmodgrp_info_t *mmghdl;
int err = PICL_FAILURE;
mmghdl = head2mmodgrp;
while (mmghdl) {
if ((mmghdl->mmgh) && (mmghdl->mmgid == id)) {
*mmodgrph = mmghdl->mmgh;
err = PICL_SUCCESS;
break;
}
mmghdl = mmghdl->next;
}
return (err);
}
static void
undo_phymem_tree(void)
{
mmodgrp_info_t *mmghdl;
mmghdl = head2mmodgrp;
while (mmghdl) {
if (mmghdl->mmgh == 0)
continue;
(void) ptree_delete_node(mmghdl->mmgh);
(void) ptree_destroy_node(mmghdl->mmgh);
mmghdl->mch = mmghdl->mmgh = 0;
mmghdl = mmghdl->next;
}
}
static int
add_mem_banks(picl_nodehdl_t msegh, int fd, struct mc_segment *mcseg)
{
int i;
int err = PICL_SUCCESS;
static picl_nodehdl_t mmodgrph;
picl_prophdl_t bankh;
ptree_propinfo_t propinfo;
struct mc_bank mcbank;
char propname[PICL_CLASSNAMELEN_MAX];
for (i = 0; i < mcseg->nbanks; i++) {
mcbank.id = mcseg->bankids[i].globalid;
if (ioctl(fd, MCIOC_BANK, &mcbank) == -1)
return (PICL_FAILURE);
err = ptree_create_and_add_node(msegh, PICL_NAME_MEMORY_BANK,
PICL_CLASS_MEMORY_BANK, &bankh);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.size),
PICL_PROP_SIZE, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.size,
NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.mask),
PICL_PROP_ADDRESSMASK, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.mask,
NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.match),
PICL_PROP_ADDRESSMATCH, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(bankh, &propinfo,
&mcbank.match, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_INT, PICL_READ, sizeof (mcbank.id), PICL_PROP_ID,
NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.id,
NULL);
if (err != PICL_SUCCESS)
break;
if ((find_mem_mod_grp_hdl(mcbank.devgrpid.globalid,
&mmodgrph)) != PICL_SUCCESS)
continue;
(void) strlcpy(propname, (ndevs > 1 ?
PICL_REFPROP_MEMORY_MODULE_GROUP :
PICL_REFPROP_MEMORY_MODULE), PICL_CLASSNAMELEN_MAX);
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
propname, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(bankh, &propinfo, &mmodgrph,
NULL);
if (err != PICL_SUCCESS)
break;
}
return (PICL_SUCCESS);
}
static void
undo_logical_tree(int nsegments)
{
int i;
for (i = 0; i < nsegments; i++) {
(void) ptree_delete_node(msegh_info[i]);
(void) ptree_destroy_node(msegh_info[i]);
}
}
static int
create_logical_tree(picl_nodehdl_t memh, int fd)
{
int i;
int err = PICL_SUCCESS;
picl_nodehdl_t msegh;
ptree_propinfo_t propinfo;
struct mc_memory *mcmem;
struct mc_segment *mcseg;
picl_prophdl_t proph;
uint64_t memsize = 0;
if ((mcmem = alloca((nsegments - 1) * sizeof (mcmem->segmentids[0]) +
sizeof (*mcmem))) == NULL)
return (PICL_FAILURE);
mcmem->nsegments = nsegments;
if (ioctl(fd, MCIOC_MEM, mcmem) == -1)
return (PICL_FAILURE);
if ((mcseg = alloca((nbanks - 1) * sizeof (mcseg->bankids[0]) +
sizeof (*mcseg))) == NULL)
return (PICL_FAILURE);
for (i = 0; i < nsegments; i++) {
mcseg->id = mcmem->segmentids[i].globalid;
mcseg->nbanks = nbanks;
if (ioctl(fd, MCIOC_SEG, mcseg) == -1)
break;
err = ptree_create_and_add_node(memh, PICL_NAME_MEMORY_SEGMENT,
PICL_CLASS_MEMORY_SEGMENT, &msegh);
if (err != PICL_SUCCESS)
break;
msegh_info[i] = msegh;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->size),
PICL_PROP_SIZE, NULL, NULL);
if (err != PICL_SUCCESS)
break;
memsize += mcseg->size;
err = ptree_create_and_add_prop(msegh, &propinfo, &mcseg->size,
NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->base),
PICL_PROP_BASEADDRESS, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(msegh, &propinfo, &mcseg->base,
NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->ifactor),
PICL_PROP_INTERLEAVE_FACTOR, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(msegh, &propinfo,
&mcseg->ifactor, NULL);
if (err != PICL_SUCCESS)
break;
err = add_mem_banks(msegh, fd, mcseg);
if (err != PICL_SUCCESS)
break;
}
if (err != PICL_SUCCESS) {
undo_logical_tree(nsegments);
return (err);
}
err = ptree_get_prop_by_name(memh, PICL_PROP_SIZE, &proph);
if (err == PICL_SUCCESS) {
err = ptree_update_propval(proph, &memsize, sizeof (memsize));
return (err);
}
(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (memsize),
PICL_PROP_SIZE, NULL, NULL);
err = ptree_create_and_add_prop(memh, &propinfo, &memsize, NULL);
return (err);
}
static int
add_mem_modules(picl_nodehdl_t mmodgrph, struct mc_devgrp *mcdevgrp)
{
uint64_t size;
picl_nodehdl_t dimmh;
ptree_propinfo_t propinfo;
int i;
int err = PICL_SUCCESS;
size = mcdevgrp->size / mcdevgrp->ndevices;
for (i = 0; i < mcdevgrp->ndevices; i++) {
err = ptree_create_and_add_node(mmodgrph,
PICL_NAME_MEMORY_MODULE, PICL_CLASS_MEMORY_MODULE, &dimmh);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (size),
PICL_PROP_SIZE, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(dimmh, &propinfo, &size, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_INT, PICL_READ, sizeof (i), PICL_PROP_ID,
NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(dimmh, &propinfo, &i,
NULL);
if (err != PICL_SUCCESS)
break;
}
return (err);
}
static int
create_physical_tree(picl_nodehdl_t mch, void *args)
{
int i, portid;
int err = PICL_SUCCESS;
mmodgrp_info_t *mmghdl;
picl_nodehdl_t mmodgrph;
ptree_propinfo_t propinfo;
struct mc_control *mccontrol;
struct mc_devgrp mcdevgrp;
int fd;
fd = (int)args;
err = ptree_get_propval_by_name(mch, OBP_PROP_PORTID, &portid,
sizeof (portid));
if (err != PICL_SUCCESS)
return (err);
if ((mccontrol = alloca((ndevgrps - 1) *
sizeof (mccontrol->devgrpids[0]) + sizeof (*mccontrol))) == NULL)
return (PICL_FAILURE);
mccontrol->id = portid;
mccontrol->ndevgrps = ndevgrps;
if (ioctl(fd, MCIOC_CONTROL, mccontrol) == -1) {
if (errno == EINVAL)
return (PICL_WALK_CONTINUE);
else
return (PICL_FAILURE);
}
if (mccontrol->ndevgrps == 0)
return (PICL_WALK_CONTINUE);
for (i = 0; i < mccontrol->ndevgrps; i++) {
int mmglocalid = mccontrol->devgrpids[i].localid;
mcdevgrp.id = mccontrol->devgrpids[i].globalid;
if (ioctl(fd, MCIOC_DEVGRP, &mcdevgrp) == -1)
return (PICL_FAILURE);
if (mcdevgrp.size == 0)
continue;
err = ptree_create_and_add_node(mch, PICL_NAME_MEM_MOD_GROUP,
PICL_CLASS_MEMORY_MODULE_GROUP, &mmodgrph);
if (err != PICL_SUCCESS)
break;
if ((mmghdl = malloc(sizeof (*mmghdl))) == NULL)
return (PICL_FAILURE);
mmghdl->mmgid = mcdevgrp.id;
mmghdl->mmgh = mmodgrph;
mmghdl->mch = mch;
mmghdl->next = head2mmodgrp;
head2mmodgrp = mmghdl;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcdevgrp.size),
PICL_PROP_SIZE, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(mmodgrph, &propinfo,
&mcdevgrp.size, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_INT, PICL_READ, sizeof (mmglocalid),
PICL_PROP_ID, NULL, NULL);
if (err != PICL_SUCCESS)
break;
err = ptree_create_and_add_prop(mmodgrph, &propinfo,
&mmglocalid, NULL);
if (err != PICL_SUCCESS)
break;
err = add_mem_modules(mmodgrph, &mcdevgrp);
if (err != PICL_SUCCESS)
break;
}
if (err == PICL_SUCCESS)
return (PICL_WALK_CONTINUE);
return (err);
}
static int
find_mc_create_tree(picl_nodehdl_t rooth, int fd)
{
int err;
err = ptree_walk_tree_by_class(rooth, PICL_CLASS_MEMORY_CONTROLLER,
(void *)fd, create_physical_tree);
return (err);
}
static int
init_mc(void)
{
struct mc_memconf mcmemconf;
int fd;
DIR *dirp;
struct dirent *retp;
char path[PATH_MAX];
int found = 0;
int valid_entry = 0;
if ((dirp = opendir(MC_DIR)) == NULL) {
if (errno != ENOENT)
syslog(LOG_ERR, EM_INIT_MC_FAILED);
return (-1);
}
while ((retp = readdir(dirp)) != NULL) {
if (strcmp(retp->d_name, ".") == 0 ||
strcmp(retp->d_name, "..") == 0)
continue;
(void) strcpy(path, MC_DIR);
(void) strcat(path, retp->d_name);
if ((fd = open(path, O_RDONLY, 0)) != -1) {
found = 1;
break;
}
if (errno != ENOENT)
valid_entry = 1;
}
(void) closedir(dirp);
if (!found) {
if (valid_entry)
syslog(LOG_ERR, EM_INIT_MC_FAILED);
return (-1);
}
if (ioctl(fd, MCIOC_MEMCONF, &mcmemconf) == -1) {
(void) close(fd);
return (-1);
}
nsegments = mcmemconf.nsegments;
nbanks = mcmemconf.nbanks;
ndevgrps = mcmemconf.ndevgrps;
ndevs = mcmemconf.ndevs;
transfersize = mcmemconf.xfer_size;
return (fd);
}
void
piclmemcfg_register(void)
{
(void) picld_plugin_register(&my_reg_info);
}
void
piclmemcfg_init(void)
{
picl_nodehdl_t plfh;
picl_nodehdl_t memh;
ptree_propinfo_t propinfo;
int fd, err;
head2mmodgrp = NULL;
msegh_info = NULL;
if ((fd = init_mc()) < 0)
return;
if ((msegh_info = malloc(nsegments * sizeof (picl_nodehdl_t))) ==
NULL) {
syslog(LOG_ERR, EM_INIT_FAILED);
(void) close(fd);
return;
}
if ((ptree_get_node_by_path(PLATFORM_PATH, &plfh)) != PICL_SUCCESS) {
syslog(LOG_ERR, EM_INIT_FAILED);
(void) close(fd);
return;
}
if ((ptree_get_node_by_path(MEMORY_PATH, &memh)) != PICL_SUCCESS) {
syslog(LOG_ERR, EM_INIT_FAILED);
(void) close(fd);
return;
}
err = find_mc_create_tree(plfh, fd);
if (err != PICL_SUCCESS) {
undo_phymem_tree();
syslog(LOG_ERR, EM_PHYSIC_MEM_TREE_FAILED);
}
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (transfersize),
PICL_PROP_TRANSFER_SIZE, NULL, NULL);
if (err != PICL_SUCCESS) {
(void) close(fd);
return;
}
err = ptree_create_and_add_prop(memh, &propinfo,
&transfersize, NULL);
if (err != PICL_SUCCESS) {
(void) close(fd);
return;
}
if ((create_logical_tree(memh, fd)) != PICL_SUCCESS) {
syslog(LOG_ERR, EM_LOGIC_MEM_TREE_FAILED);
undo_logical_tree(nsegments);
}
(void) close(fd);
(void) ptree_register_handler(PICLEVENT_MC_ADDED,
piclmemcfg_evhandler, NULL);
(void) ptree_register_handler(PICLEVENT_MC_REMOVED,
piclmemcfg_evhandler, NULL);
}
void
piclmemcfg_fini(void)
{
(void) ptree_unregister_handler(PICLEVENT_MC_ADDED,
piclmemcfg_evhandler, NULL);
(void) ptree_unregister_handler(PICLEVENT_MC_REMOVED,
piclmemcfg_evhandler, NULL);
free_allocated_mem();
if (msegh_info)
free(msegh_info);
}
static void
piclmemcfg_evhandler(const char *ename, const void *earg, size_t size,
void *cookie)
{
int err;
int fd;
picl_nodehdl_t memh;
picl_nodehdl_t nodeh;
int old_nsegs;
nvlist_t *nvlp;
memh = 0;
if (nvlist_unpack((char *)earg, size, &nvlp, 0))
return;
if (nvlist_lookup_uint64(nvlp, PICLEVENTARG_NODEHANDLE, &nodeh)) {
nvlist_free(nvlp);
return;
}
nvlist_free(nvlp);
err = ptree_get_node_by_path(MEMORY_PATH, &memh);
if (err != PICL_SUCCESS)
return;
old_nsegs = nsegments;
if ((fd = init_mc()) < 0)
return;
if (strcmp(ename, PICLEVENT_MC_ADDED) == 0)
(void) create_physical_tree(nodeh, (void *)fd);
else if (strcmp(ename, PICLEVENT_MC_REMOVED) == 0)
(void) del_plugout_mmodgrp(nodeh);
(void) undo_logical_tree(old_nsegs);
free(msegh_info);
if ((msegh_info = malloc(nsegments * sizeof (picl_nodehdl_t))) ==
NULL) {
(void) close(fd);
return;
}
(void) create_logical_tree(memh, fd);
(void) close(fd);
}