]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/edac/edac_pci.c
drivers/edac: cleanup spaces-gotos after Lindent messup
[mirror_ubuntu-bionic-kernel.git] / drivers / edac / edac_pci.c
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(unsigned int sz_pvt,
50 const char *edac_pci_name)
51 {
52 struct edac_pci_ctl_info *pci;
53 void *pvt;
54 unsigned int size;
55
56 pci = (struct edac_pci_ctl_info *)0;
57 pvt = edac_align_ptr(&pci[1], sz_pvt);
58 size = ((unsigned long)pvt) + sz_pvt;
59
60 if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
61 return NULL;
62
63 pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
64
65 pci->pvt_info = pvt;
66
67 pci->op_state = OP_ALLOC;
68
69 snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
70
71 return pci;
72 }
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
85 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
86
87 /*
88 * find_edac_pci_by_dev()
89 * scans the edac_pci list for a specific 'struct device *'
90 */
91 static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
92 {
93 struct edac_pci_ctl_info *pci;
94 struct list_head *item;
95
96 debugf3("%s()\n", __func__);
97
98 list_for_each(item, &edac_pci_list) {
99 pci = list_entry(item, struct edac_pci_ctl_info, link);
100
101 if (pci->dev == dev)
102 return pci;
103 }
104
105 return NULL;
106 }
107
108 /*
109 * add_edac_pci_to_global_list
110 * Before calling this function, caller must assign a unique value to
111 * edac_dev->pci_idx.
112 * Return:
113 * 0 on success
114 * 1 on failure
115 */
116 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
117 {
118 struct list_head *item, *insert_before;
119 struct edac_pci_ctl_info *rover;
120
121 insert_before = &edac_pci_list;
122
123 /* Determine if already on the list */
124 if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
125 goto fail0;
126
127 /* Insert in ascending order by 'pci_idx', so find position */
128 list_for_each(item, &edac_pci_list) {
129 rover = list_entry(item, struct edac_pci_ctl_info, link);
130
131 if (rover->pci_idx >= pci->pci_idx) {
132 if (unlikely(rover->pci_idx == pci->pci_idx))
133 goto fail1;
134
135 insert_before = item;
136 break;
137 }
138 }
139
140 list_add_tail_rcu(&pci->link, insert_before);
141 return 0;
142
143 fail0:
144 edac_printk(KERN_WARNING, EDAC_PCI,
145 "%s (%s) %s %s already assigned %d\n",
146 rover->dev->bus_id, dev_name(rover),
147 rover->mod_name, rover->ctl_name, rover->pci_idx);
148 return 1;
149
150 fail1:
151 edac_printk(KERN_WARNING, EDAC_PCI,
152 "but in low-level driver: attempt to assign\n"
153 "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
154 __func__);
155 return 1;
156 }
157
158 /*
159 * complete_edac_pci_list_del
160 */
161 static void complete_edac_pci_list_del(struct rcu_head *head)
162 {
163 struct edac_pci_ctl_info *pci;
164
165 pci = container_of(head, struct edac_pci_ctl_info, rcu);
166 INIT_LIST_HEAD(&pci->link);
167 complete(&pci->complete);
168 }
169
170 /*
171 * del_edac_pci_from_global_list
172 */
173 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
174 {
175 list_del_rcu(&pci->link);
176 init_completion(&pci->complete);
177 call_rcu(&pci->rcu, complete_edac_pci_list_del);
178 wait_for_completion(&pci->complete);
179 }
180
181 /*
182 * edac_pci_find()
183 * Search for an edac_pci_ctl_info structure whose index is 'idx'
184 *
185 * If found, return a pointer to the structure
186 * Else return NULL.
187 *
188 * Caller must hold pci_ctls_mutex.
189 */
190 struct edac_pci_ctl_info *edac_pci_find(int idx)
191 {
192 struct list_head *item;
193 struct edac_pci_ctl_info *pci;
194
195 /* Iterage over list, looking for exact match of ID */
196 list_for_each(item, &edac_pci_list) {
197 pci = list_entry(item, struct edac_pci_ctl_info, link);
198
199 if (pci->pci_idx >= idx) {
200 if (pci->pci_idx == idx)
201 return pci;
202
203 /* not on list, so terminate early */
204 break;
205 }
206 }
207
208 return NULL;
209 }
210
211 EXPORT_SYMBOL_GPL(edac_pci_find);
212
213 /*
214 * edac_pci_workq_function()
215 * performs the operation scheduled by a workq request
216 */
217 static void edac_pci_workq_function(struct work_struct *work_req)
218 {
219 struct delayed_work *d_work = (struct delayed_work *)work_req;
220 struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
221
222 edac_lock_pci_list();
223
224 if ((pci->op_state == OP_RUNNING_POLL) &&
225 (pci->edac_check != NULL) && (edac_pci_get_check_errors()))
226 pci->edac_check(pci);
227
228 edac_unlock_pci_list();
229
230 /* Reschedule */
231 queue_delayed_work(edac_workqueue, &pci->work,
232 msecs_to_jiffies(edac_pci_get_poll_msec()));
233 }
234
235 /*
236 * edac_pci_workq_setup()
237 * initialize a workq item for this edac_pci instance
238 * passing in the new delay period in msec
239 */
240 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
241 unsigned int msec)
242 {
243 debugf0("%s()\n", __func__);
244
245 INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
246 queue_delayed_work(edac_workqueue, &pci->work,
247 msecs_to_jiffies(edac_pci_get_poll_msec()));
248 }
249
250 /*
251 * edac_pci_workq_teardown()
252 * stop the workq processing on this edac_pci instance
253 */
254 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
255 {
256 int status;
257
258 status = cancel_delayed_work(&pci->work);
259 if (status == 0)
260 flush_workqueue(edac_workqueue);
261 }
262
263 /*
264 * edac_pci_reset_delay_period
265 */
266 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
267 unsigned long value)
268 {
269 edac_lock_pci_list();
270
271 edac_pci_workq_teardown(pci);
272
273 edac_pci_workq_setup(pci, value);
274
275 edac_unlock_pci_list();
276 }
277
278 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
279
280 /*
281 * edac_pci_add_device: Insert the 'edac_dev' structure into the
282 * edac_pci global list and create sysfs entries associated with
283 * edac_pci structure.
284 * @pci: pointer to the edac_device structure to be added to the list
285 * @edac_idx: A unique numeric identifier to be assigned to the
286 * 'edac_pci' structure.
287 *
288 * Return:
289 * 0 Success
290 * !0 Failure
291 */
292 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
293 {
294 debugf0("%s()\n", __func__);
295
296 pci->pci_idx = edac_idx;
297
298 edac_lock_pci_list();
299
300 if (add_edac_pci_to_global_list(pci))
301 goto fail0;
302
303 pci->start_time = jiffies;
304
305 if (edac_pci_create_sysfs(pci)) {
306 edac_pci_printk(pci, KERN_WARNING,
307 "failed to create sysfs pci\n");
308 goto fail1;
309 }
310
311 if (pci->edac_check != NULL) {
312 pci->op_state = OP_RUNNING_POLL;
313
314 edac_pci_workq_setup(pci, 1000);
315 } else {
316 pci->op_state = OP_RUNNING_INTERRUPT;
317 }
318
319 edac_pci_printk(pci, KERN_INFO,
320 "Giving out device to module '%s' controller '%s':"
321 " DEV '%s' (%s)\n",
322 pci->mod_name,
323 pci->ctl_name,
324 dev_name(pci), edac_op_state_toString(pci->op_state));
325
326 edac_unlock_pci_list();
327 return 0;
328
329 fail1:
330 del_edac_pci_from_global_list(pci);
331 fail0:
332 edac_unlock_pci_list();
333 return 1;
334 }
335
336 EXPORT_SYMBOL_GPL(edac_pci_add_device);
337
338 /*
339 * edac_pci_del_device()
340 * Remove sysfs entries for specified edac_pci structure and
341 * then remove edac_pci structure from global list
342 *
343 * @dev:
344 * Pointer to 'struct device' representing edac_pci structure
345 * to remove
346 *
347 * Return:
348 * Pointer to removed edac_pci structure,
349 * or NULL if device not found
350 */
351 struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
352 {
353 struct edac_pci_ctl_info *pci;
354
355 debugf0("%s()\n", __func__);
356
357 edac_lock_pci_list();
358
359 if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
360 edac_unlock_pci_list();
361 return NULL;
362 }
363
364 pci->op_state = OP_OFFLINE;
365
366 edac_pci_workq_teardown(pci);
367
368 edac_pci_remove_sysfs(pci);
369
370 del_edac_pci_from_global_list(pci);
371
372 edac_unlock_pci_list();
373
374 edac_printk(KERN_INFO, EDAC_PCI,
375 "Removed device %d for %s %s: DEV %s\n",
376 pci->pci_idx, pci->mod_name, pci->ctl_name, dev_name(pci));
377
378 return pci;
379 }
380
381 EXPORT_SYMBOL_GPL(edac_pci_del_device);
382
383 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
384 {
385 edac_pci_do_parity_check();
386 }
387
388 static int edac_pci_idx = 0;
389 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
390
391 struct edac_pci_gen_data {
392 int edac_idx;
393 };
394
395 struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
396 const char *mod_name)
397 {
398 struct edac_pci_ctl_info *pci;
399 struct edac_pci_gen_data *pdata;
400
401 pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
402 if (!pci)
403 return NULL;
404
405 pdata = pci->pvt_info;
406 pci->dev = dev;
407 dev_set_drvdata(pci->dev, pci);
408 pci->dev_name = pci_name(to_pci_dev(dev));
409
410 pci->mod_name = mod_name;
411 pci->ctl_name = EDAC_PCI_GENCTL_NAME;
412 pci->edac_check = edac_pci_generic_check;
413
414 pdata->edac_idx = edac_pci_idx++;
415
416 if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
417 debugf3("%s(): failed edac_pci_add_device()\n", __func__);
418 edac_pci_free_ctl_info(pci);
419 return NULL;
420 }
421
422 return pci;
423 }
424
425 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
426
427 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
428 {
429 edac_pci_del_device(pci->dev);
430 edac_pci_free_ctl_info(pci);
431 }
432
433 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);