blob: 8b463681a14902f93a8aabbdf73556f4e4a1f512 [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Otteadb86a2012-08-28 16:48:16 +02002/*
3 * Driver for s390 eadm subchannels
4 *
5 * Copyright IBM Corp. 2012
6 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
7 */
8
Sebastian Ott2e73c2c2012-08-28 16:48:47 +02009#include <linux/kernel_stat.h>
Sebastian Ott6aa26772013-09-25 12:29:05 +020010#include <linux/completion.h>
Sebastian Otteadb86a2012-08-28 16:48:16 +020011#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/timer.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18
19#include <asm/css_chars.h>
20#include <asm/debug.h>
21#include <asm/isc.h>
22#include <asm/cio.h>
23#include <asm/scsw.h>
24#include <asm/eadm.h>
25
26#include "eadm_sch.h"
27#include "ioasm.h"
28#include "cio.h"
29#include "css.h"
30#include "orb.h"
31
32MODULE_DESCRIPTION("driver for s390 eadm subchannels");
33MODULE_LICENSE("GPL");
34
Sebastian Otte2578b82014-12-05 16:30:49 +010035#define EADM_TIMEOUT (7 * HZ)
Sebastian Otteadb86a2012-08-28 16:48:16 +020036static DEFINE_SPINLOCK(list_lock);
37static LIST_HEAD(eadm_list);
38
39static debug_info_t *eadm_debug;
40
41#define EADM_LOG(imp, txt) do { \
42 debug_text_event(eadm_debug, imp, txt); \
43 } while (0)
44
45static void EADM_LOG_HEX(int level, void *data, int length)
46{
Sebastian Ott94158e52017-10-09 17:49:38 +020047 debug_event(eadm_debug, level, data, length);
Sebastian Otteadb86a2012-08-28 16:48:16 +020048}
49
50static void orb_init(union orb *orb)
51{
52 memset(orb, 0, sizeof(union orb));
53 orb->eadm.compat1 = 1;
54 orb->eadm.compat2 = 1;
55 orb->eadm.fmt = 1;
56 orb->eadm.x = 1;
57}
58
59static int eadm_subchannel_start(struct subchannel *sch, struct aob *aob)
60{
61 union orb *orb = &get_eadm_private(sch)->orb;
62 int cc;
63
64 orb_init(orb);
65 orb->eadm.aob = (u32)__pa(aob);
66 orb->eadm.intparm = (u32)(addr_t)sch;
67 orb->eadm.key = PAGE_DEFAULT_KEY >> 4;
68
69 EADM_LOG(6, "start");
70 EADM_LOG_HEX(6, &sch->schid, sizeof(sch->schid));
71
72 cc = ssch(sch->schid, orb);
73 switch (cc) {
74 case 0:
75 sch->schib.scsw.eadm.actl |= SCSW_ACTL_START_PEND;
76 break;
77 case 1: /* status pending */
78 case 2: /* busy */
79 return -EBUSY;
80 case 3: /* not operational */
81 return -ENODEV;
82 }
83 return 0;
84}
85
86static int eadm_subchannel_clear(struct subchannel *sch)
87{
88 int cc;
89
90 cc = csch(sch->schid);
91 if (cc)
92 return -ENODEV;
93
94 sch->schib.scsw.eadm.actl |= SCSW_ACTL_CLEAR_PEND;
95 return 0;
96}
97
Kees Cook846d0c62017-10-16 16:43:25 -070098static void eadm_subchannel_timeout(struct timer_list *t)
Sebastian Otteadb86a2012-08-28 16:48:16 +020099{
Kees Cook846d0c62017-10-16 16:43:25 -0700100 struct eadm_private *private = from_timer(private, t, timer);
101 struct subchannel *sch = private->sch;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200102
103 spin_lock_irq(sch->lock);
104 EADM_LOG(1, "timeout");
105 EADM_LOG_HEX(1, &sch->schid, sizeof(sch->schid));
106 if (eadm_subchannel_clear(sch))
107 EADM_LOG(0, "clear failed");
108 spin_unlock_irq(sch->lock);
109}
110
111static void eadm_subchannel_set_timeout(struct subchannel *sch, int expires)
112{
113 struct eadm_private *private = get_eadm_private(sch);
114
115 if (expires == 0) {
116 del_timer(&private->timer);
117 return;
118 }
119 if (timer_pending(&private->timer)) {
120 if (mod_timer(&private->timer, jiffies + expires))
121 return;
122 }
Sebastian Otteadb86a2012-08-28 16:48:16 +0200123 private->timer.expires = jiffies + expires;
124 add_timer(&private->timer);
125}
126
127static void eadm_subchannel_irq(struct subchannel *sch)
128{
129 struct eadm_private *private = get_eadm_private(sch);
130 struct eadm_scsw *scsw = &sch->schib.scsw.eadm;
Christoph Lameter0bf7fcf2014-08-17 12:30:46 -0500131 struct irb *irb = this_cpu_ptr(&cio_irb);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200132 blk_status_t error = BLK_STS_OK;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200133
134 EADM_LOG(6, "irq");
135 EADM_LOG_HEX(6, irb, sizeof(*irb));
136
Heiko Carstens420f42e2013-01-02 15:18:18 +0100137 inc_irq_stat(IRQIO_ADM);
Sebastian Ott2e73c2c2012-08-28 16:48:47 +0200138
Sebastian Otteadb86a2012-08-28 16:48:16 +0200139 if ((scsw->stctl & (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))
140 && scsw->eswf == 1 && irb->esw.eadm.erw.r)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200141 error = BLK_STS_IOERR;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200142
143 if (scsw->fctl & SCSW_FCTL_CLEAR_FUNC)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200144 error = BLK_STS_TIMEOUT;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200145
146 eadm_subchannel_set_timeout(sch, 0);
147
148 if (private->state != EADM_BUSY) {
149 EADM_LOG(1, "irq unsol");
150 EADM_LOG_HEX(1, irb, sizeof(*irb));
151 private->state = EADM_NOT_OPER;
152 css_sched_sch_todo(sch, SCH_TODO_EVAL);
153 return;
154 }
155 scm_irq_handler((struct aob *)(unsigned long)scsw->aob, error);
156 private->state = EADM_IDLE;
Sebastian Ott6aa26772013-09-25 12:29:05 +0200157
158 if (private->completion)
159 complete(private->completion);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200160}
161
162static struct subchannel *eadm_get_idle_sch(void)
163{
164 struct eadm_private *private;
165 struct subchannel *sch;
166 unsigned long flags;
167
168 spin_lock_irqsave(&list_lock, flags);
169 list_for_each_entry(private, &eadm_list, head) {
170 sch = private->sch;
171 spin_lock(sch->lock);
172 if (private->state == EADM_IDLE) {
173 private->state = EADM_BUSY;
174 list_move_tail(&private->head, &eadm_list);
175 spin_unlock(sch->lock);
176 spin_unlock_irqrestore(&list_lock, flags);
177
178 return sch;
179 }
180 spin_unlock(sch->lock);
181 }
182 spin_unlock_irqrestore(&list_lock, flags);
183
184 return NULL;
185}
186
Sebastian Ott605c3692013-11-14 10:44:56 +0100187int eadm_start_aob(struct aob *aob)
Sebastian Otteadb86a2012-08-28 16:48:16 +0200188{
189 struct eadm_private *private;
190 struct subchannel *sch;
191 unsigned long flags;
192 int ret;
193
194 sch = eadm_get_idle_sch();
195 if (!sch)
196 return -EBUSY;
197
198 spin_lock_irqsave(sch->lock, flags);
199 eadm_subchannel_set_timeout(sch, EADM_TIMEOUT);
200 ret = eadm_subchannel_start(sch, aob);
201 if (!ret)
202 goto out_unlock;
203
204 /* Handle start subchannel failure. */
205 eadm_subchannel_set_timeout(sch, 0);
206 private = get_eadm_private(sch);
207 private->state = EADM_NOT_OPER;
208 css_sched_sch_todo(sch, SCH_TODO_EVAL);
209
210out_unlock:
211 spin_unlock_irqrestore(sch->lock, flags);
212
213 return ret;
214}
Sebastian Ott605c3692013-11-14 10:44:56 +0100215EXPORT_SYMBOL_GPL(eadm_start_aob);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200216
217static int eadm_subchannel_probe(struct subchannel *sch)
218{
219 struct eadm_private *private;
220 int ret;
221
222 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
223 if (!private)
224 return -ENOMEM;
225
226 INIT_LIST_HEAD(&private->head);
Kees Cook846d0c62017-10-16 16:43:25 -0700227 timer_setup(&private->timer, eadm_subchannel_timeout, 0);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200228
229 spin_lock_irq(sch->lock);
230 set_eadm_private(sch, private);
231 private->state = EADM_IDLE;
232 private->sch = sch;
233 sch->isc = EADM_SCH_ISC;
234 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
235 if (ret) {
236 set_eadm_private(sch, NULL);
237 spin_unlock_irq(sch->lock);
238 kfree(private);
239 goto out;
240 }
241 spin_unlock_irq(sch->lock);
242
243 spin_lock_irq(&list_lock);
244 list_add(&private->head, &eadm_list);
245 spin_unlock_irq(&list_lock);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200246out:
247 return ret;
248}
249
250static void eadm_quiesce(struct subchannel *sch)
251{
Sebastian Ott6aa26772013-09-25 12:29:05 +0200252 struct eadm_private *private = get_eadm_private(sch);
253 DECLARE_COMPLETION_ONSTACK(completion);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200254 int ret;
255
Sebastian Ott6aa26772013-09-25 12:29:05 +0200256 spin_lock_irq(sch->lock);
257 if (private->state != EADM_BUSY)
258 goto disable;
259
260 if (eadm_subchannel_clear(sch))
261 goto disable;
262
263 private->completion = &completion;
264 spin_unlock_irq(sch->lock);
265
266 wait_for_completion_io(&completion);
267
268 spin_lock_irq(sch->lock);
269 private->completion = NULL;
270
271disable:
272 eadm_subchannel_set_timeout(sch, 0);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200273 do {
Sebastian Otteadb86a2012-08-28 16:48:16 +0200274 ret = cio_disable_subchannel(sch);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200275 } while (ret == -EBUSY);
Sebastian Ott6aa26772013-09-25 12:29:05 +0200276
277 spin_unlock_irq(sch->lock);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200278}
279
Uwe Kleine-Königa7bdb9a2021-07-13 21:35:19 +0200280static void eadm_subchannel_remove(struct subchannel *sch)
Sebastian Otteadb86a2012-08-28 16:48:16 +0200281{
282 struct eadm_private *private = get_eadm_private(sch);
283
284 spin_lock_irq(&list_lock);
285 list_del(&private->head);
286 spin_unlock_irq(&list_lock);
287
288 eadm_quiesce(sch);
289
290 spin_lock_irq(sch->lock);
291 set_eadm_private(sch, NULL);
292 spin_unlock_irq(sch->lock);
293
294 kfree(private);
Sebastian Otteadb86a2012-08-28 16:48:16 +0200295}
296
297static void eadm_subchannel_shutdown(struct subchannel *sch)
298{
299 eadm_quiesce(sch);
300}
301
Sebastian Otteadb86a2012-08-28 16:48:16 +0200302/**
303 * eadm_subchannel_sch_event - process subchannel event
304 * @sch: subchannel
305 * @process: non-zero if function is called in process context
306 *
307 * An unspecified event occurred for this subchannel. Adjust data according
308 * to the current operational state of the subchannel. Return zero when the
309 * event has been handled sufficiently or -EAGAIN when this function should
310 * be called again in process context.
311 */
312static int eadm_subchannel_sch_event(struct subchannel *sch, int process)
313{
314 struct eadm_private *private;
315 unsigned long flags;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200316
317 spin_lock_irqsave(sch->lock, flags);
318 if (!device_is_registered(&sch->dev))
319 goto out_unlock;
320
321 if (work_pending(&sch->todo_work))
322 goto out_unlock;
323
324 if (cio_update_schib(sch)) {
325 css_sched_sch_todo(sch, SCH_TODO_UNREG);
326 goto out_unlock;
327 }
328 private = get_eadm_private(sch);
329 if (private->state == EADM_NOT_OPER)
330 private->state = EADM_IDLE;
331
332out_unlock:
333 spin_unlock_irqrestore(sch->lock, flags);
334
Peter Senna Tschudinb2f4de82015-08-04 17:11:47 +0200335 return 0;
Sebastian Otteadb86a2012-08-28 16:48:16 +0200336}
337
338static struct css_device_id eadm_subchannel_ids[] = {
339 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_ADM, },
340 { /* end of list */ },
341};
342MODULE_DEVICE_TABLE(css, eadm_subchannel_ids);
343
344static struct css_driver eadm_subchannel_driver = {
345 .drv = {
346 .name = "eadm_subchannel",
347 .owner = THIS_MODULE,
348 },
349 .subchannel_type = eadm_subchannel_ids,
350 .irq = eadm_subchannel_irq,
351 .probe = eadm_subchannel_probe,
352 .remove = eadm_subchannel_remove,
353 .shutdown = eadm_subchannel_shutdown,
354 .sch_event = eadm_subchannel_sch_event,
Sebastian Otteadb86a2012-08-28 16:48:16 +0200355};
356
Sebastian Otteadb86a2012-08-28 16:48:16 +0200357static int __init eadm_sch_init(void)
358{
359 int ret;
360
361 if (!css_general_characteristics.eadm)
362 return -ENXIO;
363
364 eadm_debug = debug_register("eadm_log", 16, 1, 16);
365 if (!eadm_debug)
366 return -ENOMEM;
367
368 debug_register_view(eadm_debug, &debug_hex_ascii_view);
369 debug_set_level(eadm_debug, 2);
370
371 isc_register(EADM_SCH_ISC);
372 ret = css_driver_register(&eadm_subchannel_driver);
373 if (ret)
374 goto cleanup;
375
Sebastian Otteadb86a2012-08-28 16:48:16 +0200376 return ret;
377
378cleanup:
379 isc_unregister(EADM_SCH_ISC);
380 debug_unregister(eadm_debug);
381 return ret;
382}
383
384static void __exit eadm_sch_exit(void)
385{
Sebastian Otteadb86a2012-08-28 16:48:16 +0200386 css_driver_unregister(&eadm_subchannel_driver);
387 isc_unregister(EADM_SCH_ISC);
388 debug_unregister(eadm_debug);
389}
390module_init(eadm_sch_init);
391module_exit(eadm_sch_exit);