blob: 0dcfc0ee3d812b3d65363408b4c3a48c212bf9bf [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);
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200134static void recovery_func(unsigned long data);
135struct workqueue_struct *ccw_device_work;
136wait_queue_head_t ccw_device_init_wq;
137atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Cornelia Huckf08adc02008-07-14 09:59:03 +0200139static struct css_device_id io_subchannel_ids[] = {
140 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
141 { /* end of list */ },
142};
143MODULE_DEVICE_TABLE(css, io_subchannel_ids);
144
Cornelia Huck93a27592009-06-16 10:30:23 +0200145static int io_subchannel_prepare(struct subchannel *sch)
146{
147 struct ccw_device *cdev;
148 /*
149 * Don't allow suspend while a ccw device registration
150 * is still outstanding.
151 */
152 cdev = sch_get_cdev(sch);
153 if (cdev && !device_is_registered(&cdev->dev))
154 return -EAGAIN;
155 return 0;
156}
157
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200158static void io_subchannel_settle(void)
159{
160 wait_event(ccw_device_init_wq,
161 atomic_read(&ccw_device_init_count) == 0);
162 flush_workqueue(ccw_device_work);
163}
164
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200165static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100166 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200167 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100168 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200170 .sch_event = io_subchannel_sch_event,
171 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100172 .probe = io_subchannel_probe,
173 .remove = io_subchannel_remove,
174 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200175 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200176 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Sebastian Ott2f176442009-09-22 22:58:33 +0200179int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int ret;
182
183 init_waitqueue_head(&ccw_device_init_wq);
184 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100185 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 ccw_device_work = create_singlethread_workqueue("cio");
188 if (!ccw_device_work)
Sebastian Ott2f176442009-09-22 22:58:33 +0200189 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 slow_path_wq = create_singlethread_workqueue("kslowcrw");
191 if (!slow_path_wq) {
Sebastian Ott2f176442009-09-22 22:58:33 +0200192 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out_err;
194 }
195 if ((ret = bus_register (&ccw_bus_type)))
196 goto out_err;
197
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100198 ret = css_driver_register(&io_subchannel_driver);
199 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out_err;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203out_err:
204 if (ccw_device_work)
205 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (slow_path_wq)
207 destroy_workqueue(slow_path_wq);
208 return ret;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/************************ device handling **************************/
213
214/*
215 * A ccw_device has some interfaces in sysfs in addition to the
216 * standard ones.
217 * The following entries are designed to export the information which
218 * resided in 2.4 in /proc/subchannels. Subchannel and device number
219 * are obvious, so they don't have an entry :)
220 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
221 */
222static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400223chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ssize_t ret = 0;
228 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200229 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200231 for (chp = 0; chp < 8; chp++) {
232 mask = 0x80 >> chp;
233 if (ssd->path_mask & mask)
234 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
235 else
236 ret += sprintf(buf + ret, "00 ");
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ret += sprintf (buf+ret, "\n");
239 return min((ssize_t)PAGE_SIZE, ret);
240}
241
242static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400243pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct subchannel *sch = to_subchannel(dev);
246 struct pmcw *pmcw = &sch->schib.pmcw;
247
248 return sprintf (buf, "%02x %02x %02x\n",
249 pmcw->pim, pmcw->pam, pmcw->pom);
250}
251
252static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400253devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct ccw_device *cdev = to_ccwdev(dev);
256 struct ccw_device_id *id = &(cdev->id);
257
258 if (id->dev_type != 0)
259 return sprintf(buf, "%04x/%02x\n",
260 id->dev_type, id->dev_model);
261 else
262 return sprintf(buf, "n/a\n");
263}
264
265static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400266cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct ccw_device *cdev = to_ccwdev(dev);
269 struct ccw_device_id *id = &(cdev->id);
270
271 return sprintf(buf, "%04x/%02x\n",
272 id->cu_type, id->cu_model);
273}
274
275static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800276modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
277{
278 struct ccw_device *cdev = to_ccwdev(dev);
279 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200280 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800281
Cornelia Huck086a6c62007-07-17 13:36:08 +0200282 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200283
284 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800285}
286
287static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400288online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct ccw_device *cdev = to_ccwdev(dev);
291
292 return sprintf(buf, cdev->online ? "1\n" : "0\n");
293}
294
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100295int ccw_device_is_orphan(struct ccw_device *cdev)
296{
297 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
298}
299
Cornelia Huckef995162007-04-27 16:01:39 +0200300static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100301{
Sebastian Ott3b554a12009-09-11 10:28:26 +0200302 if (test_and_clear_bit(1, &cdev->private->registered)) {
Cornelia Huckef995162007-04-27 16:01:39 +0200303 device_del(&cdev->dev);
Sebastian Ott3b554a12009-09-11 10:28:26 +0200304 /* Release reference from device_initialize(). */
305 put_device(&cdev->dev);
306 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100307}
308
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200309static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200310{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200311 struct ccw_device_private *priv;
312 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200313
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200314 priv = container_of(work, struct ccw_device_private, kick_work);
315 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200316 ccw_device_unregister(cdev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200317 /* Release cdev reference for workqueue processing. */
318 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200319}
320
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200321static void
322ccw_device_remove_disconnected(struct ccw_device *cdev)
323{
324 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200325
326 /*
327 * Forced offline in disconnected state means
328 * 'throw away device'.
329 */
330 if (ccw_device_is_orphan(cdev)) {
331 /*
332 * Deregister ccw device.
333 * Unfortunately, we cannot do this directly from the
334 * attribute method.
335 */
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +0200336 /* Get cdev reference for workqueue processing. */
337 if (!get_device(&cdev->dev))
338 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200339 spin_lock_irqsave(cdev->ccwlock, flags);
340 cdev->private->state = DEV_STATE_NOT_OPER;
341 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200342 PREPARE_WORK(&cdev->private->kick_work,
343 ccw_device_remove_orphan_cb);
Sebastian Ottc4621a62009-03-31 19:16:04 +0200344 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200345 } else
346 /* Deregister subchannel, which will kill the ccw device. */
Sebastian Ottc4621a62009-03-31 19:16:04 +0200347 ccw_device_schedule_sch_unregister(cdev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200348}
349
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200350/**
351 * ccw_device_set_offline() - disable a ccw device for I/O
352 * @cdev: target ccw device
353 *
354 * This function calls the driver's set_offline() function for @cdev, if
355 * given, and then disables @cdev.
356 * Returns:
357 * %0 on success and a negative error value on failure.
358 * Context:
359 * enabled, ccw device lock not held
360 */
361int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int ret;
364
365 if (!cdev)
366 return -ENODEV;
367 if (!cdev->online || !cdev->drv)
368 return -EINVAL;
369
370 if (cdev->drv->set_offline) {
371 ret = cdev->drv->set_offline(cdev);
372 if (ret != 0)
373 return ret;
374 }
375 cdev->online = 0;
376 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200377 /* Wait until a final state or DISCONNECTED is reached */
378 while (!dev_fsm_final_state(cdev) &&
379 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200381 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
382 cdev->private->state == DEV_STATE_DISCONNECTED));
383 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200385 ret = ccw_device_offline(cdev);
386 if (ret)
387 goto error;
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 /* Give up reference from ccw_device_set_online(). */
392 put_device(&cdev->dev);
393 return 0;
394
395error:
396 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
397 ret, cdev->private->dev_id.ssid,
398 cdev->private->dev_id.devno);
399 cdev->private->state = DEV_STATE_OFFLINE;
400 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
401 spin_unlock_irq(cdev->ccwlock);
402 /* Give up reference from ccw_device_set_online(). */
403 put_device(&cdev->dev);
404 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200407/**
408 * ccw_device_set_online() - enable a ccw device for I/O
409 * @cdev: target ccw device
410 *
411 * This function first enables @cdev and then calls the driver's set_online()
412 * function for @cdev, if given. If set_online() returns an error, @cdev is
413 * disabled again.
414 * Returns:
415 * %0 on success and a negative error value on failure.
416 * Context:
417 * enabled, ccw device lock not held
418 */
419int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200422 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (!cdev)
425 return -ENODEV;
426 if (cdev->online || !cdev->drv)
427 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100428 /* Hold on to an extra reference while device is online. */
429 if (!get_device(&cdev->dev))
430 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_online(cdev);
434 spin_unlock_irq(cdev->ccwlock);
435 if (ret == 0)
436 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
437 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200438 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100442 /* Give up online reference since onlining failed. */
443 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return ret;
445 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200446 spin_lock_irq(cdev->ccwlock);
447 /* Check if online processing was successful */
448 if ((cdev->private->state != DEV_STATE_ONLINE) &&
449 (cdev->private->state != DEV_STATE_W4SENSE)) {
450 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100451 /* Give up online reference since onlining failed. */
452 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200456 if (cdev->drv->set_online)
457 ret = cdev->drv->set_online(cdev);
458 if (ret)
459 goto rollback;
460 cdev->online = 1;
461 return 0;
462
463rollback:
464 spin_lock_irq(cdev->ccwlock);
465 /* Wait until a final state or DISCONNECTED is reached */
466 while (!dev_fsm_final_state(cdev) &&
467 cdev->private->state != DEV_STATE_DISCONNECTED) {
468 spin_unlock_irq(cdev->ccwlock);
469 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
470 cdev->private->state == DEV_STATE_DISCONNECTED));
471 spin_lock_irq(cdev->ccwlock);
472 }
473 ret2 = ccw_device_offline(cdev);
474 if (ret2)
475 goto error;
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));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100479 /* Give up online reference since onlining failed. */
480 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200481 return ret;
482
483error:
484 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
485 "device 0.%x.%04x\n",
486 ret2, cdev->private->dev_id.ssid,
487 cdev->private->dev_id.devno);
488 cdev->private->state = DEV_STATE_OFFLINE;
489 spin_unlock_irq(cdev->ccwlock);
490 /* Give up online reference since onlining failed. */
491 put_device(&cdev->dev);
492 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100495static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496{
497 if (cdev->private->state == DEV_STATE_DISCONNECTED)
498 ccw_device_remove_disconnected(cdev);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100499 else if (cdev->online && cdev->drv && cdev->drv->set_offline)
500 return ccw_device_set_offline(cdev);
501 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200502}
503
504static int online_store_recog_and_online(struct ccw_device *cdev)
505{
506 int ret;
507
508 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200509 if (cdev->private->state == DEV_STATE_BOXED) {
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200510 ret = ccw_device_recognition(cdev);
511 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200512 CIO_MSG_EVENT(0, "Couldn't start recognition "
513 "for device 0.%x.%04x (ret=%d)\n",
514 cdev->private->dev_id.ssid,
515 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516 return ret;
517 }
518 wait_event(cdev->private->wait_q,
519 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200520 if (cdev->private->state != DEV_STATE_OFFLINE)
521 /* recognition failed */
522 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200523 }
524 if (cdev->drv && cdev->drv->set_online)
525 ccw_device_set_online(cdev);
526 return 0;
527}
Sebastian Ott156013f2009-03-31 19:16:03 +0200528
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200529static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200530{
531 int ret;
532
533 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200534 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200535 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200536 if (force && cdev->private->state == DEV_STATE_BOXED) {
537 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200538 if (ret)
539 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200540 if (cdev->id.cu_type == 0)
541 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200542 ret = online_store_recog_and_online(cdev);
543 if (ret)
544 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200545 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200546 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200547}
548
549static ssize_t online_store (struct device *dev, struct device_attribute *attr,
550 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200553 int force, ret;
554 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200556 if ((cdev->private->state != DEV_STATE_OFFLINE &&
557 cdev->private->state != DEV_STATE_ONLINE &&
558 cdev->private->state != DEV_STATE_BOXED &&
559 cdev->private->state != DEV_STATE_DISCONNECTED) ||
560 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EAGAIN;
562
563 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
564 atomic_set(&cdev->private->onoff, 0);
565 return -EINVAL;
566 }
567 if (!strncmp(buf, "force\n", count)) {
568 force = 1;
569 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200570 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else {
572 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200573 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200575 if (ret)
576 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200577 switch (i) {
578 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100579 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200580 break;
581 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200582 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200583 break;
584 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200585 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (cdev->drv)
589 module_put(cdev->drv->owner);
590 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100591 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400595available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct ccw_device *cdev = to_ccwdev(dev);
598 struct subchannel *sch;
599
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100600 if (ccw_device_is_orphan(cdev))
601 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 switch (cdev->private->state) {
603 case DEV_STATE_BOXED:
604 return sprintf(buf, "boxed\n");
605 case DEV_STATE_DISCONNECTED:
606 case DEV_STATE_DISCONNECTED_SENSE_ID:
607 case DEV_STATE_NOT_OPER:
608 sch = to_subchannel(dev->parent);
609 if (!sch->lpm)
610 return sprintf(buf, "no path\n");
611 else
612 return sprintf(buf, "no device\n");
613 default:
614 /* All other states considered fine. */
615 return sprintf(buf, "good\n");
616 }
617}
618
619static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
620static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
621static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
622static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800623static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static DEVICE_ATTR(availability, 0444, available_show, NULL);
626
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200627static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 &dev_attr_chpids.attr,
629 &dev_attr_pimpampom.attr,
630 NULL,
631};
632
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200633static struct attribute_group io_subchannel_attr_group = {
634 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100635};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637static struct attribute * ccwdev_attrs[] = {
638 &dev_attr_devtype.attr,
639 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800640 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 &dev_attr_online.attr,
642 &dev_attr_cmb_enable.attr,
643 &dev_attr_availability.attr,
644 NULL,
645};
646
647static struct attribute_group ccwdev_attr_group = {
648 .attrs = ccwdev_attrs,
649};
650
David Brownella4dbd672009-06-24 10:06:31 -0700651static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200652 &ccwdev_attr_group,
653 NULL,
654};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656/* this is a simple abstraction for device_register that sets the
657 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200658static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct device *dev = &cdev->dev;
661 int ret;
662
663 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200664 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
665 cdev->private->dev_id.devno);
666 if (ret)
667 return ret;
668 ret = device_add(dev);
669 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return ret;
671
672 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return ret;
674}
675
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100676static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700677{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100678 struct ccw_device *cdev = to_ccwdev(dev);
679 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700680
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100681 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
682}
683
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100684static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100685{
686 struct device *dev;
687
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100688 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100689
690 return dev ? to_ccwdev(dev) : NULL;
691}
692
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100693void ccw_device_do_unbind_bind(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100695 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct ccw_device *cdev;
697 struct subchannel *sch;
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100700 priv = container_of(work, struct ccw_device_private, kick_work);
701 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100704 if (test_bit(1, &cdev->private->registered)) {
705 device_release_driver(&cdev->dev);
706 ret = device_attach(&cdev->dev);
707 WARN_ON(ret == -ENODEV);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static void
712ccw_device_release(struct device *dev)
713{
714 struct ccw_device *cdev;
715
716 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100717 /* Release reference of parent subchannel. */
718 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 kfree(cdev->private);
720 kfree(cdev);
721}
722
Cornelia Huck7674da72006-12-08 15:54:21 +0100723static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
724{
725 struct ccw_device *cdev;
726
727 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
728 if (cdev) {
729 cdev->private = kzalloc(sizeof(struct ccw_device_private),
730 GFP_KERNEL | GFP_DMA);
731 if (cdev->private)
732 return cdev;
733 }
734 kfree(cdev);
735 return ERR_PTR(-ENOMEM);
736}
737
738static int io_subchannel_initialize_dev(struct subchannel *sch,
739 struct ccw_device *cdev)
740{
741 cdev->private->cdev = cdev;
742 atomic_set(&cdev->private->onoff, 0);
743 cdev->dev.parent = &sch->dev;
744 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100745 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200746 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100747 /* Do first half of device_register. */
748 device_initialize(&cdev->dev);
749 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100750 /* Release reference from device_initialize(). */
751 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100752 return -ENODEV;
753 }
754 return 0;
755}
756
757static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
758{
759 struct ccw_device *cdev;
760 int ret;
761
762 cdev = io_subchannel_allocate_dev(sch);
763 if (!IS_ERR(cdev)) {
764 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200765 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100766 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100767 }
768 return cdev;
769}
770
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100771static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
772
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100773static void sch_create_and_recog_new_device(struct subchannel *sch)
774{
775 struct ccw_device *cdev;
776
777 /* Need to allocate a new ccw device. */
778 cdev = io_subchannel_create_ccwdev(sch);
779 if (IS_ERR(cdev)) {
780 /* OK, we did everything we could... */
781 css_sch_device_unregister(sch);
782 return;
783 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100784 /* Start recognition for the new ccw device. */
785 if (io_subchannel_recog(cdev, sch)) {
786 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100787 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100788 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100789 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100790 /* Put reference from io_subchannel_create_ccwdev(). */
791 put_device(&sch->dev);
792 /* Give up initial reference. */
793 put_device(&cdev->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100794 }
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/*
798 * Register recognized device.
799 */
800static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100801io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100803 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct ccw_device *cdev;
805 struct subchannel *sch;
806 int ret;
807 unsigned long flags;
808
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100809 priv = container_of(work, struct ccw_device_private, kick_work);
810 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100812 /*
813 * Check if subchannel is still registered. It may have become
814 * unregistered if a machine check hit us after finishing
815 * device recognition but before the register work could be
816 * queued.
817 */
818 if (!device_is_registered(&sch->dev))
819 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200820 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100821 /*
822 * io_subchannel_register() will also be called after device
823 * recognition has been done for a boxed device (which will already
824 * be registered). We need to reprobe since we may now have sense id
825 * information.
826 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100827 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100828 if (!cdev->drv) {
829 ret = device_reprobe(&cdev->dev);
830 if (ret)
831 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200832 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200833 " %d for 0.%x.%04x\n", ret,
834 cdev->private->dev_id.ssid,
835 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto out;
838 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700839 /*
840 * Now we know this subchannel will stay, we can throw
841 * our delayed uevent.
842 */
Ming Leif67f1292009-03-01 21:10:49 +0800843 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700844 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* make it known to the system */
846 ret = ccw_device_register(cdev);
847 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200848 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
849 cdev->private->dev_id.ssid,
850 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100851 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100852 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100853 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100854 /* Release initial device reference. */
855 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100856 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out:
859 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100861out_err:
862 /* Release reference for workqueue processing. */
863 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (atomic_dec_and_test(&ccw_device_init_count))
865 wake_up(&ccw_device_init_wq);
866}
867
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200868static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100870 struct ccw_device_private *priv;
871 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct subchannel *sch;
873
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100874 priv = container_of(work, struct ccw_device_private, kick_work);
875 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200876 /* Get subchannel reference for local processing. */
877 if (!get_device(cdev->dev.parent))
878 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200880 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200881 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200883 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 put_device(&sch->dev);
885}
886
Sebastian Ottc4621a62009-03-31 19:16:04 +0200887void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
888{
Sebastian Ottbe7a2dd2009-09-11 10:28:20 +0200889 /* Get cdev reference for workqueue processing. */
890 if (!get_device(&cdev->dev))
891 return;
Sebastian Ottc4621a62009-03-31 19:16:04 +0200892 PREPARE_WORK(&cdev->private->kick_work,
893 ccw_device_call_sch_unregister);
894 queue_work(slow_path_wq, &cdev->private->kick_work);
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/*
898 * subchannel recognition done. Called from the state machine.
899 */
900void
901io_subchannel_recog_done(struct ccw_device *cdev)
902{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (css_init_done == 0) {
904 cdev->private->flags.recog_done = 1;
905 return;
906 }
907 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200908 case DEV_STATE_BOXED:
909 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case DEV_STATE_NOT_OPER:
911 cdev->private->flags.recog_done = 1;
Sebastian Ottc4621a62009-03-31 19:16:04 +0200912 ccw_device_schedule_sch_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (atomic_dec_and_test(&ccw_device_init_count))
914 wake_up(&ccw_device_init_wq);
915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 case DEV_STATE_OFFLINE:
917 /*
918 * We can't register the device in interrupt context so
919 * we schedule a work item.
920 */
921 if (!get_device(&cdev->dev))
922 break;
923 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100924 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 queue_work(slow_path_wq, &cdev->private->kick_work);
926 break;
927 }
928}
929
930static int
931io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
932{
933 int rc;
934 struct ccw_device_private *priv;
935
Cornelia Huck2ec22982006-12-08 15:54:26 +0100936 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* Init private data. */
939 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200940 priv->dev_id.devno = sch->schib.pmcw.dev;
941 priv->dev_id.ssid = sch->schid.ssid;
942 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 priv->state = DEV_STATE_NOT_OPER;
944 INIT_LIST_HEAD(&priv->cmb_list);
945 init_waitqueue_head(&priv->wait_q);
946 init_timer(&priv->timer);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* Increase counter of devices currently in recognition. */
949 atomic_inc(&ccw_device_init_count);
950
951 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100952 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100953 sch_set_cdev(sch, cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100955 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (rc) {
957 if (atomic_dec_and_test(&ccw_device_init_count))
958 wake_up(&ccw_device_init_wq);
959 }
960 return rc;
961}
962
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100963static int ccw_device_move_to_sch(struct ccw_device *cdev,
964 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100965{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100966 struct subchannel *old_sch;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100967 int rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100968
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100969 old_sch = to_subchannel(cdev->dev.parent);
970 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100971 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100972 return -ENODEV;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100973 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100974 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100975 mutex_unlock(&sch->reg_mutex);
976 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100977 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100978 cdev->private->dev_id.ssid,
979 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100980 sch->schib.pmcw.dev, rc);
981 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100982 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100983 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100984 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100985 /* Clean up old subchannel. */
986 if (!sch_is_pseudo_sch(old_sch)) {
987 spin_lock_irq(old_sch->lock);
988 sch_set_cdev(old_sch, NULL);
989 cio_disable_subchannel(old_sch);
990 spin_unlock_irq(old_sch->lock);
991 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100992 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100993 /* Release child reference for old parent. */
994 put_device(&old_sch->dev);
995 /* Initialize new subchannel. */
996 spin_lock_irq(sch->lock);
997 cdev->private->schid = sch->schid;
998 cdev->ccwlock = sch->lock;
999 if (!sch_is_pseudo_sch(sch))
1000 sch_set_cdev(sch, cdev);
1001 spin_unlock_irq(sch->lock);
1002 if (!sch_is_pseudo_sch(sch))
1003 css_update_ssd_info(sch);
1004 return 0;
1005}
1006
1007static int ccw_device_move_to_orph(struct ccw_device *cdev)
1008{
1009 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1010 struct channel_subsystem *css = to_css(sch->dev.parent);
1011
1012 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001013}
1014
Cornelia Huck602b20f2008-01-26 14:10:39 +01001015static void io_subchannel_irq(struct subchannel *sch)
1016{
1017 struct ccw_device *cdev;
1018
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001019 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001020
Sebastian Ottefd986d2009-09-11 10:28:18 +02001021 CIO_TRACE_EVENT(6, "IRQ");
1022 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001023 if (cdev)
1024 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1025}
1026
Sebastian Ott13952ec2008-12-25 13:39:13 +01001027void io_subchannel_init_config(struct subchannel *sch)
1028{
1029 memset(&sch->config, 0, sizeof(sch->config));
1030 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +01001031 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
1032 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +01001033 sch->config.mp = 1;
1034}
1035
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001036static void io_subchannel_init_fields(struct subchannel *sch)
1037{
1038 if (cio_is_console(sch->schid))
1039 sch->opm = 0xff;
1040 else
1041 sch->opm = chp_get_sch_opm(sch);
1042 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001043 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001044
1045 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1046 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1047 sch->schib.pmcw.dev, sch->schid.ssid,
1048 sch->schid.sch_no, sch->schib.pmcw.pim,
1049 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001050
1051 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001052}
1053
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001054/*
1055 * Note: We always return 0 so that we bind to the device even on error.
1056 * This is needed so that our remove function is called on unregister.
1057 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001058static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct ccw_device *cdev;
1061 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001063 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001064 rc = sysfs_create_group(&sch->dev.kobj,
1065 &io_subchannel_attr_group);
1066 if (rc)
1067 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1068 "attributes for subchannel "
1069 "0.%x.%04x (rc=%d)\n",
1070 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001072 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001073 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001074 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Ming Leif67f1292009-03-01 21:10:49 +08001076 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001077 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001078 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001079 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 device_initialize(&cdev->dev);
1081 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
1083 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001084 * the reference count needs to be corrected since we
1085 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
1087 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1088 cdev->private->state != DEV_STATE_OFFLINE &&
1089 cdev->private->state != DEV_STATE_BOXED)
1090 get_device(&cdev->dev);
1091 return 0;
1092 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001093 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001094 rc = cio_commit_config(sch);
1095 if (rc)
1096 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001097 rc = sysfs_create_group(&sch->dev.kobj,
1098 &io_subchannel_attr_group);
1099 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001100 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001101 /* Allocate I/O subchannel private data. */
1102 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1103 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001104 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001105 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001106 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001107 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001108
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001109out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001110 spin_lock_irq(sch->lock);
1111 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1112 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001117io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct ccw_device *cdev;
1120 unsigned long flags;
1121
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001122 cdev = sch_get_cdev(sch);
1123 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001124 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* Set ccw device to not operational and drop reference. */
1126 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001127 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 cdev->private->state = DEV_STATE_NOT_OPER;
1129 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001130 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001131out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001132 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001133 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135}
1136
Cornelia Huck602b20f2008-01-26 14:10:39 +01001137static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct ccw_device *cdev;
1140
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001141 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (cdev)
1143 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1144}
1145
Cornelia Huckc820de32008-07-14 09:58:45 +02001146static int check_for_io_on_path(struct subchannel *sch, int mask)
1147{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001148 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001149 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001150 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001151 return 1;
1152 return 0;
1153}
1154
1155static void terminate_internal_io(struct subchannel *sch,
1156 struct ccw_device *cdev)
1157{
1158 if (cio_clear(sch)) {
1159 /* Recheck device in case clear failed. */
1160 sch->lpm = 0;
1161 if (cdev->online)
1162 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1163 else
1164 css_schedule_eval(sch->schid);
1165 return;
1166 }
1167 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1168 /* Request retry of internal operation. */
1169 cdev->private->flags.intretry = 1;
1170 /* Call handler. */
1171 if (cdev->handler)
1172 cdev->handler(cdev, cdev->private->intparm,
1173 ERR_PTR(-EIO));
1174}
1175
1176static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 struct ccw_device *cdev;
1179
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001180 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!cdev)
1182 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001183 if (check_for_io_on_path(sch, mask)) {
1184 if (cdev->private->state == DEV_STATE_ONLINE)
1185 ccw_device_kill_io(cdev);
1186 else {
1187 terminate_internal_io(sch, cdev);
1188 /* Re-start path verification. */
1189 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1190 }
1191 } else
1192 /* trigger path verification. */
1193 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1194
1195}
1196
Cornelia Huck99611f82008-07-14 09:59:02 +02001197static int io_subchannel_chp_event(struct subchannel *sch,
1198 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001199{
1200 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001201
Cornelia Huck99611f82008-07-14 09:59:02 +02001202 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001203 if (!mask)
1204 return 0;
1205 switch (event) {
1206 case CHP_VARY_OFF:
1207 sch->opm &= ~mask;
1208 sch->lpm &= ~mask;
1209 io_subchannel_terminate_path(sch, mask);
1210 break;
1211 case CHP_VARY_ON:
1212 sch->opm |= mask;
1213 sch->lpm |= mask;
1214 io_subchannel_verify(sch);
1215 break;
1216 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001217 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001218 return -ENODEV;
1219 io_subchannel_terminate_path(sch, mask);
1220 break;
1221 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001222 if (cio_update_schib(sch))
1223 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001224 sch->lpm |= mask & sch->opm;
1225 io_subchannel_verify(sch);
1226 break;
1227 }
1228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001232io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct ccw_device *cdev;
1235 int ret;
1236
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001237 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001239 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 if (!sch->schib.pmcw.ena)
1242 /* Nothing to do. */
1243 return;
1244 ret = cio_disable_subchannel(sch);
1245 if (ret != -EBUSY)
1246 /* Subchannel is disabled, we're done. */
1247 return;
1248 cdev->private->state = DEV_STATE_QUIESCE;
1249 if (cdev->handler)
1250 cdev->handler(cdev, cdev->private->intparm,
1251 ERR_PTR(-EIO));
1252 ret = ccw_device_cancel_halt_clear(cdev);
1253 if (ret == -EBUSY) {
1254 ccw_device_set_timeout(cdev, HZ/10);
1255 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1256 }
1257 cio_disable_subchannel(sch);
1258}
1259
Cornelia Huckc820de32008-07-14 09:58:45 +02001260static int device_is_disconnected(struct ccw_device *cdev)
1261{
1262 if (!cdev)
1263 return 0;
1264 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1265 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1266}
1267
1268static int recovery_check(struct device *dev, void *data)
1269{
1270 struct ccw_device *cdev = to_ccwdev(dev);
1271 int *redo = data;
1272
1273 spin_lock_irq(cdev->ccwlock);
1274 switch (cdev->private->state) {
1275 case DEV_STATE_DISCONNECTED:
1276 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1277 cdev->private->dev_id.ssid,
1278 cdev->private->dev_id.devno);
1279 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1280 *redo = 1;
1281 break;
1282 case DEV_STATE_DISCONNECTED_SENSE_ID:
1283 *redo = 1;
1284 break;
1285 }
1286 spin_unlock_irq(cdev->ccwlock);
1287
1288 return 0;
1289}
1290
1291static void recovery_work_func(struct work_struct *unused)
1292{
1293 int redo = 0;
1294
1295 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1296 if (redo) {
1297 spin_lock_irq(&recovery_lock);
1298 if (!timer_pending(&recovery_timer)) {
1299 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1300 recovery_phase++;
1301 mod_timer(&recovery_timer, jiffies +
1302 recovery_delay[recovery_phase] * HZ);
1303 }
1304 spin_unlock_irq(&recovery_lock);
1305 } else
1306 CIO_MSG_EVENT(4, "recovery: end\n");
1307}
1308
1309static DECLARE_WORK(recovery_work, recovery_work_func);
1310
1311static void recovery_func(unsigned long data)
1312{
1313 /*
1314 * We can't do our recovery in softirq context and it's not
1315 * performance critical, so we schedule it.
1316 */
1317 schedule_work(&recovery_work);
1318}
1319
1320static void ccw_device_schedule_recovery(void)
1321{
1322 unsigned long flags;
1323
1324 CIO_MSG_EVENT(4, "recovery: schedule\n");
1325 spin_lock_irqsave(&recovery_lock, flags);
1326 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1327 recovery_phase = 0;
1328 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1329 }
1330 spin_unlock_irqrestore(&recovery_lock, flags);
1331}
1332
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001333static int purge_fn(struct device *dev, void *data)
1334{
1335 struct ccw_device *cdev = to_ccwdev(dev);
1336 struct ccw_device_private *priv = cdev->private;
1337 int unreg;
1338
1339 spin_lock_irq(cdev->ccwlock);
1340 unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
1341 (priv->state == DEV_STATE_OFFLINE);
1342 spin_unlock_irq(cdev->ccwlock);
1343 if (!unreg)
1344 goto out;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001345 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
1346 priv->dev_id.devno);
Sebastian Ottc4621a62009-03-31 19:16:04 +02001347 ccw_device_schedule_sch_unregister(cdev);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001348
1349out:
1350 /* Abort loop in case of pending signal. */
1351 if (signal_pending(current))
1352 return -EINTR;
1353
1354 return 0;
1355}
1356
1357/**
1358 * ccw_purge_blacklisted - purge unused, blacklisted devices
1359 *
1360 * Unregister all ccw devices that are offline and on the blacklist.
1361 */
1362int ccw_purge_blacklisted(void)
1363{
1364 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1365 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1366 return 0;
1367}
1368
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001369void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001370{
1371 if (!cdev)
1372 return;
1373 ccw_device_set_timeout(cdev, 0);
1374 cdev->private->flags.fake_irb = 0;
1375 cdev->private->state = DEV_STATE_DISCONNECTED;
1376 if (cdev->online)
1377 ccw_device_schedule_recovery();
1378}
1379
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001380void ccw_device_set_notoper(struct ccw_device *cdev)
1381{
1382 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1383
1384 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001385 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001386 ccw_device_set_timeout(cdev, 0);
1387 cio_disable_subchannel(sch);
1388 cdev->private->state = DEV_STATE_NOT_OPER;
1389}
1390
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001391enum io_sch_action {
1392 IO_SCH_UNREG,
1393 IO_SCH_ORPH_UNREG,
1394 IO_SCH_ATTACH,
1395 IO_SCH_UNREG_ATTACH,
1396 IO_SCH_ORPH_ATTACH,
1397 IO_SCH_REPROBE,
1398 IO_SCH_VERIFY,
1399 IO_SCH_DISC,
1400 IO_SCH_NOP,
1401};
1402
1403static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001404{
Cornelia Huckc820de32008-07-14 09:58:45 +02001405 struct ccw_device *cdev;
1406
Cornelia Huckc820de32008-07-14 09:58:45 +02001407 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001408 if (cio_update_schib(sch)) {
1409 /* Not operational. */
1410 if (!cdev)
1411 return IO_SCH_UNREG;
1412 if (!ccw_device_notify(cdev, CIO_GONE))
1413 return IO_SCH_UNREG;
1414 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001415 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001416 /* Operational. */
1417 if (!cdev)
1418 return IO_SCH_ATTACH;
1419 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1420 if (!ccw_device_notify(cdev, CIO_GONE))
1421 return IO_SCH_UNREG_ATTACH;
1422 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001423 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001424 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1425 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1426 return IO_SCH_UNREG;
1427 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001428 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001429 if (device_is_disconnected(cdev))
1430 return IO_SCH_REPROBE;
1431 if (cdev->online)
1432 return IO_SCH_VERIFY;
1433 return IO_SCH_NOP;
1434}
1435
1436/**
1437 * io_subchannel_sch_event - process subchannel event
1438 * @sch: subchannel
1439 * @process: non-zero if function is called in process context
1440 *
1441 * An unspecified event occurred for this subchannel. Adjust data according
1442 * to the current operational state of the subchannel and device. Return
1443 * zero when the event has been handled sufficiently or -EAGAIN when this
1444 * function should be called again in process context.
1445 */
1446static int io_subchannel_sch_event(struct subchannel *sch, int process)
1447{
1448 unsigned long flags;
1449 struct ccw_device *cdev;
1450 struct ccw_dev_id dev_id;
1451 enum io_sch_action action;
1452 int rc = -EAGAIN;
1453
1454 spin_lock_irqsave(sch->lock, flags);
1455 if (!device_is_registered(&sch->dev))
1456 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001457 if (work_pending(&sch->todo_work))
1458 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001459 action = sch_get_action(sch);
1460 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1461 sch->schid.ssid, sch->schid.sch_no, process,
1462 action);
1463 /* Perform immediate actions while holding the lock. */
1464 cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001465 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001466 case IO_SCH_REPROBE:
1467 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001468 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001469 rc = 0;
1470 goto out_unlock;
1471 case IO_SCH_VERIFY:
1472 /* Trigger path verification. */
1473 io_subchannel_verify(sch);
1474 rc = 0;
1475 goto out_unlock;
1476 case IO_SCH_DISC:
1477 ccw_device_set_disconnected(cdev);
1478 rc = 0;
1479 goto out_unlock;
1480 case IO_SCH_ORPH_UNREG:
1481 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001482 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001483 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001484 case IO_SCH_UNREG_ATTACH:
1485 case IO_SCH_UNREG:
1486 if (cdev)
1487 ccw_device_set_notoper(cdev);
1488 break;
1489 case IO_SCH_NOP:
1490 rc = 0;
1491 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001492 default:
1493 break;
1494 }
1495 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001496 /* All other actions require process context. */
1497 if (!process)
1498 goto out;
1499 /* Handle attached ccw device. */
1500 switch (action) {
1501 case IO_SCH_ORPH_UNREG:
1502 case IO_SCH_ORPH_ATTACH:
1503 /* Move ccw device to orphanage. */
1504 rc = ccw_device_move_to_orph(cdev);
1505 if (rc)
1506 goto out;
1507 break;
1508 case IO_SCH_UNREG_ATTACH:
1509 /* Unregister ccw device. */
1510 ccw_device_unregister(cdev);
1511 break;
1512 default:
1513 break;
1514 }
1515 /* Handle subchannel. */
1516 switch (action) {
1517 case IO_SCH_ORPH_UNREG:
1518 case IO_SCH_UNREG:
1519 css_sch_device_unregister(sch);
1520 break;
1521 case IO_SCH_ORPH_ATTACH:
1522 case IO_SCH_UNREG_ATTACH:
1523 case IO_SCH_ATTACH:
1524 dev_id.ssid = sch->schid.ssid;
1525 dev_id.devno = sch->schib.pmcw.dev;
1526 cdev = get_ccwdev_by_dev_id(&dev_id);
1527 if (!cdev) {
1528 sch_create_and_recog_new_device(sch);
1529 break;
1530 }
1531 rc = ccw_device_move_to_sch(cdev, sch);
1532 if (rc) {
1533 /* Release reference from get_ccwdev_by_dev_id() */
1534 put_device(&cdev->dev);
1535 goto out;
1536 }
1537 spin_lock_irqsave(sch->lock, flags);
1538 ccw_device_trigger_reprobe(cdev);
1539 spin_unlock_irqrestore(sch->lock, flags);
1540 /* Release reference from get_ccwdev_by_dev_id() */
1541 put_device(&cdev->dev);
1542 break;
1543 default:
1544 break;
1545 }
1546 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001547
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001548out_unlock:
1549 spin_unlock_irqrestore(sch->lock, flags);
1550out:
1551 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554#ifdef CONFIG_CCW_CONSOLE
1555static struct ccw_device console_cdev;
1556static struct ccw_device_private console_private;
1557static int console_cdev_in_use;
1558
Cornelia Huck2ec22982006-12-08 15:54:26 +01001559static DEFINE_SPINLOCK(ccw_console_lock);
1560
1561spinlock_t * cio_get_console_lock(void)
1562{
1563 return &ccw_console_lock;
1564}
1565
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001566static int ccw_device_console_enable(struct ccw_device *cdev,
1567 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 int rc;
1570
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001571 /* Attach subchannel private data. */
1572 sch->private = cio_get_console_priv();
1573 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001574 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001575 rc = cio_commit_config(sch);
1576 if (rc)
1577 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001578 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001580 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 rc = io_subchannel_recog(cdev, sch);
1582 if (rc)
1583 return rc;
1584
1585 /* Now wait for the async. recognition to come to an end. */
1586 spin_lock_irq(cdev->ccwlock);
1587 while (!dev_fsm_final_state(cdev))
1588 wait_cons_dev();
1589 rc = -EIO;
1590 if (cdev->private->state != DEV_STATE_OFFLINE)
1591 goto out_unlock;
1592 ccw_device_online(cdev);
1593 while (!dev_fsm_final_state(cdev))
1594 wait_cons_dev();
1595 if (cdev->private->state != DEV_STATE_ONLINE)
1596 goto out_unlock;
1597 rc = 0;
1598out_unlock:
1599 spin_unlock_irq(cdev->ccwlock);
1600 return 0;
1601}
1602
1603struct ccw_device *
1604ccw_device_probe_console(void)
1605{
1606 struct subchannel *sch;
1607 int ret;
1608
1609 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001610 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 sch = cio_probe_console();
1612 if (IS_ERR(sch)) {
1613 console_cdev_in_use = 0;
1614 return (void *) sch;
1615 }
1616 memset(&console_cdev, 0, sizeof(struct ccw_device));
1617 memset(&console_private, 0, sizeof(struct ccw_device_private));
1618 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001619 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ret = ccw_device_console_enable(&console_cdev, sch);
1621 if (ret) {
1622 cio_release_console();
1623 console_cdev_in_use = 0;
1624 return ERR_PTR(ret);
1625 }
1626 console_cdev.online = 1;
1627 return &console_cdev;
1628}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001629
Martin Schwidefsky66648452009-06-16 10:30:28 +02001630static int ccw_device_pm_restore(struct device *dev);
1631
1632int ccw_device_force_console(void)
1633{
1634 if (!console_cdev_in_use)
1635 return -ENODEV;
1636 return ccw_device_pm_restore(&console_cdev.dev);
1637}
1638EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639#endif
1640
1641/*
1642 * get ccw_device matching the busid, but only if owned by cdrv
1643 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001644static int
1645__ccwdev_check_busid(struct device *dev, void *id)
1646{
1647 char *bus_id;
1648
Cornelia Huck12975ae2006-10-11 15:31:47 +02001649 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001650
Kay Sievers98df67b2008-12-25 13:38:55 +01001651 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001652}
1653
1654
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001655/**
1656 * get_ccwdev_by_busid() - obtain device from a bus id
1657 * @cdrv: driver the device is owned by
1658 * @bus_id: bus id of the device to be searched
1659 *
1660 * This function searches all devices owned by @cdrv for a device with a bus
1661 * id matching @bus_id.
1662 * Returns:
1663 * If a match is found, its reference count of the found device is increased
1664 * and it is returned; else %NULL is returned.
1665 */
1666struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1667 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001669 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct device_driver *drv;
1671
1672 drv = get_driver(&cdrv->driver);
1673 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001674 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001676 dev = driver_find_device(drv, NULL, (void *)bus_id,
1677 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 put_driver(drv);
1679
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001680 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/************************** device driver handling ************************/
1684
1685/* This is the implementation of the ccw_driver class. The probe, remove
1686 * and release methods are initially very similar to the device_driver
1687 * implementations, with the difference that they have ccw_device
1688 * arguments.
1689 *
1690 * A ccw driver also contains the information that is needed for
1691 * device matching.
1692 */
1693static int
1694ccw_device_probe (struct device *dev)
1695{
1696 struct ccw_device *cdev = to_ccwdev(dev);
1697 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1698 int ret;
1699
1700 cdev->drv = cdrv; /* to let the driver call _set_online */
1701
1702 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1703
1704 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001705 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return ret;
1707 }
1708
1709 return 0;
1710}
1711
1712static int
1713ccw_device_remove (struct device *dev)
1714{
1715 struct ccw_device *cdev = to_ccwdev(dev);
1716 struct ccw_driver *cdrv = cdev->drv;
1717 int ret;
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (cdrv->remove)
1720 cdrv->remove(cdev);
1721 if (cdev->online) {
1722 cdev->online = 0;
1723 spin_lock_irq(cdev->ccwlock);
1724 ret = ccw_device_offline(cdev);
1725 spin_unlock_irq(cdev->ccwlock);
1726 if (ret == 0)
1727 wait_event(cdev->private->wait_q,
1728 dev_fsm_final_state(cdev));
1729 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001730 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001731 "device 0.%x.%04x\n",
1732 ret, cdev->private->dev_id.ssid,
1733 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001734 /* Give up reference obtained in ccw_device_set_online(). */
1735 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001738 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
1740}
1741
Cornelia Huck958974fb2007-10-12 16:11:21 +02001742static void ccw_device_shutdown(struct device *dev)
1743{
1744 struct ccw_device *cdev;
1745
1746 cdev = to_ccwdev(dev);
1747 if (cdev->drv && cdev->drv->shutdown)
1748 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001749 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001750}
1751
Sebastian Ott823d4942009-06-16 10:30:20 +02001752static int ccw_device_pm_prepare(struct device *dev)
1753{
1754 struct ccw_device *cdev = to_ccwdev(dev);
1755
1756 if (work_pending(&cdev->private->kick_work))
1757 return -EAGAIN;
1758 /* Fail while device is being set online/offline. */
1759 if (atomic_read(&cdev->private->onoff))
1760 return -EAGAIN;
1761
1762 if (cdev->online && cdev->drv && cdev->drv->prepare)
1763 return cdev->drv->prepare(cdev);
1764
1765 return 0;
1766}
1767
1768static void ccw_device_pm_complete(struct device *dev)
1769{
1770 struct ccw_device *cdev = to_ccwdev(dev);
1771
1772 if (cdev->online && cdev->drv && cdev->drv->complete)
1773 cdev->drv->complete(cdev);
1774}
1775
1776static int ccw_device_pm_freeze(struct device *dev)
1777{
1778 struct ccw_device *cdev = to_ccwdev(dev);
1779 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1780 int ret, cm_enabled;
1781
1782 /* Fail suspend while device is in transistional state. */
1783 if (!dev_fsm_final_state(cdev))
1784 return -EAGAIN;
1785 if (!cdev->online)
1786 return 0;
1787 if (cdev->drv && cdev->drv->freeze) {
1788 ret = cdev->drv->freeze(cdev);
1789 if (ret)
1790 return ret;
1791 }
1792
1793 spin_lock_irq(sch->lock);
1794 cm_enabled = cdev->private->cmb != NULL;
1795 spin_unlock_irq(sch->lock);
1796 if (cm_enabled) {
1797 /* Don't have the css write on memory. */
1798 ret = ccw_set_cmf(cdev, 0);
1799 if (ret)
1800 return ret;
1801 }
1802 /* From here on, disallow device driver I/O. */
1803 spin_lock_irq(sch->lock);
1804 ret = cio_disable_subchannel(sch);
1805 spin_unlock_irq(sch->lock);
1806
1807 return ret;
1808}
1809
1810static int ccw_device_pm_thaw(struct device *dev)
1811{
1812 struct ccw_device *cdev = to_ccwdev(dev);
1813 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1814 int ret, cm_enabled;
1815
1816 if (!cdev->online)
1817 return 0;
1818
1819 spin_lock_irq(sch->lock);
1820 /* Allow device driver I/O again. */
1821 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1822 cm_enabled = cdev->private->cmb != NULL;
1823 spin_unlock_irq(sch->lock);
1824 if (ret)
1825 return ret;
1826
1827 if (cm_enabled) {
1828 ret = ccw_set_cmf(cdev, 1);
1829 if (ret)
1830 return ret;
1831 }
1832
1833 if (cdev->drv && cdev->drv->thaw)
1834 ret = cdev->drv->thaw(cdev);
1835
1836 return ret;
1837}
1838
1839static void __ccw_device_pm_restore(struct ccw_device *cdev)
1840{
1841 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1842 int ret;
1843
1844 if (cio_is_console(sch->schid))
1845 goto out;
1846 /*
1847 * While we were sleeping, devices may have gone or become
1848 * available again. Kick re-detection.
1849 */
1850 spin_lock_irq(sch->lock);
1851 cdev->private->flags.resuming = 1;
1852 ret = ccw_device_recognition(cdev);
1853 spin_unlock_irq(sch->lock);
1854 if (ret) {
1855 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
Sebastian Ottf0148242009-09-11 10:28:23 +02001856 "0.%x.%04x (ret=%d)\n",
1857 cdev->private->dev_id.ssid,
1858 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001859 spin_lock_irq(sch->lock);
1860 cdev->private->state = DEV_STATE_DISCONNECTED;
1861 spin_unlock_irq(sch->lock);
1862 /* notify driver after the resume cb */
1863 goto out;
1864 }
1865 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1866 cdev->private->state == DEV_STATE_DISCONNECTED);
1867
1868out:
1869 cdev->private->flags.resuming = 0;
1870}
1871
1872static int resume_handle_boxed(struct ccw_device *cdev)
1873{
1874 cdev->private->state = DEV_STATE_BOXED;
1875 if (ccw_device_notify(cdev, CIO_BOXED))
1876 return 0;
1877 ccw_device_schedule_sch_unregister(cdev);
1878 return -ENODEV;
1879}
1880
1881static int resume_handle_disc(struct ccw_device *cdev)
1882{
1883 cdev->private->state = DEV_STATE_DISCONNECTED;
1884 if (ccw_device_notify(cdev, CIO_GONE))
1885 return 0;
1886 ccw_device_schedule_sch_unregister(cdev);
1887 return -ENODEV;
1888}
1889
1890static int ccw_device_pm_restore(struct device *dev)
1891{
1892 struct ccw_device *cdev = to_ccwdev(dev);
1893 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1894 int ret = 0, cm_enabled;
1895
1896 __ccw_device_pm_restore(cdev);
1897 spin_lock_irq(sch->lock);
1898 if (cio_is_console(sch->schid)) {
1899 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1900 spin_unlock_irq(sch->lock);
1901 goto out_restore;
1902 }
1903 cdev->private->flags.donotify = 0;
1904 /* check recognition results */
1905 switch (cdev->private->state) {
1906 case DEV_STATE_OFFLINE:
1907 break;
1908 case DEV_STATE_BOXED:
1909 ret = resume_handle_boxed(cdev);
1910 spin_unlock_irq(sch->lock);
1911 if (ret)
1912 goto out;
1913 goto out_restore;
1914 case DEV_STATE_DISCONNECTED:
1915 goto out_disc_unlock;
1916 default:
1917 goto out_unreg_unlock;
1918 }
1919 /* check if the device id has changed */
1920 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001921 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1922 "changed from %04x to %04x)\n",
1923 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02001924 cdev->private->dev_id.devno,
1925 sch->schib.pmcw.dev);
1926 goto out_unreg_unlock;
1927 }
1928 /* check if the device type has changed */
1929 if (!ccw_device_test_sense_data(cdev)) {
1930 ccw_device_update_sense_data(cdev);
1931 PREPARE_WORK(&cdev->private->kick_work,
1932 ccw_device_do_unbind_bind);
1933 queue_work(ccw_device_work, &cdev->private->kick_work);
1934 ret = -ENODEV;
1935 goto out_unlock;
1936 }
1937 if (!cdev->online) {
1938 ret = 0;
1939 goto out_unlock;
1940 }
1941 ret = ccw_device_online(cdev);
1942 if (ret)
1943 goto out_disc_unlock;
1944
1945 cm_enabled = cdev->private->cmb != NULL;
1946 spin_unlock_irq(sch->lock);
1947
1948 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1949 if (cdev->private->state != DEV_STATE_ONLINE) {
1950 spin_lock_irq(sch->lock);
1951 goto out_disc_unlock;
1952 }
1953 if (cm_enabled) {
1954 ret = ccw_set_cmf(cdev, 1);
1955 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001956 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1957 "(rc=%d)\n", cdev->private->dev_id.ssid,
1958 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001959 ret = 0;
1960 }
1961 }
1962
1963out_restore:
1964 if (cdev->online && cdev->drv && cdev->drv->restore)
1965 ret = cdev->drv->restore(cdev);
1966out:
1967 return ret;
1968
1969out_disc_unlock:
1970 ret = resume_handle_disc(cdev);
1971 spin_unlock_irq(sch->lock);
1972 if (ret)
1973 return ret;
1974 goto out_restore;
1975
1976out_unreg_unlock:
1977 ccw_device_schedule_sch_unregister(cdev);
1978 ret = -ENODEV;
1979out_unlock:
1980 spin_unlock_irq(sch->lock);
1981 return ret;
1982}
1983
1984static struct dev_pm_ops ccw_pm_ops = {
1985 .prepare = ccw_device_pm_prepare,
1986 .complete = ccw_device_pm_complete,
1987 .freeze = ccw_device_pm_freeze,
1988 .thaw = ccw_device_pm_thaw,
1989 .restore = ccw_device_pm_restore,
1990};
1991
Cornelia Huck8bbace72006-01-11 10:56:22 +01001992struct bus_type ccw_bus_type = {
1993 .name = "ccw",
1994 .match = ccw_bus_match,
1995 .uevent = ccw_uevent,
1996 .probe = ccw_device_probe,
1997 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001998 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001999 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002000};
2001
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002002/**
2003 * ccw_driver_register() - register a ccw driver
2004 * @cdriver: driver to be registered
2005 *
2006 * This function is mainly a wrapper around driver_register().
2007 * Returns:
2008 * %0 on success and a negative error value on failure.
2009 */
2010int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 struct device_driver *drv = &cdriver->driver;
2013
2014 drv->bus = &ccw_bus_type;
2015 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01002016 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 return driver_register(drv);
2019}
2020
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002021/**
2022 * ccw_driver_unregister() - deregister a ccw driver
2023 * @cdriver: driver to be deregistered
2024 *
2025 * This function is mainly a wrapper around driver_unregister().
2026 */
2027void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 driver_unregister(&cdriver->driver);
2030}
2031
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002032/* Helper func for qdio. */
2033struct subchannel_id
2034ccw_device_get_subchannel_id(struct ccw_device *cdev)
2035{
2036 struct subchannel *sch;
2037
2038 sch = to_subchannel(cdev->dev.parent);
2039 return sch->schid;
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042MODULE_LICENSE("GPL");
2043EXPORT_SYMBOL(ccw_device_set_online);
2044EXPORT_SYMBOL(ccw_device_set_offline);
2045EXPORT_SYMBOL(ccw_driver_register);
2046EXPORT_SYMBOL(ccw_driver_unregister);
2047EXPORT_SYMBOL(get_ccwdev_by_busid);
2048EXPORT_SYMBOL(ccw_bus_type);
2049EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002050EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);