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;
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);
127 ret = cio_clear(sch);
129 cdev->private->intparm = intparm;
134 * ccw_device_start_key() - start a s390 channel program with key
135 * @cdev: target ccw device
136 * @cpa: logical start address of channel program
137 * @intparm: user specific interruption parameter; will be presented back to
138 * @cdev's interrupt handler. Allows a device driver to associate
139 * the interrupt with a particular I/O request.
140 * @lpm: defines the channel path to be used for a specific I/O request. A
141 * value of 0 will make cio use the opm.
142 * @key: storage key to be used for the I/O
143 * @flags: additional flags; defines the action to be performed for I/O
146 * Start a S/390 channel program. When the interrupt arrives, the
147 * IRQ handler is called, either immediately, delayed (dev-end missing,
148 * or sense required) or never (no IRQ handler registered).
150 * %0, if the operation was successful;
151 * -%EBUSY, if the device is busy, or status pending;
152 * -%EACCES, if no path specified in @lpm is operational;
153 * -%ENODEV, if the device is not operational.
155 * Interrupts disabled, ccw device lock held
157 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
158 unsigned long intparm, __u8 lpm, __u8 key,
161 struct subchannel *sch;
166 sch = to_subchannel(cdev->dev.parent);
169 if (cdev->private->state == DEV_STATE_NOT_OPER)
171 if (cdev->private->state == DEV_STATE_VERIFY ||
172 cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
173 /* Remember to fake irb when finished. */
174 if (!cdev->private->flags.fake_irb) {
175 cdev->private->flags.fake_irb = 1;
176 cdev->private->intparm = intparm;
179 /* There's already a fake I/O around. */
182 if (cdev->private->state != DEV_STATE_ONLINE ||
183 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
184 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
185 cdev->private->flags.doverify)
187 ret = cio_set_options (sch, flags);
190 /* Adjust requested path mask to excluded varied off paths. */
196 ret = cio_start_key (sch, cpa, lpm, key);
199 cdev->private->intparm = intparm;
203 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
210 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
211 * @cdev: target ccw device
212 * @cpa: logical start address of channel program
213 * @intparm: user specific interruption parameter; will be presented back to
214 * @cdev's interrupt handler. Allows a device driver to associate
215 * the interrupt with a particular I/O request.
216 * @lpm: defines the channel path to be used for a specific I/O request. A
217 * value of 0 will make cio use the opm.
218 * @key: storage key to be used for the I/O
219 * @flags: additional flags; defines the action to be performed for I/O
221 * @expires: timeout value in jiffies
223 * Start a S/390 channel program. When the interrupt arrives, the
224 * IRQ handler is called, either immediately, delayed (dev-end missing,
225 * or sense required) or never (no IRQ handler registered).
226 * This function notifies the device driver if the channel program has not
227 * completed during the time specified by @expires. If a timeout occurs, the
228 * channel program is terminated via xsch, hsch or csch, and the device's
229 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
231 * %0, if the operation was successful;
232 * -%EBUSY, if the device is busy, or status pending;
233 * -%EACCES, if no path specified in @lpm is operational;
234 * -%ENODEV, if the device is not operational.
236 * Interrupts disabled, ccw device lock held
238 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
239 unsigned long intparm, __u8 lpm, __u8 key,
240 unsigned long flags, int expires)
246 ccw_device_set_timeout(cdev, expires);
247 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
249 ccw_device_set_timeout(cdev, 0);
254 * ccw_device_start() - start a s390 channel program
255 * @cdev: target ccw device
256 * @cpa: logical start address of channel program
257 * @intparm: user specific interruption parameter; will be presented back to
258 * @cdev's interrupt handler. Allows a device driver to associate
259 * the interrupt with a particular I/O request.
260 * @lpm: defines the channel path to be used for a specific I/O request. A
261 * value of 0 will make cio use the opm.
262 * @flags: additional flags; defines the action to be performed for I/O
265 * Start a S/390 channel program. When the interrupt arrives, the
266 * IRQ handler is called, either immediately, delayed (dev-end missing,
267 * or sense required) or never (no IRQ handler registered).
269 * %0, if the operation was successful;
270 * -%EBUSY, if the device is busy, or status pending;
271 * -%EACCES, if no path specified in @lpm is operational;
272 * -%ENODEV, if the device is not operational.
274 * Interrupts disabled, ccw device lock held
276 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
277 unsigned long intparm, __u8 lpm, unsigned long flags)
279 return ccw_device_start_key(cdev, cpa, intparm, lpm,
280 PAGE_DEFAULT_KEY, flags);
284 * ccw_device_start_timeout() - start a s390 channel program with timeout
285 * @cdev: target ccw device
286 * @cpa: logical start address of channel program
287 * @intparm: user specific interruption parameter; will be presented back to
288 * @cdev's interrupt handler. Allows a device driver to associate
289 * the interrupt with a particular I/O request.
290 * @lpm: defines the channel path to be used for a specific I/O request. A
291 * value of 0 will make cio use the opm.
292 * @flags: additional flags; defines the action to be performed for I/O
294 * @expires: timeout value in jiffies
296 * Start a S/390 channel program. When the interrupt arrives, the
297 * IRQ handler is called, either immediately, delayed (dev-end missing,
298 * or sense required) or never (no IRQ handler registered).
299 * This function notifies the device driver if the channel program has not
300 * completed during the time specified by @expires. If a timeout occurs, the
301 * channel program is terminated via xsch, hsch or csch, and the device's
302 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
304 * %0, if the operation was successful;
305 * -%EBUSY, if the device is busy, or status pending;
306 * -%EACCES, if no path specified in @lpm is operational;
307 * -%ENODEV, if the device is not operational.
309 * Interrupts disabled, ccw device lock held
311 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
312 unsigned long intparm, __u8 lpm,
313 unsigned long flags, int expires)
315 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
316 PAGE_DEFAULT_KEY, flags,
322 * ccw_device_halt() - halt I/O request processing
323 * @cdev: target ccw device
324 * @intparm: interruption parameter; value is only used if no I/O is
325 * outstanding, otherwise the intparm associated with the I/O request
328 * ccw_device_halt() calls hsch on @cdev's subchannel.
331 * -%ENODEV on device not operational,
332 * -%EINVAL on invalid device state,
333 * -%EBUSY on device busy or interrupt pending.
335 * Interrupts disabled, ccw device lock held
337 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
339 struct subchannel *sch;
344 if (cdev->private->state == DEV_STATE_NOT_OPER)
346 if (cdev->private->state != DEV_STATE_ONLINE &&
347 cdev->private->state != DEV_STATE_W4SENSE)
349 sch = to_subchannel(cdev->dev.parent);
354 cdev->private->intparm = intparm;
359 * ccw_device_resume() - resume channel program execution
360 * @cdev: target ccw device
362 * ccw_device_resume() calls rsch on @cdev's subchannel.
365 * -%ENODEV on device not operational,
366 * -%EINVAL on invalid device state,
367 * -%EBUSY on device busy or interrupt pending.
369 * Interrupts disabled, ccw device lock held
371 int ccw_device_resume(struct ccw_device *cdev)
373 struct subchannel *sch;
377 sch = to_subchannel(cdev->dev.parent);
380 if (cdev->private->state == DEV_STATE_NOT_OPER)
382 if (cdev->private->state != DEV_STATE_ONLINE ||
383 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
385 return cio_resume(sch);
389 * Pass interrupt to device driver.
392 ccw_device_call_handler(struct ccw_device *cdev)
394 struct subchannel *sch;
398 sch = to_subchannel(cdev->dev.parent);
401 * we allow for the device action handler if .
402 * - we received ending status
403 * - the action handler requested to see all interrupts
404 * - we received an intermediate status
405 * - fast notification was requested (primary status)
406 * - unsolicited interrupts
408 stctl = scsw_stctl(&cdev->private->irb.scsw);
409 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
410 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
411 (stctl == SCSW_STCTL_STATUS_PEND);
412 if (!ending_status &&
413 !cdev->private->options.repall &&
414 !(stctl & SCSW_STCTL_INTER_STATUS) &&
415 !(cdev->private->options.fast &&
416 (stctl & SCSW_STCTL_PRIM_STATUS)))
419 /* Clear pending timers for device driver initiated I/O. */
421 ccw_device_set_timeout(cdev, 0);
423 * Now we are ready to call the device driver interrupt handler.
426 cdev->handler(cdev, cdev->private->intparm,
427 &cdev->private->irb);
430 * Clear the old and now useless interrupt response block.
432 memset(&cdev->private->irb, 0, sizeof(struct irb));
438 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
439 * @cdev: ccw device to inspect
440 * @ct: command type to look for
442 * During SenseID, command information words (CIWs) describing special
443 * commands available to the device may have been stored in the extended
444 * sense data. This function searches for CIWs of a specified command
445 * type in the extended sense data.
447 * %NULL if no extended sense data has been stored or if no CIW of the
448 * specified command type could be found,
449 * else a pointer to the CIW of the specified command type.
451 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
455 if (cdev->private->flags.esid == 0)
457 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
458 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
459 return cdev->private->senseid.ciw + ciw_cnt;
464 * ccw_device_get_path_mask() - get currently available paths
465 * @cdev: ccw device to be queried
467 * %0 if no subchannel for the device is available,
468 * else the mask of currently available paths for the ccw device's subchannel.
470 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
472 struct subchannel *sch;
474 sch = to_subchannel(cdev->dev.parent);
482 * Try to break the lock on a boxed device.
485 ccw_device_stlck(struct ccw_device *cdev)
489 struct subchannel *sch;
495 if (cdev->drv && !cdev->private->options.force)
498 sch = to_subchannel(cdev->dev.parent);
500 CIO_TRACE_EVENT(2, "stl lock");
501 CIO_TRACE_EVENT(2, dev_name(&cdev->dev));
503 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
506 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
511 spin_lock_irqsave(sch->lock, flags);
512 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
516 * Setup ccw. We chain an unconditional reserve and a release so we
517 * only break the lock.
519 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
520 cdev->private->iccws[0].cda = (__u32) __pa(buf);
521 cdev->private->iccws[0].count = 32;
522 cdev->private->iccws[0].flags = CCW_FLAG_CC;
523 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
524 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
525 cdev->private->iccws[1].count = 32;
526 cdev->private->iccws[1].flags = 0;
527 ret = cio_start(sch, cdev->private->iccws, 0);
529 cio_disable_subchannel(sch); //FIXME: return code?
532 cdev->private->irb.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
533 spin_unlock_irqrestore(sch->lock, flags);
534 wait_event(cdev->private->wait_q,
535 cdev->private->irb.scsw.cmd.actl == 0);
536 spin_lock_irqsave(sch->lock, flags);
537 cio_disable_subchannel(sch); //FIXME: return code?
538 if ((cdev->private->irb.scsw.cmd.dstat !=
539 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
540 (cdev->private->irb.scsw.cmd.cstat != 0))
543 memset(&cdev->private->irb, 0, sizeof(struct irb));
547 spin_unlock_irqrestore(sch->lock, flags);
551 void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
553 struct subchannel *sch;
556 sch = to_subchannel(cdev->dev.parent);
558 chpid.id = sch->schib.pmcw.chpid[chp_no];
559 return chp_get_chp_desc(chpid);
563 * ccw_device_get_id - obtain a ccw device id
564 * @cdev: device to obtain the id for
565 * @dev_id: where to fill in the values
567 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
569 *dev_id = cdev->private->dev_id;
571 EXPORT_SYMBOL(ccw_device_get_id);
574 * ccw_device_tm_start_key - perform start function
575 * @cdev: ccw device on which to perform the start function
576 * @tcw: transport-command word to be started
577 * @intparm: user defined parameter to be passed to the interrupt handler
578 * @lpm: mask of paths to use
579 * @key: storage key to use for storage access
581 * Start the tcw on the given ccw device. Return zero on success, non-zero
584 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
585 unsigned long intparm, u8 lpm, u8 key)
587 struct subchannel *sch;
590 sch = to_subchannel(cdev->dev.parent);
591 if (cdev->private->state != DEV_STATE_ONLINE)
593 /* Adjust requested path mask to excluded varied off paths. */
599 rc = cio_tm_start_key(sch, tcw, lpm, key);
601 cdev->private->intparm = intparm;
604 EXPORT_SYMBOL(ccw_device_tm_start_key);
607 * ccw_device_tm_start_timeout_key - perform start function
608 * @cdev: ccw device on which to perform the start function
609 * @tcw: transport-command word to be started
610 * @intparm: user defined parameter to be passed to the interrupt handler
611 * @lpm: mask of paths to use
612 * @key: storage key to use for storage access
613 * @expires: time span in jiffies after which to abort request
615 * Start the tcw on the given ccw device. Return zero on success, non-zero
618 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
619 unsigned long intparm, u8 lpm, u8 key,
624 ccw_device_set_timeout(cdev, expires);
625 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
627 ccw_device_set_timeout(cdev, 0);
630 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
633 * ccw_device_tm_start - perform start function
634 * @cdev: ccw device on which to perform the start function
635 * @tcw: transport-command word to be started
636 * @intparm: user defined parameter to be passed to the interrupt handler
637 * @lpm: mask of paths to use
639 * Start the tcw on the given ccw device. Return zero on success, non-zero
642 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
643 unsigned long intparm, u8 lpm)
645 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
648 EXPORT_SYMBOL(ccw_device_tm_start);
651 * ccw_device_tm_start_timeout - perform start function
652 * @cdev: ccw device on which to perform the start function
653 * @tcw: transport-command word to be started
654 * @intparm: user defined parameter to be passed to the interrupt handler
655 * @lpm: mask of paths to use
656 * @expires: time span in jiffies after which to abort request
658 * Start the tcw on the given ccw device. Return zero on success, non-zero
661 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
662 unsigned long intparm, u8 lpm, int expires)
664 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
665 PAGE_DEFAULT_KEY, expires);
667 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
670 * ccw_device_tm_intrg - perform interrogate function
671 * @cdev: ccw device on which to perform the interrogate function
673 * Perform an interrogate function on the given ccw device. Return zero on
674 * success, non-zero otherwise.
676 int ccw_device_tm_intrg(struct ccw_device *cdev)
678 struct subchannel *sch = to_subchannel(cdev->dev.parent);
680 if (cdev->private->state != DEV_STATE_ONLINE)
682 if (!scsw_is_tm(&sch->schib.scsw) ||
683 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
685 return cio_tm_intrg(sch);
687 EXPORT_SYMBOL(ccw_device_tm_intrg);
689 // FIXME: these have to go:
692 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
694 return cdev->private->schid.sch_no;
698 MODULE_LICENSE("GPL");
699 EXPORT_SYMBOL(ccw_device_set_options_mask);
700 EXPORT_SYMBOL(ccw_device_set_options);
701 EXPORT_SYMBOL(ccw_device_clear_options);
702 EXPORT_SYMBOL(ccw_device_clear);
703 EXPORT_SYMBOL(ccw_device_halt);
704 EXPORT_SYMBOL(ccw_device_resume);
705 EXPORT_SYMBOL(ccw_device_start_timeout);
706 EXPORT_SYMBOL(ccw_device_start);
707 EXPORT_SYMBOL(ccw_device_start_timeout_key);
708 EXPORT_SYMBOL(ccw_device_start_key);
709 EXPORT_SYMBOL(ccw_device_get_ciw);
710 EXPORT_SYMBOL(ccw_device_get_path_mask);
711 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
712 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);