2 * drivers/s390/cio/device_ops.c
4 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 #include <linux/module.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>
17 #include <asm/ccwdev.h>
18 #include <asm/idals.h>
19 #include <asm/chpid.h>
22 #include "cio_debug.h"
29 * ccw_device_set_options_mask() - set some options and unset the rest
30 * @cdev: device for which the options are to be set
31 * @flags: options to be set
33 * All flags specified in @flags are set, all flags not specified in @flags
36 * %0 on success, -%EINVAL on an invalid flag combination.
38 int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
41 * The flag usage is mutal exclusive ...
43 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
44 (flags & CCWDEV_REPORT_ALL))
46 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
47 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
48 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
49 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
54 * ccw_device_set_options() - set some options
55 * @cdev: device for which the options are to be set
56 * @flags: options to be set
58 * All flags specified in @flags are set, the remainder is left untouched.
60 * %0 on success, -%EINVAL if an invalid flag combination would ensue.
62 int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
65 * The flag usage is mutal exclusive ...
67 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
68 (flags & CCWDEV_REPORT_ALL)) ||
69 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
70 cdev->private->options.repall) ||
71 ((flags & CCWDEV_REPORT_ALL) &&
72 cdev->private->options.fast))
74 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
75 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
76 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
77 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
82 * ccw_device_clear_options() - clear some options
83 * @cdev: device for which the options are to be cleared
84 * @flags: options to be cleared
86 * All flags specified in @flags are cleared, the remainder is left untouched.
88 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
90 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
91 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
92 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
93 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
97 * ccw_device_clear() - terminate I/O request processing
98 * @cdev: target ccw device
99 * @intparm: interruption parameter; value is only used if no I/O is
100 * outstanding, otherwise the intparm associated with the I/O request
103 * ccw_device_clear() calls csch on @cdev's subchannel.
106 * -%ENODEV on device not operational,
107 * -%EINVAL on invalid device state.
109 * Interrupts disabled, ccw device lock held
111 int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
113 struct subchannel *sch;
118 if (cdev->private->state == DEV_STATE_NOT_OPER)
120 if (cdev->private->state != DEV_STATE_ONLINE &&
121 cdev->private->state != DEV_STATE_W4SENSE)
123 sch = to_subchannel(cdev->dev.parent);
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;
165 sch = to_subchannel(cdev->dev.parent);
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.stctl & SCSW_STCTL_PRIM_STATUS) &&
183 !(sch->schib.scsw.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;
343 if (cdev->private->state == DEV_STATE_NOT_OPER)
345 if (cdev->private->state != DEV_STATE_ONLINE &&
346 cdev->private->state != DEV_STATE_W4SENSE)
348 sch = to_subchannel(cdev->dev.parent);
353 cdev->private->intparm = intparm;
358 * ccw_device_resume() - resume channel program execution
359 * @cdev: target ccw device
361 * ccw_device_resume() calls rsch on @cdev's subchannel.
364 * -%ENODEV on device not operational,
365 * -%EINVAL on invalid device state,
366 * -%EBUSY on device busy or interrupt pending.
368 * Interrupts disabled, ccw device lock held
370 int ccw_device_resume(struct ccw_device *cdev)
372 struct subchannel *sch;
376 sch = to_subchannel(cdev->dev.parent);
379 if (cdev->private->state == DEV_STATE_NOT_OPER)
381 if (cdev->private->state != DEV_STATE_ONLINE ||
382 !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
384 return cio_resume(sch);
388 * Pass interrupt to device driver.
391 ccw_device_call_handler(struct ccw_device *cdev)
393 struct subchannel *sch;
397 sch = to_subchannel(cdev->dev.parent);
400 * we allow for the device action handler if .
401 * - we received ending status
402 * - the action handler requested to see all interrupts
403 * - we received an intermediate status
404 * - fast notification was requested (primary status)
405 * - unsolicited interrupts
407 stctl = cdev->private->irb.scsw.stctl;
408 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
409 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
410 (stctl == SCSW_STCTL_STATUS_PEND);
411 if (!ending_status &&
412 !cdev->private->options.repall &&
413 !(stctl & SCSW_STCTL_INTER_STATUS) &&
414 !(cdev->private->options.fast &&
415 (stctl & SCSW_STCTL_PRIM_STATUS)))
418 /* Clear pending timers for device driver initiated I/O. */
420 ccw_device_set_timeout(cdev, 0);
422 * Now we are ready to call the device driver interrupt handler.
425 cdev->handler(cdev, cdev->private->intparm,
426 &cdev->private->irb);
429 * Clear the old and now useless interrupt response block.
431 memset(&cdev->private->irb, 0, sizeof(struct irb));
437 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
438 * @cdev: ccw device to inspect
439 * @ct: command type to look for
441 * During SenseID, command information words (CIWs) describing special
442 * commands available to the device may have been stored in the extended
443 * sense data. This function searches for CIWs of a specified command
444 * type in the extended sense data.
446 * %NULL if no extended sense data has been stored or if no CIW of the
447 * specified command type could be found,
448 * else a pointer to the CIW of the specified command type.
450 struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
454 if (cdev->private->flags.esid == 0)
456 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
457 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
458 return cdev->private->senseid.ciw + ciw_cnt;
463 * ccw_device_get_path_mask() - get currently available paths
464 * @cdev: ccw device to be queried
466 * %0 if no subchannel for the device is available,
467 * else the mask of currently available paths for the ccw device's subchannel.
469 __u8 ccw_device_get_path_mask(struct ccw_device *cdev)
471 struct subchannel *sch;
473 sch = to_subchannel(cdev->dev.parent);
481 * Try to break the lock on a boxed device.
484 ccw_device_stlck(struct ccw_device *cdev)
488 struct subchannel *sch;
494 if (cdev->drv && !cdev->private->options.force)
497 sch = to_subchannel(cdev->dev.parent);
499 CIO_TRACE_EVENT(2, "stl lock");
500 CIO_TRACE_EVENT(2, cdev->dev.bus_id);
502 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
505 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
510 spin_lock_irqsave(sch->lock, flags);
511 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
515 * Setup ccw. We chain an unconditional reserve and a release so we
516 * only break the lock.
518 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
519 cdev->private->iccws[0].cda = (__u32) __pa(buf);
520 cdev->private->iccws[0].count = 32;
521 cdev->private->iccws[0].flags = CCW_FLAG_CC;
522 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
523 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
524 cdev->private->iccws[1].count = 32;
525 cdev->private->iccws[1].flags = 0;
526 ret = cio_start(sch, cdev->private->iccws, 0);
528 cio_disable_subchannel(sch); //FIXME: return code?
531 cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
532 spin_unlock_irqrestore(sch->lock, flags);
533 wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
534 spin_lock_irqsave(sch->lock, flags);
535 cio_disable_subchannel(sch); //FIXME: return code?
536 if ((cdev->private->irb.scsw.dstat !=
537 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
538 (cdev->private->irb.scsw.cstat != 0))
541 memset(&cdev->private->irb, 0, sizeof(struct irb));
545 spin_unlock_irqrestore(sch->lock, flags);
549 void *ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
551 struct subchannel *sch;
554 sch = to_subchannel(cdev->dev.parent);
556 chpid.id = sch->schib.pmcw.chpid[chp_no];
557 return chp_get_chp_desc(chpid);
561 * ccw_device_get_id - obtain a ccw device id
562 * @cdev: device to obtain the id for
563 * @dev_id: where to fill in the values
565 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
567 *dev_id = cdev->private->dev_id;
569 EXPORT_SYMBOL(ccw_device_get_id);
571 // FIXME: these have to go:
574 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
576 return cdev->private->schid.sch_no;
580 MODULE_LICENSE("GPL");
581 EXPORT_SYMBOL(ccw_device_set_options_mask);
582 EXPORT_SYMBOL(ccw_device_set_options);
583 EXPORT_SYMBOL(ccw_device_clear_options);
584 EXPORT_SYMBOL(ccw_device_clear);
585 EXPORT_SYMBOL(ccw_device_halt);
586 EXPORT_SYMBOL(ccw_device_resume);
587 EXPORT_SYMBOL(ccw_device_start_timeout);
588 EXPORT_SYMBOL(ccw_device_start);
589 EXPORT_SYMBOL(ccw_device_start_timeout_key);
590 EXPORT_SYMBOL(ccw_device_start_key);
591 EXPORT_SYMBOL(ccw_device_get_ciw);
592 EXPORT_SYMBOL(ccw_device_get_path_mask);
593 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
594 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);