2 * Driver for s390 eadm subchannels
4 * Copyright IBM Corp. 2012
5 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
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>
18 #include <asm/css_chars.h>
19 #include <asm/debug.h>
31 MODULE_DESCRIPTION("driver for s390 eadm subchannels");
32 MODULE_LICENSE("GPL");
34 #define EADM_TIMEOUT (5 * HZ)
35 static DEFINE_SPINLOCK(list_lock);
36 static LIST_HEAD(eadm_list);
38 static debug_info_t *eadm_debug;
40 #define EADM_LOG(imp, txt) do { \
41 debug_text_event(eadm_debug, imp, txt); \
44 static void EADM_LOG_HEX(int level, void *data, int length)
46 if (!debug_level_enabled(eadm_debug, level))
49 debug_event(eadm_debug, level, data, length);
50 length -= eadm_debug->buf_size;
51 data += eadm_debug->buf_size;
55 static void orb_init(union orb *orb)
57 memset(orb, 0, sizeof(union orb));
58 orb->eadm.compat1 = 1;
59 orb->eadm.compat2 = 1;
64 static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob)
66 union orb *orb = &get_eadm_private(sch)->orb;
70 orb->eadm.aob = (u32)__pa(aob);
71 orb->eadm.intparm = (u32)(addr_t)sch;
72 orb->eadm.key = PAGE_DEFAULT_KEY >> 4;
75 EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid));
77 cc = ssch(sch->schid, orb);
80 sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND;
82 case 1: /* status pending */
85 case 3: /* not operational */
91 static int eadm_subchannel_clear(struct subchannel *sch)
95 cc = csch(sch->schid);
99 sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND;
103 static void eadm_subchannel_timeout(unsigned long data)
105 struct subchannel *sch = (struct subchannel *) data;
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);
115 static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires)
117 struct eadm_private *private = get_eadm_private(sch);
120 del_timer(&private->timer);
123 if (timer_pending(&private->timer)) {
124 if (mod_timer(&private->timer, jiffies + expires))
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);
133 static void eadm_subchannel_irq(struct subchannel *sch)
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;
141 EADM_LOG_HEX(6, irb, sizeof(*irb));
143 inc_irq_stat(IRQIO_ADM);
145 if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
146 && scsw->eswf == 1 && irb->esw.eadm.erw.r)
149 if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
152 eadm_subchannel_set_timeout(sch, 0);
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);
161 scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
162 private->state = EADM_IDLE;
164 if (private->completion)
165 complete(private->completion);
168 static struct subchannel *eadm_get_idle_sch(void)
170 struct eadm_private *private;
171 struct subchannel *sch;
174 spin_lock_irqsave(&list_lock, flags);
175 list_for_each_entry(private, &eadm_list, head) {
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);
186 spin_unlock(sch->lock);
188 spin_unlock_irqrestore(&list_lock, flags);
193 static int eadm_start_aob(struct aob *aob)
195 struct eadm_private *private;
196 struct subchannel *sch;
200 sch = eadm_get_idle_sch();
204 spin_lock_irqsave(sch->lock, flags);
205 eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
206 ret = eadm_subchannel_start(sch, aob);
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);
217 spin_unlock_irqrestore(sch->lock, flags);
222 static int eadm_subchannel_probe(struct subchannel *sch)
224 struct eadm_private *private;
227 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
231 INIT_LIST_HEAD(&private->head);
232 init_timer(&private->timer);
234 spin_lock_irq(sch->lock);
235 set_eadm_private(sch, private);
236 private->state = EADM_IDLE;
238 sch->isc = EADM_SCH_ISC;
239 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
241 set_eadm_private(sch, NULL);
242 spin_unlock_irq(sch->lock);
246 spin_unlock_irq(sch->lock);
248 spin_lock_irq(&list_lock);
249 list_add(&private->head, &eadm_list);
250 spin_unlock_irq(&list_lock);
252 if (dev_get_uevent_suppress(&sch->dev)) {
253 dev_set_uevent_suppress(&sch->dev, 0);
254 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
260 static void eadm_quiesce(struct subchannel *sch)
262 struct eadm_private *private = get_eadm_private(sch);
263 DECLARE_COMPLETION_ONSTACK(completion);
266 spin_lock_irq(sch->lock);
267 if (private->state != EADM_BUSY)
270 if (eadm_subchannel_clear(sch))
273 private->completion = &completion;
274 spin_unlock_irq(sch->lock);
276 wait_for_completion_io(&completion);
278 spin_lock_irq(sch->lock);
279 private->completion = NULL;
282 eadm_subchannel_set_timeout(sch, 0);
284 ret = cio_disable_subchannel(sch);
285 } while (ret == -EBUSY);
287 spin_unlock_irq(sch->lock);
290 static int eadm_subchannel_remove(struct subchannel *sch)
292 struct eadm_private *private = get_eadm_private(sch);
294 spin_lock_irq(&list_lock);
295 list_del(&private->head);
296 spin_unlock_irq(&list_lock);
300 spin_lock_irq(sch->lock);
301 set_eadm_private(sch, NULL);
302 spin_unlock_irq(sch->lock);
309 static void eadm_subchannel_shutdown(struct subchannel *sch)
314 static int eadm_subchannel_freeze(struct subchannel *sch)
316 return cio_disable_subchannel(sch);
319 static int eadm_subchannel_restore(struct subchannel *sch)
321 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
325 * eadm_subchannel_sch_event - process subchannel event
327 * @process: non-zero if function is called in process context
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.
334 static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
336 struct eadm_private *private;
340 spin_lock_irqsave(sch->lock, flags);
341 if (!device_is_registered(&sch->dev))
344 if (work_pending(&sch->todo_work))
347 if (cio_update_schib(sch)) {
348 css_sched_sch_todo(sch, SCH_TODO_UNREG);
351 private = get_eadm_private(sch);
352 if (private->state == EADM_NOT_OPER)
353 private->state = EADM_IDLE;
356 spin_unlock_irqrestore(sch->lock, flags);
361 static struct css_device_id eadm_subchannel_ids[] = {
362 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
363 { /* end of list */ },
365 MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
367 static struct css_driver eadm_subchannel_driver = {
369 .name = "eadm_subchannel",
370 .owner = THIS_MODULE,
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,
383 static struct eadm_ops eadm_ops = {
384 .eadm_start = eadm_start_aob,
385 .owner = THIS_MODULE,
388 static int __init eadm_sch_init(void)
392 if (!css_general_characteristics.eadm)
395 eadm_debug = debug_register("eadm_log", 16, 1, 16);
399 debug_register_view(eadm_debug, &debug_hex_ascii_view);
400 debug_set_level(eadm_debug, 2);
402 isc_register(EADM_SCH_ISC);
403 ret = css_driver_register(&eadm_subchannel_driver);
407 register_eadm_ops(&eadm_ops);
411 isc_unregister(EADM_SCH_ISC);
412 debug_unregister(eadm_debug);
416 static void __exit eadm_sch_exit(void)
418 unregister_eadm_ops(&eadm_ops);
419 css_driver_unregister(&eadm_subchannel_driver);
420 isc_unregister(EADM_SCH_ISC);
421 debug_unregister(eadm_debug);
423 module_init(eadm_sch_init);
424 module_exit(eadm_sch_exit);