hwmon: (acpi_power_meter) Fix acpi_bus_get_device() return value check
[linux-drm-fsl-dcu.git] / drivers / s390 / cio / eadm_sch.c
1 /*
2  * Driver for s390 eadm subchannels
3  *
4  * Copyright IBM Corp. 2012
5  * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
6  */
7
8 #include <linux/kernel_stat.h>
9 #include <linux/completion.h>
10 #include <linux/workqueue.h>
11 #include <linux/spinlock.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/timer.h>
15 #include <linux/slab.h>
16 #include <linux/list.h>
17
18 #include <asm/css_chars.h>
19 #include <asm/debug.h>
20 #include <asm/isc.h>
21 #include <asm/cio.h>
22 #include <asm/scsw.h>
23 #include <asm/eadm.h>
24
25 #include "eadm_sch.h"
26 #include "ioasm.h"
27 #include "cio.h"
28 #include "css.h"
29 #include "orb.h"
30
31 MODULE_DESCRIPTION("driver for s390 eadm subchannels");
32 MODULE_LICENSE("GPL");
33
34 #define EADM_TIMEOUT (5 * HZ)
35 static DEFINE_SPINLOCK(list_lock);
36 static LIST_HEAD(eadm_list);
37
38 static debug_info_t *eadm_debug;
39
40 #define EADM_LOG(imp, txt) do {                                 \
41                 debug_text_event(eadm_debug, imp, txt);         \
42         } while (0)
43
44 static void EADM_LOG_HEX(int level, void *data, int length)
45 {
46         if (!debug_level_enabled(eadm_debug, level))
47                 return;
48         while (length > 0) {
49                 debug_event(eadm_debug, level, data, length);
50                 length -= eadm_debug->buf_size;
51                 data += eadm_debug->buf_size;
52         }
53 }
54
55 static void orb_init(union orb *orb)
56 {
57         memset(orb, 0, sizeof(union orb));
58         orb->eadm.compat1 = 1;
59         orb->eadm.compat2 = 1;
60         orb->eadm.fmt = 1;
61         orb->eadm.x = 1;
62 }
63
64 static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob)
65 {
66         union orb *orb = &get_eadm_private(sch)->orb;
67         int cc;
68
69         orb_init(orb);
70         orb->eadm.aob = (u32)__pa(aob);
71         orb->eadm.intparm = (u32)(addr_t)sch;
72         orb->eadm.key = PAGE_DEFAULT_KEY >> 4;
73
74         EADM_LOG(6, "start");
75         EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid));
76
77         cc = ssch(sch->schid, orb);
78         switch (cc) {
79         case 0:
80                 sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND;
81                 break;
82         case 1:         /* status pending */
83         case 2:         /* busy */
84                 return -EBUSY;
85         case 3:         /* not operational */
86                 return -ENODEV;
87         }
88         return 0;
89 }
90
91 static int eadm_subchannel_clear(struct subchannel *sch)
92 {
93         int cc;
94
95         cc = csch(sch->schid);
96         if (cc)
97                 return -ENODEV;
98
99         sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND;
100         return 0;
101 }
102
103 static void eadm_subchannel_timeout(unsigned long data)
104 {
105         struct subchannel *sch = (struct subchannel *) data;
106
107         spin_lock_irq(sch->lock);
108         EADM_LOG(1, "timeout");
109         EADM_LOG_HEX(1, &sch->schid, sizeof(sch->schid));
110         if (eadm_subchannel_clear(sch))
111                 EADM_LOG(0, "clear failed");
112         spin_unlock_irq(sch->lock);
113 }
114
115 static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires)
116 {
117         struct eadm_private *private = get_eadm_private(sch);
118
119         if (expires == 0) {
120                 del_timer(&private->timer);
121                 return;
122         }
123         if (timer_pending(&private->timer)) {
124                 if (mod_timer(&private->timer, jiffies + expires))
125                         return;
126         }
127         private->timer.function = eadm_subchannel_timeout;
128         private->timer.data = (unsigned long) sch;
129         private->timer.expires = jiffies + expires;
130         add_timer(&private->timer);
131 }
132
133 static void eadm_subchannel_irq(struct subchannel *sch)
134 {
135         struct eadm_private *private = get_eadm_private(sch);
136         struct eadm_scsw *scsw = &sch->schib.scsw.eadm;
137         struct irb *irb = (struct irb *)&S390_lowcore.irb;
138         int error = 0;
139
140         EADM_LOG(6, "irq");
141         EADM_LOG_HEX(6, irb, sizeof(*irb));
142
143         inc_irq_stat(IRQIO_ADM);
144
145         if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
146             && scsw->eswf == 1 && irb->esw.eadm.erw.r)
147                 error = -EIO;
148
149         if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
150                 error = -ETIMEDOUT;
151
152         eadm_subchannel_set_timeout(sch, 0);
153
154         if (private->state != EADM_BUSY) {
155                 EADM_LOG(1, "irq unsol");
156                 EADM_LOG_HEX(1, irb, sizeof(*irb));
157                 private->state = EADM_NOT_OPER;
158                 css_sched_sch_todo(sch, SCH_TODO_EVAL);
159                 return;
160         }
161         scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
162         private->state = EADM_IDLE;
163
164         if (private->completion)
165                 complete(private->completion);
166 }
167
168 static struct subchannel *eadm_get_idle_sch(void)
169 {
170         struct eadm_private *private;
171         struct subchannel *sch;
172         unsigned long flags;
173
174         spin_lock_irqsave(&list_lock, flags);
175         list_for_each_entry(private, &eadm_list, head) {
176                 sch = private->sch;
177                 spin_lock(sch->lock);
178                 if (private->state == EADM_IDLE) {
179                         private->state = EADM_BUSY;
180                         list_move_tail(&private->head, &eadm_list);
181                         spin_unlock(sch->lock);
182                         spin_unlock_irqrestore(&list_lock, flags);
183
184                         return sch;
185                 }
186                 spin_unlock(sch->lock);
187         }
188         spin_unlock_irqrestore(&list_lock, flags);
189
190         return NULL;
191 }
192
193 static int eadm_start_aob(struct aob *aob)
194 {
195         struct eadm_private *private;
196         struct subchannel *sch;
197         unsigned long flags;
198         int ret;
199
200         sch = eadm_get_idle_sch();
201         if (!sch)
202                 return -EBUSY;
203
204         spin_lock_irqsave(sch->lock, flags);
205         eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
206         ret = eadm_subchannel_start(sch, aob);
207         if (!ret)
208                 goto out_unlock;
209
210         /* Handle start subchannel failure. */
211         eadm_subchannel_set_timeout(sch, 0);
212         private = get_eadm_private(sch);
213         private->state = EADM_NOT_OPER;
214         css_sched_sch_todo(sch, SCH_TODO_EVAL);
215
216 out_unlock:
217         spin_unlock_irqrestore(sch->lock, flags);
218
219         return ret;
220 }
221
222 static int eadm_subchannel_probe(struct subchannel *sch)
223 {
224         struct eadm_private *private;
225         int ret;
226
227         private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
228         if (!private)
229                 return -ENOMEM;
230
231         INIT_LIST_HEAD(&private->head);
232         init_timer(&private->timer);
233
234         spin_lock_irq(sch->lock);
235         set_eadm_private(sch, private);
236         private->state = EADM_IDLE;
237         private->sch = sch;
238         sch->isc = EADM_SCH_ISC;
239         ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
240         if (ret) {
241                 set_eadm_private(sch, NULL);
242                 spin_unlock_irq(sch->lock);
243                 kfree(private);
244                 goto out;
245         }
246         spin_unlock_irq(sch->lock);
247
248         spin_lock_irq(&list_lock);
249         list_add(&private->head, &eadm_list);
250         spin_unlock_irq(&list_lock);
251
252         if (dev_get_uevent_suppress(&sch->dev)) {
253                 dev_set_uevent_suppress(&sch->dev, 0);
254                 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
255         }
256 out:
257         return ret;
258 }
259
260 static void eadm_quiesce(struct subchannel *sch)
261 {
262         struct eadm_private *private = get_eadm_private(sch);
263         DECLARE_COMPLETION_ONSTACK(completion);
264         int ret;
265
266         spin_lock_irq(sch->lock);
267         if (private->state != EADM_BUSY)
268                 goto disable;
269
270         if (eadm_subchannel_clear(sch))
271                 goto disable;
272
273         private->completion = &completion;
274         spin_unlock_irq(sch->lock);
275
276         wait_for_completion_io(&completion);
277
278         spin_lock_irq(sch->lock);
279         private->completion = NULL;
280
281 disable:
282         eadm_subchannel_set_timeout(sch, 0);
283         do {
284                 ret = cio_disable_subchannel(sch);
285         } while (ret == -EBUSY);
286
287         spin_unlock_irq(sch->lock);
288 }
289
290 static int eadm_subchannel_remove(struct subchannel *sch)
291 {
292         struct eadm_private *private = get_eadm_private(sch);
293
294         spin_lock_irq(&list_lock);
295         list_del(&private->head);
296         spin_unlock_irq(&list_lock);
297
298         eadm_quiesce(sch);
299
300         spin_lock_irq(sch->lock);
301         set_eadm_private(sch, NULL);
302         spin_unlock_irq(sch->lock);
303
304         kfree(private);
305
306         return 0;
307 }
308
309 static void eadm_subchannel_shutdown(struct subchannel *sch)
310 {
311         eadm_quiesce(sch);
312 }
313
314 static int eadm_subchannel_freeze(struct subchannel *sch)
315 {
316         return cio_disable_subchannel(sch);
317 }
318
319 static int eadm_subchannel_restore(struct subchannel *sch)
320 {
321         return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
322 }
323
324 /**
325  * eadm_subchannel_sch_event - process subchannel event
326  * @sch: subchannel
327  * @process: non-zero if function is called in process context
328  *
329  * An unspecified event occurred for this subchannel. Adjust data according
330  * to the current operational state of the subchannel. Return zero when the
331  * event has been handled sufficiently or -EAGAIN when this function should
332  * be called again in process context.
333  */
334 static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
335 {
336         struct eadm_private *private;
337         unsigned long flags;
338         int ret = 0;
339
340         spin_lock_irqsave(sch->lock, flags);
341         if (!device_is_registered(&sch->dev))
342                 goto out_unlock;
343
344         if (work_pending(&sch->todo_work))
345                 goto out_unlock;
346
347         if (cio_update_schib(sch)) {
348                 css_sched_sch_todo(sch, SCH_TODO_UNREG);
349                 goto out_unlock;
350         }
351         private = get_eadm_private(sch);
352         if (private->state == EADM_NOT_OPER)
353                 private->state = EADM_IDLE;
354
355 out_unlock:
356         spin_unlock_irqrestore(sch->lock, flags);
357
358         return ret;
359 }
360
361 static struct css_device_id eadm_subchannel_ids[] = {
362         { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
363         { /* end of list */ },
364 };
365 MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
366
367 static struct css_driver eadm_subchannel_driver = {
368         .drv = {
369                 .name = "eadm_subchannel",
370                 .owner = THIS_MODULE,
371         },
372         .subchannel_type = eadm_subchannel_ids,
373         .irq = eadm_subchannel_irq,
374         .probe = eadm_subchannel_probe,
375         .remove = eadm_subchannel_remove,
376         .shutdown = eadm_subchannel_shutdown,
377         .sch_event = eadm_subchannel_sch_event,
378         .freeze = eadm_subchannel_freeze,
379         .thaw = eadm_subchannel_restore,
380         .restore = eadm_subchannel_restore,
381 };
382
383 static struct eadm_ops eadm_ops = {
384         .eadm_start = eadm_start_aob,
385         .owner = THIS_MODULE,
386 };
387
388 static int __init eadm_sch_init(void)
389 {
390         int ret;
391
392         if (!css_general_characteristics.eadm)
393                 return -ENXIO;
394
395         eadm_debug = debug_register("eadm_log", 16, 1, 16);
396         if (!eadm_debug)
397                 return -ENOMEM;
398
399         debug_register_view(eadm_debug, &debug_hex_ascii_view);
400         debug_set_level(eadm_debug, 2);
401
402         isc_register(EADM_SCH_ISC);
403         ret = css_driver_register(&eadm_subchannel_driver);
404         if (ret)
405                 goto cleanup;
406
407         register_eadm_ops(&eadm_ops);
408         return ret;
409
410 cleanup:
411         isc_unregister(EADM_SCH_ISC);
412         debug_unregister(eadm_debug);
413         return ret;
414 }
415
416 static void __exit eadm_sch_exit(void)
417 {
418         unregister_eadm_ops(&eadm_ops);
419         css_driver_unregister(&eadm_subchannel_driver);
420         isc_unregister(EADM_SCH_ISC);
421         debug_unregister(eadm_debug);
422 }
423 module_init(eadm_sch_init);
424 module_exit(eadm_sch_exit);