1 /*
   2  * CDDL HEADER START
   3  *
   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.
   7  *
   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.
  12  *
  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]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright (c) 2018, Joyent, Inc.
  29  */
  30 
  31 #include <sys/types.h>
  32 #include <sys/time.h>
  33 #include <sys/nvpair.h>
  34 #include <sys/cmn_err.h>
  35 #include <sys/cred.h>
  36 #include <sys/open.h>
  37 #include <sys/ddi.h>
  38 #include <sys/sunddi.h>
  39 #include <sys/conf.h>
  40 #include <sys/modctl.h>
  41 #include <sys/cyclic.h>
  42 #include <sys/errorq.h>
  43 #include <sys/stat.h>
  44 #include <sys/cpuvar.h>
  45 #include <sys/mc_intel.h>
  46 #include <sys/mc.h>
  47 #include <sys/fm/protocol.h>
  48 #include "nhm_log.h"
  49 #include "intel_nhm.h"
  50 
  51 extern nvlist_t *inhm_mc_nvl[MAX_CPU_NODES];
  52 extern char closed_page;
  53 extern char ecc_enabled;
  54 extern char lockstep[MAX_CPU_NODES];
  55 extern char mirror_mode[MAX_CPU_NODES];
  56 extern char spare_channel[MAX_CPU_NODES];
  57 
  58 static void
  59 inhm_vrank(nvlist_t *vrank, int num, uint64_t dimm_base, uint64_t limit,
  60     uint32_t sinterleave, uint32_t cinterleave, uint32_t rinterleave,
  61     uint32_t sway, uint32_t cway, uint32_t rway)
  62 {
  63         char buf[128];
  64 
  65         (void) snprintf(buf, sizeof (buf), "dimm-rank-base-%d", num);
  66         (void) nvlist_add_uint64(vrank, buf, dimm_base);
  67         (void) snprintf(buf, sizeof (buf), "dimm-rank-limit-%d", num);
  68         (void) nvlist_add_uint64(vrank, buf, dimm_base + limit);
  69         if (sinterleave > 1) {
  70                 (void) snprintf(buf, sizeof (buf), "dimm-socket-interleave-%d",
  71                     num);
  72                 (void) nvlist_add_uint32(vrank, buf, sinterleave);
  73                 (void) snprintf(buf, sizeof (buf),
  74                     "dimm-socket-interleave-way-%d", num);
  75                 (void) nvlist_add_uint32(vrank, buf, sway);
  76         }
  77         if (cinterleave > 1) {
  78                 (void) snprintf(buf, sizeof (buf), "dimm-channel-interleave-%d",
  79                     num);
  80                 (void) nvlist_add_uint32(vrank, buf, cinterleave);
  81                 (void) snprintf(buf, sizeof (buf),
  82                     "dimm-channel-interleave-way-%d", num);
  83                 (void) nvlist_add_uint32(vrank, buf, cway);
  84         }
  85         if (rinterleave > 1) {
  86                 (void) snprintf(buf, sizeof (buf), "dimm-rank-interleave-%d",
  87                     num);
  88                 (void) nvlist_add_uint32(vrank, buf, rinterleave);
  89                 (void) snprintf(buf, sizeof (buf),
  90                     "dimm-rank-interleave-way-%d", num);
  91                 (void) nvlist_add_uint32(vrank, buf, rway);
  92         }
  93 }
  94 
  95 static void
  96 inhm_rank(nvlist_t *newdimm, nhm_dimm_t *nhm_dimm, uint32_t node,
  97     uint8_t channel, uint32_t dimm, uint64_t rank_size)
  98 {
  99         nvlist_t **newrank;
 100         int num;
 101         int i;
 102         uint64_t dimm_base;
 103         uint64_t vrank_sz;
 104         uint64_t rank_addr;
 105         uint64_t pa;
 106         uint32_t sinterleave, cinterleave, rinterleave;
 107         uint32_t sway, cway, rway;
 108 
 109         newrank = kmem_zalloc(sizeof (nvlist_t *) * nhm_dimm->nranks, KM_SLEEP);
 110         for (i = 0; i < nhm_dimm->nranks; i++) {
 111                 (void) nvlist_alloc(&newrank[i], NV_UNIQUE_NAME, KM_SLEEP);
 112                 rank_addr = 0;
 113                 num = 0;
 114                 while (rank_addr < rank_size) {
 115                         pa = dimm_to_addr(node, channel, dimm * 4 + i,
 116                             rank_addr, &dimm_base, &vrank_sz, &sinterleave,
 117                             &cinterleave, &rinterleave, &sway, &cway, &rway);
 118                         if (pa == -1 || vrank_sz == 0)
 119                                 break;
 120                         inhm_vrank(newrank[i], num, dimm_base,
 121                             vrank_sz * sinterleave * cinterleave * rinterleave,
 122                             sinterleave, cinterleave, rinterleave, sway, cway,
 123                             rway);
 124                         rank_addr += vrank_sz;
 125                         num++;
 126                 }
 127 
 128         }
 129         (void) nvlist_add_nvlist_array(newdimm, MCINTEL_NVLIST_RANKS, newrank,
 130             nhm_dimm->nranks);
 131         for (i = 0; i < nhm_dimm->nranks; i++)
 132                 nvlist_free(newrank[i]);
 133         kmem_free(newrank, sizeof (nvlist_t *) * nhm_dimm->nranks);
 134 }
 135 
 136 static nvlist_t *
 137 inhm_dimm(nhm_dimm_t *nhm_dimm, uint32_t node, uint8_t channel, uint32_t dimm)
 138 {
 139         nvlist_t *newdimm;
 140         uint8_t t;
 141         char sbuf[65];
 142 
 143         (void) nvlist_alloc(&newdimm, NV_UNIQUE_NAME, KM_SLEEP);
 144         (void) nvlist_add_uint32(newdimm, "dimm-number", dimm);
 145 
 146         if (nhm_dimm->dimm_size >= 1024*1024*1024) {
 147                 (void) snprintf(sbuf, sizeof (sbuf), "%dG",
 148                     (int)(nhm_dimm->dimm_size / (1024*1024*1024)));
 149         } else {
 150                 (void) snprintf(sbuf, sizeof (sbuf), "%dM",
 151                     (int)(nhm_dimm->dimm_size / (1024*1024)));
 152         }
 153         (void) nvlist_add_string(newdimm, "dimm-size", sbuf);
 154         (void) nvlist_add_uint64(newdimm, "size", nhm_dimm->dimm_size);
 155         (void) nvlist_add_uint32(newdimm, "nbanks", (uint32_t)nhm_dimm->nbanks);
 156         (void) nvlist_add_uint32(newdimm, "ncolumn",
 157             (uint32_t)nhm_dimm->ncolumn);
 158         (void) nvlist_add_uint32(newdimm, "nrow", (uint32_t)nhm_dimm->nrow);
 159         (void) nvlist_add_uint32(newdimm, "width", (uint32_t)nhm_dimm->width);
 160         (void) nvlist_add_uint32(newdimm, "ranks", (uint32_t)nhm_dimm->nranks);
 161         inhm_rank(newdimm, nhm_dimm, node, channel, dimm,
 162             nhm_dimm->dimm_size / nhm_dimm->nranks);
 163         if (nhm_dimm->manufacturer[0]) {
 164                 t = sizeof (nhm_dimm->manufacturer);
 165                 (void) strncpy(sbuf, nhm_dimm->manufacturer, t);
 166                 sbuf[t] = 0;
 167                 (void) nvlist_add_string(newdimm, "manufacturer", sbuf);
 168         }
 169         if (nhm_dimm->serial_number[0]) {
 170                 t = sizeof (nhm_dimm->serial_number);
 171                 (void) strncpy(sbuf, nhm_dimm->serial_number, t);
 172                 sbuf[t] = 0;
 173                 (void) nvlist_add_string(newdimm, FM_FMRI_HC_SERIAL_ID, sbuf);
 174         }
 175         if (nhm_dimm->part_number[0]) {
 176                 t = sizeof (nhm_dimm->part_number);
 177                 (void) strncpy(sbuf, nhm_dimm->part_number, t);
 178                 sbuf[t] = 0;
 179                 (void) nvlist_add_string(newdimm, FM_FMRI_HC_PART, sbuf);
 180         }
 181         if (nhm_dimm->revision[0]) {
 182                 t = sizeof (nhm_dimm->revision);
 183                 (void) strncpy(sbuf, nhm_dimm->revision, t);
 184                 sbuf[t] = 0;
 185                 (void) nvlist_add_string(newdimm, FM_FMRI_HC_REVISION, sbuf);
 186         }
 187         t = sizeof (nhm_dimm->label);
 188         (void) strncpy(sbuf, nhm_dimm->label, t);
 189         sbuf[t] = 0;
 190         (void) nvlist_add_string(newdimm, FM_FAULT_FRU_LABEL, sbuf);
 191         return (newdimm);
 192 }
 193 
 194 static void
 195 inhm_dimmlist(uint32_t node, nvlist_t *nvl)
 196 {
 197         nvlist_t **dimmlist;
 198         nvlist_t **newchannel;
 199         int nchannels = CHANNELS_PER_MEMORY_CONTROLLER;
 200         int nd;
 201         uint8_t i, j;
 202         nhm_dimm_t **dimmpp;
 203         nhm_dimm_t *dimmp;
 204 
 205         dimmlist =  kmem_zalloc(sizeof (nvlist_t *) * MAX_DIMMS_PER_CHANNEL,
 206             KM_SLEEP);
 207         newchannel = kmem_zalloc(sizeof (nvlist_t *) * nchannels, KM_SLEEP);
 208         dimmpp = &nhm_dimms[node * CHANNELS_PER_MEMORY_CONTROLLER *
 209             MAX_DIMMS_PER_CHANNEL];
 210         (void) nvlist_add_string(nvl, "memory-policy",
 211             closed_page ? "closed-page" : "open-page");
 212         (void) nvlist_add_string(nvl, "memory-ecc",
 213             ecc_enabled ? lockstep[node] ? "x8" : "x4" : "no");
 214         for (i = 0; i < nchannels; i++) {
 215                 (void) nvlist_alloc(&newchannel[i], NV_UNIQUE_NAME, KM_SLEEP);
 216                 (void) nvlist_add_string(newchannel[i], "channel-mode",
 217                     CHANNEL_DISABLED(MC_STATUS_RD(node), i) ? "disabled" :
 218                     i != 2 && lockstep[node] ? "lockstep" :
 219                     i != 2 && mirror_mode[node] ?
 220                     REDUNDANCY_LOSS(MC_RAS_STATUS_RD(node)) ?
 221                     "redundancy-loss" : "mirror" :
 222                     i == 2 && spare_channel[node] &&
 223                     !REDUNDANCY_LOSS(MC_RAS_STATUS_RD(node)) ? "spare" :
 224                     "independent");
 225                 nd = 0;
 226                 for (j = 0; j < MAX_DIMMS_PER_CHANNEL; j++) {
 227                         dimmp = *dimmpp;
 228                         if (dimmp != NULL) {
 229                                 dimmlist[nd] = inhm_dimm(dimmp, node, i,
 230                                     (uint32_t)j);
 231                                 nd++;
 232                         }
 233                         dimmpp++;
 234                 }
 235                 if (nd) {
 236                         (void) nvlist_add_nvlist_array(newchannel[i],
 237                             "memory-dimms", dimmlist, nd);
 238                         for (j = 0; j < nd; j++)
 239                                 nvlist_free(dimmlist[j]);
 240                 }
 241         }
 242         (void) nvlist_add_nvlist_array(nvl, MCINTEL_NVLIST_MC, newchannel,
 243             nchannels);
 244         for (i = 0; i < nchannels; i++)
 245                 nvlist_free(newchannel[i]);
 246         kmem_free(dimmlist, sizeof (nvlist_t *) * MAX_DIMMS_PER_CHANNEL);
 247         kmem_free(newchannel, sizeof (nvlist_t *) * nchannels);
 248 }
 249 
 250 char *
 251 inhm_mc_name()
 252 {
 253         return (NHM_INTERCONNECT);
 254 }
 255 
 256 void
 257 inhm_create_nvl(int chip)
 258 {
 259         nvlist_t *nvl;
 260 
 261         (void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
 262         (void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_VERSTR,
 263             MCINTEL_NVLIST_VERS);
 264         (void) nvlist_add_string(nvl, MCINTEL_NVLIST_MEM, inhm_mc_name());
 265         (void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_NMEM, 1);
 266         (void) nvlist_add_uint8(nvl, MCINTEL_NVLIST_NRANKS, 4);
 267         inhm_dimmlist(chip, nvl);
 268 
 269         nvlist_free(inhm_mc_nvl[chip]);
 270         inhm_mc_nvl[chip] = nvl;
 271 }