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