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