[S390] cio: introduce parent-initiated device move
[deliverable/linux.git] / drivers / s390 / cio / device.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
1da177e4 4 *
c820de39 5 * Copyright IBM Corp. 2002,2008
1da177e4 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
4ce3b30c 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
90ab1336 19#include <linux/timer.h>
1da177e4
LT
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
4e57b681 23#include <asm/param.h> /* HZ */
1842f2b1 24#include <asm/cmb.h>
3a3fc29a 25#include <asm/isc.h>
1da177e4 26
0ae7a7b2 27#include "chp.h"
1da177e4 28#include "cio.h"
d7b5a4c9 29#include "cio_debug.h"
1da177e4
LT
30#include "css.h"
31#include "device.h"
32#include "ioasm.h"
cd6b4f27 33#include "io_sch.h"
ecf5d9ef 34#include "blacklist.h"
1da177e4 35
90ab1336 36static struct timer_list recovery_timer;
486d0a00 37static DEFINE_SPINLOCK(recovery_lock);
90ab1336
PO
38static int recovery_phase;
39static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
1da177e4
LT
41/******************* bus type handling ***********************/
42
43/* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
47static int
48ccw_bus_match (struct device * dev, struct device_driver * drv)
49{
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
53
54 if (!ids)
55 return 0;
56
57 found = ccw_device_id_match(ids, &cdev->id);
58 if (!found)
59 return 0;
60
61 cdev->id.driver_info = found->driver_info;
62
63 return 1;
64}
65
db0c2d59
PO
66/* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
cfbe9bb2 69static int snprint_alias(char *buf, size_t size,
db0c2d59 70 struct ccw_device_id *id, const char *suffix)
1da177e4 71{
db0c2d59 72 int len;
1da177e4 73
cfbe9bb2 74 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
db0c2d59
PO
75 if (len > size)
76 return len;
77 buf += len;
78 size -= len;
1da177e4 79
db0c2d59
PO
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
83 else
84 len += snprintf(buf, size, "dtdm%s", suffix);
1da177e4 85
db0c2d59
PO
86 return len;
87}
88
89/* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
7eff2e7a 91static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
db0c2d59
PO
92{
93 struct ccw_device *cdev = to_ccwdev(dev);
94 struct ccw_device_id *id = &(cdev->id);
cfbe9bb2
CH
95 int ret;
96 char modalias_buf[30];
1da177e4 97
db0c2d59 98 /* CU_TYPE= */
7eff2e7a 99 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
cfbe9bb2
CH
100 if (ret)
101 return ret;
db0c2d59
PO
102
103 /* CU_MODEL= */
7eff2e7a 104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
cfbe9bb2
CH
105 if (ret)
106 return ret;
1da177e4
LT
107
108 /* The next two can be zero, that's ok for us */
db0c2d59 109 /* DEV_TYPE= */
7eff2e7a 110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
cfbe9bb2
CH
111 if (ret)
112 return ret;
1da177e4 113
db0c2d59 114 /* DEV_MODEL= */
7eff2e7a 115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
cfbe9bb2
CH
116 if (ret)
117 return ret;
db0c2d59
PO
118
119 /* MODALIAS= */
cfbe9bb2 120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
7eff2e7a
KS
121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122 return ret;
1da177e4
LT
123}
124
8bbace7e 125struct bus_type ccw_bus_type;
1da177e4 126
602b20f2
CH
127static void io_subchannel_irq(struct subchannel *);
128static int io_subchannel_probe(struct subchannel *);
129static int io_subchannel_remove(struct subchannel *);
8bbace7e 130static void io_subchannel_shutdown(struct subchannel *);
c820de39 131static int io_subchannel_sch_event(struct subchannel *, int);
99611f87
CH
132static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133 int);
8ea7f559
SO
134static void recovery_func(unsigned long data);
135struct workqueue_struct *ccw_device_work;
136wait_queue_head_t ccw_device_init_wq;
137atomic_t ccw_device_init_count;
1da177e4 138
f08adc00
CH
139static struct css_device_id io_subchannel_ids[] = {
140 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
141 { /* end of list */ },
142};
143MODULE_DEVICE_TABLE(css, io_subchannel_ids);
144
93a27592
CH
145static int io_subchannel_prepare(struct subchannel *sch)
146{
147 struct ccw_device *cdev;
148 /*
149 * Don't allow suspend while a ccw device registration
150 * is still outstanding.
151 */
152 cdev = sch_get_cdev(sch);
153 if (cdev && !device_is_registered(&cdev->dev))
154 return -EAGAIN;
155 return 0;
156}
157
8ea7f559
SO
158static void io_subchannel_settle(void)
159{
160 wait_event(ccw_device_init_wq,
161 atomic_read(&ccw_device_init_count) == 0);
162 flush_workqueue(ccw_device_work);
163}
164
f7e5d67c 165static struct css_driver io_subchannel_driver = {
4beee646 166 .owner = THIS_MODULE,
f08adc00 167 .subchannel_type = io_subchannel_ids,
25b7bb58 168 .name = "io_subchannel",
1da177e4 169 .irq = io_subchannel_irq,
c820de39
CH
170 .sch_event = io_subchannel_sch_event,
171 .chp_event = io_subchannel_chp_event,
8bbace7e
CH
172 .probe = io_subchannel_probe,
173 .remove = io_subchannel_remove,
174 .shutdown = io_subchannel_shutdown,
93a27592 175 .prepare = io_subchannel_prepare,
8ea7f559 176 .settle = io_subchannel_settle,
1da177e4
LT
177};
178
2f17644d 179int __init io_subchannel_init(void)
1da177e4
LT
180{
181 int ret;
182
183 init_waitqueue_head(&ccw_device_init_wq);
184 atomic_set(&ccw_device_init_count, 0);
90ab1336 185 setup_timer(&recovery_timer, recovery_func, 0);
1da177e4
LT
186
187 ccw_device_work = create_singlethread_workqueue("cio");
188 if (!ccw_device_work)
2f17644d 189 return -ENOMEM;
1da177e4
LT
190 slow_path_wq = create_singlethread_workqueue("kslowcrw");
191 if (!slow_path_wq) {
2f17644d 192 ret = -ENOMEM;
1da177e4
LT
193 goto out_err;
194 }
195 if ((ret = bus_register (&ccw_bus_type)))
196 goto out_err;
197
25b7bb58
CH
198 ret = css_driver_register(&io_subchannel_driver);
199 if (ret)
1da177e4
LT
200 goto out_err;
201
1da177e4
LT
202 return 0;
203out_err:
204 if (ccw_device_work)
205 destroy_workqueue(ccw_device_work);
1da177e4
LT
206 if (slow_path_wq)
207 destroy_workqueue(slow_path_wq);
208 return ret;
209}
210
1da177e4
LT
211
212/************************ device handling **************************/
213
214/*
215 * A ccw_device has some interfaces in sysfs in addition to the
216 * standard ones.
217 * The following entries are designed to export the information which
218 * resided in 2.4 in /proc/subchannels. Subchannel and device number
219 * are obvious, so they don't have an entry :)
220 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
221 */
222static ssize_t
3fd3c0a5 223chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
224{
225 struct subchannel *sch = to_subchannel(dev);
7ad6a249 226 struct chsc_ssd_info *ssd = &sch->ssd_info;
1da177e4
LT
227 ssize_t ret = 0;
228 int chp;
7ad6a249
PO
229 int mask;
230
231 for (chp = 0; chp < 8; chp++) {
232 mask = 0x80 >> chp;
233 if (ssd->path_mask & mask)
234 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
235 else
236 ret += sprintf(buf + ret, "00 ");
237 }
1da177e4
LT
238 ret += sprintf (buf+ret, "\n");
239 return min((ssize_t)PAGE_SIZE, ret);
240}
241
242static ssize_t
3fd3c0a5 243pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
244{
245 struct subchannel *sch = to_subchannel(dev);
246 struct pmcw *pmcw = &sch->schib.pmcw;
247
248 return sprintf (buf, "%02x %02x %02x\n",
249 pmcw->pim, pmcw->pam, pmcw->pom);
250}
251
252static ssize_t
3fd3c0a5 253devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
254{
255 struct ccw_device *cdev = to_ccwdev(dev);
256 struct ccw_device_id *id = &(cdev->id);
257
258 if (id->dev_type != 0)
259 return sprintf(buf, "%04x/%02x\n",
260 id->dev_type, id->dev_model);
261 else
262 return sprintf(buf, "n/a\n");
263}
264
265static ssize_t
3fd3c0a5 266cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
267{
268 struct ccw_device *cdev = to_ccwdev(dev);
269 struct ccw_device_id *id = &(cdev->id);
270
271 return sprintf(buf, "%04x/%02x\n",
272 id->cu_type, id->cu_model);
273}
274
f1fc78a8
BB
275static ssize_t
276modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
277{
278 struct ccw_device *cdev = to_ccwdev(dev);
279 struct ccw_device_id *id = &(cdev->id);
db0c2d59 280 int len;
f1fc78a8 281
086a6c62 282 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
db0c2d59
PO
283
284 return len > PAGE_SIZE ? PAGE_SIZE : len;
f1fc78a8
BB
285}
286
1da177e4 287static ssize_t
3fd3c0a5 288online_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
289{
290 struct ccw_device *cdev = to_ccwdev(dev);
291
292 return sprintf(buf, cdev->online ? "1\n" : "0\n");
293}
294
d7b5a4c9
CH
295int ccw_device_is_orphan(struct ccw_device *cdev)
296{
297 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
298}
299
ef99516c 300static void ccw_device_unregister(struct ccw_device *cdev)
7674da77 301{
3b554a14 302 if (test_and_clear_bit(1, &cdev->private->registered)) {
ef99516c 303 device_del(&cdev->dev);
3b554a14
SO
304 /* Release reference from device_initialize(). */
305 put_device(&cdev->dev);
306 }
7674da77
CH
307}
308
46fbe4e4 309static void ccw_device_remove_orphan_cb(struct work_struct *work)
59a8a6e2 310{
46fbe4e4
PO
311 struct ccw_device_private *priv;
312 struct ccw_device *cdev;
59a8a6e2 313
46fbe4e4
PO
314 priv = container_of(work, struct ccw_device_private, kick_work);
315 cdev = priv->cdev;
59a8a6e2 316 ccw_device_unregister(cdev);
46fbe4e4
PO
317 /* Release cdev reference for workqueue processing. */
318 put_device(&cdev->dev);
59a8a6e2
CH
319}
320
1da177e4
LT
321static void
322ccw_device_remove_disconnected(struct ccw_device *cdev)
323{
d7b5a4c9 324 unsigned long flags;
59a8a6e2 325
1da177e4
LT
326 /*
327 * Forced offline in disconnected state means
328 * 'throw away device'.
329 */
d7b5a4c9 330 if (ccw_device_is_orphan(cdev)) {
59a8a6e2
CH
331 /*
332 * Deregister ccw device.
333 * Unfortunately, we cannot do this directly from the
334 * attribute method.
335 */
be7a2ddc
SO
336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
338 return;
d7b5a4c9
CH
339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
46fbe4e4
PO
342 PREPARE_WORK(&cdev->private->kick_work,
343 ccw_device_remove_orphan_cb);
c4621a62 344 queue_work(slow_path_wq, &cdev->private->kick_work);
46fbe4e4
PO
345 } else
346 /* Deregister subchannel, which will kill the ccw device. */
c4621a62 347 ccw_device_schedule_sch_unregister(cdev);
1da177e4
LT
348}
349
b2ffd8e9
CH
350/**
351 * ccw_device_set_offline() - disable a ccw device for I/O
352 * @cdev: target ccw device
353 *
354 * This function calls the driver's set_offline() function for @cdev, if
355 * given, and then disables @cdev.
356 * Returns:
357 * %0 on success and a negative error value on failure.
358 * Context:
359 * enabled, ccw device lock not held
360 */
361int ccw_device_set_offline(struct ccw_device *cdev)
1da177e4
LT
362{
363 int ret;
364
365 if (!cdev)
366 return -ENODEV;
367 if (!cdev->online || !cdev->drv)
368 return -EINVAL;
369
370 if (cdev->drv->set_offline) {
371 ret = cdev->drv->set_offline(cdev);
372 if (ret != 0)
373 return ret;
374 }
375 cdev->online = 0;
376 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
377 /* Wait until a final state or DISCONNECTED is reached */
378 while (!dev_fsm_final_state(cdev) &&
379 cdev->private->state != DEV_STATE_DISCONNECTED) {
1da177e4 380 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
381 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
382 cdev->private->state == DEV_STATE_DISCONNECTED));
383 spin_lock_irq(cdev->ccwlock);
1da177e4 384 }
217ee6c6
ME
385 ret = ccw_device_offline(cdev);
386 if (ret)
387 goto error;
1da177e4 388 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
389 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
390 cdev->private->state == DEV_STATE_DISCONNECTED));
391 /* Give up reference from ccw_device_set_online(). */
392 put_device(&cdev->dev);
393 return 0;
394
395error:
396 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
397 ret, cdev->private->dev_id.ssid,
398 cdev->private->dev_id.devno);
399 cdev->private->state = DEV_STATE_OFFLINE;
400 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
401 spin_unlock_irq(cdev->ccwlock);
402 /* Give up reference from ccw_device_set_online(). */
403 put_device(&cdev->dev);
404 return -ENODEV;
1da177e4
LT
405}
406
b2ffd8e9
CH
407/**
408 * ccw_device_set_online() - enable a ccw device for I/O
409 * @cdev: target ccw device
410 *
411 * This function first enables @cdev and then calls the driver's set_online()
412 * function for @cdev, if given. If set_online() returns an error, @cdev is
413 * disabled again.
414 * Returns:
415 * %0 on success and a negative error value on failure.
416 * Context:
417 * enabled, ccw device lock not held
418 */
419int ccw_device_set_online(struct ccw_device *cdev)
1da177e4
LT
420{
421 int ret;
217ee6c6 422 int ret2;
1da177e4
LT
423
424 if (!cdev)
425 return -ENODEV;
426 if (cdev->online || !cdev->drv)
427 return -EINVAL;
9cd67421
CH
428 /* Hold on to an extra reference while device is online. */
429 if (!get_device(&cdev->dev))
430 return -ENODEV;
1da177e4
LT
431
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_online(cdev);
434 spin_unlock_irq(cdev->ccwlock);
435 if (ret == 0)
436 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
437 else {
139b83dd 438 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
e556bbbd
CH
439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
9cd67421
CH
442 /* Give up online reference since onlining failed. */
443 put_device(&cdev->dev);
1da177e4
LT
444 return ret;
445 }
217ee6c6
ME
446 spin_lock_irq(cdev->ccwlock);
447 /* Check if online processing was successful */
448 if ((cdev->private->state != DEV_STATE_ONLINE) &&
449 (cdev->private->state != DEV_STATE_W4SENSE)) {
450 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
451 /* Give up online reference since onlining failed. */
452 put_device(&cdev->dev);
1da177e4 453 return -ENODEV;
9cd67421 454 }
217ee6c6
ME
455 spin_unlock_irq(cdev->ccwlock);
456 if (cdev->drv->set_online)
457 ret = cdev->drv->set_online(cdev);
458 if (ret)
459 goto rollback;
460 cdev->online = 1;
461 return 0;
462
463rollback:
1da177e4 464 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
465 /* Wait until a final state or DISCONNECTED is reached */
466 while (!dev_fsm_final_state(cdev) &&
467 cdev->private->state != DEV_STATE_DISCONNECTED) {
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 cdev->private->state == DEV_STATE_DISCONNECTED));
471 spin_lock_irq(cdev->ccwlock);
472 }
473 ret2 = ccw_device_offline(cdev);
474 if (ret2)
475 goto error;
476 spin_unlock_irq(cdev->ccwlock);
477 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
478 cdev->private->state == DEV_STATE_DISCONNECTED));
479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
481 return ret;
482
483error:
484 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
485 "device 0.%x.%04x\n",
486 ret2, cdev->private->dev_id.ssid,
487 cdev->private->dev_id.devno);
488 cdev->private->state = DEV_STATE_OFFLINE;
1da177e4 489 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
490 /* Give up online reference since onlining failed. */
491 put_device(&cdev->dev);
217ee6c6 492 return ret;
1da177e4
LT
493}
494
e74fe0ce 495static int online_store_handle_offline(struct ccw_device *cdev)
f5ba6c86
CH
496{
497 if (cdev->private->state == DEV_STATE_DISCONNECTED)
498 ccw_device_remove_disconnected(cdev);
e74fe0ce
SO
499 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
500 return ccw_device_set_offline(cdev);
501 return 0;
f5ba6c86
CH
502}
503
504static int online_store_recog_and_online(struct ccw_device *cdev)
505{
506 int ret;
507
508 /* Do device recognition, if needed. */
99f6a570 509 if (cdev->private->state == DEV_STATE_BOXED) {
f5ba6c86
CH
510 ret = ccw_device_recognition(cdev);
511 if (ret) {
e556bbbd
CH
512 CIO_MSG_EVENT(0, "Couldn't start recognition "
513 "for device 0.%x.%04x (ret=%d)\n",
514 cdev->private->dev_id.ssid,
515 cdev->private->dev_id.devno, ret);
f5ba6c86
CH
516 return ret;
517 }
518 wait_event(cdev->private->wait_q,
519 cdev->private->flags.recog_done);
156013ff
SO
520 if (cdev->private->state != DEV_STATE_OFFLINE)
521 /* recognition failed */
522 return -EAGAIN;
f5ba6c86
CH
523 }
524 if (cdev->drv && cdev->drv->set_online)
525 ccw_device_set_online(cdev);
526 return 0;
527}
156013ff 528
c78aa6cb 529static int online_store_handle_online(struct ccw_device *cdev, int force)
f5ba6c86
CH
530{
531 int ret;
532
533 ret = online_store_recog_and_online(cdev);
156013ff 534 if (ret && !force)
c78aa6cb 535 return ret;
f5ba6c86
CH
536 if (force && cdev->private->state == DEV_STATE_BOXED) {
537 ret = ccw_device_stlck(cdev);
c78aa6cb
ME
538 if (ret)
539 return ret;
f5ba6c86
CH
540 if (cdev->id.cu_type == 0)
541 cdev->private->state = DEV_STATE_NOT_OPER;
156013ff
SO
542 ret = online_store_recog_and_online(cdev);
543 if (ret)
544 return ret;
f5ba6c86 545 }
c78aa6cb 546 return 0;
f5ba6c86
CH
547}
548
549static ssize_t online_store (struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
1da177e4
LT
551{
552 struct ccw_device *cdev = to_ccwdev(dev);
2f972202
CH
553 int force, ret;
554 unsigned long i;
1da177e4 555
b5cd99e6
SO
556 if ((cdev->private->state != DEV_STATE_OFFLINE &&
557 cdev->private->state != DEV_STATE_ONLINE &&
558 cdev->private->state != DEV_STATE_BOXED &&
559 cdev->private->state != DEV_STATE_DISCONNECTED) ||
560 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4
LT
561 return -EAGAIN;
562
563 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
564 atomic_set(&cdev->private->onoff, 0);
565 return -EINVAL;
566 }
567 if (!strncmp(buf, "force\n", count)) {
568 force = 1;
569 i = 1;
2f972202 570 ret = 0;
1da177e4
LT
571 } else {
572 force = 0;
2f972202 573 ret = strict_strtoul(buf, 16, &i);
1da177e4 574 }
2f972202
CH
575 if (ret)
576 goto out;
f5ba6c86
CH
577 switch (i) {
578 case 0:
e74fe0ce 579 ret = online_store_handle_offline(cdev);
f5ba6c86
CH
580 break;
581 case 1:
c78aa6cb 582 ret = online_store_handle_online(cdev, force);
f5ba6c86
CH
583 break;
584 default:
2f972202 585 ret = -EINVAL;
1da177e4 586 }
2f972202 587out:
1da177e4
LT
588 if (cdev->drv)
589 module_put(cdev->drv->owner);
590 atomic_set(&cdev->private->onoff, 0);
e74fe0ce 591 return (ret < 0) ? ret : count;
1da177e4
LT
592}
593
594static ssize_t
3fd3c0a5 595available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
596{
597 struct ccw_device *cdev = to_ccwdev(dev);
598 struct subchannel *sch;
599
d7b5a4c9
CH
600 if (ccw_device_is_orphan(cdev))
601 return sprintf(buf, "no device\n");
1da177e4
LT
602 switch (cdev->private->state) {
603 case DEV_STATE_BOXED:
604 return sprintf(buf, "boxed\n");
605 case DEV_STATE_DISCONNECTED:
606 case DEV_STATE_DISCONNECTED_SENSE_ID:
607 case DEV_STATE_NOT_OPER:
608 sch = to_subchannel(dev->parent);
609 if (!sch->lpm)
610 return sprintf(buf, "no path\n");
611 else
612 return sprintf(buf, "no device\n");
613 default:
614 /* All other states considered fine. */
615 return sprintf(buf, "good\n");
616 }
617}
618
619static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
620static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
621static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
622static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
f1fc78a8 623static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
1da177e4 624static DEVICE_ATTR(online, 0644, online_show, online_store);
1da177e4
LT
625static DEVICE_ATTR(availability, 0444, available_show, NULL);
626
7e9db9ea 627static struct attribute *io_subchannel_attrs[] = {
1da177e4
LT
628 &dev_attr_chpids.attr,
629 &dev_attr_pimpampom.attr,
630 NULL,
631};
632
7e9db9ea
CH
633static struct attribute_group io_subchannel_attr_group = {
634 .attrs = io_subchannel_attrs,
529192f3 635};
1da177e4
LT
636
637static struct attribute * ccwdev_attrs[] = {
638 &dev_attr_devtype.attr,
639 &dev_attr_cutype.attr,
f1fc78a8 640 &dev_attr_modalias.attr,
1da177e4
LT
641 &dev_attr_online.attr,
642 &dev_attr_cmb_enable.attr,
643 &dev_attr_availability.attr,
644 NULL,
645};
646
647static struct attribute_group ccwdev_attr_group = {
648 .attrs = ccwdev_attrs,
649};
650
a4dbd674 651static const struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
652 &ccwdev_attr_group,
653 NULL,
654};
1da177e4
LT
655
656/* this is a simple abstraction for device_register that sets the
657 * correct bus type and adds the bus specific files */
3c9da7ba 658static int ccw_device_register(struct ccw_device *cdev)
1da177e4
LT
659{
660 struct device *dev = &cdev->dev;
661 int ret;
662
663 dev->bus = &ccw_bus_type;
3ac276f8
SO
664 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
665 cdev->private->dev_id.devno);
666 if (ret)
667 return ret;
668 ret = device_add(dev);
669 if (ret)
1da177e4
LT
670 return ret;
671
672 set_bit(1, &cdev->private->registered);
1da177e4
LT
673 return ret;
674}
675
5d6e6b6f 676static int match_dev_id(struct device *dev, void *data)
b0744bd2 677{
5d6e6b6f
PO
678 struct ccw_device *cdev = to_ccwdev(dev);
679 struct ccw_dev_id *dev_id = data;
d7b5a4c9 680
d7b5a4c9
CH
681 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
682}
683
5d6e6b6f 684static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
d7b5a4c9
CH
685{
686 struct device *dev;
687
5d6e6b6f 688 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
d7b5a4c9
CH
689
690 return dev ? to_ccwdev(dev) : NULL;
691}
692
eb32ae8d 693void ccw_device_do_unbind_bind(struct work_struct *work)
1da177e4 694{
c1637532 695 struct ccw_device_private *priv;
1da177e4
LT
696 struct ccw_device *cdev;
697 struct subchannel *sch;
eb32ae8d 698 int ret;
1da177e4 699
c1637532
MS
700 priv = container_of(work, struct ccw_device_private, kick_work);
701 cdev = priv->cdev;
1da177e4 702 sch = to_subchannel(cdev->dev.parent);
d7b5a4c9 703
eb32ae8d
CH
704 if (test_bit(1, &cdev->private->registered)) {
705 device_release_driver(&cdev->dev);
706 ret = device_attach(&cdev->dev);
707 WARN_ON(ret == -ENODEV);
708 }
1da177e4
LT
709}
710
711static void
712ccw_device_release(struct device *dev)
713{
714 struct ccw_device *cdev;
715
716 cdev = to_ccwdev(dev);
6eff208f
CH
717 /* Release reference of parent subchannel. */
718 put_device(cdev->dev.parent);
1da177e4
LT
719 kfree(cdev->private);
720 kfree(cdev);
721}
722
7674da77
CH
723static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
724{
725 struct ccw_device *cdev;
726
727 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
728 if (cdev) {
729 cdev->private = kzalloc(sizeof(struct ccw_device_private),
730 GFP_KERNEL | GFP_DMA);
731 if (cdev->private)
732 return cdev;
733 }
734 kfree(cdev);
735 return ERR_PTR(-ENOMEM);
736}
737
738static int io_subchannel_initialize_dev(struct subchannel *sch,
739 struct ccw_device *cdev)
740{
741 cdev->private->cdev = cdev;
742 atomic_set(&cdev->private->onoff, 0);
743 cdev->dev.parent = &sch->dev;
744 cdev->dev.release = ccw_device_release;
33583c36 745 INIT_WORK(&cdev->private->kick_work, NULL);
ef99516c 746 cdev->dev.groups = ccwdev_attr_groups;
7674da77
CH
747 /* Do first half of device_register. */
748 device_initialize(&cdev->dev);
749 if (!get_device(&sch->dev)) {
283fdd0b
CH
750 /* Release reference from device_initialize(). */
751 put_device(&cdev->dev);
7674da77
CH
752 return -ENODEV;
753 }
754 return 0;
755}
756
757static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
758{
759 struct ccw_device *cdev;
760 int ret;
761
762 cdev = io_subchannel_allocate_dev(sch);
763 if (!IS_ERR(cdev)) {
764 ret = io_subchannel_initialize_dev(sch, cdev);
06739a8a 765 if (ret)
7674da77 766 cdev = ERR_PTR(ret);
7674da77
CH
767 }
768 return cdev;
769}
770
d7b5a4c9
CH
771static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
772
d7b5a4c9
CH
773static void sch_create_and_recog_new_device(struct subchannel *sch)
774{
775 struct ccw_device *cdev;
776
777 /* Need to allocate a new ccw device. */
778 cdev = io_subchannel_create_ccwdev(sch);
779 if (IS_ERR(cdev)) {
780 /* OK, we did everything we could... */
781 css_sch_device_unregister(sch);
782 return;
783 }
d7b5a4c9
CH
784 /* Start recognition for the new ccw device. */
785 if (io_subchannel_recog(cdev, sch)) {
786 spin_lock_irq(sch->lock);
db6a6423 787 sch_set_cdev(sch, NULL);
d7b5a4c9 788 spin_unlock_irq(sch->lock);
d7b5a4c9 789 css_sch_device_unregister(sch);
6eff208f
CH
790 /* Put reference from io_subchannel_create_ccwdev(). */
791 put_device(&sch->dev);
792 /* Give up initial reference. */
793 put_device(&cdev->dev);
d7b5a4c9
CH
794 }
795}
796
1da177e4
LT
797/*
798 * Register recognized device.
799 */
800static void
c1637532 801io_subchannel_register(struct work_struct *work)
1da177e4 802{
c1637532 803 struct ccw_device_private *priv;
1da177e4
LT
804 struct ccw_device *cdev;
805 struct subchannel *sch;
806 int ret;
807 unsigned long flags;
808
c1637532
MS
809 priv = container_of(work, struct ccw_device_private, kick_work);
810 cdev = priv->cdev;
1da177e4 811 sch = to_subchannel(cdev->dev.parent);
5fb6b854
CH
812 /*
813 * Check if subchannel is still registered. It may have become
814 * unregistered if a machine check hit us after finishing
815 * device recognition but before the register work could be
816 * queued.
817 */
818 if (!device_is_registered(&sch->dev))
819 goto out_err;
82b7ac05 820 css_update_ssd_info(sch);
47af5518
CH
821 /*
822 * io_subchannel_register() will also be called after device
823 * recognition has been done for a boxed device (which will already
824 * be registered). We need to reprobe since we may now have sense id
825 * information.
826 */
d6a30761 827 if (device_is_registered(&cdev->dev)) {
47af5518
CH
828 if (!cdev->drv) {
829 ret = device_reprobe(&cdev->dev);
830 if (ret)
831 /* We can't do much here. */
139b83dd 832 CIO_MSG_EVENT(0, "device_reprobe() returned"
e556bbbd
CH
833 " %d for 0.%x.%04x\n", ret,
834 cdev->private->dev_id.ssid,
835 cdev->private->dev_id.devno);
47af5518 836 }
1da177e4
LT
837 goto out;
838 }
fa1a8c23
CH
839 /*
840 * Now we know this subchannel will stay, we can throw
841 * our delayed uevent.
842 */
f67f129e 843 dev_set_uevent_suppress(&sch->dev, 0);
fa1a8c23 844 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1da177e4
LT
845 /* make it known to the system */
846 ret = ccw_device_register(cdev);
847 if (ret) {
e556bbbd
CH
848 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
849 cdev->private->dev_id.ssid,
850 cdev->private->dev_id.devno, ret);
2ec22984 851 spin_lock_irqsave(sch->lock, flags);
db6a6423 852 sch_set_cdev(sch, NULL);
2ec22984 853 spin_unlock_irqrestore(sch->lock, flags);
6eff208f
CH
854 /* Release initial device reference. */
855 put_device(&cdev->dev);
5fb6b854 856 goto out_err;
1da177e4 857 }
1da177e4
LT
858out:
859 cdev->private->flags.recog_done = 1;
1da177e4 860 wake_up(&cdev->private->wait_q);
5fb6b854
CH
861out_err:
862 /* Release reference for workqueue processing. */
863 put_device(&cdev->dev);
1da177e4
LT
864 if (atomic_dec_and_test(&ccw_device_init_count))
865 wake_up(&ccw_device_init_wq);
866}
867
3f4cf6e7 868static void ccw_device_call_sch_unregister(struct work_struct *work)
1da177e4 869{
c1637532
MS
870 struct ccw_device_private *priv;
871 struct ccw_device *cdev;
1da177e4
LT
872 struct subchannel *sch;
873
c1637532
MS
874 priv = container_of(work, struct ccw_device_private, kick_work);
875 cdev = priv->cdev;
46fbe4e4
PO
876 /* Get subchannel reference for local processing. */
877 if (!get_device(cdev->dev.parent))
878 return;
1da177e4 879 sch = to_subchannel(cdev->dev.parent);
6ab4879a 880 css_sch_device_unregister(sch);
46fbe4e4 881 /* Release cdev reference for workqueue processing.*/
1da177e4 882 put_device(&cdev->dev);
46fbe4e4 883 /* Release subchannel reference for local processing. */
1da177e4
LT
884 put_device(&sch->dev);
885}
886
c4621a62
SO
887void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
888{
be7a2ddc
SO
889 /* Get cdev reference for workqueue processing. */
890 if (!get_device(&cdev->dev))
891 return;
c4621a62
SO
892 PREPARE_WORK(&cdev->private->kick_work,
893 ccw_device_call_sch_unregister);
894 queue_work(slow_path_wq, &cdev->private->kick_work);
895}
896
1da177e4
LT
897/*
898 * subchannel recognition done. Called from the state machine.
899 */
900void
901io_subchannel_recog_done(struct ccw_device *cdev)
902{
1da177e4
LT
903 if (css_init_done == 0) {
904 cdev->private->flags.recog_done = 1;
905 return;
906 }
907 switch (cdev->private->state) {
47593bfa
SO
908 case DEV_STATE_BOXED:
909 /* Device did not respond in time. */
1da177e4
LT
910 case DEV_STATE_NOT_OPER:
911 cdev->private->flags.recog_done = 1;
c4621a62 912 ccw_device_schedule_sch_unregister(cdev);
1da177e4
LT
913 if (atomic_dec_and_test(&ccw_device_init_count))
914 wake_up(&ccw_device_init_wq);
915 break;
1da177e4
LT
916 case DEV_STATE_OFFLINE:
917 /*
918 * We can't register the device in interrupt context so
919 * we schedule a work item.
920 */
921 if (!get_device(&cdev->dev))
922 break;
923 PREPARE_WORK(&cdev->private->kick_work,
c1637532 924 io_subchannel_register);
1da177e4
LT
925 queue_work(slow_path_wq, &cdev->private->kick_work);
926 break;
927 }
928}
929
930static int
931io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
932{
933 int rc;
934 struct ccw_device_private *priv;
935
2ec22984 936 cdev->ccwlock = sch->lock;
fb6958a5 937
1da177e4
LT
938 /* Init private data. */
939 priv = cdev->private;
78964268
CH
940 priv->dev_id.devno = sch->schib.pmcw.dev;
941 priv->dev_id.ssid = sch->schid.ssid;
942 priv->schid = sch->schid;
1da177e4
LT
943 priv->state = DEV_STATE_NOT_OPER;
944 INIT_LIST_HEAD(&priv->cmb_list);
945 init_waitqueue_head(&priv->wait_q);
946 init_timer(&priv->timer);
947
1da177e4
LT
948 /* Increase counter of devices currently in recognition. */
949 atomic_inc(&ccw_device_init_count);
950
951 /* Start async. device sensing. */
2ec22984 952 spin_lock_irq(sch->lock);
60e4dac1 953 sch_set_cdev(sch, cdev);
1da177e4 954 rc = ccw_device_recognition(cdev);
2ec22984 955 spin_unlock_irq(sch->lock);
1da177e4
LT
956 if (rc) {
957 if (atomic_dec_and_test(&ccw_device_init_count))
958 wake_up(&ccw_device_init_wq);
959 }
960 return rc;
961}
962
5d6e6b6f
PO
963static int ccw_device_move_to_sch(struct ccw_device *cdev,
964 struct subchannel *sch)
d7b5a4c9 965{
5d6e6b6f 966 struct subchannel *old_sch;
d7b5a4c9 967 int rc;
d7b5a4c9 968
5d6e6b6f
PO
969 old_sch = to_subchannel(cdev->dev.parent);
970 /* Obtain child reference for new parent. */
6eff208f 971 if (!get_device(&sch->dev))
5d6e6b6f 972 return -ENODEV;
d7b5a4c9 973 mutex_lock(&sch->reg_mutex);
ffa6a705 974 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9
CH
975 mutex_unlock(&sch->reg_mutex);
976 if (rc) {
5d6e6b6f 977 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
d7b5a4c9
CH
978 cdev->private->dev_id.ssid,
979 cdev->private->dev_id.devno, sch->schid.ssid,
5d6e6b6f
PO
980 sch->schib.pmcw.dev, rc);
981 /* Release child reference for new parent. */
6eff208f 982 put_device(&sch->dev);
5d6e6b6f 983 return rc;
d7b5a4c9 984 }
5d6e6b6f
PO
985 /* Clean up old subchannel. */
986 if (!sch_is_pseudo_sch(old_sch)) {
987 spin_lock_irq(old_sch->lock);
988 sch_set_cdev(old_sch, NULL);
989 cio_disable_subchannel(old_sch);
990 spin_unlock_irq(old_sch->lock);
991 css_schedule_eval(old_sch->schid);
d7b5a4c9 992 }
5d6e6b6f
PO
993 /* Release child reference for old parent. */
994 put_device(&old_sch->dev);
995 /* Initialize new subchannel. */
996 spin_lock_irq(sch->lock);
997 cdev->private->schid = sch->schid;
998 cdev->ccwlock = sch->lock;
999 if (!sch_is_pseudo_sch(sch))
1000 sch_set_cdev(sch, cdev);
1001 spin_unlock_irq(sch->lock);
1002 if (!sch_is_pseudo_sch(sch))
1003 css_update_ssd_info(sch);
1004 return 0;
1005}
1006
1007static int ccw_device_move_to_orph(struct ccw_device *cdev)
1008{
1009 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1010 struct channel_subsystem *css = to_css(sch->dev.parent);
1011
1012 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
d7b5a4c9
CH
1013}
1014
602b20f2
CH
1015static void io_subchannel_irq(struct subchannel *sch)
1016{
1017 struct ccw_device *cdev;
1018
db6a6423 1019 cdev = sch_get_cdev(sch);
602b20f2 1020
efd986db
SO
1021 CIO_TRACE_EVENT(6, "IRQ");
1022 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
602b20f2
CH
1023 if (cdev)
1024 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1025}
1026
13952ec1
SO
1027void io_subchannel_init_config(struct subchannel *sch)
1028{
1029 memset(&sch->config, 0, sizeof(sch->config));
1030 sch->config.csense = 1;
d36f0c66
SO
1031 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1032 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
13952ec1
SO
1033 sch->config.mp = 1;
1034}
1035
0ae7a7b2
CH
1036static void io_subchannel_init_fields(struct subchannel *sch)
1037{
1038 if (cio_is_console(sch->schid))
1039 sch->opm = 0xff;
1040 else
1041 sch->opm = chp_get_sch_opm(sch);
1042 sch->lpm = sch->schib.pmcw.pam & sch->opm;
3a3fc29a 1043 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
0ae7a7b2
CH
1044
1045 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1046 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1047 sch->schib.pmcw.dev, sch->schid.ssid,
1048 sch->schid.sch_no, sch->schib.pmcw.pim,
1049 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
13952ec1
SO
1050
1051 io_subchannel_init_config(sch);
0ae7a7b2
CH
1052}
1053
90ed2b69
CH
1054static void io_subchannel_do_unreg(struct work_struct *work)
1055{
1056 struct subchannel *sch;
1057
1058 sch = container_of(work, struct subchannel, work);
1059 css_sch_device_unregister(sch);
90ed2b69
CH
1060 put_device(&sch->dev);
1061}
1062
1063/* Schedule unregister if we have no cdev. */
1064static void io_subchannel_schedule_removal(struct subchannel *sch)
1065{
1066 get_device(&sch->dev);
1067 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1068 queue_work(slow_path_wq, &sch->work);
1069}
1070
1071/*
1072 * Note: We always return 0 so that we bind to the device even on error.
1073 * This is needed so that our remove function is called on unregister.
1074 */
0ae7a7b2 1075static int io_subchannel_probe(struct subchannel *sch)
1da177e4 1076{
1da177e4
LT
1077 struct ccw_device *cdev;
1078 int rc;
1da177e4 1079
6d7c5afc 1080 if (cio_is_console(sch->schid)) {
7e9db9ea
CH
1081 rc = sysfs_create_group(&sch->dev.kobj,
1082 &io_subchannel_attr_group);
1083 if (rc)
1084 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1085 "attributes for subchannel "
1086 "0.%x.%04x (rc=%d)\n",
1087 sch->schid.ssid, sch->schid.sch_no, rc);
1da177e4 1088 /*
6d7c5afc 1089 * The console subchannel already has an associated ccw_device.
7e9db9ea 1090 * Throw the delayed uevent for the subchannel, register
6d7c5afc 1091 * the ccw_device and exit.
1da177e4 1092 */
f67f129e 1093 dev_set_uevent_suppress(&sch->dev, 0);
7e9db9ea 1094 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
6d7c5afc 1095 cdev = sch_get_cdev(sch);
e1031786 1096 cdev->dev.groups = ccwdev_attr_groups;
1da177e4
LT
1097 device_initialize(&cdev->dev);
1098 ccw_device_register(cdev);
1da177e4
LT
1099 /*
1100 * Check if the device is already online. If it is
9cd67421
CH
1101 * the reference count needs to be corrected since we
1102 * didn't obtain a reference in ccw_device_set_online.
1da177e4
LT
1103 */
1104 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1105 cdev->private->state != DEV_STATE_OFFLINE &&
1106 cdev->private->state != DEV_STATE_BOXED)
1107 get_device(&cdev->dev);
1108 return 0;
1109 }
0ae7a7b2 1110 io_subchannel_init_fields(sch);
f444cc0e
SO
1111 rc = cio_commit_config(sch);
1112 if (rc)
1113 goto out_schedule;
7e9db9ea
CH
1114 rc = sysfs_create_group(&sch->dev.kobj,
1115 &io_subchannel_attr_group);
1116 if (rc)
90ed2b69 1117 goto out_schedule;
cd6b4f27
CH
1118 /* Allocate I/O subchannel private data. */
1119 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1120 GFP_KERNEL | GFP_DMA);
90ed2b69 1121 if (!sch->private)
48e4c385 1122 goto out_schedule;
5d6e6b6f 1123 css_schedule_eval(sch->schid);
7e9db9ea 1124 return 0;
48e4c385 1125
90ed2b69
CH
1126out_schedule:
1127 io_subchannel_schedule_removal(sch);
1128 return 0;
1da177e4
LT
1129}
1130
1da177e4 1131static int
8bbace7e 1132io_subchannel_remove (struct subchannel *sch)
1da177e4
LT
1133{
1134 struct ccw_device *cdev;
1135 unsigned long flags;
1136
db6a6423
CH
1137 cdev = sch_get_cdev(sch);
1138 if (!cdev)
48e4c385 1139 goto out_free;
1da177e4
LT
1140 /* Set ccw device to not operational and drop reference. */
1141 spin_lock_irqsave(cdev->ccwlock, flags);
db6a6423 1142 sch_set_cdev(sch, NULL);
1da177e4
LT
1143 cdev->private->state = DEV_STATE_NOT_OPER;
1144 spin_unlock_irqrestore(cdev->ccwlock, flags);
ef99516c 1145 ccw_device_unregister(cdev);
48e4c385 1146out_free:
cd6b4f27 1147 kfree(sch->private);
7e9db9ea 1148 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1149 return 0;
1150}
1151
602b20f2 1152static void io_subchannel_verify(struct subchannel *sch)
1da177e4
LT
1153{
1154 struct ccw_device *cdev;
1155
db6a6423 1156 cdev = sch_get_cdev(sch);
1da177e4
LT
1157 if (cdev)
1158 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1159}
1160
c820de39 1161static int check_for_io_on_path(struct subchannel *sch, int mask)
1da177e4 1162{
cdb912a4 1163 if (cio_update_schib(sch))
c820de39 1164 return 0;
23d805b6 1165 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
c820de39
CH
1166 return 1;
1167 return 0;
1168}
1169
1170static void terminate_internal_io(struct subchannel *sch,
1171 struct ccw_device *cdev)
1172{
1173 if (cio_clear(sch)) {
1174 /* Recheck device in case clear failed. */
1175 sch->lpm = 0;
1176 if (cdev->online)
1177 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1178 else
1179 css_schedule_eval(sch->schid);
d23861ff 1180 return;
c820de39 1181 }
1da177e4 1182 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
c820de39
CH
1183 /* Request retry of internal operation. */
1184 cdev->private->flags.intretry = 1;
1185 /* Call handler. */
1da177e4
LT
1186 if (cdev->handler)
1187 cdev->handler(cdev, cdev->private->intparm,
1188 ERR_PTR(-EIO));
1189}
1190
c820de39
CH
1191static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1192{
1193 struct ccw_device *cdev;
1194
1195 cdev = sch_get_cdev(sch);
1196 if (!cdev)
1197 return;
1198 if (check_for_io_on_path(sch, mask)) {
1199 if (cdev->private->state == DEV_STATE_ONLINE)
1200 ccw_device_kill_io(cdev);
1201 else {
1202 terminate_internal_io(sch, cdev);
1203 /* Re-start path verification. */
1204 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1205 }
1206 } else
1207 /* trigger path verification. */
1208 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1209
1210}
1211
99611f87
CH
1212static int io_subchannel_chp_event(struct subchannel *sch,
1213 struct chp_link *link, int event)
c820de39
CH
1214{
1215 int mask;
c820de39 1216
99611f87 1217 mask = chp_ssd_get_mask(&sch->ssd_info, link);
c820de39
CH
1218 if (!mask)
1219 return 0;
1220 switch (event) {
1221 case CHP_VARY_OFF:
1222 sch->opm &= ~mask;
1223 sch->lpm &= ~mask;
1224 io_subchannel_terminate_path(sch, mask);
1225 break;
1226 case CHP_VARY_ON:
1227 sch->opm |= mask;
1228 sch->lpm |= mask;
1229 io_subchannel_verify(sch);
1230 break;
1231 case CHP_OFFLINE:
cdb912a4 1232 if (cio_update_schib(sch))
c820de39
CH
1233 return -ENODEV;
1234 io_subchannel_terminate_path(sch, mask);
1235 break;
1236 case CHP_ONLINE:
cdb912a4
SO
1237 if (cio_update_schib(sch))
1238 return -ENODEV;
c820de39
CH
1239 sch->lpm |= mask & sch->opm;
1240 io_subchannel_verify(sch);
1241 break;
1242 }
1243 return 0;
1244}
1245
1da177e4 1246static void
8bbace7e 1247io_subchannel_shutdown(struct subchannel *sch)
1da177e4 1248{
1da177e4
LT
1249 struct ccw_device *cdev;
1250 int ret;
1251
db6a6423 1252 cdev = sch_get_cdev(sch);
1da177e4 1253
a8237fc4 1254 if (cio_is_console(sch->schid))
1da177e4
LT
1255 return;
1256 if (!sch->schib.pmcw.ena)
1257 /* Nothing to do. */
1258 return;
1259 ret = cio_disable_subchannel(sch);
1260 if (ret != -EBUSY)
1261 /* Subchannel is disabled, we're done. */
1262 return;
1263 cdev->private->state = DEV_STATE_QUIESCE;
1264 if (cdev->handler)
1265 cdev->handler(cdev, cdev->private->intparm,
1266 ERR_PTR(-EIO));
1267 ret = ccw_device_cancel_halt_clear(cdev);
1268 if (ret == -EBUSY) {
1269 ccw_device_set_timeout(cdev, HZ/10);
1270 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1271 }
1272 cio_disable_subchannel(sch);
1273}
1274
c820de39
CH
1275static int device_is_disconnected(struct ccw_device *cdev)
1276{
1277 if (!cdev)
1278 return 0;
1279 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1280 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1281}
1282
1283static int recovery_check(struct device *dev, void *data)
1284{
1285 struct ccw_device *cdev = to_ccwdev(dev);
1286 int *redo = data;
1287
1288 spin_lock_irq(cdev->ccwlock);
1289 switch (cdev->private->state) {
1290 case DEV_STATE_DISCONNECTED:
1291 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1292 cdev->private->dev_id.ssid,
1293 cdev->private->dev_id.devno);
1294 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1295 *redo = 1;
1296 break;
1297 case DEV_STATE_DISCONNECTED_SENSE_ID:
1298 *redo = 1;
1299 break;
1300 }
1301 spin_unlock_irq(cdev->ccwlock);
1302
1303 return 0;
1304}
1305
1306static void recovery_work_func(struct work_struct *unused)
1307{
1308 int redo = 0;
1309
1310 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1311 if (redo) {
1312 spin_lock_irq(&recovery_lock);
1313 if (!timer_pending(&recovery_timer)) {
1314 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1315 recovery_phase++;
1316 mod_timer(&recovery_timer, jiffies +
1317 recovery_delay[recovery_phase] * HZ);
1318 }
1319 spin_unlock_irq(&recovery_lock);
1320 } else
1321 CIO_MSG_EVENT(4, "recovery: end\n");
1322}
1323
1324static DECLARE_WORK(recovery_work, recovery_work_func);
1325
1326static void recovery_func(unsigned long data)
1327{
1328 /*
1329 * We can't do our recovery in softirq context and it's not
1330 * performance critical, so we schedule it.
1331 */
1332 schedule_work(&recovery_work);
1333}
1334
1335static void ccw_device_schedule_recovery(void)
1336{
1337 unsigned long flags;
1338
1339 CIO_MSG_EVENT(4, "recovery: schedule\n");
1340 spin_lock_irqsave(&recovery_lock, flags);
1341 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1342 recovery_phase = 0;
1343 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1344 }
1345 spin_unlock_irqrestore(&recovery_lock, flags);
1346}
1347
ecf5d9ef
PO
1348static int purge_fn(struct device *dev, void *data)
1349{
1350 struct ccw_device *cdev = to_ccwdev(dev);
1351 struct ccw_device_private *priv = cdev->private;
1352 int unreg;
1353
1354 spin_lock_irq(cdev->ccwlock);
1355 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1356 (priv->state == DEV_STATE_OFFLINE);
1357 spin_unlock_irq(cdev->ccwlock);
1358 if (!unreg)
1359 goto out;
ecf5d9ef
PO
1360 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1361 priv->dev_id.devno);
c4621a62 1362 ccw_device_schedule_sch_unregister(cdev);
ecf5d9ef
PO
1363
1364out:
1365 /* Abort loop in case of pending signal. */
1366 if (signal_pending(current))
1367 return -EINTR;
1368
1369 return 0;
1370}
1371
1372/**
1373 * ccw_purge_blacklisted - purge unused, blacklisted devices
1374 *
1375 * Unregister all ccw devices that are offline and on the blacklist.
1376 */
1377int ccw_purge_blacklisted(void)
1378{
1379 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1380 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1381 return 0;
1382}
1383
6afcc775 1384void ccw_device_set_disconnected(struct ccw_device *cdev)
c820de39
CH
1385{
1386 if (!cdev)
1387 return;
1388 ccw_device_set_timeout(cdev, 0);
1389 cdev->private->flags.fake_irb = 0;
1390 cdev->private->state = DEV_STATE_DISCONNECTED;
1391 if (cdev->online)
1392 ccw_device_schedule_recovery();
1393}
1394
91c36919
PO
1395void ccw_device_set_notoper(struct ccw_device *cdev)
1396{
1397 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1398
1399 CIO_TRACE_EVENT(2, "notoper");
b9d3aed7 1400 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
91c36919
PO
1401 ccw_device_set_timeout(cdev, 0);
1402 cio_disable_subchannel(sch);
1403 cdev->private->state = DEV_STATE_NOT_OPER;
1404}
1405
5d6e6b6f
PO
1406enum io_sch_action {
1407 IO_SCH_UNREG,
1408 IO_SCH_ORPH_UNREG,
1409 IO_SCH_ATTACH,
1410 IO_SCH_UNREG_ATTACH,
1411 IO_SCH_ORPH_ATTACH,
1412 IO_SCH_REPROBE,
1413 IO_SCH_VERIFY,
1414 IO_SCH_DISC,
1415 IO_SCH_NOP,
1416};
1417
1418static enum io_sch_action sch_get_action(struct subchannel *sch)
1419{
1420 struct ccw_device *cdev;
1421
1422 cdev = sch_get_cdev(sch);
1423 if (cio_update_schib(sch)) {
1424 /* Not operational. */
1425 if (!cdev)
1426 return IO_SCH_UNREG;
1427 if (!ccw_device_notify(cdev, CIO_GONE))
1428 return IO_SCH_UNREG;
1429 return IO_SCH_ORPH_UNREG;
1430 }
1431 /* Operational. */
1432 if (!cdev)
1433 return IO_SCH_ATTACH;
1434 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1435 if (!ccw_device_notify(cdev, CIO_GONE))
1436 return IO_SCH_UNREG_ATTACH;
1437 return IO_SCH_ORPH_ATTACH;
1438 }
1439 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1440 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1441 return IO_SCH_UNREG;
1442 return IO_SCH_DISC;
1443 }
1444 if (device_is_disconnected(cdev))
1445 return IO_SCH_REPROBE;
1446 if (cdev->online)
1447 return IO_SCH_VERIFY;
1448 return IO_SCH_NOP;
1449}
1450
1451/**
1452 * io_subchannel_sch_event - process subchannel event
1453 * @sch: subchannel
1454 * @process: non-zero if function is called in process context
1455 *
1456 * An unspecified event occurred for this subchannel. Adjust data according
1457 * to the current operational state of the subchannel and device. Return
1458 * zero when the event has been handled sufficiently or -EAGAIN when this
1459 * function should be called again in process context.
1460 */
1461static int io_subchannel_sch_event(struct subchannel *sch, int process)
c820de39 1462{
c820de39 1463 unsigned long flags;
c820de39 1464 struct ccw_device *cdev;
5d6e6b6f
PO
1465 struct ccw_dev_id dev_id;
1466 enum io_sch_action action;
1467 int rc = -EAGAIN;
c820de39
CH
1468
1469 spin_lock_irqsave(sch->lock, flags);
5d6e6b6f
PO
1470 if (!device_is_registered(&sch->dev))
1471 goto out_unlock;
1472 action = sch_get_action(sch);
1473 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1474 sch->schid.ssid, sch->schid.sch_no, process,
1475 action);
1476 /* Perform immediate actions while holding the lock. */
c820de39 1477 cdev = sch_get_cdev(sch);
5d6e6b6f
PO
1478 switch (action) {
1479 case IO_SCH_REPROBE:
1480 /* Trigger device recognition. */
1481 ccw_device_trigger_reprobe(cdev);
1482 rc = 0;
1483 goto out_unlock;
1484 case IO_SCH_VERIFY:
1485 /* Trigger path verification. */
1486 io_subchannel_verify(sch);
1487 rc = 0;
1488 goto out_unlock;
1489 case IO_SCH_DISC:
1490 ccw_device_set_disconnected(cdev);
1491 rc = 0;
1492 goto out_unlock;
1493 case IO_SCH_ORPH_UNREG:
1494 case IO_SCH_ORPH_ATTACH:
1495 ccw_device_set_disconnected(cdev);
1496 break;
1497 case IO_SCH_UNREG_ATTACH:
1498 case IO_SCH_UNREG:
1499 if (cdev)
1500 ccw_device_set_notoper(cdev);
1501 break;
1502 case IO_SCH_NOP:
1503 rc = 0;
1504 goto out_unlock;
1505 default:
1506 break;
c820de39 1507 }
5d6e6b6f
PO
1508 spin_unlock_irqrestore(sch->lock, flags);
1509 /* All other actions require process context. */
1510 if (!process)
1511 goto out;
1512 /* Handle attached ccw device. */
1513 switch (action) {
1514 case IO_SCH_ORPH_UNREG:
1515 case IO_SCH_ORPH_ATTACH:
1516 /* Move ccw device to orphanage. */
1517 rc = ccw_device_move_to_orph(cdev);
1518 if (rc)
1519 goto out;
c820de39 1520 break;
5d6e6b6f
PO
1521 case IO_SCH_UNREG_ATTACH:
1522 /* Unregister ccw device. */
1523 ccw_device_unregister(cdev);
c820de39 1524 break;
5d6e6b6f 1525 default:
c820de39
CH
1526 break;
1527 }
5d6e6b6f 1528 /* Handle subchannel. */
c820de39 1529 switch (action) {
5d6e6b6f
PO
1530 case IO_SCH_ORPH_UNREG:
1531 case IO_SCH_UNREG:
c820de39 1532 css_sch_device_unregister(sch);
c820de39 1533 break;
5d6e6b6f
PO
1534 case IO_SCH_ORPH_ATTACH:
1535 case IO_SCH_UNREG_ATTACH:
1536 case IO_SCH_ATTACH:
1537 dev_id.ssid = sch->schid.ssid;
1538 dev_id.devno = sch->schib.pmcw.dev;
1539 cdev = get_ccwdev_by_dev_id(&dev_id);
1540 if (!cdev) {
1541 sch_create_and_recog_new_device(sch);
1542 break;
1543 }
1544 rc = ccw_device_move_to_sch(cdev, sch);
1545 if (rc) {
1546 /* Release reference from get_ccwdev_by_dev_id() */
1547 put_device(&cdev->dev);
1548 goto out;
1549 }
1550 spin_lock_irqsave(sch->lock, flags);
c820de39 1551 ccw_device_trigger_reprobe(cdev);
5d6e6b6f
PO
1552 spin_unlock_irqrestore(sch->lock, flags);
1553 /* Release reference from get_ccwdev_by_dev_id() */
1554 put_device(&cdev->dev);
91c36919 1555 break;
c820de39
CH
1556 default:
1557 break;
1558 }
5d6e6b6f 1559 return 0;
c820de39 1560
5d6e6b6f
PO
1561out_unlock:
1562 spin_unlock_irqrestore(sch->lock, flags);
1563out:
1564 return rc;
c820de39
CH
1565}
1566
1da177e4
LT
1567#ifdef CONFIG_CCW_CONSOLE
1568static struct ccw_device console_cdev;
1569static struct ccw_device_private console_private;
1570static int console_cdev_in_use;
1571
2ec22984
CH
1572static DEFINE_SPINLOCK(ccw_console_lock);
1573
1574spinlock_t * cio_get_console_lock(void)
1575{
1576 return &ccw_console_lock;
1577}
1578
0ae7a7b2
CH
1579static int ccw_device_console_enable(struct ccw_device *cdev,
1580 struct subchannel *sch)
1da177e4
LT
1581{
1582 int rc;
1583
cd6b4f27
CH
1584 /* Attach subchannel private data. */
1585 sch->private = cio_get_console_priv();
1586 memset(sch->private, 0, sizeof(struct io_subchannel_private));
0ae7a7b2 1587 io_subchannel_init_fields(sch);
f444cc0e
SO
1588 rc = cio_commit_config(sch);
1589 if (rc)
1590 return rc;
0ae7a7b2 1591 sch->driver = &io_subchannel_driver;
1da177e4 1592 /* Initialize the ccw_device structure. */
292888c8 1593 cdev->dev.parent= &sch->dev;
1da177e4
LT
1594 rc = io_subchannel_recog(cdev, sch);
1595 if (rc)
1596 return rc;
1597
1598 /* Now wait for the async. recognition to come to an end. */
1599 spin_lock_irq(cdev->ccwlock);
1600 while (!dev_fsm_final_state(cdev))
1601 wait_cons_dev();
1602 rc = -EIO;
1603 if (cdev->private->state != DEV_STATE_OFFLINE)
1604 goto out_unlock;
1605 ccw_device_online(cdev);
1606 while (!dev_fsm_final_state(cdev))
1607 wait_cons_dev();
1608 if (cdev->private->state != DEV_STATE_ONLINE)
1609 goto out_unlock;
1610 rc = 0;
1611out_unlock:
1612 spin_unlock_irq(cdev->ccwlock);
1613 return 0;
1614}
1615
1616struct ccw_device *
1617ccw_device_probe_console(void)
1618{
1619 struct subchannel *sch;
1620 int ret;
1621
1622 if (xchg(&console_cdev_in_use, 1) != 0)
600b5d16 1623 return ERR_PTR(-EBUSY);
1da177e4
LT
1624 sch = cio_probe_console();
1625 if (IS_ERR(sch)) {
1626 console_cdev_in_use = 0;
1627 return (void *) sch;
1628 }
1629 memset(&console_cdev, 0, sizeof(struct ccw_device));
1630 memset(&console_private, 0, sizeof(struct ccw_device_private));
1631 console_cdev.private = &console_private;
c1637532 1632 console_private.cdev = &console_cdev;
1da177e4
LT
1633 ret = ccw_device_console_enable(&console_cdev, sch);
1634 if (ret) {
1635 cio_release_console();
1636 console_cdev_in_use = 0;
1637 return ERR_PTR(ret);
1638 }
1639 console_cdev.online = 1;
1640 return &console_cdev;
1641}
1f4e7eda 1642
6664845c
MS
1643static int ccw_device_pm_restore(struct device *dev);
1644
1645int ccw_device_force_console(void)
1646{
1647 if (!console_cdev_in_use)
1648 return -ENODEV;
1649 return ccw_device_pm_restore(&console_cdev.dev);
1650}
1651EXPORT_SYMBOL_GPL(ccw_device_force_console);
1da177e4
LT
1652#endif
1653
1654/*
1655 * get ccw_device matching the busid, but only if owned by cdrv
1656 */
b0744bd2
CH
1657static int
1658__ccwdev_check_busid(struct device *dev, void *id)
1659{
1660 char *bus_id;
1661
12975aef 1662 bus_id = id;
b0744bd2 1663
98df67b3 1664 return (strcmp(bus_id, dev_name(dev)) == 0);
b0744bd2
CH
1665}
1666
1667
b2ffd8e9
CH
1668/**
1669 * get_ccwdev_by_busid() - obtain device from a bus id
1670 * @cdrv: driver the device is owned by
1671 * @bus_id: bus id of the device to be searched
1672 *
1673 * This function searches all devices owned by @cdrv for a device with a bus
1674 * id matching @bus_id.
1675 * Returns:
1676 * If a match is found, its reference count of the found device is increased
1677 * and it is returned; else %NULL is returned.
1678 */
1679struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1680 const char *bus_id)
1da177e4 1681{
b0744bd2 1682 struct device *dev;
1da177e4
LT
1683 struct device_driver *drv;
1684
1685 drv = get_driver(&cdrv->driver);
1686 if (!drv)
b0744bd2 1687 return NULL;
1da177e4 1688
b0744bd2
CH
1689 dev = driver_find_device(drv, NULL, (void *)bus_id,
1690 __ccwdev_check_busid);
1da177e4
LT
1691 put_driver(drv);
1692
d2c993d8 1693 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
1694}
1695
1696/************************** device driver handling ************************/
1697
1698/* This is the implementation of the ccw_driver class. The probe, remove
1699 * and release methods are initially very similar to the device_driver
1700 * implementations, with the difference that they have ccw_device
1701 * arguments.
1702 *
1703 * A ccw driver also contains the information that is needed for
1704 * device matching.
1705 */
1706static int
1707ccw_device_probe (struct device *dev)
1708{
1709 struct ccw_device *cdev = to_ccwdev(dev);
1710 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1711 int ret;
1712
1713 cdev->drv = cdrv; /* to let the driver call _set_online */
1714
1715 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1716
1717 if (ret) {
d2c993d8 1718 cdev->drv = NULL;
1da177e4
LT
1719 return ret;
1720 }
1721
1722 return 0;
1723}
1724
1725static int
1726ccw_device_remove (struct device *dev)
1727{
1728 struct ccw_device *cdev = to_ccwdev(dev);
1729 struct ccw_driver *cdrv = cdev->drv;
1730 int ret;
1731
1da177e4
LT
1732 if (cdrv->remove)
1733 cdrv->remove(cdev);
1734 if (cdev->online) {
1735 cdev->online = 0;
1736 spin_lock_irq(cdev->ccwlock);
1737 ret = ccw_device_offline(cdev);
1738 spin_unlock_irq(cdev->ccwlock);
1739 if (ret == 0)
1740 wait_event(cdev->private->wait_q,
1741 dev_fsm_final_state(cdev));
1742 else
139b83dd 1743 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
1744 "device 0.%x.%04x\n",
1745 ret, cdev->private->dev_id.ssid,
1746 cdev->private->dev_id.devno);
9cd67421
CH
1747 /* Give up reference obtained in ccw_device_set_online(). */
1748 put_device(&cdev->dev);
1da177e4
LT
1749 }
1750 ccw_device_set_timeout(cdev, 0);
d2c993d8 1751 cdev->drv = NULL;
1da177e4
LT
1752 return 0;
1753}
1754
958974fb
CH
1755static void ccw_device_shutdown(struct device *dev)
1756{
1757 struct ccw_device *cdev;
1758
1759 cdev = to_ccwdev(dev);
1760 if (cdev->drv && cdev->drv->shutdown)
1761 cdev->drv->shutdown(cdev);
1842f2b1 1762 disable_cmf(cdev);
958974fb
CH
1763}
1764
823d494a
SO
1765static int ccw_device_pm_prepare(struct device *dev)
1766{
1767 struct ccw_device *cdev = to_ccwdev(dev);
1768
1769 if (work_pending(&cdev->private->kick_work))
1770 return -EAGAIN;
1771 /* Fail while device is being set online/offline. */
1772 if (atomic_read(&cdev->private->onoff))
1773 return -EAGAIN;
1774
1775 if (cdev->online && cdev->drv && cdev->drv->prepare)
1776 return cdev->drv->prepare(cdev);
1777
1778 return 0;
1779}
1780
1781static void ccw_device_pm_complete(struct device *dev)
1782{
1783 struct ccw_device *cdev = to_ccwdev(dev);
1784
1785 if (cdev->online && cdev->drv && cdev->drv->complete)
1786 cdev->drv->complete(cdev);
1787}
1788
1789static int ccw_device_pm_freeze(struct device *dev)
1790{
1791 struct ccw_device *cdev = to_ccwdev(dev);
1792 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1793 int ret, cm_enabled;
1794
1795 /* Fail suspend while device is in transistional state. */
1796 if (!dev_fsm_final_state(cdev))
1797 return -EAGAIN;
1798 if (!cdev->online)
1799 return 0;
1800 if (cdev->drv && cdev->drv->freeze) {
1801 ret = cdev->drv->freeze(cdev);
1802 if (ret)
1803 return ret;
1804 }
1805
1806 spin_lock_irq(sch->lock);
1807 cm_enabled = cdev->private->cmb != NULL;
1808 spin_unlock_irq(sch->lock);
1809 if (cm_enabled) {
1810 /* Don't have the css write on memory. */
1811 ret = ccw_set_cmf(cdev, 0);
1812 if (ret)
1813 return ret;
1814 }
1815 /* From here on, disallow device driver I/O. */
1816 spin_lock_irq(sch->lock);
1817 ret = cio_disable_subchannel(sch);
1818 spin_unlock_irq(sch->lock);
1819
1820 return ret;
1821}
1822
1823static int ccw_device_pm_thaw(struct device *dev)
1824{
1825 struct ccw_device *cdev = to_ccwdev(dev);
1826 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1827 int ret, cm_enabled;
1828
1829 if (!cdev->online)
1830 return 0;
1831
1832 spin_lock_irq(sch->lock);
1833 /* Allow device driver I/O again. */
1834 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1835 cm_enabled = cdev->private->cmb != NULL;
1836 spin_unlock_irq(sch->lock);
1837 if (ret)
1838 return ret;
1839
1840 if (cm_enabled) {
1841 ret = ccw_set_cmf(cdev, 1);
1842 if (ret)
1843 return ret;
1844 }
1845
1846 if (cdev->drv && cdev->drv->thaw)
1847 ret = cdev->drv->thaw(cdev);
1848
1849 return ret;
1850}
1851
1852static void __ccw_device_pm_restore(struct ccw_device *cdev)
1853{
1854 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1855 int ret;
1856
1857 if (cio_is_console(sch->schid))
1858 goto out;
1859 /*
1860 * While we were sleeping, devices may have gone or become
1861 * available again. Kick re-detection.
1862 */
1863 spin_lock_irq(sch->lock);
1864 cdev->private->flags.resuming = 1;
1865 ret = ccw_device_recognition(cdev);
1866 spin_unlock_irq(sch->lock);
1867 if (ret) {
1868 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
f014824e
SO
1869 "0.%x.%04x (ret=%d)\n",
1870 cdev->private->dev_id.ssid,
1871 cdev->private->dev_id.devno, ret);
823d494a
SO
1872 spin_lock_irq(sch->lock);
1873 cdev->private->state = DEV_STATE_DISCONNECTED;
1874 spin_unlock_irq(sch->lock);
1875 /* notify driver after the resume cb */
1876 goto out;
1877 }
1878 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1879 cdev->private->state == DEV_STATE_DISCONNECTED);
1880
1881out:
1882 cdev->private->flags.resuming = 0;
1883}
1884
1885static int resume_handle_boxed(struct ccw_device *cdev)
1886{
1887 cdev->private->state = DEV_STATE_BOXED;
1888 if (ccw_device_notify(cdev, CIO_BOXED))
1889 return 0;
1890 ccw_device_schedule_sch_unregister(cdev);
1891 return -ENODEV;
1892}
1893
1894static int resume_handle_disc(struct ccw_device *cdev)
1895{
1896 cdev->private->state = DEV_STATE_DISCONNECTED;
1897 if (ccw_device_notify(cdev, CIO_GONE))
1898 return 0;
1899 ccw_device_schedule_sch_unregister(cdev);
1900 return -ENODEV;
1901}
1902
1903static int ccw_device_pm_restore(struct device *dev)
1904{
1905 struct ccw_device *cdev = to_ccwdev(dev);
1906 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1907 int ret = 0, cm_enabled;
1908
1909 __ccw_device_pm_restore(cdev);
1910 spin_lock_irq(sch->lock);
1911 if (cio_is_console(sch->schid)) {
1912 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1913 spin_unlock_irq(sch->lock);
1914 goto out_restore;
1915 }
1916 cdev->private->flags.donotify = 0;
1917 /* check recognition results */
1918 switch (cdev->private->state) {
1919 case DEV_STATE_OFFLINE:
1920 break;
1921 case DEV_STATE_BOXED:
1922 ret = resume_handle_boxed(cdev);
1923 spin_unlock_irq(sch->lock);
1924 if (ret)
1925 goto out;
1926 goto out_restore;
1927 case DEV_STATE_DISCONNECTED:
1928 goto out_disc_unlock;
1929 default:
1930 goto out_unreg_unlock;
1931 }
1932 /* check if the device id has changed */
1933 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
f014824e
SO
1934 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1935 "changed from %04x to %04x)\n",
1936 sch->schid.ssid, sch->schid.sch_no,
823d494a
SO
1937 cdev->private->dev_id.devno,
1938 sch->schib.pmcw.dev);
1939 goto out_unreg_unlock;
1940 }
1941 /* check if the device type has changed */
1942 if (!ccw_device_test_sense_data(cdev)) {
1943 ccw_device_update_sense_data(cdev);
1944 PREPARE_WORK(&cdev->private->kick_work,
1945 ccw_device_do_unbind_bind);
1946 queue_work(ccw_device_work, &cdev->private->kick_work);
1947 ret = -ENODEV;
1948 goto out_unlock;
1949 }
1950 if (!cdev->online) {
1951 ret = 0;
1952 goto out_unlock;
1953 }
1954 ret = ccw_device_online(cdev);
1955 if (ret)
1956 goto out_disc_unlock;
1957
1958 cm_enabled = cdev->private->cmb != NULL;
1959 spin_unlock_irq(sch->lock);
1960
1961 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1962 if (cdev->private->state != DEV_STATE_ONLINE) {
1963 spin_lock_irq(sch->lock);
1964 goto out_disc_unlock;
1965 }
1966 if (cm_enabled) {
1967 ret = ccw_set_cmf(cdev, 1);
1968 if (ret) {
f014824e
SO
1969 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1970 "(rc=%d)\n", cdev->private->dev_id.ssid,
1971 cdev->private->dev_id.devno, ret);
823d494a
SO
1972 ret = 0;
1973 }
1974 }
1975
1976out_restore:
1977 if (cdev->online && cdev->drv && cdev->drv->restore)
1978 ret = cdev->drv->restore(cdev);
1979out:
1980 return ret;
1981
1982out_disc_unlock:
1983 ret = resume_handle_disc(cdev);
1984 spin_unlock_irq(sch->lock);
1985 if (ret)
1986 return ret;
1987 goto out_restore;
1988
1989out_unreg_unlock:
1990 ccw_device_schedule_sch_unregister(cdev);
1991 ret = -ENODEV;
1992out_unlock:
1993 spin_unlock_irq(sch->lock);
1994 return ret;
1995}
1996
1997static struct dev_pm_ops ccw_pm_ops = {
1998 .prepare = ccw_device_pm_prepare,
1999 .complete = ccw_device_pm_complete,
2000 .freeze = ccw_device_pm_freeze,
2001 .thaw = ccw_device_pm_thaw,
2002 .restore = ccw_device_pm_restore,
2003};
2004
8bbace7e
CH
2005struct bus_type ccw_bus_type = {
2006 .name = "ccw",
2007 .match = ccw_bus_match,
2008 .uevent = ccw_uevent,
2009 .probe = ccw_device_probe,
2010 .remove = ccw_device_remove,
958974fb 2011 .shutdown = ccw_device_shutdown,
823d494a 2012 .pm = &ccw_pm_ops,
8bbace7e
CH
2013};
2014
b2ffd8e9
CH
2015/**
2016 * ccw_driver_register() - register a ccw driver
2017 * @cdriver: driver to be registered
2018 *
2019 * This function is mainly a wrapper around driver_register().
2020 * Returns:
2021 * %0 on success and a negative error value on failure.
2022 */
2023int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
2024{
2025 struct device_driver *drv = &cdriver->driver;
2026
2027 drv->bus = &ccw_bus_type;
2028 drv->name = cdriver->name;
4beee646 2029 drv->owner = cdriver->owner;
1da177e4
LT
2030
2031 return driver_register(drv);
2032}
2033
b2ffd8e9
CH
2034/**
2035 * ccw_driver_unregister() - deregister a ccw driver
2036 * @cdriver: driver to be deregistered
2037 *
2038 * This function is mainly a wrapper around driver_unregister().
2039 */
2040void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
2041{
2042 driver_unregister(&cdriver->driver);
2043}
2044
a8237fc4
CH
2045/* Helper func for qdio. */
2046struct subchannel_id
2047ccw_device_get_subchannel_id(struct ccw_device *cdev)
2048{
2049 struct subchannel *sch;
2050
2051 sch = to_subchannel(cdev->dev.parent);
2052 return sch->schid;
2053}
2054
1da177e4
LT
2055MODULE_LICENSE("GPL");
2056EXPORT_SYMBOL(ccw_device_set_online);
2057EXPORT_SYMBOL(ccw_device_set_offline);
2058EXPORT_SYMBOL(ccw_driver_register);
2059EXPORT_SYMBOL(ccw_driver_unregister);
2060EXPORT_SYMBOL(get_ccwdev_by_busid);
2061EXPORT_SYMBOL(ccw_bus_type);
2062EXPORT_SYMBOL(ccw_device_work);
a8237fc4 2063EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);
This page took 0.648258 seconds and 5 git commands to generate.