2 * Handling of internal CCW device requests.
4 * Copyright IBM Corp. 2009
5 * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
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 req
->retries
= req
->maxretries
;
42 req
->mask
= lpm_adjust(req
->mask
>>= 1, req
->lpm
);
48 * Clean up device state and report to callback.
50 static void ccwreq_stop(struct ccw_device
*cdev
, int rc
)
52 struct ccw_request
*req
= &cdev
->private->req
;
57 ccw_device_set_timeout(cdev
, 0);
58 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
59 if (rc
&& rc
!= -ENODEV
&& req
->drc
)
61 req
->callback(cdev
, req
->data
, rc
);
65 * (Re-)Start the operation until retries and paths are exhausted.
67 static void ccwreq_do(struct ccw_device
*cdev
)
69 struct ccw_request
*req
= &cdev
->private->req
;
70 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
71 struct ccw1
*cp
= req
->cp
;
75 if (req
->retries
-- == 0) {
76 /* Retries exhausted, try next path. */
77 ccwreq_next_path(cdev
);
80 /* Perform start function. */
81 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
82 rc
= cio_start(sch
, cp
, (u8
) req
->mask
);
84 /* I/O started successfully. */
85 ccw_device_set_timeout(cdev
, req
->timeout
);
89 /* Permanent device error. */
93 /* Permant path error. */
94 ccwreq_next_path(cdev
);
97 /* Temporary improper status. */
103 ccwreq_stop(cdev
, rc
);
107 * ccw_request_start - perform I/O request
110 * Perform the I/O request specified by cdev->req.
112 void ccw_request_start(struct ccw_device
*cdev
)
114 struct ccw_request
*req
= &cdev
->private->req
;
116 /* Try all paths twice to counter link flapping. */
118 req
->retries
= req
->maxretries
;
119 req
->mask
= lpm_adjust(req
->mask
, req
->lpm
);
129 ccwreq_stop(cdev
, -EACCES
);
133 * ccw_request_cancel - cancel running I/O request
136 * Cancel the I/O request specified by cdev->req. Return non-zero if request
137 * has already finished, zero otherwise.
139 int ccw_request_cancel(struct ccw_device
*cdev
)
141 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
142 struct ccw_request
*req
= &cdev
->private->req
;
150 ccwreq_stop(cdev
, rc
);
155 * Return the status of the internal I/O started on the specified ccw device.
156 * Perform BASIC SENSE if required.
158 static enum io_status
ccwreq_status(struct ccw_device
*cdev
, struct irb
*lcirb
)
160 struct irb
*irb
= &cdev
->private->irb
;
161 struct cmd_scsw
*scsw
= &irb
->scsw
.cmd
;
164 /* Perform BASIC SENSE if needed. */
165 if (ccw_device_accumulate_and_sense(cdev
, lcirb
))
167 /* Check for halt/clear interrupt. */
168 if (scsw
->fctl
& (SCSW_FCTL_HALT_FUNC
| SCSW_FCTL_CLEAR_FUNC
))
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
)
182 /* Ask the driver what to do */
183 if (cdev
->drv
&& cdev
->drv
->uc_handler
) {
184 todo
= cdev
->drv
->uc_handler(cdev
, lcirb
);
187 return IO_STATUS_ERROR
;
188 case UC_TODO_RETRY_ON_NEW_PATH
:
189 return IO_PATH_ERROR
;
193 return IO_STATUS_ERROR
;
196 /* Assume that unexpected SENSE data implies an error. */
197 return IO_STATUS_ERROR
;
199 /* Check for channel errors. */
200 if (scsw
->cstat
!= 0)
201 return IO_STATUS_ERROR
;
202 /* Check for device errors. */
203 if (scsw
->dstat
& ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
204 return IO_STATUS_ERROR
;
205 /* Check for final state. */
206 if (!(scsw
->dstat
& DEV_STAT_DEV_END
))
208 /* Check for other improper status. */
209 if (scsw
->cc
== 1 && (scsw
->stctl
& SCSW_STCTL_ALERT_STATUS
))
210 return IO_STATUS_ERROR
;
215 * Log ccw request status.
217 static void ccwreq_log_status(struct ccw_device
*cdev
, enum io_status status
)
219 struct ccw_request
*req
= &cdev
->private->req
;
221 struct ccw_dev_id dev_id
;
225 } __attribute__ ((packed
)) data
;
226 data
.dev_id
= cdev
->private->dev_id
;
227 data
.retries
= req
->retries
;
228 data
.lpm
= (u8
) req
->mask
;
229 data
.status
= (u8
) status
;
230 CIO_TRACE_EVENT(2, "reqstat");
231 CIO_HEX_EVENT(2, &data
, sizeof(data
));
235 * ccw_request_handler - interrupt handler for I/O request procedure.
238 * Handle interrupt during I/O request procedure.
240 void ccw_request_handler(struct ccw_device
*cdev
)
242 struct irb
*irb
= (struct irb
*)&S390_lowcore
.irb
;
243 struct ccw_request
*req
= &cdev
->private->req
;
244 enum io_status status
;
245 int rc
= -EOPNOTSUPP
;
247 /* Check status of I/O request. */
248 status
= ccwreq_status(cdev
, irb
);
250 status
= req
->filter(cdev
, req
->data
, irb
, status
);
251 if (status
!= IO_RUNNING
)
252 ccw_device_set_timeout(cdev
, 0);
253 if (status
!= IO_DONE
&& status
!= IO_RUNNING
)
254 ccwreq_log_status(cdev
, status
);
264 case IO_STATUS_ERROR
:
267 /* Check if request was cancelled on purpose. */
274 /* Check back with request initiator. */
277 switch (req
->check(cdev
, req
->data
)) {
288 ccwreq_stop(cdev
, 0);
292 /* Try next path and restart I/O. */
293 if (!ccwreq_next_path(cdev
)) {
302 ccwreq_stop(cdev
, rc
);
307 * ccw_request_timeout - timeout handler for I/O request procedure
310 * Handle timeout during I/O request procedure.
312 void ccw_request_timeout(struct ccw_device
*cdev
)
314 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
315 struct ccw_request
*req
= &cdev
->private->req
;
318 if (!ccwreq_next_path(cdev
)) {
319 /* set the final return code for this request */
328 ccwreq_stop(cdev
, rc
);
332 * ccw_request_notoper - notoper handler for I/O request procedure
335 * Handle timeout during I/O request procedure.
337 void ccw_request_notoper(struct ccw_device
*cdev
)
339 ccwreq_stop(cdev
, -ENODEV
);