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
);
197 cdev
->private->intparm
= intparm
;
202 * ccw_device_start_timeout_key() - start a s390 channel program with timeout and key
203 * @cdev: target ccw device
204 * @cpa: logical start address of channel program
205 * @intparm: user specific interruption parameter; will be presented back to
206 * @cdev's interrupt handler. Allows a device driver to associate
207 * the interrupt with a particular I/O request.
208 * @lpm: defines the channel path to be used for a specific I/O request. A
209 * value of 0 will make cio use the opm.
210 * @key: storage key to be used for the I/O
211 * @flags: additional flags; defines the action to be performed for I/O
213 * @expires: timeout value in jiffies
215 * Start a S/390 channel program. When the interrupt arrives, the
216 * IRQ handler is called, either immediately, delayed (dev-end missing,
217 * or sense required) or never (no IRQ handler registered).
218 * This function notifies the device driver if the channel program has not
219 * completed during the time specified by @expires. If a timeout occurs, the
220 * channel program is terminated via xsch, hsch or csch, and the device's
221 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
223 * %0, if the operation was successful;
224 * -%EBUSY, if the device is busy, or status pending;
225 * -%EACCES, if no path specified in @lpm is operational;
226 * -%ENODEV, if the device is not operational.
228 * Interrupts disabled, ccw device lock held
230 int ccw_device_start_timeout_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
231 unsigned long intparm
, __u8 lpm
, __u8 key
,
232 unsigned long flags
, int expires
)
238 ccw_device_set_timeout(cdev
, expires
);
239 ret
= ccw_device_start_key(cdev
, cpa
, intparm
, lpm
, key
, flags
);
241 ccw_device_set_timeout(cdev
, 0);
246 * ccw_device_start() - start a s390 channel program
247 * @cdev: target ccw device
248 * @cpa: logical start address of channel program
249 * @intparm: user specific interruption parameter; will be presented back to
250 * @cdev's interrupt handler. Allows a device driver to associate
251 * the interrupt with a particular I/O request.
252 * @lpm: defines the channel path to be used for a specific I/O request. A
253 * value of 0 will make cio use the opm.
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(struct ccw_device
*cdev
, struct ccw1
*cpa
,
269 unsigned long intparm
, __u8 lpm
, unsigned long flags
)
271 return ccw_device_start_key(cdev
, cpa
, intparm
, lpm
,
272 PAGE_DEFAULT_KEY
, flags
);
276 * ccw_device_start_timeout() - start a s390 channel program with timeout
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
286 * @expires: timeout value in jiffies
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).
291 * This function notifies the device driver if the channel program has not
292 * completed during the time specified by @expires. If a timeout occurs, the
293 * channel program is terminated via xsch, hsch or csch, and the device's
294 * interrupt handler will be called with an irb containing ERR_PTR(-%ETIMEDOUT).
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_timeout(struct ccw_device
*cdev
, struct ccw1
*cpa
,
304 unsigned long intparm
, __u8 lpm
,
305 unsigned long flags
, int expires
)
307 return ccw_device_start_timeout_key(cdev
, cpa
, intparm
, lpm
,
308 PAGE_DEFAULT_KEY
, flags
,
314 * ccw_device_halt() - halt I/O request processing
315 * @cdev: target ccw device
316 * @intparm: interruption parameter; value is only used if no I/O is
317 * outstanding, otherwise the intparm associated with the I/O request
320 * ccw_device_halt() calls hsch on @cdev's subchannel.
323 * -%ENODEV on device not operational,
324 * -%EINVAL on invalid device state,
325 * -%EBUSY on device busy or interrupt pending.
327 * Interrupts disabled, ccw device lock held
329 int ccw_device_halt(struct ccw_device
*cdev
, unsigned long intparm
)
331 struct subchannel
*sch
;
336 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
338 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
339 cdev
->private->state
!= DEV_STATE_W4SENSE
)
341 sch
= to_subchannel(cdev
->dev
.parent
);
346 cdev
->private->intparm
= intparm
;
351 * ccw_device_resume() - resume channel program execution
352 * @cdev: target ccw device
354 * ccw_device_resume() calls rsch on @cdev's subchannel.
357 * -%ENODEV on device not operational,
358 * -%EINVAL on invalid device state,
359 * -%EBUSY on device busy or interrupt pending.
361 * Interrupts disabled, ccw device lock held
363 int ccw_device_resume(struct ccw_device
*cdev
)
365 struct subchannel
*sch
;
369 sch
= to_subchannel(cdev
->dev
.parent
);
372 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
374 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
375 !(sch
->schib
.scsw
.actl
& SCSW_ACTL_SUSPENDED
))
377 return cio_resume(sch
);
381 * Pass interrupt to device driver.
384 ccw_device_call_handler(struct ccw_device
*cdev
)
386 struct subchannel
*sch
;
390 sch
= to_subchannel(cdev
->dev
.parent
);
393 * we allow for the device action handler if .
394 * - we received ending status
395 * - the action handler requested to see all interrupts
396 * - we received an intermediate status
397 * - fast notification was requested (primary status)
398 * - unsolicited interrupts
400 stctl
= cdev
->private->irb
.scsw
.stctl
;
401 ending_status
= (stctl
& SCSW_STCTL_SEC_STATUS
) ||
402 (stctl
== (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
)) ||
403 (stctl
== SCSW_STCTL_STATUS_PEND
);
404 if (!ending_status
&&
405 !cdev
->private->options
.repall
&&
406 !(stctl
& SCSW_STCTL_INTER_STATUS
) &&
407 !(cdev
->private->options
.fast
&&
408 (stctl
& SCSW_STCTL_PRIM_STATUS
)))
411 /* Clear pending timers for device driver initiated I/O. */
413 ccw_device_set_timeout(cdev
, 0);
415 * Now we are ready to call the device driver interrupt handler.
418 cdev
->handler(cdev
, cdev
->private->intparm
,
419 &cdev
->private->irb
);
422 * Clear the old and now useless interrupt response block.
424 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
430 * ccw_device_get_ciw() - Search for CIW command in extended sense data.
431 * @cdev: ccw device to inspect
432 * @ct: command type to look for
434 * During SenseID, command information words (CIWs) describing special
435 * commands available to the device may have been stored in the extended
436 * sense data. This function searches for CIWs of a specified command
437 * type in the extended sense data.
439 * %NULL if no extended sense data has been stored or if no CIW of the
440 * specified command type could be found,
441 * else a pointer to the CIW of the specified command type.
443 struct ciw
*ccw_device_get_ciw(struct ccw_device
*cdev
, __u32 ct
)
447 if (cdev
->private->flags
.esid
== 0)
449 for (ciw_cnt
= 0; ciw_cnt
< MAX_CIWS
; ciw_cnt
++)
450 if (cdev
->private->senseid
.ciw
[ciw_cnt
].ct
== ct
)
451 return cdev
->private->senseid
.ciw
+ ciw_cnt
;
456 * ccw_device_get_path_mask() - get currently available paths
457 * @cdev: ccw device to be queried
459 * %0 if no subchannel for the device is available,
460 * else the mask of currently available paths for the ccw device's subchannel.
462 __u8
ccw_device_get_path_mask(struct ccw_device
*cdev
)
464 struct subchannel
*sch
;
466 sch
= to_subchannel(cdev
->dev
.parent
);
474 * Try to break the lock on a boxed device.
477 ccw_device_stlck(struct ccw_device
*cdev
)
481 struct subchannel
*sch
;
487 if (cdev
->drv
&& !cdev
->private->options
.force
)
490 sch
= to_subchannel(cdev
->dev
.parent
);
492 CIO_TRACE_EVENT(2, "stl lock");
493 CIO_TRACE_EVENT(2, cdev
->dev
.bus_id
);
495 buf
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
498 buf2
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
503 spin_lock_irqsave(sch
->lock
, flags
);
504 ret
= cio_enable_subchannel(sch
, 3);
508 * Setup ccw. We chain an unconditional reserve and a release so we
509 * only break the lock.
511 cdev
->private->iccws
[0].cmd_code
= CCW_CMD_STLCK
;
512 cdev
->private->iccws
[0].cda
= (__u32
) __pa(buf
);
513 cdev
->private->iccws
[0].count
= 32;
514 cdev
->private->iccws
[0].flags
= CCW_FLAG_CC
;
515 cdev
->private->iccws
[1].cmd_code
= CCW_CMD_RELEASE
;
516 cdev
->private->iccws
[1].cda
= (__u32
) __pa(buf2
);
517 cdev
->private->iccws
[1].count
= 32;
518 cdev
->private->iccws
[1].flags
= 0;
519 ret
= cio_start(sch
, cdev
->private->iccws
, 0);
521 cio_disable_subchannel(sch
); //FIXME: return code?
524 cdev
->private->irb
.scsw
.actl
|= SCSW_ACTL_START_PEND
;
525 spin_unlock_irqrestore(sch
->lock
, flags
);
526 wait_event(cdev
->private->wait_q
, cdev
->private->irb
.scsw
.actl
== 0);
527 spin_lock_irqsave(sch
->lock
, flags
);
528 cio_disable_subchannel(sch
); //FIXME: return code?
529 if ((cdev
->private->irb
.scsw
.dstat
!=
530 (DEV_STAT_CHN_END
|DEV_STAT_DEV_END
)) ||
531 (cdev
->private->irb
.scsw
.cstat
!= 0))
534 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
538 spin_unlock_irqrestore(sch
->lock
, flags
);
542 void *ccw_device_get_chp_desc(struct ccw_device
*cdev
, int chp_no
)
544 struct subchannel
*sch
;
547 sch
= to_subchannel(cdev
->dev
.parent
);
549 chpid
.id
= sch
->schib
.pmcw
.chpid
[chp_no
];
550 return chp_get_chp_desc(chpid
);
554 * ccw_device_get_id - obtain a ccw device id
555 * @cdev: device to obtain the id for
556 * @dev_id: where to fill in the values
558 void ccw_device_get_id(struct ccw_device
*cdev
, struct ccw_dev_id
*dev_id
)
560 *dev_id
= cdev
->private->dev_id
;
562 EXPORT_SYMBOL(ccw_device_get_id
);
564 // FIXME: these have to go:
567 _ccw_device_get_subchannel_number(struct ccw_device
*cdev
)
569 return cdev
->private->schid
.sch_no
;
573 MODULE_LICENSE("GPL");
574 EXPORT_SYMBOL(ccw_device_set_options_mask
);
575 EXPORT_SYMBOL(ccw_device_set_options
);
576 EXPORT_SYMBOL(ccw_device_clear_options
);
577 EXPORT_SYMBOL(ccw_device_clear
);
578 EXPORT_SYMBOL(ccw_device_halt
);
579 EXPORT_SYMBOL(ccw_device_resume
);
580 EXPORT_SYMBOL(ccw_device_start_timeout
);
581 EXPORT_SYMBOL(ccw_device_start
);
582 EXPORT_SYMBOL(ccw_device_start_timeout_key
);
583 EXPORT_SYMBOL(ccw_device_start_key
);
584 EXPORT_SYMBOL(ccw_device_get_ciw
);
585 EXPORT_SYMBOL(ccw_device_get_path_mask
);
586 EXPORT_SYMBOL(_ccw_device_get_subchannel_number
);
587 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc
);