xref: /openbmc/linux/arch/s390/pci/pci_event.c (revision c4a11bf4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2012
4  *
5  *  Author(s):
6  *    Jan Glauber <jang@linux.vnet.ibm.com>
7  */
8 
9 #define KMSG_COMPONENT "zpci"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <asm/pci_debug.h>
15 #include <asm/pci_dma.h>
16 #include <asm/sclp.h>
17 
18 #include "pci_bus.h"
19 
20 /* Content Code Description for PCI Function Error */
21 struct zpci_ccdf_err {
22 	u32 reserved1;
23 	u32 fh;				/* function handle */
24 	u32 fid;			/* function id */
25 	u32 ett		:  4;		/* expected table type */
26 	u32 mvn		: 12;		/* MSI vector number */
27 	u32 dmaas	:  8;		/* DMA address space */
28 	u32		:  6;
29 	u32 q		:  1;		/* event qualifier */
30 	u32 rw		:  1;		/* read/write */
31 	u64 faddr;			/* failing address */
32 	u32 reserved3;
33 	u16 reserved4;
34 	u16 pec;			/* PCI event code */
35 } __packed;
36 
37 /* Content Code Description for PCI Function Availability */
38 struct zpci_ccdf_avail {
39 	u32 reserved1;
40 	u32 fh;				/* function handle */
41 	u32 fid;			/* function id */
42 	u32 reserved2;
43 	u32 reserved3;
44 	u32 reserved4;
45 	u32 reserved5;
46 	u16 reserved6;
47 	u16 pec;			/* PCI event code */
48 } __packed;
49 
50 static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
51 {
52 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
53 	struct pci_dev *pdev = NULL;
54 
55 	zpci_dbg(3, "err fid:%x, fh:%x, pec:%x\n",
56 		 ccdf->fid, ccdf->fh, ccdf->pec);
57 	zpci_err("error CCDF:\n");
58 	zpci_err_hex(ccdf, sizeof(*ccdf));
59 
60 	if (zdev)
61 		pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
62 
63 	pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
64 	       pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
65 
66 	if (!pdev)
67 		return;
68 
69 	pdev->error_state = pci_channel_io_perm_failure;
70 	pci_dev_put(pdev);
71 }
72 
73 void zpci_event_error(void *data)
74 {
75 	if (zpci_is_enabled())
76 		__zpci_event_error(data);
77 }
78 
79 static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh)
80 {
81 	zdev->fh = fh;
82 	/* Give the driver a hint that the function is
83 	 * already unusable.
84 	 */
85 	zpci_bus_remove_device(zdev, true);
86 	/* Even though the device is already gone we still
87 	 * need to free zPCI resources as part of the disable.
88 	 */
89 	if (zdev->dma_table)
90 		zpci_dma_exit_device(zdev);
91 	if (zdev_enabled(zdev))
92 		zpci_disable_device(zdev);
93 	zdev->state = ZPCI_FN_STATE_STANDBY;
94 }
95 
96 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
97 {
98 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
99 	enum zpci_state state;
100 
101 	zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n",
102 		 ccdf->fid, ccdf->fh, ccdf->pec);
103 	zpci_err("avail CCDF:\n");
104 	zpci_err_hex(ccdf, sizeof(*ccdf));
105 
106 	switch (ccdf->pec) {
107 	case 0x0301: /* Reserved|Standby -> Configured */
108 		if (!zdev) {
109 			zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
110 			if (IS_ERR(zdev))
111 				break;
112 		} else {
113 			/* the configuration request may be stale */
114 			if (zdev->state != ZPCI_FN_STATE_STANDBY)
115 				break;
116 			zdev->state = ZPCI_FN_STATE_CONFIGURED;
117 		}
118 		zpci_scan_configured_device(zdev, ccdf->fh);
119 		break;
120 	case 0x0302: /* Reserved -> Standby */
121 		if (!zdev)
122 			zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
123 		else
124 			zdev->fh = ccdf->fh;
125 		break;
126 	case 0x0303: /* Deconfiguration requested */
127 		if (zdev) {
128 			/* The event may have been queued before we confirgured
129 			 * the device.
130 			 */
131 			if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
132 				break;
133 			zdev->fh = ccdf->fh;
134 			zpci_deconfigure_device(zdev);
135 		}
136 		break;
137 	case 0x0304: /* Configured -> Standby|Reserved */
138 		if (zdev) {
139 			/* The event may have been queued before we confirgured
140 			 * the device.:
141 			 */
142 			if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
143 				zpci_event_hard_deconfigured(zdev, ccdf->fh);
144 			/* The 0x0304 event may immediately reserve the device */
145 			if (!clp_get_state(zdev->fid, &state) &&
146 			    state == ZPCI_FN_STATE_RESERVED) {
147 				zpci_device_reserved(zdev);
148 			}
149 		}
150 		break;
151 	case 0x0306: /* 0x308 or 0x302 for multiple devices */
152 		zpci_remove_reserved_devices();
153 		clp_scan_pci_devices();
154 		break;
155 	case 0x0308: /* Standby -> Reserved */
156 		if (!zdev)
157 			break;
158 		zpci_device_reserved(zdev);
159 		break;
160 	default:
161 		break;
162 	}
163 }
164 
165 void zpci_event_availability(void *data)
166 {
167 	if (zpci_is_enabled())
168 		__zpci_event_availability(data);
169 }
170