blob: 167446785d191a11c099f638517396125194f069 [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 Huckd7b5a4c2006-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 Huckd7b5a4c2006-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
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200309/**
310 * ccw_device_set_offline() - disable a ccw device for I/O
311 * @cdev: target ccw device
312 *
313 * This function calls the driver's set_offline() function for @cdev, if
314 * given, and then disables @cdev.
315 * Returns:
316 * %0 on success and a negative error value on failure.
317 * Context:
318 * enabled, ccw device lock not held
319 */
320int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int ret;
323
324 if (!cdev)
325 return -ENODEV;
326 if (!cdev->online || !cdev->drv)
327 return -EINVAL;
328
329 if (cdev->drv->set_offline) {
330 ret = cdev->drv->set_offline(cdev);
331 if (ret != 0)
332 return ret;
333 }
334 cdev->online = 0;
335 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200336 /* Wait until a final state or DISCONNECTED is reached */
337 while (!dev_fsm_final_state(cdev) &&
338 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200340 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
341 cdev->private->state == DEV_STATE_DISCONNECTED));
342 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200344 ret = ccw_device_offline(cdev);
345 if (ret)
346 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200348 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
349 cdev->private->state == DEV_STATE_DISCONNECTED));
350 /* Give up reference from ccw_device_set_online(). */
351 put_device(&cdev->dev);
352 return 0;
353
354error:
355 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
356 ret, cdev->private->dev_id.ssid,
357 cdev->private->dev_id.devno);
358 cdev->private->state = DEV_STATE_OFFLINE;
359 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
360 spin_unlock_irq(cdev->ccwlock);
361 /* Give up reference from ccw_device_set_online(). */
362 put_device(&cdev->dev);
363 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200366/**
367 * ccw_device_set_online() - enable a ccw device for I/O
368 * @cdev: target ccw device
369 *
370 * This function first enables @cdev and then calls the driver's set_online()
371 * function for @cdev, if given. If set_online() returns an error, @cdev is
372 * disabled again.
373 * Returns:
374 * %0 on success and a negative error value on failure.
375 * Context:
376 * enabled, ccw device lock not held
377 */
378int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200381 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (!cdev)
384 return -ENODEV;
385 if (cdev->online || !cdev->drv)
386 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100387 /* Hold on to an extra reference while device is online. */
388 if (!get_device(&cdev->dev))
389 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 spin_lock_irq(cdev->ccwlock);
392 ret = ccw_device_online(cdev);
393 spin_unlock_irq(cdev->ccwlock);
394 if (ret == 0)
395 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
396 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200397 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200398 "device 0.%x.%04x\n",
399 ret, cdev->private->dev_id.ssid,
400 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100401 /* Give up online reference since onlining failed. */
402 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ret;
404 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200405 spin_lock_irq(cdev->ccwlock);
406 /* Check if online processing was successful */
407 if ((cdev->private->state != DEV_STATE_ONLINE) &&
408 (cdev->private->state != DEV_STATE_W4SENSE)) {
409 spin_unlock_irq(cdev->ccwlock);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100410 /* Give up online reference since onlining failed. */
411 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200415 if (cdev->drv->set_online)
416 ret = cdev->drv->set_online(cdev);
417 if (ret)
418 goto rollback;
419 cdev->online = 1;
420 return 0;
421
422rollback:
423 spin_lock_irq(cdev->ccwlock);
424 /* Wait until a final state or DISCONNECTED is reached */
425 while (!dev_fsm_final_state(cdev) &&
426 cdev->private->state != DEV_STATE_DISCONNECTED) {
427 spin_unlock_irq(cdev->ccwlock);
428 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
429 cdev->private->state == DEV_STATE_DISCONNECTED));
430 spin_lock_irq(cdev->ccwlock);
431 }
432 ret2 = ccw_device_offline(cdev);
433 if (ret2)
434 goto error;
435 spin_unlock_irq(cdev->ccwlock);
436 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
437 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100438 /* Give up online reference since onlining failed. */
439 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200440 return ret;
441
442error:
443 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
444 "device 0.%x.%04x\n",
445 ret2, cdev->private->dev_id.ssid,
446 cdev->private->dev_id.devno);
447 cdev->private->state = DEV_STATE_OFFLINE;
448 spin_unlock_irq(cdev->ccwlock);
449 /* Give up online reference since onlining failed. */
450 put_device(&cdev->dev);
451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100454static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200455{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100456 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
457 spin_lock_irq(cdev->ccwlock);
458 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
459 spin_unlock_irq(cdev->ccwlock);
460 } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100461 return ccw_device_set_offline(cdev);
462 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200463}
464
465static int online_store_recog_and_online(struct ccw_device *cdev)
466{
467 int ret;
468
469 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200470 if (cdev->private->state == DEV_STATE_BOXED) {
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200471 ret = ccw_device_recognition(cdev);
472 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200473 CIO_MSG_EVENT(0, "Couldn't start recognition "
474 "for device 0.%x.%04x (ret=%d)\n",
475 cdev->private->dev_id.ssid,
476 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200477 return ret;
478 }
479 wait_event(cdev->private->wait_q,
480 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200481 if (cdev->private->state != DEV_STATE_OFFLINE)
482 /* recognition failed */
483 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200484 }
485 if (cdev->drv && cdev->drv->set_online)
486 ccw_device_set_online(cdev);
487 return 0;
488}
Sebastian Ott156013f2009-03-31 19:16:03 +0200489
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200490static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491{
492 int ret;
493
494 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200495 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200496 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200497 if (force && cdev->private->state == DEV_STATE_BOXED) {
498 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200499 if (ret)
500 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200501 if (cdev->id.cu_type == 0)
502 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200503 ret = online_store_recog_and_online(cdev);
504 if (ret)
505 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200506 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200507 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200508}
509
510static ssize_t online_store (struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200514 int force, ret;
515 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Sebastian Ottb5cd99e2009-03-31 19:16:06 +0200517 if ((cdev->private->state != DEV_STATE_OFFLINE &&
518 cdev->private->state != DEV_STATE_ONLINE &&
519 cdev->private->state != DEV_STATE_BOXED &&
520 cdev->private->state != DEV_STATE_DISCONNECTED) ||
521 atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -EAGAIN;
523
524 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
525 atomic_set(&cdev->private->onoff, 0);
526 return -EINVAL;
527 }
528 if (!strncmp(buf, "force\n", count)) {
529 force = 1;
530 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200531 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } else {
533 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200534 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200536 if (ret)
537 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538 switch (i) {
539 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100540 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200541 break;
542 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200543 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200544 break;
545 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200546 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200548out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (cdev->drv)
550 module_put(cdev->drv->owner);
551 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100552 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400556available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct ccw_device *cdev = to_ccwdev(dev);
559 struct subchannel *sch;
560
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100561 if (ccw_device_is_orphan(cdev))
562 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 switch (cdev->private->state) {
564 case DEV_STATE_BOXED:
565 return sprintf(buf, "boxed\n");
566 case DEV_STATE_DISCONNECTED:
567 case DEV_STATE_DISCONNECTED_SENSE_ID:
568 case DEV_STATE_NOT_OPER:
569 sch = to_subchannel(dev->parent);
570 if (!sch->lpm)
571 return sprintf(buf, "no path\n");
572 else
573 return sprintf(buf, "no device\n");
574 default:
575 /* All other states considered fine. */
576 return sprintf(buf, "good\n");
577 }
578}
579
580static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
581static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
582static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
583static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800584static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static DEVICE_ATTR(availability, 0444, available_show, NULL);
587
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200588static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 &dev_attr_chpids.attr,
590 &dev_attr_pimpampom.attr,
591 NULL,
592};
593
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200594static struct attribute_group io_subchannel_attr_group = {
595 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100596};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598static struct attribute * ccwdev_attrs[] = {
599 &dev_attr_devtype.attr,
600 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800601 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 &dev_attr_online.attr,
603 &dev_attr_cmb_enable.attr,
604 &dev_attr_availability.attr,
605 NULL,
606};
607
608static struct attribute_group ccwdev_attr_group = {
609 .attrs = ccwdev_attrs,
610};
611
David Brownella4dbd672009-06-24 10:06:31 -0700612static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200613 &ccwdev_attr_group,
614 NULL,
615};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/* this is a simple abstraction for device_register that sets the
618 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200619static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct device *dev = &cdev->dev;
622 int ret;
623
624 dev->bus = &ccw_bus_type;
Sebastian Ott3ac276f2009-09-11 10:28:27 +0200625 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
626 cdev->private->dev_id.devno);
627 if (ret)
628 return ret;
629 ret = device_add(dev);
630 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return ret;
632
633 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return ret;
635}
636
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100637static int match_dev_id(struct device *dev, void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700638{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100639 struct ccw_device *cdev = to_ccwdev(dev);
640 struct ccw_dev_id *dev_id = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700641
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100642 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
643}
644
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100645static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100646{
647 struct device *dev;
648
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100649 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100650
651 return dev ? to_ccwdev(dev) : NULL;
652}
653
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100654static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100658 if (test_bit(1, &cdev->private->registered)) {
659 device_release_driver(&cdev->dev);
660 ret = device_attach(&cdev->dev);
661 WARN_ON(ret == -ENODEV);
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665static void
666ccw_device_release(struct device *dev)
667{
668 struct ccw_device *cdev;
669
670 cdev = to_ccwdev(dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100671 /* Release reference of parent subchannel. */
672 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 kfree(cdev->private);
674 kfree(cdev);
675}
676
Cornelia Huck7674da72006-12-08 15:54:21 +0100677static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
678{
679 struct ccw_device *cdev;
680
681 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
682 if (cdev) {
683 cdev->private = kzalloc(sizeof(struct ccw_device_private),
684 GFP_KERNEL | GFP_DMA);
685 if (cdev->private)
686 return cdev;
687 }
688 kfree(cdev);
689 return ERR_PTR(-ENOMEM);
690}
691
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100692static void ccw_device_todo(struct work_struct *work);
693
Cornelia Huck7674da72006-12-08 15:54:21 +0100694static int io_subchannel_initialize_dev(struct subchannel *sch,
695 struct ccw_device *cdev)
696{
697 cdev->private->cdev = cdev;
698 atomic_set(&cdev->private->onoff, 0);
699 cdev->dev.parent = &sch->dev;
700 cdev->dev.release = ccw_device_release;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100701 INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
Cornelia Huckef995162007-04-27 16:01:39 +0200702 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100703 /* Do first half of device_register. */
704 device_initialize(&cdev->dev);
705 if (!get_device(&sch->dev)) {
Cornelia Huck283fdd02008-12-25 13:39:10 +0100706 /* Release reference from device_initialize(). */
707 put_device(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100708 return -ENODEV;
709 }
710 return 0;
711}
712
713static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
714{
715 struct ccw_device *cdev;
716 int ret;
717
718 cdev = io_subchannel_allocate_dev(sch);
719 if (!IS_ERR(cdev)) {
720 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200721 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100722 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100723 }
724 return cdev;
725}
726
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100727static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
728
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100729static void sch_create_and_recog_new_device(struct subchannel *sch)
730{
731 struct ccw_device *cdev;
732
733 /* Need to allocate a new ccw device. */
734 cdev = io_subchannel_create_ccwdev(sch);
735 if (IS_ERR(cdev)) {
736 /* OK, we did everything we could... */
737 css_sch_device_unregister(sch);
738 return;
739 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100740 /* Start recognition for the new ccw device. */
741 if (io_subchannel_recog(cdev, sch)) {
742 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100743 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100744 spin_unlock_irq(sch->lock);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100745 css_sch_device_unregister(sch);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100746 /* Put reference from io_subchannel_create_ccwdev(). */
747 put_device(&sch->dev);
748 /* Give up initial reference. */
749 put_device(&cdev->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100750 }
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Register recognized device.
755 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100756static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct subchannel *sch;
759 int ret;
760 unsigned long flags;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100763 /*
764 * Check if subchannel is still registered. It may have become
765 * unregistered if a machine check hit us after finishing
766 * device recognition but before the register work could be
767 * queued.
768 */
769 if (!device_is_registered(&sch->dev))
770 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200771 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100772 /*
773 * io_subchannel_register() will also be called after device
774 * recognition has been done for a boxed device (which will already
775 * be registered). We need to reprobe since we may now have sense id
776 * information.
777 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100778 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100779 if (!cdev->drv) {
780 ret = device_reprobe(&cdev->dev);
781 if (ret)
782 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200783 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200784 " %d for 0.%x.%04x\n", ret,
785 cdev->private->dev_id.ssid,
786 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto out;
789 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700790 /*
791 * Now we know this subchannel will stay, we can throw
792 * our delayed uevent.
793 */
Ming Leif67f1292009-03-01 21:10:49 +0800794 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700795 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* make it known to the system */
797 ret = ccw_device_register(cdev);
798 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200799 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
800 cdev->private->dev_id.ssid,
801 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100802 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100803 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100804 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100805 /* Release initial device reference. */
806 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100807 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809out:
810 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100812out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (atomic_dec_and_test(&ccw_device_init_count))
814 wake_up(&ccw_device_init_wq);
815}
816
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100817static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct subchannel *sch;
820
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200821 /* Get subchannel reference for local processing. */
822 if (!get_device(cdev->dev.parent))
823 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200825 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200826 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 put_device(&sch->dev);
828}
829
830/*
831 * subchannel recognition done. Called from the state machine.
832 */
833void
834io_subchannel_recog_done(struct ccw_device *cdev)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (css_init_done == 0) {
837 cdev->private->flags.recog_done = 1;
838 return;
839 }
840 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200841 case DEV_STATE_BOXED:
842 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case DEV_STATE_NOT_OPER:
844 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100845 /* Remove device found not operational. */
846 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (atomic_dec_and_test(&ccw_device_init_count))
848 wake_up(&ccw_device_init_wq);
849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 case DEV_STATE_OFFLINE:
851 /*
852 * We can't register the device in interrupt context so
853 * we schedule a work item.
854 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100855 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 }
858}
859
860static int
861io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
862{
863 int rc;
864 struct ccw_device_private *priv;
865
Cornelia Huck2ec22982006-12-08 15:54:26 +0100866 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Init private data. */
869 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +0200870 priv->dev_id.devno = sch->schib.pmcw.dev;
871 priv->dev_id.ssid = sch->schid.ssid;
872 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 priv->state = DEV_STATE_NOT_OPER;
874 INIT_LIST_HEAD(&priv->cmb_list);
875 init_waitqueue_head(&priv->wait_q);
876 init_timer(&priv->timer);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Increase counter of devices currently in recognition. */
879 atomic_inc(&ccw_device_init_count);
880
881 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100882 spin_lock_irq(sch->lock);
Peter Oberparleiter60e4dac2009-12-07 12:51:16 +0100883 sch_set_cdev(sch, cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100885 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (rc) {
887 if (atomic_dec_and_test(&ccw_device_init_count))
888 wake_up(&ccw_device_init_wq);
889 }
890 return rc;
891}
892
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100893static int ccw_device_move_to_sch(struct ccw_device *cdev,
894 struct subchannel *sch)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100895{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100896 struct subchannel *old_sch;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100897 int rc;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100898
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100899 old_sch = to_subchannel(cdev->dev.parent);
900 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100901 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100902 return -ENODEV;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100903 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100904 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100905 mutex_unlock(&sch->reg_mutex);
906 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100907 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100908 cdev->private->dev_id.ssid,
909 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100910 sch->schib.pmcw.dev, rc);
911 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100912 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100913 return rc;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100914 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100915 /* Clean up old subchannel. */
916 if (!sch_is_pseudo_sch(old_sch)) {
917 spin_lock_irq(old_sch->lock);
918 sch_set_cdev(old_sch, NULL);
919 cio_disable_subchannel(old_sch);
920 spin_unlock_irq(old_sch->lock);
921 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100922 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100923 /* Release child reference for old parent. */
924 put_device(&old_sch->dev);
925 /* Initialize new subchannel. */
926 spin_lock_irq(sch->lock);
927 cdev->private->schid = sch->schid;
928 cdev->ccwlock = sch->lock;
929 if (!sch_is_pseudo_sch(sch))
930 sch_set_cdev(sch, cdev);
931 spin_unlock_irq(sch->lock);
932 if (!sch_is_pseudo_sch(sch))
933 css_update_ssd_info(sch);
934 return 0;
935}
936
937static int ccw_device_move_to_orph(struct ccw_device *cdev)
938{
939 struct subchannel *sch = to_subchannel(cdev->dev.parent);
940 struct channel_subsystem *css = to_css(sch->dev.parent);
941
942 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100943}
944
Cornelia Huck602b20f2008-01-26 14:10:39 +0100945static void io_subchannel_irq(struct subchannel *sch)
946{
947 struct ccw_device *cdev;
948
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100949 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100950
Sebastian Ottefd986d2009-09-11 10:28:18 +0200951 CIO_TRACE_EVENT(6, "IRQ");
952 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100953 if (cdev)
954 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
955}
956
Sebastian Ott13952ec2008-12-25 13:39:13 +0100957void io_subchannel_init_config(struct subchannel *sch)
958{
959 memset(&sch->config, 0, sizeof(sch->config));
960 sch->config.csense = 1;
Sebastian Ottd36f0c62008-12-25 13:39:15 +0100961 /* Use subchannel mp mode when there is more than 1 installed CHPID. */
962 if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
Sebastian Ott13952ec2008-12-25 13:39:13 +0100963 sch->config.mp = 1;
964}
965
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200966static void io_subchannel_init_fields(struct subchannel *sch)
967{
968 if (cio_is_console(sch->schid))
969 sch->opm = 0xff;
970 else
971 sch->opm = chp_get_sch_opm(sch);
972 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200973 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200974
975 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
976 " - PIM = %02X, PAM = %02X, POM = %02X\n",
977 sch->schib.pmcw.dev, sch->schid.ssid,
978 sch->schid.sch_no, sch->schib.pmcw.pim,
979 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +0100980
981 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200982}
983
Cornelia Huck90ed2b62008-12-25 13:39:09 +0100984/*
985 * Note: We always return 0 so that we bind to the device even on error.
986 * This is needed so that our remove function is called on unregister.
987 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200988static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 struct ccw_device *cdev;
991 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +0200993 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200994 rc = sysfs_create_group(&sch->dev.kobj,
995 &io_subchannel_attr_group);
996 if (rc)
997 CIO_MSG_EVENT(0, "Failed to create io subchannel "
998 "attributes for subchannel "
999 "0.%x.%04x (rc=%d)\n",
1000 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001002 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001003 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001004 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Ming Leif67f1292009-03-01 21:10:49 +08001006 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001007 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001008 cdev = sch_get_cdev(sch);
Cornelia Hucke1031782007-10-12 16:11:23 +02001009 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 device_initialize(&cdev->dev);
1011 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Check if the device is already online. If it is
Cornelia Huck9cd67422008-12-25 13:39:06 +01001014 * the reference count needs to be corrected since we
1015 * didn't obtain a reference in ccw_device_set_online.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1018 cdev->private->state != DEV_STATE_OFFLINE &&
1019 cdev->private->state != DEV_STATE_BOXED)
1020 get_device(&cdev->dev);
1021 return 0;
1022 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001023 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001024 rc = cio_commit_config(sch);
1025 if (rc)
1026 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001027 rc = sysfs_create_group(&sch->dev.kobj,
1028 &io_subchannel_attr_group);
1029 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001030 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001031 /* Allocate I/O subchannel private data. */
1032 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1033 GFP_KERNEL | GFP_DMA);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001034 if (!sch->private)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001035 goto out_schedule;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001036 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001037 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001038
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001039out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001040 spin_lock_irq(sch->lock);
1041 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1042 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001047io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct ccw_device *cdev;
1050 unsigned long flags;
1051
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001052 cdev = sch_get_cdev(sch);
1053 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001054 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Set ccw device to not operational and drop reference. */
1056 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001057 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 cdev->private->state = DEV_STATE_NOT_OPER;
1059 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001060 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001061out_free:
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001062 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001063 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065}
1066
Cornelia Huck602b20f2008-01-26 14:10:39 +01001067static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct ccw_device *cdev;
1070
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001071 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (cdev)
1073 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1074}
1075
Cornelia Huckc820de32008-07-14 09:58:45 +02001076static int check_for_io_on_path(struct subchannel *sch, int mask)
1077{
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001078 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001079 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001080 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001081 return 1;
1082 return 0;
1083}
1084
1085static void terminate_internal_io(struct subchannel *sch,
1086 struct ccw_device *cdev)
1087{
1088 if (cio_clear(sch)) {
1089 /* Recheck device in case clear failed. */
1090 sch->lpm = 0;
1091 if (cdev->online)
1092 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1093 else
1094 css_schedule_eval(sch->schid);
1095 return;
1096 }
1097 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1098 /* Request retry of internal operation. */
1099 cdev->private->flags.intretry = 1;
1100 /* Call handler. */
1101 if (cdev->handler)
1102 cdev->handler(cdev, cdev->private->intparm,
1103 ERR_PTR(-EIO));
1104}
1105
1106static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct ccw_device *cdev;
1109
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001110 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!cdev)
1112 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001113 if (check_for_io_on_path(sch, mask)) {
1114 if (cdev->private->state == DEV_STATE_ONLINE)
1115 ccw_device_kill_io(cdev);
1116 else {
1117 terminate_internal_io(sch, cdev);
1118 /* Re-start path verification. */
1119 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1120 }
1121 } else
1122 /* trigger path verification. */
1123 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1124
1125}
1126
Cornelia Huck99611f82008-07-14 09:59:02 +02001127static int io_subchannel_chp_event(struct subchannel *sch,
1128 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001129{
1130 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001131
Cornelia Huck99611f82008-07-14 09:59:02 +02001132 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001133 if (!mask)
1134 return 0;
1135 switch (event) {
1136 case CHP_VARY_OFF:
1137 sch->opm &= ~mask;
1138 sch->lpm &= ~mask;
1139 io_subchannel_terminate_path(sch, mask);
1140 break;
1141 case CHP_VARY_ON:
1142 sch->opm |= mask;
1143 sch->lpm |= mask;
1144 io_subchannel_verify(sch);
1145 break;
1146 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001147 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001148 return -ENODEV;
1149 io_subchannel_terminate_path(sch, mask);
1150 break;
1151 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001152 if (cio_update_schib(sch))
1153 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001154 sch->lpm |= mask & sch->opm;
1155 io_subchannel_verify(sch);
1156 break;
1157 }
1158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001162io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct ccw_device *cdev;
1165 int ret;
1166
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001167 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001169 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return;
1171 if (!sch->schib.pmcw.ena)
1172 /* Nothing to do. */
1173 return;
1174 ret = cio_disable_subchannel(sch);
1175 if (ret != -EBUSY)
1176 /* Subchannel is disabled, we're done. */
1177 return;
1178 cdev->private->state = DEV_STATE_QUIESCE;
1179 if (cdev->handler)
1180 cdev->handler(cdev, cdev->private->intparm,
1181 ERR_PTR(-EIO));
1182 ret = ccw_device_cancel_halt_clear(cdev);
1183 if (ret == -EBUSY) {
1184 ccw_device_set_timeout(cdev, HZ/10);
1185 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1186 }
1187 cio_disable_subchannel(sch);
1188}
1189
Cornelia Huckc820de32008-07-14 09:58:45 +02001190static int device_is_disconnected(struct ccw_device *cdev)
1191{
1192 if (!cdev)
1193 return 0;
1194 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1195 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1196}
1197
1198static int recovery_check(struct device *dev, void *data)
1199{
1200 struct ccw_device *cdev = to_ccwdev(dev);
1201 int *redo = data;
1202
1203 spin_lock_irq(cdev->ccwlock);
1204 switch (cdev->private->state) {
1205 case DEV_STATE_DISCONNECTED:
1206 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1207 cdev->private->dev_id.ssid,
1208 cdev->private->dev_id.devno);
1209 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1210 *redo = 1;
1211 break;
1212 case DEV_STATE_DISCONNECTED_SENSE_ID:
1213 *redo = 1;
1214 break;
1215 }
1216 spin_unlock_irq(cdev->ccwlock);
1217
1218 return 0;
1219}
1220
1221static void recovery_work_func(struct work_struct *unused)
1222{
1223 int redo = 0;
1224
1225 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1226 if (redo) {
1227 spin_lock_irq(&recovery_lock);
1228 if (!timer_pending(&recovery_timer)) {
1229 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1230 recovery_phase++;
1231 mod_timer(&recovery_timer, jiffies +
1232 recovery_delay[recovery_phase] * HZ);
1233 }
1234 spin_unlock_irq(&recovery_lock);
1235 } else
1236 CIO_MSG_EVENT(4, "recovery: end\n");
1237}
1238
1239static DECLARE_WORK(recovery_work, recovery_work_func);
1240
1241static void recovery_func(unsigned long data)
1242{
1243 /*
1244 * We can't do our recovery in softirq context and it's not
1245 * performance critical, so we schedule it.
1246 */
1247 schedule_work(&recovery_work);
1248}
1249
1250static void ccw_device_schedule_recovery(void)
1251{
1252 unsigned long flags;
1253
1254 CIO_MSG_EVENT(4, "recovery: schedule\n");
1255 spin_lock_irqsave(&recovery_lock, flags);
1256 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1257 recovery_phase = 0;
1258 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1259 }
1260 spin_unlock_irqrestore(&recovery_lock, flags);
1261}
1262
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001263static int purge_fn(struct device *dev, void *data)
1264{
1265 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001266 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001267
1268 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001269 if (is_blacklisted(id->ssid, id->devno) &&
1270 (cdev->private->state == DEV_STATE_OFFLINE)) {
1271 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1272 id->devno);
1273 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1274 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001275 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001276 /* Abort loop in case of pending signal. */
1277 if (signal_pending(current))
1278 return -EINTR;
1279
1280 return 0;
1281}
1282
1283/**
1284 * ccw_purge_blacklisted - purge unused, blacklisted devices
1285 *
1286 * Unregister all ccw devices that are offline and on the blacklist.
1287 */
1288int ccw_purge_blacklisted(void)
1289{
1290 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1291 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1292 return 0;
1293}
1294
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001295void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001296{
1297 if (!cdev)
1298 return;
1299 ccw_device_set_timeout(cdev, 0);
1300 cdev->private->flags.fake_irb = 0;
1301 cdev->private->state = DEV_STATE_DISCONNECTED;
1302 if (cdev->online)
1303 ccw_device_schedule_recovery();
1304}
1305
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001306void ccw_device_set_notoper(struct ccw_device *cdev)
1307{
1308 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1309
1310 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001311 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001312 ccw_device_set_timeout(cdev, 0);
1313 cio_disable_subchannel(sch);
1314 cdev->private->state = DEV_STATE_NOT_OPER;
1315}
1316
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001317enum io_sch_action {
1318 IO_SCH_UNREG,
1319 IO_SCH_ORPH_UNREG,
1320 IO_SCH_ATTACH,
1321 IO_SCH_UNREG_ATTACH,
1322 IO_SCH_ORPH_ATTACH,
1323 IO_SCH_REPROBE,
1324 IO_SCH_VERIFY,
1325 IO_SCH_DISC,
1326 IO_SCH_NOP,
1327};
1328
1329static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001330{
Cornelia Huckc820de32008-07-14 09:58:45 +02001331 struct ccw_device *cdev;
1332
Cornelia Huckc820de32008-07-14 09:58:45 +02001333 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001334 if (cio_update_schib(sch)) {
1335 /* Not operational. */
1336 if (!cdev)
1337 return IO_SCH_UNREG;
1338 if (!ccw_device_notify(cdev, CIO_GONE))
1339 return IO_SCH_UNREG;
1340 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001341 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001342 /* Operational. */
1343 if (!cdev)
1344 return IO_SCH_ATTACH;
1345 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1346 if (!ccw_device_notify(cdev, CIO_GONE))
1347 return IO_SCH_UNREG_ATTACH;
1348 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001349 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001350 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1351 if (!ccw_device_notify(cdev, CIO_NO_PATH))
1352 return IO_SCH_UNREG;
1353 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001354 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001355 if (device_is_disconnected(cdev))
1356 return IO_SCH_REPROBE;
1357 if (cdev->online)
1358 return IO_SCH_VERIFY;
1359 return IO_SCH_NOP;
1360}
1361
1362/**
1363 * io_subchannel_sch_event - process subchannel event
1364 * @sch: subchannel
1365 * @process: non-zero if function is called in process context
1366 *
1367 * An unspecified event occurred for this subchannel. Adjust data according
1368 * to the current operational state of the subchannel and device. Return
1369 * zero when the event has been handled sufficiently or -EAGAIN when this
1370 * function should be called again in process context.
1371 */
1372static int io_subchannel_sch_event(struct subchannel *sch, int process)
1373{
1374 unsigned long flags;
1375 struct ccw_device *cdev;
1376 struct ccw_dev_id dev_id;
1377 enum io_sch_action action;
1378 int rc = -EAGAIN;
1379
1380 spin_lock_irqsave(sch->lock, flags);
1381 if (!device_is_registered(&sch->dev))
1382 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001383 if (work_pending(&sch->todo_work))
1384 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001385 cdev = sch_get_cdev(sch);
1386 if (cdev && work_pending(&cdev->private->todo_work))
1387 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001388 action = sch_get_action(sch);
1389 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1390 sch->schid.ssid, sch->schid.sch_no, process,
1391 action);
1392 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001393 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001394 case IO_SCH_REPROBE:
1395 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001396 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001397 rc = 0;
1398 goto out_unlock;
1399 case IO_SCH_VERIFY:
1400 /* Trigger path verification. */
1401 io_subchannel_verify(sch);
1402 rc = 0;
1403 goto out_unlock;
1404 case IO_SCH_DISC:
1405 ccw_device_set_disconnected(cdev);
1406 rc = 0;
1407 goto out_unlock;
1408 case IO_SCH_ORPH_UNREG:
1409 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001410 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001411 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001412 case IO_SCH_UNREG_ATTACH:
1413 case IO_SCH_UNREG:
1414 if (cdev)
1415 ccw_device_set_notoper(cdev);
1416 break;
1417 case IO_SCH_NOP:
1418 rc = 0;
1419 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001420 default:
1421 break;
1422 }
1423 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001424 /* All other actions require process context. */
1425 if (!process)
1426 goto out;
1427 /* Handle attached ccw device. */
1428 switch (action) {
1429 case IO_SCH_ORPH_UNREG:
1430 case IO_SCH_ORPH_ATTACH:
1431 /* Move ccw device to orphanage. */
1432 rc = ccw_device_move_to_orph(cdev);
1433 if (rc)
1434 goto out;
1435 break;
1436 case IO_SCH_UNREG_ATTACH:
1437 /* Unregister ccw device. */
1438 ccw_device_unregister(cdev);
1439 break;
1440 default:
1441 break;
1442 }
1443 /* Handle subchannel. */
1444 switch (action) {
1445 case IO_SCH_ORPH_UNREG:
1446 case IO_SCH_UNREG:
1447 css_sch_device_unregister(sch);
1448 break;
1449 case IO_SCH_ORPH_ATTACH:
1450 case IO_SCH_UNREG_ATTACH:
1451 case IO_SCH_ATTACH:
1452 dev_id.ssid = sch->schid.ssid;
1453 dev_id.devno = sch->schib.pmcw.dev;
1454 cdev = get_ccwdev_by_dev_id(&dev_id);
1455 if (!cdev) {
1456 sch_create_and_recog_new_device(sch);
1457 break;
1458 }
1459 rc = ccw_device_move_to_sch(cdev, sch);
1460 if (rc) {
1461 /* Release reference from get_ccwdev_by_dev_id() */
1462 put_device(&cdev->dev);
1463 goto out;
1464 }
1465 spin_lock_irqsave(sch->lock, flags);
1466 ccw_device_trigger_reprobe(cdev);
1467 spin_unlock_irqrestore(sch->lock, flags);
1468 /* Release reference from get_ccwdev_by_dev_id() */
1469 put_device(&cdev->dev);
1470 break;
1471 default:
1472 break;
1473 }
1474 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001475
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001476out_unlock:
1477 spin_unlock_irqrestore(sch->lock, flags);
1478out:
1479 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#ifdef CONFIG_CCW_CONSOLE
1483static struct ccw_device console_cdev;
1484static struct ccw_device_private console_private;
1485static int console_cdev_in_use;
1486
Cornelia Huck2ec22982006-12-08 15:54:26 +01001487static DEFINE_SPINLOCK(ccw_console_lock);
1488
1489spinlock_t * cio_get_console_lock(void)
1490{
1491 return &ccw_console_lock;
1492}
1493
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001494static int ccw_device_console_enable(struct ccw_device *cdev,
1495 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 int rc;
1498
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001499 /* Attach subchannel private data. */
1500 sch->private = cio_get_console_priv();
1501 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001502 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001503 rc = cio_commit_config(sch);
1504 if (rc)
1505 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001506 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001508 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 rc = io_subchannel_recog(cdev, sch);
1510 if (rc)
1511 return rc;
1512
1513 /* Now wait for the async. recognition to come to an end. */
1514 spin_lock_irq(cdev->ccwlock);
1515 while (!dev_fsm_final_state(cdev))
1516 wait_cons_dev();
1517 rc = -EIO;
1518 if (cdev->private->state != DEV_STATE_OFFLINE)
1519 goto out_unlock;
1520 ccw_device_online(cdev);
1521 while (!dev_fsm_final_state(cdev))
1522 wait_cons_dev();
1523 if (cdev->private->state != DEV_STATE_ONLINE)
1524 goto out_unlock;
1525 rc = 0;
1526out_unlock:
1527 spin_unlock_irq(cdev->ccwlock);
1528 return 0;
1529}
1530
1531struct ccw_device *
1532ccw_device_probe_console(void)
1533{
1534 struct subchannel *sch;
1535 int ret;
1536
1537 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001538 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 sch = cio_probe_console();
1540 if (IS_ERR(sch)) {
1541 console_cdev_in_use = 0;
1542 return (void *) sch;
1543 }
1544 memset(&console_cdev, 0, sizeof(struct ccw_device));
1545 memset(&console_private, 0, sizeof(struct ccw_device_private));
1546 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001547 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 ret = ccw_device_console_enable(&console_cdev, sch);
1549 if (ret) {
1550 cio_release_console();
1551 console_cdev_in_use = 0;
1552 return ERR_PTR(ret);
1553 }
1554 console_cdev.online = 1;
1555 return &console_cdev;
1556}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001557
Martin Schwidefsky66648452009-06-16 10:30:28 +02001558static int ccw_device_pm_restore(struct device *dev);
1559
1560int ccw_device_force_console(void)
1561{
1562 if (!console_cdev_in_use)
1563 return -ENODEV;
1564 return ccw_device_pm_restore(&console_cdev.dev);
1565}
1566EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567#endif
1568
1569/*
1570 * get ccw_device matching the busid, but only if owned by cdrv
1571 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001572static int
1573__ccwdev_check_busid(struct device *dev, void *id)
1574{
1575 char *bus_id;
1576
Cornelia Huck12975ae2006-10-11 15:31:47 +02001577 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001578
Kay Sievers98df67b2008-12-25 13:38:55 +01001579 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001580}
1581
1582
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001583/**
1584 * get_ccwdev_by_busid() - obtain device from a bus id
1585 * @cdrv: driver the device is owned by
1586 * @bus_id: bus id of the device to be searched
1587 *
1588 * This function searches all devices owned by @cdrv for a device with a bus
1589 * id matching @bus_id.
1590 * Returns:
1591 * If a match is found, its reference count of the found device is increased
1592 * and it is returned; else %NULL is returned.
1593 */
1594struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1595 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001597 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct device_driver *drv;
1599
1600 drv = get_driver(&cdrv->driver);
1601 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001602 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001604 dev = driver_find_device(drv, NULL, (void *)bus_id,
1605 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 put_driver(drv);
1607
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001608 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/************************** device driver handling ************************/
1612
1613/* This is the implementation of the ccw_driver class. The probe, remove
1614 * and release methods are initially very similar to the device_driver
1615 * implementations, with the difference that they have ccw_device
1616 * arguments.
1617 *
1618 * A ccw driver also contains the information that is needed for
1619 * device matching.
1620 */
1621static int
1622ccw_device_probe (struct device *dev)
1623{
1624 struct ccw_device *cdev = to_ccwdev(dev);
1625 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1626 int ret;
1627
1628 cdev->drv = cdrv; /* to let the driver call _set_online */
1629
1630 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1631
1632 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001633 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return ret;
1635 }
1636
1637 return 0;
1638}
1639
1640static int
1641ccw_device_remove (struct device *dev)
1642{
1643 struct ccw_device *cdev = to_ccwdev(dev);
1644 struct ccw_driver *cdrv = cdev->drv;
1645 int ret;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (cdrv->remove)
1648 cdrv->remove(cdev);
1649 if (cdev->online) {
1650 cdev->online = 0;
1651 spin_lock_irq(cdev->ccwlock);
1652 ret = ccw_device_offline(cdev);
1653 spin_unlock_irq(cdev->ccwlock);
1654 if (ret == 0)
1655 wait_event(cdev->private->wait_q,
1656 dev_fsm_final_state(cdev));
1657 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001658 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001659 "device 0.%x.%04x\n",
1660 ret, cdev->private->dev_id.ssid,
1661 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001662 /* Give up reference obtained in ccw_device_set_online(). */
1663 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001666 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return 0;
1668}
1669
Cornelia Huck958974f2007-10-12 16:11:21 +02001670static void ccw_device_shutdown(struct device *dev)
1671{
1672 struct ccw_device *cdev;
1673
1674 cdev = to_ccwdev(dev);
1675 if (cdev->drv && cdev->drv->shutdown)
1676 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001677 disable_cmf(cdev);
Cornelia Huck958974f2007-10-12 16:11:21 +02001678}
1679
Sebastian Ott823d4942009-06-16 10:30:20 +02001680static int ccw_device_pm_prepare(struct device *dev)
1681{
1682 struct ccw_device *cdev = to_ccwdev(dev);
1683
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001684 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001685 return -EAGAIN;
1686 /* Fail while device is being set online/offline. */
1687 if (atomic_read(&cdev->private->onoff))
1688 return -EAGAIN;
1689
1690 if (cdev->online && cdev->drv && cdev->drv->prepare)
1691 return cdev->drv->prepare(cdev);
1692
1693 return 0;
1694}
1695
1696static void ccw_device_pm_complete(struct device *dev)
1697{
1698 struct ccw_device *cdev = to_ccwdev(dev);
1699
1700 if (cdev->online && cdev->drv && cdev->drv->complete)
1701 cdev->drv->complete(cdev);
1702}
1703
1704static int ccw_device_pm_freeze(struct device *dev)
1705{
1706 struct ccw_device *cdev = to_ccwdev(dev);
1707 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1708 int ret, cm_enabled;
1709
1710 /* Fail suspend while device is in transistional state. */
1711 if (!dev_fsm_final_state(cdev))
1712 return -EAGAIN;
1713 if (!cdev->online)
1714 return 0;
1715 if (cdev->drv && cdev->drv->freeze) {
1716 ret = cdev->drv->freeze(cdev);
1717 if (ret)
1718 return ret;
1719 }
1720
1721 spin_lock_irq(sch->lock);
1722 cm_enabled = cdev->private->cmb != NULL;
1723 spin_unlock_irq(sch->lock);
1724 if (cm_enabled) {
1725 /* Don't have the css write on memory. */
1726 ret = ccw_set_cmf(cdev, 0);
1727 if (ret)
1728 return ret;
1729 }
1730 /* From here on, disallow device driver I/O. */
1731 spin_lock_irq(sch->lock);
1732 ret = cio_disable_subchannel(sch);
1733 spin_unlock_irq(sch->lock);
1734
1735 return ret;
1736}
1737
1738static int ccw_device_pm_thaw(struct device *dev)
1739{
1740 struct ccw_device *cdev = to_ccwdev(dev);
1741 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1742 int ret, cm_enabled;
1743
1744 if (!cdev->online)
1745 return 0;
1746
1747 spin_lock_irq(sch->lock);
1748 /* Allow device driver I/O again. */
1749 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1750 cm_enabled = cdev->private->cmb != NULL;
1751 spin_unlock_irq(sch->lock);
1752 if (ret)
1753 return ret;
1754
1755 if (cm_enabled) {
1756 ret = ccw_set_cmf(cdev, 1);
1757 if (ret)
1758 return ret;
1759 }
1760
1761 if (cdev->drv && cdev->drv->thaw)
1762 ret = cdev->drv->thaw(cdev);
1763
1764 return ret;
1765}
1766
1767static void __ccw_device_pm_restore(struct ccw_device *cdev)
1768{
1769 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1770 int ret;
1771
1772 if (cio_is_console(sch->schid))
1773 goto out;
1774 /*
1775 * While we were sleeping, devices may have gone or become
1776 * available again. Kick re-detection.
1777 */
1778 spin_lock_irq(sch->lock);
1779 cdev->private->flags.resuming = 1;
1780 ret = ccw_device_recognition(cdev);
1781 spin_unlock_irq(sch->lock);
1782 if (ret) {
1783 CIO_MSG_EVENT(0, "Couldn't start recognition for device "
Sebastian Ottf0148242009-09-11 10:28:23 +02001784 "0.%x.%04x (ret=%d)\n",
1785 cdev->private->dev_id.ssid,
1786 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001787 spin_lock_irq(sch->lock);
1788 cdev->private->state = DEV_STATE_DISCONNECTED;
1789 spin_unlock_irq(sch->lock);
1790 /* notify driver after the resume cb */
1791 goto out;
1792 }
1793 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1794 cdev->private->state == DEV_STATE_DISCONNECTED);
1795
1796out:
1797 cdev->private->flags.resuming = 0;
1798}
1799
1800static int resume_handle_boxed(struct ccw_device *cdev)
1801{
1802 cdev->private->state = DEV_STATE_BOXED;
1803 if (ccw_device_notify(cdev, CIO_BOXED))
1804 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001805 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001806 return -ENODEV;
1807}
1808
1809static int resume_handle_disc(struct ccw_device *cdev)
1810{
1811 cdev->private->state = DEV_STATE_DISCONNECTED;
1812 if (ccw_device_notify(cdev, CIO_GONE))
1813 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001814 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001815 return -ENODEV;
1816}
1817
1818static int ccw_device_pm_restore(struct device *dev)
1819{
1820 struct ccw_device *cdev = to_ccwdev(dev);
1821 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1822 int ret = 0, cm_enabled;
1823
1824 __ccw_device_pm_restore(cdev);
1825 spin_lock_irq(sch->lock);
1826 if (cio_is_console(sch->schid)) {
1827 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1828 spin_unlock_irq(sch->lock);
1829 goto out_restore;
1830 }
1831 cdev->private->flags.donotify = 0;
1832 /* check recognition results */
1833 switch (cdev->private->state) {
1834 case DEV_STATE_OFFLINE:
1835 break;
1836 case DEV_STATE_BOXED:
1837 ret = resume_handle_boxed(cdev);
1838 spin_unlock_irq(sch->lock);
1839 if (ret)
1840 goto out;
1841 goto out_restore;
1842 case DEV_STATE_DISCONNECTED:
1843 goto out_disc_unlock;
1844 default:
1845 goto out_unreg_unlock;
1846 }
1847 /* check if the device id has changed */
1848 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001849 CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
1850 "changed from %04x to %04x)\n",
1851 sch->schid.ssid, sch->schid.sch_no,
Sebastian Ott823d4942009-06-16 10:30:20 +02001852 cdev->private->dev_id.devno,
1853 sch->schib.pmcw.dev);
1854 goto out_unreg_unlock;
1855 }
1856 /* check if the device type has changed */
1857 if (!ccw_device_test_sense_data(cdev)) {
1858 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001859 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001860 ret = -ENODEV;
1861 goto out_unlock;
1862 }
1863 if (!cdev->online) {
1864 ret = 0;
1865 goto out_unlock;
1866 }
1867 ret = ccw_device_online(cdev);
1868 if (ret)
1869 goto out_disc_unlock;
1870
1871 cm_enabled = cdev->private->cmb != NULL;
1872 spin_unlock_irq(sch->lock);
1873
1874 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1875 if (cdev->private->state != DEV_STATE_ONLINE) {
1876 spin_lock_irq(sch->lock);
1877 goto out_disc_unlock;
1878 }
1879 if (cm_enabled) {
1880 ret = ccw_set_cmf(cdev, 1);
1881 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001882 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1883 "(rc=%d)\n", cdev->private->dev_id.ssid,
1884 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001885 ret = 0;
1886 }
1887 }
1888
1889out_restore:
1890 if (cdev->online && cdev->drv && cdev->drv->restore)
1891 ret = cdev->drv->restore(cdev);
1892out:
1893 return ret;
1894
1895out_disc_unlock:
1896 ret = resume_handle_disc(cdev);
1897 spin_unlock_irq(sch->lock);
1898 if (ret)
1899 return ret;
1900 goto out_restore;
1901
1902out_unreg_unlock:
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001903 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
Sebastian Ott823d4942009-06-16 10:30:20 +02001904 ret = -ENODEV;
1905out_unlock:
1906 spin_unlock_irq(sch->lock);
1907 return ret;
1908}
1909
1910static struct dev_pm_ops ccw_pm_ops = {
1911 .prepare = ccw_device_pm_prepare,
1912 .complete = ccw_device_pm_complete,
1913 .freeze = ccw_device_pm_freeze,
1914 .thaw = ccw_device_pm_thaw,
1915 .restore = ccw_device_pm_restore,
1916};
1917
Cornelia Huck8bbace72006-01-11 10:56:22 +01001918struct bus_type ccw_bus_type = {
1919 .name = "ccw",
1920 .match = ccw_bus_match,
1921 .uevent = ccw_uevent,
1922 .probe = ccw_device_probe,
1923 .remove = ccw_device_remove,
Cornelia Huck958974f2007-10-12 16:11:21 +02001924 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02001925 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001926};
1927
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001928/**
1929 * ccw_driver_register() - register a ccw driver
1930 * @cdriver: driver to be registered
1931 *
1932 * This function is mainly a wrapper around driver_register().
1933 * Returns:
1934 * %0 on success and a negative error value on failure.
1935 */
1936int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct device_driver *drv = &cdriver->driver;
1939
1940 drv->bus = &ccw_bus_type;
1941 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001942 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 return driver_register(drv);
1945}
1946
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001947/**
1948 * ccw_driver_unregister() - deregister a ccw driver
1949 * @cdriver: driver to be deregistered
1950 *
1951 * This function is mainly a wrapper around driver_unregister().
1952 */
1953void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 driver_unregister(&cdriver->driver);
1956}
1957
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001958/* Helper func for qdio. */
1959struct subchannel_id
1960ccw_device_get_subchannel_id(struct ccw_device *cdev)
1961{
1962 struct subchannel *sch;
1963
1964 sch = to_subchannel(cdev->dev.parent);
1965 return sch->schid;
1966}
1967
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001968static void ccw_device_todo(struct work_struct *work)
1969{
1970 struct ccw_device_private *priv;
1971 struct ccw_device *cdev;
1972 struct subchannel *sch;
1973 enum cdev_todo todo;
1974
1975 priv = container_of(work, struct ccw_device_private, todo_work);
1976 cdev = priv->cdev;
1977 sch = to_subchannel(cdev->dev.parent);
1978 /* Find out todo. */
1979 spin_lock_irq(cdev->ccwlock);
1980 todo = priv->todo;
1981 priv->todo = CDEV_TODO_NOTHING;
1982 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1983 priv->dev_id.ssid, priv->dev_id.devno, todo);
1984 spin_unlock_irq(cdev->ccwlock);
1985 /* Perform todo. */
1986 switch (todo) {
1987 case CDEV_TODO_ENABLE_CMF:
1988 cmf_reenable(cdev);
1989 break;
1990 case CDEV_TODO_REBIND:
1991 ccw_device_do_unbind_bind(cdev);
1992 break;
1993 case CDEV_TODO_REGISTER:
1994 io_subchannel_register(cdev);
1995 break;
1996 case CDEV_TODO_UNREG_EVAL:
1997 if (!sch_is_pseudo_sch(sch))
1998 css_schedule_eval(sch->schid);
1999 /* fall-through */
2000 case CDEV_TODO_UNREG:
2001 if (sch_is_pseudo_sch(sch))
2002 ccw_device_unregister(cdev);
2003 else
2004 ccw_device_call_sch_unregister(cdev);
2005 break;
2006 default:
2007 break;
2008 }
2009 /* Release workqueue ref. */
2010 put_device(&cdev->dev);
2011}
2012
2013/**
2014 * ccw_device_sched_todo - schedule ccw device operation
2015 * @cdev: ccw device
2016 * @todo: todo
2017 *
2018 * Schedule the operation identified by @todo to be performed on the slow path
2019 * workqueue. Do nothing if another operation with higher priority is already
2020 * scheduled. Needs to be called with ccwdev lock held.
2021 */
2022void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2023{
2024 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2025 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2026 todo);
2027 if (cdev->private->todo >= todo)
2028 return;
2029 cdev->private->todo = todo;
2030 /* Get workqueue ref. */
2031 if (!get_device(&cdev->dev))
2032 return;
2033 if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
2034 /* Already queued, release workqueue ref. */
2035 put_device(&cdev->dev);
2036 }
2037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039MODULE_LICENSE("GPL");
2040EXPORT_SYMBOL(ccw_device_set_online);
2041EXPORT_SYMBOL(ccw_device_set_offline);
2042EXPORT_SYMBOL(ccw_driver_register);
2043EXPORT_SYMBOL(ccw_driver_unregister);
2044EXPORT_SYMBOL(get_ccwdev_by_busid);
2045EXPORT_SYMBOL(ccw_bus_type);
2046EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08002047EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);