blob: dc6e905ee1a5402126496bd06838302a1b26165f [file] [log] [blame]
Douglas Thompson20bcb7a2007-07-19 01:49:47 -07001/*
Douglas Thompson7c9281d2007-07-19 01:49:33 -07002 * (C) 2005, 2006 Linux Networx (http://lnxi.com)
3 * This file may be distributed under the terms of the
4 * GNU General Public License.
5 *
6 * Written Doug Thompson <norsk5@xmission.com>
7 *
8 */
9#include <linux/module.h>
Borislav Petkov30e1f7a2010-09-02 17:26:48 +020010#include <linux/edac.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Douglas Thompson7c9281d2007-07-19 01:49:33 -070012#include <linux/ctype.h>
13
Douglas Thompson20bcb7a2007-07-19 01:49:47 -070014#include "edac_core.h"
Douglas Thompson7c9281d2007-07-19 01:49:33 -070015#include "edac_module.h"
16
Doug Thompsond4c14652007-07-26 10:41:15 -070017/* Turn off this whole feature if PCI is not configured */
Douglas Thompson7c9281d2007-07-19 01:49:33 -070018#ifdef CONFIG_PCI
Dave Jiang91b99042007-07-19 01:49:52 -070019
20#define EDAC_PCI_SYMLINK "device"
21
Doug Thompsond4c14652007-07-26 10:41:15 -070022/* data variables exported via sysfs */
23static int check_pci_errors; /* default NO check PCI parity */
24static int edac_pci_panic_on_pe; /* default NO panic on PCI Parity */
25static int edac_pci_log_pe = 1; /* log PCI parity errors */
Dave Jiang4de78c62007-07-19 01:49:54 -070026static int edac_pci_log_npe = 1; /* log PCI non-parity error errors */
Doug Thompsond4c14652007-07-26 10:41:15 -070027static int edac_pci_poll_msec = 1000; /* one second workq period */
28
Douglas Thompson7c9281d2007-07-19 01:49:33 -070029static atomic_t pci_parity_count = ATOMIC_INIT(0);
Dave Jiang91b99042007-07-19 01:49:52 -070030static atomic_t pci_nonparity_count = ATOMIC_INIT(0);
Douglas Thompson7c9281d2007-07-19 01:49:33 -070031
Arthur Jones14cc5712008-07-25 01:49:08 -070032static struct kobject *edac_pci_top_main_kobj;
Dave Jiang91b99042007-07-19 01:49:52 -070033static atomic_t edac_pci_sysfs_refcount = ATOMIC_INIT(0);
Douglas Thompson7c9281d2007-07-19 01:49:33 -070034
Doug Thompsond4c14652007-07-26 10:41:15 -070035/* getter functions for the data variables */
Dave Jiang4de78c62007-07-19 01:49:54 -070036int edac_pci_get_check_errors(void)
37{
38 return check_pci_errors;
39}
40
Adrian Bunk1a450272008-04-29 01:03:18 -070041static int edac_pci_get_log_pe(void)
Dave Jiang4de78c62007-07-19 01:49:54 -070042{
43 return edac_pci_log_pe;
44}
45
Adrian Bunk1a450272008-04-29 01:03:18 -070046static int edac_pci_get_log_npe(void)
Dave Jiang4de78c62007-07-19 01:49:54 -070047{
48 return edac_pci_log_npe;
49}
50
Adrian Bunk1a450272008-04-29 01:03:18 -070051static int edac_pci_get_panic_on_pe(void)
Dave Jiang4de78c62007-07-19 01:49:54 -070052{
53 return edac_pci_panic_on_pe;
54}
55
56int edac_pci_get_poll_msec(void)
57{
58 return edac_pci_poll_msec;
59}
60
Dave Jiang91b99042007-07-19 01:49:52 -070061/**************************** EDAC PCI sysfs instance *******************/
62static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
63{
Douglas Thompson079708b2007-07-19 01:49:58 -070064 return sprintf(data, "%u\n", atomic_read(&pci->counters.pe_count));
Dave Jiang91b99042007-07-19 01:49:52 -070065}
66
67static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
Douglas Thompson052dfb42007-07-19 01:50:13 -070068 char *data)
Dave Jiang91b99042007-07-19 01:49:52 -070069{
Douglas Thompson079708b2007-07-19 01:49:58 -070070 return sprintf(data, "%u\n", atomic_read(&pci->counters.npe_count));
Dave Jiang91b99042007-07-19 01:49:52 -070071}
72
73#define to_instance(k) container_of(k, struct edac_pci_ctl_info, kobj)
74#define to_instance_attr(a) container_of(a, struct instance_attribute, attr)
75
76/* DEVICE instance kobject release() function */
77static void edac_pci_instance_release(struct kobject *kobj)
78{
79 struct edac_pci_ctl_info *pci;
80
Joe Perches956b9ba2012-04-29 17:08:39 -030081 edac_dbg(0, "\n");
Dave Jiang91b99042007-07-19 01:49:52 -070082
Doug Thompsond4c14652007-07-26 10:41:15 -070083 /* Form pointer to containing struct, the pci control struct */
Dave Jiang91b99042007-07-19 01:49:52 -070084 pci = to_instance(kobj);
Doug Thompsond4c14652007-07-26 10:41:15 -070085
86 /* decrement reference count on top main kobj */
Arthur Jones14cc5712008-07-25 01:49:08 -070087 kobject_put(edac_pci_top_main_kobj);
Doug Thompsond4c14652007-07-26 10:41:15 -070088
89 kfree(pci); /* Free the control struct */
Dave Jiang91b99042007-07-19 01:49:52 -070090}
91
92/* instance specific attribute structure */
93struct instance_attribute {
Douglas Thompson079708b2007-07-19 01:49:58 -070094 struct attribute attr;
Doug Thompsond4c14652007-07-26 10:41:15 -070095 ssize_t(*show) (struct edac_pci_ctl_info *, char *);
96 ssize_t(*store) (struct edac_pci_ctl_info *, const char *, size_t);
Dave Jiang91b99042007-07-19 01:49:52 -070097};
98
99/* Function to 'show' fields from the edac_pci 'instance' structure */
100static ssize_t edac_pci_instance_show(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700101 struct attribute *attr, char *buffer)
Dave Jiang91b99042007-07-19 01:49:52 -0700102{
Douglas Thompson079708b2007-07-19 01:49:58 -0700103 struct edac_pci_ctl_info *pci = to_instance(kobj);
104 struct instance_attribute *instance_attr = to_instance_attr(attr);
Dave Jiang91b99042007-07-19 01:49:52 -0700105
Douglas Thompson079708b2007-07-19 01:49:58 -0700106 if (instance_attr->show)
107 return instance_attr->show(pci, buffer);
108 return -EIO;
Dave Jiang91b99042007-07-19 01:49:52 -0700109}
110
Dave Jiang91b99042007-07-19 01:49:52 -0700111/* Function to 'store' fields into the edac_pci 'instance' structure */
112static ssize_t edac_pci_instance_store(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700113 struct attribute *attr,
114 const char *buffer, size_t count)
Dave Jiang91b99042007-07-19 01:49:52 -0700115{
Douglas Thompson079708b2007-07-19 01:49:58 -0700116 struct edac_pci_ctl_info *pci = to_instance(kobj);
117 struct instance_attribute *instance_attr = to_instance_attr(attr);
Dave Jiang91b99042007-07-19 01:49:52 -0700118
Douglas Thompson079708b2007-07-19 01:49:58 -0700119 if (instance_attr->store)
120 return instance_attr->store(pci, buffer, count);
121 return -EIO;
Dave Jiang91b99042007-07-19 01:49:52 -0700122}
123
Doug Thompsond4c14652007-07-26 10:41:15 -0700124/* fs_ops table */
Emese Revfy52cf25d2010-01-19 02:58:23 +0100125static const struct sysfs_ops pci_instance_ops = {
Dave Jiang91b99042007-07-19 01:49:52 -0700126 .show = edac_pci_instance_show,
127 .store = edac_pci_instance_store
128};
129
130#define INSTANCE_ATTR(_name, _mode, _show, _store) \
131static struct instance_attribute attr_instance_##_name = { \
132 .attr = {.name = __stringify(_name), .mode = _mode }, \
133 .show = _show, \
134 .store = _store, \
135};
136
137INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
138INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
139
140/* pci instance attributes */
141static struct instance_attribute *pci_instance_attr[] = {
142 &attr_instance_pe_count,
143 &attr_instance_npe_count,
144 NULL
145};
146
Doug Thompsond4c14652007-07-26 10:41:15 -0700147/* the ktype for a pci instance */
Dave Jiang91b99042007-07-19 01:49:52 -0700148static struct kobj_type ktype_pci_instance = {
149 .release = edac_pci_instance_release,
150 .sysfs_ops = &pci_instance_ops,
151 .default_attrs = (struct attribute **)pci_instance_attr,
152};
153
Doug Thompsond4c14652007-07-26 10:41:15 -0700154/*
155 * edac_pci_create_instance_kobj
156 *
157 * construct one EDAC PCI instance's kobject for use
158 */
Dave Jiang91b99042007-07-19 01:49:52 -0700159static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
160{
Doug Thompsond4c14652007-07-26 10:41:15 -0700161 struct kobject *main_kobj;
Dave Jiang91b99042007-07-19 01:49:52 -0700162 int err;
163
Joe Perches956b9ba2012-04-29 17:08:39 -0300164 edac_dbg(0, "\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700165
Doug Thompsond4c14652007-07-26 10:41:15 -0700166 /* First bump the ref count on the top main kobj, which will
167 * track the number of PCI instances we have, and thus nest
168 * properly on keeping the module loaded
169 */
Arthur Jones14cc5712008-07-25 01:49:08 -0700170 main_kobj = kobject_get(edac_pci_top_main_kobj);
Doug Thompsond4c14652007-07-26 10:41:15 -0700171 if (!main_kobj) {
172 err = -ENODEV;
173 goto error_out;
174 }
175
176 /* And now register this new kobject under the main kobj */
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400177 err = kobject_init_and_add(&pci->kobj, &ktype_pci_instance,
Arthur Jones14cc5712008-07-25 01:49:08 -0700178 edac_pci_top_main_kobj, "pci%d", idx);
Dave Jiang91b99042007-07-19 01:49:52 -0700179 if (err != 0) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300180 edac_dbg(2, "failed to register instance pci%d\n", idx);
Arthur Jones14cc5712008-07-25 01:49:08 -0700181 kobject_put(edac_pci_top_main_kobj);
Doug Thompsond4c14652007-07-26 10:41:15 -0700182 goto error_out;
Dave Jiang91b99042007-07-19 01:49:52 -0700183 }
184
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400185 kobject_uevent(&pci->kobj, KOBJ_ADD);
Joe Perches956b9ba2012-04-29 17:08:39 -0300186 edac_dbg(1, "Register instance 'pci%d' kobject\n", idx);
Dave Jiang91b99042007-07-19 01:49:52 -0700187
188 return 0;
Doug Thompsond4c14652007-07-26 10:41:15 -0700189
190 /* Error unwind statck */
191error_out:
192 return err;
Dave Jiang91b99042007-07-19 01:49:52 -0700193}
194
Doug Thompsond4c14652007-07-26 10:41:15 -0700195/*
196 * edac_pci_unregister_sysfs_instance_kobj
197 *
198 * unregister the kobj for the EDAC PCI instance
199 */
Adrian Bunk1a450272008-04-29 01:03:18 -0700200static void edac_pci_unregister_sysfs_instance_kobj(
201 struct edac_pci_ctl_info *pci)
Dave Jiang91b99042007-07-19 01:49:52 -0700202{
Joe Perches956b9ba2012-04-29 17:08:39 -0300203 edac_dbg(0, "\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700204
205 /* Unregister the instance kobject and allow its release
206 * function release the main reference count and then
207 * kfree the memory
208 */
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800209 kobject_put(&pci->kobj);
Dave Jiang91b99042007-07-19 01:49:52 -0700210}
211
212/***************************** EDAC PCI sysfs root **********************/
213#define to_edacpci(k) container_of(k, struct edac_pci_ctl_info, kobj)
214#define to_edacpci_attr(a) container_of(a, struct edac_pci_attr, attr)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700215
Doug Thompsond4c14652007-07-26 10:41:15 -0700216/* simple show/store functions for attributes */
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700217static ssize_t edac_pci_int_show(void *ptr, char *buffer)
218{
219 int *value = ptr;
Douglas Thompson079708b2007-07-19 01:49:58 -0700220 return sprintf(buffer, "%d\n", *value);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700221}
222
223static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
224{
225 int *value = ptr;
226
227 if (isdigit(*buffer))
Douglas Thompson079708b2007-07-19 01:49:58 -0700228 *value = simple_strtoul(buffer, NULL, 0);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700229
230 return count;
231}
232
233struct edac_pci_dev_attribute {
234 struct attribute attr;
235 void *value;
Douglas Thompson079708b2007-07-19 01:49:58 -0700236 ssize_t(*show) (void *, char *);
237 ssize_t(*store) (void *, const char *, size_t);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700238};
239
240/* Set of show/store abstract level functions for PCI Parity object */
241static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
Douglas Thompson079708b2007-07-19 01:49:58 -0700242 char *buffer)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700243{
244 struct edac_pci_dev_attribute *edac_pci_dev;
Douglas Thompson079708b2007-07-19 01:49:58 -0700245 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700246
247 if (edac_pci_dev->show)
248 return edac_pci_dev->show(edac_pci_dev->value, buffer);
249 return -EIO;
250}
251
252static ssize_t edac_pci_dev_store(struct kobject *kobj,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700253 struct attribute *attr, const char *buffer,
254 size_t count)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700255{
256 struct edac_pci_dev_attribute *edac_pci_dev;
Douglas Thompson079708b2007-07-19 01:49:58 -0700257 edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700258
259 if (edac_pci_dev->show)
260 return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
261 return -EIO;
262}
263
Emese Revfy52cf25d2010-01-19 02:58:23 +0100264static const struct sysfs_ops edac_pci_sysfs_ops = {
Douglas Thompson079708b2007-07-19 01:49:58 -0700265 .show = edac_pci_dev_show,
266 .store = edac_pci_dev_store
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700267};
268
269#define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
270static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
271 .attr = {.name = __stringify(_name), .mode = _mode }, \
272 .value = &_name, \
273 .show = _show, \
274 .store = _store, \
275};
276
277#define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
278static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
279 .attr = {.name = __stringify(_name), .mode = _mode }, \
280 .value = _data, \
281 .show = _show, \
282 .store = _store, \
283};
284
285/* PCI Parity control files */
Douglas Thompson079708b2007-07-19 01:49:58 -0700286EDAC_PCI_ATTR(check_pci_errors, S_IRUGO | S_IWUSR, edac_pci_int_show,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700287 edac_pci_int_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700288EDAC_PCI_ATTR(edac_pci_log_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700289 edac_pci_int_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700290EDAC_PCI_ATTR(edac_pci_log_npe, S_IRUGO | S_IWUSR, edac_pci_int_show,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700291 edac_pci_int_store);
Douglas Thompson079708b2007-07-19 01:49:58 -0700292EDAC_PCI_ATTR(edac_pci_panic_on_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700293 edac_pci_int_store);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700294EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
Dave Jiang91b99042007-07-19 01:49:52 -0700295EDAC_PCI_ATTR(pci_nonparity_count, S_IRUGO, edac_pci_int_show, NULL);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700296
297/* Base Attributes of the memory ECC object */
298static struct edac_pci_dev_attribute *edac_pci_attr[] = {
Dave Jiang91b99042007-07-19 01:49:52 -0700299 &edac_pci_attr_check_pci_errors,
Dave Jiang4de78c62007-07-19 01:49:54 -0700300 &edac_pci_attr_edac_pci_log_pe,
301 &edac_pci_attr_edac_pci_log_npe,
302 &edac_pci_attr_edac_pci_panic_on_pe,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700303 &edac_pci_attr_pci_parity_count,
Dave Jiang91b99042007-07-19 01:49:52 -0700304 &edac_pci_attr_pci_nonparity_count,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700305 NULL,
306};
307
Doug Thompsond4c14652007-07-26 10:41:15 -0700308/*
309 * edac_pci_release_main_kobj
310 *
311 * This release function is called when the reference count to the
312 * passed kobj goes to zero.
313 *
314 * This kobj is the 'main' kobject that EDAC PCI instances
315 * link to, and thus provide for proper nesting counts
316 */
317static void edac_pci_release_main_kobj(struct kobject *kobj)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700318{
Joe Perches956b9ba2012-04-29 17:08:39 -0300319 edac_dbg(0, "here to module_put(THIS_MODULE)\n");
Dave Jiang91b99042007-07-19 01:49:52 -0700320
Arthur Jones14cc5712008-07-25 01:49:08 -0700321 kfree(kobj);
322
Doug Thompsond4c14652007-07-26 10:41:15 -0700323 /* last reference to top EDAC PCI kobject has been removed,
324 * NOW release our ref count on the core module
325 */
326 module_put(THIS_MODULE);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700327}
328
Doug Thompsond4c14652007-07-26 10:41:15 -0700329/* ktype struct for the EDAC PCI main kobj */
330static struct kobj_type ktype_edac_pci_main_kobj = {
331 .release = edac_pci_release_main_kobj,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700332 .sysfs_ops = &edac_pci_sysfs_ops,
Douglas Thompson079708b2007-07-19 01:49:58 -0700333 .default_attrs = (struct attribute **)edac_pci_attr,
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700334};
335
336/**
Doug Thompsond4c14652007-07-26 10:41:15 -0700337 * edac_pci_main_kobj_setup()
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700338 *
339 * setup the sysfs for EDAC PCI attributes
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800340 * assumes edac_subsys has already been initialized
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700341 */
Adrian Bunk1a450272008-04-29 01:03:18 -0700342static int edac_pci_main_kobj_setup(void)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700343{
344 int err;
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800345 struct bus_type *edac_subsys;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700346
Joe Perches956b9ba2012-04-29 17:08:39 -0300347 edac_dbg(0, "\n");
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700348
Doug Thompsond4c14652007-07-26 10:41:15 -0700349 /* check and count if we have already created the main kobject */
350 if (atomic_inc_return(&edac_pci_sysfs_refcount) != 1)
351 return 0;
352
353 /* First time, so create the main kobject and its
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300354 * controls and attributes
Doug Thompsond4c14652007-07-26 10:41:15 -0700355 */
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800356 edac_subsys = edac_get_sysfs_subsys();
357 if (edac_subsys == NULL) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300358 edac_dbg(1, "no edac_subsys\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700359 err = -ENODEV;
360 goto decrement_count_fail;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700361 }
362
Doug Thompsond4c14652007-07-26 10:41:15 -0700363 /* Bump the reference count on this module to ensure the
364 * modules isn't unloaded until we deconstruct the top
365 * level main kobj for EDAC PCI
366 */
367 if (!try_module_get(THIS_MODULE)) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300368 edac_dbg(1, "try_module_get() failed\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700369 err = -ENODEV;
Borislav Petkov30e1f7a2010-09-02 17:26:48 +0200370 goto mod_get_fail;
Doug Thompsond4c14652007-07-26 10:41:15 -0700371 }
Dave Jiang91b99042007-07-19 01:49:52 -0700372
Arthur Jones14cc5712008-07-25 01:49:08 -0700373 edac_pci_top_main_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
374 if (!edac_pci_top_main_kobj) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300375 edac_dbg(1, "Failed to allocate\n");
Arthur Jones14cc5712008-07-25 01:49:08 -0700376 err = -ENOMEM;
377 goto kzalloc_fail;
378 }
379
Dave Jiang91b99042007-07-19 01:49:52 -0700380 /* Instanstiate the pci object */
Arthur Jones14cc5712008-07-25 01:49:08 -0700381 err = kobject_init_and_add(edac_pci_top_main_kobj,
382 &ktype_edac_pci_main_kobj,
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800383 &edac_subsys->dev_root->kobj, "pci");
Dave Jiang91b99042007-07-19 01:49:52 -0700384 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300385 edac_dbg(1, "Failed to register '.../edac/pci'\n");
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400386 goto kobject_init_and_add_fail;
Dave Jiang91b99042007-07-19 01:49:52 -0700387 }
388
Doug Thompsond4c14652007-07-26 10:41:15 -0700389 /* At this point, to 'release' the top level kobject
390 * for EDAC PCI, then edac_pci_main_kobj_teardown()
391 * must be used, for resources to be cleaned up properly
392 */
Arthur Jones14cc5712008-07-25 01:49:08 -0700393 kobject_uevent(edac_pci_top_main_kobj, KOBJ_ADD);
Joe Perches956b9ba2012-04-29 17:08:39 -0300394 edac_dbg(1, "Registered '.../edac/pci' kobject\n");
Dave Jiang91b99042007-07-19 01:49:52 -0700395
396 return 0;
Doug Thompsond4c14652007-07-26 10:41:15 -0700397
398 /* Error unwind statck */
Greg Kroah-Hartmanb2ed2152007-12-17 15:54:39 -0400399kobject_init_and_add_fail:
Arthur Jones14cc5712008-07-25 01:49:08 -0700400 kfree(edac_pci_top_main_kobj);
401
402kzalloc_fail:
Doug Thompsond4c14652007-07-26 10:41:15 -0700403 module_put(THIS_MODULE);
404
Borislav Petkov30e1f7a2010-09-02 17:26:48 +0200405mod_get_fail:
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800406 edac_put_sysfs_subsys();
Borislav Petkov30e1f7a2010-09-02 17:26:48 +0200407
Doug Thompsond4c14652007-07-26 10:41:15 -0700408decrement_count_fail:
409 /* if are on this error exit, nothing to tear down */
410 atomic_dec(&edac_pci_sysfs_refcount);
411
412 return err;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700413}
414
415/*
Doug Thompsond4c14652007-07-26 10:41:15 -0700416 * edac_pci_main_kobj_teardown()
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700417 *
Doug Thompsond4c14652007-07-26 10:41:15 -0700418 * if no longer linked (needed) remove the top level EDAC PCI
419 * kobject with its controls and attributes
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700420 */
Doug Thompsond4c14652007-07-26 10:41:15 -0700421static void edac_pci_main_kobj_teardown(void)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700422{
Joe Perches956b9ba2012-04-29 17:08:39 -0300423 edac_dbg(0, "\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700424
425 /* Decrement the count and only if no more controller instances
426 * are connected perform the unregisteration of the top level
427 * main kobj
428 */
429 if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300430 edac_dbg(0, "called kobject_put on main kobj\n");
Arthur Jones14cc5712008-07-25 01:49:08 -0700431 kobject_put(edac_pci_top_main_kobj);
Doug Thompsond4c14652007-07-26 10:41:15 -0700432 }
Kay Sieversfe5ff8b2011-12-14 15:21:07 -0800433 edac_put_sysfs_subsys();
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700434}
435
Doug Thompsond4c14652007-07-26 10:41:15 -0700436/*
437 *
438 * edac_pci_create_sysfs
439 *
440 * Create the controls/attributes for the specified EDAC PCI device
441 */
Dave Jiang91b99042007-07-19 01:49:52 -0700442int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
443{
444 int err;
445 struct kobject *edac_kobj = &pci->kobj;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700446
Joe Perches956b9ba2012-04-29 17:08:39 -0300447 edac_dbg(0, "idx=%d\n", pci->pci_idx);
Dave Jiang91b99042007-07-19 01:49:52 -0700448
Doug Thompsond4c14652007-07-26 10:41:15 -0700449 /* create the top main EDAC PCI kobject, IF needed */
450 err = edac_pci_main_kobj_setup();
451 if (err)
452 return err;
453
454 /* Create this instance's kobject under the MAIN kobject */
455 err = edac_pci_create_instance_kobj(pci, pci->pci_idx);
456 if (err)
457 goto unregister_cleanup;
458
Douglas Thompson079708b2007-07-19 01:49:58 -0700459 err = sysfs_create_link(edac_kobj, &pci->dev->kobj, EDAC_PCI_SYMLINK);
Dave Jiang91b99042007-07-19 01:49:52 -0700460 if (err) {
Joe Perches956b9ba2012-04-29 17:08:39 -0300461 edac_dbg(0, "sysfs_create_link() returned err= %d\n", err);
Doug Thompsond4c14652007-07-26 10:41:15 -0700462 goto symlink_fail;
Dave Jiang91b99042007-07-19 01:49:52 -0700463 }
464
465 return 0;
Doug Thompsond4c14652007-07-26 10:41:15 -0700466
467 /* Error unwind stack */
468symlink_fail:
469 edac_pci_unregister_sysfs_instance_kobj(pci);
470
471unregister_cleanup:
472 edac_pci_main_kobj_teardown();
473
474 return err;
Dave Jiang91b99042007-07-19 01:49:52 -0700475}
476
Doug Thompsond4c14652007-07-26 10:41:15 -0700477/*
478 * edac_pci_remove_sysfs
479 *
480 * remove the controls and attributes for this EDAC PCI device
481 */
Dave Jiang91b99042007-07-19 01:49:52 -0700482void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
483{
Joe Perches956b9ba2012-04-29 17:08:39 -0300484 edac_dbg(0, "index=%d\n", pci->pci_idx);
Dave Jiang91b99042007-07-19 01:49:52 -0700485
Doug Thompsond4c14652007-07-26 10:41:15 -0700486 /* Remove the symlink */
Dave Jiang91b99042007-07-19 01:49:52 -0700487 sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
488
Doug Thompsond4c14652007-07-26 10:41:15 -0700489 /* remove this PCI instance's sysfs entries */
490 edac_pci_unregister_sysfs_instance_kobj(pci);
491
492 /* Call the main unregister function, which will determine
493 * if this 'pci' is the last instance.
494 * If it is, the main kobject will be unregistered as a result
495 */
Joe Perches956b9ba2012-04-29 17:08:39 -0300496 edac_dbg(0, "calling edac_pci_main_kobj_teardown()\n");
Doug Thompsond4c14652007-07-26 10:41:15 -0700497 edac_pci_main_kobj_teardown();
Dave Jiang91b99042007-07-19 01:49:52 -0700498}
499
500/************************ PCI error handling *************************/
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700501static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
502{
503 int where;
504 u16 status;
505
506 where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
507 pci_read_config_word(dev, where, &status);
508
509 /* If we get back 0xFFFF then we must suspect that the card has been
510 * pulled but the Linux PCI layer has not yet finished cleaning up.
511 * We don't want to report on such devices
512 */
513
514 if (status == 0xFFFF) {
515 u32 sanity;
516
517 pci_read_config_dword(dev, 0, &sanity);
518
519 if (sanity == 0xFFFFFFFF)
520 return 0;
521 }
522
523 status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
Douglas Thompson052dfb42007-07-19 01:50:13 -0700524 PCI_STATUS_PARITY;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700525
526 if (status)
527 /* reset only the bits we are interested in */
528 pci_write_config_word(dev, where, status);
529
530 return status;
531}
532
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700533
534/* Clear any PCI parity errors logged by this device. */
535static void edac_pci_dev_parity_clear(struct pci_dev *dev)
536{
537 u8 header_type;
538
539 get_pci_parity_status(dev, 0);
540
541 /* read the device TYPE, looking for bridges */
542 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
543
544 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
545 get_pci_parity_status(dev, 1);
546}
547
548/*
549 * PCI Parity polling
550 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300551 * Function to retrieve the current parity status
Doug Thompsond4c14652007-07-26 10:41:15 -0700552 * and decode it
553 *
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700554 */
555static void edac_pci_dev_parity_test(struct pci_dev *dev)
556{
Doug Thompsond4c14652007-07-26 10:41:15 -0700557 unsigned long flags;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700558 u16 status;
Douglas Thompson079708b2007-07-19 01:49:58 -0700559 u8 header_type;
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700560
Doug Thompsond4c14652007-07-26 10:41:15 -0700561 /* stop any interrupts until we can acquire the status */
562 local_irq_save(flags);
563
564 /* read the STATUS register on this device */
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700565 status = get_pci_parity_status(dev, 0);
566
Doug Thompsond4c14652007-07-26 10:41:15 -0700567 /* read the device TYPE, looking for bridges */
568 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
569
570 local_irq_restore(flags);
571
Joe Perches956b9ba2012-04-29 17:08:39 -0300572 edac_dbg(4, "PCI STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700573
Bryan Boatright6b09ff92008-02-07 00:14:58 -0800574 /* check the status reg for errors on boards NOT marked as broken
575 * if broken, we cannot trust any of the status bits
576 */
577 if (status && !dev->broken_parity_status) {
Dave Jiang91b99042007-07-19 01:49:52 -0700578 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700579 edac_printk(KERN_CRIT, EDAC_PCI,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700580 "Signaled System Error on %s\n",
581 pci_name(dev));
Dave Jiang91b99042007-07-19 01:49:52 -0700582 atomic_inc(&pci_nonparity_count);
583 }
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700584
585 if (status & (PCI_STATUS_PARITY)) {
586 edac_printk(KERN_CRIT, EDAC_PCI,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700587 "Master Data Parity Error on %s\n",
588 pci_name(dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700589
590 atomic_inc(&pci_parity_count);
591 }
592
593 if (status & (PCI_STATUS_DETECTED_PARITY)) {
594 edac_printk(KERN_CRIT, EDAC_PCI,
Douglas Thompson052dfb42007-07-19 01:50:13 -0700595 "Detected Parity Error on %s\n",
596 pci_name(dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700597
598 atomic_inc(&pci_parity_count);
599 }
600 }
601
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700602
Joe Perches956b9ba2012-04-29 17:08:39 -0300603 edac_dbg(4, "PCI HEADER TYPE= 0x%02x %s\n",
604 header_type, dev_name(&dev->dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700605
606 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
607 /* On bridges, need to examine secondary status register */
608 status = get_pci_parity_status(dev, 1);
609
Joe Perches956b9ba2012-04-29 17:08:39 -0300610 edac_dbg(4, "PCI SEC_STATUS= 0x%04x %s\n",
611 status, dev_name(&dev->dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700612
Bryan Boatright6b09ff92008-02-07 00:14:58 -0800613 /* check the secondary status reg for errors,
614 * on NOT broken boards
615 */
616 if (status && !dev->broken_parity_status) {
Dave Jiang91b99042007-07-19 01:49:52 -0700617 if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700618 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
Douglas Thompson052dfb42007-07-19 01:50:13 -0700619 "Signaled System Error on %s\n",
620 pci_name(dev));
Dave Jiang91b99042007-07-19 01:49:52 -0700621 atomic_inc(&pci_nonparity_count);
622 }
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700623
624 if (status & (PCI_STATUS_PARITY)) {
625 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
Douglas Thompson052dfb42007-07-19 01:50:13 -0700626 "Master Data Parity Error on "
627 "%s\n", pci_name(dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700628
629 atomic_inc(&pci_parity_count);
630 }
631
632 if (status & (PCI_STATUS_DETECTED_PARITY)) {
633 edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
Douglas Thompson052dfb42007-07-19 01:50:13 -0700634 "Detected Parity Error on %s\n",
635 pci_name(dev));
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700636
637 atomic_inc(&pci_parity_count);
638 }
639 }
640 }
641}
642
Doug Thompsond4c14652007-07-26 10:41:15 -0700643/* reduce some complexity in definition of the iterator */
644typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
645
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700646/*
647 * pci_dev parity list iterator
Wei Yongjun3bfe5aa2012-12-04 00:01:42 -0500648 *
649 * Scan the PCI device list looking for SERRORs, Master Parity ERRORS or
650 * Parity ERRORs on primary or secondary devices.
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700651 */
652static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
653{
654 struct pci_dev *dev = NULL;
655
Wei Yongjun3bfe5aa2012-12-04 00:01:42 -0500656 for_each_pci_dev(dev)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700657 fn(dev);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700658}
659
660/*
661 * edac_pci_do_parity_check
662 *
663 * performs the actual PCI parity check operation
664 */
665void edac_pci_do_parity_check(void)
666{
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700667 int before_count;
668
Joe Perches956b9ba2012-04-29 17:08:39 -0300669 edac_dbg(3, "\n");
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700670
Doug Thompsond4c14652007-07-26 10:41:15 -0700671 /* if policy has PCI check off, leave now */
Dave Jiang91b99042007-07-19 01:49:52 -0700672 if (!check_pci_errors)
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700673 return;
674
675 before_count = atomic_read(&pci_parity_count);
676
677 /* scan all PCI devices looking for a Parity Error on devices and
Doug Thompsond4c14652007-07-26 10:41:15 -0700678 * bridges.
679 * The iterator calls pci_get_device() which might sleep, thus
680 * we cannot disable interrupts in this scan.
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700681 */
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700682 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700683
684 /* Only if operator has selected panic on PCI Error */
Dave Jiang4de78c62007-07-19 01:49:54 -0700685 if (edac_pci_get_panic_on_pe()) {
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700686 /* If the count is different 'after' from 'before' */
687 if (before_count != atomic_read(&pci_parity_count))
688 panic("EDAC: PCI Parity Error");
689 }
690}
691
Doug Thompsond4c14652007-07-26 10:41:15 -0700692/*
693 * edac_pci_clear_parity_errors
694 *
695 * function to perform an iteration over the PCI devices
696 * and clearn their current status
697 */
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700698void edac_pci_clear_parity_errors(void)
699{
700 /* Clear any PCI bus parity errors that devices initially have logged
701 * in their registers.
702 */
703 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
704}
Doug Thompsond4c14652007-07-26 10:41:15 -0700705
706/*
707 * edac_pci_handle_pe
708 *
709 * Called to handle a PARITY ERROR event
710 */
Dave Jiang91b99042007-07-19 01:49:52 -0700711void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
712{
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700713
Dave Jiang91b99042007-07-19 01:49:52 -0700714 /* global PE counter incremented by edac_pci_do_parity_check() */
715 atomic_inc(&pci->counters.pe_count);
716
Dave Jiang4de78c62007-07-19 01:49:54 -0700717 if (edac_pci_get_log_pe())
Dave Jiang91b99042007-07-19 01:49:52 -0700718 edac_pci_printk(pci, KERN_WARNING,
719 "Parity Error ctl: %s %d: %s\n",
720 pci->ctl_name, pci->pci_idx, msg);
721
722 /*
723 * poke all PCI devices and see which one is the troublemaker
724 * panic() is called if set
725 */
726 edac_pci_do_parity_check();
727}
728EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
729
Doug Thompsond4c14652007-07-26 10:41:15 -0700730
731/*
732 * edac_pci_handle_npe
733 *
734 * Called to handle a NON-PARITY ERROR event
735 */
Dave Jiang91b99042007-07-19 01:49:52 -0700736void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
737{
738
739 /* global NPE counter incremented by edac_pci_do_parity_check() */
740 atomic_inc(&pci->counters.npe_count);
741
Dave Jiang4de78c62007-07-19 01:49:54 -0700742 if (edac_pci_get_log_npe())
Dave Jiang91b99042007-07-19 01:49:52 -0700743 edac_pci_printk(pci, KERN_WARNING,
744 "Non-Parity Error ctl: %s %d: %s\n",
745 pci->ctl_name, pci->pci_idx, msg);
746
747 /*
748 * poke all PCI devices and see which one is the troublemaker
749 * panic() is called if set
750 */
751 edac_pci_do_parity_check();
752}
753EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700754
755/*
756 * Define the PCI parameter to the module
757 */
Dave Jiang91b99042007-07-19 01:49:52 -0700758module_param(check_pci_errors, int, 0644);
Dave Jiang4de78c62007-07-19 01:49:54 -0700759MODULE_PARM_DESC(check_pci_errors,
Douglas Thompson079708b2007-07-19 01:49:58 -0700760 "Check for PCI bus parity errors: 0=off 1=on");
Dave Jiang4de78c62007-07-19 01:49:54 -0700761module_param(edac_pci_panic_on_pe, int, 0644);
762MODULE_PARM_DESC(edac_pci_panic_on_pe,
Douglas Thompson079708b2007-07-19 01:49:58 -0700763 "Panic on PCI Bus Parity error: 0=off 1=on");
Douglas Thompson7c9281d2007-07-19 01:49:33 -0700764
Douglas Thompson079708b2007-07-19 01:49:58 -0700765#endif /* CONFIG_PCI */