1 // SPDX-License-Identifier: GPL-1.0+
3 * Copyright IBM Corp. 2002, 2009
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
6 * Cornelia Huck (cornelia.huck@de.ibm.com)
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_timeout_key() - start a s390 channel program with timeout and 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
173 * @expires: timeout value in jiffies
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).
178 * This function notifies the device driver if the channel program has not
179 * completed during the time specified by @expires. If a timeout occurs, the
180 * channel program is terminated via xsch, hsch or csch, and the device's
181 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
183 * %0, if the operation was successful;
184 * -%EBUSY, if the device is busy, or status pending;
185 * -%EACCES, if no path specified in @lpm is operational;
186 * -%ENODEV, if the device is not operational.
188 * Interrupts disabled, ccw device lock held
190 int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
191 unsigned long intparm, __u8 lpm, __u8 key,
192 unsigned long flags, int expires)
194 struct subchannel *sch;
197 if (!cdev || !cdev->dev.parent)
199 sch = to_subchannel(cdev->dev.parent);
200 if (!sch->schib.pmcw.ena)
202 if (cdev->private->state == DEV_STATE_NOT_OPER)
204 if (cdev->private->state == DEV_STATE_VERIFY) {
205 /* Remember to fake irb when finished. */
206 if (!cdev->private->flags.fake_irb) {
207 cdev->private->flags.fake_irb = FAKE_CMD_IRB;
208 cdev->private->intparm = intparm;
211 /* There's already a fake I/O around. */
214 if (cdev->private->state != DEV_STATE_ONLINE ||
215 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
216 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
217 cdev->private->flags.doverify)
219 ret = cio_set_options (sch, flags);
222 /* Adjust requested path mask to exclude unusable paths. */
228 ret = cio_start_key (sch, cpa, lpm, key);
231 cdev->private->intparm = intparm;
233 ccw_device_set_timeout(cdev, expires);
237 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
244 * ccw_device_start_key() - start a s390 channel program with key
245 * @cdev: target ccw device
246 * @cpa: logical start address of channel program
247 * @intparm: user specific interruption parameter; will be presented back to
248 * @cdev's interrupt handler. Allows a device driver to associate
249 * the interrupt with a particular I/O request.
250 * @lpm: defines the channel path to be used for a specific I/O request. A
251 * value of 0 will make cio use the opm.
252 * @key: storage key to be used for the I/O
253 * @flags: additional flags; defines the action to be performed for I/O
256 * Start a S/390 channel program. When the interrupt arrives, the
257 * IRQ handler is called, either immediately, delayed (dev-end missing,
258 * or sense required) or never (no IRQ handler registered).
260 * %0, if the operation was successful;
261 * -%EBUSY, if the device is busy, or status pending;
262 * -%EACCES, if no path specified in @lpm is operational;
263 * -%ENODEV, if the device is not operational.
265 * Interrupts disabled, ccw device lock held
267 int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
268 unsigned long intparm, __u8 lpm, __u8 key,
271 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm, key,
276 * ccw_device_start() - start a s390 channel program
277 * @cdev: target ccw device
278 * @cpa: logical start address of channel program
279 * @intparm: user specific interruption parameter; will be presented back to
280 * @cdev's interrupt handler. Allows a device driver to associate
281 * the interrupt with a particular I/O request.
282 * @lpm: defines the channel path to be used for a specific I/O request. A
283 * value of 0 will make cio use the opm.
284 * @flags: additional flags; defines the action to be performed for I/O
287 * Start a S/390 channel program. When the interrupt arrives, the
288 * IRQ handler is called, either immediately, delayed (dev-end missing,
289 * or sense required) or never (no IRQ handler registered).
291 * %0, if the operation was successful;
292 * -%EBUSY, if the device is busy, or status pending;
293 * -%EACCES, if no path specified in @lpm is operational;
294 * -%ENODEV, if the device is not operational.
296 * Interrupts disabled, ccw device lock held
298 int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
299 unsigned long intparm, __u8 lpm, unsigned long flags)
301 return ccw_device_start_key(cdev, cpa, intparm, lpm,
302 PAGE_DEFAULT_KEY, flags);
306 * ccw_device_start_timeout() - start a s390 channel program with timeout
307 * @cdev: target ccw device
308 * @cpa: logical start address of channel program
309 * @intparm: user specific interruption parameter; will be presented back to
310 * @cdev's interrupt handler. Allows a device driver to associate
311 * the interrupt with a particular I/O request.
312 * @lpm: defines the channel path to be used for a specific I/O request. A
313 * value of 0 will make cio use the opm.
314 * @flags: additional flags; defines the action to be performed for I/O
316 * @expires: timeout value in jiffies
318 * Start a S/390 channel program. When the interrupt arrives, the
319 * IRQ handler is called, either immediately, delayed (dev-end missing,
320 * or sense required) or never (no IRQ handler registered).
321 * This function notifies the device driver if the channel program has not
322 * completed during the time specified by @expires. If a timeout occurs, the
323 * channel program is terminated via xsch, hsch or csch, and the device's
324 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
326 * %0, if the operation was successful;
327 * -%EBUSY, if the device is busy, or status pending;
328 * -%EACCES, if no path specified in @lpm is operational;
329 * -%ENODEV, if the device is not operational.
331 * Interrupts disabled, ccw device lock held
333 int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
334 unsigned long intparm, __u8 lpm,
335 unsigned long flags, int expires)
337 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
338 PAGE_DEFAULT_KEY, flags,
344 * ccw_device_halt() - halt I/O request processing
345 * @cdev: target ccw device
346 * @intparm: interruption parameter; value is only used if no I/O is
347 * outstanding, otherwise the intparm associated with the I/O request
350 * ccw_device_halt() calls hsch on @cdev's subchannel.
353 * -%ENODEV on device not operational,
354 * -%EINVAL on invalid device state,
355 * -%EBUSY on device busy or interrupt pending.
357 * Interrupts disabled, ccw device lock held
359 int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
361 struct subchannel *sch;
364 if (!cdev || !cdev->dev.parent)
366 sch = to_subchannel(cdev->dev.parent);
367 if (!sch->schib.pmcw.ena)
369 if (cdev->private->state == DEV_STATE_NOT_OPER)
371 if (cdev->private->state != DEV_STATE_ONLINE &&
372 cdev->private->state != DEV_STATE_W4SENSE)
377 cdev->private->intparm = intparm;
382 * ccw_device_resume() - resume channel program execution
383 * @cdev: target ccw device
385 * ccw_device_resume() calls rsch on @cdev's subchannel.
388 * -%ENODEV on device not operational,
389 * -%EINVAL on invalid device state,
390 * -%EBUSY on device busy or interrupt pending.
392 * Interrupts disabled, ccw device lock held
394 int ccw_device_resume(struct ccw_device *cdev)
396 struct subchannel *sch;
398 if (!cdev || !cdev->dev.parent)
400 sch = to_subchannel(cdev->dev.parent);
401 if (!sch->schib.pmcw.ena)
403 if (cdev->private->state == DEV_STATE_NOT_OPER)
405 if (cdev->private->state != DEV_STATE_ONLINE ||
406 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
408 return cio_resume(sch);
412 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
413 * @cdev: ccw device to inspect
414 * @ct: command type to look for
416 * During SenseID, command information words (CIWs) describing special
417 * commands available to the device may have been stored in the extended
418 * sense data. This function searches for CIWs of a specified command
419 * type in the extended sense data.
421 * %NULL if no extended sense data has been stored or if no CIW of the
422 * specified command type could be found,
423 * else a pointer to the CIW of the specified command type.
425 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
429 if (cdev->private->flags.esid == 0)
431 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
432 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
433 return cdev->private->senseid.ciw + ciw_cnt;
438 * ccw_device_get_path_mask() - get currently available paths
439 * @cdev: ccw device to be queried
441 * %0 if no subchannel for the device is available,
442 * else the mask of currently available paths for the ccw device's subchannel.
444 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
446 struct subchannel *sch;
448 if (!cdev->dev.parent)
451 sch = to_subchannel(cdev->dev.parent);
456 * ccw_device_get_chp_desc() - return newly allocated channel-path descriptor
457 * @cdev: device to obtain the descriptor for
458 * @chp_idx: index of the channel path
460 * On success return a newly allocated copy of the channel-path description
461 * data associated with the given channel path. Return %NULL on error.
463 struct channel_path_desc *ccw_device_get_chp_desc(struct ccw_device *cdev,
466 struct subchannel *sch;
469 sch = to_subchannel(cdev->dev.parent);
471 chpid.id = sch->schib.pmcw.chpid[chp_idx];
472 return chp_get_chp_desc(chpid);
476 * ccw_device_get_id() - obtain a ccw device id
477 * @cdev: device to obtain the id for
478 * @dev_id: where to fill in the values
480 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
482 *dev_id = cdev->private->dev_id;
484 EXPORT_SYMBOL(ccw_device_get_id);
487 * ccw_device_tm_start_timeout_key() - perform start function
488 * @cdev: ccw device on which to perform the start function
489 * @tcw: transport-command word to be started
490 * @intparm: user defined parameter to be passed to the interrupt handler
491 * @lpm: mask of paths to use
492 * @key: storage key to use for storage access
493 * @expires: time span in jiffies after which to abort request
495 * Start the tcw on the given ccw device. Return zero on success, non-zero
498 int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
499 unsigned long intparm, u8 lpm, u8 key,
502 struct subchannel *sch;
505 sch = to_subchannel(cdev->dev.parent);
506 if (!sch->schib.pmcw.ena)
508 if (cdev->private->state == DEV_STATE_VERIFY) {
509 /* Remember to fake irb when finished. */
510 if (!cdev->private->flags.fake_irb) {
511 cdev->private->flags.fake_irb = FAKE_TM_IRB;
512 cdev->private->intparm = intparm;
515 /* There's already a fake I/O around. */
518 if (cdev->private->state != DEV_STATE_ONLINE)
520 /* Adjust requested path mask to exclude unusable paths. */
526 rc = cio_tm_start_key(sch, tcw, lpm, key);
528 cdev->private->intparm = intparm;
530 ccw_device_set_timeout(cdev, expires);
534 EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
537 * ccw_device_tm_start_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
544 * Start the tcw on the given ccw device. Return zero on success, non-zero
547 int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
548 unsigned long intparm, u8 lpm, u8 key)
550 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm, key, 0);
552 EXPORT_SYMBOL(ccw_device_tm_start_key);
555 * ccw_device_tm_start() - perform start function
556 * @cdev: ccw device on which to perform the start function
557 * @tcw: transport-command word to be started
558 * @intparm: user defined parameter to be passed to the interrupt handler
559 * @lpm: mask of paths to use
561 * Start the tcw on the given ccw device. Return zero on success, non-zero
564 int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
565 unsigned long intparm, u8 lpm)
567 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
570 EXPORT_SYMBOL(ccw_device_tm_start);
573 * ccw_device_tm_start_timeout() - perform start function
574 * @cdev: ccw device on which to perform the start function
575 * @tcw: transport-command word to be started
576 * @intparm: user defined parameter to be passed to the interrupt handler
577 * @lpm: mask of paths to use
578 * @expires: time span in jiffies after which to abort request
580 * Start the tcw on the given ccw device. Return zero on success, non-zero
583 int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
584 unsigned long intparm, u8 lpm, int expires)
586 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
587 PAGE_DEFAULT_KEY, expires);
589 EXPORT_SYMBOL(ccw_device_tm_start_timeout);
592 * ccw_device_get_mdc() - accumulate max data count
593 * @cdev: ccw device for which the max data count is accumulated
594 * @mask: mask of paths to use
596 * Return the number of 64K-bytes blocks all paths at least support
597 * for a transport command. Return values <= 0 indicate failures.
599 int ccw_device_get_mdc(struct ccw_device *cdev, u8 mask)
601 struct subchannel *sch = to_subchannel(cdev->dev.parent);
602 struct channel_path *chp;
606 /* Adjust requested path mask to excluded varied off paths. */
613 for (i = 0; i < 8; i++) {
614 if (!(mask & (0x80 >> i)))
616 chpid.id = sch->schib.pmcw.chpid[i];
617 chp = chpid_to_chp(chpid);
621 mutex_lock(&chp->lock);
622 if (!chp->desc_fmt1.f) {
623 mutex_unlock(&chp->lock);
626 if (!chp->desc_fmt1.r)
628 mdc = mdc ? min_t(int, mdc, chp->desc_fmt1.mdc) :
630 mutex_unlock(&chp->lock);
635 EXPORT_SYMBOL(ccw_device_get_mdc);
638 * ccw_device_tm_intrg() - perform interrogate function
639 * @cdev: ccw device on which to perform the interrogate function
641 * Perform an interrogate function on the given ccw device. Return zero on
642 * success, non-zero otherwise.
644 int ccw_device_tm_intrg(struct ccw_device *cdev)
646 struct subchannel *sch = to_subchannel(cdev->dev.parent);
648 if (!sch->schib.pmcw.ena)
650 if (cdev->private->state != DEV_STATE_ONLINE)
652 if (!scsw_is_tm(&sch->schib.scsw) ||
653 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
655 return cio_tm_intrg(sch);
657 EXPORT_SYMBOL(ccw_device_tm_intrg);
660 * ccw_device_get_schid() - obtain a subchannel id
661 * @cdev: device to obtain the id for
662 * @schid: where to fill in the values
664 void ccw_device_get_schid(struct ccw_device *cdev, struct subchannel_id *schid)
666 struct subchannel *sch = to_subchannel(cdev->dev.parent);
670 EXPORT_SYMBOL_GPL(ccw_device_get_schid);
672 EXPORT_SYMBOL(ccw_device_set_options_mask);
673 EXPORT_SYMBOL(ccw_device_set_options);
674 EXPORT_SYMBOL(ccw_device_clear_options);
675 EXPORT_SYMBOL(ccw_device_clear);
676 EXPORT_SYMBOL(ccw_device_halt);
677 EXPORT_SYMBOL(ccw_device_resume);
678 EXPORT_SYMBOL(ccw_device_start_timeout);
679 EXPORT_SYMBOL(ccw_device_start);
680 EXPORT_SYMBOL(ccw_device_start_timeout_key);
681 EXPORT_SYMBOL(ccw_device_start_key);
682 EXPORT_SYMBOL(ccw_device_get_ciw);
683 EXPORT_SYMBOL(ccw_device_get_path_mask);
684 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);