Linux 4.19.133
[linux/fpc-iii.git] / drivers / s390 / cio / ccwreq.c
blob603268a33ea1a0f7a4b0d6963b40c7f90536a179
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Handling of internal CCW device requests.
5 * Copyright IBM Corp. 2009, 2011
6 * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
7 */
9 #define KMSG_COMPONENT "cio"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/types.h>
13 #include <linux/err.h>
14 #include <asm/ccwdev.h>
15 #include <asm/cio.h>
17 #include "io_sch.h"
18 #include "cio.h"
19 #include "device.h"
20 #include "cio_debug.h"
22 /**
23 * lpm_adjust - adjust path mask
24 * @lpm: path mask to adjust
25 * @mask: mask of available paths
27 * Shift @lpm right until @lpm and @mask have at least one bit in common or
28 * until @lpm is zero. Return the resulting lpm.
30 int lpm_adjust(int lpm, int mask)
32 while (lpm && ((lpm & mask) == 0))
33 lpm >>= 1;
34 return lpm;
38 * Adjust path mask to use next path and reset retry count. Return resulting
39 * path mask.
41 static u16 ccwreq_next_path(struct ccw_device *cdev)
43 struct ccw_request *req = &cdev->private->req;
45 if (!req->singlepath) {
46 req->mask = 0;
47 goto out;
49 req->retries = req->maxretries;
50 req->mask = lpm_adjust(req->mask >> 1, req->lpm);
51 out:
52 return req->mask;
56 * Clean up device state and report to callback.
58 static void ccwreq_stop(struct ccw_device *cdev, int rc)
60 struct ccw_request *req = &cdev->private->req;
62 if (req->done)
63 return;
64 req->done = 1;
65 ccw_device_set_timeout(cdev, 0);
66 memset(&cdev->private->irb, 0, sizeof(struct irb));
67 if (rc && rc != -ENODEV && req->drc)
68 rc = req->drc;
69 req->callback(cdev, req->data, rc);
73 * (Re-)Start the operation until retries and paths are exhausted.
75 static void ccwreq_do(struct ccw_device *cdev)
77 struct ccw_request *req = &cdev->private->req;
78 struct subchannel *sch = to_subchannel(cdev->dev.parent);
79 struct ccw1 *cp = req->cp;
80 int rc = -EACCES;
82 while (req->mask) {
83 if (req->retries-- == 0) {
84 /* Retries exhausted, try next path. */
85 ccwreq_next_path(cdev);
86 continue;
88 /* Perform start function. */
89 memset(&cdev->private->irb, 0, sizeof(struct irb));
90 rc = cio_start(sch, cp, (u8) req->mask);
91 if (rc == 0) {
92 /* I/O started successfully. */
93 ccw_device_set_timeout(cdev, req->timeout);
94 return;
96 if (rc == -ENODEV) {
97 /* Permanent device error. */
98 break;
100 if (rc == -EACCES) {
101 /* Permant path error. */
102 ccwreq_next_path(cdev);
103 continue;
105 /* Temporary improper status. */
106 rc = cio_clear(sch);
107 if (rc)
108 break;
109 return;
111 ccwreq_stop(cdev, rc);
115 * ccw_request_start - perform I/O request
116 * @cdev: ccw device
118 * Perform the I/O request specified by cdev->req.
120 void ccw_request_start(struct ccw_device *cdev)
122 struct ccw_request *req = &cdev->private->req;
124 if (req->singlepath) {
125 /* Try all paths twice to counter link flapping. */
126 req->mask = 0x8080;
127 } else
128 req->mask = req->lpm;
130 req->retries = req->maxretries;
131 req->mask = lpm_adjust(req->mask, req->lpm);
132 req->drc = 0;
133 req->done = 0;
134 req->cancel = 0;
135 if (!req->mask)
136 goto out_nopath;
137 ccwreq_do(cdev);
138 return;
140 out_nopath:
141 ccwreq_stop(cdev, -EACCES);
145 * ccw_request_cancel - cancel running I/O request
146 * @cdev: ccw device
148 * Cancel the I/O request specified by cdev->req. Return non-zero if request
149 * has already finished, zero otherwise.
151 int ccw_request_cancel(struct ccw_device *cdev)
153 struct subchannel *sch = to_subchannel(cdev->dev.parent);
154 struct ccw_request *req = &cdev->private->req;
155 int rc;
157 if (req->done)
158 return 1;
159 req->cancel = 1;
160 rc = cio_clear(sch);
161 if (rc)
162 ccwreq_stop(cdev, rc);
163 return 0;
167 * Return the status of the internal I/O started on the specified ccw device.
168 * Perform BASIC SENSE if required.
170 static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
172 struct irb *irb = &cdev->private->irb;
173 struct cmd_scsw *scsw = &irb->scsw.cmd;
174 enum uc_todo todo;
176 /* Perform BASIC SENSE if needed. */
177 if (ccw_device_accumulate_and_sense(cdev, lcirb))
178 return IO_RUNNING;
179 /* Check for halt/clear interrupt. */
180 if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
181 return IO_KILLED;
182 /* Check for path error. */
183 if (scsw->cc == 3 || scsw->pno)
184 return IO_PATH_ERROR;
185 /* Handle BASIC SENSE data. */
186 if (irb->esw.esw0.erw.cons) {
187 CIO_TRACE_EVENT(2, "sensedata");
188 CIO_HEX_EVENT(2, &cdev->private->dev_id,
189 sizeof(struct ccw_dev_id));
190 CIO_HEX_EVENT(2, &cdev->private->irb.ecw, SENSE_MAX_COUNT);
191 /* Check for command reject. */
192 if (irb->ecw[0] & SNS0_CMD_REJECT)
193 return IO_REJECTED;
194 /* Ask the driver what to do */
195 if (cdev->drv && cdev->drv->uc_handler) {
196 todo = cdev->drv->uc_handler(cdev, lcirb);
197 CIO_TRACE_EVENT(2, "uc_response");
198 CIO_HEX_EVENT(2, &todo, sizeof(todo));
199 switch (todo) {
200 case UC_TODO_RETRY:
201 return IO_STATUS_ERROR;
202 case UC_TODO_RETRY_ON_NEW_PATH:
203 return IO_PATH_ERROR;
204 case UC_TODO_STOP:
205 return IO_REJECTED;
206 default:
207 return IO_STATUS_ERROR;
210 /* Assume that unexpected SENSE data implies an error. */
211 return IO_STATUS_ERROR;
213 /* Check for channel errors. */
214 if (scsw->cstat != 0)
215 return IO_STATUS_ERROR;
216 /* Check for device errors. */
217 if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
218 return IO_STATUS_ERROR;
219 /* Check for final state. */
220 if (!(scsw->dstat & DEV_STAT_DEV_END))
221 return IO_RUNNING;
222 /* Check for other improper status. */
223 if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
224 return IO_STATUS_ERROR;
225 return IO_DONE;
229 * Log ccw request status.
231 static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
233 struct ccw_request *req = &cdev->private->req;
234 struct {
235 struct ccw_dev_id dev_id;
236 u16 retries;
237 u8 lpm;
238 u8 status;
239 } __attribute__ ((packed)) data;
240 data.dev_id = cdev->private->dev_id;
241 data.retries = req->retries;
242 data.lpm = (u8) req->mask;
243 data.status = (u8) status;
244 CIO_TRACE_EVENT(2, "reqstat");
245 CIO_HEX_EVENT(2, &data, sizeof(data));
249 * ccw_request_handler - interrupt handler for I/O request procedure.
250 * @cdev: ccw device
252 * Handle interrupt during I/O request procedure.
254 void ccw_request_handler(struct ccw_device *cdev)
256 struct irb *irb = this_cpu_ptr(&cio_irb);
257 struct ccw_request *req = &cdev->private->req;
258 enum io_status status;
259 int rc = -EOPNOTSUPP;
261 /* Check status of I/O request. */
262 status = ccwreq_status(cdev, irb);
263 if (req->filter)
264 status = req->filter(cdev, req->data, irb, status);
265 if (status != IO_RUNNING)
266 ccw_device_set_timeout(cdev, 0);
267 if (status != IO_DONE && status != IO_RUNNING)
268 ccwreq_log_status(cdev, status);
269 switch (status) {
270 case IO_DONE:
271 break;
272 case IO_RUNNING:
273 return;
274 case IO_REJECTED:
275 goto err;
276 case IO_PATH_ERROR:
277 goto out_next_path;
278 case IO_STATUS_ERROR:
279 goto out_restart;
280 case IO_KILLED:
281 /* Check if request was cancelled on purpose. */
282 if (req->cancel) {
283 rc = -EIO;
284 goto err;
286 goto out_restart;
288 /* Check back with request initiator. */
289 if (!req->check)
290 goto out;
291 switch (req->check(cdev, req->data)) {
292 case 0:
293 break;
294 case -EAGAIN:
295 goto out_restart;
296 case -EACCES:
297 goto out_next_path;
298 default:
299 goto err;
301 out:
302 ccwreq_stop(cdev, 0);
303 return;
305 out_next_path:
306 /* Try next path and restart I/O. */
307 if (!ccwreq_next_path(cdev)) {
308 rc = -EACCES;
309 goto err;
311 out_restart:
312 /* Restart. */
313 ccwreq_do(cdev);
314 return;
315 err:
316 ccwreq_stop(cdev, rc);
321 * ccw_request_timeout - timeout handler for I/O request procedure
322 * @cdev: ccw device
324 * Handle timeout during I/O request procedure.
326 void ccw_request_timeout(struct ccw_device *cdev)
328 struct subchannel *sch = to_subchannel(cdev->dev.parent);
329 struct ccw_request *req = &cdev->private->req;
330 int rc = -ENODEV, chp;
332 if (cio_update_schib(sch))
333 goto err;
335 for (chp = 0; chp < 8; chp++) {
336 if ((0x80 >> chp) & sch->schib.pmcw.lpum)
337 pr_warn("%s: No interrupt was received within %lus (CS=%02x, DS=%02x, CHPID=%x.%02x)\n",
338 dev_name(&cdev->dev), req->timeout / HZ,
339 scsw_cstat(&sch->schib.scsw),
340 scsw_dstat(&sch->schib.scsw),
341 sch->schid.cssid,
342 sch->schib.pmcw.chpid[chp]);
345 if (!ccwreq_next_path(cdev)) {
346 /* set the final return code for this request */
347 req->drc = -ETIME;
349 rc = cio_clear(sch);
350 if (rc)
351 goto err;
352 return;
354 err:
355 ccwreq_stop(cdev, rc);
359 * ccw_request_notoper - notoper handler for I/O request procedure
360 * @cdev: ccw device
362 * Handle notoper during I/O request procedure.
364 void ccw_request_notoper(struct ccw_device *cdev)
366 ccwreq_stop(cdev, -ENODEV);