blob: 868f8c6b053a23938b7734f31bcf257b003bf07d [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
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800518 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EAGAIN;
520
521 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
522 atomic_set(&cdev->private->onoff, 0);
523 return -EINVAL;
524 }
525 if (!strncmp(buf, "force\n", count)) {
526 force = 1;
527 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200528 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else {
530 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200531 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200533 if (ret)
534 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200535 switch (i) {
536 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100537 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538 break;
539 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200540 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200541 break;
542 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200543 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200545out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (cdev->drv)
547 module_put(cdev->drv->owner);
548 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100549 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400553available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct ccw_device *cdev = to_ccwdev(dev);
556 struct subchannel *sch;
557
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100558 if (ccw_device_is_orphan(cdev))
559 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 switch (cdev->private->state) {
561 case DEV_STATE_BOXED:
562 return sprintf(buf, "boxed\n");
563 case DEV_STATE_DISCONNECTED:
564 case DEV_STATE_DISCONNECTED_SENSE_ID:
565 case DEV_STATE_NOT_OPER:
566 sch = to_subchannel(dev->parent);
567 if (!sch->lpm)
568 return sprintf(buf, "no path\n");
569 else
570 return sprintf(buf, "no device\n");
571 default:
572 /* All other states considered fine. */
573 return sprintf(buf, "good\n");
574 }
575}
576
577static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
578static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
579static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
580static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800581static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static DEVICE_ATTR(availability, 0444, available_show, NULL);
584
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200585static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 &dev_attr_chpids.attr,
587 &dev_attr_pimpampom.attr,
588 NULL,
589};
590
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200591static struct attribute_group io_subchannel_attr_group = {
592 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100593};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595static struct attribute * ccwdev_attrs[] = {
596 &dev_attr_devtype.attr,
597 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800598 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 &dev_attr_online.attr,
600 &dev_attr_cmb_enable.attr,
601 &dev_attr_availability.attr,
602 NULL,
603};
604
605static struct attribute_group ccwdev_attr_group = {
606 .attrs = ccwdev_attrs,
607};
608
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200609static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200610 &ccwdev_attr_group,
611 NULL,
612};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/* this is a simple abstraction for device_register that sets the
615 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200616static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct device *dev = &cdev->dev;
619 int ret;
620
621 dev->bus = &ccw_bus_type;
622
623 if ((ret = device_add(dev)))
624 return ret;
625
626 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return ret;
628}
629
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700630struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200631 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700632 struct ccw_device * sibling;
633};
634
635static int
636match_devno(struct device * dev, void * data)
637{
Cornelia Huck78964262006-10-11 15:31:38 +0200638 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700639 struct ccw_device * cdev;
640
641 cdev = to_ccwdev(dev);
642 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100643 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200644 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100645 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700646 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700647 return 0;
648}
649
Cornelia Huck78964262006-10-11 15:31:38 +0200650static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
651 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200654 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Cornelia Huck78964262006-10-11 15:31:38 +0200656 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200657 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700658 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700660 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100663static int match_orphan(struct device *dev, void *data)
664{
665 struct ccw_dev_id *dev_id;
666 struct ccw_device *cdev;
667
668 dev_id = data;
669 cdev = to_ccwdev(dev);
670 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
671}
672
673static struct ccw_device *
674get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
675 struct ccw_dev_id *dev_id)
676{
677 struct device *dev;
678
679 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
680 match_orphan);
681
682 return dev ? to_ccwdev(dev) : NULL;
683}
684
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100685void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100687 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ccw_device *cdev;
689 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100690 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100692 priv = container_of(work, struct ccw_device_private, kick_work);
693 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100696 if (test_bit(1, &cdev->private->registered)) {
697 device_release_driver(&cdev->dev);
698 ret = device_attach(&cdev->dev);
699 WARN_ON(ret == -ENODEV);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static void
704ccw_device_release(struct device *dev)
705{
706 struct ccw_device *cdev;
707
708 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100709 /* Release reference of parent subchannel. */
710 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 kfree(cdev->private);
712 kfree(cdev);
713}
714
Cornelia Huck7674da72006-12-08 15:54:21 +0100715static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
716{
717 struct ccw_device *cdev;
718
719 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
720 if (cdev) {
721 cdev->private = kzalloc(sizeof(struct ccw_device_private),
722 GFP_KERNEL | GFP_DMA);
723 if (cdev->private)
724 return cdev;
725 }
726 kfree(cdev);
727 return ERR_PTR(-ENOMEM);
728}
729
730static int io_subchannel_initialize_dev(struct subchannel *sch,
731 struct ccw_device *cdev)
732{
733 cdev->private->cdev = cdev;
734 atomic_set(&cdev->private->onoff, 0);
735 cdev->dev.parent = &sch->dev;
736 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100737 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200738 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100739 /* Do first half of device_register. */
740 device_initialize(&cdev->dev);
741 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100742 /* Release reference from device_initialize(). */
743 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100744 return -ENODEV;
745 }
746 return 0;
747}
748
749static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
750{
751 struct ccw_device *cdev;
752 int ret;
753
754 cdev = io_subchannel_allocate_dev(sch);
755 if (!IS_ERR(cdev)) {
756 ret = io_subchannel_initialize_dev(sch, cdev);
757 if (ret) {
758 kfree(cdev);
759 cdev = ERR_PTR(ret);
760 }
761 }
762 return cdev;
763}
764
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100765static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
766
767static void sch_attach_device(struct subchannel *sch,
768 struct ccw_device *cdev)
769{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200770 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100771 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100772 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100773 cdev->private->schid = sch->schid;
774 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200775 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100776 spin_unlock_irq(sch->lock);
777}
778
779static void sch_attach_disconnected_device(struct subchannel *sch,
780 struct ccw_device *cdev)
781{
782 struct subchannel *other_sch;
783 int ret;
784
Cornelia Huck6eff2082008-12-25 13:39:07 +0100785 /* Get reference for new parent. */
786 if (!get_device(&sch->dev))
787 return;
788 other_sch = to_subchannel(cdev->dev.parent);
789 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100790 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100791 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200792 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100793 "(ret=%d)!\n", cdev->private->dev_id.ssid,
794 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100795 /* Put reference for new parent. */
796 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100797 return;
798 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100799 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100800 /* No need to keep a subchannel without ccw device around. */
801 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100802 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100803 /* Put reference for old parent. */
804 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100805}
806
807static void sch_attach_orphaned_device(struct subchannel *sch,
808 struct ccw_device *cdev)
809{
810 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100811 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100812
Cornelia Huck6eff2082008-12-25 13:39:07 +0100813 /* Get reference for new parent. */
814 if (!get_device(&sch->dev))
815 return;
816 pseudo_sch = to_subchannel(cdev->dev.parent);
817 /*
818 * Try to move the ccw device to its new subchannel.
819 * Note: device_move() changes cdev->dev.parent
820 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100821 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100822 if (ret) {
823 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
824 "failed (ret=%d)!\n",
825 cdev->private->dev_id.ssid,
826 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100827 /* Put reference for new parent. */
828 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100829 return;
830 }
831 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100832 /* Put reference on pseudo subchannel. */
833 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100834}
835
836static void sch_create_and_recog_new_device(struct subchannel *sch)
837{
838 struct ccw_device *cdev;
839
840 /* Need to allocate a new ccw device. */
841 cdev = io_subchannel_create_ccwdev(sch);
842 if (IS_ERR(cdev)) {
843 /* OK, we did everything we could... */
844 css_sch_device_unregister(sch);
845 return;
846 }
847 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100848 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100849 spin_unlock_irq(sch->lock);
850 /* Start recognition for the new ccw device. */
851 if (io_subchannel_recog(cdev, sch)) {
852 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100853 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100854 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100855 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100856 /* Put reference from io_subchannel_create_ccwdev(). */
857 put_device(&sch->dev);
858 /* Give up initial reference. */
859 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100860 }
861}
862
863
864void ccw_device_move_to_orphanage(struct work_struct *work)
865{
866 struct ccw_device_private *priv;
867 struct ccw_device *cdev;
868 struct ccw_device *replacing_cdev;
869 struct subchannel *sch;
870 int ret;
871 struct channel_subsystem *css;
872 struct ccw_dev_id dev_id;
873
874 priv = container_of(work, struct ccw_device_private, kick_work);
875 cdev = priv->cdev;
876 sch = to_subchannel(cdev->dev.parent);
877 css = to_css(sch->dev.parent);
878 dev_id.devno = sch->schib.pmcw.dev;
879 dev_id.ssid = sch->schid.ssid;
880
Cornelia Huck6eff2082008-12-25 13:39:07 +0100881 /* Increase refcount for pseudo subchannel. */
882 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100883 /*
884 * Move the orphaned ccw device to the orphanage so the replacing
885 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100886 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100887 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100888 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
889 DPM_ORDER_NONE);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100890 if (ret) {
891 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
892 "(ret=%d)!\n", cdev->private->dev_id.ssid,
893 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100894 /* Decrease refcount for pseudo subchannel again. */
895 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100896 return;
897 }
898 cdev->ccwlock = css->pseudo_subchannel->lock;
899 /*
900 * Search for the replacing ccw device
901 * - among the disconnected devices
902 * - in the orphanage
903 */
904 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
905 if (replacing_cdev) {
906 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100907 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100908 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100909 /* Release reference of subchannel from old cdev. */
910 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100911 return;
912 }
913 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
914 if (replacing_cdev) {
915 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100916 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100917 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100918 /* Release reference of subchannel from old cdev. */
919 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100920 return;
921 }
922 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100923 /* Release reference of subchannel from old cdev. */
924 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * Register recognized device.
929 */
930static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100931io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100933 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct ccw_device *cdev;
935 struct subchannel *sch;
936 int ret;
937 unsigned long flags;
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 Huck5fb6b852008-12-25 13:39:08 +0100942 /*
943 * Check if subchannel is still registered. It may have become
944 * unregistered if a machine check hit us after finishing
945 * device recognition but before the register work could be
946 * queued.
947 */
948 if (!device_is_registered(&sch->dev))
949 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200950 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100951 /*
952 * io_subchannel_register() will also be called after device
953 * recognition has been done for a boxed device (which will already
954 * be registered). We need to reprobe since we may now have sense id
955 * information.
956 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100957 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100958 if (!cdev->drv) {
959 ret = device_reprobe(&cdev->dev);
960 if (ret)
961 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200962 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200963 " %d for 0.%x.%04x\n", ret,
964 cdev->private->dev_id.ssid,
965 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 goto out;
968 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700969 /*
970 * Now we know this subchannel will stay, we can throw
971 * our delayed uevent.
972 */
Ming Leif67f1292009-03-01 21:10:49 +0800973 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700974 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* make it known to the system */
976 ret = ccw_device_register(cdev);
977 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200978 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
979 cdev->private->dev_id.ssid,
980 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100981 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100982 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100983 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100984 /* Release initial device reference. */
985 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100986 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988out:
989 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100991out_err:
992 /* Release reference for workqueue processing. */
993 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (atomic_dec_and_test(&ccw_device_init_count))
995 wake_up(&ccw_device_init_wq);
996}
997
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200998static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001000 struct ccw_device_private *priv;
1001 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct subchannel *sch;
1003
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001004 priv = container_of(work, struct ccw_device_private, kick_work);
1005 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001006 /* Get subchannel reference for local processing. */
1007 if (!get_device(cdev->dev.parent))
1008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001010 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001012 sch->config.intparm = 0;
1013 cio_commit_config(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001014 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001016 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 put_device(&sch->dev);
1018}
1019
Sebastian Ottc4621a62009-03-31 19:16:04 +02001020void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1021{
1022 PREPARE_WORK(&cdev->private->kick_work,
1023 ccw_device_call_sch_unregister);
1024 queue_work(slow_path_wq, &cdev->private->kick_work);
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
1028 * subchannel recognition done. Called from the state machine.
1029 */
1030void
1031io_subchannel_recog_done(struct ccw_device *cdev)
1032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (css_init_done == 0) {
1034 cdev->private->flags.recog_done = 1;
1035 return;
1036 }
1037 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +02001038 case DEV_STATE_BOXED:
1039 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 case DEV_STATE_NOT_OPER:
1041 cdev->private->flags.recog_done = 1;
1042 /* Remove device found not operational. */
1043 if (!get_device(&cdev->dev))
1044 break;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001045 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (atomic_dec_and_test(&ccw_device_init_count))
1047 wake_up(&ccw_device_init_wq);
1048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 case DEV_STATE_OFFLINE:
1050 /*
1051 * We can't register the device in interrupt context so
1052 * we schedule a work item.
1053 */
1054 if (!get_device(&cdev->dev))
1055 break;
1056 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001057 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 queue_work(slow_path_wq, &cdev->private->kick_work);
1059 break;
1060 }
1061}
1062
1063static int
1064io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1065{
1066 int rc;
1067 struct ccw_device_private *priv;
1068
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001069 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001070 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Init private data. */
1073 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001074 priv->dev_id.devno = sch->schib.pmcw.dev;
1075 priv->dev_id.ssid = sch->schid.ssid;
1076 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 priv->state = DEV_STATE_NOT_OPER;
1078 INIT_LIST_HEAD(&priv->cmb_list);
1079 init_waitqueue_head(&priv->wait_q);
1080 init_timer(&priv->timer);
1081
1082 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001083 if (cio_is_console(sch->schid))
1084 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1085 else
1086 dev_set_name(&cdev->dev, "0.%x.%04x",
1087 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* Increase counter of devices currently in recognition. */
1090 atomic_inc(&ccw_device_init_count);
1091
1092 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001093 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001095 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (rc) {
1097 if (atomic_dec_and_test(&ccw_device_init_count))
1098 wake_up(&ccw_device_init_wq);
1099 }
1100 return rc;
1101}
1102
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001103static void ccw_device_move_to_sch(struct work_struct *work)
1104{
1105 struct ccw_device_private *priv;
1106 int rc;
1107 struct subchannel *sch;
1108 struct ccw_device *cdev;
1109 struct subchannel *former_parent;
1110
1111 priv = container_of(work, struct ccw_device_private, kick_work);
1112 sch = priv->sch;
1113 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001114 former_parent = to_subchannel(cdev->dev.parent);
1115 /* Get reference for new parent. */
1116 if (!get_device(&sch->dev))
1117 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001118 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001119 /*
1120 * Try to move the ccw device to its new subchannel.
1121 * Note: device_move() changes cdev->dev.parent
1122 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001123 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001124 mutex_unlock(&sch->reg_mutex);
1125 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001126 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001127 "0.%x.%04x failed (ret=%d)!\n",
1128 cdev->private->dev_id.ssid,
1129 cdev->private->dev_id.devno, sch->schid.ssid,
1130 sch->schid.sch_no, rc);
1131 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001132 /* Put reference for new parent again. */
1133 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001134 goto out;
1135 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001136 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001137 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001138 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001139 spin_unlock_irq(former_parent->lock);
1140 css_sch_device_unregister(former_parent);
1141 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001142 former_parent->config.intparm = 0;
1143 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001144 }
1145 sch_attach_device(sch, cdev);
1146out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001147 /* Put reference for old parent. */
1148 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001149 put_device(&cdev->dev);
1150}
1151
Cornelia Huck602b20f2008-01-26 14:10:39 +01001152static void io_subchannel_irq(struct subchannel *sch)
1153{
1154 struct ccw_device *cdev;
1155
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001156 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001157
1158 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001159 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001160 if (cdev)
1161 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1162}
1163
Sebastian Ott13952ec2008-12-25 13:39:13 +01001164void io_subchannel_init_config(struct subchannel *sch)
1165{
1166 memset(&sch->config, 0, sizeof(sch->config));
1167 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001168 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1169 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001170 sch->config.mp = 1;
1171}
1172
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001173static void io_subchannel_init_fields(struct subchannel *sch)
1174{
1175 if (cio_is_console(sch->schid))
1176 sch->opm = 0xff;
1177 else
1178 sch->opm = chp_get_sch_opm(sch);
1179 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001180 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001181
1182 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1183 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1184 sch->schib.pmcw.dev, sch->schid.ssid,
1185 sch->schid.sch_no, sch->schib.pmcw.pim,
1186 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001187
1188 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001189}
1190
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001191static void io_subchannel_do_unreg(struct work_struct *work)
1192{
1193 struct subchannel *sch;
1194
1195 sch = container_of(work, struct subchannel, work);
1196 css_sch_device_unregister(sch);
1197 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001198 sch->config.intparm = 0;
1199 cio_commit_config(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001200 put_device(&sch->dev);
1201}
1202
1203/* Schedule unregister if we have no cdev. */
1204static void io_subchannel_schedule_removal(struct subchannel *sch)
1205{
1206 get_device(&sch->dev);
1207 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1208 queue_work(slow_path_wq, &sch->work);
1209}
1210
1211/*
1212 * Note: We always return 0 so that we bind to the device even on error.
1213 * This is needed so that our remove function is called on unregister.
1214 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001215static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct ccw_device *cdev;
1218 int rc;
1219 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001220 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001222 cdev = sch_get_cdev(sch);
1223 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001224 rc = sysfs_create_group(&sch->dev.kobj,
1225 &io_subchannel_attr_group);
1226 if (rc)
1227 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1228 "attributes for subchannel "
1229 "0.%x.%04x (rc=%d)\n",
1230 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001233 * Throw the delayed uevent for the subchannel, register
1234 * the ccw_device and exit. This happens for all early
1235 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
Ming Leif67f1292009-03-01 21:10:49 +08001237 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001238 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001239 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 device_initialize(&cdev->dev);
1241 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001244 * the reference count needs to be corrected since we
1245 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
1247 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1248 cdev->private->state != DEV_STATE_OFFLINE &&
1249 cdev->private->state != DEV_STATE_BOXED)
1250 get_device(&cdev->dev);
1251 return 0;
1252 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001253 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001254 rc = cio_commit_config(sch);
1255 if (rc)
1256 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001257 rc = sysfs_create_group(&sch->dev.kobj,
1258 &io_subchannel_attr_group);
1259 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001260 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001261 /* Allocate I/O subchannel private data. */
1262 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1263 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001264 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001265 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001266 /*
1267 * First check if a fitting device may be found amongst the
1268 * disconnected devices or in the orphanage.
1269 */
1270 dev_id.devno = sch->schib.pmcw.dev;
1271 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001272 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1273 if (!cdev)
1274 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1275 &dev_id);
1276 if (cdev) {
1277 /*
1278 * Schedule moving the device until when we have a registered
1279 * subchannel to move to and succeed the probe. We can
1280 * unregister later again, when the probe is through.
1281 */
1282 cdev->private->sch = sch;
1283 PREPARE_WORK(&cdev->private->kick_work,
1284 ccw_device_move_to_sch);
1285 queue_work(slow_path_wq, &cdev->private->kick_work);
1286 return 0;
1287 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001288 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001289 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001290 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001291 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001293 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001294 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001295 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001297 return 0;
1298out_err:
1299 kfree(sch->private);
1300 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001301out_schedule:
1302 io_subchannel_schedule_removal(sch);
1303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001307io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct ccw_device *cdev;
1310 unsigned long flags;
1311
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001312 cdev = sch_get_cdev(sch);
1313 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* Set ccw device to not operational and drop reference. */
1316 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001317 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 cdev->private->state = DEV_STATE_NOT_OPER;
1319 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001320 ccw_device_unregister(cdev);
1321 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001322 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001323 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
Cornelia Huck602b20f2008-01-26 14:10:39 +01001327static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct ccw_device *cdev;
1330
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001331 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!cdev)
1333 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001334 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Cornelia Huck602b20f2008-01-26 14:10:39 +01001337static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct ccw_device *cdev;
1340
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001341 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (cdev)
1343 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1344}
1345
Cornelia Huckc820de32008-07-14 09:58:45 +02001346static int check_for_io_on_path(struct subchannel *sch, int mask)
1347{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001348 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001349 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001350 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001351 return 1;
1352 return 0;
1353}
1354
1355static void terminate_internal_io(struct subchannel *sch,
1356 struct ccw_device *cdev)
1357{
1358 if (cio_clear(sch)) {
1359 /* Recheck device in case clear failed. */
1360 sch->lpm = 0;
1361 if (cdev->online)
1362 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1363 else
1364 css_schedule_eval(sch->schid);
1365 return;
1366 }
1367 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1368 /* Request retry of internal operation. */
1369 cdev->private->flags.intretry = 1;
1370 /* Call handler. */
1371 if (cdev->handler)
1372 cdev->handler(cdev, cdev->private->intparm,
1373 ERR_PTR(-EIO));
1374}
1375
1376static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct ccw_device *cdev;
1379
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001380 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!cdev)
1382 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001383 if (check_for_io_on_path(sch, mask)) {
1384 if (cdev->private->state == DEV_STATE_ONLINE)
1385 ccw_device_kill_io(cdev);
1386 else {
1387 terminate_internal_io(sch, cdev);
1388 /* Re-start path verification. */
1389 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1390 }
1391 } else
1392 /* trigger path verification. */
1393 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1394
1395}
1396
Cornelia Huck99611f82008-07-14 09:59:02 +02001397static int io_subchannel_chp_event(struct subchannel *sch,
1398 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001399{
1400 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001401
Cornelia Huck99611f82008-07-14 09:59:02 +02001402 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001403 if (!mask)
1404 return 0;
1405 switch (event) {
1406 case CHP_VARY_OFF:
1407 sch->opm &= ~mask;
1408 sch->lpm &= ~mask;
1409 io_subchannel_terminate_path(sch, mask);
1410 break;
1411 case CHP_VARY_ON:
1412 sch->opm |= mask;
1413 sch->lpm |= mask;
1414 io_subchannel_verify(sch);
1415 break;
1416 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001417 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001418 return -ENODEV;
1419 io_subchannel_terminate_path(sch, mask);
1420 break;
1421 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001422 if (cio_update_schib(sch))
1423 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001424 sch->lpm |= mask & sch->opm;
1425 io_subchannel_verify(sch);
1426 break;
1427 }
1428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001432io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct ccw_device *cdev;
1435 int ret;
1436
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001437 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001439 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return;
1441 if (!sch->schib.pmcw.ena)
1442 /* Nothing to do. */
1443 return;
1444 ret = cio_disable_subchannel(sch);
1445 if (ret != -EBUSY)
1446 /* Subchannel is disabled, we're done. */
1447 return;
1448 cdev->private->state = DEV_STATE_QUIESCE;
1449 if (cdev->handler)
1450 cdev->handler(cdev, cdev->private->intparm,
1451 ERR_PTR(-EIO));
1452 ret = ccw_device_cancel_halt_clear(cdev);
1453 if (ret == -EBUSY) {
1454 ccw_device_set_timeout(cdev, HZ/10);
1455 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1456 }
1457 cio_disable_subchannel(sch);
1458}
1459
Cornelia Huckc820de32008-07-14 09:58:45 +02001460static int io_subchannel_get_status(struct subchannel *sch)
1461{
1462 struct schib schib;
1463
1464 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1465 return CIO_GONE;
1466 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1467 return CIO_REVALIDATE;
1468 if (!sch->lpm)
1469 return CIO_NO_PATH;
1470 return CIO_OPER;
1471}
1472
1473static int device_is_disconnected(struct ccw_device *cdev)
1474{
1475 if (!cdev)
1476 return 0;
1477 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1478 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1479}
1480
1481static int recovery_check(struct device *dev, void *data)
1482{
1483 struct ccw_device *cdev = to_ccwdev(dev);
1484 int *redo = data;
1485
1486 spin_lock_irq(cdev->ccwlock);
1487 switch (cdev->private->state) {
1488 case DEV_STATE_DISCONNECTED:
1489 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1490 cdev->private->dev_id.ssid,
1491 cdev->private->dev_id.devno);
1492 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1493 *redo = 1;
1494 break;
1495 case DEV_STATE_DISCONNECTED_SENSE_ID:
1496 *redo = 1;
1497 break;
1498 }
1499 spin_unlock_irq(cdev->ccwlock);
1500
1501 return 0;
1502}
1503
1504static void recovery_work_func(struct work_struct *unused)
1505{
1506 int redo = 0;
1507
1508 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1509 if (redo) {
1510 spin_lock_irq(&recovery_lock);
1511 if (!timer_pending(&recovery_timer)) {
1512 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1513 recovery_phase++;
1514 mod_timer(&recovery_timer, jiffies +
1515 recovery_delay[recovery_phase] * HZ);
1516 }
1517 spin_unlock_irq(&recovery_lock);
1518 } else
1519 CIO_MSG_EVENT(4, "recovery: end\n");
1520}
1521
1522static DECLARE_WORK(recovery_work, recovery_work_func);
1523
1524static void recovery_func(unsigned long data)
1525{
1526 /*
1527 * We can't do our recovery in softirq context and it's not
1528 * performance critical, so we schedule it.
1529 */
1530 schedule_work(&recovery_work);
1531}
1532
1533static void ccw_device_schedule_recovery(void)
1534{
1535 unsigned long flags;
1536
1537 CIO_MSG_EVENT(4, "recovery: schedule\n");
1538 spin_lock_irqsave(&recovery_lock, flags);
1539 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1540 recovery_phase = 0;
1541 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1542 }
1543 spin_unlock_irqrestore(&recovery_lock, flags);
1544}
1545
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001546static int purge_fn(struct device *dev, void *data)
1547{
1548 struct ccw_device *cdev = to_ccwdev(dev);
1549 struct ccw_device_private *priv = cdev->private;
1550 int unreg;
1551
1552 spin_lock_irq(cdev->ccwlock);
1553 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1554 (priv->state == DEV_STATE_OFFLINE);
1555 spin_unlock_irq(cdev->ccwlock);
1556 if (!unreg)
1557 goto out;
1558 if (!get_device(&cdev->dev))
1559 goto out;
1560 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1561 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001562 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001563
1564out:
1565 /* Abort loop in case of pending signal. */
1566 if (signal_pending(current))
1567 return -EINTR;
1568
1569 return 0;
1570}
1571
1572/**
1573 * ccw_purge_blacklisted - purge unused, blacklisted devices
1574 *
1575 * Unregister all ccw devices that are offline and on the blacklist.
1576 */
1577int ccw_purge_blacklisted(void)
1578{
1579 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1580 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1581 return 0;
1582}
1583
Cornelia Huckc820de32008-07-14 09:58:45 +02001584static void device_set_disconnected(struct ccw_device *cdev)
1585{
1586 if (!cdev)
1587 return;
1588 ccw_device_set_timeout(cdev, 0);
1589 cdev->private->flags.fake_irb = 0;
1590 cdev->private->state = DEV_STATE_DISCONNECTED;
1591 if (cdev->online)
1592 ccw_device_schedule_recovery();
1593}
1594
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001595void ccw_device_set_notoper(struct ccw_device *cdev)
1596{
1597 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1598
1599 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001600 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001601 ccw_device_set_timeout(cdev, 0);
1602 cio_disable_subchannel(sch);
1603 cdev->private->state = DEV_STATE_NOT_OPER;
1604}
1605
Cornelia Huckc820de32008-07-14 09:58:45 +02001606static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1607{
1608 int event, ret, disc;
1609 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001610 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001611 struct ccw_device *cdev;
1612
1613 spin_lock_irqsave(sch->lock, flags);
1614 cdev = sch_get_cdev(sch);
1615 disc = device_is_disconnected(cdev);
1616 if (disc && slow) {
1617 /* Disconnected devices are evaluated directly only.*/
1618 spin_unlock_irqrestore(sch->lock, flags);
1619 return 0;
1620 }
1621 /* No interrupt after machine check - kill pending timers. */
1622 if (cdev)
1623 ccw_device_set_timeout(cdev, 0);
1624 if (!disc && !slow) {
1625 /* Non-disconnected devices are evaluated on the slow path. */
1626 spin_unlock_irqrestore(sch->lock, flags);
1627 return -EAGAIN;
1628 }
1629 event = io_subchannel_get_status(sch);
1630 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1631 sch->schid.ssid, sch->schid.sch_no, event,
1632 disc ? "disconnected" : "normal",
1633 slow ? "slow" : "fast");
1634 /* Analyze subchannel status. */
1635 action = NONE;
1636 switch (event) {
1637 case CIO_NO_PATH:
1638 if (disc) {
1639 /* Check if paths have become available. */
1640 action = REPROBE;
1641 break;
1642 }
1643 /* fall through */
1644 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001645 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001646 if (io_subchannel_notify(sch, event))
1647 action = DISC;
1648 else
1649 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001650 break;
1651 case CIO_REVALIDATE:
1652 /* Device will be removed, so no notify necessary. */
1653 if (disc)
1654 /* Reprobe because immediate unregister might block. */
1655 action = REPROBE;
1656 else
1657 action = UNREGISTER_PROBE;
1658 break;
1659 case CIO_OPER:
1660 if (disc)
1661 /* Get device operational again. */
1662 action = REPROBE;
1663 break;
1664 }
1665 /* Perform action. */
1666 ret = 0;
1667 switch (action) {
1668 case UNREGISTER:
1669 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001670 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001671 /* Unregister device (will use subchannel lock). */
1672 spin_unlock_irqrestore(sch->lock, flags);
1673 css_sch_device_unregister(sch);
1674 spin_lock_irqsave(sch->lock, flags);
1675
1676 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001677 sch->config.intparm = 0;
1678 cio_commit_config(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001679 break;
1680 case REPROBE:
1681 ccw_device_trigger_reprobe(cdev);
1682 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001683 case DISC:
1684 device_set_disconnected(cdev);
1685 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001686 default:
1687 break;
1688 }
1689 spin_unlock_irqrestore(sch->lock, flags);
1690 /* Probe if necessary. */
1691 if (action == UNREGISTER_PROBE)
1692 ret = css_probe_device(sch->schid);
1693
1694 return ret;
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifdef CONFIG_CCW_CONSOLE
1698static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001699static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700static struct ccw_device_private console_private;
1701static int console_cdev_in_use;
1702
Cornelia Huck2ec22982006-12-08 15:54:26 +01001703static DEFINE_SPINLOCK(ccw_console_lock);
1704
1705spinlock_t * cio_get_console_lock(void)
1706{
1707 return &ccw_console_lock;
1708}
1709
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001710static int ccw_device_console_enable(struct ccw_device *cdev,
1711 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int rc;
1714
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001715 /* Attach subchannel private data. */
1716 sch->private = cio_get_console_priv();
1717 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001718 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001719 rc = cio_commit_config(sch);
1720 if (rc)
1721 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001722 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001724 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 rc = io_subchannel_recog(cdev, sch);
1726 if (rc)
1727 return rc;
1728
1729 /* Now wait for the async. recognition to come to an end. */
1730 spin_lock_irq(cdev->ccwlock);
1731 while (!dev_fsm_final_state(cdev))
1732 wait_cons_dev();
1733 rc = -EIO;
1734 if (cdev->private->state != DEV_STATE_OFFLINE)
1735 goto out_unlock;
1736 ccw_device_online(cdev);
1737 while (!dev_fsm_final_state(cdev))
1738 wait_cons_dev();
1739 if (cdev->private->state != DEV_STATE_ONLINE)
1740 goto out_unlock;
1741 rc = 0;
1742out_unlock:
1743 spin_unlock_irq(cdev->ccwlock);
1744 return 0;
1745}
1746
1747struct ccw_device *
1748ccw_device_probe_console(void)
1749{
1750 struct subchannel *sch;
1751 int ret;
1752
1753 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001754 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 sch = cio_probe_console();
1756 if (IS_ERR(sch)) {
1757 console_cdev_in_use = 0;
1758 return (void *) sch;
1759 }
1760 memset(&console_cdev, 0, sizeof(struct ccw_device));
1761 memset(&console_private, 0, sizeof(struct ccw_device_private));
1762 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001763 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ret = ccw_device_console_enable(&console_cdev, sch);
1765 if (ret) {
1766 cio_release_console();
1767 console_cdev_in_use = 0;
1768 return ERR_PTR(ret);
1769 }
1770 console_cdev.online = 1;
1771 return &console_cdev;
1772}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001773
1774
1775const char *cio_get_console_cdev_name(struct subchannel *sch)
1776{
1777 snprintf(console_cdev_name, 10, "0.%x.%04x",
1778 sch->schid.ssid, sch->schib.pmcw.dev);
1779 return (const char *)console_cdev_name;
1780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781#endif
1782
1783/*
1784 * get ccw_device matching the busid, but only if owned by cdrv
1785 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001786static int
1787__ccwdev_check_busid(struct device *dev, void *id)
1788{
1789 char *bus_id;
1790
Cornelia Huck12975ae2006-10-11 15:31:47 +02001791 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001792
Kay Sievers98df67b2008-12-25 13:38:55 +01001793 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001794}
1795
1796
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001797/**
1798 * get_ccwdev_by_busid() - obtain device from a bus id
1799 * @cdrv: driver the device is owned by
1800 * @bus_id: bus id of the device to be searched
1801 *
1802 * This function searches all devices owned by @cdrv for a device with a bus
1803 * id matching @bus_id.
1804 * Returns:
1805 * If a match is found, its reference count of the found device is increased
1806 * and it is returned; else %NULL is returned.
1807 */
1808struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1809 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001811 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct device_driver *drv;
1813
1814 drv = get_driver(&cdrv->driver);
1815 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001816 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001818 dev = driver_find_device(drv, NULL, (void *)bus_id,
1819 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 put_driver(drv);
1821
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001822 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825/************************** device driver handling ************************/
1826
1827/* This is the implementation of the ccw_driver class. The probe, remove
1828 * and release methods are initially very similar to the device_driver
1829 * implementations, with the difference that they have ccw_device
1830 * arguments.
1831 *
1832 * A ccw driver also contains the information that is needed for
1833 * device matching.
1834 */
1835static int
1836ccw_device_probe (struct device *dev)
1837{
1838 struct ccw_device *cdev = to_ccwdev(dev);
1839 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1840 int ret;
1841
1842 cdev->drv = cdrv; /* to let the driver call _set_online */
1843
1844 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1845
1846 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001847 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return ret;
1849 }
1850
1851 return 0;
1852}
1853
1854static int
1855ccw_device_remove (struct device *dev)
1856{
1857 struct ccw_device *cdev = to_ccwdev(dev);
1858 struct ccw_driver *cdrv = cdev->drv;
1859 int ret;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (cdrv->remove)
1862 cdrv->remove(cdev);
1863 if (cdev->online) {
1864 cdev->online = 0;
1865 spin_lock_irq(cdev->ccwlock);
1866 ret = ccw_device_offline(cdev);
1867 spin_unlock_irq(cdev->ccwlock);
1868 if (ret == 0)
1869 wait_event(cdev->private->wait_q,
1870 dev_fsm_final_state(cdev));
1871 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001872 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001873 "device 0.%x.%04x\n",
1874 ret, cdev->private->dev_id.ssid,
1875 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001876 /* Give up reference obtained in ccw_device_set_online(). */
1877 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001880 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
1882}
1883
Cornelia Huck958974fb2007-10-12 16:11:21 +02001884static void ccw_device_shutdown(struct device *dev)
1885{
1886 struct ccw_device *cdev;
1887
1888 cdev = to_ccwdev(dev);
1889 if (cdev->drv && cdev->drv->shutdown)
1890 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001891 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001892}
1893
Cornelia Huck8bbace72006-01-11 10:56:22 +01001894struct bus_type ccw_bus_type = {
1895 .name = "ccw",
1896 .match = ccw_bus_match,
1897 .uevent = ccw_uevent,
1898 .probe = ccw_device_probe,
1899 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001900 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001901};
1902
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001903/**
1904 * ccw_driver_register() - register a ccw driver
1905 * @cdriver: driver to be registered
1906 *
1907 * This function is mainly a wrapper around driver_register().
1908 * Returns:
1909 * %0 on success and a negative error value on failure.
1910 */
1911int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 struct device_driver *drv = &cdriver->driver;
1914
1915 drv->bus = &ccw_bus_type;
1916 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001917 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 return driver_register(drv);
1920}
1921
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001922/**
1923 * ccw_driver_unregister() - deregister a ccw driver
1924 * @cdriver: driver to be deregistered
1925 *
1926 * This function is mainly a wrapper around driver_unregister().
1927 */
1928void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 driver_unregister(&cdriver->driver);
1931}
1932
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001933/* Helper func for qdio. */
1934struct subchannel_id
1935ccw_device_get_subchannel_id(struct ccw_device *cdev)
1936{
1937 struct subchannel *sch;
1938
1939 sch = to_subchannel(cdev->dev.parent);
1940 return sch->schid;
1941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943MODULE_LICENSE("GPL");
1944EXPORT_SYMBOL(ccw_device_set_online);
1945EXPORT_SYMBOL(ccw_device_set_offline);
1946EXPORT_SYMBOL(ccw_driver_register);
1947EXPORT_SYMBOL(ccw_driver_unregister);
1948EXPORT_SYMBOL(get_ccwdev_by_busid);
1949EXPORT_SYMBOL(ccw_bus_type);
1950EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001951EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);