39567b262deacb57fdd52869c74626ab358c9b4f
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / pseries / eeh_pseries.c
1 /*
2  * The file intends to implement the platform dependent EEH operations on pseries.
3  * Actually, the pseries platform is built based on RTAS heavily. That means the
4  * pseries platform dependent EEH operations will be built on RTAS calls. The functions
5  * are devired from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
6  * been done.
7  *
8  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
9  * Copyright IBM Corporation 2001, 2005, 2006
10  * Copyright Dave Engebretsen & Todd Inglett 2001
11  * Copyright Linas Vepstas 2005, 2006
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  */
27
28 #include <linux/atomic.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/of.h>
34 #include <linux/pci.h>
35 #include <linux/proc_fs.h>
36 #include <linux/rbtree.h>
37 #include <linux/sched.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40
41 #include <asm/eeh.h>
42 #include <asm/eeh_event.h>
43 #include <asm/io.h>
44 #include <asm/machdep.h>
45 #include <asm/ppc-pci.h>
46 #include <asm/rtas.h>
47
48 /* RTAS tokens */
49 static int ibm_set_eeh_option;
50 static int ibm_set_slot_reset;
51 static int ibm_read_slot_reset_state;
52 static int ibm_read_slot_reset_state2;
53 static int ibm_slot_error_detail;
54 static int ibm_get_config_addr_info;
55 static int ibm_get_config_addr_info2;
56 static int ibm_configure_bridge;
57 static int ibm_configure_pe;
58
59 /**
60  * pseries_eeh_init - EEH platform dependent initialization
61  *
62  * EEH platform dependent initialization on pseries.
63  */
64 static int pseries_eeh_init(void)
65 {
66         /* figure out EEH RTAS function call tokens */
67         ibm_set_eeh_option              = rtas_token("ibm,set-eeh-option");
68         ibm_set_slot_reset              = rtas_token("ibm,set-slot-reset");
69         ibm_read_slot_reset_state2      = rtas_token("ibm,read-slot-reset-state2");
70         ibm_read_slot_reset_state       = rtas_token("ibm,read-slot-reset-state");
71         ibm_slot_error_detail           = rtas_token("ibm,slot-error-detail");
72         ibm_get_config_addr_info2       = rtas_token("ibm,get-config-addr-info2");
73         ibm_get_config_addr_info        = rtas_token("ibm,get-config-addr-info");
74         ibm_configure_pe                = rtas_token("ibm,configure-pe");
75         ibm_configure_bridge            = rtas_token ("ibm,configure-bridge");
76
77         /* necessary sanity check */
78         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
79                 pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
80                         __func__);
81                 return -EINVAL;
82         } else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
83                 pr_warning("%s: RTAS service <ibm, set-slot-reset> invalid\n",
84                         __func__);
85                 return -EINVAL;
86         } else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
87                    ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) {
88                 pr_warning("%s: RTAS service <ibm,read-slot-reset-state2> and "
89                         "<ibm,read-slot-reset-state> invalid\n",
90                         __func__);
91                 return -EINVAL;
92         } else if (ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE) {
93                 pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
94                         __func__);
95                 return -EINVAL;
96         } else if (ibm_get_config_addr_info2 == RTAS_UNKNOWN_SERVICE &&
97                    ibm_get_config_addr_info == RTAS_UNKNOWN_SERVICE) {
98                 pr_warning("%s: RTAS service <ibm,get-config-addr-info2> and "
99                         "<ibm,get-config-addr-info> invalid\n",
100                         __func__);
101                 return -EINVAL;
102         } else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
103                    ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
104                 pr_warning("%s: RTAS service <ibm,configure-pe> and "
105                         "<ibm,configure-bridge> invalid\n",
106                         __func__);
107                 return -EINVAL;
108         }
109
110         return 0;
111 }
112
113 /**
114  * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
115  * @dn: device node
116  * @option: operation to be issued
117  *
118  * The function is used to control the EEH functionality globally.
119  * Currently, following options are support according to PAPR:
120  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
121  */
122 static int pseries_eeh_set_option(struct device_node *dn, int option)
123 {
124         int ret = 0;
125         struct pci_dn *pdn;
126         const u32 *reg;
127         int config_addr;
128
129         pdn = PCI_DN(dn);
130
131         /*
132          * When we're enabling or disabling EEH functioality on
133          * the particular PE, the PE config address is possibly
134          * unavailable. Therefore, we have to figure it out from
135          * the FDT node.
136          */
137         switch (option) {
138         case EEH_OPT_DISABLE:
139         case EEH_OPT_ENABLE:
140                 reg = of_get_property(dn, "reg", NULL);
141                 config_addr = reg[0];
142                 break;
143
144         case EEH_OPT_THAW_MMIO:
145         case EEH_OPT_THAW_DMA:
146                 config_addr = pdn->eeh_config_addr;
147                 if (pdn->eeh_pe_config_addr)
148                         config_addr = pdn->eeh_pe_config_addr;
149                 break;
150
151         default:
152                 pr_err("%s: Invalid option %d\n",
153                         __func__, option);
154                 return -EINVAL;
155         }
156
157         ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
158                         config_addr, BUID_HI(pdn->phb->buid),
159                         BUID_LO(pdn->phb->buid), option);
160
161         return ret;
162 }
163
164 /**
165  * pseries_eeh_get_pe_addr - Retrieve PE address
166  * @dn: device node
167  *
168  * Retrieve the assocated PE address. Actually, there're 2 RTAS
169  * function calls dedicated for the purpose. We need implement
170  * it through the new function and then the old one. Besides,
171  * you should make sure the config address is figured out from
172  * FDT node before calling the function.
173  *
174  * It's notable that zero'ed return value means invalid PE config
175  * address.
176  */
177 static int pseries_eeh_get_pe_addr(struct device_node *dn)
178 {
179         struct pci_dn *pdn;
180         int ret = 0;
181         int rets[3];
182
183         pdn = PCI_DN(dn);
184
185         if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
186                 /*
187                  * First of all, we need to make sure there has one PE
188                  * associated with the device. Otherwise, PE address is
189                  * meaningless.
190                  */
191                 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
192                                 pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
193                                 BUID_LO(pdn->phb->buid), 1);
194                 if (ret || (rets[0] == 0))
195                         return 0;
196
197                 /* Retrieve the associated PE config address */
198                 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
199                                 pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
200                                 BUID_LO(pdn->phb->buid), 0);
201                 if (ret) {
202                         pr_warning("%s: Failed to get PE address for %s\n",
203                                 __func__, dn->full_name);
204                         return 0;
205                 }
206
207                 return rets[0];
208         }
209
210         if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
211                 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
212                                 pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
213                                 BUID_LO(pdn->phb->buid), 0);
214                 if (ret) {
215                         pr_warning("%s: Failed to get PE address for %s\n",
216                                 __func__, dn->full_name);
217                         return 0;
218                 }
219
220                 return rets[0];
221         }
222
223         return ret;
224 }
225
226 /**
227  * pseries_eeh_get_state - Retrieve PE state
228  * @dn: PE associated device node
229  * @state: return value
230  *
231  * Retrieve the state of the specified PE. On RTAS compliant
232  * pseries platform, there already has one dedicated RTAS function
233  * for the purpose. It's notable that the associated PE config address
234  * might be ready when calling the function. Therefore, endeavour to
235  * use the PE config address if possible. Further more, there're 2
236  * RTAS calls for the purpose, we need to try the new one and back
237  * to the old one if the new one couldn't work properly.
238  */
239 static int pseries_eeh_get_state(struct device_node *dn, int *state)
240 {
241         struct pci_dn *pdn;
242         int config_addr;
243         int ret;
244         int rets[4];
245         int result;
246
247         /* Figure out PE config address if possible */
248         pdn = PCI_DN(dn);
249         config_addr = pdn->eeh_config_addr;
250         if (pdn->eeh_pe_config_addr)
251                 config_addr = pdn->eeh_pe_config_addr;
252
253         if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
254                 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
255                                 config_addr, BUID_HI(pdn->phb->buid),
256                                 BUID_LO(pdn->phb->buid));
257         } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
258                 /* Fake PE unavailable info */
259                 rets[2] = 0;
260                 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
261                                 config_addr, BUID_HI(pdn->phb->buid),
262                                 BUID_LO(pdn->phb->buid));
263         } else {
264                 return EEH_STATE_NOT_SUPPORT;
265         }
266
267         if (ret)
268                 return ret;
269
270         /* Parse the result out */
271         result = 0;
272         if (rets[1]) {
273                 switch(rets[0]) {
274                 case 0:
275                         result &= ~EEH_STATE_RESET_ACTIVE;
276                         result |= EEH_STATE_MMIO_ACTIVE;
277                         result |= EEH_STATE_DMA_ACTIVE;
278                         break;
279                 case 1:
280                         result |= EEH_STATE_RESET_ACTIVE;
281                         result |= EEH_STATE_MMIO_ACTIVE;
282                         result |= EEH_STATE_DMA_ACTIVE;
283                         break;
284                 case 2:
285                         result &= ~EEH_STATE_RESET_ACTIVE;
286                         result &= ~EEH_STATE_MMIO_ACTIVE;
287                         result &= ~EEH_STATE_DMA_ACTIVE;
288                         break;
289                 case 4:
290                         result &= ~EEH_STATE_RESET_ACTIVE;
291                         result &= ~EEH_STATE_MMIO_ACTIVE;
292                         result &= ~EEH_STATE_DMA_ACTIVE;
293                         result |= EEH_STATE_MMIO_ENABLED;
294                         break;
295                 case 5:
296                         if (rets[2]) {
297                                 if (state) *state = rets[2];
298                                 result = EEH_STATE_UNAVAILABLE;
299                         } else {
300                                 result = EEH_STATE_NOT_SUPPORT;
301                         }
302                 default:
303                         result = EEH_STATE_NOT_SUPPORT;
304                 }
305         } else {
306                 result = EEH_STATE_NOT_SUPPORT;
307         }
308
309         return result;
310 }
311
312 /**
313  * pseries_eeh_reset - Reset the specified PE
314  * @dn: PE associated device node
315  * @option: reset option
316  *
317  * Reset the specified PE
318  */
319 static int pseries_eeh_reset(struct device_node *dn, int option)
320 {
321         return 0;
322 }
323
324 /**
325  * pseries_eeh_wait_state - Wait for PE state
326  * @dn: PE associated device node
327  * @max_wait: maximal period in microsecond
328  *
329  * Wait for the state of associated PE. It might take some time
330  * to retrieve the PE's state.
331  */
332 static int pseries_eeh_wait_state(struct device_node *dn, int max_wait)
333 {
334         return 0;
335 }
336
337 /**
338  * pseries_eeh_get_log - Retrieve error log
339  * @dn: device node
340  * @severity: temporary or permanent error log
341  * @drv_log: driver log to be combined with retrieved error log
342  * @len: length of driver log
343  *
344  * Retrieve the temporary or permanent error from the PE.
345  * Actually, the error will be retrieved through the dedicated
346  * RTAS call.
347  */
348 static int pseries_eeh_get_log(struct device_node *dn, int severity, char *drv_log, unsigned long len)
349 {
350         return 0;
351 }
352
353 /**
354  * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
355  * @dn: PE associated device node
356  *
357  * The function will be called to reconfigure the bridges included
358  * in the specified PE so that the mulfunctional PE would be recovered
359  * again.
360  */
361 static int pseries_eeh_configure_bridge(struct device_node *dn)
362 {
363         return 0;
364 }
365
366 static struct eeh_ops pseries_eeh_ops = {
367         .name                   = "pseries",
368         .init                   = pseries_eeh_init,
369         .set_option             = pseries_eeh_set_option,
370         .get_pe_addr            = pseries_eeh_get_pe_addr,
371         .get_state              = pseries_eeh_get_state,
372         .reset                  = pseries_eeh_reset,
373         .wait_state             = pseries_eeh_wait_state,
374         .get_log                = pseries_eeh_get_log,
375         .configure_bridge       = pseries_eeh_configure_bridge
376 };
377
378 /**
379  * eeh_pseries_init - Register platform dependent EEH operations
380  *
381  * EEH initialization on pseries platform. This function should be
382  * called before any EEH related functions.
383  */
384 int __init eeh_pseries_init(void)
385 {
386         return eeh_ops_register(&pseries_eeh_ops);
387 }