blob: 345a61f45a5a7d6a398ba2418bab27013d891ac2 [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 Huck93a27592009-06-16 10:30:23 +0200141static int io_subchannel_prepare(struct subchannel *sch)
142{
143 struct ccw_device *cdev;
144 /*
145 * Don't allow suspend while a ccw device registration
146 * is still outstanding.
147 */
148 cdev = sch_get_cdev(sch);
149 if (cdev && !device_is_registered(&cdev->dev))
150 return -EAGAIN;
151 return 0;
152}
153
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200154static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100155 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200156 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100157 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200159 .sch_event = io_subchannel_sch_event,
160 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100161 .probe = io_subchannel_probe,
162 .remove = io_subchannel_remove,
163 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200164 .prepare = io_subchannel_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct workqueue_struct *ccw_device_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200168wait_queue_head_t ccw_device_init_wq;
169atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100171static void recovery_func(unsigned long data);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int __init
174init_ccw_bus_type (void)
175{
176 int ret;
177
178 init_waitqueue_head(&ccw_device_init_wq);
179 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100180 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 ccw_device_work = create_singlethread_workqueue("cio");
183 if (!ccw_device_work)
184 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 slow_path_wq = create_singlethread_workqueue("kslowcrw");
186 if (!slow_path_wq) {
187 ret = -ENOMEM; /* FIXME: better errno ? */
188 goto out_err;
189 }
190 if ((ret = bus_register (&ccw_bus_type)))
191 goto out_err;
192
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100193 ret = css_driver_register(&io_subchannel_driver);
194 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out_err;
196
197 wait_event(ccw_device_init_wq,
198 atomic_read(&ccw_device_init_count) == 0);
199 flush_workqueue(ccw_device_work);
200 return 0;
201out_err:
202 if (ccw_device_work)
203 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (slow_path_wq)
205 destroy_workqueue(slow_path_wq);
206 return ret;
207}
208
209static void __exit
210cleanup_ccw_bus_type (void)
211{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100212 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 destroy_workqueue(ccw_device_work);
215}
216
217subsys_initcall(init_ccw_bus_type);
218module_exit(cleanup_ccw_bus_type);
219
220/************************ device handling **************************/
221
222/*
223 * A ccw_device has some interfaces in sysfs in addition to the
224 * standard ones.
225 * The following entries are designed to export the information which
226 * resided in 2.4 in /proc/subchannels. Subchannel and device number
227 * are obvious, so they don't have an entry :)
228 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
229 */
230static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400231chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200234 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ssize_t ret = 0;
236 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200237 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200239 for (chp = 0; chp < 8; chp++) {
240 mask = 0x80 >> chp;
241 if (ssd->path_mask & mask)
242 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
243 else
244 ret += sprintf(buf + ret, "00 ");
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ret += sprintf (buf+ret, "\n");
247 return min((ssize_t)PAGE_SIZE, ret);
248}
249
250static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400251pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct subchannel *sch = to_subchannel(dev);
254 struct pmcw *pmcw = &sch->schib.pmcw;
255
256 return sprintf (buf, "%02x %02x %02x\n",
257 pmcw->pim, pmcw->pam, pmcw->pom);
258}
259
260static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400261devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
265
266 if (id->dev_type != 0)
267 return sprintf(buf, "%04x/%02x\n",
268 id->dev_type, id->dev_model);
269 else
270 return sprintf(buf, "n/a\n");
271}
272
273static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400274cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct ccw_device *cdev = to_ccwdev(dev);
277 struct ccw_device_id *id = &(cdev->id);
278
279 return sprintf(buf, "%04x/%02x\n",
280 id->cu_type, id->cu_model);
281}
282
283static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800284modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
285{
286 struct ccw_device *cdev = to_ccwdev(dev);
287 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200288 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800289
Cornelia Huck086a6c62007-07-17 13:36:08 +0200290 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200291
292 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800293}
294
295static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400296online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct ccw_device *cdev = to_ccwdev(dev);
299
300 return sprintf(buf, cdev->online ? "1\n" : "0\n");
301}
302
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100303int ccw_device_is_orphan(struct ccw_device *cdev)
304{
305 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
306}
307
Cornelia Huckef995162007-04-27 16:01:39 +0200308static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100309{
Sebastian Ott3b554a12009-09-11 10:28:26 +0200310 if (test_and_clear_bit(1, &cdev->private->registered)) {
Cornelia Huckef995162007-04-27 16:01:39 +0200311 device_del(&cdev->dev);
Sebastian Ott3b554a12009-09-11 10:28:26 +0200312 /* Release reference from device_initialize(). */
313 put_device(&cdev->dev);
314 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100315}
316
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200317static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200318{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200319 struct ccw_device_private *priv;
320 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200321
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200322 priv = container_of(work, struct ccw_device_private, kick_work);
323 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200324 ccw_device_unregister(cdev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200325 /* Release cdev reference for workqueue processing. */
326 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200327}
328
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200329static void
330ccw_device_remove_disconnected(struct ccw_device *cdev)
331{
332 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200333
334 /*
335 * Forced offline in disconnected state means
336 * 'throw away device'.
337 */
338 if (ccw_device_is_orphan(cdev)) {
339 /*
340 * Deregister ccw device.
341 * Unfortunately, we cannot do this directly from the
342 * attribute method.
343 */
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +0200344 /* Get cdev reference for workqueue processing. */
345 if (!get_device(&cdev->dev))
346 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200347 spin_lock_irqsave(cdev->ccwlock, flags);
348 cdev->private->state = DEV_STATE_NOT_OPER;
349 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200350 PREPARE_WORK(&cdev->private->kick_work,
351 ccw_device_remove_orphan_cb);
Sebastian Ottc4621a62009-03-31 19:16:04 +0200352 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200353 } else
354 /* Deregister subchannel, which will kill the ccw device. */
Sebastian Ottc4621a62009-03-31 19:16:04 +0200355 ccw_device_schedule_sch_unregister(cdev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200356}
357
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200358/**
359 * ccw_device_set_offline() - disable a ccw device for I/O
360 * @cdev: target ccw device
361 *
362 * This function calls the driver's set_offline() function for @cdev, if
363 * given, and then disables @cdev.
364 * Returns:
365 * %0 on success and a negative error value on failure.
366 * Context:
367 * enabled, ccw device lock not held
368 */
369int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 int ret;
372
373 if (!cdev)
374 return -ENODEV;
375 if (!cdev->online || !cdev->drv)
376 return -EINVAL;
377
378 if (cdev->drv->set_offline) {
379 ret = cdev->drv->set_offline(cdev);
380 if (ret != 0)
381 return ret;
382 }
383 cdev->online = 0;
384 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200385 /* Wait until a final state or DISCONNECTED is reached */
386 while (!dev_fsm_final_state(cdev) &&
387 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200389 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
390 cdev->private->state == DEV_STATE_DISCONNECTED));
391 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200393 ret = ccw_device_offline(cdev);
394 if (ret)
395 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200397 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
398 cdev->private->state == DEV_STATE_DISCONNECTED));
399 /* Give up reference from ccw_device_set_online(). */
400 put_device(&cdev->dev);
401 return 0;
402
403error:
404 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
405 ret, cdev->private->dev_id.ssid,
406 cdev->private->dev_id.devno);
407 cdev->private->state = DEV_STATE_OFFLINE;
408 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
409 spin_unlock_irq(cdev->ccwlock);
410 /* Give up reference from ccw_device_set_online(). */
411 put_device(&cdev->dev);
412 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200415/**
416 * ccw_device_set_online() - enable a ccw device for I/O
417 * @cdev: target ccw device
418 *
419 * This function first enables @cdev and then calls the driver's set_online()
420 * function for @cdev, if given. If set_online() returns an error, @cdev is
421 * disabled again.
422 * Returns:
423 * %0 on success and a negative error value on failure.
424 * Context:
425 * enabled, ccw device lock not held
426 */
427int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200430 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!cdev)
433 return -ENODEV;
434 if (cdev->online || !cdev->drv)
435 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100436 /* Hold on to an extra reference while device is online. */
437 if (!get_device(&cdev->dev))
438 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 spin_lock_irq(cdev->ccwlock);
441 ret = ccw_device_online(cdev);
442 spin_unlock_irq(cdev->ccwlock);
443 if (ret == 0)
444 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
445 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200446 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200447 "device 0.%x.%04x\n",
448 ret, cdev->private->dev_id.ssid,
449 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100450 /* Give up online reference since onlining failed. */
451 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ret;
453 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200454 spin_lock_irq(cdev->ccwlock);
455 /* Check if online processing was successful */
456 if ((cdev->private->state != DEV_STATE_ONLINE) &&
457 (cdev->private->state != DEV_STATE_W4SENSE)) {
458 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100459 /* Give up online reference since onlining failed. */
460 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200464 if (cdev->drv->set_online)
465 ret = cdev->drv->set_online(cdev);
466 if (ret)
467 goto rollback;
468 cdev->online = 1;
469 return 0;
470
471rollback:
472 spin_lock_irq(cdev->ccwlock);
473 /* Wait until a final state or DISCONNECTED is reached */
474 while (!dev_fsm_final_state(cdev) &&
475 cdev->private->state != DEV_STATE_DISCONNECTED) {
476 spin_unlock_irq(cdev->ccwlock);
477 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
478 cdev->private->state == DEV_STATE_DISCONNECTED));
479 spin_lock_irq(cdev->ccwlock);
480 }
481 ret2 = ccw_device_offline(cdev);
482 if (ret2)
483 goto error;
484 spin_unlock_irq(cdev->ccwlock);
485 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
486 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100487 /* Give up online reference since onlining failed. */
488 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200489 return ret;
490
491error:
492 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
493 "device 0.%x.%04x\n",
494 ret2, cdev->private->dev_id.ssid,
495 cdev->private->dev_id.devno);
496 cdev->private->state = DEV_STATE_OFFLINE;
497 spin_unlock_irq(cdev->ccwlock);
498 /* Give up online reference since onlining failed. */
499 put_device(&cdev->dev);
500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100503static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200504{
505 if (cdev->private->state == DEV_STATE_DISCONNECTED)
506 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100507 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
508 return ccw_device_set_offline(cdev);
509 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200510}
511
512static int online_store_recog_and_online(struct ccw_device *cdev)
513{
514 int ret;
515
516 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200517 if (cdev->private->state == DEV_STATE_BOXED) {
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200518 ret = ccw_device_recognition(cdev);
519 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200520 CIO_MSG_EVENT(0, "Couldn't start recognition "
521 "for device 0.%x.%04x (ret=%d)\n",
522 cdev->private->dev_id.ssid,
523 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200524 return ret;
525 }
526 wait_event(cdev->private->wait_q,
527 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200528 if (cdev->private->state != DEV_STATE_OFFLINE)
529 /* recognition failed */
530 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200531 }
532 if (cdev->drv && cdev->drv->set_online)
533 ccw_device_set_online(cdev);
534 return 0;
535}
Sebastian Ott156013f2009-03-31 19:16:03 +0200536
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200537static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538{
539 int ret;
540
541 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200542 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200543 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200544 if (force && cdev->private->state == DEV_STATE_BOXED) {
545 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200546 if (ret)
547 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200548 if (cdev->id.cu_type == 0)
549 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200550 ret = online_store_recog_and_online(cdev);
551 if (ret)
552 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200553 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200554 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200555}
556
557static ssize_t online_store (struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200561 int force, ret;
562 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200564 if ((cdev->private->state != DEV_STATE_OFFLINE &&
565 cdev->private->state != DEV_STATE_ONLINE &&
566 cdev->private->state != DEV_STATE_BOXED &&
567 cdev->private->state != DEV_STATE_DISCONNECTED) ||
568 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EAGAIN;
570
571 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
572 atomic_set(&cdev->private->onoff, 0);
573 return -EINVAL;
574 }
575 if (!strncmp(buf, "force\n", count)) {
576 force = 1;
577 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200578 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200581 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200583 if (ret)
584 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200585 switch (i) {
586 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100587 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200588 break;
589 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200590 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200591 break;
592 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200593 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200595out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (cdev->drv)
597 module_put(cdev->drv->owner);
598 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100599 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400603available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct ccw_device *cdev = to_ccwdev(dev);
606 struct subchannel *sch;
607
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100608 if (ccw_device_is_orphan(cdev))
609 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 switch (cdev->private->state) {
611 case DEV_STATE_BOXED:
612 return sprintf(buf, "boxed\n");
613 case DEV_STATE_DISCONNECTED:
614 case DEV_STATE_DISCONNECTED_SENSE_ID:
615 case DEV_STATE_NOT_OPER:
616 sch = to_subchannel(dev->parent);
617 if (!sch->lpm)
618 return sprintf(buf, "no path\n");
619 else
620 return sprintf(buf, "no device\n");
621 default:
622 /* All other states considered fine. */
623 return sprintf(buf, "good\n");
624 }
625}
626
627static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
628static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
629static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
630static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800631static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static DEVICE_ATTR(availability, 0444, available_show, NULL);
634
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200635static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 &dev_attr_chpids.attr,
637 &dev_attr_pimpampom.attr,
638 NULL,
639};
640
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200641static struct attribute_group io_subchannel_attr_group = {
642 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100643};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645static struct attribute * ccwdev_attrs[] = {
646 &dev_attr_devtype.attr,
647 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800648 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 &dev_attr_online.attr,
650 &dev_attr_cmb_enable.attr,
651 &dev_attr_availability.attr,
652 NULL,
653};
654
655static struct attribute_group ccwdev_attr_group = {
656 .attrs = ccwdev_attrs,
657};
658
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200659static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200660 &ccwdev_attr_group,
661 NULL,
662};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/* this is a simple abstraction for device_register that sets the
665 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200666static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct device *dev = &cdev->dev;
669 int ret;
670
671 dev->bus = &ccw_bus_type;
672
673 if ((ret = device_add(dev)))
674 return ret;
675
676 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return ret;
678}
679
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700680struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200681 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700682 struct ccw_device * sibling;
683};
684
685static int
686match_devno(struct device * dev, void * data)
687{
Cornelia Huck78964262006-10-11 15:31:38 +0200688 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700689 struct ccw_device * cdev;
690
691 cdev = to_ccwdev(dev);
692 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100693 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200694 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100695 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700696 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700697 return 0;
698}
699
Cornelia Huck78964262006-10-11 15:31:38 +0200700static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
701 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200704 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Cornelia Huck78964262006-10-11 15:31:38 +0200706 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200707 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700708 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700710 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100713static int match_orphan(struct device *dev, void *data)
714{
715 struct ccw_dev_id *dev_id;
716 struct ccw_device *cdev;
717
718 dev_id = data;
719 cdev = to_ccwdev(dev);
720 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
721}
722
723static struct ccw_device *
724get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
725 struct ccw_dev_id *dev_id)
726{
727 struct device *dev;
728
729 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
730 match_orphan);
731
732 return dev ? to_ccwdev(dev) : NULL;
733}
734
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100735void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100737 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct ccw_device *cdev;
739 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100740 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100742 priv = container_of(work, struct ccw_device_private, kick_work);
743 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100746 if (test_bit(1, &cdev->private->registered)) {
747 device_release_driver(&cdev->dev);
748 ret = device_attach(&cdev->dev);
749 WARN_ON(ret == -ENODEV);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static void
754ccw_device_release(struct device *dev)
755{
756 struct ccw_device *cdev;
757
758 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100759 /* Release reference of parent subchannel. */
760 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 kfree(cdev->private);
762 kfree(cdev);
763}
764
Cornelia Huck7674da72006-12-08 15:54:21 +0100765static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
766{
767 struct ccw_device *cdev;
768
769 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
770 if (cdev) {
771 cdev->private = kzalloc(sizeof(struct ccw_device_private),
772 GFP_KERNEL | GFP_DMA);
773 if (cdev->private)
774 return cdev;
775 }
776 kfree(cdev);
777 return ERR_PTR(-ENOMEM);
778}
779
780static int io_subchannel_initialize_dev(struct subchannel *sch,
781 struct ccw_device *cdev)
782{
783 cdev->private->cdev = cdev;
784 atomic_set(&cdev->private->onoff, 0);
785 cdev->dev.parent = &sch->dev;
786 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100787 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200788 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100789 /* Do first half of device_register. */
790 device_initialize(&cdev->dev);
791 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100792 /* Release reference from device_initialize(). */
793 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100794 return -ENODEV;
795 }
796 return 0;
797}
798
799static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
800{
801 struct ccw_device *cdev;
802 int ret;
803
804 cdev = io_subchannel_allocate_dev(sch);
805 if (!IS_ERR(cdev)) {
806 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200807 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100808 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100809 }
810 return cdev;
811}
812
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100813static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
814
815static void sch_attach_device(struct subchannel *sch,
816 struct ccw_device *cdev)
817{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200818 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100819 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100820 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100821 cdev->private->schid = sch->schid;
822 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200823 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100824 spin_unlock_irq(sch->lock);
825}
826
827static void sch_attach_disconnected_device(struct subchannel *sch,
828 struct ccw_device *cdev)
829{
830 struct subchannel *other_sch;
831 int ret;
832
Cornelia Huck6eff2082008-12-25 13:39:07 +0100833 /* Get reference for new parent. */
834 if (!get_device(&sch->dev))
835 return;
836 other_sch = to_subchannel(cdev->dev.parent);
837 /* Note: device_move() changes cdev->dev.parent */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100838 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100839 if (ret) {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200840 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100841 "(ret=%d)!\n", cdev->private->dev_id.ssid,
842 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100843 /* Put reference for new parent. */
844 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100845 return;
846 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100847 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100848 /* No need to keep a subchannel without ccw device around. */
849 css_sch_device_unregister(other_sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100850 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100851 /* Put reference for old parent. */
852 put_device(&other_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100853}
854
855static void sch_attach_orphaned_device(struct subchannel *sch,
856 struct ccw_device *cdev)
857{
858 int ret;
Cornelia Huck6eff2082008-12-25 13:39:07 +0100859 struct subchannel *pseudo_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100860
Cornelia Huck6eff2082008-12-25 13:39:07 +0100861 /* Get reference for new parent. */
862 if (!get_device(&sch->dev))
863 return;
864 pseudo_sch = to_subchannel(cdev->dev.parent);
865 /*
866 * Try to move the ccw device to its new subchannel.
867 * Note: device_move() changes cdev->dev.parent
868 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100869 ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100870 if (ret) {
871 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
872 "failed (ret=%d)!\n",
873 cdev->private->dev_id.ssid,
874 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100875 /* Put reference for new parent. */
876 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100877 return;
878 }
879 sch_attach_device(sch, cdev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100880 /* Put reference on pseudo subchannel. */
881 put_device(&pseudo_sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100882}
883
884static void sch_create_and_recog_new_device(struct subchannel *sch)
885{
886 struct ccw_device *cdev;
887
888 /* Need to allocate a new ccw device. */
889 cdev = io_subchannel_create_ccwdev(sch);
890 if (IS_ERR(cdev)) {
891 /* OK, we did everything we could... */
892 css_sch_device_unregister(sch);
893 return;
894 }
895 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100896 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100897 spin_unlock_irq(sch->lock);
898 /* Start recognition for the new ccw device. */
899 if (io_subchannel_recog(cdev, sch)) {
900 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100901 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100902 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100903 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100904 /* Put reference from io_subchannel_create_ccwdev(). */
905 put_device(&sch->dev);
906 /* Give up initial reference. */
907 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100908 }
909}
910
911
912void ccw_device_move_to_orphanage(struct work_struct *work)
913{
914 struct ccw_device_private *priv;
915 struct ccw_device *cdev;
916 struct ccw_device *replacing_cdev;
917 struct subchannel *sch;
918 int ret;
919 struct channel_subsystem *css;
920 struct ccw_dev_id dev_id;
921
922 priv = container_of(work, struct ccw_device_private, kick_work);
923 cdev = priv->cdev;
924 sch = to_subchannel(cdev->dev.parent);
925 css = to_css(sch->dev.parent);
926 dev_id.devno = sch->schib.pmcw.dev;
927 dev_id.ssid = sch->schid.ssid;
928
Cornelia Huck6eff2082008-12-25 13:39:07 +0100929 /* Increase refcount for pseudo subchannel. */
930 get_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100931 /*
932 * Move the orphaned ccw device to the orphanage so the replacing
933 * ccw device can take its place on the subchannel.
Cornelia Huck6eff2082008-12-25 13:39:07 +0100934 * Note: device_move() changes cdev->dev.parent
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100935 */
Cornelia Huckffa6a702009-03-04 12:44:00 +0100936 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
937 DPM_ORDER_NONE);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100938 if (ret) {
939 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
940 "(ret=%d)!\n", cdev->private->dev_id.ssid,
941 cdev->private->dev_id.devno, ret);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100942 /* Decrease refcount for pseudo subchannel again. */
943 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100944 return;
945 }
946 cdev->ccwlock = css->pseudo_subchannel->lock;
947 /*
948 * Search for the replacing ccw device
949 * - among the disconnected devices
950 * - in the orphanage
951 */
952 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
953 if (replacing_cdev) {
954 sch_attach_disconnected_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100955 /* Release reference from get_disc_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100956 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100957 /* Release reference of subchannel from old cdev. */
958 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100959 return;
960 }
961 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
962 if (replacing_cdev) {
963 sch_attach_orphaned_device(sch, replacing_cdev);
Cornelia Huck85acc402008-11-14 18:18:06 +0100964 /* Release reference from get_orphaned_ccwdev_by_dev_id() */
Cornelia Huck97166f52008-12-25 13:39:05 +0100965 put_device(&replacing_cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100966 /* Release reference of subchannel from old cdev. */
967 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100968 return;
969 }
970 sch_create_and_recog_new_device(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100971 /* Release reference of subchannel from old cdev. */
972 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/*
976 * Register recognized device.
977 */
978static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100979io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100981 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct ccw_device *cdev;
983 struct subchannel *sch;
984 int ret;
985 unsigned long flags;
986
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100987 priv = container_of(work, struct ccw_device_private, kick_work);
988 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100990 /*
991 * Check if subchannel is still registered. It may have become
992 * unregistered if a machine check hit us after finishing
993 * device recognition but before the register work could be
994 * queued.
995 */
996 if (!device_is_registered(&sch->dev))
997 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200998 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100999 /*
1000 * io_subchannel_register() will also be called after device
1001 * recognition has been done for a boxed device (which will already
1002 * be registered). We need to reprobe since we may now have sense id
1003 * information.
1004 */
Cornelia Huckd6a30762008-12-25 13:39:11 +01001005 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +01001006 if (!cdev->drv) {
1007 ret = device_reprobe(&cdev->dev);
1008 if (ret)
1009 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +02001010 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001011 " %d for 0.%x.%04x\n", ret,
1012 cdev->private->dev_id.ssid,
1013 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +01001014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto out;
1016 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -07001017 /*
1018 * Now we know this subchannel will stay, we can throw
1019 * our delayed uevent.
1020 */
Ming Leif67f1292009-03-01 21:10:49 +08001021 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -07001022 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* make it known to the system */
1024 ret = ccw_device_register(cdev);
1025 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001026 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
1027 cdev->private->dev_id.ssid,
1028 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001029 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001030 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001031 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001032 /* Release initial device reference. */
1033 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001034 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036out:
1037 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +01001039out_err:
1040 /* Release reference for workqueue processing. */
1041 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (atomic_dec_and_test(&ccw_device_init_count))
1043 wake_up(&ccw_device_init_wq);
1044}
1045
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +02001046static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001048 struct ccw_device_private *priv;
1049 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct subchannel *sch;
1051
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001052 priv = container_of(work, struct ccw_device_private, kick_work);
1053 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001054 /* Get subchannel reference for local processing. */
1055 if (!get_device(cdev->dev.parent))
1056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +02001058 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001059 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +02001061 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 put_device(&sch->dev);
1063}
1064
Sebastian Ottc4621a62009-03-31 19:16:04 +02001065void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
1066{
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +02001067 /* Get cdev reference for workqueue processing. */
1068 if (!get_device(&cdev->dev))
1069 return;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001070 PREPARE_WORK(&cdev->private->kick_work,
1071 ccw_device_call_sch_unregister);
1072 queue_work(slow_path_wq, &cdev->private->kick_work);
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
1076 * subchannel recognition done. Called from the state machine.
1077 */
1078void
1079io_subchannel_recog_done(struct ccw_device *cdev)
1080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (css_init_done == 0) {
1082 cdev->private->flags.recog_done = 1;
1083 return;
1084 }
1085 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +02001086 case DEV_STATE_BOXED:
1087 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 case DEV_STATE_NOT_OPER:
1089 cdev->private->flags.recog_done = 1;
Sebastian Ottc4621a62009-03-31 19:16:04 +02001090 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (atomic_dec_and_test(&ccw_device_init_count))
1092 wake_up(&ccw_device_init_wq);
1093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 case DEV_STATE_OFFLINE:
1095 /*
1096 * We can't register the device in interrupt context so
1097 * we schedule a work item.
1098 */
1099 if (!get_device(&cdev->dev))
1100 break;
1101 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001102 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 queue_work(slow_path_wq, &cdev->private->kick_work);
1104 break;
1105 }
1106}
1107
1108static int
1109io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1110{
1111 int rc;
1112 struct ccw_device_private *priv;
1113
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001114 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001115 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Init private data. */
1118 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001119 priv->dev_id.devno = sch->schib.pmcw.dev;
1120 priv->dev_id.ssid = sch->schid.ssid;
1121 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 priv->state = DEV_STATE_NOT_OPER;
1123 INIT_LIST_HEAD(&priv->cmb_list);
1124 init_waitqueue_head(&priv->wait_q);
1125 init_timer(&priv->timer);
1126
1127 /* Set an initial name for the device. */
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001128 if (cio_is_console(sch->schid))
1129 cdev->dev.init_name = cio_get_console_cdev_name(sch);
1130 else
1131 dev_set_name(&cdev->dev, "0.%x.%04x",
1132 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Increase counter of devices currently in recognition. */
1135 atomic_inc(&ccw_device_init_count);
1136
1137 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001138 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001140 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (rc) {
1142 if (atomic_dec_and_test(&ccw_device_init_count))
1143 wake_up(&ccw_device_init_wq);
1144 }
1145 return rc;
1146}
1147
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001148static void ccw_device_move_to_sch(struct work_struct *work)
1149{
1150 struct ccw_device_private *priv;
1151 int rc;
1152 struct subchannel *sch;
1153 struct ccw_device *cdev;
1154 struct subchannel *former_parent;
1155
1156 priv = container_of(work, struct ccw_device_private, kick_work);
1157 sch = priv->sch;
1158 cdev = priv->cdev;
Cornelia Huck6eff2082008-12-25 13:39:07 +01001159 former_parent = to_subchannel(cdev->dev.parent);
1160 /* Get reference for new parent. */
1161 if (!get_device(&sch->dev))
1162 return;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001163 mutex_lock(&sch->reg_mutex);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001164 /*
1165 * Try to move the ccw device to its new subchannel.
1166 * Note: device_move() changes cdev->dev.parent
1167 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01001168 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001169 mutex_unlock(&sch->reg_mutex);
1170 if (rc) {
Michael Ernst139b83dd2008-05-07 09:22:54 +02001171 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001172 "0.%x.%04x failed (ret=%d)!\n",
1173 cdev->private->dev_id.ssid,
1174 cdev->private->dev_id.devno, sch->schid.ssid,
1175 sch->schid.sch_no, rc);
1176 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +01001177 /* Put reference for new parent again. */
1178 put_device(&sch->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001179 goto out;
1180 }
Cornelia Huck6eff2082008-12-25 13:39:07 +01001181 if (!sch_is_pseudo_sch(former_parent)) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001182 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001183 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001184 spin_unlock_irq(former_parent->lock);
1185 css_sch_device_unregister(former_parent);
1186 /* Reset intparm to zeroes. */
Sebastian Ott13952ec2008-12-25 13:39:13 +01001187 former_parent->config.intparm = 0;
1188 cio_commit_config(former_parent);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001189 }
1190 sch_attach_device(sch, cdev);
1191out:
Cornelia Huck6eff2082008-12-25 13:39:07 +01001192 /* Put reference for old parent. */
1193 put_device(&former_parent->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001194 put_device(&cdev->dev);
1195}
1196
Cornelia Huck602b20f2008-01-26 14:10:39 +01001197static void io_subchannel_irq(struct subchannel *sch)
1198{
1199 struct ccw_device *cdev;
1200
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001201 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001202
Sebastian Ottefd986d2009-09-11 10:28:18 +02001203 CIO_TRACE_EVENT(6, "IRQ");
1204 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001205 if (cdev)
1206 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1207}
1208
Sebastian Ott13952ec2008-12-25 13:39:13 +01001209void io_subchannel_init_config(struct subchannel *sch)
1210{
1211 memset(&sch->config, 0, sizeof(sch->config));
1212 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001213 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1214 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001215 sch->config.mp = 1;
1216}
1217
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001218static void io_subchannel_init_fields(struct subchannel *sch)
1219{
1220 if (cio_is_console(sch->schid))
1221 sch->opm = 0xff;
1222 else
1223 sch->opm = chp_get_sch_opm(sch);
1224 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001225 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001226
1227 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1228 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1229 sch->schib.pmcw.dev, sch->schid.ssid,
1230 sch->schid.sch_no, sch->schib.pmcw.pim,
1231 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001232
1233 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001234}
1235
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001236static void io_subchannel_do_unreg(struct work_struct *work)
1237{
1238 struct subchannel *sch;
1239
1240 sch = container_of(work, struct subchannel, work);
1241 css_sch_device_unregister(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001242 put_device(&sch->dev);
1243}
1244
1245/* Schedule unregister if we have no cdev. */
1246static void io_subchannel_schedule_removal(struct subchannel *sch)
1247{
1248 get_device(&sch->dev);
1249 INIT_WORK(&sch->work, io_subchannel_do_unreg);
1250 queue_work(slow_path_wq, &sch->work);
1251}
1252
1253/*
1254 * Note: We always return 0 so that we bind to the device even on error.
1255 * This is needed so that our remove function is called on unregister.
1256 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001257static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct ccw_device *cdev;
1260 int rc;
1261 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001262 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001264 cdev = sch_get_cdev(sch);
1265 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001266 rc = sysfs_create_group(&sch->dev.kobj,
1267 &io_subchannel_attr_group);
1268 if (rc)
1269 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1270 "attributes for subchannel "
1271 "0.%x.%04x (rc=%d)\n",
1272 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001275 * Throw the delayed uevent for the subchannel, register
1276 * the ccw_device and exit. This happens for all early
1277 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
Ming Leif67f1292009-03-01 21:10:49 +08001279 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001280 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001281 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 device_initialize(&cdev->dev);
1283 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001286 * the reference count needs to be corrected since we
1287 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
1289 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1290 cdev->private->state != DEV_STATE_OFFLINE &&
1291 cdev->private->state != DEV_STATE_BOXED)
1292 get_device(&cdev->dev);
1293 return 0;
1294 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001295 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001296 rc = cio_commit_config(sch);
1297 if (rc)
1298 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001299 rc = sysfs_create_group(&sch->dev.kobj,
1300 &io_subchannel_attr_group);
1301 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001302 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001303 /* Allocate I/O subchannel private data. */
1304 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1305 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001306 if (!sch->private)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001307 goto out_err;
Sebastian Ott111e95a2008-12-25 13:39:03 +01001308 /*
1309 * First check if a fitting device may be found amongst the
1310 * disconnected devices or in the orphanage.
1311 */
1312 dev_id.devno = sch->schib.pmcw.dev;
1313 dev_id.ssid = sch->schid.ssid;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001314 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1315 if (!cdev)
1316 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1317 &dev_id);
1318 if (cdev) {
1319 /*
1320 * Schedule moving the device until when we have a registered
1321 * subchannel to move to and succeed the probe. We can
1322 * unregister later again, when the probe is through.
1323 */
1324 cdev->private->sch = sch;
1325 PREPARE_WORK(&cdev->private->kick_work,
1326 ccw_device_move_to_sch);
1327 queue_work(slow_path_wq, &cdev->private->kick_work);
1328 return 0;
1329 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001330 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001331 if (IS_ERR(cdev))
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001332 goto out_err;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001333 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001335 spin_lock_irqsave(sch->lock, flags);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001336 io_subchannel_recog_done(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001337 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001339 return 0;
1340out_err:
1341 kfree(sch->private);
1342 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001343out_schedule:
1344 io_subchannel_schedule_removal(sch);
1345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001349io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct ccw_device *cdev;
1352 unsigned long flags;
1353
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001354 cdev = sch_get_cdev(sch);
1355 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* Set ccw device to not operational and drop reference. */
1358 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001359 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 cdev->private->state = DEV_STATE_NOT_OPER;
1361 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001362 ccw_device_unregister(cdev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001363 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001364 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366}
1367
Cornelia Huck602b20f2008-01-26 14:10:39 +01001368static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct ccw_device *cdev;
1371
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001372 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!cdev)
1374 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001375 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Cornelia Huck602b20f2008-01-26 14:10:39 +01001378static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct ccw_device *cdev;
1381
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001382 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (cdev)
1384 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1385}
1386
Cornelia Huckc820de32008-07-14 09:58:45 +02001387static int check_for_io_on_path(struct subchannel *sch, int mask)
1388{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001389 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001390 return 0;
Peter Oberparleiter23d805b62008-07-14 09:58:50 +02001391 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001392 return 1;
1393 return 0;
1394}
1395
1396static void terminate_internal_io(struct subchannel *sch,
1397 struct ccw_device *cdev)
1398{
1399 if (cio_clear(sch)) {
1400 /* Recheck device in case clear failed. */
1401 sch->lpm = 0;
1402 if (cdev->online)
1403 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1404 else
1405 css_schedule_eval(sch->schid);
1406 return;
1407 }
1408 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1409 /* Request retry of internal operation. */
1410 cdev->private->flags.intretry = 1;
1411 /* Call handler. */
1412 if (cdev->handler)
1413 cdev->handler(cdev, cdev->private->intparm,
1414 ERR_PTR(-EIO));
1415}
1416
1417static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct ccw_device *cdev;
1420
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001421 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!cdev)
1423 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001424 if (check_for_io_on_path(sch, mask)) {
1425 if (cdev->private->state == DEV_STATE_ONLINE)
1426 ccw_device_kill_io(cdev);
1427 else {
1428 terminate_internal_io(sch, cdev);
1429 /* Re-start path verification. */
1430 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1431 }
1432 } else
1433 /* trigger path verification. */
1434 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1435
1436}
1437
Cornelia Huck99611f82008-07-14 09:59:02 +02001438static int io_subchannel_chp_event(struct subchannel *sch,
1439 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001440{
1441 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001442
Cornelia Huck99611f82008-07-14 09:59:02 +02001443 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001444 if (!mask)
1445 return 0;
1446 switch (event) {
1447 case CHP_VARY_OFF:
1448 sch->opm &= ~mask;
1449 sch->lpm &= ~mask;
1450 io_subchannel_terminate_path(sch, mask);
1451 break;
1452 case CHP_VARY_ON:
1453 sch->opm |= mask;
1454 sch->lpm |= mask;
1455 io_subchannel_verify(sch);
1456 break;
1457 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001458 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001459 return -ENODEV;
1460 io_subchannel_terminate_path(sch, mask);
1461 break;
1462 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001463 if (cio_update_schib(sch))
1464 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001465 sch->lpm |= mask & sch->opm;
1466 io_subchannel_verify(sch);
1467 break;
1468 }
1469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001473io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct ccw_device *cdev;
1476 int ret;
1477
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001478 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001480 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return;
1482 if (!sch->schib.pmcw.ena)
1483 /* Nothing to do. */
1484 return;
1485 ret = cio_disable_subchannel(sch);
1486 if (ret != -EBUSY)
1487 /* Subchannel is disabled, we're done. */
1488 return;
1489 cdev->private->state = DEV_STATE_QUIESCE;
1490 if (cdev->handler)
1491 cdev->handler(cdev, cdev->private->intparm,
1492 ERR_PTR(-EIO));
1493 ret = ccw_device_cancel_halt_clear(cdev);
1494 if (ret == -EBUSY) {
1495 ccw_device_set_timeout(cdev, HZ/10);
1496 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1497 }
1498 cio_disable_subchannel(sch);
1499}
1500
Cornelia Huckc820de32008-07-14 09:58:45 +02001501static int io_subchannel_get_status(struct subchannel *sch)
1502{
1503 struct schib schib;
1504
1505 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1506 return CIO_GONE;
1507 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1508 return CIO_REVALIDATE;
1509 if (!sch->lpm)
1510 return CIO_NO_PATH;
1511 return CIO_OPER;
1512}
1513
1514static int device_is_disconnected(struct ccw_device *cdev)
1515{
1516 if (!cdev)
1517 return 0;
1518 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1519 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1520}
1521
1522static int recovery_check(struct device *dev, void *data)
1523{
1524 struct ccw_device *cdev = to_ccwdev(dev);
1525 int *redo = data;
1526
1527 spin_lock_irq(cdev->ccwlock);
1528 switch (cdev->private->state) {
1529 case DEV_STATE_DISCONNECTED:
1530 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1531 cdev->private->dev_id.ssid,
1532 cdev->private->dev_id.devno);
1533 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1534 *redo = 1;
1535 break;
1536 case DEV_STATE_DISCONNECTED_SENSE_ID:
1537 *redo = 1;
1538 break;
1539 }
1540 spin_unlock_irq(cdev->ccwlock);
1541
1542 return 0;
1543}
1544
1545static void recovery_work_func(struct work_struct *unused)
1546{
1547 int redo = 0;
1548
1549 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1550 if (redo) {
1551 spin_lock_irq(&recovery_lock);
1552 if (!timer_pending(&recovery_timer)) {
1553 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1554 recovery_phase++;
1555 mod_timer(&recovery_timer, jiffies +
1556 recovery_delay[recovery_phase] * HZ);
1557 }
1558 spin_unlock_irq(&recovery_lock);
1559 } else
1560 CIO_MSG_EVENT(4, "recovery: end\n");
1561}
1562
1563static DECLARE_WORK(recovery_work, recovery_work_func);
1564
1565static void recovery_func(unsigned long data)
1566{
1567 /*
1568 * We can't do our recovery in softirq context and it's not
1569 * performance critical, so we schedule it.
1570 */
1571 schedule_work(&recovery_work);
1572}
1573
1574static void ccw_device_schedule_recovery(void)
1575{
1576 unsigned long flags;
1577
1578 CIO_MSG_EVENT(4, "recovery: schedule\n");
1579 spin_lock_irqsave(&recovery_lock, flags);
1580 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1581 recovery_phase = 0;
1582 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1583 }
1584 spin_unlock_irqrestore(&recovery_lock, flags);
1585}
1586
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001587static int purge_fn(struct device *dev, void *data)
1588{
1589 struct ccw_device *cdev = to_ccwdev(dev);
1590 struct ccw_device_private *priv = cdev->private;
1591 int unreg;
1592
1593 spin_lock_irq(cdev->ccwlock);
1594 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1595 (priv->state == DEV_STATE_OFFLINE);
1596 spin_unlock_irq(cdev->ccwlock);
1597 if (!unreg)
1598 goto out;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001599 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1600 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001601 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001602
1603out:
1604 /* Abort loop in case of pending signal. */
1605 if (signal_pending(current))
1606 return -EINTR;
1607
1608 return 0;
1609}
1610
1611/**
1612 * ccw_purge_blacklisted - purge unused, blacklisted devices
1613 *
1614 * Unregister all ccw devices that are offline and on the blacklist.
1615 */
1616int ccw_purge_blacklisted(void)
1617{
1618 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1619 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1620 return 0;
1621}
1622
Cornelia Huckc820de32008-07-14 09:58:45 +02001623static void device_set_disconnected(struct ccw_device *cdev)
1624{
1625 if (!cdev)
1626 return;
1627 ccw_device_set_timeout(cdev, 0);
1628 cdev->private->flags.fake_irb = 0;
1629 cdev->private->state = DEV_STATE_DISCONNECTED;
1630 if (cdev->online)
1631 ccw_device_schedule_recovery();
1632}
1633
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001634void ccw_device_set_notoper(struct ccw_device *cdev)
1635{
1636 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1637
1638 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001639 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001640 ccw_device_set_timeout(cdev, 0);
1641 cio_disable_subchannel(sch);
1642 cdev->private->state = DEV_STATE_NOT_OPER;
1643}
1644
Cornelia Huckc820de32008-07-14 09:58:45 +02001645static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1646{
1647 int event, ret, disc;
1648 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001649 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001650 struct ccw_device *cdev;
1651
1652 spin_lock_irqsave(sch->lock, flags);
1653 cdev = sch_get_cdev(sch);
1654 disc = device_is_disconnected(cdev);
1655 if (disc && slow) {
1656 /* Disconnected devices are evaluated directly only.*/
1657 spin_unlock_irqrestore(sch->lock, flags);
1658 return 0;
1659 }
1660 /* No interrupt after machine check - kill pending timers. */
1661 if (cdev)
1662 ccw_device_set_timeout(cdev, 0);
1663 if (!disc && !slow) {
1664 /* Non-disconnected devices are evaluated on the slow path. */
1665 spin_unlock_irqrestore(sch->lock, flags);
1666 return -EAGAIN;
1667 }
1668 event = io_subchannel_get_status(sch);
1669 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1670 sch->schid.ssid, sch->schid.sch_no, event,
1671 disc ? "disconnected" : "normal",
1672 slow ? "slow" : "fast");
1673 /* Analyze subchannel status. */
1674 action = NONE;
1675 switch (event) {
1676 case CIO_NO_PATH:
1677 if (disc) {
1678 /* Check if paths have become available. */
1679 action = REPROBE;
1680 break;
1681 }
1682 /* fall through */
1683 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001684 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001685 if (io_subchannel_notify(sch, event))
1686 action = DISC;
1687 else
1688 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001689 break;
1690 case CIO_REVALIDATE:
1691 /* Device will be removed, so no notify necessary. */
1692 if (disc)
1693 /* Reprobe because immediate unregister might block. */
1694 action = REPROBE;
1695 else
1696 action = UNREGISTER_PROBE;
1697 break;
1698 case CIO_OPER:
1699 if (disc)
1700 /* Get device operational again. */
1701 action = REPROBE;
1702 break;
1703 }
1704 /* Perform action. */
1705 ret = 0;
1706 switch (action) {
1707 case UNREGISTER:
1708 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001709 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001710 /* Unregister device (will use subchannel lock). */
1711 spin_unlock_irqrestore(sch->lock, flags);
1712 css_sch_device_unregister(sch);
1713 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckc820de32008-07-14 09:58:45 +02001714 break;
1715 case REPROBE:
1716 ccw_device_trigger_reprobe(cdev);
1717 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001718 case DISC:
1719 device_set_disconnected(cdev);
1720 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001721 default:
1722 break;
1723 }
1724 spin_unlock_irqrestore(sch->lock, flags);
1725 /* Probe if necessary. */
1726 if (action == UNREGISTER_PROBE)
1727 ret = css_probe_device(sch->schid);
1728
1729 return ret;
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#ifdef CONFIG_CCW_CONSOLE
1733static struct ccw_device console_cdev;
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001734static char console_cdev_name[10] = "0.x.xxxx";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static struct ccw_device_private console_private;
1736static int console_cdev_in_use;
1737
Cornelia Huck2ec22982006-12-08 15:54:26 +01001738static DEFINE_SPINLOCK(ccw_console_lock);
1739
1740spinlock_t * cio_get_console_lock(void)
1741{
1742 return &ccw_console_lock;
1743}
1744
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001745static int ccw_device_console_enable(struct ccw_device *cdev,
1746 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 int rc;
1749
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001750 /* Attach subchannel private data. */
1751 sch->private = cio_get_console_priv();
1752 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001753 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001754 rc = cio_commit_config(sch);
1755 if (rc)
1756 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001757 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001759 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 rc = io_subchannel_recog(cdev, sch);
1761 if (rc)
1762 return rc;
1763
1764 /* Now wait for the async. recognition to come to an end. */
1765 spin_lock_irq(cdev->ccwlock);
1766 while (!dev_fsm_final_state(cdev))
1767 wait_cons_dev();
1768 rc = -EIO;
1769 if (cdev->private->state != DEV_STATE_OFFLINE)
1770 goto out_unlock;
1771 ccw_device_online(cdev);
1772 while (!dev_fsm_final_state(cdev))
1773 wait_cons_dev();
1774 if (cdev->private->state != DEV_STATE_ONLINE)
1775 goto out_unlock;
1776 rc = 0;
1777out_unlock:
1778 spin_unlock_irq(cdev->ccwlock);
1779 return 0;
1780}
1781
1782struct ccw_device *
1783ccw_device_probe_console(void)
1784{
1785 struct subchannel *sch;
1786 int ret;
1787
1788 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001789 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 sch = cio_probe_console();
1791 if (IS_ERR(sch)) {
1792 console_cdev_in_use = 0;
1793 return (void *) sch;
1794 }
1795 memset(&console_cdev, 0, sizeof(struct ccw_device));
1796 memset(&console_private, 0, sizeof(struct ccw_device_private));
1797 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001798 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 ret = ccw_device_console_enable(&console_cdev, sch);
1800 if (ret) {
1801 cio_release_console();
1802 console_cdev_in_use = 0;
1803 return ERR_PTR(ret);
1804 }
1805 console_cdev.online = 1;
1806 return &console_cdev;
1807}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001808
Martin Schwidefsky66648452009-06-16 10:30:28 +02001809static int ccw_device_pm_restore(struct device *dev);
1810
1811int ccw_device_force_console(void)
1812{
1813 if (!console_cdev_in_use)
1814 return -ENODEV;
1815 return ccw_device_pm_restore(&console_cdev.dev);
1816}
1817EXPORT_SYMBOL_GPL(ccw_device_force_console);
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001818
1819const char *cio_get_console_cdev_name(struct subchannel *sch)
1820{
1821 snprintf(console_cdev_name, 10, "0.%x.%04x",
1822 sch->schid.ssid, sch->schib.pmcw.dev);
1823 return (const char *)console_cdev_name;
1824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825#endif
1826
1827/*
1828 * get ccw_device matching the busid, but only if owned by cdrv
1829 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001830static int
1831__ccwdev_check_busid(struct device *dev, void *id)
1832{
1833 char *bus_id;
1834
Cornelia Huck12975ae2006-10-11 15:31:47 +02001835 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001836
Kay Sievers98df67b2008-12-25 13:38:55 +01001837 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001838}
1839
1840
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001841/**
1842 * get_ccwdev_by_busid() - obtain device from a bus id
1843 * @cdrv: driver the device is owned by
1844 * @bus_id: bus id of the device to be searched
1845 *
1846 * This function searches all devices owned by @cdrv for a device with a bus
1847 * id matching @bus_id.
1848 * Returns:
1849 * If a match is found, its reference count of the found device is increased
1850 * and it is returned; else %NULL is returned.
1851 */
1852struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1853 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001855 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct device_driver *drv;
1857
1858 drv = get_driver(&cdrv->driver);
1859 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001860 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001862 dev = driver_find_device(drv, NULL, (void *)bus_id,
1863 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 put_driver(drv);
1865
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001866 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
1869/************************** device driver handling ************************/
1870
1871/* This is the implementation of the ccw_driver class. The probe, remove
1872 * and release methods are initially very similar to the device_driver
1873 * implementations, with the difference that they have ccw_device
1874 * arguments.
1875 *
1876 * A ccw driver also contains the information that is needed for
1877 * device matching.
1878 */
1879static int
1880ccw_device_probe (struct device *dev)
1881{
1882 struct ccw_device *cdev = to_ccwdev(dev);
1883 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1884 int ret;
1885
1886 cdev->drv = cdrv; /* to let the driver call _set_online */
1887
1888 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1889
1890 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001891 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return ret;
1893 }
1894
1895 return 0;
1896}
1897
1898static int
1899ccw_device_remove (struct device *dev)
1900{
1901 struct ccw_device *cdev = to_ccwdev(dev);
1902 struct ccw_driver *cdrv = cdev->drv;
1903 int ret;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (cdrv->remove)
1906 cdrv->remove(cdev);
1907 if (cdev->online) {
1908 cdev->online = 0;
1909 spin_lock_irq(cdev->ccwlock);
1910 ret = ccw_device_offline(cdev);
1911 spin_unlock_irq(cdev->ccwlock);
1912 if (ret == 0)
1913 wait_event(cdev->private->wait_q,
1914 dev_fsm_final_state(cdev));
1915 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001916 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001917 "device 0.%x.%04x\n",
1918 ret, cdev->private->dev_id.ssid,
1919 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001920 /* Give up reference obtained in ccw_device_set_online(). */
1921 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001924 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 0;
1926}
1927
Cornelia Huck958974fb2007-10-12 16:11:21 +02001928static void ccw_device_shutdown(struct device *dev)
1929{
1930 struct ccw_device *cdev;
1931
1932 cdev = to_ccwdev(dev);
1933 if (cdev->drv && cdev->drv->shutdown)
1934 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001935 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001936}
1937
Sebastian Ott823d4942009-06-16 10:30:20 +02001938static int ccw_device_pm_prepare(struct device *dev)
1939{
1940 struct ccw_device *cdev = to_ccwdev(dev);
1941
1942 if (work_pending(&cdev->private->kick_work))
1943 return -EAGAIN;
1944 /* Fail while device is being set online/offline. */
1945 if (atomic_read(&cdev->private->onoff))
1946 return -EAGAIN;
1947
1948 if (cdev->online && cdev->drv && cdev->drv->prepare)
1949 return cdev->drv->prepare(cdev);
1950
1951 return 0;
1952}
1953
1954static void ccw_device_pm_complete(struct device *dev)
1955{
1956 struct ccw_device *cdev = to_ccwdev(dev);
1957
1958 if (cdev->online && cdev->drv && cdev->drv->complete)
1959 cdev->drv->complete(cdev);
1960}
1961
1962static int ccw_device_pm_freeze(struct device *dev)
1963{
1964 struct ccw_device *cdev = to_ccwdev(dev);
1965 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1966 int ret, cm_enabled;
1967
1968 /* Fail suspend while device is in transistional state. */
1969 if (!dev_fsm_final_state(cdev))
1970 return -EAGAIN;
1971 if (!cdev->online)
1972 return 0;
1973 if (cdev->drv && cdev->drv->freeze) {
1974 ret = cdev->drv->freeze(cdev);
1975 if (ret)
1976 return ret;
1977 }
1978
1979 spin_lock_irq(sch->lock);
1980 cm_enabled = cdev->private->cmb != NULL;
1981 spin_unlock_irq(sch->lock);
1982 if (cm_enabled) {
1983 /* Don't have the css write on memory. */
1984 ret = ccw_set_cmf(cdev, 0);
1985 if (ret)
1986 return ret;
1987 }
1988 /* From here on, disallow device driver I/O. */
1989 spin_lock_irq(sch->lock);
1990 ret = cio_disable_subchannel(sch);
1991 spin_unlock_irq(sch->lock);
1992
1993 return ret;
1994}
1995
1996static int ccw_device_pm_thaw(struct device *dev)
1997{
1998 struct ccw_device *cdev = to_ccwdev(dev);
1999 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2000 int ret, cm_enabled;
2001
2002 if (!cdev->online)
2003 return 0;
2004
2005 spin_lock_irq(sch->lock);
2006 /* Allow device driver I/O again. */
2007 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
2008 cm_enabled = cdev->private->cmb != NULL;
2009 spin_unlock_irq(sch->lock);
2010 if (ret)
2011 return ret;
2012
2013 if (cm_enabled) {
2014 ret = ccw_set_cmf(cdev, 1);
2015 if (ret)
2016 return ret;
2017 }
2018
2019 if (cdev->drv && cdev->drv->thaw)
2020 ret = cdev->drv->thaw(cdev);
2021
2022 return ret;
2023}
2024
2025static void __ccw_device_pm_restore(struct ccw_device *cdev)
2026{
2027 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2028 int ret;
2029
2030 if (cio_is_console(sch->schid))
2031 goto out;
2032 /*
2033 * While we were sleeping, devices may have gone or become
2034 * available again. Kick re-detection.
2035 */
2036 spin_lock_irq(sch->lock);
2037 cdev->private->flags.resuming = 1;
2038 ret = ccw_device_recognition(cdev);
2039 spin_unlock_irq(sch->lock);
2040 if (ret) {
2041 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
Sebastian Ottf0148242009-09-11 10:28:23 +02002042 "0.%x.%04x (ret=%d)\n",
2043 cdev->private->dev_id.ssid,
2044 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002045 spin_lock_irq(sch->lock);
2046 cdev->private->state = DEV_STATE_DISCONNECTED;
2047 spin_unlock_irq(sch->lock);
2048 /* notify driver after the resume cb */
2049 goto out;
2050 }
2051 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
2052 cdev->private->state == DEV_STATE_DISCONNECTED);
2053
2054out:
2055 cdev->private->flags.resuming = 0;
2056}
2057
2058static int resume_handle_boxed(struct ccw_device *cdev)
2059{
2060 cdev->private->state = DEV_STATE_BOXED;
2061 if (ccw_device_notify(cdev, CIO_BOXED))
2062 return 0;
2063 ccw_device_schedule_sch_unregister(cdev);
2064 return -ENODEV;
2065}
2066
2067static int resume_handle_disc(struct ccw_device *cdev)
2068{
2069 cdev->private->state = DEV_STATE_DISCONNECTED;
2070 if (ccw_device_notify(cdev, CIO_GONE))
2071 return 0;
2072 ccw_device_schedule_sch_unregister(cdev);
2073 return -ENODEV;
2074}
2075
2076static int ccw_device_pm_restore(struct device *dev)
2077{
2078 struct ccw_device *cdev = to_ccwdev(dev);
2079 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2080 int ret = 0, cm_enabled;
2081
2082 __ccw_device_pm_restore(cdev);
2083 spin_lock_irq(sch->lock);
2084 if (cio_is_console(sch->schid)) {
2085 cio_enable_subchannel(sch, (u32)(addr_t)sch);
2086 spin_unlock_irq(sch->lock);
2087 goto out_restore;
2088 }
2089 cdev->private->flags.donotify = 0;
2090 /* check recognition results */
2091 switch (cdev->private->state) {
2092 case DEV_STATE_OFFLINE:
2093 break;
2094 case DEV_STATE_BOXED:
2095 ret = resume_handle_boxed(cdev);
2096 spin_unlock_irq(sch->lock);
2097 if (ret)
2098 goto out;
2099 goto out_restore;
2100 case DEV_STATE_DISCONNECTED:
2101 goto out_disc_unlock;
2102 default:
2103 goto out_unreg_unlock;
2104 }
2105 /* check if the device id has changed */
2106 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002107 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
2108 "changed from %04x to %04x)\n",
2109 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02002110 cdev->private->dev_id.devno,
2111 sch->schib.pmcw.dev);
2112 goto out_unreg_unlock;
2113 }
2114 /* check if the device type has changed */
2115 if (!ccw_device_test_sense_data(cdev)) {
2116 ccw_device_update_sense_data(cdev);
2117 PREPARE_WORK(&cdev->private->kick_work,
2118 ccw_device_do_unbind_bind);
2119 queue_work(ccw_device_work, &cdev->private->kick_work);
2120 ret = -ENODEV;
2121 goto out_unlock;
2122 }
2123 if (!cdev->online) {
2124 ret = 0;
2125 goto out_unlock;
2126 }
2127 ret = ccw_device_online(cdev);
2128 if (ret)
2129 goto out_disc_unlock;
2130
2131 cm_enabled = cdev->private->cmb != NULL;
2132 spin_unlock_irq(sch->lock);
2133
2134 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
2135 if (cdev->private->state != DEV_STATE_ONLINE) {
2136 spin_lock_irq(sch->lock);
2137 goto out_disc_unlock;
2138 }
2139 if (cm_enabled) {
2140 ret = ccw_set_cmf(cdev, 1);
2141 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002142 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2143 "(rc=%d)\n", cdev->private->dev_id.ssid,
2144 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002145 ret = 0;
2146 }
2147 }
2148
2149out_restore:
2150 if (cdev->online && cdev->drv && cdev->drv->restore)
2151 ret = cdev->drv->restore(cdev);
2152out:
2153 return ret;
2154
2155out_disc_unlock:
2156 ret = resume_handle_disc(cdev);
2157 spin_unlock_irq(sch->lock);
2158 if (ret)
2159 return ret;
2160 goto out_restore;
2161
2162out_unreg_unlock:
2163 ccw_device_schedule_sch_unregister(cdev);
2164 ret = -ENODEV;
2165out_unlock:
2166 spin_unlock_irq(sch->lock);
2167 return ret;
2168}
2169
2170static struct dev_pm_ops ccw_pm_ops = {
2171 .prepare = ccw_device_pm_prepare,
2172 .complete = ccw_device_pm_complete,
2173 .freeze = ccw_device_pm_freeze,
2174 .thaw = ccw_device_pm_thaw,
2175 .restore = ccw_device_pm_restore,
2176};
2177
Cornelia Huck8bbace72006-01-11 10:56:22 +01002178struct bus_type ccw_bus_type = {
2179 .name = "ccw",
2180 .match = ccw_bus_match,
2181 .uevent = ccw_uevent,
2182 .probe = ccw_device_probe,
2183 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002184 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002185 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002186};
2187
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002188/**
2189 * ccw_driver_register() - register a ccw driver
2190 * @cdriver: driver to be registered
2191 *
2192 * This function is mainly a wrapper around driver_register().
2193 * Returns:
2194 * %0 on success and a negative error value on failure.
2195 */
2196int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 struct device_driver *drv = &cdriver->driver;
2199
2200 drv->bus = &ccw_bus_type;
2201 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01002202 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 return driver_register(drv);
2205}
2206
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002207/**
2208 * ccw_driver_unregister() - deregister a ccw driver
2209 * @cdriver: driver to be deregistered
2210 *
2211 * This function is mainly a wrapper around driver_unregister().
2212 */
2213void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 driver_unregister(&cdriver->driver);
2216}
2217
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002218/* Helper func for qdio. */
2219struct subchannel_id
2220ccw_device_get_subchannel_id(struct ccw_device *cdev)
2221{
2222 struct subchannel *sch;
2223
2224 sch = to_subchannel(cdev->dev.parent);
2225 return sch->schid;
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228MODULE_LICENSE("GPL");
2229EXPORT_SYMBOL(ccw_device_set_online);
2230EXPORT_SYMBOL(ccw_device_set_offline);
2231EXPORT_SYMBOL(ccw_driver_register);
2232EXPORT_SYMBOL(ccw_driver_unregister);
2233EXPORT_SYMBOL(get_ccwdev_by_busid);
2234EXPORT_SYMBOL(ccw_bus_type);
2235EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002236EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);