Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-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/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>
16
17 #include <asm/ccwdev.h>
18 #include <asm/idals.h>
19 #include <asm/chpid.h>
20
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "css.h"
24 #include "chsc.h"
25 #include "device.h"
26 #include "chp.h"
27
28 int ccw_device_set_options_mask(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 ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
44 {
45        /*
46         * The flag usage is mutal exclusive ...
47         */
48         if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
49             (flags & CCWDEV_REPORT_ALL)) ||
50             ((flags & CCWDEV_EARLY_NOTIFICATION) &&
51              cdev->private->options.repall) ||
52             ((flags & CCWDEV_REPORT_ALL) &&
53              cdev->private->options.fast))
54                 return -EINVAL;
55         cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
56         cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
57         cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
58         cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
59         return 0;
60 }
61
62 void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
63 {
64         cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
65         cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
66         cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
67         cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
68 }
69
70 int
71 ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
72 {
73         struct subchannel *sch;
74         int ret;
75
76         if (!cdev)
77                 return -ENODEV;
78         if (cdev->private->state == DEV_STATE_NOT_OPER)
79                 return -ENODEV;
80         if (cdev->private->state != DEV_STATE_ONLINE &&
81             cdev->private->state != DEV_STATE_W4SENSE)
82                 return -EINVAL;
83         sch = to_subchannel(cdev->dev.parent);
84         if (!sch)
85                 return -ENODEV;
86         ret = cio_clear(sch);
87         if (ret == 0)
88                 cdev->private->intparm = intparm;
89         return ret;
90 }
91
92 int
93 ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
94                      unsigned long intparm, __u8 lpm, __u8 key,
95                      unsigned long flags)
96 {
97         struct subchannel *sch;
98         int ret;
99
100         if (!cdev)
101                 return -ENODEV;
102         sch = to_subchannel(cdev->dev.parent);
103         if (!sch)
104                 return -ENODEV;
105         if (cdev->private->state == DEV_STATE_NOT_OPER)
106                 return -ENODEV;
107         if (cdev->private->state == DEV_STATE_VERIFY ||
108             cdev->private->state == DEV_STATE_CLEAR_VERIFY) {
109                 /* Remember to fake irb when finished. */
110                 if (!cdev->private->flags.fake_irb) {
111                         cdev->private->flags.fake_irb = 1;
112                         cdev->private->intparm = intparm;
113                         return 0;
114                 } else
115                         /* There's already a fake I/O around. */
116                         return -EBUSY;
117         }
118         if (cdev->private->state != DEV_STATE_ONLINE ||
119             ((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
120              !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
121             cdev->private->flags.doverify)
122                 return -EBUSY;
123         ret = cio_set_options (sch, flags);
124         if (ret)
125                 return ret;
126         /* Adjust requested path mask to excluded varied off paths. */
127         if (lpm) {
128                 lpm &= sch->opm;
129                 if (lpm == 0)
130                         return -EACCES;
131         }
132         ret = cio_start_key (sch, cpa, lpm, key);
133         if (ret == 0)
134                 cdev->private->intparm = intparm;
135         return ret;
136 }
137
138
139 int
140 ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
141                              unsigned long intparm, __u8 lpm, __u8 key,
142                              unsigned long flags, int expires)
143 {
144         int ret;
145
146         if (!cdev)
147                 return -ENODEV;
148         ccw_device_set_timeout(cdev, expires);
149         ret = ccw_device_start_key(cdev, cpa, intparm, lpm, key, flags);
150         if (ret != 0)
151                 ccw_device_set_timeout(cdev, 0);
152         return ret;
153 }
154
155 int
156 ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
157                  unsigned long intparm, __u8 lpm, unsigned long flags)
158 {
159         return ccw_device_start_key(cdev, cpa, intparm, lpm,
160                                     PAGE_DEFAULT_KEY, flags);
161 }
162
163 int
164 ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
165                          unsigned long intparm, __u8 lpm, unsigned long flags,
166                          int expires)
167 {
168         return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
169                                             PAGE_DEFAULT_KEY, flags,
170                                             expires);
171 }
172
173
174 int
175 ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
176 {
177         struct subchannel *sch;
178         int ret;
179
180         if (!cdev)
181                 return -ENODEV;
182         if (cdev->private->state == DEV_STATE_NOT_OPER)
183                 return -ENODEV;
184         if (cdev->private->state != DEV_STATE_ONLINE &&
185             cdev->private->state != DEV_STATE_W4SENSE)
186                 return -EINVAL;
187         sch = to_subchannel(cdev->dev.parent);
188         if (!sch)
189                 return -ENODEV;
190         ret = cio_halt(sch);
191         if (ret == 0)
192                 cdev->private->intparm = intparm;
193         return ret;
194 }
195
196 int
197 ccw_device_resume(struct ccw_device *cdev)
198 {
199         struct subchannel *sch;
200
201         if (!cdev)
202                 return -ENODEV;
203         sch = to_subchannel(cdev->dev.parent);
204         if (!sch)
205                 return -ENODEV;
206         if (cdev->private->state == DEV_STATE_NOT_OPER)
207                 return -ENODEV;
208         if (cdev->private->state != DEV_STATE_ONLINE ||
209             !(sch->schib.scsw.actl & SCSW_ACTL_SUSPENDED))
210                 return -EINVAL;
211         return cio_resume(sch);
212 }
213
214 /*
215  * Pass interrupt to device driver.
216  */
217 int
218 ccw_device_call_handler(struct ccw_device *cdev)
219 {
220         struct subchannel *sch;
221         unsigned int stctl;
222         int ending_status;
223
224         sch = to_subchannel(cdev->dev.parent);
225
226         /*
227          * we allow for the device action handler if .
228          *  - we received ending status
229          *  - the action handler requested to see all interrupts
230          *  - we received an intermediate status
231          *  - fast notification was requested (primary status)
232          *  - unsolicited interrupts
233          */
234         stctl = cdev->private->irb.scsw.stctl;
235         ending_status = (stctl & SCSW_STCTL_SEC_STATUS) ||
236                 (stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)) ||
237                 (stctl == SCSW_STCTL_STATUS_PEND);
238         if (!ending_status &&
239             !cdev->private->options.repall &&
240             !(stctl & SCSW_STCTL_INTER_STATUS) &&
241             !(cdev->private->options.fast &&
242               (stctl & SCSW_STCTL_PRIM_STATUS)))
243                 return 0;
244
245         /* Clear pending timers for device driver initiated I/O. */
246         if (ending_status)
247                 ccw_device_set_timeout(cdev, 0);
248         /*
249          * Now we are ready to call the device driver interrupt handler.
250          */
251         if (cdev->handler)
252                 cdev->handler(cdev, cdev->private->intparm,
253                               &cdev->private->irb);
254
255         /*
256          * Clear the old and now useless interrupt response block.
257          */
258         memset(&cdev->private->irb, 0, sizeof(struct irb));
259
260         return 1;
261 }
262
263 /*
264  * Search for CIW command in extended sense data.
265  */
266 struct ciw *
267 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
268 {
269         int ciw_cnt;
270
271         if (cdev->private->flags.esid == 0)
272                 return NULL;
273         for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
274                 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
275                         return cdev->private->senseid.ciw + ciw_cnt;
276         return NULL;
277 }
278
279 __u8
280 ccw_device_get_path_mask(struct ccw_device *cdev)
281 {
282         struct subchannel *sch;
283
284         sch = to_subchannel(cdev->dev.parent);
285         if (!sch)
286                 return 0;
287         else
288                 return sch->lpm;
289 }
290
291 static void
292 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
293 {
294         if (!ip)
295                 /* unsolicited interrupt */
296                 return;
297
298         /* Abuse intparm for error reporting. */
299         if (IS_ERR(irb))
300                 cdev->private->intparm = -EIO;
301         else if (irb->scsw.cc == 1)
302                 /* Retry for deferred condition code. */
303                 cdev->private->intparm = -EAGAIN;
304         else if ((irb->scsw.dstat !=
305                   (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
306                  (irb->scsw.cstat != 0)) {
307                 /*
308                  * We didn't get channel end / device end. Check if path
309                  * verification has been started; we can retry after it has
310                  * finished. We also retry unit checks except for command reject
311                  * or intervention required. Also check for long busy
312                  * conditions.
313                  */
314                  if (cdev->private->flags.doverify ||
315                          cdev->private->state == DEV_STATE_VERIFY)
316                          cdev->private->intparm = -EAGAIN;
317                 else if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
318                          !(irb->ecw[0] &
319                            (SNS0_CMD_REJECT | SNS0_INTERVENTION_REQ)))
320                         cdev->private->intparm = -EAGAIN;
321                 else if ((irb->scsw.dstat & DEV_STAT_ATTENTION) &&
322                          (irb->scsw.dstat & DEV_STAT_DEV_END) &&
323                          (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP))
324                         cdev->private->intparm = -EAGAIN;
325                  else
326                          cdev->private->intparm = -EIO;
327                          
328         } else
329                 cdev->private->intparm = 0;
330         wake_up(&cdev->private->wait_q);
331 }
332
333 static int
334 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic, __u8 lpm)
335 {
336         int ret;
337         struct subchannel *sch;
338
339         sch = to_subchannel(cdev->dev.parent);
340         do {
341                 ccw_device_set_timeout(cdev, 60 * HZ);
342                 ret = cio_start (sch, ccw, lpm);
343                 if (ret != 0)
344                         ccw_device_set_timeout(cdev, 0);
345                 if (ret == -EBUSY) {
346                         /* Try again later. */
347                         spin_unlock_irq(sch->lock);
348                         msleep(10);
349                         spin_lock_irq(sch->lock);
350                         continue;
351                 }
352                 if (ret != 0)
353                         /* Non-retryable error. */
354                         break;
355                 /* Wait for end of request. */
356                 cdev->private->intparm = magic;
357                 spin_unlock_irq(sch->lock);
358                 wait_event(cdev->private->wait_q,
359                            (cdev->private->intparm == -EIO) ||
360                            (cdev->private->intparm == -EAGAIN) ||
361                            (cdev->private->intparm == 0));
362                 spin_lock_irq(sch->lock);
363                 /* Check at least for channel end / device end */
364                 if (cdev->private->intparm == -EIO) {
365                         /* Non-retryable error. */
366                         ret = -EIO;
367                         break;
368                 }
369                 if (cdev->private->intparm == 0)
370                         /* Success. */
371                         break;
372                 /* Try again later. */
373                 spin_unlock_irq(sch->lock);
374                 msleep(10);
375                 spin_lock_irq(sch->lock);
376         } while (1);
377
378         return ret;
379 }
380
381 /**
382  * read_dev_chars() - read device characteristics
383  * @param cdev   target ccw device
384  * @param buffer pointer to buffer for rdc data
385  * @param length size of rdc data
386  * @returns 0 for success, negative error value on failure
387  *
388  * Context:
389  *   called for online device, lock not held
390  **/
391 int
392 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
393 {
394         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
395         struct subchannel *sch;
396         int ret;
397         struct ccw1 *rdc_ccw;
398
399         if (!cdev)
400                 return -ENODEV;
401         if (!buffer || !length)
402                 return -EINVAL;
403         sch = to_subchannel(cdev->dev.parent);
404
405         CIO_TRACE_EVENT (4, "rddevch");
406         CIO_TRACE_EVENT (4, sch->dev.bus_id);
407
408         rdc_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
409         if (!rdc_ccw)
410                 return -ENOMEM;
411         rdc_ccw->cmd_code = CCW_CMD_RDC;
412         rdc_ccw->count = length;
413         rdc_ccw->flags = CCW_FLAG_SLI;
414         ret = set_normalized_cda (rdc_ccw, (*buffer));
415         if (ret != 0) {
416                 kfree(rdc_ccw);
417                 return ret;
418         }
419
420         spin_lock_irq(sch->lock);
421         /* Save interrupt handler. */
422         handler = cdev->handler;
423         /* Temporarily install own handler. */
424         cdev->handler = ccw_device_wake_up;
425         if (cdev->private->state != DEV_STATE_ONLINE)
426                 ret = -ENODEV;
427         else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
428                   !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
429                  cdev->private->flags.doverify)
430                 ret = -EBUSY;
431         else
432                 /* 0x00D9C4C3 == ebcdic "RDC" */
433                 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3, 0);
434
435         /* Restore interrupt handler. */
436         cdev->handler = handler;
437         spin_unlock_irq(sch->lock);
438
439         clear_normalized_cda (rdc_ccw);
440         kfree(rdc_ccw);
441
442         return ret;
443 }
444
445 /*
446  *  Read Configuration data using path mask
447  */
448 int
449 read_conf_data_lpm (struct ccw_device *cdev, void **buffer, int *length, __u8 lpm)
450 {
451         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
452         struct subchannel *sch;
453         struct ciw *ciw;
454         char *rcd_buf;
455         int ret;
456         struct ccw1 *rcd_ccw;
457
458         if (!cdev)
459                 return -ENODEV;
460         if (!buffer || !length)
461                 return -EINVAL;
462         sch = to_subchannel(cdev->dev.parent);
463
464         CIO_TRACE_EVENT (4, "rdconf");
465         CIO_TRACE_EVENT (4, sch->dev.bus_id);
466
467         /*
468          * scan for RCD command in extended SenseID data
469          */
470         ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
471         if (!ciw || ciw->cmd == 0)
472                 return -EOPNOTSUPP;
473
474         /* Adjust requested path mask to excluded varied off paths. */
475         if (lpm) {
476                 lpm &= sch->opm;
477                 if (lpm == 0)
478                         return -EACCES;
479         }
480
481         rcd_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
482         if (!rcd_ccw)
483                 return -ENOMEM;
484         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
485         if (!rcd_buf) {
486                 kfree(rcd_ccw);
487                 return -ENOMEM;
488         }
489         rcd_ccw->cmd_code = ciw->cmd;
490         rcd_ccw->cda = (__u32) __pa (rcd_buf);
491         rcd_ccw->count = ciw->count;
492         rcd_ccw->flags = CCW_FLAG_SLI;
493
494         spin_lock_irq(sch->lock);
495         /* Save interrupt handler. */
496         handler = cdev->handler;
497         /* Temporarily install own handler. */
498         cdev->handler = ccw_device_wake_up;
499         if (cdev->private->state != DEV_STATE_ONLINE)
500                 ret = -ENODEV;
501         else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
502                   !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
503                  cdev->private->flags.doverify)
504                 ret = -EBUSY;
505         else
506                 /* 0x00D9C3C4 == ebcdic "RCD" */
507                 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4, lpm);
508
509         /* Restore interrupt handler. */
510         cdev->handler = handler;
511         spin_unlock_irq(sch->lock);
512
513         /*
514          * on success we update the user input parms
515          */
516         if (ret) {
517                 kfree (rcd_buf);
518                 *buffer = NULL;
519                 *length = 0;
520         } else {
521                 *length = ciw->count;
522                 *buffer = rcd_buf;
523         }
524         kfree(rcd_ccw);
525
526         return ret;
527 }
528
529 /*
530  *  Read Configuration data
531  */
532 int
533 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
534 {
535         return read_conf_data_lpm (cdev, buffer, length, 0);
536 }
537
538 /*
539  * Try to break the lock on a boxed device.
540  */
541 int
542 ccw_device_stlck(struct ccw_device *cdev)
543 {
544         void *buf, *buf2;
545         unsigned long flags;
546         struct subchannel *sch;
547         int ret;
548
549         if (!cdev)
550                 return -ENODEV;
551
552         if (cdev->drv && !cdev->private->options.force)
553                 return -EINVAL;
554
555         sch = to_subchannel(cdev->dev.parent);
556         
557         CIO_TRACE_EVENT(2, "stl lock");
558         CIO_TRACE_EVENT(2, cdev->dev.bus_id);
559
560         buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
561         if (!buf)
562                 return -ENOMEM;
563         buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
564         if (!buf2) {
565                 kfree(buf);
566                 return -ENOMEM;
567         }
568         spin_lock_irqsave(sch->lock, flags);
569         ret = cio_enable_subchannel(sch, 3);
570         if (ret)
571                 goto out_unlock;
572         /*
573          * Setup ccw. We chain an unconditional reserve and a release so we
574          * only break the lock.
575          */
576         cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
577         cdev->private->iccws[0].cda = (__u32) __pa(buf);
578         cdev->private->iccws[0].count = 32;
579         cdev->private->iccws[0].flags = CCW_FLAG_CC;
580         cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
581         cdev->private->iccws[1].cda = (__u32) __pa(buf2);
582         cdev->private->iccws[1].count = 32;
583         cdev->private->iccws[1].flags = 0;
584         ret = cio_start(sch, cdev->private->iccws, 0);
585         if (ret) {
586                 cio_disable_subchannel(sch); //FIXME: return code?
587                 goto out_unlock;
588         }
589         cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
590         spin_unlock_irqrestore(sch->lock, flags);
591         wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
592         spin_lock_irqsave(sch->lock, flags);
593         cio_disable_subchannel(sch); //FIXME: return code?
594         if ((cdev->private->irb.scsw.dstat !=
595              (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
596             (cdev->private->irb.scsw.cstat != 0))
597                 ret = -EIO;
598         /* Clear irb. */
599         memset(&cdev->private->irb, 0, sizeof(struct irb));
600 out_unlock:
601         kfree(buf);
602         kfree(buf2);
603         spin_unlock_irqrestore(sch->lock, flags);
604         return ret;
605 }
606
607 void *
608 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
609 {
610         struct subchannel *sch;
611         struct chp_id chpid;
612
613         sch = to_subchannel(cdev->dev.parent);
614         chp_id_init(&chpid);
615         chpid.id = sch->schib.pmcw.chpid[chp_no];
616         return chp_get_chp_desc(chpid);
617 }
618
619 // FIXME: these have to go:
620
621 int
622 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
623 {
624         return cdev->private->schid.sch_no;
625 }
626
627 int
628 _ccw_device_get_device_number(struct ccw_device *cdev)
629 {
630         return cdev->private->dev_id.devno;
631 }
632
633
634 MODULE_LICENSE("GPL");
635 EXPORT_SYMBOL(ccw_device_set_options_mask);
636 EXPORT_SYMBOL(ccw_device_set_options);
637 EXPORT_SYMBOL(ccw_device_clear_options);
638 EXPORT_SYMBOL(ccw_device_clear);
639 EXPORT_SYMBOL(ccw_device_halt);
640 EXPORT_SYMBOL(ccw_device_resume);
641 EXPORT_SYMBOL(ccw_device_start_timeout);
642 EXPORT_SYMBOL(ccw_device_start);
643 EXPORT_SYMBOL(ccw_device_start_timeout_key);
644 EXPORT_SYMBOL(ccw_device_start_key);
645 EXPORT_SYMBOL(ccw_device_get_ciw);
646 EXPORT_SYMBOL(ccw_device_get_path_mask);
647 EXPORT_SYMBOL(read_conf_data);
648 EXPORT_SYMBOL(read_dev_chars);
649 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
650 EXPORT_SYMBOL(_ccw_device_get_device_number);
651 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
652 EXPORT_SYMBOL_GPL(read_conf_data_lpm);