blob: e47aa3f0476938d81dae726df6968d6c91930e76 [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 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010019#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020024#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020025#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020027#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010029#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "css.h"
31#include "device.h"
32#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010033#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020034#include "blacklist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010036static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010037static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010038static int recovery_phase;
39static const unsigned long recovery_delay[] = { 3, 30, 300 };
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/******************* bus type handling ***********************/
42
43/* The Linux driver model distinguishes between a bus type and
44 * the bus itself. Of course we only have one channel
45 * subsystem driver and one channel system per machine, but
46 * we still use the abstraction. T.R. says it's a good idea. */
47static int
48ccw_bus_match (struct device * dev, struct device_driver * drv)
49{
50 struct ccw_device *cdev = to_ccwdev(dev);
51 struct ccw_driver *cdrv = to_ccwdrv(drv);
52 const struct ccw_device_id *ids = cdrv->ids, *found;
53
54 if (!ids)
55 return 0;
56
57 found = ccw_device_id_match(ids, &cdev->id);
58 if (!found)
59 return 0;
60
61 cdev->id.driver_info = found->driver_info;
62
63 return 1;
64}
65
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020066/* Store modalias string delimited by prefix/suffix string into buffer with
67 * specified size. Return length of resulting string (excluding trailing '\0')
68 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020069static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020070 struct ccw_device_id *id, const char *suffix)
71{
72 int len;
73
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020074 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020075 if (len > size)
76 return len;
77 buf += len;
78 size -= len;
79
80 if (id->dev_type != 0)
81 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
82 id->dev_model, suffix);
83 else
84 len += snprintf(buf, size, "dtdm%s", suffix);
85
86 return len;
87}
88
89/* Set up environment variables for ccw device uevent. Return 0 on success,
90 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020091static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020094 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020095 int ret;
96 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020098 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020099 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200100 if (ret)
101 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200102
103 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200105 if (ret)
106 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200109 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200110 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200111 if (ret)
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200114 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200115 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200116 if (ret)
117 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200118
119 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200120 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200121 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Cornelia Huck8bbace72006-01-11 10:56:22 +0100125struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Cornelia Huck602b20f2008-01-26 14:10:39 +0100127static void io_subchannel_irq(struct subchannel *);
128static int io_subchannel_probe(struct subchannel *);
129static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100130static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200131static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200132static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
133 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Cornelia Huckf08adc02008-07-14 09:59:03 +0200135static struct css_device_id io_subchannel_ids[] = {
136 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
137 { /* end of list */ },
138};
139MODULE_DEVICE_TABLE(css, io_subchannel_ids);
140
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200141static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100142 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200143 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100144 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200146 .sch_event = io_subchannel_sch_event,
147 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100148 .probe = io_subchannel_probe,
149 .remove = io_subchannel_remove,
150 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153struct workqueue_struct *ccw_device_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200154wait_queue_head_t ccw_device_init_wq;
155atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100157static void recovery_func(unsigned long data);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int __init
160init_ccw_bus_type (void)
161{
162 int ret;
163
164 init_waitqueue_head(&ccw_device_init_wq);
165 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100166 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 ccw_device_work = create_singlethread_workqueue("cio");
169 if (!ccw_device_work)
170 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 slow_path_wq = create_singlethread_workqueue("kslowcrw");
172 if (!slow_path_wq) {
173 ret = -ENOMEM; /* FIXME: better errno ? */
174 goto out_err;
175 }
176 if ((ret = bus_register (&ccw_bus_type)))
177 goto out_err;
178
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100179 ret = css_driver_register(&io_subchannel_driver);
180 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out_err;
182
183 wait_event(ccw_device_init_wq,
184 atomic_read(&ccw_device_init_count) == 0);
185 flush_workqueue(ccw_device_work);
186 return 0;
187out_err:
188 if (ccw_device_work)
189 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (slow_path_wq)
191 destroy_workqueue(slow_path_wq);
192 return ret;
193}
194
195static void __exit
196cleanup_ccw_bus_type (void)
197{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100198 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 destroy_workqueue(ccw_device_work);
201}
202
203subsys_initcall(init_ccw_bus_type);
204module_exit(cleanup_ccw_bus_type);
205
206/************************ device handling **************************/
207
208/*
209 * A ccw_device has some interfaces in sysfs in addition to the
210 * standard ones.
211 * The following entries are designed to export the information which
212 * resided in 2.4 in /proc/subchannels. Subchannel and device number
213 * are obvious, so they don't have an entry :)
214 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
215 */
216static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400217chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200220 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ssize_t ret = 0;
222 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200225 for (chp = 0; chp < 8; chp++) {
226 mask = 0x80 >> chp;
227 if (ssd->path_mask & mask)
228 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
229 else
230 ret += sprintf(buf + ret, "00 ");
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ret += sprintf (buf+ret, "\n");
233 return min((ssize_t)PAGE_SIZE, ret);
234}
235
236static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400237pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct subchannel *sch = to_subchannel(dev);
240 struct pmcw *pmcw = &sch->schib.pmcw;
241
242 return sprintf (buf, "%02x %02x %02x\n",
243 pmcw->pim, pmcw->pam, pmcw->pom);
244}
245
246static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400247devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct ccw_device *cdev = to_ccwdev(dev);
250 struct ccw_device_id *id = &(cdev->id);
251
252 if (id->dev_type != 0)
253 return sprintf(buf, "%04x/%02x\n",
254 id->dev_type, id->dev_model);
255 else
256 return sprintf(buf, "n/a\n");
257}
258
259static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400260cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct ccw_device *cdev = to_ccwdev(dev);
263 struct ccw_device_id *id = &(cdev->id);
264
265 return sprintf(buf, "%04x/%02x\n",
266 id->cu_type, id->cu_model);
267}
268
269static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800270modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
271{
272 struct ccw_device *cdev = to_ccwdev(dev);
273 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200274 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800275
Cornelia Huck086a6c62007-07-17 13:36:08 +0200276 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200277
278 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800279}
280
281static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400282online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct ccw_device *cdev = to_ccwdev(dev);
285
286 return sprintf(buf, cdev->online ? "1\n" : "0\n");
287}
288
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100289int ccw_device_is_orphan(struct ccw_device *cdev)
290{
291 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
292}
293
Cornelia Huckef995162007-04-27 16:01:39 +0200294static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100295{
Cornelia Huck7674da72006-12-08 15:54:21 +0100296 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200297 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100298}
299
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200300static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200301{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200302 struct ccw_device_private *priv;
303 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200304
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200305 priv = container_of(work, struct ccw_device_private, kick_work);
306 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200307 ccw_device_unregister(cdev);
308 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200309 /* Release cdev reference for workqueue processing. */
310 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200311}
312
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200313static void
314ccw_device_remove_disconnected(struct ccw_device *cdev)
315{
316 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200317
318 /*
319 * Forced offline in disconnected state means
320 * 'throw away device'.
321 */
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200322 /* Get cdev reference for workqueue processing. */
323 if (!get_device(&cdev->dev))
324 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200325 if (ccw_device_is_orphan(cdev)) {
326 /*
327 * Deregister ccw device.
328 * Unfortunately, we cannot do this directly from the
329 * attribute method.
330 */
331 spin_lock_irqsave(cdev->ccwlock, flags);
332 cdev->private->state = DEV_STATE_NOT_OPER;
333 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200334 PREPARE_WORK(&cdev->private->kick_work,
335 ccw_device_remove_orphan_cb);
Sebastian Ottc4621a62009-03-31 19:16:04 +0200336 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200337 } else
338 /* Deregister subchannel, which will kill the ccw device. */
Sebastian Ottc4621a62009-03-31 19:16:04 +0200339 ccw_device_schedule_sch_unregister(cdev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200340}
341
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200342/**
343 * ccw_device_set_offline() - disable a ccw device for I/O
344 * @cdev: target ccw device
345 *
346 * This function calls the driver's set_offline() function for @cdev, if
347 * given, and then disables @cdev.
348 * Returns:
349 * %0 on success and a negative error value on failure.
350 * Context:
351 * enabled, ccw device lock not held
352 */
353int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
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);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100376 /* Give up reference from ccw_device_set_online(). */
377 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return ret;
379 }
380 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100381 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100383 /* Give up reference from ccw_device_set_online(). */
384 put_device(&cdev->dev);
385 } else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200386 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200387 "device 0.%x.%04x\n",
388 ret, cdev->private->dev_id.ssid,
389 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 cdev->online = 1;
391 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100392 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200395/**
396 * ccw_device_set_online() - enable a ccw device for I/O
397 * @cdev: target ccw device
398 *
399 * This function first enables @cdev and then calls the driver's set_online()
400 * function for @cdev, if given. If set_online() returns an error, @cdev is
401 * disabled again.
402 * Returns:
403 * %0 on success and a negative error value on failure.
404 * Context:
405 * enabled, ccw device lock not held
406 */
407int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int ret;
410
411 if (!cdev)
412 return -ENODEV;
413 if (cdev->online || !cdev->drv)
414 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100415 /* Hold on to an extra reference while device is online. */
416 if (!get_device(&cdev->dev))
417 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 spin_lock_irq(cdev->ccwlock);
420 ret = ccw_device_online(cdev);
421 spin_unlock_irq(cdev->ccwlock);
422 if (ret == 0)
423 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
424 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200425 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200426 "device 0.%x.%04x\n",
427 ret, cdev->private->dev_id.ssid,
428 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100429 /* Give up online reference since onlining failed. */
430 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return ret;
432 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100433 if (cdev->private->state != DEV_STATE_ONLINE) {
434 /* Give up online reference since onlining failed. */
435 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
439 cdev->online = 1;
440 return 0;
441 }
442 spin_lock_irq(cdev->ccwlock);
443 ret = ccw_device_offline(cdev);
444 spin_unlock_irq(cdev->ccwlock);
445 if (ret == 0)
446 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200447 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200448 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200449 "device 0.%x.%04x\n",
450 ret, cdev->private->dev_id.ssid,
451 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100452 /* Give up online reference since onlining failed. */
453 put_device(&cdev->dev);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800454 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100457static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200458{
459 if (cdev->private->state == DEV_STATE_DISCONNECTED)
460 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100461 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
462 return ccw_device_set_offline(cdev);
463 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200464}
465
466static int online_store_recog_and_online(struct ccw_device *cdev)
467{
468 int ret;
469
470 /* Do device recognition, if needed. */
471 if (cdev->id.cu_type == 0) {
472 ret = ccw_device_recognition(cdev);
473 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200474 CIO_MSG_EVENT(0, "Couldn't start recognition "
475 "for device 0.%x.%04x (ret=%d)\n",
476 cdev->private->dev_id.ssid,
477 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478 return ret;
479 }
480 wait_event(cdev->private->wait_q,
481 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200482 if (cdev->private->state != DEV_STATE_OFFLINE)
483 /* recognition failed */
484 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200485 }
486 if (cdev->drv && cdev->drv->set_online)
487 ccw_device_set_online(cdev);
488 return 0;
489}
Sebastian Ott156013f2009-03-31 19:16:03 +0200490
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200491static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200492{
493 int ret;
494
495 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200496 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200497 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200498 if (force && cdev->private->state == DEV_STATE_BOXED) {
499 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200500 if (ret)
501 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200502 if (cdev->id.cu_type == 0)
503 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200504 ret = online_store_recog_and_online(cdev);
505 if (ret)
506 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200507 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200508 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200509}
510
511static ssize_t online_store (struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200515 int force, ret;
516 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200518 if ((cdev->private->state != DEV_STATE_OFFLINE &&
519 cdev->private->state != DEV_STATE_ONLINE &&
520 cdev->private->state != DEV_STATE_BOXED &&
521 cdev->private->state != DEV_STATE_DISCONNECTED) ||
522 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EAGAIN;
524
525 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
526 atomic_set(&cdev->private->onoff, 0);
527 return -EINVAL;
528 }
529 if (!strncmp(buf, "force\n", count)) {
530 force = 1;
531 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200532 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
534 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200535 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200537 if (ret)
538 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200539 switch (i) {
540 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100541 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200542 break;
543 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200544 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200545 break;
546 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200547 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200549out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (cdev->drv)
551 module_put(cdev->drv->owner);
552 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100553 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400557available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct ccw_device *cdev = to_ccwdev(dev);
560 struct subchannel *sch;
561
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100562 if (ccw_device_is_orphan(cdev))
563 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 switch (cdev->private->state) {
565 case DEV_STATE_BOXED:
566 return sprintf(buf, "boxed\n");
567 case DEV_STATE_DISCONNECTED:
568 case DEV_STATE_DISCONNECTED_SENSE_ID:
569 case DEV_STATE_NOT_OPER:
570 sch = to_subchannel(dev->parent);
571 if (!sch->lpm)
572 return sprintf(buf, "no path\n");
573 else
574 return sprintf(buf, "no device\n");
575 default:
576 /* All other states considered fine. */
577 return sprintf(buf, "good\n");
578 }
579}
580
581static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
582static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
583static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
584static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800585static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static DEVICE_ATTR(availability, 0444, available_show, NULL);
588
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200589static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 &dev_attr_chpids.attr,
591 &dev_attr_pimpampom.attr,
592 NULL,
593};
594
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200595static struct attribute_group io_subchannel_attr_group = {
596 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100597};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599static struct attribute * ccwdev_attrs[] = {
600 &dev_attr_devtype.attr,
601 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800602 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 &dev_attr_online.attr,
604 &dev_attr_cmb_enable.attr,
605 &dev_attr_availability.attr,
606 NULL,
607};
608
609static struct attribute_group ccwdev_attr_group = {
610 .attrs = ccwdev_attrs,
611};
612
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200613static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200614 &ccwdev_attr_group,
615 NULL,
616};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/* this is a simple abstraction for device_register that sets the
619 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200620static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct device *dev = &cdev->dev;
623 int ret;
624
625 dev->bus = &ccw_bus_type;
626
627 if ((ret = device_add(dev)))
628 return ret;
629
630 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return ret;
632}
633
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700634struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200635 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700636 struct ccw_device * sibling;
637};
638
639static int
640match_devno(struct device * dev, void * data)
641{
Cornelia Huck78964262006-10-11 15:31:38 +0200642 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700643 struct ccw_device * cdev;
644
645 cdev = to_ccwdev(dev);
646 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100647 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200648 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100649 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700650 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700651 return 0;
652}
653
Cornelia Huck78964262006-10-11 15:31:38 +0200654static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
655 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200658 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Cornelia Huck78964262006-10-11 15:31:38 +0200660 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200661 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700662 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700664 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100667static int match_orphan(struct device *dev, void *data)
668{
669 struct ccw_dev_id *dev_id;
670 struct ccw_device *cdev;
671
672 dev_id = data;
673 cdev = to_ccwdev(dev);
674 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
675}
676
677static struct ccw_device *
678get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
679 struct ccw_dev_id *dev_id)
680{
681 struct device *dev;
682
683 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
684 match_orphan);
685
686 return dev ? to_ccwdev(dev) : NULL;
687}
688
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100689void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100691 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct ccw_device *cdev;
693 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100694 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100696 priv = container_of(work, struct ccw_device_private, kick_work);
697 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100700 if (test_bit(1, &cdev->private->registered)) {
701 device_release_driver(&cdev->dev);
702 ret = device_attach(&cdev->dev);
703 WARN_ON(ret == -ENODEV);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static void
708ccw_device_release(struct device *dev)
709{
710 struct ccw_device *cdev;
711
712 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100713 /* Release reference of parent subchannel. */
714 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 kfree(cdev->private);
716 kfree(cdev);
717}
718
Cornelia Huck7674da72006-12-08 15:54:21 +0100719static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
720{
721 struct ccw_device *cdev;
722
723 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
724 if (cdev) {
725 cdev->private = kzalloc(sizeof(struct ccw_device_private),
726 GFP_KERNEL | GFP_DMA);
727 if (cdev->private)
728 return cdev;
729 }
730 kfree(cdev);
731 return ERR_PTR(-ENOMEM);
732}
733
734static int io_subchannel_initialize_dev(struct subchannel *sch,
735 struct ccw_device *cdev)
736{
737 cdev->private->cdev = cdev;
738 atomic_set(&cdev->private->onoff, 0);
739 cdev->dev.parent = &sch->dev;
740 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100741 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200742 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100743 /* Do first half of device_register. */
744 device_initialize(&cdev->dev);
745 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100746 /* Release reference from device_initialize(). */
747 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100748 return -ENODEV;
749 }
750 return 0;
751}
752
753static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
754{
755 struct ccw_device *cdev;
756 int ret;
757
758 cdev = io_subchannel_allocate_dev(sch);
759 if (!IS_ERR(cdev)) {
760 ret = io_subchannel_initialize_dev(sch, cdev);
761 if (ret) {
762 kfree(cdev);
763 cdev = ERR_PTR(ret);
764 }
765 }
766 return cdev;
767}
768
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100769static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
770
771static void sch_attach_device(struct subchannel *sch,
772 struct ccw_device *cdev)
773{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200774 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100775 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100776 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100777 cdev->private->schid = sch->schid;
778 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200779 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100780 spin_unlock_irq(sch->lock);
781}
782
783static void sch_attach_disconnected_device(struct subchannel *sch,
784 struct ccw_device *cdev)
785{
786 struct subchannel *other_sch;
787 int ret;
788
Cornelia Huck6eff2082008-12-25 13:39:07 +0100789 /* Get reference for new parent. */
790 if (!get_device(&sch->dev))
791 return;
792 other_sch = to_subchannel(cdev->dev.parent);
793 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100794 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100795 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200796 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100797 "(ret=%d)!\n", cdev->private->dev_id.ssid,
798 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100799 /* Put reference for new parent. */
800 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100801 return;
802 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100803 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100804 /* No need to keep a subchannel without ccw device around. */
805 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100806 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100807 /* Put reference for old parent. */
808 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100809}
810
811static void sch_attach_orphaned_device(struct subchannel *sch,
812 struct ccw_device *cdev)
813{
814 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100815 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100816
Cornelia Huck6eff2082008-12-25 13:39:07 +0100817 /* Get reference for new parent. */
818 if (!get_device(&sch->dev))
819 return;
820 pseudo_sch = to_subchannel(cdev->dev.parent);
821 /*
822 * Try to move the ccw device to its new subchannel.
823 * Note: device_move() changes cdev->dev.parent
824 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100825 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100826 if (ret) {
827 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
828 "failed (ret=%d)!\n",
829 cdev->private->dev_id.ssid,
830 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100831 /* Put reference for new parent. */
832 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100833 return;
834 }
835 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100836 /* Put reference on pseudo subchannel. */
837 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100838}
839
840static void sch_create_and_recog_new_device(struct subchannel *sch)
841{
842 struct ccw_device *cdev;
843
844 /* Need to allocate a new ccw device. */
845 cdev = io_subchannel_create_ccwdev(sch);
846 if (IS_ERR(cdev)) {
847 /* OK, we did everything we could... */
848 css_sch_device_unregister(sch);
849 return;
850 }
851 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100852 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100853 spin_unlock_irq(sch->lock);
854 /* Start recognition for the new ccw device. */
855 if (io_subchannel_recog(cdev, sch)) {
856 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100857 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100858 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100859 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100860 /* Put reference from io_subchannel_create_ccwdev(). */
861 put_device(&sch->dev);
862 /* Give up initial reference. */
863 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100864 }
865}
866
867
868void ccw_device_move_to_orphanage(struct work_struct *work)
869{
870 struct ccw_device_private *priv;
871 struct ccw_device *cdev;
872 struct ccw_device *replacing_cdev;
873 struct subchannel *sch;
874 int ret;
875 struct channel_subsystem *css;
876 struct ccw_dev_id dev_id;
877
878 priv = container_of(work, struct ccw_device_private, kick_work);
879 cdev = priv->cdev;
880 sch = to_subchannel(cdev->dev.parent);
881 css = to_css(sch->dev.parent);
882 dev_id.devno = sch->schib.pmcw.dev;
883 dev_id.ssid = sch->schid.ssid;
884
Cornelia Huck6eff2082008-12-25 13:39:07 +0100885 /* Increase refcount for pseudo subchannel. */
886 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100887 /*
888 * Move the orphaned ccw device to the orphanage so the replacing
889 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100890 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100891 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100892 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
893 DPM_ORDER_NONE);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100894 if (ret) {
895 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
896 "(ret=%d)!\n", cdev->private->dev_id.ssid,
897 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100898 /* Decrease refcount for pseudo subchannel again. */
899 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100900 return;
901 }
902 cdev->ccwlock = css->pseudo_subchannel->lock;
903 /*
904 * Search for the replacing ccw device
905 * - among the disconnected devices
906 * - in the orphanage
907 */
908 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
909 if (replacing_cdev) {
910 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100911 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100912 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100913 /* Release reference of subchannel from old cdev. */
914 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100915 return;
916 }
917 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
918 if (replacing_cdev) {
919 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100920 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100921 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100922 /* Release reference of subchannel from old cdev. */
923 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100924 return;
925 }
926 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100927 /* Release reference of subchannel from old cdev. */
928 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * Register recognized device.
933 */
934static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100935io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100937 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct ccw_device *cdev;
939 struct subchannel *sch;
940 int ret;
941 unsigned long flags;
942
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100943 priv = container_of(work, struct ccw_device_private, kick_work);
944 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100946 /*
947 * Check if subchannel is still registered. It may have become
948 * unregistered if a machine check hit us after finishing
949 * device recognition but before the register work could be
950 * queued.
951 */
952 if (!device_is_registered(&sch->dev))
953 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200954 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100955 /*
956 * io_subchannel_register() will also be called after device
957 * recognition has been done for a boxed device (which will already
958 * be registered). We need to reprobe since we may now have sense id
959 * information.
960 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100961 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100962 if (!cdev->drv) {
963 ret = device_reprobe(&cdev->dev);
964 if (ret)
965 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200966 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200967 " %d for 0.%x.%04x\n", ret,
968 cdev->private->dev_id.ssid,
969 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto out;
972 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700973 /*
974 * Now we know this subchannel will stay, we can throw
975 * our delayed uevent.
976 */
Ming Leif67f1292009-03-01 21:10:49 +0800977 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700978 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* make it known to the system */
980 ret = ccw_device_register(cdev);
981 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200982 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
983 cdev->private->dev_id.ssid,
984 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100985 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100986 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100987 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100988 /* Release initial device reference. */
989 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100990 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992out:
993 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100995out_err:
996 /* Release reference for workqueue processing. */
997 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (atomic_dec_and_test(&ccw_device_init_count))
999 wake_up(&ccw_device_init_wq);
1000}
1001
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001002static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001004 struct ccw_device_private *priv;
1005 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct subchannel *sch;
1007
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001008 priv = container_of(work, struct ccw_device_private, kick_work);
1009 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001010 /* Get subchannel reference for local processing. */
1011 if (!get_device(cdev->dev.parent))
1012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001014 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001016 sch->config.intparm = 0;
1017 cio_commit_config(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001018 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001020 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 put_device(&sch->dev);
1022}
1023
Sebastian Ottc4621a62009-03-31 19:16:04 +02001024void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1025{
1026 PREPARE_WORK(&cdev->private->kick_work,
1027 ccw_device_call_sch_unregister);
1028 queue_work(slow_path_wq, &cdev->private->kick_work);
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
1032 * subchannel recognition done. Called from the state machine.
1033 */
1034void
1035io_subchannel_recog_done(struct ccw_device *cdev)
1036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (css_init_done == 0) {
1038 cdev->private->flags.recog_done = 1;
1039 return;
1040 }
1041 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +02001042 case DEV_STATE_BOXED:
1043 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 case DEV_STATE_NOT_OPER:
1045 cdev->private->flags.recog_done = 1;
1046 /* Remove device found not operational. */
1047 if (!get_device(&cdev->dev))
1048 break;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001049 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (atomic_dec_and_test(&ccw_device_init_count))
1051 wake_up(&ccw_device_init_wq);
1052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 case DEV_STATE_OFFLINE:
1054 /*
1055 * We can't register the device in interrupt context so
1056 * we schedule a work item.
1057 */
1058 if (!get_device(&cdev->dev))
1059 break;
1060 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001061 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 queue_work(slow_path_wq, &cdev->private->kick_work);
1063 break;
1064 }
1065}
1066
1067static int
1068io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1069{
1070 int rc;
1071 struct ccw_device_private *priv;
1072
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001073 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001074 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* Init private data. */
1077 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001078 priv->dev_id.devno = sch->schib.pmcw.dev;
1079 priv->dev_id.ssid = sch->schid.ssid;
1080 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 priv->state = DEV_STATE_NOT_OPER;
1082 INIT_LIST_HEAD(&priv->cmb_list);
1083 init_waitqueue_head(&priv->wait_q);
1084 init_timer(&priv->timer);
1085
1086 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001087 if (cio_is_console(sch->schid))
1088 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1089 else
1090 dev_set_name(&cdev->dev, "0.%x.%04x",
1091 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /* Increase counter of devices currently in recognition. */
1094 atomic_inc(&ccw_device_init_count);
1095
1096 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001097 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001099 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (rc) {
1101 if (atomic_dec_and_test(&ccw_device_init_count))
1102 wake_up(&ccw_device_init_wq);
1103 }
1104 return rc;
1105}
1106
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001107static void ccw_device_move_to_sch(struct work_struct *work)
1108{
1109 struct ccw_device_private *priv;
1110 int rc;
1111 struct subchannel *sch;
1112 struct ccw_device *cdev;
1113 struct subchannel *former_parent;
1114
1115 priv = container_of(work, struct ccw_device_private, kick_work);
1116 sch = priv->sch;
1117 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001118 former_parent = to_subchannel(cdev->dev.parent);
1119 /* Get reference for new parent. */
1120 if (!get_device(&sch->dev))
1121 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001122 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001123 /*
1124 * Try to move the ccw device to its new subchannel.
1125 * Note: device_move() changes cdev->dev.parent
1126 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001127 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001128 mutex_unlock(&sch->reg_mutex);
1129 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001130 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001131 "0.%x.%04x failed (ret=%d)!\n",
1132 cdev->private->dev_id.ssid,
1133 cdev->private->dev_id.devno, sch->schid.ssid,
1134 sch->schid.sch_no, rc);
1135 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001136 /* Put reference for new parent again. */
1137 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001138 goto out;
1139 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001140 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001141 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001142 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001143 spin_unlock_irq(former_parent->lock);
1144 css_sch_device_unregister(former_parent);
1145 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001146 former_parent->config.intparm = 0;
1147 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001148 }
1149 sch_attach_device(sch, cdev);
1150out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001151 /* Put reference for old parent. */
1152 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001153 put_device(&cdev->dev);
1154}
1155
Cornelia Huck602b20f2008-01-26 14:10:39 +01001156static void io_subchannel_irq(struct subchannel *sch)
1157{
1158 struct ccw_device *cdev;
1159
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001160 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001161
1162 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001163 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001164 if (cdev)
1165 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1166}
1167
Sebastian Ott13952ec2008-12-25 13:39:13 +01001168void io_subchannel_init_config(struct subchannel *sch)
1169{
1170 memset(&sch->config, 0, sizeof(sch->config));
1171 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001172 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1173 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001174 sch->config.mp = 1;
1175}
1176
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001177static void io_subchannel_init_fields(struct subchannel *sch)
1178{
1179 if (cio_is_console(sch->schid))
1180 sch->opm = 0xff;
1181 else
1182 sch->opm = chp_get_sch_opm(sch);
1183 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001184 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001185
1186 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1187 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1188 sch->schib.pmcw.dev, sch->schid.ssid,
1189 sch->schid.sch_no, sch->schib.pmcw.pim,
1190 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001191
1192 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001193}
1194
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001195static void io_subchannel_do_unreg(struct work_struct *work)
1196{
1197 struct subchannel *sch;
1198
1199 sch = container_of(work, struct subchannel, work);
1200 css_sch_device_unregister(sch);
1201 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001202 sch->config.intparm = 0;
1203 cio_commit_config(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001204 put_device(&sch->dev);
1205}
1206
1207/* Schedule unregister if we have no cdev. */
1208static void io_subchannel_schedule_removal(struct subchannel *sch)
1209{
1210 get_device(&sch->dev);
1211 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1212 queue_work(slow_path_wq, &sch->work);
1213}
1214
1215/*
1216 * Note: We always return 0 so that we bind to the device even on error.
1217 * This is needed so that our remove function is called on unregister.
1218 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001219static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct ccw_device *cdev;
1222 int rc;
1223 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001224 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001226 cdev = sch_get_cdev(sch);
1227 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001228 rc = sysfs_create_group(&sch->dev.kobj,
1229 &io_subchannel_attr_group);
1230 if (rc)
1231 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1232 "attributes for subchannel "
1233 "0.%x.%04x (rc=%d)\n",
1234 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001237 * Throw the delayed uevent for the subchannel, register
1238 * the ccw_device and exit. This happens for all early
1239 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Ming Leif67f1292009-03-01 21:10:49 +08001241 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001242 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001243 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 device_initialize(&cdev->dev);
1245 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001248 * the reference count needs to be corrected since we
1249 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
1251 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1252 cdev->private->state != DEV_STATE_OFFLINE &&
1253 cdev->private->state != DEV_STATE_BOXED)
1254 get_device(&cdev->dev);
1255 return 0;
1256 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001257 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001258 rc = cio_commit_config(sch);
1259 if (rc)
1260 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001261 rc = sysfs_create_group(&sch->dev.kobj,
1262 &io_subchannel_attr_group);
1263 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001264 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001265 /* Allocate I/O subchannel private data. */
1266 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1267 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001268 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001269 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001270 /*
1271 * First check if a fitting device may be found amongst the
1272 * disconnected devices or in the orphanage.
1273 */
1274 dev_id.devno = sch->schib.pmcw.dev;
1275 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001276 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1277 if (!cdev)
1278 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1279 &dev_id);
1280 if (cdev) {
1281 /*
1282 * Schedule moving the device until when we have a registered
1283 * subchannel to move to and succeed the probe. We can
1284 * unregister later again, when the probe is through.
1285 */
1286 cdev->private->sch = sch;
1287 PREPARE_WORK(&cdev->private->kick_work,
1288 ccw_device_move_to_sch);
1289 queue_work(slow_path_wq, &cdev->private->kick_work);
1290 return 0;
1291 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001292 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001293 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001294 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001295 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001297 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001298 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001299 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001301 return 0;
1302out_err:
1303 kfree(sch->private);
1304 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001305out_schedule:
1306 io_subchannel_schedule_removal(sch);
1307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001311io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct ccw_device *cdev;
1314 unsigned long flags;
1315
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001316 cdev = sch_get_cdev(sch);
1317 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* Set ccw device to not operational and drop reference. */
1320 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001321 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 cdev->private->state = DEV_STATE_NOT_OPER;
1323 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001324 ccw_device_unregister(cdev);
1325 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001326 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001327 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 0;
1329}
1330
Cornelia Huck602b20f2008-01-26 14:10:39 +01001331static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 struct ccw_device *cdev;
1334
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001335 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (!cdev)
1337 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001338 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Cornelia Huck602b20f2008-01-26 14:10:39 +01001341static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct ccw_device *cdev;
1344
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001345 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (cdev)
1347 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1348}
1349
Cornelia Huckc820de32008-07-14 09:58:45 +02001350static int check_for_io_on_path(struct subchannel *sch, int mask)
1351{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001352 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001353 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001354 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001355 return 1;
1356 return 0;
1357}
1358
1359static void terminate_internal_io(struct subchannel *sch,
1360 struct ccw_device *cdev)
1361{
1362 if (cio_clear(sch)) {
1363 /* Recheck device in case clear failed. */
1364 sch->lpm = 0;
1365 if (cdev->online)
1366 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1367 else
1368 css_schedule_eval(sch->schid);
1369 return;
1370 }
1371 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1372 /* Request retry of internal operation. */
1373 cdev->private->flags.intretry = 1;
1374 /* Call handler. */
1375 if (cdev->handler)
1376 cdev->handler(cdev, cdev->private->intparm,
1377 ERR_PTR(-EIO));
1378}
1379
1380static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct ccw_device *cdev;
1383
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001384 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!cdev)
1386 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001387 if (check_for_io_on_path(sch, mask)) {
1388 if (cdev->private->state == DEV_STATE_ONLINE)
1389 ccw_device_kill_io(cdev);
1390 else {
1391 terminate_internal_io(sch, cdev);
1392 /* Re-start path verification. */
1393 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1394 }
1395 } else
1396 /* trigger path verification. */
1397 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1398
1399}
1400
Cornelia Huck99611f82008-07-14 09:59:02 +02001401static int io_subchannel_chp_event(struct subchannel *sch,
1402 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001403{
1404 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001405
Cornelia Huck99611f82008-07-14 09:59:02 +02001406 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001407 if (!mask)
1408 return 0;
1409 switch (event) {
1410 case CHP_VARY_OFF:
1411 sch->opm &= ~mask;
1412 sch->lpm &= ~mask;
1413 io_subchannel_terminate_path(sch, mask);
1414 break;
1415 case CHP_VARY_ON:
1416 sch->opm |= mask;
1417 sch->lpm |= mask;
1418 io_subchannel_verify(sch);
1419 break;
1420 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001421 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001422 return -ENODEV;
1423 io_subchannel_terminate_path(sch, mask);
1424 break;
1425 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001426 if (cio_update_schib(sch))
1427 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001428 sch->lpm |= mask & sch->opm;
1429 io_subchannel_verify(sch);
1430 break;
1431 }
1432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001436io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct ccw_device *cdev;
1439 int ret;
1440
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001441 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001443 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return;
1445 if (!sch->schib.pmcw.ena)
1446 /* Nothing to do. */
1447 return;
1448 ret = cio_disable_subchannel(sch);
1449 if (ret != -EBUSY)
1450 /* Subchannel is disabled, we're done. */
1451 return;
1452 cdev->private->state = DEV_STATE_QUIESCE;
1453 if (cdev->handler)
1454 cdev->handler(cdev, cdev->private->intparm,
1455 ERR_PTR(-EIO));
1456 ret = ccw_device_cancel_halt_clear(cdev);
1457 if (ret == -EBUSY) {
1458 ccw_device_set_timeout(cdev, HZ/10);
1459 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1460 }
1461 cio_disable_subchannel(sch);
1462}
1463
Cornelia Huckc820de32008-07-14 09:58:45 +02001464static int io_subchannel_get_status(struct subchannel *sch)
1465{
1466 struct schib schib;
1467
1468 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1469 return CIO_GONE;
1470 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1471 return CIO_REVALIDATE;
1472 if (!sch->lpm)
1473 return CIO_NO_PATH;
1474 return CIO_OPER;
1475}
1476
1477static int device_is_disconnected(struct ccw_device *cdev)
1478{
1479 if (!cdev)
1480 return 0;
1481 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1482 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1483}
1484
1485static int recovery_check(struct device *dev, void *data)
1486{
1487 struct ccw_device *cdev = to_ccwdev(dev);
1488 int *redo = data;
1489
1490 spin_lock_irq(cdev->ccwlock);
1491 switch (cdev->private->state) {
1492 case DEV_STATE_DISCONNECTED:
1493 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1494 cdev->private->dev_id.ssid,
1495 cdev->private->dev_id.devno);
1496 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1497 *redo = 1;
1498 break;
1499 case DEV_STATE_DISCONNECTED_SENSE_ID:
1500 *redo = 1;
1501 break;
1502 }
1503 spin_unlock_irq(cdev->ccwlock);
1504
1505 return 0;
1506}
1507
1508static void recovery_work_func(struct work_struct *unused)
1509{
1510 int redo = 0;
1511
1512 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1513 if (redo) {
1514 spin_lock_irq(&recovery_lock);
1515 if (!timer_pending(&recovery_timer)) {
1516 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1517 recovery_phase++;
1518 mod_timer(&recovery_timer, jiffies +
1519 recovery_delay[recovery_phase] * HZ);
1520 }
1521 spin_unlock_irq(&recovery_lock);
1522 } else
1523 CIO_MSG_EVENT(4, "recovery: end\n");
1524}
1525
1526static DECLARE_WORK(recovery_work, recovery_work_func);
1527
1528static void recovery_func(unsigned long data)
1529{
1530 /*
1531 * We can't do our recovery in softirq context and it's not
1532 * performance critical, so we schedule it.
1533 */
1534 schedule_work(&recovery_work);
1535}
1536
1537static void ccw_device_schedule_recovery(void)
1538{
1539 unsigned long flags;
1540
1541 CIO_MSG_EVENT(4, "recovery: schedule\n");
1542 spin_lock_irqsave(&recovery_lock, flags);
1543 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1544 recovery_phase = 0;
1545 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1546 }
1547 spin_unlock_irqrestore(&recovery_lock, flags);
1548}
1549
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001550static int purge_fn(struct device *dev, void *data)
1551{
1552 struct ccw_device *cdev = to_ccwdev(dev);
1553 struct ccw_device_private *priv = cdev->private;
1554 int unreg;
1555
1556 spin_lock_irq(cdev->ccwlock);
1557 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1558 (priv->state == DEV_STATE_OFFLINE);
1559 spin_unlock_irq(cdev->ccwlock);
1560 if (!unreg)
1561 goto out;
1562 if (!get_device(&cdev->dev))
1563 goto out;
1564 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1565 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001566 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001567
1568out:
1569 /* Abort loop in case of pending signal. */
1570 if (signal_pending(current))
1571 return -EINTR;
1572
1573 return 0;
1574}
1575
1576/**
1577 * ccw_purge_blacklisted - purge unused, blacklisted devices
1578 *
1579 * Unregister all ccw devices that are offline and on the blacklist.
1580 */
1581int ccw_purge_blacklisted(void)
1582{
1583 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1584 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1585 return 0;
1586}
1587
Cornelia Huckc820de32008-07-14 09:58:45 +02001588static void device_set_disconnected(struct ccw_device *cdev)
1589{
1590 if (!cdev)
1591 return;
1592 ccw_device_set_timeout(cdev, 0);
1593 cdev->private->flags.fake_irb = 0;
1594 cdev->private->state = DEV_STATE_DISCONNECTED;
1595 if (cdev->online)
1596 ccw_device_schedule_recovery();
1597}
1598
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001599void ccw_device_set_notoper(struct ccw_device *cdev)
1600{
1601 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1602
1603 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001604 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001605 ccw_device_set_timeout(cdev, 0);
1606 cio_disable_subchannel(sch);
1607 cdev->private->state = DEV_STATE_NOT_OPER;
1608}
1609
Cornelia Huckc820de32008-07-14 09:58:45 +02001610static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1611{
1612 int event, ret, disc;
1613 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001614 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001615 struct ccw_device *cdev;
1616
1617 spin_lock_irqsave(sch->lock, flags);
1618 cdev = sch_get_cdev(sch);
1619 disc = device_is_disconnected(cdev);
1620 if (disc && slow) {
1621 /* Disconnected devices are evaluated directly only.*/
1622 spin_unlock_irqrestore(sch->lock, flags);
1623 return 0;
1624 }
1625 /* No interrupt after machine check - kill pending timers. */
1626 if (cdev)
1627 ccw_device_set_timeout(cdev, 0);
1628 if (!disc && !slow) {
1629 /* Non-disconnected devices are evaluated on the slow path. */
1630 spin_unlock_irqrestore(sch->lock, flags);
1631 return -EAGAIN;
1632 }
1633 event = io_subchannel_get_status(sch);
1634 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1635 sch->schid.ssid, sch->schid.sch_no, event,
1636 disc ? "disconnected" : "normal",
1637 slow ? "slow" : "fast");
1638 /* Analyze subchannel status. */
1639 action = NONE;
1640 switch (event) {
1641 case CIO_NO_PATH:
1642 if (disc) {
1643 /* Check if paths have become available. */
1644 action = REPROBE;
1645 break;
1646 }
1647 /* fall through */
1648 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001649 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001650 if (io_subchannel_notify(sch, event))
1651 action = DISC;
1652 else
1653 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001654 break;
1655 case CIO_REVALIDATE:
1656 /* Device will be removed, so no notify necessary. */
1657 if (disc)
1658 /* Reprobe because immediate unregister might block. */
1659 action = REPROBE;
1660 else
1661 action = UNREGISTER_PROBE;
1662 break;
1663 case CIO_OPER:
1664 if (disc)
1665 /* Get device operational again. */
1666 action = REPROBE;
1667 break;
1668 }
1669 /* Perform action. */
1670 ret = 0;
1671 switch (action) {
1672 case UNREGISTER:
1673 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001674 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001675 /* Unregister device (will use subchannel lock). */
1676 spin_unlock_irqrestore(sch->lock, flags);
1677 css_sch_device_unregister(sch);
1678 spin_lock_irqsave(sch->lock, flags);
1679
1680 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001681 sch->config.intparm = 0;
1682 cio_commit_config(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001683 break;
1684 case REPROBE:
1685 ccw_device_trigger_reprobe(cdev);
1686 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001687 case DISC:
1688 device_set_disconnected(cdev);
1689 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001690 default:
1691 break;
1692 }
1693 spin_unlock_irqrestore(sch->lock, flags);
1694 /* Probe if necessary. */
1695 if (action == UNREGISTER_PROBE)
1696 ret = css_probe_device(sch->schid);
1697
1698 return ret;
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#ifdef CONFIG_CCW_CONSOLE
1702static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001703static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704static struct ccw_device_private console_private;
1705static int console_cdev_in_use;
1706
Cornelia Huck2ec22982006-12-08 15:54:26 +01001707static DEFINE_SPINLOCK(ccw_console_lock);
1708
1709spinlock_t * cio_get_console_lock(void)
1710{
1711 return &ccw_console_lock;
1712}
1713
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001714static int ccw_device_console_enable(struct ccw_device *cdev,
1715 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 int rc;
1718
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001719 /* Attach subchannel private data. */
1720 sch->private = cio_get_console_priv();
1721 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001722 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001723 rc = cio_commit_config(sch);
1724 if (rc)
1725 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001726 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001728 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rc = io_subchannel_recog(cdev, sch);
1730 if (rc)
1731 return rc;
1732
1733 /* Now wait for the async. recognition to come to an end. */
1734 spin_lock_irq(cdev->ccwlock);
1735 while (!dev_fsm_final_state(cdev))
1736 wait_cons_dev();
1737 rc = -EIO;
1738 if (cdev->private->state != DEV_STATE_OFFLINE)
1739 goto out_unlock;
1740 ccw_device_online(cdev);
1741 while (!dev_fsm_final_state(cdev))
1742 wait_cons_dev();
1743 if (cdev->private->state != DEV_STATE_ONLINE)
1744 goto out_unlock;
1745 rc = 0;
1746out_unlock:
1747 spin_unlock_irq(cdev->ccwlock);
1748 return 0;
1749}
1750
1751struct ccw_device *
1752ccw_device_probe_console(void)
1753{
1754 struct subchannel *sch;
1755 int ret;
1756
1757 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001758 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 sch = cio_probe_console();
1760 if (IS_ERR(sch)) {
1761 console_cdev_in_use = 0;
1762 return (void *) sch;
1763 }
1764 memset(&console_cdev, 0, sizeof(struct ccw_device));
1765 memset(&console_private, 0, sizeof(struct ccw_device_private));
1766 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001767 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 ret = ccw_device_console_enable(&console_cdev, sch);
1769 if (ret) {
1770 cio_release_console();
1771 console_cdev_in_use = 0;
1772 return ERR_PTR(ret);
1773 }
1774 console_cdev.online = 1;
1775 return &console_cdev;
1776}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001777
1778
1779const char *cio_get_console_cdev_name(struct subchannel *sch)
1780{
1781 snprintf(console_cdev_name, 10, "0.%x.%04x",
1782 sch->schid.ssid, sch->schib.pmcw.dev);
1783 return (const char *)console_cdev_name;
1784}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785#endif
1786
1787/*
1788 * get ccw_device matching the busid, but only if owned by cdrv
1789 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001790static int
1791__ccwdev_check_busid(struct device *dev, void *id)
1792{
1793 char *bus_id;
1794
Cornelia Huck12975ae2006-10-11 15:31:47 +02001795 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001796
Kay Sievers98df67b2008-12-25 13:38:55 +01001797 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001798}
1799
1800
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001801/**
1802 * get_ccwdev_by_busid() - obtain device from a bus id
1803 * @cdrv: driver the device is owned by
1804 * @bus_id: bus id of the device to be searched
1805 *
1806 * This function searches all devices owned by @cdrv for a device with a bus
1807 * id matching @bus_id.
1808 * Returns:
1809 * If a match is found, its reference count of the found device is increased
1810 * and it is returned; else %NULL is returned.
1811 */
1812struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1813 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001815 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct device_driver *drv;
1817
1818 drv = get_driver(&cdrv->driver);
1819 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001820 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001822 dev = driver_find_device(drv, NULL, (void *)bus_id,
1823 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 put_driver(drv);
1825
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001826 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
1829/************************** device driver handling ************************/
1830
1831/* This is the implementation of the ccw_driver class. The probe, remove
1832 * and release methods are initially very similar to the device_driver
1833 * implementations, with the difference that they have ccw_device
1834 * arguments.
1835 *
1836 * A ccw driver also contains the information that is needed for
1837 * device matching.
1838 */
1839static int
1840ccw_device_probe (struct device *dev)
1841{
1842 struct ccw_device *cdev = to_ccwdev(dev);
1843 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1844 int ret;
1845
1846 cdev->drv = cdrv; /* to let the driver call _set_online */
1847
1848 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1849
1850 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001851 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return ret;
1853 }
1854
1855 return 0;
1856}
1857
1858static int
1859ccw_device_remove (struct device *dev)
1860{
1861 struct ccw_device *cdev = to_ccwdev(dev);
1862 struct ccw_driver *cdrv = cdev->drv;
1863 int ret;
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (cdrv->remove)
1866 cdrv->remove(cdev);
1867 if (cdev->online) {
1868 cdev->online = 0;
1869 spin_lock_irq(cdev->ccwlock);
1870 ret = ccw_device_offline(cdev);
1871 spin_unlock_irq(cdev->ccwlock);
1872 if (ret == 0)
1873 wait_event(cdev->private->wait_q,
1874 dev_fsm_final_state(cdev));
1875 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001876 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001877 "device 0.%x.%04x\n",
1878 ret, cdev->private->dev_id.ssid,
1879 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001880 /* Give up reference obtained in ccw_device_set_online(). */
1881 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001884 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
1886}
1887
Cornelia Huck958974fb2007-10-12 16:11:21 +02001888static void ccw_device_shutdown(struct device *dev)
1889{
1890 struct ccw_device *cdev;
1891
1892 cdev = to_ccwdev(dev);
1893 if (cdev->drv && cdev->drv->shutdown)
1894 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001895 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001896}
1897
Cornelia Huck8bbace72006-01-11 10:56:22 +01001898struct bus_type ccw_bus_type = {
1899 .name = "ccw",
1900 .match = ccw_bus_match,
1901 .uevent = ccw_uevent,
1902 .probe = ccw_device_probe,
1903 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001904 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001905};
1906
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001907/**
1908 * ccw_driver_register() - register a ccw driver
1909 * @cdriver: driver to be registered
1910 *
1911 * This function is mainly a wrapper around driver_register().
1912 * Returns:
1913 * %0 on success and a negative error value on failure.
1914 */
1915int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 struct device_driver *drv = &cdriver->driver;
1918
1919 drv->bus = &ccw_bus_type;
1920 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001921 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 return driver_register(drv);
1924}
1925
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001926/**
1927 * ccw_driver_unregister() - deregister a ccw driver
1928 * @cdriver: driver to be deregistered
1929 *
1930 * This function is mainly a wrapper around driver_unregister().
1931 */
1932void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 driver_unregister(&cdriver->driver);
1935}
1936
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001937/* Helper func for qdio. */
1938struct subchannel_id
1939ccw_device_get_subchannel_id(struct ccw_device *cdev)
1940{
1941 struct subchannel *sch;
1942
1943 sch = to_subchannel(cdev->dev.parent);
1944 return sch->schid;
1945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947MODULE_LICENSE("GPL");
1948EXPORT_SYMBOL(ccw_device_set_online);
1949EXPORT_SYMBOL(ccw_device_set_offline);
1950EXPORT_SYMBOL(ccw_driver_register);
1951EXPORT_SYMBOL(ccw_driver_unregister);
1952EXPORT_SYMBOL(get_ccwdev_by_busid);
1953EXPORT_SYMBOL(ccw_bus_type);
1954EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001955EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);