Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/libata-dev.git] / drivers / s390 / char / sclp_sdias.c
blobb1032931a1c41237766eaf6c38d7e5032e71e33d
1 /*
2 * Sclp "store data in absolut storage"
4 * Copyright IBM Corp. 2003, 2007
5 * Author(s): Michael Holzheu
6 */
8 #define KMSG_COMPONENT "sclp_sdias"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
17 #include "sclp.h"
18 #include "sclp_rw.h"
20 #define TRACE(x...) debug_sprintf_event(sdias_dbf, 1, x)
22 #define SDIAS_RETRIES 300
23 #define SDIAS_SLEEP_TICKS 50
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
34 static struct debug_info *sdias_dbf;
36 static struct sclp_register sclp_sdias_register = {
37 .send_mask = EVTYP_SDIAS_MASK,
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));
60 struct sdias_sccb {
61 struct sccb_header hdr;
62 struct sdias_evbuf evbuf;
63 } __attribute__((packed));
65 static struct sdias_sccb sccb __attribute__((aligned(4096)));
66 static struct sdias_evbuf sdias_evbuf;
68 static DECLARE_COMPLETION(evbuf_accepted);
69 static DECLARE_COMPLETION(evbuf_done);
70 static DEFINE_MUTEX(sdias_mutex);
73 * Called by SCLP base when read event data has been completed (async mode only)
75 static void sclp_sdias_receiver_fn(struct evbuf_header *evbuf)
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");
84 * Called by SCLP base when sdias event has been accepted
86 static void sdias_callback(struct sclp_req *request, void *data)
88 complete(&evbuf_accepted);
89 TRACE("callback done\n");
92 static int sdias_sclp_send(struct sclp_req *req)
94 int retries;
95 int rc;
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;
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;
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;
120 * for the sync interface the response is in the initial sccb
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;
127 /* otherwise we wait for completion */
128 wait_for_completion(&evbuf_done);
129 TRACE("request done\n");
130 return 0;
132 return -EIO;
136 * Get number of blocks (4K) available in the HSA
138 int sclp_sdias_blk_count(void)
140 struct sclp_req request;
141 int rc;
143 mutex_lock(&sdias_mutex);
145 memset(&sccb, 0, sizeof(sccb));
146 memset(&request, 0, sizeof(request));
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;
156 request.sccb = &sccb;
157 request.command = SCLP_CMDW_WRITE_EVENT_DATA;
158 request.status = SCLP_REQ_FILLED;
159 request.callback = sdias_callback;
161 rc = sdias_sclp_send(&request);
162 if (rc) {
163 pr_err("sclp_send failed for get_nr_blocks\n");
164 goto out;
166 if (sccb.hdr.response_code != 0x0020) {
167 TRACE("send failed: %x\n", sccb.hdr.response_code);
168 rc = -EIO;
169 goto out;
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;
181 TRACE("%i blocks\n", rc);
182 out:
183 mutex_unlock(&sdias_mutex);
184 return rc;
188 * Copy from HSA to absolute storage (not reentrant):
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
194 * Return Value: 0 : Requested 'number' of blocks of data copied
195 * <0: ERROR - negative event status
197 int sclp_sdias_copy(void *dest, int start_blk, int nr_blks)
199 struct sclp_req request;
200 int rc;
202 mutex_lock(&sdias_mutex);
204 memset(&sccb, 0, sizeof(sccb));
205 memset(&request, 0, sizeof(request));
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 CONFIG_64BIT
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;
226 request.sccb = &sccb;
227 request.command = SCLP_CMDW_WRITE_EVENT_DATA;
228 request.status = SCLP_REQ_FILLED;
229 request.callback = sdias_callback;
231 rc = sdias_sclp_send(&request);
232 if (rc) {
233 pr_err("sclp_send failed: %x\n", rc);
234 goto out;
236 if (sccb.hdr.response_code != 0x0020) {
237 TRACE("copy failed: %x\n", sccb.hdr.response_code);
238 rc = -EIO;
239 goto out;
242 switch (sdias_evbuf.event_status) {
243 case EVSTATE_ALL_STORED:
244 TRACE("all stored\n");
245 break;
246 case EVSTATE_PART_STORED:
247 TRACE("part stored: %i\n", sdias_evbuf.blk_cnt);
248 break;
249 case EVSTATE_NO_DATA:
250 TRACE("no data\n");
251 /* fall through */
252 default:
253 pr_err("Error from SCLP while copying hsa. "
254 "Event status = %x\n",
255 sdias_evbuf.event_status);
256 rc = -EIO;
258 out:
259 mutex_unlock(&sdias_mutex);
260 return rc;
263 static int __init sclp_sdias_register_check(void)
265 int rc;
267 rc = sclp_register(&sclp_sdias_register);
268 if (rc)
269 return rc;
270 if (sclp_sdias_blk_count() == 0) {
271 sclp_unregister(&sclp_sdias_register);
272 return -ENODEV;
274 return 0;
277 static int __init sclp_sdias_init_sync(void)
279 TRACE("Try synchronous mode\n");
280 sclp_sdias_register.receive_mask = 0;
281 sclp_sdias_register.receiver_fn = NULL;
282 return sclp_sdias_register_check();
285 static int __init sclp_sdias_init_async(void)
287 TRACE("Try asynchronous mode\n");
288 sclp_sdias_register.receive_mask = EVTYP_SDIAS_MASK;
289 sclp_sdias_register.receiver_fn = sclp_sdias_receiver_fn;
290 return sclp_sdias_register_check();
293 int __init sclp_sdias_init(void)
295 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
296 return 0;
297 sdias_dbf = debug_register("dump_sdias", 4, 1, 4 * sizeof(long));
298 debug_register_view(sdias_dbf, &debug_sprintf_view);
299 debug_set_level(sdias_dbf, 6);
300 if (sclp_sdias_init_sync() == 0)
301 goto out;
302 if (sclp_sdias_init_async() == 0)
303 goto out;
304 TRACE("init failed\n");
305 return -ENODEV;
306 out:
307 TRACE("init done\n");
308 return 0;
311 void __exit sclp_sdias_exit(void)
313 debug_unregister(sdias_dbf);
314 sclp_unregister(&sclp_sdias_register);