]> Pileus Git - ~andy/linux/blob - drivers/s390/cio/ccwreq.c
[S390] cio: Log the response from the unit check handler
[~andy/linux] / drivers / s390 / cio / ccwreq.c
1 /*
2  *  Handling of internal CCW device requests.
3  *
4  *    Copyright IBM Corp. 2009
5  *    Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
6  */
7
8 #include <linux/types.h>
9 #include <linux/err.h>
10 #include <asm/ccwdev.h>
11 #include <asm/cio.h>
12
13 #include "io_sch.h"
14 #include "cio.h"
15 #include "device.h"
16 #include "cio_debug.h"
17
18 /**
19  * lpm_adjust - adjust path mask
20  * @lpm: path mask to adjust
21  * @mask: mask of available paths
22  *
23  * Shift @lpm right until @lpm and @mask have at least one bit in common or
24  * until @lpm is zero. Return the resulting lpm.
25  */
26 int lpm_adjust(int lpm, int mask)
27 {
28         while (lpm && ((lpm & mask) == 0))
29                 lpm >>= 1;
30         return lpm;
31 }
32
33 /*
34  * Adjust path mask to use next path and reset retry count. Return resulting
35  * path mask.
36  */
37 static u16 ccwreq_next_path(struct ccw_device *cdev)
38 {
39         struct ccw_request *req = &cdev->private->req;
40
41         req->retries    = req->maxretries;
42         req->mask       = lpm_adjust(req->mask >>= 1, req->lpm);
43
44         return req->mask;
45 }
46
47 /*
48  * Clean up device state and report to callback.
49  */
50 static void ccwreq_stop(struct ccw_device *cdev, int rc)
51 {
52         struct ccw_request *req = &cdev->private->req;
53
54         if (req->done)
55                 return;
56         req->done = 1;
57         ccw_device_set_timeout(cdev, 0);
58         memset(&cdev->private->irb, 0, sizeof(struct irb));
59         if (rc && rc != -ENODEV && req->drc)
60                 rc = req->drc;
61         req->callback(cdev, req->data, rc);
62 }
63
64 /*
65  * (Re-)Start the operation until retries and paths are exhausted.
66  */
67 static void ccwreq_do(struct ccw_device *cdev)
68 {
69         struct ccw_request *req = &cdev->private->req;
70         struct subchannel *sch = to_subchannel(cdev->dev.parent);
71         struct ccw1 *cp = req->cp;
72         int rc = -EACCES;
73
74         while (req->mask) {
75                 if (req->retries-- == 0) {
76                         /* Retries exhausted, try next path. */
77                         ccwreq_next_path(cdev);
78                         continue;
79                 }
80                 /* Perform start function. */
81                 memset(&cdev->private->irb, 0, sizeof(struct irb));
82                 rc = cio_start(sch, cp, (u8) req->mask);
83                 if (rc == 0) {
84                         /* I/O started successfully. */
85                         ccw_device_set_timeout(cdev, req->timeout);
86                         return;
87                 }
88                 if (rc == -ENODEV) {
89                         /* Permanent device error. */
90                         break;
91                 }
92                 if (rc == -EACCES) {
93                         /* Permant path error. */
94                         ccwreq_next_path(cdev);
95                         continue;
96                 }
97                 /* Temporary improper status. */
98                 rc = cio_clear(sch);
99                 if (rc)
100                         break;
101                 return;
102         }
103         ccwreq_stop(cdev, rc);
104 }
105
106 /**
107  * ccw_request_start - perform I/O request
108  * @cdev: ccw device
109  *
110  * Perform the I/O request specified by cdev->req.
111  */
112 void ccw_request_start(struct ccw_device *cdev)
113 {
114         struct ccw_request *req = &cdev->private->req;
115
116         /* Try all paths twice to counter link flapping. */
117         req->mask       = 0x8080;
118         req->retries    = req->maxretries;
119         req->mask       = lpm_adjust(req->mask, req->lpm);
120         req->drc        = 0;
121         req->done       = 0;
122         req->cancel     = 0;
123         if (!req->mask)
124                 goto out_nopath;
125         ccwreq_do(cdev);
126         return;
127
128 out_nopath:
129         ccwreq_stop(cdev, -EACCES);
130 }
131
132 /**
133  * ccw_request_cancel - cancel running I/O request
134  * @cdev: ccw device
135  *
136  * Cancel the I/O request specified by cdev->req. Return non-zero if request
137  * has already finished, zero otherwise.
138  */
139 int ccw_request_cancel(struct ccw_device *cdev)
140 {
141         struct subchannel *sch = to_subchannel(cdev->dev.parent);
142         struct ccw_request *req = &cdev->private->req;
143         int rc;
144
145         if (req->done)
146                 return 1;
147         req->cancel = 1;
148         rc = cio_clear(sch);
149         if (rc)
150                 ccwreq_stop(cdev, rc);
151         return 0;
152 }
153
154 /*
155  * Return the status of the internal I/O started on the specified ccw device.
156  * Perform BASIC SENSE if required.
157  */
158 static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
159 {
160         struct irb *irb = &cdev->private->irb;
161         struct cmd_scsw *scsw = &irb->scsw.cmd;
162         enum uc_todo todo;
163
164         /* Perform BASIC SENSE if needed. */
165         if (ccw_device_accumulate_and_sense(cdev, lcirb))
166                 return IO_RUNNING;
167         /* Check for halt/clear interrupt. */
168         if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
169                 return IO_KILLED;
170         /* Check for path error. */
171         if (scsw->cc == 3 || scsw->pno)
172                 return IO_PATH_ERROR;
173         /* Handle BASIC SENSE data. */
174         if (irb->esw.esw0.erw.cons) {
175                 CIO_TRACE_EVENT(2, "sensedata");
176                 CIO_HEX_EVENT(2, &cdev->private->dev_id,
177                               sizeof(struct ccw_dev_id));
178                 CIO_HEX_EVENT(2, &cdev->private->irb.ecw, SENSE_MAX_COUNT);
179                 /* Check for command reject. */
180                 if (irb->ecw[0] & SNS0_CMD_REJECT)
181                         return IO_REJECTED;
182                 /* Ask the driver what to do */
183                 if (cdev->drv && cdev->drv->uc_handler) {
184                         todo = cdev->drv->uc_handler(cdev, lcirb);
185                         CIO_TRACE_EVENT(2, "uc_response");
186                         CIO_HEX_EVENT(2, &todo, sizeof(todo));
187                         switch (todo) {
188                         case UC_TODO_RETRY:
189                                 return IO_STATUS_ERROR;
190                         case UC_TODO_RETRY_ON_NEW_PATH:
191                                 return IO_PATH_ERROR;
192                         case UC_TODO_STOP:
193                                 return IO_REJECTED;
194                         default:
195                                 return IO_STATUS_ERROR;
196                         }
197                 }
198                 /* Assume that unexpected SENSE data implies an error. */
199                 return IO_STATUS_ERROR;
200         }
201         /* Check for channel errors. */
202         if (scsw->cstat != 0)
203                 return IO_STATUS_ERROR;
204         /* Check for device errors. */
205         if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
206                 return IO_STATUS_ERROR;
207         /* Check for final state. */
208         if (!(scsw->dstat & DEV_STAT_DEV_END))
209                 return IO_RUNNING;
210         /* Check for other improper status. */
211         if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
212                 return IO_STATUS_ERROR;
213         return IO_DONE;
214 }
215
216 /*
217  * Log ccw request status.
218  */
219 static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
220 {
221         struct ccw_request *req = &cdev->private->req;
222         struct {
223                 struct ccw_dev_id dev_id;
224                 u16 retries;
225                 u8 lpm;
226                 u8 status;
227         }  __attribute__ ((packed)) data;
228         data.dev_id     = cdev->private->dev_id;
229         data.retries    = req->retries;
230         data.lpm        = (u8) req->mask;
231         data.status     = (u8) status;
232         CIO_TRACE_EVENT(2, "reqstat");
233         CIO_HEX_EVENT(2, &data, sizeof(data));
234 }
235
236 /**
237  * ccw_request_handler - interrupt handler for I/O request procedure.
238  * @cdev: ccw device
239  *
240  * Handle interrupt during I/O request procedure.
241  */
242 void ccw_request_handler(struct ccw_device *cdev)
243 {
244         struct irb *irb = (struct irb *)&S390_lowcore.irb;
245         struct ccw_request *req = &cdev->private->req;
246         enum io_status status;
247         int rc = -EOPNOTSUPP;
248
249         /* Check status of I/O request. */
250         status = ccwreq_status(cdev, irb);
251         if (req->filter)
252                 status = req->filter(cdev, req->data, irb, status);
253         if (status != IO_RUNNING)
254                 ccw_device_set_timeout(cdev, 0);
255         if (status != IO_DONE && status != IO_RUNNING)
256                 ccwreq_log_status(cdev, status);
257         switch (status) {
258         case IO_DONE:
259                 break;
260         case IO_RUNNING:
261                 return;
262         case IO_REJECTED:
263                 goto err;
264         case IO_PATH_ERROR:
265                 goto out_next_path;
266         case IO_STATUS_ERROR:
267                 goto out_restart;
268         case IO_KILLED:
269                 /* Check if request was cancelled on purpose. */
270                 if (req->cancel) {
271                         rc = -EIO;
272                         goto err;
273                 }
274                 goto out_restart;
275         }
276         /* Check back with request initiator. */
277         if (!req->check)
278                 goto out;
279         switch (req->check(cdev, req->data)) {
280         case 0:
281                 break;
282         case -EAGAIN:
283                 goto out_restart;
284         case -EACCES:
285                 goto out_next_path;
286         default:
287                 goto err;
288         }
289 out:
290         ccwreq_stop(cdev, 0);
291         return;
292
293 out_next_path:
294         /* Try next path and restart I/O. */
295         if (!ccwreq_next_path(cdev)) {
296                 rc = -EACCES;
297                 goto err;
298         }
299 out_restart:
300         /* Restart. */
301         ccwreq_do(cdev);
302         return;
303 err:
304         ccwreq_stop(cdev, rc);
305 }
306
307
308 /**
309  * ccw_request_timeout - timeout handler for I/O request procedure
310  * @cdev: ccw device
311  *
312  * Handle timeout during I/O request procedure.
313  */
314 void ccw_request_timeout(struct ccw_device *cdev)
315 {
316         struct subchannel *sch = to_subchannel(cdev->dev.parent);
317         struct ccw_request *req = &cdev->private->req;
318         int rc;
319
320         if (!ccwreq_next_path(cdev)) {
321                 /* set the final return code for this request */
322                 req->drc = -ETIME;
323         }
324         rc = cio_clear(sch);
325         if (rc)
326                 goto err;
327         return;
328
329 err:
330         ccwreq_stop(cdev, rc);
331 }
332
333 /**
334  * ccw_request_notoper - notoper handler for I/O request procedure
335  * @cdev: ccw device
336  *
337  * Handle timeout during I/O request procedure.
338  */
339 void ccw_request_notoper(struct ccw_device *cdev)
340 {
341         ccwreq_stop(cdev, -ENODEV);
342 }