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/config.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
18 #include <asm/ccwdev.h>
19 #include <asm/idals.h>
22 #include "cio_debug.h"
28 ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
31 * The flag usage is mutal exclusive ...
33 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
34 (flags & CCWDEV_REPORT_ALL))
36 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
37 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
38 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
39 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
44 ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
46 struct subchannel *sch;
51 if (cdev->private->state == DEV_STATE_NOT_OPER)
53 if (cdev->private->state != DEV_STATE_ONLINE &&
54 cdev->private->state != DEV_STATE_WAIT4IO &&
55 cdev->private->state != DEV_STATE_W4SENSE)
57 sch = to_subchannel(cdev->dev.parent);
62 cdev->private->intparm = intparm;
67 ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
68 unsigned long intparm, __u8 lpm, __u8 key,
71 struct subchannel *sch;
76 sch = to_subchannel(cdev->dev.parent);
79 if (cdev->private->state == DEV_STATE_NOT_OPER)
81 if (cdev->private->state == DEV_STATE_VERIFY ||
82 cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
83 /* Remember to fake irb when finished. */
84 if (!cdev->private->flags.fake_irb) {
85 cdev->private->flags.fake_irb = 1;
86 cdev->private->intparm = intparm;
89 /* There's already a fake I/O around. */
92 if (cdev->private->state != DEV_STATE_ONLINE ||
93 ((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
94 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
95 cdev->private->flags.doverify)
97 ret = cio_set_options (sch, flags);
100 ret = cio_start_key (sch, cpa, lpm, key);
102 cdev->private->intparm = intparm;
108 ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
109 unsigned long intparm, __u8 lpm, __u8 key,
110 unsigned long flags, int expires)
116 ccw_device_set_timeout(cdev, expires);
117 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
119 ccw_device_set_timeout(cdev, 0);
124 ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
125 unsigned long intparm, __u8 lpm, unsigned long flags)
127 return ccw_device_start_key(cdev, cpa, intparm, lpm,
128 PAGE_DEFAULT_KEY, flags);
132 ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
133 unsigned long intparm, __u8 lpm, unsigned long flags,
136 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
137 PAGE_DEFAULT_KEY, flags,
143 ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
145 struct subchannel *sch;
150 if (cdev->private->state == DEV_STATE_NOT_OPER)
152 if (cdev->private->state != DEV_STATE_ONLINE &&
153 cdev->private->state != DEV_STATE_WAIT4IO &&
154 cdev->private->state != DEV_STATE_W4SENSE)
156 sch = to_subchannel(cdev->dev.parent);
161 cdev->private->intparm = intparm;
166 ccw_device_resume(struct ccw_device *cdev)
168 struct subchannel *sch;
172 sch = to_subchannel(cdev->dev.parent);
175 if (cdev->private->state == DEV_STATE_NOT_OPER)
177 if (cdev->private->state != DEV_STATE_ONLINE ||
178 !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
180 return cio_resume(sch);
184 * Pass interrupt to device driver.
187 ccw_device_call_handler(struct ccw_device *cdev)
189 struct subchannel *sch;
193 sch = to_subchannel(cdev->dev.parent);
196 * we allow for the device action handler if .
197 * - we received ending status
198 * - the action handler requested to see all interrupts
199 * - we received an intermediate status
200 * - fast notification was requested (primary status)
201 * - unsolicited interrupts
203 stctl = cdev->private->irb.scsw.stctl;
204 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
205 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
206 (stctl == SCSW_STCTL_STATUS_PEND);
207 if (!ending_status &&
208 !cdev->private->options.repall &&
209 !(stctl & SCSW_STCTL_INTER_STATUS) &&
210 !(cdev->private->options.fast &&
211 (stctl & SCSW_STCTL_PRIM_STATUS)))
215 * Now we are ready to call the device driver interrupt handler.
218 cdev->handler(cdev, cdev->private->intparm,
219 &cdev->private->irb);
222 * Clear the old and now useless interrupt response block.
224 memset(&cdev->private->irb, 0, sizeof(struct irb));
230 * Search for CIW command in extended sense data.
233 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
237 if (cdev->private->flags.esid == 0)
239 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
240 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
241 return cdev->private->senseid.ciw + ciw_cnt;
246 ccw_device_get_path_mask(struct ccw_device *cdev)
248 struct subchannel *sch;
250 sch = to_subchannel(cdev->dev.parent);
258 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
261 /* unsolicited interrupt */
264 /* Abuse intparm for error reporting. */
266 cdev->private->intparm = -EIO;
267 else if ((irb->scsw.dstat !=
268 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
269 (irb->scsw.cstat != 0)) {
271 * We didn't get channel end / device end. Check if path
272 * verification has been started; we can retry after it has
273 * finished. We also retry unit checks except for command reject
274 * or intervention required. Also check for long busy
277 if (cdev->private->flags.doverify ||
278 cdev->private->state == DEV_STATE_VERIFY)
279 cdev->private->intparm = -EAGAIN;
280 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
282 (SNS0_CMD_REJECT | SNS0_INTERVENTION_REQ)))
283 cdev->private->intparm = -EAGAIN;
284 else if ((irb->scsw.dstat & DEV_STAT_ATTENTION) &&
285 (irb->scsw.dstat & DEV_STAT_DEV_END) &&
286 (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP))
287 cdev->private->intparm = -EAGAIN;
289 cdev->private->intparm = -EIO;
292 cdev->private->intparm = 0;
293 wake_up(&cdev->private->wait_q);
297 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic, __u8 lpm)
300 struct subchannel *sch;
302 sch = to_subchannel(cdev->dev.parent);
304 ret = cio_start (sch, ccw, lpm);
305 if ((ret == -EBUSY) || (ret == -EACCES)) {
306 /* Try again later. */
307 spin_unlock_irq(&sch->lock);
309 spin_lock_irq(&sch->lock);
313 /* Non-retryable error. */
315 /* Wait for end of request. */
316 cdev->private->intparm = magic;
317 spin_unlock_irq(&sch->lock);
318 wait_event(cdev->private->wait_q,
319 (cdev->private->intparm == -EIO) ||
320 (cdev->private->intparm == -EAGAIN) ||
321 (cdev->private->intparm == 0));
322 spin_lock_irq(&sch->lock);
323 /* Check at least for channel end / device end */
324 if (cdev->private->intparm == -EIO) {
325 /* Non-retryable error. */
329 if (cdev->private->intparm == 0)
332 /* Try again later. */
333 spin_unlock_irq(&sch->lock);
335 spin_lock_irq(&sch->lock);
342 * read_dev_chars() - read device characteristics
343 * @param cdev target ccw device
344 * @param buffer pointer to buffer for rdc data
345 * @param length size of rdc data
346 * @returns 0 for success, negative error value on failure
349 * called for online device, lock not held
352 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
354 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
355 struct subchannel *sch;
357 struct ccw1 *rdc_ccw;
361 if (!buffer || !length)
363 sch = to_subchannel(cdev->dev.parent);
365 CIO_TRACE_EVENT (4, "rddevch");
366 CIO_TRACE_EVENT (4, sch->dev.bus_id);
368 rdc_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
371 rdc_ccw->cmd_code = CCW_CMD_RDC;
372 rdc_ccw->count = length;
373 rdc_ccw->flags = CCW_FLAG_SLI;
374 ret = set_normalized_cda (rdc_ccw, (*buffer));
380 spin_lock_irq(&sch->lock);
381 /* Save interrupt handler. */
382 handler = cdev->handler;
383 /* Temporarily install own handler. */
384 cdev->handler = ccw_device_wake_up;
385 if (cdev->private->state != DEV_STATE_ONLINE)
387 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
388 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
389 cdev->private->flags.doverify)
392 /* 0x00D9C4C3 == ebcdic "RDC" */
393 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3, 0);
395 /* Restore interrupt handler. */
396 cdev->handler = handler;
397 spin_unlock_irq(&sch->lock);
399 clear_normalized_cda (rdc_ccw);
406 * Read Configuration data using path mask
409 read_conf_data_lpm (struct ccw_device *cdev, void **buffer, int *length, __u8 lpm)
411 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
412 struct subchannel *sch;
416 struct ccw1 *rcd_ccw;
420 if (!buffer || !length)
422 sch = to_subchannel(cdev->dev.parent);
424 CIO_TRACE_EVENT (4, "rdconf");
425 CIO_TRACE_EVENT (4, sch->dev.bus_id);
428 * scan for RCD command in extended SenseID data
430 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
431 if (!ciw || ciw->cmd == 0)
434 rcd_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
437 rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
442 rcd_ccw->cmd_code = ciw->cmd;
443 rcd_ccw->cda = (__u32) __pa (rcd_buf);
444 rcd_ccw->count = ciw->count;
445 rcd_ccw->flags = CCW_FLAG_SLI;
447 spin_lock_irq(&sch->lock);
448 /* Save interrupt handler. */
449 handler = cdev->handler;
450 /* Temporarily install own handler. */
451 cdev->handler = ccw_device_wake_up;
452 if (cdev->private->state != DEV_STATE_ONLINE)
454 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
455 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
456 cdev->private->flags.doverify)
459 /* 0x00D9C3C4 == ebcdic "RCD" */
460 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4, lpm);
462 /* Restore interrupt handler. */
463 cdev->handler = handler;
464 spin_unlock_irq(&sch->lock);
467 * on success we update the user input parms
474 *length = ciw->count;
483 * Read Configuration data
486 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
488 return read_conf_data_lpm (cdev, buffer, length, 0);
492 * Try to break the lock on a boxed device.
495 ccw_device_stlck(struct ccw_device *cdev)
499 struct subchannel *sch;
505 if (cdev->drv && !cdev->private->options.force)
508 sch = to_subchannel(cdev->dev.parent);
510 CIO_TRACE_EVENT(2, "stl lock");
511 CIO_TRACE_EVENT(2, cdev->dev.bus_id);
513 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
516 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
521 spin_lock_irqsave(&sch->lock, flags);
522 ret = cio_enable_subchannel(sch, 3);
526 * Setup ccw. We chain an unconditional reserve and a release so we
527 * only break the lock.
529 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
530 cdev->private->iccws[0].cda = (__u32) __pa(buf);
531 cdev->private->iccws[0].count = 32;
532 cdev->private->iccws[0].flags = CCW_FLAG_CC;
533 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
534 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
535 cdev->private->iccws[1].count = 32;
536 cdev->private->iccws[1].flags = 0;
537 ret = cio_start(sch, cdev->private->iccws, 0);
539 cio_disable_subchannel(sch); //FIXME: return code?
542 cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
543 spin_unlock_irqrestore(&sch->lock, flags);
544 wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
545 spin_lock_irqsave(&sch->lock, flags);
546 cio_disable_subchannel(sch); //FIXME: return code?
547 if ((cdev->private->irb.scsw.dstat !=
548 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
549 (cdev->private->irb.scsw.cstat != 0))
552 memset(&cdev->private->irb, 0, sizeof(struct irb));
556 spin_unlock_irqrestore(&sch->lock, flags);
561 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
563 struct subchannel *sch;
565 sch = to_subchannel(cdev->dev.parent);
566 return chsc_get_chp_desc(sch, chp_no);
569 // FIXME: these have to go:
572 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
574 return cdev->private->sch_no;
578 _ccw_device_get_device_number(struct ccw_device *cdev)
580 return cdev->private->devno;
584 MODULE_LICENSE("GPL");
585 EXPORT_SYMBOL(ccw_device_set_options);
586 EXPORT_SYMBOL(ccw_device_clear);
587 EXPORT_SYMBOL(ccw_device_halt);
588 EXPORT_SYMBOL(ccw_device_resume);
589 EXPORT_SYMBOL(ccw_device_start_timeout);
590 EXPORT_SYMBOL(ccw_device_start);
591 EXPORT_SYMBOL(ccw_device_start_timeout_key);
592 EXPORT_SYMBOL(ccw_device_start_key);
593 EXPORT_SYMBOL(ccw_device_get_ciw);
594 EXPORT_SYMBOL(ccw_device_get_path_mask);
595 EXPORT_SYMBOL(read_conf_data);
596 EXPORT_SYMBOL(read_dev_chars);
597 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
598 EXPORT_SYMBOL(_ccw_device_get_device_number);
599 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
600 EXPORT_SYMBOL_GPL(read_conf_data_lpm);