Merge remote-tracking branch 'nfsd/nfsd-next'
[deliverable/linux.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) {
baebc70a
JP
367 pr_warn("%s: The device entered boxed state while being set offline\n",
368 dev_name(&cdev->dev));
a7ae2c02 369 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
baebc70a
JP
370 pr_warn("%s: The device stopped operating while being set offline\n",
371 dev_name(&cdev->dev));
a7ae2c02 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) {
baebc70a
JP
432 pr_warn("%s: Setting the device online failed because it is boxed\n",
433 dev_name(&cdev->dev));
a7ae2c02 434 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
baebc70a
JP
435 pr_warn("%s: Setting the device online failed because it is not operational\n",
436 dev_name(&cdev->dev));
a7ae2c02 437 }
9cd67421
CH
438 /* Give up online reference since onlining failed. */
439 put_device(&cdev->dev);
1da177e4 440 return -ENODEV;
9cd67421 441 }
217ee6c6
ME
442 spin_unlock_irq(cdev->ccwlock);
443 if (cdev->drv->set_online)
444 ret = cdev->drv->set_online(cdev);
445 if (ret)
446 goto rollback;
74bd0d85
SO
447
448 spin_lock_irq(cdev->ccwlock);
217ee6c6 449 cdev->online = 1;
74bd0d85 450 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
451 return 0;
452
453rollback:
1da177e4 454 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
455 /* Wait until a final state or DISCONNECTED is reached */
456 while (!dev_fsm_final_state(cdev) &&
457 cdev->private->state != DEV_STATE_DISCONNECTED) {
458 spin_unlock_irq(cdev->ccwlock);
459 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
460 cdev->private->state == DEV_STATE_DISCONNECTED));
461 spin_lock_irq(cdev->ccwlock);
462 }
463 ret2 = ccw_device_offline(cdev);
464 if (ret2)
465 goto error;
466 spin_unlock_irq(cdev->ccwlock);
467 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
468 cdev->private->state == DEV_STATE_DISCONNECTED));
469 /* Give up online reference since onlining failed. */
470 put_device(&cdev->dev);
471 return ret;
472
473error:
474 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
475 "device 0.%x.%04x\n",
476 ret2, cdev->private->dev_id.ssid,
477 cdev->private->dev_id.devno);
478 cdev->private->state = DEV_STATE_OFFLINE;
1da177e4 479 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
480 /* Give up online reference since onlining failed. */
481 put_device(&cdev->dev);
217ee6c6 482 return ret;
1da177e4
LT
483}
484
e74fe0ce 485static int online_store_handle_offline(struct ccw_device *cdev)
f5ba6c86 486{
37de53bb
PO
487 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
488 spin_lock_irq(cdev->ccwlock);
489 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
490 spin_unlock_irq(cdev->ccwlock);
7cd40314
SO
491 return 0;
492 }
493 if (cdev->drv && cdev->drv->set_offline)
e74fe0ce 494 return ccw_device_set_offline(cdev);
7cd40314 495 return -EINVAL;
f5ba6c86
CH
496}
497
498static int online_store_recog_and_online(struct ccw_device *cdev)
499{
f5ba6c86 500 /* Do device recognition, if needed. */
99f6a570 501 if (cdev->private->state == DEV_STATE_BOXED) {
1f5bd384 502 spin_lock_irq(cdev->ccwlock);
736b5db8 503 ccw_device_recognition(cdev);
1f5bd384 504 spin_unlock_irq(cdev->ccwlock);
f5ba6c86
CH
505 wait_event(cdev->private->wait_q,
506 cdev->private->flags.recog_done);
156013ff
SO
507 if (cdev->private->state != DEV_STATE_OFFLINE)
508 /* recognition failed */
509 return -EAGAIN;
f5ba6c86
CH
510 }
511 if (cdev->drv && cdev->drv->set_online)
7cd40314
SO
512 return ccw_device_set_online(cdev);
513 return -EINVAL;
f5ba6c86 514}
156013ff 515
c78aa6cb 516static int online_store_handle_online(struct ccw_device *cdev, int force)
f5ba6c86
CH
517{
518 int ret;
519
520 ret = online_store_recog_and_online(cdev);
156013ff 521 if (ret && !force)
c78aa6cb 522 return ret;
f5ba6c86
CH
523 if (force && cdev->private->state == DEV_STATE_BOXED) {
524 ret = ccw_device_stlck(cdev);
c78aa6cb
ME
525 if (ret)
526 return ret;
f5ba6c86
CH
527 if (cdev->id.cu_type == 0)
528 cdev->private->state = DEV_STATE_NOT_OPER;
156013ff
SO
529 ret = online_store_recog_and_online(cdev);
530 if (ret)
531 return ret;
f5ba6c86 532 }
c78aa6cb 533 return 0;
f5ba6c86
CH
534}
535
536static ssize_t online_store (struct device *dev, struct device_attribute *attr,
537 const char *buf, size_t count)
1da177e4
LT
538{
539 struct ccw_device *cdev = to_ccwdev(dev);
2f972202
CH
540 int force, ret;
541 unsigned long i;
1da177e4 542
a2fc8485 543 /* Prevent conflict between multiple on-/offline processing requests. */
350e9120 544 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4 545 return -EAGAIN;
a2fc8485
PO
546 /* Prevent conflict between internal I/Os and on-/offline processing. */
547 if (!dev_fsm_final_state(cdev) &&
548 cdev->private->state != DEV_STATE_DISCONNECTED) {
549 ret = -EAGAIN;
00381eeb 550 goto out;
a2fc8485
PO
551 }
552 /* Prevent conflict between pending work and on-/offline processing.*/
553 if (work_pending(&cdev->private->todo_work)) {
554 ret = -EAGAIN;
00381eeb 555 goto out;
1da177e4
LT
556 }
557 if (!strncmp(buf, "force\n", count)) {
558 force = 1;
559 i = 1;
2f972202 560 ret = 0;
1da177e4
LT
561 } else {
562 force = 0;
0178722b 563 ret = kstrtoul(buf, 16, &i);
1da177e4 564 }
2f972202
CH
565 if (ret)
566 goto out;
00381eeb
SO
567
568 device_lock(dev);
f5ba6c86
CH
569 switch (i) {
570 case 0:
e74fe0ce 571 ret = online_store_handle_offline(cdev);
f5ba6c86
CH
572 break;
573 case 1:
c78aa6cb 574 ret = online_store_handle_online(cdev, force);
f5ba6c86
CH
575 break;
576 default:
2f972202 577 ret = -EINVAL;
1da177e4 578 }
00381eeb
SO
579 device_unlock(dev);
580
2f972202 581out:
1da177e4 582 atomic_set(&cdev->private->onoff, 0);
e74fe0ce 583 return (ret < 0) ? ret : count;
1da177e4
LT
584}
585
586static ssize_t
3fd3c0a5 587available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
588{
589 struct ccw_device *cdev = to_ccwdev(dev);
590 struct subchannel *sch;
591
d7b5a4c9
CH
592 if (ccw_device_is_orphan(cdev))
593 return sprintf(buf, "no device\n");
1da177e4
LT
594 switch (cdev->private->state) {
595 case DEV_STATE_BOXED:
596 return sprintf(buf, "boxed\n");
597 case DEV_STATE_DISCONNECTED:
598 case DEV_STATE_DISCONNECTED_SENSE_ID:
599 case DEV_STATE_NOT_OPER:
600 sch = to_subchannel(dev->parent);
601 if (!sch->lpm)
602 return sprintf(buf, "no path\n");
603 else
604 return sprintf(buf, "no device\n");
605 default:
606 /* All other states considered fine. */
607 return sprintf(buf, "good\n");
608 }
609}
610
fd0457a6
ME
611static ssize_t
612initiate_logging(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
614{
615 struct subchannel *sch = to_subchannel(dev);
616 int rc;
617
618 rc = chsc_siosl(sch->schid);
619 if (rc < 0) {
baebc70a
JP
620 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
621 sch->schid.ssid, sch->schid.sch_no, rc);
fd0457a6
ME
622 return rc;
623 }
624 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
625 sch->schid.ssid, sch->schid.sch_no);
626 return count;
627}
628
84c57ad5
SO
629static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
630 char *buf)
631{
632 struct subchannel *sch = to_subchannel(dev);
633
634 return sprintf(buf, "%02x\n", sch->vpm);
635}
636
1da177e4
LT
637static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
638static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
639static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
640static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
f1fc78a8 641static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
1da177e4 642static DEVICE_ATTR(online, 0644, online_show, online_store);
1da177e4 643static DEVICE_ATTR(availability, 0444, available_show, NULL);
fd0457a6 644static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
84c57ad5 645static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
1da177e4 646
7e9db9ea 647static struct attribute *io_subchannel_attrs[] = {
1da177e4
LT
648 &dev_attr_chpids.attr,
649 &dev_attr_pimpampom.attr,
fd0457a6 650 &dev_attr_logging.attr,
84c57ad5 651 &dev_attr_vpm.attr,
1da177e4
LT
652 NULL,
653};
654
7e9db9ea
CH
655static struct attribute_group io_subchannel_attr_group = {
656 .attrs = io_subchannel_attrs,
529192f3 657};
1da177e4
LT
658
659static struct attribute * ccwdev_attrs[] = {
660 &dev_attr_devtype.attr,
661 &dev_attr_cutype.attr,
f1fc78a8 662 &dev_attr_modalias.attr,
1da177e4
LT
663 &dev_attr_online.attr,
664 &dev_attr_cmb_enable.attr,
665 &dev_attr_availability.attr,
666 NULL,
667};
668
669static struct attribute_group ccwdev_attr_group = {
670 .attrs = ccwdev_attrs,
671};
672
a4dbd674 673static const struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
674 &ccwdev_attr_group,
675 NULL,
676};
1da177e4 677
2c3e7e15 678static int ccw_device_add(struct ccw_device *cdev)
1da177e4
LT
679{
680 struct device *dev = &cdev->dev;
1da177e4
LT
681
682 dev->bus = &ccw_bus_type;
7d253b9a 683 return device_add(dev);
1da177e4
LT
684}
685
5d6e6b6f 686static int match_dev_id(struct device *dev, void *data)
b0744bd2 687{
5d6e6b6f
PO
688 struct ccw_device *cdev = to_ccwdev(dev);
689 struct ccw_dev_id *dev_id = data;
d7b5a4c9 690
d7b5a4c9
CH
691 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
692}
693
b7a610f7
SO
694/**
695 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
696 * @dev_id: id of the device to be searched
697 *
698 * This function searches all devices attached to the ccw bus for a device
699 * matching @dev_id.
700 * Returns:
701 * If a device is found its reference count is increased and returned;
702 * else %NULL is returned.
703 */
704struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
d7b5a4c9
CH
705{
706 struct device *dev;
707
5d6e6b6f 708 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
d7b5a4c9
CH
709
710 return dev ? to_ccwdev(dev) : NULL;
711}
b7a610f7 712EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
d7b5a4c9 713
37de53bb 714static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
1da177e4 715{
eb32ae8d 716 int ret;
1da177e4 717
7d253b9a 718 if (device_is_registered(&cdev->dev)) {
eb32ae8d
CH
719 device_release_driver(&cdev->dev);
720 ret = device_attach(&cdev->dev);
721 WARN_ON(ret == -ENODEV);
722 }
1da177e4
LT
723}
724
725static void
726ccw_device_release(struct device *dev)
727{
728 struct ccw_device *cdev;
729
730 cdev = to_ccwdev(dev);
6eff208f
CH
731 /* Release reference of parent subchannel. */
732 put_device(cdev->dev.parent);
1da177e4
LT
733 kfree(cdev->private);
734 kfree(cdev);
735}
736
7674da77
CH
737static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
738{
739 struct ccw_device *cdev;
740
741 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
742 if (cdev) {
743 cdev->private = kzalloc(sizeof(struct ccw_device_private),
744 GFP_KERNEL | GFP_DMA);
745 if (cdev->private)
746 return cdev;
747 }
748 kfree(cdev);
749 return ERR_PTR(-ENOMEM);
750}
751
37de53bb
PO
752static void ccw_device_todo(struct work_struct *work);
753
7674da77
CH
754static int io_subchannel_initialize_dev(struct subchannel *sch,
755 struct ccw_device *cdev)
756{
2c3e7e15
SO
757 struct ccw_device_private *priv = cdev->private;
758 int ret;
759
760 priv->cdev = cdev;
761 priv->int_class = IRQIO_CIO;
762 priv->state = DEV_STATE_NOT_OPER;
763 priv->dev_id.devno = sch->schib.pmcw.dev;
764 priv->dev_id.ssid = sch->schid.ssid;
2c3e7e15
SO
765
766 INIT_WORK(&priv->todo_work, ccw_device_todo);
767 INIT_LIST_HEAD(&priv->cmb_list);
768 init_waitqueue_head(&priv->wait_q);
769 init_timer(&priv->timer);
770
771 atomic_set(&priv->onoff, 0);
772 cdev->ccwlock = sch->lock;
7674da77
CH
773 cdev->dev.parent = &sch->dev;
774 cdev->dev.release = ccw_device_release;
ef99516c 775 cdev->dev.groups = ccwdev_attr_groups;
7674da77
CH
776 /* Do first half of device_register. */
777 device_initialize(&cdev->dev);
2c3e7e15
SO
778 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
779 cdev->private->dev_id.devno);
780 if (ret)
781 goto out_put;
7674da77 782 if (!get_device(&sch->dev)) {
2c3e7e15
SO
783 ret = -ENODEV;
784 goto out_put;
7674da77 785 }
2c3e7e15
SO
786 priv->flags.initialized = 1;
787 spin_lock_irq(sch->lock);
788 sch_set_cdev(sch, cdev);
789 spin_unlock_irq(sch->lock);
7674da77 790 return 0;
2c3e7e15
SO
791
792out_put:
793 /* Release reference from device_initialize(). */
794 put_device(&cdev->dev);
795 return ret;
7674da77
CH
796}
797
798static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
799{
800 struct ccw_device *cdev;
801 int ret;
802
803 cdev = io_subchannel_allocate_dev(sch);
804 if (!IS_ERR(cdev)) {
805 ret = io_subchannel_initialize_dev(sch, cdev);
06739a8a 806 if (ret)
7674da77 807 cdev = ERR_PTR(ret);
7674da77
CH
808 }
809 return cdev;
810}
811
736b5db8 812static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
d7b5a4c9 813
d7b5a4c9
CH
814static void sch_create_and_recog_new_device(struct subchannel *sch)
815{
816 struct ccw_device *cdev;
817
818 /* Need to allocate a new ccw device. */
819 cdev = io_subchannel_create_ccwdev(sch);
820 if (IS_ERR(cdev)) {
821 /* OK, we did everything we could... */
822 css_sch_device_unregister(sch);
823 return;
824 }
d7b5a4c9 825 /* Start recognition for the new ccw device. */
736b5db8 826 io_subchannel_recog(cdev, sch);
d7b5a4c9
CH
827}
828
1da177e4
LT
829/*
830 * Register recognized device.
831 */
37de53bb 832static void io_subchannel_register(struct ccw_device *cdev)
1da177e4 833{
1da177e4 834 struct subchannel *sch;
a290156f 835 int ret, adjust_init_count = 1;
1da177e4
LT
836 unsigned long flags;
837
1da177e4 838 sch = to_subchannel(cdev->dev.parent);
5fb6b854
CH
839 /*
840 * Check if subchannel is still registered. It may have become
841 * unregistered if a machine check hit us after finishing
842 * device recognition but before the register work could be
843 * queued.
844 */
845 if (!device_is_registered(&sch->dev))
846 goto out_err;
82b7ac05 847 css_update_ssd_info(sch);
47af5518
CH
848 /*
849 * io_subchannel_register() will also be called after device
850 * recognition has been done for a boxed device (which will already
851 * be registered). We need to reprobe since we may now have sense id
852 * information.
853 */
d6a30761 854 if (device_is_registered(&cdev->dev)) {
47af5518
CH
855 if (!cdev->drv) {
856 ret = device_reprobe(&cdev->dev);
857 if (ret)
858 /* We can't do much here. */
139b83dd 859 CIO_MSG_EVENT(0, "device_reprobe() returned"
e556bbbd
CH
860 " %d for 0.%x.%04x\n", ret,
861 cdev->private->dev_id.ssid,
862 cdev->private->dev_id.devno);
47af5518 863 }
a290156f 864 adjust_init_count = 0;
1da177e4
LT
865 goto out;
866 }
fa1a8c23
CH
867 /*
868 * Now we know this subchannel will stay, we can throw
869 * our delayed uevent.
870 */
f67f129e 871 dev_set_uevent_suppress(&sch->dev, 0);
fa1a8c23 872 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1da177e4 873 /* make it known to the system */
2c3e7e15 874 ret = ccw_device_add(cdev);
1da177e4 875 if (ret) {
e556bbbd
CH
876 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
877 cdev->private->dev_id.ssid,
878 cdev->private->dev_id.devno, ret);
2ec22984 879 spin_lock_irqsave(sch->lock, flags);
db6a6423 880 sch_set_cdev(sch, NULL);
2ec22984 881 spin_unlock_irqrestore(sch->lock, flags);
6eff208f
CH
882 /* Release initial device reference. */
883 put_device(&cdev->dev);
5fb6b854 884 goto out_err;
1da177e4 885 }
1da177e4
LT
886out:
887 cdev->private->flags.recog_done = 1;
1da177e4 888 wake_up(&cdev->private->wait_q);
5fb6b854 889out_err:
a290156f 890 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
1da177e4
LT
891 wake_up(&ccw_device_init_wq);
892}
893
37de53bb 894static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
1da177e4 895{
1da177e4
LT
896 struct subchannel *sch;
897
46fbe4e4
PO
898 /* Get subchannel reference for local processing. */
899 if (!get_device(cdev->dev.parent))
900 return;
1da177e4 901 sch = to_subchannel(cdev->dev.parent);
6ab4879a 902 css_sch_device_unregister(sch);
46fbe4e4 903 /* Release subchannel reference for local processing. */
1da177e4
LT
904 put_device(&sch->dev);
905}
906
907/*
908 * subchannel recognition done. Called from the state machine.
909 */
910void
911io_subchannel_recog_done(struct ccw_device *cdev)
912{
1da177e4
LT
913 if (css_init_done == 0) {
914 cdev->private->flags.recog_done = 1;
915 return;
916 }
917 switch (cdev->private->state) {
47593bfa
SO
918 case DEV_STATE_BOXED:
919 /* Device did not respond in time. */
1da177e4
LT
920 case DEV_STATE_NOT_OPER:
921 cdev->private->flags.recog_done = 1;
37de53bb
PO
922 /* Remove device found not operational. */
923 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1da177e4
LT
924 if (atomic_dec_and_test(&ccw_device_init_count))
925 wake_up(&ccw_device_init_wq);
926 break;
1da177e4
LT
927 case DEV_STATE_OFFLINE:
928 /*
929 * We can't register the device in interrupt context so
930 * we schedule a work item.
931 */
37de53bb 932 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
1da177e4
LT
933 break;
934 }
935}
936
736b5db8 937static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1da177e4 938{
1da177e4
LT
939 /* Increase counter of devices currently in recognition. */
940 atomic_inc(&ccw_device_init_count);
941
942 /* Start async. device sensing. */
2ec22984 943 spin_lock_irq(sch->lock);
736b5db8 944 ccw_device_recognition(cdev);
2ec22984 945 spin_unlock_irq(sch->lock);
1da177e4
LT
946}
947
5d6e6b6f
PO
948static int ccw_device_move_to_sch(struct ccw_device *cdev,
949 struct subchannel *sch)
d7b5a4c9 950{
5d6e6b6f 951 struct subchannel *old_sch;
0c609fca 952 int rc, old_enabled = 0;
d7b5a4c9 953
5d6e6b6f
PO
954 old_sch = to_subchannel(cdev->dev.parent);
955 /* Obtain child reference for new parent. */
6eff208f 956 if (!get_device(&sch->dev))
5d6e6b6f 957 return -ENODEV;
0c609fca
SO
958
959 if (!sch_is_pseudo_sch(old_sch)) {
960 spin_lock_irq(old_sch->lock);
961 old_enabled = old_sch->schib.pmcw.ena;
962 rc = 0;
963 if (old_enabled)
964 rc = cio_disable_subchannel(old_sch);
965 spin_unlock_irq(old_sch->lock);
966 if (rc == -EBUSY) {
967 /* Release child reference for new parent. */
968 put_device(&sch->dev);
969 return rc;
970 }
971 }
972
d7b5a4c9 973 mutex_lock(&sch->reg_mutex);
ffa6a705 974 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9
CH
975 mutex_unlock(&sch->reg_mutex);
976 if (rc) {
5d6e6b6f 977 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
d7b5a4c9
CH
978 cdev->private->dev_id.ssid,
979 cdev->private->dev_id.devno, sch->schid.ssid,
5d6e6b6f 980 sch->schib.pmcw.dev, rc);
0c609fca
SO
981 if (old_enabled) {
982 /* Try to reenable the old subchannel. */
983 spin_lock_irq(old_sch->lock);
984 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
985 spin_unlock_irq(old_sch->lock);
986 }
5d6e6b6f 987 /* Release child reference for new parent. */
6eff208f 988 put_device(&sch->dev);
5d6e6b6f 989 return rc;
d7b5a4c9 990 }
5d6e6b6f
PO
991 /* Clean up old subchannel. */
992 if (!sch_is_pseudo_sch(old_sch)) {
993 spin_lock_irq(old_sch->lock);
994 sch_set_cdev(old_sch, NULL);
5d6e6b6f
PO
995 spin_unlock_irq(old_sch->lock);
996 css_schedule_eval(old_sch->schid);
d7b5a4c9 997 }
5d6e6b6f
PO
998 /* Release child reference for old parent. */
999 put_device(&old_sch->dev);
1000 /* Initialize new subchannel. */
1001 spin_lock_irq(sch->lock);
5d6e6b6f
PO
1002 cdev->ccwlock = sch->lock;
1003 if (!sch_is_pseudo_sch(sch))
1004 sch_set_cdev(sch, cdev);
1005 spin_unlock_irq(sch->lock);
1006 if (!sch_is_pseudo_sch(sch))
1007 css_update_ssd_info(sch);
1008 return 0;
1009}
1010
1011static int ccw_device_move_to_orph(struct ccw_device *cdev)
1012{
1013 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1014 struct channel_subsystem *css = to_css(sch->dev.parent);
1015
1016 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
d7b5a4c9
CH
1017}
1018
602b20f2
CH
1019static void io_subchannel_irq(struct subchannel *sch)
1020{
1021 struct ccw_device *cdev;
1022
db6a6423 1023 cdev = sch_get_cdev(sch);
602b20f2 1024
efd986db
SO
1025 CIO_TRACE_EVENT(6, "IRQ");
1026 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
602b20f2
CH
1027 if (cdev)
1028 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
de400d6b 1029 else
420f42ec 1030 inc_irq_stat(IRQIO_CIO);
602b20f2
CH
1031}
1032
13952ec1
SO
1033void io_subchannel_init_config(struct subchannel *sch)
1034{
1035 memset(&sch->config, 0, sizeof(sch->config));
1036 sch->config.csense = 1;
13952ec1
SO
1037}
1038
0ae7a7b2
CH
1039static void io_subchannel_init_fields(struct subchannel *sch)
1040{
1041 if (cio_is_console(sch->schid))
1042 sch->opm = 0xff;
1043 else
1044 sch->opm = chp_get_sch_opm(sch);
1045 sch->lpm = sch->schib.pmcw.pam & sch->opm;
3a3fc29a 1046 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
0ae7a7b2
CH
1047
1048 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1049 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1050 sch->schib.pmcw.dev, sch->schid.ssid,
1051 sch->schid.sch_no, sch->schib.pmcw.pim,
1052 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
13952ec1
SO
1053
1054 io_subchannel_init_config(sch);
0ae7a7b2
CH
1055}
1056
90ed2b69
CH
1057/*
1058 * Note: We always return 0 so that we bind to the device even on error.
1059 * This is needed so that our remove function is called on unregister.
1060 */
0ae7a7b2 1061static int io_subchannel_probe(struct subchannel *sch)
1da177e4 1062{
f92519e8 1063 struct io_subchannel_private *io_priv;
1da177e4
LT
1064 struct ccw_device *cdev;
1065 int rc;
1da177e4 1066
6d7c5afc 1067 if (cio_is_console(sch->schid)) {
7e9db9ea
CH
1068 rc = sysfs_create_group(&sch->dev.kobj,
1069 &io_subchannel_attr_group);
1070 if (rc)
1071 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1072 "attributes for subchannel "
1073 "0.%x.%04x (rc=%d)\n",
1074 sch->schid.ssid, sch->schid.sch_no, rc);
1da177e4 1075 /*
6d7c5afc 1076 * The console subchannel already has an associated ccw_device.
7e9db9ea 1077 * Throw the delayed uevent for the subchannel, register
6d7c5afc 1078 * the ccw_device and exit.
1da177e4 1079 */
f67f129e 1080 dev_set_uevent_suppress(&sch->dev, 0);
7e9db9ea 1081 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
6d7c5afc 1082 cdev = sch_get_cdev(sch);
2c3e7e15 1083 rc = ccw_device_add(cdev);
afdfed0f
SO
1084 if (rc) {
1085 /* Release online reference. */
1086 put_device(&cdev->dev);
1087 goto out_schedule;
1088 }
0ad8f714
SO
1089 if (atomic_dec_and_test(&ccw_device_init_count))
1090 wake_up(&ccw_device_init_wq);
1da177e4
LT
1091 return 0;
1092 }
0ae7a7b2 1093 io_subchannel_init_fields(sch);
f444cc0e
SO
1094 rc = cio_commit_config(sch);
1095 if (rc)
1096 goto out_schedule;
7e9db9ea
CH
1097 rc = sysfs_create_group(&sch->dev.kobj,
1098 &io_subchannel_attr_group);
1099 if (rc)
90ed2b69 1100 goto out_schedule;
cd6b4f27 1101 /* Allocate I/O subchannel private data. */
f92519e8
SO
1102 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1103 if (!io_priv)
48e4c385 1104 goto out_schedule;
f92519e8
SO
1105
1106 set_io_private(sch, io_priv);
5d6e6b6f 1107 css_schedule_eval(sch->schid);
7e9db9ea 1108 return 0;
48e4c385 1109
90ed2b69 1110out_schedule:
390935ac
PO
1111 spin_lock_irq(sch->lock);
1112 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1113 spin_unlock_irq(sch->lock);
90ed2b69 1114 return 0;
1da177e4
LT
1115}
1116
1da177e4 1117static int
8bbace7e 1118io_subchannel_remove (struct subchannel *sch)
1da177e4 1119{
f92519e8 1120 struct io_subchannel_private *io_priv = to_io_private(sch);
1da177e4 1121 struct ccw_device *cdev;
1da177e4 1122
db6a6423
CH
1123 cdev = sch_get_cdev(sch);
1124 if (!cdev)
48e4c385 1125 goto out_free;
6e9a0f67 1126 io_subchannel_quiesce(sch);
1da177e4 1127 /* Set ccw device to not operational and drop reference. */
7a8ad100 1128 spin_lock_irq(cdev->ccwlock);
db6a6423 1129 sch_set_cdev(sch, NULL);
f92519e8 1130 set_io_private(sch, NULL);
1da177e4 1131 cdev->private->state = DEV_STATE_NOT_OPER;
7a8ad100 1132 spin_unlock_irq(cdev->ccwlock);
ef99516c 1133 ccw_device_unregister(cdev);
48e4c385 1134out_free:
f92519e8 1135 kfree(io_priv);
7e9db9ea 1136 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1137 return 0;
1138}
1139
602b20f2 1140static void io_subchannel_verify(struct subchannel *sch)
1da177e4
LT
1141{
1142 struct ccw_device *cdev;
1143
db6a6423 1144 cdev = sch_get_cdev(sch);
1da177e4
LT
1145 if (cdev)
1146 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1147}
1148
c820de39
CH
1149static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1150{
1151 struct ccw_device *cdev;
1152
1153 cdev = sch_get_cdev(sch);
1154 if (!cdev)
1155 return;
4257aaec
PO
1156 if (cio_update_schib(sch))
1157 goto err;
1158 /* Check for I/O on path. */
1159 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1160 goto out;
1161 if (cdev->private->state == DEV_STATE_ONLINE) {
1162 ccw_device_kill_io(cdev);
1163 goto out;
1164 }
1165 if (cio_clear(sch))
1166 goto err;
1167out:
1168 /* Trigger path verification. */
1169 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1170 return;
c820de39 1171
4257aaec
PO
1172err:
1173 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
c820de39
CH
1174}
1175
99611f87
CH
1176static int io_subchannel_chp_event(struct subchannel *sch,
1177 struct chp_link *link, int event)
c820de39 1178{
585b954e 1179 struct ccw_device *cdev = sch_get_cdev(sch);
c820de39 1180 int mask;
c820de39 1181
99611f87 1182 mask = chp_ssd_get_mask(&sch->ssd_info, link);
c820de39
CH
1183 if (!mask)
1184 return 0;
1185 switch (event) {
1186 case CHP_VARY_OFF:
1187 sch->opm &= ~mask;
1188 sch->lpm &= ~mask;
585b954e
SO
1189 if (cdev)
1190 cdev->private->path_gone_mask |= mask;
c820de39
CH
1191 io_subchannel_terminate_path(sch, mask);
1192 break;
1193 case CHP_VARY_ON:
1194 sch->opm |= mask;
1195 sch->lpm |= mask;
585b954e
SO
1196 if (cdev)
1197 cdev->private->path_new_mask |= mask;
c820de39
CH
1198 io_subchannel_verify(sch);
1199 break;
1200 case CHP_OFFLINE:
cdb912a4 1201 if (cio_update_schib(sch))
c820de39 1202 return -ENODEV;
585b954e
SO
1203 if (cdev)
1204 cdev->private->path_gone_mask |= mask;
c820de39
CH
1205 io_subchannel_terminate_path(sch, mask);
1206 break;
1207 case CHP_ONLINE:
cdb912a4
SO
1208 if (cio_update_schib(sch))
1209 return -ENODEV;
c820de39 1210 sch->lpm |= mask & sch->opm;
585b954e
SO
1211 if (cdev)
1212 cdev->private->path_new_mask |= mask;
c820de39
CH
1213 io_subchannel_verify(sch);
1214 break;
1215 }
1216 return 0;
1217}
1218
6e9a0f67 1219static void io_subchannel_quiesce(struct subchannel *sch)
1da177e4 1220{
1da177e4
LT
1221 struct ccw_device *cdev;
1222 int ret;
1223
56e6b796 1224 spin_lock_irq(sch->lock);
db6a6423 1225 cdev = sch_get_cdev(sch);
a8237fc4 1226 if (cio_is_console(sch->schid))
56e6b796 1227 goto out_unlock;
1da177e4 1228 if (!sch->schib.pmcw.ena)
56e6b796 1229 goto out_unlock;
1da177e4
LT
1230 ret = cio_disable_subchannel(sch);
1231 if (ret != -EBUSY)
56e6b796 1232 goto out_unlock;
1da177e4 1233 if (cdev->handler)
56e6b796
SO
1234 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1235 while (ret == -EBUSY) {
1236 cdev->private->state = DEV_STATE_QUIESCE;
376ae475 1237 cdev->private->iretry = 255;
56e6b796
SO
1238 ret = ccw_device_cancel_halt_clear(cdev);
1239 if (ret == -EBUSY) {
1240 ccw_device_set_timeout(cdev, HZ/10);
1241 spin_unlock_irq(sch->lock);
1242 wait_event(cdev->private->wait_q,
1243 cdev->private->state != DEV_STATE_QUIESCE);
1244 spin_lock_irq(sch->lock);
1245 }
1246 ret = cio_disable_subchannel(sch);
1da177e4 1247 }
56e6b796
SO
1248out_unlock:
1249 spin_unlock_irq(sch->lock);
1da177e4
LT
1250}
1251
6e9a0f67
SO
1252static void io_subchannel_shutdown(struct subchannel *sch)
1253{
1254 io_subchannel_quiesce(sch);
1255}
1256
c820de39
CH
1257static int device_is_disconnected(struct ccw_device *cdev)
1258{
1259 if (!cdev)
1260 return 0;
1261 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1262 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1263}
1264
1265static int recovery_check(struct device *dev, void *data)
1266{
1267 struct ccw_device *cdev = to_ccwdev(dev);
1268 int *redo = data;
1269
1270 spin_lock_irq(cdev->ccwlock);
1271 switch (cdev->private->state) {
1272 case DEV_STATE_DISCONNECTED:
1273 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1274 cdev->private->dev_id.ssid,
1275 cdev->private->dev_id.devno);
1276 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1277 *redo = 1;
1278 break;
1279 case DEV_STATE_DISCONNECTED_SENSE_ID:
1280 *redo = 1;
1281 break;
1282 }
1283 spin_unlock_irq(cdev->ccwlock);
1284
1285 return 0;
1286}
1287
1288static void recovery_work_func(struct work_struct *unused)
1289{
1290 int redo = 0;
1291
1292 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1293 if (redo) {
1294 spin_lock_irq(&recovery_lock);
1295 if (!timer_pending(&recovery_timer)) {
1296 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1297 recovery_phase++;
1298 mod_timer(&recovery_timer, jiffies +
1299 recovery_delay[recovery_phase] * HZ);
1300 }
1301 spin_unlock_irq(&recovery_lock);
1302 } else
1303 CIO_MSG_EVENT(4, "recovery: end\n");
1304}
1305
1306static DECLARE_WORK(recovery_work, recovery_work_func);
1307
1308static void recovery_func(unsigned long data)
1309{
1310 /*
1311 * We can't do our recovery in softirq context and it's not
1312 * performance critical, so we schedule it.
1313 */
1314 schedule_work(&recovery_work);
1315}
1316
1317static void ccw_device_schedule_recovery(void)
1318{
1319 unsigned long flags;
1320
1321 CIO_MSG_EVENT(4, "recovery: schedule\n");
1322 spin_lock_irqsave(&recovery_lock, flags);
1323 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1324 recovery_phase = 0;
1325 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1326 }
1327 spin_unlock_irqrestore(&recovery_lock, flags);
1328}
1329
ecf5d9ef
PO
1330static int purge_fn(struct device *dev, void *data)
1331{
1332 struct ccw_device *cdev = to_ccwdev(dev);
37de53bb 1333 struct ccw_dev_id *id = &cdev->private->dev_id;
ecf5d9ef
PO
1334
1335 spin_lock_irq(cdev->ccwlock);
37de53bb 1336 if (is_blacklisted(id->ssid, id->devno) &&
a2fc8485
PO
1337 (cdev->private->state == DEV_STATE_OFFLINE) &&
1338 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
37de53bb
PO
1339 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1340 id->devno);
1341 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
a2fc8485 1342 atomic_set(&cdev->private->onoff, 0);
37de53bb 1343 }
ecf5d9ef 1344 spin_unlock_irq(cdev->ccwlock);
ecf5d9ef
PO
1345 /* Abort loop in case of pending signal. */
1346 if (signal_pending(current))
1347 return -EINTR;
1348
1349 return 0;
1350}
1351
1352/**
1353 * ccw_purge_blacklisted - purge unused, blacklisted devices
1354 *
1355 * Unregister all ccw devices that are offline and on the blacklist.
1356 */
1357int ccw_purge_blacklisted(void)
1358{
1359 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1360 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1361 return 0;
1362}
1363
6afcc775 1364void ccw_device_set_disconnected(struct ccw_device *cdev)
c820de39
CH
1365{
1366 if (!cdev)
1367 return;
1368 ccw_device_set_timeout(cdev, 0);
1369 cdev->private->flags.fake_irb = 0;
1370 cdev->private->state = DEV_STATE_DISCONNECTED;
1371 if (cdev->online)
1372 ccw_device_schedule_recovery();
1373}
1374
91c36919
PO
1375void ccw_device_set_notoper(struct ccw_device *cdev)
1376{
1377 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1378
1379 CIO_TRACE_EVENT(2, "notoper");
b9d3aed7 1380 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
91c36919
PO
1381 ccw_device_set_timeout(cdev, 0);
1382 cio_disable_subchannel(sch);
1383 cdev->private->state = DEV_STATE_NOT_OPER;
1384}
1385
5d6e6b6f
PO
1386enum io_sch_action {
1387 IO_SCH_UNREG,
1388 IO_SCH_ORPH_UNREG,
1389 IO_SCH_ATTACH,
1390 IO_SCH_UNREG_ATTACH,
1391 IO_SCH_ORPH_ATTACH,
1392 IO_SCH_REPROBE,
1393 IO_SCH_VERIFY,
1394 IO_SCH_DISC,
1395 IO_SCH_NOP,
1396};
1397
1398static enum io_sch_action sch_get_action(struct subchannel *sch)
1399{
1400 struct ccw_device *cdev;
1401
1402 cdev = sch_get_cdev(sch);
1403 if (cio_update_schib(sch)) {
1404 /* Not operational. */
1405 if (!cdev)
1406 return IO_SCH_UNREG;
76e6fb4b 1407 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
5d6e6b6f
PO
1408 return IO_SCH_UNREG;
1409 return IO_SCH_ORPH_UNREG;
1410 }
1411 /* Operational. */
1412 if (!cdev)
1413 return IO_SCH_ATTACH;
1414 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
76e6fb4b 1415 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
5d6e6b6f
PO
1416 return IO_SCH_UNREG_ATTACH;
1417 return IO_SCH_ORPH_ATTACH;
1418 }
1419 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
76e6fb4b 1420 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
5d6e6b6f
PO
1421 return IO_SCH_UNREG;
1422 return IO_SCH_DISC;
1423 }
1424 if (device_is_disconnected(cdev))
1425 return IO_SCH_REPROBE;
31370f75 1426 if (cdev->online && !cdev->private->flags.resuming)
5d6e6b6f 1427 return IO_SCH_VERIFY;
43d0be75
SO
1428 if (cdev->private->state == DEV_STATE_NOT_OPER)
1429 return IO_SCH_UNREG_ATTACH;
5d6e6b6f
PO
1430 return IO_SCH_NOP;
1431}
1432
1433/**
1434 * io_subchannel_sch_event - process subchannel event
1435 * @sch: subchannel
1436 * @process: non-zero if function is called in process context
1437 *
1438 * An unspecified event occurred for this subchannel. Adjust data according
1439 * to the current operational state of the subchannel and device. Return
1440 * zero when the event has been handled sufficiently or -EAGAIN when this
1441 * function should be called again in process context.
1442 */
1443static int io_subchannel_sch_event(struct subchannel *sch, int process)
c820de39 1444{
c820de39 1445 unsigned long flags;
c820de39 1446 struct ccw_device *cdev;
5d6e6b6f
PO
1447 struct ccw_dev_id dev_id;
1448 enum io_sch_action action;
1449 int rc = -EAGAIN;
c820de39
CH
1450
1451 spin_lock_irqsave(sch->lock, flags);
5d6e6b6f
PO
1452 if (!device_is_registered(&sch->dev))
1453 goto out_unlock;
390935ac
PO
1454 if (work_pending(&sch->todo_work))
1455 goto out_unlock;
37de53bb
PO
1456 cdev = sch_get_cdev(sch);
1457 if (cdev && work_pending(&cdev->private->todo_work))
1458 goto out_unlock;
5d6e6b6f
PO
1459 action = sch_get_action(sch);
1460 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1461 sch->schid.ssid, sch->schid.sch_no, process,
1462 action);
1463 /* Perform immediate actions while holding the lock. */
5d6e6b6f
PO
1464 switch (action) {
1465 case IO_SCH_REPROBE:
1466 /* Trigger device recognition. */
1467 ccw_device_trigger_reprobe(cdev);
1468 rc = 0;
1469 goto out_unlock;
1470 case IO_SCH_VERIFY:
1471 /* Trigger path verification. */
1472 io_subchannel_verify(sch);
1473 rc = 0;
1474 goto out_unlock;
1475 case IO_SCH_DISC:
1476 ccw_device_set_disconnected(cdev);
1477 rc = 0;
1478 goto out_unlock;
1479 case IO_SCH_ORPH_UNREG:
1480 case IO_SCH_ORPH_ATTACH:
1481 ccw_device_set_disconnected(cdev);
1482 break;
1483 case IO_SCH_UNREG_ATTACH:
1484 case IO_SCH_UNREG:
16d2ce27
SO
1485 if (!cdev)
1486 break;
1487 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1488 /*
1489 * Note: delayed work triggered by this event
1490 * and repeated calls to sch_event are synchronized
1491 * by the above check for work_pending(cdev).
1492 */
1493 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1494 } else
5d6e6b6f
PO
1495 ccw_device_set_notoper(cdev);
1496 break;
1497 case IO_SCH_NOP:
1498 rc = 0;
1499 goto out_unlock;
1500 default:
1501 break;
c820de39 1502 }
5d6e6b6f
PO
1503 spin_unlock_irqrestore(sch->lock, flags);
1504 /* All other actions require process context. */
1505 if (!process)
1506 goto out;
1507 /* Handle attached ccw device. */
1508 switch (action) {
1509 case IO_SCH_ORPH_UNREG:
1510 case IO_SCH_ORPH_ATTACH:
1511 /* Move ccw device to orphanage. */
1512 rc = ccw_device_move_to_orph(cdev);
1513 if (rc)
1514 goto out;
c820de39 1515 break;
5d6e6b6f 1516 case IO_SCH_UNREG_ATTACH:
3368ba25 1517 spin_lock_irqsave(sch->lock, flags);
74b6127e
SO
1518 if (cdev->private->flags.resuming) {
1519 /* Device will be handled later. */
1520 rc = 0;
3368ba25 1521 goto out_unlock;
74b6127e 1522 }
3368ba25
SO
1523 sch_set_cdev(sch, NULL);
1524 spin_unlock_irqrestore(sch->lock, flags);
5d6e6b6f 1525 /* Unregister ccw device. */
74b6127e 1526 ccw_device_unregister(cdev);
c820de39 1527 break;
5d6e6b6f 1528 default:
c820de39
CH
1529 break;
1530 }
5d6e6b6f 1531 /* Handle subchannel. */
c820de39 1532 switch (action) {
5d6e6b6f
PO
1533 case IO_SCH_ORPH_UNREG:
1534 case IO_SCH_UNREG:
0d01bb89
SO
1535 if (!cdev || !cdev->private->flags.resuming)
1536 css_sch_device_unregister(sch);
c820de39 1537 break;
5d6e6b6f
PO
1538 case IO_SCH_ORPH_ATTACH:
1539 case IO_SCH_UNREG_ATTACH:
1540 case IO_SCH_ATTACH:
1541 dev_id.ssid = sch->schid.ssid;
1542 dev_id.devno = sch->schib.pmcw.dev;
1543 cdev = get_ccwdev_by_dev_id(&dev_id);
1544 if (!cdev) {
1545 sch_create_and_recog_new_device(sch);
1546 break;
1547 }
1548 rc = ccw_device_move_to_sch(cdev, sch);
1549 if (rc) {
1550 /* Release reference from get_ccwdev_by_dev_id() */
1551 put_device(&cdev->dev);
1552 goto out;
1553 }
1554 spin_lock_irqsave(sch->lock, flags);
c820de39 1555 ccw_device_trigger_reprobe(cdev);
5d6e6b6f
PO
1556 spin_unlock_irqrestore(sch->lock, flags);
1557 /* Release reference from get_ccwdev_by_dev_id() */
1558 put_device(&cdev->dev);
91c36919 1559 break;
c820de39
CH
1560 default:
1561 break;
1562 }
5d6e6b6f 1563 return 0;
c820de39 1564
5d6e6b6f
PO
1565out_unlock:
1566 spin_unlock_irqrestore(sch->lock, flags);
1567out:
1568 return rc;
c820de39
CH
1569}
1570
137a14f4
SO
1571static void ccw_device_set_int_class(struct ccw_device *cdev)
1572{
1573 struct ccw_driver *cdrv = cdev->drv;
1574
1575 /* Note: we interpret class 0 in this context as an uninitialized
1576 * field since it translates to a non-I/O interrupt class. */
1577 if (cdrv->int_class != 0)
1578 cdev->private->int_class = cdrv->int_class;
1579 else
1580 cdev->private->int_class = IRQIO_CIO;
1581}
1582
1da177e4 1583#ifdef CONFIG_CCW_CONSOLE
1e532096 1584int __init ccw_device_enable_console(struct ccw_device *cdev)
1da177e4 1585{
1e532096 1586 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1da177e4
LT
1587 int rc;
1588
1e532096
SO
1589 if (!cdev->drv || !cdev->handler)
1590 return -EINVAL;
1591
0ae7a7b2 1592 io_subchannel_init_fields(sch);
f444cc0e
SO
1593 rc = cio_commit_config(sch);
1594 if (rc)
1595 return rc;
0ae7a7b2 1596 sch->driver = &io_subchannel_driver;
736b5db8 1597 io_subchannel_recog(cdev, sch);
1da177e4
LT
1598 /* Now wait for the async. recognition to come to an end. */
1599 spin_lock_irq(cdev->ccwlock);
1600 while (!dev_fsm_final_state(cdev))
188561a4
SO
1601 ccw_device_wait_idle(cdev);
1602
afdfed0f
SO
1603 /* Hold on to an extra reference while device is online. */
1604 get_device(&cdev->dev);
1605 rc = ccw_device_online(cdev);
1606 if (rc)
1da177e4 1607 goto out_unlock;
afdfed0f 1608
1da177e4 1609 while (!dev_fsm_final_state(cdev))
188561a4
SO
1610 ccw_device_wait_idle(cdev);
1611
afdfed0f
SO
1612 if (cdev->private->state == DEV_STATE_ONLINE)
1613 cdev->online = 1;
1614 else
1615 rc = -EIO;
1da177e4
LT
1616out_unlock:
1617 spin_unlock_irq(cdev->ccwlock);
afdfed0f
SO
1618 if (rc) /* Give up online reference since onlining failed. */
1619 put_device(&cdev->dev);
736b5db8 1620 return rc;
1da177e4
LT
1621}
1622
1e532096 1623struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1da177e4 1624{
863fc849 1625 struct io_subchannel_private *io_priv;
afdfed0f 1626 struct ccw_device *cdev;
1da177e4 1627 struct subchannel *sch;
1da177e4 1628
1da177e4 1629 sch = cio_probe_console();
afdfed0f
SO
1630 if (IS_ERR(sch))
1631 return ERR_CAST(sch);
863fc849
SO
1632
1633 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1634 if (!io_priv) {
1635 put_device(&sch->dev);
1636 return ERR_PTR(-ENOMEM);
1637 }
2c3e7e15 1638 set_io_private(sch, io_priv);
afdfed0f
SO
1639 cdev = io_subchannel_create_ccwdev(sch);
1640 if (IS_ERR(cdev)) {
1641 put_device(&sch->dev);
1642 kfree(io_priv);
1643 return cdev;
1644 }
2253e8d7 1645 cdev->drv = drv;
137a14f4 1646 ccw_device_set_int_class(cdev);
afdfed0f 1647 return cdev;
1da177e4 1648}
1f4e7eda 1649
1e532096
SO
1650void __init ccw_device_destroy_console(struct ccw_device *cdev)
1651{
1652 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1653 struct io_subchannel_private *io_priv = to_io_private(sch);
1654
1655 set_io_private(sch, NULL);
1656 put_device(&sch->dev);
1657 put_device(&cdev->dev);
1658 kfree(io_priv);
1659}
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 */
137a14f4 1744 ccw_device_set_int_class(cdev);
1da177e4 1745 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1da177e4 1746 if (ret) {
d2c993d8 1747 cdev->drv = NULL;
420f42ec 1748 cdev->private->int_class = IRQIO_CIO;
1da177e4
LT
1749 return ret;
1750 }
1751
1752 return 0;
1753}
1754
74bd0d85 1755static int ccw_device_remove(struct device *dev)
1da177e4
LT
1756{
1757 struct ccw_device *cdev = to_ccwdev(dev);
1758 struct ccw_driver *cdrv = cdev->drv;
1759 int ret;
1760
1da177e4
LT
1761 if (cdrv->remove)
1762 cdrv->remove(cdev);
74bd0d85
SO
1763
1764 spin_lock_irq(cdev->ccwlock);
1da177e4
LT
1765 if (cdev->online) {
1766 cdev->online = 0;
1da177e4
LT
1767 ret = ccw_device_offline(cdev);
1768 spin_unlock_irq(cdev->ccwlock);
1769 if (ret == 0)
1770 wait_event(cdev->private->wait_q,
1771 dev_fsm_final_state(cdev));
1772 else
139b83dd 1773 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
1774 "device 0.%x.%04x\n",
1775 ret, cdev->private->dev_id.ssid,
1776 cdev->private->dev_id.devno);
9cd67421
CH
1777 /* Give up reference obtained in ccw_device_set_online(). */
1778 put_device(&cdev->dev);
74bd0d85 1779 spin_lock_irq(cdev->ccwlock);
1da177e4
LT
1780 }
1781 ccw_device_set_timeout(cdev, 0);
d2c993d8 1782 cdev->drv = NULL;
420f42ec 1783 cdev->private->int_class = IRQIO_CIO;
74bd0d85 1784 spin_unlock_irq(cdev->ccwlock);
a6ef1565
SO
1785 __disable_cmf(cdev);
1786
1da177e4
LT
1787 return 0;
1788}
1789
958974fb
CH
1790static void ccw_device_shutdown(struct device *dev)
1791{
1792 struct ccw_device *cdev;
1793
1794 cdev = to_ccwdev(dev);
1795 if (cdev->drv && cdev->drv->shutdown)
1796 cdev->drv->shutdown(cdev);
1bc6664b 1797 __disable_cmf(cdev);
958974fb
CH
1798}
1799
823d494a
SO
1800static int ccw_device_pm_prepare(struct device *dev)
1801{
1802 struct ccw_device *cdev = to_ccwdev(dev);
1803
37de53bb 1804 if (work_pending(&cdev->private->todo_work))
823d494a
SO
1805 return -EAGAIN;
1806 /* Fail while device is being set online/offline. */
1807 if (atomic_read(&cdev->private->onoff))
1808 return -EAGAIN;
1809
1810 if (cdev->online && cdev->drv && cdev->drv->prepare)
1811 return cdev->drv->prepare(cdev);
1812
1813 return 0;
1814}
1815
1816static void ccw_device_pm_complete(struct device *dev)
1817{
1818 struct ccw_device *cdev = to_ccwdev(dev);
1819
1820 if (cdev->online && cdev->drv && cdev->drv->complete)
1821 cdev->drv->complete(cdev);
1822}
1823
1824static int ccw_device_pm_freeze(struct device *dev)
1825{
1826 struct ccw_device *cdev = to_ccwdev(dev);
1827 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1828 int ret, cm_enabled;
1829
1830 /* Fail suspend while device is in transistional state. */
1831 if (!dev_fsm_final_state(cdev))
1832 return -EAGAIN;
1833 if (!cdev->online)
1834 return 0;
1835 if (cdev->drv && cdev->drv->freeze) {
1836 ret = cdev->drv->freeze(cdev);
1837 if (ret)
1838 return ret;
1839 }
1840
1841 spin_lock_irq(sch->lock);
1842 cm_enabled = cdev->private->cmb != NULL;
1843 spin_unlock_irq(sch->lock);
1844 if (cm_enabled) {
1845 /* Don't have the css write on memory. */
1846 ret = ccw_set_cmf(cdev, 0);
1847 if (ret)
1848 return ret;
1849 }
1850 /* From here on, disallow device driver I/O. */
1851 spin_lock_irq(sch->lock);
1852 ret = cio_disable_subchannel(sch);
1853 spin_unlock_irq(sch->lock);
1854
1855 return ret;
1856}
1857
1858static int ccw_device_pm_thaw(struct device *dev)
1859{
1860 struct ccw_device *cdev = to_ccwdev(dev);
1861 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1862 int ret, cm_enabled;
1863
1864 if (!cdev->online)
1865 return 0;
1866
1867 spin_lock_irq(sch->lock);
1868 /* Allow device driver I/O again. */
1869 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1870 cm_enabled = cdev->private->cmb != NULL;
1871 spin_unlock_irq(sch->lock);
1872 if (ret)
1873 return ret;
1874
1875 if (cm_enabled) {
1876 ret = ccw_set_cmf(cdev, 1);
1877 if (ret)
1878 return ret;
1879 }
1880
1881 if (cdev->drv && cdev->drv->thaw)
1882 ret = cdev->drv->thaw(cdev);
1883
1884 return ret;
1885}
1886
1887static void __ccw_device_pm_restore(struct ccw_device *cdev)
1888{
1889 struct subchannel *sch = to_subchannel(cdev->dev.parent);
823d494a 1890
0d01bb89
SO
1891 spin_lock_irq(sch->lock);
1892 if (cio_is_console(sch->schid)) {
1893 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1894 goto out_unlock;
1895 }
823d494a
SO
1896 /*
1897 * While we were sleeping, devices may have gone or become
1898 * available again. Kick re-detection.
1899 */
823d494a 1900 cdev->private->flags.resuming = 1;
b17295e6 1901 cdev->private->path_new_mask = LPM_ANYPATH;
817e5000 1902 css_sched_sch_todo(sch, SCH_TODO_EVAL);
0d01bb89 1903 spin_unlock_irq(sch->lock);
817e5000 1904 css_wait_for_slow_path();
0d01bb89
SO
1905
1906 /* cdev may have been moved to a different subchannel. */
1907 sch = to_subchannel(cdev->dev.parent);
1908 spin_lock_irq(sch->lock);
1909 if (cdev->private->state != DEV_STATE_ONLINE &&
1910 cdev->private->state != DEV_STATE_OFFLINE)
1911 goto out_unlock;
1912
736b5db8 1913 ccw_device_recognition(cdev);
823d494a 1914 spin_unlock_irq(sch->lock);
823d494a
SO
1915 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1916 cdev->private->state == DEV_STATE_DISCONNECTED);
0d01bb89
SO
1917 spin_lock_irq(sch->lock);
1918
1919out_unlock:
823d494a 1920 cdev->private->flags.resuming = 0;
0d01bb89 1921 spin_unlock_irq(sch->lock);
823d494a
SO
1922}
1923
1924static int resume_handle_boxed(struct ccw_device *cdev)
1925{
1926 cdev->private->state = DEV_STATE_BOXED;
76e6fb4b 1927 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
823d494a 1928 return 0;
37de53bb 1929 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
823d494a
SO
1930 return -ENODEV;
1931}
1932
1933static int resume_handle_disc(struct ccw_device *cdev)
1934{
1935 cdev->private->state = DEV_STATE_DISCONNECTED;
76e6fb4b 1936 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
823d494a 1937 return 0;
37de53bb 1938 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
823d494a
SO
1939 return -ENODEV;
1940}
1941
1942static int ccw_device_pm_restore(struct device *dev)
1943{
1944 struct ccw_device *cdev = to_ccwdev(dev);
0d01bb89
SO
1945 struct subchannel *sch;
1946 int ret = 0;
823d494a
SO
1947
1948 __ccw_device_pm_restore(cdev);
0d01bb89 1949 sch = to_subchannel(cdev->dev.parent);
823d494a 1950 spin_lock_irq(sch->lock);
0d01bb89 1951 if (cio_is_console(sch->schid))
823d494a 1952 goto out_restore;
0d01bb89 1953
823d494a
SO
1954 /* check recognition results */
1955 switch (cdev->private->state) {
1956 case DEV_STATE_OFFLINE:
0d01bb89
SO
1957 case DEV_STATE_ONLINE:
1958 cdev->private->flags.donotify = 0;
823d494a
SO
1959 break;
1960 case DEV_STATE_BOXED:
1961 ret = resume_handle_boxed(cdev);
823d494a 1962 if (ret)
0d01bb89 1963 goto out_unlock;
823d494a 1964 goto out_restore;
823d494a 1965 default:
0d01bb89
SO
1966 ret = resume_handle_disc(cdev);
1967 if (ret)
1968 goto out_unlock;
1969 goto out_restore;
823d494a
SO
1970 }
1971 /* check if the device type has changed */
1972 if (!ccw_device_test_sense_data(cdev)) {
1973 ccw_device_update_sense_data(cdev);
37de53bb 1974 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
823d494a
SO
1975 ret = -ENODEV;
1976 goto out_unlock;
1977 }
0d01bb89 1978 if (!cdev->online)
823d494a 1979 goto out_unlock;
823d494a 1980
0d01bb89
SO
1981 if (ccw_device_online(cdev)) {
1982 ret = resume_handle_disc(cdev);
1983 if (ret)
1984 goto out_unlock;
1985 goto out_restore;
1986 }
823d494a 1987 spin_unlock_irq(sch->lock);
823d494a 1988 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
0d01bb89
SO
1989 spin_lock_irq(sch->lock);
1990
1991 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1992 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1993 ret = -ENODEV;
1994 goto out_unlock;
823d494a 1995 }
0d01bb89
SO
1996
1997 /* reenable cmf, if needed */
1998 if (cdev->private->cmb) {
1999 spin_unlock_irq(sch->lock);
823d494a 2000 ret = ccw_set_cmf(cdev, 1);
0d01bb89 2001 spin_lock_irq(sch->lock);
823d494a 2002 if (ret) {
f014824e
SO
2003 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2004 "(rc=%d)\n", cdev->private->dev_id.ssid,
2005 cdev->private->dev_id.devno, ret);
823d494a
SO
2006 ret = 0;
2007 }
2008 }
2009
2010out_restore:
0d01bb89 2011 spin_unlock_irq(sch->lock);
823d494a
SO
2012 if (cdev->online && cdev->drv && cdev->drv->restore)
2013 ret = cdev->drv->restore(cdev);
823d494a
SO
2014 return ret;
2015
823d494a
SO
2016out_unlock:
2017 spin_unlock_irq(sch->lock);
2018 return ret;
2019}
2020
47145210 2021static const struct dev_pm_ops ccw_pm_ops = {
823d494a
SO
2022 .prepare = ccw_device_pm_prepare,
2023 .complete = ccw_device_pm_complete,
2024 .freeze = ccw_device_pm_freeze,
2025 .thaw = ccw_device_pm_thaw,
2026 .restore = ccw_device_pm_restore,
2027};
2028
d5ab5276 2029static struct bus_type ccw_bus_type = {
8bbace7e
CH
2030 .name = "ccw",
2031 .match = ccw_bus_match,
2032 .uevent = ccw_uevent,
2033 .probe = ccw_device_probe,
2034 .remove = ccw_device_remove,
958974fb 2035 .shutdown = ccw_device_shutdown,
823d494a 2036 .pm = &ccw_pm_ops,
8bbace7e
CH
2037};
2038
b2ffd8e9
CH
2039/**
2040 * ccw_driver_register() - register a ccw driver
2041 * @cdriver: driver to be registered
2042 *
2043 * This function is mainly a wrapper around driver_register().
2044 * Returns:
2045 * %0 on success and a negative error value on failure.
2046 */
2047int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
2048{
2049 struct device_driver *drv = &cdriver->driver;
2050
2051 drv->bus = &ccw_bus_type;
1da177e4
LT
2052
2053 return driver_register(drv);
2054}
2055
b2ffd8e9
CH
2056/**
2057 * ccw_driver_unregister() - deregister a ccw driver
2058 * @cdriver: driver to be deregistered
2059 *
2060 * This function is mainly a wrapper around driver_unregister().
2061 */
2062void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
2063{
2064 driver_unregister(&cdriver->driver);
2065}
2066
37de53bb
PO
2067static void ccw_device_todo(struct work_struct *work)
2068{
2069 struct ccw_device_private *priv;
2070 struct ccw_device *cdev;
2071 struct subchannel *sch;
2072 enum cdev_todo todo;
2073
2074 priv = container_of(work, struct ccw_device_private, todo_work);
2075 cdev = priv->cdev;
2076 sch = to_subchannel(cdev->dev.parent);
2077 /* Find out todo. */
2078 spin_lock_irq(cdev->ccwlock);
2079 todo = priv->todo;
2080 priv->todo = CDEV_TODO_NOTHING;
2081 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2082 priv->dev_id.ssid, priv->dev_id.devno, todo);
2083 spin_unlock_irq(cdev->ccwlock);
2084 /* Perform todo. */
2085 switch (todo) {
2086 case CDEV_TODO_ENABLE_CMF:
2087 cmf_reenable(cdev);
2088 break;
2089 case CDEV_TODO_REBIND:
2090 ccw_device_do_unbind_bind(cdev);
2091 break;
2092 case CDEV_TODO_REGISTER:
2093 io_subchannel_register(cdev);
2094 break;
2095 case CDEV_TODO_UNREG_EVAL:
2096 if (!sch_is_pseudo_sch(sch))
2097 css_schedule_eval(sch->schid);
2098 /* fall-through */
2099 case CDEV_TODO_UNREG:
2100 if (sch_is_pseudo_sch(sch))
2101 ccw_device_unregister(cdev);
2102 else
2103 ccw_device_call_sch_unregister(cdev);
2104 break;
2105 default:
2106 break;
2107 }
2108 /* Release workqueue ref. */
2109 put_device(&cdev->dev);
2110}
2111
2112/**
2113 * ccw_device_sched_todo - schedule ccw device operation
2114 * @cdev: ccw device
2115 * @todo: todo
2116 *
2117 * Schedule the operation identified by @todo to be performed on the slow path
2118 * workqueue. Do nothing if another operation with higher priority is already
2119 * scheduled. Needs to be called with ccwdev lock held.
2120 */
2121void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2122{
2123 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2124 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2125 todo);
2126 if (cdev->private->todo >= todo)
2127 return;
2128 cdev->private->todo = todo;
2129 /* Get workqueue ref. */
2130 if (!get_device(&cdev->dev))
2131 return;
be5d3823 2132 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
37de53bb
PO
2133 /* Already queued, release workqueue ref. */
2134 put_device(&cdev->dev);
2135 }
2136}
2137
fd0457a6
ME
2138/**
2139 * ccw_device_siosl() - initiate logging
2140 * @cdev: ccw device
2141 *
2142 * This function is used to invoke model-dependent logging within the channel
2143 * subsystem.
2144 */
2145int ccw_device_siosl(struct ccw_device *cdev)
2146{
2147 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2148
2149 return chsc_siosl(sch->schid);
2150}
2151EXPORT_SYMBOL_GPL(ccw_device_siosl);
2152
1da177e4
LT
2153MODULE_LICENSE("GPL");
2154EXPORT_SYMBOL(ccw_device_set_online);
2155EXPORT_SYMBOL(ccw_device_set_offline);
2156EXPORT_SYMBOL(ccw_driver_register);
2157EXPORT_SYMBOL(ccw_driver_unregister);
2158EXPORT_SYMBOL(get_ccwdev_by_busid);
This page took 1.159312 seconds and 5 git commands to generate.