1 /* edac_mc kernel module
2 * (C) 2005, 2006 Linux Networx (http://lnxi.com)
3 * This file may be distributed under the terms of the
4 * GNU General Public License.
6 * Written Doug Thompson <norsk5@xmission.com>
9 #include <linux/module.h>
10 #include <linux/sysdev.h>
11 #include <linux/ctype.h>
14 #include "edac_module.h"
18 static int check_pci_parity
= 0; /* default YES check PCI parity */
19 static int panic_on_pci_parity
; /* default no panic on PCI Parity */
20 static atomic_t pci_parity_count
= ATOMIC_INIT(0);
22 static struct kobject edac_pci_kobj
; /* /sys/devices/system/edac/pci */
23 static struct completion edac_pci_kobj_complete
;
26 static ssize_t
edac_pci_int_show(void *ptr
, char *buffer
)
29 return sprintf(buffer
,"%d\n",*value
);
32 static ssize_t
edac_pci_int_store(void *ptr
, const char *buffer
, size_t count
)
37 *value
= simple_strtoul(buffer
,NULL
,0);
42 struct edac_pci_dev_attribute
{
43 struct attribute attr
;
45 ssize_t (*show
)(void *,char *);
46 ssize_t (*store
)(void *, const char *,size_t);
49 /* Set of show/store abstract level functions for PCI Parity object */
50 static ssize_t
edac_pci_dev_show(struct kobject
*kobj
, struct attribute
*attr
,
53 struct edac_pci_dev_attribute
*edac_pci_dev
;
54 edac_pci_dev
= (struct edac_pci_dev_attribute
*)attr
;
56 if (edac_pci_dev
->show
)
57 return edac_pci_dev
->show(edac_pci_dev
->value
, buffer
);
61 static ssize_t
edac_pci_dev_store(struct kobject
*kobj
,
62 struct attribute
*attr
, const char *buffer
, size_t count
)
64 struct edac_pci_dev_attribute
*edac_pci_dev
;
65 edac_pci_dev
= (struct edac_pci_dev_attribute
*)attr
;
67 if (edac_pci_dev
->show
)
68 return edac_pci_dev
->store(edac_pci_dev
->value
, buffer
, count
);
72 static struct sysfs_ops edac_pci_sysfs_ops
= {
73 .show
= edac_pci_dev_show
,
74 .store
= edac_pci_dev_store
77 #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
78 static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
79 .attr = {.name = __stringify(_name), .mode = _mode }, \
85 #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
86 static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
87 .attr = {.name = __stringify(_name), .mode = _mode }, \
93 /* PCI Parity control files */
94 EDAC_PCI_ATTR(check_pci_parity
, S_IRUGO
|S_IWUSR
, edac_pci_int_show
,
96 EDAC_PCI_ATTR(panic_on_pci_parity
, S_IRUGO
|S_IWUSR
, edac_pci_int_show
,
98 EDAC_PCI_ATTR(pci_parity_count
, S_IRUGO
, edac_pci_int_show
, NULL
);
100 /* Base Attributes of the memory ECC object */
101 static struct edac_pci_dev_attribute
*edac_pci_attr
[] = {
102 &edac_pci_attr_check_pci_parity
,
103 &edac_pci_attr_panic_on_pci_parity
,
104 &edac_pci_attr_pci_parity_count
,
108 /* No memory to release */
109 static void edac_pci_release(struct kobject
*kobj
)
111 debugf1("%s()\n", __func__
);
112 complete(&edac_pci_kobj_complete
);
115 static struct kobj_type ktype_edac_pci
= {
116 .release
= edac_pci_release
,
117 .sysfs_ops
= &edac_pci_sysfs_ops
,
118 .default_attrs
= (struct attribute
**) edac_pci_attr
,
122 * edac_sysfs_pci_setup()
124 * setup the sysfs for EDAC PCI attributes
125 * assumes edac_class has already been initialized
127 int edac_sysfs_pci_setup(void)
130 struct sysdev_class
*edac_class
;
132 debugf1("%s()\n", __func__
);
134 edac_class
= edac_get_edac_class();
136 memset(&edac_pci_kobj
, 0, sizeof(edac_pci_kobj
));
137 edac_pci_kobj
.parent
= &edac_class
->kset
.kobj
;
138 edac_pci_kobj
.ktype
= &ktype_edac_pci
;
139 err
= kobject_set_name(&edac_pci_kobj
, "pci");
142 /* Instanstiate the pci object */
143 /* FIXME: maybe new sysdev_create_subdir() */
144 err
= kobject_register(&edac_pci_kobj
);
147 debugf1("Failed to register '.../edac/pci'\n");
149 debugf1("Registered '.../edac/pci' kobject\n");
156 * edac_sysfs_pci_teardown
158 * perform the sysfs teardown for the PCI attributes
160 void edac_sysfs_pci_teardown(void)
162 debugf0("%s()\n", __func__
);
163 init_completion(&edac_pci_kobj_complete
);
164 kobject_unregister(&edac_pci_kobj
);
165 wait_for_completion(&edac_pci_kobj_complete
);
169 static u16
get_pci_parity_status(struct pci_dev
*dev
, int secondary
)
174 where
= secondary
? PCI_SEC_STATUS
: PCI_STATUS
;
175 pci_read_config_word(dev
, where
, &status
);
177 /* If we get back 0xFFFF then we must suspect that the card has been
178 * pulled but the Linux PCI layer has not yet finished cleaning up.
179 * We don't want to report on such devices
182 if (status
== 0xFFFF) {
185 pci_read_config_dword(dev
, 0, &sanity
);
187 if (sanity
== 0xFFFFFFFF)
191 status
&= PCI_STATUS_DETECTED_PARITY
| PCI_STATUS_SIG_SYSTEM_ERROR
|
195 /* reset only the bits we are interested in */
196 pci_write_config_word(dev
, where
, status
);
201 typedef void (*pci_parity_check_fn_t
) (struct pci_dev
*dev
);
203 /* Clear any PCI parity errors logged by this device. */
204 static void edac_pci_dev_parity_clear(struct pci_dev
*dev
)
208 get_pci_parity_status(dev
, 0);
210 /* read the device TYPE, looking for bridges */
211 pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &header_type
);
213 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
)
214 get_pci_parity_status(dev
, 1);
221 static void edac_pci_dev_parity_test(struct pci_dev
*dev
)
226 /* read the STATUS register on this device
228 status
= get_pci_parity_status(dev
, 0);
230 debugf2("PCI STATUS= 0x%04x %s\n", status
, dev
->dev
.bus_id
);
232 /* check the status reg for errors */
234 if (status
& (PCI_STATUS_SIG_SYSTEM_ERROR
))
235 edac_printk(KERN_CRIT
, EDAC_PCI
,
236 "Signaled System Error on %s\n",
239 if (status
& (PCI_STATUS_PARITY
)) {
240 edac_printk(KERN_CRIT
, EDAC_PCI
,
241 "Master Data Parity Error on %s\n",
244 atomic_inc(&pci_parity_count
);
247 if (status
& (PCI_STATUS_DETECTED_PARITY
)) {
248 edac_printk(KERN_CRIT
, EDAC_PCI
,
249 "Detected Parity Error on %s\n",
252 atomic_inc(&pci_parity_count
);
256 /* read the device TYPE, looking for bridges */
257 pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &header_type
);
259 debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type
, dev
->dev
.bus_id
);
261 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
262 /* On bridges, need to examine secondary status register */
263 status
= get_pci_parity_status(dev
, 1);
265 debugf2("PCI SEC_STATUS= 0x%04x %s\n",
266 status
, dev
->dev
.bus_id
);
268 /* check the secondary status reg for errors */
270 if (status
& (PCI_STATUS_SIG_SYSTEM_ERROR
))
271 edac_printk(KERN_CRIT
, EDAC_PCI
, "Bridge "
272 "Signaled System Error on %s\n",
275 if (status
& (PCI_STATUS_PARITY
)) {
276 edac_printk(KERN_CRIT
, EDAC_PCI
, "Bridge "
277 "Master Data Parity Error on "
278 "%s\n", pci_name(dev
));
280 atomic_inc(&pci_parity_count
);
283 if (status
& (PCI_STATUS_DETECTED_PARITY
)) {
284 edac_printk(KERN_CRIT
, EDAC_PCI
, "Bridge "
285 "Detected Parity Error on %s\n",
288 atomic_inc(&pci_parity_count
);
295 * pci_dev parity list iterator
296 * Scan the PCI device list for one iteration, looking for SERRORs
297 * Master Parity ERRORS or Parity ERRORs on primary or secondary devices
299 static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn
)
301 struct pci_dev
*dev
= NULL
;
303 /* request for kernel access to the next PCI device, if any,
304 * and while we are looking at it have its reference count
305 * bumped until we are done with it
307 while((dev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, dev
)) != NULL
) {
313 * edac_pci_do_parity_check
315 * performs the actual PCI parity check operation
317 void edac_pci_do_parity_check(void)
322 debugf3("%s()\n", __func__
);
324 if (!check_pci_parity
)
327 before_count
= atomic_read(&pci_parity_count
);
329 /* scan all PCI devices looking for a Parity Error on devices and
332 local_irq_save(flags
);
333 edac_pci_dev_parity_iterator(edac_pci_dev_parity_test
);
334 local_irq_restore(flags
);
336 /* Only if operator has selected panic on PCI Error */
337 if (panic_on_pci_parity
) {
338 /* If the count is different 'after' from 'before' */
339 if (before_count
!= atomic_read(&pci_parity_count
))
340 panic("EDAC: PCI Parity Error");
344 void edac_pci_clear_parity_errors(void)
346 /* Clear any PCI bus parity errors that devices initially have logged
347 * in their registers.
349 edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear
);
354 * Define the PCI parameter to the module
356 module_param(check_pci_parity
, int, 0644);
357 MODULE_PARM_DESC(check_pci_parity
, "Check for PCI bus parity errors: 0=off 1=on");
358 module_param(panic_on_pci_parity
, int, 0644);
359 MODULE_PARM_DESC(panic_on_pci_parity
, "Panic on PCI Bus Parity error: 0=off 1=on");
361 #endif /* CONFIG_PCI */