blob: 6a6f76bf6e3dad4fae79fedc125bb39b7678b79a [file] [log] [blame]
Michael Holzheu411ed322007-04-27 16:01:49 +02001/*
2 * Sclp "store data in absolut storage"
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2003, 2007
Michael Holzheu411ed322007-04-27 16:01:49 +02005 * Author(s): Michael Holzheu
6 */
7
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +01008#define KMSG_COMPONENT "sclp_sdias"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
Michael Holzheue3c652b2012-03-11 11:59:29 -040011#include <linux/completion.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020012#include <linux/sched.h>
13#include <asm/sclp.h>
14#include <asm/debug.h>
15#include <asm/ipl.h>
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +010016
Michael Holzheu411ed322007-04-27 16:01:49 +020017#include "sclp.h"
18#include "sclp_rw.h"
19
20#define TRACE(x...) debug_sprintf_event(sdias_dbf, 1, x)
Michael Holzheu411ed322007-04-27 16:01:49 +020021
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
34static struct debug_info *sdias_dbf;
35
36static struct sclp_register sclp_sdias_register = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +020037 .send_mask = EVTYP_SDIAS_MASK,
Michael Holzheu411ed322007-04-27 16:01:49 +020038};
39
40struct 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
60struct sdias_sccb {
61 struct sccb_header hdr;
62 struct sdias_evbuf evbuf;
63} __attribute__((packed));
64
65static struct sdias_sccb sccb __attribute__((aligned(4096)));
Michael Holzheue3c652b2012-03-11 11:59:29 -040066static struct sdias_evbuf sdias_evbuf;
Michael Holzheu411ed322007-04-27 16:01:49 +020067
Michael Holzheue3c652b2012-03-11 11:59:29 -040068static DECLARE_COMPLETION(evbuf_accepted);
69static DECLARE_COMPLETION(evbuf_done);
Michael Holzheu411ed322007-04-27 16:01:49 +020070static DEFINE_MUTEX(sdias_mutex);
71
Michael Holzheue3c652b2012-03-11 11:59:29 -040072/*
73 * Called by SCLP base when read event data has been completed (async mode only)
74 */
75static 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 */
Michael Holzheu411ed322007-04-27 16:01:49 +020086static void sdias_callback(struct sclp_req *request, void *data)
87{
Michael Holzheue3c652b2012-03-11 11:59:29 -040088 complete(&evbuf_accepted);
Michael Holzheu411ed322007-04-27 16:01:49 +020089 TRACE("callback done\n");
90}
91
92static int sdias_sclp_send(struct sclp_req *req)
93{
94 int retries;
95 int rc;
96
97 for (retries = SDIAS_RETRIES; retries; retries--) {
Michael Holzheu411ed322007-04-27 16:01:49 +020098 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 */
Michael Holzheue3c652b2012-03-11 11:59:29 -0400108 wait_for_completion(&evbuf_accepted);
Michael Holzheu411ed322007-04-27 16:01:49 +0200109 if (req->status == SCLP_REQ_FAILED) {
110 TRACE("sclp request failed\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200111 continue;
112 }
Michael Holzheue3c652b2012-03-11 11:59:29 -0400113 /* 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);
Michael Holzheu411ed322007-04-27 16:01:49 +0200129 TRACE("request done\n");
Michael Holzheue3c652b2012-03-11 11:59:29 -0400130 return 0;
Michael Holzheu411ed322007-04-27 16:01:49 +0200131 }
Michael Holzheue3c652b2012-03-11 11:59:29 -0400132 return -EIO;
Michael Holzheu411ed322007-04-27 16:01:49 +0200133}
134
135/*
136 * Get number of blocks (4K) available in the HSA
137 */
138int 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);
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200150 sccb.evbuf.hdr.type = EVTYP_SDIAS;
Michael Holzheu411ed322007-04-27 16:01:49 +0200151 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) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100163 pr_err("sclp_send failed for get_nr_blocks\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200164 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
Michael Holzheue3c652b2012-03-11 11:59:29 -0400172 switch (sdias_evbuf.event_status) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200173 case 0:
Michael Holzheue3c652b2012-03-11 11:59:29 -0400174 rc = sdias_evbuf.blk_cnt;
Michael Holzheu411ed322007-04-27 16:01:49 +0200175 break;
176 default:
Michael Holzheue3c652b2012-03-11 11:59:29 -0400177 pr_err("SCLP error: %x\n", sdias_evbuf.event_status);
Michael Holzheu411ed322007-04-27 16:01:49 +0200178 rc = -EIO;
179 goto out;
180 }
181 TRACE("%i blocks\n", rc);
182out:
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 */
197int 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);
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200209 sccb.evbuf.hdr.type = EVTYP_SDIAS;
Michael Holzheu411ed322007-04-27 16:01:49 +0200210 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;
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200214#ifdef CONFIG_64BIT
Michael Holzheu411ed322007-04-27 16:01:49 +0200215 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) {
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100233 pr_err("sclp_send failed: %x\n", rc);
Michael Holzheu411ed322007-04-27 16:01:49 +0200234 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
Michael Holzheue3c652b2012-03-11 11:59:29 -0400242 switch (sdias_evbuf.event_status) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200243 case EVSTATE_ALL_STORED:
244 TRACE("all stored\n");
245 case EVSTATE_PART_STORED:
Michael Holzheue3c652b2012-03-11 11:59:29 -0400246 TRACE("part stored: %i\n", sdias_evbuf.blk_cnt);
Michael Holzheu411ed322007-04-27 16:01:49 +0200247 break;
248 case EVSTATE_NO_DATA:
249 TRACE("no data\n");
250 default:
Martin Schwidefskyb3ff0882008-12-25 13:39:48 +0100251 pr_err("Error from SCLP while copying hsa. "
252 "Event status = %x\n",
Michael Holzheue3c652b2012-03-11 11:59:29 -0400253 sdias_evbuf.event_status);
Michael Holzheu411ed322007-04-27 16:01:49 +0200254 rc = -EIO;
255 }
256out:
257 mutex_unlock(&sdias_mutex);
258 return rc;
259}
260
Michael Holzheue3c652b2012-03-11 11:59:29 -0400261static int __init sclp_sdias_register_check(void)
Michael Holzheu411ed322007-04-27 16:01:49 +0200262{
263 int rc;
264
Michael Holzheue3c652b2012-03-11 11:59:29 -0400265 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
275static 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
283static 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
291int __init sclp_sdias_init(void)
292{
Michael Holzheu411ed322007-04-27 16:01:49 +0200293 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);
Michael Holzheue3c652b2012-03-11 11:59:29 -0400298 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;
304out:
Michael Holzheu411ed322007-04-27 16:01:49 +0200305 TRACE("init done\n");
306 return 0;
307}
308
Heiko Carstens763968e2007-05-10 15:45:46 +0200309void __exit sclp_sdias_exit(void)
Michael Holzheu411ed322007-04-27 16:01:49 +0200310{
311 debug_unregister(sdias_dbf);
312 sclp_unregister(&sclp_sdias_register);
313}