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 (7 * 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 debug_event(eadm_debug, level, data, length);
49 static void orb_init(union orb *orb)
51 memset(orb, 0, sizeof(union orb));
52 orb->eadm.compat1 = 1;
53 orb->eadm.compat2 = 1;
58 static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob)
60 union orb *orb = &get_eadm_private(sch)->orb;
64 orb->eadm.aob = (u32)__pa(aob);
65 orb->eadm.intparm = (u32)(addr_t)sch;
66 orb->eadm.key = PAGE_DEFAULT_KEY >> 4;
69 EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid));
71 cc = ssch(sch->schid, orb);
74 sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND;
76 case 1: /* status pending */
79 case 3: /* not operational */
85 static int eadm_subchannel_clear(struct subchannel *sch)
89 cc = csch(sch->schid);
93 sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND;
97 static void eadm_subchannel_timeout(struct timer_list *t)
99 struct eadm_private *private = from_timer(private, t, timer);
100 struct subchannel *sch = private->sch;
102 spin_lock_irq(sch->lock);
103 EADM_LOG(1, "timeout");
104 EADM_LOG_HEX(1, &sch->schid, sizeof(sch->schid));
105 if (eadm_subchannel_clear(sch))
106 EADM_LOG(0, "clear failed");
107 spin_unlock_irq(sch->lock);
110 static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires)
112 struct eadm_private *private = get_eadm_private(sch);
115 del_timer(&private->timer);
118 if (timer_pending(&private->timer)) {
119 if (mod_timer(&private->timer, jiffies + expires))
122 private->timer.expires = jiffies + expires;
123 add_timer(&private->timer);
126 static void eadm_subchannel_irq(struct subchannel *sch)
128 struct eadm_private *private = get_eadm_private(sch);
129 struct eadm_scsw *scsw = &sch->schib.scsw.eadm;
130 struct irb *irb = this_cpu_ptr(&cio_irb);
131 blk_status_t error = BLK_STS_OK;
134 EADM_LOG_HEX(6, irb, sizeof(*irb));
136 inc_irq_stat(IRQIO_ADM);
138 if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
139 && scsw->eswf == 1 && irb->esw.eadm.erw.r)
140 error = BLK_STS_IOERR;
142 if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
143 error = BLK_STS_TIMEOUT;
145 eadm_subchannel_set_timeout(sch, 0);
147 if (private->state != EADM_BUSY) {
148 EADM_LOG(1, "irq unsol");
149 EADM_LOG_HEX(1, irb, sizeof(*irb));
150 private->state = EADM_NOT_OPER;
151 css_sched_sch_todo(sch, SCH_TODO_EVAL);
154 scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
155 private->state = EADM_IDLE;
157 if (private->completion)
158 complete(private->completion);
161 static struct subchannel *eadm_get_idle_sch(void)
163 struct eadm_private *private;
164 struct subchannel *sch;
167 spin_lock_irqsave(&list_lock, flags);
168 list_for_each_entry(private, &eadm_list, head) {
170 spin_lock(sch->lock);
171 if (private->state == EADM_IDLE) {
172 private->state = EADM_BUSY;
173 list_move_tail(&private->head, &eadm_list);
174 spin_unlock(sch->lock);
175 spin_unlock_irqrestore(&list_lock, flags);
179 spin_unlock(sch->lock);
181 spin_unlock_irqrestore(&list_lock, flags);
186 int eadm_start_aob(struct aob *aob)
188 struct eadm_private *private;
189 struct subchannel *sch;
193 sch = eadm_get_idle_sch();
197 spin_lock_irqsave(sch->lock, flags);
198 eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
199 ret = eadm_subchannel_start(sch, aob);
203 /* Handle start subchannel failure. */
204 eadm_subchannel_set_timeout(sch, 0);
205 private = get_eadm_private(sch);
206 private->state = EADM_NOT_OPER;
207 css_sched_sch_todo(sch, SCH_TODO_EVAL);
210 spin_unlock_irqrestore(sch->lock, flags);
214 EXPORT_SYMBOL_GPL(eadm_start_aob);
216 static int eadm_subchannel_probe(struct subchannel *sch)
218 struct eadm_private *private;
221 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
225 INIT_LIST_HEAD(&private->head);
226 timer_setup(&private->timer, eadm_subchannel_timeout, 0);
228 spin_lock_irq(sch->lock);
229 set_eadm_private(sch, private);
230 private->state = EADM_IDLE;
232 sch->isc = EADM_SCH_ISC;
233 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
235 set_eadm_private(sch, NULL);
236 spin_unlock_irq(sch->lock);
240 spin_unlock_irq(sch->lock);
242 spin_lock_irq(&list_lock);
243 list_add(&private->head, &eadm_list);
244 spin_unlock_irq(&list_lock);
246 if (dev_get_uevent_suppress(&sch->dev)) {
247 dev_set_uevent_suppress(&sch->dev, 0);
248 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
254 static void eadm_quiesce(struct subchannel *sch)
256 struct eadm_private *private = get_eadm_private(sch);
257 DECLARE_COMPLETION_ONSTACK(completion);
260 spin_lock_irq(sch->lock);
261 if (private->state != EADM_BUSY)
264 if (eadm_subchannel_clear(sch))
267 private->completion = &completion;
268 spin_unlock_irq(sch->lock);
270 wait_for_completion_io(&completion);
272 spin_lock_irq(sch->lock);
273 private->completion = NULL;
276 eadm_subchannel_set_timeout(sch, 0);
278 ret = cio_disable_subchannel(sch);
279 } while (ret == -EBUSY);
281 spin_unlock_irq(sch->lock);
284 static int eadm_subchannel_remove(struct subchannel *sch)
286 struct eadm_private *private = get_eadm_private(sch);
288 spin_lock_irq(&list_lock);
289 list_del(&private->head);
290 spin_unlock_irq(&list_lock);
294 spin_lock_irq(sch->lock);
295 set_eadm_private(sch, NULL);
296 spin_unlock_irq(sch->lock);
303 static void eadm_subchannel_shutdown(struct subchannel *sch)
308 static int eadm_subchannel_freeze(struct subchannel *sch)
310 return cio_disable_subchannel(sch);
313 static int eadm_subchannel_restore(struct subchannel *sch)
315 return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
319 * eadm_subchannel_sch_event - process subchannel event
321 * @process: non-zero if function is called in process context
323 * An unspecified event occurred for this subchannel. Adjust data according
324 * to the current operational state of the subchannel. Return zero when the
325 * event has been handled sufficiently or -EAGAIN when this function should
326 * be called again in process context.
328 static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
330 struct eadm_private *private;
333 spin_lock_irqsave(sch->lock, flags);
334 if (!device_is_registered(&sch->dev))
337 if (work_pending(&sch->todo_work))
340 if (cio_update_schib(sch)) {
341 css_sched_sch_todo(sch, SCH_TODO_UNREG);
344 private = get_eadm_private(sch);
345 if (private->state == EADM_NOT_OPER)
346 private->state = EADM_IDLE;
349 spin_unlock_irqrestore(sch->lock, flags);
354 static struct css_device_id eadm_subchannel_ids[] = {
355 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
356 { /* end of list */ },
358 MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
360 static struct css_driver eadm_subchannel_driver = {
362 .name = "eadm_subchannel",
363 .owner = THIS_MODULE,
365 .subchannel_type = eadm_subchannel_ids,
366 .irq = eadm_subchannel_irq,
367 .probe = eadm_subchannel_probe,
368 .remove = eadm_subchannel_remove,
369 .shutdown = eadm_subchannel_shutdown,
370 .sch_event = eadm_subchannel_sch_event,
371 .freeze = eadm_subchannel_freeze,
372 .thaw = eadm_subchannel_restore,
373 .restore = eadm_subchannel_restore,
376 static int __init eadm_sch_init(void)
380 if (!css_general_characteristics.eadm)
383 eadm_debug = debug_register("eadm_log", 16, 1, 16);
387 debug_register_view(eadm_debug, &debug_hex_ascii_view);
388 debug_set_level(eadm_debug, 2);
390 isc_register(EADM_SCH_ISC);
391 ret = css_driver_register(&eadm_subchannel_driver);
398 isc_unregister(EADM_SCH_ISC);
399 debug_unregister(eadm_debug);
403 static void __exit eadm_sch_exit(void)
405 css_driver_unregister(&eadm_subchannel_driver);
406 isc_unregister(EADM_SCH_ISC);
407 debug_unregister(eadm_debug);
409 module_init(eadm_sch_init);
410 module_exit(eadm_sch_exit);