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