2 * Copyright IBM Corp. 2002, 2009
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
5 * Cornelia Huck (cornelia.huck@de.ibm.com)
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
15 #include <asm/ccwdev.h>
16 #include <asm/idals.h>
17 #include <asm/chpid.h>
21 #include "cio_debug.h"
28 * ccw_device_set_options_mask() - set some options and unset the rest
29 * @cdev: device for which the options are to be set
30 * @flags: options to be set
32 * All flags specified in @flags are set, all flags not specified in @flags
35 * %0 on success, -%EINVAL on an invalid flag combination.
37 int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
40 * The flag usage is mutal exclusive ...
42 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
43 (flags & CCWDEV_REPORT_ALL))
45 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
46 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
47 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
48 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
49 cdev->private->options.mpath = (flags & CCWDEV_DO_MULTIPATH) != 0;
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
58 * All flags specified in @flags are set, the remainder is left untouched.
60 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
62 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
65 * The flag usage is mutal exclusive ...
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))
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 cdev->private->options.mpath |= (flags & CCWDEV_DO_MULTIPATH) != 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;
95 cdev->private->options.mpath &= (flags & CCWDEV_DO_MULTIPATH) == 0;
99 * ccw_device_is_pathgroup - determine if paths to this device are grouped
102 * Return non-zero if there is a path group, zero otherwise.
104 int ccw_device_is_pathgroup(struct ccw_device *cdev)
106 return cdev->private->flags.pgroup;
108 EXPORT_SYMBOL(ccw_device_is_pathgroup);
111 * ccw_device_is_multipath - determine if device is operating in multipath mode
114 * Return non-zero if device is operating in multipath mode, zero otherwise.
116 int ccw_device_is_multipath(struct ccw_device *cdev)
118 return cdev->private->flags.mpath;
120 EXPORT_SYMBOL(ccw_device_is_multipath);
123 * ccw_device_clear() - terminate I/O request processing
124 * @cdev: target ccw device
125 * @intparm: interruption parameter; value is only used if no I/O is
126 * outstanding, otherwise the intparm associated with the I/O request
129 * ccw_device_clear() calls csch on @cdev's subchannel.
132 * -%ENODEV on device not operational,
133 * -%EINVAL on invalid device state.
135 * Interrupts disabled, ccw device lock held
137 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
139 struct subchannel *sch;
142 if (!cdev || !cdev->dev.parent)
144 sch = to_subchannel(cdev->dev.parent);
145 if (!sch->schib.pmcw.ena)
147 if (cdev->private->state == DEV_STATE_NOT_OPER)
149 if (cdev->private->state != DEV_STATE_ONLINE &&
150 cdev->private->state != DEV_STATE_W4SENSE)
153 ret = cio_clear(sch);
155 cdev->private->intparm = intparm;
160 * ccw_device_start_key() - start a s390 channel program with key
161 * @cdev: target ccw device
162 * @cpa: logical start address of channel program
163 * @intparm: user specific interruption parameter; will be presented back to
164 * @cdev's interrupt handler. Allows a device driver to associate
165 * the interrupt with a particular I/O request.
166 * @lpm: defines the channel path to be used for a specific I/O request. A
167 * value of 0 will make cio use the opm.
168 * @key: storage key to be used for the I/O
169 * @flags: additional flags; defines the action to be performed for I/O
172 * Start a S/390 channel program. When the interrupt arrives, the
173 * IRQ handler is called, either immediately, delayed (dev-end missing,
174 * or sense required) or never (no IRQ handler registered).
176 * %0, if the operation was successful;
177 * -%EBUSY, if the device is busy, or status pending;
178 * -%EACCES, if no path specified in @lpm is operational;
179 * -%ENODEV, if the device is not operational.
181 * Interrupts disabled, ccw device lock held
183 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
184 unsigned long intparm, __u8 lpm, __u8 key,
187 struct subchannel *sch;
190 if (!cdev || !cdev->dev.parent)
192 sch = to_subchannel(cdev->dev.parent);
193 if (!sch->schib.pmcw.ena)
195 if (cdev->private->state == DEV_STATE_NOT_OPER)
197 if (cdev->private->state == DEV_STATE_VERIFY) {
198 /* Remember to fake irb when finished. */
199 if (!cdev->private->flags.fake_irb) {
200 cdev->private->flags.fake_irb = 1;
201 cdev->private->intparm = intparm;
204 /* There's already a fake I/O around. */
207 if (cdev->private->state != DEV_STATE_ONLINE ||
208 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
209 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
210 cdev->private->flags.doverify)
212 ret = cio_set_options (sch, flags);
215 /* Adjust requested path mask to excluded varied off paths. */
221 ret = cio_start_key (sch, cpa, lpm, key);
224 cdev->private->intparm = intparm;
228 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
235 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
236 * @cdev: target ccw device
237 * @cpa: logical start address of channel program
238 * @intparm: user specific interruption parameter; will be presented back to
239 * @cdev's interrupt handler. Allows a device driver to associate
240 * the interrupt with a particular I/O request.
241 * @lpm: defines the channel path to be used for a specific I/O request. A
242 * value of 0 will make cio use the opm.
243 * @key: storage key to be used for the I/O
244 * @flags: additional flags; defines the action to be performed for I/O
246 * @expires: timeout value in jiffies
248 * Start a S/390 channel program. When the interrupt arrives, the
249 * IRQ handler is called, either immediately, delayed (dev-end missing,
250 * or sense required) or never (no IRQ handler registered).
251 * This function notifies the device driver if the channel program has not
252 * completed during the time specified by @expires. If a timeout occurs, the
253 * channel program is terminated via xsch, hsch or csch, and the device's
254 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
256 * %0, if the operation was successful;
257 * -%EBUSY, if the device is busy, or status pending;
258 * -%EACCES, if no path specified in @lpm is operational;
259 * -%ENODEV, if the device is not operational.
261 * Interrupts disabled, ccw device lock held
263 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
264 unsigned long intparm, __u8 lpm, __u8 key,
265 unsigned long flags, int expires)
271 ccw_device_set_timeout(cdev, expires);
272 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
274 ccw_device_set_timeout(cdev, 0);
279 * ccw_device_start() - start a s390 channel program
280 * @cdev: target ccw device
281 * @cpa: logical start address of channel program
282 * @intparm: user specific interruption parameter; will be presented back to
283 * @cdev's interrupt handler. Allows a device driver to associate
284 * the interrupt with a particular I/O request.
285 * @lpm: defines the channel path to be used for a specific I/O request. A
286 * value of 0 will make cio use the opm.
287 * @flags: additional flags; defines the action to be performed for I/O
290 * Start a S/390 channel program. When the interrupt arrives, the
291 * IRQ handler is called, either immediately, delayed (dev-end missing,
292 * or sense required) or never (no IRQ handler registered).
294 * %0, if the operation was successful;
295 * -%EBUSY, if the device is busy, or status pending;
296 * -%EACCES, if no path specified in @lpm is operational;
297 * -%ENODEV, if the device is not operational.
299 * Interrupts disabled, ccw device lock held
301 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
302 unsigned long intparm, __u8 lpm, unsigned long flags)
304 return ccw_device_start_key(cdev, cpa, intparm, lpm,
305 PAGE_DEFAULT_KEY, flags);
309 * ccw_device_start_timeout() - start a s390 channel program with timeout
310 * @cdev: target ccw device
311 * @cpa: logical start address of channel program
312 * @intparm: user specific interruption parameter; will be presented back to
313 * @cdev's interrupt handler. Allows a device driver to associate
314 * the interrupt with a particular I/O request.
315 * @lpm: defines the channel path to be used for a specific I/O request. A
316 * value of 0 will make cio use the opm.
317 * @flags: additional flags; defines the action to be performed for I/O
319 * @expires: timeout value in jiffies
321 * Start a S/390 channel program. When the interrupt arrives, the
322 * IRQ handler is called, either immediately, delayed (dev-end missing,
323 * or sense required) or never (no IRQ handler registered).
324 * This function notifies the device driver if the channel program has not
325 * completed during the time specified by @expires. If a timeout occurs, the
326 * channel program is terminated via xsch, hsch or csch, and the device's
327 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
329 * %0, if the operation was successful;
330 * -%EBUSY, if the device is busy, or status pending;
331 * -%EACCES, if no path specified in @lpm is operational;
332 * -%ENODEV, if the device is not operational.
334 * Interrupts disabled, ccw device lock held
336 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
337 unsigned long intparm, __u8 lpm,
338 unsigned long flags, int expires)
340 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
341 PAGE_DEFAULT_KEY, flags,
347 * ccw_device_halt() - halt I/O request processing
348 * @cdev: target ccw device
349 * @intparm: interruption parameter; value is only used if no I/O is
350 * outstanding, otherwise the intparm associated with the I/O request
353 * ccw_device_halt() calls hsch on @cdev's subchannel.
356 * -%ENODEV on device not operational,
357 * -%EINVAL on invalid device state,
358 * -%EBUSY on device busy or interrupt pending.
360 * Interrupts disabled, ccw device lock held
362 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
364 struct subchannel *sch;
367 if (!cdev || !cdev->dev.parent)
369 sch = to_subchannel(cdev->dev.parent);
370 if (!sch->schib.pmcw.ena)
372 if (cdev->private->state == DEV_STATE_NOT_OPER)
374 if (cdev->private->state != DEV_STATE_ONLINE &&
375 cdev->private->state != DEV_STATE_W4SENSE)
380 cdev->private->intparm = intparm;
385 * ccw_device_resume() - resume channel program execution
386 * @cdev: target ccw device
388 * ccw_device_resume() calls rsch on @cdev's subchannel.
391 * -%ENODEV on device not operational,
392 * -%EINVAL on invalid device state,
393 * -%EBUSY on device busy or interrupt pending.
395 * Interrupts disabled, ccw device lock held
397 int ccw_device_resume(struct ccw_device *cdev)
399 struct subchannel *sch;
401 if (!cdev || !cdev->dev.parent)
403 sch = to_subchannel(cdev->dev.parent);
404 if (!sch->schib.pmcw.ena)
406 if (cdev->private->state == DEV_STATE_NOT_OPER)
408 if (cdev->private->state != DEV_STATE_ONLINE ||
409 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
411 return cio_resume(sch);
415 * Pass interrupt to device driver.
418 ccw_device_call_handler(struct ccw_device *cdev)
420 struct subchannel *sch;
424 sch = to_subchannel(cdev->dev.parent);
427 * we allow for the device action handler if .
428 * - we received ending status
429 * - the action handler requested to see all interrupts
430 * - we received an intermediate status
431 * - fast notification was requested (primary status)
432 * - unsolicited interrupts
434 stctl = scsw_stctl(&cdev->private->irb.scsw);
435 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
436 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
437 (stctl == SCSW_STCTL_STATUS_PEND);
438 if (!ending_status &&
439 !cdev->private->options.repall &&
440 !(stctl & SCSW_STCTL_INTER_STATUS) &&
441 !(cdev->private->options.fast &&
442 (stctl & SCSW_STCTL_PRIM_STATUS)))
445 /* Clear pending timers for device driver initiated I/O. */
447 ccw_device_set_timeout(cdev, 0);
449 * Now we are ready to call the device driver interrupt handler.
452 cdev->handler(cdev, cdev->private->intparm,
453 &cdev->private->irb);
456 * Clear the old and now useless interrupt response block.
458 memset(&cdev->private->irb, 0, sizeof(struct irb));
464 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
465 * @cdev: ccw device to inspect
466 * @ct: command type to look for
468 * During SenseID, command information words (CIWs) describing special
469 * commands available to the device may have been stored in the extended
470 * sense data. This function searches for CIWs of a specified command
471 * type in the extended sense data.
473 * %NULL if no extended sense data has been stored or if no CIW of the
474 * specified command type could be found,
475 * else a pointer to the CIW of the specified command type.
477 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
481 if (cdev->private->flags.esid == 0)
483 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
484 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
485 return cdev->private->senseid.ciw + ciw_cnt;
490 * ccw_device_get_path_mask() - get currently available paths
491 * @cdev: ccw device to be queried
493 * %0 if no subchannel for the device is available,
494 * else the mask of currently available paths for the ccw device's subchannel.
496 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
498 struct subchannel *sch;
500 if (!cdev->dev.parent)
503 sch = to_subchannel(cdev->dev.parent);
508 * Try to break the lock on a boxed device.
511 ccw_device_stlck(struct ccw_device *cdev)
515 struct subchannel *sch;
521 if (cdev->drv && !cdev->private->options.force)
524 sch = to_subchannel(cdev->dev.parent);
526 CIO_TRACE_EVENT(2, "stl lock");
527 CIO_TRACE_EVENT(2, dev_name(&cdev->dev));
529 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
532 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
537 spin_lock_irqsave(sch->lock, flags);
538 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
542 * Setup ccw. We chain an unconditional reserve and a release so we
543 * only break the lock.
545 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
546 cdev->private->iccws[0].cda = (__u32) __pa(buf);
547 cdev->private->iccws[0].count = 32;
548 cdev->private->iccws[0].flags = CCW_FLAG_CC;
549 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
550 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
551 cdev->private->iccws[1].count = 32;
552 cdev->private->iccws[1].flags = 0;
553 ret = cio_start(sch, cdev->private->iccws, 0);
555 cio_disable_subchannel(sch); //FIXME: return code?
558 cdev->private->irb.scsw.cmd.actl |= SCSW_ACTL_START_PEND;
559 spin_unlock_irqrestore(sch->lock, flags);
560 wait_event(cdev->private->wait_q,
561 cdev->private->irb.scsw.cmd.actl == 0);
562 spin_lock_irqsave(sch->lock, flags);
563 cio_disable_subchannel(sch); //FIXME: return code?
564 if ((cdev->private->irb.scsw.cmd.dstat !=
565 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
566 (cdev->private->irb.scsw.cmd.cstat != 0))
569 memset(&cdev->private->irb, 0, sizeof(struct irb));
573 spin_unlock_irqrestore(sch->lock, flags);
577 void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
579 struct subchannel *sch;
582 sch = to_subchannel(cdev->dev.parent);
584 chpid.id = sch->schib.pmcw.chpid[chp_no];
585 return chp_get_chp_desc(chpid);
589 * ccw_device_get_id - obtain a ccw device id
590 * @cdev: device to obtain the id for
591 * @dev_id: where to fill in the values
593 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
595 *dev_id = cdev->private->dev_id;
597 EXPORT_SYMBOL(ccw_device_get_id);
600 * ccw_device_tm_start_key - perform start function
601 * @cdev: ccw device on which to perform the start function
602 * @tcw: transport-command word to be started
603 * @intparm: user defined parameter to be passed to the interrupt handler
604 * @lpm: mask of paths to use
605 * @key: storage key to use for storage access
607 * Start the tcw on the given ccw device. Return zero on success, non-zero
610 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
611 unsigned long intparm, u8 lpm, u8 key)
613 struct subchannel *sch;
616 sch = to_subchannel(cdev->dev.parent);
617 if (!sch->schib.pmcw.ena)
619 if (cdev->private->state != DEV_STATE_ONLINE)
621 /* Adjust requested path mask to excluded varied off paths. */
627 rc = cio_tm_start_key(sch, tcw, lpm, key);
629 cdev->private->intparm = intparm;
632 EXPORT_SYMBOL(ccw_device_tm_start_key);
635 * ccw_device_tm_start_timeout_key - perform start function
636 * @cdev: ccw device on which to perform the start function
637 * @tcw: transport-command word to be started
638 * @intparm: user defined parameter to be passed to the interrupt handler
639 * @lpm: mask of paths to use
640 * @key: storage key to use for storage access
641 * @expires: time span in jiffies after which to abort request
643 * Start the tcw on the given ccw device. Return zero on success, non-zero
646 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
647 unsigned long intparm, u8 lpm, u8 key,
652 ccw_device_set_timeout(cdev, expires);
653 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
655 ccw_device_set_timeout(cdev, 0);
658 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
661 * ccw_device_tm_start - perform start function
662 * @cdev: ccw device on which to perform the start function
663 * @tcw: transport-command word to be started
664 * @intparm: user defined parameter to be passed to the interrupt handler
665 * @lpm: mask of paths to use
667 * Start the tcw on the given ccw device. Return zero on success, non-zero
670 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
671 unsigned long intparm, u8 lpm)
673 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
676 EXPORT_SYMBOL(ccw_device_tm_start);
679 * ccw_device_tm_start_timeout - perform start function
680 * @cdev: ccw device on which to perform the start function
681 * @tcw: transport-command word to be started
682 * @intparm: user defined parameter to be passed to the interrupt handler
683 * @lpm: mask of paths to use
684 * @expires: time span in jiffies after which to abort request
686 * Start the tcw on the given ccw device. Return zero on success, non-zero
689 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
690 unsigned long intparm, u8 lpm, int expires)
692 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
693 PAGE_DEFAULT_KEY, expires);
695 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
698 * ccw_device_tm_intrg - perform interrogate function
699 * @cdev: ccw device on which to perform the interrogate function
701 * Perform an interrogate function on the given ccw device. Return zero on
702 * success, non-zero otherwise.
704 int ccw_device_tm_intrg(struct ccw_device *cdev)
706 struct subchannel *sch = to_subchannel(cdev->dev.parent);
708 if (!sch->schib.pmcw.ena)
710 if (cdev->private->state != DEV_STATE_ONLINE)
712 if (!scsw_is_tm(&sch->schib.scsw) ||
713 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
715 return cio_tm_intrg(sch);
717 EXPORT_SYMBOL(ccw_device_tm_intrg);
719 // FIXME: these have to go:
722 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
724 return cdev->private->schid.sch_no;
728 MODULE_LICENSE("GPL");
729 EXPORT_SYMBOL(ccw_device_set_options_mask);
730 EXPORT_SYMBOL(ccw_device_set_options);
731 EXPORT_SYMBOL(ccw_device_clear_options);
732 EXPORT_SYMBOL(ccw_device_clear);
733 EXPORT_SYMBOL(ccw_device_halt);
734 EXPORT_SYMBOL(ccw_device_resume);
735 EXPORT_SYMBOL(ccw_device_start_timeout);
736 EXPORT_SYMBOL(ccw_device_start);
737 EXPORT_SYMBOL(ccw_device_start_timeout_key);
738 EXPORT_SYMBOL(ccw_device_start_key);
739 EXPORT_SYMBOL(ccw_device_get_ciw);
740 EXPORT_SYMBOL(ccw_device_get_path_mask);
741 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
742 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);