1 // SPDX-License-Identifier: GPL-1.0+
3 * Copyright IBM Corp. 2002, 2009
8 #include <linux/export.h>
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/completion.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;
51 cdev->private->options.mpath = (flags & CCWDEV_DO_MULTIPATH) != 0;
56 * ccw_device_set_options() - set some options
57 * @cdev: device for which the options are to be set
58 * @flags: options to be set
60 * All flags specified in @flags are set, the remainder is left untouched.
62 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
64 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
67 * The flag usage is mutal exclusive ...
69 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
70 (flags & CCWDEV_REPORT_ALL)) ||
71 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
72 cdev->private->options.repall) ||
73 ((flags & CCWDEV_REPORT_ALL) &&
74 cdev->private->options.fast))
76 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
77 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
78 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
79 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
80 cdev->private->options.mpath |= (flags & CCWDEV_DO_MULTIPATH) != 0;
85 * ccw_device_clear_options() - clear some options
86 * @cdev: device for which the options are to be cleared
87 * @flags: options to be cleared
89 * All flags specified in @flags are cleared, the remainder is left untouched.
91 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
93 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
94 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
95 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
96 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
97 cdev->private->options.mpath &= (flags & CCWDEV_DO_MULTIPATH) == 0;
101 * ccw_device_is_pathgroup() - determine if paths to this device are grouped
104 * Return non-zero if there is a path group, zero otherwise.
106 int ccw_device_is_pathgroup(struct ccw_device *cdev)
108 return cdev->private->flags.pgroup;
110 EXPORT_SYMBOL(ccw_device_is_pathgroup);
113 * ccw_device_is_multipath() - determine if device is operating in multipath mode
116 * Return non-zero if device is operating in multipath mode, zero otherwise.
118 int ccw_device_is_multipath(struct ccw_device *cdev)
120 return cdev->private->flags.mpath;
122 EXPORT_SYMBOL(ccw_device_is_multipath);
125 * ccw_device_clear() - terminate I/O request processing
126 * @cdev: target ccw device
127 * @intparm: interruption parameter; value is only used if no I/O is
128 * outstanding, otherwise the intparm associated with the I/O request
131 * ccw_device_clear() calls csch on @cdev's subchannel.
134 * -%ENODEV on device not operational,
135 * -%EINVAL on invalid device state.
137 * Interrupts disabled, ccw device lock held
139 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
141 struct subchannel *sch;
144 if (!cdev || !cdev->dev.parent)
146 sch = to_subchannel(cdev->dev.parent);
147 if (!sch->schib.pmcw.ena)
149 if (cdev->private->state == DEV_STATE_NOT_OPER)
151 if (cdev->private->state != DEV_STATE_ONLINE &&
152 cdev->private->state != DEV_STATE_W4SENSE)
155 ret = cio_clear(sch);
157 cdev->private->intparm = intparm;
162 * ccw_device_start_key() - start a s390 channel program with key
163 * @cdev: target ccw device
164 * @cpa: logical start address of channel program
165 * @intparm: user specific interruption parameter; will be presented back to
166 * @cdev's interrupt handler. Allows a device driver to associate
167 * the interrupt with a particular I/O request.
168 * @lpm: defines the channel path to be used for a specific I/O request. A
169 * value of 0 will make cio use the opm.
170 * @key: storage key to be used for the I/O
171 * @flags: additional flags; defines the action to be performed for I/O
174 * Start a S/390 channel program. When the interrupt arrives, the
175 * IRQ handler is called, either immediately, delayed (dev-end missing,
176 * or sense required) or never (no IRQ handler registered).
178 * %0, if the operation was successful;
179 * -%EBUSY, if the device is busy, or status pending;
180 * -%EACCES, if no path specified in @lpm is operational;
181 * -%ENODEV, if the device is not operational.
183 * Interrupts disabled, ccw device lock held
185 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
186 unsigned long intparm, __u8 lpm, __u8 key,
189 struct subchannel *sch;
192 if (!cdev || !cdev->dev.parent)
194 sch = to_subchannel(cdev->dev.parent);
195 if (!sch->schib.pmcw.ena)
197 if (cdev->private->state == DEV_STATE_NOT_OPER)
199 if (cdev->private->state == DEV_STATE_VERIFY) {
200 /* Remember to fake irb when finished. */
201 if (!cdev->private->flags.fake_irb) {
202 cdev->private->flags.fake_irb = FAKE_CMD_IRB;
203 cdev->private->intparm = intparm;
206 /* There's already a fake I/O around. */
209 if (cdev->private->state != DEV_STATE_ONLINE ||
210 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
211 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
212 cdev->private->flags.doverify)
214 ret = cio_set_options (sch, flags);
217 /* Adjust requested path mask to exclude unusable paths. */
223 ret = cio_start_key (sch, cpa, lpm, key);
226 cdev->private->intparm = intparm;
230 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
237 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
238 * @cdev: target ccw device
239 * @cpa: logical start address of channel program
240 * @intparm: user specific interruption parameter; will be presented back to
241 * @cdev's interrupt handler. Allows a device driver to associate
242 * the interrupt with a particular I/O request.
243 * @lpm: defines the channel path to be used for a specific I/O request. A
244 * value of 0 will make cio use the opm.
245 * @key: storage key to be used for the I/O
246 * @flags: additional flags; defines the action to be performed for I/O
248 * @expires: timeout value in jiffies
250 * Start a S/390 channel program. When the interrupt arrives, the
251 * IRQ handler is called, either immediately, delayed (dev-end missing,
252 * or sense required) or never (no IRQ handler registered).
253 * This function notifies the device driver if the channel program has not
254 * completed during the time specified by @expires. If a timeout occurs, the
255 * channel program is terminated via xsch, hsch or csch, and the device's
256 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
258 * %0, if the operation was successful;
259 * -%EBUSY, if the device is busy, or status pending;
260 * -%EACCES, if no path specified in @lpm is operational;
261 * -%ENODEV, if the device is not operational.
263 * Interrupts disabled, ccw device lock held
265 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
266 unsigned long intparm, __u8 lpm, __u8 key,
267 unsigned long flags, int expires)
273 ccw_device_set_timeout(cdev, expires);
274 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
276 ccw_device_set_timeout(cdev, 0);
281 * ccw_device_start() - start a s390 channel program
282 * @cdev: target ccw device
283 * @cpa: logical start address of channel program
284 * @intparm: user specific interruption parameter; will be presented back to
285 * @cdev's interrupt handler. Allows a device driver to associate
286 * the interrupt with a particular I/O request.
287 * @lpm: defines the channel path to be used for a specific I/O request. A
288 * value of 0 will make cio use the opm.
289 * @flags: additional flags; defines the action to be performed for I/O
292 * Start a S/390 channel program. When the interrupt arrives, the
293 * IRQ handler is called, either immediately, delayed (dev-end missing,
294 * or sense required) or never (no IRQ handler registered).
296 * %0, if the operation was successful;
297 * -%EBUSY, if the device is busy, or status pending;
298 * -%EACCES, if no path specified in @lpm is operational;
299 * -%ENODEV, if the device is not operational.
301 * Interrupts disabled, ccw device lock held
303 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
304 unsigned long intparm, __u8 lpm, unsigned long flags)
306 return ccw_device_start_key(cdev, cpa, intparm, lpm,
307 PAGE_DEFAULT_KEY, flags);
311 * ccw_device_start_timeout() - start a s390 channel program with timeout
312 * @cdev: target ccw device
313 * @cpa: logical start address of channel program
314 * @intparm: user specific interruption parameter; will be presented back to
315 * @cdev's interrupt handler. Allows a device driver to associate
316 * the interrupt with a particular I/O request.
317 * @lpm: defines the channel path to be used for a specific I/O request. A
318 * value of 0 will make cio use the opm.
319 * @flags: additional flags; defines the action to be performed for I/O
321 * @expires: timeout value in jiffies
323 * Start a S/390 channel program. When the interrupt arrives, the
324 * IRQ handler is called, either immediately, delayed (dev-end missing,
325 * or sense required) or never (no IRQ handler registered).
326 * This function notifies the device driver if the channel program has not
327 * completed during the time specified by @expires. If a timeout occurs, the
328 * channel program is terminated via xsch, hsch or csch, and the device's
329 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
331 * %0, if the operation was successful;
332 * -%EBUSY, if the device is busy, or status pending;
333 * -%EACCES, if no path specified in @lpm is operational;
334 * -%ENODEV, if the device is not operational.
336 * Interrupts disabled, ccw device lock held
338 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
339 unsigned long intparm, __u8 lpm,
340 unsigned long flags, int expires)
342 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
343 PAGE_DEFAULT_KEY, flags,
349 * ccw_device_halt() - halt I/O request processing
350 * @cdev: target ccw device
351 * @intparm: interruption parameter; value is only used if no I/O is
352 * outstanding, otherwise the intparm associated with the I/O request
355 * ccw_device_halt() calls hsch on @cdev's subchannel.
358 * -%ENODEV on device not operational,
359 * -%EINVAL on invalid device state,
360 * -%EBUSY on device busy or interrupt pending.
362 * Interrupts disabled, ccw device lock held
364 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
366 struct subchannel *sch;
369 if (!cdev || !cdev->dev.parent)
371 sch = to_subchannel(cdev->dev.parent);
372 if (!sch->schib.pmcw.ena)
374 if (cdev->private->state == DEV_STATE_NOT_OPER)
376 if (cdev->private->state != DEV_STATE_ONLINE &&
377 cdev->private->state != DEV_STATE_W4SENSE)
382 cdev->private->intparm = intparm;
387 * ccw_device_resume() - resume channel program execution
388 * @cdev: target ccw device
390 * ccw_device_resume() calls rsch on @cdev's subchannel.
393 * -%ENODEV on device not operational,
394 * -%EINVAL on invalid device state,
395 * -%EBUSY on device busy or interrupt pending.
397 * Interrupts disabled, ccw device lock held
399 int ccw_device_resume(struct ccw_device *cdev)
401 struct subchannel *sch;
403 if (!cdev || !cdev->dev.parent)
405 sch = to_subchannel(cdev->dev.parent);
406 if (!sch->schib.pmcw.ena)
408 if (cdev->private->state == DEV_STATE_NOT_OPER)
410 if (cdev->private->state != DEV_STATE_ONLINE ||
411 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
413 return cio_resume(sch);
417 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
418 * @cdev: ccw device to inspect
419 * @ct: command type to look for
421 * During SenseID, command information words (CIWs) describing special
422 * commands available to the device may have been stored in the extended
423 * sense data. This function searches for CIWs of a specified command
424 * type in the extended sense data.
426 * %NULL if no extended sense data has been stored or if no CIW of the
427 * specified command type could be found,
428 * else a pointer to the CIW of the specified command type.
430 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
434 if (cdev->private->flags.esid == 0)
436 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
437 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
438 return cdev->private->senseid.ciw + ciw_cnt;
443 * ccw_device_get_path_mask() - get currently available paths
444 * @cdev: ccw device to be queried
446 * %0 if no subchannel for the device is available,
447 * else the mask of currently available paths for the ccw device's subchannel.
449 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
451 struct subchannel *sch;
453 if (!cdev->dev.parent)
456 sch = to_subchannel(cdev->dev.parent);
461 * ccw_device_get_chp_desc() - return newly allocated channel-path descriptor
462 * @cdev: device to obtain the descriptor for
463 * @chp_idx: index of the channel path
465 * On success return a newly allocated copy of the channel-path description
466 * data associated with the given channel path. Return %NULL on error.
468 struct channel_path_desc *ccw_device_get_chp_desc(struct ccw_device *cdev,
471 struct subchannel *sch;
474 sch = to_subchannel(cdev->dev.parent);
476 chpid.id = sch->schib.pmcw.chpid[chp_idx];
477 return chp_get_chp_desc(chpid);
481 * ccw_device_get_id() - obtain a ccw device id
482 * @cdev: device to obtain the id for
483 * @dev_id: where to fill in the values
485 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
487 *dev_id = cdev->private->dev_id;
489 EXPORT_SYMBOL(ccw_device_get_id);
492 * ccw_device_tm_start_key() - perform start function
493 * @cdev: ccw device on which to perform the start function
494 * @tcw: transport-command word to be started
495 * @intparm: user defined parameter to be passed to the interrupt handler
496 * @lpm: mask of paths to use
497 * @key: storage key to use for storage access
499 * Start the tcw on the given ccw device. Return zero on success, non-zero
502 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
503 unsigned long intparm, u8 lpm, u8 key)
505 struct subchannel *sch;
508 sch = to_subchannel(cdev->dev.parent);
509 if (!sch->schib.pmcw.ena)
511 if (cdev->private->state == DEV_STATE_VERIFY) {
512 /* Remember to fake irb when finished. */
513 if (!cdev->private->flags.fake_irb) {
514 cdev->private->flags.fake_irb = FAKE_TM_IRB;
515 cdev->private->intparm = intparm;
518 /* There's already a fake I/O around. */
521 if (cdev->private->state != DEV_STATE_ONLINE)
523 /* Adjust requested path mask to exclude unusable paths. */
529 rc = cio_tm_start_key(sch, tcw, lpm, key);
531 cdev->private->intparm = intparm;
534 EXPORT_SYMBOL(ccw_device_tm_start_key);
537 * ccw_device_tm_start_timeout_key() - perform start function
538 * @cdev: ccw device on which to perform the start function
539 * @tcw: transport-command word to be started
540 * @intparm: user defined parameter to be passed to the interrupt handler
541 * @lpm: mask of paths to use
542 * @key: storage key to use for storage access
543 * @expires: time span in jiffies after which to abort request
545 * Start the tcw on the given ccw device. Return zero on success, non-zero
548 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
549 unsigned long intparm, u8 lpm, u8 key,
554 ccw_device_set_timeout(cdev, expires);
555 ret = ccw_device_tm_start_key(cdev, tcw, intparm, lpm, key);
557 ccw_device_set_timeout(cdev, 0);
560 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
563 * ccw_device_tm_start() - perform start function
564 * @cdev: ccw device on which to perform the start function
565 * @tcw: transport-command word to be started
566 * @intparm: user defined parameter to be passed to the interrupt handler
567 * @lpm: mask of paths to use
569 * Start the tcw on the given ccw device. Return zero on success, non-zero
572 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
573 unsigned long intparm, u8 lpm)
575 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
578 EXPORT_SYMBOL(ccw_device_tm_start);
581 * ccw_device_tm_start_timeout() - perform start function
582 * @cdev: ccw device on which to perform the start function
583 * @tcw: transport-command word to be started
584 * @intparm: user defined parameter to be passed to the interrupt handler
585 * @lpm: mask of paths to use
586 * @expires: time span in jiffies after which to abort request
588 * Start the tcw on the given ccw device. Return zero on success, non-zero
591 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
592 unsigned long intparm, u8 lpm, int expires)
594 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
595 PAGE_DEFAULT_KEY, expires);
597 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
600 * ccw_device_get_mdc() - accumulate max data count
601 * @cdev: ccw device for which the max data count is accumulated
602 * @mask: mask of paths to use
604 * Return the number of 64K-bytes blocks all paths at least support
605 * for a transport command. Return values <= 0 indicate failures.
607 int ccw_device_get_mdc(struct ccw_device *cdev, u8 mask)
609 struct subchannel *sch = to_subchannel(cdev->dev.parent);
610 struct channel_path *chp;
614 /* Adjust requested path mask to excluded varied off paths. */
621 for (i = 0; i < 8; i++) {
622 if (!(mask & (0x80 >> i)))
624 chpid.id = sch->schib.pmcw.chpid[i];
625 chp = chpid_to_chp(chpid);
629 mutex_lock(&chp->lock);
630 if (!chp->desc_fmt1.f) {
631 mutex_unlock(&chp->lock);
634 if (!chp->desc_fmt1.r)
636 mdc = mdc ? min_t(int, mdc, chp->desc_fmt1.mdc) :
638 mutex_unlock(&chp->lock);
643 EXPORT_SYMBOL(ccw_device_get_mdc);
646 * ccw_device_tm_intrg() - perform interrogate function
647 * @cdev: ccw device on which to perform the interrogate function
649 * Perform an interrogate function on the given ccw device. Return zero on
650 * success, non-zero otherwise.
652 int ccw_device_tm_intrg(struct ccw_device *cdev)
654 struct subchannel *sch = to_subchannel(cdev->dev.parent);
656 if (!sch->schib.pmcw.ena)
658 if (cdev->private->state != DEV_STATE_ONLINE)
660 if (!scsw_is_tm(&sch->schib.scsw) ||
661 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
663 return cio_tm_intrg(sch);
665 EXPORT_SYMBOL(ccw_device_tm_intrg);
668 * ccw_device_get_schid() - obtain a subchannel id
669 * @cdev: device to obtain the id for
670 * @schid: where to fill in the values
672 void ccw_device_get_schid(struct ccw_device *cdev, struct subchannel_id *schid)
674 struct subchannel *sch = to_subchannel(cdev->dev.parent);
678 EXPORT_SYMBOL_GPL(ccw_device_get_schid);
680 EXPORT_SYMBOL(ccw_device_set_options_mask);
681 EXPORT_SYMBOL(ccw_device_set_options);
682 EXPORT_SYMBOL(ccw_device_clear_options);
683 EXPORT_SYMBOL(ccw_device_clear);
684 EXPORT_SYMBOL(ccw_device_halt);
685 EXPORT_SYMBOL(ccw_device_resume);
686 EXPORT_SYMBOL(ccw_device_start_timeout);
687 EXPORT_SYMBOL(ccw_device_start);
688 EXPORT_SYMBOL(ccw_device_start_timeout_key);
689 EXPORT_SYMBOL(ccw_device_start_key);
690 EXPORT_SYMBOL(ccw_device_get_ciw);
691 EXPORT_SYMBOL(ccw_device_get_path_mask);
692 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);