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_WAIT4IO &&
54 cdev->private->state != DEV_STATE_W4SENSE)
56 sch = to_subchannel(cdev->dev.parent);
61 cdev->private->intparm = intparm;
66 ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
67 unsigned long intparm, __u8 lpm, __u8 key,
70 struct subchannel *sch;
75 sch = to_subchannel(cdev->dev.parent);
78 if (cdev->private->state == DEV_STATE_NOT_OPER)
80 if (cdev->private->state == DEV_STATE_VERIFY ||
81 cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
82 /* Remember to fake irb when finished. */
83 if (!cdev->private->flags.fake_irb) {
84 cdev->private->flags.fake_irb = 1;
85 cdev->private->intparm = intparm;
88 /* There's already a fake I/O around. */
91 if (cdev->private->state != DEV_STATE_ONLINE ||
92 ((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
93 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
94 cdev->private->flags.doverify)
96 ret = cio_set_options (sch, flags);
99 ret = cio_start_key (sch, cpa, lpm, key);
101 cdev->private->intparm = intparm;
107 ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
108 unsigned long intparm, __u8 lpm, __u8 key,
109 unsigned long flags, int expires)
115 ccw_device_set_timeout(cdev, expires);
116 ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
118 ccw_device_set_timeout(cdev, 0);
123 ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
124 unsigned long intparm, __u8 lpm, unsigned long flags)
126 return ccw_device_start_key(cdev, cpa, intparm, lpm,
127 PAGE_DEFAULT_KEY, flags);
131 ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
132 unsigned long intparm, __u8 lpm, unsigned long flags,
135 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
136 PAGE_DEFAULT_KEY, flags,
142 ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
144 struct subchannel *sch;
149 if (cdev->private->state == DEV_STATE_NOT_OPER)
151 if (cdev->private->state != DEV_STATE_ONLINE &&
152 cdev->private->state != DEV_STATE_WAIT4IO &&
153 cdev->private->state != DEV_STATE_W4SENSE)
155 sch = to_subchannel(cdev->dev.parent);
160 cdev->private->intparm = intparm;
165 ccw_device_resume(struct ccw_device *cdev)
167 struct subchannel *sch;
171 sch = to_subchannel(cdev->dev.parent);
174 if (cdev->private->state == DEV_STATE_NOT_OPER)
176 if (cdev->private->state != DEV_STATE_ONLINE ||
177 !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
179 return cio_resume(sch);
183 * Pass interrupt to device driver.
186 ccw_device_call_handler(struct ccw_device *cdev)
188 struct subchannel *sch;
192 sch = to_subchannel(cdev->dev.parent);
195 * we allow for the device action handler if .
196 * - we received ending status
197 * - the action handler requested to see all interrupts
198 * - we received an intermediate status
199 * - fast notification was requested (primary status)
200 * - unsolicited interrupts
202 stctl = cdev->private->irb.scsw.stctl;
203 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
204 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
205 (stctl == SCSW_STCTL_STATUS_PEND);
206 if (!ending_status &&
207 !cdev->private->options.repall &&
208 !(stctl & SCSW_STCTL_INTER_STATUS) &&
209 !(cdev->private->options.fast &&
210 (stctl & SCSW_STCTL_PRIM_STATUS)))
214 * Now we are ready to call the device driver interrupt handler.
217 cdev->handler(cdev, cdev->private->intparm,
218 &cdev->private->irb);
221 * Clear the old and now useless interrupt response block.
223 memset(&cdev->private->irb, 0, sizeof(struct irb));
229 * Search for CIW command in extended sense data.
232 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
236 if (cdev->private->flags.esid == 0)
238 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
239 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
240 return cdev->private->senseid.ciw + ciw_cnt;
245 ccw_device_get_path_mask(struct ccw_device *cdev)
247 struct subchannel *sch;
249 sch = to_subchannel(cdev->dev.parent);
257 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
260 /* unsolicited interrupt */
263 /* Abuse intparm for error reporting. */
265 cdev->private->intparm = -EIO;
266 else if (irb->scsw.cc == 1)
267 /* Retry for deferred condition code. */
268 cdev->private->intparm = -EAGAIN;
269 else if ((irb->scsw.dstat !=
270 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
271 (irb->scsw.cstat != 0)) {
273 * We didn't get channel end / device end. Check if path
274 * verification has been started; we can retry after it has
275 * finished. We also retry unit checks except for command reject
276 * or intervention required. Also check for long busy
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;
286 else if ((irb->scsw.dstat & DEV_STAT_ATTENTION) &&
287 (irb->scsw.dstat & DEV_STAT_DEV_END) &&
288 (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP))
289 cdev->private->intparm = -EAGAIN;
291 cdev->private->intparm = -EIO;
294 cdev->private->intparm = 0;
295 wake_up(&cdev->private->wait_q);
299 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic, __u8 lpm)
302 struct subchannel *sch;
304 sch = to_subchannel(cdev->dev.parent);
306 ret = cio_start (sch, ccw, lpm);
307 if ((ret == -EBUSY) || (ret == -EACCES)) {
308 /* Try again later. */
309 spin_unlock_irq(&sch->lock);
311 spin_lock_irq(&sch->lock);
315 /* Non-retryable error. */
317 /* Wait for end of request. */
318 cdev->private->intparm = magic;
319 spin_unlock_irq(&sch->lock);
320 wait_event(cdev->private->wait_q,
321 (cdev->private->intparm == -EIO) ||
322 (cdev->private->intparm == -EAGAIN) ||
323 (cdev->private->intparm == 0));
324 spin_lock_irq(&sch->lock);
325 /* Check at least for channel end / device end */
326 if (cdev->private->intparm == -EIO) {
327 /* Non-retryable error. */
331 if (cdev->private->intparm == 0)
334 /* Try again later. */
335 spin_unlock_irq(&sch->lock);
337 spin_lock_irq(&sch->lock);
344 * read_dev_chars() - read device characteristics
345 * @param cdev target ccw device
346 * @param buffer pointer to buffer for rdc data
347 * @param length size of rdc data
348 * @returns 0 for success, negative error value on failure
351 * called for online device, lock not held
354 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
356 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
357 struct subchannel *sch;
359 struct ccw1 *rdc_ccw;
363 if (!buffer || !length)
365 sch = to_subchannel(cdev->dev.parent);
367 CIO_TRACE_EVENT (4, "rddevch");
368 CIO_TRACE_EVENT (4, sch->dev.bus_id);
370 rdc_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
373 rdc_ccw->cmd_code = CCW_CMD_RDC;
374 rdc_ccw->count = length;
375 rdc_ccw->flags = CCW_FLAG_SLI;
376 ret = set_normalized_cda (rdc_ccw, (*buffer));
382 spin_lock_irq(&sch->lock);
383 /* Save interrupt handler. */
384 handler = cdev->handler;
385 /* Temporarily install own handler. */
386 cdev->handler = ccw_device_wake_up;
387 if (cdev->private->state != DEV_STATE_ONLINE)
389 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
390 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
391 cdev->private->flags.doverify)
394 /* 0x00D9C4C3 == ebcdic "RDC" */
395 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3, 0);
397 /* Restore interrupt handler. */
398 cdev->handler = handler;
399 spin_unlock_irq(&sch->lock);
401 clear_normalized_cda (rdc_ccw);
408 * Read Configuration data using path mask
411 read_conf_data_lpm (struct ccw_device *cdev, void **buffer, int *length, __u8 lpm)
413 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
414 struct subchannel *sch;
418 struct ccw1 *rcd_ccw;
422 if (!buffer || !length)
424 sch = to_subchannel(cdev->dev.parent);
426 CIO_TRACE_EVENT (4, "rdconf");
427 CIO_TRACE_EVENT (4, sch->dev.bus_id);
430 * scan for RCD command in extended SenseID data
432 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
433 if (!ciw || ciw->cmd == 0)
436 rcd_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
439 rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
444 rcd_ccw->cmd_code = ciw->cmd;
445 rcd_ccw->cda = (__u32) __pa (rcd_buf);
446 rcd_ccw->count = ciw->count;
447 rcd_ccw->flags = CCW_FLAG_SLI;
449 spin_lock_irq(&sch->lock);
450 /* Save interrupt handler. */
451 handler = cdev->handler;
452 /* Temporarily install own handler. */
453 cdev->handler = ccw_device_wake_up;
454 if (cdev->private->state != DEV_STATE_ONLINE)
456 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
457 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
458 cdev->private->flags.doverify)
461 /* 0x00D9C3C4 == ebcdic "RCD" */
462 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4, lpm);
464 /* Restore interrupt handler. */
465 cdev->handler = handler;
466 spin_unlock_irq(&sch->lock);
469 * on success we update the user input parms
476 *length = ciw->count;
485 * Read Configuration data
488 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
490 return read_conf_data_lpm (cdev, buffer, length, 0);
494 * Try to break the lock on a boxed device.
497 ccw_device_stlck(struct ccw_device *cdev)
501 struct subchannel *sch;
507 if (cdev->drv && !cdev->private->options.force)
510 sch = to_subchannel(cdev->dev.parent);
512 CIO_TRACE_EVENT(2, "stl lock");
513 CIO_TRACE_EVENT(2, cdev->dev.bus_id);
515 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
518 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
523 spin_lock_irqsave(&sch->lock, flags);
524 ret = cio_enable_subchannel(sch, 3);
528 * Setup ccw. We chain an unconditional reserve and a release so we
529 * only break the lock.
531 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
532 cdev->private->iccws[0].cda = (__u32) __pa(buf);
533 cdev->private->iccws[0].count = 32;
534 cdev->private->iccws[0].flags = CCW_FLAG_CC;
535 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
536 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
537 cdev->private->iccws[1].count = 32;
538 cdev->private->iccws[1].flags = 0;
539 ret = cio_start(sch, cdev->private->iccws, 0);
541 cio_disable_subchannel(sch); //FIXME: return code?
544 cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
545 spin_unlock_irqrestore(&sch->lock, flags);
546 wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
547 spin_lock_irqsave(&sch->lock, flags);
548 cio_disable_subchannel(sch); //FIXME: return code?
549 if ((cdev->private->irb.scsw.dstat !=
550 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
551 (cdev->private->irb.scsw.cstat != 0))
554 memset(&cdev->private->irb, 0, sizeof(struct irb));
558 spin_unlock_irqrestore(&sch->lock, flags);
563 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
565 struct subchannel *sch;
567 sch = to_subchannel(cdev->dev.parent);
568 return chsc_get_chp_desc(sch, chp_no);
571 // FIXME: these have to go:
574 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
576 return cdev->private->sch_no;
580 _ccw_device_get_device_number(struct ccw_device *cdev)
582 return cdev->private->devno;
586 MODULE_LICENSE("GPL");
587 EXPORT_SYMBOL(ccw_device_set_options);
588 EXPORT_SYMBOL(ccw_device_clear);
589 EXPORT_SYMBOL(ccw_device_halt);
590 EXPORT_SYMBOL(ccw_device_resume);
591 EXPORT_SYMBOL(ccw_device_start_timeout);
592 EXPORT_SYMBOL(ccw_device_start);
593 EXPORT_SYMBOL(ccw_device_start_timeout_key);
594 EXPORT_SYMBOL(ccw_device_start_key);
595 EXPORT_SYMBOL(ccw_device_get_ciw);
596 EXPORT_SYMBOL(ccw_device_get_path_mask);
597 EXPORT_SYMBOL(read_conf_data);
598 EXPORT_SYMBOL(read_dev_chars);
599 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
600 EXPORT_SYMBOL(_ccw_device_get_device_number);
601 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
602 EXPORT_SYMBOL_GPL(read_conf_data_lpm);