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