2 * drivers/s390/cio/device_ops.c
4 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
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>
17 #include <asm/ccwdev.h>
18 #include <asm/idals.h>
19 #include <asm/chpid.h>
23 #include "cio_debug.h"
30 * ccw_device_set_options_mask() - set some options and unset the rest
31 * @cdev: device for which the options are to be set
32 * @flags: options to be set
34 * All flags specified in @flags are set, all flags not specified in @flags
37 * %0 on success, -%EINVAL on an invalid flag combination.
39 int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
42 * The flag usage is mutal exclusive ...
44 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
45 (flags & CCWDEV_REPORT_ALL))
47 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
48 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
49 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
50 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
55 * ccw_device_set_options() - set some options
56 * @cdev: device for which the options are to be set
57 * @flags: options to be set
59 * All flags specified in @flags are set, the remainder is left untouched.
61 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
63 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
66 * The flag usage is mutal exclusive ...
68 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
69 (flags & CCWDEV_REPORT_ALL)) ||
70 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
71 cdev->private->options.repall) ||
72 ((flags & CCWDEV_REPORT_ALL) &&
73 cdev->private->options.fast))
75 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
76 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
77 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
78 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
83 * ccw_device_clear_options() - clear some options
84 * @cdev: device for which the options are to be cleared
85 * @flags: options to be cleared
87 * All flags specified in @flags are cleared, the remainder is left untouched.
89 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
91 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
92 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
93 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
94 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
98 * ccw_device_clear() - terminate I/O request processing
99 * @cdev: target ccw device
100 * @intparm: interruption parameter; value is only used if no I/O is
101 * outstanding, otherwise the intparm associated with the I/O request
104 * ccw_device_clear() calls csch on @cdev's subchannel.
107 * -%ENODEV on device not operational,
108 * -%EINVAL on invalid device state.
110 * Interrupts disabled, ccw device lock held
112 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
114 struct subchannel *sch;
117 if (!cdev || !cdev->dev.parent)
119 if (cdev->private->state == DEV_STATE_NOT_OPER)
121 if (cdev->private->state != DEV_STATE_ONLINE &&
122 cdev->private->state != DEV_STATE_W4SENSE)
124 sch = to_subchannel(cdev->dev.parent);
125 ret = cio_clear(sch);
127 cdev->private->intparm = intparm;
132 * ccw_device_start_key() - start a s390 channel program with key
133 * @cdev: target ccw device
134 * @cpa: logical start address of channel program
135 * @intparm: user specific interruption parameter; will be presented back to
136 * @cdev's interrupt handler. Allows a device driver to associate
137 * the interrupt with a particular I/O request.
138 * @lpm: defines the channel path to be used for a specific I/O request. A
139 * value of 0 will make cio use the opm.
140 * @key: storage key to be used for the I/O
141 * @flags: additional flags; defines the action to be performed for I/O
144 * Start a S/390 channel program. When the interrupt arrives, the
145 * IRQ handler is called, either immediately, delayed (dev-end missing,
146 * or sense required) or never (no IRQ handler registered).
148 * %0, if the operation was successful;
149 * -%EBUSY, if the device is busy, or status pending;
150 * -%EACCES, if no path specified in @lpm is operational;
151 * -%ENODEV, if the device is not operational.
153 * Interrupts disabled, ccw device lock held
155 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
156 unsigned long intparm, __u8 lpm, __u8 key,
159 struct subchannel *sch;
162 if (!cdev || !cdev->dev.parent)
164 sch = to_subchannel(cdev->dev.parent);
165 if (cdev->private->state == DEV_STATE_NOT_OPER)
167 if (cdev->private->state == DEV_STATE_VERIFY ||
168 cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
169 /* Remember to fake irb when finished. */
170 if (!cdev->private->flags.fake_irb) {
171 cdev->private->flags.fake_irb = 1;
172 cdev->private->intparm = intparm;
175 /* There's already a fake I/O around. */
178 if (cdev->private->state != DEV_STATE_ONLINE ||
179 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
180 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
181 cdev->private->flags.doverify)
183 ret = cio_set_options (sch, flags);
186 /* Adjust requested path mask to excluded varied off paths. */
192 ret = cio_start_key (sch, cpa, lpm, key);
195 cdev->private->intparm = intparm;
199 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
206 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
207 * @cdev: target ccw device
208 * @cpa: logical start address of channel program
209 * @intparm: user specific interruption parameter; will be presented back to
210 * @cdev's interrupt handler. Allows a device driver to associate
211 * the interrupt with a particular I/O request.
212 * @lpm: defines the channel path to be used for a specific I/O request. A
213 * value of 0 will make cio use the opm.
214 * @key: storage key to be used for the I/O
215 * @flags: additional flags; defines the action to be performed for I/O
217 * @expires: timeout value in jiffies
219 * Start a S/390 channel program. When the interrupt arrives, the
220 * IRQ handler is called, either immediately, delayed (dev-end missing,
221 * or sense required) or never (no IRQ handler registered).
222 * This function notifies the device driver if the channel program has not
223 * completed during the time specified by @expires. If a timeout occurs, the
224 * channel program is terminated via xsch, hsch or csch, and the device's
225 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
227 * %0, if the operation was successful;
228 * -%EBUSY, if the device is busy, or status pending;
229 * -%EACCES, if no path specified in @lpm is operational;
230 * -%ENODEV, if the device is not operational.
232 * Interrupts disabled, ccw device lock held
234 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
235 unsigned long intparm, __u8 lpm, __u8 key,
236 unsigned long flags, int expires)
242 ccw_device_set_timeout(cdev, expires);
243 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
245 ccw_device_set_timeout(cdev, 0);
250 * ccw_device_start() - start a s390 channel program
251 * @cdev: target ccw device
252 * @cpa: logical start address of channel program
253 * @intparm: user specific interruption parameter; will be presented back to
254 * @cdev's interrupt handler. Allows a device driver to associate
255 * the interrupt with a particular I/O request.
256 * @lpm: defines the channel path to be used for a specific I/O request. A
257 * value of 0 will make cio use the opm.
258 * @flags: additional flags; defines the action to be performed for I/O
261 * Start a S/390 channel program. When the interrupt arrives, the
262 * IRQ handler is called, either immediately, delayed (dev-end missing,
263 * or sense required) or never (no IRQ handler registered).
265 * %0, if the operation was successful;
266 * -%EBUSY, if the device is busy, or status pending;
267 * -%EACCES, if no path specified in @lpm is operational;
268 * -%ENODEV, if the device is not operational.
270 * Interrupts disabled, ccw device lock held
272 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
273 unsigned long intparm, __u8 lpm, unsigned long flags)
275 return ccw_device_start_key(cdev, cpa, intparm, lpm,
276 PAGE_DEFAULT_KEY, flags);
280 * ccw_device_start_timeout() - start a s390 channel program with timeout
281 * @cdev: target ccw device
282 * @cpa: logical start address of channel program
283 * @intparm: user specific interruption parameter; will be presented back to
284 * @cdev's interrupt handler. Allows a device driver to associate
285 * the interrupt with a particular I/O request.
286 * @lpm: defines the channel path to be used for a specific I/O request. A
287 * value of 0 will make cio use the opm.
288 * @flags: additional flags; defines the action to be performed for I/O
290 * @expires: timeout value in jiffies
292 * Start a S/390 channel program. When the interrupt arrives, the
293 * IRQ handler is called, either immediately, delayed (dev-end missing,
294 * or sense required) or never (no IRQ handler registered).
295 * This function notifies the device driver if the channel program has not
296 * completed during the time specified by @expires. If a timeout occurs, the
297 * channel program is terminated via xsch, hsch or csch, and the device's
298 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
300 * %0, if the operation was successful;
301 * -%EBUSY, if the device is busy, or status pending;
302 * -%EACCES, if no path specified in @lpm is operational;
303 * -%ENODEV, if the device is not operational.
305 * Interrupts disabled, ccw device lock held
307 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
308 unsigned long intparm, __u8 lpm,
309 unsigned long flags, int expires)
311 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
312 PAGE_DEFAULT_KEY, flags,
318 * ccw_device_halt() - halt I/O request processing
319 * @cdev: target ccw device
320 * @intparm: interruption parameter; value is only used if no I/O is
321 * outstanding, otherwise the intparm associated with the I/O request
324 * ccw_device_halt() calls hsch on @cdev's subchannel.
327 * -%ENODEV on device not operational,
328 * -%EINVAL on invalid device state,
329 * -%EBUSY on device busy or interrupt pending.
331 * Interrupts disabled, ccw device lock held
333 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
335 struct subchannel *sch;
338 if (!cdev || !cdev->dev.parent)
340 if (cdev->private->state == DEV_STATE_NOT_OPER)
342 if (cdev->private->state != DEV_STATE_ONLINE &&
343 cdev->private->state != DEV_STATE_W4SENSE)
345 sch = to_subchannel(cdev->dev.parent);
348 cdev->private->intparm = intparm;
353 * ccw_device_resume() - resume channel program execution
354 * @cdev: target ccw device
356 * ccw_device_resume() calls rsch on @cdev's subchannel.
359 * -%ENODEV on device not operational,
360 * -%EINVAL on invalid device state,
361 * -%EBUSY on device busy or interrupt pending.
363 * Interrupts disabled, ccw device lock held
365 int ccw_device_resume(struct ccw_device *cdev)
367 struct subchannel *sch;
369 if (!cdev || !cdev->dev.parent)
371 sch = to_subchannel(cdev->dev.parent);
372 if (cdev->private->state == DEV_STATE_NOT_OPER)
374 if (cdev->private->state != DEV_STATE_ONLINE ||
375 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
377 return cio_resume(sch);
381 * Pass interrupt to device driver.
384 ccw_device_call_handler(struct ccw_device *cdev)
386 struct subchannel *sch;
390 sch = to_subchannel(cdev->dev.parent);
393 * we allow for the device action handler if .
394 * - we received ending status
395 * - the action handler requested to see all interrupts
396 * - we received an intermediate status
397 * - fast notification was requested (primary status)
398 * - unsolicited interrupts
400 stctl = scsw_stctl(&cdev->private->irb.scsw);
401 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
402 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
403 (stctl == SCSW_STCTL_STATUS_PEND);
404 if (!ending_status &&
405 !cdev->private->options.repall &&
406 !(stctl & SCSW_STCTL_INTER_STATUS) &&
407 !(cdev->private->options.fast &&
408 (stctl & SCSW_STCTL_PRIM_STATUS)))
411 /* Clear pending timers for device driver initiated I/O. */
413 ccw_device_set_timeout(cdev, 0);
415 * Now we are ready to call the device driver interrupt handler.
418 cdev->handler(cdev, cdev->private->intparm,
419 &cdev->private->irb);
422 * Clear the old and now useless interrupt response block.
424 memset(&cdev->private->irb, 0, sizeof(struct irb));
430 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
431 * @cdev: ccw device to inspect
432 * @ct: command type to look for
434 * During SenseID, command information words (CIWs) describing special
435 * commands available to the device may have been stored in the extended
436 * sense data. This function searches for CIWs of a specified command
437 * type in the extended sense data.
439 * %NULL if no extended sense data has been stored or if no CIW of the
440 * specified command type could be found,
441 * else a pointer to the CIW of the specified command type.
443 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
447 if (cdev->private->flags.esid == 0)
449 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
450 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
451 return cdev->private->senseid.ciw + ciw_cnt;
456 * ccw_device_get_path_mask() - get currently available paths
457 * @cdev: ccw device to be queried
459 * %0 if no subchannel for the device is available,
460 * else the mask of currently available paths for the ccw device's subchannel.
462 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
464 struct subchannel *sch;
466 if (!cdev->dev.parent)
469 sch = to_subchannel(cdev->dev.parent);
474 * Try to break the lock on a boxed device.
477 ccw_device_stlck(struct ccw_device *cdev)
481 struct subchannel *sch;
487 if (cdev->drv && !cdev->private->options.force)
490 sch = to_subchannel(cdev->dev.parent);
492 CIO_TRACE_EVENT(2, "stl lock");
493 CIO_TRACE_EVENT(2, dev_name(&cdev->dev));
495 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
498 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
503 spin_lock_irqsave(sch->lock, flags);
504 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
508 * Setup ccw. We chain an unconditional reserve and a release so we
509 * only break the lock.
511 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
512 cdev->private->iccws[0].cda = (__u32) __pa(buf);
513 cdev->private->iccws[0].count = 32;
514 cdev->private->iccws[0].flags = CCW_FLAG_CC;
515 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
516 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
517 cdev->private->iccws[1].count = 32;
518 cdev->private->iccws[1].flags = 0;
519 ret = cio_start(sch, cdev->private->iccws, 0);
521 cio_disable_subchannel(sch); //FIXME: return code?
524 cdev->private->irb.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
525 spin_unlock_irqrestore(sch->lock, flags);
526 wait_event(cdev->private->wait_q,
527 cdev->private->irb.scsw.cmd.actl == 0);
528 spin_lock_irqsave(sch->lock, flags);
529 cio_disable_subchannel(sch); //FIXME: return code?
530 if ((cdev->private->irb.scsw.cmd.dstat !=
531 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
532 (cdev->private->irb.scsw.cmd.cstat != 0))
535 memset(&cdev->private->irb, 0, sizeof(struct irb));
539 spin_unlock_irqrestore(sch->lock, flags);
543 void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
545 struct subchannel *sch;
548 sch = to_subchannel(cdev->dev.parent);
550 chpid.id = sch->schib.pmcw.chpid[chp_no];
551 return chp_get_chp_desc(chpid);
555 * ccw_device_get_id - obtain a ccw device id
556 * @cdev: device to obtain the id for
557 * @dev_id: where to fill in the values
559 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
561 *dev_id = cdev->private->dev_id;
563 EXPORT_SYMBOL(ccw_device_get_id);
566 * ccw_device_tm_start_key - perform start function
567 * @cdev: ccw device on which to perform the start function
568 * @tcw: transport-command word to be started
569 * @intparm: user defined parameter to be passed to the interrupt handler
570 * @lpm: mask of paths to use
571 * @key: storage key to use for storage access
573 * Start the tcw on the given ccw device. Return zero on success, non-zero
576 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
577 unsigned long intparm, u8 lpm, u8 key)
579 struct subchannel *sch;
582 sch = to_subchannel(cdev->dev.parent);
583 if (cdev->private->state != DEV_STATE_ONLINE)
585 /* Adjust requested path mask to excluded varied off paths. */
591 rc = cio_tm_start_key(sch, tcw, lpm, key);
593 cdev->private->intparm = intparm;
596 EXPORT_SYMBOL(ccw_device_tm_start_key);
599 * ccw_device_tm_start_timeout_key - perform start function
600 * @cdev: ccw device on which to perform the start function
601 * @tcw: transport-command word to be started
602 * @intparm: user defined parameter to be passed to the interrupt handler
603 * @lpm: mask of paths to use
604 * @key: storage key to use for storage access
605 * @expires: time span in jiffies after which to abort request
607 * Start the tcw on the given ccw device. Return zero on success, non-zero
610 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
611 unsigned long intparm, u8 lpm, u8 key,
616 ccw_device_set_timeout(cdev, expires);
617 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
619 ccw_device_set_timeout(cdev, 0);
622 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
625 * ccw_device_tm_start - perform start function
626 * @cdev: ccw device on which to perform the start function
627 * @tcw: transport-command word to be started
628 * @intparm: user defined parameter to be passed to the interrupt handler
629 * @lpm: mask of paths to use
631 * Start the tcw on the given ccw device. Return zero on success, non-zero
634 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
635 unsigned long intparm, u8 lpm)
637 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
640 EXPORT_SYMBOL(ccw_device_tm_start);
643 * ccw_device_tm_start_timeout - perform start function
644 * @cdev: ccw device on which to perform the start function
645 * @tcw: transport-command word to be started
646 * @intparm: user defined parameter to be passed to the interrupt handler
647 * @lpm: mask of paths to use
648 * @expires: time span in jiffies after which to abort request
650 * Start the tcw on the given ccw device. Return zero on success, non-zero
653 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
654 unsigned long intparm, u8 lpm, int expires)
656 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
657 PAGE_DEFAULT_KEY, expires);
659 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
662 * ccw_device_tm_intrg - perform interrogate function
663 * @cdev: ccw device on which to perform the interrogate function
665 * Perform an interrogate function on the given ccw device. Return zero on
666 * success, non-zero otherwise.
668 int ccw_device_tm_intrg(struct ccw_device *cdev)
670 struct subchannel *sch = to_subchannel(cdev->dev.parent);
672 if (cdev->private->state != DEV_STATE_ONLINE)
674 if (!scsw_is_tm(&sch->schib.scsw) ||
675 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
677 return cio_tm_intrg(sch);
679 EXPORT_SYMBOL(ccw_device_tm_intrg);
681 // FIXME: these have to go:
684 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
686 return cdev->private->schid.sch_no;
690 MODULE_LICENSE("GPL");
691 EXPORT_SYMBOL(ccw_device_set_options_mask);
692 EXPORT_SYMBOL(ccw_device_set_options);
693 EXPORT_SYMBOL(ccw_device_clear_options);
694 EXPORT_SYMBOL(ccw_device_clear);
695 EXPORT_SYMBOL(ccw_device_halt);
696 EXPORT_SYMBOL(ccw_device_resume);
697 EXPORT_SYMBOL(ccw_device_start_timeout);
698 EXPORT_SYMBOL(ccw_device_start);
699 EXPORT_SYMBOL(ccw_device_start_timeout_key);
700 EXPORT_SYMBOL(ccw_device_start_key);
701 EXPORT_SYMBOL(ccw_device_get_ciw);
702 EXPORT_SYMBOL(ccw_device_get_path_mask);
703 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
704 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);