blob: 86da1767f86ed6602843baa516b4f8aee8786827 [file] [log] [blame]
Douglas Thompson7c9281d2007-07-19 01:49:33 -07001/*
2 * edac_mc kernel module
Douglas Thompson42a8e392007-07-19 01:50:10 -07003 * (C) 2005-2007 Linux Networx (http://lnxi.com)
4 *
Douglas Thompson7c9281d2007-07-19 01:49:33 -07005 * This file may be distributed under the terms of the
6 * GNU General Public License.
7 *
Douglas Thompson42a8e392007-07-19 01:50:10 -07008 * Written Doug Thompson <norsk5@xmission.com> www.softwarebitmaker.com
Douglas Thompson7c9281d2007-07-19 01:49:33 -07009 *
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -030010 * (c) 2012 - Mauro Carvalho Chehab <mchehab@redhat.com>
11 * The entire API were re-written, and ported to use struct device
12 *
Douglas Thompson7c9281d2007-07-19 01:49:33 -070013 */
14
Douglas Thompson7c9281d2007-07-19 01:49:33 -070015#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Borislav Petkov30e1f7a2010-09-02 17:26:48 +020017#include <linux/edac.h>
Doug Thompson8096cfa2007-07-19 01:50:27 -070018#include <linux/bug.h>
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -030019#include <linux/pm_runtime.h>
Douglas Thompson7c9281d2007-07-19 01:49:33 -070020
Douglas Thompson20bcb7a2007-07-19 01:49:47 -070021#include "edac_core.h"
Douglas Thompson7c9281d2007-07-19 01:49:33 -070022#include "edac_module.h"
23
24/* MC EDAC Controls, setable by module parameter, and sysfs */
Dave Jiang4de78c62007-07-19 01:49:54 -070025static int edac_mc_log_ue = 1;
26static int edac_mc_log_ce = 1;
Douglas Thompsonf0440912007-07-19 01:50:19 -070027static int edac_mc_panic_on_ue;
Dave Jiang4de78c62007-07-19 01:49:54 -070028static int edac_mc_poll_msec = 1000;
Douglas Thompson7c9281d2007-07-19 01:49:33 -070029
30/* Getter functions for above */
Dave Jiang4de78c62007-07-19 01:49:54 -070031int edac_mc_get_log_ue(void)
Douglas Thompson7c9281d2007-07-19 01:49:33 -070032{
Dave Jiang4de78c62007-07-19 01:49:54 -070033 return edac_mc_log_ue;
Douglas Thompson7c9281d2007-07-19 01:49:33 -070034}
35
Dave Jiang4de78c62007-07-19 01:49:54 -070036int edac_mc_get_log_ce(void)
Douglas Thompson7c9281d2007-07-19 01:49:33 -070037{
Dave Jiang4de78c62007-07-19 01:49:54 -070038 return edac_mc_log_ce;
Douglas Thompson7c9281d2007-07-19 01:49:33 -070039}
40
Dave Jiang4de78c62007-07-19 01:49:54 -070041int edac_mc_get_panic_on_ue(void)
Douglas Thompson7c9281d2007-07-19 01:49:33 -070042{
Dave Jiang4de78c62007-07-19 01:49:54 -070043 return edac_mc_panic_on_ue;
Douglas Thompson7c9281d2007-07-19 01:49:33 -070044}
45
Dave Jiang81d87cb2007-07-19 01:49:52 -070046/* this is temporary */
47int edac_mc_get_poll_msec(void)
48{
Dave Jiang4de78c62007-07-19 01:49:54 -070049 return edac_mc_poll_msec;
Douglas Thompson7c9281d2007-07-19 01:49:33 -070050}
51
Arthur Jones096846e2008-07-25 01:49:09 -070052static int edac_set_poll_msec(const char *val, struct kernel_param *kp)
53{
54 long l;
55 int ret;
56
57 if (!val)
58 return -EINVAL;
59
60 ret = strict_strtol(val, 0, &l);
61 if (ret == -EINVAL || ((int)l != l))
62 return -EINVAL;
63 *((int *)kp->arg) = l;
64
65 /* notify edac_mc engine to reset the poll period */
66 edac_mc_reset_delay_period(l);
67
68 return 0;
69}
70
Douglas Thompson7c9281d2007-07-19 01:49:33 -070071/* Parameter declarations for above */
Dave Jiang4de78c62007-07-19 01:49:54 -070072module_param(edac_mc_panic_on_ue, int, 0644);
73MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
74module_param(edac_mc_log_ue, int, 0644);
75MODULE_PARM_DESC(edac_mc_log_ue,
Douglas Thompson079708b2007-07-19 01:49:58 -070076 "Log uncorrectable error to console: 0=off 1=on");
Dave Jiang4de78c62007-07-19 01:49:54 -070077module_param(edac_mc_log_ce, int, 0644);
78MODULE_PARM_DESC(edac_mc_log_ce,
Douglas Thompson079708b2007-07-19 01:49:58 -070079 "Log correctable error to console: 0=off 1=on");
Arthur Jones096846e2008-07-25 01:49:09 -070080module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_int,
81 &edac_mc_poll_msec, 0644);
Dave Jiang4de78c62007-07-19 01:49:54 -070082MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
Douglas Thompson7c9281d2007-07-19 01:49:33 -070083
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -030084static struct device mci_pdev;
85
Douglas Thompson7c9281d2007-07-19 01:49:33 -070086/*
87 * various constants for Memory Controllers
88 */
89static const char *mem_types[] = {
90 [MEM_EMPTY] = "Empty",
91 [MEM_RESERVED] = "Reserved",
92 [MEM_UNKNOWN] = "Unknown",
93 [MEM_FPM] = "FPM",
94 [MEM_EDO] = "EDO",
95 [MEM_BEDO] = "BEDO",
96 [MEM_SDR] = "Unbuffered-SDR",
97 [MEM_RDR] = "Registered-SDR",
98 [MEM_DDR] = "Unbuffered-DDR",
99 [MEM_RDDR] = "Registered-DDR",
Dave Jiang1a9b85e2007-07-19 01:49:38 -0700100 [MEM_RMBS] = "RMBS",
101 [MEM_DDR2] = "Unbuffered-DDR2",
102 [MEM_FB_DDR2] = "FullyBuffered-DDR2",
Benjamin Herrenschmidt1d5f7262008-02-07 00:14:52 -0800103 [MEM_RDDR2] = "Registered-DDR2",
Yang Shib1cfebc2009-06-30 11:41:22 -0700104 [MEM_XDR] = "XDR",
105 [MEM_DDR3] = "Unbuffered-DDR3",
106 [MEM_RDDR3] = "Registered-DDR3"
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700107};
108
109static const char *dev_types[] = {
110 [DEV_UNKNOWN] = "Unknown",
111 [DEV_X1] = "x1",
112 [DEV_X2] = "x2",
113 [DEV_X4] = "x4",
114 [DEV_X8] = "x8",
115 [DEV_X16] = "x16",
116 [DEV_X32] = "x32",
117 [DEV_X64] = "x64"
118};
119
120static const char *edac_caps[] = {
121 [EDAC_UNKNOWN] = "Unknown",
122 [EDAC_NONE] = "None",
123 [EDAC_RESERVED] = "Reserved",
124 [EDAC_PARITY] = "PARITY",
125 [EDAC_EC] = "EC",
126 [EDAC_SECDED] = "SECDED",
127 [EDAC_S2ECD2ED] = "S2ECD2ED",
128 [EDAC_S4ECD4ED] = "S4ECD4ED",
129 [EDAC_S8ECD8ED] = "S8ECD8ED",
130 [EDAC_S16ECD16ED] = "S16ECD16ED"
131};
132
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300133#ifdef CONFIG_EDAC_LEGACY_SYSFS
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300134/*
135 * EDAC sysfs CSROW data structures and methods
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700136 */
137
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300138#define to_csrow(k) container_of(k, struct csrow_info, dev)
139
140/*
141 * We need it to avoid namespace conflicts between the legacy API
142 * and the per-dimm/per-rank one
143 */
144#define DEVICE_ATTR_LEGACY(_name, _mode, _show, _store) \
145 struct device_attribute dev_attr_legacy_##_name = __ATTR(_name, _mode, _show, _store)
146
147struct dev_ch_attribute {
148 struct device_attribute attr;
149 int channel;
150};
151
152#define DEVICE_CHANNEL(_name, _mode, _show, _store, _var) \
153 struct dev_ch_attribute dev_attr_legacy_##_name = \
154 { __ATTR(_name, _mode, _show, _store), (_var) }
155
156#define to_channel(k) (container_of(k, struct dev_ch_attribute, attr)->channel)
157
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700158/* Set of more default csrow<id> attribute show/store functions */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300159static ssize_t csrow_ue_count_show(struct device *dev,
160 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700161{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300162 struct csrow_info *csrow = to_csrow(dev);
163
Douglas Thompson079708b2007-07-19 01:49:58 -0700164 return sprintf(data, "%u\n", csrow->ue_count);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700165}
166
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300167static ssize_t csrow_ce_count_show(struct device *dev,
168 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700169{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300170 struct csrow_info *csrow = to_csrow(dev);
171
Douglas Thompson079708b2007-07-19 01:49:58 -0700172 return sprintf(data, "%u\n", csrow->ce_count);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700173}
174
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300175static ssize_t csrow_size_show(struct device *dev,
176 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700177{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300178 struct csrow_info *csrow = to_csrow(dev);
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300179 int i;
180 u32 nr_pages = 0;
181
182 for (i = 0; i < csrow->nr_channels; i++)
183 nr_pages += csrow->channels[i].dimm->nr_pages;
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300184 return sprintf(data, "%u\n", PAGES_TO_MiB(nr_pages));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700185}
186
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300187static ssize_t csrow_mem_type_show(struct device *dev,
188 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700189{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300190 struct csrow_info *csrow = to_csrow(dev);
191
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300192 return sprintf(data, "%s\n", mem_types[csrow->channels[0].dimm->mtype]);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700193}
194
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300195static ssize_t csrow_dev_type_show(struct device *dev,
196 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700197{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300198 struct csrow_info *csrow = to_csrow(dev);
199
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300200 return sprintf(data, "%s\n", dev_types[csrow->channels[0].dimm->dtype]);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700201}
202
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300203static ssize_t csrow_edac_mode_show(struct device *dev,
204 struct device_attribute *mattr,
205 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700206{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300207 struct csrow_info *csrow = to_csrow(dev);
208
Mauro Carvalho Chehab084a4fc2012-01-27 18:38:08 -0300209 return sprintf(data, "%s\n", edac_caps[csrow->channels[0].dimm->edac_mode]);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700210}
211
212/* show/store functions for DIMM Label attributes */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300213static ssize_t channel_dimm_label_show(struct device *dev,
214 struct device_attribute *mattr,
215 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700216{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300217 struct csrow_info *csrow = to_csrow(dev);
218 unsigned chan = to_channel(mattr);
219 struct rank_info *rank = &csrow->channels[chan];
220
Arthur Jones124682c2008-07-25 01:49:12 -0700221 /* if field has not been initialized, there is nothing to send */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300222 if (!rank->dimm->label[0])
Arthur Jones124682c2008-07-25 01:49:12 -0700223 return 0;
224
225 return snprintf(data, EDAC_MC_LABEL_LEN, "%s\n",
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300226 rank->dimm->label);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700227}
228
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300229static ssize_t channel_dimm_label_store(struct device *dev,
230 struct device_attribute *mattr,
231 const char *data, size_t count)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700232{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300233 struct csrow_info *csrow = to_csrow(dev);
234 unsigned chan = to_channel(mattr);
235 struct rank_info *rank = &csrow->channels[chan];
236
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700237 ssize_t max_size = 0;
238
Douglas Thompson079708b2007-07-19 01:49:58 -0700239 max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300240 strncpy(rank->dimm->label, data, max_size);
241 rank->dimm->label[max_size] = '\0';
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700242
243 return max_size;
244}
245
246/* show function for dynamic chX_ce_count attribute */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300247static ssize_t channel_ce_count_show(struct device *dev,
248 struct device_attribute *mattr, char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700249{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300250 struct csrow_info *csrow = to_csrow(dev);
251 unsigned chan = to_channel(mattr);
252 struct rank_info *rank = &csrow->channels[chan];
253
254 return sprintf(data, "%u\n", rank->ce_count);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700255}
256
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300257/* cwrow<id>/attribute files */
258DEVICE_ATTR_LEGACY(size_mb, S_IRUGO, csrow_size_show, NULL);
259DEVICE_ATTR_LEGACY(dev_type, S_IRUGO, csrow_dev_type_show, NULL);
260DEVICE_ATTR_LEGACY(mem_type, S_IRUGO, csrow_mem_type_show, NULL);
261DEVICE_ATTR_LEGACY(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL);
262DEVICE_ATTR_LEGACY(ue_count, S_IRUGO, csrow_ue_count_show, NULL);
263DEVICE_ATTR_LEGACY(ce_count, S_IRUGO, csrow_ce_count_show, NULL);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700264
265/* default attributes of the CSROW<id> object */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300266static struct attribute *csrow_attrs[] = {
267 &dev_attr_legacy_dev_type.attr,
268 &dev_attr_legacy_mem_type.attr,
269 &dev_attr_legacy_edac_mode.attr,
270 &dev_attr_legacy_size_mb.attr,
271 &dev_attr_legacy_ue_count.attr,
272 &dev_attr_legacy_ce_count.attr,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700273 NULL,
274};
275
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300276static struct attribute_group csrow_attr_grp = {
277 .attrs = csrow_attrs,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700278};
279
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300280static const struct attribute_group *csrow_attr_groups[] = {
281 &csrow_attr_grp,
282 NULL
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700283};
284
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300285static void csrow_attr_release(struct device *device)
286{
287 debugf1("Releasing csrow device %s\n", dev_name(device));
288}
289
290static struct device_type csrow_attr_type = {
291 .groups = csrow_attr_groups,
292 .release = csrow_attr_release,
293};
294
295/*
296 * possible dynamic channel DIMM Label attribute files
297 *
298 */
299
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700300#define EDAC_NR_CHANNELS 6
301
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300302DEVICE_CHANNEL(ch0_dimm_label, S_IRUGO | S_IWUSR,
303 channel_dimm_label_show, channel_dimm_label_store, 0);
304DEVICE_CHANNEL(ch1_dimm_label, S_IRUGO | S_IWUSR,
305 channel_dimm_label_show, channel_dimm_label_store, 1);
306DEVICE_CHANNEL(ch2_dimm_label, S_IRUGO | S_IWUSR,
307 channel_dimm_label_show, channel_dimm_label_store, 2);
308DEVICE_CHANNEL(ch3_dimm_label, S_IRUGO | S_IWUSR,
309 channel_dimm_label_show, channel_dimm_label_store, 3);
310DEVICE_CHANNEL(ch4_dimm_label, S_IRUGO | S_IWUSR,
311 channel_dimm_label_show, channel_dimm_label_store, 4);
312DEVICE_CHANNEL(ch5_dimm_label, S_IRUGO | S_IWUSR,
313 channel_dimm_label_show, channel_dimm_label_store, 5);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700314
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300315/* Total possible dynamic DIMM Label attribute file table */
316static struct device_attribute *dynamic_csrow_dimm_attr[] = {
317 &dev_attr_legacy_ch0_dimm_label.attr,
318 &dev_attr_legacy_ch1_dimm_label.attr,
319 &dev_attr_legacy_ch2_dimm_label.attr,
320 &dev_attr_legacy_ch3_dimm_label.attr,
321 &dev_attr_legacy_ch4_dimm_label.attr,
322 &dev_attr_legacy_ch5_dimm_label.attr
323};
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700324
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300325/* possible dynamic channel ce_count attribute files */
326DEVICE_CHANNEL(ch0_ce_count, S_IRUGO | S_IWUSR,
327 channel_ce_count_show, NULL, 0);
328DEVICE_CHANNEL(ch1_ce_count, S_IRUGO | S_IWUSR,
329 channel_ce_count_show, NULL, 1);
330DEVICE_CHANNEL(ch2_ce_count, S_IRUGO | S_IWUSR,
331 channel_ce_count_show, NULL, 2);
332DEVICE_CHANNEL(ch3_ce_count, S_IRUGO | S_IWUSR,
333 channel_ce_count_show, NULL, 3);
334DEVICE_CHANNEL(ch4_ce_count, S_IRUGO | S_IWUSR,
335 channel_ce_count_show, NULL, 4);
336DEVICE_CHANNEL(ch5_ce_count, S_IRUGO | S_IWUSR,
337 channel_ce_count_show, NULL, 5);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700338
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300339/* Total possible dynamic ce_count attribute file table */
340static struct device_attribute *dynamic_csrow_ce_count_attr[] = {
341 &dev_attr_legacy_ch0_ce_count.attr,
342 &dev_attr_legacy_ch1_ce_count.attr,
343 &dev_attr_legacy_ch2_ce_count.attr,
344 &dev_attr_legacy_ch3_ce_count.attr,
345 &dev_attr_legacy_ch4_ce_count.attr,
346 &dev_attr_legacy_ch5_ce_count.attr
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700347};
348
349/* Create a CSROW object under specifed edac_mc_device */
Doug Thompson8096cfa2007-07-19 01:50:27 -0700350static int edac_create_csrow_object(struct mem_ctl_info *mci,
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300351 struct csrow_info *csrow, int index)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700352{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300353 int err, chan;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700354
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300355 if (csrow->nr_channels >= EDAC_NR_CHANNELS)
356 return -ENODEV;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700357
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300358 csrow->dev.type = &csrow_attr_type;
359 csrow->dev.bus = &mci->bus;
360 device_initialize(&csrow->dev);
361 csrow->dev.parent = &mci->dev;
362 dev_set_name(&csrow->dev, "csrow%d", index);
363 dev_set_drvdata(&csrow->dev, csrow);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700364
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300365 debugf0("%s(): creating (virtual) csrow node %s\n", __func__,
366 dev_name(&csrow->dev));
Doug Thompson8096cfa2007-07-19 01:50:27 -0700367
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300368 err = device_add(&csrow->dev);
369 if (err < 0)
370 return err;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700371
Doug Thompson8096cfa2007-07-19 01:50:27 -0700372 for (chan = 0; chan < csrow->nr_channels; chan++) {
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300373 err = device_create_file(&csrow->dev,
374 dynamic_csrow_dimm_attr[chan]);
375 if (err < 0)
376 goto error;
377 err = device_create_file(&csrow->dev,
378 dynamic_csrow_ce_count_attr[chan]);
379 if (err < 0) {
380 device_remove_file(&csrow->dev,
381 dynamic_csrow_dimm_attr[chan]);
382 goto error;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700383 }
384 }
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300385
Doug Thompson8096cfa2007-07-19 01:50:27 -0700386 return 0;
387
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300388error:
389 for (--chan; chan >= 0; chan--) {
390 device_remove_file(&csrow->dev,
391 dynamic_csrow_dimm_attr[chan]);
392 device_remove_file(&csrow->dev,
393 dynamic_csrow_ce_count_attr[chan]);
394 }
395 put_device(&csrow->dev);
Doug Thompson8096cfa2007-07-19 01:50:27 -0700396
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700397 return err;
398}
399
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300400/* Create a CSROW object under specifed edac_mc_device */
401static int edac_create_csrow_objects(struct mem_ctl_info *mci)
402{
403 int err, i, chan;
404 struct csrow_info *csrow;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700405
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300406 for (i = 0; i < mci->nr_csrows; i++) {
407 err = edac_create_csrow_object(mci, &mci->csrows[i], i);
408 if (err < 0)
409 goto error;
410 }
411 return 0;
412
413error:
414 for (--i; i >= 0; i--) {
415 csrow = &mci->csrows[i];
416 for (chan = csrow->nr_channels - 1; chan >= 0; chan--) {
417 device_remove_file(&csrow->dev,
418 dynamic_csrow_dimm_attr[chan]);
419 device_remove_file(&csrow->dev,
420 dynamic_csrow_ce_count_attr[chan]);
421 }
422 put_device(&mci->csrows[i].dev);
423 }
424
425 return err;
426}
427
428static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
429{
430 int i, chan;
431 struct csrow_info *csrow;
432
433 for (i = mci->nr_csrows - 1; i >= 0; i--) {
434 csrow = &mci->csrows[i];
435 for (chan = csrow->nr_channels - 1; chan >= 0; chan--) {
436 debugf1("Removing csrow %d channel %d sysfs nodes\n",
437 i, chan);
438 device_remove_file(&csrow->dev,
439 dynamic_csrow_dimm_attr[chan]);
440 device_remove_file(&csrow->dev,
441 dynamic_csrow_ce_count_attr[chan]);
442 }
443 put_device(&mci->csrows[i].dev);
444 device_del(&mci->csrows[i].dev);
445 }
446}
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300447#endif
448
449/*
450 * Per-dimm (or per-rank) devices
451 */
452
453#define to_dimm(k) container_of(k, struct dimm_info, dev)
454
455/* show/store functions for DIMM Label attributes */
456static ssize_t dimmdev_location_show(struct device *dev,
457 struct device_attribute *mattr, char *data)
458{
459 struct dimm_info *dimm = to_dimm(dev);
460 struct mem_ctl_info *mci = dimm->mci;
461 int i;
462 char *p = data;
463
464 for (i = 0; i < mci->n_layers; i++) {
465 p += sprintf(p, "%s %d ",
466 edac_layer_name[mci->layers[i].type],
467 dimm->location[i]);
468 }
469
470 return p - data;
471}
472
473static ssize_t dimmdev_label_show(struct device *dev,
474 struct device_attribute *mattr, char *data)
475{
476 struct dimm_info *dimm = to_dimm(dev);
477
478 /* if field has not been initialized, there is nothing to send */
479 if (!dimm->label[0])
480 return 0;
481
482 return snprintf(data, EDAC_MC_LABEL_LEN, "%s\n", dimm->label);
483}
484
485static ssize_t dimmdev_label_store(struct device *dev,
486 struct device_attribute *mattr,
487 const char *data,
488 size_t count)
489{
490 struct dimm_info *dimm = to_dimm(dev);
491
492 ssize_t max_size = 0;
493
494 max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
495 strncpy(dimm->label, data, max_size);
496 dimm->label[max_size] = '\0';
497
498 return max_size;
499}
500
501static ssize_t dimmdev_size_show(struct device *dev,
502 struct device_attribute *mattr, char *data)
503{
504 struct dimm_info *dimm = to_dimm(dev);
505
506 return sprintf(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
507}
508
509static ssize_t dimmdev_mem_type_show(struct device *dev,
510 struct device_attribute *mattr, char *data)
511{
512 struct dimm_info *dimm = to_dimm(dev);
513
514 return sprintf(data, "%s\n", mem_types[dimm->mtype]);
515}
516
517static ssize_t dimmdev_dev_type_show(struct device *dev,
518 struct device_attribute *mattr, char *data)
519{
520 struct dimm_info *dimm = to_dimm(dev);
521
522 return sprintf(data, "%s\n", dev_types[dimm->dtype]);
523}
524
525static ssize_t dimmdev_edac_mode_show(struct device *dev,
526 struct device_attribute *mattr,
527 char *data)
528{
529 struct dimm_info *dimm = to_dimm(dev);
530
531 return sprintf(data, "%s\n", edac_caps[dimm->edac_mode]);
532}
533
534/* dimm/rank attribute files */
535static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
536 dimmdev_label_show, dimmdev_label_store);
537static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
538static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
539static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
540static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
541static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
542
543/* attributes of the dimm<id>/rank<id> object */
544static struct attribute *dimm_attrs[] = {
545 &dev_attr_dimm_label.attr,
546 &dev_attr_dimm_location.attr,
547 &dev_attr_size.attr,
548 &dev_attr_dimm_mem_type.attr,
549 &dev_attr_dimm_dev_type.attr,
550 &dev_attr_dimm_edac_mode.attr,
551 NULL,
552};
553
554static struct attribute_group dimm_attr_grp = {
555 .attrs = dimm_attrs,
556};
557
558static const struct attribute_group *dimm_attr_groups[] = {
559 &dimm_attr_grp,
560 NULL
561};
562
563static void dimm_attr_release(struct device *device)
564{
565 debugf1("Releasing dimm device %s\n", dev_name(device));
566}
567
568static struct device_type dimm_attr_type = {
569 .groups = dimm_attr_groups,
570 .release = dimm_attr_release,
571};
572
573/* Create a DIMM object under specifed memory controller device */
574static int edac_create_dimm_object(struct mem_ctl_info *mci,
575 struct dimm_info *dimm,
576 int index)
577{
578 int err;
579 dimm->mci = mci;
580
581 dimm->dev.type = &dimm_attr_type;
582 dimm->dev.bus = &mci->bus;
583 device_initialize(&dimm->dev);
584
585 dimm->dev.parent = &mci->dev;
586 if (mci->mem_is_per_rank)
587 dev_set_name(&dimm->dev, "rank%d", index);
588 else
589 dev_set_name(&dimm->dev, "dimm%d", index);
590 dev_set_drvdata(&dimm->dev, dimm);
591 pm_runtime_forbid(&mci->dev);
592
593 err = device_add(&dimm->dev);
594
595 debugf0("%s(): creating rank/dimm device %s\n", __func__,
596 dev_name(&dimm->dev));
597
598 return err;
599}
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300600
601/*
602 * Memory controller device
603 */
604
605#define to_mci(k) container_of(k, struct mem_ctl_info, dev)
606
607static ssize_t mci_reset_counters_store(struct device *dev,
608 struct device_attribute *mattr,
Douglas Thompson079708b2007-07-19 01:49:58 -0700609 const char *data, size_t count)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700610{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300611 struct mem_ctl_info *mci = to_mci(dev);
612 int cnt, row, chan, i;
Mauro Carvalho Chehab5926ff52012-02-09 11:05:20 -0300613 mci->ue_mc = 0;
614 mci->ce_mc = 0;
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300615 mci->ue_noinfo_count = 0;
616 mci->ce_noinfo_count = 0;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700617
618 for (row = 0; row < mci->nr_csrows; row++) {
619 struct csrow_info *ri = &mci->csrows[row];
620
621 ri->ue_count = 0;
622 ri->ce_count = 0;
623
624 for (chan = 0; chan < ri->nr_channels; chan++)
625 ri->channels[chan].ce_count = 0;
626 }
627
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300628 cnt = 1;
629 for (i = 0; i < mci->n_layers; i++) {
630 cnt *= mci->layers[i].size;
631 memset(mci->ce_per_layer[i], 0, cnt * sizeof(u32));
632 memset(mci->ue_per_layer[i], 0, cnt * sizeof(u32));
633 }
634
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700635 mci->start_time = jiffies;
636 return count;
637}
638
Borislav Petkov39094442010-11-24 19:52:09 +0100639/* Memory scrubbing interface:
640 *
641 * A MC driver can limit the scrubbing bandwidth based on the CPU type.
642 * Therefore, ->set_sdram_scrub_rate should be made to return the actual
643 * bandwidth that is accepted or 0 when scrubbing is to be disabled.
644 *
645 * Negative value still means that an error has occurred while setting
646 * the scrub rate.
647 */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300648static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
649 struct device_attribute *mattr,
Borislav Petkoveba042a2010-05-25 18:21:07 +0200650 const char *data, size_t count)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700651{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300652 struct mem_ctl_info *mci = to_mci(dev);
Borislav Petkoveba042a2010-05-25 18:21:07 +0200653 unsigned long bandwidth = 0;
Borislav Petkov39094442010-11-24 19:52:09 +0100654 int new_bw = 0;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700655
Borislav Petkov39094442010-11-24 19:52:09 +0100656 if (!mci->set_sdram_scrub_rate)
Borislav Petkov5e8e19b2011-09-21 14:10:43 +0200657 return -ENODEV;
Borislav Petkoveba042a2010-05-25 18:21:07 +0200658
659 if (strict_strtoul(data, 10, &bandwidth) < 0)
660 return -EINVAL;
661
Borislav Petkov39094442010-11-24 19:52:09 +0100662 new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
Markus Trippelsdorf49496032011-04-20 14:28:45 -0400663 if (new_bw < 0) {
664 edac_printk(KERN_WARNING, EDAC_MC,
665 "Error setting scrub rate to: %lu\n", bandwidth);
666 return -EINVAL;
Borislav Petkoveba042a2010-05-25 18:21:07 +0200667 }
Borislav Petkov39094442010-11-24 19:52:09 +0100668
Markus Trippelsdorf49496032011-04-20 14:28:45 -0400669 return count;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700670}
671
Borislav Petkov39094442010-11-24 19:52:09 +0100672/*
673 * ->get_sdram_scrub_rate() return value semantics same as above.
674 */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300675static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
676 struct device_attribute *mattr,
677 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700678{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300679 struct mem_ctl_info *mci = to_mci(dev);
Borislav Petkov39094442010-11-24 19:52:09 +0100680 int bandwidth = 0;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700681
Borislav Petkov39094442010-11-24 19:52:09 +0100682 if (!mci->get_sdram_scrub_rate)
Borislav Petkov5e8e19b2011-09-21 14:10:43 +0200683 return -ENODEV;
Borislav Petkov39094442010-11-24 19:52:09 +0100684
685 bandwidth = mci->get_sdram_scrub_rate(mci);
686 if (bandwidth < 0) {
687 edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
688 return bandwidth;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700689 }
Borislav Petkoveba042a2010-05-25 18:21:07 +0200690
Borislav Petkov39094442010-11-24 19:52:09 +0100691 return sprintf(data, "%d\n", bandwidth);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700692}
693
694/* default attribute files for the MCI object */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300695static ssize_t mci_ue_count_show(struct device *dev,
696 struct device_attribute *mattr,
697 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700698{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300699 struct mem_ctl_info *mci = to_mci(dev);
700
Mauro Carvalho Chehab5926ff52012-02-09 11:05:20 -0300701 return sprintf(data, "%d\n", mci->ue_mc);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700702}
703
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300704static ssize_t mci_ce_count_show(struct device *dev,
705 struct device_attribute *mattr,
706 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700707{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300708 struct mem_ctl_info *mci = to_mci(dev);
709
Mauro Carvalho Chehab5926ff52012-02-09 11:05:20 -0300710 return sprintf(data, "%d\n", mci->ce_mc);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700711}
712
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300713static ssize_t mci_ce_noinfo_show(struct device *dev,
714 struct device_attribute *mattr,
715 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700716{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300717 struct mem_ctl_info *mci = to_mci(dev);
718
Douglas Thompson079708b2007-07-19 01:49:58 -0700719 return sprintf(data, "%d\n", mci->ce_noinfo_count);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700720}
721
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300722static ssize_t mci_ue_noinfo_show(struct device *dev,
723 struct device_attribute *mattr,
724 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700725{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300726 struct mem_ctl_info *mci = to_mci(dev);
727
Douglas Thompson079708b2007-07-19 01:49:58 -0700728 return sprintf(data, "%d\n", mci->ue_noinfo_count);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700729}
730
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300731static ssize_t mci_seconds_show(struct device *dev,
732 struct device_attribute *mattr,
733 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700734{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300735 struct mem_ctl_info *mci = to_mci(dev);
736
Douglas Thompson079708b2007-07-19 01:49:58 -0700737 return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700738}
739
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300740static ssize_t mci_ctl_name_show(struct device *dev,
741 struct device_attribute *mattr,
742 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700743{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300744 struct mem_ctl_info *mci = to_mci(dev);
745
Douglas Thompson079708b2007-07-19 01:49:58 -0700746 return sprintf(data, "%s\n", mci->ctl_name);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700747}
748
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300749static ssize_t mci_size_mb_show(struct device *dev,
750 struct device_attribute *mattr,
751 char *data)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700752{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300753 struct mem_ctl_info *mci = to_mci(dev);
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300754 int total_pages = 0, csrow_idx, j;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700755
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300756 for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700757 struct csrow_info *csrow = &mci->csrows[csrow_idx];
758
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300759 for (j = 0; j < csrow->nr_channels; j++) {
760 struct dimm_info *dimm = csrow->channels[j].dimm;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700761
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300762 total_pages += dimm->nr_pages;
763 }
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700764 }
765
Douglas Thompson079708b2007-07-19 01:49:58 -0700766 return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700767}
768
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700769/* default Control file */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300770DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700771
772/* default Attribute files */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300773DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
774DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
775DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
776DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
777DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
778DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
779DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700780
781/* memory scrubber attribute file */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300782DEVICE_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700783 mci_sdram_scrub_rate_store);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700784
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300785static struct attribute *mci_attrs[] = {
786 &dev_attr_reset_counters.attr,
787 &dev_attr_mc_name.attr,
788 &dev_attr_size_mb.attr,
789 &dev_attr_seconds_since_reset.attr,
790 &dev_attr_ue_noinfo_count.attr,
791 &dev_attr_ce_noinfo_count.attr,
792 &dev_attr_ue_count.attr,
793 &dev_attr_ce_count.attr,
794 &dev_attr_sdram_scrub_rate.attr,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700795 NULL
796};
797
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300798static struct attribute_group mci_attr_grp = {
799 .attrs = mci_attrs,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700800};
801
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300802static const struct attribute_group *mci_attr_groups[] = {
803 &mci_attr_grp,
804 NULL
Mauro Carvalho Chehabcc301b32009-09-24 16:23:42 -0300805};
806
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300807static void mci_attr_release(struct device *device)
808{
809 debugf1("Releasing mci device %s\n", dev_name(device));
810}
811
812static struct device_type mci_attr_type = {
813 .groups = mci_attr_groups,
814 .release = mci_attr_release,
Mauro Carvalho Chehabcc301b32009-09-24 16:23:42 -0300815};
816
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700817/*
818 * Create a new Memory Controller kobject instance,
819 * mc<id> under the 'mc' directory
820 *
821 * Return:
822 * 0 Success
823 * !0 Failure
824 */
825int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
826{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300827 int i, err;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700828
829 debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700830
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300831 /* get the /sys/devices/system/edac subsys reference */
Mauro Carvalho Chehabb9687592009-09-25 13:42:25 -0300832
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300833 mci->dev.type = &mci_attr_type;
834 device_initialize(&mci->dev);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700835
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300836 mci->dev.parent = &mci_pdev;
837 mci->dev.bus = &mci->bus;
838 dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
839 dev_set_drvdata(&mci->dev, mci);
840 pm_runtime_forbid(&mci->dev);
841
842 /*
843 * The memory controller needs its own bus, in order to avoid
844 * namespace conflicts at /sys/bus/edac.
Douglas Thompson42a8e392007-07-19 01:50:10 -0700845 */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300846 debugf0("creating bus %s\n",mci->bus.name);
847 mci->bus.name = kstrdup(dev_name(&mci->dev), GFP_KERNEL);
848 err = bus_register(&mci->bus);
849 if (err < 0)
850 return err;
851
852 debugf0("%s(): creating device %s\n", __func__,
853 dev_name(&mci->dev));
854 err = device_add(&mci->dev);
855 if (err < 0) {
856 bus_unregister(&mci->bus);
857 kfree(mci->bus.name);
858 return err;
Douglas Thompson42a8e392007-07-19 01:50:10 -0700859 }
860
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300861 /*
862 * Create the dimm/rank devices
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700863 */
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300864 for (i = 0; i < mci->tot_dimms; i++) {
865 struct dimm_info *dimm = &mci->dimms[i];
866 /* Only expose populated DIMMs */
867 if (dimm->nr_pages == 0)
868 continue;
869#ifdef CONFIG_EDAC_DEBUG
870 debugf1("%s creating dimm%d, located at ",
871 __func__, i);
872 if (edac_debug_level >= 1) {
873 int lay;
874 for (lay = 0; lay < mci->n_layers; lay++)
875 printk(KERN_CONT "%s %d ",
876 edac_layer_name[mci->layers[lay].type],
877 dimm->location[lay]);
878 printk(KERN_CONT "\n");
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700879 }
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300880#endif
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300881 err = edac_create_dimm_object(mci, dimm, i);
882 if (err) {
883 debugf1("%s() failure: create dimm %d obj\n",
884 __func__, i);
885 goto fail;
886 }
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700887 }
888
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300889#ifdef CONFIG_EDAC_LEGACY_SYSFS
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300890 err = edac_create_csrow_objects(mci);
891 if (err < 0)
892 goto fail;
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300893#endif
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300894
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700895 return 0;
896
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300897fail:
Douglas Thompson079708b2007-07-19 01:49:58 -0700898 for (i--; i >= 0; i--) {
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300899 struct dimm_info *dimm = &mci->dimms[i];
900 if (dimm->nr_pages == 0)
901 continue;
902 put_device(&dimm->dev);
903 device_del(&dimm->dev);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700904 }
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300905 put_device(&mci->dev);
906 device_del(&mci->dev);
907 bus_unregister(&mci->bus);
908 kfree(mci->bus.name);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700909 return err;
910}
911
912/*
913 * remove a Memory Controller instance
914 */
915void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
916{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300917 int i;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700918
919 debugf0("%s()\n", __func__);
920
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300921#ifdef CONFIG_EDAC_LEGACY_SYSFS
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300922 edac_delete_csrow_objects(mci);
Mauro Carvalho Chehab19974712012-03-21 17:06:53 -0300923#endif
Mauro Carvalho Chehaba895bf82012-01-28 09:09:38 -0300924
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300925 for (i = 0; i < mci->tot_dimms; i++) {
926 struct dimm_info *dimm = &mci->dimms[i];
927 if (dimm->nr_pages == 0)
928 continue;
929 debugf0("%s(): removing device %s\n", __func__,
930 dev_name(&dimm->dev));
931 put_device(&dimm->dev);
932 device_del(&dimm->dev);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700933 }
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700934}
Doug Thompson8096cfa2007-07-19 01:50:27 -0700935
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300936void edac_unregister_sysfs(struct mem_ctl_info *mci)
Doug Thompson8096cfa2007-07-19 01:50:27 -0700937{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300938 debugf1("Unregistering device %s\n", dev_name(&mci->dev));
939 put_device(&mci->dev);
940 device_del(&mci->dev);
941 bus_unregister(&mci->bus);
942 kfree(mci->bus.name);
943}
Doug Thompson8096cfa2007-07-19 01:50:27 -0700944
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300945static void mc_attr_release(struct device *device)
946{
947 debugf1("Releasing device %s\n", dev_name(device));
948}
949
950static struct device_type mc_attr_type = {
951 .release = mc_attr_release,
952};
953/*
954 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
955 */
956int __init edac_mc_sysfs_init(void)
957{
958 struct bus_type *edac_subsys;
959 int err;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700960
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800961 /* get the /sys/devices/system/edac subsys reference */
962 edac_subsys = edac_get_sysfs_subsys();
963 if (edac_subsys == NULL) {
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300964 debugf1("%s() no edac_subsys\n", __func__);
965 return -EINVAL;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700966 }
967
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300968 mci_pdev.bus = edac_subsys;
969 mci_pdev.type = &mc_attr_type;
970 device_initialize(&mci_pdev);
971 dev_set_name(&mci_pdev, "mc");
Doug Thompson8096cfa2007-07-19 01:50:27 -0700972
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300973 err = device_add(&mci_pdev);
974 if (err < 0)
975 return err;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700976
977 return 0;
Doug Thompson8096cfa2007-07-19 01:50:27 -0700978}
979
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300980void __exit edac_mc_sysfs_exit(void)
Doug Thompson8096cfa2007-07-19 01:50:27 -0700981{
Mauro Carvalho Chehab7a623c02012-04-16 16:41:11 -0300982 put_device(&mci_pdev);
983 device_del(&mci_pdev);
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800984 edac_put_sysfs_subsys();
Doug Thompson8096cfa2007-07-19 01:50:27 -0700985}