1 // SPDX-License-Identifier: GPL-2.0
3 * Handling of internal CCW device requests.
5 * Copyright IBM Corp. 2009, 2011
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>
20 #include "cio_debug.h"
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))
38 * Adjust path mask to use next path and reset retry count. Return resulting
41 static u16 ccwreq_next_path(struct ccw_device *cdev)
43 struct ccw_request *req = &cdev->private->req;
45 if (!req->singlepath) {
49 req->retries = req->maxretries;
50 req->mask = lpm_adjust(req->mask >> 1, req->lpm);
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;
65 ccw_device_set_timeout(cdev, 0);
66 memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
67 if (rc && rc != -ENODEV && 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;
83 if (req->retries-- == 0) {
84 /* Retries exhausted, try next path. */
85 ccwreq_next_path(cdev);
88 /* Perform start function. */
89 memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
90 rc = cio_start(sch, cp, (u8) req->mask);
92 /* I/O started successfully. */
93 ccw_device_set_timeout(cdev, req->timeout);
97 /* Permanent device error. */
101 /* Permant path error. */
102 ccwreq_next_path(cdev);
105 /* Temporary improper status. */
111 ccwreq_stop(cdev, rc);
115 * ccw_request_start - perform I/O request
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. */
128 req->mask = req->lpm;
130 req->retries = req->maxretries;
131 req->mask = lpm_adjust(req->mask, req->lpm);
141 ccwreq_stop(cdev, -EACCES);
145 * ccw_request_cancel - cancel running I/O request
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;
162 ccwreq_stop(cdev, rc);
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->dma_area->irb;
173 struct cmd_scsw *scsw = &irb->scsw.cmd;
176 /* Perform BASIC SENSE if needed. */
177 if (ccw_device_accumulate_and_sense(cdev, lcirb))
179 /* Check for halt/clear interrupt. */
180 if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
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->dma_area->irb.ecw,
192 /* Check for command reject. */
193 if (irb->ecw[0] & SNS0_CMD_REJECT)
195 /* Ask the driver what to do */
196 if (cdev->drv && cdev->drv->uc_handler) {
197 todo = cdev->drv->uc_handler(cdev, lcirb);
198 CIO_TRACE_EVENT(2, "uc_response");
199 CIO_HEX_EVENT(2, &todo, sizeof(todo));
202 return IO_STATUS_ERROR;
203 case UC_TODO_RETRY_ON_NEW_PATH:
204 return IO_PATH_ERROR;
208 return IO_STATUS_ERROR;
211 /* Assume that unexpected SENSE data implies an error. */
212 return IO_STATUS_ERROR;
214 /* Check for channel errors. */
215 if (scsw->cstat != 0)
216 return IO_STATUS_ERROR;
217 /* Check for device errors. */
218 if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
219 return IO_STATUS_ERROR;
220 /* Check for final state. */
221 if (!(scsw->dstat & DEV_STAT_DEV_END))
223 /* Check for other improper status. */
224 if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
225 return IO_STATUS_ERROR;
230 * Log ccw request status.
232 static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
234 struct ccw_request *req = &cdev->private->req;
236 struct ccw_dev_id dev_id;
240 } __attribute__ ((packed)) data;
241 data.dev_id = cdev->private->dev_id;
242 data.retries = req->retries;
243 data.lpm = (u8) req->mask;
244 data.status = (u8) status;
245 CIO_TRACE_EVENT(2, "reqstat");
246 CIO_HEX_EVENT(2, &data, sizeof(data));
250 * ccw_request_handler - interrupt handler for I/O request procedure.
253 * Handle interrupt during I/O request procedure.
255 void ccw_request_handler(struct ccw_device *cdev)
257 struct irb *irb = this_cpu_ptr(&cio_irb);
258 struct ccw_request *req = &cdev->private->req;
259 enum io_status status;
260 int rc = -EOPNOTSUPP;
262 /* Check status of I/O request. */
263 status = ccwreq_status(cdev, irb);
265 status = req->filter(cdev, req->data, irb, status);
266 if (status != IO_RUNNING)
267 ccw_device_set_timeout(cdev, 0);
268 if (status != IO_DONE && status != IO_RUNNING)
269 ccwreq_log_status(cdev, status);
279 case IO_STATUS_ERROR:
282 /* Check if request was cancelled on purpose. */
289 /* Check back with request initiator. */
292 switch (req->check(cdev, req->data)) {
303 ccwreq_stop(cdev, 0);
307 /* Try next path and restart I/O. */
308 if (!ccwreq_next_path(cdev)) {
317 ccwreq_stop(cdev, rc);
322 * ccw_request_timeout - timeout handler for I/O request procedure
325 * Handle timeout during I/O request procedure.
327 void ccw_request_timeout(struct ccw_device *cdev)
329 struct subchannel *sch = to_subchannel(cdev->dev.parent);
330 struct ccw_request *req = &cdev->private->req;
331 int rc = -ENODEV, chp;
333 if (cio_update_schib(sch))
336 for (chp = 0; chp < 8; chp++) {
337 if ((0x80 >> chp) & sch->schib.pmcw.lpum)
338 pr_warn("%s: No interrupt was received within %lus (CS=%02x, DS=%02x, CHPID=%x.%02x)\n",
339 dev_name(&cdev->dev), req->timeout / HZ,
340 scsw_cstat(&sch->schib.scsw),
341 scsw_dstat(&sch->schib.scsw),
343 sch->schib.pmcw.chpid[chp]);
346 if (!ccwreq_next_path(cdev)) {
347 /* set the final return code for this request */
356 ccwreq_stop(cdev, rc);
360 * ccw_request_notoper - notoper handler for I/O request procedure
363 * Handle notoper during I/O request procedure.
365 void ccw_request_notoper(struct ccw_device *cdev)
367 ccwreq_stop(cdev, -ENODEV);