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