2 * Handling of internal CCW device requests.
4 * Copyright IBM Corp. 2009
8 #include <linux/types.h>
10 #include <asm/ccwdev.h>
16 #include "cio_debug.h"
19 * lpm_adjust - adjust path mask
20 * @lpm: path mask to adjust
21 * @mask: mask of available paths
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.
26 int lpm_adjust(int lpm, int mask)
28 while (lpm && ((lpm & mask) == 0))
34 * Adjust path mask to use next path and reset retry count. Return resulting
37 static u16 ccwreq_next_path(struct ccw_device *cdev)
39 struct ccw_request *req = &cdev->private->req;
41 if (!req->singlepath) {
45 req->retries = req->maxretries;
46 req->mask = lpm_adjust(req->mask >>= 1, req->lpm);
52 * Clean up device state and report to callback.
54 static void ccwreq_stop(struct ccw_device *cdev, int rc)
56 struct ccw_request *req = &cdev->private->req;
61 ccw_device_set_timeout(cdev, 0);
62 memset(&cdev->private->irb, 0, sizeof(struct irb));
63 if (rc && rc != -ENODEV && req->drc)
65 req->callback(cdev, req->data, rc);
69 * (Re-)Start the operation until retries and paths are exhausted.
71 static void ccwreq_do(struct ccw_device *cdev)
73 struct ccw_request *req = &cdev->private->req;
74 struct subchannel *sch = to_subchannel(cdev->dev.parent);
75 struct ccw1 *cp = req->cp;
79 if (req->retries-- == 0) {
80 /* Retries exhausted, try next path. */
81 ccwreq_next_path(cdev);
84 /* Perform start function. */
85 memset(&cdev->private->irb, 0, sizeof(struct irb));
86 rc = cio_start(sch, cp, (u8) req->mask);
88 /* I/O started successfully. */
89 ccw_device_set_timeout(cdev, req->timeout);
93 /* Permanent device error. */
97 /* Permant path error. */
98 ccwreq_next_path(cdev);
101 /* Temporary improper status. */
107 ccwreq_stop(cdev, rc);
111 * ccw_request_start - perform I/O request
114 * Perform the I/O request specified by cdev->req.
116 void ccw_request_start(struct ccw_device *cdev)
118 struct ccw_request *req = &cdev->private->req;
120 if (req->singlepath) {
121 /* Try all paths twice to counter link flapping. */
124 req->mask = req->lpm;
126 req->retries = req->maxretries;
127 req->mask = lpm_adjust(req->mask, req->lpm);
137 ccwreq_stop(cdev, -EACCES);
141 * ccw_request_cancel - cancel running I/O request
144 * Cancel the I/O request specified by cdev->req. Return non-zero if request
145 * has already finished, zero otherwise.
147 int ccw_request_cancel(struct ccw_device *cdev)
149 struct subchannel *sch = to_subchannel(cdev->dev.parent);
150 struct ccw_request *req = &cdev->private->req;
158 ccwreq_stop(cdev, rc);
163 * Return the status of the internal I/O started on the specified ccw device.
164 * Perform BASIC SENSE if required.
166 static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
168 struct irb *irb = &cdev->private->irb;
169 struct cmd_scsw *scsw = &irb->scsw.cmd;
172 /* Perform BASIC SENSE if needed. */
173 if (ccw_device_accumulate_and_sense(cdev, lcirb))
175 /* Check for halt/clear interrupt. */
176 if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
178 /* Check for path error. */
179 if (scsw->cc == 3 || scsw->pno)
180 return IO_PATH_ERROR;
181 /* Handle BASIC SENSE data. */
182 if (irb->esw.esw0.erw.cons) {
183 CIO_TRACE_EVENT(2, "sensedata");
184 CIO_HEX_EVENT(2, &cdev->private->dev_id,
185 sizeof(struct ccw_dev_id));
186 CIO_HEX_EVENT(2, &cdev->private->irb.ecw, SENSE_MAX_COUNT);
187 /* Check for command reject. */
188 if (irb->ecw[0] & SNS0_CMD_REJECT)
190 /* Ask the driver what to do */
191 if (cdev->drv && cdev->drv->uc_handler) {
192 todo = cdev->drv->uc_handler(cdev, lcirb);
193 CIO_TRACE_EVENT(2, "uc_response");
194 CIO_HEX_EVENT(2, &todo, sizeof(todo));
197 return IO_STATUS_ERROR;
198 case UC_TODO_RETRY_ON_NEW_PATH:
199 return IO_PATH_ERROR;
203 return IO_STATUS_ERROR;
206 /* Assume that unexpected SENSE data implies an error. */
207 return IO_STATUS_ERROR;
209 /* Check for channel errors. */
210 if (scsw->cstat != 0)
211 return IO_STATUS_ERROR;
212 /* Check for device errors. */
213 if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
214 return IO_STATUS_ERROR;
215 /* Check for final state. */
216 if (!(scsw->dstat & DEV_STAT_DEV_END))
218 /* Check for other improper status. */
219 if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
220 return IO_STATUS_ERROR;
225 * Log ccw request status.
227 static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
229 struct ccw_request *req = &cdev->private->req;
231 struct ccw_dev_id dev_id;
235 } __attribute__ ((packed)) data;
236 data.dev_id = cdev->private->dev_id;
237 data.retries = req->retries;
238 data.lpm = (u8) req->mask;
239 data.status = (u8) status;
240 CIO_TRACE_EVENT(2, "reqstat");
241 CIO_HEX_EVENT(2, &data, sizeof(data));
245 * ccw_request_handler - interrupt handler for I/O request procedure.
248 * Handle interrupt during I/O request procedure.
250 void ccw_request_handler(struct ccw_device *cdev)
252 struct irb *irb = (struct irb *)&S390_lowcore.irb;
253 struct ccw_request *req = &cdev->private->req;
254 enum io_status status;
255 int rc = -EOPNOTSUPP;
257 /* Check status of I/O request. */
258 status = ccwreq_status(cdev, irb);
260 status = req->filter(cdev, req->data, irb, status);
261 if (status != IO_RUNNING)
262 ccw_device_set_timeout(cdev, 0);
263 if (status != IO_DONE && status != IO_RUNNING)
264 ccwreq_log_status(cdev, status);
274 case IO_STATUS_ERROR:
277 /* Check if request was cancelled on purpose. */
284 /* Check back with request initiator. */
287 switch (req->check(cdev, req->data)) {
298 ccwreq_stop(cdev, 0);
302 /* Try next path and restart I/O. */
303 if (!ccwreq_next_path(cdev)) {
312 ccwreq_stop(cdev, rc);
317 * ccw_request_timeout - timeout handler for I/O request procedure
320 * Handle timeout during I/O request procedure.
322 void ccw_request_timeout(struct ccw_device *cdev)
324 struct subchannel *sch = to_subchannel(cdev->dev.parent);
325 struct ccw_request *req = &cdev->private->req;
328 if (!ccwreq_next_path(cdev)) {
329 /* set the final return code for this request */
338 ccwreq_stop(cdev, rc);
342 * ccw_request_notoper - notoper handler for I/O request procedure
345 * Handle timeout during I/O request procedure.
347 void ccw_request_notoper(struct ccw_device *cdev)
349 ccwreq_stop(cdev, -ENODEV);