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"
31 ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
34 * The flag usage is mutal exclusive ...
36 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
37 (flags & CCWDEV_REPORT_ALL))
39 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
40 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
41 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
42 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
47 ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
49 struct subchannel *sch;
54 if (cdev->private->state == DEV_STATE_NOT_OPER)
56 if (cdev->private->state != DEV_STATE_ONLINE &&
57 cdev->private->state != DEV_STATE_W4SENSE)
59 sch = to_subchannel(cdev->dev.parent);
64 cdev->private->intparm = intparm;
69 ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
70 unsigned long intparm, __u8 lpm, unsigned long flags)
72 struct subchannel *sch;
77 sch = to_subchannel(cdev->dev.parent);
80 if (cdev->private->state == DEV_STATE_NOT_OPER)
82 if (cdev->private->state != DEV_STATE_ONLINE ||
83 ((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
84 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
85 cdev->private->flags.doverify)
87 ret = cio_set_options (sch, flags);
90 ret = cio_start (sch, cpa, lpm);
92 cdev->private->intparm = intparm;
97 ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
98 unsigned long intparm, __u8 lpm, unsigned long flags,
105 ccw_device_set_timeout(cdev, expires);
106 ret = ccw_device_start(cdev, cpa, intparm, lpm, flags);
108 ccw_device_set_timeout(cdev, 0);
113 ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
115 struct subchannel *sch;
120 if (cdev->private->state == DEV_STATE_NOT_OPER)
122 if (cdev->private->state != DEV_STATE_ONLINE &&
123 cdev->private->state != DEV_STATE_W4SENSE)
125 sch = to_subchannel(cdev->dev.parent);
130 cdev->private->intparm = intparm;
135 ccw_device_resume(struct ccw_device *cdev)
137 struct subchannel *sch;
141 sch = to_subchannel(cdev->dev.parent);
144 if (cdev->private->state == DEV_STATE_NOT_OPER)
146 if (cdev->private->state != DEV_STATE_ONLINE ||
147 !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
149 return cio_resume(sch);
153 * Pass interrupt to device driver.
156 ccw_device_call_handler(struct ccw_device *cdev)
158 struct subchannel *sch;
162 sch = to_subchannel(cdev->dev.parent);
165 * we allow for the device action handler if .
166 * - we received ending status
167 * - the action handler requested to see all interrupts
168 * - we received an intermediate status
169 * - fast notification was requested (primary status)
170 * - unsolicited interrupts
172 stctl = cdev->private->irb.scsw.stctl;
173 ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
174 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
175 (stctl == SCSW_STCTL_STATUS_PEND);
176 if (!ending_status &&
177 !cdev->private->options.repall &&
178 !(stctl & SCSW_STCTL_INTER_STATUS) &&
179 !(cdev->private->options.fast &&
180 (stctl & SCSW_STCTL_PRIM_STATUS)))
184 * Now we are ready to call the device driver interrupt handler.
187 cdev->handler(cdev, cdev->private->intparm,
188 &cdev->private->irb);
191 * Clear the old and now useless interrupt response block.
193 memset(&cdev->private->irb, 0, sizeof(struct irb));
199 * Search for CIW command in extended sense data.
202 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
206 if (cdev->private->flags.esid == 0)
208 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
209 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
210 return cdev->private->senseid.ciw + ciw_cnt;
215 ccw_device_get_path_mask(struct ccw_device *cdev)
217 struct subchannel *sch;
219 sch = to_subchannel(cdev->dev.parent);
227 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
230 /* unsolicited interrupt */
233 /* Abuse intparm for error reporting. */
235 cdev->private->intparm = -EIO;
236 else if ((irb->scsw.dstat !=
237 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
238 (irb->scsw.cstat != 0)) {
240 * We didn't get channel end / device end. Check if path
241 * verification has been started; we can retry after it has
242 * finished. We also retry unit checks except for command reject
243 * or intervention required.
245 if (cdev->private->flags.doverify ||
246 cdev->private->state == DEV_STATE_VERIFY)
247 cdev->private->intparm = -EAGAIN;
248 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
250 (SNS0_CMD_REJECT | SNS0_INTERVENTION_REQ)))
251 cdev->private->intparm = -EAGAIN;
253 cdev->private->intparm = -EIO;
256 cdev->private->intparm = 0;
257 wake_up(&cdev->private->wait_q);
261 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic)
264 struct subchannel *sch;
266 sch = to_subchannel(cdev->dev.parent);
268 ret = cio_start (sch, ccw, 0);
269 if ((ret == -EBUSY) || (ret == -EACCES)) {
270 /* Try again later. */
271 spin_unlock_irq(&sch->lock);
273 spin_lock_irq(&sch->lock);
277 /* Non-retryable error. */
279 /* Wait for end of request. */
280 cdev->private->intparm = magic;
281 spin_unlock_irq(&sch->lock);
282 wait_event(cdev->private->wait_q,
283 (cdev->private->intparm == -EIO) ||
284 (cdev->private->intparm == -EAGAIN) ||
285 (cdev->private->intparm == 0));
286 spin_lock_irq(&sch->lock);
287 /* Check at least for channel end / device end */
288 if (cdev->private->intparm == -EIO) {
289 /* Non-retryable error. */
293 if (cdev->private->intparm == 0)
296 /* Try again later. */
297 spin_unlock_irq(&sch->lock);
299 spin_lock_irq(&sch->lock);
306 * read_dev_chars() - read device characteristics
307 * @param cdev target ccw device
308 * @param buffer pointer to buffer for rdc data
309 * @param length size of rdc data
310 * @returns 0 for success, negative error value on failure
313 * called for online device, lock not held
316 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
318 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
319 struct subchannel *sch;
321 struct ccw1 *rdc_ccw;
325 if (!buffer || !length)
327 sch = to_subchannel(cdev->dev.parent);
329 CIO_TRACE_EVENT (4, "rddevch");
330 CIO_TRACE_EVENT (4, sch->dev.bus_id);
332 rdc_ccw = kmalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
335 memset(rdc_ccw, 0, sizeof(struct ccw1));
336 rdc_ccw->cmd_code = CCW_CMD_RDC;
337 rdc_ccw->count = length;
338 rdc_ccw->flags = CCW_FLAG_SLI;
339 ret = set_normalized_cda (rdc_ccw, (*buffer));
345 spin_lock_irq(&sch->lock);
346 /* Save interrupt handler. */
347 handler = cdev->handler;
348 /* Temporarily install own handler. */
349 cdev->handler = ccw_device_wake_up;
350 if (cdev->private->state != DEV_STATE_ONLINE)
352 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
353 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
354 cdev->private->flags.doverify)
357 /* 0x00D9C4C3 == ebcdic "RDC" */
358 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3);
360 /* Restore interrupt handler. */
361 cdev->handler = handler;
362 spin_unlock_irq(&sch->lock);
364 clear_normalized_cda (rdc_ccw);
371 * Read Configuration data
374 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
376 void (*handler)(struct ccw_device *, unsigned long, struct irb *);
377 struct subchannel *sch;
381 struct ccw1 *rcd_ccw;
385 if (!buffer || !length)
387 sch = to_subchannel(cdev->dev.parent);
389 CIO_TRACE_EVENT (4, "rdconf");
390 CIO_TRACE_EVENT (4, sch->dev.bus_id);
393 * scan for RCD command in extended SenseID data
395 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
396 if (!ciw || ciw->cmd == 0)
399 rcd_ccw = kmalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
402 memset(rcd_ccw, 0, sizeof(struct ccw1));
403 rcd_buf = kmalloc(ciw->count, GFP_KERNEL | GFP_DMA);
408 memset (rcd_buf, 0, ciw->count);
409 rcd_ccw->cmd_code = ciw->cmd;
410 rcd_ccw->cda = (__u32) __pa (rcd_buf);
411 rcd_ccw->count = ciw->count;
412 rcd_ccw->flags = CCW_FLAG_SLI;
414 spin_lock_irq(&sch->lock);
415 /* Save interrupt handler. */
416 handler = cdev->handler;
417 /* Temporarily install own handler. */
418 cdev->handler = ccw_device_wake_up;
419 if (cdev->private->state != DEV_STATE_ONLINE)
421 else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
422 !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
423 cdev->private->flags.doverify)
426 /* 0x00D9C3C4 == ebcdic "RCD" */
427 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4);
429 /* Restore interrupt handler. */
430 cdev->handler = handler;
431 spin_unlock_irq(&sch->lock);
434 * on success we update the user input parms
441 *length = ciw->count;
450 * Try to break the lock on a boxed device.
453 ccw_device_stlck(struct ccw_device *cdev)
457 struct subchannel *sch;
463 if (cdev->drv && !cdev->private->options.force)
466 sch = to_subchannel(cdev->dev.parent);
468 CIO_TRACE_EVENT(2, "stl lock");
469 CIO_TRACE_EVENT(2, cdev->dev.bus_id);
471 buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
474 buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
479 spin_lock_irqsave(&sch->lock, flags);
480 ret = cio_enable_subchannel(sch, 3);
484 * Setup ccw. We chain an unconditional reserve and a release so we
485 * only break the lock.
487 cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
488 cdev->private->iccws[0].cda = (__u32) __pa(buf);
489 cdev->private->iccws[0].count = 32;
490 cdev->private->iccws[0].flags = CCW_FLAG_CC;
491 cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
492 cdev->private->iccws[1].cda = (__u32) __pa(buf2);
493 cdev->private->iccws[1].count = 32;
494 cdev->private->iccws[1].flags = 0;
495 ret = cio_start(sch, cdev->private->iccws, 0);
497 cio_disable_subchannel(sch); //FIXME: return code?
500 cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
501 spin_unlock_irqrestore(&sch->lock, flags);
502 wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
503 spin_lock_irqsave(&sch->lock, flags);
504 cio_disable_subchannel(sch); //FIXME: return code?
505 if ((cdev->private->irb.scsw.dstat !=
506 (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
507 (cdev->private->irb.scsw.cstat != 0))
510 memset(&cdev->private->irb, 0, sizeof(struct irb));
516 spin_unlock_irqrestore(&sch->lock, flags);
520 // FIXME: these have to go:
523 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
525 return cdev->private->irq;
529 _ccw_device_get_device_number(struct ccw_device *cdev)
531 return cdev->private->devno;
535 MODULE_LICENSE("GPL");
536 EXPORT_SYMBOL(ccw_device_set_options);
537 EXPORT_SYMBOL(ccw_device_clear);
538 EXPORT_SYMBOL(ccw_device_halt);
539 EXPORT_SYMBOL(ccw_device_resume);
540 EXPORT_SYMBOL(ccw_device_start_timeout);
541 EXPORT_SYMBOL(ccw_device_start);
542 EXPORT_SYMBOL(ccw_device_get_ciw);
543 EXPORT_SYMBOL(ccw_device_get_path_mask);
544 EXPORT_SYMBOL(read_conf_data);
545 EXPORT_SYMBOL(read_dev_chars);
546 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
547 EXPORT_SYMBOL(_ccw_device_get_device_number);