Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6
[linux-2.6] / drivers / s390 / cio / device_ops.c
1 /*
2  *  drivers/s390/cio/device_ops.c
3  *
4  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
5  *                       IBM Corporation
6  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7  *               Cornelia Huck (cornelia.huck@de.ibm.com)
8  */
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>
17
18 #include <asm/ccwdev.h>
19 #include <asm/idals.h>
20
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "css.h"
24 #include "chsc.h"
25 #include "device.h"
26
27 int
28 ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
29 {
30        /*
31         * The flag usage is mutal exclusive ...
32         */
33         if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
34             (flags & CCWDEV_REPORT_ALL))
35                 return -EINVAL;
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;
40         return 0;
41 }
42
43 int
44 ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
45 {
46         struct subchannel *sch;
47         int ret;
48
49         if (!cdev)
50                 return -ENODEV;
51         if (cdev->private->state == DEV_STATE_NOT_OPER)
52                 return -ENODEV;
53         if (cdev->private->state != DEV_STATE_ONLINE &&
54             cdev->private->state != DEV_STATE_WAIT4IO &&
55             cdev->private->state != DEV_STATE_W4SENSE)
56                 return -EINVAL;
57         sch = to_subchannel(cdev->dev.parent);
58         if (!sch)
59                 return -ENODEV;
60         ret = cio_clear(sch);
61         if (ret == 0)
62                 cdev->private->intparm = intparm;
63         return ret;
64 }
65
66 int
67 ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
68                      unsigned long intparm, __u8 lpm, __u8 key,
69                      unsigned long flags)
70 {
71         struct subchannel *sch;
72         int ret;
73
74         if (!cdev)
75                 return -ENODEV;
76         sch = to_subchannel(cdev->dev.parent);
77         if (!sch)
78                 return -ENODEV;
79         if (cdev->private->state == DEV_STATE_NOT_OPER)
80                 return -ENODEV;
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;
87                         return 0;
88                 } else
89                         /* There's already a fake I/O around. */
90                         return -EBUSY;
91         }
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)
96                 return -EBUSY;
97         ret = cio_set_options (sch, flags);
98         if (ret)
99                 return ret;
100         ret = cio_start_key (sch, cpa, lpm, key);
101         if (ret == 0)
102                 cdev->private->intparm = intparm;
103         return ret;
104 }
105
106
107 int
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)
111 {
112         int ret;
113
114         if (!cdev)
115                 return -ENODEV;
116         ccw_device_set_timeout(cdev, expires);
117         ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
118         if (ret != 0)
119                 ccw_device_set_timeout(cdev, 0);
120         return ret;
121 }
122
123 int
124 ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
125                  unsigned long intparm, __u8 lpm, unsigned long flags)
126 {
127         return ccw_device_start_key(cdev, cpa, intparm, lpm,
128                                     PAGE_DEFAULT_KEY, flags);
129 }
130
131 int
132 ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
133                          unsigned long intparm, __u8 lpm, unsigned long flags,
134                          int expires)
135 {
136         return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
137                                             PAGE_DEFAULT_KEY, flags,
138                                             expires);
139 }
140
141
142 int
143 ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
144 {
145         struct subchannel *sch;
146         int ret;
147
148         if (!cdev)
149                 return -ENODEV;
150         if (cdev->private->state == DEV_STATE_NOT_OPER)
151                 return -ENODEV;
152         if (cdev->private->state != DEV_STATE_ONLINE &&
153             cdev->private->state != DEV_STATE_WAIT4IO &&
154             cdev->private->state != DEV_STATE_W4SENSE)
155                 return -EINVAL;
156         sch = to_subchannel(cdev->dev.parent);
157         if (!sch)
158                 return -ENODEV;
159         ret = cio_halt(sch);
160         if (ret == 0)
161                 cdev->private->intparm = intparm;
162         return ret;
163 }
164
165 int
166 ccw_device_resume(struct ccw_device *cdev)
167 {
168         struct subchannel *sch;
169
170         if (!cdev)
171                 return -ENODEV;
172         sch = to_subchannel(cdev->dev.parent);
173         if (!sch)
174                 return -ENODEV;
175         if (cdev->private->state == DEV_STATE_NOT_OPER)
176                 return -ENODEV;
177         if (cdev->private->state != DEV_STATE_ONLINE ||
178             !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
179                 return -EINVAL;
180         return cio_resume(sch);
181 }
182
183 /*
184  * Pass interrupt to device driver.
185  */
186 int
187 ccw_device_call_handler(struct ccw_device *cdev)
188 {
189         struct subchannel *sch;
190         unsigned int stctl;
191         int ending_status;
192
193         sch = to_subchannel(cdev->dev.parent);
194
195         /*
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
202          */
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)))
212                 return 0;
213
214         /*
215          * Now we are ready to call the device driver interrupt handler.
216          */
217         if (cdev->handler)
218                 cdev->handler(cdev, cdev->private->intparm,
219                               &cdev->private->irb);
220
221         /*
222          * Clear the old and now useless interrupt response block.
223          */
224         memset(&cdev->private->irb, 0, sizeof(struct irb));
225
226         return 1;
227 }
228
229 /*
230  * Search for CIW command in extended sense data.
231  */
232 struct ciw *
233 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
234 {
235         int ciw_cnt;
236
237         if (cdev->private->flags.esid == 0)
238                 return NULL;
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;
242         return NULL;
243 }
244
245 __u8
246 ccw_device_get_path_mask(struct ccw_device *cdev)
247 {
248         struct subchannel *sch;
249
250         sch = to_subchannel(cdev->dev.parent);
251         if (!sch)
252                 return 0;
253         else
254                 return sch->vpm;
255 }
256
257 static void
258 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
259 {
260         if (!ip)
261                 /* unsolicited interrupt */
262                 return;
263
264         /* Abuse intparm for error reporting. */
265         if (IS_ERR(irb))
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)) {
270                 /*
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
275                  * conditions.
276                  */
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) &&
281                      !(irb->ecw[0] &
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;
288                  else
289                          cdev->private->intparm = -EIO;
290                          
291         } else
292                 cdev->private->intparm = 0;
293         wake_up(&cdev->private->wait_q);
294 }
295
296 static inline int
297 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic, __u8 lpm)
298 {
299         int ret;
300         struct subchannel *sch;
301
302         sch = to_subchannel(cdev->dev.parent);
303         do {
304                 ret = cio_start (sch, ccw, lpm);
305                 if ((ret == -EBUSY) || (ret == -EACCES)) {
306                         /* Try again later. */
307                         spin_unlock_irq(&sch->lock);
308                         msleep(10);
309                         spin_lock_irq(&sch->lock);
310                         continue;
311                 }
312                 if (ret != 0)
313                         /* Non-retryable error. */
314                         break;
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. */
326                         ret = -EIO;
327                         break;
328                 }
329                 if (cdev->private->intparm == 0)
330                         /* Success. */
331                         break;
332                 /* Try again later. */
333                 spin_unlock_irq(&sch->lock);
334                 msleep(10);
335                 spin_lock_irq(&sch->lock);
336         } while (1);
337
338         return ret;
339 }
340
341 /**
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
347  *
348  * Context:
349  *   called for online device, lock not held
350  **/
351 int
352 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
353 {
354         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
355         struct subchannel *sch;
356         int ret;
357         struct ccw1 *rdc_ccw;
358
359         if (!cdev)
360                 return -ENODEV;
361         if (!buffer || !length)
362                 return -EINVAL;
363         sch = to_subchannel(cdev->dev.parent);
364
365         CIO_TRACE_EVENT (4, "rddevch");
366         CIO_TRACE_EVENT (4, sch->dev.bus_id);
367
368         rdc_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
369         if (!rdc_ccw)
370                 return -ENOMEM;
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));
375         if (ret != 0) {
376                 kfree(rdc_ccw);
377                 return ret;
378         }
379
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)
386                 ret = -ENODEV;
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)
390                 ret = -EBUSY;
391         else
392                 /* 0x00D9C4C3 == ebcdic "RDC" */
393                 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3, 0);
394
395         /* Restore interrupt handler. */
396         cdev->handler = handler;
397         spin_unlock_irq(&sch->lock);
398
399         clear_normalized_cda (rdc_ccw);
400         kfree(rdc_ccw);
401
402         return ret;
403 }
404
405 /*
406  *  Read Configuration data using path mask
407  */
408 int
409 read_conf_data_lpm (struct ccw_device *cdev, void **buffer, int *length, __u8 lpm)
410 {
411         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
412         struct subchannel *sch;
413         struct ciw *ciw;
414         char *rcd_buf;
415         int ret;
416         struct ccw1 *rcd_ccw;
417
418         if (!cdev)
419                 return -ENODEV;
420         if (!buffer || !length)
421                 return -EINVAL;
422         sch = to_subchannel(cdev->dev.parent);
423
424         CIO_TRACE_EVENT (4, "rdconf");
425         CIO_TRACE_EVENT (4, sch->dev.bus_id);
426
427         /*
428          * scan for RCD command in extended SenseID data
429          */
430         ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
431         if (!ciw || ciw->cmd == 0)
432                 return -EOPNOTSUPP;
433
434         rcd_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
435         if (!rcd_ccw)
436                 return -ENOMEM;
437         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
438         if (!rcd_buf) {
439                 kfree(rcd_ccw);
440                 return -ENOMEM;
441         }
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;
446
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)
453                 ret = -ENODEV;
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)
457                 ret = -EBUSY;
458         else
459                 /* 0x00D9C3C4 == ebcdic "RCD" */
460                 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4, lpm);
461
462         /* Restore interrupt handler. */
463         cdev->handler = handler;
464         spin_unlock_irq(&sch->lock);
465
466         /*
467          * on success we update the user input parms
468          */
469         if (ret) {
470                 kfree (rcd_buf);
471                 *buffer = NULL;
472                 *length = 0;
473         } else {
474                 *length = ciw->count;
475                 *buffer = rcd_buf;
476         }
477         kfree(rcd_ccw);
478
479         return ret;
480 }
481
482 /*
483  *  Read Configuration data
484  */
485 int
486 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
487 {
488         return read_conf_data_lpm (cdev, buffer, length, 0);
489 }
490
491 /*
492  * Try to break the lock on a boxed device.
493  */
494 int
495 ccw_device_stlck(struct ccw_device *cdev)
496 {
497         void *buf, *buf2;
498         unsigned long flags;
499         struct subchannel *sch;
500         int ret;
501
502         if (!cdev)
503                 return -ENODEV;
504
505         if (cdev->drv && !cdev->private->options.force)
506                 return -EINVAL;
507
508         sch = to_subchannel(cdev->dev.parent);
509         
510         CIO_TRACE_EVENT(2, "stl lock");
511         CIO_TRACE_EVENT(2, cdev->dev.bus_id);
512
513         buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
514         if (!buf)
515                 return -ENOMEM;
516         buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
517         if (!buf2) {
518                 kfree(buf);
519                 return -ENOMEM;
520         }
521         spin_lock_irqsave(&sch->lock, flags);
522         ret = cio_enable_subchannel(sch, 3);
523         if (ret)
524                 goto out_unlock;
525         /*
526          * Setup ccw. We chain an unconditional reserve and a release so we
527          * only break the lock.
528          */
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);
538         if (ret) {
539                 cio_disable_subchannel(sch); //FIXME: return code?
540                 goto out_unlock;
541         }
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))
550                 ret = -EIO;
551         /* Clear irb. */
552         memset(&cdev->private->irb, 0, sizeof(struct irb));
553 out_unlock:
554         kfree(buf);
555         kfree(buf2);
556         spin_unlock_irqrestore(&sch->lock, flags);
557         return ret;
558 }
559
560 void *
561 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
562 {
563         struct subchannel *sch;
564
565         sch = to_subchannel(cdev->dev.parent);
566         return chsc_get_chp_desc(sch, chp_no);
567 }
568
569 // FIXME: these have to go:
570
571 int
572 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
573 {
574         return cdev->private->sch_no;
575 }
576
577 int
578 _ccw_device_get_device_number(struct ccw_device *cdev)
579 {
580         return cdev->private->devno;
581 }
582
583
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);