Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * edac_mc kernel module
0003  * (C) 2005-2007 Linux Networx (http://lnxi.com)
0004  *
0005  * This file may be distributed under the terms of the
0006  * GNU General Public License.
0007  *
0008  * Written Doug Thompson <norsk5@xmission.com> www.softwarebitmaker.com
0009  *
0010  * (c) 2012-2013 - Mauro Carvalho Chehab
0011  *  The entire API were re-written, and ported to use struct device
0012  *
0013  */
0014 
0015 #include <linux/ctype.h>
0016 #include <linux/slab.h>
0017 #include <linux/edac.h>
0018 #include <linux/bug.h>
0019 #include <linux/pm_runtime.h>
0020 #include <linux/uaccess.h>
0021 
0022 #include "edac_mc.h"
0023 #include "edac_module.h"
0024 
0025 /* MC EDAC Controls, setable by module parameter, and sysfs */
0026 static int edac_mc_log_ue = 1;
0027 static int edac_mc_log_ce = 1;
0028 static int edac_mc_panic_on_ue;
0029 static unsigned int edac_mc_poll_msec = 1000;
0030 
0031 /* Getter functions for above */
0032 int edac_mc_get_log_ue(void)
0033 {
0034     return edac_mc_log_ue;
0035 }
0036 
0037 int edac_mc_get_log_ce(void)
0038 {
0039     return edac_mc_log_ce;
0040 }
0041 
0042 int edac_mc_get_panic_on_ue(void)
0043 {
0044     return edac_mc_panic_on_ue;
0045 }
0046 
0047 /* this is temporary */
0048 unsigned int edac_mc_get_poll_msec(void)
0049 {
0050     return edac_mc_poll_msec;
0051 }
0052 
0053 static int edac_set_poll_msec(const char *val, const struct kernel_param *kp)
0054 {
0055     unsigned int i;
0056     int ret;
0057 
0058     if (!val)
0059         return -EINVAL;
0060 
0061     ret = kstrtouint(val, 0, &i);
0062     if (ret)
0063         return ret;
0064 
0065     if (i < 1000)
0066         return -EINVAL;
0067 
0068     *((unsigned int *)kp->arg) = i;
0069 
0070     /* notify edac_mc engine to reset the poll period */
0071     edac_mc_reset_delay_period(i);
0072 
0073     return 0;
0074 }
0075 
0076 /* Parameter declarations for above */
0077 module_param(edac_mc_panic_on_ue, int, 0644);
0078 MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
0079 module_param(edac_mc_log_ue, int, 0644);
0080 MODULE_PARM_DESC(edac_mc_log_ue,
0081          "Log uncorrectable error to console: 0=off 1=on");
0082 module_param(edac_mc_log_ce, int, 0644);
0083 MODULE_PARM_DESC(edac_mc_log_ce,
0084          "Log correctable error to console: 0=off 1=on");
0085 module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_uint,
0086           &edac_mc_poll_msec, 0644);
0087 MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
0088 
0089 static struct device *mci_pdev;
0090 
0091 /*
0092  * various constants for Memory Controllers
0093  */
0094 static const char * const dev_types[] = {
0095     [DEV_UNKNOWN] = "Unknown",
0096     [DEV_X1] = "x1",
0097     [DEV_X2] = "x2",
0098     [DEV_X4] = "x4",
0099     [DEV_X8] = "x8",
0100     [DEV_X16] = "x16",
0101     [DEV_X32] = "x32",
0102     [DEV_X64] = "x64"
0103 };
0104 
0105 static const char * const edac_caps[] = {
0106     [EDAC_UNKNOWN] = "Unknown",
0107     [EDAC_NONE] = "None",
0108     [EDAC_RESERVED] = "Reserved",
0109     [EDAC_PARITY] = "PARITY",
0110     [EDAC_EC] = "EC",
0111     [EDAC_SECDED] = "SECDED",
0112     [EDAC_S2ECD2ED] = "S2ECD2ED",
0113     [EDAC_S4ECD4ED] = "S4ECD4ED",
0114     [EDAC_S8ECD8ED] = "S8ECD8ED",
0115     [EDAC_S16ECD16ED] = "S16ECD16ED"
0116 };
0117 
0118 #ifdef CONFIG_EDAC_LEGACY_SYSFS
0119 /*
0120  * EDAC sysfs CSROW data structures and methods
0121  */
0122 
0123 #define to_csrow(k) container_of(k, struct csrow_info, dev)
0124 
0125 /*
0126  * We need it to avoid namespace conflicts between the legacy API
0127  * and the per-dimm/per-rank one
0128  */
0129 #define DEVICE_ATTR_LEGACY(_name, _mode, _show, _store) \
0130     static struct device_attribute dev_attr_legacy_##_name = __ATTR(_name, _mode, _show, _store)
0131 
0132 struct dev_ch_attribute {
0133     struct device_attribute attr;
0134     unsigned int channel;
0135 };
0136 
0137 #define DEVICE_CHANNEL(_name, _mode, _show, _store, _var) \
0138     static struct dev_ch_attribute dev_attr_legacy_##_name = \
0139         { __ATTR(_name, _mode, _show, _store), (_var) }
0140 
0141 #define to_channel(k) (container_of(k, struct dev_ch_attribute, attr)->channel)
0142 
0143 /* Set of more default csrow<id> attribute show/store functions */
0144 static ssize_t csrow_ue_count_show(struct device *dev,
0145                    struct device_attribute *mattr, char *data)
0146 {
0147     struct csrow_info *csrow = to_csrow(dev);
0148 
0149     return sprintf(data, "%u\n", csrow->ue_count);
0150 }
0151 
0152 static ssize_t csrow_ce_count_show(struct device *dev,
0153                    struct device_attribute *mattr, char *data)
0154 {
0155     struct csrow_info *csrow = to_csrow(dev);
0156 
0157     return sprintf(data, "%u\n", csrow->ce_count);
0158 }
0159 
0160 static ssize_t csrow_size_show(struct device *dev,
0161                    struct device_attribute *mattr, char *data)
0162 {
0163     struct csrow_info *csrow = to_csrow(dev);
0164     int i;
0165     u32 nr_pages = 0;
0166 
0167     for (i = 0; i < csrow->nr_channels; i++)
0168         nr_pages += csrow->channels[i]->dimm->nr_pages;
0169     return sprintf(data, "%u\n", PAGES_TO_MiB(nr_pages));
0170 }
0171 
0172 static ssize_t csrow_mem_type_show(struct device *dev,
0173                    struct device_attribute *mattr, char *data)
0174 {
0175     struct csrow_info *csrow = to_csrow(dev);
0176 
0177     return sprintf(data, "%s\n", edac_mem_types[csrow->channels[0]->dimm->mtype]);
0178 }
0179 
0180 static ssize_t csrow_dev_type_show(struct device *dev,
0181                    struct device_attribute *mattr, char *data)
0182 {
0183     struct csrow_info *csrow = to_csrow(dev);
0184 
0185     return sprintf(data, "%s\n", dev_types[csrow->channels[0]->dimm->dtype]);
0186 }
0187 
0188 static ssize_t csrow_edac_mode_show(struct device *dev,
0189                     struct device_attribute *mattr,
0190                     char *data)
0191 {
0192     struct csrow_info *csrow = to_csrow(dev);
0193 
0194     return sprintf(data, "%s\n", edac_caps[csrow->channels[0]->dimm->edac_mode]);
0195 }
0196 
0197 /* show/store functions for DIMM Label attributes */
0198 static ssize_t channel_dimm_label_show(struct device *dev,
0199                        struct device_attribute *mattr,
0200                        char *data)
0201 {
0202     struct csrow_info *csrow = to_csrow(dev);
0203     unsigned int chan = to_channel(mattr);
0204     struct rank_info *rank = csrow->channels[chan];
0205 
0206     /* if field has not been initialized, there is nothing to send */
0207     if (!rank->dimm->label[0])
0208         return 0;
0209 
0210     return snprintf(data, sizeof(rank->dimm->label) + 1, "%s\n",
0211             rank->dimm->label);
0212 }
0213 
0214 static ssize_t channel_dimm_label_store(struct device *dev,
0215                     struct device_attribute *mattr,
0216                     const char *data, size_t count)
0217 {
0218     struct csrow_info *csrow = to_csrow(dev);
0219     unsigned int chan = to_channel(mattr);
0220     struct rank_info *rank = csrow->channels[chan];
0221     size_t copy_count = count;
0222 
0223     if (count == 0)
0224         return -EINVAL;
0225 
0226     if (data[count - 1] == '\0' || data[count - 1] == '\n')
0227         copy_count -= 1;
0228 
0229     if (copy_count == 0 || copy_count >= sizeof(rank->dimm->label))
0230         return -EINVAL;
0231 
0232     strncpy(rank->dimm->label, data, copy_count);
0233     rank->dimm->label[copy_count] = '\0';
0234 
0235     return count;
0236 }
0237 
0238 /* show function for dynamic chX_ce_count attribute */
0239 static ssize_t channel_ce_count_show(struct device *dev,
0240                      struct device_attribute *mattr, char *data)
0241 {
0242     struct csrow_info *csrow = to_csrow(dev);
0243     unsigned int chan = to_channel(mattr);
0244     struct rank_info *rank = csrow->channels[chan];
0245 
0246     return sprintf(data, "%u\n", rank->ce_count);
0247 }
0248 
0249 /* cwrow<id>/attribute files */
0250 DEVICE_ATTR_LEGACY(size_mb, S_IRUGO, csrow_size_show, NULL);
0251 DEVICE_ATTR_LEGACY(dev_type, S_IRUGO, csrow_dev_type_show, NULL);
0252 DEVICE_ATTR_LEGACY(mem_type, S_IRUGO, csrow_mem_type_show, NULL);
0253 DEVICE_ATTR_LEGACY(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL);
0254 DEVICE_ATTR_LEGACY(ue_count, S_IRUGO, csrow_ue_count_show, NULL);
0255 DEVICE_ATTR_LEGACY(ce_count, S_IRUGO, csrow_ce_count_show, NULL);
0256 
0257 /* default attributes of the CSROW<id> object */
0258 static struct attribute *csrow_attrs[] = {
0259     &dev_attr_legacy_dev_type.attr,
0260     &dev_attr_legacy_mem_type.attr,
0261     &dev_attr_legacy_edac_mode.attr,
0262     &dev_attr_legacy_size_mb.attr,
0263     &dev_attr_legacy_ue_count.attr,
0264     &dev_attr_legacy_ce_count.attr,
0265     NULL,
0266 };
0267 
0268 static const struct attribute_group csrow_attr_grp = {
0269     .attrs  = csrow_attrs,
0270 };
0271 
0272 static const struct attribute_group *csrow_attr_groups[] = {
0273     &csrow_attr_grp,
0274     NULL
0275 };
0276 
0277 static const struct device_type csrow_attr_type = {
0278     .groups     = csrow_attr_groups,
0279 };
0280 
0281 /*
0282  * possible dynamic channel DIMM Label attribute files
0283  *
0284  */
0285 DEVICE_CHANNEL(ch0_dimm_label, S_IRUGO | S_IWUSR,
0286     channel_dimm_label_show, channel_dimm_label_store, 0);
0287 DEVICE_CHANNEL(ch1_dimm_label, S_IRUGO | S_IWUSR,
0288     channel_dimm_label_show, channel_dimm_label_store, 1);
0289 DEVICE_CHANNEL(ch2_dimm_label, S_IRUGO | S_IWUSR,
0290     channel_dimm_label_show, channel_dimm_label_store, 2);
0291 DEVICE_CHANNEL(ch3_dimm_label, S_IRUGO | S_IWUSR,
0292     channel_dimm_label_show, channel_dimm_label_store, 3);
0293 DEVICE_CHANNEL(ch4_dimm_label, S_IRUGO | S_IWUSR,
0294     channel_dimm_label_show, channel_dimm_label_store, 4);
0295 DEVICE_CHANNEL(ch5_dimm_label, S_IRUGO | S_IWUSR,
0296     channel_dimm_label_show, channel_dimm_label_store, 5);
0297 DEVICE_CHANNEL(ch6_dimm_label, S_IRUGO | S_IWUSR,
0298     channel_dimm_label_show, channel_dimm_label_store, 6);
0299 DEVICE_CHANNEL(ch7_dimm_label, S_IRUGO | S_IWUSR,
0300     channel_dimm_label_show, channel_dimm_label_store, 7);
0301 
0302 /* Total possible dynamic DIMM Label attribute file table */
0303 static struct attribute *dynamic_csrow_dimm_attr[] = {
0304     &dev_attr_legacy_ch0_dimm_label.attr.attr,
0305     &dev_attr_legacy_ch1_dimm_label.attr.attr,
0306     &dev_attr_legacy_ch2_dimm_label.attr.attr,
0307     &dev_attr_legacy_ch3_dimm_label.attr.attr,
0308     &dev_attr_legacy_ch4_dimm_label.attr.attr,
0309     &dev_attr_legacy_ch5_dimm_label.attr.attr,
0310     &dev_attr_legacy_ch6_dimm_label.attr.attr,
0311     &dev_attr_legacy_ch7_dimm_label.attr.attr,
0312     NULL
0313 };
0314 
0315 /* possible dynamic channel ce_count attribute files */
0316 DEVICE_CHANNEL(ch0_ce_count, S_IRUGO,
0317            channel_ce_count_show, NULL, 0);
0318 DEVICE_CHANNEL(ch1_ce_count, S_IRUGO,
0319            channel_ce_count_show, NULL, 1);
0320 DEVICE_CHANNEL(ch2_ce_count, S_IRUGO,
0321            channel_ce_count_show, NULL, 2);
0322 DEVICE_CHANNEL(ch3_ce_count, S_IRUGO,
0323            channel_ce_count_show, NULL, 3);
0324 DEVICE_CHANNEL(ch4_ce_count, S_IRUGO,
0325            channel_ce_count_show, NULL, 4);
0326 DEVICE_CHANNEL(ch5_ce_count, S_IRUGO,
0327            channel_ce_count_show, NULL, 5);
0328 DEVICE_CHANNEL(ch6_ce_count, S_IRUGO,
0329            channel_ce_count_show, NULL, 6);
0330 DEVICE_CHANNEL(ch7_ce_count, S_IRUGO,
0331            channel_ce_count_show, NULL, 7);
0332 
0333 /* Total possible dynamic ce_count attribute file table */
0334 static struct attribute *dynamic_csrow_ce_count_attr[] = {
0335     &dev_attr_legacy_ch0_ce_count.attr.attr,
0336     &dev_attr_legacy_ch1_ce_count.attr.attr,
0337     &dev_attr_legacy_ch2_ce_count.attr.attr,
0338     &dev_attr_legacy_ch3_ce_count.attr.attr,
0339     &dev_attr_legacy_ch4_ce_count.attr.attr,
0340     &dev_attr_legacy_ch5_ce_count.attr.attr,
0341     &dev_attr_legacy_ch6_ce_count.attr.attr,
0342     &dev_attr_legacy_ch7_ce_count.attr.attr,
0343     NULL
0344 };
0345 
0346 static umode_t csrow_dev_is_visible(struct kobject *kobj,
0347                     struct attribute *attr, int idx)
0348 {
0349     struct device *dev = kobj_to_dev(kobj);
0350     struct csrow_info *csrow = container_of(dev, struct csrow_info, dev);
0351 
0352     if (idx >= csrow->nr_channels)
0353         return 0;
0354 
0355     if (idx >= ARRAY_SIZE(dynamic_csrow_ce_count_attr) - 1) {
0356         WARN_ONCE(1, "idx: %d\n", idx);
0357         return 0;
0358     }
0359 
0360     /* Only expose populated DIMMs */
0361     if (!csrow->channels[idx]->dimm->nr_pages)
0362         return 0;
0363 
0364     return attr->mode;
0365 }
0366 
0367 
0368 static const struct attribute_group csrow_dev_dimm_group = {
0369     .attrs = dynamic_csrow_dimm_attr,
0370     .is_visible = csrow_dev_is_visible,
0371 };
0372 
0373 static const struct attribute_group csrow_dev_ce_count_group = {
0374     .attrs = dynamic_csrow_ce_count_attr,
0375     .is_visible = csrow_dev_is_visible,
0376 };
0377 
0378 static const struct attribute_group *csrow_dev_groups[] = {
0379     &csrow_dev_dimm_group,
0380     &csrow_dev_ce_count_group,
0381     NULL
0382 };
0383 
0384 static void csrow_release(struct device *dev)
0385 {
0386     /*
0387      * Nothing to do, just unregister sysfs here. The mci
0388      * device owns the data and will also release it.
0389      */
0390 }
0391 
0392 static inline int nr_pages_per_csrow(struct csrow_info *csrow)
0393 {
0394     int chan, nr_pages = 0;
0395 
0396     for (chan = 0; chan < csrow->nr_channels; chan++)
0397         nr_pages += csrow->channels[chan]->dimm->nr_pages;
0398 
0399     return nr_pages;
0400 }
0401 
0402 /* Create a CSROW object under specifed edac_mc_device */
0403 static int edac_create_csrow_object(struct mem_ctl_info *mci,
0404                     struct csrow_info *csrow, int index)
0405 {
0406     int err;
0407 
0408     csrow->dev.type = &csrow_attr_type;
0409     csrow->dev.groups = csrow_dev_groups;
0410     csrow->dev.release = csrow_release;
0411     device_initialize(&csrow->dev);
0412     csrow->dev.parent = &mci->dev;
0413     csrow->mci = mci;
0414     dev_set_name(&csrow->dev, "csrow%d", index);
0415     dev_set_drvdata(&csrow->dev, csrow);
0416 
0417     err = device_add(&csrow->dev);
0418     if (err) {
0419         edac_dbg(1, "failure: create device %s\n", dev_name(&csrow->dev));
0420         put_device(&csrow->dev);
0421         return err;
0422     }
0423 
0424     edac_dbg(0, "device %s created\n", dev_name(&csrow->dev));
0425 
0426     return 0;
0427 }
0428 
0429 /* Create a CSROW object under specifed edac_mc_device */
0430 static int edac_create_csrow_objects(struct mem_ctl_info *mci)
0431 {
0432     int err, i;
0433     struct csrow_info *csrow;
0434 
0435     for (i = 0; i < mci->nr_csrows; i++) {
0436         csrow = mci->csrows[i];
0437         if (!nr_pages_per_csrow(csrow))
0438             continue;
0439         err = edac_create_csrow_object(mci, mci->csrows[i], i);
0440         if (err < 0)
0441             goto error;
0442     }
0443     return 0;
0444 
0445 error:
0446     for (--i; i >= 0; i--) {
0447         if (device_is_registered(&mci->csrows[i]->dev))
0448             device_unregister(&mci->csrows[i]->dev);
0449     }
0450 
0451     return err;
0452 }
0453 
0454 static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
0455 {
0456     int i;
0457 
0458     for (i = 0; i < mci->nr_csrows; i++) {
0459         if (device_is_registered(&mci->csrows[i]->dev))
0460             device_unregister(&mci->csrows[i]->dev);
0461     }
0462 }
0463 
0464 #endif
0465 
0466 /*
0467  * Per-dimm (or per-rank) devices
0468  */
0469 
0470 #define to_dimm(k) container_of(k, struct dimm_info, dev)
0471 
0472 /* show/store functions for DIMM Label attributes */
0473 static ssize_t dimmdev_location_show(struct device *dev,
0474                      struct device_attribute *mattr, char *data)
0475 {
0476     struct dimm_info *dimm = to_dimm(dev);
0477     ssize_t count;
0478 
0479     count = edac_dimm_info_location(dimm, data, PAGE_SIZE);
0480     count += scnprintf(data + count, PAGE_SIZE - count, "\n");
0481 
0482     return count;
0483 }
0484 
0485 static ssize_t dimmdev_label_show(struct device *dev,
0486                   struct device_attribute *mattr, char *data)
0487 {
0488     struct dimm_info *dimm = to_dimm(dev);
0489 
0490     /* if field has not been initialized, there is nothing to send */
0491     if (!dimm->label[0])
0492         return 0;
0493 
0494     return snprintf(data, sizeof(dimm->label) + 1, "%s\n", dimm->label);
0495 }
0496 
0497 static ssize_t dimmdev_label_store(struct device *dev,
0498                    struct device_attribute *mattr,
0499                    const char *data,
0500                    size_t count)
0501 {
0502     struct dimm_info *dimm = to_dimm(dev);
0503     size_t copy_count = count;
0504 
0505     if (count == 0)
0506         return -EINVAL;
0507 
0508     if (data[count - 1] == '\0' || data[count - 1] == '\n')
0509         copy_count -= 1;
0510 
0511     if (copy_count == 0 || copy_count >= sizeof(dimm->label))
0512         return -EINVAL;
0513 
0514     strncpy(dimm->label, data, copy_count);
0515     dimm->label[copy_count] = '\0';
0516 
0517     return count;
0518 }
0519 
0520 static ssize_t dimmdev_size_show(struct device *dev,
0521                  struct device_attribute *mattr, char *data)
0522 {
0523     struct dimm_info *dimm = to_dimm(dev);
0524 
0525     return sprintf(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
0526 }
0527 
0528 static ssize_t dimmdev_mem_type_show(struct device *dev,
0529                      struct device_attribute *mattr, char *data)
0530 {
0531     struct dimm_info *dimm = to_dimm(dev);
0532 
0533     return sprintf(data, "%s\n", edac_mem_types[dimm->mtype]);
0534 }
0535 
0536 static ssize_t dimmdev_dev_type_show(struct device *dev,
0537                      struct device_attribute *mattr, char *data)
0538 {
0539     struct dimm_info *dimm = to_dimm(dev);
0540 
0541     return sprintf(data, "%s\n", dev_types[dimm->dtype]);
0542 }
0543 
0544 static ssize_t dimmdev_edac_mode_show(struct device *dev,
0545                       struct device_attribute *mattr,
0546                       char *data)
0547 {
0548     struct dimm_info *dimm = to_dimm(dev);
0549 
0550     return sprintf(data, "%s\n", edac_caps[dimm->edac_mode]);
0551 }
0552 
0553 static ssize_t dimmdev_ce_count_show(struct device *dev,
0554                       struct device_attribute *mattr,
0555                       char *data)
0556 {
0557     struct dimm_info *dimm = to_dimm(dev);
0558 
0559     return sprintf(data, "%u\n", dimm->ce_count);
0560 }
0561 
0562 static ssize_t dimmdev_ue_count_show(struct device *dev,
0563                       struct device_attribute *mattr,
0564                       char *data)
0565 {
0566     struct dimm_info *dimm = to_dimm(dev);
0567 
0568     return sprintf(data, "%u\n", dimm->ue_count);
0569 }
0570 
0571 /* dimm/rank attribute files */
0572 static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
0573            dimmdev_label_show, dimmdev_label_store);
0574 static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
0575 static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
0576 static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
0577 static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
0578 static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
0579 static DEVICE_ATTR(dimm_ce_count, S_IRUGO, dimmdev_ce_count_show, NULL);
0580 static DEVICE_ATTR(dimm_ue_count, S_IRUGO, dimmdev_ue_count_show, NULL);
0581 
0582 /* attributes of the dimm<id>/rank<id> object */
0583 static struct attribute *dimm_attrs[] = {
0584     &dev_attr_dimm_label.attr,
0585     &dev_attr_dimm_location.attr,
0586     &dev_attr_size.attr,
0587     &dev_attr_dimm_mem_type.attr,
0588     &dev_attr_dimm_dev_type.attr,
0589     &dev_attr_dimm_edac_mode.attr,
0590     &dev_attr_dimm_ce_count.attr,
0591     &dev_attr_dimm_ue_count.attr,
0592     NULL,
0593 };
0594 
0595 static const struct attribute_group dimm_attr_grp = {
0596     .attrs  = dimm_attrs,
0597 };
0598 
0599 static const struct attribute_group *dimm_attr_groups[] = {
0600     &dimm_attr_grp,
0601     NULL
0602 };
0603 
0604 static const struct device_type dimm_attr_type = {
0605     .groups     = dimm_attr_groups,
0606 };
0607 
0608 static void dimm_release(struct device *dev)
0609 {
0610     /*
0611      * Nothing to do, just unregister sysfs here. The mci
0612      * device owns the data and will also release it.
0613      */
0614 }
0615 
0616 /* Create a DIMM object under specifed memory controller device */
0617 static int edac_create_dimm_object(struct mem_ctl_info *mci,
0618                    struct dimm_info *dimm)
0619 {
0620     int err;
0621     dimm->mci = mci;
0622 
0623     dimm->dev.type = &dimm_attr_type;
0624     dimm->dev.release = dimm_release;
0625     device_initialize(&dimm->dev);
0626 
0627     dimm->dev.parent = &mci->dev;
0628     if (mci->csbased)
0629         dev_set_name(&dimm->dev, "rank%d", dimm->idx);
0630     else
0631         dev_set_name(&dimm->dev, "dimm%d", dimm->idx);
0632     dev_set_drvdata(&dimm->dev, dimm);
0633     pm_runtime_forbid(&mci->dev);
0634 
0635     err = device_add(&dimm->dev);
0636     if (err) {
0637         edac_dbg(1, "failure: create device %s\n", dev_name(&dimm->dev));
0638         put_device(&dimm->dev);
0639         return err;
0640     }
0641 
0642     if (IS_ENABLED(CONFIG_EDAC_DEBUG)) {
0643         char location[80];
0644 
0645         edac_dimm_info_location(dimm, location, sizeof(location));
0646         edac_dbg(0, "device %s created at location %s\n",
0647             dev_name(&dimm->dev), location);
0648     }
0649 
0650     return 0;
0651 }
0652 
0653 /*
0654  * Memory controller device
0655  */
0656 
0657 #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
0658 
0659 static ssize_t mci_reset_counters_store(struct device *dev,
0660                     struct device_attribute *mattr,
0661                     const char *data, size_t count)
0662 {
0663     struct mem_ctl_info *mci = to_mci(dev);
0664     struct dimm_info *dimm;
0665     int row, chan;
0666 
0667     mci->ue_mc = 0;
0668     mci->ce_mc = 0;
0669     mci->ue_noinfo_count = 0;
0670     mci->ce_noinfo_count = 0;
0671 
0672     for (row = 0; row < mci->nr_csrows; row++) {
0673         struct csrow_info *ri = mci->csrows[row];
0674 
0675         ri->ue_count = 0;
0676         ri->ce_count = 0;
0677 
0678         for (chan = 0; chan < ri->nr_channels; chan++)
0679             ri->channels[chan]->ce_count = 0;
0680     }
0681 
0682     mci_for_each_dimm(mci, dimm) {
0683         dimm->ue_count = 0;
0684         dimm->ce_count = 0;
0685     }
0686 
0687     mci->start_time = jiffies;
0688     return count;
0689 }
0690 
0691 /* Memory scrubbing interface:
0692  *
0693  * A MC driver can limit the scrubbing bandwidth based on the CPU type.
0694  * Therefore, ->set_sdram_scrub_rate should be made to return the actual
0695  * bandwidth that is accepted or 0 when scrubbing is to be disabled.
0696  *
0697  * Negative value still means that an error has occurred while setting
0698  * the scrub rate.
0699  */
0700 static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
0701                       struct device_attribute *mattr,
0702                       const char *data, size_t count)
0703 {
0704     struct mem_ctl_info *mci = to_mci(dev);
0705     unsigned long bandwidth = 0;
0706     int new_bw = 0;
0707 
0708     if (kstrtoul(data, 10, &bandwidth) < 0)
0709         return -EINVAL;
0710 
0711     new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
0712     if (new_bw < 0) {
0713         edac_printk(KERN_WARNING, EDAC_MC,
0714                 "Error setting scrub rate to: %lu\n", bandwidth);
0715         return -EINVAL;
0716     }
0717 
0718     return count;
0719 }
0720 
0721 /*
0722  * ->get_sdram_scrub_rate() return value semantics same as above.
0723  */
0724 static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
0725                      struct device_attribute *mattr,
0726                      char *data)
0727 {
0728     struct mem_ctl_info *mci = to_mci(dev);
0729     int bandwidth = 0;
0730 
0731     bandwidth = mci->get_sdram_scrub_rate(mci);
0732     if (bandwidth < 0) {
0733         edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
0734         return bandwidth;
0735     }
0736 
0737     return sprintf(data, "%d\n", bandwidth);
0738 }
0739 
0740 /* default attribute files for the MCI object */
0741 static ssize_t mci_ue_count_show(struct device *dev,
0742                  struct device_attribute *mattr,
0743                  char *data)
0744 {
0745     struct mem_ctl_info *mci = to_mci(dev);
0746 
0747     return sprintf(data, "%u\n", mci->ue_mc);
0748 }
0749 
0750 static ssize_t mci_ce_count_show(struct device *dev,
0751                  struct device_attribute *mattr,
0752                  char *data)
0753 {
0754     struct mem_ctl_info *mci = to_mci(dev);
0755 
0756     return sprintf(data, "%u\n", mci->ce_mc);
0757 }
0758 
0759 static ssize_t mci_ce_noinfo_show(struct device *dev,
0760                   struct device_attribute *mattr,
0761                   char *data)
0762 {
0763     struct mem_ctl_info *mci = to_mci(dev);
0764 
0765     return sprintf(data, "%u\n", mci->ce_noinfo_count);
0766 }
0767 
0768 static ssize_t mci_ue_noinfo_show(struct device *dev,
0769                   struct device_attribute *mattr,
0770                   char *data)
0771 {
0772     struct mem_ctl_info *mci = to_mci(dev);
0773 
0774     return sprintf(data, "%u\n", mci->ue_noinfo_count);
0775 }
0776 
0777 static ssize_t mci_seconds_show(struct device *dev,
0778                 struct device_attribute *mattr,
0779                 char *data)
0780 {
0781     struct mem_ctl_info *mci = to_mci(dev);
0782 
0783     return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
0784 }
0785 
0786 static ssize_t mci_ctl_name_show(struct device *dev,
0787                  struct device_attribute *mattr,
0788                  char *data)
0789 {
0790     struct mem_ctl_info *mci = to_mci(dev);
0791 
0792     return sprintf(data, "%s\n", mci->ctl_name);
0793 }
0794 
0795 static ssize_t mci_size_mb_show(struct device *dev,
0796                 struct device_attribute *mattr,
0797                 char *data)
0798 {
0799     struct mem_ctl_info *mci = to_mci(dev);
0800     int total_pages = 0, csrow_idx, j;
0801 
0802     for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
0803         struct csrow_info *csrow = mci->csrows[csrow_idx];
0804 
0805         for (j = 0; j < csrow->nr_channels; j++) {
0806             struct dimm_info *dimm = csrow->channels[j]->dimm;
0807 
0808             total_pages += dimm->nr_pages;
0809         }
0810     }
0811 
0812     return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
0813 }
0814 
0815 static ssize_t mci_max_location_show(struct device *dev,
0816                      struct device_attribute *mattr,
0817                      char *data)
0818 {
0819     struct mem_ctl_info *mci = to_mci(dev);
0820     int len = PAGE_SIZE;
0821     char *p = data;
0822     int i, n;
0823 
0824     for (i = 0; i < mci->n_layers; i++) {
0825         n = scnprintf(p, len, "%s %d ",
0826                   edac_layer_name[mci->layers[i].type],
0827                   mci->layers[i].size - 1);
0828         len -= n;
0829         if (len <= 0)
0830             goto out;
0831 
0832         p += n;
0833     }
0834 
0835     p += scnprintf(p, len, "\n");
0836 out:
0837     return p - data;
0838 }
0839 
0840 /* default Control file */
0841 static DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
0842 
0843 /* default Attribute files */
0844 static DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
0845 static DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
0846 static DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
0847 static DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
0848 static DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
0849 static DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
0850 static DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
0851 static DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
0852 
0853 /* memory scrubber attribute file */
0854 static DEVICE_ATTR(sdram_scrub_rate, 0, mci_sdram_scrub_rate_show,
0855         mci_sdram_scrub_rate_store); /* umode set later in is_visible */
0856 
0857 static struct attribute *mci_attrs[] = {
0858     &dev_attr_reset_counters.attr,
0859     &dev_attr_mc_name.attr,
0860     &dev_attr_size_mb.attr,
0861     &dev_attr_seconds_since_reset.attr,
0862     &dev_attr_ue_noinfo_count.attr,
0863     &dev_attr_ce_noinfo_count.attr,
0864     &dev_attr_ue_count.attr,
0865     &dev_attr_ce_count.attr,
0866     &dev_attr_max_location.attr,
0867     &dev_attr_sdram_scrub_rate.attr,
0868     NULL
0869 };
0870 
0871 static umode_t mci_attr_is_visible(struct kobject *kobj,
0872                    struct attribute *attr, int idx)
0873 {
0874     struct device *dev = kobj_to_dev(kobj);
0875     struct mem_ctl_info *mci = to_mci(dev);
0876     umode_t mode = 0;
0877 
0878     if (attr != &dev_attr_sdram_scrub_rate.attr)
0879         return attr->mode;
0880     if (mci->get_sdram_scrub_rate)
0881         mode |= S_IRUGO;
0882     if (mci->set_sdram_scrub_rate)
0883         mode |= S_IWUSR;
0884     return mode;
0885 }
0886 
0887 static const struct attribute_group mci_attr_grp = {
0888     .attrs  = mci_attrs,
0889     .is_visible = mci_attr_is_visible,
0890 };
0891 
0892 static const struct attribute_group *mci_attr_groups[] = {
0893     &mci_attr_grp,
0894     NULL
0895 };
0896 
0897 static const struct device_type mci_attr_type = {
0898     .groups     = mci_attr_groups,
0899 };
0900 
0901 /*
0902  * Create a new Memory Controller kobject instance,
0903  *  mc<id> under the 'mc' directory
0904  *
0905  * Return:
0906  *  0   Success
0907  *  !0  Failure
0908  */
0909 int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
0910                  const struct attribute_group **groups)
0911 {
0912     struct dimm_info *dimm;
0913     int err;
0914 
0915     /* get the /sys/devices/system/edac subsys reference */
0916     mci->dev.type = &mci_attr_type;
0917     mci->dev.parent = mci_pdev;
0918     mci->dev.groups = groups;
0919     dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
0920     dev_set_drvdata(&mci->dev, mci);
0921     pm_runtime_forbid(&mci->dev);
0922 
0923     err = device_add(&mci->dev);
0924     if (err < 0) {
0925         edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
0926         /* no put_device() here, free mci with _edac_mc_free() */
0927         return err;
0928     }
0929 
0930     edac_dbg(0, "device %s created\n", dev_name(&mci->dev));
0931 
0932     /*
0933      * Create the dimm/rank devices
0934      */
0935     mci_for_each_dimm(mci, dimm) {
0936         /* Only expose populated DIMMs */
0937         if (!dimm->nr_pages)
0938             continue;
0939 
0940         err = edac_create_dimm_object(mci, dimm);
0941         if (err)
0942             goto fail;
0943     }
0944 
0945 #ifdef CONFIG_EDAC_LEGACY_SYSFS
0946     err = edac_create_csrow_objects(mci);
0947     if (err < 0)
0948         goto fail;
0949 #endif
0950 
0951     edac_create_debugfs_nodes(mci);
0952     return 0;
0953 
0954 fail:
0955     edac_remove_sysfs_mci_device(mci);
0956 
0957     return err;
0958 }
0959 
0960 /*
0961  * remove a Memory Controller instance
0962  */
0963 void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
0964 {
0965     struct dimm_info *dimm;
0966 
0967     if (!device_is_registered(&mci->dev))
0968         return;
0969 
0970     edac_dbg(0, "\n");
0971 
0972 #ifdef CONFIG_EDAC_DEBUG
0973     edac_debugfs_remove_recursive(mci->debugfs);
0974 #endif
0975 #ifdef CONFIG_EDAC_LEGACY_SYSFS
0976     edac_delete_csrow_objects(mci);
0977 #endif
0978 
0979     mci_for_each_dimm(mci, dimm) {
0980         if (!device_is_registered(&dimm->dev))
0981             continue;
0982         edac_dbg(1, "unregistering device %s\n", dev_name(&dimm->dev));
0983         device_unregister(&dimm->dev);
0984     }
0985 
0986     /* only remove the device, but keep mci */
0987     device_del(&mci->dev);
0988 }
0989 
0990 static void mc_attr_release(struct device *dev)
0991 {
0992     /*
0993      * There's no container structure here, as this is just the mci
0994      * parent device, used to create the /sys/devices/mc sysfs node.
0995      * So, there are no attributes on it.
0996      */
0997     edac_dbg(1, "device %s released\n", dev_name(dev));
0998     kfree(dev);
0999 }
1000 
1001 /*
1002  * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1003  */
1004 int __init edac_mc_sysfs_init(void)
1005 {
1006     int err;
1007 
1008     mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
1009     if (!mci_pdev)
1010         return -ENOMEM;
1011 
1012     mci_pdev->bus = edac_get_sysfs_subsys();
1013     mci_pdev->release = mc_attr_release;
1014     mci_pdev->init_name = "mc";
1015 
1016     err = device_register(mci_pdev);
1017     if (err < 0) {
1018         edac_dbg(1, "failure: create device %s\n", dev_name(mci_pdev));
1019         put_device(mci_pdev);
1020         return err;
1021     }
1022 
1023     edac_dbg(0, "device %s created\n", dev_name(mci_pdev));
1024 
1025     return 0;
1026 }
1027 
1028 void edac_mc_sysfs_exit(void)
1029 {
1030     device_unregister(mci_pdev);
1031 }