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