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