Merge branch 'fixes-jgarzik' of git://git.kernel.org/pub/scm/linux/kernel/git/linvill...
[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 /*
292  * Try to break the lock on a boxed device.
293  */
294 int
295 ccw_device_stlck(struct ccw_device *cdev)
296 {
297         void *buf, *buf2;
298         unsigned long flags;
299         struct subchannel *sch;
300         int ret;
301
302         if (!cdev)
303                 return -ENODEV;
304
305         if (cdev->drv && !cdev->private->options.force)
306                 return -EINVAL;
307
308         sch = to_subchannel(cdev->dev.parent);
309         
310         CIO_TRACE_EVENT(2, "stl lock");
311         CIO_TRACE_EVENT(2, cdev->dev.bus_id);
312
313         buf = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
314         if (!buf)
315                 return -ENOMEM;
316         buf2 = kmalloc(32*sizeof(char), GFP_DMA|GFP_KERNEL);
317         if (!buf2) {
318                 kfree(buf);
319                 return -ENOMEM;
320         }
321         spin_lock_irqsave(sch->lock, flags);
322         ret = cio_enable_subchannel(sch, 3);
323         if (ret)
324                 goto out_unlock;
325         /*
326          * Setup ccw. We chain an unconditional reserve and a release so we
327          * only break the lock.
328          */
329         cdev->private->iccws[0].cmd_code = CCW_CMD_STLCK;
330         cdev->private->iccws[0].cda = (__u32) __pa(buf);
331         cdev->private->iccws[0].count = 32;
332         cdev->private->iccws[0].flags = CCW_FLAG_CC;
333         cdev->private->iccws[1].cmd_code = CCW_CMD_RELEASE;
334         cdev->private->iccws[1].cda = (__u32) __pa(buf2);
335         cdev->private->iccws[1].count = 32;
336         cdev->private->iccws[1].flags = 0;
337         ret = cio_start(sch, cdev->private->iccws, 0);
338         if (ret) {
339                 cio_disable_subchannel(sch); //FIXME: return code?
340                 goto out_unlock;
341         }
342         cdev->private->irb.scsw.actl |= SCSW_ACTL_START_PEND;
343         spin_unlock_irqrestore(sch->lock, flags);
344         wait_event(cdev->private->wait_q, cdev->private->irb.scsw.actl == 0);
345         spin_lock_irqsave(sch->lock, flags);
346         cio_disable_subchannel(sch); //FIXME: return code?
347         if ((cdev->private->irb.scsw.dstat !=
348              (DEV_STAT_CHN_END|DEV_STAT_DEV_END)) ||
349             (cdev->private->irb.scsw.cstat != 0))
350                 ret = -EIO;
351         /* Clear irb. */
352         memset(&cdev->private->irb, 0, sizeof(struct irb));
353 out_unlock:
354         kfree(buf);
355         kfree(buf2);
356         spin_unlock_irqrestore(sch->lock, flags);
357         return ret;
358 }
359
360 void *
361 ccw_device_get_chp_desc(struct ccw_device *cdev, int chp_no)
362 {
363         struct subchannel *sch;
364         struct chp_id chpid;
365
366         sch = to_subchannel(cdev->dev.parent);
367         chp_id_init(&chpid);
368         chpid.id = sch->schib.pmcw.chpid[chp_no];
369         return chp_get_chp_desc(chpid);
370 }
371
372 /**
373  * ccw_device_get_id - obtain a ccw device id
374  * @cdev: device to obtain the id for
375  * @dev_id: where to fill in the values
376  */
377 void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
378 {
379         *dev_id = cdev->private->dev_id;
380 }
381 EXPORT_SYMBOL(ccw_device_get_id);
382
383 // FIXME: these have to go:
384
385 int
386 _ccw_device_get_subchannel_number(struct ccw_device *cdev)
387 {
388         return cdev->private->schid.sch_no;
389 }
390
391
392 MODULE_LICENSE("GPL");
393 EXPORT_SYMBOL(ccw_device_set_options_mask);
394 EXPORT_SYMBOL(ccw_device_set_options);
395 EXPORT_SYMBOL(ccw_device_clear_options);
396 EXPORT_SYMBOL(ccw_device_clear);
397 EXPORT_SYMBOL(ccw_device_halt);
398 EXPORT_SYMBOL(ccw_device_resume);
399 EXPORT_SYMBOL(ccw_device_start_timeout);
400 EXPORT_SYMBOL(ccw_device_start);
401 EXPORT_SYMBOL(ccw_device_start_timeout_key);
402 EXPORT_SYMBOL(ccw_device_start_key);
403 EXPORT_SYMBOL(ccw_device_get_ciw);
404 EXPORT_SYMBOL(ccw_device_get_path_mask);
405 EXPORT_SYMBOL(_ccw_device_get_subchannel_number);
406 EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);