xref: /linux/drivers/s390/cio/device_ops.c (revision 89fe5117928b2c1272c9376362131ded561c91ad)
1 /*
2  *  drivers/s390/cio/device_ops.c
3  *
4  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
5  *			 IBM Corporation
6  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  */
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 
17 #include <asm/ccwdev.h>
18 #include <asm/idals.h>
19 #include <asm/chpid.h>
20 
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "css.h"
24 #include "chsc.h"
25 #include "device.h"
26 #include "chp.h"
27 
28 /**
29  * ccw_device_set_options_mask() - set some options and unset the rest
30  * @cdev: device for which the options are to be set
31  * @flags: options to be set
32  *
33  * All flags specified in @flags are set, all flags not specified in @flags
34  * are cleared.
35  * Returns:
36  *   %0 on success, -%EINVAL on an invalid flag combination.
37  */
38 int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
39 {
40        /*
41 	* The flag usage is mutal exclusive ...
42 	*/
43 	if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
44 	    (flags & CCWDEV_REPORT_ALL))
45 		return -EINVAL;
46 	cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
47 	cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
48 	cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
49 	cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
50 	return 0;
51 }
52 
53 /**
54  * ccw_device_set_options() - set some options
55  * @cdev: device for which the options are to be set
56  * @flags: options to be set
57  *
58  * All flags specified in @flags are set, the remainder is left untouched.
59  * Returns:
60  *   %0 on success, -%EINVAL if an invalid flag combination would ensue.
61  */
62 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
63 {
64        /*
65 	* The flag usage is mutal exclusive ...
66 	*/
67 	if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
68 	    (flags & CCWDEV_REPORT_ALL)) ||
69 	    ((flags & CCWDEV_EARLY_NOTIFICATION) &&
70 	     cdev->private->options.repall) ||
71 	    ((flags & CCWDEV_REPORT_ALL) &&
72 	     cdev->private->options.fast))
73 		return -EINVAL;
74 	cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
75 	cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
76 	cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
77 	cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
78 	return 0;
79 }
80 
81 /**
82  * ccw_device_clear_options() - clear some options
83  * @cdev: device for which the options are to be cleared
84  * @flags: options to be cleared
85  *
86  * All flags specified in @flags are cleared, the remainder is left untouched.
87  */
88 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
89 {
90 	cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
91 	cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
92 	cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
93 	cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
94 }
95 
96 /**
97  * ccw_device_clear() - terminate I/O request processing
98  * @cdev: target ccw device
99  * @intparm: interruption parameter; value is only used if no I/O is
100  *	     outstanding, otherwise the intparm associated with the I/O request
101  *	     is returned
102  *
103  * ccw_device_clear() calls csch on @cdev's subchannel.
104  * Returns:
105  *  %0 on success,
106  *  -%ENODEV on device not operational,
107  *  -%EINVAL on invalid device state.
108  * Context:
109  *  Interrupts disabled, ccw device lock held
110  */
111 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
112 {
113 	struct subchannel *sch;
114 	int ret;
115 
116 	if (!cdev)
117 		return -ENODEV;
118 	if (cdev->private->state == DEV_STATE_NOT_OPER)
119 		return -ENODEV;
120 	if (cdev->private->state != DEV_STATE_ONLINE &&
121 	    cdev->private->state != DEV_STATE_W4SENSE)
122 		return -EINVAL;
123 	sch = to_subchannel(cdev->dev.parent);
124 	if (!sch)
125 		return -ENODEV;
126 	ret = cio_clear(sch);
127 	if (ret == 0)
128 		cdev->private->intparm = intparm;
129 	return ret;
130 }
131 
132 /**
133  * ccw_device_start_key() - start a s390 channel program with key
134  * @cdev: target ccw device
135  * @cpa: logical start address of channel program
136  * @intparm: user specific interruption parameter; will be presented back to
137  *	     @cdev's interrupt handler. Allows a device driver to associate
138  *	     the interrupt with a particular I/O request.
139  * @lpm: defines the channel path to be used for a specific I/O request. A
140  *	 value of 0 will make cio use the opm.
141  * @key: storage key to be used for the I/O
142  * @flags: additional flags; defines the action to be performed for I/O
143  *	   processing.
144  *
145  * Start a S/390 channel program. When the interrupt arrives, the
146  * IRQ handler is called, either immediately, delayed (dev-end missing,
147  * or sense required) or never (no IRQ handler registered).
148  * Returns:
149  *  %0, if the operation was successful;
150  *  -%EBUSY, if the device is busy, or status pending;
151  *  -%EACCES, if no path specified in @lpm is operational;
152  *  -%ENODEV, if the device is not operational.
153  * Context:
154  *  Interrupts disabled, ccw device lock held
155  */
156 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
157 			 unsigned long intparm, __u8 lpm, __u8 key,
158 			 unsigned long flags)
159 {
160 	struct subchannel *sch;
161 	int ret;
162 
163 	if (!cdev)
164 		return -ENODEV;
165 	sch = to_subchannel(cdev->dev.parent);
166 	if (!sch)
167 		return -ENODEV;
168 	if (cdev->private->state == DEV_STATE_NOT_OPER)
169 		return -ENODEV;
170 	if (cdev->private->state == DEV_STATE_VERIFY ||
171 	    cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
172 		/* Remember to fake irb when finished. */
173 		if (!cdev->private->flags.fake_irb) {
174 			cdev->private->flags.fake_irb = 1;
175 			cdev->private->intparm = intparm;
176 			return 0;
177 		} else
178 			/* There's already a fake I/O around. */
179 			return -EBUSY;
180 	}
181 	if (cdev->private->state != DEV_STATE_ONLINE ||
182 	    ((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
183 	     !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
184 	    cdev->private->flags.doverify)
185 		return -EBUSY;
186 	ret = cio_set_options (sch, flags);
187 	if (ret)
188 		return ret;
189 	/* Adjust requested path mask to excluded varied off paths. */
190 	if (lpm) {
191 		lpm &= sch->opm;
192 		if (lpm == 0)
193 			return -EACCES;
194 	}
195 	ret = cio_start_key (sch, cpa, lpm, key);
196 	switch (ret) {
197 	case 0:
198 		cdev->private->intparm = intparm;
199 		break;
200 	case -EACCES:
201 	case -ENODEV:
202 		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
203 		break;
204 	}
205 	return ret;
206 }
207 
208 /**
209  * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
210  * @cdev: target ccw device
211  * @cpa: logical start address of channel program
212  * @intparm: user specific interruption parameter; will be presented back to
213  *	     @cdev's interrupt handler. Allows a device driver to associate
214  *	     the interrupt with a particular I/O request.
215  * @lpm: defines the channel path to be used for a specific I/O request. A
216  *	 value of 0 will make cio use the opm.
217  * @key: storage key to be used for the I/O
218  * @flags: additional flags; defines the action to be performed for I/O
219  *	   processing.
220  * @expires: timeout value in jiffies
221  *
222  * Start a S/390 channel program. When the interrupt arrives, the
223  * IRQ handler is called, either immediately, delayed (dev-end missing,
224  * or sense required) or never (no IRQ handler registered).
225  * This function notifies the device driver if the channel program has not
226  * completed during the time specified by @expires. If a timeout occurs, the
227  * channel program is terminated via xsch, hsch or csch, and the device's
228  * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
229  * Returns:
230  *  %0, if the operation was successful;
231  *  -%EBUSY, if the device is busy, or status pending;
232  *  -%EACCES, if no path specified in @lpm is operational;
233  *  -%ENODEV, if the device is not operational.
234  * Context:
235  *  Interrupts disabled, ccw device lock held
236  */
237 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
238 				 unsigned long intparm, __u8 lpm, __u8 key,
239 				 unsigned long flags, int expires)
240 {
241 	int ret;
242 
243 	if (!cdev)
244 		return -ENODEV;
245 	ccw_device_set_timeout(cdev, expires);
246 	ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
247 	if (ret != 0)
248 		ccw_device_set_timeout(cdev, 0);
249 	return ret;
250 }
251 
252 /**
253  * ccw_device_start() - start a s390 channel program
254  * @cdev: target ccw device
255  * @cpa: logical start address of channel program
256  * @intparm: user specific interruption parameter; will be presented back to
257  *	     @cdev's interrupt handler. Allows a device driver to associate
258  *	     the interrupt with a particular I/O request.
259  * @lpm: defines the channel path to be used for a specific I/O request. A
260  *	 value of 0 will make cio use the opm.
261  * @flags: additional flags; defines the action to be performed for I/O
262  *	   processing.
263  *
264  * Start a S/390 channel program. When the interrupt arrives, the
265  * IRQ handler is called, either immediately, delayed (dev-end missing,
266  * or sense required) or never (no IRQ handler registered).
267  * Returns:
268  *  %0, if the operation was successful;
269  *  -%EBUSY, if the device is busy, or status pending;
270  *  -%EACCES, if no path specified in @lpm is operational;
271  *  -%ENODEV, if the device is not operational.
272  * Context:
273  *  Interrupts disabled, ccw device lock held
274  */
275 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
276 		     unsigned long intparm, __u8 lpm, unsigned long flags)
277 {
278 	return ccw_device_start_key(cdev, cpa, intparm, lpm,
279 				    PAGE_DEFAULT_KEY, flags);
280 }
281 
282 /**
283  * ccw_device_start_timeout() - start a s390 channel program with timeout
284  * @cdev: target ccw device
285  * @cpa: logical start address of channel program
286  * @intparm: user specific interruption parameter; will be presented back to
287  *	     @cdev's interrupt handler. Allows a device driver to associate
288  *	     the interrupt with a particular I/O request.
289  * @lpm: defines the channel path to be used for a specific I/O request. A
290  *	 value of 0 will make cio use the opm.
291  * @flags: additional flags; defines the action to be performed for I/O
292  *	   processing.
293  * @expires: timeout value in jiffies
294  *
295  * Start a S/390 channel program. When the interrupt arrives, the
296  * IRQ handler is called, either immediately, delayed (dev-end missing,
297  * or sense required) or never (no IRQ handler registered).
298  * This function notifies the device driver if the channel program has not
299  * completed during the time specified by @expires. If a timeout occurs, the
300  * channel program is terminated via xsch, hsch or csch, and the device's
301  * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
302  * Returns:
303  *  %0, if the operation was successful;
304  *  -%EBUSY, if the device is busy, or status pending;
305  *  -%EACCES, if no path specified in @lpm is operational;
306  *  -%ENODEV, if the device is not operational.
307  * Context:
308  *  Interrupts disabled, ccw device lock held
309  */
310 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
311 			     unsigned long intparm, __u8 lpm,
312 			     unsigned long flags, int expires)
313 {
314 	return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
315 					    PAGE_DEFAULT_KEY, flags,
316 					    expires);
317 }
318 
319 
320 /**
321  * ccw_device_halt() - halt I/O request processing
322  * @cdev: target ccw device
323  * @intparm: interruption parameter; value is only used if no I/O is
324  *	     outstanding, otherwise the intparm associated with the I/O request
325  *	     is returned
326  *
327  * ccw_device_halt() calls hsch on @cdev's subchannel.
328  * Returns:
329  *  %0 on success,
330  *  -%ENODEV on device not operational,
331  *  -%EINVAL on invalid device state,
332  *  -%EBUSY on device busy or interrupt pending.
333  * Context:
334  *  Interrupts disabled, ccw device lock held
335  */
336 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
337 {
338 	struct subchannel *sch;
339 	int ret;
340 
341 	if (!cdev)
342 		return -ENODEV;
343 	if (cdev->private->state == DEV_STATE_NOT_OPER)
344 		return -ENODEV;
345 	if (cdev->private->state != DEV_STATE_ONLINE &&
346 	    cdev->private->state != DEV_STATE_W4SENSE)
347 		return -EINVAL;
348 	sch = to_subchannel(cdev->dev.parent);
349 	if (!sch)
350 		return -ENODEV;
351 	ret = cio_halt(sch);
352 	if (ret == 0)
353 		cdev->private->intparm = intparm;
354 	return ret;
355 }
356 
357 /**
358  * ccw_device_resume() - resume channel program execution
359  * @cdev: target ccw device
360  *
361  * ccw_device_resume() calls rsch on @cdev's subchannel.
362  * Returns:
363  *  %0 on success,
364  *  -%ENODEV on device not operational,
365  *  -%EINVAL on invalid device state,
366  *  -%EBUSY on device busy or interrupt pending.
367  * Context:
368  *  Interrupts disabled, ccw device lock held
369  */
370 int ccw_device_resume(struct ccw_device *cdev)
371 {
372 	struct subchannel *sch;
373 
374 	if (!cdev)
375 		return -ENODEV;
376 	sch = to_subchannel(cdev->dev.parent);
377 	if (!sch)
378 		return -ENODEV;
379 	if (cdev->private->state == DEV_STATE_NOT_OPER)
380 		return -ENODEV;
381 	if (cdev->private->state != DEV_STATE_ONLINE ||
382 	    !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
383 		return -EINVAL;
384 	return cio_resume(sch);
385 }
386 
387 /*
388  * Pass interrupt to device driver.
389  */
390 int
391 ccw_device_call_handler(struct ccw_device *cdev)
392 {
393 	struct subchannel *sch;
394 	unsigned int stctl;
395 	int ending_status;
396 
397 	sch = to_subchannel(cdev->dev.parent);
398 
399 	/*
400 	 * we allow for the device action handler if .
401 	 *  - we received ending status
402 	 *  - the action handler requested to see all interrupts
403 	 *  - we received an intermediate status
404 	 *  - fast notification was requested (primary status)
405 	 *  - unsolicited interrupts
406 	 */
407 	stctl = cdev->private->irb.scsw.stctl;
408 	ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
409 		(stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
410 		(stctl == SCSW_STCTL_STATUS_PEND);
411 	if (!ending_status &&
412 	    !cdev->private->options.repall &&
413 	    !(stctl & SCSW_STCTL_INTER_STATUS) &&
414 	    !(cdev->private->options.fast &&
415 	      (stctl & SCSW_STCTL_PRIM_STATUS)))
416 		return 0;
417 
418 	/* Clear pending timers for device driver initiated I/O. */
419 	if (ending_status)
420 		ccw_device_set_timeout(cdev, 0);
421 	/*
422 	 * Now we are ready to call the device driver interrupt handler.
423 	 */
424 	if (cdev->handler)
425 		cdev->handler(cdev, cdev->private->intparm,
426 			      &cdev->private->irb);
427 
428 	/*
429 	 * Clear the old and now useless interrupt response block.
430 	 */
431 	memset(&cdev->private->irb, 0, sizeof(struct irb));
432 
433 	return 1;
434 }
435 
436 /**
437  * ccw_device_get_ciw() - Search for CIW command in extended sense data.
438  * @cdev: ccw device to inspect
439  * @ct: command type to look for
440  *
441  * During SenseID, command information words (CIWs) describing special
442  * commands available to the device may have been stored in the extended
443  * sense data. This function searches for CIWs of a specified command
444  * type in the extended sense data.
445  * Returns:
446  *  %NULL if no extended sense data has been stored or if no CIW of the
447  *  specified command type could be found,
448  *  else a pointer to the CIW of the specified command type.
449  */
450 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
451 {
452 	int ciw_cnt;
453 
454 	if (cdev->private->flags.esid == 0)
455 		return NULL;
456 	for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
457 		if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
458 			return cdev->private->senseid.ciw + ciw_cnt;
459 	return NULL;
460 }
461 
462 /**
463  * ccw_device_get_path_mask() - get currently available paths
464  * @cdev: ccw device to be queried
465  * Returns:
466  *  %0 if no subchannel for the device is available,
467  *  else the mask of currently available paths for the ccw device's subchannel.
468  */
469 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
470 {
471 	struct subchannel *sch;
472 
473 	sch = to_subchannel(cdev->dev.parent);
474 	if (!sch)
475 		return 0;
476 	else
477 		return sch->lpm;
478 }
479 
480 /*
481  * Try to break the lock on a boxed device.
482  */
483 int
484 ccw_device_stlck(struct ccw_device *cdev)
485 {
486 	void *buf, *buf2;
487 	unsigned long flags;
488 	struct subchannel *sch;
489 	int ret;
490 
491 	if (!cdev)
492 		return -ENODEV;
493 
494 	if (cdev->drv && !cdev->private->options.force)
495 		return -EINVAL;
496 
497 	sch = to_subchannel(cdev->dev.parent);
498 
499 	CIO_TRACE_EVENT(2, "stl lock");
500 	CIO_TRACE_EVENT(2, cdev->dev.bus_id);
501 
502 	buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
503 	if (!buf)
504 		return -ENOMEM;
505 	buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
506 	if (!buf2) {
507 		kfree(buf);
508 		return -ENOMEM;
509 	}
510 	spin_lock_irqsave(sch->lock, flags);
511 	ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
512 	if (ret)
513 		goto out_unlock;
514 	/*
515 	 * Setup ccw. We chain an unconditional reserve and a release so we
516 	 * only break the lock.
517 	 */
518 	cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
519 	cdev->private->iccws[0].cda = (__u32) __pa(buf);
520 	cdev->private->iccws[0].count = 32;
521 	cdev->private->iccws[0].flags = CCW_FLAG_CC;
522 	cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
523 	cdev->private->iccws[1].cda = (__u32) __pa(buf2);
524 	cdev->private->iccws[1].count = 32;
525 	cdev->private->iccws[1].flags = 0;
526 	ret = cio_start(sch, cdev->private->iccws, 0);
527 	if (ret) {
528 		cio_disable_subchannel(sch); //FIXME: return code?
529 		goto out_unlock;
530 	}
531 	cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
532 	spin_unlock_irqrestore(sch->lock, flags);
533 	wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
534 	spin_lock_irqsave(sch->lock, flags);
535 	cio_disable_subchannel(sch); //FIXME: return code?
536 	if ((cdev->private->irb.scsw.dstat !=
537 	     (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
538 	    (cdev->private->irb.scsw.cstat != 0))
539 		ret = -EIO;
540 	/* Clear irb. */
541 	memset(&cdev->private->irb, 0, sizeof(struct irb));
542 out_unlock:
543 	kfree(buf);
544 	kfree(buf2);
545 	spin_unlock_irqrestore(sch->lock, flags);
546 	return ret;
547 }
548 
549 void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
550 {
551 	struct subchannel *sch;
552 	struct chp_id chpid;
553 
554 	sch = to_subchannel(cdev->dev.parent);
555 	chp_id_init(&chpid);
556 	chpid.id = sch->schib.pmcw.chpid[chp_no];
557 	return chp_get_chp_desc(chpid);
558 }
559 
560 /**
561  * ccw_device_get_id - obtain a ccw device id
562  * @cdev: device to obtain the id for
563  * @dev_id: where to fill in the values
564  */
565 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
566 {
567 	*dev_id = cdev->private->dev_id;
568 }
569 EXPORT_SYMBOL(ccw_device_get_id);
570 
571 // FIXME: these have to go:
572 
573 int
574 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
575 {
576 	return cdev->private->schid.sch_no;
577 }
578 
579 
580 MODULE_LICENSE("GPL");
581 EXPORT_SYMBOL(ccw_device_set_options_mask);
582 EXPORT_SYMBOL(ccw_device_set_options);
583 EXPORT_SYMBOL(ccw_device_clear_options);
584 EXPORT_SYMBOL(ccw_device_clear);
585 EXPORT_SYMBOL(ccw_device_halt);
586 EXPORT_SYMBOL(ccw_device_resume);
587 EXPORT_SYMBOL(ccw_device_start_timeout);
588 EXPORT_SYMBOL(ccw_device_start);
589 EXPORT_SYMBOL(ccw_device_start_timeout_key);
590 EXPORT_SYMBOL(ccw_device_start_key);
591 EXPORT_SYMBOL(ccw_device_get_ciw);
592 EXPORT_SYMBOL(ccw_device_get_path_mask);
593 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
594 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
595