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