1 /*
2  * Sclp "store data in absolut storage"
3  *
4  * Copyright IBM Corp. 2003,2007
5  * Author(s): Michael Holzheu
6  */
7 
8 #define KMSG_COMPONENT "sclp_sdias"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
11 #include <linux/completion.h>
12 #include <linux/sched.h>
13 #include <asm/sclp.h>
14 #include <asm/debug.h>
15 #include <asm/ipl.h>
16 
17 #include "sclp.h"
18 #include "sclp_rw.h"
19 
20 #define TRACE(x...) debug_sprintf_event(sdias_dbf, 1, x)
21 
22 #define SDIAS_RETRIES 300
23 #define SDIAS_SLEEP_TICKS 50
24 
25 #define EQ_STORE_DATA	0x0
26 #define EQ_SIZE		0x1
27 #define DI_FCP_DUMP	0x0
28 #define ASA_SIZE_32	0x0
29 #define ASA_SIZE_64	0x1
30 #define EVSTATE_ALL_STORED	0x0
31 #define EVSTATE_NO_DATA		0x3
32 #define EVSTATE_PART_STORED	0x10
33 
34 static struct debug_info *sdias_dbf;
35 
36 static struct sclp_register sclp_sdias_register = {
37 	.send_mask = EVTYP_SDIAS_MASK,
38 };
39 
40 struct sdias_evbuf {
41 	struct	evbuf_header hdr;
42 	u8	event_qual;
43 	u8	data_id;
44 	u64	reserved2;
45 	u32	event_id;
46 	u16	reserved3;
47 	u8	asa_size;
48 	u8	event_status;
49 	u32	reserved4;
50 	u32	blk_cnt;
51 	u64	asa;
52 	u32	reserved5;
53 	u32	fbn;
54 	u32	reserved6;
55 	u32	lbn;
56 	u16	reserved7;
57 	u16	dbs;
58 } __attribute__((packed));
59 
60 struct sdias_sccb {
61 	struct sccb_header  hdr;
62 	struct sdias_evbuf  evbuf;
63 } __attribute__((packed));
64 
65 static struct sdias_sccb sccb __attribute__((aligned(4096)));
66 static struct sdias_evbuf sdias_evbuf;
67 
68 static DECLARE_COMPLETION(evbuf_accepted);
69 static DECLARE_COMPLETION(evbuf_done);
70 static DEFINE_MUTEX(sdias_mutex);
71 
72 /*
73  * Called by SCLP base when read event data has been completed (async mode only)
74  */
sclp_sdias_receiver_fn(struct evbuf_header * evbuf)75 static void sclp_sdias_receiver_fn(struct evbuf_header *evbuf)
76 {
77 	memcpy(&sdias_evbuf, evbuf,
78 	       min_t(unsigned long, sizeof(sdias_evbuf), evbuf->length));
79 	complete(&evbuf_done);
80 	TRACE("sclp_sdias_receiver_fn done\n");
81 }
82 
83 /*
84  * Called by SCLP base when sdias event has been accepted
85  */
sdias_callback(struct sclp_req * request,void * data)86 static void sdias_callback(struct sclp_req *request, void *data)
87 {
88 	complete(&evbuf_accepted);
89 	TRACE("callback done\n");
90 }
91 
sdias_sclp_send(struct sclp_req * req)92 static int sdias_sclp_send(struct sclp_req *req)
93 {
94 	int retries;
95 	int rc;
96 
97 	for (retries = SDIAS_RETRIES; retries; retries--) {
98 		TRACE("add request\n");
99 		rc = sclp_add_request(req);
100 		if (rc) {
101 			/* not initiated, wait some time and retry */
102 			set_current_state(TASK_INTERRUPTIBLE);
103 			TRACE("add request failed: rc = %i\n",rc);
104 			schedule_timeout(SDIAS_SLEEP_TICKS);
105 			continue;
106 		}
107 		/* initiated, wait for completion of service call */
108 		wait_for_completion(&evbuf_accepted);
109 		if (req->status == SCLP_REQ_FAILED) {
110 			TRACE("sclp request failed\n");
111 			continue;
112 		}
113 		/* if not accepted, retry */
114 		if (!(sccb.evbuf.hdr.flags & 0x80)) {
115 			TRACE("sclp request failed: flags=%x\n",
116 			      sccb.evbuf.hdr.flags);
117 			continue;
118 		}
119 		/*
120 		 * for the sync interface the response is in the initial sccb
121 		 */
122 		if (!sclp_sdias_register.receiver_fn) {
123 			memcpy(&sdias_evbuf, &sccb.evbuf, sizeof(sdias_evbuf));
124 			TRACE("sync request done\n");
125 			return 0;
126 		}
127 		/* otherwise we wait for completion */
128 		wait_for_completion(&evbuf_done);
129 		TRACE("request done\n");
130 		return 0;
131 	}
132 	return -EIO;
133 }
134 
135 /*
136  * Get number of blocks (4K) available in the HSA
137  */
sclp_sdias_blk_count(void)138 int sclp_sdias_blk_count(void)
139 {
140 	struct sclp_req request;
141 	int rc;
142 
143 	mutex_lock(&sdias_mutex);
144 
145 	memset(&sccb, 0, sizeof(sccb));
146 	memset(&request, 0, sizeof(request));
147 
148 	sccb.hdr.length = sizeof(sccb);
149 	sccb.evbuf.hdr.length = sizeof(struct sdias_evbuf);
150 	sccb.evbuf.hdr.type = EVTYP_SDIAS;
151 	sccb.evbuf.event_qual = EQ_SIZE;
152 	sccb.evbuf.data_id = DI_FCP_DUMP;
153 	sccb.evbuf.event_id = 4712;
154 	sccb.evbuf.dbs = 1;
155 
156 	request.sccb = &sccb;
157 	request.command = SCLP_CMDW_WRITE_EVENT_DATA;
158 	request.status = SCLP_REQ_FILLED;
159 	request.callback = sdias_callback;
160 
161 	rc = sdias_sclp_send(&request);
162 	if (rc) {
163 		pr_err("sclp_send failed for get_nr_blocks\n");
164 		goto out;
165 	}
166 	if (sccb.hdr.response_code != 0x0020) {
167 		TRACE("send failed: %x\n", sccb.hdr.response_code);
168 		rc = -EIO;
169 		goto out;
170 	}
171 
172 	switch (sdias_evbuf.event_status) {
173 		case 0:
174 			rc = sdias_evbuf.blk_cnt;
175 			break;
176 		default:
177 			pr_err("SCLP error: %x\n", sdias_evbuf.event_status);
178 			rc = -EIO;
179 			goto out;
180 	}
181 	TRACE("%i blocks\n", rc);
182 out:
183 	mutex_unlock(&sdias_mutex);
184 	return rc;
185 }
186 
187 /*
188  * Copy from HSA to absolute storage (not reentrant):
189  *
190  * @dest     : Address of buffer where data should be copied
191  * @start_blk: Start Block (beginning with 1)
192  * @nr_blks  : Number of 4K blocks to copy
193  *
194  * Return Value: 0 : Requested 'number' of blocks of data copied
195  *		 <0: ERROR - negative event status
196  */
sclp_sdias_copy(void * dest,int start_blk,int nr_blks)197 int sclp_sdias_copy(void *dest, int start_blk, int nr_blks)
198 {
199 	struct sclp_req request;
200 	int rc;
201 
202 	mutex_lock(&sdias_mutex);
203 
204 	memset(&sccb, 0, sizeof(sccb));
205 	memset(&request, 0, sizeof(request));
206 
207 	sccb.hdr.length = sizeof(sccb);
208 	sccb.evbuf.hdr.length = sizeof(struct sdias_evbuf);
209 	sccb.evbuf.hdr.type = EVTYP_SDIAS;
210 	sccb.evbuf.hdr.flags = 0;
211 	sccb.evbuf.event_qual = EQ_STORE_DATA;
212 	sccb.evbuf.data_id = DI_FCP_DUMP;
213 	sccb.evbuf.event_id = 4712;
214 #ifdef __s390x__
215 	sccb.evbuf.asa_size = ASA_SIZE_64;
216 #else
217 	sccb.evbuf.asa_size = ASA_SIZE_32;
218 #endif
219 	sccb.evbuf.event_status = 0;
220 	sccb.evbuf.blk_cnt = nr_blks;
221 	sccb.evbuf.asa = (unsigned long)dest;
222 	sccb.evbuf.fbn = start_blk;
223 	sccb.evbuf.lbn = 0;
224 	sccb.evbuf.dbs = 1;
225 
226 	request.sccb	 = &sccb;
227 	request.command  = SCLP_CMDW_WRITE_EVENT_DATA;
228 	request.status	 = SCLP_REQ_FILLED;
229 	request.callback = sdias_callback;
230 
231 	rc = sdias_sclp_send(&request);
232 	if (rc) {
233 		pr_err("sclp_send failed: %x\n", rc);
234 		goto out;
235 	}
236 	if (sccb.hdr.response_code != 0x0020) {
237 		TRACE("copy failed: %x\n", sccb.hdr.response_code);
238 		rc = -EIO;
239 		goto out;
240 	}
241 
242 	switch (sdias_evbuf.event_status) {
243 		case EVSTATE_ALL_STORED:
244 			TRACE("all stored\n");
245 		case EVSTATE_PART_STORED:
246 			TRACE("part stored: %i\n", sdias_evbuf.blk_cnt);
247 			break;
248 		case EVSTATE_NO_DATA:
249 			TRACE("no data\n");
250 		default:
251 			pr_err("Error from SCLP while copying hsa. "
252 			       "Event status = %x\n",
253 			       sdias_evbuf.event_status);
254 			rc = -EIO;
255 	}
256 out:
257 	mutex_unlock(&sdias_mutex);
258 	return rc;
259 }
260 
sclp_sdias_register_check(void)261 static int __init sclp_sdias_register_check(void)
262 {
263 	int rc;
264 
265 	rc = sclp_register(&sclp_sdias_register);
266 	if (rc)
267 		return rc;
268 	if (sclp_sdias_blk_count() == 0) {
269 		sclp_unregister(&sclp_sdias_register);
270 		return -ENODEV;
271 	}
272 	return 0;
273 }
274 
sclp_sdias_init_sync(void)275 static int __init sclp_sdias_init_sync(void)
276 {
277 	TRACE("Try synchronous mode\n");
278 	sclp_sdias_register.receive_mask = 0;
279 	sclp_sdias_register.receiver_fn = NULL;
280 	return sclp_sdias_register_check();
281 }
282 
sclp_sdias_init_async(void)283 static int __init sclp_sdias_init_async(void)
284 {
285 	TRACE("Try asynchronous mode\n");
286 	sclp_sdias_register.receive_mask = EVTYP_SDIAS_MASK;
287 	sclp_sdias_register.receiver_fn = sclp_sdias_receiver_fn;
288 	return sclp_sdias_register_check();
289 }
290 
sclp_sdias_init(void)291 int __init sclp_sdias_init(void)
292 {
293 	if (ipl_info.type != IPL_TYPE_FCP_DUMP)
294 		return 0;
295 	sdias_dbf = debug_register("dump_sdias", 4, 1, 4 * sizeof(long));
296 	debug_register_view(sdias_dbf, &debug_sprintf_view);
297 	debug_set_level(sdias_dbf, 6);
298 	if (sclp_sdias_init_sync() == 0)
299 		goto out;
300 	if (sclp_sdias_init_async() == 0)
301 		goto out;
302 	TRACE("init failed\n");
303 	return -ENODEV;
304 out:
305 	TRACE("init done\n");
306 	return 0;
307 }
308 
sclp_sdias_exit(void)309 void __exit sclp_sdias_exit(void)
310 {
311 	debug_unregister(sdias_dbf);
312 	sclp_unregister(&sclp_sdias_register);
313 }
314