Merge branches 'for-4.3/chicony', 'for-4.3/cp2112', 'for-4.3/i2c-hid', 'for-4.3/lenov...
[firefly-linux-kernel-4.4.55.git] / arch / s390 / pci / pci_event.c
1 /*
2  *  Copyright IBM Corp. 2012
3  *
4  *  Author(s):
5  *    Jan Glauber <jang@linux.vnet.ibm.com>
6  */
7
8 #define KMSG_COMPONENT "zpci"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <asm/pci_debug.h>
14 #include <asm/sclp.h>
15
16 /* Content Code Description for PCI Function Error */
17 struct zpci_ccdf_err {
18         u32 reserved1;
19         u32 fh;                         /* function handle */
20         u32 fid;                        /* function id */
21         u32 ett         :  4;           /* expected table type */
22         u32 mvn         : 12;           /* MSI vector number */
23         u32 dmaas       :  8;           /* DMA address space */
24         u32             :  6;
25         u32 q           :  1;           /* event qualifier */
26         u32 rw          :  1;           /* read/write */
27         u64 faddr;                      /* failing address */
28         u32 reserved3;
29         u16 reserved4;
30         u16 pec;                        /* PCI event code */
31 } __packed;
32
33 /* Content Code Description for PCI Function Availability */
34 struct zpci_ccdf_avail {
35         u32 reserved1;
36         u32 fh;                         /* function handle */
37         u32 fid;                        /* function id */
38         u32 reserved2;
39         u32 reserved3;
40         u32 reserved4;
41         u32 reserved5;
42         u16 reserved6;
43         u16 pec;                        /* PCI event code */
44 } __packed;
45
46 static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
47 {
48         struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
49
50         zpci_err("error CCDF:\n");
51         zpci_err_hex(ccdf, sizeof(*ccdf));
52
53         if (!zdev)
54                 return;
55
56         pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
57                pci_name(zdev->pdev), ccdf->pec, ccdf->fid);
58 }
59
60 void zpci_event_error(void *data)
61 {
62         if (zpci_is_enabled())
63                 __zpci_event_error(data);
64 }
65
66 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
67 {
68         struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
69         struct pci_dev *pdev = zdev ? zdev->pdev : NULL;
70         int ret;
71
72         pr_info("%s: Event 0x%x reconfigured PCI function 0x%x\n",
73                 pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
74         zpci_err("avail CCDF:\n");
75         zpci_err_hex(ccdf, sizeof(*ccdf));
76
77         switch (ccdf->pec) {
78         case 0x0301: /* Reserved|Standby -> Configured */
79                 if (!zdev) {
80                         ret = clp_add_pci_device(ccdf->fid, ccdf->fh, 0);
81                         if (ret)
82                                 break;
83                         zdev = get_zdev_by_fid(ccdf->fid);
84                 }
85                 if (!zdev || zdev->state != ZPCI_FN_STATE_STANDBY)
86                         break;
87                 zdev->state = ZPCI_FN_STATE_CONFIGURED;
88                 zdev->fh = ccdf->fh;
89                 ret = zpci_enable_device(zdev);
90                 if (ret)
91                         break;
92                 pci_rescan_bus(zdev->bus);
93                 break;
94         case 0x0302: /* Reserved -> Standby */
95                 if (!zdev)
96                         clp_add_pci_device(ccdf->fid, ccdf->fh, 0);
97                 break;
98         case 0x0303: /* Deconfiguration requested */
99                 if (pdev)
100                         pci_stop_and_remove_bus_device(pdev);
101
102                 ret = zpci_disable_device(zdev);
103                 if (ret)
104                         break;
105
106                 ret = sclp_pci_deconfigure(zdev->fid);
107                 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
108                 if (!ret)
109                         zdev->state = ZPCI_FN_STATE_STANDBY;
110
111                 break;
112         case 0x0304: /* Configured -> Standby */
113                 if (pdev) {
114                         /* Give the driver a hint that the function is
115                          * already unusable. */
116                         pdev->error_state = pci_channel_io_perm_failure;
117                         pci_stop_and_remove_bus_device(pdev);
118                 }
119
120                 zdev->fh = ccdf->fh;
121                 zpci_disable_device(zdev);
122                 zdev->state = ZPCI_FN_STATE_STANDBY;
123                 break;
124         case 0x0306: /* 0x308 or 0x302 for multiple devices */
125                 clp_rescan_pci_devices();
126                 break;
127         case 0x0308: /* Standby -> Reserved */
128                 if (!zdev)
129                         break;
130                 pci_stop_root_bus(zdev->bus);
131                 pci_remove_root_bus(zdev->bus);
132                 break;
133         default:
134                 break;
135         }
136 }
137
138 void zpci_event_availability(void *data)
139 {
140         if (zpci_is_enabled())
141                 __zpci_event_availability(data);
142 }