[S390] cio: improve unit check handling for internal operations
[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         /* Clear pending timers for device driver initiated I/O. */
220         if (ending_status)
221                 ccw_device_set_timeout(cdev, 0);
222         /*
223          * Now we are ready to call the device driver interrupt handler.
224          */
225         if (cdev->handler)
226                 cdev->handler(cdev, cdev->private->intparm,
227                               &cdev->private->irb);
228
229         /*
230          * Clear the old and now useless interrupt response block.
231          */
232         memset(&cdev->private->irb, 0, sizeof(struct irb));
233
234         return 1;
235 }
236
237 /*
238  * Search for CIW command in extended sense data.
239  */
240 struct ciw *
241 ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
242 {
243         int ciw_cnt;
244
245         if (cdev->private->flags.esid == 0)
246                 return NULL;
247         for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
248                 if (cdev->private->senseid.ciw[ciw_cnt].ct == ct)
249                         return cdev->private->senseid.ciw + ciw_cnt;
250         return NULL;
251 }
252
253 __u8
254 ccw_device_get_path_mask(struct ccw_device *cdev)
255 {
256         struct subchannel *sch;
257
258         sch = to_subchannel(cdev->dev.parent);
259         if (!sch)
260                 return 0;
261         else
262                 return sch->lpm;
263 }
264
265 static void
266 ccw_device_wake_up(struct ccw_device *cdev, unsigned long ip, struct irb *irb)
267 {
268         if (!ip)
269                 /* unsolicited interrupt */
270                 return;
271
272         /* Abuse intparm for error reporting. */
273         if (IS_ERR(irb))
274                 cdev->private->intparm = -EIO;
275         else if (irb->scsw.cc == 1)
276                 /* Retry for deferred condition code. */
277                 cdev->private->intparm = -EAGAIN;
278         else if ((irb->scsw.dstat !=
279                   (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
280                  (irb->scsw.cstat != 0)) {
281                 /*
282                  * We didn't get channel end / device end. Check if path
283                  * verification has been started; we can retry after it has
284                  * finished. We also retry unit checks except for command reject
285                  * or intervention required. Also check for long busy
286                  * conditions.
287                  */
288                  if (cdev->private->flags.doverify ||
289                          cdev->private->state == DEV_STATE_VERIFY)
290                          cdev->private->intparm = -EAGAIN;
291                 else if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
292                          !(irb->ecw[0] &
293                            (SNS0_CMD_REJECT | SNS0_INTERVENTION_REQ)))
294                         cdev->private->intparm = -EAGAIN;
295                 else if ((irb->scsw.dstat & DEV_STAT_ATTENTION) &&
296                          (irb->scsw.dstat & DEV_STAT_DEV_END) &&
297                          (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP))
298                         cdev->private->intparm = -EAGAIN;
299                  else
300                          cdev->private->intparm = -EIO;
301                          
302         } else
303                 cdev->private->intparm = 0;
304         wake_up(&cdev->private->wait_q);
305 }
306
307 static inline int
308 __ccw_device_retry_loop(struct ccw_device *cdev, struct ccw1 *ccw, long magic, __u8 lpm)
309 {
310         int ret;
311         struct subchannel *sch;
312
313         sch = to_subchannel(cdev->dev.parent);
314         do {
315                 ccw_device_set_timeout(cdev, 60 * HZ);
316                 ret = cio_start (sch, ccw, lpm);
317                 if (ret != 0)
318                         ccw_device_set_timeout(cdev, 0);
319                 if (ret == -EBUSY) {
320                         /* Try again later. */
321                         spin_unlock_irq(&sch->lock);
322                         msleep(10);
323                         spin_lock_irq(&sch->lock);
324                         continue;
325                 }
326                 if (ret != 0)
327                         /* Non-retryable error. */
328                         break;
329                 /* Wait for end of request. */
330                 cdev->private->intparm = magic;
331                 spin_unlock_irq(&sch->lock);
332                 wait_event(cdev->private->wait_q,
333                            (cdev->private->intparm == -EIO) ||
334                            (cdev->private->intparm == -EAGAIN) ||
335                            (cdev->private->intparm == 0));
336                 spin_lock_irq(&sch->lock);
337                 /* Check at least for channel end / device end */
338                 if (cdev->private->intparm == -EIO) {
339                         /* Non-retryable error. */
340                         ret = -EIO;
341                         break;
342                 }
343                 if (cdev->private->intparm == 0)
344                         /* Success. */
345                         break;
346                 /* Try again later. */
347                 spin_unlock_irq(&sch->lock);
348                 msleep(10);
349                 spin_lock_irq(&sch->lock);
350         } while (1);
351
352         return ret;
353 }
354
355 /**
356  * read_dev_chars() - read device characteristics
357  * @param cdev   target ccw device
358  * @param buffer pointer to buffer for rdc data
359  * @param length size of rdc data
360  * @returns 0 for success, negative error value on failure
361  *
362  * Context:
363  *   called for online device, lock not held
364  **/
365 int
366 read_dev_chars (struct ccw_device *cdev, void **buffer, int length)
367 {
368         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
369         struct subchannel *sch;
370         int ret;
371         struct ccw1 *rdc_ccw;
372
373         if (!cdev)
374                 return -ENODEV;
375         if (!buffer || !length)
376                 return -EINVAL;
377         sch = to_subchannel(cdev->dev.parent);
378
379         CIO_TRACE_EVENT (4, "rddevch");
380         CIO_TRACE_EVENT (4, sch->dev.bus_id);
381
382         rdc_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
383         if (!rdc_ccw)
384                 return -ENOMEM;
385         rdc_ccw->cmd_code = CCW_CMD_RDC;
386         rdc_ccw->count = length;
387         rdc_ccw->flags = CCW_FLAG_SLI;
388         ret = set_normalized_cda (rdc_ccw, (*buffer));
389         if (ret != 0) {
390                 kfree(rdc_ccw);
391                 return ret;
392         }
393
394         spin_lock_irq(&sch->lock);
395         /* Save interrupt handler. */
396         handler = cdev->handler;
397         /* Temporarily install own handler. */
398         cdev->handler = ccw_device_wake_up;
399         if (cdev->private->state != DEV_STATE_ONLINE)
400                 ret = -ENODEV;
401         else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
402                   !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
403                  cdev->private->flags.doverify)
404                 ret = -EBUSY;
405         else
406                 /* 0x00D9C4C3 == ebcdic "RDC" */
407                 ret = __ccw_device_retry_loop(cdev, rdc_ccw, 0x00D9C4C3, 0);
408
409         /* Restore interrupt handler. */
410         cdev->handler = handler;
411         spin_unlock_irq(&sch->lock);
412
413         clear_normalized_cda (rdc_ccw);
414         kfree(rdc_ccw);
415
416         return ret;
417 }
418
419 /*
420  *  Read Configuration data using path mask
421  */
422 int
423 read_conf_data_lpm (struct ccw_device *cdev, void **buffer, int *length, __u8 lpm)
424 {
425         void (*handler)(struct ccw_device *, unsigned long, struct irb *);
426         struct subchannel *sch;
427         struct ciw *ciw;
428         char *rcd_buf;
429         int ret;
430         struct ccw1 *rcd_ccw;
431
432         if (!cdev)
433                 return -ENODEV;
434         if (!buffer || !length)
435                 return -EINVAL;
436         sch = to_subchannel(cdev->dev.parent);
437
438         CIO_TRACE_EVENT (4, "rdconf");
439         CIO_TRACE_EVENT (4, sch->dev.bus_id);
440
441         /*
442          * scan for RCD command in extended SenseID data
443          */
444         ciw = ccw_device_get_ciw(cdev, CIW_TYPE_RCD);
445         if (!ciw || ciw->cmd == 0)
446                 return -EOPNOTSUPP;
447
448         /* Adjust requested path mask to excluded varied off paths. */
449         if (lpm) {
450                 lpm &= sch->opm;
451                 if (lpm == 0)
452                         return -EACCES;
453         }
454
455         rcd_ccw = kzalloc(sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
456         if (!rcd_ccw)
457                 return -ENOMEM;
458         rcd_buf = kzalloc(ciw->count, GFP_KERNEL | GFP_DMA);
459         if (!rcd_buf) {
460                 kfree(rcd_ccw);
461                 return -ENOMEM;
462         }
463         rcd_ccw->cmd_code = ciw->cmd;
464         rcd_ccw->cda = (__u32) __pa (rcd_buf);
465         rcd_ccw->count = ciw->count;
466         rcd_ccw->flags = CCW_FLAG_SLI;
467
468         spin_lock_irq(&sch->lock);
469         /* Save interrupt handler. */
470         handler = cdev->handler;
471         /* Temporarily install own handler. */
472         cdev->handler = ccw_device_wake_up;
473         if (cdev->private->state != DEV_STATE_ONLINE)
474                 ret = -ENODEV;
475         else if (((sch->schib.scsw.stctl & SCSW_STCTL_PRIM_STATUS) &&
476                   !(sch->schib.scsw.stctl & SCSW_STCTL_SEC_STATUS)) ||
477                  cdev->private->flags.doverify)
478                 ret = -EBUSY;
479         else
480                 /* 0x00D9C3C4 == ebcdic "RCD" */
481                 ret = __ccw_device_retry_loop(cdev, rcd_ccw, 0x00D9C3C4, lpm);
482
483         /* Restore interrupt handler. */
484         cdev->handler = handler;
485         spin_unlock_irq(&sch->lock);
486
487         /*
488          * on success we update the user input parms
489          */
490         if (ret) {
491                 kfree (rcd_buf);
492                 *buffer = NULL;
493                 *length = 0;
494         } else {
495                 *length = ciw->count;
496                 *buffer = rcd_buf;
497         }
498         kfree(rcd_ccw);
499
500         return ret;
501 }
502
503 /*
504  *  Read Configuration data
505  */
506 int
507 read_conf_data (struct ccw_device *cdev, void **buffer, int *length)
508 {
509         return read_conf_data_lpm (cdev, buffer, length, 0);
510 }
511
512 /*
513  * Try to break the lock on a boxed device.
514  */
515 int
516 ccw_device_stlck(struct ccw_device *cdev)
517 {
518         void *buf, *buf2;
519         unsigned long flags;
520         struct subchannel *sch;
521         int ret;
522
523         if (!cdev)
524                 return -ENODEV;
525
526         if (cdev->drv && !cdev->private->options.force)
527                 return -EINVAL;
528
529         sch = to_subchannel(cdev->dev.parent);
530         
531         CIO_TRACE_EVENT(2, "stl lock");
532         CIO_TRACE_EVENT(2, cdev->dev.bus_id);
533
534         buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
535         if (!buf)
536                 return -ENOMEM;
537         buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
538         if (!buf2) {
539                 kfree(buf);
540                 return -ENOMEM;
541         }
542         spin_lock_irqsave(&sch->lock, flags);
543         ret = cio_enable_subchannel(sch, 3);
544         if (ret)
545                 goto out_unlock;
546         /*
547          * Setup ccw. We chain an unconditional reserve and a release so we
548          * only break the lock.
549          */
550         cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
551         cdev->private->iccws[0].cda = (__u32) __pa(buf);
552         cdev->private->iccws[0].count = 32;
553         cdev->private->iccws[0].flags = CCW_FLAG_CC;
554         cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
555         cdev->private->iccws[1].cda = (__u32) __pa(buf2);
556         cdev->private->iccws[1].count = 32;
557         cdev->private->iccws[1].flags = 0;
558         ret = cio_start(sch, cdev->private->iccws, 0);
559         if (ret) {
560                 cio_disable_subchannel(sch); //FIXME: return code?
561                 goto out_unlock;
562         }
563         cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
564         spin_unlock_irqrestore(&sch->lock, flags);
565         wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
566         spin_lock_irqsave(&sch->lock, flags);
567         cio_disable_subchannel(sch); //FIXME: return code?
568         if ((cdev->private->irb.scsw.dstat !=
569              (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
570             (cdev->private->irb.scsw.cstat != 0))
571                 ret = -EIO;
572         /* Clear irb. */
573         memset(&cdev->private->irb, 0, sizeof(struct irb));
574 out_unlock:
575         kfree(buf);
576         kfree(buf2);
577         spin_unlock_irqrestore(&sch->lock, flags);
578         return ret;
579 }
580
581 void *
582 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
583 {
584         struct subchannel *sch;
585
586         sch = to_subchannel(cdev->dev.parent);
587         return chsc_get_chp_desc(sch, chp_no);
588 }
589
590 // FIXME: these have to go:
591
592 int
593 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
594 {
595         return cdev->private->sch_no;
596 }
597
598 int
599 _ccw_device_get_device_number(struct ccw_device *cdev)
600 {
601         return cdev->private->devno;
602 }
603
604
605 MODULE_LICENSE("GPL");
606 EXPORT_SYMBOL(ccw_device_set_options);
607 EXPORT_SYMBOL(ccw_device_clear);
608 EXPORT_SYMBOL(ccw_device_halt);
609 EXPORT_SYMBOL(ccw_device_resume);
610 EXPORT_SYMBOL(ccw_device_start_timeout);
611 EXPORT_SYMBOL(ccw_device_start);
612 EXPORT_SYMBOL(ccw_device_start_timeout_key);
613 EXPORT_SYMBOL(ccw_device_start_key);
614 EXPORT_SYMBOL(ccw_device_get_ciw);
615 EXPORT_SYMBOL(ccw_device_get_path_mask);
616 EXPORT_SYMBOL(read_conf_data);
617 EXPORT_SYMBOL(read_dev_chars);
618 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
619 EXPORT_SYMBOL(_ccw_device_get_device_number);
620 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
621 EXPORT_SYMBOL_GPL(read_conf_data_lpm);