blob: 9be6dd5a56649f9b9c0fbca514a48a48a4c8b62c [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
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200313static void ccw_device_call_sch_unregister(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200315static void
316ccw_device_remove_disconnected(struct ccw_device *cdev)
317{
318 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319
320 /*
321 * Forced offline in disconnected state means
322 * 'throw away device'.
323 */
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200324 /* Get cdev reference for workqueue processing. */
325 if (!get_device(&cdev->dev))
326 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200327 if (ccw_device_is_orphan(cdev)) {
328 /*
329 * Deregister ccw device.
330 * Unfortunately, we cannot do this directly from the
331 * attribute method.
332 */
333 spin_lock_irqsave(cdev->ccwlock, flags);
334 cdev->private->state = DEV_STATE_NOT_OPER;
335 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200336 PREPARE_WORK(&cdev->private->kick_work,
337 ccw_device_remove_orphan_cb);
338 } else
339 /* Deregister subchannel, which will kill the ccw device. */
340 PREPARE_WORK(&cdev->private->kick_work,
341 ccw_device_call_sch_unregister);
342 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200343}
344
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200345/**
346 * ccw_device_set_offline() - disable a ccw device for I/O
347 * @cdev: target ccw device
348 *
349 * This function calls the driver's set_offline() function for @cdev, if
350 * given, and then disables @cdev.
351 * Returns:
352 * %0 on success and a negative error value on failure.
353 * Context:
354 * enabled, ccw device lock not held
355 */
356int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int ret;
359
360 if (!cdev)
361 return -ENODEV;
362 if (!cdev->online || !cdev->drv)
363 return -EINVAL;
364
365 if (cdev->drv->set_offline) {
366 ret = cdev->drv->set_offline(cdev);
367 if (ret != 0)
368 return ret;
369 }
370 cdev->online = 0;
371 spin_lock_irq(cdev->ccwlock);
372 ret = ccw_device_offline(cdev);
373 if (ret == -ENODEV) {
374 if (cdev->private->state != DEV_STATE_NOT_OPER) {
375 cdev->private->state = DEV_STATE_OFFLINE;
376 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
377 }
378 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100379 /* Give up reference from ccw_device_set_online(). */
380 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return ret;
382 }
383 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100384 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100386 /* Give up reference from ccw_device_set_online(). */
387 put_device(&cdev->dev);
388 } else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200389 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200390 "device 0.%x.%04x\n",
391 ret, cdev->private->dev_id.ssid,
392 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 cdev->online = 1;
394 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200398/**
399 * ccw_device_set_online() - enable a ccw device for I/O
400 * @cdev: target ccw device
401 *
402 * This function first enables @cdev and then calls the driver's set_online()
403 * function for @cdev, if given. If set_online() returns an error, @cdev is
404 * disabled again.
405 * Returns:
406 * %0 on success and a negative error value on failure.
407 * Context:
408 * enabled, ccw device lock not held
409 */
410int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int ret;
413
414 if (!cdev)
415 return -ENODEV;
416 if (cdev->online || !cdev->drv)
417 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100418 /* Hold on to an extra reference while device is online. */
419 if (!get_device(&cdev->dev))
420 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 spin_lock_irq(cdev->ccwlock);
423 ret = ccw_device_online(cdev);
424 spin_unlock_irq(cdev->ccwlock);
425 if (ret == 0)
426 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
427 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200428 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200429 "device 0.%x.%04x\n",
430 ret, cdev->private->dev_id.ssid,
431 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100432 /* Give up online reference since onlining failed. */
433 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return ret;
435 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100436 if (cdev->private->state != DEV_STATE_ONLINE) {
437 /* Give up online reference since onlining failed. */
438 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
442 cdev->online = 1;
443 return 0;
444 }
445 spin_lock_irq(cdev->ccwlock);
446 ret = ccw_device_offline(cdev);
447 spin_unlock_irq(cdev->ccwlock);
448 if (ret == 0)
449 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200450 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200451 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200452 "device 0.%x.%04x\n",
453 ret, cdev->private->dev_id.ssid,
454 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100455 /* Give up online reference since onlining failed. */
456 put_device(&cdev->dev);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800457 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100460static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200461{
462 if (cdev->private->state == DEV_STATE_DISCONNECTED)
463 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100464 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
465 return ccw_device_set_offline(cdev);
466 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200467}
468
469static int online_store_recog_and_online(struct ccw_device *cdev)
470{
471 int ret;
472
473 /* Do device recognition, if needed. */
474 if (cdev->id.cu_type == 0) {
475 ret = ccw_device_recognition(cdev);
476 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200477 CIO_MSG_EVENT(0, "Couldn't start recognition "
478 "for device 0.%x.%04x (ret=%d)\n",
479 cdev->private->dev_id.ssid,
480 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200481 return ret;
482 }
483 wait_event(cdev->private->wait_q,
484 cdev->private->flags.recog_done);
485 }
486 if (cdev->drv && cdev->drv->set_online)
487 ccw_device_set_online(cdev);
488 return 0;
489}
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200490static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491{
492 int ret;
493
494 ret = online_store_recog_and_online(cdev);
495 if (ret)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200496 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200497 if (force && cdev->private->state == DEV_STATE_BOXED) {
498 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200499 if (ret)
500 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200501 if (cdev->id.cu_type == 0)
502 cdev->private->state = DEV_STATE_NOT_OPER;
503 online_store_recog_and_online(cdev);
504 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200505 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506}
507
508static ssize_t online_store (struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200512 int force, ret;
513 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800515 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EAGAIN;
517
518 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
519 atomic_set(&cdev->private->onoff, 0);
520 return -EINVAL;
521 }
522 if (!strncmp(buf, "force\n", count)) {
523 force = 1;
524 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200525 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
527 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200528 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200530 if (ret)
531 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200532 switch (i) {
533 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100534 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200535 break;
536 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200537 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538 break;
539 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200540 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200542out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (cdev->drv)
544 module_put(cdev->drv->owner);
545 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100546 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400550available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct ccw_device *cdev = to_ccwdev(dev);
553 struct subchannel *sch;
554
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100555 if (ccw_device_is_orphan(cdev))
556 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 switch (cdev->private->state) {
558 case DEV_STATE_BOXED:
559 return sprintf(buf, "boxed\n");
560 case DEV_STATE_DISCONNECTED:
561 case DEV_STATE_DISCONNECTED_SENSE_ID:
562 case DEV_STATE_NOT_OPER:
563 sch = to_subchannel(dev->parent);
564 if (!sch->lpm)
565 return sprintf(buf, "no path\n");
566 else
567 return sprintf(buf, "no device\n");
568 default:
569 /* All other states considered fine. */
570 return sprintf(buf, "good\n");
571 }
572}
573
574static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
575static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
576static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
577static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800578static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static DEVICE_ATTR(availability, 0444, available_show, NULL);
581
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200582static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 &dev_attr_chpids.attr,
584 &dev_attr_pimpampom.attr,
585 NULL,
586};
587
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200588static struct attribute_group io_subchannel_attr_group = {
589 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100590};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592static struct attribute * ccwdev_attrs[] = {
593 &dev_attr_devtype.attr,
594 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800595 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 &dev_attr_online.attr,
597 &dev_attr_cmb_enable.attr,
598 &dev_attr_availability.attr,
599 NULL,
600};
601
602static struct attribute_group ccwdev_attr_group = {
603 .attrs = ccwdev_attrs,
604};
605
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200606static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200607 &ccwdev_attr_group,
608 NULL,
609};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* this is a simple abstraction for device_register that sets the
612 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200613static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct device *dev = &cdev->dev;
616 int ret;
617
618 dev->bus = &ccw_bus_type;
619
620 if ((ret = device_add(dev)))
621 return ret;
622
623 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700627struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200628 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700629 struct ccw_device * sibling;
630};
631
632static int
633match_devno(struct device * dev, void * data)
634{
Cornelia Huck78964262006-10-11 15:31:38 +0200635 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700636 struct ccw_device * cdev;
637
638 cdev = to_ccwdev(dev);
639 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100640 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200641 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100642 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700643 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700644 return 0;
645}
646
Cornelia Huck78964262006-10-11 15:31:38 +0200647static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
648 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200651 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Cornelia Huck78964262006-10-11 15:31:38 +0200653 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200654 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700655 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700657 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100660static int match_orphan(struct device *dev, void *data)
661{
662 struct ccw_dev_id *dev_id;
663 struct ccw_device *cdev;
664
665 dev_id = data;
666 cdev = to_ccwdev(dev);
667 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
668}
669
670static struct ccw_device *
671get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
672 struct ccw_dev_id *dev_id)
673{
674 struct device *dev;
675
676 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
677 match_orphan);
678
679 return dev ? to_ccwdev(dev) : NULL;
680}
681
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100682void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100684 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct ccw_device *cdev;
686 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100689 priv = container_of(work, struct ccw_device_private, kick_work);
690 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100693 if (test_bit(1, &cdev->private->registered)) {
694 device_release_driver(&cdev->dev);
695 ret = device_attach(&cdev->dev);
696 WARN_ON(ret == -ENODEV);
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static void
701ccw_device_release(struct device *dev)
702{
703 struct ccw_device *cdev;
704
705 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100706 /* Release reference of parent subchannel. */
707 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 kfree(cdev->private);
709 kfree(cdev);
710}
711
Cornelia Huck7674da72006-12-08 15:54:21 +0100712static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
713{
714 struct ccw_device *cdev;
715
716 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
717 if (cdev) {
718 cdev->private = kzalloc(sizeof(struct ccw_device_private),
719 GFP_KERNEL | GFP_DMA);
720 if (cdev->private)
721 return cdev;
722 }
723 kfree(cdev);
724 return ERR_PTR(-ENOMEM);
725}
726
727static int io_subchannel_initialize_dev(struct subchannel *sch,
728 struct ccw_device *cdev)
729{
730 cdev->private->cdev = cdev;
731 atomic_set(&cdev->private->onoff, 0);
732 cdev->dev.parent = &sch->dev;
733 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100734 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200735 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100736 /* Do first half of device_register. */
737 device_initialize(&cdev->dev);
738 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100739 /* Release reference from device_initialize(). */
740 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100741 return -ENODEV;
742 }
743 return 0;
744}
745
746static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
747{
748 struct ccw_device *cdev;
749 int ret;
750
751 cdev = io_subchannel_allocate_dev(sch);
752 if (!IS_ERR(cdev)) {
753 ret = io_subchannel_initialize_dev(sch, cdev);
754 if (ret) {
755 kfree(cdev);
756 cdev = ERR_PTR(ret);
757 }
758 }
759 return cdev;
760}
761
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100762static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
763
764static void sch_attach_device(struct subchannel *sch,
765 struct ccw_device *cdev)
766{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200767 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100768 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100769 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100770 cdev->private->schid = sch->schid;
771 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200772 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100773 spin_unlock_irq(sch->lock);
774}
775
776static void sch_attach_disconnected_device(struct subchannel *sch,
777 struct ccw_device *cdev)
778{
779 struct subchannel *other_sch;
780 int ret;
781
Cornelia Huck6eff2082008-12-25 13:39:07 +0100782 /* Get reference for new parent. */
783 if (!get_device(&sch->dev))
784 return;
785 other_sch = to_subchannel(cdev->dev.parent);
786 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100787 ret = device_move(&cdev->dev, &sch->dev);
788 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200789 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100790 "(ret=%d)!\n", cdev->private->dev_id.ssid,
791 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100792 /* Put reference for new parent. */
793 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100794 return;
795 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100796 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100797 /* No need to keep a subchannel without ccw device around. */
798 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100799 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100800 /* Put reference for old parent. */
801 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100802}
803
804static void sch_attach_orphaned_device(struct subchannel *sch,
805 struct ccw_device *cdev)
806{
807 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100808 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100809
Cornelia Huck6eff2082008-12-25 13:39:07 +0100810 /* Get reference for new parent. */
811 if (!get_device(&sch->dev))
812 return;
813 pseudo_sch = to_subchannel(cdev->dev.parent);
814 /*
815 * Try to move the ccw device to its new subchannel.
816 * Note: device_move() changes cdev->dev.parent
817 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100818 ret = device_move(&cdev->dev, &sch->dev);
819 if (ret) {
820 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
821 "failed (ret=%d)!\n",
822 cdev->private->dev_id.ssid,
823 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100824 /* Put reference for new parent. */
825 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100826 return;
827 }
828 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100829 /* Put reference on pseudo subchannel. */
830 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100831}
832
833static void sch_create_and_recog_new_device(struct subchannel *sch)
834{
835 struct ccw_device *cdev;
836
837 /* Need to allocate a new ccw device. */
838 cdev = io_subchannel_create_ccwdev(sch);
839 if (IS_ERR(cdev)) {
840 /* OK, we did everything we could... */
841 css_sch_device_unregister(sch);
842 return;
843 }
844 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100845 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100846 spin_unlock_irq(sch->lock);
847 /* Start recognition for the new ccw device. */
848 if (io_subchannel_recog(cdev, sch)) {
849 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100850 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100851 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100852 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100853 /* Put reference from io_subchannel_create_ccwdev(). */
854 put_device(&sch->dev);
855 /* Give up initial reference. */
856 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100857 }
858}
859
860
861void ccw_device_move_to_orphanage(struct work_struct *work)
862{
863 struct ccw_device_private *priv;
864 struct ccw_device *cdev;
865 struct ccw_device *replacing_cdev;
866 struct subchannel *sch;
867 int ret;
868 struct channel_subsystem *css;
869 struct ccw_dev_id dev_id;
870
871 priv = container_of(work, struct ccw_device_private, kick_work);
872 cdev = priv->cdev;
873 sch = to_subchannel(cdev->dev.parent);
874 css = to_css(sch->dev.parent);
875 dev_id.devno = sch->schib.pmcw.dev;
876 dev_id.ssid = sch->schid.ssid;
877
Cornelia Huck6eff2082008-12-25 13:39:07 +0100878 /* Increase refcount for pseudo subchannel. */
879 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100880 /*
881 * Move the orphaned ccw device to the orphanage so the replacing
882 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100883 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100884 */
885 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
886 if (ret) {
887 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
888 "(ret=%d)!\n", cdev->private->dev_id.ssid,
889 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100890 /* Decrease refcount for pseudo subchannel again. */
891 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100892 return;
893 }
894 cdev->ccwlock = css->pseudo_subchannel->lock;
895 /*
896 * Search for the replacing ccw device
897 * - among the disconnected devices
898 * - in the orphanage
899 */
900 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
901 if (replacing_cdev) {
902 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100903 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100904 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100905 /* Release reference of subchannel from old cdev. */
906 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100907 return;
908 }
909 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
910 if (replacing_cdev) {
911 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100912 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100913 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100914 /* Release reference of subchannel from old cdev. */
915 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100916 return;
917 }
918 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100919 /* Release reference of subchannel from old cdev. */
920 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/*
924 * Register recognized device.
925 */
926static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100927io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100929 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 struct ccw_device *cdev;
931 struct subchannel *sch;
932 int ret;
933 unsigned long flags;
934
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100935 priv = container_of(work, struct ccw_device_private, kick_work);
936 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100938 /*
939 * Check if subchannel is still registered. It may have become
940 * unregistered if a machine check hit us after finishing
941 * device recognition but before the register work could be
942 * queued.
943 */
944 if (!device_is_registered(&sch->dev))
945 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200946 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100947 /*
948 * io_subchannel_register() will also be called after device
949 * recognition has been done for a boxed device (which will already
950 * be registered). We need to reprobe since we may now have sense id
951 * information.
952 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100953 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100954 if (!cdev->drv) {
955 ret = device_reprobe(&cdev->dev);
956 if (ret)
957 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200958 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200959 " %d for 0.%x.%04x\n", ret,
960 cdev->private->dev_id.ssid,
961 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto out;
964 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700965 /*
966 * Now we know this subchannel will stay, we can throw
967 * our delayed uevent.
968 */
969 sch->dev.uevent_suppress = 0;
970 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* make it known to the system */
972 ret = ccw_device_register(cdev);
973 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200974 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
975 cdev->private->dev_id.ssid,
976 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100977 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100978 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100979 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100980 /* Release initial device reference. */
981 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100982 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984out:
985 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100987out_err:
988 /* Release reference for workqueue processing. */
989 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (atomic_dec_and_test(&ccw_device_init_count))
991 wake_up(&ccw_device_init_wq);
992}
993
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200994static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100996 struct ccw_device_private *priv;
997 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 struct subchannel *sch;
999
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001000 priv = container_of(work, struct ccw_device_private, kick_work);
1001 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001002 /* Get subchannel reference for local processing. */
1003 if (!get_device(cdev->dev.parent))
1004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001006 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001008 sch->config.intparm = 0;
1009 cio_commit_config(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001010 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001012 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 put_device(&sch->dev);
1014}
1015
1016/*
1017 * subchannel recognition done. Called from the state machine.
1018 */
1019void
1020io_subchannel_recog_done(struct ccw_device *cdev)
1021{
1022 struct subchannel *sch;
1023
1024 if (css_init_done == 0) {
1025 cdev->private->flags.recog_done = 1;
1026 return;
1027 }
1028 switch (cdev->private->state) {
1029 case DEV_STATE_NOT_OPER:
1030 cdev->private->flags.recog_done = 1;
1031 /* Remove device found not operational. */
1032 if (!get_device(&cdev->dev))
1033 break;
1034 sch = to_subchannel(cdev->dev.parent);
1035 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001036 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 queue_work(slow_path_wq, &cdev->private->kick_work);
1038 if (atomic_dec_and_test(&ccw_device_init_count))
1039 wake_up(&ccw_device_init_wq);
1040 break;
1041 case DEV_STATE_BOXED:
1042 /* Device did not respond in time. */
1043 case DEV_STATE_OFFLINE:
1044 /*
1045 * We can't register the device in interrupt context so
1046 * we schedule a work item.
1047 */
1048 if (!get_device(&cdev->dev))
1049 break;
1050 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001051 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 queue_work(slow_path_wq, &cdev->private->kick_work);
1053 break;
1054 }
1055}
1056
1057static int
1058io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1059{
1060 int rc;
1061 struct ccw_device_private *priv;
1062
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001063 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001064 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Init private data. */
1067 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001068 priv->dev_id.devno = sch->schib.pmcw.dev;
1069 priv->dev_id.ssid = sch->schid.ssid;
1070 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 priv->state = DEV_STATE_NOT_OPER;
1072 INIT_LIST_HEAD(&priv->cmb_list);
1073 init_waitqueue_head(&priv->wait_q);
1074 init_timer(&priv->timer);
1075
1076 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001077 if (cio_is_console(sch->schid))
1078 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1079 else
1080 dev_set_name(&cdev->dev, "0.%x.%04x",
1081 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* Increase counter of devices currently in recognition. */
1084 atomic_inc(&ccw_device_init_count);
1085
1086 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001087 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001089 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (rc) {
1091 if (atomic_dec_and_test(&ccw_device_init_count))
1092 wake_up(&ccw_device_init_wq);
1093 }
1094 return rc;
1095}
1096
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001097static void ccw_device_move_to_sch(struct work_struct *work)
1098{
1099 struct ccw_device_private *priv;
1100 int rc;
1101 struct subchannel *sch;
1102 struct ccw_device *cdev;
1103 struct subchannel *former_parent;
1104
1105 priv = container_of(work, struct ccw_device_private, kick_work);
1106 sch = priv->sch;
1107 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001108 former_parent = to_subchannel(cdev->dev.parent);
1109 /* Get reference for new parent. */
1110 if (!get_device(&sch->dev))
1111 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001112 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001113 /*
1114 * Try to move the ccw device to its new subchannel.
1115 * Note: device_move() changes cdev->dev.parent
1116 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001117 rc = device_move(&cdev->dev, &sch->dev);
1118 mutex_unlock(&sch->reg_mutex);
1119 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001120 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001121 "0.%x.%04x failed (ret=%d)!\n",
1122 cdev->private->dev_id.ssid,
1123 cdev->private->dev_id.devno, sch->schid.ssid,
1124 sch->schid.sch_no, rc);
1125 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001126 /* Put reference for new parent again. */
1127 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001128 goto out;
1129 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001130 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001131 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001132 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001133 spin_unlock_irq(former_parent->lock);
1134 css_sch_device_unregister(former_parent);
1135 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001136 former_parent->config.intparm = 0;
1137 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001138 }
1139 sch_attach_device(sch, cdev);
1140out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001141 /* Put reference for old parent. */
1142 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001143 put_device(&cdev->dev);
1144}
1145
Cornelia Huck602b20f2008-01-26 14:10:39 +01001146static void io_subchannel_irq(struct subchannel *sch)
1147{
1148 struct ccw_device *cdev;
1149
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001150 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001151
1152 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001153 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001154 if (cdev)
1155 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1156}
1157
Sebastian Ott13952ec2008-12-25 13:39:13 +01001158void io_subchannel_init_config(struct subchannel *sch)
1159{
1160 memset(&sch->config, 0, sizeof(sch->config));
1161 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001162 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1163 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001164 sch->config.mp = 1;
1165}
1166
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001167static void io_subchannel_init_fields(struct subchannel *sch)
1168{
1169 if (cio_is_console(sch->schid))
1170 sch->opm = 0xff;
1171 else
1172 sch->opm = chp_get_sch_opm(sch);
1173 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001174 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001175
1176 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1177 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1178 sch->schib.pmcw.dev, sch->schid.ssid,
1179 sch->schid.sch_no, sch->schib.pmcw.pim,
1180 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001181
1182 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001183}
1184
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001185static void io_subchannel_do_unreg(struct work_struct *work)
1186{
1187 struct subchannel *sch;
1188
1189 sch = container_of(work, struct subchannel, work);
1190 css_sch_device_unregister(sch);
1191 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001192 sch->config.intparm = 0;
1193 cio_commit_config(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001194 put_device(&sch->dev);
1195}
1196
1197/* Schedule unregister if we have no cdev. */
1198static void io_subchannel_schedule_removal(struct subchannel *sch)
1199{
1200 get_device(&sch->dev);
1201 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1202 queue_work(slow_path_wq, &sch->work);
1203}
1204
1205/*
1206 * Note: We always return 0 so that we bind to the device even on error.
1207 * This is needed so that our remove function is called on unregister.
1208 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001209static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct ccw_device *cdev;
1212 int rc;
1213 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001214 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001216 cdev = sch_get_cdev(sch);
1217 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001218 rc = sysfs_create_group(&sch->dev.kobj,
1219 &io_subchannel_attr_group);
1220 if (rc)
1221 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1222 "attributes for subchannel "
1223 "0.%x.%04x (rc=%d)\n",
1224 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001227 * Throw the delayed uevent for the subchannel, register
1228 * the ccw_device and exit. This happens for all early
1229 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001231 sch->dev.uevent_suppress = 0;
1232 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001233 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 device_initialize(&cdev->dev);
1235 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001238 * the reference count needs to be corrected since we
1239 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
1241 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1242 cdev->private->state != DEV_STATE_OFFLINE &&
1243 cdev->private->state != DEV_STATE_BOXED)
1244 get_device(&cdev->dev);
1245 return 0;
1246 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001247 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001248 rc = cio_commit_config(sch);
1249 if (rc)
1250 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001251 rc = sysfs_create_group(&sch->dev.kobj,
1252 &io_subchannel_attr_group);
1253 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001254 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001255 /* Allocate I/O subchannel private data. */
1256 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1257 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001258 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001259 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001260 /*
1261 * First check if a fitting device may be found amongst the
1262 * disconnected devices or in the orphanage.
1263 */
1264 dev_id.devno = sch->schib.pmcw.dev;
1265 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001266 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1267 if (!cdev)
1268 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1269 &dev_id);
1270 if (cdev) {
1271 /*
1272 * Schedule moving the device until when we have a registered
1273 * subchannel to move to and succeed the probe. We can
1274 * unregister later again, when the probe is through.
1275 */
1276 cdev->private->sch = sch;
1277 PREPARE_WORK(&cdev->private->kick_work,
1278 ccw_device_move_to_sch);
1279 queue_work(slow_path_wq, &cdev->private->kick_work);
1280 return 0;
1281 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001282 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001283 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001284 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001285 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001287 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001288 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001289 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001291 return 0;
1292out_err:
1293 kfree(sch->private);
1294 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001295out_schedule:
1296 io_subchannel_schedule_removal(sch);
1297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001301io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct ccw_device *cdev;
1304 unsigned long flags;
1305
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001306 cdev = sch_get_cdev(sch);
1307 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* Set ccw device to not operational and drop reference. */
1310 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001311 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 cdev->private->state = DEV_STATE_NOT_OPER;
1313 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001314 ccw_device_unregister(cdev);
1315 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001316 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001317 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319}
1320
Cornelia Huck602b20f2008-01-26 14:10:39 +01001321static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct ccw_device *cdev;
1324
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001325 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!cdev)
1327 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001328 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Cornelia Huck602b20f2008-01-26 14:10:39 +01001331static void io_subchannel_verify(struct subchannel *sch)
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 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1338}
1339
Cornelia Huckc820de32008-07-14 09:58:45 +02001340static int check_for_io_on_path(struct subchannel *sch, int mask)
1341{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001342 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001343 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001344 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001345 return 1;
1346 return 0;
1347}
1348
1349static void terminate_internal_io(struct subchannel *sch,
1350 struct ccw_device *cdev)
1351{
1352 if (cio_clear(sch)) {
1353 /* Recheck device in case clear failed. */
1354 sch->lpm = 0;
1355 if (cdev->online)
1356 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1357 else
1358 css_schedule_eval(sch->schid);
1359 return;
1360 }
1361 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1362 /* Request retry of internal operation. */
1363 cdev->private->flags.intretry = 1;
1364 /* Call handler. */
1365 if (cdev->handler)
1366 cdev->handler(cdev, cdev->private->intparm,
1367 ERR_PTR(-EIO));
1368}
1369
1370static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 struct ccw_device *cdev;
1373
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001374 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!cdev)
1376 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001377 if (check_for_io_on_path(sch, mask)) {
1378 if (cdev->private->state == DEV_STATE_ONLINE)
1379 ccw_device_kill_io(cdev);
1380 else {
1381 terminate_internal_io(sch, cdev);
1382 /* Re-start path verification. */
1383 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1384 }
1385 } else
1386 /* trigger path verification. */
1387 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1388
1389}
1390
Cornelia Huck99611f82008-07-14 09:59:02 +02001391static int io_subchannel_chp_event(struct subchannel *sch,
1392 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001393{
1394 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001395
Cornelia Huck99611f82008-07-14 09:59:02 +02001396 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001397 if (!mask)
1398 return 0;
1399 switch (event) {
1400 case CHP_VARY_OFF:
1401 sch->opm &= ~mask;
1402 sch->lpm &= ~mask;
1403 io_subchannel_terminate_path(sch, mask);
1404 break;
1405 case CHP_VARY_ON:
1406 sch->opm |= mask;
1407 sch->lpm |= mask;
1408 io_subchannel_verify(sch);
1409 break;
1410 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001411 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001412 return -ENODEV;
1413 io_subchannel_terminate_path(sch, mask);
1414 break;
1415 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001416 if (cio_update_schib(sch))
1417 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001418 sch->lpm |= mask & sch->opm;
1419 io_subchannel_verify(sch);
1420 break;
1421 }
1422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001426io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 struct ccw_device *cdev;
1429 int ret;
1430
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001431 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001433 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return;
1435 if (!sch->schib.pmcw.ena)
1436 /* Nothing to do. */
1437 return;
1438 ret = cio_disable_subchannel(sch);
1439 if (ret != -EBUSY)
1440 /* Subchannel is disabled, we're done. */
1441 return;
1442 cdev->private->state = DEV_STATE_QUIESCE;
1443 if (cdev->handler)
1444 cdev->handler(cdev, cdev->private->intparm,
1445 ERR_PTR(-EIO));
1446 ret = ccw_device_cancel_halt_clear(cdev);
1447 if (ret == -EBUSY) {
1448 ccw_device_set_timeout(cdev, HZ/10);
1449 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1450 }
1451 cio_disable_subchannel(sch);
1452}
1453
Cornelia Huckc820de32008-07-14 09:58:45 +02001454static int io_subchannel_get_status(struct subchannel *sch)
1455{
1456 struct schib schib;
1457
1458 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1459 return CIO_GONE;
1460 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1461 return CIO_REVALIDATE;
1462 if (!sch->lpm)
1463 return CIO_NO_PATH;
1464 return CIO_OPER;
1465}
1466
1467static int device_is_disconnected(struct ccw_device *cdev)
1468{
1469 if (!cdev)
1470 return 0;
1471 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1472 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1473}
1474
1475static int recovery_check(struct device *dev, void *data)
1476{
1477 struct ccw_device *cdev = to_ccwdev(dev);
1478 int *redo = data;
1479
1480 spin_lock_irq(cdev->ccwlock);
1481 switch (cdev->private->state) {
1482 case DEV_STATE_DISCONNECTED:
1483 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1484 cdev->private->dev_id.ssid,
1485 cdev->private->dev_id.devno);
1486 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1487 *redo = 1;
1488 break;
1489 case DEV_STATE_DISCONNECTED_SENSE_ID:
1490 *redo = 1;
1491 break;
1492 }
1493 spin_unlock_irq(cdev->ccwlock);
1494
1495 return 0;
1496}
1497
1498static void recovery_work_func(struct work_struct *unused)
1499{
1500 int redo = 0;
1501
1502 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1503 if (redo) {
1504 spin_lock_irq(&recovery_lock);
1505 if (!timer_pending(&recovery_timer)) {
1506 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1507 recovery_phase++;
1508 mod_timer(&recovery_timer, jiffies +
1509 recovery_delay[recovery_phase] * HZ);
1510 }
1511 spin_unlock_irq(&recovery_lock);
1512 } else
1513 CIO_MSG_EVENT(4, "recovery: end\n");
1514}
1515
1516static DECLARE_WORK(recovery_work, recovery_work_func);
1517
1518static void recovery_func(unsigned long data)
1519{
1520 /*
1521 * We can't do our recovery in softirq context and it's not
1522 * performance critical, so we schedule it.
1523 */
1524 schedule_work(&recovery_work);
1525}
1526
1527static void ccw_device_schedule_recovery(void)
1528{
1529 unsigned long flags;
1530
1531 CIO_MSG_EVENT(4, "recovery: schedule\n");
1532 spin_lock_irqsave(&recovery_lock, flags);
1533 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1534 recovery_phase = 0;
1535 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1536 }
1537 spin_unlock_irqrestore(&recovery_lock, flags);
1538}
1539
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001540static int purge_fn(struct device *dev, void *data)
1541{
1542 struct ccw_device *cdev = to_ccwdev(dev);
1543 struct ccw_device_private *priv = cdev->private;
1544 int unreg;
1545
1546 spin_lock_irq(cdev->ccwlock);
1547 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1548 (priv->state == DEV_STATE_OFFLINE);
1549 spin_unlock_irq(cdev->ccwlock);
1550 if (!unreg)
1551 goto out;
1552 if (!get_device(&cdev->dev))
1553 goto out;
1554 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1555 priv->dev_id.devno);
1556 PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
1557 queue_work(slow_path_wq, &cdev->private->kick_work);
1558
1559out:
1560 /* Abort loop in case of pending signal. */
1561 if (signal_pending(current))
1562 return -EINTR;
1563
1564 return 0;
1565}
1566
1567/**
1568 * ccw_purge_blacklisted - purge unused, blacklisted devices
1569 *
1570 * Unregister all ccw devices that are offline and on the blacklist.
1571 */
1572int ccw_purge_blacklisted(void)
1573{
1574 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1575 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1576 return 0;
1577}
1578
Cornelia Huckc820de32008-07-14 09:58:45 +02001579static void device_set_disconnected(struct ccw_device *cdev)
1580{
1581 if (!cdev)
1582 return;
1583 ccw_device_set_timeout(cdev, 0);
1584 cdev->private->flags.fake_irb = 0;
1585 cdev->private->state = DEV_STATE_DISCONNECTED;
1586 if (cdev->online)
1587 ccw_device_schedule_recovery();
1588}
1589
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001590void ccw_device_set_notoper(struct ccw_device *cdev)
1591{
1592 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1593
1594 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001595 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001596 ccw_device_set_timeout(cdev, 0);
1597 cio_disable_subchannel(sch);
1598 cdev->private->state = DEV_STATE_NOT_OPER;
1599}
1600
Cornelia Huckc820de32008-07-14 09:58:45 +02001601static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1602{
1603 int event, ret, disc;
1604 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001605 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001606 struct ccw_device *cdev;
1607
1608 spin_lock_irqsave(sch->lock, flags);
1609 cdev = sch_get_cdev(sch);
1610 disc = device_is_disconnected(cdev);
1611 if (disc && slow) {
1612 /* Disconnected devices are evaluated directly only.*/
1613 spin_unlock_irqrestore(sch->lock, flags);
1614 return 0;
1615 }
1616 /* No interrupt after machine check - kill pending timers. */
1617 if (cdev)
1618 ccw_device_set_timeout(cdev, 0);
1619 if (!disc && !slow) {
1620 /* Non-disconnected devices are evaluated on the slow path. */
1621 spin_unlock_irqrestore(sch->lock, flags);
1622 return -EAGAIN;
1623 }
1624 event = io_subchannel_get_status(sch);
1625 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1626 sch->schid.ssid, sch->schid.sch_no, event,
1627 disc ? "disconnected" : "normal",
1628 slow ? "slow" : "fast");
1629 /* Analyze subchannel status. */
1630 action = NONE;
1631 switch (event) {
1632 case CIO_NO_PATH:
1633 if (disc) {
1634 /* Check if paths have become available. */
1635 action = REPROBE;
1636 break;
1637 }
1638 /* fall through */
1639 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001640 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001641 if (io_subchannel_notify(sch, event))
1642 action = DISC;
1643 else
1644 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001645 break;
1646 case CIO_REVALIDATE:
1647 /* Device will be removed, so no notify necessary. */
1648 if (disc)
1649 /* Reprobe because immediate unregister might block. */
1650 action = REPROBE;
1651 else
1652 action = UNREGISTER_PROBE;
1653 break;
1654 case CIO_OPER:
1655 if (disc)
1656 /* Get device operational again. */
1657 action = REPROBE;
1658 break;
1659 }
1660 /* Perform action. */
1661 ret = 0;
1662 switch (action) {
1663 case UNREGISTER:
1664 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001665 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001666 /* Unregister device (will use subchannel lock). */
1667 spin_unlock_irqrestore(sch->lock, flags);
1668 css_sch_device_unregister(sch);
1669 spin_lock_irqsave(sch->lock, flags);
1670
1671 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001672 sch->config.intparm = 0;
1673 cio_commit_config(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001674 break;
1675 case REPROBE:
1676 ccw_device_trigger_reprobe(cdev);
1677 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001678 case DISC:
1679 device_set_disconnected(cdev);
1680 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001681 default:
1682 break;
1683 }
1684 spin_unlock_irqrestore(sch->lock, flags);
1685 /* Probe if necessary. */
1686 if (action == UNREGISTER_PROBE)
1687 ret = css_probe_device(sch->schid);
1688
1689 return ret;
1690}
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#ifdef CONFIG_CCW_CONSOLE
1693static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001694static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695static struct ccw_device_private console_private;
1696static int console_cdev_in_use;
1697
Cornelia Huck2ec22982006-12-08 15:54:26 +01001698static DEFINE_SPINLOCK(ccw_console_lock);
1699
1700spinlock_t * cio_get_console_lock(void)
1701{
1702 return &ccw_console_lock;
1703}
1704
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001705static int ccw_device_console_enable(struct ccw_device *cdev,
1706 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 int rc;
1709
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001710 /* Attach subchannel private data. */
1711 sch->private = cio_get_console_priv();
1712 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001713 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001714 rc = cio_commit_config(sch);
1715 if (rc)
1716 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001717 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001719 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rc = io_subchannel_recog(cdev, sch);
1721 if (rc)
1722 return rc;
1723
1724 /* Now wait for the async. recognition to come to an end. */
1725 spin_lock_irq(cdev->ccwlock);
1726 while (!dev_fsm_final_state(cdev))
1727 wait_cons_dev();
1728 rc = -EIO;
1729 if (cdev->private->state != DEV_STATE_OFFLINE)
1730 goto out_unlock;
1731 ccw_device_online(cdev);
1732 while (!dev_fsm_final_state(cdev))
1733 wait_cons_dev();
1734 if (cdev->private->state != DEV_STATE_ONLINE)
1735 goto out_unlock;
1736 rc = 0;
1737out_unlock:
1738 spin_unlock_irq(cdev->ccwlock);
1739 return 0;
1740}
1741
1742struct ccw_device *
1743ccw_device_probe_console(void)
1744{
1745 struct subchannel *sch;
1746 int ret;
1747
1748 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001749 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 sch = cio_probe_console();
1751 if (IS_ERR(sch)) {
1752 console_cdev_in_use = 0;
1753 return (void *) sch;
1754 }
1755 memset(&console_cdev, 0, sizeof(struct ccw_device));
1756 memset(&console_private, 0, sizeof(struct ccw_device_private));
1757 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001758 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ret = ccw_device_console_enable(&console_cdev, sch);
1760 if (ret) {
1761 cio_release_console();
1762 console_cdev_in_use = 0;
1763 return ERR_PTR(ret);
1764 }
1765 console_cdev.online = 1;
1766 return &console_cdev;
1767}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001768
1769
1770const char *cio_get_console_cdev_name(struct subchannel *sch)
1771{
1772 snprintf(console_cdev_name, 10, "0.%x.%04x",
1773 sch->schid.ssid, sch->schib.pmcw.dev);
1774 return (const char *)console_cdev_name;
1775}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776#endif
1777
1778/*
1779 * get ccw_device matching the busid, but only if owned by cdrv
1780 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001781static int
1782__ccwdev_check_busid(struct device *dev, void *id)
1783{
1784 char *bus_id;
1785
Cornelia Huck12975ae2006-10-11 15:31:47 +02001786 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001787
Kay Sievers98df67b2008-12-25 13:38:55 +01001788 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001789}
1790
1791
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001792/**
1793 * get_ccwdev_by_busid() - obtain device from a bus id
1794 * @cdrv: driver the device is owned by
1795 * @bus_id: bus id of the device to be searched
1796 *
1797 * This function searches all devices owned by @cdrv for a device with a bus
1798 * id matching @bus_id.
1799 * Returns:
1800 * If a match is found, its reference count of the found device is increased
1801 * and it is returned; else %NULL is returned.
1802 */
1803struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1804 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001806 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct device_driver *drv;
1808
1809 drv = get_driver(&cdrv->driver);
1810 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001811 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001813 dev = driver_find_device(drv, NULL, (void *)bus_id,
1814 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 put_driver(drv);
1816
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001817 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820/************************** device driver handling ************************/
1821
1822/* This is the implementation of the ccw_driver class. The probe, remove
1823 * and release methods are initially very similar to the device_driver
1824 * implementations, with the difference that they have ccw_device
1825 * arguments.
1826 *
1827 * A ccw driver also contains the information that is needed for
1828 * device matching.
1829 */
1830static int
1831ccw_device_probe (struct device *dev)
1832{
1833 struct ccw_device *cdev = to_ccwdev(dev);
1834 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1835 int ret;
1836
1837 cdev->drv = cdrv; /* to let the driver call _set_online */
1838
1839 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1840
1841 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001842 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return ret;
1844 }
1845
1846 return 0;
1847}
1848
1849static int
1850ccw_device_remove (struct device *dev)
1851{
1852 struct ccw_device *cdev = to_ccwdev(dev);
1853 struct ccw_driver *cdrv = cdev->drv;
1854 int ret;
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (cdrv->remove)
1857 cdrv->remove(cdev);
1858 if (cdev->online) {
1859 cdev->online = 0;
1860 spin_lock_irq(cdev->ccwlock);
1861 ret = ccw_device_offline(cdev);
1862 spin_unlock_irq(cdev->ccwlock);
1863 if (ret == 0)
1864 wait_event(cdev->private->wait_q,
1865 dev_fsm_final_state(cdev));
1866 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001867 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001868 "device 0.%x.%04x\n",
1869 ret, cdev->private->dev_id.ssid,
1870 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001871 /* Give up reference obtained in ccw_device_set_online(). */
1872 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001875 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 0;
1877}
1878
Cornelia Huck958974fb2007-10-12 16:11:21 +02001879static void ccw_device_shutdown(struct device *dev)
1880{
1881 struct ccw_device *cdev;
1882
1883 cdev = to_ccwdev(dev);
1884 if (cdev->drv && cdev->drv->shutdown)
1885 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001886 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001887}
1888
Cornelia Huck8bbace72006-01-11 10:56:22 +01001889struct bus_type ccw_bus_type = {
1890 .name = "ccw",
1891 .match = ccw_bus_match,
1892 .uevent = ccw_uevent,
1893 .probe = ccw_device_probe,
1894 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001895 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001896};
1897
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001898/**
1899 * ccw_driver_register() - register a ccw driver
1900 * @cdriver: driver to be registered
1901 *
1902 * This function is mainly a wrapper around driver_register().
1903 * Returns:
1904 * %0 on success and a negative error value on failure.
1905 */
1906int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct device_driver *drv = &cdriver->driver;
1909
1910 drv->bus = &ccw_bus_type;
1911 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001912 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 return driver_register(drv);
1915}
1916
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001917/**
1918 * ccw_driver_unregister() - deregister a ccw driver
1919 * @cdriver: driver to be deregistered
1920 *
1921 * This function is mainly a wrapper around driver_unregister().
1922 */
1923void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 driver_unregister(&cdriver->driver);
1926}
1927
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001928/* Helper func for qdio. */
1929struct subchannel_id
1930ccw_device_get_subchannel_id(struct ccw_device *cdev)
1931{
1932 struct subchannel *sch;
1933
1934 sch = to_subchannel(cdev->dev.parent);
1935 return sch->schid;
1936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938MODULE_LICENSE("GPL");
1939EXPORT_SYMBOL(ccw_device_set_online);
1940EXPORT_SYMBOL(ccw_device_set_offline);
1941EXPORT_SYMBOL(ccw_driver_register);
1942EXPORT_SYMBOL(ccw_driver_unregister);
1943EXPORT_SYMBOL(get_ccwdev_by_busid);
1944EXPORT_SYMBOL(ccw_bus_type);
1945EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001946EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);