blob: 001682e70f672dc1be7e73e4da5918eb708427ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010026#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020056/* 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). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020059static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020060 struct ccw_device_id *id, const char *suffix)
61{
62 int len;
63
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020064 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020065 if (len > size)
66 return len;
67 buf += len;
68 size -= len;
69
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);
75
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020085 struct ccw_device_id *id = &(cdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i = 0;
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020087 int len = 0;
88 int ret;
89 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020091 /* CU_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020092 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;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020096
97 /* CU_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020098 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200104 /* DEV_TYPE= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200110 /* DEV_MODEL= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 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;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200115
116 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200117 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Cornelia Huck8bbace72006-01-11 10:56:22 +0100123struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Cornelia Huck8bbace72006-01-11 10:56:22 +0100125static int io_subchannel_probe (struct subchannel *);
126static int io_subchannel_remove (struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int io_subchannel_notify(struct device *, int);
128static void io_subchannel_verify(struct device *);
129static void io_subchannel_ioterm(struct device *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130static void io_subchannel_shutdown(struct subchannel *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200132static struct css_driver io_subchannel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .subchannel_type = SUBCHANNEL_TYPE_IO,
134 .drv = {
135 .name = "io_subchannel",
136 .bus = &css_bus_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 },
138 .irq = io_subchannel_irq,
139 .notify = io_subchannel_notify,
140 .verify = io_subchannel_verify,
141 .termination = io_subchannel_ioterm,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100142 .probe = io_subchannel_probe,
143 .remove = io_subchannel_remove,
144 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147struct workqueue_struct *ccw_device_work;
148struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200149wait_queue_head_t ccw_device_init_wq;
150atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
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
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400216chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200219 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ssize_t ret = 0;
221 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
233}
234
235static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400236pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
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
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400246devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
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
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400259cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
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
268static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800269modalias_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);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200273 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800274
Cornelia Huck086a6c62007-07-17 13:36:08 +0200275 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200276
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278}
279
280static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400281online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct ccw_device *cdev = to_ccwdev(dev);
284
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
286}
287
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100288int ccw_device_is_orphan(struct ccw_device *cdev)
289{
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
291}
292
Cornelia Huckef995162007-04-27 16:01:39 +0200293static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100294{
Cornelia Huck7674da72006-12-08 15:54:21 +0100295 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200296 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100297}
298
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200299static void ccw_device_remove_orphan_cb(struct device *dev)
300{
301 struct ccw_device *cdev = to_ccwdev(dev);
302
303 ccw_device_unregister(cdev);
304 put_device(&cdev->dev);
305}
306
307static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200310
311 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200312 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Reset intparm to zeroes. */
314 sch->schib.pmcw.intparm = 0;
315 cio_modify(sch);
316 put_device(&sch->dev);
317}
318
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319static void
320ccw_device_remove_disconnected(struct ccw_device *cdev)
321{
322 unsigned long flags;
323 int rc;
324
325 /*
326 * Forced offline in disconnected state means
327 * 'throw away device'.
328 */
329 if (ccw_device_is_orphan(cdev)) {
330 /*
331 * Deregister ccw device.
332 * Unfortunately, we cannot do this directly from the
333 * attribute method.
334 */
335 spin_lock_irqsave(cdev->ccwlock, flags);
336 cdev->private->state = DEV_STATE_NOT_OPER;
337 spin_unlock_irqrestore(cdev->ccwlock, flags);
338 rc = device_schedule_callback(&cdev->dev,
339 ccw_device_remove_orphan_cb);
340 if (rc)
341 dev_info(&cdev->dev, "Couldn't unregister orphan\n");
342 return;
343 }
344 /* Deregister subchannel, which will kill the ccw device. */
345 rc = device_schedule_callback(cdev->dev.parent,
346 ccw_device_remove_sch_cb);
347 if (rc)
348 dev_info(&cdev->dev,
349 "Couldn't unregister disconnected device\n");
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352int
353ccw_device_set_offline(struct ccw_device *cdev)
354{
355 int ret;
356
357 if (!cdev)
358 return -ENODEV;
359 if (!cdev->online || !cdev->drv)
360 return -EINVAL;
361
362 if (cdev->drv->set_offline) {
363 ret = cdev->drv->set_offline(cdev);
364 if (ret != 0)
365 return ret;
366 }
367 cdev->online = 0;
368 spin_lock_irq(cdev->ccwlock);
369 ret = ccw_device_offline(cdev);
370 if (ret == -ENODEV) {
371 if (cdev->private->state != DEV_STATE_NOT_OPER) {
372 cdev->private->state = DEV_STATE_OFFLINE;
373 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
374 }
375 spin_unlock_irq(cdev->ccwlock);
376 return ret;
377 }
378 spin_unlock_irq(cdev->ccwlock);
379 if (ret == 0)
380 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
381 else {
382 pr_debug("ccw_device_offline returned %d, device %s\n",
383 ret, cdev->dev.bus_id);
384 cdev->online = 1;
385 }
386 return ret;
387}
388
389int
390ccw_device_set_online(struct ccw_device *cdev)
391{
392 int ret;
393
394 if (!cdev)
395 return -ENODEV;
396 if (cdev->online || !cdev->drv)
397 return -EINVAL;
398
399 spin_lock_irq(cdev->ccwlock);
400 ret = ccw_device_online(cdev);
401 spin_unlock_irq(cdev->ccwlock);
402 if (ret == 0)
403 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
404 else {
405 pr_debug("ccw_device_online returned %d, device %s\n",
406 ret, cdev->dev.bus_id);
407 return ret;
408 }
409 if (cdev->private->state != DEV_STATE_ONLINE)
410 return -ENODEV;
411 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
412 cdev->online = 1;
413 return 0;
414 }
415 spin_lock_irq(cdev->ccwlock);
416 ret = ccw_device_offline(cdev);
417 spin_unlock_irq(cdev->ccwlock);
418 if (ret == 0)
419 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
420 else
421 pr_debug("ccw_device_offline returned %d, device %s\n",
422 ret, cdev->dev.bus_id);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800423 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200426static void online_store_handle_offline(struct ccw_device *cdev)
427{
428 if (cdev->private->state == DEV_STATE_DISCONNECTED)
429 ccw_device_remove_disconnected(cdev);
430 else if (cdev->drv && cdev->drv->set_offline)
431 ccw_device_set_offline(cdev);
432}
433
434static int online_store_recog_and_online(struct ccw_device *cdev)
435{
436 int ret;
437
438 /* Do device recognition, if needed. */
439 if (cdev->id.cu_type == 0) {
440 ret = ccw_device_recognition(cdev);
441 if (ret) {
442 printk(KERN_WARNING"Couldn't start recognition "
443 "for device %s (ret=%d)\n",
444 cdev->dev.bus_id, ret);
445 return ret;
446 }
447 wait_event(cdev->private->wait_q,
448 cdev->private->flags.recog_done);
449 }
450 if (cdev->drv && cdev->drv->set_online)
451 ccw_device_set_online(cdev);
452 return 0;
453}
454static void online_store_handle_online(struct ccw_device *cdev, int force)
455{
456 int ret;
457
458 ret = online_store_recog_and_online(cdev);
459 if (ret)
460 return;
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 return;
467 }
468 if (cdev->id.cu_type == 0)
469 cdev->private->state = DEV_STATE_NOT_OPER;
470 online_store_recog_and_online(cdev);
471 }
472
473}
474
475static ssize_t online_store (struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200479 int i, force;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 char *tmp;
481
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800482 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EAGAIN;
484
485 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
486 atomic_set(&cdev->private->onoff, 0);
487 return -EINVAL;
488 }
489 if (!strncmp(buf, "force\n", count)) {
490 force = 1;
491 i = 1;
492 } else {
493 force = 0;
494 i = simple_strtoul(buf, &tmp, 16);
495 }
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496
497 switch (i) {
498 case 0:
499 online_store_handle_offline(cdev);
500 break;
501 case 1:
502 online_store_handle_online(cdev, force);
503 break;
504 default:
505 count = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (cdev->drv)
508 module_put(cdev->drv->owner);
509 atomic_set(&cdev->private->onoff, 0);
510 return count;
511}
512
513static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400514available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct ccw_device *cdev = to_ccwdev(dev);
517 struct subchannel *sch;
518
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100519 if (ccw_device_is_orphan(cdev))
520 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 switch (cdev->private->state) {
522 case DEV_STATE_BOXED:
523 return sprintf(buf, "boxed\n");
524 case DEV_STATE_DISCONNECTED:
525 case DEV_STATE_DISCONNECTED_SENSE_ID:
526 case DEV_STATE_NOT_OPER:
527 sch = to_subchannel(dev->parent);
528 if (!sch->lpm)
529 return sprintf(buf, "no path\n");
530 else
531 return sprintf(buf, "no device\n");
532 default:
533 /* All other states considered fine. */
534 return sprintf(buf, "good\n");
535 }
536}
537
538static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
539static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
540static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
541static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800542static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static DEVICE_ATTR(online, 0644, online_show, online_store);
544extern struct device_attribute dev_attr_cmb_enable;
545static DEVICE_ATTR(availability, 0444, available_show, NULL);
546
547static struct attribute * subch_attrs[] = {
548 &dev_attr_chpids.attr,
549 &dev_attr_pimpampom.attr,
550 NULL,
551};
552
553static struct attribute_group subch_attr_group = {
554 .attrs = subch_attrs,
555};
556
Cornelia Huck529192f2006-12-08 15:55:57 +0100557struct attribute_group *subch_attr_groups[] = {
558 &subch_attr_group,
559 NULL,
560};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562static struct attribute * ccwdev_attrs[] = {
563 &dev_attr_devtype.attr,
564 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800565 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 &dev_attr_online.attr,
567 &dev_attr_cmb_enable.attr,
568 &dev_attr_availability.attr,
569 NULL,
570};
571
572static struct attribute_group ccwdev_attr_group = {
573 .attrs = ccwdev_attrs,
574};
575
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200576static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200577 &ccwdev_attr_group,
578 NULL,
579};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581/* this is a simple abstraction for device_register that sets the
582 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200583static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct device *dev = &cdev->dev;
586 int ret;
587
588 dev->bus = &ccw_bus_type;
589
590 if ((ret = device_add(dev)))
591 return ret;
592
593 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return ret;
595}
596
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700597struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200598 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700599 struct ccw_device * sibling;
600};
601
602static int
603match_devno(struct device * dev, void * data)
604{
Cornelia Huck78964262006-10-11 15:31:38 +0200605 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700606 struct ccw_device * cdev;
607
608 cdev = to_ccwdev(dev);
609 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100610 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200611 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100612 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700613 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700614 return 0;
615}
616
Cornelia Huck78964262006-10-11 15:31:38 +0200617static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
618 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200621 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Cornelia Huck78964262006-10-11 15:31:38 +0200623 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200624 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700625 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700627 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100630static int match_orphan(struct device *dev, void *data)
631{
632 struct ccw_dev_id *dev_id;
633 struct ccw_device *cdev;
634
635 dev_id = data;
636 cdev = to_ccwdev(dev);
637 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
638}
639
640static struct ccw_device *
641get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
642 struct ccw_dev_id *dev_id)
643{
644 struct device *dev;
645
646 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
647 match_orphan);
648
649 return dev ? to_ccwdev(dev) : NULL;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100653ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100655 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct ccw_device *cdev;
657
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100658 priv = container_of(work, struct ccw_device_private, kick_work);
659 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (device_add(&cdev->dev)) {
661 put_device(&cdev->dev);
662 return;
663 }
664 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100667void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100669 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct ccw_device *cdev;
671 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100673 priv = container_of(work, struct ccw_device_private, kick_work);
674 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Cornelia Huckef995162007-04-27 16:01:39 +0200677 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100679 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 queue_work(ccw_device_work, &cdev->private->kick_work);
681}
682
683static void
684ccw_device_release(struct device *dev)
685{
686 struct ccw_device *cdev;
687
688 cdev = to_ccwdev(dev);
689 kfree(cdev->private);
690 kfree(cdev);
691}
692
Cornelia Huck7674da72006-12-08 15:54:21 +0100693static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
694{
695 struct ccw_device *cdev;
696
697 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
698 if (cdev) {
699 cdev->private = kzalloc(sizeof(struct ccw_device_private),
700 GFP_KERNEL | GFP_DMA);
701 if (cdev->private)
702 return cdev;
703 }
704 kfree(cdev);
705 return ERR_PTR(-ENOMEM);
706}
707
708static int io_subchannel_initialize_dev(struct subchannel *sch,
709 struct ccw_device *cdev)
710{
711 cdev->private->cdev = cdev;
712 atomic_set(&cdev->private->onoff, 0);
713 cdev->dev.parent = &sch->dev;
714 cdev->dev.release = ccw_device_release;
715 INIT_LIST_HEAD(&cdev->private->kick_work.entry);
Cornelia Huckef995162007-04-27 16:01:39 +0200716 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100717 /* Do first half of device_register. */
718 device_initialize(&cdev->dev);
719 if (!get_device(&sch->dev)) {
720 if (cdev->dev.release)
721 cdev->dev.release(&cdev->dev);
722 return -ENODEV;
723 }
724 return 0;
725}
726
727static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
728{
729 struct ccw_device *cdev;
730 int ret;
731
732 cdev = io_subchannel_allocate_dev(sch);
733 if (!IS_ERR(cdev)) {
734 ret = io_subchannel_initialize_dev(sch, cdev);
735 if (ret) {
736 kfree(cdev);
737 cdev = ERR_PTR(ret);
738 }
739 }
740 return cdev;
741}
742
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100743static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
744
745static void sch_attach_device(struct subchannel *sch,
746 struct ccw_device *cdev)
747{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200748 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100749 spin_lock_irq(sch->lock);
750 sch->dev.driver_data = cdev;
751 cdev->private->schid = sch->schid;
752 cdev->ccwlock = sch->lock;
753 device_trigger_reprobe(sch);
754 spin_unlock_irq(sch->lock);
755}
756
757static void sch_attach_disconnected_device(struct subchannel *sch,
758 struct ccw_device *cdev)
759{
760 struct subchannel *other_sch;
761 int ret;
762
763 other_sch = to_subchannel(get_device(cdev->dev.parent));
764 ret = device_move(&cdev->dev, &sch->dev);
765 if (ret) {
766 CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
767 "(ret=%d)!\n", cdev->private->dev_id.ssid,
768 cdev->private->dev_id.devno, ret);
769 put_device(&other_sch->dev);
770 return;
771 }
772 other_sch->dev.driver_data = NULL;
773 /* No need to keep a subchannel without ccw device around. */
774 css_sch_device_unregister(other_sch);
775 put_device(&other_sch->dev);
776 sch_attach_device(sch, cdev);
777}
778
779static void sch_attach_orphaned_device(struct subchannel *sch,
780 struct ccw_device *cdev)
781{
782 int ret;
783
784 /* Try to move the ccw device to its new subchannel. */
785 ret = device_move(&cdev->dev, &sch->dev);
786 if (ret) {
787 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
788 "failed (ret=%d)!\n",
789 cdev->private->dev_id.ssid,
790 cdev->private->dev_id.devno, ret);
791 return;
792 }
793 sch_attach_device(sch, cdev);
794}
795
796static void sch_create_and_recog_new_device(struct subchannel *sch)
797{
798 struct ccw_device *cdev;
799
800 /* Need to allocate a new ccw device. */
801 cdev = io_subchannel_create_ccwdev(sch);
802 if (IS_ERR(cdev)) {
803 /* OK, we did everything we could... */
804 css_sch_device_unregister(sch);
805 return;
806 }
807 spin_lock_irq(sch->lock);
808 sch->dev.driver_data = cdev;
809 spin_unlock_irq(sch->lock);
810 /* Start recognition for the new ccw device. */
811 if (io_subchannel_recog(cdev, sch)) {
812 spin_lock_irq(sch->lock);
813 sch->dev.driver_data = NULL;
814 spin_unlock_irq(sch->lock);
815 if (cdev->dev.release)
816 cdev->dev.release(&cdev->dev);
817 css_sch_device_unregister(sch);
818 }
819}
820
821
822void ccw_device_move_to_orphanage(struct work_struct *work)
823{
824 struct ccw_device_private *priv;
825 struct ccw_device *cdev;
826 struct ccw_device *replacing_cdev;
827 struct subchannel *sch;
828 int ret;
829 struct channel_subsystem *css;
830 struct ccw_dev_id dev_id;
831
832 priv = container_of(work, struct ccw_device_private, kick_work);
833 cdev = priv->cdev;
834 sch = to_subchannel(cdev->dev.parent);
835 css = to_css(sch->dev.parent);
836 dev_id.devno = sch->schib.pmcw.dev;
837 dev_id.ssid = sch->schid.ssid;
838
839 /*
840 * Move the orphaned ccw device to the orphanage so the replacing
841 * ccw device can take its place on the subchannel.
842 */
843 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
844 if (ret) {
845 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
846 "(ret=%d)!\n", cdev->private->dev_id.ssid,
847 cdev->private->dev_id.devno, ret);
848 return;
849 }
850 cdev->ccwlock = css->pseudo_subchannel->lock;
851 /*
852 * Search for the replacing ccw device
853 * - among the disconnected devices
854 * - in the orphanage
855 */
856 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
857 if (replacing_cdev) {
858 sch_attach_disconnected_device(sch, replacing_cdev);
859 return;
860 }
861 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
862 if (replacing_cdev) {
863 sch_attach_orphaned_device(sch, replacing_cdev);
864 return;
865 }
866 sch_create_and_recog_new_device(sch);
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/*
870 * Register recognized device.
871 */
872static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100873io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100875 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct ccw_device *cdev;
877 struct subchannel *sch;
878 int ret;
879 unsigned long flags;
880
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100881 priv = container_of(work, struct ccw_device_private, kick_work);
882 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200884 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100885 /*
886 * io_subchannel_register() will also be called after device
887 * recognition has been done for a boxed device (which will already
888 * be registered). We need to reprobe since we may now have sense id
889 * information.
890 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700891 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100892 if (!cdev->drv) {
893 ret = device_reprobe(&cdev->dev);
894 if (ret)
895 /* We can't do much here. */
896 dev_info(&cdev->dev, "device_reprobe() returned"
897 " %d\n", ret);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700901 /*
902 * Now we know this subchannel will stay, we can throw
903 * our delayed uevent.
904 */
905 sch->dev.uevent_suppress = 0;
906 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* make it known to the system */
908 ret = ccw_device_register(cdev);
909 if (ret) {
910 printk (KERN_WARNING "%s: could not register %s\n",
911 __func__, cdev->dev.bus_id);
912 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100913 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100915 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 kfree (cdev->private);
917 kfree (cdev);
918 put_device(&sch->dev);
919 if (atomic_dec_and_test(&ccw_device_init_count))
920 wake_up(&ccw_device_init_wq);
921 return;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 put_device(&cdev->dev);
924out:
925 cdev->private->flags.recog_done = 1;
926 put_device(&sch->dev);
927 wake_up(&cdev->private->wait_q);
928 if (atomic_dec_and_test(&ccw_device_init_count))
929 wake_up(&ccw_device_init_wq);
930}
931
932void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100933ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100935 struct ccw_device_private *priv;
936 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct subchannel *sch;
938
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100939 priv = container_of(work, struct ccw_device_private, kick_work);
940 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200942 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* Reset intparm to zeroes. */
944 sch->schib.pmcw.intparm = 0;
945 cio_modify(sch);
946 put_device(&cdev->dev);
947 put_device(&sch->dev);
948}
949
950/*
951 * subchannel recognition done. Called from the state machine.
952 */
953void
954io_subchannel_recog_done(struct ccw_device *cdev)
955{
956 struct subchannel *sch;
957
958 if (css_init_done == 0) {
959 cdev->private->flags.recog_done = 1;
960 return;
961 }
962 switch (cdev->private->state) {
963 case DEV_STATE_NOT_OPER:
964 cdev->private->flags.recog_done = 1;
965 /* Remove device found not operational. */
966 if (!get_device(&cdev->dev))
967 break;
968 sch = to_subchannel(cdev->dev.parent);
969 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100970 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 queue_work(slow_path_wq, &cdev->private->kick_work);
972 if (atomic_dec_and_test(&ccw_device_init_count))
973 wake_up(&ccw_device_init_wq);
974 break;
975 case DEV_STATE_BOXED:
976 /* Device did not respond in time. */
977 case DEV_STATE_OFFLINE:
978 /*
979 * We can't register the device in interrupt context so
980 * we schedule a work item.
981 */
982 if (!get_device(&cdev->dev))
983 break;
984 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100985 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 queue_work(slow_path_wq, &cdev->private->kick_work);
987 break;
988 }
989}
990
991static int
992io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
993{
994 int rc;
995 struct ccw_device_private *priv;
996
997 sch->dev.driver_data = cdev;
998 sch->driver = &io_subchannel_driver;
Cornelia Huck2ec22982006-12-08 15:54:26 +0100999 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Init private data. */
1002 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001003 priv->dev_id.devno = sch->schib.pmcw.dev;
1004 priv->dev_id.ssid = sch->schid.ssid;
1005 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 priv->state = DEV_STATE_NOT_OPER;
1007 INIT_LIST_HEAD(&priv->cmb_list);
1008 init_waitqueue_head(&priv->wait_q);
1009 init_timer(&priv->timer);
1010
1011 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001012 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1013 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Increase counter of devices currently in recognition. */
1016 atomic_inc(&ccw_device_init_count);
1017
1018 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001019 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001021 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (rc) {
1023 if (atomic_dec_and_test(&ccw_device_init_count))
1024 wake_up(&ccw_device_init_wq);
1025 }
1026 return rc;
1027}
1028
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001029static void ccw_device_move_to_sch(struct work_struct *work)
1030{
1031 struct ccw_device_private *priv;
1032 int rc;
1033 struct subchannel *sch;
1034 struct ccw_device *cdev;
1035 struct subchannel *former_parent;
1036
1037 priv = container_of(work, struct ccw_device_private, kick_work);
1038 sch = priv->sch;
1039 cdev = priv->cdev;
1040 former_parent = ccw_device_is_orphan(cdev) ?
1041 NULL : to_subchannel(get_device(cdev->dev.parent));
1042 mutex_lock(&sch->reg_mutex);
1043 /* Try to move the ccw device to its new subchannel. */
1044 rc = device_move(&cdev->dev, &sch->dev);
1045 mutex_unlock(&sch->reg_mutex);
1046 if (rc) {
1047 CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
1048 "0.%x.%04x failed (ret=%d)!\n",
1049 cdev->private->dev_id.ssid,
1050 cdev->private->dev_id.devno, sch->schid.ssid,
1051 sch->schid.sch_no, rc);
1052 css_sch_device_unregister(sch);
1053 goto out;
1054 }
1055 if (former_parent) {
1056 spin_lock_irq(former_parent->lock);
1057 former_parent->dev.driver_data = NULL;
1058 spin_unlock_irq(former_parent->lock);
1059 css_sch_device_unregister(former_parent);
1060 /* Reset intparm to zeroes. */
1061 former_parent->schib.pmcw.intparm = 0;
1062 cio_modify(former_parent);
1063 }
1064 sch_attach_device(sch, cdev);
1065out:
1066 if (former_parent)
1067 put_device(&former_parent->dev);
1068 put_device(&cdev->dev);
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001072io_subchannel_probe (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct ccw_device *cdev;
1075 int rc;
1076 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001077 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (sch->dev.driver_data) {
1080 /*
1081 * This subchannel already has an associated ccw_device.
1082 * Register it and exit. This happens for all early
1083 * device, e.g. the console.
1084 */
1085 cdev = sch->dev.driver_data;
1086 device_initialize(&cdev->dev);
1087 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /*
1089 * Check if the device is already online. If it is
1090 * the reference count needs to be corrected
1091 * (see ccw_device_online and css_init_done for the
1092 * ugly details).
1093 */
1094 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1095 cdev->private->state != DEV_STATE_OFFLINE &&
1096 cdev->private->state != DEV_STATE_BOXED)
1097 get_device(&cdev->dev);
1098 return 0;
1099 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001100 /*
1101 * First check if a fitting device may be found amongst the
1102 * disconnected devices or in the orphanage.
1103 */
1104 dev_id.devno = sch->schib.pmcw.dev;
1105 dev_id.ssid = sch->schid.ssid;
1106 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1107 if (!cdev)
1108 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1109 &dev_id);
1110 if (cdev) {
1111 /*
1112 * Schedule moving the device until when we have a registered
1113 * subchannel to move to and succeed the probe. We can
1114 * unregister later again, when the probe is through.
1115 */
1116 cdev->private->sch = sch;
1117 PREPARE_WORK(&cdev->private->kick_work,
1118 ccw_device_move_to_sch);
1119 queue_work(slow_path_wq, &cdev->private->kick_work);
1120 return 0;
1121 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001122 cdev = io_subchannel_create_ccwdev(sch);
1123 if (IS_ERR(cdev))
1124 return PTR_ERR(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Cornelia Huck8bbace72006-01-11 10:56:22 +01001126 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001128 spin_lock_irqsave(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sch->dev.driver_data = NULL;
Cornelia Huck2ec22982006-12-08 15:54:26 +01001130 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (cdev->dev.release)
1132 cdev->dev.release(&cdev->dev);
1133 }
1134
1135 return rc;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001139io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct ccw_device *cdev;
1142 unsigned long flags;
1143
Cornelia Huck8bbace72006-01-11 10:56:22 +01001144 if (!sch->dev.driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001146 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Set ccw device to not operational and drop reference. */
1148 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huck8bbace72006-01-11 10:56:22 +01001149 sch->dev.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cdev->private->state = DEV_STATE_NOT_OPER;
1151 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001152 ccw_device_unregister(cdev);
1153 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return 0;
1155}
1156
1157static int
1158io_subchannel_notify(struct device *dev, int event)
1159{
1160 struct ccw_device *cdev;
1161
1162 cdev = dev->driver_data;
1163 if (!cdev)
1164 return 0;
1165 if (!cdev->drv)
1166 return 0;
1167 if (!cdev->online)
1168 return 0;
1169 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
1170}
1171
1172static void
1173io_subchannel_verify(struct device *dev)
1174{
1175 struct ccw_device *cdev;
1176
1177 cdev = dev->driver_data;
1178 if (cdev)
1179 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1180}
1181
1182static void
1183io_subchannel_ioterm(struct device *dev)
1184{
1185 struct ccw_device *cdev;
1186
1187 cdev = dev->driver_data;
1188 if (!cdev)
1189 return;
Cornelia Huckd23861f2006-12-04 15:41:04 +01001190 /* Internal I/O will be retried by the interrupt handler. */
1191 if (cdev->private->flags.intretry)
1192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1194 if (cdev->handler)
1195 cdev->handler(cdev, cdev->private->intparm,
1196 ERR_PTR(-EIO));
1197}
1198
1199static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001200io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct ccw_device *cdev;
1203 int ret;
1204
Cornelia Huck8bbace72006-01-11 10:56:22 +01001205 cdev = sch->dev.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001207 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return;
1209 if (!sch->schib.pmcw.ena)
1210 /* Nothing to do. */
1211 return;
1212 ret = cio_disable_subchannel(sch);
1213 if (ret != -EBUSY)
1214 /* Subchannel is disabled, we're done. */
1215 return;
1216 cdev->private->state = DEV_STATE_QUIESCE;
1217 if (cdev->handler)
1218 cdev->handler(cdev, cdev->private->intparm,
1219 ERR_PTR(-EIO));
1220 ret = ccw_device_cancel_halt_clear(cdev);
1221 if (ret == -EBUSY) {
1222 ccw_device_set_timeout(cdev, HZ/10);
1223 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1224 }
1225 cio_disable_subchannel(sch);
1226}
1227
1228#ifdef CONFIG_CCW_CONSOLE
1229static struct ccw_device console_cdev;
1230static struct ccw_device_private console_private;
1231static int console_cdev_in_use;
1232
Cornelia Huck2ec22982006-12-08 15:54:26 +01001233static DEFINE_SPINLOCK(ccw_console_lock);
1234
1235spinlock_t * cio_get_console_lock(void)
1236{
1237 return &ccw_console_lock;
1238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240static int
1241ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
1242{
1243 int rc;
1244
1245 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001246 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rc = io_subchannel_recog(cdev, sch);
1248 if (rc)
1249 return rc;
1250
1251 /* Now wait for the async. recognition to come to an end. */
1252 spin_lock_irq(cdev->ccwlock);
1253 while (!dev_fsm_final_state(cdev))
1254 wait_cons_dev();
1255 rc = -EIO;
1256 if (cdev->private->state != DEV_STATE_OFFLINE)
1257 goto out_unlock;
1258 ccw_device_online(cdev);
1259 while (!dev_fsm_final_state(cdev))
1260 wait_cons_dev();
1261 if (cdev->private->state != DEV_STATE_ONLINE)
1262 goto out_unlock;
1263 rc = 0;
1264out_unlock:
1265 spin_unlock_irq(cdev->ccwlock);
1266 return 0;
1267}
1268
1269struct ccw_device *
1270ccw_device_probe_console(void)
1271{
1272 struct subchannel *sch;
1273 int ret;
1274
1275 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001276 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sch = cio_probe_console();
1278 if (IS_ERR(sch)) {
1279 console_cdev_in_use = 0;
1280 return (void *) sch;
1281 }
1282 memset(&console_cdev, 0, sizeof(struct ccw_device));
1283 memset(&console_private, 0, sizeof(struct ccw_device_private));
1284 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001285 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 ret = ccw_device_console_enable(&console_cdev, sch);
1287 if (ret) {
1288 cio_release_console();
1289 console_cdev_in_use = 0;
1290 return ERR_PTR(ret);
1291 }
1292 console_cdev.online = 1;
1293 return &console_cdev;
1294}
1295#endif
1296
1297/*
1298 * get ccw_device matching the busid, but only if owned by cdrv
1299 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001300static int
1301__ccwdev_check_busid(struct device *dev, void *id)
1302{
1303 char *bus_id;
1304
Cornelia Huck12975ae2006-10-11 15:31:47 +02001305 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001306
1307 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1308}
1309
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311struct ccw_device *
1312get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
1313{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001314 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct device_driver *drv;
1316
1317 drv = get_driver(&cdrv->driver);
1318 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001319 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001321 dev = driver_find_device(drv, NULL, (void *)bus_id,
1322 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 put_driver(drv);
1324
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001325 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328/************************** device driver handling ************************/
1329
1330/* This is the implementation of the ccw_driver class. The probe, remove
1331 * and release methods are initially very similar to the device_driver
1332 * implementations, with the difference that they have ccw_device
1333 * arguments.
1334 *
1335 * A ccw driver also contains the information that is needed for
1336 * device matching.
1337 */
1338static int
1339ccw_device_probe (struct device *dev)
1340{
1341 struct ccw_device *cdev = to_ccwdev(dev);
1342 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1343 int ret;
1344
1345 cdev->drv = cdrv; /* to let the driver call _set_online */
1346
1347 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1348
1349 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001350 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return ret;
1352 }
1353
1354 return 0;
1355}
1356
1357static int
1358ccw_device_remove (struct device *dev)
1359{
1360 struct ccw_device *cdev = to_ccwdev(dev);
1361 struct ccw_driver *cdrv = cdev->drv;
1362 int ret;
1363
1364 pr_debug("removing device %s\n", cdev->dev.bus_id);
1365 if (cdrv->remove)
1366 cdrv->remove(cdev);
1367 if (cdev->online) {
1368 cdev->online = 0;
1369 spin_lock_irq(cdev->ccwlock);
1370 ret = ccw_device_offline(cdev);
1371 spin_unlock_irq(cdev->ccwlock);
1372 if (ret == 0)
1373 wait_event(cdev->private->wait_q,
1374 dev_fsm_final_state(cdev));
1375 else
1376 //FIXME: we can't fail!
1377 pr_debug("ccw_device_offline returned %d, device %s\n",
1378 ret, cdev->dev.bus_id);
1379 }
1380 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001381 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
1384
Cornelia Huck8bbace72006-01-11 10:56:22 +01001385struct bus_type ccw_bus_type = {
1386 .name = "ccw",
1387 .match = ccw_bus_match,
1388 .uevent = ccw_uevent,
1389 .probe = ccw_device_probe,
1390 .remove = ccw_device_remove,
1391};
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393int
1394ccw_driver_register (struct ccw_driver *cdriver)
1395{
1396 struct device_driver *drv = &cdriver->driver;
1397
1398 drv->bus = &ccw_bus_type;
1399 drv->name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return driver_register(drv);
1402}
1403
1404void
1405ccw_driver_unregister (struct ccw_driver *cdriver)
1406{
1407 driver_unregister(&cdriver->driver);
1408}
1409
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001410/* Helper func for qdio. */
1411struct subchannel_id
1412ccw_device_get_subchannel_id(struct ccw_device *cdev)
1413{
1414 struct subchannel *sch;
1415
1416 sch = to_subchannel(cdev->dev.parent);
1417 return sch->schid;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420MODULE_LICENSE("GPL");
1421EXPORT_SYMBOL(ccw_device_set_online);
1422EXPORT_SYMBOL(ccw_device_set_offline);
1423EXPORT_SYMBOL(ccw_driver_register);
1424EXPORT_SYMBOL(ccw_driver_unregister);
1425EXPORT_SYMBOL(get_ccwdev_by_busid);
1426EXPORT_SYMBOL(ccw_bus_type);
1427EXPORT_SYMBOL(ccw_device_work);
1428EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001429EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);