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_timeout_key() - start a s390 channel program with timeout and 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
174 * @expires: timeout value in jiffies
176 * Start a S/390 channel program. When the interrupt arrives, the
177 * IRQ handler is called, either immediately, delayed (dev-end missing,
178 * or sense required) or never (no IRQ handler registered).
179 * This function notifies the device driver if the channel program has not
180 * completed during the time specified by @expires. If a timeout occurs, the
181 * channel program is terminated via xsch, hsch or csch, and the device's
182 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
184 * %0, if the operation was successful;
185 * -%EBUSY, if the device is busy, or status pending;
186 * -%EACCES, if no path specified in @lpm is operational;
187 * -%ENODEV, if the device is not operational.
189 * Interrupts disabled, ccw device lock held
191 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
192 unsigned long intparm, __u8 lpm, __u8 key,
193 unsigned long flags, int expires)
195 struct subchannel *sch;
198 if (!cdev || !cdev->dev.parent)
200 sch = to_subchannel(cdev->dev.parent);
201 if (!sch->schib.pmcw.ena)
203 if (cdev->private->state == DEV_STATE_NOT_OPER)
205 if (cdev->private->state == DEV_STATE_VERIFY) {
206 /* Remember to fake irb when finished. */
207 if (!cdev->private->flags.fake_irb) {
208 cdev->private->flags.fake_irb = FAKE_CMD_IRB;
209 cdev->private->intparm = intparm;
212 /* There's already a fake I/O around. */
215 if (cdev->private->state != DEV_STATE_ONLINE ||
216 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
217 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
218 cdev->private->flags.doverify)
220 ret = cio_set_options (sch, flags);
223 /* Adjust requested path mask to exclude unusable paths. */
229 ret = cio_start_key (sch, cpa, lpm, key);
232 cdev->private->intparm = intparm;
234 ccw_device_set_timeout(cdev, expires);
238 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
245 * ccw_device_start_key() - start a s390 channel program with key
246 * @cdev: target ccw device
247 * @cpa: logical start address of channel program
248 * @intparm: user specific interruption parameter; will be presented back to
249 * @cdev's interrupt handler. Allows a device driver to associate
250 * the interrupt with a particular I/O request.
251 * @lpm: defines the channel path to be used for a specific I/O request. A
252 * value of 0 will make cio use the opm.
253 * @key: storage key to be used for the I/O
254 * @flags: additional flags; defines the action to be performed for I/O
257 * Start a S/390 channel program. When the interrupt arrives, the
258 * IRQ handler is called, either immediately, delayed (dev-end missing,
259 * or sense required) or never (no IRQ handler registered).
261 * %0, if the operation was successful;
262 * -%EBUSY, if the device is busy, or status pending;
263 * -%EACCES, if no path specified in @lpm is operational;
264 * -%ENODEV, if the device is not operational.
266 * Interrupts disabled, ccw device lock held
268 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
269 unsigned long intparm, __u8 lpm, __u8 key,
272 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm, key,
277 * ccw_device_start() - start a s390 channel program
278 * @cdev: target ccw device
279 * @cpa: logical start address of channel program
280 * @intparm: user specific interruption parameter; will be presented back to
281 * @cdev's interrupt handler. Allows a device driver to associate
282 * the interrupt with a particular I/O request.
283 * @lpm: defines the channel path to be used for a specific I/O request. A
284 * value of 0 will make cio use the opm.
285 * @flags: additional flags; defines the action to be performed for I/O
288 * Start a S/390 channel program. When the interrupt arrives, the
289 * IRQ handler is called, either immediately, delayed (dev-end missing,
290 * or sense required) or never (no IRQ handler registered).
292 * %0, if the operation was successful;
293 * -%EBUSY, if the device is busy, or status pending;
294 * -%EACCES, if no path specified in @lpm is operational;
295 * -%ENODEV, if the device is not operational.
297 * Interrupts disabled, ccw device lock held
299 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
300 unsigned long intparm, __u8 lpm, unsigned long flags)
302 return ccw_device_start_key(cdev, cpa, intparm, lpm,
303 PAGE_DEFAULT_KEY, flags);
307 * ccw_device_start_timeout() - start a s390 channel program with timeout
308 * @cdev: target ccw device
309 * @cpa: logical start address of channel program
310 * @intparm: user specific interruption parameter; will be presented back to
311 * @cdev's interrupt handler. Allows a device driver to associate
312 * the interrupt with a particular I/O request.
313 * @lpm: defines the channel path to be used for a specific I/O request. A
314 * value of 0 will make cio use the opm.
315 * @flags: additional flags; defines the action to be performed for I/O
317 * @expires: timeout value in jiffies
319 * Start a S/390 channel program. When the interrupt arrives, the
320 * IRQ handler is called, either immediately, delayed (dev-end missing,
321 * or sense required) or never (no IRQ handler registered).
322 * This function notifies the device driver if the channel program has not
323 * completed during the time specified by @expires. If a timeout occurs, the
324 * channel program is terminated via xsch, hsch or csch, and the device's
325 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
327 * %0, if the operation was successful;
328 * -%EBUSY, if the device is busy, or status pending;
329 * -%EACCES, if no path specified in @lpm is operational;
330 * -%ENODEV, if the device is not operational.
332 * Interrupts disabled, ccw device lock held
334 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
335 unsigned long intparm, __u8 lpm,
336 unsigned long flags, int expires)
338 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
339 PAGE_DEFAULT_KEY, flags,
345 * ccw_device_halt() - halt I/O request processing
346 * @cdev: target ccw device
347 * @intparm: interruption parameter; value is only used if no I/O is
348 * outstanding, otherwise the intparm associated with the I/O request
351 * ccw_device_halt() calls hsch on @cdev's subchannel.
354 * -%ENODEV on device not operational,
355 * -%EINVAL on invalid device state,
356 * -%EBUSY on device busy or interrupt pending.
358 * Interrupts disabled, ccw device lock held
360 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
362 struct subchannel *sch;
365 if (!cdev || !cdev->dev.parent)
367 sch = to_subchannel(cdev->dev.parent);
368 if (!sch->schib.pmcw.ena)
370 if (cdev->private->state == DEV_STATE_NOT_OPER)
372 if (cdev->private->state != DEV_STATE_ONLINE &&
373 cdev->private->state != DEV_STATE_W4SENSE)
378 cdev->private->intparm = intparm;
383 * ccw_device_resume() - resume channel program execution
384 * @cdev: target ccw device
386 * ccw_device_resume() calls rsch on @cdev's subchannel.
389 * -%ENODEV on device not operational,
390 * -%EINVAL on invalid device state,
391 * -%EBUSY on device busy or interrupt pending.
393 * Interrupts disabled, ccw device lock held
395 int ccw_device_resume(struct ccw_device *cdev)
397 struct subchannel *sch;
399 if (!cdev || !cdev->dev.parent)
401 sch = to_subchannel(cdev->dev.parent);
402 if (!sch->schib.pmcw.ena)
404 if (cdev->private->state == DEV_STATE_NOT_OPER)
406 if (cdev->private->state != DEV_STATE_ONLINE ||
407 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
409 return cio_resume(sch);
413 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
414 * @cdev: ccw device to inspect
415 * @ct: command type to look for
417 * During SenseID, command information words (CIWs) describing special
418 * commands available to the device may have been stored in the extended
419 * sense data. This function searches for CIWs of a specified command
420 * type in the extended sense data.
422 * %NULL if no extended sense data has been stored or if no CIW of the
423 * specified command type could be found,
424 * else a pointer to the CIW of the specified command type.
426 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
430 if (cdev->private->flags.esid == 0)
432 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
433 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
434 return cdev->private->senseid.ciw + ciw_cnt;
439 * ccw_device_get_path_mask() - get currently available paths
440 * @cdev: ccw device to be queried
442 * %0 if no subchannel for the device is available,
443 * else the mask of currently available paths for the ccw device's subchannel.
445 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
447 struct subchannel *sch;
449 if (!cdev->dev.parent)
452 sch = to_subchannel(cdev->dev.parent);
457 * ccw_device_get_chp_desc() - return newly allocated channel-path descriptor
458 * @cdev: device to obtain the descriptor for
459 * @chp_idx: index of the channel path
461 * On success return a newly allocated copy of the channel-path description
462 * data associated with the given channel path. Return %NULL on error.
464 struct channel_path_desc *ccw_device_get_chp_desc(struct ccw_device *cdev,
467 struct subchannel *sch;
470 sch = to_subchannel(cdev->dev.parent);
472 chpid.id = sch->schib.pmcw.chpid[chp_idx];
473 return chp_get_chp_desc(chpid);
477 * ccw_device_get_id() - obtain a ccw device id
478 * @cdev: device to obtain the id for
479 * @dev_id: where to fill in the values
481 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
483 *dev_id = cdev->private->dev_id;
485 EXPORT_SYMBOL(ccw_device_get_id);
488 * ccw_device_tm_start_timeout_key() - perform start function
489 * @cdev: ccw device on which to perform the start function
490 * @tcw: transport-command word to be started
491 * @intparm: user defined parameter to be passed to the interrupt handler
492 * @lpm: mask of paths to use
493 * @key: storage key to use for storage access
494 * @expires: time span in jiffies after which to abort request
496 * Start the tcw on the given ccw device. Return zero on success, non-zero
499 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
500 unsigned long intparm, u8 lpm, u8 key,
503 struct subchannel *sch;
506 sch = to_subchannel(cdev->dev.parent);
507 if (!sch->schib.pmcw.ena)
509 if (cdev->private->state == DEV_STATE_VERIFY) {
510 /* Remember to fake irb when finished. */
511 if (!cdev->private->flags.fake_irb) {
512 cdev->private->flags.fake_irb = FAKE_TM_IRB;
513 cdev->private->intparm = intparm;
516 /* There's already a fake I/O around. */
519 if (cdev->private->state != DEV_STATE_ONLINE)
521 /* Adjust requested path mask to exclude unusable paths. */
527 rc = cio_tm_start_key(sch, tcw, lpm, key);
529 cdev->private->intparm = intparm;
531 ccw_device_set_timeout(cdev, expires);
535 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
538 * ccw_device_tm_start_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
545 * Start the tcw on the given ccw device. Return zero on success, non-zero
548 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
549 unsigned long intparm, u8 lpm, u8 key)
551 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm, key, 0);
553 EXPORT_SYMBOL(ccw_device_tm_start_key);
556 * ccw_device_tm_start() - perform start function
557 * @cdev: ccw device on which to perform the start function
558 * @tcw: transport-command word to be started
559 * @intparm: user defined parameter to be passed to the interrupt handler
560 * @lpm: mask of paths to use
562 * Start the tcw on the given ccw device. Return zero on success, non-zero
565 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
566 unsigned long intparm, u8 lpm)
568 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
571 EXPORT_SYMBOL(ccw_device_tm_start);
574 * ccw_device_tm_start_timeout() - 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 * @expires: time span in jiffies after which to abort request
581 * Start the tcw on the given ccw device. Return zero on success, non-zero
584 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
585 unsigned long intparm, u8 lpm, int expires)
587 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
588 PAGE_DEFAULT_KEY, expires);
590 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
593 * ccw_device_get_mdc() - accumulate max data count
594 * @cdev: ccw device for which the max data count is accumulated
595 * @mask: mask of paths to use
597 * Return the number of 64K-bytes blocks all paths at least support
598 * for a transport command. Return value 0 indicates failure.
600 int ccw_device_get_mdc(struct ccw_device *cdev, u8 mask)
602 struct subchannel *sch = to_subchannel(cdev->dev.parent);
603 struct channel_path *chp;
607 /* Adjust requested path mask to excluded varied off paths. */
614 for (i = 0; i < 8; i++) {
615 if (!(mask & (0x80 >> i)))
617 chpid.id = sch->schib.pmcw.chpid[i];
618 chp = chpid_to_chp(chpid);
622 mutex_lock(&chp->lock);
623 if (!chp->desc_fmt1.f) {
624 mutex_unlock(&chp->lock);
627 if (!chp->desc_fmt1.r)
629 mdc = mdc ? min_t(int, mdc, chp->desc_fmt1.mdc) :
631 mutex_unlock(&chp->lock);
636 EXPORT_SYMBOL(ccw_device_get_mdc);
639 * ccw_device_tm_intrg() - perform interrogate function
640 * @cdev: ccw device on which to perform the interrogate function
642 * Perform an interrogate function on the given ccw device. Return zero on
643 * success, non-zero otherwise.
645 int ccw_device_tm_intrg(struct ccw_device *cdev)
647 struct subchannel *sch = to_subchannel(cdev->dev.parent);
649 if (!sch->schib.pmcw.ena)
651 if (cdev->private->state != DEV_STATE_ONLINE)
653 if (!scsw_is_tm(&sch->schib.scsw) ||
654 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
656 return cio_tm_intrg(sch);
658 EXPORT_SYMBOL(ccw_device_tm_intrg);
661 * ccw_device_get_schid() - obtain a subchannel id
662 * @cdev: device to obtain the id for
663 * @schid: where to fill in the values
665 void ccw_device_get_schid(struct ccw_device *cdev, struct subchannel_id *schid)
667 struct subchannel *sch = to_subchannel(cdev->dev.parent);
671 EXPORT_SYMBOL_GPL(ccw_device_get_schid);
673 EXPORT_SYMBOL(ccw_device_set_options_mask);
674 EXPORT_SYMBOL(ccw_device_set_options);
675 EXPORT_SYMBOL(ccw_device_clear_options);
676 EXPORT_SYMBOL(ccw_device_clear);
677 EXPORT_SYMBOL(ccw_device_halt);
678 EXPORT_SYMBOL(ccw_device_resume);
679 EXPORT_SYMBOL(ccw_device_start_timeout);
680 EXPORT_SYMBOL(ccw_device_start);
681 EXPORT_SYMBOL(ccw_device_start_timeout_key);
682 EXPORT_SYMBOL(ccw_device_start_key);
683 EXPORT_SYMBOL(ccw_device_get_ciw);
684 EXPORT_SYMBOL(ccw_device_get_path_mask);
685 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);