2 * Copyright IBM Corp. 2002, 2009
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
5 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 #include <linux/export.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 #include <linux/completion.h>
18 #include <asm/ccwdev.h>
19 #include <asm/idals.h>
20 #include <asm/chpid.h>
24 #include "cio_debug.h"
31 * ccw_device_set_options_mask() - set some options and unset the rest
32 * @cdev: device for which the options are to be set
33 * @flags: options to be set
35 * All flags specified in @flags are set, all flags not specified in @flags
38 * %0 on success, -%EINVAL on an invalid flag combination.
40 int ccw_device_set_options_mask(struct ccw_device
*cdev
, unsigned long flags
)
43 * The flag usage is mutal exclusive ...
45 if ((flags
& CCWDEV_EARLY_NOTIFICATION
) &&
46 (flags
& CCWDEV_REPORT_ALL
))
48 cdev
->private->options
.fast
= (flags
& CCWDEV_EARLY_NOTIFICATION
) != 0;
49 cdev
->private->options
.repall
= (flags
& CCWDEV_REPORT_ALL
) != 0;
50 cdev
->private->options
.pgroup
= (flags
& CCWDEV_DO_PATHGROUP
) != 0;
51 cdev
->private->options
.force
= (flags
& CCWDEV_ALLOW_FORCE
) != 0;
52 cdev
->private->options
.mpath
= (flags
& CCWDEV_DO_MULTIPATH
) != 0;
57 * ccw_device_set_options() - set some options
58 * @cdev: device for which the options are to be set
59 * @flags: options to be set
61 * All flags specified in @flags are set, the remainder is left untouched.
63 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
65 int ccw_device_set_options(struct ccw_device
*cdev
, unsigned long flags
)
68 * The flag usage is mutal exclusive ...
70 if (((flags
& CCWDEV_EARLY_NOTIFICATION
) &&
71 (flags
& CCWDEV_REPORT_ALL
)) ||
72 ((flags
& CCWDEV_EARLY_NOTIFICATION
) &&
73 cdev
->private->options
.repall
) ||
74 ((flags
& CCWDEV_REPORT_ALL
) &&
75 cdev
->private->options
.fast
))
77 cdev
->private->options
.fast
|= (flags
& CCWDEV_EARLY_NOTIFICATION
) != 0;
78 cdev
->private->options
.repall
|= (flags
& CCWDEV_REPORT_ALL
) != 0;
79 cdev
->private->options
.pgroup
|= (flags
& CCWDEV_DO_PATHGROUP
) != 0;
80 cdev
->private->options
.force
|= (flags
& CCWDEV_ALLOW_FORCE
) != 0;
81 cdev
->private->options
.mpath
|= (flags
& CCWDEV_DO_MULTIPATH
) != 0;
86 * ccw_device_clear_options() - clear some options
87 * @cdev: device for which the options are to be cleared
88 * @flags: options to be cleared
90 * All flags specified in @flags are cleared, the remainder is left untouched.
92 void ccw_device_clear_options(struct ccw_device
*cdev
, unsigned long flags
)
94 cdev
->private->options
.fast
&= (flags
& CCWDEV_EARLY_NOTIFICATION
) == 0;
95 cdev
->private->options
.repall
&= (flags
& CCWDEV_REPORT_ALL
) == 0;
96 cdev
->private->options
.pgroup
&= (flags
& CCWDEV_DO_PATHGROUP
) == 0;
97 cdev
->private->options
.force
&= (flags
& CCWDEV_ALLOW_FORCE
) == 0;
98 cdev
->private->options
.mpath
&= (flags
& CCWDEV_DO_MULTIPATH
) == 0;
102 * ccw_device_is_pathgroup() - determine if paths to this device are grouped
105 * Return non-zero if there is a path group, zero otherwise.
107 int ccw_device_is_pathgroup(struct ccw_device
*cdev
)
109 return cdev
->private->flags
.pgroup
;
111 EXPORT_SYMBOL(ccw_device_is_pathgroup
);
114 * ccw_device_is_multipath() - determine if device is operating in multipath mode
117 * Return non-zero if device is operating in multipath mode, zero otherwise.
119 int ccw_device_is_multipath(struct ccw_device
*cdev
)
121 return cdev
->private->flags
.mpath
;
123 EXPORT_SYMBOL(ccw_device_is_multipath
);
126 * ccw_device_clear() - terminate I/O request processing
127 * @cdev: target ccw device
128 * @intparm: interruption parameter; value is only used if no I/O is
129 * outstanding, otherwise the intparm associated with the I/O request
132 * ccw_device_clear() calls csch on @cdev's subchannel.
135 * -%ENODEV on device not operational,
136 * -%EINVAL on invalid device state.
138 * Interrupts disabled, ccw device lock held
140 int ccw_device_clear(struct ccw_device
*cdev
, unsigned long intparm
)
142 struct subchannel
*sch
;
145 if (!cdev
|| !cdev
->dev
.parent
)
147 sch
= to_subchannel(cdev
->dev
.parent
);
148 if (!sch
->schib
.pmcw
.ena
)
150 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
152 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
153 cdev
->private->state
!= DEV_STATE_W4SENSE
)
156 ret
= cio_clear(sch
);
158 cdev
->private->intparm
= intparm
;
163 * ccw_device_start_key() - start a s390 channel program with key
164 * @cdev: target ccw device
165 * @cpa: logical start address of channel program
166 * @intparm: user specific interruption parameter; will be presented back to
167 * @cdev's interrupt handler. Allows a device driver to associate
168 * the interrupt with a particular I/O request.
169 * @lpm: defines the channel path to be used for a specific I/O request. A
170 * value of 0 will make cio use the opm.
171 * @key: storage key to be used for the I/O
172 * @flags: additional flags; defines the action to be performed for I/O
175 * Start a S/390 channel program. When the interrupt arrives, the
176 * IRQ handler is called, either immediately, delayed (dev-end missing,
177 * or sense required) or never (no IRQ handler registered).
179 * %0, if the operation was successful;
180 * -%EBUSY, if the device is busy, or status pending;
181 * -%EACCES, if no path specified in @lpm is operational;
182 * -%ENODEV, if the device is not operational.
184 * Interrupts disabled, ccw device lock held
186 int ccw_device_start_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
187 unsigned long intparm
, __u8 lpm
, __u8 key
,
190 struct subchannel
*sch
;
193 if (!cdev
|| !cdev
->dev
.parent
)
195 sch
= to_subchannel(cdev
->dev
.parent
);
196 if (!sch
->schib
.pmcw
.ena
)
198 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
200 if (cdev
->private->state
== DEV_STATE_VERIFY
) {
201 /* Remember to fake irb when finished. */
202 if (!cdev
->private->flags
.fake_irb
) {
203 cdev
->private->flags
.fake_irb
= FAKE_CMD_IRB
;
204 cdev
->private->intparm
= intparm
;
207 /* There's already a fake I/O around. */
210 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
211 ((sch
->schib
.scsw
.cmd
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
212 !(sch
->schib
.scsw
.cmd
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
213 cdev
->private->flags
.doverify
)
215 ret
= cio_set_options (sch
, flags
);
218 /* Adjust requested path mask to exclude unusable paths. */
224 ret
= cio_start_key (sch
, cpa
, lpm
, key
);
227 cdev
->private->intparm
= intparm
;
231 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
238 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
239 * @cdev: target ccw device
240 * @cpa: logical start address of channel program
241 * @intparm: user specific interruption parameter; will be presented back to
242 * @cdev's interrupt handler. Allows a device driver to associate
243 * the interrupt with a particular I/O request.
244 * @lpm: defines the channel path to be used for a specific I/O request. A
245 * value of 0 will make cio use the opm.
246 * @key: storage key to be used for the I/O
247 * @flags: additional flags; defines the action to be performed for I/O
249 * @expires: timeout value in jiffies
251 * Start a S/390 channel program. When the interrupt arrives, the
252 * IRQ handler is called, either immediately, delayed (dev-end missing,
253 * or sense required) or never (no IRQ handler registered).
254 * This function notifies the device driver if the channel program has not
255 * completed during the time specified by @expires. If a timeout occurs, the
256 * channel program is terminated via xsch, hsch or csch, and the device's
257 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
259 * %0, if the operation was successful;
260 * -%EBUSY, if the device is busy, or status pending;
261 * -%EACCES, if no path specified in @lpm is operational;
262 * -%ENODEV, if the device is not operational.
264 * Interrupts disabled, ccw device lock held
266 int ccw_device_start_timeout_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
267 unsigned long intparm
, __u8 lpm
, __u8 key
,
268 unsigned long flags
, int expires
)
274 ccw_device_set_timeout(cdev
, expires
);
275 ret
= ccw_device_start_key(cdev
, cpa
, intparm
, lpm
, key
, flags
);
277 ccw_device_set_timeout(cdev
, 0);
282 * ccw_device_start() - start a s390 channel program
283 * @cdev: target ccw device
284 * @cpa: logical start address of channel program
285 * @intparm: user specific interruption parameter; will be presented back to
286 * @cdev's interrupt handler. Allows a device driver to associate
287 * the interrupt with a particular I/O request.
288 * @lpm: defines the channel path to be used for a specific I/O request. A
289 * value of 0 will make cio use the opm.
290 * @flags: additional flags; defines the action to be performed for I/O
293 * Start a S/390 channel program. When the interrupt arrives, the
294 * IRQ handler is called, either immediately, delayed (dev-end missing,
295 * or sense required) or never (no IRQ handler registered).
297 * %0, if the operation was successful;
298 * -%EBUSY, if the device is busy, or status pending;
299 * -%EACCES, if no path specified in @lpm is operational;
300 * -%ENODEV, if the device is not operational.
302 * Interrupts disabled, ccw device lock held
304 int ccw_device_start(struct ccw_device
*cdev
, struct ccw1
*cpa
,
305 unsigned long intparm
, __u8 lpm
, unsigned long flags
)
307 return ccw_device_start_key(cdev
, cpa
, intparm
, lpm
,
308 PAGE_DEFAULT_KEY
, flags
);
312 * ccw_device_start_timeout() - start a s390 channel program with timeout
313 * @cdev: target ccw device
314 * @cpa: logical start address of channel program
315 * @intparm: user specific interruption parameter; will be presented back to
316 * @cdev's interrupt handler. Allows a device driver to associate
317 * the interrupt with a particular I/O request.
318 * @lpm: defines the channel path to be used for a specific I/O request. A
319 * value of 0 will make cio use the opm.
320 * @flags: additional flags; defines the action to be performed for I/O
322 * @expires: timeout value in jiffies
324 * Start a S/390 channel program. When the interrupt arrives, the
325 * IRQ handler is called, either immediately, delayed (dev-end missing,
326 * or sense required) or never (no IRQ handler registered).
327 * This function notifies the device driver if the channel program has not
328 * completed during the time specified by @expires. If a timeout occurs, the
329 * channel program is terminated via xsch, hsch or csch, and the device's
330 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
332 * %0, if the operation was successful;
333 * -%EBUSY, if the device is busy, or status pending;
334 * -%EACCES, if no path specified in @lpm is operational;
335 * -%ENODEV, if the device is not operational.
337 * Interrupts disabled, ccw device lock held
339 int ccw_device_start_timeout(struct ccw_device
*cdev
, struct ccw1
*cpa
,
340 unsigned long intparm
, __u8 lpm
,
341 unsigned long flags
, int expires
)
343 return ccw_device_start_timeout_key(cdev
, cpa
, intparm
, lpm
,
344 PAGE_DEFAULT_KEY
, flags
,
350 * ccw_device_halt() - halt I/O request processing
351 * @cdev: target ccw device
352 * @intparm: interruption parameter; value is only used if no I/O is
353 * outstanding, otherwise the intparm associated with the I/O request
356 * ccw_device_halt() calls hsch 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_halt(struct ccw_device
*cdev
, unsigned long intparm
)
367 struct subchannel
*sch
;
370 if (!cdev
|| !cdev
->dev
.parent
)
372 sch
= to_subchannel(cdev
->dev
.parent
);
373 if (!sch
->schib
.pmcw
.ena
)
375 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
377 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
378 cdev
->private->state
!= DEV_STATE_W4SENSE
)
383 cdev
->private->intparm
= intparm
;
388 * ccw_device_resume() - resume channel program execution
389 * @cdev: target ccw device
391 * ccw_device_resume() calls rsch on @cdev's subchannel.
394 * -%ENODEV on device not operational,
395 * -%EINVAL on invalid device state,
396 * -%EBUSY on device busy or interrupt pending.
398 * Interrupts disabled, ccw device lock held
400 int ccw_device_resume(struct ccw_device
*cdev
)
402 struct subchannel
*sch
;
404 if (!cdev
|| !cdev
->dev
.parent
)
406 sch
= to_subchannel(cdev
->dev
.parent
);
407 if (!sch
->schib
.pmcw
.ena
)
409 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
411 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
412 !(sch
->schib
.scsw
.cmd
.actl
& SCSW_ACTL_SUSPENDED
))
414 return cio_resume(sch
);
418 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
419 * @cdev: ccw device to inspect
420 * @ct: command type to look for
422 * During SenseID, command information words (CIWs) describing special
423 * commands available to the device may have been stored in the extended
424 * sense data. This function searches for CIWs of a specified command
425 * type in the extended sense data.
427 * %NULL if no extended sense data has been stored or if no CIW of the
428 * specified command type could be found,
429 * else a pointer to the CIW of the specified command type.
431 struct ciw
*ccw_device_get_ciw(struct ccw_device
*cdev
, __u32 ct
)
435 if (cdev
->private->flags
.esid
== 0)
437 for (ciw_cnt
= 0; ciw_cnt
< MAX_CIWS
; ciw_cnt
++)
438 if (cdev
->private->senseid
.ciw
[ciw_cnt
].ct
== ct
)
439 return cdev
->private->senseid
.ciw
+ ciw_cnt
;
444 * ccw_device_get_path_mask() - get currently available paths
445 * @cdev: ccw device to be queried
447 * %0 if no subchannel for the device is available,
448 * else the mask of currently available paths for the ccw device's subchannel.
450 __u8
ccw_device_get_path_mask(struct ccw_device
*cdev
)
452 struct subchannel
*sch
;
454 if (!cdev
->dev
.parent
)
457 sch
= to_subchannel(cdev
->dev
.parent
);
462 * ccw_device_get_chp_desc() - return newly allocated channel-path descriptor
463 * @cdev: device to obtain the descriptor for
464 * @chp_idx: index of the channel path
466 * On success return a newly allocated copy of the channel-path description
467 * data associated with the given channel path. Return %NULL on error.
469 struct channel_path_desc
*ccw_device_get_chp_desc(struct ccw_device
*cdev
,
472 struct subchannel
*sch
;
475 sch
= to_subchannel(cdev
->dev
.parent
);
477 chpid
.id
= sch
->schib
.pmcw
.chpid
[chp_idx
];
478 return chp_get_chp_desc(chpid
);
482 * ccw_device_get_id() - obtain a ccw device id
483 * @cdev: device to obtain the id for
484 * @dev_id: where to fill in the values
486 void ccw_device_get_id(struct ccw_device
*cdev
, struct ccw_dev_id
*dev_id
)
488 *dev_id
= cdev
->private->dev_id
;
490 EXPORT_SYMBOL(ccw_device_get_id
);
493 * ccw_device_tm_start_key() - perform start function
494 * @cdev: ccw device on which to perform the start function
495 * @tcw: transport-command word to be started
496 * @intparm: user defined parameter to be passed to the interrupt handler
497 * @lpm: mask of paths to use
498 * @key: storage key to use for storage access
500 * Start the tcw on the given ccw device. Return zero on success, non-zero
503 int ccw_device_tm_start_key(struct ccw_device
*cdev
, struct tcw
*tcw
,
504 unsigned long intparm
, u8 lpm
, u8 key
)
506 struct subchannel
*sch
;
509 sch
= to_subchannel(cdev
->dev
.parent
);
510 if (!sch
->schib
.pmcw
.ena
)
512 if (cdev
->private->state
== DEV_STATE_VERIFY
) {
513 /* Remember to fake irb when finished. */
514 if (!cdev
->private->flags
.fake_irb
) {
515 cdev
->private->flags
.fake_irb
= FAKE_TM_IRB
;
516 cdev
->private->intparm
= intparm
;
519 /* There's already a fake I/O around. */
522 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
524 /* Adjust requested path mask to exclude unusable paths. */
530 rc
= cio_tm_start_key(sch
, tcw
, lpm
, key
);
532 cdev
->private->intparm
= intparm
;
535 EXPORT_SYMBOL(ccw_device_tm_start_key
);
538 * ccw_device_tm_start_timeout_key() - perform start function
539 * @cdev: ccw device on which to perform the start function
540 * @tcw: transport-command word to be started
541 * @intparm: user defined parameter to be passed to the interrupt handler
542 * @lpm: mask of paths to use
543 * @key: storage key to use for storage access
544 * @expires: time span in jiffies after which to abort request
546 * Start the tcw on the given ccw device. Return zero on success, non-zero
549 int ccw_device_tm_start_timeout_key(struct ccw_device
*cdev
, struct tcw
*tcw
,
550 unsigned long intparm
, u8 lpm
, u8 key
,
555 ccw_device_set_timeout(cdev
, expires
);
556 ret
= ccw_device_tm_start_key(cdev
, tcw
, intparm
, lpm
, key
);
558 ccw_device_set_timeout(cdev
, 0);
561 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key
);
564 * ccw_device_tm_start() - perform start function
565 * @cdev: ccw device on which to perform the start function
566 * @tcw: transport-command word to be started
567 * @intparm: user defined parameter to be passed to the interrupt handler
568 * @lpm: mask of paths to use
570 * Start the tcw on the given ccw device. Return zero on success, non-zero
573 int ccw_device_tm_start(struct ccw_device
*cdev
, struct tcw
*tcw
,
574 unsigned long intparm
, u8 lpm
)
576 return ccw_device_tm_start_key(cdev
, tcw
, intparm
, lpm
,
579 EXPORT_SYMBOL(ccw_device_tm_start
);
582 * ccw_device_tm_start_timeout() - perform start function
583 * @cdev: ccw device on which to perform the start function
584 * @tcw: transport-command word to be started
585 * @intparm: user defined parameter to be passed to the interrupt handler
586 * @lpm: mask of paths to use
587 * @expires: time span in jiffies after which to abort request
589 * Start the tcw on the given ccw device. Return zero on success, non-zero
592 int ccw_device_tm_start_timeout(struct ccw_device
*cdev
, struct tcw
*tcw
,
593 unsigned long intparm
, u8 lpm
, int expires
)
595 return ccw_device_tm_start_timeout_key(cdev
, tcw
, intparm
, lpm
,
596 PAGE_DEFAULT_KEY
, expires
);
598 EXPORT_SYMBOL(ccw_device_tm_start_timeout
);
601 * ccw_device_get_mdc() - accumulate max data count
602 * @cdev: ccw device for which the max data count is accumulated
603 * @mask: mask of paths to use
605 * Return the number of 64K-bytes blocks all paths at least support
606 * for a transport command. Return values <= 0 indicate failures.
608 int ccw_device_get_mdc(struct ccw_device
*cdev
, u8 mask
)
610 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
611 struct channel_path
*chp
;
615 /* Adjust requested path mask to excluded varied off paths. */
622 for (i
= 0; i
< 8; i
++) {
623 if (!(mask
& (0x80 >> i
)))
625 chpid
.id
= sch
->schib
.pmcw
.chpid
[i
];
626 chp
= chpid_to_chp(chpid
);
630 mutex_lock(&chp
->lock
);
631 if (!chp
->desc_fmt1
.f
) {
632 mutex_unlock(&chp
->lock
);
635 if (!chp
->desc_fmt1
.r
)
637 mdc
= mdc
? min_t(int, mdc
, chp
->desc_fmt1
.mdc
) :
639 mutex_unlock(&chp
->lock
);
644 EXPORT_SYMBOL(ccw_device_get_mdc
);
647 * ccw_device_tm_intrg() - perform interrogate function
648 * @cdev: ccw device on which to perform the interrogate function
650 * Perform an interrogate function on the given ccw device. Return zero on
651 * success, non-zero otherwise.
653 int ccw_device_tm_intrg(struct ccw_device
*cdev
)
655 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
657 if (!sch
->schib
.pmcw
.ena
)
659 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
661 if (!scsw_is_tm(&sch
->schib
.scsw
) ||
662 !(scsw_actl(&sch
->schib
.scsw
) & SCSW_ACTL_START_PEND
))
664 return cio_tm_intrg(sch
);
666 EXPORT_SYMBOL(ccw_device_tm_intrg
);
669 * ccw_device_get_schid() - obtain a subchannel id
670 * @cdev: device to obtain the id for
671 * @schid: where to fill in the values
673 void ccw_device_get_schid(struct ccw_device
*cdev
, struct subchannel_id
*schid
)
675 struct subchannel
*sch
= to_subchannel(cdev
->dev
.parent
);
679 EXPORT_SYMBOL_GPL(ccw_device_get_schid
);
681 EXPORT_SYMBOL(ccw_device_set_options_mask
);
682 EXPORT_SYMBOL(ccw_device_set_options
);
683 EXPORT_SYMBOL(ccw_device_clear_options
);
684 EXPORT_SYMBOL(ccw_device_clear
);
685 EXPORT_SYMBOL(ccw_device_halt
);
686 EXPORT_SYMBOL(ccw_device_resume
);
687 EXPORT_SYMBOL(ccw_device_start_timeout
);
688 EXPORT_SYMBOL(ccw_device_start
);
689 EXPORT_SYMBOL(ccw_device_start_timeout_key
);
690 EXPORT_SYMBOL(ccw_device_start_key
);
691 EXPORT_SYMBOL(ccw_device_get_ciw
);
692 EXPORT_SYMBOL(ccw_device_get_path_mask
);
693 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc
);