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;
53 * ccw_device_set_options() - set some options
54 * @cdev: device for which the options are to be set
55 * @flags: options to be set
57 * All flags specified in @flags are set, the remainder is left untouched.
59 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
61 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
64 * The flag usage is mutal exclusive ...
66 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
67 (flags & CCWDEV_REPORT_ALL)) ||
68 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
69 cdev->private->options.repall) ||
70 ((flags & CCWDEV_REPORT_ALL) &&
71 cdev->private->options.fast))
73 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
74 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
75 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
76 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
81 * ccw_device_clear_options() - clear some options
82 * @cdev: device for which the options are to be cleared
83 * @flags: options to be cleared
85 * All flags specified in @flags are cleared, the remainder is left untouched.
87 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
89 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
90 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
91 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
92 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
96 * ccw_device_clear() - terminate I/O request processing
97 * @cdev: target ccw device
98 * @intparm: interruption parameter; value is only used if no I/O is
99 * outstanding, otherwise the intparm associated with the I/O request
102 * ccw_device_clear() calls csch on @cdev's subchannel.
105 * -%ENODEV on device not operational,
106 * -%EINVAL on invalid device state.
108 * Interrupts disabled, ccw device lock held
110 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
112 struct subchannel *sch;
115 if (!cdev || !cdev->dev.parent)
117 sch = to_subchannel(cdev->dev.parent);
118 if (!sch->schib.pmcw.ena)
120 if (cdev->private->state == DEV_STATE_NOT_OPER)
122 if (cdev->private->state != DEV_STATE_ONLINE &&
123 cdev->private->state != DEV_STATE_W4SENSE)
126 ret = cio_clear(sch);
128 cdev->private->intparm = intparm;
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
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).
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.
154 * Interrupts disabled, ccw device lock held
156 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
157 unsigned long intparm, __u8 lpm, __u8 key,
160 struct subchannel *sch;
163 if (!cdev || !cdev->dev.parent)
165 sch = to_subchannel(cdev->dev.parent);
166 if (!sch->schib.pmcw.ena)
168 if (cdev->private->state == DEV_STATE_NOT_OPER)
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;
178 /* There's already a fake I/O around. */
181 if (cdev->private->state != DEV_STATE_ONLINE ||
182 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
183 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
184 cdev->private->flags.doverify)
186 ret = cio_set_options (sch, flags);
189 /* Adjust requested path mask to excluded varied off paths. */
195 ret = cio_start_key (sch, cpa, lpm, key);
198 cdev->private->intparm = intparm;
202 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
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
220 * @expires: timeout value in jiffies
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).
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.
235 * Interrupts disabled, ccw device lock held
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)
245 ccw_device_set_timeout(cdev, expires);
246 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
248 ccw_device_set_timeout(cdev, 0);
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
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).
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.
273 * Interrupts disabled, ccw device lock held
275 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
276 unsigned long intparm, __u8 lpm, unsigned long flags)
278 return ccw_device_start_key(cdev, cpa, intparm, lpm,
279 PAGE_DEFAULT_KEY, flags);
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
293 * @expires: timeout value in jiffies
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).
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.
308 * Interrupts disabled, ccw device lock held
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)
314 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
315 PAGE_DEFAULT_KEY, flags,
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
327 * ccw_device_halt() calls hsch on @cdev's subchannel.
330 * -%ENODEV on device not operational,
331 * -%EINVAL on invalid device state,
332 * -%EBUSY on device busy or interrupt pending.
334 * Interrupts disabled, ccw device lock held
336 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
338 struct subchannel *sch;
341 if (!cdev || !cdev->dev.parent)
343 sch = to_subchannel(cdev->dev.parent);
344 if (!sch->schib.pmcw.ena)
346 if (cdev->private->state == DEV_STATE_NOT_OPER)
348 if (cdev->private->state != DEV_STATE_ONLINE &&
349 cdev->private->state != DEV_STATE_W4SENSE)
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;
375 if (!cdev || !cdev->dev.parent)
377 sch = to_subchannel(cdev->dev.parent);
378 if (!sch->schib.pmcw.ena)
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 if (!cdev->dev.parent)
477 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 (!sch->schib.pmcw.ena)
593 if (cdev->private->state != DEV_STATE_ONLINE)
595 /* Adjust requested path mask to excluded varied off paths. */
601 rc = cio_tm_start_key(sch, tcw, lpm, key);
603 cdev->private->intparm = intparm;
606 EXPORT_SYMBOL(ccw_device_tm_start_key);
609 * ccw_device_tm_start_timeout_key - perform start function
610 * @cdev: ccw device on which to perform the start function
611 * @tcw: transport-command word to be started
612 * @intparm: user defined parameter to be passed to the interrupt handler
613 * @lpm: mask of paths to use
614 * @key: storage key to use for storage access
615 * @expires: time span in jiffies after which to abort request
617 * Start the tcw on the given ccw device. Return zero on success, non-zero
620 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
621 unsigned long intparm, u8 lpm, u8 key,
626 ccw_device_set_timeout(cdev, expires);
627 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
629 ccw_device_set_timeout(cdev, 0);
632 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
635 * ccw_device_tm_start - 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
641 * Start the tcw on the given ccw device. Return zero on success, non-zero
644 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
645 unsigned long intparm, u8 lpm)
647 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
650 EXPORT_SYMBOL(ccw_device_tm_start);
653 * ccw_device_tm_start_timeout - perform start function
654 * @cdev: ccw device on which to perform the start function
655 * @tcw: transport-command word to be started
656 * @intparm: user defined parameter to be passed to the interrupt handler
657 * @lpm: mask of paths to use
658 * @expires: time span in jiffies after which to abort request
660 * Start the tcw on the given ccw device. Return zero on success, non-zero
663 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
664 unsigned long intparm, u8 lpm, int expires)
666 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
667 PAGE_DEFAULT_KEY, expires);
669 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
672 * ccw_device_tm_intrg - perform interrogate function
673 * @cdev: ccw device on which to perform the interrogate function
675 * Perform an interrogate function on the given ccw device. Return zero on
676 * success, non-zero otherwise.
678 int ccw_device_tm_intrg(struct ccw_device *cdev)
680 struct subchannel *sch = to_subchannel(cdev->dev.parent);
682 if (!sch->schib.pmcw.ena)
684 if (cdev->private->state != DEV_STATE_ONLINE)
686 if (!scsw_is_tm(&sch->schib.scsw) ||
687 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
689 return cio_tm_intrg(sch);
691 EXPORT_SYMBOL(ccw_device_tm_intrg);
693 // FIXME: these have to go:
696 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
698 return cdev->private->schid.sch_no;
702 MODULE_LICENSE("GPL");
703 EXPORT_SYMBOL(ccw_device_set_options_mask);
704 EXPORT_SYMBOL(ccw_device_set_options);
705 EXPORT_SYMBOL(ccw_device_clear_options);
706 EXPORT_SYMBOL(ccw_device_clear);
707 EXPORT_SYMBOL(ccw_device_halt);
708 EXPORT_SYMBOL(ccw_device_resume);
709 EXPORT_SYMBOL(ccw_device_start_timeout);
710 EXPORT_SYMBOL(ccw_device_start);
711 EXPORT_SYMBOL(ccw_device_start_timeout_key);
712 EXPORT_SYMBOL(ccw_device_start_key);
713 EXPORT_SYMBOL(ccw_device_get_ciw);
714 EXPORT_SYMBOL(ccw_device_get_path_mask);
715 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
716 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);