blob: 71b3b73e8ebe77e822c6d3da6c0ad3449d2349cc [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
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200460static void online_store_handle_offline(struct ccw_device *cdev)
461{
462 if (cdev->private->state == DEV_STATE_DISCONNECTED)
463 ccw_device_remove_disconnected(cdev);
464 else if (cdev->drv && cdev->drv->set_offline)
465 ccw_device_set_offline(cdev);
466}
467
468static int online_store_recog_and_online(struct ccw_device *cdev)
469{
470 int ret;
471
472 /* Do device recognition, if needed. */
473 if (cdev->id.cu_type == 0) {
474 ret = ccw_device_recognition(cdev);
475 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200476 CIO_MSG_EVENT(0, "Couldn't start recognition "
477 "for device 0.%x.%04x (ret=%d)\n",
478 cdev->private->dev_id.ssid,
479 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200480 return ret;
481 }
482 wait_event(cdev->private->wait_q,
483 cdev->private->flags.recog_done);
484 }
485 if (cdev->drv && cdev->drv->set_online)
486 ccw_device_set_online(cdev);
487 return 0;
488}
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200489static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200490{
491 int ret;
492
493 ret = online_store_recog_and_online(cdev);
494 if (ret)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200495 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496 if (force && cdev->private->state == DEV_STATE_BOXED) {
497 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200498 if (ret)
499 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200500 if (cdev->id.cu_type == 0)
501 cdev->private->state = DEV_STATE_NOT_OPER;
502 online_store_recog_and_online(cdev);
503 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200504 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200505}
506
507static ssize_t online_store (struct device *dev, struct device_attribute *attr,
508 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200511 int force, ret;
512 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800514 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EAGAIN;
516
517 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
518 atomic_set(&cdev->private->onoff, 0);
519 return -EINVAL;
520 }
521 if (!strncmp(buf, "force\n", count)) {
522 force = 1;
523 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200524 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else {
526 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200527 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200529 if (ret)
530 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200531 switch (i) {
532 case 0:
533 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200534 ret = count;
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);
538 if (!ret)
539 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200540 break;
541 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200542 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200544out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (cdev->drv)
546 module_put(cdev->drv->owner);
547 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400552available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct ccw_device *cdev = to_ccwdev(dev);
555 struct subchannel *sch;
556
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100557 if (ccw_device_is_orphan(cdev))
558 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 switch (cdev->private->state) {
560 case DEV_STATE_BOXED:
561 return sprintf(buf, "boxed\n");
562 case DEV_STATE_DISCONNECTED:
563 case DEV_STATE_DISCONNECTED_SENSE_ID:
564 case DEV_STATE_NOT_OPER:
565 sch = to_subchannel(dev->parent);
566 if (!sch->lpm)
567 return sprintf(buf, "no path\n");
568 else
569 return sprintf(buf, "no device\n");
570 default:
571 /* All other states considered fine. */
572 return sprintf(buf, "good\n");
573 }
574}
575
576static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
577static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
578static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
579static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800580static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static DEVICE_ATTR(availability, 0444, available_show, NULL);
583
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200584static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 &dev_attr_chpids.attr,
586 &dev_attr_pimpampom.attr,
587 NULL,
588};
589
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200590static struct attribute_group io_subchannel_attr_group = {
591 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100592};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594static struct attribute * ccwdev_attrs[] = {
595 &dev_attr_devtype.attr,
596 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800597 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 &dev_attr_online.attr,
599 &dev_attr_cmb_enable.attr,
600 &dev_attr_availability.attr,
601 NULL,
602};
603
604static struct attribute_group ccwdev_attr_group = {
605 .attrs = ccwdev_attrs,
606};
607
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200608static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200609 &ccwdev_attr_group,
610 NULL,
611};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/* this is a simple abstraction for device_register that sets the
614 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200615static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct device *dev = &cdev->dev;
618 int ret;
619
620 dev->bus = &ccw_bus_type;
621
622 if ((ret = device_add(dev)))
623 return ret;
624
625 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ret;
627}
628
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700629struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200630 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631 struct ccw_device * sibling;
632};
633
634static int
635match_devno(struct device * dev, void * data)
636{
Cornelia Huck78964262006-10-11 15:31:38 +0200637 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700638 struct ccw_device * cdev;
639
640 cdev = to_ccwdev(dev);
641 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100642 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200643 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100644 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700645 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700646 return 0;
647}
648
Cornelia Huck78964262006-10-11 15:31:38 +0200649static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
650 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200653 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Cornelia Huck78964262006-10-11 15:31:38 +0200655 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200656 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700657 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700659 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100662static int match_orphan(struct device *dev, void *data)
663{
664 struct ccw_dev_id *dev_id;
665 struct ccw_device *cdev;
666
667 dev_id = data;
668 cdev = to_ccwdev(dev);
669 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
670}
671
672static struct ccw_device *
673get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
674 struct ccw_dev_id *dev_id)
675{
676 struct device *dev;
677
678 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
679 match_orphan);
680
681 return dev ? to_ccwdev(dev) : NULL;
682}
683
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100684void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100686 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct ccw_device *cdev;
688 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100691 priv = container_of(work, struct ccw_device_private, kick_work);
692 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100695 if (test_bit(1, &cdev->private->registered)) {
696 device_release_driver(&cdev->dev);
697 ret = device_attach(&cdev->dev);
698 WARN_ON(ret == -ENODEV);
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static void
703ccw_device_release(struct device *dev)
704{
705 struct ccw_device *cdev;
706
707 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100708 /* Release reference of parent subchannel. */
709 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 kfree(cdev->private);
711 kfree(cdev);
712}
713
Cornelia Huck7674da72006-12-08 15:54:21 +0100714static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
715{
716 struct ccw_device *cdev;
717
718 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
719 if (cdev) {
720 cdev->private = kzalloc(sizeof(struct ccw_device_private),
721 GFP_KERNEL | GFP_DMA);
722 if (cdev->private)
723 return cdev;
724 }
725 kfree(cdev);
726 return ERR_PTR(-ENOMEM);
727}
728
729static int io_subchannel_initialize_dev(struct subchannel *sch,
730 struct ccw_device *cdev)
731{
732 cdev->private->cdev = cdev;
733 atomic_set(&cdev->private->onoff, 0);
734 cdev->dev.parent = &sch->dev;
735 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100736 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200737 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100738 /* Do first half of device_register. */
739 device_initialize(&cdev->dev);
740 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100741 /* Release reference from device_initialize(). */
742 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100743 return -ENODEV;
744 }
745 return 0;
746}
747
748static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
749{
750 struct ccw_device *cdev;
751 int ret;
752
753 cdev = io_subchannel_allocate_dev(sch);
754 if (!IS_ERR(cdev)) {
755 ret = io_subchannel_initialize_dev(sch, cdev);
756 if (ret) {
757 kfree(cdev);
758 cdev = ERR_PTR(ret);
759 }
760 }
761 return cdev;
762}
763
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100764static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
765
766static void sch_attach_device(struct subchannel *sch,
767 struct ccw_device *cdev)
768{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200769 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100770 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100771 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100772 cdev->private->schid = sch->schid;
773 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200774 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100775 spin_unlock_irq(sch->lock);
776}
777
778static void sch_attach_disconnected_device(struct subchannel *sch,
779 struct ccw_device *cdev)
780{
781 struct subchannel *other_sch;
782 int ret;
783
Cornelia Huck6eff2082008-12-25 13:39:07 +0100784 /* Get reference for new parent. */
785 if (!get_device(&sch->dev))
786 return;
787 other_sch = to_subchannel(cdev->dev.parent);
788 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100789 ret = device_move(&cdev->dev, &sch->dev);
790 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200791 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100792 "(ret=%d)!\n", cdev->private->dev_id.ssid,
793 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100794 /* Put reference for new parent. */
795 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100796 return;
797 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100798 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100799 /* No need to keep a subchannel without ccw device around. */
800 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100801 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100802 /* Put reference for old parent. */
803 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100804}
805
806static void sch_attach_orphaned_device(struct subchannel *sch,
807 struct ccw_device *cdev)
808{
809 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100810 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100811
Cornelia Huck6eff2082008-12-25 13:39:07 +0100812 /* Get reference for new parent. */
813 if (!get_device(&sch->dev))
814 return;
815 pseudo_sch = to_subchannel(cdev->dev.parent);
816 /*
817 * Try to move the ccw device to its new subchannel.
818 * Note: device_move() changes cdev->dev.parent
819 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100820 ret = device_move(&cdev->dev, &sch->dev);
821 if (ret) {
822 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
823 "failed (ret=%d)!\n",
824 cdev->private->dev_id.ssid,
825 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100826 /* Put reference for new parent. */
827 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100828 return;
829 }
830 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100831 /* Put reference on pseudo subchannel. */
832 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100833}
834
835static void sch_create_and_recog_new_device(struct subchannel *sch)
836{
837 struct ccw_device *cdev;
838
839 /* Need to allocate a new ccw device. */
840 cdev = io_subchannel_create_ccwdev(sch);
841 if (IS_ERR(cdev)) {
842 /* OK, we did everything we could... */
843 css_sch_device_unregister(sch);
844 return;
845 }
846 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100847 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100848 spin_unlock_irq(sch->lock);
849 /* Start recognition for the new ccw device. */
850 if (io_subchannel_recog(cdev, sch)) {
851 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100852 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100853 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100854 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100855 /* Put reference from io_subchannel_create_ccwdev(). */
856 put_device(&sch->dev);
857 /* Give up initial reference. */
858 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100859 }
860}
861
862
863void ccw_device_move_to_orphanage(struct work_struct *work)
864{
865 struct ccw_device_private *priv;
866 struct ccw_device *cdev;
867 struct ccw_device *replacing_cdev;
868 struct subchannel *sch;
869 int ret;
870 struct channel_subsystem *css;
871 struct ccw_dev_id dev_id;
872
873 priv = container_of(work, struct ccw_device_private, kick_work);
874 cdev = priv->cdev;
875 sch = to_subchannel(cdev->dev.parent);
876 css = to_css(sch->dev.parent);
877 dev_id.devno = sch->schib.pmcw.dev;
878 dev_id.ssid = sch->schid.ssid;
879
Cornelia Huck6eff2082008-12-25 13:39:07 +0100880 /* Increase refcount for pseudo subchannel. */
881 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100882 /*
883 * Move the orphaned ccw device to the orphanage so the replacing
884 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100885 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100886 */
887 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
888 if (ret) {
889 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
890 "(ret=%d)!\n", cdev->private->dev_id.ssid,
891 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100892 /* Decrease refcount for pseudo subchannel again. */
893 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100894 return;
895 }
896 cdev->ccwlock = css->pseudo_subchannel->lock;
897 /*
898 * Search for the replacing ccw device
899 * - among the disconnected devices
900 * - in the orphanage
901 */
902 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
903 if (replacing_cdev) {
904 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100905 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100906 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100907 /* Release reference of subchannel from old cdev. */
908 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100909 return;
910 }
911 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
912 if (replacing_cdev) {
913 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100914 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100915 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100916 /* Release reference of subchannel from old cdev. */
917 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100918 return;
919 }
920 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100921 /* Release reference of subchannel from old cdev. */
922 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Register recognized device.
927 */
928static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100929io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100931 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct ccw_device *cdev;
933 struct subchannel *sch;
934 int ret;
935 unsigned long flags;
936
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100937 priv = container_of(work, struct ccw_device_private, kick_work);
938 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100940 /*
941 * Check if subchannel is still registered. It may have become
942 * unregistered if a machine check hit us after finishing
943 * device recognition but before the register work could be
944 * queued.
945 */
946 if (!device_is_registered(&sch->dev))
947 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200948 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100949 /*
950 * io_subchannel_register() will also be called after device
951 * recognition has been done for a boxed device (which will already
952 * be registered). We need to reprobe since we may now have sense id
953 * information.
954 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100955 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100956 if (!cdev->drv) {
957 ret = device_reprobe(&cdev->dev);
958 if (ret)
959 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200960 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200961 " %d for 0.%x.%04x\n", ret,
962 cdev->private->dev_id.ssid,
963 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
966 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700967 /*
968 * Now we know this subchannel will stay, we can throw
969 * our delayed uevent.
970 */
971 sch->dev.uevent_suppress = 0;
972 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* make it known to the system */
974 ret = ccw_device_register(cdev);
975 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200976 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
977 cdev->private->dev_id.ssid,
978 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100979 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100980 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100981 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100982 /* Release initial device reference. */
983 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100984 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986out:
987 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100989out_err:
990 /* Release reference for workqueue processing. */
991 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (atomic_dec_and_test(&ccw_device_init_count))
993 wake_up(&ccw_device_init_wq);
994}
995
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200996static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100998 struct ccw_device_private *priv;
999 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct subchannel *sch;
1001
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001002 priv = container_of(work, struct ccw_device_private, kick_work);
1003 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001004 /* Get subchannel reference for local processing. */
1005 if (!get_device(cdev->dev.parent))
1006 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001008 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001010 sch->config.intparm = 0;
1011 cio_commit_config(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001012 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001014 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 put_device(&sch->dev);
1016}
1017
1018/*
1019 * subchannel recognition done. Called from the state machine.
1020 */
1021void
1022io_subchannel_recog_done(struct ccw_device *cdev)
1023{
1024 struct subchannel *sch;
1025
1026 if (css_init_done == 0) {
1027 cdev->private->flags.recog_done = 1;
1028 return;
1029 }
1030 switch (cdev->private->state) {
1031 case DEV_STATE_NOT_OPER:
1032 cdev->private->flags.recog_done = 1;
1033 /* Remove device found not operational. */
1034 if (!get_device(&cdev->dev))
1035 break;
1036 sch = to_subchannel(cdev->dev.parent);
1037 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001038 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 queue_work(slow_path_wq, &cdev->private->kick_work);
1040 if (atomic_dec_and_test(&ccw_device_init_count))
1041 wake_up(&ccw_device_init_wq);
1042 break;
1043 case DEV_STATE_BOXED:
1044 /* Device did not respond in time. */
1045 case DEV_STATE_OFFLINE:
1046 /*
1047 * We can't register the device in interrupt context so
1048 * we schedule a work item.
1049 */
1050 if (!get_device(&cdev->dev))
1051 break;
1052 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001053 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 queue_work(slow_path_wq, &cdev->private->kick_work);
1055 break;
1056 }
1057}
1058
1059static int
1060io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1061{
1062 int rc;
1063 struct ccw_device_private *priv;
1064
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001065 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001066 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Init private data. */
1069 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001070 priv->dev_id.devno = sch->schib.pmcw.dev;
1071 priv->dev_id.ssid = sch->schid.ssid;
1072 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 priv->state = DEV_STATE_NOT_OPER;
1074 INIT_LIST_HEAD(&priv->cmb_list);
1075 init_waitqueue_head(&priv->wait_q);
1076 init_timer(&priv->timer);
1077
1078 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001079 if (cio_is_console(sch->schid))
1080 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1081 else
1082 dev_set_name(&cdev->dev, "0.%x.%04x",
1083 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Increase counter of devices currently in recognition. */
1086 atomic_inc(&ccw_device_init_count);
1087
1088 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001089 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001091 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (rc) {
1093 if (atomic_dec_and_test(&ccw_device_init_count))
1094 wake_up(&ccw_device_init_wq);
1095 }
1096 return rc;
1097}
1098
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001099static void ccw_device_move_to_sch(struct work_struct *work)
1100{
1101 struct ccw_device_private *priv;
1102 int rc;
1103 struct subchannel *sch;
1104 struct ccw_device *cdev;
1105 struct subchannel *former_parent;
1106
1107 priv = container_of(work, struct ccw_device_private, kick_work);
1108 sch = priv->sch;
1109 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001110 former_parent = to_subchannel(cdev->dev.parent);
1111 /* Get reference for new parent. */
1112 if (!get_device(&sch->dev))
1113 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001114 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001115 /*
1116 * Try to move the ccw device to its new subchannel.
1117 * Note: device_move() changes cdev->dev.parent
1118 */
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001119 rc = device_move(&cdev->dev, &sch->dev);
1120 mutex_unlock(&sch->reg_mutex);
1121 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001122 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001123 "0.%x.%04x failed (ret=%d)!\n",
1124 cdev->private->dev_id.ssid,
1125 cdev->private->dev_id.devno, sch->schid.ssid,
1126 sch->schid.sch_no, rc);
1127 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001128 /* Put reference for new parent again. */
1129 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001130 goto out;
1131 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001132 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001133 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001134 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001135 spin_unlock_irq(former_parent->lock);
1136 css_sch_device_unregister(former_parent);
1137 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001138 former_parent->config.intparm = 0;
1139 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001140 }
1141 sch_attach_device(sch, cdev);
1142out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001143 /* Put reference for old parent. */
1144 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001145 put_device(&cdev->dev);
1146}
1147
Cornelia Huck602b20f2008-01-26 14:10:39 +01001148static void io_subchannel_irq(struct subchannel *sch)
1149{
1150 struct ccw_device *cdev;
1151
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001152 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001153
1154 CIO_TRACE_EVENT(3, "IRQ");
Kay Sievers2a0217d2008-10-10 21:33:09 +02001155 CIO_TRACE_EVENT(3, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001156 if (cdev)
1157 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1158}
1159
Sebastian Ott13952ec2008-12-25 13:39:13 +01001160void io_subchannel_init_config(struct subchannel *sch)
1161{
1162 memset(&sch->config, 0, sizeof(sch->config));
1163 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001164 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1165 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001166 sch->config.mp = 1;
1167}
1168
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001169static void io_subchannel_init_fields(struct subchannel *sch)
1170{
1171 if (cio_is_console(sch->schid))
1172 sch->opm = 0xff;
1173 else
1174 sch->opm = chp_get_sch_opm(sch);
1175 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001176 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001177
1178 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1179 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1180 sch->schib.pmcw.dev, sch->schid.ssid,
1181 sch->schid.sch_no, sch->schib.pmcw.pim,
1182 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001183
1184 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001185}
1186
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001187static void io_subchannel_do_unreg(struct work_struct *work)
1188{
1189 struct subchannel *sch;
1190
1191 sch = container_of(work, struct subchannel, work);
1192 css_sch_device_unregister(sch);
1193 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001194 sch->config.intparm = 0;
1195 cio_commit_config(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001196 put_device(&sch->dev);
1197}
1198
1199/* Schedule unregister if we have no cdev. */
1200static void io_subchannel_schedule_removal(struct subchannel *sch)
1201{
1202 get_device(&sch->dev);
1203 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1204 queue_work(slow_path_wq, &sch->work);
1205}
1206
1207/*
1208 * Note: We always return 0 so that we bind to the device even on error.
1209 * This is needed so that our remove function is called on unregister.
1210 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001211static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct ccw_device *cdev;
1214 int rc;
1215 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001216 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001218 cdev = sch_get_cdev(sch);
1219 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001220 rc = sysfs_create_group(&sch->dev.kobj,
1221 &io_subchannel_attr_group);
1222 if (rc)
1223 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1224 "attributes for subchannel "
1225 "0.%x.%04x (rc=%d)\n",
1226 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001229 * Throw the delayed uevent for the subchannel, register
1230 * the ccw_device and exit. This happens for all early
1231 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001233 sch->dev.uevent_suppress = 0;
1234 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001235 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 device_initialize(&cdev->dev);
1237 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001240 * the reference count needs to be corrected since we
1241 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
1243 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1244 cdev->private->state != DEV_STATE_OFFLINE &&
1245 cdev->private->state != DEV_STATE_BOXED)
1246 get_device(&cdev->dev);
1247 return 0;
1248 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001249 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001250 rc = cio_commit_config(sch);
1251 if (rc)
1252 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001253 rc = sysfs_create_group(&sch->dev.kobj,
1254 &io_subchannel_attr_group);
1255 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001256 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001257 /* Allocate I/O subchannel private data. */
1258 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1259 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001260 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001261 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001262 /*
1263 * First check if a fitting device may be found amongst the
1264 * disconnected devices or in the orphanage.
1265 */
1266 dev_id.devno = sch->schib.pmcw.dev;
1267 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001268 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1269 if (!cdev)
1270 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1271 &dev_id);
1272 if (cdev) {
1273 /*
1274 * Schedule moving the device until when we have a registered
1275 * subchannel to move to and succeed the probe. We can
1276 * unregister later again, when the probe is through.
1277 */
1278 cdev->private->sch = sch;
1279 PREPARE_WORK(&cdev->private->kick_work,
1280 ccw_device_move_to_sch);
1281 queue_work(slow_path_wq, &cdev->private->kick_work);
1282 return 0;
1283 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001284 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001285 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001286 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001287 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001289 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001290 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001291 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001293 return 0;
1294out_err:
1295 kfree(sch->private);
1296 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001297out_schedule:
1298 io_subchannel_schedule_removal(sch);
1299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001303io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct ccw_device *cdev;
1306 unsigned long flags;
1307
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001308 cdev = sch_get_cdev(sch);
1309 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* Set ccw device to not operational and drop reference. */
1312 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001313 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 cdev->private->state = DEV_STATE_NOT_OPER;
1315 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001316 ccw_device_unregister(cdev);
1317 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001318 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001319 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321}
1322
Cornelia Huck602b20f2008-01-26 14:10:39 +01001323static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct ccw_device *cdev;
1326
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001327 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!cdev)
1329 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001330 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Cornelia Huck602b20f2008-01-26 14:10:39 +01001333static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct ccw_device *cdev;
1336
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001337 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (cdev)
1339 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1340}
1341
Cornelia Huckc820de32008-07-14 09:58:45 +02001342static int check_for_io_on_path(struct subchannel *sch, int mask)
1343{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001344 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001345 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001346 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001347 return 1;
1348 return 0;
1349}
1350
1351static void terminate_internal_io(struct subchannel *sch,
1352 struct ccw_device *cdev)
1353{
1354 if (cio_clear(sch)) {
1355 /* Recheck device in case clear failed. */
1356 sch->lpm = 0;
1357 if (cdev->online)
1358 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1359 else
1360 css_schedule_eval(sch->schid);
1361 return;
1362 }
1363 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1364 /* Request retry of internal operation. */
1365 cdev->private->flags.intretry = 1;
1366 /* Call handler. */
1367 if (cdev->handler)
1368 cdev->handler(cdev, cdev->private->intparm,
1369 ERR_PTR(-EIO));
1370}
1371
1372static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct ccw_device *cdev;
1375
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001376 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (!cdev)
1378 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001379 if (check_for_io_on_path(sch, mask)) {
1380 if (cdev->private->state == DEV_STATE_ONLINE)
1381 ccw_device_kill_io(cdev);
1382 else {
1383 terminate_internal_io(sch, cdev);
1384 /* Re-start path verification. */
1385 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1386 }
1387 } else
1388 /* trigger path verification. */
1389 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1390
1391}
1392
Cornelia Huck99611f82008-07-14 09:59:02 +02001393static int io_subchannel_chp_event(struct subchannel *sch,
1394 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001395{
1396 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001397
Cornelia Huck99611f82008-07-14 09:59:02 +02001398 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001399 if (!mask)
1400 return 0;
1401 switch (event) {
1402 case CHP_VARY_OFF:
1403 sch->opm &= ~mask;
1404 sch->lpm &= ~mask;
1405 io_subchannel_terminate_path(sch, mask);
1406 break;
1407 case CHP_VARY_ON:
1408 sch->opm |= mask;
1409 sch->lpm |= mask;
1410 io_subchannel_verify(sch);
1411 break;
1412 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001413 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001414 return -ENODEV;
1415 io_subchannel_terminate_path(sch, mask);
1416 break;
1417 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001418 if (cio_update_schib(sch))
1419 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001420 sch->lpm |= mask & sch->opm;
1421 io_subchannel_verify(sch);
1422 break;
1423 }
1424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
1427static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001428io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct ccw_device *cdev;
1431 int ret;
1432
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001433 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001435 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return;
1437 if (!sch->schib.pmcw.ena)
1438 /* Nothing to do. */
1439 return;
1440 ret = cio_disable_subchannel(sch);
1441 if (ret != -EBUSY)
1442 /* Subchannel is disabled, we're done. */
1443 return;
1444 cdev->private->state = DEV_STATE_QUIESCE;
1445 if (cdev->handler)
1446 cdev->handler(cdev, cdev->private->intparm,
1447 ERR_PTR(-EIO));
1448 ret = ccw_device_cancel_halt_clear(cdev);
1449 if (ret == -EBUSY) {
1450 ccw_device_set_timeout(cdev, HZ/10);
1451 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1452 }
1453 cio_disable_subchannel(sch);
1454}
1455
Cornelia Huckc820de32008-07-14 09:58:45 +02001456static int io_subchannel_get_status(struct subchannel *sch)
1457{
1458 struct schib schib;
1459
1460 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1461 return CIO_GONE;
1462 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1463 return CIO_REVALIDATE;
1464 if (!sch->lpm)
1465 return CIO_NO_PATH;
1466 return CIO_OPER;
1467}
1468
1469static int device_is_disconnected(struct ccw_device *cdev)
1470{
1471 if (!cdev)
1472 return 0;
1473 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1474 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1475}
1476
1477static int recovery_check(struct device *dev, void *data)
1478{
1479 struct ccw_device *cdev = to_ccwdev(dev);
1480 int *redo = data;
1481
1482 spin_lock_irq(cdev->ccwlock);
1483 switch (cdev->private->state) {
1484 case DEV_STATE_DISCONNECTED:
1485 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1486 cdev->private->dev_id.ssid,
1487 cdev->private->dev_id.devno);
1488 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1489 *redo = 1;
1490 break;
1491 case DEV_STATE_DISCONNECTED_SENSE_ID:
1492 *redo = 1;
1493 break;
1494 }
1495 spin_unlock_irq(cdev->ccwlock);
1496
1497 return 0;
1498}
1499
1500static void recovery_work_func(struct work_struct *unused)
1501{
1502 int redo = 0;
1503
1504 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1505 if (redo) {
1506 spin_lock_irq(&recovery_lock);
1507 if (!timer_pending(&recovery_timer)) {
1508 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1509 recovery_phase++;
1510 mod_timer(&recovery_timer, jiffies +
1511 recovery_delay[recovery_phase] * HZ);
1512 }
1513 spin_unlock_irq(&recovery_lock);
1514 } else
1515 CIO_MSG_EVENT(4, "recovery: end\n");
1516}
1517
1518static DECLARE_WORK(recovery_work, recovery_work_func);
1519
1520static void recovery_func(unsigned long data)
1521{
1522 /*
1523 * We can't do our recovery in softirq context and it's not
1524 * performance critical, so we schedule it.
1525 */
1526 schedule_work(&recovery_work);
1527}
1528
1529static void ccw_device_schedule_recovery(void)
1530{
1531 unsigned long flags;
1532
1533 CIO_MSG_EVENT(4, "recovery: schedule\n");
1534 spin_lock_irqsave(&recovery_lock, flags);
1535 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1536 recovery_phase = 0;
1537 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1538 }
1539 spin_unlock_irqrestore(&recovery_lock, flags);
1540}
1541
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001542static int purge_fn(struct device *dev, void *data)
1543{
1544 struct ccw_device *cdev = to_ccwdev(dev);
1545 struct ccw_device_private *priv = cdev->private;
1546 int unreg;
1547
1548 spin_lock_irq(cdev->ccwlock);
1549 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1550 (priv->state == DEV_STATE_OFFLINE);
1551 spin_unlock_irq(cdev->ccwlock);
1552 if (!unreg)
1553 goto out;
1554 if (!get_device(&cdev->dev))
1555 goto out;
1556 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1557 priv->dev_id.devno);
1558 PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
1559 queue_work(slow_path_wq, &cdev->private->kick_work);
1560
1561out:
1562 /* Abort loop in case of pending signal. */
1563 if (signal_pending(current))
1564 return -EINTR;
1565
1566 return 0;
1567}
1568
1569/**
1570 * ccw_purge_blacklisted - purge unused, blacklisted devices
1571 *
1572 * Unregister all ccw devices that are offline and on the blacklist.
1573 */
1574int ccw_purge_blacklisted(void)
1575{
1576 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1577 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1578 return 0;
1579}
1580
Cornelia Huckc820de32008-07-14 09:58:45 +02001581static void device_set_disconnected(struct ccw_device *cdev)
1582{
1583 if (!cdev)
1584 return;
1585 ccw_device_set_timeout(cdev, 0);
1586 cdev->private->flags.fake_irb = 0;
1587 cdev->private->state = DEV_STATE_DISCONNECTED;
1588 if (cdev->online)
1589 ccw_device_schedule_recovery();
1590}
1591
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001592void ccw_device_set_notoper(struct ccw_device *cdev)
1593{
1594 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1595
1596 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001597 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001598 ccw_device_set_timeout(cdev, 0);
1599 cio_disable_subchannel(sch);
1600 cdev->private->state = DEV_STATE_NOT_OPER;
1601}
1602
Cornelia Huckc820de32008-07-14 09:58:45 +02001603static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1604{
1605 int event, ret, disc;
1606 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001607 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001608 struct ccw_device *cdev;
1609
1610 spin_lock_irqsave(sch->lock, flags);
1611 cdev = sch_get_cdev(sch);
1612 disc = device_is_disconnected(cdev);
1613 if (disc && slow) {
1614 /* Disconnected devices are evaluated directly only.*/
1615 spin_unlock_irqrestore(sch->lock, flags);
1616 return 0;
1617 }
1618 /* No interrupt after machine check - kill pending timers. */
1619 if (cdev)
1620 ccw_device_set_timeout(cdev, 0);
1621 if (!disc && !slow) {
1622 /* Non-disconnected devices are evaluated on the slow path. */
1623 spin_unlock_irqrestore(sch->lock, flags);
1624 return -EAGAIN;
1625 }
1626 event = io_subchannel_get_status(sch);
1627 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1628 sch->schid.ssid, sch->schid.sch_no, event,
1629 disc ? "disconnected" : "normal",
1630 slow ? "slow" : "fast");
1631 /* Analyze subchannel status. */
1632 action = NONE;
1633 switch (event) {
1634 case CIO_NO_PATH:
1635 if (disc) {
1636 /* Check if paths have become available. */
1637 action = REPROBE;
1638 break;
1639 }
1640 /* fall through */
1641 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001642 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001643 if (io_subchannel_notify(sch, event))
1644 action = DISC;
1645 else
1646 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001647 break;
1648 case CIO_REVALIDATE:
1649 /* Device will be removed, so no notify necessary. */
1650 if (disc)
1651 /* Reprobe because immediate unregister might block. */
1652 action = REPROBE;
1653 else
1654 action = UNREGISTER_PROBE;
1655 break;
1656 case CIO_OPER:
1657 if (disc)
1658 /* Get device operational again. */
1659 action = REPROBE;
1660 break;
1661 }
1662 /* Perform action. */
1663 ret = 0;
1664 switch (action) {
1665 case UNREGISTER:
1666 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001667 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001668 /* Unregister device (will use subchannel lock). */
1669 spin_unlock_irqrestore(sch->lock, flags);
1670 css_sch_device_unregister(sch);
1671 spin_lock_irqsave(sch->lock, flags);
1672
1673 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001674 sch->config.intparm = 0;
1675 cio_commit_config(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001676 break;
1677 case REPROBE:
1678 ccw_device_trigger_reprobe(cdev);
1679 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001680 case DISC:
1681 device_set_disconnected(cdev);
1682 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001683 default:
1684 break;
1685 }
1686 spin_unlock_irqrestore(sch->lock, flags);
1687 /* Probe if necessary. */
1688 if (action == UNREGISTER_PROBE)
1689 ret = css_probe_device(sch->schid);
1690
1691 return ret;
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694#ifdef CONFIG_CCW_CONSOLE
1695static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001696static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697static struct ccw_device_private console_private;
1698static int console_cdev_in_use;
1699
Cornelia Huck2ec22982006-12-08 15:54:26 +01001700static DEFINE_SPINLOCK(ccw_console_lock);
1701
1702spinlock_t * cio_get_console_lock(void)
1703{
1704 return &ccw_console_lock;
1705}
1706
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001707static int ccw_device_console_enable(struct ccw_device *cdev,
1708 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 int rc;
1711
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001712 /* Attach subchannel private data. */
1713 sch->private = cio_get_console_priv();
1714 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001715 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001716 rc = cio_commit_config(sch);
1717 if (rc)
1718 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001719 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001721 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 rc = io_subchannel_recog(cdev, sch);
1723 if (rc)
1724 return rc;
1725
1726 /* Now wait for the async. recognition to come to an end. */
1727 spin_lock_irq(cdev->ccwlock);
1728 while (!dev_fsm_final_state(cdev))
1729 wait_cons_dev();
1730 rc = -EIO;
1731 if (cdev->private->state != DEV_STATE_OFFLINE)
1732 goto out_unlock;
1733 ccw_device_online(cdev);
1734 while (!dev_fsm_final_state(cdev))
1735 wait_cons_dev();
1736 if (cdev->private->state != DEV_STATE_ONLINE)
1737 goto out_unlock;
1738 rc = 0;
1739out_unlock:
1740 spin_unlock_irq(cdev->ccwlock);
1741 return 0;
1742}
1743
1744struct ccw_device *
1745ccw_device_probe_console(void)
1746{
1747 struct subchannel *sch;
1748 int ret;
1749
1750 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001751 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 sch = cio_probe_console();
1753 if (IS_ERR(sch)) {
1754 console_cdev_in_use = 0;
1755 return (void *) sch;
1756 }
1757 memset(&console_cdev, 0, sizeof(struct ccw_device));
1758 memset(&console_private, 0, sizeof(struct ccw_device_private));
1759 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001760 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ret = ccw_device_console_enable(&console_cdev, sch);
1762 if (ret) {
1763 cio_release_console();
1764 console_cdev_in_use = 0;
1765 return ERR_PTR(ret);
1766 }
1767 console_cdev.online = 1;
1768 return &console_cdev;
1769}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001770
1771
1772const char *cio_get_console_cdev_name(struct subchannel *sch)
1773{
1774 snprintf(console_cdev_name, 10, "0.%x.%04x",
1775 sch->schid.ssid, sch->schib.pmcw.dev);
1776 return (const char *)console_cdev_name;
1777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778#endif
1779
1780/*
1781 * get ccw_device matching the busid, but only if owned by cdrv
1782 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001783static int
1784__ccwdev_check_busid(struct device *dev, void *id)
1785{
1786 char *bus_id;
1787
Cornelia Huck12975ae2006-10-11 15:31:47 +02001788 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001789
Kay Sievers98df67b2008-12-25 13:38:55 +01001790 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001791}
1792
1793
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001794/**
1795 * get_ccwdev_by_busid() - obtain device from a bus id
1796 * @cdrv: driver the device is owned by
1797 * @bus_id: bus id of the device to be searched
1798 *
1799 * This function searches all devices owned by @cdrv for a device with a bus
1800 * id matching @bus_id.
1801 * Returns:
1802 * If a match is found, its reference count of the found device is increased
1803 * and it is returned; else %NULL is returned.
1804 */
1805struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1806 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001808 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct device_driver *drv;
1810
1811 drv = get_driver(&cdrv->driver);
1812 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001813 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001815 dev = driver_find_device(drv, NULL, (void *)bus_id,
1816 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 put_driver(drv);
1818
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001819 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822/************************** device driver handling ************************/
1823
1824/* This is the implementation of the ccw_driver class. The probe, remove
1825 * and release methods are initially very similar to the device_driver
1826 * implementations, with the difference that they have ccw_device
1827 * arguments.
1828 *
1829 * A ccw driver also contains the information that is needed for
1830 * device matching.
1831 */
1832static int
1833ccw_device_probe (struct device *dev)
1834{
1835 struct ccw_device *cdev = to_ccwdev(dev);
1836 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1837 int ret;
1838
1839 cdev->drv = cdrv; /* to let the driver call _set_online */
1840
1841 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1842
1843 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001844 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return ret;
1846 }
1847
1848 return 0;
1849}
1850
1851static int
1852ccw_device_remove (struct device *dev)
1853{
1854 struct ccw_device *cdev = to_ccwdev(dev);
1855 struct ccw_driver *cdrv = cdev->drv;
1856 int ret;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (cdrv->remove)
1859 cdrv->remove(cdev);
1860 if (cdev->online) {
1861 cdev->online = 0;
1862 spin_lock_irq(cdev->ccwlock);
1863 ret = ccw_device_offline(cdev);
1864 spin_unlock_irq(cdev->ccwlock);
1865 if (ret == 0)
1866 wait_event(cdev->private->wait_q,
1867 dev_fsm_final_state(cdev));
1868 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001869 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001870 "device 0.%x.%04x\n",
1871 ret, cdev->private->dev_id.ssid,
1872 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001873 /* Give up reference obtained in ccw_device_set_online(). */
1874 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001877 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 0;
1879}
1880
Cornelia Huck958974fb2007-10-12 16:11:21 +02001881static void ccw_device_shutdown(struct device *dev)
1882{
1883 struct ccw_device *cdev;
1884
1885 cdev = to_ccwdev(dev);
1886 if (cdev->drv && cdev->drv->shutdown)
1887 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001888 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001889}
1890
Cornelia Huck8bbace72006-01-11 10:56:22 +01001891struct bus_type ccw_bus_type = {
1892 .name = "ccw",
1893 .match = ccw_bus_match,
1894 .uevent = ccw_uevent,
1895 .probe = ccw_device_probe,
1896 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001897 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001898};
1899
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001900/**
1901 * ccw_driver_register() - register a ccw driver
1902 * @cdriver: driver to be registered
1903 *
1904 * This function is mainly a wrapper around driver_register().
1905 * Returns:
1906 * %0 on success and a negative error value on failure.
1907 */
1908int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct device_driver *drv = &cdriver->driver;
1911
1912 drv->bus = &ccw_bus_type;
1913 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001914 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 return driver_register(drv);
1917}
1918
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001919/**
1920 * ccw_driver_unregister() - deregister a ccw driver
1921 * @cdriver: driver to be deregistered
1922 *
1923 * This function is mainly a wrapper around driver_unregister().
1924 */
1925void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 driver_unregister(&cdriver->driver);
1928}
1929
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001930/* Helper func for qdio. */
1931struct subchannel_id
1932ccw_device_get_subchannel_id(struct ccw_device *cdev)
1933{
1934 struct subchannel *sch;
1935
1936 sch = to_subchannel(cdev->dev.parent);
1937 return sch->schid;
1938}
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940MODULE_LICENSE("GPL");
1941EXPORT_SYMBOL(ccw_device_set_online);
1942EXPORT_SYMBOL(ccw_device_set_offline);
1943EXPORT_SYMBOL(ccw_driver_register);
1944EXPORT_SYMBOL(ccw_driver_unregister);
1945EXPORT_SYMBOL(get_ccwdev_by_busid);
1946EXPORT_SYMBOL(ccw_bus_type);
1947EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001948EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);