Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * EDAC PCI component |
| 3 | * |
| 4 | * Author: Dave Jiang <djiang@mvista.com> |
| 5 | * |
| 6 | * 2007 (c) MontaVista Software, Inc. This file is licensed under |
| 7 | * the terms of the GNU General Public License version 2. This program |
| 8 | * is licensed "as is" without any warranty of any kind, whether express |
| 9 | * or implied. |
| 10 | * |
| 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/smp.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/sysctl.h> |
| 17 | #include <linux/highmem.h> |
| 18 | #include <linux/timer.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/sysdev.h> |
| 23 | #include <linux/ctype.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/page.h> |
| 27 | |
| 28 | #include "edac_core.h" |
| 29 | #include "edac_module.h" |
| 30 | |
| 31 | static DEFINE_MUTEX(edac_pci_ctls_mutex); |
| 32 | static struct list_head edac_pci_list = LIST_HEAD_INIT(edac_pci_list); |
| 33 | |
| 34 | static inline void edac_lock_pci_list(void) |
| 35 | { |
| 36 | mutex_lock(&edac_pci_ctls_mutex); |
| 37 | } |
| 38 | |
| 39 | static inline void edac_unlock_pci_list(void) |
| 40 | { |
| 41 | mutex_unlock(&edac_pci_ctls_mutex); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * The alloc() and free() functions for the 'edac_pci' control info |
| 46 | * structure. The chip driver will allocate one of these for each |
| 47 | * edac_pci it is going to control/register with the EDAC CORE. |
| 48 | */ |
| 49 | struct edac_pci_ctl_info * edac_pci_alloc_ctl_info( |
| 50 | unsigned int sz_pvt, |
| 51 | const char *edac_pci_name) |
| 52 | { |
| 53 | struct edac_pci_ctl_info *pci; |
| 54 | void *pvt; |
| 55 | unsigned int size; |
| 56 | |
| 57 | pci = (struct edac_pci_ctl_info *)0; |
| 58 | pvt = edac_align_ptr(&pci[1], sz_pvt); |
| 59 | size = ((unsigned long)pvt) + sz_pvt; |
| 60 | |
| 61 | if ((pci = kzalloc(size, GFP_KERNEL)) == NULL) |
| 62 | return NULL; |
| 63 | |
| 64 | pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL; |
| 65 | |
| 66 | pci->pvt_info = pvt; |
| 67 | |
| 68 | pci->op_state = OP_ALLOC; |
| 69 | |
| 70 | snprintf(pci->name, strlen(edac_pci_name)+1, "%s", edac_pci_name); |
| 71 | |
| 72 | return pci; |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info); |
| 75 | |
| 76 | /* |
| 77 | * edac_pci_free_ctl_info() |
| 78 | * frees the memory allocated by edac_pci_alloc_ctl_info() function |
| 79 | */ |
| 80 | void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci) |
| 81 | { |
| 82 | kfree(pci); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info); |
| 85 | |
| 86 | /* |
| 87 | * find_edac_pci_by_dev() |
| 88 | * scans the edac_pci list for a specific 'struct device *' |
| 89 | */ |
| 90 | static struct edac_pci_ctl_info * find_edac_pci_by_dev(struct device *dev) |
| 91 | { |
| 92 | struct edac_pci_ctl_info *pci; |
| 93 | struct list_head *item; |
| 94 | |
| 95 | debugf3("%s()\n", __func__); |
| 96 | |
| 97 | list_for_each(item, &edac_pci_list) { |
| 98 | pci = list_entry(item, struct edac_pci_ctl_info, link); |
| 99 | |
| 100 | if (pci->dev == dev) |
| 101 | return pci; |
| 102 | } |
| 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * add_edac_pci_to_global_list |
| 109 | * Before calling this function, caller must assign a unique value to |
| 110 | * edac_dev->pci_idx. |
| 111 | * Return: |
| 112 | * 0 on success |
| 113 | * 1 on failure |
| 114 | */ |
| 115 | static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci) |
| 116 | { |
| 117 | struct list_head *item, *insert_before; |
| 118 | struct edac_pci_ctl_info *rover; |
| 119 | |
| 120 | insert_before = &edac_pci_list; |
| 121 | |
| 122 | /* Determine if already on the list */ |
| 123 | if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL)) |
| 124 | goto fail0; |
| 125 | |
| 126 | /* Insert in ascending order by 'pci_idx', so find position */ |
| 127 | list_for_each(item, &edac_pci_list) { |
| 128 | rover = list_entry(item, struct edac_pci_ctl_info, link); |
| 129 | |
| 130 | if (rover->pci_idx >= pci->pci_idx) { |
| 131 | if (unlikely(rover->pci_idx == pci->pci_idx)) |
| 132 | goto fail1; |
| 133 | |
| 134 | insert_before = item; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | list_add_tail_rcu(&pci->link, insert_before); |
| 140 | return 0; |
| 141 | |
| 142 | fail0: |
| 143 | edac_printk(KERN_WARNING, EDAC_PCI, |
| 144 | "%s (%s) %s %s already assigned %d\n", |
| 145 | rover->dev->bus_id, dev_name(rover), |
| 146 | rover->mod_name, rover->ctl_name, rover->pci_idx); |
| 147 | return 1; |
| 148 | |
| 149 | fail1: |
| 150 | edac_printk(KERN_WARNING, EDAC_PCI, |
| 151 | "but in low-level driver: attempt to assign\n" |
| 152 | "\tduplicate pci_idx %d in %s()\n", rover->pci_idx, __func__); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * complete_edac_pci_list_del |
| 158 | */ |
| 159 | static void complete_edac_pci_list_del(struct rcu_head *head) |
| 160 | { |
| 161 | struct edac_pci_ctl_info *pci; |
| 162 | |
| 163 | pci = container_of(head, struct edac_pci_ctl_info, rcu); |
| 164 | INIT_LIST_HEAD(&pci->link); |
| 165 | complete(&pci->complete); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * del_edac_pci_from_global_list |
| 170 | */ |
| 171 | static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci) |
| 172 | { |
| 173 | list_del_rcu(&pci->link); |
| 174 | init_completion(&pci->complete); |
| 175 | call_rcu(&pci->rcu, complete_edac_pci_list_del); |
| 176 | wait_for_completion(&pci->complete); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * edac_pci_find() |
| 181 | * Search for an edac_pci_ctl_info structure whose index is 'idx' |
| 182 | * |
| 183 | * If found, return a pointer to the structure |
| 184 | * Else return NULL. |
| 185 | * |
| 186 | * Caller must hold pci_ctls_mutex. |
| 187 | */ |
| 188 | struct edac_pci_ctl_info * edac_pci_find(int idx) |
| 189 | { |
| 190 | struct list_head *item; |
| 191 | struct edac_pci_ctl_info *pci; |
| 192 | |
| 193 | /* Iterage over list, looking for exact match of ID */ |
| 194 | list_for_each(item, &edac_pci_list) { |
| 195 | pci = list_entry(item, struct edac_pci_ctl_info, link); |
| 196 | |
| 197 | if (pci->pci_idx >= idx) { |
| 198 | if (pci->pci_idx == idx) |
| 199 | return pci; |
| 200 | |
| 201 | /* not on list, so terminate early */ |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return NULL; |
| 207 | } |
| 208 | EXPORT_SYMBOL_GPL(edac_pci_find); |
| 209 | |
| 210 | /* |
| 211 | * edac_pci_workq_function() |
| 212 | * performs the operation scheduled by a workq request |
| 213 | */ |
| 214 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)) |
| 215 | static void edac_pci_workq_function(struct work_struct *work_req) |
| 216 | { |
| 217 | struct delayed_work *d_work = (struct delayed_work *)work_req; |
| 218 | struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work); |
| 219 | #else |
| 220 | static void edac_pci_workq_function(void *ptr) |
| 221 | { |
| 222 | struct edac_pci_ctl_info *pci = ptr; |
| 223 | #endif |
| 224 | |
| 225 | edac_lock_pci_list(); |
| 226 | |
| 227 | if ((pci->op_state == OP_RUNNING_POLL) && |
| 228 | (pci->edac_check != NULL) && |
Dave Jiang | 4de78c6 | 2007-07-19 01:49:54 -0700 | [diff] [blame^] | 229 | (edac_pci_get_check_errors())) |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 230 | pci->edac_check(pci); |
| 231 | |
| 232 | edac_unlock_pci_list(); |
| 233 | |
| 234 | /* Reschedule */ |
Dave Jiang | 4de78c6 | 2007-07-19 01:49:54 -0700 | [diff] [blame^] | 235 | queue_delayed_work(edac_workqueue, &pci->work, |
| 236 | msecs_to_jiffies(edac_pci_get_poll_msec())); |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * edac_pci_workq_setup() |
| 241 | * initialize a workq item for this edac_pci instance |
| 242 | * passing in the new delay period in msec |
| 243 | */ |
| 244 | static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci, |
| 245 | unsigned int msec) |
| 246 | { |
| 247 | debugf0("%s()\n", __func__); |
| 248 | |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 249 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)) |
| 250 | INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function); |
| 251 | #else |
| 252 | INIT_WORK(&pci->work, edac_pci_workq_function, pci); |
| 253 | #endif |
Dave Jiang | 4de78c6 | 2007-07-19 01:49:54 -0700 | [diff] [blame^] | 254 | queue_delayed_work(edac_workqueue, &pci->work, |
| 255 | msecs_to_jiffies(edac_pci_get_poll_msec())); |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * edac_pci_workq_teardown() |
| 260 | * stop the workq processing on this edac_pci instance |
| 261 | */ |
| 262 | static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci) |
| 263 | { |
| 264 | int status; |
| 265 | |
| 266 | status = cancel_delayed_work(&pci->work); |
| 267 | if (status == 0) |
| 268 | flush_workqueue(edac_workqueue); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * edac_pci_reset_delay_period |
| 273 | */ |
| 274 | void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci, |
| 275 | unsigned long value) |
| 276 | { |
| 277 | edac_lock_pci_list(); |
| 278 | |
| 279 | edac_pci_workq_teardown(pci); |
| 280 | |
| 281 | edac_pci_workq_setup(pci, value); |
| 282 | |
| 283 | edac_unlock_pci_list(); |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period); |
| 286 | |
| 287 | /* |
| 288 | * edac_pci_add_device: Insert the 'edac_dev' structure into the |
| 289 | * edac_pci global list and create sysfs entries associated with |
| 290 | * edac_pci structure. |
| 291 | * @pci: pointer to the edac_device structure to be added to the list |
| 292 | * @edac_idx: A unique numeric identifier to be assigned to the |
| 293 | * 'edac_pci' structure. |
| 294 | * |
| 295 | * Return: |
| 296 | * 0 Success |
| 297 | * !0 Failure |
| 298 | */ |
| 299 | int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx) |
| 300 | { |
| 301 | debugf0("%s()\n", __func__); |
| 302 | |
| 303 | pci->pci_idx = edac_idx; |
| 304 | |
| 305 | edac_lock_pci_list(); |
| 306 | |
| 307 | if (add_edac_pci_to_global_list(pci)) |
| 308 | goto fail0; |
| 309 | |
| 310 | pci->start_time = jiffies; |
| 311 | |
| 312 | if (edac_pci_create_sysfs(pci)) { |
| 313 | edac_pci_printk(pci, KERN_WARNING, |
| 314 | "failed to create sysfs pci\n"); |
| 315 | goto fail1; |
| 316 | } |
| 317 | |
| 318 | if (pci->edac_check != NULL) { |
| 319 | pci->op_state = OP_RUNNING_POLL; |
| 320 | |
| 321 | edac_pci_workq_setup(pci, 1000); |
| 322 | } else { |
| 323 | pci->op_state = OP_RUNNING_INTERRUPT; |
| 324 | } |
| 325 | |
| 326 | edac_pci_printk(pci, KERN_INFO, |
| 327 | "Giving out device to module '%s' controller '%s':" |
| 328 | " DEV '%s' (%s)\n", |
| 329 | pci->mod_name, |
| 330 | pci->ctl_name, |
| 331 | dev_name(pci), |
| 332 | edac_op_state_toString(pci->op_state)); |
| 333 | |
| 334 | edac_unlock_pci_list(); |
| 335 | return 0; |
| 336 | |
| 337 | fail1: |
| 338 | del_edac_pci_from_global_list(pci); |
| 339 | fail0: |
| 340 | edac_unlock_pci_list(); |
| 341 | return 1; |
| 342 | } |
| 343 | EXPORT_SYMBOL_GPL(edac_pci_add_device); |
| 344 | |
| 345 | /* |
| 346 | * edac_pci_del_device() |
| 347 | * Remove sysfs entries for specified edac_pci structure and |
| 348 | * then remove edac_pci structure from global list |
| 349 | * |
| 350 | * @dev: |
| 351 | * Pointer to 'struct device' representing edac_pci structure |
| 352 | * to remove |
| 353 | * |
| 354 | * Return: |
| 355 | * Pointer to removed edac_pci structure, |
| 356 | * or NULL if device not found |
| 357 | */ |
| 358 | struct edac_pci_ctl_info * edac_pci_del_device(struct device *dev) |
| 359 | { |
| 360 | struct edac_pci_ctl_info *pci; |
| 361 | |
| 362 | debugf0("%s()\n", __func__); |
| 363 | |
| 364 | edac_lock_pci_list(); |
| 365 | |
| 366 | if ((pci = find_edac_pci_by_dev(dev)) == NULL) { |
| 367 | edac_unlock_pci_list(); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | pci->op_state = OP_OFFLINE; |
| 372 | |
| 373 | edac_pci_workq_teardown(pci); |
| 374 | |
| 375 | edac_pci_remove_sysfs(pci); |
| 376 | |
| 377 | del_edac_pci_from_global_list(pci); |
| 378 | |
| 379 | edac_unlock_pci_list(); |
| 380 | |
| 381 | edac_printk(KERN_INFO, EDAC_PCI, |
| 382 | "Removed device %d for %s %s: DEV %s\n", |
| 383 | pci->pci_idx, |
| 384 | pci->mod_name, |
| 385 | pci->ctl_name, |
| 386 | dev_name(pci)); |
| 387 | |
| 388 | return pci; |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(edac_pci_del_device); |
| 391 | |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 392 | void edac_pci_generic_check(struct edac_pci_ctl_info *pci) |
| 393 | { |
| 394 | edac_pci_do_parity_check(); |
| 395 | } |
| 396 | |
| 397 | static int edac_pci_idx = 0; |
| 398 | #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller" |
| 399 | |
| 400 | struct edac_pci_gen_data { |
| 401 | int edac_idx; |
| 402 | }; |
| 403 | |
| 404 | struct edac_pci_ctl_info * |
| 405 | edac_pci_create_generic_ctl(struct device *dev, const char *mod_name) |
| 406 | { |
| 407 | struct edac_pci_ctl_info *pci; |
| 408 | struct edac_pci_gen_data *pdata; |
| 409 | |
| 410 | pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME); |
| 411 | if (!pci) |
| 412 | return NULL; |
| 413 | |
| 414 | pdata = pci->pvt_info; |
| 415 | pci->dev = dev; |
| 416 | dev_set_drvdata(pci->dev, pci); |
| 417 | pci->dev_name = pci_name(to_pci_dev(dev)); |
| 418 | |
| 419 | pci->mod_name = mod_name; |
| 420 | pci->ctl_name = EDAC_PCI_GENCTL_NAME; |
| 421 | pci->edac_check = edac_pci_generic_check; |
| 422 | |
| 423 | pdata->edac_idx = edac_pci_idx++; |
| 424 | |
| 425 | if (edac_pci_add_device(pci, pdata->edac_idx) > 0) { |
| 426 | debugf3("%s(): failed edac_pci_add_device()\n", __func__); |
| 427 | edac_pci_free_ctl_info(pci); |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | return pci; |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl); |
| 434 | |
| 435 | void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci) |
| 436 | { |
| 437 | edac_pci_del_device(pci->dev); |
| 438 | edac_pci_free_ctl_info(pci); |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl); |