]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/cio/device.c
Use i8253.c lock for PC speaker on MIPS, too.
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / cio / device.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
1da177e4
LT
4 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
4ce3b30c 8 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
9 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
1da177e4
LT
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/errno.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/device.h>
19#include <linux/workqueue.h>
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
4e57b681 23#include <asm/param.h> /* HZ */
1842f2b1 24#include <asm/cmb.h>
1da177e4
LT
25
26#include "cio.h"
d7b5a4c9 27#include "cio_debug.h"
1da177e4
LT
28#include "css.h"
29#include "device.h"
30#include "ioasm.h"
31
32/******************* bus type handling ***********************/
33
34/* The Linux driver model distinguishes between a bus type and
35 * the bus itself. Of course we only have one channel
36 * subsystem driver and one channel system per machine, but
37 * we still use the abstraction. T.R. says it's a good idea. */
38static int
39ccw_bus_match (struct device * dev, struct device_driver * drv)
40{
41 struct ccw_device *cdev = to_ccwdev(dev);
42 struct ccw_driver *cdrv = to_ccwdrv(drv);
43 const struct ccw_device_id *ids = cdrv->ids, *found;
44
45 if (!ids)
46 return 0;
47
48 found = ccw_device_id_match(ids, &cdev->id);
49 if (!found)
50 return 0;
51
52 cdev->id.driver_info = found->driver_info;
53
54 return 1;
55}
56
db0c2d59
PO
57/* Store modalias string delimited by prefix/suffix string into buffer with
58 * specified size. Return length of resulting string (excluding trailing '\0')
59 * even if string doesn't fit buffer (snprintf semantics). */
cfbe9bb2 60static int snprint_alias(char *buf, size_t size,
db0c2d59 61 struct ccw_device_id *id, const char *suffix)
1da177e4 62{
db0c2d59 63 int len;
1da177e4 64
cfbe9bb2 65 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
db0c2d59
PO
66 if (len > size)
67 return len;
68 buf += len;
69 size -= len;
1da177e4 70
db0c2d59
PO
71 if (id->dev_type != 0)
72 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
73 id->dev_model, suffix);
74 else
75 len += snprintf(buf, size, "dtdm%s", suffix);
1da177e4 76
db0c2d59
PO
77 return len;
78}
79
80/* Set up environment variables for ccw device uevent. Return 0 on success,
81 * non-zero otherwise. */
7eff2e7a 82static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
db0c2d59
PO
83{
84 struct ccw_device *cdev = to_ccwdev(dev);
85 struct ccw_device_id *id = &(cdev->id);
cfbe9bb2
CH
86 int ret;
87 char modalias_buf[30];
1da177e4 88
db0c2d59 89 /* CU_TYPE= */
7eff2e7a 90 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
cfbe9bb2
CH
91 if (ret)
92 return ret;
db0c2d59
PO
93
94 /* CU_MODEL= */
7eff2e7a 95 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
cfbe9bb2
CH
96 if (ret)
97 return ret;
1da177e4
LT
98
99 /* The next two can be zero, that's ok for us */
db0c2d59 100 /* DEV_TYPE= */
7eff2e7a 101 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
cfbe9bb2
CH
102 if (ret)
103 return ret;
1da177e4 104
db0c2d59 105 /* DEV_MODEL= */
7eff2e7a 106 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
cfbe9bb2
CH
107 if (ret)
108 return ret;
db0c2d59
PO
109
110 /* MODALIAS= */
cfbe9bb2 111 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
7eff2e7a
KS
112 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
113 return ret;
1da177e4
LT
114}
115
8bbace7e 116struct bus_type ccw_bus_type;
1da177e4 117
8bbace7e
CH
118static int io_subchannel_probe (struct subchannel *);
119static int io_subchannel_remove (struct subchannel *);
1da177e4
LT
120static int io_subchannel_notify(struct device *, int);
121static void io_subchannel_verify(struct device *);
122static void io_subchannel_ioterm(struct device *);
8bbace7e 123static void io_subchannel_shutdown(struct subchannel *);
1da177e4 124
f7e5d67c 125static struct css_driver io_subchannel_driver = {
1da177e4
LT
126 .subchannel_type = SUBCHANNEL_TYPE_IO,
127 .drv = {
128 .name = "io_subchannel",
129 .bus = &css_bus_type,
1da177e4
LT
130 },
131 .irq = io_subchannel_irq,
132 .notify = io_subchannel_notify,
133 .verify = io_subchannel_verify,
134 .termination = io_subchannel_ioterm,
8bbace7e
CH
135 .probe = io_subchannel_probe,
136 .remove = io_subchannel_remove,
137 .shutdown = io_subchannel_shutdown,
1da177e4
LT
138};
139
140struct workqueue_struct *ccw_device_work;
141struct workqueue_struct *ccw_device_notify_work;
40154b82
PO
142wait_queue_head_t ccw_device_init_wq;
143atomic_t ccw_device_init_count;
1da177e4
LT
144
145static int __init
146init_ccw_bus_type (void)
147{
148 int ret;
149
150 init_waitqueue_head(&ccw_device_init_wq);
151 atomic_set(&ccw_device_init_count, 0);
152
153 ccw_device_work = create_singlethread_workqueue("cio");
154 if (!ccw_device_work)
155 return -ENOMEM; /* FIXME: better errno ? */
156 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
157 if (!ccw_device_notify_work) {
158 ret = -ENOMEM; /* FIXME: better errno ? */
159 goto out_err;
160 }
161 slow_path_wq = create_singlethread_workqueue("kslowcrw");
162 if (!slow_path_wq) {
163 ret = -ENOMEM; /* FIXME: better errno ? */
164 goto out_err;
165 }
166 if ((ret = bus_register (&ccw_bus_type)))
167 goto out_err;
168
169 if ((ret = driver_register(&io_subchannel_driver.drv)))
170 goto out_err;
171
172 wait_event(ccw_device_init_wq,
173 atomic_read(&ccw_device_init_count) == 0);
174 flush_workqueue(ccw_device_work);
175 return 0;
176out_err:
177 if (ccw_device_work)
178 destroy_workqueue(ccw_device_work);
179 if (ccw_device_notify_work)
180 destroy_workqueue(ccw_device_notify_work);
181 if (slow_path_wq)
182 destroy_workqueue(slow_path_wq);
183 return ret;
184}
185
186static void __exit
187cleanup_ccw_bus_type (void)
188{
189 driver_unregister(&io_subchannel_driver.drv);
190 bus_unregister(&ccw_bus_type);
191 destroy_workqueue(ccw_device_notify_work);
192 destroy_workqueue(ccw_device_work);
193}
194
195subsys_initcall(init_ccw_bus_type);
196module_exit(cleanup_ccw_bus_type);
197
198/************************ device handling **************************/
199
200/*
201 * A ccw_device has some interfaces in sysfs in addition to the
202 * standard ones.
203 * The following entries are designed to export the information which
204 * resided in 2.4 in /proc/subchannels. Subchannel and device number
205 * are obvious, so they don't have an entry :)
206 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
207 */
208static ssize_t
3fd3c0a5 209chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
210{
211 struct subchannel *sch = to_subchannel(dev);
7ad6a249 212 struct chsc_ssd_info *ssd = &sch->ssd_info;
1da177e4
LT
213 ssize_t ret = 0;
214 int chp;
7ad6a249
PO
215 int mask;
216
217 for (chp = 0; chp < 8; chp++) {
218 mask = 0x80 >> chp;
219 if (ssd->path_mask & mask)
220 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
221 else
222 ret += sprintf(buf + ret, "00 ");
223 }
1da177e4
LT
224 ret += sprintf (buf+ret, "\n");
225 return min((ssize_t)PAGE_SIZE, ret);
226}
227
228static ssize_t
3fd3c0a5 229pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
230{
231 struct subchannel *sch = to_subchannel(dev);
232 struct pmcw *pmcw = &sch->schib.pmcw;
233
234 return sprintf (buf, "%02x %02x %02x\n",
235 pmcw->pim, pmcw->pam, pmcw->pom);
236}
237
238static ssize_t
3fd3c0a5 239devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
240{
241 struct ccw_device *cdev = to_ccwdev(dev);
242 struct ccw_device_id *id = &(cdev->id);
243
244 if (id->dev_type != 0)
245 return sprintf(buf, "%04x/%02x\n",
246 id->dev_type, id->dev_model);
247 else
248 return sprintf(buf, "n/a\n");
249}
250
251static ssize_t
3fd3c0a5 252cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
253{
254 struct ccw_device *cdev = to_ccwdev(dev);
255 struct ccw_device_id *id = &(cdev->id);
256
257 return sprintf(buf, "%04x/%02x\n",
258 id->cu_type, id->cu_model);
259}
260
f1fc78a8
BB
261static ssize_t
262modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
263{
264 struct ccw_device *cdev = to_ccwdev(dev);
265 struct ccw_device_id *id = &(cdev->id);
db0c2d59 266 int len;
f1fc78a8 267
086a6c62 268 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
db0c2d59
PO
269
270 return len > PAGE_SIZE ? PAGE_SIZE : len;
f1fc78a8
BB
271}
272
1da177e4 273static ssize_t
3fd3c0a5 274online_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
275{
276 struct ccw_device *cdev = to_ccwdev(dev);
277
278 return sprintf(buf, cdev->online ? "1\n" : "0\n");
279}
280
d7b5a4c9
CH
281int ccw_device_is_orphan(struct ccw_device *cdev)
282{
283 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
284}
285
ef99516c 286static void ccw_device_unregister(struct ccw_device *cdev)
7674da77 287{
7674da77 288 if (test_and_clear_bit(1, &cdev->private->registered))
ef99516c 289 device_del(&cdev->dev);
7674da77
CH
290}
291
59a8a6e2
CH
292static void ccw_device_remove_orphan_cb(struct device *dev)
293{
294 struct ccw_device *cdev = to_ccwdev(dev);
295
296 ccw_device_unregister(cdev);
297 put_device(&cdev->dev);
298}
299
300static void ccw_device_remove_sch_cb(struct device *dev)
301{
302 struct subchannel *sch;
303
304 sch = to_subchannel(dev);
305 css_sch_device_unregister(sch);
306 /* Reset intparm to zeroes. */
307 sch->schib.pmcw.intparm = 0;
308 cio_modify(sch);
309 put_device(&sch->dev);
310}
311
1da177e4
LT
312static void
313ccw_device_remove_disconnected(struct ccw_device *cdev)
314{
d7b5a4c9 315 unsigned long flags;
59a8a6e2
CH
316 int rc;
317
1da177e4
LT
318 /*
319 * Forced offline in disconnected state means
320 * 'throw away device'.
321 */
d7b5a4c9 322 if (ccw_device_is_orphan(cdev)) {
59a8a6e2
CH
323 /*
324 * Deregister ccw device.
325 * Unfortunately, we cannot do this directly from the
326 * attribute method.
327 */
d7b5a4c9
CH
328 spin_lock_irqsave(cdev->ccwlock, flags);
329 cdev->private->state = DEV_STATE_NOT_OPER;
330 spin_unlock_irqrestore(cdev->ccwlock, flags);
59a8a6e2
CH
331 rc = device_schedule_callback(&cdev->dev,
332 ccw_device_remove_orphan_cb);
333 if (rc)
e556bbbd
CH
334 CIO_MSG_EVENT(2, "Couldn't unregister orphan "
335 "0.%x.%04x\n",
336 cdev->private->dev_id.ssid,
337 cdev->private->dev_id.devno);
59a8a6e2 338 return;
d7b5a4c9 339 }
59a8a6e2
CH
340 /* Deregister subchannel, which will kill the ccw device. */
341 rc = device_schedule_callback(cdev->dev.parent,
342 ccw_device_remove_sch_cb);
343 if (rc)
e556bbbd
CH
344 CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
345 "0.%x.%04x\n",
346 cdev->private->dev_id.ssid,
347 cdev->private->dev_id.devno);
1da177e4
LT
348}
349
b2ffd8e9
CH
350/**
351 * ccw_device_set_offline() - disable a ccw device for I/O
352 * @cdev: target ccw device
353 *
354 * This function calls the driver's set_offline() function for @cdev, if
355 * given, and then disables @cdev.
356 * Returns:
357 * %0 on success and a negative error value on failure.
358 * Context:
359 * enabled, ccw device lock not held
360 */
361int ccw_device_set_offline(struct ccw_device *cdev)
1da177e4
LT
362{
363 int ret;
364
365 if (!cdev)
366 return -ENODEV;
367 if (!cdev->online || !cdev->drv)
368 return -EINVAL;
369
370 if (cdev->drv->set_offline) {
371 ret = cdev->drv->set_offline(cdev);
372 if (ret != 0)
373 return ret;
374 }
375 cdev->online = 0;
376 spin_lock_irq(cdev->ccwlock);
377 ret = ccw_device_offline(cdev);
378 if (ret == -ENODEV) {
379 if (cdev->private->state != DEV_STATE_NOT_OPER) {
380 cdev->private->state = DEV_STATE_OFFLINE;
381 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
382 }
383 spin_unlock_irq(cdev->ccwlock);
384 return ret;
385 }
386 spin_unlock_irq(cdev->ccwlock);
387 if (ret == 0)
388 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
389 else {
e556bbbd
CH
390 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
391 "device 0.%x.%04x\n",
392 ret, cdev->private->dev_id.ssid,
393 cdev->private->dev_id.devno);
1da177e4
LT
394 cdev->online = 1;
395 }
396 return ret;
397}
398
b2ffd8e9
CH
399/**
400 * ccw_device_set_online() - enable a ccw device for I/O
401 * @cdev: target ccw device
402 *
403 * This function first enables @cdev and then calls the driver's set_online()
404 * function for @cdev, if given. If set_online() returns an error, @cdev is
405 * disabled again.
406 * Returns:
407 * %0 on success and a negative error value on failure.
408 * Context:
409 * enabled, ccw device lock not held
410 */
411int ccw_device_set_online(struct ccw_device *cdev)
1da177e4
LT
412{
413 int ret;
414
415 if (!cdev)
416 return -ENODEV;
417 if (cdev->online || !cdev->drv)
418 return -EINVAL;
419
420 spin_lock_irq(cdev->ccwlock);
421 ret = ccw_device_online(cdev);
422 spin_unlock_irq(cdev->ccwlock);
423 if (ret == 0)
424 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
425 else {
e556bbbd
CH
426 CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
427 "device 0.%x.%04x\n",
428 ret, cdev->private->dev_id.ssid,
429 cdev->private->dev_id.devno);
1da177e4
LT
430 return ret;
431 }
432 if (cdev->private->state != DEV_STATE_ONLINE)
433 return -ENODEV;
434 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
435 cdev->online = 1;
436 return 0;
437 }
438 spin_lock_irq(cdev->ccwlock);
439 ret = ccw_device_offline(cdev);
440 spin_unlock_irq(cdev->ccwlock);
441 if (ret == 0)
442 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
e556bbbd
CH
443 else
444 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
445 "device 0.%x.%04x\n",
446 ret, cdev->private->dev_id.ssid,
447 cdev->private->dev_id.devno);
6cadb78b 448 return (ret == 0) ? -ENODEV : ret;
1da177e4
LT
449}
450
f5ba6c86
CH
451static void online_store_handle_offline(struct ccw_device *cdev)
452{
453 if (cdev->private->state == DEV_STATE_DISCONNECTED)
454 ccw_device_remove_disconnected(cdev);
455 else if (cdev->drv && cdev->drv->set_offline)
456 ccw_device_set_offline(cdev);
457}
458
459static int online_store_recog_and_online(struct ccw_device *cdev)
460{
461 int ret;
462
463 /* Do device recognition, if needed. */
464 if (cdev->id.cu_type == 0) {
465 ret = ccw_device_recognition(cdev);
466 if (ret) {
e556bbbd
CH
467 CIO_MSG_EVENT(0, "Couldn't start recognition "
468 "for device 0.%x.%04x (ret=%d)\n",
469 cdev->private->dev_id.ssid,
470 cdev->private->dev_id.devno, ret);
f5ba6c86
CH
471 return ret;
472 }
473 wait_event(cdev->private->wait_q,
474 cdev->private->flags.recog_done);
475 }
476 if (cdev->drv && cdev->drv->set_online)
477 ccw_device_set_online(cdev);
478 return 0;
479}
480static void online_store_handle_online(struct ccw_device *cdev, int force)
481{
482 int ret;
483
484 ret = online_store_recog_and_online(cdev);
485 if (ret)
486 return;
487 if (force && cdev->private->state == DEV_STATE_BOXED) {
488 ret = ccw_device_stlck(cdev);
489 if (ret) {
e556bbbd
CH
490 dev_warn(&cdev->dev,
491 "ccw_device_stlck returned %d!\n", ret);
f5ba6c86
CH
492 return;
493 }
494 if (cdev->id.cu_type == 0)
495 cdev->private->state = DEV_STATE_NOT_OPER;
496 online_store_recog_and_online(cdev);
497 }
498
499}
500
501static ssize_t online_store (struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
1da177e4
LT
503{
504 struct ccw_device *cdev = to_ccwdev(dev);
f5ba6c86 505 int i, force;
1da177e4
LT
506 char *tmp;
507
973bd993 508 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4
LT
509 return -EAGAIN;
510
511 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
512 atomic_set(&cdev->private->onoff, 0);
513 return -EINVAL;
514 }
515 if (!strncmp(buf, "force\n", count)) {
516 force = 1;
517 i = 1;
518 } else {
519 force = 0;
520 i = simple_strtoul(buf, &tmp, 16);
521 }
f5ba6c86
CH
522
523 switch (i) {
524 case 0:
525 online_store_handle_offline(cdev);
526 break;
527 case 1:
528 online_store_handle_online(cdev, force);
529 break;
530 default:
531 count = -EINVAL;
1da177e4 532 }
1da177e4
LT
533 if (cdev->drv)
534 module_put(cdev->drv->owner);
535 atomic_set(&cdev->private->onoff, 0);
536 return count;
537}
538
539static ssize_t
3fd3c0a5 540available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
541{
542 struct ccw_device *cdev = to_ccwdev(dev);
543 struct subchannel *sch;
544
d7b5a4c9
CH
545 if (ccw_device_is_orphan(cdev))
546 return sprintf(buf, "no device\n");
1da177e4
LT
547 switch (cdev->private->state) {
548 case DEV_STATE_BOXED:
549 return sprintf(buf, "boxed\n");
550 case DEV_STATE_DISCONNECTED:
551 case DEV_STATE_DISCONNECTED_SENSE_ID:
552 case DEV_STATE_NOT_OPER:
553 sch = to_subchannel(dev->parent);
554 if (!sch->lpm)
555 return sprintf(buf, "no path\n");
556 else
557 return sprintf(buf, "no device\n");
558 default:
559 /* All other states considered fine. */
560 return sprintf(buf, "good\n");
561 }
562}
563
564static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
565static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
566static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
567static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
f1fc78a8 568static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
1da177e4
LT
569static DEVICE_ATTR(online, 0644, online_show, online_store);
570extern struct device_attribute dev_attr_cmb_enable;
571static DEVICE_ATTR(availability, 0444, available_show, NULL);
572
573static struct attribute * subch_attrs[] = {
574 &dev_attr_chpids.attr,
575 &dev_attr_pimpampom.attr,
576 NULL,
577};
578
579static struct attribute_group subch_attr_group = {
580 .attrs = subch_attrs,
581};
582
529192f3
CH
583struct attribute_group *subch_attr_groups[] = {
584 &subch_attr_group,
585 NULL,
586};
1da177e4
LT
587
588static struct attribute * ccwdev_attrs[] = {
589 &dev_attr_devtype.attr,
590 &dev_attr_cutype.attr,
f1fc78a8 591 &dev_attr_modalias.attr,
1da177e4
LT
592 &dev_attr_online.attr,
593 &dev_attr_cmb_enable.attr,
594 &dev_attr_availability.attr,
595 NULL,
596};
597
598static struct attribute_group ccwdev_attr_group = {
599 .attrs = ccwdev_attrs,
600};
601
f7e5d67c 602static struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
603 &ccwdev_attr_group,
604 NULL,
605};
1da177e4
LT
606
607/* this is a simple abstraction for device_register that sets the
608 * correct bus type and adds the bus specific files */
3c9da7ba 609static int ccw_device_register(struct ccw_device *cdev)
1da177e4
LT
610{
611 struct device *dev = &cdev->dev;
612 int ret;
613
614 dev->bus = &ccw_bus_type;
615
616 if ((ret = device_add(dev)))
617 return ret;
618
619 set_bit(1, &cdev->private->registered);
1da177e4
LT
620 return ret;
621}
622
b0744bd2 623struct match_data {
78964268 624 struct ccw_dev_id dev_id;
b0744bd2
CH
625 struct ccw_device * sibling;
626};
627
628static int
629match_devno(struct device * dev, void * data)
630{
78964268 631 struct match_data * d = data;
b0744bd2
CH
632 struct ccw_device * cdev;
633
634 cdev = to_ccwdev(dev);
635 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
d7b5a4c9 636 !ccw_device_is_orphan(cdev) &&
78964268 637 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
d7b5a4c9 638 (cdev != d->sibling))
b0744bd2 639 return 1;
b0744bd2
CH
640 return 0;
641}
642
78964268
CH
643static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
644 struct ccw_device *sibling)
1da177e4 645{
1da177e4 646 struct device *dev;
292888c8 647 struct match_data data;
1da177e4 648
78964268 649 data.dev_id = *dev_id;
292888c8 650 data.sibling = sibling;
e5945b4f 651 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
1da177e4 652
b0744bd2 653 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
654}
655
d7b5a4c9
CH
656static int match_orphan(struct device *dev, void *data)
657{
658 struct ccw_dev_id *dev_id;
659 struct ccw_device *cdev;
660
661 dev_id = data;
662 cdev = to_ccwdev(dev);
663 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
664}
665
666static struct ccw_device *
667get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
668 struct ccw_dev_id *dev_id)
669{
670 struct device *dev;
671
672 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
673 match_orphan);
674
675 return dev ? to_ccwdev(dev) : NULL;
676}
677
1da177e4 678static void
c1637532 679ccw_device_add_changed(struct work_struct *work)
1da177e4 680{
c1637532 681 struct ccw_device_private *priv;
1da177e4
LT
682 struct ccw_device *cdev;
683
c1637532
MS
684 priv = container_of(work, struct ccw_device_private, kick_work);
685 cdev = priv->cdev;
1da177e4
LT
686 if (device_add(&cdev->dev)) {
687 put_device(&cdev->dev);
688 return;
689 }
690 set_bit(1, &cdev->private->registered);
1da177e4
LT
691}
692
d7b5a4c9 693void ccw_device_do_unreg_rereg(struct work_struct *work)
1da177e4 694{
c1637532 695 struct ccw_device_private *priv;
1da177e4
LT
696 struct ccw_device *cdev;
697 struct subchannel *sch;
1da177e4 698
c1637532
MS
699 priv = container_of(work, struct ccw_device_private, kick_work);
700 cdev = priv->cdev;
1da177e4 701 sch = to_subchannel(cdev->dev.parent);
d7b5a4c9 702
ef99516c 703 ccw_device_unregister(cdev);
1da177e4 704 PREPARE_WORK(&cdev->private->kick_work,
c1637532 705 ccw_device_add_changed);
1da177e4
LT
706 queue_work(ccw_device_work, &cdev->private->kick_work);
707}
708
709static void
710ccw_device_release(struct device *dev)
711{
712 struct ccw_device *cdev;
713
714 cdev = to_ccwdev(dev);
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;
741 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
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)) {
746 if (cdev->dev.release)
747 cdev->dev.release(&cdev->dev);
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
CH
775 spin_lock_irq(sch->lock);
776 sch->dev.driver_data = cdev;
777 cdev->private->schid = sch->schid;
778 cdev->ccwlock = sch->lock;
779 device_trigger_reprobe(sch);
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
789 other_sch = to_subchannel(get_device(cdev->dev.parent));
790 ret = device_move(&cdev->dev, &sch->dev);
791 if (ret) {
792 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
793 "(ret=%d)!\n", cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno, ret);
795 put_device(&other_sch->dev);
796 return;
797 }
798 other_sch->dev.driver_data = NULL;
799 /* No need to keep a subchannel without ccw device around. */
800 css_sch_device_unregister(other_sch);
801 put_device(&other_sch->dev);
802 sch_attach_device(sch, cdev);
803}
804
805static void sch_attach_orphaned_device(struct subchannel *sch,
806 struct ccw_device *cdev)
807{
808 int ret;
809
810 /* Try to move the ccw device to its new subchannel. */
811 ret = device_move(&cdev->dev, &sch->dev);
812 if (ret) {
813 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
814 "failed (ret=%d)!\n",
815 cdev->private->dev_id.ssid,
816 cdev->private->dev_id.devno, ret);
817 return;
818 }
819 sch_attach_device(sch, cdev);
820}
821
822static void sch_create_and_recog_new_device(struct subchannel *sch)
823{
824 struct ccw_device *cdev;
825
826 /* Need to allocate a new ccw device. */
827 cdev = io_subchannel_create_ccwdev(sch);
828 if (IS_ERR(cdev)) {
829 /* OK, we did everything we could... */
830 css_sch_device_unregister(sch);
831 return;
832 }
833 spin_lock_irq(sch->lock);
834 sch->dev.driver_data = cdev;
835 spin_unlock_irq(sch->lock);
836 /* Start recognition for the new ccw device. */
837 if (io_subchannel_recog(cdev, sch)) {
838 spin_lock_irq(sch->lock);
839 sch->dev.driver_data = NULL;
840 spin_unlock_irq(sch->lock);
841 if (cdev->dev.release)
842 cdev->dev.release(&cdev->dev);
843 css_sch_device_unregister(sch);
844 }
845}
846
847
848void ccw_device_move_to_orphanage(struct work_struct *work)
849{
850 struct ccw_device_private *priv;
851 struct ccw_device *cdev;
852 struct ccw_device *replacing_cdev;
853 struct subchannel *sch;
854 int ret;
855 struct channel_subsystem *css;
856 struct ccw_dev_id dev_id;
857
858 priv = container_of(work, struct ccw_device_private, kick_work);
859 cdev = priv->cdev;
860 sch = to_subchannel(cdev->dev.parent);
861 css = to_css(sch->dev.parent);
862 dev_id.devno = sch->schib.pmcw.dev;
863 dev_id.ssid = sch->schid.ssid;
864
865 /*
866 * Move the orphaned ccw device to the orphanage so the replacing
867 * ccw device can take its place on the subchannel.
868 */
869 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
870 if (ret) {
871 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
872 "(ret=%d)!\n", cdev->private->dev_id.ssid,
873 cdev->private->dev_id.devno, ret);
874 return;
875 }
876 cdev->ccwlock = css->pseudo_subchannel->lock;
877 /*
878 * Search for the replacing ccw device
879 * - among the disconnected devices
880 * - in the orphanage
881 */
882 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
883 if (replacing_cdev) {
884 sch_attach_disconnected_device(sch, replacing_cdev);
885 return;
886 }
887 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
888 if (replacing_cdev) {
889 sch_attach_orphaned_device(sch, replacing_cdev);
890 return;
891 }
892 sch_create_and_recog_new_device(sch);
893}
894
1da177e4
LT
895/*
896 * Register recognized device.
897 */
898static void
c1637532 899io_subchannel_register(struct work_struct *work)
1da177e4 900{
c1637532 901 struct ccw_device_private *priv;
1da177e4
LT
902 struct ccw_device *cdev;
903 struct subchannel *sch;
904 int ret;
905 unsigned long flags;
906
c1637532
MS
907 priv = container_of(work, struct ccw_device_private, kick_work);
908 cdev = priv->cdev;
1da177e4 909 sch = to_subchannel(cdev->dev.parent);
82b7ac05 910 css_update_ssd_info(sch);
47af5518
CH
911 /*
912 * io_subchannel_register() will also be called after device
913 * recognition has been done for a boxed device (which will already
914 * be registered). We need to reprobe since we may now have sense id
915 * information.
916 */
b0744bd2 917 if (klist_node_attached(&cdev->dev.knode_parent)) {
47af5518
CH
918 if (!cdev->drv) {
919 ret = device_reprobe(&cdev->dev);
920 if (ret)
921 /* We can't do much here. */
e556bbbd
CH
922 CIO_MSG_EVENT(2, "device_reprobe() returned"
923 " %d for 0.%x.%04x\n", ret,
924 cdev->private->dev_id.ssid,
925 cdev->private->dev_id.devno);
47af5518 926 }
1da177e4
LT
927 goto out;
928 }
fa1a8c23
CH
929 /*
930 * Now we know this subchannel will stay, we can throw
931 * our delayed uevent.
932 */
933 sch->dev.uevent_suppress = 0;
934 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1da177e4
LT
935 /* make it known to the system */
936 ret = ccw_device_register(cdev);
937 if (ret) {
e556bbbd
CH
938 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
939 cdev->private->dev_id.ssid,
940 cdev->private->dev_id.devno, ret);
1da177e4 941 put_device(&cdev->dev);
2ec22984 942 spin_lock_irqsave(sch->lock, flags);
1da177e4 943 sch->dev.driver_data = NULL;
2ec22984 944 spin_unlock_irqrestore(sch->lock, flags);
1da177e4
LT
945 kfree (cdev->private);
946 kfree (cdev);
947 put_device(&sch->dev);
948 if (atomic_dec_and_test(&ccw_device_init_count))
949 wake_up(&ccw_device_init_wq);
950 return;
951 }
1da177e4
LT
952 put_device(&cdev->dev);
953out:
954 cdev->private->flags.recog_done = 1;
955 put_device(&sch->dev);
956 wake_up(&cdev->private->wait_q);
957 if (atomic_dec_and_test(&ccw_device_init_count))
958 wake_up(&ccw_device_init_wq);
959}
960
3f4cf6e7 961static void ccw_device_call_sch_unregister(struct work_struct *work)
1da177e4 962{
c1637532
MS
963 struct ccw_device_private *priv;
964 struct ccw_device *cdev;
1da177e4
LT
965 struct subchannel *sch;
966
c1637532
MS
967 priv = container_of(work, struct ccw_device_private, kick_work);
968 cdev = priv->cdev;
1da177e4 969 sch = to_subchannel(cdev->dev.parent);
6ab4879a 970 css_sch_device_unregister(sch);
1da177e4
LT
971 /* Reset intparm to zeroes. */
972 sch->schib.pmcw.intparm = 0;
973 cio_modify(sch);
974 put_device(&cdev->dev);
975 put_device(&sch->dev);
976}
977
978/*
979 * subchannel recognition done. Called from the state machine.
980 */
981void
982io_subchannel_recog_done(struct ccw_device *cdev)
983{
984 struct subchannel *sch;
985
986 if (css_init_done == 0) {
987 cdev->private->flags.recog_done = 1;
988 return;
989 }
990 switch (cdev->private->state) {
991 case DEV_STATE_NOT_OPER:
992 cdev->private->flags.recog_done = 1;
993 /* Remove device found not operational. */
994 if (!get_device(&cdev->dev))
995 break;
996 sch = to_subchannel(cdev->dev.parent);
997 PREPARE_WORK(&cdev->private->kick_work,
c1637532 998 ccw_device_call_sch_unregister);
1da177e4
LT
999 queue_work(slow_path_wq, &cdev->private->kick_work);
1000 if (atomic_dec_and_test(&ccw_device_init_count))
1001 wake_up(&ccw_device_init_wq);
1002 break;
1003 case DEV_STATE_BOXED:
1004 /* Device did not respond in time. */
1005 case DEV_STATE_OFFLINE:
1006 /*
1007 * We can't register the device in interrupt context so
1008 * we schedule a work item.
1009 */
1010 if (!get_device(&cdev->dev))
1011 break;
1012 PREPARE_WORK(&cdev->private->kick_work,
c1637532 1013 io_subchannel_register);
1da177e4
LT
1014 queue_work(slow_path_wq, &cdev->private->kick_work);
1015 break;
1016 }
1017}
1018
1019static int
1020io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1021{
1022 int rc;
1023 struct ccw_device_private *priv;
1024
1025 sch->dev.driver_data = cdev;
1026 sch->driver = &io_subchannel_driver;
2ec22984 1027 cdev->ccwlock = sch->lock;
fb6958a5 1028
1da177e4
LT
1029 /* Init private data. */
1030 priv = cdev->private;
78964268
CH
1031 priv->dev_id.devno = sch->schib.pmcw.dev;
1032 priv->dev_id.ssid = sch->schid.ssid;
1033 priv->schid = sch->schid;
1da177e4
LT
1034 priv->state = DEV_STATE_NOT_OPER;
1035 INIT_LIST_HEAD(&priv->cmb_list);
1036 init_waitqueue_head(&priv->wait_q);
1037 init_timer(&priv->timer);
1038
1039 /* Set an initial name for the device. */
fb6958a5
CH
1040 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1041 sch->schid.ssid, sch->schib.pmcw.dev);
1da177e4
LT
1042
1043 /* Increase counter of devices currently in recognition. */
1044 atomic_inc(&ccw_device_init_count);
1045
1046 /* Start async. device sensing. */
2ec22984 1047 spin_lock_irq(sch->lock);
1da177e4 1048 rc = ccw_device_recognition(cdev);
2ec22984 1049 spin_unlock_irq(sch->lock);
1da177e4
LT
1050 if (rc) {
1051 if (atomic_dec_and_test(&ccw_device_init_count))
1052 wake_up(&ccw_device_init_wq);
1053 }
1054 return rc;
1055}
1056
d7b5a4c9
CH
1057static void ccw_device_move_to_sch(struct work_struct *work)
1058{
1059 struct ccw_device_private *priv;
1060 int rc;
1061 struct subchannel *sch;
1062 struct ccw_device *cdev;
1063 struct subchannel *former_parent;
1064
1065 priv = container_of(work, struct ccw_device_private, kick_work);
1066 sch = priv->sch;
1067 cdev = priv->cdev;
1068 former_parent = ccw_device_is_orphan(cdev) ?
1069 NULL : to_subchannel(get_device(cdev->dev.parent));
1070 mutex_lock(&sch->reg_mutex);
1071 /* Try to move the ccw device to its new subchannel. */
1072 rc = device_move(&cdev->dev, &sch->dev);
1073 mutex_unlock(&sch->reg_mutex);
1074 if (rc) {
1075 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1076 "0.%x.%04x failed (ret=%d)!\n",
1077 cdev->private->dev_id.ssid,
1078 cdev->private->dev_id.devno, sch->schid.ssid,
1079 sch->schid.sch_no, rc);
1080 css_sch_device_unregister(sch);
1081 goto out;
1082 }
1083 if (former_parent) {
1084 spin_lock_irq(former_parent->lock);
1085 former_parent->dev.driver_data = NULL;
1086 spin_unlock_irq(former_parent->lock);
1087 css_sch_device_unregister(former_parent);
1088 /* Reset intparm to zeroes. */
1089 former_parent->schib.pmcw.intparm = 0;
1090 cio_modify(former_parent);
1091 }
1092 sch_attach_device(sch, cdev);
1093out:
1094 if (former_parent)
1095 put_device(&former_parent->dev);
1096 put_device(&cdev->dev);
1097}
1098
1da177e4 1099static int
8bbace7e 1100io_subchannel_probe (struct subchannel *sch)
1da177e4 1101{
1da177e4
LT
1102 struct ccw_device *cdev;
1103 int rc;
1104 unsigned long flags;
d7b5a4c9 1105 struct ccw_dev_id dev_id;
1da177e4 1106
1da177e4
LT
1107 if (sch->dev.driver_data) {
1108 /*
1109 * This subchannel already has an associated ccw_device.
1110 * Register it and exit. This happens for all early
1111 * device, e.g. the console.
1112 */
1113 cdev = sch->dev.driver_data;
e1031786 1114 cdev->dev.groups = ccwdev_attr_groups;
1da177e4
LT
1115 device_initialize(&cdev->dev);
1116 ccw_device_register(cdev);
1da177e4
LT
1117 /*
1118 * Check if the device is already online. If it is
1119 * the reference count needs to be corrected
1120 * (see ccw_device_online and css_init_done for the
1121 * ugly details).
1122 */
1123 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1124 cdev->private->state != DEV_STATE_OFFLINE &&
1125 cdev->private->state != DEV_STATE_BOXED)
1126 get_device(&cdev->dev);
1127 return 0;
1128 }
d7b5a4c9
CH
1129 /*
1130 * First check if a fitting device may be found amongst the
1131 * disconnected devices or in the orphanage.
1132 */
1133 dev_id.devno = sch->schib.pmcw.dev;
1134 dev_id.ssid = sch->schid.ssid;
1135 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1136 if (!cdev)
1137 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1138 &dev_id);
1139 if (cdev) {
1140 /*
1141 * Schedule moving the device until when we have a registered
1142 * subchannel to move to and succeed the probe. We can
1143 * unregister later again, when the probe is through.
1144 */
1145 cdev->private->sch = sch;
1146 PREPARE_WORK(&cdev->private->kick_work,
1147 ccw_device_move_to_sch);
1148 queue_work(slow_path_wq, &cdev->private->kick_work);
1149 return 0;
1150 }
7674da77
CH
1151 cdev = io_subchannel_create_ccwdev(sch);
1152 if (IS_ERR(cdev))
1153 return PTR_ERR(cdev);
1da177e4 1154
8bbace7e 1155 rc = io_subchannel_recog(cdev, sch);
1da177e4 1156 if (rc) {
2ec22984 1157 spin_lock_irqsave(sch->lock, flags);
1da177e4 1158 sch->dev.driver_data = NULL;
2ec22984 1159 spin_unlock_irqrestore(sch->lock, flags);
1da177e4
LT
1160 if (cdev->dev.release)
1161 cdev->dev.release(&cdev->dev);
1162 }
1163
1164 return rc;
1165}
1166
1da177e4 1167static int
8bbace7e 1168io_subchannel_remove (struct subchannel *sch)
1da177e4
LT
1169{
1170 struct ccw_device *cdev;
1171 unsigned long flags;
1172
8bbace7e 1173 if (!sch->dev.driver_data)
1da177e4 1174 return 0;
8bbace7e 1175 cdev = sch->dev.driver_data;
1da177e4
LT
1176 /* Set ccw device to not operational and drop reference. */
1177 spin_lock_irqsave(cdev->ccwlock, flags);
8bbace7e 1178 sch->dev.driver_data = NULL;
1da177e4
LT
1179 cdev->private->state = DEV_STATE_NOT_OPER;
1180 spin_unlock_irqrestore(cdev->ccwlock, flags);
ef99516c
CH
1181 ccw_device_unregister(cdev);
1182 put_device(&cdev->dev);
1da177e4
LT
1183 return 0;
1184}
1185
1186static int
1187io_subchannel_notify(struct device *dev, int event)
1188{
1189 struct ccw_device *cdev;
1190
1191 cdev = dev->driver_data;
1192 if (!cdev)
1193 return 0;
1194 if (!cdev->drv)
1195 return 0;
1196 if (!cdev->online)
1197 return 0;
1198 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1199}
1200
1201static void
1202io_subchannel_verify(struct device *dev)
1203{
1204 struct ccw_device *cdev;
1205
1206 cdev = dev->driver_data;
1207 if (cdev)
1208 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1209}
1210
1211static void
1212io_subchannel_ioterm(struct device *dev)
1213{
1214 struct ccw_device *cdev;
1215
1216 cdev = dev->driver_data;
1217 if (!cdev)
1218 return;
d23861ff
CH
1219 /* Internal I/O will be retried by the interrupt handler. */
1220 if (cdev->private->flags.intretry)
1221 return;
1da177e4
LT
1222 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1223 if (cdev->handler)
1224 cdev->handler(cdev, cdev->private->intparm,
1225 ERR_PTR(-EIO));
1226}
1227
1228static void
8bbace7e 1229io_subchannel_shutdown(struct subchannel *sch)
1da177e4 1230{
1da177e4
LT
1231 struct ccw_device *cdev;
1232 int ret;
1233
8bbace7e 1234 cdev = sch->dev.driver_data;
1da177e4 1235
a8237fc4 1236 if (cio_is_console(sch->schid))
1da177e4
LT
1237 return;
1238 if (!sch->schib.pmcw.ena)
1239 /* Nothing to do. */
1240 return;
1241 ret = cio_disable_subchannel(sch);
1242 if (ret != -EBUSY)
1243 /* Subchannel is disabled, we're done. */
1244 return;
1245 cdev->private->state = DEV_STATE_QUIESCE;
1246 if (cdev->handler)
1247 cdev->handler(cdev, cdev->private->intparm,
1248 ERR_PTR(-EIO));
1249 ret = ccw_device_cancel_halt_clear(cdev);
1250 if (ret == -EBUSY) {
1251 ccw_device_set_timeout(cdev, HZ/10);
1252 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1253 }
1254 cio_disable_subchannel(sch);
1255}
1256
1257#ifdef CONFIG_CCW_CONSOLE
1258static struct ccw_device console_cdev;
1259static struct ccw_device_private console_private;
1260static int console_cdev_in_use;
1261
2ec22984
CH
1262static DEFINE_SPINLOCK(ccw_console_lock);
1263
1264spinlock_t * cio_get_console_lock(void)
1265{
1266 return &ccw_console_lock;
1267}
1268
1da177e4
LT
1269static int
1270ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1271{
1272 int rc;
1273
1274 /* Initialize the ccw_device structure. */
292888c8 1275 cdev->dev.parent= &sch->dev;
1da177e4
LT
1276 rc = io_subchannel_recog(cdev, sch);
1277 if (rc)
1278 return rc;
1279
1280 /* Now wait for the async. recognition to come to an end. */
1281 spin_lock_irq(cdev->ccwlock);
1282 while (!dev_fsm_final_state(cdev))
1283 wait_cons_dev();
1284 rc = -EIO;
1285 if (cdev->private->state != DEV_STATE_OFFLINE)
1286 goto out_unlock;
1287 ccw_device_online(cdev);
1288 while (!dev_fsm_final_state(cdev))
1289 wait_cons_dev();
1290 if (cdev->private->state != DEV_STATE_ONLINE)
1291 goto out_unlock;
1292 rc = 0;
1293out_unlock:
1294 spin_unlock_irq(cdev->ccwlock);
1295 return 0;
1296}
1297
1298struct ccw_device *
1299ccw_device_probe_console(void)
1300{
1301 struct subchannel *sch;
1302 int ret;
1303
1304 if (xchg(&console_cdev_in_use, 1) != 0)
600b5d16 1305 return ERR_PTR(-EBUSY);
1da177e4
LT
1306 sch = cio_probe_console();
1307 if (IS_ERR(sch)) {
1308 console_cdev_in_use = 0;
1309 return (void *) sch;
1310 }
1311 memset(&console_cdev, 0, sizeof(struct ccw_device));
1312 memset(&console_private, 0, sizeof(struct ccw_device_private));
1313 console_cdev.private = &console_private;
c1637532 1314 console_private.cdev = &console_cdev;
1da177e4
LT
1315 ret = ccw_device_console_enable(&console_cdev, sch);
1316 if (ret) {
1317 cio_release_console();
1318 console_cdev_in_use = 0;
1319 return ERR_PTR(ret);
1320 }
1321 console_cdev.online = 1;
1322 return &console_cdev;
1323}
1324#endif
1325
1326/*
1327 * get ccw_device matching the busid, but only if owned by cdrv
1328 */
b0744bd2
CH
1329static int
1330__ccwdev_check_busid(struct device *dev, void *id)
1331{
1332 char *bus_id;
1333
12975aef 1334 bus_id = id;
b0744bd2
CH
1335
1336 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1337}
1338
1339
b2ffd8e9
CH
1340/**
1341 * get_ccwdev_by_busid() - obtain device from a bus id
1342 * @cdrv: driver the device is owned by
1343 * @bus_id: bus id of the device to be searched
1344 *
1345 * This function searches all devices owned by @cdrv for a device with a bus
1346 * id matching @bus_id.
1347 * Returns:
1348 * If a match is found, its reference count of the found device is increased
1349 * and it is returned; else %NULL is returned.
1350 */
1351struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1352 const char *bus_id)
1da177e4 1353{
b0744bd2 1354 struct device *dev;
1da177e4
LT
1355 struct device_driver *drv;
1356
1357 drv = get_driver(&cdrv->driver);
1358 if (!drv)
b0744bd2 1359 return NULL;
1da177e4 1360
b0744bd2
CH
1361 dev = driver_find_device(drv, NULL, (void *)bus_id,
1362 __ccwdev_check_busid);
1da177e4
LT
1363 put_driver(drv);
1364
d2c993d8 1365 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
1366}
1367
1368/************************** device driver handling ************************/
1369
1370/* This is the implementation of the ccw_driver class. The probe, remove
1371 * and release methods are initially very similar to the device_driver
1372 * implementations, with the difference that they have ccw_device
1373 * arguments.
1374 *
1375 * A ccw driver also contains the information that is needed for
1376 * device matching.
1377 */
1378static int
1379ccw_device_probe (struct device *dev)
1380{
1381 struct ccw_device *cdev = to_ccwdev(dev);
1382 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1383 int ret;
1384
1385 cdev->drv = cdrv; /* to let the driver call _set_online */
1386
1387 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1388
1389 if (ret) {
d2c993d8 1390 cdev->drv = NULL;
1da177e4
LT
1391 return ret;
1392 }
1393
1394 return 0;
1395}
1396
1397static int
1398ccw_device_remove (struct device *dev)
1399{
1400 struct ccw_device *cdev = to_ccwdev(dev);
1401 struct ccw_driver *cdrv = cdev->drv;
1402 int ret;
1403
1da177e4
LT
1404 if (cdrv->remove)
1405 cdrv->remove(cdev);
1406 if (cdev->online) {
1407 cdev->online = 0;
1408 spin_lock_irq(cdev->ccwlock);
1409 ret = ccw_device_offline(cdev);
1410 spin_unlock_irq(cdev->ccwlock);
1411 if (ret == 0)
1412 wait_event(cdev->private->wait_q,
1413 dev_fsm_final_state(cdev));
1414 else
1415 //FIXME: we can't fail!
e556bbbd
CH
1416 CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
1417 "device 0.%x.%04x\n",
1418 ret, cdev->private->dev_id.ssid,
1419 cdev->private->dev_id.devno);
1da177e4
LT
1420 }
1421 ccw_device_set_timeout(cdev, 0);
d2c993d8 1422 cdev->drv = NULL;
1da177e4
LT
1423 return 0;
1424}
1425
958974fb
CH
1426static void ccw_device_shutdown(struct device *dev)
1427{
1428 struct ccw_device *cdev;
1429
1430 cdev = to_ccwdev(dev);
1431 if (cdev->drv && cdev->drv->shutdown)
1432 cdev->drv->shutdown(cdev);
1842f2b1 1433 disable_cmf(cdev);
958974fb
CH
1434}
1435
8bbace7e
CH
1436struct bus_type ccw_bus_type = {
1437 .name = "ccw",
1438 .match = ccw_bus_match,
1439 .uevent = ccw_uevent,
1440 .probe = ccw_device_probe,
1441 .remove = ccw_device_remove,
958974fb 1442 .shutdown = ccw_device_shutdown,
8bbace7e
CH
1443};
1444
b2ffd8e9
CH
1445/**
1446 * ccw_driver_register() - register a ccw driver
1447 * @cdriver: driver to be registered
1448 *
1449 * This function is mainly a wrapper around driver_register().
1450 * Returns:
1451 * %0 on success and a negative error value on failure.
1452 */
1453int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
1454{
1455 struct device_driver *drv = &cdriver->driver;
1456
1457 drv->bus = &ccw_bus_type;
1458 drv->name = cdriver->name;
1da177e4
LT
1459
1460 return driver_register(drv);
1461}
1462
b2ffd8e9
CH
1463/**
1464 * ccw_driver_unregister() - deregister a ccw driver
1465 * @cdriver: driver to be deregistered
1466 *
1467 * This function is mainly a wrapper around driver_unregister().
1468 */
1469void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
1470{
1471 driver_unregister(&cdriver->driver);
1472}
1473
a8237fc4
CH
1474/* Helper func for qdio. */
1475struct subchannel_id
1476ccw_device_get_subchannel_id(struct ccw_device *cdev)
1477{
1478 struct subchannel *sch;
1479
1480 sch = to_subchannel(cdev->dev.parent);
1481 return sch->schid;
1482}
1483
1da177e4
LT
1484MODULE_LICENSE("GPL");
1485EXPORT_SYMBOL(ccw_device_set_online);
1486EXPORT_SYMBOL(ccw_device_set_offline);
1487EXPORT_SYMBOL(ccw_driver_register);
1488EXPORT_SYMBOL(ccw_driver_unregister);
1489EXPORT_SYMBOL(get_ccwdev_by_busid);
1490EXPORT_SYMBOL(ccw_bus_type);
1491EXPORT_SYMBOL(ccw_device_work);
1492EXPORT_SYMBOL(ccw_device_notify_work);
a8237fc4 1493EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);