File Index
Files and directories
Sign in
FILE

ccwreq.c

File preview with syntax highlighting.

linux-6.19.14/drivers/s390/cio/ccwreq.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  Handling of internal CCW device requests.
  4 *
  5 *    Copyright IBM Corp. 2009, 2011
  6 *    Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  7 */
  8
  9#define pr_fmt(fmt) "cio: " fmt
 10
 11#include <linux/types.h>
 12#include <linux/err.h>
 13#include <asm/ccwdev.h>
 14#include <asm/cio.h>
 15
 16#include "io_sch.h"
 17#include "cio.h"
 18#include "device.h"
 19#include "cio_debug.h"
 20
 21/**
 22 * lpm_adjust - adjust path mask
 23 * @lpm: path mask to adjust
 24 * @mask: mask of available paths
 25 *
 26 * Shift @lpm right until @lpm and @mask have at least one bit in common or
 27 * until @lpm is zero. Return the resulting lpm.
 28 */
 29int lpm_adjust(int lpm, int mask)
 30{
 31	while (lpm && ((lpm & mask) == 0))
 32		lpm >>= 1;
 33	return lpm;
 34}
 35
 36/*
 37 * Adjust path mask to use next path and reset retry count. Return resulting
 38 * path mask.
 39 */
 40static u16 ccwreq_next_path(struct ccw_device *cdev)
 41{
 42	struct ccw_request *req = &cdev->private->req;
 43
 44	if (!req->singlepath) {
 45		req->mask = 0;
 46		goto out;
 47	}
 48	req->retries	= req->maxretries;
 49	req->mask	= lpm_adjust(req->mask >> 1, req->lpm);
 50out:
 51	return req->mask;
 52}
 53
 54/*
 55 * Clean up device state and report to callback.
 56 */
 57static void ccwreq_stop(struct ccw_device *cdev, int rc)
 58{
 59	struct ccw_request *req = &cdev->private->req;
 60
 61	if (req->done)
 62		return;
 63	req->done = 1;
 64	ccw_device_set_timeout(cdev, 0);
 65	memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
 66	if (rc && rc != -ENODEV && req->drc)
 67		rc = req->drc;
 68	req->callback(cdev, req->data, rc);
 69}
 70
 71/*
 72 * (Re-)Start the operation until retries and paths are exhausted.
 73 */
 74static void ccwreq_do(struct ccw_device *cdev)
 75{
 76	struct ccw_request *req = &cdev->private->req;
 77	struct subchannel *sch = to_subchannel(cdev->dev.parent);
 78	struct ccw1 *cp = req->cp;
 79	int rc = -EACCES;
 80
 81	while (req->mask) {
 82		if (req->retries-- == 0) {
 83			/* Retries exhausted, try next path. */
 84			ccwreq_next_path(cdev);
 85			continue;
 86		}
 87		/* Perform start function. */
 88		memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
 89		rc = cio_start(sch, cp, (u8) req->mask);
 90		if (rc == 0) {
 91			/* I/O started successfully. */
 92			ccw_device_set_timeout(cdev, req->timeout);
 93			return;
 94		}
 95		if (rc == -ENODEV) {
 96			/* Permanent device error. */
 97			break;
 98		}
 99		if (rc == -EACCES) {
100			/* Permant path error. */
101			ccwreq_next_path(cdev);
102			continue;
103		}
104		/* Temporary improper status. */
105		rc = cio_clear(sch);
106		if (rc)
107			break;
108		return;
109	}
110	ccwreq_stop(cdev, rc);
111}
112
113/**
114 * ccw_request_start - perform I/O request
115 * @cdev: ccw device
116 *
117 * Perform the I/O request specified by cdev->req.
118 */
119void ccw_request_start(struct ccw_device *cdev)
120{
121	struct ccw_request *req = &cdev->private->req;
122
123	if (req->singlepath) {
124		/* Try all paths twice to counter link flapping. */
125		req->mask = 0x8080;
126	} else
127		req->mask = req->lpm;
128
129	req->retries	= req->maxretries;
130	req->mask	= lpm_adjust(req->mask, req->lpm);
131	req->drc	= 0;
132	req->done	= 0;
133	req->cancel	= 0;
134	if (!req->mask)
135		goto out_nopath;
136	ccwreq_do(cdev);
137	return;
138
139out_nopath:
140	ccwreq_stop(cdev, -EACCES);
141}
142
143/**
144 * ccw_request_cancel - cancel running I/O request
145 * @cdev: ccw device
146 *
147 * Cancel the I/O request specified by cdev->req. Return non-zero if request
148 * has already finished, zero otherwise.
149 */
150int ccw_request_cancel(struct ccw_device *cdev)
151{
152	struct subchannel *sch = to_subchannel(cdev->dev.parent);
153	struct ccw_request *req = &cdev->private->req;
154	int rc;
155
156	if (req->done)
157		return 1;
158	req->cancel = 1;
159	rc = cio_clear(sch);
160	if (rc)
161		ccwreq_stop(cdev, rc);
162	return 0;
163}
164
165/*
166 * Return the status of the internal I/O started on the specified ccw device.
167 * Perform BASIC SENSE if required.
168 */
169static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
170{
171	struct irb *irb = &cdev->private->dma_area->irb;
172	struct cmd_scsw *scsw = &irb->scsw.cmd;
173	enum uc_todo todo;
174
175	/* Perform BASIC SENSE if needed. */
176	if (ccw_device_accumulate_and_sense(cdev, lcirb))
177		return IO_RUNNING;
178	/* Check for halt/clear interrupt. */
179	if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
180		return IO_KILLED;
181	/* Check for path error. */
182	if (scsw->cc == 3 || scsw->pno)
183		return IO_PATH_ERROR;
184	/* Handle BASIC SENSE data. */
185	if (irb->esw.esw0.erw.cons) {
186		CIO_TRACE_EVENT(2, "sensedata");
187		CIO_HEX_EVENT(2, &cdev->private->dev_id,
188			      sizeof(struct ccw_dev_id));
189		CIO_HEX_EVENT(2, &cdev->private->dma_area->irb.ecw,
190			      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;
208			}
209		}
210		/* Assume that unexpected SENSE data implies an error. */
211		return IO_STATUS_ERROR;
212	}
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;
226}
227
228/*
229 * Log ccw request status.
230 */
231static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
232{
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));
246}
247
248/**
249 * ccw_request_handler - interrupt handler for I/O request procedure.
250 * @cdev: ccw device
251 *
252 * Handle interrupt during I/O request procedure.
253 */
254void ccw_request_handler(struct ccw_device *cdev)
255{
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;
260
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;
285		}
286		goto out_restart;
287	}
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;
300	}
301out:
302	ccwreq_stop(cdev, 0);
303	return;
304
305out_next_path:
306	/* Try next path and restart I/O. */
307	if (!ccwreq_next_path(cdev)) {
308		rc = -EACCES;
309		goto err;
310	}
311out_restart:
312	/* Restart. */
313	ccwreq_do(cdev);
314	return;
315err:
316	ccwreq_stop(cdev, rc);
317}
318
319
320/**
321 * ccw_request_timeout - timeout handler for I/O request procedure
322 * @cdev: ccw device
323 *
324 * Handle timeout during I/O request procedure.
325 */
326void ccw_request_timeout(struct ccw_device *cdev)
327{
328	struct subchannel *sch = to_subchannel(cdev->dev.parent);
329	struct ccw_request *req = &cdev->private->req;
330	int rc = -ENODEV, chp;
331
332	if (cio_update_schib(sch))
333		goto err;
334
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]);
343	}
344
345	if (!ccwreq_next_path(cdev)) {
346		/* set the final return code for this request */
347		req->drc = -ETIME;
348	}
349	rc = cio_clear(sch);
350	if (rc)
351		goto err;
352	return;
353
354err:
355	ccwreq_stop(cdev, rc);
356}
357
358/**
359 * ccw_request_notoper - notoper handler for I/O request procedure
360 * @cdev: ccw device
361 *
362 * Handle notoper during I/O request procedure.
363 */
364void ccw_request_notoper(struct ccw_device *cdev)
365{
366	ccwreq_stop(cdev, -ENODEV);
367}
~/ Read-only file preview.