blob: 30b8d96ed54dc2a82d3eb0da8de72bbfe9905c78 [file] [log] [blame]
Gavin Shan55037d12012-09-07 22:44:07 +00001/*
2 * The file intends to implement PE based on the information from
3 * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
4 * All the PEs should be organized as hierarchy tree. The first level
5 * of the tree will be associated to existing PHBs since the particular
6 * PE is only meaningful in one PHB domain.
7 *
8 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/export.h>
26#include <linux/gfp.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
29#include <linux/pci.h>
30#include <linux/string.h>
31
32#include <asm/pci-bridge.h>
33#include <asm/ppc-pci.h>
34
35static LIST_HEAD(eeh_phb_pe);
36
37/**
38 * eeh_pe_alloc - Allocate PE
39 * @phb: PCI controller
40 * @type: PE type
41 *
42 * Allocate PE instance dynamically.
43 */
44static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)
45{
46 struct eeh_pe *pe;
47
48 /* Allocate PHB PE */
49 pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL);
50 if (!pe) return NULL;
51
52 /* Initialize PHB PE */
53 pe->type = type;
54 pe->phb = phb;
55 INIT_LIST_HEAD(&pe->child_list);
56 INIT_LIST_HEAD(&pe->child);
57 INIT_LIST_HEAD(&pe->edevs);
58
59 return pe;
60}
61
62/**
63 * eeh_phb_pe_create - Create PHB PE
64 * @phb: PCI controller
65 *
66 * The function should be called while the PHB is detected during
67 * system boot or PCI hotplug in order to create PHB PE.
68 */
69int __devinit eeh_phb_pe_create(struct pci_controller *phb)
70{
71 struct eeh_pe *pe;
72
73 /* Allocate PHB PE */
74 pe = eeh_pe_alloc(phb, EEH_PE_PHB);
75 if (!pe) {
76 pr_err("%s: out of memory!\n", __func__);
77 return -ENOMEM;
78 }
79
80 /* Put it into the list */
81 eeh_lock();
82 list_add_tail(&pe->child, &eeh_phb_pe);
83 eeh_unlock();
84
85 pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number);
86
87 return 0;
88}
89
90/**
91 * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
92 * @phb: PCI controller
93 *
94 * The overall PEs form hierarchy tree. The first layer of the
95 * hierarchy tree is composed of PHB PEs. The function is used
96 * to retrieve the corresponding PHB PE according to the given PHB.
97 */
98static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
99{
100 struct eeh_pe *pe;
101
102 eeh_lock();
103
104 list_for_each_entry(pe, &eeh_phb_pe, child) {
105 /*
106 * Actually, we needn't check the type since
107 * the PE for PHB has been determined when that
108 * was created.
109 */
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000110 if ((pe->type & EEH_PE_PHB) &&
Gavin Shan55037d12012-09-07 22:44:07 +0000111 pe->phb == phb) {
112 eeh_unlock();
113 return pe;
114 }
115 }
116
117 eeh_unlock();
118
119 return NULL;
120}
Gavin Shan22f4ab12012-09-07 22:44:08 +0000121
122/**
123 * eeh_pe_next - Retrieve the next PE in the tree
124 * @pe: current PE
125 * @root: root PE
126 *
127 * The function is used to retrieve the next PE in the
128 * hierarchy PE tree.
129 */
130static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
131 struct eeh_pe *root)
132{
133 struct list_head *next = pe->child_list.next;
134
135 if (next == &pe->child_list) {
136 while (1) {
137 if (pe == root)
138 return NULL;
139 next = pe->child.next;
140 if (next != &pe->parent->child_list)
141 break;
142 pe = pe->parent;
143 }
144 }
145
146 return list_entry(next, struct eeh_pe, child);
147}
148
149/**
150 * eeh_pe_traverse - Traverse PEs in the specified PHB
151 * @root: root PE
152 * @fn: callback
153 * @flag: extra parameter to callback
154 *
155 * The function is used to traverse the specified PE and its
156 * child PEs. The traversing is to be terminated once the
157 * callback returns something other than NULL, or no more PEs
158 * to be traversed.
159 */
160static void *eeh_pe_traverse(struct eeh_pe *root,
161 eeh_traverse_func fn, void *flag)
162{
163 struct eeh_pe *pe;
164 void *ret;
165
166 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
167 ret = fn(pe, flag);
168 if (ret) return ret;
169 }
170
171 return NULL;
172}
173
174/**
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000175 * eeh_pe_dev_traverse - Traverse the devices from the PE
176 * @root: EEH PE
177 * @fn: function callback
178 * @flag: extra parameter to callback
179 *
180 * The function is used to traverse the devices of the specified
181 * PE and its child PEs.
182 */
183void *eeh_pe_dev_traverse(struct eeh_pe *root,
184 eeh_traverse_func fn, void *flag)
185{
186 struct eeh_pe *pe;
187 struct eeh_dev *edev;
188 void *ret;
189
190 if (!root) {
191 pr_warning("%s: Invalid PE %p\n", __func__, root);
192 return NULL;
193 }
194
195 /* Traverse root PE */
196 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
197 eeh_pe_for_each_dev(pe, edev) {
198 ret = fn(edev, flag);
199 if (ret) return ret;
200 }
201 }
202
203 return NULL;
204}
205
206/**
Gavin Shan22f4ab12012-09-07 22:44:08 +0000207 * __eeh_pe_get - Check the PE address
208 * @data: EEH PE
209 * @flag: EEH device
210 *
211 * For one particular PE, it can be identified by PE address
212 * or tranditional BDF address. BDF address is composed of
213 * Bus/Device/Function number. The extra data referred by flag
214 * indicates which type of address should be used.
215 */
216static void *__eeh_pe_get(void *data, void *flag)
217{
218 struct eeh_pe *pe = (struct eeh_pe *)data;
219 struct eeh_dev *edev = (struct eeh_dev *)flag;
220
221 /* Unexpected PHB PE */
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000222 if (pe->type & EEH_PE_PHB)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000223 return NULL;
224
225 /* We prefer PE address */
226 if (edev->pe_config_addr &&
227 (edev->pe_config_addr == pe->addr))
228 return pe;
229
230 /* Try BDF address */
231 if (edev->pe_config_addr &&
232 (edev->config_addr == pe->config_addr))
233 return pe;
234
235 return NULL;
236}
237
238/**
239 * eeh_pe_get - Search PE based on the given address
240 * @edev: EEH device
241 *
242 * Search the corresponding PE based on the specified address which
243 * is included in the eeh device. The function is used to check if
244 * the associated PE has been created against the PE address. It's
245 * notable that the PE address has 2 format: traditional PE address
246 * which is composed of PCI bus/device/function number, or unified
247 * PE address.
248 */
249static struct eeh_pe *eeh_pe_get(struct eeh_dev *edev)
250{
251 struct eeh_pe *root = eeh_phb_pe_get(edev->phb);
252 struct eeh_pe *pe;
253
254 eeh_lock();
255 pe = eeh_pe_traverse(root, __eeh_pe_get, edev);
256 eeh_unlock();
257
258 return pe;
259}
260
261/**
262 * eeh_pe_get_parent - Retrieve the parent PE
263 * @edev: EEH device
264 *
265 * The whole PEs existing in the system are organized as hierarchy
266 * tree. The function is used to retrieve the parent PE according
267 * to the parent EEH device.
268 */
269static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
270{
271 struct device_node *dn;
272 struct eeh_dev *parent;
273
274 /*
275 * It might have the case for the indirect parent
276 * EEH device already having associated PE, but
277 * the direct parent EEH device doesn't have yet.
278 */
279 dn = edev->dn->parent;
280 while (dn) {
281 /* We're poking out of PCI territory */
282 if (!PCI_DN(dn)) return NULL;
283
284 parent = of_node_to_eeh_dev(dn);
285 /* We're poking out of PCI territory */
286 if (!parent) return NULL;
287
288 if (parent->pe)
289 return parent->pe;
290
291 dn = dn->parent;
292 }
293
294 return NULL;
295}
Gavin Shan9b843482012-09-07 22:44:09 +0000296
297/**
298 * eeh_add_to_parent_pe - Add EEH device to parent PE
299 * @edev: EEH device
300 *
301 * Add EEH device to the parent PE. If the parent PE already
302 * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
303 * we have to create new PE to hold the EEH device and the new
304 * PE will be linked to its parent PE as well.
305 */
306int eeh_add_to_parent_pe(struct eeh_dev *edev)
307{
308 struct eeh_pe *pe, *parent;
309
310 /*
311 * Search the PE has been existing or not according
312 * to the PE address. If that has been existing, the
313 * PE should be composed of PCI bus and its subordinate
314 * components.
315 */
316 pe = eeh_pe_get(edev);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000317 if (pe && !(pe->type & EEH_PE_INVALID)) {
Gavin Shan9b843482012-09-07 22:44:09 +0000318 if (!edev->pe_config_addr) {
319 pr_err("%s: PE with addr 0x%x already exists\n",
320 __func__, edev->config_addr);
321 return -EEXIST;
322 }
323
324 /* Mark the PE as type of PCI bus */
325 pe->type = EEH_PE_BUS;
326 edev->pe = pe;
327
328 /* Put the edev to PE */
329 list_add_tail(&edev->list, &pe->edevs);
330 pr_debug("EEH: Add %s to Bus PE#%x\n",
331 edev->dn->full_name, pe->addr);
332
333 return 0;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000334 } else if (pe && (pe->type & EEH_PE_INVALID)) {
335 list_add_tail(&edev->list, &pe->edevs);
336 edev->pe = pe;
337 /*
338 * We're running to here because of PCI hotplug caused by
339 * EEH recovery. We need clear EEH_PE_INVALID until the top.
340 */
341 parent = pe;
342 while (parent) {
343 if (!(parent->type & EEH_PE_INVALID))
344 break;
345 parent->type &= ~EEH_PE_INVALID;
346 parent = parent->parent;
347 }
348 pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
349 edev->dn->full_name, pe->addr, pe->parent->addr);
350
351 return 0;
Gavin Shan9b843482012-09-07 22:44:09 +0000352 }
353
354 /* Create a new EEH PE */
355 pe = eeh_pe_alloc(edev->phb, EEH_PE_DEVICE);
356 if (!pe) {
357 pr_err("%s: out of memory!\n", __func__);
358 return -ENOMEM;
359 }
360 pe->addr = edev->pe_config_addr;
361 pe->config_addr = edev->config_addr;
362
363 /*
364 * Put the new EEH PE into hierarchy tree. If the parent
365 * can't be found, the newly created PE will be attached
366 * to PHB directly. Otherwise, we have to associate the
367 * PE with its parent.
368 */
369 parent = eeh_pe_get_parent(edev);
370 if (!parent) {
371 parent = eeh_phb_pe_get(edev->phb);
372 if (!parent) {
373 pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
374 __func__, edev->phb->global_number);
375 edev->pe = NULL;
376 kfree(pe);
377 return -EEXIST;
378 }
379 }
380 pe->parent = parent;
381
382 /*
383 * Put the newly created PE into the child list and
384 * link the EEH device accordingly.
385 */
386 list_add_tail(&pe->child, &parent->child_list);
387 list_add_tail(&edev->list, &pe->edevs);
388 edev->pe = pe;
389 pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
390 edev->dn->full_name, pe->addr, pe->parent->addr);
391
392 return 0;
393}
Gavin Shan82e88822012-09-07 22:44:10 +0000394
395/**
396 * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
397 * @edev: EEH device
Gavin Shan20ee6a92012-09-11 19:16:17 +0000398 * @purge_pe: remove PE or not
Gavin Shan82e88822012-09-07 22:44:10 +0000399 *
400 * The PE hierarchy tree might be changed when doing PCI hotplug.
401 * Also, the PCI devices or buses could be removed from the system
402 * during EEH recovery. So we have to call the function remove the
403 * corresponding PE accordingly if necessary.
404 */
Gavin Shan20ee6a92012-09-11 19:16:17 +0000405int eeh_rmv_from_parent_pe(struct eeh_dev *edev, int purge_pe)
Gavin Shan82e88822012-09-07 22:44:10 +0000406{
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000407 struct eeh_pe *pe, *parent, *child;
408 int cnt;
Gavin Shan82e88822012-09-07 22:44:10 +0000409
410 if (!edev->pe) {
411 pr_warning("%s: No PE found for EEH device %s\n",
412 __func__, edev->dn->full_name);
413 return -EEXIST;
414 }
415
416 /* Remove the EEH device */
417 pe = edev->pe;
418 edev->pe = NULL;
419 list_del(&edev->list);
420
421 /*
422 * Check if the parent PE includes any EEH devices.
423 * If not, we should delete that. Also, we should
424 * delete the parent PE if it doesn't have associated
425 * child PEs and EEH devices.
426 */
427 while (1) {
428 parent = pe->parent;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000429 if (pe->type & EEH_PE_PHB)
Gavin Shan82e88822012-09-07 22:44:10 +0000430 break;
431
Gavin Shan20ee6a92012-09-11 19:16:17 +0000432 if (purge_pe) {
433 if (list_empty(&pe->edevs) &&
434 list_empty(&pe->child_list)) {
435 list_del(&pe->child);
436 kfree(pe);
437 } else {
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000438 break;
Gavin Shan20ee6a92012-09-11 19:16:17 +0000439 }
440 } else {
441 if (list_empty(&pe->edevs)) {
442 cnt = 0;
443 list_for_each_entry(child, &pe->child_list, child) {
444 if (!(pe->type & EEH_PE_INVALID)) {
445 cnt++;
446 break;
447 }
448 }
449
450 if (!cnt)
451 pe->type |= EEH_PE_INVALID;
452 else
453 break;
454 }
Gavin Shan82e88822012-09-07 22:44:10 +0000455 }
456
457 pe = parent;
458 }
459
460 return 0;
461}
Gavin Shan5b663522012-09-07 22:44:12 +0000462
463/**
464 * __eeh_pe_state_mark - Mark the state for the PE
465 * @data: EEH PE
466 * @flag: state
467 *
468 * The function is used to mark the indicated state for the given
469 * PE. Also, the associated PCI devices will be put into IO frozen
470 * state as well.
471 */
472static void *__eeh_pe_state_mark(void *data, void *flag)
473{
474 struct eeh_pe *pe = (struct eeh_pe *)data;
475 int state = *((int *)flag);
476 struct eeh_dev *tmp;
477 struct pci_dev *pdev;
478
479 /*
480 * Mark the PE with the indicated state. Also,
481 * the associated PCI device will be put into
482 * I/O frozen state to avoid I/O accesses from
483 * the PCI device driver.
484 */
485 pe->state |= state;
486 eeh_pe_for_each_dev(pe, tmp) {
487 pdev = eeh_dev_to_pci_dev(tmp);
488 if (pdev)
489 pdev->error_state = pci_channel_io_frozen;
490 }
491
492 return NULL;
493}
494
495/**
496 * eeh_pe_state_mark - Mark specified state for PE and its associated device
497 * @pe: EEH PE
498 *
499 * EEH error affects the current PE and its child PEs. The function
500 * is used to mark appropriate state for the affected PEs and the
501 * associated devices.
502 */
503void eeh_pe_state_mark(struct eeh_pe *pe, int state)
504{
505 eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
506}
507
508/**
509 * __eeh_pe_state_clear - Clear state for the PE
510 * @data: EEH PE
511 * @flag: state
512 *
513 * The function is used to clear the indicated state from the
514 * given PE. Besides, we also clear the check count of the PE
515 * as well.
516 */
517static void *__eeh_pe_state_clear(void *data, void *flag)
518{
519 struct eeh_pe *pe = (struct eeh_pe *)data;
520 int state = *((int *)flag);
521
522 pe->state &= ~state;
523 pe->check_count = 0;
524
525 return NULL;
526}
527
528/**
529 * eeh_pe_state_clear - Clear state for the PE and its children
530 * @pe: PE
531 * @state: state to be cleared
532 *
533 * When the PE and its children has been recovered from error,
534 * we need clear the error state for that. The function is used
535 * for the purpose.
536 */
537void eeh_pe_state_clear(struct eeh_pe *pe, int state)
538{
539 eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
540}
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000541
542/**
543 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
544 * @data: EEH device
545 * @flag: Unused
546 *
547 * Loads the PCI configuration space base address registers,
548 * the expansion ROM base address, the latency timer, and etc.
549 * from the saved values in the device node.
550 */
551static void *eeh_restore_one_device_bars(void *data, void *flag)
552{
553 int i;
554 u32 cmd;
555 struct eeh_dev *edev = (struct eeh_dev *)data;
556 struct device_node *dn = eeh_dev_to_of_node(edev);
557
558 for (i = 4; i < 10; i++)
559 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
560 /* 12 == Expansion ROM Address */
561 eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
562
563#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
564#define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
565
566 eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
567 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
568 eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
569 SAVED_BYTE(PCI_LATENCY_TIMER));
570
571 /* max latency, min grant, interrupt pin and line */
572 eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
573
574 /*
575 * Restore PERR & SERR bits, some devices require it,
576 * don't touch the other command bits
577 */
578 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
579 if (edev->config_space[1] & PCI_COMMAND_PARITY)
580 cmd |= PCI_COMMAND_PARITY;
581 else
582 cmd &= ~PCI_COMMAND_PARITY;
583 if (edev->config_space[1] & PCI_COMMAND_SERR)
584 cmd |= PCI_COMMAND_SERR;
585 else
586 cmd &= ~PCI_COMMAND_SERR;
587 eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
588
589 return NULL;
590}
591
592/**
593 * eeh_pe_restore_bars - Restore the PCI config space info
594 * @pe: EEH PE
595 *
596 * This routine performs a recursive walk to the children
597 * of this device as well.
598 */
599void eeh_pe_restore_bars(struct eeh_pe *pe)
600{
601 eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
602}
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000603
604/**
605 * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
606 * @pe: EEH PE
607 *
608 * Retrieve the PCI bus according to the given PE. Basically,
609 * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
610 * primary PCI bus will be retrieved. The parent bus will be
611 * returned for BUS PE. However, we don't have associated PCI
612 * bus for DEVICE PE.
613 */
614struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
615{
616 struct pci_bus *bus = NULL;
617 struct eeh_dev *edev;
618 struct pci_dev *pdev;
619
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000620 if (pe->type & EEH_PE_PHB) {
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000621 bus = pe->phb->bus;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000622 } else if (pe->type & EEH_PE_BUS) {
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000623 edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
624 pdev = eeh_dev_to_pci_dev(edev);
625 if (pdev)
626 bus = pdev->bus;
627 }
628
629 return bus;
630}