xref: /openbmc/linux/arch/s390/pci/pci_event.c (revision b802fb99)
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 	struct pci_dev *pdev = zdev ? zdev->pdev : NULL;
50 
51 	zpci_err("error CCDF:\n");
52 	zpci_err_hex(ccdf, sizeof(*ccdf));
53 
54 	pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
55 	       pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
56 
57 	if (!pdev)
58 		return;
59 
60 	pdev->error_state = pci_channel_io_perm_failure;
61 }
62 
63 void zpci_event_error(void *data)
64 {
65 	if (zpci_is_enabled())
66 		__zpci_event_error(data);
67 }
68 
69 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
70 {
71 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
72 	struct pci_dev *pdev = zdev ? zdev->pdev : NULL;
73 	int ret;
74 
75 	pr_info("%s: Event 0x%x reconfigured PCI function 0x%x\n",
76 		pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
77 	zpci_err("avail CCDF:\n");
78 	zpci_err_hex(ccdf, sizeof(*ccdf));
79 
80 	switch (ccdf->pec) {
81 	case 0x0301: /* Reserved|Standby -> Configured */
82 		if (!zdev) {
83 			ret = clp_add_pci_device(ccdf->fid, ccdf->fh, 0);
84 			if (ret)
85 				break;
86 			zdev = get_zdev_by_fid(ccdf->fid);
87 		}
88 		if (!zdev || zdev->state != ZPCI_FN_STATE_STANDBY)
89 			break;
90 		zdev->state = ZPCI_FN_STATE_CONFIGURED;
91 		zdev->fh = ccdf->fh;
92 		ret = zpci_enable_device(zdev);
93 		if (ret)
94 			break;
95 		pci_lock_rescan_remove();
96 		pci_rescan_bus(zdev->bus);
97 		pci_unlock_rescan_remove();
98 		break;
99 	case 0x0302: /* Reserved -> Standby */
100 		if (!zdev)
101 			clp_add_pci_device(ccdf->fid, ccdf->fh, 0);
102 		break;
103 	case 0x0303: /* Deconfiguration requested */
104 		if (pdev)
105 			pci_stop_and_remove_bus_device_locked(pdev);
106 
107 		ret = zpci_disable_device(zdev);
108 		if (ret)
109 			break;
110 
111 		ret = sclp_pci_deconfigure(zdev->fid);
112 		zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
113 		if (!ret)
114 			zdev->state = ZPCI_FN_STATE_STANDBY;
115 
116 		break;
117 	case 0x0304: /* Configured -> Standby */
118 		if (pdev) {
119 			/* Give the driver a hint that the function is
120 			 * already unusable. */
121 			pdev->error_state = pci_channel_io_perm_failure;
122 			pci_stop_and_remove_bus_device_locked(pdev);
123 		}
124 
125 		zdev->fh = ccdf->fh;
126 		zpci_disable_device(zdev);
127 		zdev->state = ZPCI_FN_STATE_STANDBY;
128 		break;
129 	case 0x0306: /* 0x308 or 0x302 for multiple devices */
130 		clp_rescan_pci_devices();
131 		break;
132 	case 0x0308: /* Standby -> Reserved */
133 		if (!zdev)
134 			break;
135 		pci_stop_root_bus(zdev->bus);
136 		pci_remove_root_bus(zdev->bus);
137 		break;
138 	default:
139 		break;
140 	}
141 }
142 
143 void zpci_event_availability(void *data)
144 {
145 	if (zpci_is_enabled())
146 		__zpci_event_availability(data);
147 }
148