xref: /linux/drivers/s390/char/sclp_pci.c (revision 8838a1a2d219a86ab05e679c73f68dd75a25aca5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI I/O adapter configuration related functions.
4  *
5  * Copyright IBM Corp. 2016
6  */
7 #define KMSG_COMPONENT "sclp_cmd"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9 
10 #include <linux/completion.h>
11 #include <linux/export.h>
12 #include <linux/mutex.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 
18 #include <asm/sclp.h>
19 
20 #include "sclp.h"
21 
22 #define SCLP_CMDW_CONFIGURE_PCI			0x001a0001
23 #define SCLP_CMDW_DECONFIGURE_PCI		0x001b0001
24 
25 #define SCLP_ATYPE_PCI				2
26 
27 static DEFINE_MUTEX(sclp_pci_mutex);
28 static struct sclp_register sclp_pci_event = {
29 	.send_mask = EVTYP_ERRNOTIFY_MASK,
30 };
31 
32 struct pci_cfg_sccb {
33 	struct sccb_header header;
34 	u8 atype;		/* adapter type */
35 	u8 reserved1;
36 	u16 reserved2;
37 	u32 aid;		/* adapter identifier */
38 } __packed;
39 
40 static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
41 {
42 	struct pci_cfg_sccb *sccb;
43 	int rc;
44 
45 	if (!SCLP_HAS_PCI_RECONFIG)
46 		return -EOPNOTSUPP;
47 
48 	sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
49 	if (!sccb)
50 		return -ENOMEM;
51 
52 	sccb->header.length = PAGE_SIZE;
53 	sccb->atype = SCLP_ATYPE_PCI;
54 	sccb->aid = fid;
55 	rc = sclp_sync_request(cmd, sccb);
56 	if (rc)
57 		goto out;
58 	switch (sccb->header.response_code) {
59 	case 0x0020:
60 	case 0x0120:
61 		break;
62 	default:
63 		pr_warn("configure PCI I/O adapter failed: cmd=0x%08x  response=0x%04x\n",
64 			cmd, sccb->header.response_code);
65 		rc = -EIO;
66 		break;
67 	}
68 out:
69 	free_page((unsigned long) sccb);
70 	return rc;
71 }
72 
73 int sclp_pci_configure(u32 fid)
74 {
75 	return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
76 }
77 EXPORT_SYMBOL(sclp_pci_configure);
78 
79 int sclp_pci_deconfigure(u32 fid)
80 {
81 	return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
82 }
83 EXPORT_SYMBOL(sclp_pci_deconfigure);
84 
85 static void sclp_pci_callback(struct sclp_req *req, void *data)
86 {
87 	struct completion *completion = data;
88 
89 	complete(completion);
90 }
91 
92 static int sclp_pci_check_report(struct zpci_report_error_header *report)
93 {
94 	if (report->version != 1)
95 		return -EINVAL;
96 
97 	switch (report->action) {
98 	case SCLP_ERRNOTIFY_AQ_RESET:
99 	case SCLP_ERRNOTIFY_AQ_REPAIR:
100 	case SCLP_ERRNOTIFY_AQ_INFO_LOG:
101 	case SCLP_ERRNOTIFY_AQ_OPTICS_DATA:
102 		break;
103 	default:
104 		return -EINVAL;
105 	}
106 
107 	if (report->length > (PAGE_SIZE - sizeof(struct err_notify_sccb)))
108 		return -EINVAL;
109 
110 	return 0;
111 }
112 
113 int sclp_pci_report(struct zpci_report_error_header *report, u32 fh, u32 fid)
114 {
115 	DECLARE_COMPLETION_ONSTACK(completion);
116 	struct err_notify_sccb *sccb;
117 	struct sclp_req req;
118 	int ret;
119 
120 	ret = sclp_pci_check_report(report);
121 	if (ret)
122 		return ret;
123 
124 	mutex_lock(&sclp_pci_mutex);
125 	ret = sclp_register(&sclp_pci_event);
126 	if (ret)
127 		goto out_unlock;
128 
129 	if (!(sclp_pci_event.sclp_receive_mask & EVTYP_ERRNOTIFY_MASK)) {
130 		ret = -EOPNOTSUPP;
131 		goto out_unregister;
132 	}
133 
134 	sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
135 	if (!sccb) {
136 		ret = -ENOMEM;
137 		goto out_unregister;
138 	}
139 
140 	memset(&req, 0, sizeof(req));
141 	req.callback_data = &completion;
142 	req.callback = sclp_pci_callback;
143 	req.command = SCLP_CMDW_WRITE_EVENT_DATA;
144 	req.status = SCLP_REQ_FILLED;
145 	req.sccb = sccb;
146 
147 	sccb->evbuf.header.length = sizeof(sccb->evbuf) + report->length;
148 	sccb->evbuf.header.type = EVTYP_ERRNOTIFY;
149 	sccb->header.length = sizeof(sccb->header) + sccb->evbuf.header.length;
150 
151 	sccb->evbuf.action = report->action;
152 	sccb->evbuf.atype = SCLP_ATYPE_PCI;
153 	sccb->evbuf.fh = fh;
154 	sccb->evbuf.fid = fid;
155 
156 	memcpy(sccb->evbuf.data, report->data, report->length);
157 
158 	ret = sclp_add_request(&req);
159 	if (ret)
160 		goto out_free_req;
161 
162 	wait_for_completion(&completion);
163 	if (req.status != SCLP_REQ_DONE) {
164 		pr_warn("request failed (status=0x%02x)\n",
165 			req.status);
166 		ret = -EIO;
167 		goto out_free_req;
168 	}
169 
170 	if (sccb->header.response_code != 0x0020) {
171 		pr_warn("request failed with response code 0x%x\n",
172 			sccb->header.response_code);
173 		ret = -EIO;
174 	}
175 
176 out_free_req:
177 	free_page((unsigned long) sccb);
178 out_unregister:
179 	sclp_unregister(&sclp_pci_event);
180 out_unlock:
181 	mutex_unlock(&sclp_pci_mutex);
182 	return ret;
183 }
184