blob: e7c84a4e5eb5833c7a83e9c4dc8721238750f786 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Ott68dd13d2015-11-27 11:15:36 +01002/*
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>
Sebastian Ott12283a42015-11-27 11:18:02 +010012#include <linux/mutex.h>
Sebastian Ott68dd13d2015-11-27 11:15:36 +010013#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
Sebastian Ott12283a42015-11-27 11:18:02 +010025#define SCLP_ATYPE_PCI 2
26
27#define SCLP_ERRNOTIFY_AQ_REPAIR 1
28#define SCLP_ERRNOTIFY_AQ_INFO_LOG 2
29
30static DEFINE_MUTEX(sclp_pci_mutex);
31static struct sclp_register sclp_pci_event = {
32 .send_mask = EVTYP_ERRNOTIFY_MASK,
33};
34
35struct err_notify_evbuf {
36 struct evbuf_header header;
37 u8 action;
38 u8 atype;
39 u32 fh;
40 u32 fid;
41 u8 data[0];
42} __packed;
43
44struct err_notify_sccb {
45 struct sccb_header header;
46 struct err_notify_evbuf evbuf;
47} __packed;
Sebastian Ott68dd13d2015-11-27 11:15:36 +010048
49struct pci_cfg_sccb {
50 struct sccb_header header;
51 u8 atype; /* adapter type */
52 u8 reserved1;
53 u16 reserved2;
54 u32 aid; /* adapter identifier */
55} __packed;
56
57static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
58{
59 struct pci_cfg_sccb *sccb;
60 int rc;
61
62 if (!SCLP_HAS_PCI_RECONFIG)
63 return -EOPNOTSUPP;
64
65 sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
66 if (!sccb)
67 return -ENOMEM;
68
69 sccb->header.length = PAGE_SIZE;
Sebastian Ott12283a42015-11-27 11:18:02 +010070 sccb->atype = SCLP_ATYPE_PCI;
Sebastian Ott68dd13d2015-11-27 11:15:36 +010071 sccb->aid = fid;
72 rc = sclp_sync_request(cmd, sccb);
73 if (rc)
74 goto out;
75 switch (sccb->header.response_code) {
76 case 0x0020:
77 case 0x0120:
78 break;
79 default:
80 pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
81 cmd, sccb->header.response_code);
82 rc = -EIO;
83 break;
84 }
85out:
86 free_page((unsigned long) sccb);
87 return rc;
88}
89
90int sclp_pci_configure(u32 fid)
91{
92 return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
93}
94EXPORT_SYMBOL(sclp_pci_configure);
95
96int sclp_pci_deconfigure(u32 fid)
97{
98 return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
99}
100EXPORT_SYMBOL(sclp_pci_deconfigure);
Sebastian Ott12283a42015-11-27 11:18:02 +0100101
102static void sclp_pci_callback(struct sclp_req *req, void *data)
103{
104 struct completion *completion = data;
105
106 complete(completion);
107}
108
109static int sclp_pci_check_report(struct zpci_report_error_header *report)
110{
111 if (report->version != 1)
112 return -EINVAL;
113
114 if (report->action != SCLP_ERRNOTIFY_AQ_REPAIR &&
115 report->action != SCLP_ERRNOTIFY_AQ_INFO_LOG)
116 return -EINVAL;
117
118 if (report->length > (PAGE_SIZE - sizeof(struct err_notify_sccb)))
119 return -EINVAL;
120
121 return 0;
122}
123
124int sclp_pci_report(struct zpci_report_error_header *report, u32 fh, u32 fid)
125{
126 DECLARE_COMPLETION_ONSTACK(completion);
127 struct err_notify_sccb *sccb;
Martin Schwidefsky99d00a22016-04-18 09:52:05 +0200128 struct sclp_req req;
Sebastian Ott12283a42015-11-27 11:18:02 +0100129 int ret;
130
131 ret = sclp_pci_check_report(report);
132 if (ret)
133 return ret;
134
135 mutex_lock(&sclp_pci_mutex);
136 ret = sclp_register(&sclp_pci_event);
137 if (ret)
138 goto out_unlock;
139
140 if (!(sclp_pci_event.sclp_receive_mask & EVTYP_ERRNOTIFY_MASK)) {
141 ret = -EOPNOTSUPP;
142 goto out_unregister;
143 }
144
145 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
146 if (!sccb) {
147 ret = -ENOMEM;
148 goto out_unregister;
149 }
150
Martin Schwidefsky99d00a22016-04-18 09:52:05 +0200151 memset(&req, 0, sizeof(req));
Sebastian Ott12283a42015-11-27 11:18:02 +0100152 req.callback_data = &completion;
153 req.callback = sclp_pci_callback;
154 req.command = SCLP_CMDW_WRITE_EVENT_DATA;
155 req.status = SCLP_REQ_FILLED;
156 req.sccb = sccb;
157
158 sccb->evbuf.header.length = sizeof(sccb->evbuf) + report->length;
159 sccb->evbuf.header.type = EVTYP_ERRNOTIFY;
160 sccb->header.length = sizeof(sccb->header) + sccb->evbuf.header.length;
161
162 sccb->evbuf.action = report->action;
163 sccb->evbuf.atype = SCLP_ATYPE_PCI;
164 sccb->evbuf.fh = fh;
165 sccb->evbuf.fid = fid;
166
167 memcpy(sccb->evbuf.data, report->data, report->length);
168
169 ret = sclp_add_request(&req);
170 if (ret)
171 goto out_free_req;
172
173 wait_for_completion(&completion);
174 if (req.status != SCLP_REQ_DONE) {
175 pr_warn("request failed (status=0x%02x)\n",
176 req.status);
177 ret = -EIO;
178 goto out_free_req;
179 }
180
181 if (sccb->header.response_code != 0x0020) {
182 pr_warn("request failed with response code 0x%x\n",
183 sccb->header.response_code);
184 ret = -EIO;
185 }
186
187out_free_req:
188 free_page((unsigned long) sccb);
189out_unregister:
190 sclp_unregister(&sclp_pci_event);
191out_unlock:
192 mutex_unlock(&sclp_pci_mutex);
193 return ret;
194}