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