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>
21 #include "cio_debug.h"
27 ccw_device_set_options(struct ccw_device
*cdev
, unsigned long flags
)
30 * The flag usage is mutal exclusive ...
32 if ((flags
& CCWDEV_EARLY_NOTIFICATION
) &&
33 (flags
& CCWDEV_REPORT_ALL
))
35 cdev
->private->options
.fast
= (flags
& CCWDEV_EARLY_NOTIFICATION
) != 0;
36 cdev
->private->options
.repall
= (flags
& CCWDEV_REPORT_ALL
) != 0;
37 cdev
->private->options
.pgroup
= (flags
& CCWDEV_DO_PATHGROUP
) != 0;
38 cdev
->private->options
.force
= (flags
& CCWDEV_ALLOW_FORCE
) != 0;
43 ccw_device_clear(struct ccw_device
*cdev
, unsigned long intparm
)
45 struct subchannel
*sch
;
50 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
52 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
53 cdev
->private->state
!= DEV_STATE_W4SENSE
)
55 sch
= to_subchannel(cdev
->dev
.parent
);
60 cdev
->private->intparm
= intparm
;
65 ccw_device_start_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
66 unsigned long intparm
, __u8 lpm
, __u8 key
,
69 struct subchannel
*sch
;
74 sch
= to_subchannel(cdev
->dev
.parent
);
77 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
79 if (cdev
->private->state
== DEV_STATE_VERIFY
||
80 cdev
->private->state
== DEV_STATE_CLEAR_VERIFY
) {
81 /* Remember to fake irb when finished. */
82 if (!cdev
->private->flags
.fake_irb
) {
83 cdev
->private->flags
.fake_irb
= 1;
84 cdev
->private->intparm
= intparm
;
87 /* There's already a fake I/O around. */
90 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
91 ((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
92 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
93 cdev
->private->flags
.doverify
)
95 ret
= cio_set_options (sch
, flags
);
98 /* Adjust requested path mask to excluded varied off paths. */
104 ret
= cio_start_key (sch
, cpa
, lpm
, key
);
106 cdev
->private->intparm
= intparm
;
112 ccw_device_start_timeout_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
113 unsigned long intparm
, __u8 lpm
, __u8 key
,
114 unsigned long flags
, int expires
)
120 ccw_device_set_timeout(cdev
, expires
);
121 ret
= ccw_device_start_key(cdev
, cpa
, intparm
, lpm
, key
, flags
);
123 ccw_device_set_timeout(cdev
, 0);
128 ccw_device_start(struct ccw_device
*cdev
, struct ccw1
*cpa
,
129 unsigned long intparm
, __u8 lpm
, unsigned long flags
)
131 return ccw_device_start_key(cdev
, cpa
, intparm
, lpm
,
132 PAGE_DEFAULT_KEY
, flags
);
136 ccw_device_start_timeout(struct ccw_device
*cdev
, struct ccw1
*cpa
,
137 unsigned long intparm
, __u8 lpm
, unsigned long flags
,
140 return ccw_device_start_timeout_key(cdev
, cpa
, intparm
, lpm
,
141 PAGE_DEFAULT_KEY
, flags
,
147 ccw_device_halt(struct ccw_device
*cdev
, unsigned long intparm
)
149 struct subchannel
*sch
;
154 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
156 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
157 cdev
->private->state
!= DEV_STATE_W4SENSE
)
159 sch
= to_subchannel(cdev
->dev
.parent
);
164 cdev
->private->intparm
= intparm
;
169 ccw_device_resume(struct ccw_device
*cdev
)
171 struct subchannel
*sch
;
175 sch
= to_subchannel(cdev
->dev
.parent
);
178 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
180 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
181 !(sch
->schib
.scsw
.actl
& SCSW_ACTL_SUSPENDED
))
183 return cio_resume(sch
);
187 * Pass interrupt to device driver.
190 ccw_device_call_handler(struct ccw_device
*cdev
)
192 struct subchannel
*sch
;
196 sch
= to_subchannel(cdev
->dev
.parent
);
199 * we allow for the device action handler if .
200 * - we received ending status
201 * - the action handler requested to see all interrupts
202 * - we received an intermediate status
203 * - fast notification was requested (primary status)
204 * - unsolicited interrupts
206 stctl
= cdev
->private->irb
.scsw
.stctl
;
207 ending_status
= (stctl
& SCSW_STCTL_SEC_STATUS
) ||
208 (stctl
== (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
)) ||
209 (stctl
== SCSW_STCTL_STATUS_PEND
);
210 if (!ending_status
&&
211 !cdev
->private->options
.repall
&&
212 !(stctl
& SCSW_STCTL_INTER_STATUS
) &&
213 !(cdev
->private->options
.fast
&&
214 (stctl
& SCSW_STCTL_PRIM_STATUS
)))
217 /* Clear pending timers for device driver initiated I/O. */
219 ccw_device_set_timeout(cdev
, 0);
221 * Now we are ready to call the device driver interrupt handler.
224 cdev
->handler(cdev
, cdev
->private->intparm
,
225 &cdev
->private->irb
);
228 * Clear the old and now useless interrupt response block.
230 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
236 * Search for CIW command in extended sense data.
239 ccw_device_get_ciw(struct ccw_device
*cdev
, __u32 ct
)
243 if (cdev
->private->flags
.esid
== 0)
245 for (ciw_cnt
= 0; ciw_cnt
< MAX_CIWS
; ciw_cnt
++)
246 if (cdev
->private->senseid
.ciw
[ciw_cnt
].ct
== ct
)
247 return cdev
->private->senseid
.ciw
+ ciw_cnt
;
252 ccw_device_get_path_mask(struct ccw_device
*cdev
)
254 struct subchannel
*sch
;
256 sch
= to_subchannel(cdev
->dev
.parent
);
264 ccw_device_wake_up(struct ccw_device
*cdev
, unsigned long ip
, struct irb
*irb
)
267 /* unsolicited interrupt */
270 /* Abuse intparm for error reporting. */
272 cdev
->private->intparm
= -EIO
;
273 else if (irb
->scsw
.cc
== 1)
274 /* Retry for deferred condition code. */
275 cdev
->private->intparm
= -EAGAIN
;
276 else if ((irb
->scsw
.dstat
!=
277 (DEV_STAT_CHN_END
|DEV_STAT_DEV_END
)) ||
278 (irb
->scsw
.cstat
!= 0)) {
280 * We didn't get channel end / device end. Check if path
281 * verification has been started; we can retry after it has
282 * finished. We also retry unit checks except for command reject
283 * or intervention required. Also check for long busy
286 if (cdev
->private->flags
.doverify
||
287 cdev
->private->state
== DEV_STATE_VERIFY
)
288 cdev
->private->intparm
= -EAGAIN
;
289 else if ((irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) &&
291 (SNS0_CMD_REJECT
| SNS0_INTERVENTION_REQ
)))
292 cdev
->private->intparm
= -EAGAIN
;
293 else if ((irb
->scsw
.dstat
& DEV_STAT_ATTENTION
) &&
294 (irb
->scsw
.dstat
& DEV_STAT_DEV_END
) &&
295 (irb
->scsw
.dstat
& DEV_STAT_UNIT_EXCEP
))
296 cdev
->private->intparm
= -EAGAIN
;
298 cdev
->private->intparm
= -EIO
;
301 cdev
->private->intparm
= 0;
302 wake_up(&cdev
->private->wait_q
);
306 __ccw_device_retry_loop(struct ccw_device
*cdev
, struct ccw1
*ccw
, long magic
, __u8 lpm
)
309 struct subchannel
*sch
;
311 sch
= to_subchannel(cdev
->dev
.parent
);
313 ccw_device_set_timeout(cdev
, 60 * HZ
);
314 ret
= cio_start (sch
, ccw
, lpm
);
316 ccw_device_set_timeout(cdev
, 0);
318 /* Try again later. */
319 spin_unlock_irq(sch
->lock
);
321 spin_lock_irq(sch
->lock
);
325 /* Non-retryable error. */
327 /* Wait for end of request. */
328 cdev
->private->intparm
= magic
;
329 spin_unlock_irq(sch
->lock
);
330 wait_event(cdev
->private->wait_q
,
331 (cdev
->private->intparm
== -EIO
) ||
332 (cdev
->private->intparm
== -EAGAIN
) ||
333 (cdev
->private->intparm
== 0));
334 spin_lock_irq(sch
->lock
);
335 /* Check at least for channel end / device end */
336 if (cdev
->private->intparm
== -EIO
) {
337 /* Non-retryable error. */
341 if (cdev
->private->intparm
== 0)
344 /* Try again later. */
345 spin_unlock_irq(sch
->lock
);
347 spin_lock_irq(sch
->lock
);
354 * read_dev_chars() - read device characteristics
355 * @param cdev target ccw device
356 * @param buffer pointer to buffer for rdc data
357 * @param length size of rdc data
358 * @returns 0 for success, negative error value on failure
361 * called for online device, lock not held
364 read_dev_chars (struct ccw_device
*cdev
, void **buffer
, int length
)
366 void (*handler
)(struct ccw_device
*, unsigned long, struct irb
*);
367 struct subchannel
*sch
;
369 struct ccw1
*rdc_ccw
;
373 if (!buffer
|| !length
)
375 sch
= to_subchannel(cdev
->dev
.parent
);
377 CIO_TRACE_EVENT (4, "rddevch");
378 CIO_TRACE_EVENT (4, sch
->dev
.bus_id
);
380 rdc_ccw
= kzalloc(sizeof(struct ccw1
), GFP_KERNEL
| GFP_DMA
);
383 rdc_ccw
->cmd_code
= CCW_CMD_RDC
;
384 rdc_ccw
->count
= length
;
385 rdc_ccw
->flags
= CCW_FLAG_SLI
;
386 ret
= set_normalized_cda (rdc_ccw
, (*buffer
));
392 spin_lock_irq(sch
->lock
);
393 /* Save interrupt handler. */
394 handler
= cdev
->handler
;
395 /* Temporarily install own handler. */
396 cdev
->handler
= ccw_device_wake_up
;
397 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
399 else if (((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
400 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
401 cdev
->private->flags
.doverify
)
404 /* 0x00D9C4C3 == ebcdic "RDC" */
405 ret
= __ccw_device_retry_loop(cdev
, rdc_ccw
, 0x00D9C4C3, 0);
407 /* Restore interrupt handler. */
408 cdev
->handler
= handler
;
409 spin_unlock_irq(sch
->lock
);
411 clear_normalized_cda (rdc_ccw
);
418 * Read Configuration data using path mask
421 read_conf_data_lpm (struct ccw_device
*cdev
, void **buffer
, int *length
, __u8 lpm
)
423 void (*handler
)(struct ccw_device
*, unsigned long, struct irb
*);
424 struct subchannel
*sch
;
428 struct ccw1
*rcd_ccw
;
432 if (!buffer
|| !length
)
434 sch
= to_subchannel(cdev
->dev
.parent
);
436 CIO_TRACE_EVENT (4, "rdconf");
437 CIO_TRACE_EVENT (4, sch
->dev
.bus_id
);
440 * scan for RCD command in extended SenseID data
442 ciw
= ccw_device_get_ciw(cdev
, CIW_TYPE_RCD
);
443 if (!ciw
|| ciw
->cmd
== 0)
446 /* Adjust requested path mask to excluded varied off paths. */
453 rcd_ccw
= kzalloc(sizeof(struct ccw1
), GFP_KERNEL
| GFP_DMA
);
456 rcd_buf
= kzalloc(ciw
->count
, GFP_KERNEL
| GFP_DMA
);
461 rcd_ccw
->cmd_code
= ciw
->cmd
;
462 rcd_ccw
->cda
= (__u32
) __pa (rcd_buf
);
463 rcd_ccw
->count
= ciw
->count
;
464 rcd_ccw
->flags
= CCW_FLAG_SLI
;
466 spin_lock_irq(sch
->lock
);
467 /* Save interrupt handler. */
468 handler
= cdev
->handler
;
469 /* Temporarily install own handler. */
470 cdev
->handler
= ccw_device_wake_up
;
471 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
473 else if (((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
474 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
475 cdev
->private->flags
.doverify
)
478 /* 0x00D9C3C4 == ebcdic "RCD" */
479 ret
= __ccw_device_retry_loop(cdev
, rcd_ccw
, 0x00D9C3C4, lpm
);
481 /* Restore interrupt handler. */
482 cdev
->handler
= handler
;
483 spin_unlock_irq(sch
->lock
);
486 * on success we update the user input parms
493 *length
= ciw
->count
;
502 * Read Configuration data
505 read_conf_data (struct ccw_device
*cdev
, void **buffer
, int *length
)
507 return read_conf_data_lpm (cdev
, buffer
, length
, 0);
511 * Try to break the lock on a boxed device.
514 ccw_device_stlck(struct ccw_device
*cdev
)
518 struct subchannel
*sch
;
524 if (cdev
->drv
&& !cdev
->private->options
.force
)
527 sch
= to_subchannel(cdev
->dev
.parent
);
529 CIO_TRACE_EVENT(2, "stl lock");
530 CIO_TRACE_EVENT(2, cdev
->dev
.bus_id
);
532 buf
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
535 buf2
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
540 spin_lock_irqsave(sch
->lock
, flags
);
541 ret
= cio_enable_subchannel(sch
, 3);
545 * Setup ccw. We chain an unconditional reserve and a release so we
546 * only break the lock.
548 cdev
->private->iccws
[0].cmd_code
= CCW_CMD_STLCK
;
549 cdev
->private->iccws
[0].cda
= (__u32
) __pa(buf
);
550 cdev
->private->iccws
[0].count
= 32;
551 cdev
->private->iccws
[0].flags
= CCW_FLAG_CC
;
552 cdev
->private->iccws
[1].cmd_code
= CCW_CMD_RELEASE
;
553 cdev
->private->iccws
[1].cda
= (__u32
) __pa(buf2
);
554 cdev
->private->iccws
[1].count
= 32;
555 cdev
->private->iccws
[1].flags
= 0;
556 ret
= cio_start(sch
, cdev
->private->iccws
, 0);
558 cio_disable_subchannel(sch
); //FIXME: return code?
561 cdev
->private->irb
.scsw
.actl
|= SCSW_ACTL_START_PEND
;
562 spin_unlock_irqrestore(sch
->lock
, flags
);
563 wait_event(cdev
->private->wait_q
, cdev
->private->irb
.scsw
.actl
== 0);
564 spin_lock_irqsave(sch
->lock
, flags
);
565 cio_disable_subchannel(sch
); //FIXME: return code?
566 if ((cdev
->private->irb
.scsw
.dstat
!=
567 (DEV_STAT_CHN_END
|DEV_STAT_DEV_END
)) ||
568 (cdev
->private->irb
.scsw
.cstat
!= 0))
571 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
575 spin_unlock_irqrestore(sch
->lock
, flags
);
580 ccw_device_get_chp_desc(struct ccw_device
*cdev
, int chp_no
)
582 struct subchannel
*sch
;
584 sch
= to_subchannel(cdev
->dev
.parent
);
585 return chsc_get_chp_desc(sch
, chp_no
);
588 // FIXME: these have to go:
591 _ccw_device_get_subchannel_number(struct ccw_device
*cdev
)
593 return cdev
->private->schid
.sch_no
;
597 _ccw_device_get_device_number(struct ccw_device
*cdev
)
599 return cdev
->private->dev_id
.devno
;
603 MODULE_LICENSE("GPL");
604 EXPORT_SYMBOL(ccw_device_set_options
);
605 EXPORT_SYMBOL(ccw_device_clear
);
606 EXPORT_SYMBOL(ccw_device_halt
);
607 EXPORT_SYMBOL(ccw_device_resume
);
608 EXPORT_SYMBOL(ccw_device_start_timeout
);
609 EXPORT_SYMBOL(ccw_device_start
);
610 EXPORT_SYMBOL(ccw_device_start_timeout_key
);
611 EXPORT_SYMBOL(ccw_device_start_key
);
612 EXPORT_SYMBOL(ccw_device_get_ciw
);
613 EXPORT_SYMBOL(ccw_device_get_path_mask
);
614 EXPORT_SYMBOL(read_conf_data
);
615 EXPORT_SYMBOL(read_dev_chars
);
616 EXPORT_SYMBOL(_ccw_device_get_subchannel_number
);
617 EXPORT_SYMBOL(_ccw_device_get_device_number
);
618 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc
);
619 EXPORT_SYMBOL_GPL(read_conf_data_lpm
);