2 * drivers/s390/cio/device_ops.c
6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
20 #include <asm/ccwdev.h>
21 #include <asm/idals.h>
25 #include "cio_debug.h"
32 ccw_device_set_options(struct ccw_device
*cdev
, unsigned long flags
)
35 * The flag usage is mutal exclusive ...
37 if ((flags
& CCWDEV_EARLY_NOTIFICATION
) &&
38 (flags
& CCWDEV_REPORT_ALL
))
40 cdev
->private->options
.fast
= (flags
& CCWDEV_EARLY_NOTIFICATION
) != 0;
41 cdev
->private->options
.repall
= (flags
& CCWDEV_REPORT_ALL
) != 0;
42 cdev
->private->options
.pgroup
= (flags
& CCWDEV_DO_PATHGROUP
) != 0;
43 cdev
->private->options
.force
= (flags
& CCWDEV_ALLOW_FORCE
) != 0;
48 ccw_device_clear(struct ccw_device
*cdev
, unsigned long intparm
)
50 struct subchannel
*sch
;
55 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
57 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
58 cdev
->private->state
!= DEV_STATE_WAIT4IO
&&
59 cdev
->private->state
!= DEV_STATE_W4SENSE
)
61 sch
= to_subchannel(cdev
->dev
.parent
);
66 cdev
->private->intparm
= intparm
;
71 ccw_device_start_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
72 unsigned long intparm
, __u8 lpm
, __u8 key
,
75 struct subchannel
*sch
;
80 sch
= to_subchannel(cdev
->dev
.parent
);
83 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
85 if (cdev
->private->state
== DEV_STATE_VERIFY
) {
86 /* Remember to fake irb when finished. */
87 if (!cdev
->private->flags
.fake_irb
) {
88 cdev
->private->flags
.fake_irb
= 1;
89 cdev
->private->intparm
= intparm
;
92 /* There's already a fake I/O around. */
95 if (cdev
->private->state
!= DEV_STATE_ONLINE
||
96 ((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
97 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
98 cdev
->private->flags
.doverify
)
100 ret
= cio_set_options (sch
, flags
);
103 ret
= cio_start_key (sch
, cpa
, lpm
, key
);
105 cdev
->private->intparm
= intparm
;
111 ccw_device_start_timeout_key(struct ccw_device
*cdev
, struct ccw1
*cpa
,
112 unsigned long intparm
, __u8 lpm
, __u8 key
,
113 unsigned long flags
, int expires
)
119 ccw_device_set_timeout(cdev
, expires
);
120 ret
= ccw_device_start_key(cdev
, cpa
, intparm
, lpm
, key
, flags
);
122 ccw_device_set_timeout(cdev
, 0);
127 ccw_device_start(struct ccw_device
*cdev
, struct ccw1
*cpa
,
128 unsigned long intparm
, __u8 lpm
, unsigned long flags
)
130 return ccw_device_start_key(cdev
, cpa
, intparm
, lpm
,
131 PAGE_DEFAULT_KEY
, flags
);
135 ccw_device_start_timeout(struct ccw_device
*cdev
, struct ccw1
*cpa
,
136 unsigned long intparm
, __u8 lpm
, unsigned long flags
,
139 return ccw_device_start_timeout_key(cdev
, cpa
, intparm
, lpm
,
140 PAGE_DEFAULT_KEY
, flags
,
146 ccw_device_halt(struct ccw_device
*cdev
, unsigned long intparm
)
148 struct subchannel
*sch
;
153 if (cdev
->private->state
== DEV_STATE_NOT_OPER
)
155 if (cdev
->private->state
!= DEV_STATE_ONLINE
&&
156 cdev
->private->state
!= DEV_STATE_WAIT4IO
&&
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
)))
218 * Now we are ready to call the device driver interrupt handler.
221 cdev
->handler(cdev
, cdev
->private->intparm
,
222 &cdev
->private->irb
);
225 * Clear the old and now useless interrupt response block.
227 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
233 * Search for CIW command in extended sense data.
236 ccw_device_get_ciw(struct ccw_device
*cdev
, __u32 ct
)
240 if (cdev
->private->flags
.esid
== 0)
242 for (ciw_cnt
= 0; ciw_cnt
< MAX_CIWS
; ciw_cnt
++)
243 if (cdev
->private->senseid
.ciw
[ciw_cnt
].ct
== ct
)
244 return cdev
->private->senseid
.ciw
+ ciw_cnt
;
249 ccw_device_get_path_mask(struct ccw_device
*cdev
)
251 struct subchannel
*sch
;
253 sch
= to_subchannel(cdev
->dev
.parent
);
261 ccw_device_wake_up(struct ccw_device
*cdev
, unsigned long ip
, struct irb
*irb
)
264 /* unsolicited interrupt */
267 /* Abuse intparm for error reporting. */
269 cdev
->private->intparm
= -EIO
;
270 else if ((irb
->scsw
.dstat
!=
271 (DEV_STAT_CHN_END
|DEV_STAT_DEV_END
)) ||
272 (irb
->scsw
.cstat
!= 0)) {
274 * We didn't get channel end / device end. Check if path
275 * verification has been started; we can retry after it has
276 * finished. We also retry unit checks except for command reject
277 * or intervention required.
279 if (cdev
->private->flags
.doverify
||
280 cdev
->private->state
== DEV_STATE_VERIFY
)
281 cdev
->private->intparm
= -EAGAIN
;
282 if ((irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) &&
284 (SNS0_CMD_REJECT
| SNS0_INTERVENTION_REQ
)))
285 cdev
->private->intparm
= -EAGAIN
;
287 cdev
->private->intparm
= -EIO
;
290 cdev
->private->intparm
= 0;
291 wake_up(&cdev
->private->wait_q
);
295 __ccw_device_retry_loop(struct ccw_device
*cdev
, struct ccw1
*ccw
, long magic
, __u8 lpm
)
298 struct subchannel
*sch
;
300 sch
= to_subchannel(cdev
->dev
.parent
);
302 ret
= cio_start (sch
, ccw
, lpm
);
303 if ((ret
== -EBUSY
) || (ret
== -EACCES
)) {
304 /* Try again later. */
305 spin_unlock_irq(&sch
->lock
);
307 spin_lock_irq(&sch
->lock
);
311 /* Non-retryable error. */
313 /* Wait for end of request. */
314 cdev
->private->intparm
= magic
;
315 spin_unlock_irq(&sch
->lock
);
316 wait_event(cdev
->private->wait_q
,
317 (cdev
->private->intparm
== -EIO
) ||
318 (cdev
->private->intparm
== -EAGAIN
) ||
319 (cdev
->private->intparm
== 0));
320 spin_lock_irq(&sch
->lock
);
321 /* Check at least for channel end / device end */
322 if (cdev
->private->intparm
== -EIO
) {
323 /* Non-retryable error. */
327 if (cdev
->private->intparm
== 0)
330 /* Try again later. */
331 spin_unlock_irq(&sch
->lock
);
333 spin_lock_irq(&sch
->lock
);
340 * read_dev_chars() - read device characteristics
341 * @param cdev target ccw device
342 * @param buffer pointer to buffer for rdc data
343 * @param length size of rdc data
344 * @returns 0 for success, negative error value on failure
347 * called for online device, lock not held
350 read_dev_chars (struct ccw_device
*cdev
, void **buffer
, int length
)
352 void (*handler
)(struct ccw_device
*, unsigned long, struct irb
*);
353 struct subchannel
*sch
;
355 struct ccw1
*rdc_ccw
;
359 if (!buffer
|| !length
)
361 sch
= to_subchannel(cdev
->dev
.parent
);
363 CIO_TRACE_EVENT (4, "rddevch");
364 CIO_TRACE_EVENT (4, sch
->dev
.bus_id
);
366 rdc_ccw
= kmalloc(sizeof(struct ccw1
), GFP_KERNEL
| GFP_DMA
);
369 memset(rdc_ccw
, 0, sizeof(struct ccw1
));
370 rdc_ccw
->cmd_code
= CCW_CMD_RDC
;
371 rdc_ccw
->count
= length
;
372 rdc_ccw
->flags
= CCW_FLAG_SLI
;
373 ret
= set_normalized_cda (rdc_ccw
, (*buffer
));
379 spin_lock_irq(&sch
->lock
);
380 /* Save interrupt handler. */
381 handler
= cdev
->handler
;
382 /* Temporarily install own handler. */
383 cdev
->handler
= ccw_device_wake_up
;
384 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
386 else if (((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
387 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
388 cdev
->private->flags
.doverify
)
391 /* 0x00D9C4C3 == ebcdic "RDC" */
392 ret
= __ccw_device_retry_loop(cdev
, rdc_ccw
, 0x00D9C4C3, 0);
394 /* Restore interrupt handler. */
395 cdev
->handler
= handler
;
396 spin_unlock_irq(&sch
->lock
);
398 clear_normalized_cda (rdc_ccw
);
405 * Read Configuration data using path mask
408 read_conf_data_lpm (struct ccw_device
*cdev
, void **buffer
, int *length
, __u8 lpm
)
410 void (*handler
)(struct ccw_device
*, unsigned long, struct irb
*);
411 struct subchannel
*sch
;
415 struct ccw1
*rcd_ccw
;
419 if (!buffer
|| !length
)
421 sch
= to_subchannel(cdev
->dev
.parent
);
423 CIO_TRACE_EVENT (4, "rdconf");
424 CIO_TRACE_EVENT (4, sch
->dev
.bus_id
);
427 * scan for RCD command in extended SenseID data
429 ciw
= ccw_device_get_ciw(cdev
, CIW_TYPE_RCD
);
430 if (!ciw
|| ciw
->cmd
== 0)
433 rcd_ccw
= kmalloc(sizeof(struct ccw1
), GFP_KERNEL
| GFP_DMA
);
436 memset(rcd_ccw
, 0, sizeof(struct ccw1
));
437 rcd_buf
= kmalloc(ciw
->count
, GFP_KERNEL
| GFP_DMA
);
442 memset (rcd_buf
, 0, ciw
->count
);
443 rcd_ccw
->cmd_code
= ciw
->cmd
;
444 rcd_ccw
->cda
= (__u32
) __pa (rcd_buf
);
445 rcd_ccw
->count
= ciw
->count
;
446 rcd_ccw
->flags
= CCW_FLAG_SLI
;
448 spin_lock_irq(&sch
->lock
);
449 /* Save interrupt handler. */
450 handler
= cdev
->handler
;
451 /* Temporarily install own handler. */
452 cdev
->handler
= ccw_device_wake_up
;
453 if (cdev
->private->state
!= DEV_STATE_ONLINE
)
455 else if (((sch
->schib
.scsw
.stctl
& SCSW_STCTL_PRIM_STATUS
) &&
456 !(sch
->schib
.scsw
.stctl
& SCSW_STCTL_SEC_STATUS
)) ||
457 cdev
->private->flags
.doverify
)
460 /* 0x00D9C3C4 == ebcdic "RCD" */
461 ret
= __ccw_device_retry_loop(cdev
, rcd_ccw
, 0x00D9C3C4, lpm
);
463 /* Restore interrupt handler. */
464 cdev
->handler
= handler
;
465 spin_unlock_irq(&sch
->lock
);
468 * on success we update the user input parms
475 *length
= ciw
->count
;
484 * Read Configuration data
487 read_conf_data (struct ccw_device
*cdev
, void **buffer
, int *length
)
489 return read_conf_data_lpm (cdev
, buffer
, length
, 0);
493 * Try to break the lock on a boxed device.
496 ccw_device_stlck(struct ccw_device
*cdev
)
500 struct subchannel
*sch
;
506 if (cdev
->drv
&& !cdev
->private->options
.force
)
509 sch
= to_subchannel(cdev
->dev
.parent
);
511 CIO_TRACE_EVENT(2, "stl lock");
512 CIO_TRACE_EVENT(2, cdev
->dev
.bus_id
);
514 buf
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
517 buf2
= kmalloc(32*sizeof(char), GFP_DMA
|GFP_KERNEL
);
522 spin_lock_irqsave(&sch
->lock
, flags
);
523 ret
= cio_enable_subchannel(sch
, 3);
527 * Setup ccw. We chain an unconditional reserve and a release so we
528 * only break the lock.
530 cdev
->private->iccws
[0].cmd_code
= CCW_CMD_STLCK
;
531 cdev
->private->iccws
[0].cda
= (__u32
) __pa(buf
);
532 cdev
->private->iccws
[0].count
= 32;
533 cdev
->private->iccws
[0].flags
= CCW_FLAG_CC
;
534 cdev
->private->iccws
[1].cmd_code
= CCW_CMD_RELEASE
;
535 cdev
->private->iccws
[1].cda
= (__u32
) __pa(buf2
);
536 cdev
->private->iccws
[1].count
= 32;
537 cdev
->private->iccws
[1].flags
= 0;
538 ret
= cio_start(sch
, cdev
->private->iccws
, 0);
540 cio_disable_subchannel(sch
); //FIXME: return code?
543 cdev
->private->irb
.scsw
.actl
|= SCSW_ACTL_START_PEND
;
544 spin_unlock_irqrestore(&sch
->lock
, flags
);
545 wait_event(cdev
->private->wait_q
, cdev
->private->irb
.scsw
.actl
== 0);
546 spin_lock_irqsave(&sch
->lock
, flags
);
547 cio_disable_subchannel(sch
); //FIXME: return code?
548 if ((cdev
->private->irb
.scsw
.dstat
!=
549 (DEV_STAT_CHN_END
|DEV_STAT_DEV_END
)) ||
550 (cdev
->private->irb
.scsw
.cstat
!= 0))
553 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
559 spin_unlock_irqrestore(&sch
->lock
, flags
);
564 ccw_device_get_chp_desc(struct ccw_device
*cdev
, int chp_no
)
566 struct subchannel
*sch
;
568 sch
= to_subchannel(cdev
->dev
.parent
);
569 return chsc_get_chp_desc(sch
, chp_no
);
572 // FIXME: these have to go:
575 _ccw_device_get_subchannel_number(struct ccw_device
*cdev
)
577 return cdev
->private->irq
;
581 _ccw_device_get_device_number(struct ccw_device
*cdev
)
583 return cdev
->private->devno
;
587 MODULE_LICENSE("GPL");
588 EXPORT_SYMBOL(ccw_device_set_options
);
589 EXPORT_SYMBOL(ccw_device_clear
);
590 EXPORT_SYMBOL(ccw_device_halt
);
591 EXPORT_SYMBOL(ccw_device_resume
);
592 EXPORT_SYMBOL(ccw_device_start_timeout
);
593 EXPORT_SYMBOL(ccw_device_start
);
594 EXPORT_SYMBOL(ccw_device_start_timeout_key
);
595 EXPORT_SYMBOL(ccw_device_start_key
);
596 EXPORT_SYMBOL(ccw_device_get_ciw
);
597 EXPORT_SYMBOL(ccw_device_get_path_mask
);
598 EXPORT_SYMBOL(read_conf_data
);
599 EXPORT_SYMBOL(read_dev_chars
);
600 EXPORT_SYMBOL(_ccw_device_get_subchannel_number
);
601 EXPORT_SYMBOL(_ccw_device_get_device_number
);
602 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc
);
603 EXPORT_SYMBOL_GPL(read_conf_data_lpm
);