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